1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.33.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_CORE 1
21#define SQLITE_AMALGAMATION 1
22#ifndef SQLITE_PRIVATE
23# define SQLITE_PRIVATE 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
56#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(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
62#define CTIMEOPT_VAL2(opt) 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_SCAN
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),
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__) && defined(__clang_major__)
105 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
106 CTIMEOPT_VAL(__clang_minor__) "."
107 CTIMEOPT_VAL(__clang_patchlevel__),
108#elif defined(_MSC_VER)
109 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
110#elif defined(__GNUC__) && defined(__VERSION__)
111 "COMPILER=gcc-" __VERSION__,
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_AUTOVACUUM
123 "DEFAULT_AUTOVACUUM",
124#endif
125#ifdef SQLITE_DEFAULT_CACHE_SIZE
126 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
127#endif
128#if SQLITE_DEFAULT_CKPTFULLFSYNC
129 "DEFAULT_CKPTFULLFSYNC",
130#endif
131#ifdef SQLITE_DEFAULT_FILE_FORMAT
132 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
133#endif
134#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
135 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
136#endif
137#if SQLITE_DEFAULT_FOREIGN_KEYS
138 "DEFAULT_FOREIGN_KEYS",
139#endif
140#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
141 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
142#endif
143#ifdef SQLITE_DEFAULT_LOCKING_MODE
144 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
145#endif
146#ifdef SQLITE_DEFAULT_LOOKASIDE
147 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
148#endif
149#if SQLITE_DEFAULT_MEMSTATUS
150 "DEFAULT_MEMSTATUS",
151#endif
152#ifdef SQLITE_DEFAULT_MMAP_SIZE
153 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
154#endif
155#ifdef SQLITE_DEFAULT_PAGE_SIZE
156 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
157#endif
158#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
159 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
160#endif
161#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
162 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
163#endif
164#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
165 "DEFAULT_RECURSIVE_TRIGGERS",
166#endif
167#ifdef SQLITE_DEFAULT_ROWEST
168 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
169#endif
170#ifdef SQLITE_DEFAULT_SECTOR_SIZE
171 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
172#endif
173#ifdef SQLITE_DEFAULT_SYNCHRONOUS
174 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
175#endif
176#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
177 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
178#endif
179#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
180 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
181#endif
182#ifdef SQLITE_DEFAULT_WORKER_THREADS
183 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
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),
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_BYTECODE_VTAB
222 "ENABLE_BYTECODE_VTAB",
223#endif
224#if SQLITE_ENABLE_CEROD
225 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
226#endif
227#if SQLITE_ENABLE_COLUMN_METADATA
228 "ENABLE_COLUMN_METADATA",
229#endif
230#if SQLITE_ENABLE_COLUMN_USED_MASK
231 "ENABLE_COLUMN_USED_MASK",
232#endif
233#if SQLITE_ENABLE_COSTMULT
234 "ENABLE_COSTMULT",
235#endif
236#if SQLITE_ENABLE_CURSOR_HINTS
237 "ENABLE_CURSOR_HINTS",
238#endif
239#if SQLITE_ENABLE_DBSTAT_VTAB
240 "ENABLE_DBSTAT_VTAB",
241#endif
242#if SQLITE_ENABLE_EXPENSIVE_ASSERT
243 "ENABLE_EXPENSIVE_ASSERT",
244#endif
245#if SQLITE_ENABLE_FTS1
246 "ENABLE_FTS1",
247#endif
248#if SQLITE_ENABLE_FTS2
249 "ENABLE_FTS2",
250#endif
251#if SQLITE_ENABLE_FTS3
252 "ENABLE_FTS3",
253#endif
254#if SQLITE_ENABLE_FTS3_PARENTHESIS
255 "ENABLE_FTS3_PARENTHESIS",
256#endif
257#if SQLITE_ENABLE_FTS3_TOKENIZER
258 "ENABLE_FTS3_TOKENIZER",
259#endif
260#if SQLITE_ENABLE_FTS4
261 "ENABLE_FTS4",
262#endif
263#if SQLITE_ENABLE_FTS5
264 "ENABLE_FTS5",
265#endif
266#if SQLITE_ENABLE_GEOPOLY
267 "ENABLE_GEOPOLY",
268#endif
269#if SQLITE_ENABLE_HIDDEN_COLUMNS
270 "ENABLE_HIDDEN_COLUMNS",
271#endif
272#if SQLITE_ENABLE_ICU
273 "ENABLE_ICU",
274#endif
275#if SQLITE_ENABLE_IOTRACE
276 "ENABLE_IOTRACE",
277#endif
278#if SQLITE_ENABLE_JSON1
279 "ENABLE_JSON1",
280#endif
281#if SQLITE_ENABLE_LOAD_EXTENSION
282 "ENABLE_LOAD_EXTENSION",
283#endif
284#ifdef SQLITE_ENABLE_LOCKING_STYLE
285 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
286#endif
287#if SQLITE_ENABLE_MEMORY_MANAGEMENT
288 "ENABLE_MEMORY_MANAGEMENT",
289#endif
290#if SQLITE_ENABLE_MEMSYS3
291 "ENABLE_MEMSYS3",
292#endif
293#if SQLITE_ENABLE_MEMSYS5
294 "ENABLE_MEMSYS5",
295#endif
296#if SQLITE_ENABLE_MULTIPLEX
297 "ENABLE_MULTIPLEX",
298#endif
299#if SQLITE_ENABLE_NORMALIZE
300 "ENABLE_NORMALIZE",
301#endif
302#if SQLITE_ENABLE_NULL_TRIM
303 "ENABLE_NULL_TRIM",
304#endif
305#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
306 "ENABLE_OVERSIZE_CELL_CHECK",
307#endif
308#if SQLITE_ENABLE_PREUPDATE_HOOK
309 "ENABLE_PREUPDATE_HOOK",
310#endif
311#if SQLITE_ENABLE_QPSG
312 "ENABLE_QPSG",
313#endif
314#if SQLITE_ENABLE_RBU
315 "ENABLE_RBU",
316#endif
317#if SQLITE_ENABLE_RTREE
318 "ENABLE_RTREE",
319#endif
320#if SQLITE_ENABLE_SELECTTRACE
321 "ENABLE_SELECTTRACE",
322#endif
323#if SQLITE_ENABLE_SESSION
324 "ENABLE_SESSION",
325#endif
326#if SQLITE_ENABLE_SNAPSHOT
327 "ENABLE_SNAPSHOT",
328#endif
329#if SQLITE_ENABLE_SORTER_REFERENCES
330 "ENABLE_SORTER_REFERENCES",
331#endif
332#if SQLITE_ENABLE_SQLLOG
333 "ENABLE_SQLLOG",
334#endif
335#if defined(SQLITE_ENABLE_STAT4)
336 "ENABLE_STAT4",
337#endif
338#if SQLITE_ENABLE_STMTVTAB
339 "ENABLE_STMTVTAB",
340#endif
341#if SQLITE_ENABLE_STMT_SCANSTATUS
342 "ENABLE_STMT_SCANSTATUS",
343#endif
344#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
345 "ENABLE_UNKNOWN_SQL_FUNCTION",
346#endif
347#if SQLITE_ENABLE_UNLOCK_NOTIFY
348 "ENABLE_UNLOCK_NOTIFY",
349#endif
350#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
351 "ENABLE_UPDATE_DELETE_LIMIT",
352#endif
353#if SQLITE_ENABLE_URI_00_ERROR
354 "ENABLE_URI_00_ERROR",
355#endif
356#if SQLITE_ENABLE_VFSTRACE
357 "ENABLE_VFSTRACE",
358#endif
359#if SQLITE_ENABLE_WHERETRACE
360 "ENABLE_WHERETRACE",
361#endif
362#if SQLITE_ENABLE_ZIPVFS
363 "ENABLE_ZIPVFS",
364#endif
365#if SQLITE_EXPLAIN_ESTIMATED_ROWS
366 "EXPLAIN_ESTIMATED_ROWS",
367#endif
368#if SQLITE_EXTRA_IFNULLROW
369 "EXTRA_IFNULLROW",
370#endif
371#ifdef SQLITE_EXTRA_INIT
372 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
373#endif
374#ifdef SQLITE_EXTRA_SHUTDOWN
375 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
376#endif
377#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
378 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
379#endif
380#if SQLITE_FTS5_ENABLE_TEST_MI
381 "FTS5_ENABLE_TEST_MI",
382#endif
383#if SQLITE_FTS5_NO_WITHOUT_ROWID
384 "FTS5_NO_WITHOUT_ROWID",
385#endif
386#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
387 "HAVE_ISNAN",
388#endif
389#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
390 "HOMEGROWN_RECURSIVE_MUTEX",
391#endif
392#if SQLITE_IGNORE_AFP_LOCK_ERRORS
393 "IGNORE_AFP_LOCK_ERRORS",
394#endif
395#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
396 "IGNORE_FLOCK_LOCK_ERRORS",
397#endif
398#if SQLITE_INLINE_MEMCPY
399 "INLINE_MEMCPY",
400#endif
401#if SQLITE_INT64_TYPE
402 "INT64_TYPE",
403#endif
404#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
405 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
406#endif
407#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
408 "LIKE_DOESNT_MATCH_BLOBS",
409#endif
410#if SQLITE_LOCK_TRACE
411 "LOCK_TRACE",
412#endif
413#if SQLITE_LOG_CACHE_SPILL
414 "LOG_CACHE_SPILL",
415#endif
416#ifdef SQLITE_MALLOC_SOFT_LIMIT
417 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
418#endif
419#ifdef SQLITE_MAX_ATTACHED
420 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
421#endif
422#ifdef SQLITE_MAX_COLUMN
423 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
424#endif
425#ifdef SQLITE_MAX_COMPOUND_SELECT
426 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
427#endif
428#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
429 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
430#endif
431#ifdef SQLITE_MAX_EXPR_DEPTH
432 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
433#endif
434#ifdef SQLITE_MAX_FUNCTION_ARG
435 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
436#endif
437#ifdef SQLITE_MAX_LENGTH
438 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
439#endif
440#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
441 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
442#endif
443#ifdef SQLITE_MAX_MEMORY
444 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
445#endif
446#ifdef SQLITE_MAX_MMAP_SIZE
447 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
448#endif
449#ifdef SQLITE_MAX_MMAP_SIZE_
450 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
451#endif
452#ifdef SQLITE_MAX_PAGE_COUNT
453 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
454#endif
455#ifdef SQLITE_MAX_PAGE_SIZE
456 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
457#endif
458#ifdef SQLITE_MAX_SCHEMA_RETRY
459 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
460#endif
461#ifdef SQLITE_MAX_SQL_LENGTH
462 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
463#endif
464#ifdef SQLITE_MAX_TRIGGER_DEPTH
465 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
466#endif
467#ifdef SQLITE_MAX_VARIABLE_NUMBER
468 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
469#endif
470#ifdef SQLITE_MAX_VDBE_OP
471 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
472#endif
473#ifdef SQLITE_MAX_WORKER_THREADS
474 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
475#endif
476#if SQLITE_MEMDEBUG
477 "MEMDEBUG",
478#endif
479#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
480 "MIXED_ENDIAN_64BIT_FLOAT",
481#endif
482#if SQLITE_MMAP_READWRITE
483 "MMAP_READWRITE",
484#endif
485#if SQLITE_MUTEX_NOOP
486 "MUTEX_NOOP",
487#endif
488#if SQLITE_MUTEX_NREF
489 "MUTEX_NREF",
490#endif
491#if SQLITE_MUTEX_OMIT
492 "MUTEX_OMIT",
493#endif
494#if SQLITE_MUTEX_PTHREADS
495 "MUTEX_PTHREADS",
496#endif
497#if SQLITE_MUTEX_W32
498 "MUTEX_W32",
499#endif
500#if SQLITE_NEED_ERR_NAME
501 "NEED_ERR_NAME",
502#endif
503#if SQLITE_NOINLINE
504 "NOINLINE",
505#endif
506#if SQLITE_NO_SYNC
507 "NO_SYNC",
508#endif
509#if SQLITE_OMIT_ALTERTABLE
510 "OMIT_ALTERTABLE",
511#endif
512#if SQLITE_OMIT_ANALYZE
513 "OMIT_ANALYZE",
514#endif
515#if SQLITE_OMIT_ATTACH
516 "OMIT_ATTACH",
517#endif
518#if SQLITE_OMIT_AUTHORIZATION
519 "OMIT_AUTHORIZATION",
520#endif
521#if SQLITE_OMIT_AUTOINCREMENT
522 "OMIT_AUTOINCREMENT",
523#endif
524#if SQLITE_OMIT_AUTOINIT
525 "OMIT_AUTOINIT",
526#endif
527#if SQLITE_OMIT_AUTOMATIC_INDEX
528 "OMIT_AUTOMATIC_INDEX",
529#endif
530#if SQLITE_OMIT_AUTORESET
531 "OMIT_AUTORESET",
532#endif
533#if SQLITE_OMIT_AUTOVACUUM
534 "OMIT_AUTOVACUUM",
535#endif
536#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
537 "OMIT_BETWEEN_OPTIMIZATION",
538#endif
539#if SQLITE_OMIT_BLOB_LITERAL
540 "OMIT_BLOB_LITERAL",
541#endif
542#if SQLITE_OMIT_CAST
543 "OMIT_CAST",
544#endif
545#if SQLITE_OMIT_CHECK
546 "OMIT_CHECK",
547#endif
548#if SQLITE_OMIT_COMPLETE
549 "OMIT_COMPLETE",
550#endif
551#if SQLITE_OMIT_COMPOUND_SELECT
552 "OMIT_COMPOUND_SELECT",
553#endif
554#if SQLITE_OMIT_CONFLICT_CLAUSE
555 "OMIT_CONFLICT_CLAUSE",
556#endif
557#if SQLITE_OMIT_CTE
558 "OMIT_CTE",
559#endif
560#if SQLITE_OMIT_DATETIME_FUNCS
561 "OMIT_DATETIME_FUNCS",
562#endif
563#if SQLITE_OMIT_DECLTYPE
564 "OMIT_DECLTYPE",
565#endif
566#if SQLITE_OMIT_DEPRECATED
567 "OMIT_DEPRECATED",
568#endif
569#if SQLITE_OMIT_DISKIO
570 "OMIT_DISKIO",
571#endif
572#if SQLITE_OMIT_EXPLAIN
573 "OMIT_EXPLAIN",
574#endif
575#if SQLITE_OMIT_FLAG_PRAGMAS
576 "OMIT_FLAG_PRAGMAS",
577#endif
578#if SQLITE_OMIT_FLOATING_POINT
579 "OMIT_FLOATING_POINT",
580#endif
581#if SQLITE_OMIT_FOREIGN_KEY
582 "OMIT_FOREIGN_KEY",
583#endif
584#if SQLITE_OMIT_GET_TABLE
585 "OMIT_GET_TABLE",
586#endif
587#if SQLITE_OMIT_HEX_INTEGER
588 "OMIT_HEX_INTEGER",
589#endif
590#if SQLITE_OMIT_INCRBLOB
591 "OMIT_INCRBLOB",
592#endif
593#if SQLITE_OMIT_INTEGRITY_CHECK
594 "OMIT_INTEGRITY_CHECK",
595#endif
596#if SQLITE_OMIT_LIKE_OPTIMIZATION
597 "OMIT_LIKE_OPTIMIZATION",
598#endif
599#if SQLITE_OMIT_LOAD_EXTENSION
600 "OMIT_LOAD_EXTENSION",
601#endif
602#if SQLITE_OMIT_LOCALTIME
603 "OMIT_LOCALTIME",
604#endif
605#if SQLITE_OMIT_LOOKASIDE
606 "OMIT_LOOKASIDE",
607#endif
608#if SQLITE_OMIT_MEMORYDB
609 "OMIT_MEMORYDB",
610#endif
611#if SQLITE_OMIT_OR_OPTIMIZATION
612 "OMIT_OR_OPTIMIZATION",
613#endif
614#if SQLITE_OMIT_PAGER_PRAGMAS
615 "OMIT_PAGER_PRAGMAS",
616#endif
617#if SQLITE_OMIT_PARSER_TRACE
618 "OMIT_PARSER_TRACE",
619#endif
620#if SQLITE_OMIT_POPEN
621 "OMIT_POPEN",
622#endif
623#if SQLITE_OMIT_PRAGMA
624 "OMIT_PRAGMA",
625#endif
626#if SQLITE_OMIT_PROGRESS_CALLBACK
627 "OMIT_PROGRESS_CALLBACK",
628#endif
629#if SQLITE_OMIT_QUICKBALANCE
630 "OMIT_QUICKBALANCE",
631#endif
632#if SQLITE_OMIT_REINDEX
633 "OMIT_REINDEX",
634#endif
635#if SQLITE_OMIT_SCHEMA_PRAGMAS
636 "OMIT_SCHEMA_PRAGMAS",
637#endif
638#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
639 "OMIT_SCHEMA_VERSION_PRAGMAS",
640#endif
641#if SQLITE_OMIT_SHARED_CACHE
642 "OMIT_SHARED_CACHE",
643#endif
644#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
645 "OMIT_SHUTDOWN_DIRECTORIES",
646#endif
647#if SQLITE_OMIT_SUBQUERY
648 "OMIT_SUBQUERY",
649#endif
650#if SQLITE_OMIT_TCL_VARIABLE
651 "OMIT_TCL_VARIABLE",
652#endif
653#if SQLITE_OMIT_TEMPDB
654 "OMIT_TEMPDB",
655#endif
656#if SQLITE_OMIT_TEST_CONTROL
657 "OMIT_TEST_CONTROL",
658#endif
659#if SQLITE_OMIT_TRACE
660 "OMIT_TRACE",
661#endif
662#if SQLITE_OMIT_TRIGGER
663 "OMIT_TRIGGER",
664#endif
665#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
666 "OMIT_TRUNCATE_OPTIMIZATION",
667#endif
668#if SQLITE_OMIT_UTF16
669 "OMIT_UTF16",
670#endif
671#if SQLITE_OMIT_VACUUM
672 "OMIT_VACUUM",
673#endif
674#if SQLITE_OMIT_VIEW
675 "OMIT_VIEW",
676#endif
677#if SQLITE_OMIT_VIRTUALTABLE
678 "OMIT_VIRTUALTABLE",
679#endif
680#if SQLITE_OMIT_WAL
681 "OMIT_WAL",
682#endif
683#if SQLITE_OMIT_WSD
684 "OMIT_WSD",
685#endif
686#if SQLITE_OMIT_XFER_OPT
687 "OMIT_XFER_OPT",
688#endif
689#if SQLITE_PCACHE_SEPARATE_HEADER
690 "PCACHE_SEPARATE_HEADER",
691#endif
692#if SQLITE_PERFORMANCE_TRACE
693 "PERFORMANCE_TRACE",
694#endif
695#if SQLITE_POWERSAFE_OVERWRITE
696 "POWERSAFE_OVERWRITE",
697#endif
698#if SQLITE_PREFER_PROXY_LOCKING
699 "PREFER_PROXY_LOCKING",
700#endif
701#if SQLITE_PROXY_DEBUG
702 "PROXY_DEBUG",
703#endif
704#if SQLITE_REVERSE_UNORDERED_SELECTS
705 "REVERSE_UNORDERED_SELECTS",
706#endif
707#if SQLITE_RTREE_INT_ONLY
708 "RTREE_INT_ONLY",
709#endif
710#if SQLITE_SECURE_DELETE
711 "SECURE_DELETE",
712#endif
713#if SQLITE_SMALL_STACK
714 "SMALL_STACK",
715#endif
716#ifdef SQLITE_SORTER_PMASZ
717 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
718#endif
719#if SQLITE_SOUNDEX
720 "SOUNDEX",
721#endif
722#ifdef SQLITE_STAT4_SAMPLES
723 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
724#endif
725#ifdef SQLITE_STMTJRNL_SPILL
726 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
727#endif
728#if SQLITE_SUBSTR_COMPATIBILITY
729 "SUBSTR_COMPATIBILITY",
730#endif
731#if SQLITE_SYSTEM_MALLOC
732 "SYSTEM_MALLOC",
733#endif
734#if SQLITE_TCL
735 "TCL",
736#endif
737#ifdef SQLITE_TEMP_STORE
738 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
739#endif
740#if SQLITE_TEST
741 "TEST",
742#endif
743#if defined(SQLITE_THREADSAFE)
744 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
745#elif defined(THREADSAFE)
746 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
747#else
748 "THREADSAFE=1",
749#endif
750#if SQLITE_UNLINK_AFTER_CLOSE
751 "UNLINK_AFTER_CLOSE",
752#endif
753#if SQLITE_UNTESTABLE
754 "UNTESTABLE",
755#endif
756#if SQLITE_USER_AUTHENTICATION
757 "USER_AUTHENTICATION",
758#endif
759#if SQLITE_USE_ALLOCA
760 "USE_ALLOCA",
761#endif
762#if SQLITE_USE_FCNTL_TRACE
763 "USE_FCNTL_TRACE",
764#endif
765#if SQLITE_USE_URI
766 "USE_URI",
767#endif
768#if SQLITE_VDBE_COVERAGE
769 "VDBE_COVERAGE",
770#endif
771#if SQLITE_WIN32_MALLOC
772 "WIN32_MALLOC",
773#endif
774#if SQLITE_ZERO_MALLOC
775 "ZERO_MALLOC",
776#endif
777/*
778** END CODE GENERATED BY tool/mkctime.tcl
779*/
780};
781
782SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
783 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
784 return (const char**)sqlite3azCompileOpt;
785}
786
787#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
788
789/************** End of ctime.c ***********************************************/
790/************** Begin file sqliteInt.h ***************************************/
791/*
792** 2001 September 15
793**
794** The author disclaims copyright to this source code. In place of
795** a legal notice, here is a blessing:
796**
797** May you do good and not evil.
798** May you find forgiveness for yourself and forgive others.
799** May you share freely, never taking more than you give.
800**
801*************************************************************************
802** Internal interface definitions for SQLite.
803**
804*/
805#ifndef SQLITEINT_H
806#define SQLITEINT_H
807
808/* Special Comments:
809**
810** Some comments have special meaning to the tools that measure test
811** coverage:
812**
813** NO_TEST - The branches on this line are not
814** measured by branch coverage. This is
815** used on lines of code that actually
816** implement parts of coverage testing.
817**
818** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
819** and the correct answer is still obtained,
820** though perhaps more slowly.
821**
822** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
823** and the correct answer is still obtained,
824** though perhaps more slowly.
825**
826** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
827** that would be harmless and undetectable
828** if it did occur.
829**
830** In all cases, the special comment must be enclosed in the usual
831** slash-asterisk...asterisk-slash comment marks, with no spaces between the
832** asterisks and the comment text.
833*/
834
835/*
836** Make sure the Tcl calling convention macro is defined. This macro is
837** only used by test code and Tcl integration code.
838*/
839#ifndef SQLITE_TCLAPI
840# define SQLITE_TCLAPI
841#endif
842
843/*
844** Include the header file used to customize the compiler options for MSVC.
845** This should be done first so that it can successfully prevent spurious
846** compiler warnings due to subsequent content in this file and other files
847** that are included by this file.
848*/
849/************** Include msvc.h in the middle of sqliteInt.h ******************/
850/************** Begin file msvc.h ********************************************/
851/*
852** 2015 January 12
853**
854** The author disclaims copyright to this source code. In place of
855** a legal notice, here is a blessing:
856**
857** May you do good and not evil.
858** May you find forgiveness for yourself and forgive others.
859** May you share freely, never taking more than you give.
860**
861******************************************************************************
862**
863** This file contains code that is specific to MSVC.
864*/
865#ifndef SQLITE_MSVC_H
866#define SQLITE_MSVC_H
867
868#if defined(_MSC_VER)
869#pragma warning(disable : 4054)
870#pragma warning(disable : 4055)
871#pragma warning(disable : 4100)
872#pragma warning(disable : 4127)
873#pragma warning(disable : 4130)
874#pragma warning(disable : 4152)
875#pragma warning(disable : 4189)
876#pragma warning(disable : 4206)
877#pragma warning(disable : 4210)
878#pragma warning(disable : 4232)
879#pragma warning(disable : 4244)
880#pragma warning(disable : 4305)
881#pragma warning(disable : 4306)
882#pragma warning(disable : 4702)
883#pragma warning(disable : 4706)
884#endif /* defined(_MSC_VER) */
885
886#if defined(_MSC_VER) && !defined(_WIN64)
887#undef SQLITE_4_BYTE_ALIGNED_MALLOC
888#define SQLITE_4_BYTE_ALIGNED_MALLOC
889#endif /* defined(_MSC_VER) && !defined(_WIN64) */
890
891#endif /* SQLITE_MSVC_H */
892
893/************** End of msvc.h ************************************************/
894/************** Continuing where we left off in sqliteInt.h ******************/
895
896/*
897** Special setup for VxWorks
898*/
899/************** Include vxworks.h in the middle of sqliteInt.h ***************/
900/************** Begin file vxworks.h *****************************************/
901/*
902** 2015-03-02
903**
904** The author disclaims copyright to this source code. In place of
905** a legal notice, here is a blessing:
906**
907** May you do good and not evil.
908** May you find forgiveness for yourself and forgive others.
909** May you share freely, never taking more than you give.
910**
911******************************************************************************
912**
913** This file contains code that is specific to Wind River's VxWorks
914*/
915#if defined(__RTP__) || defined(_WRS_KERNEL)
916/* This is VxWorks. Set up things specially for that OS
917*/
918#include <vxWorks.h>
919#include <pthread.h> /* amalgamator: dontcache */
920#define OS_VXWORKS 1
921#define SQLITE_OS_OTHER 0
922#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
923#define SQLITE_OMIT_LOAD_EXTENSION 1
924#define SQLITE_ENABLE_LOCKING_STYLE 0
925#define HAVE_UTIME 1
926#else
927/* This is not VxWorks. */
928#define OS_VXWORKS 0
929#define HAVE_FCHOWN 1
930#define HAVE_READLINK 1
931#define HAVE_LSTAT 1
932#endif /* defined(_WRS_KERNEL) */
933
934/************** End of vxworks.h *********************************************/
935/************** Continuing where we left off in sqliteInt.h ******************/
936
937/*
938** These #defines should enable >2GB file support on POSIX if the
939** underlying operating system supports it. If the OS lacks
940** large file support, or if the OS is windows, these should be no-ops.
941**
942** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
943** system #includes. Hence, this block of code must be the very first
944** code in all source files.
945**
946** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
947** on the compiler command line. This is necessary if you are compiling
948** on a recent machine (ex: Red Hat 7.2) but you want your code to work
949** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
950** without this option, LFS is enable. But LFS does not exist in the kernel
951** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
952** portability you should omit LFS.
953**
954** The previous paragraph was written in 2005. (This paragraph is written
955** on 2008-11-28.) These days, all Linux kernels support large files, so
956** you should probably leave LFS enabled. But some embedded platforms might
957** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
958**
959** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
960*/
961#ifndef SQLITE_DISABLE_LFS
962# define _LARGE_FILE 1
963# ifndef _FILE_OFFSET_BITS
964# define _FILE_OFFSET_BITS 64
965# endif
966# define _LARGEFILE_SOURCE 1
967#endif
968
969/* The GCC_VERSION and MSVC_VERSION macros are used to
970** conditionally include optimizations for each of these compilers. A
971** value of 0 means that compiler is not being used. The
972** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
973** optimizations, and hence set all compiler macros to 0
974**
975** There was once also a CLANG_VERSION macro. However, we learn that the
976** version numbers in clang are for "marketing" only and are inconsistent
977** and unreliable. Fortunately, all versions of clang also recognize the
978** gcc version numbers and have reasonable settings for gcc version numbers,
979** so the GCC_VERSION macro will be set to a correct non-zero value even
980** when compiling with clang.
981*/
982#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
983# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
984#else
985# define GCC_VERSION 0
986#endif
987#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
988# define MSVC_VERSION _MSC_VER
989#else
990# define MSVC_VERSION 0
991#endif
992
993/* Needed for various definitions... */
994#if defined(__GNUC__) && !defined(_GNU_SOURCE)
995# define _GNU_SOURCE
996#endif
997
998#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
999# define _BSD_SOURCE
1000#endif
1001
1002/*
1003** Macro to disable warnings about missing "break" at the end of a "case".
1004*/
1005#if GCC_VERSION>=7000000
1006# define deliberate_fall_through __attribute__((fallthrough));
1007#else
1008# define deliberate_fall_through
1009#endif
1010
1011/*
1012** For MinGW, check to see if we can include the header file containing its
1013** version information, among other things. Normally, this internal MinGW
1014** header file would [only] be included automatically by other MinGW header
1015** files; however, the contained version information is now required by this
1016** header file to work around binary compatibility issues (see below) and
1017** this is the only known way to reliably obtain it. This entire #if block
1018** would be completely unnecessary if there was any other way of detecting
1019** MinGW via their preprocessor (e.g. if they customized their GCC to define
1020** some MinGW-specific macros). When compiling for MinGW, either the
1021** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
1022** defined; otherwise, detection of conditions specific to MinGW will be
1023** disabled.
1024*/
1025#if defined(_HAVE_MINGW_H)
1026# include "mingw.h"
1027#elif defined(_HAVE__MINGW_H)
1028# include "_mingw.h"
1029#endif
1030
1031/*
1032** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
1033** define is required to maintain binary compatibility with the MSVC runtime
1034** library in use (e.g. for Windows XP).
1035*/
1036#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
1037 defined(_WIN32) && !defined(_WIN64) && \
1038 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
1039 defined(__MSVCRT__)
1040# define _USE_32BIT_TIME_T
1041#endif
1042
1043/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
1044** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
1045** MinGW.
1046*/
1047/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
1048/************** Begin file sqlite3.h *****************************************/
1049/*
1050** 2001-09-15
1051**
1052** The author disclaims copyright to this source code. In place of
1053** a legal notice, here is a blessing:
1054**
1055** May you do good and not evil.
1056** May you find forgiveness for yourself and forgive others.
1057** May you share freely, never taking more than you give.
1058**
1059*************************************************************************
1060** This header file defines the interface that the SQLite library
1061** presents to client programs. If a C-function, structure, datatype,
1062** or constant definition does not appear in this file, then it is
1063** not a published API of SQLite, is subject to change without
1064** notice, and should not be referenced by programs that use SQLite.
1065**
1066** Some of the definitions that are in this file are marked as
1067** "experimental". Experimental interfaces are normally new
1068** features recently added to SQLite. We do not anticipate changes
1069** to experimental interfaces but reserve the right to make minor changes
1070** if experience from use "in the wild" suggest such changes are prudent.
1071**
1072** The official C-language API documentation for SQLite is derived
1073** from comments in this file. This file is the authoritative source
1074** on how SQLite interfaces are supposed to operate.
1075**
1076** The name of this file under configuration management is "sqlite.h.in".
1077** The makefile makes some minor changes to this file (such as inserting
1078** the version number) and changes its name to "sqlite3.h" as
1079** part of the build process.
1080*/
1081#ifndef SQLITE3_H
1082#define SQLITE3_H
1083#include <stdarg.h> /* Needed for the definition of va_list */
1084
1085/*
1086** Make sure we can call this stuff from C++.
1087*/
1088#if 0
1089extern "C" {
1090#endif
1091
1092
1093/*
1094** Provide the ability to override linkage features of the interface.
1095*/
1096#ifndef SQLITE_EXTERN
1097# define SQLITE_EXTERN extern
1098#endif
1099#ifndef SQLITE_API
1100# define SQLITE_API
1101#endif
1102#ifndef SQLITE_CDECL
1103# define SQLITE_CDECL
1104#endif
1105#ifndef SQLITE_APICALL
1106# define SQLITE_APICALL
1107#endif
1108#ifndef SQLITE_STDCALL
1109# define SQLITE_STDCALL SQLITE_APICALL
1110#endif
1111#ifndef SQLITE_CALLBACK
1112# define SQLITE_CALLBACK
1113#endif
1114#ifndef SQLITE_SYSAPI
1115# define SQLITE_SYSAPI
1116#endif
1117
1118/*
1119** These no-op macros are used in front of interfaces to mark those
1120** interfaces as either deprecated or experimental. New applications
1121** should not use deprecated interfaces - they are supported for backwards
1122** compatibility only. Application writers should be aware that
1123** experimental interfaces are subject to change in point releases.
1124**
1125** These macros used to resolve to various kinds of compiler magic that
1126** would generate warning messages when they were used. But that
1127** compiler magic ended up generating such a flurry of bug reports
1128** that we have taken it all out and gone back to using simple
1129** noop macros.
1130*/
1131#define SQLITE_DEPRECATED
1132#define SQLITE_EXPERIMENTAL
1133
1134/*
1135** Ensure these symbols were not defined by some previous header file.
1136*/
1137#ifdef SQLITE_VERSION
1138# undef SQLITE_VERSION
1139#endif
1140#ifdef SQLITE_VERSION_NUMBER
1141# undef SQLITE_VERSION_NUMBER
1142#endif
1143
1144/*
1145** CAPI3REF: Compile-Time Library Version Numbers
1146**
1147** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
1148** evaluates to a string literal that is the SQLite version in the
1149** format "X.Y.Z" where X is the major version number (always 3 for
1150** SQLite3) and Y is the minor version number and Z is the release number.)^
1151** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
1152** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
1153** numbers used in [SQLITE_VERSION].)^
1154** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
1155** be larger than the release from which it is derived. Either Y will
1156** be held constant and Z will be incremented or else Y will be incremented
1157** and Z will be reset to zero.
1158**
1159** Since [version 3.6.18] ([dateof:3.6.18]),
1160** SQLite source code has been stored in the
1161** <a href="http://www.fossil-scm.org/">Fossil configuration management
1162** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
1163** a string which identifies a particular check-in of SQLite
1164** within its configuration management system. ^The SQLITE_SOURCE_ID
1165** string contains the date and time of the check-in (UTC) and a SHA1
1166** or SHA3-256 hash of the entire source tree. If the source code has
1167** been edited in any way since it was last checked in, then the last
1168** four hexadecimal digits of the hash may be modified.
1169**
1170** See also: [sqlite3_libversion()],
1171** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1172** [sqlite_version()] and [sqlite_source_id()].
1173*/
1174#define SQLITE_VERSION "3.33.0"
1175#define SQLITE_VERSION_NUMBER 3033000
1176#define SQLITE_SOURCE_ID "2020-08-14 13:23:32 fca8dc8b578f215a969cd899336378966156154710873e68b3d9ac5881b0ff3f"
1177
1178/*
1179** CAPI3REF: Run-Time Library Version Numbers
1180** KEYWORDS: sqlite3_version sqlite3_sourceid
1181**
1182** These interfaces provide the same information as the [SQLITE_VERSION],
1183** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
1184** but are associated with the library instead of the header file. ^(Cautious
1185** programmers might include assert() statements in their application to
1186** verify that values returned by these interfaces match the macros in
1187** the header, and thus ensure that the application is
1188** compiled with matching library and header files.
1189**
1190** <blockquote><pre>
1191** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
1192** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
1193** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
1194** </pre></blockquote>)^
1195**
1196** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
1197** macro. ^The sqlite3_libversion() function returns a pointer to the
1198** to the sqlite3_version[] string constant. The sqlite3_libversion()
1199** function is provided for use in DLLs since DLL users usually do not have
1200** direct access to string constants within the DLL. ^The
1201** sqlite3_libversion_number() function returns an integer equal to
1202** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
1203** a pointer to a string constant whose value is the same as the
1204** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
1205** using an edited copy of [the amalgamation], then the last four characters
1206** of the hash might be different from [SQLITE_SOURCE_ID].)^
1207**
1208** See also: [sqlite_version()] and [sqlite_source_id()].
1209*/
1210SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
1211SQLITE_API const char *sqlite3_libversion(void);
1212SQLITE_API const char *sqlite3_sourceid(void);
1213SQLITE_API int sqlite3_libversion_number(void);
1214
1215/*
1216** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1217**
1218** ^The sqlite3_compileoption_used() function returns 0 or 1
1219** indicating whether the specified option was defined at
1220** compile time. ^The SQLITE_ prefix may be omitted from the
1221** option name passed to sqlite3_compileoption_used().
1222**
1223** ^The sqlite3_compileoption_get() function allows iterating
1224** over the list of options that were defined at compile time by
1225** returning the N-th compile time option string. ^If N is out of range,
1226** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
1227** prefix is omitted from any strings returned by
1228** sqlite3_compileoption_get().
1229**
1230** ^Support for the diagnostic functions sqlite3_compileoption_used()
1231** and sqlite3_compileoption_get() may be omitted by specifying the
1232** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
1233**
1234** See also: SQL functions [sqlite_compileoption_used()] and
1235** [sqlite_compileoption_get()] and the [compile_options pragma].
1236*/
1237#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1238SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
1239SQLITE_API const char *sqlite3_compileoption_get(int N);
1240#else
1241# define sqlite3_compileoption_used(X) 0
1242# define sqlite3_compileoption_get(X) ((void*)0)
1243#endif
1244
1245/*
1246** CAPI3REF: Test To See If The Library Is Threadsafe
1247**
1248** ^The sqlite3_threadsafe() function returns zero if and only if
1249** SQLite was compiled with mutexing code omitted due to the
1250** [SQLITE_THREADSAFE] compile-time option being set to 0.
1251**
1252** SQLite can be compiled with or without mutexes. When
1253** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
1254** are enabled and SQLite is threadsafe. When the
1255** [SQLITE_THREADSAFE] macro is 0,
1256** the mutexes are omitted. Without the mutexes, it is not safe
1257** to use SQLite concurrently from more than one thread.
1258**
1259** Enabling mutexes incurs a measurable performance penalty.
1260** So if speed is of utmost importance, it makes sense to disable
1261** the mutexes. But for maximum safety, mutexes should be enabled.
1262** ^The default behavior is for mutexes to be enabled.
1263**
1264** This interface can be used by an application to make sure that the
1265** version of SQLite that it is linking against was compiled with
1266** the desired setting of the [SQLITE_THREADSAFE] macro.
1267**
1268** This interface only reports on the compile-time mutex setting
1269** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
1270** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
1271** can be fully or partially disabled using a call to [sqlite3_config()]
1272** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
1273** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
1274** sqlite3_threadsafe() function shows only the compile-time setting of
1275** thread safety, not any run-time changes to that setting made by
1276** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
1277** is unchanged by calls to sqlite3_config().)^
1278**
1279** See the [threading mode] documentation for additional information.
1280*/
1281SQLITE_API int sqlite3_threadsafe(void);
1282
1283/*
1284** CAPI3REF: Database Connection Handle
1285** KEYWORDS: {database connection} {database connections}
1286**
1287** Each open SQLite database is represented by a pointer to an instance of
1288** the opaque structure named "sqlite3". It is useful to think of an sqlite3
1289** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
1290** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
1291** and [sqlite3_close_v2()] are its destructors. There are many other
1292** interfaces (such as
1293** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
1294** [sqlite3_busy_timeout()] to name but three) that are methods on an
1295** sqlite3 object.
1296*/
1297typedef struct sqlite3 sqlite3;
1298
1299/*
1300** CAPI3REF: 64-Bit Integer Types
1301** KEYWORDS: sqlite_int64 sqlite_uint64
1302**
1303** Because there is no cross-platform way to specify 64-bit integer types
1304** SQLite includes typedefs for 64-bit signed and unsigned integers.
1305**
1306** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
1307** The sqlite_int64 and sqlite_uint64 types are supported for backwards
1308** compatibility only.
1309**
1310** ^The sqlite3_int64 and sqlite_int64 types can store integer values
1311** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1312** sqlite3_uint64 and sqlite_uint64 types can store integer values
1313** between 0 and +18446744073709551615 inclusive.
1314*/
1315#ifdef SQLITE_INT64_TYPE
1316 typedef SQLITE_INT64_TYPE sqlite_int64;
1317# ifdef SQLITE_UINT64_TYPE
1318 typedef SQLITE_UINT64_TYPE sqlite_uint64;
1319# else
1320 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
1321# endif
1322#elif defined(_MSC_VER) || defined(__BORLANDC__)
1323 typedef __int64 sqlite_int64;
1324 typedef unsigned __int64 sqlite_uint64;
1325#else
1326 typedef long long int sqlite_int64;
1327 typedef unsigned long long int sqlite_uint64;
1328#endif
1329typedef sqlite_int64 sqlite3_int64;
1330typedef sqlite_uint64 sqlite3_uint64;
1331
1332/*
1333** If compiling for a processor that lacks floating point support,
1334** substitute integer for floating-point.
1335*/
1336#ifdef SQLITE_OMIT_FLOATING_POINT
1337# define double sqlite3_int64
1338#endif
1339
1340/*
1341** CAPI3REF: Closing A Database Connection
1342** DESTRUCTOR: sqlite3
1343**
1344** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
1345** for the [sqlite3] object.
1346** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
1347** the [sqlite3] object is successfully destroyed and all associated
1348** resources are deallocated.
1349**
1350** Ideally, applications should [sqlite3_finalize | finalize] all
1351** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
1352** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
1353** with the [sqlite3] object prior to attempting to close the object.
1354** ^If the database connection is associated with unfinalized prepared
1355** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
1356** sqlite3_close() will leave the database connection open and return
1357** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
1358** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
1359** it returns [SQLITE_OK] regardless, but instead of deallocating the database
1360** connection immediately, it marks the database connection as an unusable
1361** "zombie" and makes arrangements to automatically deallocate the database
1362** connection after all prepared statements are finalized, all BLOB handles
1363** are closed, and all backups have finished. The sqlite3_close_v2() interface
1364** is intended for use with host languages that are garbage collected, and
1365** where the order in which destructors are called is arbitrary.
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_OK 0 /* Successful result */
1470/* beginning-of-error-codes */
1471#define SQLITE_ERROR 1 /* Generic error */
1472#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
1473#define SQLITE_PERM 3 /* Access permission denied */
1474#define SQLITE_ABORT 4 /* Callback routine requested an abort */
1475#define SQLITE_BUSY 5 /* The database file is locked */
1476#define SQLITE_LOCKED 6 /* A table in the database is locked */
1477#define SQLITE_NOMEM 7 /* A malloc() failed */
1478#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
1479#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
1480#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
1481#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
1482#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
1483#define SQLITE_FULL 13 /* Insertion failed because database is full */
1484#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
1485#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
1486#define SQLITE_EMPTY 16 /* Internal use only */
1487#define SQLITE_SCHEMA 17 /* The database schema changed */
1488#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
1489#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
1490#define SQLITE_MISMATCH 20 /* Data type mismatch */
1491#define SQLITE_MISUSE 21 /* Library used incorrectly */
1492#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
1493#define SQLITE_AUTH 23 /* Authorization denied */
1494#define SQLITE_FORMAT 24 /* Not used */
1495#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
1496#define SQLITE_NOTADB 26 /* File opened that is not a database file */
1497#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
1498#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
1499#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
1500#define SQLITE_DONE 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 (SQLITE_ERROR | (1<<8))
1521#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
1522#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
1523#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
1524#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
1525#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
1526#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
1527#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
1528#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
1529#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
1530#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
1531#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
1532#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
1533#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
1534#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
1535#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
1536#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
1537#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
1538#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
1539#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
1540#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
1541#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
1542#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
1543#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1544#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1545#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1546#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1547#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
1548#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
1549#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8))
1550#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
1551#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
1552#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
1553#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
1554#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
1555#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1556#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
1557#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1558#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1559#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8))
1560#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1561#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1562#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1563#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1564#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
1565#define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8))
1566#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1567#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
1568#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8))
1569#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1570#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1571#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1572#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
1573#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
1574#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8))
1575#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
1576#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
1577#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
1578#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
1579#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
1580#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1581#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
1582#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
1583#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
1584#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
1585#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8))
1586#define SQLITE_CONSTRAINT_PINNED (SQLITE_CONSTRAINT |(11<<8))
1587#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
1588#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
1589#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8))
1590#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8))
1591#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8))
1592#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8))
1593
1594/*
1595** CAPI3REF: Flags For File Open Operations
1596**
1597** These bit values are intended for use in the
1598** 3rd parameter to the [sqlite3_open_v2()] interface and
1599** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1600*/
1601#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
1602#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
1603#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
1604#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
1605#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
1606#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
1607#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
1608#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
1609#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
1610#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
1611#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
1612#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
1613#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
1614#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
1615#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */
1616#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
1617#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
1618#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
1619#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
1620#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
1621#define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */
1622
1623/* Reserved: 0x00F00000 */
1624/* Legacy compatibility: */
1625#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
1626
1627
1628/*
1629** CAPI3REF: Device Characteristics
1630**
1631** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1632** object returns an integer which is a vector of these
1633** bit values expressing I/O characteristics of the mass storage
1634** device that holds the file that the [sqlite3_io_methods]
1635** refers to.
1636**
1637** The SQLITE_IOCAP_ATOMIC property means that all writes of
1638** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1639** mean that writes of blocks that are nnn bytes in size and
1640** are aligned to an address which is an integer multiple of
1641** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1642** that when data is appended to a file, the data is appended
1643** first then the size of the file is extended, never the other
1644** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1645** information is written to disk in the same order as calls
1646** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1647** after reboot following a crash or power loss, the only bytes in a
1648** file that were written at the application level might have changed
1649** and that adjacent bytes, even bytes within the same sector are
1650** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
1651** flag indicates that a file cannot be deleted when open. The
1652** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
1653** read-only media and cannot be changed even by processes with
1654** elevated privileges.
1655**
1656** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
1657** filesystem supports doing multiple write operations atomically when those
1658** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
1659** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
1660*/
1661#define SQLITE_IOCAP_ATOMIC 0x00000001
1662#define SQLITE_IOCAP_ATOMIC512 0x00000002
1663#define SQLITE_IOCAP_ATOMIC1K 0x00000004
1664#define SQLITE_IOCAP_ATOMIC2K 0x00000008
1665#define SQLITE_IOCAP_ATOMIC4K 0x00000010
1666#define SQLITE_IOCAP_ATOMIC8K 0x00000020
1667#define SQLITE_IOCAP_ATOMIC16K 0x00000040
1668#define SQLITE_IOCAP_ATOMIC32K 0x00000080
1669#define SQLITE_IOCAP_ATOMIC64K 0x00000100
1670#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
1671#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
1672#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
1673#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
1674#define SQLITE_IOCAP_IMMUTABLE 0x00002000
1675#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
1676
1677/*
1678** CAPI3REF: File Locking Levels
1679**
1680** SQLite uses one of these integer values as the second
1681** argument to calls it makes to the xLock() and xUnlock() methods
1682** of an [sqlite3_io_methods] object.
1683*/
1684#define SQLITE_LOCK_NONE 0
1685#define SQLITE_LOCK_SHARED 1
1686#define SQLITE_LOCK_RESERVED 2
1687#define SQLITE_LOCK_PENDING 3
1688#define SQLITE_LOCK_EXCLUSIVE 4
1689
1690/*
1691** CAPI3REF: Synchronization Type Flags
1692**
1693** When SQLite invokes the xSync() method of an
1694** [sqlite3_io_methods] object it uses a combination of
1695** these integer values as the second argument.
1696**
1697** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1698** sync operation only needs to flush data to mass storage. Inode
1699** information need not be flushed. If the lower four bits of the flag
1700** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1701** If the lower four bits equal SQLITE_SYNC_FULL, that means
1702** to use Mac OS X style fullsync instead of fsync().
1703**
1704** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1705** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1706** settings. The [synchronous pragma] determines when calls to the
1707** xSync VFS method occur and applies uniformly across all platforms.
1708** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1709** energetic or rigorous or forceful the sync operations are and
1710** only make a difference on Mac OSX for the default SQLite code.
1711** (Third-party VFS implementations might also make the distinction
1712** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1713** operating systems natively supported by SQLite, only Mac OSX
1714** cares about the difference.)
1715*/
1716#define SQLITE_SYNC_NORMAL 0x00002
1717#define SQLITE_SYNC_FULL 0x00003
1718#define SQLITE_SYNC_DATAONLY 0x00010
1719
1720/*
1721** CAPI3REF: OS Interface Open File Handle
1722**
1723** An [sqlite3_file] object represents an open file in the
1724** [sqlite3_vfs | OS interface layer]. Individual OS interface
1725** implementations will
1726** want to subclass this object by appending additional fields
1727** for their own use. The pMethods entry is a pointer to an
1728** [sqlite3_io_methods] object that defines methods for performing
1729** I/O operations on the open file.
1730*/
1731typedef struct sqlite3_file sqlite3_file;
1732struct sqlite3_file {
1733 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1734};
1735
1736/*
1737** CAPI3REF: OS Interface File Virtual Methods Object
1738**
1739** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1740** [sqlite3_file] object (or, more commonly, a subclass of the
1741** [sqlite3_file] object) with a pointer to an instance of this object.
1742** This object defines the methods used to perform various operations
1743** against the open file represented by the [sqlite3_file] object.
1744**
1745** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1746** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1747** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1748** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1749** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1750** to NULL.
1751**
1752** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1753** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1754** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1755** flag may be ORed in to indicate that only the data of the file
1756** and not its inode needs to be synced.
1757**
1758** The integer values to xLock() and xUnlock() are one of
1759** <ul>
1760** <li> [SQLITE_LOCK_NONE],
1761** <li> [SQLITE_LOCK_SHARED],
1762** <li> [SQLITE_LOCK_RESERVED],
1763** <li> [SQLITE_LOCK_PENDING], or
1764** <li> [SQLITE_LOCK_EXCLUSIVE].
1765** </ul>
1766** xLock() increases the lock. xUnlock() decreases the lock.
1767** The xCheckReservedLock() method checks whether any database connection,
1768** either in this process or in some other process, is holding a RESERVED,
1769** PENDING, or EXCLUSIVE lock on the file. It returns true
1770** if such a lock exists and false otherwise.
1771**
1772** The xFileControl() method is a generic interface that allows custom
1773** VFS implementations to directly control an open file using the
1774** [sqlite3_file_control()] interface. The second "op" argument is an
1775** integer opcode. The third argument is a generic pointer intended to
1776** point to a structure that may contain arguments or space in which to
1777** write return values. Potential uses for xFileControl() might be
1778** functions to enable blocking locks with timeouts, to change the
1779** locking strategy (for example to use dot-file locks), to inquire
1780** about the status of a lock, or to break stale locks. The SQLite
1781** core reserves all opcodes less than 100 for its own use.
1782** A [file control opcodes | list of opcodes] less than 100 is available.
1783** Applications that define a custom xFileControl method should use opcodes
1784** greater than 100 to avoid conflicts. VFS implementations should
1785** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1786** recognize.
1787**
1788** The xSectorSize() method returns the sector size of the
1789** device that underlies the file. The sector size is the
1790** minimum write that can be performed without disturbing
1791** other bytes in the file. The xDeviceCharacteristics()
1792** method returns a bit vector describing behaviors of the
1793** underlying device:
1794**
1795** <ul>
1796** <li> [SQLITE_IOCAP_ATOMIC]
1797** <li> [SQLITE_IOCAP_ATOMIC512]
1798** <li> [SQLITE_IOCAP_ATOMIC1K]
1799** <li> [SQLITE_IOCAP_ATOMIC2K]
1800** <li> [SQLITE_IOCAP_ATOMIC4K]
1801** <li> [SQLITE_IOCAP_ATOMIC8K]
1802** <li> [SQLITE_IOCAP_ATOMIC16K]
1803** <li> [SQLITE_IOCAP_ATOMIC32K]
1804** <li> [SQLITE_IOCAP_ATOMIC64K]
1805** <li> [SQLITE_IOCAP_SAFE_APPEND]
1806** <li> [SQLITE_IOCAP_SEQUENTIAL]
1807** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1808** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1809** <li> [SQLITE_IOCAP_IMMUTABLE]
1810** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1811** </ul>
1812**
1813** The SQLITE_IOCAP_ATOMIC property means that all writes of
1814** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1815** mean that writes of blocks that are nnn bytes in size and
1816** are aligned to an address which is an integer multiple of
1817** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1818** that when data is appended to a file, the data is appended
1819** first then the size of the file is extended, never the other
1820** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1821** information is written to disk in the same order as calls
1822** to xWrite().
1823**
1824** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1825** in the unread portions of the buffer with zeros. A VFS that
1826** fails to zero-fill short reads might seem to work. However,
1827** failure to zero-fill short reads will eventually lead to
1828** database corruption.
1829*/
1830typedef struct sqlite3_io_methods sqlite3_io_methods;
1831struct sqlite3_io_methods {
1832 int iVersion;
1833 int (*xClose)(sqlite3_file*);
1834 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1835 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1836 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1837 int (*xSync)(sqlite3_file*, int flags);
1838 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1839 int (*xLock)(sqlite3_file*, int);
1840 int (*xUnlock)(sqlite3_file*, int);
1841 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1842 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1843 int (*xSectorSize)(sqlite3_file*);
1844 int (*xDeviceCharacteristics)(sqlite3_file*);
1845 /* Methods above are valid for version 1 */
1846 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1847 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1848 void (*xShmBarrier)(sqlite3_file*);
1849 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1850 /* Methods above are valid for version 2 */
1851 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1852 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1853 /* Methods above are valid for version 3 */
1854 /* Additional methods may be added in future releases */
1855};
1856
1857/*
1858** CAPI3REF: Standard File Control Opcodes
1859** KEYWORDS: {file control opcodes} {file control opcode}
1860**
1861** These integer constants are opcodes for the xFileControl method
1862** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1863** interface.
1864**
1865** <ul>
1866** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1867** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1868** opcode causes the xFileControl method to write the current state of
1869** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1870** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1871** into an integer that the pArg argument points to. This capability
1872** is used during testing and is only available when the SQLITE_TEST
1873** compile-time option is used.
1874**
1875** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1876** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1877** layer a hint of how large the database file will grow to be during the
1878** current transaction. This hint is not guaranteed to be accurate but it
1879** is often close. The underlying VFS might choose to preallocate database
1880** file space based on this hint in order to help writes to the database
1881** file run faster.
1882**
1883** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1884** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1885** implements [sqlite3_deserialize()] to set an upper bound on the size
1886** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1887** If the integer pointed to is negative, then it is filled in with the
1888** current limit. Otherwise the limit is set to the larger of the value
1889** of the integer pointed to and the current database size. The integer
1890** pointed to is set to the new limit.
1891**
1892** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1893** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1894** extends and truncates the database file in chunks of a size specified
1895** by the user. The fourth argument to [sqlite3_file_control()] should
1896** point to an integer (type int) containing the new chunk-size to use
1897** for the nominated database. Allocating database file space in large
1898** chunks (say 1MB at a time), may reduce file-system fragmentation and
1899** improve performance on some systems.
1900**
1901** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1902** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1903** to the [sqlite3_file] object associated with a particular database
1904** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1905**
1906** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1907** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1908** to the [sqlite3_file] object associated with the journal file (either
1909** the [rollback journal] or the [write-ahead log]) for a particular database
1910** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1911**
1912** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1913** No longer in use.
1914**
1915** <li>[[SQLITE_FCNTL_SYNC]]
1916** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1917** sent to the VFS immediately before the xSync method is invoked on a
1918** database file descriptor. Or, if the xSync method is not invoked
1919** because the user has configured SQLite with
1920** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1921** of the xSync method. In most cases, the pointer argument passed with
1922** this file-control is NULL. However, if the database file is being synced
1923** as part of a multi-database commit, the argument points to a nul-terminated
1924** string containing the transactions super-journal file name. VFSes that
1925** do not need this signal should silently ignore this opcode. Applications
1926** should not call [sqlite3_file_control()] with this opcode as doing so may
1927** disrupt the operation of the specialized VFSes that do require it.
1928**
1929** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1930** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1931** and sent to the VFS after a transaction has been committed immediately
1932** but before the database is unlocked. VFSes that do not need this signal
1933** should silently ignore this opcode. Applications should not call
1934** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1935** operation of the specialized VFSes that do require it.
1936**
1937** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1938** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1939** retry counts and intervals for certain disk I/O operations for the
1940** windows [VFS] in order to provide robustness in the presence of
1941** anti-virus programs. By default, the windows VFS will retry file read,
1942** file write, and file delete operations up to 10 times, with a delay
1943** of 25 milliseconds before the first retry and with the delay increasing
1944** by an additional 25 milliseconds with each subsequent retry. This
1945** opcode allows these two values (10 retries and 25 milliseconds of delay)
1946** to be adjusted. The values are changed for all database connections
1947** within the same process. The argument is a pointer to an array of two
1948** integers where the first integer is the new retry count and the second
1949** integer is the delay. If either integer is negative, then the setting
1950** is not changed but instead the prior value of that setting is written
1951** into the array entry, allowing the current retry settings to be
1952** interrogated. The zDbName parameter is ignored.
1953**
1954** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1955** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1956** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1957** write ahead log ([WAL file]) and shared memory
1958** files used for transaction control
1959** are automatically deleted when the latest connection to the database
1960** closes. Setting persistent WAL mode causes those files to persist after
1961** close. Persisting the files is useful when other processes that do not
1962** have write permission on the directory containing the database file want
1963** to read the database file, as the WAL and shared memory files must exist
1964** in order for the database to be readable. 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 persistent WAL mode or 1 to enable persistent
1967** WAL mode. If the integer is -1, then it is overwritten with the current
1968** WAL persistence setting.
1969**
1970** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1971** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1972** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1973** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1974** xDeviceCharacteristics methods. The fourth parameter to
1975** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1976** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1977** mode. If the integer is -1, then it is overwritten with the current
1978** zero-damage mode setting.
1979**
1980** <li>[[SQLITE_FCNTL_OVERWRITE]]
1981** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1982** a write transaction to indicate that, unless it is rolled back for some
1983** reason, the entire database file will be overwritten by the current
1984** transaction. This is used by VACUUM operations.
1985**
1986** <li>[[SQLITE_FCNTL_VFSNAME]]
1987** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1988** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1989** final bottom-level VFS are written into memory obtained from
1990** [sqlite3_malloc()] and the result is stored in the char* variable
1991** that the fourth parameter of [sqlite3_file_control()] points to.
1992** The caller is responsible for freeing the memory when done. As with
1993** all file-control actions, there is no guarantee that this will actually
1994** do anything. Callers should initialize the char* variable to a NULL
1995** pointer in case this file-control is not implemented. This file-control
1996** is intended for diagnostic use only.
1997**
1998** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1999** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
2000** [VFSes] currently in use. ^(The argument X in
2001** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
2002** of type "[sqlite3_vfs] **". This opcodes will set *X
2003** to a pointer to the top-level VFS.)^
2004** ^When there are multiple VFS shims in the stack, this opcode finds the
2005** upper-most shim only.
2006**
2007** <li>[[SQLITE_FCNTL_PRAGMA]]
2008** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
2009** file control is sent to the open [sqlite3_file] object corresponding
2010** to the database file to which the pragma statement refers. ^The argument
2011** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
2012** pointers to strings (char**) in which the second element of the array
2013** is the name of the pragma and the third element is the argument to the
2014** pragma or NULL if the pragma has no argument. ^The handler for an
2015** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
2016** of the char** argument point to a string obtained from [sqlite3_mprintf()]
2017** or the equivalent and that string will become the result of the pragma or
2018** the error message if the pragma fails. ^If the
2019** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
2020** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
2021** file control returns [SQLITE_OK], then the parser assumes that the
2022** VFS has handled the PRAGMA itself and the parser generates a no-op
2023** prepared statement if result string is NULL, or that returns a copy
2024** of the result string if the string is non-NULL.
2025** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
2026** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
2027** that the VFS encountered an error while handling the [PRAGMA] and the
2028** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
2029** file control occurs at the beginning of pragma statement analysis and so
2030** it is able to override built-in [PRAGMA] statements.
2031**
2032** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
2033** ^The [SQLITE_FCNTL_BUSYHANDLER]
2034** file-control may be invoked by SQLite on the database file handle
2035** shortly after it is opened in order to provide a custom VFS with access
2036** to the connection's busy-handler callback. The argument is of type (void**)
2037** - an array of two (void *) values. The first (void *) actually points
2038** to a function of type (int (*)(void *)). In order to invoke the connection's
2039** busy-handler, this function should be invoked with the second (void *) in
2040** the array as the only argument. If it returns non-zero, then the operation
2041** should be retried. If it returns zero, the custom VFS should abandon the
2042** current operation.
2043**
2044** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
2045** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2046** to have SQLite generate a
2047** temporary filename using the same algorithm that is followed to generate
2048** temporary filenames for TEMP tables and other internal uses. The
2049** argument should be a char** which will be filled with the filename
2050** written into memory obtained from [sqlite3_malloc()]. The caller should
2051** invoke [sqlite3_free()] on the result to avoid a memory leak.
2052**
2053** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
2054** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
2055** maximum number of bytes that will be used for memory-mapped I/O.
2056** The argument is a pointer to a value of type sqlite3_int64 that
2057** is an advisory maximum number of bytes in the file to memory map. The
2058** pointer is overwritten with the old value. The limit is not changed if
2059** the value originally pointed to is negative, and so the current limit
2060** can be queried by passing in a pointer to a negative number. This
2061** file-control is used internally to implement [PRAGMA mmap_size].
2062**
2063** <li>[[SQLITE_FCNTL_TRACE]]
2064** The [SQLITE_FCNTL_TRACE] file control provides advisory information
2065** to the VFS about what the higher layers of the SQLite stack are doing.
2066** This file control is used by some VFS activity tracing [shims].
2067** The argument is a zero-terminated string. Higher layers in the
2068** SQLite stack may generate instances of this file control if
2069** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2070**
2071** <li>[[SQLITE_FCNTL_HAS_MOVED]]
2072** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2073** pointer to an integer and it writes a boolean into that integer depending
2074** on whether or not the file has been renamed, moved, or deleted since it
2075** was first opened.
2076**
2077** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
2078** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
2079** underlying native file handle associated with a file handle. This file
2080** control interprets its argument as a pointer to a native file handle and
2081** writes the resulting value there.
2082**
2083** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
2084** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
2085** opcode causes the xFileControl method to swap the file handle with the one
2086** pointed to by the pArg argument. This capability is used during testing
2087** and only needs to be supported when SQLITE_TEST is defined.
2088**
2089** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
2090** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2091** be advantageous to block on the next WAL lock if the lock is not immediately
2092** available. The WAL subsystem issues this signal during rare
2093** circumstances in order to fix a problem with priority inversion.
2094** Applications should <em>not</em> use this file-control.
2095**
2096** <li>[[SQLITE_FCNTL_ZIPVFS]]
2097** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
2098** VFS should return SQLITE_NOTFOUND for this opcode.
2099**
2100** <li>[[SQLITE_FCNTL_RBU]]
2101** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
2102** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
2103** this opcode.
2104**
2105** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
2106** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
2107** the file descriptor is placed in "batch write mode", which
2108** means all subsequent write operations will be deferred and done
2109** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
2110** that do not support batch atomic writes will return SQLITE_NOTFOUND.
2111** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
2112** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
2113** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
2114** no VFS interface calls on the same [sqlite3_file] file descriptor
2115** except for calls to the xWrite method and the xFileControl method
2116** with [SQLITE_FCNTL_SIZE_HINT].
2117**
2118** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
2119** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
2120** operations since the previous successful call to
2121** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
2122** This file control returns [SQLITE_OK] if and only if the writes were
2123** all performed successfully and have been committed to persistent storage.
2124** ^Regardless of whether or not it is successful, this file control takes
2125** the file descriptor out of batch write mode so that all subsequent
2126** write operations are independent.
2127** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
2128** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2129**
2130** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
2131** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
2132** operations since the previous successful call to
2133** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2134** ^This file control takes the file descriptor out of batch write mode
2135** so that all subsequent write operations are independent.
2136** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2137** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2138**
2139** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2140** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
2141** to block for up to M milliseconds before failing when attempting to
2142** obtain a file lock using the xLock or xShmLock methods of the VFS.
2143** The parameter is a pointer to a 32-bit signed integer that contains
2144** the value that M is to be set to. Before returning, the 32-bit signed
2145** integer is overwritten with the previous value of M.
2146**
2147** <li>[[SQLITE_FCNTL_DATA_VERSION]]
2148** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
2149** a database file. The argument is a pointer to a 32-bit unsigned integer.
2150** The "data version" for the pager is written into the pointer. The
2151** "data version" changes whenever any change occurs to the corresponding
2152** database file, either through SQL statements on the same database
2153** connection or through transactions committed by separate database
2154** connections possibly in other processes. The [sqlite3_total_changes()]
2155** interface can be used to find if any database on the connection has changed,
2156** but that interface responds to changes on TEMP as well as MAIN and does
2157** not provide a mechanism to detect changes to MAIN only. Also, the
2158** [sqlite3_total_changes()] interface responds to internal changes only and
2159** omits changes made by other database connections. The
2160** [PRAGMA data_version] command provides a mechanism to detect changes to
2161** a single attached database that occur due to other database connections,
2162** but omits changes implemented by the database connection on which it is
2163** called. This file control is the only mechanism to detect changes that
2164** happen either internally or externally and that are associated with
2165** a particular attached database.
2166**
2167** <li>[[SQLITE_FCNTL_CKPT_START]]
2168** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
2169** in wal mode before the client starts to copy pages from the wal
2170** file to the database file.
2171**
2172** <li>[[SQLITE_FCNTL_CKPT_DONE]]
2173** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
2174** in wal mode after the client has finished copying pages from the wal
2175** file to the database file, but before the *-shm file is updated to
2176** record the fact that the pages have been checkpointed.
2177** </ul>
2178*/
2179#define SQLITE_FCNTL_LOCKSTATE 1
2180#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
2181#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
2182#define SQLITE_FCNTL_LAST_ERRNO 4
2183#define SQLITE_FCNTL_SIZE_HINT 5
2184#define SQLITE_FCNTL_CHUNK_SIZE 6
2185#define SQLITE_FCNTL_FILE_POINTER 7
2186#define SQLITE_FCNTL_SYNC_OMITTED 8
2187#define SQLITE_FCNTL_WIN32_AV_RETRY 9
2188#define SQLITE_FCNTL_PERSIST_WAL 10
2189#define SQLITE_FCNTL_OVERWRITE 11
2190#define SQLITE_FCNTL_VFSNAME 12
2191#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
2192#define SQLITE_FCNTL_PRAGMA 14
2193#define SQLITE_FCNTL_BUSYHANDLER 15
2194#define SQLITE_FCNTL_TEMPFILENAME 16
2195#define SQLITE_FCNTL_MMAP_SIZE 18
2196#define SQLITE_FCNTL_TRACE 19
2197#define SQLITE_FCNTL_HAS_MOVED 20
2198#define SQLITE_FCNTL_SYNC 21
2199#define SQLITE_FCNTL_COMMIT_PHASETWO 22
2200#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
2201#define SQLITE_FCNTL_WAL_BLOCK 24
2202#define SQLITE_FCNTL_ZIPVFS 25
2203#define SQLITE_FCNTL_RBU 26
2204#define SQLITE_FCNTL_VFS_POINTER 27
2205#define SQLITE_FCNTL_JOURNAL_POINTER 28
2206#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
2207#define SQLITE_FCNTL_PDB 30
2208#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
2209#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
2210#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
2211#define SQLITE_FCNTL_LOCK_TIMEOUT 34
2212#define SQLITE_FCNTL_DATA_VERSION 35
2213#define SQLITE_FCNTL_SIZE_LIMIT 36
2214#define SQLITE_FCNTL_CKPT_DONE 37
2215#define SQLITE_FCNTL_RESERVE_BYTES 38
2216#define SQLITE_FCNTL_CKPT_START 39
2217
2218/* deprecated names */
2219#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
2220#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
2221#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
2222
2223
2224/*
2225** CAPI3REF: Mutex Handle
2226**
2227** The mutex module within SQLite defines [sqlite3_mutex] to be an
2228** abstract type for a mutex object. The SQLite core never looks
2229** at the internal representation of an [sqlite3_mutex]. It only
2230** deals with pointers to the [sqlite3_mutex] object.
2231**
2232** Mutexes are created using [sqlite3_mutex_alloc()].
2233*/
2234typedef struct sqlite3_mutex sqlite3_mutex;
2235
2236/*
2237** CAPI3REF: Loadable Extension Thunk
2238**
2239** A pointer to the opaque sqlite3_api_routines structure is passed as
2240** the third parameter to entry points of [loadable extensions]. This
2241** structure must be typedefed in order to work around compiler warnings
2242** on some platforms.
2243*/
2244typedef struct sqlite3_api_routines sqlite3_api_routines;
2245
2246/*
2247** CAPI3REF: OS Interface Object
2248**
2249** An instance of the sqlite3_vfs object defines the interface between
2250** the SQLite core and the underlying operating system. The "vfs"
2251** in the name of the object stands for "virtual file system". See
2252** the [VFS | VFS documentation] for further information.
2253**
2254** The VFS interface is sometimes extended by adding new methods onto
2255** the end. Each time such an extension occurs, the iVersion field
2256** is incremented. The iVersion value started out as 1 in
2257** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
2258** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
2259** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
2260** may be appended to the sqlite3_vfs object and the iVersion value
2261** may increase again in future versions of SQLite.
2262** Note that due to an oversight, the structure
2263** of the sqlite3_vfs object changed in the transition from
2264** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
2265** and yet the iVersion field was not increased.
2266**
2267** The szOsFile field is the size of the subclassed [sqlite3_file]
2268** structure used by this VFS. mxPathname is the maximum length of
2269** a pathname in this VFS.
2270**
2271** Registered sqlite3_vfs objects are kept on a linked list formed by
2272** the pNext pointer. The [sqlite3_vfs_register()]
2273** and [sqlite3_vfs_unregister()] interfaces manage this list
2274** in a thread-safe way. The [sqlite3_vfs_find()] interface
2275** searches the list. Neither the application code nor the VFS
2276** implementation should use the pNext pointer.
2277**
2278** The pNext field is the only field in the sqlite3_vfs
2279** structure that SQLite will ever modify. SQLite will only access
2280** or modify this field while holding a particular static mutex.
2281** The application should never modify anything within the sqlite3_vfs
2282** object once the object has been registered.
2283**
2284** The zName field holds the name of the VFS module. The name must
2285** be unique across all VFS modules.
2286**
2287** [[sqlite3_vfs.xOpen]]
2288** ^SQLite guarantees that the zFilename parameter to xOpen
2289** is either a NULL pointer or string obtained
2290** from xFullPathname() with an optional suffix added.
2291** ^If a suffix is added to the zFilename parameter, it will
2292** consist of a single "-" character followed by no more than
2293** 11 alphanumeric and/or "-" characters.
2294** ^SQLite further guarantees that
2295** the string will be valid and unchanged until xClose() is
2296** called. Because of the previous sentence,
2297** the [sqlite3_file] can safely store a pointer to the
2298** filename if it needs to remember the filename for some reason.
2299** If the zFilename parameter to xOpen is a NULL pointer then xOpen
2300** must invent its own temporary name for the file. ^Whenever the
2301** xFilename parameter is NULL it will also be the case that the
2302** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
2303**
2304** The flags argument to xOpen() includes all bits set in
2305** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
2306** or [sqlite3_open16()] is used, then flags includes at least
2307** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
2308** If xOpen() opens a file read-only then it sets *pOutFlags to
2309** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
2310**
2311** ^(SQLite will also add one of the following flags to the xOpen()
2312** call, depending on the object being opened:
2313**
2314** <ul>
2315** <li> [SQLITE_OPEN_MAIN_DB]
2316** <li> [SQLITE_OPEN_MAIN_JOURNAL]
2317** <li> [SQLITE_OPEN_TEMP_DB]
2318** <li> [SQLITE_OPEN_TEMP_JOURNAL]
2319** <li> [SQLITE_OPEN_TRANSIENT_DB]
2320** <li> [SQLITE_OPEN_SUBJOURNAL]
2321** <li> [SQLITE_OPEN_SUPER_JOURNAL]
2322** <li> [SQLITE_OPEN_WAL]
2323** </ul>)^
2324**
2325** The file I/O implementation can use the object type flags to
2326** change the way it deals with files. For example, an application
2327** that does not care about crash recovery or rollback might make
2328** the open of a journal file a no-op. Writes to this journal would
2329** also be no-ops, and any attempt to read the journal would return
2330** SQLITE_IOERR. Or the implementation might recognize that a database
2331** file will be doing page-aligned sector reads and writes in a random
2332** order and set up its I/O subsystem accordingly.
2333**
2334** SQLite might also add one of the following flags to the xOpen method:
2335**
2336** <ul>
2337** <li> [SQLITE_OPEN_DELETEONCLOSE]
2338** <li> [SQLITE_OPEN_EXCLUSIVE]
2339** </ul>
2340**
2341** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
2342** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
2343** will be set for TEMP databases and their journals, transient
2344** databases, and subjournals.
2345**
2346** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
2347** with the [SQLITE_OPEN_CREATE] flag, which are both directly
2348** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
2349** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
2350** SQLITE_OPEN_CREATE, is used to indicate that file should always
2351** be created, and that it is an error if it already exists.
2352** It is <i>not</i> used to indicate the file should be opened
2353** for exclusive access.
2354**
2355** ^At least szOsFile bytes of memory are allocated by SQLite
2356** to hold the [sqlite3_file] structure passed as the third
2357** argument to xOpen. The xOpen method does not have to
2358** allocate the structure; it should just fill it in. Note that
2359** the xOpen method must set the sqlite3_file.pMethods to either
2360** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
2361** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
2362** element will be valid after xOpen returns regardless of the success
2363** or failure of the xOpen call.
2364**
2365** [[sqlite3_vfs.xAccess]]
2366** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
2367** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2368** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2369** to test whether a file is at least readable. The SQLITE_ACCESS_READ
2370** flag is never actually used and is not implemented in the built-in
2371** VFSes of SQLite. The file is named by the second argument and can be a
2372** directory. The xAccess method returns [SQLITE_OK] on success or some
2373** non-zero error code if there is an I/O error or if the name of
2374** the file given in the second argument is illegal. If SQLITE_OK
2375** is returned, then non-zero or zero is written into *pResOut to indicate
2376** whether or not the file is accessible.
2377**
2378** ^SQLite will always allocate at least mxPathname+1 bytes for the
2379** output buffer xFullPathname. The exact size of the output buffer
2380** is also passed as a parameter to both methods. If the output buffer
2381** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
2382** handled as a fatal error by SQLite, vfs implementations should endeavor
2383** to prevent this by setting mxPathname to a sufficiently large value.
2384**
2385** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
2386** interfaces are not strictly a part of the filesystem, but they are
2387** included in the VFS structure for completeness.
2388** The xRandomness() function attempts to return nBytes bytes
2389** of good-quality randomness into zOut. The return value is
2390** the actual number of bytes of randomness obtained.
2391** The xSleep() method causes the calling thread to sleep for at
2392** least the number of microseconds given. ^The xCurrentTime()
2393** method returns a Julian Day Number for the current date and time as
2394** a floating point value.
2395** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
2396** Day Number multiplied by 86400000 (the number of milliseconds in
2397** a 24-hour day).
2398** ^SQLite will use the xCurrentTimeInt64() method to get the current
2399** date and time if that method is available (if iVersion is 2 or
2400** greater and the function pointer is not NULL) and will fall back
2401** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
2402**
2403** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
2404** are not used by the SQLite core. These optional interfaces are provided
2405** by some VFSes to facilitate testing of the VFS code. By overriding
2406** system calls with functions under its control, a test program can
2407** simulate faults and error conditions that would otherwise be difficult
2408** or impossible to induce. The set of system calls that can be overridden
2409** varies from one VFS to another, and from one version of the same VFS to the
2410** next. Applications that use these interfaces must be prepared for any
2411** or all of these interfaces to be NULL or for their behavior to change
2412** from one release to the next. Applications must not attempt to access
2413** any of these methods if the iVersion of the VFS is less than 3.
2414*/
2415typedef struct sqlite3_vfs sqlite3_vfs;
2416typedef void (*sqlite3_syscall_ptr)(void);
2417struct sqlite3_vfs {
2418 int iVersion; /* Structure version number (currently 3) */
2419 int szOsFile; /* Size of subclassed sqlite3_file */
2420 int mxPathname; /* Maximum file pathname length */
2421 sqlite3_vfs *pNext; /* Next registered VFS */
2422 const char *zName; /* Name of this virtual file system */
2423 void *pAppData; /* Pointer to application-specific data */
2424 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
2425 int flags, int *pOutFlags);
2426 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
2427 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
2428 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
2429 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
2430 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
2431 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
2432 void (*xDlClose)(sqlite3_vfs*, void*);
2433 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
2434 int (*xSleep)(sqlite3_vfs*, int microseconds);
2435 int (*xCurrentTime)(sqlite3_vfs*, double*);
2436 int (*xGetLastError)(sqlite3_vfs*, int, char *);
2437 /*
2438 ** The methods above are in version 1 of the sqlite_vfs object
2439 ** definition. Those that follow are added in version 2 or later
2440 */
2441 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
2442 /*
2443 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
2444 ** Those below are for version 3 and greater.
2445 */
2446 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
2447 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
2448 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
2449 /*
2450 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
2451 ** New fields may be appended in future versions. The iVersion
2452 ** value will increment whenever this happens.
2453 */
2454};
2455
2456/*
2457** CAPI3REF: Flags for the xAccess VFS method
2458**
2459** These integer constants can be used as the third parameter to
2460** the xAccess method of an [sqlite3_vfs] object. They determine
2461** what kind of permissions the xAccess method is looking for.
2462** With SQLITE_ACCESS_EXISTS, the xAccess method
2463** simply checks whether the file exists.
2464** With SQLITE_ACCESS_READWRITE, the xAccess method
2465** checks whether the named directory is both readable and writable
2466** (in other words, if files can be added, removed, and renamed within
2467** the directory).
2468** The SQLITE_ACCESS_READWRITE constant is currently used only by the
2469** [temp_store_directory pragma], though this could change in a future
2470** release of SQLite.
2471** With SQLITE_ACCESS_READ, the xAccess method
2472** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
2473** currently unused, though it might be used in a future release of
2474** SQLite.
2475*/
2476#define SQLITE_ACCESS_EXISTS 0
2477#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
2478#define SQLITE_ACCESS_READ 2 /* Unused */
2479
2480/*
2481** CAPI3REF: Flags for the xShmLock VFS method
2482**
2483** These integer constants define the various locking operations
2484** allowed by the xShmLock method of [sqlite3_io_methods]. The
2485** following are the only legal combinations of flags to the
2486** xShmLock method:
2487**
2488** <ul>
2489** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
2490** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
2491** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
2492** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
2493** </ul>
2494**
2495** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
2496** was given on the corresponding lock.
2497**
2498** The xShmLock method can transition between unlocked and SHARED or
2499** between unlocked and EXCLUSIVE. It cannot transition between SHARED
2500** and EXCLUSIVE.
2501*/
2502#define SQLITE_SHM_UNLOCK 1
2503#define SQLITE_SHM_LOCK 2
2504#define SQLITE_SHM_SHARED 4
2505#define SQLITE_SHM_EXCLUSIVE 8
2506
2507/*
2508** CAPI3REF: Maximum xShmLock index
2509**
2510** The xShmLock method on [sqlite3_io_methods] may use values
2511** between 0 and this upper bound as its "offset" argument.
2512** The SQLite core will never attempt to acquire or release a
2513** lock outside of this range
2514*/
2515#define SQLITE_SHM_NLOCK 8
2516
2517
2518/*
2519** CAPI3REF: Initialize The SQLite Library
2520**
2521** ^The sqlite3_initialize() routine initializes the
2522** SQLite library. ^The sqlite3_shutdown() routine
2523** deallocates any resources that were allocated by sqlite3_initialize().
2524** These routines are designed to aid in process initialization and
2525** shutdown on embedded systems. Workstation applications using
2526** SQLite normally do not need to invoke either of these routines.
2527**
2528** A call to sqlite3_initialize() is an "effective" call if it is
2529** the first time sqlite3_initialize() is invoked during the lifetime of
2530** the process, or if it is the first time sqlite3_initialize() is invoked
2531** following a call to sqlite3_shutdown(). ^(Only an effective call
2532** of sqlite3_initialize() does any initialization. All other calls
2533** are harmless no-ops.)^
2534**
2535** A call to sqlite3_shutdown() is an "effective" call if it is the first
2536** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
2537** an effective call to sqlite3_shutdown() does any deinitialization.
2538** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2539**
2540** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
2541** is not. The sqlite3_shutdown() interface must only be called from a
2542** single thread. All open [database connections] must be closed and all
2543** other SQLite resources must be deallocated prior to invoking
2544** sqlite3_shutdown().
2545**
2546** Among other things, ^sqlite3_initialize() will invoke
2547** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
2548** will invoke sqlite3_os_end().
2549**
2550** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
2551** ^If for some reason, sqlite3_initialize() is unable to initialize
2552** the library (perhaps it is unable to allocate a needed resource such
2553** as a mutex) it returns an [error code] other than [SQLITE_OK].
2554**
2555** ^The sqlite3_initialize() routine is called internally by many other
2556** SQLite interfaces so that an application usually does not need to
2557** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
2558** calls sqlite3_initialize() so the SQLite library will be automatically
2559** initialized when [sqlite3_open()] is called if it has not be initialized
2560** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
2561** compile-time option, then the automatic calls to sqlite3_initialize()
2562** are omitted and the application must call sqlite3_initialize() directly
2563** prior to using any other SQLite interface. For maximum portability,
2564** it is recommended that applications always invoke sqlite3_initialize()
2565** directly prior to using any other SQLite interface. Future releases
2566** of SQLite may require this. In other words, the behavior exhibited
2567** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
2568** default behavior in some future release of SQLite.
2569**
2570** The sqlite3_os_init() routine does operating-system specific
2571** initialization of the SQLite library. The sqlite3_os_end()
2572** routine undoes the effect of sqlite3_os_init(). Typical tasks
2573** performed by these routines include allocation or deallocation
2574** of static resources, initialization of global variables,
2575** setting up a default [sqlite3_vfs] module, or setting up
2576** a default configuration using [sqlite3_config()].
2577**
2578** The application should never invoke either sqlite3_os_init()
2579** or sqlite3_os_end() directly. The application should only invoke
2580** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
2581** interface is called automatically by sqlite3_initialize() and
2582** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
2583** implementations for sqlite3_os_init() and sqlite3_os_end()
2584** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
2585** When [custom builds | built for other platforms]
2586** (using the [SQLITE_OS_OTHER=1] compile-time
2587** option) the application must supply a suitable implementation for
2588** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2589** implementation of sqlite3_os_init() or sqlite3_os_end()
2590** must return [SQLITE_OK] on success and some other [error code] upon
2591** failure.
2592*/
2593SQLITE_API int sqlite3_initialize(void);
2594SQLITE_API int sqlite3_shutdown(void);
2595SQLITE_API int sqlite3_os_init(void);
2596SQLITE_API int sqlite3_os_end(void);
2597
2598/*
2599** CAPI3REF: Configuring The SQLite Library
2600**
2601** The sqlite3_config() interface is used to make global configuration
2602** changes to SQLite in order to tune SQLite to the specific needs of
2603** the application. The default configuration is recommended for most
2604** applications and so this routine is usually not necessary. It is
2605** provided to support rare applications with unusual needs.
2606**
2607** <b>The sqlite3_config() interface is not threadsafe. The application
2608** must ensure that no other SQLite interfaces are invoked by other
2609** threads while sqlite3_config() is running.</b>
2610**
2611** The sqlite3_config() interface
2612** may only be invoked prior to library initialization using
2613** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2614** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2615** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
2616** Note, however, that ^sqlite3_config() can be called as part of the
2617** implementation of an application-defined [sqlite3_os_init()].
2618**
2619** The first argument to sqlite3_config() is an integer
2620** [configuration option] that determines
2621** what property of SQLite is to be configured. Subsequent arguments
2622** vary depending on the [configuration option]
2623** in the first argument.
2624**
2625** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2626** ^If the option is unknown or SQLite is unable to set the option
2627** then this routine returns a non-zero [error code].
2628*/
2629SQLITE_API int sqlite3_config(int, ...);
2630
2631/*
2632** CAPI3REF: Configure database connections
2633** METHOD: sqlite3
2634**
2635** The sqlite3_db_config() interface is used to make configuration
2636** changes to a [database connection]. The interface is similar to
2637** [sqlite3_config()] except that the changes apply to a single
2638** [database connection] (specified in the first argument).
2639**
2640** The second argument to sqlite3_db_config(D,V,...) is the
2641** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2642** that indicates what aspect of the [database connection] is being configured.
2643** Subsequent arguments vary depending on the configuration verb.
2644**
2645** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2646** the call is considered successful.
2647*/
2648SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2649
2650/*
2651** CAPI3REF: Memory Allocation Routines
2652**
2653** An instance of this object defines the interface between SQLite
2654** and low-level memory allocation routines.
2655**
2656** This object is used in only one place in the SQLite interface.
2657** A pointer to an instance of this object is the argument to
2658** [sqlite3_config()] when the configuration option is
2659** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2660** By creating an instance of this object
2661** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2662** during configuration, an application can specify an alternative
2663** memory allocation subsystem for SQLite to use for all of its
2664** dynamic memory needs.
2665**
2666** Note that SQLite comes with several [built-in memory allocators]
2667** that are perfectly adequate for the overwhelming majority of applications
2668** and that this object is only useful to a tiny minority of applications
2669** with specialized memory allocation requirements. This object is
2670** also used during testing of SQLite in order to specify an alternative
2671** memory allocator that simulates memory out-of-memory conditions in
2672** order to verify that SQLite recovers gracefully from such
2673** conditions.
2674**
2675** The xMalloc, xRealloc, and xFree methods must work like the
2676** malloc(), realloc() and free() functions from the standard C library.
2677** ^SQLite guarantees that the second argument to
2678** xRealloc is always a value returned by a prior call to xRoundup.
2679**
2680** xSize should return the allocated size of a memory allocation
2681** previously obtained from xMalloc or xRealloc. The allocated size
2682** is always at least as big as the requested size but may be larger.
2683**
2684** The xRoundup method returns what would be the allocated size of
2685** a memory allocation given a particular requested size. Most memory
2686** allocators round up memory allocations at least to the next multiple
2687** of 8. Some allocators round up to a larger multiple or to a power of 2.
2688** Every memory allocation request coming in through [sqlite3_malloc()]
2689** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2690** that causes the corresponding memory allocation to fail.
2691**
2692** The xInit method initializes the memory allocator. For example,
2693** it might allocate any required mutexes or initialize internal data
2694** structures. The xShutdown method is invoked (indirectly) by
2695** [sqlite3_shutdown()] and should deallocate any resources acquired
2696** by xInit. The pAppData pointer is used as the only parameter to
2697** xInit and xShutdown.
2698**
2699** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2700** the xInit method, so the xInit method need not be threadsafe. The
2701** xShutdown method is only called from [sqlite3_shutdown()] so it does
2702** not need to be threadsafe either. For all other methods, SQLite
2703** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2704** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2705** it is by default) and so the methods are automatically serialized.
2706** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2707** methods must be threadsafe or else make their own arrangements for
2708** serialization.
2709**
2710** SQLite will never invoke xInit() more than once without an intervening
2711** call to xShutdown().
2712*/
2713typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2714struct sqlite3_mem_methods {
2715 void *(*xMalloc)(int); /* Memory allocation function */
2716 void (*xFree)(void*); /* Free a prior allocation */
2717 void *(*xRealloc)(void*,int); /* Resize an allocation */
2718 int (*xSize)(void*); /* Return the size of an allocation */
2719 int (*xRoundup)(int); /* Round up request size to allocation size */
2720 int (*xInit)(void*); /* Initialize the memory allocator */
2721 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2722 void *pAppData; /* Argument to xInit() and xShutdown() */
2723};
2724
2725/*
2726** CAPI3REF: Configuration Options
2727** KEYWORDS: {configuration option}
2728**
2729** These constants are the available integer configuration options that
2730** can be passed as the first argument to the [sqlite3_config()] interface.
2731**
2732** New configuration options may be added in future releases of SQLite.
2733** Existing configuration options might be discontinued. Applications
2734** should check the return code from [sqlite3_config()] to make sure that
2735** the call worked. The [sqlite3_config()] interface will return a
2736** non-zero [error code] if a discontinued or unsupported configuration option
2737** is invoked.
2738**
2739** <dl>
2740** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2741** <dd>There are no arguments to this option. ^This option sets the
2742** [threading mode] to Single-thread. In other words, it disables
2743** all mutexing and puts SQLite into a mode where it can only be used
2744** by a single thread. ^If SQLite is compiled with
2745** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2746** it is not possible to change the [threading mode] from its default
2747** value of Single-thread and so [sqlite3_config()] will return
2748** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2749** configuration option.</dd>
2750**
2751** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2752** <dd>There are no arguments to this option. ^This option sets the
2753** [threading mode] to Multi-thread. In other words, it disables
2754** mutexing on [database connection] and [prepared statement] objects.
2755** The application is responsible for serializing access to
2756** [database connections] and [prepared statements]. But other mutexes
2757** are enabled so that SQLite will be safe to use in a multi-threaded
2758** environment as long as no two threads attempt to use the same
2759** [database connection] at the same time. ^If SQLite is compiled with
2760** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2761** it is not possible to set the Multi-thread [threading mode] and
2762** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2763** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2764**
2765** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2766** <dd>There are no arguments to this option. ^This option sets the
2767** [threading mode] to Serialized. In other words, this option enables
2768** all mutexes including the recursive
2769** mutexes on [database connection] and [prepared statement] objects.
2770** In this mode (which is the default when SQLite is compiled with
2771** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2772** to [database connections] and [prepared statements] so that the
2773** application is free to use the same [database connection] or the
2774** same [prepared statement] in different threads at the same time.
2775** ^If SQLite is compiled with
2776** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2777** it is not possible to set the Serialized [threading mode] and
2778** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2779** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2780**
2781** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2782** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2783** a pointer to an instance of the [sqlite3_mem_methods] structure.
2784** The argument specifies
2785** alternative low-level memory allocation routines to be used in place of
2786** the memory allocation routines built into SQLite.)^ ^SQLite makes
2787** its own private copy of the content of the [sqlite3_mem_methods] structure
2788** before the [sqlite3_config()] call returns.</dd>
2789**
2790** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2791** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2792** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2793** The [sqlite3_mem_methods]
2794** structure is filled with the currently defined memory allocation routines.)^
2795** This option can be used to overload the default memory allocation
2796** routines with a wrapper that simulations memory allocation failure or
2797** tracks memory usage, for example. </dd>
2798**
2799** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2800** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2801** type int, interpreted as a boolean, which if true provides a hint to
2802** SQLite that it should avoid large memory allocations if possible.
2803** SQLite will run faster if it is free to make large memory allocations,
2804** but some application might prefer to run slower in exchange for
2805** guarantees about memory fragmentation that are possible if large
2806** allocations are avoided. This hint is normally off.
2807** </dd>
2808**
2809** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2810** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2811** interpreted as a boolean, which enables or disables the collection of
2812** memory allocation statistics. ^(When memory allocation statistics are
2813** disabled, the following SQLite interfaces become non-operational:
2814** <ul>
2815** <li> [sqlite3_hard_heap_limit64()]
2816** <li> [sqlite3_memory_used()]
2817** <li> [sqlite3_memory_highwater()]
2818** <li> [sqlite3_soft_heap_limit64()]
2819** <li> [sqlite3_status64()]
2820** </ul>)^
2821** ^Memory allocation statistics are enabled by default unless SQLite is
2822** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2823** allocation statistics are disabled by default.
2824** </dd>
2825**
2826** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2827** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2828** </dd>
2829**
2830** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2831** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2832** that SQLite can use for the database page cache with the default page
2833** cache implementation.
2834** This configuration option is a no-op if an application-defined page
2835** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2836** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2837** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2838** and the number of cache lines (N).
2839** The sz argument should be the size of the largest database page
2840** (a power of two between 512 and 65536) plus some extra bytes for each
2841** page header. ^The number of extra bytes needed by the page header
2842** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2843** ^It is harmless, apart from the wasted memory,
2844** for the sz parameter to be larger than necessary. The pMem
2845** argument must be either a NULL pointer or a pointer to an 8-byte
2846** aligned block of memory of at least sz*N bytes, otherwise
2847** subsequent behavior is undefined.
2848** ^When pMem is not NULL, SQLite will strive to use the memory provided
2849** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2850** a page cache line is larger than sz bytes or if all of the pMem buffer
2851** is exhausted.
2852** ^If pMem is NULL and N is non-zero, then each database connection
2853** does an initial bulk allocation for page cache memory
2854** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2855** of -1024*N bytes if N is negative, . ^If additional
2856** page cache memory is needed beyond what is provided by the initial
2857** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2858** additional cache line. </dd>
2859**
2860** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2861** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2862** that SQLite will use for all of its dynamic memory allocation needs
2863** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2864** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2865** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2866** [SQLITE_ERROR] if invoked otherwise.
2867** ^There are three arguments to SQLITE_CONFIG_HEAP:
2868** An 8-byte aligned pointer to the memory,
2869** the number of bytes in the memory buffer, and the minimum allocation size.
2870** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2871** to using its default memory allocator (the system malloc() implementation),
2872** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2873** memory pointer is not NULL then the alternative memory
2874** allocator is engaged to handle all of SQLites memory allocation needs.
2875** The first pointer (the memory pointer) must be aligned to an 8-byte
2876** boundary or subsequent behavior of SQLite will be undefined.
2877** The minimum allocation size is capped at 2**12. Reasonable values
2878** for the minimum allocation size are 2**5 through 2**8.</dd>
2879**
2880** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2881** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2882** pointer to an instance of the [sqlite3_mutex_methods] structure.
2883** The argument specifies alternative low-level mutex routines to be used
2884** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2885** the content of the [sqlite3_mutex_methods] structure before the call to
2886** [sqlite3_config()] returns. ^If SQLite is compiled with
2887** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2888** the entire mutexing subsystem is omitted from the build and hence calls to
2889** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2890** return [SQLITE_ERROR].</dd>
2891**
2892** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2893** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2894** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2895** [sqlite3_mutex_methods]
2896** structure is filled with the currently defined mutex routines.)^
2897** This option can be used to overload the default mutex allocation
2898** routines with a wrapper used to track mutex usage for performance
2899** profiling or testing, for example. ^If SQLite is compiled with
2900** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2901** the entire mutexing subsystem is omitted from the build and hence calls to
2902** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2903** return [SQLITE_ERROR].</dd>
2904**
2905** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2906** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2907** the default size of lookaside memory on each [database connection].
2908** The first argument is the
2909** size of each lookaside buffer slot and the second is the number of
2910** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2911** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2912** option to [sqlite3_db_config()] can be used to change the lookaside
2913** configuration on individual connections.)^ </dd>
2914**
2915** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2916** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2917** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2918** the interface to a custom page cache implementation.)^
2919** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2920**
2921** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2922** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2923** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2924** the current page cache implementation into that object.)^ </dd>
2925**
2926** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2927** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2928** global [error log].
2929** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2930** function with a call signature of void(*)(void*,int,const char*),
2931** and a pointer to void. ^If the function pointer is not NULL, it is
2932** invoked by [sqlite3_log()] to process each logging event. ^If the
2933** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2934** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2935** passed through as the first parameter to the application-defined logger
2936** function whenever that function is invoked. ^The second parameter to
2937** the logger function is a copy of the first parameter to the corresponding
2938** [sqlite3_log()] call and is intended to be a [result code] or an
2939** [extended result code]. ^The third parameter passed to the logger is
2940** log message after formatting via [sqlite3_snprintf()].
2941** The SQLite logging interface is not reentrant; the logger function
2942** supplied by the application must not invoke any SQLite interface.
2943** In a multi-threaded application, the application-defined logger
2944** function must be threadsafe. </dd>
2945**
2946** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2947** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2948** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2949** then URI handling is globally disabled.)^ ^If URI handling is globally
2950** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2951** [sqlite3_open16()] or
2952** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2953** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2954** connection is opened. ^If it is globally disabled, filenames are
2955** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2956** database connection is opened. ^(By default, URI handling is globally
2957** disabled. The default value may be changed by compiling with the
2958** [SQLITE_USE_URI] symbol defined.)^
2959**
2960** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2961** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2962** argument which is interpreted as a boolean in order to enable or disable
2963** the use of covering indices for full table scans in the query optimizer.
2964** ^The default setting is determined
2965** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2966** if that compile-time option is omitted.
2967** The ability to disable the use of covering indices for full table scans
2968** is because some incorrectly coded legacy applications might malfunction
2969** when the optimization is enabled. Providing the ability to
2970** disable the optimization allows the older, buggy application code to work
2971** without change even with newer versions of SQLite.
2972**
2973** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2974** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2975** <dd> These options are obsolete and should not be used by new code.
2976** They are retained for backwards compatibility but are now no-ops.
2977** </dd>
2978**
2979** [[SQLITE_CONFIG_SQLLOG]]
2980** <dt>SQLITE_CONFIG_SQLLOG
2981** <dd>This option is only available if sqlite is compiled with the
2982** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2983** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2984** The second should be of type (void*). The callback is invoked by the library
2985** in three separate circumstances, identified by the value passed as the
2986** fourth parameter. If the fourth parameter is 0, then the database connection
2987** passed as the second argument has just been opened. The third argument
2988** points to a buffer containing the name of the main database file. If the
2989** fourth parameter is 1, then the SQL statement that the third parameter
2990** points to has just been executed. Or, if the fourth parameter is 2, then
2991** the connection being passed as the second parameter is being closed. The
2992** third parameter is passed NULL In this case. An example of using this
2993** configuration option can be seen in the "test_sqllog.c" source file in
2994** the canonical SQLite source tree.</dd>
2995**
2996** [[SQLITE_CONFIG_MMAP_SIZE]]
2997** <dt>SQLITE_CONFIG_MMAP_SIZE
2998** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2999** that are the default mmap size limit (the default setting for
3000** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
3001** ^The default setting can be overridden by each database connection using
3002** either the [PRAGMA mmap_size] command, or by using the
3003** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
3004** will be silently truncated if necessary so that it does not exceed the
3005** compile-time maximum mmap size set by the
3006** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
3007** ^If either argument to this option is negative, then that argument is
3008** changed to its compile-time default.
3009**
3010** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
3011** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
3012** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
3013** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
3014** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
3015** that specifies the maximum size of the created heap.
3016**
3017** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
3018** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
3019** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
3020** is a pointer to an integer and writes into that integer the number of extra
3021** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
3022** The amount of extra space required can change depending on the compiler,
3023** target platform, and SQLite version.
3024**
3025** [[SQLITE_CONFIG_PMASZ]]
3026** <dt>SQLITE_CONFIG_PMASZ
3027** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
3028** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
3029** sorter to that integer. The default minimum PMA Size is set by the
3030** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
3031** to help with sort operations when multithreaded sorting
3032** is enabled (using the [PRAGMA threads] command) and the amount of content
3033** to be sorted exceeds the page size times the minimum of the
3034** [PRAGMA cache_size] setting and this value.
3035**
3036** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
3037** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
3038** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
3039** becomes the [statement journal] spill-to-disk threshold.
3040** [Statement journals] are held in memory until their size (in bytes)
3041** exceeds this threshold, at which point they are written to disk.
3042** Or if the threshold is -1, statement journals are always held
3043** exclusively in memory.
3044** Since many statement journals never become large, setting the spill
3045** threshold to a value such as 64KiB can greatly reduce the amount of
3046** I/O required to support statement rollback.
3047** The default value for this setting is controlled by the
3048** [SQLITE_STMTJRNL_SPILL] compile-time option.
3049**
3050** [[SQLITE_CONFIG_SORTERREF_SIZE]]
3051** <dt>SQLITE_CONFIG_SORTERREF_SIZE
3052** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
3053** of type (int) - the new value of the sorter-reference size threshold.
3054** Usually, when SQLite uses an external sort to order records according
3055** to an ORDER BY clause, all fields required by the caller are present in the
3056** sorted records. However, if SQLite determines based on the declared type
3057** of a table column that its values are likely to be very large - larger
3058** than the configured sorter-reference size threshold - then a reference
3059** is stored in each sorted record and the required column values loaded
3060** from the database as records are returned in sorted order. The default
3061** value for this option is to never use this optimization. Specifying a
3062** negative value for this option restores the default behaviour.
3063** This option is only available if SQLite is compiled with the
3064** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3065**
3066** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
3067** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
3068** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
3069** [sqlite3_int64] parameter which is the default maximum size for an in-memory
3070** database created using [sqlite3_deserialize()]. This default maximum
3071** size can be adjusted up or down for individual databases using the
3072** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
3073** configuration setting is never used, then the default maximum is determined
3074** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
3075** compile-time option is not set, then the default maximum is 1073741824.
3076** </dl>
3077*/
3078#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
3079#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
3080#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
3081#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
3082#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
3083#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */
3084#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
3085#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
3086#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
3087#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
3088#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
3089/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
3090#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
3091#define SQLITE_CONFIG_PCACHE 14 /* no-op */
3092#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
3093#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
3094#define SQLITE_CONFIG_URI 17 /* int */
3095#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
3096#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
3097#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
3098#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
3099#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
3100#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
3101#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
3102#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
3103#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
3104#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
3105#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
3106#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */
3107
3108/*
3109** CAPI3REF: Database Connection Configuration Options
3110**
3111** These constants are the available integer configuration options that
3112** can be passed as the second argument to the [sqlite3_db_config()] interface.
3113**
3114** New configuration options may be added in future releases of SQLite.
3115** Existing configuration options might be discontinued. Applications
3116** should check the return code from [sqlite3_db_config()] to make sure that
3117** the call worked. ^The [sqlite3_db_config()] interface will return a
3118** non-zero [error code] if a discontinued or unsupported configuration option
3119** is invoked.
3120**
3121** <dl>
3122** [[SQLITE_DBCONFIG_LOOKASIDE]]
3123** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
3124** <dd> ^This option takes three additional arguments that determine the
3125** [lookaside memory allocator] configuration for the [database connection].
3126** ^The first argument (the third parameter to [sqlite3_db_config()] is a
3127** pointer to a memory buffer to use for lookaside memory.
3128** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
3129** may be NULL in which case SQLite will allocate the
3130** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
3131** size of each lookaside buffer slot. ^The third argument is the number of
3132** slots. The size of the buffer in the first argument must be greater than
3133** or equal to the product of the second and third arguments. The buffer
3134** must be aligned to an 8-byte boundary. ^If the second argument to
3135** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
3136** rounded down to the next smaller multiple of 8. ^(The lookaside memory
3137** configuration for a database connection can only be changed when that
3138** connection is not currently using lookaside memory, or in other words
3139** when the "current value" returned by
3140** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
3141** Any attempt to change the lookaside memory configuration when lookaside
3142** memory is in use leaves the configuration unchanged and returns
3143** [SQLITE_BUSY].)^</dd>
3144**
3145** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
3146** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
3147** <dd> ^This option is used to enable or disable the enforcement of
3148** [foreign key constraints]. There should be two additional arguments.
3149** The first argument is an integer which is 0 to disable FK enforcement,
3150** positive to enable FK enforcement or negative to leave FK enforcement
3151** unchanged. The second parameter is a pointer to an integer into which
3152** is written 0 or 1 to indicate whether FK enforcement is off or on
3153** following this call. The second parameter may be a NULL pointer, in
3154** which case the FK enforcement setting is not reported back. </dd>
3155**
3156** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
3157** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
3158** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
3159** There should be two additional arguments.
3160** The first argument is an integer which is 0 to disable triggers,
3161** positive to enable triggers or negative to leave the setting unchanged.
3162** The second parameter is a pointer to an integer into which
3163** is written 0 or 1 to indicate whether triggers are disabled or enabled
3164** following this call. The second parameter may be a NULL pointer, in
3165** which case the trigger setting is not reported back. </dd>
3166**
3167** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
3168** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
3169** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
3170** There should be two additional arguments.
3171** The first argument is an integer which is 0 to disable views,
3172** positive to enable views or negative to leave the setting unchanged.
3173** The second parameter is a pointer to an integer into which
3174** is written 0 or 1 to indicate whether views are disabled or enabled
3175** following this call. The second parameter may be a NULL pointer, in
3176** which case the view setting is not reported back. </dd>
3177**
3178** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
3179** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3180** <dd> ^This option is used to enable or disable the
3181** [fts3_tokenizer()] function which is part of the
3182** [FTS3] full-text search engine extension.
3183** There should be two additional arguments.
3184** The first argument is an integer which is 0 to disable fts3_tokenizer() or
3185** positive to enable fts3_tokenizer() or negative to leave the setting
3186** unchanged.
3187** The second parameter is a pointer to an integer into which
3188** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
3189** following this call. The second parameter may be a NULL pointer, in
3190** which case the new setting is not reported back. </dd>
3191**
3192** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
3193** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
3194** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
3195** interface independently of the [load_extension()] SQL function.
3196** The [sqlite3_enable_load_extension()] API enables or disables both the
3197** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3198** There should be two additional arguments.
3199** When the first argument to this interface is 1, then only the C-API is
3200** enabled and the SQL function remains disabled. If the first argument to
3201** this interface is 0, then both the C-API and the SQL function are disabled.
3202** If the first argument is -1, then no changes are made to state of either the
3203** C-API or the SQL function.
3204** The second parameter is a pointer to an integer into which
3205** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
3206** is disabled or enabled following this call. The second parameter may
3207** be a NULL pointer, in which case the new setting is not reported back.
3208** </dd>
3209**
3210** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
3211** <dd> ^This option is used to change the name of the "main" database
3212** schema. ^The sole argument is a pointer to a constant UTF8 string
3213** which will become the new schema name in place of "main". ^SQLite
3214** does not make a copy of the new main schema name string, so the application
3215** must ensure that the argument passed into this DBCONFIG option is unchanged
3216** until after the database connection closes.
3217** </dd>
3218**
3219** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
3220** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
3221** <dd> Usually, when a database in wal mode is closed or detached from a
3222** database handle, SQLite checks if this will mean that there are now no
3223** connections at all to the database. If so, it performs a checkpoint
3224** operation before closing the connection. This option may be used to
3225** override this behaviour. The first parameter passed to this operation
3226** is an integer - positive to disable checkpoints-on-close, or zero (the
3227** default) to enable them, and negative to leave the setting unchanged.
3228** The second parameter is a pointer to an integer
3229** into which is written 0 or 1 to indicate whether checkpoints-on-close
3230** have been disabled - 0 if they are not disabled, 1 if they are.
3231** </dd>
3232**
3233** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
3234** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
3235** the [query planner stability guarantee] (QPSG). When the QPSG is active,
3236** a single SQL query statement will always use the same algorithm regardless
3237** of values of [bound parameters].)^ The QPSG disables some query optimizations
3238** that look at the values of bound parameters, which can make some queries
3239** slower. But the QPSG has the advantage of more predictable behavior. With
3240** the QPSG active, SQLite will always use the same query plan in the field as
3241** was used during testing in the lab.
3242** The first argument to this setting is an integer which is 0 to disable
3243** the QPSG, positive to enable QPSG, or negative to leave the setting
3244** unchanged. The second parameter is a pointer to an integer into which
3245** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
3246** following this call.
3247** </dd>
3248**
3249** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
3250** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
3251** include output for any operations performed by trigger programs. This
3252** option is used to set or clear (the default) a flag that governs this
3253** behavior. The first parameter passed to this operation is an integer -
3254** positive to enable output for trigger programs, or zero to disable it,
3255** or negative to leave the setting unchanged.
3256** The second parameter is a pointer to an integer into which is written
3257** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3258** it is not disabled, 1 if it is.
3259** </dd>
3260**
3261** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3262** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3263** [VACUUM] in order to reset a database back to an empty database
3264** with no schema and no content. The following process works even for
3265** a badly corrupted database file:
3266** <ol>
3267** <li> If the database connection is newly opened, make sure it has read the
3268** database schema by preparing then discarding some query against the
3269** database, or calling sqlite3_table_column_metadata(), ignoring any
3270** errors. This step is only necessary if the application desires to keep
3271** the database in WAL mode after the reset if it was in WAL mode before
3272** the reset.
3273** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3274** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3275** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3276** </ol>
3277** Because resetting a database is destructive and irreversible, the
3278** process requires the use of this obscure API and multiple steps to help
3279** ensure that it does not happen by accident.
3280**
3281** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
3282** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
3283** "defensive" flag for a database connection. When the defensive
3284** flag is enabled, language features that allow ordinary SQL to
3285** deliberately corrupt the database file are disabled. The disabled
3286** features include but are not limited to the following:
3287** <ul>
3288** <li> The [PRAGMA writable_schema=ON] statement.
3289** <li> The [PRAGMA journal_mode=OFF] statement.
3290** <li> Writes to the [sqlite_dbpage] virtual table.
3291** <li> Direct writes to [shadow tables].
3292** </ul>
3293** </dd>
3294**
3295** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
3296** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
3297** "writable_schema" flag. This has the same effect and is logically equivalent
3298** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
3299** The first argument to this setting is an integer which is 0 to disable
3300** the writable_schema, positive to enable writable_schema, or negative to
3301** leave the setting unchanged. The second parameter is a pointer to an
3302** integer into which is written 0 or 1 to indicate whether the writable_schema
3303** is enabled or disabled following this call.
3304** </dd>
3305**
3306** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
3307** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
3308** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
3309** the legacy behavior of the [ALTER TABLE RENAME] command such it
3310** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
3311** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
3312** additional information. This feature can also be turned on and off
3313** using the [PRAGMA legacy_alter_table] statement.
3314** </dd>
3315**
3316** [[SQLITE_DBCONFIG_DQS_DML]]
3317** <dt>SQLITE_DBCONFIG_DQS_DML</td>
3318** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
3319** the legacy [double-quoted string literal] misfeature for DML statements
3320** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
3321** default value of this setting is determined by the [-DSQLITE_DQS]
3322** compile-time option.
3323** </dd>
3324**
3325** [[SQLITE_DBCONFIG_DQS_DDL]]
3326** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
3327** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
3328** the legacy [double-quoted string literal] misfeature for DDL statements,
3329** such as CREATE TABLE and CREATE INDEX. The
3330** default value of this setting is determined by the [-DSQLITE_DQS]
3331** compile-time option.
3332** </dd>
3333**
3334** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
3335** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</td>
3336** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
3337** assume that database schemas are untainted by malicious content.
3338** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
3339** takes additional defensive steps to protect the application from harm
3340** including:
3341** <ul>
3342** <li> Prohibit the use of SQL functions inside triggers, views,
3343** CHECK constraints, DEFAULT clauses, expression indexes,
3344** partial indexes, or generated columns
3345** unless those functions are tagged with [SQLITE_INNOCUOUS].
3346** <li> Prohibit the use of virtual tables inside of triggers or views
3347** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
3348** </ul>
3349** This setting defaults to "on" for legacy compatibility, however
3350** all applications are advised to turn it off if possible. This setting
3351** can also be controlled using the [PRAGMA trusted_schema] statement.
3352** </dd>
3353**
3354** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
3355** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</td>
3356** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
3357** the legacy file format flag. When activated, this flag causes all newly
3358** created database file to have a schema format version number (the 4-byte
3359** integer found at offset 44 into the database header) of 1. This in turn
3360** means that the resulting database file will be readable and writable by
3361** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
3362** newly created databases are generally not understandable by SQLite versions
3363** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
3364** is now scarcely any need to generated database files that are compatible
3365** all the way back to version 3.0.0, and so this setting is of little
3366** practical use, but is provided so that SQLite can continue to claim the
3367** ability to generate new database files that are compatible with version
3368** 3.0.0.
3369** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
3370** the [VACUUM] command will fail with an obscure error when attempting to
3371** process a table with generated columns and a descending index. This is
3372** not considered a bug since SQLite versions 3.3.0 and earlier do not support
3373** either generated columns or decending indexes.
3374** </dd>
3375** </dl>
3376*/
3377#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
3378#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
3379#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
3380#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
3381#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3382#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3383#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
3384#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
3385#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3386#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
3387#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */
3388#define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */
3389#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */
3390#define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */
3391#define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */
3392#define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */
3393#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */
3394#define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */
3395#define SQLITE_DBCONFIG_MAX 1017 /* Largest DBCONFIG */
3396
3397/*
3398** CAPI3REF: Enable Or Disable Extended Result Codes
3399** METHOD: sqlite3
3400**
3401** ^The sqlite3_extended_result_codes() routine enables or disables the
3402** [extended result codes] feature of SQLite. ^The extended result
3403** codes are disabled by default for historical compatibility.
3404*/
3405SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3406
3407/*
3408** CAPI3REF: Last Insert Rowid
3409** METHOD: sqlite3
3410**
3411** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3412** has a unique 64-bit signed
3413** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3414** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3415** names are not also used by explicitly declared columns. ^If
3416** the table has a column of type [INTEGER PRIMARY KEY] then that column
3417** is another alias for the rowid.
3418**
3419** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3420** the most recent successful [INSERT] into a rowid table or [virtual table]
3421** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3422** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3423** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3424** zero.
3425**
3426** As well as being set automatically as rows are inserted into database
3427** tables, the value returned by this function may be set explicitly by
3428** [sqlite3_set_last_insert_rowid()]
3429**
3430** Some virtual table implementations may INSERT rows into rowid tables as
3431** part of committing a transaction (e.g. to flush data accumulated in memory
3432** to disk). In this case subsequent calls to this function return the rowid
3433** associated with these internal INSERT operations, which leads to
3434** unintuitive results. Virtual table implementations that do write to rowid
3435** tables in this way can avoid this problem by restoring the original
3436** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3437** control to the user.
3438**
3439** ^(If an [INSERT] occurs within a trigger then this routine will
3440** return the [rowid] of the inserted row as long as the trigger is
3441** running. Once the trigger program ends, the value returned
3442** by this routine reverts to what it was before the trigger was fired.)^
3443**
3444** ^An [INSERT] that fails due to a constraint violation is not a
3445** successful [INSERT] and does not change the value returned by this
3446** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3447** and INSERT OR ABORT make no changes to the return value of this
3448** routine when their insertion fails. ^(When INSERT OR REPLACE
3449** encounters a constraint violation, it does not fail. The
3450** INSERT continues to completion after deleting rows that caused
3451** the constraint problem so INSERT OR REPLACE will always change
3452** the return value of this interface.)^
3453**
3454** ^For the purposes of this routine, an [INSERT] is considered to
3455** be successful even if it is subsequently rolled back.
3456**
3457** This function is accessible to SQL statements via the
3458** [last_insert_rowid() SQL function].
3459**
3460** If a separate thread performs a new [INSERT] on the same
3461** database connection while the [sqlite3_last_insert_rowid()]
3462** function is running and thus changes the last insert [rowid],
3463** then the value returned by [sqlite3_last_insert_rowid()] is
3464** unpredictable and might not equal either the old or the new
3465** last insert [rowid].
3466*/
3467SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3468
3469/*
3470** CAPI3REF: Set the Last Insert Rowid value.
3471** METHOD: sqlite3
3472**
3473** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3474** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3475** without inserting a row into the database.
3476*/
3477SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3478
3479/*
3480** CAPI3REF: Count The Number Of Rows Modified
3481** METHOD: sqlite3
3482**
3483** ^This function returns the number of rows modified, inserted or
3484** deleted by the most recently completed INSERT, UPDATE or DELETE
3485** statement on the database connection specified by the only parameter.
3486** ^Executing any other type of SQL statement does not modify the value
3487** returned by this function.
3488**
3489** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3490** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3491** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3492**
3493** Changes to a view that are intercepted by
3494** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3495** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3496** DELETE statement run on a view is always zero. Only changes made to real
3497** tables are counted.
3498**
3499** Things are more complicated if the sqlite3_changes() function is
3500** executed while a trigger program is running. This may happen if the
3501** program uses the [changes() SQL function], or if some other callback
3502** function invokes sqlite3_changes() directly. Essentially:
3503**
3504** <ul>
3505** <li> ^(Before entering a trigger program the value returned by
3506** sqlite3_changes() function is saved. After the trigger program
3507** has finished, the original value is restored.)^
3508**
3509** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3510** statement sets the value returned by sqlite3_changes()
3511** upon completion as normal. Of course, this value will not include
3512** any changes performed by sub-triggers, as the sqlite3_changes()
3513** value will be saved and restored after each sub-trigger has run.)^
3514** </ul>
3515**
3516** ^This means that if the changes() SQL function (or similar) is used
3517** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3518** returns the value as set when the calling statement began executing.
3519** ^If it is used by the second or subsequent such statement within a trigger
3520** program, the value returned reflects the number of rows modified by the
3521** previous INSERT, UPDATE or DELETE statement within the same trigger.
3522**
3523** If a separate thread makes changes on the same database connection
3524** while [sqlite3_changes()] is running then the value returned
3525** is unpredictable and not meaningful.
3526**
3527** See also:
3528** <ul>
3529** <li> the [sqlite3_total_changes()] interface
3530** <li> the [count_changes pragma]
3531** <li> the [changes() SQL function]
3532** <li> the [data_version pragma]
3533** </ul>
3534*/
3535SQLITE_API int sqlite3_changes(sqlite3*);
3536
3537/*
3538** CAPI3REF: Total Number Of Rows Modified
3539** METHOD: sqlite3
3540**
3541** ^This function returns the total number of rows inserted, modified or
3542** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3543** since the database connection was opened, including those executed as
3544** part of trigger programs. ^Executing any other type of SQL statement
3545** does not affect the value returned by sqlite3_total_changes().
3546**
3547** ^Changes made as part of [foreign key actions] are included in the
3548** count, but those made as part of REPLACE constraint resolution are
3549** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3550** are not counted.
3551**
3552** The [sqlite3_total_changes(D)] interface only reports the number
3553** of rows that changed due to SQL statement run against database
3554** connection D. Any changes by other database connections are ignored.
3555** To detect changes against a database file from other database
3556** connections use the [PRAGMA data_version] command or the
3557** [SQLITE_FCNTL_DATA_VERSION] [file control].
3558**
3559** If a separate thread makes changes on the same database connection
3560** while [sqlite3_total_changes()] is running then the value
3561** returned is unpredictable and not meaningful.
3562**
3563** See also:
3564** <ul>
3565** <li> the [sqlite3_changes()] interface
3566** <li> the [count_changes pragma]
3567** <li> the [changes() SQL function]
3568** <li> the [data_version pragma]
3569** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3570** </ul>
3571*/
3572SQLITE_API int sqlite3_total_changes(sqlite3*);
3573
3574/*
3575** CAPI3REF: Interrupt A Long-Running Query
3576** METHOD: sqlite3
3577**
3578** ^This function causes any pending database operation to abort and
3579** return at its earliest opportunity. This routine is typically
3580** called in response to a user action such as pressing "Cancel"
3581** or Ctrl-C where the user wants a long query operation to halt
3582** immediately.
3583**
3584** ^It is safe to call this routine from a thread different from the
3585** thread that is currently running the database operation. But it
3586** is not safe to call this routine with a [database connection] that
3587** is closed or might close before sqlite3_interrupt() returns.
3588**
3589** ^If an SQL operation is very nearly finished at the time when
3590** sqlite3_interrupt() is called, then it might not have an opportunity
3591** to be interrupted and might continue to completion.
3592**
3593** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3594** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3595** that is inside an explicit transaction, then the entire transaction
3596** will be rolled back automatically.
3597**
3598** ^The sqlite3_interrupt(D) call is in effect until all currently running
3599** SQL statements on [database connection] D complete. ^Any new SQL statements
3600** that are started after the sqlite3_interrupt() call and before the
3601** running statement count reaches zero are interrupted as if they had been
3602** running prior to the sqlite3_interrupt() call. ^New SQL statements
3603** that are started after the running statement count reaches zero are
3604** not effected by the sqlite3_interrupt().
3605** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3606** SQL statements is a no-op and has no effect on SQL statements
3607** that are started after the sqlite3_interrupt() call returns.
3608*/
3609SQLITE_API void sqlite3_interrupt(sqlite3*);
3610
3611/*
3612** CAPI3REF: Determine If An SQL Statement Is Complete
3613**
3614** These routines are useful during command-line input to determine if the
3615** currently entered text seems to form a complete SQL statement or
3616** if additional input is needed before sending the text into
3617** SQLite for parsing. ^These routines return 1 if the input string
3618** appears to be a complete SQL statement. ^A statement is judged to be
3619** complete if it ends with a semicolon token and is not a prefix of a
3620** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3621** string literals or quoted identifier names or comments are not
3622** independent tokens (they are part of the token in which they are
3623** embedded) and thus do not count as a statement terminator. ^Whitespace
3624** and comments that follow the final semicolon are ignored.
3625**
3626** ^These routines return 0 if the statement is incomplete. ^If a
3627** memory allocation fails, then SQLITE_NOMEM is returned.
3628**
3629** ^These routines do not parse the SQL statements thus
3630** will not detect syntactically incorrect SQL.
3631**
3632** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3633** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3634** automatically by sqlite3_complete16(). If that initialization fails,
3635** then the return value from sqlite3_complete16() will be non-zero
3636** regardless of whether or not the input SQL is complete.)^
3637**
3638** The input to [sqlite3_complete()] must be a zero-terminated
3639** UTF-8 string.
3640**
3641** The input to [sqlite3_complete16()] must be a zero-terminated
3642** UTF-16 string in native byte order.
3643*/
3644SQLITE_API int sqlite3_complete(const char *sql);
3645SQLITE_API int sqlite3_complete16(const void *sql);
3646
3647/*
3648** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3649** KEYWORDS: {busy-handler callback} {busy handler}
3650** METHOD: sqlite3
3651**
3652** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3653** that might be invoked with argument P whenever
3654** an attempt is made to access a database table associated with
3655** [database connection] D when another thread
3656** or process has the table locked.
3657** The sqlite3_busy_handler() interface is used to implement
3658** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3659**
3660** ^If the busy callback is NULL, then [SQLITE_BUSY]
3661** is returned immediately upon encountering the lock. ^If the busy callback
3662** is not NULL, then the callback might be invoked with two arguments.
3663**
3664** ^The first argument to the busy handler is a copy of the void* pointer which
3665** is the third argument to sqlite3_busy_handler(). ^The second argument to
3666** the busy handler callback is the number of times that the busy handler has
3667** been invoked previously for the same locking event. ^If the
3668** busy callback returns 0, then no additional attempts are made to
3669** access the database and [SQLITE_BUSY] is returned
3670** to the application.
3671** ^If the callback returns non-zero, then another attempt
3672** is made to access the database and the cycle repeats.
3673**
3674** The presence of a busy handler does not guarantee that it will be invoked
3675** when there is lock contention. ^If SQLite determines that invoking the busy
3676** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3677** to the application instead of invoking the
3678** busy handler.
3679** Consider a scenario where one process is holding a read lock that
3680** it is trying to promote to a reserved lock and
3681** a second process is holding a reserved lock that it is trying
3682** to promote to an exclusive lock. The first process cannot proceed
3683** because it is blocked by the second and the second process cannot
3684** proceed because it is blocked by the first. If both processes
3685** invoke the busy handlers, neither will make any progress. Therefore,
3686** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3687** will induce the first process to release its read lock and allow
3688** the second process to proceed.
3689**
3690** ^The default busy callback is NULL.
3691**
3692** ^(There can only be a single busy handler defined for each
3693** [database connection]. Setting a new busy handler clears any
3694** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3695** or evaluating [PRAGMA busy_timeout=N] will change the
3696** busy handler and thus clear any previously set busy handler.
3697**
3698** The busy callback should not take any actions which modify the
3699** database connection that invoked the busy handler. In other words,
3700** the busy handler is not reentrant. Any such actions
3701** result in undefined behavior.
3702**
3703** A busy handler must not close the database connection
3704** or [prepared statement] that invoked the busy handler.
3705*/
3706SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3707
3708/*
3709** CAPI3REF: Set A Busy Timeout
3710** METHOD: sqlite3
3711**
3712** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3713** for a specified amount of time when a table is locked. ^The handler
3714** will sleep multiple times until at least "ms" milliseconds of sleeping
3715** have accumulated. ^After at least "ms" milliseconds of sleeping,
3716** the handler returns 0 which causes [sqlite3_step()] to return
3717** [SQLITE_BUSY].
3718**
3719** ^Calling this routine with an argument less than or equal to zero
3720** turns off all busy handlers.
3721**
3722** ^(There can only be a single busy handler for a particular
3723** [database connection] at any given moment. If another busy handler
3724** was defined (using [sqlite3_busy_handler()]) prior to calling
3725** this routine, that other busy handler is cleared.)^
3726**
3727** See also: [PRAGMA busy_timeout]
3728*/
3729SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3730
3731/*
3732** CAPI3REF: Convenience Routines For Running Queries
3733** METHOD: sqlite3
3734**
3735** This is a legacy interface that is preserved for backwards compatibility.
3736** Use of this interface is not recommended.
3737**
3738** Definition: A <b>result table</b> is memory data structure created by the
3739** [sqlite3_get_table()] interface. A result table records the
3740** complete query results from one or more queries.
3741**
3742** The table conceptually has a number of rows and columns. But
3743** these numbers are not part of the result table itself. These
3744** numbers are obtained separately. Let N be the number of rows
3745** and M be the number of columns.
3746**
3747** A result table is an array of pointers to zero-terminated UTF-8 strings.
3748** There are (N+1)*M elements in the array. The first M pointers point
3749** to zero-terminated strings that contain the names of the columns.
3750** The remaining entries all point to query results. NULL values result
3751** in NULL pointers. All other values are in their UTF-8 zero-terminated
3752** string representation as returned by [sqlite3_column_text()].
3753**
3754** A result table might consist of one or more memory allocations.
3755** It is not safe to pass a result table directly to [sqlite3_free()].
3756** A result table should be deallocated using [sqlite3_free_table()].
3757**
3758** ^(As an example of the result table format, suppose a query result
3759** is as follows:
3760**
3761** <blockquote><pre>
3762** Name | Age
3763** -----------------------
3764** Alice | 43
3765** Bob | 28
3766** Cindy | 21
3767** </pre></blockquote>
3768**
3769** There are two columns (M==2) and three rows (N==3). Thus the
3770** result table has 8 entries. Suppose the result table is stored
3771** in an array named azResult. Then azResult holds this content:
3772**
3773** <blockquote><pre>
3774** azResult&#91;0] = "Name";
3775** azResult&#91;1] = "Age";
3776** azResult&#91;2] = "Alice";
3777** azResult&#91;3] = "43";
3778** azResult&#91;4] = "Bob";
3779** azResult&#91;5] = "28";
3780** azResult&#91;6] = "Cindy";
3781** azResult&#91;7] = "21";
3782** </pre></blockquote>)^
3783**
3784** ^The sqlite3_get_table() function evaluates one or more
3785** semicolon-separated SQL statements in the zero-terminated UTF-8
3786** string of its 2nd parameter and returns a result table to the
3787** pointer given in its 3rd parameter.
3788**
3789** After the application has finished with the result from sqlite3_get_table(),
3790** it must pass the result table pointer to sqlite3_free_table() in order to
3791** release the memory that was malloced. Because of the way the
3792** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3793** function must not try to call [sqlite3_free()] directly. Only
3794** [sqlite3_free_table()] is able to release the memory properly and safely.
3795**
3796** The sqlite3_get_table() interface is implemented as a wrapper around
3797** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3798** to any internal data structures of SQLite. It uses only the public
3799** interface defined here. As a consequence, errors that occur in the
3800** wrapper layer outside of the internal [sqlite3_exec()] call are not
3801** reflected in subsequent calls to [sqlite3_errcode()] or
3802** [sqlite3_errmsg()].
3803*/
3804SQLITE_API int sqlite3_get_table(
3805 sqlite3 *db, /* An open database */
3806 const char *zSql, /* SQL to be evaluated */
3807 char ***pazResult, /* Results of the query */
3808 int *pnRow, /* Number of result rows written here */
3809 int *pnColumn, /* Number of result columns written here */
3810 char **pzErrmsg /* Error msg written here */
3811);
3812SQLITE_API void sqlite3_free_table(char **result);
3813
3814/*
3815** CAPI3REF: Formatted String Printing Functions
3816**
3817** These routines are work-alikes of the "printf()" family of functions
3818** from the standard C library.
3819** These routines understand most of the common formatting options from
3820** the standard library printf()
3821** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3822** See the [built-in printf()] documentation for details.
3823**
3824** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3825** results into memory obtained from [sqlite3_malloc64()].
3826** The strings returned by these two routines should be
3827** released by [sqlite3_free()]. ^Both routines return a
3828** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3829** memory to hold the resulting string.
3830**
3831** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3832** the standard C library. The result is written into the
3833** buffer supplied as the second parameter whose size is given by
3834** the first parameter. Note that the order of the
3835** first two parameters is reversed from snprintf().)^ This is an
3836** historical accident that cannot be fixed without breaking
3837** backwards compatibility. ^(Note also that sqlite3_snprintf()
3838** returns a pointer to its buffer instead of the number of
3839** characters actually written into the buffer.)^ We admit that
3840** the number of characters written would be a more useful return
3841** value but we cannot change the implementation of sqlite3_snprintf()
3842** now without breaking compatibility.
3843**
3844** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3845** guarantees that the buffer is always zero-terminated. ^The first
3846** parameter "n" is the total size of the buffer, including space for
3847** the zero terminator. So the longest string that can be completely
3848** written will be n-1 characters.
3849**
3850** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3851**
3852** See also: [built-in printf()], [printf() SQL function]
3853*/
3854SQLITE_API char *sqlite3_mprintf(const char*,...);
3855SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3856SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3857SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3858
3859/*
3860** CAPI3REF: Memory Allocation Subsystem
3861**
3862** The SQLite core uses these three routines for all of its own
3863** internal memory allocation needs. "Core" in the previous sentence
3864** does not include operating-system specific [VFS] implementation. The
3865** Windows VFS uses native malloc() and free() for some operations.
3866**
3867** ^The sqlite3_malloc() routine returns a pointer to a block
3868** of memory at least N bytes in length, where N is the parameter.
3869** ^If sqlite3_malloc() is unable to obtain sufficient free
3870** memory, it returns a NULL pointer. ^If the parameter N to
3871** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3872** a NULL pointer.
3873**
3874** ^The sqlite3_malloc64(N) routine works just like
3875** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3876** of a signed 32-bit integer.
3877**
3878** ^Calling sqlite3_free() with a pointer previously returned
3879** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3880** that it might be reused. ^The sqlite3_free() routine is
3881** a no-op if is called with a NULL pointer. Passing a NULL pointer
3882** to sqlite3_free() is harmless. After being freed, memory
3883** should neither be read nor written. Even reading previously freed
3884** memory might result in a segmentation fault or other severe error.
3885** Memory corruption, a segmentation fault, or other severe error
3886** might result if sqlite3_free() is called with a non-NULL pointer that
3887** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3888**
3889** ^The sqlite3_realloc(X,N) interface attempts to resize a
3890** prior memory allocation X to be at least N bytes.
3891** ^If the X parameter to sqlite3_realloc(X,N)
3892** is a NULL pointer then its behavior is identical to calling
3893** sqlite3_malloc(N).
3894** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3895** negative then the behavior is exactly the same as calling
3896** sqlite3_free(X).
3897** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3898** of at least N bytes in size or NULL if insufficient memory is available.
3899** ^If M is the size of the prior allocation, then min(N,M) bytes
3900** of the prior allocation are copied into the beginning of buffer returned
3901** by sqlite3_realloc(X,N) and the prior allocation is freed.
3902** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3903** prior allocation is not freed.
3904**
3905** ^The sqlite3_realloc64(X,N) interfaces works the same as
3906** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3907** of a 32-bit signed integer.
3908**
3909** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3910** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3911** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3912** ^The value returned by sqlite3_msize(X) might be larger than the number
3913** of bytes requested when X was allocated. ^If X is a NULL pointer then
3914** sqlite3_msize(X) returns zero. If X points to something that is not
3915** the beginning of memory allocation, or if it points to a formerly
3916** valid memory allocation that has now been freed, then the behavior
3917** of sqlite3_msize(X) is undefined and possibly harmful.
3918**
3919** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3920** sqlite3_malloc64(), and sqlite3_realloc64()
3921** is always aligned to at least an 8 byte boundary, or to a
3922** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3923** option is used.
3924**
3925** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3926** must be either NULL or else pointers obtained from a prior
3927** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3928** not yet been released.
3929**
3930** The application must not read or write any part of
3931** a block of memory after it has been released using
3932** [sqlite3_free()] or [sqlite3_realloc()].
3933*/
3934SQLITE_API void *sqlite3_malloc(int);
3935SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3936SQLITE_API void *sqlite3_realloc(void*, int);
3937SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3938SQLITE_API void sqlite3_free(void*);
3939SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3940
3941/*
3942** CAPI3REF: Memory Allocator Statistics
3943**
3944** SQLite provides these two interfaces for reporting on the status
3945** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3946** routines, which form the built-in memory allocation subsystem.
3947**
3948** ^The [sqlite3_memory_used()] routine returns the number of bytes
3949** of memory currently outstanding (malloced but not freed).
3950** ^The [sqlite3_memory_highwater()] routine returns the maximum
3951** value of [sqlite3_memory_used()] since the high-water mark
3952** was last reset. ^The values returned by [sqlite3_memory_used()] and
3953** [sqlite3_memory_highwater()] include any overhead
3954** added by SQLite in its implementation of [sqlite3_malloc()],
3955** but not overhead added by the any underlying system library
3956** routines that [sqlite3_malloc()] may call.
3957**
3958** ^The memory high-water mark is reset to the current value of
3959** [sqlite3_memory_used()] if and only if the parameter to
3960** [sqlite3_memory_highwater()] is true. ^The value returned
3961** by [sqlite3_memory_highwater(1)] is the high-water mark
3962** prior to the reset.
3963*/
3964SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3965SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3966
3967/*
3968** CAPI3REF: Pseudo-Random Number Generator
3969**
3970** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3971** select random [ROWID | ROWIDs] when inserting new records into a table that
3972** already uses the largest possible [ROWID]. The PRNG is also used for
3973** the built-in random() and randomblob() SQL functions. This interface allows
3974** applications to access the same PRNG for other purposes.
3975**
3976** ^A call to this routine stores N bytes of randomness into buffer P.
3977** ^The P parameter can be a NULL pointer.
3978**
3979** ^If this routine has not been previously called or if the previous
3980** call had N less than one or a NULL pointer for P, then the PRNG is
3981** seeded using randomness obtained from the xRandomness method of
3982** the default [sqlite3_vfs] object.
3983** ^If the previous call to this routine had an N of 1 or more and a
3984** non-NULL P then the pseudo-randomness is generated
3985** internally and without recourse to the [sqlite3_vfs] xRandomness
3986** method.
3987*/
3988SQLITE_API void sqlite3_randomness(int N, void *P);
3989
3990/*
3991** CAPI3REF: Compile-Time Authorization Callbacks
3992** METHOD: sqlite3
3993** KEYWORDS: {authorizer callback}
3994**
3995** ^This routine registers an authorizer callback with a particular
3996** [database connection], supplied in the first argument.
3997** ^The authorizer callback is invoked as SQL statements are being compiled
3998** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3999** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
4000** and [sqlite3_prepare16_v3()]. ^At various
4001** points during the compilation process, as logic is being created
4002** to perform various actions, the authorizer callback is invoked to
4003** see if those actions are allowed. ^The authorizer callback should
4004** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
4005** specific action but allow the SQL statement to continue to be
4006** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
4007** rejected with an error. ^If the authorizer callback returns
4008** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
4009** then the [sqlite3_prepare_v2()] or equivalent call that triggered
4010** the authorizer will fail with an error message.
4011**
4012** When the callback returns [SQLITE_OK], that means the operation
4013** requested is ok. ^When the callback returns [SQLITE_DENY], the
4014** [sqlite3_prepare_v2()] or equivalent call that triggered the
4015** authorizer will fail with an error message explaining that
4016** access is denied.
4017**
4018** ^The first parameter to the authorizer callback is a copy of the third
4019** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
4020** to the callback is an integer [SQLITE_COPY | action code] that specifies
4021** the particular action to be authorized. ^The third through sixth parameters
4022** to the callback are either NULL pointers or zero-terminated strings
4023** that contain additional details about the action to be authorized.
4024** Applications must always be prepared to encounter a NULL pointer in any
4025** of the third through the sixth parameters of the authorization callback.
4026**
4027** ^If the action code is [SQLITE_READ]
4028** and the callback returns [SQLITE_IGNORE] then the
4029** [prepared statement] statement is constructed to substitute
4030** a NULL value in place of the table column that would have
4031** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
4032** return can be used to deny an untrusted user access to individual
4033** columns of a table.
4034** ^When a table is referenced by a [SELECT] but no column values are
4035** extracted from that table (for example in a query like
4036** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
4037** is invoked once for that table with a column name that is an empty string.
4038** ^If the action code is [SQLITE_DELETE] and the callback returns
4039** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
4040** [truncate optimization] is disabled and all rows are deleted individually.
4041**
4042** An authorizer is used when [sqlite3_prepare | preparing]
4043** SQL statements from an untrusted source, to ensure that the SQL statements
4044** do not try to access data they are not allowed to see, or that they do not
4045** try to execute malicious statements that damage the database. For
4046** example, an application may allow a user to enter arbitrary
4047** SQL queries for evaluation by a database. But the application does
4048** not want the user to be able to make arbitrary changes to the
4049** database. An authorizer could then be put in place while the
4050** user-entered SQL is being [sqlite3_prepare | prepared] that
4051** disallows everything except [SELECT] statements.
4052**
4053** Applications that need to process SQL from untrusted sources
4054** might also consider lowering resource limits using [sqlite3_limit()]
4055** and limiting database size using the [max_page_count] [PRAGMA]
4056** in addition to using an authorizer.
4057**
4058** ^(Only a single authorizer can be in place on a database connection
4059** at a time. Each call to sqlite3_set_authorizer overrides the
4060** previous call.)^ ^Disable the authorizer by installing a NULL callback.
4061** The authorizer is disabled by default.
4062**
4063** The authorizer callback must not do anything that will modify
4064** the database connection that invoked the authorizer callback.
4065** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4066** database connections for the meaning of "modify" in this paragraph.
4067**
4068** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
4069** statement might be re-prepared during [sqlite3_step()] due to a
4070** schema change. Hence, the application should ensure that the
4071** correct authorizer callback remains in place during the [sqlite3_step()].
4072**
4073** ^Note that the authorizer callback is invoked only during
4074** [sqlite3_prepare()] or its variants. Authorization is not
4075** performed during statement evaluation in [sqlite3_step()], unless
4076** as stated in the previous paragraph, sqlite3_step() invokes
4077** sqlite3_prepare_v2() to reprepare a statement after a schema change.
4078*/
4079SQLITE_API int sqlite3_set_authorizer(
4080 sqlite3*,
4081 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
4082 void *pUserData
4083);
4084
4085/*
4086** CAPI3REF: Authorizer Return Codes
4087**
4088** The [sqlite3_set_authorizer | authorizer callback function] must
4089** return either [SQLITE_OK] or one of these two constants in order
4090** to signal SQLite whether or not the action is permitted. See the
4091** [sqlite3_set_authorizer | authorizer documentation] for additional
4092** information.
4093**
4094** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
4095** returned from the [sqlite3_vtab_on_conflict()] interface.
4096*/
4097#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
4098#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
4099
4100/*
4101** CAPI3REF: Authorizer Action Codes
4102**
4103** The [sqlite3_set_authorizer()] interface registers a callback function
4104** that is invoked to authorize certain SQL statement actions. The
4105** second parameter to the callback is an integer code that specifies
4106** what action is being authorized. These are the integer action codes that
4107** the authorizer callback may be passed.
4108**
4109** These action code values signify what kind of operation is to be
4110** authorized. The 3rd and 4th parameters to the authorization
4111** callback function will be parameters or NULL depending on which of these
4112** codes is used as the second parameter. ^(The 5th parameter to the
4113** authorizer callback is the name of the database ("main", "temp",
4114** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
4115** is the name of the inner-most trigger or view that is responsible for
4116** the access attempt or NULL if this access attempt is directly from
4117** top-level SQL code.
4118*/
4119/******************************************* 3rd ************ 4th ***********/
4120#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
4121#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
4122#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
4123#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
4124#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
4125#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
4126#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
4127#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
4128#define SQLITE_DELETE 9 /* Table Name NULL */
4129#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
4130#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
4131#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
4132#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
4133#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
4134#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
4135#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
4136#define SQLITE_DROP_VIEW 17 /* View Name NULL */
4137#define SQLITE_INSERT 18 /* Table Name NULL */
4138#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
4139#define SQLITE_READ 20 /* Table Name Column Name */
4140#define SQLITE_SELECT 21 /* NULL NULL */
4141#define SQLITE_TRANSACTION 22 /* Operation NULL */
4142#define SQLITE_UPDATE 23 /* Table Name Column Name */
4143#define SQLITE_ATTACH 24 /* Filename NULL */
4144#define SQLITE_DETACH 25 /* Database Name NULL */
4145#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
4146#define SQLITE_REINDEX 27 /* Index Name NULL */
4147#define SQLITE_ANALYZE 28 /* Table Name NULL */
4148#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
4149#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
4150#define SQLITE_FUNCTION 31 /* NULL Function Name */
4151#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
4152#define SQLITE_COPY 0 /* No longer used */
4153#define SQLITE_RECURSIVE 33 /* NULL NULL */
4154
4155/*
4156** CAPI3REF: Tracing And Profiling Functions
4157** METHOD: sqlite3
4158**
4159** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
4160** instead of the routines described here.
4161**
4162** These routines register callback functions that can be used for
4163** tracing and profiling the execution of SQL statements.
4164**
4165** ^The callback function registered by sqlite3_trace() is invoked at
4166** various times when an SQL statement is being run by [sqlite3_step()].
4167** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
4168** SQL statement text as the statement first begins executing.
4169** ^(Additional sqlite3_trace() callbacks might occur
4170** as each triggered subprogram is entered. The callbacks for triggers
4171** contain a UTF-8 SQL comment that identifies the trigger.)^
4172**
4173** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4174** the length of [bound parameter] expansion in the output of sqlite3_trace().
4175**
4176** ^The callback function registered by sqlite3_profile() is invoked
4177** as each SQL statement finishes. ^The profile callback contains
4178** the original statement text and an estimate of wall-clock time
4179** of how long that statement took to run. ^The profile callback
4180** time is in units of nanoseconds, however the current implementation
4181** is only capable of millisecond resolution so the six least significant
4182** digits in the time are meaningless. Future versions of SQLite
4183** might provide greater resolution on the profiler callback. Invoking
4184** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
4185** profile callback.
4186*/
4187SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
4188 void(*xTrace)(void*,const char*), void*);
4189SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
4190 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
4191
4192/*
4193** CAPI3REF: SQL Trace Event Codes
4194** KEYWORDS: SQLITE_TRACE
4195**
4196** These constants identify classes of events that can be monitored
4197** using the [sqlite3_trace_v2()] tracing logic. The M argument
4198** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4199** the following constants. ^The first argument to the trace callback
4200** is one of the following constants.
4201**
4202** New tracing constants may be added in future releases.
4203**
4204** ^A trace callback has four arguments: xCallback(T,C,P,X).
4205** ^The T argument is one of the integer type codes above.
4206** ^The C argument is a copy of the context pointer passed in as the
4207** fourth argument to [sqlite3_trace_v2()].
4208** The P and X arguments are pointers whose meanings depend on T.
4209**
4210** <dl>
4211** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
4212** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
4213** first begins running and possibly at other times during the
4214** execution of the prepared statement, such as at the start of each
4215** trigger subprogram. ^The P argument is a pointer to the
4216** [prepared statement]. ^The X argument is a pointer to a string which
4217** is the unexpanded SQL text of the prepared statement or an SQL comment
4218** that indicates the invocation of a trigger. ^The callback can compute
4219** the same text that would have been returned by the legacy [sqlite3_trace()]
4220** interface by using the X argument when X begins with "--" and invoking
4221** [sqlite3_expanded_sql(P)] otherwise.
4222**
4223** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
4224** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
4225** information as is provided by the [sqlite3_profile()] callback.
4226** ^The P argument is a pointer to the [prepared statement] and the
4227** X argument points to a 64-bit integer which is the estimated of
4228** the number of nanosecond that the prepared statement took to run.
4229** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
4230**
4231** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
4232** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
4233** statement generates a single row of result.
4234** ^The P argument is a pointer to the [prepared statement] and the
4235** X argument is unused.
4236**
4237** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
4238** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
4239** connection closes.
4240** ^The P argument is a pointer to the [database connection] object
4241** and the X argument is unused.
4242** </dl>
4243*/
4244#define SQLITE_TRACE_STMT 0x01
4245#define SQLITE_TRACE_PROFILE 0x02
4246#define SQLITE_TRACE_ROW 0x04
4247#define SQLITE_TRACE_CLOSE 0x08
4248
4249/*
4250** CAPI3REF: SQL Trace Hook
4251** METHOD: sqlite3
4252**
4253** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
4254** function X against [database connection] D, using property mask M
4255** and context pointer P. ^If the X callback is
4256** NULL or if the M mask is zero, then tracing is disabled. The
4257** M argument should be the bitwise OR-ed combination of
4258** zero or more [SQLITE_TRACE] constants.
4259**
4260** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
4261** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
4262**
4263** ^The X callback is invoked whenever any of the events identified by
4264** mask M occur. ^The integer return value from the callback is currently
4265** ignored, though this may change in future releases. Callback
4266** implementations should return zero to ensure future compatibility.
4267**
4268** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
4269** ^The T argument is one of the [SQLITE_TRACE]
4270** constants to indicate why the callback was invoked.
4271** ^The C argument is a copy of the context pointer.
4272** The P and X arguments are pointers whose meanings depend on T.
4273**
4274** The sqlite3_trace_v2() interface is intended to replace the legacy
4275** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
4276** are deprecated.
4277*/
4278SQLITE_API int sqlite3_trace_v2(
4279 sqlite3*,
4280 unsigned uMask,
4281 int(*xCallback)(unsigned,void*,void*,void*),
4282 void *pCtx
4283);
4284
4285/*
4286** CAPI3REF: Query Progress Callbacks
4287** METHOD: sqlite3
4288**
4289** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
4290** function X to be invoked periodically during long running calls to
4291** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
4292** database connection D. An example use for this
4293** interface is to keep a GUI updated during a large query.
4294**
4295** ^The parameter P is passed through as the only parameter to the
4296** callback function X. ^The parameter N is the approximate number of
4297** [virtual machine instructions] that are evaluated between successive
4298** invocations of the callback X. ^If N is less than one then the progress
4299** handler is disabled.
4300**
4301** ^Only a single progress handler may be defined at one time per
4302** [database connection]; setting a new progress handler cancels the
4303** old one. ^Setting parameter X to NULL disables the progress handler.
4304** ^The progress handler is also disabled by setting N to a value less
4305** than 1.
4306**
4307** ^If the progress callback returns non-zero, the operation is
4308** interrupted. This feature can be used to implement a
4309** "Cancel" button on a GUI progress dialog box.
4310**
4311** The progress handler callback must not do anything that will modify
4312** the database connection that invoked the progress handler.
4313** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4314** database connections for the meaning of "modify" in this paragraph.
4315**
4316*/
4317SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
4318
4319/*
4320** CAPI3REF: Opening A New Database Connection
4321** CONSTRUCTOR: sqlite3
4322**
4323** ^These routines open an SQLite database file as specified by the
4324** filename argument. ^The filename argument is interpreted as UTF-8 for
4325** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4326** order for sqlite3_open16(). ^(A [database connection] handle is usually
4327** returned in *ppDb, even if an error occurs. The only exception is that
4328** if SQLite is unable to allocate memory to hold the [sqlite3] object,
4329** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4330** object.)^ ^(If the database is opened (and/or created) successfully, then
4331** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
4332** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
4333** an English language description of the error following a failure of any
4334** of the sqlite3_open() routines.
4335**
4336** ^The default encoding will be UTF-8 for databases created using
4337** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
4338** created using sqlite3_open16() will be UTF-16 in the native byte order.
4339**
4340** Whether or not an error occurs when it is opened, resources
4341** associated with the [database connection] handle should be released by
4342** passing it to [sqlite3_close()] when it is no longer required.
4343**
4344** The sqlite3_open_v2() interface works like sqlite3_open()
4345** except that it accepts two additional parameters for additional control
4346** over the new database connection. ^(The flags parameter to
4347** sqlite3_open_v2() must include, at a minimum, one of the following
4348** three flag combinations:)^
4349**
4350** <dl>
4351** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4352** <dd>The database is opened in read-only mode. If the database does not
4353** already exist, an error is returned.</dd>)^
4354**
4355** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4356** <dd>The database is opened for reading and writing if possible, or reading
4357** only if the file is write protected by the operating system. In either
4358** case the database must already exist, otherwise an error is returned.</dd>)^
4359**
4360** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4361** <dd>The database is opened for reading and writing, and is created if
4362** it does not already exist. This is the behavior that is always used for
4363** sqlite3_open() and sqlite3_open16().</dd>)^
4364** </dl>
4365**
4366** In addition to the required flags, the following optional flags are
4367** also supported:
4368**
4369** <dl>
4370** ^(<dt>[SQLITE_OPEN_URI]</dt>
4371** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
4372**
4373** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
4374** <dd>The database will be opened as an in-memory database. The database
4375** is named by the "filename" argument for the purposes of cache-sharing,
4376** if shared cache mode is enabled, but the "filename" is otherwise ignored.
4377** </dd>)^
4378**
4379** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
4380** <dd>The new database connection will use the "multi-thread"
4381** [threading mode].)^ This means that separate threads are allowed
4382** to use SQLite at the same time, as long as each thread is using
4383** a different [database connection].
4384**
4385** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
4386** <dd>The new database connection will use the "serialized"
4387** [threading mode].)^ This means the multiple threads can safely
4388** attempt to use the same database connection at the same time.
4389** (Mutexes will block any actual concurrency, but in this mode
4390** there is no harm in trying.)
4391**
4392** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
4393** <dd>The database is opened [shared cache] enabled, overriding
4394** the default shared cache setting provided by
4395** [sqlite3_enable_shared_cache()].)^
4396**
4397** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
4398** <dd>The database is opened [shared cache] disabled, overriding
4399** the default shared cache setting provided by
4400** [sqlite3_enable_shared_cache()].)^
4401**
4402** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
4403** <dd>The database filename is not allowed to be a symbolic link</dd>
4404** </dl>)^
4405**
4406** If the 3rd parameter to sqlite3_open_v2() is not one of the
4407** required combinations shown above optionally combined with other
4408** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4409** then the behavior is undefined.
4410**
4411** ^The fourth parameter to sqlite3_open_v2() is the name of the
4412** [sqlite3_vfs] object that defines the operating system interface that
4413** the new database connection should use. ^If the fourth parameter is
4414** a NULL pointer then the default [sqlite3_vfs] object is used.
4415**
4416** ^If the filename is ":memory:", then a private, temporary in-memory database
4417** is created for the connection. ^This in-memory database will vanish when
4418** the database connection is closed. Future versions of SQLite might
4419** make use of additional special filenames that begin with the ":" character.
4420** It is recommended that when a database filename actually does begin with
4421** a ":" character you should prefix the filename with a pathname such as
4422** "./" to avoid ambiguity.
4423**
4424** ^If the filename is an empty string, then a private, temporary
4425** on-disk database will be created. ^This private database will be
4426** automatically deleted as soon as the database connection is closed.
4427**
4428** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4429**
4430** ^If [URI filename] interpretation is enabled, and the filename argument
4431** begins with "file:", then the filename is interpreted as a URI. ^URI
4432** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4433** set in the third argument to sqlite3_open_v2(), or if it has
4434** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4435** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4436** URI filename interpretation is turned off
4437** by default, but future releases of SQLite might enable URI filename
4438** interpretation by default. See "[URI filenames]" for additional
4439** information.
4440**
4441** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4442** authority, then it must be either an empty string or the string
4443** "localhost". ^If the authority is not an empty string or "localhost", an
4444** error is returned to the caller. ^The fragment component of a URI, if
4445** present, is ignored.
4446**
4447** ^SQLite uses the path component of the URI as the name of the disk file
4448** which contains the database. ^If the path begins with a '/' character,
4449** then it is interpreted as an absolute path. ^If the path does not begin
4450** with a '/' (meaning that the authority section is omitted from the URI)
4451** then the path is interpreted as a relative path.
4452** ^(On windows, the first component of an absolute path
4453** is a drive specification (e.g. "C:").)^
4454**
4455** [[core URI query parameters]]
4456** The query component of a URI may contain parameters that are interpreted
4457** either by SQLite itself, or by a [VFS | custom VFS implementation].
4458** SQLite and its built-in [VFSes] interpret the
4459** following query parameters:
4460**
4461** <ul>
4462** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4463** a VFS object that provides the operating system interface that should
4464** be used to access the database file on disk. ^If this option is set to
4465** an empty string the default VFS object is used. ^Specifying an unknown
4466** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4467** present, then the VFS specified by the option takes precedence over
4468** the value passed as the fourth parameter to sqlite3_open_v2().
4469**
4470** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4471** "rwc", or "memory". Attempting to set it to any other value is
4472** an error)^.
4473** ^If "ro" is specified, then the database is opened for read-only
4474** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4475** third argument to sqlite3_open_v2(). ^If the mode option is set to
4476** "rw", then the database is opened for read-write (but not create)
4477** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4478** been set. ^Value "rwc" is equivalent to setting both
4479** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
4480** set to "memory" then a pure [in-memory database] that never reads
4481** or writes from disk is used. ^It is an error to specify a value for
4482** the mode parameter that is less restrictive than that specified by
4483** the flags passed in the third parameter to sqlite3_open_v2().
4484**
4485** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4486** "private". ^Setting it to "shared" is equivalent to setting the
4487** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4488** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4489** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4490** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4491** a URI filename, its value overrides any behavior requested by setting
4492** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4493**
4494** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4495** [powersafe overwrite] property does or does not apply to the
4496** storage media on which the database file resides.
4497**
4498** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4499** which if set disables file locking in rollback journal modes. This
4500** is useful for accessing a database on a filesystem that does not
4501** support locking. Caution: Database corruption might result if two
4502** or more processes write to the same database and any one of those
4503** processes uses nolock=1.
4504**
4505** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4506** parameter that indicates that the database file is stored on
4507** read-only media. ^When immutable is set, SQLite assumes that the
4508** database file cannot be changed, even by a process with higher
4509** privilege, and so the database is opened read-only and all locking
4510** and change detection is disabled. Caution: Setting the immutable
4511** property on a database file that does in fact change can result
4512** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4513** See also: [SQLITE_IOCAP_IMMUTABLE].
4514**
4515** </ul>
4516**
4517** ^Specifying an unknown parameter in the query component of a URI is not an
4518** error. Future versions of SQLite might understand additional query
4519** parameters. See "[query parameters with special meaning to SQLite]" for
4520** additional information.
4521**
4522** [[URI filename examples]] <h3>URI filename examples</h3>
4523**
4524** <table border="1" align=center cellpadding=5>
4525** <tr><th> URI filenames <th> Results
4526** <tr><td> file:data.db <td>
4527** Open the file "data.db" in the current directory.
4528** <tr><td> file:/home/fred/data.db<br>
4529** file:///home/fred/data.db <br>
4530** file://localhost/home/fred/data.db <br> <td>
4531** Open the database file "/home/fred/data.db".
4532** <tr><td> file://darkstar/home/fred/data.db <td>
4533** An error. "darkstar" is not a recognized authority.
4534** <tr><td style="white-space:nowrap">
4535** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4536** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4537** C:. Note that the %20 escaping in this example is not strictly
4538** necessary - space characters can be used literally
4539** in URI filenames.
4540** <tr><td> file:data.db?mode=ro&cache=private <td>
4541** Open file "data.db" in the current directory for read-only access.
4542** Regardless of whether or not shared-cache mode is enabled by
4543** default, use a private cache.
4544** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4545** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4546** that uses dot-files in place of posix advisory locking.
4547** <tr><td> file:data.db?mode=readonly <td>
4548** An error. "readonly" is not a valid option for the "mode" parameter.
4549** </table>
4550**
4551** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4552** query components of a URI. A hexadecimal escape sequence consists of a
4553** percent sign - "%" - followed by exactly two hexadecimal digits
4554** specifying an octet value. ^Before the path or query components of a
4555** URI filename are interpreted, they are encoded using UTF-8 and all
4556** hexadecimal escape sequences replaced by a single byte containing the
4557** corresponding octet. If this process generates an invalid UTF-8 encoding,
4558** the results are undefined.
4559**
4560** <b>Note to Windows users:</b> The encoding used for the filename argument
4561** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4562** codepage is currently defined. Filenames containing international
4563** characters must be converted to UTF-8 prior to passing them into
4564** sqlite3_open() or sqlite3_open_v2().
4565**
4566** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4567** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4568** features that require the use of temporary files may fail.
4569**
4570** See also: [sqlite3_temp_directory]
4571*/
4572SQLITE_API int sqlite3_open(
4573 const char *filename, /* Database filename (UTF-8) */
4574 sqlite3 **ppDb /* OUT: SQLite db handle */
4575);
4576SQLITE_API int sqlite3_open16(
4577 const void *filename, /* Database filename (UTF-16) */
4578 sqlite3 **ppDb /* OUT: SQLite db handle */
4579);
4580SQLITE_API int sqlite3_open_v2(
4581 const char *filename, /* Database filename (UTF-8) */
4582 sqlite3 **ppDb, /* OUT: SQLite db handle */
4583 int flags, /* Flags */
4584 const char *zVfs /* Name of VFS module to use */
4585);
4586
4587/*
4588** CAPI3REF: Obtain Values For URI Parameters
4589**
4590** These are utility routines, useful to [VFS|custom VFS implementations],
4591** that check if a database file was a URI that contained a specific query
4592** parameter, and if so obtains the value of that query parameter.
4593**
4594** The first parameter to these interfaces (hereafter referred to
4595** as F) must be one of:
4596** <ul>
4597** <li> A database filename pointer created by the SQLite core and
4598** passed into the xOpen() method of a VFS implemention, or
4599** <li> A filename obtained from [sqlite3_db_filename()], or
4600** <li> A new filename constructed using [sqlite3_create_filename()].
4601** </ul>
4602** If the F parameter is not one of the above, then the behavior is
4603** undefined and probably undesirable. Older versions of SQLite were
4604** more tolerant of invalid F parameters than newer versions.
4605**
4606** If F is a suitable filename (as described in the previous paragraph)
4607** and if P is the name of the query parameter, then
4608** sqlite3_uri_parameter(F,P) returns the value of the P
4609** parameter if it exists or a NULL pointer if P does not appear as a
4610** query parameter on F. If P is a query parameter of F and it
4611** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4612** a pointer to an empty string.
4613**
4614** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4615** parameter and returns true (1) or false (0) according to the value
4616** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4617** value of query parameter P is one of "yes", "true", or "on" in any
4618** case or if the value begins with a non-zero number. The
4619** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4620** query parameter P is one of "no", "false", or "off" in any case or
4621** if the value begins with a numeric zero. If P is not a query
4622** parameter on F or if the value of P does not match any of the
4623** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4624**
4625** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4626** 64-bit signed integer and returns that integer, or D if P does not
4627** exist. If the value of P is something other than an integer, then
4628** zero is returned.
4629**
4630** The sqlite3_uri_key(F,N) returns a pointer to the name (not
4631** the value) of the N-th query parameter for filename F, or a NULL
4632** pointer if N is less than zero or greater than the number of query
4633** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4634** the name of the first query parameter, 1 for the second parameter, and
4635** so forth.
4636**
4637** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4638** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4639** is not a database file pathname pointer that the SQLite core passed
4640** into the xOpen VFS method, then the behavior of this routine is undefined
4641** and probably undesirable.
4642**
4643** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
4644** parameter can also be the name of a rollback journal file or WAL file
4645** in addition to the main database file. Prior to version 3.31.0, these
4646** routines would only work if F was the name of the main database file.
4647** When the F parameter is the name of the rollback journal or WAL file,
4648** it has access to all the same query parameters as were found on the
4649** main database file.
4650**
4651** See the [URI filename] documentation for additional information.
4652*/
4653SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4654SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4655SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4656SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
4657
4658/*
4659** CAPI3REF: Translate filenames
4660**
4661** These routines are available to [VFS|custom VFS implementations] for
4662** translating filenames between the main database file, the journal file,
4663** and the WAL file.
4664**
4665** If F is the name of an sqlite database file, journal file, or WAL file
4666** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4667** returns the name of the corresponding database file.
4668**
4669** If F is the name of an sqlite database file, journal file, or WAL file
4670** passed by the SQLite core into the VFS, or if F is a database filename
4671** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4672** returns the name of the corresponding rollback journal file.
4673**
4674** If F is the name of an sqlite database file, journal file, or WAL file
4675** that was passed by the SQLite core into the VFS, or if F is a database
4676** filename obtained from [sqlite3_db_filename()], then
4677** sqlite3_filename_wal(F) returns the name of the corresponding
4678** WAL file.
4679**
4680** In all of the above, if F is not the name of a database, journal or WAL
4681** filename passed into the VFS from the SQLite core and F is not the
4682** return value from [sqlite3_db_filename()], then the result is
4683** undefined and is likely a memory access violation.
4684*/
4685SQLITE_API const char *sqlite3_filename_database(const char*);
4686SQLITE_API const char *sqlite3_filename_journal(const char*);
4687SQLITE_API const char *sqlite3_filename_wal(const char*);
4688
4689/*
4690** CAPI3REF: Database File Corresponding To A Journal
4691**
4692** ^If X is the name of a rollback or WAL-mode journal file that is
4693** passed into the xOpen method of [sqlite3_vfs], then
4694** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4695** object that represents the main database file.
4696**
4697** This routine is intended for use in custom [VFS] implementations
4698** only. It is not a general-purpose interface.
4699** The argument sqlite3_file_object(X) must be a filename pointer that
4700** has been passed into [sqlite3_vfs].xOpen method where the
4701** flags parameter to xOpen contains one of the bits
4702** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4703** of this routine results in undefined and probably undesirable
4704** behavior.
4705*/
4706SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4707
4708/*
4709** CAPI3REF: Create and Destroy VFS Filenames
4710**
4711** These interfces are provided for use by [VFS shim] implementations and
4712** are not useful outside of that context.
4713**
4714** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4715** database filename D with corresponding journal file J and WAL file W and
4716** with N URI parameters key/values pairs in the array P. The result from
4717** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4718** is safe to pass to routines like:
4719** <ul>
4720** <li> [sqlite3_uri_parameter()],
4721** <li> [sqlite3_uri_boolean()],
4722** <li> [sqlite3_uri_int64()],
4723** <li> [sqlite3_uri_key()],
4724** <li> [sqlite3_filename_database()],
4725** <li> [sqlite3_filename_journal()], or
4726** <li> [sqlite3_filename_wal()].
4727** </ul>
4728** If a memory allocation error occurs, sqlite3_create_filename() might
4729** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4730** must be released by a corresponding call to sqlite3_free_filename(Y).
4731**
4732** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4733** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4734** to a key and value for a query parameter. The P parameter may be a NULL
4735** pointer if N is zero. None of the 2*N pointers in the P array may be
4736** NULL pointers and key pointers should not be empty strings.
4737** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4738** be NULL pointers, though they can be empty strings.
4739**
4740** The sqlite3_free_filename(Y) routine releases a memory allocation
4741** previously obtained from sqlite3_create_filename(). Invoking
4742** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4743**
4744** If the Y parameter to sqlite3_free_filename(Y) is anything other
4745** than a NULL pointer or a pointer previously acquired from
4746** sqlite3_create_filename(), then bad things such as heap
4747** corruption or segfaults may occur. The value Y should be
4748** used again after sqlite3_free_filename(Y) has been called. This means
4749** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4750** then the corresponding [sqlite3_module.xClose() method should also be
4751** invoked prior to calling sqlite3_free_filename(Y).
4752*/
4753SQLITE_API char *sqlite3_create_filename(
4754 const char *zDatabase,
4755 const char *zJournal,
4756 const char *zWal,
4757 int nParam,
4758 const char **azParam
4759);
4760SQLITE_API void sqlite3_free_filename(char*);
4761
4762/*
4763** CAPI3REF: Error Codes And Messages
4764** METHOD: sqlite3
4765**
4766** ^If the most recent sqlite3_* API call associated with
4767** [database connection] D failed, then the sqlite3_errcode(D) interface
4768** returns the numeric [result code] or [extended result code] for that
4769** API call.
4770** ^The sqlite3_extended_errcode()
4771** interface is the same except that it always returns the
4772** [extended result code] even when extended result codes are
4773** disabled.
4774**
4775** The values returned by sqlite3_errcode() and/or
4776** sqlite3_extended_errcode() might change with each API call.
4777** Except, there are some interfaces that are guaranteed to never
4778** change the value of the error code. The error-code preserving
4779** interfaces are:
4780**
4781** <ul>
4782** <li> sqlite3_errcode()
4783** <li> sqlite3_extended_errcode()
4784** <li> sqlite3_errmsg()
4785** <li> sqlite3_errmsg16()
4786** </ul>
4787**
4788** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4789** text that describes the error, as either UTF-8 or UTF-16 respectively.
4790** ^(Memory to hold the error message string is managed internally.
4791** The application does not need to worry about freeing the result.
4792** However, the error string might be overwritten or deallocated by
4793** subsequent calls to other SQLite interface functions.)^
4794**
4795** ^The sqlite3_errstr() interface returns the English-language text
4796** that describes the [result code], as UTF-8.
4797** ^(Memory to hold the error message string is managed internally
4798** and must not be freed by the application)^.
4799**
4800** When the serialized [threading mode] is in use, it might be the
4801** case that a second error occurs on a separate thread in between
4802** the time of the first error and the call to these interfaces.
4803** When that happens, the second error will be reported since these
4804** interfaces always report the most recent result. To avoid
4805** this, each thread can obtain exclusive use of the [database connection] D
4806** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4807** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4808** all calls to the interfaces listed here are completed.
4809**
4810** If an interface fails with SQLITE_MISUSE, that means the interface
4811** was invoked incorrectly by the application. In that case, the
4812** error code and message may or may not be set.
4813*/
4814SQLITE_API int sqlite3_errcode(sqlite3 *db);
4815SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4816SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4817SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4818SQLITE_API const char *sqlite3_errstr(int);
4819
4820/*
4821** CAPI3REF: Prepared Statement Object
4822** KEYWORDS: {prepared statement} {prepared statements}
4823**
4824** An instance of this object represents a single SQL statement that
4825** has been compiled into binary form and is ready to be evaluated.
4826**
4827** Think of each SQL statement as a separate computer program. The
4828** original SQL text is source code. A prepared statement object
4829** is the compiled object code. All SQL must be converted into a
4830** prepared statement before it can be run.
4831**
4832** The life-cycle of a prepared statement object usually goes like this:
4833**
4834** <ol>
4835** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4836** <li> Bind values to [parameters] using the sqlite3_bind_*()
4837** interfaces.
4838** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4839** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4840** to step 2. Do this zero or more times.
4841** <li> Destroy the object using [sqlite3_finalize()].
4842** </ol>
4843*/
4844typedef struct sqlite3_stmt sqlite3_stmt;
4845
4846/*
4847** CAPI3REF: Run-time Limits
4848** METHOD: sqlite3
4849**
4850** ^(This interface allows the size of various constructs to be limited
4851** on a connection by connection basis. The first parameter is the
4852** [database connection] whose limit is to be set or queried. The
4853** second parameter is one of the [limit categories] that define a
4854** class of constructs to be size limited. The third parameter is the
4855** new limit for that construct.)^
4856**
4857** ^If the new limit is a negative number, the limit is unchanged.
4858** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4859** [limits | hard upper bound]
4860** set at compile-time by a C preprocessor macro called
4861** [limits | SQLITE_MAX_<i>NAME</i>].
4862** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4863** ^Attempts to increase a limit above its hard upper bound are
4864** silently truncated to the hard upper bound.
4865**
4866** ^Regardless of whether or not the limit was changed, the
4867** [sqlite3_limit()] interface returns the prior value of the limit.
4868** ^Hence, to find the current value of a limit without changing it,
4869** simply invoke this interface with the third parameter set to -1.
4870**
4871** Run-time limits are intended for use in applications that manage
4872** both their own internal database and also databases that are controlled
4873** by untrusted external sources. An example application might be a
4874** web browser that has its own databases for storing history and
4875** separate databases controlled by JavaScript applications downloaded
4876** off the Internet. The internal databases can be given the
4877** large, default limits. Databases managed by external sources can
4878** be given much smaller limits designed to prevent a denial of service
4879** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4880** interface to further control untrusted SQL. The size of the database
4881** created by an untrusted script can be contained using the
4882** [max_page_count] [PRAGMA].
4883**
4884** New run-time limit categories may be added in future releases.
4885*/
4886SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4887
4888/*
4889** CAPI3REF: Run-Time Limit Categories
4890** KEYWORDS: {limit category} {*limit categories}
4891**
4892** These constants define various performance limits
4893** that can be lowered at run-time using [sqlite3_limit()].
4894** The synopsis of the meanings of the various limits is shown below.
4895** Additional information is available at [limits | Limits in SQLite].
4896**
4897** <dl>
4898** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4899** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4900**
4901** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4902** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4903**
4904** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4905** <dd>The maximum number of columns in a table definition or in the
4906** result set of a [SELECT] or the maximum number of columns in an index
4907** or in an ORDER BY or GROUP BY clause.</dd>)^
4908**
4909** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4910** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4911**
4912** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4913** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4914**
4915** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4916** <dd>The maximum number of instructions in a virtual machine program
4917** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4918** the equivalent tries to allocate space for more than this many opcodes
4919** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4920**
4921** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4922** <dd>The maximum number of arguments on a function.</dd>)^
4923**
4924** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4925** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4926**
4927** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4928** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4929** <dd>The maximum length of the pattern argument to the [LIKE] or
4930** [GLOB] operators.</dd>)^
4931**
4932** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4933** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4934** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4935**
4936** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4937** <dd>The maximum depth of recursion for triggers.</dd>)^
4938**
4939** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4940** <dd>The maximum number of auxiliary worker threads that a single
4941** [prepared statement] may start.</dd>)^
4942** </dl>
4943*/
4944#define SQLITE_LIMIT_LENGTH 0
4945#define SQLITE_LIMIT_SQL_LENGTH 1
4946#define SQLITE_LIMIT_COLUMN 2
4947#define SQLITE_LIMIT_EXPR_DEPTH 3
4948#define SQLITE_LIMIT_COMPOUND_SELECT 4
4949#define SQLITE_LIMIT_VDBE_OP 5
4950#define SQLITE_LIMIT_FUNCTION_ARG 6
4951#define SQLITE_LIMIT_ATTACHED 7
4952#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
4953#define SQLITE_LIMIT_VARIABLE_NUMBER 9
4954#define SQLITE_LIMIT_TRIGGER_DEPTH 10
4955#define SQLITE_LIMIT_WORKER_THREADS 11
4956
4957/*
4958** CAPI3REF: Prepare Flags
4959**
4960** These constants define various flags that can be passed into
4961** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4962** [sqlite3_prepare16_v3()] interfaces.
4963**
4964** New flags may be added in future releases of SQLite.
4965**
4966** <dl>
4967** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4968** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4969** that the prepared statement will be retained for a long time and
4970** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4971** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4972** be used just once or at most a few times and then destroyed using
4973** [sqlite3_finalize()] relatively soon. The current implementation acts
4974** on this hint by avoiding the use of [lookaside memory] so as not to
4975** deplete the limited store of lookaside memory. Future versions of
4976** SQLite may act on this hint differently.
4977**
4978** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4979** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4980** to be required for any prepared statement that wanted to use the
4981** [sqlite3_normalized_sql()] interface. However, the
4982** [sqlite3_normalized_sql()] interface is now available to all
4983** prepared statements, regardless of whether or not they use this
4984** flag.
4985**
4986** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4987** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4988** to return an error (error code SQLITE_ERROR) if the statement uses
4989** any virtual tables.
4990** </dl>
4991*/
4992#define SQLITE_PREPARE_PERSISTENT 0x01
4993#define SQLITE_PREPARE_NORMALIZE 0x02
4994#define SQLITE_PREPARE_NO_VTAB 0x04
4995
4996/*
4997** CAPI3REF: Compiling An SQL Statement
4998** KEYWORDS: {SQL statement compiler}
4999** METHOD: sqlite3
5000** CONSTRUCTOR: sqlite3_stmt
5001**
5002** To execute an SQL statement, it must first be compiled into a byte-code
5003** program using one of these routines. Or, in other words, these routines
5004** are constructors for the [prepared statement] object.
5005**
5006** The preferred routine to use is [sqlite3_prepare_v2()]. The
5007** [sqlite3_prepare()] interface is legacy and should be avoided.
5008** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
5009** for special purposes.
5010**
5011** The use of the UTF-8 interfaces is preferred, as SQLite currently
5012** does all parsing using UTF-8. The UTF-16 interfaces are provided
5013** as a convenience. The UTF-16 interfaces work by converting the
5014** input text into UTF-8, then invoking the corresponding UTF-8 interface.
5015**
5016** The first argument, "db", is a [database connection] obtained from a
5017** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
5018** [sqlite3_open16()]. The database connection must not have been closed.
5019**
5020** The second argument, "zSql", is the statement to be compiled, encoded
5021** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
5022** and sqlite3_prepare_v3()
5023** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
5024** and sqlite3_prepare16_v3() use UTF-16.
5025**
5026** ^If the nByte argument is negative, then zSql is read up to the
5027** first zero terminator. ^If nByte is positive, then it is the
5028** number of bytes read from zSql. ^If nByte is zero, then no prepared
5029** statement is generated.
5030** If the caller knows that the supplied string is nul-terminated, then
5031** there is a small performance advantage to passing an nByte parameter that
5032** is the number of bytes in the input string <i>including</i>
5033** the nul-terminator.
5034**
5035** ^If pzTail is not NULL then *pzTail is made to point to the first byte
5036** past the end of the first SQL statement in zSql. These routines only
5037** compile the first statement in zSql, so *pzTail is left pointing to
5038** what remains uncompiled.
5039**
5040** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
5041** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
5042** to NULL. ^If the input text contains no SQL (if the input is an empty
5043** string or a comment) then *ppStmt is set to NULL.
5044** The calling procedure is responsible for deleting the compiled
5045** SQL statement using [sqlite3_finalize()] after it has finished with it.
5046** ppStmt may not be NULL.
5047**
5048** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
5049** otherwise an [error code] is returned.
5050**
5051** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
5052** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
5053** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
5054** are retained for backwards compatibility, but their use is discouraged.
5055** ^In the "vX" interfaces, the prepared statement
5056** that is returned (the [sqlite3_stmt] object) contains a copy of the
5057** original SQL text. This causes the [sqlite3_step()] interface to
5058** behave differently in three ways:
5059**
5060** <ol>
5061** <li>
5062** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
5063** always used to do, [sqlite3_step()] will automatically recompile the SQL
5064** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
5065** retries will occur before sqlite3_step() gives up and returns an error.
5066** </li>
5067**
5068** <li>
5069** ^When an error occurs, [sqlite3_step()] will return one of the detailed
5070** [error codes] or [extended error codes]. ^The legacy behavior was that
5071** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
5072** and the application would have to make a second call to [sqlite3_reset()]
5073** in order to find the underlying cause of the problem. With the "v2" prepare
5074** interfaces, the underlying reason for the error is returned immediately.
5075** </li>
5076**
5077** <li>
5078** ^If the specific value bound to a [parameter | host parameter] in the
5079** WHERE clause might influence the choice of query plan for a statement,
5080** then the statement will be automatically recompiled, as if there had been
5081** a schema change, on the first [sqlite3_step()] call following any change
5082** to the [sqlite3_bind_text | bindings] of that [parameter].
5083** ^The specific value of a WHERE-clause [parameter] might influence the
5084** choice of query plan if the parameter is the left-hand side of a [LIKE]
5085** or [GLOB] operator or if the parameter is compared to an indexed column
5086** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
5087** </li>
5088** </ol>
5089**
5090** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
5091** the extra prepFlags parameter, which is a bit array consisting of zero or
5092** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
5093** sqlite3_prepare_v2() interface works exactly the same as
5094** sqlite3_prepare_v3() with a zero prepFlags parameter.
5095*/
5096SQLITE_API int sqlite3_prepare(
5097 sqlite3 *db, /* Database handle */
5098 const char *zSql, /* SQL statement, UTF-8 encoded */
5099 int nByte, /* Maximum length of zSql in bytes. */
5100 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5101 const char **pzTail /* OUT: Pointer to unused portion of zSql */
5102);
5103SQLITE_API int sqlite3_prepare_v2(
5104 sqlite3 *db, /* Database handle */
5105 const char *zSql, /* SQL statement, UTF-8 encoded */
5106 int nByte, /* Maximum length of zSql in bytes. */
5107 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5108 const char **pzTail /* OUT: Pointer to unused portion of zSql */
5109);
5110SQLITE_API int sqlite3_prepare_v3(
5111 sqlite3 *db, /* Database handle */
5112 const char *zSql, /* SQL statement, UTF-8 encoded */
5113 int nByte, /* Maximum length of zSql in bytes. */
5114 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
5115 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5116 const char **pzTail /* OUT: Pointer to unused portion of zSql */
5117);
5118SQLITE_API int sqlite3_prepare16(
5119 sqlite3 *db, /* Database handle */
5120 const void *zSql, /* SQL statement, UTF-16 encoded */
5121 int nByte, /* Maximum length of zSql in bytes. */
5122 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5123 const void **pzTail /* OUT: Pointer to unused portion of zSql */
5124);
5125SQLITE_API int sqlite3_prepare16_v2(
5126 sqlite3 *db, /* Database handle */
5127 const void *zSql, /* SQL statement, UTF-16 encoded */
5128 int nByte, /* Maximum length of zSql in bytes. */
5129 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5130 const void **pzTail /* OUT: Pointer to unused portion of zSql */
5131);
5132SQLITE_API int sqlite3_prepare16_v3(
5133 sqlite3 *db, /* Database handle */
5134 const void *zSql, /* SQL statement, UTF-16 encoded */
5135 int nByte, /* Maximum length of zSql in bytes. */
5136 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
5137 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
5138 const void **pzTail /* OUT: Pointer to unused portion of zSql */
5139);
5140
5141/*
5142** CAPI3REF: Retrieving Statement SQL
5143** METHOD: sqlite3_stmt
5144**
5145** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
5146** SQL text used to create [prepared statement] P if P was
5147** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
5148** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5149** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
5150** string containing the SQL text of prepared statement P with
5151** [bound parameters] expanded.
5152** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
5153** string containing the normalized SQL text of prepared statement P. The
5154** semantics used to normalize a SQL statement are unspecified and subject
5155** to change. At a minimum, literal values will be replaced with suitable
5156** placeholders.
5157**
5158** ^(For example, if a prepared statement is created using the SQL
5159** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
5160** and parameter :xyz is unbound, then sqlite3_sql() will return
5161** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
5162** will return "SELECT 2345,NULL".)^
5163**
5164** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
5165** is available to hold the result, or if the result would exceed the
5166** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
5167**
5168** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
5169** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
5170** option causes sqlite3_expanded_sql() to always return NULL.
5171**
5172** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
5173** are managed by SQLite and are automatically freed when the prepared
5174** statement is finalized.
5175** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
5176** is obtained from [sqlite3_malloc()] and must be free by the application
5177** by passing it to [sqlite3_free()].
5178*/
5179SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
5180SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
5181SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
5182
5183/*
5184** CAPI3REF: Determine If An SQL Statement Writes The Database
5185** METHOD: sqlite3_stmt
5186**
5187** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
5188** and only if the [prepared statement] X makes no direct changes to
5189** the content of the database file.
5190**
5191** Note that [application-defined SQL functions] or
5192** [virtual tables] might change the database indirectly as a side effect.
5193** ^(For example, if an application defines a function "eval()" that
5194** calls [sqlite3_exec()], then the following SQL statement would
5195** change the database file through side-effects:
5196**
5197** <blockquote><pre>
5198** SELECT eval('DELETE FROM t1') FROM t2;
5199** </pre></blockquote>
5200**
5201** But because the [SELECT] statement does not change the database file
5202** directly, sqlite3_stmt_readonly() would still return true.)^
5203**
5204** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
5205** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
5206** since the statements themselves do not actually modify the database but
5207** rather they control the timing of when other statements modify the
5208** database. ^The [ATTACH] and [DETACH] statements also cause
5209** sqlite3_stmt_readonly() to return true since, while those statements
5210** change the configuration of a database connection, they do not make
5211** changes to the content of the database files on disk.
5212** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
5213** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
5214** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
5215** sqlite3_stmt_readonly() returns false for those commands.
5216*/
5217SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
5218
5219/*
5220** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
5221** METHOD: sqlite3_stmt
5222**
5223** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
5224** prepared statement S is an EXPLAIN statement, or 2 if the
5225** statement S is an EXPLAIN QUERY PLAN.
5226** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
5227** an ordinary statement or a NULL pointer.
5228*/
5229SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
5230
5231/*
5232** CAPI3REF: Determine If A Prepared Statement Has Been Reset
5233** METHOD: sqlite3_stmt
5234**
5235** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5236** [prepared statement] S has been stepped at least once using
5237** [sqlite3_step(S)] but has neither run to completion (returned
5238** [SQLITE_DONE] from [sqlite3_step(S)]) nor
5239** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
5240** interface returns false if S is a NULL pointer. If S is not a
5241** NULL pointer and is not a pointer to a valid [prepared statement]
5242** object, then the behavior is undefined and probably undesirable.
5243**
5244** This interface can be used in combination [sqlite3_next_stmt()]
5245** to locate all prepared statements associated with a database
5246** connection that are in need of being reset. This can be used,
5247** for example, in diagnostic routines to search for prepared
5248** statements that are holding a transaction open.
5249*/
5250SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
5251
5252/*
5253** CAPI3REF: Dynamically Typed Value Object
5254** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
5255**
5256** SQLite uses the sqlite3_value object to represent all values
5257** that can be stored in a database table. SQLite uses dynamic typing
5258** for the values it stores. ^Values stored in sqlite3_value objects
5259** can be integers, floating point values, strings, BLOBs, or NULL.
5260**
5261** An sqlite3_value object may be either "protected" or "unprotected".
5262** Some interfaces require a protected sqlite3_value. Other interfaces
5263** will accept either a protected or an unprotected sqlite3_value.
5264** Every interface that accepts sqlite3_value arguments specifies
5265** whether or not it requires a protected sqlite3_value. The
5266** [sqlite3_value_dup()] interface can be used to construct a new
5267** protected sqlite3_value from an unprotected sqlite3_value.
5268**
5269** The terms "protected" and "unprotected" refer to whether or not
5270** a mutex is held. An internal mutex is held for a protected
5271** sqlite3_value object but no mutex is held for an unprotected
5272** sqlite3_value object. If SQLite is compiled to be single-threaded
5273** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
5274** or if SQLite is run in one of reduced mutex modes
5275** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
5276** then there is no distinction between protected and unprotected
5277** sqlite3_value objects and they can be used interchangeably. However,
5278** for maximum code portability it is recommended that applications
5279** still make the distinction between protected and unprotected
5280** sqlite3_value objects even when not strictly required.
5281**
5282** ^The sqlite3_value objects that are passed as parameters into the
5283** implementation of [application-defined SQL functions] are protected.
5284** ^The sqlite3_value object returned by
5285** [sqlite3_column_value()] is unprotected.
5286** Unprotected sqlite3_value objects may only be used as arguments
5287** to [sqlite3_result_value()], [sqlite3_bind_value()], and
5288** [sqlite3_value_dup()].
5289** The [sqlite3_value_blob | sqlite3_value_type()] family of
5290** interfaces require protected sqlite3_value objects.
5291*/
5292typedef struct sqlite3_value sqlite3_value;
5293
5294/*
5295** CAPI3REF: SQL Function Context Object
5296**
5297** The context in which an SQL function executes is stored in an
5298** sqlite3_context object. ^A pointer to an sqlite3_context object
5299** is always first parameter to [application-defined SQL functions].
5300** The application-defined SQL function implementation will pass this
5301** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
5302** [sqlite3_aggregate_context()], [sqlite3_user_data()],
5303** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
5304** and/or [sqlite3_set_auxdata()].
5305*/
5306typedef struct sqlite3_context sqlite3_context;
5307
5308/*
5309** CAPI3REF: Binding Values To Prepared Statements
5310** KEYWORDS: {host parameter} {host parameters} {host parameter name}
5311** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
5312** METHOD: sqlite3_stmt
5313**
5314** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
5315** literals may be replaced by a [parameter] that matches one of following
5316** templates:
5317**
5318** <ul>
5319** <li> ?
5320** <li> ?NNN
5321** <li> :VVV
5322** <li> @VVV
5323** <li> $VVV
5324** </ul>
5325**
5326** In the templates above, NNN represents an integer literal,
5327** and VVV represents an alphanumeric identifier.)^ ^The values of these
5328** parameters (also called "host parameter names" or "SQL parameters")
5329** can be set using the sqlite3_bind_*() routines defined here.
5330**
5331** ^The first argument to the sqlite3_bind_*() routines is always
5332** a pointer to the [sqlite3_stmt] object returned from
5333** [sqlite3_prepare_v2()] or its variants.
5334**
5335** ^The second argument is the index of the SQL parameter to be set.
5336** ^The leftmost SQL parameter has an index of 1. ^When the same named
5337** SQL parameter is used more than once, second and subsequent
5338** occurrences have the same index as the first occurrence.
5339** ^The index for named parameters can be looked up using the
5340** [sqlite3_bind_parameter_index()] API if desired. ^The index
5341** for "?NNN" parameters is the value of NNN.
5342** ^The NNN value must be between 1 and the [sqlite3_limit()]
5343** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
5344**
5345** ^The third argument is the value to bind to the parameter.
5346** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5347** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
5348** is ignored and the end result is the same as sqlite3_bind_null().
5349** ^If the third parameter to sqlite3_bind_text() is not NULL, then
5350** it should be a pointer to well-formed UTF8 text.
5351** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
5352** it should be a pointer to well-formed UTF16 text.
5353** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
5354** it should be a pointer to a well-formed unicode string that is
5355** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
5356** otherwise.
5357**
5358** [[byte-order determination rules]] ^The byte-order of
5359** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5360** found in first character, which is removed, or in the absence of a BOM
5361** the byte order is the native byte order of the host
5362** machine for sqlite3_bind_text16() or the byte order specified in
5363** the 6th parameter for sqlite3_bind_text64().)^
5364** ^If UTF16 input text contains invalid unicode
5365** characters, then SQLite might change those invalid characters
5366** into the unicode replacement character: U+FFFD.
5367**
5368** ^(In those routines that have a fourth argument, its value is the
5369** number of bytes in the parameter. To be clear: the value is the
5370** number of <u>bytes</u> in the value, not the number of characters.)^
5371** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5372** is negative, then the length of the string is
5373** the number of bytes up to the first zero terminator.
5374** If the fourth parameter to sqlite3_bind_blob() is negative, then
5375** the behavior is undefined.
5376** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5377** or sqlite3_bind_text16() or sqlite3_bind_text64() then
5378** that parameter must be the byte offset
5379** where the NUL terminator would occur assuming the string were NUL
5380** terminated. If any NUL characters occurs at byte offsets less than
5381** the value of the fourth parameter then the resulting string value will
5382** contain embedded NULs. The result of expressions involving strings
5383** with embedded NULs is undefined.
5384**
5385** ^The fifth argument to the BLOB and string binding interfaces
5386** is a destructor used to dispose of the BLOB or
5387** string after SQLite has finished with it. ^The destructor is called
5388** to dispose of the BLOB or string even if the call to the bind API fails,
5389** except the destructor is not called if the third parameter is a NULL
5390** pointer or the fourth parameter is negative.
5391** ^If the fifth argument is
5392** the special value [SQLITE_STATIC], then SQLite assumes that the
5393** information is in static, unmanaged space and does not need to be freed.
5394** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
5395** SQLite makes its own private copy of the data immediately, before
5396** the sqlite3_bind_*() routine returns.
5397**
5398** ^The sixth argument to sqlite3_bind_text64() must be one of
5399** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5400** to specify the encoding of the text in the third parameter. If
5401** the sixth argument to sqlite3_bind_text64() is not one of the
5402** allowed values shown above, or if the text encoding is different
5403** from the encoding specified by the sixth parameter, then the behavior
5404** is undefined.
5405**
5406** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5407** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
5408** (just an integer to hold its size) while it is being processed.
5409** Zeroblobs are intended to serve as placeholders for BLOBs whose
5410** content is later written using
5411** [sqlite3_blob_open | incremental BLOB I/O] routines.
5412** ^A negative value for the zeroblob results in a zero-length BLOB.
5413**
5414** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5415** [prepared statement] S to have an SQL value of NULL, but to also be
5416** associated with the pointer P of type T. ^D is either a NULL pointer or
5417** a pointer to a destructor function for P. ^SQLite will invoke the
5418** destructor D with a single argument of P when it is finished using
5419** P. The T parameter should be a static string, preferably a string
5420** literal. The sqlite3_bind_pointer() routine is part of the
5421** [pointer passing interface] added for SQLite 3.20.0.
5422**
5423** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5424** for the [prepared statement] or with a prepared statement for which
5425** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5426** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5427** routine is passed a [prepared statement] that has been finalized, the
5428** result is undefined and probably harmful.
5429**
5430** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5431** ^Unbound parameters are interpreted as NULL.
5432**
5433** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5434** [error code] if anything goes wrong.
5435** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5436** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5437** [SQLITE_MAX_LENGTH].
5438** ^[SQLITE_RANGE] is returned if the parameter
5439** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5440**
5441** See also: [sqlite3_bind_parameter_count()],
5442** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5443*/
5444SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5445SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5446 void(*)(void*));
5447SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5448SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5449SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5450SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5451SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5452SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5453SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5454 void(*)(void*), unsigned char encoding);
5455SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5456SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5457SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5458SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5459
5460/*
5461** CAPI3REF: Number Of SQL Parameters
5462** METHOD: sqlite3_stmt
5463**
5464** ^This routine can be used to find the number of [SQL parameters]
5465** in a [prepared statement]. SQL parameters are tokens of the
5466** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5467** placeholders for values that are [sqlite3_bind_blob | bound]
5468** to the parameters at a later time.
5469**
5470** ^(This routine actually returns the index of the largest (rightmost)
5471** parameter. For all forms except ?NNN, this will correspond to the
5472** number of unique parameters. If parameters of the ?NNN form are used,
5473** there may be gaps in the list.)^
5474**
5475** See also: [sqlite3_bind_blob|sqlite3_bind()],
5476** [sqlite3_bind_parameter_name()], and
5477** [sqlite3_bind_parameter_index()].
5478*/
5479SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5480
5481/*
5482** CAPI3REF: Name Of A Host Parameter
5483** METHOD: sqlite3_stmt
5484**
5485** ^The sqlite3_bind_parameter_name(P,N) interface returns
5486** the name of the N-th [SQL parameter] in the [prepared statement] P.
5487** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5488** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5489** respectively.
5490** In other words, the initial ":" or "$" or "@" or "?"
5491** is included as part of the name.)^
5492** ^Parameters of the form "?" without a following integer have no name
5493** and are referred to as "nameless" or "anonymous parameters".
5494**
5495** ^The first host parameter has an index of 1, not 0.
5496**
5497** ^If the value N is out of range or if the N-th parameter is
5498** nameless, then NULL is returned. ^The returned string is
5499** always in UTF-8 encoding even if the named parameter was
5500** originally specified as UTF-16 in [sqlite3_prepare16()],
5501** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5502**
5503** See also: [sqlite3_bind_blob|sqlite3_bind()],
5504** [sqlite3_bind_parameter_count()], and
5505** [sqlite3_bind_parameter_index()].
5506*/
5507SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5508
5509/*
5510** CAPI3REF: Index Of A Parameter With A Given Name
5511** METHOD: sqlite3_stmt
5512**
5513** ^Return the index of an SQL parameter given its name. ^The
5514** index value returned is suitable for use as the second
5515** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5516** is returned if no matching parameter is found. ^The parameter
5517** name must be given in UTF-8 even if the original statement
5518** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5519** [sqlite3_prepare16_v3()].
5520**
5521** See also: [sqlite3_bind_blob|sqlite3_bind()],
5522** [sqlite3_bind_parameter_count()], and
5523** [sqlite3_bind_parameter_name()].
5524*/
5525SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5526
5527/*
5528** CAPI3REF: Reset All Bindings On A Prepared Statement
5529** METHOD: sqlite3_stmt
5530**
5531** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5532** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5533** ^Use this routine to reset all host parameters to NULL.
5534*/
5535SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5536
5537/*
5538** CAPI3REF: Number Of Columns In A Result Set
5539** METHOD: sqlite3_stmt
5540**
5541** ^Return the number of columns in the result set returned by the
5542** [prepared statement]. ^If this routine returns 0, that means the
5543** [prepared statement] returns no data (for example an [UPDATE]).
5544** ^However, just because this routine returns a positive number does not
5545** mean that one or more rows of data will be returned. ^A SELECT statement
5546** will always have a positive sqlite3_column_count() but depending on the
5547** WHERE clause constraints and the table content, it might return no rows.
5548**
5549** See also: [sqlite3_data_count()]
5550*/
5551SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5552
5553/*
5554** CAPI3REF: Column Names In A Result Set
5555** METHOD: sqlite3_stmt
5556**
5557** ^These routines return the name assigned to a particular column
5558** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5559** interface returns a pointer to a zero-terminated UTF-8 string
5560** and sqlite3_column_name16() returns a pointer to a zero-terminated
5561** UTF-16 string. ^The first parameter is the [prepared statement]
5562** that implements the [SELECT] statement. ^The second parameter is the
5563** column number. ^The leftmost column is number 0.
5564**
5565** ^The returned string pointer is valid until either the [prepared statement]
5566** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5567** reprepared by the first call to [sqlite3_step()] for a particular run
5568** or until the next call to
5569** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5570**
5571** ^If sqlite3_malloc() fails during the processing of either routine
5572** (for example during a conversion from UTF-8 to UTF-16) then a
5573** NULL pointer is returned.
5574**
5575** ^The name of a result column is the value of the "AS" clause for
5576** that column, if there is an AS clause. If there is no AS clause
5577** then the name of the column is unspecified and may change from
5578** one release of SQLite to the next.
5579*/
5580SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5581SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5582
5583/*
5584** CAPI3REF: Source Of Data In A Query Result
5585** METHOD: sqlite3_stmt
5586**
5587** ^These routines provide a means to determine the database, table, and
5588** table column that is the origin of a particular result column in
5589** [SELECT] statement.
5590** ^The name of the database or table or column can be returned as
5591** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5592** the database name, the _table_ routines return the table name, and
5593** the origin_ routines return the column name.
5594** ^The returned string is valid until the [prepared statement] is destroyed
5595** using [sqlite3_finalize()] or until the statement is automatically
5596** reprepared by the first call to [sqlite3_step()] for a particular run
5597** or until the same information is requested
5598** again in a different encoding.
5599**
5600** ^The names returned are the original un-aliased names of the
5601** database, table, and column.
5602**
5603** ^The first argument to these interfaces is a [prepared statement].
5604** ^These functions return information about the Nth result column returned by
5605** the statement, where N is the second function argument.
5606** ^The left-most column is column 0 for these routines.
5607**
5608** ^If the Nth column returned by the statement is an expression or
5609** subquery and is not a column value, then all of these functions return
5610** NULL. ^These routines might also return NULL if a memory allocation error
5611** occurs. ^Otherwise, they return the name of the attached database, table,
5612** or column that query result column was extracted from.
5613**
5614** ^As with all other SQLite APIs, those whose names end with "16" return
5615** UTF-16 encoded strings and the other functions return UTF-8.
5616**
5617** ^These APIs are only available if the library was compiled with the
5618** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5619**
5620** If two or more threads call one or more
5621** [sqlite3_column_database_name | column metadata interfaces]
5622** for the same [prepared statement] and result column
5623** at the same time then the results are undefined.
5624*/
5625SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5626SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5627SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5628SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5629SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5630SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5631
5632/*
5633** CAPI3REF: Declared Datatype Of A Query Result
5634** METHOD: sqlite3_stmt
5635**
5636** ^(The first parameter is a [prepared statement].
5637** If this statement is a [SELECT] statement and the Nth column of the
5638** returned result set of that [SELECT] is a table column (not an
5639** expression or subquery) then the declared type of the table
5640** column is returned.)^ ^If the Nth column of the result set is an
5641** expression or subquery, then a NULL pointer is returned.
5642** ^The returned string is always UTF-8 encoded.
5643**
5644** ^(For example, given the database schema:
5645**
5646** CREATE TABLE t1(c1 VARIANT);
5647**
5648** and the following statement to be compiled:
5649**
5650** SELECT c1 + 1, c1 FROM t1;
5651**
5652** this routine would return the string "VARIANT" for the second result
5653** column (i==1), and a NULL pointer for the first result column (i==0).)^
5654**
5655** ^SQLite uses dynamic run-time typing. ^So just because a column
5656** is declared to contain a particular type does not mean that the
5657** data stored in that column is of the declared type. SQLite is
5658** strongly typed, but the typing is dynamic not static. ^Type
5659** is associated with individual values, not with the containers
5660** used to hold those values.
5661*/
5662SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5663SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5664
5665/*
5666** CAPI3REF: Evaluate An SQL Statement
5667** METHOD: sqlite3_stmt
5668**
5669** After a [prepared statement] has been prepared using any of
5670** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5671** or [sqlite3_prepare16_v3()] or one of the legacy
5672** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5673** must be called one or more times to evaluate the statement.
5674**
5675** The details of the behavior of the sqlite3_step() interface depend
5676** on whether the statement was prepared using the newer "vX" interfaces
5677** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5678** [sqlite3_prepare16_v2()] or the older legacy
5679** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5680** new "vX" interface is recommended for new applications but the legacy
5681** interface will continue to be supported.
5682**
5683** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5684** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5685** ^With the "v2" interface, any of the other [result codes] or
5686** [extended result codes] might be returned as well.
5687**
5688** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5689** database locks it needs to do its job. ^If the statement is a [COMMIT]
5690** or occurs outside of an explicit transaction, then you can retry the
5691** statement. If the statement is not a [COMMIT] and occurs within an
5692** explicit transaction then you should rollback the transaction before
5693** continuing.
5694**
5695** ^[SQLITE_DONE] means that the statement has finished executing
5696** successfully. sqlite3_step() should not be called again on this virtual
5697** machine without first calling [sqlite3_reset()] to reset the virtual
5698** machine back to its initial state.
5699**
5700** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5701** is returned each time a new row of data is ready for processing by the
5702** caller. The values may be accessed using the [column access functions].
5703** sqlite3_step() is called again to retrieve the next row of data.
5704**
5705** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5706** violation) has occurred. sqlite3_step() should not be called again on
5707** the VM. More information may be found by calling [sqlite3_errmsg()].
5708** ^With the legacy interface, a more specific error code (for example,
5709** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5710** can be obtained by calling [sqlite3_reset()] on the
5711** [prepared statement]. ^In the "v2" interface,
5712** the more specific error code is returned directly by sqlite3_step().
5713**
5714** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5715** Perhaps it was called on a [prepared statement] that has
5716** already been [sqlite3_finalize | finalized] or on one that had
5717** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5718** be the case that the same database connection is being used by two or
5719** more threads at the same moment in time.
5720**
5721** For all versions of SQLite up to and including 3.6.23.1, a call to
5722** [sqlite3_reset()] was required after sqlite3_step() returned anything
5723** other than [SQLITE_ROW] before any subsequent invocation of
5724** sqlite3_step(). Failure to reset the prepared statement using
5725** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5726** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5727** sqlite3_step() began
5728** calling [sqlite3_reset()] automatically in this circumstance rather
5729** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5730** break because any application that ever receives an SQLITE_MISUSE error
5731** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5732** can be used to restore the legacy behavior.
5733**
5734** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5735** API always returns a generic error code, [SQLITE_ERROR], following any
5736** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5737** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5738** specific [error codes] that better describes the error.
5739** We admit that this is a goofy design. The problem has been fixed
5740** with the "v2" interface. If you prepare all of your SQL statements
5741** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5742** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5743** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5744** then the more specific [error codes] are returned directly
5745** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5746*/
5747SQLITE_API int sqlite3_step(sqlite3_stmt*);
5748
5749/*
5750** CAPI3REF: Number of columns in a result set
5751** METHOD: sqlite3_stmt
5752**
5753** ^The sqlite3_data_count(P) interface returns the number of columns in the
5754** current row of the result set of [prepared statement] P.
5755** ^If prepared statement P does not have results ready to return
5756** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5757** interfaces) then sqlite3_data_count(P) returns 0.
5758** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5759** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5760** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5761** will return non-zero if previous call to [sqlite3_step](P) returned
5762** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5763** where it always returns zero since each step of that multi-step
5764** pragma returns 0 columns of data.
5765**
5766** See also: [sqlite3_column_count()]
5767*/
5768SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5769
5770/*
5771** CAPI3REF: Fundamental Datatypes
5772** KEYWORDS: SQLITE_TEXT
5773**
5774** ^(Every value in SQLite has one of five fundamental datatypes:
5775**
5776** <ul>
5777** <li> 64-bit signed integer
5778** <li> 64-bit IEEE floating point number
5779** <li> string
5780** <li> BLOB
5781** <li> NULL
5782** </ul>)^
5783**
5784** These constants are codes for each of those types.
5785**
5786** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5787** for a completely different meaning. Software that links against both
5788** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5789** SQLITE_TEXT.
5790*/
5791#define SQLITE_INTEGER 1
5792#define SQLITE_FLOAT 2
5793#define SQLITE_BLOB 4
5794#define SQLITE_NULL 5
5795#ifdef SQLITE_TEXT
5796# undef SQLITE_TEXT
5797#else
5798# define SQLITE_TEXT 3
5799#endif
5800#define SQLITE3_TEXT 3
5801
5802/*
5803** CAPI3REF: Result Values From A Query
5804** KEYWORDS: {column access functions}
5805** METHOD: sqlite3_stmt
5806**
5807** <b>Summary:</b>
5808** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5809** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5810** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5811** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5812** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5813** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5814** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5815** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5816** [sqlite3_value|unprotected sqlite3_value] object.
5817** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5818** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5819** or a UTF-8 TEXT result in bytes
5820** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5821** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5822** TEXT in bytes
5823** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5824** datatype of the result
5825** </table></blockquote>
5826**
5827** <b>Details:</b>
5828**
5829** ^These routines return information about a single column of the current
5830** result row of a query. ^In every case the first argument is a pointer
5831** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5832** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5833** and the second argument is the index of the column for which information
5834** should be returned. ^The leftmost column of the result set has the index 0.
5835** ^The number of columns in the result can be determined using
5836** [sqlite3_column_count()].
5837**
5838** If the SQL statement does not currently point to a valid row, or if the
5839** column index is out of range, the result is undefined.
5840** These routines may only be called when the most recent call to
5841** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5842** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5843** If any of these routines are called after [sqlite3_reset()] or
5844** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5845** something other than [SQLITE_ROW], the results are undefined.
5846** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5847** are called from a different thread while any of these routines
5848** are pending, then the results are undefined.
5849**
5850** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5851** each return the value of a result column in a specific data format. If
5852** the result column is not initially in the requested format (for example,
5853** if the query returns an integer but the sqlite3_column_text() interface
5854** is used to extract the value) then an automatic type conversion is performed.
5855**
5856** ^The sqlite3_column_type() routine returns the
5857** [SQLITE_INTEGER | datatype code] for the initial data type
5858** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5859** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5860** The return value of sqlite3_column_type() can be used to decide which
5861** of the first six interface should be used to extract the column value.
5862** The value returned by sqlite3_column_type() is only meaningful if no
5863** automatic type conversions have occurred for the value in question.
5864** After a type conversion, the result of calling sqlite3_column_type()
5865** is undefined, though harmless. Future
5866** versions of SQLite may change the behavior of sqlite3_column_type()
5867** following a type conversion.
5868**
5869** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5870** or sqlite3_column_bytes16() interfaces can be used to determine the size
5871** of that BLOB or string.
5872**
5873** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5874** routine returns the number of bytes in that BLOB or string.
5875** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5876** the string to UTF-8 and then returns the number of bytes.
5877** ^If the result is a numeric value then sqlite3_column_bytes() uses
5878** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5879** the number of bytes in that string.
5880** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5881**
5882** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5883** routine returns the number of bytes in that BLOB or string.
5884** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5885** the string to UTF-16 and then returns the number of bytes.
5886** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5887** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5888** the number of bytes in that string.
5889** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5890**
5891** ^The values returned by [sqlite3_column_bytes()] and
5892** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5893** of the string. ^For clarity: the values returned by
5894** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5895** bytes in the string, not the number of characters.
5896**
5897** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5898** even empty strings, are always zero-terminated. ^The return
5899** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5900**
5901** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5902** [unprotected sqlite3_value] object. In a multithreaded environment,
5903** an unprotected sqlite3_value object may only be used safely with
5904** [sqlite3_bind_value()] and [sqlite3_result_value()].
5905** If the [unprotected sqlite3_value] object returned by
5906** [sqlite3_column_value()] is used in any other way, including calls
5907** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5908** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5909** Hence, the sqlite3_column_value() interface
5910** is normally only useful within the implementation of
5911** [application-defined SQL functions] or [virtual tables], not within
5912** top-level application code.
5913**
5914** The these routines may attempt to convert the datatype of the result.
5915** ^For example, if the internal representation is FLOAT and a text result
5916** is requested, [sqlite3_snprintf()] is used internally to perform the
5917** conversion automatically. ^(The following table details the conversions
5918** that are applied:
5919**
5920** <blockquote>
5921** <table border="1">
5922** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5923**
5924** <tr><td> NULL <td> INTEGER <td> Result is 0
5925** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5926** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5927** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5928** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5929** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5930** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5931** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5932** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5933** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5934** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5935** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5936** <tr><td> TEXT <td> BLOB <td> No change
5937** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5938** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5939** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5940** </table>
5941** </blockquote>)^
5942**
5943** Note that when type conversions occur, pointers returned by prior
5944** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5945** sqlite3_column_text16() may be invalidated.
5946** Type conversions and pointer invalidations might occur
5947** in the following cases:
5948**
5949** <ul>
5950** <li> The initial content is a BLOB and sqlite3_column_text() or
5951** sqlite3_column_text16() is called. A zero-terminator might
5952** need to be added to the string.</li>
5953** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5954** sqlite3_column_text16() is called. The content must be converted
5955** to UTF-16.</li>
5956** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5957** sqlite3_column_text() is called. The content must be converted
5958** to UTF-8.</li>
5959** </ul>
5960**
5961** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5962** not invalidate a prior pointer, though of course the content of the buffer
5963** that the prior pointer references will have been modified. Other kinds
5964** of conversion are done in place when it is possible, but sometimes they
5965** are not possible and in those cases prior pointers are invalidated.
5966**
5967** The safest policy is to invoke these routines
5968** in one of the following ways:
5969**
5970** <ul>
5971** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5972** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5973** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5974** </ul>
5975**
5976** In other words, you should call sqlite3_column_text(),
5977** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5978** into the desired format, then invoke sqlite3_column_bytes() or
5979** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5980** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5981** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5982** with calls to sqlite3_column_bytes().
5983**
5984** ^The pointers returned are valid until a type conversion occurs as
5985** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5986** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5987** and BLOBs is freed automatically. Do not pass the pointers returned
5988** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5989** [sqlite3_free()].
5990**
5991** As long as the input parameters are correct, these routines will only
5992** fail if an out-of-memory error occurs during a format conversion.
5993** Only the following subset of interfaces are subject to out-of-memory
5994** errors:
5995**
5996** <ul>
5997** <li> sqlite3_column_blob()
5998** <li> sqlite3_column_text()
5999** <li> sqlite3_column_text16()
6000** <li> sqlite3_column_bytes()
6001** <li> sqlite3_column_bytes16()
6002** </ul>
6003**
6004** If an out-of-memory error occurs, then the return value from these
6005** routines is the same as if the column had contained an SQL NULL value.
6006** Valid SQL NULL returns can be distinguished from out-of-memory errors
6007** by invoking the [sqlite3_errcode()] immediately after the suspect
6008** return value is obtained and before any
6009** other SQLite interface is called on the same [database connection].
6010*/
6011SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
6012SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
6013SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
6014SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
6015SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
6016SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
6017SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
6018SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
6019SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
6020SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
6021
6022/*
6023** CAPI3REF: Destroy A Prepared Statement Object
6024** DESTRUCTOR: sqlite3_stmt
6025**
6026** ^The sqlite3_finalize() function is called to delete a [prepared statement].
6027** ^If the most recent evaluation of the statement encountered no errors
6028** or if the statement is never been evaluated, then sqlite3_finalize() returns
6029** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
6030** sqlite3_finalize(S) returns the appropriate [error code] or
6031** [extended error code].
6032**
6033** ^The sqlite3_finalize(S) routine can be called at any point during
6034** the life cycle of [prepared statement] S:
6035** before statement S is ever evaluated, after
6036** one or more calls to [sqlite3_reset()], or after any call
6037** to [sqlite3_step()] regardless of whether or not the statement has
6038** completed execution.
6039**
6040** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
6041**
6042** The application must finalize every [prepared statement] in order to avoid
6043** resource leaks. It is a grievous error for the application to try to use
6044** a prepared statement after it has been finalized. Any use of a prepared
6045** statement after it has been finalized can result in undefined and
6046** undesirable behavior such as segfaults and heap corruption.
6047*/
6048SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
6049
6050/*
6051** CAPI3REF: Reset A Prepared Statement Object
6052** METHOD: sqlite3_stmt
6053**
6054** The sqlite3_reset() function is called to reset a [prepared statement]
6055** object back to its initial state, ready to be re-executed.
6056** ^Any SQL statement variables that had values bound to them using
6057** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
6058** Use [sqlite3_clear_bindings()] to reset the bindings.
6059**
6060** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
6061** back to the beginning of its program.
6062**
6063** ^If the most recent call to [sqlite3_step(S)] for the
6064** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
6065** or if [sqlite3_step(S)] has never before been called on S,
6066** then [sqlite3_reset(S)] returns [SQLITE_OK].
6067**
6068** ^If the most recent call to [sqlite3_step(S)] for the
6069** [prepared statement] S indicated an error, then
6070** [sqlite3_reset(S)] returns an appropriate [error code].
6071**
6072** ^The [sqlite3_reset(S)] interface does not change the values
6073** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
6074*/
6075SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
6076
6077/*
6078** CAPI3REF: Create Or Redefine SQL Functions
6079** KEYWORDS: {function creation routines}
6080** METHOD: sqlite3
6081**
6082** ^These functions (collectively known as "function creation routines")
6083** are used to add SQL functions or aggregates or to redefine the behavior
6084** of existing SQL functions or aggregates. The only differences between
6085** the three "sqlite3_create_function*" routines are the text encoding
6086** expected for the second parameter (the name of the function being
6087** created) and the presence or absence of a destructor callback for
6088** the application data pointer. Function sqlite3_create_window_function()
6089** is similar, but allows the user to supply the extra callback functions
6090** needed by [aggregate window functions].
6091**
6092** ^The first parameter is the [database connection] to which the SQL
6093** function is to be added. ^If an application uses more than one database
6094** connection then application-defined SQL functions must be added
6095** to each database connection separately.
6096**
6097** ^The second parameter is the name of the SQL function to be created or
6098** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
6099** representation, exclusive of the zero-terminator. ^Note that the name
6100** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
6101** ^Any attempt to create a function with a longer name
6102** will result in [SQLITE_MISUSE] being returned.
6103**
6104** ^The third parameter (nArg)
6105** is the number of arguments that the SQL function or
6106** aggregate takes. ^If this parameter is -1, then the SQL function or
6107** aggregate may take any number of arguments between 0 and the limit
6108** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
6109** parameter is less than -1 or greater than 127 then the behavior is
6110** undefined.
6111**
6112** ^The fourth parameter, eTextRep, specifies what
6113** [SQLITE_UTF8 | text encoding] this SQL function prefers for
6114** its parameters. The application should set this parameter to
6115** [SQLITE_UTF16LE] if the function implementation invokes
6116** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
6117** implementation invokes [sqlite3_value_text16be()] on an input, or
6118** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
6119** otherwise. ^The same SQL function may be registered multiple times using
6120** different preferred text encodings, with different implementations for
6121** each encoding.
6122** ^When multiple implementations of the same function are available, SQLite
6123** will pick the one that involves the least amount of data conversion.
6124**
6125** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
6126** to signal that the function will always return the same result given
6127** the same inputs within a single SQL statement. Most SQL functions are
6128** deterministic. The built-in [random()] SQL function is an example of a
6129** function that is not deterministic. The SQLite query planner is able to
6130** perform additional optimizations on deterministic functions, so use
6131** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
6132**
6133** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
6134** flag, which if present prevents the function from being invoked from
6135** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
6136** index expressions, or the WHERE clause of partial indexes.
6137**
6138** <span style="background-color:#ffff90;">
6139** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
6140** all application-defined SQL functions that do not need to be
6141** used inside of triggers, view, CHECK constraints, or other elements of
6142** the database schema. This flags is especially recommended for SQL
6143** functions that have side effects or reveal internal application state.
6144** Without this flag, an attacker might be able to modify the schema of
6145** a database file to include invocations of the function with parameters
6146** chosen by the attacker, which the application will then execute when
6147** the database file is opened and read.
6148** </span>
6149**
6150** ^(The fifth parameter is an arbitrary pointer. The implementation of the
6151** function can gain access to this pointer using [sqlite3_user_data()].)^
6152**
6153** ^The sixth, seventh and eighth parameters passed to the three
6154** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
6155** pointers to C-language functions that implement the SQL function or
6156** aggregate. ^A scalar SQL function requires an implementation of the xFunc
6157** callback only; NULL pointers must be passed as the xStep and xFinal
6158** parameters. ^An aggregate SQL function requires an implementation of xStep
6159** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
6160** SQL function or aggregate, pass NULL pointers for all three function
6161** callbacks.
6162**
6163** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
6164** and xInverse) passed to sqlite3_create_window_function are pointers to
6165** C-language callbacks that implement the new function. xStep and xFinal
6166** must both be non-NULL. xValue and xInverse may either both be NULL, in
6167** which case a regular aggregate function is created, or must both be
6168** non-NULL, in which case the new function may be used as either an aggregate
6169** or aggregate window function. More details regarding the implementation
6170** of aggregate window functions are
6171** [user-defined window functions|available here].
6172**
6173** ^(If the final parameter to sqlite3_create_function_v2() or
6174** sqlite3_create_window_function() is not NULL, then it is destructor for
6175** the application data pointer. The destructor is invoked when the function
6176** is deleted, either by being overloaded or when the database connection
6177** closes.)^ ^The destructor is also invoked if the call to
6178** sqlite3_create_function_v2() fails. ^When the destructor callback is
6179** invoked, it is passed a single argument which is a copy of the application
6180** data pointer which was the fifth parameter to sqlite3_create_function_v2().
6181**
6182** ^It is permitted to register multiple implementations of the same
6183** functions with the same name but with either differing numbers of
6184** arguments or differing preferred text encodings. ^SQLite will use
6185** the implementation that most closely matches the way in which the
6186** SQL function is used. ^A function implementation with a non-negative
6187** nArg parameter is a better match than a function implementation with
6188** a negative nArg. ^A function where the preferred text encoding
6189** matches the database encoding is a better
6190** match than a function where the encoding is different.
6191** ^A function where the encoding difference is between UTF16le and UTF16be
6192** is a closer match than a function where the encoding difference is
6193** between UTF8 and UTF16.
6194**
6195** ^Built-in functions may be overloaded by new application-defined functions.
6196**
6197** ^An application-defined function is permitted to call other
6198** SQLite interfaces. However, such calls must not
6199** close the database connection nor finalize or reset the prepared
6200** statement in which the function is running.
6201*/
6202SQLITE_API int sqlite3_create_function(
6203 sqlite3 *db,
6204 const char *zFunctionName,
6205 int nArg,
6206 int eTextRep,
6207 void *pApp,
6208 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6209 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6210 void (*xFinal)(sqlite3_context*)
6211);
6212SQLITE_API int sqlite3_create_function16(
6213 sqlite3 *db,
6214 const void *zFunctionName,
6215 int nArg,
6216 int eTextRep,
6217 void *pApp,
6218 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6219 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6220 void (*xFinal)(sqlite3_context*)
6221);
6222SQLITE_API int sqlite3_create_function_v2(
6223 sqlite3 *db,
6224 const char *zFunctionName,
6225 int nArg,
6226 int eTextRep,
6227 void *pApp,
6228 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
6229 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6230 void (*xFinal)(sqlite3_context*),
6231 void(*xDestroy)(void*)
6232);
6233SQLITE_API int sqlite3_create_window_function(
6234 sqlite3 *db,
6235 const char *zFunctionName,
6236 int nArg,
6237 int eTextRep,
6238 void *pApp,
6239 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
6240 void (*xFinal)(sqlite3_context*),
6241 void (*xValue)(sqlite3_context*),
6242 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
6243 void(*xDestroy)(void*)
6244);
6245
6246/*
6247** CAPI3REF: Text Encodings
6248**
6249** These constant define integer codes that represent the various
6250** text encodings supported by SQLite.
6251*/
6252#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
6253#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
6254#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
6255#define SQLITE_UTF16 4 /* Use native byte order */
6256#define SQLITE_ANY 5 /* Deprecated */
6257#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
6258
6259/*
6260** CAPI3REF: Function Flags
6261**
6262** These constants may be ORed together with the
6263** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
6264** to [sqlite3_create_function()], [sqlite3_create_function16()], or
6265** [sqlite3_create_function_v2()].
6266**
6267** <dl>
6268** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
6269** The SQLITE_DETERMINISTIC flag means that the new function always gives
6270** the same output when the input parameters are the same.
6271** The [abs|abs() function] is deterministic, for example, but
6272** [randomblob|randomblob()] is not. Functions must
6273** be deterministic in order to be used in certain contexts such as
6274** with the WHERE clause of [partial indexes] or in [generated columns].
6275** SQLite might also optimize deterministic functions by factoring them
6276** out of inner loops.
6277** </dd>
6278**
6279** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
6280** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6281** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
6282** schema structures such as [CHECK constraints], [DEFAULT clauses],
6283** [expression indexes], [partial indexes], or [generated columns].
6284** The SQLITE_DIRECTONLY flags is a security feature which is recommended
6285** for all [application-defined SQL functions], and especially for functions
6286** that have side-effects or that could potentially leak sensitive
6287** information.
6288** </dd>
6289**
6290** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
6291** The SQLITE_INNOCUOUS flag means that the function is unlikely
6292** to cause problems even if misused. An innocuous function should have
6293** no side effects and should not depend on any values other than its
6294** input parameters. The [abs|abs() function] is an example of an
6295** innocuous function.
6296** The [load_extension() SQL function] is not innocuous because of its
6297** side effects.
6298** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
6299** exactly the same. The [random|random() function] is an example of a
6300** function that is innocuous but not deterministic.
6301** <p>Some heightened security settings
6302** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
6303** disable the use of SQL functions inside views and triggers and in
6304** schema structures such as [CHECK constraints], [DEFAULT clauses],
6305** [expression indexes], [partial indexes], and [generated columns] unless
6306** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
6307** are innocuous. Developers are advised to avoid using the
6308** SQLITE_INNOCUOUS flag for application-defined functions unless the
6309** function has been carefully audited and found to be free of potentially
6310** security-adverse side-effects and information-leaks.
6311** </dd>
6312**
6313** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
6314** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6315** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6316** Specifying this flag makes no difference for scalar or aggregate user
6317** functions. However, if it is not specified for a user-defined window
6318** function, then any sub-types belonging to arguments passed to the window
6319** function may be discarded before the window function is called (i.e.
6320** sqlite3_value_subtype() will always return 0).
6321** </dd>
6322** </dl>
6323*/
6324#define SQLITE_DETERMINISTIC 0x000000800
6325#define SQLITE_DIRECTONLY 0x000080000
6326#define SQLITE_SUBTYPE 0x000100000
6327#define SQLITE_INNOCUOUS 0x000200000
6328
6329/*
6330** CAPI3REF: Deprecated Functions
6331** DEPRECATED
6332**
6333** These functions are [deprecated]. In order to maintain
6334** backwards compatibility with older code, these functions continue
6335** to be supported. However, new applications should avoid
6336** the use of these functions. To encourage programmers to avoid
6337** these functions, we will not explain what they do.
6338*/
6339#ifndef SQLITE_OMIT_DEPRECATED
6340SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
6341SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
6342SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
6343SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
6344SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
6345SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
6346 void*,sqlite3_int64);
6347#endif
6348
6349/*
6350** CAPI3REF: Obtaining SQL Values
6351** METHOD: sqlite3_value
6352**
6353** <b>Summary:</b>
6354** <blockquote><table border=0 cellpadding=0 cellspacing=0>
6355** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
6356** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
6357** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6358** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6359** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
6360** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6361** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6362** the native byteorder
6363** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6364** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6365** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
6366** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
6367** or a UTF-8 TEXT in bytes
6368** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
6369** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6370** TEXT in bytes
6371** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
6372** datatype of the value
6373** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
6374** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
6375** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
6376** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
6377** against a virtual table.
6378** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6379** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
6380** </table></blockquote>
6381**
6382** <b>Details:</b>
6383**
6384** These routines extract type, size, and content information from
6385** [protected sqlite3_value] objects. Protected sqlite3_value objects
6386** are used to pass parameter information into the functions that
6387** implement [application-defined SQL functions] and [virtual tables].
6388**
6389** These routines work only with [protected sqlite3_value] objects.
6390** Any attempt to use these routines on an [unprotected sqlite3_value]
6391** is not threadsafe.
6392**
6393** ^These routines work just like the corresponding [column access functions]
6394** except that these routines take a single [protected sqlite3_value] object
6395** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
6396**
6397** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6398** in the native byte-order of the host machine. ^The
6399** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
6400** extract UTF-16 strings as big-endian and little-endian respectively.
6401**
6402** ^If [sqlite3_value] object V was initialized
6403** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
6404** and if X and Y are strings that compare equal according to strcmp(X,Y),
6405** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
6406** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
6407** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6408**
6409** ^(The sqlite3_value_type(V) interface returns the
6410** [SQLITE_INTEGER | datatype code] for the initial datatype of the
6411** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
6412** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
6413** Other interfaces might change the datatype for an sqlite3_value object.
6414** For example, if the datatype is initially SQLITE_INTEGER and
6415** sqlite3_value_text(V) is called to extract a text value for that
6416** integer, then subsequent calls to sqlite3_value_type(V) might return
6417** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
6418** occurs is undefined and may change from one release of SQLite to the next.
6419**
6420** ^(The sqlite3_value_numeric_type() interface attempts to apply
6421** numeric affinity to the value. This means that an attempt is
6422** made to convert the value to an integer or floating point. If
6423** such a conversion is possible without loss of information (in other
6424** words, if the value is a string that looks like a number)
6425** then the conversion is performed. Otherwise no conversion occurs.
6426** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6427**
6428** ^Within the [xUpdate] method of a [virtual table], the
6429** sqlite3_value_nochange(X) interface returns true if and only if
6430** the column corresponding to X is unchanged by the UPDATE operation
6431** that the xUpdate method call was invoked to implement and if
6432** and the prior [xColumn] method call that was invoked to extracted
6433** the value for that column returned without setting a result (probably
6434** because it queried [sqlite3_vtab_nochange()] and found that the column
6435** was unchanging). ^Within an [xUpdate] method, any value for which
6436** sqlite3_value_nochange(X) is true will in all other respects appear
6437** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6438** than within an [xUpdate] method call for an UPDATE statement, then
6439** the return value is arbitrary and meaningless.
6440**
6441** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6442** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6443** interfaces. ^If X comes from an SQL literal value, or a table column,
6444** or an expression, then sqlite3_value_frombind(X) returns zero.
6445**
6446** Please pay particular attention to the fact that the pointer returned
6447** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6448** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6449** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6450** or [sqlite3_value_text16()].
6451**
6452** These routines must be called from the same thread as
6453** the SQL function that supplied the [sqlite3_value*] parameters.
6454**
6455** As long as the input parameter is correct, these routines can only
6456** fail if an out-of-memory error occurs during a format conversion.
6457** Only the following subset of interfaces are subject to out-of-memory
6458** errors:
6459**
6460** <ul>
6461** <li> sqlite3_value_blob()
6462** <li> sqlite3_value_text()
6463** <li> sqlite3_value_text16()
6464** <li> sqlite3_value_text16le()
6465** <li> sqlite3_value_text16be()
6466** <li> sqlite3_value_bytes()
6467** <li> sqlite3_value_bytes16()
6468** </ul>
6469**
6470** If an out-of-memory error occurs, then the return value from these
6471** routines is the same as if the column had contained an SQL NULL value.
6472** Valid SQL NULL returns can be distinguished from out-of-memory errors
6473** by invoking the [sqlite3_errcode()] immediately after the suspect
6474** return value is obtained and before any
6475** other SQLite interface is called on the same [database connection].
6476*/
6477SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6478SQLITE_API double sqlite3_value_double(sqlite3_value*);
6479SQLITE_API int sqlite3_value_int(sqlite3_value*);
6480SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6481SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6482SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6483SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6484SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6485SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6486SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6487SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6488SQLITE_API int sqlite3_value_type(sqlite3_value*);
6489SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6490SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6491SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6492
6493/*
6494** CAPI3REF: Finding The Subtype Of SQL Values
6495** METHOD: sqlite3_value
6496**
6497** The sqlite3_value_subtype(V) function returns the subtype for
6498** an [application-defined SQL function] argument V. The subtype
6499** information can be used to pass a limited amount of context from
6500** one SQL function to another. Use the [sqlite3_result_subtype()]
6501** routine to set the subtype for the return value of an SQL function.
6502*/
6503SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6504
6505/*
6506** CAPI3REF: Copy And Free SQL Values
6507** METHOD: sqlite3_value
6508**
6509** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6510** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6511** is a [protected sqlite3_value] object even if the input is not.
6512** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6513** memory allocation fails.
6514**
6515** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6516** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6517** then sqlite3_value_free(V) is a harmless no-op.
6518*/
6519SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6520SQLITE_API void sqlite3_value_free(sqlite3_value*);
6521
6522/*
6523** CAPI3REF: Obtain Aggregate Function Context
6524** METHOD: sqlite3_context
6525**
6526** Implementations of aggregate SQL functions use this
6527** routine to allocate memory for storing their state.
6528**
6529** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6530** for a particular aggregate function, SQLite allocates
6531** N bytes of memory, zeroes out that memory, and returns a pointer
6532** to the new memory. ^On second and subsequent calls to
6533** sqlite3_aggregate_context() for the same aggregate function instance,
6534** the same buffer is returned. Sqlite3_aggregate_context() is normally
6535** called once for each invocation of the xStep callback and then one
6536** last time when the xFinal callback is invoked. ^(When no rows match
6537** an aggregate query, the xStep() callback of the aggregate function
6538** implementation is never called and xFinal() is called exactly once.
6539** In those cases, sqlite3_aggregate_context() might be called for the
6540** first time from within xFinal().)^
6541**
6542** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6543** when first called if N is less than or equal to zero or if a memory
6544** allocate error occurs.
6545**
6546** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6547** determined by the N parameter on first successful call. Changing the
6548** value of N in any subsequent call to sqlite3_aggregate_context() within
6549** the same aggregate function instance will not resize the memory
6550** allocation.)^ Within the xFinal callback, it is customary to set
6551** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6552** pointless memory allocations occur.
6553**
6554** ^SQLite automatically frees the memory allocated by
6555** sqlite3_aggregate_context() when the aggregate query concludes.
6556**
6557** The first parameter must be a copy of the
6558** [sqlite3_context | SQL function context] that is the first parameter
6559** to the xStep or xFinal callback routine that implements the aggregate
6560** function.
6561**
6562** This routine must be called from the same thread in which
6563** the aggregate SQL function is running.
6564*/
6565SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6566
6567/*
6568** CAPI3REF: User Data For Functions
6569** METHOD: sqlite3_context
6570**
6571** ^The sqlite3_user_data() interface returns a copy of
6572** the pointer that was the pUserData parameter (the 5th parameter)
6573** of the [sqlite3_create_function()]
6574** and [sqlite3_create_function16()] routines that originally
6575** registered the application defined function.
6576**
6577** This routine must be called from the same thread in which
6578** the application-defined function is running.
6579*/
6580SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6581
6582/*
6583** CAPI3REF: Database Connection For Functions
6584** METHOD: sqlite3_context
6585**
6586** ^The sqlite3_context_db_handle() interface returns a copy of
6587** the pointer to the [database connection] (the 1st parameter)
6588** of the [sqlite3_create_function()]
6589** and [sqlite3_create_function16()] routines that originally
6590** registered the application defined function.
6591*/
6592SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6593
6594/*
6595** CAPI3REF: Function Auxiliary Data
6596** METHOD: sqlite3_context
6597**
6598** These functions may be used by (non-aggregate) SQL functions to
6599** associate metadata with argument values. If the same value is passed to
6600** multiple invocations of the same SQL function during query execution, under
6601** some circumstances the associated metadata may be preserved. An example
6602** of where this might be useful is in a regular-expression matching
6603** function. The compiled version of the regular expression can be stored as
6604** metadata associated with the pattern string.
6605** Then as long as the pattern string remains the same,
6606** the compiled regular expression can be reused on multiple
6607** invocations of the same function.
6608**
6609** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6610** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6611** value to the application-defined function. ^N is zero for the left-most
6612** function argument. ^If there is no metadata
6613** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6614** returns a NULL pointer.
6615**
6616** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6617** argument of the application-defined function. ^Subsequent
6618** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6619** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6620** NULL if the metadata has been discarded.
6621** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6622** SQLite will invoke the destructor function X with parameter P exactly
6623** once, when the metadata is discarded.
6624** SQLite is free to discard the metadata at any time, including: <ul>
6625** <li> ^(when the corresponding function parameter changes)^, or
6626** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6627** SQL statement)^, or
6628** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6629** parameter)^, or
6630** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6631** allocation error occurs.)^ </ul>
6632**
6633** Note the last bullet in particular. The destructor X in
6634** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6635** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6636** should be called near the end of the function implementation and the
6637** function implementation should not make any use of P after
6638** sqlite3_set_auxdata() has been called.
6639**
6640** ^(In practice, metadata is preserved between function calls for
6641** function parameters that are compile-time constants, including literal
6642** values and [parameters] and expressions composed from the same.)^
6643**
6644** The value of the N parameter to these interfaces should be non-negative.
6645** Future enhancements may make use of negative N values to define new
6646** kinds of function caching behavior.
6647**
6648** These routines must be called from the same thread in which
6649** the SQL function is running.
6650*/
6651SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6652SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6653
6654
6655/*
6656** CAPI3REF: Constants Defining Special Destructor Behavior
6657**
6658** These are special values for the destructor that is passed in as the
6659** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6660** argument is SQLITE_STATIC, it means that the content pointer is constant
6661** and will never change. It does not need to be destroyed. ^The
6662** SQLITE_TRANSIENT value means that the content will likely change in
6663** the near future and that SQLite should make its own private copy of
6664** the content before returning.
6665**
6666** The typedef is necessary to work around problems in certain
6667** C++ compilers.
6668*/
6669typedef void (*sqlite3_destructor_type)(void*);
6670#define SQLITE_STATIC ((sqlite3_destructor_type)0)
6671#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6672
6673/*
6674** CAPI3REF: Setting The Result Of An SQL Function
6675** METHOD: sqlite3_context
6676**
6677** These routines are used by the xFunc or xFinal callbacks that
6678** implement SQL functions and aggregates. See
6679** [sqlite3_create_function()] and [sqlite3_create_function16()]
6680** for additional information.
6681**
6682** These functions work very much like the [parameter binding] family of
6683** functions used to bind values to host parameters in prepared statements.
6684** Refer to the [SQL parameter] documentation for additional information.
6685**
6686** ^The sqlite3_result_blob() interface sets the result from
6687** an application-defined function to be the BLOB whose content is pointed
6688** to by the second parameter and which is N bytes long where N is the
6689** third parameter.
6690**
6691** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6692** interfaces set the result of the application-defined function to be
6693** a BLOB containing all zero bytes and N bytes in size.
6694**
6695** ^The sqlite3_result_double() interface sets the result from
6696** an application-defined function to be a floating point value specified
6697** by its 2nd argument.
6698**
6699** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6700** cause the implemented SQL function to throw an exception.
6701** ^SQLite uses the string pointed to by the
6702** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6703** as the text of an error message. ^SQLite interprets the error
6704** message string from sqlite3_result_error() as UTF-8. ^SQLite
6705** interprets the string from sqlite3_result_error16() as UTF-16 using
6706** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6707** ^If the third parameter to sqlite3_result_error()
6708** or sqlite3_result_error16() is negative then SQLite takes as the error
6709** message all text up through the first zero character.
6710** ^If the third parameter to sqlite3_result_error() or
6711** sqlite3_result_error16() is non-negative then SQLite takes that many
6712** bytes (not characters) from the 2nd parameter as the error message.
6713** ^The sqlite3_result_error() and sqlite3_result_error16()
6714** routines make a private copy of the error message text before
6715** they return. Hence, the calling function can deallocate or
6716** modify the text after they return without harm.
6717** ^The sqlite3_result_error_code() function changes the error code
6718** returned by SQLite as a result of an error in a function. ^By default,
6719** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6720** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6721**
6722** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6723** error indicating that a string or BLOB is too long to represent.
6724**
6725** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6726** error indicating that a memory allocation failed.
6727**
6728** ^The sqlite3_result_int() interface sets the return value
6729** of the application-defined function to be the 32-bit signed integer
6730** value given in the 2nd argument.
6731** ^The sqlite3_result_int64() interface sets the return value
6732** of the application-defined function to be the 64-bit signed integer
6733** value given in the 2nd argument.
6734**
6735** ^The sqlite3_result_null() interface sets the return value
6736** of the application-defined function to be NULL.
6737**
6738** ^The sqlite3_result_text(), sqlite3_result_text16(),
6739** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6740** set the return value of the application-defined function to be
6741** a text string which is represented as UTF-8, UTF-16 native byte order,
6742** UTF-16 little endian, or UTF-16 big endian, respectively.
6743** ^The sqlite3_result_text64() interface sets the return value of an
6744** application-defined function to be a text string in an encoding
6745** specified by the fifth (and last) parameter, which must be one
6746** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6747** ^SQLite takes the text result from the application from
6748** the 2nd parameter of the sqlite3_result_text* interfaces.
6749** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6750** is negative, then SQLite takes result text from the 2nd parameter
6751** through the first zero character.
6752** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6753** is non-negative, then as many bytes (not characters) of the text
6754** pointed to by the 2nd parameter are taken as the application-defined
6755** function result. If the 3rd parameter is non-negative, then it
6756** must be the byte offset into the string where the NUL terminator would
6757** appear if the string where NUL terminated. If any NUL characters occur
6758** in the string at a byte offset that is less than the value of the 3rd
6759** parameter, then the resulting string will contain embedded NULs and the
6760** result of expressions operating on strings with embedded NULs is undefined.
6761** ^If the 4th parameter to the sqlite3_result_text* interfaces
6762** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6763** function as the destructor on the text or BLOB result when it has
6764** finished using that result.
6765** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6766** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6767** assumes that the text or BLOB result is in constant space and does not
6768** copy the content of the parameter nor call a destructor on the content
6769** when it has finished using that result.
6770** ^If the 4th parameter to the sqlite3_result_text* interfaces
6771** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6772** then SQLite makes a copy of the result into space obtained
6773** from [sqlite3_malloc()] before it returns.
6774**
6775** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6776** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6777** when the encoding is not UTF8, if the input UTF16 begins with a
6778** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6779** string and the rest of the string is interpreted according to the
6780** byte-order specified by the BOM. ^The byte-order specified by
6781** the BOM at the beginning of the text overrides the byte-order
6782** specified by the interface procedure. ^So, for example, if
6783** sqlite3_result_text16le() is invoked with text that begins
6784** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6785** first two bytes of input are skipped and the remaining input
6786** is interpreted as UTF16BE text.
6787**
6788** ^For UTF16 input text to the sqlite3_result_text16(),
6789** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6790** sqlite3_result_text64() routines, if the text contains invalid
6791** UTF16 characters, the invalid characters might be converted
6792** into the unicode replacement character, U+FFFD.
6793**
6794** ^The sqlite3_result_value() interface sets the result of
6795** the application-defined function to be a copy of the
6796** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6797** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6798** so that the [sqlite3_value] specified in the parameter may change or
6799** be deallocated after sqlite3_result_value() returns without harm.
6800** ^A [protected sqlite3_value] object may always be used where an
6801** [unprotected sqlite3_value] object is required, so either
6802** kind of [sqlite3_value] object can be used with this interface.
6803**
6804** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6805** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6806** also associates the host-language pointer P or type T with that
6807** NULL value such that the pointer can be retrieved within an
6808** [application-defined SQL function] using [sqlite3_value_pointer()].
6809** ^If the D parameter is not NULL, then it is a pointer to a destructor
6810** for the P parameter. ^SQLite invokes D with P as its only argument
6811** when SQLite is finished with P. The T parameter should be a static
6812** string and preferably a string literal. The sqlite3_result_pointer()
6813** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6814**
6815** If these routines are called from within the different thread
6816** than the one containing the application-defined function that received
6817** the [sqlite3_context] pointer, the results are undefined.
6818*/
6819SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6820SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6821 sqlite3_uint64,void(*)(void*));
6822SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6823SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6824SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6825SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6826SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6827SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6828SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6829SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6830SQLITE_API void sqlite3_result_null(sqlite3_context*);
6831SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6832SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6833 void(*)(void*), unsigned char encoding);
6834SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6835SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6836SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6837SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6838SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6839SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6840SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6841
6842
6843/*
6844** CAPI3REF: Setting The Subtype Of An SQL Function
6845** METHOD: sqlite3_context
6846**
6847** The sqlite3_result_subtype(C,T) function causes the subtype of
6848** the result from the [application-defined SQL function] with
6849** [sqlite3_context] C to be the value T. Only the lower 8 bits
6850** of the subtype T are preserved in current versions of SQLite;
6851** higher order bits are discarded.
6852** The number of subtype bytes preserved by SQLite might increase
6853** in future releases of SQLite.
6854*/
6855SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6856
6857/*
6858** CAPI3REF: Define New Collating Sequences
6859** METHOD: sqlite3
6860**
6861** ^These functions add, remove, or modify a [collation] associated
6862** with the [database connection] specified as the first argument.
6863**
6864** ^The name of the collation is a UTF-8 string
6865** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6866** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6867** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6868** considered to be the same name.
6869**
6870** ^(The third argument (eTextRep) must be one of the constants:
6871** <ul>
6872** <li> [SQLITE_UTF8],
6873** <li> [SQLITE_UTF16LE],
6874** <li> [SQLITE_UTF16BE],
6875** <li> [SQLITE_UTF16], or
6876** <li> [SQLITE_UTF16_ALIGNED].
6877** </ul>)^
6878** ^The eTextRep argument determines the encoding of strings passed
6879** to the collating function callback, xCompare.
6880** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6881** force strings to be UTF16 with native byte order.
6882** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6883** on an even byte address.
6884**
6885** ^The fourth argument, pArg, is an application data pointer that is passed
6886** through as the first argument to the collating function callback.
6887**
6888** ^The fifth argument, xCompare, is a pointer to the collating function.
6889** ^Multiple collating functions can be registered using the same name but
6890** with different eTextRep parameters and SQLite will use whichever
6891** function requires the least amount of data transformation.
6892** ^If the xCompare argument is NULL then the collating function is
6893** deleted. ^When all collating functions having the same name are deleted,
6894** that collation is no longer usable.
6895**
6896** ^The collating function callback is invoked with a copy of the pArg
6897** application data pointer and with two strings in the encoding specified
6898** by the eTextRep argument. The two integer parameters to the collating
6899** function callback are the length of the two strings, in bytes. The collating
6900** function must return an integer that is negative, zero, or positive
6901** if the first string is less than, equal to, or greater than the second,
6902** respectively. A collating function must always return the same answer
6903** given the same inputs. If two or more collating functions are registered
6904** to the same collation name (using different eTextRep values) then all
6905** must give an equivalent answer when invoked with equivalent strings.
6906** The collating function must obey the following properties for all
6907** strings A, B, and C:
6908**
6909** <ol>
6910** <li> If A==B then B==A.
6911** <li> If A==B and B==C then A==C.
6912** <li> If A&lt;B THEN B&gt;A.
6913** <li> If A&lt;B and B&lt;C then A&lt;C.
6914** </ol>
6915**
6916** If a collating function fails any of the above constraints and that
6917** collating function is registered and used, then the behavior of SQLite
6918** is undefined.
6919**
6920** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6921** with the addition that the xDestroy callback is invoked on pArg when
6922** the collating function is deleted.
6923** ^Collating functions are deleted when they are overridden by later
6924** calls to the collation creation functions or when the
6925** [database connection] is closed using [sqlite3_close()].
6926**
6927** ^The xDestroy callback is <u>not</u> called if the
6928** sqlite3_create_collation_v2() function fails. Applications that invoke
6929** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6930** check the return code and dispose of the application data pointer
6931** themselves rather than expecting SQLite to deal with it for them.
6932** This is different from every other SQLite interface. The inconsistency
6933** is unfortunate but cannot be changed without breaking backwards
6934** compatibility.
6935**
6936** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6937*/
6938SQLITE_API int sqlite3_create_collation(
6939 sqlite3*,
6940 const char *zName,
6941 int eTextRep,
6942 void *pArg,
6943 int(*xCompare)(void*,int,const void*,int,const void*)
6944);
6945SQLITE_API int sqlite3_create_collation_v2(
6946 sqlite3*,
6947 const char *zName,
6948 int eTextRep,
6949 void *pArg,
6950 int(*xCompare)(void*,int,const void*,int,const void*),
6951 void(*xDestroy)(void*)
6952);
6953SQLITE_API int sqlite3_create_collation16(
6954 sqlite3*,
6955 const void *zName,
6956 int eTextRep,
6957 void *pArg,
6958 int(*xCompare)(void*,int,const void*,int,const void*)
6959);
6960
6961/*
6962** CAPI3REF: Collation Needed Callbacks
6963** METHOD: sqlite3
6964**
6965** ^To avoid having to register all collation sequences before a database
6966** can be used, a single callback function may be registered with the
6967** [database connection] to be invoked whenever an undefined collation
6968** sequence is required.
6969**
6970** ^If the function is registered using the sqlite3_collation_needed() API,
6971** then it is passed the names of undefined collation sequences as strings
6972** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6973** the names are passed as UTF-16 in machine native byte order.
6974** ^A call to either function replaces the existing collation-needed callback.
6975**
6976** ^(When the callback is invoked, the first argument passed is a copy
6977** of the second argument to sqlite3_collation_needed() or
6978** sqlite3_collation_needed16(). The second argument is the database
6979** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6980** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6981** sequence function required. The fourth parameter is the name of the
6982** required collation sequence.)^
6983**
6984** The callback function should register the desired collation using
6985** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6986** [sqlite3_create_collation_v2()].
6987*/
6988SQLITE_API int sqlite3_collation_needed(
6989 sqlite3*,
6990 void*,
6991 void(*)(void*,sqlite3*,int eTextRep,const char*)
6992);
6993SQLITE_API int sqlite3_collation_needed16(
6994 sqlite3*,
6995 void*,
6996 void(*)(void*,sqlite3*,int eTextRep,const void*)
6997);
6998
6999#ifdef SQLITE_ENABLE_CEROD
7000/*
7001** Specify the activation key for a CEROD database. Unless
7002** activated, none of the CEROD routines will work.
7003*/
7004SQLITE_API void sqlite3_activate_cerod(
7005 const char *zPassPhrase /* Activation phrase */
7006);
7007#endif
7008
7009/*
7010** CAPI3REF: Suspend Execution For A Short Time
7011**
7012** The sqlite3_sleep() function causes the current thread to suspend execution
7013** for at least a number of milliseconds specified in its parameter.
7014**
7015** If the operating system does not support sleep requests with
7016** millisecond time resolution, then the time will be rounded up to
7017** the nearest second. The number of milliseconds of sleep actually
7018** requested from the operating system is returned.
7019**
7020** ^SQLite implements this interface by calling the xSleep()
7021** method of the default [sqlite3_vfs] object. If the xSleep() method
7022** of the default VFS is not implemented correctly, or not implemented at
7023** all, then the behavior of sqlite3_sleep() may deviate from the description
7024** in the previous paragraphs.
7025*/
7026SQLITE_API int sqlite3_sleep(int);
7027
7028/*
7029** CAPI3REF: Name Of The Folder Holding Temporary Files
7030**
7031** ^(If this global variable is made to point to a string which is
7032** the name of a folder (a.k.a. directory), then all temporary files
7033** created by SQLite when using a built-in [sqlite3_vfs | VFS]
7034** will be placed in that directory.)^ ^If this variable
7035** is a NULL pointer, then SQLite performs a search for an appropriate
7036** temporary file directory.
7037**
7038** Applications are strongly discouraged from using this global variable.
7039** It is required to set a temporary folder on Windows Runtime (WinRT).
7040** But for all other platforms, it is highly recommended that applications
7041** neither read nor write this variable. This global variable is a relic
7042** that exists for backwards compatibility of legacy applications and should
7043** be avoided in new projects.
7044**
7045** It is not safe to read or modify this variable in more than one
7046** thread at a time. It is not safe to read or modify this variable
7047** if a [database connection] is being used at the same time in a separate
7048** thread.
7049** It is intended that this variable be set once
7050** as part of process initialization and before any SQLite interface
7051** routines have been called and that this variable remain unchanged
7052** thereafter.
7053**
7054** ^The [temp_store_directory pragma] may modify this variable and cause
7055** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
7056** the [temp_store_directory pragma] always assumes that any string
7057** that this variable points to is held in memory obtained from
7058** [sqlite3_malloc] and the pragma may attempt to free that memory
7059** using [sqlite3_free].
7060** Hence, if this variable is modified directly, either it should be
7061** made NULL or made to point to memory obtained from [sqlite3_malloc]
7062** or else the use of the [temp_store_directory pragma] should be avoided.
7063** Except when requested by the [temp_store_directory pragma], SQLite
7064** does not free the memory that sqlite3_temp_directory points to. If
7065** the application wants that memory to be freed, it must do
7066** so itself, taking care to only do so after all [database connection]
7067** objects have been destroyed.
7068**
7069** <b>Note to Windows Runtime users:</b> The temporary directory must be set
7070** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
7071** features that require the use of temporary files may fail. Here is an
7072** example of how to do this using C++ with the Windows Runtime:
7073**
7074** <blockquote><pre>
7075** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
7076** &nbsp; TemporaryFolder->Path->Data();
7077** char zPathBuf&#91;MAX_PATH + 1&#93;;
7078** memset(zPathBuf, 0, sizeof(zPathBuf));
7079** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
7080** &nbsp; NULL, NULL);
7081** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
7082** </pre></blockquote>
7083*/
7084SQLITE_API char *sqlite3_temp_directory;
7085
7086/*
7087** CAPI3REF: Name Of The Folder Holding Database Files
7088**
7089** ^(If this global variable is made to point to a string which is
7090** the name of a folder (a.k.a. directory), then all database files
7091** specified with a relative pathname and created or accessed by
7092** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
7093** to be relative to that directory.)^ ^If this variable is a NULL
7094** pointer, then SQLite assumes that all database files specified
7095** with a relative pathname are relative to the current directory
7096** for the process. Only the windows VFS makes use of this global
7097** variable; it is ignored by the unix VFS.
7098**
7099** Changing the value of this variable while a database connection is
7100** open can result in a corrupt database.
7101**
7102** It is not safe to read or modify this variable in more than one
7103** thread at a time. It is not safe to read or modify this variable
7104** if a [database connection] is being used at the same time in a separate
7105** thread.
7106** It is intended that this variable be set once
7107** as part of process initialization and before any SQLite interface
7108** routines have been called and that this variable remain unchanged
7109** thereafter.
7110**
7111** ^The [data_store_directory pragma] may modify this variable and cause
7112** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
7113** the [data_store_directory pragma] always assumes that any string
7114** that this variable points to is held in memory obtained from
7115** [sqlite3_malloc] and the pragma may attempt to free that memory
7116** using [sqlite3_free].
7117** Hence, if this variable is modified directly, either it should be
7118** made NULL or made to point to memory obtained from [sqlite3_malloc]
7119** or else the use of the [data_store_directory pragma] should be avoided.
7120*/
7121SQLITE_API char *sqlite3_data_directory;
7122
7123/*
7124** CAPI3REF: Win32 Specific Interface
7125**
7126** These interfaces are available only on Windows. The
7127** [sqlite3_win32_set_directory] interface is used to set the value associated
7128** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
7129** zValue, depending on the value of the type parameter. The zValue parameter
7130** should be NULL to cause the previous value to be freed via [sqlite3_free];
7131** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
7132** prior to being used. The [sqlite3_win32_set_directory] interface returns
7133** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
7134** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
7135** [sqlite3_data_directory] variable is intended to act as a replacement for
7136** the current directory on the sub-platforms of Win32 where that concept is
7137** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
7138** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
7139** sqlite3_win32_set_directory interface except the string parameter must be
7140** UTF-8 or UTF-16, respectively.
7141*/
7142SQLITE_API int sqlite3_win32_set_directory(
7143 unsigned long type, /* Identifier for directory being set or reset */
7144 void *zValue /* New value for directory being set or reset */
7145);
7146SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
7147SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
7148
7149/*
7150** CAPI3REF: Win32 Directory Types
7151**
7152** These macros are only available on Windows. They define the allowed values
7153** for the type argument to the [sqlite3_win32_set_directory] interface.
7154*/
7155#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
7156#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
7157
7158/*
7159** CAPI3REF: Test For Auto-Commit Mode
7160** KEYWORDS: {autocommit mode}
7161** METHOD: sqlite3
7162**
7163** ^The sqlite3_get_autocommit() interface returns non-zero or
7164** zero if the given database connection is or is not in autocommit mode,
7165** respectively. ^Autocommit mode is on by default.
7166** ^Autocommit mode is disabled by a [BEGIN] statement.
7167** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
7168**
7169** If certain kinds of errors occur on a statement within a multi-statement
7170** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
7171** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
7172** transaction might be rolled back automatically. The only way to
7173** find out whether SQLite automatically rolled back the transaction after
7174** an error is to use this function.
7175**
7176** If another thread changes the autocommit status of the database
7177** connection while this routine is running, then the return value
7178** is undefined.
7179*/
7180SQLITE_API int sqlite3_get_autocommit(sqlite3*);
7181
7182/*
7183** CAPI3REF: Find The Database Handle Of A Prepared Statement
7184** METHOD: sqlite3_stmt
7185**
7186** ^The sqlite3_db_handle interface returns the [database connection] handle
7187** to which a [prepared statement] belongs. ^The [database connection]
7188** returned by sqlite3_db_handle is the same [database connection]
7189** that was the first argument
7190** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
7191** create the statement in the first place.
7192*/
7193SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
7194
7195/*
7196** CAPI3REF: Return The Filename For A Database Connection
7197** METHOD: sqlite3
7198**
7199** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
7200** associated with database N of connection D.
7201** ^If there is no attached database N on the database
7202** connection D, or if database N is a temporary or in-memory database, then
7203** this function will return either a NULL pointer or an empty string.
7204**
7205** ^The string value returned by this routine is owned and managed by
7206** the database connection. ^The value will be valid until the database N
7207** is [DETACH]-ed or until the database connection closes.
7208**
7209** ^The filename returned by this function is the output of the
7210** xFullPathname method of the [VFS]. ^In other words, the filename
7211** will be an absolute pathname, even if the filename used
7212** to open the database originally was a URI or relative pathname.
7213**
7214** If the filename pointer returned by this routine is not NULL, then it
7215** can be used as the filename input parameter to these routines:
7216** <ul>
7217** <li> [sqlite3_uri_parameter()]
7218** <li> [sqlite3_uri_boolean()]
7219** <li> [sqlite3_uri_int64()]
7220** <li> [sqlite3_filename_database()]
7221** <li> [sqlite3_filename_journal()]
7222** <li> [sqlite3_filename_wal()]
7223** </ul>
7224*/
7225SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
7226
7227/*
7228** CAPI3REF: Determine if a database is read-only
7229** METHOD: sqlite3
7230**
7231** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
7232** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7233** the name of a database on connection D.
7234*/
7235SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7236
7237/*
7238** CAPI3REF: Find the next prepared statement
7239** METHOD: sqlite3
7240**
7241** ^This interface returns a pointer to the next [prepared statement] after
7242** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
7243** then this interface returns a pointer to the first prepared statement
7244** associated with the database connection pDb. ^If no prepared statement
7245** satisfies the conditions of this routine, it returns NULL.
7246**
7247** The [database connection] pointer D in a call to
7248** [sqlite3_next_stmt(D,S)] must refer to an open database
7249** connection and in particular must not be a NULL pointer.
7250*/
7251SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
7252
7253/*
7254** CAPI3REF: Commit And Rollback Notification Callbacks
7255** METHOD: sqlite3
7256**
7257** ^The sqlite3_commit_hook() interface registers a callback
7258** function to be invoked whenever a transaction is [COMMIT | committed].
7259** ^Any callback set by a previous call to sqlite3_commit_hook()
7260** for the same database connection is overridden.
7261** ^The sqlite3_rollback_hook() interface registers a callback
7262** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
7263** ^Any callback set by a previous call to sqlite3_rollback_hook()
7264** for the same database connection is overridden.
7265** ^The pArg argument is passed through to the callback.
7266** ^If the callback on a commit hook function returns non-zero,
7267** then the commit is converted into a rollback.
7268**
7269** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
7270** return the P argument from the previous call of the same function
7271** on the same [database connection] D, or NULL for
7272** the first call for each function on D.
7273**
7274** The commit and rollback hook callbacks are not reentrant.
7275** The callback implementation must not do anything that will modify
7276** the database connection that invoked the callback. Any actions
7277** to modify the database connection must be deferred until after the
7278** completion of the [sqlite3_step()] call that triggered the commit
7279** or rollback hook in the first place.
7280** Note that running any other SQL statements, including SELECT statements,
7281** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
7282** the database connections for the meaning of "modify" in this paragraph.
7283**
7284** ^Registering a NULL function disables the callback.
7285**
7286** ^When the commit hook callback routine returns zero, the [COMMIT]
7287** operation is allowed to continue normally. ^If the commit hook
7288** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7289** ^The rollback hook is invoked on a rollback that results from a commit
7290** hook returning non-zero, just as it would be with any other rollback.
7291**
7292** ^For the purposes of this API, a transaction is said to have been
7293** rolled back if an explicit "ROLLBACK" statement is executed, or
7294** an error or constraint causes an implicit rollback to occur.
7295** ^The rollback callback is not invoked if a transaction is
7296** automatically rolled back because the database connection is closed.
7297**
7298** See also the [sqlite3_update_hook()] interface.
7299*/
7300SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
7301SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
7302
7303/*
7304** CAPI3REF: Data Change Notification Callbacks
7305** METHOD: sqlite3
7306**
7307** ^The sqlite3_update_hook() interface registers a callback function
7308** with the [database connection] identified by the first argument
7309** to be invoked whenever a row is updated, inserted or deleted in
7310** a [rowid table].
7311** ^Any callback set by a previous call to this function
7312** for the same database connection is overridden.
7313**
7314** ^The second argument is a pointer to the function to invoke when a
7315** row is updated, inserted or deleted in a rowid table.
7316** ^The first argument to the callback is a copy of the third argument
7317** to sqlite3_update_hook().
7318** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7319** or [SQLITE_UPDATE], depending on the operation that caused the callback
7320** to be invoked.
7321** ^The third and fourth arguments to the callback contain pointers to the
7322** database and table name containing the affected row.
7323** ^The final callback parameter is the [rowid] of the row.
7324** ^In the case of an update, this is the [rowid] after the update takes place.
7325**
7326** ^(The update hook is not invoked when internal system tables are
7327** modified (i.e. sqlite_sequence).)^
7328** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7329**
7330** ^In the current implementation, the update hook
7331** is not invoked when conflicting rows are deleted because of an
7332** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
7333** invoked when rows are deleted using the [truncate optimization].
7334** The exceptions defined in this paragraph might change in a future
7335** release of SQLite.
7336**
7337** The update hook implementation must not do anything that will modify
7338** the database connection that invoked the update hook. Any actions
7339** to modify the database connection must be deferred until after the
7340** completion of the [sqlite3_step()] call that triggered the update hook.
7341** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7342** database connections for the meaning of "modify" in this paragraph.
7343**
7344** ^The sqlite3_update_hook(D,C,P) function
7345** returns the P argument from the previous call
7346** on the same [database connection] D, or NULL for
7347** the first call on D.
7348**
7349** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7350** and [sqlite3_preupdate_hook()] interfaces.
7351*/
7352SQLITE_API void *sqlite3_update_hook(
7353 sqlite3*,
7354 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7355 void*
7356);
7357
7358/*
7359** CAPI3REF: Enable Or Disable Shared Pager Cache
7360**
7361** ^(This routine enables or disables the sharing of the database cache
7362** and schema data structures between [database connection | connections]
7363** to the same database. Sharing is enabled if the argument is true
7364** and disabled if the argument is false.)^
7365**
7366** ^Cache sharing is enabled and disabled for an entire process.
7367** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7368** In prior versions of SQLite,
7369** sharing was enabled or disabled for each thread separately.
7370**
7371** ^(The cache sharing mode set by this interface effects all subsequent
7372** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7373** Existing database connections continue to use the sharing mode
7374** that was in effect at the time they were opened.)^
7375**
7376** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7377** successfully. An [error code] is returned otherwise.)^
7378**
7379** ^Shared cache is disabled by default. It is recommended that it stay
7380** that way. In other words, do not use this routine. This interface
7381** continues to be provided for historical compatibility, but its use is
7382** discouraged. Any use of shared cache is discouraged. If shared cache
7383** must be used, it is recommended that shared cache only be enabled for
7384** individual database connections using the [sqlite3_open_v2()] interface
7385** with the [SQLITE_OPEN_SHAREDCACHE] flag.
7386**
7387** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7388** and will always return SQLITE_MISUSE. On those systems,
7389** shared cache mode should be enabled per-database connection via
7390** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7391**
7392** This interface is threadsafe on processors where writing a
7393** 32-bit integer is atomic.
7394**
7395** See Also: [SQLite Shared-Cache Mode]
7396*/
7397SQLITE_API int sqlite3_enable_shared_cache(int);
7398
7399/*
7400** CAPI3REF: Attempt To Free Heap Memory
7401**
7402** ^The sqlite3_release_memory() interface attempts to free N bytes
7403** of heap memory by deallocating non-essential memory allocations
7404** held by the database library. Memory used to cache database
7405** pages to improve performance is an example of non-essential memory.
7406** ^sqlite3_release_memory() returns the number of bytes actually freed,
7407** which might be more or less than the amount requested.
7408** ^The sqlite3_release_memory() routine is a no-op returning zero
7409** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7410**
7411** See also: [sqlite3_db_release_memory()]
7412*/
7413SQLITE_API int sqlite3_release_memory(int);
7414
7415/*
7416** CAPI3REF: Free Memory Used By A Database Connection
7417** METHOD: sqlite3
7418**
7419** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7420** memory as possible from database connection D. Unlike the
7421** [sqlite3_release_memory()] interface, this interface is in effect even
7422** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7423** omitted.
7424**
7425** See also: [sqlite3_release_memory()]
7426*/
7427SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7428
7429/*
7430** CAPI3REF: Impose A Limit On Heap Size
7431**
7432** These interfaces impose limits on the amount of heap memory that will be
7433** by all database connections within a single process.
7434**
7435** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7436** soft limit on the amount of heap memory that may be allocated by SQLite.
7437** ^SQLite strives to keep heap memory utilization below the soft heap
7438** limit by reducing the number of pages held in the page cache
7439** as heap memory usages approaches the limit.
7440** ^The soft heap limit is "soft" because even though SQLite strives to stay
7441** below the limit, it will exceed the limit rather than generate
7442** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7443** is advisory only.
7444**
7445** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
7446** N bytes on the amount of memory that will be allocated. ^The
7447** sqlite3_hard_heap_limit64(N) interface is similar to
7448** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
7449** when the hard heap limit is reached.
7450**
7451** ^The return value from both sqlite3_soft_heap_limit64() and
7452** sqlite3_hard_heap_limit64() is the size of
7453** the heap limit prior to the call, or negative in the case of an
7454** error. ^If the argument N is negative
7455** then no change is made to the heap limit. Hence, the current
7456** size of heap limits can be determined by invoking
7457** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7458**
7459** ^Setting the heap limits to zero disables the heap limiter mechanism.
7460**
7461** ^The soft heap limit may not be greater than the hard heap limit.
7462** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7463** is invoked with a value of N that is greater than the hard heap limit,
7464** the the soft heap limit is set to the value of the hard heap limit.
7465** ^The soft heap limit is automatically enabled whenever the hard heap
7466** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7467** the soft heap limit is outside the range of 1..N, then the soft heap
7468** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
7469** hard heap limit is enabled makes the soft heap limit equal to the
7470** hard heap limit.
7471**
7472** The memory allocation limits can also be adjusted using
7473** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7474**
7475** ^(The heap limits are not enforced in the current implementation
7476** if one or more of following conditions are true:
7477**
7478** <ul>
7479** <li> The limit value is set to zero.
7480** <li> Memory accounting is disabled using a combination of the
7481** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7482** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7483** <li> An alternative page cache implementation is specified using
7484** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7485** <li> The page cache allocates from its own memory pool supplied
7486** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7487** from the heap.
7488** </ul>)^
7489**
7490** The circumstances under which SQLite will enforce the heap limits may
7491** changes in future releases of SQLite.
7492*/
7493SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7494SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7495
7496/*
7497** CAPI3REF: Deprecated Soft Heap Limit Interface
7498** DEPRECATED
7499**
7500** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7501** interface. This routine is provided for historical compatibility
7502** only. All new applications should use the
7503** [sqlite3_soft_heap_limit64()] interface rather than this one.
7504*/
7505SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7506
7507
7508/*
7509** CAPI3REF: Extract Metadata About A Column Of A Table
7510** METHOD: sqlite3
7511**
7512** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7513** information about column C of table T in database D
7514** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7515** interface returns SQLITE_OK and fills in the non-NULL pointers in
7516** the final five arguments with appropriate values if the specified
7517** column exists. ^The sqlite3_table_column_metadata() interface returns
7518** SQLITE_ERROR if the specified column does not exist.
7519** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7520** NULL pointer, then this routine simply checks for the existence of the
7521** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7522** does not. If the table name parameter T in a call to
7523** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7524** undefined behavior.
7525**
7526** ^The column is identified by the second, third and fourth parameters to
7527** this function. ^(The second parameter is either the name of the database
7528** (i.e. "main", "temp", or an attached database) containing the specified
7529** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7530** for the table using the same algorithm used by the database engine to
7531** resolve unqualified table references.
7532**
7533** ^The third and fourth parameters to this function are the table and column
7534** name of the desired column, respectively.
7535**
7536** ^Metadata is returned by writing to the memory locations passed as the 5th
7537** and subsequent parameters to this function. ^Any of these arguments may be
7538** NULL, in which case the corresponding element of metadata is omitted.
7539**
7540** ^(<blockquote>
7541** <table border="1">
7542** <tr><th> Parameter <th> Output<br>Type <th> Description
7543**
7544** <tr><td> 5th <td> const char* <td> Data type
7545** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7546** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7547** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7548** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7549** </table>
7550** </blockquote>)^
7551**
7552** ^The memory pointed to by the character pointers returned for the
7553** declaration type and collation sequence is valid until the next
7554** call to any SQLite API function.
7555**
7556** ^If the specified table is actually a view, an [error code] is returned.
7557**
7558** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7559** is not a [WITHOUT ROWID] table and an
7560** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7561** parameters are set for the explicitly declared column. ^(If there is no
7562** [INTEGER PRIMARY KEY] column, then the outputs
7563** for the [rowid] are set as follows:
7564**
7565** <pre>
7566** data type: "INTEGER"
7567** collation sequence: "BINARY"
7568** not null: 0
7569** primary key: 1
7570** auto increment: 0
7571** </pre>)^
7572**
7573** ^This function causes all database schemas to be read from disk and
7574** parsed, if that has not already been done, and returns an error if
7575** any errors are encountered while loading the schema.
7576*/
7577SQLITE_API int sqlite3_table_column_metadata(
7578 sqlite3 *db, /* Connection handle */
7579 const char *zDbName, /* Database name or NULL */
7580 const char *zTableName, /* Table name */
7581 const char *zColumnName, /* Column name */
7582 char const **pzDataType, /* OUTPUT: Declared data type */
7583 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7584 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7585 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7586 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7587);
7588
7589/*
7590** CAPI3REF: Load An Extension
7591** METHOD: sqlite3
7592**
7593** ^This interface loads an SQLite extension library from the named file.
7594**
7595** ^The sqlite3_load_extension() interface attempts to load an
7596** [SQLite extension] library contained in the file zFile. If
7597** the file cannot be loaded directly, attempts are made to load
7598** with various operating-system specific extensions added.
7599** So for example, if "samplelib" cannot be loaded, then names like
7600** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7601** be tried also.
7602**
7603** ^The entry point is zProc.
7604** ^(zProc may be 0, in which case SQLite will try to come up with an
7605** entry point name on its own. It first tries "sqlite3_extension_init".
7606** If that does not work, it constructs a name "sqlite3_X_init" where the
7607** X is consists of the lower-case equivalent of all ASCII alphabetic
7608** characters in the filename from the last "/" to the first following
7609** "." and omitting any initial "lib".)^
7610** ^The sqlite3_load_extension() interface returns
7611** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7612** ^If an error occurs and pzErrMsg is not 0, then the
7613** [sqlite3_load_extension()] interface shall attempt to
7614** fill *pzErrMsg with error message text stored in memory
7615** obtained from [sqlite3_malloc()]. The calling function
7616** should free this memory by calling [sqlite3_free()].
7617**
7618** ^Extension loading must be enabled using
7619** [sqlite3_enable_load_extension()] or
7620** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7621** prior to calling this API,
7622** otherwise an error will be returned.
7623**
7624** <b>Security warning:</b> It is recommended that the
7625** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7626** interface. The use of the [sqlite3_enable_load_extension()] interface
7627** should be avoided. This will keep the SQL function [load_extension()]
7628** disabled and prevent SQL injections from giving attackers
7629** access to extension loading capabilities.
7630**
7631** See also the [load_extension() SQL function].
7632*/
7633SQLITE_API int sqlite3_load_extension(
7634 sqlite3 *db, /* Load the extension into this database connection */
7635 const char *zFile, /* Name of the shared library containing extension */
7636 const char *zProc, /* Entry point. Derived from zFile if 0 */
7637 char **pzErrMsg /* Put error message here if not 0 */
7638);
7639
7640/*
7641** CAPI3REF: Enable Or Disable Extension Loading
7642** METHOD: sqlite3
7643**
7644** ^So as not to open security holes in older applications that are
7645** unprepared to deal with [extension loading], and as a means of disabling
7646** [extension loading] while evaluating user-entered SQL, the following API
7647** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7648**
7649** ^Extension loading is off by default.
7650** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7651** to turn extension loading on and call it with onoff==0 to turn
7652** it back off again.
7653**
7654** ^This interface enables or disables both the C-API
7655** [sqlite3_load_extension()] and the SQL function [load_extension()].
7656** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7657** to enable or disable only the C-API.)^
7658**
7659** <b>Security warning:</b> It is recommended that extension loading
7660** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7661** rather than this interface, so the [load_extension()] SQL function
7662** remains disabled. This will prevent SQL injections from giving attackers
7663** access to extension loading capabilities.
7664*/
7665SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7666
7667/*
7668** CAPI3REF: Automatically Load Statically Linked Extensions
7669**
7670** ^This interface causes the xEntryPoint() function to be invoked for
7671** each new [database connection] that is created. The idea here is that
7672** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7673** that is to be automatically loaded into all new database connections.
7674**
7675** ^(Even though the function prototype shows that xEntryPoint() takes
7676** no arguments and returns void, SQLite invokes xEntryPoint() with three
7677** arguments and expects an integer result as if the signature of the
7678** entry point where as follows:
7679**
7680** <blockquote><pre>
7681** &nbsp; int xEntryPoint(
7682** &nbsp; sqlite3 *db,
7683** &nbsp; const char **pzErrMsg,
7684** &nbsp; const struct sqlite3_api_routines *pThunk
7685** &nbsp; );
7686** </pre></blockquote>)^
7687**
7688** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7689** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7690** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7691** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7692** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7693** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7694** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7695**
7696** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7697** on the list of automatic extensions is a harmless no-op. ^No entry point
7698** will be called more than once for each database connection that is opened.
7699**
7700** See also: [sqlite3_reset_auto_extension()]
7701** and [sqlite3_cancel_auto_extension()]
7702*/
7703SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7704
7705/*
7706** CAPI3REF: Cancel Automatic Extension Loading
7707**
7708** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7709** initialization routine X that was registered using a prior call to
7710** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7711** routine returns 1 if initialization routine X was successfully
7712** unregistered and it returns 0 if X was not on the list of initialization
7713** routines.
7714*/
7715SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7716
7717/*
7718** CAPI3REF: Reset Automatic Extension Loading
7719**
7720** ^This interface disables all automatic extensions previously
7721** registered using [sqlite3_auto_extension()].
7722*/
7723SQLITE_API void sqlite3_reset_auto_extension(void);
7724
7725/*
7726** The interface to the virtual-table mechanism is currently considered
7727** to be experimental. The interface might change in incompatible ways.
7728** If this is a problem for you, do not use the interface at this time.
7729**
7730** When the virtual-table mechanism stabilizes, we will declare the
7731** interface fixed, support it indefinitely, and remove this comment.
7732*/
7733
7734/*
7735** Structures used by the virtual table interface
7736*/
7737typedef struct sqlite3_vtab sqlite3_vtab;
7738typedef struct sqlite3_index_info sqlite3_index_info;
7739typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7740typedef struct sqlite3_module sqlite3_module;
7741
7742/*
7743** CAPI3REF: Virtual Table Object
7744** KEYWORDS: sqlite3_module {virtual table module}
7745**
7746** This structure, sometimes called a "virtual table module",
7747** defines the implementation of a [virtual table].
7748** This structure consists mostly of methods for the module.
7749**
7750** ^A virtual table module is created by filling in a persistent
7751** instance of this structure and passing a pointer to that instance
7752** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7753** ^The registration remains valid until it is replaced by a different
7754** module or until the [database connection] closes. The content
7755** of this structure must not change while it is registered with
7756** any database connection.
7757*/
7758struct sqlite3_module {
7759 int iVersion;
7760 int (*xCreate)(sqlite3*, void *pAux,
7761 int argc, const char *const*argv,
7762 sqlite3_vtab **ppVTab, char**);
7763 int (*xConnect)(sqlite3*, void *pAux,
7764 int argc, const char *const*argv,
7765 sqlite3_vtab **ppVTab, char**);
7766 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7767 int (*xDisconnect)(sqlite3_vtab *pVTab);
7768 int (*xDestroy)(sqlite3_vtab *pVTab);
7769 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7770 int (*xClose)(sqlite3_vtab_cursor*);
7771 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7772 int argc, sqlite3_value **argv);
7773 int (*xNext)(sqlite3_vtab_cursor*);
7774 int (*xEof)(sqlite3_vtab_cursor*);
7775 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7776 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7777 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7778 int (*xBegin)(sqlite3_vtab *pVTab);
7779 int (*xSync)(sqlite3_vtab *pVTab);
7780 int (*xCommit)(sqlite3_vtab *pVTab);
7781 int (*xRollback)(sqlite3_vtab *pVTab);
7782 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7783 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7784 void **ppArg);
7785 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7786 /* The methods above are in version 1 of the sqlite_module object. Those
7787 ** below are for version 2 and greater. */
7788 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7789 int (*xRelease)(sqlite3_vtab *pVTab, int);
7790 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7791 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7792 ** Those below are for version 3 and greater. */
7793 int (*xShadowName)(const char*);
7794};
7795
7796/*
7797** CAPI3REF: Virtual Table Indexing Information
7798** KEYWORDS: sqlite3_index_info
7799**
7800** The sqlite3_index_info structure and its substructures is used as part
7801** of the [virtual table] interface to
7802** pass information into and receive the reply from the [xBestIndex]
7803** method of a [virtual table module]. The fields under **Inputs** are the
7804** inputs to xBestIndex and are read-only. xBestIndex inserts its
7805** results into the **Outputs** fields.
7806**
7807** ^(The aConstraint[] array records WHERE clause constraints of the form:
7808**
7809** <blockquote>column OP expr</blockquote>
7810**
7811** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7812** stored in aConstraint[].op using one of the
7813** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7814** ^(The index of the column is stored in
7815** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7816** expr on the right-hand side can be evaluated (and thus the constraint
7817** is usable) and false if it cannot.)^
7818**
7819** ^The optimizer automatically inverts terms of the form "expr OP column"
7820** and makes other simplifications to the WHERE clause in an attempt to
7821** get as many WHERE clause terms into the form shown above as possible.
7822** ^The aConstraint[] array only reports WHERE clause terms that are
7823** relevant to the particular virtual table being queried.
7824**
7825** ^Information about the ORDER BY clause is stored in aOrderBy[].
7826** ^Each term of aOrderBy records a column of the ORDER BY clause.
7827**
7828** The colUsed field indicates which columns of the virtual table may be
7829** required by the current scan. Virtual table columns are numbered from
7830** zero in the order in which they appear within the CREATE TABLE statement
7831** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7832** the corresponding bit is set within the colUsed mask if the column may be
7833** required by SQLite. If the table has at least 64 columns and any column
7834** to the right of the first 63 is required, then bit 63 of colUsed is also
7835** set. In other words, column iCol may be required if the expression
7836** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7837** non-zero.
7838**
7839** The [xBestIndex] method must fill aConstraintUsage[] with information
7840** about what parameters to pass to xFilter. ^If argvIndex>0 then
7841** the right-hand side of the corresponding aConstraint[] is evaluated
7842** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7843** is true, then the constraint is assumed to be fully handled by the
7844** virtual table and might not be checked again by the byte code.)^ ^(The
7845** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7846** is left in its default setting of false, the constraint will always be
7847** checked separately in byte code. If the omit flag is change to true, then
7848** the constraint may or may not be checked in byte code. In other words,
7849** when the omit flag is true there is no guarantee that the constraint will
7850** not be checked again using byte code.)^
7851**
7852** ^The idxNum and idxPtr values are recorded and passed into the
7853** [xFilter] method.
7854** ^[sqlite3_free()] is used to free idxPtr if and only if
7855** needToFreeIdxPtr is true.
7856**
7857** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7858** the correct order to satisfy the ORDER BY clause so that no separate
7859** sorting step is required.
7860**
7861** ^The estimatedCost value is an estimate of the cost of a particular
7862** strategy. A cost of N indicates that the cost of the strategy is similar
7863** to a linear scan of an SQLite table with N rows. A cost of log(N)
7864** indicates that the expense of the operation is similar to that of a
7865** binary search on a unique indexed field of an SQLite table with N rows.
7866**
7867** ^The estimatedRows value is an estimate of the number of rows that
7868** will be returned by the strategy.
7869**
7870** The xBestIndex method may optionally populate the idxFlags field with a
7871** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7872** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7873** assumes that the strategy may visit at most one row.
7874**
7875** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7876** SQLite also assumes that if a call to the xUpdate() method is made as
7877** part of the same statement to delete or update a virtual table row and the
7878** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7879** any database changes. In other words, if the xUpdate() returns
7880** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7881** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7882** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7883** the xUpdate method are automatically rolled back by SQLite.
7884**
7885** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7886** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7887** If a virtual table extension is
7888** used with an SQLite version earlier than 3.8.2, the results of attempting
7889** to read or write the estimatedRows field are undefined (but are likely
7890** to include crashing the application). The estimatedRows field should
7891** therefore only be used if [sqlite3_libversion_number()] returns a
7892** value greater than or equal to 3008002. Similarly, the idxFlags field
7893** was added for [version 3.9.0] ([dateof:3.9.0]).
7894** It may therefore only be used if
7895** sqlite3_libversion_number() returns a value greater than or equal to
7896** 3009000.
7897*/
7898struct sqlite3_index_info {
7899 /* Inputs */
7900 int nConstraint; /* Number of entries in aConstraint */
7901 struct sqlite3_index_constraint {
7902 int iColumn; /* Column constrained. -1 for ROWID */
7903 unsigned char op; /* Constraint operator */
7904 unsigned char usable; /* True if this constraint is usable */
7905 int iTermOffset; /* Used internally - xBestIndex should ignore */
7906 } *aConstraint; /* Table of WHERE clause constraints */
7907 int nOrderBy; /* Number of terms in the ORDER BY clause */
7908 struct sqlite3_index_orderby {
7909 int iColumn; /* Column number */
7910 unsigned char desc; /* True for DESC. False for ASC. */
7911 } *aOrderBy; /* The ORDER BY clause */
7912 /* Outputs */
7913 struct sqlite3_index_constraint_usage {
7914 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7915 unsigned char omit; /* Do not code a test for this constraint */
7916 } *aConstraintUsage;
7917 int idxNum; /* Number used to identify the index */
7918 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7919 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7920 int orderByConsumed; /* True if output is already ordered */
7921 double estimatedCost; /* Estimated cost of using this index */
7922 /* Fields below are only available in SQLite 3.8.2 and later */
7923 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7924 /* Fields below are only available in SQLite 3.9.0 and later */
7925 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7926 /* Fields below are only available in SQLite 3.10.0 and later */
7927 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7928};
7929
7930/*
7931** CAPI3REF: Virtual Table Scan Flags
7932**
7933** Virtual table implementations are allowed to set the
7934** [sqlite3_index_info].idxFlags field to some combination of
7935** these bits.
7936*/
7937#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7938
7939/*
7940** CAPI3REF: Virtual Table Constraint Operator Codes
7941**
7942** These macros define the allowed values for the
7943** [sqlite3_index_info].aConstraint[].op field. Each value represents
7944** an operator that is part of a constraint term in the wHERE clause of
7945** a query that uses a [virtual table].
7946*/
7947#define SQLITE_INDEX_CONSTRAINT_EQ 2
7948#define SQLITE_INDEX_CONSTRAINT_GT 4
7949#define SQLITE_INDEX_CONSTRAINT_LE 8
7950#define SQLITE_INDEX_CONSTRAINT_LT 16
7951#define SQLITE_INDEX_CONSTRAINT_GE 32
7952#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7953#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7954#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7955#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7956#define SQLITE_INDEX_CONSTRAINT_NE 68
7957#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7958#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7959#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7960#define SQLITE_INDEX_CONSTRAINT_IS 72
7961#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7962
7963/*
7964** CAPI3REF: Register A Virtual Table Implementation
7965** METHOD: sqlite3
7966**
7967** ^These routines are used to register a new [virtual table module] name.
7968** ^Module names must be registered before
7969** creating a new [virtual table] using the module and before using a
7970** preexisting [virtual table] for the module.
7971**
7972** ^The module name is registered on the [database connection] specified
7973** by the first parameter. ^The name of the module is given by the
7974** second parameter. ^The third parameter is a pointer to
7975** the implementation of the [virtual table module]. ^The fourth
7976** parameter is an arbitrary client data pointer that is passed through
7977** into the [xCreate] and [xConnect] methods of the virtual table module
7978** when a new virtual table is be being created or reinitialized.
7979**
7980** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7981** is a pointer to a destructor for the pClientData. ^SQLite will
7982** invoke the destructor function (if it is not NULL) when SQLite
7983** no longer needs the pClientData pointer. ^The destructor will also
7984** be invoked if the call to sqlite3_create_module_v2() fails.
7985** ^The sqlite3_create_module()
7986** interface is equivalent to sqlite3_create_module_v2() with a NULL
7987** destructor.
7988**
7989** ^If the third parameter (the pointer to the sqlite3_module object) is
7990** NULL then no new module is create and any existing modules with the
7991** same name are dropped.
7992**
7993** See also: [sqlite3_drop_modules()]
7994*/
7995SQLITE_API int sqlite3_create_module(
7996 sqlite3 *db, /* SQLite connection to register module with */
7997 const char *zName, /* Name of the module */
7998 const sqlite3_module *p, /* Methods for the module */
7999 void *pClientData /* Client data for xCreate/xConnect */
8000);
8001SQLITE_API int sqlite3_create_module_v2(
8002 sqlite3 *db, /* SQLite connection to register module with */
8003 const char *zName, /* Name of the module */
8004 const sqlite3_module *p, /* Methods for the module */
8005 void *pClientData, /* Client data for xCreate/xConnect */
8006 void(*xDestroy)(void*) /* Module destructor function */
8007);
8008
8009/*
8010** CAPI3REF: Remove Unnecessary Virtual Table Implementations
8011** METHOD: sqlite3
8012**
8013** ^The sqlite3_drop_modules(D,L) interface removes all virtual
8014** table modules from database connection D except those named on list L.
8015** The L parameter must be either NULL or a pointer to an array of pointers
8016** to strings where the array is terminated by a single NULL pointer.
8017** ^If the L parameter is NULL, then all virtual table modules are removed.
8018**
8019** See also: [sqlite3_create_module()]
8020*/
8021SQLITE_API int sqlite3_drop_modules(
8022 sqlite3 *db, /* Remove modules from this connection */
8023 const char **azKeep /* Except, do not remove the ones named here */
8024);
8025
8026/*
8027** CAPI3REF: Virtual Table Instance Object
8028** KEYWORDS: sqlite3_vtab
8029**
8030** Every [virtual table module] implementation uses a subclass
8031** of this object to describe a particular instance
8032** of the [virtual table]. Each subclass will
8033** be tailored to the specific needs of the module implementation.
8034** The purpose of this superclass is to define certain fields that are
8035** common to all module implementations.
8036**
8037** ^Virtual tables methods can set an error message by assigning a
8038** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
8039** take care that any prior string is freed by a call to [sqlite3_free()]
8040** prior to assigning a new string to zErrMsg. ^After the error message
8041** is delivered up to the client application, the string will be automatically
8042** freed by sqlite3_free() and the zErrMsg field will be zeroed.
8043*/
8044struct sqlite3_vtab {
8045 const sqlite3_module *pModule; /* The module for this virtual table */
8046 int nRef; /* Number of open cursors */
8047 char *zErrMsg; /* Error message from sqlite3_mprintf() */
8048 /* Virtual table implementations will typically add additional fields */
8049};
8050
8051/*
8052** CAPI3REF: Virtual Table Cursor Object
8053** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
8054**
8055** Every [virtual table module] implementation uses a subclass of the
8056** following structure to describe cursors that point into the
8057** [virtual table] and are used
8058** to loop through the virtual table. Cursors are created using the
8059** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
8060** by the [sqlite3_module.xClose | xClose] method. Cursors are used
8061** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
8062** of the module. Each module implementation will define
8063** the content of a cursor structure to suit its own needs.
8064**
8065** This superclass exists in order to define fields of the cursor that
8066** are common to all implementations.
8067*/
8068struct sqlite3_vtab_cursor {
8069 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
8070 /* Virtual table implementations will typically add additional fields */
8071};
8072
8073/*
8074** CAPI3REF: Declare The Schema Of A Virtual Table
8075**
8076** ^The [xCreate] and [xConnect] methods of a
8077** [virtual table module] call this interface
8078** to declare the format (the names and datatypes of the columns) of
8079** the virtual tables they implement.
8080*/
8081SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
8082
8083/*
8084** CAPI3REF: Overload A Function For A Virtual Table
8085** METHOD: sqlite3
8086**
8087** ^(Virtual tables can provide alternative implementations of functions
8088** using the [xFindFunction] method of the [virtual table module].
8089** But global versions of those functions
8090** must exist in order to be overloaded.)^
8091**
8092** ^(This API makes sure a global version of a function with a particular
8093** name and number of parameters exists. If no such function exists
8094** before this API is called, a new function is created.)^ ^The implementation
8095** of the new function always causes an exception to be thrown. So
8096** the new function is not good for anything by itself. Its only
8097** purpose is to be a placeholder function that can be overloaded
8098** by a [virtual table].
8099*/
8100SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
8101
8102/*
8103** The interface to the virtual-table mechanism defined above (back up
8104** to a comment remarkably similar to this one) is currently considered
8105** to be experimental. The interface might change in incompatible ways.
8106** If this is a problem for you, do not use the interface at this time.
8107**
8108** When the virtual-table mechanism stabilizes, we will declare the
8109** interface fixed, support it indefinitely, and remove this comment.
8110*/
8111
8112/*
8113** CAPI3REF: A Handle To An Open BLOB
8114** KEYWORDS: {BLOB handle} {BLOB handles}
8115**
8116** An instance of this object represents an open BLOB on which
8117** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
8118** ^Objects of this type are created by [sqlite3_blob_open()]
8119** and destroyed by [sqlite3_blob_close()].
8120** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
8121** can be used to read or write small subsections of the BLOB.
8122** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
8123*/
8124typedef struct sqlite3_blob sqlite3_blob;
8125
8126/*
8127** CAPI3REF: Open A BLOB For Incremental I/O
8128** METHOD: sqlite3
8129** CONSTRUCTOR: sqlite3_blob
8130**
8131** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
8132** in row iRow, column zColumn, table zTable in database zDb;
8133** in other words, the same BLOB that would be selected by:
8134**
8135** <pre>
8136** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
8137** </pre>)^
8138**
8139** ^(Parameter zDb is not the filename that contains the database, but
8140** rather the symbolic name of the database. For attached databases, this is
8141** the name that appears after the AS keyword in the [ATTACH] statement.
8142** For the main database file, the database name is "main". For TEMP
8143** tables, the database name is "temp".)^
8144**
8145** ^If the flags parameter is non-zero, then the BLOB is opened for read
8146** and write access. ^If the flags parameter is zero, the BLOB is opened for
8147** read-only access.
8148**
8149** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
8150** in *ppBlob. Otherwise an [error code] is returned and, unless the error
8151** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
8152** the API is not misused, it is always safe to call [sqlite3_blob_close()]
8153** on *ppBlob after this function it returns.
8154**
8155** This function fails with SQLITE_ERROR if any of the following are true:
8156** <ul>
8157** <li> ^(Database zDb does not exist)^,
8158** <li> ^(Table zTable does not exist within database zDb)^,
8159** <li> ^(Table zTable is a WITHOUT ROWID table)^,
8160** <li> ^(Column zColumn does not exist)^,
8161** <li> ^(Row iRow is not present in the table)^,
8162** <li> ^(The specified column of row iRow contains a value that is not
8163** a TEXT or BLOB value)^,
8164** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
8165** constraint and the blob is being opened for read/write access)^,
8166** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
8167** column zColumn is part of a [child key] definition and the blob is
8168** being opened for read/write access)^.
8169** </ul>
8170**
8171** ^Unless it returns SQLITE_MISUSE, this function sets the
8172** [database connection] error code and message accessible via
8173** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8174**
8175** A BLOB referenced by sqlite3_blob_open() may be read using the
8176** [sqlite3_blob_read()] interface and modified by using
8177** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
8178** different row of the same table using the [sqlite3_blob_reopen()]
8179** interface. However, the column, table, or database of a [BLOB handle]
8180** cannot be changed after the [BLOB handle] is opened.
8181**
8182** ^(If the row that a BLOB handle points to is modified by an
8183** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8184** then the BLOB handle is marked as "expired".
8185** This is true if any column of the row is changed, even a column
8186** other than the one the BLOB handle is open on.)^
8187** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
8188** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
8189** ^(Changes written into a BLOB prior to the BLOB expiring are not
8190** rolled back by the expiration of the BLOB. Such changes will eventually
8191** commit if the transaction continues to completion.)^
8192**
8193** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
8194** the opened blob. ^The size of a blob may not be changed by this
8195** interface. Use the [UPDATE] SQL command to change the size of a
8196** blob.
8197**
8198** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
8199** and the built-in [zeroblob] SQL function may be used to create a
8200** zero-filled blob to read or write using the incremental-blob interface.
8201**
8202** To avoid a resource leak, every open [BLOB handle] should eventually
8203** be released by a call to [sqlite3_blob_close()].
8204**
8205** See also: [sqlite3_blob_close()],
8206** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
8207** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
8208*/
8209SQLITE_API int sqlite3_blob_open(
8210 sqlite3*,
8211 const char *zDb,
8212 const char *zTable,
8213 const char *zColumn,
8214 sqlite3_int64 iRow,
8215 int flags,
8216 sqlite3_blob **ppBlob
8217);
8218
8219/*
8220** CAPI3REF: Move a BLOB Handle to a New Row
8221** METHOD: sqlite3_blob
8222**
8223** ^This function is used to move an existing [BLOB handle] so that it points
8224** to a different row of the same database table. ^The new row is identified
8225** by the rowid value passed as the second argument. Only the row can be
8226** changed. ^The database, table and column on which the blob handle is open
8227** remain the same. Moving an existing [BLOB handle] to a new row is
8228** faster than closing the existing handle and opening a new one.
8229**
8230** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8231** it must exist and there must be either a blob or text value stored in
8232** the nominated column.)^ ^If the new row is not present in the table, or if
8233** it does not contain a blob or text value, or if another error occurs, an
8234** SQLite error code is returned and the blob handle is considered aborted.
8235** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
8236** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
8237** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
8238** always returns zero.
8239**
8240** ^This function sets the database handle error code and message.
8241*/
8242SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8243
8244/*
8245** CAPI3REF: Close A BLOB Handle
8246** DESTRUCTOR: sqlite3_blob
8247**
8248** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
8249** unconditionally. Even if this routine returns an error code, the
8250** handle is still closed.)^
8251**
8252** ^If the blob handle being closed was opened for read-write access, and if
8253** the database is in auto-commit mode and there are no other open read-write
8254** blob handles or active write statements, the current transaction is
8255** committed. ^If an error occurs while committing the transaction, an error
8256** code is returned and the transaction rolled back.
8257**
8258** Calling this function with an argument that is not a NULL pointer or an
8259** open blob handle results in undefined behaviour. ^Calling this routine
8260** with a null pointer (such as would be returned by a failed call to
8261** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8262** is passed a valid open blob handle, the values returned by the
8263** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
8264*/
8265SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8266
8267/*
8268** CAPI3REF: Return The Size Of An Open BLOB
8269** METHOD: sqlite3_blob
8270**
8271** ^Returns the size in bytes of the BLOB accessible via the
8272** successfully opened [BLOB handle] in its only argument. ^The
8273** incremental blob I/O routines can only read or overwriting existing
8274** blob content; they cannot change the size of a blob.
8275**
8276** This routine only works on a [BLOB handle] which has been created
8277** by a prior successful call to [sqlite3_blob_open()] and which has not
8278** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8279** to this routine results in undefined and probably undesirable behavior.
8280*/
8281SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8282
8283/*
8284** CAPI3REF: Read Data From A BLOB Incrementally
8285** METHOD: sqlite3_blob
8286**
8287** ^(This function is used to read data from an open [BLOB handle] into a
8288** caller-supplied buffer. N bytes of data are copied into buffer Z
8289** from the open BLOB, starting at offset iOffset.)^
8290**
8291** ^If offset iOffset is less than N bytes from the end of the BLOB,
8292** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
8293** less than zero, [SQLITE_ERROR] is returned and no data is read.
8294** ^The size of the blob (and hence the maximum value of N+iOffset)
8295** can be determined using the [sqlite3_blob_bytes()] interface.
8296**
8297** ^An attempt to read from an expired [BLOB handle] fails with an
8298** error code of [SQLITE_ABORT].
8299**
8300** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
8301** Otherwise, an [error code] or an [extended error code] is returned.)^
8302**
8303** This routine only works on a [BLOB handle] which has been created
8304** by a prior successful call to [sqlite3_blob_open()] and which has not
8305** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8306** to this routine results in undefined and probably undesirable behavior.
8307**
8308** See also: [sqlite3_blob_write()].
8309*/
8310SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8311
8312/*
8313** CAPI3REF: Write Data Into A BLOB Incrementally
8314** METHOD: sqlite3_blob
8315**
8316** ^(This function is used to write data into an open [BLOB handle] from a
8317** caller-supplied buffer. N bytes of data are copied from the buffer Z
8318** into the open BLOB, starting at offset iOffset.)^
8319**
8320** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
8321** Otherwise, an [error code] or an [extended error code] is returned.)^
8322** ^Unless SQLITE_MISUSE is returned, this function sets the
8323** [database connection] error code and message accessible via
8324** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8325**
8326** ^If the [BLOB handle] passed as the first argument was not opened for
8327** writing (the flags parameter to [sqlite3_blob_open()] was zero),
8328** this function returns [SQLITE_READONLY].
8329**
8330** This function may only modify the contents of the BLOB; it is
8331** not possible to increase the size of a BLOB using this API.
8332** ^If offset iOffset is less than N bytes from the end of the BLOB,
8333** [SQLITE_ERROR] is returned and no data is written. The size of the
8334** BLOB (and hence the maximum value of N+iOffset) can be determined
8335** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
8336** than zero [SQLITE_ERROR] is returned and no data is written.
8337**
8338** ^An attempt to write to an expired [BLOB handle] fails with an
8339** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
8340** before the [BLOB handle] expired are not rolled back by the
8341** expiration of the handle, though of course those changes might
8342** have been overwritten by the statement that expired the BLOB handle
8343** or by other independent statements.
8344**
8345** This routine only works on a [BLOB handle] which has been created
8346** by a prior successful call to [sqlite3_blob_open()] and which has not
8347** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8348** to this routine results in undefined and probably undesirable behavior.
8349**
8350** See also: [sqlite3_blob_read()].
8351*/
8352SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8353
8354/*
8355** CAPI3REF: Virtual File System Objects
8356**
8357** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8358** that SQLite uses to interact
8359** with the underlying operating system. Most SQLite builds come with a
8360** single default VFS that is appropriate for the host computer.
8361** New VFSes can be registered and existing VFSes can be unregistered.
8362** The following interfaces are provided.
8363**
8364** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8365** ^Names are case sensitive.
8366** ^Names are zero-terminated UTF-8 strings.
8367** ^If there is no match, a NULL pointer is returned.
8368** ^If zVfsName is NULL then the default VFS is returned.
8369**
8370** ^New VFSes are registered with sqlite3_vfs_register().
8371** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8372** ^The same VFS can be registered multiple times without injury.
8373** ^To make an existing VFS into the default VFS, register it again
8374** with the makeDflt flag set. If two different VFSes with the
8375** same name are registered, the behavior is undefined. If a
8376** VFS is registered with a name that is NULL or an empty string,
8377** then the behavior is undefined.
8378**
8379** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8380** ^(If the default VFS is unregistered, another VFS is chosen as
8381** the default. The choice for the new VFS is arbitrary.)^
8382*/
8383SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8384SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8385SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8386
8387/*
8388** CAPI3REF: Mutexes
8389**
8390** The SQLite core uses these routines for thread
8391** synchronization. Though they are intended for internal
8392** use by SQLite, code that links against SQLite is
8393** permitted to use any of these routines.
8394**
8395** The SQLite source code contains multiple implementations
8396** of these mutex routines. An appropriate implementation
8397** is selected automatically at compile-time. The following
8398** implementations are available in the SQLite core:
8399**
8400** <ul>
8401** <li> SQLITE_MUTEX_PTHREADS
8402** <li> SQLITE_MUTEX_W32
8403** <li> SQLITE_MUTEX_NOOP
8404** </ul>
8405**
8406** The SQLITE_MUTEX_NOOP implementation is a set of routines
8407** that does no real locking and is appropriate for use in
8408** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8409** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8410** and Windows.
8411**
8412** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8413** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8414** implementation is included with the library. In this case the
8415** application must supply a custom mutex implementation using the
8416** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8417** before calling sqlite3_initialize() or any other public sqlite3_
8418** function that calls sqlite3_initialize().
8419**
8420** ^The sqlite3_mutex_alloc() routine allocates a new
8421** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8422** routine returns NULL if it is unable to allocate the requested
8423** mutex. The argument to sqlite3_mutex_alloc() must one of these
8424** integer constants:
8425**
8426** <ul>
8427** <li> SQLITE_MUTEX_FAST
8428** <li> SQLITE_MUTEX_RECURSIVE
8429** <li> SQLITE_MUTEX_STATIC_MAIN
8430** <li> SQLITE_MUTEX_STATIC_MEM
8431** <li> SQLITE_MUTEX_STATIC_OPEN
8432** <li> SQLITE_MUTEX_STATIC_PRNG
8433** <li> SQLITE_MUTEX_STATIC_LRU
8434** <li> SQLITE_MUTEX_STATIC_PMEM
8435** <li> SQLITE_MUTEX_STATIC_APP1
8436** <li> SQLITE_MUTEX_STATIC_APP2
8437** <li> SQLITE_MUTEX_STATIC_APP3
8438** <li> SQLITE_MUTEX_STATIC_VFS1
8439** <li> SQLITE_MUTEX_STATIC_VFS2
8440** <li> SQLITE_MUTEX_STATIC_VFS3
8441** </ul>
8442**
8443** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8444** cause sqlite3_mutex_alloc() to create
8445** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8446** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8447** The mutex implementation does not need to make a distinction
8448** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8449** not want to. SQLite will only request a recursive mutex in
8450** cases where it really needs one. If a faster non-recursive mutex
8451** implementation is available on the host platform, the mutex subsystem
8452** might return such a mutex in response to SQLITE_MUTEX_FAST.
8453**
8454** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8455** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8456** a pointer to a static preexisting mutex. ^Nine static mutexes are
8457** used by the current version of SQLite. Future versions of SQLite
8458** may add additional static mutexes. Static mutexes are for internal
8459** use by SQLite only. Applications that use SQLite mutexes should
8460** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8461** SQLITE_MUTEX_RECURSIVE.
8462**
8463** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8464** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8465** returns a different mutex on every call. ^For the static
8466** mutex types, the same mutex is returned on every call that has
8467** the same type number.
8468**
8469** ^The sqlite3_mutex_free() routine deallocates a previously
8470** allocated dynamic mutex. Attempting to deallocate a static
8471** mutex results in undefined behavior.
8472**
8473** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8474** to enter a mutex. ^If another thread is already within the mutex,
8475** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8476** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8477** upon successful entry. ^(Mutexes created using
8478** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8479** In such cases, the
8480** mutex must be exited an equal number of times before another thread
8481** can enter.)^ If the same thread tries to enter any mutex other
8482** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8483**
8484** ^(Some systems (for example, Windows 95) do not support the operation
8485** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8486** will always return SQLITE_BUSY. The SQLite core only ever uses
8487** sqlite3_mutex_try() as an optimization so this is acceptable
8488** behavior.)^
8489**
8490** ^The sqlite3_mutex_leave() routine exits a mutex that was
8491** previously entered by the same thread. The behavior
8492** is undefined if the mutex is not currently entered by the
8493** calling thread or is not currently allocated.
8494**
8495** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
8496** sqlite3_mutex_leave() is a NULL pointer, then all three routines
8497** behave as no-ops.
8498**
8499** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8500*/
8501SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8502SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8503SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8504SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8505SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8506
8507/*
8508** CAPI3REF: Mutex Methods Object
8509**
8510** An instance of this structure defines the low-level routines
8511** used to allocate and use mutexes.
8512**
8513** Usually, the default mutex implementations provided by SQLite are
8514** sufficient, however the application has the option of substituting a custom
8515** implementation for specialized deployments or systems for which SQLite
8516** does not provide a suitable implementation. In this case, the application
8517** creates and populates an instance of this structure to pass
8518** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8519** Additionally, an instance of this structure can be used as an
8520** output variable when querying the system for the current mutex
8521** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8522**
8523** ^The xMutexInit method defined by this structure is invoked as
8524** part of system initialization by the sqlite3_initialize() function.
8525** ^The xMutexInit routine is called by SQLite exactly once for each
8526** effective call to [sqlite3_initialize()].
8527**
8528** ^The xMutexEnd method defined by this structure is invoked as
8529** part of system shutdown by the sqlite3_shutdown() function. The
8530** implementation of this method is expected to release all outstanding
8531** resources obtained by the mutex methods implementation, especially
8532** those obtained by the xMutexInit method. ^The xMutexEnd()
8533** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8534**
8535** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8536** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8537** xMutexNotheld) implement the following interfaces (respectively):
8538**
8539** <ul>
8540** <li> [sqlite3_mutex_alloc()] </li>
8541** <li> [sqlite3_mutex_free()] </li>
8542** <li> [sqlite3_mutex_enter()] </li>
8543** <li> [sqlite3_mutex_try()] </li>
8544** <li> [sqlite3_mutex_leave()] </li>
8545** <li> [sqlite3_mutex_held()] </li>
8546** <li> [sqlite3_mutex_notheld()] </li>
8547** </ul>)^
8548**
8549** The only difference is that the public sqlite3_XXX functions enumerated
8550** above silently ignore any invocations that pass a NULL pointer instead
8551** of a valid mutex handle. The implementations of the methods defined
8552** by this structure are not required to handle this case. The results
8553** of passing a NULL pointer instead of a valid mutex handle are undefined
8554** (i.e. it is acceptable to provide an implementation that segfaults if
8555** it is passed a NULL pointer).
8556**
8557** The xMutexInit() method must be threadsafe. It must be harmless to
8558** invoke xMutexInit() multiple times within the same process and without
8559** intervening calls to xMutexEnd(). Second and subsequent calls to
8560** xMutexInit() must be no-ops.
8561**
8562** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8563** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8564** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8565** memory allocation for a fast or recursive mutex.
8566**
8567** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8568** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8569** If xMutexInit fails in any way, it is expected to clean up after itself
8570** prior to returning.
8571*/
8572typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8573struct sqlite3_mutex_methods {
8574 int (*xMutexInit)(void);
8575 int (*xMutexEnd)(void);
8576 sqlite3_mutex *(*xMutexAlloc)(int);
8577 void (*xMutexFree)(sqlite3_mutex *);
8578 void (*xMutexEnter)(sqlite3_mutex *);
8579 int (*xMutexTry)(sqlite3_mutex *);
8580 void (*xMutexLeave)(sqlite3_mutex *);
8581 int (*xMutexHeld)(sqlite3_mutex *);
8582 int (*xMutexNotheld)(sqlite3_mutex *);
8583};
8584
8585/*
8586** CAPI3REF: Mutex Verification Routines
8587**
8588** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8589** are intended for use inside assert() statements. The SQLite core
8590** never uses these routines except inside an assert() and applications
8591** are advised to follow the lead of the core. The SQLite core only
8592** provides implementations for these routines when it is compiled
8593** with the SQLITE_DEBUG flag. External mutex implementations
8594** are only required to provide these routines if SQLITE_DEBUG is
8595** defined and if NDEBUG is not defined.
8596**
8597** These routines should return true if the mutex in their argument
8598** is held or not held, respectively, by the calling thread.
8599**
8600** The implementation is not required to provide versions of these
8601** routines that actually work. If the implementation does not provide working
8602** versions of these routines, it should at least provide stubs that always
8603** return true so that one does not get spurious assertion failures.
8604**
8605** If the argument to sqlite3_mutex_held() is a NULL pointer then
8606** the routine should return 1. This seems counter-intuitive since
8607** clearly the mutex cannot be held if it does not exist. But
8608** the reason the mutex does not exist is because the build is not
8609** using mutexes. And we do not want the assert() containing the
8610** call to sqlite3_mutex_held() to fail, so a non-zero return is
8611** the appropriate thing to do. The sqlite3_mutex_notheld()
8612** interface should also return 1 when given a NULL pointer.
8613*/
8614#ifndef NDEBUG
8615SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8616SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8617#endif
8618
8619/*
8620** CAPI3REF: Mutex Types
8621**
8622** The [sqlite3_mutex_alloc()] interface takes a single argument
8623** which is one of these integer constants.
8624**
8625** The set of static mutexes may change from one SQLite release to the
8626** next. Applications that override the built-in mutex logic must be
8627** prepared to accommodate additional static mutexes.
8628*/
8629#define SQLITE_MUTEX_FAST 0
8630#define SQLITE_MUTEX_RECURSIVE 1
8631#define SQLITE_MUTEX_STATIC_MAIN 2
8632#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
8633#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
8634#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
8635#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
8636#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8637#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
8638#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
8639#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
8640#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
8641#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
8642#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8643#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */
8644#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */
8645
8646/* Legacy compatibility: */
8647#define SQLITE_MUTEX_STATIC_MASTER 2
8648
8649
8650/*
8651** CAPI3REF: Retrieve the mutex for a database connection
8652** METHOD: sqlite3
8653**
8654** ^This interface returns a pointer the [sqlite3_mutex] object that
8655** serializes access to the [database connection] given in the argument
8656** when the [threading mode] is Serialized.
8657** ^If the [threading mode] is Single-thread or Multi-thread then this
8658** routine returns a NULL pointer.
8659*/
8660SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8661
8662/*
8663** CAPI3REF: Low-Level Control Of Database Files
8664** METHOD: sqlite3
8665** KEYWORDS: {file control}
8666**
8667** ^The [sqlite3_file_control()] interface makes a direct call to the
8668** xFileControl method for the [sqlite3_io_methods] object associated
8669** with a particular database identified by the second argument. ^The
8670** name of the database is "main" for the main database or "temp" for the
8671** TEMP database, or the name that appears after the AS keyword for
8672** databases that are added using the [ATTACH] SQL command.
8673** ^A NULL pointer can be used in place of "main" to refer to the
8674** main database file.
8675** ^The third and fourth parameters to this routine
8676** are passed directly through to the second and third parameters of
8677** the xFileControl method. ^The return value of the xFileControl
8678** method becomes the return value of this routine.
8679**
8680** A few opcodes for [sqlite3_file_control()] are handled directly
8681** by the SQLite core and never invoke the
8682** sqlite3_io_methods.xFileControl method.
8683** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8684** a pointer to the underlying [sqlite3_file] object to be written into
8685** the space pointed to by the 4th parameter. The
8686** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8687** the [sqlite3_file] object associated with the journal file instead of
8688** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8689** a pointer to the underlying [sqlite3_vfs] object for the file.
8690** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8691** from the pager.
8692**
8693** ^If the second parameter (zDbName) does not match the name of any
8694** open database file, then SQLITE_ERROR is returned. ^This error
8695** code is not remembered and will not be recalled by [sqlite3_errcode()]
8696** or [sqlite3_errmsg()]. The underlying xFileControl method might
8697** also return SQLITE_ERROR. There is no way to distinguish between
8698** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8699** xFileControl method.
8700**
8701** See also: [file control opcodes]
8702*/
8703SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8704
8705/*
8706** CAPI3REF: Testing Interface
8707**
8708** ^The sqlite3_test_control() interface is used to read out internal
8709** state of SQLite and to inject faults into SQLite for testing
8710** purposes. ^The first parameter is an operation code that determines
8711** the number, meaning, and operation of all subsequent parameters.
8712**
8713** This interface is not for use by applications. It exists solely
8714** for verifying the correct operation of the SQLite library. Depending
8715** on how the SQLite library is compiled, this interface might not exist.
8716**
8717** The details of the operation codes, their meanings, the parameters
8718** they take, and what they do are all subject to change without notice.
8719** Unlike most of the SQLite API, this function is not guaranteed to
8720** operate consistently from one release to the next.
8721*/
8722SQLITE_API int sqlite3_test_control(int op, ...);
8723
8724/*
8725** CAPI3REF: Testing Interface Operation Codes
8726**
8727** These constants are the valid operation code parameters used
8728** as the first argument to [sqlite3_test_control()].
8729**
8730** These parameters and their meanings are subject to change
8731** without notice. These values are for testing purposes only.
8732** Applications should not use any of these parameters or the
8733** [sqlite3_test_control()] interface.
8734*/
8735#define SQLITE_TESTCTRL_FIRST 5
8736#define SQLITE_TESTCTRL_PRNG_SAVE 5
8737#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8738#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */
8739#define SQLITE_TESTCTRL_BITVEC_TEST 8
8740#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8741#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
8742#define SQLITE_TESTCTRL_PENDING_BYTE 11
8743#define SQLITE_TESTCTRL_ASSERT 12
8744#define SQLITE_TESTCTRL_ALWAYS 13
8745#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */
8746#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8747#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8748#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
8749#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17
8750#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
8751#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
8752#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19
8753#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
8754#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
8755#define SQLITE_TESTCTRL_BYTEORDER 22
8756#define SQLITE_TESTCTRL_ISINIT 23
8757#define SQLITE_TESTCTRL_SORTER_MMAP 24
8758#define SQLITE_TESTCTRL_IMPOSTER 25
8759#define SQLITE_TESTCTRL_PARSER_COVERAGE 26
8760#define SQLITE_TESTCTRL_RESULT_INTREAL 27
8761#define SQLITE_TESTCTRL_PRNG_SEED 28
8762#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8763#define SQLITE_TESTCTRL_LAST 29 /* Largest TESTCTRL */
8764
8765/*
8766** CAPI3REF: SQL Keyword Checking
8767**
8768** These routines provide access to the set of SQL language keywords
8769** recognized by SQLite. Applications can uses these routines to determine
8770** whether or not a specific identifier needs to be escaped (for example,
8771** by enclosing in double-quotes) so as not to confuse the parser.
8772**
8773** The sqlite3_keyword_count() interface returns the number of distinct
8774** keywords understood by SQLite.
8775**
8776** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8777** makes *Z point to that keyword expressed as UTF8 and writes the number
8778** of bytes in the keyword into *L. The string that *Z points to is not
8779** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8780** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8781** or L are NULL or invalid pointers then calls to
8782** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8783**
8784** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8785** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8786** if it is and zero if not.
8787**
8788** The parser used by SQLite is forgiving. It is often possible to use
8789** a keyword as an identifier as long as such use does not result in a
8790** parsing ambiguity. For example, the statement
8791** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8792** creates a new table named "BEGIN" with three columns named
8793** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8794** using keywords as identifiers. Common techniques used to avoid keyword
8795** name collisions include:
8796** <ul>
8797** <li> Put all identifier names inside double-quotes. This is the official
8798** SQL way to escape identifier names.
8799** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8800** but it is what SQL Server does and so lots of programmers use this
8801** technique.
8802** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8803** with "Z".
8804** <li> Include a digit somewhere in every identifier name.
8805** </ul>
8806**
8807** Note that the number of keywords understood by SQLite can depend on
8808** compile-time options. For example, "VACUUM" is not a keyword if
8809** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8810** new keywords may be added to future releases of SQLite.
8811*/
8812SQLITE_API int sqlite3_keyword_count(void);
8813SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8814SQLITE_API int sqlite3_keyword_check(const char*,int);
8815
8816/*
8817** CAPI3REF: Dynamic String Object
8818** KEYWORDS: {dynamic string}
8819**
8820** An instance of the sqlite3_str object contains a dynamically-sized
8821** string under construction.
8822**
8823** The lifecycle of an sqlite3_str object is as follows:
8824** <ol>
8825** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8826** <li> ^Text is appended to the sqlite3_str object using various
8827** methods, such as [sqlite3_str_appendf()].
8828** <li> ^The sqlite3_str object is destroyed and the string it created
8829** is returned using the [sqlite3_str_finish()] interface.
8830** </ol>
8831*/
8832typedef struct sqlite3_str sqlite3_str;
8833
8834/*
8835** CAPI3REF: Create A New Dynamic String Object
8836** CONSTRUCTOR: sqlite3_str
8837**
8838** ^The [sqlite3_str_new(D)] interface allocates and initializes
8839** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8840** [sqlite3_str_new()] must be freed by a subsequent call to
8841** [sqlite3_str_finish(X)].
8842**
8843** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8844** valid [sqlite3_str] object, though in the event of an out-of-memory
8845** error the returned object might be a special singleton that will
8846** silently reject new text, always return SQLITE_NOMEM from
8847** [sqlite3_str_errcode()], always return 0 for
8848** [sqlite3_str_length()], and always return NULL from
8849** [sqlite3_str_finish(X)]. It is always safe to use the value
8850** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8851** to any of the other [sqlite3_str] methods.
8852**
8853** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8854** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8855** length of the string contained in the [sqlite3_str] object will be
8856** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8857** of [SQLITE_MAX_LENGTH].
8858*/
8859SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8860
8861/*
8862** CAPI3REF: Finalize A Dynamic String
8863** DESTRUCTOR: sqlite3_str
8864**
8865** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8866** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8867** that contains the constructed string. The calling application should
8868** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8869** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8870** errors were encountered during construction of the string. ^The
8871** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8872** string in [sqlite3_str] object X is zero bytes long.
8873*/
8874SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8875
8876/*
8877** CAPI3REF: Add Content To A Dynamic String
8878** METHOD: sqlite3_str
8879**
8880** These interfaces add content to an sqlite3_str object previously obtained
8881** from [sqlite3_str_new()].
8882**
8883** ^The [sqlite3_str_appendf(X,F,...)] and
8884** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8885** functionality of SQLite to append formatted text onto the end of
8886** [sqlite3_str] object X.
8887**
8888** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8889** onto the end of the [sqlite3_str] object X. N must be non-negative.
8890** S must contain at least N non-zero bytes of content. To append a
8891** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8892** method instead.
8893**
8894** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8895** zero-terminated string S onto the end of [sqlite3_str] object X.
8896**
8897** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8898** single-byte character C onto the end of [sqlite3_str] object X.
8899** ^This method can be used, for example, to add whitespace indentation.
8900**
8901** ^The [sqlite3_str_reset(X)] method resets the string under construction
8902** inside [sqlite3_str] object X back to zero bytes in length.
8903**
8904** These methods do not return a result code. ^If an error occurs, that fact
8905** is recorded in the [sqlite3_str] object and can be recovered by a
8906** subsequent call to [sqlite3_str_errcode(X)].
8907*/
8908SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8909SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8910SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8911SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8912SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8913SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8914
8915/*
8916** CAPI3REF: Status Of A Dynamic String
8917** METHOD: sqlite3_str
8918**
8919** These interfaces return the current status of an [sqlite3_str] object.
8920**
8921** ^If any prior errors have occurred while constructing the dynamic string
8922** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8923** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8924** [SQLITE_NOMEM] following any out-of-memory error, or
8925** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8926** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8927**
8928** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8929** of the dynamic string under construction in [sqlite3_str] object X.
8930** ^The length returned by [sqlite3_str_length(X)] does not include the
8931** zero-termination byte.
8932**
8933** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8934** content of the dynamic string under construction in X. The value
8935** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8936** and might be freed or altered by any subsequent method on the same
8937** [sqlite3_str] object. Applications must not used the pointer returned
8938** [sqlite3_str_value(X)] after any subsequent method call on the same
8939** object. ^Applications may change the content of the string returned
8940** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8941** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8942** write any byte after any subsequent sqlite3_str method call.
8943*/
8944SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8945SQLITE_API int sqlite3_str_length(sqlite3_str*);
8946SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8947
8948/*
8949** CAPI3REF: SQLite Runtime Status
8950**
8951** ^These interfaces are used to retrieve runtime status information
8952** about the performance of SQLite, and optionally to reset various
8953** highwater marks. ^The first argument is an integer code for
8954** the specific parameter to measure. ^(Recognized integer codes
8955** are of the form [status parameters | SQLITE_STATUS_...].)^
8956** ^The current value of the parameter is returned into *pCurrent.
8957** ^The highest recorded value is returned in *pHighwater. ^If the
8958** resetFlag is true, then the highest record value is reset after
8959** *pHighwater is written. ^(Some parameters do not record the highest
8960** value. For those parameters
8961** nothing is written into *pHighwater and the resetFlag is ignored.)^
8962** ^(Other parameters record only the highwater mark and not the current
8963** value. For these latter parameters nothing is written into *pCurrent.)^
8964**
8965** ^The sqlite3_status() and sqlite3_status64() routines return
8966** SQLITE_OK on success and a non-zero [error code] on failure.
8967**
8968** If either the current value or the highwater mark is too large to
8969** be represented by a 32-bit integer, then the values returned by
8970** sqlite3_status() are undefined.
8971**
8972** See also: [sqlite3_db_status()]
8973*/
8974SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8975SQLITE_API int sqlite3_status64(
8976 int op,
8977 sqlite3_int64 *pCurrent,
8978 sqlite3_int64 *pHighwater,
8979 int resetFlag
8980);
8981
8982
8983/*
8984** CAPI3REF: Status Parameters
8985** KEYWORDS: {status parameters}
8986**
8987** These integer constants designate various run-time status parameters
8988** that can be returned by [sqlite3_status()].
8989**
8990** <dl>
8991** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8992** <dd>This parameter is the current amount of memory checked out
8993** using [sqlite3_malloc()], either directly or indirectly. The
8994** figure includes calls made to [sqlite3_malloc()] by the application
8995** and internal memory usage by the SQLite library. Auxiliary page-cache
8996** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8997** this parameter. The amount returned is the sum of the allocation
8998** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8999**
9000** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
9001** <dd>This parameter records the largest memory allocation request
9002** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
9003** internal equivalents). Only the value returned in the
9004** *pHighwater parameter to [sqlite3_status()] is of interest.
9005** The value written into the *pCurrent parameter is undefined.</dd>)^
9006**
9007** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
9008** <dd>This parameter records the number of separate memory allocations
9009** currently checked out.</dd>)^
9010**
9011** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
9012** <dd>This parameter returns the number of pages used out of the
9013** [pagecache memory allocator] that was configured using
9014** [SQLITE_CONFIG_PAGECACHE]. The
9015** value returned is in pages, not in bytes.</dd>)^
9016**
9017** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
9018** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
9019** <dd>This parameter returns the number of bytes of page cache
9020** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
9021** buffer and where forced to overflow to [sqlite3_malloc()]. The
9022** returned value includes allocations that overflowed because they
9023** where too large (they were larger than the "sz" parameter to
9024** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
9025** no space was left in the page cache.</dd>)^
9026**
9027** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
9028** <dd>This parameter records the largest memory allocation request
9029** handed to the [pagecache memory allocator]. Only the value returned in the
9030** *pHighwater parameter to [sqlite3_status()] is of interest.
9031** The value written into the *pCurrent parameter is undefined.</dd>)^
9032**
9033** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
9034** <dd>No longer used.</dd>
9035**
9036** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
9037** <dd>No longer used.</dd>
9038**
9039** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
9040** <dd>No longer used.</dd>
9041**
9042** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
9043** <dd>The *pHighwater parameter records the deepest parser stack.
9044** The *pCurrent value is undefined. The *pHighwater value is only
9045** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
9046** </dl>
9047**
9048** New status parameters may be added from time to time.
9049*/
9050#define SQLITE_STATUS_MEMORY_USED 0
9051#define SQLITE_STATUS_PAGECACHE_USED 1
9052#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
9053#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */
9054#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */
9055#define SQLITE_STATUS_MALLOC_SIZE 5
9056#define SQLITE_STATUS_PARSER_STACK 6
9057#define SQLITE_STATUS_PAGECACHE_SIZE 7
9058#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */
9059#define SQLITE_STATUS_MALLOC_COUNT 9
9060
9061/*
9062** CAPI3REF: Database Connection Status
9063** METHOD: sqlite3
9064**
9065** ^This interface is used to retrieve runtime status information
9066** about a single [database connection]. ^The first argument is the
9067** database connection object to be interrogated. ^The second argument
9068** is an integer constant, taken from the set of
9069** [SQLITE_DBSTATUS options], that
9070** determines the parameter to interrogate. The set of
9071** [SQLITE_DBSTATUS options] is likely
9072** to grow in future releases of SQLite.
9073**
9074** ^The current value of the requested parameter is written into *pCur
9075** and the highest instantaneous value is written into *pHiwtr. ^If
9076** the resetFlg is true, then the highest instantaneous value is
9077** reset back down to the current value.
9078**
9079** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
9080** non-zero [error code] on failure.
9081**
9082** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
9083*/
9084SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
9085
9086/*
9087** CAPI3REF: Status Parameters for database connections
9088** KEYWORDS: {SQLITE_DBSTATUS options}
9089**
9090** These constants are the available integer "verbs" that can be passed as
9091** the second argument to the [sqlite3_db_status()] interface.
9092**
9093** New verbs may be added in future releases of SQLite. Existing verbs
9094** might be discontinued. Applications should check the return code from
9095** [sqlite3_db_status()] to make sure that the call worked.
9096** The [sqlite3_db_status()] interface will return a non-zero error code
9097** if a discontinued or unsupported verb is invoked.
9098**
9099** <dl>
9100** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
9101** <dd>This parameter returns the number of lookaside memory slots currently
9102** checked out.</dd>)^
9103**
9104** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
9105** <dd>This parameter returns the number of malloc attempts that were
9106** satisfied using lookaside memory. Only the high-water value is meaningful;
9107** the current value is always zero.)^
9108**
9109** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
9110** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
9111** <dd>This parameter returns the number malloc attempts that might have
9112** been satisfied using lookaside memory but failed due to the amount of
9113** memory requested being larger than the lookaside slot size.
9114** Only the high-water value is meaningful;
9115** the current value is always zero.)^
9116**
9117** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
9118** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
9119** <dd>This parameter returns the number malloc attempts that might have
9120** been satisfied using lookaside memory but failed due to all lookaside
9121** memory already being in use.
9122** Only the high-water value is meaningful;
9123** the current value is always zero.)^
9124**
9125** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
9126** <dd>This parameter returns the approximate number of bytes of heap
9127** memory used by all pager caches associated with the database connection.)^
9128** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
9129**
9130** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
9131** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
9132** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
9133** pager cache is shared between two or more connections the bytes of heap
9134** memory used by that pager cache is divided evenly between the attached
9135** connections.)^ In other words, if none of the pager caches associated
9136** with the database connection are shared, this request returns the same
9137** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
9138** shared, the value returned by this call will be smaller than that returned
9139** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
9140** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
9141**
9142** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
9143** <dd>This parameter returns the approximate number of bytes of heap
9144** memory used to store the schema for all databases associated
9145** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9146** ^The full amount of memory used by the schemas is reported, even if the
9147** schema memory is shared with other database connections due to
9148** [shared cache mode] being enabled.
9149** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
9150**
9151** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
9152** <dd>This parameter returns the approximate number of bytes of heap
9153** and lookaside memory used by all prepared statements associated with
9154** the database connection.)^
9155** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
9156** </dd>
9157**
9158** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
9159** <dd>This parameter returns the number of pager cache hits that have
9160** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
9161** is always 0.
9162** </dd>
9163**
9164** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
9165** <dd>This parameter returns the number of pager cache misses that have
9166** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
9167** is always 0.
9168** </dd>
9169**
9170** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
9171** <dd>This parameter returns the number of dirty cache entries that have
9172** been written to disk. Specifically, the number of pages written to the
9173** wal file in wal mode databases, or the number of pages written to the
9174** database file in rollback mode databases. Any pages written as part of
9175** transaction rollback or database recovery operations are not included.
9176** If an IO or other error occurs while writing a page to disk, the effect
9177** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
9178** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
9179** </dd>
9180**
9181** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
9182** <dd>This parameter returns the number of dirty cache entries that have
9183** been written to disk in the middle of a transaction due to the page
9184** cache overflowing. Transactions are more efficient if they are written
9185** to disk all at once. When pages spill mid-transaction, that introduces
9186** additional overhead. This parameter can be used help identify
9187** inefficiencies that can be resolved by increasing the cache size.
9188** </dd>
9189**
9190** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
9191** <dd>This parameter returns zero for the current value if and only if
9192** all foreign key constraints (deferred or immediate) have been
9193** resolved.)^ ^The highwater mark is always 0.
9194** </dd>
9195** </dl>
9196*/
9197#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
9198#define SQLITE_DBSTATUS_CACHE_USED 1
9199#define SQLITE_DBSTATUS_SCHEMA_USED 2
9200#define SQLITE_DBSTATUS_STMT_USED 3
9201#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
9202#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
9203#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
9204#define SQLITE_DBSTATUS_CACHE_HIT 7
9205#define SQLITE_DBSTATUS_CACHE_MISS 8
9206#define SQLITE_DBSTATUS_CACHE_WRITE 9
9207#define SQLITE_DBSTATUS_DEFERRED_FKS 10
9208#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
9209#define SQLITE_DBSTATUS_CACHE_SPILL 12
9210#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
9211
9212
9213/*
9214** CAPI3REF: Prepared Statement Status
9215** METHOD: sqlite3_stmt
9216**
9217** ^(Each prepared statement maintains various
9218** [SQLITE_STMTSTATUS counters] that measure the number
9219** of times it has performed specific operations.)^ These counters can
9220** be used to monitor the performance characteristics of the prepared
9221** statements. For example, if the number of table steps greatly exceeds
9222** the number of table searches or result rows, that would tend to indicate
9223** that the prepared statement is using a full table scan rather than
9224** an index.
9225**
9226** ^(This interface is used to retrieve and reset counter values from
9227** a [prepared statement]. The first argument is the prepared statement
9228** object to be interrogated. The second argument
9229** is an integer code for a specific [SQLITE_STMTSTATUS counter]
9230** to be interrogated.)^
9231** ^The current value of the requested counter is returned.
9232** ^If the resetFlg is true, then the counter is reset to zero after this
9233** interface call returns.
9234**
9235** See also: [sqlite3_status()] and [sqlite3_db_status()].
9236*/
9237SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9238
9239/*
9240** CAPI3REF: Status Parameters for prepared statements
9241** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
9242**
9243** These preprocessor macros define integer codes that name counter
9244** values associated with the [sqlite3_stmt_status()] interface.
9245** The meanings of the various counters are as follows:
9246**
9247** <dl>
9248** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
9249** <dd>^This is the number of times that SQLite has stepped forward in
9250** a table as part of a full table scan. Large numbers for this counter
9251** may indicate opportunities for performance improvement through
9252** careful use of indices.</dd>
9253**
9254** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
9255** <dd>^This is the number of sort operations that have occurred.
9256** A non-zero value in this counter may indicate an opportunity to
9257** improvement performance through careful use of indices.</dd>
9258**
9259** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
9260** <dd>^This is the number of rows inserted into transient indices that
9261** were created automatically in order to help joins run faster.
9262** A non-zero value in this counter may indicate an opportunity to
9263** improvement performance by adding permanent indices that do not
9264** need to be reinitialized each time the statement is run.</dd>
9265**
9266** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
9267** <dd>^This is the number of virtual machine operations executed
9268** by the prepared statement if that number is less than or equal
9269** to 2147483647. The number of virtual machine operations can be
9270** used as a proxy for the total work done by the prepared statement.
9271** If the number of virtual machine operations exceeds 2147483647
9272** then the value returned by this statement status code is undefined.
9273**
9274** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
9275** <dd>^This is the number of times that the prepare statement has been
9276** automatically regenerated due to schema changes or changes to
9277** [bound parameters] that might affect the query plan.
9278**
9279** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
9280** <dd>^This is the number of times that the prepared statement has
9281** been run. A single "run" for the purposes of this counter is one
9282** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
9283** The counter is incremented on the first [sqlite3_step()] call of each
9284** cycle.
9285**
9286** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
9287** <dd>^This is the approximate number of bytes of heap memory
9288** used to store the prepared statement. ^This value is not actually
9289** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
9290** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
9291** </dd>
9292** </dl>
9293*/
9294#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
9295#define SQLITE_STMTSTATUS_SORT 2
9296#define SQLITE_STMTSTATUS_AUTOINDEX 3
9297#define SQLITE_STMTSTATUS_VM_STEP 4
9298#define SQLITE_STMTSTATUS_REPREPARE 5
9299#define SQLITE_STMTSTATUS_RUN 6
9300#define SQLITE_STMTSTATUS_MEMUSED 99
9301
9302/*
9303** CAPI3REF: Custom Page Cache Object
9304**
9305** The sqlite3_pcache type is opaque. It is implemented by
9306** the pluggable module. The SQLite core has no knowledge of
9307** its size or internal structure and never deals with the
9308** sqlite3_pcache object except by holding and passing pointers
9309** to the object.
9310**
9311** See [sqlite3_pcache_methods2] for additional information.
9312*/
9313typedef struct sqlite3_pcache sqlite3_pcache;
9314
9315/*
9316** CAPI3REF: Custom Page Cache Object
9317**
9318** The sqlite3_pcache_page object represents a single page in the
9319** page cache. The page cache will allocate instances of this
9320** object. Various methods of the page cache use pointers to instances
9321** of this object as parameters or as their return value.
9322**
9323** See [sqlite3_pcache_methods2] for additional information.
9324*/
9325typedef struct sqlite3_pcache_page sqlite3_pcache_page;
9326struct sqlite3_pcache_page {
9327 void *pBuf; /* The content of the page */
9328 void *pExtra; /* Extra information associated with the page */
9329};
9330
9331/*
9332** CAPI3REF: Application Defined Page Cache.
9333** KEYWORDS: {page cache}
9334**
9335** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
9336** register an alternative page cache implementation by passing in an
9337** instance of the sqlite3_pcache_methods2 structure.)^
9338** In many applications, most of the heap memory allocated by
9339** SQLite is used for the page cache.
9340** By implementing a
9341** custom page cache using this API, an application can better control
9342** the amount of memory consumed by SQLite, the way in which
9343** that memory is allocated and released, and the policies used to
9344** determine exactly which parts of a database file are cached and for
9345** how long.
9346**
9347** The alternative page cache mechanism is an
9348** extreme measure that is only needed by the most demanding applications.
9349** The built-in page cache is recommended for most uses.
9350**
9351** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
9352** internal buffer by SQLite within the call to [sqlite3_config]. Hence
9353** the application may discard the parameter after the call to
9354** [sqlite3_config()] returns.)^
9355**
9356** [[the xInit() page cache method]]
9357** ^(The xInit() method is called once for each effective
9358** call to [sqlite3_initialize()])^
9359** (usually only once during the lifetime of the process). ^(The xInit()
9360** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9361** The intent of the xInit() method is to set up global data structures
9362** required by the custom page cache implementation.
9363** ^(If the xInit() method is NULL, then the
9364** built-in default page cache is used instead of the application defined
9365** page cache.)^
9366**
9367** [[the xShutdown() page cache method]]
9368** ^The xShutdown() method is called by [sqlite3_shutdown()].
9369** It can be used to clean up
9370** any outstanding resources before process shutdown, if required.
9371** ^The xShutdown() method may be NULL.
9372**
9373** ^SQLite automatically serializes calls to the xInit method,
9374** so the xInit method need not be threadsafe. ^The
9375** xShutdown method is only called from [sqlite3_shutdown()] so it does
9376** not need to be threadsafe either. All other methods must be threadsafe
9377** in multithreaded applications.
9378**
9379** ^SQLite will never invoke xInit() more than once without an intervening
9380** call to xShutdown().
9381**
9382** [[the xCreate() page cache methods]]
9383** ^SQLite invokes the xCreate() method to construct a new cache instance.
9384** SQLite will typically create one cache instance for each open database file,
9385** though this is not guaranteed. ^The
9386** first parameter, szPage, is the size in bytes of the pages that must
9387** be allocated by the cache. ^szPage will always a power of two. ^The
9388** second parameter szExtra is a number of bytes of extra storage
9389** associated with each page cache entry. ^The szExtra parameter will
9390** a number less than 250. SQLite will use the
9391** extra szExtra bytes on each page to store metadata about the underlying
9392** database page on disk. The value passed into szExtra depends
9393** on the SQLite version, the target platform, and how SQLite was compiled.
9394** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9395** created will be used to cache database pages of a file stored on disk, or
9396** false if it is used for an in-memory database. The cache implementation
9397** does not have to do anything special based with the value of bPurgeable;
9398** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
9399** never invoke xUnpin() except to deliberately delete a page.
9400** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9401** false will always have the "discard" flag set to true.
9402** ^Hence, a cache created with bPurgeable false will
9403** never contain any unpinned pages.
9404**
9405** [[the xCachesize() page cache method]]
9406** ^(The xCachesize() method may be called at any time by SQLite to set the
9407** suggested maximum cache-size (number of pages stored by) the cache
9408** instance passed as the first argument. This is the value configured using
9409** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
9410** parameter, the implementation is not required to do anything with this
9411** value; it is advisory only.
9412**
9413** [[the xPagecount() page cache methods]]
9414** The xPagecount() method must return the number of pages currently
9415** stored in the cache, both pinned and unpinned.
9416**
9417** [[the xFetch() page cache methods]]
9418** The xFetch() method locates a page in the cache and returns a pointer to
9419** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9420** The pBuf element of the returned sqlite3_pcache_page object will be a
9421** pointer to a buffer of szPage bytes used to store the content of a
9422** single database page. The pExtra element of sqlite3_pcache_page will be
9423** a pointer to the szExtra bytes of extra storage that SQLite has requested
9424** for each entry in the page cache.
9425**
9426** The page to be fetched is determined by the key. ^The minimum key value
9427** is 1. After it has been retrieved using xFetch, the page is considered
9428** to be "pinned".
9429**
9430** If the requested page is already in the page cache, then the page cache
9431** implementation must return a pointer to the page buffer with its content
9432** intact. If the requested page is not already in the cache, then the
9433** cache implementation should use the value of the createFlag
9434** parameter to help it determined what action to take:
9435**
9436** <table border=1 width=85% align=center>
9437** <tr><th> createFlag <th> Behavior when page is not already in cache
9438** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9439** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9440** Otherwise return NULL.
9441** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9442** NULL if allocating a new page is effectively impossible.
9443** </table>
9444**
9445** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9446** will only use a createFlag of 2 after a prior call with a createFlag of 1
9447** failed.)^ In between the xFetch() calls, SQLite may
9448** attempt to unpin one or more cache pages by spilling the content of
9449** pinned pages to disk and synching the operating system disk cache.
9450**
9451** [[the xUnpin() page cache method]]
9452** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9453** as its second argument. If the third parameter, discard, is non-zero,
9454** then the page must be evicted from the cache.
9455** ^If the discard parameter is
9456** zero, then the page may be discarded or retained at the discretion of
9457** page cache implementation. ^The page cache implementation
9458** may choose to evict unpinned pages at any time.
9459**
9460** The cache must not perform any reference counting. A single
9461** call to xUnpin() unpins the page regardless of the number of prior calls
9462** to xFetch().
9463**
9464** [[the xRekey() page cache methods]]
9465** The xRekey() method is used to change the key value associated with the
9466** page passed as the second argument. If the cache
9467** previously contains an entry associated with newKey, it must be
9468** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9469** to be pinned.
9470**
9471** When SQLite calls the xTruncate() method, the cache must discard all
9472** existing cache entries with page numbers (keys) greater than or equal
9473** to the value of the iLimit parameter passed to xTruncate(). If any
9474** of these pages are pinned, they are implicitly unpinned, meaning that
9475** they can be safely discarded.
9476**
9477** [[the xDestroy() page cache method]]
9478** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9479** All resources associated with the specified cache should be freed. ^After
9480** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9481** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9482** functions.
9483**
9484** [[the xShrink() page cache method]]
9485** ^SQLite invokes the xShrink() method when it wants the page cache to
9486** free up as much of heap memory as possible. The page cache implementation
9487** is not obligated to free any memory, but well-behaved implementations should
9488** do their best.
9489*/
9490typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9491struct sqlite3_pcache_methods2 {
9492 int iVersion;
9493 void *pArg;
9494 int (*xInit)(void*);
9495 void (*xShutdown)(void*);
9496 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9497 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9498 int (*xPagecount)(sqlite3_pcache*);
9499 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9500 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9501 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9502 unsigned oldKey, unsigned newKey);
9503 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9504 void (*xDestroy)(sqlite3_pcache*);
9505 void (*xShrink)(sqlite3_pcache*);
9506};
9507
9508/*
9509** This is the obsolete pcache_methods object that has now been replaced
9510** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9511** retained in the header file for backwards compatibility only.
9512*/
9513typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9514struct sqlite3_pcache_methods {
9515 void *pArg;
9516 int (*xInit)(void*);
9517 void (*xShutdown)(void*);
9518 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9519 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9520 int (*xPagecount)(sqlite3_pcache*);
9521 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9522 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9523 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9524 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9525 void (*xDestroy)(sqlite3_pcache*);
9526};
9527
9528
9529/*
9530** CAPI3REF: Online Backup Object
9531**
9532** The sqlite3_backup object records state information about an ongoing
9533** online backup operation. ^The sqlite3_backup object is created by
9534** a call to [sqlite3_backup_init()] and is destroyed by a call to
9535** [sqlite3_backup_finish()].
9536**
9537** See Also: [Using the SQLite Online Backup API]
9538*/
9539typedef struct sqlite3_backup sqlite3_backup;
9540
9541/*
9542** CAPI3REF: Online Backup API.
9543**
9544** The backup API copies the content of one database into another.
9545** It is useful either for creating backups of databases or
9546** for copying in-memory databases to or from persistent files.
9547**
9548** See Also: [Using the SQLite Online Backup API]
9549**
9550** ^SQLite holds a write transaction open on the destination database file
9551** for the duration of the backup operation.
9552** ^The source database is read-locked only while it is being read;
9553** it is not locked continuously for the entire backup operation.
9554** ^Thus, the backup may be performed on a live source database without
9555** preventing other database connections from
9556** reading or writing to the source database while the backup is underway.
9557**
9558** ^(To perform a backup operation:
9559** <ol>
9560** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9561** backup,
9562** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9563** the data between the two databases, and finally
9564** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9565** associated with the backup operation.
9566** </ol>)^
9567** There should be exactly one call to sqlite3_backup_finish() for each
9568** successful call to sqlite3_backup_init().
9569**
9570** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9571**
9572** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9573** [database connection] associated with the destination database
9574** and the database name, respectively.
9575** ^The database name is "main" for the main database, "temp" for the
9576** temporary database, or the name specified after the AS keyword in
9577** an [ATTACH] statement for an attached database.
9578** ^The S and M arguments passed to
9579** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9580** and database name of the source database, respectively.
9581** ^The source and destination [database connections] (parameters S and D)
9582** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9583** an error.
9584**
9585** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9586** there is already a read or read-write transaction open on the
9587** destination database.
9588**
9589** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9590** returned and an error code and error message are stored in the
9591** destination [database connection] D.
9592** ^The error code and message for the failed call to sqlite3_backup_init()
9593** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9594** [sqlite3_errmsg16()] functions.
9595** ^A successful call to sqlite3_backup_init() returns a pointer to an
9596** [sqlite3_backup] object.
9597** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9598** sqlite3_backup_finish() functions to perform the specified backup
9599** operation.
9600**
9601** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9602**
9603** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9604** the source and destination databases specified by [sqlite3_backup] object B.
9605** ^If N is negative, all remaining source pages are copied.
9606** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9607** are still more pages to be copied, then the function returns [SQLITE_OK].
9608** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9609** from source to destination, then it returns [SQLITE_DONE].
9610** ^If an error occurs while running sqlite3_backup_step(B,N),
9611** then an [error code] is returned. ^As well as [SQLITE_OK] and
9612** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9613** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9614** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9615**
9616** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9617** <ol>
9618** <li> the destination database was opened read-only, or
9619** <li> the destination database is using write-ahead-log journaling
9620** and the destination and source page sizes differ, or
9621** <li> the destination database is an in-memory database and the
9622** destination and source page sizes differ.
9623** </ol>)^
9624**
9625** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9626** the [sqlite3_busy_handler | busy-handler function]
9627** is invoked (if one is specified). ^If the
9628** busy-handler returns non-zero before the lock is available, then
9629** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9630** sqlite3_backup_step() can be retried later. ^If the source
9631** [database connection]
9632** is being used to write to the source database when sqlite3_backup_step()
9633** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9634** case the call to sqlite3_backup_step() can be retried later on. ^(If
9635** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9636** [SQLITE_READONLY] is returned, then
9637** there is no point in retrying the call to sqlite3_backup_step(). These
9638** errors are considered fatal.)^ The application must accept
9639** that the backup operation has failed and pass the backup operation handle
9640** to the sqlite3_backup_finish() to release associated resources.
9641**
9642** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9643** on the destination file. ^The exclusive lock is not released until either
9644** sqlite3_backup_finish() is called or the backup operation is complete
9645** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9646** sqlite3_backup_step() obtains a [shared lock] on the source database that
9647** lasts for the duration of the sqlite3_backup_step() call.
9648** ^Because the source database is not locked between calls to
9649** sqlite3_backup_step(), the source database may be modified mid-way
9650** through the backup process. ^If the source database is modified by an
9651** external process or via a database connection other than the one being
9652** used by the backup operation, then the backup will be automatically
9653** restarted by the next call to sqlite3_backup_step(). ^If the source
9654** database is modified by the using the same database connection as is used
9655** by the backup operation, then the backup database is automatically
9656** updated at the same time.
9657**
9658** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9659**
9660** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9661** application wishes to abandon the backup operation, the application
9662** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9663** ^The sqlite3_backup_finish() interfaces releases all
9664** resources associated with the [sqlite3_backup] object.
9665** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9666** active write-transaction on the destination database is rolled back.
9667** The [sqlite3_backup] object is invalid
9668** and may not be used following a call to sqlite3_backup_finish().
9669**
9670** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9671** sqlite3_backup_step() errors occurred, regardless or whether or not
9672** sqlite3_backup_step() completed.
9673** ^If an out-of-memory condition or IO error occurred during any prior
9674** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9675** sqlite3_backup_finish() returns the corresponding [error code].
9676**
9677** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9678** is not a permanent error and does not affect the return value of
9679** sqlite3_backup_finish().
9680**
9681** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9682** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9683**
9684** ^The sqlite3_backup_remaining() routine returns the number of pages still
9685** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9686** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9687** in the source database at the conclusion of the most recent
9688** sqlite3_backup_step().
9689** ^(The values returned by these functions are only updated by
9690** sqlite3_backup_step(). If the source database is modified in a way that
9691** changes the size of the source database or the number of pages remaining,
9692** those changes are not reflected in the output of sqlite3_backup_pagecount()
9693** and sqlite3_backup_remaining() until after the next
9694** sqlite3_backup_step().)^
9695**
9696** <b>Concurrent Usage of Database Handles</b>
9697**
9698** ^The source [database connection] may be used by the application for other
9699** purposes while a backup operation is underway or being initialized.
9700** ^If SQLite is compiled and configured to support threadsafe database
9701** connections, then the source database connection may be used concurrently
9702** from within other threads.
9703**
9704** However, the application must guarantee that the destination
9705** [database connection] is not passed to any other API (by any thread) after
9706** sqlite3_backup_init() is called and before the corresponding call to
9707** sqlite3_backup_finish(). SQLite does not currently check to see
9708** if the application incorrectly accesses the destination [database connection]
9709** and so no error code is reported, but the operations may malfunction
9710** nevertheless. Use of the destination database connection while a
9711** backup is in progress might also also cause a mutex deadlock.
9712**
9713** If running in [shared cache mode], the application must
9714** guarantee that the shared cache used by the destination database
9715** is not accessed while the backup is running. In practice this means
9716** that the application must guarantee that the disk file being
9717** backed up to is not accessed by any connection within the process,
9718** not just the specific connection that was passed to sqlite3_backup_init().
9719**
9720** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9721** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9722** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9723** APIs are not strictly speaking threadsafe. If they are invoked at the
9724** same time as another thread is invoking sqlite3_backup_step() it is
9725** possible that they return invalid values.
9726*/
9727SQLITE_API sqlite3_backup *sqlite3_backup_init(
9728 sqlite3 *pDest, /* Destination database handle */
9729 const char *zDestName, /* Destination database name */
9730 sqlite3 *pSource, /* Source database handle */
9731 const char *zSourceName /* Source database name */
9732);
9733SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9734SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9735SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9736SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9737
9738/*
9739** CAPI3REF: Unlock Notification
9740** METHOD: sqlite3
9741**
9742** ^When running in shared-cache mode, a database operation may fail with
9743** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9744** individual tables within the shared-cache cannot be obtained. See
9745** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9746** ^This API may be used to register a callback that SQLite will invoke
9747** when the connection currently holding the required lock relinquishes it.
9748** ^This API is only available if the library was compiled with the
9749** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9750**
9751** See Also: [Using the SQLite Unlock Notification Feature].
9752**
9753** ^Shared-cache locks are released when a database connection concludes
9754** its current transaction, either by committing it or rolling it back.
9755**
9756** ^When a connection (known as the blocked connection) fails to obtain a
9757** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9758** identity of the database connection (the blocking connection) that
9759** has locked the required resource is stored internally. ^After an
9760** application receives an SQLITE_LOCKED error, it may call the
9761** sqlite3_unlock_notify() method with the blocked connection handle as
9762** the first argument to register for a callback that will be invoked
9763** when the blocking connections current transaction is concluded. ^The
9764** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9765** call that concludes the blocking connection's transaction.
9766**
9767** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9768** there is a chance that the blocking connection will have already
9769** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9770** If this happens, then the specified callback is invoked immediately,
9771** from within the call to sqlite3_unlock_notify().)^
9772**
9773** ^If the blocked connection is attempting to obtain a write-lock on a
9774** shared-cache table, and more than one other connection currently holds
9775** a read-lock on the same table, then SQLite arbitrarily selects one of
9776** the other connections to use as the blocking connection.
9777**
9778** ^(There may be at most one unlock-notify callback registered by a
9779** blocked connection. If sqlite3_unlock_notify() is called when the
9780** blocked connection already has a registered unlock-notify callback,
9781** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9782** called with a NULL pointer as its second argument, then any existing
9783** unlock-notify callback is canceled. ^The blocked connections
9784** unlock-notify callback may also be canceled by closing the blocked
9785** connection using [sqlite3_close()].
9786**
9787** The unlock-notify callback is not reentrant. If an application invokes
9788** any sqlite3_xxx API functions from within an unlock-notify callback, a
9789** crash or deadlock may be the result.
9790**
9791** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9792** returns SQLITE_OK.
9793**
9794** <b>Callback Invocation Details</b>
9795**
9796** When an unlock-notify callback is registered, the application provides a
9797** single void* pointer that is passed to the callback when it is invoked.
9798** However, the signature of the callback function allows SQLite to pass
9799** it an array of void* context pointers. The first argument passed to
9800** an unlock-notify callback is a pointer to an array of void* pointers,
9801** and the second is the number of entries in the array.
9802**
9803** When a blocking connection's transaction is concluded, there may be
9804** more than one blocked connection that has registered for an unlock-notify
9805** callback. ^If two or more such blocked connections have specified the
9806** same callback function, then instead of invoking the callback function
9807** multiple times, it is invoked once with the set of void* context pointers
9808** specified by the blocked connections bundled together into an array.
9809** This gives the application an opportunity to prioritize any actions
9810** related to the set of unblocked database connections.
9811**
9812** <b>Deadlock Detection</b>
9813**
9814** Assuming that after registering for an unlock-notify callback a
9815** database waits for the callback to be issued before taking any further
9816** action (a reasonable assumption), then using this API may cause the
9817** application to deadlock. For example, if connection X is waiting for
9818** connection Y's transaction to be concluded, and similarly connection
9819** Y is waiting on connection X's transaction, then neither connection
9820** will proceed and the system may remain deadlocked indefinitely.
9821**
9822** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9823** detection. ^If a given call to sqlite3_unlock_notify() would put the
9824** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9825** unlock-notify callback is registered. The system is said to be in
9826** a deadlocked state if connection A has registered for an unlock-notify
9827** callback on the conclusion of connection B's transaction, and connection
9828** B has itself registered for an unlock-notify callback when connection
9829** A's transaction is concluded. ^Indirect deadlock is also detected, so
9830** the system is also considered to be deadlocked if connection B has
9831** registered for an unlock-notify callback on the conclusion of connection
9832** C's transaction, where connection C is waiting on connection A. ^Any
9833** number of levels of indirection are allowed.
9834**
9835** <b>The "DROP TABLE" Exception</b>
9836**
9837** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9838** always appropriate to call sqlite3_unlock_notify(). There is however,
9839** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9840** SQLite checks if there are any currently executing SELECT statements
9841** that belong to the same connection. If there are, SQLITE_LOCKED is
9842** returned. In this case there is no "blocking connection", so invoking
9843** sqlite3_unlock_notify() results in the unlock-notify callback being
9844** invoked immediately. If the application then re-attempts the "DROP TABLE"
9845** or "DROP INDEX" query, an infinite loop might be the result.
9846**
9847** One way around this problem is to check the extended error code returned
9848** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9849** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9850** the special "DROP TABLE/INDEX" case, the extended error code is just
9851** SQLITE_LOCKED.)^
9852*/
9853SQLITE_API int sqlite3_unlock_notify(
9854 sqlite3 *pBlocked, /* Waiting connection */
9855 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9856 void *pNotifyArg /* Argument to pass to xNotify */
9857);
9858
9859
9860/*
9861** CAPI3REF: String Comparison
9862**
9863** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9864** and extensions to compare the contents of two buffers containing UTF-8
9865** strings in a case-independent fashion, using the same definition of "case
9866** independence" that SQLite uses internally when comparing identifiers.
9867*/
9868SQLITE_API int sqlite3_stricmp(const char *, const char *);
9869SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9870
9871/*
9872** CAPI3REF: String Globbing
9873*
9874** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9875** string X matches the [GLOB] pattern P.
9876** ^The definition of [GLOB] pattern matching used in
9877** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9878** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9879** is case sensitive.
9880**
9881** Note that this routine returns zero on a match and non-zero if the strings
9882** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9883**
9884** See also: [sqlite3_strlike()].
9885*/
9886SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9887
9888/*
9889** CAPI3REF: String LIKE Matching
9890*
9891** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9892** string X matches the [LIKE] pattern P with escape character E.
9893** ^The definition of [LIKE] pattern matching used in
9894** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9895** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9896** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9897** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9898** insensitive - equivalent upper and lower case ASCII characters match
9899** one another.
9900**
9901** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9902** only ASCII characters are case folded.
9903**
9904** Note that this routine returns zero on a match and non-zero if the strings
9905** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9906**
9907** See also: [sqlite3_strglob()].
9908*/
9909SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9910
9911/*
9912** CAPI3REF: Error Logging Interface
9913**
9914** ^The [sqlite3_log()] interface writes a message into the [error log]
9915** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9916** ^If logging is enabled, the zFormat string and subsequent arguments are
9917** used with [sqlite3_snprintf()] to generate the final output string.
9918**
9919** The sqlite3_log() interface is intended for use by extensions such as
9920** virtual tables, collating functions, and SQL functions. While there is
9921** nothing to prevent an application from calling sqlite3_log(), doing so
9922** is considered bad form.
9923**
9924** The zFormat string must not be NULL.
9925**
9926** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9927** will not use dynamically allocated memory. The log message is stored in
9928** a fixed-length buffer on the stack. If the log message is longer than
9929** a few hundred characters, it will be truncated to the length of the
9930** buffer.
9931*/
9932SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9933
9934/*
9935** CAPI3REF: Write-Ahead Log Commit Hook
9936** METHOD: sqlite3
9937**
9938** ^The [sqlite3_wal_hook()] function is used to register a callback that
9939** is invoked each time data is committed to a database in wal mode.
9940**
9941** ^(The callback is invoked by SQLite after the commit has taken place and
9942** the associated write-lock on the database released)^, so the implementation
9943** may read, write or [checkpoint] the database as required.
9944**
9945** ^The first parameter passed to the callback function when it is invoked
9946** is a copy of the third parameter passed to sqlite3_wal_hook() when
9947** registering the callback. ^The second is a copy of the database handle.
9948** ^The third parameter is the name of the database that was written to -
9949** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9950** is the number of pages currently in the write-ahead log file,
9951** including those that were just committed.
9952**
9953** The callback function should normally return [SQLITE_OK]. ^If an error
9954** code is returned, that error will propagate back up through the
9955** SQLite code base to cause the statement that provoked the callback
9956** to report an error, though the commit will have still occurred. If the
9957** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9958** that does not correspond to any valid SQLite error code, the results
9959** are undefined.
9960**
9961** A single database handle may have at most a single write-ahead log callback
9962** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9963** previously registered write-ahead log callback. ^Note that the
9964** [sqlite3_wal_autocheckpoint()] interface and the
9965** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9966** overwrite any prior [sqlite3_wal_hook()] settings.
9967*/
9968SQLITE_API void *sqlite3_wal_hook(
9969 sqlite3*,
9970 int(*)(void *,sqlite3*,const char*,int),
9971 void*
9972);
9973
9974/*
9975** CAPI3REF: Configure an auto-checkpoint
9976** METHOD: sqlite3
9977**
9978** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9979** [sqlite3_wal_hook()] that causes any database on [database connection] D
9980** to automatically [checkpoint]
9981** after committing a transaction if there are N or
9982** more frames in the [write-ahead log] file. ^Passing zero or
9983** a negative value as the nFrame parameter disables automatic
9984** checkpoints entirely.
9985**
9986** ^The callback registered by this function replaces any existing callback
9987** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9988** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9989** configured by this function.
9990**
9991** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9992** from SQL.
9993**
9994** ^Checkpoints initiated by this mechanism are
9995** [sqlite3_wal_checkpoint_v2|PASSIVE].
9996**
9997** ^Every new [database connection] defaults to having the auto-checkpoint
9998** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9999** pages. The use of this interface
10000** is only necessary if the default setting is found to be suboptimal
10001** for a particular application.
10002*/
10003SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
10004
10005/*
10006** CAPI3REF: Checkpoint a database
10007** METHOD: sqlite3
10008**
10009** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
10010** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
10011**
10012** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
10013** [write-ahead log] for database X on [database connection] D to be
10014** transferred into the database file and for the write-ahead log to
10015** be reset. See the [checkpointing] documentation for addition
10016** information.
10017**
10018** This interface used to be the only way to cause a checkpoint to
10019** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
10020** interface was added. This interface is retained for backwards
10021** compatibility and as a convenience for applications that need to manually
10022** start a callback but which do not need the full power (and corresponding
10023** complication) of [sqlite3_wal_checkpoint_v2()].
10024*/
10025SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
10026
10027/*
10028** CAPI3REF: Checkpoint a database
10029** METHOD: sqlite3
10030**
10031** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
10032** operation on database X of [database connection] D in mode M. Status
10033** information is written back into integers pointed to by L and C.)^
10034** ^(The M parameter must be a valid [checkpoint mode]:)^
10035**
10036** <dl>
10037** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
10038** ^Checkpoint as many frames as possible without waiting for any database
10039** readers or writers to finish, then sync the database file if all frames
10040** in the log were checkpointed. ^The [busy-handler callback]
10041** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
10042** ^On the other hand, passive mode might leave the checkpoint unfinished
10043** if there are concurrent readers or writers.
10044**
10045** <dt>SQLITE_CHECKPOINT_FULL<dd>
10046** ^This mode blocks (it invokes the
10047** [sqlite3_busy_handler|busy-handler callback]) until there is no
10048** database writer and all readers are reading from the most recent database
10049** snapshot. ^It then checkpoints all frames in the log file and syncs the
10050** database file. ^This mode blocks new database writers while it is pending,
10051** but new database readers are allowed to continue unimpeded.
10052**
10053** <dt>SQLITE_CHECKPOINT_RESTART<dd>
10054** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
10055** that after checkpointing the log file it blocks (calls the
10056** [busy-handler callback])
10057** until all readers are reading from the database file only. ^This ensures
10058** that the next writer will restart the log file from the beginning.
10059** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
10060** database writer attempts while it is pending, but does not impede readers.
10061**
10062** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
10063** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
10064** addition that it also truncates the log file to zero bytes just prior
10065** to a successful return.
10066** </dl>
10067**
10068** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
10069** the log file or to -1 if the checkpoint could not run because
10070** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
10071** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
10072** log file (including any that were already checkpointed before the function
10073** was called) or to -1 if the checkpoint could not run due to an error or
10074** because the database is not in WAL mode. ^Note that upon successful
10075** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
10076** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
10077**
10078** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
10079** any other process is running a checkpoint operation at the same time, the
10080** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
10081** busy-handler configured, it will not be invoked in this case.
10082**
10083** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
10084** exclusive "writer" lock on the database file. ^If the writer lock cannot be
10085** obtained immediately, and a busy-handler is configured, it is invoked and
10086** the writer lock retried until either the busy-handler returns 0 or the lock
10087** is successfully obtained. ^The busy-handler is also invoked while waiting for
10088** database readers as described above. ^If the busy-handler returns 0 before
10089** the writer lock is obtained or while waiting for database readers, the
10090** checkpoint operation proceeds from that point in the same way as
10091** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10092** without blocking any further. ^SQLITE_BUSY is returned in this case.
10093**
10094** ^If parameter zDb is NULL or points to a zero length string, then the
10095** specified operation is attempted on all WAL databases [attached] to
10096** [database connection] db. In this case the
10097** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
10098** an SQLITE_BUSY error is encountered when processing one or more of the
10099** attached WAL databases, the operation is still attempted on any remaining
10100** attached databases and SQLITE_BUSY is returned at the end. ^If any other
10101** error occurs while processing an attached database, processing is abandoned
10102** and the error code is returned to the caller immediately. ^If no error
10103** (SQLITE_BUSY or otherwise) is encountered while processing the attached
10104** databases, SQLITE_OK is returned.
10105**
10106** ^If database zDb is the name of an attached database that is not in WAL
10107** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10108** zDb is not NULL (or a zero length string) and is not the name of any
10109** attached database, SQLITE_ERROR is returned to the caller.
10110**
10111** ^Unless it returns SQLITE_MISUSE,
10112** the sqlite3_wal_checkpoint_v2() interface
10113** sets the error information that is queried by
10114** [sqlite3_errcode()] and [sqlite3_errmsg()].
10115**
10116** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
10117** from SQL.
10118*/
10119SQLITE_API int sqlite3_wal_checkpoint_v2(
10120 sqlite3 *db, /* Database handle */
10121 const char *zDb, /* Name of attached database (or NULL) */
10122 int eMode, /* SQLITE_CHECKPOINT_* value */
10123 int *pnLog, /* OUT: Size of WAL log in frames */
10124 int *pnCkpt /* OUT: Total number of frames checkpointed */
10125);
10126
10127/*
10128** CAPI3REF: Checkpoint Mode Values
10129** KEYWORDS: {checkpoint mode}
10130**
10131** These constants define all valid values for the "checkpoint mode" passed
10132** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
10133** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
10134** meaning of each of these checkpoint modes.
10135*/
10136#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
10137#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
10138#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
10139#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
10140
10141/*
10142** CAPI3REF: Virtual Table Interface Configuration
10143**
10144** This function may be called by either the [xConnect] or [xCreate] method
10145** of a [virtual table] implementation to configure
10146** various facets of the virtual table interface.
10147**
10148** If this interface is invoked outside the context of an xConnect or
10149** xCreate virtual table method then the behavior is undefined.
10150**
10151** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
10152** [database connection] in which the virtual table is being created and
10153** which is passed in as the first argument to the [xConnect] or [xCreate]
10154** method that is invoking sqlite3_vtab_config(). The C parameter is one
10155** of the [virtual table configuration options]. The presence and meaning
10156** of parameters after C depend on which [virtual table configuration option]
10157** is used.
10158*/
10159SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10160
10161/*
10162** CAPI3REF: Virtual Table Configuration Options
10163** KEYWORDS: {virtual table configuration options}
10164** KEYWORDS: {virtual table configuration option}
10165**
10166** These macros define the various options to the
10167** [sqlite3_vtab_config()] interface that [virtual table] implementations
10168** can use to customize and optimize their behavior.
10169**
10170** <dl>
10171** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
10172** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
10173** <dd>Calls of the form
10174** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
10175** where X is an integer. If X is zero, then the [virtual table] whose
10176** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
10177** support constraints. In this configuration (which is the default) if
10178** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
10179** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
10180** specified as part of the users SQL statement, regardless of the actual
10181** ON CONFLICT mode specified.
10182**
10183** If X is non-zero, then the virtual table implementation guarantees
10184** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
10185** any modifications to internal or persistent data structures have been made.
10186** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
10187** is able to roll back a statement or database transaction, and abandon
10188** or continue processing the current SQL statement as appropriate.
10189** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
10190** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
10191** had been ABORT.
10192**
10193** Virtual table implementations that are required to handle OR REPLACE
10194** must do so within the [xUpdate] method. If a call to the
10195** [sqlite3_vtab_on_conflict()] function indicates that the current ON
10196** CONFLICT policy is REPLACE, the virtual table implementation should
10197** silently replace the appropriate rows within the xUpdate callback and
10198** return SQLITE_OK. Or, if this is not possible, it may return
10199** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
10200** constraint handling.
10201** </dd>
10202**
10203** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
10204** <dd>Calls of the form
10205** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
10206** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
10207** prohibits that virtual table from being used from within triggers and
10208** views.
10209** </dd>
10210**
10211** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
10212** <dd>Calls of the form
10213** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
10214** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
10215** identify that virtual table as being safe to use from within triggers
10216** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
10217** virtual table can do no serious harm even if it is controlled by a
10218** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
10219** flag unless absolutely necessary.
10220** </dd>
10221** </dl>
10222*/
10223#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
10224#define SQLITE_VTAB_INNOCUOUS 2
10225#define SQLITE_VTAB_DIRECTONLY 3
10226
10227/*
10228** CAPI3REF: Determine The Virtual Table Conflict Policy
10229**
10230** This function may only be called from within a call to the [xUpdate] method
10231** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
10232** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
10233** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
10234** of the SQL statement that triggered the call to the [xUpdate] method of the
10235** [virtual table].
10236*/
10237SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10238
10239/*
10240** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
10241**
10242** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
10243** method of a [virtual table], then it returns true if and only if the
10244** column is being fetched as part of an UPDATE operation during which the
10245** column value will not change. Applications might use this to substitute
10246** a return value that is less expensive to compute and that the corresponding
10247** [xUpdate] method understands as a "no-change" value.
10248**
10249** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
10250** the column is not changed by the UPDATE statement, then the xColumn
10251** method can optionally return without setting a result, without calling
10252** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
10253** In that case, [sqlite3_value_nochange(X)] will return true for the
10254** same column in the [xUpdate] method.
10255*/
10256SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10257
10258/*
10259** CAPI3REF: Determine The Collation For a Virtual Table Constraint
10260**
10261** This function may only be called from within a call to the [xBestIndex]
10262** method of a [virtual table].
10263**
10264** The first argument must be the sqlite3_index_info object that is the
10265** first parameter to the xBestIndex() method. The second argument must be
10266** an index into the aConstraint[] array belonging to the sqlite3_index_info
10267** structure passed to xBestIndex. This function returns a pointer to a buffer
10268** containing the name of the collation sequence for the corresponding
10269** constraint.
10270*/
10271SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10272
10273/*
10274** CAPI3REF: Conflict resolution modes
10275** KEYWORDS: {conflict resolution mode}
10276**
10277** These constants are returned by [sqlite3_vtab_on_conflict()] to
10278** inform a [virtual table] implementation what the [ON CONFLICT] mode
10279** is for the SQL statement being evaluated.
10280**
10281** Note that the [SQLITE_IGNORE] constant is also used as a potential
10282** return value from the [sqlite3_set_authorizer()] callback and that
10283** [SQLITE_ABORT] is also a [result code].
10284*/
10285#define SQLITE_ROLLBACK 1
10286/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10287#define SQLITE_FAIL 3
10288/* #define SQLITE_ABORT 4 // Also an error code */
10289#define SQLITE_REPLACE 5
10290
10291/*
10292** CAPI3REF: Prepared Statement Scan Status Opcodes
10293** KEYWORDS: {scanstatus options}
10294**
10295** The following constants can be used for the T parameter to the
10296** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
10297** different metric for sqlite3_stmt_scanstatus() to return.
10298**
10299** When the value returned to V is a string, space to hold that string is
10300** managed by the prepared statement S and will be automatically freed when
10301** S is finalized.
10302**
10303** <dl>
10304** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10305** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10306** set to the total number of times that the X-th loop has run.</dd>
10307**
10308** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10309** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10310** to the total number of rows examined by all iterations of the X-th loop.</dd>
10311**
10312** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10313** <dd>^The "double" variable pointed to by the V parameter will be set to the
10314** query planner's estimate for the average number of rows output from each
10315** iteration of the X-th loop. If the query planner's estimates was accurate,
10316** then this value will approximate the quotient NVISIT/NLOOP and the
10317** product of this value for all prior loops with the same SELECTID will
10318** be the NLOOP value for the current loop.
10319**
10320** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10321** <dd>^The "const char *" variable pointed to by the V parameter will be set
10322** to a zero-terminated UTF-8 string containing the name of the index or table
10323** used for the X-th loop.
10324**
10325** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10326** <dd>^The "const char *" variable pointed to by the V parameter will be set
10327** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10328** description for the X-th loop.
10329**
10330** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
10331** <dd>^The "int" variable pointed to by the V parameter will be set to the
10332** "select-id" for the X-th loop. The select-id identifies which query or
10333** subquery the loop is part of. The main query has a select-id of zero.
10334** The select-id is the same value as is output in the first column
10335** of an [EXPLAIN QUERY PLAN] query.
10336** </dl>
10337*/
10338#define SQLITE_SCANSTAT_NLOOP 0
10339#define SQLITE_SCANSTAT_NVISIT 1
10340#define SQLITE_SCANSTAT_EST 2
10341#define SQLITE_SCANSTAT_NAME 3
10342#define SQLITE_SCANSTAT_EXPLAIN 4
10343#define SQLITE_SCANSTAT_SELECTID 5
10344
10345/*
10346** CAPI3REF: Prepared Statement Scan Status
10347** METHOD: sqlite3_stmt
10348**
10349** This interface returns information about the predicted and measured
10350** performance for pStmt. Advanced applications can use this
10351** interface to compare the predicted and the measured performance and
10352** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10353**
10354** Since this interface is expected to be rarely used, it is only
10355** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10356** compile-time option.
10357**
10358** The "iScanStatusOp" parameter determines which status information to return.
10359** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10360** of this interface is undefined.
10361** ^The requested measurement is written into a variable pointed to by
10362** the "pOut" parameter.
10363** Parameter "idx" identifies the specific loop to retrieve statistics for.
10364** Loops are numbered starting from zero. ^If idx is out of range - less than
10365** zero or greater than or equal to the total number of loops used to implement
10366** the statement - a non-zero value is returned and the variable that pOut
10367** points to is unchanged.
10368**
10369** ^Statistics might not be available for all loops in all statements. ^In cases
10370** where there exist loops with no available statistics, this function behaves
10371** as if the loop did not exist - it returns non-zero and leave the variable
10372** that pOut points to unchanged.
10373**
10374** See also: [sqlite3_stmt_scanstatus_reset()]
10375*/
10376SQLITE_API int sqlite3_stmt_scanstatus(
10377 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10378 int idx, /* Index of loop to report on */
10379 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10380 void *pOut /* Result written here */
10381);
10382
10383/*
10384** CAPI3REF: Zero Scan-Status Counters
10385** METHOD: sqlite3_stmt
10386**
10387** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10388**
10389** This API is only available if the library is built with pre-processor
10390** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10391*/
10392SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10393
10394/*
10395** CAPI3REF: Flush caches to disk mid-transaction
10396**
10397** ^If a write-transaction is open on [database connection] D when the
10398** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10399** pages in the pager-cache that are not currently in use are written out
10400** to disk. A dirty page may be in use if a database cursor created by an
10401** active SQL statement is reading from it, or if it is page 1 of a database
10402** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10403** interface flushes caches for all schemas - "main", "temp", and
10404** any [attached] databases.
10405**
10406** ^If this function needs to obtain extra database locks before dirty pages
10407** can be flushed to disk, it does so. ^If those locks cannot be obtained
10408** immediately and there is a busy-handler callback configured, it is invoked
10409** in the usual manner. ^If the required lock still cannot be obtained, then
10410** the database is skipped and an attempt made to flush any dirty pages
10411** belonging to the next (if any) database. ^If any databases are skipped
10412** because locks cannot be obtained, but no other error occurs, this
10413** function returns SQLITE_BUSY.
10414**
10415** ^If any other error occurs while flushing dirty pages to disk (for
10416** example an IO error or out-of-memory condition), then processing is
10417** abandoned and an SQLite [error code] is returned to the caller immediately.
10418**
10419** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10420**
10421** ^This function does not set the database handle error code or message
10422** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10423*/
10424SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10425
10426/*
10427** CAPI3REF: The pre-update hook.
10428**
10429** ^These interfaces are only available if SQLite is compiled using the
10430** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10431**
10432** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10433** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10434** on a database table.
10435** ^At most one preupdate hook may be registered at a time on a single
10436** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10437** the previous setting.
10438** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10439** with a NULL pointer as the second parameter.
10440** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10441** the first parameter to callbacks.
10442**
10443** ^The preupdate hook only fires for changes to real database tables; the
10444** preupdate hook is not invoked for changes to [virtual tables] or to
10445** system tables like sqlite_sequence or sqlite_stat1.
10446**
10447** ^The second parameter to the preupdate callback is a pointer to
10448** the [database connection] that registered the preupdate hook.
10449** ^The third parameter to the preupdate callback is one of the constants
10450** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10451** kind of update operation that is about to occur.
10452** ^(The fourth parameter to the preupdate callback is the name of the
10453** database within the database connection that is being modified. This
10454** will be "main" for the main database or "temp" for TEMP tables or
10455** the name given after the AS keyword in the [ATTACH] statement for attached
10456** databases.)^
10457** ^The fifth parameter to the preupdate callback is the name of the
10458** table that is being modified.
10459**
10460** For an UPDATE or DELETE operation on a [rowid table], the sixth
10461** parameter passed to the preupdate callback is the initial [rowid] of the
10462** row being modified or deleted. For an INSERT operation on a rowid table,
10463** or any operation on a WITHOUT ROWID table, the value of the sixth
10464** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10465** seventh parameter is the final rowid value of the row being inserted
10466** or updated. The value of the seventh parameter passed to the callback
10467** function is not defined for operations on WITHOUT ROWID tables, or for
10468** INSERT operations on rowid tables.
10469**
10470** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10471** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10472** provide additional information about a preupdate event. These routines
10473** may only be called from within a preupdate callback. Invoking any of
10474** these routines from outside of a preupdate callback or with a
10475** [database connection] pointer that is different from the one supplied
10476** to the preupdate callback results in undefined and probably undesirable
10477** behavior.
10478**
10479** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10480** in the row that is being inserted, updated, or deleted.
10481**
10482** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10483** a [protected sqlite3_value] that contains the value of the Nth column of
10484** the table row before it is updated. The N parameter must be between 0
10485** and one less than the number of columns or the behavior will be
10486** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10487** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10488** behavior is undefined. The [sqlite3_value] that P points to
10489** will be destroyed when the preupdate callback returns.
10490**
10491** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10492** a [protected sqlite3_value] that contains the value of the Nth column of
10493** the table row after it is updated. The N parameter must be between 0
10494** and one less than the number of columns or the behavior will be
10495** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10496** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10497** behavior is undefined. The [sqlite3_value] that P points to
10498** will be destroyed when the preupdate callback returns.
10499**
10500** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10501** callback was invoked as a result of a direct insert, update, or delete
10502** operation; or 1 for inserts, updates, or deletes invoked by top-level
10503** triggers; or 2 for changes resulting from triggers called by top-level
10504** triggers; and so forth.
10505**
10506** See also: [sqlite3_update_hook()]
10507*/
10508#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10509SQLITE_API void *sqlite3_preupdate_hook(
10510 sqlite3 *db,
10511 void(*xPreUpdate)(
10512 void *pCtx, /* Copy of third arg to preupdate_hook() */
10513 sqlite3 *db, /* Database handle */
10514 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10515 char const *zDb, /* Database name */
10516 char const *zName, /* Table name */
10517 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10518 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10519 ),
10520 void*
10521);
10522SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10523SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10524SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10525SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10526#endif
10527
10528/*
10529** CAPI3REF: Low-level system error code
10530**
10531** ^Attempt to return the underlying operating system error code or error
10532** number that caused the most recent I/O error or failure to open a file.
10533** The return value is OS-dependent. For example, on unix systems, after
10534** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10535** called to get back the underlying "errno" that caused the problem, such
10536** as ENOSPC, EAUTH, EISDIR, and so forth.
10537*/
10538SQLITE_API int sqlite3_system_errno(sqlite3*);
10539
10540/*
10541** CAPI3REF: Database Snapshot
10542** KEYWORDS: {snapshot} {sqlite3_snapshot}
10543**
10544** An instance of the snapshot object records the state of a [WAL mode]
10545** database for some specific point in history.
10546**
10547** In [WAL mode], multiple [database connections] that are open on the
10548** same database file can each be reading a different historical version
10549** of the database file. When a [database connection] begins a read
10550** transaction, that connection sees an unchanging copy of the database
10551** as it existed for the point in time when the transaction first started.
10552** Subsequent changes to the database from other connections are not seen
10553** by the reader until a new read transaction is started.
10554**
10555** The sqlite3_snapshot object records state information about an historical
10556** version of the database file so that it is possible to later open a new read
10557** transaction that sees that historical version of the database rather than
10558** the most recent version.
10559*/
10560typedef struct sqlite3_snapshot {
10561 unsigned char hidden[48];
10562} sqlite3_snapshot;
10563
10564/*
10565** CAPI3REF: Record A Database Snapshot
10566** CONSTRUCTOR: sqlite3_snapshot
10567**
10568** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10569** new [sqlite3_snapshot] object that records the current state of
10570** schema S in database connection D. ^On success, the
10571** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10572** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10573** If there is not already a read-transaction open on schema S when
10574** this function is called, one is opened automatically.
10575**
10576** The following must be true for this function to succeed. If any of
10577** the following statements are false when sqlite3_snapshot_get() is
10578** called, SQLITE_ERROR is returned. The final value of *P is undefined
10579** in this case.
10580**
10581** <ul>
10582** <li> The database handle must not be in [autocommit mode].
10583**
10584** <li> Schema S of [database connection] D must be a [WAL mode] database.
10585**
10586** <li> There must not be a write transaction open on schema S of database
10587** connection D.
10588**
10589** <li> One or more transactions must have been written to the current wal
10590** file since it was created on disk (by any connection). This means
10591** that a snapshot cannot be taken on a wal mode database with no wal
10592** file immediately after it is first opened. At least one transaction
10593** must be written to it first.
10594** </ul>
10595**
10596** This function may also return SQLITE_NOMEM. If it is called with the
10597** database handle in autocommit mode but fails for some other reason,
10598** whether or not a read transaction is opened on schema S is undefined.
10599**
10600** The [sqlite3_snapshot] object returned from a successful call to
10601** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10602** to avoid a memory leak.
10603**
10604** The [sqlite3_snapshot_get()] interface is only available when the
10605** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10606*/
10607SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10608 sqlite3 *db,
10609 const char *zSchema,
10610 sqlite3_snapshot **ppSnapshot
10611);
10612
10613/*
10614** CAPI3REF: Start a read transaction on an historical snapshot
10615** METHOD: sqlite3_snapshot
10616**
10617** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10618** transaction or upgrades an existing one for schema S of
10619** [database connection] D such that the read transaction refers to
10620** historical [snapshot] P, rather than the most recent change to the
10621** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10622** on success or an appropriate [error code] if it fails.
10623**
10624** ^In order to succeed, the database connection must not be in
10625** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10626** is already a read transaction open on schema S, then the database handle
10627** must have no active statements (SELECT statements that have been passed
10628** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10629** SQLITE_ERROR is returned if either of these conditions is violated, or
10630** if schema S does not exist, or if the snapshot object is invalid.
10631**
10632** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10633** snapshot has been overwritten by a [checkpoint]. In this case
10634** SQLITE_ERROR_SNAPSHOT is returned.
10635**
10636** If there is already a read transaction open when this function is
10637** invoked, then the same read transaction remains open (on the same
10638** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10639** is returned. If another error code - for example SQLITE_PROTOCOL or an
10640** SQLITE_IOERR error code - is returned, then the final state of the
10641** read transaction is undefined. If SQLITE_OK is returned, then the
10642** read transaction is now open on database snapshot P.
10643**
10644** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10645** database connection D does not know that the database file for
10646** schema S is in [WAL mode]. A database connection might not know
10647** that the database file is in [WAL mode] if there has been no prior
10648** I/O on that database connection, or if the database entered [WAL mode]
10649** after the most recent I/O on the database connection.)^
10650** (Hint: Run "[PRAGMA application_id]" against a newly opened
10651** database connection in order to make it ready to use snapshots.)
10652**
10653** The [sqlite3_snapshot_open()] interface is only available when the
10654** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10655*/
10656SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10657 sqlite3 *db,
10658 const char *zSchema,
10659 sqlite3_snapshot *pSnapshot
10660);
10661
10662/*
10663** CAPI3REF: Destroy a snapshot
10664** DESTRUCTOR: sqlite3_snapshot
10665**
10666** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10667** The application must eventually free every [sqlite3_snapshot] object
10668** using this routine to avoid a memory leak.
10669**
10670** The [sqlite3_snapshot_free()] interface is only available when the
10671** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10672*/
10673SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10674
10675/*
10676** CAPI3REF: Compare the ages of two snapshot handles.
10677** METHOD: sqlite3_snapshot
10678**
10679** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10680** of two valid snapshot handles.
10681**
10682** If the two snapshot handles are not associated with the same database
10683** file, the result of the comparison is undefined.
10684**
10685** Additionally, the result of the comparison is only valid if both of the
10686** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10687** last time the wal file was deleted. The wal file is deleted when the
10688** database is changed back to rollback mode or when the number of database
10689** clients drops to zero. If either snapshot handle was obtained before the
10690** wal file was last deleted, the value returned by this function
10691** is undefined.
10692**
10693** Otherwise, this API returns a negative value if P1 refers to an older
10694** snapshot than P2, zero if the two handles refer to the same database
10695** snapshot, and a positive value if P1 is a newer snapshot than P2.
10696**
10697** This interface is only available if SQLite is compiled with the
10698** [SQLITE_ENABLE_SNAPSHOT] option.
10699*/
10700SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10701 sqlite3_snapshot *p1,
10702 sqlite3_snapshot *p2
10703);
10704
10705/*
10706** CAPI3REF: Recover snapshots from a wal file
10707** METHOD: sqlite3_snapshot
10708**
10709** If a [WAL file] remains on disk after all database connections close
10710** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10711** or because the last process to have the database opened exited without
10712** calling [sqlite3_close()]) and a new connection is subsequently opened
10713** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10714** will only be able to open the last transaction added to the WAL file
10715** even though the WAL file contains other valid transactions.
10716**
10717** This function attempts to scan the WAL file associated with database zDb
10718** of database handle db and make all valid snapshots available to
10719** sqlite3_snapshot_open(). It is an error if there is already a read
10720** transaction open on the database, or if the database is not a WAL mode
10721** database.
10722**
10723** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10724**
10725** This interface is only available if SQLite is compiled with the
10726** [SQLITE_ENABLE_SNAPSHOT] option.
10727*/
10728SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10729
10730/*
10731** CAPI3REF: Serialize a database
10732**
10733** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10734** that is a serialization of the S database on [database connection] D.
10735** If P is not a NULL pointer, then the size of the database in bytes
10736** is written into *P.
10737**
10738** For an ordinary on-disk database file, the serialization is just a
10739** copy of the disk file. For an in-memory database or a "TEMP" database,
10740** the serialization is the same sequence of bytes which would be written
10741** to disk if that database where backed up to disk.
10742**
10743** The usual case is that sqlite3_serialize() copies the serialization of
10744** the database into memory obtained from [sqlite3_malloc64()] and returns
10745** a pointer to that memory. The caller is responsible for freeing the
10746** returned value to avoid a memory leak. However, if the F argument
10747** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10748** are made, and the sqlite3_serialize() function will return a pointer
10749** to the contiguous memory representation of the database that SQLite
10750** is currently using for that database, or NULL if the no such contiguous
10751** memory representation of the database exists. A contiguous memory
10752** representation of the database will usually only exist if there has
10753** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10754** values of D and S.
10755** The size of the database is written into *P even if the
10756** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10757** of the database exists.
10758**
10759** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10760** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10761** allocation error occurs.
10762**
10763** This interface is only available if SQLite is compiled with the
10764** [SQLITE_ENABLE_DESERIALIZE] option.
10765*/
10766SQLITE_API unsigned char *sqlite3_serialize(
10767 sqlite3 *db, /* The database connection */
10768 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10769 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10770 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10771);
10772
10773/*
10774** CAPI3REF: Flags for sqlite3_serialize
10775**
10776** Zero or more of the following constants can be OR-ed together for
10777** the F argument to [sqlite3_serialize(D,S,P,F)].
10778**
10779** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10780** a pointer to contiguous in-memory database that it is currently using,
10781** without making a copy of the database. If SQLite is not currently using
10782** a contiguous in-memory database, then this option causes
10783** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10784** using a contiguous in-memory database if it has been initialized by a
10785** prior call to [sqlite3_deserialize()].
10786*/
10787#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
10788
10789/*
10790** CAPI3REF: Deserialize a database
10791**
10792** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10793** [database connection] D to disconnect from database S and then
10794** reopen S as an in-memory database based on the serialization contained
10795** in P. The serialized database P is N bytes in size. M is the size of
10796** the buffer P, which might be larger than N. If M is larger than N, and
10797** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10798** permitted to add content to the in-memory database as long as the total
10799** size does not exceed M bytes.
10800**
10801** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10802** invoke sqlite3_free() on the serialization buffer when the database
10803** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10804** SQLite will try to increase the buffer size using sqlite3_realloc64()
10805** if writes on the database cause it to grow larger than M bytes.
10806**
10807** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10808** database is currently in a read transaction or is involved in a backup
10809** operation.
10810**
10811** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10812** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10813** [sqlite3_free()] is invoked on argument P prior to returning.
10814**
10815** This interface is only available if SQLite is compiled with the
10816** [SQLITE_ENABLE_DESERIALIZE] option.
10817*/
10818SQLITE_API int sqlite3_deserialize(
10819 sqlite3 *db, /* The database connection */
10820 const char *zSchema, /* Which DB to reopen with the deserialization */
10821 unsigned char *pData, /* The serialized database content */
10822 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10823 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10824 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10825);
10826
10827/*
10828** CAPI3REF: Flags for sqlite3_deserialize()
10829**
10830** The following are allowed values for 6th argument (the F argument) to
10831** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10832**
10833** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10834** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10835** and that SQLite should take ownership of this memory and automatically
10836** free it when it has finished using it. Without this flag, the caller
10837** is responsible for freeing any dynamically allocated memory.
10838**
10839** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10840** grow the size of the database using calls to [sqlite3_realloc64()]. This
10841** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10842** Without this flag, the deserialized database cannot increase in size beyond
10843** the number of bytes specified by the M parameter.
10844**
10845** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10846** should be treated as read-only.
10847*/
10848#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10849#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
10850#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10851
10852/*
10853** Undo the hack that converts floating point types to integer for
10854** builds on processors without floating point support.
10855*/
10856#ifdef SQLITE_OMIT_FLOATING_POINT
10857# undef double
10858#endif
10859
10860#if 0
10861} /* End of the 'extern "C"' block */
10862#endif
10863#endif /* SQLITE3_H */
10864
10865/******** Begin file sqlite3rtree.h *********/
10866/*
10867** 2010 August 30
10868**
10869** The author disclaims copyright to this source code. In place of
10870** a legal notice, here is a blessing:
10871**
10872** May you do good and not evil.
10873** May you find forgiveness for yourself and forgive others.
10874** May you share freely, never taking more than you give.
10875**
10876*************************************************************************
10877*/
10878
10879#ifndef _SQLITE3RTREE_H_
10880#define _SQLITE3RTREE_H_
10881
10882
10883#if 0
10884extern "C" {
10885#endif
10886
10887typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10888typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10889
10890/* The double-precision datatype used by RTree depends on the
10891** SQLITE_RTREE_INT_ONLY compile-time option.
10892*/
10893#ifdef SQLITE_RTREE_INT_ONLY
10894 typedef sqlite3_int64 sqlite3_rtree_dbl;
10895#else
10896 typedef double sqlite3_rtree_dbl;
10897#endif
10898
10899/*
10900** Register a geometry callback named zGeom that can be used as part of an
10901** R-Tree geometry query as follows:
10902**
10903** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10904*/
10905SQLITE_API int sqlite3_rtree_geometry_callback(
10906 sqlite3 *db,
10907 const char *zGeom,
10908 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10909 void *pContext
10910);
10911
10912
10913/*
10914** A pointer to a structure of the following type is passed as the first
10915** argument to callbacks registered using rtree_geometry_callback().
10916*/
10917struct sqlite3_rtree_geometry {
10918 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10919 int nParam; /* Size of array aParam[] */
10920 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10921 void *pUser; /* Callback implementation user data */
10922 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10923};
10924
10925/*
10926** Register a 2nd-generation geometry callback named zScore that can be
10927** used as part of an R-Tree geometry query as follows:
10928**
10929** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10930*/
10931SQLITE_API int sqlite3_rtree_query_callback(
10932 sqlite3 *db,
10933 const char *zQueryFunc,
10934 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10935 void *pContext,
10936 void (*xDestructor)(void*)
10937);
10938
10939
10940/*
10941** A pointer to a structure of the following type is passed as the
10942** argument to scored geometry callback registered using
10943** sqlite3_rtree_query_callback().
10944**
10945** Note that the first 5 fields of this structure are identical to
10946** sqlite3_rtree_geometry. This structure is a subclass of
10947** sqlite3_rtree_geometry.
10948*/
10949struct sqlite3_rtree_query_info {
10950 void *pContext; /* pContext from when function registered */
10951 int nParam; /* Number of function parameters */
10952 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10953 void *pUser; /* callback can use this, if desired */
10954 void (*xDelUser)(void*); /* function to free pUser */
10955 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10956 unsigned int *anQueue; /* Number of pending entries in the queue */
10957 int nCoord; /* Number of coordinates */
10958 int iLevel; /* Level of current node or entry */
10959 int mxLevel; /* The largest iLevel value in the tree */
10960 sqlite3_int64 iRowid; /* Rowid for current entry */
10961 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10962 int eParentWithin; /* Visibility of parent node */
10963 int eWithin; /* OUT: Visibility */
10964 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10965 /* The following fields are only available in 3.8.11 and later */
10966 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10967};
10968
10969/*
10970** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10971*/
10972#define NOT_WITHIN 0 /* Object completely outside of query region */
10973#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
10974#define FULLY_WITHIN 2 /* Object fully contained within query region */
10975
10976
10977#if 0
10978} /* end of the 'extern "C"' block */
10979#endif
10980
10981#endif /* ifndef _SQLITE3RTREE_H_ */
10982
10983/******** End of sqlite3rtree.h *********/
10984/******** Begin file sqlite3session.h *********/
10985
10986#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10987#define __SQLITESESSION_H_ 1
10988
10989/*
10990** Make sure we can call this stuff from C++.
10991*/
10992#if 0
10993extern "C" {
10994#endif
10995
10996
10997/*
10998** CAPI3REF: Session Object Handle
10999**
11000** An instance of this object is a [session] that can be used to
11001** record changes to a database.
11002*/
11003typedef struct sqlite3_session sqlite3_session;
11004
11005/*
11006** CAPI3REF: Changeset Iterator Handle
11007**
11008** An instance of this object acts as a cursor for iterating
11009** over the elements of a [changeset] or [patchset].
11010*/
11011typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
11012
11013/*
11014** CAPI3REF: Create A New Session Object
11015** CONSTRUCTOR: sqlite3_session
11016**
11017** Create a new session object attached to database handle db. If successful,
11018** a pointer to the new object is written to *ppSession and SQLITE_OK is
11019** returned. If an error occurs, *ppSession is set to NULL and an SQLite
11020** error code (e.g. SQLITE_NOMEM) is returned.
11021**
11022** It is possible to create multiple session objects attached to a single
11023** database handle.
11024**
11025** Session objects created using this function should be deleted using the
11026** [sqlite3session_delete()] function before the database handle that they
11027** are attached to is itself closed. If the database handle is closed before
11028** the session object is deleted, then the results of calling any session
11029** module function, including [sqlite3session_delete()] on the session object
11030** are undefined.
11031**
11032** Because the session module uses the [sqlite3_preupdate_hook()] API, it
11033** is not possible for an application to register a pre-update hook on a
11034** database handle that has one or more session objects attached. Nor is
11035** it possible to create a session object attached to a database handle for
11036** which a pre-update hook is already defined. The results of attempting
11037** either of these things are undefined.
11038**
11039** The session object will be used to create changesets for tables in
11040** database zDb, where zDb is either "main", or "temp", or the name of an
11041** attached database. It is not an error if database zDb is not attached
11042** to the database when the session object is created.
11043*/
11044SQLITE_API int sqlite3session_create(
11045 sqlite3 *db, /* Database handle */
11046 const char *zDb, /* Name of db (e.g. "main") */
11047 sqlite3_session **ppSession /* OUT: New session object */
11048);
11049
11050/*
11051** CAPI3REF: Delete A Session Object
11052** DESTRUCTOR: sqlite3_session
11053**
11054** Delete a session object previously allocated using
11055** [sqlite3session_create()]. Once a session object has been deleted, the
11056** results of attempting to use pSession with any other session module
11057** function are undefined.
11058**
11059** Session objects must be deleted before the database handle to which they
11060** are attached is closed. Refer to the documentation for
11061** [sqlite3session_create()] for details.
11062*/
11063SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
11064
11065
11066/*
11067** CAPI3REF: Enable Or Disable A Session Object
11068** METHOD: sqlite3_session
11069**
11070** Enable or disable the recording of changes by a session object. When
11071** enabled, a session object records changes made to the database. When
11072** disabled - it does not. A newly created session object is enabled.
11073** Refer to the documentation for [sqlite3session_changeset()] for further
11074** details regarding how enabling and disabling a session object affects
11075** the eventual changesets.
11076**
11077** Passing zero to this function disables the session. Passing a value
11078** greater than zero enables it. Passing a value less than zero is a
11079** no-op, and may be used to query the current state of the session.
11080**
11081** The return value indicates the final state of the session object: 0 if
11082** the session is disabled, or 1 if it is enabled.
11083*/
11084SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11085
11086/*
11087** CAPI3REF: Set Or Clear the Indirect Change Flag
11088** METHOD: sqlite3_session
11089**
11090** Each change recorded by a session object is marked as either direct or
11091** indirect. A change is marked as indirect if either:
11092**
11093** <ul>
11094** <li> The session object "indirect" flag is set when the change is
11095** made, or
11096** <li> The change is made by an SQL trigger or foreign key action
11097** instead of directly as a result of a users SQL statement.
11098** </ul>
11099**
11100** If a single row is affected by more than one operation within a session,
11101** then the change is considered indirect if all operations meet the criteria
11102** for an indirect change above, or direct otherwise.
11103**
11104** This function is used to set, clear or query the session object indirect
11105** flag. If the second argument passed to this function is zero, then the
11106** indirect flag is cleared. If it is greater than zero, the indirect flag
11107** is set. Passing a value less than zero does not modify the current value
11108** of the indirect flag, and may be used to query the current state of the
11109** indirect flag for the specified session object.
11110**
11111** The return value indicates the final state of the indirect flag: 0 if
11112** it is clear, or 1 if it is set.
11113*/
11114SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11115
11116/*
11117** CAPI3REF: Attach A Table To A Session Object
11118** METHOD: sqlite3_session
11119**
11120** If argument zTab is not NULL, then it is the name of a table to attach
11121** to the session object passed as the first argument. All subsequent changes
11122** made to the table while the session object is enabled will be recorded. See
11123** documentation for [sqlite3session_changeset()] for further details.
11124**
11125** Or, if argument zTab is NULL, then changes are recorded for all tables
11126** in the database. If additional tables are added to the database (by
11127** executing "CREATE TABLE" statements) after this call is made, changes for
11128** the new tables are also recorded.
11129**
11130** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11131** defined as part of their CREATE TABLE statement. It does not matter if the
11132** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11133** KEY may consist of a single column, or may be a composite key.
11134**
11135** It is not an error if the named table does not exist in the database. Nor
11136** is it an error if the named table does not have a PRIMARY KEY. However,
11137** no changes will be recorded in either of these scenarios.
11138**
11139** Changes are not recorded for individual rows that have NULL values stored
11140** in one or more of their PRIMARY KEY columns.
11141**
11142** SQLITE_OK is returned if the call completes without error. Or, if an error
11143** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11144**
11145** <h3>Special sqlite_stat1 Handling</h3>
11146**
11147** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11148** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11149** <pre>
11150** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
11151** </pre>
11152**
11153** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11154** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11155** are recorded for rows for which (idx IS NULL) is true. However, for such
11156** rows a zero-length blob (SQL value X'') is stored in the changeset or
11157** patchset instead of a NULL value. This allows such changesets to be
11158** manipulated by legacy implementations of sqlite3changeset_invert(),
11159** concat() and similar.
11160**
11161** The sqlite3changeset_apply() function automatically converts the
11162** zero-length blob back to a NULL value when updating the sqlite_stat1
11163** table. However, if the application calls sqlite3changeset_new(),
11164** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11165** iterator directly (including on a changeset iterator passed to a
11166** conflict-handler callback) then the X'' value is returned. The application
11167** must translate X'' to NULL itself if required.
11168**
11169** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11170** changes made to the sqlite_stat1 table. Legacy versions of the
11171** sqlite3changeset_apply() function silently ignore any modifications to the
11172** sqlite_stat1 table that are part of a changeset or patchset.
11173*/
11174SQLITE_API int sqlite3session_attach(
11175 sqlite3_session *pSession, /* Session object */
11176 const char *zTab /* Table name */
11177);
11178
11179/*
11180** CAPI3REF: Set a table filter on a Session Object.
11181** METHOD: sqlite3_session
11182**
11183** The second argument (xFilter) is the "filter callback". For changes to rows
11184** in tables that are not attached to the Session object, the filter is called
11185** to determine whether changes to the table's rows should be tracked or not.
11186** If xFilter returns 0, changes are not tracked. Note that once a table is
11187** attached, xFilter will not be called again.
11188*/
11189SQLITE_API void sqlite3session_table_filter(
11190 sqlite3_session *pSession, /* Session object */
11191 int(*xFilter)(
11192 void *pCtx, /* Copy of third arg to _filter_table() */
11193 const char *zTab /* Table name */
11194 ),
11195 void *pCtx /* First argument passed to xFilter */
11196);
11197
11198/*
11199** CAPI3REF: Generate A Changeset From A Session Object
11200** METHOD: sqlite3_session
11201**
11202** Obtain a changeset containing changes to the tables attached to the
11203** session object passed as the first argument. If successful,
11204** set *ppChangeset to point to a buffer containing the changeset
11205** and *pnChangeset to the size of the changeset in bytes before returning
11206** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11207** zero and return an SQLite error code.
11208**
11209** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11210** each representing a change to a single row of an attached table. An INSERT
11211** change contains the values of each field of a new database row. A DELETE
11212** contains the original values of each field of a deleted database row. An
11213** UPDATE change contains the original values of each field of an updated
11214** database row along with the updated values for each updated non-primary-key
11215** column. It is not possible for an UPDATE change to represent a change that
11216** modifies the values of primary key columns. If such a change is made, it
11217** is represented in a changeset as a DELETE followed by an INSERT.
11218**
11219** Changes are not recorded for rows that have NULL values stored in one or
11220** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11221** no corresponding change is present in the changesets returned by this
11222** function. If an existing row with one or more NULL values stored in
11223** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11224** only an INSERT is appears in the changeset. Similarly, if an existing row
11225** with non-NULL PRIMARY KEY values is updated so that one or more of its
11226** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11227** DELETE change only.
11228**
11229** The contents of a changeset may be traversed using an iterator created
11230** using the [sqlite3changeset_start()] API. A changeset may be applied to
11231** a database with a compatible schema using the [sqlite3changeset_apply()]
11232** API.
11233**
11234** Within a changeset generated by this function, all changes related to a
11235** single table are grouped together. In other words, when iterating through
11236** a changeset or when applying a changeset to a database, all changes related
11237** to a single table are processed before moving on to the next table. Tables
11238** are sorted in the same order in which they were attached (or auto-attached)
11239** to the sqlite3_session object. The order in which the changes related to
11240** a single table are stored is undefined.
11241**
11242** Following a successful call to this function, it is the responsibility of
11243** the caller to eventually free the buffer that *ppChangeset points to using
11244** [sqlite3_free()].
11245**
11246** <h3>Changeset Generation</h3>
11247**
11248** Once a table has been attached to a session object, the session object
11249** records the primary key values of all new rows inserted into the table.
11250** It also records the original primary key and other column values of any
11251** deleted or updated rows. For each unique primary key value, data is only
11252** recorded once - the first time a row with said primary key is inserted,
11253** updated or deleted in the lifetime of the session.
11254**
11255** There is one exception to the previous paragraph: when a row is inserted,
11256** updated or deleted, if one or more of its primary key columns contain a
11257** NULL value, no record of the change is made.
11258**
11259** The session object therefore accumulates two types of records - those
11260** that consist of primary key values only (created when the user inserts
11261** a new record) and those that consist of the primary key values and the
11262** original values of other table columns (created when the users deletes
11263** or updates a record).
11264**
11265** When this function is called, the requested changeset is created using
11266** both the accumulated records and the current contents of the database
11267** file. Specifically:
11268**
11269** <ul>
11270** <li> For each record generated by an insert, the database is queried
11271** for a row with a matching primary key. If one is found, an INSERT
11272** change is added to the changeset. If no such row is found, no change
11273** is added to the changeset.
11274**
11275** <li> For each record generated by an update or delete, the database is
11276** queried for a row with a matching primary key. If such a row is
11277** found and one or more of the non-primary key fields have been
11278** modified from their original values, an UPDATE change is added to
11279** the changeset. Or, if no such row is found in the table, a DELETE
11280** change is added to the changeset. If there is a row with a matching
11281** primary key in the database, but all fields contain their original
11282** values, no change is added to the changeset.
11283** </ul>
11284**
11285** This means, amongst other things, that if a row is inserted and then later
11286** deleted while a session object is active, neither the insert nor the delete
11287** will be present in the changeset. Or if a row is deleted and then later a
11288** row with the same primary key values inserted while a session object is
11289** active, the resulting changeset will contain an UPDATE change instead of
11290** a DELETE and an INSERT.
11291**
11292** When a session object is disabled (see the [sqlite3session_enable()] API),
11293** it does not accumulate records when rows are inserted, updated or deleted.
11294** This may appear to have some counter-intuitive effects if a single row
11295** is written to more than once during a session. For example, if a row
11296** is inserted while a session object is enabled, then later deleted while
11297** the same session object is disabled, no INSERT record will appear in the
11298** changeset, even though the delete took place while the session was disabled.
11299** Or, if one field of a row is updated while a session is disabled, and
11300** another field of the same row is updated while the session is enabled, the
11301** resulting changeset will contain an UPDATE change that updates both fields.
11302*/
11303SQLITE_API int sqlite3session_changeset(
11304 sqlite3_session *pSession, /* Session object */
11305 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11306 void **ppChangeset /* OUT: Buffer containing changeset */
11307);
11308
11309/*
11310** CAPI3REF: Load The Difference Between Tables Into A Session
11311** METHOD: sqlite3_session
11312**
11313** If it is not already attached to the session object passed as the first
11314** argument, this function attaches table zTbl in the same manner as the
11315** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11316** does not have a primary key, this function is a no-op (but does not return
11317** an error).
11318**
11319** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11320** attached to the same database handle as the session object that contains
11321** a table compatible with the table attached to the session by this function.
11322** A table is considered compatible if it:
11323**
11324** <ul>
11325** <li> Has the same name,
11326** <li> Has the same set of columns declared in the same order, and
11327** <li> Has the same PRIMARY KEY definition.
11328** </ul>
11329**
11330** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11331** are compatible but do not have any PRIMARY KEY columns, it is not an error
11332** but no changes are added to the session object. As with other session
11333** APIs, tables without PRIMARY KEYs are simply ignored.
11334**
11335** This function adds a set of changes to the session object that could be
11336** used to update the table in database zFrom (call this the "from-table")
11337** so that its content is the same as the table attached to the session
11338** object (call this the "to-table"). Specifically:
11339**
11340** <ul>
11341** <li> For each row (primary key) that exists in the to-table but not in
11342** the from-table, an INSERT record is added to the session object.
11343**
11344** <li> For each row (primary key) that exists in the to-table but not in
11345** the from-table, a DELETE record is added to the session object.
11346**
11347** <li> For each row (primary key) that exists in both tables, but features
11348** different non-PK values in each, an UPDATE record is added to the
11349** session.
11350** </ul>
11351**
11352** To clarify, if this function is called and then a changeset constructed
11353** using [sqlite3session_changeset()], then after applying that changeset to
11354** database zFrom the contents of the two compatible tables would be
11355** identical.
11356**
11357** It an error if database zFrom does not exist or does not contain the
11358** required compatible table.
11359**
11360** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11361** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11362** may be set to point to a buffer containing an English language error
11363** message. It is the responsibility of the caller to free this buffer using
11364** sqlite3_free().
11365*/
11366SQLITE_API int sqlite3session_diff(
11367 sqlite3_session *pSession,
11368 const char *zFromDb,
11369 const char *zTbl,
11370 char **pzErrMsg
11371);
11372
11373
11374/*
11375** CAPI3REF: Generate A Patchset From A Session Object
11376** METHOD: sqlite3_session
11377**
11378** The differences between a patchset and a changeset are that:
11379**
11380** <ul>
11381** <li> DELETE records consist of the primary key fields only. The
11382** original values of other fields are omitted.
11383** <li> The original values of any modified fields are omitted from
11384** UPDATE records.
11385** </ul>
11386**
11387** A patchset blob may be used with up to date versions of all
11388** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11389** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11390** attempting to use a patchset blob with old versions of the
11391** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11392**
11393** Because the non-primary key "old.*" fields are omitted, no
11394** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11395** is passed to the sqlite3changeset_apply() API. Other conflict types work
11396** in the same way as for changesets.
11397**
11398** Changes within a patchset are ordered in the same way as for changesets
11399** generated by the sqlite3session_changeset() function (i.e. all changes for
11400** a single table are grouped together, tables appear in the order in which
11401** they were attached to the session object).
11402*/
11403SQLITE_API int sqlite3session_patchset(
11404 sqlite3_session *pSession, /* Session object */
11405 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11406 void **ppPatchset /* OUT: Buffer containing patchset */
11407);
11408
11409/*
11410** CAPI3REF: Test if a changeset has recorded any changes.
11411**
11412** Return non-zero if no changes to attached tables have been recorded by
11413** the session object passed as the first argument. Otherwise, if one or
11414** more changes have been recorded, return zero.
11415**
11416** Even if this function returns zero, it is possible that calling
11417** [sqlite3session_changeset()] on the session handle may still return a
11418** changeset that contains no changes. This can happen when a row in
11419** an attached table is modified and then later on the original values
11420** are restored. However, if this function returns non-zero, then it is
11421** guaranteed that a call to sqlite3session_changeset() will return a
11422** changeset containing zero changes.
11423*/
11424SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11425
11426/*
11427** CAPI3REF: Create An Iterator To Traverse A Changeset
11428** CONSTRUCTOR: sqlite3_changeset_iter
11429**
11430** Create an iterator used to iterate through the contents of a changeset.
11431** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11432** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11433** SQLite error code is returned.
11434**
11435** The following functions can be used to advance and query a changeset
11436** iterator created by this function:
11437**
11438** <ul>
11439** <li> [sqlite3changeset_next()]
11440** <li> [sqlite3changeset_op()]
11441** <li> [sqlite3changeset_new()]
11442** <li> [sqlite3changeset_old()]
11443** </ul>
11444**
11445** It is the responsibility of the caller to eventually destroy the iterator
11446** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11447** changeset (pChangeset) must remain valid until after the iterator is
11448** destroyed.
11449**
11450** Assuming the changeset blob was created by one of the
11451** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11452** [sqlite3changeset_invert()] functions, all changes within the changeset
11453** that apply to a single table are grouped together. This means that when
11454** an application iterates through a changeset using an iterator created by
11455** this function, all changes that relate to a single table are visited
11456** consecutively. There is no chance that the iterator will visit a change
11457** the applies to table X, then one for table Y, and then later on visit
11458** another change for table X.
11459**
11460** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11461** may be modified by passing a combination of
11462** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11463**
11464** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11465** and therefore subject to change.
11466*/
11467SQLITE_API int sqlite3changeset_start(
11468 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11469 int nChangeset, /* Size of changeset blob in bytes */
11470 void *pChangeset /* Pointer to blob containing changeset */
11471);
11472SQLITE_API int sqlite3changeset_start_v2(
11473 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11474 int nChangeset, /* Size of changeset blob in bytes */
11475 void *pChangeset, /* Pointer to blob containing changeset */
11476 int flags /* SESSION_CHANGESETSTART_* flags */
11477);
11478
11479/*
11480** CAPI3REF: Flags for sqlite3changeset_start_v2
11481**
11482** The following flags may passed via the 4th parameter to
11483** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11484**
11485** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11486** Invert the changeset while iterating through it. This is equivalent to
11487** inverting a changeset using sqlite3changeset_invert() before applying it.
11488** It is an error to specify this flag with a patchset.
11489*/
11490#define SQLITE_CHANGESETSTART_INVERT 0x0002
11491
11492
11493/*
11494** CAPI3REF: Advance A Changeset Iterator
11495** METHOD: sqlite3_changeset_iter
11496**
11497** This function may only be used with iterators created by the function
11498** [sqlite3changeset_start()]. If it is called on an iterator passed to
11499** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11500** is returned and the call has no effect.
11501**
11502** Immediately after an iterator is created by sqlite3changeset_start(), it
11503** does not point to any change in the changeset. Assuming the changeset
11504** is not empty, the first call to this function advances the iterator to
11505** point to the first change in the changeset. Each subsequent call advances
11506** the iterator to point to the next change in the changeset (if any). If
11507** no error occurs and the iterator points to a valid change after a call
11508** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11509** Otherwise, if all changes in the changeset have already been visited,
11510** SQLITE_DONE is returned.
11511**
11512** If an error occurs, an SQLite error code is returned. Possible error
11513** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11514** SQLITE_NOMEM.
11515*/
11516SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11517
11518/*
11519** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11520** METHOD: sqlite3_changeset_iter
11521**
11522** The pIter argument passed to this function may either be an iterator
11523** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11524** created by [sqlite3changeset_start()]. In the latter case, the most recent
11525** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11526** is not the case, this function returns [SQLITE_MISUSE].
11527**
11528** If argument pzTab is not NULL, then *pzTab is set to point to a
11529** nul-terminated utf-8 encoded string containing the name of the table
11530** affected by the current change. The buffer remains valid until either
11531** sqlite3changeset_next() is called on the iterator or until the
11532** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
11533** set to the number of columns in the table affected by the change. If
11534** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11535** is an indirect change, or false (0) otherwise. See the documentation for
11536** [sqlite3session_indirect()] for a description of direct and indirect
11537** changes. Finally, if pOp is not NULL, then *pOp is set to one of
11538** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
11539** type of change that the iterator currently points to.
11540**
11541** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11542** SQLite error code is returned. The values of the output variables may not
11543** be trusted in this case.
11544*/
11545SQLITE_API int sqlite3changeset_op(
11546 sqlite3_changeset_iter *pIter, /* Iterator object */
11547 const char **pzTab, /* OUT: Pointer to table name */
11548 int *pnCol, /* OUT: Number of columns in table */
11549 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11550 int *pbIndirect /* OUT: True for an 'indirect' change */
11551);
11552
11553/*
11554** CAPI3REF: Obtain The Primary Key Definition Of A Table
11555** METHOD: sqlite3_changeset_iter
11556**
11557** For each modified table, a changeset includes the following:
11558**
11559** <ul>
11560** <li> The number of columns in the table, and
11561** <li> Which of those columns make up the tables PRIMARY KEY.
11562** </ul>
11563**
11564** This function is used to find which columns comprise the PRIMARY KEY of
11565** the table modified by the change that iterator pIter currently points to.
11566** If successful, *pabPK is set to point to an array of nCol entries, where
11567** nCol is the number of columns in the table. Elements of *pabPK are set to
11568** 0x01 if the corresponding column is part of the tables primary key, or
11569** 0x00 if it is not.
11570**
11571** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11572** in the table.
11573**
11574** If this function is called when the iterator does not point to a valid
11575** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11576** SQLITE_OK is returned and the output variables populated as described
11577** above.
11578*/
11579SQLITE_API int sqlite3changeset_pk(
11580 sqlite3_changeset_iter *pIter, /* Iterator object */
11581 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11582 int *pnCol /* OUT: Number of entries in output array */
11583);
11584
11585/*
11586** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11587** METHOD: sqlite3_changeset_iter
11588**
11589** The pIter argument passed to this function may either be an iterator
11590** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11591** created by [sqlite3changeset_start()]. In the latter case, the most recent
11592** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11593** Furthermore, it may only be called if the type of change that the iterator
11594** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11595** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11596**
11597** Argument iVal must be greater than or equal to 0, and less than the number
11598** of columns in the table affected by the current change. Otherwise,
11599** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11600**
11601** If successful, this function sets *ppValue to point to a protected
11602** sqlite3_value object containing the iVal'th value from the vector of
11603** original row values stored as part of the UPDATE or DELETE change and
11604** returns SQLITE_OK. The name of the function comes from the fact that this
11605** is similar to the "old.*" columns available to update or delete triggers.
11606**
11607** If some other error occurs (e.g. an OOM condition), an SQLite error code
11608** is returned and *ppValue is set to NULL.
11609*/
11610SQLITE_API int sqlite3changeset_old(
11611 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11612 int iVal, /* Column number */
11613 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11614);
11615
11616/*
11617** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11618** METHOD: sqlite3_changeset_iter
11619**
11620** The pIter argument passed to this function may either be an iterator
11621** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11622** created by [sqlite3changeset_start()]. In the latter case, the most recent
11623** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11624** Furthermore, it may only be called if the type of change that the iterator
11625** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11626** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11627**
11628** Argument iVal must be greater than or equal to 0, and less than the number
11629** of columns in the table affected by the current change. Otherwise,
11630** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11631**
11632** If successful, this function sets *ppValue to point to a protected
11633** sqlite3_value object containing the iVal'th value from the vector of
11634** new row values stored as part of the UPDATE or INSERT change and
11635** returns SQLITE_OK. If the change is an UPDATE and does not include
11636** a new value for the requested column, *ppValue is set to NULL and
11637** SQLITE_OK returned. The name of the function comes from the fact that
11638** this is similar to the "new.*" columns available to update or delete
11639** triggers.
11640**
11641** If some other error occurs (e.g. an OOM condition), an SQLite error code
11642** is returned and *ppValue is set to NULL.
11643*/
11644SQLITE_API int sqlite3changeset_new(
11645 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11646 int iVal, /* Column number */
11647 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11648);
11649
11650/*
11651** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11652** METHOD: sqlite3_changeset_iter
11653**
11654** This function should only be used with iterator objects passed to a
11655** conflict-handler callback by [sqlite3changeset_apply()] with either
11656** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11657** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11658** is set to NULL.
11659**
11660** Argument iVal must be greater than or equal to 0, and less than the number
11661** of columns in the table affected by the current change. Otherwise,
11662** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11663**
11664** If successful, this function sets *ppValue to point to a protected
11665** sqlite3_value object containing the iVal'th value from the
11666** "conflicting row" associated with the current conflict-handler callback
11667** and returns SQLITE_OK.
11668**
11669** If some other error occurs (e.g. an OOM condition), an SQLite error code
11670** is returned and *ppValue is set to NULL.
11671*/
11672SQLITE_API int sqlite3changeset_conflict(
11673 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11674 int iVal, /* Column number */
11675 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11676);
11677
11678/*
11679** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11680** METHOD: sqlite3_changeset_iter
11681**
11682** This function may only be called with an iterator passed to an
11683** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11684** it sets the output variable to the total number of known foreign key
11685** violations in the destination database and returns SQLITE_OK.
11686**
11687** In all other cases this function returns SQLITE_MISUSE.
11688*/
11689SQLITE_API int sqlite3changeset_fk_conflicts(
11690 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11691 int *pnOut /* OUT: Number of FK violations */
11692);
11693
11694
11695/*
11696** CAPI3REF: Finalize A Changeset Iterator
11697** METHOD: sqlite3_changeset_iter
11698**
11699** This function is used to finalize an iterator allocated with
11700** [sqlite3changeset_start()].
11701**
11702** This function should only be called on iterators created using the
11703** [sqlite3changeset_start()] function. If an application calls this
11704** function with an iterator passed to a conflict-handler by
11705** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11706** call has no effect.
11707**
11708** If an error was encountered within a call to an sqlite3changeset_xxx()
11709** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11710** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11711** to that error is returned by this function. Otherwise, SQLITE_OK is
11712** returned. This is to allow the following pattern (pseudo-code):
11713**
11714** <pre>
11715** sqlite3changeset_start();
11716** while( SQLITE_ROW==sqlite3changeset_next() ){
11717** // Do something with change.
11718** }
11719** rc = sqlite3changeset_finalize();
11720** if( rc!=SQLITE_OK ){
11721** // An error has occurred
11722** }
11723** </pre>
11724*/
11725SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11726
11727/*
11728** CAPI3REF: Invert A Changeset
11729**
11730** This function is used to "invert" a changeset object. Applying an inverted
11731** changeset to a database reverses the effects of applying the uninverted
11732** changeset. Specifically:
11733**
11734** <ul>
11735** <li> Each DELETE change is changed to an INSERT, and
11736** <li> Each INSERT change is changed to a DELETE, and
11737** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11738** </ul>
11739**
11740** This function does not change the order in which changes appear within
11741** the changeset. It merely reverses the sense of each individual change.
11742**
11743** If successful, a pointer to a buffer containing the inverted changeset
11744** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11745** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11746** zeroed and an SQLite error code returned.
11747**
11748** It is the responsibility of the caller to eventually call sqlite3_free()
11749** on the *ppOut pointer to free the buffer allocation following a successful
11750** call to this function.
11751**
11752** WARNING/TODO: This function currently assumes that the input is a valid
11753** changeset. If it is not, the results are undefined.
11754*/
11755SQLITE_API int sqlite3changeset_invert(
11756 int nIn, const void *pIn, /* Input changeset */
11757 int *pnOut, void **ppOut /* OUT: Inverse of input */
11758);
11759
11760/*
11761** CAPI3REF: Concatenate Two Changeset Objects
11762**
11763** This function is used to concatenate two changesets, A and B, into a
11764** single changeset. The result is a changeset equivalent to applying
11765** changeset A followed by changeset B.
11766**
11767** This function combines the two input changesets using an
11768** sqlite3_changegroup object. Calling it produces similar results as the
11769** following code fragment:
11770**
11771** <pre>
11772** sqlite3_changegroup *pGrp;
11773** rc = sqlite3_changegroup_new(&pGrp);
11774** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11775** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11776** if( rc==SQLITE_OK ){
11777** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11778** }else{
11779** *ppOut = 0;
11780** *pnOut = 0;
11781** }
11782** </pre>
11783**
11784** Refer to the sqlite3_changegroup documentation below for details.
11785*/
11786SQLITE_API int sqlite3changeset_concat(
11787 int nA, /* Number of bytes in buffer pA */
11788 void *pA, /* Pointer to buffer containing changeset A */
11789 int nB, /* Number of bytes in buffer pB */
11790 void *pB, /* Pointer to buffer containing changeset B */
11791 int *pnOut, /* OUT: Number of bytes in output changeset */
11792 void **ppOut /* OUT: Buffer containing output changeset */
11793);
11794
11795
11796/*
11797** CAPI3REF: Changegroup Handle
11798**
11799** A changegroup is an object used to combine two or more
11800** [changesets] or [patchsets]
11801*/
11802typedef struct sqlite3_changegroup sqlite3_changegroup;
11803
11804/*
11805** CAPI3REF: Create A New Changegroup Object
11806** CONSTRUCTOR: sqlite3_changegroup
11807**
11808** An sqlite3_changegroup object is used to combine two or more changesets
11809** (or patchsets) into a single changeset (or patchset). A single changegroup
11810** object may combine changesets or patchsets, but not both. The output is
11811** always in the same format as the input.
11812**
11813** If successful, this function returns SQLITE_OK and populates (*pp) with
11814** a pointer to a new sqlite3_changegroup object before returning. The caller
11815** should eventually free the returned object using a call to
11816** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11817** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11818**
11819** The usual usage pattern for an sqlite3_changegroup object is as follows:
11820**
11821** <ul>
11822** <li> It is created using a call to sqlite3changegroup_new().
11823**
11824** <li> Zero or more changesets (or patchsets) are added to the object
11825** by calling sqlite3changegroup_add().
11826**
11827** <li> The result of combining all input changesets together is obtained
11828** by the application via a call to sqlite3changegroup_output().
11829**
11830** <li> The object is deleted using a call to sqlite3changegroup_delete().
11831** </ul>
11832**
11833** Any number of calls to add() and output() may be made between the calls to
11834** new() and delete(), and in any order.
11835**
11836** As well as the regular sqlite3changegroup_add() and
11837** sqlite3changegroup_output() functions, also available are the streaming
11838** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11839*/
11840SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11841
11842/*
11843** CAPI3REF: Add A Changeset To A Changegroup
11844** METHOD: sqlite3_changegroup
11845**
11846** Add all changes within the changeset (or patchset) in buffer pData (size
11847** nData bytes) to the changegroup.
11848**
11849** If the buffer contains a patchset, then all prior calls to this function
11850** on the same changegroup object must also have specified patchsets. Or, if
11851** the buffer contains a changeset, so must have the earlier calls to this
11852** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11853** to the changegroup.
11854**
11855** Rows within the changeset and changegroup are identified by the values in
11856** their PRIMARY KEY columns. A change in the changeset is considered to
11857** apply to the same row as a change already present in the changegroup if
11858** the two rows have the same primary key.
11859**
11860** Changes to rows that do not already appear in the changegroup are
11861** simply copied into it. Or, if both the new changeset and the changegroup
11862** contain changes that apply to a single row, the final contents of the
11863** changegroup depends on the type of each change, as follows:
11864**
11865** <table border=1 style="margin-left:8ex;margin-right:8ex">
11866** <tr><th style="white-space:pre">Existing Change </th>
11867** <th style="white-space:pre">New Change </th>
11868** <th>Output Change
11869** <tr><td>INSERT <td>INSERT <td>
11870** The new change is ignored. This case does not occur if the new
11871** changeset was recorded immediately after the changesets already
11872** added to the changegroup.
11873** <tr><td>INSERT <td>UPDATE <td>
11874** The INSERT change remains in the changegroup. The values in the
11875** INSERT change are modified as if the row was inserted by the
11876** existing change and then updated according to the new change.
11877** <tr><td>INSERT <td>DELETE <td>
11878** The existing INSERT is removed from the changegroup. The DELETE is
11879** not added.
11880** <tr><td>UPDATE <td>INSERT <td>
11881** The new change is ignored. This case does not occur if the new
11882** changeset was recorded immediately after the changesets already
11883** added to the changegroup.
11884** <tr><td>UPDATE <td>UPDATE <td>
11885** The existing UPDATE remains within the changegroup. It is amended
11886** so that the accompanying values are as if the row was updated once
11887** by the existing change and then again by the new change.
11888** <tr><td>UPDATE <td>DELETE <td>
11889** The existing UPDATE is replaced by the new DELETE within the
11890** changegroup.
11891** <tr><td>DELETE <td>INSERT <td>
11892** If one or more of the column values in the row inserted by the
11893** new change differ from those in the row deleted by the existing
11894** change, the existing DELETE is replaced by an UPDATE within the
11895** changegroup. Otherwise, if the inserted row is exactly the same
11896** as the deleted row, the existing DELETE is simply discarded.
11897** <tr><td>DELETE <td>UPDATE <td>
11898** The new change is ignored. This case does not occur if the new
11899** changeset was recorded immediately after the changesets already
11900** added to the changegroup.
11901** <tr><td>DELETE <td>DELETE <td>
11902** The new change is ignored. This case does not occur if the new
11903** changeset was recorded immediately after the changesets already
11904** added to the changegroup.
11905** </table>
11906**
11907** If the new changeset contains changes to a table that is already present
11908** in the changegroup, then the number of columns and the position of the
11909** primary key columns for the table must be consistent. If this is not the
11910** case, this function fails with SQLITE_SCHEMA. If the input changeset
11911** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11912** returned. Or, if an out-of-memory condition occurs during processing, this
11913** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
11914** of the final contents of the changegroup is undefined.
11915**
11916** If no error occurs, SQLITE_OK is returned.
11917*/
11918SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11919
11920/*
11921** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11922** METHOD: sqlite3_changegroup
11923**
11924** Obtain a buffer containing a changeset (or patchset) representing the
11925** current contents of the changegroup. If the inputs to the changegroup
11926** were themselves changesets, the output is a changeset. Or, if the
11927** inputs were patchsets, the output is also a patchset.
11928**
11929** As with the output of the sqlite3session_changeset() and
11930** sqlite3session_patchset() functions, all changes related to a single
11931** table are grouped together in the output of this function. Tables appear
11932** in the same order as for the very first changeset added to the changegroup.
11933** If the second or subsequent changesets added to the changegroup contain
11934** changes for tables that do not appear in the first changeset, they are
11935** appended onto the end of the output changeset, again in the order in
11936** which they are first encountered.
11937**
11938** If an error occurs, an SQLite error code is returned and the output
11939** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11940** is returned and the output variables are set to the size of and a
11941** pointer to the output buffer, respectively. In this case it is the
11942** responsibility of the caller to eventually free the buffer using a
11943** call to sqlite3_free().
11944*/
11945SQLITE_API int sqlite3changegroup_output(
11946 sqlite3_changegroup*,
11947 int *pnData, /* OUT: Size of output buffer in bytes */
11948 void **ppData /* OUT: Pointer to output buffer */
11949);
11950
11951/*
11952** CAPI3REF: Delete A Changegroup Object
11953** DESTRUCTOR: sqlite3_changegroup
11954*/
11955SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11956
11957/*
11958** CAPI3REF: Apply A Changeset To A Database
11959**
11960** Apply a changeset or patchset to a database. These functions attempt to
11961** update the "main" database attached to handle db with the changes found in
11962** the changeset passed via the second and third arguments.
11963**
11964** The fourth argument (xFilter) passed to these functions is the "filter
11965** callback". If it is not NULL, then for each table affected by at least one
11966** change in the changeset, the filter callback is invoked with
11967** the table name as the second argument, and a copy of the context pointer
11968** passed as the sixth argument as the first. If the "filter callback"
11969** returns zero, then no attempt is made to apply any changes to the table.
11970** Otherwise, if the return value is non-zero or the xFilter argument to
11971** is NULL, all changes related to the table are attempted.
11972**
11973** For each table that is not excluded by the filter callback, this function
11974** tests that the target database contains a compatible table. A table is
11975** considered compatible if all of the following are true:
11976**
11977** <ul>
11978** <li> The table has the same name as the name recorded in the
11979** changeset, and
11980** <li> The table has at least as many columns as recorded in the
11981** changeset, and
11982** <li> The table has primary key columns in the same position as
11983** recorded in the changeset.
11984** </ul>
11985**
11986** If there is no compatible table, it is not an error, but none of the
11987** changes associated with the table are applied. A warning message is issued
11988** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11989** one such warning is issued for each table in the changeset.
11990**
11991** For each change for which there is a compatible table, an attempt is made
11992** to modify the table contents according to the UPDATE, INSERT or DELETE
11993** change. If a change cannot be applied cleanly, the conflict handler
11994** function passed as the fifth argument to sqlite3changeset_apply() may be
11995** invoked. A description of exactly when the conflict handler is invoked for
11996** each type of change is below.
11997**
11998** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11999** of passing anything other than a valid function pointer as the xConflict
12000** argument are undefined.
12001**
12002** Each time the conflict handler function is invoked, it must return one
12003** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
12004** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
12005** if the second argument passed to the conflict handler is either
12006** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12007** returns an illegal value, any changes already made are rolled back and
12008** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
12009** actions are taken by sqlite3changeset_apply() depending on the value
12010** returned by each invocation of the conflict-handler function. Refer to
12011** the documentation for the three
12012** [SQLITE_CHANGESET_OMIT|available return values] for details.
12013**
12014** <dl>
12015** <dt>DELETE Changes<dd>
12016** For each DELETE change, the function checks if the target database
12017** contains a row with the same primary key value (or values) as the
12018** original row values stored in the changeset. If it does, and the values
12019** stored in all non-primary key columns also match the values stored in
12020** the changeset the row is deleted from the target database.
12021**
12022** If a row with matching primary key values is found, but one or more of
12023** the non-primary key fields contains a value different from the original
12024** row value stored in the changeset, the conflict-handler function is
12025** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
12026** database table has more columns than are recorded in the changeset,
12027** only the values of those non-primary key fields are compared against
12028** the current database contents - any trailing database table columns
12029** are ignored.
12030**
12031** If no row with matching primary key values is found in the database,
12032** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12033** passed as the second argument.
12034**
12035** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
12036** (which can only happen if a foreign key constraint is violated), the
12037** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12038** passed as the second argument. This includes the case where the DELETE
12039** operation is attempted because an earlier call to the conflict handler
12040** function returned [SQLITE_CHANGESET_REPLACE].
12041**
12042** <dt>INSERT Changes<dd>
12043** For each INSERT change, an attempt is made to insert the new row into
12044** the database. If the changeset row contains fewer fields than the
12045** database table, the trailing fields are populated with their default
12046** values.
12047**
12048** If the attempt to insert the row fails because the database already
12049** contains a row with the same primary key values, the conflict handler
12050** function is invoked with the second argument set to
12051** [SQLITE_CHANGESET_CONFLICT].
12052**
12053** If the attempt to insert the row fails because of some other constraint
12054** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
12055** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
12056** This includes the case where the INSERT operation is re-attempted because
12057** an earlier call to the conflict handler function returned
12058** [SQLITE_CHANGESET_REPLACE].
12059**
12060** <dt>UPDATE Changes<dd>
12061** For each UPDATE change, the function checks if the target database
12062** contains a row with the same primary key value (or values) as the
12063** original row values stored in the changeset. If it does, and the values
12064** stored in all modified non-primary key columns also match the values
12065** stored in the changeset the row is updated within the target database.
12066**
12067** If a row with matching primary key values is found, but one or more of
12068** the modified non-primary key fields contains a value different from an
12069** original row value stored in the changeset, the conflict-handler function
12070** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
12071** UPDATE changes only contain values for non-primary key fields that are
12072** to be modified, only those fields need to match the original values to
12073** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12074**
12075** If no row with matching primary key values is found in the database,
12076** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12077** passed as the second argument.
12078**
12079** If the UPDATE operation is attempted, but SQLite returns
12080** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12081** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12082** This includes the case where the UPDATE operation is attempted after
12083** an earlier call to the conflict handler function returned
12084** [SQLITE_CHANGESET_REPLACE].
12085** </dl>
12086**
12087** It is safe to execute SQL statements, including those that write to the
12088** table that the callback related to, from within the xConflict callback.
12089** This can be used to further customize the application's conflict
12090** resolution strategy.
12091**
12092** All changes made by these functions are enclosed in a savepoint transaction.
12093** If any other error (aside from a constraint failure when attempting to
12094** write to the target database) occurs, then the savepoint transaction is
12095** rolled back, restoring the target database to its original state, and an
12096** SQLite error code returned.
12097**
12098** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12099** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12100** may set (*ppRebase) to point to a "rebase" that may be used with the
12101** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12102** is set to the size of the buffer in bytes. It is the responsibility of the
12103** caller to eventually free any such buffer using sqlite3_free(). The buffer
12104** is only allocated and populated if one or more conflicts were encountered
12105** while applying the patchset. See comments surrounding the sqlite3_rebaser
12106** APIs for further details.
12107**
12108** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12109** may be modified by passing a combination of
12110** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12111**
12112** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12113** and therefore subject to change.
12114*/
12115SQLITE_API int sqlite3changeset_apply(
12116 sqlite3 *db, /* Apply change to "main" db of this handle */
12117 int nChangeset, /* Size of changeset in bytes */
12118 void *pChangeset, /* Changeset blob */
12119 int(*xFilter)(
12120 void *pCtx, /* Copy of sixth arg to _apply() */
12121 const char *zTab /* Table name */
12122 ),
12123 int(*xConflict)(
12124 void *pCtx, /* Copy of sixth arg to _apply() */
12125 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12126 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12127 ),
12128 void *pCtx /* First argument passed to xConflict */
12129);
12130SQLITE_API int sqlite3changeset_apply_v2(
12131 sqlite3 *db, /* Apply change to "main" db of this handle */
12132 int nChangeset, /* Size of changeset in bytes */
12133 void *pChangeset, /* Changeset blob */
12134 int(*xFilter)(
12135 void *pCtx, /* Copy of sixth arg to _apply() */
12136 const char *zTab /* Table name */
12137 ),
12138 int(*xConflict)(
12139 void *pCtx, /* Copy of sixth arg to _apply() */
12140 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12141 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12142 ),
12143 void *pCtx, /* First argument passed to xConflict */
12144 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12145 int flags /* SESSION_CHANGESETAPPLY_* flags */
12146);
12147
12148/*
12149** CAPI3REF: Flags for sqlite3changeset_apply_v2
12150**
12151** The following flags may passed via the 9th parameter to
12152** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12153**
12154** <dl>
12155** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12156** Usually, the sessions module encloses all operations performed by
12157** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12158** SAVEPOINT is committed if the changeset or patchset is successfully
12159** applied, or rolled back if an error occurs. Specifying this flag
12160** causes the sessions module to omit this savepoint. In this case, if the
12161** caller has an open transaction or savepoint when apply_v2() is called,
12162** it may revert the partially applied changeset by rolling it back.
12163**
12164** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12165** Invert the changeset before applying it. This is equivalent to inverting
12166** a changeset using sqlite3changeset_invert() before applying it. It is
12167** an error to specify this flag with a patchset.
12168*/
12169#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
12170#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
12171
12172/*
12173** CAPI3REF: Constants Passed To The Conflict Handler
12174**
12175** Values that may be passed as the second argument to a conflict-handler.
12176**
12177** <dl>
12178** <dt>SQLITE_CHANGESET_DATA<dd>
12179** The conflict handler is invoked with CHANGESET_DATA as the second argument
12180** when processing a DELETE or UPDATE change if a row with the required
12181** PRIMARY KEY fields is present in the database, but one or more other
12182** (non primary-key) fields modified by the update do not contain the
12183** expected "before" values.
12184**
12185** The conflicting row, in this case, is the database row with the matching
12186** primary key.
12187**
12188** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12189** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12190** argument when processing a DELETE or UPDATE change if a row with the
12191** required PRIMARY KEY fields is not present in the database.
12192**
12193** There is no conflicting row in this case. The results of invoking the
12194** sqlite3changeset_conflict() API are undefined.
12195**
12196** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12197** CHANGESET_CONFLICT is passed as the second argument to the conflict
12198** handler while processing an INSERT change if the operation would result
12199** in duplicate primary key values.
12200**
12201** The conflicting row in this case is the database row with the matching
12202** primary key.
12203**
12204** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12205** If foreign key handling is enabled, and applying a changeset leaves the
12206** database in a state containing foreign key violations, the conflict
12207** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12208** exactly once before the changeset is committed. If the conflict handler
12209** returns CHANGESET_OMIT, the changes, including those that caused the
12210** foreign key constraint violation, are committed. Or, if it returns
12211** CHANGESET_ABORT, the changeset is rolled back.
12212**
12213** No current or conflicting row information is provided. The only function
12214** it is possible to call on the supplied sqlite3_changeset_iter handle
12215** is sqlite3changeset_fk_conflicts().
12216**
12217** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12218** If any other constraint violation occurs while applying a change (i.e.
12219** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12220** invoked with CHANGESET_CONSTRAINT as the second argument.
12221**
12222** There is no conflicting row in this case. The results of invoking the
12223** sqlite3changeset_conflict() API are undefined.
12224**
12225** </dl>
12226*/
12227#define SQLITE_CHANGESET_DATA 1
12228#define SQLITE_CHANGESET_NOTFOUND 2
12229#define SQLITE_CHANGESET_CONFLICT 3
12230#define SQLITE_CHANGESET_CONSTRAINT 4
12231#define SQLITE_CHANGESET_FOREIGN_KEY 5
12232
12233/*
12234** CAPI3REF: Constants Returned By The Conflict Handler
12235**
12236** A conflict handler callback must return one of the following three values.
12237**
12238** <dl>
12239** <dt>SQLITE_CHANGESET_OMIT<dd>
12240** If a conflict handler returns this value no special action is taken. The
12241** change that caused the conflict is not applied. The session module
12242** continues to the next change in the changeset.
12243**
12244** <dt>SQLITE_CHANGESET_REPLACE<dd>
12245** This value may only be returned if the second argument to the conflict
12246** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12247** is not the case, any changes applied so far are rolled back and the
12248** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12249**
12250** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12251** handler, then the conflicting row is either updated or deleted, depending
12252** on the type of change.
12253**
12254** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12255** handler, then the conflicting row is removed from the database and a
12256** second attempt to apply the change is made. If this second attempt fails,
12257** the original row is restored to the database before continuing.
12258**
12259** <dt>SQLITE_CHANGESET_ABORT<dd>
12260** If this value is returned, any changes applied so far are rolled back
12261** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12262** </dl>
12263*/
12264#define SQLITE_CHANGESET_OMIT 0
12265#define SQLITE_CHANGESET_REPLACE 1
12266#define SQLITE_CHANGESET_ABORT 2
12267
12268/*
12269** CAPI3REF: Rebasing changesets
12270** EXPERIMENTAL
12271**
12272** Suppose there is a site hosting a database in state S0. And that
12273** modifications are made that move that database to state S1 and a
12274** changeset recorded (the "local" changeset). Then, a changeset based
12275** on S0 is received from another site (the "remote" changeset) and
12276** applied to the database. The database is then in state
12277** (S1+"remote"), where the exact state depends on any conflict
12278** resolution decisions (OMIT or REPLACE) made while applying "remote".
12279** Rebasing a changeset is to update it to take those conflict
12280** resolution decisions into account, so that the same conflicts
12281** do not have to be resolved elsewhere in the network.
12282**
12283** For example, if both the local and remote changesets contain an
12284** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12285**
12286** local: INSERT INTO t1 VALUES(1, 'v1');
12287** remote: INSERT INTO t1 VALUES(1, 'v2');
12288**
12289** and the conflict resolution is REPLACE, then the INSERT change is
12290** removed from the local changeset (it was overridden). Or, if the
12291** conflict resolution was "OMIT", then the local changeset is modified
12292** to instead contain:
12293**
12294** UPDATE t1 SET b = 'v2' WHERE a=1;
12295**
12296** Changes within the local changeset are rebased as follows:
12297**
12298** <dl>
12299** <dt>Local INSERT<dd>
12300** This may only conflict with a remote INSERT. If the conflict
12301** resolution was OMIT, then add an UPDATE change to the rebased
12302** changeset. Or, if the conflict resolution was REPLACE, add
12303** nothing to the rebased changeset.
12304**
12305** <dt>Local DELETE<dd>
12306** This may conflict with a remote UPDATE or DELETE. In both cases the
12307** only possible resolution is OMIT. If the remote operation was a
12308** DELETE, then add no change to the rebased changeset. If the remote
12309** operation was an UPDATE, then the old.* fields of change are updated
12310** to reflect the new.* values in the UPDATE.
12311**
12312** <dt>Local UPDATE<dd>
12313** This may conflict with a remote UPDATE or DELETE. If it conflicts
12314** with a DELETE, and the conflict resolution was OMIT, then the update
12315** is changed into an INSERT. Any undefined values in the new.* record
12316** from the update change are filled in using the old.* values from
12317** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
12318** the UPDATE change is simply omitted from the rebased changeset.
12319**
12320** If conflict is with a remote UPDATE and the resolution is OMIT, then
12321** the old.* values are rebased using the new.* values in the remote
12322** change. Or, if the resolution is REPLACE, then the change is copied
12323** into the rebased changeset with updates to columns also updated by
12324** the conflicting remote UPDATE removed. If this means no columns would
12325** be updated, the change is omitted.
12326** </dl>
12327**
12328** A local change may be rebased against multiple remote changes
12329** simultaneously. If a single key is modified by multiple remote
12330** changesets, they are combined as follows before the local changeset
12331** is rebased:
12332**
12333** <ul>
12334** <li> If there has been one or more REPLACE resolutions on a
12335** key, it is rebased according to a REPLACE.
12336**
12337** <li> If there have been no REPLACE resolutions on a key, then
12338** the local changeset is rebased according to the most recent
12339** of the OMIT resolutions.
12340** </ul>
12341**
12342** Note that conflict resolutions from multiple remote changesets are
12343** combined on a per-field basis, not per-row. This means that in the
12344** case of multiple remote UPDATE operations, some fields of a single
12345** local change may be rebased for REPLACE while others are rebased for
12346** OMIT.
12347**
12348** In order to rebase a local changeset, the remote changeset must first
12349** be applied to the local database using sqlite3changeset_apply_v2() and
12350** the buffer of rebase information captured. Then:
12351**
12352** <ol>
12353** <li> An sqlite3_rebaser object is created by calling
12354** sqlite3rebaser_create().
12355** <li> The new object is configured with the rebase buffer obtained from
12356** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
12357** If the local changeset is to be rebased against multiple remote
12358** changesets, then sqlite3rebaser_configure() should be called
12359** multiple times, in the same order that the multiple
12360** sqlite3changeset_apply_v2() calls were made.
12361** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
12362** <li> The sqlite3_rebaser object is deleted by calling
12363** sqlite3rebaser_delete().
12364** </ol>
12365*/
12366typedef struct sqlite3_rebaser sqlite3_rebaser;
12367
12368/*
12369** CAPI3REF: Create a changeset rebaser object.
12370** EXPERIMENTAL
12371**
12372** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
12373** point to the new object and return SQLITE_OK. Otherwise, if an error
12374** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
12375** to NULL.
12376*/
12377SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12378
12379/*
12380** CAPI3REF: Configure a changeset rebaser object.
12381** EXPERIMENTAL
12382**
12383** Configure the changeset rebaser object to rebase changesets according
12384** to the conflict resolutions described by buffer pRebase (size nRebase
12385** bytes), which must have been obtained from a previous call to
12386** sqlite3changeset_apply_v2().
12387*/
12388SQLITE_API int sqlite3rebaser_configure(
12389 sqlite3_rebaser*,
12390 int nRebase, const void *pRebase
12391);
12392
12393/*
12394** CAPI3REF: Rebase a changeset
12395** EXPERIMENTAL
12396**
12397** Argument pIn must point to a buffer containing a changeset nIn bytes
12398** in size. This function allocates and populates a buffer with a copy
12399** of the changeset rebased according to the configuration of the
12400** rebaser object passed as the first argument. If successful, (*ppOut)
12401** is set to point to the new buffer containing the rebased changeset and
12402** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12403** responsibility of the caller to eventually free the new buffer using
12404** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12405** are set to zero and an SQLite error code returned.
12406*/
12407SQLITE_API int sqlite3rebaser_rebase(
12408 sqlite3_rebaser*,
12409 int nIn, const void *pIn,
12410 int *pnOut, void **ppOut
12411);
12412
12413/*
12414** CAPI3REF: Delete a changeset rebaser object.
12415** EXPERIMENTAL
12416**
12417** Delete the changeset rebaser object and all associated resources. There
12418** should be one call to this function for each successful invocation
12419** of sqlite3rebaser_create().
12420*/
12421SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12422
12423/*
12424** CAPI3REF: Streaming Versions of API functions.
12425**
12426** The six streaming API xxx_strm() functions serve similar purposes to the
12427** corresponding non-streaming API functions:
12428**
12429** <table border=1 style="margin-left:8ex;margin-right:8ex">
12430** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12431** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12432** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12433** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12434** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12435** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12436** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12437** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12438** </table>
12439**
12440** Non-streaming functions that accept changesets (or patchsets) as input
12441** require that the entire changeset be stored in a single buffer in memory.
12442** Similarly, those that return a changeset or patchset do so by returning
12443** a pointer to a single large buffer allocated using sqlite3_malloc().
12444** Normally this is convenient. However, if an application running in a
12445** low-memory environment is required to handle very large changesets, the
12446** large contiguous memory allocations required can become onerous.
12447**
12448** In order to avoid this problem, instead of a single large buffer, input
12449** is passed to a streaming API functions by way of a callback function that
12450** the sessions module invokes to incrementally request input data as it is
12451** required. In all cases, a pair of API function parameters such as
12452**
12453** <pre>
12454** &nbsp; int nChangeset,
12455** &nbsp; void *pChangeset,
12456** </pre>
12457**
12458** Is replaced by:
12459**
12460** <pre>
12461** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12462** &nbsp; void *pIn,
12463** </pre>
12464**
12465** Each time the xInput callback is invoked by the sessions module, the first
12466** argument passed is a copy of the supplied pIn context pointer. The second
12467** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12468** error occurs the xInput method should copy up to (*pnData) bytes of data
12469** into the buffer and set (*pnData) to the actual number of bytes copied
12470** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12471** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12472** error code should be returned. In all cases, if an xInput callback returns
12473** an error, all processing is abandoned and the streaming API function
12474** returns a copy of the error code to the caller.
12475**
12476** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12477** invoked by the sessions module at any point during the lifetime of the
12478** iterator. If such an xInput callback returns an error, the iterator enters
12479** an error state, whereby all subsequent calls to iterator functions
12480** immediately fail with the same error code as returned by xInput.
12481**
12482** Similarly, streaming API functions that return changesets (or patchsets)
12483** return them in chunks by way of a callback function instead of via a
12484** pointer to a single large buffer. In this case, a pair of parameters such
12485** as:
12486**
12487** <pre>
12488** &nbsp; int *pnChangeset,
12489** &nbsp; void **ppChangeset,
12490** </pre>
12491**
12492** Is replaced by:
12493**
12494** <pre>
12495** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12496** &nbsp; void *pOut
12497** </pre>
12498**
12499** The xOutput callback is invoked zero or more times to return data to
12500** the application. The first parameter passed to each call is a copy of the
12501** pOut pointer supplied by the application. The second parameter, pData,
12502** points to a buffer nData bytes in size containing the chunk of output
12503** data being returned. If the xOutput callback successfully processes the
12504** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12505** it should return some other SQLite error code. In this case processing
12506** is immediately abandoned and the streaming API function returns a copy
12507** of the xOutput error code to the application.
12508**
12509** The sessions module never invokes an xOutput callback with the third
12510** parameter set to a value less than or equal to zero. Other than this,
12511** no guarantees are made as to the size of the chunks of data returned.
12512*/
12513SQLITE_API int sqlite3changeset_apply_strm(
12514 sqlite3 *db, /* Apply change to "main" db of this handle */
12515 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12516 void *pIn, /* First arg for xInput */
12517 int(*xFilter)(
12518 void *pCtx, /* Copy of sixth arg to _apply() */
12519 const char *zTab /* Table name */
12520 ),
12521 int(*xConflict)(
12522 void *pCtx, /* Copy of sixth arg to _apply() */
12523 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12524 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12525 ),
12526 void *pCtx /* First argument passed to xConflict */
12527);
12528SQLITE_API int sqlite3changeset_apply_v2_strm(
12529 sqlite3 *db, /* Apply change to "main" db of this handle */
12530 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12531 void *pIn, /* First arg for xInput */
12532 int(*xFilter)(
12533 void *pCtx, /* Copy of sixth arg to _apply() */
12534 const char *zTab /* Table name */
12535 ),
12536 int(*xConflict)(
12537 void *pCtx, /* Copy of sixth arg to _apply() */
12538 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12539 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12540 ),
12541 void *pCtx, /* First argument passed to xConflict */
12542 void **ppRebase, int *pnRebase,
12543 int flags
12544);
12545SQLITE_API int sqlite3changeset_concat_strm(
12546 int (*xInputA)(void *pIn, void *pData, int *pnData),
12547 void *pInA,
12548 int (*xInputB)(void *pIn, void *pData, int *pnData),
12549 void *pInB,
12550 int (*xOutput)(void *pOut, const void *pData, int nData),
12551 void *pOut
12552);
12553SQLITE_API int sqlite3changeset_invert_strm(
12554 int (*xInput)(void *pIn, void *pData, int *pnData),
12555 void *pIn,
12556 int (*xOutput)(void *pOut, const void *pData, int nData),
12557 void *pOut
12558);
12559SQLITE_API int sqlite3changeset_start_strm(
12560 sqlite3_changeset_iter **pp,
12561 int (*xInput)(void *pIn, void *pData, int *pnData),
12562 void *pIn
12563);
12564SQLITE_API int sqlite3changeset_start_v2_strm(
12565 sqlite3_changeset_iter **pp,
12566 int (*xInput)(void *pIn, void *pData, int *pnData),
12567 void *pIn,
12568 int flags
12569);
12570SQLITE_API int sqlite3session_changeset_strm(
12571 sqlite3_session *pSession,
12572 int (*xOutput)(void *pOut, const void *pData, int nData),
12573 void *pOut
12574);
12575SQLITE_API int sqlite3session_patchset_strm(
12576 sqlite3_session *pSession,
12577 int (*xOutput)(void *pOut, const void *pData, int nData),
12578 void *pOut
12579);
12580SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12581 int (*xInput)(void *pIn, void *pData, int *pnData),
12582 void *pIn
12583);
12584SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12585 int (*xOutput)(void *pOut, const void *pData, int nData),
12586 void *pOut
12587);
12588SQLITE_API int sqlite3rebaser_rebase_strm(
12589 sqlite3_rebaser *pRebaser,
12590 int (*xInput)(void *pIn, void *pData, int *pnData),
12591 void *pIn,
12592 int (*xOutput)(void *pOut, const void *pData, int nData),
12593 void *pOut
12594);
12595
12596/*
12597** CAPI3REF: Configure global parameters
12598**
12599** The sqlite3session_config() interface is used to make global configuration
12600** changes to the sessions module in order to tune it to the specific needs
12601** of the application.
12602**
12603** The sqlite3session_config() interface is not threadsafe. If it is invoked
12604** while any other thread is inside any other sessions method then the
12605** results are undefined. Furthermore, if it is invoked after any sessions
12606** related objects have been created, the results are also undefined.
12607**
12608** The first argument to the sqlite3session_config() function must be one
12609** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12610** interpretation of the (void*) value passed as the second parameter and
12611** the effect of calling this function depends on the value of the first
12612** parameter.
12613**
12614** <dl>
12615** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12616** By default, the sessions module streaming interfaces attempt to input
12617** and output data in approximately 1 KiB chunks. This operand may be used
12618** to set and query the value of this configuration setting. The pointer
12619** passed as the second argument must point to a value of type (int).
12620** If this value is greater than 0, it is used as the new streaming data
12621** chunk size for both input and output. Before returning, the (int) value
12622** pointed to by pArg is set to the final value of the streaming interface
12623** chunk size.
12624** </dl>
12625**
12626** This function returns SQLITE_OK if successful, or an SQLite error code
12627** otherwise.
12628*/
12629SQLITE_API int sqlite3session_config(int op, void *pArg);
12630
12631/*
12632** CAPI3REF: Values for sqlite3session_config().
12633*/
12634#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12635
12636/*
12637** Make sure we can call this stuff from C++.
12638*/
12639#if 0
12640}
12641#endif
12642
12643#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12644
12645/******** End of sqlite3session.h *********/
12646/******** Begin file fts5.h *********/
12647/*
12648** 2014 May 31
12649**
12650** The author disclaims copyright to this source code. In place of
12651** a legal notice, here is a blessing:
12652**
12653** May you do good and not evil.
12654** May you find forgiveness for yourself and forgive others.
12655** May you share freely, never taking more than you give.
12656**
12657******************************************************************************
12658**
12659** Interfaces to extend FTS5. Using the interfaces defined in this file,
12660** FTS5 may be extended with:
12661**
12662** * custom tokenizers, and
12663** * custom auxiliary functions.
12664*/
12665
12666
12667#ifndef _FTS5_H
12668#define _FTS5_H
12669
12670
12671#if 0
12672extern "C" {
12673#endif
12674
12675/*************************************************************************
12676** CUSTOM AUXILIARY FUNCTIONS
12677**
12678** Virtual table implementations may overload SQL functions by implementing
12679** the sqlite3_module.xFindFunction() method.
12680*/
12681
12682typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12683typedef struct Fts5Context Fts5Context;
12684typedef struct Fts5PhraseIter Fts5PhraseIter;
12685
12686typedef void (*fts5_extension_function)(
12687 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12688 Fts5Context *pFts, /* First arg to pass to pApi functions */
12689 sqlite3_context *pCtx, /* Context for returning result/error */
12690 int nVal, /* Number of values in apVal[] array */
12691 sqlite3_value **apVal /* Array of trailing arguments */
12692);
12693
12694struct Fts5PhraseIter {
12695 const unsigned char *a;
12696 const unsigned char *b;
12697};
12698
12699/*
12700** EXTENSION API FUNCTIONS
12701**
12702** xUserData(pFts):
12703** Return a copy of the context pointer the extension function was
12704** registered with.
12705**
12706** xColumnTotalSize(pFts, iCol, pnToken):
12707** If parameter iCol is less than zero, set output variable *pnToken
12708** to the total number of tokens in the FTS5 table. Or, if iCol is
12709** non-negative but less than the number of columns in the table, return
12710** the total number of tokens in column iCol, considering all rows in
12711** the FTS5 table.
12712**
12713** If parameter iCol is greater than or equal to the number of columns
12714** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12715** an OOM condition or IO error), an appropriate SQLite error code is
12716** returned.
12717**
12718** xColumnCount(pFts):
12719** Return the number of columns in the table.
12720**
12721** xColumnSize(pFts, iCol, pnToken):
12722** If parameter iCol is less than zero, set output variable *pnToken
12723** to the total number of tokens in the current row. Or, if iCol is
12724** non-negative but less than the number of columns in the table, set
12725** *pnToken to the number of tokens in column iCol of the current row.
12726**
12727** If parameter iCol is greater than or equal to the number of columns
12728** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12729** an OOM condition or IO error), an appropriate SQLite error code is
12730** returned.
12731**
12732** This function may be quite inefficient if used with an FTS5 table
12733** created with the "columnsize=0" option.
12734**
12735** xColumnText:
12736** This function attempts to retrieve the text of column iCol of the
12737** current document. If successful, (*pz) is set to point to a buffer
12738** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12739** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12740** if an error occurs, an SQLite error code is returned and the final values
12741** of (*pz) and (*pn) are undefined.
12742**
12743** xPhraseCount:
12744** Returns the number of phrases in the current query expression.
12745**
12746** xPhraseSize:
12747** Returns the number of tokens in phrase iPhrase of the query. Phrases
12748** are numbered starting from zero.
12749**
12750** xInstCount:
12751** Set *pnInst to the total number of occurrences of all phrases within
12752** the query within the current row. Return SQLITE_OK if successful, or
12753** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12754**
12755** This API can be quite slow if used with an FTS5 table created with the
12756** "detail=none" or "detail=column" option. If the FTS5 table is created
12757** with either "detail=none" or "detail=column" and "content=" option
12758** (i.e. if it is a contentless table), then this API always returns 0.
12759**
12760** xInst:
12761** Query for the details of phrase match iIdx within the current row.
12762** Phrase matches are numbered starting from zero, so the iIdx argument
12763** should be greater than or equal to zero and smaller than the value
12764** output by xInstCount().
12765**
12766** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12767** to the column in which it occurs and *piOff the token offset of the
12768** first token of the phrase. Returns SQLITE_OK if successful, or an error
12769** code (i.e. SQLITE_NOMEM) if an error occurs.
12770**
12771** This API can be quite slow if used with an FTS5 table created with the
12772** "detail=none" or "detail=column" option.
12773**
12774** xRowid:
12775** Returns the rowid of the current row.
12776**
12777** xTokenize:
12778** Tokenize text using the tokenizer belonging to the FTS5 table.
12779**
12780** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12781** This API function is used to query the FTS table for phrase iPhrase
12782** of the current query. Specifically, a query equivalent to:
12783**
12784** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12785**
12786** with $p set to a phrase equivalent to the phrase iPhrase of the
12787** current query is executed. Any column filter that applies to
12788** phrase iPhrase of the current query is included in $p. For each
12789** row visited, the callback function passed as the fourth argument
12790** is invoked. The context and API objects passed to the callback
12791** function may be used to access the properties of each matched row.
12792** Invoking Api.xUserData() returns a copy of the pointer passed as
12793** the third argument to pUserData.
12794**
12795** If the callback function returns any value other than SQLITE_OK, the
12796** query is abandoned and the xQueryPhrase function returns immediately.
12797** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12798** Otherwise, the error code is propagated upwards.
12799**
12800** If the query runs to completion without incident, SQLITE_OK is returned.
12801** Or, if some error occurs before the query completes or is aborted by
12802** the callback, an SQLite error code is returned.
12803**
12804**
12805** xSetAuxdata(pFts5, pAux, xDelete)
12806**
12807** Save the pointer passed as the second argument as the extension function's
12808** "auxiliary data". The pointer may then be retrieved by the current or any
12809** future invocation of the same fts5 extension function made as part of
12810** the same MATCH query using the xGetAuxdata() API.
12811**
12812** Each extension function is allocated a single auxiliary data slot for
12813** each FTS query (MATCH expression). If the extension function is invoked
12814** more than once for a single FTS query, then all invocations share a
12815** single auxiliary data context.
12816**
12817** If there is already an auxiliary data pointer when this function is
12818** invoked, then it is replaced by the new pointer. If an xDelete callback
12819** was specified along with the original pointer, it is invoked at this
12820** point.
12821**
12822** The xDelete callback, if one is specified, is also invoked on the
12823** auxiliary data pointer after the FTS5 query has finished.
12824**
12825** If an error (e.g. an OOM condition) occurs within this function,
12826** the auxiliary data is set to NULL and an error code returned. If the
12827** xDelete parameter was not NULL, it is invoked on the auxiliary data
12828** pointer before returning.
12829**
12830**
12831** xGetAuxdata(pFts5, bClear)
12832**
12833** Returns the current auxiliary data pointer for the fts5 extension
12834** function. See the xSetAuxdata() method for details.
12835**
12836** If the bClear argument is non-zero, then the auxiliary data is cleared
12837** (set to NULL) before this function returns. In this case the xDelete,
12838** if any, is not invoked.
12839**
12840**
12841** xRowCount(pFts5, pnRow)
12842**
12843** This function is used to retrieve the total number of rows in the table.
12844** In other words, the same value that would be returned by:
12845**
12846** SELECT count(*) FROM ftstable;
12847**
12848** xPhraseFirst()
12849** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12850** method, to iterate through all instances of a single query phrase within
12851** the current row. This is the same information as is accessible via the
12852** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12853** to use, this API may be faster under some circumstances. To iterate
12854** through instances of phrase iPhrase, use the following code:
12855**
12856** Fts5PhraseIter iter;
12857** int iCol, iOff;
12858** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12859** iCol>=0;
12860** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12861** ){
12862** // An instance of phrase iPhrase at offset iOff of column iCol
12863** }
12864**
12865** The Fts5PhraseIter structure is defined above. Applications should not
12866** modify this structure directly - it should only be used as shown above
12867** with the xPhraseFirst() and xPhraseNext() API methods (and by
12868** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12869**
12870** This API can be quite slow if used with an FTS5 table created with the
12871** "detail=none" or "detail=column" option. If the FTS5 table is created
12872** with either "detail=none" or "detail=column" and "content=" option
12873** (i.e. if it is a contentless table), then this API always iterates
12874** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12875**
12876** xPhraseNext()
12877** See xPhraseFirst above.
12878**
12879** xPhraseFirstColumn()
12880** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12881** and xPhraseNext() APIs described above. The difference is that instead
12882** of iterating through all instances of a phrase in the current row, these
12883** APIs are used to iterate through the set of columns in the current row
12884** that contain one or more instances of a specified phrase. For example:
12885**
12886** Fts5PhraseIter iter;
12887** int iCol;
12888** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12889** iCol>=0;
12890** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12891** ){
12892** // Column iCol contains at least one instance of phrase iPhrase
12893** }
12894**
12895** This API can be quite slow if used with an FTS5 table created with the
12896** "detail=none" option. If the FTS5 table is created with either
12897** "detail=none" "content=" option (i.e. if it is a contentless table),
12898** then this API always iterates through an empty set (all calls to
12899** xPhraseFirstColumn() set iCol to -1).
12900**
12901** The information accessed using this API and its companion
12902** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12903** (or xInst/xInstCount). The chief advantage of this API is that it is
12904** significantly more efficient than those alternatives when used with
12905** "detail=column" tables.
12906**
12907** xPhraseNextColumn()
12908** See xPhraseFirstColumn above.
12909*/
12910struct Fts5ExtensionApi {
12911 int iVersion; /* Currently always set to 3 */
12912
12913 void *(*xUserData)(Fts5Context*);
12914
12915 int (*xColumnCount)(Fts5Context*);
12916 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12917 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12918
12919 int (*xTokenize)(Fts5Context*,
12920 const char *pText, int nText, /* Text to tokenize */
12921 void *pCtx, /* Context passed to xToken() */
12922 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12923 );
12924
12925 int (*xPhraseCount)(Fts5Context*);
12926 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12927
12928 int (*xInstCount)(Fts5Context*, int *pnInst);
12929 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12930
12931 sqlite3_int64 (*xRowid)(Fts5Context*);
12932 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12933 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12934
12935 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12936 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12937 );
12938 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12939 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12940
12941 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12942 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12943
12944 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12945 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12946};
12947
12948/*
12949** CUSTOM AUXILIARY FUNCTIONS
12950*************************************************************************/
12951
12952/*************************************************************************
12953** CUSTOM TOKENIZERS
12954**
12955** Applications may also register custom tokenizer types. A tokenizer
12956** is registered by providing fts5 with a populated instance of the
12957** following structure. All structure methods must be defined, setting
12958** any member of the fts5_tokenizer struct to NULL leads to undefined
12959** behaviour. The structure methods are expected to function as follows:
12960**
12961** xCreate:
12962** This function is used to allocate and initialize a tokenizer instance.
12963** A tokenizer instance is required to actually tokenize text.
12964**
12965** The first argument passed to this function is a copy of the (void*)
12966** pointer provided by the application when the fts5_tokenizer object
12967** was registered with FTS5 (the third argument to xCreateTokenizer()).
12968** The second and third arguments are an array of nul-terminated strings
12969** containing the tokenizer arguments, if any, specified following the
12970** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12971** to create the FTS5 table.
12972**
12973** The final argument is an output variable. If successful, (*ppOut)
12974** should be set to point to the new tokenizer handle and SQLITE_OK
12975** returned. If an error occurs, some value other than SQLITE_OK should
12976** be returned. In this case, fts5 assumes that the final value of *ppOut
12977** is undefined.
12978**
12979** xDelete:
12980** This function is invoked to delete a tokenizer handle previously
12981** allocated using xCreate(). Fts5 guarantees that this function will
12982** be invoked exactly once for each successful call to xCreate().
12983**
12984** xTokenize:
12985** This function is expected to tokenize the nText byte string indicated
12986** by argument pText. pText may or may not be nul-terminated. The first
12987** argument passed to this function is a pointer to an Fts5Tokenizer object
12988** returned by an earlier call to xCreate().
12989**
12990** The second argument indicates the reason that FTS5 is requesting
12991** tokenization of the supplied text. This is always one of the following
12992** four values:
12993**
12994** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12995** or removed from the FTS table. The tokenizer is being invoked to
12996** determine the set of tokens to add to (or delete from) the
12997** FTS index.
12998**
12999** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13000** against the FTS index. The tokenizer is being called to tokenize
13001** a bareword or quoted string specified as part of the query.
13002**
13003** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13004** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
13005** followed by a "*" character, indicating that the last token
13006** returned by the tokenizer will be treated as a token prefix.
13007**
13008** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13009** satisfy an fts5_api.xTokenize() request made by an auxiliary
13010** function. Or an fts5_api.xColumnSize() request made by the same
13011** on a columnsize=0 database.
13012** </ul>
13013**
13014** For each token in the input string, the supplied callback xToken() must
13015** be invoked. The first argument to it should be a copy of the pointer
13016** passed as the second argument to xTokenize(). The third and fourth
13017** arguments are a pointer to a buffer containing the token text, and the
13018** size of the token in bytes. The 4th and 5th arguments are the byte offsets
13019** of the first byte of and first byte immediately following the text from
13020** which the token is derived within the input.
13021**
13022** The second argument passed to the xToken() callback ("tflags") should
13023** normally be set to 0. The exception is if the tokenizer supports
13024** synonyms. In this case see the discussion below for details.
13025**
13026** FTS5 assumes the xToken() callback is invoked for each token in the
13027** order that they occur within the input text.
13028**
13029** If an xToken() callback returns any value other than SQLITE_OK, then
13030** the tokenization should be abandoned and the xTokenize() method should
13031** immediately return a copy of the xToken() return value. Or, if the
13032** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
13033** if an error occurs with the xTokenize() implementation itself, it
13034** may abandon the tokenization and return any error code other than
13035** SQLITE_OK or SQLITE_DONE.
13036**
13037** SYNONYM SUPPORT
13038**
13039** Custom tokenizers may also support synonyms. Consider a case in which a
13040** user wishes to query for a phrase such as "first place". Using the
13041** built-in tokenizers, the FTS5 query 'first + place' will match instances
13042** of "first place" within the document set, but not alternative forms
13043** such as "1st place". In some applications, it would be better to match
13044** all instances of "first place" or "1st place" regardless of which form
13045** the user specified in the MATCH query text.
13046**
13047** There are several ways to approach this in FTS5:
13048**
13049** <ol><li> By mapping all synonyms to a single token. In this case, using
13050** the above example, this means that the tokenizer returns the
13051** same token for inputs "first" and "1st". Say that token is in
13052** fact "first", so that when the user inserts the document "I won
13053** 1st place" entries are added to the index for tokens "i", "won",
13054** "first" and "place". If the user then queries for '1st + place',
13055** the tokenizer substitutes "first" for "1st" and the query works
13056** as expected.
13057**
13058** <li> By querying the index for all synonyms of each query term
13059** separately. In this case, when tokenizing query text, the
13060** tokenizer may provide multiple synonyms for a single term
13061** within the document. FTS5 then queries the index for each
13062** synonym individually. For example, faced with the query:
13063**
13064** <codeblock>
13065** ... MATCH 'first place'</codeblock>
13066**
13067** the tokenizer offers both "1st" and "first" as synonyms for the
13068** first token in the MATCH query and FTS5 effectively runs a query
13069** similar to:
13070**
13071** <codeblock>
13072** ... MATCH '(first OR 1st) place'</codeblock>
13073**
13074** except that, for the purposes of auxiliary functions, the query
13075** still appears to contain just two phrases - "(first OR 1st)"
13076** being treated as a single phrase.
13077**
13078** <li> By adding multiple synonyms for a single term to the FTS index.
13079** Using this method, when tokenizing document text, the tokenizer
13080** provides multiple synonyms for each token. So that when a
13081** document such as "I won first place" is tokenized, entries are
13082** added to the FTS index for "i", "won", "first", "1st" and
13083** "place".
13084**
13085** This way, even if the tokenizer does not provide synonyms
13086** when tokenizing query text (it should not - to do so would be
13087** inefficient), it doesn't matter if the user queries for
13088** 'first + place' or '1st + place', as there are entries in the
13089** FTS index corresponding to both forms of the first token.
13090** </ol>
13091**
13092** Whether it is parsing document or query text, any call to xToken that
13093** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13094** is considered to supply a synonym for the previous token. For example,
13095** when parsing the document "I won first place", a tokenizer that supports
13096** synonyms would call xToken() 5 times, as follows:
13097**
13098** <codeblock>
13099** xToken(pCtx, 0, "i", 1, 0, 1);
13100** xToken(pCtx, 0, "won", 3, 2, 5);
13101** xToken(pCtx, 0, "first", 5, 6, 11);
13102** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
13103** xToken(pCtx, 0, "place", 5, 12, 17);
13104**</codeblock>
13105**
13106** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13107** xToken() is called. Multiple synonyms may be specified for a single token
13108** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13109** There is no limit to the number of synonyms that may be provided for a
13110** single token.
13111**
13112** In many cases, method (1) above is the best approach. It does not add
13113** extra data to the FTS index or require FTS5 to query for multiple terms,
13114** so it is efficient in terms of disk space and query speed. However, it
13115** does not support prefix queries very well. If, as suggested above, the
13116** token "first" is substituted for "1st" by the tokenizer, then the query:
13117**
13118** <codeblock>
13119** ... MATCH '1s*'</codeblock>
13120**
13121** will not match documents that contain the token "1st" (as the tokenizer
13122** will probably not map "1s" to any prefix of "first").
13123**
13124** For full prefix support, method (3) may be preferred. In this case,
13125** because the index contains entries for both "first" and "1st", prefix
13126** queries such as 'fi*' or '1s*' will match correctly. However, because
13127** extra entries are added to the FTS index, this method uses more space
13128** within the database.
13129**
13130** Method (2) offers a midpoint between (1) and (3). Using this method,
13131** a query such as '1s*' will match documents that contain the literal
13132** token "1st", but not "first" (assuming the tokenizer is not able to
13133** provide synonyms for prefixes). However, a non-prefix query like '1st'
13134** will match against "1st" and "first". This method does not require
13135** extra disk space, as no extra entries are added to the FTS index.
13136** On the other hand, it may require more CPU cycles to run MATCH queries,
13137** as separate queries of the FTS index are required for each synonym.
13138**
13139** When using methods (2) or (3), it is important that the tokenizer only
13140** provide synonyms when tokenizing document text (method (2)) or query
13141** text (method (3)), not both. Doing so will not cause any errors, but is
13142** inefficient.
13143*/
13144typedef struct Fts5Tokenizer Fts5Tokenizer;
13145typedef struct fts5_tokenizer fts5_tokenizer;
13146struct fts5_tokenizer {
13147 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13148 void (*xDelete)(Fts5Tokenizer*);
13149 int (*xTokenize)(Fts5Tokenizer*,
13150 void *pCtx,
13151 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13152 const char *pText, int nText,
13153 int (*xToken)(
13154 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13155 int tflags, /* Mask of FTS5_TOKEN_* flags */
13156 const char *pToken, /* Pointer to buffer containing token */
13157 int nToken, /* Size of token in bytes */
13158 int iStart, /* Byte offset of token within input text */
13159 int iEnd /* Byte offset of end of token within input text */
13160 )
13161 );
13162};
13163
13164/* Flags that may be passed as the third argument to xTokenize() */
13165#define FTS5_TOKENIZE_QUERY 0x0001
13166#define FTS5_TOKENIZE_PREFIX 0x0002
13167#define FTS5_TOKENIZE_DOCUMENT 0x0004
13168#define FTS5_TOKENIZE_AUX 0x0008
13169
13170/* Flags that may be passed by the tokenizer implementation back to FTS5
13171** as the third argument to the supplied xToken callback. */
13172#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
13173
13174/*
13175** END OF CUSTOM TOKENIZERS
13176*************************************************************************/
13177
13178/*************************************************************************
13179** FTS5 EXTENSION REGISTRATION API
13180*/
13181typedef struct fts5_api fts5_api;
13182struct fts5_api {
13183 int iVersion; /* Currently always set to 2 */
13184
13185 /* Create a new tokenizer */
13186 int (*xCreateTokenizer)(
13187 fts5_api *pApi,
13188 const char *zName,
13189 void *pContext,
13190 fts5_tokenizer *pTokenizer,
13191 void (*xDestroy)(void*)
13192 );
13193
13194 /* Find an existing tokenizer */
13195 int (*xFindTokenizer)(
13196 fts5_api *pApi,
13197 const char *zName,
13198 void **ppContext,
13199 fts5_tokenizer *pTokenizer
13200 );
13201
13202 /* Create a new auxiliary function */
13203 int (*xCreateFunction)(
13204 fts5_api *pApi,
13205 const char *zName,
13206 void *pContext,
13207 fts5_extension_function xFunction,
13208 void (*xDestroy)(void*)
13209 );
13210};
13211
13212/*
13213** END OF REGISTRATION API
13214*************************************************************************/
13215
13216#if 0
13217} /* end of the 'extern "C"' block */
13218#endif
13219
13220#endif /* _FTS5_H */
13221
13222/******** End of fts5.h *********/
13223
13224/************** End of sqlite3.h *********************************************/
13225/************** Continuing where we left off in sqliteInt.h ******************/
13226
13227/*
13228** Include the configuration header output by 'configure' if we're using the
13229** autoconf-based build
13230*/
13231#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13232/* #include "config.h" */
13233#define SQLITECONFIG_H 1
13234#endif
13235
13236/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13237/************** Begin file sqliteLimit.h *************************************/
13238/*
13239** 2007 May 7
13240**
13241** The author disclaims copyright to this source code. In place of
13242** a legal notice, here is a blessing:
13243**
13244** May you do good and not evil.
13245** May you find forgiveness for yourself and forgive others.
13246** May you share freely, never taking more than you give.
13247**
13248*************************************************************************
13249**
13250** This file defines various limits of what SQLite can process.
13251*/
13252
13253/*
13254** The maximum length of a TEXT or BLOB in bytes. This also
13255** limits the size of a row in a table or index.
13256**
13257** The hard limit is the ability of a 32-bit signed integer
13258** to count the size: 2^31-1 or 2147483647.
13259*/
13260#ifndef SQLITE_MAX_LENGTH
13261# define SQLITE_MAX_LENGTH 1000000000
13262#endif
13263
13264/*
13265** This is the maximum number of
13266**
13267** * Columns in a table
13268** * Columns in an index
13269** * Columns in a view
13270** * Terms in the SET clause of an UPDATE statement
13271** * Terms in the result set of a SELECT statement
13272** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
13273** * Terms in the VALUES clause of an INSERT statement
13274**
13275** The hard upper limit here is 32676. Most database people will
13276** tell you that in a well-normalized database, you usually should
13277** not have more than a dozen or so columns in any table. And if
13278** that is the case, there is no point in having more than a few
13279** dozen values in any of the other situations described above.
13280*/
13281#ifndef SQLITE_MAX_COLUMN
13282# define SQLITE_MAX_COLUMN 2000
13283#endif
13284
13285/*
13286** The maximum length of a single SQL statement in bytes.
13287**
13288** It used to be the case that setting this value to zero would
13289** turn the limit off. That is no longer true. It is not possible
13290** to turn this limit off.
13291*/
13292#ifndef SQLITE_MAX_SQL_LENGTH
13293# define SQLITE_MAX_SQL_LENGTH 1000000000
13294#endif
13295
13296/*
13297** The maximum depth of an expression tree. This is limited to
13298** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
13299** want to place more severe limits on the complexity of an
13300** expression.
13301**
13302** A value of 0 used to mean that the limit was not enforced.
13303** But that is no longer true. The limit is now strictly enforced
13304** at all times.
13305*/
13306#ifndef SQLITE_MAX_EXPR_DEPTH
13307# define SQLITE_MAX_EXPR_DEPTH 1000
13308#endif
13309
13310/*
13311** The maximum number of terms in a compound SELECT statement.
13312** The code generator for compound SELECT statements does one
13313** level of recursion for each term. A stack overflow can result
13314** if the number of terms is too large. In practice, most SQL
13315** never has more than 3 or 4 terms. Use a value of 0 to disable
13316** any limit on the number of terms in a compount SELECT.
13317*/
13318#ifndef SQLITE_MAX_COMPOUND_SELECT
13319# define SQLITE_MAX_COMPOUND_SELECT 500
13320#endif
13321
13322/*
13323** The maximum number of opcodes in a VDBE program.
13324** Not currently enforced.
13325*/
13326#ifndef SQLITE_MAX_VDBE_OP
13327# define SQLITE_MAX_VDBE_OP 250000000
13328#endif
13329
13330/*
13331** The maximum number of arguments to an SQL function.
13332*/
13333#ifndef SQLITE_MAX_FUNCTION_ARG
13334# define SQLITE_MAX_FUNCTION_ARG 127
13335#endif
13336
13337/*
13338** The suggested maximum number of in-memory pages to use for
13339** the main database table and for temporary tables.
13340**
13341** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13342** which means the cache size is limited to 2048000 bytes of memory.
13343** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13344** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13345*/
13346#ifndef SQLITE_DEFAULT_CACHE_SIZE
13347# define SQLITE_DEFAULT_CACHE_SIZE -2000
13348#endif
13349
13350/*
13351** The default number of frames to accumulate in the log file before
13352** checkpointing the database in WAL mode.
13353*/
13354#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
13355# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
13356#endif
13357
13358/*
13359** The maximum number of attached databases. This must be between 0
13360** and 125. The upper bound of 125 is because the attached databases are
13361** counted using a signed 8-bit integer which has a maximum value of 127
13362** and we have to allow 2 extra counts for the "main" and "temp" databases.
13363*/
13364#ifndef SQLITE_MAX_ATTACHED
13365# define SQLITE_MAX_ATTACHED 10
13366#endif
13367
13368
13369/*
13370** The maximum value of a ?nnn wildcard that the parser will accept.
13371** If the value exceeds 32767 then extra space is required for the Expr
13372** structure. But otherwise, we believe that the number can be as large
13373** as a signed 32-bit integer can hold.
13374*/
13375#ifndef SQLITE_MAX_VARIABLE_NUMBER
13376# define SQLITE_MAX_VARIABLE_NUMBER 32766
13377#endif
13378
13379/* Maximum page size. The upper bound on this value is 65536. This a limit
13380** imposed by the use of 16-bit offsets within each page.
13381**
13382** Earlier versions of SQLite allowed the user to change this value at
13383** compile time. This is no longer permitted, on the grounds that it creates
13384** a library that is technically incompatible with an SQLite library
13385** compiled with a different limit. If a process operating on a database
13386** with a page-size of 65536 bytes crashes, then an instance of SQLite
13387** compiled with the default page-size limit will not be able to rollback
13388** the aborted transaction. This could lead to database corruption.
13389*/
13390#ifdef SQLITE_MAX_PAGE_SIZE
13391# undef SQLITE_MAX_PAGE_SIZE
13392#endif
13393#define SQLITE_MAX_PAGE_SIZE 65536
13394
13395
13396/*
13397** The default size of a database page.
13398*/
13399#ifndef SQLITE_DEFAULT_PAGE_SIZE
13400# define SQLITE_DEFAULT_PAGE_SIZE 4096
13401#endif
13402#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13403# undef SQLITE_DEFAULT_PAGE_SIZE
13404# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13405#endif
13406
13407/*
13408** Ordinarily, if no value is explicitly provided, SQLite creates databases
13409** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
13410** device characteristics (sector-size and atomic write() support),
13411** SQLite may choose a larger value. This constant is the maximum value
13412** SQLite will choose on its own.
13413*/
13414#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
13415# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
13416#endif
13417#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
13418# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
13419# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
13420#endif
13421
13422
13423/*
13424** Maximum number of pages in one database file.
13425**
13426** This is really just the default value for the max_page_count pragma.
13427** This value can be lowered (or raised) at run-time using that the
13428** max_page_count macro.
13429*/
13430#ifndef SQLITE_MAX_PAGE_COUNT
13431# define SQLITE_MAX_PAGE_COUNT 1073741823
13432#endif
13433
13434/*
13435** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13436** operator.
13437*/
13438#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
13439# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
13440#endif
13441
13442/*
13443** Maximum depth of recursion for triggers.
13444**
13445** A value of 1 means that a trigger program will not be able to itself
13446** fire any triggers. A value of 0 means that no trigger programs at all
13447** may be executed.
13448*/
13449#ifndef SQLITE_MAX_TRIGGER_DEPTH
13450# define SQLITE_MAX_TRIGGER_DEPTH 1000
13451#endif
13452
13453/************** End of sqliteLimit.h *****************************************/
13454/************** Continuing where we left off in sqliteInt.h ******************/
13455
13456/* Disable nuisance warnings on Borland compilers */
13457#if defined(__BORLANDC__)
13458#pragma warn -rch /* unreachable code */
13459#pragma warn -ccc /* Condition is always true or false */
13460#pragma warn -aus /* Assigned value is never used */
13461#pragma warn -csu /* Comparing signed and unsigned */
13462#pragma warn -spa /* Suspicious pointer arithmetic */
13463#endif
13464
13465/*
13466** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13467** places. The following macros try to make this explicit.
13468*/
13469#ifndef __has_extension
13470# define __has_extension(x) 0 /* compatibility with non-clang compilers */
13471#endif
13472#if GCC_VERSION>=4007000 || __has_extension(c_atomic)
13473# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
13474# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
13475#else
13476# define AtomicLoad(PTR) (*(PTR))
13477# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
13478#endif
13479
13480/*
13481** Include standard header files as necessary
13482*/
13483#ifdef HAVE_STDINT_H
13484#include <stdint.h>
13485#endif
13486#ifdef HAVE_INTTYPES_H
13487#include <inttypes.h>
13488#endif
13489
13490/*
13491** The following macros are used to cast pointers to integers and
13492** integers to pointers. The way you do this varies from one compiler
13493** to the next, so we have developed the following set of #if statements
13494** to generate appropriate macros for a wide range of compilers.
13495**
13496** The correct "ANSI" way to do this is to use the intptr_t type.
13497** Unfortunately, that typedef is not available on all compilers, or
13498** if it is available, it requires an #include of specific headers
13499** that vary from one machine to the next.
13500**
13501** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13502** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
13503** So we have to define the macros in different ways depending on the
13504** compiler.
13505*/
13506#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13507# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
13508# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
13509#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
13510# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
13511# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
13512#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
13513# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
13514# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13515#else /* Generates a warning - but it always works */
13516# define SQLITE_INT_TO_PTR(X) ((void*)(X))
13517# define SQLITE_PTR_TO_INT(X) ((int)(X))
13518#endif
13519
13520/*
13521** A macro to hint to the compiler that a function should not be
13522** inlined.
13523*/
13524#if defined(__GNUC__)
13525# define SQLITE_NOINLINE __attribute__((noinline))
13526#elif defined(_MSC_VER) && _MSC_VER>=1310
13527# define SQLITE_NOINLINE __declspec(noinline)
13528#else
13529# define SQLITE_NOINLINE
13530#endif
13531
13532/*
13533** Make sure that the compiler intrinsics we desire are enabled when
13534** compiling with an appropriate version of MSVC unless prevented by
13535** the SQLITE_DISABLE_INTRINSIC define.
13536*/
13537#if !defined(SQLITE_DISABLE_INTRINSIC)
13538# if defined(_MSC_VER) && _MSC_VER>=1400
13539# if !defined(_WIN32_WCE)
13540# include <intrin.h>
13541# pragma intrinsic(_byteswap_ushort)
13542# pragma intrinsic(_byteswap_ulong)
13543# pragma intrinsic(_byteswap_uint64)
13544# pragma intrinsic(_ReadWriteBarrier)
13545# else
13546# include <cmnintrin.h>
13547# endif
13548# endif
13549#endif
13550
13551/*
13552** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13553** 0 means mutexes are permanently disable and the library is never
13554** threadsafe. 1 means the library is serialized which is the highest
13555** level of threadsafety. 2 means the library is multithreaded - multiple
13556** threads can use SQLite as long as no two threads try to use the same
13557** database connection at the same time.
13558**
13559** Older versions of SQLite used an optional THREADSAFE macro.
13560** We support that for legacy.
13561**
13562** To ensure that the correct value of "THREADSAFE" is reported when querying
13563** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13564** logic is partially replicated in ctime.c. If it is updated here, it should
13565** also be updated there.
13566*/
13567#if !defined(SQLITE_THREADSAFE)
13568# if defined(THREADSAFE)
13569# define SQLITE_THREADSAFE THREADSAFE
13570# else
13571# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13572# endif
13573#endif
13574
13575/*
13576** Powersafe overwrite is on by default. But can be turned off using
13577** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13578*/
13579#ifndef SQLITE_POWERSAFE_OVERWRITE
13580# define SQLITE_POWERSAFE_OVERWRITE 1
13581#endif
13582
13583/*
13584** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13585** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13586** which case memory allocation statistics are disabled by default.
13587*/
13588#if !defined(SQLITE_DEFAULT_MEMSTATUS)
13589# define SQLITE_DEFAULT_MEMSTATUS 1
13590#endif
13591
13592/*
13593** Exactly one of the following macros must be defined in order to
13594** specify which memory allocation subsystem to use.
13595**
13596** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13597** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13598** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13599** SQLITE_MEMDEBUG // Debugging version of system malloc()
13600**
13601** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13602** assert() macro is enabled, each call into the Win32 native heap subsystem
13603** will cause HeapValidate to be called. If heap validation should fail, an
13604** assertion will be triggered.
13605**
13606** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13607** the default.
13608*/
13609#if defined(SQLITE_SYSTEM_MALLOC) \
13610 + defined(SQLITE_WIN32_MALLOC) \
13611 + defined(SQLITE_ZERO_MALLOC) \
13612 + defined(SQLITE_MEMDEBUG)>1
13613# error "Two or more of the following compile-time configuration options\
13614 are defined but at most one is allowed:\
13615 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13616 SQLITE_ZERO_MALLOC"
13617#endif
13618#if defined(SQLITE_SYSTEM_MALLOC) \
13619 + defined(SQLITE_WIN32_MALLOC) \
13620 + defined(SQLITE_ZERO_MALLOC) \
13621 + defined(SQLITE_MEMDEBUG)==0
13622# define SQLITE_SYSTEM_MALLOC 1
13623#endif
13624
13625/*
13626** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13627** sizes of memory allocations below this value where possible.
13628*/
13629#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
13630# define SQLITE_MALLOC_SOFT_LIMIT 1024
13631#endif
13632
13633/*
13634** We need to define _XOPEN_SOURCE as follows in order to enable
13635** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13636** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13637** it.
13638*/
13639#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13640# define _XOPEN_SOURCE 600
13641#endif
13642
13643/*
13644** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13645** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13646** make it true by defining or undefining NDEBUG.
13647**
13648** Setting NDEBUG makes the code smaller and faster by disabling the
13649** assert() statements in the code. So we want the default action
13650** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13651** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13652** feature.
13653*/
13654#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13655# define NDEBUG 1
13656#endif
13657#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13658# undef NDEBUG
13659#endif
13660
13661/*
13662** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13663*/
13664#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13665# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13666#endif
13667
13668/*
13669** The testcase() macro is used to aid in coverage testing. When
13670** doing coverage testing, the condition inside the argument to
13671** testcase() must be evaluated both true and false in order to
13672** get full branch coverage. The testcase() macro is inserted
13673** to help ensure adequate test coverage in places where simple
13674** condition/decision coverage is inadequate. For example, testcase()
13675** can be used to make sure boundary values are tested. For
13676** bitmask tests, testcase() can be used to make sure each bit
13677** is significant and used at least once. On switch statements
13678** where multiple cases go to the same block of code, testcase()
13679** can insure that all cases are evaluated.
13680**
13681*/
13682#ifdef SQLITE_COVERAGE_TEST
13683SQLITE_PRIVATE void sqlite3Coverage(int);
13684# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
13685#else
13686# define testcase(X)
13687#endif
13688
13689/*
13690** The TESTONLY macro is used to enclose variable declarations or
13691** other bits of code that are needed to support the arguments
13692** within testcase() and assert() macros.
13693*/
13694#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13695# define TESTONLY(X) X
13696#else
13697# define TESTONLY(X)
13698#endif
13699
13700/*
13701** Sometimes we need a small amount of code such as a variable initialization
13702** to setup for a later assert() statement. We do not want this code to
13703** appear when assert() is disabled. The following macro is therefore
13704** used to contain that setup code. The "VVA" acronym stands for
13705** "Verification, Validation, and Accreditation". In other words, the
13706** code within VVA_ONLY() will only run during verification processes.
13707*/
13708#ifndef NDEBUG
13709# define VVA_ONLY(X) X
13710#else
13711# define VVA_ONLY(X)
13712#endif
13713
13714/*
13715** The ALWAYS and NEVER macros surround boolean expressions which
13716** are intended to always be true or false, respectively. Such
13717** expressions could be omitted from the code completely. But they
13718** are included in a few cases in order to enhance the resilience
13719** of SQLite to unexpected behavior - to make the code "self-healing"
13720** or "ductile" rather than being "brittle" and crashing at the first
13721** hint of unplanned behavior.
13722**
13723** In other words, ALWAYS and NEVER are added for defensive code.
13724**
13725** When doing coverage testing ALWAYS and NEVER are hard-coded to
13726** be true and false so that the unreachable code they specify will
13727** not be counted as untested code.
13728*/
13729#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13730# define ALWAYS(X) (1)
13731# define NEVER(X) (0)
13732#elif !defined(NDEBUG)
13733# define ALWAYS(X) ((X)?1:(assert(0),0))
13734# define NEVER(X) ((X)?(assert(0),1):0)
13735#else
13736# define ALWAYS(X) (X)
13737# define NEVER(X) (X)
13738#endif
13739
13740/*
13741** The harmless(X) macro indicates that expression X is usually false
13742** but can be true without causing any problems, but we don't know of
13743** any way to cause X to be true.
13744**
13745** In debugging and testing builds, this macro will abort if X is ever
13746** true. In this way, developers are alerted to a possible test case
13747** that causes X to be true. If a harmless macro ever fails, that is
13748** an opportunity to change the macro into a testcase() and add a new
13749** test case to the test suite.
13750**
13751** For normal production builds, harmless(X) is a no-op, since it does
13752** not matter whether expression X is true or false.
13753*/
13754#ifdef SQLITE_DEBUG
13755# define harmless(X) assert(!(X));
13756#else
13757# define harmless(X)
13758#endif
13759
13760/*
13761** Some conditionals are optimizations only. In other words, if the
13762** conditionals are replaced with a constant 1 (true) or 0 (false) then
13763** the correct answer is still obtained, though perhaps not as quickly.
13764**
13765** The following macros mark these optimizations conditionals.
13766*/
13767#if defined(SQLITE_MUTATION_TEST)
13768# define OK_IF_ALWAYS_TRUE(X) (1)
13769# define OK_IF_ALWAYS_FALSE(X) (0)
13770#else
13771# define OK_IF_ALWAYS_TRUE(X) (X)
13772# define OK_IF_ALWAYS_FALSE(X) (X)
13773#endif
13774
13775/*
13776** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13777** defined. We need to defend against those failures when testing with
13778** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13779** during a normal build. The following macro can be used to disable tests
13780** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13781*/
13782#if defined(SQLITE_TEST_REALLOC_STRESS)
13783# define ONLY_IF_REALLOC_STRESS(X) (X)
13784#elif !defined(NDEBUG)
13785# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
13786#else
13787# define ONLY_IF_REALLOC_STRESS(X) (0)
13788#endif
13789
13790/*
13791** Declarations used for tracing the operating system interfaces.
13792*/
13793#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13794 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13795 extern int sqlite3OSTrace;
13796# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13797# define SQLITE_HAVE_OS_TRACE
13798#else
13799# define OSTRACE(X)
13800# undef SQLITE_HAVE_OS_TRACE
13801#endif
13802
13803/*
13804** Is the sqlite3ErrName() function needed in the build? Currently,
13805** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13806** OSTRACE is enabled), and by several "test*.c" files (which are
13807** compiled using SQLITE_TEST).
13808*/
13809#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13810 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13811# define SQLITE_NEED_ERR_NAME
13812#else
13813# undef SQLITE_NEED_ERR_NAME
13814#endif
13815
13816/*
13817** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13818*/
13819#ifdef SQLITE_OMIT_EXPLAIN
13820# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13821#endif
13822
13823/*
13824** Return true (non-zero) if the input is an integer that is too large
13825** to fit in 32-bits. This macro is used inside of various testcase()
13826** macros to verify that we have tested SQLite for large-file support.
13827*/
13828#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
13829
13830/*
13831** The macro unlikely() is a hint that surrounds a boolean
13832** expression that is usually false. Macro likely() surrounds
13833** a boolean expression that is usually true. These hints could,
13834** in theory, be used by the compiler to generate better code, but
13835** currently they are just comments for human readers.
13836*/
13837#define likely(X) (X)
13838#define unlikely(X) (X)
13839
13840/************** Include hash.h in the middle of sqliteInt.h ******************/
13841/************** Begin file hash.h ********************************************/
13842/*
13843** 2001 September 22
13844**
13845** The author disclaims copyright to this source code. In place of
13846** a legal notice, here is a blessing:
13847**
13848** May you do good and not evil.
13849** May you find forgiveness for yourself and forgive others.
13850** May you share freely, never taking more than you give.
13851**
13852*************************************************************************
13853** This is the header file for the generic hash-table implementation
13854** used in SQLite.
13855*/
13856#ifndef SQLITE_HASH_H
13857#define SQLITE_HASH_H
13858
13859/* Forward declarations of structures. */
13860typedef struct Hash Hash;
13861typedef struct HashElem HashElem;
13862
13863/* A complete hash table is an instance of the following structure.
13864** The internals of this structure are intended to be opaque -- client
13865** code should not attempt to access or modify the fields of this structure
13866** directly. Change this structure only by using the routines below.
13867** However, some of the "procedures" and "functions" for modifying and
13868** accessing this structure are really macros, so we can't really make
13869** this structure opaque.
13870**
13871** All elements of the hash table are on a single doubly-linked list.
13872** Hash.first points to the head of this list.
13873**
13874** There are Hash.htsize buckets. Each bucket points to a spot in
13875** the global doubly-linked list. The contents of the bucket are the
13876** element pointed to plus the next _ht.count-1 elements in the list.
13877**
13878** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13879** by a linear search of the global list. For small tables, the
13880** Hash.ht table is never allocated because if there are few elements
13881** in the table, it is faster to do a linear search than to manage
13882** the hash table.
13883*/
13884struct Hash {
13885 unsigned int htsize; /* Number of buckets in the hash table */
13886 unsigned int count; /* Number of entries in this table */
13887 HashElem *first; /* The first element of the array */
13888 struct _ht { /* the hash table */
13889 unsigned int count; /* Number of entries with this hash */
13890 HashElem *chain; /* Pointer to first entry with this hash */
13891 } *ht;
13892};
13893
13894/* Each element in the hash table is an instance of the following
13895** structure. All elements are stored on a single doubly-linked list.
13896**
13897** Again, this structure is intended to be opaque, but it can't really
13898** be opaque because it is used by macros.
13899*/
13900struct HashElem {
13901 HashElem *next, *prev; /* Next and previous elements in the table */
13902 void *data; /* Data associated with this element */
13903 const char *pKey; /* Key associated with this element */
13904};
13905
13906/*
13907** Access routines. To delete, insert a NULL pointer.
13908*/
13909SQLITE_PRIVATE void sqlite3HashInit(Hash*);
13910SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13911SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
13912SQLITE_PRIVATE void sqlite3HashClear(Hash*);
13913
13914/*
13915** Macros for looping over all elements of a hash table. The idiom is
13916** like this:
13917**
13918** Hash h;
13919** HashElem *p;
13920** ...
13921** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13922** SomeStructure *pData = sqliteHashData(p);
13923** // do something with pData
13924** }
13925*/
13926#define sqliteHashFirst(H) ((H)->first)
13927#define sqliteHashNext(E) ((E)->next)
13928#define sqliteHashData(E) ((E)->data)
13929/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13930/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13931
13932/*
13933** Number of entries in a hash table
13934*/
13935/* #define sqliteHashCount(H) ((H)->count) // NOT USED */
13936
13937#endif /* SQLITE_HASH_H */
13938
13939/************** End of hash.h ************************************************/
13940/************** Continuing where we left off in sqliteInt.h ******************/
13941/************** Include parse.h in the middle of sqliteInt.h *****************/
13942/************** Begin file parse.h *******************************************/
13943#define TK_SEMI 1
13944#define TK_EXPLAIN 2
13945#define TK_QUERY 3
13946#define TK_PLAN 4
13947#define TK_BEGIN 5
13948#define TK_TRANSACTION 6
13949#define TK_DEFERRED 7
13950#define TK_IMMEDIATE 8
13951#define TK_EXCLUSIVE 9
13952#define TK_COMMIT 10
13953#define TK_END 11
13954#define TK_ROLLBACK 12
13955#define TK_SAVEPOINT 13
13956#define TK_RELEASE 14
13957#define TK_TO 15
13958#define TK_TABLE 16
13959#define TK_CREATE 17
13960#define TK_IF 18
13961#define TK_NOT 19
13962#define TK_EXISTS 20
13963#define TK_TEMP 21
13964#define TK_LP 22
13965#define TK_RP 23
13966#define TK_AS 24
13967#define TK_WITHOUT 25
13968#define TK_COMMA 26
13969#define TK_ABORT 27
13970#define TK_ACTION 28
13971#define TK_AFTER 29
13972#define TK_ANALYZE 30
13973#define TK_ASC 31
13974#define TK_ATTACH 32
13975#define TK_BEFORE 33
13976#define TK_BY 34
13977#define TK_CASCADE 35
13978#define TK_CAST 36
13979#define TK_CONFLICT 37
13980#define TK_DATABASE 38
13981#define TK_DESC 39
13982#define TK_DETACH 40
13983#define TK_EACH 41
13984#define TK_FAIL 42
13985#define TK_OR 43
13986#define TK_AND 44
13987#define TK_IS 45
13988#define TK_MATCH 46
13989#define TK_LIKE_KW 47
13990#define TK_BETWEEN 48
13991#define TK_IN 49
13992#define TK_ISNULL 50
13993#define TK_NOTNULL 51
13994#define TK_NE 52
13995#define TK_EQ 53
13996#define TK_GT 54
13997#define TK_LE 55
13998#define TK_LT 56
13999#define TK_GE 57
14000#define TK_ESCAPE 58
14001#define TK_ID 59
14002#define TK_COLUMNKW 60
14003#define TK_DO 61
14004#define TK_FOR 62
14005#define TK_IGNORE 63
14006#define TK_INITIALLY 64
14007#define TK_INSTEAD 65
14008#define TK_NO 66
14009#define TK_KEY 67
14010#define TK_OF 68
14011#define TK_OFFSET 69
14012#define TK_PRAGMA 70
14013#define TK_RAISE 71
14014#define TK_RECURSIVE 72
14015#define TK_REPLACE 73
14016#define TK_RESTRICT 74
14017#define TK_ROW 75
14018#define TK_ROWS 76
14019#define TK_TRIGGER 77
14020#define TK_VACUUM 78
14021#define TK_VIEW 79
14022#define TK_VIRTUAL 80
14023#define TK_WITH 81
14024#define TK_NULLS 82
14025#define TK_FIRST 83
14026#define TK_LAST 84
14027#define TK_CURRENT 85
14028#define TK_FOLLOWING 86
14029#define TK_PARTITION 87
14030#define TK_PRECEDING 88
14031#define TK_RANGE 89
14032#define TK_UNBOUNDED 90
14033#define TK_EXCLUDE 91
14034#define TK_GROUPS 92
14035#define TK_OTHERS 93
14036#define TK_TIES 94
14037#define TK_GENERATED 95
14038#define TK_ALWAYS 96
14039#define TK_REINDEX 97
14040#define TK_RENAME 98
14041#define TK_CTIME_KW 99
14042#define TK_ANY 100
14043#define TK_BITAND 101
14044#define TK_BITOR 102
14045#define TK_LSHIFT 103
14046#define TK_RSHIFT 104
14047#define TK_PLUS 105
14048#define TK_MINUS 106
14049#define TK_STAR 107
14050#define TK_SLASH 108
14051#define TK_REM 109
14052#define TK_CONCAT 110
14053#define TK_COLLATE 111
14054#define TK_BITNOT 112
14055#define TK_ON 113
14056#define TK_INDEXED 114
14057#define TK_STRING 115
14058#define TK_JOIN_KW 116
14059#define TK_CONSTRAINT 117
14060#define TK_DEFAULT 118
14061#define TK_NULL 119
14062#define TK_PRIMARY 120
14063#define TK_UNIQUE 121
14064#define TK_CHECK 122
14065#define TK_REFERENCES 123
14066#define TK_AUTOINCR 124
14067#define TK_INSERT 125
14068#define TK_DELETE 126
14069#define TK_UPDATE 127
14070#define TK_SET 128
14071#define TK_DEFERRABLE 129
14072#define TK_FOREIGN 130
14073#define TK_DROP 131
14074#define TK_UNION 132
14075#define TK_ALL 133
14076#define TK_EXCEPT 134
14077#define TK_INTERSECT 135
14078#define TK_SELECT 136
14079#define TK_VALUES 137
14080#define TK_DISTINCT 138
14081#define TK_DOT 139
14082#define TK_FROM 140
14083#define TK_JOIN 141
14084#define TK_USING 142
14085#define TK_ORDER 143
14086#define TK_GROUP 144
14087#define TK_HAVING 145
14088#define TK_LIMIT 146
14089#define TK_WHERE 147
14090#define TK_INTO 148
14091#define TK_NOTHING 149
14092#define TK_FLOAT 150
14093#define TK_BLOB 151
14094#define TK_INTEGER 152
14095#define TK_VARIABLE 153
14096#define TK_CASE 154
14097#define TK_WHEN 155
14098#define TK_THEN 156
14099#define TK_ELSE 157
14100#define TK_INDEX 158
14101#define TK_ALTER 159
14102#define TK_ADD 160
14103#define TK_WINDOW 161
14104#define TK_OVER 162
14105#define TK_FILTER 163
14106#define TK_COLUMN 164
14107#define TK_AGG_FUNCTION 165
14108#define TK_AGG_COLUMN 166
14109#define TK_TRUEFALSE 167
14110#define TK_ISNOT 168
14111#define TK_FUNCTION 169
14112#define TK_UMINUS 170
14113#define TK_UPLUS 171
14114#define TK_TRUTH 172
14115#define TK_REGISTER 173
14116#define TK_VECTOR 174
14117#define TK_SELECT_COLUMN 175
14118#define TK_IF_NULL_ROW 176
14119#define TK_ASTERISK 177
14120#define TK_SPAN 178
14121#define TK_SPACE 179
14122#define TK_ILLEGAL 180
14123
14124/************** End of parse.h ***********************************************/
14125/************** Continuing where we left off in sqliteInt.h ******************/
14126#include <stdio.h>
14127#include <stdlib.h>
14128#include <string.h>
14129#include <assert.h>
14130#include <stddef.h>
14131
14132/*
14133** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
14134** This allows better measurements of where memcpy() is used when running
14135** cachegrind. But this macro version of memcpy() is very slow so it
14136** should not be used in production. This is a performance measurement
14137** hack only.
14138*/
14139#ifdef SQLITE_INLINE_MEMCPY
14140# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
14141 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14142#endif
14143
14144/*
14145** If compiling for a processor that lacks floating point support,
14146** substitute integer for floating-point
14147*/
14148#ifdef SQLITE_OMIT_FLOATING_POINT
14149# define double sqlite_int64
14150# define float sqlite_int64
14151# define LONGDOUBLE_TYPE sqlite_int64
14152# ifndef SQLITE_BIG_DBL
14153# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
14154# endif
14155# define SQLITE_OMIT_DATETIME_FUNCS 1
14156# define SQLITE_OMIT_TRACE 1
14157# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14158# undef SQLITE_HAVE_ISNAN
14159#endif
14160#ifndef SQLITE_BIG_DBL
14161# define SQLITE_BIG_DBL (1e99)
14162#endif
14163
14164/*
14165** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
14166** afterward. Having this macro allows us to cause the C compiler
14167** to omit code used by TEMP tables without messy #ifndef statements.
14168*/
14169#ifdef SQLITE_OMIT_TEMPDB
14170#define OMIT_TEMPDB 1
14171#else
14172#define OMIT_TEMPDB 0
14173#endif
14174
14175/*
14176** The "file format" number is an integer that is incremented whenever
14177** the VDBE-level file format changes. The following macros define the
14178** the default file format for new databases and the maximum file format
14179** that the library can read.
14180*/
14181#define SQLITE_MAX_FILE_FORMAT 4
14182#ifndef SQLITE_DEFAULT_FILE_FORMAT
14183# define SQLITE_DEFAULT_FILE_FORMAT 4
14184#endif
14185
14186/*
14187** Determine whether triggers are recursive by default. This can be
14188** changed at run-time using a pragma.
14189*/
14190#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
14191# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
14192#endif
14193
14194/*
14195** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
14196** on the command-line
14197*/
14198#ifndef SQLITE_TEMP_STORE
14199# define SQLITE_TEMP_STORE 1
14200#endif
14201
14202/*
14203** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
14204** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
14205** to zero.
14206*/
14207#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
14208# undef SQLITE_MAX_WORKER_THREADS
14209# define SQLITE_MAX_WORKER_THREADS 0
14210#endif
14211#ifndef SQLITE_MAX_WORKER_THREADS
14212# define SQLITE_MAX_WORKER_THREADS 8
14213#endif
14214#ifndef SQLITE_DEFAULT_WORKER_THREADS
14215# define SQLITE_DEFAULT_WORKER_THREADS 0
14216#endif
14217#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
14218# undef SQLITE_MAX_WORKER_THREADS
14219# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
14220#endif
14221
14222/*
14223** The default initial allocation for the pagecache when using separate
14224** pagecaches for each database connection. A positive number is the
14225** number of pages. A negative number N translations means that a buffer
14226** of -1024*N bytes is allocated and used for as many pages as it will hold.
14227**
14228** The default value of "20" was choosen to minimize the run-time of the
14229** speedtest1 test program with options: --shrink-memory --reprepare
14230*/
14231#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
14232# define SQLITE_DEFAULT_PCACHE_INITSZ 20
14233#endif
14234
14235/*
14236** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
14237*/
14238#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
14239# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
14240#endif
14241
14242/*
14243** The compile-time options SQLITE_MMAP_READWRITE and
14244** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
14245** You must choose one or the other (or neither) but not both.
14246*/
14247#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
14248#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
14249#endif
14250
14251/*
14252** GCC does not define the offsetof() macro so we'll have to do it
14253** ourselves.
14254*/
14255#ifndef offsetof
14256#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14257#endif
14258
14259/*
14260** Macros to compute minimum and maximum of two numbers.
14261*/
14262#ifndef MIN
14263# define MIN(A,B) ((A)<(B)?(A):(B))
14264#endif
14265#ifndef MAX
14266# define MAX(A,B) ((A)>(B)?(A):(B))
14267#endif
14268
14269/*
14270** Swap two objects of type TYPE.
14271*/
14272#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
14273
14274/*
14275** Check to see if this machine uses EBCDIC. (Yes, believe it or
14276** not, there are still machines out there that use EBCDIC.)
14277*/
14278#if 'A' == '\301'
14279# define SQLITE_EBCDIC 1
14280#else
14281# define SQLITE_ASCII 1
14282#endif
14283
14284/*
14285** Integers of known sizes. These typedefs might change for architectures
14286** where the sizes very. Preprocessor macros are available so that the
14287** types can be conveniently redefined at compile-type. Like this:
14288**
14289** cc '-DUINTPTR_TYPE=long long int' ...
14290*/
14291#ifndef UINT32_TYPE
14292# ifdef HAVE_UINT32_T
14293# define UINT32_TYPE uint32_t
14294# else
14295# define UINT32_TYPE unsigned int
14296# endif
14297#endif
14298#ifndef UINT16_TYPE
14299# ifdef HAVE_UINT16_T
14300# define UINT16_TYPE uint16_t
14301# else
14302# define UINT16_TYPE unsigned short int
14303# endif
14304#endif
14305#ifndef INT16_TYPE
14306# ifdef HAVE_INT16_T
14307# define INT16_TYPE int16_t
14308# else
14309# define INT16_TYPE short int
14310# endif
14311#endif
14312#ifndef UINT8_TYPE
14313# ifdef HAVE_UINT8_T
14314# define UINT8_TYPE uint8_t
14315# else
14316# define UINT8_TYPE unsigned char
14317# endif
14318#endif
14319#ifndef INT8_TYPE
14320# ifdef HAVE_INT8_T
14321# define INT8_TYPE int8_t
14322# else
14323# define INT8_TYPE signed char
14324# endif
14325#endif
14326#ifndef LONGDOUBLE_TYPE
14327# define LONGDOUBLE_TYPE long double
14328#endif
14329typedef sqlite_int64 i64; /* 8-byte signed integer */
14330typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14331typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14332typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14333typedef INT16_TYPE i16; /* 2-byte signed integer */
14334typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14335typedef INT8_TYPE i8; /* 1-byte signed integer */
14336
14337/*
14338** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
14339** that can be stored in a u32 without loss of data. The value
14340** is 0x00000000ffffffff. But because of quirks of some compilers, we
14341** have to specify the value in the less intuitive manner shown:
14342*/
14343#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14344
14345/*
14346** The datatype used to store estimates of the number of rows in a
14347** table or index. This is an unsigned integer type. For 99.9% of
14348** the world, a 32-bit integer is sufficient. But a 64-bit integer
14349** can be used at compile-time if desired.
14350*/
14351#ifdef SQLITE_64BIT_STATS
14352 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14353#else
14354 typedef u32 tRowcnt; /* 32-bit is the default */
14355#endif
14356
14357/*
14358** Estimated quantities used for query planning are stored as 16-bit
14359** logarithms. For quantity X, the value stored is 10*log2(X). This
14360** gives a possible range of values of approximately 1.0e986 to 1e-986.
14361** But the allowed values are "grainy". Not every value is representable.
14362** For example, quantities 16 and 17 are both represented by a LogEst
14363** of 40. However, since LogEst quantities are suppose to be estimates,
14364** not exact values, this imprecision is not a problem.
14365**
14366** "LogEst" is short for "Logarithmic Estimate".
14367**
14368** Examples:
14369** 1 -> 0 20 -> 43 10000 -> 132
14370** 2 -> 10 25 -> 46 25000 -> 146
14371** 3 -> 16 100 -> 66 1000000 -> 199
14372** 4 -> 20 1000 -> 99 1048576 -> 200
14373** 10 -> 33 1024 -> 100 4294967296 -> 320
14374**
14375** The LogEst can be negative to indicate fractional values.
14376** Examples:
14377**
14378** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14379*/
14380typedef INT16_TYPE LogEst;
14381
14382/*
14383** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
14384*/
14385#ifndef SQLITE_PTRSIZE
14386# if defined(__SIZEOF_POINTER__)
14387# define SQLITE_PTRSIZE __SIZEOF_POINTER__
14388# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14389 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
14390 (defined(__TOS_AIX__) && !defined(__64BIT__))
14391# define SQLITE_PTRSIZE 4
14392# else
14393# define SQLITE_PTRSIZE 8
14394# endif
14395#endif
14396
14397/* The uptr type is an unsigned integer large enough to hold a pointer
14398*/
14399#if defined(HAVE_STDINT_H)
14400 typedef uintptr_t uptr;
14401#elif SQLITE_PTRSIZE==4
14402 typedef u32 uptr;
14403#else
14404 typedef u64 uptr;
14405#endif
14406
14407/*
14408** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
14409** something between S (inclusive) and E (exclusive).
14410**
14411** In other words, S is a buffer and E is a pointer to the first byte after
14412** the end of buffer S. This macro returns true if P points to something
14413** contained within the buffer S.
14414*/
14415#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14416
14417
14418/*
14419** Macros to determine whether the machine is big or little endian,
14420** and whether or not that determination is run-time or compile-time.
14421**
14422** For best performance, an attempt is made to guess at the byte-order
14423** using C-preprocessor macros. If that is unsuccessful, or if
14424** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14425** at run-time.
14426*/
14427#ifndef SQLITE_BYTEORDER
14428# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14429 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
14430 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14431 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14432# define SQLITE_BYTEORDER 1234
14433# elif defined(sparc) || defined(__ppc__) || \
14434 defined(__ARMEB__) || defined(__AARCH64EB__)
14435# define SQLITE_BYTEORDER 4321
14436# else
14437# define SQLITE_BYTEORDER 0
14438# endif
14439#endif
14440#if SQLITE_BYTEORDER==4321
14441# define SQLITE_BIGENDIAN 1
14442# define SQLITE_LITTLEENDIAN 0
14443# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
14444#elif SQLITE_BYTEORDER==1234
14445# define SQLITE_BIGENDIAN 0
14446# define SQLITE_LITTLEENDIAN 1
14447# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
14448#else
14449# ifdef SQLITE_AMALGAMATION
14450 const int sqlite3one = 1;
14451# else
14452 extern const int sqlite3one;
14453# endif
14454# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
14455# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
14456# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
14457#endif
14458
14459/*
14460** Constants for the largest and smallest possible 64-bit signed integers.
14461** These macros are designed to work correctly on both 32-bit and 64-bit
14462** compilers.
14463*/
14464#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
14465#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))
14466#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14467
14468/*
14469** Round up a number to the next larger multiple of 8. This is used
14470** to force 8-byte alignment on 64-bit architectures.
14471*/
14472#define ROUND8(x) (((x)+7)&~7)
14473
14474/*
14475** Round down to the nearest multiple of 8
14476*/
14477#define ROUNDDOWN8(x) ((x)&~7)
14478
14479/*
14480** Assert that the pointer X is aligned to an 8-byte boundary. This
14481** macro is used only within assert() to verify that the code gets
14482** all alignment restrictions correct.
14483**
14484** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14485** underlying malloc() implementation might return us 4-byte aligned
14486** pointers. In that case, only verify 4-byte alignment.
14487*/
14488#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14489# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14490#else
14491# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14492#endif
14493
14494/*
14495** Disable MMAP on platforms where it is known to not work
14496*/
14497#if defined(__OpenBSD__) || defined(__QNXNTO__)
14498# undef SQLITE_MAX_MMAP_SIZE
14499# define SQLITE_MAX_MMAP_SIZE 0
14500#endif
14501
14502/*
14503** Default maximum size of memory used by memory-mapped I/O in the VFS
14504*/
14505#ifdef __APPLE__
14506# include <TargetConditionals.h>
14507#endif
14508#ifndef SQLITE_MAX_MMAP_SIZE
14509# if defined(__linux__) \
14510 || defined(_WIN32) \
14511 || (defined(__APPLE__) && defined(__MACH__)) \
14512 || defined(__sun) \
14513 || defined(__FreeBSD__) \
14514 || defined(__DragonFly__)
14515# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
14516# else
14517# define SQLITE_MAX_MMAP_SIZE 0
14518# endif
14519#endif
14520
14521/*
14522** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
14523** default MMAP_SIZE is specified at compile-time, make sure that it does
14524** not exceed the maximum mmap size.
14525*/
14526#ifndef SQLITE_DEFAULT_MMAP_SIZE
14527# define SQLITE_DEFAULT_MMAP_SIZE 0
14528#endif
14529#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
14530# undef SQLITE_DEFAULT_MMAP_SIZE
14531# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
14532#endif
14533
14534/*
14535** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
14536** the Select query generator tracing logic is turned on.
14537*/
14538#if defined(SQLITE_ENABLE_SELECTTRACE)
14539# define SELECTTRACE_ENABLED 1
14540#else
14541# define SELECTTRACE_ENABLED 0
14542#endif
14543#if defined(SQLITE_ENABLE_SELECTTRACE)
14544# define SELECTTRACE_ENABLED 1
14545# define SELECTTRACE(K,P,S,X) \
14546 if(sqlite3_unsupported_selecttrace&(K)) \
14547 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14548 sqlite3DebugPrintf X
14549#else
14550# define SELECTTRACE(K,P,S,X)
14551# define SELECTTRACE_ENABLED 0
14552#endif
14553
14554/*
14555** An instance of the following structure is used to store the busy-handler
14556** callback for a given sqlite handle.
14557**
14558** The sqlite.busyHandler member of the sqlite struct contains the busy
14559** callback for the database handle. Each pager opened via the sqlite
14560** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14561** callback is currently invoked only from within pager.c.
14562*/
14563typedef struct BusyHandler BusyHandler;
14564struct BusyHandler {
14565 int (*xBusyHandler)(void *,int); /* The busy callback */
14566 void *pBusyArg; /* First arg to busy callback */
14567 int nBusy; /* Incremented with each busy call */
14568};
14569
14570/*
14571** Name of table that holds the database schema.
14572*/
14573#define DFLT_SCHEMA_TABLE "sqlite_master"
14574#define DFLT_TEMP_SCHEMA_TABLE "sqlite_temp_master"
14575#define ALT_SCHEMA_TABLE "sqlite_schema"
14576#define ALT_TEMP_SCHEMA_TABLE "sqlite_temp_schema"
14577
14578
14579/*
14580** The root-page of the schema table.
14581*/
14582#define SCHEMA_ROOT 1
14583
14584/*
14585** The name of the schema table. The name is different for TEMP.
14586*/
14587#define SCHEMA_TABLE(x) \
14588 ((!OMIT_TEMPDB)&&(x==1)?DFLT_TEMP_SCHEMA_TABLE:DFLT_SCHEMA_TABLE)
14589
14590/*
14591** A convenience macro that returns the number of elements in
14592** an array.
14593*/
14594#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
14595
14596/*
14597** Determine if the argument is a power of two
14598*/
14599#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14600
14601/*
14602** The following value as a destructor means to use sqlite3DbFree().
14603** The sqlite3DbFree() routine requires two parameters instead of the
14604** one parameter that destructors normally want. So we have to introduce
14605** this magic value that the code knows to handle differently. Any
14606** pointer will work here as long as it is distinct from SQLITE_STATIC
14607** and SQLITE_TRANSIENT.
14608*/
14609#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3OomFault)
14610
14611/*
14612** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14613** not support Writable Static Data (WSD) such as global and static variables.
14614** All variables must either be on the stack or dynamically allocated from
14615** the heap. When WSD is unsupported, the variable declarations scattered
14616** throughout the SQLite code must become constants instead. The SQLITE_WSD
14617** macro is used for this purpose. And instead of referencing the variable
14618** directly, we use its constant as a key to lookup the run-time allocated
14619** buffer that holds real variable. The constant is also the initializer
14620** for the run-time allocated buffer.
14621**
14622** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14623** macros become no-ops and have zero performance impact.
14624*/
14625#ifdef SQLITE_OMIT_WSD
14626 #define SQLITE_WSD const
14627 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14628 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
14629SQLITE_API int sqlite3_wsd_init(int N, int J);
14630SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14631#else
14632 #define SQLITE_WSD
14633 #define GLOBAL(t,v) v
14634 #define sqlite3GlobalConfig sqlite3Config
14635#endif
14636
14637/*
14638** The following macros are used to suppress compiler warnings and to
14639** make it clear to human readers when a function parameter is deliberately
14640** left unused within the body of a function. This usually happens when
14641** a function is called via a function pointer. For example the
14642** implementation of an SQL aggregate step callback may not use the
14643** parameter indicating the number of arguments passed to the aggregate,
14644** if it knows that this is enforced elsewhere.
14645**
14646** When a function parameter is not used at all within the body of a function,
14647** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14648** However, these macros may also be used to suppress warnings related to
14649** parameters that may or may not be used depending on compilation options.
14650** For example those parameters only used in assert() statements. In these
14651** cases the parameters are named as per the usual conventions.
14652*/
14653#define UNUSED_PARAMETER(x) (void)(x)
14654#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14655
14656/*
14657** Forward references to structures
14658*/
14659typedef struct AggInfo AggInfo;
14660typedef struct AuthContext AuthContext;
14661typedef struct AutoincInfo AutoincInfo;
14662typedef struct Bitvec Bitvec;
14663typedef struct CollSeq CollSeq;
14664typedef struct Column Column;
14665typedef struct Db Db;
14666typedef struct Schema Schema;
14667typedef struct Expr Expr;
14668typedef struct ExprList ExprList;
14669typedef struct FKey FKey;
14670typedef struct FuncDestructor FuncDestructor;
14671typedef struct FuncDef FuncDef;
14672typedef struct FuncDefHash FuncDefHash;
14673typedef struct IdList IdList;
14674typedef struct Index Index;
14675typedef struct IndexSample IndexSample;
14676typedef struct KeyClass KeyClass;
14677typedef struct KeyInfo KeyInfo;
14678typedef struct Lookaside Lookaside;
14679typedef struct LookasideSlot LookasideSlot;
14680typedef struct Module Module;
14681typedef struct NameContext NameContext;
14682typedef struct Parse Parse;
14683typedef struct PreUpdate PreUpdate;
14684typedef struct PrintfArguments PrintfArguments;
14685typedef struct RenameToken RenameToken;
14686typedef struct RowSet RowSet;
14687typedef struct Savepoint Savepoint;
14688typedef struct Select Select;
14689typedef struct SQLiteThread SQLiteThread;
14690typedef struct SelectDest SelectDest;
14691typedef struct SrcList SrcList;
14692typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14693typedef struct Table Table;
14694typedef struct TableLock TableLock;
14695typedef struct Token Token;
14696typedef struct TreeView TreeView;
14697typedef struct Trigger Trigger;
14698typedef struct TriggerPrg TriggerPrg;
14699typedef struct TriggerStep TriggerStep;
14700typedef struct UnpackedRecord UnpackedRecord;
14701typedef struct Upsert Upsert;
14702typedef struct VTable VTable;
14703typedef struct VtabCtx VtabCtx;
14704typedef struct Walker Walker;
14705typedef struct WhereInfo WhereInfo;
14706typedef struct Window Window;
14707typedef struct With With;
14708
14709
14710/*
14711** The bitmask datatype defined below is used for various optimizations.
14712**
14713** Changing this from a 64-bit to a 32-bit type limits the number of
14714** tables in a join to 32 instead of 64. But it also reduces the size
14715** of the library by 738 bytes on ix86.
14716*/
14717#ifdef SQLITE_BITMASK_TYPE
14718 typedef SQLITE_BITMASK_TYPE Bitmask;
14719#else
14720 typedef u64 Bitmask;
14721#endif
14722
14723/*
14724** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14725*/
14726#define BMS ((int)(sizeof(Bitmask)*8))
14727
14728/*
14729** A bit in a Bitmask
14730*/
14731#define MASKBIT(n) (((Bitmask)1)<<(n))
14732#define MASKBIT64(n) (((u64)1)<<(n))
14733#define MASKBIT32(n) (((unsigned int)1)<<(n))
14734#define ALLBITS ((Bitmask)-1)
14735
14736/* A VList object records a mapping between parameters/variables/wildcards
14737** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14738** variable number associated with that parameter. See the format description
14739** on the sqlite3VListAdd() routine for more information. A VList is really
14740** just an array of integers.
14741*/
14742typedef int VList;
14743
14744/*
14745** Defer sourcing vdbe.h and btree.h until after the "u8" and
14746** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14747** pointer types (i.e. FuncDef) defined above.
14748*/
14749/************** Include pager.h in the middle of sqliteInt.h *****************/
14750/************** Begin file pager.h *******************************************/
14751/*
14752** 2001 September 15
14753**
14754** The author disclaims copyright to this source code. In place of
14755** a legal notice, here is a blessing:
14756**
14757** May you do good and not evil.
14758** May you find forgiveness for yourself and forgive others.
14759** May you share freely, never taking more than you give.
14760**
14761*************************************************************************
14762** This header file defines the interface that the sqlite page cache
14763** subsystem. The page cache subsystem reads and writes a file a page
14764** at a time and provides a journal for rollback.
14765*/
14766
14767#ifndef SQLITE_PAGER_H
14768#define SQLITE_PAGER_H
14769
14770/*
14771** Default maximum size for persistent journal files. A negative
14772** value means no limit. This value may be overridden using the
14773** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
14774*/
14775#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
14776 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14777#endif
14778
14779/*
14780** The type used to represent a page number. The first page in a file
14781** is called page 1. 0 is used to represent "not a page".
14782*/
14783typedef u32 Pgno;
14784
14785/*
14786** Each open file is managed by a separate instance of the "Pager" structure.
14787*/
14788typedef struct Pager Pager;
14789
14790/*
14791** Handle type for pages.
14792*/
14793typedef struct PgHdr DbPage;
14794
14795/*
14796** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
14797** reserved for working around a windows/posix incompatibility). It is
14798** used in the journal to signify that the remainder of the journal file
14799** is devoted to storing a super-journal name - there are no more pages to
14800** roll back. See comments for function writeSuperJournal() in pager.c
14801** for details.
14802*/
14803#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14804
14805/*
14806** Allowed values for the flags parameter to sqlite3PagerOpen().
14807**
14808** NOTE: These values must match the corresponding BTREE_ values in btree.h.
14809*/
14810#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
14811#define PAGER_MEMORY 0x0002 /* In-memory database */
14812
14813/*
14814** Valid values for the second argument to sqlite3PagerLockingMode().
14815*/
14816#define PAGER_LOCKINGMODE_QUERY -1
14817#define PAGER_LOCKINGMODE_NORMAL 0
14818#define PAGER_LOCKINGMODE_EXCLUSIVE 1
14819
14820/*
14821** Numeric constants that encode the journalmode.
14822**
14823** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
14824** are exposed in the API via the "PRAGMA journal_mode" command and
14825** therefore cannot be changed without a compatibility break.
14826*/
14827#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14828#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
14829#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
14830#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
14831#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
14832#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14833#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14834
14835/*
14836** Flags that make up the mask passed to sqlite3PagerGet().
14837*/
14838#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
14839#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14840
14841/*
14842** Flags for sqlite3PagerSetFlags()
14843**
14844** Value constraints (enforced via assert()):
14845** PAGER_FULLFSYNC == SQLITE_FullFSync
14846** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
14847** PAGER_CACHE_SPILL == SQLITE_CacheSpill
14848*/
14849#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
14850#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
14851#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
14852#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
14853#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
14854#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
14855#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
14856#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
14857#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
14858
14859/*
14860** The remainder of this file contains the declarations of the functions
14861** that make up the Pager sub-system API. See source code comments for
14862** a detailed description of each routine.
14863*/
14864
14865/* Open and close a Pager connection. */
14866SQLITE_PRIVATE int sqlite3PagerOpen(
14867 sqlite3_vfs*,
14868 Pager **ppPager,
14869 const char*,
14870 int,
14871 int,
14872 int,
14873 void(*)(DbPage*)
14874);
14875SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
14876SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
14877
14878/* Functions used to configure a Pager object. */
14879SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
14880SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
14881SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
14882SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
14883SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
14884SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
14885SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
14886SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
14887SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
14888SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
14889SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
14890SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
14891SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
14892SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
14893SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
14894
14895/* Functions used to obtain and release page references. */
14896SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
14897SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
14898SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
14899SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
14900SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
14901SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
14902
14903/* Operations on page references. */
14904SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
14905SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
14906SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
14907SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
14908SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
14909SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
14910
14911/* Functions used to manage pager transactions and savepoints. */
14912SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
14913SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
14914SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
14915SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
14916SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper);
14917SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
14918SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
14919SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
14920SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
14921SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
14922
14923#ifndef SQLITE_OMIT_WAL
14924SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
14925SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
14926SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
14927SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
14928SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
14929# ifdef SQLITE_ENABLE_SNAPSHOT
14930SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
14931SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
14932SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
14933SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
14934SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager);
14935# endif
14936#endif
14937
14938#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
14939SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int);
14940SQLITE_PRIVATE void sqlite3PagerWalDb(Pager*, sqlite3*);
14941#else
14942# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK
14943# define sqlite3PagerWalDb(x,y)
14944#endif
14945
14946#ifdef SQLITE_DIRECT_OVERFLOW_READ
14947SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
14948#endif
14949
14950#ifdef SQLITE_ENABLE_ZIPVFS
14951SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
14952#endif
14953
14954/* Functions used to query pager state and configuration. */
14955SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
14956SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
14957#ifdef SQLITE_DEBUG
14958SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
14959#endif
14960SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
14961SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager*, int);
14962SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
14963SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
14964SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
14965SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
14966SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
14967SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
14968SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
14969SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
14970SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
14971
14972/* Functions used to truncate the database file. */
14973SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
14974
14975SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
14976
14977/* Functions to support testing and debugging. */
14978#if !defined(NDEBUG) || defined(SQLITE_TEST)
14979SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
14980SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
14981#endif
14982#ifdef SQLITE_TEST
14983SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
14984SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
14985 void disable_simulated_io_errors(void);
14986 void enable_simulated_io_errors(void);
14987#else
14988# define disable_simulated_io_errors()
14989# define enable_simulated_io_errors()
14990#endif
14991
14992#endif /* SQLITE_PAGER_H */
14993
14994/************** End of pager.h ***********************************************/
14995/************** Continuing where we left off in sqliteInt.h ******************/
14996/************** Include btree.h in the middle of sqliteInt.h *****************/
14997/************** Begin file btree.h *******************************************/
14998/*
14999** 2001 September 15
15000**
15001** The author disclaims copyright to this source code. In place of
15002** a legal notice, here is a blessing:
15003**
15004** May you do good and not evil.
15005** May you find forgiveness for yourself and forgive others.
15006** May you share freely, never taking more than you give.
15007**
15008*************************************************************************
15009** This header file defines the interface that the sqlite B-Tree file
15010** subsystem. See comments in the source code for a detailed description
15011** of what each interface routine does.
15012*/
15013#ifndef SQLITE_BTREE_H
15014#define SQLITE_BTREE_H
15015
15016/* TODO: This definition is just included so other modules compile. It
15017** needs to be revisited.
15018*/
15019#define SQLITE_N_BTREE_META 16
15020
15021/*
15022** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15023** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
15024*/
15025#ifndef SQLITE_DEFAULT_AUTOVACUUM
15026 #define SQLITE_DEFAULT_AUTOVACUUM 0
15027#endif
15028
15029#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15030#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15031#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
15032
15033/*
15034** Forward declarations of structure
15035*/
15036typedef struct Btree Btree;
15037typedef struct BtCursor BtCursor;
15038typedef struct BtShared BtShared;
15039typedef struct BtreePayload BtreePayload;
15040
15041
15042SQLITE_PRIVATE int sqlite3BtreeOpen(
15043 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15044 const char *zFilename, /* Name of database file to open */
15045 sqlite3 *db, /* Associated database connection */
15046 Btree **ppBtree, /* Return open Btree* here */
15047 int flags, /* Flags */
15048 int vfsFlags /* Flags passed through to VFS open */
15049);
15050
15051/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
15052** following values.
15053**
15054** NOTE: These values must match the corresponding PAGER_ values in
15055** pager.h.
15056*/
15057#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
15058#define BTREE_MEMORY 2 /* This is an in-memory DB */
15059#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15060#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
15061
15062SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
15063SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
15064SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
15065#if SQLITE_MAX_MMAP_SIZE>0
15066SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
15067#endif
15068SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
15069SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
15070SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
15071SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
15072SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree*);
15073SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
15074SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree*);
15075SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
15076SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
15077SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
15078SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
15079SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
15080SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
15081SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
15082SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
15083SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
15084SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
15085SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
15086SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
15087SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
15088SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
15089SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
15090#ifndef SQLITE_OMIT_SHARED_CACHE
15091SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
15092#endif
15093SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
15094
15095SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
15096SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
15097SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
15098
15099SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
15100
15101/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
15102** of the flags shown below.
15103**
15104** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
15105** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15106** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
15107** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
15108** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15109** indices.)
15110*/
15111#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15112#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15113
15114SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
15115SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
15116SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
15117SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
15118
15119SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
15120SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
15121
15122SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
15123
15124/*
15125** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
15126** should be one of the following values. The integer values are assigned
15127** to constants so that the offset of the corresponding field in an
15128** SQLite database header may be found using the following formula:
15129**
15130** offset = 36 + (idx * 4)
15131**
15132** For example, the free-page-count field is located at byte offset 36 of
15133** the database file header. The incr-vacuum-flag field is located at
15134** byte offset 64 (== 36+4*7).
15135**
15136** The BTREE_DATA_VERSION value is not really a value stored in the header.
15137** It is a read-only number computed by the pager. But we merge it with
15138** the header value access routines since its access pattern is the same.
15139** Call it a "virtual meta value".
15140*/
15141#define BTREE_FREE_PAGE_COUNT 0
15142#define BTREE_SCHEMA_VERSION 1
15143#define BTREE_FILE_FORMAT 2
15144#define BTREE_DEFAULT_CACHE_SIZE 3
15145#define BTREE_LARGEST_ROOT_PAGE 4
15146#define BTREE_TEXT_ENCODING 5
15147#define BTREE_USER_VERSION 6
15148#define BTREE_INCR_VACUUM 7
15149#define BTREE_APPLICATION_ID 8
15150#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15151
15152/*
15153** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
15154** interface.
15155**
15156** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
15157**
15158** The first argument is an Expr* (which is guaranteed to be constant for
15159** the lifetime of the cursor) that defines constraints on which rows
15160** might be fetched with this cursor. The Expr* tree may contain
15161** TK_REGISTER nodes that refer to values stored in the array of registers
15162** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
15163** then the value of the node is the value in Mem[pExpr.iTable]. Any
15164** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15165** column of the b-tree of the cursor. The Expr tree will not contain
15166** any function calls nor subqueries nor references to b-trees other than
15167** the cursor being hinted.
15168**
15169** The design of the _RANGE hint is aid b-tree implementations that try
15170** to prefetch content from remote machines - to provide those
15171** implementations with limits on what needs to be prefetched and thereby
15172** reduce network bandwidth.
15173**
15174** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
15175** standard SQLite. The other hints are provided for extentions that use
15176** the SQLite parser and code generator but substitute their own storage
15177** engine.
15178*/
15179#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
15180
15181/*
15182** Values that may be OR'd together to form the argument to the
15183** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
15184**
15185** The BTREE_BULKLOAD flag is set on index cursors when the index is going
15186** to be filled with content that is already in sorted order.
15187**
15188** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
15189** OP_SeekLE opcodes for a range search, but where the range of entries
15190** selected will all have the same key. In other words, the cursor will
15191** be used only for equality key searches.
15192**
15193*/
15194#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
15195#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15196
15197/*
15198** Flags passed as the third argument to sqlite3BtreeCursor().
15199**
15200** For read-only cursors the wrFlag argument is always zero. For read-write
15201** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
15202** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
15203** only be used by SQLite for the following:
15204**
15205** * to seek to and then delete specific entries, and/or
15206**
15207** * to read values that will be used to create keys that other
15208** BTREE_FORDELETE cursors will seek to and delete.
15209**
15210** The BTREE_FORDELETE flag is an optimization hint. It is not used by
15211** by this, the native b-tree engine of SQLite, but it is available to
15212** alternative storage engines that might be substituted in place of this
15213** b-tree system. For alternative storage engines in which a delete of
15214** the main table row automatically deletes corresponding index rows,
15215** the FORDELETE flag hint allows those alternative storage engines to
15216** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
15217** and DELETE operations as no-ops, and any READ operation against a
15218** FORDELETE cursor may return a null row: 0x01 0x00.
15219*/
15220#define BTREE_WRCSR 0x00000004 /* read-write cursor */
15221#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
15222
15223SQLITE_PRIVATE int sqlite3BtreeCursor(
15224 Btree*, /* BTree containing table to open */
15225 Pgno iTable, /* Index of root page */
15226 int wrFlag, /* 1 for writing. 0 for read-only */
15227 struct KeyInfo*, /* First argument to compare function */
15228 BtCursor *pCursor /* Space to write cursor structure */
15229);
15230SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
15231SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
15232SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
15233SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
15234#ifdef SQLITE_ENABLE_CURSOR_HINTS
15235SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
15236#endif
15237
15238SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
15239SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
15240 BtCursor*,
15241 UnpackedRecord *pUnKey,
15242 i64 intKey,
15243 int bias,
15244 int *pRes
15245);
15246SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
15247SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
15248SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
15249
15250/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
15251#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
15252#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
15253#define BTREE_APPEND 0x08 /* Insert is likely an append */
15254
15255/* An instance of the BtreePayload object describes the content of a single
15256** entry in either an index or table btree.
15257**
15258** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
15259** an arbitrary key and no data. These btrees have pKey,nKey set to the
15260** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
15261** fields give an array of Mem objects that are a decomposition of the key.
15262** The nMem field might be zero, indicating that no decomposition is available.
15263**
15264** Table btrees (used for rowid tables) contain an integer rowid used as
15265** the key and passed in the nKey field. The pKey field is zero.
15266** pData,nData hold the content of the new entry. nZero extra zero bytes
15267** are appended to the end of the content when constructing the entry.
15268** The aMem,nMem fields are uninitialized for table btrees.
15269**
15270** Field usage summary:
15271**
15272** Table BTrees Index Btrees
15273**
15274** pKey always NULL encoded key
15275** nKey the ROWID length of pKey
15276** pData data not used
15277** aMem not used decomposed key value
15278** nMem not used entries in aMem
15279** nData length of pData not used
15280** nZero extra zeros after pData not used
15281**
15282** This object is used to pass information into sqlite3BtreeInsert(). The
15283** same information used to be passed as five separate parameters. But placing
15284** the information into this object helps to keep the interface more
15285** organized and understandable, and it also helps the resulting code to
15286** run a little faster by using fewer registers for parameter passing.
15287*/
15288struct BtreePayload {
15289 const void *pKey; /* Key content for indexes. NULL for tables */
15290 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
15291 const void *pData; /* Data for tables. */
15292 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
15293 u16 nMem; /* Number of aMem[] value. Might be zero */
15294 int nData; /* Size of pData. 0 if none. */
15295 int nZero; /* Extra zero data appended after pData,nData */
15296};
15297
15298SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
15299 int flags, int seekResult);
15300SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
15301SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
15302SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
15303SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
15304SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
15305SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
15306SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor*);
15307SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor*);
15308#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
15309SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
15310#endif
15311SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
15312SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
15313SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
15314SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
15315
15316SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,Pgno*aRoot,int nRoot,int,int*);
15317SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
15318SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
15319
15320#ifndef SQLITE_OMIT_INCRBLOB
15321SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
15322SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
15323SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
15324#endif
15325SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
15326SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
15327SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
15328SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
15329SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
15330
15331#ifndef NDEBUG
15332SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
15333#endif
15334SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
15335
15336SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
15337
15338#ifdef SQLITE_TEST
15339SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
15340SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
15341#endif
15342
15343#ifndef SQLITE_OMIT_WAL
15344SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
15345#endif
15346
15347/*
15348** If we are not using shared cache, then there is no need to
15349** use mutexes to access the BtShared structures. So make the
15350** Enter and Leave procedures no-ops.
15351*/
15352#ifndef SQLITE_OMIT_SHARED_CACHE
15353SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
15354SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
15355SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
15356SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
15357SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
15358#else
15359# define sqlite3BtreeEnter(X)
15360# define sqlite3BtreeEnterAll(X)
15361# define sqlite3BtreeSharable(X) 0
15362# define sqlite3BtreeEnterCursor(X)
15363# define sqlite3BtreeConnectionCount(X) 1
15364#endif
15365
15366#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
15367SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
15368SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
15369SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
15370#ifndef NDEBUG
15371 /* These routines are used inside assert() statements only. */
15372SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
15373SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
15374SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
15375#endif
15376#else
15377
15378# define sqlite3BtreeLeave(X)
15379# define sqlite3BtreeLeaveCursor(X)
15380# define sqlite3BtreeLeaveAll(X)
15381
15382# define sqlite3BtreeHoldsMutex(X) 1
15383# define sqlite3BtreeHoldsAllMutexes(X) 1
15384# define sqlite3SchemaMutexHeld(X,Y,Z) 1
15385#endif
15386
15387
15388#endif /* SQLITE_BTREE_H */
15389
15390/************** End of btree.h ***********************************************/
15391/************** Continuing where we left off in sqliteInt.h ******************/
15392/************** Include vdbe.h in the middle of sqliteInt.h ******************/
15393/************** Begin file vdbe.h ********************************************/
15394/*
15395** 2001 September 15
15396**
15397** The author disclaims copyright to this source code. In place of
15398** a legal notice, here is a blessing:
15399**
15400** May you do good and not evil.
15401** May you find forgiveness for yourself and forgive others.
15402** May you share freely, never taking more than you give.
15403**
15404*************************************************************************
15405** Header file for the Virtual DataBase Engine (VDBE)
15406**
15407** This header defines the interface to the virtual database engine
15408** or VDBE. The VDBE implements an abstract machine that runs a
15409** simple program to access and modify the underlying database.
15410*/
15411#ifndef SQLITE_VDBE_H
15412#define SQLITE_VDBE_H
15413/* #include <stdio.h> */
15414
15415/*
15416** A single VDBE is an opaque structure named "Vdbe". Only routines
15417** in the source file sqliteVdbe.c are allowed to see the insides
15418** of this structure.
15419*/
15420typedef struct Vdbe Vdbe;
15421
15422/*
15423** The names of the following types declared in vdbeInt.h are required
15424** for the VdbeOp definition.
15425*/
15426typedef struct sqlite3_value Mem;
15427typedef struct SubProgram SubProgram;
15428
15429/*
15430** A single instruction of the virtual machine has an opcode
15431** and as many as three operands. The instruction is recorded
15432** as an instance of the following structure:
15433*/
15434struct VdbeOp {
15435 u8 opcode; /* What operation to perform */
15436 signed char p4type; /* One of the P4_xxx constants for p4 */
15437 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15438 int p1; /* First operand */
15439 int p2; /* Second parameter (often the jump destination) */
15440 int p3; /* The third parameter */
15441 union p4union { /* fourth parameter */
15442 int i; /* Integer value if p4type==P4_INT32 */
15443 void *p; /* Generic pointer */
15444 char *z; /* Pointer to data for string (char array) types */
15445 i64 *pI64; /* Used when p4type is P4_INT64 */
15446 double *pReal; /* Used when p4type is P4_REAL */
15447 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
15448 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
15449 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
15450 Mem *pMem; /* Used when p4type is P4_MEM */
15451 VTable *pVtab; /* Used when p4type is P4_VTAB */
15452 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
15453 u32 *ai; /* Used when p4type is P4_INTARRAY */
15454 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
15455 Table *pTab; /* Used when p4type is P4_TABLE */
15456#ifdef SQLITE_ENABLE_CURSOR_HINTS
15457 Expr *pExpr; /* Used when p4type is P4_EXPR */
15458#endif
15459 int (*xAdvance)(BtCursor *, int);
15460 } p4;
15461#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15462 char *zComment; /* Comment to improve readability */
15463#endif
15464#ifdef VDBE_PROFILE
15465 u32 cnt; /* Number of times this instruction was executed */
15466 u64 cycles; /* Total time spent executing this instruction */
15467#endif
15468#ifdef SQLITE_VDBE_COVERAGE
15469 u32 iSrcLine; /* Source-code line that generated this opcode
15470 ** with flags in the upper 8 bits */
15471#endif
15472};
15473typedef struct VdbeOp VdbeOp;
15474
15475
15476/*
15477** A sub-routine used to implement a trigger program.
15478*/
15479struct SubProgram {
15480 VdbeOp *aOp; /* Array of opcodes for sub-program */
15481 int nOp; /* Elements in aOp[] */
15482 int nMem; /* Number of memory cells required */
15483 int nCsr; /* Number of cursors required */
15484 u8 *aOnce; /* Array of OP_Once flags */
15485 void *token; /* id that may be used to recursive triggers */
15486 SubProgram *pNext; /* Next sub-program already visited */
15487};
15488
15489/*
15490** A smaller version of VdbeOp used for the VdbeAddOpList() function because
15491** it takes up less space.
15492*/
15493struct VdbeOpList {
15494 u8 opcode; /* What operation to perform */
15495 signed char p1; /* First operand */
15496 signed char p2; /* Second parameter (often the jump destination) */
15497 signed char p3; /* Third parameter */
15498};
15499typedef struct VdbeOpList VdbeOpList;
15500
15501/*
15502** Allowed values of VdbeOp.p4type
15503*/
15504#define P4_NOTUSED 0 /* The P4 parameter is not used */
15505#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
15506#define P4_STATIC (-1) /* Pointer to a static string */
15507#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15508#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15509#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15510#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
15511#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
15512/* Above do not own any resources. Must free those below */
15513#define P4_FREE_IF_LE (-7)
15514#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
15515#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
15516#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
15517#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
15518#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
15519#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
15520#define P4_REAL (-13) /* P4 is a 64-bit floating point value */
15521#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
15522#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
15523#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
15524#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
15525
15526/* Error message codes for OP_Halt */
15527#define P5_ConstraintNotNull 1
15528#define P5_ConstraintUnique 2
15529#define P5_ConstraintCheck 3
15530#define P5_ConstraintFK 4
15531
15532/*
15533** The Vdbe.aColName array contains 5n Mem structures, where n is the
15534** number of columns of data returned by the statement.
15535*/
15536#define COLNAME_NAME 0
15537#define COLNAME_DECLTYPE 1
15538#define COLNAME_DATABASE 2
15539#define COLNAME_TABLE 3
15540#define COLNAME_COLUMN 4
15541#ifdef SQLITE_ENABLE_COLUMN_METADATA
15542# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
15543#else
15544# ifdef SQLITE_OMIT_DECLTYPE
15545# define COLNAME_N 1 /* Store only the name */
15546# else
15547# define COLNAME_N 2 /* Store the name and decltype */
15548# endif
15549#endif
15550
15551/*
15552** The following macro converts a label returned by sqlite3VdbeMakeLabel()
15553** into an index into the Parse.aLabel[] array that contains the resolved
15554** address of that label.
15555*/
15556#define ADDR(X) (~(X))
15557
15558/*
15559** The makefile scans the vdbe.c source file and creates the "opcodes.h"
15560** header file that defines a number for each opcode used by the VDBE.
15561*/
15562/************** Include opcodes.h in the middle of vdbe.h ********************/
15563/************** Begin file opcodes.h *****************************************/
15564/* Automatically generated. Do not edit */
15565/* See the tool/mkopcodeh.tcl script for details */
15566#define OP_Savepoint 0
15567#define OP_AutoCommit 1
15568#define OP_Transaction 2
15569#define OP_SorterNext 3 /* jump */
15570#define OP_Prev 4 /* jump */
15571#define OP_Next 5 /* jump */
15572#define OP_Checkpoint 6
15573#define OP_JournalMode 7
15574#define OP_Vacuum 8
15575#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */
15576#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */
15577#define OP_Goto 11 /* jump */
15578#define OP_Gosub 12 /* jump */
15579#define OP_InitCoroutine 13 /* jump */
15580#define OP_Yield 14 /* jump */
15581#define OP_MustBeInt 15 /* jump */
15582#define OP_Jump 16 /* jump */
15583#define OP_Once 17 /* jump */
15584#define OP_If 18 /* jump */
15585#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15586#define OP_IfNot 20 /* jump */
15587#define OP_IfNullRow 21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15588#define OP_SeekLT 22 /* jump, synopsis: key=r[P3@P4] */
15589#define OP_SeekLE 23 /* jump, synopsis: key=r[P3@P4] */
15590#define OP_SeekGE 24 /* jump, synopsis: key=r[P3@P4] */
15591#define OP_SeekGT 25 /* jump, synopsis: key=r[P3@P4] */
15592#define OP_IfNotOpen 26 /* jump, synopsis: if( !csr[P1] ) goto P2 */
15593#define OP_IfNoHope 27 /* jump, synopsis: key=r[P3@P4] */
15594#define OP_NoConflict 28 /* jump, synopsis: key=r[P3@P4] */
15595#define OP_NotFound 29 /* jump, synopsis: key=r[P3@P4] */
15596#define OP_Found 30 /* jump, synopsis: key=r[P3@P4] */
15597#define OP_SeekRowid 31 /* jump, synopsis: intkey=r[P3] */
15598#define OP_NotExists 32 /* jump, synopsis: intkey=r[P3] */
15599#define OP_Last 33 /* jump */
15600#define OP_IfSmaller 34 /* jump */
15601#define OP_SorterSort 35 /* jump */
15602#define OP_Sort 36 /* jump */
15603#define OP_Rewind 37 /* jump */
15604#define OP_IdxLE 38 /* jump, synopsis: key=r[P3@P4] */
15605#define OP_IdxGT 39 /* jump, synopsis: key=r[P3@P4] */
15606#define OP_IdxLT 40 /* jump, synopsis: key=r[P3@P4] */
15607#define OP_IdxGE 41 /* jump, synopsis: key=r[P3@P4] */
15608#define OP_RowSetRead 42 /* jump, synopsis: r[P3]=rowset(P1) */
15609#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
15610#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
15611#define OP_RowSetTest 45 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
15612#define OP_Program 46 /* jump */
15613#define OP_FkIfZero 47 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
15614#define OP_IfPos 48 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15615#define OP_IfNotZero 49 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15616#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
15617#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
15618#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
15619#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
15620#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
15621#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
15622#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
15623#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
15624#define OP_ElseNotEq 58 /* jump, same as TK_ESCAPE */
15625#define OP_DecrJumpZero 59 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15626#define OP_IncrVacuum 60 /* jump */
15627#define OP_VNext 61 /* jump */
15628#define OP_Init 62 /* jump, synopsis: Start at P2 */
15629#define OP_PureFunc 63 /* synopsis: r[P3]=func(r[P2@NP]) */
15630#define OP_Function 64 /* synopsis: r[P3]=func(r[P2@NP]) */
15631#define OP_Return 65
15632#define OP_EndCoroutine 66
15633#define OP_HaltIfNull 67 /* synopsis: if r[P3]=null halt */
15634#define OP_Halt 68
15635#define OP_Integer 69 /* synopsis: r[P2]=P1 */
15636#define OP_Int64 70 /* synopsis: r[P2]=P4 */
15637#define OP_String 71 /* synopsis: r[P2]='P4' (len=P1) */
15638#define OP_Null 72 /* synopsis: r[P2..P3]=NULL */
15639#define OP_SoftNull 73 /* synopsis: r[P1]=NULL */
15640#define OP_Blob 74 /* synopsis: r[P2]=P4 (len=P1) */
15641#define OP_Variable 75 /* synopsis: r[P2]=parameter(P1,P4) */
15642#define OP_Move 76 /* synopsis: r[P2@P3]=r[P1@P3] */
15643#define OP_Copy 77 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
15644#define OP_SCopy 78 /* synopsis: r[P2]=r[P1] */
15645#define OP_IntCopy 79 /* synopsis: r[P2]=r[P1] */
15646#define OP_ResultRow 80 /* synopsis: output=r[P1@P2] */
15647#define OP_CollSeq 81
15648#define OP_AddImm 82 /* synopsis: r[P1]=r[P1]+P2 */
15649#define OP_RealAffinity 83
15650#define OP_Cast 84 /* synopsis: affinity(r[P1]) */
15651#define OP_Permutation 85
15652#define OP_Compare 86 /* synopsis: r[P1@P3] <-> r[P2@P3] */
15653#define OP_IsTrue 87 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
15654#define OP_Offset 88 /* synopsis: r[P3] = sqlite_offset(P1) */
15655#define OP_Column 89 /* synopsis: r[P3]=PX */
15656#define OP_Affinity 90 /* synopsis: affinity(r[P1@P2]) */
15657#define OP_MakeRecord 91 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
15658#define OP_Count 92 /* synopsis: r[P2]=count() */
15659#define OP_ReadCookie 93
15660#define OP_SetCookie 94
15661#define OP_ReopenIdx 95 /* synopsis: root=P2 iDb=P3 */
15662#define OP_OpenRead 96 /* synopsis: root=P2 iDb=P3 */
15663#define OP_OpenWrite 97 /* synopsis: root=P2 iDb=P3 */
15664#define OP_OpenDup 98
15665#define OP_OpenAutoindex 99 /* synopsis: nColumn=P2 */
15666#define OP_OpenEphemeral 100 /* synopsis: nColumn=P2 */
15667#define OP_BitAnd 101 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
15668#define OP_BitOr 102 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
15669#define OP_ShiftLeft 103 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
15670#define OP_ShiftRight 104 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
15671#define OP_Add 105 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
15672#define OP_Subtract 106 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15673#define OP_Multiply 107 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
15674#define OP_Divide 108 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
15675#define OP_Remainder 109 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
15676#define OP_Concat 110 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
15677#define OP_SorterOpen 111
15678#define OP_BitNot 112 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
15679#define OP_SequenceTest 113 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
15680#define OP_OpenPseudo 114 /* synopsis: P3 columns in r[P2] */
15681#define OP_String8 115 /* same as TK_STRING, synopsis: r[P2]='P4' */
15682#define OP_Close 116
15683#define OP_ColumnsUsed 117
15684#define OP_SeekHit 118 /* synopsis: seekHit=P2 */
15685#define OP_Sequence 119 /* synopsis: r[P2]=cursor[P1].ctr++ */
15686#define OP_NewRowid 120 /* synopsis: r[P2]=rowid */
15687#define OP_Insert 121 /* synopsis: intkey=r[P3] data=r[P2] */
15688#define OP_Delete 122
15689#define OP_ResetCount 123
15690#define OP_SorterCompare 124 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
15691#define OP_SorterData 125 /* synopsis: r[P2]=data */
15692#define OP_RowData 126 /* synopsis: r[P2]=data */
15693#define OP_Rowid 127 /* synopsis: r[P2]=rowid */
15694#define OP_NullRow 128
15695#define OP_SeekEnd 129
15696#define OP_IdxInsert 130 /* synopsis: key=r[P2] */
15697#define OP_SorterInsert 131 /* synopsis: key=r[P2] */
15698#define OP_IdxDelete 132 /* synopsis: key=r[P2@P3] */
15699#define OP_DeferredSeek 133 /* synopsis: Move P3 to P1.rowid if needed */
15700#define OP_IdxRowid 134 /* synopsis: r[P2]=rowid */
15701#define OP_FinishSeek 135
15702#define OP_Destroy 136
15703#define OP_Clear 137
15704#define OP_ResetSorter 138
15705#define OP_CreateBtree 139 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
15706#define OP_SqlExec 140
15707#define OP_ParseSchema 141
15708#define OP_LoadAnalysis 142
15709#define OP_DropTable 143
15710#define OP_DropIndex 144
15711#define OP_DropTrigger 145
15712#define OP_IntegrityCk 146
15713#define OP_RowSetAdd 147 /* synopsis: rowset(P1)=r[P2] */
15714#define OP_Param 148
15715#define OP_FkCounter 149 /* synopsis: fkctr[P1]+=P2 */
15716#define OP_Real 150 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
15717#define OP_MemMax 151 /* synopsis: r[P1]=max(r[P1],r[P2]) */
15718#define OP_OffsetLimit 152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
15719#define OP_AggInverse 153 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
15720#define OP_AggStep 154 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15721#define OP_AggStep1 155 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15722#define OP_AggValue 156 /* synopsis: r[P3]=value N=P2 */
15723#define OP_AggFinal 157 /* synopsis: accum=r[P1] N=P2 */
15724#define OP_Expire 158
15725#define OP_CursorLock 159
15726#define OP_CursorUnlock 160
15727#define OP_TableLock 161 /* synopsis: iDb=P1 root=P2 write=P3 */
15728#define OP_VBegin 162
15729#define OP_VCreate 163
15730#define OP_VDestroy 164
15731#define OP_VOpen 165
15732#define OP_VColumn 166 /* synopsis: r[P3]=vcolumn(P2) */
15733#define OP_VRename 167
15734#define OP_Pagecount 168
15735#define OP_MaxPgcnt 169
15736#define OP_Trace 170
15737#define OP_CursorHint 171
15738#define OP_ReleaseReg 172 /* synopsis: release r[P1@P2] mask P3 */
15739#define OP_Noop 173
15740#define OP_Explain 174
15741#define OP_Abortable 175
15742
15743/* Properties such as "out2" or "jump" that are specified in
15744** comments following the "case" for each opcode in the vdbe.c
15745** are encoded into bitvectors as follows:
15746*/
15747#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
15748#define OPFLG_IN1 0x02 /* in1: P1 is an input */
15749#define OPFLG_IN2 0x04 /* in2: P2 is an input */
15750#define OPFLG_IN3 0x08 /* in3: P3 is an input */
15751#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
15752#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
15753#define OPFLG_INITIALIZER {\
15754/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
15755/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
15756/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
15757/* 24 */ 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09,\
15758/* 32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15759/* 40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
15760/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15761/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x00,\
15762/* 64 */ 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10,\
15763/* 72 */ 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10,\
15764/* 80 */ 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x12,\
15765/* 88 */ 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
15766/* 96 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x26, 0x26,\
15767/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
15768/* 112 */ 0x12, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10,\
15769/* 120 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
15770/* 128 */ 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00,\
15771/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
15772/* 144 */ 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x10, 0x04,\
15773/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15774/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15775/* 168 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15776}
15777
15778/* The sqlite3P2Values() routine is able to run faster if it knows
15779** the value of the largest JUMP opcode. The smaller the maximum
15780** JUMP opcode the better, so the mkopcodeh.tcl script that
15781** generated this include file strives to group all JUMP opcodes
15782** together near the beginning of the list.
15783*/
15784#define SQLITE_MX_JUMP_OPCODE 62 /* Maximum JUMP opcode */
15785
15786/************** End of opcodes.h *********************************************/
15787/************** Continuing where we left off in vdbe.h ***********************/
15788
15789/*
15790** Additional non-public SQLITE_PREPARE_* flags
15791*/
15792#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
15793#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
15794
15795/*
15796** Prototypes for the VDBE interface. See comments on the implementation
15797** for a description of what each of these routines does.
15798*/
15799SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
15800SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe*);
15801SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
15802SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
15803SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15804SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
15805SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15806SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15807SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15808SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15809SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15810SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15811SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
15812SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
15813#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15814SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15815SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15816#else
15817# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15818# define sqlite3VdbeVerifyNoResultRow(A)
15819#endif
15820#if defined(SQLITE_DEBUG)
15821SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15822#else
15823# define sqlite3VdbeVerifyAbortable(A,B)
15824#endif
15825SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15826#ifndef SQLITE_OMIT_EXPLAIN
15827SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15828SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
15829SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
15830# define ExplainQueryPlan(P) sqlite3VdbeExplain P
15831# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
15832# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
15833#else
15834# define ExplainQueryPlan(P)
15835# define ExplainQueryPlanPop(P)
15836# define ExplainQueryPlanParent(P) 0
15837# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15838#endif
15839#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
15840SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char*,const char*);
15841#else
15842# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15843#endif
15844SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
15845SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
15846SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
15847SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
15848SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
15849SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15850SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
15851SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
15852SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15853SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15854#ifdef SQLITE_DEBUG
15855SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
15856#else
15857# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
15858#endif
15859SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15860SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15861SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15862SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
15863SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15864SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
15865SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
15866SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
15867SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
15868SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
15869SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15870SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
15871SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
15872SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
15873#ifdef SQLITE_DEBUG
15874SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15875#endif
15876SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
15877SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
15878SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
15879SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
15880SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15881SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
15882SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
15883SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
15884SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15885#ifdef SQLITE_ENABLE_NORMALIZE
15886SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
15887SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
15888#endif
15889SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15890SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15891SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15892SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
15893#ifndef SQLITE_OMIT_TRACE
15894SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15895#endif
15896SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15897SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
15898
15899SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15900SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15901SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15902SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15903
15904typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15905SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15906
15907SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15908SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*);
15909
15910SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
15911#ifdef SQLITE_ENABLE_BYTECODE_VTAB
15912SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
15913#endif
15914
15915/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15916** each VDBE opcode.
15917**
15918** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15919** comments in VDBE programs that show key decision points in the code
15920** generator.
15921*/
15922#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15923SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
15924# define VdbeComment(X) sqlite3VdbeComment X
15925SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15926# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15927# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15928# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15929# else
15930# define VdbeModuleComment(X)
15931# endif
15932#else
15933# define VdbeComment(X)
15934# define VdbeNoopComment(X)
15935# define VdbeModuleComment(X)
15936#endif
15937
15938/*
15939** The VdbeCoverage macros are used to set a coverage testing point
15940** for VDBE branch instructions. The coverage testing points are line
15941** numbers in the sqlite3.c source file. VDBE branch coverage testing
15942** only works with an amalagmation build. That's ok since a VDBE branch
15943** coverage build designed for testing the test suite only. No application
15944** should ever ship with VDBE branch coverage measuring turned on.
15945**
15946** VdbeCoverage(v) // Mark the previously coded instruction
15947** // as a branch
15948**
15949** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15950**
15951** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15952**
15953** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15954**
15955** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15956** // taken on the first two ways. The
15957** // NULL option is not possible
15958**
15959** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15960** // in distingishing equal and not-equal.
15961**
15962** Every VDBE branch operation must be tagged with one of the macros above.
15963** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15964** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15965** routine in vdbe.c, alerting the developer to the missed tag.
15966**
15967** During testing, the test application will invoke
15968** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15969** routine that is invoked as each bytecode branch is taken. The callback
15970** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15971** flags to indicate whether or not the branch was taken. The test application
15972** is responsible for keeping track of this and reporting byte-code branches
15973** that are never taken.
15974**
15975** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15976** vdbe.c source file for additional information.
15977*/
15978#ifdef SQLITE_VDBE_COVERAGE
15979SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
15980# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
15981# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
15982# define VdbeCoverageAlwaysTaken(v) \
15983 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
15984# define VdbeCoverageNeverTaken(v) \
15985 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
15986# define VdbeCoverageNeverNull(v) \
15987 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15988# define VdbeCoverageNeverNullIf(v,x) \
15989 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15990# define VdbeCoverageEqNe(v) \
15991 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
15992# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
15993#else
15994# define VdbeCoverage(v)
15995# define VdbeCoverageIf(v,x)
15996# define VdbeCoverageAlwaysTaken(v)
15997# define VdbeCoverageNeverTaken(v)
15998# define VdbeCoverageNeverNull(v)
15999# define VdbeCoverageNeverNullIf(v,x)
16000# define VdbeCoverageEqNe(v)
16001# define VDBE_OFFSET_LINENO(x) 0
16002#endif
16003
16004#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
16005SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
16006#else
16007# define sqlite3VdbeScanStatus(a,b,c,d,e)
16008#endif
16009
16010#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
16011SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
16012#endif
16013
16014#endif /* SQLITE_VDBE_H */
16015
16016/************** End of vdbe.h ************************************************/
16017/************** Continuing where we left off in sqliteInt.h ******************/
16018/************** Include pcache.h in the middle of sqliteInt.h ****************/
16019/************** Begin file pcache.h ******************************************/
16020/*
16021** 2008 August 05
16022**
16023** The author disclaims copyright to this source code. In place of
16024** a legal notice, here is a blessing:
16025**
16026** May you do good and not evil.
16027** May you find forgiveness for yourself and forgive others.
16028** May you share freely, never taking more than you give.
16029**
16030*************************************************************************
16031** This header file defines the interface that the sqlite page cache
16032** subsystem.
16033*/
16034
16035#ifndef _PCACHE_H_
16036
16037typedef struct PgHdr PgHdr;
16038typedef struct PCache PCache;
16039
16040/*
16041** Every page in the cache is controlled by an instance of the following
16042** structure.
16043*/
16044struct PgHdr {
16045 sqlite3_pcache_page *pPage; /* Pcache object page handle */
16046 void *pData; /* Page data */
16047 void *pExtra; /* Extra content */
16048 PCache *pCache; /* PRIVATE: Cache that owns this page */
16049 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
16050 Pager *pPager; /* The pager this page is part of */
16051 Pgno pgno; /* Page number for this page */
16052#ifdef SQLITE_CHECK_PAGES
16053 u32 pageHash; /* Hash of page content */
16054#endif
16055 u16 flags; /* PGHDR flags defined below */
16056
16057 /**********************************************************************
16058 ** Elements above, except pCache, are public. All that follow are
16059 ** private to pcache.c and should not be accessed by other modules.
16060 ** pCache is grouped with the public elements for efficiency.
16061 */
16062 i16 nRef; /* Number of users of this page */
16063 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
16064 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
16065 /* NB: pDirtyNext and pDirtyPrev are undefined if the
16066 ** PgHdr object is not dirty */
16067};
16068
16069/* Bit values for PgHdr.flags */
16070#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
16071#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
16072#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
16073#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
16074 ** writing this page to the database */
16075#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
16076#define PGHDR_MMAP 0x020 /* This is an mmap page object */
16077
16078#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
16079
16080/* Initialize and shutdown the page cache subsystem */
16081SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
16082SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
16083
16084/* Page cache buffer management:
16085** These routines implement SQLITE_CONFIG_PAGECACHE.
16086*/
16087SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
16088
16089/* Create a new pager cache.
16090** Under memory stress, invoke xStress to try to make pages clean.
16091** Only clean and unpinned pages can be reclaimed.
16092*/
16093SQLITE_PRIVATE int sqlite3PcacheOpen(
16094 int szPage, /* Size of every page */
16095 int szExtra, /* Extra space associated with each page */
16096 int bPurgeable, /* True if pages are on backing store */
16097 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
16098 void *pStress, /* Argument to xStress */
16099 PCache *pToInit /* Preallocated space for the PCache */
16100);
16101
16102/* Modify the page-size after the cache has been created. */
16103SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
16104
16105/* Return the size in bytes of a PCache object. Used to preallocate
16106** storage space.
16107*/
16108SQLITE_PRIVATE int sqlite3PcacheSize(void);
16109
16110/* One release per successful fetch. Page is pinned until released.
16111** Reference counted.
16112*/
16113SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
16114SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
16115SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
16116SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
16117
16118SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
16119SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
16120SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
16121SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
16122SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
16123
16124/* Change a page number. Used by incr-vacuum. */
16125SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
16126
16127/* Remove all pages with pgno>x. Reset the cache if x==0 */
16128SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
16129
16130/* Get a list of all dirty pages in the cache, sorted by page number */
16131SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
16132
16133/* Reset and close the cache object */
16134SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
16135
16136/* Clear flags from pages of the page cache */
16137SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
16138
16139/* Discard the contents of the cache */
16140SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
16141
16142/* Return the total number of outstanding page references */
16143SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
16144
16145/* Increment the reference count of an existing page */
16146SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
16147
16148SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
16149
16150/* Return the total number of pages stored in the cache */
16151SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
16152
16153#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
16154/* Iterate through all dirty pages currently stored in the cache. This
16155** interface is only available if SQLITE_CHECK_PAGES is defined when the
16156** library is built.
16157*/
16158SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
16159#endif
16160
16161#if defined(SQLITE_DEBUG)
16162/* Check invariants on a PgHdr object */
16163SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
16164#endif
16165
16166/* Set and get the suggested cache-size for the specified pager-cache.
16167**
16168** If no global maximum is configured, then the system attempts to limit
16169** the total number of pages cached by purgeable pager-caches to the sum
16170** of the suggested cache-sizes.
16171*/
16172SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
16173#ifdef SQLITE_TEST
16174SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
16175#endif
16176
16177/* Set or get the suggested spill-size for the specified pager-cache.
16178**
16179** The spill-size is the minimum number of pages in cache before the cache
16180** will attempt to spill dirty pages by calling xStress.
16181*/
16182SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
16183
16184/* Free up as much memory as possible from the page cache */
16185SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
16186
16187#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
16188/* Try to return memory used by the pcache module to the main memory heap */
16189SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
16190#endif
16191
16192#ifdef SQLITE_TEST
16193SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
16194#endif
16195
16196SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
16197
16198/* Return the header size */
16199SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
16200SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
16201
16202/* Number of dirty pages as a percentage of the configured cache size */
16203SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
16204
16205#ifdef SQLITE_DIRECT_OVERFLOW_READ
16206SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
16207#endif
16208
16209#endif /* _PCACHE_H_ */
16210
16211/************** End of pcache.h **********************************************/
16212/************** Continuing where we left off in sqliteInt.h ******************/
16213/************** Include os.h in the middle of sqliteInt.h ********************/
16214/************** Begin file os.h **********************************************/
16215/*
16216** 2001 September 16
16217**
16218** The author disclaims copyright to this source code. In place of
16219** a legal notice, here is a blessing:
16220**
16221** May you do good and not evil.
16222** May you find forgiveness for yourself and forgive others.
16223** May you share freely, never taking more than you give.
16224**
16225******************************************************************************
16226**
16227** This header file (together with is companion C source-code file
16228** "os.c") attempt to abstract the underlying operating system so that
16229** the SQLite library will work on both POSIX and windows systems.
16230**
16231** This header file is #include-ed by sqliteInt.h and thus ends up
16232** being included by every source file.
16233*/
16234#ifndef _SQLITE_OS_H_
16235#define _SQLITE_OS_H_
16236
16237/*
16238** Attempt to automatically detect the operating system and setup the
16239** necessary pre-processor macros for it.
16240*/
16241/************** Include os_setup.h in the middle of os.h *********************/
16242/************** Begin file os_setup.h ****************************************/
16243/*
16244** 2013 November 25
16245**
16246** The author disclaims copyright to this source code. In place of
16247** a legal notice, here is a blessing:
16248**
16249** May you do good and not evil.
16250** May you find forgiveness for yourself and forgive others.
16251** May you share freely, never taking more than you give.
16252**
16253******************************************************************************
16254**
16255** This file contains pre-processor directives related to operating system
16256** detection and/or setup.
16257*/
16258#ifndef SQLITE_OS_SETUP_H
16259#define SQLITE_OS_SETUP_H
16260
16261/*
16262** Figure out if we are dealing with Unix, Windows, or some other operating
16263** system.
16264**
16265** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
16266** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
16267** the three will be 1. The other two will be 0.
16268*/
16269#if defined(SQLITE_OS_OTHER)
16270# if SQLITE_OS_OTHER==1
16271# undef SQLITE_OS_UNIX
16272# define SQLITE_OS_UNIX 0
16273# undef SQLITE_OS_WIN
16274# define SQLITE_OS_WIN 0
16275# else
16276# undef SQLITE_OS_OTHER
16277# endif
16278#endif
16279#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
16280# define SQLITE_OS_OTHER 0
16281# ifndef SQLITE_OS_WIN
16282# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
16283 defined(__MINGW32__) || defined(__BORLANDC__)
16284# define SQLITE_OS_WIN 1
16285# define SQLITE_OS_UNIX 0
16286# else
16287# define SQLITE_OS_WIN 0
16288# define SQLITE_OS_UNIX 1
16289# endif
16290# else
16291# define SQLITE_OS_UNIX 0
16292# endif
16293#else
16294# ifndef SQLITE_OS_WIN
16295# define SQLITE_OS_WIN 0
16296# endif
16297#endif
16298
16299#endif /* SQLITE_OS_SETUP_H */
16300
16301/************** End of os_setup.h ********************************************/
16302/************** Continuing where we left off in os.h *************************/
16303
16304/* If the SET_FULLSYNC macro is not defined above, then make it
16305** a no-op
16306*/
16307#ifndef SET_FULLSYNC
16308# define SET_FULLSYNC(x,y)
16309#endif
16310
16311/*
16312** The default size of a disk sector
16313*/
16314#ifndef SQLITE_DEFAULT_SECTOR_SIZE
16315# define SQLITE_DEFAULT_SECTOR_SIZE 4096
16316#endif
16317
16318/*
16319** Temporary files are named starting with this prefix followed by 16 random
16320** alphanumeric characters, and no file extension. They are stored in the
16321** OS's standard temporary file directory, and are deleted prior to exit.
16322** If sqlite is being embedded in another program, you may wish to change the
16323** prefix to reflect your program's name, so that if your program exits
16324** prematurely, old temporary files can be easily identified. This can be done
16325** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16326**
16327** 2006-10-31: The default prefix used to be "sqlite_". But then
16328** Mcafee started using SQLite in their anti-virus product and it
16329** started putting files with the "sqlite" name in the c:/temp folder.
16330** This annoyed many windows users. Those users would then do a
16331** Google search for "sqlite", find the telephone numbers of the
16332** developers and call to wake them up at night and complain.
16333** For this reason, the default name prefix is changed to be "sqlite"
16334** spelled backwards. So the temp files are still identified, but
16335** anybody smart enough to figure out the code is also likely smart
16336** enough to know that calling the developer will not help get rid
16337** of the file.
16338*/
16339#ifndef SQLITE_TEMP_FILE_PREFIX
16340# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
16341#endif
16342
16343/*
16344** The following values may be passed as the second argument to
16345** sqlite3OsLock(). The various locks exhibit the following semantics:
16346**
16347** SHARED: Any number of processes may hold a SHARED lock simultaneously.
16348** RESERVED: A single process may hold a RESERVED lock on a file at
16349** any time. Other processes may hold and obtain new SHARED locks.
16350** PENDING: A single process may hold a PENDING lock on a file at
16351** any one time. Existing SHARED locks may persist, but no new
16352** SHARED locks may be obtained by other processes.
16353** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16354**
16355** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16356** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16357** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16358** sqlite3OsLock().
16359*/
16360#define NO_LOCK 0
16361#define SHARED_LOCK 1
16362#define RESERVED_LOCK 2
16363#define PENDING_LOCK 3
16364#define EXCLUSIVE_LOCK 4
16365
16366/*
16367** File Locking Notes: (Mostly about windows but also some info for Unix)
16368**
16369** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16370** those functions are not available. So we use only LockFile() and
16371** UnlockFile().
16372**
16373** LockFile() prevents not just writing but also reading by other processes.
16374** A SHARED_LOCK is obtained by locking a single randomly-chosen
16375** byte out of a specific range of bytes. The lock byte is obtained at
16376** random so two separate readers can probably access the file at the
16377** same time, unless they are unlucky and choose the same lock byte.
16378** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16379** There can only be one writer. A RESERVED_LOCK is obtained by locking
16380** a single byte of the file that is designated as the reserved lock byte.
16381** A PENDING_LOCK is obtained by locking a designated byte different from
16382** the RESERVED_LOCK byte.
16383**
16384** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16385** which means we can use reader/writer locks. When reader/writer locks
16386** are used, the lock is placed on the same range of bytes that is used
16387** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16388** will support two or more Win95 readers or two or more WinNT readers.
16389** But a single Win95 reader will lock out all WinNT readers and a single
16390** WinNT reader will lock out all other Win95 readers.
16391**
16392** The following #defines specify the range of bytes used for locking.
16393** SHARED_SIZE is the number of bytes available in the pool from which
16394** a random byte is selected for a shared lock. The pool of bytes for
16395** shared locks begins at SHARED_FIRST.
16396**
16397** The same locking strategy and
16398** byte ranges are used for Unix. This leaves open the possibility of having
16399** clients on win95, winNT, and unix all talking to the same shared file
16400** and all locking correctly. To do so would require that samba (or whatever
16401** tool is being used for file sharing) implements locks correctly between
16402** windows and unix. I'm guessing that isn't likely to happen, but by
16403** using the same locking range we are at least open to the possibility.
16404**
16405** Locking in windows is manditory. For this reason, we cannot store
16406** actual data in the bytes used for locking. The pager never allocates
16407** the pages involved in locking therefore. SHARED_SIZE is selected so
16408** that all locks will fit on a single page even at the minimum page size.
16409** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16410** is set high so that we don't have to allocate an unused page except
16411** for very large databases. But one should test the page skipping logic
16412** by setting PENDING_BYTE low and running the entire regression suite.
16413**
16414** Changing the value of PENDING_BYTE results in a subtly incompatible
16415** file format. Depending on how it is changed, you might not notice
16416** the incompatibility right away, even running a full regression test.
16417** The default location of PENDING_BYTE is the first byte past the
16418** 1GB boundary.
16419**
16420*/
16421#ifdef SQLITE_OMIT_WSD
16422# define PENDING_BYTE (0x40000000)
16423#else
16424# define PENDING_BYTE sqlite3PendingByte
16425#endif
16426#define RESERVED_BYTE (PENDING_BYTE+1)
16427#define SHARED_FIRST (PENDING_BYTE+2)
16428#define SHARED_SIZE 510
16429
16430/*
16431** Wrapper around OS specific sqlite3_os_init() function.
16432*/
16433SQLITE_PRIVATE int sqlite3OsInit(void);
16434
16435/*
16436** Functions for accessing sqlite3_file methods
16437*/
16438SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
16439SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16440SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16441SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
16442SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
16443SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16444SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
16445SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
16446SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16447SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
16448SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16449#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
16450SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
16451SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16452#ifndef SQLITE_OMIT_WAL
16453SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16454SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16455SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
16456SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
16457#endif /* SQLITE_OMIT_WAL */
16458SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16459SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16460
16461
16462/*
16463** Functions for accessing sqlite3_vfs methods
16464*/
16465SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16466SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16467SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16468SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16469#ifndef SQLITE_OMIT_LOAD_EXTENSION
16470SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16471SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16472SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16473SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
16474#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16475SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16476SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
16477SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
16478SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16479
16480/*
16481** Convenience functions for opening and closing files using
16482** sqlite3_malloc() to obtain space for the file-handle structure.
16483*/
16484SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16485SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
16486
16487#endif /* _SQLITE_OS_H_ */
16488
16489/************** End of os.h **************************************************/
16490/************** Continuing where we left off in sqliteInt.h ******************/
16491/************** Include mutex.h in the middle of sqliteInt.h *****************/
16492/************** Begin file mutex.h *******************************************/
16493/*
16494** 2007 August 28
16495**
16496** The author disclaims copyright to this source code. In place of
16497** a legal notice, here is a blessing:
16498**
16499** May you do good and not evil.
16500** May you find forgiveness for yourself and forgive others.
16501** May you share freely, never taking more than you give.
16502**
16503*************************************************************************
16504**
16505** This file contains the common header for all mutex implementations.
16506** The sqliteInt.h header #includes this file so that it is available
16507** to all source files. We break it out in an effort to keep the code
16508** better organized.
16509**
16510** NOTE: source files should *not* #include this header file directly.
16511** Source files should #include the sqliteInt.h file and let that file
16512** include this one indirectly.
16513*/
16514
16515
16516/*
16517** Figure out what version of the code to use. The choices are
16518**
16519** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
16520** mutexes implementation cannot be overridden
16521** at start-time.
16522**
16523** SQLITE_MUTEX_NOOP For single-threaded applications. No
16524** mutual exclusion is provided. But this
16525** implementation can be overridden at
16526** start-time.
16527**
16528** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16529**
16530** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16531*/
16532#if !SQLITE_THREADSAFE
16533# define SQLITE_MUTEX_OMIT
16534#endif
16535#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
16536# if SQLITE_OS_UNIX
16537# define SQLITE_MUTEX_PTHREADS
16538# elif SQLITE_OS_WIN
16539# define SQLITE_MUTEX_W32
16540# else
16541# define SQLITE_MUTEX_NOOP
16542# endif
16543#endif
16544
16545#ifdef SQLITE_MUTEX_OMIT
16546/*
16547** If this is a no-op implementation, implement everything as macros.
16548*/
16549#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
16550#define sqlite3_mutex_free(X)
16551#define sqlite3_mutex_enter(X)
16552#define sqlite3_mutex_try(X) SQLITE_OK
16553#define sqlite3_mutex_leave(X)
16554#define sqlite3_mutex_held(X) ((void)(X),1)
16555#define sqlite3_mutex_notheld(X) ((void)(X),1)
16556#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
16557#define sqlite3MutexInit() SQLITE_OK
16558#define sqlite3MutexEnd()
16559#define MUTEX_LOGIC(X)
16560#else
16561#define MUTEX_LOGIC(X) X
16562SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
16563#endif /* defined(SQLITE_MUTEX_OMIT) */
16564
16565/************** End of mutex.h ***********************************************/
16566/************** Continuing where we left off in sqliteInt.h ******************/
16567
16568/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16569** synchronous setting to EXTRA. It is no longer supported.
16570*/
16571#ifdef SQLITE_EXTRA_DURABLE
16572# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
16573# define SQLITE_DEFAULT_SYNCHRONOUS 3
16574#endif
16575
16576/*
16577** Default synchronous levels.
16578**
16579** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16580** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16581**
16582** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
16583** OFF 1 0
16584** NORMAL 2 1
16585** FULL 3 2
16586** EXTRA 4 3
16587**
16588** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16589** In other words, the zero-based numbers are used for all external interfaces
16590** and the one-based values are used internally.
16591*/
16592#ifndef SQLITE_DEFAULT_SYNCHRONOUS
16593# define SQLITE_DEFAULT_SYNCHRONOUS 2
16594#endif
16595#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
16596# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
16597#endif
16598
16599/*
16600** Each database file to be accessed by the system is an instance
16601** of the following structure. There are normally two of these structures
16602** in the sqlite.aDb[] array. aDb[0] is the main database file and
16603** aDb[1] is the database file used to hold temporary tables. Additional
16604** databases may be attached.
16605*/
16606struct Db {
16607 char *zDbSName; /* Name of this database. (schema name, not filename) */
16608 Btree *pBt; /* The B*Tree structure for this database file */
16609 u8 safety_level; /* How aggressive at syncing data to disk */
16610 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
16611 Schema *pSchema; /* Pointer to database schema (possibly shared) */
16612};
16613
16614/*
16615** An instance of the following structure stores a database schema.
16616**
16617** Most Schema objects are associated with a Btree. The exception is
16618** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16619** In shared cache mode, a single Schema object can be shared by multiple
16620** Btrees that refer to the same underlying BtShared object.
16621**
16622** Schema objects are automatically deallocated when the last Btree that
16623** references them is destroyed. The TEMP Schema is manually freed by
16624** sqlite3_close().
16625*
16626** A thread must be holding a mutex on the corresponding Btree in order
16627** to access Schema content. This implies that the thread must also be
16628** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16629** For a TEMP Schema, only the connection mutex is required.
16630*/
16631struct Schema {
16632 int schema_cookie; /* Database schema version number for this file */
16633 int iGeneration; /* Generation counter. Incremented with each change */
16634 Hash tblHash; /* All tables indexed by name */
16635 Hash idxHash; /* All (named) indices indexed by name */
16636 Hash trigHash; /* All triggers indexed by name */
16637 Hash fkeyHash; /* All foreign keys by referenced table name */
16638 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
16639 u8 file_format; /* Schema format version for this file */
16640 u8 enc; /* Text encoding used by this database */
16641 u16 schemaFlags; /* Flags associated with this schema */
16642 int cache_size; /* Number of pages to use in the cache */
16643};
16644
16645/*
16646** These macros can be used to test, set, or clear bits in the
16647** Db.pSchema->flags field.
16648*/
16649#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16650#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16651#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16652#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16653
16654/*
16655** Allowed values for the DB.pSchema->flags field.
16656**
16657** The DB_SchemaLoaded flag is set after the database schema has been
16658** read into internal hash tables.
16659**
16660** DB_UnresetViews means that one or more views have column names that
16661** have been filled out. If the schema changes, these column names might
16662** changes and so the view will need to be reset.
16663*/
16664#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
16665#define DB_UnresetViews 0x0002 /* Some views have defined column names */
16666#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
16667
16668/*
16669** The number of different kinds of things that can be limited
16670** using the sqlite3_limit() interface.
16671*/
16672#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
16673
16674/*
16675** Lookaside malloc is a set of fixed-size buffers that can be used
16676** to satisfy small transient memory allocation requests for objects
16677** associated with a particular database connection. The use of
16678** lookaside malloc provides a significant performance enhancement
16679** (approx 10%) by avoiding numerous malloc/free requests while parsing
16680** SQL statements.
16681**
16682** The Lookaside structure holds configuration information about the
16683** lookaside malloc subsystem. Each available memory allocation in
16684** the lookaside subsystem is stored on a linked list of LookasideSlot
16685** objects.
16686**
16687** Lookaside allocations are only allowed for objects that are associated
16688** with a particular database connection. Hence, schema information cannot
16689** be stored in lookaside because in shared cache mode the schema information
16690** is shared by multiple database connections. Therefore, while parsing
16691** schema information, the Lookaside.bEnabled flag is cleared so that
16692** lookaside allocations are not used to construct the schema objects.
16693**
16694** New lookaside allocations are only allowed if bDisable==0. When
16695** bDisable is greater than zero, sz is set to zero which effectively
16696** disables lookaside without adding a new test for the bDisable flag
16697** in a performance-critical path. sz should be set by to szTrue whenever
16698** bDisable changes back to zero.
16699**
16700** Lookaside buffers are initially held on the pInit list. As they are
16701** used and freed, they are added back to the pFree list. New allocations
16702** come off of pFree first, then pInit as a fallback. This dual-list
16703** allows use to compute a high-water mark - the maximum number of allocations
16704** outstanding at any point in the past - by subtracting the number of
16705** allocations on the pInit list from the total number of allocations.
16706**
16707** Enhancement on 2019-12-12: Two-size-lookaside
16708** The default lookaside configuration is 100 slots of 1200 bytes each.
16709** The larger slot sizes are important for performance, but they waste
16710** a lot of space, as most lookaside allocations are less than 128 bytes.
16711** The two-size-lookaside enhancement breaks up the lookaside allocation
16712** into two pools: One of 128-byte slots and the other of the default size
16713** (1200-byte) slots. Allocations are filled from the small-pool first,
16714** failing over to the full-size pool if that does not work. Thus more
16715** lookaside slots are available while also using less memory.
16716** This enhancement can be omitted by compiling with
16717** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
16718*/
16719struct Lookaside {
16720 u32 bDisable; /* Only operate the lookaside when zero */
16721 u16 sz; /* Size of each buffer in bytes */
16722 u16 szTrue; /* True value of sz, even if disabled */
16723 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16724 u32 nSlot; /* Number of lookaside slots allocated */
16725 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16726 LookasideSlot *pInit; /* List of buffers not previously used */
16727 LookasideSlot *pFree; /* List of available buffers */
16728#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16729 LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
16730 LookasideSlot *pSmallFree; /* List of available small buffers */
16731 void *pMiddle; /* First byte past end of full-size buffers and
16732 ** the first byte of LOOKASIDE_SMALL buffers */
16733#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
16734 void *pStart; /* First byte of available memory space */
16735 void *pEnd; /* First byte past end of available space */
16736};
16737struct LookasideSlot {
16738 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16739};
16740
16741#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16742#define EnableLookaside db->lookaside.bDisable--;\
16743 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16744
16745/* Size of the smaller allocations in two-size lookside */
16746#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
16747# define LOOKASIDE_SMALL 0
16748#else
16749# define LOOKASIDE_SMALL 128
16750#endif
16751
16752/*
16753** A hash table for built-in function definitions. (Application-defined
16754** functions use a regular table table from hash.h.)
16755**
16756** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16757** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
16758** macro to compute a hash on the function name.
16759*/
16760#define SQLITE_FUNC_HASH_SZ 23
16761struct FuncDefHash {
16762 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
16763};
16764#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
16765
16766#ifdef SQLITE_USER_AUTHENTICATION
16767/*
16768** Information held in the "sqlite3" database connection object and used
16769** to manage user authentication.
16770*/
16771typedef struct sqlite3_userauth sqlite3_userauth;
16772struct sqlite3_userauth {
16773 u8 authLevel; /* Current authentication level */
16774 int nAuthPW; /* Size of the zAuthPW in bytes */
16775 char *zAuthPW; /* Password used to authenticate */
16776 char *zAuthUser; /* User name used to authenticate */
16777};
16778
16779/* Allowed values for sqlite3_userauth.authLevel */
16780#define UAUTH_Unknown 0 /* Authentication not yet checked */
16781#define UAUTH_Fail 1 /* User authentication failed */
16782#define UAUTH_User 2 /* Authenticated as a normal user */
16783#define UAUTH_Admin 3 /* Authenticated as an administrator */
16784
16785/* Functions used only by user authorization logic */
16786SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
16787SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16788SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
16789SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16790
16791#endif /* SQLITE_USER_AUTHENTICATION */
16792
16793/*
16794** typedef for the authorization callback function.
16795*/
16796#ifdef SQLITE_USER_AUTHENTICATION
16797 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16798 const char*, const char*);
16799#else
16800 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16801 const char*);
16802#endif
16803
16804#ifndef SQLITE_OMIT_DEPRECATED
16805/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16806** in the style of sqlite3_trace()
16807*/
16808#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
16809#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
16810#else
16811#define SQLITE_TRACE_LEGACY 0
16812#define SQLITE_TRACE_XPROFILE 0
16813#endif /* SQLITE_OMIT_DEPRECATED */
16814#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
16815
16816
16817/*
16818** Each database connection is an instance of the following structure.
16819*/
16820struct sqlite3 {
16821 sqlite3_vfs *pVfs; /* OS Interface */
16822 struct Vdbe *pVdbe; /* List of active virtual machines */
16823 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
16824 sqlite3_mutex *mutex; /* Connection mutex */
16825 Db *aDb; /* All backends */
16826 int nDb; /* Number of backends currently in use */
16827 u32 mDbFlags; /* flags recording internal state */
16828 u64 flags; /* flags settable by pragmas. See below */
16829 i64 lastRowid; /* ROWID of most recent insert (see above) */
16830 i64 szMmap; /* Default mmap_size setting */
16831 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16832 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16833 int errCode; /* Most recent error code (SQLITE_*) */
16834 int errMask; /* & result codes with this before returning */
16835 int iSysErrno; /* Errno value from last system error */
16836 u16 dbOptFlags; /* Flags to enable/disable optimizations */
16837 u8 enc; /* Text encoding */
16838 u8 autoCommit; /* The auto-commit flag. */
16839 u8 temp_store; /* 1: file 2: memory 0: default */
16840 u8 mallocFailed; /* True if we have seen a malloc failure */
16841 u8 bBenignMalloc; /* Do not require OOMs if true */
16842 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16843 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16844 u8 suppressErr; /* Do not issue error messages if true */
16845 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16846 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16847 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16848 u8 noSharedCache; /* True if no shared-cache backends */
16849 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16850 int nextPagesize; /* Pagesize after VACUUM if >0 */
16851 u32 magic; /* Magic number for detect library misuse */
16852 int nChange; /* Value returned by sqlite3_changes() */
16853 int nTotalChange; /* Value returned by sqlite3_total_changes() */
16854 int aLimit[SQLITE_N_LIMIT]; /* Limits */
16855 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16856 struct sqlite3InitInfo { /* Information used during initialization */
16857 Pgno newTnum; /* Rootpage of table being initialized */
16858 u8 iDb; /* Which db file is being initialized */
16859 u8 busy; /* TRUE if currently initializing */
16860 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16861 unsigned imposterTable : 1; /* Building an imposter table */
16862 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16863 char **azInit; /* "type", "name", and "tbl_name" columns */
16864 } init;
16865 int nVdbeActive; /* Number of VDBEs currently running */
16866 int nVdbeRead; /* Number of active VDBEs that read or write */
16867 int nVdbeWrite; /* Number of active VDBEs that read and write */
16868 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16869 int nVDestroy; /* Number of active OP_VDestroy operations */
16870 int nExtension; /* Number of loaded extensions */
16871 void **aExtension; /* Array of shared library handles */
16872 union {
16873 void (*xLegacy)(void*,const char*); /* Legacy trace function */
16874 int (*xV2)(u32,void*,void*,void*); /* V2 Trace function */
16875 } trace;
16876 void *pTraceArg; /* Argument to the trace function */
16877#ifndef SQLITE_OMIT_DEPRECATED
16878 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16879 void *pProfileArg; /* Argument to profile function */
16880#endif
16881 void *pCommitArg; /* Argument to xCommitCallback() */
16882 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16883 void *pRollbackArg; /* Argument to xRollbackCallback() */
16884 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16885 void *pUpdateArg;
16886 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16887 Parse *pParse; /* Current parse */
16888#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16889 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16890 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16891 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16892 );
16893 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16894#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16895#ifndef SQLITE_OMIT_WAL
16896 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16897 void *pWalArg;
16898#endif
16899 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16900 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16901 void *pCollNeededArg;
16902 sqlite3_value *pErr; /* Most recent error message */
16903 union {
16904 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16905 double notUsed1; /* Spacer */
16906 } u1;
16907 Lookaside lookaside; /* Lookaside malloc configuration */
16908#ifndef SQLITE_OMIT_AUTHORIZATION
16909 sqlite3_xauth xAuth; /* Access authorization function */
16910 void *pAuthArg; /* 1st argument to the access auth function */
16911#endif
16912#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16913 int (*xProgress)(void *); /* The progress callback */
16914 void *pProgressArg; /* Argument to the progress callback */
16915 unsigned nProgressOps; /* Number of opcodes for progress callback */
16916#endif
16917#ifndef SQLITE_OMIT_VIRTUALTABLE
16918 int nVTrans; /* Allocated size of aVTrans */
16919 Hash aModule; /* populated by sqlite3_create_module() */
16920 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16921 VTable **aVTrans; /* Virtual tables with open transactions */
16922 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16923#endif
16924 Hash aFunc; /* Hash table of connection functions */
16925 Hash aCollSeq; /* All collating sequences */
16926 BusyHandler busyHandler; /* Busy callback */
16927 Db aDbStatic[2]; /* Static space for the 2 default backends */
16928 Savepoint *pSavepoint; /* List of active savepoints */
16929 int nAnalysisLimit; /* Number of index rows to ANALYZE */
16930 int busyTimeout; /* Busy handler timeout, in msec */
16931 int nSavepoint; /* Number of non-transaction savepoints */
16932 int nStatement; /* Number of nested statement-transactions */
16933 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16934 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16935 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16936#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16937 /* The following variables are all protected by the STATIC_MAIN
16938 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16939 **
16940 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16941 ** unlock so that it can proceed.
16942 **
16943 ** When X.pBlockingConnection==Y, that means that something that X tried
16944 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16945 ** held by Y.
16946 */
16947 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16948 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16949 void *pUnlockArg; /* Argument to xUnlockNotify */
16950 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16951 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16952#endif
16953#ifdef SQLITE_USER_AUTHENTICATION
16954 sqlite3_userauth auth; /* User authentication information */
16955#endif
16956};
16957
16958/*
16959** A macro to discover the encoding of a database.
16960*/
16961#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16962#define ENC(db) ((db)->enc)
16963
16964/*
16965** A u64 constant where the lower 32 bits are all zeros. Only the
16966** upper 32 bits are included in the argument. Necessary because some
16967** C-compilers still do not accept LL integer literals.
16968*/
16969#define HI(X) ((u64)(X)<<32)
16970
16971/*
16972** Possible values for the sqlite3.flags.
16973**
16974** Value constraints (enforced via assert()):
16975** SQLITE_FullFSync == PAGER_FULLFSYNC
16976** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16977** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16978*/
16979#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_SCHEMA */
16980#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
16981#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
16982#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
16983#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
16984#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
16985#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
16986#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
16987 ** vtabs in the schema definition */
16988#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
16989 /* result set is empty */
16990#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
16991#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
16992#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
16993#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
16994#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
16995#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
16996#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
16997#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
16998#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
16999#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
17000#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
17001#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
17002#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
17003#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
17004#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
17005#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
17006#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
17007#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
17008#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
17009#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
17010#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17011#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17012#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
17013#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
17014 /* DELETE, or UPDATE and return */
17015 /* the count using a callback. */
17016
17017/* Flags used only if debugging */
17018#ifdef SQLITE_DEBUG
17019#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
17020#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
17021#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
17022#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
17023#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
17024#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
17025#endif
17026
17027/*
17028** Allowed values for sqlite3.mDbFlags
17029*/
17030#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
17031#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17032#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
17033#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
17034#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
17035#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
17036#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
17037
17038/*
17039** Bits of the sqlite3.dbOptFlags field that are used by the
17040** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
17041** selectively disable various optimizations.
17042*/
17043#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
17044#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
17045#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
17046#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
17047#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
17048#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
17049#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
17050#define SQLITE_Transitive 0x0080 /* Transitive constraints */
17051#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
17052#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
17053#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
17054#define SQLITE_Stat4 0x0800 /* Use STAT4 data */
17055 /* TH3 expects the Stat4 ^^^^^^ value to be 0x0800. Don't change it */
17056#define SQLITE_PushDown 0x1000 /* The push-down optimization */
17057#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
17058#define SQLITE_SkipScan 0x4000 /* Skip-scans */
17059#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
17060#define SQLITE_AllOpts 0xffff /* All optimizations */
17061
17062/*
17063** Macros for testing whether or not optimizations are enabled or disabled.
17064*/
17065#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17066#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17067
17068/*
17069** Return true if it OK to factor constant expressions into the initialization
17070** code. The argument is a Parse object for the code generator.
17071*/
17072#define ConstFactorOk(P) ((P)->okConstFactor)
17073
17074/*
17075** Possible values for the sqlite.magic field.
17076** The numbers are obtained at random and have no special meaning, other
17077** than being distinct from one another.
17078*/
17079#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
17080#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
17081#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
17082#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
17083#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
17084#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
17085
17086/*
17087** Each SQL function is defined by an instance of the following
17088** structure. For global built-in functions (ex: substr(), max(), count())
17089** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
17090** For per-connection application-defined functions, a pointer to this
17091** structure is held in the db->aHash hash table.
17092**
17093** The u.pHash field is used by the global built-ins. The u.pDestructor
17094** field is used by per-connection app-def functions.
17095*/
17096struct FuncDef {
17097 i8 nArg; /* Number of arguments. -1 means unlimited */
17098 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
17099 void *pUserData; /* User data parameter */
17100 FuncDef *pNext; /* Next function with same name */
17101 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17102 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
17103 void (*xValue)(sqlite3_context*); /* Current agg value */
17104 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17105 const char *zName; /* SQL name of the function. */
17106 union {
17107 FuncDef *pHash; /* Next with a different name but the same hash */
17108 FuncDestructor *pDestructor; /* Reference counted destructor function */
17109 } u;
17110};
17111
17112/*
17113** This structure encapsulates a user-function destructor callback (as
17114** configured using create_function_v2()) and a reference counter. When
17115** create_function_v2() is called to create a function with a destructor,
17116** a single object of this type is allocated. FuncDestructor.nRef is set to
17117** the number of FuncDef objects created (either 1 or 3, depending on whether
17118** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
17119** member of each of the new FuncDef objects is set to point to the allocated
17120** FuncDestructor.
17121**
17122** Thereafter, when one of the FuncDef objects is deleted, the reference
17123** count on this object is decremented. When it reaches 0, the destructor
17124** is invoked and the FuncDestructor structure freed.
17125*/
17126struct FuncDestructor {
17127 int nRef;
17128 void (*xDestroy)(void *);
17129 void *pUserData;
17130};
17131
17132/*
17133** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
17134** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
17135** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
17136** are assert() statements in the code to verify this.
17137**
17138** Value constraints (enforced via assert()):
17139** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
17140** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
17141** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
17142** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
17143** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
17144** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS
17145** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
17146*/
17147#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
17148#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
17149#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17150#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
17151#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
17152#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17153#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17154#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17155/* 0x0200 -- available for reuse */
17156#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17157#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
17158#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
17159#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17160 ** single query - might change over time */
17161#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17162#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
17163#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17164#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17165#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17166#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17167#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17168#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17169
17170/* Identifier numbers for each in-line function */
17171#define INLINEFUNC_coalesce 0
17172#define INLINEFUNC_implies_nonnull_row 1
17173#define INLINEFUNC_expr_implies_expr 2
17174#define INLINEFUNC_expr_compare 3
17175#define INLINEFUNC_affinity 4
17176#define INLINEFUNC_iif 5
17177#define INLINEFUNC_unlikely 99 /* Default case */
17178
17179/*
17180** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
17181** used to create the initializers for the FuncDef structures.
17182**
17183** FUNCTION(zName, nArg, iArg, bNC, xFunc)
17184** Used to create a scalar function definition of a function zName
17185** implemented by C function xFunc that accepts nArg arguments. The
17186** value passed as iArg is cast to a (void*) and made available
17187** as the user-data (sqlite3_user_data()) for the function. If
17188** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
17189**
17190** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
17191** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
17192**
17193** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
17194** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17195** adds the SQLITE_DIRECTONLY flag.
17196**
17197** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
17198** zName is the name of a function that is implemented by in-line
17199** byte code rather than by the usual callbacks. The iFuncId
17200** parameter determines the function id. The mFlags parameter is
17201** optional SQLITE_FUNC_ flags for this function.
17202**
17203** TEST_FUNC(zName, nArg, iFuncId, mFlags)
17204** zName is the name of a test-only function implemented by in-line
17205** byte code rather than by the usual callbacks. The iFuncId
17206** parameter determines the function id. The mFlags parameter is
17207** optional SQLITE_FUNC_ flags for this function.
17208**
17209** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
17210** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
17211** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
17212** and functions like sqlite_version() that can change, but not during
17213** a single query. The iArg is ignored. The user-data is always set
17214** to a NULL pointer. The bNC parameter is not used.
17215**
17216** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
17217** Used for "pure" date/time functions, this macro is like DFUNCTION
17218** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
17219** ignored and the user-data for these functions is set to an
17220** arbitrary non-NULL pointer. The bNC parameter is not used.
17221**
17222** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
17223** Used to create an aggregate function definition implemented by
17224** the C functions xStep and xFinal. The first four parameters
17225** are interpreted in the same way as the first 4 parameters to
17226** FUNCTION().
17227**
17228** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
17229** Used to create an aggregate function definition implemented by
17230** the C functions xStep and xFinal. The first four parameters
17231** are interpreted in the same way as the first 4 parameters to
17232** FUNCTION().
17233**
17234** LIKEFUNC(zName, nArg, pArg, flags)
17235** Used to create a scalar function definition of a function zName
17236** that accepts nArg arguments and is implemented by a call to C
17237** function likeFunc. Argument pArg is cast to a (void *) and made
17238** available as the function user-data (sqlite3_user_data()). The
17239** FuncDef.flags variable is set to the value passed as the flags
17240** parameter.
17241*/
17242#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
17243 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17244 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17245#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17246 {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17247 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17248#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17249 {nArg, SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
17250 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17251#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
17252 {nArg, SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17253 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17254#define TEST_FUNC(zName, nArg, iArg, mFlags) \
17255 {nArg, SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
17256 SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
17257 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
17258#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
17259 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
17260 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17261#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
17262 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17263 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
17264#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
17265 {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
17266 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
17267#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
17268 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
17269 pArg, 0, xFunc, 0, 0, 0, #zName, }
17270#define LIKEFUNC(zName, nArg, arg, flags) \
17271 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
17272 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
17273#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
17274 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
17275 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
17276#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
17277 {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
17278 0, 0, xFunc, 0, 0, 0, #zName, {0} }
17279
17280
17281/*
17282** All current savepoints are stored in a linked list starting at
17283** sqlite3.pSavepoint. The first element in the list is the most recently
17284** opened savepoint. Savepoints are added to the list by the vdbe
17285** OP_Savepoint instruction.
17286*/
17287struct Savepoint {
17288 char *zName; /* Savepoint name (nul-terminated) */
17289 i64 nDeferredCons; /* Number of deferred fk violations */
17290 i64 nDeferredImmCons; /* Number of deferred imm fk. */
17291 Savepoint *pNext; /* Parent savepoint (if any) */
17292};
17293
17294/*
17295** The following are used as the second parameter to sqlite3Savepoint(),
17296** and as the P1 argument to the OP_Savepoint instruction.
17297*/
17298#define SAVEPOINT_BEGIN 0
17299#define SAVEPOINT_RELEASE 1
17300#define SAVEPOINT_ROLLBACK 2
17301
17302
17303/*
17304** Each SQLite module (virtual table definition) is defined by an
17305** instance of the following structure, stored in the sqlite3.aModule
17306** hash table.
17307*/
17308struct Module {
17309 const sqlite3_module *pModule; /* Callback pointers */
17310 const char *zName; /* Name passed to create_module() */
17311 int nRefModule; /* Number of pointers to this object */
17312 void *pAux; /* pAux passed to create_module() */
17313 void (*xDestroy)(void *); /* Module destructor function */
17314 Table *pEpoTab; /* Eponymous table for this module */
17315};
17316
17317/*
17318** Information about each column of an SQL table is held in an instance
17319** of the Column structure, in the Table.aCol[] array.
17320**
17321** Definitions:
17322**
17323** "table column index" This is the index of the column in the
17324** Table.aCol[] array, and also the index of
17325** the column in the original CREATE TABLE stmt.
17326**
17327** "storage column index" This is the index of the column in the
17328** record BLOB generated by the OP_MakeRecord
17329** opcode. The storage column index is less than
17330** or equal to the table column index. It is
17331** equal if and only if there are no VIRTUAL
17332** columns to the left.
17333*/
17334struct Column {
17335 char *zName; /* Name of this column, \000, then the type */
17336 Expr *pDflt; /* Default value or GENERATED ALWAYS AS value */
17337 char *zColl; /* Collating sequence. If NULL, use the default */
17338 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
17339 char affinity; /* One of the SQLITE_AFF_... values */
17340 u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
17341 u8 hName; /* Column name hash for faster lookup */
17342 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
17343};
17344
17345/* Allowed values for Column.colFlags:
17346*/
17347#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
17348#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
17349#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
17350#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
17351#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17352#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
17353#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
17354#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
17355#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
17356#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
17357#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
17358
17359/*
17360** A "Collating Sequence" is defined by an instance of the following
17361** structure. Conceptually, a collating sequence consists of a name and
17362** a comparison routine that defines the order of that sequence.
17363**
17364** If CollSeq.xCmp is NULL, it means that the
17365** collating sequence is undefined. Indices built on an undefined
17366** collating sequence may not be read or written.
17367*/
17368struct CollSeq {
17369 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17370 u8 enc; /* Text encoding handled by xCmp() */
17371 void *pUser; /* First argument to xCmp() */
17372 int (*xCmp)(void*,int, const void*, int, const void*);
17373 void (*xDel)(void*); /* Destructor for pUser */
17374};
17375
17376/*
17377** A sort order can be either ASC or DESC.
17378*/
17379#define SQLITE_SO_ASC 0 /* Sort in ascending order */
17380#define SQLITE_SO_DESC 1 /* Sort in ascending order */
17381#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17382
17383/*
17384** Column affinity types.
17385**
17386** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
17387** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
17388** the speed a little by numbering the values consecutively.
17389**
17390** But rather than start with 0 or 1, we begin with 'A'. That way,
17391** when multiple affinity types are concatenated into a string and
17392** used as the P4 operand, they will be more readable.
17393**
17394** Note also that the numeric types are grouped together so that testing
17395** for a numeric type is a single comparison. And the BLOB type is first.
17396*/
17397#define SQLITE_AFF_NONE 0x40 /* '@' */
17398#define SQLITE_AFF_BLOB 0x41 /* 'A' */
17399#define SQLITE_AFF_TEXT 0x42 /* 'B' */
17400#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
17401#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
17402#define SQLITE_AFF_REAL 0x45 /* 'E' */
17403
17404#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
17405
17406/*
17407** The SQLITE_AFF_MASK values masks off the significant bits of an
17408** affinity value.
17409*/
17410#define SQLITE_AFF_MASK 0x47
17411
17412/*
17413** Additional bit values that can be ORed with an affinity without
17414** changing the affinity.
17415**
17416** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
17417** It causes an assert() to fire if either operand to a comparison
17418** operator is NULL. It is added to certain comparison operators to
17419** prove that the operands are always NOT NULL.
17420*/
17421#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */
17422#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
17423#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */
17424#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
17425#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
17426
17427/*
17428** An object of this type is created for each virtual table present in
17429** the database schema.
17430**
17431** If the database schema is shared, then there is one instance of this
17432** structure for each database connection (sqlite3*) that uses the shared
17433** schema. This is because each database connection requires its own unique
17434** instance of the sqlite3_vtab* handle used to access the virtual table
17435** implementation. sqlite3_vtab* handles can not be shared between
17436** database connections, even when the rest of the in-memory database
17437** schema is shared, as the implementation often stores the database
17438** connection handle passed to it via the xConnect() or xCreate() method
17439** during initialization internally. This database connection handle may
17440** then be used by the virtual table implementation to access real tables
17441** within the database. So that they appear as part of the callers
17442** transaction, these accesses need to be made via the same database
17443** connection as that used to execute SQL operations on the virtual table.
17444**
17445** All VTable objects that correspond to a single table in a shared
17446** database schema are initially stored in a linked-list pointed to by
17447** the Table.pVTable member variable of the corresponding Table object.
17448** When an sqlite3_prepare() operation is required to access the virtual
17449** table, it searches the list for the VTable that corresponds to the
17450** database connection doing the preparing so as to use the correct
17451** sqlite3_vtab* handle in the compiled query.
17452**
17453** When an in-memory Table object is deleted (for example when the
17454** schema is being reloaded for some reason), the VTable objects are not
17455** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
17456** immediately. Instead, they are moved from the Table.pVTable list to
17457** another linked list headed by the sqlite3.pDisconnect member of the
17458** corresponding sqlite3 structure. They are then deleted/xDisconnected
17459** next time a statement is prepared using said sqlite3*. This is done
17460** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
17461** Refer to comments above function sqlite3VtabUnlockList() for an
17462** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
17463** list without holding the corresponding sqlite3.mutex mutex.
17464**
17465** The memory for objects of this type is always allocated by
17466** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
17467** the first argument.
17468*/
17469struct VTable {
17470 sqlite3 *db; /* Database connection associated with this table */
17471 Module *pMod; /* Pointer to module implementation */
17472 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
17473 int nRef; /* Number of pointers to this structure */
17474 u8 bConstraint; /* True if constraints are supported */
17475 u8 eVtabRisk; /* Riskiness of allowing hacker access */
17476 int iSavepoint; /* Depth of the SAVEPOINT stack */
17477 VTable *pNext; /* Next in linked list (see above) */
17478};
17479
17480/* Allowed values for VTable.eVtabRisk
17481*/
17482#define SQLITE_VTABRISK_Low 0
17483#define SQLITE_VTABRISK_Normal 1
17484#define SQLITE_VTABRISK_High 2
17485
17486/*
17487** The schema for each SQL table and view is represented in memory
17488** by an instance of the following structure.
17489*/
17490struct Table {
17491 char *zName; /* Name of the table or view */
17492 Column *aCol; /* Information about each column */
17493 Index *pIndex; /* List of SQL indexes on this table. */
17494 Select *pSelect; /* NULL for tables. Points to definition if a view. */
17495 FKey *pFKey; /* Linked list of all foreign keys in this table */
17496 char *zColAff; /* String defining the affinity of each column */
17497 ExprList *pCheck; /* All CHECK constraints */
17498 /* ... also used as column name list in a VIEW */
17499 Pgno tnum; /* Root BTree page for this table */
17500 u32 nTabRef; /* Number of pointers to this Table */
17501 u32 tabFlags; /* Mask of TF_* values */
17502 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
17503 i16 nCol; /* Number of columns in this table */
17504 i16 nNVCol; /* Number of columns that are not VIRTUAL */
17505 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17506 LogEst szTabRow; /* Estimated size of each table row in bytes */
17507#ifdef SQLITE_ENABLE_COSTMULT
17508 LogEst costMult; /* Cost multiplier for using this table */
17509#endif
17510 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
17511#ifndef SQLITE_OMIT_ALTERTABLE
17512 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
17513#endif
17514#ifndef SQLITE_OMIT_VIRTUALTABLE
17515 int nModuleArg; /* Number of arguments to the module */
17516 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
17517 VTable *pVTable; /* List of VTable objects. */
17518#endif
17519 Trigger *pTrigger; /* List of triggers stored in pSchema */
17520 Schema *pSchema; /* Schema that contains this table */
17521 Table *pNextZombie; /* Next on the Parse.pZombieTab list */
17522};
17523
17524/*
17525** Allowed values for Table.tabFlags.
17526**
17527** TF_OOOHidden applies to tables or view that have hidden columns that are
17528** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17529** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17530** the TF_OOOHidden attribute would apply in this case. Such tables require
17531** special handling during INSERT processing. The "OOO" means "Out Of Order".
17532**
17533** Constraints:
17534**
17535** TF_HasVirtual == COLFLAG_Virtual
17536** TF_HasStored == COLFLAG_Stored
17537*/
17538#define TF_Readonly 0x0001 /* Read-only system table */
17539#define TF_Ephemeral 0x0002 /* An ephemeral table */
17540#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
17541#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */
17542#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */
17543#define TF_HasVirtual 0x0020 /* Has one or more VIRTUAL columns */
17544#define TF_HasStored 0x0040 /* Has one or more STORED columns */
17545#define TF_HasGenerated 0x0060 /* Combo: HasVirtual + HasStored */
17546#define TF_WithoutRowid 0x0080 /* No rowid. PRIMARY KEY is the key */
17547#define TF_StatsUsed 0x0100 /* Query planner decisions affected by
17548 ** Index.aiRowLogEst[] values */
17549#define TF_NoVisibleRowid 0x0200 /* No user-visible "rowid" column */
17550#define TF_OOOHidden 0x0400 /* Out-of-Order hidden columns */
17551#define TF_HasNotNull 0x0800 /* Contains NOT NULL constraints */
17552#define TF_Shadow 0x1000 /* True for a shadow table */
17553
17554/*
17555** Test to see whether or not a table is a virtual table. This is
17556** done as a macro so that it will be optimized out when virtual
17557** table support is omitted from the build.
17558*/
17559#ifndef SQLITE_OMIT_VIRTUALTABLE
17560# define IsVirtual(X) ((X)->nModuleArg)
17561# define ExprIsVtab(X) \
17562 ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
17563#else
17564# define IsVirtual(X) 0
17565# define ExprIsVtab(X) 0
17566#endif
17567
17568/*
17569** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
17570** only works for non-virtual tables (ordinary tables and views) and is
17571** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
17572** IsHiddenColumn() macro is general purpose.
17573*/
17574#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
17575# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17576# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17577#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
17578# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17579# define IsOrdinaryHiddenColumn(X) 0
17580#else
17581# define IsHiddenColumn(X) 0
17582# define IsOrdinaryHiddenColumn(X) 0
17583#endif
17584
17585
17586/* Does the table have a rowid */
17587#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17588#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17589
17590/*
17591** Each foreign key constraint is an instance of the following structure.
17592**
17593** A foreign key is associated with two tables. The "from" table is
17594** the table that contains the REFERENCES clause that creates the foreign
17595** key. The "to" table is the table that is named in the REFERENCES clause.
17596** Consider this example:
17597**
17598** CREATE TABLE ex1(
17599** a INTEGER PRIMARY KEY,
17600** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
17601** );
17602**
17603** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17604** Equivalent names:
17605**
17606** from-table == child-table
17607** to-table == parent-table
17608**
17609** Each REFERENCES clause generates an instance of the following structure
17610** which is attached to the from-table. The to-table need not exist when
17611** the from-table is created. The existence of the to-table is not checked.
17612**
17613** The list of all parents for child Table X is held at X.pFKey.
17614**
17615** A list of all children for a table named Z (which might not even exist)
17616** is held in Schema.fkeyHash with a hash key of Z.
17617*/
17618struct FKey {
17619 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
17620 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
17621 char *zTo; /* Name of table that the key points to (aka: Parent) */
17622 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
17623 FKey *pPrevTo; /* Previous with the same zTo */
17624 int nCol; /* Number of columns in this key */
17625 /* EV: R-30323-21917 */
17626 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
17627 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
17628 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
17629 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
17630 int iFrom; /* Index of column in pFrom */
17631 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
17632 } aCol[1]; /* One entry for each of nCol columns */
17633};
17634
17635/*
17636** SQLite supports many different ways to resolve a constraint
17637** error. ROLLBACK processing means that a constraint violation
17638** causes the operation in process to fail and for the current transaction
17639** to be rolled back. ABORT processing means the operation in process
17640** fails and any prior changes from that one operation are backed out,
17641** but the transaction is not rolled back. FAIL processing means that
17642** the operation in progress stops and returns an error code. But prior
17643** changes due to the same operation are not backed out and no rollback
17644** occurs. IGNORE means that the particular row that caused the constraint
17645** error is not inserted or updated. Processing continues and no error
17646** is returned. REPLACE means that preexisting database rows that caused
17647** a UNIQUE constraint violation are removed so that the new insert or
17648** update can proceed. Processing continues and no error is reported.
17649**
17650** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
17651** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
17652** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
17653** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
17654** referenced table row is propagated into the row that holds the
17655** foreign key.
17656**
17657** The following symbolic values are used to record which type
17658** of action to take.
17659*/
17660#define OE_None 0 /* There is no constraint to check */
17661#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
17662#define OE_Abort 2 /* Back out changes but do no rollback transaction */
17663#define OE_Fail 3 /* Stop the operation but leave all prior changes */
17664#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
17665#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
17666#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
17667#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
17668#define OE_SetNull 8 /* Set the foreign key value to NULL */
17669#define OE_SetDflt 9 /* Set the foreign key value to its default */
17670#define OE_Cascade 10 /* Cascade the changes */
17671#define OE_Default 11 /* Do whatever the default action is */
17672
17673
17674/*
17675** An instance of the following structure is passed as the first
17676** argument to sqlite3VdbeKeyCompare and is used to control the
17677** comparison of the two index keys.
17678**
17679** Note that aSortOrder[] and aColl[] have nField+1 slots. There
17680** are nField slots for the columns of an index then one extra slot
17681** for the rowid at the end.
17682*/
17683struct KeyInfo {
17684 u32 nRef; /* Number of references to this KeyInfo object */
17685 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17686 u16 nKeyField; /* Number of key columns in the index */
17687 u16 nAllField; /* Total columns, including key plus others */
17688 sqlite3 *db; /* The database connection */
17689 u8 *aSortFlags; /* Sort order for each column. */
17690 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
17691};
17692
17693/*
17694** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
17695*/
17696#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
17697#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
17698
17699/*
17700** This object holds a record which has been parsed out into individual
17701** fields, for the purposes of doing a comparison.
17702**
17703** A record is an object that contains one or more fields of data.
17704** Records are used to store the content of a table row and to store
17705** the key of an index. A blob encoding of a record is created by
17706** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17707** OP_Column opcode.
17708**
17709** An instance of this object serves as a "key" for doing a search on
17710** an index b+tree. The goal of the search is to find the entry that
17711** is closed to the key described by this object. This object might hold
17712** just a prefix of the key. The number of fields is given by
17713** pKeyInfo->nField.
17714**
17715** The r1 and r2 fields are the values to return if this key is less than
17716** or greater than a key in the btree, respectively. These are normally
17717** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17718** is in DESC order.
17719**
17720** The key comparison functions actually return default_rc when they find
17721** an equals comparison. default_rc can be -1, 0, or +1. If there are
17722** multiple entries in the b-tree with the same key (when only looking
17723** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17724** cause the search to find the last match, or +1 to cause the search to
17725** find the first match.
17726**
17727** The key comparison functions will set eqSeen to true if they ever
17728** get and equal results when comparing this structure to a b-tree record.
17729** When default_rc!=0, the search might end up on the record immediately
17730** before the first match or immediately after the last match. The
17731** eqSeen field will indicate whether or not an exact match exists in the
17732** b-tree.
17733*/
17734struct UnpackedRecord {
17735 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17736 Mem *aMem; /* Values */
17737 u16 nField; /* Number of entries in apMem[] */
17738 i8 default_rc; /* Comparison result if keys are equal */
17739 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17740 i8 r1; /* Value to return if (lhs < rhs) */
17741 i8 r2; /* Value to return if (lhs > rhs) */
17742 u8 eqSeen; /* True if an equality comparison has been seen */
17743};
17744
17745
17746/*
17747** Each SQL index is represented in memory by an
17748** instance of the following structure.
17749**
17750** The columns of the table that are to be indexed are described
17751** by the aiColumn[] field of this structure. For example, suppose
17752** we have the following table and index:
17753**
17754** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17755** CREATE INDEX Ex2 ON Ex1(c3,c1);
17756**
17757** In the Table structure describing Ex1, nCol==3 because there are
17758** three columns in the table. In the Index structure describing
17759** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17760** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
17761** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17762** The second column to be indexed (c1) has an index of 0 in
17763** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17764**
17765** The Index.onError field determines whether or not the indexed columns
17766** must be unique and what to do if they are not. When Index.onError=OE_None,
17767** it means this is not a unique index. Otherwise it is a unique index
17768** and the value of Index.onError indicate the which conflict resolution
17769** algorithm to employ whenever an attempt is made to insert a non-unique
17770** element.
17771**
17772** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17773** generate VDBE code (as opposed to parsing one read from an sqlite_schema
17774** table as part of parsing an existing database schema), transient instances
17775** of this structure may be created. In this case the Index.tnum variable is
17776** used to store the address of a VDBE instruction, not a database page
17777** number (it cannot - the database page is not allocated until the VDBE
17778** program is executed). See convertToWithoutRowidTable() for details.
17779*/
17780struct Index {
17781 char *zName; /* Name of this index */
17782 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
17783 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
17784 Table *pTable; /* The SQL table being indexed */
17785 char *zColAff; /* String defining the affinity of each column */
17786 Index *pNext; /* The next index associated with the same table */
17787 Schema *pSchema; /* Schema containing this index */
17788 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
17789 const char **azColl; /* Array of collation sequence names for index */
17790 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
17791 ExprList *aColExpr; /* Column expressions */
17792 Pgno tnum; /* DB Page containing root of this index */
17793 LogEst szIdxRow; /* Estimated average row size in bytes */
17794 u16 nKeyCol; /* Number of columns forming the key */
17795 u16 nColumn; /* Number of columns stored in the index */
17796 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17797 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
17798 unsigned bUnordered:1; /* Use this index for == or IN queries only */
17799 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
17800 unsigned isResized:1; /* True if resizeIndexObject() has been called */
17801 unsigned isCovering:1; /* True if this is a covering index */
17802 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17803 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17804 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17805 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
17806 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
17807#ifdef SQLITE_ENABLE_STAT4
17808 int nSample; /* Number of elements in aSample[] */
17809 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17810 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17811 IndexSample *aSample; /* Samples of the left-most key */
17812 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17813 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17814#endif
17815 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17816};
17817
17818/*
17819** Allowed values for Index.idxType
17820*/
17821#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
17822#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
17823#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
17824#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
17825
17826/* Return true if index X is a PRIMARY KEY index */
17827#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
17828
17829/* Return true if index X is a UNIQUE index */
17830#define IsUniqueIndex(X) ((X)->onError!=OE_None)
17831
17832/* The Index.aiColumn[] values are normally positive integer. But
17833** there are some negative values that have special meaning:
17834*/
17835#define XN_ROWID (-1) /* Indexed column is the rowid */
17836#define XN_EXPR (-2) /* Indexed column is an expression */
17837
17838/*
17839** Each sample stored in the sqlite_stat4 table is represented in memory
17840** using a structure of this type. See documentation at the top of the
17841** analyze.c source file for additional information.
17842*/
17843struct IndexSample {
17844 void *p; /* Pointer to sampled record */
17845 int n; /* Size of record in bytes */
17846 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17847 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17848 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17849};
17850
17851/*
17852** Possible values to use within the flags argument to sqlite3GetToken().
17853*/
17854#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
17855#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
17856
17857/*
17858** Each token coming out of the lexer is an instance of
17859** this structure. Tokens are also used as part of an expression.
17860**
17861** The memory that "z" points to is owned by other objects. Take care
17862** that the owner of the "z" string does not deallocate the string before
17863** the Token goes out of scope! Very often, the "z" points to some place
17864** in the middle of the Parse.zSql text. But it might also point to a
17865** static string.
17866*/
17867struct Token {
17868 const char *z; /* Text of the token. Not NULL-terminated! */
17869 unsigned int n; /* Number of characters in this token */
17870};
17871
17872/*
17873** An instance of this structure contains information needed to generate
17874** code for a SELECT that contains aggregate functions.
17875**
17876** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17877** pointer to this structure. The Expr.iAgg field is the index in
17878** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17879** code for that node.
17880**
17881** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17882** original Select structure that describes the SELECT statement. These
17883** fields do not need to be freed when deallocating the AggInfo structure.
17884*/
17885struct AggInfo {
17886 u8 directMode; /* Direct rendering mode means take data directly
17887 ** from source tables rather than from accumulators */
17888 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
17889 ** than the source table */
17890 int sortingIdx; /* Cursor number of the sorting index */
17891 int sortingIdxPTab; /* Cursor number of pseudo-table */
17892 int nSortingColumn; /* Number of columns in the sorting index */
17893 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
17894 ExprList *pGroupBy; /* The group by clause */
17895 struct AggInfo_col { /* For each column used in source tables */
17896 Table *pTab; /* Source table */
17897 Expr *pCExpr; /* The original expression */
17898 int iTable; /* Cursor number of the source table */
17899 int iMem; /* Memory location that acts as accumulator */
17900 i16 iColumn; /* Column number within the source table */
17901 i16 iSorterColumn; /* Column number in the sorting index */
17902 } *aCol;
17903 int nColumn; /* Number of used entries in aCol[] */
17904 int nAccumulator; /* Number of columns that show through to the output.
17905 ** Additional columns are used only as parameters to
17906 ** aggregate functions */
17907 struct AggInfo_func { /* For each aggregate function */
17908 Expr *pFExpr; /* Expression encoding the function */
17909 FuncDef *pFunc; /* The aggregate function implementation */
17910 int iMem; /* Memory location that acts as accumulator */
17911 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
17912 } *aFunc;
17913 int nFunc; /* Number of entries in aFunc[] */
17914 u32 selId; /* Select to which this AggInfo belongs */
17915 AggInfo *pNext; /* Next in list of them all */
17916};
17917
17918/*
17919** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17920** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
17921** than 32767 we have to make it 32-bit. 16-bit is preferred because
17922** it uses less memory in the Expr object, which is a big memory user
17923** in systems with lots of prepared statements. And few applications
17924** need more than about 10 or 20 variables. But some extreme users want
17925** to have prepared statements with over 32766 variables, and for them
17926** the option is available (at compile-time).
17927*/
17928#if SQLITE_MAX_VARIABLE_NUMBER<32767
17929typedef i16 ynVar;
17930#else
17931typedef int ynVar;
17932#endif
17933
17934/*
17935** Each node of an expression in the parse tree is an instance
17936** of this structure.
17937**
17938** Expr.op is the opcode. The integer parser token codes are reused
17939** as opcodes here. For example, the parser defines TK_GE to be an integer
17940** code representing the ">=" operator. This same integer code is reused
17941** to represent the greater-than-or-equal-to operator in the expression
17942** tree.
17943**
17944** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17945** or TK_STRING), then Expr.token contains the text of the SQL literal. If
17946** the expression is a variable (TK_VARIABLE), then Expr.token contains the
17947** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17948** then Expr.token contains the name of the function.
17949**
17950** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17951** binary operator. Either or both may be NULL.
17952**
17953** Expr.x.pList is a list of arguments if the expression is an SQL function,
17954** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17955** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17956** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17957** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17958** valid.
17959**
17960** An expression of the form ID or ID.ID refers to a column in a table.
17961** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17962** the integer cursor number of a VDBE cursor pointing to that table and
17963** Expr.iColumn is the column number for the specific column. If the
17964** expression is used as a result in an aggregate SELECT, then the
17965** value is also stored in the Expr.iAgg column in the aggregate so that
17966** it can be accessed after all aggregates are computed.
17967**
17968** If the expression is an unbound variable marker (a question mark
17969** character '?' in the original SQL) then the Expr.iTable holds the index
17970** number for that variable.
17971**
17972** If the expression is a subquery then Expr.iColumn holds an integer
17973** register number containing the result of the subquery. If the
17974** subquery gives a constant result, then iTable is -1. If the subquery
17975** gives a different answer at different times during statement processing
17976** then iTable is the address of a subroutine that computes the subquery.
17977**
17978** If the Expr is of type OP_Column, and the table it is selecting from
17979** is a disk table or the "old.*" pseudo-table, then pTab points to the
17980** corresponding table definition.
17981**
17982** ALLOCATION NOTES:
17983**
17984** Expr objects can use a lot of memory space in database schema. To
17985** help reduce memory requirements, sometimes an Expr object will be
17986** truncated. And to reduce the number of memory allocations, sometimes
17987** two or more Expr objects will be stored in a single memory allocation,
17988** together with Expr.zToken strings.
17989**
17990** If the EP_Reduced and EP_TokenOnly flags are set when
17991** an Expr object is truncated. When EP_Reduced is set, then all
17992** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17993** are contained within the same memory allocation. Note, however, that
17994** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17995** allocated, regardless of whether or not EP_Reduced is set.
17996*/
17997struct Expr {
17998 u8 op; /* Operation performed by this node */
17999 char affExpr; /* affinity, or RAISE type */
18000 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
18001 ** TK_COLUMN: the value of p5 for OP_Column
18002 ** TK_AGG_FUNCTION: nesting depth
18003 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
18004#ifdef SQLITE_DEBUG
18005 u8 vvaFlags; /* Verification flags. */
18006#endif
18007 u32 flags; /* Various flags. EP_* See below */
18008 union {
18009 char *zToken; /* Token value. Zero terminated and dequoted */
18010 int iValue; /* Non-negative integer value if EP_IntValue */
18011 } u;
18012
18013 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
18014 ** space is allocated for the fields below this point. An attempt to
18015 ** access them will result in a segfault or malfunction.
18016 *********************************************************************/
18017
18018 Expr *pLeft; /* Left subnode */
18019 Expr *pRight; /* Right subnode */
18020 union {
18021 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
18022 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
18023 } x;
18024
18025 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
18026 ** space is allocated for the fields below this point. An attempt to
18027 ** access them will result in a segfault or malfunction.
18028 *********************************************************************/
18029
18030#if SQLITE_MAX_EXPR_DEPTH>0
18031 int nHeight; /* Height of the tree headed by this node */
18032#endif
18033 int iTable; /* TK_COLUMN: cursor number of table holding column
18034 ** TK_REGISTER: register number
18035 ** TK_TRIGGER: 1 -> new, 0 -> old
18036 ** EP_Unlikely: 134217728 times likelihood
18037 ** TK_IN: ephemerial table holding RHS
18038 ** TK_SELECT_COLUMN: Number of columns on the LHS
18039 ** TK_SELECT: 1st register of result vector */
18040 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18041 ** TK_VARIABLE: variable number (always >= 1).
18042 ** TK_SELECT_COLUMN: column of the result vector */
18043 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18044 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
18045 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
18046 union {
18047 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
18048 ** for a column of an index on an expression */
18049 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
18050 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
18051 int iAddr; /* Subroutine entry address */
18052 int regReturn; /* Register used to hold return address */
18053 } sub;
18054 } y;
18055};
18056
18057/*
18058** The following are the meanings of bits in the Expr.flags field.
18059** Value restrictions:
18060**
18061** EP_Agg == NC_HasAgg == SF_HasAgg
18062** EP_Win == NC_HasWin
18063*/
18064#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
18065#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */
18066#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
18067#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
18068#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
18069#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
18070#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
18071#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
18072#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
18073#define EP_Commuted 0x000200 /* Comparison operator has been commuted */
18074#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
18075#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
18076#define EP_Skip 0x001000 /* Operator does not contribute to affinity */
18077#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
18078#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18079#define EP_Win 0x008000 /* Contains window functions */
18080#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
18081 /* 0x020000 // available for reuse */
18082#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
18083#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
18084#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
18085#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
18086#define EP_Alias 0x400000 /* Is an alias for a result set column */
18087#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
18088#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
18089#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
18090#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
18091#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
18092#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
18093#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
18094#define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */
18095 /* 0x80000000 // Available */
18096
18097/*
18098** The EP_Propagate mask is a set of properties that automatically propagate
18099** upwards into parent nodes.
18100*/
18101#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
18102
18103/*
18104** These macros can be used to test, set, or clear bits in the
18105** Expr.flags field.
18106*/
18107#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18108#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18109#define ExprSetProperty(E,P) (E)->flags|=(P)
18110#define ExprClearProperty(E,P) (E)->flags&=~(P)
18111#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
18112#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
18113
18114
18115/* Flags for use with Expr.vvaFlags
18116*/
18117#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
18118#define EP_Immutable 0x02 /* Do not change this Expr node */
18119
18120/* The ExprSetVVAProperty() macro is used for Verification, Validation,
18121** and Accreditation only. It works like ExprSetProperty() during VVA
18122** processes but is a no-op for delivery.
18123*/
18124#ifdef SQLITE_DEBUG
18125# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18126# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18127# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18128#else
18129# define ExprSetVVAProperty(E,P)
18130# define ExprHasVVAProperty(E,P) 0
18131# define ExprClearVVAProperties(E)
18132#endif
18133
18134/*
18135** Macros to determine the number of bytes required by a normal Expr
18136** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
18137** and an Expr struct with the EP_TokenOnly flag set.
18138*/
18139#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
18140#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
18141#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
18142
18143/*
18144** Flags passed to the sqlite3ExprDup() function. See the header comment
18145** above sqlite3ExprDup() for details.
18146*/
18147#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18148
18149/*
18150** True if the expression passed as an argument was a function with
18151** an OVER() clause (a window function).
18152*/
18153#ifdef SQLITE_OMIT_WINDOWFUNC
18154# define IsWindowFunc(p) 0
18155#else
18156# define IsWindowFunc(p) ( \
18157 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18158 )
18159#endif
18160
18161/*
18162** A list of expressions. Each expression may optionally have a
18163** name. An expr/name combination can be used in several ways, such
18164** as the list of "expr AS ID" fields following a "SELECT" or in the
18165** list of "ID = expr" items in an UPDATE. A list of expressions can
18166** also be used as the argument to a function, in which case the a.zName
18167** field is not used.
18168**
18169** In order to try to keep memory usage down, the Expr.a.zEName field
18170** is used for multiple purposes:
18171**
18172** eEName Usage
18173** ---------- -------------------------
18174** ENAME_NAME (1) the AS of result set column
18175** (2) COLUMN= of an UPDATE
18176**
18177** ENAME_TAB DB.TABLE.NAME used to resolve names
18178** of subqueries
18179**
18180** ENAME_SPAN Text of the original result set
18181** expression.
18182*/
18183struct ExprList {
18184 int nExpr; /* Number of expressions on the list */
18185 struct ExprList_item { /* For each expression in the list */
18186 Expr *pExpr; /* The parse tree for this expression */
18187 char *zEName; /* Token associated with this expression */
18188 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
18189 unsigned eEName :2; /* Meaning of zEName */
18190 unsigned done :1; /* A flag to indicate when processing is finished */
18191 unsigned reusable :1; /* Constant expression is reusable */
18192 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
18193 unsigned bNulls: 1; /* True if explicit "NULLS FIRST/LAST" */
18194 union {
18195 struct {
18196 u16 iOrderByCol; /* For ORDER BY, column number in result set */
18197 u16 iAlias; /* Index into Parse.aAlias[] for zName */
18198 } x;
18199 int iConstExprReg; /* Register in which Expr value is cached */
18200 } u;
18201 } a[1]; /* One slot for each expression in the list */
18202};
18203
18204/*
18205** Allowed values for Expr.a.eEName
18206*/
18207#define ENAME_NAME 0 /* The AS clause of a result set */
18208#define ENAME_SPAN 1 /* Complete text of the result set expression */
18209#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
18210
18211/*
18212** An instance of this structure can hold a simple list of identifiers,
18213** such as the list "a,b,c" in the following statements:
18214**
18215** INSERT INTO t(a,b,c) VALUES ...;
18216** CREATE INDEX idx ON t(a,b,c);
18217** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
18218**
18219** The IdList.a.idx field is used when the IdList represents the list of
18220** column names after a table name in an INSERT statement. In the statement
18221**
18222** INSERT INTO t(a,b,c) ...
18223**
18224** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18225*/
18226struct IdList {
18227 struct IdList_item {
18228 char *zName; /* Name of the identifier */
18229 int idx; /* Index in some Table.aCol[] of a column named zName */
18230 } *a;
18231 int nId; /* Number of identifiers on the list */
18232};
18233
18234/*
18235** The following structure describes the FROM clause of a SELECT statement.
18236** Each table or subquery in the FROM clause is a separate element of
18237** the SrcList.a[] array.
18238**
18239** With the addition of multiple database support, the following structure
18240** can also be used to describe a particular table such as the table that
18241** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
18242** such a table must be a simple name: ID. But in SQLite, the table can
18243** now be identified by a database name, a dot, then the table name: ID.ID.
18244**
18245** The jointype starts out showing the join type between the current table
18246** and the next table on the list. The parser builds the list this way.
18247** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18248** jointype expresses the join between the table and the previous table.
18249**
18250** In the colUsed field, the high-order bit (bit 63) is set if the table
18251** contains more than 63 columns and the 64-th or later column is used.
18252*/
18253struct SrcList {
18254 int nSrc; /* Number of tables or subqueries in the FROM clause */
18255 u32 nAlloc; /* Number of entries allocated in a[] below */
18256 struct SrcList_item {
18257 Schema *pSchema; /* Schema to which this item is fixed */
18258 char *zDatabase; /* Name of database holding this table */
18259 char *zName; /* Name of the table */
18260 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
18261 Table *pTab; /* An SQL table corresponding to zName */
18262 Select *pSelect; /* A SELECT statement used in place of a table name */
18263 int addrFillSub; /* Address of subroutine to manifest a subquery */
18264 int regReturn; /* Register holding return address of addrFillSub */
18265 int regResult; /* Registers holding results of a co-routine */
18266 struct {
18267 u8 jointype; /* Type of join between this table and the previous */
18268 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
18269 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
18270 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18271 unsigned isCorrelated :1; /* True if sub-query is correlated */
18272 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18273 unsigned isRecursive :1; /* True for recursive reference in WITH */
18274 unsigned fromDDL :1; /* Comes from sqlite_schema */
18275 } fg;
18276 int iCursor; /* The VDBE cursor number used to access this table */
18277 Expr *pOn; /* The ON clause of a join */
18278 IdList *pUsing; /* The USING clause of a join */
18279 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
18280 union {
18281 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18282 ExprList *pFuncArg; /* Arguments to table-valued-function */
18283 } u1;
18284 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18285 } a[1]; /* One entry for each identifier on the list */
18286};
18287
18288/*
18289** Permitted values of the SrcList.a.jointype field
18290*/
18291#define JT_INNER 0x0001 /* Any kind of inner or cross join */
18292#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
18293#define JT_NATURAL 0x0004 /* True for a "natural" join */
18294#define JT_LEFT 0x0008 /* Left outer join */
18295#define JT_RIGHT 0x0010 /* Right outer join */
18296#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
18297#define JT_ERROR 0x0040 /* unknown or unsupported join type */
18298
18299
18300/*
18301** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
18302** and the WhereInfo.wctrlFlags member.
18303**
18304** Value constraints (enforced via assert()):
18305** WHERE_USE_LIMIT == SF_FixedLimit
18306*/
18307#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18308#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
18309#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
18310#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18311#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
18312#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
18313#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18314 ** the OR optimization */
18315#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
18316#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
18317#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
18318#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
18319#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
18320#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
18321#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
18322 /* 0x2000 not currently used */
18323#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
18324 /* 0x8000 not currently used */
18325
18326/* Allowed return values from sqlite3WhereIsDistinct()
18327*/
18328#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
18329#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
18330#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
18331#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
18332
18333/*
18334** A NameContext defines a context in which to resolve table and column
18335** names. The context consists of a list of tables (the pSrcList) field and
18336** a list of named expression (pEList). The named expression list may
18337** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
18338** to the table being operated on by INSERT, UPDATE, or DELETE. The
18339** pEList corresponds to the result set of a SELECT and is NULL for
18340** other statements.
18341**
18342** NameContexts can be nested. When resolving names, the inner-most
18343** context is searched first. If no match is found, the next outer
18344** context is checked. If there is still no match, the next context
18345** is checked. This process continues until either a match is found
18346** or all contexts are check. When a match is found, the nRef member of
18347** the context containing the match is incremented.
18348**
18349** Each subquery gets a new NameContext. The pNext field points to the
18350** NameContext in the parent query. Thus the process of scanning the
18351** NameContext list corresponds to searching through successively outer
18352** subqueries looking for a match.
18353*/
18354struct NameContext {
18355 Parse *pParse; /* The parser */
18356 SrcList *pSrcList; /* One or more tables used to resolve names */
18357 union {
18358 ExprList *pEList; /* Optional list of result-set columns */
18359 AggInfo *pAggInfo; /* Information about aggregates at this level */
18360 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
18361 } uNC;
18362 NameContext *pNext; /* Next outer name context. NULL for outermost */
18363 int nRef; /* Number of names resolved by this context */
18364 int nErr; /* Number of errors encountered while resolving names */
18365 int ncFlags; /* Zero or more NC_* flags defined below */
18366 Select *pWinSelect; /* SELECT statement for any window functions */
18367};
18368
18369/*
18370** Allowed values for the NameContext, ncFlags field.
18371**
18372** Value constraints (all checked via assert()):
18373** NC_HasAgg == SF_HasAgg == EP_Agg
18374** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
18375** NC_HasWin == EP_Win
18376**
18377*/
18378#define NC_AllowAgg 0x00001 /* Aggregate functions are allowed here */
18379#define NC_PartIdx 0x00002 /* True if resolving a partial index WHERE */
18380#define NC_IsCheck 0x00004 /* True if resolving a CHECK constraint */
18381#define NC_GenCol 0x00008 /* True for a GENERATED ALWAYS AS clause */
18382#define NC_HasAgg 0x00010 /* One or more aggregate functions seen */
18383#define NC_IdxExpr 0x00020 /* True if resolving columns of CREATE INDEX */
18384#define NC_SelfRef 0x0002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
18385#define NC_VarSelect 0x00040 /* A correlated subquery has been seen */
18386#define NC_UEList 0x00080 /* True if uNC.pEList is used */
18387#define NC_UAggInfo 0x00100 /* True if uNC.pAggInfo is used */
18388#define NC_UUpsert 0x00200 /* True if uNC.pUpsert is used */
18389#define NC_MinMaxAgg 0x01000 /* min/max aggregates seen. See note above */
18390#define NC_Complex 0x02000 /* True if a function or subquery seen */
18391#define NC_AllowWin 0x04000 /* Window functions are allowed here */
18392#define NC_HasWin 0x08000 /* One or more window functions seen */
18393#define NC_IsDDL 0x10000 /* Resolving names in a CREATE statement */
18394#define NC_InAggFunc 0x20000 /* True if analyzing arguments to an agg func */
18395#define NC_FromDDL 0x40000 /* SQL text comes from sqlite_schema */
18396
18397/*
18398** An instance of the following object describes a single ON CONFLICT
18399** clause in an upsert.
18400**
18401** The pUpsertTarget field is only set if the ON CONFLICT clause includes
18402** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18403** conflict-target clause.) The pUpsertTargetWhere is the optional
18404** WHERE clause used to identify partial unique indexes.
18405**
18406** pUpsertSet is the list of column=expr terms of the UPDATE statement.
18407** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
18408** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
18409** WHERE clause is omitted.
18410*/
18411struct Upsert {
18412 ExprList *pUpsertTarget; /* Optional description of conflicting index */
18413 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
18414 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
18415 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
18416 /* The fields above comprise the parse tree for the upsert clause.
18417 ** The fields below are used to transfer information from the INSERT
18418 ** processing down into the UPDATE processing while generating code.
18419 ** Upsert owns the memory allocated above, but not the memory below. */
18420 Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
18421 SrcList *pUpsertSrc; /* Table to be updated */
18422 int regData; /* First register holding array of VALUES */
18423 int iDataCur; /* Index of the data cursor */
18424 int iIdxCur; /* Index of the first index cursor */
18425};
18426
18427/*
18428** An instance of the following structure contains all information
18429** needed to generate code for a single SELECT statement.
18430**
18431** See the header comment on the computeLimitRegisters() routine for a
18432** detailed description of the meaning of the iLimit and iOffset fields.
18433**
18434** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
18435** These addresses must be stored so that we can go back and fill in
18436** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
18437** the number of columns in P2 can be computed at the same time
18438** as the OP_OpenEphm instruction is coded because not
18439** enough information about the compound query is known at that point.
18440** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
18441** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
18442** sequences for the ORDER BY clause.
18443*/
18444struct Select {
18445 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
18446 LogEst nSelectRow; /* Estimated number of result rows */
18447 u32 selFlags; /* Various SF_* values */
18448 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
18449 u32 selId; /* Unique identifier number for this SELECT */
18450 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
18451 ExprList *pEList; /* The fields of the result */
18452 SrcList *pSrc; /* The FROM clause */
18453 Expr *pWhere; /* The WHERE clause */
18454 ExprList *pGroupBy; /* The GROUP BY clause */
18455 Expr *pHaving; /* The HAVING clause */
18456 ExprList *pOrderBy; /* The ORDER BY clause */
18457 Select *pPrior; /* Prior select in a compound select statement */
18458 Select *pNext; /* Next select to the left in a compound */
18459 Expr *pLimit; /* LIMIT expression. NULL means not used. */
18460 With *pWith; /* WITH clause attached to this select. Or NULL. */
18461#ifndef SQLITE_OMIT_WINDOWFUNC
18462 Window *pWin; /* List of window functions */
18463 Window *pWinDefn; /* List of named window definitions */
18464#endif
18465};
18466
18467/*
18468** Allowed values for Select.selFlags. The "SF" prefix stands for
18469** "Select Flag".
18470**
18471** Value constraints (all checked via assert())
18472** SF_HasAgg == NC_HasAgg
18473** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
18474** SF_FixedLimit == WHERE_USE_LIMIT
18475*/
18476#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
18477#define SF_All 0x0000002 /* Includes the ALL keyword */
18478#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
18479#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
18480#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
18481#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
18482#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
18483#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
18484#define SF_Compound 0x0000100 /* Part of a compound query */
18485#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
18486#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
18487#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
18488#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
18489#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
18490#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
18491#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
18492#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
18493#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
18494#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
18495#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
18496#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
18497#define SF_View 0x0200000 /* SELECT statement is a view */
18498#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
18499#define SF_UpdateFrom 0x0800000 /* Statement is an UPDATE...FROM */
18500
18501/*
18502** The results of a SELECT can be distributed in several ways, as defined
18503** by one of the following macros. The "SRT" prefix means "SELECT Result
18504** Type".
18505**
18506** SRT_Union Store results as a key in a temporary index
18507** identified by pDest->iSDParm.
18508**
18509** SRT_Except Remove results from the temporary index pDest->iSDParm.
18510**
18511** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18512** set is not empty.
18513**
18514** SRT_Discard Throw the results away. This is used by SELECT
18515** statements within triggers whose only purpose is
18516** the side-effects of functions.
18517**
18518** All of the above are free to ignore their ORDER BY clause. Those that
18519** follow must honor the ORDER BY clause.
18520**
18521** SRT_Output Generate a row of output (using the OP_ResultRow
18522** opcode) for each row in the result set.
18523**
18524** SRT_Mem Only valid if the result is a single column.
18525** Store the first column of the first result row
18526** in register pDest->iSDParm then abandon the rest
18527** of the query. This destination implies "LIMIT 1".
18528**
18529** SRT_Set The result must be a single column. Store each
18530** row of result as the key in table pDest->iSDParm.
18531** Apply the affinity pDest->affSdst before storing
18532** results. Used to implement "IN (SELECT ...)".
18533**
18534** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18535** the result there. The cursor is left open after
18536** returning. This is like SRT_Table except that
18537** this destination uses OP_OpenEphemeral to create
18538** the table first.
18539**
18540** SRT_Coroutine Generate a co-routine that returns a new row of
18541** results each time it is invoked. The entry point
18542** of the co-routine is stored in register pDest->iSDParm
18543** and the result row is stored in pDest->nDest registers
18544** starting with pDest->iSdst.
18545**
18546** SRT_Table Store results in temporary table pDest->iSDParm.
18547** SRT_Fifo This is like SRT_EphemTab except that the table
18548** is assumed to already be open. SRT_Fifo has
18549** the additional property of being able to ignore
18550** the ORDER BY clause.
18551**
18552** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18553** But also use temporary table pDest->iSDParm+1 as
18554** a record of all prior results and ignore any duplicate
18555** rows. Name means: "Distinct Fifo".
18556**
18557** SRT_Queue Store results in priority queue pDest->iSDParm (really
18558** an index). Append a sequence number so that all entries
18559** are distinct.
18560**
18561** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18562** the same record has never been stored before. The
18563** index at pDest->iSDParm+1 hold all prior stores.
18564**
18565** SRT_Upfrom Store results in the temporary table already opened by
18566** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18567** table is an intkey table - in this case the first
18568** column returned by the SELECT is used as the integer
18569** key. If (pDest->iSDParm>0), then the table is an index
18570** table. (pDest->iSDParm) is the number of key columns in
18571** each index record in this case.
18572*/
18573#define SRT_Union 1 /* Store result as keys in an index */
18574#define SRT_Except 2 /* Remove result from a UNION index */
18575#define SRT_Exists 3 /* Store 1 if the result is not empty */
18576#define SRT_Discard 4 /* Do not save the results anywhere */
18577#define SRT_Fifo 5 /* Store result as data with an automatic rowid */
18578#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */
18579#define SRT_Queue 7 /* Store result in an queue */
18580#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */
18581
18582/* The ORDER BY clause is ignored for all of the above */
18583#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
18584
18585#define SRT_Output 9 /* Output each row of result */
18586#define SRT_Mem 10 /* Store result in a memory cell */
18587#define SRT_Set 11 /* Store results as keys in an index */
18588#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
18589#define SRT_Coroutine 13 /* Generate a single row of result */
18590#define SRT_Table 14 /* Store result as data with an automatic rowid */
18591#define SRT_Upfrom 15 /* Store result as data with rowid */
18592
18593/*
18594** An instance of this object describes where to put of the results of
18595** a SELECT statement.
18596*/
18597struct SelectDest {
18598 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
18599 int iSDParm; /* A parameter used by the eDest disposal method */
18600 int iSDParm2; /* A second parameter for the eDest disposal method */
18601 int iSdst; /* Base register where results are written */
18602 int nSdst; /* Number of registers allocated */
18603 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
18604 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
18605};
18606
18607/*
18608** During code generation of statements that do inserts into AUTOINCREMENT
18609** tables, the following information is attached to the Table.u.autoInc.p
18610** pointer of each autoincrement table to record some side information that
18611** the code generator needs. We have to keep per-table autoincrement
18612** information in case inserts are done within triggers. Triggers do not
18613** normally coordinate their activities, but we do need to coordinate the
18614** loading and saving of autoincrement information.
18615*/
18616struct AutoincInfo {
18617 AutoincInfo *pNext; /* Next info block in a list of them all */
18618 Table *pTab; /* Table this info block refers to */
18619 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
18620 int regCtr; /* Memory register holding the rowid counter */
18621};
18622
18623/*
18624** At least one instance of the following structure is created for each
18625** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
18626** statement. All such objects are stored in the linked list headed at
18627** Parse.pTriggerPrg and deleted once statement compilation has been
18628** completed.
18629**
18630** A Vdbe sub-program that implements the body and WHEN clause of trigger
18631** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
18632** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
18633** The Parse.pTriggerPrg list never contains two entries with the same
18634** values for both pTrigger and orconf.
18635**
18636** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
18637** accessed (or set to 0 for triggers fired as a result of INSERT
18638** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
18639** a mask of new.* columns used by the program.
18640*/
18641struct TriggerPrg {
18642 Trigger *pTrigger; /* Trigger this program was coded from */
18643 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
18644 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
18645 int orconf; /* Default ON CONFLICT policy */
18646 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
18647};
18648
18649/*
18650** The yDbMask datatype for the bitmask of all attached databases.
18651*/
18652#if SQLITE_MAX_ATTACHED>30
18653 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
18654# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
18655# define DbMaskZero(M) memset((M),0,sizeof(M))
18656# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
18657# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
18658# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
18659#else
18660 typedef unsigned int yDbMask;
18661# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
18662# define DbMaskZero(M) (M)=0
18663# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18664# define DbMaskAllZero(M) (M)==0
18665# define DbMaskNonZero(M) (M)!=0
18666#endif
18667
18668/*
18669** An SQL parser context. A copy of this structure is passed through
18670** the parser and down into all the parser action routine in order to
18671** carry around information that is global to the entire parse.
18672**
18673** The structure is divided into two parts. When the parser and code
18674** generate call themselves recursively, the first part of the structure
18675** is constant but the second part is reset at the beginning and end of
18676** each recursion.
18677**
18678** The nTableLock and aTableLock variables are only used if the shared-cache
18679** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18680** used to store the set of table-locks required by the statement being
18681** compiled. Function sqlite3TableLock() is used to add entries to the
18682** list.
18683*/
18684struct Parse {
18685 sqlite3 *db; /* The main database structure */
18686 char *zErrMsg; /* An error message */
18687 Vdbe *pVdbe; /* An engine for executing database bytecode */
18688 int rc; /* Return code from execution */
18689 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
18690 u8 checkSchema; /* Causes schema cookie check after an error */
18691 u8 nested; /* Number of nested calls to the parser/code generator */
18692 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
18693 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
18694 u8 mayAbort; /* True if statement may throw an ABORT exception */
18695 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18696 u8 okConstFactor; /* OK to factor out constants */
18697 u8 disableLookaside; /* Number of times lookaside has been disabled */
18698 u8 disableVtab; /* Disable all virtual tables for this parse */
18699 int nRangeReg; /* Size of the temporary register block */
18700 int iRangeReg; /* First register in temporary register block */
18701 int nErr; /* Number of errors seen */
18702 int nTab; /* Number of previously allocated VDBE cursors */
18703 int nMem; /* Number of memory cells used so far */
18704 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
18705 int iSelfTab; /* Table associated with an index on expr, or negative
18706 ** of the base register during check-constraint eval */
18707 int nLabel; /* The *negative* of the number of labels used */
18708 int nLabelAlloc; /* Number of slots in aLabel */
18709 int *aLabel; /* Space to hold the labels */
18710 ExprList *pConstExpr;/* Constant expressions */
18711 Token constraintName;/* Name of the constraint currently being parsed */
18712 yDbMask writeMask; /* Start a write transaction on these databases */
18713 yDbMask cookieMask; /* Bitmask of schema verified databases */
18714 int regRowid; /* Register holding rowid of CREATE TABLE entry */
18715 int regRoot; /* Register holding root page number for new objects */
18716 int nMaxArg; /* Max args passed to user function by sub-program */
18717 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
18718#ifndef SQLITE_OMIT_SHARED_CACHE
18719 int nTableLock; /* Number of locks in aTableLock */
18720 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18721#endif
18722 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18723 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18724 Table *pTriggerTab; /* Table triggers are being coded for */
18725 Parse *pParentParse; /* Parent parser if this parser is nested */
18726 AggInfo *pAggList; /* List of all AggInfo objects */
18727 int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
18728 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
18729 u32 oldmask; /* Mask of old.* columns referenced */
18730 u32 newmask; /* Mask of new.* columns referenced */
18731 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
18732 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
18733 u8 disableTriggers; /* True to disable triggers */
18734
18735 /**************************************************************************
18736 ** Fields above must be initialized to zero. The fields that follow,
18737 ** down to the beginning of the recursive section, do not need to be
18738 ** initialized as they will be set before being used. The boundary is
18739 ** determined by offsetof(Parse,aTempReg).
18740 **************************************************************************/
18741
18742 int aTempReg[8]; /* Holding area for temporary registers */
18743 Token sNameToken; /* Token with unqualified schema object name */
18744
18745 /************************************************************************
18746 ** Above is constant between recursions. Below is reset before and after
18747 ** each recursion. The boundary between these two regions is determined
18748 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
18749 ** first field in the recursive region.
18750 ************************************************************************/
18751
18752 Token sLastToken; /* The last token parsed */
18753 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
18754 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
18755 u8 explain; /* True if the EXPLAIN flag is found on the query */
18756 u8 eParseMode; /* PARSE_MODE_XXX constant */
18757#ifndef SQLITE_OMIT_VIRTUALTABLE
18758 int nVtabLock; /* Number of virtual tables to lock */
18759#endif
18760 int nHeight; /* Expression tree height of current sub-select */
18761#ifndef SQLITE_OMIT_EXPLAIN
18762 int addrExplain; /* Address of current OP_Explain opcode */
18763#endif
18764 VList *pVList; /* Mapping between variable names and numbers */
18765 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
18766 const char *zTail; /* All SQL text past the last semicolon parsed */
18767 Table *pNewTable; /* A table being constructed by CREATE TABLE */
18768 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
18769 ** Also used to hold redundant UNIQUE constraints
18770 ** during a RENAME COLUMN */
18771 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
18772 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18773#ifndef SQLITE_OMIT_VIRTUALTABLE
18774 Token sArg; /* Complete text of a module argument */
18775 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18776#endif
18777 Table *pZombieTab; /* List of Table objects to delete after code gen */
18778 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18779 With *pWith; /* Current WITH clause, or NULL */
18780 With *pWithToFree; /* Free this WITH object at the end of the parse */
18781#ifndef SQLITE_OMIT_ALTERTABLE
18782 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18783#endif
18784};
18785
18786#define PARSE_MODE_NORMAL 0
18787#define PARSE_MODE_DECLARE_VTAB 1
18788#define PARSE_MODE_RENAME 2
18789#define PARSE_MODE_UNMAP 3
18790
18791/*
18792** Sizes and pointers of various parts of the Parse object.
18793*/
18794#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
18795#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
18796#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
18797#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
18798
18799/*
18800** Return true if currently inside an sqlite3_declare_vtab() call.
18801*/
18802#ifdef SQLITE_OMIT_VIRTUALTABLE
18803 #define IN_DECLARE_VTAB 0
18804#else
18805 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
18806#endif
18807
18808#if defined(SQLITE_OMIT_ALTERTABLE)
18809 #define IN_RENAME_OBJECT 0
18810#else
18811 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
18812#endif
18813
18814#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
18815 #define IN_SPECIAL_PARSE 0
18816#else
18817 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
18818#endif
18819
18820/*
18821** An instance of the following structure can be declared on a stack and used
18822** to save the Parse.zAuthContext value so that it can be restored later.
18823*/
18824struct AuthContext {
18825 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
18826 Parse *pParse; /* The Parse structure */
18827};
18828
18829/*
18830** Bitfield flags for P5 value in various opcodes.
18831**
18832** Value constraints (enforced via assert()):
18833** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
18834** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
18835** OPFLAG_BULKCSR == BTREE_BULKLOAD
18836** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
18837** OPFLAG_FORDELETE == BTREE_FORDELETE
18838** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
18839** OPFLAG_AUXDELETE == BTREE_AUXDELETE
18840*/
18841#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
18842 /* Also used in P2 (not P5) of OP_Delete */
18843#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
18844#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
18845#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
18846#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
18847#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
18848#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
18849#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
18850#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
18851#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
18852#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
18853#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
18854#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
18855#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
18856#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
18857#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
18858#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
18859#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
18860
18861/*
18862 * Each trigger present in the database schema is stored as an instance of
18863 * struct Trigger.
18864 *
18865 * Pointers to instances of struct Trigger are stored in two ways.
18866 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
18867 * database). This allows Trigger structures to be retrieved by name.
18868 * 2. All triggers associated with a single table form a linked list, using the
18869 * pNext member of struct Trigger. A pointer to the first element of the
18870 * linked list is stored as the "pTrigger" member of the associated
18871 * struct Table.
18872 *
18873 * The "step_list" member points to the first element of a linked list
18874 * containing the SQL statements specified as the trigger program.
18875 */
18876struct Trigger {
18877 char *zName; /* The name of the trigger */
18878 char *table; /* The table or view to which the trigger applies */
18879 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
18880 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
18881 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
18882 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
18883 the <column-list> is stored here */
18884 Schema *pSchema; /* Schema containing the trigger */
18885 Schema *pTabSchema; /* Schema containing the table */
18886 TriggerStep *step_list; /* Link list of trigger program steps */
18887 Trigger *pNext; /* Next trigger associated with the table */
18888};
18889
18890/*
18891** A trigger is either a BEFORE or an AFTER trigger. The following constants
18892** determine which.
18893**
18894** If there are multiple triggers, you might of some BEFORE and some AFTER.
18895** In that cases, the constants below can be ORed together.
18896*/
18897#define TRIGGER_BEFORE 1
18898#define TRIGGER_AFTER 2
18899
18900/*
18901 * An instance of struct TriggerStep is used to store a single SQL statement
18902 * that is a part of a trigger-program.
18903 *
18904 * Instances of struct TriggerStep are stored in a singly linked list (linked
18905 * using the "pNext" member) referenced by the "step_list" member of the
18906 * associated struct Trigger instance. The first element of the linked list is
18907 * the first step of the trigger-program.
18908 *
18909 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
18910 * "SELECT" statement. The meanings of the other members is determined by the
18911 * value of "op" as follows:
18912 *
18913 * (op == TK_INSERT)
18914 * orconf -> stores the ON CONFLICT algorithm
18915 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
18916 * this stores a pointer to the SELECT statement. Otherwise NULL.
18917 * zTarget -> Dequoted name of the table to insert into.
18918 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
18919 * this stores values to be inserted. Otherwise NULL.
18920 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
18921 * statement, then this stores the column-names to be
18922 * inserted into.
18923 *
18924 * (op == TK_DELETE)
18925 * zTarget -> Dequoted name of the table to delete from.
18926 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
18927 * Otherwise NULL.
18928 *
18929 * (op == TK_UPDATE)
18930 * zTarget -> Dequoted name of the table to update.
18931 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
18932 * Otherwise NULL.
18933 * pExprList -> A list of the columns to update and the expressions to update
18934 * them to. See sqlite3Update() documentation of "pChanges"
18935 * argument.
18936 *
18937 */
18938struct TriggerStep {
18939 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
18940 u8 orconf; /* OE_Rollback etc. */
18941 Trigger *pTrig; /* The trigger that this step is a part of */
18942 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
18943 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
18944 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
18945 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
18946 ExprList *pExprList; /* SET clause for UPDATE */
18947 IdList *pIdList; /* Column names for INSERT */
18948 Upsert *pUpsert; /* Upsert clauses on an INSERT */
18949 char *zSpan; /* Original SQL text of this command */
18950 TriggerStep *pNext; /* Next in the link-list */
18951 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
18952};
18953
18954/*
18955** The following structure contains information used by the sqliteFix...
18956** routines as they walk the parse tree to make database references
18957** explicit.
18958*/
18959typedef struct DbFixer DbFixer;
18960struct DbFixer {
18961 Parse *pParse; /* The parsing context. Error messages written here */
18962 Schema *pSchema; /* Fix items to this schema */
18963 u8 bTemp; /* True for TEMP schema entries */
18964 const char *zDb; /* Make sure all objects are contained in this database */
18965 const char *zType; /* Type of the container - used for error messages */
18966 const Token *pName; /* Name of the container - used for error messages */
18967};
18968
18969/*
18970** An objected used to accumulate the text of a string where we
18971** do not necessarily know how big the string will be in the end.
18972*/
18973struct sqlite3_str {
18974 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
18975 char *zText; /* The string collected so far */
18976 u32 nAlloc; /* Amount of space allocated in zText */
18977 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
18978 u32 nChar; /* Length of the string so far */
18979 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
18980 u8 printfFlags; /* SQLITE_PRINTF flags below */
18981};
18982#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
18983#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
18984#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
18985
18986#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
18987
18988
18989/*
18990** A pointer to this structure is used to communicate information
18991** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18992*/
18993typedef struct {
18994 sqlite3 *db; /* The database being initialized */
18995 char **pzErrMsg; /* Error message stored here */
18996 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
18997 int rc; /* Result code stored here */
18998 u32 mInitFlags; /* Flags controlling error messages */
18999 u32 nInitRow; /* Number of rows processed */
19000 Pgno mxPage; /* Maximum page number. 0 for no limit. */
19001} InitData;
19002
19003/*
19004** Allowed values for mInitFlags
19005*/
19006#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */
19007
19008/*
19009** Structure containing global configuration data for the SQLite library.
19010**
19011** This structure also contains some state information.
19012*/
19013struct Sqlite3Config {
19014 int bMemstat; /* True to enable memory status */
19015 u8 bCoreMutex; /* True to enable core mutexing */
19016 u8 bFullMutex; /* True to enable full mutexing */
19017 u8 bOpenUri; /* True to interpret filenames as URIs */
19018 u8 bUseCis; /* Use covering indices for full-scans */
19019 u8 bSmallMalloc; /* Avoid large memory allocations if true */
19020 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
19021 int mxStrlen; /* Maximum string length */
19022 int neverCorrupt; /* Database is always well-formed */
19023 int szLookaside; /* Default lookaside buffer size */
19024 int nLookaside; /* Default lookaside buffer count */
19025 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19026 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19027 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19028 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19029 void *pHeap; /* Heap storage space */
19030 int nHeap; /* Size of pHeap[] */
19031 int mnReq, mxReq; /* Min and max heap requests sizes */
19032 sqlite3_int64 szMmap; /* mmap() space per open file */
19033 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
19034 void *pPage; /* Page cache memory */
19035 int szPage; /* Size of each page in pPage[] */
19036 int nPage; /* Number of pages in pPage[] */
19037 int mxParserStack; /* maximum depth of the parser stack */
19038 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19039 u32 szPma; /* Maximum Sorter PMA size */
19040 /* The above might be initialized to non-zero. The following need to always
19041 ** initially be zero, however. */
19042 int isInit; /* True after initialization has finished */
19043 int inProgress; /* True while initialization in progress */
19044 int isMutexInit; /* True after mutexes are initialized */
19045 int isMallocInit; /* True after malloc is initialized */
19046 int isPCacheInit; /* True after malloc is initialized */
19047 int nRefInitMutex; /* Number of users of pInitMutex */
19048 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
19049 void (*xLog)(void*,int,const char*); /* Function for logging */
19050 void *pLogArg; /* First argument to xLog() */
19051#ifdef SQLITE_ENABLE_SQLLOG
19052 void(*xSqllog)(void*,sqlite3*,const char*, int);
19053 void *pSqllogArg;
19054#endif
19055#ifdef SQLITE_VDBE_COVERAGE
19056 /* The following callback (if not NULL) is invoked on every VDBE branch
19057 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
19058 */
19059 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
19060 void *pVdbeBranchArg; /* 1st argument */
19061#endif
19062#ifdef SQLITE_ENABLE_DESERIALIZE
19063 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
19064#endif
19065#ifndef SQLITE_UNTESTABLE
19066 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
19067#endif
19068 int bLocaltimeFault; /* True to fail localtime() calls */
19069 int iOnceResetThreshold; /* When to reset OP_Once counters */
19070 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19071 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
19072};
19073
19074/*
19075** This macro is used inside of assert() statements to indicate that
19076** the assert is only valid on a well-formed database. Instead of:
19077**
19078** assert( X );
19079**
19080** One writes:
19081**
19082** assert( X || CORRUPT_DB );
19083**
19084** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
19085** that the database is definitely corrupt, only that it might be corrupt.
19086** For most test cases, CORRUPT_DB is set to false using a special
19087** sqlite3_test_control(). This enables assert() statements to prove
19088** things that are always true for well-formed databases.
19089*/
19090#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
19091
19092/*
19093** Context pointer passed down through the tree-walk.
19094*/
19095struct Walker {
19096 Parse *pParse; /* Parser context. */
19097 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
19098 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
19099 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
19100 int walkerDepth; /* Number of subqueries */
19101 u16 eCode; /* A small processing code */
19102 union { /* Extra data for callback */
19103 NameContext *pNC; /* Naming context */
19104 int n; /* A counter */
19105 int iCur; /* A cursor number */
19106 SrcList *pSrcList; /* FROM clause */
19107 struct SrcCount *pSrcCount; /* Counting column references */
19108 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
19109 int *aiCol; /* array of column indexes */
19110 struct IdxCover *pIdxCover; /* Check for index coverage */
19111 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
19112 ExprList *pGroupBy; /* GROUP BY clause */
19113 Select *pSelect; /* HAVING to WHERE clause ctx */
19114 struct WindowRewrite *pRewrite; /* Window rewrite context */
19115 struct WhereConst *pConst; /* WHERE clause constants */
19116 struct RenameCtx *pRename; /* RENAME COLUMN context */
19117 struct Table *pTab; /* Table of generated column */
19118 struct SrcList_item *pSrcItem; /* A single FROM clause item */
19119 } u;
19120};
19121
19122/* Forward declarations */
19123SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
19124SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
19125SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
19126SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
19127SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
19128SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
19129SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
19130SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
19131SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*);
19132SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*);
19133
19134#ifdef SQLITE_DEBUG
19135SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
19136#endif
19137
19138/*
19139** Return code from the parse-tree walking primitives and their
19140** callbacks.
19141*/
19142#define WRC_Continue 0 /* Continue down into children */
19143#define WRC_Prune 1 /* Omit children but continue walking siblings */
19144#define WRC_Abort 2 /* Abandon the tree walk */
19145
19146/*
19147** An instance of this structure represents a set of one or more CTEs
19148** (common table expressions) created by a single WITH clause.
19149*/
19150struct With {
19151 int nCte; /* Number of CTEs in the WITH clause */
19152 With *pOuter; /* Containing WITH clause, or NULL */
19153 struct Cte { /* For each CTE in the WITH clause.... */
19154 char *zName; /* Name of this CTE */
19155 ExprList *pCols; /* List of explicit column names, or NULL */
19156 Select *pSelect; /* The definition of this CTE */
19157 const char *zCteErr; /* Error message for circular references */
19158 } a[1];
19159};
19160
19161#ifdef SQLITE_DEBUG
19162/*
19163** An instance of the TreeView object is used for printing the content of
19164** data structures on sqlite3DebugPrintf() using a tree-like view.
19165*/
19166struct TreeView {
19167 int iLevel; /* Which level of the tree we are on */
19168 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
19169};
19170#endif /* SQLITE_DEBUG */
19171
19172/*
19173** This object is used in various ways, most (but not all) related to window
19174** functions.
19175**
19176** (1) A single instance of this structure is attached to the
19177** the Expr.y.pWin field for each window function in an expression tree.
19178** This object holds the information contained in the OVER clause,
19179** plus additional fields used during code generation.
19180**
19181** (2) All window functions in a single SELECT form a linked-list
19182** attached to Select.pWin. The Window.pFunc and Window.pExpr
19183** fields point back to the expression that is the window function.
19184**
19185** (3) The terms of the WINDOW clause of a SELECT are instances of this
19186** object on a linked list attached to Select.pWinDefn.
19187**
19188** (4) For an aggregate function with a FILTER clause, an instance
19189** of this object is stored in Expr.y.pWin with eFrmType set to
19190** TK_FILTER. In this case the only field used is Window.pFilter.
19191**
19192** The uses (1) and (2) are really the same Window object that just happens
19193** to be accessible in two different ways. Use case (3) are separate objects.
19194*/
19195struct Window {
19196 char *zName; /* Name of window (may be NULL) */
19197 char *zBase; /* Name of base window for chaining (may be NULL) */
19198 ExprList *pPartition; /* PARTITION BY clause */
19199 ExprList *pOrderBy; /* ORDER BY clause */
19200 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
19201 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19202 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
19203 u8 bImplicitFrame; /* True if frame was implicitly specified */
19204 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
19205 Expr *pStart; /* Expression for "<expr> PRECEDING" */
19206 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
19207 Window **ppThis; /* Pointer to this object in Select.pWin list */
19208 Window *pNextWin; /* Next window function belonging to this SELECT */
19209 Expr *pFilter; /* The FILTER expression */
19210 FuncDef *pFunc; /* The function */
19211 int iEphCsr; /* Partition buffer or Peer buffer */
19212 int regAccum; /* Accumulator */
19213 int regResult; /* Interim result */
19214 int csrApp; /* Function cursor (used by min/max) */
19215 int regApp; /* Function register (also used by min/max) */
19216 int regPart; /* Array of registers for PARTITION BY values */
19217 Expr *pOwner; /* Expression object this window is attached to */
19218 int nBufferCol; /* Number of columns in buffer table */
19219 int iArgCol; /* Offset of first argument for this function */
19220 int regOne; /* Register containing constant value 1 */
19221 int regStartRowid;
19222 int regEndRowid;
19223 u8 bExprArgs; /* Defer evaluation of window function arguments
19224 ** due to the SQLITE_SUBTYPE flag */
19225};
19226
19227#ifndef SQLITE_OMIT_WINDOWFUNC
19228SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
19229SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
19230SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
19231SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
19232SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
19233SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin);
19234SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int);
19235SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*);
19236SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
19237SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
19238SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
19239SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
19240SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
19241SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
19242SQLITE_PRIVATE void sqlite3WindowFunctions(void);
19243SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
19244SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
19245#else
19246# define sqlite3WindowDelete(a,b)
19247# define sqlite3WindowFunctions()
19248# define sqlite3WindowAttach(a,b,c)
19249#endif
19250
19251/*
19252** Assuming zIn points to the first byte of a UTF-8 character,
19253** advance zIn to point to the first byte of the next UTF-8 character.
19254*/
19255#define SQLITE_SKIP_UTF8(zIn) { \
19256 if( (*(zIn++))>=0xc0 ){ \
19257 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
19258 } \
19259}
19260
19261/*
19262** The SQLITE_*_BKPT macros are substitutes for the error codes with
19263** the same name but without the _BKPT suffix. These macros invoke
19264** routines that report the line-number on which the error originated
19265** using sqlite3_log(). The routines also provide a convenient place
19266** to set a debugger breakpoint.
19267*/
19268SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
19269SQLITE_PRIVATE int sqlite3CorruptError(int);
19270SQLITE_PRIVATE int sqlite3MisuseError(int);
19271SQLITE_PRIVATE int sqlite3CantopenError(int);
19272#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
19273#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
19274#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
19275#ifdef SQLITE_DEBUG
19276SQLITE_PRIVATE int sqlite3NomemError(int);
19277SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
19278# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
19279# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
19280#else
19281# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
19282# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
19283#endif
19284#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
19285SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
19286# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
19287#else
19288# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
19289#endif
19290
19291/*
19292** FTS3 and FTS4 both require virtual table support
19293*/
19294#if defined(SQLITE_OMIT_VIRTUALTABLE)
19295# undef SQLITE_ENABLE_FTS3
19296# undef SQLITE_ENABLE_FTS4
19297#endif
19298
19299/*
19300** FTS4 is really an extension for FTS3. It is enabled using the
19301** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
19302** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
19303*/
19304#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
19305# define SQLITE_ENABLE_FTS3 1
19306#endif
19307
19308/*
19309** The ctype.h header is needed for non-ASCII systems. It is also
19310** needed by FTS3 when FTS3 is included in the amalgamation.
19311*/
19312#if !defined(SQLITE_ASCII) || \
19313 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
19314# include <ctype.h>
19315#endif
19316
19317/*
19318** The following macros mimic the standard library functions toupper(),
19319** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
19320** sqlite versions only work for ASCII characters, regardless of locale.
19321*/
19322#ifdef SQLITE_ASCII
19323# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
19324# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
19325# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
19326# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
19327# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
19328# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
19329# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
19330# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
19331#else
19332# define sqlite3Toupper(x) toupper((unsigned char)(x))
19333# define sqlite3Isspace(x) isspace((unsigned char)(x))
19334# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
19335# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
19336# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
19337# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
19338# define sqlite3Tolower(x) tolower((unsigned char)(x))
19339# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
19340#endif
19341SQLITE_PRIVATE int sqlite3IsIdChar(u8);
19342
19343/*
19344** Internal function prototypes
19345*/
19346SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
19347SQLITE_PRIVATE int sqlite3Strlen30(const char*);
19348#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
19349SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
19350#define sqlite3StrNICmp sqlite3_strnicmp
19351
19352SQLITE_PRIVATE int sqlite3MallocInit(void);
19353SQLITE_PRIVATE void sqlite3MallocEnd(void);
19354SQLITE_PRIVATE void *sqlite3Malloc(u64);
19355SQLITE_PRIVATE void *sqlite3MallocZero(u64);
19356SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
19357SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
19358SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
19359SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
19360SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
19361SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
19362SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
19363SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
19364SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
19365SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
19366SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
19367SQLITE_PRIVATE int sqlite3MallocSize(void*);
19368SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
19369SQLITE_PRIVATE void *sqlite3PageMalloc(int);
19370SQLITE_PRIVATE void sqlite3PageFree(void*);
19371SQLITE_PRIVATE void sqlite3MemSetDefault(void);
19372#ifndef SQLITE_UNTESTABLE
19373SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
19374#endif
19375SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
19376
19377/*
19378** On systems with ample stack space and that support alloca(), make
19379** use of alloca() to obtain space for large automatic objects. By default,
19380** obtain space from malloc().
19381**
19382** The alloca() routine never returns NULL. This will cause code paths
19383** that deal with sqlite3StackAlloc() failures to be unreachable.
19384*/
19385#ifdef SQLITE_USE_ALLOCA
19386# define sqlite3StackAllocRaw(D,N) alloca(N)
19387# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
19388# define sqlite3StackFree(D,P)
19389#else
19390# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
19391# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
19392# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
19393#endif
19394
19395/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
19396** are, disable MEMSYS3
19397*/
19398#ifdef SQLITE_ENABLE_MEMSYS5
19399SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
19400#undef SQLITE_ENABLE_MEMSYS3
19401#endif
19402#ifdef SQLITE_ENABLE_MEMSYS3
19403SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
19404#endif
19405
19406
19407#ifndef SQLITE_MUTEX_OMIT
19408SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
19409SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
19410SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
19411SQLITE_PRIVATE int sqlite3MutexInit(void);
19412SQLITE_PRIVATE int sqlite3MutexEnd(void);
19413#endif
19414#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
19415SQLITE_PRIVATE void sqlite3MemoryBarrier(void);
19416#else
19417# define sqlite3MemoryBarrier()
19418#endif
19419
19420SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
19421SQLITE_PRIVATE void sqlite3StatusUp(int, int);
19422SQLITE_PRIVATE void sqlite3StatusDown(int, int);
19423SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
19424SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
19425
19426/* Access to mutexes used by sqlite3_status() */
19427SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
19428SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
19429
19430#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
19431SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
19432#else
19433# define sqlite3MutexWarnOnContention(x)
19434#endif
19435
19436#ifndef SQLITE_OMIT_FLOATING_POINT
19437# define EXP754 (((u64)0x7ff)<<52)
19438# define MAN754 ((((u64)1)<<52)-1)
19439# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
19440SQLITE_PRIVATE int sqlite3IsNaN(double);
19441#else
19442# define IsNaN(X) 0
19443# define sqlite3IsNaN(X) 0
19444#endif
19445
19446/*
19447** An instance of the following structure holds information about SQL
19448** functions arguments that are the parameters to the printf() function.
19449*/
19450struct PrintfArguments {
19451 int nArg; /* Total number of arguments */
19452 int nUsed; /* Number of arguments used so far */
19453 sqlite3_value **apArg; /* The argument values */
19454};
19455
19456SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
19457SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
19458#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
19459SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
19460#endif
19461#if defined(SQLITE_TEST)
19462SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
19463#endif
19464
19465#if defined(SQLITE_DEBUG)
19466SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
19467SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
19468SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
19469SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
19470SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
19471SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8);
19472#ifndef SQLITE_OMIT_WINDOWFUNC
19473SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
19474SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
19475#endif
19476#endif
19477
19478
19479SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
19480SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
19481SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
19482SQLITE_PRIVATE void sqlite3Dequote(char*);
19483SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
19484SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
19485SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19486SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
19487SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
19488SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
19489SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
19490SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
19491SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
19492SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
19493#ifdef SQLITE_DEBUG
19494SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
19495#endif
19496SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
19497SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
19498SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
19499SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
19500SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
19501SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
19502SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*);
19503SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
19504SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,Expr*,FuncDef*);
19505SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
19506SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
19507SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
19508SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
19509SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
19510SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
19511SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
19512SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
19513SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
19514SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
19515SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
19516SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
19517SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
19518SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
19519SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
19520#ifndef SQLITE_OMIT_VIRTUALTABLE
19521SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
19522#endif
19523SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
19524SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
19525SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
19526SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
19527SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
19528SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
19529SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
19530SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
19531SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
19532SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
19533SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
19534#ifdef SQLITE_OMIT_GENERATED_COLUMNS
19535# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
19536# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
19537#else
19538SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
19539SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table*, i16);
19540#endif
19541SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
19542#if SQLITE_ENABLE_HIDDEN_COLUMNS
19543SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*);
19544#else
19545# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
19546#endif
19547SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
19548SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
19549SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
19550SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
19551SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
19552SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
19553SQLITE_PRIVATE void sqlite3AddGenerated(Parse*,Expr*,Token*);
19554SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
19555SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
19556 sqlite3_vfs**,char**,char **);
19557#define sqlite3CodecQueryParameters(A,B,C) 0
19558SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
19559
19560#ifdef SQLITE_UNTESTABLE
19561# define sqlite3FaultSim(X) SQLITE_OK
19562#else
19563SQLITE_PRIVATE int sqlite3FaultSim(int);
19564#endif
19565
19566SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
19567SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
19568SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
19569SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
19570SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
19571SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
19572SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
19573#ifndef SQLITE_UNTESTABLE
19574SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
19575#endif
19576
19577SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
19578SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
19579SQLITE_PRIVATE void sqlite3RowSetClear(void*);
19580SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
19581SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
19582SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
19583
19584SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
19585
19586#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
19587SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
19588#else
19589# define sqlite3ViewGetColumnNames(A,B) 0
19590#endif
19591
19592#if SQLITE_MAX_ATTACHED>30
19593SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
19594#endif
19595SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
19596SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
19597SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
19598SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
19599#ifndef SQLITE_OMIT_AUTOINCREMENT
19600SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
19601SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
19602#else
19603# define sqlite3AutoincrementBegin(X)
19604# define sqlite3AutoincrementEnd(X)
19605#endif
19606SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
19607#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19608SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
19609#endif
19610SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
19611SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
19612SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
19613SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
19614SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
19615SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
19616SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
19617 Token*, Select*, Expr*, IdList*);
19618SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
19619SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
19620SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
19621SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
19622SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
19623SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
19624SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
19625SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
19626SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
19627 Expr*, int, int, u8);
19628SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
19629SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
19630SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
19631 Expr*,ExprList*,u32,Expr*);
19632SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
19633SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
19634SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
19635SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
19636#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
19637SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
19638#endif
19639SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19640SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19641 Upsert*);
19642SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
19643SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19644SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19645SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19646SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19647SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
19648SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
19649SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
19650SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
19651SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
19652#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
19653#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
19654#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
19655SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo*);
19656SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
19657SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
19658SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
19659SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
19660SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
19661#ifndef SQLITE_OMIT_GENERATED_COLUMNS
19662SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
19663#endif
19664SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
19665SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
19666SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
19667SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
19668SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
19669SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
19670#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
19671#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
19672#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
19673#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
19674SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
19675SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
19676SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
19677SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
19678#define LOCATE_VIEW 0x01
19679#define LOCATE_NOERR 0x02
19680SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
19681SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
19682SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
19683SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
19684SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
19685SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*);
19686SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
19687SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
19688SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
19689SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
19690SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
19691SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
19692SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
19693SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
19694SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
19695SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
19696SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
19697SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
19698SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
19699#ifndef SQLITE_UNTESTABLE
19700SQLITE_PRIVATE void sqlite3PrngSaveState(void);
19701SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
19702#endif
19703SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
19704SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
19705SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
19706SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
19707SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
19708SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
19709SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
19710SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
19711SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char*);
19712SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
19713SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
19714SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
19715SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
19716SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
19717SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
19718SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
19719#ifdef SQLITE_ENABLE_CURSOR_HINTS
19720SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
19721#endif
19722SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
19723SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
19724SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
19725SQLITE_PRIVATE int sqlite3IsRowid(const char*);
19726SQLITE_PRIVATE void sqlite3GenerateRowDelete(
19727 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
19728SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
19729SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
19730SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
19731SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
19732SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
19733 u8,u8,int,int*,int*,Upsert*);
19734#ifdef SQLITE_ENABLE_NULL_TRIM
19735SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
19736#else
19737# define sqlite3SetMakeRecordP5(A,B)
19738#endif
19739SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
19740SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
19741SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
19742SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
19743SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
19744SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
19745SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
19746SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
19747SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
19748SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
19749SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
19750SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
19751SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
19752SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
19753SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19754SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19755SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
19756SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
19757SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19758SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
19759SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
19760SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
19761
19762#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
19763SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
19764#endif
19765
19766#ifndef SQLITE_OMIT_TRIGGER
19767SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
19768 Expr*,int, int);
19769SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
19770SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
19771SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
19772SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
19773SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
19774SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
19775 int, int, int);
19776SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
19777 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
19778SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
19779SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
19780 const char*,const char*);
19781SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
19782 Select*,u8,Upsert*,
19783 const char*,const char*);
19784SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
19785 Expr*, u8, const char*,const char*);
19786SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
19787 const char*,const char*);
19788SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
19789SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
19790SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
19791SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
19792# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
19793# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
19794#else
19795# define sqlite3TriggersExist(B,C,D,E,F) 0
19796# define sqlite3DeleteTrigger(A,B)
19797# define sqlite3DropTriggerPtr(A,B)
19798# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
19799# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
19800# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
19801# define sqlite3TriggerList(X, Y) 0
19802# define sqlite3ParseToplevel(p) p
19803# define sqlite3IsToplevel(p) 1
19804# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
19805# define sqlite3TriggerStepSrc(A,B) 0
19806#endif
19807
19808SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
19809SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr*,int);
19810SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
19811SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
19812#ifndef SQLITE_OMIT_AUTHORIZATION
19813SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
19814SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
19815SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
19816SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
19817SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
19818#else
19819# define sqlite3AuthRead(a,b,c,d)
19820# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
19821# define sqlite3AuthContextPush(a,b,c)
19822# define sqlite3AuthContextPop(a) ((void)(a))
19823#endif
19824SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
19825SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19826SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
19827SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19828SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
19829SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
19830SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
19831SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
19832SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
19833SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
19834SQLITE_PRIVATE void sqlite3Int64ToText(i64,char*);
19835SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
19836SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
19837SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*);
19838SQLITE_PRIVATE int sqlite3Atoi(const char*);
19839#ifndef SQLITE_OMIT_UTF16
19840SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
19841#endif
19842SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
19843SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
19844SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
19845SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
19846#ifndef SQLITE_OMIT_VIRTUALTABLE
19847SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
19848#endif
19849#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
19850 defined(SQLITE_ENABLE_STAT4) || \
19851 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
19852SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
19853#endif
19854SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
19855SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
19856SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
19857
19858/*
19859** Routines to read and write variable-length integers. These used to
19860** be defined locally, but now we use the varint routines in the util.c
19861** file.
19862*/
19863SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
19864SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
19865SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
19866SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
19867
19868/*
19869** The common case is for a varint to be a single byte. They following
19870** macros handle the common case without a procedure call, but then call
19871** the procedure for larger varints.
19872*/
19873#define getVarint32(A,B) \
19874 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
19875#define getVarint32NR(A,B) \
19876 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
19877#define putVarint32(A,B) \
19878 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
19879 sqlite3PutVarint((A),(B)))
19880#define getVarint sqlite3GetVarint
19881#define putVarint sqlite3PutVarint
19882
19883
19884SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19885SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
19886SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
19887SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
19888SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
19889SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr);
19890SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
19891SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
19892SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19893SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
19894SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
19895SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
19896SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
19897SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
19898
19899#if defined(SQLITE_NEED_ERR_NAME)
19900SQLITE_PRIVATE const char *sqlite3ErrName(int);
19901#endif
19902
19903#ifdef SQLITE_ENABLE_DESERIALIZE
19904SQLITE_PRIVATE int sqlite3MemdbInit(void);
19905#endif
19906
19907SQLITE_PRIVATE const char *sqlite3ErrStr(int);
19908SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
19909SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19910SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
19911SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19912SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8);
19913SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
19914SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
19915SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
19916SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19917SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19918SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
19919SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
19920SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
19921SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
19922SQLITE_PRIVATE int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
19923SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
19924SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
19925SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
19926SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
19927SQLITE_PRIVATE int sqlite3AbsInt32(int);
19928#ifdef SQLITE_ENABLE_8_3_NAMES
19929SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
19930#else
19931# define sqlite3FileSuffix3(X,Y)
19932#endif
19933SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
19934
19935SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
19936SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
19937SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
19938 void(*)(void*));
19939SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
19940SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
19941#ifndef SQLITE_UNTESTABLE
19942SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context*);
19943#endif
19944SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
19945#ifndef SQLITE_OMIT_UTF16
19946SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
19947#endif
19948SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
19949SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
19950#ifndef SQLITE_AMALGAMATION
19951SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
19952SQLITE_PRIVATE const char sqlite3StrBINARY[];
19953SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
19954SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
19955SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
19956SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19957SQLITE_API extern u32 sqlite3_unsupported_selecttrace;
19958#ifndef SQLITE_OMIT_WSD
19959SQLITE_PRIVATE int sqlite3PendingByte;
19960#endif
19961#endif /* SQLITE_AMALGAMATION */
19962#ifdef VDBE_PROFILE
19963SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
19964#endif
19965SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
19966SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
19967SQLITE_PRIVATE void sqlite3AlterFunctions(void);
19968SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
19969SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
19970SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
19971SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
19972SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
19973SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
19974SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
19975SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
19976SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
19977SQLITE_PRIVATE int sqlite3MatchEName(
19978 const struct ExprList_item*,
19979 const char*,
19980 const char*,
19981 const char*
19982);
19983SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
19984SQLITE_PRIVATE u8 sqlite3StrIHash(const char*);
19985SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
19986SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19987SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19988SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19989SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
19990SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
19991SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
19992SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
19993SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*);
19994SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
19995SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
19996SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
19997SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
19998SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
19999SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
20000SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
20001SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
20002SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
20003SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
20004SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
20005SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
20006SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
20007SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
20008SQLITE_PRIVATE void sqlite3SchemaClear(void *);
20009SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
20010SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
20011SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
20012SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
20013SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
20014SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
20015SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
20016SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse*, ExprList*);
20017
20018#ifdef SQLITE_DEBUG
20019SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
20020#endif
20021SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
20022 void (*)(sqlite3_context*,int,sqlite3_value **),
20023 void (*)(sqlite3_context*,int,sqlite3_value **),
20024 void (*)(sqlite3_context*),
20025 void (*)(sqlite3_context*),
20026 void (*)(sqlite3_context*,int,sqlite3_value **),
20027 FuncDestructor *pDestructor
20028);
20029SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
20030SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
20031SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
20032SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
20033SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
20034
20035SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
20036SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
20037SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
20038SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
20039
20040SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
20041SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
20042
20043#ifndef SQLITE_OMIT_SUBQUERY
20044SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
20045#else
20046# define sqlite3ExprCheckIN(x,y) SQLITE_OK
20047#endif
20048
20049#ifdef SQLITE_ENABLE_STAT4
20050SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
20051 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
20052SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
20053SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
20054SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
20055SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
20056#endif
20057
20058/*
20059** The interface to the LEMON-generated parser
20060*/
20061#ifndef SQLITE_AMALGAMATION
20062SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
20063SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
20064#endif
20065SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
20066SQLITE_PRIVATE int sqlite3ParserFallback(int);
20067#ifdef YYTRACKMAXSTACKDEPTH
20068SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
20069#endif
20070
20071SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
20072#ifndef SQLITE_OMIT_LOAD_EXTENSION
20073SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
20074#else
20075# define sqlite3CloseExtensions(X)
20076#endif
20077
20078#ifndef SQLITE_OMIT_SHARED_CACHE
20079SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
20080#else
20081 #define sqlite3TableLock(v,w,x,y,z)
20082#endif
20083
20084#ifdef SQLITE_TEST
20085SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
20086#endif
20087
20088#ifdef SQLITE_OMIT_VIRTUALTABLE
20089# define sqlite3VtabClear(Y)
20090# define sqlite3VtabSync(X,Y) SQLITE_OK
20091# define sqlite3VtabRollback(X)
20092# define sqlite3VtabCommit(X)
20093# define sqlite3VtabInSync(db) 0
20094# define sqlite3VtabLock(X)
20095# define sqlite3VtabUnlock(X)
20096# define sqlite3VtabModuleUnref(D,X)
20097# define sqlite3VtabUnlockList(X)
20098# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
20099# define sqlite3GetVTable(X,Y) ((VTable*)0)
20100#else
20101SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
20102SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
20103SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
20104SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
20105SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
20106SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
20107SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
20108SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3*,Module*);
20109SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
20110SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
20111SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
20112SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
20113SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
20114 sqlite3*,
20115 const char*,
20116 const sqlite3_module*,
20117 void*,
20118 void(*)(void*)
20119 );
20120# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20121#endif
20122SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db);
20123#ifndef SQLITE_OMIT_VIRTUALTABLE
20124SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
20125SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
20126#else
20127# define sqlite3ShadowTableName(A,B) 0
20128# define sqlite3IsShadowTableOf(A,B,C) 0
20129#endif
20130SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
20131SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
20132SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
20133SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
20134SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
20135SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
20136SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
20137SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
20138SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
20139SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
20140SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
20141SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
20142SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
20143SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
20144SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
20145SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
20146#ifdef SQLITE_ENABLE_NORMALIZE
20147SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
20148#endif
20149SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
20150SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
20151SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
20152SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
20153SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
20154SQLITE_PRIVATE const char *sqlite3JournalModename(int);
20155#ifndef SQLITE_OMIT_WAL
20156SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
20157SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
20158#endif
20159#ifndef SQLITE_OMIT_CTE
20160SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
20161SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
20162SQLITE_PRIVATE void sqlite3WithPush(Parse*, With*, u8);
20163#else
20164#define sqlite3WithPush(x,y,z)
20165#define sqlite3WithDelete(x,y)
20166#endif
20167#ifndef SQLITE_OMIT_UPSERT
20168SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
20169SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
20170SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
20171SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
20172SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
20173#else
20174#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
20175#define sqlite3UpsertDelete(x,y)
20176#define sqlite3UpsertDup(x,y) ((Upsert*)0)
20177#endif
20178
20179
20180/* Declarations for functions in fkey.c. All of these are replaced by
20181** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20182** key functionality is available. If OMIT_TRIGGER is defined but
20183** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20184** this case foreign keys are parsed, but no other functionality is
20185** provided (enforcement of FK constraints requires the triggers sub-system).
20186*/
20187#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
20188SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
20189SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
20190SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
20191SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
20192SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
20193SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
20194#else
20195 #define sqlite3FkActions(a,b,c,d,e,f)
20196 #define sqlite3FkCheck(a,b,c,d,e,f)
20197 #define sqlite3FkDropTable(a,b,c)
20198 #define sqlite3FkOldmask(a,b) 0
20199 #define sqlite3FkRequired(a,b,c,d) 0
20200 #define sqlite3FkReferences(a) 0
20201#endif
20202#ifndef SQLITE_OMIT_FOREIGN_KEY
20203SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
20204SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
20205#else
20206 #define sqlite3FkDelete(a,b)
20207 #define sqlite3FkLocateIndex(a,b,c,d,e)
20208#endif
20209
20210
20211/*
20212** Available fault injectors. Should be numbered beginning with 0.
20213*/
20214#define SQLITE_FAULTINJECTOR_MALLOC 0
20215#define SQLITE_FAULTINJECTOR_COUNT 1
20216
20217/*
20218** The interface to the code in fault.c used for identifying "benign"
20219** malloc failures. This is only present if SQLITE_UNTESTABLE
20220** is not defined.
20221*/
20222#ifndef SQLITE_UNTESTABLE
20223SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
20224SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
20225#else
20226 #define sqlite3BeginBenignMalloc()
20227 #define sqlite3EndBenignMalloc()
20228#endif
20229
20230/*
20231** Allowed return values from sqlite3FindInIndex()
20232*/
20233#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
20234#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
20235#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
20236#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
20237#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
20238/*
20239** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
20240*/
20241#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
20242#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
20243#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
20244SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
20245
20246SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
20247SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
20248#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
20249 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
20250SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
20251#endif
20252
20253SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
20254SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
20255
20256SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
20257#if SQLITE_MAX_EXPR_DEPTH>0
20258SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *);
20259SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
20260#else
20261 #define sqlite3SelectExprHeight(x) 0
20262 #define sqlite3ExprCheckHeight(x,y)
20263#endif
20264
20265SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
20266SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
20267
20268#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
20269SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
20270SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
20271SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
20272#else
20273 #define sqlite3ConnectionBlocked(x,y)
20274 #define sqlite3ConnectionUnlocked(x)
20275 #define sqlite3ConnectionClosed(x)
20276#endif
20277
20278#ifdef SQLITE_DEBUG
20279SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
20280#endif
20281#if defined(YYCOVERAGE)
20282SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
20283#endif
20284
20285/*
20286** If the SQLITE_ENABLE IOTRACE exists then the global variable
20287** sqlite3IoTrace is a pointer to a printf-like routine used to
20288** print I/O tracing messages.
20289*/
20290#ifdef SQLITE_ENABLE_IOTRACE
20291# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
20292SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
20293SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
20294#else
20295# define IOTRACE(A)
20296# define sqlite3VdbeIOTraceSql(X)
20297#endif
20298
20299/*
20300** These routines are available for the mem2.c debugging memory allocator
20301** only. They are used to verify that different "types" of memory
20302** allocations are properly tracked by the system.
20303**
20304** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
20305** the MEMTYPE_* macros defined below. The type must be a bitmask with
20306** a single bit set.
20307**
20308** sqlite3MemdebugHasType() returns true if any of the bits in its second
20309** argument match the type set by the previous sqlite3MemdebugSetType().
20310** sqlite3MemdebugHasType() is intended for use inside assert() statements.
20311**
20312** sqlite3MemdebugNoType() returns true if none of the bits in its second
20313** argument match the type set by the previous sqlite3MemdebugSetType().
20314**
20315** Perhaps the most important point is the difference between MEMTYPE_HEAP
20316** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
20317** it might have been allocated by lookaside, except the allocation was
20318** too large or lookaside was already full. It is important to verify
20319** that allocations that might have been satisfied by lookaside are not
20320** passed back to non-lookaside free() routines. Asserts such as the
20321** example above are placed on the non-lookaside free() routines to verify
20322** this constraint.
20323**
20324** All of this is no-op for a production build. It only comes into
20325** play when the SQLITE_MEMDEBUG compile-time option is used.
20326*/
20327#ifdef SQLITE_MEMDEBUG
20328SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
20329SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8);
20330SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8);
20331#else
20332# define sqlite3MemdebugSetType(X,Y) /* no-op */
20333# define sqlite3MemdebugHasType(X,Y) 1
20334# define sqlite3MemdebugNoType(X,Y) 1
20335#endif
20336#define MEMTYPE_HEAP 0x01 /* General heap allocations */
20337#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
20338#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
20339
20340/*
20341** Threading interface
20342*/
20343#if SQLITE_MAX_WORKER_THREADS>0
20344SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
20345SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
20346#endif
20347
20348#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
20349SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
20350#endif
20351#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
20352SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
20353#endif
20354
20355SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
20356SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
20357SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
20358SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
20359SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20360
20361#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20362SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20363#endif
20364
20365#endif /* SQLITEINT_H */
20366
20367/************** End of sqliteInt.h *******************************************/
20368/************** Begin file global.c ******************************************/
20369/*
20370** 2008 June 13
20371**
20372** The author disclaims copyright to this source code. In place of
20373** a legal notice, here is a blessing:
20374**
20375** May you do good and not evil.
20376** May you find forgiveness for yourself and forgive others.
20377** May you share freely, never taking more than you give.
20378**
20379*************************************************************************
20380**
20381** This file contains definitions of global variables and constants.
20382*/
20383/* #include "sqliteInt.h" */
20384
20385/* An array to map all upper-case characters into their corresponding
20386** lower-case character.
20387**
20388** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
20389** handle case conversions for the UTF character set since the tables
20390** involved are nearly as big or bigger than SQLite itself.
20391*/
20392SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
20393#ifdef SQLITE_ASCII
20394 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
20395 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
20396 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
20397 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
20398 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
20399 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
20400 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
20401 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
20402 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
20403 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
20404 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
20405 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
20406 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
20407 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
20408 252,253,254,255
20409#endif
20410#ifdef SQLITE_EBCDIC
20411 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
20412 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
20413 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
20414 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
20415 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
20416 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
20417 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
20418 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
20419 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
20420 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
20421 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
20422 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
20423 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
20424 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
20425 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
20426 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
20427#endif
20428};
20429
20430/*
20431** The following 256 byte lookup table is used to support SQLites built-in
20432** equivalents to the following standard library functions:
20433**
20434** isspace() 0x01
20435** isalpha() 0x02
20436** isdigit() 0x04
20437** isalnum() 0x06
20438** isxdigit() 0x08
20439** toupper() 0x20
20440** SQLite identifier character 0x40
20441** Quote character 0x80
20442**
20443** Bit 0x20 is set if the mapped character requires translation to upper
20444** case. i.e. if the character is a lower-case ASCII character.
20445** If x is a lower-case ASCII character, then its upper-case equivalent
20446** is (x - 0x20). Therefore toupper() can be implemented as:
20447**
20448** (x & ~(map[x]&0x20))
20449**
20450** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
20451** array. tolower() is used more often than toupper() by SQLite.
20452**
20453** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
20454** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
20455** non-ASCII UTF character. Hence the test for whether or not a character is
20456** part of an identifier is 0x46.
20457*/
20458SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
20459 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
20460 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
20461 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
20462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
20463 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
20464 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
20465 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
20466 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
20467
20468 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
20469 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
20470 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
20471 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
20472 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
20473 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
20474 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
20475 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
20476
20477 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
20478 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
20479 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
20480 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
20481 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
20482 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
20483 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
20484 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
20485
20486 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
20487 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
20488 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
20489 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
20490 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
20491 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
20492 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
20493 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
20494};
20495
20496/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
20497** compatibility for legacy applications, the URI filename capability is
20498** disabled by default.
20499**
20500** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
20501** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
20502**
20503** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
20504** disabled. The default value may be changed by compiling with the
20505** SQLITE_USE_URI symbol defined.
20506*/
20507#ifndef SQLITE_USE_URI
20508# define SQLITE_USE_URI 0
20509#endif
20510
20511/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
20512** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
20513** that compile-time option is omitted.
20514*/
20515#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN)
20516# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
20517#else
20518# if !SQLITE_ALLOW_COVERING_INDEX_SCAN
20519# error "Compile-time disabling of covering index scan using the\
20520 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
20521 Contact SQLite developers if this is a problem for you, and\
20522 delete this #error macro to continue with your build."
20523# endif
20524#endif
20525
20526/* The minimum PMA size is set to this value multiplied by the database
20527** page size in bytes.
20528*/
20529#ifndef SQLITE_SORTER_PMASZ
20530# define SQLITE_SORTER_PMASZ 250
20531#endif
20532
20533/* Statement journals spill to disk when their size exceeds the following
20534** threshold (in bytes). 0 means that statement journals are created and
20535** written to disk immediately (the default behavior for SQLite versions
20536** before 3.12.0). -1 means always keep the entire statement journal in
20537** memory. (The statement journal is also always held entirely in memory
20538** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
20539** setting.)
20540*/
20541#ifndef SQLITE_STMTJRNL_SPILL
20542# define SQLITE_STMTJRNL_SPILL (64*1024)
20543#endif
20544
20545/*
20546** The default lookaside-configuration, the format "SZ,N". SZ is the
20547** number of bytes in each lookaside slot (should be a multiple of 8)
20548** and N is the number of slots. The lookaside-configuration can be
20549** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
20550** or at run-time for an individual database connection using
20551** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
20552**
20553** With the two-size-lookaside enhancement, less lookaside is required.
20554** The default configuration of 1200,40 actually provides 30 1200-byte slots
20555** and 93 128-byte slots, which is more lookaside than is available
20556** using the older 1200,100 configuration without two-size-lookaside.
20557*/
20558#ifndef SQLITE_DEFAULT_LOOKASIDE
20559# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
20560# define SQLITE_DEFAULT_LOOKASIDE 1200,100 /* 120KB of memory */
20561# else
20562# define SQLITE_DEFAULT_LOOKASIDE 1200,40 /* 48KB of memory */
20563# endif
20564#endif
20565
20566
20567/* The default maximum size of an in-memory database created using
20568** sqlite3_deserialize()
20569*/
20570#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
20571# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
20572#endif
20573
20574/*
20575** The following singleton contains the global configuration for
20576** the SQLite library.
20577*/
20578SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
20579 SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
20580 1, /* bCoreMutex */
20581 SQLITE_THREADSAFE==1, /* bFullMutex */
20582 SQLITE_USE_URI, /* bOpenUri */
20583 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
20584 0, /* bSmallMalloc */
20585 1, /* bExtraSchemaChecks */
20586 0x7ffffffe, /* mxStrlen */
20587 0, /* neverCorrupt */
20588 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
20589 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
20590 {0,0,0,0,0,0,0,0}, /* m */
20591 {0,0,0,0,0,0,0,0,0}, /* mutex */
20592 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
20593 (void*)0, /* pHeap */
20594 0, /* nHeap */
20595 0, 0, /* mnHeap, mxHeap */
20596 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
20597 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
20598 (void*)0, /* pPage */
20599 0, /* szPage */
20600 SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
20601 0, /* mxParserStack */
20602 0, /* sharedCacheEnabled */
20603 SQLITE_SORTER_PMASZ, /* szPma */
20604 /* All the rest should always be initialized to zero */
20605 0, /* isInit */
20606 0, /* inProgress */
20607 0, /* isMutexInit */
20608 0, /* isMallocInit */
20609 0, /* isPCacheInit */
20610 0, /* nRefInitMutex */
20611 0, /* pInitMutex */
20612 0, /* xLog */
20613 0, /* pLogArg */
20614#ifdef SQLITE_ENABLE_SQLLOG
20615 0, /* xSqllog */
20616 0, /* pSqllogArg */
20617#endif
20618#ifdef SQLITE_VDBE_COVERAGE
20619 0, /* xVdbeBranch */
20620 0, /* pVbeBranchArg */
20621#endif
20622#ifdef SQLITE_ENABLE_DESERIALIZE
20623 SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */
20624#endif
20625#ifndef SQLITE_UNTESTABLE
20626 0, /* xTestCallback */
20627#endif
20628 0, /* bLocaltimeFault */
20629 0x7ffffffe, /* iOnceResetThreshold */
20630 SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */
20631 0, /* iPrngSeed */
20632};
20633
20634/*
20635** Hash table for global functions - functions common to all
20636** database connections. After initialization, this table is
20637** read-only.
20638*/
20639SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
20640
20641#ifdef VDBE_PROFILE
20642/*
20643** The following performance counter can be used in place of
20644** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
20645*/
20646SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
20647#endif
20648
20649/*
20650** The value of the "pending" byte must be 0x40000000 (1 byte past the
20651** 1-gibabyte boundary) in a compatible database. SQLite never uses
20652** the database page that contains the pending byte. It never attempts
20653** to read or write that page. The pending byte page is set aside
20654** for use by the VFS layers as space for managing file locks.
20655**
20656** During testing, it is often desirable to move the pending byte to
20657** a different position in the file. This allows code that has to
20658** deal with the pending byte to run on files that are much smaller
20659** than 1 GiB. The sqlite3_test_control() interface can be used to
20660** move the pending byte.
20661**
20662** IMPORTANT: Changing the pending byte to any value other than
20663** 0x40000000 results in an incompatible database file format!
20664** Changing the pending byte during operation will result in undefined
20665** and incorrect behavior.
20666*/
20667#ifndef SQLITE_OMIT_WSD
20668SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
20669#endif
20670
20671/*
20672** Flags for select tracing and the ".selecttrace" macro of the CLI
20673*/
20674SQLITE_API u32 sqlite3_unsupported_selecttrace = 0;
20675
20676/* #include "opcodes.h" */
20677/*
20678** Properties of opcodes. The OPFLG_INITIALIZER macro is
20679** created by mkopcodeh.awk during compilation. Data is obtained
20680** from the comments following the "case OP_xxxx:" statements in
20681** the vdbe.c file.
20682*/
20683SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
20684
20685/*
20686** Name of the default collating sequence
20687*/
20688SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
20689
20690/************** End of global.c **********************************************/
20691/************** Begin file status.c ******************************************/
20692/*
20693** 2008 June 18
20694**
20695** The author disclaims copyright to this source code. In place of
20696** a legal notice, here is a blessing:
20697**
20698** May you do good and not evil.
20699** May you find forgiveness for yourself and forgive others.
20700** May you share freely, never taking more than you give.
20701**
20702*************************************************************************
20703**
20704** This module implements the sqlite3_status() interface and related
20705** functionality.
20706*/
20707/* #include "sqliteInt.h" */
20708/************** Include vdbeInt.h in the middle of status.c ******************/
20709/************** Begin file vdbeInt.h *****************************************/
20710/*
20711** 2003 September 6
20712**
20713** The author disclaims copyright to this source code. In place of
20714** a legal notice, here is a blessing:
20715**
20716** May you do good and not evil.
20717** May you find forgiveness for yourself and forgive others.
20718** May you share freely, never taking more than you give.
20719**
20720*************************************************************************
20721** This is the header file for information that is private to the
20722** VDBE. This information used to all be at the top of the single
20723** source code file "vdbe.c". When that file became too big (over
20724** 6000 lines long) it was split up into several smaller files and
20725** this header information was factored out.
20726*/
20727#ifndef SQLITE_VDBEINT_H
20728#define SQLITE_VDBEINT_H
20729
20730/*
20731** The maximum number of times that a statement will try to reparse
20732** itself before giving up and returning SQLITE_SCHEMA.
20733*/
20734#ifndef SQLITE_MAX_SCHEMA_RETRY
20735# define SQLITE_MAX_SCHEMA_RETRY 50
20736#endif
20737
20738/*
20739** VDBE_DISPLAY_P4 is true or false depending on whether or not the
20740** "explain" P4 display logic is enabled.
20741*/
20742#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
20743 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
20744 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
20745# define VDBE_DISPLAY_P4 1
20746#else
20747# define VDBE_DISPLAY_P4 0
20748#endif
20749
20750/*
20751** SQL is translated into a sequence of instructions to be
20752** executed by a virtual machine. Each instruction is an instance
20753** of the following structure.
20754*/
20755typedef struct VdbeOp Op;
20756
20757/*
20758** Boolean values
20759*/
20760typedef unsigned Bool;
20761
20762/* Opaque type used by code in vdbesort.c */
20763typedef struct VdbeSorter VdbeSorter;
20764
20765/* Elements of the linked list at Vdbe.pAuxData */
20766typedef struct AuxData AuxData;
20767
20768/* Types of VDBE cursors */
20769#define CURTYPE_BTREE 0
20770#define CURTYPE_SORTER 1
20771#define CURTYPE_VTAB 2
20772#define CURTYPE_PSEUDO 3
20773
20774/*
20775** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
20776**
20777** * A b-tree cursor
20778** - In the main database or in an ephemeral database
20779** - On either an index or a table
20780** * A sorter
20781** * A virtual table
20782** * A one-row "pseudotable" stored in a single register
20783*/
20784typedef struct VdbeCursor VdbeCursor;
20785struct VdbeCursor {
20786 u8 eCurType; /* One of the CURTYPE_* values above */
20787 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
20788 u8 nullRow; /* True if pointing to a row with no data */
20789 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
20790 u8 isTable; /* True for rowid tables. False for indexes */
20791#ifdef SQLITE_DEBUG
20792 u8 seekOp; /* Most recent seek operation on this cursor */
20793 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
20794#endif
20795 Bool isEphemeral:1; /* True for an ephemeral table */
20796 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
20797 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
20798 Bool seekHit:1; /* See the OP_SeekHit and OP_IfNoHope opcodes */
20799 Btree *pBtx; /* Separate file holding temporary table */
20800 i64 seqCount; /* Sequence counter */
20801 u32 *aAltMap; /* Mapping from table to index column numbers */
20802
20803 /* Cached OP_Column parse information is only valid if cacheStatus matches
20804 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
20805 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
20806 ** the cache is out of date. */
20807 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
20808 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
20809 ** if there have been no prior seeks on the cursor. */
20810 /* seekResult does not distinguish between "no seeks have ever occurred
20811 ** on this cursor" and "the most recent seek was an exact match".
20812 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
20813
20814 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
20815 ** The fields that follow are uninitialized, and must be individually
20816 ** initialized prior to first use. */
20817 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
20818 union {
20819 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
20820 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
20821 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
20822 } uc;
20823 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
20824 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
20825 Pgno pgnoRoot; /* Root page of the open btree cursor */
20826 i16 nField; /* Number of fields in the header */
20827 u16 nHdrParsed; /* Number of header fields parsed so far */
20828 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
20829 u32 *aOffset; /* Pointer to aType[nField] */
20830 const u8 *aRow; /* Data for the current row, if all on one page */
20831 u32 payloadSize; /* Total number of bytes in the record */
20832 u32 szRow; /* Byte available in aRow */
20833#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
20834 u64 maskUsed; /* Mask of columns used by this cursor */
20835#endif
20836
20837 /* 2*nField extra array elements allocated for aType[], beyond the one
20838 ** static element declared in the structure. nField total array slots for
20839 ** aType[] and nField+1 array slots for aOffset[] */
20840 u32 aType[1]; /* Type values record decode. MUST BE LAST */
20841};
20842
20843
20844/*
20845** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
20846*/
20847#define CACHE_STALE 0
20848
20849/*
20850** When a sub-program is executed (OP_Program), a structure of this type
20851** is allocated to store the current value of the program counter, as
20852** well as the current memory cell array and various other frame specific
20853** values stored in the Vdbe struct. When the sub-program is finished,
20854** these values are copied back to the Vdbe from the VdbeFrame structure,
20855** restoring the state of the VM to as it was before the sub-program
20856** began executing.
20857**
20858** The memory for a VdbeFrame object is allocated and managed by a memory
20859** cell in the parent (calling) frame. When the memory cell is deleted or
20860** overwritten, the VdbeFrame object is not freed immediately. Instead, it
20861** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
20862** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
20863** this instead of deleting the VdbeFrame immediately is to avoid recursive
20864** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
20865** child frame are released.
20866**
20867** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
20868** set to NULL if the currently executing frame is the main program.
20869*/
20870typedef struct VdbeFrame VdbeFrame;
20871struct VdbeFrame {
20872 Vdbe *v; /* VM this frame belongs to */
20873 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
20874 Op *aOp; /* Program instructions for parent frame */
20875 i64 *anExec; /* Event counters from parent frame */
20876 Mem *aMem; /* Array of memory cells for parent frame */
20877 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
20878 u8 *aOnce; /* Bitmask used by OP_Once */
20879 void *token; /* Copy of SubProgram.token */
20880 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
20881 AuxData *pAuxData; /* Linked list of auxdata allocations */
20882#if SQLITE_DEBUG
20883 u32 iFrameMagic; /* magic number for sanity checking */
20884#endif
20885 int nCursor; /* Number of entries in apCsr */
20886 int pc; /* Program Counter in parent (calling) frame */
20887 int nOp; /* Size of aOp array */
20888 int nMem; /* Number of entries in aMem */
20889 int nChildMem; /* Number of memory cells for child frame */
20890 int nChildCsr; /* Number of cursors for child frame */
20891 int nChange; /* Statement changes (Vdbe.nChange) */
20892 int nDbChange; /* Value of db->nChange */
20893};
20894
20895/* Magic number for sanity checking on VdbeFrame objects */
20896#define SQLITE_FRAME_MAGIC 0x879fb71e
20897
20898/*
20899** Return a pointer to the array of registers allocated for use
20900** by a VdbeFrame.
20901*/
20902#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
20903
20904/*
20905** Internally, the vdbe manipulates nearly all SQL values as Mem
20906** structures. Each Mem struct may cache multiple representations (string,
20907** integer etc.) of the same value.
20908*/
20909struct sqlite3_value {
20910 union MemValue {
20911 double r; /* Real value used when MEM_Real is set in flags */
20912 i64 i; /* Integer value used when MEM_Int is set in flags */
20913 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
20914 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
20915 FuncDef *pDef; /* Used only when flags==MEM_Agg */
20916 } u;
20917 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
20918 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
20919 u8 eSubtype; /* Subtype for this value */
20920 int n; /* Number of characters in string value, excluding '\0' */
20921 char *z; /* String or BLOB value */
20922 /* ShallowCopy only needs to copy the information above */
20923 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
20924 int szMalloc; /* Size of the zMalloc allocation */
20925 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
20926 sqlite3 *db; /* The associated database connection */
20927 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
20928#ifdef SQLITE_DEBUG
20929 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
20930 u16 mScopyFlags; /* flags value immediately after the shallow copy */
20931#endif
20932};
20933
20934/*
20935** Size of struct Mem not including the Mem.zMalloc member or anything that
20936** follows.
20937*/
20938#define MEMCELLSIZE offsetof(Mem,zMalloc)
20939
20940/* One or more of the following flags are set to indicate the validOK
20941** representations of the value stored in the Mem struct.
20942**
20943** If the MEM_Null flag is set, then the value is an SQL NULL value.
20944** For a pointer type created using sqlite3_bind_pointer() or
20945** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
20946**
20947** If the MEM_Str flag is set then Mem.z points at a string representation.
20948** Usually this is encoded in the same unicode encoding as the main
20949** database (see below for exceptions). If the MEM_Term flag is also
20950** set, then the string is nul terminated. The MEM_Int and MEM_Real
20951** flags may coexist with the MEM_Str flag.
20952*/
20953#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
20954#define MEM_Str 0x0002 /* Value is a string */
20955#define MEM_Int 0x0004 /* Value is an integer */
20956#define MEM_Real 0x0008 /* Value is a real number */
20957#define MEM_Blob 0x0010 /* Value is a BLOB */
20958#define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */
20959#define MEM_AffMask 0x003f /* Mask of affinity bits */
20960#define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */
20961#define MEM_Undefined 0x0080 /* Value is undefined */
20962#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
20963#define MEM_TypeMask 0xc1bf /* Mask of type bits */
20964
20965
20966/* Whenever Mem contains a valid string or blob representation, one of
20967** the following flags must be set to determine the memory management
20968** policy for Mem.z. The MEM_Term flag tells us whether or not the
20969** string is \000 or \u0000 terminated
20970*/
20971#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */
20972#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
20973#define MEM_Static 0x0800 /* Mem.z points to a static string */
20974#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
20975#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
20976#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
20977#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
20978#ifdef SQLITE_OMIT_INCRBLOB
20979 #undef MEM_Zero
20980 #define MEM_Zero 0x0000
20981#endif
20982
20983/* Return TRUE if Mem X contains dynamically allocated content - anything
20984** that needs to be deallocated to avoid a leak.
20985*/
20986#define VdbeMemDynamic(X) \
20987 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
20988
20989/*
20990** Clear any existing type flags from a Mem and replace them with f
20991*/
20992#define MemSetTypeFlag(p, f) \
20993 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
20994
20995/*
20996** True if Mem X is a NULL-nochng type.
20997*/
20998#define MemNullNochng(X) \
20999 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
21000 && (X)->n==0 && (X)->u.nZero==0)
21001
21002/*
21003** Return true if a memory cell is not marked as invalid. This macro
21004** is for use inside assert() statements only.
21005*/
21006#ifdef SQLITE_DEBUG
21007#define memIsValid(M) ((M)->flags & MEM_Undefined)==0
21008#endif
21009
21010/*
21011** Each auxiliary data pointer stored by a user defined function
21012** implementation calling sqlite3_set_auxdata() is stored in an instance
21013** of this structure. All such structures associated with a single VM
21014** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
21015** when the VM is halted (if not before).
21016*/
21017struct AuxData {
21018 int iAuxOp; /* Instruction number of OP_Function opcode */
21019 int iAuxArg; /* Index of function argument. */
21020 void *pAux; /* Aux data pointer */
21021 void (*xDeleteAux)(void*); /* Destructor for the aux data */
21022 AuxData *pNextAux; /* Next element in list */
21023};
21024
21025/*
21026** The "context" argument for an installable function. A pointer to an
21027** instance of this structure is the first argument to the routines used
21028** implement the SQL functions.
21029**
21030** There is a typedef for this structure in sqlite.h. So all routines,
21031** even the public interface to SQLite, can use a pointer to this structure.
21032** But this file is the only place where the internal details of this
21033** structure are known.
21034**
21035** This structure is defined inside of vdbeInt.h because it uses substructures
21036** (Mem) which are only defined there.
21037*/
21038struct sqlite3_context {
21039 Mem *pOut; /* The return value is stored here */
21040 FuncDef *pFunc; /* Pointer to function information */
21041 Mem *pMem; /* Memory cell used to store aggregate context */
21042 Vdbe *pVdbe; /* The VM that owns this context */
21043 int iOp; /* Instruction number of OP_Function */
21044 int isError; /* Error code returned by the function. */
21045 u8 skipFlag; /* Skip accumulator loading if true */
21046 u8 argc; /* Number of arguments */
21047 sqlite3_value *argv[1]; /* Argument set */
21048};
21049
21050/* A bitfield type for use inside of structures. Always follow with :N where
21051** N is the number of bits.
21052*/
21053typedef unsigned bft; /* Bit Field Type */
21054
21055/* The ScanStatus object holds a single value for the
21056** sqlite3_stmt_scanstatus() interface.
21057*/
21058typedef struct ScanStatus ScanStatus;
21059struct ScanStatus {
21060 int addrExplain; /* OP_Explain for loop */
21061 int addrLoop; /* Address of "loops" counter */
21062 int addrVisit; /* Address of "rows visited" counter */
21063 int iSelectID; /* The "Select-ID" for this loop */
21064 LogEst nEst; /* Estimated output rows per loop */
21065 char *zName; /* Name of table or index */
21066};
21067
21068/* The DblquoteStr object holds the text of a double-quoted
21069** string for a prepared statement. A linked list of these objects
21070** is constructed during statement parsing and is held on Vdbe.pDblStr.
21071** When computing a normalized SQL statement for an SQL statement, that
21072** list is consulted for each double-quoted identifier to see if the
21073** identifier should really be a string literal.
21074*/
21075typedef struct DblquoteStr DblquoteStr;
21076struct DblquoteStr {
21077 DblquoteStr *pNextStr; /* Next string literal in the list */
21078 char z[8]; /* Dequoted value for the string */
21079};
21080
21081/*
21082** An instance of the virtual machine. This structure contains the complete
21083** state of the virtual machine.
21084**
21085** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
21086** is really a pointer to an instance of this structure.
21087*/
21088struct Vdbe {
21089 sqlite3 *db; /* The database connection that owns this statement */
21090 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
21091 Parse *pParse; /* Parsing context used to create this Vdbe */
21092 ynVar nVar; /* Number of entries in aVar[] */
21093 u32 magic; /* Magic number for sanity checking */
21094 int nMem; /* Number of memory locations currently allocated */
21095 int nCursor; /* Number of slots in apCsr[] */
21096 u32 cacheCtr; /* VdbeCursor row cache generation counter */
21097 int pc; /* The program counter */
21098 int rc; /* Value to return */
21099 int nChange; /* Number of db changes made since last reset */
21100 int iStatement; /* Statement number (or 0 if has no opened stmt) */
21101 i64 iCurrentTime; /* Value of julianday('now') for this statement */
21102 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
21103 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
21104 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
21105 Mem *aMem; /* The memory locations */
21106 Mem **apArg; /* Arguments to currently executing user function */
21107 VdbeCursor **apCsr; /* One element of this array for each open cursor */
21108 Mem *aVar; /* Values for the OP_Variable opcode. */
21109
21110 /* When allocating a new Vdbe object, all of the fields below should be
21111 ** initialized to zero or NULL */
21112
21113 Op *aOp; /* Space to hold the virtual machine's program */
21114 int nOp; /* Number of instructions in the program */
21115 int nOpAlloc; /* Slots allocated for aOp[] */
21116 Mem *aColName; /* Column names to return */
21117 Mem *pResultSet; /* Pointer to an array of results */
21118 char *zErrMsg; /* Error message written here */
21119 VList *pVList; /* Name of variables */
21120#ifndef SQLITE_OMIT_TRACE
21121 i64 startTime; /* Time when query started - used for profiling */
21122#endif
21123#ifdef SQLITE_DEBUG
21124 int rcApp; /* errcode set by sqlite3_result_error_code() */
21125 u32 nWrite; /* Number of write operations that have occurred */
21126#endif
21127 u16 nResColumn; /* Number of columns in one row of the result set */
21128 u8 errorAction; /* Recovery action to do in case of an error */
21129 u8 minWriteFileFormat; /* Minimum file format for writable database files */
21130 u8 prepFlags; /* SQLITE_PREPARE_* flags */
21131 u8 doingRerun; /* True if rerunning after an auto-reprepare */
21132 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
21133 bft explain:2; /* True if EXPLAIN present on SQL command */
21134 bft changeCntOn:1; /* True to update the change-counter */
21135 bft runOnlyOnce:1; /* Automatically expire on reset */
21136 bft usesStmtJournal:1; /* True if uses a statement journal */
21137 bft readOnly:1; /* True for statements that do not write */
21138 bft bIsReader:1; /* True for statements that read */
21139 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
21140 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
21141 u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */
21142 char *zSql; /* Text of the SQL statement that generated this */
21143#ifdef SQLITE_ENABLE_NORMALIZE
21144 char *zNormSql; /* Normalization of the associated SQL statement */
21145 DblquoteStr *pDblStr; /* List of double-quoted string literals */
21146#endif
21147 void *pFree; /* Free this when deleting the vdbe */
21148 VdbeFrame *pFrame; /* Parent frame */
21149 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
21150 int nFrame; /* Number of frames in pFrame list */
21151 u32 expmask; /* Binding to these vars invalidates VM */
21152 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
21153 AuxData *pAuxData; /* Linked list of auxdata allocations */
21154#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
21155 i64 *anExec; /* Number of times each op has been executed */
21156 int nScan; /* Entries in aScan[] */
21157 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
21158#endif
21159};
21160
21161/*
21162** The following are allowed values for Vdbe.magic
21163*/
21164#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */
21165#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */
21166#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */
21167#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */
21168#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */
21169
21170/*
21171** Structure used to store the context required by the
21172** sqlite3_preupdate_*() API functions.
21173*/
21174struct PreUpdate {
21175 Vdbe *v;
21176 VdbeCursor *pCsr; /* Cursor to read old values from */
21177 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
21178 u8 *aRecord; /* old.* database record */
21179 KeyInfo keyinfo;
21180 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
21181 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
21182 int iNewReg; /* Register for new.* values */
21183 i64 iKey1; /* First key value passed to hook */
21184 i64 iKey2; /* Second key value passed to hook */
21185 Mem *aNew; /* Array of new.* values */
21186 Table *pTab; /* Schema object being upated */
21187 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
21188};
21189
21190/*
21191** Function prototypes
21192*/
21193SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
21194SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
21195void sqliteVdbePopStack(Vdbe*,int);
21196SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*);
21197SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*);
21198SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
21199SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
21200SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
21201SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
21202SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
21203SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
21204
21205int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
21206SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
21207SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
21208SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
21209#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
21210SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
21211SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
21212#endif
21213#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
21214SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
21215#endif
21216#if !defined(SQLITE_OMIT_EXPLAIN)
21217SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
21218#endif
21219SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
21220SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
21221SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
21222SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
21223SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
21224SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
21225SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
21226SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
21227SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
21228#ifdef SQLITE_OMIT_FLOATING_POINT
21229# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
21230#else
21231SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
21232#endif
21233SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
21234SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
21235SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
21236SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
21237#ifdef SQLITE_DEBUG
21238SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
21239#endif
21240SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
21241SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
21242SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
21243SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
21244SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
21245SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
21246SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
21247SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
21248SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
21249SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
21250SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
21251SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
21252SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
21253SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
21254SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
21255#ifndef SQLITE_OMIT_WINDOWFUNC
21256SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
21257#endif
21258#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
21259SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
21260#endif
21261SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
21262SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
21263SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
21264#ifdef SQLITE_DEBUG
21265SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
21266#endif
21267SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
21268SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
21269SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
21270#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
21271SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
21272#endif
21273SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
21274
21275SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
21276SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
21277SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
21278SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
21279SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
21280SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
21281SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
21282SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
21283
21284#ifdef SQLITE_DEBUG
21285SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
21286SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
21287#else
21288# define sqlite3VdbeIncrWriteCounter(V,C)
21289# define sqlite3VdbeAssertAbortable(V)
21290#endif
21291
21292#if !defined(SQLITE_OMIT_SHARED_CACHE)
21293SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
21294#else
21295# define sqlite3VdbeEnter(X)
21296#endif
21297
21298#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
21299SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
21300#else
21301# define sqlite3VdbeLeave(X)
21302#endif
21303
21304#ifdef SQLITE_DEBUG
21305SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
21306SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
21307#endif
21308
21309#ifndef SQLITE_OMIT_FOREIGN_KEY
21310SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
21311#else
21312# define sqlite3VdbeCheckFk(p,i) 0
21313#endif
21314
21315#ifdef SQLITE_DEBUG
21316SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
21317SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
21318#endif
21319#ifndef SQLITE_OMIT_UTF16
21320SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
21321SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
21322#endif
21323
21324#ifndef SQLITE_OMIT_INCRBLOB
21325SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
21326 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
21327#else
21328 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
21329 #define ExpandBlob(P) SQLITE_OK
21330#endif
21331
21332#endif /* !defined(SQLITE_VDBEINT_H) */
21333
21334/************** End of vdbeInt.h *********************************************/
21335/************** Continuing where we left off in status.c *********************/
21336
21337/*
21338** Variables in which to record status information.
21339*/
21340#if SQLITE_PTRSIZE>4
21341typedef sqlite3_int64 sqlite3StatValueType;
21342#else
21343typedef u32 sqlite3StatValueType;
21344#endif
21345typedef struct sqlite3StatType sqlite3StatType;
21346static SQLITE_WSD struct sqlite3StatType {
21347 sqlite3StatValueType nowValue[10]; /* Current value */
21348 sqlite3StatValueType mxValue[10]; /* Maximum value */
21349} sqlite3Stat = { {0,}, {0,} };
21350
21351/*
21352** Elements of sqlite3Stat[] are protected by either the memory allocator
21353** mutex, or by the pcache1 mutex. The following array determines which.
21354*/
21355static const char statMutex[] = {
21356 0, /* SQLITE_STATUS_MEMORY_USED */
21357 1, /* SQLITE_STATUS_PAGECACHE_USED */
21358 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
21359 0, /* SQLITE_STATUS_SCRATCH_USED */
21360 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
21361 0, /* SQLITE_STATUS_MALLOC_SIZE */
21362 0, /* SQLITE_STATUS_PARSER_STACK */
21363 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
21364 0, /* SQLITE_STATUS_SCRATCH_SIZE */
21365 0, /* SQLITE_STATUS_MALLOC_COUNT */
21366};
21367
21368
21369/* The "wsdStat" macro will resolve to the status information
21370** state vector. If writable static data is unsupported on the target,
21371** we have to locate the state vector at run-time. In the more common
21372** case where writable static data is supported, wsdStat can refer directly
21373** to the "sqlite3Stat" state vector declared above.
21374*/
21375#ifdef SQLITE_OMIT_WSD
21376# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
21377# define wsdStat x[0]
21378#else
21379# define wsdStatInit
21380# define wsdStat sqlite3Stat
21381#endif
21382
21383/*
21384** Return the current value of a status parameter. The caller must
21385** be holding the appropriate mutex.
21386*/
21387SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
21388 wsdStatInit;
21389 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21390 assert( op>=0 && op<ArraySize(statMutex) );
21391 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21392 : sqlite3MallocMutex()) );
21393 return wsdStat.nowValue[op];
21394}
21395
21396/*
21397** Add N to the value of a status record. The caller must hold the
21398** appropriate mutex. (Locking is checked by assert()).
21399**
21400** The StatusUp() routine can accept positive or negative values for N.
21401** The value of N is added to the current status value and the high-water
21402** mark is adjusted if necessary.
21403**
21404** The StatusDown() routine lowers the current value by N. The highwater
21405** mark is unchanged. N must be non-negative for StatusDown().
21406*/
21407SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
21408 wsdStatInit;
21409 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21410 assert( op>=0 && op<ArraySize(statMutex) );
21411 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21412 : sqlite3MallocMutex()) );
21413 wsdStat.nowValue[op] += N;
21414 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
21415 wsdStat.mxValue[op] = wsdStat.nowValue[op];
21416 }
21417}
21418SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
21419 wsdStatInit;
21420 assert( N>=0 );
21421 assert( op>=0 && op<ArraySize(statMutex) );
21422 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21423 : sqlite3MallocMutex()) );
21424 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21425 wsdStat.nowValue[op] -= N;
21426}
21427
21428/*
21429** Adjust the highwater mark if necessary.
21430** The caller must hold the appropriate mutex.
21431*/
21432SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
21433 sqlite3StatValueType newValue;
21434 wsdStatInit;
21435 assert( X>=0 );
21436 newValue = (sqlite3StatValueType)X;
21437 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
21438 assert( op>=0 && op<ArraySize(statMutex) );
21439 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
21440 : sqlite3MallocMutex()) );
21441 assert( op==SQLITE_STATUS_MALLOC_SIZE
21442 || op==SQLITE_STATUS_PAGECACHE_SIZE
21443 || op==SQLITE_STATUS_PARSER_STACK );
21444 if( newValue>wsdStat.mxValue[op] ){
21445 wsdStat.mxValue[op] = newValue;
21446 }
21447}
21448
21449/*
21450** Query status information.
21451*/
21452SQLITE_API int sqlite3_status64(
21453 int op,
21454 sqlite3_int64 *pCurrent,
21455 sqlite3_int64 *pHighwater,
21456 int resetFlag
21457){
21458 sqlite3_mutex *pMutex;
21459 wsdStatInit;
21460 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
21461 return SQLITE_MISUSE_BKPT;
21462 }
21463#ifdef SQLITE_ENABLE_API_ARMOR
21464 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
21465#endif
21466 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
21467 sqlite3_mutex_enter(pMutex);
21468 *pCurrent = wsdStat.nowValue[op];
21469 *pHighwater = wsdStat.mxValue[op];
21470 if( resetFlag ){
21471 wsdStat.mxValue[op] = wsdStat.nowValue[op];
21472 }
21473 sqlite3_mutex_leave(pMutex);
21474 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
21475 return SQLITE_OK;
21476}
21477SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
21478 sqlite3_int64 iCur = 0, iHwtr = 0;
21479 int rc;
21480#ifdef SQLITE_ENABLE_API_ARMOR
21481 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
21482#endif
21483 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
21484 if( rc==0 ){
21485 *pCurrent = (int)iCur;
21486 *pHighwater = (int)iHwtr;
21487 }
21488 return rc;
21489}
21490
21491/*
21492** Return the number of LookasideSlot elements on the linked list
21493*/
21494static u32 countLookasideSlots(LookasideSlot *p){
21495 u32 cnt = 0;
21496 while( p ){
21497 p = p->pNext;
21498 cnt++;
21499 }
21500 return cnt;
21501}
21502
21503/*
21504** Count the number of slots of lookaside memory that are outstanding
21505*/
21506SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
21507 u32 nInit = countLookasideSlots(db->lookaside.pInit);
21508 u32 nFree = countLookasideSlots(db->lookaside.pFree);
21509#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21510 nInit += countLookasideSlots(db->lookaside.pSmallInit);
21511 nFree += countLookasideSlots(db->lookaside.pSmallFree);
21512#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
21513 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
21514 return db->lookaside.nSlot - (nInit+nFree);
21515}
21516
21517/*
21518** Query status information for a single database connection
21519*/
21520SQLITE_API int sqlite3_db_status(
21521 sqlite3 *db, /* The database connection whose status is desired */
21522 int op, /* Status verb */
21523 int *pCurrent, /* Write current value here */
21524 int *pHighwater, /* Write high-water mark here */
21525 int resetFlag /* Reset high-water mark if true */
21526){
21527 int rc = SQLITE_OK; /* Return code */
21528#ifdef SQLITE_ENABLE_API_ARMOR
21529 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
21530 return SQLITE_MISUSE_BKPT;
21531 }
21532#endif
21533 sqlite3_mutex_enter(db->mutex);
21534 switch( op ){
21535 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
21536 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
21537 if( resetFlag ){
21538 LookasideSlot *p = db->lookaside.pFree;
21539 if( p ){
21540 while( p->pNext ) p = p->pNext;
21541 p->pNext = db->lookaside.pInit;
21542 db->lookaside.pInit = db->lookaside.pFree;
21543 db->lookaside.pFree = 0;
21544 }
21545#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
21546 p = db->lookaside.pSmallFree;
21547 if( p ){
21548 while( p->pNext ) p = p->pNext;
21549 p->pNext = db->lookaside.pSmallInit;
21550 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
21551 db->lookaside.pSmallFree = 0;
21552 }
21553#endif
21554 }
21555 break;
21556 }
21557
21558 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
21559 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
21560 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
21561 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
21562 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
21563 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
21564 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
21565 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
21566 *pCurrent = 0;
21567 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
21568 if( resetFlag ){
21569 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
21570 }
21571 break;
21572 }
21573
21574 /*
21575 ** Return an approximation for the amount of memory currently used
21576 ** by all pagers associated with the given database connection. The
21577 ** highwater mark is meaningless and is returned as zero.
21578 */
21579 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
21580 case SQLITE_DBSTATUS_CACHE_USED: {
21581 int totalUsed = 0;
21582 int i;
21583 sqlite3BtreeEnterAll(db);
21584 for(i=0; i<db->nDb; i++){
21585 Btree *pBt = db->aDb[i].pBt;
21586 if( pBt ){
21587 Pager *pPager = sqlite3BtreePager(pBt);
21588 int nByte = sqlite3PagerMemUsed(pPager);
21589 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
21590 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
21591 }
21592 totalUsed += nByte;
21593 }
21594 }
21595 sqlite3BtreeLeaveAll(db);
21596 *pCurrent = totalUsed;
21597 *pHighwater = 0;
21598 break;
21599 }
21600
21601 /*
21602 ** *pCurrent gets an accurate estimate of the amount of memory used
21603 ** to store the schema for all databases (main, temp, and any ATTACHed
21604 ** databases. *pHighwater is set to zero.
21605 */
21606 case SQLITE_DBSTATUS_SCHEMA_USED: {
21607 int i; /* Used to iterate through schemas */
21608 int nByte = 0; /* Used to accumulate return value */
21609
21610 sqlite3BtreeEnterAll(db);
21611 db->pnBytesFreed = &nByte;
21612 for(i=0; i<db->nDb; i++){
21613 Schema *pSchema = db->aDb[i].pSchema;
21614 if( ALWAYS(pSchema!=0) ){
21615 HashElem *p;
21616
21617 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
21618 pSchema->tblHash.count
21619 + pSchema->trigHash.count
21620 + pSchema->idxHash.count
21621 + pSchema->fkeyHash.count
21622 );
21623 nByte += sqlite3_msize(pSchema->tblHash.ht);
21624 nByte += sqlite3_msize(pSchema->trigHash.ht);
21625 nByte += sqlite3_msize(pSchema->idxHash.ht);
21626 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
21627
21628 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
21629 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
21630 }
21631 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
21632 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
21633 }
21634 }
21635 }
21636 db->pnBytesFreed = 0;
21637 sqlite3BtreeLeaveAll(db);
21638
21639 *pHighwater = 0;
21640 *pCurrent = nByte;
21641 break;
21642 }
21643
21644 /*
21645 ** *pCurrent gets an accurate estimate of the amount of memory used
21646 ** to store all prepared statements.
21647 ** *pHighwater is set to zero.
21648 */
21649 case SQLITE_DBSTATUS_STMT_USED: {
21650 struct Vdbe *pVdbe; /* Used to iterate through VMs */
21651 int nByte = 0; /* Used to accumulate return value */
21652
21653 db->pnBytesFreed = &nByte;
21654 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
21655 sqlite3VdbeClearObject(db, pVdbe);
21656 sqlite3DbFree(db, pVdbe);
21657 }
21658 db->pnBytesFreed = 0;
21659
21660 *pHighwater = 0; /* IMP: R-64479-57858 */
21661 *pCurrent = nByte;
21662
21663 break;
21664 }
21665
21666 /*
21667 ** Set *pCurrent to the total cache hits or misses encountered by all
21668 ** pagers the database handle is connected to. *pHighwater is always set
21669 ** to zero.
21670 */
21671 case SQLITE_DBSTATUS_CACHE_SPILL:
21672 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
21673 /* no break */ deliberate_fall_through
21674 case SQLITE_DBSTATUS_CACHE_HIT:
21675 case SQLITE_DBSTATUS_CACHE_MISS:
21676 case SQLITE_DBSTATUS_CACHE_WRITE:{
21677 int i;
21678 int nRet = 0;
21679 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
21680 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
21681
21682 for(i=0; i<db->nDb; i++){
21683 if( db->aDb[i].pBt ){
21684 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
21685 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
21686 }
21687 }
21688 *pHighwater = 0; /* IMP: R-42420-56072 */
21689 /* IMP: R-54100-20147 */
21690 /* IMP: R-29431-39229 */
21691 *pCurrent = nRet;
21692 break;
21693 }
21694
21695 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
21696 ** key constraints. Set *pCurrent to zero if all foreign key constraints
21697 ** have been satisfied. The *pHighwater is always set to zero.
21698 */
21699 case SQLITE_DBSTATUS_DEFERRED_FKS: {
21700 *pHighwater = 0; /* IMP: R-11967-56545 */
21701 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
21702 break;
21703 }
21704
21705 default: {
21706 rc = SQLITE_ERROR;
21707 }
21708 }
21709 sqlite3_mutex_leave(db->mutex);
21710 return rc;
21711}
21712
21713/************** End of status.c **********************************************/
21714/************** Begin file date.c ********************************************/
21715/*
21716** 2003 October 31
21717**
21718** The author disclaims copyright to this source code. In place of
21719** a legal notice, here is a blessing:
21720**
21721** May you do good and not evil.
21722** May you find forgiveness for yourself and forgive others.
21723** May you share freely, never taking more than you give.
21724**
21725*************************************************************************
21726** This file contains the C functions that implement date and time
21727** functions for SQLite.
21728**
21729** There is only one exported symbol in this file - the function
21730** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
21731** All other code has file scope.
21732**
21733** SQLite processes all times and dates as julian day numbers. The
21734** dates and times are stored as the number of days since noon
21735** in Greenwich on November 24, 4714 B.C. according to the Gregorian
21736** calendar system.
21737**
21738** 1970-01-01 00:00:00 is JD 2440587.5
21739** 2000-01-01 00:00:00 is JD 2451544.5
21740**
21741** This implementation requires years to be expressed as a 4-digit number
21742** which means that only dates between 0000-01-01 and 9999-12-31 can
21743** be represented, even though julian day numbers allow a much wider
21744** range of dates.
21745**
21746** The Gregorian calendar system is used for all dates and times,
21747** even those that predate the Gregorian calendar. Historians usually
21748** use the julian calendar for dates prior to 1582-10-15 and for some
21749** dates afterwards, depending on locale. Beware of this difference.
21750**
21751** The conversion algorithms are implemented based on descriptions
21752** in the following text:
21753**
21754** Jean Meeus
21755** Astronomical Algorithms, 2nd Edition, 1998
21756** ISBN 0-943396-61-1
21757** Willmann-Bell, Inc
21758** Richmond, Virginia (USA)
21759*/
21760/* #include "sqliteInt.h" */
21761/* #include <stdlib.h> */
21762/* #include <assert.h> */
21763#include <time.h>
21764
21765#ifndef SQLITE_OMIT_DATETIME_FUNCS
21766
21767/*
21768** The MSVC CRT on Windows CE may not have a localtime() function.
21769** So declare a substitute. The substitute function itself is
21770** defined in "os_win.c".
21771*/
21772#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21773 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21774struct tm *__cdecl localtime(const time_t *);
21775#endif
21776
21777/*
21778** A structure for holding a single date and time.
21779*/
21780typedef struct DateTime DateTime;
21781struct DateTime {
21782 sqlite3_int64 iJD; /* The julian day number times 86400000 */
21783 int Y, M, D; /* Year, month, and day */
21784 int h, m; /* Hour and minutes */
21785 int tz; /* Timezone offset in minutes */
21786 double s; /* Seconds */
21787 char validJD; /* True (1) if iJD is valid */
21788 char rawS; /* Raw numeric value stored in s */
21789 char validYMD; /* True (1) if Y,M,D are valid */
21790 char validHMS; /* True (1) if h,m,s are valid */
21791 char validTZ; /* True (1) if tz is valid */
21792 char tzSet; /* Timezone was set explicitly */
21793 char isError; /* An overflow has occurred */
21794};
21795
21796
21797/*
21798** Convert zDate into one or more integers according to the conversion
21799** specifier zFormat.
21800**
21801** zFormat[] contains 4 characters for each integer converted, except for
21802** the last integer which is specified by three characters. The meaning
21803** of a four-character format specifiers ABCD is:
21804**
21805** A: number of digits to convert. Always "2" or "4".
21806** B: minimum value. Always "0" or "1".
21807** C: maximum value, decoded as:
21808** a: 12
21809** b: 14
21810** c: 24
21811** d: 31
21812** e: 59
21813** f: 9999
21814** D: the separator character, or \000 to indicate this is the
21815** last number to convert.
21816**
21817** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
21818** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
21819** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
21820** the 2-digit day which is the last integer in the set.
21821**
21822** The function returns the number of successful conversions.
21823*/
21824static int getDigits(const char *zDate, const char *zFormat, ...){
21825 /* The aMx[] array translates the 3rd character of each format
21826 ** spec into a max size: a b c d e f */
21827 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
21828 va_list ap;
21829 int cnt = 0;
21830 char nextC;
21831 va_start(ap, zFormat);
21832 do{
21833 char N = zFormat[0] - '0';
21834 char min = zFormat[1] - '0';
21835 int val = 0;
21836 u16 max;
21837
21838 assert( zFormat[2]>='a' && zFormat[2]<='f' );
21839 max = aMx[zFormat[2] - 'a'];
21840 nextC = zFormat[3];
21841 val = 0;
21842 while( N-- ){
21843 if( !sqlite3Isdigit(*zDate) ){
21844 goto end_getDigits;
21845 }
21846 val = val*10 + *zDate - '0';
21847 zDate++;
21848 }
21849 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
21850 goto end_getDigits;
21851 }
21852 *va_arg(ap,int*) = val;
21853 zDate++;
21854 cnt++;
21855 zFormat += 4;
21856 }while( nextC );
21857end_getDigits:
21858 va_end(ap);
21859 return cnt;
21860}
21861
21862/*
21863** Parse a timezone extension on the end of a date-time.
21864** The extension is of the form:
21865**
21866** (+/-)HH:MM
21867**
21868** Or the "zulu" notation:
21869**
21870** Z
21871**
21872** If the parse is successful, write the number of minutes
21873** of change in p->tz and return 0. If a parser error occurs,
21874** return non-zero.
21875**
21876** A missing specifier is not considered an error.
21877*/
21878static int parseTimezone(const char *zDate, DateTime *p){
21879 int sgn = 0;
21880 int nHr, nMn;
21881 int c;
21882 while( sqlite3Isspace(*zDate) ){ zDate++; }
21883 p->tz = 0;
21884 c = *zDate;
21885 if( c=='-' ){
21886 sgn = -1;
21887 }else if( c=='+' ){
21888 sgn = +1;
21889 }else if( c=='Z' || c=='z' ){
21890 zDate++;
21891 goto zulu_time;
21892 }else{
21893 return c!=0;
21894 }
21895 zDate++;
21896 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
21897 return 1;
21898 }
21899 zDate += 5;
21900 p->tz = sgn*(nMn + nHr*60);
21901zulu_time:
21902 while( sqlite3Isspace(*zDate) ){ zDate++; }
21903 p->tzSet = 1;
21904 return *zDate!=0;
21905}
21906
21907/*
21908** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
21909** The HH, MM, and SS must each be exactly 2 digits. The
21910** fractional seconds FFFF can be one or more digits.
21911**
21912** Return 1 if there is a parsing error and 0 on success.
21913*/
21914static int parseHhMmSs(const char *zDate, DateTime *p){
21915 int h, m, s;
21916 double ms = 0.0;
21917 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
21918 return 1;
21919 }
21920 zDate += 5;
21921 if( *zDate==':' ){
21922 zDate++;
21923 if( getDigits(zDate, "20e", &s)!=1 ){
21924 return 1;
21925 }
21926 zDate += 2;
21927 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
21928 double rScale = 1.0;
21929 zDate++;
21930 while( sqlite3Isdigit(*zDate) ){
21931 ms = ms*10.0 + *zDate - '0';
21932 rScale *= 10.0;
21933 zDate++;
21934 }
21935 ms /= rScale;
21936 }
21937 }else{
21938 s = 0;
21939 }
21940 p->validJD = 0;
21941 p->rawS = 0;
21942 p->validHMS = 1;
21943 p->h = h;
21944 p->m = m;
21945 p->s = s + ms;
21946 if( parseTimezone(zDate, p) ) return 1;
21947 p->validTZ = (p->tz!=0)?1:0;
21948 return 0;
21949}
21950
21951/*
21952** Put the DateTime object into its error state.
21953*/
21954static void datetimeError(DateTime *p){
21955 memset(p, 0, sizeof(*p));
21956 p->isError = 1;
21957}
21958
21959/*
21960** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
21961** that the YYYY-MM-DD is according to the Gregorian calendar.
21962**
21963** Reference: Meeus page 61
21964*/
21965static void computeJD(DateTime *p){
21966 int Y, M, D, A, B, X1, X2;
21967
21968 if( p->validJD ) return;
21969 if( p->validYMD ){
21970 Y = p->Y;
21971 M = p->M;
21972 D = p->D;
21973 }else{
21974 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
21975 M = 1;
21976 D = 1;
21977 }
21978 if( Y<-4713 || Y>9999 || p->rawS ){
21979 datetimeError(p);
21980 return;
21981 }
21982 if( M<=2 ){
21983 Y--;
21984 M += 12;
21985 }
21986 A = Y/100;
21987 B = 2 - A + (A/4);
21988 X1 = 36525*(Y+4716)/100;
21989 X2 = 306001*(M+1)/10000;
21990 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
21991 p->validJD = 1;
21992 if( p->validHMS ){
21993 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
21994 if( p->validTZ ){
21995 p->iJD -= p->tz*60000;
21996 p->validYMD = 0;
21997 p->validHMS = 0;
21998 p->validTZ = 0;
21999 }
22000 }
22001}
22002
22003/*
22004** Parse dates of the form
22005**
22006** YYYY-MM-DD HH:MM:SS.FFF
22007** YYYY-MM-DD HH:MM:SS
22008** YYYY-MM-DD HH:MM
22009** YYYY-MM-DD
22010**
22011** Write the result into the DateTime structure and return 0
22012** on success and 1 if the input string is not a well-formed
22013** date.
22014*/
22015static int parseYyyyMmDd(const char *zDate, DateTime *p){
22016 int Y, M, D, neg;
22017
22018 if( zDate[0]=='-' ){
22019 zDate++;
22020 neg = 1;
22021 }else{
22022 neg = 0;
22023 }
22024 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
22025 return 1;
22026 }
22027 zDate += 10;
22028 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
22029 if( parseHhMmSs(zDate, p)==0 ){
22030 /* We got the time */
22031 }else if( *zDate==0 ){
22032 p->validHMS = 0;
22033 }else{
22034 return 1;
22035 }
22036 p->validJD = 0;
22037 p->validYMD = 1;
22038 p->Y = neg ? -Y : Y;
22039 p->M = M;
22040 p->D = D;
22041 if( p->validTZ ){
22042 computeJD(p);
22043 }
22044 return 0;
22045}
22046
22047/*
22048** Set the time to the current time reported by the VFS.
22049**
22050** Return the number of errors.
22051*/
22052static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
22053 p->iJD = sqlite3StmtCurrentTime(context);
22054 if( p->iJD>0 ){
22055 p->validJD = 1;
22056 return 0;
22057 }else{
22058 return 1;
22059 }
22060}
22061
22062/*
22063** Input "r" is a numeric quantity which might be a julian day number,
22064** or the number of seconds since 1970. If the value if r is within
22065** range of a julian day number, install it as such and set validJD.
22066** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
22067*/
22068static void setRawDateNumber(DateTime *p, double r){
22069 p->s = r;
22070 p->rawS = 1;
22071 if( r>=0.0 && r<5373484.5 ){
22072 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
22073 p->validJD = 1;
22074 }
22075}
22076
22077/*
22078** Attempt to parse the given string into a julian day number. Return
22079** the number of errors.
22080**
22081** The following are acceptable forms for the input string:
22082**
22083** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
22084** DDDD.DD
22085** now
22086**
22087** In the first form, the +/-HH:MM is always optional. The fractional
22088** seconds extension (the ".FFF") is optional. The seconds portion
22089** (":SS.FFF") is option. The year and date can be omitted as long
22090** as there is a time string. The time string can be omitted as long
22091** as there is a year and date.
22092*/
22093static int parseDateOrTime(
22094 sqlite3_context *context,
22095 const char *zDate,
22096 DateTime *p
22097){
22098 double r;
22099 if( parseYyyyMmDd(zDate,p)==0 ){
22100 return 0;
22101 }else if( parseHhMmSs(zDate, p)==0 ){
22102 return 0;
22103 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
22104 return setDateTimeToCurrent(context, p);
22105 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){
22106 setRawDateNumber(p, r);
22107 return 0;
22108 }
22109 return 1;
22110}
22111
22112/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
22113** Multiplying this by 86400000 gives 464269060799999 as the maximum value
22114** for DateTime.iJD.
22115**
22116** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
22117** such a large integer literal, so we have to encode it.
22118*/
22119#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
22120
22121/*
22122** Return TRUE if the given julian day number is within range.
22123**
22124** The input is the JulianDay times 86400000.
22125*/
22126static int validJulianDay(sqlite3_int64 iJD){
22127 return iJD>=0 && iJD<=INT_464269060799999;
22128}
22129
22130/*
22131** Compute the Year, Month, and Day from the julian day number.
22132*/
22133static void computeYMD(DateTime *p){
22134 int Z, A, B, C, D, E, X1;
22135 if( p->validYMD ) return;
22136 if( !p->validJD ){
22137 p->Y = 2000;
22138 p->M = 1;
22139 p->D = 1;
22140 }else if( !validJulianDay(p->iJD) ){
22141 datetimeError(p);
22142 return;
22143 }else{
22144 Z = (int)((p->iJD + 43200000)/86400000);
22145 A = (int)((Z - 1867216.25)/36524.25);
22146 A = Z + 1 + A - (A/4);
22147 B = A + 1524;
22148 C = (int)((B - 122.1)/365.25);
22149 D = (36525*(C&32767))/100;
22150 E = (int)((B-D)/30.6001);
22151 X1 = (int)(30.6001*E);
22152 p->D = B - D - X1;
22153 p->M = E<14 ? E-1 : E-13;
22154 p->Y = p->M>2 ? C - 4716 : C - 4715;
22155 }
22156 p->validYMD = 1;
22157}
22158
22159/*
22160** Compute the Hour, Minute, and Seconds from the julian day number.
22161*/
22162static void computeHMS(DateTime *p){
22163 int s;
22164 if( p->validHMS ) return;
22165 computeJD(p);
22166 s = (int)((p->iJD + 43200000) % 86400000);
22167 p->s = s/1000.0;
22168 s = (int)p->s;
22169 p->s -= s;
22170 p->h = s/3600;
22171 s -= p->h*3600;
22172 p->m = s/60;
22173 p->s += s - p->m*60;
22174 p->rawS = 0;
22175 p->validHMS = 1;
22176}
22177
22178/*
22179** Compute both YMD and HMS
22180*/
22181static void computeYMD_HMS(DateTime *p){
22182 computeYMD(p);
22183 computeHMS(p);
22184}
22185
22186/*
22187** Clear the YMD and HMS and the TZ
22188*/
22189static void clearYMD_HMS_TZ(DateTime *p){
22190 p->validYMD = 0;
22191 p->validHMS = 0;
22192 p->validTZ = 0;
22193}
22194
22195#ifndef SQLITE_OMIT_LOCALTIME
22196/*
22197** On recent Windows platforms, the localtime_s() function is available
22198** as part of the "Secure CRT". It is essentially equivalent to
22199** localtime_r() available under most POSIX platforms, except that the
22200** order of the parameters is reversed.
22201**
22202** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
22203**
22204** If the user has not indicated to use localtime_r() or localtime_s()
22205** already, check for an MSVC build environment that provides
22206** localtime_s().
22207*/
22208#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
22209 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
22210#undef HAVE_LOCALTIME_S
22211#define HAVE_LOCALTIME_S 1
22212#endif
22213
22214/*
22215** The following routine implements the rough equivalent of localtime_r()
22216** using whatever operating-system specific localtime facility that
22217** is available. This routine returns 0 on success and
22218** non-zero on any kind of error.
22219**
22220** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
22221** routine will always fail.
22222**
22223** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
22224** library function localtime_r() is used to assist in the calculation of
22225** local time.
22226*/
22227static int osLocaltime(time_t *t, struct tm *pTm){
22228 int rc;
22229#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
22230 struct tm *pX;
22231#if SQLITE_THREADSAFE>0
22232 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
22233#endif
22234 sqlite3_mutex_enter(mutex);
22235 pX = localtime(t);
22236#ifndef SQLITE_UNTESTABLE
22237 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
22238#endif
22239 if( pX ) *pTm = *pX;
22240 sqlite3_mutex_leave(mutex);
22241 rc = pX==0;
22242#else
22243#ifndef SQLITE_UNTESTABLE
22244 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
22245#endif
22246#if HAVE_LOCALTIME_R
22247 rc = localtime_r(t, pTm)==0;
22248#else
22249 rc = localtime_s(pTm, t);
22250#endif /* HAVE_LOCALTIME_R */
22251#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
22252 return rc;
22253}
22254#endif /* SQLITE_OMIT_LOCALTIME */
22255
22256
22257#ifndef SQLITE_OMIT_LOCALTIME
22258/*
22259** Compute the difference (in milliseconds) between localtime and UTC
22260** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
22261** return this value and set *pRc to SQLITE_OK.
22262**
22263** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
22264** is undefined in this case.
22265*/
22266static sqlite3_int64 localtimeOffset(
22267 DateTime *p, /* Date at which to calculate offset */
22268 sqlite3_context *pCtx, /* Write error here if one occurs */
22269 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
22270){
22271 DateTime x, y;
22272 time_t t;
22273 struct tm sLocal;
22274
22275 /* Initialize the contents of sLocal to avoid a compiler warning. */
22276 memset(&sLocal, 0, sizeof(sLocal));
22277
22278 x = *p;
22279 computeYMD_HMS(&x);
22280 if( x.Y<1971 || x.Y>=2038 ){
22281 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
22282 ** works for years between 1970 and 2037. For dates outside this range,
22283 ** SQLite attempts to map the year into an equivalent year within this
22284 ** range, do the calculation, then map the year back.
22285 */
22286 x.Y = 2000;
22287 x.M = 1;
22288 x.D = 1;
22289 x.h = 0;
22290 x.m = 0;
22291 x.s = 0.0;
22292 } else {
22293 int s = (int)(x.s + 0.5);
22294 x.s = s;
22295 }
22296 x.tz = 0;
22297 x.validJD = 0;
22298 computeJD(&x);
22299 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
22300 if( osLocaltime(&t, &sLocal) ){
22301 sqlite3_result_error(pCtx, "local time unavailable", -1);
22302 *pRc = SQLITE_ERROR;
22303 return 0;
22304 }
22305 y.Y = sLocal.tm_year + 1900;
22306 y.M = sLocal.tm_mon + 1;
22307 y.D = sLocal.tm_mday;
22308 y.h = sLocal.tm_hour;
22309 y.m = sLocal.tm_min;
22310 y.s = sLocal.tm_sec;
22311 y.validYMD = 1;
22312 y.validHMS = 1;
22313 y.validJD = 0;
22314 y.rawS = 0;
22315 y.validTZ = 0;
22316 y.isError = 0;
22317 computeJD(&y);
22318 *pRc = SQLITE_OK;
22319 return y.iJD - x.iJD;
22320}
22321#endif /* SQLITE_OMIT_LOCALTIME */
22322
22323/*
22324** The following table defines various date transformations of the form
22325**
22326** 'NNN days'
22327**
22328** Where NNN is an arbitrary floating-point number and "days" can be one
22329** of several units of time.
22330*/
22331static const struct {
22332 u8 eType; /* Transformation type code */
22333 u8 nName; /* Length of th name */
22334 char *zName; /* Name of the transformation */
22335 double rLimit; /* Maximum NNN value for this transform */
22336 double rXform; /* Constant used for this transform */
22337} aXformType[] = {
22338 { 0, 6, "second", 464269060800.0, 1000.0 },
22339 { 0, 6, "minute", 7737817680.0, 60000.0 },
22340 { 0, 4, "hour", 128963628.0, 3600000.0 },
22341 { 0, 3, "day", 5373485.0, 86400000.0 },
22342 { 1, 5, "month", 176546.0, 2592000000.0 },
22343 { 2, 4, "year", 14713.0, 31536000000.0 },
22344};
22345
22346/*
22347** Process a modifier to a date-time stamp. The modifiers are
22348** as follows:
22349**
22350** NNN days
22351** NNN hours
22352** NNN minutes
22353** NNN.NNNN seconds
22354** NNN months
22355** NNN years
22356** start of month
22357** start of year
22358** start of week
22359** start of day
22360** weekday N
22361** unixepoch
22362** localtime
22363** utc
22364**
22365** Return 0 on success and 1 if there is any kind of error. If the error
22366** is in a system call (i.e. localtime()), then an error message is written
22367** to context pCtx. If the error is an unrecognized modifier, no error is
22368** written to pCtx.
22369*/
22370static int parseModifier(
22371 sqlite3_context *pCtx, /* Function context */
22372 const char *z, /* The text of the modifier */
22373 int n, /* Length of zMod in bytes */
22374 DateTime *p /* The date/time value to be modified */
22375){
22376 int rc = 1;
22377 double r;
22378 switch(sqlite3UpperToLower[(u8)z[0]] ){
22379#ifndef SQLITE_OMIT_LOCALTIME
22380 case 'l': {
22381 /* localtime
22382 **
22383 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
22384 ** show local time.
22385 */
22386 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
22387 computeJD(p);
22388 p->iJD += localtimeOffset(p, pCtx, &rc);
22389 clearYMD_HMS_TZ(p);
22390 }
22391 break;
22392 }
22393#endif
22394 case 'u': {
22395 /*
22396 ** unixepoch
22397 **
22398 ** Treat the current value of p->s as the number of
22399 ** seconds since 1970. Convert to a real julian day number.
22400 */
22401 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
22402 r = p->s*1000.0 + 210866760000000.0;
22403 if( r>=0.0 && r<464269060800000.0 ){
22404 clearYMD_HMS_TZ(p);
22405 p->iJD = (sqlite3_int64)(r + 0.5);
22406 p->validJD = 1;
22407 p->rawS = 0;
22408 rc = 0;
22409 }
22410 }
22411#ifndef SQLITE_OMIT_LOCALTIME
22412 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
22413 if( p->tzSet==0 ){
22414 sqlite3_int64 c1;
22415 computeJD(p);
22416 c1 = localtimeOffset(p, pCtx, &rc);
22417 if( rc==SQLITE_OK ){
22418 p->iJD -= c1;
22419 clearYMD_HMS_TZ(p);
22420 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
22421 }
22422 p->tzSet = 1;
22423 }else{
22424 rc = SQLITE_OK;
22425 }
22426 }
22427#endif
22428 break;
22429 }
22430 case 'w': {
22431 /*
22432 ** weekday N
22433 **
22434 ** Move the date to the same time on the next occurrence of
22435 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
22436 ** date is already on the appropriate weekday, this is a no-op.
22437 */
22438 if( sqlite3_strnicmp(z, "weekday ", 8)==0
22439 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0
22440 && (n=(int)r)==r && n>=0 && r<7 ){
22441 sqlite3_int64 Z;
22442 computeYMD_HMS(p);
22443 p->validTZ = 0;
22444 p->validJD = 0;
22445 computeJD(p);
22446 Z = ((p->iJD + 129600000)/86400000) % 7;
22447 if( Z>n ) Z -= 7;
22448 p->iJD += (n - Z)*86400000;
22449 clearYMD_HMS_TZ(p);
22450 rc = 0;
22451 }
22452 break;
22453 }
22454 case 's': {
22455 /*
22456 ** start of TTTTT
22457 **
22458 ** Move the date backwards to the beginning of the current day,
22459 ** or month or year.
22460 */
22461 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
22462 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
22463 z += 9;
22464 computeYMD(p);
22465 p->validHMS = 1;
22466 p->h = p->m = 0;
22467 p->s = 0.0;
22468 p->rawS = 0;
22469 p->validTZ = 0;
22470 p->validJD = 0;
22471 if( sqlite3_stricmp(z,"month")==0 ){
22472 p->D = 1;
22473 rc = 0;
22474 }else if( sqlite3_stricmp(z,"year")==0 ){
22475 p->M = 1;
22476 p->D = 1;
22477 rc = 0;
22478 }else if( sqlite3_stricmp(z,"day")==0 ){
22479 rc = 0;
22480 }
22481 break;
22482 }
22483 case '+':
22484 case '-':
22485 case '0':
22486 case '1':
22487 case '2':
22488 case '3':
22489 case '4':
22490 case '5':
22491 case '6':
22492 case '7':
22493 case '8':
22494 case '9': {
22495 double rRounder;
22496 int i;
22497 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
22498 if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){
22499 rc = 1;
22500 break;
22501 }
22502 if( z[n]==':' ){
22503 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
22504 ** specified number of hours, minutes, seconds, and fractional seconds
22505 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
22506 ** omitted.
22507 */
22508 const char *z2 = z;
22509 DateTime tx;
22510 sqlite3_int64 day;
22511 if( !sqlite3Isdigit(*z2) ) z2++;
22512 memset(&tx, 0, sizeof(tx));
22513 if( parseHhMmSs(z2, &tx) ) break;
22514 computeJD(&tx);
22515 tx.iJD -= 43200000;
22516 day = tx.iJD/86400000;
22517 tx.iJD -= day*86400000;
22518 if( z[0]=='-' ) tx.iJD = -tx.iJD;
22519 computeJD(p);
22520 clearYMD_HMS_TZ(p);
22521 p->iJD += tx.iJD;
22522 rc = 0;
22523 break;
22524 }
22525
22526 /* If control reaches this point, it means the transformation is
22527 ** one of the forms like "+NNN days". */
22528 z += n;
22529 while( sqlite3Isspace(*z) ) z++;
22530 n = sqlite3Strlen30(z);
22531 if( n>10 || n<3 ) break;
22532 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
22533 computeJD(p);
22534 rc = 1;
22535 rRounder = r<0 ? -0.5 : +0.5;
22536 for(i=0; i<ArraySize(aXformType); i++){
22537 if( aXformType[i].nName==n
22538 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
22539 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
22540 ){
22541 switch( aXformType[i].eType ){
22542 case 1: { /* Special processing to add months */
22543 int x;
22544 computeYMD_HMS(p);
22545 p->M += (int)r;
22546 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
22547 p->Y += x;
22548 p->M -= x*12;
22549 p->validJD = 0;
22550 r -= (int)r;
22551 break;
22552 }
22553 case 2: { /* Special processing to add years */
22554 int y = (int)r;
22555 computeYMD_HMS(p);
22556 p->Y += y;
22557 p->validJD = 0;
22558 r -= (int)r;
22559 break;
22560 }
22561 }
22562 computeJD(p);
22563 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
22564 rc = 0;
22565 break;
22566 }
22567 }
22568 clearYMD_HMS_TZ(p);
22569 break;
22570 }
22571 default: {
22572 break;
22573 }
22574 }
22575 return rc;
22576}
22577
22578/*
22579** Process time function arguments. argv[0] is a date-time stamp.
22580** argv[1] and following are modifiers. Parse them all and write
22581** the resulting time into the DateTime structure p. Return 0
22582** on success and 1 if there are any errors.
22583**
22584** If there are zero parameters (if even argv[0] is undefined)
22585** then assume a default value of "now" for argv[0].
22586*/
22587static int isDate(
22588 sqlite3_context *context,
22589 int argc,
22590 sqlite3_value **argv,
22591 DateTime *p
22592){
22593 int i, n;
22594 const unsigned char *z;
22595 int eType;
22596 memset(p, 0, sizeof(*p));
22597 if( argc==0 ){
22598 return setDateTimeToCurrent(context, p);
22599 }
22600 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
22601 || eType==SQLITE_INTEGER ){
22602 setRawDateNumber(p, sqlite3_value_double(argv[0]));
22603 }else{
22604 z = sqlite3_value_text(argv[0]);
22605 if( !z || parseDateOrTime(context, (char*)z, p) ){
22606 return 1;
22607 }
22608 }
22609 for(i=1; i<argc; i++){
22610 z = sqlite3_value_text(argv[i]);
22611 n = sqlite3_value_bytes(argv[i]);
22612 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
22613 }
22614 computeJD(p);
22615 if( p->isError || !validJulianDay(p->iJD) ) return 1;
22616 return 0;
22617}
22618
22619
22620/*
22621** The following routines implement the various date and time functions
22622** of SQLite.
22623*/
22624
22625/*
22626** julianday( TIMESTRING, MOD, MOD, ...)
22627**
22628** Return the julian day number of the date specified in the arguments
22629*/
22630static void juliandayFunc(
22631 sqlite3_context *context,
22632 int argc,
22633 sqlite3_value **argv
22634){
22635 DateTime x;
22636 if( isDate(context, argc, argv, &x)==0 ){
22637 computeJD(&x);
22638 sqlite3_result_double(context, x.iJD/86400000.0);
22639 }
22640}
22641
22642/*
22643** datetime( TIMESTRING, MOD, MOD, ...)
22644**
22645** Return YYYY-MM-DD HH:MM:SS
22646*/
22647static void datetimeFunc(
22648 sqlite3_context *context,
22649 int argc,
22650 sqlite3_value **argv
22651){
22652 DateTime x;
22653 if( isDate(context, argc, argv, &x)==0 ){
22654 char zBuf[100];
22655 computeYMD_HMS(&x);
22656 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
22657 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
22658 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22659 }
22660}
22661
22662/*
22663** time( TIMESTRING, MOD, MOD, ...)
22664**
22665** Return HH:MM:SS
22666*/
22667static void timeFunc(
22668 sqlite3_context *context,
22669 int argc,
22670 sqlite3_value **argv
22671){
22672 DateTime x;
22673 if( isDate(context, argc, argv, &x)==0 ){
22674 char zBuf[100];
22675 computeHMS(&x);
22676 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
22677 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22678 }
22679}
22680
22681/*
22682** date( TIMESTRING, MOD, MOD, ...)
22683**
22684** Return YYYY-MM-DD
22685*/
22686static void dateFunc(
22687 sqlite3_context *context,
22688 int argc,
22689 sqlite3_value **argv
22690){
22691 DateTime x;
22692 if( isDate(context, argc, argv, &x)==0 ){
22693 char zBuf[100];
22694 computeYMD(&x);
22695 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
22696 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22697 }
22698}
22699
22700/*
22701** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
22702**
22703** Return a string described by FORMAT. Conversions as follows:
22704**
22705** %d day of month
22706** %f ** fractional seconds SS.SSS
22707** %H hour 00-24
22708** %j day of year 000-366
22709** %J ** julian day number
22710** %m month 01-12
22711** %M minute 00-59
22712** %s seconds since 1970-01-01
22713** %S seconds 00-59
22714** %w day of week 0-6 sunday==0
22715** %W week of year 00-53
22716** %Y year 0000-9999
22717** %% %
22718*/
22719static void strftimeFunc(
22720 sqlite3_context *context,
22721 int argc,
22722 sqlite3_value **argv
22723){
22724 DateTime x;
22725 u64 n;
22726 size_t i,j;
22727 char *z;
22728 sqlite3 *db;
22729 const char *zFmt;
22730 char zBuf[100];
22731 if( argc==0 ) return;
22732 zFmt = (const char*)sqlite3_value_text(argv[0]);
22733 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
22734 db = sqlite3_context_db_handle(context);
22735 for(i=0, n=1; zFmt[i]; i++, n++){
22736 if( zFmt[i]=='%' ){
22737 switch( zFmt[i+1] ){
22738 case 'd':
22739 case 'H':
22740 case 'm':
22741 case 'M':
22742 case 'S':
22743 case 'W':
22744 n++;
22745 /* fall thru */
22746 case 'w':
22747 case '%':
22748 break;
22749 case 'f':
22750 n += 8;
22751 break;
22752 case 'j':
22753 n += 3;
22754 break;
22755 case 'Y':
22756 n += 8;
22757 break;
22758 case 's':
22759 case 'J':
22760 n += 50;
22761 break;
22762 default:
22763 return; /* ERROR. return a NULL */
22764 }
22765 i++;
22766 }
22767 }
22768 testcase( n==sizeof(zBuf)-1 );
22769 testcase( n==sizeof(zBuf) );
22770 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
22771 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
22772 if( n<sizeof(zBuf) ){
22773 z = zBuf;
22774 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
22775 sqlite3_result_error_toobig(context);
22776 return;
22777 }else{
22778 z = sqlite3DbMallocRawNN(db, (int)n);
22779 if( z==0 ){
22780 sqlite3_result_error_nomem(context);
22781 return;
22782 }
22783 }
22784 computeJD(&x);
22785 computeYMD_HMS(&x);
22786 for(i=j=0; zFmt[i]; i++){
22787 if( zFmt[i]!='%' ){
22788 z[j++] = zFmt[i];
22789 }else{
22790 i++;
22791 switch( zFmt[i] ){
22792 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
22793 case 'f': {
22794 double s = x.s;
22795 if( s>59.999 ) s = 59.999;
22796 sqlite3_snprintf(7, &z[j],"%06.3f", s);
22797 j += sqlite3Strlen30(&z[j]);
22798 break;
22799 }
22800 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
22801 case 'W': /* Fall thru */
22802 case 'j': {
22803 int nDay; /* Number of days since 1st day of year */
22804 DateTime y = x;
22805 y.validJD = 0;
22806 y.M = 1;
22807 y.D = 1;
22808 computeJD(&y);
22809 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
22810 if( zFmt[i]=='W' ){
22811 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
22812 wd = (int)(((x.iJD+43200000)/86400000)%7);
22813 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
22814 j += 2;
22815 }else{
22816 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
22817 j += 3;
22818 }
22819 break;
22820 }
22821 case 'J': {
22822 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
22823 j+=sqlite3Strlen30(&z[j]);
22824 break;
22825 }
22826 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
22827 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
22828 case 's': {
22829 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
22830 sqlite3Int64ToText(iS, &z[j]);
22831 j += sqlite3Strlen30(&z[j]);
22832 break;
22833 }
22834 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
22835 case 'w': {
22836 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
22837 break;
22838 }
22839 case 'Y': {
22840 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
22841 break;
22842 }
22843 default: z[j++] = '%'; break;
22844 }
22845 }
22846 }
22847 z[j] = 0;
22848 sqlite3_result_text(context, z, -1,
22849 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
22850}
22851
22852/*
22853** current_time()
22854**
22855** This function returns the same value as time('now').
22856*/
22857static void ctimeFunc(
22858 sqlite3_context *context,
22859 int NotUsed,
22860 sqlite3_value **NotUsed2
22861){
22862 UNUSED_PARAMETER2(NotUsed, NotUsed2);
22863 timeFunc(context, 0, 0);
22864}
22865
22866/*
22867** current_date()
22868**
22869** This function returns the same value as date('now').
22870*/
22871static void cdateFunc(
22872 sqlite3_context *context,
22873 int NotUsed,
22874 sqlite3_value **NotUsed2
22875){
22876 UNUSED_PARAMETER2(NotUsed, NotUsed2);
22877 dateFunc(context, 0, 0);
22878}
22879
22880/*
22881** current_timestamp()
22882**
22883** This function returns the same value as datetime('now').
22884*/
22885static void ctimestampFunc(
22886 sqlite3_context *context,
22887 int NotUsed,
22888 sqlite3_value **NotUsed2
22889){
22890 UNUSED_PARAMETER2(NotUsed, NotUsed2);
22891 datetimeFunc(context, 0, 0);
22892}
22893#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
22894
22895#ifdef SQLITE_OMIT_DATETIME_FUNCS
22896/*
22897** If the library is compiled to omit the full-scale date and time
22898** handling (to get a smaller binary), the following minimal version
22899** of the functions current_time(), current_date() and current_timestamp()
22900** are included instead. This is to support column declarations that
22901** include "DEFAULT CURRENT_TIME" etc.
22902**
22903** This function uses the C-library functions time(), gmtime()
22904** and strftime(). The format string to pass to strftime() is supplied
22905** as the user-data for the function.
22906*/
22907static void currentTimeFunc(
22908 sqlite3_context *context,
22909 int argc,
22910 sqlite3_value **argv
22911){
22912 time_t t;
22913 char *zFormat = (char *)sqlite3_user_data(context);
22914 sqlite3_int64 iT;
22915 struct tm *pTm;
22916 struct tm sNow;
22917 char zBuf[20];
22918
22919 UNUSED_PARAMETER(argc);
22920 UNUSED_PARAMETER(argv);
22921
22922 iT = sqlite3StmtCurrentTime(context);
22923 if( iT<=0 ) return;
22924 t = iT/1000 - 10000*(sqlite3_int64)21086676;
22925#if HAVE_GMTIME_R
22926 pTm = gmtime_r(&t, &sNow);
22927#else
22928 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
22929 pTm = gmtime(&t);
22930 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
22931 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
22932#endif
22933 if( pTm ){
22934 strftime(zBuf, 20, zFormat, &sNow);
22935 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
22936 }
22937}
22938#endif
22939
22940/*
22941** This function registered all of the above C functions as SQL
22942** functions. This should be the only routine in this file with
22943** external linkage.
22944*/
22945SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
22946 static FuncDef aDateTimeFuncs[] = {
22947#ifndef SQLITE_OMIT_DATETIME_FUNCS
22948 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
22949 PURE_DATE(date, -1, 0, 0, dateFunc ),
22950 PURE_DATE(time, -1, 0, 0, timeFunc ),
22951 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
22952 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
22953 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
22954 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
22955 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
22956#else
22957 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
22958 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
22959 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
22960#endif
22961 };
22962 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
22963}
22964
22965/************** End of date.c ************************************************/
22966/************** Begin file os.c **********************************************/
22967/*
22968** 2005 November 29
22969**
22970** The author disclaims copyright to this source code. In place of
22971** a legal notice, here is a blessing:
22972**
22973** May you do good and not evil.
22974** May you find forgiveness for yourself and forgive others.
22975** May you share freely, never taking more than you give.
22976**
22977******************************************************************************
22978**
22979** This file contains OS interface code that is common to all
22980** architectures.
22981*/
22982/* #include "sqliteInt.h" */
22983
22984/*
22985** If we compile with the SQLITE_TEST macro set, then the following block
22986** of code will give us the ability to simulate a disk I/O error. This
22987** is used for testing the I/O recovery logic.
22988*/
22989#if defined(SQLITE_TEST)
22990SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
22991SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
22992SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
22993SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
22994SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
22995SQLITE_API int sqlite3_diskfull_pending = 0;
22996SQLITE_API int sqlite3_diskfull = 0;
22997#endif /* defined(SQLITE_TEST) */
22998
22999/*
23000** When testing, also keep a count of the number of open files.
23001*/
23002#if defined(SQLITE_TEST)
23003SQLITE_API int sqlite3_open_file_count = 0;
23004#endif /* defined(SQLITE_TEST) */
23005
23006/*
23007** The default SQLite sqlite3_vfs implementations do not allocate
23008** memory (actually, os_unix.c allocates a small amount of memory
23009** from within OsOpen()), but some third-party implementations may.
23010** So we test the effects of a malloc() failing and the sqlite3OsXXX()
23011** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
23012**
23013** The following functions are instrumented for malloc() failure
23014** testing:
23015**
23016** sqlite3OsRead()
23017** sqlite3OsWrite()
23018** sqlite3OsSync()
23019** sqlite3OsFileSize()
23020** sqlite3OsLock()
23021** sqlite3OsCheckReservedLock()
23022** sqlite3OsFileControl()
23023** sqlite3OsShmMap()
23024** sqlite3OsOpen()
23025** sqlite3OsDelete()
23026** sqlite3OsAccess()
23027** sqlite3OsFullPathname()
23028**
23029*/
23030#if defined(SQLITE_TEST)
23031SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
23032 #define DO_OS_MALLOC_TEST(x) \
23033 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
23034 void *pTstAlloc = sqlite3Malloc(10); \
23035 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
23036 sqlite3_free(pTstAlloc); \
23037 }
23038#else
23039 #define DO_OS_MALLOC_TEST(x)
23040#endif
23041
23042/*
23043** The following routines are convenience wrappers around methods
23044** of the sqlite3_file object. This is mostly just syntactic sugar. All
23045** of this would be completely automatic if SQLite were coded using
23046** C++ instead of plain old C.
23047*/
23048SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
23049 if( pId->pMethods ){
23050 pId->pMethods->xClose(pId);
23051 pId->pMethods = 0;
23052 }
23053}
23054SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
23055 DO_OS_MALLOC_TEST(id);
23056 return id->pMethods->xRead(id, pBuf, amt, offset);
23057}
23058SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
23059 DO_OS_MALLOC_TEST(id);
23060 return id->pMethods->xWrite(id, pBuf, amt, offset);
23061}
23062SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
23063 return id->pMethods->xTruncate(id, size);
23064}
23065SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
23066 DO_OS_MALLOC_TEST(id);
23067 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
23068}
23069SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
23070 DO_OS_MALLOC_TEST(id);
23071 return id->pMethods->xFileSize(id, pSize);
23072}
23073SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
23074 DO_OS_MALLOC_TEST(id);
23075 return id->pMethods->xLock(id, lockType);
23076}
23077SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
23078 return id->pMethods->xUnlock(id, lockType);
23079}
23080SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
23081 DO_OS_MALLOC_TEST(id);
23082 return id->pMethods->xCheckReservedLock(id, pResOut);
23083}
23084
23085/*
23086** Use sqlite3OsFileControl() when we are doing something that might fail
23087** and we need to know about the failures. Use sqlite3OsFileControlHint()
23088** when simply tossing information over the wall to the VFS and we do not
23089** really care if the VFS receives and understands the information since it
23090** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
23091** routine has no return value since the return value would be meaningless.
23092*/
23093SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
23094 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
23095#ifdef SQLITE_TEST
23096 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
23097 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
23098 ){
23099 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
23100 ** is using a regular VFS, it is called after the corresponding
23101 ** transaction has been committed. Injecting a fault at this point
23102 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
23103 ** but the transaction is committed anyway.
23104 **
23105 ** The core must call OsFileControl() though, not OsFileControlHint(),
23106 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
23107 ** means the commit really has failed and an error should be returned
23108 ** to the user. */
23109 DO_OS_MALLOC_TEST(id);
23110 }
23111#endif
23112 return id->pMethods->xFileControl(id, op, pArg);
23113}
23114SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
23115 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
23116}
23117
23118SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
23119 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
23120 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
23121}
23122SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
23123 return id->pMethods->xDeviceCharacteristics(id);
23124}
23125#ifndef SQLITE_OMIT_WAL
23126SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
23127 return id->pMethods->xShmLock(id, offset, n, flags);
23128}
23129SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
23130 id->pMethods->xShmBarrier(id);
23131}
23132SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
23133 return id->pMethods->xShmUnmap(id, deleteFlag);
23134}
23135SQLITE_PRIVATE int sqlite3OsShmMap(
23136 sqlite3_file *id, /* Database file handle */
23137 int iPage,
23138 int pgsz,
23139 int bExtend, /* True to extend file if necessary */
23140 void volatile **pp /* OUT: Pointer to mapping */
23141){
23142 DO_OS_MALLOC_TEST(id);
23143 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
23144}
23145#endif /* SQLITE_OMIT_WAL */
23146
23147#if SQLITE_MAX_MMAP_SIZE>0
23148/* The real implementation of xFetch and xUnfetch */
23149SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
23150 DO_OS_MALLOC_TEST(id);
23151 return id->pMethods->xFetch(id, iOff, iAmt, pp);
23152}
23153SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
23154 return id->pMethods->xUnfetch(id, iOff, p);
23155}
23156#else
23157/* No-op stubs to use when memory-mapped I/O is disabled */
23158SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
23159 *pp = 0;
23160 return SQLITE_OK;
23161}
23162SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
23163 return SQLITE_OK;
23164}
23165#endif
23166
23167/*
23168** The next group of routines are convenience wrappers around the
23169** VFS methods.
23170*/
23171SQLITE_PRIVATE int sqlite3OsOpen(
23172 sqlite3_vfs *pVfs,
23173 const char *zPath,
23174 sqlite3_file *pFile,
23175 int flags,
23176 int *pFlagsOut
23177){
23178 int rc;
23179 DO_OS_MALLOC_TEST(0);
23180 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
23181 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
23182 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
23183 ** reaching the VFS. */
23184 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
23185 assert( rc==SQLITE_OK || pFile->pMethods==0 );
23186 return rc;
23187}
23188SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
23189 DO_OS_MALLOC_TEST(0);
23190 assert( dirSync==0 || dirSync==1 );
23191 return pVfs->xDelete(pVfs, zPath, dirSync);
23192}
23193SQLITE_PRIVATE int sqlite3OsAccess(
23194 sqlite3_vfs *pVfs,
23195 const char *zPath,
23196 int flags,
23197 int *pResOut
23198){
23199 DO_OS_MALLOC_TEST(0);
23200 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
23201}
23202SQLITE_PRIVATE int sqlite3OsFullPathname(
23203 sqlite3_vfs *pVfs,
23204 const char *zPath,
23205 int nPathOut,
23206 char *zPathOut
23207){
23208 DO_OS_MALLOC_TEST(0);
23209 zPathOut[0] = 0;
23210 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
23211}
23212#ifndef SQLITE_OMIT_LOAD_EXTENSION
23213SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
23214 return pVfs->xDlOpen(pVfs, zPath);
23215}
23216SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
23217 pVfs->xDlError(pVfs, nByte, zBufOut);
23218}
23219SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
23220 return pVfs->xDlSym(pVfs, pHdle, zSym);
23221}
23222SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
23223 pVfs->xDlClose(pVfs, pHandle);
23224}
23225#endif /* SQLITE_OMIT_LOAD_EXTENSION */
23226SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
23227 if( sqlite3Config.iPrngSeed ){
23228 memset(zBufOut, 0, nByte);
23229 if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
23230 memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
23231 return SQLITE_OK;
23232 }else{
23233 return pVfs->xRandomness(pVfs, nByte, zBufOut);
23234 }
23235
23236}
23237SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
23238 return pVfs->xSleep(pVfs, nMicro);
23239}
23240SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
23241 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
23242}
23243SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
23244 int rc;
23245 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
23246 ** method to get the current date and time if that method is available
23247 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
23248 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
23249 ** unavailable.
23250 */
23251 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
23252 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
23253 }else{
23254 double r;
23255 rc = pVfs->xCurrentTime(pVfs, &r);
23256 *pTimeOut = (sqlite3_int64)(r*86400000.0);
23257 }
23258 return rc;
23259}
23260
23261SQLITE_PRIVATE int sqlite3OsOpenMalloc(
23262 sqlite3_vfs *pVfs,
23263 const char *zFile,
23264 sqlite3_file **ppFile,
23265 int flags,
23266 int *pOutFlags
23267){
23268 int rc;
23269 sqlite3_file *pFile;
23270 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
23271 if( pFile ){
23272 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
23273 if( rc!=SQLITE_OK ){
23274 sqlite3_free(pFile);
23275 }else{
23276 *ppFile = pFile;
23277 }
23278 }else{
23279 rc = SQLITE_NOMEM_BKPT;
23280 }
23281 return rc;
23282}
23283SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
23284 assert( pFile );
23285 sqlite3OsClose(pFile);
23286 sqlite3_free(pFile);
23287}
23288
23289/*
23290** This function is a wrapper around the OS specific implementation of
23291** sqlite3_os_init(). The purpose of the wrapper is to provide the
23292** ability to simulate a malloc failure, so that the handling of an
23293** error in sqlite3_os_init() by the upper layers can be tested.
23294*/
23295SQLITE_PRIVATE int sqlite3OsInit(void){
23296 void *p = sqlite3_malloc(10);
23297 if( p==0 ) return SQLITE_NOMEM_BKPT;
23298 sqlite3_free(p);
23299 return sqlite3_os_init();
23300}
23301
23302/*
23303** The list of all registered VFS implementations.
23304*/
23305static sqlite3_vfs * SQLITE_WSD vfsList = 0;
23306#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
23307
23308/*
23309** Locate a VFS by name. If no name is given, simply return the
23310** first VFS on the list.
23311*/
23312SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
23313 sqlite3_vfs *pVfs = 0;
23314#if SQLITE_THREADSAFE
23315 sqlite3_mutex *mutex;
23316#endif
23317#ifndef SQLITE_OMIT_AUTOINIT
23318 int rc = sqlite3_initialize();
23319 if( rc ) return 0;
23320#endif
23321#if SQLITE_THREADSAFE
23322 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
23323#endif
23324 sqlite3_mutex_enter(mutex);
23325 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
23326 if( zVfs==0 ) break;
23327 if( strcmp(zVfs, pVfs->zName)==0 ) break;
23328 }
23329 sqlite3_mutex_leave(mutex);
23330 return pVfs;
23331}
23332
23333/*
23334** Unlink a VFS from the linked list
23335*/
23336static void vfsUnlink(sqlite3_vfs *pVfs){
23337 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) );
23338 if( pVfs==0 ){
23339 /* No-op */
23340 }else if( vfsList==pVfs ){
23341 vfsList = pVfs->pNext;
23342 }else if( vfsList ){
23343 sqlite3_vfs *p = vfsList;
23344 while( p->pNext && p->pNext!=pVfs ){
23345 p = p->pNext;
23346 }
23347 if( p->pNext==pVfs ){
23348 p->pNext = pVfs->pNext;
23349 }
23350 }
23351}
23352
23353/*
23354** Register a VFS with the system. It is harmless to register the same
23355** VFS multiple times. The new VFS becomes the default if makeDflt is
23356** true.
23357*/
23358SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
23359 MUTEX_LOGIC(sqlite3_mutex *mutex;)
23360#ifndef SQLITE_OMIT_AUTOINIT
23361 int rc = sqlite3_initialize();
23362 if( rc ) return rc;
23363#endif
23364#ifdef SQLITE_ENABLE_API_ARMOR
23365 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
23366#endif
23367
23368 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
23369 sqlite3_mutex_enter(mutex);
23370 vfsUnlink(pVfs);
23371 if( makeDflt || vfsList==0 ){
23372 pVfs->pNext = vfsList;
23373 vfsList = pVfs;
23374 }else{
23375 pVfs->pNext = vfsList->pNext;
23376 vfsList->pNext = pVfs;
23377 }
23378 assert(vfsList);
23379 sqlite3_mutex_leave(mutex);
23380 return SQLITE_OK;
23381}
23382
23383/*
23384** Unregister a VFS so that it is no longer accessible.
23385*/
23386SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
23387 MUTEX_LOGIC(sqlite3_mutex *mutex;)
23388#ifndef SQLITE_OMIT_AUTOINIT
23389 int rc = sqlite3_initialize();
23390 if( rc ) return rc;
23391#endif
23392 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
23393 sqlite3_mutex_enter(mutex);
23394 vfsUnlink(pVfs);
23395 sqlite3_mutex_leave(mutex);
23396 return SQLITE_OK;
23397}
23398
23399/************** End of os.c **************************************************/
23400/************** Begin file fault.c *******************************************/
23401/*
23402** 2008 Jan 22
23403**
23404** The author disclaims copyright to this source code. In place of
23405** a legal notice, here is a blessing:
23406**
23407** May you do good and not evil.
23408** May you find forgiveness for yourself and forgive others.
23409** May you share freely, never taking more than you give.
23410**
23411*************************************************************************
23412**
23413** This file contains code to support the concept of "benign"
23414** malloc failures (when the xMalloc() or xRealloc() method of the
23415** sqlite3_mem_methods structure fails to allocate a block of memory
23416** and returns 0).
23417**
23418** Most malloc failures are non-benign. After they occur, SQLite
23419** abandons the current operation and returns an error code (usually
23420** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
23421** fatal. For example, if a malloc fails while resizing a hash table, this
23422** is completely recoverable simply by not carrying out the resize. The
23423** hash table will continue to function normally. So a malloc failure
23424** during a hash table resize is a benign fault.
23425*/
23426
23427/* #include "sqliteInt.h" */
23428
23429#ifndef SQLITE_UNTESTABLE
23430
23431/*
23432** Global variables.
23433*/
23434typedef struct BenignMallocHooks BenignMallocHooks;
23435static SQLITE_WSD struct BenignMallocHooks {
23436 void (*xBenignBegin)(void);
23437 void (*xBenignEnd)(void);
23438} sqlite3Hooks = { 0, 0 };
23439
23440/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
23441** structure. If writable static data is unsupported on the target,
23442** we have to locate the state vector at run-time. In the more common
23443** case where writable static data is supported, wsdHooks can refer directly
23444** to the "sqlite3Hooks" state vector declared above.
23445*/
23446#ifdef SQLITE_OMIT_WSD
23447# define wsdHooksInit \
23448 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
23449# define wsdHooks x[0]
23450#else
23451# define wsdHooksInit
23452# define wsdHooks sqlite3Hooks
23453#endif
23454
23455
23456/*
23457** Register hooks to call when sqlite3BeginBenignMalloc() and
23458** sqlite3EndBenignMalloc() are called, respectively.
23459*/
23460SQLITE_PRIVATE void sqlite3BenignMallocHooks(
23461 void (*xBenignBegin)(void),
23462 void (*xBenignEnd)(void)
23463){
23464 wsdHooksInit;
23465 wsdHooks.xBenignBegin = xBenignBegin;
23466 wsdHooks.xBenignEnd = xBenignEnd;
23467}
23468
23469/*
23470** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
23471** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
23472** indicates that subsequent malloc failures are non-benign.
23473*/
23474SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
23475 wsdHooksInit;
23476 if( wsdHooks.xBenignBegin ){
23477 wsdHooks.xBenignBegin();
23478 }
23479}
23480SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
23481 wsdHooksInit;
23482 if( wsdHooks.xBenignEnd ){
23483 wsdHooks.xBenignEnd();
23484 }
23485}
23486
23487#endif /* #ifndef SQLITE_UNTESTABLE */
23488
23489/************** End of fault.c ***********************************************/
23490/************** Begin file mem0.c ********************************************/
23491/*
23492** 2008 October 28
23493**
23494** The author disclaims copyright to this source code. In place of
23495** a legal notice, here is a blessing:
23496**
23497** May you do good and not evil.
23498** May you find forgiveness for yourself and forgive others.
23499** May you share freely, never taking more than you give.
23500**
23501*************************************************************************
23502**
23503** This file contains a no-op memory allocation drivers for use when
23504** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
23505** here always fail. SQLite will not operate with these drivers. These
23506** are merely placeholders. Real drivers must be substituted using
23507** sqlite3_config() before SQLite will operate.
23508*/
23509/* #include "sqliteInt.h" */
23510
23511/*
23512** This version of the memory allocator is the default. It is
23513** used when no other memory allocator is specified using compile-time
23514** macros.
23515*/
23516#ifdef SQLITE_ZERO_MALLOC
23517
23518/*
23519** No-op versions of all memory allocation routines
23520*/
23521static void *sqlite3MemMalloc(int nByte){ return 0; }
23522static void sqlite3MemFree(void *pPrior){ return; }
23523static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
23524static int sqlite3MemSize(void *pPrior){ return 0; }
23525static int sqlite3MemRoundup(int n){ return n; }
23526static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
23527static void sqlite3MemShutdown(void *NotUsed){ return; }
23528
23529/*
23530** This routine is the only routine in this file with external linkage.
23531**
23532** Populate the low-level memory allocation function pointers in
23533** sqlite3GlobalConfig.m with pointers to the routines in this file.
23534*/
23535SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23536 static const sqlite3_mem_methods defaultMethods = {
23537 sqlite3MemMalloc,
23538 sqlite3MemFree,
23539 sqlite3MemRealloc,
23540 sqlite3MemSize,
23541 sqlite3MemRoundup,
23542 sqlite3MemInit,
23543 sqlite3MemShutdown,
23544 0
23545 };
23546 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23547}
23548
23549#endif /* SQLITE_ZERO_MALLOC */
23550
23551/************** End of mem0.c ************************************************/
23552/************** Begin file mem1.c ********************************************/
23553/*
23554** 2007 August 14
23555**
23556** The author disclaims copyright to this source code. In place of
23557** a legal notice, here is a blessing:
23558**
23559** May you do good and not evil.
23560** May you find forgiveness for yourself and forgive others.
23561** May you share freely, never taking more than you give.
23562**
23563*************************************************************************
23564**
23565** This file contains low-level memory allocation drivers for when
23566** SQLite will use the standard C-library malloc/realloc/free interface
23567** to obtain the memory it needs.
23568**
23569** This file contains implementations of the low-level memory allocation
23570** routines specified in the sqlite3_mem_methods object. The content of
23571** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
23572** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
23573** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
23574** default configuration is to use memory allocation routines in this
23575** file.
23576**
23577** C-preprocessor macro summary:
23578**
23579** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
23580** the malloc_usable_size() interface exists
23581** on the target platform. Or, this symbol
23582** can be set manually, if desired.
23583** If an equivalent interface exists by
23584** a different name, using a separate -D
23585** option to rename it.
23586**
23587** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
23588** memory allocator. Set this symbol to enable
23589** building on older macs.
23590**
23591** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
23592** _msize() on windows systems. This might
23593** be necessary when compiling for Delphi,
23594** for example.
23595*/
23596/* #include "sqliteInt.h" */
23597
23598/*
23599** This version of the memory allocator is the default. It is
23600** used when no other memory allocator is specified using compile-time
23601** macros.
23602*/
23603#ifdef SQLITE_SYSTEM_MALLOC
23604#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
23605
23606/*
23607** Use the zone allocator available on apple products unless the
23608** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
23609*/
23610#include <sys/sysctl.h>
23611#include <malloc/malloc.h>
23612#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
23613#include <libkern/OSAtomic.h>
23614#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
23615static malloc_zone_t* _sqliteZone_;
23616#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
23617#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
23618#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
23619#define SQLITE_MALLOCSIZE(x) \
23620 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
23621
23622#else /* if not __APPLE__ */
23623
23624/*
23625** Use standard C library malloc and free on non-Apple systems.
23626** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
23627*/
23628#define SQLITE_MALLOC(x) malloc(x)
23629#define SQLITE_FREE(x) free(x)
23630#define SQLITE_REALLOC(x,y) realloc((x),(y))
23631
23632/*
23633** The malloc.h header file is needed for malloc_usable_size() function
23634** on some systems (e.g. Linux).
23635*/
23636#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
23637# define SQLITE_USE_MALLOC_H 1
23638# define SQLITE_USE_MALLOC_USABLE_SIZE 1
23639/*
23640** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
23641** use of _msize() is automatic, but can be disabled by compiling with
23642** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
23643** the malloc.h header file.
23644*/
23645#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
23646# define SQLITE_USE_MALLOC_H
23647# define SQLITE_USE_MSIZE
23648#endif
23649
23650/*
23651** Include the malloc.h header file, if necessary. Also set define macro
23652** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
23653** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
23654** The memory size function can always be overridden manually by defining
23655** the macro SQLITE_MALLOCSIZE to the desired function name.
23656*/
23657#if defined(SQLITE_USE_MALLOC_H)
23658# include <malloc.h>
23659# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
23660# if !defined(SQLITE_MALLOCSIZE)
23661# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
23662# endif
23663# elif defined(SQLITE_USE_MSIZE)
23664# if !defined(SQLITE_MALLOCSIZE)
23665# define SQLITE_MALLOCSIZE _msize
23666# endif
23667# endif
23668#endif /* defined(SQLITE_USE_MALLOC_H) */
23669
23670#endif /* __APPLE__ or not __APPLE__ */
23671
23672/*
23673** Like malloc(), but remember the size of the allocation
23674** so that we can find it later using sqlite3MemSize().
23675**
23676** For this low-level routine, we are guaranteed that nByte>0 because
23677** cases of nByte<=0 will be intercepted and dealt with by higher level
23678** routines.
23679*/
23680static void *sqlite3MemMalloc(int nByte){
23681#ifdef SQLITE_MALLOCSIZE
23682 void *p;
23683 testcase( ROUND8(nByte)==nByte );
23684 p = SQLITE_MALLOC( nByte );
23685 if( p==0 ){
23686 testcase( sqlite3GlobalConfig.xLog!=0 );
23687 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
23688 }
23689 return p;
23690#else
23691 sqlite3_int64 *p;
23692 assert( nByte>0 );
23693 testcase( ROUND8(nByte)!=nByte );
23694 p = SQLITE_MALLOC( nByte+8 );
23695 if( p ){
23696 p[0] = nByte;
23697 p++;
23698 }else{
23699 testcase( sqlite3GlobalConfig.xLog!=0 );
23700 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
23701 }
23702 return (void *)p;
23703#endif
23704}
23705
23706/*
23707** Like free() but works for allocations obtained from sqlite3MemMalloc()
23708** or sqlite3MemRealloc().
23709**
23710** For this low-level routine, we already know that pPrior!=0 since
23711** cases where pPrior==0 will have been intecepted and dealt with
23712** by higher-level routines.
23713*/
23714static void sqlite3MemFree(void *pPrior){
23715#ifdef SQLITE_MALLOCSIZE
23716 SQLITE_FREE(pPrior);
23717#else
23718 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
23719 assert( pPrior!=0 );
23720 p--;
23721 SQLITE_FREE(p);
23722#endif
23723}
23724
23725/*
23726** Report the allocated size of a prior return from xMalloc()
23727** or xRealloc().
23728*/
23729static int sqlite3MemSize(void *pPrior){
23730#ifdef SQLITE_MALLOCSIZE
23731 assert( pPrior!=0 );
23732 return (int)SQLITE_MALLOCSIZE(pPrior);
23733#else
23734 sqlite3_int64 *p;
23735 assert( pPrior!=0 );
23736 p = (sqlite3_int64*)pPrior;
23737 p--;
23738 return (int)p[0];
23739#endif
23740}
23741
23742/*
23743** Like realloc(). Resize an allocation previously obtained from
23744** sqlite3MemMalloc().
23745**
23746** For this low-level interface, we know that pPrior!=0. Cases where
23747** pPrior==0 while have been intercepted by higher-level routine and
23748** redirected to xMalloc. Similarly, we know that nByte>0 because
23749** cases where nByte<=0 will have been intercepted by higher-level
23750** routines and redirected to xFree.
23751*/
23752static void *sqlite3MemRealloc(void *pPrior, int nByte){
23753#ifdef SQLITE_MALLOCSIZE
23754 void *p = SQLITE_REALLOC(pPrior, nByte);
23755 if( p==0 ){
23756 testcase( sqlite3GlobalConfig.xLog!=0 );
23757 sqlite3_log(SQLITE_NOMEM,
23758 "failed memory resize %u to %u bytes",
23759 SQLITE_MALLOCSIZE(pPrior), nByte);
23760 }
23761 return p;
23762#else
23763 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
23764 assert( pPrior!=0 && nByte>0 );
23765 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
23766 p--;
23767 p = SQLITE_REALLOC(p, nByte+8 );
23768 if( p ){
23769 p[0] = nByte;
23770 p++;
23771 }else{
23772 testcase( sqlite3GlobalConfig.xLog!=0 );
23773 sqlite3_log(SQLITE_NOMEM,
23774 "failed memory resize %u to %u bytes",
23775 sqlite3MemSize(pPrior), nByte);
23776 }
23777 return (void*)p;
23778#endif
23779}
23780
23781/*
23782** Round up a request size to the next valid allocation size.
23783*/
23784static int sqlite3MemRoundup(int n){
23785 return ROUND8(n);
23786}
23787
23788/*
23789** Initialize this module.
23790*/
23791static int sqlite3MemInit(void *NotUsed){
23792#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
23793 int cpuCount;
23794 size_t len;
23795 if( _sqliteZone_ ){
23796 return SQLITE_OK;
23797 }
23798 len = sizeof(cpuCount);
23799 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
23800 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
23801 if( cpuCount>1 ){
23802 /* defer MT decisions to system malloc */
23803 _sqliteZone_ = malloc_default_zone();
23804 }else{
23805 /* only 1 core, use our own zone to contention over global locks,
23806 ** e.g. we have our own dedicated locks */
23807 _sqliteZone_ = malloc_create_zone(4096, 0);
23808 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
23809 }
23810#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
23811 UNUSED_PARAMETER(NotUsed);
23812 return SQLITE_OK;
23813}
23814
23815/*
23816** Deinitialize this module.
23817*/
23818static void sqlite3MemShutdown(void *NotUsed){
23819 UNUSED_PARAMETER(NotUsed);
23820 return;
23821}
23822
23823/*
23824** This routine is the only routine in this file with external linkage.
23825**
23826** Populate the low-level memory allocation function pointers in
23827** sqlite3GlobalConfig.m with pointers to the routines in this file.
23828*/
23829SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23830 static const sqlite3_mem_methods defaultMethods = {
23831 sqlite3MemMalloc,
23832 sqlite3MemFree,
23833 sqlite3MemRealloc,
23834 sqlite3MemSize,
23835 sqlite3MemRoundup,
23836 sqlite3MemInit,
23837 sqlite3MemShutdown,
23838 0
23839 };
23840 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23841}
23842
23843#endif /* SQLITE_SYSTEM_MALLOC */
23844
23845/************** End of mem1.c ************************************************/
23846/************** Begin file mem2.c ********************************************/
23847/*
23848** 2007 August 15
23849**
23850** The author disclaims copyright to this source code. In place of
23851** a legal notice, here is a blessing:
23852**
23853** May you do good and not evil.
23854** May you find forgiveness for yourself and forgive others.
23855** May you share freely, never taking more than you give.
23856**
23857*************************************************************************
23858**
23859** This file contains low-level memory allocation drivers for when
23860** SQLite will use the standard C-library malloc/realloc/free interface
23861** to obtain the memory it needs while adding lots of additional debugging
23862** information to each allocation in order to help detect and fix memory
23863** leaks and memory usage errors.
23864**
23865** This file contains implementations of the low-level memory allocation
23866** routines specified in the sqlite3_mem_methods object.
23867*/
23868/* #include "sqliteInt.h" */
23869
23870/*
23871** This version of the memory allocator is used only if the
23872** SQLITE_MEMDEBUG macro is defined
23873*/
23874#ifdef SQLITE_MEMDEBUG
23875
23876/*
23877** The backtrace functionality is only available with GLIBC
23878*/
23879#ifdef __GLIBC__
23880 extern int backtrace(void**,int);
23881 extern void backtrace_symbols_fd(void*const*,int,int);
23882#else
23883# define backtrace(A,B) 1
23884# define backtrace_symbols_fd(A,B,C)
23885#endif
23886/* #include <stdio.h> */
23887
23888/*
23889** Each memory allocation looks like this:
23890**
23891** ------------------------------------------------------------------------
23892** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
23893** ------------------------------------------------------------------------
23894**
23895** The application code sees only a pointer to the allocation. We have
23896** to back up from the allocation pointer to find the MemBlockHdr. The
23897** MemBlockHdr tells us the size of the allocation and the number of
23898** backtrace pointers. There is also a guard word at the end of the
23899** MemBlockHdr.
23900*/
23901struct MemBlockHdr {
23902 i64 iSize; /* Size of this allocation */
23903 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
23904 char nBacktrace; /* Number of backtraces on this alloc */
23905 char nBacktraceSlots; /* Available backtrace slots */
23906 u8 nTitle; /* Bytes of title; includes '\0' */
23907 u8 eType; /* Allocation type code */
23908 int iForeGuard; /* Guard word for sanity */
23909};
23910
23911/*
23912** Guard words
23913*/
23914#define FOREGUARD 0x80F5E153
23915#define REARGUARD 0xE4676B53
23916
23917/*
23918** Number of malloc size increments to track.
23919*/
23920#define NCSIZE 1000
23921
23922/*
23923** All of the static variables used by this module are collected
23924** into a single structure named "mem". This is to keep the
23925** static variables organized and to reduce namespace pollution
23926** when this module is combined with other in the amalgamation.
23927*/
23928static struct {
23929
23930 /*
23931 ** Mutex to control access to the memory allocation subsystem.
23932 */
23933 sqlite3_mutex *mutex;
23934
23935 /*
23936 ** Head and tail of a linked list of all outstanding allocations
23937 */
23938 struct MemBlockHdr *pFirst;
23939 struct MemBlockHdr *pLast;
23940
23941 /*
23942 ** The number of levels of backtrace to save in new allocations.
23943 */
23944 int nBacktrace;
23945 void (*xBacktrace)(int, int, void **);
23946
23947 /*
23948 ** Title text to insert in front of each block
23949 */
23950 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
23951 char zTitle[100]; /* The title text */
23952
23953 /*
23954 ** sqlite3MallocDisallow() increments the following counter.
23955 ** sqlite3MallocAllow() decrements it.
23956 */
23957 int disallow; /* Do not allow memory allocation */
23958
23959 /*
23960 ** Gather statistics on the sizes of memory allocations.
23961 ** nAlloc[i] is the number of allocation attempts of i*8
23962 ** bytes. i==NCSIZE is the number of allocation attempts for
23963 ** sizes more than NCSIZE*8 bytes.
23964 */
23965 int nAlloc[NCSIZE]; /* Total number of allocations */
23966 int nCurrent[NCSIZE]; /* Current number of allocations */
23967 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
23968
23969} mem;
23970
23971
23972/*
23973** Adjust memory usage statistics
23974*/
23975static void adjustStats(int iSize, int increment){
23976 int i = ROUND8(iSize)/8;
23977 if( i>NCSIZE-1 ){
23978 i = NCSIZE - 1;
23979 }
23980 if( increment>0 ){
23981 mem.nAlloc[i]++;
23982 mem.nCurrent[i]++;
23983 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
23984 mem.mxCurrent[i] = mem.nCurrent[i];
23985 }
23986 }else{
23987 mem.nCurrent[i]--;
23988 assert( mem.nCurrent[i]>=0 );
23989 }
23990}
23991
23992/*
23993** Given an allocation, find the MemBlockHdr for that allocation.
23994**
23995** This routine checks the guards at either end of the allocation and
23996** if they are incorrect it asserts.
23997*/
23998static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
23999 struct MemBlockHdr *p;
24000 int *pInt;
24001 u8 *pU8;
24002 int nReserve;
24003
24004 p = (struct MemBlockHdr*)pAllocation;
24005 p--;
24006 assert( p->iForeGuard==(int)FOREGUARD );
24007 nReserve = ROUND8(p->iSize);
24008 pInt = (int*)pAllocation;
24009 pU8 = (u8*)pAllocation;
24010 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
24011 /* This checks any of the "extra" bytes allocated due
24012 ** to rounding up to an 8 byte boundary to ensure
24013 ** they haven't been overwritten.
24014 */
24015 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
24016 return p;
24017}
24018
24019/*
24020** Return the number of bytes currently allocated at address p.
24021*/
24022static int sqlite3MemSize(void *p){
24023 struct MemBlockHdr *pHdr;
24024 if( !p ){
24025 return 0;
24026 }
24027 pHdr = sqlite3MemsysGetHeader(p);
24028 return (int)pHdr->iSize;
24029}
24030
24031/*
24032** Initialize the memory allocation subsystem.
24033*/
24034static int sqlite3MemInit(void *NotUsed){
24035 UNUSED_PARAMETER(NotUsed);
24036 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
24037 if( !sqlite3GlobalConfig.bMemstat ){
24038 /* If memory status is enabled, then the malloc.c wrapper will already
24039 ** hold the STATIC_MEM mutex when the routines here are invoked. */
24040 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
24041 }
24042 return SQLITE_OK;
24043}
24044
24045/*
24046** Deinitialize the memory allocation subsystem.
24047*/
24048static void sqlite3MemShutdown(void *NotUsed){
24049 UNUSED_PARAMETER(NotUsed);
24050 mem.mutex = 0;
24051}
24052
24053/*
24054** Round up a request size to the next valid allocation size.
24055*/
24056static int sqlite3MemRoundup(int n){
24057 return ROUND8(n);
24058}
24059
24060/*
24061** Fill a buffer with pseudo-random bytes. This is used to preset
24062** the content of a new memory allocation to unpredictable values and
24063** to clear the content of a freed allocation to unpredictable values.
24064*/
24065static void randomFill(char *pBuf, int nByte){
24066 unsigned int x, y, r;
24067 x = SQLITE_PTR_TO_INT(pBuf);
24068 y = nByte | 1;
24069 while( nByte >= 4 ){
24070 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
24071 y = y*1103515245 + 12345;
24072 r = x ^ y;
24073 *(int*)pBuf = r;
24074 pBuf += 4;
24075 nByte -= 4;
24076 }
24077 while( nByte-- > 0 ){
24078 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
24079 y = y*1103515245 + 12345;
24080 r = x ^ y;
24081 *(pBuf++) = r & 0xff;
24082 }
24083}
24084
24085/*
24086** Allocate nByte bytes of memory.
24087*/
24088static void *sqlite3MemMalloc(int nByte){
24089 struct MemBlockHdr *pHdr;
24090 void **pBt;
24091 char *z;
24092 int *pInt;
24093 void *p = 0;
24094 int totalSize;
24095 int nReserve;
24096 sqlite3_mutex_enter(mem.mutex);
24097 assert( mem.disallow==0 );
24098 nReserve = ROUND8(nByte);
24099 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
24100 mem.nBacktrace*sizeof(void*) + mem.nTitle;
24101 p = malloc(totalSize);
24102 if( p ){
24103 z = p;
24104 pBt = (void**)&z[mem.nTitle];
24105 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
24106 pHdr->pNext = 0;
24107 pHdr->pPrev = mem.pLast;
24108 if( mem.pLast ){
24109 mem.pLast->pNext = pHdr;
24110 }else{
24111 mem.pFirst = pHdr;
24112 }
24113 mem.pLast = pHdr;
24114 pHdr->iForeGuard = FOREGUARD;
24115 pHdr->eType = MEMTYPE_HEAP;
24116 pHdr->nBacktraceSlots = mem.nBacktrace;
24117 pHdr->nTitle = mem.nTitle;
24118 if( mem.nBacktrace ){
24119 void *aAddr[40];
24120 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
24121 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
24122 assert(pBt[0]);
24123 if( mem.xBacktrace ){
24124 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
24125 }
24126 }else{
24127 pHdr->nBacktrace = 0;
24128 }
24129 if( mem.nTitle ){
24130 memcpy(z, mem.zTitle, mem.nTitle);
24131 }
24132 pHdr->iSize = nByte;
24133 adjustStats(nByte, +1);
24134 pInt = (int*)&pHdr[1];
24135 pInt[nReserve/sizeof(int)] = REARGUARD;
24136 randomFill((char*)pInt, nByte);
24137 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
24138 p = (void*)pInt;
24139 }
24140 sqlite3_mutex_leave(mem.mutex);
24141 return p;
24142}
24143
24144/*
24145** Free memory.
24146*/
24147static void sqlite3MemFree(void *pPrior){
24148 struct MemBlockHdr *pHdr;
24149 void **pBt;
24150 char *z;
24151 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
24152 || mem.mutex!=0 );
24153 pHdr = sqlite3MemsysGetHeader(pPrior);
24154 pBt = (void**)pHdr;
24155 pBt -= pHdr->nBacktraceSlots;
24156 sqlite3_mutex_enter(mem.mutex);
24157 if( pHdr->pPrev ){
24158 assert( pHdr->pPrev->pNext==pHdr );
24159 pHdr->pPrev->pNext = pHdr->pNext;
24160 }else{
24161 assert( mem.pFirst==pHdr );
24162 mem.pFirst = pHdr->pNext;
24163 }
24164 if( pHdr->pNext ){
24165 assert( pHdr->pNext->pPrev==pHdr );
24166 pHdr->pNext->pPrev = pHdr->pPrev;
24167 }else{
24168 assert( mem.pLast==pHdr );
24169 mem.pLast = pHdr->pPrev;
24170 }
24171 z = (char*)pBt;
24172 z -= pHdr->nTitle;
24173 adjustStats((int)pHdr->iSize, -1);
24174 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
24175 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
24176 free(z);
24177 sqlite3_mutex_leave(mem.mutex);
24178}
24179
24180/*
24181** Change the size of an existing memory allocation.
24182**
24183** For this debugging implementation, we *always* make a copy of the
24184** allocation into a new place in memory. In this way, if the
24185** higher level code is using pointer to the old allocation, it is
24186** much more likely to break and we are much more liking to find
24187** the error.
24188*/
24189static void *sqlite3MemRealloc(void *pPrior, int nByte){
24190 struct MemBlockHdr *pOldHdr;
24191 void *pNew;
24192 assert( mem.disallow==0 );
24193 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
24194 pOldHdr = sqlite3MemsysGetHeader(pPrior);
24195 pNew = sqlite3MemMalloc(nByte);
24196 if( pNew ){
24197 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
24198 if( nByte>pOldHdr->iSize ){
24199 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
24200 }
24201 sqlite3MemFree(pPrior);
24202 }
24203 return pNew;
24204}
24205
24206/*
24207** Populate the low-level memory allocation function pointers in
24208** sqlite3GlobalConfig.m with pointers to the routines in this file.
24209*/
24210SQLITE_PRIVATE void sqlite3MemSetDefault(void){
24211 static const sqlite3_mem_methods defaultMethods = {
24212 sqlite3MemMalloc,
24213 sqlite3MemFree,
24214 sqlite3MemRealloc,
24215 sqlite3MemSize,
24216 sqlite3MemRoundup,
24217 sqlite3MemInit,
24218 sqlite3MemShutdown,
24219 0
24220 };
24221 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
24222}
24223
24224/*
24225** Set the "type" of an allocation.
24226*/
24227SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
24228 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
24229 struct MemBlockHdr *pHdr;
24230 pHdr = sqlite3MemsysGetHeader(p);
24231 assert( pHdr->iForeGuard==FOREGUARD );
24232 pHdr->eType = eType;
24233 }
24234}
24235
24236/*
24237** Return TRUE if the mask of type in eType matches the type of the
24238** allocation p. Also return true if p==NULL.
24239**
24240** This routine is designed for use within an assert() statement, to
24241** verify the type of an allocation. For example:
24242**
24243** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
24244*/
24245SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
24246 int rc = 1;
24247 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
24248 struct MemBlockHdr *pHdr;
24249 pHdr = sqlite3MemsysGetHeader(p);
24250 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
24251 if( (pHdr->eType&eType)==0 ){
24252 rc = 0;
24253 }
24254 }
24255 return rc;
24256}
24257
24258/*
24259** Return TRUE if the mask of type in eType matches no bits of the type of the
24260** allocation p. Also return true if p==NULL.
24261**
24262** This routine is designed for use within an assert() statement, to
24263** verify the type of an allocation. For example:
24264**
24265** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
24266*/
24267SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
24268 int rc = 1;
24269 if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){
24270 struct MemBlockHdr *pHdr;
24271 pHdr = sqlite3MemsysGetHeader(p);
24272 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
24273 if( (pHdr->eType&eType)!=0 ){
24274 rc = 0;
24275 }
24276 }
24277 return rc;
24278}
24279
24280/*
24281** Set the number of backtrace levels kept for each allocation.
24282** A value of zero turns off backtracing. The number is always rounded
24283** up to a multiple of 2.
24284*/
24285SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
24286 if( depth<0 ){ depth = 0; }
24287 if( depth>20 ){ depth = 20; }
24288 depth = (depth+1)&0xfe;
24289 mem.nBacktrace = depth;
24290}
24291
24292SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
24293 mem.xBacktrace = xBacktrace;
24294}
24295
24296/*
24297** Set the title string for subsequent allocations.
24298*/
24299SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
24300 unsigned int n = sqlite3Strlen30(zTitle) + 1;
24301 sqlite3_mutex_enter(mem.mutex);
24302 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
24303 memcpy(mem.zTitle, zTitle, n);
24304 mem.zTitle[n] = 0;
24305 mem.nTitle = ROUND8(n);
24306 sqlite3_mutex_leave(mem.mutex);
24307}
24308
24309SQLITE_PRIVATE void sqlite3MemdebugSync(){
24310 struct MemBlockHdr *pHdr;
24311 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
24312 void **pBt = (void**)pHdr;
24313 pBt -= pHdr->nBacktraceSlots;
24314 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
24315 }
24316}
24317
24318/*
24319** Open the file indicated and write a log of all unfreed memory
24320** allocations into that log.
24321*/
24322SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
24323 FILE *out;
24324 struct MemBlockHdr *pHdr;
24325 void **pBt;
24326 int i;
24327 out = fopen(zFilename, "w");
24328 if( out==0 ){
24329 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24330 zFilename);
24331 return;
24332 }
24333 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
24334 char *z = (char*)pHdr;
24335 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
24336 fprintf(out, "**** %lld bytes at %p from %s ****\n",
24337 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
24338 if( pHdr->nBacktrace ){
24339 fflush(out);
24340 pBt = (void**)pHdr;
24341 pBt -= pHdr->nBacktraceSlots;
24342 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
24343 fprintf(out, "\n");
24344 }
24345 }
24346 fprintf(out, "COUNTS:\n");
24347 for(i=0; i<NCSIZE-1; i++){
24348 if( mem.nAlloc[i] ){
24349 fprintf(out, " %5d: %10d %10d %10d\n",
24350 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
24351 }
24352 }
24353 if( mem.nAlloc[NCSIZE-1] ){
24354 fprintf(out, " %5d: %10d %10d %10d\n",
24355 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
24356 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
24357 }
24358 fclose(out);
24359}
24360
24361/*
24362** Return the number of times sqlite3MemMalloc() has been called.
24363*/
24364SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
24365 int i;
24366 int nTotal = 0;
24367 for(i=0; i<NCSIZE; i++){
24368 nTotal += mem.nAlloc[i];
24369 }
24370 return nTotal;
24371}
24372
24373
24374#endif /* SQLITE_MEMDEBUG */
24375
24376/************** End of mem2.c ************************************************/
24377/************** Begin file mem3.c ********************************************/
24378/*
24379** 2007 October 14
24380**
24381** The author disclaims copyright to this source code. In place of
24382** a legal notice, here is a blessing:
24383**
24384** May you do good and not evil.
24385** May you find forgiveness for yourself and forgive others.
24386** May you share freely, never taking more than you give.
24387**
24388*************************************************************************
24389** This file contains the C functions that implement a memory
24390** allocation subsystem for use by SQLite.
24391**
24392** This version of the memory allocation subsystem omits all
24393** use of malloc(). The SQLite user supplies a block of memory
24394** before calling sqlite3_initialize() from which allocations
24395** are made and returned by the xMalloc() and xRealloc()
24396** implementations. Once sqlite3_initialize() has been called,
24397** the amount of memory available to SQLite is fixed and cannot
24398** be changed.
24399**
24400** This version of the memory allocation subsystem is included
24401** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
24402*/
24403/* #include "sqliteInt.h" */
24404
24405/*
24406** This version of the memory allocator is only built into the library
24407** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
24408** mean that the library will use a memory-pool by default, just that
24409** it is available. The mempool allocator is activated by calling
24410** sqlite3_config().
24411*/
24412#ifdef SQLITE_ENABLE_MEMSYS3
24413
24414/*
24415** Maximum size (in Mem3Blocks) of a "small" chunk.
24416*/
24417#define MX_SMALL 10
24418
24419
24420/*
24421** Number of freelist hash slots
24422*/
24423#define N_HASH 61
24424
24425/*
24426** A memory allocation (also called a "chunk") consists of two or
24427** more blocks where each block is 8 bytes. The first 8 bytes are
24428** a header that is not returned to the user.
24429**
24430** A chunk is two or more blocks that is either checked out or
24431** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
24432** size of the allocation in blocks if the allocation is free.
24433** The u.hdr.size4x&1 bit is true if the chunk is checked out and
24434** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
24435** is true if the previous chunk is checked out and false if the
24436** previous chunk is free. The u.hdr.prevSize field is the size of
24437** the previous chunk in blocks if the previous chunk is on the
24438** freelist. If the previous chunk is checked out, then
24439** u.hdr.prevSize can be part of the data for that chunk and should
24440** not be read or written.
24441**
24442** We often identify a chunk by its index in mem3.aPool[]. When
24443** this is done, the chunk index refers to the second block of
24444** the chunk. In this way, the first chunk has an index of 1.
24445** A chunk index of 0 means "no such chunk" and is the equivalent
24446** of a NULL pointer.
24447**
24448** The second block of free chunks is of the form u.list. The
24449** two fields form a double-linked list of chunks of related sizes.
24450** Pointers to the head of the list are stored in mem3.aiSmall[]
24451** for smaller chunks and mem3.aiHash[] for larger chunks.
24452**
24453** The second block of a chunk is user data if the chunk is checked
24454** out. If a chunk is checked out, the user data may extend into
24455** the u.hdr.prevSize value of the following chunk.
24456*/
24457typedef struct Mem3Block Mem3Block;
24458struct Mem3Block {
24459 union {
24460 struct {
24461 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
24462 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
24463 } hdr;
24464 struct {
24465 u32 next; /* Index in mem3.aPool[] of next free chunk */
24466 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
24467 } list;
24468 } u;
24469};
24470
24471/*
24472** All of the static variables used by this module are collected
24473** into a single structure named "mem3". This is to keep the
24474** static variables organized and to reduce namespace pollution
24475** when this module is combined with other in the amalgamation.
24476*/
24477static SQLITE_WSD struct Mem3Global {
24478 /*
24479 ** Memory available for allocation. nPool is the size of the array
24480 ** (in Mem3Blocks) pointed to by aPool less 2.
24481 */
24482 u32 nPool;
24483 Mem3Block *aPool;
24484
24485 /*
24486 ** True if we are evaluating an out-of-memory callback.
24487 */
24488 int alarmBusy;
24489
24490 /*
24491 ** Mutex to control access to the memory allocation subsystem.
24492 */
24493 sqlite3_mutex *mutex;
24494
24495 /*
24496 ** The minimum amount of free space that we have seen.
24497 */
24498 u32 mnKeyBlk;
24499
24500 /*
24501 ** iKeyBlk is the index of the key chunk. Most new allocations
24502 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
24503 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
24504 ** The key chunk is not in either the aiHash[] or aiSmall[].
24505 */
24506 u32 iKeyBlk;
24507 u32 szKeyBlk;
24508
24509 /*
24510 ** Array of lists of free blocks according to the block size
24511 ** for smaller chunks, or a hash on the block size for larger
24512 ** chunks.
24513 */
24514 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
24515 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
24516} mem3 = { 97535575 };
24517
24518#define mem3 GLOBAL(struct Mem3Global, mem3)
24519
24520/*
24521** Unlink the chunk at mem3.aPool[i] from list it is currently
24522** on. *pRoot is the list that i is a member of.
24523*/
24524static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
24525 u32 next = mem3.aPool[i].u.list.next;
24526 u32 prev = mem3.aPool[i].u.list.prev;
24527 assert( sqlite3_mutex_held(mem3.mutex) );
24528 if( prev==0 ){
24529 *pRoot = next;
24530 }else{
24531 mem3.aPool[prev].u.list.next = next;
24532 }
24533 if( next ){
24534 mem3.aPool[next].u.list.prev = prev;
24535 }
24536 mem3.aPool[i].u.list.next = 0;
24537 mem3.aPool[i].u.list.prev = 0;
24538}
24539
24540/*
24541** Unlink the chunk at index i from
24542** whatever list is currently a member of.
24543*/
24544static void memsys3Unlink(u32 i){
24545 u32 size, hash;
24546 assert( sqlite3_mutex_held(mem3.mutex) );
24547 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
24548 assert( i>=1 );
24549 size = mem3.aPool[i-1].u.hdr.size4x/4;
24550 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
24551 assert( size>=2 );
24552 if( size <= MX_SMALL ){
24553 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
24554 }else{
24555 hash = size % N_HASH;
24556 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
24557 }
24558}
24559
24560/*
24561** Link the chunk at mem3.aPool[i] so that is on the list rooted
24562** at *pRoot.
24563*/
24564static void memsys3LinkIntoList(u32 i, u32 *pRoot){
24565 assert( sqlite3_mutex_held(mem3.mutex) );
24566 mem3.aPool[i].u.list.next = *pRoot;
24567 mem3.aPool[i].u.list.prev = 0;
24568 if( *pRoot ){
24569 mem3.aPool[*pRoot].u.list.prev = i;
24570 }
24571 *pRoot = i;
24572}
24573
24574/*
24575** Link the chunk at index i into either the appropriate
24576** small chunk list, or into the large chunk hash table.
24577*/
24578static void memsys3Link(u32 i){
24579 u32 size, hash;
24580 assert( sqlite3_mutex_held(mem3.mutex) );
24581 assert( i>=1 );
24582 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
24583 size = mem3.aPool[i-1].u.hdr.size4x/4;
24584 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
24585 assert( size>=2 );
24586 if( size <= MX_SMALL ){
24587 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
24588 }else{
24589 hash = size % N_HASH;
24590 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
24591 }
24592}
24593
24594/*
24595** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
24596** will already be held (obtained by code in malloc.c) if
24597** sqlite3GlobalConfig.bMemStat is true.
24598*/
24599static void memsys3Enter(void){
24600 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
24601 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
24602 }
24603 sqlite3_mutex_enter(mem3.mutex);
24604}
24605static void memsys3Leave(void){
24606 sqlite3_mutex_leave(mem3.mutex);
24607}
24608
24609/*
24610** Called when we are unable to satisfy an allocation of nBytes.
24611*/
24612static void memsys3OutOfMemory(int nByte){
24613 if( !mem3.alarmBusy ){
24614 mem3.alarmBusy = 1;
24615 assert( sqlite3_mutex_held(mem3.mutex) );
24616 sqlite3_mutex_leave(mem3.mutex);
24617 sqlite3_release_memory(nByte);
24618 sqlite3_mutex_enter(mem3.mutex);
24619 mem3.alarmBusy = 0;
24620 }
24621}
24622
24623
24624/*
24625** Chunk i is a free chunk that has been unlinked. Adjust its
24626** size parameters for check-out and return a pointer to the
24627** user portion of the chunk.
24628*/
24629static void *memsys3Checkout(u32 i, u32 nBlock){
24630 u32 x;
24631 assert( sqlite3_mutex_held(mem3.mutex) );
24632 assert( i>=1 );
24633 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
24634 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
24635 x = mem3.aPool[i-1].u.hdr.size4x;
24636 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
24637 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
24638 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
24639 return &mem3.aPool[i];
24640}
24641
24642/*
24643** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
24644** Return a pointer to the new allocation. Or, if the key chunk
24645** is not large enough, return 0.
24646*/
24647static void *memsys3FromKeyBlk(u32 nBlock){
24648 assert( sqlite3_mutex_held(mem3.mutex) );
24649 assert( mem3.szKeyBlk>=nBlock );
24650 if( nBlock>=mem3.szKeyBlk-1 ){
24651 /* Use the entire key chunk */
24652 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
24653 mem3.iKeyBlk = 0;
24654 mem3.szKeyBlk = 0;
24655 mem3.mnKeyBlk = 0;
24656 return p;
24657 }else{
24658 /* Split the key block. Return the tail. */
24659 u32 newi, x;
24660 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
24661 assert( newi > mem3.iKeyBlk+1 );
24662 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
24663 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
24664 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
24665 mem3.szKeyBlk -= nBlock;
24666 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
24667 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
24668 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
24669 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
24670 mem3.mnKeyBlk = mem3.szKeyBlk;
24671 }
24672 return (void*)&mem3.aPool[newi];
24673 }
24674}
24675
24676/*
24677** *pRoot is the head of a list of free chunks of the same size
24678** or same size hash. In other words, *pRoot is an entry in either
24679** mem3.aiSmall[] or mem3.aiHash[].
24680**
24681** This routine examines all entries on the given list and tries
24682** to coalesce each entries with adjacent free chunks.
24683**
24684** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
24685** the current mem3.iKeyBlk with the new larger chunk. In order for
24686** this mem3.iKeyBlk replacement to work, the key chunk must be
24687** linked into the hash tables. That is not the normal state of
24688** affairs, of course. The calling routine must link the key
24689** chunk before invoking this routine, then must unlink the (possibly
24690** changed) key chunk once this routine has finished.
24691*/
24692static void memsys3Merge(u32 *pRoot){
24693 u32 iNext, prev, size, i, x;
24694
24695 assert( sqlite3_mutex_held(mem3.mutex) );
24696 for(i=*pRoot; i>0; i=iNext){
24697 iNext = mem3.aPool[i].u.list.next;
24698 size = mem3.aPool[i-1].u.hdr.size4x;
24699 assert( (size&1)==0 );
24700 if( (size&2)==0 ){
24701 memsys3UnlinkFromList(i, pRoot);
24702 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
24703 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
24704 if( prev==iNext ){
24705 iNext = mem3.aPool[prev].u.list.next;
24706 }
24707 memsys3Unlink(prev);
24708 size = i + size/4 - prev;
24709 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
24710 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
24711 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
24712 memsys3Link(prev);
24713 i = prev;
24714 }else{
24715 size /= 4;
24716 }
24717 if( size>mem3.szKeyBlk ){
24718 mem3.iKeyBlk = i;
24719 mem3.szKeyBlk = size;
24720 }
24721 }
24722}
24723
24724/*
24725** Return a block of memory of at least nBytes in size.
24726** Return NULL if unable.
24727**
24728** This function assumes that the necessary mutexes, if any, are
24729** already held by the caller. Hence "Unsafe".
24730*/
24731static void *memsys3MallocUnsafe(int nByte){
24732 u32 i;
24733 u32 nBlock;
24734 u32 toFree;
24735
24736 assert( sqlite3_mutex_held(mem3.mutex) );
24737 assert( sizeof(Mem3Block)==8 );
24738 if( nByte<=12 ){
24739 nBlock = 2;
24740 }else{
24741 nBlock = (nByte + 11)/8;
24742 }
24743 assert( nBlock>=2 );
24744
24745 /* STEP 1:
24746 ** Look for an entry of the correct size in either the small
24747 ** chunk table or in the large chunk hash table. This is
24748 ** successful most of the time (about 9 times out of 10).
24749 */
24750 if( nBlock <= MX_SMALL ){
24751 i = mem3.aiSmall[nBlock-2];
24752 if( i>0 ){
24753 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
24754 return memsys3Checkout(i, nBlock);
24755 }
24756 }else{
24757 int hash = nBlock % N_HASH;
24758 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
24759 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
24760 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
24761 return memsys3Checkout(i, nBlock);
24762 }
24763 }
24764 }
24765
24766 /* STEP 2:
24767 ** Try to satisfy the allocation by carving a piece off of the end
24768 ** of the key chunk. This step usually works if step 1 fails.
24769 */
24770 if( mem3.szKeyBlk>=nBlock ){
24771 return memsys3FromKeyBlk(nBlock);
24772 }
24773
24774
24775 /* STEP 3:
24776 ** Loop through the entire memory pool. Coalesce adjacent free
24777 ** chunks. Recompute the key chunk as the largest free chunk.
24778 ** Then try again to satisfy the allocation by carving a piece off
24779 ** of the end of the key chunk. This step happens very
24780 ** rarely (we hope!)
24781 */
24782 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
24783 memsys3OutOfMemory(toFree);
24784 if( mem3.iKeyBlk ){
24785 memsys3Link(mem3.iKeyBlk);
24786 mem3.iKeyBlk = 0;
24787 mem3.szKeyBlk = 0;
24788 }
24789 for(i=0; i<N_HASH; i++){
24790 memsys3Merge(&mem3.aiHash[i]);
24791 }
24792 for(i=0; i<MX_SMALL-1; i++){
24793 memsys3Merge(&mem3.aiSmall[i]);
24794 }
24795 if( mem3.szKeyBlk ){
24796 memsys3Unlink(mem3.iKeyBlk);
24797 if( mem3.szKeyBlk>=nBlock ){
24798 return memsys3FromKeyBlk(nBlock);
24799 }
24800 }
24801 }
24802
24803 /* If none of the above worked, then we fail. */
24804 return 0;
24805}
24806
24807/*
24808** Free an outstanding memory allocation.
24809**
24810** This function assumes that the necessary mutexes, if any, are
24811** already held by the caller. Hence "Unsafe".
24812*/
24813static void memsys3FreeUnsafe(void *pOld){
24814 Mem3Block *p = (Mem3Block*)pOld;
24815 int i;
24816 u32 size, x;
24817 assert( sqlite3_mutex_held(mem3.mutex) );
24818 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
24819 i = p - mem3.aPool;
24820 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
24821 size = mem3.aPool[i-1].u.hdr.size4x/4;
24822 assert( i+size<=mem3.nPool+1 );
24823 mem3.aPool[i-1].u.hdr.size4x &= ~1;
24824 mem3.aPool[i+size-1].u.hdr.prevSize = size;
24825 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
24826 memsys3Link(i);
24827
24828 /* Try to expand the key using the newly freed chunk */
24829 if( mem3.iKeyBlk ){
24830 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
24831 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
24832 mem3.iKeyBlk -= size;
24833 mem3.szKeyBlk += size;
24834 memsys3Unlink(mem3.iKeyBlk);
24835 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
24836 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
24837 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
24838 }
24839 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
24840 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
24841 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
24842 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
24843 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
24844 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
24845 }
24846 }
24847}
24848
24849/*
24850** Return the size of an outstanding allocation, in bytes. The
24851** size returned omits the 8-byte header overhead. This only
24852** works for chunks that are currently checked out.
24853*/
24854static int memsys3Size(void *p){
24855 Mem3Block *pBlock;
24856 assert( p!=0 );
24857 pBlock = (Mem3Block*)p;
24858 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
24859 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
24860}
24861
24862/*
24863** Round up a request size to the next valid allocation size.
24864*/
24865static int memsys3Roundup(int n){
24866 if( n<=12 ){
24867 return 12;
24868 }else{
24869 return ((n+11)&~7) - 4;
24870 }
24871}
24872
24873/*
24874** Allocate nBytes of memory.
24875*/
24876static void *memsys3Malloc(int nBytes){
24877 sqlite3_int64 *p;
24878 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
24879 memsys3Enter();
24880 p = memsys3MallocUnsafe(nBytes);
24881 memsys3Leave();
24882 return (void*)p;
24883}
24884
24885/*
24886** Free memory.
24887*/
24888static void memsys3Free(void *pPrior){
24889 assert( pPrior );
24890 memsys3Enter();
24891 memsys3FreeUnsafe(pPrior);
24892 memsys3Leave();
24893}
24894
24895/*
24896** Change the size of an existing memory allocation
24897*/
24898static void *memsys3Realloc(void *pPrior, int nBytes){
24899 int nOld;
24900 void *p;
24901 if( pPrior==0 ){
24902 return sqlite3_malloc(nBytes);
24903 }
24904 if( nBytes<=0 ){
24905 sqlite3_free(pPrior);
24906 return 0;
24907 }
24908 nOld = memsys3Size(pPrior);
24909 if( nBytes<=nOld && nBytes>=nOld-128 ){
24910 return pPrior;
24911 }
24912 memsys3Enter();
24913 p = memsys3MallocUnsafe(nBytes);
24914 if( p ){
24915 if( nOld<nBytes ){
24916 memcpy(p, pPrior, nOld);
24917 }else{
24918 memcpy(p, pPrior, nBytes);
24919 }
24920 memsys3FreeUnsafe(pPrior);
24921 }
24922 memsys3Leave();
24923 return p;
24924}
24925
24926/*
24927** Initialize this module.
24928*/
24929static int memsys3Init(void *NotUsed){
24930 UNUSED_PARAMETER(NotUsed);
24931 if( !sqlite3GlobalConfig.pHeap ){
24932 return SQLITE_ERROR;
24933 }
24934
24935 /* Store a pointer to the memory block in global structure mem3. */
24936 assert( sizeof(Mem3Block)==8 );
24937 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
24938 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
24939
24940 /* Initialize the key block. */
24941 mem3.szKeyBlk = mem3.nPool;
24942 mem3.mnKeyBlk = mem3.szKeyBlk;
24943 mem3.iKeyBlk = 1;
24944 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
24945 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
24946 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
24947
24948 return SQLITE_OK;
24949}
24950
24951/*
24952** Deinitialize this module.
24953*/
24954static void memsys3Shutdown(void *NotUsed){
24955 UNUSED_PARAMETER(NotUsed);
24956 mem3.mutex = 0;
24957 return;
24958}
24959
24960
24961
24962/*
24963** Open the file indicated and write a log of all unfreed memory
24964** allocations into that log.
24965*/
24966SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
24967#ifdef SQLITE_DEBUG
24968 FILE *out;
24969 u32 i, j;
24970 u32 size;
24971 if( zFilename==0 || zFilename[0]==0 ){
24972 out = stdout;
24973 }else{
24974 out = fopen(zFilename, "w");
24975 if( out==0 ){
24976 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24977 zFilename);
24978 return;
24979 }
24980 }
24981 memsys3Enter();
24982 fprintf(out, "CHUNKS:\n");
24983 for(i=1; i<=mem3.nPool; i+=size/4){
24984 size = mem3.aPool[i-1].u.hdr.size4x;
24985 if( size/4<=1 ){
24986 fprintf(out, "%p size error\n", &mem3.aPool[i]);
24987 assert( 0 );
24988 break;
24989 }
24990 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
24991 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
24992 assert( 0 );
24993 break;
24994 }
24995 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
24996 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
24997 assert( 0 );
24998 break;
24999 }
25000 if( size&1 ){
25001 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
25002 }else{
25003 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
25004 i==mem3.iKeyBlk ? " **key**" : "");
25005 }
25006 }
25007 for(i=0; i<MX_SMALL-1; i++){
25008 if( mem3.aiSmall[i]==0 ) continue;
25009 fprintf(out, "small(%2d):", i);
25010 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
25011 fprintf(out, " %p(%d)", &mem3.aPool[j],
25012 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
25013 }
25014 fprintf(out, "\n");
25015 }
25016 for(i=0; i<N_HASH; i++){
25017 if( mem3.aiHash[i]==0 ) continue;
25018 fprintf(out, "hash(%2d):", i);
25019 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
25020 fprintf(out, " %p(%d)", &mem3.aPool[j],
25021 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
25022 }
25023 fprintf(out, "\n");
25024 }
25025 fprintf(out, "key=%d\n", mem3.iKeyBlk);
25026 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
25027 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
25028 sqlite3_mutex_leave(mem3.mutex);
25029 if( out==stdout ){
25030 fflush(stdout);
25031 }else{
25032 fclose(out);
25033 }
25034#else
25035 UNUSED_PARAMETER(zFilename);
25036#endif
25037}
25038
25039/*
25040** This routine is the only routine in this file with external
25041** linkage.
25042**
25043** Populate the low-level memory allocation function pointers in
25044** sqlite3GlobalConfig.m with pointers to the routines in this file. The
25045** arguments specify the block of memory to manage.
25046**
25047** This routine is only called by sqlite3_config(), and therefore
25048** is not required to be threadsafe (it is not).
25049*/
25050SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
25051 static const sqlite3_mem_methods mempoolMethods = {
25052 memsys3Malloc,
25053 memsys3Free,
25054 memsys3Realloc,
25055 memsys3Size,
25056 memsys3Roundup,
25057 memsys3Init,
25058 memsys3Shutdown,
25059 0
25060 };
25061 return &mempoolMethods;
25062}
25063
25064#endif /* SQLITE_ENABLE_MEMSYS3 */
25065
25066/************** End of mem3.c ************************************************/
25067/************** Begin file mem5.c ********************************************/
25068/*
25069** 2007 October 14
25070**
25071** The author disclaims copyright to this source code. In place of
25072** a legal notice, here is a blessing:
25073**
25074** May you do good and not evil.
25075** May you find forgiveness for yourself and forgive others.
25076** May you share freely, never taking more than you give.
25077**
25078*************************************************************************
25079** This file contains the C functions that implement a memory
25080** allocation subsystem for use by SQLite.
25081**
25082** This version of the memory allocation subsystem omits all
25083** use of malloc(). The application gives SQLite a block of memory
25084** before calling sqlite3_initialize() from which allocations
25085** are made and returned by the xMalloc() and xRealloc()
25086** implementations. Once sqlite3_initialize() has been called,
25087** the amount of memory available to SQLite is fixed and cannot
25088** be changed.
25089**
25090** This version of the memory allocation subsystem is included
25091** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
25092**
25093** This memory allocator uses the following algorithm:
25094**
25095** 1. All memory allocation sizes are rounded up to a power of 2.
25096**
25097** 2. If two adjacent free blocks are the halves of a larger block,
25098** then the two blocks are coalesced into the single larger block.
25099**
25100** 3. New memory is allocated from the first available free block.
25101**
25102** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
25103** Concerning Dynamic Storage Allocation". Journal of the Association for
25104** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
25105**
25106** Let n be the size of the largest allocation divided by the minimum
25107** allocation size (after rounding all sizes up to a power of 2.) Let M
25108** be the maximum amount of memory ever outstanding at one time. Let
25109** N be the total amount of memory available for allocation. Robson
25110** proved that this memory allocator will never breakdown due to
25111** fragmentation as long as the following constraint holds:
25112**
25113** N >= M*(1 + log2(n)/2) - n + 1
25114**
25115** The sqlite3_status() logic tracks the maximum values of n and M so
25116** that an application can, at any time, verify this constraint.
25117*/
25118/* #include "sqliteInt.h" */
25119
25120/*
25121** This version of the memory allocator is used only when
25122** SQLITE_ENABLE_MEMSYS5 is defined.
25123*/
25124#ifdef SQLITE_ENABLE_MEMSYS5
25125
25126/*
25127** A minimum allocation is an instance of the following structure.
25128** Larger allocations are an array of these structures where the
25129** size of the array is a power of 2.
25130**
25131** The size of this object must be a power of two. That fact is
25132** verified in memsys5Init().
25133*/
25134typedef struct Mem5Link Mem5Link;
25135struct Mem5Link {
25136 int next; /* Index of next free chunk */
25137 int prev; /* Index of previous free chunk */
25138};
25139
25140/*
25141** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
25142** mem5.szAtom is always at least 8 and 32-bit integers are used,
25143** it is not actually possible to reach this limit.
25144*/
25145#define LOGMAX 30
25146
25147/*
25148** Masks used for mem5.aCtrl[] elements.
25149*/
25150#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
25151#define CTRL_FREE 0x20 /* True if not checked out */
25152
25153/*
25154** All of the static variables used by this module are collected
25155** into a single structure named "mem5". This is to keep the
25156** static variables organized and to reduce namespace pollution
25157** when this module is combined with other in the amalgamation.
25158*/
25159static SQLITE_WSD struct Mem5Global {
25160 /*
25161 ** Memory available for allocation
25162 */
25163 int szAtom; /* Smallest possible allocation in bytes */
25164 int nBlock; /* Number of szAtom sized blocks in zPool */
25165 u8 *zPool; /* Memory available to be allocated */
25166
25167 /*
25168 ** Mutex to control access to the memory allocation subsystem.
25169 */
25170 sqlite3_mutex *mutex;
25171
25172#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25173 /*
25174 ** Performance statistics
25175 */
25176 u64 nAlloc; /* Total number of calls to malloc */
25177 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
25178 u64 totalExcess; /* Total internal fragmentation */
25179 u32 currentOut; /* Current checkout, including internal fragmentation */
25180 u32 currentCount; /* Current number of distinct checkouts */
25181 u32 maxOut; /* Maximum instantaneous currentOut */
25182 u32 maxCount; /* Maximum instantaneous currentCount */
25183 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
25184#endif
25185
25186 /*
25187 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
25188 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
25189 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
25190 */
25191 int aiFreelist[LOGMAX+1];
25192
25193 /*
25194 ** Space for tracking which blocks are checked out and the size
25195 ** of each block. One byte per block.
25196 */
25197 u8 *aCtrl;
25198
25199} mem5;
25200
25201/*
25202** Access the static variable through a macro for SQLITE_OMIT_WSD.
25203*/
25204#define mem5 GLOBAL(struct Mem5Global, mem5)
25205
25206/*
25207** Assuming mem5.zPool is divided up into an array of Mem5Link
25208** structures, return a pointer to the idx-th such link.
25209*/
25210#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
25211
25212/*
25213** Unlink the chunk at mem5.aPool[i] from list it is currently
25214** on. It should be found on mem5.aiFreelist[iLogsize].
25215*/
25216static void memsys5Unlink(int i, int iLogsize){
25217 int next, prev;
25218 assert( i>=0 && i<mem5.nBlock );
25219 assert( iLogsize>=0 && iLogsize<=LOGMAX );
25220 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
25221
25222 next = MEM5LINK(i)->next;
25223 prev = MEM5LINK(i)->prev;
25224 if( prev<0 ){
25225 mem5.aiFreelist[iLogsize] = next;
25226 }else{
25227 MEM5LINK(prev)->next = next;
25228 }
25229 if( next>=0 ){
25230 MEM5LINK(next)->prev = prev;
25231 }
25232}
25233
25234/*
25235** Link the chunk at mem5.aPool[i] so that is on the iLogsize
25236** free list.
25237*/
25238static void memsys5Link(int i, int iLogsize){
25239 int x;
25240 assert( sqlite3_mutex_held(mem5.mutex) );
25241 assert( i>=0 && i<mem5.nBlock );
25242 assert( iLogsize>=0 && iLogsize<=LOGMAX );
25243 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
25244
25245 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
25246 MEM5LINK(i)->prev = -1;
25247 if( x>=0 ){
25248 assert( x<mem5.nBlock );
25249 MEM5LINK(x)->prev = i;
25250 }
25251 mem5.aiFreelist[iLogsize] = i;
25252}
25253
25254/*
25255** Obtain or release the mutex needed to access global data structures.
25256*/
25257static void memsys5Enter(void){
25258 sqlite3_mutex_enter(mem5.mutex);
25259}
25260static void memsys5Leave(void){
25261 sqlite3_mutex_leave(mem5.mutex);
25262}
25263
25264/*
25265** Return the size of an outstanding allocation, in bytes.
25266** This only works for chunks that are currently checked out.
25267*/
25268static int memsys5Size(void *p){
25269 int iSize, i;
25270 assert( p!=0 );
25271 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
25272 assert( i>=0 && i<mem5.nBlock );
25273 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
25274 return iSize;
25275}
25276
25277/*
25278** Return a block of memory of at least nBytes in size.
25279** Return NULL if unable. Return NULL if nBytes==0.
25280**
25281** The caller guarantees that nByte is positive.
25282**
25283** The caller has obtained a mutex prior to invoking this
25284** routine so there is never any chance that two or more
25285** threads can be in this routine at the same time.
25286*/
25287static void *memsys5MallocUnsafe(int nByte){
25288 int i; /* Index of a mem5.aPool[] slot */
25289 int iBin; /* Index into mem5.aiFreelist[] */
25290 int iFullSz; /* Size of allocation rounded up to power of 2 */
25291 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
25292
25293 /* nByte must be a positive */
25294 assert( nByte>0 );
25295
25296 /* No more than 1GiB per allocation */
25297 if( nByte > 0x40000000 ) return 0;
25298
25299#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25300 /* Keep track of the maximum allocation request. Even unfulfilled
25301 ** requests are counted */
25302 if( (u32)nByte>mem5.maxRequest ){
25303 mem5.maxRequest = nByte;
25304 }
25305#endif
25306
25307
25308 /* Round nByte up to the next valid power of two */
25309 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
25310
25311 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
25312 ** block. If not, then split a block of the next larger power of
25313 ** two in order to create a new free block of size iLogsize.
25314 */
25315 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
25316 if( iBin>LOGMAX ){
25317 testcase( sqlite3GlobalConfig.xLog!=0 );
25318 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
25319 return 0;
25320 }
25321 i = mem5.aiFreelist[iBin];
25322 memsys5Unlink(i, iBin);
25323 while( iBin>iLogsize ){
25324 int newSize;
25325
25326 iBin--;
25327 newSize = 1 << iBin;
25328 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
25329 memsys5Link(i+newSize, iBin);
25330 }
25331 mem5.aCtrl[i] = iLogsize;
25332
25333#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25334 /* Update allocator performance statistics. */
25335 mem5.nAlloc++;
25336 mem5.totalAlloc += iFullSz;
25337 mem5.totalExcess += iFullSz - nByte;
25338 mem5.currentCount++;
25339 mem5.currentOut += iFullSz;
25340 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
25341 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
25342#endif
25343
25344#ifdef SQLITE_DEBUG
25345 /* Make sure the allocated memory does not assume that it is set to zero
25346 ** or retains a value from a previous allocation */
25347 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
25348#endif
25349
25350 /* Return a pointer to the allocated memory. */
25351 return (void*)&mem5.zPool[i*mem5.szAtom];
25352}
25353
25354/*
25355** Free an outstanding memory allocation.
25356*/
25357static void memsys5FreeUnsafe(void *pOld){
25358 u32 size, iLogsize;
25359 int iBlock;
25360
25361 /* Set iBlock to the index of the block pointed to by pOld in
25362 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
25363 */
25364 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
25365
25366 /* Check that the pointer pOld points to a valid, non-free block. */
25367 assert( iBlock>=0 && iBlock<mem5.nBlock );
25368 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
25369 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
25370
25371 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
25372 size = 1<<iLogsize;
25373 assert( iBlock+size-1<(u32)mem5.nBlock );
25374
25375 mem5.aCtrl[iBlock] |= CTRL_FREE;
25376 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
25377
25378#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
25379 assert( mem5.currentCount>0 );
25380 assert( mem5.currentOut>=(size*mem5.szAtom) );
25381 mem5.currentCount--;
25382 mem5.currentOut -= size*mem5.szAtom;
25383 assert( mem5.currentOut>0 || mem5.currentCount==0 );
25384 assert( mem5.currentCount>0 || mem5.currentOut==0 );
25385#endif
25386
25387 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
25388 while( ALWAYS(iLogsize<LOGMAX) ){
25389 int iBuddy;
25390 if( (iBlock>>iLogsize) & 1 ){
25391 iBuddy = iBlock - size;
25392 assert( iBuddy>=0 );
25393 }else{
25394 iBuddy = iBlock + size;
25395 if( iBuddy>=mem5.nBlock ) break;
25396 }
25397 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
25398 memsys5Unlink(iBuddy, iLogsize);
25399 iLogsize++;
25400 if( iBuddy<iBlock ){
25401 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
25402 mem5.aCtrl[iBlock] = 0;
25403 iBlock = iBuddy;
25404 }else{
25405 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
25406 mem5.aCtrl[iBuddy] = 0;
25407 }
25408 size *= 2;
25409 }
25410
25411#ifdef SQLITE_DEBUG
25412 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
25413 ** not used after being freed */
25414 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
25415#endif
25416
25417 memsys5Link(iBlock, iLogsize);
25418}
25419
25420/*
25421** Allocate nBytes of memory.
25422*/
25423static void *memsys5Malloc(int nBytes){
25424 sqlite3_int64 *p = 0;
25425 if( nBytes>0 ){
25426 memsys5Enter();
25427 p = memsys5MallocUnsafe(nBytes);
25428 memsys5Leave();
25429 }
25430 return (void*)p;
25431}
25432
25433/*
25434** Free memory.
25435**
25436** The outer layer memory allocator prevents this routine from
25437** being called with pPrior==0.
25438*/
25439static void memsys5Free(void *pPrior){
25440 assert( pPrior!=0 );
25441 memsys5Enter();
25442 memsys5FreeUnsafe(pPrior);
25443 memsys5Leave();
25444}
25445
25446/*
25447** Change the size of an existing memory allocation.
25448**
25449** The outer layer memory allocator prevents this routine from
25450** being called with pPrior==0.
25451**
25452** nBytes is always a value obtained from a prior call to
25453** memsys5Round(). Hence nBytes is always a non-negative power
25454** of two. If nBytes==0 that means that an oversize allocation
25455** (an allocation larger than 0x40000000) was requested and this
25456** routine should return 0 without freeing pPrior.
25457*/
25458static void *memsys5Realloc(void *pPrior, int nBytes){
25459 int nOld;
25460 void *p;
25461 assert( pPrior!=0 );
25462 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
25463 assert( nBytes>=0 );
25464 if( nBytes==0 ){
25465 return 0;
25466 }
25467 nOld = memsys5Size(pPrior);
25468 if( nBytes<=nOld ){
25469 return pPrior;
25470 }
25471 p = memsys5Malloc(nBytes);
25472 if( p ){
25473 memcpy(p, pPrior, nOld);
25474 memsys5Free(pPrior);
25475 }
25476 return p;
25477}
25478
25479/*
25480** Round up a request size to the next valid allocation size. If
25481** the allocation is too large to be handled by this allocation system,
25482** return 0.
25483**
25484** All allocations must be a power of two and must be expressed by a
25485** 32-bit signed integer. Hence the largest allocation is 0x40000000
25486** or 1073741824 bytes.
25487*/
25488static int memsys5Roundup(int n){
25489 int iFullSz;
25490 if( n > 0x40000000 ) return 0;
25491 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
25492 return iFullSz;
25493}
25494
25495/*
25496** Return the ceiling of the logarithm base 2 of iValue.
25497**
25498** Examples: memsys5Log(1) -> 0
25499** memsys5Log(2) -> 1
25500** memsys5Log(4) -> 2
25501** memsys5Log(5) -> 3
25502** memsys5Log(8) -> 3
25503** memsys5Log(9) -> 4
25504*/
25505static int memsys5Log(int iValue){
25506 int iLog;
25507 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
25508 return iLog;
25509}
25510
25511/*
25512** Initialize the memory allocator.
25513**
25514** This routine is not threadsafe. The caller must be holding a mutex
25515** to prevent multiple threads from entering at the same time.
25516*/
25517static int memsys5Init(void *NotUsed){
25518 int ii; /* Loop counter */
25519 int nByte; /* Number of bytes of memory available to this allocator */
25520 u8 *zByte; /* Memory usable by this allocator */
25521 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
25522 int iOffset; /* An offset into mem5.aCtrl[] */
25523
25524 UNUSED_PARAMETER(NotUsed);
25525
25526 /* For the purposes of this routine, disable the mutex */
25527 mem5.mutex = 0;
25528
25529 /* The size of a Mem5Link object must be a power of two. Verify that
25530 ** this is case.
25531 */
25532 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
25533
25534 nByte = sqlite3GlobalConfig.nHeap;
25535 zByte = (u8*)sqlite3GlobalConfig.pHeap;
25536 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
25537
25538 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
25539 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
25540 mem5.szAtom = (1<<nMinLog);
25541 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
25542 mem5.szAtom = mem5.szAtom << 1;
25543 }
25544
25545 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
25546 mem5.zPool = zByte;
25547 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
25548
25549 for(ii=0; ii<=LOGMAX; ii++){
25550 mem5.aiFreelist[ii] = -1;
25551 }
25552
25553 iOffset = 0;
25554 for(ii=LOGMAX; ii>=0; ii--){
25555 int nAlloc = (1<<ii);
25556 if( (iOffset+nAlloc)<=mem5.nBlock ){
25557 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
25558 memsys5Link(iOffset, ii);
25559 iOffset += nAlloc;
25560 }
25561 assert((iOffset+nAlloc)>mem5.nBlock);
25562 }
25563
25564 /* If a mutex is required for normal operation, allocate one */
25565 if( sqlite3GlobalConfig.bMemstat==0 ){
25566 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
25567 }
25568
25569 return SQLITE_OK;
25570}
25571
25572/*
25573** Deinitialize this module.
25574*/
25575static void memsys5Shutdown(void *NotUsed){
25576 UNUSED_PARAMETER(NotUsed);
25577 mem5.mutex = 0;
25578 return;
25579}
25580
25581#ifdef SQLITE_TEST
25582/*
25583** Open the file indicated and write a log of all unfreed memory
25584** allocations into that log.
25585*/
25586SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
25587 FILE *out;
25588 int i, j, n;
25589 int nMinLog;
25590
25591 if( zFilename==0 || zFilename[0]==0 ){
25592 out = stdout;
25593 }else{
25594 out = fopen(zFilename, "w");
25595 if( out==0 ){
25596 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
25597 zFilename);
25598 return;
25599 }
25600 }
25601 memsys5Enter();
25602 nMinLog = memsys5Log(mem5.szAtom);
25603 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
25604 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
25605 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
25606 }
25607 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
25608 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
25609 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
25610 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
25611 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
25612 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
25613 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
25614 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
25615 memsys5Leave();
25616 if( out==stdout ){
25617 fflush(stdout);
25618 }else{
25619 fclose(out);
25620 }
25621}
25622#endif
25623
25624/*
25625** This routine is the only routine in this file with external
25626** linkage. It returns a pointer to a static sqlite3_mem_methods
25627** struct populated with the memsys5 methods.
25628*/
25629SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
25630 static const sqlite3_mem_methods memsys5Methods = {
25631 memsys5Malloc,
25632 memsys5Free,
25633 memsys5Realloc,
25634 memsys5Size,
25635 memsys5Roundup,
25636 memsys5Init,
25637 memsys5Shutdown,
25638 0
25639 };
25640 return &memsys5Methods;
25641}
25642
25643#endif /* SQLITE_ENABLE_MEMSYS5 */
25644
25645/************** End of mem5.c ************************************************/
25646/************** Begin file mutex.c *******************************************/
25647/*
25648** 2007 August 14
25649**
25650** The author disclaims copyright to this source code. In place of
25651** a legal notice, here is a blessing:
25652**
25653** May you do good and not evil.
25654** May you find forgiveness for yourself and forgive others.
25655** May you share freely, never taking more than you give.
25656**
25657*************************************************************************
25658** This file contains the C functions that implement mutexes.
25659**
25660** This file contains code that is common across all mutex implementations.
25661*/
25662/* #include "sqliteInt.h" */
25663
25664#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
25665/*
25666** For debugging purposes, record when the mutex subsystem is initialized
25667** and uninitialized so that we can assert() if there is an attempt to
25668** allocate a mutex while the system is uninitialized.
25669*/
25670static SQLITE_WSD int mutexIsInit = 0;
25671#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
25672
25673
25674#ifndef SQLITE_MUTEX_OMIT
25675
25676#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
25677/*
25678** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
25679** the implementation of a wrapper around the system default mutex
25680** implementation (sqlite3DefaultMutex()).
25681**
25682** Most calls are passed directly through to the underlying default
25683** mutex implementation. Except, if a mutex is configured by calling
25684** sqlite3MutexWarnOnContention() on it, then if contention is ever
25685** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
25686**
25687** This type of mutex is used as the database handle mutex when testing
25688** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
25689*/
25690
25691/*
25692** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
25693** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
25694** allocated by the system mutex implementation. Variable iType is usually set
25695** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
25696** or one of the static mutex identifiers. Or, if this is a recursive mutex
25697** that has been configured using sqlite3MutexWarnOnContention(), it is
25698** set to SQLITE_MUTEX_WARNONCONTENTION.
25699*/
25700typedef struct CheckMutex CheckMutex;
25701struct CheckMutex {
25702 int iType;
25703 sqlite3_mutex *mutex;
25704};
25705
25706#define SQLITE_MUTEX_WARNONCONTENTION (-1)
25707
25708/*
25709** Pointer to real mutex methods object used by the CheckMutex
25710** implementation. Set by checkMutexInit().
25711*/
25712static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
25713
25714#ifdef SQLITE_DEBUG
25715static int checkMutexHeld(sqlite3_mutex *p){
25716 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
25717}
25718static int checkMutexNotheld(sqlite3_mutex *p){
25719 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
25720}
25721#endif
25722
25723/*
25724** Initialize and deinitialize the mutex subsystem.
25725*/
25726static int checkMutexInit(void){
25727 pGlobalMutexMethods = sqlite3DefaultMutex();
25728 return SQLITE_OK;
25729}
25730static int checkMutexEnd(void){
25731 pGlobalMutexMethods = 0;
25732 return SQLITE_OK;
25733}
25734
25735/*
25736** Allocate a mutex.
25737*/
25738static sqlite3_mutex *checkMutexAlloc(int iType){
25739 static CheckMutex staticMutexes[] = {
25740 {2, 0}, {3, 0}, {4, 0}, {5, 0},
25741 {6, 0}, {7, 0}, {8, 0}, {9, 0},
25742 {10, 0}, {11, 0}, {12, 0}, {13, 0}
25743 };
25744 CheckMutex *p = 0;
25745
25746 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
25747 if( iType<2 ){
25748 p = sqlite3MallocZero(sizeof(CheckMutex));
25749 if( p==0 ) return 0;
25750 p->iType = iType;
25751 }else{
25752#ifdef SQLITE_ENABLE_API_ARMOR
25753 if( iType-2>=ArraySize(staticMutexes) ){
25754 (void)SQLITE_MISUSE_BKPT;
25755 return 0;
25756 }
25757#endif
25758 p = &staticMutexes[iType-2];
25759 }
25760
25761 if( p->mutex==0 ){
25762 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
25763 if( p->mutex==0 ){
25764 if( iType<2 ){
25765 sqlite3_free(p);
25766 }
25767 p = 0;
25768 }
25769 }
25770
25771 return (sqlite3_mutex*)p;
25772}
25773
25774/*
25775** Free a mutex.
25776*/
25777static void checkMutexFree(sqlite3_mutex *p){
25778 assert( SQLITE_MUTEX_RECURSIVE<2 );
25779 assert( SQLITE_MUTEX_FAST<2 );
25780 assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
25781
25782#if SQLITE_ENABLE_API_ARMOR
25783 if( ((CheckMutex*)p)->iType<2 )
25784#endif
25785 {
25786 CheckMutex *pCheck = (CheckMutex*)p;
25787 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
25788 sqlite3_free(pCheck);
25789 }
25790#ifdef SQLITE_ENABLE_API_ARMOR
25791 else{
25792 (void)SQLITE_MISUSE_BKPT;
25793 }
25794#endif
25795}
25796
25797/*
25798** Enter the mutex.
25799*/
25800static void checkMutexEnter(sqlite3_mutex *p){
25801 CheckMutex *pCheck = (CheckMutex*)p;
25802 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
25803 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
25804 return;
25805 }
25806 sqlite3_log(SQLITE_MISUSE,
25807 "illegal multi-threaded access to database connection"
25808 );
25809 }
25810 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
25811}
25812
25813/*
25814** Enter the mutex (do not block).
25815*/
25816static int checkMutexTry(sqlite3_mutex *p){
25817 CheckMutex *pCheck = (CheckMutex*)p;
25818 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
25819}
25820
25821/*
25822** Leave the mutex.
25823*/
25824static void checkMutexLeave(sqlite3_mutex *p){
25825 CheckMutex *pCheck = (CheckMutex*)p;
25826 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
25827}
25828
25829sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
25830 static const sqlite3_mutex_methods sMutex = {
25831 checkMutexInit,
25832 checkMutexEnd,
25833 checkMutexAlloc,
25834 checkMutexFree,
25835 checkMutexEnter,
25836 checkMutexTry,
25837 checkMutexLeave,
25838#ifdef SQLITE_DEBUG
25839 checkMutexHeld,
25840 checkMutexNotheld
25841#else
25842 0,
25843 0
25844#endif
25845 };
25846 return &sMutex;
25847}
25848
25849/*
25850** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
25851** one on which there should be no contention.
25852*/
25853SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
25854 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
25855 CheckMutex *pCheck = (CheckMutex*)p;
25856 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
25857 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
25858 }
25859}
25860#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
25861
25862/*
25863** Initialize the mutex system.
25864*/
25865SQLITE_PRIVATE int sqlite3MutexInit(void){
25866 int rc = SQLITE_OK;
25867 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
25868 /* If the xMutexAlloc method has not been set, then the user did not
25869 ** install a mutex implementation via sqlite3_config() prior to
25870 ** sqlite3_initialize() being called. This block copies pointers to
25871 ** the default implementation into the sqlite3GlobalConfig structure.
25872 */
25873 sqlite3_mutex_methods const *pFrom;
25874 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
25875
25876 if( sqlite3GlobalConfig.bCoreMutex ){
25877#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
25878 pFrom = multiThreadedCheckMutex();
25879#else
25880 pFrom = sqlite3DefaultMutex();
25881#endif
25882 }else{
25883 pFrom = sqlite3NoopMutex();
25884 }
25885 pTo->xMutexInit = pFrom->xMutexInit;
25886 pTo->xMutexEnd = pFrom->xMutexEnd;
25887 pTo->xMutexFree = pFrom->xMutexFree;
25888 pTo->xMutexEnter = pFrom->xMutexEnter;
25889 pTo->xMutexTry = pFrom->xMutexTry;
25890 pTo->xMutexLeave = pFrom->xMutexLeave;
25891 pTo->xMutexHeld = pFrom->xMutexHeld;
25892 pTo->xMutexNotheld = pFrom->xMutexNotheld;
25893 sqlite3MemoryBarrier();
25894 pTo->xMutexAlloc = pFrom->xMutexAlloc;
25895 }
25896 assert( sqlite3GlobalConfig.mutex.xMutexInit );
25897 rc = sqlite3GlobalConfig.mutex.xMutexInit();
25898
25899#ifdef SQLITE_DEBUG
25900 GLOBAL(int, mutexIsInit) = 1;
25901#endif
25902
25903 sqlite3MemoryBarrier();
25904 return rc;
25905}
25906
25907/*
25908** Shutdown the mutex system. This call frees resources allocated by
25909** sqlite3MutexInit().
25910*/
25911SQLITE_PRIVATE int sqlite3MutexEnd(void){
25912 int rc = SQLITE_OK;
25913 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
25914 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
25915 }
25916
25917#ifdef SQLITE_DEBUG
25918 GLOBAL(int, mutexIsInit) = 0;
25919#endif
25920
25921 return rc;
25922}
25923
25924/*
25925** Retrieve a pointer to a static mutex or allocate a new dynamic one.
25926*/
25927SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
25928#ifndef SQLITE_OMIT_AUTOINIT
25929 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
25930 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
25931#endif
25932 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
25933 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
25934}
25935
25936SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
25937 if( !sqlite3GlobalConfig.bCoreMutex ){
25938 return 0;
25939 }
25940 assert( GLOBAL(int, mutexIsInit) );
25941 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
25942 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
25943}
25944
25945/*
25946** Free a dynamic mutex.
25947*/
25948SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
25949 if( p ){
25950 assert( sqlite3GlobalConfig.mutex.xMutexFree );
25951 sqlite3GlobalConfig.mutex.xMutexFree(p);
25952 }
25953}
25954
25955/*
25956** Obtain the mutex p. If some other thread already has the mutex, block
25957** until it can be obtained.
25958*/
25959SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
25960 if( p ){
25961 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
25962 sqlite3GlobalConfig.mutex.xMutexEnter(p);
25963 }
25964}
25965
25966/*
25967** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
25968** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
25969*/
25970SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
25971 int rc = SQLITE_OK;
25972 if( p ){
25973 assert( sqlite3GlobalConfig.mutex.xMutexTry );
25974 return sqlite3GlobalConfig.mutex.xMutexTry(p);
25975 }
25976 return rc;
25977}
25978
25979/*
25980** The sqlite3_mutex_leave() routine exits a mutex that was previously
25981** entered by the same thread. The behavior is undefined if the mutex
25982** is not currently entered. If a NULL pointer is passed as an argument
25983** this function is a no-op.
25984*/
25985SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
25986 if( p ){
25987 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
25988 sqlite3GlobalConfig.mutex.xMutexLeave(p);
25989 }
25990}
25991
25992#ifndef NDEBUG
25993/*
25994** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25995** intended for use inside assert() statements.
25996*/
25997SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
25998 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
25999 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
26000}
26001SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
26002 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
26003 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
26004}
26005#endif
26006
26007#endif /* !defined(SQLITE_MUTEX_OMIT) */
26008
26009/************** End of mutex.c ***********************************************/
26010/************** Begin file mutex_noop.c **************************************/
26011/*
26012** 2008 October 07
26013**
26014** The author disclaims copyright to this source code. In place of
26015** a legal notice, here is a blessing:
26016**
26017** May you do good and not evil.
26018** May you find forgiveness for yourself and forgive others.
26019** May you share freely, never taking more than you give.
26020**
26021*************************************************************************
26022** This file contains the C functions that implement mutexes.
26023**
26024** This implementation in this file does not provide any mutual
26025** exclusion and is thus suitable for use only in applications
26026** that use SQLite in a single thread. The routines defined
26027** here are place-holders. Applications can substitute working
26028** mutex routines at start-time using the
26029**
26030** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
26031**
26032** interface.
26033**
26034** If compiled with SQLITE_DEBUG, then additional logic is inserted
26035** that does error checking on mutexes to make sure they are being
26036** called correctly.
26037*/
26038/* #include "sqliteInt.h" */
26039
26040#ifndef SQLITE_MUTEX_OMIT
26041
26042#ifndef SQLITE_DEBUG
26043/*
26044** Stub routines for all mutex methods.
26045**
26046** This routines provide no mutual exclusion or error checking.
26047*/
26048static int noopMutexInit(void){ return SQLITE_OK; }
26049static int noopMutexEnd(void){ return SQLITE_OK; }
26050static sqlite3_mutex *noopMutexAlloc(int id){
26051 UNUSED_PARAMETER(id);
26052 return (sqlite3_mutex*)8;
26053}
26054static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26055static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26056static int noopMutexTry(sqlite3_mutex *p){
26057 UNUSED_PARAMETER(p);
26058 return SQLITE_OK;
26059}
26060static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
26061
26062SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
26063 static const sqlite3_mutex_methods sMutex = {
26064 noopMutexInit,
26065 noopMutexEnd,
26066 noopMutexAlloc,
26067 noopMutexFree,
26068 noopMutexEnter,
26069 noopMutexTry,
26070 noopMutexLeave,
26071
26072 0,
26073 0,
26074 };
26075
26076 return &sMutex;
26077}
26078#endif /* !SQLITE_DEBUG */
26079
26080#ifdef SQLITE_DEBUG
26081/*
26082** In this implementation, error checking is provided for testing
26083** and debugging purposes. The mutexes still do not provide any
26084** mutual exclusion.
26085*/
26086
26087/*
26088** The mutex object
26089*/
26090typedef struct sqlite3_debug_mutex {
26091 int id; /* The mutex type */
26092 int cnt; /* Number of entries without a matching leave */
26093} sqlite3_debug_mutex;
26094
26095/*
26096** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26097** intended for use inside assert() statements.
26098*/
26099static int debugMutexHeld(sqlite3_mutex *pX){
26100 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26101 return p==0 || p->cnt>0;
26102}
26103static int debugMutexNotheld(sqlite3_mutex *pX){
26104 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26105 return p==0 || p->cnt==0;
26106}
26107
26108/*
26109** Initialize and deinitialize the mutex subsystem.
26110*/
26111static int debugMutexInit(void){ return SQLITE_OK; }
26112static int debugMutexEnd(void){ return SQLITE_OK; }
26113
26114/*
26115** The sqlite3_mutex_alloc() routine allocates a new
26116** mutex and returns a pointer to it. If it returns NULL
26117** that means that a mutex could not be allocated.
26118*/
26119static sqlite3_mutex *debugMutexAlloc(int id){
26120 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
26121 sqlite3_debug_mutex *pNew = 0;
26122 switch( id ){
26123 case SQLITE_MUTEX_FAST:
26124 case SQLITE_MUTEX_RECURSIVE: {
26125 pNew = sqlite3Malloc(sizeof(*pNew));
26126 if( pNew ){
26127 pNew->id = id;
26128 pNew->cnt = 0;
26129 }
26130 break;
26131 }
26132 default: {
26133#ifdef SQLITE_ENABLE_API_ARMOR
26134 if( id-2<0 || id-2>=ArraySize(aStatic) ){
26135 (void)SQLITE_MISUSE_BKPT;
26136 return 0;
26137 }
26138#endif
26139 pNew = &aStatic[id-2];
26140 pNew->id = id;
26141 break;
26142 }
26143 }
26144 return (sqlite3_mutex*)pNew;
26145}
26146
26147/*
26148** This routine deallocates a previously allocated mutex.
26149*/
26150static void debugMutexFree(sqlite3_mutex *pX){
26151 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26152 assert( p->cnt==0 );
26153 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
26154 sqlite3_free(p);
26155 }else{
26156#ifdef SQLITE_ENABLE_API_ARMOR
26157 (void)SQLITE_MISUSE_BKPT;
26158#endif
26159 }
26160}
26161
26162/*
26163** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26164** to enter a mutex. If another thread is already within the mutex,
26165** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26166** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
26167** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
26168** be entered multiple times by the same thread. In such cases the,
26169** mutex must be exited an equal number of times before another thread
26170** can enter. If the same thread tries to enter any other kind of mutex
26171** more than once, the behavior is undefined.
26172*/
26173static void debugMutexEnter(sqlite3_mutex *pX){
26174 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26175 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26176 p->cnt++;
26177}
26178static int debugMutexTry(sqlite3_mutex *pX){
26179 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26180 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26181 p->cnt++;
26182 return SQLITE_OK;
26183}
26184
26185/*
26186** The sqlite3_mutex_leave() routine exits a mutex that was
26187** previously entered by the same thread. The behavior
26188** is undefined if the mutex is not currently entered or
26189** is not currently allocated. SQLite will never do either.
26190*/
26191static void debugMutexLeave(sqlite3_mutex *pX){
26192 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
26193 assert( debugMutexHeld(pX) );
26194 p->cnt--;
26195 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
26196}
26197
26198SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
26199 static const sqlite3_mutex_methods sMutex = {
26200 debugMutexInit,
26201 debugMutexEnd,
26202 debugMutexAlloc,
26203 debugMutexFree,
26204 debugMutexEnter,
26205 debugMutexTry,
26206 debugMutexLeave,
26207
26208 debugMutexHeld,
26209 debugMutexNotheld
26210 };
26211
26212 return &sMutex;
26213}
26214#endif /* SQLITE_DEBUG */
26215
26216/*
26217** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
26218** is used regardless of the run-time threadsafety setting.
26219*/
26220#ifdef SQLITE_MUTEX_NOOP
26221SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26222 return sqlite3NoopMutex();
26223}
26224#endif /* defined(SQLITE_MUTEX_NOOP) */
26225#endif /* !defined(SQLITE_MUTEX_OMIT) */
26226
26227/************** End of mutex_noop.c ******************************************/
26228/************** Begin file mutex_unix.c **************************************/
26229/*
26230** 2007 August 28
26231**
26232** The author disclaims copyright to this source code. In place of
26233** a legal notice, here is a blessing:
26234**
26235** May you do good and not evil.
26236** May you find forgiveness for yourself and forgive others.
26237** May you share freely, never taking more than you give.
26238**
26239*************************************************************************
26240** This file contains the C functions that implement mutexes for pthreads
26241*/
26242/* #include "sqliteInt.h" */
26243
26244/*
26245** The code in this file is only used if we are compiling threadsafe
26246** under unix with pthreads.
26247**
26248** Note that this implementation requires a version of pthreads that
26249** supports recursive mutexes.
26250*/
26251#ifdef SQLITE_MUTEX_PTHREADS
26252
26253#include <pthread.h>
26254
26255/*
26256** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
26257** are necessary under two condidtions: (1) Debug builds and (2) using
26258** home-grown mutexes. Encapsulate these conditions into a single #define.
26259*/
26260#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
26261# define SQLITE_MUTEX_NREF 1
26262#else
26263# define SQLITE_MUTEX_NREF 0
26264#endif
26265
26266/*
26267** Each recursive mutex is an instance of the following structure.
26268*/
26269struct sqlite3_mutex {
26270 pthread_mutex_t mutex; /* Mutex controlling the lock */
26271#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26272 int id; /* Mutex type */
26273#endif
26274#if SQLITE_MUTEX_NREF
26275 volatile int nRef; /* Number of entrances */
26276 volatile pthread_t owner; /* Thread that is within this mutex */
26277 int trace; /* True to trace changes */
26278#endif
26279};
26280#if SQLITE_MUTEX_NREF
26281# define SQLITE3_MUTEX_INITIALIZER(id) \
26282 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
26283#elif defined(SQLITE_ENABLE_API_ARMOR)
26284# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
26285#else
26286#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
26287#endif
26288
26289/*
26290** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26291** intended for use only inside assert() statements. On some platforms,
26292** there might be race conditions that can cause these routines to
26293** deliver incorrect results. In particular, if pthread_equal() is
26294** not an atomic operation, then these routines might delivery
26295** incorrect results. On most platforms, pthread_equal() is a
26296** comparison of two integers and is therefore atomic. But we are
26297** told that HPUX is not such a platform. If so, then these routines
26298** will not always work correctly on HPUX.
26299**
26300** On those platforms where pthread_equal() is not atomic, SQLite
26301** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
26302** make sure no assert() statements are evaluated and hence these
26303** routines are never called.
26304*/
26305#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
26306static int pthreadMutexHeld(sqlite3_mutex *p){
26307 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
26308}
26309static int pthreadMutexNotheld(sqlite3_mutex *p){
26310 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
26311}
26312#endif
26313
26314/*
26315** Try to provide a memory barrier operation, needed for initialization
26316** and also for the implementation of xShmBarrier in the VFS in cases
26317** where SQLite is compiled without mutexes.
26318*/
26319SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
26320#if defined(SQLITE_MEMORY_BARRIER)
26321 SQLITE_MEMORY_BARRIER;
26322#elif defined(__GNUC__) && GCC_VERSION>=4001000
26323 __sync_synchronize();
26324#endif
26325}
26326
26327/*
26328** Initialize and deinitialize the mutex subsystem.
26329*/
26330static int pthreadMutexInit(void){ return SQLITE_OK; }
26331static int pthreadMutexEnd(void){ return SQLITE_OK; }
26332
26333/*
26334** The sqlite3_mutex_alloc() routine allocates a new
26335** mutex and returns a pointer to it. If it returns NULL
26336** that means that a mutex could not be allocated. SQLite
26337** will unwind its stack and return an error. The argument
26338** to sqlite3_mutex_alloc() is one of these integer constants:
26339**
26340** <ul>
26341** <li> SQLITE_MUTEX_FAST
26342** <li> SQLITE_MUTEX_RECURSIVE
26343** <li> SQLITE_MUTEX_STATIC_MAIN
26344** <li> SQLITE_MUTEX_STATIC_MEM
26345** <li> SQLITE_MUTEX_STATIC_OPEN
26346** <li> SQLITE_MUTEX_STATIC_PRNG
26347** <li> SQLITE_MUTEX_STATIC_LRU
26348** <li> SQLITE_MUTEX_STATIC_PMEM
26349** <li> SQLITE_MUTEX_STATIC_APP1
26350** <li> SQLITE_MUTEX_STATIC_APP2
26351** <li> SQLITE_MUTEX_STATIC_APP3
26352** <li> SQLITE_MUTEX_STATIC_VFS1
26353** <li> SQLITE_MUTEX_STATIC_VFS2
26354** <li> SQLITE_MUTEX_STATIC_VFS3
26355** </ul>
26356**
26357** The first two constants cause sqlite3_mutex_alloc() to create
26358** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
26359** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
26360** The mutex implementation does not need to make a distinction
26361** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
26362** not want to. But SQLite will only request a recursive mutex in
26363** cases where it really needs one. If a faster non-recursive mutex
26364** implementation is available on the host platform, the mutex subsystem
26365** might return such a mutex in response to SQLITE_MUTEX_FAST.
26366**
26367** The other allowed parameters to sqlite3_mutex_alloc() each return
26368** a pointer to a static preexisting mutex. Six static mutexes are
26369** used by the current version of SQLite. Future versions of SQLite
26370** may add additional static mutexes. Static mutexes are for internal
26371** use by SQLite only. Applications that use SQLite mutexes should
26372** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
26373** SQLITE_MUTEX_RECURSIVE.
26374**
26375** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
26376** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
26377** returns a different mutex on every call. But for the static
26378** mutex types, the same mutex is returned on every call that has
26379** the same type number.
26380*/
26381static sqlite3_mutex *pthreadMutexAlloc(int iType){
26382 static sqlite3_mutex staticMutexes[] = {
26383 SQLITE3_MUTEX_INITIALIZER(2),
26384 SQLITE3_MUTEX_INITIALIZER(3),
26385 SQLITE3_MUTEX_INITIALIZER(4),
26386 SQLITE3_MUTEX_INITIALIZER(5),
26387 SQLITE3_MUTEX_INITIALIZER(6),
26388 SQLITE3_MUTEX_INITIALIZER(7),
26389 SQLITE3_MUTEX_INITIALIZER(8),
26390 SQLITE3_MUTEX_INITIALIZER(9),
26391 SQLITE3_MUTEX_INITIALIZER(10),
26392 SQLITE3_MUTEX_INITIALIZER(11),
26393 SQLITE3_MUTEX_INITIALIZER(12),
26394 SQLITE3_MUTEX_INITIALIZER(13)
26395 };
26396 sqlite3_mutex *p;
26397 switch( iType ){
26398 case SQLITE_MUTEX_RECURSIVE: {
26399 p = sqlite3MallocZero( sizeof(*p) );
26400 if( p ){
26401#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26402 /* If recursive mutexes are not available, we will have to
26403 ** build our own. See below. */
26404 pthread_mutex_init(&p->mutex, 0);
26405#else
26406 /* Use a recursive mutex if it is available */
26407 pthread_mutexattr_t recursiveAttr;
26408 pthread_mutexattr_init(&recursiveAttr);
26409 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
26410 pthread_mutex_init(&p->mutex, &recursiveAttr);
26411 pthread_mutexattr_destroy(&recursiveAttr);
26412#endif
26413#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26414 p->id = SQLITE_MUTEX_RECURSIVE;
26415#endif
26416 }
26417 break;
26418 }
26419 case SQLITE_MUTEX_FAST: {
26420 p = sqlite3MallocZero( sizeof(*p) );
26421 if( p ){
26422 pthread_mutex_init(&p->mutex, 0);
26423#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26424 p->id = SQLITE_MUTEX_FAST;
26425#endif
26426 }
26427 break;
26428 }
26429 default: {
26430#ifdef SQLITE_ENABLE_API_ARMOR
26431 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
26432 (void)SQLITE_MISUSE_BKPT;
26433 return 0;
26434 }
26435#endif
26436 p = &staticMutexes[iType-2];
26437 break;
26438 }
26439 }
26440#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
26441 assert( p==0 || p->id==iType );
26442#endif
26443 return p;
26444}
26445
26446
26447/*
26448** This routine deallocates a previously
26449** allocated mutex. SQLite is careful to deallocate every
26450** mutex that it allocates.
26451*/
26452static void pthreadMutexFree(sqlite3_mutex *p){
26453 assert( p->nRef==0 );
26454#if SQLITE_ENABLE_API_ARMOR
26455 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
26456#endif
26457 {
26458 pthread_mutex_destroy(&p->mutex);
26459 sqlite3_free(p);
26460 }
26461#ifdef SQLITE_ENABLE_API_ARMOR
26462 else{
26463 (void)SQLITE_MISUSE_BKPT;
26464 }
26465#endif
26466}
26467
26468/*
26469** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26470** to enter a mutex. If another thread is already within the mutex,
26471** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26472** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
26473** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
26474** be entered multiple times by the same thread. In such cases the,
26475** mutex must be exited an equal number of times before another thread
26476** can enter. If the same thread tries to enter any other kind of mutex
26477** more than once, the behavior is undefined.
26478*/
26479static void pthreadMutexEnter(sqlite3_mutex *p){
26480 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
26481
26482#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26483 /* If recursive mutexes are not available, then we have to grow
26484 ** our own. This implementation assumes that pthread_equal()
26485 ** is atomic - that it cannot be deceived into thinking self
26486 ** and p->owner are equal if p->owner changes between two values
26487 ** that are not equal to self while the comparison is taking place.
26488 ** This implementation also assumes a coherent cache - that
26489 ** separate processes cannot read different values from the same
26490 ** address at the same time. If either of these two conditions
26491 ** are not met, then the mutexes will fail and problems will result.
26492 */
26493 {
26494 pthread_t self = pthread_self();
26495 if( p->nRef>0 && pthread_equal(p->owner, self) ){
26496 p->nRef++;
26497 }else{
26498 pthread_mutex_lock(&p->mutex);
26499 assert( p->nRef==0 );
26500 p->owner = self;
26501 p->nRef = 1;
26502 }
26503 }
26504#else
26505 /* Use the built-in recursive mutexes if they are available.
26506 */
26507 pthread_mutex_lock(&p->mutex);
26508#if SQLITE_MUTEX_NREF
26509 assert( p->nRef>0 || p->owner==0 );
26510 p->owner = pthread_self();
26511 p->nRef++;
26512#endif
26513#endif
26514
26515#ifdef SQLITE_DEBUG
26516 if( p->trace ){
26517 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26518 }
26519#endif
26520}
26521static int pthreadMutexTry(sqlite3_mutex *p){
26522 int rc;
26523 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
26524
26525#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26526 /* If recursive mutexes are not available, then we have to grow
26527 ** our own. This implementation assumes that pthread_equal()
26528 ** is atomic - that it cannot be deceived into thinking self
26529 ** and p->owner are equal if p->owner changes between two values
26530 ** that are not equal to self while the comparison is taking place.
26531 ** This implementation also assumes a coherent cache - that
26532 ** separate processes cannot read different values from the same
26533 ** address at the same time. If either of these two conditions
26534 ** are not met, then the mutexes will fail and problems will result.
26535 */
26536 {
26537 pthread_t self = pthread_self();
26538 if( p->nRef>0 && pthread_equal(p->owner, self) ){
26539 p->nRef++;
26540 rc = SQLITE_OK;
26541 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
26542 assert( p->nRef==0 );
26543 p->owner = self;
26544 p->nRef = 1;
26545 rc = SQLITE_OK;
26546 }else{
26547 rc = SQLITE_BUSY;
26548 }
26549 }
26550#else
26551 /* Use the built-in recursive mutexes if they are available.
26552 */
26553 if( pthread_mutex_trylock(&p->mutex)==0 ){
26554#if SQLITE_MUTEX_NREF
26555 p->owner = pthread_self();
26556 p->nRef++;
26557#endif
26558 rc = SQLITE_OK;
26559 }else{
26560 rc = SQLITE_BUSY;
26561 }
26562#endif
26563
26564#ifdef SQLITE_DEBUG
26565 if( rc==SQLITE_OK && p->trace ){
26566 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26567 }
26568#endif
26569 return rc;
26570}
26571
26572/*
26573** The sqlite3_mutex_leave() routine exits a mutex that was
26574** previously entered by the same thread. The behavior
26575** is undefined if the mutex is not currently entered or
26576** is not currently allocated. SQLite will never do either.
26577*/
26578static void pthreadMutexLeave(sqlite3_mutex *p){
26579 assert( pthreadMutexHeld(p) );
26580#if SQLITE_MUTEX_NREF
26581 p->nRef--;
26582 if( p->nRef==0 ) p->owner = 0;
26583#endif
26584 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
26585
26586#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
26587 if( p->nRef==0 ){
26588 pthread_mutex_unlock(&p->mutex);
26589 }
26590#else
26591 pthread_mutex_unlock(&p->mutex);
26592#endif
26593
26594#ifdef SQLITE_DEBUG
26595 if( p->trace ){
26596 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
26597 }
26598#endif
26599}
26600
26601SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26602 static const sqlite3_mutex_methods sMutex = {
26603 pthreadMutexInit,
26604 pthreadMutexEnd,
26605 pthreadMutexAlloc,
26606 pthreadMutexFree,
26607 pthreadMutexEnter,
26608 pthreadMutexTry,
26609 pthreadMutexLeave,
26610#ifdef SQLITE_DEBUG
26611 pthreadMutexHeld,
26612 pthreadMutexNotheld
26613#else
26614 0,
26615 0
26616#endif
26617 };
26618
26619 return &sMutex;
26620}
26621
26622#endif /* SQLITE_MUTEX_PTHREADS */
26623
26624/************** End of mutex_unix.c ******************************************/
26625/************** Begin file mutex_w32.c ***************************************/
26626/*
26627** 2007 August 14
26628**
26629** The author disclaims copyright to this source code. In place of
26630** a legal notice, here is a blessing:
26631**
26632** May you do good and not evil.
26633** May you find forgiveness for yourself and forgive others.
26634** May you share freely, never taking more than you give.
26635**
26636*************************************************************************
26637** This file contains the C functions that implement mutexes for Win32.
26638*/
26639/* #include "sqliteInt.h" */
26640
26641#if SQLITE_OS_WIN
26642/*
26643** Include code that is common to all os_*.c files
26644*/
26645/************** Include os_common.h in the middle of mutex_w32.c *************/
26646/************** Begin file os_common.h ***************************************/
26647/*
26648** 2004 May 22
26649**
26650** The author disclaims copyright to this source code. In place of
26651** a legal notice, here is a blessing:
26652**
26653** May you do good and not evil.
26654** May you find forgiveness for yourself and forgive others.
26655** May you share freely, never taking more than you give.
26656**
26657******************************************************************************
26658**
26659** This file contains macros and a little bit of code that is common to
26660** all of the platform-specific files (os_*.c) and is #included into those
26661** files.
26662**
26663** This file should be #included by the os_*.c files only. It is not a
26664** general purpose header file.
26665*/
26666#ifndef _OS_COMMON_H_
26667#define _OS_COMMON_H_
26668
26669/*
26670** At least two bugs have slipped in because we changed the MEMORY_DEBUG
26671** macro to SQLITE_DEBUG and some older makefiles have not yet made the
26672** switch. The following code should catch this problem at compile-time.
26673*/
26674#ifdef MEMORY_DEBUG
26675# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
26676#endif
26677
26678/*
26679** Macros for performance tracing. Normally turned off. Only works
26680** on i486 hardware.
26681*/
26682#ifdef SQLITE_PERFORMANCE_TRACE
26683
26684/*
26685** hwtime.h contains inline assembler code for implementing
26686** high-performance timing routines.
26687*/
26688/************** Include hwtime.h in the middle of os_common.h ****************/
26689/************** Begin file hwtime.h ******************************************/
26690/*
26691** 2008 May 27
26692**
26693** The author disclaims copyright to this source code. In place of
26694** a legal notice, here is a blessing:
26695**
26696** May you do good and not evil.
26697** May you find forgiveness for yourself and forgive others.
26698** May you share freely, never taking more than you give.
26699**
26700******************************************************************************
26701**
26702** This file contains inline asm code for retrieving "high-performance"
26703** counters for x86 and x86_64 class CPUs.
26704*/
26705#ifndef SQLITE_HWTIME_H
26706#define SQLITE_HWTIME_H
26707
26708/*
26709** The following routine only works on pentium-class (or newer) processors.
26710** It uses the RDTSC opcode to read the cycle count value out of the
26711** processor and returns that value. This can be used for high-res
26712** profiling.
26713*/
26714#if !defined(__STRICT_ANSI__) && \
26715 (defined(__GNUC__) || defined(_MSC_VER)) && \
26716 (defined(i386) || defined(__i386__) || defined(_M_IX86))
26717
26718 #if defined(__GNUC__)
26719
26720 __inline__ sqlite_uint64 sqlite3Hwtime(void){
26721 unsigned int lo, hi;
26722 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
26723 return (sqlite_uint64)hi << 32 | lo;
26724 }
26725
26726 #elif defined(_MSC_VER)
26727
26728 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
26729 __asm {
26730 rdtsc
26731 ret ; return value at EDX:EAX
26732 }
26733 }
26734
26735 #endif
26736
26737#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
26738
26739 __inline__ sqlite_uint64 sqlite3Hwtime(void){
26740 unsigned long val;
26741 __asm__ __volatile__ ("rdtsc" : "=A" (val));
26742 return val;
26743 }
26744
26745#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
26746
26747 __inline__ sqlite_uint64 sqlite3Hwtime(void){
26748 unsigned long long retval;
26749 unsigned long junk;
26750 __asm__ __volatile__ ("\n\
26751 1: mftbu %1\n\
26752 mftb %L0\n\
26753 mftbu %0\n\
26754 cmpw %0,%1\n\
26755 bne 1b"
26756 : "=r" (retval), "=r" (junk));
26757 return retval;
26758 }
26759
26760#else
26761
26762 /*
26763 ** asm() is needed for hardware timing support. Without asm(),
26764 ** disable the sqlite3Hwtime() routine.
26765 **
26766 ** sqlite3Hwtime() is only used for some obscure debugging
26767 ** and analysis configurations, not in any deliverable, so this
26768 ** should not be a great loss.
26769 */
26770SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
26771
26772#endif
26773
26774#endif /* !defined(SQLITE_HWTIME_H) */
26775
26776/************** End of hwtime.h **********************************************/
26777/************** Continuing where we left off in os_common.h ******************/
26778
26779static sqlite_uint64 g_start;
26780static sqlite_uint64 g_elapsed;
26781#define TIMER_START g_start=sqlite3Hwtime()
26782#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
26783#define TIMER_ELAPSED g_elapsed
26784#else
26785#define TIMER_START
26786#define TIMER_END
26787#define TIMER_ELAPSED ((sqlite_uint64)0)
26788#endif
26789
26790/*
26791** If we compile with the SQLITE_TEST macro set, then the following block
26792** of code will give us the ability to simulate a disk I/O error. This
26793** is used for testing the I/O recovery logic.
26794*/
26795#if defined(SQLITE_TEST)
26796SQLITE_API extern int sqlite3_io_error_hit;
26797SQLITE_API extern int sqlite3_io_error_hardhit;
26798SQLITE_API extern int sqlite3_io_error_pending;
26799SQLITE_API extern int sqlite3_io_error_persist;
26800SQLITE_API extern int sqlite3_io_error_benign;
26801SQLITE_API extern int sqlite3_diskfull_pending;
26802SQLITE_API extern int sqlite3_diskfull;
26803#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
26804#define SimulateIOError(CODE) \
26805 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
26806 || sqlite3_io_error_pending-- == 1 ) \
26807 { local_ioerr(); CODE; }
26808static void local_ioerr(){
26809 IOTRACE(("IOERR\n"));
26810 sqlite3_io_error_hit++;
26811 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
26812}
26813#define SimulateDiskfullError(CODE) \
26814 if( sqlite3_diskfull_pending ){ \
26815 if( sqlite3_diskfull_pending == 1 ){ \
26816 local_ioerr(); \
26817 sqlite3_diskfull = 1; \
26818 sqlite3_io_error_hit = 1; \
26819 CODE; \
26820 }else{ \
26821 sqlite3_diskfull_pending--; \
26822 } \
26823 }
26824#else
26825#define SimulateIOErrorBenign(X)
26826#define SimulateIOError(A)
26827#define SimulateDiskfullError(A)
26828#endif /* defined(SQLITE_TEST) */
26829
26830/*
26831** When testing, keep a count of the number of open files.
26832*/
26833#if defined(SQLITE_TEST)
26834SQLITE_API extern int sqlite3_open_file_count;
26835#define OpenCounter(X) sqlite3_open_file_count+=(X)
26836#else
26837#define OpenCounter(X)
26838#endif /* defined(SQLITE_TEST) */
26839
26840#endif /* !defined(_OS_COMMON_H_) */
26841
26842/************** End of os_common.h *******************************************/
26843/************** Continuing where we left off in mutex_w32.c ******************/
26844
26845/*
26846** Include the header file for the Windows VFS.
26847*/
26848/************** Include os_win.h in the middle of mutex_w32.c ****************/
26849/************** Begin file os_win.h ******************************************/
26850/*
26851** 2013 November 25
26852**
26853** The author disclaims copyright to this source code. In place of
26854** a legal notice, here is a blessing:
26855**
26856** May you do good and not evil.
26857** May you find forgiveness for yourself and forgive others.
26858** May you share freely, never taking more than you give.
26859**
26860******************************************************************************
26861**
26862** This file contains code that is specific to Windows.
26863*/
26864#ifndef SQLITE_OS_WIN_H
26865#define SQLITE_OS_WIN_H
26866
26867/*
26868** Include the primary Windows SDK header file.
26869*/
26870#include "windows.h"
26871
26872#ifdef __CYGWIN__
26873# include <sys/cygwin.h>
26874# include <errno.h> /* amalgamator: dontcache */
26875#endif
26876
26877/*
26878** Determine if we are dealing with Windows NT.
26879**
26880** We ought to be able to determine if we are compiling for Windows 9x or
26881** Windows NT using the _WIN32_WINNT macro as follows:
26882**
26883** #if defined(_WIN32_WINNT)
26884** # define SQLITE_OS_WINNT 1
26885** #else
26886** # define SQLITE_OS_WINNT 0
26887** #endif
26888**
26889** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
26890** it ought to, so the above test does not work. We'll just assume that
26891** everything is Windows NT unless the programmer explicitly says otherwise
26892** by setting SQLITE_OS_WINNT to 0.
26893*/
26894#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
26895# define SQLITE_OS_WINNT 1
26896#endif
26897
26898/*
26899** Determine if we are dealing with Windows CE - which has a much reduced
26900** API.
26901*/
26902#if defined(_WIN32_WCE)
26903# define SQLITE_OS_WINCE 1
26904#else
26905# define SQLITE_OS_WINCE 0
26906#endif
26907
26908/*
26909** Determine if we are dealing with WinRT, which provides only a subset of
26910** the full Win32 API.
26911*/
26912#if !defined(SQLITE_OS_WINRT)
26913# define SQLITE_OS_WINRT 0
26914#endif
26915
26916/*
26917** For WinCE, some API function parameters do not appear to be declared as
26918** volatile.
26919*/
26920#if SQLITE_OS_WINCE
26921# define SQLITE_WIN32_VOLATILE
26922#else
26923# define SQLITE_WIN32_VOLATILE volatile
26924#endif
26925
26926/*
26927** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
26928** functions are not available (e.g. those not using MSVC, Cygwin, etc).
26929*/
26930#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
26931 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
26932# define SQLITE_OS_WIN_THREADS 1
26933#else
26934# define SQLITE_OS_WIN_THREADS 0
26935#endif
26936
26937#endif /* SQLITE_OS_WIN_H */
26938
26939/************** End of os_win.h **********************************************/
26940/************** Continuing where we left off in mutex_w32.c ******************/
26941#endif
26942
26943/*
26944** The code in this file is only used if we are compiling multithreaded
26945** on a Win32 system.
26946*/
26947#ifdef SQLITE_MUTEX_W32
26948
26949/*
26950** Each recursive mutex is an instance of the following structure.
26951*/
26952struct sqlite3_mutex {
26953 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
26954 int id; /* Mutex type */
26955#ifdef SQLITE_DEBUG
26956 volatile int nRef; /* Number of enterances */
26957 volatile DWORD owner; /* Thread holding this mutex */
26958 volatile LONG trace; /* True to trace changes */
26959#endif
26960};
26961
26962/*
26963** These are the initializer values used when declaring a "static" mutex
26964** on Win32. It should be noted that all mutexes require initialization
26965** on the Win32 platform.
26966*/
26967#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
26968
26969#ifdef SQLITE_DEBUG
26970#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
26971 0L, (DWORD)0, 0 }
26972#else
26973#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
26974#endif
26975
26976#ifdef SQLITE_DEBUG
26977/*
26978** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26979** intended for use only inside assert() statements.
26980*/
26981static int winMutexHeld(sqlite3_mutex *p){
26982 return p->nRef!=0 && p->owner==GetCurrentThreadId();
26983}
26984
26985static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
26986 return p->nRef==0 || p->owner!=tid;
26987}
26988
26989static int winMutexNotheld(sqlite3_mutex *p){
26990 DWORD tid = GetCurrentThreadId();
26991 return winMutexNotheld2(p, tid);
26992}
26993#endif
26994
26995/*
26996** Try to provide a memory barrier operation, needed for initialization
26997** and also for the xShmBarrier method of the VFS in cases when SQLite is
26998** compiled without mutexes (SQLITE_THREADSAFE=0).
26999*/
27000SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
27001#if defined(SQLITE_MEMORY_BARRIER)
27002 SQLITE_MEMORY_BARRIER;
27003#elif defined(__GNUC__)
27004 __sync_synchronize();
27005#elif MSVC_VERSION>=1300
27006 _ReadWriteBarrier();
27007#elif defined(MemoryBarrier)
27008 MemoryBarrier();
27009#endif
27010}
27011
27012/*
27013** Initialize and deinitialize the mutex subsystem.
27014*/
27015static sqlite3_mutex winMutex_staticMutexes[] = {
27016 SQLITE3_MUTEX_INITIALIZER(2),
27017 SQLITE3_MUTEX_INITIALIZER(3),
27018 SQLITE3_MUTEX_INITIALIZER(4),
27019 SQLITE3_MUTEX_INITIALIZER(5),
27020 SQLITE3_MUTEX_INITIALIZER(6),
27021 SQLITE3_MUTEX_INITIALIZER(7),
27022 SQLITE3_MUTEX_INITIALIZER(8),
27023 SQLITE3_MUTEX_INITIALIZER(9),
27024 SQLITE3_MUTEX_INITIALIZER(10),
27025 SQLITE3_MUTEX_INITIALIZER(11),
27026 SQLITE3_MUTEX_INITIALIZER(12),
27027 SQLITE3_MUTEX_INITIALIZER(13)
27028};
27029
27030static int winMutex_isInit = 0;
27031static int winMutex_isNt = -1; /* <0 means "need to query" */
27032
27033/* As the winMutexInit() and winMutexEnd() functions are called as part
27034** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
27035** "interlocked" magic used here is probably not strictly necessary.
27036*/
27037static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
27038
27039SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
27040SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
27041
27042static int winMutexInit(void){
27043 /* The first to increment to 1 does actual initialization */
27044 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
27045 int i;
27046 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27047#if SQLITE_OS_WINRT
27048 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
27049#else
27050 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
27051#endif
27052 }
27053 winMutex_isInit = 1;
27054 }else{
27055 /* Another thread is (in the process of) initializing the static
27056 ** mutexes */
27057 while( !winMutex_isInit ){
27058 sqlite3_win32_sleep(1);
27059 }
27060 }
27061 return SQLITE_OK;
27062}
27063
27064static int winMutexEnd(void){
27065 /* The first to decrement to 0 does actual shutdown
27066 ** (which should be the last to shutdown.) */
27067 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
27068 if( winMutex_isInit==1 ){
27069 int i;
27070 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
27071 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
27072 }
27073 winMutex_isInit = 0;
27074 }
27075 }
27076 return SQLITE_OK;
27077}
27078
27079/*
27080** The sqlite3_mutex_alloc() routine allocates a new
27081** mutex and returns a pointer to it. If it returns NULL
27082** that means that a mutex could not be allocated. SQLite
27083** will unwind its stack and return an error. The argument
27084** to sqlite3_mutex_alloc() is one of these integer constants:
27085**
27086** <ul>
27087** <li> SQLITE_MUTEX_FAST
27088** <li> SQLITE_MUTEX_RECURSIVE
27089** <li> SQLITE_MUTEX_STATIC_MAIN
27090** <li> SQLITE_MUTEX_STATIC_MEM
27091** <li> SQLITE_MUTEX_STATIC_OPEN
27092** <li> SQLITE_MUTEX_STATIC_PRNG
27093** <li> SQLITE_MUTEX_STATIC_LRU
27094** <li> SQLITE_MUTEX_STATIC_PMEM
27095** <li> SQLITE_MUTEX_STATIC_APP1
27096** <li> SQLITE_MUTEX_STATIC_APP2
27097** <li> SQLITE_MUTEX_STATIC_APP3
27098** <li> SQLITE_MUTEX_STATIC_VFS1
27099** <li> SQLITE_MUTEX_STATIC_VFS2
27100** <li> SQLITE_MUTEX_STATIC_VFS3
27101** </ul>
27102**
27103** The first two constants cause sqlite3_mutex_alloc() to create
27104** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
27105** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
27106** The mutex implementation does not need to make a distinction
27107** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
27108** not want to. But SQLite will only request a recursive mutex in
27109** cases where it really needs one. If a faster non-recursive mutex
27110** implementation is available on the host platform, the mutex subsystem
27111** might return such a mutex in response to SQLITE_MUTEX_FAST.
27112**
27113** The other allowed parameters to sqlite3_mutex_alloc() each return
27114** a pointer to a static preexisting mutex. Six static mutexes are
27115** used by the current version of SQLite. Future versions of SQLite
27116** may add additional static mutexes. Static mutexes are for internal
27117** use by SQLite only. Applications that use SQLite mutexes should
27118** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
27119** SQLITE_MUTEX_RECURSIVE.
27120**
27121** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
27122** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
27123** returns a different mutex on every call. But for the static
27124** mutex types, the same mutex is returned on every call that has
27125** the same type number.
27126*/
27127static sqlite3_mutex *winMutexAlloc(int iType){
27128 sqlite3_mutex *p;
27129
27130 switch( iType ){
27131 case SQLITE_MUTEX_FAST:
27132 case SQLITE_MUTEX_RECURSIVE: {
27133 p = sqlite3MallocZero( sizeof(*p) );
27134 if( p ){
27135 p->id = iType;
27136#ifdef SQLITE_DEBUG
27137#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
27138 p->trace = 1;
27139#endif
27140#endif
27141#if SQLITE_OS_WINRT
27142 InitializeCriticalSectionEx(&p->mutex, 0, 0);
27143#else
27144 InitializeCriticalSection(&p->mutex);
27145#endif
27146 }
27147 break;
27148 }
27149 default: {
27150#ifdef SQLITE_ENABLE_API_ARMOR
27151 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
27152 (void)SQLITE_MISUSE_BKPT;
27153 return 0;
27154 }
27155#endif
27156 p = &winMutex_staticMutexes[iType-2];
27157#ifdef SQLITE_DEBUG
27158#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
27159 InterlockedCompareExchange(&p->trace, 1, 0);
27160#endif
27161#endif
27162 break;
27163 }
27164 }
27165 assert( p==0 || p->id==iType );
27166 return p;
27167}
27168
27169
27170/*
27171** This routine deallocates a previously
27172** allocated mutex. SQLite is careful to deallocate every
27173** mutex that it allocates.
27174*/
27175static void winMutexFree(sqlite3_mutex *p){
27176 assert( p );
27177 assert( p->nRef==0 && p->owner==0 );
27178 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
27179 DeleteCriticalSection(&p->mutex);
27180 sqlite3_free(p);
27181 }else{
27182#ifdef SQLITE_ENABLE_API_ARMOR
27183 (void)SQLITE_MISUSE_BKPT;
27184#endif
27185 }
27186}
27187
27188/*
27189** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
27190** to enter a mutex. If another thread is already within the mutex,
27191** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
27192** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
27193** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
27194** be entered multiple times by the same thread. In such cases the,
27195** mutex must be exited an equal number of times before another thread
27196** can enter. If the same thread tries to enter any other kind of mutex
27197** more than once, the behavior is undefined.
27198*/
27199static void winMutexEnter(sqlite3_mutex *p){
27200#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27201 DWORD tid = GetCurrentThreadId();
27202#endif
27203#ifdef SQLITE_DEBUG
27204 assert( p );
27205 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27206#else
27207 assert( p );
27208#endif
27209 assert( winMutex_isInit==1 );
27210 EnterCriticalSection(&p->mutex);
27211#ifdef SQLITE_DEBUG
27212 assert( p->nRef>0 || p->owner==0 );
27213 p->owner = tid;
27214 p->nRef++;
27215 if( p->trace ){
27216 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27217 tid, p->id, p, p->trace, p->nRef));
27218 }
27219#endif
27220}
27221
27222static int winMutexTry(sqlite3_mutex *p){
27223#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27224 DWORD tid = GetCurrentThreadId();
27225#endif
27226 int rc = SQLITE_BUSY;
27227 assert( p );
27228 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27229 /*
27230 ** The sqlite3_mutex_try() routine is very rarely used, and when it
27231 ** is used it is merely an optimization. So it is OK for it to always
27232 ** fail.
27233 **
27234 ** The TryEnterCriticalSection() interface is only available on WinNT.
27235 ** And some windows compilers complain if you try to use it without
27236 ** first doing some #defines that prevent SQLite from building on Win98.
27237 ** For that reason, we will omit this optimization for now. See
27238 ** ticket #2685.
27239 */
27240#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
27241 assert( winMutex_isInit==1 );
27242 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
27243 if( winMutex_isNt<0 ){
27244 winMutex_isNt = sqlite3_win32_is_nt();
27245 }
27246 assert( winMutex_isNt==0 || winMutex_isNt==1 );
27247 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
27248#ifdef SQLITE_DEBUG
27249 p->owner = tid;
27250 p->nRef++;
27251#endif
27252 rc = SQLITE_OK;
27253 }
27254#else
27255 UNUSED_PARAMETER(p);
27256#endif
27257#ifdef SQLITE_DEBUG
27258 if( p->trace ){
27259 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
27260 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
27261 }
27262#endif
27263 return rc;
27264}
27265
27266/*
27267** The sqlite3_mutex_leave() routine exits a mutex that was
27268** previously entered by the same thread. The behavior
27269** is undefined if the mutex is not currently entered or
27270** is not currently allocated. SQLite will never do either.
27271*/
27272static void winMutexLeave(sqlite3_mutex *p){
27273#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
27274 DWORD tid = GetCurrentThreadId();
27275#endif
27276 assert( p );
27277#ifdef SQLITE_DEBUG
27278 assert( p->nRef>0 );
27279 assert( p->owner==tid );
27280 p->nRef--;
27281 if( p->nRef==0 ) p->owner = 0;
27282 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
27283#endif
27284 assert( winMutex_isInit==1 );
27285 LeaveCriticalSection(&p->mutex);
27286#ifdef SQLITE_DEBUG
27287 if( p->trace ){
27288 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27289 tid, p->id, p, p->trace, p->nRef));
27290 }
27291#endif
27292}
27293
27294SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
27295 static const sqlite3_mutex_methods sMutex = {
27296 winMutexInit,
27297 winMutexEnd,
27298 winMutexAlloc,
27299 winMutexFree,
27300 winMutexEnter,
27301 winMutexTry,
27302 winMutexLeave,
27303#ifdef SQLITE_DEBUG
27304 winMutexHeld,
27305 winMutexNotheld
27306#else
27307 0,
27308 0
27309#endif
27310 };
27311 return &sMutex;
27312}
27313
27314#endif /* SQLITE_MUTEX_W32 */
27315
27316/************** End of mutex_w32.c *******************************************/
27317/************** Begin file malloc.c ******************************************/
27318/*
27319** 2001 September 15
27320**
27321** The author disclaims copyright to this source code. In place of
27322** a legal notice, here is a blessing:
27323**
27324** May you do good and not evil.
27325** May you find forgiveness for yourself and forgive others.
27326** May you share freely, never taking more than you give.
27327**
27328*************************************************************************
27329**
27330** Memory allocation functions used throughout sqlite.
27331*/
27332/* #include "sqliteInt.h" */
27333/* #include <stdarg.h> */
27334
27335/*
27336** Attempt to release up to n bytes of non-essential memory currently
27337** held by SQLite. An example of non-essential memory is memory used to
27338** cache database pages that are not currently in use.
27339*/
27340SQLITE_API int sqlite3_release_memory(int n){
27341#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27342 return sqlite3PcacheReleaseMemory(n);
27343#else
27344 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
27345 ** is a no-op returning zero if SQLite is not compiled with
27346 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
27347 UNUSED_PARAMETER(n);
27348 return 0;
27349#endif
27350}
27351
27352/*
27353** Default value of the hard heap limit. 0 means "no limit".
27354*/
27355#ifndef SQLITE_MAX_MEMORY
27356# define SQLITE_MAX_MEMORY 0
27357#endif
27358
27359/*
27360** State information local to the memory allocation subsystem.
27361*/
27362static SQLITE_WSD struct Mem0Global {
27363 sqlite3_mutex *mutex; /* Mutex to serialize access */
27364 sqlite3_int64 alarmThreshold; /* The soft heap limit */
27365 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
27366
27367 /*
27368 ** True if heap is nearly "full" where "full" is defined by the
27369 ** sqlite3_soft_heap_limit() setting.
27370 */
27371 int nearlyFull;
27372} mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
27373
27374#define mem0 GLOBAL(struct Mem0Global, mem0)
27375
27376/*
27377** Return the memory allocator mutex. sqlite3_status() needs it.
27378*/
27379SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
27380 return mem0.mutex;
27381}
27382
27383#ifndef SQLITE_OMIT_DEPRECATED
27384/*
27385** Deprecated external interface. It used to set an alarm callback
27386** that was invoked when memory usage grew too large. Now it is a
27387** no-op.
27388*/
27389SQLITE_API int sqlite3_memory_alarm(
27390 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
27391 void *pArg,
27392 sqlite3_int64 iThreshold
27393){
27394 (void)xCallback;
27395 (void)pArg;
27396 (void)iThreshold;
27397 return SQLITE_OK;
27398}
27399#endif
27400
27401/*
27402** Set the soft heap-size limit for the library. An argument of
27403** zero disables the limit. A negative argument is a no-op used to
27404** obtain the return value.
27405**
27406** The return value is the value of the heap limit just before this
27407** interface was called.
27408**
27409** If the hard heap limit is enabled, then the soft heap limit cannot
27410** be disabled nor raised above the hard heap limit.
27411*/
27412SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
27413 sqlite3_int64 priorLimit;
27414 sqlite3_int64 excess;
27415 sqlite3_int64 nUsed;
27416#ifndef SQLITE_OMIT_AUTOINIT
27417 int rc = sqlite3_initialize();
27418 if( rc ) return -1;
27419#endif
27420 sqlite3_mutex_enter(mem0.mutex);
27421 priorLimit = mem0.alarmThreshold;
27422 if( n<0 ){
27423 sqlite3_mutex_leave(mem0.mutex);
27424 return priorLimit;
27425 }
27426 if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
27427 n = mem0.hardLimit;
27428 }
27429 mem0.alarmThreshold = n;
27430 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27431 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
27432 sqlite3_mutex_leave(mem0.mutex);
27433 excess = sqlite3_memory_used() - n;
27434 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
27435 return priorLimit;
27436}
27437SQLITE_API void sqlite3_soft_heap_limit(int n){
27438 if( n<0 ) n = 0;
27439 sqlite3_soft_heap_limit64(n);
27440}
27441
27442/*
27443** Set the hard heap-size limit for the library. An argument of zero
27444** disables the hard heap limit. A negative argument is a no-op used
27445** to obtain the return value without affecting the hard heap limit.
27446**
27447** The return value is the value of the hard heap limit just prior to
27448** calling this interface.
27449**
27450** Setting the hard heap limit will also activate the soft heap limit
27451** and constrain the soft heap limit to be no more than the hard heap
27452** limit.
27453*/
27454SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
27455 sqlite3_int64 priorLimit;
27456#ifndef SQLITE_OMIT_AUTOINIT
27457 int rc = sqlite3_initialize();
27458 if( rc ) return -1;
27459#endif
27460 sqlite3_mutex_enter(mem0.mutex);
27461 priorLimit = mem0.hardLimit;
27462 if( n>=0 ){
27463 mem0.hardLimit = n;
27464 if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
27465 mem0.alarmThreshold = n;
27466 }
27467 }
27468 sqlite3_mutex_leave(mem0.mutex);
27469 return priorLimit;
27470}
27471
27472
27473/*
27474** Initialize the memory allocation subsystem.
27475*/
27476SQLITE_PRIVATE int sqlite3MallocInit(void){
27477 int rc;
27478 if( sqlite3GlobalConfig.m.xMalloc==0 ){
27479 sqlite3MemSetDefault();
27480 }
27481 memset(&mem0, 0, sizeof(mem0));
27482 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
27483 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
27484 || sqlite3GlobalConfig.nPage<=0 ){
27485 sqlite3GlobalConfig.pPage = 0;
27486 sqlite3GlobalConfig.szPage = 0;
27487 }
27488 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
27489 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
27490 return rc;
27491}
27492
27493/*
27494** Return true if the heap is currently under memory pressure - in other
27495** words if the amount of heap used is close to the limit set by
27496** sqlite3_soft_heap_limit().
27497*/
27498SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
27499 return AtomicLoad(&mem0.nearlyFull);
27500}
27501
27502/*
27503** Deinitialize the memory allocation subsystem.
27504*/
27505SQLITE_PRIVATE void sqlite3MallocEnd(void){
27506 if( sqlite3GlobalConfig.m.xShutdown ){
27507 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
27508 }
27509 memset(&mem0, 0, sizeof(mem0));
27510}
27511
27512/*
27513** Return the amount of memory currently checked out.
27514*/
27515SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
27516 sqlite3_int64 res, mx;
27517 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
27518 return res;
27519}
27520
27521/*
27522** Return the maximum amount of memory that has ever been
27523** checked out since either the beginning of this process
27524** or since the most recent reset.
27525*/
27526SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
27527 sqlite3_int64 res, mx;
27528 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
27529 return mx;
27530}
27531
27532/*
27533** Trigger the alarm
27534*/
27535static void sqlite3MallocAlarm(int nByte){
27536 if( mem0.alarmThreshold<=0 ) return;
27537 sqlite3_mutex_leave(mem0.mutex);
27538 sqlite3_release_memory(nByte);
27539 sqlite3_mutex_enter(mem0.mutex);
27540}
27541
27542/*
27543** Do a memory allocation with statistics and alarms. Assume the
27544** lock is already held.
27545*/
27546static void mallocWithAlarm(int n, void **pp){
27547 void *p;
27548 int nFull;
27549 assert( sqlite3_mutex_held(mem0.mutex) );
27550 assert( n>0 );
27551
27552 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
27553 ** implementation of malloc_good_size(), which must be called in debug
27554 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
27555 ** or else a crash results. Hence, do not attempt to optimize out the
27556 ** following xRoundup() call. */
27557 nFull = sqlite3GlobalConfig.m.xRoundup(n);
27558
27559 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
27560 if( mem0.alarmThreshold>0 ){
27561 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27562 if( nUsed >= mem0.alarmThreshold - nFull ){
27563 AtomicStore(&mem0.nearlyFull, 1);
27564 sqlite3MallocAlarm(nFull);
27565 if( mem0.hardLimit ){
27566 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
27567 if( nUsed >= mem0.hardLimit - nFull ){
27568 *pp = 0;
27569 return;
27570 }
27571 }
27572 }else{
27573 AtomicStore(&mem0.nearlyFull, 0);
27574 }
27575 }
27576 p = sqlite3GlobalConfig.m.xMalloc(nFull);
27577#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27578 if( p==0 && mem0.alarmThreshold>0 ){
27579 sqlite3MallocAlarm(nFull);
27580 p = sqlite3GlobalConfig.m.xMalloc(nFull);
27581 }
27582#endif
27583 if( p ){
27584 nFull = sqlite3MallocSize(p);
27585 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
27586 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
27587 }
27588 *pp = p;
27589}
27590
27591/*
27592** Allocate memory. This routine is like sqlite3_malloc() except that it
27593** assumes the memory subsystem has already been initialized.
27594*/
27595SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
27596 void *p;
27597 if( n==0 || n>=0x7fffff00 ){
27598 /* A memory allocation of a number of bytes which is near the maximum
27599 ** signed integer value might cause an integer overflow inside of the
27600 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
27601 ** 255 bytes of overhead. SQLite itself will never use anything near
27602 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
27603 p = 0;
27604 }else if( sqlite3GlobalConfig.bMemstat ){
27605 sqlite3_mutex_enter(mem0.mutex);
27606 mallocWithAlarm((int)n, &p);
27607 sqlite3_mutex_leave(mem0.mutex);
27608 }else{
27609 p = sqlite3GlobalConfig.m.xMalloc((int)n);
27610 }
27611 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
27612 return p;
27613}
27614
27615/*
27616** This version of the memory allocation is for use by the application.
27617** First make sure the memory subsystem is initialized, then do the
27618** allocation.
27619*/
27620SQLITE_API void *sqlite3_malloc(int n){
27621#ifndef SQLITE_OMIT_AUTOINIT
27622 if( sqlite3_initialize() ) return 0;
27623#endif
27624 return n<=0 ? 0 : sqlite3Malloc(n);
27625}
27626SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
27627#ifndef SQLITE_OMIT_AUTOINIT
27628 if( sqlite3_initialize() ) return 0;
27629#endif
27630 return sqlite3Malloc(n);
27631}
27632
27633/*
27634** TRUE if p is a lookaside memory allocation from db
27635*/
27636#ifndef SQLITE_OMIT_LOOKASIDE
27637static int isLookaside(sqlite3 *db, void *p){
27638 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
27639}
27640#else
27641#define isLookaside(A,B) 0
27642#endif
27643
27644/*
27645** Return the size of a memory allocation previously obtained from
27646** sqlite3Malloc() or sqlite3_malloc().
27647*/
27648SQLITE_PRIVATE int sqlite3MallocSize(void *p){
27649 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27650 return sqlite3GlobalConfig.m.xSize(p);
27651}
27652static int lookasideMallocSize(sqlite3 *db, void *p){
27653#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27654 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
27655#else
27656 return db->lookaside.szTrue;
27657#endif
27658}
27659SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
27660 assert( p!=0 );
27661#ifdef SQLITE_DEBUG
27662 if( db==0 || !isLookaside(db,p) ){
27663 if( db==0 ){
27664 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27665 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27666 }else{
27667 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27668 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27669 }
27670 }
27671#endif
27672 if( db ){
27673 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
27674#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27675 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
27676 assert( sqlite3_mutex_held(db->mutex) );
27677 return LOOKASIDE_SMALL;
27678 }
27679#endif
27680 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
27681 assert( sqlite3_mutex_held(db->mutex) );
27682 return db->lookaside.szTrue;
27683 }
27684 }
27685 }
27686 return sqlite3GlobalConfig.m.xSize(p);
27687}
27688SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
27689 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27690 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27691 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
27692}
27693
27694/*
27695** Free memory previously obtained from sqlite3Malloc().
27696*/
27697SQLITE_API void sqlite3_free(void *p){
27698 if( p==0 ) return; /* IMP: R-49053-54554 */
27699 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27700 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
27701 if( sqlite3GlobalConfig.bMemstat ){
27702 sqlite3_mutex_enter(mem0.mutex);
27703 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
27704 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
27705 sqlite3GlobalConfig.m.xFree(p);
27706 sqlite3_mutex_leave(mem0.mutex);
27707 }else{
27708 sqlite3GlobalConfig.m.xFree(p);
27709 }
27710}
27711
27712/*
27713** Add the size of memory allocation "p" to the count in
27714** *db->pnBytesFreed.
27715*/
27716static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
27717 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
27718}
27719
27720/*
27721** Free memory that might be associated with a particular database
27722** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
27723** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
27724*/
27725SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
27726 assert( db==0 || sqlite3_mutex_held(db->mutex) );
27727 assert( p!=0 );
27728 if( db ){
27729 if( db->pnBytesFreed ){
27730 measureAllocationSize(db, p);
27731 return;
27732 }
27733 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
27734#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27735 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
27736 LookasideSlot *pBuf = (LookasideSlot*)p;
27737#ifdef SQLITE_DEBUG
27738 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
27739#endif
27740 pBuf->pNext = db->lookaside.pSmallFree;
27741 db->lookaside.pSmallFree = pBuf;
27742 return;
27743 }
27744#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
27745 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
27746 LookasideSlot *pBuf = (LookasideSlot*)p;
27747#ifdef SQLITE_DEBUG
27748 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
27749#endif
27750 pBuf->pNext = db->lookaside.pFree;
27751 db->lookaside.pFree = pBuf;
27752 return;
27753 }
27754 }
27755 }
27756 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27757 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27758 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
27759 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27760 sqlite3_free(p);
27761}
27762SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
27763 assert( db==0 || sqlite3_mutex_held(db->mutex) );
27764 if( p ) sqlite3DbFreeNN(db, p);
27765}
27766
27767/*
27768** Change the size of an existing memory allocation
27769*/
27770SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
27771 int nOld, nNew, nDiff;
27772 void *pNew;
27773 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
27774 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
27775 if( pOld==0 ){
27776 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
27777 }
27778 if( nBytes==0 ){
27779 sqlite3_free(pOld); /* IMP: R-26507-47431 */
27780 return 0;
27781 }
27782 if( nBytes>=0x7fffff00 ){
27783 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
27784 return 0;
27785 }
27786 nOld = sqlite3MallocSize(pOld);
27787 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
27788 ** argument to xRealloc is always a value returned by a prior call to
27789 ** xRoundup. */
27790 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
27791 if( nOld==nNew ){
27792 pNew = pOld;
27793 }else if( sqlite3GlobalConfig.bMemstat ){
27794 sqlite3_mutex_enter(mem0.mutex);
27795 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
27796 nDiff = nNew - nOld;
27797 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
27798 mem0.alarmThreshold-nDiff ){
27799 sqlite3MallocAlarm(nDiff);
27800 }
27801 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27802#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27803 if( pNew==0 && mem0.alarmThreshold>0 ){
27804 sqlite3MallocAlarm((int)nBytes);
27805 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27806 }
27807#endif
27808 if( pNew ){
27809 nNew = sqlite3MallocSize(pNew);
27810 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
27811 }
27812 sqlite3_mutex_leave(mem0.mutex);
27813 }else{
27814 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
27815 }
27816 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
27817 return pNew;
27818}
27819
27820/*
27821** The public interface to sqlite3Realloc. Make sure that the memory
27822** subsystem is initialized prior to invoking sqliteRealloc.
27823*/
27824SQLITE_API void *sqlite3_realloc(void *pOld, int n){
27825#ifndef SQLITE_OMIT_AUTOINIT
27826 if( sqlite3_initialize() ) return 0;
27827#endif
27828 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
27829 return sqlite3Realloc(pOld, n);
27830}
27831SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
27832#ifndef SQLITE_OMIT_AUTOINIT
27833 if( sqlite3_initialize() ) return 0;
27834#endif
27835 return sqlite3Realloc(pOld, n);
27836}
27837
27838
27839/*
27840** Allocate and zero memory.
27841*/
27842SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
27843 void *p = sqlite3Malloc(n);
27844 if( p ){
27845 memset(p, 0, (size_t)n);
27846 }
27847 return p;
27848}
27849
27850/*
27851** Allocate and zero memory. If the allocation fails, make
27852** the mallocFailed flag in the connection pointer.
27853*/
27854SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
27855 void *p;
27856 testcase( db==0 );
27857 p = sqlite3DbMallocRaw(db, n);
27858 if( p ) memset(p, 0, (size_t)n);
27859 return p;
27860}
27861
27862
27863/* Finish the work of sqlite3DbMallocRawNN for the unusual and
27864** slower case when the allocation cannot be fulfilled using lookaside.
27865*/
27866static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
27867 void *p;
27868 assert( db!=0 );
27869 p = sqlite3Malloc(n);
27870 if( !p ) sqlite3OomFault(db);
27871 sqlite3MemdebugSetType(p,
27872 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
27873 return p;
27874}
27875
27876/*
27877** Allocate memory, either lookaside (if possible) or heap.
27878** If the allocation fails, set the mallocFailed flag in
27879** the connection pointer.
27880**
27881** If db!=0 and db->mallocFailed is true (indicating a prior malloc
27882** failure on the same database connection) then always return 0.
27883** Hence for a particular database connection, once malloc starts
27884** failing, it fails consistently until mallocFailed is reset.
27885** This is an important assumption. There are many places in the
27886** code that do things like this:
27887**
27888** int *a = (int*)sqlite3DbMallocRaw(db, 100);
27889** int *b = (int*)sqlite3DbMallocRaw(db, 200);
27890** if( b ) a[10] = 9;
27891**
27892** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
27893** that all prior mallocs (ex: "a") worked too.
27894**
27895** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
27896** not a NULL pointer.
27897*/
27898SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
27899 void *p;
27900 if( db ) return sqlite3DbMallocRawNN(db, n);
27901 p = sqlite3Malloc(n);
27902 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27903 return p;
27904}
27905SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
27906#ifndef SQLITE_OMIT_LOOKASIDE
27907 LookasideSlot *pBuf;
27908 assert( db!=0 );
27909 assert( sqlite3_mutex_held(db->mutex) );
27910 assert( db->pnBytesFreed==0 );
27911 if( n>db->lookaside.sz ){
27912 if( !db->lookaside.bDisable ){
27913 db->lookaside.anStat[1]++;
27914 }else if( db->mallocFailed ){
27915 return 0;
27916 }
27917 return dbMallocRawFinish(db, n);
27918 }
27919#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27920 if( n<=LOOKASIDE_SMALL ){
27921 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
27922 db->lookaside.pSmallFree = pBuf->pNext;
27923 db->lookaside.anStat[0]++;
27924 return (void*)pBuf;
27925 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
27926 db->lookaside.pSmallInit = pBuf->pNext;
27927 db->lookaside.anStat[0]++;
27928 return (void*)pBuf;
27929 }
27930 }
27931#endif
27932 if( (pBuf = db->lookaside.pFree)!=0 ){
27933 db->lookaside.pFree = pBuf->pNext;
27934 db->lookaside.anStat[0]++;
27935 return (void*)pBuf;
27936 }else if( (pBuf = db->lookaside.pInit)!=0 ){
27937 db->lookaside.pInit = pBuf->pNext;
27938 db->lookaside.anStat[0]++;
27939 return (void*)pBuf;
27940 }else{
27941 db->lookaside.anStat[2]++;
27942 }
27943#else
27944 assert( db!=0 );
27945 assert( sqlite3_mutex_held(db->mutex) );
27946 assert( db->pnBytesFreed==0 );
27947 if( db->mallocFailed ){
27948 return 0;
27949 }
27950#endif
27951 return dbMallocRawFinish(db, n);
27952}
27953
27954/* Forward declaration */
27955static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
27956
27957/*
27958** Resize the block of memory pointed to by p to n bytes. If the
27959** resize fails, set the mallocFailed flag in the connection object.
27960*/
27961SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
27962 assert( db!=0 );
27963 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
27964 assert( sqlite3_mutex_held(db->mutex) );
27965 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
27966#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
27967 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
27968 if( n<=LOOKASIDE_SMALL ) return p;
27969 }else
27970#endif
27971 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
27972 if( n<=db->lookaside.szTrue ) return p;
27973 }
27974 }
27975 return dbReallocFinish(db, p, n);
27976}
27977static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
27978 void *pNew = 0;
27979 assert( db!=0 );
27980 assert( p!=0 );
27981 if( db->mallocFailed==0 ){
27982 if( isLookaside(db, p) ){
27983 pNew = sqlite3DbMallocRawNN(db, n);
27984 if( pNew ){
27985 memcpy(pNew, p, lookasideMallocSize(db, p));
27986 sqlite3DbFree(db, p);
27987 }
27988 }else{
27989 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27990 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
27991 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27992 pNew = sqlite3Realloc(p, n);
27993 if( !pNew ){
27994 sqlite3OomFault(db);
27995 }
27996 sqlite3MemdebugSetType(pNew,
27997 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
27998 }
27999 }
28000 return pNew;
28001}
28002
28003/*
28004** Attempt to reallocate p. If the reallocation fails, then free p
28005** and set the mallocFailed flag in the database connection.
28006*/
28007SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
28008 void *pNew;
28009 pNew = sqlite3DbRealloc(db, p, n);
28010 if( !pNew ){
28011 sqlite3DbFree(db, p);
28012 }
28013 return pNew;
28014}
28015
28016/*
28017** Make a copy of a string in memory obtained from sqliteMalloc(). These
28018** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
28019** is because when memory debugging is turned on, these two functions are
28020** called via macros that record the current file and line number in the
28021** ThreadData structure.
28022*/
28023SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
28024 char *zNew;
28025 size_t n;
28026 if( z==0 ){
28027 return 0;
28028 }
28029 n = strlen(z) + 1;
28030 zNew = sqlite3DbMallocRaw(db, n);
28031 if( zNew ){
28032 memcpy(zNew, z, n);
28033 }
28034 return zNew;
28035}
28036SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
28037 char *zNew;
28038 assert( db!=0 );
28039 assert( z!=0 || n==0 );
28040 assert( (n&0x7fffffff)==n );
28041 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
28042 if( zNew ){
28043 memcpy(zNew, z, (size_t)n);
28044 zNew[n] = 0;
28045 }
28046 return zNew;
28047}
28048
28049/*
28050** The text between zStart and zEnd represents a phrase within a larger
28051** SQL statement. Make a copy of this phrase in space obtained form
28052** sqlite3DbMalloc(). Omit leading and trailing whitespace.
28053*/
28054SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
28055 int n;
28056 while( sqlite3Isspace(zStart[0]) ) zStart++;
28057 n = (int)(zEnd - zStart);
28058 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
28059 return sqlite3DbStrNDup(db, zStart, n);
28060}
28061
28062/*
28063** Free any prior content in *pz and replace it with a copy of zNew.
28064*/
28065SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
28066 sqlite3DbFree(db, *pz);
28067 *pz = sqlite3DbStrDup(db, zNew);
28068}
28069
28070/*
28071** Call this routine to record the fact that an OOM (out-of-memory) error
28072** has happened. This routine will set db->mallocFailed, and also
28073** temporarily disable the lookaside memory allocator and interrupt
28074** any running VDBEs.
28075*/
28076SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
28077 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
28078 db->mallocFailed = 1;
28079 if( db->nVdbeExec>0 ){
28080 AtomicStore(&db->u1.isInterrupted, 1);
28081 }
28082 DisableLookaside;
28083 if( db->pParse ){
28084 db->pParse->rc = SQLITE_NOMEM_BKPT;
28085 }
28086 }
28087}
28088
28089/*
28090** This routine reactivates the memory allocator and clears the
28091** db->mallocFailed flag as necessary.
28092**
28093** The memory allocator is not restarted if there are running
28094** VDBEs.
28095*/
28096SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
28097 if( db->mallocFailed && db->nVdbeExec==0 ){
28098 db->mallocFailed = 0;
28099 AtomicStore(&db->u1.isInterrupted, 0);
28100 assert( db->lookaside.bDisable>0 );
28101 EnableLookaside;
28102 }
28103}
28104
28105/*
28106** Take actions at the end of an API call to indicate an OOM error
28107*/
28108static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
28109 sqlite3OomClear(db);
28110 sqlite3Error(db, SQLITE_NOMEM);
28111 return SQLITE_NOMEM_BKPT;
28112}
28113
28114/*
28115** This function must be called before exiting any API function (i.e.
28116** returning control to the user) that has called sqlite3_malloc or
28117** sqlite3_realloc.
28118**
28119** The returned value is normally a copy of the second argument to this
28120** function. However, if a malloc() failure has occurred since the previous
28121** invocation SQLITE_NOMEM is returned instead.
28122**
28123** If an OOM as occurred, then the connection error-code (the value
28124** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
28125*/
28126SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
28127 /* If the db handle must hold the connection handle mutex here.
28128 ** Otherwise the read (and possible write) of db->mallocFailed
28129 ** is unsafe, as is the call to sqlite3Error().
28130 */
28131 assert( db!=0 );
28132 assert( sqlite3_mutex_held(db->mutex) );
28133 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
28134 return apiOomError(db);
28135 }
28136 return rc & db->errMask;
28137}
28138
28139/************** End of malloc.c **********************************************/
28140/************** Begin file printf.c ******************************************/
28141/*
28142** The "printf" code that follows dates from the 1980's. It is in
28143** the public domain.
28144**
28145**************************************************************************
28146**
28147** This file contains code for a set of "printf"-like routines. These
28148** routines format strings much like the printf() from the standard C
28149** library, though the implementation here has enhancements to support
28150** SQLite.
28151*/
28152/* #include "sqliteInt.h" */
28153
28154/*
28155** Conversion types fall into various categories as defined by the
28156** following enumeration.
28157*/
28158#define etRADIX 0 /* non-decimal integer types. %x %o */
28159#define etFLOAT 1 /* Floating point. %f */
28160#define etEXP 2 /* Exponentional notation. %e and %E */
28161#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
28162#define etSIZE 4 /* Return number of characters processed so far. %n */
28163#define etSTRING 5 /* Strings. %s */
28164#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
28165#define etPERCENT 7 /* Percent symbol. %% */
28166#define etCHARX 8 /* Characters. %c */
28167/* The rest are extensions, not normally found in printf() */
28168#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
28169#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
28170 NULL pointers replaced by SQL NULL. %Q */
28171#define etTOKEN 11 /* a pointer to a Token structure */
28172#define etSRCLIST 12 /* a pointer to a SrcList */
28173#define etPOINTER 13 /* The %p conversion */
28174#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
28175#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
28176#define etDECIMAL 16 /* %d or %u, but not %x, %o */
28177
28178#define etINVALID 17 /* Any unrecognized conversion type */
28179
28180
28181/*
28182** An "etByte" is an 8-bit unsigned value.
28183*/
28184typedef unsigned char etByte;
28185
28186/*
28187** Each builtin conversion character (ex: the 'd' in "%d") is described
28188** by an instance of the following structure
28189*/
28190typedef struct et_info { /* Information about each format field */
28191 char fmttype; /* The format field code letter */
28192 etByte base; /* The base for radix conversion */
28193 etByte flags; /* One or more of FLAG_ constants below */
28194 etByte type; /* Conversion paradigm */
28195 etByte charset; /* Offset into aDigits[] of the digits string */
28196 etByte prefix; /* Offset into aPrefix[] of the prefix string */
28197} et_info;
28198
28199/*
28200** Allowed values for et_info.flags
28201*/
28202#define FLAG_SIGNED 1 /* True if the value to convert is signed */
28203#define FLAG_STRING 4 /* Allow infinite precision */
28204
28205
28206/*
28207** The following table is searched linearly, so it is good to put the
28208** most frequently used conversion types first.
28209*/
28210static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
28211static const char aPrefix[] = "-x0\000X0";
28212static const et_info fmtinfo[] = {
28213 { 'd', 10, 1, etDECIMAL, 0, 0 },
28214 { 's', 0, 4, etSTRING, 0, 0 },
28215 { 'g', 0, 1, etGENERIC, 30, 0 },
28216 { 'z', 0, 4, etDYNSTRING, 0, 0 },
28217 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
28218 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
28219 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
28220 { 'c', 0, 0, etCHARX, 0, 0 },
28221 { 'o', 8, 0, etRADIX, 0, 2 },
28222 { 'u', 10, 0, etDECIMAL, 0, 0 },
28223 { 'x', 16, 0, etRADIX, 16, 1 },
28224 { 'X', 16, 0, etRADIX, 0, 4 },
28225#ifndef SQLITE_OMIT_FLOATING_POINT
28226 { 'f', 0, 1, etFLOAT, 0, 0 },
28227 { 'e', 0, 1, etEXP, 30, 0 },
28228 { 'E', 0, 1, etEXP, 14, 0 },
28229 { 'G', 0, 1, etGENERIC, 14, 0 },
28230#endif
28231 { 'i', 10, 1, etDECIMAL, 0, 0 },
28232 { 'n', 0, 0, etSIZE, 0, 0 },
28233 { '%', 0, 0, etPERCENT, 0, 0 },
28234 { 'p', 16, 0, etPOINTER, 0, 1 },
28235
28236 /* All the rest are undocumented and are for internal use only */
28237 { 'T', 0, 0, etTOKEN, 0, 0 },
28238 { 'S', 0, 0, etSRCLIST, 0, 0 },
28239 { 'r', 10, 1, etORDINAL, 0, 0 },
28240};
28241
28242/* Floating point constants used for rounding */
28243static const double arRound[] = {
28244 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
28245 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
28246};
28247
28248/*
28249** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
28250** conversions will work.
28251*/
28252#ifndef SQLITE_OMIT_FLOATING_POINT
28253/*
28254** "*val" is a double such that 0.1 <= *val < 10.0
28255** Return the ascii code for the leading digit of *val, then
28256** multiply "*val" by 10.0 to renormalize.
28257**
28258** Example:
28259** input: *val = 3.14159
28260** output: *val = 1.4159 function return = '3'
28261**
28262** The counter *cnt is incremented each time. After counter exceeds
28263** 16 (the number of significant digits in a 64-bit float) '0' is
28264** always returned.
28265*/
28266static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
28267 int digit;
28268 LONGDOUBLE_TYPE d;
28269 if( (*cnt)<=0 ) return '0';
28270 (*cnt)--;
28271 digit = (int)*val;
28272 d = digit;
28273 digit += '0';
28274 *val = (*val - d)*10.0;
28275 return (char)digit;
28276}
28277#endif /* SQLITE_OMIT_FLOATING_POINT */
28278
28279/*
28280** Set the StrAccum object to an error mode.
28281*/
28282static void setStrAccumError(StrAccum *p, u8 eError){
28283 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
28284 p->accError = eError;
28285 if( p->mxAlloc ) sqlite3_str_reset(p);
28286 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
28287}
28288
28289/*
28290** Extra argument values from a PrintfArguments object
28291*/
28292static sqlite3_int64 getIntArg(PrintfArguments *p){
28293 if( p->nArg<=p->nUsed ) return 0;
28294 return sqlite3_value_int64(p->apArg[p->nUsed++]);
28295}
28296static double getDoubleArg(PrintfArguments *p){
28297 if( p->nArg<=p->nUsed ) return 0.0;
28298 return sqlite3_value_double(p->apArg[p->nUsed++]);
28299}
28300static char *getTextArg(PrintfArguments *p){
28301 if( p->nArg<=p->nUsed ) return 0;
28302 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
28303}
28304
28305/*
28306** Allocate memory for a temporary buffer needed for printf rendering.
28307**
28308** If the requested size of the temp buffer is larger than the size
28309** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
28310** Do the size check before the memory allocation to prevent rogue
28311** SQL from requesting large allocations using the precision or width
28312** field of the printf() function.
28313*/
28314static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
28315 char *z;
28316 if( pAccum->accError ) return 0;
28317 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
28318 setStrAccumError(pAccum, SQLITE_TOOBIG);
28319 return 0;
28320 }
28321 z = sqlite3DbMallocRaw(pAccum->db, n);
28322 if( z==0 ){
28323 setStrAccumError(pAccum, SQLITE_NOMEM);
28324 }
28325 return z;
28326}
28327
28328/*
28329** On machines with a small stack size, you can redefine the
28330** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
28331*/
28332#ifndef SQLITE_PRINT_BUF_SIZE
28333# define SQLITE_PRINT_BUF_SIZE 70
28334#endif
28335#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
28336
28337/*
28338** Hard limit on the precision of floating-point conversions.
28339*/
28340#ifndef SQLITE_PRINTF_PRECISION_LIMIT
28341# define SQLITE_FP_PRECISION_LIMIT 100000000
28342#endif
28343
28344/*
28345** Render a string given by "fmt" into the StrAccum object.
28346*/
28347SQLITE_API void sqlite3_str_vappendf(
28348 sqlite3_str *pAccum, /* Accumulate results here */
28349 const char *fmt, /* Format string */
28350 va_list ap /* arguments */
28351){
28352 int c; /* Next character in the format string */
28353 char *bufpt; /* Pointer to the conversion buffer */
28354 int precision; /* Precision of the current field */
28355 int length; /* Length of the field */
28356 int idx; /* A general purpose loop counter */
28357 int width; /* Width of the current field */
28358 etByte flag_leftjustify; /* True if "-" flag is present */
28359 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
28360 etByte flag_alternateform; /* True if "#" flag is present */
28361 etByte flag_altform2; /* True if "!" flag is present */
28362 etByte flag_zeropad; /* True if field width constant starts with zero */
28363 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
28364 etByte done; /* Loop termination flag */
28365 etByte cThousand; /* Thousands separator for %d and %u */
28366 etByte xtype = etINVALID; /* Conversion paradigm */
28367 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
28368 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
28369 sqlite_uint64 longvalue; /* Value for integer types */
28370 LONGDOUBLE_TYPE realvalue; /* Value for real types */
28371 const et_info *infop; /* Pointer to the appropriate info structure */
28372 char *zOut; /* Rendering buffer */
28373 int nOut; /* Size of the rendering buffer */
28374 char *zExtra = 0; /* Malloced memory used by some conversion */
28375#ifndef SQLITE_OMIT_FLOATING_POINT
28376 int exp, e2; /* exponent of real numbers */
28377 int nsd; /* Number of significant digits returned */
28378 double rounder; /* Used for rounding floating point values */
28379 etByte flag_dp; /* True if decimal point should be shown */
28380 etByte flag_rtz; /* True if trailing zeros should be removed */
28381#endif
28382 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
28383 char buf[etBUFSIZE]; /* Conversion buffer */
28384
28385 /* pAccum never starts out with an empty buffer that was obtained from
28386 ** malloc(). This precondition is required by the mprintf("%z...")
28387 ** optimization. */
28388 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
28389
28390 bufpt = 0;
28391 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
28392 pArgList = va_arg(ap, PrintfArguments*);
28393 bArgList = 1;
28394 }else{
28395 bArgList = 0;
28396 }
28397 for(; (c=(*fmt))!=0; ++fmt){
28398 if( c!='%' ){
28399 bufpt = (char *)fmt;
28400#if HAVE_STRCHRNUL
28401 fmt = strchrnul(fmt, '%');
28402#else
28403 do{ fmt++; }while( *fmt && *fmt != '%' );
28404#endif
28405 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
28406 if( *fmt==0 ) break;
28407 }
28408 if( (c=(*++fmt))==0 ){
28409 sqlite3_str_append(pAccum, "%", 1);
28410 break;
28411 }
28412 /* Find out what flags are present */
28413 flag_leftjustify = flag_prefix = cThousand =
28414 flag_alternateform = flag_altform2 = flag_zeropad = 0;
28415 done = 0;
28416 width = 0;
28417 flag_long = 0;
28418 precision = -1;
28419 do{
28420 switch( c ){
28421 case '-': flag_leftjustify = 1; break;
28422 case '+': flag_prefix = '+'; break;
28423 case ' ': flag_prefix = ' '; break;
28424 case '#': flag_alternateform = 1; break;
28425 case '!': flag_altform2 = 1; break;
28426 case '0': flag_zeropad = 1; break;
28427 case ',': cThousand = ','; break;
28428 default: done = 1; break;
28429 case 'l': {
28430 flag_long = 1;
28431 c = *++fmt;
28432 if( c=='l' ){
28433 c = *++fmt;
28434 flag_long = 2;
28435 }
28436 done = 1;
28437 break;
28438 }
28439 case '1': case '2': case '3': case '4': case '5':
28440 case '6': case '7': case '8': case '9': {
28441 unsigned wx = c - '0';
28442 while( (c = *++fmt)>='0' && c<='9' ){
28443 wx = wx*10 + c - '0';
28444 }
28445 testcase( wx>0x7fffffff );
28446 width = wx & 0x7fffffff;
28447#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28448 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
28449 width = SQLITE_PRINTF_PRECISION_LIMIT;
28450 }
28451#endif
28452 if( c!='.' && c!='l' ){
28453 done = 1;
28454 }else{
28455 fmt--;
28456 }
28457 break;
28458 }
28459 case '*': {
28460 if( bArgList ){
28461 width = (int)getIntArg(pArgList);
28462 }else{
28463 width = va_arg(ap,int);
28464 }
28465 if( width<0 ){
28466 flag_leftjustify = 1;
28467 width = width >= -2147483647 ? -width : 0;
28468 }
28469#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28470 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
28471 width = SQLITE_PRINTF_PRECISION_LIMIT;
28472 }
28473#endif
28474 if( (c = fmt[1])!='.' && c!='l' ){
28475 c = *++fmt;
28476 done = 1;
28477 }
28478 break;
28479 }
28480 case '.': {
28481 c = *++fmt;
28482 if( c=='*' ){
28483 if( bArgList ){
28484 precision = (int)getIntArg(pArgList);
28485 }else{
28486 precision = va_arg(ap,int);
28487 }
28488 if( precision<0 ){
28489 precision = precision >= -2147483647 ? -precision : -1;
28490 }
28491 c = *++fmt;
28492 }else{
28493 unsigned px = 0;
28494 while( c>='0' && c<='9' ){
28495 px = px*10 + c - '0';
28496 c = *++fmt;
28497 }
28498 testcase( px>0x7fffffff );
28499 precision = px & 0x7fffffff;
28500 }
28501#ifdef SQLITE_PRINTF_PRECISION_LIMIT
28502 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
28503 precision = SQLITE_PRINTF_PRECISION_LIMIT;
28504 }
28505#endif
28506 if( c=='l' ){
28507 --fmt;
28508 }else{
28509 done = 1;
28510 }
28511 break;
28512 }
28513 }
28514 }while( !done && (c=(*++fmt))!=0 );
28515
28516 /* Fetch the info entry for the field */
28517 infop = &fmtinfo[0];
28518 xtype = etINVALID;
28519 for(idx=0; idx<ArraySize(fmtinfo); idx++){
28520 if( c==fmtinfo[idx].fmttype ){
28521 infop = &fmtinfo[idx];
28522 xtype = infop->type;
28523 break;
28524 }
28525 }
28526
28527 /*
28528 ** At this point, variables are initialized as follows:
28529 **
28530 ** flag_alternateform TRUE if a '#' is present.
28531 ** flag_altform2 TRUE if a '!' is present.
28532 ** flag_prefix '+' or ' ' or zero
28533 ** flag_leftjustify TRUE if a '-' is present or if the
28534 ** field width was negative.
28535 ** flag_zeropad TRUE if the width began with 0.
28536 ** flag_long 1 for "l", 2 for "ll"
28537 ** width The specified field width. This is
28538 ** always non-negative. Zero is the default.
28539 ** precision The specified precision. The default
28540 ** is -1.
28541 ** xtype The class of the conversion.
28542 ** infop Pointer to the appropriate info struct.
28543 */
28544 assert( width>=0 );
28545 assert( precision>=(-1) );
28546 switch( xtype ){
28547 case etPOINTER:
28548 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
28549 sizeof(char*)==sizeof(long int) ? 1 : 0;
28550 /* no break */ deliberate_fall_through
28551 case etORDINAL:
28552 case etRADIX:
28553 cThousand = 0;
28554 /* no break */ deliberate_fall_through
28555 case etDECIMAL:
28556 if( infop->flags & FLAG_SIGNED ){
28557 i64 v;
28558 if( bArgList ){
28559 v = getIntArg(pArgList);
28560 }else if( flag_long ){
28561 if( flag_long==2 ){
28562 v = va_arg(ap,i64) ;
28563 }else{
28564 v = va_arg(ap,long int);
28565 }
28566 }else{
28567 v = va_arg(ap,int);
28568 }
28569 if( v<0 ){
28570 if( v==SMALLEST_INT64 ){
28571 longvalue = ((u64)1)<<63;
28572 }else{
28573 longvalue = -v;
28574 }
28575 prefix = '-';
28576 }else{
28577 longvalue = v;
28578 prefix = flag_prefix;
28579 }
28580 }else{
28581 if( bArgList ){
28582 longvalue = (u64)getIntArg(pArgList);
28583 }else if( flag_long ){
28584 if( flag_long==2 ){
28585 longvalue = va_arg(ap,u64);
28586 }else{
28587 longvalue = va_arg(ap,unsigned long int);
28588 }
28589 }else{
28590 longvalue = va_arg(ap,unsigned int);
28591 }
28592 prefix = 0;
28593 }
28594 if( longvalue==0 ) flag_alternateform = 0;
28595 if( flag_zeropad && precision<width-(prefix!=0) ){
28596 precision = width-(prefix!=0);
28597 }
28598 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
28599 nOut = etBUFSIZE;
28600 zOut = buf;
28601 }else{
28602 u64 n;
28603 n = (u64)precision + 10;
28604 if( cThousand ) n += precision/3;
28605 zOut = zExtra = printfTempBuf(pAccum, n);
28606 if( zOut==0 ) return;
28607 nOut = (int)n;
28608 }
28609 bufpt = &zOut[nOut-1];
28610 if( xtype==etORDINAL ){
28611 static const char zOrd[] = "thstndrd";
28612 int x = (int)(longvalue % 10);
28613 if( x>=4 || (longvalue/10)%10==1 ){
28614 x = 0;
28615 }
28616 *(--bufpt) = zOrd[x*2+1];
28617 *(--bufpt) = zOrd[x*2];
28618 }
28619 {
28620 const char *cset = &aDigits[infop->charset];
28621 u8 base = infop->base;
28622 do{ /* Convert to ascii */
28623 *(--bufpt) = cset[longvalue%base];
28624 longvalue = longvalue/base;
28625 }while( longvalue>0 );
28626 }
28627 length = (int)(&zOut[nOut-1]-bufpt);
28628 while( precision>length ){
28629 *(--bufpt) = '0'; /* Zero pad */
28630 length++;
28631 }
28632 if( cThousand ){
28633 int nn = (length - 1)/3; /* Number of "," to insert */
28634 int ix = (length - 1)%3 + 1;
28635 bufpt -= nn;
28636 for(idx=0; nn>0; idx++){
28637 bufpt[idx] = bufpt[idx+nn];
28638 ix--;
28639 if( ix==0 ){
28640 bufpt[++idx] = cThousand;
28641 nn--;
28642 ix = 3;
28643 }
28644 }
28645 }
28646 if( prefix ) *(--bufpt) = prefix; /* Add sign */
28647 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
28648 const char *pre;
28649 char x;
28650 pre = &aPrefix[infop->prefix];
28651 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
28652 }
28653 length = (int)(&zOut[nOut-1]-bufpt);
28654 break;
28655 case etFLOAT:
28656 case etEXP:
28657 case etGENERIC:
28658 if( bArgList ){
28659 realvalue = getDoubleArg(pArgList);
28660 }else{
28661 realvalue = va_arg(ap,double);
28662 }
28663#ifdef SQLITE_OMIT_FLOATING_POINT
28664 length = 0;
28665#else
28666 if( precision<0 ) precision = 6; /* Set default precision */
28667#ifdef SQLITE_FP_PRECISION_LIMIT
28668 if( precision>SQLITE_FP_PRECISION_LIMIT ){
28669 precision = SQLITE_FP_PRECISION_LIMIT;
28670 }
28671#endif
28672 if( realvalue<0.0 ){
28673 realvalue = -realvalue;
28674 prefix = '-';
28675 }else{
28676 prefix = flag_prefix;
28677 }
28678 if( xtype==etGENERIC && precision>0 ) precision--;
28679 testcase( precision>0xfff );
28680 idx = precision & 0xfff;
28681 rounder = arRound[idx%10];
28682 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
28683 if( xtype==etFLOAT ){
28684 double rx = (double)realvalue;
28685 sqlite3_uint64 u;
28686 int ex;
28687 memcpy(&u, &rx, sizeof(u));
28688 ex = -1023 + (int)((u>>52)&0x7ff);
28689 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
28690 realvalue += rounder;
28691 }
28692 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
28693 exp = 0;
28694 if( sqlite3IsNaN((double)realvalue) ){
28695 bufpt = "NaN";
28696 length = 3;
28697 break;
28698 }
28699 if( realvalue>0.0 ){
28700 LONGDOUBLE_TYPE scale = 1.0;
28701 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
28702 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
28703 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
28704 realvalue /= scale;
28705 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
28706 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
28707 if( exp>350 ){
28708 bufpt = buf;
28709 buf[0] = prefix;
28710 memcpy(buf+(prefix!=0),"Inf",4);
28711 length = 3+(prefix!=0);
28712 break;
28713 }
28714 }
28715 bufpt = buf;
28716 /*
28717 ** If the field type is etGENERIC, then convert to either etEXP
28718 ** or etFLOAT, as appropriate.
28719 */
28720 if( xtype!=etFLOAT ){
28721 realvalue += rounder;
28722 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
28723 }
28724 if( xtype==etGENERIC ){
28725 flag_rtz = !flag_alternateform;
28726 if( exp<-4 || exp>precision ){
28727 xtype = etEXP;
28728 }else{
28729 precision = precision - exp;
28730 xtype = etFLOAT;
28731 }
28732 }else{
28733 flag_rtz = flag_altform2;
28734 }
28735 if( xtype==etEXP ){
28736 e2 = 0;
28737 }else{
28738 e2 = exp;
28739 }
28740 {
28741 i64 szBufNeeded; /* Size of a temporary buffer needed */
28742 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
28743 if( szBufNeeded > etBUFSIZE ){
28744 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
28745 if( bufpt==0 ) return;
28746 }
28747 }
28748 zOut = bufpt;
28749 nsd = 16 + flag_altform2*10;
28750 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
28751 /* The sign in front of the number */
28752 if( prefix ){
28753 *(bufpt++) = prefix;
28754 }
28755 /* Digits prior to the decimal point */
28756 if( e2<0 ){
28757 *(bufpt++) = '0';
28758 }else{
28759 for(; e2>=0; e2--){
28760 *(bufpt++) = et_getdigit(&realvalue,&nsd);
28761 }
28762 }
28763 /* The decimal point */
28764 if( flag_dp ){
28765 *(bufpt++) = '.';
28766 }
28767 /* "0" digits after the decimal point but before the first
28768 ** significant digit of the number */
28769 for(e2++; e2<0; precision--, e2++){
28770 assert( precision>0 );
28771 *(bufpt++) = '0';
28772 }
28773 /* Significant digits after the decimal point */
28774 while( (precision--)>0 ){
28775 *(bufpt++) = et_getdigit(&realvalue,&nsd);
28776 }
28777 /* Remove trailing zeros and the "." if no digits follow the "." */
28778 if( flag_rtz && flag_dp ){
28779 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
28780 assert( bufpt>zOut );
28781 if( bufpt[-1]=='.' ){
28782 if( flag_altform2 ){
28783 *(bufpt++) = '0';
28784 }else{
28785 *(--bufpt) = 0;
28786 }
28787 }
28788 }
28789 /* Add the "eNNN" suffix */
28790 if( xtype==etEXP ){
28791 *(bufpt++) = aDigits[infop->charset];
28792 if( exp<0 ){
28793 *(bufpt++) = '-'; exp = -exp;
28794 }else{
28795 *(bufpt++) = '+';
28796 }
28797 if( exp>=100 ){
28798 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
28799 exp %= 100;
28800 }
28801 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
28802 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
28803 }
28804 *bufpt = 0;
28805
28806 /* The converted number is in buf[] and zero terminated. Output it.
28807 ** Note that the number is in the usual order, not reversed as with
28808 ** integer conversions. */
28809 length = (int)(bufpt-zOut);
28810 bufpt = zOut;
28811
28812 /* Special case: Add leading zeros if the flag_zeropad flag is
28813 ** set and we are not left justified */
28814 if( flag_zeropad && !flag_leftjustify && length < width){
28815 int i;
28816 int nPad = width - length;
28817 for(i=width; i>=nPad; i--){
28818 bufpt[i] = bufpt[i-nPad];
28819 }
28820 i = prefix!=0;
28821 while( nPad-- ) bufpt[i++] = '0';
28822 length = width;
28823 }
28824#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
28825 break;
28826 case etSIZE:
28827 if( !bArgList ){
28828 *(va_arg(ap,int*)) = pAccum->nChar;
28829 }
28830 length = width = 0;
28831 break;
28832 case etPERCENT:
28833 buf[0] = '%';
28834 bufpt = buf;
28835 length = 1;
28836 break;
28837 case etCHARX:
28838 if( bArgList ){
28839 bufpt = getTextArg(pArgList);
28840 length = 1;
28841 if( bufpt ){
28842 buf[0] = c = *(bufpt++);
28843 if( (c&0xc0)==0xc0 ){
28844 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
28845 buf[length++] = *(bufpt++);
28846 }
28847 }
28848 }else{
28849 buf[0] = 0;
28850 }
28851 }else{
28852 unsigned int ch = va_arg(ap,unsigned int);
28853 if( ch<0x00080 ){
28854 buf[0] = ch & 0xff;
28855 length = 1;
28856 }else if( ch<0x00800 ){
28857 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
28858 buf[1] = 0x80 + (u8)(ch & 0x3f);
28859 length = 2;
28860 }else if( ch<0x10000 ){
28861 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
28862 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
28863 buf[2] = 0x80 + (u8)(ch & 0x3f);
28864 length = 3;
28865 }else{
28866 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
28867 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
28868 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
28869 buf[3] = 0x80 + (u8)(ch & 0x3f);
28870 length = 4;
28871 }
28872 }
28873 if( precision>1 ){
28874 width -= precision-1;
28875 if( width>1 && !flag_leftjustify ){
28876 sqlite3_str_appendchar(pAccum, width-1, ' ');
28877 width = 0;
28878 }
28879 while( precision-- > 1 ){
28880 sqlite3_str_append(pAccum, buf, length);
28881 }
28882 }
28883 bufpt = buf;
28884 flag_altform2 = 1;
28885 goto adjust_width_for_utf8;
28886 case etSTRING:
28887 case etDYNSTRING:
28888 if( bArgList ){
28889 bufpt = getTextArg(pArgList);
28890 xtype = etSTRING;
28891 }else{
28892 bufpt = va_arg(ap,char*);
28893 }
28894 if( bufpt==0 ){
28895 bufpt = "";
28896 }else if( xtype==etDYNSTRING ){
28897 if( pAccum->nChar==0
28898 && pAccum->mxAlloc
28899 && width==0
28900 && precision<0
28901 && pAccum->accError==0
28902 ){
28903 /* Special optimization for sqlite3_mprintf("%z..."):
28904 ** Extend an existing memory allocation rather than creating
28905 ** a new one. */
28906 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
28907 pAccum->zText = bufpt;
28908 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
28909 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
28910 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
28911 length = 0;
28912 break;
28913 }
28914 zExtra = bufpt;
28915 }
28916 if( precision>=0 ){
28917 if( flag_altform2 ){
28918 /* Set length to the number of bytes needed in order to display
28919 ** precision characters */
28920 unsigned char *z = (unsigned char*)bufpt;
28921 while( precision-- > 0 && z[0] ){
28922 SQLITE_SKIP_UTF8(z);
28923 }
28924 length = (int)(z - (unsigned char*)bufpt);
28925 }else{
28926 for(length=0; length<precision && bufpt[length]; length++){}
28927 }
28928 }else{
28929 length = 0x7fffffff & (int)strlen(bufpt);
28930 }
28931 adjust_width_for_utf8:
28932 if( flag_altform2 && width>0 ){
28933 /* Adjust width to account for extra bytes in UTF-8 characters */
28934 int ii = length - 1;
28935 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
28936 }
28937 break;
28938 case etSQLESCAPE: /* %q: Escape ' characters */
28939 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
28940 case etSQLESCAPE3: { /* %w: Escape " characters */
28941 int i, j, k, n, isnull;
28942 int needQuote;
28943 char ch;
28944 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
28945 char *escarg;
28946
28947 if( bArgList ){
28948 escarg = getTextArg(pArgList);
28949 }else{
28950 escarg = va_arg(ap,char*);
28951 }
28952 isnull = escarg==0;
28953 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
28954 /* For %q, %Q, and %w, the precision is the number of bytes (or
28955 ** characters if the ! flags is present) to use from the input.
28956 ** Because of the extra quoting characters inserted, the number
28957 ** of output characters may be larger than the precision.
28958 */
28959 k = precision;
28960 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
28961 if( ch==q ) n++;
28962 if( flag_altform2 && (ch&0xc0)==0xc0 ){
28963 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
28964 }
28965 }
28966 needQuote = !isnull && xtype==etSQLESCAPE2;
28967 n += i + 3;
28968 if( n>etBUFSIZE ){
28969 bufpt = zExtra = printfTempBuf(pAccum, n);
28970 if( bufpt==0 ) return;
28971 }else{
28972 bufpt = buf;
28973 }
28974 j = 0;
28975 if( needQuote ) bufpt[j++] = q;
28976 k = i;
28977 for(i=0; i<k; i++){
28978 bufpt[j++] = ch = escarg[i];
28979 if( ch==q ) bufpt[j++] = ch;
28980 }
28981 if( needQuote ) bufpt[j++] = q;
28982 bufpt[j] = 0;
28983 length = j;
28984 goto adjust_width_for_utf8;
28985 }
28986 case etTOKEN: {
28987 Token *pToken;
28988 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
28989 pToken = va_arg(ap, Token*);
28990 assert( bArgList==0 );
28991 if( pToken && pToken->n ){
28992 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
28993 }
28994 length = width = 0;
28995 break;
28996 }
28997 case etSRCLIST: {
28998 SrcList *pSrc;
28999 int k;
29000 struct SrcList_item *pItem;
29001 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29002 pSrc = va_arg(ap, SrcList*);
29003 k = va_arg(ap, int);
29004 pItem = &pSrc->a[k];
29005 assert( bArgList==0 );
29006 assert( k>=0 && k<pSrc->nSrc );
29007 if( pItem->zDatabase ){
29008 sqlite3_str_appendall(pAccum, pItem->zDatabase);
29009 sqlite3_str_append(pAccum, ".", 1);
29010 }
29011 sqlite3_str_appendall(pAccum, pItem->zName);
29012 length = width = 0;
29013 break;
29014 }
29015 default: {
29016 assert( xtype==etINVALID );
29017 return;
29018 }
29019 }/* End switch over the format type */
29020 /*
29021 ** The text of the conversion is pointed to by "bufpt" and is
29022 ** "length" characters long. The field width is "width". Do
29023 ** the output. Both length and width are in bytes, not characters,
29024 ** at this point. If the "!" flag was present on string conversions
29025 ** indicating that width and precision should be expressed in characters,
29026 ** then the values have been translated prior to reaching this point.
29027 */
29028 width -= length;
29029 if( width>0 ){
29030 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
29031 sqlite3_str_append(pAccum, bufpt, length);
29032 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
29033 }else{
29034 sqlite3_str_append(pAccum, bufpt, length);
29035 }
29036
29037 if( zExtra ){
29038 sqlite3DbFree(pAccum->db, zExtra);
29039 zExtra = 0;
29040 }
29041 }/* End for loop over the format string */
29042} /* End of function */
29043
29044/*
29045** Enlarge the memory allocation on a StrAccum object so that it is
29046** able to accept at least N more bytes of text.
29047**
29048** Return the number of bytes of text that StrAccum is able to accept
29049** after the attempted enlargement. The value returned might be zero.
29050*/
29051static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29052 char *zNew;
29053 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29054 if( p->accError ){
29055 testcase(p->accError==SQLITE_TOOBIG);
29056 testcase(p->accError==SQLITE_NOMEM);
29057 return 0;
29058 }
29059 if( p->mxAlloc==0 ){
29060 setStrAccumError(p, SQLITE_TOOBIG);
29061 return p->nAlloc - p->nChar - 1;
29062 }else{
29063 char *zOld = isMalloced(p) ? p->zText : 0;
29064 i64 szNew = p->nChar;
29065 szNew += N + 1;
29066 if( szNew+p->nChar<=p->mxAlloc ){
29067 /* Force exponential buffer size growth as long as it does not overflow,
29068 ** to avoid having to call this routine too often */
29069 szNew += p->nChar;
29070 }
29071 if( szNew > p->mxAlloc ){
29072 sqlite3_str_reset(p);
29073 setStrAccumError(p, SQLITE_TOOBIG);
29074 return 0;
29075 }else{
29076 p->nAlloc = (int)szNew;
29077 }
29078 if( p->db ){
29079 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
29080 }else{
29081 zNew = sqlite3Realloc(zOld, p->nAlloc);
29082 }
29083 if( zNew ){
29084 assert( p->zText!=0 || p->nChar==0 );
29085 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
29086 p->zText = zNew;
29087 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
29088 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29089 }else{
29090 sqlite3_str_reset(p);
29091 setStrAccumError(p, SQLITE_NOMEM);
29092 return 0;
29093 }
29094 }
29095 return N;
29096}
29097
29098/*
29099** Append N copies of character c to the given string buffer.
29100*/
29101SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
29102 testcase( p->nChar + (i64)N > 0x7fffffff );
29103 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
29104 return;
29105 }
29106 while( (N--)>0 ) p->zText[p->nChar++] = c;
29107}
29108
29109/*
29110** The StrAccum "p" is not large enough to accept N new bytes of z[].
29111** So enlarge if first, then do the append.
29112**
29113** This is a helper routine to sqlite3_str_append() that does special-case
29114** work (enlarging the buffer) using tail recursion, so that the
29115** sqlite3_str_append() routine can use fast calling semantics.
29116*/
29117static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
29118 N = sqlite3StrAccumEnlarge(p, N);
29119 if( N>0 ){
29120 memcpy(&p->zText[p->nChar], z, N);
29121 p->nChar += N;
29122 }
29123}
29124
29125/*
29126** Append N bytes of text from z to the StrAccum object. Increase the
29127** size of the memory allocation for StrAccum if necessary.
29128*/
29129SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
29130 assert( z!=0 || N==0 );
29131 assert( p->zText!=0 || p->nChar==0 || p->accError );
29132 assert( N>=0 );
29133 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
29134 if( p->nChar+N >= p->nAlloc ){
29135 enlargeAndAppend(p,z,N);
29136 }else if( N ){
29137 assert( p->zText );
29138 p->nChar += N;
29139 memcpy(&p->zText[p->nChar-N], z, N);
29140 }
29141}
29142
29143/*
29144** Append the complete text of zero-terminated string z[] to the p string.
29145*/
29146SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
29147 sqlite3_str_append(p, z, sqlite3Strlen30(z));
29148}
29149
29150
29151/*
29152** Finish off a string by making sure it is zero-terminated.
29153** Return a pointer to the resulting string. Return a NULL
29154** pointer if any kind of error was encountered.
29155*/
29156static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
29157 char *zText;
29158 assert( p->mxAlloc>0 && !isMalloced(p) );
29159 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
29160 if( zText ){
29161 memcpy(zText, p->zText, p->nChar+1);
29162 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29163 }else{
29164 setStrAccumError(p, SQLITE_NOMEM);
29165 }
29166 p->zText = zText;
29167 return zText;
29168}
29169SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
29170 if( p->zText ){
29171 p->zText[p->nChar] = 0;
29172 if( p->mxAlloc>0 && !isMalloced(p) ){
29173 return strAccumFinishRealloc(p);
29174 }
29175 }
29176 return p->zText;
29177}
29178
29179/*
29180** This singleton is an sqlite3_str object that is returned if
29181** sqlite3_malloc() fails to provide space for a real one. This
29182** sqlite3_str object accepts no new text and always returns
29183** an SQLITE_NOMEM error.
29184*/
29185static sqlite3_str sqlite3OomStr = {
29186 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
29187};
29188
29189/* Finalize a string created using sqlite3_str_new().
29190*/
29191SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
29192 char *z;
29193 if( p!=0 && p!=&sqlite3OomStr ){
29194 z = sqlite3StrAccumFinish(p);
29195 sqlite3_free(p);
29196 }else{
29197 z = 0;
29198 }
29199 return z;
29200}
29201
29202/* Return any error code associated with p */
29203SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
29204 return p ? p->accError : SQLITE_NOMEM;
29205}
29206
29207/* Return the current length of p in bytes */
29208SQLITE_API int sqlite3_str_length(sqlite3_str *p){
29209 return p ? p->nChar : 0;
29210}
29211
29212/* Return the current value for p */
29213SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
29214 if( p==0 || p->nChar==0 ) return 0;
29215 p->zText[p->nChar] = 0;
29216 return p->zText;
29217}
29218
29219/*
29220** Reset an StrAccum string. Reclaim all malloced memory.
29221*/
29222SQLITE_API void sqlite3_str_reset(StrAccum *p){
29223 if( isMalloced(p) ){
29224 sqlite3DbFree(p->db, p->zText);
29225 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
29226 }
29227 p->nAlloc = 0;
29228 p->nChar = 0;
29229 p->zText = 0;
29230}
29231
29232/*
29233** Initialize a string accumulator.
29234**
29235** p: The accumulator to be initialized.
29236** db: Pointer to a database connection. May be NULL. Lookaside
29237** memory is used if not NULL. db->mallocFailed is set appropriately
29238** when not NULL.
29239** zBase: An initial buffer. May be NULL in which case the initial buffer
29240** is malloced.
29241** n: Size of zBase in bytes. If total space requirements never exceed
29242** n then no memory allocations ever occur.
29243** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
29244** allocations will ever occur.
29245*/
29246SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
29247 p->zText = zBase;
29248 p->db = db;
29249 p->nAlloc = n;
29250 p->mxAlloc = mx;
29251 p->nChar = 0;
29252 p->accError = 0;
29253 p->printfFlags = 0;
29254}
29255
29256/* Allocate and initialize a new dynamic string object */
29257SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
29258 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
29259 if( p ){
29260 sqlite3StrAccumInit(p, 0, 0, 0,
29261 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
29262 }else{
29263 p = &sqlite3OomStr;
29264 }
29265 return p;
29266}
29267
29268/*
29269** Print into memory obtained from sqliteMalloc(). Use the internal
29270** %-conversion extensions.
29271*/
29272SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
29273 char *z;
29274 char zBase[SQLITE_PRINT_BUF_SIZE];
29275 StrAccum acc;
29276 assert( db!=0 );
29277 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
29278 db->aLimit[SQLITE_LIMIT_LENGTH]);
29279 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
29280 sqlite3_str_vappendf(&acc, zFormat, ap);
29281 z = sqlite3StrAccumFinish(&acc);
29282 if( acc.accError==SQLITE_NOMEM ){
29283 sqlite3OomFault(db);
29284 }
29285 return z;
29286}
29287
29288/*
29289** Print into memory obtained from sqliteMalloc(). Use the internal
29290** %-conversion extensions.
29291*/
29292SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
29293 va_list ap;
29294 char *z;
29295 va_start(ap, zFormat);
29296 z = sqlite3VMPrintf(db, zFormat, ap);
29297 va_end(ap);
29298 return z;
29299}
29300
29301/*
29302** Print into memory obtained from sqlite3_malloc(). Omit the internal
29303** %-conversion extensions.
29304*/
29305SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
29306 char *z;
29307 char zBase[SQLITE_PRINT_BUF_SIZE];
29308 StrAccum acc;
29309
29310#ifdef SQLITE_ENABLE_API_ARMOR
29311 if( zFormat==0 ){
29312 (void)SQLITE_MISUSE_BKPT;
29313 return 0;
29314 }
29315#endif
29316#ifndef SQLITE_OMIT_AUTOINIT
29317 if( sqlite3_initialize() ) return 0;
29318#endif
29319 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
29320 sqlite3_str_vappendf(&acc, zFormat, ap);
29321 z = sqlite3StrAccumFinish(&acc);
29322 return z;
29323}
29324
29325/*
29326** Print into memory obtained from sqlite3_malloc()(). Omit the internal
29327** %-conversion extensions.
29328*/
29329SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
29330 va_list ap;
29331 char *z;
29332#ifndef SQLITE_OMIT_AUTOINIT
29333 if( sqlite3_initialize() ) return 0;
29334#endif
29335 va_start(ap, zFormat);
29336 z = sqlite3_vmprintf(zFormat, ap);
29337 va_end(ap);
29338 return z;
29339}
29340
29341/*
29342** sqlite3_snprintf() works like snprintf() except that it ignores the
29343** current locale settings. This is important for SQLite because we
29344** are not able to use a "," as the decimal point in place of "." as
29345** specified by some locales.
29346**
29347** Oops: The first two arguments of sqlite3_snprintf() are backwards
29348** from the snprintf() standard. Unfortunately, it is too late to change
29349** this without breaking compatibility, so we just have to live with the
29350** mistake.
29351**
29352** sqlite3_vsnprintf() is the varargs version.
29353*/
29354SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
29355 StrAccum acc;
29356 if( n<=0 ) return zBuf;
29357#ifdef SQLITE_ENABLE_API_ARMOR
29358 if( zBuf==0 || zFormat==0 ) {
29359 (void)SQLITE_MISUSE_BKPT;
29360 if( zBuf ) zBuf[0] = 0;
29361 return zBuf;
29362 }
29363#endif
29364 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
29365 sqlite3_str_vappendf(&acc, zFormat, ap);
29366 zBuf[acc.nChar] = 0;
29367 return zBuf;
29368}
29369SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
29370 char *z;
29371 va_list ap;
29372 va_start(ap,zFormat);
29373 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
29374 va_end(ap);
29375 return z;
29376}
29377
29378/*
29379** This is the routine that actually formats the sqlite3_log() message.
29380** We house it in a separate routine from sqlite3_log() to avoid using
29381** stack space on small-stack systems when logging is disabled.
29382**
29383** sqlite3_log() must render into a static buffer. It cannot dynamically
29384** allocate memory because it might be called while the memory allocator
29385** mutex is held.
29386**
29387** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
29388** certain format characters (%q) or for very large precisions or widths.
29389** Care must be taken that any sqlite3_log() calls that occur while the
29390** memory mutex is held do not use these mechanisms.
29391*/
29392static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
29393 StrAccum acc; /* String accumulator */
29394 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
29395
29396 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
29397 sqlite3_str_vappendf(&acc, zFormat, ap);
29398 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
29399 sqlite3StrAccumFinish(&acc));
29400}
29401
29402/*
29403** Format and write a message to the log if logging is enabled.
29404*/
29405SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
29406 va_list ap; /* Vararg list */
29407 if( sqlite3GlobalConfig.xLog ){
29408 va_start(ap, zFormat);
29409 renderLogMsg(iErrCode, zFormat, ap);
29410 va_end(ap);
29411 }
29412}
29413
29414#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
29415/*
29416** A version of printf() that understands %lld. Used for debugging.
29417** The printf() built into some versions of windows does not understand %lld
29418** and segfaults if you give it a long long int.
29419*/
29420SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
29421 va_list ap;
29422 StrAccum acc;
29423 char zBuf[SQLITE_PRINT_BUF_SIZE*10];
29424 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
29425 va_start(ap,zFormat);
29426 sqlite3_str_vappendf(&acc, zFormat, ap);
29427 va_end(ap);
29428 sqlite3StrAccumFinish(&acc);
29429#ifdef SQLITE_OS_TRACE_PROC
29430 {
29431 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
29432 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
29433 }
29434#else
29435 fprintf(stdout,"%s", zBuf);
29436 fflush(stdout);
29437#endif
29438}
29439#endif
29440
29441
29442/*
29443** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
29444** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
29445*/
29446SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
29447 va_list ap;
29448 va_start(ap,zFormat);
29449 sqlite3_str_vappendf(p, zFormat, ap);
29450 va_end(ap);
29451}
29452
29453/************** End of printf.c **********************************************/
29454/************** Begin file treeview.c ****************************************/
29455/*
29456** 2015-06-08
29457**
29458** The author disclaims copyright to this source code. In place of
29459** a legal notice, here is a blessing:
29460**
29461** May you do good and not evil.
29462** May you find forgiveness for yourself and forgive others.
29463** May you share freely, never taking more than you give.
29464**
29465*************************************************************************
29466**
29467** This file contains C code to implement the TreeView debugging routines.
29468** These routines print a parse tree to standard output for debugging and
29469** analysis.
29470**
29471** The interfaces in this file is only available when compiling
29472** with SQLITE_DEBUG.
29473*/
29474/* #include "sqliteInt.h" */
29475#ifdef SQLITE_DEBUG
29476
29477/*
29478** Add a new subitem to the tree. The moreToFollow flag indicates that this
29479** is not the last item in the tree.
29480*/
29481static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
29482 if( p==0 ){
29483 p = sqlite3_malloc64( sizeof(*p) );
29484 if( p==0 ) return 0;
29485 memset(p, 0, sizeof(*p));
29486 }else{
29487 p->iLevel++;
29488 }
29489 assert( moreToFollow==0 || moreToFollow==1 );
29490 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
29491 return p;
29492}
29493
29494/*
29495** Finished with one layer of the tree
29496*/
29497static void sqlite3TreeViewPop(TreeView *p){
29498 if( p==0 ) return;
29499 p->iLevel--;
29500 if( p->iLevel<0 ) sqlite3_free(p);
29501}
29502
29503/*
29504** Generate a single line of output for the tree, with a prefix that contains
29505** all the appropriate tree lines
29506*/
29507static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
29508 va_list ap;
29509 int i;
29510 StrAccum acc;
29511 char zBuf[500];
29512 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
29513 if( p ){
29514 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
29515 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
29516 }
29517 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
29518 }
29519 if( zFormat!=0 ){
29520 va_start(ap, zFormat);
29521 sqlite3_str_vappendf(&acc, zFormat, ap);
29522 va_end(ap);
29523 assert( acc.nChar>0 || acc.accError );
29524 sqlite3_str_append(&acc, "\n", 1);
29525 }
29526 sqlite3StrAccumFinish(&acc);
29527 fprintf(stdout,"%s", zBuf);
29528 fflush(stdout);
29529}
29530
29531/*
29532** Shorthand for starting a new tree item that consists of a single label
29533*/
29534static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
29535 p = sqlite3TreeViewPush(p, moreFollows);
29536 sqlite3TreeViewLine(p, "%s", zLabel);
29537}
29538
29539/*
29540** Generate a human-readable description of a WITH clause.
29541*/
29542SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
29543 int i;
29544 if( pWith==0 ) return;
29545 if( pWith->nCte==0 ) return;
29546 if( pWith->pOuter ){
29547 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
29548 }else{
29549 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
29550 }
29551 if( pWith->nCte>0 ){
29552 pView = sqlite3TreeViewPush(pView, 1);
29553 for(i=0; i<pWith->nCte; i++){
29554 StrAccum x;
29555 char zLine[1000];
29556 const struct Cte *pCte = &pWith->a[i];
29557 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
29558 sqlite3_str_appendf(&x, "%s", pCte->zName);
29559 if( pCte->pCols && pCte->pCols->nExpr>0 ){
29560 char cSep = '(';
29561 int j;
29562 for(j=0; j<pCte->pCols->nExpr; j++){
29563 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
29564 cSep = ',';
29565 }
29566 sqlite3_str_appendf(&x, ")");
29567 }
29568 sqlite3_str_appendf(&x, " AS");
29569 sqlite3StrAccumFinish(&x);
29570 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
29571 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
29572 sqlite3TreeViewPop(pView);
29573 }
29574 sqlite3TreeViewPop(pView);
29575 }
29576}
29577
29578/*
29579** Generate a human-readable description of a SrcList object.
29580*/
29581SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
29582 int i;
29583 for(i=0; i<pSrc->nSrc; i++){
29584 const struct SrcList_item *pItem = &pSrc->a[i];
29585 StrAccum x;
29586 char zLine[100];
29587 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
29588 sqlite3_str_appendf(&x, "{%d:*}", pItem->iCursor);
29589 if( pItem->zDatabase ){
29590 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
29591 }else if( pItem->zName ){
29592 sqlite3_str_appendf(&x, " %s", pItem->zName);
29593 }
29594 if( pItem->pTab ){
29595 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx",
29596 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
29597 }
29598 if( pItem->zAlias ){
29599 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
29600 }
29601 if( pItem->fg.jointype & JT_LEFT ){
29602 sqlite3_str_appendf(&x, " LEFT-JOIN");
29603 }
29604 if( pItem->fg.fromDDL ){
29605 sqlite3_str_appendf(&x, " DDL");
29606 }
29607 sqlite3StrAccumFinish(&x);
29608 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
29609 if( pItem->pSelect ){
29610 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
29611 }
29612 if( pItem->fg.isTabFunc ){
29613 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
29614 }
29615 sqlite3TreeViewPop(pView);
29616 }
29617}
29618
29619/*
29620** Generate a human-readable description of a Select object.
29621*/
29622SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
29623 int n = 0;
29624 int cnt = 0;
29625 if( p==0 ){
29626 sqlite3TreeViewLine(pView, "nil-SELECT");
29627 return;
29628 }
29629 pView = sqlite3TreeViewPush(pView, moreToFollow);
29630 if( p->pWith ){
29631 sqlite3TreeViewWith(pView, p->pWith, 1);
29632 cnt = 1;
29633 sqlite3TreeViewPush(pView, 1);
29634 }
29635 do{
29636 if( p->selFlags & SF_WhereBegin ){
29637 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
29638 }else{
29639 sqlite3TreeViewLine(pView,
29640 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
29641 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
29642 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
29643 p->selId, p, p->selFlags,
29644 (int)p->nSelectRow
29645 );
29646 }
29647 if( cnt++ ) sqlite3TreeViewPop(pView);
29648 if( p->pPrior ){
29649 n = 1000;
29650 }else{
29651 n = 0;
29652 if( p->pSrc && p->pSrc->nSrc ) n++;
29653 if( p->pWhere ) n++;
29654 if( p->pGroupBy ) n++;
29655 if( p->pHaving ) n++;
29656 if( p->pOrderBy ) n++;
29657 if( p->pLimit ) n++;
29658#ifndef SQLITE_OMIT_WINDOWFUNC
29659 if( p->pWin ) n++;
29660 if( p->pWinDefn ) n++;
29661#endif
29662 }
29663 if( p->pEList ){
29664 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
29665 }
29666 n--;
29667#ifndef SQLITE_OMIT_WINDOWFUNC
29668 if( p->pWin ){
29669 Window *pX;
29670 pView = sqlite3TreeViewPush(pView, (n--)>0);
29671 sqlite3TreeViewLine(pView, "window-functions");
29672 for(pX=p->pWin; pX; pX=pX->pNextWin){
29673 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
29674 }
29675 sqlite3TreeViewPop(pView);
29676 }
29677#endif
29678 if( p->pSrc && p->pSrc->nSrc ){
29679 pView = sqlite3TreeViewPush(pView, (n--)>0);
29680 sqlite3TreeViewLine(pView, "FROM");
29681 sqlite3TreeViewSrcList(pView, p->pSrc);
29682 sqlite3TreeViewPop(pView);
29683 }
29684 if( p->pWhere ){
29685 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
29686 sqlite3TreeViewExpr(pView, p->pWhere, 0);
29687 sqlite3TreeViewPop(pView);
29688 }
29689 if( p->pGroupBy ){
29690 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
29691 }
29692 if( p->pHaving ){
29693 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
29694 sqlite3TreeViewExpr(pView, p->pHaving, 0);
29695 sqlite3TreeViewPop(pView);
29696 }
29697#ifndef SQLITE_OMIT_WINDOWFUNC
29698 if( p->pWinDefn ){
29699 Window *pX;
29700 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
29701 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
29702 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
29703 }
29704 sqlite3TreeViewPop(pView);
29705 }
29706#endif
29707 if( p->pOrderBy ){
29708 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
29709 }
29710 if( p->pLimit ){
29711 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
29712 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
29713 if( p->pLimit->pRight ){
29714 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
29715 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
29716 sqlite3TreeViewPop(pView);
29717 }
29718 sqlite3TreeViewPop(pView);
29719 }
29720 if( p->pPrior ){
29721 const char *zOp = "UNION";
29722 switch( p->op ){
29723 case TK_ALL: zOp = "UNION ALL"; break;
29724 case TK_INTERSECT: zOp = "INTERSECT"; break;
29725 case TK_EXCEPT: zOp = "EXCEPT"; break;
29726 }
29727 sqlite3TreeViewItem(pView, zOp, 1);
29728 }
29729 p = p->pPrior;
29730 }while( p!=0 );
29731 sqlite3TreeViewPop(pView);
29732}
29733
29734#ifndef SQLITE_OMIT_WINDOWFUNC
29735/*
29736** Generate a description of starting or stopping bounds
29737*/
29738SQLITE_PRIVATE void sqlite3TreeViewBound(
29739 TreeView *pView, /* View context */
29740 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
29741 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
29742 u8 moreToFollow /* True if more to follow */
29743){
29744 switch( eBound ){
29745 case TK_UNBOUNDED: {
29746 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
29747 sqlite3TreeViewPop(pView);
29748 break;
29749 }
29750 case TK_CURRENT: {
29751 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
29752 sqlite3TreeViewPop(pView);
29753 break;
29754 }
29755 case TK_PRECEDING: {
29756 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
29757 sqlite3TreeViewExpr(pView, pExpr, 0);
29758 sqlite3TreeViewPop(pView);
29759 break;
29760 }
29761 case TK_FOLLOWING: {
29762 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
29763 sqlite3TreeViewExpr(pView, pExpr, 0);
29764 sqlite3TreeViewPop(pView);
29765 break;
29766 }
29767 }
29768}
29769#endif /* SQLITE_OMIT_WINDOWFUNC */
29770
29771#ifndef SQLITE_OMIT_WINDOWFUNC
29772/*
29773** Generate a human-readable explanation for a Window object
29774*/
29775SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
29776 int nElement = 0;
29777 if( pWin->pFilter ){
29778 sqlite3TreeViewItem(pView, "FILTER", 1);
29779 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
29780 sqlite3TreeViewPop(pView);
29781 }
29782 pView = sqlite3TreeViewPush(pView, more);
29783 if( pWin->zName ){
29784 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
29785 }else{
29786 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
29787 }
29788 if( pWin->zBase ) nElement++;
29789 if( pWin->pOrderBy ) nElement++;
29790 if( pWin->eFrmType ) nElement++;
29791 if( pWin->eExclude ) nElement++;
29792 if( pWin->zBase ){
29793 sqlite3TreeViewPush(pView, (--nElement)>0);
29794 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
29795 sqlite3TreeViewPop(pView);
29796 }
29797 if( pWin->pPartition ){
29798 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
29799 }
29800 if( pWin->pOrderBy ){
29801 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
29802 }
29803 if( pWin->eFrmType ){
29804 char zBuf[30];
29805 const char *zFrmType = "ROWS";
29806 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
29807 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
29808 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
29809 pWin->bImplicitFrame ? " (implied)" : "");
29810 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
29811 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
29812 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
29813 sqlite3TreeViewPop(pView);
29814 }
29815 if( pWin->eExclude ){
29816 char zBuf[30];
29817 const char *zExclude;
29818 switch( pWin->eExclude ){
29819 case TK_NO: zExclude = "NO OTHERS"; break;
29820 case TK_CURRENT: zExclude = "CURRENT ROW"; break;
29821 case TK_GROUP: zExclude = "GROUP"; break;
29822 case TK_TIES: zExclude = "TIES"; break;
29823 default:
29824 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
29825 zExclude = zBuf;
29826 break;
29827 }
29828 sqlite3TreeViewPush(pView, 0);
29829 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
29830 sqlite3TreeViewPop(pView);
29831 }
29832 sqlite3TreeViewPop(pView);
29833}
29834#endif /* SQLITE_OMIT_WINDOWFUNC */
29835
29836#ifndef SQLITE_OMIT_WINDOWFUNC
29837/*
29838** Generate a human-readable explanation for a Window Function object
29839*/
29840SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
29841 pView = sqlite3TreeViewPush(pView, more);
29842 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
29843 pWin->pFunc->zName, pWin->pFunc->nArg);
29844 sqlite3TreeViewWindow(pView, pWin, 0);
29845 sqlite3TreeViewPop(pView);
29846}
29847#endif /* SQLITE_OMIT_WINDOWFUNC */
29848
29849/*
29850** Generate a human-readable explanation of an expression tree.
29851*/
29852SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
29853 const char *zBinOp = 0; /* Binary operator */
29854 const char *zUniOp = 0; /* Unary operator */
29855 char zFlgs[200];
29856 pView = sqlite3TreeViewPush(pView, moreToFollow);
29857 if( pExpr==0 ){
29858 sqlite3TreeViewLine(pView, "nil");
29859 sqlite3TreeViewPop(pView);
29860 return;
29861 }
29862 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
29863 StrAccum x;
29864 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
29865 sqlite3_str_appendf(&x, " fg.af=%x.%c",
29866 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
29867 if( ExprHasProperty(pExpr, EP_FromJoin) ){
29868 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
29869 }
29870 if( ExprHasProperty(pExpr, EP_FromDDL) ){
29871 sqlite3_str_appendf(&x, " DDL");
29872 }
29873 if( ExprHasVVAProperty(pExpr, EP_Immutable) ){
29874 sqlite3_str_appendf(&x, " IMMUTABLE");
29875 }
29876 sqlite3StrAccumFinish(&x);
29877 }else{
29878 zFlgs[0] = 0;
29879 }
29880 switch( pExpr->op ){
29881 case TK_AGG_COLUMN: {
29882 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
29883 pExpr->iTable, pExpr->iColumn, zFlgs);
29884 break;
29885 }
29886 case TK_COLUMN: {
29887 if( pExpr->iTable<0 ){
29888 /* This only happens when coding check constraints */
29889 char zOp2[16];
29890 if( pExpr->op2 ){
29891 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
29892 }else{
29893 zOp2[0] = 0;
29894 }
29895 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
29896 pExpr->iColumn, zFlgs, zOp2);
29897 }else{
29898 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
29899 pExpr->iTable, pExpr->iColumn,
29900 pExpr->y.pTab, zFlgs);
29901 }
29902 if( ExprHasProperty(pExpr, EP_FixedCol) ){
29903 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29904 }
29905 break;
29906 }
29907 case TK_INTEGER: {
29908 if( pExpr->flags & EP_IntValue ){
29909 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
29910 }else{
29911 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
29912 }
29913 break;
29914 }
29915#ifndef SQLITE_OMIT_FLOATING_POINT
29916 case TK_FLOAT: {
29917 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29918 break;
29919 }
29920#endif
29921 case TK_STRING: {
29922 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
29923 break;
29924 }
29925 case TK_NULL: {
29926 sqlite3TreeViewLine(pView,"NULL");
29927 break;
29928 }
29929 case TK_TRUEFALSE: {
29930 sqlite3TreeViewLine(pView,
29931 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
29932 break;
29933 }
29934#ifndef SQLITE_OMIT_BLOB_LITERAL
29935 case TK_BLOB: {
29936 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29937 break;
29938 }
29939#endif
29940 case TK_VARIABLE: {
29941 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
29942 pExpr->u.zToken, pExpr->iColumn);
29943 break;
29944 }
29945 case TK_REGISTER: {
29946 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
29947 break;
29948 }
29949 case TK_ID: {
29950 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
29951 break;
29952 }
29953#ifndef SQLITE_OMIT_CAST
29954 case TK_CAST: {
29955 /* Expressions of the form: CAST(pLeft AS token) */
29956 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
29957 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29958 break;
29959 }
29960#endif /* SQLITE_OMIT_CAST */
29961 case TK_LT: zBinOp = "LT"; break;
29962 case TK_LE: zBinOp = "LE"; break;
29963 case TK_GT: zBinOp = "GT"; break;
29964 case TK_GE: zBinOp = "GE"; break;
29965 case TK_NE: zBinOp = "NE"; break;
29966 case TK_EQ: zBinOp = "EQ"; break;
29967 case TK_IS: zBinOp = "IS"; break;
29968 case TK_ISNOT: zBinOp = "ISNOT"; break;
29969 case TK_AND: zBinOp = "AND"; break;
29970 case TK_OR: zBinOp = "OR"; break;
29971 case TK_PLUS: zBinOp = "ADD"; break;
29972 case TK_STAR: zBinOp = "MUL"; break;
29973 case TK_MINUS: zBinOp = "SUB"; break;
29974 case TK_REM: zBinOp = "REM"; break;
29975 case TK_BITAND: zBinOp = "BITAND"; break;
29976 case TK_BITOR: zBinOp = "BITOR"; break;
29977 case TK_SLASH: zBinOp = "DIV"; break;
29978 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
29979 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
29980 case TK_CONCAT: zBinOp = "CONCAT"; break;
29981 case TK_DOT: zBinOp = "DOT"; break;
29982 case TK_LIMIT: zBinOp = "LIMIT"; break;
29983
29984 case TK_UMINUS: zUniOp = "UMINUS"; break;
29985 case TK_UPLUS: zUniOp = "UPLUS"; break;
29986 case TK_BITNOT: zUniOp = "BITNOT"; break;
29987 case TK_NOT: zUniOp = "NOT"; break;
29988 case TK_ISNULL: zUniOp = "ISNULL"; break;
29989 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
29990
29991 case TK_TRUTH: {
29992 int x;
29993 const char *azOp[] = {
29994 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
29995 };
29996 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
29997 assert( pExpr->pRight );
29998 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
29999 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
30000 zUniOp = azOp[x];
30001 break;
30002 }
30003
30004 case TK_SPAN: {
30005 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
30006 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30007 break;
30008 }
30009
30010 case TK_COLLATE: {
30011 /* COLLATE operators without the EP_Collate flag are intended to
30012 ** emulate collation associated with a table column. These show
30013 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
30014 ** operators that appear in the original SQL always have the
30015 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
30016 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
30017 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
30018 pExpr->u.zToken, zFlgs);
30019 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30020 break;
30021 }
30022
30023 case TK_AGG_FUNCTION:
30024 case TK_FUNCTION: {
30025 ExprList *pFarg; /* List of function arguments */
30026 Window *pWin;
30027 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
30028 pFarg = 0;
30029 pWin = 0;
30030 }else{
30031 pFarg = pExpr->x.pList;
30032#ifndef SQLITE_OMIT_WINDOWFUNC
30033 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
30034#else
30035 pWin = 0;
30036#endif
30037 }
30038 if( pExpr->op==TK_AGG_FUNCTION ){
30039 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
30040 pExpr->op2, pExpr->u.zToken, zFlgs,
30041 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
30042 pExpr->iAgg, pExpr->pAggInfo);
30043 }else if( pExpr->op2!=0 ){
30044 const char *zOp2;
30045 char zBuf[8];
30046 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
30047 zOp2 = zBuf;
30048 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
30049 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
30050 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
30051 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
30052 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
30053 pExpr->u.zToken, zFlgs, zOp2);
30054 }else{
30055 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
30056 }
30057 if( pFarg ){
30058 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
30059 }
30060#ifndef SQLITE_OMIT_WINDOWFUNC
30061 if( pWin ){
30062 sqlite3TreeViewWindow(pView, pWin, 0);
30063 }
30064#endif
30065 break;
30066 }
30067#ifndef SQLITE_OMIT_SUBQUERY
30068 case TK_EXISTS: {
30069 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
30070 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30071 break;
30072 }
30073 case TK_SELECT: {
30074 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
30075 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30076 break;
30077 }
30078 case TK_IN: {
30079 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
30080 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30081 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
30082 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30083 }else{
30084 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30085 }
30086 break;
30087 }
30088#endif /* SQLITE_OMIT_SUBQUERY */
30089
30090 /*
30091 ** x BETWEEN y AND z
30092 **
30093 ** This is equivalent to
30094 **
30095 ** x>=y AND x<=z
30096 **
30097 ** X is stored in pExpr->pLeft.
30098 ** Y is stored in pExpr->pList->a[0].pExpr.
30099 ** Z is stored in pExpr->pList->a[1].pExpr.
30100 */
30101 case TK_BETWEEN: {
30102 Expr *pX = pExpr->pLeft;
30103 Expr *pY = pExpr->x.pList->a[0].pExpr;
30104 Expr *pZ = pExpr->x.pList->a[1].pExpr;
30105 sqlite3TreeViewLine(pView, "BETWEEN");
30106 sqlite3TreeViewExpr(pView, pX, 1);
30107 sqlite3TreeViewExpr(pView, pY, 1);
30108 sqlite3TreeViewExpr(pView, pZ, 0);
30109 break;
30110 }
30111 case TK_TRIGGER: {
30112 /* If the opcode is TK_TRIGGER, then the expression is a reference
30113 ** to a column in the new.* or old.* pseudo-tables available to
30114 ** trigger programs. In this case Expr.iTable is set to 1 for the
30115 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
30116 ** is set to the column of the pseudo-table to read, or to -1 to
30117 ** read the rowid field.
30118 */
30119 sqlite3TreeViewLine(pView, "%s(%d)",
30120 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
30121 break;
30122 }
30123 case TK_CASE: {
30124 sqlite3TreeViewLine(pView, "CASE");
30125 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30126 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30127 break;
30128 }
30129#ifndef SQLITE_OMIT_TRIGGER
30130 case TK_RAISE: {
30131 const char *zType = "unk";
30132 switch( pExpr->affExpr ){
30133 case OE_Rollback: zType = "rollback"; break;
30134 case OE_Abort: zType = "abort"; break;
30135 case OE_Fail: zType = "fail"; break;
30136 case OE_Ignore: zType = "ignore"; break;
30137 }
30138 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
30139 break;
30140 }
30141#endif
30142 case TK_MATCH: {
30143 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
30144 pExpr->iTable, pExpr->iColumn, zFlgs);
30145 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30146 break;
30147 }
30148 case TK_VECTOR: {
30149 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
30150 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
30151 sqlite3_free(z);
30152 break;
30153 }
30154 case TK_SELECT_COLUMN: {
30155 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
30156 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
30157 break;
30158 }
30159 case TK_IF_NULL_ROW: {
30160 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
30161 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30162 break;
30163 }
30164 default: {
30165 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
30166 break;
30167 }
30168 }
30169 if( zBinOp ){
30170 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
30171 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30172 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30173 }else if( zUniOp ){
30174 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
30175 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30176 }
30177 sqlite3TreeViewPop(pView);
30178}
30179
30180
30181/*
30182** Generate a human-readable explanation of an expression list.
30183*/
30184SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
30185 TreeView *pView,
30186 const ExprList *pList,
30187 const char *zLabel
30188){
30189 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
30190 if( pList==0 ){
30191 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
30192 }else{
30193 int i;
30194 sqlite3TreeViewLine(pView, "%s", zLabel);
30195 for(i=0; i<pList->nExpr; i++){
30196 int j = pList->a[i].u.x.iOrderByCol;
30197 char *zName = pList->a[i].zEName;
30198 int moreToFollow = i<pList->nExpr - 1;
30199 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
30200 if( j || zName ){
30201 sqlite3TreeViewPush(pView, moreToFollow);
30202 moreToFollow = 0;
30203 sqlite3TreeViewLine(pView, 0);
30204 if( zName ){
30205 fprintf(stdout, "AS %s ", zName);
30206 }
30207 if( j ){
30208 fprintf(stdout, "iOrderByCol=%d", j);
30209 }
30210 fprintf(stdout, "\n");
30211 fflush(stdout);
30212 }
30213 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
30214 if( j || zName ){
30215 sqlite3TreeViewPop(pView);
30216 }
30217 }
30218 }
30219}
30220SQLITE_PRIVATE void sqlite3TreeViewExprList(
30221 TreeView *pView,
30222 const ExprList *pList,
30223 u8 moreToFollow,
30224 const char *zLabel
30225){
30226 pView = sqlite3TreeViewPush(pView, moreToFollow);
30227 sqlite3TreeViewBareExprList(pView, pList, zLabel);
30228 sqlite3TreeViewPop(pView);
30229}
30230
30231#endif /* SQLITE_DEBUG */
30232
30233/************** End of treeview.c ********************************************/
30234/************** Begin file random.c ******************************************/
30235/*
30236** 2001 September 15
30237**
30238** The author disclaims copyright to this source code. In place of
30239** a legal notice, here is a blessing:
30240**
30241** May you do good and not evil.
30242** May you find forgiveness for yourself and forgive others.
30243** May you share freely, never taking more than you give.
30244**
30245*************************************************************************
30246** This file contains code to implement a pseudo-random number
30247** generator (PRNG) for SQLite.
30248**
30249** Random numbers are used by some of the database backends in order
30250** to generate random integer keys for tables or random filenames.
30251*/
30252/* #include "sqliteInt.h" */
30253
30254
30255/* All threads share a single random number generator.
30256** This structure is the current state of the generator.
30257*/
30258static SQLITE_WSD struct sqlite3PrngType {
30259 unsigned char isInit; /* True if initialized */
30260 unsigned char i, j; /* State variables */
30261 unsigned char s[256]; /* State variables */
30262} sqlite3Prng;
30263
30264/*
30265** Return N random bytes.
30266*/
30267SQLITE_API void sqlite3_randomness(int N, void *pBuf){
30268 unsigned char t;
30269 unsigned char *zBuf = pBuf;
30270
30271 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
30272 ** state vector. If writable static data is unsupported on the target,
30273 ** we have to locate the state vector at run-time. In the more common
30274 ** case where writable static data is supported, wsdPrng can refer directly
30275 ** to the "sqlite3Prng" state vector declared above.
30276 */
30277#ifdef SQLITE_OMIT_WSD
30278 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
30279# define wsdPrng p[0]
30280#else
30281# define wsdPrng sqlite3Prng
30282#endif
30283
30284#if SQLITE_THREADSAFE
30285 sqlite3_mutex *mutex;
30286#endif
30287
30288#ifndef SQLITE_OMIT_AUTOINIT
30289 if( sqlite3_initialize() ) return;
30290#endif
30291
30292#if SQLITE_THREADSAFE
30293 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
30294#endif
30295
30296 sqlite3_mutex_enter(mutex);
30297 if( N<=0 || pBuf==0 ){
30298 wsdPrng.isInit = 0;
30299 sqlite3_mutex_leave(mutex);
30300 return;
30301 }
30302
30303 /* Initialize the state of the random number generator once,
30304 ** the first time this routine is called. The seed value does
30305 ** not need to contain a lot of randomness since we are not
30306 ** trying to do secure encryption or anything like that...
30307 **
30308 ** Nothing in this file or anywhere else in SQLite does any kind of
30309 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
30310 ** number generator) not as an encryption device.
30311 */
30312 if( !wsdPrng.isInit ){
30313 int i;
30314 char k[256];
30315 wsdPrng.j = 0;
30316 wsdPrng.i = 0;
30317 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
30318 for(i=0; i<256; i++){
30319 wsdPrng.s[i] = (u8)i;
30320 }
30321 for(i=0; i<256; i++){
30322 wsdPrng.j += wsdPrng.s[i] + k[i];
30323 t = wsdPrng.s[wsdPrng.j];
30324 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
30325 wsdPrng.s[i] = t;
30326 }
30327 wsdPrng.isInit = 1;
30328 }
30329
30330 assert( N>0 );
30331 do{
30332 wsdPrng.i++;
30333 t = wsdPrng.s[wsdPrng.i];
30334 wsdPrng.j += t;
30335 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
30336 wsdPrng.s[wsdPrng.j] = t;
30337 t += wsdPrng.s[wsdPrng.i];
30338 *(zBuf++) = wsdPrng.s[t];
30339 }while( --N );
30340 sqlite3_mutex_leave(mutex);
30341}
30342
30343#ifndef SQLITE_UNTESTABLE
30344/*
30345** For testing purposes, we sometimes want to preserve the state of
30346** PRNG and restore the PRNG to its saved state at a later time, or
30347** to reset the PRNG to its initial state. These routines accomplish
30348** those tasks.
30349**
30350** The sqlite3_test_control() interface calls these routines to
30351** control the PRNG.
30352*/
30353static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
30354SQLITE_PRIVATE void sqlite3PrngSaveState(void){
30355 memcpy(
30356 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
30357 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
30358 sizeof(sqlite3Prng)
30359 );
30360}
30361SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
30362 memcpy(
30363 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
30364 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
30365 sizeof(sqlite3Prng)
30366 );
30367}
30368#endif /* SQLITE_UNTESTABLE */
30369
30370/************** End of random.c **********************************************/
30371/************** Begin file threads.c *****************************************/
30372/*
30373** 2012 July 21
30374**
30375** The author disclaims copyright to this source code. In place of
30376** a legal notice, here is a blessing:
30377**
30378** May you do good and not evil.
30379** May you find forgiveness for yourself and forgive others.
30380** May you share freely, never taking more than you give.
30381**
30382******************************************************************************
30383**
30384** This file presents a simple cross-platform threading interface for
30385** use internally by SQLite.
30386**
30387** A "thread" can be created using sqlite3ThreadCreate(). This thread
30388** runs independently of its creator until it is joined using
30389** sqlite3ThreadJoin(), at which point it terminates.
30390**
30391** Threads do not have to be real. It could be that the work of the
30392** "thread" is done by the main thread at either the sqlite3ThreadCreate()
30393** or sqlite3ThreadJoin() call. This is, in fact, what happens in
30394** single threaded systems. Nothing in SQLite requires multiple threads.
30395** This interface exists so that applications that want to take advantage
30396** of multiple cores can do so, while also allowing applications to stay
30397** single-threaded if desired.
30398*/
30399/* #include "sqliteInt.h" */
30400#if SQLITE_OS_WIN
30401/* # include "os_win.h" */
30402#endif
30403
30404#if SQLITE_MAX_WORKER_THREADS>0
30405
30406/********************************* Unix Pthreads ****************************/
30407#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
30408
30409#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
30410/* #include <pthread.h> */
30411
30412/* A running thread */
30413struct SQLiteThread {
30414 pthread_t tid; /* Thread ID */
30415 int done; /* Set to true when thread finishes */
30416 void *pOut; /* Result returned by the thread */
30417 void *(*xTask)(void*); /* The thread routine */
30418 void *pIn; /* Argument to the thread */
30419};
30420
30421/* Create a new thread */
30422SQLITE_PRIVATE int sqlite3ThreadCreate(
30423 SQLiteThread **ppThread, /* OUT: Write the thread object here */
30424 void *(*xTask)(void*), /* Routine to run in a separate thread */
30425 void *pIn /* Argument passed into xTask() */
30426){
30427 SQLiteThread *p;
30428 int rc;
30429
30430 assert( ppThread!=0 );
30431 assert( xTask!=0 );
30432 /* This routine is never used in single-threaded mode */
30433 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
30434
30435 *ppThread = 0;
30436 p = sqlite3Malloc(sizeof(*p));
30437 if( p==0 ) return SQLITE_NOMEM_BKPT;
30438 memset(p, 0, sizeof(*p));
30439 p->xTask = xTask;
30440 p->pIn = pIn;
30441 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
30442 ** function that returns SQLITE_ERROR when passed the argument 200, that
30443 ** forces worker threads to run sequentially and deterministically
30444 ** for testing purposes. */
30445 if( sqlite3FaultSim(200) ){
30446 rc = 1;
30447 }else{
30448 rc = pthread_create(&p->tid, 0, xTask, pIn);
30449 }
30450 if( rc ){
30451 p->done = 1;
30452 p->pOut = xTask(pIn);
30453 }
30454 *ppThread = p;
30455 return SQLITE_OK;
30456}
30457
30458/* Get the results of the thread */
30459SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30460 int rc;
30461
30462 assert( ppOut!=0 );
30463 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30464 if( p->done ){
30465 *ppOut = p->pOut;
30466 rc = SQLITE_OK;
30467 }else{
30468 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
30469 }
30470 sqlite3_free(p);
30471 return rc;
30472}
30473
30474#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
30475/******************************** End Unix Pthreads *************************/
30476
30477
30478/********************************* Win32 Threads ****************************/
30479#if SQLITE_OS_WIN_THREADS
30480
30481#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
30482#include <process.h>
30483
30484/* A running thread */
30485struct SQLiteThread {
30486 void *tid; /* The thread handle */
30487 unsigned id; /* The thread identifier */
30488 void *(*xTask)(void*); /* The routine to run as a thread */
30489 void *pIn; /* Argument to xTask */
30490 void *pResult; /* Result of xTask */
30491};
30492
30493/* Thread procedure Win32 compatibility shim */
30494static unsigned __stdcall sqlite3ThreadProc(
30495 void *pArg /* IN: Pointer to the SQLiteThread structure */
30496){
30497 SQLiteThread *p = (SQLiteThread *)pArg;
30498
30499 assert( p!=0 );
30500#if 0
30501 /*
30502 ** This assert appears to trigger spuriously on certain
30503 ** versions of Windows, possibly due to _beginthreadex()
30504 ** and/or CreateThread() not fully setting their thread
30505 ** ID parameter before starting the thread.
30506 */
30507 assert( p->id==GetCurrentThreadId() );
30508#endif
30509 assert( p->xTask!=0 );
30510 p->pResult = p->xTask(p->pIn);
30511
30512 _endthreadex(0);
30513 return 0; /* NOT REACHED */
30514}
30515
30516/* Create a new thread */
30517SQLITE_PRIVATE int sqlite3ThreadCreate(
30518 SQLiteThread **ppThread, /* OUT: Write the thread object here */
30519 void *(*xTask)(void*), /* Routine to run in a separate thread */
30520 void *pIn /* Argument passed into xTask() */
30521){
30522 SQLiteThread *p;
30523
30524 assert( ppThread!=0 );
30525 assert( xTask!=0 );
30526 *ppThread = 0;
30527 p = sqlite3Malloc(sizeof(*p));
30528 if( p==0 ) return SQLITE_NOMEM_BKPT;
30529 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
30530 ** function that returns SQLITE_ERROR when passed the argument 200, that
30531 ** forces worker threads to run sequentially and deterministically
30532 ** (via the sqlite3FaultSim() term of the conditional) for testing
30533 ** purposes. */
30534 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
30535 memset(p, 0, sizeof(*p));
30536 }else{
30537 p->xTask = xTask;
30538 p->pIn = pIn;
30539 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
30540 if( p->tid==0 ){
30541 memset(p, 0, sizeof(*p));
30542 }
30543 }
30544 if( p->xTask==0 ){
30545 p->id = GetCurrentThreadId();
30546 p->pResult = xTask(pIn);
30547 }
30548 *ppThread = p;
30549 return SQLITE_OK;
30550}
30551
30552SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
30553
30554/* Get the results of the thread */
30555SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30556 DWORD rc;
30557 BOOL bRc;
30558
30559 assert( ppOut!=0 );
30560 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30561 if( p->xTask==0 ){
30562 /* assert( p->id==GetCurrentThreadId() ); */
30563 rc = WAIT_OBJECT_0;
30564 assert( p->tid==0 );
30565 }else{
30566 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
30567 rc = sqlite3Win32Wait((HANDLE)p->tid);
30568 assert( rc!=WAIT_IO_COMPLETION );
30569 bRc = CloseHandle((HANDLE)p->tid);
30570 assert( bRc );
30571 }
30572 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
30573 sqlite3_free(p);
30574 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
30575}
30576
30577#endif /* SQLITE_OS_WIN_THREADS */
30578/******************************** End Win32 Threads *************************/
30579
30580
30581/********************************* Single-Threaded **************************/
30582#ifndef SQLITE_THREADS_IMPLEMENTED
30583/*
30584** This implementation does not actually create a new thread. It does the
30585** work of the thread in the main thread, when either the thread is created
30586** or when it is joined
30587*/
30588
30589/* A running thread */
30590struct SQLiteThread {
30591 void *(*xTask)(void*); /* The routine to run as a thread */
30592 void *pIn; /* Argument to xTask */
30593 void *pResult; /* Result of xTask */
30594};
30595
30596/* Create a new thread */
30597SQLITE_PRIVATE int sqlite3ThreadCreate(
30598 SQLiteThread **ppThread, /* OUT: Write the thread object here */
30599 void *(*xTask)(void*), /* Routine to run in a separate thread */
30600 void *pIn /* Argument passed into xTask() */
30601){
30602 SQLiteThread *p;
30603
30604 assert( ppThread!=0 );
30605 assert( xTask!=0 );
30606 *ppThread = 0;
30607 p = sqlite3Malloc(sizeof(*p));
30608 if( p==0 ) return SQLITE_NOMEM_BKPT;
30609 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
30610 p->xTask = xTask;
30611 p->pIn = pIn;
30612 }else{
30613 p->xTask = 0;
30614 p->pResult = xTask(pIn);
30615 }
30616 *ppThread = p;
30617 return SQLITE_OK;
30618}
30619
30620/* Get the results of the thread */
30621SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
30622
30623 assert( ppOut!=0 );
30624 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
30625 if( p->xTask ){
30626 *ppOut = p->xTask(p->pIn);
30627 }else{
30628 *ppOut = p->pResult;
30629 }
30630 sqlite3_free(p);
30631
30632#if defined(SQLITE_TEST)
30633 {
30634 void *pTstAlloc = sqlite3Malloc(10);
30635 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
30636 sqlite3_free(pTstAlloc);
30637 }
30638#endif
30639
30640 return SQLITE_OK;
30641}
30642
30643#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
30644/****************************** End Single-Threaded *************************/
30645#endif /* SQLITE_MAX_WORKER_THREADS>0 */
30646
30647/************** End of threads.c *********************************************/
30648/************** Begin file utf.c *********************************************/
30649/*
30650** 2004 April 13
30651**
30652** The author disclaims copyright to this source code. In place of
30653** a legal notice, here is a blessing:
30654**
30655** May you do good and not evil.
30656** May you find forgiveness for yourself and forgive others.
30657** May you share freely, never taking more than you give.
30658**
30659*************************************************************************
30660** This file contains routines used to translate between UTF-8,
30661** UTF-16, UTF-16BE, and UTF-16LE.
30662**
30663** Notes on UTF-8:
30664**
30665** Byte-0 Byte-1 Byte-2 Byte-3 Value
30666** 0xxxxxxx 00000000 00000000 0xxxxxxx
30667** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
30668** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
30669** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
30670**
30671**
30672** Notes on UTF-16: (with wwww+1==uuuuu)
30673**
30674** Word-0 Word-1 Value
30675** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
30676** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
30677**
30678**
30679** BOM or Byte Order Mark:
30680** 0xff 0xfe little-endian utf-16 follows
30681** 0xfe 0xff big-endian utf-16 follows
30682**
30683*/
30684/* #include "sqliteInt.h" */
30685/* #include <assert.h> */
30686/* #include "vdbeInt.h" */
30687
30688#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
30689/*
30690** The following constant value is used by the SQLITE_BIGENDIAN and
30691** SQLITE_LITTLEENDIAN macros.
30692*/
30693SQLITE_PRIVATE const int sqlite3one = 1;
30694#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
30695
30696/*
30697** This lookup table is used to help decode the first byte of
30698** a multi-byte UTF8 character.
30699*/
30700static const unsigned char sqlite3Utf8Trans1[] = {
30701 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30702 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30703 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
30704 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
30705 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30706 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30707 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30708 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
30709};
30710
30711
30712#define WRITE_UTF8(zOut, c) { \
30713 if( c<0x00080 ){ \
30714 *zOut++ = (u8)(c&0xFF); \
30715 } \
30716 else if( c<0x00800 ){ \
30717 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
30718 *zOut++ = 0x80 + (u8)(c & 0x3F); \
30719 } \
30720 else if( c<0x10000 ){ \
30721 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
30722 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
30723 *zOut++ = 0x80 + (u8)(c & 0x3F); \
30724 }else{ \
30725 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
30726 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
30727 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
30728 *zOut++ = 0x80 + (u8)(c & 0x3F); \
30729 } \
30730}
30731
30732#define WRITE_UTF16LE(zOut, c) { \
30733 if( c<=0xFFFF ){ \
30734 *zOut++ = (u8)(c&0x00FF); \
30735 *zOut++ = (u8)((c>>8)&0x00FF); \
30736 }else{ \
30737 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
30738 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
30739 *zOut++ = (u8)(c&0x00FF); \
30740 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
30741 } \
30742}
30743
30744#define WRITE_UTF16BE(zOut, c) { \
30745 if( c<=0xFFFF ){ \
30746 *zOut++ = (u8)((c>>8)&0x00FF); \
30747 *zOut++ = (u8)(c&0x00FF); \
30748 }else{ \
30749 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
30750 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
30751 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
30752 *zOut++ = (u8)(c&0x00FF); \
30753 } \
30754}
30755
30756/*
30757** Translate a single UTF-8 character. Return the unicode value.
30758**
30759** During translation, assume that the byte that zTerm points
30760** is a 0x00.
30761**
30762** Write a pointer to the next unread byte back into *pzNext.
30763**
30764** Notes On Invalid UTF-8:
30765**
30766** * This routine never allows a 7-bit character (0x00 through 0x7f) to
30767** be encoded as a multi-byte character. Any multi-byte character that
30768** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
30769**
30770** * This routine never allows a UTF16 surrogate value to be encoded.
30771** If a multi-byte character attempts to encode a value between
30772** 0xd800 and 0xe000 then it is rendered as 0xfffd.
30773**
30774** * Bytes in the range of 0x80 through 0xbf which occur as the first
30775** byte of a character are interpreted as single-byte characters
30776** and rendered as themselves even though they are technically
30777** invalid characters.
30778**
30779** * This routine accepts over-length UTF8 encodings
30780** for unicode values 0x80 and greater. It does not change over-length
30781** encodings to 0xfffd as some systems recommend.
30782*/
30783#define READ_UTF8(zIn, zTerm, c) \
30784 c = *(zIn++); \
30785 if( c>=0xc0 ){ \
30786 c = sqlite3Utf8Trans1[c-0xc0]; \
30787 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
30788 c = (c<<6) + (0x3f & *(zIn++)); \
30789 } \
30790 if( c<0x80 \
30791 || (c&0xFFFFF800)==0xD800 \
30792 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
30793 }
30794SQLITE_PRIVATE u32 sqlite3Utf8Read(
30795 const unsigned char **pz /* Pointer to string from which to read char */
30796){
30797 unsigned int c;
30798
30799 /* Same as READ_UTF8() above but without the zTerm parameter.
30800 ** For this routine, we assume the UTF8 string is always zero-terminated.
30801 */
30802 c = *((*pz)++);
30803 if( c>=0xc0 ){
30804 c = sqlite3Utf8Trans1[c-0xc0];
30805 while( (*(*pz) & 0xc0)==0x80 ){
30806 c = (c<<6) + (0x3f & *((*pz)++));
30807 }
30808 if( c<0x80
30809 || (c&0xFFFFF800)==0xD800
30810 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
30811 }
30812 return c;
30813}
30814
30815
30816
30817
30818/*
30819** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
30820** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
30821*/
30822/* #define TRANSLATE_TRACE 1 */
30823
30824#ifndef SQLITE_OMIT_UTF16
30825/*
30826** This routine transforms the internal text encoding used by pMem to
30827** desiredEnc. It is an error if the string is already of the desired
30828** encoding, or if *pMem does not contain a string value.
30829*/
30830SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
30831 sqlite3_int64 len; /* Maximum length of output string in bytes */
30832 unsigned char *zOut; /* Output buffer */
30833 unsigned char *zIn; /* Input iterator */
30834 unsigned char *zTerm; /* End of input */
30835 unsigned char *z; /* Output iterator */
30836 unsigned int c;
30837
30838 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
30839 assert( pMem->flags&MEM_Str );
30840 assert( pMem->enc!=desiredEnc );
30841 assert( pMem->enc!=0 );
30842 assert( pMem->n>=0 );
30843
30844#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
30845 {
30846 StrAccum acc;
30847 char zBuf[1000];
30848 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
30849 sqlite3VdbeMemPrettyPrint(pMem, &acc);
30850 fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
30851 }
30852#endif
30853
30854 /* If the translation is between UTF-16 little and big endian, then
30855 ** all that is required is to swap the byte order. This case is handled
30856 ** differently from the others.
30857 */
30858 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
30859 u8 temp;
30860 int rc;
30861 rc = sqlite3VdbeMemMakeWriteable(pMem);
30862 if( rc!=SQLITE_OK ){
30863 assert( rc==SQLITE_NOMEM );
30864 return SQLITE_NOMEM_BKPT;
30865 }
30866 zIn = (u8*)pMem->z;
30867 zTerm = &zIn[pMem->n&~1];
30868 while( zIn<zTerm ){
30869 temp = *zIn;
30870 *zIn = *(zIn+1);
30871 zIn++;
30872 *zIn++ = temp;
30873 }
30874 pMem->enc = desiredEnc;
30875 goto translate_out;
30876 }
30877
30878 /* Set len to the maximum number of bytes required in the output buffer. */
30879 if( desiredEnc==SQLITE_UTF8 ){
30880 /* When converting from UTF-16, the maximum growth results from
30881 ** translating a 2-byte character to a 4-byte UTF-8 character.
30882 ** A single byte is required for the output string
30883 ** nul-terminator.
30884 */
30885 pMem->n &= ~1;
30886 len = 2 * (sqlite3_int64)pMem->n + 1;
30887 }else{
30888 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
30889 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
30890 ** character. Two bytes are required in the output buffer for the
30891 ** nul-terminator.
30892 */
30893 len = 2 * (sqlite3_int64)pMem->n + 2;
30894 }
30895
30896 /* Set zIn to point at the start of the input buffer and zTerm to point 1
30897 ** byte past the end.
30898 **
30899 ** Variable zOut is set to point at the output buffer, space obtained
30900 ** from sqlite3_malloc().
30901 */
30902 zIn = (u8*)pMem->z;
30903 zTerm = &zIn[pMem->n];
30904 zOut = sqlite3DbMallocRaw(pMem->db, len);
30905 if( !zOut ){
30906 return SQLITE_NOMEM_BKPT;
30907 }
30908 z = zOut;
30909
30910 if( pMem->enc==SQLITE_UTF8 ){
30911 if( desiredEnc==SQLITE_UTF16LE ){
30912 /* UTF-8 -> UTF-16 Little-endian */
30913 while( zIn<zTerm ){
30914 READ_UTF8(zIn, zTerm, c);
30915 WRITE_UTF16LE(z, c);
30916 }
30917 }else{
30918 assert( desiredEnc==SQLITE_UTF16BE );
30919 /* UTF-8 -> UTF-16 Big-endian */
30920 while( zIn<zTerm ){
30921 READ_UTF8(zIn, zTerm, c);
30922 WRITE_UTF16BE(z, c);
30923 }
30924 }
30925 pMem->n = (int)(z - zOut);
30926 *z++ = 0;
30927 }else{
30928 assert( desiredEnc==SQLITE_UTF8 );
30929 if( pMem->enc==SQLITE_UTF16LE ){
30930 /* UTF-16 Little-endian -> UTF-8 */
30931 while( zIn<zTerm ){
30932 c = *(zIn++);
30933 c += (*(zIn++))<<8;
30934 if( c>=0xd800 && c<0xe000 ){
30935#ifdef SQLITE_REPLACE_INVALID_UTF
30936 if( c>=0xdc00 || zIn>=zTerm ){
30937 c = 0xfffd;
30938 }else{
30939 int c2 = *(zIn++);
30940 c2 += (*(zIn++))<<8;
30941 if( c2<0xdc00 || c2>=0xe000 ){
30942 zIn -= 2;
30943 c = 0xfffd;
30944 }else{
30945 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
30946 }
30947 }
30948#else
30949 if( zIn<zTerm ){
30950 int c2 = (*zIn++);
30951 c2 += ((*zIn++)<<8);
30952 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
30953 }
30954#endif
30955 }
30956 WRITE_UTF8(z, c);
30957 }
30958 }else{
30959 /* UTF-16 Big-endian -> UTF-8 */
30960 while( zIn<zTerm ){
30961 c = (*(zIn++))<<8;
30962 c += *(zIn++);
30963 if( c>=0xd800 && c<0xe000 ){
30964#ifdef SQLITE_REPLACE_INVALID_UTF
30965 if( c>=0xdc00 || zIn>=zTerm ){
30966 c = 0xfffd;
30967 }else{
30968 int c2 = (*(zIn++))<<8;
30969 c2 += *(zIn++);
30970 if( c2<0xdc00 || c2>=0xe000 ){
30971 zIn -= 2;
30972 c = 0xfffd;
30973 }else{
30974 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
30975 }
30976 }
30977#else
30978 if( zIn<zTerm ){
30979 int c2 = ((*zIn++)<<8);
30980 c2 += (*zIn++);
30981 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
30982 }
30983#endif
30984 }
30985 WRITE_UTF8(z, c);
30986 }
30987 }
30988 pMem->n = (int)(z - zOut);
30989 }
30990 *z = 0;
30991 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
30992
30993 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
30994 sqlite3VdbeMemRelease(pMem);
30995 pMem->flags = c;
30996 pMem->enc = desiredEnc;
30997 pMem->z = (char*)zOut;
30998 pMem->zMalloc = pMem->z;
30999 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
31000
31001translate_out:
31002#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
31003 {
31004 StrAccum acc;
31005 char zBuf[1000];
31006 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
31007 sqlite3VdbeMemPrettyPrint(pMem, &acc);
31008 fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
31009 }
31010#endif
31011 return SQLITE_OK;
31012}
31013#endif /* SQLITE_OMIT_UTF16 */
31014
31015#ifndef SQLITE_OMIT_UTF16
31016/*
31017** This routine checks for a byte-order mark at the beginning of the
31018** UTF-16 string stored in *pMem. If one is present, it is removed and
31019** the encoding of the Mem adjusted. This routine does not do any
31020** byte-swapping, it just sets Mem.enc appropriately.
31021**
31022** The allocation (static, dynamic etc.) and encoding of the Mem may be
31023** changed by this function.
31024*/
31025SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
31026 int rc = SQLITE_OK;
31027 u8 bom = 0;
31028
31029 assert( pMem->n>=0 );
31030 if( pMem->n>1 ){
31031 u8 b1 = *(u8 *)pMem->z;
31032 u8 b2 = *(((u8 *)pMem->z) + 1);
31033 if( b1==0xFE && b2==0xFF ){
31034 bom = SQLITE_UTF16BE;
31035 }
31036 if( b1==0xFF && b2==0xFE ){
31037 bom = SQLITE_UTF16LE;
31038 }
31039 }
31040
31041 if( bom ){
31042 rc = sqlite3VdbeMemMakeWriteable(pMem);
31043 if( rc==SQLITE_OK ){
31044 pMem->n -= 2;
31045 memmove(pMem->z, &pMem->z[2], pMem->n);
31046 pMem->z[pMem->n] = '\0';
31047 pMem->z[pMem->n+1] = '\0';
31048 pMem->flags |= MEM_Term;
31049 pMem->enc = bom;
31050 }
31051 }
31052 return rc;
31053}
31054#endif /* SQLITE_OMIT_UTF16 */
31055
31056/*
31057** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
31058** return the number of unicode characters in pZ up to (but not including)
31059** the first 0x00 byte. If nByte is not less than zero, return the
31060** number of unicode characters in the first nByte of pZ (or up to
31061** the first 0x00, whichever comes first).
31062*/
31063SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
31064 int r = 0;
31065 const u8 *z = (const u8*)zIn;
31066 const u8 *zTerm;
31067 if( nByte>=0 ){
31068 zTerm = &z[nByte];
31069 }else{
31070 zTerm = (const u8*)(-1);
31071 }
31072 assert( z<=zTerm );
31073 while( *z!=0 && z<zTerm ){
31074 SQLITE_SKIP_UTF8(z);
31075 r++;
31076 }
31077 return r;
31078}
31079
31080/* This test function is not currently used by the automated test-suite.
31081** Hence it is only available in debug builds.
31082*/
31083#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31084/*
31085** Translate UTF-8 to UTF-8.
31086**
31087** This has the effect of making sure that the string is well-formed
31088** UTF-8. Miscoded characters are removed.
31089**
31090** The translation is done in-place and aborted if the output
31091** overruns the input.
31092*/
31093SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
31094 unsigned char *zOut = zIn;
31095 unsigned char *zStart = zIn;
31096 u32 c;
31097
31098 while( zIn[0] && zOut<=zIn ){
31099 c = sqlite3Utf8Read((const u8**)&zIn);
31100 if( c!=0xfffd ){
31101 WRITE_UTF8(zOut, c);
31102 }
31103 }
31104 *zOut = 0;
31105 return (int)(zOut - zStart);
31106}
31107#endif
31108
31109#ifndef SQLITE_OMIT_UTF16
31110/*
31111** Convert a UTF-16 string in the native encoding into a UTF-8 string.
31112** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
31113** be freed by the calling function.
31114**
31115** NULL is returned if there is an allocation error.
31116*/
31117SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
31118 Mem m;
31119 memset(&m, 0, sizeof(m));
31120 m.db = db;
31121 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
31122 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
31123 if( db->mallocFailed ){
31124 sqlite3VdbeMemRelease(&m);
31125 m.z = 0;
31126 }
31127 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
31128 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
31129 assert( m.z || db->mallocFailed );
31130 return m.z;
31131}
31132
31133/*
31134** zIn is a UTF-16 encoded unicode string at least nChar characters long.
31135** Return the number of bytes in the first nChar unicode characters
31136** in pZ. nChar must be non-negative.
31137*/
31138SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
31139 int c;
31140 unsigned char const *z = zIn;
31141 int n = 0;
31142
31143 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
31144 while( n<nChar ){
31145 c = z[0];
31146 z += 2;
31147 if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
31148 n++;
31149 }
31150 return (int)(z-(unsigned char const *)zIn)
31151 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
31152}
31153
31154#if defined(SQLITE_TEST)
31155/*
31156** This routine is called from the TCL test function "translate_selftest".
31157** It checks that the primitives for serializing and deserializing
31158** characters in each encoding are inverses of each other.
31159*/
31160SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
31161 unsigned int i, t;
31162 unsigned char zBuf[20];
31163 unsigned char *z;
31164 int n;
31165 unsigned int c;
31166
31167 for(i=0; i<0x00110000; i++){
31168 z = zBuf;
31169 WRITE_UTF8(z, i);
31170 n = (int)(z-zBuf);
31171 assert( n>0 && n<=4 );
31172 z[0] = 0;
31173 z = zBuf;
31174 c = sqlite3Utf8Read((const u8**)&z);
31175 t = i;
31176 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
31177 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
31178 assert( c==t );
31179 assert( (z-zBuf)==n );
31180 }
31181}
31182#endif /* SQLITE_TEST */
31183#endif /* SQLITE_OMIT_UTF16 */
31184
31185/************** End of utf.c *************************************************/
31186/************** Begin file util.c ********************************************/
31187/*
31188** 2001 September 15
31189**
31190** The author disclaims copyright to this source code. In place of
31191** a legal notice, here is a blessing:
31192**
31193** May you do good and not evil.
31194** May you find forgiveness for yourself and forgive others.
31195** May you share freely, never taking more than you give.
31196**
31197*************************************************************************
31198** Utility functions used throughout sqlite.
31199**
31200** This file contains functions for allocating memory, comparing
31201** strings, and stuff like that.
31202**
31203*/
31204/* #include "sqliteInt.h" */
31205/* #include <stdarg.h> */
31206#ifndef SQLITE_OMIT_FLOATING_POINT
31207#include <math.h>
31208#endif
31209
31210/*
31211** Routine needed to support the testcase() macro.
31212*/
31213#ifdef SQLITE_COVERAGE_TEST
31214SQLITE_PRIVATE void sqlite3Coverage(int x){
31215 static unsigned dummy = 0;
31216 dummy += (unsigned)x;
31217}
31218#endif
31219
31220/*
31221** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
31222** or to bypass normal error detection during testing in order to let
31223** execute proceed futher downstream.
31224**
31225** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
31226** sqlite3FaultSim() function only returns non-zero during testing.
31227**
31228** During testing, if the test harness has set a fault-sim callback using
31229** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
31230** each call to sqlite3FaultSim() is relayed to that application-supplied
31231** callback and the integer return value form the application-supplied
31232** callback is returned by sqlite3FaultSim().
31233**
31234** The integer argument to sqlite3FaultSim() is a code to identify which
31235** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
31236** should have a unique code. To prevent legacy testing applications from
31237** breaking, the codes should not be changed or reused.
31238*/
31239#ifndef SQLITE_UNTESTABLE
31240SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
31241 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
31242 return xCallback ? xCallback(iTest) : SQLITE_OK;
31243}
31244#endif
31245
31246#ifndef SQLITE_OMIT_FLOATING_POINT
31247/*
31248** Return true if the floating point value is Not a Number (NaN).
31249*/
31250SQLITE_PRIVATE int sqlite3IsNaN(double x){
31251 u64 y;
31252 memcpy(&y,&x,sizeof(y));
31253 return IsNaN(y);
31254}
31255#endif /* SQLITE_OMIT_FLOATING_POINT */
31256
31257/*
31258** Compute a string length that is limited to what can be stored in
31259** lower 30 bits of a 32-bit signed integer.
31260**
31261** The value returned will never be negative. Nor will it ever be greater
31262** than the actual length of the string. For very long strings (greater
31263** than 1GiB) the value returned might be less than the true string length.
31264*/
31265SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
31266 if( z==0 ) return 0;
31267 return 0x3fffffff & (int)strlen(z);
31268}
31269
31270/*
31271** Return the declared type of a column. Or return zDflt if the column
31272** has no declared type.
31273**
31274** The column type is an extra string stored after the zero-terminator on
31275** the column name if and only if the COLFLAG_HASTYPE flag is set.
31276*/
31277SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
31278 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
31279 return pCol->zName + strlen(pCol->zName) + 1;
31280}
31281
31282/*
31283** Helper function for sqlite3Error() - called rarely. Broken out into
31284** a separate routine to avoid unnecessary register saves on entry to
31285** sqlite3Error().
31286*/
31287static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
31288 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
31289 sqlite3SystemError(db, err_code);
31290}
31291
31292/*
31293** Set the current error code to err_code and clear any prior error message.
31294** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
31295** that would be appropriate.
31296*/
31297SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
31298 assert( db!=0 );
31299 db->errCode = err_code;
31300 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
31301}
31302
31303/*
31304** Load the sqlite3.iSysErrno field if that is an appropriate thing
31305** to do based on the SQLite error code in rc.
31306*/
31307SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
31308 if( rc==SQLITE_IOERR_NOMEM ) return;
31309 rc &= 0xff;
31310 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
31311 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
31312 }
31313}
31314
31315/*
31316** Set the most recent error code and error string for the sqlite
31317** handle "db". The error code is set to "err_code".
31318**
31319** If it is not NULL, string zFormat specifies the format of the
31320** error string in the style of the printf functions: The following
31321** format characters are allowed:
31322**
31323** %s Insert a string
31324** %z A string that should be freed after use
31325** %d Insert an integer
31326** %T Insert a token
31327** %S Insert the first element of a SrcList
31328**
31329** zFormat and any string tokens that follow it are assumed to be
31330** encoded in UTF-8.
31331**
31332** To clear the most recent error for sqlite handle "db", sqlite3Error
31333** should be called with err_code set to SQLITE_OK and zFormat set
31334** to NULL.
31335*/
31336SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
31337 assert( db!=0 );
31338 db->errCode = err_code;
31339 sqlite3SystemError(db, err_code);
31340 if( zFormat==0 ){
31341 sqlite3Error(db, err_code);
31342 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
31343 char *z;
31344 va_list ap;
31345 va_start(ap, zFormat);
31346 z = sqlite3VMPrintf(db, zFormat, ap);
31347 va_end(ap);
31348 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
31349 }
31350}
31351
31352/*
31353** Add an error message to pParse->zErrMsg and increment pParse->nErr.
31354** The following formatting characters are allowed:
31355**
31356** %s Insert a string
31357** %z A string that should be freed after use
31358** %d Insert an integer
31359** %T Insert a token
31360** %S Insert the first element of a SrcList
31361**
31362** This function should be used to report any error that occurs while
31363** compiling an SQL statement (i.e. within sqlite3_prepare()). The
31364** last thing the sqlite3_prepare() function does is copy the error
31365** stored by this function into the database handle using sqlite3Error().
31366** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
31367** during statement execution (sqlite3_step() etc.).
31368*/
31369SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
31370 char *zMsg;
31371 va_list ap;
31372 sqlite3 *db = pParse->db;
31373 va_start(ap, zFormat);
31374 zMsg = sqlite3VMPrintf(db, zFormat, ap);
31375 va_end(ap);
31376 if( db->suppressErr ){
31377 sqlite3DbFree(db, zMsg);
31378 }else{
31379 pParse->nErr++;
31380 sqlite3DbFree(db, pParse->zErrMsg);
31381 pParse->zErrMsg = zMsg;
31382 pParse->rc = SQLITE_ERROR;
31383 pParse->pWith = 0;
31384 }
31385}
31386
31387/*
31388** If database connection db is currently parsing SQL, then transfer
31389** error code errCode to that parser if the parser has not already
31390** encountered some other kind of error.
31391*/
31392SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
31393 Parse *pParse;
31394 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
31395 pParse->rc = errCode;
31396 pParse->nErr++;
31397 return errCode;
31398}
31399
31400/*
31401** Convert an SQL-style quoted string into a normal string by removing
31402** the quote characters. The conversion is done in-place. If the
31403** input does not begin with a quote character, then this routine
31404** is a no-op.
31405**
31406** The input string must be zero-terminated. A new zero-terminator
31407** is added to the dequoted string.
31408**
31409** The return value is -1 if no dequoting occurs or the length of the
31410** dequoted string, exclusive of the zero terminator, if dequoting does
31411** occur.
31412**
31413** 2002-02-14: This routine is extended to remove MS-Access style
31414** brackets from around identifiers. For example: "[a-b-c]" becomes
31415** "a-b-c".
31416*/
31417SQLITE_PRIVATE void sqlite3Dequote(char *z){
31418 char quote;
31419 int i, j;
31420 if( z==0 ) return;
31421 quote = z[0];
31422 if( !sqlite3Isquote(quote) ) return;
31423 if( quote=='[' ) quote = ']';
31424 for(i=1, j=0;; i++){
31425 assert( z[i] );
31426 if( z[i]==quote ){
31427 if( z[i+1]==quote ){
31428 z[j++] = quote;
31429 i++;
31430 }else{
31431 break;
31432 }
31433 }else{
31434 z[j++] = z[i];
31435 }
31436 }
31437 z[j] = 0;
31438}
31439SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
31440 assert( sqlite3Isquote(p->u.zToken[0]) );
31441 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
31442 sqlite3Dequote(p->u.zToken);
31443}
31444
31445/*
31446** Generate a Token object from a string
31447*/
31448SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
31449 p->z = z;
31450 p->n = sqlite3Strlen30(z);
31451}
31452
31453/* Convenient short-hand */
31454#define UpperToLower sqlite3UpperToLower
31455
31456/*
31457** Some systems have stricmp(). Others have strcasecmp(). Because
31458** there is no consistency, we will define our own.
31459**
31460** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
31461** sqlite3_strnicmp() APIs allow applications and extensions to compare
31462** the contents of two buffers containing UTF-8 strings in a
31463** case-independent fashion, using the same definition of "case
31464** independence" that SQLite uses internally when comparing identifiers.
31465*/
31466SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
31467 if( zLeft==0 ){
31468 return zRight ? -1 : 0;
31469 }else if( zRight==0 ){
31470 return 1;
31471 }
31472 return sqlite3StrICmp(zLeft, zRight);
31473}
31474SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
31475 unsigned char *a, *b;
31476 int c, x;
31477 a = (unsigned char *)zLeft;
31478 b = (unsigned char *)zRight;
31479 for(;;){
31480 c = *a;
31481 x = *b;
31482 if( c==x ){
31483 if( c==0 ) break;
31484 }else{
31485 c = (int)UpperToLower[c] - (int)UpperToLower[x];
31486 if( c ) break;
31487 }
31488 a++;
31489 b++;
31490 }
31491 return c;
31492}
31493SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
31494 register unsigned char *a, *b;
31495 if( zLeft==0 ){
31496 return zRight ? -1 : 0;
31497 }else if( zRight==0 ){
31498 return 1;
31499 }
31500 a = (unsigned char *)zLeft;
31501 b = (unsigned char *)zRight;
31502 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
31503 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
31504}
31505
31506/*
31507** Compute an 8-bit hash on a string that is insensitive to case differences
31508*/
31509SQLITE_PRIVATE u8 sqlite3StrIHash(const char *z){
31510 u8 h = 0;
31511 if( z==0 ) return 0;
31512 while( z[0] ){
31513 h += UpperToLower[(unsigned char)z[0]];
31514 z++;
31515 }
31516 return h;
31517}
31518
31519/*
31520** Compute 10 to the E-th power. Examples: E==1 results in 10.
31521** E==2 results in 100. E==50 results in 1.0e50.
31522**
31523** This routine only works for values of E between 1 and 341.
31524*/
31525static LONGDOUBLE_TYPE sqlite3Pow10(int E){
31526#if defined(_MSC_VER)
31527 static const LONGDOUBLE_TYPE x[] = {
31528 1.0e+001L,
31529 1.0e+002L,
31530 1.0e+004L,
31531 1.0e+008L,
31532 1.0e+016L,
31533 1.0e+032L,
31534 1.0e+064L,
31535 1.0e+128L,
31536 1.0e+256L
31537 };
31538 LONGDOUBLE_TYPE r = 1.0;
31539 int i;
31540 assert( E>=0 && E<=307 );
31541 for(i=0; E!=0; i++, E >>=1){
31542 if( E & 1 ) r *= x[i];
31543 }
31544 return r;
31545#else
31546 LONGDOUBLE_TYPE x = 10.0;
31547 LONGDOUBLE_TYPE r = 1.0;
31548 while(1){
31549 if( E & 1 ) r *= x;
31550 E >>= 1;
31551 if( E==0 ) break;
31552 x *= x;
31553 }
31554 return r;
31555#endif
31556}
31557
31558/*
31559** The string z[] is an text representation of a real number.
31560** Convert this string to a double and write it into *pResult.
31561**
31562** The string z[] is length bytes in length (bytes, not characters) and
31563** uses the encoding enc. The string is not necessarily zero-terminated.
31564**
31565** Return TRUE if the result is a valid real number (or integer) and FALSE
31566** if the string is empty or contains extraneous text. More specifically
31567** return
31568** 1 => The input string is a pure integer
31569** 2 or more => The input has a decimal point or eNNN clause
31570** 0 or less => The input string is not a valid number
31571** -1 => Not a valid number, but has a valid prefix which
31572** includes a decimal point and/or an eNNN clause
31573**
31574** Valid numbers are in one of these formats:
31575**
31576** [+-]digits[E[+-]digits]
31577** [+-]digits.[digits][E[+-]digits]
31578** [+-].digits[E[+-]digits]
31579**
31580** Leading and trailing whitespace is ignored for the purpose of determining
31581** validity.
31582**
31583** If some prefix of the input string is a valid number, this routine
31584** returns FALSE but it still converts the prefix and writes the result
31585** into *pResult.
31586*/
31587#if defined(_MSC_VER)
31588#pragma warning(disable : 4756)
31589#endif
31590SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
31591#ifndef SQLITE_OMIT_FLOATING_POINT
31592 int incr;
31593 const char *zEnd;
31594 /* sign * significand * (10 ^ (esign * exponent)) */
31595 int sign = 1; /* sign of significand */
31596 i64 s = 0; /* significand */
31597 int d = 0; /* adjust exponent for shifting decimal point */
31598 int esign = 1; /* sign of exponent */
31599 int e = 0; /* exponent */
31600 int eValid = 1; /* True exponent is either not used or is well-formed */
31601 double result;
31602 int nDigit = 0; /* Number of digits processed */
31603 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
31604
31605 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
31606 *pResult = 0.0; /* Default return value, in case of an error */
31607 if( length==0 ) return 0;
31608
31609 if( enc==SQLITE_UTF8 ){
31610 incr = 1;
31611 zEnd = z + length;
31612 }else{
31613 int i;
31614 incr = 2;
31615 length &= ~1;
31616 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
31617 testcase( enc==SQLITE_UTF16LE );
31618 testcase( enc==SQLITE_UTF16BE );
31619 for(i=3-enc; i<length && z[i]==0; i+=2){}
31620 if( i<length ) eType = -100;
31621 zEnd = &z[i^1];
31622 z += (enc&1);
31623 }
31624
31625 /* skip leading spaces */
31626 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
31627 if( z>=zEnd ) return 0;
31628
31629 /* get sign of significand */
31630 if( *z=='-' ){
31631 sign = -1;
31632 z+=incr;
31633 }else if( *z=='+' ){
31634 z+=incr;
31635 }
31636
31637 /* copy max significant digits to significand */
31638 while( z<zEnd && sqlite3Isdigit(*z) ){
31639 s = s*10 + (*z - '0');
31640 z+=incr; nDigit++;
31641 if( s>=((LARGEST_INT64-9)/10) ){
31642 /* skip non-significant significand digits
31643 ** (increase exponent by d to shift decimal left) */
31644 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
31645 }
31646 }
31647 if( z>=zEnd ) goto do_atof_calc;
31648
31649 /* if decimal point is present */
31650 if( *z=='.' ){
31651 z+=incr;
31652 eType++;
31653 /* copy digits from after decimal to significand
31654 ** (decrease exponent by d to shift decimal right) */
31655 while( z<zEnd && sqlite3Isdigit(*z) ){
31656 if( s<((LARGEST_INT64-9)/10) ){
31657 s = s*10 + (*z - '0');
31658 d--;
31659 nDigit++;
31660 }
31661 z+=incr;
31662 }
31663 }
31664 if( z>=zEnd ) goto do_atof_calc;
31665
31666 /* if exponent is present */
31667 if( *z=='e' || *z=='E' ){
31668 z+=incr;
31669 eValid = 0;
31670 eType++;
31671
31672 /* This branch is needed to avoid a (harmless) buffer overread. The
31673 ** special comment alerts the mutation tester that the correct answer
31674 ** is obtained even if the branch is omitted */
31675 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
31676
31677 /* get sign of exponent */
31678 if( *z=='-' ){
31679 esign = -1;
31680 z+=incr;
31681 }else if( *z=='+' ){
31682 z+=incr;
31683 }
31684 /* copy digits to exponent */
31685 while( z<zEnd && sqlite3Isdigit(*z) ){
31686 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
31687 z+=incr;
31688 eValid = 1;
31689 }
31690 }
31691
31692 /* skip trailing spaces */
31693 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
31694
31695do_atof_calc:
31696 /* adjust exponent by d, and update sign */
31697 e = (e*esign) + d;
31698 if( e<0 ) {
31699 esign = -1;
31700 e *= -1;
31701 } else {
31702 esign = 1;
31703 }
31704
31705 if( s==0 ) {
31706 /* In the IEEE 754 standard, zero is signed. */
31707 result = sign<0 ? -(double)0 : (double)0;
31708 } else {
31709 /* Attempt to reduce exponent.
31710 **
31711 ** Branches that are not required for the correct answer but which only
31712 ** help to obtain the correct answer faster are marked with special
31713 ** comments, as a hint to the mutation tester.
31714 */
31715 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
31716 if( esign>0 ){
31717 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
31718 s *= 10;
31719 }else{
31720 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
31721 s /= 10;
31722 }
31723 e--;
31724 }
31725
31726 /* adjust the sign of significand */
31727 s = sign<0 ? -s : s;
31728
31729 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
31730 result = (double)s;
31731 }else{
31732 /* attempt to handle extremely small/large numbers better */
31733 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
31734 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
31735 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
31736 if( esign<0 ){
31737 result = s / scale;
31738 result /= 1.0e+308;
31739 }else{
31740 result = s * scale;
31741 result *= 1.0e+308;
31742 }
31743 }else{ assert( e>=342 );
31744 if( esign<0 ){
31745 result = 0.0*s;
31746 }else{
31747#ifdef INFINITY
31748 result = INFINITY*s;
31749#else
31750 result = 1e308*1e308*s; /* Infinity */
31751#endif
31752 }
31753 }
31754 }else{
31755 LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
31756 if( esign<0 ){
31757 result = s / scale;
31758 }else{
31759 result = s * scale;
31760 }
31761 }
31762 }
31763 }
31764
31765 /* store the result */
31766 *pResult = result;
31767
31768 /* return true if number and no extra non-whitespace chracters after */
31769 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
31770 return eType;
31771 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
31772 return -1;
31773 }else{
31774 return 0;
31775 }
31776#else
31777 return !sqlite3Atoi64(z, pResult, length, enc);
31778#endif /* SQLITE_OMIT_FLOATING_POINT */
31779}
31780#if defined(_MSC_VER)
31781#pragma warning(default : 4756)
31782#endif
31783
31784/*
31785** Render an signed 64-bit integer as text. Store the result in zOut[].
31786**
31787** The caller must ensure that zOut[] is at least 21 bytes in size.
31788*/
31789SQLITE_PRIVATE void sqlite3Int64ToText(i64 v, char *zOut){
31790 int i;
31791 u64 x;
31792 char zTemp[22];
31793 if( v<0 ){
31794 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
31795 }else{
31796 x = v;
31797 }
31798 i = sizeof(zTemp)-2;
31799 zTemp[sizeof(zTemp)-1] = 0;
31800 do{
31801 zTemp[i--] = (x%10) + '0';
31802 x = x/10;
31803 }while( x );
31804 if( v<0 ) zTemp[i--] = '-';
31805 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
31806}
31807
31808/*
31809** Compare the 19-character string zNum against the text representation
31810** value 2^63: 9223372036854775808. Return negative, zero, or positive
31811** if zNum is less than, equal to, or greater than the string.
31812** Note that zNum must contain exactly 19 characters.
31813**
31814** Unlike memcmp() this routine is guaranteed to return the difference
31815** in the values of the last digit if the only difference is in the
31816** last digit. So, for example,
31817**
31818** compare2pow63("9223372036854775800", 1)
31819**
31820** will return -8.
31821*/
31822static int compare2pow63(const char *zNum, int incr){
31823 int c = 0;
31824 int i;
31825 /* 012345678901234567 */
31826 const char *pow63 = "922337203685477580";
31827 for(i=0; c==0 && i<18; i++){
31828 c = (zNum[i*incr]-pow63[i])*10;
31829 }
31830 if( c==0 ){
31831 c = zNum[18*incr] - '8';
31832 testcase( c==(-1) );
31833 testcase( c==0 );
31834 testcase( c==(+1) );
31835 }
31836 return c;
31837}
31838
31839/*
31840** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
31841** routine does *not* accept hexadecimal notation.
31842**
31843** Returns:
31844**
31845** -1 Not even a prefix of the input text looks like an integer
31846** 0 Successful transformation. Fits in a 64-bit signed integer.
31847** 1 Excess non-space text after the integer value
31848** 2 Integer too large for a 64-bit signed integer or is malformed
31849** 3 Special case of 9223372036854775808
31850**
31851** length is the number of bytes in the string (bytes, not characters).
31852** The string is not necessarily zero-terminated. The encoding is
31853** given by enc.
31854*/
31855SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
31856 int incr;
31857 u64 u = 0;
31858 int neg = 0; /* assume positive */
31859 int i;
31860 int c = 0;
31861 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
31862 int rc; /* Baseline return code */
31863 const char *zStart;
31864 const char *zEnd = zNum + length;
31865 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
31866 if( enc==SQLITE_UTF8 ){
31867 incr = 1;
31868 }else{
31869 incr = 2;
31870 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
31871 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
31872 nonNum = i<length;
31873 zEnd = &zNum[i^1];
31874 zNum += (enc&1);
31875 }
31876 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
31877 if( zNum<zEnd ){
31878 if( *zNum=='-' ){
31879 neg = 1;
31880 zNum+=incr;
31881 }else if( *zNum=='+' ){
31882 zNum+=incr;
31883 }
31884 }
31885 zStart = zNum;
31886 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
31887 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
31888 u = u*10 + c - '0';
31889 }
31890 testcase( i==18*incr );
31891 testcase( i==19*incr );
31892 testcase( i==20*incr );
31893 if( u>LARGEST_INT64 ){
31894 /* This test and assignment is needed only to suppress UB warnings
31895 ** from clang and -fsanitize=undefined. This test and assignment make
31896 ** the code a little larger and slower, and no harm comes from omitting
31897 ** them, but we must appaise the undefined-behavior pharisees. */
31898 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
31899 }else if( neg ){
31900 *pNum = -(i64)u;
31901 }else{
31902 *pNum = (i64)u;
31903 }
31904 rc = 0;
31905 if( i==0 && zStart==zNum ){ /* No digits */
31906 rc = -1;
31907 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
31908 rc = 1;
31909 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
31910 int jj = i;
31911 do{
31912 if( !sqlite3Isspace(zNum[jj]) ){
31913 rc = 1; /* Extra non-space text after the integer */
31914 break;
31915 }
31916 jj += incr;
31917 }while( &zNum[jj]<zEnd );
31918 }
31919 if( i<19*incr ){
31920 /* Less than 19 digits, so we know that it fits in 64 bits */
31921 assert( u<=LARGEST_INT64 );
31922 return rc;
31923 }else{
31924 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
31925 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
31926 if( c<0 ){
31927 /* zNum is less than 9223372036854775808 so it fits */
31928 assert( u<=LARGEST_INT64 );
31929 return rc;
31930 }else{
31931 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
31932 if( c>0 ){
31933 /* zNum is greater than 9223372036854775808 so it overflows */
31934 return 2;
31935 }else{
31936 /* zNum is exactly 9223372036854775808. Fits if negative. The
31937 ** special case 2 overflow if positive */
31938 assert( u-1==LARGEST_INT64 );
31939 return neg ? rc : 3;
31940 }
31941 }
31942 }
31943}
31944
31945/*
31946** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
31947** into a 64-bit signed integer. This routine accepts hexadecimal literals,
31948** whereas sqlite3Atoi64() does not.
31949**
31950** Returns:
31951**
31952** 0 Successful transformation. Fits in a 64-bit signed integer.
31953** 1 Excess text after the integer value
31954** 2 Integer too large for a 64-bit signed integer or is malformed
31955** 3 Special case of 9223372036854775808
31956*/
31957SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
31958#ifndef SQLITE_OMIT_HEX_INTEGER
31959 if( z[0]=='0'
31960 && (z[1]=='x' || z[1]=='X')
31961 ){
31962 u64 u = 0;
31963 int i, k;
31964 for(i=2; z[i]=='0'; i++){}
31965 for(k=i; sqlite3Isxdigit(z[k]); k++){
31966 u = u*16 + sqlite3HexToInt(z[k]);
31967 }
31968 memcpy(pOut, &u, 8);
31969 return (z[k]==0 && k-i<=16) ? 0 : 2;
31970 }else
31971#endif /* SQLITE_OMIT_HEX_INTEGER */
31972 {
31973 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
31974 }
31975}
31976
31977/*
31978** If zNum represents an integer that will fit in 32-bits, then set
31979** *pValue to that integer and return true. Otherwise return false.
31980**
31981** This routine accepts both decimal and hexadecimal notation for integers.
31982**
31983** Any non-numeric characters that following zNum are ignored.
31984** This is different from sqlite3Atoi64() which requires the
31985** input number to be zero-terminated.
31986*/
31987SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
31988 sqlite_int64 v = 0;
31989 int i, c;
31990 int neg = 0;
31991 if( zNum[0]=='-' ){
31992 neg = 1;
31993 zNum++;
31994 }else if( zNum[0]=='+' ){
31995 zNum++;
31996 }
31997#ifndef SQLITE_OMIT_HEX_INTEGER
31998 else if( zNum[0]=='0'
31999 && (zNum[1]=='x' || zNum[1]=='X')
32000 && sqlite3Isxdigit(zNum[2])
32001 ){
32002 u32 u = 0;
32003 zNum += 2;
32004 while( zNum[0]=='0' ) zNum++;
32005 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
32006 u = u*16 + sqlite3HexToInt(zNum[i]);
32007 }
32008 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
32009 memcpy(pValue, &u, 4);
32010 return 1;
32011 }else{
32012 return 0;
32013 }
32014 }
32015#endif
32016 if( !sqlite3Isdigit(zNum[0]) ) return 0;
32017 while( zNum[0]=='0' ) zNum++;
32018 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
32019 v = v*10 + c;
32020 }
32021
32022 /* The longest decimal representation of a 32 bit integer is 10 digits:
32023 **
32024 ** 1234567890
32025 ** 2^31 -> 2147483648
32026 */
32027 testcase( i==10 );
32028 if( i>10 ){
32029 return 0;
32030 }
32031 testcase( v-neg==2147483647 );
32032 if( v-neg>2147483647 ){
32033 return 0;
32034 }
32035 if( neg ){
32036 v = -v;
32037 }
32038 *pValue = (int)v;
32039 return 1;
32040}
32041
32042/*
32043** Return a 32-bit integer value extracted from a string. If the
32044** string is not an integer, just return 0.
32045*/
32046SQLITE_PRIVATE int sqlite3Atoi(const char *z){
32047 int x = 0;
32048 sqlite3GetInt32(z, &x);
32049 return x;
32050}
32051
32052/*
32053** Try to convert z into an unsigned 32-bit integer. Return true on
32054** success and false if there is an error.
32055**
32056** Only decimal notation is accepted.
32057*/
32058SQLITE_PRIVATE int sqlite3GetUInt32(const char *z, u32 *pI){
32059 u64 v = 0;
32060 int i;
32061 for(i=0; sqlite3Isdigit(z[i]); i++){
32062 v = v*10 + z[i] - '0';
32063 if( v>4294967296LL ){ *pI = 0; return 0; }
32064 }
32065 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
32066 *pI = (u32)v;
32067 return 1;
32068}
32069
32070/*
32071** The variable-length integer encoding is as follows:
32072**
32073** KEY:
32074** A = 0xxxxxxx 7 bits of data and one flag bit
32075** B = 1xxxxxxx 7 bits of data and one flag bit
32076** C = xxxxxxxx 8 bits of data
32077**
32078** 7 bits - A
32079** 14 bits - BA
32080** 21 bits - BBA
32081** 28 bits - BBBA
32082** 35 bits - BBBBA
32083** 42 bits - BBBBBA
32084** 49 bits - BBBBBBA
32085** 56 bits - BBBBBBBA
32086** 64 bits - BBBBBBBBC
32087*/
32088
32089/*
32090** Write a 64-bit variable-length integer to memory starting at p[0].
32091** The length of data write will be between 1 and 9 bytes. The number
32092** of bytes written is returned.
32093**
32094** A variable-length integer consists of the lower 7 bits of each byte
32095** for all bytes that have the 8th bit set and one byte with the 8th
32096** bit clear. Except, if we get to the 9th byte, it stores the full
32097** 8 bits and is the last byte.
32098*/
32099static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
32100 int i, j, n;
32101 u8 buf[10];
32102 if( v & (((u64)0xff000000)<<32) ){
32103 p[8] = (u8)v;
32104 v >>= 8;
32105 for(i=7; i>=0; i--){
32106 p[i] = (u8)((v & 0x7f) | 0x80);
32107 v >>= 7;
32108 }
32109 return 9;
32110 }
32111 n = 0;
32112 do{
32113 buf[n++] = (u8)((v & 0x7f) | 0x80);
32114 v >>= 7;
32115 }while( v!=0 );
32116 buf[0] &= 0x7f;
32117 assert( n<=9 );
32118 for(i=0, j=n-1; j>=0; j--, i++){
32119 p[i] = buf[j];
32120 }
32121 return n;
32122}
32123SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
32124 if( v<=0x7f ){
32125 p[0] = v&0x7f;
32126 return 1;
32127 }
32128 if( v<=0x3fff ){
32129 p[0] = ((v>>7)&0x7f)|0x80;
32130 p[1] = v&0x7f;
32131 return 2;
32132 }
32133 return putVarint64(p,v);
32134}
32135
32136/*
32137** Bitmasks used by sqlite3GetVarint(). These precomputed constants
32138** are defined here rather than simply putting the constant expressions
32139** inline in order to work around bugs in the RVT compiler.
32140**
32141** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
32142**
32143** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
32144*/
32145#define SLOT_2_0 0x001fc07f
32146#define SLOT_4_2_0 0xf01fc07f
32147
32148
32149/*
32150** Read a 64-bit variable-length integer from memory starting at p[0].
32151** Return the number of bytes read. The value is stored in *v.
32152*/
32153SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
32154 u32 a,b,s;
32155
32156 if( ((signed char*)p)[0]>=0 ){
32157 *v = *p;
32158 return 1;
32159 }
32160 if( ((signed char*)p)[1]>=0 ){
32161 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
32162 return 2;
32163 }
32164
32165 /* Verify that constants are precomputed correctly */
32166 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
32167 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
32168
32169 a = ((u32)p[0])<<14;
32170 b = p[1];
32171 p += 2;
32172 a |= *p;
32173 /* a: p0<<14 | p2 (unmasked) */
32174 if (!(a&0x80))
32175 {
32176 a &= SLOT_2_0;
32177 b &= 0x7f;
32178 b = b<<7;
32179 a |= b;
32180 *v = a;
32181 return 3;
32182 }
32183
32184 /* CSE1 from below */
32185 a &= SLOT_2_0;
32186 p++;
32187 b = b<<14;
32188 b |= *p;
32189 /* b: p1<<14 | p3 (unmasked) */
32190 if (!(b&0x80))
32191 {
32192 b &= SLOT_2_0;
32193 /* moved CSE1 up */
32194 /* a &= (0x7f<<14)|(0x7f); */
32195 a = a<<7;
32196 a |= b;
32197 *v = a;
32198 return 4;
32199 }
32200
32201 /* a: p0<<14 | p2 (masked) */
32202 /* b: p1<<14 | p3 (unmasked) */
32203 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32204 /* moved CSE1 up */
32205 /* a &= (0x7f<<14)|(0x7f); */
32206 b &= SLOT_2_0;
32207 s = a;
32208 /* s: p0<<14 | p2 (masked) */
32209
32210 p++;
32211 a = a<<14;
32212 a |= *p;
32213 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
32214 if (!(a&0x80))
32215 {
32216 /* we can skip these cause they were (effectively) done above
32217 ** while calculating s */
32218 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
32219 /* b &= (0x7f<<14)|(0x7f); */
32220 b = b<<7;
32221 a |= b;
32222 s = s>>18;
32223 *v = ((u64)s)<<32 | a;
32224 return 5;
32225 }
32226
32227 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32228 s = s<<7;
32229 s |= b;
32230 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
32231
32232 p++;
32233 b = b<<14;
32234 b |= *p;
32235 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
32236 if (!(b&0x80))
32237 {
32238 /* we can skip this cause it was (effectively) done above in calc'ing s */
32239 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
32240 a &= SLOT_2_0;
32241 a = a<<7;
32242 a |= b;
32243 s = s>>18;
32244 *v = ((u64)s)<<32 | a;
32245 return 6;
32246 }
32247
32248 p++;
32249 a = a<<14;
32250 a |= *p;
32251 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
32252 if (!(a&0x80))
32253 {
32254 a &= SLOT_4_2_0;
32255 b &= SLOT_2_0;
32256 b = b<<7;
32257 a |= b;
32258 s = s>>11;
32259 *v = ((u64)s)<<32 | a;
32260 return 7;
32261 }
32262
32263 /* CSE2 from below */
32264 a &= SLOT_2_0;
32265 p++;
32266 b = b<<14;
32267 b |= *p;
32268 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
32269 if (!(b&0x80))
32270 {
32271 b &= SLOT_4_2_0;
32272 /* moved CSE2 up */
32273 /* a &= (0x7f<<14)|(0x7f); */
32274 a = a<<7;
32275 a |= b;
32276 s = s>>4;
32277 *v = ((u64)s)<<32 | a;
32278 return 8;
32279 }
32280
32281 p++;
32282 a = a<<15;
32283 a |= *p;
32284 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
32285
32286 /* moved CSE2 up */
32287 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
32288 b &= SLOT_2_0;
32289 b = b<<8;
32290 a |= b;
32291
32292 s = s<<4;
32293 b = p[-4];
32294 b &= 0x7f;
32295 b = b>>3;
32296 s |= b;
32297
32298 *v = ((u64)s)<<32 | a;
32299
32300 return 9;
32301}
32302
32303/*
32304** Read a 32-bit variable-length integer from memory starting at p[0].
32305** Return the number of bytes read. The value is stored in *v.
32306**
32307** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
32308** integer, then set *v to 0xffffffff.
32309**
32310** A MACRO version, getVarint32, is provided which inlines the
32311** single-byte case. All code should use the MACRO version as
32312** this function assumes the single-byte case has already been handled.
32313*/
32314SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
32315 u32 a,b;
32316
32317 /* The 1-byte case. Overwhelmingly the most common. Handled inline
32318 ** by the getVarin32() macro */
32319 a = *p;
32320 /* a: p0 (unmasked) */
32321#ifndef getVarint32
32322 if (!(a&0x80))
32323 {
32324 /* Values between 0 and 127 */
32325 *v = a;
32326 return 1;
32327 }
32328#endif
32329
32330 /* The 2-byte case */
32331 p++;
32332 b = *p;
32333 /* b: p1 (unmasked) */
32334 if (!(b&0x80))
32335 {
32336 /* Values between 128 and 16383 */
32337 a &= 0x7f;
32338 a = a<<7;
32339 *v = a | b;
32340 return 2;
32341 }
32342
32343 /* The 3-byte case */
32344 p++;
32345 a = a<<14;
32346 a |= *p;
32347 /* a: p0<<14 | p2 (unmasked) */
32348 if (!(a&0x80))
32349 {
32350 /* Values between 16384 and 2097151 */
32351 a &= (0x7f<<14)|(0x7f);
32352 b &= 0x7f;
32353 b = b<<7;
32354 *v = a | b;
32355 return 3;
32356 }
32357
32358 /* A 32-bit varint is used to store size information in btrees.
32359 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
32360 ** A 3-byte varint is sufficient, for example, to record the size
32361 ** of a 1048569-byte BLOB or string.
32362 **
32363 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
32364 ** rare larger cases can be handled by the slower 64-bit varint
32365 ** routine.
32366 */
32367#if 1
32368 {
32369 u64 v64;
32370 u8 n;
32371
32372 n = sqlite3GetVarint(p-2, &v64);
32373 assert( n>3 && n<=9 );
32374 if( (v64 & SQLITE_MAX_U32)!=v64 ){
32375 *v = 0xffffffff;
32376 }else{
32377 *v = (u32)v64;
32378 }
32379 return n;
32380 }
32381
32382#else
32383 /* For following code (kept for historical record only) shows an
32384 ** unrolling for the 3- and 4-byte varint cases. This code is
32385 ** slightly faster, but it is also larger and much harder to test.
32386 */
32387 p++;
32388 b = b<<14;
32389 b |= *p;
32390 /* b: p1<<14 | p3 (unmasked) */
32391 if (!(b&0x80))
32392 {
32393 /* Values between 2097152 and 268435455 */
32394 b &= (0x7f<<14)|(0x7f);
32395 a &= (0x7f<<14)|(0x7f);
32396 a = a<<7;
32397 *v = a | b;
32398 return 4;
32399 }
32400
32401 p++;
32402 a = a<<14;
32403 a |= *p;
32404 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
32405 if (!(a&0x80))
32406 {
32407 /* Values between 268435456 and 34359738367 */
32408 a &= SLOT_4_2_0;
32409 b &= SLOT_4_2_0;
32410 b = b<<7;
32411 *v = a | b;
32412 return 5;
32413 }
32414
32415 /* We can only reach this point when reading a corrupt database
32416 ** file. In that case we are not in any hurry. Use the (relatively
32417 ** slow) general-purpose sqlite3GetVarint() routine to extract the
32418 ** value. */
32419 {
32420 u64 v64;
32421 u8 n;
32422
32423 p -= 4;
32424 n = sqlite3GetVarint(p, &v64);
32425 assert( n>5 && n<=9 );
32426 *v = (u32)v64;
32427 return n;
32428 }
32429#endif
32430}
32431
32432/*
32433** Return the number of bytes that will be needed to store the given
32434** 64-bit integer.
32435*/
32436SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
32437 int i;
32438 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
32439 return i;
32440}
32441
32442
32443/*
32444** Read or write a four-byte big-endian integer value.
32445*/
32446SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
32447#if SQLITE_BYTEORDER==4321
32448 u32 x;
32449 memcpy(&x,p,4);
32450 return x;
32451#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
32452 u32 x;
32453 memcpy(&x,p,4);
32454 return __builtin_bswap32(x);
32455#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
32456 u32 x;
32457 memcpy(&x,p,4);
32458 return _byteswap_ulong(x);
32459#else
32460 testcase( p[0]&0x80 );
32461 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
32462#endif
32463}
32464SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
32465#if SQLITE_BYTEORDER==4321
32466 memcpy(p,&v,4);
32467#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
32468 u32 x = __builtin_bswap32(v);
32469 memcpy(p,&x,4);
32470#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
32471 u32 x = _byteswap_ulong(v);
32472 memcpy(p,&x,4);
32473#else
32474 p[0] = (u8)(v>>24);
32475 p[1] = (u8)(v>>16);
32476 p[2] = (u8)(v>>8);
32477 p[3] = (u8)v;
32478#endif
32479}
32480
32481
32482
32483/*
32484** Translate a single byte of Hex into an integer.
32485** This routine only works if h really is a valid hexadecimal
32486** character: 0..9a..fA..F
32487*/
32488SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
32489 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
32490#ifdef SQLITE_ASCII
32491 h += 9*(1&(h>>6));
32492#endif
32493#ifdef SQLITE_EBCDIC
32494 h += 9*(1&~(h>>4));
32495#endif
32496 return (u8)(h & 0xf);
32497}
32498
32499#if !defined(SQLITE_OMIT_BLOB_LITERAL)
32500/*
32501** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
32502** value. Return a pointer to its binary value. Space to hold the
32503** binary value has been obtained from malloc and must be freed by
32504** the calling routine.
32505*/
32506SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
32507 char *zBlob;
32508 int i;
32509
32510 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
32511 n--;
32512 if( zBlob ){
32513 for(i=0; i<n; i+=2){
32514 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
32515 }
32516 zBlob[i/2] = 0;
32517 }
32518 return zBlob;
32519}
32520#endif /* !SQLITE_OMIT_BLOB_LITERAL */
32521
32522/*
32523** Log an error that is an API call on a connection pointer that should
32524** not have been used. The "type" of connection pointer is given as the
32525** argument. The zType is a word like "NULL" or "closed" or "invalid".
32526*/
32527static void logBadConnection(const char *zType){
32528 sqlite3_log(SQLITE_MISUSE,
32529 "API call with %s database connection pointer",
32530 zType
32531 );
32532}
32533
32534/*
32535** Check to make sure we have a valid db pointer. This test is not
32536** foolproof but it does provide some measure of protection against
32537** misuse of the interface such as passing in db pointers that are
32538** NULL or which have been previously closed. If this routine returns
32539** 1 it means that the db pointer is valid and 0 if it should not be
32540** dereferenced for any reason. The calling function should invoke
32541** SQLITE_MISUSE immediately.
32542**
32543** sqlite3SafetyCheckOk() requires that the db pointer be valid for
32544** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
32545** open properly and is not fit for general use but which can be
32546** used as an argument to sqlite3_errmsg() or sqlite3_close().
32547*/
32548SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
32549 u32 magic;
32550 if( db==0 ){
32551 logBadConnection("NULL");
32552 return 0;
32553 }
32554 magic = db->magic;
32555 if( magic!=SQLITE_MAGIC_OPEN ){
32556 if( sqlite3SafetyCheckSickOrOk(db) ){
32557 testcase( sqlite3GlobalConfig.xLog!=0 );
32558 logBadConnection("unopened");
32559 }
32560 return 0;
32561 }else{
32562 return 1;
32563 }
32564}
32565SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
32566 u32 magic;
32567 magic = db->magic;
32568 if( magic!=SQLITE_MAGIC_SICK &&
32569 magic!=SQLITE_MAGIC_OPEN &&
32570 magic!=SQLITE_MAGIC_BUSY ){
32571 testcase( sqlite3GlobalConfig.xLog!=0 );
32572 logBadConnection("invalid");
32573 return 0;
32574 }else{
32575 return 1;
32576 }
32577}
32578
32579/*
32580** Attempt to add, substract, or multiply the 64-bit signed value iB against
32581** the other 64-bit signed integer at *pA and store the result in *pA.
32582** Return 0 on success. Or if the operation would have resulted in an
32583** overflow, leave *pA unchanged and return 1.
32584*/
32585SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
32586#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32587 return __builtin_add_overflow(*pA, iB, pA);
32588#else
32589 i64 iA = *pA;
32590 testcase( iA==0 ); testcase( iA==1 );
32591 testcase( iB==-1 ); testcase( iB==0 );
32592 if( iB>=0 ){
32593 testcase( iA>0 && LARGEST_INT64 - iA == iB );
32594 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
32595 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
32596 }else{
32597 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
32598 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
32599 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
32600 }
32601 *pA += iB;
32602 return 0;
32603#endif
32604}
32605SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
32606#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32607 return __builtin_sub_overflow(*pA, iB, pA);
32608#else
32609 testcase( iB==SMALLEST_INT64+1 );
32610 if( iB==SMALLEST_INT64 ){
32611 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
32612 if( (*pA)>=0 ) return 1;
32613 *pA -= iB;
32614 return 0;
32615 }else{
32616 return sqlite3AddInt64(pA, -iB);
32617 }
32618#endif
32619}
32620SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
32621#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
32622 return __builtin_mul_overflow(*pA, iB, pA);
32623#else
32624 i64 iA = *pA;
32625 if( iB>0 ){
32626 if( iA>LARGEST_INT64/iB ) return 1;
32627 if( iA<SMALLEST_INT64/iB ) return 1;
32628 }else if( iB<0 ){
32629 if( iA>0 ){
32630 if( iB<SMALLEST_INT64/iA ) return 1;
32631 }else if( iA<0 ){
32632 if( iB==SMALLEST_INT64 ) return 1;
32633 if( iA==SMALLEST_INT64 ) return 1;
32634 if( -iA>LARGEST_INT64/-iB ) return 1;
32635 }
32636 }
32637 *pA = iA*iB;
32638 return 0;
32639#endif
32640}
32641
32642/*
32643** Compute the absolute value of a 32-bit signed integer, of possible. Or
32644** if the integer has a value of -2147483648, return +2147483647
32645*/
32646SQLITE_PRIVATE int sqlite3AbsInt32(int x){
32647 if( x>=0 ) return x;
32648 if( x==(int)0x80000000 ) return 0x7fffffff;
32649 return -x;
32650}
32651
32652#ifdef SQLITE_ENABLE_8_3_NAMES
32653/*
32654** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
32655** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
32656** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
32657** three characters, then shorten the suffix on z[] to be the last three
32658** characters of the original suffix.
32659**
32660** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
32661** do the suffix shortening regardless of URI parameter.
32662**
32663** Examples:
32664**
32665** test.db-journal => test.nal
32666** test.db-wal => test.wal
32667** test.db-shm => test.shm
32668** test.db-mj7f3319fa => test.9fa
32669*/
32670SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
32671#if SQLITE_ENABLE_8_3_NAMES<2
32672 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
32673#endif
32674 {
32675 int i, sz;
32676 sz = sqlite3Strlen30(z);
32677 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
32678 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
32679 }
32680}
32681#endif
32682
32683/*
32684** Find (an approximate) sum of two LogEst values. This computation is
32685** not a simple "+" operator because LogEst is stored as a logarithmic
32686** value.
32687**
32688*/
32689SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
32690 static const unsigned char x[] = {
32691 10, 10, /* 0,1 */
32692 9, 9, /* 2,3 */
32693 8, 8, /* 4,5 */
32694 7, 7, 7, /* 6,7,8 */
32695 6, 6, 6, /* 9,10,11 */
32696 5, 5, 5, /* 12-14 */
32697 4, 4, 4, 4, /* 15-18 */
32698 3, 3, 3, 3, 3, 3, /* 19-24 */
32699 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
32700 };
32701 if( a>=b ){
32702 if( a>b+49 ) return a;
32703 if( a>b+31 ) return a+1;
32704 return a+x[a-b];
32705 }else{
32706 if( b>a+49 ) return b;
32707 if( b>a+31 ) return b+1;
32708 return b+x[b-a];
32709 }
32710}
32711
32712/*
32713** Convert an integer into a LogEst. In other words, compute an
32714** approximation for 10*log2(x).
32715*/
32716SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
32717 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
32718 LogEst y = 40;
32719 if( x<8 ){
32720 if( x<2 ) return 0;
32721 while( x<8 ){ y -= 10; x <<= 1; }
32722 }else{
32723#if GCC_VERSION>=5004000
32724 int i = 60 - __builtin_clzll(x);
32725 y += i*10;
32726 x >>= i;
32727#else
32728 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
32729 while( x>15 ){ y += 10; x >>= 1; }
32730#endif
32731 }
32732 return a[x&7] + y - 10;
32733}
32734
32735#ifndef SQLITE_OMIT_VIRTUALTABLE
32736/*
32737** Convert a double into a LogEst
32738** In other words, compute an approximation for 10*log2(x).
32739*/
32740SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
32741 u64 a;
32742 LogEst e;
32743 assert( sizeof(x)==8 && sizeof(a)==8 );
32744 if( x<=1 ) return 0;
32745 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
32746 memcpy(&a, &x, 8);
32747 e = (a>>52) - 1022;
32748 return e*10;
32749}
32750#endif /* SQLITE_OMIT_VIRTUALTABLE */
32751
32752#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
32753 defined(SQLITE_ENABLE_STAT4) || \
32754 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
32755/*
32756** Convert a LogEst into an integer.
32757**
32758** Note that this routine is only used when one or more of various
32759** non-standard compile-time options is enabled.
32760*/
32761SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
32762 u64 n;
32763 n = x%10;
32764 x /= 10;
32765 if( n>=5 ) n -= 2;
32766 else if( n>=1 ) n -= 1;
32767#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
32768 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
32769 if( x>60 ) return (u64)LARGEST_INT64;
32770#else
32771 /* If only SQLITE_ENABLE_STAT4 is on, then the largest input
32772 ** possible to this routine is 310, resulting in a maximum x of 31 */
32773 assert( x<=60 );
32774#endif
32775 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
32776}
32777#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
32778
32779/*
32780** Add a new name/number pair to a VList. This might require that the
32781** VList object be reallocated, so return the new VList. If an OOM
32782** error occurs, the original VList returned and the
32783** db->mallocFailed flag is set.
32784**
32785** A VList is really just an array of integers. To destroy a VList,
32786** simply pass it to sqlite3DbFree().
32787**
32788** The first integer is the number of integers allocated for the whole
32789** VList. The second integer is the number of integers actually used.
32790** Each name/number pair is encoded by subsequent groups of 3 or more
32791** integers.
32792**
32793** Each name/number pair starts with two integers which are the numeric
32794** value for the pair and the size of the name/number pair, respectively.
32795** The text name overlays one or more following integers. The text name
32796** is always zero-terminated.
32797**
32798** Conceptually:
32799**
32800** struct VList {
32801** int nAlloc; // Number of allocated slots
32802** int nUsed; // Number of used slots
32803** struct VListEntry {
32804** int iValue; // Value for this entry
32805** int nSlot; // Slots used by this entry
32806** // ... variable name goes here
32807** } a[0];
32808** }
32809**
32810** During code generation, pointers to the variable names within the
32811** VList are taken. When that happens, nAlloc is set to zero as an
32812** indication that the VList may never again be enlarged, since the
32813** accompanying realloc() would invalidate the pointers.
32814*/
32815SQLITE_PRIVATE VList *sqlite3VListAdd(
32816 sqlite3 *db, /* The database connection used for malloc() */
32817 VList *pIn, /* The input VList. Might be NULL */
32818 const char *zName, /* Name of symbol to add */
32819 int nName, /* Bytes of text in zName */
32820 int iVal /* Value to associate with zName */
32821){
32822 int nInt; /* number of sizeof(int) objects needed for zName */
32823 char *z; /* Pointer to where zName will be stored */
32824 int i; /* Index in pIn[] where zName is stored */
32825
32826 nInt = nName/4 + 3;
32827 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
32828 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
32829 /* Enlarge the allocation */
32830 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
32831 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
32832 if( pOut==0 ) return pIn;
32833 if( pIn==0 ) pOut[1] = 2;
32834 pIn = pOut;
32835 pIn[0] = nAlloc;
32836 }
32837 i = pIn[1];
32838 pIn[i] = iVal;
32839 pIn[i+1] = nInt;
32840 z = (char*)&pIn[i+2];
32841 pIn[1] = i+nInt;
32842 assert( pIn[1]<=pIn[0] );
32843 memcpy(z, zName, nName);
32844 z[nName] = 0;
32845 return pIn;
32846}
32847
32848/*
32849** Return a pointer to the name of a variable in the given VList that
32850** has the value iVal. Or return a NULL if there is no such variable in
32851** the list
32852*/
32853SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
32854 int i, mx;
32855 if( pIn==0 ) return 0;
32856 mx = pIn[1];
32857 i = 2;
32858 do{
32859 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
32860 i += pIn[i+1];
32861 }while( i<mx );
32862 return 0;
32863}
32864
32865/*
32866** Return the number of the variable named zName, if it is in VList.
32867** or return 0 if there is no such variable.
32868*/
32869SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
32870 int i, mx;
32871 if( pIn==0 ) return 0;
32872 mx = pIn[1];
32873 i = 2;
32874 do{
32875 const char *z = (const char*)&pIn[i+2];
32876 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
32877 i += pIn[i+1];
32878 }while( i<mx );
32879 return 0;
32880}
32881
32882/************** End of util.c ************************************************/
32883/************** Begin file hash.c ********************************************/
32884/*
32885** 2001 September 22
32886**
32887** The author disclaims copyright to this source code. In place of
32888** a legal notice, here is a blessing:
32889**
32890** May you do good and not evil.
32891** May you find forgiveness for yourself and forgive others.
32892** May you share freely, never taking more than you give.
32893**
32894*************************************************************************
32895** This is the implementation of generic hash-tables
32896** used in SQLite.
32897*/
32898/* #include "sqliteInt.h" */
32899/* #include <assert.h> */
32900
32901/* Turn bulk memory into a hash table object by initializing the
32902** fields of the Hash structure.
32903**
32904** "pNew" is a pointer to the hash table that is to be initialized.
32905*/
32906SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
32907 assert( pNew!=0 );
32908 pNew->first = 0;
32909 pNew->count = 0;
32910 pNew->htsize = 0;
32911 pNew->ht = 0;
32912}
32913
32914/* Remove all entries from a hash table. Reclaim all memory.
32915** Call this routine to delete a hash table or to reset a hash table
32916** to the empty state.
32917*/
32918SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
32919 HashElem *elem; /* For looping over all elements of the table */
32920
32921 assert( pH!=0 );
32922 elem = pH->first;
32923 pH->first = 0;
32924 sqlite3_free(pH->ht);
32925 pH->ht = 0;
32926 pH->htsize = 0;
32927 while( elem ){
32928 HashElem *next_elem = elem->next;
32929 sqlite3_free(elem);
32930 elem = next_elem;
32931 }
32932 pH->count = 0;
32933}
32934
32935/*
32936** The hashing function.
32937*/
32938static unsigned int strHash(const char *z){
32939 unsigned int h = 0;
32940 unsigned char c;
32941 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
32942 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
32943 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
32944 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
32945 h += sqlite3UpperToLower[c];
32946 h *= 0x9e3779b1;
32947 }
32948 return h;
32949}
32950
32951
32952/* Link pNew element into the hash table pH. If pEntry!=0 then also
32953** insert pNew into the pEntry hash bucket.
32954*/
32955static void insertElement(
32956 Hash *pH, /* The complete hash table */
32957 struct _ht *pEntry, /* The entry into which pNew is inserted */
32958 HashElem *pNew /* The element to be inserted */
32959){
32960 HashElem *pHead; /* First element already in pEntry */
32961 if( pEntry ){
32962 pHead = pEntry->count ? pEntry->chain : 0;
32963 pEntry->count++;
32964 pEntry->chain = pNew;
32965 }else{
32966 pHead = 0;
32967 }
32968 if( pHead ){
32969 pNew->next = pHead;
32970 pNew->prev = pHead->prev;
32971 if( pHead->prev ){ pHead->prev->next = pNew; }
32972 else { pH->first = pNew; }
32973 pHead->prev = pNew;
32974 }else{
32975 pNew->next = pH->first;
32976 if( pH->first ){ pH->first->prev = pNew; }
32977 pNew->prev = 0;
32978 pH->first = pNew;
32979 }
32980}
32981
32982
32983/* Resize the hash table so that it cantains "new_size" buckets.
32984**
32985** The hash table might fail to resize if sqlite3_malloc() fails or
32986** if the new size is the same as the prior size.
32987** Return TRUE if the resize occurs and false if not.
32988*/
32989static int rehash(Hash *pH, unsigned int new_size){
32990 struct _ht *new_ht; /* The new hash table */
32991 HashElem *elem, *next_elem; /* For looping over existing elements */
32992
32993#if SQLITE_MALLOC_SOFT_LIMIT>0
32994 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
32995 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
32996 }
32997 if( new_size==pH->htsize ) return 0;
32998#endif
32999
33000 /* The inability to allocates space for a larger hash table is
33001 ** a performance hit but it is not a fatal error. So mark the
33002 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
33003 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
33004 ** only zeroes the requested number of bytes whereas this module will
33005 ** use the actual amount of space allocated for the hash table (which
33006 ** may be larger than the requested amount).
33007 */
33008 sqlite3BeginBenignMalloc();
33009 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
33010 sqlite3EndBenignMalloc();
33011
33012 if( new_ht==0 ) return 0;
33013 sqlite3_free(pH->ht);
33014 pH->ht = new_ht;
33015 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
33016 memset(new_ht, 0, new_size*sizeof(struct _ht));
33017 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
33018 unsigned int h = strHash(elem->pKey) % new_size;
33019 next_elem = elem->next;
33020 insertElement(pH, &new_ht[h], elem);
33021 }
33022 return 1;
33023}
33024
33025/* This function (for internal use only) locates an element in an
33026** hash table that matches the given key. If no element is found,
33027** a pointer to a static null element with HashElem.data==0 is returned.
33028** If pH is not NULL, then the hash for this key is written to *pH.
33029*/
33030static HashElem *findElementWithHash(
33031 const Hash *pH, /* The pH to be searched */
33032 const char *pKey, /* The key we are searching for */
33033 unsigned int *pHash /* Write the hash value here */
33034){
33035 HashElem *elem; /* Used to loop thru the element list */
33036 unsigned int count; /* Number of elements left to test */
33037 unsigned int h; /* The computed hash */
33038 static HashElem nullElement = { 0, 0, 0, 0 };
33039
33040 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
33041 struct _ht *pEntry;
33042 h = strHash(pKey) % pH->htsize;
33043 pEntry = &pH->ht[h];
33044 elem = pEntry->chain;
33045 count = pEntry->count;
33046 }else{
33047 h = 0;
33048 elem = pH->first;
33049 count = pH->count;
33050 }
33051 if( pHash ) *pHash = h;
33052 while( count-- ){
33053 assert( elem!=0 );
33054 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
33055 return elem;
33056 }
33057 elem = elem->next;
33058 }
33059 return &nullElement;
33060}
33061
33062/* Remove a single entry from the hash table given a pointer to that
33063** element and a hash on the element's key.
33064*/
33065static void removeElementGivenHash(
33066 Hash *pH, /* The pH containing "elem" */
33067 HashElem* elem, /* The element to be removed from the pH */
33068 unsigned int h /* Hash value for the element */
33069){
33070 struct _ht *pEntry;
33071 if( elem->prev ){
33072 elem->prev->next = elem->next;
33073 }else{
33074 pH->first = elem->next;
33075 }
33076 if( elem->next ){
33077 elem->next->prev = elem->prev;
33078 }
33079 if( pH->ht ){
33080 pEntry = &pH->ht[h];
33081 if( pEntry->chain==elem ){
33082 pEntry->chain = elem->next;
33083 }
33084 assert( pEntry->count>0 );
33085 pEntry->count--;
33086 }
33087 sqlite3_free( elem );
33088 pH->count--;
33089 if( pH->count==0 ){
33090 assert( pH->first==0 );
33091 assert( pH->count==0 );
33092 sqlite3HashClear(pH);
33093 }
33094}
33095
33096/* Attempt to locate an element of the hash table pH with a key
33097** that matches pKey. Return the data for this element if it is
33098** found, or NULL if there is no match.
33099*/
33100SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
33101 assert( pH!=0 );
33102 assert( pKey!=0 );
33103 return findElementWithHash(pH, pKey, 0)->data;
33104}
33105
33106/* Insert an element into the hash table pH. The key is pKey
33107** and the data is "data".
33108**
33109** If no element exists with a matching key, then a new
33110** element is created and NULL is returned.
33111**
33112** If another element already exists with the same key, then the
33113** new data replaces the old data and the old data is returned.
33114** The key is not copied in this instance. If a malloc fails, then
33115** the new data is returned and the hash table is unchanged.
33116**
33117** If the "data" parameter to this function is NULL, then the
33118** element corresponding to "key" is removed from the hash table.
33119*/
33120SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
33121 unsigned int h; /* the hash of the key modulo hash table size */
33122 HashElem *elem; /* Used to loop thru the element list */
33123 HashElem *new_elem; /* New element added to the pH */
33124
33125 assert( pH!=0 );
33126 assert( pKey!=0 );
33127 elem = findElementWithHash(pH,pKey,&h);
33128 if( elem->data ){
33129 void *old_data = elem->data;
33130 if( data==0 ){
33131 removeElementGivenHash(pH,elem,h);
33132 }else{
33133 elem->data = data;
33134 elem->pKey = pKey;
33135 }
33136 return old_data;
33137 }
33138 if( data==0 ) return 0;
33139 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
33140 if( new_elem==0 ) return data;
33141 new_elem->pKey = pKey;
33142 new_elem->data = data;
33143 pH->count++;
33144 if( pH->count>=10 && pH->count > 2*pH->htsize ){
33145 if( rehash(pH, pH->count*2) ){
33146 assert( pH->htsize>0 );
33147 h = strHash(pKey) % pH->htsize;
33148 }
33149 }
33150 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
33151 return 0;
33152}
33153
33154/************** End of hash.c ************************************************/
33155/************** Begin file opcodes.c *****************************************/
33156/* Automatically generated. Do not edit */
33157/* See the tool/mkopcodec.tcl script for details. */
33158#if !defined(SQLITE_OMIT_EXPLAIN) \
33159 || defined(VDBE_PROFILE) \
33160 || defined(SQLITE_DEBUG)
33161#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
33162# define OpHelp(X) "\0" X
33163#else
33164# define OpHelp(X)
33165#endif
33166SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
33167 static const char *const azName[] = {
33168 /* 0 */ "Savepoint" OpHelp(""),
33169 /* 1 */ "AutoCommit" OpHelp(""),
33170 /* 2 */ "Transaction" OpHelp(""),
33171 /* 3 */ "SorterNext" OpHelp(""),
33172 /* 4 */ "Prev" OpHelp(""),
33173 /* 5 */ "Next" OpHelp(""),
33174 /* 6 */ "Checkpoint" OpHelp(""),
33175 /* 7 */ "JournalMode" OpHelp(""),
33176 /* 8 */ "Vacuum" OpHelp(""),
33177 /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
33178 /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"),
33179 /* 11 */ "Goto" OpHelp(""),
33180 /* 12 */ "Gosub" OpHelp(""),
33181 /* 13 */ "InitCoroutine" OpHelp(""),
33182 /* 14 */ "Yield" OpHelp(""),
33183 /* 15 */ "MustBeInt" OpHelp(""),
33184 /* 16 */ "Jump" OpHelp(""),
33185 /* 17 */ "Once" OpHelp(""),
33186 /* 18 */ "If" OpHelp(""),
33187 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
33188 /* 20 */ "IfNot" OpHelp(""),
33189 /* 21 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
33190 /* 22 */ "SeekLT" OpHelp("key=r[P3@P4]"),
33191 /* 23 */ "SeekLE" OpHelp("key=r[P3@P4]"),
33192 /* 24 */ "SeekGE" OpHelp("key=r[P3@P4]"),
33193 /* 25 */ "SeekGT" OpHelp("key=r[P3@P4]"),
33194 /* 26 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
33195 /* 27 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
33196 /* 28 */ "NoConflict" OpHelp("key=r[P3@P4]"),
33197 /* 29 */ "NotFound" OpHelp("key=r[P3@P4]"),
33198 /* 30 */ "Found" OpHelp("key=r[P3@P4]"),
33199 /* 31 */ "SeekRowid" OpHelp("intkey=r[P3]"),
33200 /* 32 */ "NotExists" OpHelp("intkey=r[P3]"),
33201 /* 33 */ "Last" OpHelp(""),
33202 /* 34 */ "IfSmaller" OpHelp(""),
33203 /* 35 */ "SorterSort" OpHelp(""),
33204 /* 36 */ "Sort" OpHelp(""),
33205 /* 37 */ "Rewind" OpHelp(""),
33206 /* 38 */ "IdxLE" OpHelp("key=r[P3@P4]"),
33207 /* 39 */ "IdxGT" OpHelp("key=r[P3@P4]"),
33208 /* 40 */ "IdxLT" OpHelp("key=r[P3@P4]"),
33209 /* 41 */ "IdxGE" OpHelp("key=r[P3@P4]"),
33210 /* 42 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
33211 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
33212 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
33213 /* 45 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
33214 /* 46 */ "Program" OpHelp(""),
33215 /* 47 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
33216 /* 48 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
33217 /* 49 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
33218 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
33219 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
33220 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
33221 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
33222 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
33223 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
33224 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
33225 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
33226 /* 58 */ "ElseNotEq" OpHelp(""),
33227 /* 59 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
33228 /* 60 */ "IncrVacuum" OpHelp(""),
33229 /* 61 */ "VNext" OpHelp(""),
33230 /* 62 */ "Init" OpHelp("Start at P2"),
33231 /* 63 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
33232 /* 64 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
33233 /* 65 */ "Return" OpHelp(""),
33234 /* 66 */ "EndCoroutine" OpHelp(""),
33235 /* 67 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
33236 /* 68 */ "Halt" OpHelp(""),
33237 /* 69 */ "Integer" OpHelp("r[P2]=P1"),
33238 /* 70 */ "Int64" OpHelp("r[P2]=P4"),
33239 /* 71 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
33240 /* 72 */ "Null" OpHelp("r[P2..P3]=NULL"),
33241 /* 73 */ "SoftNull" OpHelp("r[P1]=NULL"),
33242 /* 74 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
33243 /* 75 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
33244 /* 76 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
33245 /* 77 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
33246 /* 78 */ "SCopy" OpHelp("r[P2]=r[P1]"),
33247 /* 79 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
33248 /* 80 */ "ResultRow" OpHelp("output=r[P1@P2]"),
33249 /* 81 */ "CollSeq" OpHelp(""),
33250 /* 82 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
33251 /* 83 */ "RealAffinity" OpHelp(""),
33252 /* 84 */ "Cast" OpHelp("affinity(r[P1])"),
33253 /* 85 */ "Permutation" OpHelp(""),
33254 /* 86 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
33255 /* 87 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
33256 /* 88 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
33257 /* 89 */ "Column" OpHelp("r[P3]=PX"),
33258 /* 90 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
33259 /* 91 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
33260 /* 92 */ "Count" OpHelp("r[P2]=count()"),
33261 /* 93 */ "ReadCookie" OpHelp(""),
33262 /* 94 */ "SetCookie" OpHelp(""),
33263 /* 95 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
33264 /* 96 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
33265 /* 97 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
33266 /* 98 */ "OpenDup" OpHelp(""),
33267 /* 99 */ "OpenAutoindex" OpHelp("nColumn=P2"),
33268 /* 100 */ "OpenEphemeral" OpHelp("nColumn=P2"),
33269 /* 101 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
33270 /* 102 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
33271 /* 103 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
33272 /* 104 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
33273 /* 105 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
33274 /* 106 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
33275 /* 107 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
33276 /* 108 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
33277 /* 109 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
33278 /* 110 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
33279 /* 111 */ "SorterOpen" OpHelp(""),
33280 /* 112 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
33281 /* 113 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
33282 /* 114 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
33283 /* 115 */ "String8" OpHelp("r[P2]='P4'"),
33284 /* 116 */ "Close" OpHelp(""),
33285 /* 117 */ "ColumnsUsed" OpHelp(""),
33286 /* 118 */ "SeekHit" OpHelp("seekHit=P2"),
33287 /* 119 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
33288 /* 120 */ "NewRowid" OpHelp("r[P2]=rowid"),
33289 /* 121 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
33290 /* 122 */ "Delete" OpHelp(""),
33291 /* 123 */ "ResetCount" OpHelp(""),
33292 /* 124 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
33293 /* 125 */ "SorterData" OpHelp("r[P2]=data"),
33294 /* 126 */ "RowData" OpHelp("r[P2]=data"),
33295 /* 127 */ "Rowid" OpHelp("r[P2]=rowid"),
33296 /* 128 */ "NullRow" OpHelp(""),
33297 /* 129 */ "SeekEnd" OpHelp(""),
33298 /* 130 */ "IdxInsert" OpHelp("key=r[P2]"),
33299 /* 131 */ "SorterInsert" OpHelp("key=r[P2]"),
33300 /* 132 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
33301 /* 133 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
33302 /* 134 */ "IdxRowid" OpHelp("r[P2]=rowid"),
33303 /* 135 */ "FinishSeek" OpHelp(""),
33304 /* 136 */ "Destroy" OpHelp(""),
33305 /* 137 */ "Clear" OpHelp(""),
33306 /* 138 */ "ResetSorter" OpHelp(""),
33307 /* 139 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
33308 /* 140 */ "SqlExec" OpHelp(""),
33309 /* 141 */ "ParseSchema" OpHelp(""),
33310 /* 142 */ "LoadAnalysis" OpHelp(""),
33311 /* 143 */ "DropTable" OpHelp(""),
33312 /* 144 */ "DropIndex" OpHelp(""),
33313 /* 145 */ "DropTrigger" OpHelp(""),
33314 /* 146 */ "IntegrityCk" OpHelp(""),
33315 /* 147 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
33316 /* 148 */ "Param" OpHelp(""),
33317 /* 149 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
33318 /* 150 */ "Real" OpHelp("r[P2]=P4"),
33319 /* 151 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
33320 /* 152 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
33321 /* 153 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
33322 /* 154 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
33323 /* 155 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
33324 /* 156 */ "AggValue" OpHelp("r[P3]=value N=P2"),
33325 /* 157 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
33326 /* 158 */ "Expire" OpHelp(""),
33327 /* 159 */ "CursorLock" OpHelp(""),
33328 /* 160 */ "CursorUnlock" OpHelp(""),
33329 /* 161 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
33330 /* 162 */ "VBegin" OpHelp(""),
33331 /* 163 */ "VCreate" OpHelp(""),
33332 /* 164 */ "VDestroy" OpHelp(""),
33333 /* 165 */ "VOpen" OpHelp(""),
33334 /* 166 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
33335 /* 167 */ "VRename" OpHelp(""),
33336 /* 168 */ "Pagecount" OpHelp(""),
33337 /* 169 */ "MaxPgcnt" OpHelp(""),
33338 /* 170 */ "Trace" OpHelp(""),
33339 /* 171 */ "CursorHint" OpHelp(""),
33340 /* 172 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
33341 /* 173 */ "Noop" OpHelp(""),
33342 /* 174 */ "Explain" OpHelp(""),
33343 /* 175 */ "Abortable" OpHelp(""),
33344 };
33345 return azName[i];
33346}
33347#endif
33348
33349/************** End of opcodes.c *********************************************/
33350/************** Begin file os_unix.c *****************************************/
33351/*
33352** 2004 May 22
33353**
33354** The author disclaims copyright to this source code. In place of
33355** a legal notice, here is a blessing:
33356**
33357** May you do good and not evil.
33358** May you find forgiveness for yourself and forgive others.
33359** May you share freely, never taking more than you give.
33360**
33361******************************************************************************
33362**
33363** This file contains the VFS implementation for unix-like operating systems
33364** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
33365**
33366** There are actually several different VFS implementations in this file.
33367** The differences are in the way that file locking is done. The default
33368** implementation uses Posix Advisory Locks. Alternative implementations
33369** use flock(), dot-files, various proprietary locking schemas, or simply
33370** skip locking all together.
33371**
33372** This source file is organized into divisions where the logic for various
33373** subfunctions is contained within the appropriate division. PLEASE
33374** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
33375** in the correct division and should be clearly labeled.
33376**
33377** The layout of divisions is as follows:
33378**
33379** * General-purpose declarations and utility functions.
33380** * Unique file ID logic used by VxWorks.
33381** * Various locking primitive implementations (all except proxy locking):
33382** + for Posix Advisory Locks
33383** + for no-op locks
33384** + for dot-file locks
33385** + for flock() locking
33386** + for named semaphore locks (VxWorks only)
33387** + for AFP filesystem locks (MacOSX only)
33388** * sqlite3_file methods not associated with locking.
33389** * Definitions of sqlite3_io_methods objects for all locking
33390** methods plus "finder" functions for each locking method.
33391** * sqlite3_vfs method implementations.
33392** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
33393** * Definitions of sqlite3_vfs objects for all locking methods
33394** plus implementations of sqlite3_os_init() and sqlite3_os_end().
33395*/
33396/* #include "sqliteInt.h" */
33397#if SQLITE_OS_UNIX /* This file is used on unix only */
33398
33399/*
33400** There are various methods for file locking used for concurrency
33401** control:
33402**
33403** 1. POSIX locking (the default),
33404** 2. No locking,
33405** 3. Dot-file locking,
33406** 4. flock() locking,
33407** 5. AFP locking (OSX only),
33408** 6. Named POSIX semaphores (VXWorks only),
33409** 7. proxy locking. (OSX only)
33410**
33411** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
33412** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
33413** selection of the appropriate locking style based on the filesystem
33414** where the database is located.
33415*/
33416#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
33417# if defined(__APPLE__)
33418# define SQLITE_ENABLE_LOCKING_STYLE 1
33419# else
33420# define SQLITE_ENABLE_LOCKING_STYLE 0
33421# endif
33422#endif
33423
33424/* Use pread() and pwrite() if they are available */
33425#if defined(__APPLE__)
33426# define HAVE_PREAD 1
33427# define HAVE_PWRITE 1
33428#endif
33429#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
33430# undef USE_PREAD
33431# define USE_PREAD64 1
33432#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
33433# undef USE_PREAD64
33434# define USE_PREAD 1
33435#endif
33436
33437/*
33438** standard include files.
33439*/
33440#include <sys/types.h>
33441#include <sys/stat.h>
33442#include <fcntl.h>
33443#include <sys/ioctl.h>
33444#include <unistd.h>
33445/* #include <time.h> */
33446#include <sys/time.h>
33447#include <errno.h>
33448#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
33449# include <sys/mman.h>
33450#endif
33451
33452#if SQLITE_ENABLE_LOCKING_STYLE
33453/* # include <sys/ioctl.h> */
33454# include <sys/file.h>
33455# include <sys/param.h>
33456#endif /* SQLITE_ENABLE_LOCKING_STYLE */
33457
33458/*
33459** Try to determine if gethostuuid() is available based on standard
33460** macros. This might sometimes compute the wrong value for some
33461** obscure platforms. For those cases, simply compile with one of
33462** the following:
33463**
33464** -DHAVE_GETHOSTUUID=0
33465** -DHAVE_GETHOSTUUID=1
33466**
33467** None if this matters except when building on Apple products with
33468** -DSQLITE_ENABLE_LOCKING_STYLE.
33469*/
33470#ifndef HAVE_GETHOSTUUID
33471# define HAVE_GETHOSTUUID 0
33472# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
33473 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
33474# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
33475 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
33476# undef HAVE_GETHOSTUUID
33477# define HAVE_GETHOSTUUID 1
33478# else
33479# warning "gethostuuid() is disabled."
33480# endif
33481# endif
33482#endif
33483
33484
33485#if OS_VXWORKS
33486/* # include <sys/ioctl.h> */
33487# include <semaphore.h>
33488# include <limits.h>
33489#endif /* OS_VXWORKS */
33490
33491#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
33492# include <sys/mount.h>
33493#endif
33494
33495#ifdef HAVE_UTIME
33496# include <utime.h>
33497#endif
33498
33499/*
33500** Allowed values of unixFile.fsFlags
33501*/
33502#define SQLITE_FSFLAGS_IS_MSDOS 0x1
33503
33504/*
33505** If we are to be thread-safe, include the pthreads header.
33506*/
33507#if SQLITE_THREADSAFE
33508/* # include <pthread.h> */
33509#endif
33510
33511/*
33512** Default permissions when creating a new file
33513*/
33514#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
33515# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
33516#endif
33517
33518/*
33519** Default permissions when creating auto proxy dir
33520*/
33521#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
33522# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
33523#endif
33524
33525/*
33526** Maximum supported path-length.
33527*/
33528#define MAX_PATHNAME 512
33529
33530/*
33531** Maximum supported symbolic links
33532*/
33533#define SQLITE_MAX_SYMLINKS 100
33534
33535/* Always cast the getpid() return type for compatibility with
33536** kernel modules in VxWorks. */
33537#define osGetpid(X) (pid_t)getpid()
33538
33539/*
33540** Only set the lastErrno if the error code is a real error and not
33541** a normal expected return code of SQLITE_BUSY or SQLITE_OK
33542*/
33543#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
33544
33545/* Forward references */
33546typedef struct unixShm unixShm; /* Connection shared memory */
33547typedef struct unixShmNode unixShmNode; /* Shared memory instance */
33548typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
33549typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
33550
33551/*
33552** Sometimes, after a file handle is closed by SQLite, the file descriptor
33553** cannot be closed immediately. In these cases, instances of the following
33554** structure are used to store the file descriptor while waiting for an
33555** opportunity to either close or reuse it.
33556*/
33557struct UnixUnusedFd {
33558 int fd; /* File descriptor to close */
33559 int flags; /* Flags this file descriptor was opened with */
33560 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
33561};
33562
33563/*
33564** The unixFile structure is subclass of sqlite3_file specific to the unix
33565** VFS implementations.
33566*/
33567typedef struct unixFile unixFile;
33568struct unixFile {
33569 sqlite3_io_methods const *pMethod; /* Always the first entry */
33570 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
33571 unixInodeInfo *pInode; /* Info about locks on this inode */
33572 int h; /* The file descriptor */
33573 unsigned char eFileLock; /* The type of lock held on this fd */
33574 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
33575 int lastErrno; /* The unix errno from last I/O error */
33576 void *lockingContext; /* Locking style specific state */
33577 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
33578 const char *zPath; /* Name of the file */
33579 unixShm *pShm; /* Shared memory segment information */
33580 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
33581#if SQLITE_MAX_MMAP_SIZE>0
33582 int nFetchOut; /* Number of outstanding xFetch refs */
33583 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
33584 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
33585 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
33586 void *pMapRegion; /* Memory mapped region */
33587#endif
33588 int sectorSize; /* Device sector size */
33589 int deviceCharacteristics; /* Precomputed device characteristics */
33590#if SQLITE_ENABLE_LOCKING_STYLE
33591 int openFlags; /* The flags specified at open() */
33592#endif
33593#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
33594 unsigned fsFlags; /* cached details from statfs() */
33595#endif
33596#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
33597 unsigned iBusyTimeout; /* Wait this many millisec on locks */
33598#endif
33599#if OS_VXWORKS
33600 struct vxworksFileId *pId; /* Unique file ID */
33601#endif
33602#ifdef SQLITE_DEBUG
33603 /* The next group of variables are used to track whether or not the
33604 ** transaction counter in bytes 24-27 of database files are updated
33605 ** whenever any part of the database changes. An assertion fault will
33606 ** occur if a file is updated without also updating the transaction
33607 ** counter. This test is made to avoid new problems similar to the
33608 ** one described by ticket #3584.
33609 */
33610 unsigned char transCntrChng; /* True if the transaction counter changed */
33611 unsigned char dbUpdate; /* True if any part of database file changed */
33612 unsigned char inNormalWrite; /* True if in a normal write operation */
33613
33614#endif
33615
33616#ifdef SQLITE_TEST
33617 /* In test mode, increase the size of this structure a bit so that
33618 ** it is larger than the struct CrashFile defined in test6.c.
33619 */
33620 char aPadding[32];
33621#endif
33622};
33623
33624/* This variable holds the process id (pid) from when the xRandomness()
33625** method was called. If xOpen() is called from a different process id,
33626** indicating that a fork() has occurred, the PRNG will be reset.
33627*/
33628static pid_t randomnessPid = 0;
33629
33630/*
33631** Allowed values for the unixFile.ctrlFlags bitmask:
33632*/
33633#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
33634#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
33635#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
33636#ifndef SQLITE_DISABLE_DIRSYNC
33637# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
33638#else
33639# define UNIXFILE_DIRSYNC 0x00
33640#endif
33641#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
33642#define UNIXFILE_DELETE 0x20 /* Delete on close */
33643#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
33644#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
33645
33646/*
33647** Include code that is common to all os_*.c files
33648*/
33649/************** Include os_common.h in the middle of os_unix.c ***************/
33650/************** Begin file os_common.h ***************************************/
33651/*
33652** 2004 May 22
33653**
33654** The author disclaims copyright to this source code. In place of
33655** a legal notice, here is a blessing:
33656**
33657** May you do good and not evil.
33658** May you find forgiveness for yourself and forgive others.
33659** May you share freely, never taking more than you give.
33660**
33661******************************************************************************
33662**
33663** This file contains macros and a little bit of code that is common to
33664** all of the platform-specific files (os_*.c) and is #included into those
33665** files.
33666**
33667** This file should be #included by the os_*.c files only. It is not a
33668** general purpose header file.
33669*/
33670#ifndef _OS_COMMON_H_
33671#define _OS_COMMON_H_
33672
33673/*
33674** At least two bugs have slipped in because we changed the MEMORY_DEBUG
33675** macro to SQLITE_DEBUG and some older makefiles have not yet made the
33676** switch. The following code should catch this problem at compile-time.
33677*/
33678#ifdef MEMORY_DEBUG
33679# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
33680#endif
33681
33682/*
33683** Macros for performance tracing. Normally turned off. Only works
33684** on i486 hardware.
33685*/
33686#ifdef SQLITE_PERFORMANCE_TRACE
33687
33688/*
33689** hwtime.h contains inline assembler code for implementing
33690** high-performance timing routines.
33691*/
33692/************** Include hwtime.h in the middle of os_common.h ****************/
33693/************** Begin file hwtime.h ******************************************/
33694/*
33695** 2008 May 27
33696**
33697** The author disclaims copyright to this source code. In place of
33698** a legal notice, here is a blessing:
33699**
33700** May you do good and not evil.
33701** May you find forgiveness for yourself and forgive others.
33702** May you share freely, never taking more than you give.
33703**
33704******************************************************************************
33705**
33706** This file contains inline asm code for retrieving "high-performance"
33707** counters for x86 and x86_64 class CPUs.
33708*/
33709#ifndef SQLITE_HWTIME_H
33710#define SQLITE_HWTIME_H
33711
33712/*
33713** The following routine only works on pentium-class (or newer) processors.
33714** It uses the RDTSC opcode to read the cycle count value out of the
33715** processor and returns that value. This can be used for high-res
33716** profiling.
33717*/
33718#if !defined(__STRICT_ANSI__) && \
33719 (defined(__GNUC__) || defined(_MSC_VER)) && \
33720 (defined(i386) || defined(__i386__) || defined(_M_IX86))
33721
33722 #if defined(__GNUC__)
33723
33724 __inline__ sqlite_uint64 sqlite3Hwtime(void){
33725 unsigned int lo, hi;
33726 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
33727 return (sqlite_uint64)hi << 32 | lo;
33728 }
33729
33730 #elif defined(_MSC_VER)
33731
33732 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
33733 __asm {
33734 rdtsc
33735 ret ; return value at EDX:EAX
33736 }
33737 }
33738
33739 #endif
33740
33741#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
33742
33743 __inline__ sqlite_uint64 sqlite3Hwtime(void){
33744 unsigned long val;
33745 __asm__ __volatile__ ("rdtsc" : "=A" (val));
33746 return val;
33747 }
33748
33749#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
33750
33751 __inline__ sqlite_uint64 sqlite3Hwtime(void){
33752 unsigned long long retval;
33753 unsigned long junk;
33754 __asm__ __volatile__ ("\n\
33755 1: mftbu %1\n\
33756 mftb %L0\n\
33757 mftbu %0\n\
33758 cmpw %0,%1\n\
33759 bne 1b"
33760 : "=r" (retval), "=r" (junk));
33761 return retval;
33762 }
33763
33764#else
33765
33766 /*
33767 ** asm() is needed for hardware timing support. Without asm(),
33768 ** disable the sqlite3Hwtime() routine.
33769 **
33770 ** sqlite3Hwtime() is only used for some obscure debugging
33771 ** and analysis configurations, not in any deliverable, so this
33772 ** should not be a great loss.
33773 */
33774SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
33775
33776#endif
33777
33778#endif /* !defined(SQLITE_HWTIME_H) */
33779
33780/************** End of hwtime.h **********************************************/
33781/************** Continuing where we left off in os_common.h ******************/
33782
33783static sqlite_uint64 g_start;
33784static sqlite_uint64 g_elapsed;
33785#define TIMER_START g_start=sqlite3Hwtime()
33786#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
33787#define TIMER_ELAPSED g_elapsed
33788#else
33789#define TIMER_START
33790#define TIMER_END
33791#define TIMER_ELAPSED ((sqlite_uint64)0)
33792#endif
33793
33794/*
33795** If we compile with the SQLITE_TEST macro set, then the following block
33796** of code will give us the ability to simulate a disk I/O error. This
33797** is used for testing the I/O recovery logic.
33798*/
33799#if defined(SQLITE_TEST)
33800SQLITE_API extern int sqlite3_io_error_hit;
33801SQLITE_API extern int sqlite3_io_error_hardhit;
33802SQLITE_API extern int sqlite3_io_error_pending;
33803SQLITE_API extern int sqlite3_io_error_persist;
33804SQLITE_API extern int sqlite3_io_error_benign;
33805SQLITE_API extern int sqlite3_diskfull_pending;
33806SQLITE_API extern int sqlite3_diskfull;
33807#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
33808#define SimulateIOError(CODE) \
33809 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
33810 || sqlite3_io_error_pending-- == 1 ) \
33811 { local_ioerr(); CODE; }
33812static void local_ioerr(){
33813 IOTRACE(("IOERR\n"));
33814 sqlite3_io_error_hit++;
33815 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
33816}
33817#define SimulateDiskfullError(CODE) \
33818 if( sqlite3_diskfull_pending ){ \
33819 if( sqlite3_diskfull_pending == 1 ){ \
33820 local_ioerr(); \
33821 sqlite3_diskfull = 1; \
33822 sqlite3_io_error_hit = 1; \
33823 CODE; \
33824 }else{ \
33825 sqlite3_diskfull_pending--; \
33826 } \
33827 }
33828#else
33829#define SimulateIOErrorBenign(X)
33830#define SimulateIOError(A)
33831#define SimulateDiskfullError(A)
33832#endif /* defined(SQLITE_TEST) */
33833
33834/*
33835** When testing, keep a count of the number of open files.
33836*/
33837#if defined(SQLITE_TEST)
33838SQLITE_API extern int sqlite3_open_file_count;
33839#define OpenCounter(X) sqlite3_open_file_count+=(X)
33840#else
33841#define OpenCounter(X)
33842#endif /* defined(SQLITE_TEST) */
33843
33844#endif /* !defined(_OS_COMMON_H_) */
33845
33846/************** End of os_common.h *******************************************/
33847/************** Continuing where we left off in os_unix.c ********************/
33848
33849/*
33850** Define various macros that are missing from some systems.
33851*/
33852#ifndef O_LARGEFILE
33853# define O_LARGEFILE 0
33854#endif
33855#ifdef SQLITE_DISABLE_LFS
33856# undef O_LARGEFILE
33857# define O_LARGEFILE 0
33858#endif
33859#ifndef O_NOFOLLOW
33860# define O_NOFOLLOW 0
33861#endif
33862#ifndef O_BINARY
33863# define O_BINARY 0
33864#endif
33865
33866/*
33867** The threadid macro resolves to the thread-id or to 0. Used for
33868** testing and debugging only.
33869*/
33870#if SQLITE_THREADSAFE
33871#define threadid pthread_self()
33872#else
33873#define threadid 0
33874#endif
33875
33876/*
33877** HAVE_MREMAP defaults to true on Linux and false everywhere else.
33878*/
33879#if !defined(HAVE_MREMAP)
33880# if defined(__linux__) && defined(_GNU_SOURCE)
33881# define HAVE_MREMAP 1
33882# else
33883# define HAVE_MREMAP 0
33884# endif
33885#endif
33886
33887/*
33888** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
33889** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
33890*/
33891#ifdef __ANDROID__
33892# define lseek lseek64
33893#endif
33894
33895#ifdef __linux__
33896/*
33897** Linux-specific IOCTL magic numbers used for controlling F2FS
33898*/
33899#define F2FS_IOCTL_MAGIC 0xf5
33900#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
33901#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
33902#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
33903#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
33904#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32)
33905#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
33906#endif /* __linux__ */
33907
33908
33909/*
33910** Different Unix systems declare open() in different ways. Same use
33911** open(const char*,int,mode_t). Others use open(const char*,int,...).
33912** The difference is important when using a pointer to the function.
33913**
33914** The safest way to deal with the problem is to always use this wrapper
33915** which always has the same well-defined interface.
33916*/
33917static int posixOpen(const char *zFile, int flags, int mode){
33918 return open(zFile, flags, mode);
33919}
33920
33921/* Forward reference */
33922static int openDirectory(const char*, int*);
33923static int unixGetpagesize(void);
33924
33925/*
33926** Many system calls are accessed through pointer-to-functions so that
33927** they may be overridden at runtime to facilitate fault injection during
33928** testing and sandboxing. The following array holds the names and pointers
33929** to all overrideable system calls.
33930*/
33931static struct unix_syscall {
33932 const char *zName; /* Name of the system call */
33933 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
33934 sqlite3_syscall_ptr pDefault; /* Default value */
33935} aSyscall[] = {
33936 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
33937#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
33938
33939 { "close", (sqlite3_syscall_ptr)close, 0 },
33940#define osClose ((int(*)(int))aSyscall[1].pCurrent)
33941
33942 { "access", (sqlite3_syscall_ptr)access, 0 },
33943#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
33944
33945 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
33946#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
33947
33948 { "stat", (sqlite3_syscall_ptr)stat, 0 },
33949#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
33950
33951/*
33952** The DJGPP compiler environment looks mostly like Unix, but it
33953** lacks the fcntl() system call. So redefine fcntl() to be something
33954** that always succeeds. This means that locking does not occur under
33955** DJGPP. But it is DOS - what did you expect?
33956*/
33957#ifdef __DJGPP__
33958 { "fstat", 0, 0 },
33959#define osFstat(a,b,c) 0
33960#else
33961 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
33962#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
33963#endif
33964
33965 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
33966#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
33967
33968 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
33969#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
33970
33971 { "read", (sqlite3_syscall_ptr)read, 0 },
33972#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
33973
33974#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
33975 { "pread", (sqlite3_syscall_ptr)pread, 0 },
33976#else
33977 { "pread", (sqlite3_syscall_ptr)0, 0 },
33978#endif
33979#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
33980
33981#if defined(USE_PREAD64)
33982 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
33983#else
33984 { "pread64", (sqlite3_syscall_ptr)0, 0 },
33985#endif
33986#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
33987
33988 { "write", (sqlite3_syscall_ptr)write, 0 },
33989#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
33990
33991#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
33992 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
33993#else
33994 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
33995#endif
33996#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
33997 aSyscall[12].pCurrent)
33998
33999#if defined(USE_PREAD64)
34000 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
34001#else
34002 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
34003#endif
34004#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
34005 aSyscall[13].pCurrent)
34006
34007 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
34008#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
34009
34010#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
34011 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
34012#else
34013 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
34014#endif
34015#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
34016
34017 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
34018#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
34019
34020 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
34021#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
34022
34023 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
34024#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
34025
34026 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
34027#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
34028
34029#if defined(HAVE_FCHOWN)
34030 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
34031#else
34032 { "fchown", (sqlite3_syscall_ptr)0, 0 },
34033#endif
34034#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
34035
34036#if defined(HAVE_FCHOWN)
34037 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
34038#else
34039 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
34040#endif
34041#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
34042
34043#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34044 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
34045#else
34046 { "mmap", (sqlite3_syscall_ptr)0, 0 },
34047#endif
34048#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
34049
34050#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34051 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
34052#else
34053 { "munmap", (sqlite3_syscall_ptr)0, 0 },
34054#endif
34055#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
34056
34057#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
34058 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
34059#else
34060 { "mremap", (sqlite3_syscall_ptr)0, 0 },
34061#endif
34062#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
34063
34064#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34065 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
34066#else
34067 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
34068#endif
34069#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
34070
34071#if defined(HAVE_READLINK)
34072 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
34073#else
34074 { "readlink", (sqlite3_syscall_ptr)0, 0 },
34075#endif
34076#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
34077
34078#if defined(HAVE_LSTAT)
34079 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
34080#else
34081 { "lstat", (sqlite3_syscall_ptr)0, 0 },
34082#endif
34083#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
34084
34085#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
34086# ifdef __ANDROID__
34087 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
34088#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
34089# else
34090 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
34091#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
34092# endif
34093#else
34094 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
34095#endif
34096
34097}; /* End of the overrideable system calls */
34098
34099
34100/*
34101** On some systems, calls to fchown() will trigger a message in a security
34102** log if they come from non-root processes. So avoid calling fchown() if
34103** we are not running as root.
34104*/
34105static int robustFchown(int fd, uid_t uid, gid_t gid){
34106#if defined(HAVE_FCHOWN)
34107 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
34108#else
34109 return 0;
34110#endif
34111}
34112
34113/*
34114** This is the xSetSystemCall() method of sqlite3_vfs for all of the
34115** "unix" VFSes. Return SQLITE_OK opon successfully updating the
34116** system call pointer, or SQLITE_NOTFOUND if there is no configurable
34117** system call named zName.
34118*/
34119static int unixSetSystemCall(
34120 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
34121 const char *zName, /* Name of system call to override */
34122 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
34123){
34124 unsigned int i;
34125 int rc = SQLITE_NOTFOUND;
34126
34127 UNUSED_PARAMETER(pNotUsed);
34128 if( zName==0 ){
34129 /* If no zName is given, restore all system calls to their default
34130 ** settings and return NULL
34131 */
34132 rc = SQLITE_OK;
34133 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34134 if( aSyscall[i].pDefault ){
34135 aSyscall[i].pCurrent = aSyscall[i].pDefault;
34136 }
34137 }
34138 }else{
34139 /* If zName is specified, operate on only the one system call
34140 ** specified.
34141 */
34142 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34143 if( strcmp(zName, aSyscall[i].zName)==0 ){
34144 if( aSyscall[i].pDefault==0 ){
34145 aSyscall[i].pDefault = aSyscall[i].pCurrent;
34146 }
34147 rc = SQLITE_OK;
34148 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
34149 aSyscall[i].pCurrent = pNewFunc;
34150 break;
34151 }
34152 }
34153 }
34154 return rc;
34155}
34156
34157/*
34158** Return the value of a system call. Return NULL if zName is not a
34159** recognized system call name. NULL is also returned if the system call
34160** is currently undefined.
34161*/
34162static sqlite3_syscall_ptr unixGetSystemCall(
34163 sqlite3_vfs *pNotUsed,
34164 const char *zName
34165){
34166 unsigned int i;
34167
34168 UNUSED_PARAMETER(pNotUsed);
34169 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34170 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
34171 }
34172 return 0;
34173}
34174
34175/*
34176** Return the name of the first system call after zName. If zName==NULL
34177** then return the name of the first system call. Return NULL if zName
34178** is the last system call or if zName is not the name of a valid
34179** system call.
34180*/
34181static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
34182 int i = -1;
34183
34184 UNUSED_PARAMETER(p);
34185 if( zName ){
34186 for(i=0; i<ArraySize(aSyscall)-1; i++){
34187 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
34188 }
34189 }
34190 for(i++; i<ArraySize(aSyscall); i++){
34191 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
34192 }
34193 return 0;
34194}
34195
34196/*
34197** Do not accept any file descriptor less than this value, in order to avoid
34198** opening database file using file descriptors that are commonly used for
34199** standard input, output, and error.
34200*/
34201#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
34202# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
34203#endif
34204
34205/*
34206** Invoke open(). Do so multiple times, until it either succeeds or
34207** fails for some reason other than EINTR.
34208**
34209** If the file creation mode "m" is 0 then set it to the default for
34210** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
34211** 0644) as modified by the system umask. If m is not 0, then
34212** make the file creation mode be exactly m ignoring the umask.
34213**
34214** The m parameter will be non-zero only when creating -wal, -journal,
34215** and -shm files. We want those files to have *exactly* the same
34216** permissions as their original database, unadulterated by the umask.
34217** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
34218** transaction crashes and leaves behind hot journals, then any
34219** process that is able to write to the database will also be able to
34220** recover the hot journals.
34221*/
34222static int robust_open(const char *z, int f, mode_t m){
34223 int fd;
34224 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
34225 while(1){
34226#if defined(O_CLOEXEC)
34227 fd = osOpen(z,f|O_CLOEXEC,m2);
34228#else
34229 fd = osOpen(z,f,m2);
34230#endif
34231 if( fd<0 ){
34232 if( errno==EINTR ) continue;
34233 break;
34234 }
34235 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
34236 osClose(fd);
34237 sqlite3_log(SQLITE_WARNING,
34238 "attempt to open \"%s\" as file descriptor %d", z, fd);
34239 fd = -1;
34240 if( osOpen("/dev/null", O_RDONLY, m)<0 ) break;
34241 }
34242 if( fd>=0 ){
34243 if( m!=0 ){
34244 struct stat statbuf;
34245 if( osFstat(fd, &statbuf)==0
34246 && statbuf.st_size==0
34247 && (statbuf.st_mode&0777)!=m
34248 ){
34249 osFchmod(fd, m);
34250 }
34251 }
34252#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
34253 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
34254#endif
34255 }
34256 return fd;
34257}
34258
34259/*
34260** Helper functions to obtain and relinquish the global mutex. The
34261** global mutex is used to protect the unixInodeInfo and
34262** vxworksFileId objects used by this file, all of which may be
34263** shared by multiple threads.
34264**
34265** Function unixMutexHeld() is used to assert() that the global mutex
34266** is held when required. This function is only used as part of assert()
34267** statements. e.g.
34268**
34269** unixEnterMutex()
34270** assert( unixMutexHeld() );
34271** unixEnterLeave()
34272**
34273** To prevent deadlock, the global unixBigLock must must be acquired
34274** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
34275** OK to get the pLockMutex without holding unixBigLock first, but if
34276** that happens, the unixBigLock mutex must not be acquired until after
34277** pLockMutex is released.
34278**
34279** OK: enter(unixBigLock), enter(pLockInfo)
34280** OK: enter(unixBigLock)
34281** OK: enter(pLockInfo)
34282** ERROR: enter(pLockInfo), enter(unixBigLock)
34283*/
34284static sqlite3_mutex *unixBigLock = 0;
34285static void unixEnterMutex(void){
34286 assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */
34287 sqlite3_mutex_enter(unixBigLock);
34288}
34289static void unixLeaveMutex(void){
34290 assert( sqlite3_mutex_held(unixBigLock) );
34291 sqlite3_mutex_leave(unixBigLock);
34292}
34293#ifdef SQLITE_DEBUG
34294static int unixMutexHeld(void) {
34295 return sqlite3_mutex_held(unixBigLock);
34296}
34297#endif
34298
34299
34300#ifdef SQLITE_HAVE_OS_TRACE
34301/*
34302** Helper function for printing out trace information from debugging
34303** binaries. This returns the string representation of the supplied
34304** integer lock-type.
34305*/
34306static const char *azFileLock(int eFileLock){
34307 switch( eFileLock ){
34308 case NO_LOCK: return "NONE";
34309 case SHARED_LOCK: return "SHARED";
34310 case RESERVED_LOCK: return "RESERVED";
34311 case PENDING_LOCK: return "PENDING";
34312 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
34313 }
34314 return "ERROR";
34315}
34316#endif
34317
34318#ifdef SQLITE_LOCK_TRACE
34319/*
34320** Print out information about all locking operations.
34321**
34322** This routine is used for troubleshooting locks on multithreaded
34323** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
34324** command-line option on the compiler. This code is normally
34325** turned off.
34326*/
34327static int lockTrace(int fd, int op, struct flock *p){
34328 char *zOpName, *zType;
34329 int s;
34330 int savedErrno;
34331 if( op==F_GETLK ){
34332 zOpName = "GETLK";
34333 }else if( op==F_SETLK ){
34334 zOpName = "SETLK";
34335 }else{
34336 s = osFcntl(fd, op, p);
34337 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
34338 return s;
34339 }
34340 if( p->l_type==F_RDLCK ){
34341 zType = "RDLCK";
34342 }else if( p->l_type==F_WRLCK ){
34343 zType = "WRLCK";
34344 }else if( p->l_type==F_UNLCK ){
34345 zType = "UNLCK";
34346 }else{
34347 assert( 0 );
34348 }
34349 assert( p->l_whence==SEEK_SET );
34350 s = osFcntl(fd, op, p);
34351 savedErrno = errno;
34352 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
34353 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
34354 (int)p->l_pid, s);
34355 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
34356 struct flock l2;
34357 l2 = *p;
34358 osFcntl(fd, F_GETLK, &l2);
34359 if( l2.l_type==F_RDLCK ){
34360 zType = "RDLCK";
34361 }else if( l2.l_type==F_WRLCK ){
34362 zType = "WRLCK";
34363 }else if( l2.l_type==F_UNLCK ){
34364 zType = "UNLCK";
34365 }else{
34366 assert( 0 );
34367 }
34368 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
34369 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
34370 }
34371 errno = savedErrno;
34372 return s;
34373}
34374#undef osFcntl
34375#define osFcntl lockTrace
34376#endif /* SQLITE_LOCK_TRACE */
34377
34378/*
34379** Retry ftruncate() calls that fail due to EINTR
34380**
34381** All calls to ftruncate() within this file should be made through
34382** this wrapper. On the Android platform, bypassing the logic below
34383** could lead to a corrupt database.
34384*/
34385static int robust_ftruncate(int h, sqlite3_int64 sz){
34386 int rc;
34387#ifdef __ANDROID__
34388 /* On Android, ftruncate() always uses 32-bit offsets, even if
34389 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
34390 ** truncate a file to any size larger than 2GiB. Silently ignore any
34391 ** such attempts. */
34392 if( sz>(sqlite3_int64)0x7FFFFFFF ){
34393 rc = SQLITE_OK;
34394 }else
34395#endif
34396 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
34397 return rc;
34398}
34399
34400/*
34401** This routine translates a standard POSIX errno code into something
34402** useful to the clients of the sqlite3 functions. Specifically, it is
34403** intended to translate a variety of "try again" errors into SQLITE_BUSY
34404** and a variety of "please close the file descriptor NOW" errors into
34405** SQLITE_IOERR
34406**
34407** Errors during initialization of locks, or file system support for locks,
34408** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
34409*/
34410static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
34411 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
34412 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
34413 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
34414 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
34415 switch (posixError) {
34416 case EACCES:
34417 case EAGAIN:
34418 case ETIMEDOUT:
34419 case EBUSY:
34420 case EINTR:
34421 case ENOLCK:
34422 /* random NFS retry error, unless during file system support
34423 * introspection, in which it actually means what it says */
34424 return SQLITE_BUSY;
34425
34426 case EPERM:
34427 return SQLITE_PERM;
34428
34429 default:
34430 return sqliteIOErr;
34431 }
34432}
34433
34434
34435/******************************************************************************
34436****************** Begin Unique File ID Utility Used By VxWorks ***************
34437**
34438** On most versions of unix, we can get a unique ID for a file by concatenating
34439** the device number and the inode number. But this does not work on VxWorks.
34440** On VxWorks, a unique file id must be based on the canonical filename.
34441**
34442** A pointer to an instance of the following structure can be used as a
34443** unique file ID in VxWorks. Each instance of this structure contains
34444** a copy of the canonical filename. There is also a reference count.
34445** The structure is reclaimed when the number of pointers to it drops to
34446** zero.
34447**
34448** There are never very many files open at one time and lookups are not
34449** a performance-critical path, so it is sufficient to put these
34450** structures on a linked list.
34451*/
34452struct vxworksFileId {
34453 struct vxworksFileId *pNext; /* Next in a list of them all */
34454 int nRef; /* Number of references to this one */
34455 int nName; /* Length of the zCanonicalName[] string */
34456 char *zCanonicalName; /* Canonical filename */
34457};
34458
34459#if OS_VXWORKS
34460/*
34461** All unique filenames are held on a linked list headed by this
34462** variable:
34463*/
34464static struct vxworksFileId *vxworksFileList = 0;
34465
34466/*
34467** Simplify a filename into its canonical form
34468** by making the following changes:
34469**
34470** * removing any trailing and duplicate /
34471** * convert /./ into just /
34472** * convert /A/../ where A is any simple name into just /
34473**
34474** Changes are made in-place. Return the new name length.
34475**
34476** The original filename is in z[0..n-1]. Return the number of
34477** characters in the simplified name.
34478*/
34479static int vxworksSimplifyName(char *z, int n){
34480 int i, j;
34481 while( n>1 && z[n-1]=='/' ){ n--; }
34482 for(i=j=0; i<n; i++){
34483 if( z[i]=='/' ){
34484 if( z[i+1]=='/' ) continue;
34485 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
34486 i += 1;
34487 continue;
34488 }
34489 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
34490 while( j>0 && z[j-1]!='/' ){ j--; }
34491 if( j>0 ){ j--; }
34492 i += 2;
34493 continue;
34494 }
34495 }
34496 z[j++] = z[i];
34497 }
34498 z[j] = 0;
34499 return j;
34500}
34501
34502/*
34503** Find a unique file ID for the given absolute pathname. Return
34504** a pointer to the vxworksFileId object. This pointer is the unique
34505** file ID.
34506**
34507** The nRef field of the vxworksFileId object is incremented before
34508** the object is returned. A new vxworksFileId object is created
34509** and added to the global list if necessary.
34510**
34511** If a memory allocation error occurs, return NULL.
34512*/
34513static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
34514 struct vxworksFileId *pNew; /* search key and new file ID */
34515 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
34516 int n; /* Length of zAbsoluteName string */
34517
34518 assert( zAbsoluteName[0]=='/' );
34519 n = (int)strlen(zAbsoluteName);
34520 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
34521 if( pNew==0 ) return 0;
34522 pNew->zCanonicalName = (char*)&pNew[1];
34523 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
34524 n = vxworksSimplifyName(pNew->zCanonicalName, n);
34525
34526 /* Search for an existing entry that matching the canonical name.
34527 ** If found, increment the reference count and return a pointer to
34528 ** the existing file ID.
34529 */
34530 unixEnterMutex();
34531 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
34532 if( pCandidate->nName==n
34533 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
34534 ){
34535 sqlite3_free(pNew);
34536 pCandidate->nRef++;
34537 unixLeaveMutex();
34538 return pCandidate;
34539 }
34540 }
34541
34542 /* No match was found. We will make a new file ID */
34543 pNew->nRef = 1;
34544 pNew->nName = n;
34545 pNew->pNext = vxworksFileList;
34546 vxworksFileList = pNew;
34547 unixLeaveMutex();
34548 return pNew;
34549}
34550
34551/*
34552** Decrement the reference count on a vxworksFileId object. Free
34553** the object when the reference count reaches zero.
34554*/
34555static void vxworksReleaseFileId(struct vxworksFileId *pId){
34556 unixEnterMutex();
34557 assert( pId->nRef>0 );
34558 pId->nRef--;
34559 if( pId->nRef==0 ){
34560 struct vxworksFileId **pp;
34561 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
34562 assert( *pp==pId );
34563 *pp = pId->pNext;
34564 sqlite3_free(pId);
34565 }
34566 unixLeaveMutex();
34567}
34568#endif /* OS_VXWORKS */
34569/*************** End of Unique File ID Utility Used By VxWorks ****************
34570******************************************************************************/
34571
34572
34573/******************************************************************************
34574*************************** Posix Advisory Locking ****************************
34575**
34576** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
34577** section 6.5.2.2 lines 483 through 490 specify that when a process
34578** sets or clears a lock, that operation overrides any prior locks set
34579** by the same process. It does not explicitly say so, but this implies
34580** that it overrides locks set by the same process using a different
34581** file descriptor. Consider this test case:
34582**
34583** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
34584** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
34585**
34586** Suppose ./file1 and ./file2 are really the same file (because
34587** one is a hard or symbolic link to the other) then if you set
34588** an exclusive lock on fd1, then try to get an exclusive lock
34589** on fd2, it works. I would have expected the second lock to
34590** fail since there was already a lock on the file due to fd1.
34591** But not so. Since both locks came from the same process, the
34592** second overrides the first, even though they were on different
34593** file descriptors opened on different file names.
34594**
34595** This means that we cannot use POSIX locks to synchronize file access
34596** among competing threads of the same process. POSIX locks will work fine
34597** to synchronize access for threads in separate processes, but not
34598** threads within the same process.
34599**
34600** To work around the problem, SQLite has to manage file locks internally
34601** on its own. Whenever a new database is opened, we have to find the
34602** specific inode of the database file (the inode is determined by the
34603** st_dev and st_ino fields of the stat structure that fstat() fills in)
34604** and check for locks already existing on that inode. When locks are
34605** created or removed, we have to look at our own internal record of the
34606** locks to see if another thread has previously set a lock on that same
34607** inode.
34608**
34609** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
34610** For VxWorks, we have to use the alternative unique ID system based on
34611** canonical filename and implemented in the previous division.)
34612**
34613** The sqlite3_file structure for POSIX is no longer just an integer file
34614** descriptor. It is now a structure that holds the integer file
34615** descriptor and a pointer to a structure that describes the internal
34616** locks on the corresponding inode. There is one locking structure
34617** per inode, so if the same inode is opened twice, both unixFile structures
34618** point to the same locking structure. The locking structure keeps
34619** a reference count (so we will know when to delete it) and a "cnt"
34620** field that tells us its internal lock status. cnt==0 means the
34621** file is unlocked. cnt==-1 means the file has an exclusive lock.
34622** cnt>0 means there are cnt shared locks on the file.
34623**
34624** Any attempt to lock or unlock a file first checks the locking
34625** structure. The fcntl() system call is only invoked to set a
34626** POSIX lock if the internal lock structure transitions between
34627** a locked and an unlocked state.
34628**
34629** But wait: there are yet more problems with POSIX advisory locks.
34630**
34631** If you close a file descriptor that points to a file that has locks,
34632** all locks on that file that are owned by the current process are
34633** released. To work around this problem, each unixInodeInfo object
34634** maintains a count of the number of pending locks on tha inode.
34635** When an attempt is made to close an unixFile, if there are
34636** other unixFile open on the same inode that are holding locks, the call
34637** to close() the file descriptor is deferred until all of the locks clear.
34638** The unixInodeInfo structure keeps a list of file descriptors that need to
34639** be closed and that list is walked (and cleared) when the last lock
34640** clears.
34641**
34642** Yet another problem: LinuxThreads do not play well with posix locks.
34643**
34644** Many older versions of linux use the LinuxThreads library which is
34645** not posix compliant. Under LinuxThreads, a lock created by thread
34646** A cannot be modified or overridden by a different thread B.
34647** Only thread A can modify the lock. Locking behavior is correct
34648** if the appliation uses the newer Native Posix Thread Library (NPTL)
34649** on linux - with NPTL a lock created by thread A can override locks
34650** in thread B. But there is no way to know at compile-time which
34651** threading library is being used. So there is no way to know at
34652** compile-time whether or not thread A can override locks on thread B.
34653** One has to do a run-time check to discover the behavior of the
34654** current process.
34655**
34656** SQLite used to support LinuxThreads. But support for LinuxThreads
34657** was dropped beginning with version 3.7.0. SQLite will still work with
34658** LinuxThreads provided that (1) there is no more than one connection
34659** per database file in the same process and (2) database connections
34660** do not move across threads.
34661*/
34662
34663/*
34664** An instance of the following structure serves as the key used
34665** to locate a particular unixInodeInfo object.
34666*/
34667struct unixFileId {
34668 dev_t dev; /* Device number */
34669#if OS_VXWORKS
34670 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
34671#else
34672 /* We are told that some versions of Android contain a bug that
34673 ** sizes ino_t at only 32-bits instead of 64-bits. (See
34674 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
34675 ** To work around this, always allocate 64-bits for the inode number.
34676 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
34677 ** but that should not be a big deal. */
34678 /* WAS: ino_t ino; */
34679 u64 ino; /* Inode number */
34680#endif
34681};
34682
34683/*
34684** An instance of the following structure is allocated for each open
34685** inode.
34686**
34687** A single inode can have multiple file descriptors, so each unixFile
34688** structure contains a pointer to an instance of this object and this
34689** object keeps a count of the number of unixFile pointing to it.
34690**
34691** Mutex rules:
34692**
34693** (1) Only the pLockMutex mutex must be held in order to read or write
34694** any of the locking fields:
34695** nShared, nLock, eFileLock, bProcessLock, pUnused
34696**
34697** (2) When nRef>0, then the following fields are unchanging and can
34698** be read (but not written) without holding any mutex:
34699** fileId, pLockMutex
34700**
34701** (3) With the exceptions above, all the fields may only be read
34702** or written while holding the global unixBigLock mutex.
34703**
34704** Deadlock prevention: The global unixBigLock mutex may not
34705** be acquired while holding the pLockMutex mutex. If both unixBigLock
34706** and pLockMutex are needed, then unixBigLock must be acquired first.
34707*/
34708struct unixInodeInfo {
34709 struct unixFileId fileId; /* The lookup key */
34710 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
34711 int nShared; /* Number of SHARED locks held */
34712 int nLock; /* Number of outstanding file locks */
34713 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
34714 unsigned char bProcessLock; /* An exclusive process lock is held */
34715 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
34716 int nRef; /* Number of pointers to this structure */
34717 unixShmNode *pShmNode; /* Shared memory associated with this inode */
34718 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
34719 unixInodeInfo *pPrev; /* .... doubly linked */
34720#if SQLITE_ENABLE_LOCKING_STYLE
34721 unsigned long long sharedByte; /* for AFP simulated shared lock */
34722#endif
34723#if OS_VXWORKS
34724 sem_t *pSem; /* Named POSIX semaphore */
34725 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
34726#endif
34727};
34728
34729/*
34730** A lists of all unixInodeInfo objects.
34731**
34732** Must hold unixBigLock in order to read or write this variable.
34733*/
34734static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
34735
34736#ifdef SQLITE_DEBUG
34737/*
34738** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
34739** This routine is used only within assert() to help verify correct mutex
34740** usage.
34741*/
34742int unixFileMutexHeld(unixFile *pFile){
34743 assert( pFile->pInode );
34744 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
34745}
34746int unixFileMutexNotheld(unixFile *pFile){
34747 assert( pFile->pInode );
34748 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
34749}
34750#endif
34751
34752/*
34753**
34754** This function - unixLogErrorAtLine(), is only ever called via the macro
34755** unixLogError().
34756**
34757** It is invoked after an error occurs in an OS function and errno has been
34758** set. It logs a message using sqlite3_log() containing the current value of
34759** errno and, if possible, the human-readable equivalent from strerror() or
34760** strerror_r().
34761**
34762** The first argument passed to the macro should be the error code that
34763** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
34764** The two subsequent arguments should be the name of the OS function that
34765** failed (e.g. "unlink", "open") and the associated file-system path,
34766** if any.
34767*/
34768#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
34769static int unixLogErrorAtLine(
34770 int errcode, /* SQLite error code */
34771 const char *zFunc, /* Name of OS function that failed */
34772 const char *zPath, /* File path associated with error */
34773 int iLine /* Source line number where error occurred */
34774){
34775 char *zErr; /* Message from strerror() or equivalent */
34776 int iErrno = errno; /* Saved syscall error number */
34777
34778 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
34779 ** the strerror() function to obtain the human-readable error message
34780 ** equivalent to errno. Otherwise, use strerror_r().
34781 */
34782#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
34783 char aErr[80];
34784 memset(aErr, 0, sizeof(aErr));
34785 zErr = aErr;
34786
34787 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
34788 ** assume that the system provides the GNU version of strerror_r() that
34789 ** returns a pointer to a buffer containing the error message. That pointer
34790 ** may point to aErr[], or it may point to some static storage somewhere.
34791 ** Otherwise, assume that the system provides the POSIX version of
34792 ** strerror_r(), which always writes an error message into aErr[].
34793 **
34794 ** If the code incorrectly assumes that it is the POSIX version that is
34795 ** available, the error message will often be an empty string. Not a
34796 ** huge problem. Incorrectly concluding that the GNU version is available
34797 ** could lead to a segfault though.
34798 */
34799#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
34800 zErr =
34801# endif
34802 strerror_r(iErrno, aErr, sizeof(aErr)-1);
34803
34804#elif SQLITE_THREADSAFE
34805 /* This is a threadsafe build, but strerror_r() is not available. */
34806 zErr = "";
34807#else
34808 /* Non-threadsafe build, use strerror(). */
34809 zErr = strerror(iErrno);
34810#endif
34811
34812 if( zPath==0 ) zPath = "";
34813 sqlite3_log(errcode,
34814 "os_unix.c:%d: (%d) %s(%s) - %s",
34815 iLine, iErrno, zFunc, zPath, zErr
34816 );
34817
34818 return errcode;
34819}
34820
34821/*
34822** Close a file descriptor.
34823**
34824** We assume that close() almost always works, since it is only in a
34825** very sick application or on a very sick platform that it might fail.
34826** If it does fail, simply leak the file descriptor, but do log the
34827** error.
34828**
34829** Note that it is not safe to retry close() after EINTR since the
34830** file descriptor might have already been reused by another thread.
34831** So we don't even try to recover from an EINTR. Just log the error
34832** and move on.
34833*/
34834static void robust_close(unixFile *pFile, int h, int lineno){
34835 if( osClose(h) ){
34836 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
34837 pFile ? pFile->zPath : 0, lineno);
34838 }
34839}
34840
34841/*
34842** Set the pFile->lastErrno. Do this in a subroutine as that provides
34843** a convenient place to set a breakpoint.
34844*/
34845static void storeLastErrno(unixFile *pFile, int error){
34846 pFile->lastErrno = error;
34847}
34848
34849/*
34850** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
34851*/
34852static void closePendingFds(unixFile *pFile){
34853 unixInodeInfo *pInode = pFile->pInode;
34854 UnixUnusedFd *p;
34855 UnixUnusedFd *pNext;
34856 assert( unixFileMutexHeld(pFile) );
34857 for(p=pInode->pUnused; p; p=pNext){
34858 pNext = p->pNext;
34859 robust_close(pFile, p->fd, __LINE__);
34860 sqlite3_free(p);
34861 }
34862 pInode->pUnused = 0;
34863}
34864
34865/*
34866** Release a unixInodeInfo structure previously allocated by findInodeInfo().
34867**
34868** The global mutex must be held when this routine is called, but the mutex
34869** on the inode being deleted must NOT be held.
34870*/
34871static void releaseInodeInfo(unixFile *pFile){
34872 unixInodeInfo *pInode = pFile->pInode;
34873 assert( unixMutexHeld() );
34874 assert( unixFileMutexNotheld(pFile) );
34875 if( ALWAYS(pInode) ){
34876 pInode->nRef--;
34877 if( pInode->nRef==0 ){
34878 assert( pInode->pShmNode==0 );
34879 sqlite3_mutex_enter(pInode->pLockMutex);
34880 closePendingFds(pFile);
34881 sqlite3_mutex_leave(pInode->pLockMutex);
34882 if( pInode->pPrev ){
34883 assert( pInode->pPrev->pNext==pInode );
34884 pInode->pPrev->pNext = pInode->pNext;
34885 }else{
34886 assert( inodeList==pInode );
34887 inodeList = pInode->pNext;
34888 }
34889 if( pInode->pNext ){
34890 assert( pInode->pNext->pPrev==pInode );
34891 pInode->pNext->pPrev = pInode->pPrev;
34892 }
34893 sqlite3_mutex_free(pInode->pLockMutex);
34894 sqlite3_free(pInode);
34895 }
34896 }
34897}
34898
34899/*
34900** Given a file descriptor, locate the unixInodeInfo object that
34901** describes that file descriptor. Create a new one if necessary. The
34902** return value might be uninitialized if an error occurs.
34903**
34904** The global mutex must held when calling this routine.
34905**
34906** Return an appropriate error code.
34907*/
34908static int findInodeInfo(
34909 unixFile *pFile, /* Unix file with file desc used in the key */
34910 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
34911){
34912 int rc; /* System call return code */
34913 int fd; /* The file descriptor for pFile */
34914 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
34915 struct stat statbuf; /* Low-level file information */
34916 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
34917
34918 assert( unixMutexHeld() );
34919
34920 /* Get low-level information about the file that we can used to
34921 ** create a unique name for the file.
34922 */
34923 fd = pFile->h;
34924 rc = osFstat(fd, &statbuf);
34925 if( rc!=0 ){
34926 storeLastErrno(pFile, errno);
34927#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
34928 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
34929#endif
34930 return SQLITE_IOERR;
34931 }
34932
34933#ifdef __APPLE__
34934 /* On OS X on an msdos filesystem, the inode number is reported
34935 ** incorrectly for zero-size files. See ticket #3260. To work
34936 ** around this problem (we consider it a bug in OS X, not SQLite)
34937 ** we always increase the file size to 1 by writing a single byte
34938 ** prior to accessing the inode number. The one byte written is
34939 ** an ASCII 'S' character which also happens to be the first byte
34940 ** in the header of every SQLite database. In this way, if there
34941 ** is a race condition such that another thread has already populated
34942 ** the first page of the database, no damage is done.
34943 */
34944 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
34945 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
34946 if( rc!=1 ){
34947 storeLastErrno(pFile, errno);
34948 return SQLITE_IOERR;
34949 }
34950 rc = osFstat(fd, &statbuf);
34951 if( rc!=0 ){
34952 storeLastErrno(pFile, errno);
34953 return SQLITE_IOERR;
34954 }
34955 }
34956#endif
34957
34958 memset(&fileId, 0, sizeof(fileId));
34959 fileId.dev = statbuf.st_dev;
34960#if OS_VXWORKS
34961 fileId.pId = pFile->pId;
34962#else
34963 fileId.ino = (u64)statbuf.st_ino;
34964#endif
34965 assert( unixMutexHeld() );
34966 pInode = inodeList;
34967 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
34968 pInode = pInode->pNext;
34969 }
34970 if( pInode==0 ){
34971 pInode = sqlite3_malloc64( sizeof(*pInode) );
34972 if( pInode==0 ){
34973 return SQLITE_NOMEM_BKPT;
34974 }
34975 memset(pInode, 0, sizeof(*pInode));
34976 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
34977 if( sqlite3GlobalConfig.bCoreMutex ){
34978 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34979 if( pInode->pLockMutex==0 ){
34980 sqlite3_free(pInode);
34981 return SQLITE_NOMEM_BKPT;
34982 }
34983 }
34984 pInode->nRef = 1;
34985 assert( unixMutexHeld() );
34986 pInode->pNext = inodeList;
34987 pInode->pPrev = 0;
34988 if( inodeList ) inodeList->pPrev = pInode;
34989 inodeList = pInode;
34990 }else{
34991 pInode->nRef++;
34992 }
34993 *ppInode = pInode;
34994 return SQLITE_OK;
34995}
34996
34997/*
34998** Return TRUE if pFile has been renamed or unlinked since it was first opened.
34999*/
35000static int fileHasMoved(unixFile *pFile){
35001#if OS_VXWORKS
35002 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
35003#else
35004 struct stat buf;
35005 return pFile->pInode!=0 &&
35006 (osStat(pFile->zPath, &buf)!=0
35007 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
35008#endif
35009}
35010
35011
35012/*
35013** Check a unixFile that is a database. Verify the following:
35014**
35015** (1) There is exactly one hard link on the file
35016** (2) The file is not a symbolic link
35017** (3) The file has not been renamed or unlinked
35018**
35019** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
35020*/
35021static void verifyDbFile(unixFile *pFile){
35022 struct stat buf;
35023 int rc;
35024
35025 /* These verifications occurs for the main database only */
35026 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
35027
35028 rc = osFstat(pFile->h, &buf);
35029 if( rc!=0 ){
35030 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
35031 return;
35032 }
35033 if( buf.st_nlink==0 ){
35034 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
35035 return;
35036 }
35037 if( buf.st_nlink>1 ){
35038 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
35039 return;
35040 }
35041 if( fileHasMoved(pFile) ){
35042 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
35043 return;
35044 }
35045}
35046
35047
35048/*
35049** This routine checks if there is a RESERVED lock held on the specified
35050** file by this or any other process. If such a lock is held, set *pResOut
35051** to a non-zero value otherwise *pResOut is set to zero. The return value
35052** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35053*/
35054static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
35055 int rc = SQLITE_OK;
35056 int reserved = 0;
35057 unixFile *pFile = (unixFile*)id;
35058
35059 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35060
35061 assert( pFile );
35062 assert( pFile->eFileLock<=SHARED_LOCK );
35063 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
35064
35065 /* Check if a thread in this process holds such a lock */
35066 if( pFile->pInode->eFileLock>SHARED_LOCK ){
35067 reserved = 1;
35068 }
35069
35070 /* Otherwise see if some other process holds it.
35071 */
35072#ifndef __DJGPP__
35073 if( !reserved && !pFile->pInode->bProcessLock ){
35074 struct flock lock;
35075 lock.l_whence = SEEK_SET;
35076 lock.l_start = RESERVED_BYTE;
35077 lock.l_len = 1;
35078 lock.l_type = F_WRLCK;
35079 if( osFcntl(pFile->h, F_GETLK, &lock) ){
35080 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
35081 storeLastErrno(pFile, errno);
35082 } else if( lock.l_type!=F_UNLCK ){
35083 reserved = 1;
35084 }
35085 }
35086#endif
35087
35088 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
35089 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
35090
35091 *pResOut = reserved;
35092 return rc;
35093}
35094
35095/*
35096** Set a posix-advisory-lock.
35097**
35098** There are two versions of this routine. If compiled with
35099** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
35100** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
35101** value is set, then it is the number of milliseconds to wait before
35102** failing the lock. The iBusyTimeout value is always reset back to
35103** zero on each call.
35104**
35105** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
35106** attempt to set the lock.
35107*/
35108#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
35109# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
35110#else
35111static int osSetPosixAdvisoryLock(
35112 int h, /* The file descriptor on which to take the lock */
35113 struct flock *pLock, /* The description of the lock */
35114 unixFile *pFile /* Structure holding timeout value */
35115){
35116 int tm = pFile->iBusyTimeout;
35117 int rc = osFcntl(h,F_SETLK,pLock);
35118 while( rc<0 && tm>0 ){
35119 /* On systems that support some kind of blocking file lock with a timeout,
35120 ** make appropriate changes here to invoke that blocking file lock. On
35121 ** generic posix, however, there is no such API. So we simply try the
35122 ** lock once every millisecond until either the timeout expires, or until
35123 ** the lock is obtained. */
35124 usleep(1000);
35125 rc = osFcntl(h,F_SETLK,pLock);
35126 tm--;
35127 }
35128 return rc;
35129}
35130#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
35131
35132
35133/*
35134** Attempt to set a system-lock on the file pFile. The lock is
35135** described by pLock.
35136**
35137** If the pFile was opened read/write from unix-excl, then the only lock
35138** ever obtained is an exclusive lock, and it is obtained exactly once
35139** the first time any lock is attempted. All subsequent system locking
35140** operations become no-ops. Locking operations still happen internally,
35141** in order to coordinate access between separate database connections
35142** within this process, but all of that is handled in memory and the
35143** operating system does not participate.
35144**
35145** This function is a pass-through to fcntl(F_SETLK) if pFile is using
35146** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
35147** and is read-only.
35148**
35149** Zero is returned if the call completes successfully, or -1 if a call
35150** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
35151*/
35152static int unixFileLock(unixFile *pFile, struct flock *pLock){
35153 int rc;
35154 unixInodeInfo *pInode = pFile->pInode;
35155 assert( pInode!=0 );
35156 assert( sqlite3_mutex_held(pInode->pLockMutex) );
35157 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
35158 if( pInode->bProcessLock==0 ){
35159 struct flock lock;
35160 assert( pInode->nLock==0 );
35161 lock.l_whence = SEEK_SET;
35162 lock.l_start = SHARED_FIRST;
35163 lock.l_len = SHARED_SIZE;
35164 lock.l_type = F_WRLCK;
35165 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
35166 if( rc<0 ) return rc;
35167 pInode->bProcessLock = 1;
35168 pInode->nLock++;
35169 }else{
35170 rc = 0;
35171 }
35172 }else{
35173 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
35174 }
35175 return rc;
35176}
35177
35178/*
35179** Lock the file with the lock specified by parameter eFileLock - one
35180** of the following:
35181**
35182** (1) SHARED_LOCK
35183** (2) RESERVED_LOCK
35184** (3) PENDING_LOCK
35185** (4) EXCLUSIVE_LOCK
35186**
35187** Sometimes when requesting one lock state, additional lock states
35188** are inserted in between. The locking might fail on one of the later
35189** transitions leaving the lock state different from what it started but
35190** still short of its goal. The following chart shows the allowed
35191** transitions and the inserted intermediate states:
35192**
35193** UNLOCKED -> SHARED
35194** SHARED -> RESERVED
35195** SHARED -> (PENDING) -> EXCLUSIVE
35196** RESERVED -> (PENDING) -> EXCLUSIVE
35197** PENDING -> EXCLUSIVE
35198**
35199** This routine will only increase a lock. Use the sqlite3OsUnlock()
35200** routine to lower a locking level.
35201*/
35202static int unixLock(sqlite3_file *id, int eFileLock){
35203 /* The following describes the implementation of the various locks and
35204 ** lock transitions in terms of the POSIX advisory shared and exclusive
35205 ** lock primitives (called read-locks and write-locks below, to avoid
35206 ** confusion with SQLite lock names). The algorithms are complicated
35207 ** slightly in order to be compatible with Windows95 systems simultaneously
35208 ** accessing the same database file, in case that is ever required.
35209 **
35210 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
35211 ** byte', each single bytes at well known offsets, and the 'shared byte
35212 ** range', a range of 510 bytes at a well known offset.
35213 **
35214 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
35215 ** byte'. If this is successful, 'shared byte range' is read-locked
35216 ** and the lock on the 'pending byte' released. (Legacy note: When
35217 ** SQLite was first developed, Windows95 systems were still very common,
35218 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
35219 ** single randomly selected by from the 'shared byte range' is locked.
35220 ** Windows95 is now pretty much extinct, but this work-around for the
35221 ** lack of shared-locks on Windows95 lives on, for backwards
35222 ** compatibility.)
35223 **
35224 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
35225 ** A RESERVED lock is implemented by grabbing a write-lock on the
35226 ** 'reserved byte'.
35227 **
35228 ** A process may only obtain a PENDING lock after it has obtained a
35229 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
35230 ** on the 'pending byte'. This ensures that no new SHARED locks can be
35231 ** obtained, but existing SHARED locks are allowed to persist. A process
35232 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
35233 ** This property is used by the algorithm for rolling back a journal file
35234 ** after a crash.
35235 **
35236 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
35237 ** implemented by obtaining a write-lock on the entire 'shared byte
35238 ** range'. Since all other locks require a read-lock on one of the bytes
35239 ** within this range, this ensures that no other locks are held on the
35240 ** database.
35241 */
35242 int rc = SQLITE_OK;
35243 unixFile *pFile = (unixFile*)id;
35244 unixInodeInfo *pInode;
35245 struct flock lock;
35246 int tErrno = 0;
35247
35248 assert( pFile );
35249 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
35250 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35251 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
35252 osGetpid(0)));
35253
35254 /* If there is already a lock of this type or more restrictive on the
35255 ** unixFile, do nothing. Don't use the end_lock: exit path, as
35256 ** unixEnterMutex() hasn't been called yet.
35257 */
35258 if( pFile->eFileLock>=eFileLock ){
35259 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
35260 azFileLock(eFileLock)));
35261 return SQLITE_OK;
35262 }
35263
35264 /* Make sure the locking sequence is correct.
35265 ** (1) We never move from unlocked to anything higher than shared lock.
35266 ** (2) SQLite never explicitly requests a pendig lock.
35267 ** (3) A shared lock is always held when a reserve lock is requested.
35268 */
35269 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35270 assert( eFileLock!=PENDING_LOCK );
35271 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35272
35273 /* This mutex is needed because pFile->pInode is shared across threads
35274 */
35275 pInode = pFile->pInode;
35276 sqlite3_mutex_enter(pInode->pLockMutex);
35277
35278 /* If some thread using this PID has a lock via a different unixFile*
35279 ** handle that precludes the requested lock, return BUSY.
35280 */
35281 if( (pFile->eFileLock!=pInode->eFileLock &&
35282 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35283 ){
35284 rc = SQLITE_BUSY;
35285 goto end_lock;
35286 }
35287
35288 /* If a SHARED lock is requested, and some thread using this PID already
35289 ** has a SHARED or RESERVED lock, then increment reference counts and
35290 ** return SQLITE_OK.
35291 */
35292 if( eFileLock==SHARED_LOCK &&
35293 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35294 assert( eFileLock==SHARED_LOCK );
35295 assert( pFile->eFileLock==0 );
35296 assert( pInode->nShared>0 );
35297 pFile->eFileLock = SHARED_LOCK;
35298 pInode->nShared++;
35299 pInode->nLock++;
35300 goto end_lock;
35301 }
35302
35303
35304 /* A PENDING lock is needed before acquiring a SHARED lock and before
35305 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
35306 ** be released.
35307 */
35308 lock.l_len = 1L;
35309 lock.l_whence = SEEK_SET;
35310 if( eFileLock==SHARED_LOCK
35311 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
35312 ){
35313 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
35314 lock.l_start = PENDING_BYTE;
35315 if( unixFileLock(pFile, &lock) ){
35316 tErrno = errno;
35317 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35318 if( rc!=SQLITE_BUSY ){
35319 storeLastErrno(pFile, tErrno);
35320 }
35321 goto end_lock;
35322 }
35323 }
35324
35325
35326 /* If control gets to this point, then actually go ahead and make
35327 ** operating system calls for the specified lock.
35328 */
35329 if( eFileLock==SHARED_LOCK ){
35330 assert( pInode->nShared==0 );
35331 assert( pInode->eFileLock==0 );
35332 assert( rc==SQLITE_OK );
35333
35334 /* Now get the read-lock */
35335 lock.l_start = SHARED_FIRST;
35336 lock.l_len = SHARED_SIZE;
35337 if( unixFileLock(pFile, &lock) ){
35338 tErrno = errno;
35339 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35340 }
35341
35342 /* Drop the temporary PENDING lock */
35343 lock.l_start = PENDING_BYTE;
35344 lock.l_len = 1L;
35345 lock.l_type = F_UNLCK;
35346 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
35347 /* This could happen with a network mount */
35348 tErrno = errno;
35349 rc = SQLITE_IOERR_UNLOCK;
35350 }
35351
35352 if( rc ){
35353 if( rc!=SQLITE_BUSY ){
35354 storeLastErrno(pFile, tErrno);
35355 }
35356 goto end_lock;
35357 }else{
35358 pFile->eFileLock = SHARED_LOCK;
35359 pInode->nLock++;
35360 pInode->nShared = 1;
35361 }
35362 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
35363 /* We are trying for an exclusive lock but another thread in this
35364 ** same process is still holding a shared lock. */
35365 rc = SQLITE_BUSY;
35366 }else{
35367 /* The request was for a RESERVED or EXCLUSIVE lock. It is
35368 ** assumed that there is a SHARED or greater lock on the file
35369 ** already.
35370 */
35371 assert( 0!=pFile->eFileLock );
35372 lock.l_type = F_WRLCK;
35373
35374 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
35375 if( eFileLock==RESERVED_LOCK ){
35376 lock.l_start = RESERVED_BYTE;
35377 lock.l_len = 1L;
35378 }else{
35379 lock.l_start = SHARED_FIRST;
35380 lock.l_len = SHARED_SIZE;
35381 }
35382
35383 if( unixFileLock(pFile, &lock) ){
35384 tErrno = errno;
35385 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35386 if( rc!=SQLITE_BUSY ){
35387 storeLastErrno(pFile, tErrno);
35388 }
35389 }
35390 }
35391
35392
35393#ifdef SQLITE_DEBUG
35394 /* Set up the transaction-counter change checking flags when
35395 ** transitioning from a SHARED to a RESERVED lock. The change
35396 ** from SHARED to RESERVED marks the beginning of a normal
35397 ** write operation (not a hot journal rollback).
35398 */
35399 if( rc==SQLITE_OK
35400 && pFile->eFileLock<=SHARED_LOCK
35401 && eFileLock==RESERVED_LOCK
35402 ){
35403 pFile->transCntrChng = 0;
35404 pFile->dbUpdate = 0;
35405 pFile->inNormalWrite = 1;
35406 }
35407#endif
35408
35409
35410 if( rc==SQLITE_OK ){
35411 pFile->eFileLock = eFileLock;
35412 pInode->eFileLock = eFileLock;
35413 }else if( eFileLock==EXCLUSIVE_LOCK ){
35414 pFile->eFileLock = PENDING_LOCK;
35415 pInode->eFileLock = PENDING_LOCK;
35416 }
35417
35418end_lock:
35419 sqlite3_mutex_leave(pInode->pLockMutex);
35420 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
35421 rc==SQLITE_OK ? "ok" : "failed"));
35422 return rc;
35423}
35424
35425/*
35426** Add the file descriptor used by file handle pFile to the corresponding
35427** pUnused list.
35428*/
35429static void setPendingFd(unixFile *pFile){
35430 unixInodeInfo *pInode = pFile->pInode;
35431 UnixUnusedFd *p = pFile->pPreallocatedUnused;
35432 assert( unixFileMutexHeld(pFile) );
35433 p->pNext = pInode->pUnused;
35434 pInode->pUnused = p;
35435 pFile->h = -1;
35436 pFile->pPreallocatedUnused = 0;
35437}
35438
35439/*
35440** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35441** must be either NO_LOCK or SHARED_LOCK.
35442**
35443** If the locking level of the file descriptor is already at or below
35444** the requested locking level, this routine is a no-op.
35445**
35446** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
35447** the byte range is divided into 2 parts and the first part is unlocked then
35448** set to a read lock, then the other part is simply unlocked. This works
35449** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
35450** remove the write lock on a region when a read lock is set.
35451*/
35452static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
35453 unixFile *pFile = (unixFile*)id;
35454 unixInodeInfo *pInode;
35455 struct flock lock;
35456 int rc = SQLITE_OK;
35457
35458 assert( pFile );
35459 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
35460 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35461 osGetpid(0)));
35462
35463 assert( eFileLock<=SHARED_LOCK );
35464 if( pFile->eFileLock<=eFileLock ){
35465 return SQLITE_OK;
35466 }
35467 pInode = pFile->pInode;
35468 sqlite3_mutex_enter(pInode->pLockMutex);
35469 assert( pInode->nShared!=0 );
35470 if( pFile->eFileLock>SHARED_LOCK ){
35471 assert( pInode->eFileLock==pFile->eFileLock );
35472
35473#ifdef SQLITE_DEBUG
35474 /* When reducing a lock such that other processes can start
35475 ** reading the database file again, make sure that the
35476 ** transaction counter was updated if any part of the database
35477 ** file changed. If the transaction counter is not updated,
35478 ** other connections to the same file might not realize that
35479 ** the file has changed and hence might not know to flush their
35480 ** cache. The use of a stale cache can lead to database corruption.
35481 */
35482 pFile->inNormalWrite = 0;
35483#endif
35484
35485 /* downgrading to a shared lock on NFS involves clearing the write lock
35486 ** before establishing the readlock - to avoid a race condition we downgrade
35487 ** the lock in 2 blocks, so that part of the range will be covered by a
35488 ** write lock until the rest is covered by a read lock:
35489 ** 1: [WWWWW]
35490 ** 2: [....W]
35491 ** 3: [RRRRW]
35492 ** 4: [RRRR.]
35493 */
35494 if( eFileLock==SHARED_LOCK ){
35495#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
35496 (void)handleNFSUnlock;
35497 assert( handleNFSUnlock==0 );
35498#endif
35499#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
35500 if( handleNFSUnlock ){
35501 int tErrno; /* Error code from system call errors */
35502 off_t divSize = SHARED_SIZE - 1;
35503
35504 lock.l_type = F_UNLCK;
35505 lock.l_whence = SEEK_SET;
35506 lock.l_start = SHARED_FIRST;
35507 lock.l_len = divSize;
35508 if( unixFileLock(pFile, &lock)==(-1) ){
35509 tErrno = errno;
35510 rc = SQLITE_IOERR_UNLOCK;
35511 storeLastErrno(pFile, tErrno);
35512 goto end_unlock;
35513 }
35514 lock.l_type = F_RDLCK;
35515 lock.l_whence = SEEK_SET;
35516 lock.l_start = SHARED_FIRST;
35517 lock.l_len = divSize;
35518 if( unixFileLock(pFile, &lock)==(-1) ){
35519 tErrno = errno;
35520 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
35521 if( IS_LOCK_ERROR(rc) ){
35522 storeLastErrno(pFile, tErrno);
35523 }
35524 goto end_unlock;
35525 }
35526 lock.l_type = F_UNLCK;
35527 lock.l_whence = SEEK_SET;
35528 lock.l_start = SHARED_FIRST+divSize;
35529 lock.l_len = SHARED_SIZE-divSize;
35530 if( unixFileLock(pFile, &lock)==(-1) ){
35531 tErrno = errno;
35532 rc = SQLITE_IOERR_UNLOCK;
35533 storeLastErrno(pFile, tErrno);
35534 goto end_unlock;
35535 }
35536 }else
35537#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35538 {
35539 lock.l_type = F_RDLCK;
35540 lock.l_whence = SEEK_SET;
35541 lock.l_start = SHARED_FIRST;
35542 lock.l_len = SHARED_SIZE;
35543 if( unixFileLock(pFile, &lock) ){
35544 /* In theory, the call to unixFileLock() cannot fail because another
35545 ** process is holding an incompatible lock. If it does, this
35546 ** indicates that the other process is not following the locking
35547 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
35548 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
35549 ** an assert to fail). */
35550 rc = SQLITE_IOERR_RDLOCK;
35551 storeLastErrno(pFile, errno);
35552 goto end_unlock;
35553 }
35554 }
35555 }
35556 lock.l_type = F_UNLCK;
35557 lock.l_whence = SEEK_SET;
35558 lock.l_start = PENDING_BYTE;
35559 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
35560 if( unixFileLock(pFile, &lock)==0 ){
35561 pInode->eFileLock = SHARED_LOCK;
35562 }else{
35563 rc = SQLITE_IOERR_UNLOCK;
35564 storeLastErrno(pFile, errno);
35565 goto end_unlock;
35566 }
35567 }
35568 if( eFileLock==NO_LOCK ){
35569 /* Decrement the shared lock counter. Release the lock using an
35570 ** OS call only when all threads in this same process have released
35571 ** the lock.
35572 */
35573 pInode->nShared--;
35574 if( pInode->nShared==0 ){
35575 lock.l_type = F_UNLCK;
35576 lock.l_whence = SEEK_SET;
35577 lock.l_start = lock.l_len = 0L;
35578 if( unixFileLock(pFile, &lock)==0 ){
35579 pInode->eFileLock = NO_LOCK;
35580 }else{
35581 rc = SQLITE_IOERR_UNLOCK;
35582 storeLastErrno(pFile, errno);
35583 pInode->eFileLock = NO_LOCK;
35584 pFile->eFileLock = NO_LOCK;
35585 }
35586 }
35587
35588 /* Decrement the count of locks against this same file. When the
35589 ** count reaches zero, close any other file descriptors whose close
35590 ** was deferred because of outstanding locks.
35591 */
35592 pInode->nLock--;
35593 assert( pInode->nLock>=0 );
35594 if( pInode->nLock==0 ) closePendingFds(pFile);
35595 }
35596
35597end_unlock:
35598 sqlite3_mutex_leave(pInode->pLockMutex);
35599 if( rc==SQLITE_OK ){
35600 pFile->eFileLock = eFileLock;
35601 }
35602 return rc;
35603}
35604
35605/*
35606** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35607** must be either NO_LOCK or SHARED_LOCK.
35608**
35609** If the locking level of the file descriptor is already at or below
35610** the requested locking level, this routine is a no-op.
35611*/
35612static int unixUnlock(sqlite3_file *id, int eFileLock){
35613#if SQLITE_MAX_MMAP_SIZE>0
35614 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
35615#endif
35616 return posixUnlock(id, eFileLock, 0);
35617}
35618
35619#if SQLITE_MAX_MMAP_SIZE>0
35620static int unixMapfile(unixFile *pFd, i64 nByte);
35621static void unixUnmapfile(unixFile *pFd);
35622#endif
35623
35624/*
35625** This function performs the parts of the "close file" operation
35626** common to all locking schemes. It closes the directory and file
35627** handles, if they are valid, and sets all fields of the unixFile
35628** structure to 0.
35629**
35630** It is *not* necessary to hold the mutex when this routine is called,
35631** even on VxWorks. A mutex will be acquired on VxWorks by the
35632** vxworksReleaseFileId() routine.
35633*/
35634static int closeUnixFile(sqlite3_file *id){
35635 unixFile *pFile = (unixFile*)id;
35636#if SQLITE_MAX_MMAP_SIZE>0
35637 unixUnmapfile(pFile);
35638#endif
35639 if( pFile->h>=0 ){
35640 robust_close(pFile, pFile->h, __LINE__);
35641 pFile->h = -1;
35642 }
35643#if OS_VXWORKS
35644 if( pFile->pId ){
35645 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35646 osUnlink(pFile->pId->zCanonicalName);
35647 }
35648 vxworksReleaseFileId(pFile->pId);
35649 pFile->pId = 0;
35650 }
35651#endif
35652#ifdef SQLITE_UNLINK_AFTER_CLOSE
35653 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35654 osUnlink(pFile->zPath);
35655 sqlite3_free(*(char**)&pFile->zPath);
35656 pFile->zPath = 0;
35657 }
35658#endif
35659 OSTRACE(("CLOSE %-3d\n", pFile->h));
35660 OpenCounter(-1);
35661 sqlite3_free(pFile->pPreallocatedUnused);
35662 memset(pFile, 0, sizeof(unixFile));
35663 return SQLITE_OK;
35664}
35665
35666/*
35667** Close a file.
35668*/
35669static int unixClose(sqlite3_file *id){
35670 int rc = SQLITE_OK;
35671 unixFile *pFile = (unixFile *)id;
35672 unixInodeInfo *pInode = pFile->pInode;
35673
35674 assert( pInode!=0 );
35675 verifyDbFile(pFile);
35676 unixUnlock(id, NO_LOCK);
35677 assert( unixFileMutexNotheld(pFile) );
35678 unixEnterMutex();
35679
35680 /* unixFile.pInode is always valid here. Otherwise, a different close
35681 ** routine (e.g. nolockClose()) would be called instead.
35682 */
35683 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
35684 sqlite3_mutex_enter(pInode->pLockMutex);
35685 if( pInode->nLock ){
35686 /* If there are outstanding locks, do not actually close the file just
35687 ** yet because that would clear those locks. Instead, add the file
35688 ** descriptor to pInode->pUnused list. It will be automatically closed
35689 ** when the last lock is cleared.
35690 */
35691 setPendingFd(pFile);
35692 }
35693 sqlite3_mutex_leave(pInode->pLockMutex);
35694 releaseInodeInfo(pFile);
35695 rc = closeUnixFile(id);
35696 unixLeaveMutex();
35697 return rc;
35698}
35699
35700/************** End of the posix advisory lock implementation *****************
35701******************************************************************************/
35702
35703/******************************************************************************
35704****************************** No-op Locking **********************************
35705**
35706** Of the various locking implementations available, this is by far the
35707** simplest: locking is ignored. No attempt is made to lock the database
35708** file for reading or writing.
35709**
35710** This locking mode is appropriate for use on read-only databases
35711** (ex: databases that are burned into CD-ROM, for example.) It can
35712** also be used if the application employs some external mechanism to
35713** prevent simultaneous access of the same database by two or more
35714** database connections. But there is a serious risk of database
35715** corruption if this locking mode is used in situations where multiple
35716** database connections are accessing the same database file at the same
35717** time and one or more of those connections are writing.
35718*/
35719
35720static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
35721 UNUSED_PARAMETER(NotUsed);
35722 *pResOut = 0;
35723 return SQLITE_OK;
35724}
35725static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
35726 UNUSED_PARAMETER2(NotUsed, NotUsed2);
35727 return SQLITE_OK;
35728}
35729static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
35730 UNUSED_PARAMETER2(NotUsed, NotUsed2);
35731 return SQLITE_OK;
35732}
35733
35734/*
35735** Close the file.
35736*/
35737static int nolockClose(sqlite3_file *id) {
35738 return closeUnixFile(id);
35739}
35740
35741/******************* End of the no-op lock implementation *********************
35742******************************************************************************/
35743
35744/******************************************************************************
35745************************* Begin dot-file Locking ******************************
35746**
35747** The dotfile locking implementation uses the existence of separate lock
35748** files (really a directory) to control access to the database. This works
35749** on just about every filesystem imaginable. But there are serious downsides:
35750**
35751** (1) There is zero concurrency. A single reader blocks all other
35752** connections from reading or writing the database.
35753**
35754** (2) An application crash or power loss can leave stale lock files
35755** sitting around that need to be cleared manually.
35756**
35757** Nevertheless, a dotlock is an appropriate locking mode for use if no
35758** other locking strategy is available.
35759**
35760** Dotfile locking works by creating a subdirectory in the same directory as
35761** the database and with the same name but with a ".lock" extension added.
35762** The existence of a lock directory implies an EXCLUSIVE lock. All other
35763** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
35764*/
35765
35766/*
35767** The file suffix added to the data base filename in order to create the
35768** lock directory.
35769*/
35770#define DOTLOCK_SUFFIX ".lock"
35771
35772/*
35773** This routine checks if there is a RESERVED lock held on the specified
35774** file by this or any other process. If such a lock is held, set *pResOut
35775** to a non-zero value otherwise *pResOut is set to zero. The return value
35776** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35777**
35778** In dotfile locking, either a lock exists or it does not. So in this
35779** variation of CheckReservedLock(), *pResOut is set to true if any lock
35780** is held on the file and false if the file is unlocked.
35781*/
35782static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
35783 int rc = SQLITE_OK;
35784 int reserved = 0;
35785 unixFile *pFile = (unixFile*)id;
35786
35787 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35788
35789 assert( pFile );
35790 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
35791 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
35792 *pResOut = reserved;
35793 return rc;
35794}
35795
35796/*
35797** Lock the file with the lock specified by parameter eFileLock - one
35798** of the following:
35799**
35800** (1) SHARED_LOCK
35801** (2) RESERVED_LOCK
35802** (3) PENDING_LOCK
35803** (4) EXCLUSIVE_LOCK
35804**
35805** Sometimes when requesting one lock state, additional lock states
35806** are inserted in between. The locking might fail on one of the later
35807** transitions leaving the lock state different from what it started but
35808** still short of its goal. The following chart shows the allowed
35809** transitions and the inserted intermediate states:
35810**
35811** UNLOCKED -> SHARED
35812** SHARED -> RESERVED
35813** SHARED -> (PENDING) -> EXCLUSIVE
35814** RESERVED -> (PENDING) -> EXCLUSIVE
35815** PENDING -> EXCLUSIVE
35816**
35817** This routine will only increase a lock. Use the sqlite3OsUnlock()
35818** routine to lower a locking level.
35819**
35820** With dotfile locking, we really only support state (4): EXCLUSIVE.
35821** But we track the other locking levels internally.
35822*/
35823static int dotlockLock(sqlite3_file *id, int eFileLock) {
35824 unixFile *pFile = (unixFile*)id;
35825 char *zLockFile = (char *)pFile->lockingContext;
35826 int rc = SQLITE_OK;
35827
35828
35829 /* If we have any lock, then the lock file already exists. All we have
35830 ** to do is adjust our internal record of the lock level.
35831 */
35832 if( pFile->eFileLock > NO_LOCK ){
35833 pFile->eFileLock = eFileLock;
35834 /* Always update the timestamp on the old file */
35835#ifdef HAVE_UTIME
35836 utime(zLockFile, NULL);
35837#else
35838 utimes(zLockFile, NULL);
35839#endif
35840 return SQLITE_OK;
35841 }
35842
35843 /* grab an exclusive lock */
35844 rc = osMkdir(zLockFile, 0777);
35845 if( rc<0 ){
35846 /* failed to open/create the lock directory */
35847 int tErrno = errno;
35848 if( EEXIST == tErrno ){
35849 rc = SQLITE_BUSY;
35850 } else {
35851 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35852 if( rc!=SQLITE_BUSY ){
35853 storeLastErrno(pFile, tErrno);
35854 }
35855 }
35856 return rc;
35857 }
35858
35859 /* got it, set the type and return ok */
35860 pFile->eFileLock = eFileLock;
35861 return rc;
35862}
35863
35864/*
35865** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35866** must be either NO_LOCK or SHARED_LOCK.
35867**
35868** If the locking level of the file descriptor is already at or below
35869** the requested locking level, this routine is a no-op.
35870**
35871** When the locking level reaches NO_LOCK, delete the lock file.
35872*/
35873static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
35874 unixFile *pFile = (unixFile*)id;
35875 char *zLockFile = (char *)pFile->lockingContext;
35876 int rc;
35877
35878 assert( pFile );
35879 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
35880 pFile->eFileLock, osGetpid(0)));
35881 assert( eFileLock<=SHARED_LOCK );
35882
35883 /* no-op if possible */
35884 if( pFile->eFileLock==eFileLock ){
35885 return SQLITE_OK;
35886 }
35887
35888 /* To downgrade to shared, simply update our internal notion of the
35889 ** lock state. No need to mess with the file on disk.
35890 */
35891 if( eFileLock==SHARED_LOCK ){
35892 pFile->eFileLock = SHARED_LOCK;
35893 return SQLITE_OK;
35894 }
35895
35896 /* To fully unlock the database, delete the lock file */
35897 assert( eFileLock==NO_LOCK );
35898 rc = osRmdir(zLockFile);
35899 if( rc<0 ){
35900 int tErrno = errno;
35901 if( tErrno==ENOENT ){
35902 rc = SQLITE_OK;
35903 }else{
35904 rc = SQLITE_IOERR_UNLOCK;
35905 storeLastErrno(pFile, tErrno);
35906 }
35907 return rc;
35908 }
35909 pFile->eFileLock = NO_LOCK;
35910 return SQLITE_OK;
35911}
35912
35913/*
35914** Close a file. Make sure the lock has been released before closing.
35915*/
35916static int dotlockClose(sqlite3_file *id) {
35917 unixFile *pFile = (unixFile*)id;
35918 assert( id!=0 );
35919 dotlockUnlock(id, NO_LOCK);
35920 sqlite3_free(pFile->lockingContext);
35921 return closeUnixFile(id);
35922}
35923/****************** End of the dot-file lock implementation *******************
35924******************************************************************************/
35925
35926/******************************************************************************
35927************************** Begin flock Locking ********************************
35928**
35929** Use the flock() system call to do file locking.
35930**
35931** flock() locking is like dot-file locking in that the various
35932** fine-grain locking levels supported by SQLite are collapsed into
35933** a single exclusive lock. In other words, SHARED, RESERVED, and
35934** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
35935** still works when you do this, but concurrency is reduced since
35936** only a single process can be reading the database at a time.
35937**
35938** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
35939*/
35940#if SQLITE_ENABLE_LOCKING_STYLE
35941
35942/*
35943** Retry flock() calls that fail with EINTR
35944*/
35945#ifdef EINTR
35946static int robust_flock(int fd, int op){
35947 int rc;
35948 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
35949 return rc;
35950}
35951#else
35952# define robust_flock(a,b) flock(a,b)
35953#endif
35954
35955
35956/*
35957** This routine checks if there is a RESERVED lock held on the specified
35958** file by this or any other process. If such a lock is held, set *pResOut
35959** to a non-zero value otherwise *pResOut is set to zero. The return value
35960** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35961*/
35962static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
35963 int rc = SQLITE_OK;
35964 int reserved = 0;
35965 unixFile *pFile = (unixFile*)id;
35966
35967 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35968
35969 assert( pFile );
35970
35971 /* Check if a thread in this process holds such a lock */
35972 if( pFile->eFileLock>SHARED_LOCK ){
35973 reserved = 1;
35974 }
35975
35976 /* Otherwise see if some other process holds it. */
35977 if( !reserved ){
35978 /* attempt to get the lock */
35979 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
35980 if( !lrc ){
35981 /* got the lock, unlock it */
35982 lrc = robust_flock(pFile->h, LOCK_UN);
35983 if ( lrc ) {
35984 int tErrno = errno;
35985 /* unlock failed with an error */
35986 lrc = SQLITE_IOERR_UNLOCK;
35987 storeLastErrno(pFile, tErrno);
35988 rc = lrc;
35989 }
35990 } else {
35991 int tErrno = errno;
35992 reserved = 1;
35993 /* someone else might have it reserved */
35994 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
35995 if( IS_LOCK_ERROR(lrc) ){
35996 storeLastErrno(pFile, tErrno);
35997 rc = lrc;
35998 }
35999 }
36000 }
36001 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
36002
36003#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36004 if( (rc & 0xff) == SQLITE_IOERR ){
36005 rc = SQLITE_OK;
36006 reserved=1;
36007 }
36008#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36009 *pResOut = reserved;
36010 return rc;
36011}
36012
36013/*
36014** Lock the file with the lock specified by parameter eFileLock - one
36015** of the following:
36016**
36017** (1) SHARED_LOCK
36018** (2) RESERVED_LOCK
36019** (3) PENDING_LOCK
36020** (4) EXCLUSIVE_LOCK
36021**
36022** Sometimes when requesting one lock state, additional lock states
36023** are inserted in between. The locking might fail on one of the later
36024** transitions leaving the lock state different from what it started but
36025** still short of its goal. The following chart shows the allowed
36026** transitions and the inserted intermediate states:
36027**
36028** UNLOCKED -> SHARED
36029** SHARED -> RESERVED
36030** SHARED -> (PENDING) -> EXCLUSIVE
36031** RESERVED -> (PENDING) -> EXCLUSIVE
36032** PENDING -> EXCLUSIVE
36033**
36034** flock() only really support EXCLUSIVE locks. We track intermediate
36035** lock states in the sqlite3_file structure, but all locks SHARED or
36036** above are really EXCLUSIVE locks and exclude all other processes from
36037** access the file.
36038**
36039** This routine will only increase a lock. Use the sqlite3OsUnlock()
36040** routine to lower a locking level.
36041*/
36042static int flockLock(sqlite3_file *id, int eFileLock) {
36043 int rc = SQLITE_OK;
36044 unixFile *pFile = (unixFile*)id;
36045
36046 assert( pFile );
36047
36048 /* if we already have a lock, it is exclusive.
36049 ** Just adjust level and punt on outta here. */
36050 if (pFile->eFileLock > NO_LOCK) {
36051 pFile->eFileLock = eFileLock;
36052 return SQLITE_OK;
36053 }
36054
36055 /* grab an exclusive lock */
36056
36057 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
36058 int tErrno = errno;
36059 /* didn't get, must be busy */
36060 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
36061 if( IS_LOCK_ERROR(rc) ){
36062 storeLastErrno(pFile, tErrno);
36063 }
36064 } else {
36065 /* got it, set the type and return ok */
36066 pFile->eFileLock = eFileLock;
36067 }
36068 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
36069 rc==SQLITE_OK ? "ok" : "failed"));
36070#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36071 if( (rc & 0xff) == SQLITE_IOERR ){
36072 rc = SQLITE_BUSY;
36073 }
36074#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36075 return rc;
36076}
36077
36078
36079/*
36080** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36081** must be either NO_LOCK or SHARED_LOCK.
36082**
36083** If the locking level of the file descriptor is already at or below
36084** the requested locking level, this routine is a no-op.
36085*/
36086static int flockUnlock(sqlite3_file *id, int eFileLock) {
36087 unixFile *pFile = (unixFile*)id;
36088
36089 assert( pFile );
36090 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
36091 pFile->eFileLock, osGetpid(0)));
36092 assert( eFileLock<=SHARED_LOCK );
36093
36094 /* no-op if possible */
36095 if( pFile->eFileLock==eFileLock ){
36096 return SQLITE_OK;
36097 }
36098
36099 /* shared can just be set because we always have an exclusive */
36100 if (eFileLock==SHARED_LOCK) {
36101 pFile->eFileLock = eFileLock;
36102 return SQLITE_OK;
36103 }
36104
36105 /* no, really, unlock. */
36106 if( robust_flock(pFile->h, LOCK_UN) ){
36107#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
36108 return SQLITE_OK;
36109#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
36110 return SQLITE_IOERR_UNLOCK;
36111 }else{
36112 pFile->eFileLock = NO_LOCK;
36113 return SQLITE_OK;
36114 }
36115}
36116
36117/*
36118** Close a file.
36119*/
36120static int flockClose(sqlite3_file *id) {
36121 assert( id!=0 );
36122 flockUnlock(id, NO_LOCK);
36123 return closeUnixFile(id);
36124}
36125
36126#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
36127
36128/******************* End of the flock lock implementation *********************
36129******************************************************************************/
36130
36131/******************************************************************************
36132************************ Begin Named Semaphore Locking ************************
36133**
36134** Named semaphore locking is only supported on VxWorks.
36135**
36136** Semaphore locking is like dot-lock and flock in that it really only
36137** supports EXCLUSIVE locking. Only a single process can read or write
36138** the database file at a time. This reduces potential concurrency, but
36139** makes the lock implementation much easier.
36140*/
36141#if OS_VXWORKS
36142
36143/*
36144** This routine checks if there is a RESERVED lock held on the specified
36145** file by this or any other process. If such a lock is held, set *pResOut
36146** to a non-zero value otherwise *pResOut is set to zero. The return value
36147** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36148*/
36149static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
36150 int rc = SQLITE_OK;
36151 int reserved = 0;
36152 unixFile *pFile = (unixFile*)id;
36153
36154 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36155
36156 assert( pFile );
36157
36158 /* Check if a thread in this process holds such a lock */
36159 if( pFile->eFileLock>SHARED_LOCK ){
36160 reserved = 1;
36161 }
36162
36163 /* Otherwise see if some other process holds it. */
36164 if( !reserved ){
36165 sem_t *pSem = pFile->pInode->pSem;
36166
36167 if( sem_trywait(pSem)==-1 ){
36168 int tErrno = errno;
36169 if( EAGAIN != tErrno ){
36170 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
36171 storeLastErrno(pFile, tErrno);
36172 } else {
36173 /* someone else has the lock when we are in NO_LOCK */
36174 reserved = (pFile->eFileLock < SHARED_LOCK);
36175 }
36176 }else{
36177 /* we could have it if we want it */
36178 sem_post(pSem);
36179 }
36180 }
36181 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
36182
36183 *pResOut = reserved;
36184 return rc;
36185}
36186
36187/*
36188** Lock the file with the lock specified by parameter eFileLock - one
36189** of the following:
36190**
36191** (1) SHARED_LOCK
36192** (2) RESERVED_LOCK
36193** (3) PENDING_LOCK
36194** (4) EXCLUSIVE_LOCK
36195**
36196** Sometimes when requesting one lock state, additional lock states
36197** are inserted in between. The locking might fail on one of the later
36198** transitions leaving the lock state different from what it started but
36199** still short of its goal. The following chart shows the allowed
36200** transitions and the inserted intermediate states:
36201**
36202** UNLOCKED -> SHARED
36203** SHARED -> RESERVED
36204** SHARED -> (PENDING) -> EXCLUSIVE
36205** RESERVED -> (PENDING) -> EXCLUSIVE
36206** PENDING -> EXCLUSIVE
36207**
36208** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
36209** lock states in the sqlite3_file structure, but all locks SHARED or
36210** above are really EXCLUSIVE locks and exclude all other processes from
36211** access the file.
36212**
36213** This routine will only increase a lock. Use the sqlite3OsUnlock()
36214** routine to lower a locking level.
36215*/
36216static int semXLock(sqlite3_file *id, int eFileLock) {
36217 unixFile *pFile = (unixFile*)id;
36218 sem_t *pSem = pFile->pInode->pSem;
36219 int rc = SQLITE_OK;
36220
36221 /* if we already have a lock, it is exclusive.
36222 ** Just adjust level and punt on outta here. */
36223 if (pFile->eFileLock > NO_LOCK) {
36224 pFile->eFileLock = eFileLock;
36225 rc = SQLITE_OK;
36226 goto sem_end_lock;
36227 }
36228
36229 /* lock semaphore now but bail out when already locked. */
36230 if( sem_trywait(pSem)==-1 ){
36231 rc = SQLITE_BUSY;
36232 goto sem_end_lock;
36233 }
36234
36235 /* got it, set the type and return ok */
36236 pFile->eFileLock = eFileLock;
36237
36238 sem_end_lock:
36239 return rc;
36240}
36241
36242/*
36243** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36244** must be either NO_LOCK or SHARED_LOCK.
36245**
36246** If the locking level of the file descriptor is already at or below
36247** the requested locking level, this routine is a no-op.
36248*/
36249static int semXUnlock(sqlite3_file *id, int eFileLock) {
36250 unixFile *pFile = (unixFile*)id;
36251 sem_t *pSem = pFile->pInode->pSem;
36252
36253 assert( pFile );
36254 assert( pSem );
36255 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
36256 pFile->eFileLock, osGetpid(0)));
36257 assert( eFileLock<=SHARED_LOCK );
36258
36259 /* no-op if possible */
36260 if( pFile->eFileLock==eFileLock ){
36261 return SQLITE_OK;
36262 }
36263
36264 /* shared can just be set because we always have an exclusive */
36265 if (eFileLock==SHARED_LOCK) {
36266 pFile->eFileLock = eFileLock;
36267 return SQLITE_OK;
36268 }
36269
36270 /* no, really unlock. */
36271 if ( sem_post(pSem)==-1 ) {
36272 int rc, tErrno = errno;
36273 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
36274 if( IS_LOCK_ERROR(rc) ){
36275 storeLastErrno(pFile, tErrno);
36276 }
36277 return rc;
36278 }
36279 pFile->eFileLock = NO_LOCK;
36280 return SQLITE_OK;
36281}
36282
36283/*
36284 ** Close a file.
36285 */
36286static int semXClose(sqlite3_file *id) {
36287 if( id ){
36288 unixFile *pFile = (unixFile*)id;
36289 semXUnlock(id, NO_LOCK);
36290 assert( pFile );
36291 assert( unixFileMutexNotheld(pFile) );
36292 unixEnterMutex();
36293 releaseInodeInfo(pFile);
36294 unixLeaveMutex();
36295 closeUnixFile(id);
36296 }
36297 return SQLITE_OK;
36298}
36299
36300#endif /* OS_VXWORKS */
36301/*
36302** Named semaphore locking is only available on VxWorks.
36303**
36304*************** End of the named semaphore lock implementation ****************
36305******************************************************************************/
36306
36307
36308/******************************************************************************
36309*************************** Begin AFP Locking *********************************
36310**
36311** AFP is the Apple Filing Protocol. AFP is a network filesystem found
36312** on Apple Macintosh computers - both OS9 and OSX.
36313**
36314** Third-party implementations of AFP are available. But this code here
36315** only works on OSX.
36316*/
36317
36318#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
36319/*
36320** The afpLockingContext structure contains all afp lock specific state
36321*/
36322typedef struct afpLockingContext afpLockingContext;
36323struct afpLockingContext {
36324 int reserved;
36325 const char *dbPath; /* Name of the open file */
36326};
36327
36328struct ByteRangeLockPB2
36329{
36330 unsigned long long offset; /* offset to first byte to lock */
36331 unsigned long long length; /* nbr of bytes to lock */
36332 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
36333 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
36334 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
36335 int fd; /* file desc to assoc this lock with */
36336};
36337
36338#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
36339
36340/*
36341** This is a utility for setting or clearing a bit-range lock on an
36342** AFP filesystem.
36343**
36344** Return SQLITE_OK on success, SQLITE_BUSY on failure.
36345*/
36346static int afpSetLock(
36347 const char *path, /* Name of the file to be locked or unlocked */
36348 unixFile *pFile, /* Open file descriptor on path */
36349 unsigned long long offset, /* First byte to be locked */
36350 unsigned long long length, /* Number of bytes to lock */
36351 int setLockFlag /* True to set lock. False to clear lock */
36352){
36353 struct ByteRangeLockPB2 pb;
36354 int err;
36355
36356 pb.unLockFlag = setLockFlag ? 0 : 1;
36357 pb.startEndFlag = 0;
36358 pb.offset = offset;
36359 pb.length = length;
36360 pb.fd = pFile->h;
36361
36362 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
36363 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
36364 offset, length));
36365 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
36366 if ( err==-1 ) {
36367 int rc;
36368 int tErrno = errno;
36369 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
36370 path, tErrno, strerror(tErrno)));
36371#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
36372 rc = SQLITE_BUSY;
36373#else
36374 rc = sqliteErrorFromPosixError(tErrno,
36375 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
36376#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
36377 if( IS_LOCK_ERROR(rc) ){
36378 storeLastErrno(pFile, tErrno);
36379 }
36380 return rc;
36381 } else {
36382 return SQLITE_OK;
36383 }
36384}
36385
36386/*
36387** This routine checks if there is a RESERVED lock held on the specified
36388** file by this or any other process. If such a lock is held, set *pResOut
36389** to a non-zero value otherwise *pResOut is set to zero. The return value
36390** is set to SQLITE_OK unless an I/O error occurs during lock checking.
36391*/
36392static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
36393 int rc = SQLITE_OK;
36394 int reserved = 0;
36395 unixFile *pFile = (unixFile*)id;
36396 afpLockingContext *context;
36397
36398 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36399
36400 assert( pFile );
36401 context = (afpLockingContext *) pFile->lockingContext;
36402 if( context->reserved ){
36403 *pResOut = 1;
36404 return SQLITE_OK;
36405 }
36406 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
36407 /* Check if a thread in this process holds such a lock */
36408 if( pFile->pInode->eFileLock>SHARED_LOCK ){
36409 reserved = 1;
36410 }
36411
36412 /* Otherwise see if some other process holds it.
36413 */
36414 if( !reserved ){
36415 /* lock the RESERVED byte */
36416 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36417 if( SQLITE_OK==lrc ){
36418 /* if we succeeded in taking the reserved lock, unlock it to restore
36419 ** the original state */
36420 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
36421 } else {
36422 /* if we failed to get the lock then someone else must have it */
36423 reserved = 1;
36424 }
36425 if( IS_LOCK_ERROR(lrc) ){
36426 rc=lrc;
36427 }
36428 }
36429
36430 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
36431 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
36432
36433 *pResOut = reserved;
36434 return rc;
36435}
36436
36437/*
36438** Lock the file with the lock specified by parameter eFileLock - one
36439** of the following:
36440**
36441** (1) SHARED_LOCK
36442** (2) RESERVED_LOCK
36443** (3) PENDING_LOCK
36444** (4) EXCLUSIVE_LOCK
36445**
36446** Sometimes when requesting one lock state, additional lock states
36447** are inserted in between. The locking might fail on one of the later
36448** transitions leaving the lock state different from what it started but
36449** still short of its goal. The following chart shows the allowed
36450** transitions and the inserted intermediate states:
36451**
36452** UNLOCKED -> SHARED
36453** SHARED -> RESERVED
36454** SHARED -> (PENDING) -> EXCLUSIVE
36455** RESERVED -> (PENDING) -> EXCLUSIVE
36456** PENDING -> EXCLUSIVE
36457**
36458** This routine will only increase a lock. Use the sqlite3OsUnlock()
36459** routine to lower a locking level.
36460*/
36461static int afpLock(sqlite3_file *id, int eFileLock){
36462 int rc = SQLITE_OK;
36463 unixFile *pFile = (unixFile*)id;
36464 unixInodeInfo *pInode = pFile->pInode;
36465 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36466
36467 assert( pFile );
36468 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
36469 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
36470 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
36471
36472 /* If there is already a lock of this type or more restrictive on the
36473 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
36474 ** unixEnterMutex() hasn't been called yet.
36475 */
36476 if( pFile->eFileLock>=eFileLock ){
36477 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
36478 azFileLock(eFileLock)));
36479 return SQLITE_OK;
36480 }
36481
36482 /* Make sure the locking sequence is correct
36483 ** (1) We never move from unlocked to anything higher than shared lock.
36484 ** (2) SQLite never explicitly requests a pendig lock.
36485 ** (3) A shared lock is always held when a reserve lock is requested.
36486 */
36487 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
36488 assert( eFileLock!=PENDING_LOCK );
36489 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
36490
36491 /* This mutex is needed because pFile->pInode is shared across threads
36492 */
36493 pInode = pFile->pInode;
36494 sqlite3_mutex_enter(pInode->pLockMutex);
36495
36496 /* If some thread using this PID has a lock via a different unixFile*
36497 ** handle that precludes the requested lock, return BUSY.
36498 */
36499 if( (pFile->eFileLock!=pInode->eFileLock &&
36500 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
36501 ){
36502 rc = SQLITE_BUSY;
36503 goto afp_end_lock;
36504 }
36505
36506 /* If a SHARED lock is requested, and some thread using this PID already
36507 ** has a SHARED or RESERVED lock, then increment reference counts and
36508 ** return SQLITE_OK.
36509 */
36510 if( eFileLock==SHARED_LOCK &&
36511 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
36512 assert( eFileLock==SHARED_LOCK );
36513 assert( pFile->eFileLock==0 );
36514 assert( pInode->nShared>0 );
36515 pFile->eFileLock = SHARED_LOCK;
36516 pInode->nShared++;
36517 pInode->nLock++;
36518 goto afp_end_lock;
36519 }
36520
36521 /* A PENDING lock is needed before acquiring a SHARED lock and before
36522 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
36523 ** be released.
36524 */
36525 if( eFileLock==SHARED_LOCK
36526 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
36527 ){
36528 int failed;
36529 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
36530 if (failed) {
36531 rc = failed;
36532 goto afp_end_lock;
36533 }
36534 }
36535
36536 /* If control gets to this point, then actually go ahead and make
36537 ** operating system calls for the specified lock.
36538 */
36539 if( eFileLock==SHARED_LOCK ){
36540 int lrc1, lrc2, lrc1Errno = 0;
36541 long lk, mask;
36542
36543 assert( pInode->nShared==0 );
36544 assert( pInode->eFileLock==0 );
36545
36546 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
36547 /* Now get the read-lock SHARED_LOCK */
36548 /* note that the quality of the randomness doesn't matter that much */
36549 lk = random();
36550 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
36551 lrc1 = afpSetLock(context->dbPath, pFile,
36552 SHARED_FIRST+pInode->sharedByte, 1, 1);
36553 if( IS_LOCK_ERROR(lrc1) ){
36554 lrc1Errno = pFile->lastErrno;
36555 }
36556 /* Drop the temporary PENDING lock */
36557 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
36558
36559 if( IS_LOCK_ERROR(lrc1) ) {
36560 storeLastErrno(pFile, lrc1Errno);
36561 rc = lrc1;
36562 goto afp_end_lock;
36563 } else if( IS_LOCK_ERROR(lrc2) ){
36564 rc = lrc2;
36565 goto afp_end_lock;
36566 } else if( lrc1 != SQLITE_OK ) {
36567 rc = lrc1;
36568 } else {
36569 pFile->eFileLock = SHARED_LOCK;
36570 pInode->nLock++;
36571 pInode->nShared = 1;
36572 }
36573 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
36574 /* We are trying for an exclusive lock but another thread in this
36575 ** same process is still holding a shared lock. */
36576 rc = SQLITE_BUSY;
36577 }else{
36578 /* The request was for a RESERVED or EXCLUSIVE lock. It is
36579 ** assumed that there is a SHARED or greater lock on the file
36580 ** already.
36581 */
36582 int failed = 0;
36583 assert( 0!=pFile->eFileLock );
36584 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
36585 /* Acquire a RESERVED lock */
36586 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36587 if( !failed ){
36588 context->reserved = 1;
36589 }
36590 }
36591 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
36592 /* Acquire an EXCLUSIVE lock */
36593
36594 /* Remove the shared lock before trying the range. we'll need to
36595 ** reestablish the shared lock if we can't get the afpUnlock
36596 */
36597 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
36598 pInode->sharedByte, 1, 0)) ){
36599 int failed2 = SQLITE_OK;
36600 /* now attemmpt to get the exclusive lock range */
36601 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
36602 SHARED_SIZE, 1);
36603 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
36604 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
36605 /* Can't reestablish the shared lock. Sqlite can't deal, this is
36606 ** a critical I/O error
36607 */
36608 rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
36609 SQLITE_IOERR_LOCK;
36610 goto afp_end_lock;
36611 }
36612 }else{
36613 rc = failed;
36614 }
36615 }
36616 if( failed ){
36617 rc = failed;
36618 }
36619 }
36620
36621 if( rc==SQLITE_OK ){
36622 pFile->eFileLock = eFileLock;
36623 pInode->eFileLock = eFileLock;
36624 }else if( eFileLock==EXCLUSIVE_LOCK ){
36625 pFile->eFileLock = PENDING_LOCK;
36626 pInode->eFileLock = PENDING_LOCK;
36627 }
36628
36629afp_end_lock:
36630 sqlite3_mutex_leave(pInode->pLockMutex);
36631 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
36632 rc==SQLITE_OK ? "ok" : "failed"));
36633 return rc;
36634}
36635
36636/*
36637** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36638** must be either NO_LOCK or SHARED_LOCK.
36639**
36640** If the locking level of the file descriptor is already at or below
36641** the requested locking level, this routine is a no-op.
36642*/
36643static int afpUnlock(sqlite3_file *id, int eFileLock) {
36644 int rc = SQLITE_OK;
36645 unixFile *pFile = (unixFile*)id;
36646 unixInodeInfo *pInode;
36647 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36648 int skipShared = 0;
36649#ifdef SQLITE_TEST
36650 int h = pFile->h;
36651#endif
36652
36653 assert( pFile );
36654 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
36655 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
36656 osGetpid(0)));
36657
36658 assert( eFileLock<=SHARED_LOCK );
36659 if( pFile->eFileLock<=eFileLock ){
36660 return SQLITE_OK;
36661 }
36662 pInode = pFile->pInode;
36663 sqlite3_mutex_enter(pInode->pLockMutex);
36664 assert( pInode->nShared!=0 );
36665 if( pFile->eFileLock>SHARED_LOCK ){
36666 assert( pInode->eFileLock==pFile->eFileLock );
36667 SimulateIOErrorBenign(1);
36668 SimulateIOError( h=(-1) )
36669 SimulateIOErrorBenign(0);
36670
36671#ifdef SQLITE_DEBUG
36672 /* When reducing a lock such that other processes can start
36673 ** reading the database file again, make sure that the
36674 ** transaction counter was updated if any part of the database
36675 ** file changed. If the transaction counter is not updated,
36676 ** other connections to the same file might not realize that
36677 ** the file has changed and hence might not know to flush their
36678 ** cache. The use of a stale cache can lead to database corruption.
36679 */
36680 assert( pFile->inNormalWrite==0
36681 || pFile->dbUpdate==0
36682 || pFile->transCntrChng==1 );
36683 pFile->inNormalWrite = 0;
36684#endif
36685
36686 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
36687 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
36688 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
36689 /* only re-establish the shared lock if necessary */
36690 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
36691 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
36692 } else {
36693 skipShared = 1;
36694 }
36695 }
36696 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
36697 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
36698 }
36699 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
36700 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
36701 if( !rc ){
36702 context->reserved = 0;
36703 }
36704 }
36705 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
36706 pInode->eFileLock = SHARED_LOCK;
36707 }
36708 }
36709 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
36710
36711 /* Decrement the shared lock counter. Release the lock using an
36712 ** OS call only when all threads in this same process have released
36713 ** the lock.
36714 */
36715 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
36716 pInode->nShared--;
36717 if( pInode->nShared==0 ){
36718 SimulateIOErrorBenign(1);
36719 SimulateIOError( h=(-1) )
36720 SimulateIOErrorBenign(0);
36721 if( !skipShared ){
36722 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
36723 }
36724 if( !rc ){
36725 pInode->eFileLock = NO_LOCK;
36726 pFile->eFileLock = NO_LOCK;
36727 }
36728 }
36729 if( rc==SQLITE_OK ){
36730 pInode->nLock--;
36731 assert( pInode->nLock>=0 );
36732 if( pInode->nLock==0 ) closePendingFds(pFile);
36733 }
36734 }
36735
36736 sqlite3_mutex_leave(pInode->pLockMutex);
36737 if( rc==SQLITE_OK ){
36738 pFile->eFileLock = eFileLock;
36739 }
36740 return rc;
36741}
36742
36743/*
36744** Close a file & cleanup AFP specific locking context
36745*/
36746static int afpClose(sqlite3_file *id) {
36747 int rc = SQLITE_OK;
36748 unixFile *pFile = (unixFile*)id;
36749 assert( id!=0 );
36750 afpUnlock(id, NO_LOCK);
36751 assert( unixFileMutexNotheld(pFile) );
36752 unixEnterMutex();
36753 if( pFile->pInode ){
36754 unixInodeInfo *pInode = pFile->pInode;
36755 sqlite3_mutex_enter(pInode->pLockMutex);
36756 if( pInode->nLock ){
36757 /* If there are outstanding locks, do not actually close the file just
36758 ** yet because that would clear those locks. Instead, add the file
36759 ** descriptor to pInode->aPending. It will be automatically closed when
36760 ** the last lock is cleared.
36761 */
36762 setPendingFd(pFile);
36763 }
36764 sqlite3_mutex_leave(pInode->pLockMutex);
36765 }
36766 releaseInodeInfo(pFile);
36767 sqlite3_free(pFile->lockingContext);
36768 rc = closeUnixFile(id);
36769 unixLeaveMutex();
36770 return rc;
36771}
36772
36773#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
36774/*
36775** The code above is the AFP lock implementation. The code is specific
36776** to MacOSX and does not work on other unix platforms. No alternative
36777** is available. If you don't compile for a mac, then the "unix-afp"
36778** VFS is not available.
36779**
36780********************* End of the AFP lock implementation **********************
36781******************************************************************************/
36782
36783/******************************************************************************
36784*************************** Begin NFS Locking ********************************/
36785
36786#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
36787/*
36788 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
36789 ** must be either NO_LOCK or SHARED_LOCK.
36790 **
36791 ** If the locking level of the file descriptor is already at or below
36792 ** the requested locking level, this routine is a no-op.
36793 */
36794static int nfsUnlock(sqlite3_file *id, int eFileLock){
36795 return posixUnlock(id, eFileLock, 1);
36796}
36797
36798#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
36799/*
36800** The code above is the NFS lock implementation. The code is specific
36801** to MacOSX and does not work on other unix platforms. No alternative
36802** is available.
36803**
36804********************* End of the NFS lock implementation **********************
36805******************************************************************************/
36806
36807/******************************************************************************
36808**************** Non-locking sqlite3_file methods *****************************
36809**
36810** The next division contains implementations for all methods of the
36811** sqlite3_file object other than the locking methods. The locking
36812** methods were defined in divisions above (one locking method per
36813** division). Those methods that are common to all locking modes
36814** are gather together into this division.
36815*/
36816
36817/*
36818** Seek to the offset passed as the second argument, then read cnt
36819** bytes into pBuf. Return the number of bytes actually read.
36820**
36821** NB: If you define USE_PREAD or USE_PREAD64, then it might also
36822** be necessary to define _XOPEN_SOURCE to be 500. This varies from
36823** one system to another. Since SQLite does not define USE_PREAD
36824** in any form by default, we will not attempt to define _XOPEN_SOURCE.
36825** See tickets #2741 and #2681.
36826**
36827** To avoid stomping the errno value on a failed read the lastErrno value
36828** is set before returning.
36829*/
36830static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
36831 int got;
36832 int prior = 0;
36833#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
36834 i64 newOffset;
36835#endif
36836 TIMER_START;
36837 assert( cnt==(cnt&0x1ffff) );
36838 assert( id->h>2 );
36839 do{
36840#if defined(USE_PREAD)
36841 got = osPread(id->h, pBuf, cnt, offset);
36842 SimulateIOError( got = -1 );
36843#elif defined(USE_PREAD64)
36844 got = osPread64(id->h, pBuf, cnt, offset);
36845 SimulateIOError( got = -1 );
36846#else
36847 newOffset = lseek(id->h, offset, SEEK_SET);
36848 SimulateIOError( newOffset = -1 );
36849 if( newOffset<0 ){
36850 storeLastErrno((unixFile*)id, errno);
36851 return -1;
36852 }
36853 got = osRead(id->h, pBuf, cnt);
36854#endif
36855 if( got==cnt ) break;
36856 if( got<0 ){
36857 if( errno==EINTR ){ got = 1; continue; }
36858 prior = 0;
36859 storeLastErrno((unixFile*)id, errno);
36860 break;
36861 }else if( got>0 ){
36862 cnt -= got;
36863 offset += got;
36864 prior += got;
36865 pBuf = (void*)(got + (char*)pBuf);
36866 }
36867 }while( got>0 );
36868 TIMER_END;
36869 OSTRACE(("READ %-3d %5d %7lld %llu\n",
36870 id->h, got+prior, offset-prior, TIMER_ELAPSED));
36871 return got+prior;
36872}
36873
36874/*
36875** Read data from a file into a buffer. Return SQLITE_OK if all
36876** bytes were read successfully and SQLITE_IOERR if anything goes
36877** wrong.
36878*/
36879static int unixRead(
36880 sqlite3_file *id,
36881 void *pBuf,
36882 int amt,
36883 sqlite3_int64 offset
36884){
36885 unixFile *pFile = (unixFile *)id;
36886 int got;
36887 assert( id );
36888 assert( offset>=0 );
36889 assert( amt>0 );
36890
36891 /* If this is a database file (not a journal, super-journal or temp
36892 ** file), the bytes in the locking range should never be read or written. */
36893#if 0
36894 assert( pFile->pPreallocatedUnused==0
36895 || offset>=PENDING_BYTE+512
36896 || offset+amt<=PENDING_BYTE
36897 );
36898#endif
36899
36900#if SQLITE_MAX_MMAP_SIZE>0
36901 /* Deal with as much of this read request as possible by transfering
36902 ** data from the memory mapping using memcpy(). */
36903 if( offset<pFile->mmapSize ){
36904 if( offset+amt <= pFile->mmapSize ){
36905 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
36906 return SQLITE_OK;
36907 }else{
36908 int nCopy = pFile->mmapSize - offset;
36909 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
36910 pBuf = &((u8 *)pBuf)[nCopy];
36911 amt -= nCopy;
36912 offset += nCopy;
36913 }
36914 }
36915#endif
36916
36917 got = seekAndRead(pFile, offset, pBuf, amt);
36918 if( got==amt ){
36919 return SQLITE_OK;
36920 }else if( got<0 ){
36921 /* lastErrno set by seekAndRead */
36922 return SQLITE_IOERR_READ;
36923 }else{
36924 storeLastErrno(pFile, 0); /* not a system error */
36925 /* Unread parts of the buffer must be zero-filled */
36926 memset(&((char*)pBuf)[got], 0, amt-got);
36927 return SQLITE_IOERR_SHORT_READ;
36928 }
36929}
36930
36931/*
36932** Attempt to seek the file-descriptor passed as the first argument to
36933** absolute offset iOff, then attempt to write nBuf bytes of data from
36934** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
36935** return the actual number of bytes written (which may be less than
36936** nBuf).
36937*/
36938static int seekAndWriteFd(
36939 int fd, /* File descriptor to write to */
36940 i64 iOff, /* File offset to begin writing at */
36941 const void *pBuf, /* Copy data from this buffer to the file */
36942 int nBuf, /* Size of buffer pBuf in bytes */
36943 int *piErrno /* OUT: Error number if error occurs */
36944){
36945 int rc = 0; /* Value returned by system call */
36946
36947 assert( nBuf==(nBuf&0x1ffff) );
36948 assert( fd>2 );
36949 assert( piErrno!=0 );
36950 nBuf &= 0x1ffff;
36951 TIMER_START;
36952
36953#if defined(USE_PREAD)
36954 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
36955#elif defined(USE_PREAD64)
36956 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
36957#else
36958 do{
36959 i64 iSeek = lseek(fd, iOff, SEEK_SET);
36960 SimulateIOError( iSeek = -1 );
36961 if( iSeek<0 ){
36962 rc = -1;
36963 break;
36964 }
36965 rc = osWrite(fd, pBuf, nBuf);
36966 }while( rc<0 && errno==EINTR );
36967#endif
36968
36969 TIMER_END;
36970 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
36971
36972 if( rc<0 ) *piErrno = errno;
36973 return rc;
36974}
36975
36976
36977/*
36978** Seek to the offset in id->offset then read cnt bytes into pBuf.
36979** Return the number of bytes actually read. Update the offset.
36980**
36981** To avoid stomping the errno value on a failed write the lastErrno value
36982** is set before returning.
36983*/
36984static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
36985 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
36986}
36987
36988
36989/*
36990** Write data from a buffer into a file. Return SQLITE_OK on success
36991** or some other error code on failure.
36992*/
36993static int unixWrite(
36994 sqlite3_file *id,
36995 const void *pBuf,
36996 int amt,
36997 sqlite3_int64 offset
36998){
36999 unixFile *pFile = (unixFile*)id;
37000 int wrote = 0;
37001 assert( id );
37002 assert( amt>0 );
37003
37004 /* If this is a database file (not a journal, super-journal or temp
37005 ** file), the bytes in the locking range should never be read or written. */
37006#if 0
37007 assert( pFile->pPreallocatedUnused==0
37008 || offset>=PENDING_BYTE+512
37009 || offset+amt<=PENDING_BYTE
37010 );
37011#endif
37012
37013#ifdef SQLITE_DEBUG
37014 /* If we are doing a normal write to a database file (as opposed to
37015 ** doing a hot-journal rollback or a write to some file other than a
37016 ** normal database file) then record the fact that the database
37017 ** has changed. If the transaction counter is modified, record that
37018 ** fact too.
37019 */
37020 if( pFile->inNormalWrite ){
37021 pFile->dbUpdate = 1; /* The database has been modified */
37022 if( offset<=24 && offset+amt>=27 ){
37023 int rc;
37024 char oldCntr[4];
37025 SimulateIOErrorBenign(1);
37026 rc = seekAndRead(pFile, 24, oldCntr, 4);
37027 SimulateIOErrorBenign(0);
37028 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
37029 pFile->transCntrChng = 1; /* The transaction counter has changed */
37030 }
37031 }
37032 }
37033#endif
37034
37035#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
37036 /* Deal with as much of this write request as possible by transfering
37037 ** data from the memory mapping using memcpy(). */
37038 if( offset<pFile->mmapSize ){
37039 if( offset+amt <= pFile->mmapSize ){
37040 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
37041 return SQLITE_OK;
37042 }else{
37043 int nCopy = pFile->mmapSize - offset;
37044 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
37045 pBuf = &((u8 *)pBuf)[nCopy];
37046 amt -= nCopy;
37047 offset += nCopy;
37048 }
37049 }
37050#endif
37051
37052 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
37053 amt -= wrote;
37054 offset += wrote;
37055 pBuf = &((char*)pBuf)[wrote];
37056 }
37057 SimulateIOError(( wrote=(-1), amt=1 ));
37058 SimulateDiskfullError(( wrote=0, amt=1 ));
37059
37060 if( amt>wrote ){
37061 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
37062 /* lastErrno set by seekAndWrite */
37063 return SQLITE_IOERR_WRITE;
37064 }else{
37065 storeLastErrno(pFile, 0); /* not a system error */
37066 return SQLITE_FULL;
37067 }
37068 }
37069
37070 return SQLITE_OK;
37071}
37072
37073#ifdef SQLITE_TEST
37074/*
37075** Count the number of fullsyncs and normal syncs. This is used to test
37076** that syncs and fullsyncs are occurring at the right times.
37077*/
37078SQLITE_API int sqlite3_sync_count = 0;
37079SQLITE_API int sqlite3_fullsync_count = 0;
37080#endif
37081
37082/*
37083** We do not trust systems to provide a working fdatasync(). Some do.
37084** Others do no. To be safe, we will stick with the (slightly slower)
37085** fsync(). If you know that your system does support fdatasync() correctly,
37086** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
37087*/
37088#if !defined(fdatasync) && !HAVE_FDATASYNC
37089# define fdatasync fsync
37090#endif
37091
37092/*
37093** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
37094** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
37095** only available on Mac OS X. But that could change.
37096*/
37097#ifdef F_FULLFSYNC
37098# define HAVE_FULLFSYNC 1
37099#else
37100# define HAVE_FULLFSYNC 0
37101#endif
37102
37103
37104/*
37105** The fsync() system call does not work as advertised on many
37106** unix systems. The following procedure is an attempt to make
37107** it work better.
37108**
37109** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
37110** for testing when we want to run through the test suite quickly.
37111** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
37112** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
37113** or power failure will likely corrupt the database file.
37114**
37115** SQLite sets the dataOnly flag if the size of the file is unchanged.
37116** The idea behind dataOnly is that it should only write the file content
37117** to disk, not the inode. We only set dataOnly if the file size is
37118** unchanged since the file size is part of the inode. However,
37119** Ted Ts'o tells us that fdatasync() will also write the inode if the
37120** file size has changed. The only real difference between fdatasync()
37121** and fsync(), Ted tells us, is that fdatasync() will not flush the
37122** inode if the mtime or owner or other inode attributes have changed.
37123** We only care about the file size, not the other file attributes, so
37124** as far as SQLite is concerned, an fdatasync() is always adequate.
37125** So, we always use fdatasync() if it is available, regardless of
37126** the value of the dataOnly flag.
37127*/
37128static int full_fsync(int fd, int fullSync, int dataOnly){
37129 int rc;
37130
37131 /* The following "ifdef/elif/else/" block has the same structure as
37132 ** the one below. It is replicated here solely to avoid cluttering
37133 ** up the real code with the UNUSED_PARAMETER() macros.
37134 */
37135#ifdef SQLITE_NO_SYNC
37136 UNUSED_PARAMETER(fd);
37137 UNUSED_PARAMETER(fullSync);
37138 UNUSED_PARAMETER(dataOnly);
37139#elif HAVE_FULLFSYNC
37140 UNUSED_PARAMETER(dataOnly);
37141#else
37142 UNUSED_PARAMETER(fullSync);
37143 UNUSED_PARAMETER(dataOnly);
37144#endif
37145
37146 /* Record the number of times that we do a normal fsync() and
37147 ** FULLSYNC. This is used during testing to verify that this procedure
37148 ** gets called with the correct arguments.
37149 */
37150#ifdef SQLITE_TEST
37151 if( fullSync ) sqlite3_fullsync_count++;
37152 sqlite3_sync_count++;
37153#endif
37154
37155 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
37156 ** no-op. But go ahead and call fstat() to validate the file
37157 ** descriptor as we need a method to provoke a failure during
37158 ** coverate testing.
37159 */
37160#ifdef SQLITE_NO_SYNC
37161 {
37162 struct stat buf;
37163 rc = osFstat(fd, &buf);
37164 }
37165#elif HAVE_FULLFSYNC
37166 if( fullSync ){
37167 rc = osFcntl(fd, F_FULLFSYNC, 0);
37168 }else{
37169 rc = 1;
37170 }
37171 /* If the FULLFSYNC failed, fall back to attempting an fsync().
37172 ** It shouldn't be possible for fullfsync to fail on the local
37173 ** file system (on OSX), so failure indicates that FULLFSYNC
37174 ** isn't supported for this file system. So, attempt an fsync
37175 ** and (for now) ignore the overhead of a superfluous fcntl call.
37176 ** It'd be better to detect fullfsync support once and avoid
37177 ** the fcntl call every time sync is called.
37178 */
37179 if( rc ) rc = fsync(fd);
37180
37181#elif defined(__APPLE__)
37182 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
37183 ** so currently we default to the macro that redefines fdatasync to fsync
37184 */
37185 rc = fsync(fd);
37186#else
37187 rc = fdatasync(fd);
37188#if OS_VXWORKS
37189 if( rc==-1 && errno==ENOTSUP ){
37190 rc = fsync(fd);
37191 }
37192#endif /* OS_VXWORKS */
37193#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
37194
37195 if( OS_VXWORKS && rc!= -1 ){
37196 rc = 0;
37197 }
37198 return rc;
37199}
37200
37201/*
37202** Open a file descriptor to the directory containing file zFilename.
37203** If successful, *pFd is set to the opened file descriptor and
37204** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
37205** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
37206** value.
37207**
37208** The directory file descriptor is used for only one thing - to
37209** fsync() a directory to make sure file creation and deletion events
37210** are flushed to disk. Such fsyncs are not needed on newer
37211** journaling filesystems, but are required on older filesystems.
37212**
37213** This routine can be overridden using the xSetSysCall interface.
37214** The ability to override this routine was added in support of the
37215** chromium sandbox. Opening a directory is a security risk (we are
37216** told) so making it overrideable allows the chromium sandbox to
37217** replace this routine with a harmless no-op. To make this routine
37218** a no-op, replace it with a stub that returns SQLITE_OK but leaves
37219** *pFd set to a negative number.
37220**
37221** If SQLITE_OK is returned, the caller is responsible for closing
37222** the file descriptor *pFd using close().
37223*/
37224static int openDirectory(const char *zFilename, int *pFd){
37225 int ii;
37226 int fd = -1;
37227 char zDirname[MAX_PATHNAME+1];
37228
37229 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
37230 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
37231 if( ii>0 ){
37232 zDirname[ii] = '\0';
37233 }else{
37234 if( zDirname[0]!='/' ) zDirname[0] = '.';
37235 zDirname[1] = 0;
37236 }
37237 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
37238 if( fd>=0 ){
37239 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
37240 }
37241 *pFd = fd;
37242 if( fd>=0 ) return SQLITE_OK;
37243 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
37244}
37245
37246/*
37247** Make sure all writes to a particular file are committed to disk.
37248**
37249** If dataOnly==0 then both the file itself and its metadata (file
37250** size, access time, etc) are synced. If dataOnly!=0 then only the
37251** file data is synced.
37252**
37253** Under Unix, also make sure that the directory entry for the file
37254** has been created by fsync-ing the directory that contains the file.
37255** If we do not do this and we encounter a power failure, the directory
37256** entry for the journal might not exist after we reboot. The next
37257** SQLite to access the file will not know that the journal exists (because
37258** the directory entry for the journal was never created) and the transaction
37259** will not roll back - possibly leading to database corruption.
37260*/
37261static int unixSync(sqlite3_file *id, int flags){
37262 int rc;
37263 unixFile *pFile = (unixFile*)id;
37264
37265 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
37266 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
37267
37268 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
37269 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
37270 || (flags&0x0F)==SQLITE_SYNC_FULL
37271 );
37272
37273 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
37274 ** line is to test that doing so does not cause any problems.
37275 */
37276 SimulateDiskfullError( return SQLITE_FULL );
37277
37278 assert( pFile );
37279 OSTRACE(("SYNC %-3d\n", pFile->h));
37280 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
37281 SimulateIOError( rc=1 );
37282 if( rc ){
37283 storeLastErrno(pFile, errno);
37284 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
37285 }
37286
37287 /* Also fsync the directory containing the file if the DIRSYNC flag
37288 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
37289 ** are unable to fsync a directory, so ignore errors on the fsync.
37290 */
37291 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
37292 int dirfd;
37293 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
37294 HAVE_FULLFSYNC, isFullsync));
37295 rc = osOpenDirectory(pFile->zPath, &dirfd);
37296 if( rc==SQLITE_OK ){
37297 full_fsync(dirfd, 0, 0);
37298 robust_close(pFile, dirfd, __LINE__);
37299 }else{
37300 assert( rc==SQLITE_CANTOPEN );
37301 rc = SQLITE_OK;
37302 }
37303 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
37304 }
37305 return rc;
37306}
37307
37308/*
37309** Truncate an open file to a specified size
37310*/
37311static int unixTruncate(sqlite3_file *id, i64 nByte){
37312 unixFile *pFile = (unixFile *)id;
37313 int rc;
37314 assert( pFile );
37315 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
37316
37317 /* If the user has configured a chunk-size for this file, truncate the
37318 ** file so that it consists of an integer number of chunks (i.e. the
37319 ** actual file size after the operation may be larger than the requested
37320 ** size).
37321 */
37322 if( pFile->szChunk>0 ){
37323 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
37324 }
37325
37326 rc = robust_ftruncate(pFile->h, nByte);
37327 if( rc ){
37328 storeLastErrno(pFile, errno);
37329 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37330 }else{
37331#ifdef SQLITE_DEBUG
37332 /* If we are doing a normal write to a database file (as opposed to
37333 ** doing a hot-journal rollback or a write to some file other than a
37334 ** normal database file) and we truncate the file to zero length,
37335 ** that effectively updates the change counter. This might happen
37336 ** when restoring a database using the backup API from a zero-length
37337 ** source.
37338 */
37339 if( pFile->inNormalWrite && nByte==0 ){
37340 pFile->transCntrChng = 1;
37341 }
37342#endif
37343
37344#if SQLITE_MAX_MMAP_SIZE>0
37345 /* If the file was just truncated to a size smaller than the currently
37346 ** mapped region, reduce the effective mapping size as well. SQLite will
37347 ** use read() and write() to access data beyond this point from now on.
37348 */
37349 if( nByte<pFile->mmapSize ){
37350 pFile->mmapSize = nByte;
37351 }
37352#endif
37353
37354 return SQLITE_OK;
37355 }
37356}
37357
37358/*
37359** Determine the current size of a file in bytes
37360*/
37361static int unixFileSize(sqlite3_file *id, i64 *pSize){
37362 int rc;
37363 struct stat buf;
37364 assert( id );
37365 rc = osFstat(((unixFile*)id)->h, &buf);
37366 SimulateIOError( rc=1 );
37367 if( rc!=0 ){
37368 storeLastErrno((unixFile*)id, errno);
37369 return SQLITE_IOERR_FSTAT;
37370 }
37371 *pSize = buf.st_size;
37372
37373 /* When opening a zero-size database, the findInodeInfo() procedure
37374 ** writes a single byte into that file in order to work around a bug
37375 ** in the OS-X msdos filesystem. In order to avoid problems with upper
37376 ** layers, we need to report this file size as zero even though it is
37377 ** really 1. Ticket #3260.
37378 */
37379 if( *pSize==1 ) *pSize = 0;
37380
37381
37382 return SQLITE_OK;
37383}
37384
37385#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37386/*
37387** Handler for proxy-locking file-control verbs. Defined below in the
37388** proxying locking division.
37389*/
37390static int proxyFileControl(sqlite3_file*,int,void*);
37391#endif
37392
37393/*
37394** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
37395** file-control operation. Enlarge the database to nBytes in size
37396** (rounded up to the next chunk-size). If the database is already
37397** nBytes or larger, this routine is a no-op.
37398*/
37399static int fcntlSizeHint(unixFile *pFile, i64 nByte){
37400 if( pFile->szChunk>0 ){
37401 i64 nSize; /* Required file size */
37402 struct stat buf; /* Used to hold return values of fstat() */
37403
37404 if( osFstat(pFile->h, &buf) ){
37405 return SQLITE_IOERR_FSTAT;
37406 }
37407
37408 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
37409 if( nSize>(i64)buf.st_size ){
37410
37411#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
37412 /* The code below is handling the return value of osFallocate()
37413 ** correctly. posix_fallocate() is defined to "returns zero on success,
37414 ** or an error number on failure". See the manpage for details. */
37415 int err;
37416 do{
37417 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
37418 }while( err==EINTR );
37419 if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
37420#else
37421 /* If the OS does not have posix_fallocate(), fake it. Write a
37422 ** single byte to the last byte in each block that falls entirely
37423 ** within the extended region. Then, if required, a single byte
37424 ** at offset (nSize-1), to set the size of the file correctly.
37425 ** This is a similar technique to that used by glibc on systems
37426 ** that do not have a real fallocate() call.
37427 */
37428 int nBlk = buf.st_blksize; /* File-system block size */
37429 int nWrite = 0; /* Number of bytes written by seekAndWrite */
37430 i64 iWrite; /* Next offset to write to */
37431
37432 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
37433 assert( iWrite>=buf.st_size );
37434 assert( ((iWrite+1)%nBlk)==0 );
37435 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
37436 if( iWrite>=nSize ) iWrite = nSize - 1;
37437 nWrite = seekAndWrite(pFile, iWrite, "", 1);
37438 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
37439 }
37440#endif
37441 }
37442 }
37443
37444#if SQLITE_MAX_MMAP_SIZE>0
37445 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
37446 int rc;
37447 if( pFile->szChunk<=0 ){
37448 if( robust_ftruncate(pFile->h, nByte) ){
37449 storeLastErrno(pFile, errno);
37450 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37451 }
37452 }
37453
37454 rc = unixMapfile(pFile, nByte);
37455 return rc;
37456 }
37457#endif
37458
37459 return SQLITE_OK;
37460}
37461
37462/*
37463** If *pArg is initially negative then this is a query. Set *pArg to
37464** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
37465**
37466** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
37467*/
37468static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
37469 if( *pArg<0 ){
37470 *pArg = (pFile->ctrlFlags & mask)!=0;
37471 }else if( (*pArg)==0 ){
37472 pFile->ctrlFlags &= ~mask;
37473 }else{
37474 pFile->ctrlFlags |= mask;
37475 }
37476}
37477
37478/* Forward declaration */
37479static int unixGetTempname(int nBuf, char *zBuf);
37480
37481/*
37482** Information and control of an open file handle.
37483*/
37484static int unixFileControl(sqlite3_file *id, int op, void *pArg){
37485 unixFile *pFile = (unixFile*)id;
37486 switch( op ){
37487#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
37488 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
37489 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
37490 return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
37491 }
37492 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
37493 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
37494 return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
37495 }
37496 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
37497 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
37498 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
37499 }
37500#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
37501
37502 case SQLITE_FCNTL_LOCKSTATE: {
37503 *(int*)pArg = pFile->eFileLock;
37504 return SQLITE_OK;
37505 }
37506 case SQLITE_FCNTL_LAST_ERRNO: {
37507 *(int*)pArg = pFile->lastErrno;
37508 return SQLITE_OK;
37509 }
37510 case SQLITE_FCNTL_CHUNK_SIZE: {
37511 pFile->szChunk = *(int *)pArg;
37512 return SQLITE_OK;
37513 }
37514 case SQLITE_FCNTL_SIZE_HINT: {
37515 int rc;
37516 SimulateIOErrorBenign(1);
37517 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
37518 SimulateIOErrorBenign(0);
37519 return rc;
37520 }
37521 case SQLITE_FCNTL_PERSIST_WAL: {
37522 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
37523 return SQLITE_OK;
37524 }
37525 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
37526 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
37527 return SQLITE_OK;
37528 }
37529 case SQLITE_FCNTL_VFSNAME: {
37530 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
37531 return SQLITE_OK;
37532 }
37533 case SQLITE_FCNTL_TEMPFILENAME: {
37534 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
37535 if( zTFile ){
37536 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
37537 *(char**)pArg = zTFile;
37538 }
37539 return SQLITE_OK;
37540 }
37541 case SQLITE_FCNTL_HAS_MOVED: {
37542 *(int*)pArg = fileHasMoved(pFile);
37543 return SQLITE_OK;
37544 }
37545#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
37546 case SQLITE_FCNTL_LOCK_TIMEOUT: {
37547 int iOld = pFile->iBusyTimeout;
37548 pFile->iBusyTimeout = *(int*)pArg;
37549 *(int*)pArg = iOld;
37550 return SQLITE_OK;
37551 }
37552#endif
37553#if SQLITE_MAX_MMAP_SIZE>0
37554 case SQLITE_FCNTL_MMAP_SIZE: {
37555 i64 newLimit = *(i64*)pArg;
37556 int rc = SQLITE_OK;
37557 if( newLimit>sqlite3GlobalConfig.mxMmap ){
37558 newLimit = sqlite3GlobalConfig.mxMmap;
37559 }
37560
37561 /* The value of newLimit may be eventually cast to (size_t) and passed
37562 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
37563 ** 64-bit type. */
37564 if( newLimit>0 && sizeof(size_t)<8 ){
37565 newLimit = (newLimit & 0x7FFFFFFF);
37566 }
37567
37568 *(i64*)pArg = pFile->mmapSizeMax;
37569 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
37570 pFile->mmapSizeMax = newLimit;
37571 if( pFile->mmapSize>0 ){
37572 unixUnmapfile(pFile);
37573 rc = unixMapfile(pFile, -1);
37574 }
37575 }
37576 return rc;
37577 }
37578#endif
37579#ifdef SQLITE_DEBUG
37580 /* The pager calls this method to signal that it has done
37581 ** a rollback and that the database is therefore unchanged and
37582 ** it hence it is OK for the transaction change counter to be
37583 ** unchanged.
37584 */
37585 case SQLITE_FCNTL_DB_UNCHANGED: {
37586 ((unixFile*)id)->dbUpdate = 0;
37587 return SQLITE_OK;
37588 }
37589#endif
37590#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37591 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
37592 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
37593 return proxyFileControl(id,op,pArg);
37594 }
37595#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
37596 }
37597 return SQLITE_NOTFOUND;
37598}
37599
37600/*
37601** If pFd->sectorSize is non-zero when this function is called, it is a
37602** no-op. Otherwise, the values of pFd->sectorSize and
37603** pFd->deviceCharacteristics are set according to the file-system
37604** characteristics.
37605**
37606** There are two versions of this function. One for QNX and one for all
37607** other systems.
37608*/
37609#ifndef __QNXNTO__
37610static void setDeviceCharacteristics(unixFile *pFd){
37611 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
37612 if( pFd->sectorSize==0 ){
37613#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
37614 int res;
37615 u32 f = 0;
37616
37617 /* Check for support for F2FS atomic batch writes. */
37618 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
37619 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
37620 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
37621 }
37622#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
37623
37624 /* Set the POWERSAFE_OVERWRITE flag if requested. */
37625 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
37626 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
37627 }
37628
37629 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37630 }
37631}
37632#else
37633#include <sys/dcmd_blk.h>
37634#include <sys/statvfs.h>
37635static void setDeviceCharacteristics(unixFile *pFile){
37636 if( pFile->sectorSize == 0 ){
37637 struct statvfs fsInfo;
37638
37639 /* Set defaults for non-supported filesystems */
37640 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37641 pFile->deviceCharacteristics = 0;
37642 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
37643 return;
37644 }
37645
37646 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
37647 pFile->sectorSize = fsInfo.f_bsize;
37648 pFile->deviceCharacteristics =
37649 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
37650 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
37651 ** the write succeeds */
37652 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
37653 ** so it is ordered */
37654 0;
37655 }else if( strstr(fsInfo.f_basetype, "etfs") ){
37656 pFile->sectorSize = fsInfo.f_bsize;
37657 pFile->deviceCharacteristics =
37658 /* etfs cluster size writes are atomic */
37659 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
37660 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
37661 ** the write succeeds */
37662 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
37663 ** so it is ordered */
37664 0;
37665 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
37666 pFile->sectorSize = fsInfo.f_bsize;
37667 pFile->deviceCharacteristics =
37668 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
37669 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
37670 ** the write succeeds */
37671 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
37672 ** so it is ordered */
37673 0;
37674 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
37675 pFile->sectorSize = fsInfo.f_bsize;
37676 pFile->deviceCharacteristics =
37677 /* full bitset of atomics from max sector size and smaller */
37678 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
37679 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
37680 ** so it is ordered */
37681 0;
37682 }else if( strstr(fsInfo.f_basetype, "dos") ){
37683 pFile->sectorSize = fsInfo.f_bsize;
37684 pFile->deviceCharacteristics =
37685 /* full bitset of atomics from max sector size and smaller */
37686 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
37687 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
37688 ** so it is ordered */
37689 0;
37690 }else{
37691 pFile->deviceCharacteristics =
37692 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
37693 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
37694 ** the write succeeds */
37695 0;
37696 }
37697 }
37698 /* Last chance verification. If the sector size isn't a multiple of 512
37699 ** then it isn't valid.*/
37700 if( pFile->sectorSize % 512 != 0 ){
37701 pFile->deviceCharacteristics = 0;
37702 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37703 }
37704}
37705#endif
37706
37707/*
37708** Return the sector size in bytes of the underlying block device for
37709** the specified file. This is almost always 512 bytes, but may be
37710** larger for some devices.
37711**
37712** SQLite code assumes this function cannot fail. It also assumes that
37713** if two files are created in the same file-system directory (i.e.
37714** a database and its journal file) that the sector size will be the
37715** same for both.
37716*/
37717static int unixSectorSize(sqlite3_file *id){
37718 unixFile *pFd = (unixFile*)id;
37719 setDeviceCharacteristics(pFd);
37720 return pFd->sectorSize;
37721}
37722
37723/*
37724** Return the device characteristics for the file.
37725**
37726** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
37727** However, that choice is controversial since technically the underlying
37728** file system does not always provide powersafe overwrites. (In other
37729** words, after a power-loss event, parts of the file that were never
37730** written might end up being altered.) However, non-PSOW behavior is very,
37731** very rare. And asserting PSOW makes a large reduction in the amount
37732** of required I/O for journaling, since a lot of padding is eliminated.
37733** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
37734** available to turn it off and URI query parameter available to turn it off.
37735*/
37736static int unixDeviceCharacteristics(sqlite3_file *id){
37737 unixFile *pFd = (unixFile*)id;
37738 setDeviceCharacteristics(pFd);
37739 return pFd->deviceCharacteristics;
37740}
37741
37742#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
37743
37744/*
37745** Return the system page size.
37746**
37747** This function should not be called directly by other code in this file.
37748** Instead, it should be called via macro osGetpagesize().
37749*/
37750static int unixGetpagesize(void){
37751#if OS_VXWORKS
37752 return 1024;
37753#elif defined(_BSD_SOURCE)
37754 return getpagesize();
37755#else
37756 return (int)sysconf(_SC_PAGESIZE);
37757#endif
37758}
37759
37760#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
37761
37762#ifndef SQLITE_OMIT_WAL
37763
37764/*
37765** Object used to represent an shared memory buffer.
37766**
37767** When multiple threads all reference the same wal-index, each thread
37768** has its own unixShm object, but they all point to a single instance
37769** of this unixShmNode object. In other words, each wal-index is opened
37770** only once per process.
37771**
37772** Each unixShmNode object is connected to a single unixInodeInfo object.
37773** We could coalesce this object into unixInodeInfo, but that would mean
37774** every open file that does not use shared memory (in other words, most
37775** open files) would have to carry around this extra information. So
37776** the unixInodeInfo object contains a pointer to this unixShmNode object
37777** and the unixShmNode object is created only when needed.
37778**
37779** unixMutexHeld() must be true when creating or destroying
37780** this object or while reading or writing the following fields:
37781**
37782** nRef
37783**
37784** The following fields are read-only after the object is created:
37785**
37786** hShm
37787** zFilename
37788**
37789** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
37790** unixMutexHeld() is true when reading or writing any other field
37791** in this structure.
37792*/
37793struct unixShmNode {
37794 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
37795 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
37796 char *zFilename; /* Name of the mmapped file */
37797 int hShm; /* Open file descriptor */
37798 int szRegion; /* Size of shared-memory regions */
37799 u16 nRegion; /* Size of array apRegion */
37800 u8 isReadonly; /* True if read-only */
37801 u8 isUnlocked; /* True if no DMS lock held */
37802 char **apRegion; /* Array of mapped shared-memory regions */
37803 int nRef; /* Number of unixShm objects pointing to this */
37804 unixShm *pFirst; /* All unixShm objects pointing to this */
37805#ifdef SQLITE_DEBUG
37806 u8 exclMask; /* Mask of exclusive locks held */
37807 u8 sharedMask; /* Mask of shared locks held */
37808 u8 nextShmId; /* Next available unixShm.id value */
37809#endif
37810};
37811
37812/*
37813** Structure used internally by this VFS to record the state of an
37814** open shared memory connection.
37815**
37816** The following fields are initialized when this object is created and
37817** are read-only thereafter:
37818**
37819** unixShm.pShmNode
37820** unixShm.id
37821**
37822** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
37823** be held while accessing any read/write fields.
37824*/
37825struct unixShm {
37826 unixShmNode *pShmNode; /* The underlying unixShmNode object */
37827 unixShm *pNext; /* Next unixShm with the same unixShmNode */
37828 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
37829 u8 id; /* Id of this connection within its unixShmNode */
37830 u16 sharedMask; /* Mask of shared locks held */
37831 u16 exclMask; /* Mask of exclusive locks held */
37832};
37833
37834/*
37835** Constants used for locking
37836*/
37837#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
37838#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
37839
37840/*
37841** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
37842**
37843** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
37844** otherwise.
37845*/
37846static int unixShmSystemLock(
37847 unixFile *pFile, /* Open connection to the WAL file */
37848 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
37849 int ofst, /* First byte of the locking range */
37850 int n /* Number of bytes to lock */
37851){
37852 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
37853 struct flock f; /* The posix advisory locking structure */
37854 int rc = SQLITE_OK; /* Result code form fcntl() */
37855
37856 /* Access to the unixShmNode object is serialized by the caller */
37857 pShmNode = pFile->pInode->pShmNode;
37858 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
37859 assert( pShmNode->nRef>0 || unixMutexHeld() );
37860
37861 /* Shared locks never span more than one byte */
37862 assert( n==1 || lockType!=F_RDLCK );
37863
37864 /* Locks are within range */
37865 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
37866
37867 if( pShmNode->hShm>=0 ){
37868 int res;
37869 /* Initialize the locking parameters */
37870 f.l_type = lockType;
37871 f.l_whence = SEEK_SET;
37872 f.l_start = ofst;
37873 f.l_len = n;
37874 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
37875 if( res==-1 ){
37876#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
37877 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
37878#else
37879 rc = SQLITE_BUSY;
37880#endif
37881 }
37882 }
37883
37884 /* Update the global lock state and do debug tracing */
37885#ifdef SQLITE_DEBUG
37886 { u16 mask;
37887 OSTRACE(("SHM-LOCK "));
37888 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
37889 if( rc==SQLITE_OK ){
37890 if( lockType==F_UNLCK ){
37891 OSTRACE(("unlock %d ok", ofst));
37892 pShmNode->exclMask &= ~mask;
37893 pShmNode->sharedMask &= ~mask;
37894 }else if( lockType==F_RDLCK ){
37895 OSTRACE(("read-lock %d ok", ofst));
37896 pShmNode->exclMask &= ~mask;
37897 pShmNode->sharedMask |= mask;
37898 }else{
37899 assert( lockType==F_WRLCK );
37900 OSTRACE(("write-lock %d ok", ofst));
37901 pShmNode->exclMask |= mask;
37902 pShmNode->sharedMask &= ~mask;
37903 }
37904 }else{
37905 if( lockType==F_UNLCK ){
37906 OSTRACE(("unlock %d failed", ofst));
37907 }else if( lockType==F_RDLCK ){
37908 OSTRACE(("read-lock failed"));
37909 }else{
37910 assert( lockType==F_WRLCK );
37911 OSTRACE(("write-lock %d failed", ofst));
37912 }
37913 }
37914 OSTRACE((" - afterwards %03x,%03x\n",
37915 pShmNode->sharedMask, pShmNode->exclMask));
37916 }
37917#endif
37918
37919 return rc;
37920}
37921
37922/*
37923** Return the minimum number of 32KB shm regions that should be mapped at
37924** a time, assuming that each mapping must be an integer multiple of the
37925** current system page-size.
37926**
37927** Usually, this is 1. The exception seems to be systems that are configured
37928** to use 64KB pages - in this case each mapping must cover at least two
37929** shm regions.
37930*/
37931static int unixShmRegionPerMap(void){
37932 int shmsz = 32*1024; /* SHM region size */
37933 int pgsz = osGetpagesize(); /* System page size */
37934 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
37935 if( pgsz<shmsz ) return 1;
37936 return pgsz/shmsz;
37937}
37938
37939/*
37940** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
37941**
37942** This is not a VFS shared-memory method; it is a utility function called
37943** by VFS shared-memory methods.
37944*/
37945static void unixShmPurge(unixFile *pFd){
37946 unixShmNode *p = pFd->pInode->pShmNode;
37947 assert( unixMutexHeld() );
37948 if( p && ALWAYS(p->nRef==0) ){
37949 int nShmPerMap = unixShmRegionPerMap();
37950 int i;
37951 assert( p->pInode==pFd->pInode );
37952 sqlite3_mutex_free(p->pShmMutex);
37953 for(i=0; i<p->nRegion; i+=nShmPerMap){
37954 if( p->hShm>=0 ){
37955 osMunmap(p->apRegion[i], p->szRegion);
37956 }else{
37957 sqlite3_free(p->apRegion[i]);
37958 }
37959 }
37960 sqlite3_free(p->apRegion);
37961 if( p->hShm>=0 ){
37962 robust_close(pFd, p->hShm, __LINE__);
37963 p->hShm = -1;
37964 }
37965 p->pInode->pShmNode = 0;
37966 sqlite3_free(p);
37967 }
37968}
37969
37970/*
37971** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
37972** take it now. Return SQLITE_OK if successful, or an SQLite error
37973** code otherwise.
37974**
37975** If the DMS cannot be locked because this is a readonly_shm=1
37976** connection and no other process already holds a lock, return
37977** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
37978*/
37979static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
37980 struct flock lock;
37981 int rc = SQLITE_OK;
37982
37983 /* Use F_GETLK to determine the locks other processes are holding
37984 ** on the DMS byte. If it indicates that another process is holding
37985 ** a SHARED lock, then this process may also take a SHARED lock
37986 ** and proceed with opening the *-shm file.
37987 **
37988 ** Or, if no other process is holding any lock, then this process
37989 ** is the first to open it. In this case take an EXCLUSIVE lock on the
37990 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
37991 ** downgrade to a SHARED lock on the DMS byte.
37992 **
37993 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
37994 ** return SQLITE_BUSY to the caller (it will try again). An earlier
37995 ** version of this code attempted the SHARED lock at this point. But
37996 ** this introduced a subtle race condition: if the process holding
37997 ** EXCLUSIVE failed just before truncating the *-shm file, then this
37998 ** process might open and use the *-shm file without truncating it.
37999 ** And if the *-shm file has been corrupted by a power failure or
38000 ** system crash, the database itself may also become corrupt. */
38001 lock.l_whence = SEEK_SET;
38002 lock.l_start = UNIX_SHM_DMS;
38003 lock.l_len = 1;
38004 lock.l_type = F_WRLCK;
38005 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
38006 rc = SQLITE_IOERR_LOCK;
38007 }else if( lock.l_type==F_UNLCK ){
38008 if( pShmNode->isReadonly ){
38009 pShmNode->isUnlocked = 1;
38010 rc = SQLITE_READONLY_CANTINIT;
38011 }else{
38012 rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
38013 /* The first connection to attach must truncate the -shm file. We
38014 ** truncate to 3 bytes (an arbitrary small number, less than the
38015 ** -shm header size) rather than 0 as a system debugging aid, to
38016 ** help detect if a -shm file truncation is legitimate or is the work
38017 ** or a rogue process. */
38018 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
38019 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
38020 }
38021 }
38022 }else if( lock.l_type==F_WRLCK ){
38023 rc = SQLITE_BUSY;
38024 }
38025
38026 if( rc==SQLITE_OK ){
38027 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
38028 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
38029 }
38030 return rc;
38031}
38032
38033/*
38034** Open a shared-memory area associated with open database file pDbFd.
38035** This particular implementation uses mmapped files.
38036**
38037** The file used to implement shared-memory is in the same directory
38038** as the open database file and has the same name as the open database
38039** file with the "-shm" suffix added. For example, if the database file
38040** is "/home/user1/config.db" then the file that is created and mmapped
38041** for shared memory will be called "/home/user1/config.db-shm".
38042**
38043** Another approach to is to use files in /dev/shm or /dev/tmp or an
38044** some other tmpfs mount. But if a file in a different directory
38045** from the database file is used, then differing access permissions
38046** or a chroot() might cause two different processes on the same
38047** database to end up using different files for shared memory -
38048** meaning that their memory would not really be shared - resulting
38049** in database corruption. Nevertheless, this tmpfs file usage
38050** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
38051** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
38052** option results in an incompatible build of SQLite; builds of SQLite
38053** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
38054** same database file at the same time, database corruption will likely
38055** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
38056** "unsupported" and may go away in a future SQLite release.
38057**
38058** When opening a new shared-memory file, if no other instances of that
38059** file are currently open, in this process or in other processes, then
38060** the file must be truncated to zero length or have its header cleared.
38061**
38062** If the original database file (pDbFd) is using the "unix-excl" VFS
38063** that means that an exclusive lock is held on the database file and
38064** that no other processes are able to read or write the database. In
38065** that case, we do not really need shared memory. No shared memory
38066** file is created. The shared memory will be simulated with heap memory.
38067*/
38068static int unixOpenSharedMemory(unixFile *pDbFd){
38069 struct unixShm *p = 0; /* The connection to be opened */
38070 struct unixShmNode *pShmNode; /* The underlying mmapped file */
38071 int rc = SQLITE_OK; /* Result code */
38072 unixInodeInfo *pInode; /* The inode of fd */
38073 char *zShm; /* Name of the file used for SHM */
38074 int nShmFilename; /* Size of the SHM filename in bytes */
38075
38076 /* Allocate space for the new unixShm object. */
38077 p = sqlite3_malloc64( sizeof(*p) );
38078 if( p==0 ) return SQLITE_NOMEM_BKPT;
38079 memset(p, 0, sizeof(*p));
38080 assert( pDbFd->pShm==0 );
38081
38082 /* Check to see if a unixShmNode object already exists. Reuse an existing
38083 ** one if present. Create a new one if necessary.
38084 */
38085 assert( unixFileMutexNotheld(pDbFd) );
38086 unixEnterMutex();
38087 pInode = pDbFd->pInode;
38088 pShmNode = pInode->pShmNode;
38089 if( pShmNode==0 ){
38090 struct stat sStat; /* fstat() info for database file */
38091#ifndef SQLITE_SHM_DIRECTORY
38092 const char *zBasePath = pDbFd->zPath;
38093#endif
38094
38095 /* Call fstat() to figure out the permissions on the database file. If
38096 ** a new *-shm file is created, an attempt will be made to create it
38097 ** with the same permissions.
38098 */
38099 if( osFstat(pDbFd->h, &sStat) ){
38100 rc = SQLITE_IOERR_FSTAT;
38101 goto shm_open_err;
38102 }
38103
38104#ifdef SQLITE_SHM_DIRECTORY
38105 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
38106#else
38107 nShmFilename = 6 + (int)strlen(zBasePath);
38108#endif
38109 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
38110 if( pShmNode==0 ){
38111 rc = SQLITE_NOMEM_BKPT;
38112 goto shm_open_err;
38113 }
38114 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
38115 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
38116#ifdef SQLITE_SHM_DIRECTORY
38117 sqlite3_snprintf(nShmFilename, zShm,
38118 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
38119 (u32)sStat.st_ino, (u32)sStat.st_dev);
38120#else
38121 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
38122 sqlite3FileSuffix3(pDbFd->zPath, zShm);
38123#endif
38124 pShmNode->hShm = -1;
38125 pDbFd->pInode->pShmNode = pShmNode;
38126 pShmNode->pInode = pDbFd->pInode;
38127 if( sqlite3GlobalConfig.bCoreMutex ){
38128 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38129 if( pShmNode->pShmMutex==0 ){
38130 rc = SQLITE_NOMEM_BKPT;
38131 goto shm_open_err;
38132 }
38133 }
38134
38135 if( pInode->bProcessLock==0 ){
38136 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
38137 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
38138 (sStat.st_mode&0777));
38139 }
38140 if( pShmNode->hShm<0 ){
38141 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
38142 (sStat.st_mode&0777));
38143 if( pShmNode->hShm<0 ){
38144 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
38145 goto shm_open_err;
38146 }
38147 pShmNode->isReadonly = 1;
38148 }
38149
38150 /* If this process is running as root, make sure that the SHM file
38151 ** is owned by the same user that owns the original database. Otherwise,
38152 ** the original owner will not be able to connect.
38153 */
38154 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
38155
38156 rc = unixLockSharedMemory(pDbFd, pShmNode);
38157 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
38158 }
38159 }
38160
38161 /* Make the new connection a child of the unixShmNode */
38162 p->pShmNode = pShmNode;
38163#ifdef SQLITE_DEBUG
38164 p->id = pShmNode->nextShmId++;
38165#endif
38166 pShmNode->nRef++;
38167 pDbFd->pShm = p;
38168 unixLeaveMutex();
38169
38170 /* The reference count on pShmNode has already been incremented under
38171 ** the cover of the unixEnterMutex() mutex and the pointer from the
38172 ** new (struct unixShm) object to the pShmNode has been set. All that is
38173 ** left to do is to link the new object into the linked list starting
38174 ** at pShmNode->pFirst. This must be done while holding the
38175 ** pShmNode->pShmMutex.
38176 */
38177 sqlite3_mutex_enter(pShmNode->pShmMutex);
38178 p->pNext = pShmNode->pFirst;
38179 pShmNode->pFirst = p;
38180 sqlite3_mutex_leave(pShmNode->pShmMutex);
38181 return rc;
38182
38183 /* Jump here on any error */
38184shm_open_err:
38185 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
38186 sqlite3_free(p);
38187 unixLeaveMutex();
38188 return rc;
38189}
38190
38191/*
38192** This function is called to obtain a pointer to region iRegion of the
38193** shared-memory associated with the database file fd. Shared-memory regions
38194** are numbered starting from zero. Each shared-memory region is szRegion
38195** bytes in size.
38196**
38197** If an error occurs, an error code is returned and *pp is set to NULL.
38198**
38199** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
38200** region has not been allocated (by any client, including one running in a
38201** separate process), then *pp is set to NULL and SQLITE_OK returned. If
38202** bExtend is non-zero and the requested shared-memory region has not yet
38203** been allocated, it is allocated by this function.
38204**
38205** If the shared-memory region has already been allocated or is allocated by
38206** this call as described above, then it is mapped into this processes
38207** address space (if it is not already), *pp is set to point to the mapped
38208** memory and SQLITE_OK returned.
38209*/
38210static int unixShmMap(
38211 sqlite3_file *fd, /* Handle open on database file */
38212 int iRegion, /* Region to retrieve */
38213 int szRegion, /* Size of regions */
38214 int bExtend, /* True to extend file if necessary */
38215 void volatile **pp /* OUT: Mapped memory */
38216){
38217 unixFile *pDbFd = (unixFile*)fd;
38218 unixShm *p;
38219 unixShmNode *pShmNode;
38220 int rc = SQLITE_OK;
38221 int nShmPerMap = unixShmRegionPerMap();
38222 int nReqRegion;
38223
38224 /* If the shared-memory file has not yet been opened, open it now. */
38225 if( pDbFd->pShm==0 ){
38226 rc = unixOpenSharedMemory(pDbFd);
38227 if( rc!=SQLITE_OK ) return rc;
38228 }
38229
38230 p = pDbFd->pShm;
38231 pShmNode = p->pShmNode;
38232 sqlite3_mutex_enter(pShmNode->pShmMutex);
38233 if( pShmNode->isUnlocked ){
38234 rc = unixLockSharedMemory(pDbFd, pShmNode);
38235 if( rc!=SQLITE_OK ) goto shmpage_out;
38236 pShmNode->isUnlocked = 0;
38237 }
38238 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
38239 assert( pShmNode->pInode==pDbFd->pInode );
38240 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38241 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38242
38243 /* Minimum number of regions required to be mapped. */
38244 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
38245
38246 if( pShmNode->nRegion<nReqRegion ){
38247 char **apNew; /* New apRegion[] array */
38248 int nByte = nReqRegion*szRegion; /* Minimum required file size */
38249 struct stat sStat; /* Used by fstat() */
38250
38251 pShmNode->szRegion = szRegion;
38252
38253 if( pShmNode->hShm>=0 ){
38254 /* The requested region is not mapped into this processes address space.
38255 ** Check to see if it has been allocated (i.e. if the wal-index file is
38256 ** large enough to contain the requested region).
38257 */
38258 if( osFstat(pShmNode->hShm, &sStat) ){
38259 rc = SQLITE_IOERR_SHMSIZE;
38260 goto shmpage_out;
38261 }
38262
38263 if( sStat.st_size<nByte ){
38264 /* The requested memory region does not exist. If bExtend is set to
38265 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
38266 */
38267 if( !bExtend ){
38268 goto shmpage_out;
38269 }
38270
38271 /* Alternatively, if bExtend is true, extend the file. Do this by
38272 ** writing a single byte to the end of each (OS) page being
38273 ** allocated or extended. Technically, we need only write to the
38274 ** last page in order to extend the file. But writing to all new
38275 ** pages forces the OS to allocate them immediately, which reduces
38276 ** the chances of SIGBUS while accessing the mapped region later on.
38277 */
38278 else{
38279 static const int pgsz = 4096;
38280 int iPg;
38281
38282 /* Write to the last byte of each newly allocated or extended page */
38283 assert( (nByte % pgsz)==0 );
38284 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
38285 int x = 0;
38286 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
38287 const char *zFile = pShmNode->zFilename;
38288 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
38289 goto shmpage_out;
38290 }
38291 }
38292 }
38293 }
38294 }
38295
38296 /* Map the requested memory region into this processes address space. */
38297 apNew = (char **)sqlite3_realloc(
38298 pShmNode->apRegion, nReqRegion*sizeof(char *)
38299 );
38300 if( !apNew ){
38301 rc = SQLITE_IOERR_NOMEM_BKPT;
38302 goto shmpage_out;
38303 }
38304 pShmNode->apRegion = apNew;
38305 while( pShmNode->nRegion<nReqRegion ){
38306 int nMap = szRegion*nShmPerMap;
38307 int i;
38308 void *pMem;
38309 if( pShmNode->hShm>=0 ){
38310 pMem = osMmap(0, nMap,
38311 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
38312 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
38313 );
38314 if( pMem==MAP_FAILED ){
38315 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
38316 goto shmpage_out;
38317 }
38318 }else{
38319 pMem = sqlite3_malloc64(nMap);
38320 if( pMem==0 ){
38321 rc = SQLITE_NOMEM_BKPT;
38322 goto shmpage_out;
38323 }
38324 memset(pMem, 0, nMap);
38325 }
38326
38327 for(i=0; i<nShmPerMap; i++){
38328 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
38329 }
38330 pShmNode->nRegion += nShmPerMap;
38331 }
38332 }
38333
38334shmpage_out:
38335 if( pShmNode->nRegion>iRegion ){
38336 *pp = pShmNode->apRegion[iRegion];
38337 }else{
38338 *pp = 0;
38339 }
38340 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
38341 sqlite3_mutex_leave(pShmNode->pShmMutex);
38342 return rc;
38343}
38344
38345/*
38346** Change the lock state for a shared-memory segment.
38347**
38348** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
38349** different here than in posix. In xShmLock(), one can go from unlocked
38350** to shared and back or from unlocked to exclusive and back. But one may
38351** not go from shared to exclusive or from exclusive to shared.
38352*/
38353static int unixShmLock(
38354 sqlite3_file *fd, /* Database file holding the shared memory */
38355 int ofst, /* First lock to acquire or release */
38356 int n, /* Number of locks to acquire or release */
38357 int flags /* What to do with the lock */
38358){
38359 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
38360 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
38361 unixShm *pX; /* For looping over all siblings */
38362 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
38363 int rc = SQLITE_OK; /* Result code */
38364 u16 mask; /* Mask of locks to take or release */
38365
38366 assert( pShmNode==pDbFd->pInode->pShmNode );
38367 assert( pShmNode->pInode==pDbFd->pInode );
38368 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
38369 assert( n>=1 );
38370 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
38371 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
38372 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
38373 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
38374 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
38375 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38376 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38377
38378 /* Check that, if this to be a blocking lock, no locks that occur later
38379 ** in the following list than the lock being obtained are already held:
38380 **
38381 ** 1. Checkpointer lock (ofst==1).
38382 ** 2. Write lock (ofst==0).
38383 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
38384 **
38385 ** In other words, if this is a blocking lock, none of the locks that
38386 ** occur later in the above list than the lock being obtained may be
38387 ** held.
38388 **
38389 ** It is not permitted to block on the RECOVER lock.
38390 */
38391#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38392 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
38393 (ofst!=2) /* not RECOVER */
38394 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
38395 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
38396 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
38397 ));
38398#endif
38399
38400 mask = (1<<(ofst+n)) - (1<<ofst);
38401 assert( n>1 || mask==(1<<ofst) );
38402 sqlite3_mutex_enter(pShmNode->pShmMutex);
38403 if( flags & SQLITE_SHM_UNLOCK ){
38404 u16 allMask = 0; /* Mask of locks held by siblings */
38405
38406 /* See if any siblings hold this same lock */
38407 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38408 if( pX==p ) continue;
38409 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
38410 allMask |= pX->sharedMask;
38411 }
38412
38413 /* Unlock the system-level locks */
38414 if( (mask & allMask)==0 ){
38415 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
38416 }else{
38417 rc = SQLITE_OK;
38418 }
38419
38420 /* Undo the local locks */
38421 if( rc==SQLITE_OK ){
38422 p->exclMask &= ~mask;
38423 p->sharedMask &= ~mask;
38424 }
38425 }else if( flags & SQLITE_SHM_SHARED ){
38426 u16 allShared = 0; /* Union of locks held by connections other than "p" */
38427
38428 /* Find out which shared locks are already held by sibling connections.
38429 ** If any sibling already holds an exclusive lock, go ahead and return
38430 ** SQLITE_BUSY.
38431 */
38432 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38433 if( (pX->exclMask & mask)!=0 ){
38434 rc = SQLITE_BUSY;
38435 break;
38436 }
38437 allShared |= pX->sharedMask;
38438 }
38439
38440 /* Get shared locks at the system level, if necessary */
38441 if( rc==SQLITE_OK ){
38442 if( (allShared & mask)==0 ){
38443 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
38444 }else{
38445 rc = SQLITE_OK;
38446 }
38447 }
38448
38449 /* Get the local shared locks */
38450 if( rc==SQLITE_OK ){
38451 p->sharedMask |= mask;
38452 }
38453 }else{
38454 /* Make sure no sibling connections hold locks that will block this
38455 ** lock. If any do, return SQLITE_BUSY right away.
38456 */
38457 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38458 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
38459 rc = SQLITE_BUSY;
38460 break;
38461 }
38462 }
38463
38464 /* Get the exclusive locks at the system level. Then if successful
38465 ** also mark the local connection as being locked.
38466 */
38467 if( rc==SQLITE_OK ){
38468 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
38469 if( rc==SQLITE_OK ){
38470 assert( (p->sharedMask & mask)==0 );
38471 p->exclMask |= mask;
38472 }
38473 }
38474 }
38475 sqlite3_mutex_leave(pShmNode->pShmMutex);
38476 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
38477 p->id, osGetpid(0), p->sharedMask, p->exclMask));
38478 return rc;
38479}
38480
38481/*
38482** Implement a memory barrier or memory fence on shared memory.
38483**
38484** All loads and stores begun before the barrier must complete before
38485** any load or store begun after the barrier.
38486*/
38487static void unixShmBarrier(
38488 sqlite3_file *fd /* Database file holding the shared memory */
38489){
38490 UNUSED_PARAMETER(fd);
38491 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
38492 assert( fd->pMethods->xLock==nolockLock
38493 || unixFileMutexNotheld((unixFile*)fd)
38494 );
38495 unixEnterMutex(); /* Also mutex, for redundancy */
38496 unixLeaveMutex();
38497}
38498
38499/*
38500** Close a connection to shared-memory. Delete the underlying
38501** storage if deleteFlag is true.
38502**
38503** If there is no shared memory associated with the connection then this
38504** routine is a harmless no-op.
38505*/
38506static int unixShmUnmap(
38507 sqlite3_file *fd, /* The underlying database file */
38508 int deleteFlag /* Delete shared-memory if true */
38509){
38510 unixShm *p; /* The connection to be closed */
38511 unixShmNode *pShmNode; /* The underlying shared-memory file */
38512 unixShm **pp; /* For looping over sibling connections */
38513 unixFile *pDbFd; /* The underlying database file */
38514
38515 pDbFd = (unixFile*)fd;
38516 p = pDbFd->pShm;
38517 if( p==0 ) return SQLITE_OK;
38518 pShmNode = p->pShmNode;
38519
38520 assert( pShmNode==pDbFd->pInode->pShmNode );
38521 assert( pShmNode->pInode==pDbFd->pInode );
38522
38523 /* Remove connection p from the set of connections associated
38524 ** with pShmNode */
38525 sqlite3_mutex_enter(pShmNode->pShmMutex);
38526 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
38527 *pp = p->pNext;
38528
38529 /* Free the connection p */
38530 sqlite3_free(p);
38531 pDbFd->pShm = 0;
38532 sqlite3_mutex_leave(pShmNode->pShmMutex);
38533
38534 /* If pShmNode->nRef has reached 0, then close the underlying
38535 ** shared-memory file, too */
38536 assert( unixFileMutexNotheld(pDbFd) );
38537 unixEnterMutex();
38538 assert( pShmNode->nRef>0 );
38539 pShmNode->nRef--;
38540 if( pShmNode->nRef==0 ){
38541 if( deleteFlag && pShmNode->hShm>=0 ){
38542 osUnlink(pShmNode->zFilename);
38543 }
38544 unixShmPurge(pDbFd);
38545 }
38546 unixLeaveMutex();
38547
38548 return SQLITE_OK;
38549}
38550
38551
38552#else
38553# define unixShmMap 0
38554# define unixShmLock 0
38555# define unixShmBarrier 0
38556# define unixShmUnmap 0
38557#endif /* #ifndef SQLITE_OMIT_WAL */
38558
38559#if SQLITE_MAX_MMAP_SIZE>0
38560/*
38561** If it is currently memory mapped, unmap file pFd.
38562*/
38563static void unixUnmapfile(unixFile *pFd){
38564 assert( pFd->nFetchOut==0 );
38565 if( pFd->pMapRegion ){
38566 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
38567 pFd->pMapRegion = 0;
38568 pFd->mmapSize = 0;
38569 pFd->mmapSizeActual = 0;
38570 }
38571}
38572
38573/*
38574** Attempt to set the size of the memory mapping maintained by file
38575** descriptor pFd to nNew bytes. Any existing mapping is discarded.
38576**
38577** If successful, this function sets the following variables:
38578**
38579** unixFile.pMapRegion
38580** unixFile.mmapSize
38581** unixFile.mmapSizeActual
38582**
38583** If unsuccessful, an error message is logged via sqlite3_log() and
38584** the three variables above are zeroed. In this case SQLite should
38585** continue accessing the database using the xRead() and xWrite()
38586** methods.
38587*/
38588static void unixRemapfile(
38589 unixFile *pFd, /* File descriptor object */
38590 i64 nNew /* Required mapping size */
38591){
38592 const char *zErr = "mmap";
38593 int h = pFd->h; /* File descriptor open on db file */
38594 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
38595 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
38596 u8 *pNew = 0; /* Location of new mapping */
38597 int flags = PROT_READ; /* Flags to pass to mmap() */
38598
38599 assert( pFd->nFetchOut==0 );
38600 assert( nNew>pFd->mmapSize );
38601 assert( nNew<=pFd->mmapSizeMax );
38602 assert( nNew>0 );
38603 assert( pFd->mmapSizeActual>=pFd->mmapSize );
38604 assert( MAP_FAILED!=0 );
38605
38606#ifdef SQLITE_MMAP_READWRITE
38607 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
38608#endif
38609
38610 if( pOrig ){
38611#if HAVE_MREMAP
38612 i64 nReuse = pFd->mmapSize;
38613#else
38614 const int szSyspage = osGetpagesize();
38615 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
38616#endif
38617 u8 *pReq = &pOrig[nReuse];
38618
38619 /* Unmap any pages of the existing mapping that cannot be reused. */
38620 if( nReuse!=nOrig ){
38621 osMunmap(pReq, nOrig-nReuse);
38622 }
38623
38624#if HAVE_MREMAP
38625 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
38626 zErr = "mremap";
38627#else
38628 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
38629 if( pNew!=MAP_FAILED ){
38630 if( pNew!=pReq ){
38631 osMunmap(pNew, nNew - nReuse);
38632 pNew = 0;
38633 }else{
38634 pNew = pOrig;
38635 }
38636 }
38637#endif
38638
38639 /* The attempt to extend the existing mapping failed. Free it. */
38640 if( pNew==MAP_FAILED || pNew==0 ){
38641 osMunmap(pOrig, nReuse);
38642 }
38643 }
38644
38645 /* If pNew is still NULL, try to create an entirely new mapping. */
38646 if( pNew==0 ){
38647 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
38648 }
38649
38650 if( pNew==MAP_FAILED ){
38651 pNew = 0;
38652 nNew = 0;
38653 unixLogError(SQLITE_OK, zErr, pFd->zPath);
38654
38655 /* If the mmap() above failed, assume that all subsequent mmap() calls
38656 ** will probably fail too. Fall back to using xRead/xWrite exclusively
38657 ** in this case. */
38658 pFd->mmapSizeMax = 0;
38659 }
38660 pFd->pMapRegion = (void *)pNew;
38661 pFd->mmapSize = pFd->mmapSizeActual = nNew;
38662}
38663
38664/*
38665** Memory map or remap the file opened by file-descriptor pFd (if the file
38666** is already mapped, the existing mapping is replaced by the new). Or, if
38667** there already exists a mapping for this file, and there are still
38668** outstanding xFetch() references to it, this function is a no-op.
38669**
38670** If parameter nByte is non-negative, then it is the requested size of
38671** the mapping to create. Otherwise, if nByte is less than zero, then the
38672** requested size is the size of the file on disk. The actual size of the
38673** created mapping is either the requested size or the value configured
38674** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
38675**
38676** SQLITE_OK is returned if no error occurs (even if the mapping is not
38677** recreated as a result of outstanding references) or an SQLite error
38678** code otherwise.
38679*/
38680static int unixMapfile(unixFile *pFd, i64 nMap){
38681 assert( nMap>=0 || pFd->nFetchOut==0 );
38682 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
38683 if( pFd->nFetchOut>0 ) return SQLITE_OK;
38684
38685 if( nMap<0 ){
38686 struct stat statbuf; /* Low-level file information */
38687 if( osFstat(pFd->h, &statbuf) ){
38688 return SQLITE_IOERR_FSTAT;
38689 }
38690 nMap = statbuf.st_size;
38691 }
38692 if( nMap>pFd->mmapSizeMax ){
38693 nMap = pFd->mmapSizeMax;
38694 }
38695
38696 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
38697 if( nMap!=pFd->mmapSize ){
38698 unixRemapfile(pFd, nMap);
38699 }
38700
38701 return SQLITE_OK;
38702}
38703#endif /* SQLITE_MAX_MMAP_SIZE>0 */
38704
38705/*
38706** If possible, return a pointer to a mapping of file fd starting at offset
38707** iOff. The mapping must be valid for at least nAmt bytes.
38708**
38709** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
38710** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
38711** Finally, if an error does occur, return an SQLite error code. The final
38712** value of *pp is undefined in this case.
38713**
38714** If this function does return a pointer, the caller must eventually
38715** release the reference by calling unixUnfetch().
38716*/
38717static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
38718#if SQLITE_MAX_MMAP_SIZE>0
38719 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
38720#endif
38721 *pp = 0;
38722
38723#if SQLITE_MAX_MMAP_SIZE>0
38724 if( pFd->mmapSizeMax>0 ){
38725 if( pFd->pMapRegion==0 ){
38726 int rc = unixMapfile(pFd, -1);
38727 if( rc!=SQLITE_OK ) return rc;
38728 }
38729 if( pFd->mmapSize >= iOff+nAmt ){
38730 *pp = &((u8 *)pFd->pMapRegion)[iOff];
38731 pFd->nFetchOut++;
38732 }
38733 }
38734#endif
38735 return SQLITE_OK;
38736}
38737
38738/*
38739** If the third argument is non-NULL, then this function releases a
38740** reference obtained by an earlier call to unixFetch(). The second
38741** argument passed to this function must be the same as the corresponding
38742** argument that was passed to the unixFetch() invocation.
38743**
38744** Or, if the third argument is NULL, then this function is being called
38745** to inform the VFS layer that, according to POSIX, any existing mapping
38746** may now be invalid and should be unmapped.
38747*/
38748static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
38749#if SQLITE_MAX_MMAP_SIZE>0
38750 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
38751 UNUSED_PARAMETER(iOff);
38752
38753 /* If p==0 (unmap the entire file) then there must be no outstanding
38754 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
38755 ** then there must be at least one outstanding. */
38756 assert( (p==0)==(pFd->nFetchOut==0) );
38757
38758 /* If p!=0, it must match the iOff value. */
38759 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
38760
38761 if( p ){
38762 pFd->nFetchOut--;
38763 }else{
38764 unixUnmapfile(pFd);
38765 }
38766
38767 assert( pFd->nFetchOut>=0 );
38768#else
38769 UNUSED_PARAMETER(fd);
38770 UNUSED_PARAMETER(p);
38771 UNUSED_PARAMETER(iOff);
38772#endif
38773 return SQLITE_OK;
38774}
38775
38776/*
38777** Here ends the implementation of all sqlite3_file methods.
38778**
38779********************** End sqlite3_file Methods *******************************
38780******************************************************************************/
38781
38782/*
38783** This division contains definitions of sqlite3_io_methods objects that
38784** implement various file locking strategies. It also contains definitions
38785** of "finder" functions. A finder-function is used to locate the appropriate
38786** sqlite3_io_methods object for a particular database file. The pAppData
38787** field of the sqlite3_vfs VFS objects are initialized to be pointers to
38788** the correct finder-function for that VFS.
38789**
38790** Most finder functions return a pointer to a fixed sqlite3_io_methods
38791** object. The only interesting finder-function is autolockIoFinder, which
38792** looks at the filesystem type and tries to guess the best locking
38793** strategy from that.
38794**
38795** For finder-function F, two objects are created:
38796**
38797** (1) The real finder-function named "FImpt()".
38798**
38799** (2) A constant pointer to this function named just "F".
38800**
38801**
38802** A pointer to the F pointer is used as the pAppData value for VFS
38803** objects. We have to do this instead of letting pAppData point
38804** directly at the finder-function since C90 rules prevent a void*
38805** from be cast into a function pointer.
38806**
38807**
38808** Each instance of this macro generates two objects:
38809**
38810** * A constant sqlite3_io_methods object call METHOD that has locking
38811** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
38812**
38813** * An I/O method finder function called FINDER that returns a pointer
38814** to the METHOD object in the previous bullet.
38815*/
38816#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
38817static const sqlite3_io_methods METHOD = { \
38818 VERSION, /* iVersion */ \
38819 CLOSE, /* xClose */ \
38820 unixRead, /* xRead */ \
38821 unixWrite, /* xWrite */ \
38822 unixTruncate, /* xTruncate */ \
38823 unixSync, /* xSync */ \
38824 unixFileSize, /* xFileSize */ \
38825 LOCK, /* xLock */ \
38826 UNLOCK, /* xUnlock */ \
38827 CKLOCK, /* xCheckReservedLock */ \
38828 unixFileControl, /* xFileControl */ \
38829 unixSectorSize, /* xSectorSize */ \
38830 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
38831 SHMMAP, /* xShmMap */ \
38832 unixShmLock, /* xShmLock */ \
38833 unixShmBarrier, /* xShmBarrier */ \
38834 unixShmUnmap, /* xShmUnmap */ \
38835 unixFetch, /* xFetch */ \
38836 unixUnfetch, /* xUnfetch */ \
38837}; \
38838static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
38839 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
38840 return &METHOD; \
38841} \
38842static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
38843 = FINDER##Impl;
38844
38845/*
38846** Here are all of the sqlite3_io_methods objects for each of the
38847** locking strategies. Functions that return pointers to these methods
38848** are also created.
38849*/
38850IOMETHODS(
38851 posixIoFinder, /* Finder function name */
38852 posixIoMethods, /* sqlite3_io_methods object name */
38853 3, /* shared memory and mmap are enabled */
38854 unixClose, /* xClose method */
38855 unixLock, /* xLock method */
38856 unixUnlock, /* xUnlock method */
38857 unixCheckReservedLock, /* xCheckReservedLock method */
38858 unixShmMap /* xShmMap method */
38859)
38860IOMETHODS(
38861 nolockIoFinder, /* Finder function name */
38862 nolockIoMethods, /* sqlite3_io_methods object name */
38863 3, /* shared memory and mmap are enabled */
38864 nolockClose, /* xClose method */
38865 nolockLock, /* xLock method */
38866 nolockUnlock, /* xUnlock method */
38867 nolockCheckReservedLock, /* xCheckReservedLock method */
38868 0 /* xShmMap method */
38869)
38870IOMETHODS(
38871 dotlockIoFinder, /* Finder function name */
38872 dotlockIoMethods, /* sqlite3_io_methods object name */
38873 1, /* shared memory is disabled */
38874 dotlockClose, /* xClose method */
38875 dotlockLock, /* xLock method */
38876 dotlockUnlock, /* xUnlock method */
38877 dotlockCheckReservedLock, /* xCheckReservedLock method */
38878 0 /* xShmMap method */
38879)
38880
38881#if SQLITE_ENABLE_LOCKING_STYLE
38882IOMETHODS(
38883 flockIoFinder, /* Finder function name */
38884 flockIoMethods, /* sqlite3_io_methods object name */
38885 1, /* shared memory is disabled */
38886 flockClose, /* xClose method */
38887 flockLock, /* xLock method */
38888 flockUnlock, /* xUnlock method */
38889 flockCheckReservedLock, /* xCheckReservedLock method */
38890 0 /* xShmMap method */
38891)
38892#endif
38893
38894#if OS_VXWORKS
38895IOMETHODS(
38896 semIoFinder, /* Finder function name */
38897 semIoMethods, /* sqlite3_io_methods object name */
38898 1, /* shared memory is disabled */
38899 semXClose, /* xClose method */
38900 semXLock, /* xLock method */
38901 semXUnlock, /* xUnlock method */
38902 semXCheckReservedLock, /* xCheckReservedLock method */
38903 0 /* xShmMap method */
38904)
38905#endif
38906
38907#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38908IOMETHODS(
38909 afpIoFinder, /* Finder function name */
38910 afpIoMethods, /* sqlite3_io_methods object name */
38911 1, /* shared memory is disabled */
38912 afpClose, /* xClose method */
38913 afpLock, /* xLock method */
38914 afpUnlock, /* xUnlock method */
38915 afpCheckReservedLock, /* xCheckReservedLock method */
38916 0 /* xShmMap method */
38917)
38918#endif
38919
38920/*
38921** The proxy locking method is a "super-method" in the sense that it
38922** opens secondary file descriptors for the conch and lock files and
38923** it uses proxy, dot-file, AFP, and flock() locking methods on those
38924** secondary files. For this reason, the division that implements
38925** proxy locking is located much further down in the file. But we need
38926** to go ahead and define the sqlite3_io_methods and finder function
38927** for proxy locking here. So we forward declare the I/O methods.
38928*/
38929#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38930static int proxyClose(sqlite3_file*);
38931static int proxyLock(sqlite3_file*, int);
38932static int proxyUnlock(sqlite3_file*, int);
38933static int proxyCheckReservedLock(sqlite3_file*, int*);
38934IOMETHODS(
38935 proxyIoFinder, /* Finder function name */
38936 proxyIoMethods, /* sqlite3_io_methods object name */
38937 1, /* shared memory is disabled */
38938 proxyClose, /* xClose method */
38939 proxyLock, /* xLock method */
38940 proxyUnlock, /* xUnlock method */
38941 proxyCheckReservedLock, /* xCheckReservedLock method */
38942 0 /* xShmMap method */
38943)
38944#endif
38945
38946/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
38947#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38948IOMETHODS(
38949 nfsIoFinder, /* Finder function name */
38950 nfsIoMethods, /* sqlite3_io_methods object name */
38951 1, /* shared memory is disabled */
38952 unixClose, /* xClose method */
38953 unixLock, /* xLock method */
38954 nfsUnlock, /* xUnlock method */
38955 unixCheckReservedLock, /* xCheckReservedLock method */
38956 0 /* xShmMap method */
38957)
38958#endif
38959
38960#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38961/*
38962** This "finder" function attempts to determine the best locking strategy
38963** for the database file "filePath". It then returns the sqlite3_io_methods
38964** object that implements that strategy.
38965**
38966** This is for MacOSX only.
38967*/
38968static const sqlite3_io_methods *autolockIoFinderImpl(
38969 const char *filePath, /* name of the database file */
38970 unixFile *pNew /* open file object for the database file */
38971){
38972 static const struct Mapping {
38973 const char *zFilesystem; /* Filesystem type name */
38974 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
38975 } aMap[] = {
38976 { "hfs", &posixIoMethods },
38977 { "ufs", &posixIoMethods },
38978 { "afpfs", &afpIoMethods },
38979 { "smbfs", &afpIoMethods },
38980 { "webdav", &nolockIoMethods },
38981 { 0, 0 }
38982 };
38983 int i;
38984 struct statfs fsInfo;
38985 struct flock lockInfo;
38986
38987 if( !filePath ){
38988 /* If filePath==NULL that means we are dealing with a transient file
38989 ** that does not need to be locked. */
38990 return &nolockIoMethods;
38991 }
38992 if( statfs(filePath, &fsInfo) != -1 ){
38993 if( fsInfo.f_flags & MNT_RDONLY ){
38994 return &nolockIoMethods;
38995 }
38996 for(i=0; aMap[i].zFilesystem; i++){
38997 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
38998 return aMap[i].pMethods;
38999 }
39000 }
39001 }
39002
39003 /* Default case. Handles, amongst others, "nfs".
39004 ** Test byte-range lock using fcntl(). If the call succeeds,
39005 ** assume that the file-system supports POSIX style locks.
39006 */
39007 lockInfo.l_len = 1;
39008 lockInfo.l_start = 0;
39009 lockInfo.l_whence = SEEK_SET;
39010 lockInfo.l_type = F_RDLCK;
39011 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39012 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
39013 return &nfsIoMethods;
39014 } else {
39015 return &posixIoMethods;
39016 }
39017 }else{
39018 return &dotlockIoMethods;
39019 }
39020}
39021static const sqlite3_io_methods
39022 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
39023
39024#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
39025
39026#if OS_VXWORKS
39027/*
39028** This "finder" function for VxWorks checks to see if posix advisory
39029** locking works. If it does, then that is what is used. If it does not
39030** work, then fallback to named semaphore locking.
39031*/
39032static const sqlite3_io_methods *vxworksIoFinderImpl(
39033 const char *filePath, /* name of the database file */
39034 unixFile *pNew /* the open file object */
39035){
39036 struct flock lockInfo;
39037
39038 if( !filePath ){
39039 /* If filePath==NULL that means we are dealing with a transient file
39040 ** that does not need to be locked. */
39041 return &nolockIoMethods;
39042 }
39043
39044 /* Test if fcntl() is supported and use POSIX style locks.
39045 ** Otherwise fall back to the named semaphore method.
39046 */
39047 lockInfo.l_len = 1;
39048 lockInfo.l_start = 0;
39049 lockInfo.l_whence = SEEK_SET;
39050 lockInfo.l_type = F_RDLCK;
39051 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39052 return &posixIoMethods;
39053 }else{
39054 return &semIoMethods;
39055 }
39056}
39057static const sqlite3_io_methods
39058 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
39059
39060#endif /* OS_VXWORKS */
39061
39062/*
39063** An abstract type for a pointer to an IO method finder function:
39064*/
39065typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
39066
39067
39068/****************************************************************************
39069**************************** sqlite3_vfs methods ****************************
39070**
39071** This division contains the implementation of methods on the
39072** sqlite3_vfs object.
39073*/
39074
39075/*
39076** Initialize the contents of the unixFile structure pointed to by pId.
39077*/
39078static int fillInUnixFile(
39079 sqlite3_vfs *pVfs, /* Pointer to vfs object */
39080 int h, /* Open file descriptor of file being opened */
39081 sqlite3_file *pId, /* Write to the unixFile structure here */
39082 const char *zFilename, /* Name of the file being opened */
39083 int ctrlFlags /* Zero or more UNIXFILE_* values */
39084){
39085 const sqlite3_io_methods *pLockingStyle;
39086 unixFile *pNew = (unixFile *)pId;
39087 int rc = SQLITE_OK;
39088
39089 assert( pNew->pInode==NULL );
39090
39091 /* No locking occurs in temporary files */
39092 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
39093
39094 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
39095 pNew->h = h;
39096 pNew->pVfs = pVfs;
39097 pNew->zPath = zFilename;
39098 pNew->ctrlFlags = (u8)ctrlFlags;
39099#if SQLITE_MAX_MMAP_SIZE>0
39100 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
39101#endif
39102 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
39103 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
39104 pNew->ctrlFlags |= UNIXFILE_PSOW;
39105 }
39106 if( strcmp(pVfs->zName,"unix-excl")==0 ){
39107 pNew->ctrlFlags |= UNIXFILE_EXCL;
39108 }
39109
39110#if OS_VXWORKS
39111 pNew->pId = vxworksFindFileId(zFilename);
39112 if( pNew->pId==0 ){
39113 ctrlFlags |= UNIXFILE_NOLOCK;
39114 rc = SQLITE_NOMEM_BKPT;
39115 }
39116#endif
39117
39118 if( ctrlFlags & UNIXFILE_NOLOCK ){
39119 pLockingStyle = &nolockIoMethods;
39120 }else{
39121 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
39122#if SQLITE_ENABLE_LOCKING_STYLE
39123 /* Cache zFilename in the locking context (AFP and dotlock override) for
39124 ** proxyLock activation is possible (remote proxy is based on db name)
39125 ** zFilename remains valid until file is closed, to support */
39126 pNew->lockingContext = (void*)zFilename;
39127#endif
39128 }
39129
39130 if( pLockingStyle == &posixIoMethods
39131#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
39132 || pLockingStyle == &nfsIoMethods
39133#endif
39134 ){
39135 unixEnterMutex();
39136 rc = findInodeInfo(pNew, &pNew->pInode);
39137 if( rc!=SQLITE_OK ){
39138 /* If an error occurred in findInodeInfo(), close the file descriptor
39139 ** immediately, before releasing the mutex. findInodeInfo() may fail
39140 ** in two scenarios:
39141 **
39142 ** (a) A call to fstat() failed.
39143 ** (b) A malloc failed.
39144 **
39145 ** Scenario (b) may only occur if the process is holding no other
39146 ** file descriptors open on the same file. If there were other file
39147 ** descriptors on this file, then no malloc would be required by
39148 ** findInodeInfo(). If this is the case, it is quite safe to close
39149 ** handle h - as it is guaranteed that no posix locks will be released
39150 ** by doing so.
39151 **
39152 ** If scenario (a) caused the error then things are not so safe. The
39153 ** implicit assumption here is that if fstat() fails, things are in
39154 ** such bad shape that dropping a lock or two doesn't matter much.
39155 */
39156 robust_close(pNew, h, __LINE__);
39157 h = -1;
39158 }
39159 unixLeaveMutex();
39160 }
39161
39162#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39163 else if( pLockingStyle == &afpIoMethods ){
39164 /* AFP locking uses the file path so it needs to be included in
39165 ** the afpLockingContext.
39166 */
39167 afpLockingContext *pCtx;
39168 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39169 if( pCtx==0 ){
39170 rc = SQLITE_NOMEM_BKPT;
39171 }else{
39172 /* NB: zFilename exists and remains valid until the file is closed
39173 ** according to requirement F11141. So we do not need to make a
39174 ** copy of the filename. */
39175 pCtx->dbPath = zFilename;
39176 pCtx->reserved = 0;
39177 srandomdev();
39178 unixEnterMutex();
39179 rc = findInodeInfo(pNew, &pNew->pInode);
39180 if( rc!=SQLITE_OK ){
39181 sqlite3_free(pNew->lockingContext);
39182 robust_close(pNew, h, __LINE__);
39183 h = -1;
39184 }
39185 unixLeaveMutex();
39186 }
39187 }
39188#endif
39189
39190 else if( pLockingStyle == &dotlockIoMethods ){
39191 /* Dotfile locking uses the file path so it needs to be included in
39192 ** the dotlockLockingContext
39193 */
39194 char *zLockFile;
39195 int nFilename;
39196 assert( zFilename!=0 );
39197 nFilename = (int)strlen(zFilename) + 6;
39198 zLockFile = (char *)sqlite3_malloc64(nFilename);
39199 if( zLockFile==0 ){
39200 rc = SQLITE_NOMEM_BKPT;
39201 }else{
39202 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
39203 }
39204 pNew->lockingContext = zLockFile;
39205 }
39206
39207#if OS_VXWORKS
39208 else if( pLockingStyle == &semIoMethods ){
39209 /* Named semaphore locking uses the file path so it needs to be
39210 ** included in the semLockingContext
39211 */
39212 unixEnterMutex();
39213 rc = findInodeInfo(pNew, &pNew->pInode);
39214 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
39215 char *zSemName = pNew->pInode->aSemName;
39216 int n;
39217 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
39218 pNew->pId->zCanonicalName);
39219 for( n=1; zSemName[n]; n++ )
39220 if( zSemName[n]=='/' ) zSemName[n] = '_';
39221 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
39222 if( pNew->pInode->pSem == SEM_FAILED ){
39223 rc = SQLITE_NOMEM_BKPT;
39224 pNew->pInode->aSemName[0] = '\0';
39225 }
39226 }
39227 unixLeaveMutex();
39228 }
39229#endif
39230
39231 storeLastErrno(pNew, 0);
39232#if OS_VXWORKS
39233 if( rc!=SQLITE_OK ){
39234 if( h>=0 ) robust_close(pNew, h, __LINE__);
39235 h = -1;
39236 osUnlink(zFilename);
39237 pNew->ctrlFlags |= UNIXFILE_DELETE;
39238 }
39239#endif
39240 if( rc!=SQLITE_OK ){
39241 if( h>=0 ) robust_close(pNew, h, __LINE__);
39242 }else{
39243 pId->pMethods = pLockingStyle;
39244 OpenCounter(+1);
39245 verifyDbFile(pNew);
39246 }
39247 return rc;
39248}
39249
39250/*
39251** Return the name of a directory in which to put temporary files.
39252** If no suitable temporary file directory can be found, return NULL.
39253*/
39254static const char *unixTempFileDir(void){
39255 static const char *azDirs[] = {
39256 0,
39257 0,
39258 "/var/tmp",
39259 "/usr/tmp",
39260 "/tmp",
39261 "."
39262 };
39263 unsigned int i = 0;
39264 struct stat buf;
39265 const char *zDir = sqlite3_temp_directory;
39266
39267 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
39268 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
39269 while(1){
39270 if( zDir!=0
39271 && osStat(zDir, &buf)==0
39272 && S_ISDIR(buf.st_mode)
39273 && osAccess(zDir, 03)==0
39274 ){
39275 return zDir;
39276 }
39277 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
39278 zDir = azDirs[i++];
39279 }
39280 return 0;
39281}
39282
39283/*
39284** Create a temporary file name in zBuf. zBuf must be allocated
39285** by the calling process and must be big enough to hold at least
39286** pVfs->mxPathname bytes.
39287*/
39288static int unixGetTempname(int nBuf, char *zBuf){
39289 const char *zDir;
39290 int iLimit = 0;
39291
39292 /* It's odd to simulate an io-error here, but really this is just
39293 ** using the io-error infrastructure to test that SQLite handles this
39294 ** function failing.
39295 */
39296 zBuf[0] = 0;
39297 SimulateIOError( return SQLITE_IOERR );
39298
39299 zDir = unixTempFileDir();
39300 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
39301 do{
39302 u64 r;
39303 sqlite3_randomness(sizeof(r), &r);
39304 assert( nBuf>2 );
39305 zBuf[nBuf-2] = 0;
39306 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
39307 zDir, r, 0);
39308 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
39309 }while( osAccess(zBuf,0)==0 );
39310 return SQLITE_OK;
39311}
39312
39313#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39314/*
39315** Routine to transform a unixFile into a proxy-locking unixFile.
39316** Implementation in the proxy-lock division, but used by unixOpen()
39317** if SQLITE_PREFER_PROXY_LOCKING is defined.
39318*/
39319static int proxyTransformUnixFile(unixFile*, const char*);
39320#endif
39321
39322/*
39323** Search for an unused file descriptor that was opened on the database
39324** file (not a journal or super-journal file) identified by pathname
39325** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
39326** argument to this function.
39327**
39328** Such a file descriptor may exist if a database connection was closed
39329** but the associated file descriptor could not be closed because some
39330** other file descriptor open on the same file is holding a file-lock.
39331** Refer to comments in the unixClose() function and the lengthy comment
39332** describing "Posix Advisory Locking" at the start of this file for
39333** further details. Also, ticket #4018.
39334**
39335** If a suitable file descriptor is found, then it is returned. If no
39336** such file descriptor is located, -1 is returned.
39337*/
39338static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
39339 UnixUnusedFd *pUnused = 0;
39340
39341 /* Do not search for an unused file descriptor on vxworks. Not because
39342 ** vxworks would not benefit from the change (it might, we're not sure),
39343 ** but because no way to test it is currently available. It is better
39344 ** not to risk breaking vxworks support for the sake of such an obscure
39345 ** feature. */
39346#if !OS_VXWORKS
39347 struct stat sStat; /* Results of stat() call */
39348
39349 unixEnterMutex();
39350
39351 /* A stat() call may fail for various reasons. If this happens, it is
39352 ** almost certain that an open() call on the same path will also fail.
39353 ** For this reason, if an error occurs in the stat() call here, it is
39354 ** ignored and -1 is returned. The caller will try to open a new file
39355 ** descriptor on the same path, fail, and return an error to SQLite.
39356 **
39357 ** Even if a subsequent open() call does succeed, the consequences of
39358 ** not searching for a reusable file descriptor are not dire. */
39359 if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
39360 unixInodeInfo *pInode;
39361
39362 pInode = inodeList;
39363 while( pInode && (pInode->fileId.dev!=sStat.st_dev
39364 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
39365 pInode = pInode->pNext;
39366 }
39367 if( pInode ){
39368 UnixUnusedFd **pp;
39369 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
39370 sqlite3_mutex_enter(pInode->pLockMutex);
39371 flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
39372 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
39373 pUnused = *pp;
39374 if( pUnused ){
39375 *pp = pUnused->pNext;
39376 }
39377 sqlite3_mutex_leave(pInode->pLockMutex);
39378 }
39379 }
39380 unixLeaveMutex();
39381#endif /* if !OS_VXWORKS */
39382 return pUnused;
39383}
39384
39385/*
39386** Find the mode, uid and gid of file zFile.
39387*/
39388static int getFileMode(
39389 const char *zFile, /* File name */
39390 mode_t *pMode, /* OUT: Permissions of zFile */
39391 uid_t *pUid, /* OUT: uid of zFile. */
39392 gid_t *pGid /* OUT: gid of zFile. */
39393){
39394 struct stat sStat; /* Output of stat() on database file */
39395 int rc = SQLITE_OK;
39396 if( 0==osStat(zFile, &sStat) ){
39397 *pMode = sStat.st_mode & 0777;
39398 *pUid = sStat.st_uid;
39399 *pGid = sStat.st_gid;
39400 }else{
39401 rc = SQLITE_IOERR_FSTAT;
39402 }
39403 return rc;
39404}
39405
39406/*
39407** This function is called by unixOpen() to determine the unix permissions
39408** to create new files with. If no error occurs, then SQLITE_OK is returned
39409** and a value suitable for passing as the third argument to open(2) is
39410** written to *pMode. If an IO error occurs, an SQLite error code is
39411** returned and the value of *pMode is not modified.
39412**
39413** In most cases, this routine sets *pMode to 0, which will become
39414** an indication to robust_open() to create the file using
39415** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
39416** But if the file being opened is a WAL or regular journal file, then
39417** this function queries the file-system for the permissions on the
39418** corresponding database file and sets *pMode to this value. Whenever
39419** possible, WAL and journal files are created using the same permissions
39420** as the associated database file.
39421**
39422** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
39423** original filename is unavailable. But 8_3_NAMES is only used for
39424** FAT filesystems and permissions do not matter there, so just use
39425** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
39426*/
39427static int findCreateFileMode(
39428 const char *zPath, /* Path of file (possibly) being created */
39429 int flags, /* Flags passed as 4th argument to xOpen() */
39430 mode_t *pMode, /* OUT: Permissions to open file with */
39431 uid_t *pUid, /* OUT: uid to set on the file */
39432 gid_t *pGid /* OUT: gid to set on the file */
39433){
39434 int rc = SQLITE_OK; /* Return Code */
39435 *pMode = 0;
39436 *pUid = 0;
39437 *pGid = 0;
39438 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
39439 char zDb[MAX_PATHNAME+1]; /* Database file path */
39440 int nDb; /* Number of valid bytes in zDb */
39441
39442 /* zPath is a path to a WAL or journal file. The following block derives
39443 ** the path to the associated database file from zPath. This block handles
39444 ** the following naming conventions:
39445 **
39446 ** "<path to db>-journal"
39447 ** "<path to db>-wal"
39448 ** "<path to db>-journalNN"
39449 ** "<path to db>-walNN"
39450 **
39451 ** where NN is a decimal number. The NN naming schemes are
39452 ** used by the test_multiplex.c module.
39453 */
39454 nDb = sqlite3Strlen30(zPath) - 1;
39455 while( zPath[nDb]!='-' ){
39456 /* In normal operation, the journal file name will always contain
39457 ** a '-' character. However in 8+3 filename mode, or if a corrupt
39458 ** rollback journal specifies a super-journal with a goofy name, then
39459 ** the '-' might be missing. */
39460 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
39461 nDb--;
39462 }
39463 memcpy(zDb, zPath, nDb);
39464 zDb[nDb] = '\0';
39465
39466 rc = getFileMode(zDb, pMode, pUid, pGid);
39467 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
39468 *pMode = 0600;
39469 }else if( flags & SQLITE_OPEN_URI ){
39470 /* If this is a main database file and the file was opened using a URI
39471 ** filename, check for the "modeof" parameter. If present, interpret
39472 ** its value as a filename and try to copy the mode, uid and gid from
39473 ** that file. */
39474 const char *z = sqlite3_uri_parameter(zPath, "modeof");
39475 if( z ){
39476 rc = getFileMode(z, pMode, pUid, pGid);
39477 }
39478 }
39479 return rc;
39480}
39481
39482/*
39483** Open the file zPath.
39484**
39485** Previously, the SQLite OS layer used three functions in place of this
39486** one:
39487**
39488** sqlite3OsOpenReadWrite();
39489** sqlite3OsOpenReadOnly();
39490** sqlite3OsOpenExclusive();
39491**
39492** These calls correspond to the following combinations of flags:
39493**
39494** ReadWrite() -> (READWRITE | CREATE)
39495** ReadOnly() -> (READONLY)
39496** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
39497**
39498** The old OpenExclusive() accepted a boolean argument - "delFlag". If
39499** true, the file was configured to be automatically deleted when the
39500** file handle closed. To achieve the same effect using this new
39501** interface, add the DELETEONCLOSE flag to those specified above for
39502** OpenExclusive().
39503*/
39504static int unixOpen(
39505 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
39506 const char *zPath, /* Pathname of file to be opened */
39507 sqlite3_file *pFile, /* The file descriptor to be filled in */
39508 int flags, /* Input flags to control the opening */
39509 int *pOutFlags /* Output flags returned to SQLite core */
39510){
39511 unixFile *p = (unixFile *)pFile;
39512 int fd = -1; /* File descriptor returned by open() */
39513 int openFlags = 0; /* Flags to pass to open() */
39514 int eType = flags&0x0FFF00; /* Type of file to open */
39515 int noLock; /* True to omit locking primitives */
39516 int rc = SQLITE_OK; /* Function Return Code */
39517 int ctrlFlags = 0; /* UNIXFILE_* flags */
39518
39519 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
39520 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
39521 int isCreate = (flags & SQLITE_OPEN_CREATE);
39522 int isReadonly = (flags & SQLITE_OPEN_READONLY);
39523 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
39524#if SQLITE_ENABLE_LOCKING_STYLE
39525 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
39526#endif
39527#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
39528 struct statfs fsInfo;
39529#endif
39530
39531 /* If creating a super- or main-file journal, this function will open
39532 ** a file-descriptor on the directory too. The first time unixSync()
39533 ** is called the directory file descriptor will be fsync()ed and close()d.
39534 */
39535 int isNewJrnl = (isCreate && (
39536 eType==SQLITE_OPEN_SUPER_JOURNAL
39537 || eType==SQLITE_OPEN_MAIN_JOURNAL
39538 || eType==SQLITE_OPEN_WAL
39539 ));
39540
39541 /* If argument zPath is a NULL pointer, this function is required to open
39542 ** a temporary file. Use this buffer to store the file name in.
39543 */
39544 char zTmpname[MAX_PATHNAME+2];
39545 const char *zName = zPath;
39546
39547 /* Check the following statements are true:
39548 **
39549 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
39550 ** (b) if CREATE is set, then READWRITE must also be set, and
39551 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
39552 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
39553 */
39554 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
39555 assert(isCreate==0 || isReadWrite);
39556 assert(isExclusive==0 || isCreate);
39557 assert(isDelete==0 || isCreate);
39558
39559 /* The main DB, main journal, WAL file and super-journal are never
39560 ** automatically deleted. Nor are they ever temporary files. */
39561 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
39562 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
39563 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
39564 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
39565
39566 /* Assert that the upper layer has set one of the "file-type" flags. */
39567 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
39568 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
39569 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
39570 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
39571 );
39572
39573 /* Detect a pid change and reset the PRNG. There is a race condition
39574 ** here such that two or more threads all trying to open databases at
39575 ** the same instant might all reset the PRNG. But multiple resets
39576 ** are harmless.
39577 */
39578 if( randomnessPid!=osGetpid(0) ){
39579 randomnessPid = osGetpid(0);
39580 sqlite3_randomness(0,0);
39581 }
39582 memset(p, 0, sizeof(unixFile));
39583
39584 if( eType==SQLITE_OPEN_MAIN_DB ){
39585 UnixUnusedFd *pUnused;
39586 pUnused = findReusableFd(zName, flags);
39587 if( pUnused ){
39588 fd = pUnused->fd;
39589 }else{
39590 pUnused = sqlite3_malloc64(sizeof(*pUnused));
39591 if( !pUnused ){
39592 return SQLITE_NOMEM_BKPT;
39593 }
39594 }
39595 p->pPreallocatedUnused = pUnused;
39596
39597 /* Database filenames are double-zero terminated if they are not
39598 ** URIs with parameters. Hence, they can always be passed into
39599 ** sqlite3_uri_parameter(). */
39600 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
39601
39602 }else if( !zName ){
39603 /* If zName is NULL, the upper layer is requesting a temp file. */
39604 assert(isDelete && !isNewJrnl);
39605 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
39606 if( rc!=SQLITE_OK ){
39607 return rc;
39608 }
39609 zName = zTmpname;
39610
39611 /* Generated temporary filenames are always double-zero terminated
39612 ** for use by sqlite3_uri_parameter(). */
39613 assert( zName[strlen(zName)+1]==0 );
39614 }
39615
39616 /* Determine the value of the flags parameter passed to POSIX function
39617 ** open(). These must be calculated even if open() is not called, as
39618 ** they may be stored as part of the file handle and used by the
39619 ** 'conch file' locking functions later on. */
39620 if( isReadonly ) openFlags |= O_RDONLY;
39621 if( isReadWrite ) openFlags |= O_RDWR;
39622 if( isCreate ) openFlags |= O_CREAT;
39623 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
39624 openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW);
39625
39626 if( fd<0 ){
39627 mode_t openMode; /* Permissions to create file with */
39628 uid_t uid; /* Userid for the file */
39629 gid_t gid; /* Groupid for the file */
39630 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
39631 if( rc!=SQLITE_OK ){
39632 assert( !p->pPreallocatedUnused );
39633 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
39634 return rc;
39635 }
39636 fd = robust_open(zName, openFlags, openMode);
39637 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
39638 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
39639 if( fd<0 ){
39640 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
39641 /* If unable to create a journal because the directory is not
39642 ** writable, change the error code to indicate that. */
39643 rc = SQLITE_READONLY_DIRECTORY;
39644 }else if( errno!=EISDIR && isReadWrite ){
39645 /* Failed to open the file for read/write access. Try read-only. */
39646 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
39647 openFlags &= ~(O_RDWR|O_CREAT);
39648 flags |= SQLITE_OPEN_READONLY;
39649 openFlags |= O_RDONLY;
39650 isReadonly = 1;
39651 fd = robust_open(zName, openFlags, openMode);
39652 }
39653 }
39654 if( fd<0 ){
39655 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
39656 if( rc==SQLITE_OK ) rc = rc2;
39657 goto open_finished;
39658 }
39659
39660 /* The owner of the rollback journal or WAL file should always be the
39661 ** same as the owner of the database file. Try to ensure that this is
39662 ** the case. The chown() system call will be a no-op if the current
39663 ** process lacks root privileges, be we should at least try. Without
39664 ** this step, if a root process opens a database file, it can leave
39665 ** behinds a journal/WAL that is owned by root and hence make the
39666 ** database inaccessible to unprivileged processes.
39667 **
39668 ** If openMode==0, then that means uid and gid are not set correctly
39669 ** (probably because SQLite is configured to use 8+3 filename mode) and
39670 ** in that case we do not want to attempt the chown().
39671 */
39672 if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){
39673 robustFchown(fd, uid, gid);
39674 }
39675 }
39676 assert( fd>=0 );
39677 if( pOutFlags ){
39678 *pOutFlags = flags;
39679 }
39680
39681 if( p->pPreallocatedUnused ){
39682 p->pPreallocatedUnused->fd = fd;
39683 p->pPreallocatedUnused->flags =
39684 flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
39685 }
39686
39687 if( isDelete ){
39688#if OS_VXWORKS
39689 zPath = zName;
39690#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
39691 zPath = sqlite3_mprintf("%s", zName);
39692 if( zPath==0 ){
39693 robust_close(p, fd, __LINE__);
39694 return SQLITE_NOMEM_BKPT;
39695 }
39696#else
39697 osUnlink(zName);
39698#endif
39699 }
39700#if SQLITE_ENABLE_LOCKING_STYLE
39701 else{
39702 p->openFlags = openFlags;
39703 }
39704#endif
39705
39706#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
39707 if( fstatfs(fd, &fsInfo) == -1 ){
39708 storeLastErrno(p, errno);
39709 robust_close(p, fd, __LINE__);
39710 return SQLITE_IOERR_ACCESS;
39711 }
39712 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
39713 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
39714 }
39715 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
39716 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
39717 }
39718#endif
39719
39720 /* Set up appropriate ctrlFlags */
39721 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
39722 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
39723 noLock = eType!=SQLITE_OPEN_MAIN_DB;
39724 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
39725 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC;
39726 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
39727
39728#if SQLITE_ENABLE_LOCKING_STYLE
39729#if SQLITE_PREFER_PROXY_LOCKING
39730 isAutoProxy = 1;
39731#endif
39732 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
39733 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
39734 int useProxy = 0;
39735
39736 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
39737 ** never use proxy, NULL means use proxy for non-local files only. */
39738 if( envforce!=NULL ){
39739 useProxy = atoi(envforce)>0;
39740 }else{
39741 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
39742 }
39743 if( useProxy ){
39744 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
39745 if( rc==SQLITE_OK ){
39746 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
39747 if( rc!=SQLITE_OK ){
39748 /* Use unixClose to clean up the resources added in fillInUnixFile
39749 ** and clear all the structure's references. Specifically,
39750 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
39751 */
39752 unixClose(pFile);
39753 return rc;
39754 }
39755 }
39756 goto open_finished;
39757 }
39758 }
39759#endif
39760
39761 assert( zPath==0 || zPath[0]=='/'
39762 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
39763 );
39764 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
39765
39766open_finished:
39767 if( rc!=SQLITE_OK ){
39768 sqlite3_free(p->pPreallocatedUnused);
39769 }
39770 return rc;
39771}
39772
39773
39774/*
39775** Delete the file at zPath. If the dirSync argument is true, fsync()
39776** the directory after deleting the file.
39777*/
39778static int unixDelete(
39779 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
39780 const char *zPath, /* Name of file to be deleted */
39781 int dirSync /* If true, fsync() directory after deleting file */
39782){
39783 int rc = SQLITE_OK;
39784 UNUSED_PARAMETER(NotUsed);
39785 SimulateIOError(return SQLITE_IOERR_DELETE);
39786 if( osUnlink(zPath)==(-1) ){
39787 if( errno==ENOENT
39788#if OS_VXWORKS
39789 || osAccess(zPath,0)!=0
39790#endif
39791 ){
39792 rc = SQLITE_IOERR_DELETE_NOENT;
39793 }else{
39794 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
39795 }
39796 return rc;
39797 }
39798#ifndef SQLITE_DISABLE_DIRSYNC
39799 if( (dirSync & 1)!=0 ){
39800 int fd;
39801 rc = osOpenDirectory(zPath, &fd);
39802 if( rc==SQLITE_OK ){
39803 if( full_fsync(fd,0,0) ){
39804 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
39805 }
39806 robust_close(0, fd, __LINE__);
39807 }else{
39808 assert( rc==SQLITE_CANTOPEN );
39809 rc = SQLITE_OK;
39810 }
39811 }
39812#endif
39813 return rc;
39814}
39815
39816/*
39817** Test the existence of or access permissions of file zPath. The
39818** test performed depends on the value of flags:
39819**
39820** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
39821** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
39822** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
39823**
39824** Otherwise return 0.
39825*/
39826static int unixAccess(
39827 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
39828 const char *zPath, /* Path of the file to examine */
39829 int flags, /* What do we want to learn about the zPath file? */
39830 int *pResOut /* Write result boolean here */
39831){
39832 UNUSED_PARAMETER(NotUsed);
39833 SimulateIOError( return SQLITE_IOERR_ACCESS; );
39834 assert( pResOut!=0 );
39835
39836 /* The spec says there are three possible values for flags. But only
39837 ** two of them are actually used */
39838 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
39839
39840 if( flags==SQLITE_ACCESS_EXISTS ){
39841 struct stat buf;
39842 *pResOut = 0==osStat(zPath, &buf) &&
39843 (!S_ISREG(buf.st_mode) || buf.st_size>0);
39844 }else{
39845 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
39846 }
39847 return SQLITE_OK;
39848}
39849
39850/*
39851**
39852*/
39853static int mkFullPathname(
39854 const char *zPath, /* Input path */
39855 char *zOut, /* Output buffer */
39856 int nOut /* Allocated size of buffer zOut */
39857){
39858 int nPath = sqlite3Strlen30(zPath);
39859 int iOff = 0;
39860 if( zPath[0]!='/' ){
39861 if( osGetcwd(zOut, nOut-2)==0 ){
39862 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
39863 }
39864 iOff = sqlite3Strlen30(zOut);
39865 zOut[iOff++] = '/';
39866 }
39867 if( (iOff+nPath+1)>nOut ){
39868 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
39869 ** even if it returns an error. */
39870 zOut[iOff] = '\0';
39871 return SQLITE_CANTOPEN_BKPT;
39872 }
39873 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
39874 return SQLITE_OK;
39875}
39876
39877/*
39878** Turn a relative pathname into a full pathname. The relative path
39879** is stored as a nul-terminated string in the buffer pointed to by
39880** zPath.
39881**
39882** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
39883** (in this case, MAX_PATHNAME bytes). The full-path is written to
39884** this buffer before returning.
39885*/
39886static int unixFullPathname(
39887 sqlite3_vfs *pVfs, /* Pointer to vfs object */
39888 const char *zPath, /* Possibly relative input path */
39889 int nOut, /* Size of output buffer in bytes */
39890 char *zOut /* Output buffer */
39891){
39892#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
39893 return mkFullPathname(zPath, zOut, nOut);
39894#else
39895 int rc = SQLITE_OK;
39896 int nByte;
39897 int nLink = 0; /* Number of symbolic links followed so far */
39898 const char *zIn = zPath; /* Input path for each iteration of loop */
39899 char *zDel = 0;
39900
39901 assert( pVfs->mxPathname==MAX_PATHNAME );
39902 UNUSED_PARAMETER(pVfs);
39903
39904 /* It's odd to simulate an io-error here, but really this is just
39905 ** using the io-error infrastructure to test that SQLite handles this
39906 ** function failing. This function could fail if, for example, the
39907 ** current working directory has been unlinked.
39908 */
39909 SimulateIOError( return SQLITE_ERROR );
39910
39911 do {
39912
39913 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
39914 ** link, or false otherwise. */
39915 int bLink = 0;
39916 struct stat buf;
39917 if( osLstat(zIn, &buf)!=0 ){
39918 if( errno!=ENOENT ){
39919 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
39920 }
39921 }else{
39922 bLink = S_ISLNK(buf.st_mode);
39923 }
39924
39925 if( bLink ){
39926 nLink++;
39927 if( zDel==0 ){
39928 zDel = sqlite3_malloc(nOut);
39929 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
39930 }else if( nLink>=SQLITE_MAX_SYMLINKS ){
39931 rc = SQLITE_CANTOPEN_BKPT;
39932 }
39933
39934 if( rc==SQLITE_OK ){
39935 nByte = osReadlink(zIn, zDel, nOut-1);
39936 if( nByte<0 ){
39937 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
39938 }else{
39939 if( zDel[0]!='/' ){
39940 int n;
39941 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
39942 if( nByte+n+1>nOut ){
39943 rc = SQLITE_CANTOPEN_BKPT;
39944 }else{
39945 memmove(&zDel[n], zDel, nByte+1);
39946 memcpy(zDel, zIn, n);
39947 nByte += n;
39948 }
39949 }
39950 zDel[nByte] = '\0';
39951 }
39952 }
39953
39954 zIn = zDel;
39955 }
39956
39957 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
39958 if( rc==SQLITE_OK && zIn!=zOut ){
39959 rc = mkFullPathname(zIn, zOut, nOut);
39960 }
39961 if( bLink==0 ) break;
39962 zIn = zOut;
39963 }while( rc==SQLITE_OK );
39964
39965 sqlite3_free(zDel);
39966 if( rc==SQLITE_OK && nLink ) rc = SQLITE_OK_SYMLINK;
39967 return rc;
39968#endif /* HAVE_READLINK && HAVE_LSTAT */
39969}
39970
39971
39972#ifndef SQLITE_OMIT_LOAD_EXTENSION
39973/*
39974** Interfaces for opening a shared library, finding entry points
39975** within the shared library, and closing the shared library.
39976*/
39977#include <dlfcn.h>
39978static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
39979 UNUSED_PARAMETER(NotUsed);
39980 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
39981}
39982
39983/*
39984** SQLite calls this function immediately after a call to unixDlSym() or
39985** unixDlOpen() fails (returns a null pointer). If a more detailed error
39986** message is available, it is written to zBufOut. If no error message
39987** is available, zBufOut is left unmodified and SQLite uses a default
39988** error message.
39989*/
39990static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
39991 const char *zErr;
39992 UNUSED_PARAMETER(NotUsed);
39993 unixEnterMutex();
39994 zErr = dlerror();
39995 if( zErr ){
39996 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
39997 }
39998 unixLeaveMutex();
39999}
40000static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
40001 /*
40002 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
40003 ** cast into a pointer to a function. And yet the library dlsym() routine
40004 ** returns a void* which is really a pointer to a function. So how do we
40005 ** use dlsym() with -pedantic-errors?
40006 **
40007 ** Variable x below is defined to be a pointer to a function taking
40008 ** parameters void* and const char* and returning a pointer to a function.
40009 ** We initialize x by assigning it a pointer to the dlsym() function.
40010 ** (That assignment requires a cast.) Then we call the function that
40011 ** x points to.
40012 **
40013 ** This work-around is unlikely to work correctly on any system where
40014 ** you really cannot cast a function pointer into void*. But then, on the
40015 ** other hand, dlsym() will not work on such a system either, so we have
40016 ** not really lost anything.
40017 */
40018 void (*(*x)(void*,const char*))(void);
40019 UNUSED_PARAMETER(NotUsed);
40020 x = (void(*(*)(void*,const char*))(void))dlsym;
40021 return (*x)(p, zSym);
40022}
40023static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
40024 UNUSED_PARAMETER(NotUsed);
40025 dlclose(pHandle);
40026}
40027#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
40028 #define unixDlOpen 0
40029 #define unixDlError 0
40030 #define unixDlSym 0
40031 #define unixDlClose 0
40032#endif
40033
40034/*
40035** Write nBuf bytes of random data to the supplied buffer zBuf.
40036*/
40037static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
40038 UNUSED_PARAMETER(NotUsed);
40039 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
40040
40041 /* We have to initialize zBuf to prevent valgrind from reporting
40042 ** errors. The reports issued by valgrind are incorrect - we would
40043 ** prefer that the randomness be increased by making use of the
40044 ** uninitialized space in zBuf - but valgrind errors tend to worry
40045 ** some users. Rather than argue, it seems easier just to initialize
40046 ** the whole array and silence valgrind, even if that means less randomness
40047 ** in the random seed.
40048 **
40049 ** When testing, initializing zBuf[] to zero is all we do. That means
40050 ** that we always use the same random number sequence. This makes the
40051 ** tests repeatable.
40052 */
40053 memset(zBuf, 0, nBuf);
40054 randomnessPid = osGetpid(0);
40055#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
40056 {
40057 int fd, got;
40058 fd = robust_open("/dev/urandom", O_RDONLY, 0);
40059 if( fd<0 ){
40060 time_t t;
40061 time(&t);
40062 memcpy(zBuf, &t, sizeof(t));
40063 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
40064 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
40065 nBuf = sizeof(t) + sizeof(randomnessPid);
40066 }else{
40067 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
40068 robust_close(0, fd, __LINE__);
40069 }
40070 }
40071#endif
40072 return nBuf;
40073}
40074
40075
40076/*
40077** Sleep for a little while. Return the amount of time slept.
40078** The argument is the number of microseconds we want to sleep.
40079** The return value is the number of microseconds of sleep actually
40080** requested from the underlying operating system, a number which
40081** might be greater than or equal to the argument, but not less
40082** than the argument.
40083*/
40084static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
40085#if OS_VXWORKS
40086 struct timespec sp;
40087
40088 sp.tv_sec = microseconds / 1000000;
40089 sp.tv_nsec = (microseconds % 1000000) * 1000;
40090 nanosleep(&sp, NULL);
40091 UNUSED_PARAMETER(NotUsed);
40092 return microseconds;
40093#elif defined(HAVE_USLEEP) && HAVE_USLEEP
40094 usleep(microseconds);
40095 UNUSED_PARAMETER(NotUsed);
40096 return microseconds;
40097#else
40098 int seconds = (microseconds+999999)/1000000;
40099 sleep(seconds);
40100 UNUSED_PARAMETER(NotUsed);
40101 return seconds*1000000;
40102#endif
40103}
40104
40105/*
40106** The following variable, if set to a non-zero value, is interpreted as
40107** the number of seconds since 1970 and is used to set the result of
40108** sqlite3OsCurrentTime() during testing.
40109*/
40110#ifdef SQLITE_TEST
40111SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
40112#endif
40113
40114/*
40115** Find the current time (in Universal Coordinated Time). Write into *piNow
40116** the current time and date as a Julian Day number times 86_400_000. In
40117** other words, write into *piNow the number of milliseconds since the Julian
40118** epoch of noon in Greenwich on November 24, 4714 B.C according to the
40119** proleptic Gregorian calendar.
40120**
40121** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
40122** cannot be found.
40123*/
40124static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
40125 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
40126 int rc = SQLITE_OK;
40127#if defined(NO_GETTOD)
40128 time_t t;
40129 time(&t);
40130 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
40131#elif OS_VXWORKS
40132 struct timespec sNow;
40133 clock_gettime(CLOCK_REALTIME, &sNow);
40134 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
40135#else
40136 struct timeval sNow;
40137 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
40138 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
40139#endif
40140
40141#ifdef SQLITE_TEST
40142 if( sqlite3_current_time ){
40143 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
40144 }
40145#endif
40146 UNUSED_PARAMETER(NotUsed);
40147 return rc;
40148}
40149
40150#ifndef SQLITE_OMIT_DEPRECATED
40151/*
40152** Find the current time (in Universal Coordinated Time). Write the
40153** current time and date as a Julian Day number into *prNow and
40154** return 0. Return 1 if the time and date cannot be found.
40155*/
40156static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
40157 sqlite3_int64 i = 0;
40158 int rc;
40159 UNUSED_PARAMETER(NotUsed);
40160 rc = unixCurrentTimeInt64(0, &i);
40161 *prNow = i/86400000.0;
40162 return rc;
40163}
40164#else
40165# define unixCurrentTime 0
40166#endif
40167
40168/*
40169** The xGetLastError() method is designed to return a better
40170** low-level error message when operating-system problems come up
40171** during SQLite operation. Only the integer return code is currently
40172** used.
40173*/
40174static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
40175 UNUSED_PARAMETER(NotUsed);
40176 UNUSED_PARAMETER(NotUsed2);
40177 UNUSED_PARAMETER(NotUsed3);
40178 return errno;
40179}
40180
40181
40182/*
40183************************ End of sqlite3_vfs methods ***************************
40184******************************************************************************/
40185
40186/******************************************************************************
40187************************** Begin Proxy Locking ********************************
40188**
40189** Proxy locking is a "uber-locking-method" in this sense: It uses the
40190** other locking methods on secondary lock files. Proxy locking is a
40191** meta-layer over top of the primitive locking implemented above. For
40192** this reason, the division that implements of proxy locking is deferred
40193** until late in the file (here) after all of the other I/O methods have
40194** been defined - so that the primitive locking methods are available
40195** as services to help with the implementation of proxy locking.
40196**
40197****
40198**
40199** The default locking schemes in SQLite use byte-range locks on the
40200** database file to coordinate safe, concurrent access by multiple readers
40201** and writers [http://sqlite.org/lockingv3.html]. The five file locking
40202** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
40203** as POSIX read & write locks over fixed set of locations (via fsctl),
40204** on AFP and SMB only exclusive byte-range locks are available via fsctl
40205** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
40206** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
40207** address in the shared range is taken for a SHARED lock, the entire
40208** shared range is taken for an EXCLUSIVE lock):
40209**
40210** PENDING_BYTE 0x40000000
40211** RESERVED_BYTE 0x40000001
40212** SHARED_RANGE 0x40000002 -> 0x40000200
40213**
40214** This works well on the local file system, but shows a nearly 100x
40215** slowdown in read performance on AFP because the AFP client disables
40216** the read cache when byte-range locks are present. Enabling the read
40217** cache exposes a cache coherency problem that is present on all OS X
40218** supported network file systems. NFS and AFP both observe the
40219** close-to-open semantics for ensuring cache coherency
40220** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
40221** address the requirements for concurrent database access by multiple
40222** readers and writers
40223** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
40224**
40225** To address the performance and cache coherency issues, proxy file locking
40226** changes the way database access is controlled by limiting access to a
40227** single host at a time and moving file locks off of the database file
40228** and onto a proxy file on the local file system.
40229**
40230**
40231** Using proxy locks
40232** -----------------
40233**
40234** C APIs
40235**
40236** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
40237** <proxy_path> | ":auto:");
40238** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
40239** &<proxy_path>);
40240**
40241**
40242** SQL pragmas
40243**
40244** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
40245** PRAGMA [database.]lock_proxy_file
40246**
40247** Specifying ":auto:" means that if there is a conch file with a matching
40248** host ID in it, the proxy path in the conch file will be used, otherwise
40249** a proxy path based on the user's temp dir
40250** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
40251** actual proxy file name is generated from the name and path of the
40252** database file. For example:
40253**
40254** For database path "/Users/me/foo.db"
40255** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
40256**
40257** Once a lock proxy is configured for a database connection, it can not
40258** be removed, however it may be switched to a different proxy path via
40259** the above APIs (assuming the conch file is not being held by another
40260** connection or process).
40261**
40262**
40263** How proxy locking works
40264** -----------------------
40265**
40266** Proxy file locking relies primarily on two new supporting files:
40267**
40268** * conch file to limit access to the database file to a single host
40269** at a time
40270**
40271** * proxy file to act as a proxy for the advisory locks normally
40272** taken on the database
40273**
40274** The conch file - to use a proxy file, sqlite must first "hold the conch"
40275** by taking an sqlite-style shared lock on the conch file, reading the
40276** contents and comparing the host's unique host ID (see below) and lock
40277** proxy path against the values stored in the conch. The conch file is
40278** stored in the same directory as the database file and the file name
40279** is patterned after the database file name as ".<databasename>-conch".
40280** If the conch file does not exist, or its contents do not match the
40281** host ID and/or proxy path, then the lock is escalated to an exclusive
40282** lock and the conch file contents is updated with the host ID and proxy
40283** path and the lock is downgraded to a shared lock again. If the conch
40284** is held by another process (with a shared lock), the exclusive lock
40285** will fail and SQLITE_BUSY is returned.
40286**
40287** The proxy file - a single-byte file used for all advisory file locks
40288** normally taken on the database file. This allows for safe sharing
40289** of the database file for multiple readers and writers on the same
40290** host (the conch ensures that they all use the same local lock file).
40291**
40292** Requesting the lock proxy does not immediately take the conch, it is
40293** only taken when the first request to lock database file is made.
40294** This matches the semantics of the traditional locking behavior, where
40295** opening a connection to a database file does not take a lock on it.
40296** The shared lock and an open file descriptor are maintained until
40297** the connection to the database is closed.
40298**
40299** The proxy file and the lock file are never deleted so they only need
40300** to be created the first time they are used.
40301**
40302** Configuration options
40303** ---------------------
40304**
40305** SQLITE_PREFER_PROXY_LOCKING
40306**
40307** Database files accessed on non-local file systems are
40308** automatically configured for proxy locking, lock files are
40309** named automatically using the same logic as
40310** PRAGMA lock_proxy_file=":auto:"
40311**
40312** SQLITE_PROXY_DEBUG
40313**
40314** Enables the logging of error messages during host id file
40315** retrieval and creation
40316**
40317** LOCKPROXYDIR
40318**
40319** Overrides the default directory used for lock proxy files that
40320** are named automatically via the ":auto:" setting
40321**
40322** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
40323**
40324** Permissions to use when creating a directory for storing the
40325** lock proxy files, only used when LOCKPROXYDIR is not set.
40326**
40327**
40328** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
40329** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
40330** force proxy locking to be used for every database file opened, and 0
40331** will force automatic proxy locking to be disabled for all database
40332** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
40333** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
40334*/
40335
40336/*
40337** Proxy locking is only available on MacOSX
40338*/
40339#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
40340
40341/*
40342** The proxyLockingContext has the path and file structures for the remote
40343** and local proxy files in it
40344*/
40345typedef struct proxyLockingContext proxyLockingContext;
40346struct proxyLockingContext {
40347 unixFile *conchFile; /* Open conch file */
40348 char *conchFilePath; /* Name of the conch file */
40349 unixFile *lockProxy; /* Open proxy lock file */
40350 char *lockProxyPath; /* Name of the proxy lock file */
40351 char *dbPath; /* Name of the open file */
40352 int conchHeld; /* 1 if the conch is held, -1 if lockless */
40353 int nFails; /* Number of conch taking failures */
40354 void *oldLockingContext; /* Original lockingcontext to restore on close */
40355 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
40356};
40357
40358/*
40359** The proxy lock file path for the database at dbPath is written into lPath,
40360** which must point to valid, writable memory large enough for a maxLen length
40361** file path.
40362*/
40363static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
40364 int len;
40365 int dbLen;
40366 int i;
40367
40368#ifdef LOCKPROXYDIR
40369 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
40370#else
40371# ifdef _CS_DARWIN_USER_TEMP_DIR
40372 {
40373 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
40374 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
40375 lPath, errno, osGetpid(0)));
40376 return SQLITE_IOERR_LOCK;
40377 }
40378 len = strlcat(lPath, "sqliteplocks", maxLen);
40379 }
40380# else
40381 len = strlcpy(lPath, "/tmp/", maxLen);
40382# endif
40383#endif
40384
40385 if( lPath[len-1]!='/' ){
40386 len = strlcat(lPath, "/", maxLen);
40387 }
40388
40389 /* transform the db path to a unique cache name */
40390 dbLen = (int)strlen(dbPath);
40391 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
40392 char c = dbPath[i];
40393 lPath[i+len] = (c=='/')?'_':c;
40394 }
40395 lPath[i+len]='\0';
40396 strlcat(lPath, ":auto:", maxLen);
40397 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
40398 return SQLITE_OK;
40399}
40400
40401/*
40402 ** Creates the lock file and any missing directories in lockPath
40403 */
40404static int proxyCreateLockPath(const char *lockPath){
40405 int i, len;
40406 char buf[MAXPATHLEN];
40407 int start = 0;
40408
40409 assert(lockPath!=NULL);
40410 /* try to create all the intermediate directories */
40411 len = (int)strlen(lockPath);
40412 buf[0] = lockPath[0];
40413 for( i=1; i<len; i++ ){
40414 if( lockPath[i] == '/' && (i - start > 0) ){
40415 /* only mkdir if leaf dir != "." or "/" or ".." */
40416 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
40417 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
40418 buf[i]='\0';
40419 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
40420 int err=errno;
40421 if( err!=EEXIST ) {
40422 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
40423 "'%s' proxy lock path=%s pid=%d\n",
40424 buf, strerror(err), lockPath, osGetpid(0)));
40425 return err;
40426 }
40427 }
40428 }
40429 start=i+1;
40430 }
40431 buf[i] = lockPath[i];
40432 }
40433 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
40434 return 0;
40435}
40436
40437/*
40438** Create a new VFS file descriptor (stored in memory obtained from
40439** sqlite3_malloc) and open the file named "path" in the file descriptor.
40440**
40441** The caller is responsible not only for closing the file descriptor
40442** but also for freeing the memory associated with the file descriptor.
40443*/
40444static int proxyCreateUnixFile(
40445 const char *path, /* path for the new unixFile */
40446 unixFile **ppFile, /* unixFile created and returned by ref */
40447 int islockfile /* if non zero missing dirs will be created */
40448) {
40449 int fd = -1;
40450 unixFile *pNew;
40451 int rc = SQLITE_OK;
40452 int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW;
40453 sqlite3_vfs dummyVfs;
40454 int terrno = 0;
40455 UnixUnusedFd *pUnused = NULL;
40456
40457 /* 1. first try to open/create the file
40458 ** 2. if that fails, and this is a lock file (not-conch), try creating
40459 ** the parent directories and then try again.
40460 ** 3. if that fails, try to open the file read-only
40461 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
40462 */
40463 pUnused = findReusableFd(path, openFlags);
40464 if( pUnused ){
40465 fd = pUnused->fd;
40466 }else{
40467 pUnused = sqlite3_malloc64(sizeof(*pUnused));
40468 if( !pUnused ){
40469 return SQLITE_NOMEM_BKPT;
40470 }
40471 }
40472 if( fd<0 ){
40473 fd = robust_open(path, openFlags, 0);
40474 terrno = errno;
40475 if( fd<0 && errno==ENOENT && islockfile ){
40476 if( proxyCreateLockPath(path) == SQLITE_OK ){
40477 fd = robust_open(path, openFlags, 0);
40478 }
40479 }
40480 }
40481 if( fd<0 ){
40482 openFlags = O_RDONLY | O_NOFOLLOW;
40483 fd = robust_open(path, openFlags, 0);
40484 terrno = errno;
40485 }
40486 if( fd<0 ){
40487 if( islockfile ){
40488 return SQLITE_BUSY;
40489 }
40490 switch (terrno) {
40491 case EACCES:
40492 return SQLITE_PERM;
40493 case EIO:
40494 return SQLITE_IOERR_LOCK; /* even though it is the conch */
40495 default:
40496 return SQLITE_CANTOPEN_BKPT;
40497 }
40498 }
40499
40500 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
40501 if( pNew==NULL ){
40502 rc = SQLITE_NOMEM_BKPT;
40503 goto end_create_proxy;
40504 }
40505 memset(pNew, 0, sizeof(unixFile));
40506 pNew->openFlags = openFlags;
40507 memset(&dummyVfs, 0, sizeof(dummyVfs));
40508 dummyVfs.pAppData = (void*)&autolockIoFinder;
40509 dummyVfs.zName = "dummy";
40510 pUnused->fd = fd;
40511 pUnused->flags = openFlags;
40512 pNew->pPreallocatedUnused = pUnused;
40513
40514 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
40515 if( rc==SQLITE_OK ){
40516 *ppFile = pNew;
40517 return SQLITE_OK;
40518 }
40519end_create_proxy:
40520 robust_close(pNew, fd, __LINE__);
40521 sqlite3_free(pNew);
40522 sqlite3_free(pUnused);
40523 return rc;
40524}
40525
40526#ifdef SQLITE_TEST
40527/* simulate multiple hosts by creating unique hostid file paths */
40528SQLITE_API int sqlite3_hostid_num = 0;
40529#endif
40530
40531#define PROXY_HOSTIDLEN 16 /* conch file host id length */
40532
40533#if HAVE_GETHOSTUUID
40534/* Not always defined in the headers as it ought to be */
40535extern int gethostuuid(uuid_t id, const struct timespec *wait);
40536#endif
40537
40538/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
40539** bytes of writable memory.
40540*/
40541static int proxyGetHostID(unsigned char *pHostID, int *pError){
40542 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
40543 memset(pHostID, 0, PROXY_HOSTIDLEN);
40544#if HAVE_GETHOSTUUID
40545 {
40546 struct timespec timeout = {1, 0}; /* 1 sec timeout */
40547 if( gethostuuid(pHostID, &timeout) ){
40548 int err = errno;
40549 if( pError ){
40550 *pError = err;
40551 }
40552 return SQLITE_IOERR;
40553 }
40554 }
40555#else
40556 UNUSED_PARAMETER(pError);
40557#endif
40558#ifdef SQLITE_TEST
40559 /* simulate multiple hosts by creating unique hostid file paths */
40560 if( sqlite3_hostid_num != 0){
40561 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
40562 }
40563#endif
40564
40565 return SQLITE_OK;
40566}
40567
40568/* The conch file contains the header, host id and lock file path
40569 */
40570#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
40571#define PROXY_HEADERLEN 1 /* conch file header length */
40572#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
40573#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
40574
40575/*
40576** Takes an open conch file, copies the contents to a new path and then moves
40577** it back. The newly created file's file descriptor is assigned to the
40578** conch file structure and finally the original conch file descriptor is
40579** closed. Returns zero if successful.
40580*/
40581static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
40582 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40583 unixFile *conchFile = pCtx->conchFile;
40584 char tPath[MAXPATHLEN];
40585 char buf[PROXY_MAXCONCHLEN];
40586 char *cPath = pCtx->conchFilePath;
40587 size_t readLen = 0;
40588 size_t pathLen = 0;
40589 char errmsg[64] = "";
40590 int fd = -1;
40591 int rc = -1;
40592 UNUSED_PARAMETER(myHostID);
40593
40594 /* create a new path by replace the trailing '-conch' with '-break' */
40595 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
40596 if( pathLen>MAXPATHLEN || pathLen<6 ||
40597 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
40598 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
40599 goto end_breaklock;
40600 }
40601 /* read the conch content */
40602 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
40603 if( readLen<PROXY_PATHINDEX ){
40604 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
40605 goto end_breaklock;
40606 }
40607 /* write it out to the temporary break file */
40608 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0);
40609 if( fd<0 ){
40610 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
40611 goto end_breaklock;
40612 }
40613 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
40614 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
40615 goto end_breaklock;
40616 }
40617 if( rename(tPath, cPath) ){
40618 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
40619 goto end_breaklock;
40620 }
40621 rc = 0;
40622 fprintf(stderr, "broke stale lock on %s\n", cPath);
40623 robust_close(pFile, conchFile->h, __LINE__);
40624 conchFile->h = fd;
40625 conchFile->openFlags = O_RDWR | O_CREAT;
40626
40627end_breaklock:
40628 if( rc ){
40629 if( fd>=0 ){
40630 osUnlink(tPath);
40631 robust_close(pFile, fd, __LINE__);
40632 }
40633 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
40634 }
40635 return rc;
40636}
40637
40638/* Take the requested lock on the conch file and break a stale lock if the
40639** host id matches.
40640*/
40641static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
40642 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40643 unixFile *conchFile = pCtx->conchFile;
40644 int rc = SQLITE_OK;
40645 int nTries = 0;
40646 struct timespec conchModTime;
40647
40648 memset(&conchModTime, 0, sizeof(conchModTime));
40649 do {
40650 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
40651 nTries ++;
40652 if( rc==SQLITE_BUSY ){
40653 /* If the lock failed (busy):
40654 * 1st try: get the mod time of the conch, wait 0.5s and try again.
40655 * 2nd try: fail if the mod time changed or host id is different, wait
40656 * 10 sec and try again
40657 * 3rd try: break the lock unless the mod time has changed.
40658 */
40659 struct stat buf;
40660 if( osFstat(conchFile->h, &buf) ){
40661 storeLastErrno(pFile, errno);
40662 return SQLITE_IOERR_LOCK;
40663 }
40664
40665 if( nTries==1 ){
40666 conchModTime = buf.st_mtimespec;
40667 usleep(500000); /* wait 0.5 sec and try the lock again*/
40668 continue;
40669 }
40670
40671 assert( nTries>1 );
40672 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
40673 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
40674 return SQLITE_BUSY;
40675 }
40676
40677 if( nTries==2 ){
40678 char tBuf[PROXY_MAXCONCHLEN];
40679 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
40680 if( len<0 ){
40681 storeLastErrno(pFile, errno);
40682 return SQLITE_IOERR_LOCK;
40683 }
40684 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
40685 /* don't break the lock if the host id doesn't match */
40686 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
40687 return SQLITE_BUSY;
40688 }
40689 }else{
40690 /* don't break the lock on short read or a version mismatch */
40691 return SQLITE_BUSY;
40692 }
40693 usleep(10000000); /* wait 10 sec and try the lock again */
40694 continue;
40695 }
40696
40697 assert( nTries==3 );
40698 if( 0==proxyBreakConchLock(pFile, myHostID) ){
40699 rc = SQLITE_OK;
40700 if( lockType==EXCLUSIVE_LOCK ){
40701 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
40702 }
40703 if( !rc ){
40704 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
40705 }
40706 }
40707 }
40708 } while( rc==SQLITE_BUSY && nTries<3 );
40709
40710 return rc;
40711}
40712
40713/* Takes the conch by taking a shared lock and read the contents conch, if
40714** lockPath is non-NULL, the host ID and lock file path must match. A NULL
40715** lockPath means that the lockPath in the conch file will be used if the
40716** host IDs match, or a new lock path will be generated automatically
40717** and written to the conch file.
40718*/
40719static int proxyTakeConch(unixFile *pFile){
40720 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40721
40722 if( pCtx->conchHeld!=0 ){
40723 return SQLITE_OK;
40724 }else{
40725 unixFile *conchFile = pCtx->conchFile;
40726 uuid_t myHostID;
40727 int pError = 0;
40728 char readBuf[PROXY_MAXCONCHLEN];
40729 char lockPath[MAXPATHLEN];
40730 char *tempLockPath = NULL;
40731 int rc = SQLITE_OK;
40732 int createConch = 0;
40733 int hostIdMatch = 0;
40734 int readLen = 0;
40735 int tryOldLockPath = 0;
40736 int forceNewLockPath = 0;
40737
40738 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
40739 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
40740 osGetpid(0)));
40741
40742 rc = proxyGetHostID(myHostID, &pError);
40743 if( (rc&0xff)==SQLITE_IOERR ){
40744 storeLastErrno(pFile, pError);
40745 goto end_takeconch;
40746 }
40747 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
40748 if( rc!=SQLITE_OK ){
40749 goto end_takeconch;
40750 }
40751 /* read the existing conch file */
40752 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
40753 if( readLen<0 ){
40754 /* I/O error: lastErrno set by seekAndRead */
40755 storeLastErrno(pFile, conchFile->lastErrno);
40756 rc = SQLITE_IOERR_READ;
40757 goto end_takeconch;
40758 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
40759 readBuf[0]!=(char)PROXY_CONCHVERSION ){
40760 /* a short read or version format mismatch means we need to create a new
40761 ** conch file.
40762 */
40763 createConch = 1;
40764 }
40765 /* if the host id matches and the lock path already exists in the conch
40766 ** we'll try to use the path there, if we can't open that path, we'll
40767 ** retry with a new auto-generated path
40768 */
40769 do { /* in case we need to try again for an :auto: named lock file */
40770
40771 if( !createConch && !forceNewLockPath ){
40772 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
40773 PROXY_HOSTIDLEN);
40774 /* if the conch has data compare the contents */
40775 if( !pCtx->lockProxyPath ){
40776 /* for auto-named local lock file, just check the host ID and we'll
40777 ** use the local lock file path that's already in there
40778 */
40779 if( hostIdMatch ){
40780 size_t pathLen = (readLen - PROXY_PATHINDEX);
40781
40782 if( pathLen>=MAXPATHLEN ){
40783 pathLen=MAXPATHLEN-1;
40784 }
40785 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
40786 lockPath[pathLen] = 0;
40787 tempLockPath = lockPath;
40788 tryOldLockPath = 1;
40789 /* create a copy of the lock path if the conch is taken */
40790 goto end_takeconch;
40791 }
40792 }else if( hostIdMatch
40793 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
40794 readLen-PROXY_PATHINDEX)
40795 ){
40796 /* conch host and lock path match */
40797 goto end_takeconch;
40798 }
40799 }
40800
40801 /* if the conch isn't writable and doesn't match, we can't take it */
40802 if( (conchFile->openFlags&O_RDWR) == 0 ){
40803 rc = SQLITE_BUSY;
40804 goto end_takeconch;
40805 }
40806
40807 /* either the conch didn't match or we need to create a new one */
40808 if( !pCtx->lockProxyPath ){
40809 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
40810 tempLockPath = lockPath;
40811 /* create a copy of the lock path _only_ if the conch is taken */
40812 }
40813
40814 /* update conch with host and path (this will fail if other process
40815 ** has a shared lock already), if the host id matches, use the big
40816 ** stick.
40817 */
40818 futimes(conchFile->h, NULL);
40819 if( hostIdMatch && !createConch ){
40820 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
40821 /* We are trying for an exclusive lock but another thread in this
40822 ** same process is still holding a shared lock. */
40823 rc = SQLITE_BUSY;
40824 } else {
40825 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
40826 }
40827 }else{
40828 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
40829 }
40830 if( rc==SQLITE_OK ){
40831 char writeBuffer[PROXY_MAXCONCHLEN];
40832 int writeSize = 0;
40833
40834 writeBuffer[0] = (char)PROXY_CONCHVERSION;
40835 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
40836 if( pCtx->lockProxyPath!=NULL ){
40837 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
40838 MAXPATHLEN);
40839 }else{
40840 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
40841 }
40842 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
40843 robust_ftruncate(conchFile->h, writeSize);
40844 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
40845 full_fsync(conchFile->h,0,0);
40846 /* If we created a new conch file (not just updated the contents of a
40847 ** valid conch file), try to match the permissions of the database
40848 */
40849 if( rc==SQLITE_OK && createConch ){
40850 struct stat buf;
40851 int err = osFstat(pFile->h, &buf);
40852 if( err==0 ){
40853 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
40854 S_IROTH|S_IWOTH);
40855 /* try to match the database file R/W permissions, ignore failure */
40856#ifndef SQLITE_PROXY_DEBUG
40857 osFchmod(conchFile->h, cmode);
40858#else
40859 do{
40860 rc = osFchmod(conchFile->h, cmode);
40861 }while( rc==(-1) && errno==EINTR );
40862 if( rc!=0 ){
40863 int code = errno;
40864 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
40865 cmode, code, strerror(code));
40866 } else {
40867 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
40868 }
40869 }else{
40870 int code = errno;
40871 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
40872 err, code, strerror(code));
40873#endif
40874 }
40875 }
40876 }
40877 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
40878
40879 end_takeconch:
40880 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
40881 if( rc==SQLITE_OK && pFile->openFlags ){
40882 int fd;
40883 if( pFile->h>=0 ){
40884 robust_close(pFile, pFile->h, __LINE__);
40885 }
40886 pFile->h = -1;
40887 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
40888 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
40889 if( fd>=0 ){
40890 pFile->h = fd;
40891 }else{
40892 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
40893 during locking */
40894 }
40895 }
40896 if( rc==SQLITE_OK && !pCtx->lockProxy ){
40897 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
40898 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
40899 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
40900 /* we couldn't create the proxy lock file with the old lock file path
40901 ** so try again via auto-naming
40902 */
40903 forceNewLockPath = 1;
40904 tryOldLockPath = 0;
40905 continue; /* go back to the do {} while start point, try again */
40906 }
40907 }
40908 if( rc==SQLITE_OK ){
40909 /* Need to make a copy of path if we extracted the value
40910 ** from the conch file or the path was allocated on the stack
40911 */
40912 if( tempLockPath ){
40913 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
40914 if( !pCtx->lockProxyPath ){
40915 rc = SQLITE_NOMEM_BKPT;
40916 }
40917 }
40918 }
40919 if( rc==SQLITE_OK ){
40920 pCtx->conchHeld = 1;
40921
40922 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
40923 afpLockingContext *afpCtx;
40924 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
40925 afpCtx->dbPath = pCtx->lockProxyPath;
40926 }
40927 } else {
40928 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
40929 }
40930 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
40931 rc==SQLITE_OK?"ok":"failed"));
40932 return rc;
40933 } while (1); /* in case we need to retry the :auto: lock file -
40934 ** we should never get here except via the 'continue' call. */
40935 }
40936}
40937
40938/*
40939** If pFile holds a lock on a conch file, then release that lock.
40940*/
40941static int proxyReleaseConch(unixFile *pFile){
40942 int rc = SQLITE_OK; /* Subroutine return code */
40943 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
40944 unixFile *conchFile; /* Name of the conch file */
40945
40946 pCtx = (proxyLockingContext *)pFile->lockingContext;
40947 conchFile = pCtx->conchFile;
40948 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
40949 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
40950 osGetpid(0)));
40951 if( pCtx->conchHeld>0 ){
40952 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
40953 }
40954 pCtx->conchHeld = 0;
40955 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
40956 (rc==SQLITE_OK ? "ok" : "failed")));
40957 return rc;
40958}
40959
40960/*
40961** Given the name of a database file, compute the name of its conch file.
40962** Store the conch filename in memory obtained from sqlite3_malloc64().
40963** Make *pConchPath point to the new name. Return SQLITE_OK on success
40964** or SQLITE_NOMEM if unable to obtain memory.
40965**
40966** The caller is responsible for ensuring that the allocated memory
40967** space is eventually freed.
40968**
40969** *pConchPath is set to NULL if a memory allocation error occurs.
40970*/
40971static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
40972 int i; /* Loop counter */
40973 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
40974 char *conchPath; /* buffer in which to construct conch name */
40975
40976 /* Allocate space for the conch filename and initialize the name to
40977 ** the name of the original database file. */
40978 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
40979 if( conchPath==0 ){
40980 return SQLITE_NOMEM_BKPT;
40981 }
40982 memcpy(conchPath, dbPath, len+1);
40983
40984 /* now insert a "." before the last / character */
40985 for( i=(len-1); i>=0; i-- ){
40986 if( conchPath[i]=='/' ){
40987 i++;
40988 break;
40989 }
40990 }
40991 conchPath[i]='.';
40992 while ( i<len ){
40993 conchPath[i+1]=dbPath[i];
40994 i++;
40995 }
40996
40997 /* append the "-conch" suffix to the file */
40998 memcpy(&conchPath[i+1], "-conch", 7);
40999 assert( (int)strlen(conchPath) == len+7 );
41000
41001 return SQLITE_OK;
41002}
41003
41004
41005/* Takes a fully configured proxy locking-style unix file and switches
41006** the local lock file path
41007*/
41008static int switchLockProxyPath(unixFile *pFile, const char *path) {
41009 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41010 char *oldPath = pCtx->lockProxyPath;
41011 int rc = SQLITE_OK;
41012
41013 if( pFile->eFileLock!=NO_LOCK ){
41014 return SQLITE_BUSY;
41015 }
41016
41017 /* nothing to do if the path is NULL, :auto: or matches the existing path */
41018 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
41019 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
41020 return SQLITE_OK;
41021 }else{
41022 unixFile *lockProxy = pCtx->lockProxy;
41023 pCtx->lockProxy=NULL;
41024 pCtx->conchHeld = 0;
41025 if( lockProxy!=NULL ){
41026 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
41027 if( rc ) return rc;
41028 sqlite3_free(lockProxy);
41029 }
41030 sqlite3_free(oldPath);
41031 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
41032 }
41033
41034 return rc;
41035}
41036
41037/*
41038** pFile is a file that has been opened by a prior xOpen call. dbPath
41039** is a string buffer at least MAXPATHLEN+1 characters in size.
41040**
41041** This routine find the filename associated with pFile and writes it
41042** int dbPath.
41043*/
41044static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
41045#if defined(__APPLE__)
41046 if( pFile->pMethod == &afpIoMethods ){
41047 /* afp style keeps a reference to the db path in the filePath field
41048 ** of the struct */
41049 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41050 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
41051 MAXPATHLEN);
41052 } else
41053#endif
41054 if( pFile->pMethod == &dotlockIoMethods ){
41055 /* dot lock style uses the locking context to store the dot lock
41056 ** file path */
41057 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
41058 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
41059 }else{
41060 /* all other styles use the locking context to store the db file path */
41061 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41062 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
41063 }
41064 return SQLITE_OK;
41065}
41066
41067/*
41068** Takes an already filled in unix file and alters it so all file locking
41069** will be performed on the local proxy lock file. The following fields
41070** are preserved in the locking context so that they can be restored and
41071** the unix structure properly cleaned up at close time:
41072** ->lockingContext
41073** ->pMethod
41074*/
41075static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
41076 proxyLockingContext *pCtx;
41077 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
41078 char *lockPath=NULL;
41079 int rc = SQLITE_OK;
41080
41081 if( pFile->eFileLock!=NO_LOCK ){
41082 return SQLITE_BUSY;
41083 }
41084 proxyGetDbPathForUnixFile(pFile, dbPath);
41085 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
41086 lockPath=NULL;
41087 }else{
41088 lockPath=(char *)path;
41089 }
41090
41091 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
41092 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
41093
41094 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41095 if( pCtx==0 ){
41096 return SQLITE_NOMEM_BKPT;
41097 }
41098 memset(pCtx, 0, sizeof(*pCtx));
41099
41100 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
41101 if( rc==SQLITE_OK ){
41102 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
41103 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
41104 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
41105 ** (c) the file system is read-only, then enable no-locking access.
41106 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
41107 ** that openFlags will have only one of O_RDONLY or O_RDWR.
41108 */
41109 struct statfs fsInfo;
41110 struct stat conchInfo;
41111 int goLockless = 0;
41112
41113 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
41114 int err = errno;
41115 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
41116 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
41117 }
41118 }
41119 if( goLockless ){
41120 pCtx->conchHeld = -1; /* read only FS/ lockless */
41121 rc = SQLITE_OK;
41122 }
41123 }
41124 }
41125 if( rc==SQLITE_OK && lockPath ){
41126 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
41127 }
41128
41129 if( rc==SQLITE_OK ){
41130 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
41131 if( pCtx->dbPath==NULL ){
41132 rc = SQLITE_NOMEM_BKPT;
41133 }
41134 }
41135 if( rc==SQLITE_OK ){
41136 /* all memory is allocated, proxys are created and assigned,
41137 ** switch the locking context and pMethod then return.
41138 */
41139 pCtx->oldLockingContext = pFile->lockingContext;
41140 pFile->lockingContext = pCtx;
41141 pCtx->pOldMethod = pFile->pMethod;
41142 pFile->pMethod = &proxyIoMethods;
41143 }else{
41144 if( pCtx->conchFile ){
41145 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
41146 sqlite3_free(pCtx->conchFile);
41147 }
41148 sqlite3DbFree(0, pCtx->lockProxyPath);
41149 sqlite3_free(pCtx->conchFilePath);
41150 sqlite3_free(pCtx);
41151 }
41152 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
41153 (rc==SQLITE_OK ? "ok" : "failed")));
41154 return rc;
41155}
41156
41157
41158/*
41159** This routine handles sqlite3_file_control() calls that are specific
41160** to proxy locking.
41161*/
41162static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
41163 switch( op ){
41164 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
41165 unixFile *pFile = (unixFile*)id;
41166 if( pFile->pMethod == &proxyIoMethods ){
41167 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41168 proxyTakeConch(pFile);
41169 if( pCtx->lockProxyPath ){
41170 *(const char **)pArg = pCtx->lockProxyPath;
41171 }else{
41172 *(const char **)pArg = ":auto: (not held)";
41173 }
41174 } else {
41175 *(const char **)pArg = NULL;
41176 }
41177 return SQLITE_OK;
41178 }
41179 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
41180 unixFile *pFile = (unixFile*)id;
41181 int rc = SQLITE_OK;
41182 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
41183 if( pArg==NULL || (const char *)pArg==0 ){
41184 if( isProxyStyle ){
41185 /* turn off proxy locking - not supported. If support is added for
41186 ** switching proxy locking mode off then it will need to fail if
41187 ** the journal mode is WAL mode.
41188 */
41189 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
41190 }else{
41191 /* turn off proxy locking - already off - NOOP */
41192 rc = SQLITE_OK;
41193 }
41194 }else{
41195 const char *proxyPath = (const char *)pArg;
41196 if( isProxyStyle ){
41197 proxyLockingContext *pCtx =
41198 (proxyLockingContext*)pFile->lockingContext;
41199 if( !strcmp(pArg, ":auto:")
41200 || (pCtx->lockProxyPath &&
41201 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
41202 ){
41203 rc = SQLITE_OK;
41204 }else{
41205 rc = switchLockProxyPath(pFile, proxyPath);
41206 }
41207 }else{
41208 /* turn on proxy file locking */
41209 rc = proxyTransformUnixFile(pFile, proxyPath);
41210 }
41211 }
41212 return rc;
41213 }
41214 default: {
41215 assert( 0 ); /* The call assures that only valid opcodes are sent */
41216 }
41217 }
41218 /*NOTREACHED*/ assert(0);
41219 return SQLITE_ERROR;
41220}
41221
41222/*
41223** Within this division (the proxying locking implementation) the procedures
41224** above this point are all utilities. The lock-related methods of the
41225** proxy-locking sqlite3_io_method object follow.
41226*/
41227
41228
41229/*
41230** This routine checks if there is a RESERVED lock held on the specified
41231** file by this or any other process. If such a lock is held, set *pResOut
41232** to a non-zero value otherwise *pResOut is set to zero. The return value
41233** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41234*/
41235static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
41236 unixFile *pFile = (unixFile*)id;
41237 int rc = proxyTakeConch(pFile);
41238 if( rc==SQLITE_OK ){
41239 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41240 if( pCtx->conchHeld>0 ){
41241 unixFile *proxy = pCtx->lockProxy;
41242 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
41243 }else{ /* conchHeld < 0 is lockless */
41244 pResOut=0;
41245 }
41246 }
41247 return rc;
41248}
41249
41250/*
41251** Lock the file with the lock specified by parameter eFileLock - one
41252** of the following:
41253**
41254** (1) SHARED_LOCK
41255** (2) RESERVED_LOCK
41256** (3) PENDING_LOCK
41257** (4) EXCLUSIVE_LOCK
41258**
41259** Sometimes when requesting one lock state, additional lock states
41260** are inserted in between. The locking might fail on one of the later
41261** transitions leaving the lock state different from what it started but
41262** still short of its goal. The following chart shows the allowed
41263** transitions and the inserted intermediate states:
41264**
41265** UNLOCKED -> SHARED
41266** SHARED -> RESERVED
41267** SHARED -> (PENDING) -> EXCLUSIVE
41268** RESERVED -> (PENDING) -> EXCLUSIVE
41269** PENDING -> EXCLUSIVE
41270**
41271** This routine will only increase a lock. Use the sqlite3OsUnlock()
41272** routine to lower a locking level.
41273*/
41274static int proxyLock(sqlite3_file *id, int eFileLock) {
41275 unixFile *pFile = (unixFile*)id;
41276 int rc = proxyTakeConch(pFile);
41277 if( rc==SQLITE_OK ){
41278 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41279 if( pCtx->conchHeld>0 ){
41280 unixFile *proxy = pCtx->lockProxy;
41281 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
41282 pFile->eFileLock = proxy->eFileLock;
41283 }else{
41284 /* conchHeld < 0 is lockless */
41285 }
41286 }
41287 return rc;
41288}
41289
41290
41291/*
41292** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41293** must be either NO_LOCK or SHARED_LOCK.
41294**
41295** If the locking level of the file descriptor is already at or below
41296** the requested locking level, this routine is a no-op.
41297*/
41298static int proxyUnlock(sqlite3_file *id, int eFileLock) {
41299 unixFile *pFile = (unixFile*)id;
41300 int rc = proxyTakeConch(pFile);
41301 if( rc==SQLITE_OK ){
41302 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41303 if( pCtx->conchHeld>0 ){
41304 unixFile *proxy = pCtx->lockProxy;
41305 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
41306 pFile->eFileLock = proxy->eFileLock;
41307 }else{
41308 /* conchHeld < 0 is lockless */
41309 }
41310 }
41311 return rc;
41312}
41313
41314/*
41315** Close a file that uses proxy locks.
41316*/
41317static int proxyClose(sqlite3_file *id) {
41318 if( ALWAYS(id) ){
41319 unixFile *pFile = (unixFile*)id;
41320 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41321 unixFile *lockProxy = pCtx->lockProxy;
41322 unixFile *conchFile = pCtx->conchFile;
41323 int rc = SQLITE_OK;
41324
41325 if( lockProxy ){
41326 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
41327 if( rc ) return rc;
41328 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
41329 if( rc ) return rc;
41330 sqlite3_free(lockProxy);
41331 pCtx->lockProxy = 0;
41332 }
41333 if( conchFile ){
41334 if( pCtx->conchHeld ){
41335 rc = proxyReleaseConch(pFile);
41336 if( rc ) return rc;
41337 }
41338 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
41339 if( rc ) return rc;
41340 sqlite3_free(conchFile);
41341 }
41342 sqlite3DbFree(0, pCtx->lockProxyPath);
41343 sqlite3_free(pCtx->conchFilePath);
41344 sqlite3DbFree(0, pCtx->dbPath);
41345 /* restore the original locking context and pMethod then close it */
41346 pFile->lockingContext = pCtx->oldLockingContext;
41347 pFile->pMethod = pCtx->pOldMethod;
41348 sqlite3_free(pCtx);
41349 return pFile->pMethod->xClose(id);
41350 }
41351 return SQLITE_OK;
41352}
41353
41354
41355
41356#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
41357/*
41358** The proxy locking style is intended for use with AFP filesystems.
41359** And since AFP is only supported on MacOSX, the proxy locking is also
41360** restricted to MacOSX.
41361**
41362**
41363******************* End of the proxy lock implementation **********************
41364******************************************************************************/
41365
41366/*
41367** Initialize the operating system interface.
41368**
41369** This routine registers all VFS implementations for unix-like operating
41370** systems. This routine, and the sqlite3_os_end() routine that follows,
41371** should be the only routines in this file that are visible from other
41372** files.
41373**
41374** This routine is called once during SQLite initialization and by a
41375** single thread. The memory allocation and mutex subsystems have not
41376** necessarily been initialized when this routine is called, and so they
41377** should not be used.
41378*/
41379SQLITE_API int sqlite3_os_init(void){
41380 /*
41381 ** The following macro defines an initializer for an sqlite3_vfs object.
41382 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
41383 ** to the "finder" function. (pAppData is a pointer to a pointer because
41384 ** silly C90 rules prohibit a void* from being cast to a function pointer
41385 ** and so we have to go through the intermediate pointer to avoid problems
41386 ** when compiling with -pedantic-errors on GCC.)
41387 **
41388 ** The FINDER parameter to this macro is the name of the pointer to the
41389 ** finder-function. The finder-function returns a pointer to the
41390 ** sqlite_io_methods object that implements the desired locking
41391 ** behaviors. See the division above that contains the IOMETHODS
41392 ** macro for addition information on finder-functions.
41393 **
41394 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
41395 ** object. But the "autolockIoFinder" available on MacOSX does a little
41396 ** more than that; it looks at the filesystem type that hosts the
41397 ** database file and tries to choose an locking method appropriate for
41398 ** that filesystem time.
41399 */
41400 #define UNIXVFS(VFSNAME, FINDER) { \
41401 3, /* iVersion */ \
41402 sizeof(unixFile), /* szOsFile */ \
41403 MAX_PATHNAME, /* mxPathname */ \
41404 0, /* pNext */ \
41405 VFSNAME, /* zName */ \
41406 (void*)&FINDER, /* pAppData */ \
41407 unixOpen, /* xOpen */ \
41408 unixDelete, /* xDelete */ \
41409 unixAccess, /* xAccess */ \
41410 unixFullPathname, /* xFullPathname */ \
41411 unixDlOpen, /* xDlOpen */ \
41412 unixDlError, /* xDlError */ \
41413 unixDlSym, /* xDlSym */ \
41414 unixDlClose, /* xDlClose */ \
41415 unixRandomness, /* xRandomness */ \
41416 unixSleep, /* xSleep */ \
41417 unixCurrentTime, /* xCurrentTime */ \
41418 unixGetLastError, /* xGetLastError */ \
41419 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
41420 unixSetSystemCall, /* xSetSystemCall */ \
41421 unixGetSystemCall, /* xGetSystemCall */ \
41422 unixNextSystemCall, /* xNextSystemCall */ \
41423 }
41424
41425 /*
41426 ** All default VFSes for unix are contained in the following array.
41427 **
41428 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
41429 ** by the SQLite core when the VFS is registered. So the following
41430 ** array cannot be const.
41431 */
41432 static sqlite3_vfs aVfs[] = {
41433#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41434 UNIXVFS("unix", autolockIoFinder ),
41435#elif OS_VXWORKS
41436 UNIXVFS("unix", vxworksIoFinder ),
41437#else
41438 UNIXVFS("unix", posixIoFinder ),
41439#endif
41440 UNIXVFS("unix-none", nolockIoFinder ),
41441 UNIXVFS("unix-dotfile", dotlockIoFinder ),
41442 UNIXVFS("unix-excl", posixIoFinder ),
41443#if OS_VXWORKS
41444 UNIXVFS("unix-namedsem", semIoFinder ),
41445#endif
41446#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
41447 UNIXVFS("unix-posix", posixIoFinder ),
41448#endif
41449#if SQLITE_ENABLE_LOCKING_STYLE
41450 UNIXVFS("unix-flock", flockIoFinder ),
41451#endif
41452#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
41453 UNIXVFS("unix-afp", afpIoFinder ),
41454 UNIXVFS("unix-nfs", nfsIoFinder ),
41455 UNIXVFS("unix-proxy", proxyIoFinder ),
41456#endif
41457 };
41458 unsigned int i; /* Loop counter */
41459
41460 /* Double-check that the aSyscall[] array has been constructed
41461 ** correctly. See ticket [bb3a86e890c8e96ab] */
41462 assert( ArraySize(aSyscall)==29 );
41463
41464 /* Register all VFSes defined in the aVfs[] array */
41465 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
41466 sqlite3_vfs_register(&aVfs[i], i==0);
41467 }
41468 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
41469 return SQLITE_OK;
41470}
41471
41472/*
41473** Shutdown the operating system interface.
41474**
41475** Some operating systems might need to do some cleanup in this routine,
41476** to release dynamically allocated objects. But not on unix.
41477** This routine is a no-op for unix.
41478*/
41479SQLITE_API int sqlite3_os_end(void){
41480 unixBigLock = 0;
41481 return SQLITE_OK;
41482}
41483
41484#endif /* SQLITE_OS_UNIX */
41485
41486/************** End of os_unix.c *********************************************/
41487/************** Begin file os_win.c ******************************************/
41488/*
41489** 2004 May 22
41490**
41491** The author disclaims copyright to this source code. In place of
41492** a legal notice, here is a blessing:
41493**
41494** May you do good and not evil.
41495** May you find forgiveness for yourself and forgive others.
41496** May you share freely, never taking more than you give.
41497**
41498******************************************************************************
41499**
41500** This file contains code that is specific to Windows.
41501*/
41502/* #include "sqliteInt.h" */
41503#if SQLITE_OS_WIN /* This file is used for Windows only */
41504
41505/*
41506** Include code that is common to all os_*.c files
41507*/
41508/************** Include os_common.h in the middle of os_win.c ****************/
41509/************** Begin file os_common.h ***************************************/
41510/*
41511** 2004 May 22
41512**
41513** The author disclaims copyright to this source code. In place of
41514** a legal notice, here is a blessing:
41515**
41516** May you do good and not evil.
41517** May you find forgiveness for yourself and forgive others.
41518** May you share freely, never taking more than you give.
41519**
41520******************************************************************************
41521**
41522** This file contains macros and a little bit of code that is common to
41523** all of the platform-specific files (os_*.c) and is #included into those
41524** files.
41525**
41526** This file should be #included by the os_*.c files only. It is not a
41527** general purpose header file.
41528*/
41529#ifndef _OS_COMMON_H_
41530#define _OS_COMMON_H_
41531
41532/*
41533** At least two bugs have slipped in because we changed the MEMORY_DEBUG
41534** macro to SQLITE_DEBUG and some older makefiles have not yet made the
41535** switch. The following code should catch this problem at compile-time.
41536*/
41537#ifdef MEMORY_DEBUG
41538# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
41539#endif
41540
41541/*
41542** Macros for performance tracing. Normally turned off. Only works
41543** on i486 hardware.
41544*/
41545#ifdef SQLITE_PERFORMANCE_TRACE
41546
41547/*
41548** hwtime.h contains inline assembler code for implementing
41549** high-performance timing routines.
41550*/
41551/************** Include hwtime.h in the middle of os_common.h ****************/
41552/************** Begin file hwtime.h ******************************************/
41553/*
41554** 2008 May 27
41555**
41556** The author disclaims copyright to this source code. In place of
41557** a legal notice, here is a blessing:
41558**
41559** May you do good and not evil.
41560** May you find forgiveness for yourself and forgive others.
41561** May you share freely, never taking more than you give.
41562**
41563******************************************************************************
41564**
41565** This file contains inline asm code for retrieving "high-performance"
41566** counters for x86 and x86_64 class CPUs.
41567*/
41568#ifndef SQLITE_HWTIME_H
41569#define SQLITE_HWTIME_H
41570
41571/*
41572** The following routine only works on pentium-class (or newer) processors.
41573** It uses the RDTSC opcode to read the cycle count value out of the
41574** processor and returns that value. This can be used for high-res
41575** profiling.
41576*/
41577#if !defined(__STRICT_ANSI__) && \
41578 (defined(__GNUC__) || defined(_MSC_VER)) && \
41579 (defined(i386) || defined(__i386__) || defined(_M_IX86))
41580
41581 #if defined(__GNUC__)
41582
41583 __inline__ sqlite_uint64 sqlite3Hwtime(void){
41584 unsigned int lo, hi;
41585 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
41586 return (sqlite_uint64)hi << 32 | lo;
41587 }
41588
41589 #elif defined(_MSC_VER)
41590
41591 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
41592 __asm {
41593 rdtsc
41594 ret ; return value at EDX:EAX
41595 }
41596 }
41597
41598 #endif
41599
41600#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
41601
41602 __inline__ sqlite_uint64 sqlite3Hwtime(void){
41603 unsigned long val;
41604 __asm__ __volatile__ ("rdtsc" : "=A" (val));
41605 return val;
41606 }
41607
41608#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
41609
41610 __inline__ sqlite_uint64 sqlite3Hwtime(void){
41611 unsigned long long retval;
41612 unsigned long junk;
41613 __asm__ __volatile__ ("\n\
41614 1: mftbu %1\n\
41615 mftb %L0\n\
41616 mftbu %0\n\
41617 cmpw %0,%1\n\
41618 bne 1b"
41619 : "=r" (retval), "=r" (junk));
41620 return retval;
41621 }
41622
41623#else
41624
41625 /*
41626 ** asm() is needed for hardware timing support. Without asm(),
41627 ** disable the sqlite3Hwtime() routine.
41628 **
41629 ** sqlite3Hwtime() is only used for some obscure debugging
41630 ** and analysis configurations, not in any deliverable, so this
41631 ** should not be a great loss.
41632 */
41633SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
41634
41635#endif
41636
41637#endif /* !defined(SQLITE_HWTIME_H) */
41638
41639/************** End of hwtime.h **********************************************/
41640/************** Continuing where we left off in os_common.h ******************/
41641
41642static sqlite_uint64 g_start;
41643static sqlite_uint64 g_elapsed;
41644#define TIMER_START g_start=sqlite3Hwtime()
41645#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
41646#define TIMER_ELAPSED g_elapsed
41647#else
41648#define TIMER_START
41649#define TIMER_END
41650#define TIMER_ELAPSED ((sqlite_uint64)0)
41651#endif
41652
41653/*
41654** If we compile with the SQLITE_TEST macro set, then the following block
41655** of code will give us the ability to simulate a disk I/O error. This
41656** is used for testing the I/O recovery logic.
41657*/
41658#if defined(SQLITE_TEST)
41659SQLITE_API extern int sqlite3_io_error_hit;
41660SQLITE_API extern int sqlite3_io_error_hardhit;
41661SQLITE_API extern int sqlite3_io_error_pending;
41662SQLITE_API extern int sqlite3_io_error_persist;
41663SQLITE_API extern int sqlite3_io_error_benign;
41664SQLITE_API extern int sqlite3_diskfull_pending;
41665SQLITE_API extern int sqlite3_diskfull;
41666#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
41667#define SimulateIOError(CODE) \
41668 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
41669 || sqlite3_io_error_pending-- == 1 ) \
41670 { local_ioerr(); CODE; }
41671static void local_ioerr(){
41672 IOTRACE(("IOERR\n"));
41673 sqlite3_io_error_hit++;
41674 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
41675}
41676#define SimulateDiskfullError(CODE) \
41677 if( sqlite3_diskfull_pending ){ \
41678 if( sqlite3_diskfull_pending == 1 ){ \
41679 local_ioerr(); \
41680 sqlite3_diskfull = 1; \
41681 sqlite3_io_error_hit = 1; \
41682 CODE; \
41683 }else{ \
41684 sqlite3_diskfull_pending--; \
41685 } \
41686 }
41687#else
41688#define SimulateIOErrorBenign(X)
41689#define SimulateIOError(A)
41690#define SimulateDiskfullError(A)
41691#endif /* defined(SQLITE_TEST) */
41692
41693/*
41694** When testing, keep a count of the number of open files.
41695*/
41696#if defined(SQLITE_TEST)
41697SQLITE_API extern int sqlite3_open_file_count;
41698#define OpenCounter(X) sqlite3_open_file_count+=(X)
41699#else
41700#define OpenCounter(X)
41701#endif /* defined(SQLITE_TEST) */
41702
41703#endif /* !defined(_OS_COMMON_H_) */
41704
41705/************** End of os_common.h *******************************************/
41706/************** Continuing where we left off in os_win.c *********************/
41707
41708/*
41709** Include the header file for the Windows VFS.
41710*/
41711/* #include "os_win.h" */
41712
41713/*
41714** Compiling and using WAL mode requires several APIs that are only
41715** available in Windows platforms based on the NT kernel.
41716*/
41717#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
41718# error "WAL mode requires support from the Windows NT kernel, compile\
41719 with SQLITE_OMIT_WAL."
41720#endif
41721
41722#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
41723# error "Memory mapped files require support from the Windows NT kernel,\
41724 compile with SQLITE_MAX_MMAP_SIZE=0."
41725#endif
41726
41727/*
41728** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
41729** based on the sub-platform)?
41730*/
41731#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
41732# define SQLITE_WIN32_HAS_ANSI
41733#endif
41734
41735/*
41736** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
41737** based on the sub-platform)?
41738*/
41739#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
41740 !defined(SQLITE_WIN32_NO_WIDE)
41741# define SQLITE_WIN32_HAS_WIDE
41742#endif
41743
41744/*
41745** Make sure at least one set of Win32 APIs is available.
41746*/
41747#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
41748# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
41749 must be defined."
41750#endif
41751
41752/*
41753** Define the required Windows SDK version constants if they are not
41754** already available.
41755*/
41756#ifndef NTDDI_WIN8
41757# define NTDDI_WIN8 0x06020000
41758#endif
41759
41760#ifndef NTDDI_WINBLUE
41761# define NTDDI_WINBLUE 0x06030000
41762#endif
41763
41764#ifndef NTDDI_WINTHRESHOLD
41765# define NTDDI_WINTHRESHOLD 0x06040000
41766#endif
41767
41768/*
41769** Check to see if the GetVersionEx[AW] functions are deprecated on the
41770** target system. GetVersionEx was first deprecated in Win8.1.
41771*/
41772#ifndef SQLITE_WIN32_GETVERSIONEX
41773# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
41774# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
41775# else
41776# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
41777# endif
41778#endif
41779
41780/*
41781** Check to see if the CreateFileMappingA function is supported on the
41782** target system. It is unavailable when using "mincore.lib" on Win10.
41783** When compiling for Windows 10, always assume "mincore.lib" is in use.
41784*/
41785#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
41786# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
41787# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
41788# else
41789# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
41790# endif
41791#endif
41792
41793/*
41794** This constant should already be defined (in the "WinDef.h" SDK file).
41795*/
41796#ifndef MAX_PATH
41797# define MAX_PATH (260)
41798#endif
41799
41800/*
41801** Maximum pathname length (in chars) for Win32. This should normally be
41802** MAX_PATH.
41803*/
41804#ifndef SQLITE_WIN32_MAX_PATH_CHARS
41805# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
41806#endif
41807
41808/*
41809** This constant should already be defined (in the "WinNT.h" SDK file).
41810*/
41811#ifndef UNICODE_STRING_MAX_CHARS
41812# define UNICODE_STRING_MAX_CHARS (32767)
41813#endif
41814
41815/*
41816** Maximum pathname length (in chars) for WinNT. This should normally be
41817** UNICODE_STRING_MAX_CHARS.
41818*/
41819#ifndef SQLITE_WINNT_MAX_PATH_CHARS
41820# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
41821#endif
41822
41823/*
41824** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
41825** characters, so we allocate 4 bytes per character assuming worst-case of
41826** 4-bytes-per-character for UTF8.
41827*/
41828#ifndef SQLITE_WIN32_MAX_PATH_BYTES
41829# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
41830#endif
41831
41832/*
41833** Maximum pathname length (in bytes) for WinNT. This should normally be
41834** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
41835*/
41836#ifndef SQLITE_WINNT_MAX_PATH_BYTES
41837# define SQLITE_WINNT_MAX_PATH_BYTES \
41838 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
41839#endif
41840
41841/*
41842** Maximum error message length (in chars) for WinRT.
41843*/
41844#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
41845# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
41846#endif
41847
41848/*
41849** Returns non-zero if the character should be treated as a directory
41850** separator.
41851*/
41852#ifndef winIsDirSep
41853# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
41854#endif
41855
41856/*
41857** This macro is used when a local variable is set to a value that is
41858** [sometimes] not used by the code (e.g. via conditional compilation).
41859*/
41860#ifndef UNUSED_VARIABLE_VALUE
41861# define UNUSED_VARIABLE_VALUE(x) (void)(x)
41862#endif
41863
41864/*
41865** Returns the character that should be used as the directory separator.
41866*/
41867#ifndef winGetDirSep
41868# define winGetDirSep() '\\'
41869#endif
41870
41871/*
41872** Do we need to manually define the Win32 file mapping APIs for use with WAL
41873** mode or memory mapped files (e.g. these APIs are available in the Windows
41874** CE SDK; however, they are not present in the header file)?
41875*/
41876#if SQLITE_WIN32_FILEMAPPING_API && \
41877 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41878/*
41879** Two of the file mapping APIs are different under WinRT. Figure out which
41880** set we need.
41881*/
41882#if SQLITE_OS_WINRT
41883WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
41884 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
41885
41886WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
41887#else
41888#if defined(SQLITE_WIN32_HAS_ANSI)
41889WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
41890 DWORD, DWORD, DWORD, LPCSTR);
41891#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
41892
41893#if defined(SQLITE_WIN32_HAS_WIDE)
41894WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
41895 DWORD, DWORD, DWORD, LPCWSTR);
41896#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
41897
41898WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
41899#endif /* SQLITE_OS_WINRT */
41900
41901/*
41902** These file mapping APIs are common to both Win32 and WinRT.
41903*/
41904
41905WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
41906WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
41907#endif /* SQLITE_WIN32_FILEMAPPING_API */
41908
41909/*
41910** Some Microsoft compilers lack this definition.
41911*/
41912#ifndef INVALID_FILE_ATTRIBUTES
41913# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
41914#endif
41915
41916#ifndef FILE_FLAG_MASK
41917# define FILE_FLAG_MASK (0xFF3C0000)
41918#endif
41919
41920#ifndef FILE_ATTRIBUTE_MASK
41921# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
41922#endif
41923
41924#ifndef SQLITE_OMIT_WAL
41925/* Forward references to structures used for WAL */
41926typedef struct winShm winShm; /* A connection to shared-memory */
41927typedef struct winShmNode winShmNode; /* A region of shared-memory */
41928#endif
41929
41930/*
41931** WinCE lacks native support for file locking so we have to fake it
41932** with some code of our own.
41933*/
41934#if SQLITE_OS_WINCE
41935typedef struct winceLock {
41936 int nReaders; /* Number of reader locks obtained */
41937 BOOL bPending; /* Indicates a pending lock has been obtained */
41938 BOOL bReserved; /* Indicates a reserved lock has been obtained */
41939 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
41940} winceLock;
41941#endif
41942
41943/*
41944** The winFile structure is a subclass of sqlite3_file* specific to the win32
41945** portability layer.
41946*/
41947typedef struct winFile winFile;
41948struct winFile {
41949 const sqlite3_io_methods *pMethod; /*** Must be first ***/
41950 sqlite3_vfs *pVfs; /* The VFS used to open this file */
41951 HANDLE h; /* Handle for accessing the file */
41952 u8 locktype; /* Type of lock currently held on this file */
41953 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
41954 u8 ctrlFlags; /* Flags. See WINFILE_* below */
41955 DWORD lastErrno; /* The Windows errno from the last I/O error */
41956#ifndef SQLITE_OMIT_WAL
41957 winShm *pShm; /* Instance of shared memory on this file */
41958#endif
41959 const char *zPath; /* Full pathname of this file */
41960 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
41961#if SQLITE_OS_WINCE
41962 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
41963 HANDLE hMutex; /* Mutex used to control access to shared lock */
41964 HANDLE hShared; /* Shared memory segment used for locking */
41965 winceLock local; /* Locks obtained by this instance of winFile */
41966 winceLock *shared; /* Global shared lock memory for the file */
41967#endif
41968#if SQLITE_MAX_MMAP_SIZE>0
41969 int nFetchOut; /* Number of outstanding xFetch references */
41970 HANDLE hMap; /* Handle for accessing memory mapping */
41971 void *pMapRegion; /* Area memory mapped */
41972 sqlite3_int64 mmapSize; /* Size of mapped region */
41973 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
41974#endif
41975};
41976
41977/*
41978** The winVfsAppData structure is used for the pAppData member for all of the
41979** Win32 VFS variants.
41980*/
41981typedef struct winVfsAppData winVfsAppData;
41982struct winVfsAppData {
41983 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
41984 void *pAppData; /* The extra pAppData, if any. */
41985 BOOL bNoLock; /* Non-zero if locking is disabled. */
41986};
41987
41988/*
41989** Allowed values for winFile.ctrlFlags
41990*/
41991#define WINFILE_RDONLY 0x02 /* Connection is read only */
41992#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
41993#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
41994
41995/*
41996 * The size of the buffer used by sqlite3_win32_write_debug().
41997 */
41998#ifndef SQLITE_WIN32_DBG_BUF_SIZE
41999# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
42000#endif
42001
42002/*
42003 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
42004 * various Win32 API heap functions instead of our own.
42005 */
42006#ifdef SQLITE_WIN32_MALLOC
42007
42008/*
42009 * If this is non-zero, an isolated heap will be created by the native Win32
42010 * allocator subsystem; otherwise, the default process heap will be used. This
42011 * setting has no effect when compiling for WinRT. By default, this is enabled
42012 * and an isolated heap will be created to store all allocated data.
42013 *
42014 ******************************************************************************
42015 * WARNING: It is important to note that when this setting is non-zero and the
42016 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
42017 * function), all data that was allocated using the isolated heap will
42018 * be freed immediately and any attempt to access any of that freed
42019 * data will almost certainly result in an immediate access violation.
42020 ******************************************************************************
42021 */
42022#ifndef SQLITE_WIN32_HEAP_CREATE
42023# define SQLITE_WIN32_HEAP_CREATE (TRUE)
42024#endif
42025
42026/*
42027 * This is the maximum possible initial size of the Win32-specific heap, in
42028 * bytes.
42029 */
42030#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
42031# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
42032#endif
42033
42034/*
42035 * This is the extra space for the initial size of the Win32-specific heap,
42036 * in bytes. This value may be zero.
42037 */
42038#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
42039# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
42040#endif
42041
42042/*
42043 * Calculate the maximum legal cache size, in pages, based on the maximum
42044 * possible initial heap size and the default page size, setting aside the
42045 * needed extra space.
42046 */
42047#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
42048# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
42049 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
42050 (SQLITE_DEFAULT_PAGE_SIZE))
42051#endif
42052
42053/*
42054 * This is cache size used in the calculation of the initial size of the
42055 * Win32-specific heap. It cannot be negative.
42056 */
42057#ifndef SQLITE_WIN32_CACHE_SIZE
42058# if SQLITE_DEFAULT_CACHE_SIZE>=0
42059# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
42060# else
42061# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
42062# endif
42063#endif
42064
42065/*
42066 * Make sure that the calculated cache size, in pages, cannot cause the
42067 * initial size of the Win32-specific heap to exceed the maximum amount
42068 * of memory that can be specified in the call to HeapCreate.
42069 */
42070#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
42071# undef SQLITE_WIN32_CACHE_SIZE
42072# define SQLITE_WIN32_CACHE_SIZE (2000)
42073#endif
42074
42075/*
42076 * The initial size of the Win32-specific heap. This value may be zero.
42077 */
42078#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
42079# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
42080 (SQLITE_DEFAULT_PAGE_SIZE) + \
42081 (SQLITE_WIN32_HEAP_INIT_EXTRA))
42082#endif
42083
42084/*
42085 * The maximum size of the Win32-specific heap. This value may be zero.
42086 */
42087#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
42088# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
42089#endif
42090
42091/*
42092 * The extra flags to use in calls to the Win32 heap APIs. This value may be
42093 * zero for the default behavior.
42094 */
42095#ifndef SQLITE_WIN32_HEAP_FLAGS
42096# define SQLITE_WIN32_HEAP_FLAGS (0)
42097#endif
42098
42099
42100/*
42101** The winMemData structure stores information required by the Win32-specific
42102** sqlite3_mem_methods implementation.
42103*/
42104typedef struct winMemData winMemData;
42105struct winMemData {
42106#ifndef NDEBUG
42107 u32 magic1; /* Magic number to detect structure corruption. */
42108#endif
42109 HANDLE hHeap; /* The handle to our heap. */
42110 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
42111#ifndef NDEBUG
42112 u32 magic2; /* Magic number to detect structure corruption. */
42113#endif
42114};
42115
42116#ifndef NDEBUG
42117#define WINMEM_MAGIC1 0x42b2830b
42118#define WINMEM_MAGIC2 0xbd4d7cf4
42119#endif
42120
42121static struct winMemData win_mem_data = {
42122#ifndef NDEBUG
42123 WINMEM_MAGIC1,
42124#endif
42125 NULL, FALSE
42126#ifndef NDEBUG
42127 ,WINMEM_MAGIC2
42128#endif
42129};
42130
42131#ifndef NDEBUG
42132#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
42133#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
42134#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
42135#else
42136#define winMemAssertMagic()
42137#endif
42138
42139#define winMemGetDataPtr() &win_mem_data
42140#define winMemGetHeap() win_mem_data.hHeap
42141#define winMemGetOwned() win_mem_data.bOwned
42142
42143static void *winMemMalloc(int nBytes);
42144static void winMemFree(void *pPrior);
42145static void *winMemRealloc(void *pPrior, int nBytes);
42146static int winMemSize(void *p);
42147static int winMemRoundup(int n);
42148static int winMemInit(void *pAppData);
42149static void winMemShutdown(void *pAppData);
42150
42151SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
42152#endif /* SQLITE_WIN32_MALLOC */
42153
42154/*
42155** The following variable is (normally) set once and never changes
42156** thereafter. It records whether the operating system is Win9x
42157** or WinNT.
42158**
42159** 0: Operating system unknown.
42160** 1: Operating system is Win9x.
42161** 2: Operating system is WinNT.
42162**
42163** In order to facilitate testing on a WinNT system, the test fixture
42164** can manually set this value to 1 to emulate Win98 behavior.
42165*/
42166#ifdef SQLITE_TEST
42167SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42168#else
42169static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
42170#endif
42171
42172#ifndef SYSCALL
42173# define SYSCALL sqlite3_syscall_ptr
42174#endif
42175
42176/*
42177** This function is not available on Windows CE or WinRT.
42178 */
42179
42180#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
42181# define osAreFileApisANSI() 1
42182#endif
42183
42184/*
42185** Many system calls are accessed through pointer-to-functions so that
42186** they may be overridden at runtime to facilitate fault injection during
42187** testing and sandboxing. The following array holds the names and pointers
42188** to all overrideable system calls.
42189*/
42190static struct win_syscall {
42191 const char *zName; /* Name of the system call */
42192 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
42193 sqlite3_syscall_ptr pDefault; /* Default value */
42194} aSyscall[] = {
42195#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42196 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
42197#else
42198 { "AreFileApisANSI", (SYSCALL)0, 0 },
42199#endif
42200
42201#ifndef osAreFileApisANSI
42202#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
42203#endif
42204
42205#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42206 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
42207#else
42208 { "CharLowerW", (SYSCALL)0, 0 },
42209#endif
42210
42211#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
42212
42213#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
42214 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
42215#else
42216 { "CharUpperW", (SYSCALL)0, 0 },
42217#endif
42218
42219#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
42220
42221 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
42222
42223#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
42224
42225#if defined(SQLITE_WIN32_HAS_ANSI)
42226 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
42227#else
42228 { "CreateFileA", (SYSCALL)0, 0 },
42229#endif
42230
42231#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
42232 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
42233
42234#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42235 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
42236#else
42237 { "CreateFileW", (SYSCALL)0, 0 },
42238#endif
42239
42240#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
42241 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
42242
42243#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
42244 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
42245 SQLITE_WIN32_CREATEFILEMAPPINGA
42246 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
42247#else
42248 { "CreateFileMappingA", (SYSCALL)0, 0 },
42249#endif
42250
42251#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42252 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
42253
42254#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42255 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
42256 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
42257#else
42258 { "CreateFileMappingW", (SYSCALL)0, 0 },
42259#endif
42260
42261#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
42262 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
42263
42264#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42265 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
42266#else
42267 { "CreateMutexW", (SYSCALL)0, 0 },
42268#endif
42269
42270#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
42271 LPCWSTR))aSyscall[8].pCurrent)
42272
42273#if defined(SQLITE_WIN32_HAS_ANSI)
42274 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
42275#else
42276 { "DeleteFileA", (SYSCALL)0, 0 },
42277#endif
42278
42279#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
42280
42281#if defined(SQLITE_WIN32_HAS_WIDE)
42282 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
42283#else
42284 { "DeleteFileW", (SYSCALL)0, 0 },
42285#endif
42286
42287#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
42288
42289#if SQLITE_OS_WINCE
42290 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
42291#else
42292 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
42293#endif
42294
42295#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42296 LPFILETIME))aSyscall[11].pCurrent)
42297
42298#if SQLITE_OS_WINCE
42299 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
42300#else
42301 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
42302#endif
42303
42304#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
42305 LPSYSTEMTIME))aSyscall[12].pCurrent)
42306
42307 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
42308
42309#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
42310
42311#if defined(SQLITE_WIN32_HAS_ANSI)
42312 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
42313#else
42314 { "FormatMessageA", (SYSCALL)0, 0 },
42315#endif
42316
42317#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
42318 DWORD,va_list*))aSyscall[14].pCurrent)
42319
42320#if defined(SQLITE_WIN32_HAS_WIDE)
42321 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
42322#else
42323 { "FormatMessageW", (SYSCALL)0, 0 },
42324#endif
42325
42326#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
42327 DWORD,va_list*))aSyscall[15].pCurrent)
42328
42329#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
42330 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
42331#else
42332 { "FreeLibrary", (SYSCALL)0, 0 },
42333#endif
42334
42335#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
42336
42337 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
42338
42339#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
42340
42341#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
42342 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
42343#else
42344 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
42345#endif
42346
42347#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
42348 LPDWORD))aSyscall[18].pCurrent)
42349
42350#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42351 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
42352#else
42353 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
42354#endif
42355
42356#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
42357 LPDWORD))aSyscall[19].pCurrent)
42358
42359#if defined(SQLITE_WIN32_HAS_ANSI)
42360 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
42361#else
42362 { "GetFileAttributesA", (SYSCALL)0, 0 },
42363#endif
42364
42365#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
42366
42367#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42368 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
42369#else
42370 { "GetFileAttributesW", (SYSCALL)0, 0 },
42371#endif
42372
42373#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
42374
42375#if defined(SQLITE_WIN32_HAS_WIDE)
42376 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
42377#else
42378 { "GetFileAttributesExW", (SYSCALL)0, 0 },
42379#endif
42380
42381#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
42382 LPVOID))aSyscall[22].pCurrent)
42383
42384#if !SQLITE_OS_WINRT
42385 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
42386#else
42387 { "GetFileSize", (SYSCALL)0, 0 },
42388#endif
42389
42390#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
42391
42392#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
42393 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
42394#else
42395 { "GetFullPathNameA", (SYSCALL)0, 0 },
42396#endif
42397
42398#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
42399 LPSTR*))aSyscall[24].pCurrent)
42400
42401#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42402 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
42403#else
42404 { "GetFullPathNameW", (SYSCALL)0, 0 },
42405#endif
42406
42407#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
42408 LPWSTR*))aSyscall[25].pCurrent)
42409
42410 { "GetLastError", (SYSCALL)GetLastError, 0 },
42411
42412#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
42413
42414#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
42415#if SQLITE_OS_WINCE
42416 /* The GetProcAddressA() routine is only available on Windows CE. */
42417 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
42418#else
42419 /* All other Windows platforms expect GetProcAddress() to take
42420 ** an ANSI string regardless of the _UNICODE setting */
42421 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
42422#endif
42423#else
42424 { "GetProcAddressA", (SYSCALL)0, 0 },
42425#endif
42426
42427#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
42428 LPCSTR))aSyscall[27].pCurrent)
42429
42430#if !SQLITE_OS_WINRT
42431 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
42432#else
42433 { "GetSystemInfo", (SYSCALL)0, 0 },
42434#endif
42435
42436#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
42437
42438 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
42439
42440#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
42441
42442#if !SQLITE_OS_WINCE
42443 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
42444#else
42445 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
42446#endif
42447
42448#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
42449 LPFILETIME))aSyscall[30].pCurrent)
42450
42451#if defined(SQLITE_WIN32_HAS_ANSI)
42452 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
42453#else
42454 { "GetTempPathA", (SYSCALL)0, 0 },
42455#endif
42456
42457#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
42458
42459#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
42460 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
42461#else
42462 { "GetTempPathW", (SYSCALL)0, 0 },
42463#endif
42464
42465#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
42466
42467#if !SQLITE_OS_WINRT
42468 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
42469#else
42470 { "GetTickCount", (SYSCALL)0, 0 },
42471#endif
42472
42473#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
42474
42475#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
42476 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
42477#else
42478 { "GetVersionExA", (SYSCALL)0, 0 },
42479#endif
42480
42481#define osGetVersionExA ((BOOL(WINAPI*)( \
42482 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
42483
42484#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42485 SQLITE_WIN32_GETVERSIONEX
42486 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
42487#else
42488 { "GetVersionExW", (SYSCALL)0, 0 },
42489#endif
42490
42491#define osGetVersionExW ((BOOL(WINAPI*)( \
42492 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
42493
42494 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
42495
42496#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
42497 SIZE_T))aSyscall[36].pCurrent)
42498
42499#if !SQLITE_OS_WINRT
42500 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
42501#else
42502 { "HeapCreate", (SYSCALL)0, 0 },
42503#endif
42504
42505#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
42506 SIZE_T))aSyscall[37].pCurrent)
42507
42508#if !SQLITE_OS_WINRT
42509 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
42510#else
42511 { "HeapDestroy", (SYSCALL)0, 0 },
42512#endif
42513
42514#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
42515
42516 { "HeapFree", (SYSCALL)HeapFree, 0 },
42517
42518#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
42519
42520 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
42521
42522#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
42523 SIZE_T))aSyscall[40].pCurrent)
42524
42525 { "HeapSize", (SYSCALL)HeapSize, 0 },
42526
42527#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
42528 LPCVOID))aSyscall[41].pCurrent)
42529
42530#if !SQLITE_OS_WINRT
42531 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
42532#else
42533 { "HeapValidate", (SYSCALL)0, 0 },
42534#endif
42535
42536#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
42537 LPCVOID))aSyscall[42].pCurrent)
42538
42539#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42540 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
42541#else
42542 { "HeapCompact", (SYSCALL)0, 0 },
42543#endif
42544
42545#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
42546
42547#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
42548 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
42549#else
42550 { "LoadLibraryA", (SYSCALL)0, 0 },
42551#endif
42552
42553#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
42554
42555#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
42556 !defined(SQLITE_OMIT_LOAD_EXTENSION)
42557 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
42558#else
42559 { "LoadLibraryW", (SYSCALL)0, 0 },
42560#endif
42561
42562#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
42563
42564#if !SQLITE_OS_WINRT
42565 { "LocalFree", (SYSCALL)LocalFree, 0 },
42566#else
42567 { "LocalFree", (SYSCALL)0, 0 },
42568#endif
42569
42570#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
42571
42572#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42573 { "LockFile", (SYSCALL)LockFile, 0 },
42574#else
42575 { "LockFile", (SYSCALL)0, 0 },
42576#endif
42577
42578#ifndef osLockFile
42579#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42580 DWORD))aSyscall[47].pCurrent)
42581#endif
42582
42583#if !SQLITE_OS_WINCE
42584 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
42585#else
42586 { "LockFileEx", (SYSCALL)0, 0 },
42587#endif
42588
42589#ifndef osLockFileEx
42590#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
42591 LPOVERLAPPED))aSyscall[48].pCurrent)
42592#endif
42593
42594#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
42595 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
42596 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
42597#else
42598 { "MapViewOfFile", (SYSCALL)0, 0 },
42599#endif
42600
42601#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42602 SIZE_T))aSyscall[49].pCurrent)
42603
42604 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
42605
42606#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
42607 int))aSyscall[50].pCurrent)
42608
42609 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
42610
42611#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
42612 LARGE_INTEGER*))aSyscall[51].pCurrent)
42613
42614 { "ReadFile", (SYSCALL)ReadFile, 0 },
42615
42616#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
42617 LPOVERLAPPED))aSyscall[52].pCurrent)
42618
42619 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
42620
42621#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
42622
42623#if !SQLITE_OS_WINRT
42624 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
42625#else
42626 { "SetFilePointer", (SYSCALL)0, 0 },
42627#endif
42628
42629#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
42630 DWORD))aSyscall[54].pCurrent)
42631
42632#if !SQLITE_OS_WINRT
42633 { "Sleep", (SYSCALL)Sleep, 0 },
42634#else
42635 { "Sleep", (SYSCALL)0, 0 },
42636#endif
42637
42638#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
42639
42640 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
42641
42642#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
42643 LPFILETIME))aSyscall[56].pCurrent)
42644
42645#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42646 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
42647#else
42648 { "UnlockFile", (SYSCALL)0, 0 },
42649#endif
42650
42651#ifndef osUnlockFile
42652#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42653 DWORD))aSyscall[57].pCurrent)
42654#endif
42655
42656#if !SQLITE_OS_WINCE
42657 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
42658#else
42659 { "UnlockFileEx", (SYSCALL)0, 0 },
42660#endif
42661
42662#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
42663 LPOVERLAPPED))aSyscall[58].pCurrent)
42664
42665#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
42666 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
42667#else
42668 { "UnmapViewOfFile", (SYSCALL)0, 0 },
42669#endif
42670
42671#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
42672
42673 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
42674
42675#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
42676 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
42677
42678 { "WriteFile", (SYSCALL)WriteFile, 0 },
42679
42680#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
42681 LPOVERLAPPED))aSyscall[61].pCurrent)
42682
42683#if SQLITE_OS_WINRT
42684 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
42685#else
42686 { "CreateEventExW", (SYSCALL)0, 0 },
42687#endif
42688
42689#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
42690 DWORD,DWORD))aSyscall[62].pCurrent)
42691
42692#if !SQLITE_OS_WINRT
42693 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
42694#else
42695 { "WaitForSingleObject", (SYSCALL)0, 0 },
42696#endif
42697
42698#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
42699 DWORD))aSyscall[63].pCurrent)
42700
42701#if !SQLITE_OS_WINCE
42702 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
42703#else
42704 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
42705#endif
42706
42707#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
42708 BOOL))aSyscall[64].pCurrent)
42709
42710#if SQLITE_OS_WINRT
42711 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
42712#else
42713 { "SetFilePointerEx", (SYSCALL)0, 0 },
42714#endif
42715
42716#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
42717 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
42718
42719#if SQLITE_OS_WINRT
42720 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
42721#else
42722 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
42723#endif
42724
42725#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
42726 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
42727
42728#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
42729 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
42730#else
42731 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
42732#endif
42733
42734#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
42735 SIZE_T))aSyscall[67].pCurrent)
42736
42737#if SQLITE_OS_WINRT
42738 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
42739#else
42740 { "CreateFile2", (SYSCALL)0, 0 },
42741#endif
42742
42743#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
42744 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
42745
42746#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
42747 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
42748#else
42749 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
42750#endif
42751
42752#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
42753 DWORD))aSyscall[69].pCurrent)
42754
42755#if SQLITE_OS_WINRT
42756 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
42757#else
42758 { "GetTickCount64", (SYSCALL)0, 0 },
42759#endif
42760
42761#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
42762
42763#if SQLITE_OS_WINRT
42764 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
42765#else
42766 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
42767#endif
42768
42769#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
42770 LPSYSTEM_INFO))aSyscall[71].pCurrent)
42771
42772#if defined(SQLITE_WIN32_HAS_ANSI)
42773 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
42774#else
42775 { "OutputDebugStringA", (SYSCALL)0, 0 },
42776#endif
42777
42778#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
42779
42780#if defined(SQLITE_WIN32_HAS_WIDE)
42781 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
42782#else
42783 { "OutputDebugStringW", (SYSCALL)0, 0 },
42784#endif
42785
42786#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
42787
42788 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
42789
42790#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
42791
42792#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
42793 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
42794#else
42795 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
42796#endif
42797
42798#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
42799 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
42800
42801/*
42802** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
42803** is really just a macro that uses a compiler intrinsic (e.g. x64).
42804** So do not try to make this is into a redefinable interface.
42805*/
42806#if defined(InterlockedCompareExchange)
42807 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
42808
42809#define osInterlockedCompareExchange InterlockedCompareExchange
42810#else
42811 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
42812
42813#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
42814 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
42815#endif /* defined(InterlockedCompareExchange) */
42816
42817#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
42818 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
42819#else
42820 { "UuidCreate", (SYSCALL)0, 0 },
42821#endif
42822
42823#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
42824
42825#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
42826 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
42827#else
42828 { "UuidCreateSequential", (SYSCALL)0, 0 },
42829#endif
42830
42831#define osUuidCreateSequential \
42832 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
42833
42834#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
42835 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
42836#else
42837 { "FlushViewOfFile", (SYSCALL)0, 0 },
42838#endif
42839
42840#define osFlushViewOfFile \
42841 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
42842
42843}; /* End of the overrideable system calls */
42844
42845/*
42846** This is the xSetSystemCall() method of sqlite3_vfs for all of the
42847** "win32" VFSes. Return SQLITE_OK opon successfully updating the
42848** system call pointer, or SQLITE_NOTFOUND if there is no configurable
42849** system call named zName.
42850*/
42851static int winSetSystemCall(
42852 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
42853 const char *zName, /* Name of system call to override */
42854 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
42855){
42856 unsigned int i;
42857 int rc = SQLITE_NOTFOUND;
42858
42859 UNUSED_PARAMETER(pNotUsed);
42860 if( zName==0 ){
42861 /* If no zName is given, restore all system calls to their default
42862 ** settings and return NULL
42863 */
42864 rc = SQLITE_OK;
42865 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42866 if( aSyscall[i].pDefault ){
42867 aSyscall[i].pCurrent = aSyscall[i].pDefault;
42868 }
42869 }
42870 }else{
42871 /* If zName is specified, operate on only the one system call
42872 ** specified.
42873 */
42874 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42875 if( strcmp(zName, aSyscall[i].zName)==0 ){
42876 if( aSyscall[i].pDefault==0 ){
42877 aSyscall[i].pDefault = aSyscall[i].pCurrent;
42878 }
42879 rc = SQLITE_OK;
42880 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
42881 aSyscall[i].pCurrent = pNewFunc;
42882 break;
42883 }
42884 }
42885 }
42886 return rc;
42887}
42888
42889/*
42890** Return the value of a system call. Return NULL if zName is not a
42891** recognized system call name. NULL is also returned if the system call
42892** is currently undefined.
42893*/
42894static sqlite3_syscall_ptr winGetSystemCall(
42895 sqlite3_vfs *pNotUsed,
42896 const char *zName
42897){
42898 unsigned int i;
42899
42900 UNUSED_PARAMETER(pNotUsed);
42901 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
42902 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
42903 }
42904 return 0;
42905}
42906
42907/*
42908** Return the name of the first system call after zName. If zName==NULL
42909** then return the name of the first system call. Return NULL if zName
42910** is the last system call or if zName is not the name of a valid
42911** system call.
42912*/
42913static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
42914 int i = -1;
42915
42916 UNUSED_PARAMETER(p);
42917 if( zName ){
42918 for(i=0; i<ArraySize(aSyscall)-1; i++){
42919 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
42920 }
42921 }
42922 for(i++; i<ArraySize(aSyscall); i++){
42923 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
42924 }
42925 return 0;
42926}
42927
42928#ifdef SQLITE_WIN32_MALLOC
42929/*
42930** If a Win32 native heap has been configured, this function will attempt to
42931** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
42932** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
42933** "pnLargest" argument, if non-zero, will be used to return the size of the
42934** largest committed free block in the heap, in bytes.
42935*/
42936SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
42937 int rc = SQLITE_OK;
42938 UINT nLargest = 0;
42939 HANDLE hHeap;
42940
42941 winMemAssertMagic();
42942 hHeap = winMemGetHeap();
42943 assert( hHeap!=0 );
42944 assert( hHeap!=INVALID_HANDLE_VALUE );
42945#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42946 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
42947#endif
42948#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
42949 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
42950 DWORD lastErrno = osGetLastError();
42951 if( lastErrno==NO_ERROR ){
42952 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
42953 (void*)hHeap);
42954 rc = SQLITE_NOMEM_BKPT;
42955 }else{
42956 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
42957 osGetLastError(), (void*)hHeap);
42958 rc = SQLITE_ERROR;
42959 }
42960 }
42961#else
42962 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
42963 (void*)hHeap);
42964 rc = SQLITE_NOTFOUND;
42965#endif
42966 if( pnLargest ) *pnLargest = nLargest;
42967 return rc;
42968}
42969
42970/*
42971** If a Win32 native heap has been configured, this function will attempt to
42972** destroy and recreate it. If the Win32 native heap is not isolated and/or
42973** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
42974** be returned and no changes will be made to the Win32 native heap.
42975*/
42976SQLITE_API int sqlite3_win32_reset_heap(){
42977 int rc;
42978 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
42979 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
42980 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
42981 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
42982 sqlite3_mutex_enter(pMainMtx);
42983 sqlite3_mutex_enter(pMem);
42984 winMemAssertMagic();
42985 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
42986 /*
42987 ** At this point, there should be no outstanding memory allocations on
42988 ** the heap. Also, since both the main and memsys locks are currently
42989 ** being held by us, no other function (i.e. from another thread) should
42990 ** be able to even access the heap. Attempt to destroy and recreate our
42991 ** isolated Win32 native heap now.
42992 */
42993 assert( winMemGetHeap()!=NULL );
42994 assert( winMemGetOwned() );
42995 assert( sqlite3_memory_used()==0 );
42996 winMemShutdown(winMemGetDataPtr());
42997 assert( winMemGetHeap()==NULL );
42998 assert( !winMemGetOwned() );
42999 assert( sqlite3_memory_used()==0 );
43000 rc = winMemInit(winMemGetDataPtr());
43001 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
43002 assert( rc!=SQLITE_OK || winMemGetOwned() );
43003 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
43004 }else{
43005 /*
43006 ** The Win32 native heap cannot be modified because it may be in use.
43007 */
43008 rc = SQLITE_BUSY;
43009 }
43010 sqlite3_mutex_leave(pMem);
43011 sqlite3_mutex_leave(pMainMtx);
43012 return rc;
43013}
43014#endif /* SQLITE_WIN32_MALLOC */
43015
43016/*
43017** This function outputs the specified (ANSI) string to the Win32 debugger
43018** (if available).
43019*/
43020
43021SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
43022 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
43023 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
43024 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
43025 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
43026#ifdef SQLITE_ENABLE_API_ARMOR
43027 if( !zBuf ){
43028 (void)SQLITE_MISUSE_BKPT;
43029 return;
43030 }
43031#endif
43032#if defined(SQLITE_WIN32_HAS_ANSI)
43033 if( nMin>0 ){
43034 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43035 memcpy(zDbgBuf, zBuf, nMin);
43036 osOutputDebugStringA(zDbgBuf);
43037 }else{
43038 osOutputDebugStringA(zBuf);
43039 }
43040#elif defined(SQLITE_WIN32_HAS_WIDE)
43041 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43042 if ( osMultiByteToWideChar(
43043 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
43044 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
43045 return;
43046 }
43047 osOutputDebugStringW((LPCWSTR)zDbgBuf);
43048#else
43049 if( nMin>0 ){
43050 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
43051 memcpy(zDbgBuf, zBuf, nMin);
43052 fprintf(stderr, "%s", zDbgBuf);
43053 }else{
43054 fprintf(stderr, "%s", zBuf);
43055 }
43056#endif
43057}
43058
43059/*
43060** The following routine suspends the current thread for at least ms
43061** milliseconds. This is equivalent to the Win32 Sleep() interface.
43062*/
43063#if SQLITE_OS_WINRT
43064static HANDLE sleepObj = NULL;
43065#endif
43066
43067SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
43068#if SQLITE_OS_WINRT
43069 if ( sleepObj==NULL ){
43070 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
43071 SYNCHRONIZE);
43072 }
43073 assert( sleepObj!=NULL );
43074 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
43075#else
43076 osSleep(milliseconds);
43077#endif
43078}
43079
43080#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
43081 SQLITE_THREADSAFE>0
43082SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
43083 DWORD rc;
43084 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
43085 TRUE))==WAIT_IO_COMPLETION ){}
43086 return rc;
43087}
43088#endif
43089
43090/*
43091** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
43092** or WinCE. Return false (zero) for Win95, Win98, or WinME.
43093**
43094** Here is an interesting observation: Win95, Win98, and WinME lack
43095** the LockFileEx() API. But we can still statically link against that
43096** API as long as we don't call it when running Win95/98/ME. A call to
43097** this routine is used to determine if the host is Win95/98/ME or
43098** WinNT/2K/XP so that we will know whether or not we can safely call
43099** the LockFileEx() API.
43100*/
43101
43102#if !SQLITE_WIN32_GETVERSIONEX
43103# define osIsNT() (1)
43104#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
43105# define osIsNT() (1)
43106#elif !defined(SQLITE_WIN32_HAS_WIDE)
43107# define osIsNT() (0)
43108#else
43109# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
43110#endif
43111
43112/*
43113** This function determines if the machine is running a version of Windows
43114** based on the NT kernel.
43115*/
43116SQLITE_API int sqlite3_win32_is_nt(void){
43117#if SQLITE_OS_WINRT
43118 /*
43119 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
43120 ** kernel.
43121 */
43122 return 1;
43123#elif SQLITE_WIN32_GETVERSIONEX
43124 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
43125#if defined(SQLITE_WIN32_HAS_ANSI)
43126 OSVERSIONINFOA sInfo;
43127 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43128 osGetVersionExA(&sInfo);
43129 osInterlockedCompareExchange(&sqlite3_os_type,
43130 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43131#elif defined(SQLITE_WIN32_HAS_WIDE)
43132 OSVERSIONINFOW sInfo;
43133 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
43134 osGetVersionExW(&sInfo);
43135 osInterlockedCompareExchange(&sqlite3_os_type,
43136 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
43137#endif
43138 }
43139 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43140#elif SQLITE_TEST
43141 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
43142#else
43143 /*
43144 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
43145 ** deprecated are always assumed to be based on the NT kernel.
43146 */
43147 return 1;
43148#endif
43149}
43150
43151#ifdef SQLITE_WIN32_MALLOC
43152/*
43153** Allocate nBytes of memory.
43154*/
43155static void *winMemMalloc(int nBytes){
43156 HANDLE hHeap;
43157 void *p;
43158
43159 winMemAssertMagic();
43160 hHeap = winMemGetHeap();
43161 assert( hHeap!=0 );
43162 assert( hHeap!=INVALID_HANDLE_VALUE );
43163#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43164 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43165#endif
43166 assert( nBytes>=0 );
43167 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43168 if( !p ){
43169 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
43170 nBytes, osGetLastError(), (void*)hHeap);
43171 }
43172 return p;
43173}
43174
43175/*
43176** Free memory.
43177*/
43178static void winMemFree(void *pPrior){
43179 HANDLE hHeap;
43180
43181 winMemAssertMagic();
43182 hHeap = winMemGetHeap();
43183 assert( hHeap!=0 );
43184 assert( hHeap!=INVALID_HANDLE_VALUE );
43185#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43186 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43187#endif
43188 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
43189 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
43190 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
43191 pPrior, osGetLastError(), (void*)hHeap);
43192 }
43193}
43194
43195/*
43196** Change the size of an existing memory allocation
43197*/
43198static void *winMemRealloc(void *pPrior, int nBytes){
43199 HANDLE hHeap;
43200 void *p;
43201
43202 winMemAssertMagic();
43203 hHeap = winMemGetHeap();
43204 assert( hHeap!=0 );
43205 assert( hHeap!=INVALID_HANDLE_VALUE );
43206#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43207 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
43208#endif
43209 assert( nBytes>=0 );
43210 if( !pPrior ){
43211 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
43212 }else{
43213 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
43214 }
43215 if( !p ){
43216 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
43217 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
43218 (void*)hHeap);
43219 }
43220 return p;
43221}
43222
43223/*
43224** Return the size of an outstanding allocation, in bytes.
43225*/
43226static int winMemSize(void *p){
43227 HANDLE hHeap;
43228 SIZE_T n;
43229
43230 winMemAssertMagic();
43231 hHeap = winMemGetHeap();
43232 assert( hHeap!=0 );
43233 assert( hHeap!=INVALID_HANDLE_VALUE );
43234#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43235 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
43236#endif
43237 if( !p ) return 0;
43238 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
43239 if( n==(SIZE_T)-1 ){
43240 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
43241 p, osGetLastError(), (void*)hHeap);
43242 return 0;
43243 }
43244 return (int)n;
43245}
43246
43247/*
43248** Round up a request size to the next valid allocation size.
43249*/
43250static int winMemRoundup(int n){
43251 return n;
43252}
43253
43254/*
43255** Initialize this module.
43256*/
43257static int winMemInit(void *pAppData){
43258 winMemData *pWinMemData = (winMemData *)pAppData;
43259
43260 if( !pWinMemData ) return SQLITE_ERROR;
43261 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43262 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43263
43264#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
43265 if( !pWinMemData->hHeap ){
43266 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
43267 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
43268 if( dwMaximumSize==0 ){
43269 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
43270 }else if( dwInitialSize>dwMaximumSize ){
43271 dwInitialSize = dwMaximumSize;
43272 }
43273 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
43274 dwInitialSize, dwMaximumSize);
43275 if( !pWinMemData->hHeap ){
43276 sqlite3_log(SQLITE_NOMEM,
43277 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
43278 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
43279 dwMaximumSize);
43280 return SQLITE_NOMEM_BKPT;
43281 }
43282 pWinMemData->bOwned = TRUE;
43283 assert( pWinMemData->bOwned );
43284 }
43285#else
43286 pWinMemData->hHeap = osGetProcessHeap();
43287 if( !pWinMemData->hHeap ){
43288 sqlite3_log(SQLITE_NOMEM,
43289 "failed to GetProcessHeap (%lu)", osGetLastError());
43290 return SQLITE_NOMEM_BKPT;
43291 }
43292 pWinMemData->bOwned = FALSE;
43293 assert( !pWinMemData->bOwned );
43294#endif
43295 assert( pWinMemData->hHeap!=0 );
43296 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43297#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43298 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43299#endif
43300 return SQLITE_OK;
43301}
43302
43303/*
43304** Deinitialize this module.
43305*/
43306static void winMemShutdown(void *pAppData){
43307 winMemData *pWinMemData = (winMemData *)pAppData;
43308
43309 if( !pWinMemData ) return;
43310 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43311 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43312
43313 if( pWinMemData->hHeap ){
43314 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43315#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
43316 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43317#endif
43318 if( pWinMemData->bOwned ){
43319 if( !osHeapDestroy(pWinMemData->hHeap) ){
43320 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
43321 osGetLastError(), (void*)pWinMemData->hHeap);
43322 }
43323 pWinMemData->bOwned = FALSE;
43324 }
43325 pWinMemData->hHeap = NULL;
43326 }
43327}
43328
43329/*
43330** Populate the low-level memory allocation function pointers in
43331** sqlite3GlobalConfig.m with pointers to the routines in this file. The
43332** arguments specify the block of memory to manage.
43333**
43334** This routine is only called by sqlite3_config(), and therefore
43335** is not required to be threadsafe (it is not).
43336*/
43337SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
43338 static const sqlite3_mem_methods winMemMethods = {
43339 winMemMalloc,
43340 winMemFree,
43341 winMemRealloc,
43342 winMemSize,
43343 winMemRoundup,
43344 winMemInit,
43345 winMemShutdown,
43346 &win_mem_data
43347 };
43348 return &winMemMethods;
43349}
43350
43351SQLITE_PRIVATE void sqlite3MemSetDefault(void){
43352 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
43353}
43354#endif /* SQLITE_WIN32_MALLOC */
43355
43356/*
43357** Convert a UTF-8 string to Microsoft Unicode.
43358**
43359** Space to hold the returned string is obtained from sqlite3_malloc().
43360*/
43361static LPWSTR winUtf8ToUnicode(const char *zText){
43362 int nChar;
43363 LPWSTR zWideText;
43364
43365 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
43366 if( nChar==0 ){
43367 return 0;
43368 }
43369 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
43370 if( zWideText==0 ){
43371 return 0;
43372 }
43373 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
43374 nChar);
43375 if( nChar==0 ){
43376 sqlite3_free(zWideText);
43377 zWideText = 0;
43378 }
43379 return zWideText;
43380}
43381
43382/*
43383** Convert a Microsoft Unicode string to UTF-8.
43384**
43385** Space to hold the returned string is obtained from sqlite3_malloc().
43386*/
43387static char *winUnicodeToUtf8(LPCWSTR zWideText){
43388 int nByte;
43389 char *zText;
43390
43391 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
43392 if( nByte == 0 ){
43393 return 0;
43394 }
43395 zText = sqlite3MallocZero( nByte );
43396 if( zText==0 ){
43397 return 0;
43398 }
43399 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
43400 0, 0);
43401 if( nByte == 0 ){
43402 sqlite3_free(zText);
43403 zText = 0;
43404 }
43405 return zText;
43406}
43407
43408/*
43409** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
43410** code page.
43411**
43412** Space to hold the returned string is obtained from sqlite3_malloc().
43413*/
43414static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
43415 int nByte;
43416 LPWSTR zMbcsText;
43417 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
43418
43419 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
43420 0)*sizeof(WCHAR);
43421 if( nByte==0 ){
43422 return 0;
43423 }
43424 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
43425 if( zMbcsText==0 ){
43426 return 0;
43427 }
43428 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
43429 nByte);
43430 if( nByte==0 ){
43431 sqlite3_free(zMbcsText);
43432 zMbcsText = 0;
43433 }
43434 return zMbcsText;
43435}
43436
43437/*
43438** Convert a Microsoft Unicode string to a multi-byte character string,
43439** using the ANSI or OEM code page.
43440**
43441** Space to hold the returned string is obtained from sqlite3_malloc().
43442*/
43443static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
43444 int nByte;
43445 char *zText;
43446 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
43447
43448 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
43449 if( nByte == 0 ){
43450 return 0;
43451 }
43452 zText = sqlite3MallocZero( nByte );
43453 if( zText==0 ){
43454 return 0;
43455 }
43456 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
43457 nByte, 0, 0);
43458 if( nByte == 0 ){
43459 sqlite3_free(zText);
43460 zText = 0;
43461 }
43462 return zText;
43463}
43464
43465/*
43466** Convert a multi-byte character string to UTF-8.
43467**
43468** Space to hold the returned string is obtained from sqlite3_malloc().
43469*/
43470static char *winMbcsToUtf8(const char *zText, int useAnsi){
43471 char *zTextUtf8;
43472 LPWSTR zTmpWide;
43473
43474 zTmpWide = winMbcsToUnicode(zText, useAnsi);
43475 if( zTmpWide==0 ){
43476 return 0;
43477 }
43478 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
43479 sqlite3_free(zTmpWide);
43480 return zTextUtf8;
43481}
43482
43483/*
43484** Convert a UTF-8 string to a multi-byte character string.
43485**
43486** Space to hold the returned string is obtained from sqlite3_malloc().
43487*/
43488static char *winUtf8ToMbcs(const char *zText, int useAnsi){
43489 char *zTextMbcs;
43490 LPWSTR zTmpWide;
43491
43492 zTmpWide = winUtf8ToUnicode(zText);
43493 if( zTmpWide==0 ){
43494 return 0;
43495 }
43496 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
43497 sqlite3_free(zTmpWide);
43498 return zTextMbcs;
43499}
43500
43501/*
43502** This is a public wrapper for the winUtf8ToUnicode() function.
43503*/
43504SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
43505#ifdef SQLITE_ENABLE_API_ARMOR
43506 if( !zText ){
43507 (void)SQLITE_MISUSE_BKPT;
43508 return 0;
43509 }
43510#endif
43511#ifndef SQLITE_OMIT_AUTOINIT
43512 if( sqlite3_initialize() ) return 0;
43513#endif
43514 return winUtf8ToUnicode(zText);
43515}
43516
43517/*
43518** This is a public wrapper for the winUnicodeToUtf8() function.
43519*/
43520SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
43521#ifdef SQLITE_ENABLE_API_ARMOR
43522 if( !zWideText ){
43523 (void)SQLITE_MISUSE_BKPT;
43524 return 0;
43525 }
43526#endif
43527#ifndef SQLITE_OMIT_AUTOINIT
43528 if( sqlite3_initialize() ) return 0;
43529#endif
43530 return winUnicodeToUtf8(zWideText);
43531}
43532
43533/*
43534** This is a public wrapper for the winMbcsToUtf8() function.
43535*/
43536SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
43537#ifdef SQLITE_ENABLE_API_ARMOR
43538 if( !zText ){
43539 (void)SQLITE_MISUSE_BKPT;
43540 return 0;
43541 }
43542#endif
43543#ifndef SQLITE_OMIT_AUTOINIT
43544 if( sqlite3_initialize() ) return 0;
43545#endif
43546 return winMbcsToUtf8(zText, osAreFileApisANSI());
43547}
43548
43549/*
43550** This is a public wrapper for the winMbcsToUtf8() function.
43551*/
43552SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
43553#ifdef SQLITE_ENABLE_API_ARMOR
43554 if( !zText ){
43555 (void)SQLITE_MISUSE_BKPT;
43556 return 0;
43557 }
43558#endif
43559#ifndef SQLITE_OMIT_AUTOINIT
43560 if( sqlite3_initialize() ) return 0;
43561#endif
43562 return winMbcsToUtf8(zText, useAnsi);
43563}
43564
43565/*
43566** This is a public wrapper for the winUtf8ToMbcs() function.
43567*/
43568SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
43569#ifdef SQLITE_ENABLE_API_ARMOR
43570 if( !zText ){
43571 (void)SQLITE_MISUSE_BKPT;
43572 return 0;
43573 }
43574#endif
43575#ifndef SQLITE_OMIT_AUTOINIT
43576 if( sqlite3_initialize() ) return 0;
43577#endif
43578 return winUtf8ToMbcs(zText, osAreFileApisANSI());
43579}
43580
43581/*
43582** This is a public wrapper for the winUtf8ToMbcs() function.
43583*/
43584SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
43585#ifdef SQLITE_ENABLE_API_ARMOR
43586 if( !zText ){
43587 (void)SQLITE_MISUSE_BKPT;
43588 return 0;
43589 }
43590#endif
43591#ifndef SQLITE_OMIT_AUTOINIT
43592 if( sqlite3_initialize() ) return 0;
43593#endif
43594 return winUtf8ToMbcs(zText, useAnsi);
43595}
43596
43597/*
43598** This function is the same as sqlite3_win32_set_directory (below); however,
43599** it accepts a UTF-8 string.
43600*/
43601SQLITE_API int sqlite3_win32_set_directory8(
43602 unsigned long type, /* Identifier for directory being set or reset */
43603 const char *zValue /* New value for directory being set or reset */
43604){
43605 char **ppDirectory = 0;
43606#ifndef SQLITE_OMIT_AUTOINIT
43607 int rc = sqlite3_initialize();
43608 if( rc ) return rc;
43609#endif
43610 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
43611 ppDirectory = &sqlite3_data_directory;
43612 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
43613 ppDirectory = &sqlite3_temp_directory;
43614 }
43615 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
43616 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
43617 );
43618 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
43619 if( ppDirectory ){
43620 char *zCopy = 0;
43621 if( zValue && zValue[0] ){
43622 zCopy = sqlite3_mprintf("%s", zValue);
43623 if ( zCopy==0 ){
43624 return SQLITE_NOMEM_BKPT;
43625 }
43626 }
43627 sqlite3_free(*ppDirectory);
43628 *ppDirectory = zCopy;
43629 return SQLITE_OK;
43630 }
43631 return SQLITE_ERROR;
43632}
43633
43634/*
43635** This function is the same as sqlite3_win32_set_directory (below); however,
43636** it accepts a UTF-16 string.
43637*/
43638SQLITE_API int sqlite3_win32_set_directory16(
43639 unsigned long type, /* Identifier for directory being set or reset */
43640 const void *zValue /* New value for directory being set or reset */
43641){
43642 int rc;
43643 char *zUtf8 = 0;
43644 if( zValue ){
43645 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
43646 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
43647 }
43648 rc = sqlite3_win32_set_directory8(type, zUtf8);
43649 if( zUtf8 ) sqlite3_free(zUtf8);
43650 return rc;
43651}
43652
43653/*
43654** This function sets the data directory or the temporary directory based on
43655** the provided arguments. The type argument must be 1 in order to set the
43656** data directory or 2 in order to set the temporary directory. The zValue
43657** argument is the name of the directory to use. The return value will be
43658** SQLITE_OK if successful.
43659*/
43660SQLITE_API int sqlite3_win32_set_directory(
43661 unsigned long type, /* Identifier for directory being set or reset */
43662 void *zValue /* New value for directory being set or reset */
43663){
43664 return sqlite3_win32_set_directory16(type, zValue);
43665}
43666
43667/*
43668** The return value of winGetLastErrorMsg
43669** is zero if the error message fits in the buffer, or non-zero
43670** otherwise (if the message was truncated).
43671*/
43672static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
43673 /* FormatMessage returns 0 on failure. Otherwise it
43674 ** returns the number of TCHARs written to the output
43675 ** buffer, excluding the terminating null char.
43676 */
43677 DWORD dwLen = 0;
43678 char *zOut = 0;
43679
43680 if( osIsNT() ){
43681#if SQLITE_OS_WINRT
43682 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
43683 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
43684 FORMAT_MESSAGE_IGNORE_INSERTS,
43685 NULL,
43686 lastErrno,
43687 0,
43688 zTempWide,
43689 SQLITE_WIN32_MAX_ERRMSG_CHARS,
43690 0);
43691#else
43692 LPWSTR zTempWide = NULL;
43693 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43694 FORMAT_MESSAGE_FROM_SYSTEM |
43695 FORMAT_MESSAGE_IGNORE_INSERTS,
43696 NULL,
43697 lastErrno,
43698 0,
43699 (LPWSTR) &zTempWide,
43700 0,
43701 0);
43702#endif
43703 if( dwLen > 0 ){
43704 /* allocate a buffer and convert to UTF8 */
43705 sqlite3BeginBenignMalloc();
43706 zOut = winUnicodeToUtf8(zTempWide);
43707 sqlite3EndBenignMalloc();
43708#if !SQLITE_OS_WINRT
43709 /* free the system buffer allocated by FormatMessage */
43710 osLocalFree(zTempWide);
43711#endif
43712 }
43713 }
43714#ifdef SQLITE_WIN32_HAS_ANSI
43715 else{
43716 char *zTemp = NULL;
43717 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43718 FORMAT_MESSAGE_FROM_SYSTEM |
43719 FORMAT_MESSAGE_IGNORE_INSERTS,
43720 NULL,
43721 lastErrno,
43722 0,
43723 (LPSTR) &zTemp,
43724 0,
43725 0);
43726 if( dwLen > 0 ){
43727 /* allocate a buffer and convert to UTF8 */
43728 sqlite3BeginBenignMalloc();
43729 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
43730 sqlite3EndBenignMalloc();
43731 /* free the system buffer allocated by FormatMessage */
43732 osLocalFree(zTemp);
43733 }
43734 }
43735#endif
43736 if( 0 == dwLen ){
43737 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
43738 }else{
43739 /* copy a maximum of nBuf chars to output buffer */
43740 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
43741 /* free the UTF8 buffer */
43742 sqlite3_free(zOut);
43743 }
43744 return 0;
43745}
43746
43747/*
43748**
43749** This function - winLogErrorAtLine() - is only ever called via the macro
43750** winLogError().
43751**
43752** This routine is invoked after an error occurs in an OS function.
43753** It logs a message using sqlite3_log() containing the current value of
43754** error code and, if possible, the human-readable equivalent from
43755** FormatMessage.
43756**
43757** The first argument passed to the macro should be the error code that
43758** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
43759** The two subsequent arguments should be the name of the OS function that
43760** failed and the associated file-system path, if any.
43761*/
43762#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
43763static int winLogErrorAtLine(
43764 int errcode, /* SQLite error code */
43765 DWORD lastErrno, /* Win32 last error */
43766 const char *zFunc, /* Name of OS function that failed */
43767 const char *zPath, /* File path associated with error */
43768 int iLine /* Source line number where error occurred */
43769){
43770 char zMsg[500]; /* Human readable error text */
43771 int i; /* Loop counter */
43772
43773 zMsg[0] = 0;
43774 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
43775 assert( errcode!=SQLITE_OK );
43776 if( zPath==0 ) zPath = "";
43777 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
43778 zMsg[i] = 0;
43779 sqlite3_log(errcode,
43780 "os_win.c:%d: (%lu) %s(%s) - %s",
43781 iLine, lastErrno, zFunc, zPath, zMsg
43782 );
43783
43784 return errcode;
43785}
43786
43787/*
43788** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
43789** will be retried following a locking error - probably caused by
43790** antivirus software. Also the initial delay before the first retry.
43791** The delay increases linearly with each retry.
43792*/
43793#ifndef SQLITE_WIN32_IOERR_RETRY
43794# define SQLITE_WIN32_IOERR_RETRY 10
43795#endif
43796#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
43797# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
43798#endif
43799static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
43800static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
43801
43802/*
43803** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
43804** error code obtained via GetLastError() is eligible to be retried. It
43805** must accept the error code DWORD as its only argument and should return
43806** non-zero if the error code is transient in nature and the operation
43807** responsible for generating the original error might succeed upon being
43808** retried. The argument to this macro should be a variable.
43809**
43810** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
43811** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
43812** returns zero. The "winIoerrCanRetry2" macro is completely optional and
43813** may be used to include additional error codes in the set that should
43814** result in the failing I/O operation being retried by the caller. If
43815** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
43816** identical to those of the "winIoerrCanRetry1" macro.
43817*/
43818#if !defined(winIoerrCanRetry1)
43819#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
43820 ((a)==ERROR_SHARING_VIOLATION) || \
43821 ((a)==ERROR_LOCK_VIOLATION) || \
43822 ((a)==ERROR_DEV_NOT_EXIST) || \
43823 ((a)==ERROR_NETNAME_DELETED) || \
43824 ((a)==ERROR_SEM_TIMEOUT) || \
43825 ((a)==ERROR_NETWORK_UNREACHABLE))
43826#endif
43827
43828/*
43829** If a ReadFile() or WriteFile() error occurs, invoke this routine
43830** to see if it should be retried. Return TRUE to retry. Return FALSE
43831** to give up with an error.
43832*/
43833static int winRetryIoerr(int *pnRetry, DWORD *pError){
43834 DWORD e = osGetLastError();
43835 if( *pnRetry>=winIoerrRetry ){
43836 if( pError ){
43837 *pError = e;
43838 }
43839 return 0;
43840 }
43841 if( winIoerrCanRetry1(e) ){
43842 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
43843 ++*pnRetry;
43844 return 1;
43845 }
43846#if defined(winIoerrCanRetry2)
43847 else if( winIoerrCanRetry2(e) ){
43848 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
43849 ++*pnRetry;
43850 return 1;
43851 }
43852#endif
43853 if( pError ){
43854 *pError = e;
43855 }
43856 return 0;
43857}
43858
43859/*
43860** Log a I/O error retry episode.
43861*/
43862static void winLogIoerr(int nRetry, int lineno){
43863 if( nRetry ){
43864 sqlite3_log(SQLITE_NOTICE,
43865 "delayed %dms for lock/sharing conflict at line %d",
43866 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
43867 );
43868 }
43869}
43870
43871/*
43872** This #if does not rely on the SQLITE_OS_WINCE define because the
43873** corresponding section in "date.c" cannot use it.
43874*/
43875#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
43876 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
43877/*
43878** The MSVC CRT on Windows CE may not have a localtime() function.
43879** So define a substitute.
43880*/
43881/* # include <time.h> */
43882struct tm *__cdecl localtime(const time_t *t)
43883{
43884 static struct tm y;
43885 FILETIME uTm, lTm;
43886 SYSTEMTIME pTm;
43887 sqlite3_int64 t64;
43888 t64 = *t;
43889 t64 = (t64 + 11644473600)*10000000;
43890 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
43891 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
43892 osFileTimeToLocalFileTime(&uTm,&lTm);
43893 osFileTimeToSystemTime(&lTm,&pTm);
43894 y.tm_year = pTm.wYear - 1900;
43895 y.tm_mon = pTm.wMonth - 1;
43896 y.tm_wday = pTm.wDayOfWeek;
43897 y.tm_mday = pTm.wDay;
43898 y.tm_hour = pTm.wHour;
43899 y.tm_min = pTm.wMinute;
43900 y.tm_sec = pTm.wSecond;
43901 return &y;
43902}
43903#endif
43904
43905#if SQLITE_OS_WINCE
43906/*************************************************************************
43907** This section contains code for WinCE only.
43908*/
43909#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
43910
43911/*
43912** Acquire a lock on the handle h
43913*/
43914static void winceMutexAcquire(HANDLE h){
43915 DWORD dwErr;
43916 do {
43917 dwErr = osWaitForSingleObject(h, INFINITE);
43918 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
43919}
43920/*
43921** Release a lock acquired by winceMutexAcquire()
43922*/
43923#define winceMutexRelease(h) ReleaseMutex(h)
43924
43925/*
43926** Create the mutex and shared memory used for locking in the file
43927** descriptor pFile
43928*/
43929static int winceCreateLock(const char *zFilename, winFile *pFile){
43930 LPWSTR zTok;
43931 LPWSTR zName;
43932 DWORD lastErrno;
43933 BOOL bLogged = FALSE;
43934 BOOL bInit = TRUE;
43935
43936 zName = winUtf8ToUnicode(zFilename);
43937 if( zName==0 ){
43938 /* out of memory */
43939 return SQLITE_IOERR_NOMEM_BKPT;
43940 }
43941
43942 /* Initialize the local lockdata */
43943 memset(&pFile->local, 0, sizeof(pFile->local));
43944
43945 /* Replace the backslashes from the filename and lowercase it
43946 ** to derive a mutex name. */
43947 zTok = osCharLowerW(zName);
43948 for (;*zTok;zTok++){
43949 if (*zTok == '\\') *zTok = '_';
43950 }
43951
43952 /* Create/open the named mutex */
43953 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
43954 if (!pFile->hMutex){
43955 pFile->lastErrno = osGetLastError();
43956 sqlite3_free(zName);
43957 return winLogError(SQLITE_IOERR, pFile->lastErrno,
43958 "winceCreateLock1", zFilename);
43959 }
43960
43961 /* Acquire the mutex before continuing */
43962 winceMutexAcquire(pFile->hMutex);
43963
43964 /* Since the names of named mutexes, semaphores, file mappings etc are
43965 ** case-sensitive, take advantage of that by uppercasing the mutex name
43966 ** and using that as the shared filemapping name.
43967 */
43968 osCharUpperW(zName);
43969 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
43970 PAGE_READWRITE, 0, sizeof(winceLock),
43971 zName);
43972
43973 /* Set a flag that indicates we're the first to create the memory so it
43974 ** must be zero-initialized */
43975 lastErrno = osGetLastError();
43976 if (lastErrno == ERROR_ALREADY_EXISTS){
43977 bInit = FALSE;
43978 }
43979
43980 sqlite3_free(zName);
43981
43982 /* If we succeeded in making the shared memory handle, map it. */
43983 if( pFile->hShared ){
43984 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
43985 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
43986 /* If mapping failed, close the shared memory handle and erase it */
43987 if( !pFile->shared ){
43988 pFile->lastErrno = osGetLastError();
43989 winLogError(SQLITE_IOERR, pFile->lastErrno,
43990 "winceCreateLock2", zFilename);
43991 bLogged = TRUE;
43992 osCloseHandle(pFile->hShared);
43993 pFile->hShared = NULL;
43994 }
43995 }
43996
43997 /* If shared memory could not be created, then close the mutex and fail */
43998 if( pFile->hShared==NULL ){
43999 if( !bLogged ){
44000 pFile->lastErrno = lastErrno;
44001 winLogError(SQLITE_IOERR, pFile->lastErrno,
44002 "winceCreateLock3", zFilename);
44003 bLogged = TRUE;
44004 }
44005 winceMutexRelease(pFile->hMutex);
44006 osCloseHandle(pFile->hMutex);
44007 pFile->hMutex = NULL;
44008 return SQLITE_IOERR;
44009 }
44010
44011 /* Initialize the shared memory if we're supposed to */
44012 if( bInit ){
44013 memset(pFile->shared, 0, sizeof(winceLock));
44014 }
44015
44016 winceMutexRelease(pFile->hMutex);
44017 return SQLITE_OK;
44018}
44019
44020/*
44021** Destroy the part of winFile that deals with wince locks
44022*/
44023static void winceDestroyLock(winFile *pFile){
44024 if (pFile->hMutex){
44025 /* Acquire the mutex */
44026 winceMutexAcquire(pFile->hMutex);
44027
44028 /* The following blocks should probably assert in debug mode, but they
44029 are to cleanup in case any locks remained open */
44030 if (pFile->local.nReaders){
44031 pFile->shared->nReaders --;
44032 }
44033 if (pFile->local.bReserved){
44034 pFile->shared->bReserved = FALSE;
44035 }
44036 if (pFile->local.bPending){
44037 pFile->shared->bPending = FALSE;
44038 }
44039 if (pFile->local.bExclusive){
44040 pFile->shared->bExclusive = FALSE;
44041 }
44042
44043 /* De-reference and close our copy of the shared memory handle */
44044 osUnmapViewOfFile(pFile->shared);
44045 osCloseHandle(pFile->hShared);
44046
44047 /* Done with the mutex */
44048 winceMutexRelease(pFile->hMutex);
44049 osCloseHandle(pFile->hMutex);
44050 pFile->hMutex = NULL;
44051 }
44052}
44053
44054/*
44055** An implementation of the LockFile() API of Windows for CE
44056*/
44057static BOOL winceLockFile(
44058 LPHANDLE phFile,
44059 DWORD dwFileOffsetLow,
44060 DWORD dwFileOffsetHigh,
44061 DWORD nNumberOfBytesToLockLow,
44062 DWORD nNumberOfBytesToLockHigh
44063){
44064 winFile *pFile = HANDLE_TO_WINFILE(phFile);
44065 BOOL bReturn = FALSE;
44066
44067 UNUSED_PARAMETER(dwFileOffsetHigh);
44068 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
44069
44070 if (!pFile->hMutex) return TRUE;
44071 winceMutexAcquire(pFile->hMutex);
44072
44073 /* Wanting an exclusive lock? */
44074 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
44075 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
44076 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
44077 pFile->shared->bExclusive = TRUE;
44078 pFile->local.bExclusive = TRUE;
44079 bReturn = TRUE;
44080 }
44081 }
44082
44083 /* Want a read-only lock? */
44084 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
44085 nNumberOfBytesToLockLow == 1){
44086 if (pFile->shared->bExclusive == 0){
44087 pFile->local.nReaders ++;
44088 if (pFile->local.nReaders == 1){
44089 pFile->shared->nReaders ++;
44090 }
44091 bReturn = TRUE;
44092 }
44093 }
44094
44095 /* Want a pending lock? */
44096 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44097 && nNumberOfBytesToLockLow == 1){
44098 /* If no pending lock has been acquired, then acquire it */
44099 if (pFile->shared->bPending == 0) {
44100 pFile->shared->bPending = TRUE;
44101 pFile->local.bPending = TRUE;
44102 bReturn = TRUE;
44103 }
44104 }
44105
44106 /* Want a reserved lock? */
44107 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44108 && nNumberOfBytesToLockLow == 1){
44109 if (pFile->shared->bReserved == 0) {
44110 pFile->shared->bReserved = TRUE;
44111 pFile->local.bReserved = TRUE;
44112 bReturn = TRUE;
44113 }
44114 }
44115
44116 winceMutexRelease(pFile->hMutex);
44117 return bReturn;
44118}
44119
44120/*
44121** An implementation of the UnlockFile API of Windows for CE
44122*/
44123static BOOL winceUnlockFile(
44124 LPHANDLE phFile,
44125 DWORD dwFileOffsetLow,
44126 DWORD dwFileOffsetHigh,
44127 DWORD nNumberOfBytesToUnlockLow,
44128 DWORD nNumberOfBytesToUnlockHigh
44129){
44130 winFile *pFile = HANDLE_TO_WINFILE(phFile);
44131 BOOL bReturn = FALSE;
44132
44133 UNUSED_PARAMETER(dwFileOffsetHigh);
44134 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
44135
44136 if (!pFile->hMutex) return TRUE;
44137 winceMutexAcquire(pFile->hMutex);
44138
44139 /* Releasing a reader lock or an exclusive lock */
44140 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
44141 /* Did we have an exclusive lock? */
44142 if (pFile->local.bExclusive){
44143 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
44144 pFile->local.bExclusive = FALSE;
44145 pFile->shared->bExclusive = FALSE;
44146 bReturn = TRUE;
44147 }
44148
44149 /* Did we just have a reader lock? */
44150 else if (pFile->local.nReaders){
44151 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
44152 || nNumberOfBytesToUnlockLow == 1);
44153 pFile->local.nReaders --;
44154 if (pFile->local.nReaders == 0)
44155 {
44156 pFile->shared->nReaders --;
44157 }
44158 bReturn = TRUE;
44159 }
44160 }
44161
44162 /* Releasing a pending lock */
44163 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
44164 && nNumberOfBytesToUnlockLow == 1){
44165 if (pFile->local.bPending){
44166 pFile->local.bPending = FALSE;
44167 pFile->shared->bPending = FALSE;
44168 bReturn = TRUE;
44169 }
44170 }
44171 /* Releasing a reserved lock */
44172 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
44173 && nNumberOfBytesToUnlockLow == 1){
44174 if (pFile->local.bReserved) {
44175 pFile->local.bReserved = FALSE;
44176 pFile->shared->bReserved = FALSE;
44177 bReturn = TRUE;
44178 }
44179 }
44180
44181 winceMutexRelease(pFile->hMutex);
44182 return bReturn;
44183}
44184/*
44185** End of the special code for wince
44186*****************************************************************************/
44187#endif /* SQLITE_OS_WINCE */
44188
44189/*
44190** Lock a file region.
44191*/
44192static BOOL winLockFile(
44193 LPHANDLE phFile,
44194 DWORD flags,
44195 DWORD offsetLow,
44196 DWORD offsetHigh,
44197 DWORD numBytesLow,
44198 DWORD numBytesHigh
44199){
44200#if SQLITE_OS_WINCE
44201 /*
44202 ** NOTE: Windows CE is handled differently here due its lack of the Win32
44203 ** API LockFile.
44204 */
44205 return winceLockFile(phFile, offsetLow, offsetHigh,
44206 numBytesLow, numBytesHigh);
44207#else
44208 if( osIsNT() ){
44209 OVERLAPPED ovlp;
44210 memset(&ovlp, 0, sizeof(OVERLAPPED));
44211 ovlp.Offset = offsetLow;
44212 ovlp.OffsetHigh = offsetHigh;
44213 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
44214 }else{
44215 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44216 numBytesHigh);
44217 }
44218#endif
44219}
44220
44221/*
44222** Unlock a file region.
44223 */
44224static BOOL winUnlockFile(
44225 LPHANDLE phFile,
44226 DWORD offsetLow,
44227 DWORD offsetHigh,
44228 DWORD numBytesLow,
44229 DWORD numBytesHigh
44230){
44231#if SQLITE_OS_WINCE
44232 /*
44233 ** NOTE: Windows CE is handled differently here due its lack of the Win32
44234 ** API UnlockFile.
44235 */
44236 return winceUnlockFile(phFile, offsetLow, offsetHigh,
44237 numBytesLow, numBytesHigh);
44238#else
44239 if( osIsNT() ){
44240 OVERLAPPED ovlp;
44241 memset(&ovlp, 0, sizeof(OVERLAPPED));
44242 ovlp.Offset = offsetLow;
44243 ovlp.OffsetHigh = offsetHigh;
44244 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
44245 }else{
44246 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
44247 numBytesHigh);
44248 }
44249#endif
44250}
44251
44252/*****************************************************************************
44253** The next group of routines implement the I/O methods specified
44254** by the sqlite3_io_methods object.
44255******************************************************************************/
44256
44257/*
44258** Some Microsoft compilers lack this definition.
44259*/
44260#ifndef INVALID_SET_FILE_POINTER
44261# define INVALID_SET_FILE_POINTER ((DWORD)-1)
44262#endif
44263
44264/*
44265** Move the current position of the file handle passed as the first
44266** argument to offset iOffset within the file. If successful, return 0.
44267** Otherwise, set pFile->lastErrno and return non-zero.
44268*/
44269static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
44270#if !SQLITE_OS_WINRT
44271 LONG upperBits; /* Most sig. 32 bits of new offset */
44272 LONG lowerBits; /* Least sig. 32 bits of new offset */
44273 DWORD dwRet; /* Value returned by SetFilePointer() */
44274 DWORD lastErrno; /* Value returned by GetLastError() */
44275
44276 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
44277
44278 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
44279 lowerBits = (LONG)(iOffset & 0xffffffff);
44280
44281 /* API oddity: If successful, SetFilePointer() returns a dword
44282 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
44283 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
44284 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
44285 ** whether an error has actually occurred, it is also necessary to call
44286 ** GetLastError().
44287 */
44288 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
44289
44290 if( (dwRet==INVALID_SET_FILE_POINTER
44291 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
44292 pFile->lastErrno = lastErrno;
44293 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44294 "winSeekFile", pFile->zPath);
44295 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44296 return 1;
44297 }
44298
44299 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44300 return 0;
44301#else
44302 /*
44303 ** Same as above, except that this implementation works for WinRT.
44304 */
44305
44306 LARGE_INTEGER x; /* The new offset */
44307 BOOL bRet; /* Value returned by SetFilePointerEx() */
44308
44309 x.QuadPart = iOffset;
44310 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
44311
44312 if(!bRet){
44313 pFile->lastErrno = osGetLastError();
44314 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44315 "winSeekFile", pFile->zPath);
44316 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44317 return 1;
44318 }
44319
44320 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44321 return 0;
44322#endif
44323}
44324
44325#if SQLITE_MAX_MMAP_SIZE>0
44326/* Forward references to VFS helper methods used for memory mapped files */
44327static int winMapfile(winFile*, sqlite3_int64);
44328static int winUnmapfile(winFile*);
44329#endif
44330
44331/*
44332** Close a file.
44333**
44334** It is reported that an attempt to close a handle might sometimes
44335** fail. This is a very unreasonable result, but Windows is notorious
44336** for being unreasonable so I do not doubt that it might happen. If
44337** the close fails, we pause for 100 milliseconds and try again. As
44338** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
44339** giving up and returning an error.
44340*/
44341#define MX_CLOSE_ATTEMPT 3
44342static int winClose(sqlite3_file *id){
44343 int rc, cnt = 0;
44344 winFile *pFile = (winFile*)id;
44345
44346 assert( id!=0 );
44347#ifndef SQLITE_OMIT_WAL
44348 assert( pFile->pShm==0 );
44349#endif
44350 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
44351 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
44352 osGetCurrentProcessId(), pFile, pFile->h));
44353
44354#if SQLITE_MAX_MMAP_SIZE>0
44355 winUnmapfile(pFile);
44356#endif
44357
44358 do{
44359 rc = osCloseHandle(pFile->h);
44360 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
44361 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
44362#if SQLITE_OS_WINCE
44363#define WINCE_DELETION_ATTEMPTS 3
44364 {
44365 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
44366 if( pAppData==NULL || !pAppData->bNoLock ){
44367 winceDestroyLock(pFile);
44368 }
44369 }
44370 if( pFile->zDeleteOnClose ){
44371 int cnt = 0;
44372 while(
44373 osDeleteFileW(pFile->zDeleteOnClose)==0
44374 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
44375 && cnt++ < WINCE_DELETION_ATTEMPTS
44376 ){
44377 sqlite3_win32_sleep(100); /* Wait a little before trying again */
44378 }
44379 sqlite3_free(pFile->zDeleteOnClose);
44380 }
44381#endif
44382 if( rc ){
44383 pFile->h = NULL;
44384 }
44385 OpenCounter(-1);
44386 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
44387 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
44388 return rc ? SQLITE_OK
44389 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
44390 "winClose", pFile->zPath);
44391}
44392
44393/*
44394** Read data from a file into a buffer. Return SQLITE_OK if all
44395** bytes were read successfully and SQLITE_IOERR if anything goes
44396** wrong.
44397*/
44398static int winRead(
44399 sqlite3_file *id, /* File to read from */
44400 void *pBuf, /* Write content into this buffer */
44401 int amt, /* Number of bytes to read */
44402 sqlite3_int64 offset /* Begin reading at this offset */
44403){
44404#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44405 OVERLAPPED overlapped; /* The offset for ReadFile. */
44406#endif
44407 winFile *pFile = (winFile*)id; /* file handle */
44408 DWORD nRead; /* Number of bytes actually read from file */
44409 int nRetry = 0; /* Number of retrys */
44410
44411 assert( id!=0 );
44412 assert( amt>0 );
44413 assert( offset>=0 );
44414 SimulateIOError(return SQLITE_IOERR_READ);
44415 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
44416 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
44417 pFile->h, pBuf, amt, offset, pFile->locktype));
44418
44419#if SQLITE_MAX_MMAP_SIZE>0
44420 /* Deal with as much of this read request as possible by transfering
44421 ** data from the memory mapping using memcpy(). */
44422 if( offset<pFile->mmapSize ){
44423 if( offset+amt <= pFile->mmapSize ){
44424 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
44425 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44426 osGetCurrentProcessId(), pFile, pFile->h));
44427 return SQLITE_OK;
44428 }else{
44429 int nCopy = (int)(pFile->mmapSize - offset);
44430 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
44431 pBuf = &((u8 *)pBuf)[nCopy];
44432 amt -= nCopy;
44433 offset += nCopy;
44434 }
44435 }
44436#endif
44437
44438#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44439 if( winSeekFile(pFile, offset) ){
44440 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
44441 osGetCurrentProcessId(), pFile, pFile->h));
44442 return SQLITE_FULL;
44443 }
44444 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
44445#else
44446 memset(&overlapped, 0, sizeof(OVERLAPPED));
44447 overlapped.Offset = (LONG)(offset & 0xffffffff);
44448 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44449 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
44450 osGetLastError()!=ERROR_HANDLE_EOF ){
44451#endif
44452 DWORD lastErrno;
44453 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
44454 pFile->lastErrno = lastErrno;
44455 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
44456 osGetCurrentProcessId(), pFile, pFile->h));
44457 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
44458 "winRead", pFile->zPath);
44459 }
44460 winLogIoerr(nRetry, __LINE__);
44461 if( nRead<(DWORD)amt ){
44462 /* Unread parts of the buffer must be zero-filled */
44463 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
44464 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
44465 osGetCurrentProcessId(), pFile, pFile->h));
44466 return SQLITE_IOERR_SHORT_READ;
44467 }
44468
44469 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44470 osGetCurrentProcessId(), pFile, pFile->h));
44471 return SQLITE_OK;
44472}
44473
44474/*
44475** Write data from a buffer into a file. Return SQLITE_OK on success
44476** or some other error code on failure.
44477*/
44478static int winWrite(
44479 sqlite3_file *id, /* File to write into */
44480 const void *pBuf, /* The bytes to be written */
44481 int amt, /* Number of bytes to write */
44482 sqlite3_int64 offset /* Offset into the file to begin writing at */
44483){
44484 int rc = 0; /* True if error has occurred, else false */
44485 winFile *pFile = (winFile*)id; /* File handle */
44486 int nRetry = 0; /* Number of retries */
44487
44488 assert( amt>0 );
44489 assert( pFile );
44490 SimulateIOError(return SQLITE_IOERR_WRITE);
44491 SimulateDiskfullError(return SQLITE_FULL);
44492
44493 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
44494 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
44495 pFile->h, pBuf, amt, offset, pFile->locktype));
44496
44497#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
44498 /* Deal with as much of this write request as possible by transfering
44499 ** data from the memory mapping using memcpy(). */
44500 if( offset<pFile->mmapSize ){
44501 if( offset+amt <= pFile->mmapSize ){
44502 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
44503 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44504 osGetCurrentProcessId(), pFile, pFile->h));
44505 return SQLITE_OK;
44506 }else{
44507 int nCopy = (int)(pFile->mmapSize - offset);
44508 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
44509 pBuf = &((u8 *)pBuf)[nCopy];
44510 amt -= nCopy;
44511 offset += nCopy;
44512 }
44513 }
44514#endif
44515
44516#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44517 rc = winSeekFile(pFile, offset);
44518 if( rc==0 ){
44519#else
44520 {
44521#endif
44522#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44523 OVERLAPPED overlapped; /* The offset for WriteFile. */
44524#endif
44525 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
44526 int nRem = amt; /* Number of bytes yet to be written */
44527 DWORD nWrite; /* Bytes written by each WriteFile() call */
44528 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
44529
44530#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44531 memset(&overlapped, 0, sizeof(OVERLAPPED));
44532 overlapped.Offset = (LONG)(offset & 0xffffffff);
44533 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44534#endif
44535
44536 while( nRem>0 ){
44537#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
44538 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
44539#else
44540 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
44541#endif
44542 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
44543 break;
44544 }
44545 assert( nWrite==0 || nWrite<=(DWORD)nRem );
44546 if( nWrite==0 || nWrite>(DWORD)nRem ){
44547 lastErrno = osGetLastError();
44548 break;
44549 }
44550#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
44551 offset += nWrite;
44552 overlapped.Offset = (LONG)(offset & 0xffffffff);
44553 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
44554#endif
44555 aRem += nWrite;
44556 nRem -= nWrite;
44557 }
44558 if( nRem>0 ){
44559 pFile->lastErrno = lastErrno;
44560 rc = 1;
44561 }
44562 }
44563
44564 if( rc ){
44565 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
44566 || ( pFile->lastErrno==ERROR_DISK_FULL )){
44567 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
44568 osGetCurrentProcessId(), pFile, pFile->h));
44569 return winLogError(SQLITE_FULL, pFile->lastErrno,
44570 "winWrite1", pFile->zPath);
44571 }
44572 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
44573 osGetCurrentProcessId(), pFile, pFile->h));
44574 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
44575 "winWrite2", pFile->zPath);
44576 }else{
44577 winLogIoerr(nRetry, __LINE__);
44578 }
44579 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44580 osGetCurrentProcessId(), pFile, pFile->h));
44581 return SQLITE_OK;
44582}
44583
44584/*
44585** Truncate an open file to a specified size
44586*/
44587static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
44588 winFile *pFile = (winFile*)id; /* File handle object */
44589 int rc = SQLITE_OK; /* Return code for this function */
44590 DWORD lastErrno;
44591#if SQLITE_MAX_MMAP_SIZE>0
44592 sqlite3_int64 oldMmapSize;
44593 if( pFile->nFetchOut>0 ){
44594 /* File truncation is a no-op if there are outstanding memory mapped
44595 ** pages. This is because truncating the file means temporarily unmapping
44596 ** the file, and that might delete memory out from under existing cursors.
44597 **
44598 ** This can result in incremental vacuum not truncating the file,
44599 ** if there is an active read cursor when the incremental vacuum occurs.
44600 ** No real harm comes of this - the database file is not corrupted,
44601 ** though some folks might complain that the file is bigger than it
44602 ** needs to be.
44603 **
44604 ** The only feasible work-around is to defer the truncation until after
44605 ** all references to memory-mapped content are closed. That is doable,
44606 ** but involves adding a few branches in the common write code path which
44607 ** could slow down normal operations slightly. Hence, we have decided for
44608 ** now to simply make trancations a no-op if there are pending reads. We
44609 ** can maybe revisit this decision in the future.
44610 */
44611 return SQLITE_OK;
44612 }
44613#endif
44614
44615 assert( pFile );
44616 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
44617 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
44618 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
44619
44620 /* If the user has configured a chunk-size for this file, truncate the
44621 ** file so that it consists of an integer number of chunks (i.e. the
44622 ** actual file size after the operation may be larger than the requested
44623 ** size).
44624 */
44625 if( pFile->szChunk>0 ){
44626 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
44627 }
44628
44629#if SQLITE_MAX_MMAP_SIZE>0
44630 if( pFile->pMapRegion ){
44631 oldMmapSize = pFile->mmapSize;
44632 }else{
44633 oldMmapSize = 0;
44634 }
44635 winUnmapfile(pFile);
44636#endif
44637
44638 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
44639 if( winSeekFile(pFile, nByte) ){
44640 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
44641 "winTruncate1", pFile->zPath);
44642 }else if( 0==osSetEndOfFile(pFile->h) &&
44643 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
44644 pFile->lastErrno = lastErrno;
44645 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
44646 "winTruncate2", pFile->zPath);
44647 }
44648
44649#if SQLITE_MAX_MMAP_SIZE>0
44650 if( rc==SQLITE_OK && oldMmapSize>0 ){
44651 if( oldMmapSize>nByte ){
44652 winMapfile(pFile, -1);
44653 }else{
44654 winMapfile(pFile, oldMmapSize);
44655 }
44656 }
44657#endif
44658
44659 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
44660 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
44661 return rc;
44662}
44663
44664#ifdef SQLITE_TEST
44665/*
44666** Count the number of fullsyncs and normal syncs. This is used to test
44667** that syncs and fullsyncs are occuring at the right times.
44668*/
44669SQLITE_API int sqlite3_sync_count = 0;
44670SQLITE_API int sqlite3_fullsync_count = 0;
44671#endif
44672
44673/*
44674** Make sure all writes to a particular file are committed to disk.
44675*/
44676static int winSync(sqlite3_file *id, int flags){
44677#ifndef SQLITE_NO_SYNC
44678 /*
44679 ** Used only when SQLITE_NO_SYNC is not defined.
44680 */
44681 BOOL rc;
44682#endif
44683#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
44684 defined(SQLITE_HAVE_OS_TRACE)
44685 /*
44686 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
44687 ** OSTRACE() macros.
44688 */
44689 winFile *pFile = (winFile*)id;
44690#else
44691 UNUSED_PARAMETER(id);
44692#endif
44693
44694 assert( pFile );
44695 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
44696 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
44697 || (flags&0x0F)==SQLITE_SYNC_FULL
44698 );
44699
44700 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
44701 ** line is to test that doing so does not cause any problems.
44702 */
44703 SimulateDiskfullError( return SQLITE_FULL );
44704
44705 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
44706 osGetCurrentProcessId(), pFile, pFile->h, flags,
44707 pFile->locktype));
44708
44709#ifndef SQLITE_TEST
44710 UNUSED_PARAMETER(flags);
44711#else
44712 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
44713 sqlite3_fullsync_count++;
44714 }
44715 sqlite3_sync_count++;
44716#endif
44717
44718 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
44719 ** no-op
44720 */
44721#ifdef SQLITE_NO_SYNC
44722 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44723 osGetCurrentProcessId(), pFile, pFile->h));
44724 return SQLITE_OK;
44725#else
44726#if SQLITE_MAX_MMAP_SIZE>0
44727 if( pFile->pMapRegion ){
44728 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
44729 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
44730 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
44731 pFile, pFile->pMapRegion));
44732 }else{
44733 pFile->lastErrno = osGetLastError();
44734 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
44735 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
44736 pFile, pFile->pMapRegion));
44737 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44738 "winSync1", pFile->zPath);
44739 }
44740 }
44741#endif
44742 rc = osFlushFileBuffers(pFile->h);
44743 SimulateIOError( rc=FALSE );
44744 if( rc ){
44745 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44746 osGetCurrentProcessId(), pFile, pFile->h));
44747 return SQLITE_OK;
44748 }else{
44749 pFile->lastErrno = osGetLastError();
44750 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
44751 osGetCurrentProcessId(), pFile, pFile->h));
44752 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
44753 "winSync2", pFile->zPath);
44754 }
44755#endif
44756}
44757
44758/*
44759** Determine the current size of a file in bytes
44760*/
44761static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
44762 winFile *pFile = (winFile*)id;
44763 int rc = SQLITE_OK;
44764
44765 assert( id!=0 );
44766 assert( pSize!=0 );
44767 SimulateIOError(return SQLITE_IOERR_FSTAT);
44768 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
44769
44770#if SQLITE_OS_WINRT
44771 {
44772 FILE_STANDARD_INFO info;
44773 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
44774 &info, sizeof(info)) ){
44775 *pSize = info.EndOfFile.QuadPart;
44776 }else{
44777 pFile->lastErrno = osGetLastError();
44778 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
44779 "winFileSize", pFile->zPath);
44780 }
44781 }
44782#else
44783 {
44784 DWORD upperBits;
44785 DWORD lowerBits;
44786 DWORD lastErrno;
44787
44788 lowerBits = osGetFileSize(pFile->h, &upperBits);
44789 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
44790 if( (lowerBits == INVALID_FILE_SIZE)
44791 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
44792 pFile->lastErrno = lastErrno;
44793 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
44794 "winFileSize", pFile->zPath);
44795 }
44796 }
44797#endif
44798 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
44799 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
44800 return rc;
44801}
44802
44803/*
44804** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
44805*/
44806#ifndef LOCKFILE_FAIL_IMMEDIATELY
44807# define LOCKFILE_FAIL_IMMEDIATELY 1
44808#endif
44809
44810#ifndef LOCKFILE_EXCLUSIVE_LOCK
44811# define LOCKFILE_EXCLUSIVE_LOCK 2
44812#endif
44813
44814/*
44815** Historically, SQLite has used both the LockFile and LockFileEx functions.
44816** When the LockFile function was used, it was always expected to fail
44817** immediately if the lock could not be obtained. Also, it always expected to
44818** obtain an exclusive lock. These flags are used with the LockFileEx function
44819** and reflect those expectations; therefore, they should not be changed.
44820*/
44821#ifndef SQLITE_LOCKFILE_FLAGS
44822# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
44823 LOCKFILE_EXCLUSIVE_LOCK)
44824#endif
44825
44826/*
44827** Currently, SQLite never calls the LockFileEx function without wanting the
44828** call to fail immediately if the lock cannot be obtained.
44829*/
44830#ifndef SQLITE_LOCKFILEEX_FLAGS
44831# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
44832#endif
44833
44834/*
44835** Acquire a reader lock.
44836** Different API routines are called depending on whether or not this
44837** is Win9x or WinNT.
44838*/
44839static int winGetReadLock(winFile *pFile){
44840 int res;
44841 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
44842 if( osIsNT() ){
44843#if SQLITE_OS_WINCE
44844 /*
44845 ** NOTE: Windows CE is handled differently here due its lack of the Win32
44846 ** API LockFileEx.
44847 */
44848 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
44849#else
44850 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
44851 SHARED_SIZE, 0);
44852#endif
44853 }
44854#ifdef SQLITE_WIN32_HAS_ANSI
44855 else{
44856 int lk;
44857 sqlite3_randomness(sizeof(lk), &lk);
44858 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
44859 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
44860 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
44861 }
44862#endif
44863 if( res == 0 ){
44864 pFile->lastErrno = osGetLastError();
44865 /* No need to log a failure to lock */
44866 }
44867 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
44868 return res;
44869}
44870
44871/*
44872** Undo a readlock
44873*/
44874static int winUnlockReadLock(winFile *pFile){
44875 int res;
44876 DWORD lastErrno;
44877 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
44878 if( osIsNT() ){
44879 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
44880 }
44881#ifdef SQLITE_WIN32_HAS_ANSI
44882 else{
44883 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
44884 }
44885#endif
44886 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
44887 pFile->lastErrno = lastErrno;
44888 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
44889 "winUnlockReadLock", pFile->zPath);
44890 }
44891 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
44892 return res;
44893}
44894
44895/*
44896** Lock the file with the lock specified by parameter locktype - one
44897** of the following:
44898**
44899** (1) SHARED_LOCK
44900** (2) RESERVED_LOCK
44901** (3) PENDING_LOCK
44902** (4) EXCLUSIVE_LOCK
44903**
44904** Sometimes when requesting one lock state, additional lock states
44905** are inserted in between. The locking might fail on one of the later
44906** transitions leaving the lock state different from what it started but
44907** still short of its goal. The following chart shows the allowed
44908** transitions and the inserted intermediate states:
44909**
44910** UNLOCKED -> SHARED
44911** SHARED -> RESERVED
44912** SHARED -> (PENDING) -> EXCLUSIVE
44913** RESERVED -> (PENDING) -> EXCLUSIVE
44914** PENDING -> EXCLUSIVE
44915**
44916** This routine will only increase a lock. The winUnlock() routine
44917** erases all locks at once and returns us immediately to locking level 0.
44918** It is not possible to lower the locking level one step at a time. You
44919** must go straight to locking level 0.
44920*/
44921static int winLock(sqlite3_file *id, int locktype){
44922 int rc = SQLITE_OK; /* Return code from subroutines */
44923 int res = 1; /* Result of a Windows lock call */
44924 int newLocktype; /* Set pFile->locktype to this value before exiting */
44925 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
44926 winFile *pFile = (winFile*)id;
44927 DWORD lastErrno = NO_ERROR;
44928
44929 assert( id!=0 );
44930 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
44931 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
44932
44933 /* If there is already a lock of this type or more restrictive on the
44934 ** OsFile, do nothing. Don't use the end_lock: exit path, as
44935 ** sqlite3OsEnterMutex() hasn't been called yet.
44936 */
44937 if( pFile->locktype>=locktype ){
44938 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
44939 return SQLITE_OK;
44940 }
44941
44942 /* Do not allow any kind of write-lock on a read-only database
44943 */
44944 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
44945 return SQLITE_IOERR_LOCK;
44946 }
44947
44948 /* Make sure the locking sequence is correct
44949 */
44950 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
44951 assert( locktype!=PENDING_LOCK );
44952 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
44953
44954 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
44955 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
44956 ** the PENDING_LOCK byte is temporary.
44957 */
44958 newLocktype = pFile->locktype;
44959 if( pFile->locktype==NO_LOCK
44960 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
44961 ){
44962 int cnt = 3;
44963 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
44964 PENDING_BYTE, 0, 1, 0))==0 ){
44965 /* Try 3 times to get the pending lock. This is needed to work
44966 ** around problems caused by indexing and/or anti-virus software on
44967 ** Windows systems.
44968 ** If you are using this code as a model for alternative VFSes, do not
44969 ** copy this retry logic. It is a hack intended for Windows only.
44970 */
44971 lastErrno = osGetLastError();
44972 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
44973 pFile->h, cnt, res));
44974 if( lastErrno==ERROR_INVALID_HANDLE ){
44975 pFile->lastErrno = lastErrno;
44976 rc = SQLITE_IOERR_LOCK;
44977 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
44978 pFile->h, cnt, sqlite3ErrName(rc)));
44979 return rc;
44980 }
44981 if( cnt ) sqlite3_win32_sleep(1);
44982 }
44983 gotPendingLock = res;
44984 if( !res ){
44985 lastErrno = osGetLastError();
44986 }
44987 }
44988
44989 /* Acquire a shared lock
44990 */
44991 if( locktype==SHARED_LOCK && res ){
44992 assert( pFile->locktype==NO_LOCK );
44993 res = winGetReadLock(pFile);
44994 if( res ){
44995 newLocktype = SHARED_LOCK;
44996 }else{
44997 lastErrno = osGetLastError();
44998 }
44999 }
45000
45001 /* Acquire a RESERVED lock
45002 */
45003 if( locktype==RESERVED_LOCK && res ){
45004 assert( pFile->locktype==SHARED_LOCK );
45005 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
45006 if( res ){
45007 newLocktype = RESERVED_LOCK;
45008 }else{
45009 lastErrno = osGetLastError();
45010 }
45011 }
45012
45013 /* Acquire a PENDING lock
45014 */
45015 if( locktype==EXCLUSIVE_LOCK && res ){
45016 newLocktype = PENDING_LOCK;
45017 gotPendingLock = 0;
45018 }
45019
45020 /* Acquire an EXCLUSIVE lock
45021 */
45022 if( locktype==EXCLUSIVE_LOCK && res ){
45023 assert( pFile->locktype>=SHARED_LOCK );
45024 res = winUnlockReadLock(pFile);
45025 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
45026 SHARED_SIZE, 0);
45027 if( res ){
45028 newLocktype = EXCLUSIVE_LOCK;
45029 }else{
45030 lastErrno = osGetLastError();
45031 winGetReadLock(pFile);
45032 }
45033 }
45034
45035 /* If we are holding a PENDING lock that ought to be released, then
45036 ** release it now.
45037 */
45038 if( gotPendingLock && locktype==SHARED_LOCK ){
45039 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45040 }
45041
45042 /* Update the state of the lock has held in the file descriptor then
45043 ** return the appropriate result code.
45044 */
45045 if( res ){
45046 rc = SQLITE_OK;
45047 }else{
45048 pFile->lastErrno = lastErrno;
45049 rc = SQLITE_BUSY;
45050 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
45051 pFile->h, locktype, newLocktype));
45052 }
45053 pFile->locktype = (u8)newLocktype;
45054 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
45055 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45056 return rc;
45057}
45058
45059/*
45060** This routine checks if there is a RESERVED lock held on the specified
45061** file by this or any other process. If such a lock is held, return
45062** non-zero, otherwise zero.
45063*/
45064static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
45065 int res;
45066 winFile *pFile = (winFile*)id;
45067
45068 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
45069 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
45070
45071 assert( id!=0 );
45072 if( pFile->locktype>=RESERVED_LOCK ){
45073 res = 1;
45074 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
45075 }else{
45076 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
45077 if( res ){
45078 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45079 }
45080 res = !res;
45081 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
45082 }
45083 *pResOut = res;
45084 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
45085 pFile->h, pResOut, *pResOut));
45086 return SQLITE_OK;
45087}
45088
45089/*
45090** Lower the locking level on file descriptor id to locktype. locktype
45091** must be either NO_LOCK or SHARED_LOCK.
45092**
45093** If the locking level of the file descriptor is already at or below
45094** the requested locking level, this routine is a no-op.
45095**
45096** It is not possible for this routine to fail if the second argument
45097** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
45098** might return SQLITE_IOERR;
45099*/
45100static int winUnlock(sqlite3_file *id, int locktype){
45101 int type;
45102 winFile *pFile = (winFile*)id;
45103 int rc = SQLITE_OK;
45104 assert( pFile!=0 );
45105 assert( locktype<=SHARED_LOCK );
45106 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
45107 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45108 type = pFile->locktype;
45109 if( type>=EXCLUSIVE_LOCK ){
45110 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45111 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
45112 /* This should never happen. We should always be able to
45113 ** reacquire the read lock */
45114 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
45115 "winUnlock", pFile->zPath);
45116 }
45117 }
45118 if( type>=RESERVED_LOCK ){
45119 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45120 }
45121 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
45122 winUnlockReadLock(pFile);
45123 }
45124 if( type>=PENDING_LOCK ){
45125 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45126 }
45127 pFile->locktype = (u8)locktype;
45128 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
45129 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45130 return rc;
45131}
45132
45133/******************************************************************************
45134****************************** No-op Locking **********************************
45135**
45136** Of the various locking implementations available, this is by far the
45137** simplest: locking is ignored. No attempt is made to lock the database
45138** file for reading or writing.
45139**
45140** This locking mode is appropriate for use on read-only databases
45141** (ex: databases that are burned into CD-ROM, for example.) It can
45142** also be used if the application employs some external mechanism to
45143** prevent simultaneous access of the same database by two or more
45144** database connections. But there is a serious risk of database
45145** corruption if this locking mode is used in situations where multiple
45146** database connections are accessing the same database file at the same
45147** time and one or more of those connections are writing.
45148*/
45149
45150static int winNolockLock(sqlite3_file *id, int locktype){
45151 UNUSED_PARAMETER(id);
45152 UNUSED_PARAMETER(locktype);
45153 return SQLITE_OK;
45154}
45155
45156static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
45157 UNUSED_PARAMETER(id);
45158 UNUSED_PARAMETER(pResOut);
45159 return SQLITE_OK;
45160}
45161
45162static int winNolockUnlock(sqlite3_file *id, int locktype){
45163 UNUSED_PARAMETER(id);
45164 UNUSED_PARAMETER(locktype);
45165 return SQLITE_OK;
45166}
45167
45168/******************* End of the no-op lock implementation *********************
45169******************************************************************************/
45170
45171/*
45172** If *pArg is initially negative then this is a query. Set *pArg to
45173** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
45174**
45175** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
45176*/
45177static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
45178 if( *pArg<0 ){
45179 *pArg = (pFile->ctrlFlags & mask)!=0;
45180 }else if( (*pArg)==0 ){
45181 pFile->ctrlFlags &= ~mask;
45182 }else{
45183 pFile->ctrlFlags |= mask;
45184 }
45185}
45186
45187/* Forward references to VFS helper methods used for temporary files */
45188static int winGetTempname(sqlite3_vfs *, char **);
45189static int winIsDir(const void *);
45190static BOOL winIsLongPathPrefix(const char *);
45191static BOOL winIsDriveLetterAndColon(const char *);
45192
45193/*
45194** Control and query of the open file handle.
45195*/
45196static int winFileControl(sqlite3_file *id, int op, void *pArg){
45197 winFile *pFile = (winFile*)id;
45198 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
45199 switch( op ){
45200 case SQLITE_FCNTL_LOCKSTATE: {
45201 *(int*)pArg = pFile->locktype;
45202 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45203 return SQLITE_OK;
45204 }
45205 case SQLITE_FCNTL_LAST_ERRNO: {
45206 *(int*)pArg = (int)pFile->lastErrno;
45207 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45208 return SQLITE_OK;
45209 }
45210 case SQLITE_FCNTL_CHUNK_SIZE: {
45211 pFile->szChunk = *(int *)pArg;
45212 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45213 return SQLITE_OK;
45214 }
45215 case SQLITE_FCNTL_SIZE_HINT: {
45216 if( pFile->szChunk>0 ){
45217 sqlite3_int64 oldSz;
45218 int rc = winFileSize(id, &oldSz);
45219 if( rc==SQLITE_OK ){
45220 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
45221 if( newSz>oldSz ){
45222 SimulateIOErrorBenign(1);
45223 rc = winTruncate(id, newSz);
45224 SimulateIOErrorBenign(0);
45225 }
45226 }
45227 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45228 return rc;
45229 }
45230 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45231 return SQLITE_OK;
45232 }
45233 case SQLITE_FCNTL_PERSIST_WAL: {
45234 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
45235 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45236 return SQLITE_OK;
45237 }
45238 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
45239 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
45240 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45241 return SQLITE_OK;
45242 }
45243 case SQLITE_FCNTL_VFSNAME: {
45244 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
45245 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45246 return SQLITE_OK;
45247 }
45248 case SQLITE_FCNTL_WIN32_AV_RETRY: {
45249 int *a = (int*)pArg;
45250 if( a[0]>0 ){
45251 winIoerrRetry = a[0];
45252 }else{
45253 a[0] = winIoerrRetry;
45254 }
45255 if( a[1]>0 ){
45256 winIoerrRetryDelay = a[1];
45257 }else{
45258 a[1] = winIoerrRetryDelay;
45259 }
45260 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45261 return SQLITE_OK;
45262 }
45263 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
45264 LPHANDLE phFile = (LPHANDLE)pArg;
45265 *phFile = pFile->h;
45266 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45267 return SQLITE_OK;
45268 }
45269#ifdef SQLITE_TEST
45270 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
45271 LPHANDLE phFile = (LPHANDLE)pArg;
45272 HANDLE hOldFile = pFile->h;
45273 pFile->h = *phFile;
45274 *phFile = hOldFile;
45275 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
45276 hOldFile, pFile->h));
45277 return SQLITE_OK;
45278 }
45279#endif
45280 case SQLITE_FCNTL_TEMPFILENAME: {
45281 char *zTFile = 0;
45282 int rc = winGetTempname(pFile->pVfs, &zTFile);
45283 if( rc==SQLITE_OK ){
45284 *(char**)pArg = zTFile;
45285 }
45286 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45287 return rc;
45288 }
45289#if SQLITE_MAX_MMAP_SIZE>0
45290 case SQLITE_FCNTL_MMAP_SIZE: {
45291 i64 newLimit = *(i64*)pArg;
45292 int rc = SQLITE_OK;
45293 if( newLimit>sqlite3GlobalConfig.mxMmap ){
45294 newLimit = sqlite3GlobalConfig.mxMmap;
45295 }
45296
45297 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
45298 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
45299 ** least a 64-bit type. */
45300 if( newLimit>0 && sizeof(SIZE_T)<8 ){
45301 newLimit = (newLimit & 0x7FFFFFFF);
45302 }
45303
45304 *(i64*)pArg = pFile->mmapSizeMax;
45305 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
45306 pFile->mmapSizeMax = newLimit;
45307 if( pFile->mmapSize>0 ){
45308 winUnmapfile(pFile);
45309 rc = winMapfile(pFile, -1);
45310 }
45311 }
45312 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45313 return rc;
45314 }
45315#endif
45316 }
45317 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
45318 return SQLITE_NOTFOUND;
45319}
45320
45321/*
45322** Return the sector size in bytes of the underlying block device for
45323** the specified file. This is almost always 512 bytes, but may be
45324** larger for some devices.
45325**
45326** SQLite code assumes this function cannot fail. It also assumes that
45327** if two files are created in the same file-system directory (i.e.
45328** a database and its journal file) that the sector size will be the
45329** same for both.
45330*/
45331static int winSectorSize(sqlite3_file *id){
45332 (void)id;
45333 return SQLITE_DEFAULT_SECTOR_SIZE;
45334}
45335
45336/*
45337** Return a vector of device characteristics.
45338*/
45339static int winDeviceCharacteristics(sqlite3_file *id){
45340 winFile *p = (winFile*)id;
45341 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
45342 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
45343}
45344
45345/*
45346** Windows will only let you create file view mappings
45347** on allocation size granularity boundaries.
45348** During sqlite3_os_init() we do a GetSystemInfo()
45349** to get the granularity size.
45350*/
45351static SYSTEM_INFO winSysInfo;
45352
45353#ifndef SQLITE_OMIT_WAL
45354
45355/*
45356** Helper functions to obtain and relinquish the global mutex. The
45357** global mutex is used to protect the winLockInfo objects used by
45358** this file, all of which may be shared by multiple threads.
45359**
45360** Function winShmMutexHeld() is used to assert() that the global mutex
45361** is held when required. This function is only used as part of assert()
45362** statements. e.g.
45363**
45364** winShmEnterMutex()
45365** assert( winShmMutexHeld() );
45366** winShmLeaveMutex()
45367*/
45368static sqlite3_mutex *winBigLock = 0;
45369static void winShmEnterMutex(void){
45370 sqlite3_mutex_enter(winBigLock);
45371}
45372static void winShmLeaveMutex(void){
45373 sqlite3_mutex_leave(winBigLock);
45374}
45375#ifndef NDEBUG
45376static int winShmMutexHeld(void) {
45377 return sqlite3_mutex_held(winBigLock);
45378}
45379#endif
45380
45381/*
45382** Object used to represent a single file opened and mmapped to provide
45383** shared memory. When multiple threads all reference the same
45384** log-summary, each thread has its own winFile object, but they all
45385** point to a single instance of this object. In other words, each
45386** log-summary is opened only once per process.
45387**
45388** winShmMutexHeld() must be true when creating or destroying
45389** this object or while reading or writing the following fields:
45390**
45391** nRef
45392** pNext
45393**
45394** The following fields are read-only after the object is created:
45395**
45396** fid
45397** zFilename
45398**
45399** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
45400** winShmMutexHeld() is true when reading or writing any other field
45401** in this structure.
45402**
45403*/
45404struct winShmNode {
45405 sqlite3_mutex *mutex; /* Mutex to access this object */
45406 char *zFilename; /* Name of the file */
45407 winFile hFile; /* File handle from winOpen */
45408
45409 int szRegion; /* Size of shared-memory regions */
45410 int nRegion; /* Size of array apRegion */
45411 u8 isReadonly; /* True if read-only */
45412 u8 isUnlocked; /* True if no DMS lock held */
45413
45414 struct ShmRegion {
45415 HANDLE hMap; /* File handle from CreateFileMapping */
45416 void *pMap;
45417 } *aRegion;
45418 DWORD lastErrno; /* The Windows errno from the last I/O error */
45419
45420 int nRef; /* Number of winShm objects pointing to this */
45421 winShm *pFirst; /* All winShm objects pointing to this */
45422 winShmNode *pNext; /* Next in list of all winShmNode objects */
45423#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45424 u8 nextShmId; /* Next available winShm.id value */
45425#endif
45426};
45427
45428/*
45429** A global array of all winShmNode objects.
45430**
45431** The winShmMutexHeld() must be true while reading or writing this list.
45432*/
45433static winShmNode *winShmNodeList = 0;
45434
45435/*
45436** Structure used internally by this VFS to record the state of an
45437** open shared memory connection.
45438**
45439** The following fields are initialized when this object is created and
45440** are read-only thereafter:
45441**
45442** winShm.pShmNode
45443** winShm.id
45444**
45445** All other fields are read/write. The winShm.pShmNode->mutex must be held
45446** while accessing any read/write fields.
45447*/
45448struct winShm {
45449 winShmNode *pShmNode; /* The underlying winShmNode object */
45450 winShm *pNext; /* Next winShm with the same winShmNode */
45451 u8 hasMutex; /* True if holding the winShmNode mutex */
45452 u16 sharedMask; /* Mask of shared locks held */
45453 u16 exclMask; /* Mask of exclusive locks held */
45454#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45455 u8 id; /* Id of this connection with its winShmNode */
45456#endif
45457};
45458
45459/*
45460** Constants used for locking
45461*/
45462#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
45463#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
45464
45465/*
45466** Apply advisory locks for all n bytes beginning at ofst.
45467*/
45468#define WINSHM_UNLCK 1
45469#define WINSHM_RDLCK 2
45470#define WINSHM_WRLCK 3
45471static int winShmSystemLock(
45472 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
45473 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
45474 int ofst, /* Offset to first byte to be locked/unlocked */
45475 int nByte /* Number of bytes to lock or unlock */
45476){
45477 int rc = 0; /* Result code form Lock/UnlockFileEx() */
45478
45479 /* Access to the winShmNode object is serialized by the caller */
45480 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
45481
45482 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
45483 pFile->hFile.h, lockType, ofst, nByte));
45484
45485 /* Release/Acquire the system-level lock */
45486 if( lockType==WINSHM_UNLCK ){
45487 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
45488 }else{
45489 /* Initialize the locking parameters */
45490 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
45491 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
45492 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
45493 }
45494
45495 if( rc!= 0 ){
45496 rc = SQLITE_OK;
45497 }else{
45498 pFile->lastErrno = osGetLastError();
45499 rc = SQLITE_BUSY;
45500 }
45501
45502 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
45503 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
45504 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
45505
45506 return rc;
45507}
45508
45509/* Forward references to VFS methods */
45510static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
45511static int winDelete(sqlite3_vfs *,const char*,int);
45512
45513/*
45514** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
45515**
45516** This is not a VFS shared-memory method; it is a utility function called
45517** by VFS shared-memory methods.
45518*/
45519static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
45520 winShmNode **pp;
45521 winShmNode *p;
45522 assert( winShmMutexHeld() );
45523 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
45524 osGetCurrentProcessId(), deleteFlag));
45525 pp = &winShmNodeList;
45526 while( (p = *pp)!=0 ){
45527 if( p->nRef==0 ){
45528 int i;
45529 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
45530 for(i=0; i<p->nRegion; i++){
45531 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
45532 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
45533 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
45534 UNUSED_VARIABLE_VALUE(bRc);
45535 bRc = osCloseHandle(p->aRegion[i].hMap);
45536 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
45537 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
45538 UNUSED_VARIABLE_VALUE(bRc);
45539 }
45540 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
45541 SimulateIOErrorBenign(1);
45542 winClose((sqlite3_file *)&p->hFile);
45543 SimulateIOErrorBenign(0);
45544 }
45545 if( deleteFlag ){
45546 SimulateIOErrorBenign(1);
45547 sqlite3BeginBenignMalloc();
45548 winDelete(pVfs, p->zFilename, 0);
45549 sqlite3EndBenignMalloc();
45550 SimulateIOErrorBenign(0);
45551 }
45552 *pp = p->pNext;
45553 sqlite3_free(p->aRegion);
45554 sqlite3_free(p);
45555 }else{
45556 pp = &p->pNext;
45557 }
45558 }
45559}
45560
45561/*
45562** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
45563** take it now. Return SQLITE_OK if successful, or an SQLite error
45564** code otherwise.
45565**
45566** If the DMS cannot be locked because this is a readonly_shm=1
45567** connection and no other process already holds a lock, return
45568** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
45569*/
45570static int winLockSharedMemory(winShmNode *pShmNode){
45571 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
45572
45573 if( rc==SQLITE_OK ){
45574 if( pShmNode->isReadonly ){
45575 pShmNode->isUnlocked = 1;
45576 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45577 return SQLITE_READONLY_CANTINIT;
45578 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
45579 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45580 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
45581 "winLockSharedMemory", pShmNode->zFilename);
45582 }
45583 }
45584
45585 if( rc==SQLITE_OK ){
45586 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45587 }
45588
45589 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
45590}
45591
45592/*
45593** Open the shared-memory area associated with database file pDbFd.
45594**
45595** When opening a new shared-memory file, if no other instances of that
45596** file are currently open, in this process or in other processes, then
45597** the file must be truncated to zero length or have its header cleared.
45598*/
45599static int winOpenSharedMemory(winFile *pDbFd){
45600 struct winShm *p; /* The connection to be opened */
45601 winShmNode *pShmNode = 0; /* The underlying mmapped file */
45602 int rc = SQLITE_OK; /* Result code */
45603 winShmNode *pNew; /* Newly allocated winShmNode */
45604 int nName; /* Size of zName in bytes */
45605
45606 assert( pDbFd->pShm==0 ); /* Not previously opened */
45607
45608 /* Allocate space for the new sqlite3_shm object. Also speculatively
45609 ** allocate space for a new winShmNode and filename.
45610 */
45611 p = sqlite3MallocZero( sizeof(*p) );
45612 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
45613 nName = sqlite3Strlen30(pDbFd->zPath);
45614 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
45615 if( pNew==0 ){
45616 sqlite3_free(p);
45617 return SQLITE_IOERR_NOMEM_BKPT;
45618 }
45619 pNew->zFilename = (char*)&pNew[1];
45620 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
45621 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
45622
45623 /* Look to see if there is an existing winShmNode that can be used.
45624 ** If no matching winShmNode currently exists, create a new one.
45625 */
45626 winShmEnterMutex();
45627 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
45628 /* TBD need to come up with better match here. Perhaps
45629 ** use FILE_ID_BOTH_DIR_INFO Structure.
45630 */
45631 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
45632 }
45633 if( pShmNode ){
45634 sqlite3_free(pNew);
45635 }else{
45636 int inFlags = SQLITE_OPEN_WAL;
45637 int outFlags = 0;
45638
45639 pShmNode = pNew;
45640 pNew = 0;
45641 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
45642 pShmNode->pNext = winShmNodeList;
45643 winShmNodeList = pShmNode;
45644
45645 if( sqlite3GlobalConfig.bCoreMutex ){
45646 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
45647 if( pShmNode->mutex==0 ){
45648 rc = SQLITE_IOERR_NOMEM_BKPT;
45649 goto shm_open_err;
45650 }
45651 }
45652
45653 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
45654 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
45655 }else{
45656 inFlags |= SQLITE_OPEN_READONLY;
45657 }
45658 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
45659 (sqlite3_file*)&pShmNode->hFile,
45660 inFlags, &outFlags);
45661 if( rc!=SQLITE_OK ){
45662 rc = winLogError(rc, osGetLastError(), "winOpenShm",
45663 pShmNode->zFilename);
45664 goto shm_open_err;
45665 }
45666 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
45667
45668 rc = winLockSharedMemory(pShmNode);
45669 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
45670 }
45671
45672 /* Make the new connection a child of the winShmNode */
45673 p->pShmNode = pShmNode;
45674#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
45675 p->id = pShmNode->nextShmId++;
45676#endif
45677 pShmNode->nRef++;
45678 pDbFd->pShm = p;
45679 winShmLeaveMutex();
45680
45681 /* The reference count on pShmNode has already been incremented under
45682 ** the cover of the winShmEnterMutex() mutex and the pointer from the
45683 ** new (struct winShm) object to the pShmNode has been set. All that is
45684 ** left to do is to link the new object into the linked list starting
45685 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
45686 ** mutex.
45687 */
45688 sqlite3_mutex_enter(pShmNode->mutex);
45689 p->pNext = pShmNode->pFirst;
45690 pShmNode->pFirst = p;
45691 sqlite3_mutex_leave(pShmNode->mutex);
45692 return rc;
45693
45694 /* Jump here on any error */
45695shm_open_err:
45696 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
45697 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
45698 sqlite3_free(p);
45699 sqlite3_free(pNew);
45700 winShmLeaveMutex();
45701 return rc;
45702}
45703
45704/*
45705** Close a connection to shared-memory. Delete the underlying
45706** storage if deleteFlag is true.
45707*/
45708static int winShmUnmap(
45709 sqlite3_file *fd, /* Database holding shared memory */
45710 int deleteFlag /* Delete after closing if true */
45711){
45712 winFile *pDbFd; /* Database holding shared-memory */
45713 winShm *p; /* The connection to be closed */
45714 winShmNode *pShmNode; /* The underlying shared-memory file */
45715 winShm **pp; /* For looping over sibling connections */
45716
45717 pDbFd = (winFile*)fd;
45718 p = pDbFd->pShm;
45719 if( p==0 ) return SQLITE_OK;
45720 pShmNode = p->pShmNode;
45721
45722 /* Remove connection p from the set of connections associated
45723 ** with pShmNode */
45724 sqlite3_mutex_enter(pShmNode->mutex);
45725 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
45726 *pp = p->pNext;
45727
45728 /* Free the connection p */
45729 sqlite3_free(p);
45730 pDbFd->pShm = 0;
45731 sqlite3_mutex_leave(pShmNode->mutex);
45732
45733 /* If pShmNode->nRef has reached 0, then close the underlying
45734 ** shared-memory file, too */
45735 winShmEnterMutex();
45736 assert( pShmNode->nRef>0 );
45737 pShmNode->nRef--;
45738 if( pShmNode->nRef==0 ){
45739 winShmPurge(pDbFd->pVfs, deleteFlag);
45740 }
45741 winShmLeaveMutex();
45742
45743 return SQLITE_OK;
45744}
45745
45746/*
45747** Change the lock state for a shared-memory segment.
45748*/
45749static int winShmLock(
45750 sqlite3_file *fd, /* Database file holding the shared memory */
45751 int ofst, /* First lock to acquire or release */
45752 int n, /* Number of locks to acquire or release */
45753 int flags /* What to do with the lock */
45754){
45755 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
45756 winShm *p = pDbFd->pShm; /* The shared memory being locked */
45757 winShm *pX; /* For looping over all siblings */
45758 winShmNode *pShmNode = p->pShmNode;
45759 int rc = SQLITE_OK; /* Result code */
45760 u16 mask; /* Mask of locks to take or release */
45761
45762 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
45763 assert( n>=1 );
45764 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
45765 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
45766 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
45767 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
45768 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
45769
45770 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
45771 assert( n>1 || mask==(1<<ofst) );
45772 sqlite3_mutex_enter(pShmNode->mutex);
45773 if( flags & SQLITE_SHM_UNLOCK ){
45774 u16 allMask = 0; /* Mask of locks held by siblings */
45775
45776 /* See if any siblings hold this same lock */
45777 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45778 if( pX==p ) continue;
45779 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
45780 allMask |= pX->sharedMask;
45781 }
45782
45783 /* Unlock the system-level locks */
45784 if( (mask & allMask)==0 ){
45785 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
45786 }else{
45787 rc = SQLITE_OK;
45788 }
45789
45790 /* Undo the local locks */
45791 if( rc==SQLITE_OK ){
45792 p->exclMask &= ~mask;
45793 p->sharedMask &= ~mask;
45794 }
45795 }else if( flags & SQLITE_SHM_SHARED ){
45796 u16 allShared = 0; /* Union of locks held by connections other than "p" */
45797
45798 /* Find out which shared locks are already held by sibling connections.
45799 ** If any sibling already holds an exclusive lock, go ahead and return
45800 ** SQLITE_BUSY.
45801 */
45802 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45803 if( (pX->exclMask & mask)!=0 ){
45804 rc = SQLITE_BUSY;
45805 break;
45806 }
45807 allShared |= pX->sharedMask;
45808 }
45809
45810 /* Get shared locks at the system level, if necessary */
45811 if( rc==SQLITE_OK ){
45812 if( (allShared & mask)==0 ){
45813 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
45814 }else{
45815 rc = SQLITE_OK;
45816 }
45817 }
45818
45819 /* Get the local shared locks */
45820 if( rc==SQLITE_OK ){
45821 p->sharedMask |= mask;
45822 }
45823 }else{
45824 /* Make sure no sibling connections hold locks that will block this
45825 ** lock. If any do, return SQLITE_BUSY right away.
45826 */
45827 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
45828 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
45829 rc = SQLITE_BUSY;
45830 break;
45831 }
45832 }
45833
45834 /* Get the exclusive locks at the system level. Then if successful
45835 ** also mark the local connection as being locked.
45836 */
45837 if( rc==SQLITE_OK ){
45838 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
45839 if( rc==SQLITE_OK ){
45840 assert( (p->sharedMask & mask)==0 );
45841 p->exclMask |= mask;
45842 }
45843 }
45844 }
45845 sqlite3_mutex_leave(pShmNode->mutex);
45846 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
45847 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
45848 sqlite3ErrName(rc)));
45849 return rc;
45850}
45851
45852/*
45853** Implement a memory barrier or memory fence on shared memory.
45854**
45855** All loads and stores begun before the barrier must complete before
45856** any load or store begun after the barrier.
45857*/
45858static void winShmBarrier(
45859 sqlite3_file *fd /* Database holding the shared memory */
45860){
45861 UNUSED_PARAMETER(fd);
45862 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
45863 winShmEnterMutex(); /* Also mutex, for redundancy */
45864 winShmLeaveMutex();
45865}
45866
45867/*
45868** This function is called to obtain a pointer to region iRegion of the
45869** shared-memory associated with the database file fd. Shared-memory regions
45870** are numbered starting from zero. Each shared-memory region is szRegion
45871** bytes in size.
45872**
45873** If an error occurs, an error code is returned and *pp is set to NULL.
45874**
45875** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
45876** region has not been allocated (by any client, including one running in a
45877** separate process), then *pp is set to NULL and SQLITE_OK returned. If
45878** isWrite is non-zero and the requested shared-memory region has not yet
45879** been allocated, it is allocated by this function.
45880**
45881** If the shared-memory region has already been allocated or is allocated by
45882** this call as described above, then it is mapped into this processes
45883** address space (if it is not already), *pp is set to point to the mapped
45884** memory and SQLITE_OK returned.
45885*/
45886static int winShmMap(
45887 sqlite3_file *fd, /* Handle open on database file */
45888 int iRegion, /* Region to retrieve */
45889 int szRegion, /* Size of regions */
45890 int isWrite, /* True to extend file if necessary */
45891 void volatile **pp /* OUT: Mapped memory */
45892){
45893 winFile *pDbFd = (winFile*)fd;
45894 winShm *pShm = pDbFd->pShm;
45895 winShmNode *pShmNode;
45896 DWORD protect = PAGE_READWRITE;
45897 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
45898 int rc = SQLITE_OK;
45899
45900 if( !pShm ){
45901 rc = winOpenSharedMemory(pDbFd);
45902 if( rc!=SQLITE_OK ) return rc;
45903 pShm = pDbFd->pShm;
45904 assert( pShm!=0 );
45905 }
45906 pShmNode = pShm->pShmNode;
45907
45908 sqlite3_mutex_enter(pShmNode->mutex);
45909 if( pShmNode->isUnlocked ){
45910 rc = winLockSharedMemory(pShmNode);
45911 if( rc!=SQLITE_OK ) goto shmpage_out;
45912 pShmNode->isUnlocked = 0;
45913 }
45914 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
45915
45916 if( pShmNode->nRegion<=iRegion ){
45917 struct ShmRegion *apNew; /* New aRegion[] array */
45918 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
45919 sqlite3_int64 sz; /* Current size of wal-index file */
45920
45921 pShmNode->szRegion = szRegion;
45922
45923 /* The requested region is not mapped into this processes address space.
45924 ** Check to see if it has been allocated (i.e. if the wal-index file is
45925 ** large enough to contain the requested region).
45926 */
45927 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
45928 if( rc!=SQLITE_OK ){
45929 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
45930 "winShmMap1", pDbFd->zPath);
45931 goto shmpage_out;
45932 }
45933
45934 if( sz<nByte ){
45935 /* The requested memory region does not exist. If isWrite is set to
45936 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
45937 **
45938 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
45939 ** the requested memory region.
45940 */
45941 if( !isWrite ) goto shmpage_out;
45942 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
45943 if( rc!=SQLITE_OK ){
45944 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
45945 "winShmMap2", pDbFd->zPath);
45946 goto shmpage_out;
45947 }
45948 }
45949
45950 /* Map the requested memory region into this processes address space. */
45951 apNew = (struct ShmRegion *)sqlite3_realloc64(
45952 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
45953 );
45954 if( !apNew ){
45955 rc = SQLITE_IOERR_NOMEM_BKPT;
45956 goto shmpage_out;
45957 }
45958 pShmNode->aRegion = apNew;
45959
45960 if( pShmNode->isReadonly ){
45961 protect = PAGE_READONLY;
45962 flags = FILE_MAP_READ;
45963 }
45964
45965 while( pShmNode->nRegion<=iRegion ){
45966 HANDLE hMap = NULL; /* file-mapping handle */
45967 void *pMap = 0; /* Mapped memory region */
45968
45969#if SQLITE_OS_WINRT
45970 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
45971 NULL, protect, nByte, NULL
45972 );
45973#elif defined(SQLITE_WIN32_HAS_WIDE)
45974 hMap = osCreateFileMappingW(pShmNode->hFile.h,
45975 NULL, protect, 0, nByte, NULL
45976 );
45977#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
45978 hMap = osCreateFileMappingA(pShmNode->hFile.h,
45979 NULL, protect, 0, nByte, NULL
45980 );
45981#endif
45982 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
45983 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
45984 hMap ? "ok" : "failed"));
45985 if( hMap ){
45986 int iOffset = pShmNode->nRegion*szRegion;
45987 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
45988#if SQLITE_OS_WINRT
45989 pMap = osMapViewOfFileFromApp(hMap, flags,
45990 iOffset - iOffsetShift, szRegion + iOffsetShift
45991 );
45992#else
45993 pMap = osMapViewOfFile(hMap, flags,
45994 0, iOffset - iOffsetShift, szRegion + iOffsetShift
45995 );
45996#endif
45997 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
45998 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
45999 szRegion, pMap ? "ok" : "failed"));
46000 }
46001 if( !pMap ){
46002 pShmNode->lastErrno = osGetLastError();
46003 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
46004 "winShmMap3", pDbFd->zPath);
46005 if( hMap ) osCloseHandle(hMap);
46006 goto shmpage_out;
46007 }
46008
46009 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
46010 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
46011 pShmNode->nRegion++;
46012 }
46013 }
46014
46015shmpage_out:
46016 if( pShmNode->nRegion>iRegion ){
46017 int iOffset = iRegion*szRegion;
46018 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
46019 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
46020 *pp = (void *)&p[iOffsetShift];
46021 }else{
46022 *pp = 0;
46023 }
46024 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
46025 sqlite3_mutex_leave(pShmNode->mutex);
46026 return rc;
46027}
46028
46029#else
46030# define winShmMap 0
46031# define winShmLock 0
46032# define winShmBarrier 0
46033# define winShmUnmap 0
46034#endif /* #ifndef SQLITE_OMIT_WAL */
46035
46036/*
46037** Cleans up the mapped region of the specified file, if any.
46038*/
46039#if SQLITE_MAX_MMAP_SIZE>0
46040static int winUnmapfile(winFile *pFile){
46041 assert( pFile!=0 );
46042 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
46043 "mmapSize=%lld, mmapSizeMax=%lld\n",
46044 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
46045 pFile->mmapSize, pFile->mmapSizeMax));
46046 if( pFile->pMapRegion ){
46047 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
46048 pFile->lastErrno = osGetLastError();
46049 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
46050 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
46051 pFile->pMapRegion));
46052 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46053 "winUnmapfile1", pFile->zPath);
46054 }
46055 pFile->pMapRegion = 0;
46056 pFile->mmapSize = 0;
46057 }
46058 if( pFile->hMap!=NULL ){
46059 if( !osCloseHandle(pFile->hMap) ){
46060 pFile->lastErrno = osGetLastError();
46061 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
46062 osGetCurrentProcessId(), pFile, pFile->hMap));
46063 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46064 "winUnmapfile2", pFile->zPath);
46065 }
46066 pFile->hMap = NULL;
46067 }
46068 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46069 osGetCurrentProcessId(), pFile));
46070 return SQLITE_OK;
46071}
46072
46073/*
46074** Memory map or remap the file opened by file-descriptor pFd (if the file
46075** is already mapped, the existing mapping is replaced by the new). Or, if
46076** there already exists a mapping for this file, and there are still
46077** outstanding xFetch() references to it, this function is a no-op.
46078**
46079** If parameter nByte is non-negative, then it is the requested size of
46080** the mapping to create. Otherwise, if nByte is less than zero, then the
46081** requested size is the size of the file on disk. The actual size of the
46082** created mapping is either the requested size or the value configured
46083** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
46084**
46085** SQLITE_OK is returned if no error occurs (even if the mapping is not
46086** recreated as a result of outstanding references) or an SQLite error
46087** code otherwise.
46088*/
46089static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
46090 sqlite3_int64 nMap = nByte;
46091 int rc;
46092
46093 assert( nMap>=0 || pFd->nFetchOut==0 );
46094 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
46095 osGetCurrentProcessId(), pFd, nByte));
46096
46097 if( pFd->nFetchOut>0 ) return SQLITE_OK;
46098
46099 if( nMap<0 ){
46100 rc = winFileSize((sqlite3_file*)pFd, &nMap);
46101 if( rc ){
46102 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
46103 osGetCurrentProcessId(), pFd));
46104 return SQLITE_IOERR_FSTAT;
46105 }
46106 }
46107 if( nMap>pFd->mmapSizeMax ){
46108 nMap = pFd->mmapSizeMax;
46109 }
46110 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
46111
46112 if( nMap==0 && pFd->mmapSize>0 ){
46113 winUnmapfile(pFd);
46114 }
46115 if( nMap!=pFd->mmapSize ){
46116 void *pNew = 0;
46117 DWORD protect = PAGE_READONLY;
46118 DWORD flags = FILE_MAP_READ;
46119
46120 winUnmapfile(pFd);
46121#ifdef SQLITE_MMAP_READWRITE
46122 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
46123 protect = PAGE_READWRITE;
46124 flags |= FILE_MAP_WRITE;
46125 }
46126#endif
46127#if SQLITE_OS_WINRT
46128 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
46129#elif defined(SQLITE_WIN32_HAS_WIDE)
46130 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
46131 (DWORD)((nMap>>32) & 0xffffffff),
46132 (DWORD)(nMap & 0xffffffff), NULL);
46133#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
46134 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
46135 (DWORD)((nMap>>32) & 0xffffffff),
46136 (DWORD)(nMap & 0xffffffff), NULL);
46137#endif
46138 if( pFd->hMap==NULL ){
46139 pFd->lastErrno = osGetLastError();
46140 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46141 "winMapfile1", pFd->zPath);
46142 /* Log the error, but continue normal operation using xRead/xWrite */
46143 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
46144 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46145 return SQLITE_OK;
46146 }
46147 assert( (nMap % winSysInfo.dwPageSize)==0 );
46148 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
46149#if SQLITE_OS_WINRT
46150 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
46151#else
46152 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
46153#endif
46154 if( pNew==NULL ){
46155 osCloseHandle(pFd->hMap);
46156 pFd->hMap = NULL;
46157 pFd->lastErrno = osGetLastError();
46158 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46159 "winMapfile2", pFd->zPath);
46160 /* Log the error, but continue normal operation using xRead/xWrite */
46161 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
46162 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46163 return SQLITE_OK;
46164 }
46165 pFd->pMapRegion = pNew;
46166 pFd->mmapSize = nMap;
46167 }
46168
46169 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46170 osGetCurrentProcessId(), pFd));
46171 return SQLITE_OK;
46172}
46173#endif /* SQLITE_MAX_MMAP_SIZE>0 */
46174
46175/*
46176** If possible, return a pointer to a mapping of file fd starting at offset
46177** iOff. The mapping must be valid for at least nAmt bytes.
46178**
46179** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
46180** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
46181** Finally, if an error does occur, return an SQLite error code. The final
46182** value of *pp is undefined in this case.
46183**
46184** If this function does return a pointer, the caller must eventually
46185** release the reference by calling winUnfetch().
46186*/
46187static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
46188#if SQLITE_MAX_MMAP_SIZE>0
46189 winFile *pFd = (winFile*)fd; /* The underlying database file */
46190#endif
46191 *pp = 0;
46192
46193 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
46194 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
46195
46196#if SQLITE_MAX_MMAP_SIZE>0
46197 if( pFd->mmapSizeMax>0 ){
46198 if( pFd->pMapRegion==0 ){
46199 int rc = winMapfile(pFd, -1);
46200 if( rc!=SQLITE_OK ){
46201 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
46202 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
46203 return rc;
46204 }
46205 }
46206 if( pFd->mmapSize >= iOff+nAmt ){
46207 assert( pFd->pMapRegion!=0 );
46208 *pp = &((u8 *)pFd->pMapRegion)[iOff];
46209 pFd->nFetchOut++;
46210 }
46211 }
46212#endif
46213
46214 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
46215 osGetCurrentProcessId(), fd, pp, *pp));
46216 return SQLITE_OK;
46217}
46218
46219/*
46220** If the third argument is non-NULL, then this function releases a
46221** reference obtained by an earlier call to winFetch(). The second
46222** argument passed to this function must be the same as the corresponding
46223** argument that was passed to the winFetch() invocation.
46224**
46225** Or, if the third argument is NULL, then this function is being called
46226** to inform the VFS layer that, according to POSIX, any existing mapping
46227** may now be invalid and should be unmapped.
46228*/
46229static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
46230#if SQLITE_MAX_MMAP_SIZE>0
46231 winFile *pFd = (winFile*)fd; /* The underlying database file */
46232
46233 /* If p==0 (unmap the entire file) then there must be no outstanding
46234 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
46235 ** then there must be at least one outstanding. */
46236 assert( (p==0)==(pFd->nFetchOut==0) );
46237
46238 /* If p!=0, it must match the iOff value. */
46239 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
46240
46241 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
46242 osGetCurrentProcessId(), pFd, iOff, p));
46243
46244 if( p ){
46245 pFd->nFetchOut--;
46246 }else{
46247 /* FIXME: If Windows truly always prevents truncating or deleting a
46248 ** file while a mapping is held, then the following winUnmapfile() call
46249 ** is unnecessary can be omitted - potentially improving
46250 ** performance. */
46251 winUnmapfile(pFd);
46252 }
46253
46254 assert( pFd->nFetchOut>=0 );
46255#endif
46256
46257 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46258 osGetCurrentProcessId(), fd));
46259 return SQLITE_OK;
46260}
46261
46262/*
46263** Here ends the implementation of all sqlite3_file methods.
46264**
46265********************** End sqlite3_file Methods *******************************
46266******************************************************************************/
46267
46268/*
46269** This vector defines all the methods that can operate on an
46270** sqlite3_file for win32.
46271*/
46272static const sqlite3_io_methods winIoMethod = {
46273 3, /* iVersion */
46274 winClose, /* xClose */
46275 winRead, /* xRead */
46276 winWrite, /* xWrite */
46277 winTruncate, /* xTruncate */
46278 winSync, /* xSync */
46279 winFileSize, /* xFileSize */
46280 winLock, /* xLock */
46281 winUnlock, /* xUnlock */
46282 winCheckReservedLock, /* xCheckReservedLock */
46283 winFileControl, /* xFileControl */
46284 winSectorSize, /* xSectorSize */
46285 winDeviceCharacteristics, /* xDeviceCharacteristics */
46286 winShmMap, /* xShmMap */
46287 winShmLock, /* xShmLock */
46288 winShmBarrier, /* xShmBarrier */
46289 winShmUnmap, /* xShmUnmap */
46290 winFetch, /* xFetch */
46291 winUnfetch /* xUnfetch */
46292};
46293
46294/*
46295** This vector defines all the methods that can operate on an
46296** sqlite3_file for win32 without performing any locking.
46297*/
46298static const sqlite3_io_methods winIoNolockMethod = {
46299 3, /* iVersion */
46300 winClose, /* xClose */
46301 winRead, /* xRead */
46302 winWrite, /* xWrite */
46303 winTruncate, /* xTruncate */
46304 winSync, /* xSync */
46305 winFileSize, /* xFileSize */
46306 winNolockLock, /* xLock */
46307 winNolockUnlock, /* xUnlock */
46308 winNolockCheckReservedLock, /* xCheckReservedLock */
46309 winFileControl, /* xFileControl */
46310 winSectorSize, /* xSectorSize */
46311 winDeviceCharacteristics, /* xDeviceCharacteristics */
46312 winShmMap, /* xShmMap */
46313 winShmLock, /* xShmLock */
46314 winShmBarrier, /* xShmBarrier */
46315 winShmUnmap, /* xShmUnmap */
46316 winFetch, /* xFetch */
46317 winUnfetch /* xUnfetch */
46318};
46319
46320static winVfsAppData winAppData = {
46321 &winIoMethod, /* pMethod */
46322 0, /* pAppData */
46323 0 /* bNoLock */
46324};
46325
46326static winVfsAppData winNolockAppData = {
46327 &winIoNolockMethod, /* pMethod */
46328 0, /* pAppData */
46329 1 /* bNoLock */
46330};
46331
46332/****************************************************************************
46333**************************** sqlite3_vfs methods ****************************
46334**
46335** This division contains the implementation of methods on the
46336** sqlite3_vfs object.
46337*/
46338
46339#if defined(__CYGWIN__)
46340/*
46341** Convert a filename from whatever the underlying operating system
46342** supports for filenames into UTF-8. Space to hold the result is
46343** obtained from malloc and must be freed by the calling function.
46344*/
46345static char *winConvertToUtf8Filename(const void *zFilename){
46346 char *zConverted = 0;
46347 if( osIsNT() ){
46348 zConverted = winUnicodeToUtf8(zFilename);
46349 }
46350#ifdef SQLITE_WIN32_HAS_ANSI
46351 else{
46352 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
46353 }
46354#endif
46355 /* caller will handle out of memory */
46356 return zConverted;
46357}
46358#endif
46359
46360/*
46361** Convert a UTF-8 filename into whatever form the underlying
46362** operating system wants filenames in. Space to hold the result
46363** is obtained from malloc and must be freed by the calling
46364** function.
46365*/
46366static void *winConvertFromUtf8Filename(const char *zFilename){
46367 void *zConverted = 0;
46368 if( osIsNT() ){
46369 zConverted = winUtf8ToUnicode(zFilename);
46370 }
46371#ifdef SQLITE_WIN32_HAS_ANSI
46372 else{
46373 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
46374 }
46375#endif
46376 /* caller will handle out of memory */
46377 return zConverted;
46378}
46379
46380/*
46381** This function returns non-zero if the specified UTF-8 string buffer
46382** ends with a directory separator character or one was successfully
46383** added to it.
46384*/
46385static int winMakeEndInDirSep(int nBuf, char *zBuf){
46386 if( zBuf ){
46387 int nLen = sqlite3Strlen30(zBuf);
46388 if( nLen>0 ){
46389 if( winIsDirSep(zBuf[nLen-1]) ){
46390 return 1;
46391 }else if( nLen+1<nBuf ){
46392 zBuf[nLen] = winGetDirSep();
46393 zBuf[nLen+1] = '\0';
46394 return 1;
46395 }
46396 }
46397 }
46398 return 0;
46399}
46400
46401/*
46402** Create a temporary file name and store the resulting pointer into pzBuf.
46403** The pointer returned in pzBuf must be freed via sqlite3_free().
46404*/
46405static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
46406 static char zChars[] =
46407 "abcdefghijklmnopqrstuvwxyz"
46408 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46409 "0123456789";
46410 size_t i, j;
46411 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
46412 int nMax, nBuf, nDir, nLen;
46413 char *zBuf;
46414
46415 /* It's odd to simulate an io-error here, but really this is just
46416 ** using the io-error infrastructure to test that SQLite handles this
46417 ** function failing.
46418 */
46419 SimulateIOError( return SQLITE_IOERR );
46420
46421 /* Allocate a temporary buffer to store the fully qualified file
46422 ** name for the temporary file. If this fails, we cannot continue.
46423 */
46424 nMax = pVfs->mxPathname; nBuf = nMax + 2;
46425 zBuf = sqlite3MallocZero( nBuf );
46426 if( !zBuf ){
46427 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46428 return SQLITE_IOERR_NOMEM_BKPT;
46429 }
46430
46431 /* Figure out the effective temporary directory. First, check if one
46432 ** has been explicitly set by the application; otherwise, use the one
46433 ** configured by the operating system.
46434 */
46435 nDir = nMax - (nPre + 15);
46436 assert( nDir>0 );
46437 if( sqlite3_temp_directory ){
46438 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
46439 if( nDirLen>0 ){
46440 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
46441 nDirLen++;
46442 }
46443 if( nDirLen>nDir ){
46444 sqlite3_free(zBuf);
46445 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46446 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
46447 }
46448 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
46449 }
46450 }
46451#if defined(__CYGWIN__)
46452 else{
46453 static const char *azDirs[] = {
46454 0, /* getenv("SQLITE_TMPDIR") */
46455 0, /* getenv("TMPDIR") */
46456 0, /* getenv("TMP") */
46457 0, /* getenv("TEMP") */
46458 0, /* getenv("USERPROFILE") */
46459 "/var/tmp",
46460 "/usr/tmp",
46461 "/tmp",
46462 ".",
46463 0 /* List terminator */
46464 };
46465 unsigned int i;
46466 const char *zDir = 0;
46467
46468 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
46469 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
46470 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
46471 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
46472 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
46473 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
46474 void *zConverted;
46475 if( zDir==0 ) continue;
46476 /* If the path starts with a drive letter followed by the colon
46477 ** character, assume it is already a native Win32 path; otherwise,
46478 ** it must be converted to a native Win32 path via the Cygwin API
46479 ** prior to using it.
46480 */
46481 if( winIsDriveLetterAndColon(zDir) ){
46482 zConverted = winConvertFromUtf8Filename(zDir);
46483 if( !zConverted ){
46484 sqlite3_free(zBuf);
46485 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46486 return SQLITE_IOERR_NOMEM_BKPT;
46487 }
46488 if( winIsDir(zConverted) ){
46489 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
46490 sqlite3_free(zConverted);
46491 break;
46492 }
46493 sqlite3_free(zConverted);
46494 }else{
46495 zConverted = sqlite3MallocZero( nMax+1 );
46496 if( !zConverted ){
46497 sqlite3_free(zBuf);
46498 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46499 return SQLITE_IOERR_NOMEM_BKPT;
46500 }
46501 if( cygwin_conv_path(
46502 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
46503 zConverted, nMax+1)<0 ){
46504 sqlite3_free(zConverted);
46505 sqlite3_free(zBuf);
46506 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
46507 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
46508 "winGetTempname2", zDir);
46509 }
46510 if( winIsDir(zConverted) ){
46511 /* At this point, we know the candidate directory exists and should
46512 ** be used. However, we may need to convert the string containing
46513 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
46514 */
46515 char *zUtf8 = winConvertToUtf8Filename(zConverted);
46516 if( !zUtf8 ){
46517 sqlite3_free(zConverted);
46518 sqlite3_free(zBuf);
46519 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46520 return SQLITE_IOERR_NOMEM_BKPT;
46521 }
46522 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
46523 sqlite3_free(zUtf8);
46524 sqlite3_free(zConverted);
46525 break;
46526 }
46527 sqlite3_free(zConverted);
46528 }
46529 }
46530 }
46531#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
46532 else if( osIsNT() ){
46533 char *zMulti;
46534 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
46535 if( !zWidePath ){
46536 sqlite3_free(zBuf);
46537 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46538 return SQLITE_IOERR_NOMEM_BKPT;
46539 }
46540 if( osGetTempPathW(nMax, zWidePath)==0 ){
46541 sqlite3_free(zWidePath);
46542 sqlite3_free(zBuf);
46543 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
46544 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
46545 "winGetTempname2", 0);
46546 }
46547 zMulti = winUnicodeToUtf8(zWidePath);
46548 if( zMulti ){
46549 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
46550 sqlite3_free(zMulti);
46551 sqlite3_free(zWidePath);
46552 }else{
46553 sqlite3_free(zWidePath);
46554 sqlite3_free(zBuf);
46555 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46556 return SQLITE_IOERR_NOMEM_BKPT;
46557 }
46558 }
46559#ifdef SQLITE_WIN32_HAS_ANSI
46560 else{
46561 char *zUtf8;
46562 char *zMbcsPath = sqlite3MallocZero( nMax );
46563 if( !zMbcsPath ){
46564 sqlite3_free(zBuf);
46565 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46566 return SQLITE_IOERR_NOMEM_BKPT;
46567 }
46568 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
46569 sqlite3_free(zBuf);
46570 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
46571 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
46572 "winGetTempname3", 0);
46573 }
46574 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
46575 if( zUtf8 ){
46576 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
46577 sqlite3_free(zUtf8);
46578 }else{
46579 sqlite3_free(zBuf);
46580 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46581 return SQLITE_IOERR_NOMEM_BKPT;
46582 }
46583 }
46584#endif /* SQLITE_WIN32_HAS_ANSI */
46585#endif /* !SQLITE_OS_WINRT */
46586
46587 /*
46588 ** Check to make sure the temporary directory ends with an appropriate
46589 ** separator. If it does not and there is not enough space left to add
46590 ** one, fail.
46591 */
46592 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
46593 sqlite3_free(zBuf);
46594 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46595 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
46596 }
46597
46598 /*
46599 ** Check that the output buffer is large enough for the temporary file
46600 ** name in the following format:
46601 **
46602 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
46603 **
46604 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
46605 ** account for the space used by the 15 character random suffix and the
46606 ** two trailing NUL characters. The final directory separator character
46607 ** has already added if it was not already present.
46608 */
46609 nLen = sqlite3Strlen30(zBuf);
46610 if( (nLen + nPre + 17) > nBuf ){
46611 sqlite3_free(zBuf);
46612 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46613 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
46614 }
46615
46616 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
46617
46618 j = sqlite3Strlen30(zBuf);
46619 sqlite3_randomness(15, &zBuf[j]);
46620 for(i=0; i<15; i++, j++){
46621 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
46622 }
46623 zBuf[j] = 0;
46624 zBuf[j+1] = 0;
46625 *pzBuf = zBuf;
46626
46627 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
46628 return SQLITE_OK;
46629}
46630
46631/*
46632** Return TRUE if the named file is really a directory. Return false if
46633** it is something other than a directory, or if there is any kind of memory
46634** allocation failure.
46635*/
46636static int winIsDir(const void *zConverted){
46637 DWORD attr;
46638 int rc = 0;
46639 DWORD lastErrno;
46640
46641 if( osIsNT() ){
46642 int cnt = 0;
46643 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
46644 memset(&sAttrData, 0, sizeof(sAttrData));
46645 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
46646 GetFileExInfoStandard,
46647 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
46648 if( !rc ){
46649 return 0; /* Invalid name? */
46650 }
46651 attr = sAttrData.dwFileAttributes;
46652#if SQLITE_OS_WINCE==0
46653 }else{
46654 attr = osGetFileAttributesA((char*)zConverted);
46655#endif
46656 }
46657 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
46658}
46659
46660/* forward reference */
46661static int winAccess(
46662 sqlite3_vfs *pVfs, /* Not used on win32 */
46663 const char *zFilename, /* Name of file to check */
46664 int flags, /* Type of test to make on this file */
46665 int *pResOut /* OUT: Result */
46666);
46667
46668/*
46669** Open a file.
46670*/
46671static int winOpen(
46672 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
46673 const char *zName, /* Name of the file (UTF-8) */
46674 sqlite3_file *id, /* Write the SQLite file handle here */
46675 int flags, /* Open mode flags */
46676 int *pOutFlags /* Status return flags */
46677){
46678 HANDLE h;
46679 DWORD lastErrno = 0;
46680 DWORD dwDesiredAccess;
46681 DWORD dwShareMode;
46682 DWORD dwCreationDisposition;
46683 DWORD dwFlagsAndAttributes = 0;
46684#if SQLITE_OS_WINCE
46685 int isTemp = 0;
46686#endif
46687 winVfsAppData *pAppData;
46688 winFile *pFile = (winFile*)id;
46689 void *zConverted; /* Filename in OS encoding */
46690 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
46691 int cnt = 0;
46692
46693 /* If argument zPath is a NULL pointer, this function is required to open
46694 ** a temporary file. Use this buffer to store the file name in.
46695 */
46696 char *zTmpname = 0; /* For temporary filename, if necessary. */
46697
46698 int rc = SQLITE_OK; /* Function Return Code */
46699#if !defined(NDEBUG) || SQLITE_OS_WINCE
46700 int eType = flags&0xFFFFFF00; /* Type of file to open */
46701#endif
46702
46703 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
46704 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
46705 int isCreate = (flags & SQLITE_OPEN_CREATE);
46706 int isReadonly = (flags & SQLITE_OPEN_READONLY);
46707 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
46708
46709#ifndef NDEBUG
46710 int isOpenJournal = (isCreate && (
46711 eType==SQLITE_OPEN_SUPER_JOURNAL
46712 || eType==SQLITE_OPEN_MAIN_JOURNAL
46713 || eType==SQLITE_OPEN_WAL
46714 ));
46715#endif
46716
46717 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
46718 zUtf8Name, id, flags, pOutFlags));
46719
46720 /* Check the following statements are true:
46721 **
46722 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
46723 ** (b) if CREATE is set, then READWRITE must also be set, and
46724 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
46725 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
46726 */
46727 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
46728 assert(isCreate==0 || isReadWrite);
46729 assert(isExclusive==0 || isCreate);
46730 assert(isDelete==0 || isCreate);
46731
46732 /* The main DB, main journal, WAL file and super-journal are never
46733 ** automatically deleted. Nor are they ever temporary files. */
46734 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
46735 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
46736 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
46737 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
46738
46739 /* Assert that the upper layer has set one of the "file-type" flags. */
46740 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
46741 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
46742 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
46743 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
46744 );
46745
46746 assert( pFile!=0 );
46747 memset(pFile, 0, sizeof(winFile));
46748 pFile->h = INVALID_HANDLE_VALUE;
46749
46750#if SQLITE_OS_WINRT
46751 if( !zUtf8Name && !sqlite3_temp_directory ){
46752 sqlite3_log(SQLITE_ERROR,
46753 "sqlite3_temp_directory variable should be set for WinRT");
46754 }
46755#endif
46756
46757 /* If the second argument to this function is NULL, generate a
46758 ** temporary file name to use
46759 */
46760 if( !zUtf8Name ){
46761 assert( isDelete && !isOpenJournal );
46762 rc = winGetTempname(pVfs, &zTmpname);
46763 if( rc!=SQLITE_OK ){
46764 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
46765 return rc;
46766 }
46767 zUtf8Name = zTmpname;
46768 }
46769
46770 /* Database filenames are double-zero terminated if they are not
46771 ** URIs with parameters. Hence, they can always be passed into
46772 ** sqlite3_uri_parameter().
46773 */
46774 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
46775 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
46776
46777 /* Convert the filename to the system encoding. */
46778 zConverted = winConvertFromUtf8Filename(zUtf8Name);
46779 if( zConverted==0 ){
46780 sqlite3_free(zTmpname);
46781 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
46782 return SQLITE_IOERR_NOMEM_BKPT;
46783 }
46784
46785 if( winIsDir(zConverted) ){
46786 sqlite3_free(zConverted);
46787 sqlite3_free(zTmpname);
46788 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
46789 return SQLITE_CANTOPEN_ISDIR;
46790 }
46791
46792 if( isReadWrite ){
46793 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
46794 }else{
46795 dwDesiredAccess = GENERIC_READ;
46796 }
46797
46798 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
46799 ** created. SQLite doesn't use it to indicate "exclusive access"
46800 ** as it is usually understood.
46801 */
46802 if( isExclusive ){
46803 /* Creates a new file, only if it does not already exist. */
46804 /* If the file exists, it fails. */
46805 dwCreationDisposition = CREATE_NEW;
46806 }else if( isCreate ){
46807 /* Open existing file, or create if it doesn't exist */
46808 dwCreationDisposition = OPEN_ALWAYS;
46809 }else{
46810 /* Opens a file, only if it exists. */
46811 dwCreationDisposition = OPEN_EXISTING;
46812 }
46813
46814 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
46815
46816 if( isDelete ){
46817#if SQLITE_OS_WINCE
46818 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
46819 isTemp = 1;
46820#else
46821 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
46822 | FILE_ATTRIBUTE_HIDDEN
46823 | FILE_FLAG_DELETE_ON_CLOSE;
46824#endif
46825 }else{
46826 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
46827 }
46828 /* Reports from the internet are that performance is always
46829 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
46830#if SQLITE_OS_WINCE
46831 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
46832#endif
46833
46834 if( osIsNT() ){
46835#if SQLITE_OS_WINRT
46836 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
46837 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
46838 extendedParameters.dwFileAttributes =
46839 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
46840 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
46841 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
46842 extendedParameters.lpSecurityAttributes = NULL;
46843 extendedParameters.hTemplateFile = NULL;
46844 do{
46845 h = osCreateFile2((LPCWSTR)zConverted,
46846 dwDesiredAccess,
46847 dwShareMode,
46848 dwCreationDisposition,
46849 &extendedParameters);
46850 if( h!=INVALID_HANDLE_VALUE ) break;
46851 if( isReadWrite ){
46852 int rc2, isRO = 0;
46853 sqlite3BeginBenignMalloc();
46854 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46855 sqlite3EndBenignMalloc();
46856 if( rc2==SQLITE_OK && isRO ) break;
46857 }
46858 }while( winRetryIoerr(&cnt, &lastErrno) );
46859#else
46860 do{
46861 h = osCreateFileW((LPCWSTR)zConverted,
46862 dwDesiredAccess,
46863 dwShareMode, NULL,
46864 dwCreationDisposition,
46865 dwFlagsAndAttributes,
46866 NULL);
46867 if( h!=INVALID_HANDLE_VALUE ) break;
46868 if( isReadWrite ){
46869 int rc2, isRO = 0;
46870 sqlite3BeginBenignMalloc();
46871 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46872 sqlite3EndBenignMalloc();
46873 if( rc2==SQLITE_OK && isRO ) break;
46874 }
46875 }while( winRetryIoerr(&cnt, &lastErrno) );
46876#endif
46877 }
46878#ifdef SQLITE_WIN32_HAS_ANSI
46879 else{
46880 do{
46881 h = osCreateFileA((LPCSTR)zConverted,
46882 dwDesiredAccess,
46883 dwShareMode, NULL,
46884 dwCreationDisposition,
46885 dwFlagsAndAttributes,
46886 NULL);
46887 if( h!=INVALID_HANDLE_VALUE ) break;
46888 if( isReadWrite ){
46889 int rc2, isRO = 0;
46890 sqlite3BeginBenignMalloc();
46891 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
46892 sqlite3EndBenignMalloc();
46893 if( rc2==SQLITE_OK && isRO ) break;
46894 }
46895 }while( winRetryIoerr(&cnt, &lastErrno) );
46896 }
46897#endif
46898 winLogIoerr(cnt, __LINE__);
46899
46900 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
46901 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
46902
46903 if( h==INVALID_HANDLE_VALUE ){
46904 sqlite3_free(zConverted);
46905 sqlite3_free(zTmpname);
46906 if( isReadWrite && !isExclusive ){
46907 return winOpen(pVfs, zName, id,
46908 ((flags|SQLITE_OPEN_READONLY) &
46909 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
46910 pOutFlags);
46911 }else{
46912 pFile->lastErrno = lastErrno;
46913 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
46914 return SQLITE_CANTOPEN_BKPT;
46915 }
46916 }
46917
46918 if( pOutFlags ){
46919 if( isReadWrite ){
46920 *pOutFlags = SQLITE_OPEN_READWRITE;
46921 }else{
46922 *pOutFlags = SQLITE_OPEN_READONLY;
46923 }
46924 }
46925
46926 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
46927 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
46928 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
46929
46930 pAppData = (winVfsAppData*)pVfs->pAppData;
46931
46932#if SQLITE_OS_WINCE
46933 {
46934 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
46935 && ((pAppData==NULL) || !pAppData->bNoLock)
46936 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
46937 ){
46938 osCloseHandle(h);
46939 sqlite3_free(zConverted);
46940 sqlite3_free(zTmpname);
46941 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
46942 return rc;
46943 }
46944 }
46945 if( isTemp ){
46946 pFile->zDeleteOnClose = zConverted;
46947 }else
46948#endif
46949 {
46950 sqlite3_free(zConverted);
46951 }
46952
46953 sqlite3_free(zTmpname);
46954 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
46955 pFile->pVfs = pVfs;
46956 pFile->h = h;
46957 if( isReadonly ){
46958 pFile->ctrlFlags |= WINFILE_RDONLY;
46959 }
46960 if( (flags & SQLITE_OPEN_MAIN_DB)
46961 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE)
46962 ){
46963 pFile->ctrlFlags |= WINFILE_PSOW;
46964 }
46965 pFile->lastErrno = NO_ERROR;
46966 pFile->zPath = zName;
46967#if SQLITE_MAX_MMAP_SIZE>0
46968 pFile->hMap = NULL;
46969 pFile->pMapRegion = 0;
46970 pFile->mmapSize = 0;
46971 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
46972#endif
46973
46974 OpenCounter(+1);
46975 return rc;
46976}
46977
46978/*
46979** Delete the named file.
46980**
46981** Note that Windows does not allow a file to be deleted if some other
46982** process has it open. Sometimes a virus scanner or indexing program
46983** will open a journal file shortly after it is created in order to do
46984** whatever it does. While this other process is holding the
46985** file open, we will be unable to delete it. To work around this
46986** problem, we delay 100 milliseconds and try to delete again. Up
46987** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
46988** up and returning an error.
46989*/
46990static int winDelete(
46991 sqlite3_vfs *pVfs, /* Not used on win32 */
46992 const char *zFilename, /* Name of file to delete */
46993 int syncDir /* Not used on win32 */
46994){
46995 int cnt = 0;
46996 int rc;
46997 DWORD attr;
46998 DWORD lastErrno = 0;
46999 void *zConverted;
47000 UNUSED_PARAMETER(pVfs);
47001 UNUSED_PARAMETER(syncDir);
47002
47003 SimulateIOError(return SQLITE_IOERR_DELETE);
47004 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
47005
47006 zConverted = winConvertFromUtf8Filename(zFilename);
47007 if( zConverted==0 ){
47008 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
47009 return SQLITE_IOERR_NOMEM_BKPT;
47010 }
47011 if( osIsNT() ){
47012 do {
47013#if SQLITE_OS_WINRT
47014 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47015 memset(&sAttrData, 0, sizeof(sAttrData));
47016 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
47017 &sAttrData) ){
47018 attr = sAttrData.dwFileAttributes;
47019 }else{
47020 lastErrno = osGetLastError();
47021 if( lastErrno==ERROR_FILE_NOT_FOUND
47022 || lastErrno==ERROR_PATH_NOT_FOUND ){
47023 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47024 }else{
47025 rc = SQLITE_ERROR;
47026 }
47027 break;
47028 }
47029#else
47030 attr = osGetFileAttributesW(zConverted);
47031#endif
47032 if ( attr==INVALID_FILE_ATTRIBUTES ){
47033 lastErrno = osGetLastError();
47034 if( lastErrno==ERROR_FILE_NOT_FOUND
47035 || lastErrno==ERROR_PATH_NOT_FOUND ){
47036 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47037 }else{
47038 rc = SQLITE_ERROR;
47039 }
47040 break;
47041 }
47042 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
47043 rc = SQLITE_ERROR; /* Files only. */
47044 break;
47045 }
47046 if ( osDeleteFileW(zConverted) ){
47047 rc = SQLITE_OK; /* Deleted OK. */
47048 break;
47049 }
47050 if ( !winRetryIoerr(&cnt, &lastErrno) ){
47051 rc = SQLITE_ERROR; /* No more retries. */
47052 break;
47053 }
47054 } while(1);
47055 }
47056#ifdef SQLITE_WIN32_HAS_ANSI
47057 else{
47058 do {
47059 attr = osGetFileAttributesA(zConverted);
47060 if ( attr==INVALID_FILE_ATTRIBUTES ){
47061 lastErrno = osGetLastError();
47062 if( lastErrno==ERROR_FILE_NOT_FOUND
47063 || lastErrno==ERROR_PATH_NOT_FOUND ){
47064 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
47065 }else{
47066 rc = SQLITE_ERROR;
47067 }
47068 break;
47069 }
47070 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
47071 rc = SQLITE_ERROR; /* Files only. */
47072 break;
47073 }
47074 if ( osDeleteFileA(zConverted) ){
47075 rc = SQLITE_OK; /* Deleted OK. */
47076 break;
47077 }
47078 if ( !winRetryIoerr(&cnt, &lastErrno) ){
47079 rc = SQLITE_ERROR; /* No more retries. */
47080 break;
47081 }
47082 } while(1);
47083 }
47084#endif
47085 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
47086 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
47087 }else{
47088 winLogIoerr(cnt, __LINE__);
47089 }
47090 sqlite3_free(zConverted);
47091 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
47092 return rc;
47093}
47094
47095/*
47096** Check the existence and status of a file.
47097*/
47098static int winAccess(
47099 sqlite3_vfs *pVfs, /* Not used on win32 */
47100 const char *zFilename, /* Name of file to check */
47101 int flags, /* Type of test to make on this file */
47102 int *pResOut /* OUT: Result */
47103){
47104 DWORD attr;
47105 int rc = 0;
47106 DWORD lastErrno = 0;
47107 void *zConverted;
47108 UNUSED_PARAMETER(pVfs);
47109
47110 SimulateIOError( return SQLITE_IOERR_ACCESS; );
47111 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
47112 zFilename, flags, pResOut));
47113
47114 zConverted = winConvertFromUtf8Filename(zFilename);
47115 if( zConverted==0 ){
47116 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
47117 return SQLITE_IOERR_NOMEM_BKPT;
47118 }
47119 if( osIsNT() ){
47120 int cnt = 0;
47121 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
47122 memset(&sAttrData, 0, sizeof(sAttrData));
47123 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
47124 GetFileExInfoStandard,
47125 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
47126 if( rc ){
47127 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
47128 ** as if it does not exist.
47129 */
47130 if( flags==SQLITE_ACCESS_EXISTS
47131 && sAttrData.nFileSizeHigh==0
47132 && sAttrData.nFileSizeLow==0 ){
47133 attr = INVALID_FILE_ATTRIBUTES;
47134 }else{
47135 attr = sAttrData.dwFileAttributes;
47136 }
47137 }else{
47138 winLogIoerr(cnt, __LINE__);
47139 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
47140 sqlite3_free(zConverted);
47141 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
47142 zFilename);
47143 }else{
47144 attr = INVALID_FILE_ATTRIBUTES;
47145 }
47146 }
47147 }
47148#ifdef SQLITE_WIN32_HAS_ANSI
47149 else{
47150 attr = osGetFileAttributesA((char*)zConverted);
47151 }
47152#endif
47153 sqlite3_free(zConverted);
47154 switch( flags ){
47155 case SQLITE_ACCESS_READ:
47156 case SQLITE_ACCESS_EXISTS:
47157 rc = attr!=INVALID_FILE_ATTRIBUTES;
47158 break;
47159 case SQLITE_ACCESS_READWRITE:
47160 rc = attr!=INVALID_FILE_ATTRIBUTES &&
47161 (attr & FILE_ATTRIBUTE_READONLY)==0;
47162 break;
47163 default:
47164 assert(!"Invalid flags argument");
47165 }
47166 *pResOut = rc;
47167 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
47168 zFilename, pResOut, *pResOut));
47169 return SQLITE_OK;
47170}
47171
47172/*
47173** Returns non-zero if the specified path name starts with the "long path"
47174** prefix.
47175*/
47176static BOOL winIsLongPathPrefix(
47177 const char *zPathname
47178){
47179 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
47180 && zPathname[2]=='?' && zPathname[3]=='\\' );
47181}
47182
47183/*
47184** Returns non-zero if the specified path name starts with a drive letter
47185** followed by a colon character.
47186*/
47187static BOOL winIsDriveLetterAndColon(
47188 const char *zPathname
47189){
47190 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
47191}
47192
47193/*
47194** Returns non-zero if the specified path name should be used verbatim. If
47195** non-zero is returned from this function, the calling function must simply
47196** use the provided path name verbatim -OR- resolve it into a full path name
47197** using the GetFullPathName Win32 API function (if available).
47198*/
47199static BOOL winIsVerbatimPathname(
47200 const char *zPathname
47201){
47202 /*
47203 ** If the path name starts with a forward slash or a backslash, it is either
47204 ** a legal UNC name, a volume relative path, or an absolute path name in the
47205 ** "Unix" format on Windows. There is no easy way to differentiate between
47206 ** the final two cases; therefore, we return the safer return value of TRUE
47207 ** so that callers of this function will simply use it verbatim.
47208 */
47209 if ( winIsDirSep(zPathname[0]) ){
47210 return TRUE;
47211 }
47212
47213 /*
47214 ** If the path name starts with a letter and a colon it is either a volume
47215 ** relative path or an absolute path. Callers of this function must not
47216 ** attempt to treat it as a relative path name (i.e. they should simply use
47217 ** it verbatim).
47218 */
47219 if ( winIsDriveLetterAndColon(zPathname) ){
47220 return TRUE;
47221 }
47222
47223 /*
47224 ** If we get to this point, the path name should almost certainly be a purely
47225 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
47226 */
47227 return FALSE;
47228}
47229
47230/*
47231** Turn a relative pathname into a full pathname. Write the full
47232** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
47233** bytes in size.
47234*/
47235static int winFullPathname(
47236 sqlite3_vfs *pVfs, /* Pointer to vfs object */
47237 const char *zRelative, /* Possibly relative input path */
47238 int nFull, /* Size of output buffer in bytes */
47239 char *zFull /* Output buffer */
47240){
47241#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47242 DWORD nByte;
47243 void *zConverted;
47244 char *zOut;
47245#endif
47246
47247 /* If this path name begins with "/X:" or "\\?\", where "X" is any
47248 ** alphabetic character, discard the initial "/" from the pathname.
47249 */
47250 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
47251 || winIsLongPathPrefix(zRelative+1)) ){
47252 zRelative++;
47253 }
47254
47255#if defined(__CYGWIN__)
47256 SimulateIOError( return SQLITE_ERROR );
47257 UNUSED_PARAMETER(nFull);
47258 assert( nFull>=pVfs->mxPathname );
47259 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47260 /*
47261 ** NOTE: We are dealing with a relative path name and the data
47262 ** directory has been set. Therefore, use it as the basis
47263 ** for converting the relative path name to an absolute
47264 ** one by prepending the data directory and a slash.
47265 */
47266 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47267 if( !zOut ){
47268 return SQLITE_IOERR_NOMEM_BKPT;
47269 }
47270 if( cygwin_conv_path(
47271 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
47272 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
47273 sqlite3_free(zOut);
47274 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47275 "winFullPathname1", zRelative);
47276 }else{
47277 char *zUtf8 = winConvertToUtf8Filename(zOut);
47278 if( !zUtf8 ){
47279 sqlite3_free(zOut);
47280 return SQLITE_IOERR_NOMEM_BKPT;
47281 }
47282 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47283 sqlite3_data_directory, winGetDirSep(), zUtf8);
47284 sqlite3_free(zUtf8);
47285 sqlite3_free(zOut);
47286 }
47287 }else{
47288 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47289 if( !zOut ){
47290 return SQLITE_IOERR_NOMEM_BKPT;
47291 }
47292 if( cygwin_conv_path(
47293 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
47294 zRelative, zOut, pVfs->mxPathname+1)<0 ){
47295 sqlite3_free(zOut);
47296 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
47297 "winFullPathname2", zRelative);
47298 }else{
47299 char *zUtf8 = winConvertToUtf8Filename(zOut);
47300 if( !zUtf8 ){
47301 sqlite3_free(zOut);
47302 return SQLITE_IOERR_NOMEM_BKPT;
47303 }
47304 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
47305 sqlite3_free(zUtf8);
47306 sqlite3_free(zOut);
47307 }
47308 }
47309 return SQLITE_OK;
47310#endif
47311
47312#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
47313 SimulateIOError( return SQLITE_ERROR );
47314 /* WinCE has no concept of a relative pathname, or so I am told. */
47315 /* WinRT has no way to convert a relative path to an absolute one. */
47316 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47317 /*
47318 ** NOTE: We are dealing with a relative path name and the data
47319 ** directory has been set. Therefore, use it as the basis
47320 ** for converting the relative path name to an absolute
47321 ** one by prepending the data directory and a backslash.
47322 */
47323 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47324 sqlite3_data_directory, winGetDirSep(), zRelative);
47325 }else{
47326 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
47327 }
47328 return SQLITE_OK;
47329#endif
47330
47331#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
47332 /* It's odd to simulate an io-error here, but really this is just
47333 ** using the io-error infrastructure to test that SQLite handles this
47334 ** function failing. This function could fail if, for example, the
47335 ** current working directory has been unlinked.
47336 */
47337 SimulateIOError( return SQLITE_ERROR );
47338 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
47339 /*
47340 ** NOTE: We are dealing with a relative path name and the data
47341 ** directory has been set. Therefore, use it as the basis
47342 ** for converting the relative path name to an absolute
47343 ** one by prepending the data directory and a backslash.
47344 */
47345 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47346 sqlite3_data_directory, winGetDirSep(), zRelative);
47347 return SQLITE_OK;
47348 }
47349 zConverted = winConvertFromUtf8Filename(zRelative);
47350 if( zConverted==0 ){
47351 return SQLITE_IOERR_NOMEM_BKPT;
47352 }
47353 if( osIsNT() ){
47354 LPWSTR zTemp;
47355 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
47356 if( nByte==0 ){
47357 sqlite3_free(zConverted);
47358 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47359 "winFullPathname1", zRelative);
47360 }
47361 nByte += 3;
47362 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
47363 if( zTemp==0 ){
47364 sqlite3_free(zConverted);
47365 return SQLITE_IOERR_NOMEM_BKPT;
47366 }
47367 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
47368 if( nByte==0 ){
47369 sqlite3_free(zConverted);
47370 sqlite3_free(zTemp);
47371 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47372 "winFullPathname2", zRelative);
47373 }
47374 sqlite3_free(zConverted);
47375 zOut = winUnicodeToUtf8(zTemp);
47376 sqlite3_free(zTemp);
47377 }
47378#ifdef SQLITE_WIN32_HAS_ANSI
47379 else{
47380 char *zTemp;
47381 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
47382 if( nByte==0 ){
47383 sqlite3_free(zConverted);
47384 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47385 "winFullPathname3", zRelative);
47386 }
47387 nByte += 3;
47388 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
47389 if( zTemp==0 ){
47390 sqlite3_free(zConverted);
47391 return SQLITE_IOERR_NOMEM_BKPT;
47392 }
47393 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
47394 if( nByte==0 ){
47395 sqlite3_free(zConverted);
47396 sqlite3_free(zTemp);
47397 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
47398 "winFullPathname4", zRelative);
47399 }
47400 sqlite3_free(zConverted);
47401 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
47402 sqlite3_free(zTemp);
47403 }
47404#endif
47405 if( zOut ){
47406 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
47407 sqlite3_free(zOut);
47408 return SQLITE_OK;
47409 }else{
47410 return SQLITE_IOERR_NOMEM_BKPT;
47411 }
47412#endif
47413}
47414
47415#ifndef SQLITE_OMIT_LOAD_EXTENSION
47416/*
47417** Interfaces for opening a shared library, finding entry points
47418** within the shared library, and closing the shared library.
47419*/
47420static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
47421 HANDLE h;
47422#if defined(__CYGWIN__)
47423 int nFull = pVfs->mxPathname+1;
47424 char *zFull = sqlite3MallocZero( nFull );
47425 void *zConverted = 0;
47426 if( zFull==0 ){
47427 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47428 return 0;
47429 }
47430 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
47431 sqlite3_free(zFull);
47432 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47433 return 0;
47434 }
47435 zConverted = winConvertFromUtf8Filename(zFull);
47436 sqlite3_free(zFull);
47437#else
47438 void *zConverted = winConvertFromUtf8Filename(zFilename);
47439 UNUSED_PARAMETER(pVfs);
47440#endif
47441 if( zConverted==0 ){
47442 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
47443 return 0;
47444 }
47445 if( osIsNT() ){
47446#if SQLITE_OS_WINRT
47447 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
47448#else
47449 h = osLoadLibraryW((LPCWSTR)zConverted);
47450#endif
47451 }
47452#ifdef SQLITE_WIN32_HAS_ANSI
47453 else{
47454 h = osLoadLibraryA((char*)zConverted);
47455 }
47456#endif
47457 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
47458 sqlite3_free(zConverted);
47459 return (void*)h;
47460}
47461static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
47462 UNUSED_PARAMETER(pVfs);
47463 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
47464}
47465static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
47466 FARPROC proc;
47467 UNUSED_PARAMETER(pVfs);
47468 proc = osGetProcAddressA((HANDLE)pH, zSym);
47469 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
47470 (void*)pH, zSym, (void*)proc));
47471 return (void(*)(void))proc;
47472}
47473static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
47474 UNUSED_PARAMETER(pVfs);
47475 osFreeLibrary((HANDLE)pHandle);
47476 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
47477}
47478#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
47479 #define winDlOpen 0
47480 #define winDlError 0
47481 #define winDlSym 0
47482 #define winDlClose 0
47483#endif
47484
47485/* State information for the randomness gatherer. */
47486typedef struct EntropyGatherer EntropyGatherer;
47487struct EntropyGatherer {
47488 unsigned char *a; /* Gather entropy into this buffer */
47489 int na; /* Size of a[] in bytes */
47490 int i; /* XOR next input into a[i] */
47491 int nXor; /* Number of XOR operations done */
47492};
47493
47494#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
47495/* Mix sz bytes of entropy into p. */
47496static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
47497 int j, k;
47498 for(j=0, k=p->i; j<sz; j++){
47499 p->a[k++] ^= x[j];
47500 if( k>=p->na ) k = 0;
47501 }
47502 p->i = k;
47503 p->nXor += sz;
47504}
47505#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
47506
47507/*
47508** Write up to nBuf bytes of randomness into zBuf.
47509*/
47510static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47511#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
47512 UNUSED_PARAMETER(pVfs);
47513 memset(zBuf, 0, nBuf);
47514 return nBuf;
47515#else
47516 EntropyGatherer e;
47517 UNUSED_PARAMETER(pVfs);
47518 memset(zBuf, 0, nBuf);
47519 e.a = (unsigned char*)zBuf;
47520 e.na = nBuf;
47521 e.nXor = 0;
47522 e.i = 0;
47523 {
47524 SYSTEMTIME x;
47525 osGetSystemTime(&x);
47526 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
47527 }
47528 {
47529 DWORD pid = osGetCurrentProcessId();
47530 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
47531 }
47532#if SQLITE_OS_WINRT
47533 {
47534 ULONGLONG cnt = osGetTickCount64();
47535 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
47536 }
47537#else
47538 {
47539 DWORD cnt = osGetTickCount();
47540 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
47541 }
47542#endif /* SQLITE_OS_WINRT */
47543 {
47544 LARGE_INTEGER i;
47545 osQueryPerformanceCounter(&i);
47546 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
47547 }
47548#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
47549 {
47550 UUID id;
47551 memset(&id, 0, sizeof(UUID));
47552 osUuidCreate(&id);
47553 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
47554 memset(&id, 0, sizeof(UUID));
47555 osUuidCreateSequential(&id);
47556 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
47557 }
47558#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
47559 return e.nXor>nBuf ? nBuf : e.nXor;
47560#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
47561}
47562
47563
47564/*
47565** Sleep for a little while. Return the amount of time slept.
47566*/
47567static int winSleep(sqlite3_vfs *pVfs, int microsec){
47568 sqlite3_win32_sleep((microsec+999)/1000);
47569 UNUSED_PARAMETER(pVfs);
47570 return ((microsec+999)/1000)*1000;
47571}
47572
47573/*
47574** The following variable, if set to a non-zero value, is interpreted as
47575** the number of seconds since 1970 and is used to set the result of
47576** sqlite3OsCurrentTime() during testing.
47577*/
47578#ifdef SQLITE_TEST
47579SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
47580#endif
47581
47582/*
47583** Find the current time (in Universal Coordinated Time). Write into *piNow
47584** the current time and date as a Julian Day number times 86_400_000. In
47585** other words, write into *piNow the number of milliseconds since the Julian
47586** epoch of noon in Greenwich on November 24, 4714 B.C according to the
47587** proleptic Gregorian calendar.
47588**
47589** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
47590** cannot be found.
47591*/
47592static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
47593 /* FILETIME structure is a 64-bit value representing the number of
47594 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
47595 */
47596 FILETIME ft;
47597 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
47598#ifdef SQLITE_TEST
47599 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
47600#endif
47601 /* 2^32 - to avoid use of LL and warnings in gcc */
47602 static const sqlite3_int64 max32BitValue =
47603 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
47604 (sqlite3_int64)294967296;
47605
47606#if SQLITE_OS_WINCE
47607 SYSTEMTIME time;
47608 osGetSystemTime(&time);
47609 /* if SystemTimeToFileTime() fails, it returns zero. */
47610 if (!osSystemTimeToFileTime(&time,&ft)){
47611 return SQLITE_ERROR;
47612 }
47613#else
47614 osGetSystemTimeAsFileTime( &ft );
47615#endif
47616
47617 *piNow = winFiletimeEpoch +
47618 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
47619 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
47620
47621#ifdef SQLITE_TEST
47622 if( sqlite3_current_time ){
47623 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
47624 }
47625#endif
47626 UNUSED_PARAMETER(pVfs);
47627 return SQLITE_OK;
47628}
47629
47630/*
47631** Find the current time (in Universal Coordinated Time). Write the
47632** current time and date as a Julian Day number into *prNow and
47633** return 0. Return 1 if the time and date cannot be found.
47634*/
47635static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
47636 int rc;
47637 sqlite3_int64 i;
47638 rc = winCurrentTimeInt64(pVfs, &i);
47639 if( !rc ){
47640 *prNow = i/86400000.0;
47641 }
47642 return rc;
47643}
47644
47645/*
47646** The idea is that this function works like a combination of
47647** GetLastError() and FormatMessage() on Windows (or errno and
47648** strerror_r() on Unix). After an error is returned by an OS
47649** function, SQLite calls this function with zBuf pointing to
47650** a buffer of nBuf bytes. The OS layer should populate the
47651** buffer with a nul-terminated UTF-8 encoded error message
47652** describing the last IO error to have occurred within the calling
47653** thread.
47654**
47655** If the error message is too large for the supplied buffer,
47656** it should be truncated. The return value of xGetLastError
47657** is zero if the error message fits in the buffer, or non-zero
47658** otherwise (if the message was truncated). If non-zero is returned,
47659** then it is not necessary to include the nul-terminator character
47660** in the output buffer.
47661**
47662** Not supplying an error message will have no adverse effect
47663** on SQLite. It is fine to have an implementation that never
47664** returns an error message:
47665**
47666** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47667** assert(zBuf[0]=='\0');
47668** return 0;
47669** }
47670**
47671** However if an error message is supplied, it will be incorporated
47672** by sqlite into the error message available to the user using
47673** sqlite3_errmsg(), possibly making IO errors easier to debug.
47674*/
47675static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
47676 DWORD e = osGetLastError();
47677 UNUSED_PARAMETER(pVfs);
47678 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
47679 return e;
47680}
47681
47682/*
47683** Initialize and deinitialize the operating system interface.
47684*/
47685SQLITE_API int sqlite3_os_init(void){
47686 static sqlite3_vfs winVfs = {
47687 3, /* iVersion */
47688 sizeof(winFile), /* szOsFile */
47689 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
47690 0, /* pNext */
47691 "win32", /* zName */
47692 &winAppData, /* pAppData */
47693 winOpen, /* xOpen */
47694 winDelete, /* xDelete */
47695 winAccess, /* xAccess */
47696 winFullPathname, /* xFullPathname */
47697 winDlOpen, /* xDlOpen */
47698 winDlError, /* xDlError */
47699 winDlSym, /* xDlSym */
47700 winDlClose, /* xDlClose */
47701 winRandomness, /* xRandomness */
47702 winSleep, /* xSleep */
47703 winCurrentTime, /* xCurrentTime */
47704 winGetLastError, /* xGetLastError */
47705 winCurrentTimeInt64, /* xCurrentTimeInt64 */
47706 winSetSystemCall, /* xSetSystemCall */
47707 winGetSystemCall, /* xGetSystemCall */
47708 winNextSystemCall, /* xNextSystemCall */
47709 };
47710#if defined(SQLITE_WIN32_HAS_WIDE)
47711 static sqlite3_vfs winLongPathVfs = {
47712 3, /* iVersion */
47713 sizeof(winFile), /* szOsFile */
47714 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
47715 0, /* pNext */
47716 "win32-longpath", /* zName */
47717 &winAppData, /* pAppData */
47718 winOpen, /* xOpen */
47719 winDelete, /* xDelete */
47720 winAccess, /* xAccess */
47721 winFullPathname, /* xFullPathname */
47722 winDlOpen, /* xDlOpen */
47723 winDlError, /* xDlError */
47724 winDlSym, /* xDlSym */
47725 winDlClose, /* xDlClose */
47726 winRandomness, /* xRandomness */
47727 winSleep, /* xSleep */
47728 winCurrentTime, /* xCurrentTime */
47729 winGetLastError, /* xGetLastError */
47730 winCurrentTimeInt64, /* xCurrentTimeInt64 */
47731 winSetSystemCall, /* xSetSystemCall */
47732 winGetSystemCall, /* xGetSystemCall */
47733 winNextSystemCall, /* xNextSystemCall */
47734 };
47735#endif
47736 static sqlite3_vfs winNolockVfs = {
47737 3, /* iVersion */
47738 sizeof(winFile), /* szOsFile */
47739 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
47740 0, /* pNext */
47741 "win32-none", /* zName */
47742 &winNolockAppData, /* pAppData */
47743 winOpen, /* xOpen */
47744 winDelete, /* xDelete */
47745 winAccess, /* xAccess */
47746 winFullPathname, /* xFullPathname */
47747 winDlOpen, /* xDlOpen */
47748 winDlError, /* xDlError */
47749 winDlSym, /* xDlSym */
47750 winDlClose, /* xDlClose */
47751 winRandomness, /* xRandomness */
47752 winSleep, /* xSleep */
47753 winCurrentTime, /* xCurrentTime */
47754 winGetLastError, /* xGetLastError */
47755 winCurrentTimeInt64, /* xCurrentTimeInt64 */
47756 winSetSystemCall, /* xSetSystemCall */
47757 winGetSystemCall, /* xGetSystemCall */
47758 winNextSystemCall, /* xNextSystemCall */
47759 };
47760#if defined(SQLITE_WIN32_HAS_WIDE)
47761 static sqlite3_vfs winLongPathNolockVfs = {
47762 3, /* iVersion */
47763 sizeof(winFile), /* szOsFile */
47764 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
47765 0, /* pNext */
47766 "win32-longpath-none", /* zName */
47767 &winNolockAppData, /* pAppData */
47768 winOpen, /* xOpen */
47769 winDelete, /* xDelete */
47770 winAccess, /* xAccess */
47771 winFullPathname, /* xFullPathname */
47772 winDlOpen, /* xDlOpen */
47773 winDlError, /* xDlError */
47774 winDlSym, /* xDlSym */
47775 winDlClose, /* xDlClose */
47776 winRandomness, /* xRandomness */
47777 winSleep, /* xSleep */
47778 winCurrentTime, /* xCurrentTime */
47779 winGetLastError, /* xGetLastError */
47780 winCurrentTimeInt64, /* xCurrentTimeInt64 */
47781 winSetSystemCall, /* xSetSystemCall */
47782 winGetSystemCall, /* xGetSystemCall */
47783 winNextSystemCall, /* xNextSystemCall */
47784 };
47785#endif
47786
47787 /* Double-check that the aSyscall[] array has been constructed
47788 ** correctly. See ticket [bb3a86e890c8e96ab] */
47789 assert( ArraySize(aSyscall)==80 );
47790
47791 /* get memory map allocation granularity */
47792 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
47793#if SQLITE_OS_WINRT
47794 osGetNativeSystemInfo(&winSysInfo);
47795#else
47796 osGetSystemInfo(&winSysInfo);
47797#endif
47798 assert( winSysInfo.dwAllocationGranularity>0 );
47799 assert( winSysInfo.dwPageSize>0 );
47800
47801 sqlite3_vfs_register(&winVfs, 1);
47802
47803#if defined(SQLITE_WIN32_HAS_WIDE)
47804 sqlite3_vfs_register(&winLongPathVfs, 0);
47805#endif
47806
47807 sqlite3_vfs_register(&winNolockVfs, 0);
47808
47809#if defined(SQLITE_WIN32_HAS_WIDE)
47810 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
47811#endif
47812
47813#ifndef SQLITE_OMIT_WAL
47814 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
47815#endif
47816
47817 return SQLITE_OK;
47818}
47819
47820SQLITE_API int sqlite3_os_end(void){
47821#if SQLITE_OS_WINRT
47822 if( sleepObj!=NULL ){
47823 osCloseHandle(sleepObj);
47824 sleepObj = NULL;
47825 }
47826#endif
47827
47828#ifndef SQLITE_OMIT_WAL
47829 winBigLock = 0;
47830#endif
47831
47832 return SQLITE_OK;
47833}
47834
47835#endif /* SQLITE_OS_WIN */
47836
47837/************** End of os_win.c **********************************************/
47838/************** Begin file memdb.c *******************************************/
47839/*
47840** 2016-09-07
47841**
47842** The author disclaims copyright to this source code. In place of
47843** a legal notice, here is a blessing:
47844**
47845** May you do good and not evil.
47846** May you find forgiveness for yourself and forgive others.
47847** May you share freely, never taking more than you give.
47848**
47849******************************************************************************
47850**
47851** This file implements an in-memory VFS. A database is held as a contiguous
47852** block of memory.
47853**
47854** This file also implements interface sqlite3_serialize() and
47855** sqlite3_deserialize().
47856*/
47857/* #include "sqliteInt.h" */
47858#ifdef SQLITE_ENABLE_DESERIALIZE
47859
47860/*
47861** Forward declaration of objects used by this utility
47862*/
47863typedef struct sqlite3_vfs MemVfs;
47864typedef struct MemFile MemFile;
47865
47866/* Access to a lower-level VFS that (might) implement dynamic loading,
47867** access to randomness, etc.
47868*/
47869#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
47870
47871/* An open file */
47872struct MemFile {
47873 sqlite3_file base; /* IO methods */
47874 sqlite3_int64 sz; /* Size of the file */
47875 sqlite3_int64 szAlloc; /* Space allocated to aData */
47876 sqlite3_int64 szMax; /* Maximum allowed size of the file */
47877 unsigned char *aData; /* content of the file */
47878 int nMmap; /* Number of memory mapped pages */
47879 unsigned mFlags; /* Flags */
47880 int eLock; /* Most recent lock against this file */
47881};
47882
47883/*
47884** Methods for MemFile
47885*/
47886static int memdbClose(sqlite3_file*);
47887static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
47888static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
47889static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
47890static int memdbSync(sqlite3_file*, int flags);
47891static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
47892static int memdbLock(sqlite3_file*, int);
47893/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
47894static int memdbFileControl(sqlite3_file*, int op, void *pArg);
47895/* static int memdbSectorSize(sqlite3_file*); // not used */
47896static int memdbDeviceCharacteristics(sqlite3_file*);
47897static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
47898static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
47899
47900/*
47901** Methods for MemVfs
47902*/
47903static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
47904/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
47905static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
47906static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
47907static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
47908static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
47909static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
47910static void memdbDlClose(sqlite3_vfs*, void*);
47911static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
47912static int memdbSleep(sqlite3_vfs*, int microseconds);
47913/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
47914static int memdbGetLastError(sqlite3_vfs*, int, char *);
47915static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
47916
47917static sqlite3_vfs memdb_vfs = {
47918 2, /* iVersion */
47919 0, /* szOsFile (set when registered) */
47920 1024, /* mxPathname */
47921 0, /* pNext */
47922 "memdb", /* zName */
47923 0, /* pAppData (set when registered) */
47924 memdbOpen, /* xOpen */
47925 0, /* memdbDelete, */ /* xDelete */
47926 memdbAccess, /* xAccess */
47927 memdbFullPathname, /* xFullPathname */
47928 memdbDlOpen, /* xDlOpen */
47929 memdbDlError, /* xDlError */
47930 memdbDlSym, /* xDlSym */
47931 memdbDlClose, /* xDlClose */
47932 memdbRandomness, /* xRandomness */
47933 memdbSleep, /* xSleep */
47934 0, /* memdbCurrentTime, */ /* xCurrentTime */
47935 memdbGetLastError, /* xGetLastError */
47936 memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
47937};
47938
47939static const sqlite3_io_methods memdb_io_methods = {
47940 3, /* iVersion */
47941 memdbClose, /* xClose */
47942 memdbRead, /* xRead */
47943 memdbWrite, /* xWrite */
47944 memdbTruncate, /* xTruncate */
47945 memdbSync, /* xSync */
47946 memdbFileSize, /* xFileSize */
47947 memdbLock, /* xLock */
47948 memdbLock, /* xUnlock - same as xLock in this case */
47949 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
47950 memdbFileControl, /* xFileControl */
47951 0, /* memdbSectorSize,*/ /* xSectorSize */
47952 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
47953 0, /* xShmMap */
47954 0, /* xShmLock */
47955 0, /* xShmBarrier */
47956 0, /* xShmUnmap */
47957 memdbFetch, /* xFetch */
47958 memdbUnfetch /* xUnfetch */
47959};
47960
47961
47962
47963/*
47964** Close an memdb-file.
47965**
47966** The pData pointer is owned by the application, so there is nothing
47967** to free.
47968*/
47969static int memdbClose(sqlite3_file *pFile){
47970 MemFile *p = (MemFile *)pFile;
47971 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
47972 return SQLITE_OK;
47973}
47974
47975/*
47976** Read data from an memdb-file.
47977*/
47978static int memdbRead(
47979 sqlite3_file *pFile,
47980 void *zBuf,
47981 int iAmt,
47982 sqlite_int64 iOfst
47983){
47984 MemFile *p = (MemFile *)pFile;
47985 if( iOfst+iAmt>p->sz ){
47986 memset(zBuf, 0, iAmt);
47987 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
47988 return SQLITE_IOERR_SHORT_READ;
47989 }
47990 memcpy(zBuf, p->aData+iOfst, iAmt);
47991 return SQLITE_OK;
47992}
47993
47994/*
47995** Try to enlarge the memory allocation to hold at least sz bytes
47996*/
47997static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
47998 unsigned char *pNew;
47999 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
48000 return SQLITE_FULL;
48001 }
48002 if( newSz>p->szMax ){
48003 return SQLITE_FULL;
48004 }
48005 newSz *= 2;
48006 if( newSz>p->szMax ) newSz = p->szMax;
48007 pNew = sqlite3Realloc(p->aData, newSz);
48008 if( pNew==0 ) return SQLITE_NOMEM;
48009 p->aData = pNew;
48010 p->szAlloc = newSz;
48011 return SQLITE_OK;
48012}
48013
48014/*
48015** Write data to an memdb-file.
48016*/
48017static int memdbWrite(
48018 sqlite3_file *pFile,
48019 const void *z,
48020 int iAmt,
48021 sqlite_int64 iOfst
48022){
48023 MemFile *p = (MemFile *)pFile;
48024 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY;
48025 if( iOfst+iAmt>p->sz ){
48026 int rc;
48027 if( iOfst+iAmt>p->szAlloc
48028 && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK
48029 ){
48030 return rc;
48031 }
48032 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
48033 p->sz = iOfst+iAmt;
48034 }
48035 memcpy(p->aData+iOfst, z, iAmt);
48036 return SQLITE_OK;
48037}
48038
48039/*
48040** Truncate an memdb-file.
48041**
48042** In rollback mode (which is always the case for memdb, as it does not
48043** support WAL mode) the truncate() method is only used to reduce
48044** the size of a file, never to increase the size.
48045*/
48046static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
48047 MemFile *p = (MemFile *)pFile;
48048 if( NEVER(size>p->sz) ) return SQLITE_FULL;
48049 p->sz = size;
48050 return SQLITE_OK;
48051}
48052
48053/*
48054** Sync an memdb-file.
48055*/
48056static int memdbSync(sqlite3_file *pFile, int flags){
48057 return SQLITE_OK;
48058}
48059
48060/*
48061** Return the current file-size of an memdb-file.
48062*/
48063static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
48064 MemFile *p = (MemFile *)pFile;
48065 *pSize = p->sz;
48066 return SQLITE_OK;
48067}
48068
48069/*
48070** Lock an memdb-file.
48071*/
48072static int memdbLock(sqlite3_file *pFile, int eLock){
48073 MemFile *p = (MemFile *)pFile;
48074 if( eLock>SQLITE_LOCK_SHARED
48075 && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0
48076 ){
48077 return SQLITE_READONLY;
48078 }
48079 p->eLock = eLock;
48080 return SQLITE_OK;
48081}
48082
48083#if 0 /* Never used because memdbAccess() always returns false */
48084/*
48085** Check if another file-handle holds a RESERVED lock on an memdb-file.
48086*/
48087static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
48088 *pResOut = 0;
48089 return SQLITE_OK;
48090}
48091#endif
48092
48093/*
48094** File control method. For custom operations on an memdb-file.
48095*/
48096static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
48097 MemFile *p = (MemFile *)pFile;
48098 int rc = SQLITE_NOTFOUND;
48099 if( op==SQLITE_FCNTL_VFSNAME ){
48100 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
48101 rc = SQLITE_OK;
48102 }
48103 if( op==SQLITE_FCNTL_SIZE_LIMIT ){
48104 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
48105 if( iLimit<p->sz ){
48106 if( iLimit<0 ){
48107 iLimit = p->szMax;
48108 }else{
48109 iLimit = p->sz;
48110 }
48111 }
48112 p->szMax = iLimit;
48113 *(sqlite3_int64*)pArg = iLimit;
48114 rc = SQLITE_OK;
48115 }
48116 return rc;
48117}
48118
48119#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
48120/*
48121** Return the sector-size in bytes for an memdb-file.
48122*/
48123static int memdbSectorSize(sqlite3_file *pFile){
48124 return 1024;
48125}
48126#endif
48127
48128/*
48129** Return the device characteristic flags supported by an memdb-file.
48130*/
48131static int memdbDeviceCharacteristics(sqlite3_file *pFile){
48132 return SQLITE_IOCAP_ATOMIC |
48133 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
48134 SQLITE_IOCAP_SAFE_APPEND |
48135 SQLITE_IOCAP_SEQUENTIAL;
48136}
48137
48138/* Fetch a page of a memory-mapped file */
48139static int memdbFetch(
48140 sqlite3_file *pFile,
48141 sqlite3_int64 iOfst,
48142 int iAmt,
48143 void **pp
48144){
48145 MemFile *p = (MemFile *)pFile;
48146 if( iOfst+iAmt>p->sz ){
48147 *pp = 0;
48148 }else{
48149 p->nMmap++;
48150 *pp = (void*)(p->aData + iOfst);
48151 }
48152 return SQLITE_OK;
48153}
48154
48155/* Release a memory-mapped page */
48156static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
48157 MemFile *p = (MemFile *)pFile;
48158 p->nMmap--;
48159 return SQLITE_OK;
48160}
48161
48162/*
48163** Open an mem file handle.
48164*/
48165static int memdbOpen(
48166 sqlite3_vfs *pVfs,
48167 const char *zName,
48168 sqlite3_file *pFile,
48169 int flags,
48170 int *pOutFlags
48171){
48172 MemFile *p = (MemFile*)pFile;
48173 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
48174 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
48175 }
48176 memset(p, 0, sizeof(*p));
48177 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
48178 assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
48179 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
48180 pFile->pMethods = &memdb_io_methods;
48181 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48182 return SQLITE_OK;
48183}
48184
48185#if 0 /* Only used to delete rollback journals, super-journals, and WAL
48186 ** files, none of which exist in memdb. So this routine is never used */
48187/*
48188** Delete the file located at zPath. If the dirSync argument is true,
48189** ensure the file-system modifications are synced to disk before
48190** returning.
48191*/
48192static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
48193 return SQLITE_IOERR_DELETE;
48194}
48195#endif
48196
48197/*
48198** Test for access permissions. Return true if the requested permission
48199** is available, or false otherwise.
48200**
48201** With memdb, no files ever exist on disk. So always return false.
48202*/
48203static int memdbAccess(
48204 sqlite3_vfs *pVfs,
48205 const char *zPath,
48206 int flags,
48207 int *pResOut
48208){
48209 *pResOut = 0;
48210 return SQLITE_OK;
48211}
48212
48213/*
48214** Populate buffer zOut with the full canonical pathname corresponding
48215** to the pathname in zPath. zOut is guaranteed to point to a buffer
48216** of at least (INST_MAX_PATHNAME+1) bytes.
48217*/
48218static int memdbFullPathname(
48219 sqlite3_vfs *pVfs,
48220 const char *zPath,
48221 int nOut,
48222 char *zOut
48223){
48224 sqlite3_snprintf(nOut, zOut, "%s", zPath);
48225 return SQLITE_OK;
48226}
48227
48228/*
48229** Open the dynamic library located at zPath and return a handle.
48230*/
48231static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
48232 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
48233}
48234
48235/*
48236** Populate the buffer zErrMsg (size nByte bytes) with a human readable
48237** utf-8 string describing the most recent error encountered associated
48238** with dynamic libraries.
48239*/
48240static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
48241 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
48242}
48243
48244/*
48245** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
48246*/
48247static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
48248 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
48249}
48250
48251/*
48252** Close the dynamic library handle pHandle.
48253*/
48254static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
48255 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
48256}
48257
48258/*
48259** Populate the buffer pointed to by zBufOut with nByte bytes of
48260** random data.
48261*/
48262static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
48263 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
48264}
48265
48266/*
48267** Sleep for nMicro microseconds. Return the number of microseconds
48268** actually slept.
48269*/
48270static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
48271 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
48272}
48273
48274#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
48275/*
48276** Return the current time as a Julian Day number in *pTimeOut.
48277*/
48278static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
48279 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
48280}
48281#endif
48282
48283static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
48284 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
48285}
48286static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
48287 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
48288}
48289
48290/*
48291** Translate a database connection pointer and schema name into a
48292** MemFile pointer.
48293*/
48294static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
48295 MemFile *p = 0;
48296 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
48297 if( rc ) return 0;
48298 if( p->base.pMethods!=&memdb_io_methods ) return 0;
48299 return p;
48300}
48301
48302/*
48303** Return the serialization of a database
48304*/
48305SQLITE_API unsigned char *sqlite3_serialize(
48306 sqlite3 *db, /* The database connection */
48307 const char *zSchema, /* Which database within the connection */
48308 sqlite3_int64 *piSize, /* Write size here, if not NULL */
48309 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
48310){
48311 MemFile *p;
48312 int iDb;
48313 Btree *pBt;
48314 sqlite3_int64 sz;
48315 int szPage = 0;
48316 sqlite3_stmt *pStmt = 0;
48317 unsigned char *pOut;
48318 char *zSql;
48319 int rc;
48320
48321#ifdef SQLITE_ENABLE_API_ARMOR
48322 if( !sqlite3SafetyCheckOk(db) ){
48323 (void)SQLITE_MISUSE_BKPT;
48324 return 0;
48325 }
48326#endif
48327
48328 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48329 p = memdbFromDbSchema(db, zSchema);
48330 iDb = sqlite3FindDbName(db, zSchema);
48331 if( piSize ) *piSize = -1;
48332 if( iDb<0 ) return 0;
48333 if( p ){
48334 if( piSize ) *piSize = p->sz;
48335 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
48336 pOut = p->aData;
48337 }else{
48338 pOut = sqlite3_malloc64( p->sz );
48339 if( pOut ) memcpy(pOut, p->aData, p->sz);
48340 }
48341 return pOut;
48342 }
48343 pBt = db->aDb[iDb].pBt;
48344 if( pBt==0 ) return 0;
48345 szPage = sqlite3BtreeGetPageSize(pBt);
48346 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
48347 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
48348 sqlite3_free(zSql);
48349 if( rc ) return 0;
48350 rc = sqlite3_step(pStmt);
48351 if( rc!=SQLITE_ROW ){
48352 pOut = 0;
48353 }else{
48354 sz = sqlite3_column_int64(pStmt, 0)*szPage;
48355 if( piSize ) *piSize = sz;
48356 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
48357 pOut = 0;
48358 }else{
48359 pOut = sqlite3_malloc64( sz );
48360 if( pOut ){
48361 int nPage = sqlite3_column_int(pStmt, 0);
48362 Pager *pPager = sqlite3BtreePager(pBt);
48363 int pgno;
48364 for(pgno=1; pgno<=nPage; pgno++){
48365 DbPage *pPage = 0;
48366 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
48367 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
48368 if( rc==SQLITE_OK ){
48369 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
48370 }else{
48371 memset(pTo, 0, szPage);
48372 }
48373 sqlite3PagerUnref(pPage);
48374 }
48375 }
48376 }
48377 }
48378 sqlite3_finalize(pStmt);
48379 return pOut;
48380}
48381
48382/* Convert zSchema to a MemDB and initialize its content.
48383*/
48384SQLITE_API int sqlite3_deserialize(
48385 sqlite3 *db, /* The database connection */
48386 const char *zSchema, /* Which DB to reopen with the deserialization */
48387 unsigned char *pData, /* The serialized database content */
48388 sqlite3_int64 szDb, /* Number bytes in the deserialization */
48389 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
48390 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
48391){
48392 MemFile *p;
48393 char *zSql;
48394 sqlite3_stmt *pStmt = 0;
48395 int rc;
48396 int iDb;
48397
48398#ifdef SQLITE_ENABLE_API_ARMOR
48399 if( !sqlite3SafetyCheckOk(db) ){
48400 return SQLITE_MISUSE_BKPT;
48401 }
48402 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
48403 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
48404#endif
48405
48406 sqlite3_mutex_enter(db->mutex);
48407 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48408 iDb = sqlite3FindDbName(db, zSchema);
48409 if( iDb<0 ){
48410 rc = SQLITE_ERROR;
48411 goto end_deserialize;
48412 }
48413 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
48414 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
48415 sqlite3_free(zSql);
48416 if( rc ) goto end_deserialize;
48417 db->init.iDb = (u8)iDb;
48418 db->init.reopenMemdb = 1;
48419 rc = sqlite3_step(pStmt);
48420 db->init.reopenMemdb = 0;
48421 if( rc!=SQLITE_DONE ){
48422 rc = SQLITE_ERROR;
48423 goto end_deserialize;
48424 }
48425 p = memdbFromDbSchema(db, zSchema);
48426 if( p==0 ){
48427 rc = SQLITE_ERROR;
48428 }else{
48429 p->aData = pData;
48430 p->sz = szDb;
48431 p->szAlloc = szBuf;
48432 p->szMax = szBuf;
48433 if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){
48434 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48435 }
48436 p->mFlags = mFlags;
48437 rc = SQLITE_OK;
48438 }
48439
48440end_deserialize:
48441 sqlite3_finalize(pStmt);
48442 sqlite3_mutex_leave(db->mutex);
48443 return rc;
48444}
48445
48446/*
48447** This routine is called when the extension is loaded.
48448** Register the new VFS.
48449*/
48450SQLITE_PRIVATE int sqlite3MemdbInit(void){
48451 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
48452 int sz = pLower->szOsFile;
48453 memdb_vfs.pAppData = pLower;
48454 /* The following conditional can only be true when compiled for
48455 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
48456 ** it in, to be safe, but it is marked as NO_TEST since there
48457 ** is no way to reach it under most builds. */
48458 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
48459 memdb_vfs.szOsFile = sz;
48460 return sqlite3_vfs_register(&memdb_vfs, 0);
48461}
48462#endif /* SQLITE_ENABLE_DESERIALIZE */
48463
48464/************** End of memdb.c ***********************************************/
48465/************** Begin file bitvec.c ******************************************/
48466/*
48467** 2008 February 16
48468**
48469** The author disclaims copyright to this source code. In place of
48470** a legal notice, here is a blessing:
48471**
48472** May you do good and not evil.
48473** May you find forgiveness for yourself and forgive others.
48474** May you share freely, never taking more than you give.
48475**
48476*************************************************************************
48477** This file implements an object that represents a fixed-length
48478** bitmap. Bits are numbered starting with 1.
48479**
48480** A bitmap is used to record which pages of a database file have been
48481** journalled during a transaction, or which pages have the "dont-write"
48482** property. Usually only a few pages are meet either condition.
48483** So the bitmap is usually sparse and has low cardinality.
48484** But sometimes (for example when during a DROP of a large table) most
48485** or all of the pages in a database can get journalled. In those cases,
48486** the bitmap becomes dense with high cardinality. The algorithm needs
48487** to handle both cases well.
48488**
48489** The size of the bitmap is fixed when the object is created.
48490**
48491** All bits are clear when the bitmap is created. Individual bits
48492** may be set or cleared one at a time.
48493**
48494** Test operations are about 100 times more common that set operations.
48495** Clear operations are exceedingly rare. There are usually between
48496** 5 and 500 set operations per Bitvec object, though the number of sets can
48497** sometimes grow into tens of thousands or larger. The size of the
48498** Bitvec object is the number of pages in the database file at the
48499** start of a transaction, and is thus usually less than a few thousand,
48500** but can be as large as 2 billion for a really big database.
48501*/
48502/* #include "sqliteInt.h" */
48503
48504/* Size of the Bitvec structure in bytes. */
48505#define BITVEC_SZ 512
48506
48507/* Round the union size down to the nearest pointer boundary, since that's how
48508** it will be aligned within the Bitvec struct. */
48509#define BITVEC_USIZE \
48510 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
48511
48512/* Type of the array "element" for the bitmap representation.
48513** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
48514** Setting this to the "natural word" size of your CPU may improve
48515** performance. */
48516#define BITVEC_TELEM u8
48517/* Size, in bits, of the bitmap element. */
48518#define BITVEC_SZELEM 8
48519/* Number of elements in a bitmap array. */
48520#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
48521/* Number of bits in the bitmap array. */
48522#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
48523
48524/* Number of u32 values in hash table. */
48525#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
48526/* Maximum number of entries in hash table before
48527** sub-dividing and re-hashing. */
48528#define BITVEC_MXHASH (BITVEC_NINT/2)
48529/* Hashing function for the aHash representation.
48530** Empirical testing showed that the *37 multiplier
48531** (an arbitrary prime)in the hash function provided
48532** no fewer collisions than the no-op *1. */
48533#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
48534
48535#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
48536
48537
48538/*
48539** A bitmap is an instance of the following structure.
48540**
48541** This bitmap records the existence of zero or more bits
48542** with values between 1 and iSize, inclusive.
48543**
48544** There are three possible representations of the bitmap.
48545** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
48546** bitmap. The least significant bit is bit 1.
48547**
48548** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
48549** a hash table that will hold up to BITVEC_MXHASH distinct values.
48550**
48551** Otherwise, the value i is redirected into one of BITVEC_NPTR
48552** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
48553** handles up to iDivisor separate values of i. apSub[0] holds
48554** values between 1 and iDivisor. apSub[1] holds values between
48555** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
48556** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
48557** to hold deal with values between 1 and iDivisor.
48558*/
48559struct Bitvec {
48560 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
48561 u32 nSet; /* Number of bits that are set - only valid for aHash
48562 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
48563 ** this would be 125. */
48564 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
48565 /* Should >=0 for apSub element. */
48566 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
48567 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
48568 union {
48569 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
48570 u32 aHash[BITVEC_NINT]; /* Hash table representation */
48571 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
48572 } u;
48573};
48574
48575/*
48576** Create a new bitmap object able to handle bits between 0 and iSize,
48577** inclusive. Return a pointer to the new object. Return NULL if
48578** malloc fails.
48579*/
48580SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
48581 Bitvec *p;
48582 assert( sizeof(*p)==BITVEC_SZ );
48583 p = sqlite3MallocZero( sizeof(*p) );
48584 if( p ){
48585 p->iSize = iSize;
48586 }
48587 return p;
48588}
48589
48590/*
48591** Check to see if the i-th bit is set. Return true or false.
48592** If p is NULL (if the bitmap has not been created) or if
48593** i is out of range, then return false.
48594*/
48595SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
48596 assert( p!=0 );
48597 i--;
48598 if( i>=p->iSize ) return 0;
48599 while( p->iDivisor ){
48600 u32 bin = i/p->iDivisor;
48601 i = i%p->iDivisor;
48602 p = p->u.apSub[bin];
48603 if (!p) {
48604 return 0;
48605 }
48606 }
48607 if( p->iSize<=BITVEC_NBIT ){
48608 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
48609 } else{
48610 u32 h = BITVEC_HASH(i++);
48611 while( p->u.aHash[h] ){
48612 if( p->u.aHash[h]==i ) return 1;
48613 h = (h+1) % BITVEC_NINT;
48614 }
48615 return 0;
48616 }
48617}
48618SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
48619 return p!=0 && sqlite3BitvecTestNotNull(p,i);
48620}
48621
48622/*
48623** Set the i-th bit. Return 0 on success and an error code if
48624** anything goes wrong.
48625**
48626** This routine might cause sub-bitmaps to be allocated. Failing
48627** to get the memory needed to hold the sub-bitmap is the only
48628** that can go wrong with an insert, assuming p and i are valid.
48629**
48630** The calling function must ensure that p is a valid Bitvec object
48631** and that the value for "i" is within range of the Bitvec object.
48632** Otherwise the behavior is undefined.
48633*/
48634SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
48635 u32 h;
48636 if( p==0 ) return SQLITE_OK;
48637 assert( i>0 );
48638 assert( i<=p->iSize );
48639 i--;
48640 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
48641 u32 bin = i/p->iDivisor;
48642 i = i%p->iDivisor;
48643 if( p->u.apSub[bin]==0 ){
48644 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
48645 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
48646 }
48647 p = p->u.apSub[bin];
48648 }
48649 if( p->iSize<=BITVEC_NBIT ){
48650 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
48651 return SQLITE_OK;
48652 }
48653 h = BITVEC_HASH(i++);
48654 /* if there wasn't a hash collision, and this doesn't */
48655 /* completely fill the hash, then just add it without */
48656 /* worring about sub-dividing and re-hashing. */
48657 if( !p->u.aHash[h] ){
48658 if (p->nSet<(BITVEC_NINT-1)) {
48659 goto bitvec_set_end;
48660 } else {
48661 goto bitvec_set_rehash;
48662 }
48663 }
48664 /* there was a collision, check to see if it's already */
48665 /* in hash, if not, try to find a spot for it */
48666 do {
48667 if( p->u.aHash[h]==i ) return SQLITE_OK;
48668 h++;
48669 if( h>=BITVEC_NINT ) h = 0;
48670 } while( p->u.aHash[h] );
48671 /* we didn't find it in the hash. h points to the first */
48672 /* available free spot. check to see if this is going to */
48673 /* make our hash too "full". */
48674bitvec_set_rehash:
48675 if( p->nSet>=BITVEC_MXHASH ){
48676 unsigned int j;
48677 int rc;
48678 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
48679 if( aiValues==0 ){
48680 return SQLITE_NOMEM_BKPT;
48681 }else{
48682 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
48683 memset(p->u.apSub, 0, sizeof(p->u.apSub));
48684 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
48685 rc = sqlite3BitvecSet(p, i);
48686 for(j=0; j<BITVEC_NINT; j++){
48687 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
48688 }
48689 sqlite3StackFree(0, aiValues);
48690 return rc;
48691 }
48692 }
48693bitvec_set_end:
48694 p->nSet++;
48695 p->u.aHash[h] = i;
48696 return SQLITE_OK;
48697}
48698
48699/*
48700** Clear the i-th bit.
48701**
48702** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
48703** that BitvecClear can use to rebuilt its hash table.
48704*/
48705SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
48706 if( p==0 ) return;
48707 assert( i>0 );
48708 i--;
48709 while( p->iDivisor ){
48710 u32 bin = i/p->iDivisor;
48711 i = i%p->iDivisor;
48712 p = p->u.apSub[bin];
48713 if (!p) {
48714 return;
48715 }
48716 }
48717 if( p->iSize<=BITVEC_NBIT ){
48718 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
48719 }else{
48720 unsigned int j;
48721 u32 *aiValues = pBuf;
48722 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
48723 memset(p->u.aHash, 0, sizeof(p->u.aHash));
48724 p->nSet = 0;
48725 for(j=0; j<BITVEC_NINT; j++){
48726 if( aiValues[j] && aiValues[j]!=(i+1) ){
48727 u32 h = BITVEC_HASH(aiValues[j]-1);
48728 p->nSet++;
48729 while( p->u.aHash[h] ){
48730 h++;
48731 if( h>=BITVEC_NINT ) h = 0;
48732 }
48733 p->u.aHash[h] = aiValues[j];
48734 }
48735 }
48736 }
48737}
48738
48739/*
48740** Destroy a bitmap object. Reclaim all memory used.
48741*/
48742SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
48743 if( p==0 ) return;
48744 if( p->iDivisor ){
48745 unsigned int i;
48746 for(i=0; i<BITVEC_NPTR; i++){
48747 sqlite3BitvecDestroy(p->u.apSub[i]);
48748 }
48749 }
48750 sqlite3_free(p);
48751}
48752
48753/*
48754** Return the value of the iSize parameter specified when Bitvec *p
48755** was created.
48756*/
48757SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
48758 return p->iSize;
48759}
48760
48761#ifndef SQLITE_UNTESTABLE
48762/*
48763** Let V[] be an array of unsigned characters sufficient to hold
48764** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
48765** Then the following macros can be used to set, clear, or test
48766** individual bits within V.
48767*/
48768#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
48769#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
48770#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
48771
48772/*
48773** This routine runs an extensive test of the Bitvec code.
48774**
48775** The input is an array of integers that acts as a program
48776** to test the Bitvec. The integers are opcodes followed
48777** by 0, 1, or 3 operands, depending on the opcode. Another
48778** opcode follows immediately after the last operand.
48779**
48780** There are 6 opcodes numbered from 0 through 5. 0 is the
48781** "halt" opcode and causes the test to end.
48782**
48783** 0 Halt and return the number of errors
48784** 1 N S X Set N bits beginning with S and incrementing by X
48785** 2 N S X Clear N bits beginning with S and incrementing by X
48786** 3 N Set N randomly chosen bits
48787** 4 N Clear N randomly chosen bits
48788** 5 N S X Set N bits from S increment X in array only, not in bitvec
48789**
48790** The opcodes 1 through 4 perform set and clear operations are performed
48791** on both a Bitvec object and on a linear array of bits obtained from malloc.
48792** Opcode 5 works on the linear array only, not on the Bitvec.
48793** Opcode 5 is used to deliberately induce a fault in order to
48794** confirm that error detection works.
48795**
48796** At the conclusion of the test the linear array is compared
48797** against the Bitvec object. If there are any differences,
48798** an error is returned. If they are the same, zero is returned.
48799**
48800** If a memory allocation error occurs, return -1.
48801*/
48802SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
48803 Bitvec *pBitvec = 0;
48804 unsigned char *pV = 0;
48805 int rc = -1;
48806 int i, nx, pc, op;
48807 void *pTmpSpace;
48808
48809 /* Allocate the Bitvec to be tested and a linear array of
48810 ** bits to act as the reference */
48811 pBitvec = sqlite3BitvecCreate( sz );
48812 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
48813 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
48814 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
48815
48816 /* NULL pBitvec tests */
48817 sqlite3BitvecSet(0, 1);
48818 sqlite3BitvecClear(0, 1, pTmpSpace);
48819
48820 /* Run the program */
48821 pc = 0;
48822 while( (op = aOp[pc])!=0 ){
48823 switch( op ){
48824 case 1:
48825 case 2:
48826 case 5: {
48827 nx = 4;
48828 i = aOp[pc+2] - 1;
48829 aOp[pc+2] += aOp[pc+3];
48830 break;
48831 }
48832 case 3:
48833 case 4:
48834 default: {
48835 nx = 2;
48836 sqlite3_randomness(sizeof(i), &i);
48837 break;
48838 }
48839 }
48840 if( (--aOp[pc+1]) > 0 ) nx = 0;
48841 pc += nx;
48842 i = (i & 0x7fffffff)%sz;
48843 if( (op & 1)!=0 ){
48844 SETBIT(pV, (i+1));
48845 if( op!=5 ){
48846 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
48847 }
48848 }else{
48849 CLEARBIT(pV, (i+1));
48850 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
48851 }
48852 }
48853
48854 /* Test to make sure the linear array exactly matches the
48855 ** Bitvec object. Start with the assumption that they do
48856 ** match (rc==0). Change rc to non-zero if a discrepancy
48857 ** is found.
48858 */
48859 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
48860 + sqlite3BitvecTest(pBitvec, 0)
48861 + (sqlite3BitvecSize(pBitvec) - sz);
48862 for(i=1; i<=sz; i++){
48863 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
48864 rc = i;
48865 break;
48866 }
48867 }
48868
48869 /* Free allocated structure */
48870bitvec_end:
48871 sqlite3_free(pTmpSpace);
48872 sqlite3_free(pV);
48873 sqlite3BitvecDestroy(pBitvec);
48874 return rc;
48875}
48876#endif /* SQLITE_UNTESTABLE */
48877
48878/************** End of bitvec.c **********************************************/
48879/************** Begin file pcache.c ******************************************/
48880/*
48881** 2008 August 05
48882**
48883** The author disclaims copyright to this source code. In place of
48884** a legal notice, here is a blessing:
48885**
48886** May you do good and not evil.
48887** May you find forgiveness for yourself and forgive others.
48888** May you share freely, never taking more than you give.
48889**
48890*************************************************************************
48891** This file implements that page cache.
48892*/
48893/* #include "sqliteInt.h" */
48894
48895/*
48896** A complete page cache is an instance of this structure. Every
48897** entry in the cache holds a single page of the database file. The
48898** btree layer only operates on the cached copy of the database pages.
48899**
48900** A page cache entry is "clean" if it exactly matches what is currently
48901** on disk. A page is "dirty" if it has been modified and needs to be
48902** persisted to disk.
48903**
48904** pDirty, pDirtyTail, pSynced:
48905** All dirty pages are linked into the doubly linked list using
48906** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
48907** such that p was added to the list more recently than p->pDirtyNext.
48908** PCache.pDirty points to the first (newest) element in the list and
48909** pDirtyTail to the last (oldest).
48910**
48911** The PCache.pSynced variable is used to optimize searching for a dirty
48912** page to eject from the cache mid-transaction. It is better to eject
48913** a page that does not require a journal sync than one that does.
48914** Therefore, pSynced is maintained so that it *almost* always points
48915** to either the oldest page in the pDirty/pDirtyTail list that has a
48916** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
48917** (so that the right page to eject can be found by following pDirtyPrev
48918** pointers).
48919*/
48920struct PCache {
48921 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
48922 PgHdr *pSynced; /* Last synced page in dirty page list */
48923 int nRefSum; /* Sum of ref counts over all pages */
48924 int szCache; /* Configured cache size */
48925 int szSpill; /* Size before spilling occurs */
48926 int szPage; /* Size of every page in this cache */
48927 int szExtra; /* Size of extra space for each page */
48928 u8 bPurgeable; /* True if pages are on backing store */
48929 u8 eCreate; /* eCreate value for for xFetch() */
48930 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
48931 void *pStress; /* Argument to xStress */
48932 sqlite3_pcache *pCache; /* Pluggable cache module */
48933};
48934
48935/********************************** Test and Debug Logic **********************/
48936/*
48937** Debug tracing macros. Enable by by changing the "0" to "1" and
48938** recompiling.
48939**
48940** When sqlite3PcacheTrace is 1, single line trace messages are issued.
48941** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
48942** is displayed for many operations, resulting in a lot of output.
48943*/
48944#if defined(SQLITE_DEBUG) && 0
48945 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
48946 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
48947# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
48948 void pcacheDump(PCache *pCache){
48949 int N;
48950 int i, j;
48951 sqlite3_pcache_page *pLower;
48952 PgHdr *pPg;
48953 unsigned char *a;
48954
48955 if( sqlite3PcacheTrace<2 ) return;
48956 if( pCache->pCache==0 ) return;
48957 N = sqlite3PcachePagecount(pCache);
48958 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
48959 for(i=1; i<=N; i++){
48960 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
48961 if( pLower==0 ) continue;
48962 pPg = (PgHdr*)pLower->pExtra;
48963 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
48964 a = (unsigned char *)pLower->pBuf;
48965 for(j=0; j<12; j++) printf("%02x", a[j]);
48966 printf("\n");
48967 if( pPg->pPage==0 ){
48968 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
48969 }
48970 }
48971 }
48972 #else
48973# define pcacheTrace(X)
48974# define pcacheDump(X)
48975#endif
48976
48977/*
48978** Check invariants on a PgHdr entry. Return true if everything is OK.
48979** Return false if any invariant is violated.
48980**
48981** This routine is for use inside of assert() statements only. For
48982** example:
48983**
48984** assert( sqlite3PcachePageSanity(pPg) );
48985*/
48986#ifdef SQLITE_DEBUG
48987SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
48988 PCache *pCache;
48989 assert( pPg!=0 );
48990 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
48991 pCache = pPg->pCache;
48992 assert( pCache!=0 ); /* Every page has an associated PCache */
48993 if( pPg->flags & PGHDR_CLEAN ){
48994 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
48995 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
48996 assert( pCache->pDirtyTail!=pPg );
48997 }
48998 /* WRITEABLE pages must also be DIRTY */
48999 if( pPg->flags & PGHDR_WRITEABLE ){
49000 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
49001 }
49002 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
49003 ** for example, when using the sqlite3PagerDontWrite() optimization:
49004 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
49005 ** (2) Page X moved to freelist, WRITEABLE is cleared
49006 ** (3) Page X reused, WRITEABLE is set again
49007 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
49008 ** in step 3, and page might be written into the database without first
49009 ** syncing the rollback journal, which might cause corruption on a power
49010 ** loss.
49011 **
49012 ** Another example is when the database page size is smaller than the
49013 ** disk sector size. When any page of a sector is journalled, all pages
49014 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
49015 ** in case they are later modified, since all pages in the same sector
49016 ** must be journalled and synced before any of those pages can be safely
49017 ** written.
49018 */
49019 return 1;
49020}
49021#endif /* SQLITE_DEBUG */
49022
49023
49024/********************************** Linked List Management ********************/
49025
49026/* Allowed values for second argument to pcacheManageDirtyList() */
49027#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
49028#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
49029#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
49030
49031/*
49032** Manage pPage's participation on the dirty list. Bits of the addRemove
49033** argument determines what operation to do. The 0x01 bit means first
49034** remove pPage from the dirty list. The 0x02 means add pPage back to
49035** the dirty list. Doing both moves pPage to the front of the dirty list.
49036*/
49037static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
49038 PCache *p = pPage->pCache;
49039
49040 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
49041 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
49042 pPage->pgno));
49043 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
49044 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
49045 assert( pPage->pDirtyPrev || pPage==p->pDirty );
49046
49047 /* Update the PCache1.pSynced variable if necessary. */
49048 if( p->pSynced==pPage ){
49049 p->pSynced = pPage->pDirtyPrev;
49050 }
49051
49052 if( pPage->pDirtyNext ){
49053 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
49054 }else{
49055 assert( pPage==p->pDirtyTail );
49056 p->pDirtyTail = pPage->pDirtyPrev;
49057 }
49058 if( pPage->pDirtyPrev ){
49059 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
49060 }else{
49061 /* If there are now no dirty pages in the cache, set eCreate to 2.
49062 ** This is an optimization that allows sqlite3PcacheFetch() to skip
49063 ** searching for a dirty page to eject from the cache when it might
49064 ** otherwise have to. */
49065 assert( pPage==p->pDirty );
49066 p->pDirty = pPage->pDirtyNext;
49067 assert( p->bPurgeable || p->eCreate==2 );
49068 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
49069 assert( p->bPurgeable==0 || p->eCreate==1 );
49070 p->eCreate = 2;
49071 }
49072 }
49073 }
49074 if( addRemove & PCACHE_DIRTYLIST_ADD ){
49075 pPage->pDirtyPrev = 0;
49076 pPage->pDirtyNext = p->pDirty;
49077 if( pPage->pDirtyNext ){
49078 assert( pPage->pDirtyNext->pDirtyPrev==0 );
49079 pPage->pDirtyNext->pDirtyPrev = pPage;
49080 }else{
49081 p->pDirtyTail = pPage;
49082 if( p->bPurgeable ){
49083 assert( p->eCreate==2 );
49084 p->eCreate = 1;
49085 }
49086 }
49087 p->pDirty = pPage;
49088
49089 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
49090 ** pSynced to point to it. Checking the NEED_SYNC flag is an
49091 ** optimization, as if pSynced points to a page with the NEED_SYNC
49092 ** flag set sqlite3PcacheFetchStress() searches through all newer
49093 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
49094 if( !p->pSynced
49095 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
49096 ){
49097 p->pSynced = pPage;
49098 }
49099 }
49100 pcacheDump(p);
49101}
49102
49103/*
49104** Wrapper around the pluggable caches xUnpin method. If the cache is
49105** being used for an in-memory database, this function is a no-op.
49106*/
49107static void pcacheUnpin(PgHdr *p){
49108 if( p->pCache->bPurgeable ){
49109 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
49110 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
49111 pcacheDump(p->pCache);
49112 }
49113}
49114
49115/*
49116** Compute the number of pages of cache requested. p->szCache is the
49117** cache size requested by the "PRAGMA cache_size" statement.
49118*/
49119static int numberOfCachePages(PCache *p){
49120 if( p->szCache>=0 ){
49121 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
49122 ** suggested cache size is set to N. */
49123 return p->szCache;
49124 }else{
49125 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
49126 ** number of cache pages is adjusted to be a number of pages that would
49127 ** use approximately abs(N*1024) bytes of memory based on the current
49128 ** page size. */
49129 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
49130 }
49131}
49132
49133/*************************************************** General Interfaces ******
49134**
49135** Initialize and shutdown the page cache subsystem. Neither of these
49136** functions are threadsafe.
49137*/
49138SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
49139 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
49140 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
49141 ** built-in default page cache is used instead of the application defined
49142 ** page cache. */
49143 sqlite3PCacheSetDefault();
49144 assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
49145 }
49146 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
49147}
49148SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
49149 if( sqlite3GlobalConfig.pcache2.xShutdown ){
49150 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
49151 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
49152 }
49153}
49154
49155/*
49156** Return the size in bytes of a PCache object.
49157*/
49158SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
49159
49160/*
49161** Create a new PCache object. Storage space to hold the object
49162** has already been allocated and is passed in as the p pointer.
49163** The caller discovers how much space needs to be allocated by
49164** calling sqlite3PcacheSize().
49165**
49166** szExtra is some extra space allocated for each page. The first
49167** 8 bytes of the extra space will be zeroed as the page is allocated,
49168** but remaining content will be uninitialized. Though it is opaque
49169** to this module, the extra space really ends up being the MemPage
49170** structure in the pager.
49171*/
49172SQLITE_PRIVATE int sqlite3PcacheOpen(
49173 int szPage, /* Size of every page */
49174 int szExtra, /* Extra space associated with each page */
49175 int bPurgeable, /* True if pages are on backing store */
49176 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
49177 void *pStress, /* Argument to xStress */
49178 PCache *p /* Preallocated space for the PCache */
49179){
49180 memset(p, 0, sizeof(PCache));
49181 p->szPage = 1;
49182 p->szExtra = szExtra;
49183 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
49184 p->bPurgeable = bPurgeable;
49185 p->eCreate = 2;
49186 p->xStress = xStress;
49187 p->pStress = pStress;
49188 p->szCache = 100;
49189 p->szSpill = 1;
49190 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
49191 return sqlite3PcacheSetPageSize(p, szPage);
49192}
49193
49194/*
49195** Change the page size for PCache object. The caller must ensure that there
49196** are no outstanding page references when this function is called.
49197*/
49198SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
49199 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
49200 if( pCache->szPage ){
49201 sqlite3_pcache *pNew;
49202 pNew = sqlite3GlobalConfig.pcache2.xCreate(
49203 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
49204 pCache->bPurgeable
49205 );
49206 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
49207 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
49208 if( pCache->pCache ){
49209 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
49210 }
49211 pCache->pCache = pNew;
49212 pCache->szPage = szPage;
49213 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
49214 }
49215 return SQLITE_OK;
49216}
49217
49218/*
49219** Try to obtain a page from the cache.
49220**
49221** This routine returns a pointer to an sqlite3_pcache_page object if
49222** such an object is already in cache, or if a new one is created.
49223** This routine returns a NULL pointer if the object was not in cache
49224** and could not be created.
49225**
49226** The createFlags should be 0 to check for existing pages and should
49227** be 3 (not 1, but 3) to try to create a new page.
49228**
49229** If the createFlag is 0, then NULL is always returned if the page
49230** is not already in the cache. If createFlag is 1, then a new page
49231** is created only if that can be done without spilling dirty pages
49232** and without exceeding the cache size limit.
49233**
49234** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
49235** initialize the sqlite3_pcache_page object and convert it into a
49236** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
49237** routines are split this way for performance reasons. When separated
49238** they can both (usually) operate without having to push values to
49239** the stack on entry and pop them back off on exit, which saves a
49240** lot of pushing and popping.
49241*/
49242SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
49243 PCache *pCache, /* Obtain the page from this cache */
49244 Pgno pgno, /* Page number to obtain */
49245 int createFlag /* If true, create page if it does not exist already */
49246){
49247 int eCreate;
49248 sqlite3_pcache_page *pRes;
49249
49250 assert( pCache!=0 );
49251 assert( pCache->pCache!=0 );
49252 assert( createFlag==3 || createFlag==0 );
49253 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
49254
49255 /* eCreate defines what to do if the page does not exist.
49256 ** 0 Do not allocate a new page. (createFlag==0)
49257 ** 1 Allocate a new page if doing so is inexpensive.
49258 ** (createFlag==1 AND bPurgeable AND pDirty)
49259 ** 2 Allocate a new page even it doing so is difficult.
49260 ** (createFlag==1 AND !(bPurgeable AND pDirty)
49261 */
49262 eCreate = createFlag & pCache->eCreate;
49263 assert( eCreate==0 || eCreate==1 || eCreate==2 );
49264 assert( createFlag==0 || pCache->eCreate==eCreate );
49265 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
49266 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
49267 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
49268 createFlag?" create":"",pRes));
49269 return pRes;
49270}
49271
49272/*
49273** If the sqlite3PcacheFetch() routine is unable to allocate a new
49274** page because no clean pages are available for reuse and the cache
49275** size limit has been reached, then this routine can be invoked to
49276** try harder to allocate a page. This routine might invoke the stress
49277** callback to spill dirty pages to the journal. It will then try to
49278** allocate the new page and will only fail to allocate a new page on
49279** an OOM error.
49280**
49281** This routine should be invoked only after sqlite3PcacheFetch() fails.
49282*/
49283SQLITE_PRIVATE int sqlite3PcacheFetchStress(
49284 PCache *pCache, /* Obtain the page from this cache */
49285 Pgno pgno, /* Page number to obtain */
49286 sqlite3_pcache_page **ppPage /* Write result here */
49287){
49288 PgHdr *pPg;
49289 if( pCache->eCreate==2 ) return 0;
49290
49291 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
49292 /* Find a dirty page to write-out and recycle. First try to find a
49293 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
49294 ** cleared), but if that is not possible settle for any other
49295 ** unreferenced dirty page.
49296 **
49297 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
49298 ** flag is currently referenced, then the following may leave pSynced
49299 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
49300 ** cleared). This is Ok, as pSynced is just an optimization. */
49301 for(pPg=pCache->pSynced;
49302 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
49303 pPg=pPg->pDirtyPrev
49304 );
49305 pCache->pSynced = pPg;
49306 if( !pPg ){
49307 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
49308 }
49309 if( pPg ){
49310 int rc;
49311#ifdef SQLITE_LOG_CACHE_SPILL
49312 sqlite3_log(SQLITE_FULL,
49313 "spill page %d making room for %d - cache used: %d/%d",
49314 pPg->pgno, pgno,
49315 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
49316 numberOfCachePages(pCache));
49317#endif
49318 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
49319 rc = pCache->xStress(pCache->pStress, pPg);
49320 pcacheDump(pCache);
49321 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
49322 return rc;
49323 }
49324 }
49325 }
49326 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
49327 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
49328}
49329
49330/*
49331** This is a helper routine for sqlite3PcacheFetchFinish()
49332**
49333** In the uncommon case where the page being fetched has not been
49334** initialized, this routine is invoked to do the initialization.
49335** This routine is broken out into a separate function since it
49336** requires extra stack manipulation that can be avoided in the common
49337** case.
49338*/
49339static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
49340 PCache *pCache, /* Obtain the page from this cache */
49341 Pgno pgno, /* Page number obtained */
49342 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
49343){
49344 PgHdr *pPgHdr;
49345 assert( pPage!=0 );
49346 pPgHdr = (PgHdr*)pPage->pExtra;
49347 assert( pPgHdr->pPage==0 );
49348 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
49349 pPgHdr->pPage = pPage;
49350 pPgHdr->pData = pPage->pBuf;
49351 pPgHdr->pExtra = (void *)&pPgHdr[1];
49352 memset(pPgHdr->pExtra, 0, 8);
49353 pPgHdr->pCache = pCache;
49354 pPgHdr->pgno = pgno;
49355 pPgHdr->flags = PGHDR_CLEAN;
49356 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
49357}
49358
49359/*
49360** This routine converts the sqlite3_pcache_page object returned by
49361** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
49362** must be called after sqlite3PcacheFetch() in order to get a usable
49363** result.
49364*/
49365SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
49366 PCache *pCache, /* Obtain the page from this cache */
49367 Pgno pgno, /* Page number obtained */
49368 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
49369){
49370 PgHdr *pPgHdr;
49371
49372 assert( pPage!=0 );
49373 pPgHdr = (PgHdr *)pPage->pExtra;
49374
49375 if( !pPgHdr->pPage ){
49376 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
49377 }
49378 pCache->nRefSum++;
49379 pPgHdr->nRef++;
49380 assert( sqlite3PcachePageSanity(pPgHdr) );
49381 return pPgHdr;
49382}
49383
49384/*
49385** Decrement the reference count on a page. If the page is clean and the
49386** reference count drops to 0, then it is made eligible for recycling.
49387*/
49388SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
49389 assert( p->nRef>0 );
49390 p->pCache->nRefSum--;
49391 if( (--p->nRef)==0 ){
49392 if( p->flags&PGHDR_CLEAN ){
49393 pcacheUnpin(p);
49394 }else{
49395 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
49396 }
49397 }
49398}
49399
49400/*
49401** Increase the reference count of a supplied page by 1.
49402*/
49403SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
49404 assert(p->nRef>0);
49405 assert( sqlite3PcachePageSanity(p) );
49406 p->nRef++;
49407 p->pCache->nRefSum++;
49408}
49409
49410/*
49411** Drop a page from the cache. There must be exactly one reference to the
49412** page. This function deletes that reference, so after it returns the
49413** page pointed to by p is invalid.
49414*/
49415SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
49416 assert( p->nRef==1 );
49417 assert( sqlite3PcachePageSanity(p) );
49418 if( p->flags&PGHDR_DIRTY ){
49419 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
49420 }
49421 p->pCache->nRefSum--;
49422 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
49423}
49424
49425/*
49426** Make sure the page is marked as dirty. If it isn't dirty already,
49427** make it so.
49428*/
49429SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
49430 assert( p->nRef>0 );
49431 assert( sqlite3PcachePageSanity(p) );
49432 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
49433 p->flags &= ~PGHDR_DONT_WRITE;
49434 if( p->flags & PGHDR_CLEAN ){
49435 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
49436 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
49437 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
49438 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
49439 }
49440 assert( sqlite3PcachePageSanity(p) );
49441 }
49442}
49443
49444/*
49445** Make sure the page is marked as clean. If it isn't clean already,
49446** make it so.
49447*/
49448SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
49449 assert( sqlite3PcachePageSanity(p) );
49450 assert( (p->flags & PGHDR_DIRTY)!=0 );
49451 assert( (p->flags & PGHDR_CLEAN)==0 );
49452 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
49453 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49454 p->flags |= PGHDR_CLEAN;
49455 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
49456 assert( sqlite3PcachePageSanity(p) );
49457 if( p->nRef==0 ){
49458 pcacheUnpin(p);
49459 }
49460}
49461
49462/*
49463** Make every page in the cache clean.
49464*/
49465SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
49466 PgHdr *p;
49467 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
49468 while( (p = pCache->pDirty)!=0 ){
49469 sqlite3PcacheMakeClean(p);
49470 }
49471}
49472
49473/*
49474** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
49475*/
49476SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
49477 PgHdr *p;
49478 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
49479 for(p=pCache->pDirty; p; p=p->pDirtyNext){
49480 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49481 }
49482 pCache->pSynced = pCache->pDirtyTail;
49483}
49484
49485/*
49486** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
49487*/
49488SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
49489 PgHdr *p;
49490 for(p=pCache->pDirty; p; p=p->pDirtyNext){
49491 p->flags &= ~PGHDR_NEED_SYNC;
49492 }
49493 pCache->pSynced = pCache->pDirtyTail;
49494}
49495
49496/*
49497** Change the page number of page p to newPgno.
49498*/
49499SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
49500 PCache *pCache = p->pCache;
49501 assert( p->nRef>0 );
49502 assert( newPgno>0 );
49503 assert( sqlite3PcachePageSanity(p) );
49504 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
49505 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
49506 p->pgno = newPgno;
49507 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
49508 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
49509 }
49510}
49511
49512/*
49513** Drop every cache entry whose page number is greater than "pgno". The
49514** caller must ensure that there are no outstanding references to any pages
49515** other than page 1 with a page number greater than pgno.
49516**
49517** If there is a reference to page 1 and the pgno parameter passed to this
49518** function is 0, then the data area associated with page 1 is zeroed, but
49519** the page object is not dropped.
49520*/
49521SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
49522 if( pCache->pCache ){
49523 PgHdr *p;
49524 PgHdr *pNext;
49525 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
49526 for(p=pCache->pDirty; p; p=pNext){
49527 pNext = p->pDirtyNext;
49528 /* This routine never gets call with a positive pgno except right
49529 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
49530 ** it must be that pgno==0.
49531 */
49532 assert( p->pgno>0 );
49533 if( p->pgno>pgno ){
49534 assert( p->flags&PGHDR_DIRTY );
49535 sqlite3PcacheMakeClean(p);
49536 }
49537 }
49538 if( pgno==0 && pCache->nRefSum ){
49539 sqlite3_pcache_page *pPage1;
49540 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
49541 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
49542 ** pCache->nRefSum>0 */
49543 memset(pPage1->pBuf, 0, pCache->szPage);
49544 pgno = 1;
49545 }
49546 }
49547 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
49548 }
49549}
49550
49551/*
49552** Close a cache.
49553*/
49554SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
49555 assert( pCache->pCache!=0 );
49556 pcacheTrace(("%p.CLOSE\n",pCache));
49557 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
49558}
49559
49560/*
49561** Discard the contents of the cache.
49562*/
49563SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
49564 sqlite3PcacheTruncate(pCache, 0);
49565}
49566
49567/*
49568** Merge two lists of pages connected by pDirty and in pgno order.
49569** Do not bother fixing the pDirtyPrev pointers.
49570*/
49571static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
49572 PgHdr result, *pTail;
49573 pTail = &result;
49574 assert( pA!=0 && pB!=0 );
49575 for(;;){
49576 if( pA->pgno<pB->pgno ){
49577 pTail->pDirty = pA;
49578 pTail = pA;
49579 pA = pA->pDirty;
49580 if( pA==0 ){
49581 pTail->pDirty = pB;
49582 break;
49583 }
49584 }else{
49585 pTail->pDirty = pB;
49586 pTail = pB;
49587 pB = pB->pDirty;
49588 if( pB==0 ){
49589 pTail->pDirty = pA;
49590 break;
49591 }
49592 }
49593 }
49594 return result.pDirty;
49595}
49596
49597/*
49598** Sort the list of pages in accending order by pgno. Pages are
49599** connected by pDirty pointers. The pDirtyPrev pointers are
49600** corrupted by this sort.
49601**
49602** Since there cannot be more than 2^31 distinct pages in a database,
49603** there cannot be more than 31 buckets required by the merge sorter.
49604** One extra bucket is added to catch overflow in case something
49605** ever changes to make the previous sentence incorrect.
49606*/
49607#define N_SORT_BUCKET 32
49608static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
49609 PgHdr *a[N_SORT_BUCKET], *p;
49610 int i;
49611 memset(a, 0, sizeof(a));
49612 while( pIn ){
49613 p = pIn;
49614 pIn = p->pDirty;
49615 p->pDirty = 0;
49616 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
49617 if( a[i]==0 ){
49618 a[i] = p;
49619 break;
49620 }else{
49621 p = pcacheMergeDirtyList(a[i], p);
49622 a[i] = 0;
49623 }
49624 }
49625 if( NEVER(i==N_SORT_BUCKET-1) ){
49626 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
49627 ** the input list. But that is impossible.
49628 */
49629 a[i] = pcacheMergeDirtyList(a[i], p);
49630 }
49631 }
49632 p = a[0];
49633 for(i=1; i<N_SORT_BUCKET; i++){
49634 if( a[i]==0 ) continue;
49635 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
49636 }
49637 return p;
49638}
49639
49640/*
49641** Return a list of all dirty pages in the cache, sorted by page number.
49642*/
49643SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
49644 PgHdr *p;
49645 for(p=pCache->pDirty; p; p=p->pDirtyNext){
49646 p->pDirty = p->pDirtyNext;
49647 }
49648 return pcacheSortDirtyList(pCache->pDirty);
49649}
49650
49651/*
49652** Return the total number of references to all pages held by the cache.
49653**
49654** This is not the total number of pages referenced, but the sum of the
49655** reference count for all pages.
49656*/
49657SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
49658 return pCache->nRefSum;
49659}
49660
49661/*
49662** Return the number of references to the page supplied as an argument.
49663*/
49664SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
49665 return p->nRef;
49666}
49667
49668/*
49669** Return the total number of pages in the cache.
49670*/
49671SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
49672 assert( pCache->pCache!=0 );
49673 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
49674}
49675
49676#ifdef SQLITE_TEST
49677/*
49678** Get the suggested cache-size value.
49679*/
49680SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
49681 return numberOfCachePages(pCache);
49682}
49683#endif
49684
49685/*
49686** Set the suggested cache-size value.
49687*/
49688SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
49689 assert( pCache->pCache!=0 );
49690 pCache->szCache = mxPage;
49691 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
49692 numberOfCachePages(pCache));
49693}
49694
49695/*
49696** Set the suggested cache-spill value. Make no changes if if the
49697** argument is zero. Return the effective cache-spill size, which will
49698** be the larger of the szSpill and szCache.
49699*/
49700SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
49701 int res;
49702 assert( p->pCache!=0 );
49703 if( mxPage ){
49704 if( mxPage<0 ){
49705 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
49706 }
49707 p->szSpill = mxPage;
49708 }
49709 res = numberOfCachePages(p);
49710 if( res<p->szSpill ) res = p->szSpill;
49711 return res;
49712}
49713
49714/*
49715** Free up as much memory as possible from the page cache.
49716*/
49717SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
49718 assert( pCache->pCache!=0 );
49719 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
49720}
49721
49722/*
49723** Return the size of the header added by this middleware layer
49724** in the page-cache hierarchy.
49725*/
49726SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
49727
49728/*
49729** Return the number of dirty pages currently in the cache, as a percentage
49730** of the configured cache size.
49731*/
49732SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
49733 PgHdr *pDirty;
49734 int nDirty = 0;
49735 int nCache = numberOfCachePages(pCache);
49736 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
49737 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
49738}
49739
49740#ifdef SQLITE_DIRECT_OVERFLOW_READ
49741/*
49742** Return true if there are one or more dirty pages in the cache. Else false.
49743*/
49744SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
49745 return (pCache->pDirty!=0);
49746}
49747#endif
49748
49749#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
49750/*
49751** For all dirty pages currently in the cache, invoke the specified
49752** callback. This is only used if the SQLITE_CHECK_PAGES macro is
49753** defined.
49754*/
49755SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
49756 PgHdr *pDirty;
49757 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
49758 xIter(pDirty);
49759 }
49760}
49761#endif
49762
49763/************** End of pcache.c **********************************************/
49764/************** Begin file pcache1.c *****************************************/
49765/*
49766** 2008 November 05
49767**
49768** The author disclaims copyright to this source code. In place of
49769** a legal notice, here is a blessing:
49770**
49771** May you do good and not evil.
49772** May you find forgiveness for yourself and forgive others.
49773** May you share freely, never taking more than you give.
49774**
49775*************************************************************************
49776**
49777** This file implements the default page cache implementation (the
49778** sqlite3_pcache interface). It also contains part of the implementation
49779** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
49780** If the default page cache implementation is overridden, then neither of
49781** these two features are available.
49782**
49783** A Page cache line looks like this:
49784**
49785** -------------------------------------------------------------
49786** | database page content | PgHdr1 | MemPage | PgHdr |
49787** -------------------------------------------------------------
49788**
49789** The database page content is up front (so that buffer overreads tend to
49790** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
49791** is the extension added by the btree.c module containing information such
49792** as the database page number and how that database page is used. PgHdr
49793** is added by the pcache.c layer and contains information used to keep track
49794** of which pages are "dirty". PgHdr1 is an extension added by this
49795** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
49796** PgHdr1 contains information needed to look up a page by its page number.
49797** The superclass sqlite3_pcache_page.pBuf points to the start of the
49798** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
49799**
49800** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
49801** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
49802** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
49803** size can vary according to architecture, compile-time options, and
49804** SQLite library version number.
49805**
49806** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
49807** using a separate memory allocation from the database page content. This
49808** seeks to overcome the "clownshoe" problem (also called "internal
49809** fragmentation" in academic literature) of allocating a few bytes more
49810** than a power of two with the memory allocator rounding up to the next
49811** power of two, and leaving the rounded-up space unused.
49812**
49813** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
49814** with this module. Information is passed back and forth as PgHdr1 pointers.
49815**
49816** The pcache.c and pager.c modules deal pointers to PgHdr objects.
49817** The btree.c module deals with pointers to MemPage objects.
49818**
49819** SOURCE OF PAGE CACHE MEMORY:
49820**
49821** Memory for a page might come from any of three sources:
49822**
49823** (1) The general-purpose memory allocator - sqlite3Malloc()
49824** (2) Global page-cache memory provided using sqlite3_config() with
49825** SQLITE_CONFIG_PAGECACHE.
49826** (3) PCache-local bulk allocation.
49827**
49828** The third case is a chunk of heap memory (defaulting to 100 pages worth)
49829** that is allocated when the page cache is created. The size of the local
49830** bulk allocation can be adjusted using
49831**
49832** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
49833**
49834** If N is positive, then N pages worth of memory are allocated using a single
49835** sqlite3Malloc() call and that memory is used for the first N pages allocated.
49836** Or if N is negative, then -1024*N bytes of memory are allocated and used
49837** for as many pages as can be accomodated.
49838**
49839** Only one of (2) or (3) can be used. Once the memory available to (2) or
49840** (3) is exhausted, subsequent allocations fail over to the general-purpose
49841** memory allocator (1).
49842**
49843** Earlier versions of SQLite used only methods (1) and (2). But experiments
49844** show that method (3) with N==100 provides about a 5% performance boost for
49845** common workloads.
49846*/
49847/* #include "sqliteInt.h" */
49848
49849typedef struct PCache1 PCache1;
49850typedef struct PgHdr1 PgHdr1;
49851typedef struct PgFreeslot PgFreeslot;
49852typedef struct PGroup PGroup;
49853
49854/*
49855** Each cache entry is represented by an instance of the following
49856** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
49857** PgHdr1.pCache->szPage bytes is allocated directly before this structure
49858** in memory.
49859**
49860** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
49861** but causes a 2-byte gap in the structure for most architectures (since
49862** pointers must be either 4 or 8-byte aligned). As this structure is located
49863** in memory directly after the associated page data, if the database is
49864** corrupt, code at the b-tree layer may overread the page buffer and
49865** read part of this structure before the corruption is detected. This
49866** can cause a valgrind error if the unitialized gap is accessed. Using u16
49867** ensures there is no such gap, and therefore no bytes of unitialized memory
49868** in the structure.
49869*/
49870struct PgHdr1 {
49871 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
49872 unsigned int iKey; /* Key value (page number) */
49873 u16 isBulkLocal; /* This page from bulk local storage */
49874 u16 isAnchor; /* This is the PGroup.lru element */
49875 PgHdr1 *pNext; /* Next in hash table chain */
49876 PCache1 *pCache; /* Cache that currently owns this page */
49877 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
49878 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
49879 /* NB: pLruPrev is only valid if pLruNext!=0 */
49880};
49881
49882/*
49883** A page is pinned if it is not on the LRU list. To be "pinned" means
49884** that the page is in active use and must not be deallocated.
49885*/
49886#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
49887#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
49888
49889/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
49890** of one or more PCaches that are able to recycle each other's unpinned
49891** pages when they are under memory pressure. A PGroup is an instance of
49892** the following object.
49893**
49894** This page cache implementation works in one of two modes:
49895**
49896** (1) Every PCache is the sole member of its own PGroup. There is
49897** one PGroup per PCache.
49898**
49899** (2) There is a single global PGroup that all PCaches are a member
49900** of.
49901**
49902** Mode 1 uses more memory (since PCache instances are not able to rob
49903** unused pages from other PCaches) but it also operates without a mutex,
49904** and is therefore often faster. Mode 2 requires a mutex in order to be
49905** threadsafe, but recycles pages more efficiently.
49906**
49907** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
49908** PGroup which is the pcache1.grp global variable and its mutex is
49909** SQLITE_MUTEX_STATIC_LRU.
49910*/
49911struct PGroup {
49912 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
49913 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
49914 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
49915 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
49916 unsigned int nPurgeable; /* Number of purgeable pages allocated */
49917 PgHdr1 lru; /* The beginning and end of the LRU list */
49918};
49919
49920/* Each page cache is an instance of the following object. Every
49921** open database file (including each in-memory database and each
49922** temporary or transient database) has a single page cache which
49923** is an instance of this object.
49924**
49925** Pointers to structures of this type are cast and returned as
49926** opaque sqlite3_pcache* handles.
49927*/
49928struct PCache1 {
49929 /* Cache configuration parameters. Page size (szPage) and the purgeable
49930 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
49931 ** cache is created and are never changed thereafter. nMax may be
49932 ** modified at any time by a call to the pcache1Cachesize() method.
49933 ** The PGroup mutex must be held when accessing nMax.
49934 */
49935 PGroup *pGroup; /* PGroup this cache belongs to */
49936 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
49937 int szPage; /* Size of database content section */
49938 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
49939 int szAlloc; /* Total size of one pcache line */
49940 int bPurgeable; /* True if cache is purgeable */
49941 unsigned int nMin; /* Minimum number of pages reserved */
49942 unsigned int nMax; /* Configured "cache_size" value */
49943 unsigned int n90pct; /* nMax*9/10 */
49944 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
49945 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
49946
49947 /* Hash table of all pages. The following variables may only be accessed
49948 ** when the accessor is holding the PGroup mutex.
49949 */
49950 unsigned int nRecyclable; /* Number of pages in the LRU list */
49951 unsigned int nPage; /* Total number of pages in apHash */
49952 unsigned int nHash; /* Number of slots in apHash[] */
49953 PgHdr1 **apHash; /* Hash table for fast lookup by key */
49954 PgHdr1 *pFree; /* List of unused pcache-local pages */
49955 void *pBulk; /* Bulk memory used by pcache-local */
49956};
49957
49958/*
49959** Free slots in the allocator used to divide up the global page cache
49960** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
49961*/
49962struct PgFreeslot {
49963 PgFreeslot *pNext; /* Next free slot */
49964};
49965
49966/*
49967** Global data used by this cache.
49968*/
49969static SQLITE_WSD struct PCacheGlobal {
49970 PGroup grp; /* The global PGroup for mode (2) */
49971
49972 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
49973 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
49974 ** fixed at sqlite3_initialize() time and do not require mutex protection.
49975 ** The nFreeSlot and pFree values do require mutex protection.
49976 */
49977 int isInit; /* True if initialized */
49978 int separateCache; /* Use a new PGroup for each PCache */
49979 int nInitPage; /* Initial bulk allocation size */
49980 int szSlot; /* Size of each free slot */
49981 int nSlot; /* The number of pcache slots */
49982 int nReserve; /* Try to keep nFreeSlot above this */
49983 void *pStart, *pEnd; /* Bounds of global page cache memory */
49984 /* Above requires no mutex. Use mutex below for variable that follow. */
49985 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
49986 PgFreeslot *pFree; /* Free page blocks */
49987 int nFreeSlot; /* Number of unused pcache slots */
49988 /* The following value requires a mutex to change. We skip the mutex on
49989 ** reading because (1) most platforms read a 32-bit integer atomically and
49990 ** (2) even if an incorrect value is read, no great harm is done since this
49991 ** is really just an optimization. */
49992 int bUnderPressure; /* True if low on PAGECACHE memory */
49993} pcache1_g;
49994
49995/*
49996** All code in this file should access the global structure above via the
49997** alias "pcache1". This ensures that the WSD emulation is used when
49998** compiling for systems that do not support real WSD.
49999*/
50000#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
50001
50002/*
50003** Macros to enter and leave the PCache LRU mutex.
50004*/
50005#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
50006# define pcache1EnterMutex(X) assert((X)->mutex==0)
50007# define pcache1LeaveMutex(X) assert((X)->mutex==0)
50008# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
50009#else
50010# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
50011# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
50012# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
50013#endif
50014
50015/******************************************************************************/
50016/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
50017
50018
50019/*
50020** This function is called during initialization if a static buffer is
50021** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
50022** verb to sqlite3_config(). Parameter pBuf points to an allocation large
50023** enough to contain 'n' buffers of 'sz' bytes each.
50024**
50025** This routine is called from sqlite3_initialize() and so it is guaranteed
50026** to be serialized already. There is no need for further mutexing.
50027*/
50028SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
50029 if( pcache1.isInit ){
50030 PgFreeslot *p;
50031 if( pBuf==0 ) sz = n = 0;
50032 if( n==0 ) sz = 0;
50033 sz = ROUNDDOWN8(sz);
50034 pcache1.szSlot = sz;
50035 pcache1.nSlot = pcache1.nFreeSlot = n;
50036 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
50037 pcache1.pStart = pBuf;
50038 pcache1.pFree = 0;
50039 pcache1.bUnderPressure = 0;
50040 while( n-- ){
50041 p = (PgFreeslot*)pBuf;
50042 p->pNext = pcache1.pFree;
50043 pcache1.pFree = p;
50044 pBuf = (void*)&((char*)pBuf)[sz];
50045 }
50046 pcache1.pEnd = pBuf;
50047 }
50048}
50049
50050/*
50051** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
50052** true if pCache->pFree ends up containing one or more free pages.
50053*/
50054static int pcache1InitBulk(PCache1 *pCache){
50055 i64 szBulk;
50056 char *zBulk;
50057 if( pcache1.nInitPage==0 ) return 0;
50058 /* Do not bother with a bulk allocation if the cache size very small */
50059 if( pCache->nMax<3 ) return 0;
50060 sqlite3BeginBenignMalloc();
50061 if( pcache1.nInitPage>0 ){
50062 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
50063 }else{
50064 szBulk = -1024 * (i64)pcache1.nInitPage;
50065 }
50066 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
50067 szBulk = pCache->szAlloc*(i64)pCache->nMax;
50068 }
50069 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
50070 sqlite3EndBenignMalloc();
50071 if( zBulk ){
50072 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
50073 do{
50074 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
50075 pX->page.pBuf = zBulk;
50076 pX->page.pExtra = &pX[1];
50077 pX->isBulkLocal = 1;
50078 pX->isAnchor = 0;
50079 pX->pNext = pCache->pFree;
50080 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
50081 pCache->pFree = pX;
50082 zBulk += pCache->szAlloc;
50083 }while( --nBulk );
50084 }
50085 return pCache->pFree!=0;
50086}
50087
50088/*
50089** Malloc function used within this file to allocate space from the buffer
50090** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
50091** such buffer exists or there is no space left in it, this function falls
50092** back to sqlite3Malloc().
50093**
50094** Multiple threads can run this routine at the same time. Global variables
50095** in pcache1 need to be protected via mutex.
50096*/
50097static void *pcache1Alloc(int nByte){
50098 void *p = 0;
50099 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
50100 if( nByte<=pcache1.szSlot ){
50101 sqlite3_mutex_enter(pcache1.mutex);
50102 p = (PgHdr1 *)pcache1.pFree;
50103 if( p ){
50104 pcache1.pFree = pcache1.pFree->pNext;
50105 pcache1.nFreeSlot--;
50106 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
50107 assert( pcache1.nFreeSlot>=0 );
50108 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
50109 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
50110 }
50111 sqlite3_mutex_leave(pcache1.mutex);
50112 }
50113 if( p==0 ){
50114 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
50115 ** it from sqlite3Malloc instead.
50116 */
50117 p = sqlite3Malloc(nByte);
50118#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
50119 if( p ){
50120 int sz = sqlite3MallocSize(p);
50121 sqlite3_mutex_enter(pcache1.mutex);
50122 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
50123 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
50124 sqlite3_mutex_leave(pcache1.mutex);
50125 }
50126#endif
50127 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
50128 }
50129 return p;
50130}
50131
50132/*
50133** Free an allocated buffer obtained from pcache1Alloc().
50134*/
50135static void pcache1Free(void *p){
50136 if( p==0 ) return;
50137 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
50138 PgFreeslot *pSlot;
50139 sqlite3_mutex_enter(pcache1.mutex);
50140 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
50141 pSlot = (PgFreeslot*)p;
50142 pSlot->pNext = pcache1.pFree;
50143 pcache1.pFree = pSlot;
50144 pcache1.nFreeSlot++;
50145 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
50146 assert( pcache1.nFreeSlot<=pcache1.nSlot );
50147 sqlite3_mutex_leave(pcache1.mutex);
50148 }else{
50149 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
50150 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
50151#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
50152 {
50153 int nFreed = 0;
50154 nFreed = sqlite3MallocSize(p);
50155 sqlite3_mutex_enter(pcache1.mutex);
50156 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
50157 sqlite3_mutex_leave(pcache1.mutex);
50158 }
50159#endif
50160 sqlite3_free(p);
50161 }
50162}
50163
50164#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50165/*
50166** Return the size of a pcache allocation
50167*/
50168static int pcache1MemSize(void *p){
50169 if( p>=pcache1.pStart && p<pcache1.pEnd ){
50170 return pcache1.szSlot;
50171 }else{
50172 int iSize;
50173 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
50174 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
50175 iSize = sqlite3MallocSize(p);
50176 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
50177 return iSize;
50178 }
50179}
50180#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
50181
50182/*
50183** Allocate a new page object initially associated with cache pCache.
50184*/
50185static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
50186 PgHdr1 *p = 0;
50187 void *pPg;
50188
50189 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50190 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
50191 assert( pCache->pFree!=0 );
50192 p = pCache->pFree;
50193 pCache->pFree = p->pNext;
50194 p->pNext = 0;
50195 }else{
50196#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50197 /* The group mutex must be released before pcache1Alloc() is called. This
50198 ** is because it might call sqlite3_release_memory(), which assumes that
50199 ** this mutex is not held. */
50200 assert( pcache1.separateCache==0 );
50201 assert( pCache->pGroup==&pcache1.grp );
50202 pcache1LeaveMutex(pCache->pGroup);
50203#endif
50204 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
50205#ifdef SQLITE_PCACHE_SEPARATE_HEADER
50206 pPg = pcache1Alloc(pCache->szPage);
50207 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
50208 if( !pPg || !p ){
50209 pcache1Free(pPg);
50210 sqlite3_free(p);
50211 pPg = 0;
50212 }
50213#else
50214 pPg = pcache1Alloc(pCache->szAlloc);
50215#endif
50216 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
50217#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50218 pcache1EnterMutex(pCache->pGroup);
50219#endif
50220 if( pPg==0 ) return 0;
50221#ifndef SQLITE_PCACHE_SEPARATE_HEADER
50222 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
50223#endif
50224 p->page.pBuf = pPg;
50225 p->page.pExtra = &p[1];
50226 p->isBulkLocal = 0;
50227 p->isAnchor = 0;
50228 }
50229 (*pCache->pnPurgeable)++;
50230 return p;
50231}
50232
50233/*
50234** Free a page object allocated by pcache1AllocPage().
50235*/
50236static void pcache1FreePage(PgHdr1 *p){
50237 PCache1 *pCache;
50238 assert( p!=0 );
50239 pCache = p->pCache;
50240 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
50241 if( p->isBulkLocal ){
50242 p->pNext = pCache->pFree;
50243 pCache->pFree = p;
50244 }else{
50245 pcache1Free(p->page.pBuf);
50246#ifdef SQLITE_PCACHE_SEPARATE_HEADER
50247 sqlite3_free(p);
50248#endif
50249 }
50250 (*pCache->pnPurgeable)--;
50251}
50252
50253/*
50254** Malloc function used by SQLite to obtain space from the buffer configured
50255** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
50256** exists, this function falls back to sqlite3Malloc().
50257*/
50258SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
50259 assert( sz<=65536+8 ); /* These allocations are never very large */
50260 return pcache1Alloc(sz);
50261}
50262
50263/*
50264** Free an allocated buffer obtained from sqlite3PageMalloc().
50265*/
50266SQLITE_PRIVATE void sqlite3PageFree(void *p){
50267 pcache1Free(p);
50268}
50269
50270
50271/*
50272** Return true if it desirable to avoid allocating a new page cache
50273** entry.
50274**
50275** If memory was allocated specifically to the page cache using
50276** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
50277** it is desirable to avoid allocating a new page cache entry because
50278** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
50279** for all page cache needs and we should not need to spill the
50280** allocation onto the heap.
50281**
50282** Or, the heap is used for all page cache memory but the heap is
50283** under memory pressure, then again it is desirable to avoid
50284** allocating a new page cache entry in order to avoid stressing
50285** the heap even further.
50286*/
50287static int pcache1UnderMemoryPressure(PCache1 *pCache){
50288 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
50289 return pcache1.bUnderPressure;
50290 }else{
50291 return sqlite3HeapNearlyFull();
50292 }
50293}
50294
50295/******************************************************************************/
50296/******** General Implementation Functions ************************************/
50297
50298/*
50299** This function is used to resize the hash table used by the cache passed
50300** as the first argument.
50301**
50302** The PCache mutex must be held when this function is called.
50303*/
50304static void pcache1ResizeHash(PCache1 *p){
50305 PgHdr1 **apNew;
50306 unsigned int nNew;
50307 unsigned int i;
50308
50309 assert( sqlite3_mutex_held(p->pGroup->mutex) );
50310
50311 nNew = p->nHash*2;
50312 if( nNew<256 ){
50313 nNew = 256;
50314 }
50315
50316 pcache1LeaveMutex(p->pGroup);
50317 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
50318 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
50319 if( p->nHash ){ sqlite3EndBenignMalloc(); }
50320 pcache1EnterMutex(p->pGroup);
50321 if( apNew ){
50322 for(i=0; i<p->nHash; i++){
50323 PgHdr1 *pPage;
50324 PgHdr1 *pNext = p->apHash[i];
50325 while( (pPage = pNext)!=0 ){
50326 unsigned int h = pPage->iKey % nNew;
50327 pNext = pPage->pNext;
50328 pPage->pNext = apNew[h];
50329 apNew[h] = pPage;
50330 }
50331 }
50332 sqlite3_free(p->apHash);
50333 p->apHash = apNew;
50334 p->nHash = nNew;
50335 }
50336}
50337
50338/*
50339** This function is used internally to remove the page pPage from the
50340** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
50341** LRU list, then this function is a no-op.
50342**
50343** The PGroup mutex must be held when this function is called.
50344*/
50345static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
50346 assert( pPage!=0 );
50347 assert( PAGE_IS_UNPINNED(pPage) );
50348 assert( pPage->pLruNext );
50349 assert( pPage->pLruPrev );
50350 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
50351 pPage->pLruPrev->pLruNext = pPage->pLruNext;
50352 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
50353 pPage->pLruNext = 0;
50354 /* pPage->pLruPrev = 0;
50355 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
50356 assert( pPage->isAnchor==0 );
50357 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
50358 pPage->pCache->nRecyclable--;
50359 return pPage;
50360}
50361
50362
50363/*
50364** Remove the page supplied as an argument from the hash table
50365** (PCache1.apHash structure) that it is currently stored in.
50366** Also free the page if freePage is true.
50367**
50368** The PGroup mutex must be held when this function is called.
50369*/
50370static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
50371 unsigned int h;
50372 PCache1 *pCache = pPage->pCache;
50373 PgHdr1 **pp;
50374
50375 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50376 h = pPage->iKey % pCache->nHash;
50377 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
50378 *pp = (*pp)->pNext;
50379
50380 pCache->nPage--;
50381 if( freeFlag ) pcache1FreePage(pPage);
50382}
50383
50384/*
50385** If there are currently more than nMaxPage pages allocated, try
50386** to recycle pages to reduce the number allocated to nMaxPage.
50387*/
50388static void pcache1EnforceMaxPage(PCache1 *pCache){
50389 PGroup *pGroup = pCache->pGroup;
50390 PgHdr1 *p;
50391 assert( sqlite3_mutex_held(pGroup->mutex) );
50392 while( pGroup->nPurgeable>pGroup->nMaxPage
50393 && (p=pGroup->lru.pLruPrev)->isAnchor==0
50394 ){
50395 assert( p->pCache->pGroup==pGroup );
50396 assert( PAGE_IS_UNPINNED(p) );
50397 pcache1PinPage(p);
50398 pcache1RemoveFromHash(p, 1);
50399 }
50400 if( pCache->nPage==0 && pCache->pBulk ){
50401 sqlite3_free(pCache->pBulk);
50402 pCache->pBulk = pCache->pFree = 0;
50403 }
50404}
50405
50406/*
50407** Discard all pages from cache pCache with a page number (key value)
50408** greater than or equal to iLimit. Any pinned pages that meet this
50409** criteria are unpinned before they are discarded.
50410**
50411** The PCache mutex must be held when this function is called.
50412*/
50413static void pcache1TruncateUnsafe(
50414 PCache1 *pCache, /* The cache to truncate */
50415 unsigned int iLimit /* Drop pages with this pgno or larger */
50416){
50417 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
50418 unsigned int h, iStop;
50419 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50420 assert( pCache->iMaxKey >= iLimit );
50421 assert( pCache->nHash > 0 );
50422 if( pCache->iMaxKey - iLimit < pCache->nHash ){
50423 /* If we are just shaving the last few pages off the end of the
50424 ** cache, then there is no point in scanning the entire hash table.
50425 ** Only scan those hash slots that might contain pages that need to
50426 ** be removed. */
50427 h = iLimit % pCache->nHash;
50428 iStop = pCache->iMaxKey % pCache->nHash;
50429 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
50430 }else{
50431 /* This is the general case where many pages are being removed.
50432 ** It is necessary to scan the entire hash table */
50433 h = pCache->nHash/2;
50434 iStop = h - 1;
50435 }
50436 for(;;){
50437 PgHdr1 **pp;
50438 PgHdr1 *pPage;
50439 assert( h<pCache->nHash );
50440 pp = &pCache->apHash[h];
50441 while( (pPage = *pp)!=0 ){
50442 if( pPage->iKey>=iLimit ){
50443 pCache->nPage--;
50444 *pp = pPage->pNext;
50445 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
50446 pcache1FreePage(pPage);
50447 }else{
50448 pp = &pPage->pNext;
50449 TESTONLY( if( nPage>=0 ) nPage++; )
50450 }
50451 }
50452 if( h==iStop ) break;
50453 h = (h+1) % pCache->nHash;
50454 }
50455 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
50456}
50457
50458/******************************************************************************/
50459/******** sqlite3_pcache Methods **********************************************/
50460
50461/*
50462** Implementation of the sqlite3_pcache.xInit method.
50463*/
50464static int pcache1Init(void *NotUsed){
50465 UNUSED_PARAMETER(NotUsed);
50466 assert( pcache1.isInit==0 );
50467 memset(&pcache1, 0, sizeof(pcache1));
50468
50469
50470 /*
50471 ** The pcache1.separateCache variable is true if each PCache has its own
50472 ** private PGroup (mode-1). pcache1.separateCache is false if the single
50473 ** PGroup in pcache1.grp is used for all page caches (mode-2).
50474 **
50475 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
50476 **
50477 ** * Use a unified cache in single-threaded applications that have
50478 ** configured a start-time buffer for use as page-cache memory using
50479 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
50480 ** pBuf argument.
50481 **
50482 ** * Otherwise use separate caches (mode-1)
50483 */
50484#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
50485 pcache1.separateCache = 0;
50486#elif SQLITE_THREADSAFE
50487 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
50488 || sqlite3GlobalConfig.bCoreMutex>0;
50489#else
50490 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
50491#endif
50492
50493#if SQLITE_THREADSAFE
50494 if( sqlite3GlobalConfig.bCoreMutex ){
50495 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
50496 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
50497 }
50498#endif
50499 if( pcache1.separateCache
50500 && sqlite3GlobalConfig.nPage!=0
50501 && sqlite3GlobalConfig.pPage==0
50502 ){
50503 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
50504 }else{
50505 pcache1.nInitPage = 0;
50506 }
50507 pcache1.grp.mxPinned = 10;
50508 pcache1.isInit = 1;
50509 return SQLITE_OK;
50510}
50511
50512/*
50513** Implementation of the sqlite3_pcache.xShutdown method.
50514** Note that the static mutex allocated in xInit does
50515** not need to be freed.
50516*/
50517static void pcache1Shutdown(void *NotUsed){
50518 UNUSED_PARAMETER(NotUsed);
50519 assert( pcache1.isInit!=0 );
50520 memset(&pcache1, 0, sizeof(pcache1));
50521}
50522
50523/* forward declaration */
50524static void pcache1Destroy(sqlite3_pcache *p);
50525
50526/*
50527** Implementation of the sqlite3_pcache.xCreate method.
50528**
50529** Allocate a new cache.
50530*/
50531static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
50532 PCache1 *pCache; /* The newly created page cache */
50533 PGroup *pGroup; /* The group the new page cache will belong to */
50534 int sz; /* Bytes of memory required to allocate the new cache */
50535
50536 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
50537 assert( szExtra < 300 );
50538
50539 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
50540 pCache = (PCache1 *)sqlite3MallocZero(sz);
50541 if( pCache ){
50542 if( pcache1.separateCache ){
50543 pGroup = (PGroup*)&pCache[1];
50544 pGroup->mxPinned = 10;
50545 }else{
50546 pGroup = &pcache1.grp;
50547 }
50548 pcache1EnterMutex(pGroup);
50549 if( pGroup->lru.isAnchor==0 ){
50550 pGroup->lru.isAnchor = 1;
50551 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
50552 }
50553 pCache->pGroup = pGroup;
50554 pCache->szPage = szPage;
50555 pCache->szExtra = szExtra;
50556 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
50557 pCache->bPurgeable = (bPurgeable ? 1 : 0);
50558 pcache1ResizeHash(pCache);
50559 if( bPurgeable ){
50560 pCache->nMin = 10;
50561 pGroup->nMinPage += pCache->nMin;
50562 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50563 pCache->pnPurgeable = &pGroup->nPurgeable;
50564 }else{
50565 pCache->pnPurgeable = &pCache->nPurgeableDummy;
50566 }
50567 pcache1LeaveMutex(pGroup);
50568 if( pCache->nHash==0 ){
50569 pcache1Destroy((sqlite3_pcache*)pCache);
50570 pCache = 0;
50571 }
50572 }
50573 return (sqlite3_pcache *)pCache;
50574}
50575
50576/*
50577** Implementation of the sqlite3_pcache.xCachesize method.
50578**
50579** Configure the cache_size limit for a cache.
50580*/
50581static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
50582 PCache1 *pCache = (PCache1 *)p;
50583 if( pCache->bPurgeable ){
50584 PGroup *pGroup = pCache->pGroup;
50585 pcache1EnterMutex(pGroup);
50586 pGroup->nMaxPage += (nMax - pCache->nMax);
50587 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50588 pCache->nMax = nMax;
50589 pCache->n90pct = pCache->nMax*9/10;
50590 pcache1EnforceMaxPage(pCache);
50591 pcache1LeaveMutex(pGroup);
50592 }
50593}
50594
50595/*
50596** Implementation of the sqlite3_pcache.xShrink method.
50597**
50598** Free up as much memory as possible.
50599*/
50600static void pcache1Shrink(sqlite3_pcache *p){
50601 PCache1 *pCache = (PCache1*)p;
50602 if( pCache->bPurgeable ){
50603 PGroup *pGroup = pCache->pGroup;
50604 int savedMaxPage;
50605 pcache1EnterMutex(pGroup);
50606 savedMaxPage = pGroup->nMaxPage;
50607 pGroup->nMaxPage = 0;
50608 pcache1EnforceMaxPage(pCache);
50609 pGroup->nMaxPage = savedMaxPage;
50610 pcache1LeaveMutex(pGroup);
50611 }
50612}
50613
50614/*
50615** Implementation of the sqlite3_pcache.xPagecount method.
50616*/
50617static int pcache1Pagecount(sqlite3_pcache *p){
50618 int n;
50619 PCache1 *pCache = (PCache1*)p;
50620 pcache1EnterMutex(pCache->pGroup);
50621 n = pCache->nPage;
50622 pcache1LeaveMutex(pCache->pGroup);
50623 return n;
50624}
50625
50626
50627/*
50628** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
50629** in the header of the pcache1Fetch() procedure.
50630**
50631** This steps are broken out into a separate procedure because they are
50632** usually not needed, and by avoiding the stack initialization required
50633** for these steps, the main pcache1Fetch() procedure can run faster.
50634*/
50635static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
50636 PCache1 *pCache,
50637 unsigned int iKey,
50638 int createFlag
50639){
50640 unsigned int nPinned;
50641 PGroup *pGroup = pCache->pGroup;
50642 PgHdr1 *pPage = 0;
50643
50644 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
50645 assert( pCache->nPage >= pCache->nRecyclable );
50646 nPinned = pCache->nPage - pCache->nRecyclable;
50647 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
50648 assert( pCache->n90pct == pCache->nMax*9/10 );
50649 if( createFlag==1 && (
50650 nPinned>=pGroup->mxPinned
50651 || nPinned>=pCache->n90pct
50652 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
50653 )){
50654 return 0;
50655 }
50656
50657 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
50658 assert( pCache->nHash>0 && pCache->apHash );
50659
50660 /* Step 4. Try to recycle a page. */
50661 if( pCache->bPurgeable
50662 && !pGroup->lru.pLruPrev->isAnchor
50663 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
50664 ){
50665 PCache1 *pOther;
50666 pPage = pGroup->lru.pLruPrev;
50667 assert( PAGE_IS_UNPINNED(pPage) );
50668 pcache1RemoveFromHash(pPage, 0);
50669 pcache1PinPage(pPage);
50670 pOther = pPage->pCache;
50671 if( pOther->szAlloc != pCache->szAlloc ){
50672 pcache1FreePage(pPage);
50673 pPage = 0;
50674 }else{
50675 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
50676 }
50677 }
50678
50679 /* Step 5. If a usable page buffer has still not been found,
50680 ** attempt to allocate a new one.
50681 */
50682 if( !pPage ){
50683 pPage = pcache1AllocPage(pCache, createFlag==1);
50684 }
50685
50686 if( pPage ){
50687 unsigned int h = iKey % pCache->nHash;
50688 pCache->nPage++;
50689 pPage->iKey = iKey;
50690 pPage->pNext = pCache->apHash[h];
50691 pPage->pCache = pCache;
50692 pPage->pLruNext = 0;
50693 /* pPage->pLruPrev = 0;
50694 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
50695 *(void **)pPage->page.pExtra = 0;
50696 pCache->apHash[h] = pPage;
50697 if( iKey>pCache->iMaxKey ){
50698 pCache->iMaxKey = iKey;
50699 }
50700 }
50701 return pPage;
50702}
50703
50704/*
50705** Implementation of the sqlite3_pcache.xFetch method.
50706**
50707** Fetch a page by key value.
50708**
50709** Whether or not a new page may be allocated by this function depends on
50710** the value of the createFlag argument. 0 means do not allocate a new
50711** page. 1 means allocate a new page if space is easily available. 2
50712** means to try really hard to allocate a new page.
50713**
50714** For a non-purgeable cache (a cache used as the storage for an in-memory
50715** database) there is really no difference between createFlag 1 and 2. So
50716** the calling function (pcache.c) will never have a createFlag of 1 on
50717** a non-purgeable cache.
50718**
50719** There are three different approaches to obtaining space for a page,
50720** depending on the value of parameter createFlag (which may be 0, 1 or 2).
50721**
50722** 1. Regardless of the value of createFlag, the cache is searched for a
50723** copy of the requested page. If one is found, it is returned.
50724**
50725** 2. If createFlag==0 and the page is not already in the cache, NULL is
50726** returned.
50727**
50728** 3. If createFlag is 1, and the page is not already in the cache, then
50729** return NULL (do not allocate a new page) if any of the following
50730** conditions are true:
50731**
50732** (a) the number of pages pinned by the cache is greater than
50733** PCache1.nMax, or
50734**
50735** (b) the number of pages pinned by the cache is greater than
50736** the sum of nMax for all purgeable caches, less the sum of
50737** nMin for all other purgeable caches, or
50738**
50739** 4. If none of the first three conditions apply and the cache is marked
50740** as purgeable, and if one of the following is true:
50741**
50742** (a) The number of pages allocated for the cache is already
50743** PCache1.nMax, or
50744**
50745** (b) The number of pages allocated for all purgeable caches is
50746** already equal to or greater than the sum of nMax for all
50747** purgeable caches,
50748**
50749** (c) The system is under memory pressure and wants to avoid
50750** unnecessary pages cache entry allocations
50751**
50752** then attempt to recycle a page from the LRU list. If it is the right
50753** size, return the recycled buffer. Otherwise, free the buffer and
50754** proceed to step 5.
50755**
50756** 5. Otherwise, allocate and return a new page buffer.
50757**
50758** There are two versions of this routine. pcache1FetchWithMutex() is
50759** the general case. pcache1FetchNoMutex() is a faster implementation for
50760** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
50761** invokes the appropriate routine.
50762*/
50763static PgHdr1 *pcache1FetchNoMutex(
50764 sqlite3_pcache *p,
50765 unsigned int iKey,
50766 int createFlag
50767){
50768 PCache1 *pCache = (PCache1 *)p;
50769 PgHdr1 *pPage = 0;
50770
50771 /* Step 1: Search the hash table for an existing entry. */
50772 pPage = pCache->apHash[iKey % pCache->nHash];
50773 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
50774
50775 /* Step 2: If the page was found in the hash table, then return it.
50776 ** If the page was not in the hash table and createFlag is 0, abort.
50777 ** Otherwise (page not in hash and createFlag!=0) continue with
50778 ** subsequent steps to try to create the page. */
50779 if( pPage ){
50780 if( PAGE_IS_UNPINNED(pPage) ){
50781 return pcache1PinPage(pPage);
50782 }else{
50783 return pPage;
50784 }
50785 }else if( createFlag ){
50786 /* Steps 3, 4, and 5 implemented by this subroutine */
50787 return pcache1FetchStage2(pCache, iKey, createFlag);
50788 }else{
50789 return 0;
50790 }
50791}
50792#if PCACHE1_MIGHT_USE_GROUP_MUTEX
50793static PgHdr1 *pcache1FetchWithMutex(
50794 sqlite3_pcache *p,
50795 unsigned int iKey,
50796 int createFlag
50797){
50798 PCache1 *pCache = (PCache1 *)p;
50799 PgHdr1 *pPage;
50800
50801 pcache1EnterMutex(pCache->pGroup);
50802 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
50803 assert( pPage==0 || pCache->iMaxKey>=iKey );
50804 pcache1LeaveMutex(pCache->pGroup);
50805 return pPage;
50806}
50807#endif
50808static sqlite3_pcache_page *pcache1Fetch(
50809 sqlite3_pcache *p,
50810 unsigned int iKey,
50811 int createFlag
50812){
50813#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
50814 PCache1 *pCache = (PCache1 *)p;
50815#endif
50816
50817 assert( offsetof(PgHdr1,page)==0 );
50818 assert( pCache->bPurgeable || createFlag!=1 );
50819 assert( pCache->bPurgeable || pCache->nMin==0 );
50820 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
50821 assert( pCache->nMin==0 || pCache->bPurgeable );
50822 assert( pCache->nHash>0 );
50823#if PCACHE1_MIGHT_USE_GROUP_MUTEX
50824 if( pCache->pGroup->mutex ){
50825 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
50826 }else
50827#endif
50828 {
50829 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
50830 }
50831}
50832
50833
50834/*
50835** Implementation of the sqlite3_pcache.xUnpin method.
50836**
50837** Mark a page as unpinned (eligible for asynchronous recycling).
50838*/
50839static void pcache1Unpin(
50840 sqlite3_pcache *p,
50841 sqlite3_pcache_page *pPg,
50842 int reuseUnlikely
50843){
50844 PCache1 *pCache = (PCache1 *)p;
50845 PgHdr1 *pPage = (PgHdr1 *)pPg;
50846 PGroup *pGroup = pCache->pGroup;
50847
50848 assert( pPage->pCache==pCache );
50849 pcache1EnterMutex(pGroup);
50850
50851 /* It is an error to call this function if the page is already
50852 ** part of the PGroup LRU list.
50853 */
50854 assert( pPage->pLruNext==0 );
50855 assert( PAGE_IS_PINNED(pPage) );
50856
50857 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
50858 pcache1RemoveFromHash(pPage, 1);
50859 }else{
50860 /* Add the page to the PGroup LRU list. */
50861 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
50862 pPage->pLruPrev = &pGroup->lru;
50863 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
50864 *ppFirst = pPage;
50865 pCache->nRecyclable++;
50866 }
50867
50868 pcache1LeaveMutex(pCache->pGroup);
50869}
50870
50871/*
50872** Implementation of the sqlite3_pcache.xRekey method.
50873*/
50874static void pcache1Rekey(
50875 sqlite3_pcache *p,
50876 sqlite3_pcache_page *pPg,
50877 unsigned int iOld,
50878 unsigned int iNew
50879){
50880 PCache1 *pCache = (PCache1 *)p;
50881 PgHdr1 *pPage = (PgHdr1 *)pPg;
50882 PgHdr1 **pp;
50883 unsigned int h;
50884 assert( pPage->iKey==iOld );
50885 assert( pPage->pCache==pCache );
50886
50887 pcache1EnterMutex(pCache->pGroup);
50888
50889 h = iOld%pCache->nHash;
50890 pp = &pCache->apHash[h];
50891 while( (*pp)!=pPage ){
50892 pp = &(*pp)->pNext;
50893 }
50894 *pp = pPage->pNext;
50895
50896 h = iNew%pCache->nHash;
50897 pPage->iKey = iNew;
50898 pPage->pNext = pCache->apHash[h];
50899 pCache->apHash[h] = pPage;
50900 if( iNew>pCache->iMaxKey ){
50901 pCache->iMaxKey = iNew;
50902 }
50903
50904 pcache1LeaveMutex(pCache->pGroup);
50905}
50906
50907/*
50908** Implementation of the sqlite3_pcache.xTruncate method.
50909**
50910** Discard all unpinned pages in the cache with a page number equal to
50911** or greater than parameter iLimit. Any pinned pages with a page number
50912** equal to or greater than iLimit are implicitly unpinned.
50913*/
50914static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
50915 PCache1 *pCache = (PCache1 *)p;
50916 pcache1EnterMutex(pCache->pGroup);
50917 if( iLimit<=pCache->iMaxKey ){
50918 pcache1TruncateUnsafe(pCache, iLimit);
50919 pCache->iMaxKey = iLimit-1;
50920 }
50921 pcache1LeaveMutex(pCache->pGroup);
50922}
50923
50924/*
50925** Implementation of the sqlite3_pcache.xDestroy method.
50926**
50927** Destroy a cache allocated using pcache1Create().
50928*/
50929static void pcache1Destroy(sqlite3_pcache *p){
50930 PCache1 *pCache = (PCache1 *)p;
50931 PGroup *pGroup = pCache->pGroup;
50932 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
50933 pcache1EnterMutex(pGroup);
50934 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
50935 assert( pGroup->nMaxPage >= pCache->nMax );
50936 pGroup->nMaxPage -= pCache->nMax;
50937 assert( pGroup->nMinPage >= pCache->nMin );
50938 pGroup->nMinPage -= pCache->nMin;
50939 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
50940 pcache1EnforceMaxPage(pCache);
50941 pcache1LeaveMutex(pGroup);
50942 sqlite3_free(pCache->pBulk);
50943 sqlite3_free(pCache->apHash);
50944 sqlite3_free(pCache);
50945}
50946
50947/*
50948** This function is called during initialization (sqlite3_initialize()) to
50949** install the default pluggable cache module, assuming the user has not
50950** already provided an alternative.
50951*/
50952SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
50953 static const sqlite3_pcache_methods2 defaultMethods = {
50954 1, /* iVersion */
50955 0, /* pArg */
50956 pcache1Init, /* xInit */
50957 pcache1Shutdown, /* xShutdown */
50958 pcache1Create, /* xCreate */
50959 pcache1Cachesize, /* xCachesize */
50960 pcache1Pagecount, /* xPagecount */
50961 pcache1Fetch, /* xFetch */
50962 pcache1Unpin, /* xUnpin */
50963 pcache1Rekey, /* xRekey */
50964 pcache1Truncate, /* xTruncate */
50965 pcache1Destroy, /* xDestroy */
50966 pcache1Shrink /* xShrink */
50967 };
50968 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
50969}
50970
50971/*
50972** Return the size of the header on each page of this PCACHE implementation.
50973*/
50974SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
50975
50976/*
50977** Return the global mutex used by this PCACHE implementation. The
50978** sqlite3_status() routine needs access to this mutex.
50979*/
50980SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
50981 return pcache1.mutex;
50982}
50983
50984#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
50985/*
50986** This function is called to free superfluous dynamically allocated memory
50987** held by the pager system. Memory in use by any SQLite pager allocated
50988** by the current thread may be sqlite3_free()ed.
50989**
50990** nReq is the number of bytes of memory required. Once this much has
50991** been released, the function returns. The return value is the total number
50992** of bytes of memory released.
50993*/
50994SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
50995 int nFree = 0;
50996 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
50997 assert( sqlite3_mutex_notheld(pcache1.mutex) );
50998 if( sqlite3GlobalConfig.pPage==0 ){
50999 PgHdr1 *p;
51000 pcache1EnterMutex(&pcache1.grp);
51001 while( (nReq<0 || nFree<nReq)
51002 && (p=pcache1.grp.lru.pLruPrev)!=0
51003 && p->isAnchor==0
51004 ){
51005 nFree += pcache1MemSize(p->page.pBuf);
51006#ifdef SQLITE_PCACHE_SEPARATE_HEADER
51007 nFree += sqlite3MemSize(p);
51008#endif
51009 assert( PAGE_IS_UNPINNED(p) );
51010 pcache1PinPage(p);
51011 pcache1RemoveFromHash(p, 1);
51012 }
51013 pcache1LeaveMutex(&pcache1.grp);
51014 }
51015 return nFree;
51016}
51017#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
51018
51019#ifdef SQLITE_TEST
51020/*
51021** This function is used by test procedures to inspect the internal state
51022** of the global cache.
51023*/
51024SQLITE_PRIVATE void sqlite3PcacheStats(
51025 int *pnCurrent, /* OUT: Total number of pages cached */
51026 int *pnMax, /* OUT: Global maximum cache size */
51027 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
51028 int *pnRecyclable /* OUT: Total number of pages available for recycling */
51029){
51030 PgHdr1 *p;
51031 int nRecyclable = 0;
51032 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
51033 assert( PAGE_IS_UNPINNED(p) );
51034 nRecyclable++;
51035 }
51036 *pnCurrent = pcache1.grp.nPurgeable;
51037 *pnMax = (int)pcache1.grp.nMaxPage;
51038 *pnMin = (int)pcache1.grp.nMinPage;
51039 *pnRecyclable = nRecyclable;
51040}
51041#endif
51042
51043/************** End of pcache1.c *********************************************/
51044/************** Begin file rowset.c ******************************************/
51045/*
51046** 2008 December 3
51047**
51048** The author disclaims copyright to this source code. In place of
51049** a legal notice, here is a blessing:
51050**
51051** May you do good and not evil.
51052** May you find forgiveness for yourself and forgive others.
51053** May you share freely, never taking more than you give.
51054**
51055*************************************************************************
51056**
51057** This module implements an object we call a "RowSet".
51058**
51059** The RowSet object is a collection of rowids. Rowids
51060** are inserted into the RowSet in an arbitrary order. Inserts
51061** can be intermixed with tests to see if a given rowid has been
51062** previously inserted into the RowSet.
51063**
51064** After all inserts are finished, it is possible to extract the
51065** elements of the RowSet in sorted order. Once this extraction
51066** process has started, no new elements may be inserted.
51067**
51068** Hence, the primitive operations for a RowSet are:
51069**
51070** CREATE
51071** INSERT
51072** TEST
51073** SMALLEST
51074** DESTROY
51075**
51076** The CREATE and DESTROY primitives are the constructor and destructor,
51077** obviously. The INSERT primitive adds a new element to the RowSet.
51078** TEST checks to see if an element is already in the RowSet. SMALLEST
51079** extracts the least value from the RowSet.
51080**
51081** The INSERT primitive might allocate additional memory. Memory is
51082** allocated in chunks so most INSERTs do no allocation. There is an
51083** upper bound on the size of allocated memory. No memory is freed
51084** until DESTROY.
51085**
51086** The TEST primitive includes a "batch" number. The TEST primitive
51087** will only see elements that were inserted before the last change
51088** in the batch number. In other words, if an INSERT occurs between
51089** two TESTs where the TESTs have the same batch nubmer, then the
51090** value added by the INSERT will not be visible to the second TEST.
51091** The initial batch number is zero, so if the very first TEST contains
51092** a non-zero batch number, it will see all prior INSERTs.
51093**
51094** No INSERTs may occurs after a SMALLEST. An assertion will fail if
51095** that is attempted.
51096**
51097** The cost of an INSERT is roughly constant. (Sometimes new memory
51098** has to be allocated on an INSERT.) The cost of a TEST with a new
51099** batch number is O(NlogN) where N is the number of elements in the RowSet.
51100** The cost of a TEST using the same batch number is O(logN). The cost
51101** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
51102** primitives are constant time. The cost of DESTROY is O(N).
51103**
51104** TEST and SMALLEST may not be used by the same RowSet. This used to
51105** be possible, but the feature was not used, so it was removed in order
51106** to simplify the code.
51107*/
51108/* #include "sqliteInt.h" */
51109
51110
51111/*
51112** Target size for allocation chunks.
51113*/
51114#define ROWSET_ALLOCATION_SIZE 1024
51115
51116/*
51117** The number of rowset entries per allocation chunk.
51118*/
51119#define ROWSET_ENTRY_PER_CHUNK \
51120 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
51121
51122/*
51123** Each entry in a RowSet is an instance of the following object.
51124**
51125** This same object is reused to store a linked list of trees of RowSetEntry
51126** objects. In that alternative use, pRight points to the next entry
51127** in the list, pLeft points to the tree, and v is unused. The
51128** RowSet.pForest value points to the head of this forest list.
51129*/
51130struct RowSetEntry {
51131 i64 v; /* ROWID value for this entry */
51132 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
51133 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
51134};
51135
51136/*
51137** RowSetEntry objects are allocated in large chunks (instances of the
51138** following structure) to reduce memory allocation overhead. The
51139** chunks are kept on a linked list so that they can be deallocated
51140** when the RowSet is destroyed.
51141*/
51142struct RowSetChunk {
51143 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
51144 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
51145};
51146
51147/*
51148** A RowSet in an instance of the following structure.
51149**
51150** A typedef of this structure if found in sqliteInt.h.
51151*/
51152struct RowSet {
51153 struct RowSetChunk *pChunk; /* List of all chunk allocations */
51154 sqlite3 *db; /* The database connection */
51155 struct RowSetEntry *pEntry; /* List of entries using pRight */
51156 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
51157 struct RowSetEntry *pFresh; /* Source of new entry objects */
51158 struct RowSetEntry *pForest; /* List of binary trees of entries */
51159 u16 nFresh; /* Number of objects on pFresh */
51160 u16 rsFlags; /* Various flags */
51161 int iBatch; /* Current insert batch */
51162};
51163
51164/*
51165** Allowed values for RowSet.rsFlags
51166*/
51167#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
51168#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
51169
51170/*
51171** Allocate a RowSet object. Return NULL if a memory allocation
51172** error occurs.
51173*/
51174SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
51175 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
51176 if( p ){
51177 int N = sqlite3DbMallocSize(db, p);
51178 p->pChunk = 0;
51179 p->db = db;
51180 p->pEntry = 0;
51181 p->pLast = 0;
51182 p->pForest = 0;
51183 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
51184 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
51185 p->rsFlags = ROWSET_SORTED;
51186 p->iBatch = 0;
51187 }
51188 return p;
51189}
51190
51191/*
51192** Deallocate all chunks from a RowSet. This frees all memory that
51193** the RowSet has allocated over its lifetime. This routine is
51194** the destructor for the RowSet.
51195*/
51196SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
51197 RowSet *p = (RowSet*)pArg;
51198 struct RowSetChunk *pChunk, *pNextChunk;
51199 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
51200 pNextChunk = pChunk->pNextChunk;
51201 sqlite3DbFree(p->db, pChunk);
51202 }
51203 p->pChunk = 0;
51204 p->nFresh = 0;
51205 p->pEntry = 0;
51206 p->pLast = 0;
51207 p->pForest = 0;
51208 p->rsFlags = ROWSET_SORTED;
51209}
51210
51211/*
51212** Deallocate all chunks from a RowSet. This frees all memory that
51213** the RowSet has allocated over its lifetime. This routine is
51214** the destructor for the RowSet.
51215*/
51216SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
51217 sqlite3RowSetClear(pArg);
51218 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
51219}
51220
51221/*
51222** Allocate a new RowSetEntry object that is associated with the
51223** given RowSet. Return a pointer to the new and completely uninitialized
51224** object.
51225**
51226** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
51227** routine returns NULL.
51228*/
51229static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
51230 assert( p!=0 );
51231 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
51232 /* We could allocate a fresh RowSetEntry each time one is needed, but it
51233 ** is more efficient to pull a preallocated entry from the pool */
51234 struct RowSetChunk *pNew;
51235 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
51236 if( pNew==0 ){
51237 return 0;
51238 }
51239 pNew->pNextChunk = p->pChunk;
51240 p->pChunk = pNew;
51241 p->pFresh = pNew->aEntry;
51242 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
51243 }
51244 p->nFresh--;
51245 return p->pFresh++;
51246}
51247
51248/*
51249** Insert a new value into a RowSet.
51250**
51251** The mallocFailed flag of the database connection is set if a
51252** memory allocation fails.
51253*/
51254SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
51255 struct RowSetEntry *pEntry; /* The new entry */
51256 struct RowSetEntry *pLast; /* The last prior entry */
51257
51258 /* This routine is never called after sqlite3RowSetNext() */
51259 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
51260
51261 pEntry = rowSetEntryAlloc(p);
51262 if( pEntry==0 ) return;
51263 pEntry->v = rowid;
51264 pEntry->pRight = 0;
51265 pLast = p->pLast;
51266 if( pLast ){
51267 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
51268 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
51269 ** where possible */
51270 p->rsFlags &= ~ROWSET_SORTED;
51271 }
51272 pLast->pRight = pEntry;
51273 }else{
51274 p->pEntry = pEntry;
51275 }
51276 p->pLast = pEntry;
51277}
51278
51279/*
51280** Merge two lists of RowSetEntry objects. Remove duplicates.
51281**
51282** The input lists are connected via pRight pointers and are
51283** assumed to each already be in sorted order.
51284*/
51285static struct RowSetEntry *rowSetEntryMerge(
51286 struct RowSetEntry *pA, /* First sorted list to be merged */
51287 struct RowSetEntry *pB /* Second sorted list to be merged */
51288){
51289 struct RowSetEntry head;
51290 struct RowSetEntry *pTail;
51291
51292 pTail = &head;
51293 assert( pA!=0 && pB!=0 );
51294 for(;;){
51295 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
51296 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
51297 if( pA->v<=pB->v ){
51298 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
51299 pA = pA->pRight;
51300 if( pA==0 ){
51301 pTail->pRight = pB;
51302 break;
51303 }
51304 }else{
51305 pTail = pTail->pRight = pB;
51306 pB = pB->pRight;
51307 if( pB==0 ){
51308 pTail->pRight = pA;
51309 break;
51310 }
51311 }
51312 }
51313 return head.pRight;
51314}
51315
51316/*
51317** Sort all elements on the list of RowSetEntry objects into order of
51318** increasing v.
51319*/
51320static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
51321 unsigned int i;
51322 struct RowSetEntry *pNext, *aBucket[40];
51323
51324 memset(aBucket, 0, sizeof(aBucket));
51325 while( pIn ){
51326 pNext = pIn->pRight;
51327 pIn->pRight = 0;
51328 for(i=0; aBucket[i]; i++){
51329 pIn = rowSetEntryMerge(aBucket[i], pIn);
51330 aBucket[i] = 0;
51331 }
51332 aBucket[i] = pIn;
51333 pIn = pNext;
51334 }
51335 pIn = aBucket[0];
51336 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
51337 if( aBucket[i]==0 ) continue;
51338 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
51339 }
51340 return pIn;
51341}
51342
51343
51344/*
51345** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
51346** Convert this tree into a linked list connected by the pRight pointers
51347** and return pointers to the first and last elements of the new list.
51348*/
51349static void rowSetTreeToList(
51350 struct RowSetEntry *pIn, /* Root of the input tree */
51351 struct RowSetEntry **ppFirst, /* Write head of the output list here */
51352 struct RowSetEntry **ppLast /* Write tail of the output list here */
51353){
51354 assert( pIn!=0 );
51355 if( pIn->pLeft ){
51356 struct RowSetEntry *p;
51357 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
51358 p->pRight = pIn;
51359 }else{
51360 *ppFirst = pIn;
51361 }
51362 if( pIn->pRight ){
51363 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
51364 }else{
51365 *ppLast = pIn;
51366 }
51367 assert( (*ppLast)->pRight==0 );
51368}
51369
51370
51371/*
51372** Convert a sorted list of elements (connected by pRight) into a binary
51373** tree with depth of iDepth. A depth of 1 means the tree contains a single
51374** node taken from the head of *ppList. A depth of 2 means a tree with
51375** three nodes. And so forth.
51376**
51377** Use as many entries from the input list as required and update the
51378** *ppList to point to the unused elements of the list. If the input
51379** list contains too few elements, then construct an incomplete tree
51380** and leave *ppList set to NULL.
51381**
51382** Return a pointer to the root of the constructed binary tree.
51383*/
51384static struct RowSetEntry *rowSetNDeepTree(
51385 struct RowSetEntry **ppList,
51386 int iDepth
51387){
51388 struct RowSetEntry *p; /* Root of the new tree */
51389 struct RowSetEntry *pLeft; /* Left subtree */
51390 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
51391 /* Prevent unnecessary deep recursion when we run out of entries */
51392 return 0;
51393 }
51394 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
51395 /* This branch causes a *balanced* tree to be generated. A valid tree
51396 ** is still generated without this branch, but the tree is wildly
51397 ** unbalanced and inefficient. */
51398 pLeft = rowSetNDeepTree(ppList, iDepth-1);
51399 p = *ppList;
51400 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
51401 /* It is safe to always return here, but the resulting tree
51402 ** would be unbalanced */
51403 return pLeft;
51404 }
51405 p->pLeft = pLeft;
51406 *ppList = p->pRight;
51407 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
51408 }else{
51409 p = *ppList;
51410 *ppList = p->pRight;
51411 p->pLeft = p->pRight = 0;
51412 }
51413 return p;
51414}
51415
51416/*
51417** Convert a sorted list of elements into a binary tree. Make the tree
51418** as deep as it needs to be in order to contain the entire list.
51419*/
51420static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
51421 int iDepth; /* Depth of the tree so far */
51422 struct RowSetEntry *p; /* Current tree root */
51423 struct RowSetEntry *pLeft; /* Left subtree */
51424
51425 assert( pList!=0 );
51426 p = pList;
51427 pList = p->pRight;
51428 p->pLeft = p->pRight = 0;
51429 for(iDepth=1; pList; iDepth++){
51430 pLeft = p;
51431 p = pList;
51432 pList = p->pRight;
51433 p->pLeft = pLeft;
51434 p->pRight = rowSetNDeepTree(&pList, iDepth);
51435 }
51436 return p;
51437}
51438
51439/*
51440** Extract the smallest element from the RowSet.
51441** Write the element into *pRowid. Return 1 on success. Return
51442** 0 if the RowSet is already empty.
51443**
51444** After this routine has been called, the sqlite3RowSetInsert()
51445** routine may not be called again.
51446**
51447** This routine may not be called after sqlite3RowSetTest() has
51448** been used. Older versions of RowSet allowed that, but as the
51449** capability was not used by the code generator, it was removed
51450** for code economy.
51451*/
51452SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
51453 assert( p!=0 );
51454 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
51455
51456 /* Merge the forest into a single sorted list on first call */
51457 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51458 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51459 p->pEntry = rowSetEntrySort(p->pEntry);
51460 }
51461 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
51462 }
51463
51464 /* Return the next entry on the list */
51465 if( p->pEntry ){
51466 *pRowid = p->pEntry->v;
51467 p->pEntry = p->pEntry->pRight;
51468 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
51469 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
51470 sqlite3RowSetClear(p);
51471 }
51472 return 1;
51473 }else{
51474 return 0;
51475 }
51476}
51477
51478/*
51479** Check to see if element iRowid was inserted into the rowset as
51480** part of any insert batch prior to iBatch. Return 1 or 0.
51481**
51482** If this is the first test of a new batch and if there exist entries
51483** on pRowSet->pEntry, then sort those entries into the forest at
51484** pRowSet->pForest so that they can be tested.
51485*/
51486SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
51487 struct RowSetEntry *p, *pTree;
51488
51489 /* This routine is never called after sqlite3RowSetNext() */
51490 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
51491
51492 /* Sort entries into the forest on the first test of a new batch.
51493 ** To save unnecessary work, only do this when the batch number changes.
51494 */
51495 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
51496 p = pRowSet->pEntry;
51497 if( p ){
51498 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
51499 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51500 /* Only sort the current set of entries if they need it */
51501 p = rowSetEntrySort(p);
51502 }
51503 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51504 ppPrevTree = &pTree->pRight;
51505 if( pTree->pLeft==0 ){
51506 pTree->pLeft = rowSetListToTree(p);
51507 break;
51508 }else{
51509 struct RowSetEntry *pAux, *pTail;
51510 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
51511 pTree->pLeft = 0;
51512 p = rowSetEntryMerge(pAux, p);
51513 }
51514 }
51515 if( pTree==0 ){
51516 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
51517 if( pTree ){
51518 pTree->v = 0;
51519 pTree->pRight = 0;
51520 pTree->pLeft = rowSetListToTree(p);
51521 }
51522 }
51523 pRowSet->pEntry = 0;
51524 pRowSet->pLast = 0;
51525 pRowSet->rsFlags |= ROWSET_SORTED;
51526 }
51527 pRowSet->iBatch = iBatch;
51528 }
51529
51530 /* Test to see if the iRowid value appears anywhere in the forest.
51531 ** Return 1 if it does and 0 if not.
51532 */
51533 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51534 p = pTree->pLeft;
51535 while( p ){
51536 if( p->v<iRowid ){
51537 p = p->pRight;
51538 }else if( p->v>iRowid ){
51539 p = p->pLeft;
51540 }else{
51541 return 1;
51542 }
51543 }
51544 }
51545 return 0;
51546}
51547
51548/************** End of rowset.c **********************************************/
51549/************** Begin file pager.c *******************************************/
51550/*
51551** 2001 September 15
51552**
51553** The author disclaims copyright to this source code. In place of
51554** a legal notice, here is a blessing:
51555**
51556** May you do good and not evil.
51557** May you find forgiveness for yourself and forgive others.
51558** May you share freely, never taking more than you give.
51559**
51560*************************************************************************
51561** This is the implementation of the page cache subsystem or "pager".
51562**
51563** The pager is used to access a database disk file. It implements
51564** atomic commit and rollback through the use of a journal file that
51565** is separate from the database file. The pager also implements file
51566** locking to prevent two processes from writing the same database
51567** file simultaneously, or one process from reading the database while
51568** another is writing.
51569*/
51570#ifndef SQLITE_OMIT_DISKIO
51571/* #include "sqliteInt.h" */
51572/************** Include wal.h in the middle of pager.c ***********************/
51573/************** Begin file wal.h *********************************************/
51574/*
51575** 2010 February 1
51576**
51577** The author disclaims copyright to this source code. In place of
51578** a legal notice, here is a blessing:
51579**
51580** May you do good and not evil.
51581** May you find forgiveness for yourself and forgive others.
51582** May you share freely, never taking more than you give.
51583**
51584*************************************************************************
51585** This header file defines the interface to the write-ahead logging
51586** system. Refer to the comments below and the header comment attached to
51587** the implementation of each function in log.c for further details.
51588*/
51589
51590#ifndef SQLITE_WAL_H
51591#define SQLITE_WAL_H
51592
51593/* #include "sqliteInt.h" */
51594
51595/* Macros for extracting appropriate sync flags for either transaction
51596** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
51597*/
51598#define WAL_SYNC_FLAGS(X) ((X)&0x03)
51599#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03)
51600
51601#ifdef SQLITE_OMIT_WAL
51602# define sqlite3WalOpen(x,y,z) 0
51603# define sqlite3WalLimit(x,y)
51604# define sqlite3WalClose(v,w,x,y,z) 0
51605# define sqlite3WalBeginReadTransaction(y,z) 0
51606# define sqlite3WalEndReadTransaction(z)
51607# define sqlite3WalDbsize(y) 0
51608# define sqlite3WalBeginWriteTransaction(y) 0
51609# define sqlite3WalEndWriteTransaction(x) 0
51610# define sqlite3WalUndo(x,y,z) 0
51611# define sqlite3WalSavepoint(y,z)
51612# define sqlite3WalSavepointUndo(y,z) 0
51613# define sqlite3WalFrames(u,v,w,x,y,z) 0
51614# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
51615# define sqlite3WalCallback(z) 0
51616# define sqlite3WalExclusiveMode(y,z) 0
51617# define sqlite3WalHeapMemory(z) 0
51618# define sqlite3WalFramesize(z) 0
51619# define sqlite3WalFindFrame(x,y,z) 0
51620# define sqlite3WalFile(x) 0
51621#else
51622
51623#define WAL_SAVEPOINT_NDATA 4
51624
51625/* Connection to a write-ahead log (WAL) file.
51626** There is one object of this type for each pager.
51627*/
51628typedef struct Wal Wal;
51629
51630/* Open and close a connection to a write-ahead log. */
51631SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
51632SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
51633
51634/* Set the limiting size of a WAL file. */
51635SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
51636
51637/* Used by readers to open (lock) and close (unlock) a snapshot. A
51638** snapshot is like a read-transaction. It is the state of the database
51639** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
51640** preserves the current state even if the other threads or processes
51641** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
51642** transaction and releases the lock.
51643*/
51644SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
51645SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
51646
51647/* Read a page from the write-ahead log, if it is present. */
51648SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
51649SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
51650
51651/* If the WAL is not empty, return the size of the database. */
51652SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
51653
51654/* Obtain or release the WRITER lock. */
51655SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
51656SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
51657
51658/* Undo any frames written (but not committed) to the log */
51659SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
51660
51661/* Return an integer that records the current (uncommitted) write
51662** position in the WAL */
51663SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
51664
51665/* Move the write position of the WAL back to iFrame. Called in
51666** response to a ROLLBACK TO command. */
51667SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
51668
51669/* Write a frame or frames to the log. */
51670SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
51671
51672/* Copy pages from the log to the database file */
51673SQLITE_PRIVATE int sqlite3WalCheckpoint(
51674 Wal *pWal, /* Write-ahead log connection */
51675 sqlite3 *db, /* Check this handle's interrupt flag */
51676 int eMode, /* One of PASSIVE, FULL and RESTART */
51677 int (*xBusy)(void*), /* Function to call when busy */
51678 void *pBusyArg, /* Context argument for xBusyHandler */
51679 int sync_flags, /* Flags to sync db file with (or 0) */
51680 int nBuf, /* Size of buffer nBuf */
51681 u8 *zBuf, /* Temporary buffer to use */
51682 int *pnLog, /* OUT: Number of frames in WAL */
51683 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
51684);
51685
51686/* Return the value to pass to a sqlite3_wal_hook callback, the
51687** number of frames in the WAL at the point of the last commit since
51688** sqlite3WalCallback() was called. If no commits have occurred since
51689** the last call, then return 0.
51690*/
51691SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
51692
51693/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
51694** by the pager layer on the database file.
51695*/
51696SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
51697
51698/* Return true if the argument is non-NULL and the WAL module is using
51699** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
51700** WAL module is using shared-memory, return false.
51701*/
51702SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
51703
51704#ifdef SQLITE_ENABLE_SNAPSHOT
51705SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
51706SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
51707SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
51708SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
51709SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
51710#endif
51711
51712#ifdef SQLITE_ENABLE_ZIPVFS
51713/* If the WAL file is not empty, return the number of bytes of content
51714** stored in each frame (i.e. the db page-size when the WAL was created).
51715*/
51716SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
51717#endif
51718
51719/* Return the sqlite3_file object for the WAL file */
51720SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
51721
51722#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51723SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
51724SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db);
51725#endif
51726
51727#endif /* ifndef SQLITE_OMIT_WAL */
51728#endif /* SQLITE_WAL_H */
51729
51730/************** End of wal.h *************************************************/
51731/************** Continuing where we left off in pager.c **********************/
51732
51733
51734/******************* NOTES ON THE DESIGN OF THE PAGER ************************
51735**
51736** This comment block describes invariants that hold when using a rollback
51737** journal. These invariants do not apply for journal_mode=WAL,
51738** journal_mode=MEMORY, or journal_mode=OFF.
51739**
51740** Within this comment block, a page is deemed to have been synced
51741** automatically as soon as it is written when PRAGMA synchronous=OFF.
51742** Otherwise, the page is not synced until the xSync method of the VFS
51743** is called successfully on the file containing the page.
51744**
51745** Definition: A page of the database file is said to be "overwriteable" if
51746** one or more of the following are true about the page:
51747**
51748** (a) The original content of the page as it was at the beginning of
51749** the transaction has been written into the rollback journal and
51750** synced.
51751**
51752** (b) The page was a freelist leaf page at the start of the transaction.
51753**
51754** (c) The page number is greater than the largest page that existed in
51755** the database file at the start of the transaction.
51756**
51757** (1) A page of the database file is never overwritten unless one of the
51758** following are true:
51759**
51760** (a) The page and all other pages on the same sector are overwriteable.
51761**
51762** (b) The atomic page write optimization is enabled, and the entire
51763** transaction other than the update of the transaction sequence
51764** number consists of a single page change.
51765**
51766** (2) The content of a page written into the rollback journal exactly matches
51767** both the content in the database when the rollback journal was written
51768** and the content in the database at the beginning of the current
51769** transaction.
51770**
51771** (3) Writes to the database file are an integer multiple of the page size
51772** in length and are aligned on a page boundary.
51773**
51774** (4) Reads from the database file are either aligned on a page boundary and
51775** an integer multiple of the page size in length or are taken from the
51776** first 100 bytes of the database file.
51777**
51778** (5) All writes to the database file are synced prior to the rollback journal
51779** being deleted, truncated, or zeroed.
51780**
51781** (6) If a super-journal file is used, then all writes to the database file
51782** are synced prior to the super-journal being deleted.
51783**
51784** Definition: Two databases (or the same database at two points it time)
51785** are said to be "logically equivalent" if they give the same answer to
51786** all queries. Note in particular the content of freelist leaf
51787** pages can be changed arbitrarily without affecting the logical equivalence
51788** of the database.
51789**
51790** (7) At any time, if any subset, including the empty set and the total set,
51791** of the unsynced changes to a rollback journal are removed and the
51792** journal is rolled back, the resulting database file will be logically
51793** equivalent to the database file at the beginning of the transaction.
51794**
51795** (8) When a transaction is rolled back, the xTruncate method of the VFS
51796** is called to restore the database file to the same size it was at
51797** the beginning of the transaction. (In some VFSes, the xTruncate
51798** method is a no-op, but that does not change the fact the SQLite will
51799** invoke it.)
51800**
51801** (9) Whenever the database file is modified, at least one bit in the range
51802** of bytes from 24 through 39 inclusive will be changed prior to releasing
51803** the EXCLUSIVE lock, thus signaling other connections on the same
51804** database to flush their caches.
51805**
51806** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
51807** than one billion transactions.
51808**
51809** (11) A database file is well-formed at the beginning and at the conclusion
51810** of every transaction.
51811**
51812** (12) An EXCLUSIVE lock is held on the database file when writing to
51813** the database file.
51814**
51815** (13) A SHARED lock is held on the database file while reading any
51816** content out of the database file.
51817**
51818******************************************************************************/
51819
51820/*
51821** Macros for troubleshooting. Normally turned off
51822*/
51823#if 0
51824int sqlite3PagerTrace=1; /* True to enable tracing */
51825#define sqlite3DebugPrintf printf
51826#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
51827#else
51828#define PAGERTRACE(X)
51829#endif
51830
51831/*
51832** The following two macros are used within the PAGERTRACE() macros above
51833** to print out file-descriptors.
51834**
51835** PAGERID() takes a pointer to a Pager struct as its argument. The
51836** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
51837** struct as its argument.
51838*/
51839#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
51840#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
51841
51842/*
51843** The Pager.eState variable stores the current 'state' of a pager. A
51844** pager may be in any one of the seven states shown in the following
51845** state diagram.
51846**
51847** OPEN <------+------+
51848** | | |
51849** V | |
51850** +---------> READER-------+ |
51851** | | |
51852** | V |
51853** |<-------WRITER_LOCKED------> ERROR
51854** | | ^
51855** | V |
51856** |<------WRITER_CACHEMOD-------->|
51857** | | |
51858** | V |
51859** |<-------WRITER_DBMOD---------->|
51860** | | |
51861** | V |
51862** +<------WRITER_FINISHED-------->+
51863**
51864**
51865** List of state transitions and the C [function] that performs each:
51866**
51867** OPEN -> READER [sqlite3PagerSharedLock]
51868** READER -> OPEN [pager_unlock]
51869**
51870** READER -> WRITER_LOCKED [sqlite3PagerBegin]
51871** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
51872** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
51873** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
51874** WRITER_*** -> READER [pager_end_transaction]
51875**
51876** WRITER_*** -> ERROR [pager_error]
51877** ERROR -> OPEN [pager_unlock]
51878**
51879**
51880** OPEN:
51881**
51882** The pager starts up in this state. Nothing is guaranteed in this
51883** state - the file may or may not be locked and the database size is
51884** unknown. The database may not be read or written.
51885**
51886** * No read or write transaction is active.
51887** * Any lock, or no lock at all, may be held on the database file.
51888** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
51889**
51890** READER:
51891**
51892** In this state all the requirements for reading the database in
51893** rollback (non-WAL) mode are met. Unless the pager is (or recently
51894** was) in exclusive-locking mode, a user-level read transaction is
51895** open. The database size is known in this state.
51896**
51897** A connection running with locking_mode=normal enters this state when
51898** it opens a read-transaction on the database and returns to state
51899** OPEN after the read-transaction is completed. However a connection
51900** running in locking_mode=exclusive (including temp databases) remains in
51901** this state even after the read-transaction is closed. The only way
51902** a locking_mode=exclusive connection can transition from READER to OPEN
51903** is via the ERROR state (see below).
51904**
51905** * A read transaction may be active (but a write-transaction cannot).
51906** * A SHARED or greater lock is held on the database file.
51907** * The dbSize variable may be trusted (even if a user-level read
51908** transaction is not active). The dbOrigSize and dbFileSize variables
51909** may not be trusted at this point.
51910** * If the database is a WAL database, then the WAL connection is open.
51911** * Even if a read-transaction is not open, it is guaranteed that
51912** there is no hot-journal in the file-system.
51913**
51914** WRITER_LOCKED:
51915**
51916** The pager moves to this state from READER when a write-transaction
51917** is first opened on the database. In WRITER_LOCKED state, all locks
51918** required to start a write-transaction are held, but no actual
51919** modifications to the cache or database have taken place.
51920**
51921** In rollback mode, a RESERVED or (if the transaction was opened with
51922** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
51923** moving to this state, but the journal file is not written to or opened
51924** to in this state. If the transaction is committed or rolled back while
51925** in WRITER_LOCKED state, all that is required is to unlock the database
51926** file.
51927**
51928** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
51929** If the connection is running with locking_mode=exclusive, an attempt
51930** is made to obtain an EXCLUSIVE lock on the database file.
51931**
51932** * A write transaction is active.
51933** * If the connection is open in rollback-mode, a RESERVED or greater
51934** lock is held on the database file.
51935** * If the connection is open in WAL-mode, a WAL write transaction
51936** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
51937** called).
51938** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
51939** * The contents of the pager cache have not been modified.
51940** * The journal file may or may not be open.
51941** * Nothing (not even the first header) has been written to the journal.
51942**
51943** WRITER_CACHEMOD:
51944**
51945** A pager moves from WRITER_LOCKED state to this state when a page is
51946** first modified by the upper layer. In rollback mode the journal file
51947** is opened (if it is not already open) and a header written to the
51948** start of it. The database file on disk has not been modified.
51949**
51950** * A write transaction is active.
51951** * A RESERVED or greater lock is held on the database file.
51952** * The journal file is open and the first header has been written
51953** to it, but the header has not been synced to disk.
51954** * The contents of the page cache have been modified.
51955**
51956** WRITER_DBMOD:
51957**
51958** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
51959** when it modifies the contents of the database file. WAL connections
51960** never enter this state (since they do not modify the database file,
51961** just the log file).
51962**
51963** * A write transaction is active.
51964** * An EXCLUSIVE or greater lock is held on the database file.
51965** * The journal file is open and the first header has been written
51966** and synced to disk.
51967** * The contents of the page cache have been modified (and possibly
51968** written to disk).
51969**
51970** WRITER_FINISHED:
51971**
51972** It is not possible for a WAL connection to enter this state.
51973**
51974** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
51975** state after the entire transaction has been successfully written into the
51976** database file. In this state the transaction may be committed simply
51977** by finalizing the journal file. Once in WRITER_FINISHED state, it is
51978** not possible to modify the database further. At this point, the upper
51979** layer must either commit or rollback the transaction.
51980**
51981** * A write transaction is active.
51982** * An EXCLUSIVE or greater lock is held on the database file.
51983** * All writing and syncing of journal and database data has finished.
51984** If no error occurred, all that remains is to finalize the journal to
51985** commit the transaction. If an error did occur, the caller will need
51986** to rollback the transaction.
51987**
51988** ERROR:
51989**
51990** The ERROR state is entered when an IO or disk-full error (including
51991** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
51992** difficult to be sure that the in-memory pager state (cache contents,
51993** db size etc.) are consistent with the contents of the file-system.
51994**
51995** Temporary pager files may enter the ERROR state, but in-memory pagers
51996** cannot.
51997**
51998** For example, if an IO error occurs while performing a rollback,
51999** the contents of the page-cache may be left in an inconsistent state.
52000** At this point it would be dangerous to change back to READER state
52001** (as usually happens after a rollback). Any subsequent readers might
52002** report database corruption (due to the inconsistent cache), and if
52003** they upgrade to writers, they may inadvertently corrupt the database
52004** file. To avoid this hazard, the pager switches into the ERROR state
52005** instead of READER following such an error.
52006**
52007** Once it has entered the ERROR state, any attempt to use the pager
52008** to read or write data returns an error. Eventually, once all
52009** outstanding transactions have been abandoned, the pager is able to
52010** transition back to OPEN state, discarding the contents of the
52011** page-cache and any other in-memory state at the same time. Everything
52012** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
52013** when a read-transaction is next opened on the pager (transitioning
52014** the pager into READER state). At that point the system has recovered
52015** from the error.
52016**
52017** Specifically, the pager jumps into the ERROR state if:
52018**
52019** 1. An error occurs while attempting a rollback. This happens in
52020** function sqlite3PagerRollback().
52021**
52022** 2. An error occurs while attempting to finalize a journal file
52023** following a commit in function sqlite3PagerCommitPhaseTwo().
52024**
52025** 3. An error occurs while attempting to write to the journal or
52026** database file in function pagerStress() in order to free up
52027** memory.
52028**
52029** In other cases, the error is returned to the b-tree layer. The b-tree
52030** layer then attempts a rollback operation. If the error condition
52031** persists, the pager enters the ERROR state via condition (1) above.
52032**
52033** Condition (3) is necessary because it can be triggered by a read-only
52034** statement executed within a transaction. In this case, if the error
52035** code were simply returned to the user, the b-tree layer would not
52036** automatically attempt a rollback, as it assumes that an error in a
52037** read-only statement cannot leave the pager in an internally inconsistent
52038** state.
52039**
52040** * The Pager.errCode variable is set to something other than SQLITE_OK.
52041** * There are one or more outstanding references to pages (after the
52042** last reference is dropped the pager should move back to OPEN state).
52043** * The pager is not an in-memory pager.
52044**
52045**
52046** Notes:
52047**
52048** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
52049** connection is open in WAL mode. A WAL connection is always in one
52050** of the first four states.
52051**
52052** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
52053** state. There are two exceptions: immediately after exclusive-mode has
52054** been turned on (and before any read or write transactions are
52055** executed), and when the pager is leaving the "error state".
52056**
52057** * See also: assert_pager_state().
52058*/
52059#define PAGER_OPEN 0
52060#define PAGER_READER 1
52061#define PAGER_WRITER_LOCKED 2
52062#define PAGER_WRITER_CACHEMOD 3
52063#define PAGER_WRITER_DBMOD 4
52064#define PAGER_WRITER_FINISHED 5
52065#define PAGER_ERROR 6
52066
52067/*
52068** The Pager.eLock variable is almost always set to one of the
52069** following locking-states, according to the lock currently held on
52070** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
52071** This variable is kept up to date as locks are taken and released by
52072** the pagerLockDb() and pagerUnlockDb() wrappers.
52073**
52074** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
52075** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
52076** the operation was successful. In these circumstances pagerLockDb() and
52077** pagerUnlockDb() take a conservative approach - eLock is always updated
52078** when unlocking the file, and only updated when locking the file if the
52079** VFS call is successful. This way, the Pager.eLock variable may be set
52080** to a less exclusive (lower) value than the lock that is actually held
52081** at the system level, but it is never set to a more exclusive value.
52082**
52083** This is usually safe. If an xUnlock fails or appears to fail, there may
52084** be a few redundant xLock() calls or a lock may be held for longer than
52085** required, but nothing really goes wrong.
52086**
52087** The exception is when the database file is unlocked as the pager moves
52088** from ERROR to OPEN state. At this point there may be a hot-journal file
52089** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
52090** transition, by the same pager or any other). If the call to xUnlock()
52091** fails at this point and the pager is left holding an EXCLUSIVE lock, this
52092** can confuse the call to xCheckReservedLock() call made later as part
52093** of hot-journal detection.
52094**
52095** xCheckReservedLock() is defined as returning true "if there is a RESERVED
52096** lock held by this process or any others". So xCheckReservedLock may
52097** return true because the caller itself is holding an EXCLUSIVE lock (but
52098** doesn't know it because of a previous error in xUnlock). If this happens
52099** a hot-journal may be mistaken for a journal being created by an active
52100** transaction in another process, causing SQLite to read from the database
52101** without rolling it back.
52102**
52103** To work around this, if a call to xUnlock() fails when unlocking the
52104** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
52105** is only changed back to a real locking state after a successful call
52106** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
52107** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
52108** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
52109** lock on the database file before attempting to roll it back. See function
52110** PagerSharedLock() for more detail.
52111**
52112** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
52113** PAGER_OPEN state.
52114*/
52115#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
52116
52117/*
52118** The maximum allowed sector size. 64KiB. If the xSectorsize() method
52119** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
52120** This could conceivably cause corruption following a power failure on
52121** such a system. This is currently an undocumented limit.
52122*/
52123#define MAX_SECTOR_SIZE 0x10000
52124
52125
52126/*
52127** An instance of the following structure is allocated for each active
52128** savepoint and statement transaction in the system. All such structures
52129** are stored in the Pager.aSavepoint[] array, which is allocated and
52130** resized using sqlite3Realloc().
52131**
52132** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
52133** set to 0. If a journal-header is written into the main journal while
52134** the savepoint is active, then iHdrOffset is set to the byte offset
52135** immediately following the last journal record written into the main
52136** journal before the journal-header. This is required during savepoint
52137** rollback (see pagerPlaybackSavepoint()).
52138*/
52139typedef struct PagerSavepoint PagerSavepoint;
52140struct PagerSavepoint {
52141 i64 iOffset; /* Starting offset in main journal */
52142 i64 iHdrOffset; /* See above */
52143 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
52144 Pgno nOrig; /* Original number of pages in file */
52145 Pgno iSubRec; /* Index of first record in sub-journal */
52146#ifndef SQLITE_OMIT_WAL
52147 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
52148#endif
52149};
52150
52151/*
52152** Bits of the Pager.doNotSpill flag. See further description below.
52153*/
52154#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
52155#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
52156#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
52157
52158/*
52159** An open page cache is an instance of struct Pager. A description of
52160** some of the more important member variables follows:
52161**
52162** eState
52163**
52164** The current 'state' of the pager object. See the comment and state
52165** diagram above for a description of the pager state.
52166**
52167** eLock
52168**
52169** For a real on-disk database, the current lock held on the database file -
52170** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
52171**
52172** For a temporary or in-memory database (neither of which require any
52173** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
52174** databases always have Pager.exclusiveMode==1, this tricks the pager
52175** logic into thinking that it already has all the locks it will ever
52176** need (and no reason to release them).
52177**
52178** In some (obscure) circumstances, this variable may also be set to
52179** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
52180** details.
52181**
52182** changeCountDone
52183**
52184** This boolean variable is used to make sure that the change-counter
52185** (the 4-byte header field at byte offset 24 of the database file) is
52186** not updated more often than necessary.
52187**
52188** It is set to true when the change-counter field is updated, which
52189** can only happen if an exclusive lock is held on the database file.
52190** It is cleared (set to false) whenever an exclusive lock is
52191** relinquished on the database file. Each time a transaction is committed,
52192** The changeCountDone flag is inspected. If it is true, the work of
52193** updating the change-counter is omitted for the current transaction.
52194**
52195** This mechanism means that when running in exclusive mode, a connection
52196** need only update the change-counter once, for the first transaction
52197** committed.
52198**
52199** setSuper
52200**
52201** When PagerCommitPhaseOne() is called to commit a transaction, it may
52202** (or may not) specify a super-journal name to be written into the
52203** journal file before it is synced to disk.
52204**
52205** Whether or not a journal file contains a super-journal pointer affects
52206** the way in which the journal file is finalized after the transaction is
52207** committed or rolled back when running in "journal_mode=PERSIST" mode.
52208** If a journal file does not contain a super-journal pointer, it is
52209** finalized by overwriting the first journal header with zeroes. If
52210** it does contain a super-journal pointer the journal file is finalized
52211** by truncating it to zero bytes, just as if the connection were
52212** running in "journal_mode=truncate" mode.
52213**
52214** Journal files that contain super-journal pointers cannot be finalized
52215** simply by overwriting the first journal-header with zeroes, as the
52216** super-journal pointer could interfere with hot-journal rollback of any
52217** subsequently interrupted transaction that reuses the journal file.
52218**
52219** The flag is cleared as soon as the journal file is finalized (either
52220** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
52221** journal file from being successfully finalized, the setSuper flag
52222** is cleared anyway (and the pager will move to ERROR state).
52223**
52224** doNotSpill
52225**
52226** This variables control the behavior of cache-spills (calls made by
52227** the pcache module to the pagerStress() routine to write cached data
52228** to the file-system in order to free up memory).
52229**
52230** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
52231** writing to the database from pagerStress() is disabled altogether.
52232** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
52233** comes up during savepoint rollback that requires the pcache module
52234** to allocate a new page to prevent the journal file from being written
52235** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
52236** case is a user preference.
52237**
52238** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
52239** pagerStress() is permitted, but syncing the journal file is not.
52240** This flag is set by sqlite3PagerWrite() when the file-system sector-size
52241** is larger than the database page-size in order to prevent a journal sync
52242** from happening in between the journalling of two pages on the same sector.
52243**
52244** subjInMemory
52245**
52246** This is a boolean variable. If true, then any required sub-journal
52247** is opened as an in-memory journal file. If false, then in-memory
52248** sub-journals are only used for in-memory pager files.
52249**
52250** This variable is updated by the upper layer each time a new
52251** write-transaction is opened.
52252**
52253** dbSize, dbOrigSize, dbFileSize
52254**
52255** Variable dbSize is set to the number of pages in the database file.
52256** It is valid in PAGER_READER and higher states (all states except for
52257** OPEN and ERROR).
52258**
52259** dbSize is set based on the size of the database file, which may be
52260** larger than the size of the database (the value stored at offset
52261** 28 of the database header by the btree). If the size of the file
52262** is not an integer multiple of the page-size, the value stored in
52263** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
52264** Except, any file that is greater than 0 bytes in size is considered
52265** to have at least one page. (i.e. a 1KB file with 2K page-size leads
52266** to dbSize==1).
52267**
52268** During a write-transaction, if pages with page-numbers greater than
52269** dbSize are modified in the cache, dbSize is updated accordingly.
52270** Similarly, if the database is truncated using PagerTruncateImage(),
52271** dbSize is updated.
52272**
52273** Variables dbOrigSize and dbFileSize are valid in states
52274** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
52275** variable at the start of the transaction. It is used during rollback,
52276** and to determine whether or not pages need to be journalled before
52277** being modified.
52278**
52279** Throughout a write-transaction, dbFileSize contains the size of
52280** the file on disk in pages. It is set to a copy of dbSize when the
52281** write-transaction is first opened, and updated when VFS calls are made
52282** to write or truncate the database file on disk.
52283**
52284** The only reason the dbFileSize variable is required is to suppress
52285** unnecessary calls to xTruncate() after committing a transaction. If,
52286** when a transaction is committed, the dbFileSize variable indicates
52287** that the database file is larger than the database image (Pager.dbSize),
52288** pager_truncate() is called. The pager_truncate() call uses xFilesize()
52289** to measure the database file on disk, and then truncates it if required.
52290** dbFileSize is not used when rolling back a transaction. In this case
52291** pager_truncate() is called unconditionally (which means there may be
52292** a call to xFilesize() that is not strictly required). In either case,
52293** pager_truncate() may cause the file to become smaller or larger.
52294**
52295** dbHintSize
52296**
52297** The dbHintSize variable is used to limit the number of calls made to
52298** the VFS xFileControl(FCNTL_SIZE_HINT) method.
52299**
52300** dbHintSize is set to a copy of the dbSize variable when a
52301** write-transaction is opened (at the same time as dbFileSize and
52302** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
52303** dbHintSize is increased to the number of pages that correspond to the
52304** size-hint passed to the method call. See pager_write_pagelist() for
52305** details.
52306**
52307** errCode
52308**
52309** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
52310** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
52311** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
52312** sub-codes.
52313**
52314** syncFlags, walSyncFlags
52315**
52316** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
52317** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
52318** and contains the flags used to sync the checkpoint operations in the
52319** lower two bits, and sync flags used for transaction commits in the WAL
52320** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
52321** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
52322** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
52323** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
52324** meaning that the 0x04 and 0x08 bits are both zero.
52325*/
52326struct Pager {
52327 sqlite3_vfs *pVfs; /* OS functions to use for IO */
52328 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
52329 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
52330 u8 useJournal; /* Use a rollback journal on this file */
52331 u8 noSync; /* Do not sync the journal if true */
52332 u8 fullSync; /* Do extra syncs of the journal for robustness */
52333 u8 extraSync; /* sync directory after journal delete */
52334 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
52335 u8 walSyncFlags; /* See description above */
52336 u8 tempFile; /* zFilename is a temporary or immutable file */
52337 u8 noLock; /* Do not lock (except in WAL mode) */
52338 u8 readOnly; /* True for a read-only database */
52339 u8 memDb; /* True to inhibit all file I/O */
52340
52341 /**************************************************************************
52342 ** The following block contains those class members that change during
52343 ** routine operation. Class members not in this block are either fixed
52344 ** when the pager is first created or else only change when there is a
52345 ** significant mode change (such as changing the page_size, locking_mode,
52346 ** or the journal_mode). From another view, these class members describe
52347 ** the "state" of the pager, while other class members describe the
52348 ** "configuration" of the pager.
52349 */
52350 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
52351 u8 eLock; /* Current lock held on database file */
52352 u8 changeCountDone; /* Set after incrementing the change-counter */
52353 u8 setSuper; /* Super-jrnl name is written into jrnl */
52354 u8 doNotSpill; /* Do not spill the cache when non-zero */
52355 u8 subjInMemory; /* True to use in-memory sub-journals */
52356 u8 bUseFetch; /* True to use xFetch() */
52357 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
52358 Pgno dbSize; /* Number of pages in the database */
52359 Pgno dbOrigSize; /* dbSize before the current transaction */
52360 Pgno dbFileSize; /* Number of pages in the database file */
52361 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
52362 int errCode; /* One of several kinds of errors */
52363 int nRec; /* Pages journalled since last j-header written */
52364 u32 cksumInit; /* Quasi-random value added to every checksum */
52365 u32 nSubRec; /* Number of records written to sub-journal */
52366 Bitvec *pInJournal; /* One bit for each page in the database file */
52367 sqlite3_file *fd; /* File descriptor for database */
52368 sqlite3_file *jfd; /* File descriptor for main journal */
52369 sqlite3_file *sjfd; /* File descriptor for sub-journal */
52370 i64 journalOff; /* Current write offset in the journal file */
52371 i64 journalHdr; /* Byte offset to previous journal header */
52372 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
52373 PagerSavepoint *aSavepoint; /* Array of active savepoints */
52374 int nSavepoint; /* Number of elements in aSavepoint[] */
52375 u32 iDataVersion; /* Changes whenever database content changes */
52376 char dbFileVers[16]; /* Changes whenever database file changes */
52377
52378 int nMmapOut; /* Number of mmap pages currently outstanding */
52379 sqlite3_int64 szMmap; /* Desired maximum mmap size */
52380 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
52381 /*
52382 ** End of the routinely-changing class members
52383 ***************************************************************************/
52384
52385 u16 nExtra; /* Add this many bytes to each in-memory page */
52386 i16 nReserve; /* Number of unused bytes at end of each page */
52387 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
52388 u32 sectorSize; /* Assumed sector size during rollback */
52389 int pageSize; /* Number of bytes in a page */
52390 Pgno mxPgno; /* Maximum allowed size of the database */
52391 i64 journalSizeLimit; /* Size limit for persistent journal files */
52392 char *zFilename; /* Name of the database file */
52393 char *zJournal; /* Name of the journal file */
52394 int (*xBusyHandler)(void*); /* Function to call when busy */
52395 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
52396 int aStat[4]; /* Total cache hits, misses, writes, spills */
52397#ifdef SQLITE_TEST
52398 int nRead; /* Database pages read */
52399#endif
52400 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
52401 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
52402 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
52403 PCache *pPCache; /* Pointer to page cache object */
52404#ifndef SQLITE_OMIT_WAL
52405 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
52406 char *zWal; /* File name for write-ahead log */
52407#endif
52408};
52409
52410/*
52411** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
52412** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
52413** or CACHE_WRITE to sqlite3_db_status().
52414*/
52415#define PAGER_STAT_HIT 0
52416#define PAGER_STAT_MISS 1
52417#define PAGER_STAT_WRITE 2
52418#define PAGER_STAT_SPILL 3
52419
52420/*
52421** The following global variables hold counters used for
52422** testing purposes only. These variables do not exist in
52423** a non-testing build. These variables are not thread-safe.
52424*/
52425#ifdef SQLITE_TEST
52426SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
52427SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
52428SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
52429# define PAGER_INCR(v) v++
52430#else
52431# define PAGER_INCR(v)
52432#endif
52433
52434
52435
52436/*
52437** Journal files begin with the following magic string. The data
52438** was obtained from /dev/random. It is used only as a sanity check.
52439**
52440** Since version 2.8.0, the journal format contains additional sanity
52441** checking information. If the power fails while the journal is being
52442** written, semi-random garbage data might appear in the journal
52443** file after power is restored. If an attempt is then made
52444** to roll the journal back, the database could be corrupted. The additional
52445** sanity checking data is an attempt to discover the garbage in the
52446** journal and ignore it.
52447**
52448** The sanity checking information for the new journal format consists
52449** of a 32-bit checksum on each page of data. The checksum covers both
52450** the page number and the pPager->pageSize bytes of data for the page.
52451** This cksum is initialized to a 32-bit random value that appears in the
52452** journal file right after the header. The random initializer is important,
52453** because garbage data that appears at the end of a journal is likely
52454** data that was once in other files that have now been deleted. If the
52455** garbage data came from an obsolete journal file, the checksums might
52456** be correct. But by initializing the checksum to random value which
52457** is different for every journal, we minimize that risk.
52458*/
52459static const unsigned char aJournalMagic[] = {
52460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
52461};
52462
52463/*
52464** The size of the of each page record in the journal is given by
52465** the following macro.
52466*/
52467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
52468
52469/*
52470** The journal header size for this pager. This is usually the same
52471** size as a single disk sector. See also setSectorSize().
52472*/
52473#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
52474
52475/*
52476** The macro MEMDB is true if we are dealing with an in-memory database.
52477** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
52478** the value of MEMDB will be a constant and the compiler will optimize
52479** out code that would never execute.
52480*/
52481#ifdef SQLITE_OMIT_MEMORYDB
52482# define MEMDB 0
52483#else
52484# define MEMDB pPager->memDb
52485#endif
52486
52487/*
52488** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
52489** interfaces to access the database using memory-mapped I/O.
52490*/
52491#if SQLITE_MAX_MMAP_SIZE>0
52492# define USEFETCH(x) ((x)->bUseFetch)
52493#else
52494# define USEFETCH(x) 0
52495#endif
52496
52497/*
52498** The argument to this macro is a file descriptor (type sqlite3_file*).
52499** Return 0 if it is not open, or non-zero (but not 1) if it is.
52500**
52501** This is so that expressions can be written as:
52502**
52503** if( isOpen(pPager->jfd) ){ ...
52504**
52505** instead of
52506**
52507** if( pPager->jfd->pMethods ){ ...
52508*/
52509#define isOpen(pFd) ((pFd)->pMethods!=0)
52510
52511#ifdef SQLITE_DIRECT_OVERFLOW_READ
52512/*
52513** Return true if page pgno can be read directly from the database file
52514** by the b-tree layer. This is the case if:
52515**
52516** * the database file is open,
52517** * there are no dirty pages in the cache, and
52518** * the desired page is not currently in the wal file.
52519*/
52520SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
52521 if( pPager->fd->pMethods==0 ) return 0;
52522 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
52523#ifndef SQLITE_OMIT_WAL
52524 if( pPager->pWal ){
52525 u32 iRead = 0;
52526 int rc;
52527 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
52528 return (rc==SQLITE_OK && iRead==0);
52529 }
52530#endif
52531 return 1;
52532}
52533#endif
52534
52535#ifndef SQLITE_OMIT_WAL
52536# define pagerUseWal(x) ((x)->pWal!=0)
52537#else
52538# define pagerUseWal(x) 0
52539# define pagerRollbackWal(x) 0
52540# define pagerWalFrames(v,w,x,y) 0
52541# define pagerOpenWalIfPresent(z) SQLITE_OK
52542# define pagerBeginReadTransaction(z) SQLITE_OK
52543#endif
52544
52545#ifndef NDEBUG
52546/*
52547** Usage:
52548**
52549** assert( assert_pager_state(pPager) );
52550**
52551** This function runs many asserts to try to find inconsistencies in
52552** the internal state of the Pager object.
52553*/
52554static int assert_pager_state(Pager *p){
52555 Pager *pPager = p;
52556
52557 /* State must be valid. */
52558 assert( p->eState==PAGER_OPEN
52559 || p->eState==PAGER_READER
52560 || p->eState==PAGER_WRITER_LOCKED
52561 || p->eState==PAGER_WRITER_CACHEMOD
52562 || p->eState==PAGER_WRITER_DBMOD
52563 || p->eState==PAGER_WRITER_FINISHED
52564 || p->eState==PAGER_ERROR
52565 );
52566
52567 /* Regardless of the current state, a temp-file connection always behaves
52568 ** as if it has an exclusive lock on the database file. It never updates
52569 ** the change-counter field, so the changeCountDone flag is always set.
52570 */
52571 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
52572 assert( p->tempFile==0 || pPager->changeCountDone );
52573
52574 /* If the useJournal flag is clear, the journal-mode must be "OFF".
52575 ** And if the journal-mode is "OFF", the journal file must not be open.
52576 */
52577 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
52578 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
52579
52580 /* Check that MEMDB implies noSync. And an in-memory journal. Since
52581 ** this means an in-memory pager performs no IO at all, it cannot encounter
52582 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
52583 ** a journal file. (although the in-memory journal implementation may
52584 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
52585 ** is therefore not possible for an in-memory pager to enter the ERROR
52586 ** state.
52587 */
52588 if( MEMDB ){
52589 assert( !isOpen(p->fd) );
52590 assert( p->noSync );
52591 assert( p->journalMode==PAGER_JOURNALMODE_OFF
52592 || p->journalMode==PAGER_JOURNALMODE_MEMORY
52593 );
52594 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
52595 assert( pagerUseWal(p)==0 );
52596 }
52597
52598 /* If changeCountDone is set, a RESERVED lock or greater must be held
52599 ** on the file.
52600 */
52601 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
52602 assert( p->eLock!=PENDING_LOCK );
52603
52604 switch( p->eState ){
52605 case PAGER_OPEN:
52606 assert( !MEMDB );
52607 assert( pPager->errCode==SQLITE_OK );
52608 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
52609 break;
52610
52611 case PAGER_READER:
52612 assert( pPager->errCode==SQLITE_OK );
52613 assert( p->eLock!=UNKNOWN_LOCK );
52614 assert( p->eLock>=SHARED_LOCK );
52615 break;
52616
52617 case PAGER_WRITER_LOCKED:
52618 assert( p->eLock!=UNKNOWN_LOCK );
52619 assert( pPager->errCode==SQLITE_OK );
52620 if( !pagerUseWal(pPager) ){
52621 assert( p->eLock>=RESERVED_LOCK );
52622 }
52623 assert( pPager->dbSize==pPager->dbOrigSize );
52624 assert( pPager->dbOrigSize==pPager->dbFileSize );
52625 assert( pPager->dbOrigSize==pPager->dbHintSize );
52626 assert( pPager->setSuper==0 );
52627 break;
52628
52629 case PAGER_WRITER_CACHEMOD:
52630 assert( p->eLock!=UNKNOWN_LOCK );
52631 assert( pPager->errCode==SQLITE_OK );
52632 if( !pagerUseWal(pPager) ){
52633 /* It is possible that if journal_mode=wal here that neither the
52634 ** journal file nor the WAL file are open. This happens during
52635 ** a rollback transaction that switches from journal_mode=off
52636 ** to journal_mode=wal.
52637 */
52638 assert( p->eLock>=RESERVED_LOCK );
52639 assert( isOpen(p->jfd)
52640 || p->journalMode==PAGER_JOURNALMODE_OFF
52641 || p->journalMode==PAGER_JOURNALMODE_WAL
52642 );
52643 }
52644 assert( pPager->dbOrigSize==pPager->dbFileSize );
52645 assert( pPager->dbOrigSize==pPager->dbHintSize );
52646 break;
52647
52648 case PAGER_WRITER_DBMOD:
52649 assert( p->eLock==EXCLUSIVE_LOCK );
52650 assert( pPager->errCode==SQLITE_OK );
52651 assert( !pagerUseWal(pPager) );
52652 assert( p->eLock>=EXCLUSIVE_LOCK );
52653 assert( isOpen(p->jfd)
52654 || p->journalMode==PAGER_JOURNALMODE_OFF
52655 || p->journalMode==PAGER_JOURNALMODE_WAL
52656 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52657 );
52658 assert( pPager->dbOrigSize<=pPager->dbHintSize );
52659 break;
52660
52661 case PAGER_WRITER_FINISHED:
52662 assert( p->eLock==EXCLUSIVE_LOCK );
52663 assert( pPager->errCode==SQLITE_OK );
52664 assert( !pagerUseWal(pPager) );
52665 assert( isOpen(p->jfd)
52666 || p->journalMode==PAGER_JOURNALMODE_OFF
52667 || p->journalMode==PAGER_JOURNALMODE_WAL
52668 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52669 );
52670 break;
52671
52672 case PAGER_ERROR:
52673 /* There must be at least one outstanding reference to the pager if
52674 ** in ERROR state. Otherwise the pager should have already dropped
52675 ** back to OPEN state.
52676 */
52677 assert( pPager->errCode!=SQLITE_OK );
52678 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
52679 break;
52680 }
52681
52682 return 1;
52683}
52684#endif /* ifndef NDEBUG */
52685
52686#ifdef SQLITE_DEBUG
52687/*
52688** Return a pointer to a human readable string in a static buffer
52689** containing the state of the Pager object passed as an argument. This
52690** is intended to be used within debuggers. For example, as an alternative
52691** to "print *pPager" in gdb:
52692**
52693** (gdb) printf "%s", print_pager_state(pPager)
52694**
52695** This routine has external linkage in order to suppress compiler warnings
52696** about an unused function. It is enclosed within SQLITE_DEBUG and so does
52697** not appear in normal builds.
52698*/
52699char *print_pager_state(Pager *p){
52700 static char zRet[1024];
52701
52702 sqlite3_snprintf(1024, zRet,
52703 "Filename: %s\n"
52704 "State: %s errCode=%d\n"
52705 "Lock: %s\n"
52706 "Locking mode: locking_mode=%s\n"
52707 "Journal mode: journal_mode=%s\n"
52708 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
52709 "Journal: journalOff=%lld journalHdr=%lld\n"
52710 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
52711 , p->zFilename
52712 , p->eState==PAGER_OPEN ? "OPEN" :
52713 p->eState==PAGER_READER ? "READER" :
52714 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
52715 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
52716 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
52717 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
52718 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
52719 , (int)p->errCode
52720 , p->eLock==NO_LOCK ? "NO_LOCK" :
52721 p->eLock==RESERVED_LOCK ? "RESERVED" :
52722 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
52723 p->eLock==SHARED_LOCK ? "SHARED" :
52724 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
52725 , p->exclusiveMode ? "exclusive" : "normal"
52726 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
52727 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
52728 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
52729 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
52730 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
52731 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
52732 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
52733 , p->journalOff, p->journalHdr
52734 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
52735 );
52736
52737 return zRet;
52738}
52739#endif
52740
52741/* Forward references to the various page getters */
52742static int getPageNormal(Pager*,Pgno,DbPage**,int);
52743static int getPageError(Pager*,Pgno,DbPage**,int);
52744#if SQLITE_MAX_MMAP_SIZE>0
52745static int getPageMMap(Pager*,Pgno,DbPage**,int);
52746#endif
52747
52748/*
52749** Set the Pager.xGet method for the appropriate routine used to fetch
52750** content from the pager.
52751*/
52752static void setGetterMethod(Pager *pPager){
52753 if( pPager->errCode ){
52754 pPager->xGet = getPageError;
52755#if SQLITE_MAX_MMAP_SIZE>0
52756 }else if( USEFETCH(pPager) ){
52757 pPager->xGet = getPageMMap;
52758#endif /* SQLITE_MAX_MMAP_SIZE>0 */
52759 }else{
52760 pPager->xGet = getPageNormal;
52761 }
52762}
52763
52764/*
52765** Return true if it is necessary to write page *pPg into the sub-journal.
52766** A page needs to be written into the sub-journal if there exists one
52767** or more open savepoints for which:
52768**
52769** * The page-number is less than or equal to PagerSavepoint.nOrig, and
52770** * The bit corresponding to the page-number is not set in
52771** PagerSavepoint.pInSavepoint.
52772*/
52773static int subjRequiresPage(PgHdr *pPg){
52774 Pager *pPager = pPg->pPager;
52775 PagerSavepoint *p;
52776 Pgno pgno = pPg->pgno;
52777 int i;
52778 for(i=0; i<pPager->nSavepoint; i++){
52779 p = &pPager->aSavepoint[i];
52780 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
52781 return 1;
52782 }
52783 }
52784 return 0;
52785}
52786
52787#ifdef SQLITE_DEBUG
52788/*
52789** Return true if the page is already in the journal file.
52790*/
52791static int pageInJournal(Pager *pPager, PgHdr *pPg){
52792 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
52793}
52794#endif
52795
52796/*
52797** Read a 32-bit integer from the given file descriptor. Store the integer
52798** that is read in *pRes. Return SQLITE_OK if everything worked, or an
52799** error code is something goes wrong.
52800**
52801** All values are stored on disk as big-endian.
52802*/
52803static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
52804 unsigned char ac[4];
52805 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
52806 if( rc==SQLITE_OK ){
52807 *pRes = sqlite3Get4byte(ac);
52808 }
52809 return rc;
52810}
52811
52812/*
52813** Write a 32-bit integer into a string buffer in big-endian byte order.
52814*/
52815#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
52816
52817
52818/*
52819** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
52820** on success or an error code is something goes wrong.
52821*/
52822static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
52823 char ac[4];
52824 put32bits(ac, val);
52825 return sqlite3OsWrite(fd, ac, 4, offset);
52826}
52827
52828/*
52829** Unlock the database file to level eLock, which must be either NO_LOCK
52830** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
52831** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
52832**
52833** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
52834** called, do not modify it. See the comment above the #define of
52835** UNKNOWN_LOCK for an explanation of this.
52836*/
52837static int pagerUnlockDb(Pager *pPager, int eLock){
52838 int rc = SQLITE_OK;
52839
52840 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
52841 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
52842 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
52843 if( isOpen(pPager->fd) ){
52844 assert( pPager->eLock>=eLock );
52845 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
52846 if( pPager->eLock!=UNKNOWN_LOCK ){
52847 pPager->eLock = (u8)eLock;
52848 }
52849 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
52850 }
52851 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
52852 return rc;
52853}
52854
52855/*
52856** Lock the database file to level eLock, which must be either SHARED_LOCK,
52857** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
52858** Pager.eLock variable to the new locking state.
52859**
52860** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
52861** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
52862** See the comment above the #define of UNKNOWN_LOCK for an explanation
52863** of this.
52864*/
52865static int pagerLockDb(Pager *pPager, int eLock){
52866 int rc = SQLITE_OK;
52867
52868 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
52869 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
52870 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
52871 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
52872 pPager->eLock = (u8)eLock;
52873 IOTRACE(("LOCK %p %d\n", pPager, eLock))
52874 }
52875 }
52876 return rc;
52877}
52878
52879/*
52880** This function determines whether or not the atomic-write or
52881** atomic-batch-write optimizations can be used with this pager. The
52882** atomic-write optimization can be used if:
52883**
52884** (a) the value returned by OsDeviceCharacteristics() indicates that
52885** a database page may be written atomically, and
52886** (b) the value returned by OsSectorSize() is less than or equal
52887** to the page size.
52888**
52889** If it can be used, then the value returned is the size of the journal
52890** file when it contains rollback data for exactly one page.
52891**
52892** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
52893** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
52894** returned in this case.
52895**
52896** If neither optimization can be used, 0 is returned.
52897*/
52898static int jrnlBufferSize(Pager *pPager){
52899 assert( !MEMDB );
52900
52901#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
52902 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
52903 int dc; /* Device characteristics */
52904
52905 assert( isOpen(pPager->fd) );
52906 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
52907#else
52908 UNUSED_PARAMETER(pPager);
52909#endif
52910
52911#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
52912 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
52913 return -1;
52914 }
52915#endif
52916
52917#ifdef SQLITE_ENABLE_ATOMIC_WRITE
52918 {
52919 int nSector = pPager->sectorSize;
52920 int szPage = pPager->pageSize;
52921
52922 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
52923 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
52924 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
52925 return 0;
52926 }
52927 }
52928
52929 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
52930#endif
52931
52932 return 0;
52933}
52934
52935/*
52936** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
52937** on the cache using a hash function. This is used for testing
52938** and debugging only.
52939*/
52940#ifdef SQLITE_CHECK_PAGES
52941/*
52942** Return a 32-bit hash of the page data for pPage.
52943*/
52944static u32 pager_datahash(int nByte, unsigned char *pData){
52945 u32 hash = 0;
52946 int i;
52947 for(i=0; i<nByte; i++){
52948 hash = (hash*1039) + pData[i];
52949 }
52950 return hash;
52951}
52952static u32 pager_pagehash(PgHdr *pPage){
52953 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
52954}
52955static void pager_set_pagehash(PgHdr *pPage){
52956 pPage->pageHash = pager_pagehash(pPage);
52957}
52958
52959/*
52960** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
52961** is defined, and NDEBUG is not defined, an assert() statement checks
52962** that the page is either dirty or still matches the calculated page-hash.
52963*/
52964#define CHECK_PAGE(x) checkPage(x)
52965static void checkPage(PgHdr *pPg){
52966 Pager *pPager = pPg->pPager;
52967 assert( pPager->eState!=PAGER_ERROR );
52968 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
52969}
52970
52971#else
52972#define pager_datahash(X,Y) 0
52973#define pager_pagehash(X) 0
52974#define pager_set_pagehash(X)
52975#define CHECK_PAGE(x)
52976#endif /* SQLITE_CHECK_PAGES */
52977
52978/*
52979** When this is called the journal file for pager pPager must be open.
52980** This function attempts to read a super-journal file name from the
52981** end of the file and, if successful, copies it into memory supplied
52982** by the caller. See comments above writeSuperJournal() for the format
52983** used to store a super-journal file name at the end of a journal file.
52984**
52985** zSuper must point to a buffer of at least nSuper bytes allocated by
52986** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
52987** enough space to write the super-journal name). If the super-journal
52988** name in the journal is longer than nSuper bytes (including a
52989** nul-terminator), then this is handled as if no super-journal name
52990** were present in the journal.
52991**
52992** If a super-journal file name is present at the end of the journal
52993** file, then it is copied into the buffer pointed to by zSuper. A
52994** nul-terminator byte is appended to the buffer following the
52995** super-journal file name.
52996**
52997** If it is determined that no super-journal file name is present
52998** zSuper[0] is set to 0 and SQLITE_OK returned.
52999**
53000** If an error occurs while reading from the journal file, an SQLite
53001** error code is returned.
53002*/
53003static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
53004 int rc; /* Return code */
53005 u32 len; /* Length in bytes of super-journal name */
53006 i64 szJ; /* Total size in bytes of journal file pJrnl */
53007 u32 cksum; /* MJ checksum value read from journal */
53008 u32 u; /* Unsigned loop counter */
53009 unsigned char aMagic[8]; /* A buffer to hold the magic header */
53010 zSuper[0] = '\0';
53011
53012 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
53013 || szJ<16
53014 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
53015 || len>=nSuper
53016 || len>szJ-16
53017 || len==0
53018 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
53019 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
53020 || memcmp(aMagic, aJournalMagic, 8)
53021 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
53022 ){
53023 return rc;
53024 }
53025
53026 /* See if the checksum matches the super-journal name */
53027 for(u=0; u<len; u++){
53028 cksum -= zSuper[u];
53029 }
53030 if( cksum ){
53031 /* If the checksum doesn't add up, then one or more of the disk sectors
53032 ** containing the super-journal filename is corrupted. This means
53033 ** definitely roll back, so just return SQLITE_OK and report a (nul)
53034 ** super-journal filename.
53035 */
53036 len = 0;
53037 }
53038 zSuper[len] = '\0';
53039 zSuper[len+1] = '\0';
53040
53041 return SQLITE_OK;
53042}
53043
53044/*
53045** Return the offset of the sector boundary at or immediately
53046** following the value in pPager->journalOff, assuming a sector
53047** size of pPager->sectorSize bytes.
53048**
53049** i.e for a sector size of 512:
53050**
53051** Pager.journalOff Return value
53052** ---------------------------------------
53053** 0 0
53054** 512 512
53055** 100 512
53056** 2000 2048
53057**
53058*/
53059static i64 journalHdrOffset(Pager *pPager){
53060 i64 offset = 0;
53061 i64 c = pPager->journalOff;
53062 if( c ){
53063 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
53064 }
53065 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
53066 assert( offset>=c );
53067 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
53068 return offset;
53069}
53070
53071/*
53072** The journal file must be open when this function is called.
53073**
53074** This function is a no-op if the journal file has not been written to
53075** within the current transaction (i.e. if Pager.journalOff==0).
53076**
53077** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
53078** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
53079** zero the 28-byte header at the start of the journal file. In either case,
53080** if the pager is not in no-sync mode, sync the journal file immediately
53081** after writing or truncating it.
53082**
53083** If Pager.journalSizeLimit is set to a positive, non-zero value, and
53084** following the truncation or zeroing described above the size of the
53085** journal file in bytes is larger than this value, then truncate the
53086** journal file to Pager.journalSizeLimit bytes. The journal file does
53087** not need to be synced following this operation.
53088**
53089** If an IO error occurs, abandon processing and return the IO error code.
53090** Otherwise, return SQLITE_OK.
53091*/
53092static int zeroJournalHdr(Pager *pPager, int doTruncate){
53093 int rc = SQLITE_OK; /* Return code */
53094 assert( isOpen(pPager->jfd) );
53095 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
53096 if( pPager->journalOff ){
53097 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
53098
53099 IOTRACE(("JZEROHDR %p\n", pPager))
53100 if( doTruncate || iLimit==0 ){
53101 rc = sqlite3OsTruncate(pPager->jfd, 0);
53102 }else{
53103 static const char zeroHdr[28] = {0};
53104 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
53105 }
53106 if( rc==SQLITE_OK && !pPager->noSync ){
53107 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
53108 }
53109
53110 /* At this point the transaction is committed but the write lock
53111 ** is still held on the file. If there is a size limit configured for
53112 ** the persistent journal and the journal file currently consumes more
53113 ** space than that limit allows for, truncate it now. There is no need
53114 ** to sync the file following this operation.
53115 */
53116 if( rc==SQLITE_OK && iLimit>0 ){
53117 i64 sz;
53118 rc = sqlite3OsFileSize(pPager->jfd, &sz);
53119 if( rc==SQLITE_OK && sz>iLimit ){
53120 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
53121 }
53122 }
53123 }
53124 return rc;
53125}
53126
53127/*
53128** The journal file must be open when this routine is called. A journal
53129** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
53130** current location.
53131**
53132** The format for the journal header is as follows:
53133** - 8 bytes: Magic identifying journal format.
53134** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
53135** - 4 bytes: Random number used for page hash.
53136** - 4 bytes: Initial database page count.
53137** - 4 bytes: Sector size used by the process that wrote this journal.
53138** - 4 bytes: Database page size.
53139**
53140** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
53141*/
53142static int writeJournalHdr(Pager *pPager){
53143 int rc = SQLITE_OK; /* Return code */
53144 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
53145 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
53146 u32 nWrite; /* Bytes of header sector written */
53147 int ii; /* Loop counter */
53148
53149 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
53150
53151 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
53152 nHeader = JOURNAL_HDR_SZ(pPager);
53153 }
53154
53155 /* If there are active savepoints and any of them were created
53156 ** since the most recent journal header was written, update the
53157 ** PagerSavepoint.iHdrOffset fields now.
53158 */
53159 for(ii=0; ii<pPager->nSavepoint; ii++){
53160 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
53161 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
53162 }
53163 }
53164
53165 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
53166
53167 /*
53168 ** Write the nRec Field - the number of page records that follow this
53169 ** journal header. Normally, zero is written to this value at this time.
53170 ** After the records are added to the journal (and the journal synced,
53171 ** if in full-sync mode), the zero is overwritten with the true number
53172 ** of records (see syncJournal()).
53173 **
53174 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
53175 ** reading the journal this value tells SQLite to assume that the
53176 ** rest of the journal file contains valid page records. This assumption
53177 ** is dangerous, as if a failure occurred whilst writing to the journal
53178 ** file it may contain some garbage data. There are two scenarios
53179 ** where this risk can be ignored:
53180 **
53181 ** * When the pager is in no-sync mode. Corruption can follow a
53182 ** power failure in this case anyway.
53183 **
53184 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
53185 ** that garbage data is never appended to the journal file.
53186 */
53187 assert( isOpen(pPager->fd) || pPager->noSync );
53188 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
53189 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
53190 ){
53191 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
53192 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
53193 }else{
53194 memset(zHeader, 0, sizeof(aJournalMagic)+4);
53195 }
53196
53197 /* The random check-hash initializer */
53198 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
53199 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
53200 /* The initial database size */
53201 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
53202 /* The assumed sector size for this process */
53203 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
53204
53205 /* The page size */
53206 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
53207
53208 /* Initializing the tail of the buffer is not necessary. Everything
53209 ** works find if the following memset() is omitted. But initializing
53210 ** the memory prevents valgrind from complaining, so we are willing to
53211 ** take the performance hit.
53212 */
53213 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
53214 nHeader-(sizeof(aJournalMagic)+20));
53215
53216 /* In theory, it is only necessary to write the 28 bytes that the
53217 ** journal header consumes to the journal file here. Then increment the
53218 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
53219 ** record is written to the following sector (leaving a gap in the file
53220 ** that will be implicitly filled in by the OS).
53221 **
53222 ** However it has been discovered that on some systems this pattern can
53223 ** be significantly slower than contiguously writing data to the file,
53224 ** even if that means explicitly writing data to the block of
53225 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
53226 ** is done.
53227 **
53228 ** The loop is required here in case the sector-size is larger than the
53229 ** database page size. Since the zHeader buffer is only Pager.pageSize
53230 ** bytes in size, more than one call to sqlite3OsWrite() may be required
53231 ** to populate the entire journal header sector.
53232 */
53233 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
53234 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
53235 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
53236 assert( pPager->journalHdr <= pPager->journalOff );
53237 pPager->journalOff += nHeader;
53238 }
53239
53240 return rc;
53241}
53242
53243/*
53244** The journal file must be open when this is called. A journal header file
53245** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
53246** file. The current location in the journal file is given by
53247** pPager->journalOff. See comments above function writeJournalHdr() for
53248** a description of the journal header format.
53249**
53250** If the header is read successfully, *pNRec is set to the number of
53251** page records following this header and *pDbSize is set to the size of the
53252** database before the transaction began, in pages. Also, pPager->cksumInit
53253** is set to the value read from the journal header. SQLITE_OK is returned
53254** in this case.
53255**
53256** If the journal header file appears to be corrupted, SQLITE_DONE is
53257** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
53258** cannot be read from the journal file an error code is returned.
53259*/
53260static int readJournalHdr(
53261 Pager *pPager, /* Pager object */
53262 int isHot,
53263 i64 journalSize, /* Size of the open journal file in bytes */
53264 u32 *pNRec, /* OUT: Value read from the nRec field */
53265 u32 *pDbSize /* OUT: Value of original database size field */
53266){
53267 int rc; /* Return code */
53268 unsigned char aMagic[8]; /* A buffer to hold the magic header */
53269 i64 iHdrOff; /* Offset of journal header being read */
53270
53271 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
53272
53273 /* Advance Pager.journalOff to the start of the next sector. If the
53274 ** journal file is too small for there to be a header stored at this
53275 ** point, return SQLITE_DONE.
53276 */
53277 pPager->journalOff = journalHdrOffset(pPager);
53278 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
53279 return SQLITE_DONE;
53280 }
53281 iHdrOff = pPager->journalOff;
53282
53283 /* Read in the first 8 bytes of the journal header. If they do not match
53284 ** the magic string found at the start of each journal header, return
53285 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
53286 ** proceed.
53287 */
53288 if( isHot || iHdrOff!=pPager->journalHdr ){
53289 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
53290 if( rc ){
53291 return rc;
53292 }
53293 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
53294 return SQLITE_DONE;
53295 }
53296 }
53297
53298 /* Read the first three 32-bit fields of the journal header: The nRec
53299 ** field, the checksum-initializer and the database size at the start
53300 ** of the transaction. Return an error code if anything goes wrong.
53301 */
53302 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
53303 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
53304 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
53305 ){
53306 return rc;
53307 }
53308
53309 if( pPager->journalOff==0 ){
53310 u32 iPageSize; /* Page-size field of journal header */
53311 u32 iSectorSize; /* Sector-size field of journal header */
53312
53313 /* Read the page-size and sector-size journal header fields. */
53314 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
53315 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
53316 ){
53317 return rc;
53318 }
53319
53320 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
53321 ** journal header to zero. In this case, assume that the Pager.pageSize
53322 ** variable is already set to the correct page size.
53323 */
53324 if( iPageSize==0 ){
53325 iPageSize = pPager->pageSize;
53326 }
53327
53328 /* Check that the values read from the page-size and sector-size fields
53329 ** are within range. To be 'in range', both values need to be a power
53330 ** of two greater than or equal to 512 or 32, and not greater than their
53331 ** respective compile time maximum limits.
53332 */
53333 if( iPageSize<512 || iSectorSize<32
53334 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
53335 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
53336 ){
53337 /* If the either the page-size or sector-size in the journal-header is
53338 ** invalid, then the process that wrote the journal-header must have
53339 ** crashed before the header was synced. In this case stop reading
53340 ** the journal file here.
53341 */
53342 return SQLITE_DONE;
53343 }
53344
53345 /* Update the page-size to match the value read from the journal.
53346 ** Use a testcase() macro to make sure that malloc failure within
53347 ** PagerSetPagesize() is tested.
53348 */
53349 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
53350 testcase( rc!=SQLITE_OK );
53351
53352 /* Update the assumed sector-size to match the value used by
53353 ** the process that created this journal. If this journal was
53354 ** created by a process other than this one, then this routine
53355 ** is being called from within pager_playback(). The local value
53356 ** of Pager.sectorSize is restored at the end of that routine.
53357 */
53358 pPager->sectorSize = iSectorSize;
53359 }
53360
53361 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
53362 return rc;
53363}
53364
53365
53366/*
53367** Write the supplied super-journal name into the journal file for pager
53368** pPager at the current location. The super-journal name must be the last
53369** thing written to a journal file. If the pager is in full-sync mode, the
53370** journal file descriptor is advanced to the next sector boundary before
53371** anything is written. The format is:
53372**
53373** + 4 bytes: PAGER_MJ_PGNO.
53374** + N bytes: super-journal filename in utf-8.
53375** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
53376** + 4 bytes: super-journal name checksum.
53377** + 8 bytes: aJournalMagic[].
53378**
53379** The super-journal page checksum is the sum of the bytes in thesuper-journal
53380** name, where each byte is interpreted as a signed 8-bit integer.
53381**
53382** If zSuper is a NULL pointer (occurs for a single database transaction),
53383** this call is a no-op.
53384*/
53385static int writeSuperJournal(Pager *pPager, const char *zSuper){
53386 int rc; /* Return code */
53387 int nSuper; /* Length of string zSuper */
53388 i64 iHdrOff; /* Offset of header in journal file */
53389 i64 jrnlSize; /* Size of journal file on disk */
53390 u32 cksum = 0; /* Checksum of string zSuper */
53391
53392 assert( pPager->setSuper==0 );
53393 assert( !pagerUseWal(pPager) );
53394
53395 if( !zSuper
53396 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
53397 || !isOpen(pPager->jfd)
53398 ){
53399 return SQLITE_OK;
53400 }
53401 pPager->setSuper = 1;
53402 assert( pPager->journalHdr <= pPager->journalOff );
53403
53404 /* Calculate the length in bytes and the checksum of zSuper */
53405 for(nSuper=0; zSuper[nSuper]; nSuper++){
53406 cksum += zSuper[nSuper];
53407 }
53408
53409 /* If in full-sync mode, advance to the next disk sector before writing
53410 ** the super-journal name. This is in case the previous page written to
53411 ** the journal has already been synced.
53412 */
53413 if( pPager->fullSync ){
53414 pPager->journalOff = journalHdrOffset(pPager);
53415 }
53416 iHdrOff = pPager->journalOff;
53417
53418 /* Write the super-journal data to the end of the journal file. If
53419 ** an error occurs, return the error code to the caller.
53420 */
53421 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
53422 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
53423 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
53424 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
53425 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
53426 iHdrOff+4+nSuper+8)))
53427 ){
53428 return rc;
53429 }
53430 pPager->journalOff += (nSuper+20);
53431
53432 /* If the pager is in peristent-journal mode, then the physical
53433 ** journal-file may extend past the end of the super-journal name
53434 ** and 8 bytes of magic data just written to the file. This is
53435 ** dangerous because the code to rollback a hot-journal file
53436 ** will not be able to find the super-journal name to determine
53437 ** whether or not the journal is hot.
53438 **
53439 ** Easiest thing to do in this scenario is to truncate the journal
53440 ** file to the required size.
53441 */
53442 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
53443 && jrnlSize>pPager->journalOff
53444 ){
53445 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
53446 }
53447 return rc;
53448}
53449
53450/*
53451** Discard the entire contents of the in-memory page-cache.
53452*/
53453static void pager_reset(Pager *pPager){
53454 pPager->iDataVersion++;
53455 sqlite3BackupRestart(pPager->pBackup);
53456 sqlite3PcacheClear(pPager->pPCache);
53457}
53458
53459/*
53460** Return the pPager->iDataVersion value
53461*/
53462SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
53463 return pPager->iDataVersion;
53464}
53465
53466/*
53467** Free all structures in the Pager.aSavepoint[] array and set both
53468** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
53469** if it is open and the pager is not in exclusive mode.
53470*/
53471static void releaseAllSavepoints(Pager *pPager){
53472 int ii; /* Iterator for looping through Pager.aSavepoint */
53473 for(ii=0; ii<pPager->nSavepoint; ii++){
53474 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
53475 }
53476 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
53477 sqlite3OsClose(pPager->sjfd);
53478 }
53479 sqlite3_free(pPager->aSavepoint);
53480 pPager->aSavepoint = 0;
53481 pPager->nSavepoint = 0;
53482 pPager->nSubRec = 0;
53483}
53484
53485/*
53486** Set the bit number pgno in the PagerSavepoint.pInSavepoint
53487** bitvecs of all open savepoints. Return SQLITE_OK if successful
53488** or SQLITE_NOMEM if a malloc failure occurs.
53489*/
53490static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
53491 int ii; /* Loop counter */
53492 int rc = SQLITE_OK; /* Result code */
53493
53494 for(ii=0; ii<pPager->nSavepoint; ii++){
53495 PagerSavepoint *p = &pPager->aSavepoint[ii];
53496 if( pgno<=p->nOrig ){
53497 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
53498 testcase( rc==SQLITE_NOMEM );
53499 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
53500 }
53501 }
53502 return rc;
53503}
53504
53505/*
53506** This function is a no-op if the pager is in exclusive mode and not
53507** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
53508** state.
53509**
53510** If the pager is not in exclusive-access mode, the database file is
53511** completely unlocked. If the file is unlocked and the file-system does
53512** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
53513** closed (if it is open).
53514**
53515** If the pager is in ERROR state when this function is called, the
53516** contents of the pager cache are discarded before switching back to
53517** the OPEN state. Regardless of whether the pager is in exclusive-mode
53518** or not, any journal file left in the file-system will be treated
53519** as a hot-journal and rolled back the next time a read-transaction
53520** is opened (by this or by any other connection).
53521*/
53522static void pager_unlock(Pager *pPager){
53523
53524 assert( pPager->eState==PAGER_READER
53525 || pPager->eState==PAGER_OPEN
53526 || pPager->eState==PAGER_ERROR
53527 );
53528
53529 sqlite3BitvecDestroy(pPager->pInJournal);
53530 pPager->pInJournal = 0;
53531 releaseAllSavepoints(pPager);
53532
53533 if( pagerUseWal(pPager) ){
53534 assert( !isOpen(pPager->jfd) );
53535 sqlite3WalEndReadTransaction(pPager->pWal);
53536 pPager->eState = PAGER_OPEN;
53537 }else if( !pPager->exclusiveMode ){
53538 int rc; /* Error code returned by pagerUnlockDb() */
53539 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
53540
53541 /* If the operating system support deletion of open files, then
53542 ** close the journal file when dropping the database lock. Otherwise
53543 ** another connection with journal_mode=delete might delete the file
53544 ** out from under us.
53545 */
53546 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
53547 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
53548 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
53549 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
53550 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
53551 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
53552 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
53553 || 1!=(pPager->journalMode & 5)
53554 ){
53555 sqlite3OsClose(pPager->jfd);
53556 }
53557
53558 /* If the pager is in the ERROR state and the call to unlock the database
53559 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
53560 ** above the #define for UNKNOWN_LOCK for an explanation of why this
53561 ** is necessary.
53562 */
53563 rc = pagerUnlockDb(pPager, NO_LOCK);
53564 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
53565 pPager->eLock = UNKNOWN_LOCK;
53566 }
53567
53568 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
53569 ** without clearing the error code. This is intentional - the error
53570 ** code is cleared and the cache reset in the block below.
53571 */
53572 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
53573 pPager->eState = PAGER_OPEN;
53574 }
53575
53576 /* If Pager.errCode is set, the contents of the pager cache cannot be
53577 ** trusted. Now that there are no outstanding references to the pager,
53578 ** it can safely move back to PAGER_OPEN state. This happens in both
53579 ** normal and exclusive-locking mode.
53580 */
53581 assert( pPager->errCode==SQLITE_OK || !MEMDB );
53582 if( pPager->errCode ){
53583 if( pPager->tempFile==0 ){
53584 pager_reset(pPager);
53585 pPager->changeCountDone = 0;
53586 pPager->eState = PAGER_OPEN;
53587 }else{
53588 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
53589 }
53590 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
53591 pPager->errCode = SQLITE_OK;
53592 setGetterMethod(pPager);
53593 }
53594
53595 pPager->journalOff = 0;
53596 pPager->journalHdr = 0;
53597 pPager->setSuper = 0;
53598}
53599
53600/*
53601** This function is called whenever an IOERR or FULL error that requires
53602** the pager to transition into the ERROR state may ahve occurred.
53603** The first argument is a pointer to the pager structure, the second
53604** the error-code about to be returned by a pager API function. The
53605** value returned is a copy of the second argument to this function.
53606**
53607** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
53608** IOERR sub-codes, the pager enters the ERROR state and the error code
53609** is stored in Pager.errCode. While the pager remains in the ERROR state,
53610** all major API calls on the Pager will immediately return Pager.errCode.
53611**
53612** The ERROR state indicates that the contents of the pager-cache
53613** cannot be trusted. This state can be cleared by completely discarding
53614** the contents of the pager-cache. If a transaction was active when
53615** the persistent error occurred, then the rollback journal may need
53616** to be replayed to restore the contents of the database file (as if
53617** it were a hot-journal).
53618*/
53619static int pager_error(Pager *pPager, int rc){
53620 int rc2 = rc & 0xff;
53621 assert( rc==SQLITE_OK || !MEMDB );
53622 assert(
53623 pPager->errCode==SQLITE_FULL ||
53624 pPager->errCode==SQLITE_OK ||
53625 (pPager->errCode & 0xff)==SQLITE_IOERR
53626 );
53627 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
53628 pPager->errCode = rc;
53629 pPager->eState = PAGER_ERROR;
53630 setGetterMethod(pPager);
53631 }
53632 return rc;
53633}
53634
53635static int pager_truncate(Pager *pPager, Pgno nPage);
53636
53637/*
53638** The write transaction open on pPager is being committed (bCommit==1)
53639** or rolled back (bCommit==0).
53640**
53641** Return TRUE if and only if all dirty pages should be flushed to disk.
53642**
53643** Rules:
53644**
53645** * For non-TEMP databases, always sync to disk. This is necessary
53646** for transactions to be durable.
53647**
53648** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
53649** file has been created already (via a spill on pagerStress()) and
53650** when the number of dirty pages in memory exceeds 25% of the total
53651** cache size.
53652*/
53653static int pagerFlushOnCommit(Pager *pPager, int bCommit){
53654 if( pPager->tempFile==0 ) return 1;
53655 if( !bCommit ) return 0;
53656 if( !isOpen(pPager->fd) ) return 0;
53657 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
53658}
53659
53660/*
53661** This routine ends a transaction. A transaction is usually ended by
53662** either a COMMIT or a ROLLBACK operation. This routine may be called
53663** after rollback of a hot-journal, or if an error occurs while opening
53664** the journal file or writing the very first journal-header of a
53665** database transaction.
53666**
53667** This routine is never called in PAGER_ERROR state. If it is called
53668** in PAGER_NONE or PAGER_SHARED state and the lock held is less
53669** exclusive than a RESERVED lock, it is a no-op.
53670**
53671** Otherwise, any active savepoints are released.
53672**
53673** If the journal file is open, then it is "finalized". Once a journal
53674** file has been finalized it is not possible to use it to roll back a
53675** transaction. Nor will it be considered to be a hot-journal by this
53676** or any other database connection. Exactly how a journal is finalized
53677** depends on whether or not the pager is running in exclusive mode and
53678** the current journal-mode (Pager.journalMode value), as follows:
53679**
53680** journalMode==MEMORY
53681** Journal file descriptor is simply closed. This destroys an
53682** in-memory journal.
53683**
53684** journalMode==TRUNCATE
53685** Journal file is truncated to zero bytes in size.
53686**
53687** journalMode==PERSIST
53688** The first 28 bytes of the journal file are zeroed. This invalidates
53689** the first journal header in the file, and hence the entire journal
53690** file. An invalid journal file cannot be rolled back.
53691**
53692** journalMode==DELETE
53693** The journal file is closed and deleted using sqlite3OsDelete().
53694**
53695** If the pager is running in exclusive mode, this method of finalizing
53696** the journal file is never used. Instead, if the journalMode is
53697** DELETE and the pager is in exclusive mode, the method described under
53698** journalMode==PERSIST is used instead.
53699**
53700** After the journal is finalized, the pager moves to PAGER_READER state.
53701** If running in non-exclusive rollback mode, the lock on the file is
53702** downgraded to a SHARED_LOCK.
53703**
53704** SQLITE_OK is returned if no error occurs. If an error occurs during
53705** any of the IO operations to finalize the journal file or unlock the
53706** database then the IO error code is returned to the user. If the
53707** operation to finalize the journal file fails, then the code still
53708** tries to unlock the database file if not in exclusive mode. If the
53709** unlock operation fails as well, then the first error code related
53710** to the first error encountered (the journal finalization one) is
53711** returned.
53712*/
53713static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
53714 int rc = SQLITE_OK; /* Error code from journal finalization operation */
53715 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
53716
53717 /* Do nothing if the pager does not have an open write transaction
53718 ** or at least a RESERVED lock. This function may be called when there
53719 ** is no write-transaction active but a RESERVED or greater lock is
53720 ** held under two circumstances:
53721 **
53722 ** 1. After a successful hot-journal rollback, it is called with
53723 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
53724 **
53725 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
53726 ** lock switches back to locking_mode=normal and then executes a
53727 ** read-transaction, this function is called with eState==PAGER_READER
53728 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
53729 */
53730 assert( assert_pager_state(pPager) );
53731 assert( pPager->eState!=PAGER_ERROR );
53732 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
53733 return SQLITE_OK;
53734 }
53735
53736 releaseAllSavepoints(pPager);
53737 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
53738 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53739 );
53740 if( isOpen(pPager->jfd) ){
53741 assert( !pagerUseWal(pPager) );
53742
53743 /* Finalize the journal file. */
53744 if( sqlite3JournalIsInMemory(pPager->jfd) ){
53745 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
53746 sqlite3OsClose(pPager->jfd);
53747 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
53748 if( pPager->journalOff==0 ){
53749 rc = SQLITE_OK;
53750 }else{
53751 rc = sqlite3OsTruncate(pPager->jfd, 0);
53752 if( rc==SQLITE_OK && pPager->fullSync ){
53753 /* Make sure the new file size is written into the inode right away.
53754 ** Otherwise the journal might resurrect following a power loss and
53755 ** cause the last transaction to roll back. See
53756 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
53757 */
53758 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
53759 }
53760 }
53761 pPager->journalOff = 0;
53762 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
53763 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
53764 ){
53765 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
53766 pPager->journalOff = 0;
53767 }else{
53768 /* This branch may be executed with Pager.journalMode==MEMORY if
53769 ** a hot-journal was just rolled back. In this case the journal
53770 ** file should be closed and deleted. If this connection writes to
53771 ** the database file, it will do so using an in-memory journal.
53772 */
53773 int bDelete = !pPager->tempFile;
53774 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
53775 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
53776 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
53777 || pPager->journalMode==PAGER_JOURNALMODE_WAL
53778 );
53779 sqlite3OsClose(pPager->jfd);
53780 if( bDelete ){
53781 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
53782 }
53783 }
53784 }
53785
53786#ifdef SQLITE_CHECK_PAGES
53787 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
53788 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
53789 PgHdr *p = sqlite3PagerLookup(pPager, 1);
53790 if( p ){
53791 p->pageHash = 0;
53792 sqlite3PagerUnrefNotNull(p);
53793 }
53794 }
53795#endif
53796
53797 sqlite3BitvecDestroy(pPager->pInJournal);
53798 pPager->pInJournal = 0;
53799 pPager->nRec = 0;
53800 if( rc==SQLITE_OK ){
53801 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
53802 sqlite3PcacheCleanAll(pPager->pPCache);
53803 }else{
53804 sqlite3PcacheClearWritable(pPager->pPCache);
53805 }
53806 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
53807 }
53808
53809 if( pagerUseWal(pPager) ){
53810 /* Drop the WAL write-lock, if any. Also, if the connection was in
53811 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
53812 ** lock held on the database file.
53813 */
53814 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
53815 assert( rc2==SQLITE_OK );
53816 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
53817 /* This branch is taken when committing a transaction in rollback-journal
53818 ** mode if the database file on disk is larger than the database image.
53819 ** At this point the journal has been finalized and the transaction
53820 ** successfully committed, but the EXCLUSIVE lock is still held on the
53821 ** file. So it is safe to truncate the database file to its minimum
53822 ** required size. */
53823 assert( pPager->eLock==EXCLUSIVE_LOCK );
53824 rc = pager_truncate(pPager, pPager->dbSize);
53825 }
53826
53827 if( rc==SQLITE_OK && bCommit ){
53828 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
53829 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
53830 }
53831
53832 if( !pPager->exclusiveMode
53833 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
53834 ){
53835 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
53836 }
53837 pPager->eState = PAGER_READER;
53838 pPager->setSuper = 0;
53839
53840 return (rc==SQLITE_OK?rc2:rc);
53841}
53842
53843/*
53844** Execute a rollback if a transaction is active and unlock the
53845** database file.
53846**
53847** If the pager has already entered the ERROR state, do not attempt
53848** the rollback at this time. Instead, pager_unlock() is called. The
53849** call to pager_unlock() will discard all in-memory pages, unlock
53850** the database file and move the pager back to OPEN state. If this
53851** means that there is a hot-journal left in the file-system, the next
53852** connection to obtain a shared lock on the pager (which may be this one)
53853** will roll it back.
53854**
53855** If the pager has not already entered the ERROR state, but an IO or
53856** malloc error occurs during a rollback, then this will itself cause
53857** the pager to enter the ERROR state. Which will be cleared by the
53858** call to pager_unlock(), as described above.
53859*/
53860static void pagerUnlockAndRollback(Pager *pPager){
53861 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
53862 assert( assert_pager_state(pPager) );
53863 if( pPager->eState>=PAGER_WRITER_LOCKED ){
53864 sqlite3BeginBenignMalloc();
53865 sqlite3PagerRollback(pPager);
53866 sqlite3EndBenignMalloc();
53867 }else if( !pPager->exclusiveMode ){
53868 assert( pPager->eState==PAGER_READER );
53869 pager_end_transaction(pPager, 0, 0);
53870 }
53871 }
53872 pager_unlock(pPager);
53873}
53874
53875/*
53876** Parameter aData must point to a buffer of pPager->pageSize bytes
53877** of data. Compute and return a checksum based ont the contents of the
53878** page of data and the current value of pPager->cksumInit.
53879**
53880** This is not a real checksum. It is really just the sum of the
53881** random initial value (pPager->cksumInit) and every 200th byte
53882** of the page data, starting with byte offset (pPager->pageSize%200).
53883** Each byte is interpreted as an 8-bit unsigned integer.
53884**
53885** Changing the formula used to compute this checksum results in an
53886** incompatible journal file format.
53887**
53888** If journal corruption occurs due to a power failure, the most likely
53889** scenario is that one end or the other of the record will be changed.
53890** It is much less likely that the two ends of the journal record will be
53891** correct and the middle be corrupt. Thus, this "checksum" scheme,
53892** though fast and simple, catches the mostly likely kind of corruption.
53893*/
53894static u32 pager_cksum(Pager *pPager, const u8 *aData){
53895 u32 cksum = pPager->cksumInit; /* Checksum value to return */
53896 int i = pPager->pageSize-200; /* Loop counter */
53897 while( i>0 ){
53898 cksum += aData[i];
53899 i -= 200;
53900 }
53901 return cksum;
53902}
53903
53904/*
53905** Read a single page from either the journal file (if isMainJrnl==1) or
53906** from the sub-journal (if isMainJrnl==0) and playback that page.
53907** The page begins at offset *pOffset into the file. The *pOffset
53908** value is increased to the start of the next page in the journal.
53909**
53910** The main rollback journal uses checksums - the statement journal does
53911** not.
53912**
53913** If the page number of the page record read from the (sub-)journal file
53914** is greater than the current value of Pager.dbSize, then playback is
53915** skipped and SQLITE_OK is returned.
53916**
53917** If pDone is not NULL, then it is a record of pages that have already
53918** been played back. If the page at *pOffset has already been played back
53919** (if the corresponding pDone bit is set) then skip the playback.
53920** Make sure the pDone bit corresponding to the *pOffset page is set
53921** prior to returning.
53922**
53923** If the page record is successfully read from the (sub-)journal file
53924** and played back, then SQLITE_OK is returned. If an IO error occurs
53925** while reading the record from the (sub-)journal file or while writing
53926** to the database file, then the IO error code is returned. If data
53927** is successfully read from the (sub-)journal file but appears to be
53928** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
53929** two circumstances:
53930**
53931** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
53932** * If the record is being rolled back from the main journal file
53933** and the checksum field does not match the record content.
53934**
53935** Neither of these two scenarios are possible during a savepoint rollback.
53936**
53937** If this is a savepoint rollback, then memory may have to be dynamically
53938** allocated by this function. If this is the case and an allocation fails,
53939** SQLITE_NOMEM is returned.
53940*/
53941static int pager_playback_one_page(
53942 Pager *pPager, /* The pager being played back */
53943 i64 *pOffset, /* Offset of record to playback */
53944 Bitvec *pDone, /* Bitvec of pages already played back */
53945 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
53946 int isSavepnt /* True for a savepoint rollback */
53947){
53948 int rc;
53949 PgHdr *pPg; /* An existing page in the cache */
53950 Pgno pgno; /* The page number of a page in journal */
53951 u32 cksum; /* Checksum used for sanity checking */
53952 char *aData; /* Temporary storage for the page */
53953 sqlite3_file *jfd; /* The file descriptor for the journal file */
53954 int isSynced; /* True if journal page is synced */
53955
53956 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
53957 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
53958 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
53959 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
53960
53961 aData = pPager->pTmpSpace;
53962 assert( aData ); /* Temp storage must have already been allocated */
53963 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
53964
53965 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
53966 ** or savepoint rollback done at the request of the caller) or this is
53967 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
53968 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
53969 ** only reads from the main journal, not the sub-journal.
53970 */
53971 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
53972 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
53973 );
53974 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
53975
53976 /* Read the page number and page data from the journal or sub-journal
53977 ** file. Return an error code to the caller if an IO error occurs.
53978 */
53979 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
53980 rc = read32bits(jfd, *pOffset, &pgno);
53981 if( rc!=SQLITE_OK ) return rc;
53982 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
53983 if( rc!=SQLITE_OK ) return rc;
53984 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
53985
53986 /* Sanity checking on the page. This is more important that I originally
53987 ** thought. If a power failure occurs while the journal is being written,
53988 ** it could cause invalid data to be written into the journal. We need to
53989 ** detect this invalid data (with high probability) and ignore it.
53990 */
53991 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
53992 assert( !isSavepnt );
53993 return SQLITE_DONE;
53994 }
53995 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
53996 return SQLITE_OK;
53997 }
53998 if( isMainJrnl ){
53999 rc = read32bits(jfd, (*pOffset)-4, &cksum);
54000 if( rc ) return rc;
54001 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
54002 return SQLITE_DONE;
54003 }
54004 }
54005
54006 /* If this page has already been played back before during the current
54007 ** rollback, then don't bother to play it back again.
54008 */
54009 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
54010 return rc;
54011 }
54012
54013 /* When playing back page 1, restore the nReserve setting
54014 */
54015 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
54016 pPager->nReserve = ((u8*)aData)[20];
54017 }
54018
54019 /* If the pager is in CACHEMOD state, then there must be a copy of this
54020 ** page in the pager cache. In this case just update the pager cache,
54021 ** not the database file. The page is left marked dirty in this case.
54022 **
54023 ** An exception to the above rule: If the database is in no-sync mode
54024 ** and a page is moved during an incremental vacuum then the page may
54025 ** not be in the pager cache. Later: if a malloc() or IO error occurs
54026 ** during a Movepage() call, then the page may not be in the cache
54027 ** either. So the condition described in the above paragraph is not
54028 ** assert()able.
54029 **
54030 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
54031 ** pager cache if it exists and the main file. The page is then marked
54032 ** not dirty. Since this code is only executed in PAGER_OPEN state for
54033 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
54034 ** if the pager is in OPEN state.
54035 **
54036 ** Ticket #1171: The statement journal might contain page content that is
54037 ** different from the page content at the start of the transaction.
54038 ** This occurs when a page is changed prior to the start of a statement
54039 ** then changed again within the statement. When rolling back such a
54040 ** statement we must not write to the original database unless we know
54041 ** for certain that original page contents are synced into the main rollback
54042 ** journal. Otherwise, a power loss might leave modified data in the
54043 ** database file without an entry in the rollback journal that can
54044 ** restore the database to its original form. Two conditions must be
54045 ** met before writing to the database files. (1) the database must be
54046 ** locked. (2) we know that the original page content is fully synced
54047 ** in the main journal either because the page is not in cache or else
54048 ** the page is marked as needSync==0.
54049 **
54050 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
54051 ** is possible to fail a statement on a database that does not yet exist.
54052 ** Do not attempt to write if database file has never been opened.
54053 */
54054 if( pagerUseWal(pPager) ){
54055 pPg = 0;
54056 }else{
54057 pPg = sqlite3PagerLookup(pPager, pgno);
54058 }
54059 assert( pPg || !MEMDB );
54060 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
54061 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
54062 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
54063 (isMainJrnl?"main-journal":"sub-journal")
54064 ));
54065 if( isMainJrnl ){
54066 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
54067 }else{
54068 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
54069 }
54070 if( isOpen(pPager->fd)
54071 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54072 && isSynced
54073 ){
54074 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
54075 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
54076 assert( !pagerUseWal(pPager) );
54077
54078 /* Write the data read from the journal back into the database file.
54079 ** This is usually safe even for an encrypted database - as the data
54080 ** was encrypted before it was written to the journal file. The exception
54081 ** is if the data was just read from an in-memory sub-journal. In that
54082 ** case it must be encrypted here before it is copied into the database
54083 ** file. */
54084 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
54085
54086 if( pgno>pPager->dbFileSize ){
54087 pPager->dbFileSize = pgno;
54088 }
54089 if( pPager->pBackup ){
54090 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
54091 }
54092 }else if( !isMainJrnl && pPg==0 ){
54093 /* If this is a rollback of a savepoint and data was not written to
54094 ** the database and the page is not in-memory, there is a potential
54095 ** problem. When the page is next fetched by the b-tree layer, it
54096 ** will be read from the database file, which may or may not be
54097 ** current.
54098 **
54099 ** There are a couple of different ways this can happen. All are quite
54100 ** obscure. When running in synchronous mode, this can only happen
54101 ** if the page is on the free-list at the start of the transaction, then
54102 ** populated, then moved using sqlite3PagerMovepage().
54103 **
54104 ** The solution is to add an in-memory page to the cache containing
54105 ** the data just read from the sub-journal. Mark the page as dirty
54106 ** and if the pager requires a journal-sync, then mark the page as
54107 ** requiring a journal-sync before it is written.
54108 */
54109 assert( isSavepnt );
54110 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
54111 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
54112 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
54113 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
54114 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
54115 if( rc!=SQLITE_OK ) return rc;
54116 sqlite3PcacheMakeDirty(pPg);
54117 }
54118 if( pPg ){
54119 /* No page should ever be explicitly rolled back that is in use, except
54120 ** for page 1 which is held in use in order to keep the lock on the
54121 ** database active. However such a page may be rolled back as a result
54122 ** of an internal error resulting in an automatic call to
54123 ** sqlite3PagerRollback().
54124 */
54125 void *pData;
54126 pData = pPg->pData;
54127 memcpy(pData, (u8*)aData, pPager->pageSize);
54128 pPager->xReiniter(pPg);
54129 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
54130 ** that call was dangerous and had no detectable benefit since the cache
54131 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
54132 ** has been removed. */
54133 pager_set_pagehash(pPg);
54134
54135 /* If this was page 1, then restore the value of Pager.dbFileVers.
54136 ** Do this before any decoding. */
54137 if( pgno==1 ){
54138 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
54139 }
54140 sqlite3PcacheRelease(pPg);
54141 }
54142 return rc;
54143}
54144
54145/*
54146** Parameter zSuper is the name of a super-journal file. A single journal
54147** file that referred to the super-journal file has just been rolled back.
54148** This routine checks if it is possible to delete the super-journal file,
54149** and does so if it is.
54150**
54151** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
54152** available for use within this function.
54153**
54154** When a super-journal file is created, it is populated with the names
54155** of all of its child journals, one after another, formatted as utf-8
54156** encoded text. The end of each child journal file is marked with a
54157** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
54158** file for a transaction involving two databases might be:
54159**
54160** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
54161**
54162** A super-journal file may only be deleted once all of its child
54163** journals have been rolled back.
54164**
54165** This function reads the contents of the super-journal file into
54166** memory and loops through each of the child journal names. For
54167** each child journal, it checks if:
54168**
54169** * if the child journal exists, and if so
54170** * if the child journal contains a reference to super-journal
54171** file zSuper
54172**
54173** If a child journal can be found that matches both of the criteria
54174** above, this function returns without doing anything. Otherwise, if
54175** no such child journal can be found, file zSuper is deleted from
54176** the file-system using sqlite3OsDelete().
54177**
54178** If an IO error within this function, an error code is returned. This
54179** function allocates memory by calling sqlite3Malloc(). If an allocation
54180** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
54181** occur, SQLITE_OK is returned.
54182**
54183** TODO: This function allocates a single block of memory to load
54184** the entire contents of the super-journal file. This could be
54185** a couple of kilobytes or so - potentially larger than the page
54186** size.
54187*/
54188static int pager_delsuper(Pager *pPager, const char *zSuper){
54189 sqlite3_vfs *pVfs = pPager->pVfs;
54190 int rc; /* Return code */
54191 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
54192 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
54193 char *zSuperJournal = 0; /* Contents of super-journal file */
54194 i64 nSuperJournal; /* Size of super-journal file */
54195 char *zJournal; /* Pointer to one journal within MJ file */
54196 char *zSuperPtr; /* Space to hold super-journal filename */
54197 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
54198
54199 /* Allocate space for both the pJournal and pSuper file descriptors.
54200 ** If successful, open the super-journal file for reading.
54201 */
54202 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
54203 if( !pSuper ){
54204 rc = SQLITE_NOMEM_BKPT;
54205 pJournal = 0;
54206 }else{
54207 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
54208 rc = sqlite3OsOpen(pVfs, zSuper, pSuper, flags, 0);
54209 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
54210 }
54211 if( rc!=SQLITE_OK ) goto delsuper_out;
54212
54213 /* Load the entire super-journal file into space obtained from
54214 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
54215 ** sufficient space (in zSuperPtr) to hold the names of super-journal
54216 ** files extracted from regular rollback-journals.
54217 */
54218 rc = sqlite3OsFileSize(pSuper, &nSuperJournal);
54219 if( rc!=SQLITE_OK ) goto delsuper_out;
54220 nSuperPtr = pVfs->mxPathname+1;
54221 zSuperJournal = sqlite3Malloc(nSuperJournal + nSuperPtr + 2);
54222 if( !zSuperJournal ){
54223 rc = SQLITE_NOMEM_BKPT;
54224 goto delsuper_out;
54225 }
54226 zSuperPtr = &zSuperJournal[nSuperJournal+2];
54227 rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0);
54228 if( rc!=SQLITE_OK ) goto delsuper_out;
54229 zSuperJournal[nSuperJournal] = 0;
54230 zSuperJournal[nSuperJournal+1] = 0;
54231
54232 zJournal = zSuperJournal;
54233 while( (zJournal-zSuperJournal)<nSuperJournal ){
54234 int exists;
54235 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
54236 if( rc!=SQLITE_OK ){
54237 goto delsuper_out;
54238 }
54239 if( exists ){
54240 /* One of the journals pointed to by the super-journal exists.
54241 ** Open it and check if it points at the super-journal. If
54242 ** so, return without deleting the super-journal file.
54243 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
54244 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
54245 ** name into sqlite3_database_file_object().
54246 */
54247 int c;
54248 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL);
54249 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
54250 if( rc!=SQLITE_OK ){
54251 goto delsuper_out;
54252 }
54253
54254 rc = readSuperJournal(pJournal, zSuperPtr, nSuperPtr);
54255 sqlite3OsClose(pJournal);
54256 if( rc!=SQLITE_OK ){
54257 goto delsuper_out;
54258 }
54259
54260 c = zSuperPtr[0]!=0 && strcmp(zSuperPtr, zSuper)==0;
54261 if( c ){
54262 /* We have a match. Do not delete the super-journal file. */
54263 goto delsuper_out;
54264 }
54265 }
54266 zJournal += (sqlite3Strlen30(zJournal)+1);
54267 }
54268
54269 sqlite3OsClose(pSuper);
54270 rc = sqlite3OsDelete(pVfs, zSuper, 0);
54271
54272delsuper_out:
54273 sqlite3_free(zSuperJournal);
54274 if( pSuper ){
54275 sqlite3OsClose(pSuper);
54276 assert( !isOpen(pJournal) );
54277 sqlite3_free(pSuper);
54278 }
54279 return rc;
54280}
54281
54282
54283/*
54284** This function is used to change the actual size of the database
54285** file in the file-system. This only happens when committing a transaction,
54286** or rolling back a transaction (including rolling back a hot-journal).
54287**
54288** If the main database file is not open, or the pager is not in either
54289** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
54290** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
54291** If the file on disk is currently larger than nPage pages, then use the VFS
54292** xTruncate() method to truncate it.
54293**
54294** Or, it might be the case that the file on disk is smaller than
54295** nPage pages. Some operating system implementations can get confused if
54296** you try to truncate a file to some size that is larger than it
54297** currently is, so detect this case and write a single zero byte to
54298** the end of the new file instead.
54299**
54300** If successful, return SQLITE_OK. If an IO error occurs while modifying
54301** the database file, return the error code to the caller.
54302*/
54303static int pager_truncate(Pager *pPager, Pgno nPage){
54304 int rc = SQLITE_OK;
54305 assert( pPager->eState!=PAGER_ERROR );
54306 assert( pPager->eState!=PAGER_READER );
54307
54308 if( isOpen(pPager->fd)
54309 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54310 ){
54311 i64 currentSize, newSize;
54312 int szPage = pPager->pageSize;
54313 assert( pPager->eLock==EXCLUSIVE_LOCK );
54314 /* TODO: Is it safe to use Pager.dbFileSize here? */
54315 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
54316 newSize = szPage*(i64)nPage;
54317 if( rc==SQLITE_OK && currentSize!=newSize ){
54318 if( currentSize>newSize ){
54319 rc = sqlite3OsTruncate(pPager->fd, newSize);
54320 }else if( (currentSize+szPage)<=newSize ){
54321 char *pTmp = pPager->pTmpSpace;
54322 memset(pTmp, 0, szPage);
54323 testcase( (newSize-szPage) == currentSize );
54324 testcase( (newSize-szPage) > currentSize );
54325 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
54326 }
54327 if( rc==SQLITE_OK ){
54328 pPager->dbFileSize = nPage;
54329 }
54330 }
54331 }
54332 return rc;
54333}
54334
54335/*
54336** Return a sanitized version of the sector-size of OS file pFile. The
54337** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
54338*/
54339SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
54340 int iRet = sqlite3OsSectorSize(pFile);
54341 if( iRet<32 ){
54342 iRet = 512;
54343 }else if( iRet>MAX_SECTOR_SIZE ){
54344 assert( MAX_SECTOR_SIZE>=512 );
54345 iRet = MAX_SECTOR_SIZE;
54346 }
54347 return iRet;
54348}
54349
54350/*
54351** Set the value of the Pager.sectorSize variable for the given
54352** pager based on the value returned by the xSectorSize method
54353** of the open database file. The sector size will be used
54354** to determine the size and alignment of journal header and
54355** super-journal pointers within created journal files.
54356**
54357** For temporary files the effective sector size is always 512 bytes.
54358**
54359** Otherwise, for non-temporary files, the effective sector size is
54360** the value returned by the xSectorSize() method rounded up to 32 if
54361** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
54362** is greater than MAX_SECTOR_SIZE.
54363**
54364** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
54365** the effective sector size to its minimum value (512). The purpose of
54366** pPager->sectorSize is to define the "blast radius" of bytes that
54367** might change if a crash occurs while writing to a single byte in
54368** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
54369** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
54370** size. For backwards compatibility of the rollback journal file format,
54371** we cannot reduce the effective sector size below 512.
54372*/
54373static void setSectorSize(Pager *pPager){
54374 assert( isOpen(pPager->fd) || pPager->tempFile );
54375
54376 if( pPager->tempFile
54377 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
54378 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
54379 ){
54380 /* Sector size doesn't matter for temporary files. Also, the file
54381 ** may not have been opened yet, in which case the OsSectorSize()
54382 ** call will segfault. */
54383 pPager->sectorSize = 512;
54384 }else{
54385 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
54386 }
54387}
54388
54389/*
54390** Playback the journal and thus restore the database file to
54391** the state it was in before we started making changes.
54392**
54393** The journal file format is as follows:
54394**
54395** (1) 8 byte prefix. A copy of aJournalMagic[].
54396** (2) 4 byte big-endian integer which is the number of valid page records
54397** in the journal. If this value is 0xffffffff, then compute the
54398** number of page records from the journal size.
54399** (3) 4 byte big-endian integer which is the initial value for the
54400** sanity checksum.
54401** (4) 4 byte integer which is the number of pages to truncate the
54402** database to during a rollback.
54403** (5) 4 byte big-endian integer which is the sector size. The header
54404** is this many bytes in size.
54405** (6) 4 byte big-endian integer which is the page size.
54406** (7) zero padding out to the next sector size.
54407** (8) Zero or more pages instances, each as follows:
54408** + 4 byte page number.
54409** + pPager->pageSize bytes of data.
54410** + 4 byte checksum
54411**
54412** When we speak of the journal header, we mean the first 7 items above.
54413** Each entry in the journal is an instance of the 8th item.
54414**
54415** Call the value from the second bullet "nRec". nRec is the number of
54416** valid page entries in the journal. In most cases, you can compute the
54417** value of nRec from the size of the journal file. But if a power
54418** failure occurred while the journal was being written, it could be the
54419** case that the size of the journal file had already been increased but
54420** the extra entries had not yet made it safely to disk. In such a case,
54421** the value of nRec computed from the file size would be too large. For
54422** that reason, we always use the nRec value in the header.
54423**
54424** If the nRec value is 0xffffffff it means that nRec should be computed
54425** from the file size. This value is used when the user selects the
54426** no-sync option for the journal. A power failure could lead to corruption
54427** in this case. But for things like temporary table (which will be
54428** deleted when the power is restored) we don't care.
54429**
54430** If the file opened as the journal file is not a well-formed
54431** journal file then all pages up to the first corrupted page are rolled
54432** back (or no pages if the journal header is corrupted). The journal file
54433** is then deleted and SQLITE_OK returned, just as if no corruption had
54434** been encountered.
54435**
54436** If an I/O or malloc() error occurs, the journal-file is not deleted
54437** and an error code is returned.
54438**
54439** The isHot parameter indicates that we are trying to rollback a journal
54440** that might be a hot journal. Or, it could be that the journal is
54441** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
54442** If the journal really is hot, reset the pager cache prior rolling
54443** back any content. If the journal is merely persistent, no reset is
54444** needed.
54445*/
54446static int pager_playback(Pager *pPager, int isHot){
54447 sqlite3_vfs *pVfs = pPager->pVfs;
54448 i64 szJ; /* Size of the journal file in bytes */
54449 u32 nRec; /* Number of Records in the journal */
54450 u32 u; /* Unsigned loop counter */
54451 Pgno mxPg = 0; /* Size of the original file in pages */
54452 int rc; /* Result code of a subroutine */
54453 int res = 1; /* Value returned by sqlite3OsAccess() */
54454 char *zSuper = 0; /* Name of super-journal file if any */
54455 int needPagerReset; /* True to reset page prior to first page rollback */
54456 int nPlayback = 0; /* Total number of pages restored from journal */
54457 u32 savedPageSize = pPager->pageSize;
54458
54459 /* Figure out how many records are in the journal. Abort early if
54460 ** the journal is empty.
54461 */
54462 assert( isOpen(pPager->jfd) );
54463 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
54464 if( rc!=SQLITE_OK ){
54465 goto end_playback;
54466 }
54467
54468 /* Read the super-journal name from the journal, if it is present.
54469 ** If a super-journal file name is specified, but the file is not
54470 ** present on disk, then the journal is not hot and does not need to be
54471 ** played back.
54472 **
54473 ** TODO: Technically the following is an error because it assumes that
54474 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
54475 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
54476 ** mxPathname is 512, which is the same as the minimum allowable value
54477 ** for pageSize.
54478 */
54479 zSuper = pPager->pTmpSpace;
54480 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
54481 if( rc==SQLITE_OK && zSuper[0] ){
54482 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
54483 }
54484 zSuper = 0;
54485 if( rc!=SQLITE_OK || !res ){
54486 goto end_playback;
54487 }
54488 pPager->journalOff = 0;
54489 needPagerReset = isHot;
54490
54491 /* This loop terminates either when a readJournalHdr() or
54492 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
54493 ** occurs.
54494 */
54495 while( 1 ){
54496 /* Read the next journal header from the journal file. If there are
54497 ** not enough bytes left in the journal file for a complete header, or
54498 ** it is corrupted, then a process must have failed while writing it.
54499 ** This indicates nothing more needs to be rolled back.
54500 */
54501 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
54502 if( rc!=SQLITE_OK ){
54503 if( rc==SQLITE_DONE ){
54504 rc = SQLITE_OK;
54505 }
54506 goto end_playback;
54507 }
54508
54509 /* If nRec is 0xffffffff, then this journal was created by a process
54510 ** working in no-sync mode. This means that the rest of the journal
54511 ** file consists of pages, there are no more journal headers. Compute
54512 ** the value of nRec based on this assumption.
54513 */
54514 if( nRec==0xffffffff ){
54515 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
54516 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
54517 }
54518
54519 /* If nRec is 0 and this rollback is of a transaction created by this
54520 ** process and if this is the final header in the journal, then it means
54521 ** that this part of the journal was being filled but has not yet been
54522 ** synced to disk. Compute the number of pages based on the remaining
54523 ** size of the file.
54524 **
54525 ** The third term of the test was added to fix ticket #2565.
54526 ** When rolling back a hot journal, nRec==0 always means that the next
54527 ** chunk of the journal contains zero pages to be rolled back. But
54528 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
54529 ** the journal, it means that the journal might contain additional
54530 ** pages that need to be rolled back and that the number of pages
54531 ** should be computed based on the journal file size.
54532 */
54533 if( nRec==0 && !isHot &&
54534 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
54535 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
54536 }
54537
54538 /* If this is the first header read from the journal, truncate the
54539 ** database file back to its original size.
54540 */
54541 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
54542 rc = pager_truncate(pPager, mxPg);
54543 if( rc!=SQLITE_OK ){
54544 goto end_playback;
54545 }
54546 pPager->dbSize = mxPg;
54547 }
54548
54549 /* Copy original pages out of the journal and back into the
54550 ** database file and/or page cache.
54551 */
54552 for(u=0; u<nRec; u++){
54553 if( needPagerReset ){
54554 pager_reset(pPager);
54555 needPagerReset = 0;
54556 }
54557 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
54558 if( rc==SQLITE_OK ){
54559 nPlayback++;
54560 }else{
54561 if( rc==SQLITE_DONE ){
54562 pPager->journalOff = szJ;
54563 break;
54564 }else if( rc==SQLITE_IOERR_SHORT_READ ){
54565 /* If the journal has been truncated, simply stop reading and
54566 ** processing the journal. This might happen if the journal was
54567 ** not completely written and synced prior to a crash. In that
54568 ** case, the database should have never been written in the
54569 ** first place so it is OK to simply abandon the rollback. */
54570 rc = SQLITE_OK;
54571 goto end_playback;
54572 }else{
54573 /* If we are unable to rollback, quit and return the error
54574 ** code. This will cause the pager to enter the error state
54575 ** so that no further harm will be done. Perhaps the next
54576 ** process to come along will be able to rollback the database.
54577 */
54578 goto end_playback;
54579 }
54580 }
54581 }
54582 }
54583 /*NOTREACHED*/
54584 assert( 0 );
54585
54586end_playback:
54587 if( rc==SQLITE_OK ){
54588 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
54589 }
54590 /* Following a rollback, the database file should be back in its original
54591 ** state prior to the start of the transaction, so invoke the
54592 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
54593 ** assertion that the transaction counter was modified.
54594 */
54595#ifdef SQLITE_DEBUG
54596 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
54597#endif
54598
54599 /* If this playback is happening automatically as a result of an IO or
54600 ** malloc error that occurred after the change-counter was updated but
54601 ** before the transaction was committed, then the change-counter
54602 ** modification may just have been reverted. If this happens in exclusive
54603 ** mode, then subsequent transactions performed by the connection will not
54604 ** update the change-counter at all. This may lead to cache inconsistency
54605 ** problems for other processes at some point in the future. So, just
54606 ** in case this has happened, clear the changeCountDone flag now.
54607 */
54608 pPager->changeCountDone = pPager->tempFile;
54609
54610 if( rc==SQLITE_OK ){
54611 zSuper = pPager->pTmpSpace;
54612 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
54613 testcase( rc!=SQLITE_OK );
54614 }
54615 if( rc==SQLITE_OK
54616 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54617 ){
54618 rc = sqlite3PagerSync(pPager, 0);
54619 }
54620 if( rc==SQLITE_OK ){
54621 rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0);
54622 testcase( rc!=SQLITE_OK );
54623 }
54624 if( rc==SQLITE_OK && zSuper[0] && res ){
54625 /* If there was a super-journal and this routine will return success,
54626 ** see if it is possible to delete the super-journal.
54627 */
54628 rc = pager_delsuper(pPager, zSuper);
54629 testcase( rc!=SQLITE_OK );
54630 }
54631 if( isHot && nPlayback ){
54632 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
54633 nPlayback, pPager->zJournal);
54634 }
54635
54636 /* The Pager.sectorSize variable may have been updated while rolling
54637 ** back a journal created by a process with a different sector size
54638 ** value. Reset it to the correct value for this process.
54639 */
54640 setSectorSize(pPager);
54641 return rc;
54642}
54643
54644
54645/*
54646** Read the content for page pPg out of the database file (or out of
54647** the WAL if that is where the most recent copy if found) into
54648** pPg->pData. A shared lock or greater must be held on the database
54649** file before this function is called.
54650**
54651** If page 1 is read, then the value of Pager.dbFileVers[] is set to
54652** the value read from the database file.
54653**
54654** If an IO error occurs, then the IO error is returned to the caller.
54655** Otherwise, SQLITE_OK is returned.
54656*/
54657static int readDbPage(PgHdr *pPg){
54658 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
54659 int rc = SQLITE_OK; /* Return code */
54660
54661#ifndef SQLITE_OMIT_WAL
54662 u32 iFrame = 0; /* Frame of WAL containing pgno */
54663
54664 assert( pPager->eState>=PAGER_READER && !MEMDB );
54665 assert( isOpen(pPager->fd) );
54666
54667 if( pagerUseWal(pPager) ){
54668 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
54669 if( rc ) return rc;
54670 }
54671 if( iFrame ){
54672 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
54673 }else
54674#endif
54675 {
54676 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
54677 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
54678 if( rc==SQLITE_IOERR_SHORT_READ ){
54679 rc = SQLITE_OK;
54680 }
54681 }
54682
54683 if( pPg->pgno==1 ){
54684 if( rc ){
54685 /* If the read is unsuccessful, set the dbFileVers[] to something
54686 ** that will never be a valid file version. dbFileVers[] is a copy
54687 ** of bytes 24..39 of the database. Bytes 28..31 should always be
54688 ** zero or the size of the database in page. Bytes 32..35 and 35..39
54689 ** should be page numbers which are never 0xffffffff. So filling
54690 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
54691 **
54692 ** For an encrypted database, the situation is more complex: bytes
54693 ** 24..39 of the database are white noise. But the probability of
54694 ** white noise equaling 16 bytes of 0xff is vanishingly small so
54695 ** we should still be ok.
54696 */
54697 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
54698 }else{
54699 u8 *dbFileVers = &((u8*)pPg->pData)[24];
54700 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
54701 }
54702 }
54703 PAGER_INCR(sqlite3_pager_readdb_count);
54704 PAGER_INCR(pPager->nRead);
54705 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
54706 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
54707 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
54708
54709 return rc;
54710}
54711
54712/*
54713** Update the value of the change-counter at offsets 24 and 92 in
54714** the header and the sqlite version number at offset 96.
54715**
54716** This is an unconditional update. See also the pager_incr_changecounter()
54717** routine which only updates the change-counter if the update is actually
54718** needed, as determined by the pPager->changeCountDone state variable.
54719*/
54720static void pager_write_changecounter(PgHdr *pPg){
54721 u32 change_counter;
54722
54723 /* Increment the value just read and write it back to byte 24. */
54724 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
54725 put32bits(((char*)pPg->pData)+24, change_counter);
54726
54727 /* Also store the SQLite version number in bytes 96..99 and in
54728 ** bytes 92..95 store the change counter for which the version number
54729 ** is valid. */
54730 put32bits(((char*)pPg->pData)+92, change_counter);
54731 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
54732}
54733
54734#ifndef SQLITE_OMIT_WAL
54735/*
54736** This function is invoked once for each page that has already been
54737** written into the log file when a WAL transaction is rolled back.
54738** Parameter iPg is the page number of said page. The pCtx argument
54739** is actually a pointer to the Pager structure.
54740**
54741** If page iPg is present in the cache, and has no outstanding references,
54742** it is discarded. Otherwise, if there are one or more outstanding
54743** references, the page content is reloaded from the database. If the
54744** attempt to reload content from the database is required and fails,
54745** return an SQLite error code. Otherwise, SQLITE_OK.
54746*/
54747static int pagerUndoCallback(void *pCtx, Pgno iPg){
54748 int rc = SQLITE_OK;
54749 Pager *pPager = (Pager *)pCtx;
54750 PgHdr *pPg;
54751
54752 assert( pagerUseWal(pPager) );
54753 pPg = sqlite3PagerLookup(pPager, iPg);
54754 if( pPg ){
54755 if( sqlite3PcachePageRefcount(pPg)==1 ){
54756 sqlite3PcacheDrop(pPg);
54757 }else{
54758 rc = readDbPage(pPg);
54759 if( rc==SQLITE_OK ){
54760 pPager->xReiniter(pPg);
54761 }
54762 sqlite3PagerUnrefNotNull(pPg);
54763 }
54764 }
54765
54766 /* Normally, if a transaction is rolled back, any backup processes are
54767 ** updated as data is copied out of the rollback journal and into the
54768 ** database. This is not generally possible with a WAL database, as
54769 ** rollback involves simply truncating the log file. Therefore, if one
54770 ** or more frames have already been written to the log (and therefore
54771 ** also copied into the backup databases) as part of this transaction,
54772 ** the backups must be restarted.
54773 */
54774 sqlite3BackupRestart(pPager->pBackup);
54775
54776 return rc;
54777}
54778
54779/*
54780** This function is called to rollback a transaction on a WAL database.
54781*/
54782static int pagerRollbackWal(Pager *pPager){
54783 int rc; /* Return Code */
54784 PgHdr *pList; /* List of dirty pages to revert */
54785
54786 /* For all pages in the cache that are currently dirty or have already
54787 ** been written (but not committed) to the log file, do one of the
54788 ** following:
54789 **
54790 ** + Discard the cached page (if refcount==0), or
54791 ** + Reload page content from the database (if refcount>0).
54792 */
54793 pPager->dbSize = pPager->dbOrigSize;
54794 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
54795 pList = sqlite3PcacheDirtyList(pPager->pPCache);
54796 while( pList && rc==SQLITE_OK ){
54797 PgHdr *pNext = pList->pDirty;
54798 rc = pagerUndoCallback((void *)pPager, pList->pgno);
54799 pList = pNext;
54800 }
54801
54802 return rc;
54803}
54804
54805/*
54806** This function is a wrapper around sqlite3WalFrames(). As well as logging
54807** the contents of the list of pages headed by pList (connected by pDirty),
54808** this function notifies any active backup processes that the pages have
54809** changed.
54810**
54811** The list of pages passed into this routine is always sorted by page number.
54812** Hence, if page 1 appears anywhere on the list, it will be the first page.
54813*/
54814static int pagerWalFrames(
54815 Pager *pPager, /* Pager object */
54816 PgHdr *pList, /* List of frames to log */
54817 Pgno nTruncate, /* Database size after this commit */
54818 int isCommit /* True if this is a commit */
54819){
54820 int rc; /* Return code */
54821 int nList; /* Number of pages in pList */
54822 PgHdr *p; /* For looping over pages */
54823
54824 assert( pPager->pWal );
54825 assert( pList );
54826#ifdef SQLITE_DEBUG
54827 /* Verify that the page list is in accending order */
54828 for(p=pList; p && p->pDirty; p=p->pDirty){
54829 assert( p->pgno < p->pDirty->pgno );
54830 }
54831#endif
54832
54833 assert( pList->pDirty==0 || isCommit );
54834 if( isCommit ){
54835 /* If a WAL transaction is being committed, there is no point in writing
54836 ** any pages with page numbers greater than nTruncate into the WAL file.
54837 ** They will never be read by any client. So remove them from the pDirty
54838 ** list here. */
54839 PgHdr **ppNext = &pList;
54840 nList = 0;
54841 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
54842 if( p->pgno<=nTruncate ){
54843 ppNext = &p->pDirty;
54844 nList++;
54845 }
54846 }
54847 assert( pList );
54848 }else{
54849 nList = 1;
54850 }
54851 pPager->aStat[PAGER_STAT_WRITE] += nList;
54852
54853 if( pList->pgno==1 ) pager_write_changecounter(pList);
54854 rc = sqlite3WalFrames(pPager->pWal,
54855 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
54856 );
54857 if( rc==SQLITE_OK && pPager->pBackup ){
54858 for(p=pList; p; p=p->pDirty){
54859 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
54860 }
54861 }
54862
54863#ifdef SQLITE_CHECK_PAGES
54864 pList = sqlite3PcacheDirtyList(pPager->pPCache);
54865 for(p=pList; p; p=p->pDirty){
54866 pager_set_pagehash(p);
54867 }
54868#endif
54869
54870 return rc;
54871}
54872
54873/*
54874** Begin a read transaction on the WAL.
54875**
54876** This routine used to be called "pagerOpenSnapshot()" because it essentially
54877** makes a snapshot of the database at the current point in time and preserves
54878** that snapshot for use by the reader in spite of concurrently changes by
54879** other writers or checkpointers.
54880*/
54881static int pagerBeginReadTransaction(Pager *pPager){
54882 int rc; /* Return code */
54883 int changed = 0; /* True if cache must be reset */
54884
54885 assert( pagerUseWal(pPager) );
54886 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
54887
54888 /* sqlite3WalEndReadTransaction() was not called for the previous
54889 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
54890 ** are in locking_mode=NORMAL and EndRead() was previously called,
54891 ** the duplicate call is harmless.
54892 */
54893 sqlite3WalEndReadTransaction(pPager->pWal);
54894
54895 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
54896 if( rc!=SQLITE_OK || changed ){
54897 pager_reset(pPager);
54898 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
54899 }
54900
54901 return rc;
54902}
54903#endif
54904
54905/*
54906** This function is called as part of the transition from PAGER_OPEN
54907** to PAGER_READER state to determine the size of the database file
54908** in pages (assuming the page size currently stored in Pager.pageSize).
54909**
54910** If no error occurs, SQLITE_OK is returned and the size of the database
54911** in pages is stored in *pnPage. Otherwise, an error code (perhaps
54912** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
54913*/
54914static int pagerPagecount(Pager *pPager, Pgno *pnPage){
54915 Pgno nPage; /* Value to return via *pnPage */
54916
54917 /* Query the WAL sub-system for the database size. The WalDbsize()
54918 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
54919 ** if the database size is not available. The database size is not
54920 ** available from the WAL sub-system if the log file is empty or
54921 ** contains no valid committed transactions.
54922 */
54923 assert( pPager->eState==PAGER_OPEN );
54924 assert( pPager->eLock>=SHARED_LOCK );
54925 assert( isOpen(pPager->fd) );
54926 assert( pPager->tempFile==0 );
54927 nPage = sqlite3WalDbsize(pPager->pWal);
54928
54929 /* If the number of pages in the database is not available from the
54930 ** WAL sub-system, determine the page count based on the size of
54931 ** the database file. If the size of the database file is not an
54932 ** integer multiple of the page-size, round up the result.
54933 */
54934 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
54935 i64 n = 0; /* Size of db file in bytes */
54936 int rc = sqlite3OsFileSize(pPager->fd, &n);
54937 if( rc!=SQLITE_OK ){
54938 return rc;
54939 }
54940 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
54941 }
54942
54943 /* If the current number of pages in the file is greater than the
54944 ** configured maximum pager number, increase the allowed limit so
54945 ** that the file can be read.
54946 */
54947 if( nPage>pPager->mxPgno ){
54948 pPager->mxPgno = (Pgno)nPage;
54949 }
54950
54951 *pnPage = nPage;
54952 return SQLITE_OK;
54953}
54954
54955#ifndef SQLITE_OMIT_WAL
54956/*
54957** Check if the *-wal file that corresponds to the database opened by pPager
54958** exists if the database is not empy, or verify that the *-wal file does
54959** not exist (by deleting it) if the database file is empty.
54960**
54961** If the database is not empty and the *-wal file exists, open the pager
54962** in WAL mode. If the database is empty or if no *-wal file exists and
54963** if no error occurs, make sure Pager.journalMode is not set to
54964** PAGER_JOURNALMODE_WAL.
54965**
54966** Return SQLITE_OK or an error code.
54967**
54968** The caller must hold a SHARED lock on the database file to call this
54969** function. Because an EXCLUSIVE lock on the db file is required to delete
54970** a WAL on a none-empty database, this ensures there is no race condition
54971** between the xAccess() below and an xDelete() being executed by some
54972** other connection.
54973*/
54974static int pagerOpenWalIfPresent(Pager *pPager){
54975 int rc = SQLITE_OK;
54976 assert( pPager->eState==PAGER_OPEN );
54977 assert( pPager->eLock>=SHARED_LOCK );
54978
54979 if( !pPager->tempFile ){
54980 int isWal; /* True if WAL file exists */
54981 rc = sqlite3OsAccess(
54982 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
54983 );
54984 if( rc==SQLITE_OK ){
54985 if( isWal ){
54986 Pgno nPage; /* Size of the database file */
54987
54988 rc = pagerPagecount(pPager, &nPage);
54989 if( rc ) return rc;
54990 if( nPage==0 ){
54991 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
54992 }else{
54993 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
54994 rc = sqlite3PagerOpenWal(pPager, 0);
54995 }
54996 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
54997 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
54998 }
54999 }
55000 }
55001 return rc;
55002}
55003#endif
55004
55005/*
55006** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
55007** the entire super-journal file. The case pSavepoint==NULL occurs when
55008** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
55009** savepoint.
55010**
55011** When pSavepoint is not NULL (meaning a non-transaction savepoint is
55012** being rolled back), then the rollback consists of up to three stages,
55013** performed in the order specified:
55014**
55015** * Pages are played back from the main journal starting at byte
55016** offset PagerSavepoint.iOffset and continuing to
55017** PagerSavepoint.iHdrOffset, or to the end of the main journal
55018** file if PagerSavepoint.iHdrOffset is zero.
55019**
55020** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
55021** back starting from the journal header immediately following
55022** PagerSavepoint.iHdrOffset to the end of the main journal file.
55023**
55024** * Pages are then played back from the sub-journal file, starting
55025** with the PagerSavepoint.iSubRec and continuing to the end of
55026** the journal file.
55027**
55028** Throughout the rollback process, each time a page is rolled back, the
55029** corresponding bit is set in a bitvec structure (variable pDone in the
55030** implementation below). This is used to ensure that a page is only
55031** rolled back the first time it is encountered in either journal.
55032**
55033** If pSavepoint is NULL, then pages are only played back from the main
55034** journal file. There is no need for a bitvec in this case.
55035**
55036** In either case, before playback commences the Pager.dbSize variable
55037** is reset to the value that it held at the start of the savepoint
55038** (or transaction). No page with a page-number greater than this value
55039** is played back. If one is encountered it is simply skipped.
55040*/
55041static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
55042 i64 szJ; /* Effective size of the main journal */
55043 i64 iHdrOff; /* End of first segment of main-journal records */
55044 int rc = SQLITE_OK; /* Return code */
55045 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
55046
55047 assert( pPager->eState!=PAGER_ERROR );
55048 assert( pPager->eState>=PAGER_WRITER_LOCKED );
55049
55050 /* Allocate a bitvec to use to store the set of pages rolled back */
55051 if( pSavepoint ){
55052 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
55053 if( !pDone ){
55054 return SQLITE_NOMEM_BKPT;
55055 }
55056 }
55057
55058 /* Set the database size back to the value it was before the savepoint
55059 ** being reverted was opened.
55060 */
55061 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
55062 pPager->changeCountDone = pPager->tempFile;
55063
55064 if( !pSavepoint && pagerUseWal(pPager) ){
55065 return pagerRollbackWal(pPager);
55066 }
55067
55068 /* Use pPager->journalOff as the effective size of the main rollback
55069 ** journal. The actual file might be larger than this in
55070 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
55071 ** past pPager->journalOff is off-limits to us.
55072 */
55073 szJ = pPager->journalOff;
55074 assert( pagerUseWal(pPager)==0 || szJ==0 );
55075
55076 /* Begin by rolling back records from the main journal starting at
55077 ** PagerSavepoint.iOffset and continuing to the next journal header.
55078 ** There might be records in the main journal that have a page number
55079 ** greater than the current database size (pPager->dbSize) but those
55080 ** will be skipped automatically. Pages are added to pDone as they
55081 ** are played back.
55082 */
55083 if( pSavepoint && !pagerUseWal(pPager) ){
55084 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
55085 pPager->journalOff = pSavepoint->iOffset;
55086 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
55087 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55088 }
55089 assert( rc!=SQLITE_DONE );
55090 }else{
55091 pPager->journalOff = 0;
55092 }
55093
55094 /* Continue rolling back records out of the main journal starting at
55095 ** the first journal header seen and continuing until the effective end
55096 ** of the main journal file. Continue to skip out-of-range pages and
55097 ** continue adding pages rolled back to pDone.
55098 */
55099 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
55100 u32 ii; /* Loop counter */
55101 u32 nJRec = 0; /* Number of Journal Records */
55102 u32 dummy;
55103 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
55104 assert( rc!=SQLITE_DONE );
55105
55106 /*
55107 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
55108 ** test is related to ticket #2565. See the discussion in the
55109 ** pager_playback() function for additional information.
55110 */
55111 if( nJRec==0
55112 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
55113 ){
55114 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
55115 }
55116 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
55117 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55118 }
55119 assert( rc!=SQLITE_DONE );
55120 }
55121 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
55122
55123 /* Finally, rollback pages from the sub-journal. Page that were
55124 ** previously rolled back out of the main journal (and are hence in pDone)
55125 ** will be skipped. Out-of-range pages are also skipped.
55126 */
55127 if( pSavepoint ){
55128 u32 ii; /* Loop counter */
55129 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
55130
55131 if( pagerUseWal(pPager) ){
55132 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
55133 }
55134 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
55135 assert( offset==(i64)ii*(4+pPager->pageSize) );
55136 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
55137 }
55138 assert( rc!=SQLITE_DONE );
55139 }
55140
55141 sqlite3BitvecDestroy(pDone);
55142 if( rc==SQLITE_OK ){
55143 pPager->journalOff = szJ;
55144 }
55145
55146 return rc;
55147}
55148
55149/*
55150** Change the maximum number of in-memory pages that are allowed
55151** before attempting to recycle clean and unused pages.
55152*/
55153SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
55154 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
55155}
55156
55157/*
55158** Change the maximum number of in-memory pages that are allowed
55159** before attempting to spill pages to journal.
55160*/
55161SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
55162 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
55163}
55164
55165/*
55166** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
55167*/
55168static void pagerFixMaplimit(Pager *pPager){
55169#if SQLITE_MAX_MMAP_SIZE>0
55170 sqlite3_file *fd = pPager->fd;
55171 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
55172 sqlite3_int64 sz;
55173 sz = pPager->szMmap;
55174 pPager->bUseFetch = (sz>0);
55175 setGetterMethod(pPager);
55176 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
55177 }
55178#endif
55179}
55180
55181/*
55182** Change the maximum size of any memory mapping made of the database file.
55183*/
55184SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
55185 pPager->szMmap = szMmap;
55186 pagerFixMaplimit(pPager);
55187}
55188
55189/*
55190** Free as much memory as possible from the pager.
55191*/
55192SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
55193 sqlite3PcacheShrink(pPager->pPCache);
55194}
55195
55196/*
55197** Adjust settings of the pager to those specified in the pgFlags parameter.
55198**
55199** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
55200** of the database to damage due to OS crashes or power failures by
55201** changing the number of syncs()s when writing the journals.
55202** There are four levels:
55203**
55204** OFF sqlite3OsSync() is never called. This is the default
55205** for temporary and transient files.
55206**
55207** NORMAL The journal is synced once before writes begin on the
55208** database. This is normally adequate protection, but
55209** it is theoretically possible, though very unlikely,
55210** that an inopertune power failure could leave the journal
55211** in a state which would cause damage to the database
55212** when it is rolled back.
55213**
55214** FULL The journal is synced twice before writes begin on the
55215** database (with some additional information - the nRec field
55216** of the journal header - being written in between the two
55217** syncs). If we assume that writing a
55218** single disk sector is atomic, then this mode provides
55219** assurance that the journal will not be corrupted to the
55220** point of causing damage to the database during rollback.
55221**
55222** EXTRA This is like FULL except that is also syncs the directory
55223** that contains the rollback journal after the rollback
55224** journal is unlinked.
55225**
55226** The above is for a rollback-journal mode. For WAL mode, OFF continues
55227** to mean that no syncs ever occur. NORMAL means that the WAL is synced
55228** prior to the start of checkpoint and that the database file is synced
55229** at the conclusion of the checkpoint if the entire content of the WAL
55230** was written back into the database. But no sync operations occur for
55231** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
55232** file is synced following each commit operation, in addition to the
55233** syncs associated with NORMAL. There is no difference between FULL
55234** and EXTRA for WAL mode.
55235**
55236** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
55237** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
55238** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
55239** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
55240** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
55241** synchronous=FULL versus synchronous=NORMAL setting determines when
55242** the xSync primitive is called and is relevant to all platforms.
55243**
55244** Numeric values associated with these states are OFF==1, NORMAL=2,
55245** and FULL=3.
55246*/
55247#ifndef SQLITE_OMIT_PAGER_PRAGMAS
55248SQLITE_PRIVATE void sqlite3PagerSetFlags(
55249 Pager *pPager, /* The pager to set safety level for */
55250 unsigned pgFlags /* Various flags */
55251){
55252 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
55253 if( pPager->tempFile ){
55254 pPager->noSync = 1;
55255 pPager->fullSync = 0;
55256 pPager->extraSync = 0;
55257 }else{
55258 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
55259 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
55260 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
55261 }
55262 if( pPager->noSync ){
55263 pPager->syncFlags = 0;
55264 }else if( pgFlags & PAGER_FULLFSYNC ){
55265 pPager->syncFlags = SQLITE_SYNC_FULL;
55266 }else{
55267 pPager->syncFlags = SQLITE_SYNC_NORMAL;
55268 }
55269 pPager->walSyncFlags = (pPager->syncFlags<<2);
55270 if( pPager->fullSync ){
55271 pPager->walSyncFlags |= pPager->syncFlags;
55272 }
55273 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
55274 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
55275 }
55276 if( pgFlags & PAGER_CACHESPILL ){
55277 pPager->doNotSpill &= ~SPILLFLAG_OFF;
55278 }else{
55279 pPager->doNotSpill |= SPILLFLAG_OFF;
55280 }
55281}
55282#endif
55283
55284/*
55285** The following global variable is incremented whenever the library
55286** attempts to open a temporary file. This information is used for
55287** testing and analysis only.
55288*/
55289#ifdef SQLITE_TEST
55290SQLITE_API int sqlite3_opentemp_count = 0;
55291#endif
55292
55293/*
55294** Open a temporary file.
55295**
55296** Write the file descriptor into *pFile. Return SQLITE_OK on success
55297** or some other error code if we fail. The OS will automatically
55298** delete the temporary file when it is closed.
55299**
55300** The flags passed to the VFS layer xOpen() call are those specified
55301** by parameter vfsFlags ORed with the following:
55302**
55303** SQLITE_OPEN_READWRITE
55304** SQLITE_OPEN_CREATE
55305** SQLITE_OPEN_EXCLUSIVE
55306** SQLITE_OPEN_DELETEONCLOSE
55307*/
55308static int pagerOpentemp(
55309 Pager *pPager, /* The pager object */
55310 sqlite3_file *pFile, /* Write the file descriptor here */
55311 int vfsFlags /* Flags passed through to the VFS */
55312){
55313 int rc; /* Return code */
55314
55315#ifdef SQLITE_TEST
55316 sqlite3_opentemp_count++; /* Used for testing and analysis only */
55317#endif
55318
55319 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
55320 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
55321 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
55322 assert( rc!=SQLITE_OK || isOpen(pFile) );
55323 return rc;
55324}
55325
55326/*
55327** Set the busy handler function.
55328**
55329** The pager invokes the busy-handler if sqlite3OsLock() returns
55330** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
55331** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
55332** lock. It does *not* invoke the busy handler when upgrading from
55333** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
55334** (which occurs during hot-journal rollback). Summary:
55335**
55336** Transition | Invokes xBusyHandler
55337** --------------------------------------------------------
55338** NO_LOCK -> SHARED_LOCK | Yes
55339** SHARED_LOCK -> RESERVED_LOCK | No
55340** SHARED_LOCK -> EXCLUSIVE_LOCK | No
55341** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
55342**
55343** If the busy-handler callback returns non-zero, the lock is
55344** retried. If it returns zero, then the SQLITE_BUSY error is
55345** returned to the caller of the pager API function.
55346*/
55347SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
55348 Pager *pPager, /* Pager object */
55349 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
55350 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
55351){
55352 void **ap;
55353 pPager->xBusyHandler = xBusyHandler;
55354 pPager->pBusyHandlerArg = pBusyHandlerArg;
55355 ap = (void **)&pPager->xBusyHandler;
55356 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
55357 assert( ap[1]==pBusyHandlerArg );
55358 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
55359}
55360
55361/*
55362** Change the page size used by the Pager object. The new page size
55363** is passed in *pPageSize.
55364**
55365** If the pager is in the error state when this function is called, it
55366** is a no-op. The value returned is the error state error code (i.e.
55367** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
55368**
55369** Otherwise, if all of the following are true:
55370**
55371** * the new page size (value of *pPageSize) is valid (a power
55372** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
55373**
55374** * there are no outstanding page references, and
55375**
55376** * the database is either not an in-memory database or it is
55377** an in-memory database that currently consists of zero pages.
55378**
55379** then the pager object page size is set to *pPageSize.
55380**
55381** If the page size is changed, then this function uses sqlite3PagerMalloc()
55382** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
55383** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
55384** In all other cases, SQLITE_OK is returned.
55385**
55386** If the page size is not changed, either because one of the enumerated
55387** conditions above is not true, the pager was in error state when this
55388** function was called, or because the memory allocation attempt failed,
55389** then *pPageSize is set to the old, retained page size before returning.
55390*/
55391SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
55392 int rc = SQLITE_OK;
55393
55394 /* It is not possible to do a full assert_pager_state() here, as this
55395 ** function may be called from within PagerOpen(), before the state
55396 ** of the Pager object is internally consistent.
55397 **
55398 ** At one point this function returned an error if the pager was in
55399 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
55400 ** there is at least one outstanding page reference, this function
55401 ** is a no-op for that case anyhow.
55402 */
55403
55404 u32 pageSize = *pPageSize;
55405 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
55406 if( (pPager->memDb==0 || pPager->dbSize==0)
55407 && sqlite3PcacheRefCount(pPager->pPCache)==0
55408 && pageSize && pageSize!=(u32)pPager->pageSize
55409 ){
55410 char *pNew = NULL; /* New temp space */
55411 i64 nByte = 0;
55412
55413 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
55414 rc = sqlite3OsFileSize(pPager->fd, &nByte);
55415 }
55416 if( rc==SQLITE_OK ){
55417 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
55418 * cell header parser will never run off the end of the allocation */
55419 pNew = (char *)sqlite3PageMalloc(pageSize+8);
55420 if( !pNew ){
55421 rc = SQLITE_NOMEM_BKPT;
55422 }else{
55423 memset(pNew+pageSize, 0, 8);
55424 }
55425 }
55426
55427 if( rc==SQLITE_OK ){
55428 pager_reset(pPager);
55429 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
55430 }
55431 if( rc==SQLITE_OK ){
55432 sqlite3PageFree(pPager->pTmpSpace);
55433 pPager->pTmpSpace = pNew;
55434 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
55435 pPager->pageSize = pageSize;
55436 }else{
55437 sqlite3PageFree(pNew);
55438 }
55439 }
55440
55441 *pPageSize = pPager->pageSize;
55442 if( rc==SQLITE_OK ){
55443 if( nReserve<0 ) nReserve = pPager->nReserve;
55444 assert( nReserve>=0 && nReserve<1000 );
55445 pPager->nReserve = (i16)nReserve;
55446 pagerFixMaplimit(pPager);
55447 }
55448 return rc;
55449}
55450
55451/*
55452** Return a pointer to the "temporary page" buffer held internally
55453** by the pager. This is a buffer that is big enough to hold the
55454** entire content of a database page. This buffer is used internally
55455** during rollback and will be overwritten whenever a rollback
55456** occurs. But other modules are free to use it too, as long as
55457** no rollbacks are happening.
55458*/
55459SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
55460 return pPager->pTmpSpace;
55461}
55462
55463/*
55464** Attempt to set the maximum database page count if mxPage is positive.
55465** Make no changes if mxPage is zero or negative. And never reduce the
55466** maximum page count below the current size of the database.
55467**
55468** Regardless of mxPage, return the current maximum page count.
55469*/
55470SQLITE_PRIVATE Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
55471 if( mxPage>0 ){
55472 pPager->mxPgno = mxPage;
55473 }
55474 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
55475 /* assert( pPager->mxPgno>=pPager->dbSize ); */
55476 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
55477 ** less than the total number of valid pages in the database. But this
55478 ** may be less than Pager.dbSize, and so the assert() above is not valid */
55479 return pPager->mxPgno;
55480}
55481
55482/*
55483** The following set of routines are used to disable the simulated
55484** I/O error mechanism. These routines are used to avoid simulated
55485** errors in places where we do not care about errors.
55486**
55487** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
55488** and generate no code.
55489*/
55490#ifdef SQLITE_TEST
55491SQLITE_API extern int sqlite3_io_error_pending;
55492SQLITE_API extern int sqlite3_io_error_hit;
55493static int saved_cnt;
55494void disable_simulated_io_errors(void){
55495 saved_cnt = sqlite3_io_error_pending;
55496 sqlite3_io_error_pending = -1;
55497}
55498void enable_simulated_io_errors(void){
55499 sqlite3_io_error_pending = saved_cnt;
55500}
55501#else
55502# define disable_simulated_io_errors()
55503# define enable_simulated_io_errors()
55504#endif
55505
55506/*
55507** Read the first N bytes from the beginning of the file into memory
55508** that pDest points to.
55509**
55510** If the pager was opened on a transient file (zFilename==""), or
55511** opened on a file less than N bytes in size, the output buffer is
55512** zeroed and SQLITE_OK returned. The rationale for this is that this
55513** function is used to read database headers, and a new transient or
55514** zero sized database has a header than consists entirely of zeroes.
55515**
55516** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
55517** the error code is returned to the caller and the contents of the
55518** output buffer undefined.
55519*/
55520SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
55521 int rc = SQLITE_OK;
55522 memset(pDest, 0, N);
55523 assert( isOpen(pPager->fd) || pPager->tempFile );
55524
55525 /* This routine is only called by btree immediately after creating
55526 ** the Pager object. There has not been an opportunity to transition
55527 ** to WAL mode yet.
55528 */
55529 assert( !pagerUseWal(pPager) );
55530
55531 if( isOpen(pPager->fd) ){
55532 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
55533 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
55534 if( rc==SQLITE_IOERR_SHORT_READ ){
55535 rc = SQLITE_OK;
55536 }
55537 }
55538 return rc;
55539}
55540
55541/*
55542** This function may only be called when a read-transaction is open on
55543** the pager. It returns the total number of pages in the database.
55544**
55545** However, if the file is between 1 and <page-size> bytes in size, then
55546** this is considered a 1 page file.
55547*/
55548SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
55549 assert( pPager->eState>=PAGER_READER );
55550 assert( pPager->eState!=PAGER_WRITER_FINISHED );
55551 *pnPage = (int)pPager->dbSize;
55552}
55553
55554
55555/*
55556** Try to obtain a lock of type locktype on the database file. If
55557** a similar or greater lock is already held, this function is a no-op
55558** (returning SQLITE_OK immediately).
55559**
55560** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
55561** the busy callback if the lock is currently not available. Repeat
55562** until the busy callback returns false or until the attempt to
55563** obtain the lock succeeds.
55564**
55565** Return SQLITE_OK on success and an error code if we cannot obtain
55566** the lock. If the lock is obtained successfully, set the Pager.state
55567** variable to locktype before returning.
55568*/
55569static int pager_wait_on_lock(Pager *pPager, int locktype){
55570 int rc; /* Return code */
55571
55572 /* Check that this is either a no-op (because the requested lock is
55573 ** already held), or one of the transitions that the busy-handler
55574 ** may be invoked during, according to the comment above
55575 ** sqlite3PagerSetBusyhandler().
55576 */
55577 assert( (pPager->eLock>=locktype)
55578 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
55579 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
55580 );
55581
55582 do {
55583 rc = pagerLockDb(pPager, locktype);
55584 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
55585 return rc;
55586}
55587
55588/*
55589** Function assertTruncateConstraint(pPager) checks that one of the
55590** following is true for all dirty pages currently in the page-cache:
55591**
55592** a) The page number is less than or equal to the size of the
55593** current database image, in pages, OR
55594**
55595** b) if the page content were written at this time, it would not
55596** be necessary to write the current content out to the sub-journal
55597** (as determined by function subjRequiresPage()).
55598**
55599** If the condition asserted by this function were not true, and the
55600** dirty page were to be discarded from the cache via the pagerStress()
55601** routine, pagerStress() would not write the current page content to
55602** the database file. If a savepoint transaction were rolled back after
55603** this happened, the correct behavior would be to restore the current
55604** content of the page. However, since this content is not present in either
55605** the database file or the portion of the rollback journal and
55606** sub-journal rolled back the content could not be restored and the
55607** database image would become corrupt. It is therefore fortunate that
55608** this circumstance cannot arise.
55609*/
55610#if defined(SQLITE_DEBUG)
55611static void assertTruncateConstraintCb(PgHdr *pPg){
55612 assert( pPg->flags&PGHDR_DIRTY );
55613 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
55614}
55615static void assertTruncateConstraint(Pager *pPager){
55616 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
55617}
55618#else
55619# define assertTruncateConstraint(pPager)
55620#endif
55621
55622/*
55623** Truncate the in-memory database file image to nPage pages. This
55624** function does not actually modify the database file on disk. It
55625** just sets the internal state of the pager object so that the
55626** truncation will be done when the current transaction is committed.
55627**
55628** This function is only called right before committing a transaction.
55629** Once this function has been called, the transaction must either be
55630** rolled back or committed. It is not safe to call this function and
55631** then continue writing to the database.
55632*/
55633SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
55634 assert( pPager->dbSize>=nPage );
55635 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
55636 pPager->dbSize = nPage;
55637
55638 /* At one point the code here called assertTruncateConstraint() to
55639 ** ensure that all pages being truncated away by this operation are,
55640 ** if one or more savepoints are open, present in the savepoint
55641 ** journal so that they can be restored if the savepoint is rolled
55642 ** back. This is no longer necessary as this function is now only
55643 ** called right before committing a transaction. So although the
55644 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
55645 ** they cannot be rolled back. So the assertTruncateConstraint() call
55646 ** is no longer correct. */
55647}
55648
55649
55650/*
55651** This function is called before attempting a hot-journal rollback. It
55652** syncs the journal file to disk, then sets pPager->journalHdr to the
55653** size of the journal file so that the pager_playback() routine knows
55654** that the entire journal file has been synced.
55655**
55656** Syncing a hot-journal to disk before attempting to roll it back ensures
55657** that if a power-failure occurs during the rollback, the process that
55658** attempts rollback following system recovery sees the same journal
55659** content as this process.
55660**
55661** If everything goes as planned, SQLITE_OK is returned. Otherwise,
55662** an SQLite error code.
55663*/
55664static int pagerSyncHotJournal(Pager *pPager){
55665 int rc = SQLITE_OK;
55666 if( !pPager->noSync ){
55667 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
55668 }
55669 if( rc==SQLITE_OK ){
55670 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
55671 }
55672 return rc;
55673}
55674
55675#if SQLITE_MAX_MMAP_SIZE>0
55676/*
55677** Obtain a reference to a memory mapped page object for page number pgno.
55678** The new object will use the pointer pData, obtained from xFetch().
55679** If successful, set *ppPage to point to the new page reference
55680** and return SQLITE_OK. Otherwise, return an SQLite error code and set
55681** *ppPage to zero.
55682**
55683** Page references obtained by calling this function should be released
55684** by calling pagerReleaseMapPage().
55685*/
55686static int pagerAcquireMapPage(
55687 Pager *pPager, /* Pager object */
55688 Pgno pgno, /* Page number */
55689 void *pData, /* xFetch()'d data for this page */
55690 PgHdr **ppPage /* OUT: Acquired page object */
55691){
55692 PgHdr *p; /* Memory mapped page to return */
55693
55694 if( pPager->pMmapFreelist ){
55695 *ppPage = p = pPager->pMmapFreelist;
55696 pPager->pMmapFreelist = p->pDirty;
55697 p->pDirty = 0;
55698 assert( pPager->nExtra>=8 );
55699 memset(p->pExtra, 0, 8);
55700 }else{
55701 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
55702 if( p==0 ){
55703 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
55704 return SQLITE_NOMEM_BKPT;
55705 }
55706 p->pExtra = (void *)&p[1];
55707 p->flags = PGHDR_MMAP;
55708 p->nRef = 1;
55709 p->pPager = pPager;
55710 }
55711
55712 assert( p->pExtra==(void *)&p[1] );
55713 assert( p->pPage==0 );
55714 assert( p->flags==PGHDR_MMAP );
55715 assert( p->pPager==pPager );
55716 assert( p->nRef==1 );
55717
55718 p->pgno = pgno;
55719 p->pData = pData;
55720 pPager->nMmapOut++;
55721
55722 return SQLITE_OK;
55723}
55724#endif
55725
55726/*
55727** Release a reference to page pPg. pPg must have been returned by an
55728** earlier call to pagerAcquireMapPage().
55729*/
55730static void pagerReleaseMapPage(PgHdr *pPg){
55731 Pager *pPager = pPg->pPager;
55732 pPager->nMmapOut--;
55733 pPg->pDirty = pPager->pMmapFreelist;
55734 pPager->pMmapFreelist = pPg;
55735
55736 assert( pPager->fd->pMethods->iVersion>=3 );
55737 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
55738}
55739
55740/*
55741** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
55742*/
55743static void pagerFreeMapHdrs(Pager *pPager){
55744 PgHdr *p;
55745 PgHdr *pNext;
55746 for(p=pPager->pMmapFreelist; p; p=pNext){
55747 pNext = p->pDirty;
55748 sqlite3_free(p);
55749 }
55750}
55751
55752/* Verify that the database file has not be deleted or renamed out from
55753** under the pager. Return SQLITE_OK if the database is still where it ought
55754** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
55755** code from sqlite3OsAccess()) if the database has gone missing.
55756*/
55757static int databaseIsUnmoved(Pager *pPager){
55758 int bHasMoved = 0;
55759 int rc;
55760
55761 if( pPager->tempFile ) return SQLITE_OK;
55762 if( pPager->dbSize==0 ) return SQLITE_OK;
55763 assert( pPager->zFilename && pPager->zFilename[0] );
55764 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
55765 if( rc==SQLITE_NOTFOUND ){
55766 /* If the HAS_MOVED file-control is unimplemented, assume that the file
55767 ** has not been moved. That is the historical behavior of SQLite: prior to
55768 ** version 3.8.3, it never checked */
55769 rc = SQLITE_OK;
55770 }else if( rc==SQLITE_OK && bHasMoved ){
55771 rc = SQLITE_READONLY_DBMOVED;
55772 }
55773 return rc;
55774}
55775
55776
55777/*
55778** Shutdown the page cache. Free all memory and close all files.
55779**
55780** If a transaction was in progress when this routine is called, that
55781** transaction is rolled back. All outstanding pages are invalidated
55782** and their memory is freed. Any attempt to use a page associated
55783** with this page cache after this function returns will likely
55784** result in a coredump.
55785**
55786** This function always succeeds. If a transaction is active an attempt
55787** is made to roll it back. If an error occurs during the rollback
55788** a hot journal may be left in the filesystem but no error is returned
55789** to the caller.
55790*/
55791SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
55792 u8 *pTmp = (u8*)pPager->pTmpSpace;
55793 assert( db || pagerUseWal(pPager)==0 );
55794 assert( assert_pager_state(pPager) );
55795 disable_simulated_io_errors();
55796 sqlite3BeginBenignMalloc();
55797 pagerFreeMapHdrs(pPager);
55798 /* pPager->errCode = 0; */
55799 pPager->exclusiveMode = 0;
55800#ifndef SQLITE_OMIT_WAL
55801 {
55802 u8 *a = 0;
55803 assert( db || pPager->pWal==0 );
55804 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
55805 && SQLITE_OK==databaseIsUnmoved(pPager)
55806 ){
55807 a = pTmp;
55808 }
55809 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
55810 pPager->pWal = 0;
55811 }
55812#endif
55813 pager_reset(pPager);
55814 if( MEMDB ){
55815 pager_unlock(pPager);
55816 }else{
55817 /* If it is open, sync the journal file before calling UnlockAndRollback.
55818 ** If this is not done, then an unsynced portion of the open journal
55819 ** file may be played back into the database. If a power failure occurs
55820 ** while this is happening, the database could become corrupt.
55821 **
55822 ** If an error occurs while trying to sync the journal, shift the pager
55823 ** into the ERROR state. This causes UnlockAndRollback to unlock the
55824 ** database and close the journal file without attempting to roll it
55825 ** back or finalize it. The next database user will have to do hot-journal
55826 ** rollback before accessing the database file.
55827 */
55828 if( isOpen(pPager->jfd) ){
55829 pager_error(pPager, pagerSyncHotJournal(pPager));
55830 }
55831 pagerUnlockAndRollback(pPager);
55832 }
55833 sqlite3EndBenignMalloc();
55834 enable_simulated_io_errors();
55835 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
55836 IOTRACE(("CLOSE %p\n", pPager))
55837 sqlite3OsClose(pPager->jfd);
55838 sqlite3OsClose(pPager->fd);
55839 sqlite3PageFree(pTmp);
55840 sqlite3PcacheClose(pPager->pPCache);
55841 assert( !pPager->aSavepoint && !pPager->pInJournal );
55842 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
55843
55844 sqlite3_free(pPager);
55845 return SQLITE_OK;
55846}
55847
55848#if !defined(NDEBUG) || defined(SQLITE_TEST)
55849/*
55850** Return the page number for page pPg.
55851*/
55852SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
55853 return pPg->pgno;
55854}
55855#endif
55856
55857/*
55858** Increment the reference count for page pPg.
55859*/
55860SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
55861 sqlite3PcacheRef(pPg);
55862}
55863
55864/*
55865** Sync the journal. In other words, make sure all the pages that have
55866** been written to the journal have actually reached the surface of the
55867** disk and can be restored in the event of a hot-journal rollback.
55868**
55869** If the Pager.noSync flag is set, then this function is a no-op.
55870** Otherwise, the actions required depend on the journal-mode and the
55871** device characteristics of the file-system, as follows:
55872**
55873** * If the journal file is an in-memory journal file, no action need
55874** be taken.
55875**
55876** * Otherwise, if the device does not support the SAFE_APPEND property,
55877** then the nRec field of the most recently written journal header
55878** is updated to contain the number of journal records that have
55879** been written following it. If the pager is operating in full-sync
55880** mode, then the journal file is synced before this field is updated.
55881**
55882** * If the device does not support the SEQUENTIAL property, then
55883** journal file is synced.
55884**
55885** Or, in pseudo-code:
55886**
55887** if( NOT <in-memory journal> ){
55888** if( NOT SAFE_APPEND ){
55889** if( <full-sync mode> ) xSync(<journal file>);
55890** <update nRec field>
55891** }
55892** if( NOT SEQUENTIAL ) xSync(<journal file>);
55893** }
55894**
55895** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
55896** page currently held in memory before returning SQLITE_OK. If an IO
55897** error is encountered, then the IO error code is returned to the caller.
55898*/
55899static int syncJournal(Pager *pPager, int newHdr){
55900 int rc; /* Return code */
55901
55902 assert( pPager->eState==PAGER_WRITER_CACHEMOD
55903 || pPager->eState==PAGER_WRITER_DBMOD
55904 );
55905 assert( assert_pager_state(pPager) );
55906 assert( !pagerUseWal(pPager) );
55907
55908 rc = sqlite3PagerExclusiveLock(pPager);
55909 if( rc!=SQLITE_OK ) return rc;
55910
55911 if( !pPager->noSync ){
55912 assert( !pPager->tempFile );
55913 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
55914 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
55915 assert( isOpen(pPager->jfd) );
55916
55917 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
55918 /* This block deals with an obscure problem. If the last connection
55919 ** that wrote to this database was operating in persistent-journal
55920 ** mode, then the journal file may at this point actually be larger
55921 ** than Pager.journalOff bytes. If the next thing in the journal
55922 ** file happens to be a journal-header (written as part of the
55923 ** previous connection's transaction), and a crash or power-failure
55924 ** occurs after nRec is updated but before this connection writes
55925 ** anything else to the journal file (or commits/rolls back its
55926 ** transaction), then SQLite may become confused when doing the
55927 ** hot-journal rollback following recovery. It may roll back all
55928 ** of this connections data, then proceed to rolling back the old,
55929 ** out-of-date data that follows it. Database corruption.
55930 **
55931 ** To work around this, if the journal file does appear to contain
55932 ** a valid header following Pager.journalOff, then write a 0x00
55933 ** byte to the start of it to prevent it from being recognized.
55934 **
55935 ** Variable iNextHdrOffset is set to the offset at which this
55936 ** problematic header will occur, if it exists. aMagic is used
55937 ** as a temporary buffer to inspect the first couple of bytes of
55938 ** the potential journal header.
55939 */
55940 i64 iNextHdrOffset;
55941 u8 aMagic[8];
55942 u8 zHeader[sizeof(aJournalMagic)+4];
55943
55944 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
55945 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
55946
55947 iNextHdrOffset = journalHdrOffset(pPager);
55948 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
55949 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
55950 static const u8 zerobyte = 0;
55951 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
55952 }
55953 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
55954 return rc;
55955 }
55956
55957 /* Write the nRec value into the journal file header. If in
55958 ** full-synchronous mode, sync the journal first. This ensures that
55959 ** all data has really hit the disk before nRec is updated to mark
55960 ** it as a candidate for rollback.
55961 **
55962 ** This is not required if the persistent media supports the
55963 ** SAFE_APPEND property. Because in this case it is not possible
55964 ** for garbage data to be appended to the file, the nRec field
55965 ** is populated with 0xFFFFFFFF when the journal header is written
55966 ** and never needs to be updated.
55967 */
55968 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
55969 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
55970 IOTRACE(("JSYNC %p\n", pPager))
55971 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
55972 if( rc!=SQLITE_OK ) return rc;
55973 }
55974 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
55975 rc = sqlite3OsWrite(
55976 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
55977 );
55978 if( rc!=SQLITE_OK ) return rc;
55979 }
55980 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
55981 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
55982 IOTRACE(("JSYNC %p\n", pPager))
55983 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
55984 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
55985 );
55986 if( rc!=SQLITE_OK ) return rc;
55987 }
55988
55989 pPager->journalHdr = pPager->journalOff;
55990 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
55991 pPager->nRec = 0;
55992 rc = writeJournalHdr(pPager);
55993 if( rc!=SQLITE_OK ) return rc;
55994 }
55995 }else{
55996 pPager->journalHdr = pPager->journalOff;
55997 }
55998 }
55999
56000 /* Unless the pager is in noSync mode, the journal file was just
56001 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
56002 ** all pages.
56003 */
56004 sqlite3PcacheClearSyncFlags(pPager->pPCache);
56005 pPager->eState = PAGER_WRITER_DBMOD;
56006 assert( assert_pager_state(pPager) );
56007 return SQLITE_OK;
56008}
56009
56010/*
56011** The argument is the first in a linked list of dirty pages connected
56012** by the PgHdr.pDirty pointer. This function writes each one of the
56013** in-memory pages in the list to the database file. The argument may
56014** be NULL, representing an empty list. In this case this function is
56015** a no-op.
56016**
56017** The pager must hold at least a RESERVED lock when this function
56018** is called. Before writing anything to the database file, this lock
56019** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
56020** SQLITE_BUSY is returned and no data is written to the database file.
56021**
56022** If the pager is a temp-file pager and the actual file-system file
56023** is not yet open, it is created and opened before any data is
56024** written out.
56025**
56026** Once the lock has been upgraded and, if necessary, the file opened,
56027** the pages are written out to the database file in list order. Writing
56028** a page is skipped if it meets either of the following criteria:
56029**
56030** * The page number is greater than Pager.dbSize, or
56031** * The PGHDR_DONT_WRITE flag is set on the page.
56032**
56033** If writing out a page causes the database file to grow, Pager.dbFileSize
56034** is updated accordingly. If page 1 is written out, then the value cached
56035** in Pager.dbFileVers[] is updated to match the new value stored in
56036** the database file.
56037**
56038** If everything is successful, SQLITE_OK is returned. If an IO error
56039** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
56040** be obtained, SQLITE_BUSY is returned.
56041*/
56042static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
56043 int rc = SQLITE_OK; /* Return code */
56044
56045 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
56046 assert( !pagerUseWal(pPager) );
56047 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
56048 assert( pPager->eLock==EXCLUSIVE_LOCK );
56049 assert( isOpen(pPager->fd) || pList->pDirty==0 );
56050
56051 /* If the file is a temp-file has not yet been opened, open it now. It
56052 ** is not possible for rc to be other than SQLITE_OK if this branch
56053 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
56054 */
56055 if( !isOpen(pPager->fd) ){
56056 assert( pPager->tempFile && rc==SQLITE_OK );
56057 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
56058 }
56059
56060 /* Before the first write, give the VFS a hint of what the final
56061 ** file size will be.
56062 */
56063 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
56064 if( rc==SQLITE_OK
56065 && pPager->dbHintSize<pPager->dbSize
56066 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
56067 ){
56068 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
56069 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
56070 pPager->dbHintSize = pPager->dbSize;
56071 }
56072
56073 while( rc==SQLITE_OK && pList ){
56074 Pgno pgno = pList->pgno;
56075
56076 /* If there are dirty pages in the page cache with page numbers greater
56077 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
56078 ** make the file smaller (presumably by auto-vacuum code). Do not write
56079 ** any such pages to the file.
56080 **
56081 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
56082 ** set (set by sqlite3PagerDontWrite()).
56083 */
56084 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
56085 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
56086 char *pData; /* Data to write */
56087
56088 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
56089 if( pList->pgno==1 ) pager_write_changecounter(pList);
56090
56091 pData = pList->pData;
56092
56093 /* Write out the page data. */
56094 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
56095
56096 /* If page 1 was just written, update Pager.dbFileVers to match
56097 ** the value now stored in the database file. If writing this
56098 ** page caused the database file to grow, update dbFileSize.
56099 */
56100 if( pgno==1 ){
56101 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
56102 }
56103 if( pgno>pPager->dbFileSize ){
56104 pPager->dbFileSize = pgno;
56105 }
56106 pPager->aStat[PAGER_STAT_WRITE]++;
56107
56108 /* Update any backup objects copying the contents of this pager. */
56109 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
56110
56111 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
56112 PAGERID(pPager), pgno, pager_pagehash(pList)));
56113 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
56114 PAGER_INCR(sqlite3_pager_writedb_count);
56115 }else{
56116 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
56117 }
56118 pager_set_pagehash(pList);
56119 pList = pList->pDirty;
56120 }
56121
56122 return rc;
56123}
56124
56125/*
56126** Ensure that the sub-journal file is open. If it is already open, this
56127** function is a no-op.
56128**
56129** SQLITE_OK is returned if everything goes according to plan. An
56130** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
56131** fails.
56132*/
56133static int openSubJournal(Pager *pPager){
56134 int rc = SQLITE_OK;
56135 if( !isOpen(pPager->sjfd) ){
56136 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
56137 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
56138 | SQLITE_OPEN_DELETEONCLOSE;
56139 int nStmtSpill = sqlite3Config.nStmtSpill;
56140 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
56141 nStmtSpill = -1;
56142 }
56143 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
56144 }
56145 return rc;
56146}
56147
56148/*
56149** Append a record of the current state of page pPg to the sub-journal.
56150**
56151** If successful, set the bit corresponding to pPg->pgno in the bitvecs
56152** for all open savepoints before returning.
56153**
56154** This function returns SQLITE_OK if everything is successful, an IO
56155** error code if the attempt to write to the sub-journal fails, or
56156** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
56157** bitvec.
56158*/
56159static int subjournalPage(PgHdr *pPg){
56160 int rc = SQLITE_OK;
56161 Pager *pPager = pPg->pPager;
56162 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
56163
56164 /* Open the sub-journal, if it has not already been opened */
56165 assert( pPager->useJournal );
56166 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
56167 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
56168 assert( pagerUseWal(pPager)
56169 || pageInJournal(pPager, pPg)
56170 || pPg->pgno>pPager->dbOrigSize
56171 );
56172 rc = openSubJournal(pPager);
56173
56174 /* If the sub-journal was opened successfully (or was already open),
56175 ** write the journal record into the file. */
56176 if( rc==SQLITE_OK ){
56177 void *pData = pPg->pData;
56178 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
56179 char *pData2;
56180 pData2 = pData;
56181 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
56182 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
56183 if( rc==SQLITE_OK ){
56184 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
56185 }
56186 }
56187 }
56188 if( rc==SQLITE_OK ){
56189 pPager->nSubRec++;
56190 assert( pPager->nSavepoint>0 );
56191 rc = addToSavepointBitvecs(pPager, pPg->pgno);
56192 }
56193 return rc;
56194}
56195static int subjournalPageIfRequired(PgHdr *pPg){
56196 if( subjRequiresPage(pPg) ){
56197 return subjournalPage(pPg);
56198 }else{
56199 return SQLITE_OK;
56200 }
56201}
56202
56203/*
56204** This function is called by the pcache layer when it has reached some
56205** soft memory limit. The first argument is a pointer to a Pager object
56206** (cast as a void*). The pager is always 'purgeable' (not an in-memory
56207** database). The second argument is a reference to a page that is
56208** currently dirty but has no outstanding references. The page
56209** is always associated with the Pager object passed as the first
56210** argument.
56211**
56212** The job of this function is to make pPg clean by writing its contents
56213** out to the database file, if possible. This may involve syncing the
56214** journal file.
56215**
56216** If successful, sqlite3PcacheMakeClean() is called on the page and
56217** SQLITE_OK returned. If an IO error occurs while trying to make the
56218** page clean, the IO error code is returned. If the page cannot be
56219** made clean for some other reason, but no error occurs, then SQLITE_OK
56220** is returned by sqlite3PcacheMakeClean() is not called.
56221*/
56222static int pagerStress(void *p, PgHdr *pPg){
56223 Pager *pPager = (Pager *)p;
56224 int rc = SQLITE_OK;
56225
56226 assert( pPg->pPager==pPager );
56227 assert( pPg->flags&PGHDR_DIRTY );
56228
56229 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
56230 ** journal (and adding a new header) is not allowed. This occurs
56231 ** during calls to sqlite3PagerWrite() while trying to journal multiple
56232 ** pages belonging to the same sector.
56233 **
56234 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
56235 ** regardless of whether or not a sync is required. This is set during
56236 ** a rollback or by user request, respectively.
56237 **
56238 ** Spilling is also prohibited when in an error state since that could
56239 ** lead to database corruption. In the current implementation it
56240 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
56241 ** while in the error state, hence it is impossible for this routine to
56242 ** be called in the error state. Nevertheless, we include a NEVER()
56243 ** test for the error state as a safeguard against future changes.
56244 */
56245 if( NEVER(pPager->errCode) ) return SQLITE_OK;
56246 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
56247 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
56248 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
56249 if( pPager->doNotSpill
56250 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
56251 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
56252 ){
56253 return SQLITE_OK;
56254 }
56255
56256 pPager->aStat[PAGER_STAT_SPILL]++;
56257 pPg->pDirty = 0;
56258 if( pagerUseWal(pPager) ){
56259 /* Write a single frame for this page to the log. */
56260 rc = subjournalPageIfRequired(pPg);
56261 if( rc==SQLITE_OK ){
56262 rc = pagerWalFrames(pPager, pPg, 0, 0);
56263 }
56264 }else{
56265
56266#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56267 if( pPager->tempFile==0 ){
56268 rc = sqlite3JournalCreate(pPager->jfd);
56269 if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
56270 }
56271#endif
56272
56273 /* Sync the journal file if required. */
56274 if( pPg->flags&PGHDR_NEED_SYNC
56275 || pPager->eState==PAGER_WRITER_CACHEMOD
56276 ){
56277 rc = syncJournal(pPager, 1);
56278 }
56279
56280 /* Write the contents of the page out to the database file. */
56281 if( rc==SQLITE_OK ){
56282 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
56283 rc = pager_write_pagelist(pPager, pPg);
56284 }
56285 }
56286
56287 /* Mark the page as clean. */
56288 if( rc==SQLITE_OK ){
56289 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
56290 sqlite3PcacheMakeClean(pPg);
56291 }
56292
56293 return pager_error(pPager, rc);
56294}
56295
56296/*
56297** Flush all unreferenced dirty pages to disk.
56298*/
56299SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
56300 int rc = pPager->errCode;
56301 if( !MEMDB ){
56302 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
56303 assert( assert_pager_state(pPager) );
56304 while( rc==SQLITE_OK && pList ){
56305 PgHdr *pNext = pList->pDirty;
56306 if( pList->nRef==0 ){
56307 rc = pagerStress((void*)pPager, pList);
56308 }
56309 pList = pNext;
56310 }
56311 }
56312
56313 return rc;
56314}
56315
56316/*
56317** Allocate and initialize a new Pager object and put a pointer to it
56318** in *ppPager. The pager should eventually be freed by passing it
56319** to sqlite3PagerClose().
56320**
56321** The zFilename argument is the path to the database file to open.
56322** If zFilename is NULL then a randomly-named temporary file is created
56323** and used as the file to be cached. Temporary files are be deleted
56324** automatically when they are closed. If zFilename is ":memory:" then
56325** all information is held in cache. It is never written to disk.
56326** This can be used to implement an in-memory database.
56327**
56328** The nExtra parameter specifies the number of bytes of space allocated
56329** along with each page reference. This space is available to the user
56330** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
56331** first 8 bytes of this space are zeroed but the remainder is uninitialized.
56332** (The extra space is used by btree as the MemPage object.)
56333**
56334** The flags argument is used to specify properties that affect the
56335** operation of the pager. It should be passed some bitwise combination
56336** of the PAGER_* flags.
56337**
56338** The vfsFlags parameter is a bitmask to pass to the flags parameter
56339** of the xOpen() method of the supplied VFS when opening files.
56340**
56341** If the pager object is allocated and the specified file opened
56342** successfully, SQLITE_OK is returned and *ppPager set to point to
56343** the new pager object. If an error occurs, *ppPager is set to NULL
56344** and error code returned. This function may return SQLITE_NOMEM
56345** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
56346** various SQLITE_IO_XXX errors.
56347*/
56348SQLITE_PRIVATE int sqlite3PagerOpen(
56349 sqlite3_vfs *pVfs, /* The virtual file system to use */
56350 Pager **ppPager, /* OUT: Return the Pager structure here */
56351 const char *zFilename, /* Name of the database file to open */
56352 int nExtra, /* Extra bytes append to each in-memory page */
56353 int flags, /* flags controlling this file */
56354 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
56355 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
56356){
56357 u8 *pPtr;
56358 Pager *pPager = 0; /* Pager object to allocate and return */
56359 int rc = SQLITE_OK; /* Return code */
56360 int tempFile = 0; /* True for temp files (incl. in-memory files) */
56361 int memDb = 0; /* True if this is an in-memory file */
56362#ifdef SQLITE_ENABLE_DESERIALIZE
56363 int memJM = 0; /* Memory journal mode */
56364#else
56365# define memJM 0
56366#endif
56367 int readOnly = 0; /* True if this is a read-only file */
56368 int journalFileSize; /* Bytes to allocate for each journal fd */
56369 char *zPathname = 0; /* Full path to database file */
56370 int nPathname = 0; /* Number of bytes in zPathname */
56371 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
56372 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
56373 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
56374 const char *zUri = 0; /* URI args to copy */
56375 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
56376 int nUri = 0; /* Number of URI parameters */
56377
56378 /* Figure out how much space is required for each journal file-handle
56379 ** (there are two of them, the main journal and the sub-journal). */
56380 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
56381
56382 /* Set the output variable to NULL in case an error occurs. */
56383 *ppPager = 0;
56384
56385#ifndef SQLITE_OMIT_MEMORYDB
56386 if( flags & PAGER_MEMORY ){
56387 memDb = 1;
56388 if( zFilename && zFilename[0] ){
56389 zPathname = sqlite3DbStrDup(0, zFilename);
56390 if( zPathname==0 ) return SQLITE_NOMEM_BKPT;
56391 nPathname = sqlite3Strlen30(zPathname);
56392 zFilename = 0;
56393 }
56394 }
56395#endif
56396
56397 /* Compute and store the full pathname in an allocated buffer pointed
56398 ** to by zPathname, length nPathname. Or, if this is a temporary file,
56399 ** leave both nPathname and zPathname set to 0.
56400 */
56401 if( zFilename && zFilename[0] ){
56402 const char *z;
56403 nPathname = pVfs->mxPathname+1;
56404 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
56405 if( zPathname==0 ){
56406 return SQLITE_NOMEM_BKPT;
56407 }
56408 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
56409 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
56410 if( rc!=SQLITE_OK ){
56411 if( rc==SQLITE_OK_SYMLINK ){
56412 if( vfsFlags & SQLITE_OPEN_NOFOLLOW ){
56413 rc = SQLITE_CANTOPEN_SYMLINK;
56414 }else{
56415 rc = SQLITE_OK;
56416 }
56417 }
56418 }
56419 nPathname = sqlite3Strlen30(zPathname);
56420 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
56421 while( *z ){
56422 z += strlen(z)+1;
56423 z += strlen(z)+1;
56424 nUri++;
56425 }
56426 nUriByte = (int)(&z[1] - zUri);
56427 assert( nUriByte>=1 );
56428 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
56429 /* This branch is taken when the journal path required by
56430 ** the database being opened will be more than pVfs->mxPathname
56431 ** bytes in length. This means the database cannot be opened,
56432 ** as it will not be possible to open the journal file or even
56433 ** check for a hot-journal before reading.
56434 */
56435 rc = SQLITE_CANTOPEN_BKPT;
56436 }
56437 if( rc!=SQLITE_OK ){
56438 sqlite3DbFree(0, zPathname);
56439 return rc;
56440 }
56441 }
56442
56443 /* Allocate memory for the Pager structure, PCache object, the
56444 ** three file descriptors, the database file name and the journal
56445 ** file name. The layout in memory is as follows:
56446 **
56447 ** Pager object (sizeof(Pager) bytes)
56448 ** PCache object (sqlite3PcacheSize() bytes)
56449 ** Database file handle (pVfs->szOsFile bytes)
56450 ** Sub-journal file handle (journalFileSize bytes)
56451 ** Main journal file handle (journalFileSize bytes)
56452 ** Ptr back to the Pager (sizeof(Pager*) bytes)
56453 ** \0\0\0\0 database prefix (4 bytes)
56454 ** Database file name (nPathname+1 bytes)
56455 ** URI query parameters (nUriByte bytes)
56456 ** Journal filename (nPathname+8+1 bytes)
56457 ** WAL filename (nPathname+4+1 bytes)
56458 ** \0\0\0 terminator (3 bytes)
56459 **
56460 ** Some 3rd-party software, over which we have no control, depends on
56461 ** the specific order of the filenames and the \0 separators between them
56462 ** so that it can (for example) find the database filename given the WAL
56463 ** filename without using the sqlite3_filename_database() API. This is a
56464 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
56465 ** software is in widespread use, so we try to avoid changing the filename
56466 ** order and formatting if possible. In particular, the details of the
56467 ** filename format expected by 3rd-party software should be as follows:
56468 **
56469 ** - Main Database Path
56470 ** - \0
56471 ** - Multiple URI components consisting of:
56472 ** - Key
56473 ** - \0
56474 ** - Value
56475 ** - \0
56476 ** - \0
56477 ** - Journal Path
56478 ** - \0
56479 ** - WAL Path (zWALName)
56480 ** - \0
56481 **
56482 ** The sqlite3_create_filename() interface and the databaseFilename() utility
56483 ** that is used by sqlite3_filename_database() and kin also depend on the
56484 ** specific formatting and order of the various filenames, so if the format
56485 ** changes here, be sure to change it there as well.
56486 */
56487 pPtr = (u8 *)sqlite3MallocZero(
56488 ROUND8(sizeof(*pPager)) + /* Pager structure */
56489 ROUND8(pcacheSize) + /* PCache object */
56490 ROUND8(pVfs->szOsFile) + /* The main db file */
56491 journalFileSize * 2 + /* The two journal files */
56492 sizeof(pPager) + /* Space to hold a pointer */
56493 4 + /* Database prefix */
56494 nPathname + 1 + /* database filename */
56495 nUriByte + /* query parameters */
56496 nPathname + 8 + 1 + /* Journal filename */
56497#ifndef SQLITE_OMIT_WAL
56498 nPathname + 4 + 1 + /* WAL filename */
56499#endif
56500 3 /* Terminator */
56501 );
56502 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
56503 if( !pPtr ){
56504 sqlite3DbFree(0, zPathname);
56505 return SQLITE_NOMEM_BKPT;
56506 }
56507 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager));
56508 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
56509 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
56510 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
56511 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
56512 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
56513 memcpy(pPtr, &pPager, sizeof(pPager)); pPtr += sizeof(pPager);
56514
56515 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
56516 pPtr += 4; /* Skip zero prefix */
56517 pPager->zFilename = (char*)pPtr;
56518 if( nPathname>0 ){
56519 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname + 1;
56520 if( zUri ){
56521 memcpy(pPtr, zUri, nUriByte); pPtr += nUriByte;
56522 }else{
56523 pPtr++;
56524 }
56525 }
56526
56527
56528 /* Fill in Pager.zJournal */
56529 if( nPathname>0 ){
56530 pPager->zJournal = (char*)pPtr;
56531 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
56532 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
56533#ifdef SQLITE_ENABLE_8_3_NAMES
56534 sqlite3FileSuffix3(zFilename,pPager->zJournal);
56535 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
56536#endif
56537 }else{
56538 pPager->zJournal = 0;
56539 }
56540
56541#ifndef SQLITE_OMIT_WAL
56542 /* Fill in Pager.zWal */
56543 if( nPathname>0 ){
56544 pPager->zWal = (char*)pPtr;
56545 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
56546 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
56547#ifdef SQLITE_ENABLE_8_3_NAMES
56548 sqlite3FileSuffix3(zFilename, pPager->zWal);
56549 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
56550#endif
56551 }else{
56552 pPager->zWal = 0;
56553 }
56554#endif
56555
56556 if( nPathname ) sqlite3DbFree(0, zPathname);
56557 pPager->pVfs = pVfs;
56558 pPager->vfsFlags = vfsFlags;
56559
56560 /* Open the pager file.
56561 */
56562 if( zFilename && zFilename[0] ){
56563 int fout = 0; /* VFS flags returned by xOpen() */
56564 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
56565 assert( !memDb );
56566#ifdef SQLITE_ENABLE_DESERIALIZE
56567 memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
56568#endif
56569 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
56570
56571 /* If the file was successfully opened for read/write access,
56572 ** choose a default page size in case we have to create the
56573 ** database file. The default page size is the maximum of:
56574 **
56575 ** + SQLITE_DEFAULT_PAGE_SIZE,
56576 ** + The value returned by sqlite3OsSectorSize()
56577 ** + The largest page size that can be written atomically.
56578 */
56579 if( rc==SQLITE_OK ){
56580 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
56581 if( !readOnly ){
56582 setSectorSize(pPager);
56583 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
56584 if( szPageDflt<pPager->sectorSize ){
56585 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
56586 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
56587 }else{
56588 szPageDflt = (u32)pPager->sectorSize;
56589 }
56590 }
56591#ifdef SQLITE_ENABLE_ATOMIC_WRITE
56592 {
56593 int ii;
56594 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
56595 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
56596 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
56597 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
56598 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
56599 szPageDflt = ii;
56600 }
56601 }
56602 }
56603#endif
56604 }
56605 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
56606 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
56607 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
56608 vfsFlags |= SQLITE_OPEN_READONLY;
56609 goto act_like_temp_file;
56610 }
56611 }
56612 }else{
56613 /* If a temporary file is requested, it is not opened immediately.
56614 ** In this case we accept the default page size and delay actually
56615 ** opening the file until the first call to OsWrite().
56616 **
56617 ** This branch is also run for an in-memory database. An in-memory
56618 ** database is the same as a temp-file that is never written out to
56619 ** disk and uses an in-memory rollback journal.
56620 **
56621 ** This branch also runs for files marked as immutable.
56622 */
56623act_like_temp_file:
56624 tempFile = 1;
56625 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
56626 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
56627 pPager->noLock = 1; /* Do no locking */
56628 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
56629 }
56630
56631 /* The following call to PagerSetPagesize() serves to set the value of
56632 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
56633 */
56634 if( rc==SQLITE_OK ){
56635 assert( pPager->memDb==0 );
56636 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
56637 testcase( rc!=SQLITE_OK );
56638 }
56639
56640 /* Initialize the PCache object. */
56641 if( rc==SQLITE_OK ){
56642 nExtra = ROUND8(nExtra);
56643 assert( nExtra>=8 && nExtra<1000 );
56644 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
56645 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
56646 }
56647
56648 /* If an error occurred above, free the Pager structure and close the file.
56649 */
56650 if( rc!=SQLITE_OK ){
56651 sqlite3OsClose(pPager->fd);
56652 sqlite3PageFree(pPager->pTmpSpace);
56653 sqlite3_free(pPager);
56654 return rc;
56655 }
56656
56657 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
56658 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
56659
56660 pPager->useJournal = (u8)useJournal;
56661 /* pPager->stmtOpen = 0; */
56662 /* pPager->stmtInUse = 0; */
56663 /* pPager->nRef = 0; */
56664 /* pPager->stmtSize = 0; */
56665 /* pPager->stmtJSize = 0; */
56666 /* pPager->nPage = 0; */
56667 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
56668 /* pPager->state = PAGER_UNLOCK; */
56669 /* pPager->errMask = 0; */
56670 pPager->tempFile = (u8)tempFile;
56671 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
56672 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
56673 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
56674 pPager->exclusiveMode = (u8)tempFile;
56675 pPager->changeCountDone = pPager->tempFile;
56676 pPager->memDb = (u8)memDb;
56677 pPager->readOnly = (u8)readOnly;
56678 assert( useJournal || pPager->tempFile );
56679 pPager->noSync = pPager->tempFile;
56680 if( pPager->noSync ){
56681 assert( pPager->fullSync==0 );
56682 assert( pPager->extraSync==0 );
56683 assert( pPager->syncFlags==0 );
56684 assert( pPager->walSyncFlags==0 );
56685 }else{
56686 pPager->fullSync = 1;
56687 pPager->extraSync = 0;
56688 pPager->syncFlags = SQLITE_SYNC_NORMAL;
56689 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
56690 }
56691 /* pPager->pFirst = 0; */
56692 /* pPager->pFirstSynced = 0; */
56693 /* pPager->pLast = 0; */
56694 pPager->nExtra = (u16)nExtra;
56695 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
56696 assert( isOpen(pPager->fd) || tempFile );
56697 setSectorSize(pPager);
56698 if( !useJournal ){
56699 pPager->journalMode = PAGER_JOURNALMODE_OFF;
56700 }else if( memDb || memJM ){
56701 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
56702 }
56703 /* pPager->xBusyHandler = 0; */
56704 /* pPager->pBusyHandlerArg = 0; */
56705 pPager->xReiniter = xReinit;
56706 setGetterMethod(pPager);
56707 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
56708 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
56709
56710 *ppPager = pPager;
56711 return SQLITE_OK;
56712}
56713
56714/*
56715** Return the sqlite3_file for the main database given the name
56716** of the corresonding WAL or Journal name as passed into
56717** xOpen.
56718*/
56719SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
56720 Pager *pPager;
56721 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
56722 zName--;
56723 }
56724 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
56725 return pPager->fd;
56726}
56727
56728
56729/*
56730** This function is called after transitioning from PAGER_UNLOCK to
56731** PAGER_SHARED state. It tests if there is a hot journal present in
56732** the file-system for the given pager. A hot journal is one that
56733** needs to be played back. According to this function, a hot-journal
56734** file exists if the following criteria are met:
56735**
56736** * The journal file exists in the file system, and
56737** * No process holds a RESERVED or greater lock on the database file, and
56738** * The database file itself is greater than 0 bytes in size, and
56739** * The first byte of the journal file exists and is not 0x00.
56740**
56741** If the current size of the database file is 0 but a journal file
56742** exists, that is probably an old journal left over from a prior
56743** database with the same name. In this case the journal file is
56744** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
56745** is returned.
56746**
56747** This routine does not check if there is a super-journal filename
56748** at the end of the file. If there is, and that super-journal file
56749** does not exist, then the journal file is not really hot. In this
56750** case this routine will return a false-positive. The pager_playback()
56751** routine will discover that the journal file is not really hot and
56752** will not roll it back.
56753**
56754** If a hot-journal file is found to exist, *pExists is set to 1 and
56755** SQLITE_OK returned. If no hot-journal file is present, *pExists is
56756** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
56757** to determine whether or not a hot-journal file exists, the IO error
56758** code is returned and the value of *pExists is undefined.
56759*/
56760static int hasHotJournal(Pager *pPager, int *pExists){
56761 sqlite3_vfs * const pVfs = pPager->pVfs;
56762 int rc = SQLITE_OK; /* Return code */
56763 int exists = 1; /* True if a journal file is present */
56764 int jrnlOpen = !!isOpen(pPager->jfd);
56765
56766 assert( pPager->useJournal );
56767 assert( isOpen(pPager->fd) );
56768 assert( pPager->eState==PAGER_OPEN );
56769
56770 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
56771 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
56772 ));
56773
56774 *pExists = 0;
56775 if( !jrnlOpen ){
56776 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
56777 }
56778 if( rc==SQLITE_OK && exists ){
56779 int locked = 0; /* True if some process holds a RESERVED lock */
56780
56781 /* Race condition here: Another process might have been holding the
56782 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
56783 ** call above, but then delete the journal and drop the lock before
56784 ** we get to the following sqlite3OsCheckReservedLock() call. If that
56785 ** is the case, this routine might think there is a hot journal when
56786 ** in fact there is none. This results in a false-positive which will
56787 ** be dealt with by the playback routine. Ticket #3883.
56788 */
56789 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
56790 if( rc==SQLITE_OK && !locked ){
56791 Pgno nPage; /* Number of pages in database file */
56792
56793 assert( pPager->tempFile==0 );
56794 rc = pagerPagecount(pPager, &nPage);
56795 if( rc==SQLITE_OK ){
56796 /* If the database is zero pages in size, that means that either (1) the
56797 ** journal is a remnant from a prior database with the same name where
56798 ** the database file but not the journal was deleted, or (2) the initial
56799 ** transaction that populates a new database is being rolled back.
56800 ** In either case, the journal file can be deleted. However, take care
56801 ** not to delete the journal file if it is already open due to
56802 ** journal_mode=PERSIST.
56803 */
56804 if( nPage==0 && !jrnlOpen ){
56805 sqlite3BeginBenignMalloc();
56806 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
56807 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
56808 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
56809 }
56810 sqlite3EndBenignMalloc();
56811 }else{
56812 /* The journal file exists and no other connection has a reserved
56813 ** or greater lock on the database file. Now check that there is
56814 ** at least one non-zero bytes at the start of the journal file.
56815 ** If there is, then we consider this journal to be hot. If not,
56816 ** it can be ignored.
56817 */
56818 if( !jrnlOpen ){
56819 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
56820 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
56821 }
56822 if( rc==SQLITE_OK ){
56823 u8 first = 0;
56824 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
56825 if( rc==SQLITE_IOERR_SHORT_READ ){
56826 rc = SQLITE_OK;
56827 }
56828 if( !jrnlOpen ){
56829 sqlite3OsClose(pPager->jfd);
56830 }
56831 *pExists = (first!=0);
56832 }else if( rc==SQLITE_CANTOPEN ){
56833 /* If we cannot open the rollback journal file in order to see if
56834 ** it has a zero header, that might be due to an I/O error, or
56835 ** it might be due to the race condition described above and in
56836 ** ticket #3883. Either way, assume that the journal is hot.
56837 ** This might be a false positive. But if it is, then the
56838 ** automatic journal playback and recovery mechanism will deal
56839 ** with it under an EXCLUSIVE lock where we do not need to
56840 ** worry so much with race conditions.
56841 */
56842 *pExists = 1;
56843 rc = SQLITE_OK;
56844 }
56845 }
56846 }
56847 }
56848 }
56849
56850 return rc;
56851}
56852
56853/*
56854** This function is called to obtain a shared lock on the database file.
56855** It is illegal to call sqlite3PagerGet() until after this function
56856** has been successfully called. If a shared-lock is already held when
56857** this function is called, it is a no-op.
56858**
56859** The following operations are also performed by this function.
56860**
56861** 1) If the pager is currently in PAGER_OPEN state (no lock held
56862** on the database file), then an attempt is made to obtain a
56863** SHARED lock on the database file. Immediately after obtaining
56864** the SHARED lock, the file-system is checked for a hot-journal,
56865** which is played back if present. Following any hot-journal
56866** rollback, the contents of the cache are validated by checking
56867** the 'change-counter' field of the database file header and
56868** discarded if they are found to be invalid.
56869**
56870** 2) If the pager is running in exclusive-mode, and there are currently
56871** no outstanding references to any pages, and is in the error state,
56872** then an attempt is made to clear the error state by discarding
56873** the contents of the page cache and rolling back any open journal
56874** file.
56875**
56876** If everything is successful, SQLITE_OK is returned. If an IO error
56877** occurs while locking the database, checking for a hot-journal file or
56878** rolling back a journal file, the IO error code is returned.
56879*/
56880SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
56881 int rc = SQLITE_OK; /* Return code */
56882
56883 /* This routine is only called from b-tree and only when there are no
56884 ** outstanding pages. This implies that the pager state should either
56885 ** be OPEN or READER. READER is only possible if the pager is or was in
56886 ** exclusive access mode. */
56887 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
56888 assert( assert_pager_state(pPager) );
56889 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
56890 assert( pPager->errCode==SQLITE_OK );
56891
56892 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
56893 int bHotJournal = 1; /* True if there exists a hot journal-file */
56894
56895 assert( !MEMDB );
56896 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
56897
56898 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
56899 if( rc!=SQLITE_OK ){
56900 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
56901 goto failed;
56902 }
56903
56904 /* If a journal file exists, and there is no RESERVED lock on the
56905 ** database file, then it either needs to be played back or deleted.
56906 */
56907 if( pPager->eLock<=SHARED_LOCK ){
56908 rc = hasHotJournal(pPager, &bHotJournal);
56909 }
56910 if( rc!=SQLITE_OK ){
56911 goto failed;
56912 }
56913 if( bHotJournal ){
56914 if( pPager->readOnly ){
56915 rc = SQLITE_READONLY_ROLLBACK;
56916 goto failed;
56917 }
56918
56919 /* Get an EXCLUSIVE lock on the database file. At this point it is
56920 ** important that a RESERVED lock is not obtained on the way to the
56921 ** EXCLUSIVE lock. If it were, another process might open the
56922 ** database file, detect the RESERVED lock, and conclude that the
56923 ** database is safe to read while this process is still rolling the
56924 ** hot-journal back.
56925 **
56926 ** Because the intermediate RESERVED lock is not requested, any
56927 ** other process attempting to access the database file will get to
56928 ** this point in the code and fail to obtain its own EXCLUSIVE lock
56929 ** on the database file.
56930 **
56931 ** Unless the pager is in locking_mode=exclusive mode, the lock is
56932 ** downgraded to SHARED_LOCK before this function returns.
56933 */
56934 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
56935 if( rc!=SQLITE_OK ){
56936 goto failed;
56937 }
56938
56939 /* If it is not already open and the file exists on disk, open the
56940 ** journal for read/write access. Write access is required because
56941 ** in exclusive-access mode the file descriptor will be kept open
56942 ** and possibly used for a transaction later on. Also, write-access
56943 ** is usually required to finalize the journal in journal_mode=persist
56944 ** mode (and also for journal_mode=truncate on some systems).
56945 **
56946 ** If the journal does not exist, it usually means that some
56947 ** other connection managed to get in and roll it back before
56948 ** this connection obtained the exclusive lock above. Or, it
56949 ** may mean that the pager was in the error-state when this
56950 ** function was called and the journal file does not exist.
56951 */
56952 if( !isOpen(pPager->jfd) ){
56953 sqlite3_vfs * const pVfs = pPager->pVfs;
56954 int bExists; /* True if journal file exists */
56955 rc = sqlite3OsAccess(
56956 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
56957 if( rc==SQLITE_OK && bExists ){
56958 int fout = 0;
56959 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
56960 assert( !pPager->tempFile );
56961 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
56962 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
56963 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
56964 rc = SQLITE_CANTOPEN_BKPT;
56965 sqlite3OsClose(pPager->jfd);
56966 }
56967 }
56968 }
56969
56970 /* Playback and delete the journal. Drop the database write
56971 ** lock and reacquire the read lock. Purge the cache before
56972 ** playing back the hot-journal so that we don't end up with
56973 ** an inconsistent cache. Sync the hot journal before playing
56974 ** it back since the process that crashed and left the hot journal
56975 ** probably did not sync it and we are required to always sync
56976 ** the journal before playing it back.
56977 */
56978 if( isOpen(pPager->jfd) ){
56979 assert( rc==SQLITE_OK );
56980 rc = pagerSyncHotJournal(pPager);
56981 if( rc==SQLITE_OK ){
56982 rc = pager_playback(pPager, !pPager->tempFile);
56983 pPager->eState = PAGER_OPEN;
56984 }
56985 }else if( !pPager->exclusiveMode ){
56986 pagerUnlockDb(pPager, SHARED_LOCK);
56987 }
56988
56989 if( rc!=SQLITE_OK ){
56990 /* This branch is taken if an error occurs while trying to open
56991 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
56992 ** pager_unlock() routine will be called before returning to unlock
56993 ** the file. If the unlock attempt fails, then Pager.eLock must be
56994 ** set to UNKNOWN_LOCK (see the comment above the #define for
56995 ** UNKNOWN_LOCK above for an explanation).
56996 **
56997 ** In order to get pager_unlock() to do this, set Pager.eState to
56998 ** PAGER_ERROR now. This is not actually counted as a transition
56999 ** to ERROR state in the state diagram at the top of this file,
57000 ** since we know that the same call to pager_unlock() will very
57001 ** shortly transition the pager object to the OPEN state. Calling
57002 ** assert_pager_state() would fail now, as it should not be possible
57003 ** to be in ERROR state when there are zero outstanding page
57004 ** references.
57005 */
57006 pager_error(pPager, rc);
57007 goto failed;
57008 }
57009
57010 assert( pPager->eState==PAGER_OPEN );
57011 assert( (pPager->eLock==SHARED_LOCK)
57012 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
57013 );
57014 }
57015
57016 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
57017 /* The shared-lock has just been acquired then check to
57018 ** see if the database has been modified. If the database has changed,
57019 ** flush the cache. The hasHeldSharedLock flag prevents this from
57020 ** occurring on the very first access to a file, in order to save a
57021 ** single unnecessary sqlite3OsRead() call at the start-up.
57022 **
57023 ** Database changes are detected by looking at 15 bytes beginning
57024 ** at offset 24 into the file. The first 4 of these 16 bytes are
57025 ** a 32-bit counter that is incremented with each change. The
57026 ** other bytes change randomly with each file change when
57027 ** a codec is in use.
57028 **
57029 ** There is a vanishingly small chance that a change will not be
57030 ** detected. The chance of an undetected change is so small that
57031 ** it can be neglected.
57032 */
57033 char dbFileVers[sizeof(pPager->dbFileVers)];
57034
57035 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
57036 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
57037 if( rc!=SQLITE_OK ){
57038 if( rc!=SQLITE_IOERR_SHORT_READ ){
57039 goto failed;
57040 }
57041 memset(dbFileVers, 0, sizeof(dbFileVers));
57042 }
57043
57044 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
57045 pager_reset(pPager);
57046
57047 /* Unmap the database file. It is possible that external processes
57048 ** may have truncated the database file and then extended it back
57049 ** to its original size while this process was not holding a lock.
57050 ** In this case there may exist a Pager.pMap mapping that appears
57051 ** to be the right size but is not actually valid. Avoid this
57052 ** possibility by unmapping the db here. */
57053 if( USEFETCH(pPager) ){
57054 sqlite3OsUnfetch(pPager->fd, 0, 0);
57055 }
57056 }
57057 }
57058
57059 /* If there is a WAL file in the file-system, open this database in WAL
57060 ** mode. Otherwise, the following function call is a no-op.
57061 */
57062 rc = pagerOpenWalIfPresent(pPager);
57063#ifndef SQLITE_OMIT_WAL
57064 assert( pPager->pWal==0 || rc==SQLITE_OK );
57065#endif
57066 }
57067
57068 if( pagerUseWal(pPager) ){
57069 assert( rc==SQLITE_OK );
57070 rc = pagerBeginReadTransaction(pPager);
57071 }
57072
57073 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
57074 rc = pagerPagecount(pPager, &pPager->dbSize);
57075 }
57076
57077 failed:
57078 if( rc!=SQLITE_OK ){
57079 assert( !MEMDB );
57080 pager_unlock(pPager);
57081 assert( pPager->eState==PAGER_OPEN );
57082 }else{
57083 pPager->eState = PAGER_READER;
57084 pPager->hasHeldSharedLock = 1;
57085 }
57086 return rc;
57087}
57088
57089/*
57090** If the reference count has reached zero, rollback any active
57091** transaction and unlock the pager.
57092**
57093** Except, in locking_mode=EXCLUSIVE when there is nothing to in
57094** the rollback journal, the unlock is not performed and there is
57095** nothing to rollback, so this routine is a no-op.
57096*/
57097static void pagerUnlockIfUnused(Pager *pPager){
57098 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
57099 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
57100 pagerUnlockAndRollback(pPager);
57101 }
57102}
57103
57104/*
57105** The page getter methods each try to acquire a reference to a
57106** page with page number pgno. If the requested reference is
57107** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
57108**
57109** There are different implementations of the getter method depending
57110** on the current state of the pager.
57111**
57112** getPageNormal() -- The normal getter
57113** getPageError() -- Used if the pager is in an error state
57114** getPageMmap() -- Used if memory-mapped I/O is enabled
57115**
57116** If the requested page is already in the cache, it is returned.
57117** Otherwise, a new page object is allocated and populated with data
57118** read from the database file. In some cases, the pcache module may
57119** choose not to allocate a new page object and may reuse an existing
57120** object with no outstanding references.
57121**
57122** The extra data appended to a page is always initialized to zeros the
57123** first time a page is loaded into memory. If the page requested is
57124** already in the cache when this function is called, then the extra
57125** data is left as it was when the page object was last used.
57126**
57127** If the database image is smaller than the requested page or if
57128** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
57129** requested page is not already stored in the cache, then no
57130** actual disk read occurs. In this case the memory image of the
57131** page is initialized to all zeros.
57132**
57133** If PAGER_GET_NOCONTENT is true, it means that we do not care about
57134** the contents of the page. This occurs in two scenarios:
57135**
57136** a) When reading a free-list leaf page from the database, and
57137**
57138** b) When a savepoint is being rolled back and we need to load
57139** a new page into the cache to be filled with the data read
57140** from the savepoint journal.
57141**
57142** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
57143** of being read from the database. Additionally, the bits corresponding
57144** to pgno in Pager.pInJournal (bitvec of pages already written to the
57145** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
57146** savepoints are set. This means if the page is made writable at any
57147** point in the future, using a call to sqlite3PagerWrite(), its contents
57148** will not be journaled. This saves IO.
57149**
57150** The acquisition might fail for several reasons. In all cases,
57151** an appropriate error code is returned and *ppPage is set to NULL.
57152**
57153** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
57154** to find a page in the in-memory cache first. If the page is not already
57155** in memory, this routine goes to disk to read it in whereas Lookup()
57156** just returns 0. This routine acquires a read-lock the first time it
57157** has to go to disk, and could also playback an old journal if necessary.
57158** Since Lookup() never goes to disk, it never has to deal with locks
57159** or journal files.
57160*/
57161static int getPageNormal(
57162 Pager *pPager, /* The pager open on the database file */
57163 Pgno pgno, /* Page number to fetch */
57164 DbPage **ppPage, /* Write a pointer to the page here */
57165 int flags /* PAGER_GET_XXX flags */
57166){
57167 int rc = SQLITE_OK;
57168 PgHdr *pPg;
57169 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
57170 sqlite3_pcache_page *pBase;
57171
57172 assert( pPager->errCode==SQLITE_OK );
57173 assert( pPager->eState>=PAGER_READER );
57174 assert( assert_pager_state(pPager) );
57175 assert( pPager->hasHeldSharedLock==1 );
57176
57177 if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
57178 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
57179 if( pBase==0 ){
57180 pPg = 0;
57181 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
57182 if( rc!=SQLITE_OK ) goto pager_acquire_err;
57183 if( pBase==0 ){
57184 rc = SQLITE_NOMEM_BKPT;
57185 goto pager_acquire_err;
57186 }
57187 }
57188 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
57189 assert( pPg==(*ppPage) );
57190 assert( pPg->pgno==pgno );
57191 assert( pPg->pPager==pPager || pPg->pPager==0 );
57192
57193 noContent = (flags & PAGER_GET_NOCONTENT)!=0;
57194 if( pPg->pPager && !noContent ){
57195 /* In this case the pcache already contains an initialized copy of
57196 ** the page. Return without further ado. */
57197 assert( pgno!=PAGER_MJ_PGNO(pPager) );
57198 pPager->aStat[PAGER_STAT_HIT]++;
57199 return SQLITE_OK;
57200
57201 }else{
57202 /* The pager cache has created a new page. Its content needs to
57203 ** be initialized. But first some error checks:
57204 **
57205 ** (*) obsolete. Was: maximum page number is 2^31
57206 ** (2) Never try to fetch the locking page
57207 */
57208 if( pgno==PAGER_MJ_PGNO(pPager) ){
57209 rc = SQLITE_CORRUPT_BKPT;
57210 goto pager_acquire_err;
57211 }
57212
57213 pPg->pPager = pPager;
57214
57215 assert( !isOpen(pPager->fd) || !MEMDB );
57216 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
57217 if( pgno>pPager->mxPgno ){
57218 rc = SQLITE_FULL;
57219 goto pager_acquire_err;
57220 }
57221 if( noContent ){
57222 /* Failure to set the bits in the InJournal bit-vectors is benign.
57223 ** It merely means that we might do some extra work to journal a
57224 ** page that does not need to be journaled. Nevertheless, be sure
57225 ** to test the case where a malloc error occurs while trying to set
57226 ** a bit in a bit vector.
57227 */
57228 sqlite3BeginBenignMalloc();
57229 if( pgno<=pPager->dbOrigSize ){
57230 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
57231 testcase( rc==SQLITE_NOMEM );
57232 }
57233 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
57234 testcase( rc==SQLITE_NOMEM );
57235 sqlite3EndBenignMalloc();
57236 }
57237 memset(pPg->pData, 0, pPager->pageSize);
57238 IOTRACE(("ZERO %p %d\n", pPager, pgno));
57239 }else{
57240 assert( pPg->pPager==pPager );
57241 pPager->aStat[PAGER_STAT_MISS]++;
57242 rc = readDbPage(pPg);
57243 if( rc!=SQLITE_OK ){
57244 goto pager_acquire_err;
57245 }
57246 }
57247 pager_set_pagehash(pPg);
57248 }
57249 return SQLITE_OK;
57250
57251pager_acquire_err:
57252 assert( rc!=SQLITE_OK );
57253 if( pPg ){
57254 sqlite3PcacheDrop(pPg);
57255 }
57256 pagerUnlockIfUnused(pPager);
57257 *ppPage = 0;
57258 return rc;
57259}
57260
57261#if SQLITE_MAX_MMAP_SIZE>0
57262/* The page getter for when memory-mapped I/O is enabled */
57263static int getPageMMap(
57264 Pager *pPager, /* The pager open on the database file */
57265 Pgno pgno, /* Page number to fetch */
57266 DbPage **ppPage, /* Write a pointer to the page here */
57267 int flags /* PAGER_GET_XXX flags */
57268){
57269 int rc = SQLITE_OK;
57270 PgHdr *pPg = 0;
57271 u32 iFrame = 0; /* Frame to read from WAL file */
57272
57273 /* It is acceptable to use a read-only (mmap) page for any page except
57274 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
57275 ** flag was specified by the caller. And so long as the db is not a
57276 ** temporary or in-memory database. */
57277 const int bMmapOk = (pgno>1
57278 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
57279 );
57280
57281 assert( USEFETCH(pPager) );
57282
57283 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
57284 ** allows the compiler optimizer to reuse the results of the "pgno>1"
57285 ** test in the previous statement, and avoid testing pgno==0 in the
57286 ** common case where pgno is large. */
57287 if( pgno<=1 && pgno==0 ){
57288 return SQLITE_CORRUPT_BKPT;
57289 }
57290 assert( pPager->eState>=PAGER_READER );
57291 assert( assert_pager_state(pPager) );
57292 assert( pPager->hasHeldSharedLock==1 );
57293 assert( pPager->errCode==SQLITE_OK );
57294
57295 if( bMmapOk && pagerUseWal(pPager) ){
57296 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
57297 if( rc!=SQLITE_OK ){
57298 *ppPage = 0;
57299 return rc;
57300 }
57301 }
57302 if( bMmapOk && iFrame==0 ){
57303 void *pData = 0;
57304 rc = sqlite3OsFetch(pPager->fd,
57305 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
57306 );
57307 if( rc==SQLITE_OK && pData ){
57308 if( pPager->eState>PAGER_READER || pPager->tempFile ){
57309 pPg = sqlite3PagerLookup(pPager, pgno);
57310 }
57311 if( pPg==0 ){
57312 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
57313 }else{
57314 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
57315 }
57316 if( pPg ){
57317 assert( rc==SQLITE_OK );
57318 *ppPage = pPg;
57319 return SQLITE_OK;
57320 }
57321 }
57322 if( rc!=SQLITE_OK ){
57323 *ppPage = 0;
57324 return rc;
57325 }
57326 }
57327 return getPageNormal(pPager, pgno, ppPage, flags);
57328}
57329#endif /* SQLITE_MAX_MMAP_SIZE>0 */
57330
57331/* The page getter method for when the pager is an error state */
57332static int getPageError(
57333 Pager *pPager, /* The pager open on the database file */
57334 Pgno pgno, /* Page number to fetch */
57335 DbPage **ppPage, /* Write a pointer to the page here */
57336 int flags /* PAGER_GET_XXX flags */
57337){
57338 UNUSED_PARAMETER(pgno);
57339 UNUSED_PARAMETER(flags);
57340 assert( pPager->errCode!=SQLITE_OK );
57341 *ppPage = 0;
57342 return pPager->errCode;
57343}
57344
57345
57346/* Dispatch all page fetch requests to the appropriate getter method.
57347*/
57348SQLITE_PRIVATE int sqlite3PagerGet(
57349 Pager *pPager, /* The pager open on the database file */
57350 Pgno pgno, /* Page number to fetch */
57351 DbPage **ppPage, /* Write a pointer to the page here */
57352 int flags /* PAGER_GET_XXX flags */
57353){
57354 return pPager->xGet(pPager, pgno, ppPage, flags);
57355}
57356
57357/*
57358** Acquire a page if it is already in the in-memory cache. Do
57359** not read the page from disk. Return a pointer to the page,
57360** or 0 if the page is not in cache.
57361**
57362** See also sqlite3PagerGet(). The difference between this routine
57363** and sqlite3PagerGet() is that _get() will go to the disk and read
57364** in the page if the page is not already in cache. This routine
57365** returns NULL if the page is not in cache or if a disk I/O error
57366** has ever happened.
57367*/
57368SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
57369 sqlite3_pcache_page *pPage;
57370 assert( pPager!=0 );
57371 assert( pgno!=0 );
57372 assert( pPager->pPCache!=0 );
57373 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
57374 assert( pPage==0 || pPager->hasHeldSharedLock );
57375 if( pPage==0 ) return 0;
57376 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
57377}
57378
57379/*
57380** Release a page reference.
57381**
57382** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
57383** used if we know that the page being released is not the last page.
57384** The btree layer always holds page1 open until the end, so these first
57385** to routines can be used to release any page other than BtShared.pPage1.
57386**
57387** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
57388** checks the total number of outstanding pages and if the number of
57389** pages reaches zero it drops the database lock.
57390*/
57391SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
57392 TESTONLY( Pager *pPager = pPg->pPager; )
57393 assert( pPg!=0 );
57394 if( pPg->flags & PGHDR_MMAP ){
57395 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
57396 pagerReleaseMapPage(pPg);
57397 }else{
57398 sqlite3PcacheRelease(pPg);
57399 }
57400 /* Do not use this routine to release the last reference to page1 */
57401 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
57402}
57403SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
57404 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
57405}
57406SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
57407 Pager *pPager;
57408 assert( pPg!=0 );
57409 assert( pPg->pgno==1 );
57410 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
57411 pPager = pPg->pPager;
57412 sqlite3PcacheRelease(pPg);
57413 pagerUnlockIfUnused(pPager);
57414}
57415
57416/*
57417** This function is called at the start of every write transaction.
57418** There must already be a RESERVED or EXCLUSIVE lock on the database
57419** file when this routine is called.
57420**
57421** Open the journal file for pager pPager and write a journal header
57422** to the start of it. If there are active savepoints, open the sub-journal
57423** as well. This function is only used when the journal file is being
57424** opened to write a rollback log for a transaction. It is not used
57425** when opening a hot journal file to roll it back.
57426**
57427** If the journal file is already open (as it may be in exclusive mode),
57428** then this function just writes a journal header to the start of the
57429** already open file.
57430**
57431** Whether or not the journal file is opened by this function, the
57432** Pager.pInJournal bitvec structure is allocated.
57433**
57434** Return SQLITE_OK if everything is successful. Otherwise, return
57435** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
57436** an IO error code if opening or writing the journal file fails.
57437*/
57438static int pager_open_journal(Pager *pPager){
57439 int rc = SQLITE_OK; /* Return code */
57440 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
57441
57442 assert( pPager->eState==PAGER_WRITER_LOCKED );
57443 assert( assert_pager_state(pPager) );
57444 assert( pPager->pInJournal==0 );
57445
57446 /* If already in the error state, this function is a no-op. But on
57447 ** the other hand, this routine is never called if we are already in
57448 ** an error state. */
57449 if( NEVER(pPager->errCode) ) return pPager->errCode;
57450
57451 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
57452 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
57453 if( pPager->pInJournal==0 ){
57454 return SQLITE_NOMEM_BKPT;
57455 }
57456
57457 /* Open the journal file if it is not already open. */
57458 if( !isOpen(pPager->jfd) ){
57459 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
57460 sqlite3MemJournalOpen(pPager->jfd);
57461 }else{
57462 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
57463 int nSpill;
57464
57465 if( pPager->tempFile ){
57466 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
57467 nSpill = sqlite3Config.nStmtSpill;
57468 }else{
57469 flags |= SQLITE_OPEN_MAIN_JOURNAL;
57470 nSpill = jrnlBufferSize(pPager);
57471 }
57472
57473 /* Verify that the database still has the same name as it did when
57474 ** it was originally opened. */
57475 rc = databaseIsUnmoved(pPager);
57476 if( rc==SQLITE_OK ){
57477 rc = sqlite3JournalOpen (
57478 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
57479 );
57480 }
57481 }
57482 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
57483 }
57484
57485
57486 /* Write the first journal header to the journal file and open
57487 ** the sub-journal if necessary.
57488 */
57489 if( rc==SQLITE_OK ){
57490 /* TODO: Check if all of these are really required. */
57491 pPager->nRec = 0;
57492 pPager->journalOff = 0;
57493 pPager->setSuper = 0;
57494 pPager->journalHdr = 0;
57495 rc = writeJournalHdr(pPager);
57496 }
57497 }
57498
57499 if( rc!=SQLITE_OK ){
57500 sqlite3BitvecDestroy(pPager->pInJournal);
57501 pPager->pInJournal = 0;
57502 }else{
57503 assert( pPager->eState==PAGER_WRITER_LOCKED );
57504 pPager->eState = PAGER_WRITER_CACHEMOD;
57505 }
57506
57507 return rc;
57508}
57509
57510/*
57511** Begin a write-transaction on the specified pager object. If a
57512** write-transaction has already been opened, this function is a no-op.
57513**
57514** If the exFlag argument is false, then acquire at least a RESERVED
57515** lock on the database file. If exFlag is true, then acquire at least
57516** an EXCLUSIVE lock. If such a lock is already held, no locking
57517** functions need be called.
57518**
57519** If the subjInMemory argument is non-zero, then any sub-journal opened
57520** within this transaction will be opened as an in-memory file. This
57521** has no effect if the sub-journal is already opened (as it may be when
57522** running in exclusive mode) or if the transaction does not require a
57523** sub-journal. If the subjInMemory argument is zero, then any required
57524** sub-journal is implemented in-memory if pPager is an in-memory database,
57525** or using a temporary file otherwise.
57526*/
57527SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
57528 int rc = SQLITE_OK;
57529
57530 if( pPager->errCode ) return pPager->errCode;
57531 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
57532 pPager->subjInMemory = (u8)subjInMemory;
57533
57534 if( ALWAYS(pPager->eState==PAGER_READER) ){
57535 assert( pPager->pInJournal==0 );
57536
57537 if( pagerUseWal(pPager) ){
57538 /* If the pager is configured to use locking_mode=exclusive, and an
57539 ** exclusive lock on the database is not already held, obtain it now.
57540 */
57541 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
57542 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
57543 if( rc!=SQLITE_OK ){
57544 return rc;
57545 }
57546 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
57547 }
57548
57549 /* Grab the write lock on the log file. If successful, upgrade to
57550 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
57551 ** The busy-handler is not invoked if another connection already
57552 ** holds the write-lock. If possible, the upper layer will call it.
57553 */
57554 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
57555 }else{
57556 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
57557 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
57558 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
57559 ** lock, but not when obtaining the RESERVED lock.
57560 */
57561 rc = pagerLockDb(pPager, RESERVED_LOCK);
57562 if( rc==SQLITE_OK && exFlag ){
57563 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
57564 }
57565 }
57566
57567 if( rc==SQLITE_OK ){
57568 /* Change to WRITER_LOCKED state.
57569 **
57570 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
57571 ** when it has an open transaction, but never to DBMOD or FINISHED.
57572 ** This is because in those states the code to roll back savepoint
57573 ** transactions may copy data from the sub-journal into the database
57574 ** file as well as into the page cache. Which would be incorrect in
57575 ** WAL mode.
57576 */
57577 pPager->eState = PAGER_WRITER_LOCKED;
57578 pPager->dbHintSize = pPager->dbSize;
57579 pPager->dbFileSize = pPager->dbSize;
57580 pPager->dbOrigSize = pPager->dbSize;
57581 pPager->journalOff = 0;
57582 }
57583
57584 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
57585 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
57586 assert( assert_pager_state(pPager) );
57587 }
57588
57589 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
57590 return rc;
57591}
57592
57593/*
57594** Write page pPg onto the end of the rollback journal.
57595*/
57596static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
57597 Pager *pPager = pPg->pPager;
57598 int rc;
57599 u32 cksum;
57600 char *pData2;
57601 i64 iOff = pPager->journalOff;
57602
57603 /* We should never write to the journal file the page that
57604 ** contains the database locks. The following assert verifies
57605 ** that we do not. */
57606 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
57607
57608 assert( pPager->journalHdr<=pPager->journalOff );
57609 pData2 = pPg->pData;
57610 cksum = pager_cksum(pPager, (u8*)pData2);
57611
57612 /* Even if an IO or diskfull error occurs while journalling the
57613 ** page in the block above, set the need-sync flag for the page.
57614 ** Otherwise, when the transaction is rolled back, the logic in
57615 ** playback_one_page() will think that the page needs to be restored
57616 ** in the database file. And if an IO error occurs while doing so,
57617 ** then corruption may follow.
57618 */
57619 pPg->flags |= PGHDR_NEED_SYNC;
57620
57621 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
57622 if( rc!=SQLITE_OK ) return rc;
57623 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
57624 if( rc!=SQLITE_OK ) return rc;
57625 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
57626 if( rc!=SQLITE_OK ) return rc;
57627
57628 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
57629 pPager->journalOff, pPager->pageSize));
57630 PAGER_INCR(sqlite3_pager_writej_count);
57631 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
57632 PAGERID(pPager), pPg->pgno,
57633 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
57634
57635 pPager->journalOff += 8 + pPager->pageSize;
57636 pPager->nRec++;
57637 assert( pPager->pInJournal!=0 );
57638 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
57639 testcase( rc==SQLITE_NOMEM );
57640 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
57641 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
57642 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
57643 return rc;
57644}
57645
57646/*
57647** Mark a single data page as writeable. The page is written into the
57648** main journal or sub-journal as required. If the page is written into
57649** one of the journals, the corresponding bit is set in the
57650** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
57651** of any open savepoints as appropriate.
57652*/
57653static int pager_write(PgHdr *pPg){
57654 Pager *pPager = pPg->pPager;
57655 int rc = SQLITE_OK;
57656
57657 /* This routine is not called unless a write-transaction has already
57658 ** been started. The journal file may or may not be open at this point.
57659 ** It is never called in the ERROR state.
57660 */
57661 assert( pPager->eState==PAGER_WRITER_LOCKED
57662 || pPager->eState==PAGER_WRITER_CACHEMOD
57663 || pPager->eState==PAGER_WRITER_DBMOD
57664 );
57665 assert( assert_pager_state(pPager) );
57666 assert( pPager->errCode==0 );
57667 assert( pPager->readOnly==0 );
57668 CHECK_PAGE(pPg);
57669
57670 /* The journal file needs to be opened. Higher level routines have already
57671 ** obtained the necessary locks to begin the write-transaction, but the
57672 ** rollback journal might not yet be open. Open it now if this is the case.
57673 **
57674 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
57675 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
57676 ** an error might occur and the pager would end up in WRITER_LOCKED state
57677 ** with pages marked as dirty in the cache.
57678 */
57679 if( pPager->eState==PAGER_WRITER_LOCKED ){
57680 rc = pager_open_journal(pPager);
57681 if( rc!=SQLITE_OK ) return rc;
57682 }
57683 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
57684 assert( assert_pager_state(pPager) );
57685
57686 /* Mark the page that is about to be modified as dirty. */
57687 sqlite3PcacheMakeDirty(pPg);
57688
57689 /* If a rollback journal is in use, them make sure the page that is about
57690 ** to change is in the rollback journal, or if the page is a new page off
57691 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
57692 */
57693 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
57694 if( pPager->pInJournal!=0
57695 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
57696 ){
57697 assert( pagerUseWal(pPager)==0 );
57698 if( pPg->pgno<=pPager->dbOrigSize ){
57699 rc = pagerAddPageToRollbackJournal(pPg);
57700 if( rc!=SQLITE_OK ){
57701 return rc;
57702 }
57703 }else{
57704 if( pPager->eState!=PAGER_WRITER_DBMOD ){
57705 pPg->flags |= PGHDR_NEED_SYNC;
57706 }
57707 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
57708 PAGERID(pPager), pPg->pgno,
57709 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
57710 }
57711 }
57712
57713 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
57714 ** and before writing the page into the rollback journal. Wait until now,
57715 ** after the page has been successfully journalled, before setting the
57716 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
57717 */
57718 pPg->flags |= PGHDR_WRITEABLE;
57719
57720 /* If the statement journal is open and the page is not in it,
57721 ** then write the page into the statement journal.
57722 */
57723 if( pPager->nSavepoint>0 ){
57724 rc = subjournalPageIfRequired(pPg);
57725 }
57726
57727 /* Update the database size and return. */
57728 if( pPager->dbSize<pPg->pgno ){
57729 pPager->dbSize = pPg->pgno;
57730 }
57731 return rc;
57732}
57733
57734/*
57735** This is a variant of sqlite3PagerWrite() that runs when the sector size
57736** is larger than the page size. SQLite makes the (reasonable) assumption that
57737** all bytes of a sector are written together by hardware. Hence, all bytes of
57738** a sector need to be journalled in case of a power loss in the middle of
57739** a write.
57740**
57741** Usually, the sector size is less than or equal to the page size, in which
57742** case pages can be individually written. This routine only runs in the
57743** exceptional case where the page size is smaller than the sector size.
57744*/
57745static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
57746 int rc = SQLITE_OK; /* Return code */
57747 Pgno nPageCount; /* Total number of pages in database file */
57748 Pgno pg1; /* First page of the sector pPg is located on. */
57749 int nPage = 0; /* Number of pages starting at pg1 to journal */
57750 int ii; /* Loop counter */
57751 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
57752 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
57753 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
57754
57755 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
57756 ** a journal header to be written between the pages journaled by
57757 ** this function.
57758 */
57759 assert( !MEMDB );
57760 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
57761 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
57762
57763 /* This trick assumes that both the page-size and sector-size are
57764 ** an integer power of 2. It sets variable pg1 to the identifier
57765 ** of the first page of the sector pPg is located on.
57766 */
57767 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
57768
57769 nPageCount = pPager->dbSize;
57770 if( pPg->pgno>nPageCount ){
57771 nPage = (pPg->pgno - pg1)+1;
57772 }else if( (pg1+nPagePerSector-1)>nPageCount ){
57773 nPage = nPageCount+1-pg1;
57774 }else{
57775 nPage = nPagePerSector;
57776 }
57777 assert(nPage>0);
57778 assert(pg1<=pPg->pgno);
57779 assert((pg1+nPage)>pPg->pgno);
57780
57781 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
57782 Pgno pg = pg1+ii;
57783 PgHdr *pPage;
57784 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
57785 if( pg!=PAGER_MJ_PGNO(pPager) ){
57786 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
57787 if( rc==SQLITE_OK ){
57788 rc = pager_write(pPage);
57789 if( pPage->flags&PGHDR_NEED_SYNC ){
57790 needSync = 1;
57791 }
57792 sqlite3PagerUnrefNotNull(pPage);
57793 }
57794 }
57795 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
57796 if( pPage->flags&PGHDR_NEED_SYNC ){
57797 needSync = 1;
57798 }
57799 sqlite3PagerUnrefNotNull(pPage);
57800 }
57801 }
57802
57803 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
57804 ** starting at pg1, then it needs to be set for all of them. Because
57805 ** writing to any of these nPage pages may damage the others, the
57806 ** journal file must contain sync()ed copies of all of them
57807 ** before any of them can be written out to the database file.
57808 */
57809 if( rc==SQLITE_OK && needSync ){
57810 assert( !MEMDB );
57811 for(ii=0; ii<nPage; ii++){
57812 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
57813 if( pPage ){
57814 pPage->flags |= PGHDR_NEED_SYNC;
57815 sqlite3PagerUnrefNotNull(pPage);
57816 }
57817 }
57818 }
57819
57820 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
57821 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
57822 return rc;
57823}
57824
57825/*
57826** Mark a data page as writeable. This routine must be called before
57827** making changes to a page. The caller must check the return value
57828** of this function and be careful not to change any page data unless
57829** this routine returns SQLITE_OK.
57830**
57831** The difference between this function and pager_write() is that this
57832** function also deals with the special case where 2 or more pages
57833** fit on a single disk sector. In this case all co-resident pages
57834** must have been written to the journal file before returning.
57835**
57836** If an error occurs, SQLITE_NOMEM or an IO error code is returned
57837** as appropriate. Otherwise, SQLITE_OK.
57838*/
57839SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
57840 Pager *pPager = pPg->pPager;
57841 assert( (pPg->flags & PGHDR_MMAP)==0 );
57842 assert( pPager->eState>=PAGER_WRITER_LOCKED );
57843 assert( assert_pager_state(pPager) );
57844 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
57845 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
57846 return SQLITE_OK;
57847 }else if( pPager->errCode ){
57848 return pPager->errCode;
57849 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
57850 assert( pPager->tempFile==0 );
57851 return pagerWriteLargeSector(pPg);
57852 }else{
57853 return pager_write(pPg);
57854 }
57855}
57856
57857/*
57858** Return TRUE if the page given in the argument was previously passed
57859** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
57860** to change the content of the page.
57861*/
57862#ifndef NDEBUG
57863SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
57864 return pPg->flags & PGHDR_WRITEABLE;
57865}
57866#endif
57867
57868/*
57869** A call to this routine tells the pager that it is not necessary to
57870** write the information on page pPg back to the disk, even though
57871** that page might be marked as dirty. This happens, for example, when
57872** the page has been added as a leaf of the freelist and so its
57873** content no longer matters.
57874**
57875** The overlying software layer calls this routine when all of the data
57876** on the given page is unused. The pager marks the page as clean so
57877** that it does not get written to disk.
57878**
57879** Tests show that this optimization can quadruple the speed of large
57880** DELETE operations.
57881**
57882** This optimization cannot be used with a temp-file, as the page may
57883** have been dirty at the start of the transaction. In that case, if
57884** memory pressure forces page pPg out of the cache, the data does need
57885** to be written out to disk so that it may be read back in if the
57886** current transaction is rolled back.
57887*/
57888SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
57889 Pager *pPager = pPg->pPager;
57890 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
57891 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
57892 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
57893 pPg->flags |= PGHDR_DONT_WRITE;
57894 pPg->flags &= ~PGHDR_WRITEABLE;
57895 testcase( pPg->flags & PGHDR_NEED_SYNC );
57896 pager_set_pagehash(pPg);
57897 }
57898}
57899
57900/*
57901** This routine is called to increment the value of the database file
57902** change-counter, stored as a 4-byte big-endian integer starting at
57903** byte offset 24 of the pager file. The secondary change counter at
57904** 92 is also updated, as is the SQLite version number at offset 96.
57905**
57906** But this only happens if the pPager->changeCountDone flag is false.
57907** To avoid excess churning of page 1, the update only happens once.
57908** See also the pager_write_changecounter() routine that does an
57909** unconditional update of the change counters.
57910**
57911** If the isDirectMode flag is zero, then this is done by calling
57912** sqlite3PagerWrite() on page 1, then modifying the contents of the
57913** page data. In this case the file will be updated when the current
57914** transaction is committed.
57915**
57916** The isDirectMode flag may only be non-zero if the library was compiled
57917** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
57918** if isDirect is non-zero, then the database file is updated directly
57919** by writing an updated version of page 1 using a call to the
57920** sqlite3OsWrite() function.
57921*/
57922static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
57923 int rc = SQLITE_OK;
57924
57925 assert( pPager->eState==PAGER_WRITER_CACHEMOD
57926 || pPager->eState==PAGER_WRITER_DBMOD
57927 );
57928 assert( assert_pager_state(pPager) );
57929
57930 /* Declare and initialize constant integer 'isDirect'. If the
57931 ** atomic-write optimization is enabled in this build, then isDirect
57932 ** is initialized to the value passed as the isDirectMode parameter
57933 ** to this function. Otherwise, it is always set to zero.
57934 **
57935 ** The idea is that if the atomic-write optimization is not
57936 ** enabled at compile time, the compiler can omit the tests of
57937 ** 'isDirect' below, as well as the block enclosed in the
57938 ** "if( isDirect )" condition.
57939 */
57940#ifndef SQLITE_ENABLE_ATOMIC_WRITE
57941# define DIRECT_MODE 0
57942 assert( isDirectMode==0 );
57943 UNUSED_PARAMETER(isDirectMode);
57944#else
57945# define DIRECT_MODE isDirectMode
57946#endif
57947
57948 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
57949 PgHdr *pPgHdr; /* Reference to page 1 */
57950
57951 assert( !pPager->tempFile && isOpen(pPager->fd) );
57952
57953 /* Open page 1 of the file for writing. */
57954 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
57955 assert( pPgHdr==0 || rc==SQLITE_OK );
57956
57957 /* If page one was fetched successfully, and this function is not
57958 ** operating in direct-mode, make page 1 writable. When not in
57959 ** direct mode, page 1 is always held in cache and hence the PagerGet()
57960 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
57961 */
57962 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
57963 rc = sqlite3PagerWrite(pPgHdr);
57964 }
57965
57966 if( rc==SQLITE_OK ){
57967 /* Actually do the update of the change counter */
57968 pager_write_changecounter(pPgHdr);
57969
57970 /* If running in direct mode, write the contents of page 1 to the file. */
57971 if( DIRECT_MODE ){
57972 const void *zBuf;
57973 assert( pPager->dbFileSize>0 );
57974 zBuf = pPgHdr->pData;
57975 if( rc==SQLITE_OK ){
57976 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
57977 pPager->aStat[PAGER_STAT_WRITE]++;
57978 }
57979 if( rc==SQLITE_OK ){
57980 /* Update the pager's copy of the change-counter. Otherwise, the
57981 ** next time a read transaction is opened the cache will be
57982 ** flushed (as the change-counter values will not match). */
57983 const void *pCopy = (const void *)&((const char *)zBuf)[24];
57984 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
57985 pPager->changeCountDone = 1;
57986 }
57987 }else{
57988 pPager->changeCountDone = 1;
57989 }
57990 }
57991
57992 /* Release the page reference. */
57993 sqlite3PagerUnref(pPgHdr);
57994 }
57995 return rc;
57996}
57997
57998/*
57999** Sync the database file to disk. This is a no-op for in-memory databases
58000** or pages with the Pager.noSync flag set.
58001**
58002** If successful, or if called on a pager for which it is a no-op, this
58003** function returns SQLITE_OK. Otherwise, an IO error code is returned.
58004*/
58005SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zSuper){
58006 int rc = SQLITE_OK;
58007 void *pArg = (void*)zSuper;
58008 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
58009 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
58010 if( rc==SQLITE_OK && !pPager->noSync ){
58011 assert( !MEMDB );
58012 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
58013 }
58014 return rc;
58015}
58016
58017/*
58018** This function may only be called while a write-transaction is active in
58019** rollback. If the connection is in WAL mode, this call is a no-op.
58020** Otherwise, if the connection does not already have an EXCLUSIVE lock on
58021** the database file, an attempt is made to obtain one.
58022**
58023** If the EXCLUSIVE lock is already held or the attempt to obtain it is
58024** successful, or the connection is in WAL mode, SQLITE_OK is returned.
58025** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
58026** returned.
58027*/
58028SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
58029 int rc = pPager->errCode;
58030 assert( assert_pager_state(pPager) );
58031 if( rc==SQLITE_OK ){
58032 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58033 || pPager->eState==PAGER_WRITER_DBMOD
58034 || pPager->eState==PAGER_WRITER_LOCKED
58035 );
58036 assert( assert_pager_state(pPager) );
58037 if( 0==pagerUseWal(pPager) ){
58038 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
58039 }
58040 }
58041 return rc;
58042}
58043
58044/*
58045** Sync the database file for the pager pPager. zSuper points to the name
58046** of a super-journal file that should be written into the individual
58047** journal file. zSuper may be NULL, which is interpreted as no
58048** super-journal (a single database transaction).
58049**
58050** This routine ensures that:
58051**
58052** * The database file change-counter is updated,
58053** * the journal is synced (unless the atomic-write optimization is used),
58054** * all dirty pages are written to the database file,
58055** * the database file is truncated (if required), and
58056** * the database file synced.
58057**
58058** The only thing that remains to commit the transaction is to finalize
58059** (delete, truncate or zero the first part of) the journal file (or
58060** delete the super-journal file if specified).
58061**
58062** Note that if zSuper==NULL, this does not overwrite a previous value
58063** passed to an sqlite3PagerCommitPhaseOne() call.
58064**
58065** If the final parameter - noSync - is true, then the database file itself
58066** is not synced. The caller must call sqlite3PagerSync() directly to
58067** sync the database file before calling CommitPhaseTwo() to delete the
58068** journal file in this case.
58069*/
58070SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
58071 Pager *pPager, /* Pager object */
58072 const char *zSuper, /* If not NULL, the super-journal name */
58073 int noSync /* True to omit the xSync on the db file */
58074){
58075 int rc = SQLITE_OK; /* Return code */
58076
58077 assert( pPager->eState==PAGER_WRITER_LOCKED
58078 || pPager->eState==PAGER_WRITER_CACHEMOD
58079 || pPager->eState==PAGER_WRITER_DBMOD
58080 || pPager->eState==PAGER_ERROR
58081 );
58082 assert( assert_pager_state(pPager) );
58083
58084 /* If a prior error occurred, report that error again. */
58085 if( NEVER(pPager->errCode) ) return pPager->errCode;
58086
58087 /* Provide the ability to easily simulate an I/O error during testing */
58088 if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
58089
58090 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
58091 pPager->zFilename, zSuper, pPager->dbSize));
58092
58093 /* If no database changes have been made, return early. */
58094 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
58095
58096 assert( MEMDB==0 || pPager->tempFile );
58097 assert( isOpen(pPager->fd) || pPager->tempFile );
58098 if( 0==pagerFlushOnCommit(pPager, 1) ){
58099 /* If this is an in-memory db, or no pages have been written to, or this
58100 ** function has already been called, it is mostly a no-op. However, any
58101 ** backup in progress needs to be restarted. */
58102 sqlite3BackupRestart(pPager->pBackup);
58103 }else{
58104 PgHdr *pList;
58105 if( pagerUseWal(pPager) ){
58106 PgHdr *pPageOne = 0;
58107 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58108 if( pList==0 ){
58109 /* Must have at least one page for the WAL commit flag.
58110 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
58111 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
58112 pList = pPageOne;
58113 pList->pDirty = 0;
58114 }
58115 assert( rc==SQLITE_OK );
58116 if( ALWAYS(pList) ){
58117 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
58118 }
58119 sqlite3PagerUnref(pPageOne);
58120 if( rc==SQLITE_OK ){
58121 sqlite3PcacheCleanAll(pPager->pPCache);
58122 }
58123 }else{
58124 /* The bBatch boolean is true if the batch-atomic-write commit method
58125 ** should be used. No rollback journal is created if batch-atomic-write
58126 ** is enabled.
58127 */
58128#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58129 sqlite3_file *fd = pPager->fd;
58130 int bBatch = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
58131 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
58132 && !pPager->noSync
58133 && sqlite3JournalIsInMemory(pPager->jfd);
58134#else
58135# define bBatch 0
58136#endif
58137
58138#ifdef SQLITE_ENABLE_ATOMIC_WRITE
58139 /* The following block updates the change-counter. Exactly how it
58140 ** does this depends on whether or not the atomic-update optimization
58141 ** was enabled at compile time, and if this transaction meets the
58142 ** runtime criteria to use the operation:
58143 **
58144 ** * The file-system supports the atomic-write property for
58145 ** blocks of size page-size, and
58146 ** * This commit is not part of a multi-file transaction, and
58147 ** * Exactly one page has been modified and store in the journal file.
58148 **
58149 ** If the optimization was not enabled at compile time, then the
58150 ** pager_incr_changecounter() function is called to update the change
58151 ** counter in 'indirect-mode'. If the optimization is compiled in but
58152 ** is not applicable to this transaction, call sqlite3JournalCreate()
58153 ** to make sure the journal file has actually been created, then call
58154 ** pager_incr_changecounter() to update the change-counter in indirect
58155 ** mode.
58156 **
58157 ** Otherwise, if the optimization is both enabled and applicable,
58158 ** then call pager_incr_changecounter() to update the change-counter
58159 ** in 'direct' mode. In this case the journal file will never be
58160 ** created for this transaction.
58161 */
58162 if( bBatch==0 ){
58163 PgHdr *pPg;
58164 assert( isOpen(pPager->jfd)
58165 || pPager->journalMode==PAGER_JOURNALMODE_OFF
58166 || pPager->journalMode==PAGER_JOURNALMODE_WAL
58167 );
58168 if( !zSuper && isOpen(pPager->jfd)
58169 && pPager->journalOff==jrnlBufferSize(pPager)
58170 && pPager->dbSize>=pPager->dbOrigSize
58171 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
58172 ){
58173 /* Update the db file change counter via the direct-write method. The
58174 ** following call will modify the in-memory representation of page 1
58175 ** to include the updated change counter and then write page 1
58176 ** directly to the database file. Because of the atomic-write
58177 ** property of the host file-system, this is safe.
58178 */
58179 rc = pager_incr_changecounter(pPager, 1);
58180 }else{
58181 rc = sqlite3JournalCreate(pPager->jfd);
58182 if( rc==SQLITE_OK ){
58183 rc = pager_incr_changecounter(pPager, 0);
58184 }
58185 }
58186 }
58187#else /* SQLITE_ENABLE_ATOMIC_WRITE */
58188#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58189 if( zSuper ){
58190 rc = sqlite3JournalCreate(pPager->jfd);
58191 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58192 assert( bBatch==0 );
58193 }
58194#endif
58195 rc = pager_incr_changecounter(pPager, 0);
58196#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
58197 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58198
58199 /* Write the super-journal name into the journal file. If a
58200 ** super-journal file name has already been written to the journal file,
58201 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
58202 */
58203 rc = writeSuperJournal(pPager, zSuper);
58204 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58205
58206 /* Sync the journal file and write all dirty pages to the database.
58207 ** If the atomic-update optimization is being used, this sync will not
58208 ** create the journal file or perform any real IO.
58209 **
58210 ** Because the change-counter page was just modified, unless the
58211 ** atomic-update optimization is used it is almost certain that the
58212 ** journal requires a sync here. However, in locking_mode=exclusive
58213 ** on a system under memory pressure it is just possible that this is
58214 ** not the case. In this case it is likely enough that the redundant
58215 ** xSync() call will be changed to a no-op by the OS anyhow.
58216 */
58217 rc = syncJournal(pPager, 0);
58218 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58219
58220 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58221#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58222 if( bBatch ){
58223 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
58224 if( rc==SQLITE_OK ){
58225 rc = pager_write_pagelist(pPager, pList);
58226 if( rc==SQLITE_OK ){
58227 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
58228 }
58229 if( rc!=SQLITE_OK ){
58230 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
58231 }
58232 }
58233
58234 if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
58235 rc = sqlite3JournalCreate(pPager->jfd);
58236 if( rc!=SQLITE_OK ){
58237 sqlite3OsClose(pPager->jfd);
58238 goto commit_phase_one_exit;
58239 }
58240 bBatch = 0;
58241 }else{
58242 sqlite3OsClose(pPager->jfd);
58243 }
58244 }
58245#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
58246
58247 if( bBatch==0 ){
58248 rc = pager_write_pagelist(pPager, pList);
58249 }
58250 if( rc!=SQLITE_OK ){
58251 assert( rc!=SQLITE_IOERR_BLOCKED );
58252 goto commit_phase_one_exit;
58253 }
58254 sqlite3PcacheCleanAll(pPager->pPCache);
58255
58256 /* If the file on disk is smaller than the database image, use
58257 ** pager_truncate to grow the file here. This can happen if the database
58258 ** image was extended as part of the current transaction and then the
58259 ** last page in the db image moved to the free-list. In this case the
58260 ** last page is never written out to disk, leaving the database file
58261 ** undersized. Fix this now if it is the case. */
58262 if( pPager->dbSize>pPager->dbFileSize ){
58263 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
58264 assert( pPager->eState==PAGER_WRITER_DBMOD );
58265 rc = pager_truncate(pPager, nNew);
58266 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
58267 }
58268
58269 /* Finally, sync the database file. */
58270 if( !noSync ){
58271 rc = sqlite3PagerSync(pPager, zSuper);
58272 }
58273 IOTRACE(("DBSYNC %p\n", pPager))
58274 }
58275 }
58276
58277commit_phase_one_exit:
58278 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
58279 pPager->eState = PAGER_WRITER_FINISHED;
58280 }
58281 return rc;
58282}
58283
58284
58285/*
58286** When this function is called, the database file has been completely
58287** updated to reflect the changes made by the current transaction and
58288** synced to disk. The journal file still exists in the file-system
58289** though, and if a failure occurs at this point it will eventually
58290** be used as a hot-journal and the current transaction rolled back.
58291**
58292** This function finalizes the journal file, either by deleting,
58293** truncating or partially zeroing it, so that it cannot be used
58294** for hot-journal rollback. Once this is done the transaction is
58295** irrevocably committed.
58296**
58297** If an error occurs, an IO error code is returned and the pager
58298** moves into the error state. Otherwise, SQLITE_OK is returned.
58299*/
58300SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
58301 int rc = SQLITE_OK; /* Return code */
58302
58303 /* This routine should not be called if a prior error has occurred.
58304 ** But if (due to a coding error elsewhere in the system) it does get
58305 ** called, just return the same error code without doing anything. */
58306 if( NEVER(pPager->errCode) ) return pPager->errCode;
58307 pPager->iDataVersion++;
58308
58309 assert( pPager->eState==PAGER_WRITER_LOCKED
58310 || pPager->eState==PAGER_WRITER_FINISHED
58311 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
58312 );
58313 assert( assert_pager_state(pPager) );
58314
58315 /* An optimization. If the database was not actually modified during
58316 ** this transaction, the pager is running in exclusive-mode and is
58317 ** using persistent journals, then this function is a no-op.
58318 **
58319 ** The start of the journal file currently contains a single journal
58320 ** header with the nRec field set to 0. If such a journal is used as
58321 ** a hot-journal during hot-journal rollback, 0 changes will be made
58322 ** to the database file. So there is no need to zero the journal
58323 ** header. Since the pager is in exclusive mode, there is no need
58324 ** to drop any locks either.
58325 */
58326 if( pPager->eState==PAGER_WRITER_LOCKED
58327 && pPager->exclusiveMode
58328 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
58329 ){
58330 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
58331 pPager->eState = PAGER_READER;
58332 return SQLITE_OK;
58333 }
58334
58335 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
58336 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
58337 return pager_error(pPager, rc);
58338}
58339
58340/*
58341** If a write transaction is open, then all changes made within the
58342** transaction are reverted and the current write-transaction is closed.
58343** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
58344** state if an error occurs.
58345**
58346** If the pager is already in PAGER_ERROR state when this function is called,
58347** it returns Pager.errCode immediately. No work is performed in this case.
58348**
58349** Otherwise, in rollback mode, this function performs two functions:
58350**
58351** 1) It rolls back the journal file, restoring all database file and
58352** in-memory cache pages to the state they were in when the transaction
58353** was opened, and
58354**
58355** 2) It finalizes the journal file, so that it is not used for hot
58356** rollback at any point in the future.
58357**
58358** Finalization of the journal file (task 2) is only performed if the
58359** rollback is successful.
58360**
58361** In WAL mode, all cache-entries containing data modified within the
58362** current transaction are either expelled from the cache or reverted to
58363** their pre-transaction state by re-reading data from the database or
58364** WAL files. The WAL transaction is then closed.
58365*/
58366SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
58367 int rc = SQLITE_OK; /* Return code */
58368 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
58369
58370 /* PagerRollback() is a no-op if called in READER or OPEN state. If
58371 ** the pager is already in the ERROR state, the rollback is not
58372 ** attempted here. Instead, the error code is returned to the caller.
58373 */
58374 assert( assert_pager_state(pPager) );
58375 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
58376 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
58377
58378 if( pagerUseWal(pPager) ){
58379 int rc2;
58380 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
58381 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
58382 if( rc==SQLITE_OK ) rc = rc2;
58383 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
58384 int eState = pPager->eState;
58385 rc = pager_end_transaction(pPager, 0, 0);
58386 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
58387 /* This can happen using journal_mode=off. Move the pager to the error
58388 ** state to indicate that the contents of the cache may not be trusted.
58389 ** Any active readers will get SQLITE_ABORT.
58390 */
58391 pPager->errCode = SQLITE_ABORT;
58392 pPager->eState = PAGER_ERROR;
58393 setGetterMethod(pPager);
58394 return rc;
58395 }
58396 }else{
58397 rc = pager_playback(pPager, 0);
58398 }
58399
58400 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
58401 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
58402 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
58403 || rc==SQLITE_CANTOPEN
58404 );
58405
58406 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
58407 ** cache. So call pager_error() on the way out to make any error persistent.
58408 */
58409 return pager_error(pPager, rc);
58410}
58411
58412/*
58413** Return TRUE if the database file is opened read-only. Return FALSE
58414** if the database is (in theory) writable.
58415*/
58416SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
58417 return pPager->readOnly;
58418}
58419
58420#ifdef SQLITE_DEBUG
58421/*
58422** Return the sum of the reference counts for all pages held by pPager.
58423*/
58424SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
58425 return sqlite3PcacheRefCount(pPager->pPCache);
58426}
58427#endif
58428
58429/*
58430** Return the approximate number of bytes of memory currently
58431** used by the pager and its associated cache.
58432*/
58433SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
58434 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
58435 + 5*sizeof(void*);
58436 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
58437 + sqlite3MallocSize(pPager)
58438 + pPager->pageSize;
58439}
58440
58441/*
58442** Return the number of references to the specified page.
58443*/
58444SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
58445 return sqlite3PcachePageRefcount(pPage);
58446}
58447
58448#ifdef SQLITE_TEST
58449/*
58450** This routine is used for testing and analysis only.
58451*/
58452SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
58453 static int a[11];
58454 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
58455 a[1] = sqlite3PcachePagecount(pPager->pPCache);
58456 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
58457 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
58458 a[4] = pPager->eState;
58459 a[5] = pPager->errCode;
58460 a[6] = pPager->aStat[PAGER_STAT_HIT];
58461 a[7] = pPager->aStat[PAGER_STAT_MISS];
58462 a[8] = 0; /* Used to be pPager->nOvfl */
58463 a[9] = pPager->nRead;
58464 a[10] = pPager->aStat[PAGER_STAT_WRITE];
58465 return a;
58466}
58467#endif
58468
58469/*
58470** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
58471** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
58472** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
58473** it was added later.
58474**
58475** Before returning, *pnVal is incremented by the
58476** current cache hit or miss count, according to the value of eStat. If the
58477** reset parameter is non-zero, the cache hit or miss count is zeroed before
58478** returning.
58479*/
58480SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
58481
58482 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
58483 || eStat==SQLITE_DBSTATUS_CACHE_MISS
58484 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
58485 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
58486 );
58487
58488 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
58489 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
58490 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
58491 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
58492
58493 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
58494 *pnVal += pPager->aStat[eStat];
58495 if( reset ){
58496 pPager->aStat[eStat] = 0;
58497 }
58498}
58499
58500/*
58501** Return true if this is an in-memory or temp-file backed pager.
58502*/
58503SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
58504 return pPager->tempFile;
58505}
58506
58507/*
58508** Check that there are at least nSavepoint savepoints open. If there are
58509** currently less than nSavepoints open, then open one or more savepoints
58510** to make up the difference. If the number of savepoints is already
58511** equal to nSavepoint, then this function is a no-op.
58512**
58513** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
58514** occurs while opening the sub-journal file, then an IO error code is
58515** returned. Otherwise, SQLITE_OK.
58516*/
58517static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
58518 int rc = SQLITE_OK; /* Return code */
58519 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
58520 int ii; /* Iterator variable */
58521 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
58522
58523 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58524 assert( assert_pager_state(pPager) );
58525 assert( nSavepoint>nCurrent && pPager->useJournal );
58526
58527 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
58528 ** if the allocation fails. Otherwise, zero the new portion in case a
58529 ** malloc failure occurs while populating it in the for(...) loop below.
58530 */
58531 aNew = (PagerSavepoint *)sqlite3Realloc(
58532 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
58533 );
58534 if( !aNew ){
58535 return SQLITE_NOMEM_BKPT;
58536 }
58537 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
58538 pPager->aSavepoint = aNew;
58539
58540 /* Populate the PagerSavepoint structures just allocated. */
58541 for(ii=nCurrent; ii<nSavepoint; ii++){
58542 aNew[ii].nOrig = pPager->dbSize;
58543 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
58544 aNew[ii].iOffset = pPager->journalOff;
58545 }else{
58546 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
58547 }
58548 aNew[ii].iSubRec = pPager->nSubRec;
58549 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
58550 if( !aNew[ii].pInSavepoint ){
58551 return SQLITE_NOMEM_BKPT;
58552 }
58553 if( pagerUseWal(pPager) ){
58554 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
58555 }
58556 pPager->nSavepoint = ii+1;
58557 }
58558 assert( pPager->nSavepoint==nSavepoint );
58559 assertTruncateConstraint(pPager);
58560 return rc;
58561}
58562SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
58563 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58564 assert( assert_pager_state(pPager) );
58565
58566 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
58567 return pagerOpenSavepoint(pPager, nSavepoint);
58568 }else{
58569 return SQLITE_OK;
58570 }
58571}
58572
58573
58574/*
58575** This function is called to rollback or release (commit) a savepoint.
58576** The savepoint to release or rollback need not be the most recently
58577** created savepoint.
58578**
58579** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
58580** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
58581** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
58582** that have occurred since the specified savepoint was created.
58583**
58584** The savepoint to rollback or release is identified by parameter
58585** iSavepoint. A value of 0 means to operate on the outermost savepoint
58586** (the first created). A value of (Pager.nSavepoint-1) means operate
58587** on the most recently created savepoint. If iSavepoint is greater than
58588** (Pager.nSavepoint-1), then this function is a no-op.
58589**
58590** If a negative value is passed to this function, then the current
58591** transaction is rolled back. This is different to calling
58592** sqlite3PagerRollback() because this function does not terminate
58593** the transaction or unlock the database, it just restores the
58594** contents of the database to its original state.
58595**
58596** In any case, all savepoints with an index greater than iSavepoint
58597** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
58598** then savepoint iSavepoint is also destroyed.
58599**
58600** This function may return SQLITE_NOMEM if a memory allocation fails,
58601** or an IO error code if an IO error occurs while rolling back a
58602** savepoint. If no errors occur, SQLITE_OK is returned.
58603*/
58604SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
58605 int rc = pPager->errCode;
58606
58607#ifdef SQLITE_ENABLE_ZIPVFS
58608 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
58609#endif
58610
58611 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
58612 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
58613
58614 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
58615 int ii; /* Iterator variable */
58616 int nNew; /* Number of remaining savepoints after this op. */
58617
58618 /* Figure out how many savepoints will still be active after this
58619 ** operation. Store this value in nNew. Then free resources associated
58620 ** with any savepoints that are destroyed by this operation.
58621 */
58622 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
58623 for(ii=nNew; ii<pPager->nSavepoint; ii++){
58624 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
58625 }
58626 pPager->nSavepoint = nNew;
58627
58628 /* If this is a release of the outermost savepoint, truncate
58629 ** the sub-journal to zero bytes in size. */
58630 if( op==SAVEPOINT_RELEASE ){
58631 if( nNew==0 && isOpen(pPager->sjfd) ){
58632 /* Only truncate if it is an in-memory sub-journal. */
58633 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
58634 rc = sqlite3OsTruncate(pPager->sjfd, 0);
58635 assert( rc==SQLITE_OK );
58636 }
58637 pPager->nSubRec = 0;
58638 }
58639 }
58640 /* Else this is a rollback operation, playback the specified savepoint.
58641 ** If this is a temp-file, it is possible that the journal file has
58642 ** not yet been opened. In this case there have been no changes to
58643 ** the database file, so the playback operation can be skipped.
58644 */
58645 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
58646 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
58647 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
58648 assert(rc!=SQLITE_DONE);
58649 }
58650
58651#ifdef SQLITE_ENABLE_ZIPVFS
58652 /* If the cache has been modified but the savepoint cannot be rolled
58653 ** back journal_mode=off, put the pager in the error state. This way,
58654 ** if the VFS used by this pager includes ZipVFS, the entire transaction
58655 ** can be rolled back at the ZipVFS level. */
58656 else if(
58657 pPager->journalMode==PAGER_JOURNALMODE_OFF
58658 && pPager->eState>=PAGER_WRITER_CACHEMOD
58659 ){
58660 pPager->errCode = SQLITE_ABORT;
58661 pPager->eState = PAGER_ERROR;
58662 setGetterMethod(pPager);
58663 }
58664#endif
58665 }
58666
58667 return rc;
58668}
58669
58670/*
58671** Return the full pathname of the database file.
58672**
58673** Except, if the pager is in-memory only, then return an empty string if
58674** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
58675** used to report the filename to the user, for compatibility with legacy
58676** behavior. But when the Btree needs to know the filename for matching to
58677** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
58678** participate in shared-cache.
58679**
58680** The return value to this routine is always safe to use with
58681** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
58682*/
58683SQLITE_PRIVATE const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
58684 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
58685 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
58686}
58687
58688/*
58689** Return the VFS structure for the pager.
58690*/
58691SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
58692 return pPager->pVfs;
58693}
58694
58695/*
58696** Return the file handle for the database file associated
58697** with the pager. This might return NULL if the file has
58698** not yet been opened.
58699*/
58700SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
58701 return pPager->fd;
58702}
58703
58704/*
58705** Return the file handle for the journal file (if it exists).
58706** This will be either the rollback journal or the WAL file.
58707*/
58708SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
58709#if SQLITE_OMIT_WAL
58710 return pPager->jfd;
58711#else
58712 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
58713#endif
58714}
58715
58716/*
58717** Return the full pathname of the journal file.
58718*/
58719SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
58720 return pPager->zJournal;
58721}
58722
58723#ifndef SQLITE_OMIT_AUTOVACUUM
58724/*
58725** Move the page pPg to location pgno in the file.
58726**
58727** There must be no references to the page previously located at
58728** pgno (which we call pPgOld) though that page is allowed to be
58729** in cache. If the page previously located at pgno is not already
58730** in the rollback journal, it is not put there by by this routine.
58731**
58732** References to the page pPg remain valid. Updating any
58733** meta-data associated with pPg (i.e. data stored in the nExtra bytes
58734** allocated along with the page) is the responsibility of the caller.
58735**
58736** A transaction must be active when this routine is called. It used to be
58737** required that a statement transaction was not active, but this restriction
58738** has been removed (CREATE INDEX needs to move a page when a statement
58739** transaction is active).
58740**
58741** If the fourth argument, isCommit, is non-zero, then this page is being
58742** moved as part of a database reorganization just before the transaction
58743** is being committed. In this case, it is guaranteed that the database page
58744** pPg refers to will not be written to again within this transaction.
58745**
58746** This function may return SQLITE_NOMEM or an IO error code if an error
58747** occurs. Otherwise, it returns SQLITE_OK.
58748*/
58749SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
58750 PgHdr *pPgOld; /* The page being overwritten. */
58751 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
58752 int rc; /* Return code */
58753 Pgno origPgno; /* The original page number */
58754
58755 assert( pPg->nRef>0 );
58756 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58757 || pPager->eState==PAGER_WRITER_DBMOD
58758 );
58759 assert( assert_pager_state(pPager) );
58760
58761 /* In order to be able to rollback, an in-memory database must journal
58762 ** the page we are moving from.
58763 */
58764 assert( pPager->tempFile || !MEMDB );
58765 if( pPager->tempFile ){
58766 rc = sqlite3PagerWrite(pPg);
58767 if( rc ) return rc;
58768 }
58769
58770 /* If the page being moved is dirty and has not been saved by the latest
58771 ** savepoint, then save the current contents of the page into the
58772 ** sub-journal now. This is required to handle the following scenario:
58773 **
58774 ** BEGIN;
58775 ** <journal page X, then modify it in memory>
58776 ** SAVEPOINT one;
58777 ** <Move page X to location Y>
58778 ** ROLLBACK TO one;
58779 **
58780 ** If page X were not written to the sub-journal here, it would not
58781 ** be possible to restore its contents when the "ROLLBACK TO one"
58782 ** statement were is processed.
58783 **
58784 ** subjournalPage() may need to allocate space to store pPg->pgno into
58785 ** one or more savepoint bitvecs. This is the reason this function
58786 ** may return SQLITE_NOMEM.
58787 */
58788 if( (pPg->flags & PGHDR_DIRTY)!=0
58789 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
58790 ){
58791 return rc;
58792 }
58793
58794 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
58795 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
58796 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
58797
58798 /* If the journal needs to be sync()ed before page pPg->pgno can
58799 ** be written to, store pPg->pgno in local variable needSyncPgno.
58800 **
58801 ** If the isCommit flag is set, there is no need to remember that
58802 ** the journal needs to be sync()ed before database page pPg->pgno
58803 ** can be written to. The caller has already promised not to write to it.
58804 */
58805 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
58806 needSyncPgno = pPg->pgno;
58807 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
58808 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
58809 assert( pPg->flags&PGHDR_DIRTY );
58810 }
58811
58812 /* If the cache contains a page with page-number pgno, remove it
58813 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
58814 ** page pgno before the 'move' operation, it needs to be retained
58815 ** for the page moved there.
58816 */
58817 pPg->flags &= ~PGHDR_NEED_SYNC;
58818 pPgOld = sqlite3PagerLookup(pPager, pgno);
58819 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
58820 if( pPgOld ){
58821 if( pPgOld->nRef>1 ){
58822 sqlite3PagerUnrefNotNull(pPgOld);
58823 return SQLITE_CORRUPT_BKPT;
58824 }
58825 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
58826 if( pPager->tempFile ){
58827 /* Do not discard pages from an in-memory database since we might
58828 ** need to rollback later. Just move the page out of the way. */
58829 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
58830 }else{
58831 sqlite3PcacheDrop(pPgOld);
58832 }
58833 }
58834
58835 origPgno = pPg->pgno;
58836 sqlite3PcacheMove(pPg, pgno);
58837 sqlite3PcacheMakeDirty(pPg);
58838
58839 /* For an in-memory database, make sure the original page continues
58840 ** to exist, in case the transaction needs to roll back. Use pPgOld
58841 ** as the original page since it has already been allocated.
58842 */
58843 if( pPager->tempFile && pPgOld ){
58844 sqlite3PcacheMove(pPgOld, origPgno);
58845 sqlite3PagerUnrefNotNull(pPgOld);
58846 }
58847
58848 if( needSyncPgno ){
58849 /* If needSyncPgno is non-zero, then the journal file needs to be
58850 ** sync()ed before any data is written to database file page needSyncPgno.
58851 ** Currently, no such page exists in the page-cache and the
58852 ** "is journaled" bitvec flag has been set. This needs to be remedied by
58853 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
58854 ** flag.
58855 **
58856 ** If the attempt to load the page into the page-cache fails, (due
58857 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
58858 ** array. Otherwise, if the page is loaded and written again in
58859 ** this transaction, it may be written to the database file before
58860 ** it is synced into the journal file. This way, it may end up in
58861 ** the journal file twice, but that is not a problem.
58862 */
58863 PgHdr *pPgHdr;
58864 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
58865 if( rc!=SQLITE_OK ){
58866 if( needSyncPgno<=pPager->dbOrigSize ){
58867 assert( pPager->pTmpSpace!=0 );
58868 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
58869 }
58870 return rc;
58871 }
58872 pPgHdr->flags |= PGHDR_NEED_SYNC;
58873 sqlite3PcacheMakeDirty(pPgHdr);
58874 sqlite3PagerUnrefNotNull(pPgHdr);
58875 }
58876
58877 return SQLITE_OK;
58878}
58879#endif
58880
58881/*
58882** The page handle passed as the first argument refers to a dirty page
58883** with a page number other than iNew. This function changes the page's
58884** page number to iNew and sets the value of the PgHdr.flags field to
58885** the value passed as the third parameter.
58886*/
58887SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
58888 assert( pPg->pgno!=iNew );
58889 pPg->flags = flags;
58890 sqlite3PcacheMove(pPg, iNew);
58891}
58892
58893/*
58894** Return a pointer to the data for the specified page.
58895*/
58896SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
58897 assert( pPg->nRef>0 || pPg->pPager->memDb );
58898 return pPg->pData;
58899}
58900
58901/*
58902** Return a pointer to the Pager.nExtra bytes of "extra" space
58903** allocated along with the specified page.
58904*/
58905SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
58906 return pPg->pExtra;
58907}
58908
58909/*
58910** Get/set the locking-mode for this pager. Parameter eMode must be one
58911** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
58912** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
58913** the locking-mode is set to the value specified.
58914**
58915** The returned value is either PAGER_LOCKINGMODE_NORMAL or
58916** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
58917** locking-mode.
58918*/
58919SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
58920 assert( eMode==PAGER_LOCKINGMODE_QUERY
58921 || eMode==PAGER_LOCKINGMODE_NORMAL
58922 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
58923 assert( PAGER_LOCKINGMODE_QUERY<0 );
58924 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
58925 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
58926 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
58927 pPager->exclusiveMode = (u8)eMode;
58928 }
58929 return (int)pPager->exclusiveMode;
58930}
58931
58932/*
58933** Set the journal-mode for this pager. Parameter eMode must be one of:
58934**
58935** PAGER_JOURNALMODE_DELETE
58936** PAGER_JOURNALMODE_TRUNCATE
58937** PAGER_JOURNALMODE_PERSIST
58938** PAGER_JOURNALMODE_OFF
58939** PAGER_JOURNALMODE_MEMORY
58940** PAGER_JOURNALMODE_WAL
58941**
58942** The journalmode is set to the value specified if the change is allowed.
58943** The change may be disallowed for the following reasons:
58944**
58945** * An in-memory database can only have its journal_mode set to _OFF
58946** or _MEMORY.
58947**
58948** * Temporary databases cannot have _WAL journalmode.
58949**
58950** The returned indicate the current (possibly updated) journal-mode.
58951*/
58952SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
58953 u8 eOld = pPager->journalMode; /* Prior journalmode */
58954
58955 /* The eMode parameter is always valid */
58956 assert( eMode==PAGER_JOURNALMODE_DELETE
58957 || eMode==PAGER_JOURNALMODE_TRUNCATE
58958 || eMode==PAGER_JOURNALMODE_PERSIST
58959 || eMode==PAGER_JOURNALMODE_OFF
58960 || eMode==PAGER_JOURNALMODE_WAL
58961 || eMode==PAGER_JOURNALMODE_MEMORY );
58962
58963 /* This routine is only called from the OP_JournalMode opcode, and
58964 ** the logic there will never allow a temporary file to be changed
58965 ** to WAL mode.
58966 */
58967 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
58968
58969 /* Do allow the journalmode of an in-memory database to be set to
58970 ** anything other than MEMORY or OFF
58971 */
58972 if( MEMDB ){
58973 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
58974 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
58975 eMode = eOld;
58976 }
58977 }
58978
58979 if( eMode!=eOld ){
58980
58981 /* Change the journal mode. */
58982 assert( pPager->eState!=PAGER_ERROR );
58983 pPager->journalMode = (u8)eMode;
58984
58985 /* When transistioning from TRUNCATE or PERSIST to any other journal
58986 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
58987 ** delete the journal file.
58988 */
58989 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
58990 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
58991 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
58992 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
58993 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
58994 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
58995
58996 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
58997 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
58998
58999 /* In this case we would like to delete the journal file. If it is
59000 ** not possible, then that is not a problem. Deleting the journal file
59001 ** here is an optimization only.
59002 **
59003 ** Before deleting the journal file, obtain a RESERVED lock on the
59004 ** database file. This ensures that the journal file is not deleted
59005 ** while it is in use by some other client.
59006 */
59007 sqlite3OsClose(pPager->jfd);
59008 if( pPager->eLock>=RESERVED_LOCK ){
59009 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
59010 }else{
59011 int rc = SQLITE_OK;
59012 int state = pPager->eState;
59013 assert( state==PAGER_OPEN || state==PAGER_READER );
59014 if( state==PAGER_OPEN ){
59015 rc = sqlite3PagerSharedLock(pPager);
59016 }
59017 if( pPager->eState==PAGER_READER ){
59018 assert( rc==SQLITE_OK );
59019 rc = pagerLockDb(pPager, RESERVED_LOCK);
59020 }
59021 if( rc==SQLITE_OK ){
59022 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
59023 }
59024 if( rc==SQLITE_OK && state==PAGER_READER ){
59025 pagerUnlockDb(pPager, SHARED_LOCK);
59026 }else if( state==PAGER_OPEN ){
59027 pager_unlock(pPager);
59028 }
59029 assert( state==pPager->eState );
59030 }
59031 }else if( eMode==PAGER_JOURNALMODE_OFF ){
59032 sqlite3OsClose(pPager->jfd);
59033 }
59034 }
59035
59036 /* Return the new journal mode */
59037 return (int)pPager->journalMode;
59038}
59039
59040/*
59041** Return the current journal mode.
59042*/
59043SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
59044 return (int)pPager->journalMode;
59045}
59046
59047/*
59048** Return TRUE if the pager is in a state where it is OK to change the
59049** journalmode. Journalmode changes can only happen when the database
59050** is unmodified.
59051*/
59052SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
59053 assert( assert_pager_state(pPager) );
59054 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
59055 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
59056 return 1;
59057}
59058
59059/*
59060** Get/set the size-limit used for persistent journal files.
59061**
59062** Setting the size limit to -1 means no limit is enforced.
59063** An attempt to set a limit smaller than -1 is a no-op.
59064*/
59065SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
59066 if( iLimit>=-1 ){
59067 pPager->journalSizeLimit = iLimit;
59068 sqlite3WalLimit(pPager->pWal, iLimit);
59069 }
59070 return pPager->journalSizeLimit;
59071}
59072
59073/*
59074** Return a pointer to the pPager->pBackup variable. The backup module
59075** in backup.c maintains the content of this variable. This module
59076** uses it opaquely as an argument to sqlite3BackupRestart() and
59077** sqlite3BackupUpdate() only.
59078*/
59079SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
59080 return &pPager->pBackup;
59081}
59082
59083#ifndef SQLITE_OMIT_VACUUM
59084/*
59085** Unless this is an in-memory or temporary database, clear the pager cache.
59086*/
59087SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
59088 assert( MEMDB==0 || pPager->tempFile );
59089 if( pPager->tempFile==0 ) pager_reset(pPager);
59090}
59091#endif
59092
59093
59094#ifndef SQLITE_OMIT_WAL
59095/*
59096** This function is called when the user invokes "PRAGMA wal_checkpoint",
59097** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
59098** or wal_blocking_checkpoint() API functions.
59099**
59100** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
59101*/
59102SQLITE_PRIVATE int sqlite3PagerCheckpoint(
59103 Pager *pPager, /* Checkpoint on this pager */
59104 sqlite3 *db, /* Db handle used to check for interrupts */
59105 int eMode, /* Type of checkpoint */
59106 int *pnLog, /* OUT: Final number of frames in log */
59107 int *pnCkpt /* OUT: Final number of checkpointed frames */
59108){
59109 int rc = SQLITE_OK;
59110 if( pPager->pWal ){
59111 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
59112 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
59113 pPager->pBusyHandlerArg,
59114 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
59115 pnLog, pnCkpt
59116 );
59117 }
59118 return rc;
59119}
59120
59121SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
59122 return sqlite3WalCallback(pPager->pWal);
59123}
59124
59125/*
59126** Return true if the underlying VFS for the given pager supports the
59127** primitives necessary for write-ahead logging.
59128*/
59129SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
59130 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
59131 if( pPager->noLock ) return 0;
59132 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
59133}
59134
59135/*
59136** Attempt to take an exclusive lock on the database file. If a PENDING lock
59137** is obtained instead, immediately release it.
59138*/
59139static int pagerExclusiveLock(Pager *pPager){
59140 int rc; /* Return code */
59141
59142 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59143 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
59144 if( rc!=SQLITE_OK ){
59145 /* If the attempt to grab the exclusive lock failed, release the
59146 ** pending lock that may have been obtained instead. */
59147 pagerUnlockDb(pPager, SHARED_LOCK);
59148 }
59149
59150 return rc;
59151}
59152
59153/*
59154** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
59155** exclusive-locking mode when this function is called, take an EXCLUSIVE
59156** lock on the database file and use heap-memory to store the wal-index
59157** in. Otherwise, use the normal shared-memory.
59158*/
59159static int pagerOpenWal(Pager *pPager){
59160 int rc = SQLITE_OK;
59161
59162 assert( pPager->pWal==0 && pPager->tempFile==0 );
59163 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59164
59165 /* If the pager is already in exclusive-mode, the WAL module will use
59166 ** heap-memory for the wal-index instead of the VFS shared-memory
59167 ** implementation. Take the exclusive lock now, before opening the WAL
59168 ** file, to make sure this is safe.
59169 */
59170 if( pPager->exclusiveMode ){
59171 rc = pagerExclusiveLock(pPager);
59172 }
59173
59174 /* Open the connection to the log file. If this operation fails,
59175 ** (e.g. due to malloc() failure), return an error code.
59176 */
59177 if( rc==SQLITE_OK ){
59178 rc = sqlite3WalOpen(pPager->pVfs,
59179 pPager->fd, pPager->zWal, pPager->exclusiveMode,
59180 pPager->journalSizeLimit, &pPager->pWal
59181 );
59182 }
59183 pagerFixMaplimit(pPager);
59184
59185 return rc;
59186}
59187
59188
59189/*
59190** The caller must be holding a SHARED lock on the database file to call
59191** this function.
59192**
59193** If the pager passed as the first argument is open on a real database
59194** file (not a temp file or an in-memory database), and the WAL file
59195** is not already open, make an attempt to open it now. If successful,
59196** return SQLITE_OK. If an error occurs or the VFS used by the pager does
59197** not support the xShmXXX() methods, return an error code. *pbOpen is
59198** not modified in either case.
59199**
59200** If the pager is open on a temp-file (or in-memory database), or if
59201** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
59202** without doing anything.
59203*/
59204SQLITE_PRIVATE int sqlite3PagerOpenWal(
59205 Pager *pPager, /* Pager object */
59206 int *pbOpen /* OUT: Set to true if call is a no-op */
59207){
59208 int rc = SQLITE_OK; /* Return code */
59209
59210 assert( assert_pager_state(pPager) );
59211 assert( pPager->eState==PAGER_OPEN || pbOpen );
59212 assert( pPager->eState==PAGER_READER || !pbOpen );
59213 assert( pbOpen==0 || *pbOpen==0 );
59214 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
59215
59216 if( !pPager->tempFile && !pPager->pWal ){
59217 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
59218
59219 /* Close any rollback journal previously open */
59220 sqlite3OsClose(pPager->jfd);
59221
59222 rc = pagerOpenWal(pPager);
59223 if( rc==SQLITE_OK ){
59224 pPager->journalMode = PAGER_JOURNALMODE_WAL;
59225 pPager->eState = PAGER_OPEN;
59226 }
59227 }else{
59228 *pbOpen = 1;
59229 }
59230
59231 return rc;
59232}
59233
59234/*
59235** This function is called to close the connection to the log file prior
59236** to switching from WAL to rollback mode.
59237**
59238** Before closing the log file, this function attempts to take an
59239** EXCLUSIVE lock on the database file. If this cannot be obtained, an
59240** error (SQLITE_BUSY) is returned and the log connection is not closed.
59241** If successful, the EXCLUSIVE lock is not released before returning.
59242*/
59243SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
59244 int rc = SQLITE_OK;
59245
59246 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
59247
59248 /* If the log file is not already open, but does exist in the file-system,
59249 ** it may need to be checkpointed before the connection can switch to
59250 ** rollback mode. Open it now so this can happen.
59251 */
59252 if( !pPager->pWal ){
59253 int logexists = 0;
59254 rc = pagerLockDb(pPager, SHARED_LOCK);
59255 if( rc==SQLITE_OK ){
59256 rc = sqlite3OsAccess(
59257 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
59258 );
59259 }
59260 if( rc==SQLITE_OK && logexists ){
59261 rc = pagerOpenWal(pPager);
59262 }
59263 }
59264
59265 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
59266 ** the database file, the log and log-summary files will be deleted.
59267 */
59268 if( rc==SQLITE_OK && pPager->pWal ){
59269 rc = pagerExclusiveLock(pPager);
59270 if( rc==SQLITE_OK ){
59271 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
59272 pPager->pageSize, (u8*)pPager->pTmpSpace);
59273 pPager->pWal = 0;
59274 pagerFixMaplimit(pPager);
59275 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
59276 }
59277 }
59278 return rc;
59279}
59280
59281#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
59282/*
59283** If pager pPager is a wal-mode database not in exclusive locking mode,
59284** invoke the sqlite3WalWriteLock() function on the associated Wal object
59285** with the same db and bLock parameters as were passed to this function.
59286** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
59287*/
59288SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
59289 int rc = SQLITE_OK;
59290 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
59291 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
59292 }
59293 return rc;
59294}
59295
59296/*
59297** Set the database handle used by the wal layer to determine if
59298** blocking locks are required.
59299*/
59300SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
59301 if( pagerUseWal(pPager) ){
59302 sqlite3WalDb(pPager->pWal, db);
59303 }
59304}
59305#endif
59306
59307#ifdef SQLITE_ENABLE_SNAPSHOT
59308/*
59309** If this is a WAL database, obtain a snapshot handle for the snapshot
59310** currently open. Otherwise, return an error.
59311*/
59312SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
59313 int rc = SQLITE_ERROR;
59314 if( pPager->pWal ){
59315 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
59316 }
59317 return rc;
59318}
59319
59320/*
59321** If this is a WAL database, store a pointer to pSnapshot. Next time a
59322** read transaction is opened, attempt to read from the snapshot it
59323** identifies. If this is not a WAL database, return an error.
59324*/
59325SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(
59326 Pager *pPager,
59327 sqlite3_snapshot *pSnapshot
59328){
59329 int rc = SQLITE_OK;
59330 if( pPager->pWal ){
59331 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
59332 }else{
59333 rc = SQLITE_ERROR;
59334 }
59335 return rc;
59336}
59337
59338/*
59339** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
59340** is not a WAL database, return an error.
59341*/
59342SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
59343 int rc;
59344 if( pPager->pWal ){
59345 rc = sqlite3WalSnapshotRecover(pPager->pWal);
59346 }else{
59347 rc = SQLITE_ERROR;
59348 }
59349 return rc;
59350}
59351
59352/*
59353** The caller currently has a read transaction open on the database.
59354** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
59355** this function takes a SHARED lock on the CHECKPOINTER slot and then
59356** checks if the snapshot passed as the second argument is still
59357** available. If so, SQLITE_OK is returned.
59358**
59359** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
59360** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
59361** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
59362** lock is released before returning.
59363*/
59364SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
59365 int rc;
59366 if( pPager->pWal ){
59367 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
59368 }else{
59369 rc = SQLITE_ERROR;
59370 }
59371 return rc;
59372}
59373
59374/*
59375** Release a lock obtained by an earlier successful call to
59376** sqlite3PagerSnapshotCheck().
59377*/
59378SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
59379 assert( pPager->pWal );
59380 sqlite3WalSnapshotUnlock(pPager->pWal);
59381}
59382
59383#endif /* SQLITE_ENABLE_SNAPSHOT */
59384#endif /* !SQLITE_OMIT_WAL */
59385
59386#ifdef SQLITE_ENABLE_ZIPVFS
59387/*
59388** A read-lock must be held on the pager when this function is called. If
59389** the pager is in WAL mode and the WAL file currently contains one or more
59390** frames, return the size in bytes of the page images stored within the
59391** WAL frames. Otherwise, if this is not a WAL database or the WAL file
59392** is empty, return 0.
59393*/
59394SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
59395 assert( pPager->eState>=PAGER_READER );
59396 return sqlite3WalFramesize(pPager->pWal);
59397}
59398#endif
59399
59400#endif /* SQLITE_OMIT_DISKIO */
59401
59402/************** End of pager.c ***********************************************/
59403/************** Begin file wal.c *********************************************/
59404/*
59405** 2010 February 1
59406**
59407** The author disclaims copyright to this source code. In place of
59408** a legal notice, here is a blessing:
59409**
59410** May you do good and not evil.
59411** May you find forgiveness for yourself and forgive others.
59412** May you share freely, never taking more than you give.
59413**
59414*************************************************************************
59415**
59416** This file contains the implementation of a write-ahead log (WAL) used in
59417** "journal_mode=WAL" mode.
59418**
59419** WRITE-AHEAD LOG (WAL) FILE FORMAT
59420**
59421** A WAL file consists of a header followed by zero or more "frames".
59422** Each frame records the revised content of a single page from the
59423** database file. All changes to the database are recorded by writing
59424** frames into the WAL. Transactions commit when a frame is written that
59425** contains a commit marker. A single WAL can and usually does record
59426** multiple transactions. Periodically, the content of the WAL is
59427** transferred back into the database file in an operation called a
59428** "checkpoint".
59429**
59430** A single WAL file can be used multiple times. In other words, the
59431** WAL can fill up with frames and then be checkpointed and then new
59432** frames can overwrite the old ones. A WAL always grows from beginning
59433** toward the end. Checksums and counters attached to each frame are
59434** used to determine which frames within the WAL are valid and which
59435** are leftovers from prior checkpoints.
59436**
59437** The WAL header is 32 bytes in size and consists of the following eight
59438** big-endian 32-bit unsigned integer values:
59439**
59440** 0: Magic number. 0x377f0682 or 0x377f0683
59441** 4: File format version. Currently 3007000
59442** 8: Database page size. Example: 1024
59443** 12: Checkpoint sequence number
59444** 16: Salt-1, random integer incremented with each checkpoint
59445** 20: Salt-2, a different random integer changing with each ckpt
59446** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
59447** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
59448**
59449** Immediately following the wal-header are zero or more frames. Each
59450** frame consists of a 24-byte frame-header followed by a <page-size> bytes
59451** of page data. The frame-header is six big-endian 32-bit unsigned
59452** integer values, as follows:
59453**
59454** 0: Page number.
59455** 4: For commit records, the size of the database image in pages
59456** after the commit. For all other records, zero.
59457** 8: Salt-1 (copied from the header)
59458** 12: Salt-2 (copied from the header)
59459** 16: Checksum-1.
59460** 20: Checksum-2.
59461**
59462** A frame is considered valid if and only if the following conditions are
59463** true:
59464**
59465** (1) The salt-1 and salt-2 values in the frame-header match
59466** salt values in the wal-header
59467**
59468** (2) The checksum values in the final 8 bytes of the frame-header
59469** exactly match the checksum computed consecutively on the
59470** WAL header and the first 8 bytes and the content of all frames
59471** up to and including the current frame.
59472**
59473** The checksum is computed using 32-bit big-endian integers if the
59474** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
59475** is computed using little-endian if the magic number is 0x377f0682.
59476** The checksum values are always stored in the frame header in a
59477** big-endian format regardless of which byte order is used to compute
59478** the checksum. The checksum is computed by interpreting the input as
59479** an even number of unsigned 32-bit integers: x[0] through x[N]. The
59480** algorithm used for the checksum is as follows:
59481**
59482** for i from 0 to n-1 step 2:
59483** s0 += x[i] + s1;
59484** s1 += x[i+1] + s0;
59485** endfor
59486**
59487** Note that s0 and s1 are both weighted checksums using fibonacci weights
59488** in reverse order (the largest fibonacci weight occurs on the first element
59489** of the sequence being summed.) The s1 value spans all 32-bit
59490** terms of the sequence whereas s0 omits the final term.
59491**
59492** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
59493** WAL is transferred into the database, then the database is VFS.xSync-ed.
59494** The VFS.xSync operations serve as write barriers - all writes launched
59495** before the xSync must complete before any write that launches after the
59496** xSync begins.
59497**
59498** After each checkpoint, the salt-1 value is incremented and the salt-2
59499** value is randomized. This prevents old and new frames in the WAL from
59500** being considered valid at the same time and being checkpointing together
59501** following a crash.
59502**
59503** READER ALGORITHM
59504**
59505** To read a page from the database (call it page number P), a reader
59506** first checks the WAL to see if it contains page P. If so, then the
59507** last valid instance of page P that is a followed by a commit frame
59508** or is a commit frame itself becomes the value read. If the WAL
59509** contains no copies of page P that are valid and which are a commit
59510** frame or are followed by a commit frame, then page P is read from
59511** the database file.
59512**
59513** To start a read transaction, the reader records the index of the last
59514** valid frame in the WAL. The reader uses this recorded "mxFrame" value
59515** for all subsequent read operations. New transactions can be appended
59516** to the WAL, but as long as the reader uses its original mxFrame value
59517** and ignores the newly appended content, it will see a consistent snapshot
59518** of the database from a single point in time. This technique allows
59519** multiple concurrent readers to view different versions of the database
59520** content simultaneously.
59521**
59522** The reader algorithm in the previous paragraphs works correctly, but
59523** because frames for page P can appear anywhere within the WAL, the
59524** reader has to scan the entire WAL looking for page P frames. If the
59525** WAL is large (multiple megabytes is typical) that scan can be slow,
59526** and read performance suffers. To overcome this problem, a separate
59527** data structure called the wal-index is maintained to expedite the
59528** search for frames of a particular page.
59529**
59530** WAL-INDEX FORMAT
59531**
59532** Conceptually, the wal-index is shared memory, though VFS implementations
59533** might choose to implement the wal-index using a mmapped file. Because
59534** the wal-index is shared memory, SQLite does not support journal_mode=WAL
59535** on a network filesystem. All users of the database must be able to
59536** share memory.
59537**
59538** In the default unix and windows implementation, the wal-index is a mmapped
59539** file whose name is the database name with a "-shm" suffix added. For that
59540** reason, the wal-index is sometimes called the "shm" file.
59541**
59542** The wal-index is transient. After a crash, the wal-index can (and should
59543** be) reconstructed from the original WAL file. In fact, the VFS is required
59544** to either truncate or zero the header of the wal-index when the last
59545** connection to it closes. Because the wal-index is transient, it can
59546** use an architecture-specific format; it does not have to be cross-platform.
59547** Hence, unlike the database and WAL file formats which store all values
59548** as big endian, the wal-index can store multi-byte values in the native
59549** byte order of the host computer.
59550**
59551** The purpose of the wal-index is to answer this question quickly: Given
59552** a page number P and a maximum frame index M, return the index of the
59553** last frame in the wal before frame M for page P in the WAL, or return
59554** NULL if there are no frames for page P in the WAL prior to M.
59555**
59556** The wal-index consists of a header region, followed by an one or
59557** more index blocks.
59558**
59559** The wal-index header contains the total number of frames within the WAL
59560** in the mxFrame field.
59561**
59562** Each index block except for the first contains information on
59563** HASHTABLE_NPAGE frames. The first index block contains information on
59564** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
59565** HASHTABLE_NPAGE are selected so that together the wal-index header and
59566** first index block are the same size as all other index blocks in the
59567** wal-index.
59568**
59569** Each index block contains two sections, a page-mapping that contains the
59570** database page number associated with each wal frame, and a hash-table
59571** that allows readers to query an index block for a specific page number.
59572** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
59573** for the first index block) 32-bit page numbers. The first entry in the
59574** first index-block contains the database page number corresponding to the
59575** first frame in the WAL file. The first entry in the second index block
59576** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
59577** the log, and so on.
59578**
59579** The last index block in a wal-index usually contains less than the full
59580** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
59581** depending on the contents of the WAL file. This does not change the
59582** allocated size of the page-mapping array - the page-mapping array merely
59583** contains unused entries.
59584**
59585** Even without using the hash table, the last frame for page P
59586** can be found by scanning the page-mapping sections of each index block
59587** starting with the last index block and moving toward the first, and
59588** within each index block, starting at the end and moving toward the
59589** beginning. The first entry that equals P corresponds to the frame
59590** holding the content for that page.
59591**
59592** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
59593** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
59594** hash table for each page number in the mapping section, so the hash
59595** table is never more than half full. The expected number of collisions
59596** prior to finding a match is 1. Each entry of the hash table is an
59597** 1-based index of an entry in the mapping section of the same
59598** index block. Let K be the 1-based index of the largest entry in
59599** the mapping section. (For index blocks other than the last, K will
59600** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
59601** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
59602** contain a value of 0.
59603**
59604** To look for page P in the hash table, first compute a hash iKey on
59605** P as follows:
59606**
59607** iKey = (P * 383) % HASHTABLE_NSLOT
59608**
59609** Then start scanning entries of the hash table, starting with iKey
59610** (wrapping around to the beginning when the end of the hash table is
59611** reached) until an unused hash slot is found. Let the first unused slot
59612** be at index iUnused. (iUnused might be less than iKey if there was
59613** wrap-around.) Because the hash table is never more than half full,
59614** the search is guaranteed to eventually hit an unused entry. Let
59615** iMax be the value between iKey and iUnused, closest to iUnused,
59616** where aHash[iMax]==P. If there is no iMax entry (if there exists
59617** no hash slot such that aHash[i]==p) then page P is not in the
59618** current index block. Otherwise the iMax-th mapping entry of the
59619** current index block corresponds to the last entry that references
59620** page P.
59621**
59622** A hash search begins with the last index block and moves toward the
59623** first index block, looking for entries corresponding to page P. On
59624** average, only two or three slots in each index block need to be
59625** examined in order to either find the last entry for page P, or to
59626** establish that no such entry exists in the block. Each index block
59627** holds over 4000 entries. So two or three index blocks are sufficient
59628** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
59629** comparisons (on average) suffice to either locate a frame in the
59630** WAL or to establish that the frame does not exist in the WAL. This
59631** is much faster than scanning the entire 10MB WAL.
59632**
59633** Note that entries are added in order of increasing K. Hence, one
59634** reader might be using some value K0 and a second reader that started
59635** at a later time (after additional transactions were added to the WAL
59636** and to the wal-index) might be using a different value K1, where K1>K0.
59637** Both readers can use the same hash table and mapping section to get
59638** the correct result. There may be entries in the hash table with
59639** K>K0 but to the first reader, those entries will appear to be unused
59640** slots in the hash table and so the first reader will get an answer as
59641** if no values greater than K0 had ever been inserted into the hash table
59642** in the first place - which is what reader one wants. Meanwhile, the
59643** second reader using K1 will see additional values that were inserted
59644** later, which is exactly what reader two wants.
59645**
59646** When a rollback occurs, the value of K is decreased. Hash table entries
59647** that correspond to frames greater than the new K value are removed
59648** from the hash table at this point.
59649*/
59650#ifndef SQLITE_OMIT_WAL
59651
59652/* #include "wal.h" */
59653
59654/*
59655** Trace output macros
59656*/
59657#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
59658SQLITE_PRIVATE int sqlite3WalTrace = 0;
59659# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
59660#else
59661# define WALTRACE(X)
59662#endif
59663
59664/*
59665** The maximum (and only) versions of the wal and wal-index formats
59666** that may be interpreted by this version of SQLite.
59667**
59668** If a client begins recovering a WAL file and finds that (a) the checksum
59669** values in the wal-header are correct and (b) the version field is not
59670** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
59671**
59672** Similarly, if a client successfully reads a wal-index header (i.e. the
59673** checksum test is successful) and finds that the version field is not
59674** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
59675** returns SQLITE_CANTOPEN.
59676*/
59677#define WAL_MAX_VERSION 3007000
59678#define WALINDEX_MAX_VERSION 3007000
59679
59680/*
59681** Index numbers for various locking bytes. WAL_NREADER is the number
59682** of available reader locks and should be at least 3. The default
59683** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
59684**
59685** Technically, the various VFSes are free to implement these locks however
59686** they see fit. However, compatibility is encouraged so that VFSes can
59687** interoperate. The standard implemention used on both unix and windows
59688** is for the index number to indicate a byte offset into the
59689** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
59690** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
59691** should be 120) is the location in the shm file for the first locking
59692** byte.
59693*/
59694#define WAL_WRITE_LOCK 0
59695#define WAL_ALL_BUT_WRITE 1
59696#define WAL_CKPT_LOCK 1
59697#define WAL_RECOVER_LOCK 2
59698#define WAL_READ_LOCK(I) (3+(I))
59699#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
59700
59701
59702/* Object declarations */
59703typedef struct WalIndexHdr WalIndexHdr;
59704typedef struct WalIterator WalIterator;
59705typedef struct WalCkptInfo WalCkptInfo;
59706
59707
59708/*
59709** The following object holds a copy of the wal-index header content.
59710**
59711** The actual header in the wal-index consists of two copies of this
59712** object followed by one instance of the WalCkptInfo object.
59713** For all versions of SQLite through 3.10.0 and probably beyond,
59714** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
59715** the total header size is 136 bytes.
59716**
59717** The szPage value can be any power of 2 between 512 and 32768, inclusive.
59718** Or it can be 1 to represent a 65536-byte page. The latter case was
59719** added in 3.7.1 when support for 64K pages was added.
59720*/
59721struct WalIndexHdr {
59722 u32 iVersion; /* Wal-index version */
59723 u32 unused; /* Unused (padding) field */
59724 u32 iChange; /* Counter incremented each transaction */
59725 u8 isInit; /* 1 when initialized */
59726 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
59727 u16 szPage; /* Database page size in bytes. 1==64K */
59728 u32 mxFrame; /* Index of last valid frame in the WAL */
59729 u32 nPage; /* Size of database in pages */
59730 u32 aFrameCksum[2]; /* Checksum of last frame in log */
59731 u32 aSalt[2]; /* Two salt values copied from WAL header */
59732 u32 aCksum[2]; /* Checksum over all prior fields */
59733};
59734
59735/*
59736** A copy of the following object occurs in the wal-index immediately
59737** following the second copy of the WalIndexHdr. This object stores
59738** information used by checkpoint.
59739**
59740** nBackfill is the number of frames in the WAL that have been written
59741** back into the database. (We call the act of moving content from WAL to
59742** database "backfilling".) The nBackfill number is never greater than
59743** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
59744** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
59745** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
59746** mxFrame back to zero when the WAL is reset.
59747**
59748** nBackfillAttempted is the largest value of nBackfill that a checkpoint
59749** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
59750** the nBackfillAttempted is set before any backfilling is done and the
59751** nBackfill is only set after all backfilling completes. So if a checkpoint
59752** crashes, nBackfillAttempted might be larger than nBackfill. The
59753** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
59754**
59755** The aLock[] field is a set of bytes used for locking. These bytes should
59756** never be read or written.
59757**
59758** There is one entry in aReadMark[] for each reader lock. If a reader
59759** holds read-lock K, then the value in aReadMark[K] is no greater than
59760** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
59761** for any aReadMark[] means that entry is unused. aReadMark[0] is
59762** a special case; its value is never used and it exists as a place-holder
59763** to avoid having to offset aReadMark[] indexs by one. Readers holding
59764** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
59765** directly from the database.
59766**
59767** The value of aReadMark[K] may only be changed by a thread that
59768** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
59769** aReadMark[K] cannot changed while there is a reader is using that mark
59770** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
59771**
59772** The checkpointer may only transfer frames from WAL to database where
59773** the frame numbers are less than or equal to every aReadMark[] that is
59774** in use (that is, every aReadMark[j] for which there is a corresponding
59775** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
59776** largest value and will increase an unused aReadMark[] to mxFrame if there
59777** is not already an aReadMark[] equal to mxFrame. The exception to the
59778** previous sentence is when nBackfill equals mxFrame (meaning that everything
59779** in the WAL has been backfilled into the database) then new readers
59780** will choose aReadMark[0] which has value 0 and hence such reader will
59781** get all their all content directly from the database file and ignore
59782** the WAL.
59783**
59784** Writers normally append new frames to the end of the WAL. However,
59785** if nBackfill equals mxFrame (meaning that all WAL content has been
59786** written back into the database) and if no readers are using the WAL
59787** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
59788** the writer will first "reset" the WAL back to the beginning and start
59789** writing new content beginning at frame 1.
59790**
59791** We assume that 32-bit loads are atomic and so no locks are needed in
59792** order to read from any aReadMark[] entries.
59793*/
59794struct WalCkptInfo {
59795 u32 nBackfill; /* Number of WAL frames backfilled into DB */
59796 u32 aReadMark[WAL_NREADER]; /* Reader marks */
59797 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
59798 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
59799 u32 notUsed0; /* Available for future enhancements */
59800};
59801#define READMARK_NOT_USED 0xffffffff
59802
59803
59804/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
59805** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
59806** only support mandatory file-locks, we do not read or write data
59807** from the region of the file on which locks are applied.
59808*/
59809#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
59810#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
59811
59812/* Size of header before each frame in wal */
59813#define WAL_FRAME_HDRSIZE 24
59814
59815/* Size of write ahead log header, including checksum. */
59816#define WAL_HDRSIZE 32
59817
59818/* WAL magic value. Either this value, or the same value with the least
59819** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
59820** big-endian format in the first 4 bytes of a WAL file.
59821**
59822** If the LSB is set, then the checksums for each frame within the WAL
59823** file are calculated by treating all data as an array of 32-bit
59824** big-endian words. Otherwise, they are calculated by interpreting
59825** all data as 32-bit little-endian words.
59826*/
59827#define WAL_MAGIC 0x377f0682
59828
59829/*
59830** Return the offset of frame iFrame in the write-ahead log file,
59831** assuming a database page size of szPage bytes. The offset returned
59832** is to the start of the write-ahead log frame-header.
59833*/
59834#define walFrameOffset(iFrame, szPage) ( \
59835 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
59836)
59837
59838/*
59839** An open write-ahead log file is represented by an instance of the
59840** following object.
59841*/
59842struct Wal {
59843 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
59844 sqlite3_file *pDbFd; /* File handle for the database file */
59845 sqlite3_file *pWalFd; /* File handle for WAL file */
59846 u32 iCallback; /* Value to pass to log callback (or 0) */
59847 i64 mxWalSize; /* Truncate WAL to this size upon reset */
59848 int nWiData; /* Size of array apWiData */
59849 int szFirstBlock; /* Size of first block written to WAL file */
59850 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
59851 u32 szPage; /* Database page size */
59852 i16 readLock; /* Which read lock is being held. -1 for none */
59853 u8 syncFlags; /* Flags to use to sync header writes */
59854 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
59855 u8 writeLock; /* True if in a write transaction */
59856 u8 ckptLock; /* True if holding a checkpoint lock */
59857 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
59858 u8 truncateOnCommit; /* True to truncate WAL file on commit */
59859 u8 syncHeader; /* Fsync the WAL header if true */
59860 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
59861 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
59862 WalIndexHdr hdr; /* Wal-index header for current transaction */
59863 u32 minFrame; /* Ignore wal frames before this one */
59864 u32 iReCksum; /* On commit, recalculate checksums from here */
59865 const char *zWalName; /* Name of WAL file */
59866 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
59867#ifdef SQLITE_DEBUG
59868 u8 lockError; /* True if a locking error has occurred */
59869#endif
59870#ifdef SQLITE_ENABLE_SNAPSHOT
59871 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
59872#endif
59873#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
59874 sqlite3 *db;
59875#endif
59876};
59877
59878/*
59879** Candidate values for Wal.exclusiveMode.
59880*/
59881#define WAL_NORMAL_MODE 0
59882#define WAL_EXCLUSIVE_MODE 1
59883#define WAL_HEAPMEMORY_MODE 2
59884
59885/*
59886** Possible values for WAL.readOnly
59887*/
59888#define WAL_RDWR 0 /* Normal read/write connection */
59889#define WAL_RDONLY 1 /* The WAL file is readonly */
59890#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
59891
59892/*
59893** Each page of the wal-index mapping contains a hash-table made up of
59894** an array of HASHTABLE_NSLOT elements of the following type.
59895*/
59896typedef u16 ht_slot;
59897
59898/*
59899** This structure is used to implement an iterator that loops through
59900** all frames in the WAL in database page order. Where two or more frames
59901** correspond to the same database page, the iterator visits only the
59902** frame most recently written to the WAL (in other words, the frame with
59903** the largest index).
59904**
59905** The internals of this structure are only accessed by:
59906**
59907** walIteratorInit() - Create a new iterator,
59908** walIteratorNext() - Step an iterator,
59909** walIteratorFree() - Free an iterator.
59910**
59911** This functionality is used by the checkpoint code (see walCheckpoint()).
59912*/
59913struct WalIterator {
59914 u32 iPrior; /* Last result returned from the iterator */
59915 int nSegment; /* Number of entries in aSegment[] */
59916 struct WalSegment {
59917 int iNext; /* Next slot in aIndex[] not yet returned */
59918 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
59919 u32 *aPgno; /* Array of page numbers. */
59920 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
59921 int iZero; /* Frame number associated with aPgno[0] */
59922 } aSegment[1]; /* One for every 32KB page in the wal-index */
59923};
59924
59925/*
59926** Define the parameters of the hash tables in the wal-index file. There
59927** is a hash-table following every HASHTABLE_NPAGE page numbers in the
59928** wal-index.
59929**
59930** Changing any of these constants will alter the wal-index format and
59931** create incompatibilities.
59932*/
59933#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
59934#define HASHTABLE_HASH_1 383 /* Should be prime */
59935#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
59936
59937/*
59938** The block of page numbers associated with the first hash-table in a
59939** wal-index is smaller than usual. This is so that there is a complete
59940** hash-table on each aligned 32KB page of the wal-index.
59941*/
59942#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
59943
59944/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
59945#define WALINDEX_PGSZ ( \
59946 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
59947)
59948
59949/*
59950** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
59951** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
59952** numbered from zero.
59953**
59954** If the wal-index is currently smaller the iPage pages then the size
59955** of the wal-index might be increased, but only if it is safe to do
59956** so. It is safe to enlarge the wal-index if pWal->writeLock is true
59957** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
59958**
59959** If this call is successful, *ppPage is set to point to the wal-index
59960** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
59961** then an SQLite error code is returned and *ppPage is set to 0.
59962*/
59963static SQLITE_NOINLINE int walIndexPageRealloc(
59964 Wal *pWal, /* The WAL context */
59965 int iPage, /* The page we seek */
59966 volatile u32 **ppPage /* Write the page pointer here */
59967){
59968 int rc = SQLITE_OK;
59969
59970 /* Enlarge the pWal->apWiData[] array if required */
59971 if( pWal->nWiData<=iPage ){
59972 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
59973 volatile u32 **apNew;
59974 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
59975 if( !apNew ){
59976 *ppPage = 0;
59977 return SQLITE_NOMEM_BKPT;
59978 }
59979 memset((void*)&apNew[pWal->nWiData], 0,
59980 sizeof(u32*)*(iPage+1-pWal->nWiData));
59981 pWal->apWiData = apNew;
59982 pWal->nWiData = iPage+1;
59983 }
59984
59985 /* Request a pointer to the required page from the VFS */
59986 assert( pWal->apWiData[iPage]==0 );
59987 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
59988 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
59989 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
59990 }else{
59991 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
59992 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
59993 );
59994 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
59995 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
59996 if( rc==SQLITE_OK ){
59997 if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM;
59998 }else if( (rc&0xff)==SQLITE_READONLY ){
59999 pWal->readOnly |= WAL_SHM_RDONLY;
60000 if( rc==SQLITE_READONLY ){
60001 rc = SQLITE_OK;
60002 }
60003 }
60004 }
60005
60006 *ppPage = pWal->apWiData[iPage];
60007 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
60008 return rc;
60009}
60010static int walIndexPage(
60011 Wal *pWal, /* The WAL context */
60012 int iPage, /* The page we seek */
60013 volatile u32 **ppPage /* Write the page pointer here */
60014){
60015 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
60016 return walIndexPageRealloc(pWal, iPage, ppPage);
60017 }
60018 return SQLITE_OK;
60019}
60020
60021/*
60022** Return a pointer to the WalCkptInfo structure in the wal-index.
60023*/
60024static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
60025 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60026 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
60027}
60028
60029/*
60030** Return a pointer to the WalIndexHdr structure in the wal-index.
60031*/
60032static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
60033 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60034 return (volatile WalIndexHdr*)pWal->apWiData[0];
60035}
60036
60037/*
60038** The argument to this macro must be of type u32. On a little-endian
60039** architecture, it returns the u32 value that results from interpreting
60040** the 4 bytes as a big-endian value. On a big-endian architecture, it
60041** returns the value that would be produced by interpreting the 4 bytes
60042** of the input value as a little-endian integer.
60043*/
60044#define BYTESWAP32(x) ( \
60045 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
60046 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
60047)
60048
60049/*
60050** Generate or extend an 8 byte checksum based on the data in
60051** array aByte[] and the initial values of aIn[0] and aIn[1] (or
60052** initial values of 0 and 0 if aIn==NULL).
60053**
60054** The checksum is written back into aOut[] before returning.
60055**
60056** nByte must be a positive multiple of 8.
60057*/
60058static void walChecksumBytes(
60059 int nativeCksum, /* True for native byte-order, false for non-native */
60060 u8 *a, /* Content to be checksummed */
60061 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
60062 const u32 *aIn, /* Initial checksum value input */
60063 u32 *aOut /* OUT: Final checksum value output */
60064){
60065 u32 s1, s2;
60066 u32 *aData = (u32 *)a;
60067 u32 *aEnd = (u32 *)&a[nByte];
60068
60069 if( aIn ){
60070 s1 = aIn[0];
60071 s2 = aIn[1];
60072 }else{
60073 s1 = s2 = 0;
60074 }
60075
60076 assert( nByte>=8 );
60077 assert( (nByte&0x00000007)==0 );
60078 assert( nByte<=65536 );
60079
60080 if( nativeCksum ){
60081 do {
60082 s1 += *aData++ + s2;
60083 s2 += *aData++ + s1;
60084 }while( aData<aEnd );
60085 }else{
60086 do {
60087 s1 += BYTESWAP32(aData[0]) + s2;
60088 s2 += BYTESWAP32(aData[1]) + s1;
60089 aData += 2;
60090 }while( aData<aEnd );
60091 }
60092
60093 aOut[0] = s1;
60094 aOut[1] = s2;
60095}
60096
60097/*
60098** If there is the possibility of concurrent access to the SHM file
60099** from multiple threads and/or processes, then do a memory barrier.
60100*/
60101static void walShmBarrier(Wal *pWal){
60102 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
60103 sqlite3OsShmBarrier(pWal->pDbFd);
60104 }
60105}
60106
60107/*
60108** Add the SQLITE_NO_TSAN as part of the return-type of a function
60109** definition as a hint that the function contains constructs that
60110** might give false-positive TSAN warnings.
60111**
60112** See tag-20200519-1.
60113*/
60114#if defined(__clang__) && !defined(SQLITE_NO_TSAN)
60115# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread))
60116#else
60117# define SQLITE_NO_TSAN
60118#endif
60119
60120/*
60121** Write the header information in pWal->hdr into the wal-index.
60122**
60123** The checksum on pWal->hdr is updated before it is written.
60124*/
60125static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){
60126 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
60127 const int nCksum = offsetof(WalIndexHdr, aCksum);
60128
60129 assert( pWal->writeLock );
60130 pWal->hdr.isInit = 1;
60131 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
60132 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
60133 /* Possible TSAN false-positive. See tag-20200519-1 */
60134 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60135 walShmBarrier(pWal);
60136 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60137}
60138
60139/*
60140** This function encodes a single frame header and writes it to a buffer
60141** supplied by the caller. A frame-header is made up of a series of
60142** 4-byte big-endian integers, as follows:
60143**
60144** 0: Page number.
60145** 4: For commit records, the size of the database image in pages
60146** after the commit. For all other records, zero.
60147** 8: Salt-1 (copied from the wal-header)
60148** 12: Salt-2 (copied from the wal-header)
60149** 16: Checksum-1.
60150** 20: Checksum-2.
60151*/
60152static void walEncodeFrame(
60153 Wal *pWal, /* The write-ahead log */
60154 u32 iPage, /* Database page number for frame */
60155 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
60156 u8 *aData, /* Pointer to page data */
60157 u8 *aFrame /* OUT: Write encoded frame here */
60158){
60159 int nativeCksum; /* True for native byte-order checksums */
60160 u32 *aCksum = pWal->hdr.aFrameCksum;
60161 assert( WAL_FRAME_HDRSIZE==24 );
60162 sqlite3Put4byte(&aFrame[0], iPage);
60163 sqlite3Put4byte(&aFrame[4], nTruncate);
60164 if( pWal->iReCksum==0 ){
60165 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
60166
60167 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60168 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
60169 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60170
60171 sqlite3Put4byte(&aFrame[16], aCksum[0]);
60172 sqlite3Put4byte(&aFrame[20], aCksum[1]);
60173 }else{
60174 memset(&aFrame[8], 0, 16);
60175 }
60176}
60177
60178/*
60179** Check to see if the frame with header in aFrame[] and content
60180** in aData[] is valid. If it is a valid frame, fill *piPage and
60181** *pnTruncate and return true. Return if the frame is not valid.
60182*/
60183static int walDecodeFrame(
60184 Wal *pWal, /* The write-ahead log */
60185 u32 *piPage, /* OUT: Database page number for frame */
60186 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
60187 u8 *aData, /* Pointer to page data (for checksum) */
60188 u8 *aFrame /* Frame data */
60189){
60190 int nativeCksum; /* True for native byte-order checksums */
60191 u32 *aCksum = pWal->hdr.aFrameCksum;
60192 u32 pgno; /* Page number of the frame */
60193 assert( WAL_FRAME_HDRSIZE==24 );
60194
60195 /* A frame is only valid if the salt values in the frame-header
60196 ** match the salt values in the wal-header.
60197 */
60198 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
60199 return 0;
60200 }
60201
60202 /* A frame is only valid if the page number is creater than zero.
60203 */
60204 pgno = sqlite3Get4byte(&aFrame[0]);
60205 if( pgno==0 ){
60206 return 0;
60207 }
60208
60209 /* A frame is only valid if a checksum of the WAL header,
60210 ** all prior frams, the first 16 bytes of this frame-header,
60211 ** and the frame-data matches the checksum in the last 8
60212 ** bytes of this frame-header.
60213 */
60214 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60215 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
60216 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60217 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
60218 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
60219 ){
60220 /* Checksum failed. */
60221 return 0;
60222 }
60223
60224 /* If we reach this point, the frame is valid. Return the page number
60225 ** and the new database size.
60226 */
60227 *piPage = pgno;
60228 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
60229 return 1;
60230}
60231
60232
60233#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
60234/*
60235** Names of locks. This routine is used to provide debugging output and is not
60236** a part of an ordinary build.
60237*/
60238static const char *walLockName(int lockIdx){
60239 if( lockIdx==WAL_WRITE_LOCK ){
60240 return "WRITE-LOCK";
60241 }else if( lockIdx==WAL_CKPT_LOCK ){
60242 return "CKPT-LOCK";
60243 }else if( lockIdx==WAL_RECOVER_LOCK ){
60244 return "RECOVER-LOCK";
60245 }else{
60246 static char zName[15];
60247 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
60248 lockIdx-WAL_READ_LOCK(0));
60249 return zName;
60250 }
60251}
60252#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
60253
60254
60255/*
60256** Set or release locks on the WAL. Locks are either shared or exclusive.
60257** A lock cannot be moved directly between shared and exclusive - it must go
60258** through the unlocked state first.
60259**
60260** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
60261*/
60262static int walLockShared(Wal *pWal, int lockIdx){
60263 int rc;
60264 if( pWal->exclusiveMode ) return SQLITE_OK;
60265 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60266 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
60267 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
60268 walLockName(lockIdx), rc ? "failed" : "ok"));
60269 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60270 return rc;
60271}
60272static void walUnlockShared(Wal *pWal, int lockIdx){
60273 if( pWal->exclusiveMode ) return;
60274 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60275 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
60276 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
60277}
60278static int walLockExclusive(Wal *pWal, int lockIdx, int n){
60279 int rc;
60280 if( pWal->exclusiveMode ) return SQLITE_OK;
60281 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60282 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
60283 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
60284 walLockName(lockIdx), n, rc ? "failed" : "ok"));
60285 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60286 return rc;
60287}
60288static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
60289 if( pWal->exclusiveMode ) return;
60290 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60291 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
60292 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
60293 walLockName(lockIdx), n));
60294}
60295
60296/*
60297** Compute a hash on a page number. The resulting hash value must land
60298** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
60299** the hash to the next value in the event of a collision.
60300*/
60301static int walHash(u32 iPage){
60302 assert( iPage>0 );
60303 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
60304 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
60305}
60306static int walNextHash(int iPriorHash){
60307 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
60308}
60309
60310/*
60311** An instance of the WalHashLoc object is used to describe the location
60312** of a page hash table in the wal-index. This becomes the return value
60313** from walHashGet().
60314*/
60315typedef struct WalHashLoc WalHashLoc;
60316struct WalHashLoc {
60317 volatile ht_slot *aHash; /* Start of the wal-index hash table */
60318 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
60319 u32 iZero; /* One less than the frame number of first indexed*/
60320};
60321
60322/*
60323** Return pointers to the hash table and page number array stored on
60324** page iHash of the wal-index. The wal-index is broken into 32KB pages
60325** numbered starting from 0.
60326**
60327** Set output variable pLoc->aHash to point to the start of the hash table
60328** in the wal-index file. Set pLoc->iZero to one less than the frame
60329** number of the first frame indexed by this hash table. If a
60330** slot in the hash table is set to N, it refers to frame number
60331** (pLoc->iZero+N) in the log.
60332**
60333** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
60334** first frame indexed by the hash table, frame (pLoc->iZero+1).
60335*/
60336static int walHashGet(
60337 Wal *pWal, /* WAL handle */
60338 int iHash, /* Find the iHash'th table */
60339 WalHashLoc *pLoc /* OUT: Hash table location */
60340){
60341 int rc; /* Return code */
60342
60343 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
60344 assert( rc==SQLITE_OK || iHash>0 );
60345
60346 if( rc==SQLITE_OK ){
60347 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
60348 if( iHash==0 ){
60349 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
60350 pLoc->iZero = 0;
60351 }else{
60352 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
60353 }
60354 pLoc->aPgno = &pLoc->aPgno[-1];
60355 }
60356 return rc;
60357}
60358
60359/*
60360** Return the number of the wal-index page that contains the hash-table
60361** and page-number array that contain entries corresponding to WAL frame
60362** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
60363** are numbered starting from 0.
60364*/
60365static int walFramePage(u32 iFrame){
60366 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
60367 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
60368 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
60369 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
60370 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
60371 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
60372 );
60373 assert( iHash>=0 );
60374 return iHash;
60375}
60376
60377/*
60378** Return the page number associated with frame iFrame in this WAL.
60379*/
60380static u32 walFramePgno(Wal *pWal, u32 iFrame){
60381 int iHash = walFramePage(iFrame);
60382 if( iHash==0 ){
60383 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
60384 }
60385 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
60386}
60387
60388/*
60389** Remove entries from the hash table that point to WAL slots greater
60390** than pWal->hdr.mxFrame.
60391**
60392** This function is called whenever pWal->hdr.mxFrame is decreased due
60393** to a rollback or savepoint.
60394**
60395** At most only the hash table containing pWal->hdr.mxFrame needs to be
60396** updated. Any later hash tables will be automatically cleared when
60397** pWal->hdr.mxFrame advances to the point where those hash tables are
60398** actually needed.
60399*/
60400static void walCleanupHash(Wal *pWal){
60401 WalHashLoc sLoc; /* Hash table location */
60402 int iLimit = 0; /* Zero values greater than this */
60403 int nByte; /* Number of bytes to zero in aPgno[] */
60404 int i; /* Used to iterate through aHash[] */
60405 int rc; /* Return code form walHashGet() */
60406
60407 assert( pWal->writeLock );
60408 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
60409 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
60410 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
60411
60412 if( pWal->hdr.mxFrame==0 ) return;
60413
60414 /* Obtain pointers to the hash-table and page-number array containing
60415 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
60416 ** that the page said hash-table and array reside on is already mapped.(1)
60417 */
60418 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
60419 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
60420 rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
60421 if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
60422
60423 /* Zero all hash-table entries that correspond to frame numbers greater
60424 ** than pWal->hdr.mxFrame.
60425 */
60426 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
60427 assert( iLimit>0 );
60428 for(i=0; i<HASHTABLE_NSLOT; i++){
60429 if( sLoc.aHash[i]>iLimit ){
60430 sLoc.aHash[i] = 0;
60431 }
60432 }
60433
60434 /* Zero the entries in the aPgno array that correspond to frames with
60435 ** frame numbers greater than pWal->hdr.mxFrame.
60436 */
60437 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
60438 memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
60439
60440#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60441 /* Verify that the every entry in the mapping region is still reachable
60442 ** via the hash table even after the cleanup.
60443 */
60444 if( iLimit ){
60445 int j; /* Loop counter */
60446 int iKey; /* Hash key */
60447 for(j=1; j<=iLimit; j++){
60448 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
60449 if( sLoc.aHash[iKey]==j ) break;
60450 }
60451 assert( sLoc.aHash[iKey]==j );
60452 }
60453 }
60454#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
60455}
60456
60457
60458/*
60459** Set an entry in the wal-index that will map database page number
60460** pPage into WAL frame iFrame.
60461*/
60462static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
60463 int rc; /* Return code */
60464 WalHashLoc sLoc; /* Wal-index hash table location */
60465
60466 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
60467
60468 /* Assuming the wal-index file was successfully mapped, populate the
60469 ** page number array and hash table entry.
60470 */
60471 if( rc==SQLITE_OK ){
60472 int iKey; /* Hash table key */
60473 int idx; /* Value to write to hash-table slot */
60474 int nCollide; /* Number of hash collisions */
60475
60476 idx = iFrame - sLoc.iZero;
60477 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
60478
60479 /* If this is the first entry to be added to this hash-table, zero the
60480 ** entire hash table and aPgno[] array before proceeding.
60481 */
60482 if( idx==1 ){
60483 int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
60484 - (u8 *)&sLoc.aPgno[1]);
60485 memset((void*)&sLoc.aPgno[1], 0, nByte);
60486 }
60487
60488 /* If the entry in aPgno[] is already set, then the previous writer
60489 ** must have exited unexpectedly in the middle of a transaction (after
60490 ** writing one or more dirty pages to the WAL to free up memory).
60491 ** Remove the remnants of that writers uncommitted transaction from
60492 ** the hash-table before writing any new entries.
60493 */
60494 if( sLoc.aPgno[idx] ){
60495 walCleanupHash(pWal);
60496 assert( !sLoc.aPgno[idx] );
60497 }
60498
60499 /* Write the aPgno[] array entry and the hash-table slot. */
60500 nCollide = idx;
60501 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
60502 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
60503 }
60504 sLoc.aPgno[idx] = iPage;
60505 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx);
60506
60507#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60508 /* Verify that the number of entries in the hash table exactly equals
60509 ** the number of entries in the mapping region.
60510 */
60511 {
60512 int i; /* Loop counter */
60513 int nEntry = 0; /* Number of entries in the hash table */
60514 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
60515 assert( nEntry==idx );
60516 }
60517
60518 /* Verify that the every entry in the mapping region is reachable
60519 ** via the hash table. This turns out to be a really, really expensive
60520 ** thing to check, so only do this occasionally - not on every
60521 ** iteration.
60522 */
60523 if( (idx&0x3ff)==0 ){
60524 int i; /* Loop counter */
60525 for(i=1; i<=idx; i++){
60526 for(iKey=walHash(sLoc.aPgno[i]);
60527 sLoc.aHash[iKey];
60528 iKey=walNextHash(iKey)){
60529 if( sLoc.aHash[iKey]==i ) break;
60530 }
60531 assert( sLoc.aHash[iKey]==i );
60532 }
60533 }
60534#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
60535 }
60536
60537
60538 return rc;
60539}
60540
60541
60542/*
60543** Recover the wal-index by reading the write-ahead log file.
60544**
60545** This routine first tries to establish an exclusive lock on the
60546** wal-index to prevent other threads/processes from doing anything
60547** with the WAL or wal-index while recovery is running. The
60548** WAL_RECOVER_LOCK is also held so that other threads will know
60549** that this thread is running recovery. If unable to establish
60550** the necessary locks, this routine returns SQLITE_BUSY.
60551*/
60552static int walIndexRecover(Wal *pWal){
60553 int rc; /* Return Code */
60554 i64 nSize; /* Size of log file */
60555 u32 aFrameCksum[2] = {0, 0};
60556 int iLock; /* Lock offset to lock for checkpoint */
60557
60558 /* Obtain an exclusive lock on all byte in the locking range not already
60559 ** locked by the caller. The caller is guaranteed to have locked the
60560 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
60561 ** If successful, the same bytes that are locked here are unlocked before
60562 ** this function returns.
60563 */
60564 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
60565 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
60566 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
60567 assert( pWal->writeLock );
60568 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
60569 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
60570 if( rc ){
60571 return rc;
60572 }
60573
60574 WALTRACE(("WAL%p: recovery begin...\n", pWal));
60575
60576 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
60577
60578 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
60579 if( rc!=SQLITE_OK ){
60580 goto recovery_error;
60581 }
60582
60583 if( nSize>WAL_HDRSIZE ){
60584 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
60585 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
60586 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
60587 int szFrame; /* Number of bytes in buffer aFrame[] */
60588 u8 *aData; /* Pointer to data part of aFrame buffer */
60589 int szPage; /* Page size according to the log */
60590 u32 magic; /* Magic value read from WAL header */
60591 u32 version; /* Magic value read from WAL header */
60592 int isValid; /* True if this frame is valid */
60593 u32 iPg; /* Current 32KB wal-index page */
60594 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
60595
60596 /* Read in the WAL header. */
60597 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
60598 if( rc!=SQLITE_OK ){
60599 goto recovery_error;
60600 }
60601
60602 /* If the database page size is not a power of two, or is greater than
60603 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
60604 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
60605 ** WAL file.
60606 */
60607 magic = sqlite3Get4byte(&aBuf[0]);
60608 szPage = sqlite3Get4byte(&aBuf[8]);
60609 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
60610 || szPage&(szPage-1)
60611 || szPage>SQLITE_MAX_PAGE_SIZE
60612 || szPage<512
60613 ){
60614 goto finished;
60615 }
60616 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
60617 pWal->szPage = szPage;
60618 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
60619 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
60620
60621 /* Verify that the WAL header checksum is correct */
60622 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
60623 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
60624 );
60625 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
60626 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
60627 ){
60628 goto finished;
60629 }
60630
60631 /* Verify that the version number on the WAL format is one that
60632 ** are able to understand */
60633 version = sqlite3Get4byte(&aBuf[4]);
60634 if( version!=WAL_MAX_VERSION ){
60635 rc = SQLITE_CANTOPEN_BKPT;
60636 goto finished;
60637 }
60638
60639 /* Malloc a buffer to read frames into. */
60640 szFrame = szPage + WAL_FRAME_HDRSIZE;
60641 aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ);
60642 if( !aFrame ){
60643 rc = SQLITE_NOMEM_BKPT;
60644 goto recovery_error;
60645 }
60646 aData = &aFrame[WAL_FRAME_HDRSIZE];
60647 aPrivate = (u32*)&aData[szPage];
60648
60649 /* Read all frames from the log file. */
60650 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
60651 for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){
60652 u32 *aShare;
60653 u32 iFrame; /* Index of last frame read */
60654 u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE);
60655 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
60656 u32 nHdr, nHdr32;
60657 rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
60658 if( rc ) break;
60659 pWal->apWiData[iPg] = aPrivate;
60660
60661 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
60662 i64 iOffset = walFrameOffset(iFrame, szPage);
60663 u32 pgno; /* Database page number for frame */
60664 u32 nTruncate; /* dbsize field from frame header */
60665
60666 /* Read and decode the next log frame. */
60667 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
60668 if( rc!=SQLITE_OK ) break;
60669 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
60670 if( !isValid ) break;
60671 rc = walIndexAppend(pWal, iFrame, pgno);
60672 if( NEVER(rc!=SQLITE_OK) ) break;
60673
60674 /* If nTruncate is non-zero, this is a commit record. */
60675 if( nTruncate ){
60676 pWal->hdr.mxFrame = iFrame;
60677 pWal->hdr.nPage = nTruncate;
60678 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
60679 testcase( szPage<=32768 );
60680 testcase( szPage>=65536 );
60681 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
60682 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
60683 }
60684 }
60685 pWal->apWiData[iPg] = aShare;
60686 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0);
60687 nHdr32 = nHdr / sizeof(u32);
60688#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
60689 /* Memcpy() should work fine here, on all reasonable implementations.
60690 ** Technically, memcpy() might change the destination to some
60691 ** intermediate value before setting to the final value, and that might
60692 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
60693 ** do that, according to the spec, but no memcpy() implementation that
60694 ** we know of actually does that, which is why we say that memcpy()
60695 ** is safe for this. Memcpy() is certainly a lot faster.
60696 */
60697 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
60698#else
60699 /* In the event that some platform is found for which memcpy()
60700 ** changes the destination to some intermediate value before
60701 ** setting the final value, this alternative copy routine is
60702 ** provided.
60703 */
60704 {
60705 int i;
60706 for(i=nHdr32; i<WALINDEX_PGSZ/sizeof(u32); i++){
60707 if( aShare[i]!=aPrivate[i] ){
60708 /* Atomic memory operations are not required here because if
60709 ** the value needs to be changed, that means it is not being
60710 ** accessed concurrently. */
60711 aShare[i] = aPrivate[i];
60712 }
60713 }
60714 }
60715#endif
60716 if( iFrame<=iLast ) break;
60717 }
60718
60719 sqlite3_free(aFrame);
60720 }
60721
60722finished:
60723 if( rc==SQLITE_OK ){
60724 volatile WalCkptInfo *pInfo;
60725 int i;
60726 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
60727 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
60728 walIndexWriteHdr(pWal);
60729
60730 /* Reset the checkpoint-header. This is safe because this thread is
60731 ** currently holding locks that exclude all other writers and
60732 ** checkpointers. Then set the values of read-mark slots 1 through N.
60733 */
60734 pInfo = walCkptInfo(pWal);
60735 pInfo->nBackfill = 0;
60736 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
60737 pInfo->aReadMark[0] = 0;
60738 for(i=1; i<WAL_NREADER; i++){
60739 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
60740 if( rc==SQLITE_OK ){
60741 if( i==1 && pWal->hdr.mxFrame ){
60742 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
60743 }else{
60744 pInfo->aReadMark[i] = READMARK_NOT_USED;
60745 }
60746 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
60747 }else if( rc!=SQLITE_BUSY ){
60748 goto recovery_error;
60749 }
60750 }
60751
60752 /* If more than one frame was recovered from the log file, report an
60753 ** event via sqlite3_log(). This is to help with identifying performance
60754 ** problems caused by applications routinely shutting down without
60755 ** checkpointing the log file.
60756 */
60757 if( pWal->hdr.nPage ){
60758 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
60759 "recovered %d frames from WAL file %s",
60760 pWal->hdr.mxFrame, pWal->zWalName
60761 );
60762 }
60763 }
60764
60765recovery_error:
60766 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
60767 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
60768 return rc;
60769}
60770
60771/*
60772** Close an open wal-index.
60773*/
60774static void walIndexClose(Wal *pWal, int isDelete){
60775 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
60776 int i;
60777 for(i=0; i<pWal->nWiData; i++){
60778 sqlite3_free((void *)pWal->apWiData[i]);
60779 pWal->apWiData[i] = 0;
60780 }
60781 }
60782 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
60783 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
60784 }
60785}
60786
60787/*
60788** Open a connection to the WAL file zWalName. The database file must
60789** already be opened on connection pDbFd. The buffer that zWalName points
60790** to must remain valid for the lifetime of the returned Wal* handle.
60791**
60792** A SHARED lock should be held on the database file when this function
60793** is called. The purpose of this SHARED lock is to prevent any other
60794** client from unlinking the WAL or wal-index file. If another process
60795** were to do this just after this client opened one of these files, the
60796** system would be badly broken.
60797**
60798** If the log file is successfully opened, SQLITE_OK is returned and
60799** *ppWal is set to point to a new WAL handle. If an error occurs,
60800** an SQLite error code is returned and *ppWal is left unmodified.
60801*/
60802SQLITE_PRIVATE int sqlite3WalOpen(
60803 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
60804 sqlite3_file *pDbFd, /* The open database file */
60805 const char *zWalName, /* Name of the WAL file */
60806 int bNoShm, /* True to run in heap-memory mode */
60807 i64 mxWalSize, /* Truncate WAL to this size on reset */
60808 Wal **ppWal /* OUT: Allocated Wal handle */
60809){
60810 int rc; /* Return Code */
60811 Wal *pRet; /* Object to allocate and return */
60812 int flags; /* Flags passed to OsOpen() */
60813
60814 assert( zWalName && zWalName[0] );
60815 assert( pDbFd );
60816
60817 /* In the amalgamation, the os_unix.c and os_win.c source files come before
60818 ** this source file. Verify that the #defines of the locking byte offsets
60819 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
60820 ** For that matter, if the lock offset ever changes from its initial design
60821 ** value of 120, we need to know that so there is an assert() to check it.
60822 */
60823 assert( 120==WALINDEX_LOCK_OFFSET );
60824 assert( 136==WALINDEX_HDR_SIZE );
60825#ifdef WIN_SHM_BASE
60826 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
60827#endif
60828#ifdef UNIX_SHM_BASE
60829 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
60830#endif
60831
60832
60833 /* Allocate an instance of struct Wal to return. */
60834 *ppWal = 0;
60835 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
60836 if( !pRet ){
60837 return SQLITE_NOMEM_BKPT;
60838 }
60839
60840 pRet->pVfs = pVfs;
60841 pRet->pWalFd = (sqlite3_file *)&pRet[1];
60842 pRet->pDbFd = pDbFd;
60843 pRet->readLock = -1;
60844 pRet->mxWalSize = mxWalSize;
60845 pRet->zWalName = zWalName;
60846 pRet->syncHeader = 1;
60847 pRet->padToSectorBoundary = 1;
60848 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
60849
60850 /* Open file handle on the write-ahead log file. */
60851 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
60852 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
60853 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
60854 pRet->readOnly = WAL_RDONLY;
60855 }
60856
60857 if( rc!=SQLITE_OK ){
60858 walIndexClose(pRet, 0);
60859 sqlite3OsClose(pRet->pWalFd);
60860 sqlite3_free(pRet);
60861 }else{
60862 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
60863 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
60864 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
60865 pRet->padToSectorBoundary = 0;
60866 }
60867 *ppWal = pRet;
60868 WALTRACE(("WAL%d: opened\n", pRet));
60869 }
60870 return rc;
60871}
60872
60873/*
60874** Change the size to which the WAL file is trucated on each reset.
60875*/
60876SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
60877 if( pWal ) pWal->mxWalSize = iLimit;
60878}
60879
60880/*
60881** Find the smallest page number out of all pages held in the WAL that
60882** has not been returned by any prior invocation of this method on the
60883** same WalIterator object. Write into *piFrame the frame index where
60884** that page was last written into the WAL. Write into *piPage the page
60885** number.
60886**
60887** Return 0 on success. If there are no pages in the WAL with a page
60888** number larger than *piPage, then return 1.
60889*/
60890static int walIteratorNext(
60891 WalIterator *p, /* Iterator */
60892 u32 *piPage, /* OUT: The page number of the next page */
60893 u32 *piFrame /* OUT: Wal frame index of next page */
60894){
60895 u32 iMin; /* Result pgno must be greater than iMin */
60896 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
60897 int i; /* For looping through segments */
60898
60899 iMin = p->iPrior;
60900 assert( iMin<0xffffffff );
60901 for(i=p->nSegment-1; i>=0; i--){
60902 struct WalSegment *pSegment = &p->aSegment[i];
60903 while( pSegment->iNext<pSegment->nEntry ){
60904 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
60905 if( iPg>iMin ){
60906 if( iPg<iRet ){
60907 iRet = iPg;
60908 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
60909 }
60910 break;
60911 }
60912 pSegment->iNext++;
60913 }
60914 }
60915
60916 *piPage = p->iPrior = iRet;
60917 return (iRet==0xFFFFFFFF);
60918}
60919
60920/*
60921** This function merges two sorted lists into a single sorted list.
60922**
60923** aLeft[] and aRight[] are arrays of indices. The sort key is
60924** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
60925** is guaranteed for all J<K:
60926**
60927** aContent[aLeft[J]] < aContent[aLeft[K]]
60928** aContent[aRight[J]] < aContent[aRight[K]]
60929**
60930** This routine overwrites aRight[] with a new (probably longer) sequence
60931** of indices such that the aRight[] contains every index that appears in
60932** either aLeft[] or the old aRight[] and such that the second condition
60933** above is still met.
60934**
60935** The aContent[aLeft[X]] values will be unique for all X. And the
60936** aContent[aRight[X]] values will be unique too. But there might be
60937** one or more combinations of X and Y such that
60938**
60939** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
60940**
60941** When that happens, omit the aLeft[X] and use the aRight[Y] index.
60942*/
60943static void walMerge(
60944 const u32 *aContent, /* Pages in wal - keys for the sort */
60945 ht_slot *aLeft, /* IN: Left hand input list */
60946 int nLeft, /* IN: Elements in array *paLeft */
60947 ht_slot **paRight, /* IN/OUT: Right hand input list */
60948 int *pnRight, /* IN/OUT: Elements in *paRight */
60949 ht_slot *aTmp /* Temporary buffer */
60950){
60951 int iLeft = 0; /* Current index in aLeft */
60952 int iRight = 0; /* Current index in aRight */
60953 int iOut = 0; /* Current index in output buffer */
60954 int nRight = *pnRight;
60955 ht_slot *aRight = *paRight;
60956
60957 assert( nLeft>0 && nRight>0 );
60958 while( iRight<nRight || iLeft<nLeft ){
60959 ht_slot logpage;
60960 Pgno dbpage;
60961
60962 if( (iLeft<nLeft)
60963 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
60964 ){
60965 logpage = aLeft[iLeft++];
60966 }else{
60967 logpage = aRight[iRight++];
60968 }
60969 dbpage = aContent[logpage];
60970
60971 aTmp[iOut++] = logpage;
60972 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
60973
60974 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
60975 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
60976 }
60977
60978 *paRight = aLeft;
60979 *pnRight = iOut;
60980 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
60981}
60982
60983/*
60984** Sort the elements in list aList using aContent[] as the sort key.
60985** Remove elements with duplicate keys, preferring to keep the
60986** larger aList[] values.
60987**
60988** The aList[] entries are indices into aContent[]. The values in
60989** aList[] are to be sorted so that for all J<K:
60990**
60991** aContent[aList[J]] < aContent[aList[K]]
60992**
60993** For any X and Y such that
60994**
60995** aContent[aList[X]] == aContent[aList[Y]]
60996**
60997** Keep the larger of the two values aList[X] and aList[Y] and discard
60998** the smaller.
60999*/
61000static void walMergesort(
61001 const u32 *aContent, /* Pages in wal */
61002 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
61003 ht_slot *aList, /* IN/OUT: List to sort */
61004 int *pnList /* IN/OUT: Number of elements in aList[] */
61005){
61006 struct Sublist {
61007 int nList; /* Number of elements in aList */
61008 ht_slot *aList; /* Pointer to sub-list content */
61009 };
61010
61011 const int nList = *pnList; /* Size of input list */
61012 int nMerge = 0; /* Number of elements in list aMerge */
61013 ht_slot *aMerge = 0; /* List to be merged */
61014 int iList; /* Index into input list */
61015 u32 iSub = 0; /* Index into aSub array */
61016 struct Sublist aSub[13]; /* Array of sub-lists */
61017
61018 memset(aSub, 0, sizeof(aSub));
61019 assert( nList<=HASHTABLE_NPAGE && nList>0 );
61020 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
61021
61022 for(iList=0; iList<nList; iList++){
61023 nMerge = 1;
61024 aMerge = &aList[iList];
61025 for(iSub=0; iList & (1<<iSub); iSub++){
61026 struct Sublist *p;
61027 assert( iSub<ArraySize(aSub) );
61028 p = &aSub[iSub];
61029 assert( p->aList && p->nList<=(1<<iSub) );
61030 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
61031 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
61032 }
61033 aSub[iSub].aList = aMerge;
61034 aSub[iSub].nList = nMerge;
61035 }
61036
61037 for(iSub++; iSub<ArraySize(aSub); iSub++){
61038 if( nList & (1<<iSub) ){
61039 struct Sublist *p;
61040 assert( iSub<ArraySize(aSub) );
61041 p = &aSub[iSub];
61042 assert( p->nList<=(1<<iSub) );
61043 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
61044 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
61045 }
61046 }
61047 assert( aMerge==aList );
61048 *pnList = nMerge;
61049
61050#ifdef SQLITE_DEBUG
61051 {
61052 int i;
61053 for(i=1; i<*pnList; i++){
61054 assert( aContent[aList[i]] > aContent[aList[i-1]] );
61055 }
61056 }
61057#endif
61058}
61059
61060/*
61061** Free an iterator allocated by walIteratorInit().
61062*/
61063static void walIteratorFree(WalIterator *p){
61064 sqlite3_free(p);
61065}
61066
61067/*
61068** Construct a WalInterator object that can be used to loop over all
61069** pages in the WAL following frame nBackfill in ascending order. Frames
61070** nBackfill or earlier may be included - excluding them is an optimization
61071** only. The caller must hold the checkpoint lock.
61072**
61073** On success, make *pp point to the newly allocated WalInterator object
61074** return SQLITE_OK. Otherwise, return an error code. If this routine
61075** returns an error, the value of *pp is undefined.
61076**
61077** The calling routine should invoke walIteratorFree() to destroy the
61078** WalIterator object when it has finished with it.
61079*/
61080static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
61081 WalIterator *p; /* Return value */
61082 int nSegment; /* Number of segments to merge */
61083 u32 iLast; /* Last frame in log */
61084 sqlite3_int64 nByte; /* Number of bytes to allocate */
61085 int i; /* Iterator variable */
61086 ht_slot *aTmp; /* Temp space used by merge-sort */
61087 int rc = SQLITE_OK; /* Return Code */
61088
61089 /* This routine only runs while holding the checkpoint lock. And
61090 ** it only runs if there is actually content in the log (mxFrame>0).
61091 */
61092 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
61093 iLast = pWal->hdr.mxFrame;
61094
61095 /* Allocate space for the WalIterator object. */
61096 nSegment = walFramePage(iLast) + 1;
61097 nByte = sizeof(WalIterator)
61098 + (nSegment-1)*sizeof(struct WalSegment)
61099 + iLast*sizeof(ht_slot);
61100 p = (WalIterator *)sqlite3_malloc64(nByte);
61101 if( !p ){
61102 return SQLITE_NOMEM_BKPT;
61103 }
61104 memset(p, 0, nByte);
61105 p->nSegment = nSegment;
61106
61107 /* Allocate temporary space used by the merge-sort routine. This block
61108 ** of memory will be freed before this function returns.
61109 */
61110 aTmp = (ht_slot *)sqlite3_malloc64(
61111 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
61112 );
61113 if( !aTmp ){
61114 rc = SQLITE_NOMEM_BKPT;
61115 }
61116
61117 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
61118 WalHashLoc sLoc;
61119
61120 rc = walHashGet(pWal, i, &sLoc);
61121 if( rc==SQLITE_OK ){
61122 int j; /* Counter variable */
61123 int nEntry; /* Number of entries in this segment */
61124 ht_slot *aIndex; /* Sorted index for this segment */
61125
61126 sLoc.aPgno++;
61127 if( (i+1)==nSegment ){
61128 nEntry = (int)(iLast - sLoc.iZero);
61129 }else{
61130 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
61131 }
61132 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
61133 sLoc.iZero++;
61134
61135 for(j=0; j<nEntry; j++){
61136 aIndex[j] = (ht_slot)j;
61137 }
61138 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
61139 p->aSegment[i].iZero = sLoc.iZero;
61140 p->aSegment[i].nEntry = nEntry;
61141 p->aSegment[i].aIndex = aIndex;
61142 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
61143 }
61144 }
61145 sqlite3_free(aTmp);
61146
61147 if( rc!=SQLITE_OK ){
61148 walIteratorFree(p);
61149 p = 0;
61150 }
61151 *pp = p;
61152 return rc;
61153}
61154
61155#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
61156/*
61157** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
61158** they are supported by the VFS, and (b) the database handle is configured
61159** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
61160** or 0 otherwise.
61161*/
61162static int walEnableBlocking(Wal *pWal){
61163 int res = 0;
61164 if( pWal->db ){
61165 int tmout = pWal->db->busyTimeout;
61166 if( tmout ){
61167 int rc;
61168 rc = sqlite3OsFileControl(
61169 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
61170 );
61171 res = (rc==SQLITE_OK);
61172 }
61173 }
61174 return res;
61175}
61176
61177/*
61178** Disable blocking locks.
61179*/
61180static void walDisableBlocking(Wal *pWal){
61181 int tmout = 0;
61182 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
61183}
61184
61185/*
61186** If parameter bLock is true, attempt to enable blocking locks, take
61187** the WRITER lock, and then disable blocking locks. If blocking locks
61188** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
61189** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
61190** an error if blocking locks can not be enabled.
61191**
61192** If the bLock parameter is false and the WRITER lock is held, release it.
61193*/
61194SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){
61195 int rc = SQLITE_OK;
61196 assert( pWal->readLock<0 || bLock==0 );
61197 if( bLock ){
61198 assert( pWal->db );
61199 if( walEnableBlocking(pWal) ){
61200 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
61201 if( rc==SQLITE_OK ){
61202 pWal->writeLock = 1;
61203 }
61204 walDisableBlocking(pWal);
61205 }
61206 }else if( pWal->writeLock ){
61207 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61208 pWal->writeLock = 0;
61209 }
61210 return rc;
61211}
61212
61213/*
61214** Set the database handle used to determine if blocking locks are required.
61215*/
61216SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){
61217 pWal->db = db;
61218}
61219
61220/*
61221** Take an exclusive WRITE lock. Blocking if so configured.
61222*/
61223static int walLockWriter(Wal *pWal){
61224 int rc;
61225 walEnableBlocking(pWal);
61226 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
61227 walDisableBlocking(pWal);
61228 return rc;
61229}
61230#else
61231# define walEnableBlocking(x) 0
61232# define walDisableBlocking(x)
61233# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1)
61234# define sqlite3WalDb(pWal, db)
61235#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
61236
61237
61238/*
61239** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
61240** n. If the attempt fails and parameter xBusy is not NULL, then it is a
61241** busy-handler function. Invoke it and retry the lock until either the
61242** lock is successfully obtained or the busy-handler returns 0.
61243*/
61244static int walBusyLock(
61245 Wal *pWal, /* WAL connection */
61246 int (*xBusy)(void*), /* Function to call when busy */
61247 void *pBusyArg, /* Context argument for xBusyHandler */
61248 int lockIdx, /* Offset of first byte to lock */
61249 int n /* Number of bytes to lock */
61250){
61251 int rc;
61252 do {
61253 rc = walLockExclusive(pWal, lockIdx, n);
61254 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
61255#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
61256 if( rc==SQLITE_BUSY_TIMEOUT ){
61257 walDisableBlocking(pWal);
61258 rc = SQLITE_BUSY;
61259 }
61260#endif
61261 return rc;
61262}
61263
61264/*
61265** The cache of the wal-index header must be valid to call this function.
61266** Return the page-size in bytes used by the database.
61267*/
61268static int walPagesize(Wal *pWal){
61269 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
61270}
61271
61272/*
61273** The following is guaranteed when this function is called:
61274**
61275** a) the WRITER lock is held,
61276** b) the entire log file has been checkpointed, and
61277** c) any existing readers are reading exclusively from the database
61278** file - there are no readers that may attempt to read a frame from
61279** the log file.
61280**
61281** This function updates the shared-memory structures so that the next
61282** client to write to the database (which may be this one) does so by
61283** writing frames into the start of the log file.
61284**
61285** The value of parameter salt1 is used as the aSalt[1] value in the
61286** new wal-index header. It should be passed a pseudo-random value (i.e.
61287** one obtained from sqlite3_randomness()).
61288*/
61289static void walRestartHdr(Wal *pWal, u32 salt1){
61290 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61291 int i; /* Loop counter */
61292 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
61293 pWal->nCkpt++;
61294 pWal->hdr.mxFrame = 0;
61295 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
61296 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
61297 walIndexWriteHdr(pWal);
61298 AtomicStore(&pInfo->nBackfill, 0);
61299 pInfo->nBackfillAttempted = 0;
61300 pInfo->aReadMark[1] = 0;
61301 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
61302 assert( pInfo->aReadMark[0]==0 );
61303}
61304
61305/*
61306** Copy as much content as we can from the WAL back into the database file
61307** in response to an sqlite3_wal_checkpoint() request or the equivalent.
61308**
61309** The amount of information copies from WAL to database might be limited
61310** by active readers. This routine will never overwrite a database page
61311** that a concurrent reader might be using.
61312**
61313** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
61314** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
61315** checkpoints are always run by a background thread or background
61316** process, foreground threads will never block on a lengthy fsync call.
61317**
61318** Fsync is called on the WAL before writing content out of the WAL and
61319** into the database. This ensures that if the new content is persistent
61320** in the WAL and can be recovered following a power-loss or hard reset.
61321**
61322** Fsync is also called on the database file if (and only if) the entire
61323** WAL content is copied into the database file. This second fsync makes
61324** it safe to delete the WAL since the new content will persist in the
61325** database file.
61326**
61327** This routine uses and updates the nBackfill field of the wal-index header.
61328** This is the only routine that will increase the value of nBackfill.
61329** (A WAL reset or recovery will revert nBackfill to zero, but not increase
61330** its value.)
61331**
61332** The caller must be holding sufficient locks to ensure that no other
61333** checkpoint is running (in any other thread or process) at the same
61334** time.
61335*/
61336static int walCheckpoint(
61337 Wal *pWal, /* Wal connection */
61338 sqlite3 *db, /* Check for interrupts on this handle */
61339 int eMode, /* One of PASSIVE, FULL or RESTART */
61340 int (*xBusy)(void*), /* Function to call when busy */
61341 void *pBusyArg, /* Context argument for xBusyHandler */
61342 int sync_flags, /* Flags for OsSync() (or 0) */
61343 u8 *zBuf /* Temporary buffer to use */
61344){
61345 int rc = SQLITE_OK; /* Return code */
61346 int szPage; /* Database page-size */
61347 WalIterator *pIter = 0; /* Wal iterator context */
61348 u32 iDbpage = 0; /* Next database page to write */
61349 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
61350 u32 mxSafeFrame; /* Max frame that can be backfilled */
61351 u32 mxPage; /* Max database page to write */
61352 int i; /* Loop counter */
61353 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
61354
61355 szPage = walPagesize(pWal);
61356 testcase( szPage<=32768 );
61357 testcase( szPage>=65536 );
61358 pInfo = walCkptInfo(pWal);
61359 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61360
61361 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61362 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
61363 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
61364
61365 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
61366 ** safe to write into the database. Frames beyond mxSafeFrame might
61367 ** overwrite database pages that are in use by active readers and thus
61368 ** cannot be backfilled from the WAL.
61369 */
61370 mxSafeFrame = pWal->hdr.mxFrame;
61371 mxPage = pWal->hdr.nPage;
61372 for(i=1; i<WAL_NREADER; i++){
61373 u32 y = AtomicLoad(pInfo->aReadMark+i);
61374 if( mxSafeFrame>y ){
61375 assert( y<=pWal->hdr.mxFrame );
61376 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
61377 if( rc==SQLITE_OK ){
61378 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
61379 AtomicStore(pInfo->aReadMark+i, iMark);
61380 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
61381 }else if( rc==SQLITE_BUSY ){
61382 mxSafeFrame = y;
61383 xBusy = 0;
61384 }else{
61385 goto walcheckpoint_out;
61386 }
61387 }
61388 }
61389
61390 /* Allocate the iterator */
61391 if( pInfo->nBackfill<mxSafeFrame ){
61392 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
61393 assert( rc==SQLITE_OK || pIter==0 );
61394 }
61395
61396 if( pIter
61397 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK
61398 ){
61399 u32 nBackfill = pInfo->nBackfill;
61400
61401 pInfo->nBackfillAttempted = mxSafeFrame;
61402
61403 /* Sync the WAL to disk */
61404 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
61405
61406 /* If the database may grow as a result of this checkpoint, hint
61407 ** about the eventual size of the db file to the VFS layer.
61408 */
61409 if( rc==SQLITE_OK ){
61410 i64 nReq = ((i64)mxPage * szPage);
61411 i64 nSize; /* Current size of database file */
61412 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
61413 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
61414 if( rc==SQLITE_OK && nSize<nReq ){
61415 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
61416 /* If the size of the final database is larger than the current
61417 ** database plus the amount of data in the wal file, plus the
61418 ** maximum size of the pending-byte page (65536 bytes), then
61419 ** must be corruption somewhere. */
61420 rc = SQLITE_CORRUPT_BKPT;
61421 }else{
61422 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
61423 }
61424 }
61425
61426 }
61427
61428 /* Iterate through the contents of the WAL, copying data to the db file */
61429 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
61430 i64 iOffset;
61431 assert( walFramePgno(pWal, iFrame)==iDbpage );
61432 if( AtomicLoad(&db->u1.isInterrupted) ){
61433 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
61434 break;
61435 }
61436 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
61437 continue;
61438 }
61439 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
61440 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
61441 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
61442 if( rc!=SQLITE_OK ) break;
61443 iOffset = (iDbpage-1)*(i64)szPage;
61444 testcase( IS_BIG_INT(iOffset) );
61445 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
61446 if( rc!=SQLITE_OK ) break;
61447 }
61448 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
61449
61450 /* If work was actually accomplished... */
61451 if( rc==SQLITE_OK ){
61452 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
61453 i64 szDb = pWal->hdr.nPage*(i64)szPage;
61454 testcase( IS_BIG_INT(szDb) );
61455 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
61456 if( rc==SQLITE_OK ){
61457 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
61458 }
61459 }
61460 if( rc==SQLITE_OK ){
61461 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
61462 }
61463 }
61464
61465 /* Release the reader lock held while backfilling */
61466 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
61467 }
61468
61469 if( rc==SQLITE_BUSY ){
61470 /* Reset the return code so as not to report a checkpoint failure
61471 ** just because there are active readers. */
61472 rc = SQLITE_OK;
61473 }
61474 }
61475
61476 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
61477 ** entire wal file has been copied into the database file, then block
61478 ** until all readers have finished using the wal file. This ensures that
61479 ** the next process to write to the database restarts the wal file.
61480 */
61481 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
61482 assert( pWal->writeLock );
61483 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61484 rc = SQLITE_BUSY;
61485 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
61486 u32 salt1;
61487 sqlite3_randomness(4, &salt1);
61488 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
61489 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
61490 if( rc==SQLITE_OK ){
61491 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
61492 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
61493 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
61494 ** truncates the log file to zero bytes just prior to a
61495 ** successful return.
61496 **
61497 ** In theory, it might be safe to do this without updating the
61498 ** wal-index header in shared memory, as all subsequent reader or
61499 ** writer clients should see that the entire log file has been
61500 ** checkpointed and behave accordingly. This seems unsafe though,
61501 ** as it would leave the system in a state where the contents of
61502 ** the wal-index header do not match the contents of the
61503 ** file-system. To avoid this, update the wal-index header to
61504 ** indicate that the log file contains zero valid frames. */
61505 walRestartHdr(pWal, salt1);
61506 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
61507 }
61508 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
61509 }
61510 }
61511 }
61512
61513 walcheckpoint_out:
61514 walIteratorFree(pIter);
61515 return rc;
61516}
61517
61518/*
61519** If the WAL file is currently larger than nMax bytes in size, truncate
61520** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
61521*/
61522static void walLimitSize(Wal *pWal, i64 nMax){
61523 i64 sz;
61524 int rx;
61525 sqlite3BeginBenignMalloc();
61526 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
61527 if( rx==SQLITE_OK && (sz > nMax ) ){
61528 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
61529 }
61530 sqlite3EndBenignMalloc();
61531 if( rx ){
61532 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
61533 }
61534}
61535
61536/*
61537** Close a connection to a log file.
61538*/
61539SQLITE_PRIVATE int sqlite3WalClose(
61540 Wal *pWal, /* Wal to close */
61541 sqlite3 *db, /* For interrupt flag */
61542 int sync_flags, /* Flags to pass to OsSync() (or 0) */
61543 int nBuf,
61544 u8 *zBuf /* Buffer of at least nBuf bytes */
61545){
61546 int rc = SQLITE_OK;
61547 if( pWal ){
61548 int isDelete = 0; /* True to unlink wal and wal-index files */
61549
61550 /* If an EXCLUSIVE lock can be obtained on the database file (using the
61551 ** ordinary, rollback-mode locking methods, this guarantees that the
61552 ** connection associated with this log file is the only connection to
61553 ** the database. In this case checkpoint the database and unlink both
61554 ** the wal and wal-index files.
61555 **
61556 ** The EXCLUSIVE lock is not released before returning.
61557 */
61558 if( zBuf!=0
61559 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
61560 ){
61561 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
61562 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61563 }
61564 rc = sqlite3WalCheckpoint(pWal, db,
61565 SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
61566 );
61567 if( rc==SQLITE_OK ){
61568 int bPersist = -1;
61569 sqlite3OsFileControlHint(
61570 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
61571 );
61572 if( bPersist!=1 ){
61573 /* Try to delete the WAL file if the checkpoint completed and
61574 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
61575 ** mode (!bPersist) */
61576 isDelete = 1;
61577 }else if( pWal->mxWalSize>=0 ){
61578 /* Try to truncate the WAL file to zero bytes if the checkpoint
61579 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
61580 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
61581 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
61582 ** to zero bytes as truncating to the journal_size_limit might
61583 ** leave a corrupt WAL file on disk. */
61584 walLimitSize(pWal, 0);
61585 }
61586 }
61587 }
61588
61589 walIndexClose(pWal, isDelete);
61590 sqlite3OsClose(pWal->pWalFd);
61591 if( isDelete ){
61592 sqlite3BeginBenignMalloc();
61593 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
61594 sqlite3EndBenignMalloc();
61595 }
61596 WALTRACE(("WAL%p: closed\n", pWal));
61597 sqlite3_free((void *)pWal->apWiData);
61598 sqlite3_free(pWal);
61599 }
61600 return rc;
61601}
61602
61603/*
61604** Try to read the wal-index header. Return 0 on success and 1 if
61605** there is a problem.
61606**
61607** The wal-index is in shared memory. Another thread or process might
61608** be writing the header at the same time this procedure is trying to
61609** read it, which might result in inconsistency. A dirty read is detected
61610** by verifying that both copies of the header are the same and also by
61611** a checksum on the header.
61612**
61613** If and only if the read is consistent and the header is different from
61614** pWal->hdr, then pWal->hdr is updated to the content of the new header
61615** and *pChanged is set to 1.
61616**
61617** If the checksum cannot be verified return non-zero. If the header
61618** is read successfully and the checksum verified, return zero.
61619*/
61620static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
61621 u32 aCksum[2]; /* Checksum on the header content */
61622 WalIndexHdr h1, h2; /* Two copies of the header content */
61623 WalIndexHdr volatile *aHdr; /* Header in shared memory */
61624
61625 /* The first page of the wal-index must be mapped at this point. */
61626 assert( pWal->nWiData>0 && pWal->apWiData[0] );
61627
61628 /* Read the header. This might happen concurrently with a write to the
61629 ** same area of shared memory on a different CPU in a SMP,
61630 ** meaning it is possible that an inconsistent snapshot is read
61631 ** from the file. If this happens, return non-zero.
61632 **
61633 ** tag-20200519-1:
61634 ** There are two copies of the header at the beginning of the wal-index.
61635 ** When reading, read [0] first then [1]. Writes are in the reverse order.
61636 ** Memory barriers are used to prevent the compiler or the hardware from
61637 ** reordering the reads and writes. TSAN and similar tools can sometimes
61638 ** give false-positive warnings about these accesses because the tools do not
61639 ** account for the double-read and the memory barrier. The use of mutexes
61640 ** here would be problematic as the memory being accessed is potentially
61641 ** shared among multiple processes and not all mutex implementions work
61642 ** reliably in that environment.
61643 */
61644 aHdr = walIndexHdr(pWal);
61645 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
61646 walShmBarrier(pWal);
61647 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
61648
61649 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
61650 return 1; /* Dirty read */
61651 }
61652 if( h1.isInit==0 ){
61653 return 1; /* Malformed header - probably all zeros */
61654 }
61655 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
61656 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
61657 return 1; /* Checksum does not match */
61658 }
61659
61660 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
61661 *pChanged = 1;
61662 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
61663 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
61664 testcase( pWal->szPage<=32768 );
61665 testcase( pWal->szPage>=65536 );
61666 }
61667
61668 /* The header was successfully read. Return zero. */
61669 return 0;
61670}
61671
61672/*
61673** This is the value that walTryBeginRead returns when it needs to
61674** be retried.
61675*/
61676#define WAL_RETRY (-1)
61677
61678/*
61679** Read the wal-index header from the wal-index and into pWal->hdr.
61680** If the wal-header appears to be corrupt, try to reconstruct the
61681** wal-index from the WAL before returning.
61682**
61683** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
61684** changed by this operation. If pWal->hdr is unchanged, set *pChanged
61685** to 0.
61686**
61687** If the wal-index header is successfully read, return SQLITE_OK.
61688** Otherwise an SQLite error code.
61689*/
61690static int walIndexReadHdr(Wal *pWal, int *pChanged){
61691 int rc; /* Return code */
61692 int badHdr; /* True if a header read failed */
61693 volatile u32 *page0; /* Chunk of wal-index containing header */
61694
61695 /* Ensure that page 0 of the wal-index (the page that contains the
61696 ** wal-index header) is mapped. Return early if an error occurs here.
61697 */
61698 assert( pChanged );
61699 rc = walIndexPage(pWal, 0, &page0);
61700 if( rc!=SQLITE_OK ){
61701 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
61702 if( rc==SQLITE_READONLY_CANTINIT ){
61703 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
61704 ** was openable but is not writable, and this thread is unable to
61705 ** confirm that another write-capable connection has the shared-memory
61706 ** open, and hence the content of the shared-memory is unreliable,
61707 ** since the shared-memory might be inconsistent with the WAL file
61708 ** and there is no writer on hand to fix it. */
61709 assert( page0==0 );
61710 assert( pWal->writeLock==0 );
61711 assert( pWal->readOnly & WAL_SHM_RDONLY );
61712 pWal->bShmUnreliable = 1;
61713 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
61714 *pChanged = 1;
61715 }else{
61716 return rc; /* Any other non-OK return is just an error */
61717 }
61718 }else{
61719 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
61720 ** is zero, which prevents the SHM from growing */
61721 testcase( page0!=0 );
61722 }
61723 assert( page0!=0 || pWal->writeLock==0 );
61724
61725 /* If the first page of the wal-index has been mapped, try to read the
61726 ** wal-index header immediately, without holding any lock. This usually
61727 ** works, but may fail if the wal-index header is corrupt or currently
61728 ** being modified by another thread or process.
61729 */
61730 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
61731
61732 /* If the first attempt failed, it might have been due to a race
61733 ** with a writer. So get a WRITE lock and try again.
61734 */
61735 if( badHdr ){
61736 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
61737 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
61738 walUnlockShared(pWal, WAL_WRITE_LOCK);
61739 rc = SQLITE_READONLY_RECOVERY;
61740 }
61741 }else{
61742 int bWriteLock = pWal->writeLock;
61743 if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){
61744 pWal->writeLock = 1;
61745 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
61746 badHdr = walIndexTryHdr(pWal, pChanged);
61747 if( badHdr ){
61748 /* If the wal-index header is still malformed even while holding
61749 ** a WRITE lock, it can only mean that the header is corrupted and
61750 ** needs to be reconstructed. So run recovery to do exactly that.
61751 */
61752 rc = walIndexRecover(pWal);
61753 *pChanged = 1;
61754 }
61755 }
61756 if( bWriteLock==0 ){
61757 pWal->writeLock = 0;
61758 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
61759 }
61760 }
61761 }
61762 }
61763
61764 /* If the header is read successfully, check the version number to make
61765 ** sure the wal-index was not constructed with some future format that
61766 ** this version of SQLite cannot understand.
61767 */
61768 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
61769 rc = SQLITE_CANTOPEN_BKPT;
61770 }
61771 if( pWal->bShmUnreliable ){
61772 if( rc!=SQLITE_OK ){
61773 walIndexClose(pWal, 0);
61774 pWal->bShmUnreliable = 0;
61775 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
61776 /* walIndexRecover() might have returned SHORT_READ if a concurrent
61777 ** writer truncated the WAL out from under it. If that happens, it
61778 ** indicates that a writer has fixed the SHM file for us, so retry */
61779 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
61780 }
61781 pWal->exclusiveMode = WAL_NORMAL_MODE;
61782 }
61783
61784 return rc;
61785}
61786
61787/*
61788** Open a transaction in a connection where the shared-memory is read-only
61789** and where we cannot verify that there is a separate write-capable connection
61790** on hand to keep the shared-memory up-to-date with the WAL file.
61791**
61792** This can happen, for example, when the shared-memory is implemented by
61793** memory-mapping a *-shm file, where a prior writer has shut down and
61794** left the *-shm file on disk, and now the present connection is trying
61795** to use that database but lacks write permission on the *-shm file.
61796** Other scenarios are also possible, depending on the VFS implementation.
61797**
61798** Precondition:
61799**
61800** The *-wal file has been read and an appropriate wal-index has been
61801** constructed in pWal->apWiData[] using heap memory instead of shared
61802** memory.
61803**
61804** If this function returns SQLITE_OK, then the read transaction has
61805** been successfully opened. In this case output variable (*pChanged)
61806** is set to true before returning if the caller should discard the
61807** contents of the page cache before proceeding. Or, if it returns
61808** WAL_RETRY, then the heap memory wal-index has been discarded and
61809** the caller should retry opening the read transaction from the
61810** beginning (including attempting to map the *-shm file).
61811**
61812** If an error occurs, an SQLite error code is returned.
61813*/
61814static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
61815 i64 szWal; /* Size of wal file on disk in bytes */
61816 i64 iOffset; /* Current offset when reading wal file */
61817 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
61818 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
61819 int szFrame; /* Number of bytes in buffer aFrame[] */
61820 u8 *aData; /* Pointer to data part of aFrame buffer */
61821 volatile void *pDummy; /* Dummy argument for xShmMap */
61822 int rc; /* Return code */
61823 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
61824
61825 assert( pWal->bShmUnreliable );
61826 assert( pWal->readOnly & WAL_SHM_RDONLY );
61827 assert( pWal->nWiData>0 && pWal->apWiData[0] );
61828
61829 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
61830 ** writers from running a checkpoint, but does not stop them
61831 ** from running recovery. */
61832 rc = walLockShared(pWal, WAL_READ_LOCK(0));
61833 if( rc!=SQLITE_OK ){
61834 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
61835 goto begin_unreliable_shm_out;
61836 }
61837 pWal->readLock = 0;
61838
61839 /* Check to see if a separate writer has attached to the shared-memory area,
61840 ** thus making the shared-memory "reliable" again. Do this by invoking
61841 ** the xShmMap() routine of the VFS and looking to see if the return
61842 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
61843 **
61844 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
61845 ** cause the heap-memory WAL-index to be discarded and the actual
61846 ** shared memory to be used in its place.
61847 **
61848 ** This step is important because, even though this connection is holding
61849 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
61850 ** have already checkpointed the WAL file and, while the current
61851 ** is active, wrap the WAL and start overwriting frames that this
61852 ** process wants to use.
61853 **
61854 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
61855 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
61856 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
61857 ** even if some external agent does a "chmod" to make the shared-memory
61858 ** writable by us, until sqlite3OsShmUnmap() has been called.
61859 ** This is a requirement on the VFS implementation.
61860 */
61861 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
61862 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
61863 if( rc!=SQLITE_READONLY_CANTINIT ){
61864 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
61865 goto begin_unreliable_shm_out;
61866 }
61867
61868 /* We reach this point only if the real shared-memory is still unreliable.
61869 ** Assume the in-memory WAL-index substitute is correct and load it
61870 ** into pWal->hdr.
61871 */
61872 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
61873
61874 /* Make sure some writer hasn't come in and changed the WAL file out
61875 ** from under us, then disconnected, while we were not looking.
61876 */
61877 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
61878 if( rc!=SQLITE_OK ){
61879 goto begin_unreliable_shm_out;
61880 }
61881 if( szWal<WAL_HDRSIZE ){
61882 /* If the wal file is too small to contain a wal-header and the
61883 ** wal-index header has mxFrame==0, then it must be safe to proceed
61884 ** reading the database file only. However, the page cache cannot
61885 ** be trusted, as a read/write connection may have connected, written
61886 ** the db, run a checkpoint, truncated the wal file and disconnected
61887 ** since this client's last read transaction. */
61888 *pChanged = 1;
61889 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
61890 goto begin_unreliable_shm_out;
61891 }
61892
61893 /* Check the salt keys at the start of the wal file still match. */
61894 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
61895 if( rc!=SQLITE_OK ){
61896 goto begin_unreliable_shm_out;
61897 }
61898 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
61899 /* Some writer has wrapped the WAL file while we were not looking.
61900 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
61901 ** rebuilt. */
61902 rc = WAL_RETRY;
61903 goto begin_unreliable_shm_out;
61904 }
61905
61906 /* Allocate a buffer to read frames into */
61907 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
61908 aFrame = (u8 *)sqlite3_malloc64(szFrame);
61909 if( aFrame==0 ){
61910 rc = SQLITE_NOMEM_BKPT;
61911 goto begin_unreliable_shm_out;
61912 }
61913 aData = &aFrame[WAL_FRAME_HDRSIZE];
61914
61915 /* Check to see if a complete transaction has been appended to the
61916 ** wal file since the heap-memory wal-index was created. If so, the
61917 ** heap-memory wal-index is discarded and WAL_RETRY returned to
61918 ** the caller. */
61919 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
61920 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
61921 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
61922 iOffset+szFrame<=szWal;
61923 iOffset+=szFrame
61924 ){
61925 u32 pgno; /* Database page number for frame */
61926 u32 nTruncate; /* dbsize field from frame header */
61927
61928 /* Read and decode the next log frame. */
61929 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
61930 if( rc!=SQLITE_OK ) break;
61931 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
61932
61933 /* If nTruncate is non-zero, then a complete transaction has been
61934 ** appended to this wal file. Set rc to WAL_RETRY and break out of
61935 ** the loop. */
61936 if( nTruncate ){
61937 rc = WAL_RETRY;
61938 break;
61939 }
61940 }
61941 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
61942 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
61943
61944 begin_unreliable_shm_out:
61945 sqlite3_free(aFrame);
61946 if( rc!=SQLITE_OK ){
61947 int i;
61948 for(i=0; i<pWal->nWiData; i++){
61949 sqlite3_free((void*)pWal->apWiData[i]);
61950 pWal->apWiData[i] = 0;
61951 }
61952 pWal->bShmUnreliable = 0;
61953 sqlite3WalEndReadTransaction(pWal);
61954 *pChanged = 1;
61955 }
61956 return rc;
61957}
61958
61959/*
61960** Attempt to start a read transaction. This might fail due to a race or
61961** other transient condition. When that happens, it returns WAL_RETRY to
61962** indicate to the caller that it is safe to retry immediately.
61963**
61964** On success return SQLITE_OK. On a permanent failure (such an
61965** I/O error or an SQLITE_BUSY because another process is running
61966** recovery) return a positive error code.
61967**
61968** The useWal parameter is true to force the use of the WAL and disable
61969** the case where the WAL is bypassed because it has been completely
61970** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
61971** to make a copy of the wal-index header into pWal->hdr. If the
61972** wal-index header has changed, *pChanged is set to 1 (as an indication
61973** to the caller that the local page cache is obsolete and needs to be
61974** flushed.) When useWal==1, the wal-index header is assumed to already
61975** be loaded and the pChanged parameter is unused.
61976**
61977** The caller must set the cnt parameter to the number of prior calls to
61978** this routine during the current read attempt that returned WAL_RETRY.
61979** This routine will start taking more aggressive measures to clear the
61980** race conditions after multiple WAL_RETRY returns, and after an excessive
61981** number of errors will ultimately return SQLITE_PROTOCOL. The
61982** SQLITE_PROTOCOL return indicates that some other process has gone rogue
61983** and is not honoring the locking protocol. There is a vanishingly small
61984** chance that SQLITE_PROTOCOL could be returned because of a run of really
61985** bad luck when there is lots of contention for the wal-index, but that
61986** possibility is so small that it can be safely neglected, we believe.
61987**
61988** On success, this routine obtains a read lock on
61989** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
61990** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
61991** that means the Wal does not hold any read lock. The reader must not
61992** access any database page that is modified by a WAL frame up to and
61993** including frame number aReadMark[pWal->readLock]. The reader will
61994** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
61995** Or if pWal->readLock==0, then the reader will ignore the WAL
61996** completely and get all content directly from the database file.
61997** If the useWal parameter is 1 then the WAL will never be ignored and
61998** this routine will always set pWal->readLock>0 on success.
61999** When the read transaction is completed, the caller must release the
62000** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
62001**
62002** This routine uses the nBackfill and aReadMark[] fields of the header
62003** to select a particular WAL_READ_LOCK() that strives to let the
62004** checkpoint process do as much work as possible. This routine might
62005** update values of the aReadMark[] array in the header, but if it does
62006** so it takes care to hold an exclusive lock on the corresponding
62007** WAL_READ_LOCK() while changing values.
62008*/
62009static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
62010 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
62011 u32 mxReadMark; /* Largest aReadMark[] value */
62012 int mxI; /* Index of largest aReadMark[] value */
62013 int i; /* Loop counter */
62014 int rc = SQLITE_OK; /* Return code */
62015 u32 mxFrame; /* Wal frame to lock to */
62016
62017 assert( pWal->readLock<0 ); /* Not currently locked */
62018
62019 /* useWal may only be set for read/write connections */
62020 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
62021
62022 /* Take steps to avoid spinning forever if there is a protocol error.
62023 **
62024 ** Circumstances that cause a RETRY should only last for the briefest
62025 ** instances of time. No I/O or other system calls are done while the
62026 ** locks are held, so the locks should not be held for very long. But
62027 ** if we are unlucky, another process that is holding a lock might get
62028 ** paged out or take a page-fault that is time-consuming to resolve,
62029 ** during the few nanoseconds that it is holding the lock. In that case,
62030 ** it might take longer than normal for the lock to free.
62031 **
62032 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
62033 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
62034 ** is more of a scheduler yield than an actual delay. But on the 10th
62035 ** an subsequent retries, the delays start becoming longer and longer,
62036 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
62037 ** The total delay time before giving up is less than 10 seconds.
62038 */
62039 if( cnt>5 ){
62040 int nDelay = 1; /* Pause time in microseconds */
62041 if( cnt>100 ){
62042 VVA_ONLY( pWal->lockError = 1; )
62043 return SQLITE_PROTOCOL;
62044 }
62045 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
62046 sqlite3OsSleep(pWal->pVfs, nDelay);
62047 }
62048
62049 if( !useWal ){
62050 assert( rc==SQLITE_OK );
62051 if( pWal->bShmUnreliable==0 ){
62052 rc = walIndexReadHdr(pWal, pChanged);
62053 }
62054 if( rc==SQLITE_BUSY ){
62055 /* If there is not a recovery running in another thread or process
62056 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
62057 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
62058 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
62059 ** would be technically correct. But the race is benign since with
62060 ** WAL_RETRY this routine will be called again and will probably be
62061 ** right on the second iteration.
62062 */
62063 if( pWal->apWiData[0]==0 ){
62064 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
62065 ** We assume this is a transient condition, so return WAL_RETRY. The
62066 ** xShmMap() implementation used by the default unix and win32 VFS
62067 ** modules may return SQLITE_BUSY due to a race condition in the
62068 ** code that determines whether or not the shared-memory region
62069 ** must be zeroed before the requested page is returned.
62070 */
62071 rc = WAL_RETRY;
62072 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
62073 walUnlockShared(pWal, WAL_RECOVER_LOCK);
62074 rc = WAL_RETRY;
62075 }else if( rc==SQLITE_BUSY ){
62076 rc = SQLITE_BUSY_RECOVERY;
62077 }
62078 }
62079 if( rc!=SQLITE_OK ){
62080 return rc;
62081 }
62082 else if( pWal->bShmUnreliable ){
62083 return walBeginShmUnreliable(pWal, pChanged);
62084 }
62085 }
62086
62087 assert( pWal->nWiData>0 );
62088 assert( pWal->apWiData[0]!=0 );
62089 pInfo = walCkptInfo(pWal);
62090 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
62091#ifdef SQLITE_ENABLE_SNAPSHOT
62092 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
62093#endif
62094 ){
62095 /* The WAL has been completely backfilled (or it is empty).
62096 ** and can be safely ignored.
62097 */
62098 rc = walLockShared(pWal, WAL_READ_LOCK(0));
62099 walShmBarrier(pWal);
62100 if( rc==SQLITE_OK ){
62101 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
62102 /* It is not safe to allow the reader to continue here if frames
62103 ** may have been appended to the log before READ_LOCK(0) was obtained.
62104 ** When holding READ_LOCK(0), the reader ignores the entire log file,
62105 ** which implies that the database file contains a trustworthy
62106 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
62107 ** happening, this is usually correct.
62108 **
62109 ** However, if frames have been appended to the log (or if the log
62110 ** is wrapped and written for that matter) before the READ_LOCK(0)
62111 ** is obtained, that is not necessarily true. A checkpointer may
62112 ** have started to backfill the appended frames but crashed before
62113 ** it finished. Leaving a corrupt image in the database file.
62114 */
62115 walUnlockShared(pWal, WAL_READ_LOCK(0));
62116 return WAL_RETRY;
62117 }
62118 pWal->readLock = 0;
62119 return SQLITE_OK;
62120 }else if( rc!=SQLITE_BUSY ){
62121 return rc;
62122 }
62123 }
62124
62125 /* If we get this far, it means that the reader will want to use
62126 ** the WAL to get at content from recent commits. The job now is
62127 ** to select one of the aReadMark[] entries that is closest to
62128 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
62129 */
62130 mxReadMark = 0;
62131 mxI = 0;
62132 mxFrame = pWal->hdr.mxFrame;
62133#ifdef SQLITE_ENABLE_SNAPSHOT
62134 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
62135 mxFrame = pWal->pSnapshot->mxFrame;
62136 }
62137#endif
62138 for(i=1; i<WAL_NREADER; i++){
62139 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
62140 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
62141 assert( thisMark!=READMARK_NOT_USED );
62142 mxReadMark = thisMark;
62143 mxI = i;
62144 }
62145 }
62146 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
62147 && (mxReadMark<mxFrame || mxI==0)
62148 ){
62149 for(i=1; i<WAL_NREADER; i++){
62150 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
62151 if( rc==SQLITE_OK ){
62152 AtomicStore(pInfo->aReadMark+i,mxFrame);
62153 mxReadMark = mxFrame;
62154 mxI = i;
62155 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
62156 break;
62157 }else if( rc!=SQLITE_BUSY ){
62158 return rc;
62159 }
62160 }
62161 }
62162 if( mxI==0 ){
62163 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
62164 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
62165 }
62166
62167 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
62168 if( rc ){
62169 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
62170 }
62171 /* Now that the read-lock has been obtained, check that neither the
62172 ** value in the aReadMark[] array or the contents of the wal-index
62173 ** header have changed.
62174 **
62175 ** It is necessary to check that the wal-index header did not change
62176 ** between the time it was read and when the shared-lock was obtained
62177 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
62178 ** that the log file may have been wrapped by a writer, or that frames
62179 ** that occur later in the log than pWal->hdr.mxFrame may have been
62180 ** copied into the database by a checkpointer. If either of these things
62181 ** happened, then reading the database with the current value of
62182 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
62183 ** instead.
62184 **
62185 ** Before checking that the live wal-index header has not changed
62186 ** since it was read, set Wal.minFrame to the first frame in the wal
62187 ** file that has not yet been checkpointed. This client will not need
62188 ** to read any frames earlier than minFrame from the wal file - they
62189 ** can be safely read directly from the database file.
62190 **
62191 ** Because a ShmBarrier() call is made between taking the copy of
62192 ** nBackfill and checking that the wal-header in shared-memory still
62193 ** matches the one cached in pWal->hdr, it is guaranteed that the
62194 ** checkpointer that set nBackfill was not working with a wal-index
62195 ** header newer than that cached in pWal->hdr. If it were, that could
62196 ** cause a problem. The checkpointer could omit to checkpoint
62197 ** a version of page X that lies before pWal->minFrame (call that version
62198 ** A) on the basis that there is a newer version (version B) of the same
62199 ** page later in the wal file. But if version B happens to like past
62200 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
62201 ** that it can read version A from the database file. However, since
62202 ** we can guarantee that the checkpointer that set nBackfill could not
62203 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
62204 */
62205 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
62206 walShmBarrier(pWal);
62207 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
62208 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
62209 ){
62210 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
62211 return WAL_RETRY;
62212 }else{
62213 assert( mxReadMark<=pWal->hdr.mxFrame );
62214 pWal->readLock = (i16)mxI;
62215 }
62216 return rc;
62217}
62218
62219#ifdef SQLITE_ENABLE_SNAPSHOT
62220/*
62221** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
62222** variable so that older snapshots can be accessed. To do this, loop
62223** through all wal frames from nBackfillAttempted to (nBackfill+1),
62224** comparing their content to the corresponding page with the database
62225** file, if any. Set nBackfillAttempted to the frame number of the
62226** first frame for which the wal file content matches the db file.
62227**
62228** This is only really safe if the file-system is such that any page
62229** writes made by earlier checkpointers were atomic operations, which
62230** is not always true. It is also possible that nBackfillAttempted
62231** may be left set to a value larger than expected, if a wal frame
62232** contains content that duplicate of an earlier version of the same
62233** page.
62234**
62235** SQLITE_OK is returned if successful, or an SQLite error code if an
62236** error occurs. It is not an error if nBackfillAttempted cannot be
62237** decreased at all.
62238*/
62239SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
62240 int rc;
62241
62242 assert( pWal->readLock>=0 );
62243 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
62244 if( rc==SQLITE_OK ){
62245 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62246 int szPage = (int)pWal->szPage;
62247 i64 szDb; /* Size of db file in bytes */
62248
62249 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
62250 if( rc==SQLITE_OK ){
62251 void *pBuf1 = sqlite3_malloc(szPage);
62252 void *pBuf2 = sqlite3_malloc(szPage);
62253 if( pBuf1==0 || pBuf2==0 ){
62254 rc = SQLITE_NOMEM;
62255 }else{
62256 u32 i = pInfo->nBackfillAttempted;
62257 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
62258 WalHashLoc sLoc; /* Hash table location */
62259 u32 pgno; /* Page number in db file */
62260 i64 iDbOff; /* Offset of db file entry */
62261 i64 iWalOff; /* Offset of wal file entry */
62262
62263 rc = walHashGet(pWal, walFramePage(i), &sLoc);
62264 if( rc!=SQLITE_OK ) break;
62265 pgno = sLoc.aPgno[i-sLoc.iZero];
62266 iDbOff = (i64)(pgno-1) * szPage;
62267
62268 if( iDbOff+szPage<=szDb ){
62269 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
62270 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
62271
62272 if( rc==SQLITE_OK ){
62273 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
62274 }
62275
62276 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
62277 break;
62278 }
62279 }
62280
62281 pInfo->nBackfillAttempted = i-1;
62282 }
62283 }
62284
62285 sqlite3_free(pBuf1);
62286 sqlite3_free(pBuf2);
62287 }
62288 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
62289 }
62290
62291 return rc;
62292}
62293#endif /* SQLITE_ENABLE_SNAPSHOT */
62294
62295/*
62296** Begin a read transaction on the database.
62297**
62298** This routine used to be called sqlite3OpenSnapshot() and with good reason:
62299** it takes a snapshot of the state of the WAL and wal-index for the current
62300** instant in time. The current thread will continue to use this snapshot.
62301** Other threads might append new content to the WAL and wal-index but
62302** that extra content is ignored by the current thread.
62303**
62304** If the database contents have changes since the previous read
62305** transaction, then *pChanged is set to 1 before returning. The
62306** Pager layer will use this to know that its cache is stale and
62307** needs to be flushed.
62308*/
62309SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
62310 int rc; /* Return code */
62311 int cnt = 0; /* Number of TryBeginRead attempts */
62312#ifdef SQLITE_ENABLE_SNAPSHOT
62313 int bChanged = 0;
62314 WalIndexHdr *pSnapshot = pWal->pSnapshot;
62315#endif
62316
62317 assert( pWal->ckptLock==0 );
62318
62319#ifdef SQLITE_ENABLE_SNAPSHOT
62320 if( pSnapshot ){
62321 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62322 bChanged = 1;
62323 }
62324
62325 /* It is possible that there is a checkpointer thread running
62326 ** concurrent with this code. If this is the case, it may be that the
62327 ** checkpointer has already determined that it will checkpoint
62328 ** snapshot X, where X is later in the wal file than pSnapshot, but
62329 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
62330 ** its intent. To avoid the race condition this leads to, ensure that
62331 ** there is no checkpointer process by taking a shared CKPT lock
62332 ** before checking pInfo->nBackfillAttempted. */
62333 (void)walEnableBlocking(pWal);
62334 rc = walLockShared(pWal, WAL_CKPT_LOCK);
62335 walDisableBlocking(pWal);
62336
62337 if( rc!=SQLITE_OK ){
62338 return rc;
62339 }
62340 pWal->ckptLock = 1;
62341 }
62342#endif
62343
62344 do{
62345 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
62346 }while( rc==WAL_RETRY );
62347 testcase( (rc&0xff)==SQLITE_BUSY );
62348 testcase( (rc&0xff)==SQLITE_IOERR );
62349 testcase( rc==SQLITE_PROTOCOL );
62350 testcase( rc==SQLITE_OK );
62351
62352#ifdef SQLITE_ENABLE_SNAPSHOT
62353 if( rc==SQLITE_OK ){
62354 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62355 /* At this point the client has a lock on an aReadMark[] slot holding
62356 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
62357 ** is populated with the wal-index header corresponding to the head
62358 ** of the wal file. Verify that pSnapshot is still valid before
62359 ** continuing. Reasons why pSnapshot might no longer be valid:
62360 **
62361 ** (1) The WAL file has been reset since the snapshot was taken.
62362 ** In this case, the salt will have changed.
62363 **
62364 ** (2) A checkpoint as been attempted that wrote frames past
62365 ** pSnapshot->mxFrame into the database file. Note that the
62366 ** checkpoint need not have completed for this to cause problems.
62367 */
62368 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62369
62370 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
62371 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
62372
62373 /* Check that the wal file has not been wrapped. Assuming that it has
62374 ** not, also check that no checkpointer has attempted to checkpoint any
62375 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
62376 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
62377 ** with *pSnapshot and set *pChanged as appropriate for opening the
62378 ** snapshot. */
62379 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
62380 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
62381 ){
62382 assert( pWal->readLock>0 );
62383 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
62384 *pChanged = bChanged;
62385 }else{
62386 rc = SQLITE_ERROR_SNAPSHOT;
62387 }
62388
62389 /* A client using a non-current snapshot may not ignore any frames
62390 ** from the start of the wal file. This is because, for a system
62391 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
62392 ** have omitted to checkpoint a frame earlier than minFrame in
62393 ** the file because there exists a frame after iSnapshot that
62394 ** is the same database page. */
62395 pWal->minFrame = 1;
62396
62397 if( rc!=SQLITE_OK ){
62398 sqlite3WalEndReadTransaction(pWal);
62399 }
62400 }
62401 }
62402
62403 /* Release the shared CKPT lock obtained above. */
62404 if( pWal->ckptLock ){
62405 assert( pSnapshot );
62406 walUnlockShared(pWal, WAL_CKPT_LOCK);
62407 pWal->ckptLock = 0;
62408 }
62409#endif
62410 return rc;
62411}
62412
62413/*
62414** Finish with a read transaction. All this does is release the
62415** read-lock.
62416*/
62417SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
62418 sqlite3WalEndWriteTransaction(pWal);
62419 if( pWal->readLock>=0 ){
62420 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
62421 pWal->readLock = -1;
62422 }
62423}
62424
62425/*
62426** Search the wal file for page pgno. If found, set *piRead to the frame that
62427** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
62428** to zero.
62429**
62430** Return SQLITE_OK if successful, or an error code if an error occurs. If an
62431** error does occur, the final value of *piRead is undefined.
62432*/
62433SQLITE_PRIVATE int sqlite3WalFindFrame(
62434 Wal *pWal, /* WAL handle */
62435 Pgno pgno, /* Database page number to read data for */
62436 u32 *piRead /* OUT: Frame number (or zero) */
62437){
62438 u32 iRead = 0; /* If !=0, WAL frame to return data from */
62439 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
62440 int iHash; /* Used to loop through N hash tables */
62441 int iMinHash;
62442
62443 /* This routine is only be called from within a read transaction. */
62444 assert( pWal->readLock>=0 || pWal->lockError );
62445
62446 /* If the "last page" field of the wal-index header snapshot is 0, then
62447 ** no data will be read from the wal under any circumstances. Return early
62448 ** in this case as an optimization. Likewise, if pWal->readLock==0,
62449 ** then the WAL is ignored by the reader so return early, as if the
62450 ** WAL were empty.
62451 */
62452 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
62453 *piRead = 0;
62454 return SQLITE_OK;
62455 }
62456
62457 /* Search the hash table or tables for an entry matching page number
62458 ** pgno. Each iteration of the following for() loop searches one
62459 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
62460 **
62461 ** This code might run concurrently to the code in walIndexAppend()
62462 ** that adds entries to the wal-index (and possibly to this hash
62463 ** table). This means the value just read from the hash
62464 ** slot (aHash[iKey]) may have been added before or after the
62465 ** current read transaction was opened. Values added after the
62466 ** read transaction was opened may have been written incorrectly -
62467 ** i.e. these slots may contain garbage data. However, we assume
62468 ** that any slots written before the current read transaction was
62469 ** opened remain unmodified.
62470 **
62471 ** For the reasons above, the if(...) condition featured in the inner
62472 ** loop of the following block is more stringent that would be required
62473 ** if we had exclusive access to the hash-table:
62474 **
62475 ** (aPgno[iFrame]==pgno):
62476 ** This condition filters out normal hash-table collisions.
62477 **
62478 ** (iFrame<=iLast):
62479 ** This condition filters out entries that were added to the hash
62480 ** table after the current read-transaction had started.
62481 */
62482 iMinHash = walFramePage(pWal->minFrame);
62483 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
62484 WalHashLoc sLoc; /* Hash table location */
62485 int iKey; /* Hash slot index */
62486 int nCollide; /* Number of hash collisions remaining */
62487 int rc; /* Error code */
62488 u32 iH;
62489
62490 rc = walHashGet(pWal, iHash, &sLoc);
62491 if( rc!=SQLITE_OK ){
62492 return rc;
62493 }
62494 nCollide = HASHTABLE_NSLOT;
62495 iKey = walHash(pgno);
62496 while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
62497 u32 iFrame = iH + sLoc.iZero;
62498 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
62499 assert( iFrame>iRead || CORRUPT_DB );
62500 iRead = iFrame;
62501 }
62502 if( (nCollide--)==0 ){
62503 return SQLITE_CORRUPT_BKPT;
62504 }
62505 iKey = walNextHash(iKey);
62506 }
62507 if( iRead ) break;
62508 }
62509
62510#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
62511 /* If expensive assert() statements are available, do a linear search
62512 ** of the wal-index file content. Make sure the results agree with the
62513 ** result obtained using the hash indexes above. */
62514 {
62515 u32 iRead2 = 0;
62516 u32 iTest;
62517 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
62518 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
62519 if( walFramePgno(pWal, iTest)==pgno ){
62520 iRead2 = iTest;
62521 break;
62522 }
62523 }
62524 assert( iRead==iRead2 );
62525 }
62526#endif
62527
62528 *piRead = iRead;
62529 return SQLITE_OK;
62530}
62531
62532/*
62533** Read the contents of frame iRead from the wal file into buffer pOut
62534** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
62535** error code otherwise.
62536*/
62537SQLITE_PRIVATE int sqlite3WalReadFrame(
62538 Wal *pWal, /* WAL handle */
62539 u32 iRead, /* Frame to read */
62540 int nOut, /* Size of buffer pOut in bytes */
62541 u8 *pOut /* Buffer to write page data to */
62542){
62543 int sz;
62544 i64 iOffset;
62545 sz = pWal->hdr.szPage;
62546 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
62547 testcase( sz<=32768 );
62548 testcase( sz>=65536 );
62549 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
62550 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
62551 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
62552}
62553
62554/*
62555** Return the size of the database in pages (or zero, if unknown).
62556*/
62557SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
62558 if( pWal && ALWAYS(pWal->readLock>=0) ){
62559 return pWal->hdr.nPage;
62560 }
62561 return 0;
62562}
62563
62564
62565/*
62566** This function starts a write transaction on the WAL.
62567**
62568** A read transaction must have already been started by a prior call
62569** to sqlite3WalBeginReadTransaction().
62570**
62571** If another thread or process has written into the database since
62572** the read transaction was started, then it is not possible for this
62573** thread to write as doing so would cause a fork. So this routine
62574** returns SQLITE_BUSY in that case and no write transaction is started.
62575**
62576** There can only be a single writer active at a time.
62577*/
62578SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
62579 int rc;
62580
62581#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
62582 /* If the write-lock is already held, then it was obtained before the
62583 ** read-transaction was even opened, making this call a no-op.
62584 ** Return early. */
62585 if( pWal->writeLock ){
62586 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
62587 return SQLITE_OK;
62588 }
62589#endif
62590
62591 /* Cannot start a write transaction without first holding a read
62592 ** transaction. */
62593 assert( pWal->readLock>=0 );
62594 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
62595
62596 if( pWal->readOnly ){
62597 return SQLITE_READONLY;
62598 }
62599
62600 /* Only one writer allowed at a time. Get the write lock. Return
62601 ** SQLITE_BUSY if unable.
62602 */
62603 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
62604 if( rc ){
62605 return rc;
62606 }
62607 pWal->writeLock = 1;
62608
62609 /* If another connection has written to the database file since the
62610 ** time the read transaction on this connection was started, then
62611 ** the write is disallowed.
62612 */
62613 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
62614 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
62615 pWal->writeLock = 0;
62616 rc = SQLITE_BUSY_SNAPSHOT;
62617 }
62618
62619 return rc;
62620}
62621
62622/*
62623** End a write transaction. The commit has already been done. This
62624** routine merely releases the lock.
62625*/
62626SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
62627 if( pWal->writeLock ){
62628 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
62629 pWal->writeLock = 0;
62630 pWal->iReCksum = 0;
62631 pWal->truncateOnCommit = 0;
62632 }
62633 return SQLITE_OK;
62634}
62635
62636/*
62637** If any data has been written (but not committed) to the log file, this
62638** function moves the write-pointer back to the start of the transaction.
62639**
62640** Additionally, the callback function is invoked for each frame written
62641** to the WAL since the start of the transaction. If the callback returns
62642** other than SQLITE_OK, it is not invoked again and the error code is
62643** returned to the caller.
62644**
62645** Otherwise, if the callback function does not return an error, this
62646** function returns SQLITE_OK.
62647*/
62648SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
62649 int rc = SQLITE_OK;
62650 if( ALWAYS(pWal->writeLock) ){
62651 Pgno iMax = pWal->hdr.mxFrame;
62652 Pgno iFrame;
62653
62654 /* Restore the clients cache of the wal-index header to the state it
62655 ** was in before the client began writing to the database.
62656 */
62657 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
62658
62659 for(iFrame=pWal->hdr.mxFrame+1;
62660 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
62661 iFrame++
62662 ){
62663 /* This call cannot fail. Unless the page for which the page number
62664 ** is passed as the second argument is (a) in the cache and
62665 ** (b) has an outstanding reference, then xUndo is either a no-op
62666 ** (if (a) is false) or simply expels the page from the cache (if (b)
62667 ** is false).
62668 **
62669 ** If the upper layer is doing a rollback, it is guaranteed that there
62670 ** are no outstanding references to any page other than page 1. And
62671 ** page 1 is never written to the log until the transaction is
62672 ** committed. As a result, the call to xUndo may not fail.
62673 */
62674 assert( walFramePgno(pWal, iFrame)!=1 );
62675 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
62676 }
62677 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
62678 }
62679 return rc;
62680}
62681
62682/*
62683** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
62684** values. This function populates the array with values required to
62685** "rollback" the write position of the WAL handle back to the current
62686** point in the event of a savepoint rollback (via WalSavepointUndo()).
62687*/
62688SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
62689 assert( pWal->writeLock );
62690 aWalData[0] = pWal->hdr.mxFrame;
62691 aWalData[1] = pWal->hdr.aFrameCksum[0];
62692 aWalData[2] = pWal->hdr.aFrameCksum[1];
62693 aWalData[3] = pWal->nCkpt;
62694}
62695
62696/*
62697** Move the write position of the WAL back to the point identified by
62698** the values in the aWalData[] array. aWalData must point to an array
62699** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
62700** by a call to WalSavepoint().
62701*/
62702SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
62703 int rc = SQLITE_OK;
62704
62705 assert( pWal->writeLock );
62706 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
62707
62708 if( aWalData[3]!=pWal->nCkpt ){
62709 /* This savepoint was opened immediately after the write-transaction
62710 ** was started. Right after that, the writer decided to wrap around
62711 ** to the start of the log. Update the savepoint values to match.
62712 */
62713 aWalData[0] = 0;
62714 aWalData[3] = pWal->nCkpt;
62715 }
62716
62717 if( aWalData[0]<pWal->hdr.mxFrame ){
62718 pWal->hdr.mxFrame = aWalData[0];
62719 pWal->hdr.aFrameCksum[0] = aWalData[1];
62720 pWal->hdr.aFrameCksum[1] = aWalData[2];
62721 walCleanupHash(pWal);
62722 }
62723
62724 return rc;
62725}
62726
62727/*
62728** This function is called just before writing a set of frames to the log
62729** file (see sqlite3WalFrames()). It checks to see if, instead of appending
62730** to the current log file, it is possible to overwrite the start of the
62731** existing log file with the new frames (i.e. "reset" the log). If so,
62732** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
62733** unchanged.
62734**
62735** SQLITE_OK is returned if no error is encountered (regardless of whether
62736** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
62737** if an error occurs.
62738*/
62739static int walRestartLog(Wal *pWal){
62740 int rc = SQLITE_OK;
62741 int cnt;
62742
62743 if( pWal->readLock==0 ){
62744 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
62745 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
62746 if( pInfo->nBackfill>0 ){
62747 u32 salt1;
62748 sqlite3_randomness(4, &salt1);
62749 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
62750 if( rc==SQLITE_OK ){
62751 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
62752 ** readers are currently using the WAL), then the transactions
62753 ** frames will overwrite the start of the existing log. Update the
62754 ** wal-index header to reflect this.
62755 **
62756 ** In theory it would be Ok to update the cache of the header only
62757 ** at this point. But updating the actual wal-index header is also
62758 ** safe and means there is no special case for sqlite3WalUndo()
62759 ** to handle if this transaction is rolled back. */
62760 walRestartHdr(pWal, salt1);
62761 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
62762 }else if( rc!=SQLITE_BUSY ){
62763 return rc;
62764 }
62765 }
62766 walUnlockShared(pWal, WAL_READ_LOCK(0));
62767 pWal->readLock = -1;
62768 cnt = 0;
62769 do{
62770 int notUsed;
62771 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
62772 }while( rc==WAL_RETRY );
62773 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
62774 testcase( (rc&0xff)==SQLITE_IOERR );
62775 testcase( rc==SQLITE_PROTOCOL );
62776 testcase( rc==SQLITE_OK );
62777 }
62778 return rc;
62779}
62780
62781/*
62782** Information about the current state of the WAL file and where
62783** the next fsync should occur - passed from sqlite3WalFrames() into
62784** walWriteToLog().
62785*/
62786typedef struct WalWriter {
62787 Wal *pWal; /* The complete WAL information */
62788 sqlite3_file *pFd; /* The WAL file to which we write */
62789 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
62790 int syncFlags; /* Flags for the fsync */
62791 int szPage; /* Size of one page */
62792} WalWriter;
62793
62794/*
62795** Write iAmt bytes of content into the WAL file beginning at iOffset.
62796** Do a sync when crossing the p->iSyncPoint boundary.
62797**
62798** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
62799** first write the part before iSyncPoint, then sync, then write the
62800** rest.
62801*/
62802static int walWriteToLog(
62803 WalWriter *p, /* WAL to write to */
62804 void *pContent, /* Content to be written */
62805 int iAmt, /* Number of bytes to write */
62806 sqlite3_int64 iOffset /* Start writing at this offset */
62807){
62808 int rc;
62809 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
62810 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
62811 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
62812 if( rc ) return rc;
62813 iOffset += iFirstAmt;
62814 iAmt -= iFirstAmt;
62815 pContent = (void*)(iFirstAmt + (char*)pContent);
62816 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
62817 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
62818 if( iAmt==0 || rc ) return rc;
62819 }
62820 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
62821 return rc;
62822}
62823
62824/*
62825** Write out a single frame of the WAL
62826*/
62827static int walWriteOneFrame(
62828 WalWriter *p, /* Where to write the frame */
62829 PgHdr *pPage, /* The page of the frame to be written */
62830 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
62831 sqlite3_int64 iOffset /* Byte offset at which to write */
62832){
62833 int rc; /* Result code from subfunctions */
62834 void *pData; /* Data actually written */
62835 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
62836 pData = pPage->pData;
62837 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
62838 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
62839 if( rc ) return rc;
62840 /* Write the page data */
62841 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
62842 return rc;
62843}
62844
62845/*
62846** This function is called as part of committing a transaction within which
62847** one or more frames have been overwritten. It updates the checksums for
62848** all frames written to the wal file by the current transaction starting
62849** with the earliest to have been overwritten.
62850**
62851** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
62852*/
62853static int walRewriteChecksums(Wal *pWal, u32 iLast){
62854 const int szPage = pWal->szPage;/* Database page size */
62855 int rc = SQLITE_OK; /* Return code */
62856 u8 *aBuf; /* Buffer to load data from wal file into */
62857 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
62858 u32 iRead; /* Next frame to read from wal file */
62859 i64 iCksumOff;
62860
62861 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
62862 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
62863
62864 /* Find the checksum values to use as input for the recalculating the
62865 ** first checksum. If the first frame is frame 1 (implying that the current
62866 ** transaction restarted the wal file), these values must be read from the
62867 ** wal-file header. Otherwise, read them from the frame header of the
62868 ** previous frame. */
62869 assert( pWal->iReCksum>0 );
62870 if( pWal->iReCksum==1 ){
62871 iCksumOff = 24;
62872 }else{
62873 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
62874 }
62875 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
62876 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
62877 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
62878
62879 iRead = pWal->iReCksum;
62880 pWal->iReCksum = 0;
62881 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
62882 i64 iOff = walFrameOffset(iRead, szPage);
62883 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
62884 if( rc==SQLITE_OK ){
62885 u32 iPgno, nDbSize;
62886 iPgno = sqlite3Get4byte(aBuf);
62887 nDbSize = sqlite3Get4byte(&aBuf[4]);
62888
62889 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
62890 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
62891 }
62892 }
62893
62894 sqlite3_free(aBuf);
62895 return rc;
62896}
62897
62898/*
62899** Write a set of frames to the log. The caller must hold the write-lock
62900** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
62901*/
62902SQLITE_PRIVATE int sqlite3WalFrames(
62903 Wal *pWal, /* Wal handle to write to */
62904 int szPage, /* Database page-size in bytes */
62905 PgHdr *pList, /* List of dirty pages to write */
62906 Pgno nTruncate, /* Database size after this commit */
62907 int isCommit, /* True if this is a commit */
62908 int sync_flags /* Flags to pass to OsSync() (or 0) */
62909){
62910 int rc; /* Used to catch return codes */
62911 u32 iFrame; /* Next frame address */
62912 PgHdr *p; /* Iterator to run through pList with. */
62913 PgHdr *pLast = 0; /* Last frame in list */
62914 int nExtra = 0; /* Number of extra copies of last page */
62915 int szFrame; /* The size of a single frame */
62916 i64 iOffset; /* Next byte to write in WAL file */
62917 WalWriter w; /* The writer */
62918 u32 iFirst = 0; /* First frame that may be overwritten */
62919 WalIndexHdr *pLive; /* Pointer to shared header */
62920
62921 assert( pList );
62922 assert( pWal->writeLock );
62923
62924 /* If this frame set completes a transaction, then nTruncate>0. If
62925 ** nTruncate==0 then this frame set does not complete the transaction. */
62926 assert( (isCommit!=0)==(nTruncate!=0) );
62927
62928#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
62929 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
62930 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
62931 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
62932 }
62933#endif
62934
62935 pLive = (WalIndexHdr*)walIndexHdr(pWal);
62936 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
62937 iFirst = pLive->mxFrame+1;
62938 }
62939
62940 /* See if it is possible to write these frames into the start of the
62941 ** log file, instead of appending to it at pWal->hdr.mxFrame.
62942 */
62943 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
62944 return rc;
62945 }
62946
62947 /* If this is the first frame written into the log, write the WAL
62948 ** header to the start of the WAL file. See comments at the top of
62949 ** this source file for a description of the WAL header format.
62950 */
62951 iFrame = pWal->hdr.mxFrame;
62952 if( iFrame==0 ){
62953 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
62954 u32 aCksum[2]; /* Checksum for wal-header */
62955
62956 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
62957 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
62958 sqlite3Put4byte(&aWalHdr[8], szPage);
62959 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
62960 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
62961 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
62962 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
62963 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
62964 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
62965
62966 pWal->szPage = szPage;
62967 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
62968 pWal->hdr.aFrameCksum[0] = aCksum[0];
62969 pWal->hdr.aFrameCksum[1] = aCksum[1];
62970 pWal->truncateOnCommit = 1;
62971
62972 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
62973 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
62974 if( rc!=SQLITE_OK ){
62975 return rc;
62976 }
62977
62978 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
62979 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
62980 ** an out-of-order write following a WAL restart could result in
62981 ** database corruption. See the ticket:
62982 **
62983 ** https://sqlite.org/src/info/ff5be73dee
62984 */
62985 if( pWal->syncHeader ){
62986 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
62987 if( rc ) return rc;
62988 }
62989 }
62990 assert( (int)pWal->szPage==szPage );
62991
62992 /* Setup information needed to write frames into the WAL */
62993 w.pWal = pWal;
62994 w.pFd = pWal->pWalFd;
62995 w.iSyncPoint = 0;
62996 w.syncFlags = sync_flags;
62997 w.szPage = szPage;
62998 iOffset = walFrameOffset(iFrame+1, szPage);
62999 szFrame = szPage + WAL_FRAME_HDRSIZE;
63000
63001 /* Write all frames into the log file exactly once */
63002 for(p=pList; p; p=p->pDirty){
63003 int nDbSize; /* 0 normally. Positive == commit flag */
63004
63005 /* Check if this page has already been written into the wal file by
63006 ** the current transaction. If so, overwrite the existing frame and
63007 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
63008 ** checksums must be recomputed when the transaction is committed. */
63009 if( iFirst && (p->pDirty || isCommit==0) ){
63010 u32 iWrite = 0;
63011 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
63012 assert( rc==SQLITE_OK || iWrite==0 );
63013 if( iWrite>=iFirst ){
63014 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
63015 void *pData;
63016 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
63017 pWal->iReCksum = iWrite;
63018 }
63019 pData = p->pData;
63020 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
63021 if( rc ) return rc;
63022 p->flags &= ~PGHDR_WAL_APPEND;
63023 continue;
63024 }
63025 }
63026
63027 iFrame++;
63028 assert( iOffset==walFrameOffset(iFrame, szPage) );
63029 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
63030 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
63031 if( rc ) return rc;
63032 pLast = p;
63033 iOffset += szFrame;
63034 p->flags |= PGHDR_WAL_APPEND;
63035 }
63036
63037 /* Recalculate checksums within the wal file if required. */
63038 if( isCommit && pWal->iReCksum ){
63039 rc = walRewriteChecksums(pWal, iFrame);
63040 if( rc ) return rc;
63041 }
63042
63043 /* If this is the end of a transaction, then we might need to pad
63044 ** the transaction and/or sync the WAL file.
63045 **
63046 ** Padding and syncing only occur if this set of frames complete a
63047 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
63048 ** or synchronous==OFF, then no padding or syncing are needed.
63049 **
63050 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
63051 ** needed and only the sync is done. If padding is needed, then the
63052 ** final frame is repeated (with its commit mark) until the next sector
63053 ** boundary is crossed. Only the part of the WAL prior to the last
63054 ** sector boundary is synced; the part of the last frame that extends
63055 ** past the sector boundary is written after the sync.
63056 */
63057 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
63058 int bSync = 1;
63059 if( pWal->padToSectorBoundary ){
63060 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
63061 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
63062 bSync = (w.iSyncPoint==iOffset);
63063 testcase( bSync );
63064 while( iOffset<w.iSyncPoint ){
63065 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
63066 if( rc ) return rc;
63067 iOffset += szFrame;
63068 nExtra++;
63069 assert( pLast!=0 );
63070 }
63071 }
63072 if( bSync ){
63073 assert( rc==SQLITE_OK );
63074 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
63075 }
63076 }
63077
63078 /* If this frame set completes the first transaction in the WAL and
63079 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
63080 ** journal size limit, if possible.
63081 */
63082 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
63083 i64 sz = pWal->mxWalSize;
63084 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
63085 sz = walFrameOffset(iFrame+nExtra+1, szPage);
63086 }
63087 walLimitSize(pWal, sz);
63088 pWal->truncateOnCommit = 0;
63089 }
63090
63091 /* Append data to the wal-index. It is not necessary to lock the
63092 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
63093 ** guarantees that there are no other writers, and no data that may
63094 ** be in use by existing readers is being overwritten.
63095 */
63096 iFrame = pWal->hdr.mxFrame;
63097 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
63098 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
63099 iFrame++;
63100 rc = walIndexAppend(pWal, iFrame, p->pgno);
63101 }
63102 assert( pLast!=0 || nExtra==0 );
63103 while( rc==SQLITE_OK && nExtra>0 ){
63104 iFrame++;
63105 nExtra--;
63106 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
63107 }
63108
63109 if( rc==SQLITE_OK ){
63110 /* Update the private copy of the header. */
63111 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
63112 testcase( szPage<=32768 );
63113 testcase( szPage>=65536 );
63114 pWal->hdr.mxFrame = iFrame;
63115 if( isCommit ){
63116 pWal->hdr.iChange++;
63117 pWal->hdr.nPage = nTruncate;
63118 }
63119 /* If this is a commit, update the wal-index header too. */
63120 if( isCommit ){
63121 walIndexWriteHdr(pWal);
63122 pWal->iCallback = iFrame;
63123 }
63124 }
63125
63126 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
63127 return rc;
63128}
63129
63130/*
63131** This routine is called to implement sqlite3_wal_checkpoint() and
63132** related interfaces.
63133**
63134** Obtain a CHECKPOINT lock and then backfill as much information as
63135** we can from WAL into the database.
63136**
63137** If parameter xBusy is not NULL, it is a pointer to a busy-handler
63138** callback. In this case this function runs a blocking checkpoint.
63139*/
63140SQLITE_PRIVATE int sqlite3WalCheckpoint(
63141 Wal *pWal, /* Wal connection */
63142 sqlite3 *db, /* Check this handle's interrupt flag */
63143 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
63144 int (*xBusy)(void*), /* Function to call when busy */
63145 void *pBusyArg, /* Context argument for xBusyHandler */
63146 int sync_flags, /* Flags to sync db file with (or 0) */
63147 int nBuf, /* Size of temporary buffer */
63148 u8 *zBuf, /* Temporary buffer to use */
63149 int *pnLog, /* OUT: Number of frames in WAL */
63150 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
63151){
63152 int rc; /* Return code */
63153 int isChanged = 0; /* True if a new wal-index header is loaded */
63154 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
63155 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
63156
63157 assert( pWal->ckptLock==0 );
63158 assert( pWal->writeLock==0 );
63159
63160 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63161 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
63162 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
63163
63164 if( pWal->readOnly ) return SQLITE_READONLY;
63165 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
63166
63167 /* Enable blocking locks, if possible. If blocking locks are successfully
63168 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
63169 sqlite3WalDb(pWal, db);
63170 (void)walEnableBlocking(pWal);
63171
63172 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
63173 ** "checkpoint" lock on the database file.
63174 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
63175 ** checkpoint operation at the same time, the lock cannot be obtained and
63176 ** SQLITE_BUSY is returned.
63177 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
63178 ** it will not be invoked in this case.
63179 */
63180 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
63181 testcase( rc==SQLITE_BUSY );
63182 testcase( rc!=SQLITE_OK && xBusy2!=0 );
63183 if( rc==SQLITE_OK ){
63184 pWal->ckptLock = 1;
63185
63186 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
63187 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
63188 ** file.
63189 **
63190 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
63191 ** immediately, and a busy-handler is configured, it is invoked and the
63192 ** writer lock retried until either the busy-handler returns 0 or the
63193 ** lock is successfully obtained.
63194 */
63195 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
63196 rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1);
63197 if( rc==SQLITE_OK ){
63198 pWal->writeLock = 1;
63199 }else if( rc==SQLITE_BUSY ){
63200 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
63201 xBusy2 = 0;
63202 rc = SQLITE_OK;
63203 }
63204 }
63205 }
63206
63207
63208 /* Read the wal-index header. */
63209 if( rc==SQLITE_OK ){
63210 walDisableBlocking(pWal);
63211 rc = walIndexReadHdr(pWal, &isChanged);
63212 (void)walEnableBlocking(pWal);
63213 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
63214 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
63215 }
63216 }
63217
63218 /* Copy data from the log to the database file. */
63219 if( rc==SQLITE_OK ){
63220
63221 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
63222 rc = SQLITE_CORRUPT_BKPT;
63223 }else{
63224 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
63225 }
63226
63227 /* If no error occurred, set the output variables. */
63228 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
63229 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
63230 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
63231 }
63232 }
63233
63234 if( isChanged ){
63235 /* If a new wal-index header was loaded before the checkpoint was
63236 ** performed, then the pager-cache associated with pWal is now
63237 ** out of date. So zero the cached wal-index header to ensure that
63238 ** next time the pager opens a snapshot on this database it knows that
63239 ** the cache needs to be reset.
63240 */
63241 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
63242 }
63243
63244 walDisableBlocking(pWal);
63245 sqlite3WalDb(pWal, 0);
63246
63247 /* Release the locks. */
63248 sqlite3WalEndWriteTransaction(pWal);
63249 if( pWal->ckptLock ){
63250 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
63251 pWal->ckptLock = 0;
63252 }
63253 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
63254#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
63255 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
63256#endif
63257 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
63258}
63259
63260/* Return the value to pass to a sqlite3_wal_hook callback, the
63261** number of frames in the WAL at the point of the last commit since
63262** sqlite3WalCallback() was called. If no commits have occurred since
63263** the last call, then return 0.
63264*/
63265SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
63266 u32 ret = 0;
63267 if( pWal ){
63268 ret = pWal->iCallback;
63269 pWal->iCallback = 0;
63270 }
63271 return (int)ret;
63272}
63273
63274/*
63275** This function is called to change the WAL subsystem into or out
63276** of locking_mode=EXCLUSIVE.
63277**
63278** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
63279** into locking_mode=NORMAL. This means that we must acquire a lock
63280** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
63281** or if the acquisition of the lock fails, then return 0. If the
63282** transition out of exclusive-mode is successful, return 1. This
63283** operation must occur while the pager is still holding the exclusive
63284** lock on the main database file.
63285**
63286** If op is one, then change from locking_mode=NORMAL into
63287** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
63288** be released. Return 1 if the transition is made and 0 if the
63289** WAL is already in exclusive-locking mode - meaning that this
63290** routine is a no-op. The pager must already hold the exclusive lock
63291** on the main database file before invoking this operation.
63292**
63293** If op is negative, then do a dry-run of the op==1 case but do
63294** not actually change anything. The pager uses this to see if it
63295** should acquire the database exclusive lock prior to invoking
63296** the op==1 case.
63297*/
63298SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
63299 int rc;
63300 assert( pWal->writeLock==0 );
63301 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
63302
63303 /* pWal->readLock is usually set, but might be -1 if there was a
63304 ** prior error while attempting to acquire are read-lock. This cannot
63305 ** happen if the connection is actually in exclusive mode (as no xShmLock
63306 ** locks are taken in this case). Nor should the pager attempt to
63307 ** upgrade to exclusive-mode following such an error.
63308 */
63309 assert( pWal->readLock>=0 || pWal->lockError );
63310 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
63311
63312 if( op==0 ){
63313 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
63314 pWal->exclusiveMode = WAL_NORMAL_MODE;
63315 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
63316 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63317 }
63318 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63319 }else{
63320 /* Already in locking_mode=NORMAL */
63321 rc = 0;
63322 }
63323 }else if( op>0 ){
63324 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
63325 assert( pWal->readLock>=0 );
63326 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
63327 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63328 rc = 1;
63329 }else{
63330 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63331 }
63332 return rc;
63333}
63334
63335/*
63336** Return true if the argument is non-NULL and the WAL module is using
63337** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
63338** WAL module is using shared-memory, return false.
63339*/
63340SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
63341 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
63342}
63343
63344#ifdef SQLITE_ENABLE_SNAPSHOT
63345/* Create a snapshot object. The content of a snapshot is opaque to
63346** every other subsystem, so the WAL module can put whatever it needs
63347** in the object.
63348*/
63349SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
63350 int rc = SQLITE_OK;
63351 WalIndexHdr *pRet;
63352 static const u32 aZero[4] = { 0, 0, 0, 0 };
63353
63354 assert( pWal->readLock>=0 && pWal->writeLock==0 );
63355
63356 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
63357 *ppSnapshot = 0;
63358 return SQLITE_ERROR;
63359 }
63360 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
63361 if( pRet==0 ){
63362 rc = SQLITE_NOMEM_BKPT;
63363 }else{
63364 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
63365 *ppSnapshot = (sqlite3_snapshot*)pRet;
63366 }
63367
63368 return rc;
63369}
63370
63371/* Try to open on pSnapshot when the next read-transaction starts
63372*/
63373SQLITE_PRIVATE void sqlite3WalSnapshotOpen(
63374 Wal *pWal,
63375 sqlite3_snapshot *pSnapshot
63376){
63377 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
63378}
63379
63380/*
63381** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
63382** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
63383*/
63384SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
63385 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
63386 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
63387
63388 /* aSalt[0] is a copy of the value stored in the wal file header. It
63389 ** is incremented each time the wal file is restarted. */
63390 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
63391 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
63392 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
63393 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
63394 return 0;
63395}
63396
63397/*
63398** The caller currently has a read transaction open on the database.
63399** This function takes a SHARED lock on the CHECKPOINTER slot and then
63400** checks if the snapshot passed as the second argument is still
63401** available. If so, SQLITE_OK is returned.
63402**
63403** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
63404** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
63405** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
63406** lock is released before returning.
63407*/
63408SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
63409 int rc;
63410 rc = walLockShared(pWal, WAL_CKPT_LOCK);
63411 if( rc==SQLITE_OK ){
63412 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
63413 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
63414 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
63415 ){
63416 rc = SQLITE_ERROR_SNAPSHOT;
63417 walUnlockShared(pWal, WAL_CKPT_LOCK);
63418 }
63419 }
63420 return rc;
63421}
63422
63423/*
63424** Release a lock obtained by an earlier successful call to
63425** sqlite3WalSnapshotCheck().
63426*/
63427SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
63428 assert( pWal );
63429 walUnlockShared(pWal, WAL_CKPT_LOCK);
63430}
63431
63432
63433#endif /* SQLITE_ENABLE_SNAPSHOT */
63434
63435#ifdef SQLITE_ENABLE_ZIPVFS
63436/*
63437** If the argument is not NULL, it points to a Wal object that holds a
63438** read-lock. This function returns the database page-size if it is known,
63439** or zero if it is not (or if pWal is NULL).
63440*/
63441SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
63442 assert( pWal==0 || pWal->readLock>=0 );
63443 return (pWal ? pWal->szPage : 0);
63444}
63445#endif
63446
63447/* Return the sqlite3_file object for the WAL file
63448*/
63449SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
63450 return pWal->pWalFd;
63451}
63452
63453#endif /* #ifndef SQLITE_OMIT_WAL */
63454
63455/************** End of wal.c *************************************************/
63456/************** Begin file btmutex.c *****************************************/
63457/*
63458** 2007 August 27
63459**
63460** The author disclaims copyright to this source code. In place of
63461** a legal notice, here is a blessing:
63462**
63463** May you do good and not evil.
63464** May you find forgiveness for yourself and forgive others.
63465** May you share freely, never taking more than you give.
63466**
63467*************************************************************************
63468**
63469** This file contains code used to implement mutexes on Btree objects.
63470** This code really belongs in btree.c. But btree.c is getting too
63471** big and we want to break it down some. This packaged seemed like
63472** a good breakout.
63473*/
63474/************** Include btreeInt.h in the middle of btmutex.c ****************/
63475/************** Begin file btreeInt.h ****************************************/
63476/*
63477** 2004 April 6
63478**
63479** The author disclaims copyright to this source code. In place of
63480** a legal notice, here is a blessing:
63481**
63482** May you do good and not evil.
63483** May you find forgiveness for yourself and forgive others.
63484** May you share freely, never taking more than you give.
63485**
63486*************************************************************************
63487** This file implements an external (disk-based) database using BTrees.
63488** For a detailed discussion of BTrees, refer to
63489**
63490** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
63491** "Sorting And Searching", pages 473-480. Addison-Wesley
63492** Publishing Company, Reading, Massachusetts.
63493**
63494** The basic idea is that each page of the file contains N database
63495** entries and N+1 pointers to subpages.
63496**
63497** ----------------------------------------------------------------
63498** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
63499** ----------------------------------------------------------------
63500**
63501** All of the keys on the page that Ptr(0) points to have values less
63502** than Key(0). All of the keys on page Ptr(1) and its subpages have
63503** values greater than Key(0) and less than Key(1). All of the keys
63504** on Ptr(N) and its subpages have values greater than Key(N-1). And
63505** so forth.
63506**
63507** Finding a particular key requires reading O(log(M)) pages from the
63508** disk where M is the number of entries in the tree.
63509**
63510** In this implementation, a single file can hold one or more separate
63511** BTrees. Each BTree is identified by the index of its root page. The
63512** key and data for any entry are combined to form the "payload". A
63513** fixed amount of payload can be carried directly on the database
63514** page. If the payload is larger than the preset amount then surplus
63515** bytes are stored on overflow pages. The payload for an entry
63516** and the preceding pointer are combined to form a "Cell". Each
63517** page has a small header which contains the Ptr(N) pointer and other
63518** information such as the size of key and data.
63519**
63520** FORMAT DETAILS
63521**
63522** The file is divided into pages. The first page is called page 1,
63523** the second is page 2, and so forth. A page number of zero indicates
63524** "no such page". The page size can be any power of 2 between 512 and 65536.
63525** Each page can be either a btree page, a freelist page, an overflow
63526** page, or a pointer-map page.
63527**
63528** The first page is always a btree page. The first 100 bytes of the first
63529** page contain a special header (the "file header") that describes the file.
63530** The format of the file header is as follows:
63531**
63532** OFFSET SIZE DESCRIPTION
63533** 0 16 Header string: "SQLite format 3\000"
63534** 16 2 Page size in bytes. (1 means 65536)
63535** 18 1 File format write version
63536** 19 1 File format read version
63537** 20 1 Bytes of unused space at the end of each page
63538** 21 1 Max embedded payload fraction (must be 64)
63539** 22 1 Min embedded payload fraction (must be 32)
63540** 23 1 Min leaf payload fraction (must be 32)
63541** 24 4 File change counter
63542** 28 4 Reserved for future use
63543** 32 4 First freelist page
63544** 36 4 Number of freelist pages in the file
63545** 40 60 15 4-byte meta values passed to higher layers
63546**
63547** 40 4 Schema cookie
63548** 44 4 File format of schema layer
63549** 48 4 Size of page cache
63550** 52 4 Largest root-page (auto/incr_vacuum)
63551** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
63552** 60 4 User version
63553** 64 4 Incremental vacuum mode
63554** 68 4 Application-ID
63555** 72 20 unused
63556** 92 4 The version-valid-for number
63557** 96 4 SQLITE_VERSION_NUMBER
63558**
63559** All of the integer values are big-endian (most significant byte first).
63560**
63561** The file change counter is incremented when the database is changed
63562** This counter allows other processes to know when the file has changed
63563** and thus when they need to flush their cache.
63564**
63565** The max embedded payload fraction is the amount of the total usable
63566** space in a page that can be consumed by a single cell for standard
63567** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
63568** is to limit the maximum cell size so that at least 4 cells will fit
63569** on one page. Thus the default max embedded payload fraction is 64.
63570**
63571** If the payload for a cell is larger than the max payload, then extra
63572** payload is spilled to overflow pages. Once an overflow page is allocated,
63573** as many bytes as possible are moved into the overflow pages without letting
63574** the cell size drop below the min embedded payload fraction.
63575**
63576** The min leaf payload fraction is like the min embedded payload fraction
63577** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
63578** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
63579** not specified in the header.
63580**
63581** Each btree pages is divided into three sections: The header, the
63582** cell pointer array, and the cell content area. Page 1 also has a 100-byte
63583** file header that occurs before the page header.
63584**
63585** |----------------|
63586** | file header | 100 bytes. Page 1 only.
63587** |----------------|
63588** | page header | 8 bytes for leaves. 12 bytes for interior nodes
63589** |----------------|
63590** | cell pointer | | 2 bytes per cell. Sorted order.
63591** | array | | Grows downward
63592** | | v
63593** |----------------|
63594** | unallocated |
63595** | space |
63596** |----------------| ^ Grows upwards
63597** | cell content | | Arbitrary order interspersed with freeblocks.
63598** | area | | and free space fragments.
63599** |----------------|
63600**
63601** The page headers looks like this:
63602**
63603** OFFSET SIZE DESCRIPTION
63604** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
63605** 1 2 byte offset to the first freeblock
63606** 3 2 number of cells on this page
63607** 5 2 first byte of the cell content area
63608** 7 1 number of fragmented free bytes
63609** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
63610**
63611** The flags define the format of this btree page. The leaf flag means that
63612** this page has no children. The zerodata flag means that this page carries
63613** only keys and no data. The intkey flag means that the key is an integer
63614** which is stored in the key size entry of the cell header rather than in
63615** the payload area.
63616**
63617** The cell pointer array begins on the first byte after the page header.
63618** The cell pointer array contains zero or more 2-byte numbers which are
63619** offsets from the beginning of the page to the cell content in the cell
63620** content area. The cell pointers occur in sorted order. The system strives
63621** to keep free space after the last cell pointer so that new cells can
63622** be easily added without having to defragment the page.
63623**
63624** Cell content is stored at the very end of the page and grows toward the
63625** beginning of the page.
63626**
63627** Unused space within the cell content area is collected into a linked list of
63628** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
63629** to the first freeblock is given in the header. Freeblocks occur in
63630** increasing order. Because a freeblock must be at least 4 bytes in size,
63631** any group of 3 or fewer unused bytes in the cell content area cannot
63632** exist on the freeblock chain. A group of 3 or fewer free bytes is called
63633** a fragment. The total number of bytes in all fragments is recorded.
63634** in the page header at offset 7.
63635**
63636** SIZE DESCRIPTION
63637** 2 Byte offset of the next freeblock
63638** 2 Bytes in this freeblock
63639**
63640** Cells are of variable length. Cells are stored in the cell content area at
63641** the end of the page. Pointers to the cells are in the cell pointer array
63642** that immediately follows the page header. Cells is not necessarily
63643** contiguous or in order, but cell pointers are contiguous and in order.
63644**
63645** Cell content makes use of variable length integers. A variable
63646** length integer is 1 to 9 bytes where the lower 7 bits of each
63647** byte are used. The integer consists of all bytes that have bit 8 set and
63648** the first byte with bit 8 clear. The most significant byte of the integer
63649** appears first. A variable-length integer may not be more than 9 bytes long.
63650** As a special case, all 8 bytes of the 9th byte are used as data. This
63651** allows a 64-bit integer to be encoded in 9 bytes.
63652**
63653** 0x00 becomes 0x00000000
63654** 0x7f becomes 0x0000007f
63655** 0x81 0x00 becomes 0x00000080
63656** 0x82 0x00 becomes 0x00000100
63657** 0x80 0x7f becomes 0x0000007f
63658** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
63659** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
63660**
63661** Variable length integers are used for rowids and to hold the number of
63662** bytes of key and data in a btree cell.
63663**
63664** The content of a cell looks like this:
63665**
63666** SIZE DESCRIPTION
63667** 4 Page number of the left child. Omitted if leaf flag is set.
63668** var Number of bytes of data. Omitted if the zerodata flag is set.
63669** var Number of bytes of key. Or the key itself if intkey flag is set.
63670** * Payload
63671** 4 First page of the overflow chain. Omitted if no overflow
63672**
63673** Overflow pages form a linked list. Each page except the last is completely
63674** filled with data (pagesize - 4 bytes). The last page can have as little
63675** as 1 byte of data.
63676**
63677** SIZE DESCRIPTION
63678** 4 Page number of next overflow page
63679** * Data
63680**
63681** Freelist pages come in two subtypes: trunk pages and leaf pages. The
63682** file header points to the first in a linked list of trunk page. Each trunk
63683** page points to multiple leaf pages. The content of a leaf page is
63684** unspecified. A trunk page looks like this:
63685**
63686** SIZE DESCRIPTION
63687** 4 Page number of next trunk page
63688** 4 Number of leaf pointers on this page
63689** * zero or more pages numbers of leaves
63690*/
63691/* #include "sqliteInt.h" */
63692
63693
63694/* The following value is the maximum cell size assuming a maximum page
63695** size give above.
63696*/
63697#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
63698
63699/* The maximum number of cells on a single page of the database. This
63700** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
63701** plus 2 bytes for the index to the cell in the page header). Such
63702** small cells will be rare, but they are possible.
63703*/
63704#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
63705
63706/* Forward declarations */
63707typedef struct MemPage MemPage;
63708typedef struct BtLock BtLock;
63709typedef struct CellInfo CellInfo;
63710
63711/*
63712** This is a magic string that appears at the beginning of every
63713** SQLite database in order to identify the file as a real database.
63714**
63715** You can change this value at compile-time by specifying a
63716** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
63717** header must be exactly 16 bytes including the zero-terminator so
63718** the string itself should be 15 characters long. If you change
63719** the header, then your custom library will not be able to read
63720** databases generated by the standard tools and the standard tools
63721** will not be able to read databases created by your custom library.
63722*/
63723#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
63724# define SQLITE_FILE_HEADER "SQLite format 3"
63725#endif
63726
63727/*
63728** Page type flags. An ORed combination of these flags appear as the
63729** first byte of on-disk image of every BTree page.
63730*/
63731#define PTF_INTKEY 0x01
63732#define PTF_ZERODATA 0x02
63733#define PTF_LEAFDATA 0x04
63734#define PTF_LEAF 0x08
63735
63736/*
63737** An instance of this object stores information about each a single database
63738** page that has been loaded into memory. The information in this object
63739** is derived from the raw on-disk page content.
63740**
63741** As each database page is loaded into memory, the pager allocats an
63742** instance of this object and zeros the first 8 bytes. (This is the
63743** "extra" information associated with each page of the pager.)
63744**
63745** Access to all fields of this structure is controlled by the mutex
63746** stored in MemPage.pBt->mutex.
63747*/
63748struct MemPage {
63749 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
63750 u8 bBusy; /* Prevent endless loops on corrupt database files */
63751 u8 intKey; /* True if table b-trees. False for index b-trees */
63752 u8 intKeyLeaf; /* True if the leaf of an intKey table */
63753 Pgno pgno; /* Page number for this page */
63754 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
63755 ** is allocated. All fields that follow must be initialized before use */
63756 u8 leaf; /* True if a leaf page */
63757 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
63758 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
63759 u8 max1bytePayload; /* min(maxLocal,127) */
63760 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
63761 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
63762 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
63763 u16 cellOffset; /* Index in aData of first cell pointer */
63764 int nFree; /* Number of free bytes on the page. -1 for unknown */
63765 u16 nCell; /* Number of cells on this page, local and ovfl */
63766 u16 maskPage; /* Mask for page offset */
63767 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
63768 ** non-overflow cell */
63769 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
63770 BtShared *pBt; /* Pointer to BtShared that this page is part of */
63771 u8 *aData; /* Pointer to disk image of the page data */
63772 u8 *aDataEnd; /* One byte past the end of usable data */
63773 u8 *aCellIdx; /* The cell index area */
63774 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
63775 DbPage *pDbPage; /* Pager page handle */
63776 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
63777 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
63778};
63779
63780/*
63781** A linked list of the following structures is stored at BtShared.pLock.
63782** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
63783** is opened on the table with root page BtShared.iTable. Locks are removed
63784** from this list when a transaction is committed or rolled back, or when
63785** a btree handle is closed.
63786*/
63787struct BtLock {
63788 Btree *pBtree; /* Btree handle holding this lock */
63789 Pgno iTable; /* Root page of table */
63790 u8 eLock; /* READ_LOCK or WRITE_LOCK */
63791 BtLock *pNext; /* Next in BtShared.pLock list */
63792};
63793
63794/* Candidate values for BtLock.eLock */
63795#define READ_LOCK 1
63796#define WRITE_LOCK 2
63797
63798/* A Btree handle
63799**
63800** A database connection contains a pointer to an instance of
63801** this object for every database file that it has open. This structure
63802** is opaque to the database connection. The database connection cannot
63803** see the internals of this structure and only deals with pointers to
63804** this structure.
63805**
63806** For some database files, the same underlying database cache might be
63807** shared between multiple connections. In that case, each connection
63808** has it own instance of this object. But each instance of this object
63809** points to the same BtShared object. The database cache and the
63810** schema associated with the database file are all contained within
63811** the BtShared object.
63812**
63813** All fields in this structure are accessed under sqlite3.mutex.
63814** The pBt pointer itself may not be changed while there exists cursors
63815** in the referenced BtShared that point back to this Btree since those
63816** cursors have to go through this Btree to find their BtShared and
63817** they often do so without holding sqlite3.mutex.
63818*/
63819struct Btree {
63820 sqlite3 *db; /* The database connection holding this btree */
63821 BtShared *pBt; /* Sharable content of this btree */
63822 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
63823 u8 sharable; /* True if we can share pBt with another db */
63824 u8 locked; /* True if db currently has pBt locked */
63825 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
63826 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
63827 int nBackup; /* Number of backup operations reading this btree */
63828 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
63829 Btree *pNext; /* List of other sharable Btrees from the same db */
63830 Btree *pPrev; /* Back pointer of the same list */
63831#ifndef SQLITE_OMIT_SHARED_CACHE
63832 BtLock lock; /* Object used to lock page 1 */
63833#endif
63834};
63835
63836/*
63837** Btree.inTrans may take one of the following values.
63838**
63839** If the shared-data extension is enabled, there may be multiple users
63840** of the Btree structure. At most one of these may open a write transaction,
63841** but any number may have active read transactions.
63842*/
63843#define TRANS_NONE 0
63844#define TRANS_READ 1
63845#define TRANS_WRITE 2
63846
63847/*
63848** An instance of this object represents a single database file.
63849**
63850** A single database file can be in use at the same time by two
63851** or more database connections. When two or more connections are
63852** sharing the same database file, each connection has it own
63853** private Btree object for the file and each of those Btrees points
63854** to this one BtShared object. BtShared.nRef is the number of
63855** connections currently sharing this database file.
63856**
63857** Fields in this structure are accessed under the BtShared.mutex
63858** mutex, except for nRef and pNext which are accessed under the
63859** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
63860** may not be modified once it is initially set as long as nRef>0.
63861** The pSchema field may be set once under BtShared.mutex and
63862** thereafter is unchanged as long as nRef>0.
63863**
63864** isPending:
63865**
63866** If a BtShared client fails to obtain a write-lock on a database
63867** table (because there exists one or more read-locks on the table),
63868** the shared-cache enters 'pending-lock' state and isPending is
63869** set to true.
63870**
63871** The shared-cache leaves the 'pending lock' state when either of
63872** the following occur:
63873**
63874** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
63875** 2) The number of locks held by other connections drops to zero.
63876**
63877** while in the 'pending-lock' state, no connection may start a new
63878** transaction.
63879**
63880** This feature is included to help prevent writer-starvation.
63881*/
63882struct BtShared {
63883 Pager *pPager; /* The page cache */
63884 sqlite3 *db; /* Database connection currently using this Btree */
63885 BtCursor *pCursor; /* A list of all open cursors */
63886 MemPage *pPage1; /* First page of the database */
63887 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
63888#ifndef SQLITE_OMIT_AUTOVACUUM
63889 u8 autoVacuum; /* True if auto-vacuum is enabled */
63890 u8 incrVacuum; /* True if incr-vacuum is enabled */
63891 u8 bDoTruncate; /* True to truncate db on commit */
63892#endif
63893 u8 inTransaction; /* Transaction state */
63894 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
63895 u8 nReserveWanted; /* Desired number of extra bytes per page */
63896 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
63897 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
63898 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
63899 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
63900 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
63901 u32 pageSize; /* Total number of bytes on a page */
63902 u32 usableSize; /* Number of usable bytes on each page */
63903 int nTransaction; /* Number of open transactions (read + write) */
63904 u32 nPage; /* Number of pages in the database */
63905 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
63906 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
63907 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
63908 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
63909#ifndef SQLITE_OMIT_SHARED_CACHE
63910 int nRef; /* Number of references to this structure */
63911 BtShared *pNext; /* Next on a list of sharable BtShared structs */
63912 BtLock *pLock; /* List of locks held on this shared-btree struct */
63913 Btree *pWriter; /* Btree with currently open write transaction */
63914#endif
63915 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
63916};
63917
63918/*
63919** Allowed values for BtShared.btsFlags
63920*/
63921#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
63922#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
63923#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
63924#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */
63925#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */
63926#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */
63927#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
63928#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */
63929#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
63930
63931/*
63932** An instance of the following structure is used to hold information
63933** about a cell. The parseCellPtr() function fills in this structure
63934** based on information extract from the raw disk page.
63935*/
63936struct CellInfo {
63937 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
63938 u8 *pPayload; /* Pointer to the start of payload */
63939 u32 nPayload; /* Bytes of payload */
63940 u16 nLocal; /* Amount of payload held locally, not on overflow */
63941 u16 nSize; /* Size of the cell content on the main b-tree page */
63942};
63943
63944/*
63945** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
63946** this will be declared corrupt. This value is calculated based on a
63947** maximum database size of 2^31 pages a minimum fanout of 2 for a
63948** root-node and 3 for all other internal nodes.
63949**
63950** If a tree that appears to be taller than this is encountered, it is
63951** assumed that the database is corrupt.
63952*/
63953#define BTCURSOR_MAX_DEPTH 20
63954
63955/*
63956** A cursor is a pointer to a particular entry within a particular
63957** b-tree within a database file.
63958**
63959** The entry is identified by its MemPage and the index in
63960** MemPage.aCell[] of the entry.
63961**
63962** A single database file can be shared by two more database connections,
63963** but cursors cannot be shared. Each cursor is associated with a
63964** particular database connection identified BtCursor.pBtree.db.
63965**
63966** Fields in this structure are accessed under the BtShared.mutex
63967** found at self->pBt->mutex.
63968**
63969** skipNext meaning:
63970** The meaning of skipNext depends on the value of eState:
63971**
63972** eState Meaning of skipNext
63973** VALID skipNext is meaningless and is ignored
63974** INVALID skipNext is meaningless and is ignored
63975** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
63976** sqlite3BtreePrevious() is no-op if skipNext<0.
63977** REQUIRESEEK restoreCursorPosition() restores the cursor to
63978** eState=SKIPNEXT if skipNext!=0
63979** FAULT skipNext holds the cursor fault error code.
63980*/
63981struct BtCursor {
63982 u8 eState; /* One of the CURSOR_XXX constants (see below) */
63983 u8 curFlags; /* zero or more BTCF_* flags defined below */
63984 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
63985 u8 hints; /* As configured by CursorSetHints() */
63986 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
63987 ** Error code if eState==CURSOR_FAULT */
63988 Btree *pBtree; /* The Btree to which this cursor belongs */
63989 Pgno *aOverflow; /* Cache of overflow page locations */
63990 void *pKey; /* Saved key that was cursor last known position */
63991 /* All fields above are zeroed when the cursor is allocated. See
63992 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
63993 ** initialized. */
63994#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
63995 BtShared *pBt; /* The BtShared this cursor points to */
63996 BtCursor *pNext; /* Forms a linked list of all cursors */
63997 CellInfo info; /* A parse of the cell we are pointing at */
63998 i64 nKey; /* Size of pKey, or last integer key */
63999 Pgno pgnoRoot; /* The root page of this tree */
64000 i8 iPage; /* Index of current page in apPage */
64001 u8 curIntKey; /* Value of apPage[0]->intKey */
64002 u16 ix; /* Current index for apPage[iPage] */
64003 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
64004 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
64005 MemPage *pPage; /* Current page */
64006 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
64007};
64008
64009/*
64010** Legal values for BtCursor.curFlags
64011*/
64012#define BTCF_WriteFlag 0x01 /* True if a write cursor */
64013#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
64014#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
64015#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
64016#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
64017#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
64018#define BTCF_Pinned 0x40 /* Cursor is busy and cannot be moved */
64019
64020/*
64021** Potential values for BtCursor.eState.
64022**
64023** CURSOR_INVALID:
64024** Cursor does not point to a valid entry. This can happen (for example)
64025** because the table is empty or because BtreeCursorFirst() has not been
64026** called.
64027**
64028** CURSOR_VALID:
64029** Cursor points to a valid entry. getPayload() etc. may be called.
64030**
64031** CURSOR_SKIPNEXT:
64032** Cursor is valid except that the Cursor.skipNext field is non-zero
64033** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
64034** operation should be a no-op.
64035**
64036** CURSOR_REQUIRESEEK:
64037** The table that this cursor was opened on still exists, but has been
64038** modified since the cursor was last used. The cursor position is saved
64039** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
64040** this state, restoreCursorPosition() can be called to attempt to
64041** seek the cursor to the saved position.
64042**
64043** CURSOR_FAULT:
64044** An unrecoverable error (an I/O error or a malloc failure) has occurred
64045** on a different connection that shares the BtShared cache with this
64046** cursor. The error has left the cache in an inconsistent state.
64047** Do nothing else with this cursor. Any attempt to use the cursor
64048** should return the error code stored in BtCursor.skipNext
64049*/
64050#define CURSOR_VALID 0
64051#define CURSOR_INVALID 1
64052#define CURSOR_SKIPNEXT 2
64053#define CURSOR_REQUIRESEEK 3
64054#define CURSOR_FAULT 4
64055
64056/*
64057** The database page the PENDING_BYTE occupies. This page is never used.
64058*/
64059# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
64060
64061/*
64062** These macros define the location of the pointer-map entry for a
64063** database page. The first argument to each is the number of usable
64064** bytes on each page of the database (often 1024). The second is the
64065** page number to look up in the pointer map.
64066**
64067** PTRMAP_PAGENO returns the database page number of the pointer-map
64068** page that stores the required pointer. PTRMAP_PTROFFSET returns
64069** the offset of the requested map entry.
64070**
64071** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
64072** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
64073** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
64074** this test.
64075*/
64076#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
64077#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
64078#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
64079
64080/*
64081** The pointer map is a lookup table that identifies the parent page for
64082** each child page in the database file. The parent page is the page that
64083** contains a pointer to the child. Every page in the database contains
64084** 0 or 1 parent pages. (In this context 'database page' refers
64085** to any page that is not part of the pointer map itself.) Each pointer map
64086** entry consists of a single byte 'type' and a 4 byte parent page number.
64087** The PTRMAP_XXX identifiers below are the valid types.
64088**
64089** The purpose of the pointer map is to facility moving pages from one
64090** position in the file to another as part of autovacuum. When a page
64091** is moved, the pointer in its parent must be updated to point to the
64092** new location. The pointer map is used to locate the parent page quickly.
64093**
64094** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
64095** used in this case.
64096**
64097** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
64098** is not used in this case.
64099**
64100** PTRMAP_OVERFLOW1: The database page is the first page in a list of
64101** overflow pages. The page number identifies the page that
64102** contains the cell with a pointer to this overflow page.
64103**
64104** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
64105** overflow pages. The page-number identifies the previous
64106** page in the overflow page list.
64107**
64108** PTRMAP_BTREE: The database page is a non-root btree page. The page number
64109** identifies the parent page in the btree.
64110*/
64111#define PTRMAP_ROOTPAGE 1
64112#define PTRMAP_FREEPAGE 2
64113#define PTRMAP_OVERFLOW1 3
64114#define PTRMAP_OVERFLOW2 4
64115#define PTRMAP_BTREE 5
64116
64117/* A bunch of assert() statements to check the transaction state variables
64118** of handle p (type Btree*) are internally consistent.
64119*/
64120#define btreeIntegrity(p) \
64121 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
64122 assert( p->pBt->inTransaction>=p->inTrans );
64123
64124
64125/*
64126** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
64127** if the database supports auto-vacuum or not. Because it is used
64128** within an expression that is an argument to another macro
64129** (sqliteMallocRaw), it is not possible to use conditional compilation.
64130** So, this macro is defined instead.
64131*/
64132#ifndef SQLITE_OMIT_AUTOVACUUM
64133#define ISAUTOVACUUM (pBt->autoVacuum)
64134#else
64135#define ISAUTOVACUUM 0
64136#endif
64137
64138
64139/*
64140** This structure is passed around through all the sanity checking routines
64141** in order to keep track of some global state information.
64142**
64143** The aRef[] array is allocated so that there is 1 bit for each page in
64144** the database. As the integrity-check proceeds, for each page used in
64145** the database the corresponding bit is set. This allows integrity-check to
64146** detect pages that are used twice and orphaned pages (both of which
64147** indicate corruption).
64148*/
64149typedef struct IntegrityCk IntegrityCk;
64150struct IntegrityCk {
64151 BtShared *pBt; /* The tree being checked out */
64152 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
64153 u8 *aPgRef; /* 1 bit per page in the db (see above) */
64154 Pgno nPage; /* Number of pages in the database */
64155 int mxErr; /* Stop accumulating errors when this reaches zero */
64156 int nErr; /* Number of messages written to zErrMsg so far */
64157 int bOomFault; /* A memory allocation error has occurred */
64158 const char *zPfx; /* Error message prefix */
64159 Pgno v1; /* Value for first %u substitution in zPfx */
64160 int v2; /* Value for second %d substitution in zPfx */
64161 StrAccum errMsg; /* Accumulate the error message text here */
64162 u32 *heap; /* Min-heap used for analyzing cell coverage */
64163 sqlite3 *db; /* Database connection running the check */
64164};
64165
64166/*
64167** Routines to read or write a two- and four-byte big-endian integer values.
64168*/
64169#define get2byte(x) ((x)[0]<<8 | (x)[1])
64170#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
64171#define get4byte sqlite3Get4byte
64172#define put4byte sqlite3Put4byte
64173
64174/*
64175** get2byteAligned(), unlike get2byte(), requires that its argument point to a
64176** two-byte aligned address. get2bytea() is only used for accessing the
64177** cell addresses in a btree header.
64178*/
64179#if SQLITE_BYTEORDER==4321
64180# define get2byteAligned(x) (*(u16*)(x))
64181#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
64182# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
64183#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
64184# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
64185#else
64186# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
64187#endif
64188
64189/************** End of btreeInt.h ********************************************/
64190/************** Continuing where we left off in btmutex.c ********************/
64191#ifndef SQLITE_OMIT_SHARED_CACHE
64192#if SQLITE_THREADSAFE
64193
64194/*
64195** Obtain the BtShared mutex associated with B-Tree handle p. Also,
64196** set BtShared.db to the database handle associated with p and the
64197** p->locked boolean to true.
64198*/
64199static void lockBtreeMutex(Btree *p){
64200 assert( p->locked==0 );
64201 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
64202 assert( sqlite3_mutex_held(p->db->mutex) );
64203
64204 sqlite3_mutex_enter(p->pBt->mutex);
64205 p->pBt->db = p->db;
64206 p->locked = 1;
64207}
64208
64209/*
64210** Release the BtShared mutex associated with B-Tree handle p and
64211** clear the p->locked boolean.
64212*/
64213static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
64214 BtShared *pBt = p->pBt;
64215 assert( p->locked==1 );
64216 assert( sqlite3_mutex_held(pBt->mutex) );
64217 assert( sqlite3_mutex_held(p->db->mutex) );
64218 assert( p->db==pBt->db );
64219
64220 sqlite3_mutex_leave(pBt->mutex);
64221 p->locked = 0;
64222}
64223
64224/* Forward reference */
64225static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
64226
64227/*
64228** Enter a mutex on the given BTree object.
64229**
64230** If the object is not sharable, then no mutex is ever required
64231** and this routine is a no-op. The underlying mutex is non-recursive.
64232** But we keep a reference count in Btree.wantToLock so the behavior
64233** of this interface is recursive.
64234**
64235** To avoid deadlocks, multiple Btrees are locked in the same order
64236** by all database connections. The p->pNext is a list of other
64237** Btrees belonging to the same database connection as the p Btree
64238** which need to be locked after p. If we cannot get a lock on
64239** p, then first unlock all of the others on p->pNext, then wait
64240** for the lock to become available on p, then relock all of the
64241** subsequent Btrees that desire a lock.
64242*/
64243SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
64244 /* Some basic sanity checking on the Btree. The list of Btrees
64245 ** connected by pNext and pPrev should be in sorted order by
64246 ** Btree.pBt value. All elements of the list should belong to
64247 ** the same connection. Only shared Btrees are on the list. */
64248 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
64249 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
64250 assert( p->pNext==0 || p->pNext->db==p->db );
64251 assert( p->pPrev==0 || p->pPrev->db==p->db );
64252 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
64253
64254 /* Check for locking consistency */
64255 assert( !p->locked || p->wantToLock>0 );
64256 assert( p->sharable || p->wantToLock==0 );
64257
64258 /* We should already hold a lock on the database connection */
64259 assert( sqlite3_mutex_held(p->db->mutex) );
64260
64261 /* Unless the database is sharable and unlocked, then BtShared.db
64262 ** should already be set correctly. */
64263 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
64264
64265 if( !p->sharable ) return;
64266 p->wantToLock++;
64267 if( p->locked ) return;
64268 btreeLockCarefully(p);
64269}
64270
64271/* This is a helper function for sqlite3BtreeLock(). By moving
64272** complex, but seldom used logic, out of sqlite3BtreeLock() and
64273** into this routine, we avoid unnecessary stack pointer changes
64274** and thus help the sqlite3BtreeLock() routine to run much faster
64275** in the common case.
64276*/
64277static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
64278 Btree *pLater;
64279
64280 /* In most cases, we should be able to acquire the lock we
64281 ** want without having to go through the ascending lock
64282 ** procedure that follows. Just be sure not to block.
64283 */
64284 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
64285 p->pBt->db = p->db;
64286 p->locked = 1;
64287 return;
64288 }
64289
64290 /* To avoid deadlock, first release all locks with a larger
64291 ** BtShared address. Then acquire our lock. Then reacquire
64292 ** the other BtShared locks that we used to hold in ascending
64293 ** order.
64294 */
64295 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64296 assert( pLater->sharable );
64297 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
64298 assert( !pLater->locked || pLater->wantToLock>0 );
64299 if( pLater->locked ){
64300 unlockBtreeMutex(pLater);
64301 }
64302 }
64303 lockBtreeMutex(p);
64304 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64305 if( pLater->wantToLock ){
64306 lockBtreeMutex(pLater);
64307 }
64308 }
64309}
64310
64311
64312/*
64313** Exit the recursive mutex on a Btree.
64314*/
64315SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
64316 assert( sqlite3_mutex_held(p->db->mutex) );
64317 if( p->sharable ){
64318 assert( p->wantToLock>0 );
64319 p->wantToLock--;
64320 if( p->wantToLock==0 ){
64321 unlockBtreeMutex(p);
64322 }
64323 }
64324}
64325
64326#ifndef NDEBUG
64327/*
64328** Return true if the BtShared mutex is held on the btree, or if the
64329** B-Tree is not marked as sharable.
64330**
64331** This routine is used only from within assert() statements.
64332*/
64333SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
64334 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
64335 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
64336 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
64337 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
64338
64339 return (p->sharable==0 || p->locked);
64340}
64341#endif
64342
64343
64344/*
64345** Enter the mutex on every Btree associated with a database
64346** connection. This is needed (for example) prior to parsing
64347** a statement since we will be comparing table and column names
64348** against all schemas and we do not want those schemas being
64349** reset out from under us.
64350**
64351** There is a corresponding leave-all procedures.
64352**
64353** Enter the mutexes in accending order by BtShared pointer address
64354** to avoid the possibility of deadlock when two threads with
64355** two or more btrees in common both try to lock all their btrees
64356** at the same instant.
64357*/
64358static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
64359 int i;
64360 int skipOk = 1;
64361 Btree *p;
64362 assert( sqlite3_mutex_held(db->mutex) );
64363 for(i=0; i<db->nDb; i++){
64364 p = db->aDb[i].pBt;
64365 if( p && p->sharable ){
64366 sqlite3BtreeEnter(p);
64367 skipOk = 0;
64368 }
64369 }
64370 db->noSharedCache = skipOk;
64371}
64372SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
64373 if( db->noSharedCache==0 ) btreeEnterAll(db);
64374}
64375static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
64376 int i;
64377 Btree *p;
64378 assert( sqlite3_mutex_held(db->mutex) );
64379 for(i=0; i<db->nDb; i++){
64380 p = db->aDb[i].pBt;
64381 if( p ) sqlite3BtreeLeave(p);
64382 }
64383}
64384SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
64385 if( db->noSharedCache==0 ) btreeLeaveAll(db);
64386}
64387
64388#ifndef NDEBUG
64389/*
64390** Return true if the current thread holds the database connection
64391** mutex and all required BtShared mutexes.
64392**
64393** This routine is used inside assert() statements only.
64394*/
64395SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
64396 int i;
64397 if( !sqlite3_mutex_held(db->mutex) ){
64398 return 0;
64399 }
64400 for(i=0; i<db->nDb; i++){
64401 Btree *p;
64402 p = db->aDb[i].pBt;
64403 if( p && p->sharable &&
64404 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
64405 return 0;
64406 }
64407 }
64408 return 1;
64409}
64410#endif /* NDEBUG */
64411
64412#ifndef NDEBUG
64413/*
64414** Return true if the correct mutexes are held for accessing the
64415** db->aDb[iDb].pSchema structure. The mutexes required for schema
64416** access are:
64417**
64418** (1) The mutex on db
64419** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
64420**
64421** If pSchema is not NULL, then iDb is computed from pSchema and
64422** db using sqlite3SchemaToIndex().
64423*/
64424SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
64425 Btree *p;
64426 assert( db!=0 );
64427 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
64428 assert( iDb>=0 && iDb<db->nDb );
64429 if( !sqlite3_mutex_held(db->mutex) ) return 0;
64430 if( iDb==1 ) return 1;
64431 p = db->aDb[iDb].pBt;
64432 assert( p!=0 );
64433 return p->sharable==0 || p->locked==1;
64434}
64435#endif /* NDEBUG */
64436
64437#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
64438/*
64439** The following are special cases for mutex enter routines for use
64440** in single threaded applications that use shared cache. Except for
64441** these two routines, all mutex operations are no-ops in that case and
64442** are null #defines in btree.h.
64443**
64444** If shared cache is disabled, then all btree mutex routines, including
64445** the ones below, are no-ops and are null #defines in btree.h.
64446*/
64447
64448SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
64449 p->pBt->db = p->db;
64450}
64451SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
64452 int i;
64453 for(i=0; i<db->nDb; i++){
64454 Btree *p = db->aDb[i].pBt;
64455 if( p ){
64456 p->pBt->db = p->db;
64457 }
64458 }
64459}
64460#endif /* if SQLITE_THREADSAFE */
64461
64462#ifndef SQLITE_OMIT_INCRBLOB
64463/*
64464** Enter a mutex on a Btree given a cursor owned by that Btree.
64465**
64466** These entry points are used by incremental I/O only. Enter() is required
64467** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
64468** the build is threadsafe. Leave() is only required by threadsafe builds.
64469*/
64470SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
64471 sqlite3BtreeEnter(pCur->pBtree);
64472}
64473# if SQLITE_THREADSAFE
64474SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
64475 sqlite3BtreeLeave(pCur->pBtree);
64476}
64477# endif
64478#endif /* ifndef SQLITE_OMIT_INCRBLOB */
64479
64480#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
64481
64482/************** End of btmutex.c *********************************************/
64483/************** Begin file btree.c *******************************************/
64484/*
64485** 2004 April 6
64486**
64487** The author disclaims copyright to this source code. In place of
64488** a legal notice, here is a blessing:
64489**
64490** May you do good and not evil.
64491** May you find forgiveness for yourself and forgive others.
64492** May you share freely, never taking more than you give.
64493**
64494*************************************************************************
64495** This file implements an external (disk-based) database using BTrees.
64496** See the header comment on "btreeInt.h" for additional information.
64497** Including a description of file format and an overview of operation.
64498*/
64499/* #include "btreeInt.h" */
64500
64501/*
64502** The header string that appears at the beginning of every
64503** SQLite database.
64504*/
64505static const char zMagicHeader[] = SQLITE_FILE_HEADER;
64506
64507/*
64508** Set this global variable to 1 to enable tracing using the TRACE
64509** macro.
64510*/
64511#if 0
64512int sqlite3BtreeTrace=1; /* True to enable tracing */
64513# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
64514#else
64515# define TRACE(X)
64516#endif
64517
64518/*
64519** Extract a 2-byte big-endian integer from an array of unsigned bytes.
64520** But if the value is zero, make it 65536.
64521**
64522** This routine is used to extract the "offset to cell content area" value
64523** from the header of a btree page. If the page size is 65536 and the page
64524** is empty, the offset should be 65536, but the 2-byte value stores zero.
64525** This routine makes the necessary adjustment to 65536.
64526*/
64527#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
64528
64529/*
64530** Values passed as the 5th argument to allocateBtreePage()
64531*/
64532#define BTALLOC_ANY 0 /* Allocate any page */
64533#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
64534#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
64535
64536/*
64537** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
64538** defined, or 0 if it is. For example:
64539**
64540** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
64541*/
64542#ifndef SQLITE_OMIT_AUTOVACUUM
64543#define IfNotOmitAV(expr) (expr)
64544#else
64545#define IfNotOmitAV(expr) 0
64546#endif
64547
64548#ifndef SQLITE_OMIT_SHARED_CACHE
64549/*
64550** A list of BtShared objects that are eligible for participation
64551** in shared cache. This variable has file scope during normal builds,
64552** but the test harness needs to access it so we make it global for
64553** test builds.
64554**
64555** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
64556*/
64557#ifdef SQLITE_TEST
64558SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
64559#else
64560static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
64561#endif
64562#endif /* SQLITE_OMIT_SHARED_CACHE */
64563
64564#ifndef SQLITE_OMIT_SHARED_CACHE
64565/*
64566** Enable or disable the shared pager and schema features.
64567**
64568** This routine has no effect on existing database connections.
64569** The shared cache setting effects only future calls to
64570** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
64571*/
64572SQLITE_API int sqlite3_enable_shared_cache(int enable){
64573 sqlite3GlobalConfig.sharedCacheEnabled = enable;
64574 return SQLITE_OK;
64575}
64576#endif
64577
64578
64579
64580#ifdef SQLITE_OMIT_SHARED_CACHE
64581 /*
64582 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
64583 ** and clearAllSharedCacheTableLocks()
64584 ** manipulate entries in the BtShared.pLock linked list used to store
64585 ** shared-cache table level locks. If the library is compiled with the
64586 ** shared-cache feature disabled, then there is only ever one user
64587 ** of each BtShared structure and so this locking is not necessary.
64588 ** So define the lock related functions as no-ops.
64589 */
64590 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
64591 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
64592 #define clearAllSharedCacheTableLocks(a)
64593 #define downgradeAllSharedCacheTableLocks(a)
64594 #define hasSharedCacheTableLock(a,b,c,d) 1
64595 #define hasReadConflicts(a, b) 0
64596#endif
64597
64598/*
64599** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
64600** (MemPage*) as an argument. The (MemPage*) must not be NULL.
64601**
64602** If SQLITE_DEBUG is not defined, then this macro is equivalent to
64603** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
64604** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
64605** with the page number and filename associated with the (MemPage*).
64606*/
64607#ifdef SQLITE_DEBUG
64608int corruptPageError(int lineno, MemPage *p){
64609 char *zMsg;
64610 sqlite3BeginBenignMalloc();
64611 zMsg = sqlite3_mprintf("database corruption page %d of %s",
64612 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
64613 );
64614 sqlite3EndBenignMalloc();
64615 if( zMsg ){
64616 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
64617 }
64618 sqlite3_free(zMsg);
64619 return SQLITE_CORRUPT_BKPT;
64620}
64621# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
64622#else
64623# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
64624#endif
64625
64626#ifndef SQLITE_OMIT_SHARED_CACHE
64627
64628#ifdef SQLITE_DEBUG
64629/*
64630**** This function is only used as part of an assert() statement. ***
64631**
64632** Check to see if pBtree holds the required locks to read or write to the
64633** table with root page iRoot. Return 1 if it does and 0 if not.
64634**
64635** For example, when writing to a table with root-page iRoot via
64636** Btree connection pBtree:
64637**
64638** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
64639**
64640** When writing to an index that resides in a sharable database, the
64641** caller should have first obtained a lock specifying the root page of
64642** the corresponding table. This makes things a bit more complicated,
64643** as this module treats each table as a separate structure. To determine
64644** the table corresponding to the index being written, this
64645** function has to search through the database schema.
64646**
64647** Instead of a lock on the table/index rooted at page iRoot, the caller may
64648** hold a write-lock on the schema table (root page 1). This is also
64649** acceptable.
64650*/
64651static int hasSharedCacheTableLock(
64652 Btree *pBtree, /* Handle that must hold lock */
64653 Pgno iRoot, /* Root page of b-tree */
64654 int isIndex, /* True if iRoot is the root of an index b-tree */
64655 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
64656){
64657 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
64658 Pgno iTab = 0;
64659 BtLock *pLock;
64660
64661 /* If this database is not shareable, or if the client is reading
64662 ** and has the read-uncommitted flag set, then no lock is required.
64663 ** Return true immediately.
64664 */
64665 if( (pBtree->sharable==0)
64666 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
64667 ){
64668 return 1;
64669 }
64670
64671 /* If the client is reading or writing an index and the schema is
64672 ** not loaded, then it is too difficult to actually check to see if
64673 ** the correct locks are held. So do not bother - just return true.
64674 ** This case does not come up very often anyhow.
64675 */
64676 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
64677 return 1;
64678 }
64679
64680 /* Figure out the root-page that the lock should be held on. For table
64681 ** b-trees, this is just the root page of the b-tree being read or
64682 ** written. For index b-trees, it is the root page of the associated
64683 ** table. */
64684 if( isIndex ){
64685 HashElem *p;
64686 int bSeen = 0;
64687 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
64688 Index *pIdx = (Index *)sqliteHashData(p);
64689 if( pIdx->tnum==(int)iRoot ){
64690 if( bSeen ){
64691 /* Two or more indexes share the same root page. There must
64692 ** be imposter tables. So just return true. The assert is not
64693 ** useful in that case. */
64694 return 1;
64695 }
64696 iTab = pIdx->pTable->tnum;
64697 bSeen = 1;
64698 }
64699 }
64700 }else{
64701 iTab = iRoot;
64702 }
64703
64704 /* Search for the required lock. Either a write-lock on root-page iTab, a
64705 ** write-lock on the schema table, or (if the client is reading) a
64706 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
64707 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
64708 if( pLock->pBtree==pBtree
64709 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
64710 && pLock->eLock>=eLockType
64711 ){
64712 return 1;
64713 }
64714 }
64715
64716 /* Failed to find the required lock. */
64717 return 0;
64718}
64719#endif /* SQLITE_DEBUG */
64720
64721#ifdef SQLITE_DEBUG
64722/*
64723**** This function may be used as part of assert() statements only. ****
64724**
64725** Return true if it would be illegal for pBtree to write into the
64726** table or index rooted at iRoot because other shared connections are
64727** simultaneously reading that same table or index.
64728**
64729** It is illegal for pBtree to write if some other Btree object that
64730** shares the same BtShared object is currently reading or writing
64731** the iRoot table. Except, if the other Btree object has the
64732** read-uncommitted flag set, then it is OK for the other object to
64733** have a read cursor.
64734**
64735** For example, before writing to any part of the table or index
64736** rooted at page iRoot, one should call:
64737**
64738** assert( !hasReadConflicts(pBtree, iRoot) );
64739*/
64740static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
64741 BtCursor *p;
64742 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
64743 if( p->pgnoRoot==iRoot
64744 && p->pBtree!=pBtree
64745 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
64746 ){
64747 return 1;
64748 }
64749 }
64750 return 0;
64751}
64752#endif /* #ifdef SQLITE_DEBUG */
64753
64754/*
64755** Query to see if Btree handle p may obtain a lock of type eLock
64756** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
64757** SQLITE_OK if the lock may be obtained (by calling
64758** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
64759*/
64760static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
64761 BtShared *pBt = p->pBt;
64762 BtLock *pIter;
64763
64764 assert( sqlite3BtreeHoldsMutex(p) );
64765 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
64766 assert( p->db!=0 );
64767 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
64768
64769 /* If requesting a write-lock, then the Btree must have an open write
64770 ** transaction on this file. And, obviously, for this to be so there
64771 ** must be an open write transaction on the file itself.
64772 */
64773 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
64774 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
64775
64776 /* This routine is a no-op if the shared-cache is not enabled */
64777 if( !p->sharable ){
64778 return SQLITE_OK;
64779 }
64780
64781 /* If some other connection is holding an exclusive lock, the
64782 ** requested lock may not be obtained.
64783 */
64784 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
64785 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
64786 return SQLITE_LOCKED_SHAREDCACHE;
64787 }
64788
64789 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
64790 /* The condition (pIter->eLock!=eLock) in the following if(...)
64791 ** statement is a simplification of:
64792 **
64793 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
64794 **
64795 ** since we know that if eLock==WRITE_LOCK, then no other connection
64796 ** may hold a WRITE_LOCK on any table in this file (since there can
64797 ** only be a single writer).
64798 */
64799 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
64800 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
64801 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
64802 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
64803 if( eLock==WRITE_LOCK ){
64804 assert( p==pBt->pWriter );
64805 pBt->btsFlags |= BTS_PENDING;
64806 }
64807 return SQLITE_LOCKED_SHAREDCACHE;
64808 }
64809 }
64810 return SQLITE_OK;
64811}
64812#endif /* !SQLITE_OMIT_SHARED_CACHE */
64813
64814#ifndef SQLITE_OMIT_SHARED_CACHE
64815/*
64816** Add a lock on the table with root-page iTable to the shared-btree used
64817** by Btree handle p. Parameter eLock must be either READ_LOCK or
64818** WRITE_LOCK.
64819**
64820** This function assumes the following:
64821**
64822** (a) The specified Btree object p is connected to a sharable
64823** database (one with the BtShared.sharable flag set), and
64824**
64825** (b) No other Btree objects hold a lock that conflicts
64826** with the requested lock (i.e. querySharedCacheTableLock() has
64827** already been called and returned SQLITE_OK).
64828**
64829** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
64830** is returned if a malloc attempt fails.
64831*/
64832static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
64833 BtShared *pBt = p->pBt;
64834 BtLock *pLock = 0;
64835 BtLock *pIter;
64836
64837 assert( sqlite3BtreeHoldsMutex(p) );
64838 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
64839 assert( p->db!=0 );
64840
64841 /* A connection with the read-uncommitted flag set will never try to
64842 ** obtain a read-lock using this function. The only read-lock obtained
64843 ** by a connection in read-uncommitted mode is on the sqlite_schema
64844 ** table, and that lock is obtained in BtreeBeginTrans(). */
64845 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
64846
64847 /* This function should only be called on a sharable b-tree after it
64848 ** has been determined that no other b-tree holds a conflicting lock. */
64849 assert( p->sharable );
64850 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
64851
64852 /* First search the list for an existing lock on this table. */
64853 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
64854 if( pIter->iTable==iTable && pIter->pBtree==p ){
64855 pLock = pIter;
64856 break;
64857 }
64858 }
64859
64860 /* If the above search did not find a BtLock struct associating Btree p
64861 ** with table iTable, allocate one and link it into the list.
64862 */
64863 if( !pLock ){
64864 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
64865 if( !pLock ){
64866 return SQLITE_NOMEM_BKPT;
64867 }
64868 pLock->iTable = iTable;
64869 pLock->pBtree = p;
64870 pLock->pNext = pBt->pLock;
64871 pBt->pLock = pLock;
64872 }
64873
64874 /* Set the BtLock.eLock variable to the maximum of the current lock
64875 ** and the requested lock. This means if a write-lock was already held
64876 ** and a read-lock requested, we don't incorrectly downgrade the lock.
64877 */
64878 assert( WRITE_LOCK>READ_LOCK );
64879 if( eLock>pLock->eLock ){
64880 pLock->eLock = eLock;
64881 }
64882
64883 return SQLITE_OK;
64884}
64885#endif /* !SQLITE_OMIT_SHARED_CACHE */
64886
64887#ifndef SQLITE_OMIT_SHARED_CACHE
64888/*
64889** Release all the table locks (locks obtained via calls to
64890** the setSharedCacheTableLock() procedure) held by Btree object p.
64891**
64892** This function assumes that Btree p has an open read or write
64893** transaction. If it does not, then the BTS_PENDING flag
64894** may be incorrectly cleared.
64895*/
64896static void clearAllSharedCacheTableLocks(Btree *p){
64897 BtShared *pBt = p->pBt;
64898 BtLock **ppIter = &pBt->pLock;
64899
64900 assert( sqlite3BtreeHoldsMutex(p) );
64901 assert( p->sharable || 0==*ppIter );
64902 assert( p->inTrans>0 );
64903
64904 while( *ppIter ){
64905 BtLock *pLock = *ppIter;
64906 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
64907 assert( pLock->pBtree->inTrans>=pLock->eLock );
64908 if( pLock->pBtree==p ){
64909 *ppIter = pLock->pNext;
64910 assert( pLock->iTable!=1 || pLock==&p->lock );
64911 if( pLock->iTable!=1 ){
64912 sqlite3_free(pLock);
64913 }
64914 }else{
64915 ppIter = &pLock->pNext;
64916 }
64917 }
64918
64919 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
64920 if( pBt->pWriter==p ){
64921 pBt->pWriter = 0;
64922 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
64923 }else if( pBt->nTransaction==2 ){
64924 /* This function is called when Btree p is concluding its
64925 ** transaction. If there currently exists a writer, and p is not
64926 ** that writer, then the number of locks held by connections other
64927 ** than the writer must be about to drop to zero. In this case
64928 ** set the BTS_PENDING flag to 0.
64929 **
64930 ** If there is not currently a writer, then BTS_PENDING must
64931 ** be zero already. So this next line is harmless in that case.
64932 */
64933 pBt->btsFlags &= ~BTS_PENDING;
64934 }
64935}
64936
64937/*
64938** This function changes all write-locks held by Btree p into read-locks.
64939*/
64940static void downgradeAllSharedCacheTableLocks(Btree *p){
64941 BtShared *pBt = p->pBt;
64942 if( pBt->pWriter==p ){
64943 BtLock *pLock;
64944 pBt->pWriter = 0;
64945 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
64946 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
64947 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
64948 pLock->eLock = READ_LOCK;
64949 }
64950 }
64951}
64952
64953#endif /* SQLITE_OMIT_SHARED_CACHE */
64954
64955static void releasePage(MemPage *pPage); /* Forward reference */
64956static void releasePageOne(MemPage *pPage); /* Forward reference */
64957static void releasePageNotNull(MemPage *pPage); /* Forward reference */
64958
64959/*
64960***** This routine is used inside of assert() only ****
64961**
64962** Verify that the cursor holds the mutex on its BtShared
64963*/
64964#ifdef SQLITE_DEBUG
64965static int cursorHoldsMutex(BtCursor *p){
64966 return sqlite3_mutex_held(p->pBt->mutex);
64967}
64968
64969/* Verify that the cursor and the BtShared agree about what is the current
64970** database connetion. This is important in shared-cache mode. If the database
64971** connection pointers get out-of-sync, it is possible for routines like
64972** btreeInitPage() to reference an stale connection pointer that references a
64973** a connection that has already closed. This routine is used inside assert()
64974** statements only and for the purpose of double-checking that the btree code
64975** does keep the database connection pointers up-to-date.
64976*/
64977static int cursorOwnsBtShared(BtCursor *p){
64978 assert( cursorHoldsMutex(p) );
64979 return (p->pBtree->db==p->pBt->db);
64980}
64981#endif
64982
64983/*
64984** Invalidate the overflow cache of the cursor passed as the first argument.
64985** on the shared btree structure pBt.
64986*/
64987#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
64988
64989/*
64990** Invalidate the overflow page-list cache for all cursors opened
64991** on the shared btree structure pBt.
64992*/
64993static void invalidateAllOverflowCache(BtShared *pBt){
64994 BtCursor *p;
64995 assert( sqlite3_mutex_held(pBt->mutex) );
64996 for(p=pBt->pCursor; p; p=p->pNext){
64997 invalidateOverflowCache(p);
64998 }
64999}
65000
65001#ifndef SQLITE_OMIT_INCRBLOB
65002/*
65003** This function is called before modifying the contents of a table
65004** to invalidate any incrblob cursors that are open on the
65005** row or one of the rows being modified.
65006**
65007** If argument isClearTable is true, then the entire contents of the
65008** table is about to be deleted. In this case invalidate all incrblob
65009** cursors open on any row within the table with root-page pgnoRoot.
65010**
65011** Otherwise, if argument isClearTable is false, then the row with
65012** rowid iRow is being replaced or deleted. In this case invalidate
65013** only those incrblob cursors open on that specific row.
65014*/
65015static void invalidateIncrblobCursors(
65016 Btree *pBtree, /* The database file to check */
65017 Pgno pgnoRoot, /* The table that might be changing */
65018 i64 iRow, /* The rowid that might be changing */
65019 int isClearTable /* True if all rows are being deleted */
65020){
65021 BtCursor *p;
65022 if( pBtree->hasIncrblobCur==0 ) return;
65023 assert( sqlite3BtreeHoldsMutex(pBtree) );
65024 pBtree->hasIncrblobCur = 0;
65025 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
65026 if( (p->curFlags & BTCF_Incrblob)!=0 ){
65027 pBtree->hasIncrblobCur = 1;
65028 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
65029 p->eState = CURSOR_INVALID;
65030 }
65031 }
65032 }
65033}
65034
65035#else
65036 /* Stub function when INCRBLOB is omitted */
65037 #define invalidateIncrblobCursors(w,x,y,z)
65038#endif /* SQLITE_OMIT_INCRBLOB */
65039
65040/*
65041** Set bit pgno of the BtShared.pHasContent bitvec. This is called
65042** when a page that previously contained data becomes a free-list leaf
65043** page.
65044**
65045** The BtShared.pHasContent bitvec exists to work around an obscure
65046** bug caused by the interaction of two useful IO optimizations surrounding
65047** free-list leaf pages:
65048**
65049** 1) When all data is deleted from a page and the page becomes
65050** a free-list leaf page, the page is not written to the database
65051** (as free-list leaf pages contain no meaningful data). Sometimes
65052** such a page is not even journalled (as it will not be modified,
65053** why bother journalling it?).
65054**
65055** 2) When a free-list leaf page is reused, its content is not read
65056** from the database or written to the journal file (why should it
65057** be, if it is not at all meaningful?).
65058**
65059** By themselves, these optimizations work fine and provide a handy
65060** performance boost to bulk delete or insert operations. However, if
65061** a page is moved to the free-list and then reused within the same
65062** transaction, a problem comes up. If the page is not journalled when
65063** it is moved to the free-list and it is also not journalled when it
65064** is extracted from the free-list and reused, then the original data
65065** may be lost. In the event of a rollback, it may not be possible
65066** to restore the database to its original configuration.
65067**
65068** The solution is the BtShared.pHasContent bitvec. Whenever a page is
65069** moved to become a free-list leaf page, the corresponding bit is
65070** set in the bitvec. Whenever a leaf page is extracted from the free-list,
65071** optimization 2 above is omitted if the corresponding bit is already
65072** set in BtShared.pHasContent. The contents of the bitvec are cleared
65073** at the end of every transaction.
65074*/
65075static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
65076 int rc = SQLITE_OK;
65077 if( !pBt->pHasContent ){
65078 assert( pgno<=pBt->nPage );
65079 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
65080 if( !pBt->pHasContent ){
65081 rc = SQLITE_NOMEM_BKPT;
65082 }
65083 }
65084 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
65085 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
65086 }
65087 return rc;
65088}
65089
65090/*
65091** Query the BtShared.pHasContent vector.
65092**
65093** This function is called when a free-list leaf page is removed from the
65094** free-list for reuse. It returns false if it is safe to retrieve the
65095** page from the pager layer with the 'no-content' flag set. True otherwise.
65096*/
65097static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
65098 Bitvec *p = pBt->pHasContent;
65099 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
65100}
65101
65102/*
65103** Clear (destroy) the BtShared.pHasContent bitvec. This should be
65104** invoked at the conclusion of each write-transaction.
65105*/
65106static void btreeClearHasContent(BtShared *pBt){
65107 sqlite3BitvecDestroy(pBt->pHasContent);
65108 pBt->pHasContent = 0;
65109}
65110
65111/*
65112** Release all of the apPage[] pages for a cursor.
65113*/
65114static void btreeReleaseAllCursorPages(BtCursor *pCur){
65115 int i;
65116 if( pCur->iPage>=0 ){
65117 for(i=0; i<pCur->iPage; i++){
65118 releasePageNotNull(pCur->apPage[i]);
65119 }
65120 releasePageNotNull(pCur->pPage);
65121 pCur->iPage = -1;
65122 }
65123}
65124
65125/*
65126** The cursor passed as the only argument must point to a valid entry
65127** when this function is called (i.e. have eState==CURSOR_VALID). This
65128** function saves the current cursor key in variables pCur->nKey and
65129** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
65130** code otherwise.
65131**
65132** If the cursor is open on an intkey table, then the integer key
65133** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
65134** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
65135** set to point to a malloced buffer pCur->nKey bytes in size containing
65136** the key.
65137*/
65138static int saveCursorKey(BtCursor *pCur){
65139 int rc = SQLITE_OK;
65140 assert( CURSOR_VALID==pCur->eState );
65141 assert( 0==pCur->pKey );
65142 assert( cursorHoldsMutex(pCur) );
65143
65144 if( pCur->curIntKey ){
65145 /* Only the rowid is required for a table btree */
65146 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
65147 }else{
65148 /* For an index btree, save the complete key content. It is possible
65149 ** that the current key is corrupt. In that case, it is possible that
65150 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
65151 ** up to the size of 1 varint plus 1 8-byte value when the cursor
65152 ** position is restored. Hence the 17 bytes of padding allocated
65153 ** below. */
65154 void *pKey;
65155 pCur->nKey = sqlite3BtreePayloadSize(pCur);
65156 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
65157 if( pKey ){
65158 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
65159 if( rc==SQLITE_OK ){
65160 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
65161 pCur->pKey = pKey;
65162 }else{
65163 sqlite3_free(pKey);
65164 }
65165 }else{
65166 rc = SQLITE_NOMEM_BKPT;
65167 }
65168 }
65169 assert( !pCur->curIntKey || !pCur->pKey );
65170 return rc;
65171}
65172
65173/*
65174** Save the current cursor position in the variables BtCursor.nKey
65175** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
65176**
65177** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
65178** prior to calling this routine.
65179*/
65180static int saveCursorPosition(BtCursor *pCur){
65181 int rc;
65182
65183 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
65184 assert( 0==pCur->pKey );
65185 assert( cursorHoldsMutex(pCur) );
65186
65187 if( pCur->curFlags & BTCF_Pinned ){
65188 return SQLITE_CONSTRAINT_PINNED;
65189 }
65190 if( pCur->eState==CURSOR_SKIPNEXT ){
65191 pCur->eState = CURSOR_VALID;
65192 }else{
65193 pCur->skipNext = 0;
65194 }
65195
65196 rc = saveCursorKey(pCur);
65197 if( rc==SQLITE_OK ){
65198 btreeReleaseAllCursorPages(pCur);
65199 pCur->eState = CURSOR_REQUIRESEEK;
65200 }
65201
65202 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
65203 return rc;
65204}
65205
65206/* Forward reference */
65207static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
65208
65209/*
65210** Save the positions of all cursors (except pExcept) that are open on
65211** the table with root-page iRoot. "Saving the cursor position" means that
65212** the location in the btree is remembered in such a way that it can be
65213** moved back to the same spot after the btree has been modified. This
65214** routine is called just before cursor pExcept is used to modify the
65215** table, for example in BtreeDelete() or BtreeInsert().
65216**
65217** If there are two or more cursors on the same btree, then all such
65218** cursors should have their BTCF_Multiple flag set. The btreeCursor()
65219** routine enforces that rule. This routine only needs to be called in
65220** the uncommon case when pExpect has the BTCF_Multiple flag set.
65221**
65222** If pExpect!=NULL and if no other cursors are found on the same root-page,
65223** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
65224** pointless call to this routine.
65225**
65226** Implementation note: This routine merely checks to see if any cursors
65227** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
65228** event that cursors are in need to being saved.
65229*/
65230static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
65231 BtCursor *p;
65232 assert( sqlite3_mutex_held(pBt->mutex) );
65233 assert( pExcept==0 || pExcept->pBt==pBt );
65234 for(p=pBt->pCursor; p; p=p->pNext){
65235 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
65236 }
65237 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
65238 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
65239 return SQLITE_OK;
65240}
65241
65242/* This helper routine to saveAllCursors does the actual work of saving
65243** the cursors if and when a cursor is found that actually requires saving.
65244** The common case is that no cursors need to be saved, so this routine is
65245** broken out from its caller to avoid unnecessary stack pointer movement.
65246*/
65247static int SQLITE_NOINLINE saveCursorsOnList(
65248 BtCursor *p, /* The first cursor that needs saving */
65249 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
65250 BtCursor *pExcept /* Do not save this cursor */
65251){
65252 do{
65253 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
65254 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
65255 int rc = saveCursorPosition(p);
65256 if( SQLITE_OK!=rc ){
65257 return rc;
65258 }
65259 }else{
65260 testcase( p->iPage>=0 );
65261 btreeReleaseAllCursorPages(p);
65262 }
65263 }
65264 p = p->pNext;
65265 }while( p );
65266 return SQLITE_OK;
65267}
65268
65269/*
65270** Clear the current cursor position.
65271*/
65272SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
65273 assert( cursorHoldsMutex(pCur) );
65274 sqlite3_free(pCur->pKey);
65275 pCur->pKey = 0;
65276 pCur->eState = CURSOR_INVALID;
65277}
65278
65279/*
65280** In this version of BtreeMoveto, pKey is a packed index record
65281** such as is generated by the OP_MakeRecord opcode. Unpack the
65282** record and then call BtreeMovetoUnpacked() to do the work.
65283*/
65284static int btreeMoveto(
65285 BtCursor *pCur, /* Cursor open on the btree to be searched */
65286 const void *pKey, /* Packed key if the btree is an index */
65287 i64 nKey, /* Integer key for tables. Size of pKey for indices */
65288 int bias, /* Bias search to the high end */
65289 int *pRes /* Write search results here */
65290){
65291 int rc; /* Status code */
65292 UnpackedRecord *pIdxKey; /* Unpacked index key */
65293
65294 if( pKey ){
65295 KeyInfo *pKeyInfo = pCur->pKeyInfo;
65296 assert( nKey==(i64)(int)nKey );
65297 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
65298 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
65299 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
65300 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
65301 rc = SQLITE_CORRUPT_BKPT;
65302 goto moveto_done;
65303 }
65304 }else{
65305 pIdxKey = 0;
65306 }
65307 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
65308moveto_done:
65309 if( pIdxKey ){
65310 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
65311 }
65312 return rc;
65313}
65314
65315/*
65316** Restore the cursor to the position it was in (or as close to as possible)
65317** when saveCursorPosition() was called. Note that this call deletes the
65318** saved position info stored by saveCursorPosition(), so there can be
65319** at most one effective restoreCursorPosition() call after each
65320** saveCursorPosition().
65321*/
65322static int btreeRestoreCursorPosition(BtCursor *pCur){
65323 int rc;
65324 int skipNext = 0;
65325 assert( cursorOwnsBtShared(pCur) );
65326 assert( pCur->eState>=CURSOR_REQUIRESEEK );
65327 if( pCur->eState==CURSOR_FAULT ){
65328 return pCur->skipNext;
65329 }
65330 pCur->eState = CURSOR_INVALID;
65331 if( sqlite3FaultSim(410) ){
65332 rc = SQLITE_IOERR;
65333 }else{
65334 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
65335 }
65336 if( rc==SQLITE_OK ){
65337 sqlite3_free(pCur->pKey);
65338 pCur->pKey = 0;
65339 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
65340 if( skipNext ) pCur->skipNext = skipNext;
65341 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
65342 pCur->eState = CURSOR_SKIPNEXT;
65343 }
65344 }
65345 return rc;
65346}
65347
65348#define restoreCursorPosition(p) \
65349 (p->eState>=CURSOR_REQUIRESEEK ? \
65350 btreeRestoreCursorPosition(p) : \
65351 SQLITE_OK)
65352
65353/*
65354** Determine whether or not a cursor has moved from the position where
65355** it was last placed, or has been invalidated for any other reason.
65356** Cursors can move when the row they are pointing at is deleted out
65357** from under them, for example. Cursor might also move if a btree
65358** is rebalanced.
65359**
65360** Calling this routine with a NULL cursor pointer returns false.
65361**
65362** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
65363** back to where it ought to be if this routine returns true.
65364*/
65365SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
65366 assert( EIGHT_BYTE_ALIGNMENT(pCur)
65367 || pCur==sqlite3BtreeFakeValidCursor() );
65368 assert( offsetof(BtCursor, eState)==0 );
65369 assert( sizeof(pCur->eState)==1 );
65370 return CURSOR_VALID != *(u8*)pCur;
65371}
65372
65373/*
65374** Return a pointer to a fake BtCursor object that will always answer
65375** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
65376** cursor returned must not be used with any other Btree interface.
65377*/
65378SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
65379 static u8 fakeCursor = CURSOR_VALID;
65380 assert( offsetof(BtCursor, eState)==0 );
65381 return (BtCursor*)&fakeCursor;
65382}
65383
65384/*
65385** This routine restores a cursor back to its original position after it
65386** has been moved by some outside activity (such as a btree rebalance or
65387** a row having been deleted out from under the cursor).
65388**
65389** On success, the *pDifferentRow parameter is false if the cursor is left
65390** pointing at exactly the same row. *pDifferntRow is the row the cursor
65391** was pointing to has been deleted, forcing the cursor to point to some
65392** nearby row.
65393**
65394** This routine should only be called for a cursor that just returned
65395** TRUE from sqlite3BtreeCursorHasMoved().
65396*/
65397SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
65398 int rc;
65399
65400 assert( pCur!=0 );
65401 assert( pCur->eState!=CURSOR_VALID );
65402 rc = restoreCursorPosition(pCur);
65403 if( rc ){
65404 *pDifferentRow = 1;
65405 return rc;
65406 }
65407 if( pCur->eState!=CURSOR_VALID ){
65408 *pDifferentRow = 1;
65409 }else{
65410 *pDifferentRow = 0;
65411 }
65412 return SQLITE_OK;
65413}
65414
65415#ifdef SQLITE_ENABLE_CURSOR_HINTS
65416/*
65417** Provide hints to the cursor. The particular hint given (and the type
65418** and number of the varargs parameters) is determined by the eHintType
65419** parameter. See the definitions of the BTREE_HINT_* macros for details.
65420*/
65421SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
65422 /* Used only by system that substitute their own storage engine */
65423}
65424#endif
65425
65426/*
65427** Provide flag hints to the cursor.
65428*/
65429SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
65430 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
65431 pCur->hints = x;
65432}
65433
65434
65435#ifndef SQLITE_OMIT_AUTOVACUUM
65436/*
65437** Given a page number of a regular database page, return the page
65438** number for the pointer-map page that contains the entry for the
65439** input page number.
65440**
65441** Return 0 (not a valid page) for pgno==1 since there is
65442** no pointer map associated with page 1. The integrity_check logic
65443** requires that ptrmapPageno(*,1)!=1.
65444*/
65445static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
65446 int nPagesPerMapPage;
65447 Pgno iPtrMap, ret;
65448 assert( sqlite3_mutex_held(pBt->mutex) );
65449 if( pgno<2 ) return 0;
65450 nPagesPerMapPage = (pBt->usableSize/5)+1;
65451 iPtrMap = (pgno-2)/nPagesPerMapPage;
65452 ret = (iPtrMap*nPagesPerMapPage) + 2;
65453 if( ret==PENDING_BYTE_PAGE(pBt) ){
65454 ret++;
65455 }
65456 return ret;
65457}
65458
65459/*
65460** Write an entry into the pointer map.
65461**
65462** This routine updates the pointer map entry for page number 'key'
65463** so that it maps to type 'eType' and parent page number 'pgno'.
65464**
65465** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
65466** a no-op. If an error occurs, the appropriate error code is written
65467** into *pRC.
65468*/
65469static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
65470 DbPage *pDbPage; /* The pointer map page */
65471 u8 *pPtrmap; /* The pointer map data */
65472 Pgno iPtrmap; /* The pointer map page number */
65473 int offset; /* Offset in pointer map page */
65474 int rc; /* Return code from subfunctions */
65475
65476 if( *pRC ) return;
65477
65478 assert( sqlite3_mutex_held(pBt->mutex) );
65479 /* The super-journal page number must never be used as a pointer map page */
65480 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
65481
65482 assert( pBt->autoVacuum );
65483 if( key==0 ){
65484 *pRC = SQLITE_CORRUPT_BKPT;
65485 return;
65486 }
65487 iPtrmap = PTRMAP_PAGENO(pBt, key);
65488 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
65489 if( rc!=SQLITE_OK ){
65490 *pRC = rc;
65491 return;
65492 }
65493 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
65494 /* The first byte of the extra data is the MemPage.isInit byte.
65495 ** If that byte is set, it means this page is also being used
65496 ** as a btree page. */
65497 *pRC = SQLITE_CORRUPT_BKPT;
65498 goto ptrmap_exit;
65499 }
65500 offset = PTRMAP_PTROFFSET(iPtrmap, key);
65501 if( offset<0 ){
65502 *pRC = SQLITE_CORRUPT_BKPT;
65503 goto ptrmap_exit;
65504 }
65505 assert( offset <= (int)pBt->usableSize-5 );
65506 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
65507
65508 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
65509 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
65510 *pRC= rc = sqlite3PagerWrite(pDbPage);
65511 if( rc==SQLITE_OK ){
65512 pPtrmap[offset] = eType;
65513 put4byte(&pPtrmap[offset+1], parent);
65514 }
65515 }
65516
65517ptrmap_exit:
65518 sqlite3PagerUnref(pDbPage);
65519}
65520
65521/*
65522** Read an entry from the pointer map.
65523**
65524** This routine retrieves the pointer map entry for page 'key', writing
65525** the type and parent page number to *pEType and *pPgno respectively.
65526** An error code is returned if something goes wrong, otherwise SQLITE_OK.
65527*/
65528static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
65529 DbPage *pDbPage; /* The pointer map page */
65530 int iPtrmap; /* Pointer map page index */
65531 u8 *pPtrmap; /* Pointer map page data */
65532 int offset; /* Offset of entry in pointer map */
65533 int rc;
65534
65535 assert( sqlite3_mutex_held(pBt->mutex) );
65536
65537 iPtrmap = PTRMAP_PAGENO(pBt, key);
65538 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
65539 if( rc!=0 ){
65540 return rc;
65541 }
65542 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
65543
65544 offset = PTRMAP_PTROFFSET(iPtrmap, key);
65545 if( offset<0 ){
65546 sqlite3PagerUnref(pDbPage);
65547 return SQLITE_CORRUPT_BKPT;
65548 }
65549 assert( offset <= (int)pBt->usableSize-5 );
65550 assert( pEType!=0 );
65551 *pEType = pPtrmap[offset];
65552 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
65553
65554 sqlite3PagerUnref(pDbPage);
65555 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
65556 return SQLITE_OK;
65557}
65558
65559#else /* if defined SQLITE_OMIT_AUTOVACUUM */
65560 #define ptrmapPut(w,x,y,z,rc)
65561 #define ptrmapGet(w,x,y,z) SQLITE_OK
65562 #define ptrmapPutOvflPtr(x, y, z, rc)
65563#endif
65564
65565/*
65566** Given a btree page and a cell index (0 means the first cell on
65567** the page, 1 means the second cell, and so forth) return a pointer
65568** to the cell content.
65569**
65570** findCellPastPtr() does the same except it skips past the initial
65571** 4-byte child pointer found on interior pages, if there is one.
65572**
65573** This routine works only for pages that do not contain overflow cells.
65574*/
65575#define findCell(P,I) \
65576 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
65577#define findCellPastPtr(P,I) \
65578 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
65579
65580
65581/*
65582** This is common tail processing for btreeParseCellPtr() and
65583** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
65584** on a single B-tree page. Make necessary adjustments to the CellInfo
65585** structure.
65586*/
65587static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
65588 MemPage *pPage, /* Page containing the cell */
65589 u8 *pCell, /* Pointer to the cell text. */
65590 CellInfo *pInfo /* Fill in this structure */
65591){
65592 /* If the payload will not fit completely on the local page, we have
65593 ** to decide how much to store locally and how much to spill onto
65594 ** overflow pages. The strategy is to minimize the amount of unused
65595 ** space on overflow pages while keeping the amount of local storage
65596 ** in between minLocal and maxLocal.
65597 **
65598 ** Warning: changing the way overflow payload is distributed in any
65599 ** way will result in an incompatible file format.
65600 */
65601 int minLocal; /* Minimum amount of payload held locally */
65602 int maxLocal; /* Maximum amount of payload held locally */
65603 int surplus; /* Overflow payload available for local storage */
65604
65605 minLocal = pPage->minLocal;
65606 maxLocal = pPage->maxLocal;
65607 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
65608 testcase( surplus==maxLocal );
65609 testcase( surplus==maxLocal+1 );
65610 if( surplus <= maxLocal ){
65611 pInfo->nLocal = (u16)surplus;
65612 }else{
65613 pInfo->nLocal = (u16)minLocal;
65614 }
65615 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
65616}
65617
65618/*
65619** The following routines are implementations of the MemPage.xParseCell()
65620** method.
65621**
65622** Parse a cell content block and fill in the CellInfo structure.
65623**
65624** btreeParseCellPtr() => table btree leaf nodes
65625** btreeParseCellNoPayload() => table btree internal nodes
65626** btreeParseCellPtrIndex() => index btree nodes
65627**
65628** There is also a wrapper function btreeParseCell() that works for
65629** all MemPage types and that references the cell by index rather than
65630** by pointer.
65631*/
65632static void btreeParseCellPtrNoPayload(
65633 MemPage *pPage, /* Page containing the cell */
65634 u8 *pCell, /* Pointer to the cell text. */
65635 CellInfo *pInfo /* Fill in this structure */
65636){
65637 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65638 assert( pPage->leaf==0 );
65639 assert( pPage->childPtrSize==4 );
65640#ifndef SQLITE_DEBUG
65641 UNUSED_PARAMETER(pPage);
65642#endif
65643 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
65644 pInfo->nPayload = 0;
65645 pInfo->nLocal = 0;
65646 pInfo->pPayload = 0;
65647 return;
65648}
65649static void btreeParseCellPtr(
65650 MemPage *pPage, /* Page containing the cell */
65651 u8 *pCell, /* Pointer to the cell text. */
65652 CellInfo *pInfo /* Fill in this structure */
65653){
65654 u8 *pIter; /* For scanning through pCell */
65655 u32 nPayload; /* Number of bytes of cell payload */
65656 u64 iKey; /* Extracted Key value */
65657
65658 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65659 assert( pPage->leaf==0 || pPage->leaf==1 );
65660 assert( pPage->intKeyLeaf );
65661 assert( pPage->childPtrSize==0 );
65662 pIter = pCell;
65663
65664 /* The next block of code is equivalent to:
65665 **
65666 ** pIter += getVarint32(pIter, nPayload);
65667 **
65668 ** The code is inlined to avoid a function call.
65669 */
65670 nPayload = *pIter;
65671 if( nPayload>=0x80 ){
65672 u8 *pEnd = &pIter[8];
65673 nPayload &= 0x7f;
65674 do{
65675 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
65676 }while( (*pIter)>=0x80 && pIter<pEnd );
65677 }
65678 pIter++;
65679
65680 /* The next block of code is equivalent to:
65681 **
65682 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
65683 **
65684 ** The code is inlined to avoid a function call.
65685 */
65686 iKey = *pIter;
65687 if( iKey>=0x80 ){
65688 u8 *pEnd = &pIter[7];
65689 iKey &= 0x7f;
65690 while(1){
65691 iKey = (iKey<<7) | (*++pIter & 0x7f);
65692 if( (*pIter)<0x80 ) break;
65693 if( pIter>=pEnd ){
65694 iKey = (iKey<<8) | *++pIter;
65695 break;
65696 }
65697 }
65698 }
65699 pIter++;
65700
65701 pInfo->nKey = *(i64*)&iKey;
65702 pInfo->nPayload = nPayload;
65703 pInfo->pPayload = pIter;
65704 testcase( nPayload==pPage->maxLocal );
65705 testcase( nPayload==pPage->maxLocal+1 );
65706 if( nPayload<=pPage->maxLocal ){
65707 /* This is the (easy) common case where the entire payload fits
65708 ** on the local page. No overflow is required.
65709 */
65710 pInfo->nSize = nPayload + (u16)(pIter - pCell);
65711 if( pInfo->nSize<4 ) pInfo->nSize = 4;
65712 pInfo->nLocal = (u16)nPayload;
65713 }else{
65714 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
65715 }
65716}
65717static void btreeParseCellPtrIndex(
65718 MemPage *pPage, /* Page containing the cell */
65719 u8 *pCell, /* Pointer to the cell text. */
65720 CellInfo *pInfo /* Fill in this structure */
65721){
65722 u8 *pIter; /* For scanning through pCell */
65723 u32 nPayload; /* Number of bytes of cell payload */
65724
65725 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65726 assert( pPage->leaf==0 || pPage->leaf==1 );
65727 assert( pPage->intKeyLeaf==0 );
65728 pIter = pCell + pPage->childPtrSize;
65729 nPayload = *pIter;
65730 if( nPayload>=0x80 ){
65731 u8 *pEnd = &pIter[8];
65732 nPayload &= 0x7f;
65733 do{
65734 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
65735 }while( *(pIter)>=0x80 && pIter<pEnd );
65736 }
65737 pIter++;
65738 pInfo->nKey = nPayload;
65739 pInfo->nPayload = nPayload;
65740 pInfo->pPayload = pIter;
65741 testcase( nPayload==pPage->maxLocal );
65742 testcase( nPayload==pPage->maxLocal+1 );
65743 if( nPayload<=pPage->maxLocal ){
65744 /* This is the (easy) common case where the entire payload fits
65745 ** on the local page. No overflow is required.
65746 */
65747 pInfo->nSize = nPayload + (u16)(pIter - pCell);
65748 if( pInfo->nSize<4 ) pInfo->nSize = 4;
65749 pInfo->nLocal = (u16)nPayload;
65750 }else{
65751 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
65752 }
65753}
65754static void btreeParseCell(
65755 MemPage *pPage, /* Page containing the cell */
65756 int iCell, /* The cell index. First cell is 0 */
65757 CellInfo *pInfo /* Fill in this structure */
65758){
65759 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
65760}
65761
65762/*
65763** The following routines are implementations of the MemPage.xCellSize
65764** method.
65765**
65766** Compute the total number of bytes that a Cell needs in the cell
65767** data area of the btree-page. The return number includes the cell
65768** data header and the local payload, but not any overflow page or
65769** the space used by the cell pointer.
65770**
65771** cellSizePtrNoPayload() => table internal nodes
65772** cellSizePtr() => all index nodes & table leaf nodes
65773*/
65774static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
65775 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
65776 u8 *pEnd; /* End mark for a varint */
65777 u32 nSize; /* Size value to return */
65778
65779#ifdef SQLITE_DEBUG
65780 /* The value returned by this function should always be the same as
65781 ** the (CellInfo.nSize) value found by doing a full parse of the
65782 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
65783 ** this function verifies that this invariant is not violated. */
65784 CellInfo debuginfo;
65785 pPage->xParseCell(pPage, pCell, &debuginfo);
65786#endif
65787
65788 nSize = *pIter;
65789 if( nSize>=0x80 ){
65790 pEnd = &pIter[8];
65791 nSize &= 0x7f;
65792 do{
65793 nSize = (nSize<<7) | (*++pIter & 0x7f);
65794 }while( *(pIter)>=0x80 && pIter<pEnd );
65795 }
65796 pIter++;
65797 if( pPage->intKey ){
65798 /* pIter now points at the 64-bit integer key value, a variable length
65799 ** integer. The following block moves pIter to point at the first byte
65800 ** past the end of the key value. */
65801 pEnd = &pIter[9];
65802 while( (*pIter++)&0x80 && pIter<pEnd );
65803 }
65804 testcase( nSize==pPage->maxLocal );
65805 testcase( nSize==pPage->maxLocal+1 );
65806 if( nSize<=pPage->maxLocal ){
65807 nSize += (u32)(pIter - pCell);
65808 if( nSize<4 ) nSize = 4;
65809 }else{
65810 int minLocal = pPage->minLocal;
65811 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
65812 testcase( nSize==pPage->maxLocal );
65813 testcase( nSize==pPage->maxLocal+1 );
65814 if( nSize>pPage->maxLocal ){
65815 nSize = minLocal;
65816 }
65817 nSize += 4 + (u16)(pIter - pCell);
65818 }
65819 assert( nSize==debuginfo.nSize || CORRUPT_DB );
65820 return (u16)nSize;
65821}
65822static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
65823 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
65824 u8 *pEnd; /* End mark for a varint */
65825
65826#ifdef SQLITE_DEBUG
65827 /* The value returned by this function should always be the same as
65828 ** the (CellInfo.nSize) value found by doing a full parse of the
65829 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
65830 ** this function verifies that this invariant is not violated. */
65831 CellInfo debuginfo;
65832 pPage->xParseCell(pPage, pCell, &debuginfo);
65833#else
65834 UNUSED_PARAMETER(pPage);
65835#endif
65836
65837 assert( pPage->childPtrSize==4 );
65838 pEnd = pIter + 9;
65839 while( (*pIter++)&0x80 && pIter<pEnd );
65840 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
65841 return (u16)(pIter - pCell);
65842}
65843
65844
65845#ifdef SQLITE_DEBUG
65846/* This variation on cellSizePtr() is used inside of assert() statements
65847** only. */
65848static u16 cellSize(MemPage *pPage, int iCell){
65849 return pPage->xCellSize(pPage, findCell(pPage, iCell));
65850}
65851#endif
65852
65853#ifndef SQLITE_OMIT_AUTOVACUUM
65854/*
65855** The cell pCell is currently part of page pSrc but will ultimately be part
65856** of pPage. (pSrc and pPager are often the same.) If pCell contains a
65857** pointer to an overflow page, insert an entry into the pointer-map for
65858** the overflow page that will be valid after pCell has been moved to pPage.
65859*/
65860static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
65861 CellInfo info;
65862 if( *pRC ) return;
65863 assert( pCell!=0 );
65864 pPage->xParseCell(pPage, pCell, &info);
65865 if( info.nLocal<info.nPayload ){
65866 Pgno ovfl;
65867 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
65868 testcase( pSrc!=pPage );
65869 *pRC = SQLITE_CORRUPT_BKPT;
65870 return;
65871 }
65872 ovfl = get4byte(&pCell[info.nSize-4]);
65873 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
65874 }
65875}
65876#endif
65877
65878
65879/*
65880** Defragment the page given. This routine reorganizes cells within the
65881** page so that there are no free-blocks on the free-block list.
65882**
65883** Parameter nMaxFrag is the maximum amount of fragmented space that may be
65884** present in the page after this routine returns.
65885**
65886** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
65887** b-tree page so that there are no freeblocks or fragment bytes, all
65888** unused bytes are contained in the unallocated space region, and all
65889** cells are packed tightly at the end of the page.
65890*/
65891static int defragmentPage(MemPage *pPage, int nMaxFrag){
65892 int i; /* Loop counter */
65893 int pc; /* Address of the i-th cell */
65894 int hdr; /* Offset to the page header */
65895 int size; /* Size of a cell */
65896 int usableSize; /* Number of usable bytes on a page */
65897 int cellOffset; /* Offset to the cell pointer array */
65898 int cbrk; /* Offset to the cell content area */
65899 int nCell; /* Number of cells on the page */
65900 unsigned char *data; /* The page data */
65901 unsigned char *temp; /* Temp area for cell content */
65902 unsigned char *src; /* Source of content */
65903 int iCellFirst; /* First allowable cell index */
65904 int iCellLast; /* Last possible cell index */
65905
65906 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
65907 assert( pPage->pBt!=0 );
65908 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
65909 assert( pPage->nOverflow==0 );
65910 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
65911 temp = 0;
65912 src = data = pPage->aData;
65913 hdr = pPage->hdrOffset;
65914 cellOffset = pPage->cellOffset;
65915 nCell = pPage->nCell;
65916 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
65917 iCellFirst = cellOffset + 2*nCell;
65918 usableSize = pPage->pBt->usableSize;
65919
65920 /* This block handles pages with two or fewer free blocks and nMaxFrag
65921 ** or fewer fragmented bytes. In this case it is faster to move the
65922 ** two (or one) blocks of cells using memmove() and add the required
65923 ** offsets to each pointer in the cell-pointer array than it is to
65924 ** reconstruct the entire page. */
65925 if( (int)data[hdr+7]<=nMaxFrag ){
65926 int iFree = get2byte(&data[hdr+1]);
65927 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
65928 if( iFree ){
65929 int iFree2 = get2byte(&data[iFree]);
65930 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
65931 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
65932 u8 *pEnd = &data[cellOffset + nCell*2];
65933 u8 *pAddr;
65934 int sz2 = 0;
65935 int sz = get2byte(&data[iFree+2]);
65936 int top = get2byte(&data[hdr+5]);
65937 if( top>=iFree ){
65938 return SQLITE_CORRUPT_PAGE(pPage);
65939 }
65940 if( iFree2 ){
65941 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
65942 sz2 = get2byte(&data[iFree2+2]);
65943 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
65944 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
65945 sz += sz2;
65946 }else if( NEVER(iFree+sz>usableSize) ){
65947 return SQLITE_CORRUPT_PAGE(pPage);
65948 }
65949
65950 cbrk = top+sz;
65951 assert( cbrk+(iFree-top) <= usableSize );
65952 memmove(&data[cbrk], &data[top], iFree-top);
65953 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
65954 pc = get2byte(pAddr);
65955 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
65956 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
65957 }
65958 goto defragment_out;
65959 }
65960 }
65961 }
65962
65963 cbrk = usableSize;
65964 iCellLast = usableSize - 4;
65965 for(i=0; i<nCell; i++){
65966 u8 *pAddr; /* The i-th cell pointer */
65967 pAddr = &data[cellOffset + i*2];
65968 pc = get2byte(pAddr);
65969 testcase( pc==iCellFirst );
65970 testcase( pc==iCellLast );
65971 /* These conditions have already been verified in btreeInitPage()
65972 ** if PRAGMA cell_size_check=ON.
65973 */
65974 if( pc<iCellFirst || pc>iCellLast ){
65975 return SQLITE_CORRUPT_PAGE(pPage);
65976 }
65977 assert( pc>=iCellFirst && pc<=iCellLast );
65978 size = pPage->xCellSize(pPage, &src[pc]);
65979 cbrk -= size;
65980 if( cbrk<iCellFirst || pc+size>usableSize ){
65981 return SQLITE_CORRUPT_PAGE(pPage);
65982 }
65983 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
65984 testcase( cbrk+size==usableSize );
65985 testcase( pc+size==usableSize );
65986 put2byte(pAddr, cbrk);
65987 if( temp==0 ){
65988 int x;
65989 if( cbrk==pc ) continue;
65990 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
65991 x = get2byte(&data[hdr+5]);
65992 memcpy(&temp[x], &data[x], (cbrk+size) - x);
65993 src = temp;
65994 }
65995 memcpy(&data[cbrk], &src[pc], size);
65996 }
65997 data[hdr+7] = 0;
65998
65999 defragment_out:
66000 assert( pPage->nFree>=0 );
66001 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
66002 return SQLITE_CORRUPT_PAGE(pPage);
66003 }
66004 assert( cbrk>=iCellFirst );
66005 put2byte(&data[hdr+5], cbrk);
66006 data[hdr+1] = 0;
66007 data[hdr+2] = 0;
66008 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
66009 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66010 return SQLITE_OK;
66011}
66012
66013/*
66014** Search the free-list on page pPg for space to store a cell nByte bytes in
66015** size. If one can be found, return a pointer to the space and remove it
66016** from the free-list.
66017**
66018** If no suitable space can be found on the free-list, return NULL.
66019**
66020** This function may detect corruption within pPg. If corruption is
66021** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
66022**
66023** Slots on the free list that are between 1 and 3 bytes larger than nByte
66024** will be ignored if adding the extra space to the fragmentation count
66025** causes the fragmentation count to exceed 60.
66026*/
66027static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
66028 const int hdr = pPg->hdrOffset; /* Offset to page header */
66029 u8 * const aData = pPg->aData; /* Page data */
66030 int iAddr = hdr + 1; /* Address of ptr to pc */
66031 int pc = get2byte(&aData[iAddr]); /* Address of a free slot */
66032 int x; /* Excess size of the slot */
66033 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
66034 int size; /* Size of the free slot */
66035
66036 assert( pc>0 );
66037 while( pc<=maxPC ){
66038 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
66039 ** freeblock form a big-endian integer which is the size of the freeblock
66040 ** in bytes, including the 4-byte header. */
66041 size = get2byte(&aData[pc+2]);
66042 if( (x = size - nByte)>=0 ){
66043 testcase( x==4 );
66044 testcase( x==3 );
66045 if( x<4 ){
66046 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
66047 ** number of bytes in fragments may not exceed 60. */
66048 if( aData[hdr+7]>57 ) return 0;
66049
66050 /* Remove the slot from the free-list. Update the number of
66051 ** fragmented bytes within the page. */
66052 memcpy(&aData[iAddr], &aData[pc], 2);
66053 aData[hdr+7] += (u8)x;
66054 }else if( x+pc > maxPC ){
66055 /* This slot extends off the end of the usable part of the page */
66056 *pRc = SQLITE_CORRUPT_PAGE(pPg);
66057 return 0;
66058 }else{
66059 /* The slot remains on the free-list. Reduce its size to account
66060 ** for the portion used by the new allocation. */
66061 put2byte(&aData[pc+2], x);
66062 }
66063 return &aData[pc + x];
66064 }
66065 iAddr = pc;
66066 pc = get2byte(&aData[pc]);
66067 if( pc<=iAddr+size ){
66068 if( pc ){
66069 /* The next slot in the chain is not past the end of the current slot */
66070 *pRc = SQLITE_CORRUPT_PAGE(pPg);
66071 }
66072 return 0;
66073 }
66074 }
66075 if( pc>maxPC+nByte-4 ){
66076 /* The free slot chain extends off the end of the page */
66077 *pRc = SQLITE_CORRUPT_PAGE(pPg);
66078 }
66079 return 0;
66080}
66081
66082/*
66083** Allocate nByte bytes of space from within the B-Tree page passed
66084** as the first argument. Write into *pIdx the index into pPage->aData[]
66085** of the first byte of allocated space. Return either SQLITE_OK or
66086** an error code (usually SQLITE_CORRUPT).
66087**
66088** The caller guarantees that there is sufficient space to make the
66089** allocation. This routine might need to defragment in order to bring
66090** all the space together, however. This routine will avoid using
66091** the first two bytes past the cell pointer area since presumably this
66092** allocation is being made in order to insert a new cell, so we will
66093** also end up needing a new cell pointer.
66094*/
66095static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
66096 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
66097 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
66098 int top; /* First byte of cell content area */
66099 int rc = SQLITE_OK; /* Integer return code */
66100 int gap; /* First byte of gap between cell pointers and cell content */
66101
66102 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66103 assert( pPage->pBt );
66104 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66105 assert( nByte>=0 ); /* Minimum cell size is 4 */
66106 assert( pPage->nFree>=nByte );
66107 assert( pPage->nOverflow==0 );
66108 assert( nByte < (int)(pPage->pBt->usableSize-8) );
66109
66110 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
66111 gap = pPage->cellOffset + 2*pPage->nCell;
66112 assert( gap<=65536 );
66113 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
66114 ** and the reserved space is zero (the usual value for reserved space)
66115 ** then the cell content offset of an empty page wants to be 65536.
66116 ** However, that integer is too large to be stored in a 2-byte unsigned
66117 ** integer, so a value of 0 is used in its place. */
66118 top = get2byte(&data[hdr+5]);
66119 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
66120 if( gap>top ){
66121 if( top==0 && pPage->pBt->usableSize==65536 ){
66122 top = 65536;
66123 }else{
66124 return SQLITE_CORRUPT_PAGE(pPage);
66125 }
66126 }
66127
66128 /* If there is enough space between gap and top for one more cell pointer,
66129 ** and if the freelist is not empty, then search the
66130 ** freelist looking for a slot big enough to satisfy the request.
66131 */
66132 testcase( gap+2==top );
66133 testcase( gap+1==top );
66134 testcase( gap==top );
66135 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
66136 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
66137 if( pSpace ){
66138 int g2;
66139 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
66140 *pIdx = g2 = (int)(pSpace-data);
66141 if( NEVER(g2<=gap) ){
66142 return SQLITE_CORRUPT_PAGE(pPage);
66143 }else{
66144 return SQLITE_OK;
66145 }
66146 }else if( rc ){
66147 return rc;
66148 }
66149 }
66150
66151 /* The request could not be fulfilled using a freelist slot. Check
66152 ** to see if defragmentation is necessary.
66153 */
66154 testcase( gap+2+nByte==top );
66155 if( gap+2+nByte>top ){
66156 assert( pPage->nCell>0 || CORRUPT_DB );
66157 assert( pPage->nFree>=0 );
66158 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
66159 if( rc ) return rc;
66160 top = get2byteNotZero(&data[hdr+5]);
66161 assert( gap+2+nByte<=top );
66162 }
66163
66164
66165 /* Allocate memory from the gap in between the cell pointer array
66166 ** and the cell content area. The btreeComputeFreeSpace() call has already
66167 ** validated the freelist. Given that the freelist is valid, there
66168 ** is no way that the allocation can extend off the end of the page.
66169 ** The assert() below verifies the previous sentence.
66170 */
66171 top -= nByte;
66172 put2byte(&data[hdr+5], top);
66173 assert( top+nByte <= (int)pPage->pBt->usableSize );
66174 *pIdx = top;
66175 return SQLITE_OK;
66176}
66177
66178/*
66179** Return a section of the pPage->aData to the freelist.
66180** The first byte of the new free block is pPage->aData[iStart]
66181** and the size of the block is iSize bytes.
66182**
66183** Adjacent freeblocks are coalesced.
66184**
66185** Even though the freeblock list was checked by btreeComputeFreeSpace(),
66186** that routine will not detect overlap between cells or freeblocks. Nor
66187** does it detect cells or freeblocks that encrouch into the reserved bytes
66188** at the end of the page. So do additional corruption checks inside this
66189** routine and return SQLITE_CORRUPT if any problems are found.
66190*/
66191static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
66192 u16 iPtr; /* Address of ptr to next freeblock */
66193 u16 iFreeBlk; /* Address of the next freeblock */
66194 u8 hdr; /* Page header size. 0 or 100 */
66195 u8 nFrag = 0; /* Reduction in fragmentation */
66196 u16 iOrigSize = iSize; /* Original value of iSize */
66197 u16 x; /* Offset to cell content area */
66198 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
66199 unsigned char *data = pPage->aData; /* Page content */
66200
66201 assert( pPage->pBt!=0 );
66202 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66203 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
66204 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
66205 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66206 assert( iSize>=4 ); /* Minimum cell size is 4 */
66207 assert( iStart<=pPage->pBt->usableSize-4 );
66208
66209 /* The list of freeblocks must be in ascending order. Find the
66210 ** spot on the list where iStart should be inserted.
66211 */
66212 hdr = pPage->hdrOffset;
66213 iPtr = hdr + 1;
66214 if( data[iPtr+1]==0 && data[iPtr]==0 ){
66215 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
66216 }else{
66217 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
66218 if( iFreeBlk<iPtr+4 ){
66219 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
66220 return SQLITE_CORRUPT_PAGE(pPage);
66221 }
66222 iPtr = iFreeBlk;
66223 }
66224 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
66225 return SQLITE_CORRUPT_PAGE(pPage);
66226 }
66227 assert( iFreeBlk>iPtr || iFreeBlk==0 );
66228
66229 /* At this point:
66230 ** iFreeBlk: First freeblock after iStart, or zero if none
66231 ** iPtr: The address of a pointer to iFreeBlk
66232 **
66233 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
66234 */
66235 if( iFreeBlk && iEnd+3>=iFreeBlk ){
66236 nFrag = iFreeBlk - iEnd;
66237 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
66238 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
66239 if( iEnd > pPage->pBt->usableSize ){
66240 return SQLITE_CORRUPT_PAGE(pPage);
66241 }
66242 iSize = iEnd - iStart;
66243 iFreeBlk = get2byte(&data[iFreeBlk]);
66244 }
66245
66246 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
66247 ** pointer in the page header) then check to see if iStart should be
66248 ** coalesced onto the end of iPtr.
66249 */
66250 if( iPtr>hdr+1 ){
66251 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
66252 if( iPtrEnd+3>=iStart ){
66253 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
66254 nFrag += iStart - iPtrEnd;
66255 iSize = iEnd - iPtr;
66256 iStart = iPtr;
66257 }
66258 }
66259 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
66260 data[hdr+7] -= nFrag;
66261 }
66262 x = get2byte(&data[hdr+5]);
66263 if( iStart<=x ){
66264 /* The new freeblock is at the beginning of the cell content area,
66265 ** so just extend the cell content area rather than create another
66266 ** freelist entry */
66267 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
66268 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
66269 put2byte(&data[hdr+1], iFreeBlk);
66270 put2byte(&data[hdr+5], iEnd);
66271 }else{
66272 /* Insert the new freeblock into the freelist */
66273 put2byte(&data[iPtr], iStart);
66274 }
66275 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
66276 /* Overwrite deleted information with zeros when the secure_delete
66277 ** option is enabled */
66278 memset(&data[iStart], 0, iSize);
66279 }
66280 put2byte(&data[iStart], iFreeBlk);
66281 put2byte(&data[iStart+2], iSize);
66282 pPage->nFree += iOrigSize;
66283 return SQLITE_OK;
66284}
66285
66286/*
66287** Decode the flags byte (the first byte of the header) for a page
66288** and initialize fields of the MemPage structure accordingly.
66289**
66290** Only the following combinations are supported. Anything different
66291** indicates a corrupt database files:
66292**
66293** PTF_ZERODATA
66294** PTF_ZERODATA | PTF_LEAF
66295** PTF_LEAFDATA | PTF_INTKEY
66296** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
66297*/
66298static int decodeFlags(MemPage *pPage, int flagByte){
66299 BtShared *pBt; /* A copy of pPage->pBt */
66300
66301 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
66302 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66303 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
66304 flagByte &= ~PTF_LEAF;
66305 pPage->childPtrSize = 4-4*pPage->leaf;
66306 pPage->xCellSize = cellSizePtr;
66307 pBt = pPage->pBt;
66308 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
66309 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
66310 ** interior table b-tree page. */
66311 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
66312 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
66313 ** leaf table b-tree page. */
66314 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
66315 pPage->intKey = 1;
66316 if( pPage->leaf ){
66317 pPage->intKeyLeaf = 1;
66318 pPage->xParseCell = btreeParseCellPtr;
66319 }else{
66320 pPage->intKeyLeaf = 0;
66321 pPage->xCellSize = cellSizePtrNoPayload;
66322 pPage->xParseCell = btreeParseCellPtrNoPayload;
66323 }
66324 pPage->maxLocal = pBt->maxLeaf;
66325 pPage->minLocal = pBt->minLeaf;
66326 }else if( flagByte==PTF_ZERODATA ){
66327 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
66328 ** interior index b-tree page. */
66329 assert( (PTF_ZERODATA)==2 );
66330 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
66331 ** leaf index b-tree page. */
66332 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
66333 pPage->intKey = 0;
66334 pPage->intKeyLeaf = 0;
66335 pPage->xParseCell = btreeParseCellPtrIndex;
66336 pPage->maxLocal = pBt->maxLocal;
66337 pPage->minLocal = pBt->minLocal;
66338 }else{
66339 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
66340 ** an error. */
66341 return SQLITE_CORRUPT_PAGE(pPage);
66342 }
66343 pPage->max1bytePayload = pBt->max1bytePayload;
66344 return SQLITE_OK;
66345}
66346
66347/*
66348** Compute the amount of freespace on the page. In other words, fill
66349** in the pPage->nFree field.
66350*/
66351static int btreeComputeFreeSpace(MemPage *pPage){
66352 int pc; /* Address of a freeblock within pPage->aData[] */
66353 u8 hdr; /* Offset to beginning of page header */
66354 u8 *data; /* Equal to pPage->aData */
66355 int usableSize; /* Amount of usable space on each page */
66356 int nFree; /* Number of unused bytes on the page */
66357 int top; /* First byte of the cell content area */
66358 int iCellFirst; /* First allowable cell or freeblock offset */
66359 int iCellLast; /* Last possible cell or freeblock offset */
66360
66361 assert( pPage->pBt!=0 );
66362 assert( pPage->pBt->db!=0 );
66363 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66364 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66365 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66366 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66367 assert( pPage->isInit==1 );
66368 assert( pPage->nFree<0 );
66369
66370 usableSize = pPage->pBt->usableSize;
66371 hdr = pPage->hdrOffset;
66372 data = pPage->aData;
66373 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
66374 ** the start of the cell content area. A zero value for this integer is
66375 ** interpreted as 65536. */
66376 top = get2byteNotZero(&data[hdr+5]);
66377 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
66378 iCellLast = usableSize - 4;
66379
66380 /* Compute the total free space on the page
66381 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
66382 ** start of the first freeblock on the page, or is zero if there are no
66383 ** freeblocks. */
66384 pc = get2byte(&data[hdr+1]);
66385 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
66386 if( pc>0 ){
66387 u32 next, size;
66388 if( pc<top ){
66389 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
66390 ** always be at least one cell before the first freeblock.
66391 */
66392 return SQLITE_CORRUPT_PAGE(pPage);
66393 }
66394 while( 1 ){
66395 if( pc>iCellLast ){
66396 /* Freeblock off the end of the page */
66397 return SQLITE_CORRUPT_PAGE(pPage);
66398 }
66399 next = get2byte(&data[pc]);
66400 size = get2byte(&data[pc+2]);
66401 nFree = nFree + size;
66402 if( next<=pc+size+3 ) break;
66403 pc = next;
66404 }
66405 if( next>0 ){
66406 /* Freeblock not in ascending order */
66407 return SQLITE_CORRUPT_PAGE(pPage);
66408 }
66409 if( pc+size>(unsigned int)usableSize ){
66410 /* Last freeblock extends past page end */
66411 return SQLITE_CORRUPT_PAGE(pPage);
66412 }
66413 }
66414
66415 /* At this point, nFree contains the sum of the offset to the start
66416 ** of the cell-content area plus the number of free bytes within
66417 ** the cell-content area. If this is greater than the usable-size
66418 ** of the page, then the page must be corrupted. This check also
66419 ** serves to verify that the offset to the start of the cell-content
66420 ** area, according to the page header, lies within the page.
66421 */
66422 if( nFree>usableSize || nFree<iCellFirst ){
66423 return SQLITE_CORRUPT_PAGE(pPage);
66424 }
66425 pPage->nFree = (u16)(nFree - iCellFirst);
66426 return SQLITE_OK;
66427}
66428
66429/*
66430** Do additional sanity check after btreeInitPage() if
66431** PRAGMA cell_size_check=ON
66432*/
66433static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
66434 int iCellFirst; /* First allowable cell or freeblock offset */
66435 int iCellLast; /* Last possible cell or freeblock offset */
66436 int i; /* Index into the cell pointer array */
66437 int sz; /* Size of a cell */
66438 int pc; /* Address of a freeblock within pPage->aData[] */
66439 u8 *data; /* Equal to pPage->aData */
66440 int usableSize; /* Maximum usable space on the page */
66441 int cellOffset; /* Start of cell content area */
66442
66443 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
66444 usableSize = pPage->pBt->usableSize;
66445 iCellLast = usableSize - 4;
66446 data = pPage->aData;
66447 cellOffset = pPage->cellOffset;
66448 if( !pPage->leaf ) iCellLast--;
66449 for(i=0; i<pPage->nCell; i++){
66450 pc = get2byteAligned(&data[cellOffset+i*2]);
66451 testcase( pc==iCellFirst );
66452 testcase( pc==iCellLast );
66453 if( pc<iCellFirst || pc>iCellLast ){
66454 return SQLITE_CORRUPT_PAGE(pPage);
66455 }
66456 sz = pPage->xCellSize(pPage, &data[pc]);
66457 testcase( pc+sz==usableSize );
66458 if( pc+sz>usableSize ){
66459 return SQLITE_CORRUPT_PAGE(pPage);
66460 }
66461 }
66462 return SQLITE_OK;
66463}
66464
66465/*
66466** Initialize the auxiliary information for a disk block.
66467**
66468** Return SQLITE_OK on success. If we see that the page does
66469** not contain a well-formed database page, then return
66470** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
66471** guarantee that the page is well-formed. It only shows that
66472** we failed to detect any corruption.
66473*/
66474static int btreeInitPage(MemPage *pPage){
66475 u8 *data; /* Equal to pPage->aData */
66476 BtShared *pBt; /* The main btree structure */
66477
66478 assert( pPage->pBt!=0 );
66479 assert( pPage->pBt->db!=0 );
66480 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66481 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66482 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66483 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66484 assert( pPage->isInit==0 );
66485
66486 pBt = pPage->pBt;
66487 data = pPage->aData + pPage->hdrOffset;
66488 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
66489 ** the b-tree page type. */
66490 if( decodeFlags(pPage, data[0]) ){
66491 return SQLITE_CORRUPT_PAGE(pPage);
66492 }
66493 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
66494 pPage->maskPage = (u16)(pBt->pageSize - 1);
66495 pPage->nOverflow = 0;
66496 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
66497 pPage->aCellIdx = data + pPage->childPtrSize + 8;
66498 pPage->aDataEnd = pPage->aData + pBt->usableSize;
66499 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
66500 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
66501 ** number of cells on the page. */
66502 pPage->nCell = get2byte(&data[3]);
66503 if( pPage->nCell>MX_CELL(pBt) ){
66504 /* To many cells for a single page. The page must be corrupt */
66505 return SQLITE_CORRUPT_PAGE(pPage);
66506 }
66507 testcase( pPage->nCell==MX_CELL(pBt) );
66508 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
66509 ** possible for a root page of a table that contains no rows) then the
66510 ** offset to the cell content area will equal the page size minus the
66511 ** bytes of reserved space. */
66512 assert( pPage->nCell>0
66513 || get2byteNotZero(&data[5])==(int)pBt->usableSize
66514 || CORRUPT_DB );
66515 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
66516 pPage->isInit = 1;
66517 if( pBt->db->flags & SQLITE_CellSizeCk ){
66518 return btreeCellSizeCheck(pPage);
66519 }
66520 return SQLITE_OK;
66521}
66522
66523/*
66524** Set up a raw page so that it looks like a database page holding
66525** no entries.
66526*/
66527static void zeroPage(MemPage *pPage, int flags){
66528 unsigned char *data = pPage->aData;
66529 BtShared *pBt = pPage->pBt;
66530 u8 hdr = pPage->hdrOffset;
66531 u16 first;
66532
66533 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
66534 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66535 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
66536 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66537 assert( sqlite3_mutex_held(pBt->mutex) );
66538 if( pBt->btsFlags & BTS_FAST_SECURE ){
66539 memset(&data[hdr], 0, pBt->usableSize - hdr);
66540 }
66541 data[hdr] = (char)flags;
66542 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
66543 memset(&data[hdr+1], 0, 4);
66544 data[hdr+7] = 0;
66545 put2byte(&data[hdr+5], pBt->usableSize);
66546 pPage->nFree = (u16)(pBt->usableSize - first);
66547 decodeFlags(pPage, flags);
66548 pPage->cellOffset = first;
66549 pPage->aDataEnd = &data[pBt->usableSize];
66550 pPage->aCellIdx = &data[first];
66551 pPage->aDataOfst = &data[pPage->childPtrSize];
66552 pPage->nOverflow = 0;
66553 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
66554 pPage->maskPage = (u16)(pBt->pageSize - 1);
66555 pPage->nCell = 0;
66556 pPage->isInit = 1;
66557}
66558
66559
66560/*
66561** Convert a DbPage obtained from the pager into a MemPage used by
66562** the btree layer.
66563*/
66564static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
66565 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
66566 if( pgno!=pPage->pgno ){
66567 pPage->aData = sqlite3PagerGetData(pDbPage);
66568 pPage->pDbPage = pDbPage;
66569 pPage->pBt = pBt;
66570 pPage->pgno = pgno;
66571 pPage->hdrOffset = pgno==1 ? 100 : 0;
66572 }
66573 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
66574 return pPage;
66575}
66576
66577/*
66578** Get a page from the pager. Initialize the MemPage.pBt and
66579** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
66580**
66581** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
66582** about the content of the page at this time. So do not go to the disk
66583** to fetch the content. Just fill in the content with zeros for now.
66584** If in the future we call sqlite3PagerWrite() on this page, that
66585** means we have started to be concerned about content and the disk
66586** read should occur at that point.
66587*/
66588static int btreeGetPage(
66589 BtShared *pBt, /* The btree */
66590 Pgno pgno, /* Number of the page to fetch */
66591 MemPage **ppPage, /* Return the page in this parameter */
66592 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
66593){
66594 int rc;
66595 DbPage *pDbPage;
66596
66597 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
66598 assert( sqlite3_mutex_held(pBt->mutex) );
66599 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
66600 if( rc ) return rc;
66601 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
66602 return SQLITE_OK;
66603}
66604
66605/*
66606** Retrieve a page from the pager cache. If the requested page is not
66607** already in the pager cache return NULL. Initialize the MemPage.pBt and
66608** MemPage.aData elements if needed.
66609*/
66610static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
66611 DbPage *pDbPage;
66612 assert( sqlite3_mutex_held(pBt->mutex) );
66613 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
66614 if( pDbPage ){
66615 return btreePageFromDbPage(pDbPage, pgno, pBt);
66616 }
66617 return 0;
66618}
66619
66620/*
66621** Return the size of the database file in pages. If there is any kind of
66622** error, return ((unsigned int)-1).
66623*/
66624static Pgno btreePagecount(BtShared *pBt){
66625 return pBt->nPage;
66626}
66627SQLITE_PRIVATE Pgno sqlite3BtreeLastPage(Btree *p){
66628 assert( sqlite3BtreeHoldsMutex(p) );
66629 return btreePagecount(p->pBt);
66630}
66631
66632/*
66633** Get a page from the pager and initialize it.
66634**
66635** If pCur!=0 then the page is being fetched as part of a moveToChild()
66636** call. Do additional sanity checking on the page in this case.
66637** And if the fetch fails, this routine must decrement pCur->iPage.
66638**
66639** The page is fetched as read-write unless pCur is not NULL and is
66640** a read-only cursor.
66641**
66642** If an error occurs, then *ppPage is undefined. It
66643** may remain unchanged, or it may be set to an invalid value.
66644*/
66645static int getAndInitPage(
66646 BtShared *pBt, /* The database file */
66647 Pgno pgno, /* Number of the page to get */
66648 MemPage **ppPage, /* Write the page pointer here */
66649 BtCursor *pCur, /* Cursor to receive the page, or NULL */
66650 int bReadOnly /* True for a read-only page */
66651){
66652 int rc;
66653 DbPage *pDbPage;
66654 assert( sqlite3_mutex_held(pBt->mutex) );
66655 assert( pCur==0 || ppPage==&pCur->pPage );
66656 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
66657 assert( pCur==0 || pCur->iPage>0 );
66658
66659 if( pgno>btreePagecount(pBt) ){
66660 rc = SQLITE_CORRUPT_BKPT;
66661 goto getAndInitPage_error1;
66662 }
66663 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
66664 if( rc ){
66665 goto getAndInitPage_error1;
66666 }
66667 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
66668 if( (*ppPage)->isInit==0 ){
66669 btreePageFromDbPage(pDbPage, pgno, pBt);
66670 rc = btreeInitPage(*ppPage);
66671 if( rc!=SQLITE_OK ){
66672 goto getAndInitPage_error2;
66673 }
66674 }
66675 assert( (*ppPage)->pgno==pgno );
66676 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
66677
66678 /* If obtaining a child page for a cursor, we must verify that the page is
66679 ** compatible with the root page. */
66680 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
66681 rc = SQLITE_CORRUPT_PGNO(pgno);
66682 goto getAndInitPage_error2;
66683 }
66684 return SQLITE_OK;
66685
66686getAndInitPage_error2:
66687 releasePage(*ppPage);
66688getAndInitPage_error1:
66689 if( pCur ){
66690 pCur->iPage--;
66691 pCur->pPage = pCur->apPage[pCur->iPage];
66692 }
66693 testcase( pgno==0 );
66694 assert( pgno!=0 || rc==SQLITE_CORRUPT );
66695 return rc;
66696}
66697
66698/*
66699** Release a MemPage. This should be called once for each prior
66700** call to btreeGetPage.
66701**
66702** Page1 is a special case and must be released using releasePageOne().
66703*/
66704static void releasePageNotNull(MemPage *pPage){
66705 assert( pPage->aData );
66706 assert( pPage->pBt );
66707 assert( pPage->pDbPage!=0 );
66708 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66709 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
66710 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66711 sqlite3PagerUnrefNotNull(pPage->pDbPage);
66712}
66713static void releasePage(MemPage *pPage){
66714 if( pPage ) releasePageNotNull(pPage);
66715}
66716static void releasePageOne(MemPage *pPage){
66717 assert( pPage!=0 );
66718 assert( pPage->aData );
66719 assert( pPage->pBt );
66720 assert( pPage->pDbPage!=0 );
66721 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
66722 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
66723 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66724 sqlite3PagerUnrefPageOne(pPage->pDbPage);
66725}
66726
66727/*
66728** Get an unused page.
66729**
66730** This works just like btreeGetPage() with the addition:
66731**
66732** * If the page is already in use for some other purpose, immediately
66733** release it and return an SQLITE_CURRUPT error.
66734** * Make sure the isInit flag is clear
66735*/
66736static int btreeGetUnusedPage(
66737 BtShared *pBt, /* The btree */
66738 Pgno pgno, /* Number of the page to fetch */
66739 MemPage **ppPage, /* Return the page in this parameter */
66740 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
66741){
66742 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
66743 if( rc==SQLITE_OK ){
66744 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
66745 releasePage(*ppPage);
66746 *ppPage = 0;
66747 return SQLITE_CORRUPT_BKPT;
66748 }
66749 (*ppPage)->isInit = 0;
66750 }else{
66751 *ppPage = 0;
66752 }
66753 return rc;
66754}
66755
66756
66757/*
66758** During a rollback, when the pager reloads information into the cache
66759** so that the cache is restored to its original state at the start of
66760** the transaction, for each page restored this routine is called.
66761**
66762** This routine needs to reset the extra data section at the end of the
66763** page to agree with the restored data.
66764*/
66765static void pageReinit(DbPage *pData){
66766 MemPage *pPage;
66767 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
66768 assert( sqlite3PagerPageRefcount(pData)>0 );
66769 if( pPage->isInit ){
66770 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66771 pPage->isInit = 0;
66772 if( sqlite3PagerPageRefcount(pData)>1 ){
66773 /* pPage might not be a btree page; it might be an overflow page
66774 ** or ptrmap page or a free page. In those cases, the following
66775 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
66776 ** But no harm is done by this. And it is very important that
66777 ** btreeInitPage() be called on every btree page so we make
66778 ** the call for every page that comes in for re-initing. */
66779 btreeInitPage(pPage);
66780 }
66781 }
66782}
66783
66784/*
66785** Invoke the busy handler for a btree.
66786*/
66787static int btreeInvokeBusyHandler(void *pArg){
66788 BtShared *pBt = (BtShared*)pArg;
66789 assert( pBt->db );
66790 assert( sqlite3_mutex_held(pBt->db->mutex) );
66791 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
66792}
66793
66794/*
66795** Open a database file.
66796**
66797** zFilename is the name of the database file. If zFilename is NULL
66798** then an ephemeral database is created. The ephemeral database might
66799** be exclusively in memory, or it might use a disk-based memory cache.
66800** Either way, the ephemeral database will be automatically deleted
66801** when sqlite3BtreeClose() is called.
66802**
66803** If zFilename is ":memory:" then an in-memory database is created
66804** that is automatically destroyed when it is closed.
66805**
66806** The "flags" parameter is a bitmask that might contain bits like
66807** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
66808**
66809** If the database is already opened in the same database connection
66810** and we are in shared cache mode, then the open will fail with an
66811** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
66812** objects in the same database connection since doing so will lead
66813** to problems with locking.
66814*/
66815SQLITE_PRIVATE int sqlite3BtreeOpen(
66816 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
66817 const char *zFilename, /* Name of the file containing the BTree database */
66818 sqlite3 *db, /* Associated database handle */
66819 Btree **ppBtree, /* Pointer to new Btree object written here */
66820 int flags, /* Options */
66821 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
66822){
66823 BtShared *pBt = 0; /* Shared part of btree structure */
66824 Btree *p; /* Handle to return */
66825 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
66826 int rc = SQLITE_OK; /* Result code from this function */
66827 u8 nReserve; /* Byte of unused space on each page */
66828 unsigned char zDbHeader[100]; /* Database header content */
66829
66830 /* True if opening an ephemeral, temporary database */
66831 const int isTempDb = zFilename==0 || zFilename[0]==0;
66832
66833 /* Set the variable isMemdb to true for an in-memory database, or
66834 ** false for a file-based database.
66835 */
66836#ifdef SQLITE_OMIT_MEMORYDB
66837 const int isMemdb = 0;
66838#else
66839 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
66840 || (isTempDb && sqlite3TempInMemory(db))
66841 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
66842#endif
66843
66844 assert( db!=0 );
66845 assert( pVfs!=0 );
66846 assert( sqlite3_mutex_held(db->mutex) );
66847 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
66848
66849 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
66850 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
66851
66852 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
66853 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
66854
66855 if( isMemdb ){
66856 flags |= BTREE_MEMORY;
66857 }
66858 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
66859 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
66860 }
66861 p = sqlite3MallocZero(sizeof(Btree));
66862 if( !p ){
66863 return SQLITE_NOMEM_BKPT;
66864 }
66865 p->inTrans = TRANS_NONE;
66866 p->db = db;
66867#ifndef SQLITE_OMIT_SHARED_CACHE
66868 p->lock.pBtree = p;
66869 p->lock.iTable = 1;
66870#endif
66871
66872#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
66873 /*
66874 ** If this Btree is a candidate for shared cache, try to find an
66875 ** existing BtShared object that we can share with
66876 */
66877 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
66878 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
66879 int nFilename = sqlite3Strlen30(zFilename)+1;
66880 int nFullPathname = pVfs->mxPathname+1;
66881 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
66882 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
66883
66884 p->sharable = 1;
66885 if( !zFullPathname ){
66886 sqlite3_free(p);
66887 return SQLITE_NOMEM_BKPT;
66888 }
66889 if( isMemdb ){
66890 memcpy(zFullPathname, zFilename, nFilename);
66891 }else{
66892 rc = sqlite3OsFullPathname(pVfs, zFilename,
66893 nFullPathname, zFullPathname);
66894 if( rc ){
66895 if( rc==SQLITE_OK_SYMLINK ){
66896 rc = SQLITE_OK;
66897 }else{
66898 sqlite3_free(zFullPathname);
66899 sqlite3_free(p);
66900 return rc;
66901 }
66902 }
66903 }
66904#if SQLITE_THREADSAFE
66905 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
66906 sqlite3_mutex_enter(mutexOpen);
66907 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
66908 sqlite3_mutex_enter(mutexShared);
66909#endif
66910 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
66911 assert( pBt->nRef>0 );
66912 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
66913 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
66914 int iDb;
66915 for(iDb=db->nDb-1; iDb>=0; iDb--){
66916 Btree *pExisting = db->aDb[iDb].pBt;
66917 if( pExisting && pExisting->pBt==pBt ){
66918 sqlite3_mutex_leave(mutexShared);
66919 sqlite3_mutex_leave(mutexOpen);
66920 sqlite3_free(zFullPathname);
66921 sqlite3_free(p);
66922 return SQLITE_CONSTRAINT;
66923 }
66924 }
66925 p->pBt = pBt;
66926 pBt->nRef++;
66927 break;
66928 }
66929 }
66930 sqlite3_mutex_leave(mutexShared);
66931 sqlite3_free(zFullPathname);
66932 }
66933#ifdef SQLITE_DEBUG
66934 else{
66935 /* In debug mode, we mark all persistent databases as sharable
66936 ** even when they are not. This exercises the locking code and
66937 ** gives more opportunity for asserts(sqlite3_mutex_held())
66938 ** statements to find locking problems.
66939 */
66940 p->sharable = 1;
66941 }
66942#endif
66943 }
66944#endif
66945 if( pBt==0 ){
66946 /*
66947 ** The following asserts make sure that structures used by the btree are
66948 ** the right size. This is to guard against size changes that result
66949 ** when compiling on a different architecture.
66950 */
66951 assert( sizeof(i64)==8 );
66952 assert( sizeof(u64)==8 );
66953 assert( sizeof(u32)==4 );
66954 assert( sizeof(u16)==2 );
66955 assert( sizeof(Pgno)==4 );
66956
66957 pBt = sqlite3MallocZero( sizeof(*pBt) );
66958 if( pBt==0 ){
66959 rc = SQLITE_NOMEM_BKPT;
66960 goto btree_open_out;
66961 }
66962 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
66963 sizeof(MemPage), flags, vfsFlags, pageReinit);
66964 if( rc==SQLITE_OK ){
66965 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
66966 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
66967 }
66968 if( rc!=SQLITE_OK ){
66969 goto btree_open_out;
66970 }
66971 pBt->openFlags = (u8)flags;
66972 pBt->db = db;
66973 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
66974 p->pBt = pBt;
66975
66976 pBt->pCursor = 0;
66977 pBt->pPage1 = 0;
66978 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
66979#if defined(SQLITE_SECURE_DELETE)
66980 pBt->btsFlags |= BTS_SECURE_DELETE;
66981#elif defined(SQLITE_FAST_SECURE_DELETE)
66982 pBt->btsFlags |= BTS_OVERWRITE;
66983#endif
66984 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
66985 ** determined by the 2-byte integer located at an offset of 16 bytes from
66986 ** the beginning of the database file. */
66987 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
66988 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
66989 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
66990 pBt->pageSize = 0;
66991#ifndef SQLITE_OMIT_AUTOVACUUM
66992 /* If the magic name ":memory:" will create an in-memory database, then
66993 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
66994 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
66995 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
66996 ** regular file-name. In this case the auto-vacuum applies as per normal.
66997 */
66998 if( zFilename && !isMemdb ){
66999 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
67000 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
67001 }
67002#endif
67003 nReserve = 0;
67004 }else{
67005 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
67006 ** determined by the one-byte unsigned integer found at an offset of 20
67007 ** into the database file header. */
67008 nReserve = zDbHeader[20];
67009 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67010#ifndef SQLITE_OMIT_AUTOVACUUM
67011 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
67012 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
67013#endif
67014 }
67015 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
67016 if( rc ) goto btree_open_out;
67017 pBt->usableSize = pBt->pageSize - nReserve;
67018 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
67019
67020#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
67021 /* Add the new BtShared object to the linked list sharable BtShareds.
67022 */
67023 pBt->nRef = 1;
67024 if( p->sharable ){
67025 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
67026 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)
67027 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
67028 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
67029 if( pBt->mutex==0 ){
67030 rc = SQLITE_NOMEM_BKPT;
67031 goto btree_open_out;
67032 }
67033 }
67034 sqlite3_mutex_enter(mutexShared);
67035 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
67036 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
67037 sqlite3_mutex_leave(mutexShared);
67038 }
67039#endif
67040 }
67041
67042#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
67043 /* If the new Btree uses a sharable pBtShared, then link the new
67044 ** Btree into the list of all sharable Btrees for the same connection.
67045 ** The list is kept in ascending order by pBt address.
67046 */
67047 if( p->sharable ){
67048 int i;
67049 Btree *pSib;
67050 for(i=0; i<db->nDb; i++){
67051 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
67052 while( pSib->pPrev ){ pSib = pSib->pPrev; }
67053 if( (uptr)p->pBt<(uptr)pSib->pBt ){
67054 p->pNext = pSib;
67055 p->pPrev = 0;
67056 pSib->pPrev = p;
67057 }else{
67058 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
67059 pSib = pSib->pNext;
67060 }
67061 p->pNext = pSib->pNext;
67062 p->pPrev = pSib;
67063 if( p->pNext ){
67064 p->pNext->pPrev = p;
67065 }
67066 pSib->pNext = p;
67067 }
67068 break;
67069 }
67070 }
67071 }
67072#endif
67073 *ppBtree = p;
67074
67075btree_open_out:
67076 if( rc!=SQLITE_OK ){
67077 if( pBt && pBt->pPager ){
67078 sqlite3PagerClose(pBt->pPager, 0);
67079 }
67080 sqlite3_free(pBt);
67081 sqlite3_free(p);
67082 *ppBtree = 0;
67083 }else{
67084 sqlite3_file *pFile;
67085
67086 /* If the B-Tree was successfully opened, set the pager-cache size to the
67087 ** default value. Except, when opening on an existing shared pager-cache,
67088 ** do not change the pager-cache size.
67089 */
67090 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
67091 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
67092 }
67093
67094 pFile = sqlite3PagerFile(pBt->pPager);
67095 if( pFile->pMethods ){
67096 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
67097 }
67098 }
67099 if( mutexOpen ){
67100 assert( sqlite3_mutex_held(mutexOpen) );
67101 sqlite3_mutex_leave(mutexOpen);
67102 }
67103 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
67104 return rc;
67105}
67106
67107/*
67108** Decrement the BtShared.nRef counter. When it reaches zero,
67109** remove the BtShared structure from the sharing list. Return
67110** true if the BtShared.nRef counter reaches zero and return
67111** false if it is still positive.
67112*/
67113static int removeFromSharingList(BtShared *pBt){
67114#ifndef SQLITE_OMIT_SHARED_CACHE
67115 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )
67116 BtShared *pList;
67117 int removed = 0;
67118
67119 assert( sqlite3_mutex_notheld(pBt->mutex) );
67120 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
67121 sqlite3_mutex_enter(pMainMtx);
67122 pBt->nRef--;
67123 if( pBt->nRef<=0 ){
67124 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
67125 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
67126 }else{
67127 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
67128 while( ALWAYS(pList) && pList->pNext!=pBt ){
67129 pList=pList->pNext;
67130 }
67131 if( ALWAYS(pList) ){
67132 pList->pNext = pBt->pNext;
67133 }
67134 }
67135 if( SQLITE_THREADSAFE ){
67136 sqlite3_mutex_free(pBt->mutex);
67137 }
67138 removed = 1;
67139 }
67140 sqlite3_mutex_leave(pMainMtx);
67141 return removed;
67142#else
67143 return 1;
67144#endif
67145}
67146
67147/*
67148** Make sure pBt->pTmpSpace points to an allocation of
67149** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
67150** pointer.
67151*/
67152static void allocateTempSpace(BtShared *pBt){
67153 if( !pBt->pTmpSpace ){
67154 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
67155
67156 /* One of the uses of pBt->pTmpSpace is to format cells before
67157 ** inserting them into a leaf page (function fillInCell()). If
67158 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
67159 ** by the various routines that manipulate binary cells. Which
67160 ** can mean that fillInCell() only initializes the first 2 or 3
67161 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
67162 ** it into a database page. This is not actually a problem, but it
67163 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
67164 ** data is passed to system call write(). So to avoid this error,
67165 ** zero the first 4 bytes of temp space here.
67166 **
67167 ** Also: Provide four bytes of initialized space before the
67168 ** beginning of pTmpSpace as an area available to prepend the
67169 ** left-child pointer to the beginning of a cell.
67170 */
67171 if( pBt->pTmpSpace ){
67172 memset(pBt->pTmpSpace, 0, 8);
67173 pBt->pTmpSpace += 4;
67174 }
67175 }
67176}
67177
67178/*
67179** Free the pBt->pTmpSpace allocation
67180*/
67181static void freeTempSpace(BtShared *pBt){
67182 if( pBt->pTmpSpace ){
67183 pBt->pTmpSpace -= 4;
67184 sqlite3PageFree(pBt->pTmpSpace);
67185 pBt->pTmpSpace = 0;
67186 }
67187}
67188
67189/*
67190** Close an open database and invalidate all cursors.
67191*/
67192SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
67193 BtShared *pBt = p->pBt;
67194 BtCursor *pCur;
67195
67196 /* Close all cursors opened via this handle. */
67197 assert( sqlite3_mutex_held(p->db->mutex) );
67198 sqlite3BtreeEnter(p);
67199 pCur = pBt->pCursor;
67200 while( pCur ){
67201 BtCursor *pTmp = pCur;
67202 pCur = pCur->pNext;
67203 if( pTmp->pBtree==p ){
67204 sqlite3BtreeCloseCursor(pTmp);
67205 }
67206 }
67207
67208 /* Rollback any active transaction and free the handle structure.
67209 ** The call to sqlite3BtreeRollback() drops any table-locks held by
67210 ** this handle.
67211 */
67212 sqlite3BtreeRollback(p, SQLITE_OK, 0);
67213 sqlite3BtreeLeave(p);
67214
67215 /* If there are still other outstanding references to the shared-btree
67216 ** structure, return now. The remainder of this procedure cleans
67217 ** up the shared-btree.
67218 */
67219 assert( p->wantToLock==0 && p->locked==0 );
67220 if( !p->sharable || removeFromSharingList(pBt) ){
67221 /* The pBt is no longer on the sharing list, so we can access
67222 ** it without having to hold the mutex.
67223 **
67224 ** Clean out and delete the BtShared object.
67225 */
67226 assert( !pBt->pCursor );
67227 sqlite3PagerClose(pBt->pPager, p->db);
67228 if( pBt->xFreeSchema && pBt->pSchema ){
67229 pBt->xFreeSchema(pBt->pSchema);
67230 }
67231 sqlite3DbFree(0, pBt->pSchema);
67232 freeTempSpace(pBt);
67233 sqlite3_free(pBt);
67234 }
67235
67236#ifndef SQLITE_OMIT_SHARED_CACHE
67237 assert( p->wantToLock==0 );
67238 assert( p->locked==0 );
67239 if( p->pPrev ) p->pPrev->pNext = p->pNext;
67240 if( p->pNext ) p->pNext->pPrev = p->pPrev;
67241#endif
67242
67243 sqlite3_free(p);
67244 return SQLITE_OK;
67245}
67246
67247/*
67248** Change the "soft" limit on the number of pages in the cache.
67249** Unused and unmodified pages will be recycled when the number of
67250** pages in the cache exceeds this soft limit. But the size of the
67251** cache is allowed to grow larger than this limit if it contains
67252** dirty pages or pages still in active use.
67253*/
67254SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
67255 BtShared *pBt = p->pBt;
67256 assert( sqlite3_mutex_held(p->db->mutex) );
67257 sqlite3BtreeEnter(p);
67258 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
67259 sqlite3BtreeLeave(p);
67260 return SQLITE_OK;
67261}
67262
67263/*
67264** Change the "spill" limit on the number of pages in the cache.
67265** If the number of pages exceeds this limit during a write transaction,
67266** the pager might attempt to "spill" pages to the journal early in
67267** order to free up memory.
67268**
67269** The value returned is the current spill size. If zero is passed
67270** as an argument, no changes are made to the spill size setting, so
67271** using mxPage of 0 is a way to query the current spill size.
67272*/
67273SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
67274 BtShared *pBt = p->pBt;
67275 int res;
67276 assert( sqlite3_mutex_held(p->db->mutex) );
67277 sqlite3BtreeEnter(p);
67278 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
67279 sqlite3BtreeLeave(p);
67280 return res;
67281}
67282
67283#if SQLITE_MAX_MMAP_SIZE>0
67284/*
67285** Change the limit on the amount of the database file that may be
67286** memory mapped.
67287*/
67288SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
67289 BtShared *pBt = p->pBt;
67290 assert( sqlite3_mutex_held(p->db->mutex) );
67291 sqlite3BtreeEnter(p);
67292 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
67293 sqlite3BtreeLeave(p);
67294 return SQLITE_OK;
67295}
67296#endif /* SQLITE_MAX_MMAP_SIZE>0 */
67297
67298/*
67299** Change the way data is synced to disk in order to increase or decrease
67300** how well the database resists damage due to OS crashes and power
67301** failures. Level 1 is the same as asynchronous (no syncs() occur and
67302** there is a high probability of damage) Level 2 is the default. There
67303** is a very low but non-zero probability of damage. Level 3 reduces the
67304** probability of damage to near zero but with a write performance reduction.
67305*/
67306#ifndef SQLITE_OMIT_PAGER_PRAGMAS
67307SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
67308 Btree *p, /* The btree to set the safety level on */
67309 unsigned pgFlags /* Various PAGER_* flags */
67310){
67311 BtShared *pBt = p->pBt;
67312 assert( sqlite3_mutex_held(p->db->mutex) );
67313 sqlite3BtreeEnter(p);
67314 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
67315 sqlite3BtreeLeave(p);
67316 return SQLITE_OK;
67317}
67318#endif
67319
67320/*
67321** Change the default pages size and the number of reserved bytes per page.
67322** Or, if the page size has already been fixed, return SQLITE_READONLY
67323** without changing anything.
67324**
67325** The page size must be a power of 2 between 512 and 65536. If the page
67326** size supplied does not meet this constraint then the page size is not
67327** changed.
67328**
67329** Page sizes are constrained to be a power of two so that the region
67330** of the database file used for locking (beginning at PENDING_BYTE,
67331** the first byte past the 1GB boundary, 0x40000000) needs to occur
67332** at the beginning of a page.
67333**
67334** If parameter nReserve is less than zero, then the number of reserved
67335** bytes per page is left unchanged.
67336**
67337** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
67338** and autovacuum mode can no longer be changed.
67339*/
67340SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
67341 int rc = SQLITE_OK;
67342 int x;
67343 BtShared *pBt = p->pBt;
67344 assert( nReserve>=0 && nReserve<=255 );
67345 sqlite3BtreeEnter(p);
67346 pBt->nReserveWanted = nReserve;
67347 x = pBt->pageSize - pBt->usableSize;
67348 if( nReserve<x ) nReserve = x;
67349 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
67350 sqlite3BtreeLeave(p);
67351 return SQLITE_READONLY;
67352 }
67353 assert( nReserve>=0 && nReserve<=255 );
67354 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
67355 ((pageSize-1)&pageSize)==0 ){
67356 assert( (pageSize & 7)==0 );
67357 assert( !pBt->pCursor );
67358 pBt->pageSize = (u32)pageSize;
67359 freeTempSpace(pBt);
67360 }
67361 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
67362 pBt->usableSize = pBt->pageSize - (u16)nReserve;
67363 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67364 sqlite3BtreeLeave(p);
67365 return rc;
67366}
67367
67368/*
67369** Return the currently defined page size
67370*/
67371SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
67372 return p->pBt->pageSize;
67373}
67374
67375/*
67376** This function is similar to sqlite3BtreeGetReserve(), except that it
67377** may only be called if it is guaranteed that the b-tree mutex is already
67378** held.
67379**
67380** This is useful in one special case in the backup API code where it is
67381** known that the shared b-tree mutex is held, but the mutex on the
67382** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
67383** were to be called, it might collide with some other operation on the
67384** database handle that owns *p, causing undefined behavior.
67385*/
67386SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
67387 int n;
67388 assert( sqlite3_mutex_held(p->pBt->mutex) );
67389 n = p->pBt->pageSize - p->pBt->usableSize;
67390 return n;
67391}
67392
67393/*
67394** Return the number of bytes of space at the end of every page that
67395** are intentually left unused. This is the "reserved" space that is
67396** sometimes used by extensions.
67397**
67398** The value returned is the larger of the current reserve size and
67399** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
67400** The amount of reserve can only grow - never shrink.
67401*/
67402SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){
67403 int n1, n2;
67404 sqlite3BtreeEnter(p);
67405 n1 = (int)p->pBt->nReserveWanted;
67406 n2 = sqlite3BtreeGetReserveNoMutex(p);
67407 sqlite3BtreeLeave(p);
67408 return n1>n2 ? n1 : n2;
67409}
67410
67411
67412/*
67413** Set the maximum page count for a database if mxPage is positive.
67414** No changes are made if mxPage is 0 or negative.
67415** Regardless of the value of mxPage, return the maximum page count.
67416*/
67417SQLITE_PRIVATE Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
67418 Pgno n;
67419 sqlite3BtreeEnter(p);
67420 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
67421 sqlite3BtreeLeave(p);
67422 return n;
67423}
67424
67425/*
67426** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
67427**
67428** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
67429** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
67430** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
67431** newFlag==(-1) No changes
67432**
67433** This routine acts as a query if newFlag is less than zero
67434**
67435** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
67436** freelist leaf pages are not written back to the database. Thus in-page
67437** deleted content is cleared, but freelist deleted content is not.
67438**
67439** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
67440** that freelist leaf pages are written back into the database, increasing
67441** the amount of disk I/O.
67442*/
67443SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
67444 int b;
67445 if( p==0 ) return 0;
67446 sqlite3BtreeEnter(p);
67447 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
67448 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
67449 if( newFlag>=0 ){
67450 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
67451 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
67452 }
67453 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
67454 sqlite3BtreeLeave(p);
67455 return b;
67456}
67457
67458/*
67459** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
67460** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
67461** is disabled. The default value for the auto-vacuum property is
67462** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
67463*/
67464SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
67465#ifdef SQLITE_OMIT_AUTOVACUUM
67466 return SQLITE_READONLY;
67467#else
67468 BtShared *pBt = p->pBt;
67469 int rc = SQLITE_OK;
67470 u8 av = (u8)autoVacuum;
67471
67472 sqlite3BtreeEnter(p);
67473 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
67474 rc = SQLITE_READONLY;
67475 }else{
67476 pBt->autoVacuum = av ?1:0;
67477 pBt->incrVacuum = av==2 ?1:0;
67478 }
67479 sqlite3BtreeLeave(p);
67480 return rc;
67481#endif
67482}
67483
67484/*
67485** Return the value of the 'auto-vacuum' property. If auto-vacuum is
67486** enabled 1 is returned. Otherwise 0.
67487*/
67488SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
67489#ifdef SQLITE_OMIT_AUTOVACUUM
67490 return BTREE_AUTOVACUUM_NONE;
67491#else
67492 int rc;
67493 sqlite3BtreeEnter(p);
67494 rc = (
67495 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
67496 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
67497 BTREE_AUTOVACUUM_INCR
67498 );
67499 sqlite3BtreeLeave(p);
67500 return rc;
67501#endif
67502}
67503
67504/*
67505** If the user has not set the safety-level for this database connection
67506** using "PRAGMA synchronous", and if the safety-level is not already
67507** set to the value passed to this function as the second parameter,
67508** set it so.
67509*/
67510#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
67511 && !defined(SQLITE_OMIT_WAL)
67512static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
67513 sqlite3 *db;
67514 Db *pDb;
67515 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
67516 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
67517 if( pDb->bSyncSet==0
67518 && pDb->safety_level!=safety_level
67519 && pDb!=&db->aDb[1]
67520 ){
67521 pDb->safety_level = safety_level;
67522 sqlite3PagerSetFlags(pBt->pPager,
67523 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
67524 }
67525 }
67526}
67527#else
67528# define setDefaultSyncFlag(pBt,safety_level)
67529#endif
67530
67531/* Forward declaration */
67532static int newDatabase(BtShared*);
67533
67534
67535/*
67536** Get a reference to pPage1 of the database file. This will
67537** also acquire a readlock on that file.
67538**
67539** SQLITE_OK is returned on success. If the file is not a
67540** well-formed database file, then SQLITE_CORRUPT is returned.
67541** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
67542** is returned if we run out of memory.
67543*/
67544static int lockBtree(BtShared *pBt){
67545 int rc; /* Result code from subfunctions */
67546 MemPage *pPage1; /* Page 1 of the database file */
67547 u32 nPage; /* Number of pages in the database */
67548 u32 nPageFile = 0; /* Number of pages in the database file */
67549 u32 nPageHeader; /* Number of pages in the database according to hdr */
67550
67551 assert( sqlite3_mutex_held(pBt->mutex) );
67552 assert( pBt->pPage1==0 );
67553 rc = sqlite3PagerSharedLock(pBt->pPager);
67554 if( rc!=SQLITE_OK ) return rc;
67555 rc = btreeGetPage(pBt, 1, &pPage1, 0);
67556 if( rc!=SQLITE_OK ) return rc;
67557
67558 /* Do some checking to help insure the file we opened really is
67559 ** a valid database file.
67560 */
67561 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
67562 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
67563 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
67564 nPage = nPageFile;
67565 }
67566 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
67567 nPage = 0;
67568 }
67569 if( nPage>0 ){
67570 u32 pageSize;
67571 u32 usableSize;
67572 u8 *page1 = pPage1->aData;
67573 rc = SQLITE_NOTADB;
67574 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
67575 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
67576 ** 61 74 20 33 00. */
67577 if( memcmp(page1, zMagicHeader, 16)!=0 ){
67578 goto page1_init_failed;
67579 }
67580
67581#ifdef SQLITE_OMIT_WAL
67582 if( page1[18]>1 ){
67583 pBt->btsFlags |= BTS_READ_ONLY;
67584 }
67585 if( page1[19]>1 ){
67586 goto page1_init_failed;
67587 }
67588#else
67589 if( page1[18]>2 ){
67590 pBt->btsFlags |= BTS_READ_ONLY;
67591 }
67592 if( page1[19]>2 ){
67593 goto page1_init_failed;
67594 }
67595
67596 /* If the write version is set to 2, this database should be accessed
67597 ** in WAL mode. If the log is not already open, open it now. Then
67598 ** return SQLITE_OK and return without populating BtShared.pPage1.
67599 ** The caller detects this and calls this function again. This is
67600 ** required as the version of page 1 currently in the page1 buffer
67601 ** may not be the latest version - there may be a newer one in the log
67602 ** file.
67603 */
67604 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
67605 int isOpen = 0;
67606 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
67607 if( rc!=SQLITE_OK ){
67608 goto page1_init_failed;
67609 }else{
67610 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
67611 if( isOpen==0 ){
67612 releasePageOne(pPage1);
67613 return SQLITE_OK;
67614 }
67615 }
67616 rc = SQLITE_NOTADB;
67617 }else{
67618 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
67619 }
67620#endif
67621
67622 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
67623 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
67624 **
67625 ** The original design allowed these amounts to vary, but as of
67626 ** version 3.6.0, we require them to be fixed.
67627 */
67628 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
67629 goto page1_init_failed;
67630 }
67631 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
67632 ** determined by the 2-byte integer located at an offset of 16 bytes from
67633 ** the beginning of the database file. */
67634 pageSize = (page1[16]<<8) | (page1[17]<<16);
67635 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
67636 ** between 512 and 65536 inclusive. */
67637 if( ((pageSize-1)&pageSize)!=0
67638 || pageSize>SQLITE_MAX_PAGE_SIZE
67639 || pageSize<=256
67640 ){
67641 goto page1_init_failed;
67642 }
67643 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67644 assert( (pageSize & 7)==0 );
67645 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
67646 ** integer at offset 20 is the number of bytes of space at the end of
67647 ** each page to reserve for extensions.
67648 **
67649 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
67650 ** determined by the one-byte unsigned integer found at an offset of 20
67651 ** into the database file header. */
67652 usableSize = pageSize - page1[20];
67653 if( (u32)pageSize!=pBt->pageSize ){
67654 /* After reading the first page of the database assuming a page size
67655 ** of BtShared.pageSize, we have discovered that the page-size is
67656 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
67657 ** zero and return SQLITE_OK. The caller will call this function
67658 ** again with the correct page-size.
67659 */
67660 releasePageOne(pPage1);
67661 pBt->usableSize = usableSize;
67662 pBt->pageSize = pageSize;
67663 freeTempSpace(pBt);
67664 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
67665 pageSize-usableSize);
67666 return rc;
67667 }
67668 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
67669 rc = SQLITE_CORRUPT_BKPT;
67670 goto page1_init_failed;
67671 }
67672 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
67673 ** be less than 480. In other words, if the page size is 512, then the
67674 ** reserved space size cannot exceed 32. */
67675 if( usableSize<480 ){
67676 goto page1_init_failed;
67677 }
67678 pBt->pageSize = pageSize;
67679 pBt->usableSize = usableSize;
67680#ifndef SQLITE_OMIT_AUTOVACUUM
67681 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
67682 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
67683#endif
67684 }
67685
67686 /* maxLocal is the maximum amount of payload to store locally for
67687 ** a cell. Make sure it is small enough so that at least minFanout
67688 ** cells can will fit on one page. We assume a 10-byte page header.
67689 ** Besides the payload, the cell must store:
67690 ** 2-byte pointer to the cell
67691 ** 4-byte child pointer
67692 ** 9-byte nKey value
67693 ** 4-byte nData value
67694 ** 4-byte overflow page pointer
67695 ** So a cell consists of a 2-byte pointer, a header which is as much as
67696 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
67697 ** page pointer.
67698 */
67699 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
67700 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
67701 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
67702 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
67703 if( pBt->maxLocal>127 ){
67704 pBt->max1bytePayload = 127;
67705 }else{
67706 pBt->max1bytePayload = (u8)pBt->maxLocal;
67707 }
67708 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
67709 pBt->pPage1 = pPage1;
67710 pBt->nPage = nPage;
67711 return SQLITE_OK;
67712
67713page1_init_failed:
67714 releasePageOne(pPage1);
67715 pBt->pPage1 = 0;
67716 return rc;
67717}
67718
67719#ifndef NDEBUG
67720/*
67721** Return the number of cursors open on pBt. This is for use
67722** in assert() expressions, so it is only compiled if NDEBUG is not
67723** defined.
67724**
67725** Only write cursors are counted if wrOnly is true. If wrOnly is
67726** false then all cursors are counted.
67727**
67728** For the purposes of this routine, a cursor is any cursor that
67729** is capable of reading or writing to the database. Cursors that
67730** have been tripped into the CURSOR_FAULT state are not counted.
67731*/
67732static int countValidCursors(BtShared *pBt, int wrOnly){
67733 BtCursor *pCur;
67734 int r = 0;
67735 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
67736 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
67737 && pCur->eState!=CURSOR_FAULT ) r++;
67738 }
67739 return r;
67740}
67741#endif
67742
67743/*
67744** If there are no outstanding cursors and we are not in the middle
67745** of a transaction but there is a read lock on the database, then
67746** this routine unrefs the first page of the database file which
67747** has the effect of releasing the read lock.
67748**
67749** If there is a transaction in progress, this routine is a no-op.
67750*/
67751static void unlockBtreeIfUnused(BtShared *pBt){
67752 assert( sqlite3_mutex_held(pBt->mutex) );
67753 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
67754 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
67755 MemPage *pPage1 = pBt->pPage1;
67756 assert( pPage1->aData );
67757 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
67758 pBt->pPage1 = 0;
67759 releasePageOne(pPage1);
67760 }
67761}
67762
67763/*
67764** If pBt points to an empty file then convert that empty file
67765** into a new empty database by initializing the first page of
67766** the database.
67767*/
67768static int newDatabase(BtShared *pBt){
67769 MemPage *pP1;
67770 unsigned char *data;
67771 int rc;
67772
67773 assert( sqlite3_mutex_held(pBt->mutex) );
67774 if( pBt->nPage>0 ){
67775 return SQLITE_OK;
67776 }
67777 pP1 = pBt->pPage1;
67778 assert( pP1!=0 );
67779 data = pP1->aData;
67780 rc = sqlite3PagerWrite(pP1->pDbPage);
67781 if( rc ) return rc;
67782 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
67783 assert( sizeof(zMagicHeader)==16 );
67784 data[16] = (u8)((pBt->pageSize>>8)&0xff);
67785 data[17] = (u8)((pBt->pageSize>>16)&0xff);
67786 data[18] = 1;
67787 data[19] = 1;
67788 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
67789 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
67790 data[21] = 64;
67791 data[22] = 32;
67792 data[23] = 32;
67793 memset(&data[24], 0, 100-24);
67794 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
67795 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67796#ifndef SQLITE_OMIT_AUTOVACUUM
67797 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
67798 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
67799 put4byte(&data[36 + 4*4], pBt->autoVacuum);
67800 put4byte(&data[36 + 7*4], pBt->incrVacuum);
67801#endif
67802 pBt->nPage = 1;
67803 data[31] = 1;
67804 return SQLITE_OK;
67805}
67806
67807/*
67808** Initialize the first page of the database file (creating a database
67809** consisting of a single page and no schema objects). Return SQLITE_OK
67810** if successful, or an SQLite error code otherwise.
67811*/
67812SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
67813 int rc;
67814 sqlite3BtreeEnter(p);
67815 p->pBt->nPage = 0;
67816 rc = newDatabase(p->pBt);
67817 sqlite3BtreeLeave(p);
67818 return rc;
67819}
67820
67821/*
67822** Attempt to start a new transaction. A write-transaction
67823** is started if the second argument is nonzero, otherwise a read-
67824** transaction. If the second argument is 2 or more and exclusive
67825** transaction is started, meaning that no other process is allowed
67826** to access the database. A preexisting transaction may not be
67827** upgraded to exclusive by calling this routine a second time - the
67828** exclusivity flag only works for a new transaction.
67829**
67830** A write-transaction must be started before attempting any
67831** changes to the database. None of the following routines
67832** will work unless a transaction is started first:
67833**
67834** sqlite3BtreeCreateTable()
67835** sqlite3BtreeCreateIndex()
67836** sqlite3BtreeClearTable()
67837** sqlite3BtreeDropTable()
67838** sqlite3BtreeInsert()
67839** sqlite3BtreeDelete()
67840** sqlite3BtreeUpdateMeta()
67841**
67842** If an initial attempt to acquire the lock fails because of lock contention
67843** and the database was previously unlocked, then invoke the busy handler
67844** if there is one. But if there was previously a read-lock, do not
67845** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
67846** returned when there is already a read-lock in order to avoid a deadlock.
67847**
67848** Suppose there are two processes A and B. A has a read lock and B has
67849** a reserved lock. B tries to promote to exclusive but is blocked because
67850** of A's read lock. A tries to promote to reserved but is blocked by B.
67851** One or the other of the two processes must give way or there can be
67852** no progress. By returning SQLITE_BUSY and not invoking the busy callback
67853** when A already has a read lock, we encourage A to give up and let B
67854** proceed.
67855*/
67856SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
67857 BtShared *pBt = p->pBt;
67858 Pager *pPager = pBt->pPager;
67859 int rc = SQLITE_OK;
67860
67861 sqlite3BtreeEnter(p);
67862 btreeIntegrity(p);
67863
67864 /* If the btree is already in a write-transaction, or it
67865 ** is already in a read-transaction and a read-transaction
67866 ** is requested, this is a no-op.
67867 */
67868 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
67869 goto trans_begun;
67870 }
67871 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
67872
67873 if( (p->db->flags & SQLITE_ResetDatabase)
67874 && sqlite3PagerIsreadonly(pPager)==0
67875 ){
67876 pBt->btsFlags &= ~BTS_READ_ONLY;
67877 }
67878
67879 /* Write transactions are not possible on a read-only database */
67880 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
67881 rc = SQLITE_READONLY;
67882 goto trans_begun;
67883 }
67884
67885#ifndef SQLITE_OMIT_SHARED_CACHE
67886 {
67887 sqlite3 *pBlock = 0;
67888 /* If another database handle has already opened a write transaction
67889 ** on this shared-btree structure and a second write transaction is
67890 ** requested, return SQLITE_LOCKED.
67891 */
67892 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
67893 || (pBt->btsFlags & BTS_PENDING)!=0
67894 ){
67895 pBlock = pBt->pWriter->db;
67896 }else if( wrflag>1 ){
67897 BtLock *pIter;
67898 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67899 if( pIter->pBtree!=p ){
67900 pBlock = pIter->pBtree->db;
67901 break;
67902 }
67903 }
67904 }
67905 if( pBlock ){
67906 sqlite3ConnectionBlocked(p->db, pBlock);
67907 rc = SQLITE_LOCKED_SHAREDCACHE;
67908 goto trans_begun;
67909 }
67910 }
67911#endif
67912
67913 /* Any read-only or read-write transaction implies a read-lock on
67914 ** page 1. So if some other shared-cache client already has a write-lock
67915 ** on page 1, the transaction cannot be opened. */
67916 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
67917 if( SQLITE_OK!=rc ) goto trans_begun;
67918
67919 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
67920 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
67921 do {
67922 sqlite3PagerWalDb(pPager, p->db);
67923
67924#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67925 /* If transitioning from no transaction directly to a write transaction,
67926 ** block for the WRITER lock first if possible. */
67927 if( pBt->pPage1==0 && wrflag ){
67928 assert( pBt->inTransaction==TRANS_NONE );
67929 rc = sqlite3PagerWalWriteLock(pPager, 1);
67930 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
67931 }
67932#endif
67933
67934 /* Call lockBtree() until either pBt->pPage1 is populated or
67935 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
67936 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
67937 ** reading page 1 it discovers that the page-size of the database
67938 ** file is not pBt->pageSize. In this case lockBtree() will update
67939 ** pBt->pageSize to the page-size of the file on disk.
67940 */
67941 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
67942
67943 if( rc==SQLITE_OK && wrflag ){
67944 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
67945 rc = SQLITE_READONLY;
67946 }else{
67947 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
67948 if( rc==SQLITE_OK ){
67949 rc = newDatabase(pBt);
67950 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
67951 /* if there was no transaction opened when this function was
67952 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
67953 ** code to SQLITE_BUSY. */
67954 rc = SQLITE_BUSY;
67955 }
67956 }
67957 }
67958
67959 if( rc!=SQLITE_OK ){
67960 (void)sqlite3PagerWalWriteLock(pPager, 0);
67961 unlockBtreeIfUnused(pBt);
67962 }
67963 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
67964 btreeInvokeBusyHandler(pBt) );
67965 sqlite3PagerWalDb(pPager, 0);
67966#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67967 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
67968#endif
67969
67970 if( rc==SQLITE_OK ){
67971 if( p->inTrans==TRANS_NONE ){
67972 pBt->nTransaction++;
67973#ifndef SQLITE_OMIT_SHARED_CACHE
67974 if( p->sharable ){
67975 assert( p->lock.pBtree==p && p->lock.iTable==1 );
67976 p->lock.eLock = READ_LOCK;
67977 p->lock.pNext = pBt->pLock;
67978 pBt->pLock = &p->lock;
67979 }
67980#endif
67981 }
67982 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
67983 if( p->inTrans>pBt->inTransaction ){
67984 pBt->inTransaction = p->inTrans;
67985 }
67986 if( wrflag ){
67987 MemPage *pPage1 = pBt->pPage1;
67988#ifndef SQLITE_OMIT_SHARED_CACHE
67989 assert( !pBt->pWriter );
67990 pBt->pWriter = p;
67991 pBt->btsFlags &= ~BTS_EXCLUSIVE;
67992 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
67993#endif
67994
67995 /* If the db-size header field is incorrect (as it may be if an old
67996 ** client has been writing the database file), update it now. Doing
67997 ** this sooner rather than later means the database size can safely
67998 ** re-read the database size from page 1 if a savepoint or transaction
67999 ** rollback occurs within the transaction.
68000 */
68001 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
68002 rc = sqlite3PagerWrite(pPage1->pDbPage);
68003 if( rc==SQLITE_OK ){
68004 put4byte(&pPage1->aData[28], pBt->nPage);
68005 }
68006 }
68007 }
68008 }
68009
68010trans_begun:
68011 if( rc==SQLITE_OK ){
68012 if( pSchemaVersion ){
68013 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
68014 }
68015 if( wrflag ){
68016 /* This call makes sure that the pager has the correct number of
68017 ** open savepoints. If the second parameter is greater than 0 and
68018 ** the sub-journal is not already open, then it will be opened here.
68019 */
68020 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
68021 }
68022 }
68023
68024 btreeIntegrity(p);
68025 sqlite3BtreeLeave(p);
68026 return rc;
68027}
68028
68029#ifndef SQLITE_OMIT_AUTOVACUUM
68030
68031/*
68032** Set the pointer-map entries for all children of page pPage. Also, if
68033** pPage contains cells that point to overflow pages, set the pointer
68034** map entries for the overflow pages as well.
68035*/
68036static int setChildPtrmaps(MemPage *pPage){
68037 int i; /* Counter variable */
68038 int nCell; /* Number of cells in page pPage */
68039 int rc; /* Return code */
68040 BtShared *pBt = pPage->pBt;
68041 Pgno pgno = pPage->pgno;
68042
68043 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68044 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
68045 if( rc!=SQLITE_OK ) return rc;
68046 nCell = pPage->nCell;
68047
68048 for(i=0; i<nCell; i++){
68049 u8 *pCell = findCell(pPage, i);
68050
68051 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
68052
68053 if( !pPage->leaf ){
68054 Pgno childPgno = get4byte(pCell);
68055 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
68056 }
68057 }
68058
68059 if( !pPage->leaf ){
68060 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
68061 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
68062 }
68063
68064 return rc;
68065}
68066
68067/*
68068** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
68069** that it points to iTo. Parameter eType describes the type of pointer to
68070** be modified, as follows:
68071**
68072** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
68073** page of pPage.
68074**
68075** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
68076** page pointed to by one of the cells on pPage.
68077**
68078** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
68079** overflow page in the list.
68080*/
68081static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
68082 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68083 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68084 if( eType==PTRMAP_OVERFLOW2 ){
68085 /* The pointer is always the first 4 bytes of the page in this case. */
68086 if( get4byte(pPage->aData)!=iFrom ){
68087 return SQLITE_CORRUPT_PAGE(pPage);
68088 }
68089 put4byte(pPage->aData, iTo);
68090 }else{
68091 int i;
68092 int nCell;
68093 int rc;
68094
68095 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
68096 if( rc ) return rc;
68097 nCell = pPage->nCell;
68098
68099 for(i=0; i<nCell; i++){
68100 u8 *pCell = findCell(pPage, i);
68101 if( eType==PTRMAP_OVERFLOW1 ){
68102 CellInfo info;
68103 pPage->xParseCell(pPage, pCell, &info);
68104 if( info.nLocal<info.nPayload ){
68105 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
68106 return SQLITE_CORRUPT_PAGE(pPage);
68107 }
68108 if( iFrom==get4byte(pCell+info.nSize-4) ){
68109 put4byte(pCell+info.nSize-4, iTo);
68110 break;
68111 }
68112 }
68113 }else{
68114 if( get4byte(pCell)==iFrom ){
68115 put4byte(pCell, iTo);
68116 break;
68117 }
68118 }
68119 }
68120
68121 if( i==nCell ){
68122 if( eType!=PTRMAP_BTREE ||
68123 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
68124 return SQLITE_CORRUPT_PAGE(pPage);
68125 }
68126 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
68127 }
68128 }
68129 return SQLITE_OK;
68130}
68131
68132
68133/*
68134** Move the open database page pDbPage to location iFreePage in the
68135** database. The pDbPage reference remains valid.
68136**
68137** The isCommit flag indicates that there is no need to remember that
68138** the journal needs to be sync()ed before database page pDbPage->pgno
68139** can be written to. The caller has already promised not to write to that
68140** page.
68141*/
68142static int relocatePage(
68143 BtShared *pBt, /* Btree */
68144 MemPage *pDbPage, /* Open page to move */
68145 u8 eType, /* Pointer map 'type' entry for pDbPage */
68146 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
68147 Pgno iFreePage, /* The location to move pDbPage to */
68148 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
68149){
68150 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
68151 Pgno iDbPage = pDbPage->pgno;
68152 Pager *pPager = pBt->pPager;
68153 int rc;
68154
68155 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
68156 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
68157 assert( sqlite3_mutex_held(pBt->mutex) );
68158 assert( pDbPage->pBt==pBt );
68159 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
68160
68161 /* Move page iDbPage from its current location to page number iFreePage */
68162 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
68163 iDbPage, iFreePage, iPtrPage, eType));
68164 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
68165 if( rc!=SQLITE_OK ){
68166 return rc;
68167 }
68168 pDbPage->pgno = iFreePage;
68169
68170 /* If pDbPage was a btree-page, then it may have child pages and/or cells
68171 ** that point to overflow pages. The pointer map entries for all these
68172 ** pages need to be changed.
68173 **
68174 ** If pDbPage is an overflow page, then the first 4 bytes may store a
68175 ** pointer to a subsequent overflow page. If this is the case, then
68176 ** the pointer map needs to be updated for the subsequent overflow page.
68177 */
68178 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
68179 rc = setChildPtrmaps(pDbPage);
68180 if( rc!=SQLITE_OK ){
68181 return rc;
68182 }
68183 }else{
68184 Pgno nextOvfl = get4byte(pDbPage->aData);
68185 if( nextOvfl!=0 ){
68186 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
68187 if( rc!=SQLITE_OK ){
68188 return rc;
68189 }
68190 }
68191 }
68192
68193 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
68194 ** that it points at iFreePage. Also fix the pointer map entry for
68195 ** iPtrPage.
68196 */
68197 if( eType!=PTRMAP_ROOTPAGE ){
68198 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
68199 if( rc!=SQLITE_OK ){
68200 return rc;
68201 }
68202 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
68203 if( rc!=SQLITE_OK ){
68204 releasePage(pPtrPage);
68205 return rc;
68206 }
68207 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
68208 releasePage(pPtrPage);
68209 if( rc==SQLITE_OK ){
68210 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
68211 }
68212 }
68213 return rc;
68214}
68215
68216/* Forward declaration required by incrVacuumStep(). */
68217static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
68218
68219/*
68220** Perform a single step of an incremental-vacuum. If successful, return
68221** SQLITE_OK. If there is no work to do (and therefore no point in
68222** calling this function again), return SQLITE_DONE. Or, if an error
68223** occurs, return some other error code.
68224**
68225** More specifically, this function attempts to re-organize the database so
68226** that the last page of the file currently in use is no longer in use.
68227**
68228** Parameter nFin is the number of pages that this database would contain
68229** were this function called until it returns SQLITE_DONE.
68230**
68231** If the bCommit parameter is non-zero, this function assumes that the
68232** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
68233** or an error. bCommit is passed true for an auto-vacuum-on-commit
68234** operation, or false for an incremental vacuum.
68235*/
68236static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
68237 Pgno nFreeList; /* Number of pages still on the free-list */
68238 int rc;
68239
68240 assert( sqlite3_mutex_held(pBt->mutex) );
68241 assert( iLastPg>nFin );
68242
68243 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
68244 u8 eType;
68245 Pgno iPtrPage;
68246
68247 nFreeList = get4byte(&pBt->pPage1->aData[36]);
68248 if( nFreeList==0 ){
68249 return SQLITE_DONE;
68250 }
68251
68252 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
68253 if( rc!=SQLITE_OK ){
68254 return rc;
68255 }
68256 if( eType==PTRMAP_ROOTPAGE ){
68257 return SQLITE_CORRUPT_BKPT;
68258 }
68259
68260 if( eType==PTRMAP_FREEPAGE ){
68261 if( bCommit==0 ){
68262 /* Remove the page from the files free-list. This is not required
68263 ** if bCommit is non-zero. In that case, the free-list will be
68264 ** truncated to zero after this function returns, so it doesn't
68265 ** matter if it still contains some garbage entries.
68266 */
68267 Pgno iFreePg;
68268 MemPage *pFreePg;
68269 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
68270 if( rc!=SQLITE_OK ){
68271 return rc;
68272 }
68273 assert( iFreePg==iLastPg );
68274 releasePage(pFreePg);
68275 }
68276 } else {
68277 Pgno iFreePg; /* Index of free page to move pLastPg to */
68278 MemPage *pLastPg;
68279 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
68280 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
68281
68282 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
68283 if( rc!=SQLITE_OK ){
68284 return rc;
68285 }
68286
68287 /* If bCommit is zero, this loop runs exactly once and page pLastPg
68288 ** is swapped with the first free page pulled off the free list.
68289 **
68290 ** On the other hand, if bCommit is greater than zero, then keep
68291 ** looping until a free-page located within the first nFin pages
68292 ** of the file is found.
68293 */
68294 if( bCommit==0 ){
68295 eMode = BTALLOC_LE;
68296 iNear = nFin;
68297 }
68298 do {
68299 MemPage *pFreePg;
68300 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
68301 if( rc!=SQLITE_OK ){
68302 releasePage(pLastPg);
68303 return rc;
68304 }
68305 releasePage(pFreePg);
68306 }while( bCommit && iFreePg>nFin );
68307 assert( iFreePg<iLastPg );
68308
68309 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
68310 releasePage(pLastPg);
68311 if( rc!=SQLITE_OK ){
68312 return rc;
68313 }
68314 }
68315 }
68316
68317 if( bCommit==0 ){
68318 do {
68319 iLastPg--;
68320 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
68321 pBt->bDoTruncate = 1;
68322 pBt->nPage = iLastPg;
68323 }
68324 return SQLITE_OK;
68325}
68326
68327/*
68328** The database opened by the first argument is an auto-vacuum database
68329** nOrig pages in size containing nFree free pages. Return the expected
68330** size of the database in pages following an auto-vacuum operation.
68331*/
68332static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
68333 int nEntry; /* Number of entries on one ptrmap page */
68334 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
68335 Pgno nFin; /* Return value */
68336
68337 nEntry = pBt->usableSize/5;
68338 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
68339 nFin = nOrig - nFree - nPtrmap;
68340 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
68341 nFin--;
68342 }
68343 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
68344 nFin--;
68345 }
68346
68347 return nFin;
68348}
68349
68350/*
68351** A write-transaction must be opened before calling this function.
68352** It performs a single unit of work towards an incremental vacuum.
68353**
68354** If the incremental vacuum is finished after this function has run,
68355** SQLITE_DONE is returned. If it is not finished, but no error occurred,
68356** SQLITE_OK is returned. Otherwise an SQLite error code.
68357*/
68358SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
68359 int rc;
68360 BtShared *pBt = p->pBt;
68361
68362 sqlite3BtreeEnter(p);
68363 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
68364 if( !pBt->autoVacuum ){
68365 rc = SQLITE_DONE;
68366 }else{
68367 Pgno nOrig = btreePagecount(pBt);
68368 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
68369 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
68370
68371 if( nOrig<nFin || nFree>=nOrig ){
68372 rc = SQLITE_CORRUPT_BKPT;
68373 }else if( nFree>0 ){
68374 rc = saveAllCursors(pBt, 0, 0);
68375 if( rc==SQLITE_OK ){
68376 invalidateAllOverflowCache(pBt);
68377 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
68378 }
68379 if( rc==SQLITE_OK ){
68380 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68381 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
68382 }
68383 }else{
68384 rc = SQLITE_DONE;
68385 }
68386 }
68387 sqlite3BtreeLeave(p);
68388 return rc;
68389}
68390
68391/*
68392** This routine is called prior to sqlite3PagerCommit when a transaction
68393** is committed for an auto-vacuum database.
68394**
68395** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
68396** the database file should be truncated to during the commit process.
68397** i.e. the database has been reorganized so that only the first *pnTrunc
68398** pages are in use.
68399*/
68400static int autoVacuumCommit(BtShared *pBt){
68401 int rc = SQLITE_OK;
68402 Pager *pPager = pBt->pPager;
68403 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
68404
68405 assert( sqlite3_mutex_held(pBt->mutex) );
68406 invalidateAllOverflowCache(pBt);
68407 assert(pBt->autoVacuum);
68408 if( !pBt->incrVacuum ){
68409 Pgno nFin; /* Number of pages in database after autovacuuming */
68410 Pgno nFree; /* Number of pages on the freelist initially */
68411 Pgno iFree; /* The next page to be freed */
68412 Pgno nOrig; /* Database size before freeing */
68413
68414 nOrig = btreePagecount(pBt);
68415 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
68416 /* It is not possible to create a database for which the final page
68417 ** is either a pointer-map page or the pending-byte page. If one
68418 ** is encountered, this indicates corruption.
68419 */
68420 return SQLITE_CORRUPT_BKPT;
68421 }
68422
68423 nFree = get4byte(&pBt->pPage1->aData[36]);
68424 nFin = finalDbSize(pBt, nOrig, nFree);
68425 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
68426 if( nFin<nOrig ){
68427 rc = saveAllCursors(pBt, 0, 0);
68428 }
68429 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
68430 rc = incrVacuumStep(pBt, nFin, iFree, 1);
68431 }
68432 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
68433 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68434 put4byte(&pBt->pPage1->aData[32], 0);
68435 put4byte(&pBt->pPage1->aData[36], 0);
68436 put4byte(&pBt->pPage1->aData[28], nFin);
68437 pBt->bDoTruncate = 1;
68438 pBt->nPage = nFin;
68439 }
68440 if( rc!=SQLITE_OK ){
68441 sqlite3PagerRollback(pPager);
68442 }
68443 }
68444
68445 assert( nRef>=sqlite3PagerRefcount(pPager) );
68446 return rc;
68447}
68448
68449#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
68450# define setChildPtrmaps(x) SQLITE_OK
68451#endif
68452
68453/*
68454** This routine does the first phase of a two-phase commit. This routine
68455** causes a rollback journal to be created (if it does not already exist)
68456** and populated with enough information so that if a power loss occurs
68457** the database can be restored to its original state by playing back
68458** the journal. Then the contents of the journal are flushed out to
68459** the disk. After the journal is safely on oxide, the changes to the
68460** database are written into the database file and flushed to oxide.
68461** At the end of this call, the rollback journal still exists on the
68462** disk and we are still holding all locks, so the transaction has not
68463** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
68464** commit process.
68465**
68466** This call is a no-op if no write-transaction is currently active on pBt.
68467**
68468** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
68469** the name of a super-journal file that should be written into the
68470** individual journal file, or is NULL, indicating no super-journal file
68471** (single database transaction).
68472**
68473** When this is called, the super-journal should already have been
68474** created, populated with this journal pointer and synced to disk.
68475**
68476** Once this is routine has returned, the only thing required to commit
68477** the write-transaction for this database file is to delete the journal.
68478*/
68479SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
68480 int rc = SQLITE_OK;
68481 if( p->inTrans==TRANS_WRITE ){
68482 BtShared *pBt = p->pBt;
68483 sqlite3BtreeEnter(p);
68484#ifndef SQLITE_OMIT_AUTOVACUUM
68485 if( pBt->autoVacuum ){
68486 rc = autoVacuumCommit(pBt);
68487 if( rc!=SQLITE_OK ){
68488 sqlite3BtreeLeave(p);
68489 return rc;
68490 }
68491 }
68492 if( pBt->bDoTruncate ){
68493 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
68494 }
68495#endif
68496 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
68497 sqlite3BtreeLeave(p);
68498 }
68499 return rc;
68500}
68501
68502/*
68503** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
68504** at the conclusion of a transaction.
68505*/
68506static void btreeEndTransaction(Btree *p){
68507 BtShared *pBt = p->pBt;
68508 sqlite3 *db = p->db;
68509 assert( sqlite3BtreeHoldsMutex(p) );
68510
68511#ifndef SQLITE_OMIT_AUTOVACUUM
68512 pBt->bDoTruncate = 0;
68513#endif
68514 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
68515 /* If there are other active statements that belong to this database
68516 ** handle, downgrade to a read-only transaction. The other statements
68517 ** may still be reading from the database. */
68518 downgradeAllSharedCacheTableLocks(p);
68519 p->inTrans = TRANS_READ;
68520 }else{
68521 /* If the handle had any kind of transaction open, decrement the
68522 ** transaction count of the shared btree. If the transaction count
68523 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
68524 ** call below will unlock the pager. */
68525 if( p->inTrans!=TRANS_NONE ){
68526 clearAllSharedCacheTableLocks(p);
68527 pBt->nTransaction--;
68528 if( 0==pBt->nTransaction ){
68529 pBt->inTransaction = TRANS_NONE;
68530 }
68531 }
68532
68533 /* Set the current transaction state to TRANS_NONE and unlock the
68534 ** pager if this call closed the only read or write transaction. */
68535 p->inTrans = TRANS_NONE;
68536 unlockBtreeIfUnused(pBt);
68537 }
68538
68539 btreeIntegrity(p);
68540}
68541
68542/*
68543** Commit the transaction currently in progress.
68544**
68545** This routine implements the second phase of a 2-phase commit. The
68546** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
68547** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
68548** routine did all the work of writing information out to disk and flushing the
68549** contents so that they are written onto the disk platter. All this
68550** routine has to do is delete or truncate or zero the header in the
68551** the rollback journal (which causes the transaction to commit) and
68552** drop locks.
68553**
68554** Normally, if an error occurs while the pager layer is attempting to
68555** finalize the underlying journal file, this function returns an error and
68556** the upper layer will attempt a rollback. However, if the second argument
68557** is non-zero then this b-tree transaction is part of a multi-file
68558** transaction. In this case, the transaction has already been committed
68559** (by deleting a super-journal file) and the caller will ignore this
68560** functions return code. So, even if an error occurs in the pager layer,
68561** reset the b-tree objects internal state to indicate that the write
68562** transaction has been closed. This is quite safe, as the pager will have
68563** transitioned to the error state.
68564**
68565** This will release the write lock on the database file. If there
68566** are no active cursors, it also releases the read lock.
68567*/
68568SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
68569
68570 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
68571 sqlite3BtreeEnter(p);
68572 btreeIntegrity(p);
68573
68574 /* If the handle has a write-transaction open, commit the shared-btrees
68575 ** transaction and set the shared state to TRANS_READ.
68576 */
68577 if( p->inTrans==TRANS_WRITE ){
68578 int rc;
68579 BtShared *pBt = p->pBt;
68580 assert( pBt->inTransaction==TRANS_WRITE );
68581 assert( pBt->nTransaction>0 );
68582 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
68583 if( rc!=SQLITE_OK && bCleanup==0 ){
68584 sqlite3BtreeLeave(p);
68585 return rc;
68586 }
68587 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
68588 pBt->inTransaction = TRANS_READ;
68589 btreeClearHasContent(pBt);
68590 }
68591
68592 btreeEndTransaction(p);
68593 sqlite3BtreeLeave(p);
68594 return SQLITE_OK;
68595}
68596
68597/*
68598** Do both phases of a commit.
68599*/
68600SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
68601 int rc;
68602 sqlite3BtreeEnter(p);
68603 rc = sqlite3BtreeCommitPhaseOne(p, 0);
68604 if( rc==SQLITE_OK ){
68605 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
68606 }
68607 sqlite3BtreeLeave(p);
68608 return rc;
68609}
68610
68611/*
68612** This routine sets the state to CURSOR_FAULT and the error
68613** code to errCode for every cursor on any BtShared that pBtree
68614** references. Or if the writeOnly flag is set to 1, then only
68615** trip write cursors and leave read cursors unchanged.
68616**
68617** Every cursor is a candidate to be tripped, including cursors
68618** that belong to other database connections that happen to be
68619** sharing the cache with pBtree.
68620**
68621** This routine gets called when a rollback occurs. If the writeOnly
68622** flag is true, then only write-cursors need be tripped - read-only
68623** cursors save their current positions so that they may continue
68624** following the rollback. Or, if writeOnly is false, all cursors are
68625** tripped. In general, writeOnly is false if the transaction being
68626** rolled back modified the database schema. In this case b-tree root
68627** pages may be moved or deleted from the database altogether, making
68628** it unsafe for read cursors to continue.
68629**
68630** If the writeOnly flag is true and an error is encountered while
68631** saving the current position of a read-only cursor, all cursors,
68632** including all read-cursors are tripped.
68633**
68634** SQLITE_OK is returned if successful, or if an error occurs while
68635** saving a cursor position, an SQLite error code.
68636*/
68637SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
68638 BtCursor *p;
68639 int rc = SQLITE_OK;
68640
68641 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
68642 if( pBtree ){
68643 sqlite3BtreeEnter(pBtree);
68644 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
68645 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
68646 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
68647 rc = saveCursorPosition(p);
68648 if( rc!=SQLITE_OK ){
68649 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
68650 break;
68651 }
68652 }
68653 }else{
68654 sqlite3BtreeClearCursor(p);
68655 p->eState = CURSOR_FAULT;
68656 p->skipNext = errCode;
68657 }
68658 btreeReleaseAllCursorPages(p);
68659 }
68660 sqlite3BtreeLeave(pBtree);
68661 }
68662 return rc;
68663}
68664
68665/*
68666** Set the pBt->nPage field correctly, according to the current
68667** state of the database. Assume pBt->pPage1 is valid.
68668*/
68669static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
68670 int nPage = get4byte(&pPage1->aData[28]);
68671 testcase( nPage==0 );
68672 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
68673 testcase( pBt->nPage!=nPage );
68674 pBt->nPage = nPage;
68675}
68676
68677/*
68678** Rollback the transaction in progress.
68679**
68680** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
68681** Only write cursors are tripped if writeOnly is true but all cursors are
68682** tripped if writeOnly is false. Any attempt to use
68683** a tripped cursor will result in an error.
68684**
68685** This will release the write lock on the database file. If there
68686** are no active cursors, it also releases the read lock.
68687*/
68688SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
68689 int rc;
68690 BtShared *pBt = p->pBt;
68691 MemPage *pPage1;
68692
68693 assert( writeOnly==1 || writeOnly==0 );
68694 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
68695 sqlite3BtreeEnter(p);
68696 if( tripCode==SQLITE_OK ){
68697 rc = tripCode = saveAllCursors(pBt, 0, 0);
68698 if( rc ) writeOnly = 0;
68699 }else{
68700 rc = SQLITE_OK;
68701 }
68702 if( tripCode ){
68703 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
68704 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
68705 if( rc2!=SQLITE_OK ) rc = rc2;
68706 }
68707 btreeIntegrity(p);
68708
68709 if( p->inTrans==TRANS_WRITE ){
68710 int rc2;
68711
68712 assert( TRANS_WRITE==pBt->inTransaction );
68713 rc2 = sqlite3PagerRollback(pBt->pPager);
68714 if( rc2!=SQLITE_OK ){
68715 rc = rc2;
68716 }
68717
68718 /* The rollback may have destroyed the pPage1->aData value. So
68719 ** call btreeGetPage() on page 1 again to make
68720 ** sure pPage1->aData is set correctly. */
68721 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
68722 btreeSetNPage(pBt, pPage1);
68723 releasePageOne(pPage1);
68724 }
68725 assert( countValidCursors(pBt, 1)==0 );
68726 pBt->inTransaction = TRANS_READ;
68727 btreeClearHasContent(pBt);
68728 }
68729
68730 btreeEndTransaction(p);
68731 sqlite3BtreeLeave(p);
68732 return rc;
68733}
68734
68735/*
68736** Start a statement subtransaction. The subtransaction can be rolled
68737** back independently of the main transaction. You must start a transaction
68738** before starting a subtransaction. The subtransaction is ended automatically
68739** if the main transaction commits or rolls back.
68740**
68741** Statement subtransactions are used around individual SQL statements
68742** that are contained within a BEGIN...COMMIT block. If a constraint
68743** error occurs within the statement, the effect of that one statement
68744** can be rolled back without having to rollback the entire transaction.
68745**
68746** A statement sub-transaction is implemented as an anonymous savepoint. The
68747** value passed as the second parameter is the total number of savepoints,
68748** including the new anonymous savepoint, open on the B-Tree. i.e. if there
68749** are no active savepoints and no other statement-transactions open,
68750** iStatement is 1. This anonymous savepoint can be released or rolled back
68751** using the sqlite3BtreeSavepoint() function.
68752*/
68753SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
68754 int rc;
68755 BtShared *pBt = p->pBt;
68756 sqlite3BtreeEnter(p);
68757 assert( p->inTrans==TRANS_WRITE );
68758 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
68759 assert( iStatement>0 );
68760 assert( iStatement>p->db->nSavepoint );
68761 assert( pBt->inTransaction==TRANS_WRITE );
68762 /* At the pager level, a statement transaction is a savepoint with
68763 ** an index greater than all savepoints created explicitly using
68764 ** SQL statements. It is illegal to open, release or rollback any
68765 ** such savepoints while the statement transaction savepoint is active.
68766 */
68767 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
68768 sqlite3BtreeLeave(p);
68769 return rc;
68770}
68771
68772/*
68773** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
68774** or SAVEPOINT_RELEASE. This function either releases or rolls back the
68775** savepoint identified by parameter iSavepoint, depending on the value
68776** of op.
68777**
68778** Normally, iSavepoint is greater than or equal to zero. However, if op is
68779** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
68780** contents of the entire transaction are rolled back. This is different
68781** from a normal transaction rollback, as no locks are released and the
68782** transaction remains open.
68783*/
68784SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
68785 int rc = SQLITE_OK;
68786 if( p && p->inTrans==TRANS_WRITE ){
68787 BtShared *pBt = p->pBt;
68788 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
68789 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
68790 sqlite3BtreeEnter(p);
68791 if( op==SAVEPOINT_ROLLBACK ){
68792 rc = saveAllCursors(pBt, 0, 0);
68793 }
68794 if( rc==SQLITE_OK ){
68795 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
68796 }
68797 if( rc==SQLITE_OK ){
68798 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
68799 pBt->nPage = 0;
68800 }
68801 rc = newDatabase(pBt);
68802 btreeSetNPage(pBt, pBt->pPage1);
68803
68804 /* pBt->nPage might be zero if the database was corrupt when
68805 ** the transaction was started. Otherwise, it must be at least 1. */
68806 assert( CORRUPT_DB || pBt->nPage>0 );
68807 }
68808 sqlite3BtreeLeave(p);
68809 }
68810 return rc;
68811}
68812
68813/*
68814** Create a new cursor for the BTree whose root is on the page
68815** iTable. If a read-only cursor is requested, it is assumed that
68816** the caller already has at least a read-only transaction open
68817** on the database already. If a write-cursor is requested, then
68818** the caller is assumed to have an open write transaction.
68819**
68820** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
68821** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
68822** can be used for reading or for writing if other conditions for writing
68823** are also met. These are the conditions that must be met in order
68824** for writing to be allowed:
68825**
68826** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
68827**
68828** 2: Other database connections that share the same pager cache
68829** but which are not in the READ_UNCOMMITTED state may not have
68830** cursors open with wrFlag==0 on the same table. Otherwise
68831** the changes made by this write cursor would be visible to
68832** the read cursors in the other database connection.
68833**
68834** 3: The database must be writable (not on read-only media)
68835**
68836** 4: There must be an active transaction.
68837**
68838** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
68839** is set. If FORDELETE is set, that is a hint to the implementation that
68840** this cursor will only be used to seek to and delete entries of an index
68841** as part of a larger DELETE statement. The FORDELETE hint is not used by
68842** this implementation. But in a hypothetical alternative storage engine
68843** in which index entries are automatically deleted when corresponding table
68844** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
68845** operations on this cursor can be no-ops and all READ operations can
68846** return a null row (2-bytes: 0x01 0x00).
68847**
68848** No checking is done to make sure that page iTable really is the
68849** root page of a b-tree. If it is not, then the cursor acquired
68850** will not work correctly.
68851**
68852** It is assumed that the sqlite3BtreeCursorZero() has been called
68853** on pCur to initialize the memory space prior to invoking this routine.
68854*/
68855static int btreeCursor(
68856 Btree *p, /* The btree */
68857 Pgno iTable, /* Root page of table to open */
68858 int wrFlag, /* 1 to write. 0 read-only */
68859 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
68860 BtCursor *pCur /* Space for new cursor */
68861){
68862 BtShared *pBt = p->pBt; /* Shared b-tree handle */
68863 BtCursor *pX; /* Looping over other all cursors */
68864
68865 assert( sqlite3BtreeHoldsMutex(p) );
68866 assert( wrFlag==0
68867 || wrFlag==BTREE_WRCSR
68868 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
68869 );
68870
68871 /* The following assert statements verify that if this is a sharable
68872 ** b-tree database, the connection is holding the required table locks,
68873 ** and that no other connection has any open cursor that conflicts with
68874 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
68875 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
68876 || iTable<1 );
68877 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
68878
68879 /* Assert that the caller has opened the required transaction. */
68880 assert( p->inTrans>TRANS_NONE );
68881 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
68882 assert( pBt->pPage1 && pBt->pPage1->aData );
68883 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
68884
68885 if( wrFlag ){
68886 allocateTempSpace(pBt);
68887 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
68888 }
68889 if( iTable<=1 ){
68890 if( iTable<1 ){
68891 return SQLITE_CORRUPT_BKPT;
68892 }else if( btreePagecount(pBt)==0 ){
68893 assert( wrFlag==0 );
68894 iTable = 0;
68895 }
68896 }
68897
68898 /* Now that no other errors can occur, finish filling in the BtCursor
68899 ** variables and link the cursor into the BtShared list. */
68900 pCur->pgnoRoot = iTable;
68901 pCur->iPage = -1;
68902 pCur->pKeyInfo = pKeyInfo;
68903 pCur->pBtree = p;
68904 pCur->pBt = pBt;
68905 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
68906 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
68907 /* If there are two or more cursors on the same btree, then all such
68908 ** cursors *must* have the BTCF_Multiple flag set. */
68909 for(pX=pBt->pCursor; pX; pX=pX->pNext){
68910 if( pX->pgnoRoot==iTable ){
68911 pX->curFlags |= BTCF_Multiple;
68912 pCur->curFlags |= BTCF_Multiple;
68913 }
68914 }
68915 pCur->pNext = pBt->pCursor;
68916 pBt->pCursor = pCur;
68917 pCur->eState = CURSOR_INVALID;
68918 return SQLITE_OK;
68919}
68920static int btreeCursorWithLock(
68921 Btree *p, /* The btree */
68922 Pgno iTable, /* Root page of table to open */
68923 int wrFlag, /* 1 to write. 0 read-only */
68924 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
68925 BtCursor *pCur /* Space for new cursor */
68926){
68927 int rc;
68928 sqlite3BtreeEnter(p);
68929 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
68930 sqlite3BtreeLeave(p);
68931 return rc;
68932}
68933SQLITE_PRIVATE int sqlite3BtreeCursor(
68934 Btree *p, /* The btree */
68935 Pgno iTable, /* Root page of table to open */
68936 int wrFlag, /* 1 to write. 0 read-only */
68937 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
68938 BtCursor *pCur /* Write new cursor here */
68939){
68940 if( p->sharable ){
68941 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
68942 }else{
68943 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
68944 }
68945}
68946
68947/*
68948** Return the size of a BtCursor object in bytes.
68949**
68950** This interfaces is needed so that users of cursors can preallocate
68951** sufficient storage to hold a cursor. The BtCursor object is opaque
68952** to users so they cannot do the sizeof() themselves - they must call
68953** this routine.
68954*/
68955SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
68956 return ROUND8(sizeof(BtCursor));
68957}
68958
68959/*
68960** Initialize memory that will be converted into a BtCursor object.
68961**
68962** The simple approach here would be to memset() the entire object
68963** to zero. But it turns out that the apPage[] and aiIdx[] arrays
68964** do not need to be zeroed and they are large, so we can save a lot
68965** of run-time by skipping the initialization of those elements.
68966*/
68967SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
68968 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
68969}
68970
68971/*
68972** Close a cursor. The read lock on the database file is released
68973** when the last cursor is closed.
68974*/
68975SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
68976 Btree *pBtree = pCur->pBtree;
68977 if( pBtree ){
68978 BtShared *pBt = pCur->pBt;
68979 sqlite3BtreeEnter(pBtree);
68980 assert( pBt->pCursor!=0 );
68981 if( pBt->pCursor==pCur ){
68982 pBt->pCursor = pCur->pNext;
68983 }else{
68984 BtCursor *pPrev = pBt->pCursor;
68985 do{
68986 if( pPrev->pNext==pCur ){
68987 pPrev->pNext = pCur->pNext;
68988 break;
68989 }
68990 pPrev = pPrev->pNext;
68991 }while( ALWAYS(pPrev) );
68992 }
68993 btreeReleaseAllCursorPages(pCur);
68994 unlockBtreeIfUnused(pBt);
68995 sqlite3_free(pCur->aOverflow);
68996 sqlite3_free(pCur->pKey);
68997 sqlite3BtreeLeave(pBtree);
68998 pCur->pBtree = 0;
68999 }
69000 return SQLITE_OK;
69001}
69002
69003/*
69004** Make sure the BtCursor* given in the argument has a valid
69005** BtCursor.info structure. If it is not already valid, call
69006** btreeParseCell() to fill it in.
69007**
69008** BtCursor.info is a cache of the information in the current cell.
69009** Using this cache reduces the number of calls to btreeParseCell().
69010*/
69011#ifndef NDEBUG
69012 static int cellInfoEqual(CellInfo *a, CellInfo *b){
69013 if( a->nKey!=b->nKey ) return 0;
69014 if( a->pPayload!=b->pPayload ) return 0;
69015 if( a->nPayload!=b->nPayload ) return 0;
69016 if( a->nLocal!=b->nLocal ) return 0;
69017 if( a->nSize!=b->nSize ) return 0;
69018 return 1;
69019 }
69020 static void assertCellInfo(BtCursor *pCur){
69021 CellInfo info;
69022 memset(&info, 0, sizeof(info));
69023 btreeParseCell(pCur->pPage, pCur->ix, &info);
69024 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
69025 }
69026#else
69027 #define assertCellInfo(x)
69028#endif
69029static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
69030 if( pCur->info.nSize==0 ){
69031 pCur->curFlags |= BTCF_ValidNKey;
69032 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
69033 }else{
69034 assertCellInfo(pCur);
69035 }
69036}
69037
69038#ifndef NDEBUG /* The next routine used only within assert() statements */
69039/*
69040** Return true if the given BtCursor is valid. A valid cursor is one
69041** that is currently pointing to a row in a (non-empty) table.
69042** This is a verification routine is used only within assert() statements.
69043*/
69044SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
69045 return pCur && pCur->eState==CURSOR_VALID;
69046}
69047#endif /* NDEBUG */
69048SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
69049 assert( pCur!=0 );
69050 return pCur->eState==CURSOR_VALID;
69051}
69052
69053/*
69054** Return the value of the integer key or "rowid" for a table btree.
69055** This routine is only valid for a cursor that is pointing into a
69056** ordinary table btree. If the cursor points to an index btree or
69057** is invalid, the result of this routine is undefined.
69058*/
69059SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
69060 assert( cursorHoldsMutex(pCur) );
69061 assert( pCur->eState==CURSOR_VALID );
69062 assert( pCur->curIntKey );
69063 getCellInfo(pCur);
69064 return pCur->info.nKey;
69065}
69066
69067/*
69068** Pin or unpin a cursor.
69069*/
69070SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor *pCur){
69071 assert( (pCur->curFlags & BTCF_Pinned)==0 );
69072 pCur->curFlags |= BTCF_Pinned;
69073}
69074SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor *pCur){
69075 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
69076 pCur->curFlags &= ~BTCF_Pinned;
69077}
69078
69079#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
69080/*
69081** Return the offset into the database file for the start of the
69082** payload to which the cursor is pointing.
69083*/
69084SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
69085 assert( cursorHoldsMutex(pCur) );
69086 assert( pCur->eState==CURSOR_VALID );
69087 getCellInfo(pCur);
69088 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
69089 (i64)(pCur->info.pPayload - pCur->pPage->aData);
69090}
69091#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
69092
69093/*
69094** Return the number of bytes of payload for the entry that pCur is
69095** currently pointing to. For table btrees, this will be the amount
69096** of data. For index btrees, this will be the size of the key.
69097**
69098** The caller must guarantee that the cursor is pointing to a non-NULL
69099** valid entry. In other words, the calling procedure must guarantee
69100** that the cursor has Cursor.eState==CURSOR_VALID.
69101*/
69102SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
69103 assert( cursorHoldsMutex(pCur) );
69104 assert( pCur->eState==CURSOR_VALID );
69105 getCellInfo(pCur);
69106 return pCur->info.nPayload;
69107}
69108
69109/*
69110** Return an upper bound on the size of any record for the table
69111** that the cursor is pointing into.
69112**
69113** This is an optimization. Everything will still work if this
69114** routine always returns 2147483647 (which is the largest record
69115** that SQLite can handle) or more. But returning a smaller value might
69116** prevent large memory allocations when trying to interpret a
69117** corrupt datrabase.
69118**
69119** The current implementation merely returns the size of the underlying
69120** database file.
69121*/
69122SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
69123 assert( cursorHoldsMutex(pCur) );
69124 assert( pCur->eState==CURSOR_VALID );
69125 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
69126}
69127
69128/*
69129** Given the page number of an overflow page in the database (parameter
69130** ovfl), this function finds the page number of the next page in the
69131** linked list of overflow pages. If possible, it uses the auto-vacuum
69132** pointer-map data instead of reading the content of page ovfl to do so.
69133**
69134** If an error occurs an SQLite error code is returned. Otherwise:
69135**
69136** The page number of the next overflow page in the linked list is
69137** written to *pPgnoNext. If page ovfl is the last page in its linked
69138** list, *pPgnoNext is set to zero.
69139**
69140** If ppPage is not NULL, and a reference to the MemPage object corresponding
69141** to page number pOvfl was obtained, then *ppPage is set to point to that
69142** reference. It is the responsibility of the caller to call releasePage()
69143** on *ppPage to free the reference. In no reference was obtained (because
69144** the pointer-map was used to obtain the value for *pPgnoNext), then
69145** *ppPage is set to zero.
69146*/
69147static int getOverflowPage(
69148 BtShared *pBt, /* The database file */
69149 Pgno ovfl, /* Current overflow page number */
69150 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
69151 Pgno *pPgnoNext /* OUT: Next overflow page number */
69152){
69153 Pgno next = 0;
69154 MemPage *pPage = 0;
69155 int rc = SQLITE_OK;
69156
69157 assert( sqlite3_mutex_held(pBt->mutex) );
69158 assert(pPgnoNext);
69159
69160#ifndef SQLITE_OMIT_AUTOVACUUM
69161 /* Try to find the next page in the overflow list using the
69162 ** autovacuum pointer-map pages. Guess that the next page in
69163 ** the overflow list is page number (ovfl+1). If that guess turns
69164 ** out to be wrong, fall back to loading the data of page
69165 ** number ovfl to determine the next page number.
69166 */
69167 if( pBt->autoVacuum ){
69168 Pgno pgno;
69169 Pgno iGuess = ovfl+1;
69170 u8 eType;
69171
69172 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
69173 iGuess++;
69174 }
69175
69176 if( iGuess<=btreePagecount(pBt) ){
69177 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
69178 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
69179 next = iGuess;
69180 rc = SQLITE_DONE;
69181 }
69182 }
69183 }
69184#endif
69185
69186 assert( next==0 || rc==SQLITE_DONE );
69187 if( rc==SQLITE_OK ){
69188 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
69189 assert( rc==SQLITE_OK || pPage==0 );
69190 if( rc==SQLITE_OK ){
69191 next = get4byte(pPage->aData);
69192 }
69193 }
69194
69195 *pPgnoNext = next;
69196 if( ppPage ){
69197 *ppPage = pPage;
69198 }else{
69199 releasePage(pPage);
69200 }
69201 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
69202}
69203
69204/*
69205** Copy data from a buffer to a page, or from a page to a buffer.
69206**
69207** pPayload is a pointer to data stored on database page pDbPage.
69208** If argument eOp is false, then nByte bytes of data are copied
69209** from pPayload to the buffer pointed at by pBuf. If eOp is true,
69210** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
69211** of data are copied from the buffer pBuf to pPayload.
69212**
69213** SQLITE_OK is returned on success, otherwise an error code.
69214*/
69215static int copyPayload(
69216 void *pPayload, /* Pointer to page data */
69217 void *pBuf, /* Pointer to buffer */
69218 int nByte, /* Number of bytes to copy */
69219 int eOp, /* 0 -> copy from page, 1 -> copy to page */
69220 DbPage *pDbPage /* Page containing pPayload */
69221){
69222 if( eOp ){
69223 /* Copy data from buffer to page (a write operation) */
69224 int rc = sqlite3PagerWrite(pDbPage);
69225 if( rc!=SQLITE_OK ){
69226 return rc;
69227 }
69228 memcpy(pPayload, pBuf, nByte);
69229 }else{
69230 /* Copy data from page to buffer (a read operation) */
69231 memcpy(pBuf, pPayload, nByte);
69232 }
69233 return SQLITE_OK;
69234}
69235
69236/*
69237** This function is used to read or overwrite payload information
69238** for the entry that the pCur cursor is pointing to. The eOp
69239** argument is interpreted as follows:
69240**
69241** 0: The operation is a read. Populate the overflow cache.
69242** 1: The operation is a write. Populate the overflow cache.
69243**
69244** A total of "amt" bytes are read or written beginning at "offset".
69245** Data is read to or from the buffer pBuf.
69246**
69247** The content being read or written might appear on the main page
69248** or be scattered out on multiple overflow pages.
69249**
69250** If the current cursor entry uses one or more overflow pages
69251** this function may allocate space for and lazily populate
69252** the overflow page-list cache array (BtCursor.aOverflow).
69253** Subsequent calls use this cache to make seeking to the supplied offset
69254** more efficient.
69255**
69256** Once an overflow page-list cache has been allocated, it must be
69257** invalidated if some other cursor writes to the same table, or if
69258** the cursor is moved to a different row. Additionally, in auto-vacuum
69259** mode, the following events may invalidate an overflow page-list cache.
69260**
69261** * An incremental vacuum,
69262** * A commit in auto_vacuum="full" mode,
69263** * Creating a table (may require moving an overflow page).
69264*/
69265static int accessPayload(
69266 BtCursor *pCur, /* Cursor pointing to entry to read from */
69267 u32 offset, /* Begin reading this far into payload */
69268 u32 amt, /* Read this many bytes */
69269 unsigned char *pBuf, /* Write the bytes into this buffer */
69270 int eOp /* zero to read. non-zero to write. */
69271){
69272 unsigned char *aPayload;
69273 int rc = SQLITE_OK;
69274 int iIdx = 0;
69275 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
69276 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
69277#ifdef SQLITE_DIRECT_OVERFLOW_READ
69278 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
69279#endif
69280
69281 assert( pPage );
69282 assert( eOp==0 || eOp==1 );
69283 assert( pCur->eState==CURSOR_VALID );
69284 assert( pCur->ix<pPage->nCell );
69285 assert( cursorHoldsMutex(pCur) );
69286
69287 getCellInfo(pCur);
69288 aPayload = pCur->info.pPayload;
69289 assert( offset+amt <= pCur->info.nPayload );
69290
69291 assert( aPayload > pPage->aData );
69292 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
69293 /* Trying to read or write past the end of the data is an error. The
69294 ** conditional above is really:
69295 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
69296 ** but is recast into its current form to avoid integer overflow problems
69297 */
69298 return SQLITE_CORRUPT_PAGE(pPage);
69299 }
69300
69301 /* Check if data must be read/written to/from the btree page itself. */
69302 if( offset<pCur->info.nLocal ){
69303 int a = amt;
69304 if( a+offset>pCur->info.nLocal ){
69305 a = pCur->info.nLocal - offset;
69306 }
69307 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
69308 offset = 0;
69309 pBuf += a;
69310 amt -= a;
69311 }else{
69312 offset -= pCur->info.nLocal;
69313 }
69314
69315
69316 if( rc==SQLITE_OK && amt>0 ){
69317 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
69318 Pgno nextPage;
69319
69320 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
69321
69322 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
69323 **
69324 ** The aOverflow[] array is sized at one entry for each overflow page
69325 ** in the overflow chain. The page number of the first overflow page is
69326 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
69327 ** means "not yet known" (the cache is lazily populated).
69328 */
69329 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
69330 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
69331 if( pCur->aOverflow==0
69332 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
69333 ){
69334 Pgno *aNew = (Pgno*)sqlite3Realloc(
69335 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
69336 );
69337 if( aNew==0 ){
69338 return SQLITE_NOMEM_BKPT;
69339 }else{
69340 pCur->aOverflow = aNew;
69341 }
69342 }
69343 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
69344 pCur->curFlags |= BTCF_ValidOvfl;
69345 }else{
69346 /* If the overflow page-list cache has been allocated and the
69347 ** entry for the first required overflow page is valid, skip
69348 ** directly to it.
69349 */
69350 if( pCur->aOverflow[offset/ovflSize] ){
69351 iIdx = (offset/ovflSize);
69352 nextPage = pCur->aOverflow[iIdx];
69353 offset = (offset%ovflSize);
69354 }
69355 }
69356
69357 assert( rc==SQLITE_OK && amt>0 );
69358 while( nextPage ){
69359 /* If required, populate the overflow page-list cache. */
69360 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
69361 assert( pCur->aOverflow[iIdx]==0
69362 || pCur->aOverflow[iIdx]==nextPage
69363 || CORRUPT_DB );
69364 pCur->aOverflow[iIdx] = nextPage;
69365
69366 if( offset>=ovflSize ){
69367 /* The only reason to read this page is to obtain the page
69368 ** number for the next page in the overflow chain. The page
69369 ** data is not required. So first try to lookup the overflow
69370 ** page-list cache, if any, then fall back to the getOverflowPage()
69371 ** function.
69372 */
69373 assert( pCur->curFlags & BTCF_ValidOvfl );
69374 assert( pCur->pBtree->db==pBt->db );
69375 if( pCur->aOverflow[iIdx+1] ){
69376 nextPage = pCur->aOverflow[iIdx+1];
69377 }else{
69378 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
69379 }
69380 offset -= ovflSize;
69381 }else{
69382 /* Need to read this page properly. It contains some of the
69383 ** range of data that is being read (eOp==0) or written (eOp!=0).
69384 */
69385 int a = amt;
69386 if( a + offset > ovflSize ){
69387 a = ovflSize - offset;
69388 }
69389
69390#ifdef SQLITE_DIRECT_OVERFLOW_READ
69391 /* If all the following are true:
69392 **
69393 ** 1) this is a read operation, and
69394 ** 2) data is required from the start of this overflow page, and
69395 ** 3) there are no dirty pages in the page-cache
69396 ** 4) the database is file-backed, and
69397 ** 5) the page is not in the WAL file
69398 ** 6) at least 4 bytes have already been read into the output buffer
69399 **
69400 ** then data can be read directly from the database file into the
69401 ** output buffer, bypassing the page-cache altogether. This speeds
69402 ** up loading large records that span many overflow pages.
69403 */
69404 if( eOp==0 /* (1) */
69405 && offset==0 /* (2) */
69406 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
69407 && &pBuf[-4]>=pBufStart /* (6) */
69408 ){
69409 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
69410 u8 aSave[4];
69411 u8 *aWrite = &pBuf[-4];
69412 assert( aWrite>=pBufStart ); /* due to (6) */
69413 memcpy(aSave, aWrite, 4);
69414 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
69415 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
69416 nextPage = get4byte(aWrite);
69417 memcpy(aWrite, aSave, 4);
69418 }else
69419#endif
69420
69421 {
69422 DbPage *pDbPage;
69423 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
69424 (eOp==0 ? PAGER_GET_READONLY : 0)
69425 );
69426 if( rc==SQLITE_OK ){
69427 aPayload = sqlite3PagerGetData(pDbPage);
69428 nextPage = get4byte(aPayload);
69429 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
69430 sqlite3PagerUnref(pDbPage);
69431 offset = 0;
69432 }
69433 }
69434 amt -= a;
69435 if( amt==0 ) return rc;
69436 pBuf += a;
69437 }
69438 if( rc ) break;
69439 iIdx++;
69440 }
69441 }
69442
69443 if( rc==SQLITE_OK && amt>0 ){
69444 /* Overflow chain ends prematurely */
69445 return SQLITE_CORRUPT_PAGE(pPage);
69446 }
69447 return rc;
69448}
69449
69450/*
69451** Read part of the payload for the row at which that cursor pCur is currently
69452** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
69453** begins at "offset".
69454**
69455** pCur can be pointing to either a table or an index b-tree.
69456** If pointing to a table btree, then the content section is read. If
69457** pCur is pointing to an index b-tree then the key section is read.
69458**
69459** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
69460** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
69461** cursor might be invalid or might need to be restored before being read.
69462**
69463** Return SQLITE_OK on success or an error code if anything goes
69464** wrong. An error is returned if "offset+amt" is larger than
69465** the available payload.
69466*/
69467SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
69468 assert( cursorHoldsMutex(pCur) );
69469 assert( pCur->eState==CURSOR_VALID );
69470 assert( pCur->iPage>=0 && pCur->pPage );
69471 assert( pCur->ix<pCur->pPage->nCell );
69472 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
69473}
69474
69475/*
69476** This variant of sqlite3BtreePayload() works even if the cursor has not
69477** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
69478** interface.
69479*/
69480#ifndef SQLITE_OMIT_INCRBLOB
69481static SQLITE_NOINLINE int accessPayloadChecked(
69482 BtCursor *pCur,
69483 u32 offset,
69484 u32 amt,
69485 void *pBuf
69486){
69487 int rc;
69488 if ( pCur->eState==CURSOR_INVALID ){
69489 return SQLITE_ABORT;
69490 }
69491 assert( cursorOwnsBtShared(pCur) );
69492 rc = btreeRestoreCursorPosition(pCur);
69493 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
69494}
69495SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
69496 if( pCur->eState==CURSOR_VALID ){
69497 assert( cursorOwnsBtShared(pCur) );
69498 return accessPayload(pCur, offset, amt, pBuf, 0);
69499 }else{
69500 return accessPayloadChecked(pCur, offset, amt, pBuf);
69501 }
69502}
69503#endif /* SQLITE_OMIT_INCRBLOB */
69504
69505/*
69506** Return a pointer to payload information from the entry that the
69507** pCur cursor is pointing to. The pointer is to the beginning of
69508** the key if index btrees (pPage->intKey==0) and is the data for
69509** table btrees (pPage->intKey==1). The number of bytes of available
69510** key/data is written into *pAmt. If *pAmt==0, then the value
69511** returned will not be a valid pointer.
69512**
69513** This routine is an optimization. It is common for the entire key
69514** and data to fit on the local page and for there to be no overflow
69515** pages. When that is so, this routine can be used to access the
69516** key and data without making a copy. If the key and/or data spills
69517** onto overflow pages, then accessPayload() must be used to reassemble
69518** the key/data and copy it into a preallocated buffer.
69519**
69520** The pointer returned by this routine looks directly into the cached
69521** page of the database. The data might change or move the next time
69522** any btree routine is called.
69523*/
69524static const void *fetchPayload(
69525 BtCursor *pCur, /* Cursor pointing to entry to read from */
69526 u32 *pAmt /* Write the number of available bytes here */
69527){
69528 int amt;
69529 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
69530 assert( pCur->eState==CURSOR_VALID );
69531 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69532 assert( cursorOwnsBtShared(pCur) );
69533 assert( pCur->ix<pCur->pPage->nCell );
69534 assert( pCur->info.nSize>0 );
69535 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
69536 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
69537 amt = pCur->info.nLocal;
69538 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
69539 /* There is too little space on the page for the expected amount
69540 ** of local content. Database must be corrupt. */
69541 assert( CORRUPT_DB );
69542 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
69543 }
69544 *pAmt = (u32)amt;
69545 return (void*)pCur->info.pPayload;
69546}
69547
69548
69549/*
69550** For the entry that cursor pCur is point to, return as
69551** many bytes of the key or data as are available on the local
69552** b-tree page. Write the number of available bytes into *pAmt.
69553**
69554** The pointer returned is ephemeral. The key/data may move
69555** or be destroyed on the next call to any Btree routine,
69556** including calls from other threads against the same cache.
69557** Hence, a mutex on the BtShared should be held prior to calling
69558** this routine.
69559**
69560** These routines is used to get quick access to key and data
69561** in the common case where no overflow pages are used.
69562*/
69563SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
69564 return fetchPayload(pCur, pAmt);
69565}
69566
69567
69568/*
69569** Move the cursor down to a new child page. The newPgno argument is the
69570** page number of the child page to move to.
69571**
69572** This function returns SQLITE_CORRUPT if the page-header flags field of
69573** the new child page does not match the flags field of the parent (i.e.
69574** if an intkey page appears to be the parent of a non-intkey page, or
69575** vice-versa).
69576*/
69577static int moveToChild(BtCursor *pCur, u32 newPgno){
69578 BtShared *pBt = pCur->pBt;
69579
69580 assert( cursorOwnsBtShared(pCur) );
69581 assert( pCur->eState==CURSOR_VALID );
69582 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
69583 assert( pCur->iPage>=0 );
69584 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
69585 return SQLITE_CORRUPT_BKPT;
69586 }
69587 pCur->info.nSize = 0;
69588 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
69589 pCur->aiIdx[pCur->iPage] = pCur->ix;
69590 pCur->apPage[pCur->iPage] = pCur->pPage;
69591 pCur->ix = 0;
69592 pCur->iPage++;
69593 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
69594}
69595
69596#ifdef SQLITE_DEBUG
69597/*
69598** Page pParent is an internal (non-leaf) tree page. This function
69599** asserts that page number iChild is the left-child if the iIdx'th
69600** cell in page pParent. Or, if iIdx is equal to the total number of
69601** cells in pParent, that page number iChild is the right-child of
69602** the page.
69603*/
69604static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
69605 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
69606 ** in a corrupt database */
69607 assert( iIdx<=pParent->nCell );
69608 if( iIdx==pParent->nCell ){
69609 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
69610 }else{
69611 assert( get4byte(findCell(pParent, iIdx))==iChild );
69612 }
69613}
69614#else
69615# define assertParentIndex(x,y,z)
69616#endif
69617
69618/*
69619** Move the cursor up to the parent page.
69620**
69621** pCur->idx is set to the cell index that contains the pointer
69622** to the page we are coming from. If we are coming from the
69623** right-most child page then pCur->idx is set to one more than
69624** the largest cell index.
69625*/
69626static void moveToParent(BtCursor *pCur){
69627 MemPage *pLeaf;
69628 assert( cursorOwnsBtShared(pCur) );
69629 assert( pCur->eState==CURSOR_VALID );
69630 assert( pCur->iPage>0 );
69631 assert( pCur->pPage );
69632 assertParentIndex(
69633 pCur->apPage[pCur->iPage-1],
69634 pCur->aiIdx[pCur->iPage-1],
69635 pCur->pPage->pgno
69636 );
69637 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
69638 pCur->info.nSize = 0;
69639 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
69640 pCur->ix = pCur->aiIdx[pCur->iPage-1];
69641 pLeaf = pCur->pPage;
69642 pCur->pPage = pCur->apPage[--pCur->iPage];
69643 releasePageNotNull(pLeaf);
69644}
69645
69646/*
69647** Move the cursor to point to the root page of its b-tree structure.
69648**
69649** If the table has a virtual root page, then the cursor is moved to point
69650** to the virtual root page instead of the actual root page. A table has a
69651** virtual root page when the actual root page contains no cells and a
69652** single child page. This can only happen with the table rooted at page 1.
69653**
69654** If the b-tree structure is empty, the cursor state is set to
69655** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
69656** the cursor is set to point to the first cell located on the root
69657** (or virtual root) page and the cursor state is set to CURSOR_VALID.
69658**
69659** If this function returns successfully, it may be assumed that the
69660** page-header flags indicate that the [virtual] root-page is the expected
69661** kind of b-tree page (i.e. if when opening the cursor the caller did not
69662** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
69663** indicating a table b-tree, or if the caller did specify a KeyInfo
69664** structure the flags byte is set to 0x02 or 0x0A, indicating an index
69665** b-tree).
69666*/
69667static int moveToRoot(BtCursor *pCur){
69668 MemPage *pRoot;
69669 int rc = SQLITE_OK;
69670
69671 assert( cursorOwnsBtShared(pCur) );
69672 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
69673 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
69674 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
69675 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
69676 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
69677
69678 if( pCur->iPage>=0 ){
69679 if( pCur->iPage ){
69680 releasePageNotNull(pCur->pPage);
69681 while( --pCur->iPage ){
69682 releasePageNotNull(pCur->apPage[pCur->iPage]);
69683 }
69684 pCur->pPage = pCur->apPage[0];
69685 goto skip_init;
69686 }
69687 }else if( pCur->pgnoRoot==0 ){
69688 pCur->eState = CURSOR_INVALID;
69689 return SQLITE_EMPTY;
69690 }else{
69691 assert( pCur->iPage==(-1) );
69692 if( pCur->eState>=CURSOR_REQUIRESEEK ){
69693 if( pCur->eState==CURSOR_FAULT ){
69694 assert( pCur->skipNext!=SQLITE_OK );
69695 return pCur->skipNext;
69696 }
69697 sqlite3BtreeClearCursor(pCur);
69698 }
69699 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
69700 0, pCur->curPagerFlags);
69701 if( rc!=SQLITE_OK ){
69702 pCur->eState = CURSOR_INVALID;
69703 return rc;
69704 }
69705 pCur->iPage = 0;
69706 pCur->curIntKey = pCur->pPage->intKey;
69707 }
69708 pRoot = pCur->pPage;
69709 assert( pRoot->pgno==pCur->pgnoRoot );
69710
69711 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
69712 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
69713 ** NULL, the caller expects a table b-tree. If this is not the case,
69714 ** return an SQLITE_CORRUPT error.
69715 **
69716 ** Earlier versions of SQLite assumed that this test could not fail
69717 ** if the root page was already loaded when this function was called (i.e.
69718 ** if pCur->iPage>=0). But this is not so if the database is corrupted
69719 ** in such a way that page pRoot is linked into a second b-tree table
69720 ** (or the freelist). */
69721 assert( pRoot->intKey==1 || pRoot->intKey==0 );
69722 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
69723 return SQLITE_CORRUPT_PAGE(pCur->pPage);
69724 }
69725
69726skip_init:
69727 pCur->ix = 0;
69728 pCur->info.nSize = 0;
69729 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
69730
69731 pRoot = pCur->pPage;
69732 if( pRoot->nCell>0 ){
69733 pCur->eState = CURSOR_VALID;
69734 }else if( !pRoot->leaf ){
69735 Pgno subpage;
69736 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
69737 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
69738 pCur->eState = CURSOR_VALID;
69739 rc = moveToChild(pCur, subpage);
69740 }else{
69741 pCur->eState = CURSOR_INVALID;
69742 rc = SQLITE_EMPTY;
69743 }
69744 return rc;
69745}
69746
69747/*
69748** Move the cursor down to the left-most leaf entry beneath the
69749** entry to which it is currently pointing.
69750**
69751** The left-most leaf is the one with the smallest key - the first
69752** in ascending order.
69753*/
69754static int moveToLeftmost(BtCursor *pCur){
69755 Pgno pgno;
69756 int rc = SQLITE_OK;
69757 MemPage *pPage;
69758
69759 assert( cursorOwnsBtShared(pCur) );
69760 assert( pCur->eState==CURSOR_VALID );
69761 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
69762 assert( pCur->ix<pPage->nCell );
69763 pgno = get4byte(findCell(pPage, pCur->ix));
69764 rc = moveToChild(pCur, pgno);
69765 }
69766 return rc;
69767}
69768
69769/*
69770** Move the cursor down to the right-most leaf entry beneath the
69771** page to which it is currently pointing. Notice the difference
69772** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
69773** finds the left-most entry beneath the *entry* whereas moveToRightmost()
69774** finds the right-most entry beneath the *page*.
69775**
69776** The right-most entry is the one with the largest key - the last
69777** key in ascending order.
69778*/
69779static int moveToRightmost(BtCursor *pCur){
69780 Pgno pgno;
69781 int rc = SQLITE_OK;
69782 MemPage *pPage = 0;
69783
69784 assert( cursorOwnsBtShared(pCur) );
69785 assert( pCur->eState==CURSOR_VALID );
69786 while( !(pPage = pCur->pPage)->leaf ){
69787 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
69788 pCur->ix = pPage->nCell;
69789 rc = moveToChild(pCur, pgno);
69790 if( rc ) return rc;
69791 }
69792 pCur->ix = pPage->nCell-1;
69793 assert( pCur->info.nSize==0 );
69794 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
69795 return SQLITE_OK;
69796}
69797
69798/* Move the cursor to the first entry in the table. Return SQLITE_OK
69799** on success. Set *pRes to 0 if the cursor actually points to something
69800** or set *pRes to 1 if the table is empty.
69801*/
69802SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
69803 int rc;
69804
69805 assert( cursorOwnsBtShared(pCur) );
69806 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69807 rc = moveToRoot(pCur);
69808 if( rc==SQLITE_OK ){
69809 assert( pCur->pPage->nCell>0 );
69810 *pRes = 0;
69811 rc = moveToLeftmost(pCur);
69812 }else if( rc==SQLITE_EMPTY ){
69813 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69814 *pRes = 1;
69815 rc = SQLITE_OK;
69816 }
69817 return rc;
69818}
69819
69820/* Move the cursor to the last entry in the table. Return SQLITE_OK
69821** on success. Set *pRes to 0 if the cursor actually points to something
69822** or set *pRes to 1 if the table is empty.
69823*/
69824SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
69825 int rc;
69826
69827 assert( cursorOwnsBtShared(pCur) );
69828 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69829
69830 /* If the cursor already points to the last entry, this is a no-op. */
69831 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
69832#ifdef SQLITE_DEBUG
69833 /* This block serves to assert() that the cursor really does point
69834 ** to the last entry in the b-tree. */
69835 int ii;
69836 for(ii=0; ii<pCur->iPage; ii++){
69837 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
69838 }
69839 assert( pCur->ix==pCur->pPage->nCell-1 );
69840 assert( pCur->pPage->leaf );
69841#endif
69842 *pRes = 0;
69843 return SQLITE_OK;
69844 }
69845
69846 rc = moveToRoot(pCur);
69847 if( rc==SQLITE_OK ){
69848 assert( pCur->eState==CURSOR_VALID );
69849 *pRes = 0;
69850 rc = moveToRightmost(pCur);
69851 if( rc==SQLITE_OK ){
69852 pCur->curFlags |= BTCF_AtLast;
69853 }else{
69854 pCur->curFlags &= ~BTCF_AtLast;
69855 }
69856 }else if( rc==SQLITE_EMPTY ){
69857 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69858 *pRes = 1;
69859 rc = SQLITE_OK;
69860 }
69861 return rc;
69862}
69863
69864/* Move the cursor so that it points to an entry near the key
69865** specified by pIdxKey or intKey. Return a success code.
69866**
69867** For INTKEY tables, the intKey parameter is used. pIdxKey
69868** must be NULL. For index tables, pIdxKey is used and intKey
69869** is ignored.
69870**
69871** If an exact match is not found, then the cursor is always
69872** left pointing at a leaf page which would hold the entry if it
69873** were present. The cursor might point to an entry that comes
69874** before or after the key.
69875**
69876** An integer is written into *pRes which is the result of
69877** comparing the key with the entry to which the cursor is
69878** pointing. The meaning of the integer written into
69879** *pRes is as follows:
69880**
69881** *pRes<0 The cursor is left pointing at an entry that
69882** is smaller than intKey/pIdxKey or if the table is empty
69883** and the cursor is therefore left point to nothing.
69884**
69885** *pRes==0 The cursor is left pointing at an entry that
69886** exactly matches intKey/pIdxKey.
69887**
69888** *pRes>0 The cursor is left pointing at an entry that
69889** is larger than intKey/pIdxKey.
69890**
69891** For index tables, the pIdxKey->eqSeen field is set to 1 if there
69892** exists an entry in the table that exactly matches pIdxKey.
69893*/
69894SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
69895 BtCursor *pCur, /* The cursor to be moved */
69896 UnpackedRecord *pIdxKey, /* Unpacked index key */
69897 i64 intKey, /* The table key */
69898 int biasRight, /* If true, bias the search to the high end */
69899 int *pRes /* Write search results here */
69900){
69901 int rc;
69902 RecordCompare xRecordCompare;
69903
69904 assert( cursorOwnsBtShared(pCur) );
69905 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
69906 assert( pRes );
69907 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
69908 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
69909
69910 /* If the cursor is already positioned at the point we are trying
69911 ** to move to, then just return without doing any work */
69912 if( pIdxKey==0
69913 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
69914 ){
69915 if( pCur->info.nKey==intKey ){
69916 *pRes = 0;
69917 return SQLITE_OK;
69918 }
69919 if( pCur->info.nKey<intKey ){
69920 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
69921 *pRes = -1;
69922 return SQLITE_OK;
69923 }
69924 /* If the requested key is one more than the previous key, then
69925 ** try to get there using sqlite3BtreeNext() rather than a full
69926 ** binary search. This is an optimization only. The correct answer
69927 ** is still obtained without this case, only a little more slowely */
69928 if( pCur->info.nKey+1==intKey ){
69929 *pRes = 0;
69930 rc = sqlite3BtreeNext(pCur, 0);
69931 if( rc==SQLITE_OK ){
69932 getCellInfo(pCur);
69933 if( pCur->info.nKey==intKey ){
69934 return SQLITE_OK;
69935 }
69936 }else if( rc==SQLITE_DONE ){
69937 rc = SQLITE_OK;
69938 }else{
69939 return rc;
69940 }
69941 }
69942 }
69943 }
69944
69945 if( pIdxKey ){
69946 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
69947 pIdxKey->errCode = 0;
69948 assert( pIdxKey->default_rc==1
69949 || pIdxKey->default_rc==0
69950 || pIdxKey->default_rc==-1
69951 );
69952 }else{
69953 xRecordCompare = 0; /* All keys are integers */
69954 }
69955
69956 rc = moveToRoot(pCur);
69957 if( rc ){
69958 if( rc==SQLITE_EMPTY ){
69959 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
69960 *pRes = -1;
69961 return SQLITE_OK;
69962 }
69963 return rc;
69964 }
69965 assert( pCur->pPage );
69966 assert( pCur->pPage->isInit );
69967 assert( pCur->eState==CURSOR_VALID );
69968 assert( pCur->pPage->nCell > 0 );
69969 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
69970 assert( pCur->curIntKey || pIdxKey );
69971 for(;;){
69972 int lwr, upr, idx, c;
69973 Pgno chldPg;
69974 MemPage *pPage = pCur->pPage;
69975 u8 *pCell; /* Pointer to current cell in pPage */
69976
69977 /* pPage->nCell must be greater than zero. If this is the root-page
69978 ** the cursor would have been INVALID above and this for(;;) loop
69979 ** not run. If this is not the root-page, then the moveToChild() routine
69980 ** would have already detected db corruption. Similarly, pPage must
69981 ** be the right kind (index or table) of b-tree page. Otherwise
69982 ** a moveToChild() or moveToRoot() call would have detected corruption. */
69983 assert( pPage->nCell>0 );
69984 assert( pPage->intKey==(pIdxKey==0) );
69985 lwr = 0;
69986 upr = pPage->nCell-1;
69987 assert( biasRight==0 || biasRight==1 );
69988 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
69989 pCur->ix = (u16)idx;
69990 if( xRecordCompare==0 ){
69991 for(;;){
69992 i64 nCellKey;
69993 pCell = findCellPastPtr(pPage, idx);
69994 if( pPage->intKeyLeaf ){
69995 while( 0x80 <= *(pCell++) ){
69996 if( pCell>=pPage->aDataEnd ){
69997 return SQLITE_CORRUPT_PAGE(pPage);
69998 }
69999 }
70000 }
70001 getVarint(pCell, (u64*)&nCellKey);
70002 if( nCellKey<intKey ){
70003 lwr = idx+1;
70004 if( lwr>upr ){ c = -1; break; }
70005 }else if( nCellKey>intKey ){
70006 upr = idx-1;
70007 if( lwr>upr ){ c = +1; break; }
70008 }else{
70009 assert( nCellKey==intKey );
70010 pCur->ix = (u16)idx;
70011 if( !pPage->leaf ){
70012 lwr = idx;
70013 goto moveto_next_layer;
70014 }else{
70015 pCur->curFlags |= BTCF_ValidNKey;
70016 pCur->info.nKey = nCellKey;
70017 pCur->info.nSize = 0;
70018 *pRes = 0;
70019 return SQLITE_OK;
70020 }
70021 }
70022 assert( lwr+upr>=0 );
70023 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
70024 }
70025 }else{
70026 for(;;){
70027 int nCell; /* Size of the pCell cell in bytes */
70028 pCell = findCellPastPtr(pPage, idx);
70029
70030 /* The maximum supported page-size is 65536 bytes. This means that
70031 ** the maximum number of record bytes stored on an index B-Tree
70032 ** page is less than 16384 bytes and may be stored as a 2-byte
70033 ** varint. This information is used to attempt to avoid parsing
70034 ** the entire cell by checking for the cases where the record is
70035 ** stored entirely within the b-tree page by inspecting the first
70036 ** 2 bytes of the cell.
70037 */
70038 nCell = pCell[0];
70039 if( nCell<=pPage->max1bytePayload ){
70040 /* This branch runs if the record-size field of the cell is a
70041 ** single byte varint and the record fits entirely on the main
70042 ** b-tree page. */
70043 testcase( pCell+nCell+1==pPage->aDataEnd );
70044 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
70045 }else if( !(pCell[1] & 0x80)
70046 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
70047 ){
70048 /* The record-size field is a 2 byte varint and the record
70049 ** fits entirely on the main b-tree page. */
70050 testcase( pCell+nCell+2==pPage->aDataEnd );
70051 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
70052 }else{
70053 /* The record flows over onto one or more overflow pages. In
70054 ** this case the whole cell needs to be parsed, a buffer allocated
70055 ** and accessPayload() used to retrieve the record into the
70056 ** buffer before VdbeRecordCompare() can be called.
70057 **
70058 ** If the record is corrupt, the xRecordCompare routine may read
70059 ** up to two varints past the end of the buffer. An extra 18
70060 ** bytes of padding is allocated at the end of the buffer in
70061 ** case this happens. */
70062 void *pCellKey;
70063 u8 * const pCellBody = pCell - pPage->childPtrSize;
70064 const int nOverrun = 18; /* Size of the overrun padding */
70065 pPage->xParseCell(pPage, pCellBody, &pCur->info);
70066 nCell = (int)pCur->info.nKey;
70067 testcase( nCell<0 ); /* True if key size is 2^32 or more */
70068 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
70069 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
70070 testcase( nCell==2 ); /* Minimum legal index key size */
70071 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
70072 rc = SQLITE_CORRUPT_PAGE(pPage);
70073 goto moveto_finish;
70074 }
70075 pCellKey = sqlite3Malloc( nCell+nOverrun );
70076 if( pCellKey==0 ){
70077 rc = SQLITE_NOMEM_BKPT;
70078 goto moveto_finish;
70079 }
70080 pCur->ix = (u16)idx;
70081 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
70082 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
70083 pCur->curFlags &= ~BTCF_ValidOvfl;
70084 if( rc ){
70085 sqlite3_free(pCellKey);
70086 goto moveto_finish;
70087 }
70088 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
70089 sqlite3_free(pCellKey);
70090 }
70091 assert(
70092 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
70093 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
70094 );
70095 if( c<0 ){
70096 lwr = idx+1;
70097 }else if( c>0 ){
70098 upr = idx-1;
70099 }else{
70100 assert( c==0 );
70101 *pRes = 0;
70102 rc = SQLITE_OK;
70103 pCur->ix = (u16)idx;
70104 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
70105 goto moveto_finish;
70106 }
70107 if( lwr>upr ) break;
70108 assert( lwr+upr>=0 );
70109 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
70110 }
70111 }
70112 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
70113 assert( pPage->isInit );
70114 if( pPage->leaf ){
70115 assert( pCur->ix<pCur->pPage->nCell );
70116 pCur->ix = (u16)idx;
70117 *pRes = c;
70118 rc = SQLITE_OK;
70119 goto moveto_finish;
70120 }
70121moveto_next_layer:
70122 if( lwr>=pPage->nCell ){
70123 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70124 }else{
70125 chldPg = get4byte(findCell(pPage, lwr));
70126 }
70127 pCur->ix = (u16)lwr;
70128 rc = moveToChild(pCur, chldPg);
70129 if( rc ) break;
70130 }
70131moveto_finish:
70132 pCur->info.nSize = 0;
70133 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
70134 return rc;
70135}
70136
70137
70138/*
70139** Return TRUE if the cursor is not pointing at an entry of the table.
70140**
70141** TRUE will be returned after a call to sqlite3BtreeNext() moves
70142** past the last entry in the table or sqlite3BtreePrev() moves past
70143** the first entry. TRUE is also returned if the table is empty.
70144*/
70145SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
70146 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
70147 ** have been deleted? This API will need to change to return an error code
70148 ** as well as the boolean result value.
70149 */
70150 return (CURSOR_VALID!=pCur->eState);
70151}
70152
70153/*
70154** Return an estimate for the number of rows in the table that pCur is
70155** pointing to. Return a negative number if no estimate is currently
70156** available.
70157*/
70158SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
70159 i64 n;
70160 u8 i;
70161
70162 assert( cursorOwnsBtShared(pCur) );
70163 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70164
70165 /* Currently this interface is only called by the OP_IfSmaller
70166 ** opcode, and it that case the cursor will always be valid and
70167 ** will always point to a leaf node. */
70168 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
70169 if( NEVER(pCur->pPage->leaf==0) ) return -1;
70170
70171 n = pCur->pPage->nCell;
70172 for(i=0; i<pCur->iPage; i++){
70173 n *= pCur->apPage[i]->nCell;
70174 }
70175 return n;
70176}
70177
70178/*
70179** Advance the cursor to the next entry in the database.
70180** Return value:
70181**
70182** SQLITE_OK success
70183** SQLITE_DONE cursor is already pointing at the last element
70184** otherwise some kind of error occurred
70185**
70186** The main entry point is sqlite3BtreeNext(). That routine is optimized
70187** for the common case of merely incrementing the cell counter BtCursor.aiIdx
70188** to the next cell on the current page. The (slower) btreeNext() helper
70189** routine is called when it is necessary to move to a different page or
70190** to restore the cursor.
70191**
70192** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
70193** cursor corresponds to an SQL index and this routine could have been
70194** skipped if the SQL index had been a unique index. The F argument
70195** is a hint to the implement. SQLite btree implementation does not use
70196** this hint, but COMDB2 does.
70197*/
70198static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
70199 int rc;
70200 int idx;
70201 MemPage *pPage;
70202
70203 assert( cursorOwnsBtShared(pCur) );
70204 if( pCur->eState!=CURSOR_VALID ){
70205 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
70206 rc = restoreCursorPosition(pCur);
70207 if( rc!=SQLITE_OK ){
70208 return rc;
70209 }
70210 if( CURSOR_INVALID==pCur->eState ){
70211 return SQLITE_DONE;
70212 }
70213 if( pCur->eState==CURSOR_SKIPNEXT ){
70214 pCur->eState = CURSOR_VALID;
70215 if( pCur->skipNext>0 ) return SQLITE_OK;
70216 }
70217 }
70218
70219 pPage = pCur->pPage;
70220 idx = ++pCur->ix;
70221 if( !pPage->isInit ){
70222 /* The only known way for this to happen is for there to be a
70223 ** recursive SQL function that does a DELETE operation as part of a
70224 ** SELECT which deletes content out from under an active cursor
70225 ** in a corrupt database file where the table being DELETE-ed from
70226 ** has pages in common with the table being queried. See TH3
70227 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
70228 ** example. */
70229 return SQLITE_CORRUPT_BKPT;
70230 }
70231
70232 /* If the database file is corrupt, it is possible for the value of idx
70233 ** to be invalid here. This can only occur if a second cursor modifies
70234 ** the page while cursor pCur is holding a reference to it. Which can
70235 ** only happen if the database is corrupt in such a way as to link the
70236 ** page into more than one b-tree structure.
70237 **
70238 ** Update 2019-12-23: appears to long longer be possible after the
70239 ** addition of anotherValidCursor() condition on balance_deeper(). */
70240 harmless( idx>pPage->nCell );
70241
70242 if( idx>=pPage->nCell ){
70243 if( !pPage->leaf ){
70244 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
70245 if( rc ) return rc;
70246 return moveToLeftmost(pCur);
70247 }
70248 do{
70249 if( pCur->iPage==0 ){
70250 pCur->eState = CURSOR_INVALID;
70251 return SQLITE_DONE;
70252 }
70253 moveToParent(pCur);
70254 pPage = pCur->pPage;
70255 }while( pCur->ix>=pPage->nCell );
70256 if( pPage->intKey ){
70257 return sqlite3BtreeNext(pCur, 0);
70258 }else{
70259 return SQLITE_OK;
70260 }
70261 }
70262 if( pPage->leaf ){
70263 return SQLITE_OK;
70264 }else{
70265 return moveToLeftmost(pCur);
70266 }
70267}
70268SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
70269 MemPage *pPage;
70270 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
70271 assert( cursorOwnsBtShared(pCur) );
70272 assert( flags==0 || flags==1 );
70273 pCur->info.nSize = 0;
70274 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70275 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
70276 pPage = pCur->pPage;
70277 if( (++pCur->ix)>=pPage->nCell ){
70278 pCur->ix--;
70279 return btreeNext(pCur);
70280 }
70281 if( pPage->leaf ){
70282 return SQLITE_OK;
70283 }else{
70284 return moveToLeftmost(pCur);
70285 }
70286}
70287
70288/*
70289** Step the cursor to the back to the previous entry in the database.
70290** Return values:
70291**
70292** SQLITE_OK success
70293** SQLITE_DONE the cursor is already on the first element of the table
70294** otherwise some kind of error occurred
70295**
70296** The main entry point is sqlite3BtreePrevious(). That routine is optimized
70297** for the common case of merely decrementing the cell counter BtCursor.aiIdx
70298** to the previous cell on the current page. The (slower) btreePrevious()
70299** helper routine is called when it is necessary to move to a different page
70300** or to restore the cursor.
70301**
70302** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
70303** the cursor corresponds to an SQL index and this routine could have been
70304** skipped if the SQL index had been a unique index. The F argument is a
70305** hint to the implement. The native SQLite btree implementation does not
70306** use this hint, but COMDB2 does.
70307*/
70308static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
70309 int rc;
70310 MemPage *pPage;
70311
70312 assert( cursorOwnsBtShared(pCur) );
70313 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
70314 assert( pCur->info.nSize==0 );
70315 if( pCur->eState!=CURSOR_VALID ){
70316 rc = restoreCursorPosition(pCur);
70317 if( rc!=SQLITE_OK ){
70318 return rc;
70319 }
70320 if( CURSOR_INVALID==pCur->eState ){
70321 return SQLITE_DONE;
70322 }
70323 if( CURSOR_SKIPNEXT==pCur->eState ){
70324 pCur->eState = CURSOR_VALID;
70325 if( pCur->skipNext<0 ) return SQLITE_OK;
70326 }
70327 }
70328
70329 pPage = pCur->pPage;
70330 assert( pPage->isInit );
70331 if( !pPage->leaf ){
70332 int idx = pCur->ix;
70333 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
70334 if( rc ) return rc;
70335 rc = moveToRightmost(pCur);
70336 }else{
70337 while( pCur->ix==0 ){
70338 if( pCur->iPage==0 ){
70339 pCur->eState = CURSOR_INVALID;
70340 return SQLITE_DONE;
70341 }
70342 moveToParent(pCur);
70343 }
70344 assert( pCur->info.nSize==0 );
70345 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
70346
70347 pCur->ix--;
70348 pPage = pCur->pPage;
70349 if( pPage->intKey && !pPage->leaf ){
70350 rc = sqlite3BtreePrevious(pCur, 0);
70351 }else{
70352 rc = SQLITE_OK;
70353 }
70354 }
70355 return rc;
70356}
70357SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
70358 assert( cursorOwnsBtShared(pCur) );
70359 assert( flags==0 || flags==1 );
70360 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
70361 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
70362 pCur->info.nSize = 0;
70363 if( pCur->eState!=CURSOR_VALID
70364 || pCur->ix==0
70365 || pCur->pPage->leaf==0
70366 ){
70367 return btreePrevious(pCur);
70368 }
70369 pCur->ix--;
70370 return SQLITE_OK;
70371}
70372
70373/*
70374** Allocate a new page from the database file.
70375**
70376** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
70377** has already been called on the new page.) The new page has also
70378** been referenced and the calling routine is responsible for calling
70379** sqlite3PagerUnref() on the new page when it is done.
70380**
70381** SQLITE_OK is returned on success. Any other return value indicates
70382** an error. *ppPage is set to NULL in the event of an error.
70383**
70384** If the "nearby" parameter is not 0, then an effort is made to
70385** locate a page close to the page number "nearby". This can be used in an
70386** attempt to keep related pages close to each other in the database file,
70387** which in turn can make database access faster.
70388**
70389** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
70390** anywhere on the free-list, then it is guaranteed to be returned. If
70391** eMode is BTALLOC_LT then the page returned will be less than or equal
70392** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
70393** are no restrictions on which page is returned.
70394*/
70395static int allocateBtreePage(
70396 BtShared *pBt, /* The btree */
70397 MemPage **ppPage, /* Store pointer to the allocated page here */
70398 Pgno *pPgno, /* Store the page number here */
70399 Pgno nearby, /* Search for a page near this one */
70400 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
70401){
70402 MemPage *pPage1;
70403 int rc;
70404 u32 n; /* Number of pages on the freelist */
70405 u32 k; /* Number of leaves on the trunk of the freelist */
70406 MemPage *pTrunk = 0;
70407 MemPage *pPrevTrunk = 0;
70408 Pgno mxPage; /* Total size of the database file */
70409
70410 assert( sqlite3_mutex_held(pBt->mutex) );
70411 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
70412 pPage1 = pBt->pPage1;
70413 mxPage = btreePagecount(pBt);
70414 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
70415 ** stores stores the total number of pages on the freelist. */
70416 n = get4byte(&pPage1->aData[36]);
70417 testcase( n==mxPage-1 );
70418 if( n>=mxPage ){
70419 return SQLITE_CORRUPT_BKPT;
70420 }
70421 if( n>0 ){
70422 /* There are pages on the freelist. Reuse one of those pages. */
70423 Pgno iTrunk;
70424 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
70425 u32 nSearch = 0; /* Count of the number of search attempts */
70426
70427 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
70428 ** shows that the page 'nearby' is somewhere on the free-list, then
70429 ** the entire-list will be searched for that page.
70430 */
70431#ifndef SQLITE_OMIT_AUTOVACUUM
70432 if( eMode==BTALLOC_EXACT ){
70433 if( nearby<=mxPage ){
70434 u8 eType;
70435 assert( nearby>0 );
70436 assert( pBt->autoVacuum );
70437 rc = ptrmapGet(pBt, nearby, &eType, 0);
70438 if( rc ) return rc;
70439 if( eType==PTRMAP_FREEPAGE ){
70440 searchList = 1;
70441 }
70442 }
70443 }else if( eMode==BTALLOC_LE ){
70444 searchList = 1;
70445 }
70446#endif
70447
70448 /* Decrement the free-list count by 1. Set iTrunk to the index of the
70449 ** first free-list trunk page. iPrevTrunk is initially 1.
70450 */
70451 rc = sqlite3PagerWrite(pPage1->pDbPage);
70452 if( rc ) return rc;
70453 put4byte(&pPage1->aData[36], n-1);
70454
70455 /* The code within this loop is run only once if the 'searchList' variable
70456 ** is not true. Otherwise, it runs once for each trunk-page on the
70457 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
70458 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
70459 */
70460 do {
70461 pPrevTrunk = pTrunk;
70462 if( pPrevTrunk ){
70463 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
70464 ** is the page number of the next freelist trunk page in the list or
70465 ** zero if this is the last freelist trunk page. */
70466 iTrunk = get4byte(&pPrevTrunk->aData[0]);
70467 }else{
70468 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
70469 ** stores the page number of the first page of the freelist, or zero if
70470 ** the freelist is empty. */
70471 iTrunk = get4byte(&pPage1->aData[32]);
70472 }
70473 testcase( iTrunk==mxPage );
70474 if( iTrunk>mxPage || nSearch++ > n ){
70475 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
70476 }else{
70477 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
70478 }
70479 if( rc ){
70480 pTrunk = 0;
70481 goto end_allocate_page;
70482 }
70483 assert( pTrunk!=0 );
70484 assert( pTrunk->aData!=0 );
70485 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
70486 ** is the number of leaf page pointers to follow. */
70487 k = get4byte(&pTrunk->aData[4]);
70488 if( k==0 && !searchList ){
70489 /* The trunk has no leaves and the list is not being searched.
70490 ** So extract the trunk page itself and use it as the newly
70491 ** allocated page */
70492 assert( pPrevTrunk==0 );
70493 rc = sqlite3PagerWrite(pTrunk->pDbPage);
70494 if( rc ){
70495 goto end_allocate_page;
70496 }
70497 *pPgno = iTrunk;
70498 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
70499 *ppPage = pTrunk;
70500 pTrunk = 0;
70501 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
70502 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
70503 /* Value of k is out of range. Database corruption */
70504 rc = SQLITE_CORRUPT_PGNO(iTrunk);
70505 goto end_allocate_page;
70506#ifndef SQLITE_OMIT_AUTOVACUUM
70507 }else if( searchList
70508 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
70509 ){
70510 /* The list is being searched and this trunk page is the page
70511 ** to allocate, regardless of whether it has leaves.
70512 */
70513 *pPgno = iTrunk;
70514 *ppPage = pTrunk;
70515 searchList = 0;
70516 rc = sqlite3PagerWrite(pTrunk->pDbPage);
70517 if( rc ){
70518 goto end_allocate_page;
70519 }
70520 if( k==0 ){
70521 if( !pPrevTrunk ){
70522 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
70523 }else{
70524 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
70525 if( rc!=SQLITE_OK ){
70526 goto end_allocate_page;
70527 }
70528 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
70529 }
70530 }else{
70531 /* The trunk page is required by the caller but it contains
70532 ** pointers to free-list leaves. The first leaf becomes a trunk
70533 ** page in this case.
70534 */
70535 MemPage *pNewTrunk;
70536 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
70537 if( iNewTrunk>mxPage ){
70538 rc = SQLITE_CORRUPT_PGNO(iTrunk);
70539 goto end_allocate_page;
70540 }
70541 testcase( iNewTrunk==mxPage );
70542 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
70543 if( rc!=SQLITE_OK ){
70544 goto end_allocate_page;
70545 }
70546 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
70547 if( rc!=SQLITE_OK ){
70548 releasePage(pNewTrunk);
70549 goto end_allocate_page;
70550 }
70551 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
70552 put4byte(&pNewTrunk->aData[4], k-1);
70553 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
70554 releasePage(pNewTrunk);
70555 if( !pPrevTrunk ){
70556 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
70557 put4byte(&pPage1->aData[32], iNewTrunk);
70558 }else{
70559 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
70560 if( rc ){
70561 goto end_allocate_page;
70562 }
70563 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
70564 }
70565 }
70566 pTrunk = 0;
70567 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
70568#endif
70569 }else if( k>0 ){
70570 /* Extract a leaf from the trunk */
70571 u32 closest;
70572 Pgno iPage;
70573 unsigned char *aData = pTrunk->aData;
70574 if( nearby>0 ){
70575 u32 i;
70576 closest = 0;
70577 if( eMode==BTALLOC_LE ){
70578 for(i=0; i<k; i++){
70579 iPage = get4byte(&aData[8+i*4]);
70580 if( iPage<=nearby ){
70581 closest = i;
70582 break;
70583 }
70584 }
70585 }else{
70586 int dist;
70587 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
70588 for(i=1; i<k; i++){
70589 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
70590 if( d2<dist ){
70591 closest = i;
70592 dist = d2;
70593 }
70594 }
70595 }
70596 }else{
70597 closest = 0;
70598 }
70599
70600 iPage = get4byte(&aData[8+closest*4]);
70601 testcase( iPage==mxPage );
70602 if( iPage>mxPage ){
70603 rc = SQLITE_CORRUPT_PGNO(iTrunk);
70604 goto end_allocate_page;
70605 }
70606 testcase( iPage==mxPage );
70607 if( !searchList
70608 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
70609 ){
70610 int noContent;
70611 *pPgno = iPage;
70612 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
70613 ": %d more free pages\n",
70614 *pPgno, closest+1, k, pTrunk->pgno, n-1));
70615 rc = sqlite3PagerWrite(pTrunk->pDbPage);
70616 if( rc ) goto end_allocate_page;
70617 if( closest<k-1 ){
70618 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
70619 }
70620 put4byte(&aData[4], k-1);
70621 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
70622 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
70623 if( rc==SQLITE_OK ){
70624 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
70625 if( rc!=SQLITE_OK ){
70626 releasePage(*ppPage);
70627 *ppPage = 0;
70628 }
70629 }
70630 searchList = 0;
70631 }
70632 }
70633 releasePage(pPrevTrunk);
70634 pPrevTrunk = 0;
70635 }while( searchList );
70636 }else{
70637 /* There are no pages on the freelist, so append a new page to the
70638 ** database image.
70639 **
70640 ** Normally, new pages allocated by this block can be requested from the
70641 ** pager layer with the 'no-content' flag set. This prevents the pager
70642 ** from trying to read the pages content from disk. However, if the
70643 ** current transaction has already run one or more incremental-vacuum
70644 ** steps, then the page we are about to allocate may contain content
70645 ** that is required in the event of a rollback. In this case, do
70646 ** not set the no-content flag. This causes the pager to load and journal
70647 ** the current page content before overwriting it.
70648 **
70649 ** Note that the pager will not actually attempt to load or journal
70650 ** content for any page that really does lie past the end of the database
70651 ** file on disk. So the effects of disabling the no-content optimization
70652 ** here are confined to those pages that lie between the end of the
70653 ** database image and the end of the database file.
70654 */
70655 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
70656
70657 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70658 if( rc ) return rc;
70659 pBt->nPage++;
70660 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
70661
70662#ifndef SQLITE_OMIT_AUTOVACUUM
70663 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
70664 /* If *pPgno refers to a pointer-map page, allocate two new pages
70665 ** at the end of the file instead of one. The first allocated page
70666 ** becomes a new pointer-map page, the second is used by the caller.
70667 */
70668 MemPage *pPg = 0;
70669 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
70670 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
70671 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
70672 if( rc==SQLITE_OK ){
70673 rc = sqlite3PagerWrite(pPg->pDbPage);
70674 releasePage(pPg);
70675 }
70676 if( rc ) return rc;
70677 pBt->nPage++;
70678 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
70679 }
70680#endif
70681 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
70682 *pPgno = pBt->nPage;
70683
70684 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
70685 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
70686 if( rc ) return rc;
70687 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
70688 if( rc!=SQLITE_OK ){
70689 releasePage(*ppPage);
70690 *ppPage = 0;
70691 }
70692 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
70693 }
70694
70695 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
70696
70697end_allocate_page:
70698 releasePage(pTrunk);
70699 releasePage(pPrevTrunk);
70700 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
70701 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
70702 return rc;
70703}
70704
70705/*
70706** This function is used to add page iPage to the database file free-list.
70707** It is assumed that the page is not already a part of the free-list.
70708**
70709** The value passed as the second argument to this function is optional.
70710** If the caller happens to have a pointer to the MemPage object
70711** corresponding to page iPage handy, it may pass it as the second value.
70712** Otherwise, it may pass NULL.
70713**
70714** If a pointer to a MemPage object is passed as the second argument,
70715** its reference count is not altered by this function.
70716*/
70717static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
70718 MemPage *pTrunk = 0; /* Free-list trunk page */
70719 Pgno iTrunk = 0; /* Page number of free-list trunk page */
70720 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
70721 MemPage *pPage; /* Page being freed. May be NULL. */
70722 int rc; /* Return Code */
70723 u32 nFree; /* Initial number of pages on free-list */
70724
70725 assert( sqlite3_mutex_held(pBt->mutex) );
70726 assert( CORRUPT_DB || iPage>1 );
70727 assert( !pMemPage || pMemPage->pgno==iPage );
70728
70729 if( iPage<2 || iPage>pBt->nPage ){
70730 return SQLITE_CORRUPT_BKPT;
70731 }
70732 if( pMemPage ){
70733 pPage = pMemPage;
70734 sqlite3PagerRef(pPage->pDbPage);
70735 }else{
70736 pPage = btreePageLookup(pBt, iPage);
70737 }
70738
70739 /* Increment the free page count on pPage1 */
70740 rc = sqlite3PagerWrite(pPage1->pDbPage);
70741 if( rc ) goto freepage_out;
70742 nFree = get4byte(&pPage1->aData[36]);
70743 put4byte(&pPage1->aData[36], nFree+1);
70744
70745 if( pBt->btsFlags & BTS_SECURE_DELETE ){
70746 /* If the secure_delete option is enabled, then
70747 ** always fully overwrite deleted information with zeros.
70748 */
70749 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
70750 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
70751 ){
70752 goto freepage_out;
70753 }
70754 memset(pPage->aData, 0, pPage->pBt->pageSize);
70755 }
70756
70757 /* If the database supports auto-vacuum, write an entry in the pointer-map
70758 ** to indicate that the page is free.
70759 */
70760 if( ISAUTOVACUUM ){
70761 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
70762 if( rc ) goto freepage_out;
70763 }
70764
70765 /* Now manipulate the actual database free-list structure. There are two
70766 ** possibilities. If the free-list is currently empty, or if the first
70767 ** trunk page in the free-list is full, then this page will become a
70768 ** new free-list trunk page. Otherwise, it will become a leaf of the
70769 ** first trunk page in the current free-list. This block tests if it
70770 ** is possible to add the page as a new free-list leaf.
70771 */
70772 if( nFree!=0 ){
70773 u32 nLeaf; /* Initial number of leaf cells on trunk page */
70774
70775 iTrunk = get4byte(&pPage1->aData[32]);
70776 if( iTrunk>btreePagecount(pBt) ){
70777 rc = SQLITE_CORRUPT_BKPT;
70778 goto freepage_out;
70779 }
70780 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
70781 if( rc!=SQLITE_OK ){
70782 goto freepage_out;
70783 }
70784
70785 nLeaf = get4byte(&pTrunk->aData[4]);
70786 assert( pBt->usableSize>32 );
70787 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
70788 rc = SQLITE_CORRUPT_BKPT;
70789 goto freepage_out;
70790 }
70791 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
70792 /* In this case there is room on the trunk page to insert the page
70793 ** being freed as a new leaf.
70794 **
70795 ** Note that the trunk page is not really full until it contains
70796 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
70797 ** coded. But due to a coding error in versions of SQLite prior to
70798 ** 3.6.0, databases with freelist trunk pages holding more than
70799 ** usableSize/4 - 8 entries will be reported as corrupt. In order
70800 ** to maintain backwards compatibility with older versions of SQLite,
70801 ** we will continue to restrict the number of entries to usableSize/4 - 8
70802 ** for now. At some point in the future (once everyone has upgraded
70803 ** to 3.6.0 or later) we should consider fixing the conditional above
70804 ** to read "usableSize/4-2" instead of "usableSize/4-8".
70805 **
70806 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
70807 ** avoid using the last six entries in the freelist trunk page array in
70808 ** order that database files created by newer versions of SQLite can be
70809 ** read by older versions of SQLite.
70810 */
70811 rc = sqlite3PagerWrite(pTrunk->pDbPage);
70812 if( rc==SQLITE_OK ){
70813 put4byte(&pTrunk->aData[4], nLeaf+1);
70814 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
70815 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
70816 sqlite3PagerDontWrite(pPage->pDbPage);
70817 }
70818 rc = btreeSetHasContent(pBt, iPage);
70819 }
70820 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
70821 goto freepage_out;
70822 }
70823 }
70824
70825 /* If control flows to this point, then it was not possible to add the
70826 ** the page being freed as a leaf page of the first trunk in the free-list.
70827 ** Possibly because the free-list is empty, or possibly because the
70828 ** first trunk in the free-list is full. Either way, the page being freed
70829 ** will become the new first trunk page in the free-list.
70830 */
70831 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
70832 goto freepage_out;
70833 }
70834 rc = sqlite3PagerWrite(pPage->pDbPage);
70835 if( rc!=SQLITE_OK ){
70836 goto freepage_out;
70837 }
70838 put4byte(pPage->aData, iTrunk);
70839 put4byte(&pPage->aData[4], 0);
70840 put4byte(&pPage1->aData[32], iPage);
70841 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
70842
70843freepage_out:
70844 if( pPage ){
70845 pPage->isInit = 0;
70846 }
70847 releasePage(pPage);
70848 releasePage(pTrunk);
70849 return rc;
70850}
70851static void freePage(MemPage *pPage, int *pRC){
70852 if( (*pRC)==SQLITE_OK ){
70853 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
70854 }
70855}
70856
70857/*
70858** Free any overflow pages associated with the given Cell. Store
70859** size information about the cell in pInfo.
70860*/
70861static int clearCell(
70862 MemPage *pPage, /* The page that contains the Cell */
70863 unsigned char *pCell, /* First byte of the Cell */
70864 CellInfo *pInfo /* Size information about the cell */
70865){
70866 BtShared *pBt;
70867 Pgno ovflPgno;
70868 int rc;
70869 int nOvfl;
70870 u32 ovflPageSize;
70871
70872 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70873 pPage->xParseCell(pPage, pCell, pInfo);
70874 if( pInfo->nLocal==pInfo->nPayload ){
70875 return SQLITE_OK; /* No overflow pages. Return without doing anything */
70876 }
70877 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
70878 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
70879 if( pCell + pInfo->nSize > pPage->aDataEnd ){
70880 /* Cell extends past end of page */
70881 return SQLITE_CORRUPT_PAGE(pPage);
70882 }
70883 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
70884 pBt = pPage->pBt;
70885 assert( pBt->usableSize > 4 );
70886 ovflPageSize = pBt->usableSize - 4;
70887 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
70888 assert( nOvfl>0 ||
70889 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
70890 );
70891 while( nOvfl-- ){
70892 Pgno iNext = 0;
70893 MemPage *pOvfl = 0;
70894 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
70895 /* 0 is not a legal page number and page 1 cannot be an
70896 ** overflow page. Therefore if ovflPgno<2 or past the end of the
70897 ** file the database must be corrupt. */
70898 return SQLITE_CORRUPT_BKPT;
70899 }
70900 if( nOvfl ){
70901 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
70902 if( rc ) return rc;
70903 }
70904
70905 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
70906 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
70907 ){
70908 /* There is no reason any cursor should have an outstanding reference
70909 ** to an overflow page belonging to a cell that is being deleted/updated.
70910 ** So if there exists more than one reference to this page, then it
70911 ** must not really be an overflow page and the database must be corrupt.
70912 ** It is helpful to detect this before calling freePage2(), as
70913 ** freePage2() may zero the page contents if secure-delete mode is
70914 ** enabled. If this 'overflow' page happens to be a page that the
70915 ** caller is iterating through or using in some other way, this
70916 ** can be problematic.
70917 */
70918 rc = SQLITE_CORRUPT_BKPT;
70919 }else{
70920 rc = freePage2(pBt, pOvfl, ovflPgno);
70921 }
70922
70923 if( pOvfl ){
70924 sqlite3PagerUnref(pOvfl->pDbPage);
70925 }
70926 if( rc ) return rc;
70927 ovflPgno = iNext;
70928 }
70929 return SQLITE_OK;
70930}
70931
70932/*
70933** Create the byte sequence used to represent a cell on page pPage
70934** and write that byte sequence into pCell[]. Overflow pages are
70935** allocated and filled in as necessary. The calling procedure
70936** is responsible for making sure sufficient space has been allocated
70937** for pCell[].
70938**
70939** Note that pCell does not necessary need to point to the pPage->aData
70940** area. pCell might point to some temporary storage. The cell will
70941** be constructed in this temporary area then copied into pPage->aData
70942** later.
70943*/
70944static int fillInCell(
70945 MemPage *pPage, /* The page that contains the cell */
70946 unsigned char *pCell, /* Complete text of the cell */
70947 const BtreePayload *pX, /* Payload with which to construct the cell */
70948 int *pnSize /* Write cell size here */
70949){
70950 int nPayload;
70951 const u8 *pSrc;
70952 int nSrc, n, rc, mn;
70953 int spaceLeft;
70954 MemPage *pToRelease;
70955 unsigned char *pPrior;
70956 unsigned char *pPayload;
70957 BtShared *pBt;
70958 Pgno pgnoOvfl;
70959 int nHeader;
70960
70961 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70962
70963 /* pPage is not necessarily writeable since pCell might be auxiliary
70964 ** buffer space that is separate from the pPage buffer area */
70965 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
70966 || sqlite3PagerIswriteable(pPage->pDbPage) );
70967
70968 /* Fill in the header. */
70969 nHeader = pPage->childPtrSize;
70970 if( pPage->intKey ){
70971 nPayload = pX->nData + pX->nZero;
70972 pSrc = pX->pData;
70973 nSrc = pX->nData;
70974 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
70975 nHeader += putVarint32(&pCell[nHeader], nPayload);
70976 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
70977 }else{
70978 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
70979 nSrc = nPayload = (int)pX->nKey;
70980 pSrc = pX->pKey;
70981 nHeader += putVarint32(&pCell[nHeader], nPayload);
70982 }
70983
70984 /* Fill in the payload */
70985 pPayload = &pCell[nHeader];
70986 if( nPayload<=pPage->maxLocal ){
70987 /* This is the common case where everything fits on the btree page
70988 ** and no overflow pages are required. */
70989 n = nHeader + nPayload;
70990 testcase( n==3 );
70991 testcase( n==4 );
70992 if( n<4 ) n = 4;
70993 *pnSize = n;
70994 assert( nSrc<=nPayload );
70995 testcase( nSrc<nPayload );
70996 memcpy(pPayload, pSrc, nSrc);
70997 memset(pPayload+nSrc, 0, nPayload-nSrc);
70998 return SQLITE_OK;
70999 }
71000
71001 /* If we reach this point, it means that some of the content will need
71002 ** to spill onto overflow pages.
71003 */
71004 mn = pPage->minLocal;
71005 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
71006 testcase( n==pPage->maxLocal );
71007 testcase( n==pPage->maxLocal+1 );
71008 if( n > pPage->maxLocal ) n = mn;
71009 spaceLeft = n;
71010 *pnSize = n + nHeader + 4;
71011 pPrior = &pCell[nHeader+n];
71012 pToRelease = 0;
71013 pgnoOvfl = 0;
71014 pBt = pPage->pBt;
71015
71016 /* At this point variables should be set as follows:
71017 **
71018 ** nPayload Total payload size in bytes
71019 ** pPayload Begin writing payload here
71020 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
71021 ** that means content must spill into overflow pages.
71022 ** *pnSize Size of the local cell (not counting overflow pages)
71023 ** pPrior Where to write the pgno of the first overflow page
71024 **
71025 ** Use a call to btreeParseCellPtr() to verify that the values above
71026 ** were computed correctly.
71027 */
71028#ifdef SQLITE_DEBUG
71029 {
71030 CellInfo info;
71031 pPage->xParseCell(pPage, pCell, &info);
71032 assert( nHeader==(int)(info.pPayload - pCell) );
71033 assert( info.nKey==pX->nKey );
71034 assert( *pnSize == info.nSize );
71035 assert( spaceLeft == info.nLocal );
71036 }
71037#endif
71038
71039 /* Write the payload into the local Cell and any extra into overflow pages */
71040 while( 1 ){
71041 n = nPayload;
71042 if( n>spaceLeft ) n = spaceLeft;
71043
71044 /* If pToRelease is not zero than pPayload points into the data area
71045 ** of pToRelease. Make sure pToRelease is still writeable. */
71046 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
71047
71048 /* If pPayload is part of the data area of pPage, then make sure pPage
71049 ** is still writeable */
71050 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
71051 || sqlite3PagerIswriteable(pPage->pDbPage) );
71052
71053 if( nSrc>=n ){
71054 memcpy(pPayload, pSrc, n);
71055 }else if( nSrc>0 ){
71056 n = nSrc;
71057 memcpy(pPayload, pSrc, n);
71058 }else{
71059 memset(pPayload, 0, n);
71060 }
71061 nPayload -= n;
71062 if( nPayload<=0 ) break;
71063 pPayload += n;
71064 pSrc += n;
71065 nSrc -= n;
71066 spaceLeft -= n;
71067 if( spaceLeft==0 ){
71068 MemPage *pOvfl = 0;
71069#ifndef SQLITE_OMIT_AUTOVACUUM
71070 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
71071 if( pBt->autoVacuum ){
71072 do{
71073 pgnoOvfl++;
71074 } while(
71075 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
71076 );
71077 }
71078#endif
71079 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
71080#ifndef SQLITE_OMIT_AUTOVACUUM
71081 /* If the database supports auto-vacuum, and the second or subsequent
71082 ** overflow page is being allocated, add an entry to the pointer-map
71083 ** for that page now.
71084 **
71085 ** If this is the first overflow page, then write a partial entry
71086 ** to the pointer-map. If we write nothing to this pointer-map slot,
71087 ** then the optimistic overflow chain processing in clearCell()
71088 ** may misinterpret the uninitialized values and delete the
71089 ** wrong pages from the database.
71090 */
71091 if( pBt->autoVacuum && rc==SQLITE_OK ){
71092 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
71093 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
71094 if( rc ){
71095 releasePage(pOvfl);
71096 }
71097 }
71098#endif
71099 if( rc ){
71100 releasePage(pToRelease);
71101 return rc;
71102 }
71103
71104 /* If pToRelease is not zero than pPrior points into the data area
71105 ** of pToRelease. Make sure pToRelease is still writeable. */
71106 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
71107
71108 /* If pPrior is part of the data area of pPage, then make sure pPage
71109 ** is still writeable */
71110 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
71111 || sqlite3PagerIswriteable(pPage->pDbPage) );
71112
71113 put4byte(pPrior, pgnoOvfl);
71114 releasePage(pToRelease);
71115 pToRelease = pOvfl;
71116 pPrior = pOvfl->aData;
71117 put4byte(pPrior, 0);
71118 pPayload = &pOvfl->aData[4];
71119 spaceLeft = pBt->usableSize - 4;
71120 }
71121 }
71122 releasePage(pToRelease);
71123 return SQLITE_OK;
71124}
71125
71126/*
71127** Remove the i-th cell from pPage. This routine effects pPage only.
71128** The cell content is not freed or deallocated. It is assumed that
71129** the cell content has been copied someplace else. This routine just
71130** removes the reference to the cell from pPage.
71131**
71132** "sz" must be the number of bytes in the cell.
71133*/
71134static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
71135 u32 pc; /* Offset to cell content of cell being deleted */
71136 u8 *data; /* pPage->aData */
71137 u8 *ptr; /* Used to move bytes around within data[] */
71138 int rc; /* The return code */
71139 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
71140
71141 if( *pRC ) return;
71142 assert( idx>=0 && idx<pPage->nCell );
71143 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
71144 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71145 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71146 assert( pPage->nFree>=0 );
71147 data = pPage->aData;
71148 ptr = &pPage->aCellIdx[2*idx];
71149 pc = get2byte(ptr);
71150 hdr = pPage->hdrOffset;
71151 testcase( pc==get2byte(&data[hdr+5]) );
71152 testcase( pc+sz==pPage->pBt->usableSize );
71153 if( pc+sz > pPage->pBt->usableSize ){
71154 *pRC = SQLITE_CORRUPT_BKPT;
71155 return;
71156 }
71157 rc = freeSpace(pPage, pc, sz);
71158 if( rc ){
71159 *pRC = rc;
71160 return;
71161 }
71162 pPage->nCell--;
71163 if( pPage->nCell==0 ){
71164 memset(&data[hdr+1], 0, 4);
71165 data[hdr+7] = 0;
71166 put2byte(&data[hdr+5], pPage->pBt->usableSize);
71167 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
71168 - pPage->childPtrSize - 8;
71169 }else{
71170 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
71171 put2byte(&data[hdr+3], pPage->nCell);
71172 pPage->nFree += 2;
71173 }
71174}
71175
71176/*
71177** Insert a new cell on pPage at cell index "i". pCell points to the
71178** content of the cell.
71179**
71180** If the cell content will fit on the page, then put it there. If it
71181** will not fit, then make a copy of the cell content into pTemp if
71182** pTemp is not null. Regardless of pTemp, allocate a new entry
71183** in pPage->apOvfl[] and make it point to the cell content (either
71184** in pTemp or the original pCell) and also record its index.
71185** Allocating a new entry in pPage->aCell[] implies that
71186** pPage->nOverflow is incremented.
71187**
71188** *pRC must be SQLITE_OK when this routine is called.
71189*/
71190static void insertCell(
71191 MemPage *pPage, /* Page into which we are copying */
71192 int i, /* New cell becomes the i-th cell of the page */
71193 u8 *pCell, /* Content of the new cell */
71194 int sz, /* Bytes of content in pCell */
71195 u8 *pTemp, /* Temp storage space for pCell, if needed */
71196 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
71197 int *pRC /* Read and write return code from here */
71198){
71199 int idx = 0; /* Where to write new cell content in data[] */
71200 int j; /* Loop counter */
71201 u8 *data; /* The content of the whole page */
71202 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
71203
71204 assert( *pRC==SQLITE_OK );
71205 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
71206 assert( MX_CELL(pPage->pBt)<=10921 );
71207 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
71208 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
71209 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
71210 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71211 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
71212 assert( pPage->nFree>=0 );
71213 if( pPage->nOverflow || sz+2>pPage->nFree ){
71214 if( pTemp ){
71215 memcpy(pTemp, pCell, sz);
71216 pCell = pTemp;
71217 }
71218 if( iChild ){
71219 put4byte(pCell, iChild);
71220 }
71221 j = pPage->nOverflow++;
71222 /* Comparison against ArraySize-1 since we hold back one extra slot
71223 ** as a contingency. In other words, never need more than 3 overflow
71224 ** slots but 4 are allocated, just to be safe. */
71225 assert( j < ArraySize(pPage->apOvfl)-1 );
71226 pPage->apOvfl[j] = pCell;
71227 pPage->aiOvfl[j] = (u16)i;
71228
71229 /* When multiple overflows occur, they are always sequential and in
71230 ** sorted order. This invariants arise because multiple overflows can
71231 ** only occur when inserting divider cells into the parent page during
71232 ** balancing, and the dividers are adjacent and sorted.
71233 */
71234 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
71235 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
71236 }else{
71237 int rc = sqlite3PagerWrite(pPage->pDbPage);
71238 if( rc!=SQLITE_OK ){
71239 *pRC = rc;
71240 return;
71241 }
71242 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71243 data = pPage->aData;
71244 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
71245 rc = allocateSpace(pPage, sz, &idx);
71246 if( rc ){ *pRC = rc; return; }
71247 /* The allocateSpace() routine guarantees the following properties
71248 ** if it returns successfully */
71249 assert( idx >= 0 );
71250 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
71251 assert( idx+sz <= (int)pPage->pBt->usableSize );
71252 pPage->nFree -= (u16)(2 + sz);
71253 if( iChild ){
71254 /* In a corrupt database where an entry in the cell index section of
71255 ** a btree page has a value of 3 or less, the pCell value might point
71256 ** as many as 4 bytes in front of the start of the aData buffer for
71257 ** the source page. Make sure this does not cause problems by not
71258 ** reading the first 4 bytes */
71259 memcpy(&data[idx+4], pCell+4, sz-4);
71260 put4byte(&data[idx], iChild);
71261 }else{
71262 memcpy(&data[idx], pCell, sz);
71263 }
71264 pIns = pPage->aCellIdx + i*2;
71265 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
71266 put2byte(pIns, idx);
71267 pPage->nCell++;
71268 /* increment the cell count */
71269 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
71270 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
71271#ifndef SQLITE_OMIT_AUTOVACUUM
71272 if( pPage->pBt->autoVacuum ){
71273 /* The cell may contain a pointer to an overflow page. If so, write
71274 ** the entry for the overflow page into the pointer map.
71275 */
71276 ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
71277 }
71278#endif
71279 }
71280}
71281
71282/*
71283** The following parameters determine how many adjacent pages get involved
71284** in a balancing operation. NN is the number of neighbors on either side
71285** of the page that participate in the balancing operation. NB is the
71286** total number of pages that participate, including the target page and
71287** NN neighbors on either side.
71288**
71289** The minimum value of NN is 1 (of course). Increasing NN above 1
71290** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
71291** in exchange for a larger degradation in INSERT and UPDATE performance.
71292** The value of NN appears to give the best results overall.
71293**
71294** (Later:) The description above makes it seem as if these values are
71295** tunable - as if you could change them and recompile and it would all work.
71296** But that is unlikely. NB has been 3 since the inception of SQLite and
71297** we have never tested any other value.
71298*/
71299#define NN 1 /* Number of neighbors on either side of pPage */
71300#define NB 3 /* (NN*2+1): Total pages involved in the balance */
71301
71302/*
71303** A CellArray object contains a cache of pointers and sizes for a
71304** consecutive sequence of cells that might be held on multiple pages.
71305**
71306** The cells in this array are the divider cell or cells from the pParent
71307** page plus up to three child pages. There are a total of nCell cells.
71308**
71309** pRef is a pointer to one of the pages that contributes cells. This is
71310** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
71311** which should be common to all pages that contribute cells to this array.
71312**
71313** apCell[] and szCell[] hold, respectively, pointers to the start of each
71314** cell and the size of each cell. Some of the apCell[] pointers might refer
71315** to overflow cells. In other words, some apCel[] pointers might not point
71316** to content area of the pages.
71317**
71318** A szCell[] of zero means the size of that cell has not yet been computed.
71319**
71320** The cells come from as many as four different pages:
71321**
71322** -----------
71323** | Parent |
71324** -----------
71325** / | \
71326** / | \
71327** --------- --------- ---------
71328** |Child-1| |Child-2| |Child-3|
71329** --------- --------- ---------
71330**
71331** The order of cells is in the array is for an index btree is:
71332**
71333** 1. All cells from Child-1 in order
71334** 2. The first divider cell from Parent
71335** 3. All cells from Child-2 in order
71336** 4. The second divider cell from Parent
71337** 5. All cells from Child-3 in order
71338**
71339** For a table-btree (with rowids) the items 2 and 4 are empty because
71340** content exists only in leaves and there are no divider cells.
71341**
71342** For an index btree, the apEnd[] array holds pointer to the end of page
71343** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
71344** respectively. The ixNx[] array holds the number of cells contained in
71345** each of these 5 stages, and all stages to the left. Hence:
71346**
71347** ixNx[0] = Number of cells in Child-1.
71348** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
71349** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
71350** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
71351** ixNx[4] = Total number of cells.
71352**
71353** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
71354** are used and they point to the leaf pages only, and the ixNx value are:
71355**
71356** ixNx[0] = Number of cells in Child-1.
71357** ixNx[1] = Number of cells in Child-1 and Child-2.
71358** ixNx[2] = Total number of cells.
71359**
71360** Sometimes when deleting, a child page can have zero cells. In those
71361** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
71362** entries, shift down. The end result is that each ixNx[] entry should
71363** be larger than the previous
71364*/
71365typedef struct CellArray CellArray;
71366struct CellArray {
71367 int nCell; /* Number of cells in apCell[] */
71368 MemPage *pRef; /* Reference page */
71369 u8 **apCell; /* All cells begin balanced */
71370 u16 *szCell; /* Local size of all cells in apCell[] */
71371 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */
71372 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
71373};
71374
71375/*
71376** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
71377** computed.
71378*/
71379static void populateCellCache(CellArray *p, int idx, int N){
71380 assert( idx>=0 && idx+N<=p->nCell );
71381 while( N>0 ){
71382 assert( p->apCell[idx]!=0 );
71383 if( p->szCell[idx]==0 ){
71384 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
71385 }else{
71386 assert( CORRUPT_DB ||
71387 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
71388 }
71389 idx++;
71390 N--;
71391 }
71392}
71393
71394/*
71395** Return the size of the Nth element of the cell array
71396*/
71397static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
71398 assert( N>=0 && N<p->nCell );
71399 assert( p->szCell[N]==0 );
71400 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
71401 return p->szCell[N];
71402}
71403static u16 cachedCellSize(CellArray *p, int N){
71404 assert( N>=0 && N<p->nCell );
71405 if( p->szCell[N] ) return p->szCell[N];
71406 return computeCellSize(p, N);
71407}
71408
71409/*
71410** Array apCell[] contains pointers to nCell b-tree page cells. The
71411** szCell[] array contains the size in bytes of each cell. This function
71412** replaces the current contents of page pPg with the contents of the cell
71413** array.
71414**
71415** Some of the cells in apCell[] may currently be stored in pPg. This
71416** function works around problems caused by this by making a copy of any
71417** such cells before overwriting the page data.
71418**
71419** The MemPage.nFree field is invalidated by this function. It is the
71420** responsibility of the caller to set it correctly.
71421*/
71422static int rebuildPage(
71423 CellArray *pCArray, /* Content to be added to page pPg */
71424 int iFirst, /* First cell in pCArray to use */
71425 int nCell, /* Final number of cells on page */
71426 MemPage *pPg /* The page to be reconstructed */
71427){
71428 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
71429 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
71430 const int usableSize = pPg->pBt->usableSize;
71431 u8 * const pEnd = &aData[usableSize];
71432 int i = iFirst; /* Which cell to copy from pCArray*/
71433 u32 j; /* Start of cell content area */
71434 int iEnd = i+nCell; /* Loop terminator */
71435 u8 *pCellptr = pPg->aCellIdx;
71436 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
71437 u8 *pData;
71438 int k; /* Current slot in pCArray->apEnd[] */
71439 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
71440
71441 assert( i<iEnd );
71442 j = get2byte(&aData[hdr+5]);
71443 if( NEVER(j>(u32)usableSize) ){ j = 0; }
71444 memcpy(&pTmp[j], &aData[j], usableSize - j);
71445
71446 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
71447 pSrcEnd = pCArray->apEnd[k];
71448
71449 pData = pEnd;
71450 while( 1/*exit by break*/ ){
71451 u8 *pCell = pCArray->apCell[i];
71452 u16 sz = pCArray->szCell[i];
71453 assert( sz>0 );
71454 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
71455 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
71456 pCell = &pTmp[pCell - aData];
71457 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
71458 && (uptr)(pCell)<(uptr)pSrcEnd
71459 ){
71460 return SQLITE_CORRUPT_BKPT;
71461 }
71462
71463 pData -= sz;
71464 put2byte(pCellptr, (pData - aData));
71465 pCellptr += 2;
71466 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
71467 memcpy(pData, pCell, sz);
71468 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
71469 testcase( sz!=pPg->xCellSize(pPg,pCell) )
71470 i++;
71471 if( i>=iEnd ) break;
71472 if( pCArray->ixNx[k]<=i ){
71473 k++;
71474 pSrcEnd = pCArray->apEnd[k];
71475 }
71476 }
71477
71478 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
71479 pPg->nCell = nCell;
71480 pPg->nOverflow = 0;
71481
71482 put2byte(&aData[hdr+1], 0);
71483 put2byte(&aData[hdr+3], pPg->nCell);
71484 put2byte(&aData[hdr+5], pData - aData);
71485 aData[hdr+7] = 0x00;
71486 return SQLITE_OK;
71487}
71488
71489/*
71490** The pCArray objects contains pointers to b-tree cells and the cell sizes.
71491** This function attempts to add the cells stored in the array to page pPg.
71492** If it cannot (because the page needs to be defragmented before the cells
71493** will fit), non-zero is returned. Otherwise, if the cells are added
71494** successfully, zero is returned.
71495**
71496** Argument pCellptr points to the first entry in the cell-pointer array
71497** (part of page pPg) to populate. After cell apCell[0] is written to the
71498** page body, a 16-bit offset is written to pCellptr. And so on, for each
71499** cell in the array. It is the responsibility of the caller to ensure
71500** that it is safe to overwrite this part of the cell-pointer array.
71501**
71502** When this function is called, *ppData points to the start of the
71503** content area on page pPg. If the size of the content area is extended,
71504** *ppData is updated to point to the new start of the content area
71505** before returning.
71506**
71507** Finally, argument pBegin points to the byte immediately following the
71508** end of the space required by this page for the cell-pointer area (for
71509** all cells - not just those inserted by the current call). If the content
71510** area must be extended to before this point in order to accomodate all
71511** cells in apCell[], then the cells do not fit and non-zero is returned.
71512*/
71513static int pageInsertArray(
71514 MemPage *pPg, /* Page to add cells to */
71515 u8 *pBegin, /* End of cell-pointer array */
71516 u8 **ppData, /* IN/OUT: Page content-area pointer */
71517 u8 *pCellptr, /* Pointer to cell-pointer area */
71518 int iFirst, /* Index of first cell to add */
71519 int nCell, /* Number of cells to add to pPg */
71520 CellArray *pCArray /* Array of cells */
71521){
71522 int i = iFirst; /* Loop counter - cell index to insert */
71523 u8 *aData = pPg->aData; /* Complete page */
71524 u8 *pData = *ppData; /* Content area. A subset of aData[] */
71525 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
71526 int k; /* Current slot in pCArray->apEnd[] */
71527 u8 *pEnd; /* Maximum extent of cell data */
71528 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
71529 if( iEnd<=iFirst ) return 0;
71530 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
71531 pEnd = pCArray->apEnd[k];
71532 while( 1 /*Exit by break*/ ){
71533 int sz, rc;
71534 u8 *pSlot;
71535 assert( pCArray->szCell[i]!=0 );
71536 sz = pCArray->szCell[i];
71537 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
71538 if( (pData - pBegin)<sz ) return 1;
71539 pData -= sz;
71540 pSlot = pData;
71541 }
71542 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
71543 ** database. But they might for a corrupt database. Hence use memmove()
71544 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
71545 assert( (pSlot+sz)<=pCArray->apCell[i]
71546 || pSlot>=(pCArray->apCell[i]+sz)
71547 || CORRUPT_DB );
71548 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
71549 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
71550 ){
71551 assert( CORRUPT_DB );
71552 (void)SQLITE_CORRUPT_BKPT;
71553 return 1;
71554 }
71555 memmove(pSlot, pCArray->apCell[i], sz);
71556 put2byte(pCellptr, (pSlot - aData));
71557 pCellptr += 2;
71558 i++;
71559 if( i>=iEnd ) break;
71560 if( pCArray->ixNx[k]<=i ){
71561 k++;
71562 pEnd = pCArray->apEnd[k];
71563 }
71564 }
71565 *ppData = pData;
71566 return 0;
71567}
71568
71569/*
71570** The pCArray object contains pointers to b-tree cells and their sizes.
71571**
71572** This function adds the space associated with each cell in the array
71573** that is currently stored within the body of pPg to the pPg free-list.
71574** The cell-pointers and other fields of the page are not updated.
71575**
71576** This function returns the total number of cells added to the free-list.
71577*/
71578static int pageFreeArray(
71579 MemPage *pPg, /* Page to edit */
71580 int iFirst, /* First cell to delete */
71581 int nCell, /* Cells to delete */
71582 CellArray *pCArray /* Array of cells */
71583){
71584 u8 * const aData = pPg->aData;
71585 u8 * const pEnd = &aData[pPg->pBt->usableSize];
71586 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
71587 int nRet = 0;
71588 int i;
71589 int iEnd = iFirst + nCell;
71590 u8 *pFree = 0;
71591 int szFree = 0;
71592
71593 for(i=iFirst; i<iEnd; i++){
71594 u8 *pCell = pCArray->apCell[i];
71595 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
71596 int sz;
71597 /* No need to use cachedCellSize() here. The sizes of all cells that
71598 ** are to be freed have already been computing while deciding which
71599 ** cells need freeing */
71600 sz = pCArray->szCell[i]; assert( sz>0 );
71601 if( pFree!=(pCell + sz) ){
71602 if( pFree ){
71603 assert( pFree>aData && (pFree - aData)<65536 );
71604 freeSpace(pPg, (u16)(pFree - aData), szFree);
71605 }
71606 pFree = pCell;
71607 szFree = sz;
71608 if( pFree+sz>pEnd ) return 0;
71609 }else{
71610 pFree = pCell;
71611 szFree += sz;
71612 }
71613 nRet++;
71614 }
71615 }
71616 if( pFree ){
71617 assert( pFree>aData && (pFree - aData)<65536 );
71618 freeSpace(pPg, (u16)(pFree - aData), szFree);
71619 }
71620 return nRet;
71621}
71622
71623/*
71624** pCArray contains pointers to and sizes of all cells in the page being
71625** balanced. The current page, pPg, has pPg->nCell cells starting with
71626** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
71627** starting at apCell[iNew].
71628**
71629** This routine makes the necessary adjustments to pPg so that it contains
71630** the correct cells after being balanced.
71631**
71632** The pPg->nFree field is invalid when this function returns. It is the
71633** responsibility of the caller to set it correctly.
71634*/
71635static int editPage(
71636 MemPage *pPg, /* Edit this page */
71637 int iOld, /* Index of first cell currently on page */
71638 int iNew, /* Index of new first cell on page */
71639 int nNew, /* Final number of cells on page */
71640 CellArray *pCArray /* Array of cells and sizes */
71641){
71642 u8 * const aData = pPg->aData;
71643 const int hdr = pPg->hdrOffset;
71644 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
71645 int nCell = pPg->nCell; /* Cells stored on pPg */
71646 u8 *pData;
71647 u8 *pCellptr;
71648 int i;
71649 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
71650 int iNewEnd = iNew + nNew;
71651
71652#ifdef SQLITE_DEBUG
71653 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
71654 memcpy(pTmp, aData, pPg->pBt->usableSize);
71655#endif
71656
71657 /* Remove cells from the start and end of the page */
71658 assert( nCell>=0 );
71659 if( iOld<iNew ){
71660 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
71661 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
71662 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
71663 nCell -= nShift;
71664 }
71665 if( iNewEnd < iOldEnd ){
71666 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
71667 assert( nCell>=nTail );
71668 nCell -= nTail;
71669 }
71670
71671 pData = &aData[get2byteNotZero(&aData[hdr+5])];
71672 if( pData<pBegin ) goto editpage_fail;
71673
71674 /* Add cells to the start of the page */
71675 if( iNew<iOld ){
71676 int nAdd = MIN(nNew,iOld-iNew);
71677 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
71678 assert( nAdd>=0 );
71679 pCellptr = pPg->aCellIdx;
71680 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
71681 if( pageInsertArray(
71682 pPg, pBegin, &pData, pCellptr,
71683 iNew, nAdd, pCArray
71684 ) ) goto editpage_fail;
71685 nCell += nAdd;
71686 }
71687
71688 /* Add any overflow cells */
71689 for(i=0; i<pPg->nOverflow; i++){
71690 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
71691 if( iCell>=0 && iCell<nNew ){
71692 pCellptr = &pPg->aCellIdx[iCell * 2];
71693 if( nCell>iCell ){
71694 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
71695 }
71696 nCell++;
71697 cachedCellSize(pCArray, iCell+iNew);
71698 if( pageInsertArray(
71699 pPg, pBegin, &pData, pCellptr,
71700 iCell+iNew, 1, pCArray
71701 ) ) goto editpage_fail;
71702 }
71703 }
71704
71705 /* Append cells to the end of the page */
71706 assert( nCell>=0 );
71707 pCellptr = &pPg->aCellIdx[nCell*2];
71708 if( pageInsertArray(
71709 pPg, pBegin, &pData, pCellptr,
71710 iNew+nCell, nNew-nCell, pCArray
71711 ) ) goto editpage_fail;
71712
71713 pPg->nCell = nNew;
71714 pPg->nOverflow = 0;
71715
71716 put2byte(&aData[hdr+3], pPg->nCell);
71717 put2byte(&aData[hdr+5], pData - aData);
71718
71719#ifdef SQLITE_DEBUG
71720 for(i=0; i<nNew && !CORRUPT_DB; i++){
71721 u8 *pCell = pCArray->apCell[i+iNew];
71722 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
71723 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
71724 pCell = &pTmp[pCell - aData];
71725 }
71726 assert( 0==memcmp(pCell, &aData[iOff],
71727 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
71728 }
71729#endif
71730
71731 return SQLITE_OK;
71732 editpage_fail:
71733 /* Unable to edit this page. Rebuild it from scratch instead. */
71734 populateCellCache(pCArray, iNew, nNew);
71735 return rebuildPage(pCArray, iNew, nNew, pPg);
71736}
71737
71738
71739#ifndef SQLITE_OMIT_QUICKBALANCE
71740/*
71741** This version of balance() handles the common special case where
71742** a new entry is being inserted on the extreme right-end of the
71743** tree, in other words, when the new entry will become the largest
71744** entry in the tree.
71745**
71746** Instead of trying to balance the 3 right-most leaf pages, just add
71747** a new page to the right-hand side and put the one new entry in
71748** that page. This leaves the right side of the tree somewhat
71749** unbalanced. But odds are that we will be inserting new entries
71750** at the end soon afterwards so the nearly empty page will quickly
71751** fill up. On average.
71752**
71753** pPage is the leaf page which is the right-most page in the tree.
71754** pParent is its parent. pPage must have a single overflow entry
71755** which is also the right-most entry on the page.
71756**
71757** The pSpace buffer is used to store a temporary copy of the divider
71758** cell that will be inserted into pParent. Such a cell consists of a 4
71759** byte page number followed by a variable length integer. In other
71760** words, at most 13 bytes. Hence the pSpace buffer must be at
71761** least 13 bytes in size.
71762*/
71763static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
71764 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
71765 MemPage *pNew; /* Newly allocated page */
71766 int rc; /* Return Code */
71767 Pgno pgnoNew; /* Page number of pNew */
71768
71769 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71770 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
71771 assert( pPage->nOverflow==1 );
71772
71773 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
71774 assert( pPage->nFree>=0 );
71775 assert( pParent->nFree>=0 );
71776
71777 /* Allocate a new page. This page will become the right-sibling of
71778 ** pPage. Make the parent page writable, so that the new divider cell
71779 ** may be inserted. If both these operations are successful, proceed.
71780 */
71781 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
71782
71783 if( rc==SQLITE_OK ){
71784
71785 u8 *pOut = &pSpace[4];
71786 u8 *pCell = pPage->apOvfl[0];
71787 u16 szCell = pPage->xCellSize(pPage, pCell);
71788 u8 *pStop;
71789 CellArray b;
71790
71791 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
71792 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
71793 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
71794 b.nCell = 1;
71795 b.pRef = pPage;
71796 b.apCell = &pCell;
71797 b.szCell = &szCell;
71798 b.apEnd[0] = pPage->aDataEnd;
71799 b.ixNx[0] = 2;
71800 rc = rebuildPage(&b, 0, 1, pNew);
71801 if( NEVER(rc) ){
71802 releasePage(pNew);
71803 return rc;
71804 }
71805 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
71806
71807 /* If this is an auto-vacuum database, update the pointer map
71808 ** with entries for the new page, and any pointer from the
71809 ** cell on the page to an overflow page. If either of these
71810 ** operations fails, the return code is set, but the contents
71811 ** of the parent page are still manipulated by thh code below.
71812 ** That is Ok, at this point the parent page is guaranteed to
71813 ** be marked as dirty. Returning an error code will cause a
71814 ** rollback, undoing any changes made to the parent page.
71815 */
71816 if( ISAUTOVACUUM ){
71817 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
71818 if( szCell>pNew->minLocal ){
71819 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
71820 }
71821 }
71822
71823 /* Create a divider cell to insert into pParent. The divider cell
71824 ** consists of a 4-byte page number (the page number of pPage) and
71825 ** a variable length key value (which must be the same value as the
71826 ** largest key on pPage).
71827 **
71828 ** To find the largest key value on pPage, first find the right-most
71829 ** cell on pPage. The first two fields of this cell are the
71830 ** record-length (a variable length integer at most 32-bits in size)
71831 ** and the key value (a variable length integer, may have any value).
71832 ** The first of the while(...) loops below skips over the record-length
71833 ** field. The second while(...) loop copies the key value from the
71834 ** cell on pPage into the pSpace buffer.
71835 */
71836 pCell = findCell(pPage, pPage->nCell-1);
71837 pStop = &pCell[9];
71838 while( (*(pCell++)&0x80) && pCell<pStop );
71839 pStop = &pCell[9];
71840 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
71841
71842 /* Insert the new divider cell into pParent. */
71843 if( rc==SQLITE_OK ){
71844 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
71845 0, pPage->pgno, &rc);
71846 }
71847
71848 /* Set the right-child pointer of pParent to point to the new page. */
71849 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
71850
71851 /* Release the reference to the new page. */
71852 releasePage(pNew);
71853 }
71854
71855 return rc;
71856}
71857#endif /* SQLITE_OMIT_QUICKBALANCE */
71858
71859#if 0
71860/*
71861** This function does not contribute anything to the operation of SQLite.
71862** it is sometimes activated temporarily while debugging code responsible
71863** for setting pointer-map entries.
71864*/
71865static int ptrmapCheckPages(MemPage **apPage, int nPage){
71866 int i, j;
71867 for(i=0; i<nPage; i++){
71868 Pgno n;
71869 u8 e;
71870 MemPage *pPage = apPage[i];
71871 BtShared *pBt = pPage->pBt;
71872 assert( pPage->isInit );
71873
71874 for(j=0; j<pPage->nCell; j++){
71875 CellInfo info;
71876 u8 *z;
71877
71878 z = findCell(pPage, j);
71879 pPage->xParseCell(pPage, z, &info);
71880 if( info.nLocal<info.nPayload ){
71881 Pgno ovfl = get4byte(&z[info.nSize-4]);
71882 ptrmapGet(pBt, ovfl, &e, &n);
71883 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
71884 }
71885 if( !pPage->leaf ){
71886 Pgno child = get4byte(z);
71887 ptrmapGet(pBt, child, &e, &n);
71888 assert( n==pPage->pgno && e==PTRMAP_BTREE );
71889 }
71890 }
71891 if( !pPage->leaf ){
71892 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
71893 ptrmapGet(pBt, child, &e, &n);
71894 assert( n==pPage->pgno && e==PTRMAP_BTREE );
71895 }
71896 }
71897 return 1;
71898}
71899#endif
71900
71901/*
71902** This function is used to copy the contents of the b-tree node stored
71903** on page pFrom to page pTo. If page pFrom was not a leaf page, then
71904** the pointer-map entries for each child page are updated so that the
71905** parent page stored in the pointer map is page pTo. If pFrom contained
71906** any cells with overflow page pointers, then the corresponding pointer
71907** map entries are also updated so that the parent page is page pTo.
71908**
71909** If pFrom is currently carrying any overflow cells (entries in the
71910** MemPage.apOvfl[] array), they are not copied to pTo.
71911**
71912** Before returning, page pTo is reinitialized using btreeInitPage().
71913**
71914** The performance of this function is not critical. It is only used by
71915** the balance_shallower() and balance_deeper() procedures, neither of
71916** which are called often under normal circumstances.
71917*/
71918static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
71919 if( (*pRC)==SQLITE_OK ){
71920 BtShared * const pBt = pFrom->pBt;
71921 u8 * const aFrom = pFrom->aData;
71922 u8 * const aTo = pTo->aData;
71923 int const iFromHdr = pFrom->hdrOffset;
71924 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
71925 int rc;
71926 int iData;
71927
71928
71929 assert( pFrom->isInit );
71930 assert( pFrom->nFree>=iToHdr );
71931 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
71932
71933 /* Copy the b-tree node content from page pFrom to page pTo. */
71934 iData = get2byte(&aFrom[iFromHdr+5]);
71935 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
71936 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
71937
71938 /* Reinitialize page pTo so that the contents of the MemPage structure
71939 ** match the new data. The initialization of pTo can actually fail under
71940 ** fairly obscure circumstances, even though it is a copy of initialized
71941 ** page pFrom.
71942 */
71943 pTo->isInit = 0;
71944 rc = btreeInitPage(pTo);
71945 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
71946 if( rc!=SQLITE_OK ){
71947 *pRC = rc;
71948 return;
71949 }
71950
71951 /* If this is an auto-vacuum database, update the pointer-map entries
71952 ** for any b-tree or overflow pages that pTo now contains the pointers to.
71953 */
71954 if( ISAUTOVACUUM ){
71955 *pRC = setChildPtrmaps(pTo);
71956 }
71957 }
71958}
71959
71960/*
71961** This routine redistributes cells on the iParentIdx'th child of pParent
71962** (hereafter "the page") and up to 2 siblings so that all pages have about the
71963** same amount of free space. Usually a single sibling on either side of the
71964** page are used in the balancing, though both siblings might come from one
71965** side if the page is the first or last child of its parent. If the page
71966** has fewer than 2 siblings (something which can only happen if the page
71967** is a root page or a child of a root page) then all available siblings
71968** participate in the balancing.
71969**
71970** The number of siblings of the page might be increased or decreased by
71971** one or two in an effort to keep pages nearly full but not over full.
71972**
71973** Note that when this routine is called, some of the cells on the page
71974** might not actually be stored in MemPage.aData[]. This can happen
71975** if the page is overfull. This routine ensures that all cells allocated
71976** to the page and its siblings fit into MemPage.aData[] before returning.
71977**
71978** In the course of balancing the page and its siblings, cells may be
71979** inserted into or removed from the parent page (pParent). Doing so
71980** may cause the parent page to become overfull or underfull. If this
71981** happens, it is the responsibility of the caller to invoke the correct
71982** balancing routine to fix this problem (see the balance() routine).
71983**
71984** If this routine fails for any reason, it might leave the database
71985** in a corrupted state. So if this routine fails, the database should
71986** be rolled back.
71987**
71988** The third argument to this function, aOvflSpace, is a pointer to a
71989** buffer big enough to hold one page. If while inserting cells into the parent
71990** page (pParent) the parent page becomes overfull, this buffer is
71991** used to store the parent's overflow cells. Because this function inserts
71992** a maximum of four divider cells into the parent page, and the maximum
71993** size of a cell stored within an internal node is always less than 1/4
71994** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
71995** enough for all overflow cells.
71996**
71997** If aOvflSpace is set to a null pointer, this function returns
71998** SQLITE_NOMEM.
71999*/
72000static int balance_nonroot(
72001 MemPage *pParent, /* Parent page of siblings being balanced */
72002 int iParentIdx, /* Index of "the page" in pParent */
72003 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
72004 int isRoot, /* True if pParent is a root-page */
72005 int bBulk /* True if this call is part of a bulk load */
72006){
72007 BtShared *pBt; /* The whole database */
72008 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
72009 int nNew = 0; /* Number of pages in apNew[] */
72010 int nOld; /* Number of pages in apOld[] */
72011 int i, j, k; /* Loop counters */
72012 int nxDiv; /* Next divider slot in pParent->aCell[] */
72013 int rc = SQLITE_OK; /* The return code */
72014 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
72015 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
72016 int usableSpace; /* Bytes in pPage beyond the header */
72017 int pageFlags; /* Value of pPage->aData[0] */
72018 int iSpace1 = 0; /* First unused byte of aSpace1[] */
72019 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
72020 int szScratch; /* Size of scratch memory requested */
72021 MemPage *apOld[NB]; /* pPage and up to two siblings */
72022 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
72023 u8 *pRight; /* Location in parent of right-sibling pointer */
72024 u8 *apDiv[NB-1]; /* Divider cells in pParent */
72025 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
72026 int cntOld[NB+2]; /* Old index in b.apCell[] */
72027 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
72028 u8 *aSpace1; /* Space for copies of dividers cells */
72029 Pgno pgno; /* Temp var to store a page number in */
72030 u8 abDone[NB+2]; /* True after i'th new page is populated */
72031 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
72032 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
72033 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
72034 CellArray b; /* Parsed information on cells being balanced */
72035
72036 memset(abDone, 0, sizeof(abDone));
72037 b.nCell = 0;
72038 b.apCell = 0;
72039 pBt = pParent->pBt;
72040 assert( sqlite3_mutex_held(pBt->mutex) );
72041 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72042
72043 /* At this point pParent may have at most one overflow cell. And if
72044 ** this overflow cell is present, it must be the cell with
72045 ** index iParentIdx. This scenario comes about when this function
72046 ** is called (indirectly) from sqlite3BtreeDelete().
72047 */
72048 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
72049 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
72050
72051 if( !aOvflSpace ){
72052 return SQLITE_NOMEM_BKPT;
72053 }
72054 assert( pParent->nFree>=0 );
72055
72056 /* Find the sibling pages to balance. Also locate the cells in pParent
72057 ** that divide the siblings. An attempt is made to find NN siblings on
72058 ** either side of pPage. More siblings are taken from one side, however,
72059 ** if there are fewer than NN siblings on the other side. If pParent
72060 ** has NB or fewer children then all children of pParent are taken.
72061 **
72062 ** This loop also drops the divider cells from the parent page. This
72063 ** way, the remainder of the function does not have to deal with any
72064 ** overflow cells in the parent page, since if any existed they will
72065 ** have already been removed.
72066 */
72067 i = pParent->nOverflow + pParent->nCell;
72068 if( i<2 ){
72069 nxDiv = 0;
72070 }else{
72071 assert( bBulk==0 || bBulk==1 );
72072 if( iParentIdx==0 ){
72073 nxDiv = 0;
72074 }else if( iParentIdx==i ){
72075 nxDiv = i-2+bBulk;
72076 }else{
72077 nxDiv = iParentIdx-1;
72078 }
72079 i = 2-bBulk;
72080 }
72081 nOld = i+1;
72082 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
72083 pRight = &pParent->aData[pParent->hdrOffset+8];
72084 }else{
72085 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
72086 }
72087 pgno = get4byte(pRight);
72088 while( 1 ){
72089 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
72090 if( rc ){
72091 memset(apOld, 0, (i+1)*sizeof(MemPage*));
72092 goto balance_cleanup;
72093 }
72094 if( apOld[i]->nFree<0 ){
72095 rc = btreeComputeFreeSpace(apOld[i]);
72096 if( rc ){
72097 memset(apOld, 0, (i)*sizeof(MemPage*));
72098 goto balance_cleanup;
72099 }
72100 }
72101 if( (i--)==0 ) break;
72102
72103 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
72104 apDiv[i] = pParent->apOvfl[0];
72105 pgno = get4byte(apDiv[i]);
72106 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
72107 pParent->nOverflow = 0;
72108 }else{
72109 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
72110 pgno = get4byte(apDiv[i]);
72111 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
72112
72113 /* Drop the cell from the parent page. apDiv[i] still points to
72114 ** the cell within the parent, even though it has been dropped.
72115 ** This is safe because dropping a cell only overwrites the first
72116 ** four bytes of it, and this function does not need the first
72117 ** four bytes of the divider cell. So the pointer is safe to use
72118 ** later on.
72119 **
72120 ** But not if we are in secure-delete mode. In secure-delete mode,
72121 ** the dropCell() routine will overwrite the entire cell with zeroes.
72122 ** In this case, temporarily copy the cell into the aOvflSpace[]
72123 ** buffer. It will be copied out again as soon as the aSpace[] buffer
72124 ** is allocated. */
72125 if( pBt->btsFlags & BTS_FAST_SECURE ){
72126 int iOff;
72127
72128 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
72129 if( (iOff+szNew[i])>(int)pBt->usableSize ){
72130 rc = SQLITE_CORRUPT_BKPT;
72131 memset(apOld, 0, (i+1)*sizeof(MemPage*));
72132 goto balance_cleanup;
72133 }else{
72134 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
72135 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
72136 }
72137 }
72138 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
72139 }
72140 }
72141
72142 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
72143 ** alignment */
72144 nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
72145 nMaxCells = (nMaxCells + 3)&~3;
72146
72147 /*
72148 ** Allocate space for memory structures
72149 */
72150 szScratch =
72151 nMaxCells*sizeof(u8*) /* b.apCell */
72152 + nMaxCells*sizeof(u16) /* b.szCell */
72153 + pBt->pageSize; /* aSpace1 */
72154
72155 assert( szScratch<=7*(int)pBt->pageSize );
72156 b.apCell = sqlite3StackAllocRaw(0, szScratch );
72157 if( b.apCell==0 ){
72158 rc = SQLITE_NOMEM_BKPT;
72159 goto balance_cleanup;
72160 }
72161 b.szCell = (u16*)&b.apCell[nMaxCells];
72162 aSpace1 = (u8*)&b.szCell[nMaxCells];
72163 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
72164
72165 /*
72166 ** Load pointers to all cells on sibling pages and the divider cells
72167 ** into the local b.apCell[] array. Make copies of the divider cells
72168 ** into space obtained from aSpace1[]. The divider cells have already
72169 ** been removed from pParent.
72170 **
72171 ** If the siblings are on leaf pages, then the child pointers of the
72172 ** divider cells are stripped from the cells before they are copied
72173 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
72174 ** child pointers. If siblings are not leaves, then all cell in
72175 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
72176 ** are alike.
72177 **
72178 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
72179 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
72180 */
72181 b.pRef = apOld[0];
72182 leafCorrection = b.pRef->leaf*4;
72183 leafData = b.pRef->intKeyLeaf;
72184 for(i=0; i<nOld; i++){
72185 MemPage *pOld = apOld[i];
72186 int limit = pOld->nCell;
72187 u8 *aData = pOld->aData;
72188 u16 maskPage = pOld->maskPage;
72189 u8 *piCell = aData + pOld->cellOffset;
72190 u8 *piEnd;
72191 VVA_ONLY( int nCellAtStart = b.nCell; )
72192
72193 /* Verify that all sibling pages are of the same "type" (table-leaf,
72194 ** table-interior, index-leaf, or index-interior).
72195 */
72196 if( pOld->aData[0]!=apOld[0]->aData[0] ){
72197 rc = SQLITE_CORRUPT_BKPT;
72198 goto balance_cleanup;
72199 }
72200
72201 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
72202 ** contains overflow cells, include them in the b.apCell[] array
72203 ** in the correct spot.
72204 **
72205 ** Note that when there are multiple overflow cells, it is always the
72206 ** case that they are sequential and adjacent. This invariant arises
72207 ** because multiple overflows can only occurs when inserting divider
72208 ** cells into a parent on a prior balance, and divider cells are always
72209 ** adjacent and are inserted in order. There is an assert() tagged
72210 ** with "NOTE 1" in the overflow cell insertion loop to prove this
72211 ** invariant.
72212 **
72213 ** This must be done in advance. Once the balance starts, the cell
72214 ** offset section of the btree page will be overwritten and we will no
72215 ** long be able to find the cells if a pointer to each cell is not saved
72216 ** first.
72217 */
72218 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
72219 if( pOld->nOverflow>0 ){
72220 if( NEVER(limit<pOld->aiOvfl[0]) ){
72221 rc = SQLITE_CORRUPT_BKPT;
72222 goto balance_cleanup;
72223 }
72224 limit = pOld->aiOvfl[0];
72225 for(j=0; j<limit; j++){
72226 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
72227 piCell += 2;
72228 b.nCell++;
72229 }
72230 for(k=0; k<pOld->nOverflow; k++){
72231 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
72232 b.apCell[b.nCell] = pOld->apOvfl[k];
72233 b.nCell++;
72234 }
72235 }
72236 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
72237 while( piCell<piEnd ){
72238 assert( b.nCell<nMaxCells );
72239 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
72240 piCell += 2;
72241 b.nCell++;
72242 }
72243 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
72244
72245 cntOld[i] = b.nCell;
72246 if( i<nOld-1 && !leafData){
72247 u16 sz = (u16)szNew[i];
72248 u8 *pTemp;
72249 assert( b.nCell<nMaxCells );
72250 b.szCell[b.nCell] = sz;
72251 pTemp = &aSpace1[iSpace1];
72252 iSpace1 += sz;
72253 assert( sz<=pBt->maxLocal+23 );
72254 assert( iSpace1 <= (int)pBt->pageSize );
72255 memcpy(pTemp, apDiv[i], sz);
72256 b.apCell[b.nCell] = pTemp+leafCorrection;
72257 assert( leafCorrection==0 || leafCorrection==4 );
72258 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
72259 if( !pOld->leaf ){
72260 assert( leafCorrection==0 );
72261 assert( pOld->hdrOffset==0 );
72262 /* The right pointer of the child page pOld becomes the left
72263 ** pointer of the divider cell */
72264 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
72265 }else{
72266 assert( leafCorrection==4 );
72267 while( b.szCell[b.nCell]<4 ){
72268 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
72269 ** does exist, pad it with 0x00 bytes. */
72270 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
72271 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
72272 aSpace1[iSpace1++] = 0x00;
72273 b.szCell[b.nCell]++;
72274 }
72275 }
72276 b.nCell++;
72277 }
72278 }
72279
72280 /*
72281 ** Figure out the number of pages needed to hold all b.nCell cells.
72282 ** Store this number in "k". Also compute szNew[] which is the total
72283 ** size of all cells on the i-th page and cntNew[] which is the index
72284 ** in b.apCell[] of the cell that divides page i from page i+1.
72285 ** cntNew[k] should equal b.nCell.
72286 **
72287 ** Values computed by this block:
72288 **
72289 ** k: The total number of sibling pages
72290 ** szNew[i]: Spaced used on the i-th sibling page.
72291 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
72292 ** the right of the i-th sibling page.
72293 ** usableSpace: Number of bytes of space available on each sibling.
72294 **
72295 */
72296 usableSpace = pBt->usableSize - 12 + leafCorrection;
72297 for(i=k=0; i<nOld; i++, k++){
72298 MemPage *p = apOld[i];
72299 b.apEnd[k] = p->aDataEnd;
72300 b.ixNx[k] = cntOld[i];
72301 if( k && b.ixNx[k]==b.ixNx[k-1] ){
72302 k--; /* Omit b.ixNx[] entry for child pages with no cells */
72303 }
72304 if( !leafData ){
72305 k++;
72306 b.apEnd[k] = pParent->aDataEnd;
72307 b.ixNx[k] = cntOld[i]+1;
72308 }
72309 assert( p->nFree>=0 );
72310 szNew[i] = usableSpace - p->nFree;
72311 for(j=0; j<p->nOverflow; j++){
72312 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
72313 }
72314 cntNew[i] = cntOld[i];
72315 }
72316 k = nOld;
72317 for(i=0; i<k; i++){
72318 int sz;
72319 while( szNew[i]>usableSpace ){
72320 if( i+1>=k ){
72321 k = i+2;
72322 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
72323 szNew[k-1] = 0;
72324 cntNew[k-1] = b.nCell;
72325 }
72326 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
72327 szNew[i] -= sz;
72328 if( !leafData ){
72329 if( cntNew[i]<b.nCell ){
72330 sz = 2 + cachedCellSize(&b, cntNew[i]);
72331 }else{
72332 sz = 0;
72333 }
72334 }
72335 szNew[i+1] += sz;
72336 cntNew[i]--;
72337 }
72338 while( cntNew[i]<b.nCell ){
72339 sz = 2 + cachedCellSize(&b, cntNew[i]);
72340 if( szNew[i]+sz>usableSpace ) break;
72341 szNew[i] += sz;
72342 cntNew[i]++;
72343 if( !leafData ){
72344 if( cntNew[i]<b.nCell ){
72345 sz = 2 + cachedCellSize(&b, cntNew[i]);
72346 }else{
72347 sz = 0;
72348 }
72349 }
72350 szNew[i+1] -= sz;
72351 }
72352 if( cntNew[i]>=b.nCell ){
72353 k = i+1;
72354 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
72355 rc = SQLITE_CORRUPT_BKPT;
72356 goto balance_cleanup;
72357 }
72358 }
72359
72360 /*
72361 ** The packing computed by the previous block is biased toward the siblings
72362 ** on the left side (siblings with smaller keys). The left siblings are
72363 ** always nearly full, while the right-most sibling might be nearly empty.
72364 ** The next block of code attempts to adjust the packing of siblings to
72365 ** get a better balance.
72366 **
72367 ** This adjustment is more than an optimization. The packing above might
72368 ** be so out of balance as to be illegal. For example, the right-most
72369 ** sibling might be completely empty. This adjustment is not optional.
72370 */
72371 for(i=k-1; i>0; i--){
72372 int szRight = szNew[i]; /* Size of sibling on the right */
72373 int szLeft = szNew[i-1]; /* Size of sibling on the left */
72374 int r; /* Index of right-most cell in left sibling */
72375 int d; /* Index of first cell to the left of right sibling */
72376
72377 r = cntNew[i-1] - 1;
72378 d = r + 1 - leafData;
72379 (void)cachedCellSize(&b, d);
72380 do{
72381 assert( d<nMaxCells );
72382 assert( r<nMaxCells );
72383 (void)cachedCellSize(&b, r);
72384 if( szRight!=0
72385 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
72386 break;
72387 }
72388 szRight += b.szCell[d] + 2;
72389 szLeft -= b.szCell[r] + 2;
72390 cntNew[i-1] = r;
72391 r--;
72392 d--;
72393 }while( r>=0 );
72394 szNew[i] = szRight;
72395 szNew[i-1] = szLeft;
72396 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
72397 rc = SQLITE_CORRUPT_BKPT;
72398 goto balance_cleanup;
72399 }
72400 }
72401
72402 /* Sanity check: For a non-corrupt database file one of the follwing
72403 ** must be true:
72404 ** (1) We found one or more cells (cntNew[0])>0), or
72405 ** (2) pPage is a virtual root page. A virtual root page is when
72406 ** the real root page is page 1 and we are the only child of
72407 ** that page.
72408 */
72409 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
72410 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
72411 apOld[0]->pgno, apOld[0]->nCell,
72412 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
72413 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
72414 ));
72415
72416 /*
72417 ** Allocate k new pages. Reuse old pages where possible.
72418 */
72419 pageFlags = apOld[0]->aData[0];
72420 for(i=0; i<k; i++){
72421 MemPage *pNew;
72422 if( i<nOld ){
72423 pNew = apNew[i] = apOld[i];
72424 apOld[i] = 0;
72425 rc = sqlite3PagerWrite(pNew->pDbPage);
72426 nNew++;
72427 if( rc ) goto balance_cleanup;
72428 }else{
72429 assert( i>0 );
72430 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
72431 if( rc ) goto balance_cleanup;
72432 zeroPage(pNew, pageFlags);
72433 apNew[i] = pNew;
72434 nNew++;
72435 cntOld[i] = b.nCell;
72436
72437 /* Set the pointer-map entry for the new sibling page. */
72438 if( ISAUTOVACUUM ){
72439 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
72440 if( rc!=SQLITE_OK ){
72441 goto balance_cleanup;
72442 }
72443 }
72444 }
72445 }
72446
72447 /*
72448 ** Reassign page numbers so that the new pages are in ascending order.
72449 ** This helps to keep entries in the disk file in order so that a scan
72450 ** of the table is closer to a linear scan through the file. That in turn
72451 ** helps the operating system to deliver pages from the disk more rapidly.
72452 **
72453 ** An O(n^2) insertion sort algorithm is used, but since n is never more
72454 ** than (NB+2) (a small constant), that should not be a problem.
72455 **
72456 ** When NB==3, this one optimization makes the database about 25% faster
72457 ** for large insertions and deletions.
72458 */
72459 for(i=0; i<nNew; i++){
72460 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
72461 aPgFlags[i] = apNew[i]->pDbPage->flags;
72462 for(j=0; j<i; j++){
72463 if( aPgno[j]==aPgno[i] ){
72464 /* This branch is taken if the set of sibling pages somehow contains
72465 ** duplicate entries. This can happen if the database is corrupt.
72466 ** It would be simpler to detect this as part of the loop below, but
72467 ** we do the detection here in order to avoid populating the pager
72468 ** cache with two separate objects associated with the same
72469 ** page number. */
72470 assert( CORRUPT_DB );
72471 rc = SQLITE_CORRUPT_BKPT;
72472 goto balance_cleanup;
72473 }
72474 }
72475 }
72476 for(i=0; i<nNew; i++){
72477 int iBest = 0; /* aPgno[] index of page number to use */
72478 for(j=1; j<nNew; j++){
72479 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
72480 }
72481 pgno = aPgOrder[iBest];
72482 aPgOrder[iBest] = 0xffffffff;
72483 if( iBest!=i ){
72484 if( iBest>i ){
72485 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
72486 }
72487 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
72488 apNew[i]->pgno = pgno;
72489 }
72490 }
72491
72492 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
72493 "%d(%d nc=%d) %d(%d nc=%d)\n",
72494 apNew[0]->pgno, szNew[0], cntNew[0],
72495 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
72496 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
72497 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
72498 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
72499 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
72500 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
72501 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
72502 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
72503 ));
72504
72505 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72506 assert( nNew>=1 && nNew<=ArraySize(apNew) );
72507 assert( apNew[nNew-1]!=0 );
72508 put4byte(pRight, apNew[nNew-1]->pgno);
72509
72510 /* If the sibling pages are not leaves, ensure that the right-child pointer
72511 ** of the right-most new sibling page is set to the value that was
72512 ** originally in the same field of the right-most old sibling page. */
72513 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
72514 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
72515 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
72516 }
72517
72518 /* Make any required updates to pointer map entries associated with
72519 ** cells stored on sibling pages following the balance operation. Pointer
72520 ** map entries associated with divider cells are set by the insertCell()
72521 ** routine. The associated pointer map entries are:
72522 **
72523 ** a) if the cell contains a reference to an overflow chain, the
72524 ** entry associated with the first page in the overflow chain, and
72525 **
72526 ** b) if the sibling pages are not leaves, the child page associated
72527 ** with the cell.
72528 **
72529 ** If the sibling pages are not leaves, then the pointer map entry
72530 ** associated with the right-child of each sibling may also need to be
72531 ** updated. This happens below, after the sibling pages have been
72532 ** populated, not here.
72533 */
72534 if( ISAUTOVACUUM ){
72535 MemPage *pOld;
72536 MemPage *pNew = pOld = apNew[0];
72537 int cntOldNext = pNew->nCell + pNew->nOverflow;
72538 int iNew = 0;
72539 int iOld = 0;
72540
72541 for(i=0; i<b.nCell; i++){
72542 u8 *pCell = b.apCell[i];
72543 while( i==cntOldNext ){
72544 iOld++;
72545 assert( iOld<nNew || iOld<nOld );
72546 assert( iOld>=0 && iOld<NB );
72547 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
72548 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
72549 }
72550 if( i==cntNew[iNew] ){
72551 pNew = apNew[++iNew];
72552 if( !leafData ) continue;
72553 }
72554
72555 /* Cell pCell is destined for new sibling page pNew. Originally, it
72556 ** was either part of sibling page iOld (possibly an overflow cell),
72557 ** or else the divider cell to the left of sibling page iOld. So,
72558 ** if sibling page iOld had the same page number as pNew, and if
72559 ** pCell really was a part of sibling page iOld (not a divider or
72560 ** overflow cell), we can skip updating the pointer map entries. */
72561 if( iOld>=nNew
72562 || pNew->pgno!=aPgno[iOld]
72563 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
72564 ){
72565 if( !leafCorrection ){
72566 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
72567 }
72568 if( cachedCellSize(&b,i)>pNew->minLocal ){
72569 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
72570 }
72571 if( rc ) goto balance_cleanup;
72572 }
72573 }
72574 }
72575
72576 /* Insert new divider cells into pParent. */
72577 for(i=0; i<nNew-1; i++){
72578 u8 *pCell;
72579 u8 *pTemp;
72580 int sz;
72581 MemPage *pNew = apNew[i];
72582 j = cntNew[i];
72583
72584 assert( j<nMaxCells );
72585 assert( b.apCell[j]!=0 );
72586 pCell = b.apCell[j];
72587 sz = b.szCell[j] + leafCorrection;
72588 pTemp = &aOvflSpace[iOvflSpace];
72589 if( !pNew->leaf ){
72590 memcpy(&pNew->aData[8], pCell, 4);
72591 }else if( leafData ){
72592 /* If the tree is a leaf-data tree, and the siblings are leaves,
72593 ** then there is no divider cell in b.apCell[]. Instead, the divider
72594 ** cell consists of the integer key for the right-most cell of
72595 ** the sibling-page assembled above only.
72596 */
72597 CellInfo info;
72598 j--;
72599 pNew->xParseCell(pNew, b.apCell[j], &info);
72600 pCell = pTemp;
72601 sz = 4 + putVarint(&pCell[4], info.nKey);
72602 pTemp = 0;
72603 }else{
72604 pCell -= 4;
72605 /* Obscure case for non-leaf-data trees: If the cell at pCell was
72606 ** previously stored on a leaf node, and its reported size was 4
72607 ** bytes, then it may actually be smaller than this
72608 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
72609 ** any cell). But it is important to pass the correct size to
72610 ** insertCell(), so reparse the cell now.
72611 **
72612 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
72613 ** and WITHOUT ROWID tables with exactly one column which is the
72614 ** primary key.
72615 */
72616 if( b.szCell[j]==4 ){
72617 assert(leafCorrection==4);
72618 sz = pParent->xCellSize(pParent, pCell);
72619 }
72620 }
72621 iOvflSpace += sz;
72622 assert( sz<=pBt->maxLocal+23 );
72623 assert( iOvflSpace <= (int)pBt->pageSize );
72624 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
72625 if( rc!=SQLITE_OK ) goto balance_cleanup;
72626 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72627 }
72628
72629 /* Now update the actual sibling pages. The order in which they are updated
72630 ** is important, as this code needs to avoid disrupting any page from which
72631 ** cells may still to be read. In practice, this means:
72632 **
72633 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
72634 ** then it is not safe to update page apNew[iPg] until after
72635 ** the left-hand sibling apNew[iPg-1] has been updated.
72636 **
72637 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
72638 ** then it is not safe to update page apNew[iPg] until after
72639 ** the right-hand sibling apNew[iPg+1] has been updated.
72640 **
72641 ** If neither of the above apply, the page is safe to update.
72642 **
72643 ** The iPg value in the following loop starts at nNew-1 goes down
72644 ** to 0, then back up to nNew-1 again, thus making two passes over
72645 ** the pages. On the initial downward pass, only condition (1) above
72646 ** needs to be tested because (2) will always be true from the previous
72647 ** step. On the upward pass, both conditions are always true, so the
72648 ** upwards pass simply processes pages that were missed on the downward
72649 ** pass.
72650 */
72651 for(i=1-nNew; i<nNew; i++){
72652 int iPg = i<0 ? -i : i;
72653 assert( iPg>=0 && iPg<nNew );
72654 if( abDone[iPg] ) continue; /* Skip pages already processed */
72655 if( i>=0 /* On the upwards pass, or... */
72656 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
72657 ){
72658 int iNew;
72659 int iOld;
72660 int nNewCell;
72661
72662 /* Verify condition (1): If cells are moving left, update iPg
72663 ** only after iPg-1 has already been updated. */
72664 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
72665
72666 /* Verify condition (2): If cells are moving right, update iPg
72667 ** only after iPg+1 has already been updated. */
72668 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
72669
72670 if( iPg==0 ){
72671 iNew = iOld = 0;
72672 nNewCell = cntNew[0];
72673 }else{
72674 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
72675 iNew = cntNew[iPg-1] + !leafData;
72676 nNewCell = cntNew[iPg] - iNew;
72677 }
72678
72679 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
72680 if( rc ) goto balance_cleanup;
72681 abDone[iPg]++;
72682 apNew[iPg]->nFree = usableSpace-szNew[iPg];
72683 assert( apNew[iPg]->nOverflow==0 );
72684 assert( apNew[iPg]->nCell==nNewCell );
72685 }
72686 }
72687
72688 /* All pages have been processed exactly once */
72689 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
72690
72691 assert( nOld>0 );
72692 assert( nNew>0 );
72693
72694 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
72695 /* The root page of the b-tree now contains no cells. The only sibling
72696 ** page is the right-child of the parent. Copy the contents of the
72697 ** child page into the parent, decreasing the overall height of the
72698 ** b-tree structure by one. This is described as the "balance-shallower"
72699 ** sub-algorithm in some documentation.
72700 **
72701 ** If this is an auto-vacuum database, the call to copyNodeContent()
72702 ** sets all pointer-map entries corresponding to database image pages
72703 ** for which the pointer is stored within the content being copied.
72704 **
72705 ** It is critical that the child page be defragmented before being
72706 ** copied into the parent, because if the parent is page 1 then it will
72707 ** by smaller than the child due to the database header, and so all the
72708 ** free space needs to be up front.
72709 */
72710 assert( nNew==1 || CORRUPT_DB );
72711 rc = defragmentPage(apNew[0], -1);
72712 testcase( rc!=SQLITE_OK );
72713 assert( apNew[0]->nFree ==
72714 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
72715 - apNew[0]->nCell*2)
72716 || rc!=SQLITE_OK
72717 );
72718 copyNodeContent(apNew[0], pParent, &rc);
72719 freePage(apNew[0], &rc);
72720 }else if( ISAUTOVACUUM && !leafCorrection ){
72721 /* Fix the pointer map entries associated with the right-child of each
72722 ** sibling page. All other pointer map entries have already been taken
72723 ** care of. */
72724 for(i=0; i<nNew; i++){
72725 u32 key = get4byte(&apNew[i]->aData[8]);
72726 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
72727 }
72728 }
72729
72730 assert( pParent->isInit );
72731 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
72732 nOld, nNew, b.nCell));
72733
72734 /* Free any old pages that were not reused as new pages.
72735 */
72736 for(i=nNew; i<nOld; i++){
72737 freePage(apOld[i], &rc);
72738 }
72739
72740#if 0
72741 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
72742 /* The ptrmapCheckPages() contains assert() statements that verify that
72743 ** all pointer map pages are set correctly. This is helpful while
72744 ** debugging. This is usually disabled because a corrupt database may
72745 ** cause an assert() statement to fail. */
72746 ptrmapCheckPages(apNew, nNew);
72747 ptrmapCheckPages(&pParent, 1);
72748 }
72749#endif
72750
72751 /*
72752 ** Cleanup before returning.
72753 */
72754balance_cleanup:
72755 sqlite3StackFree(0, b.apCell);
72756 for(i=0; i<nOld; i++){
72757 releasePage(apOld[i]);
72758 }
72759 for(i=0; i<nNew; i++){
72760 releasePage(apNew[i]);
72761 }
72762
72763 return rc;
72764}
72765
72766
72767/*
72768** This function is called when the root page of a b-tree structure is
72769** overfull (has one or more overflow pages).
72770**
72771** A new child page is allocated and the contents of the current root
72772** page, including overflow cells, are copied into the child. The root
72773** page is then overwritten to make it an empty page with the right-child
72774** pointer pointing to the new page.
72775**
72776** Before returning, all pointer-map entries corresponding to pages
72777** that the new child-page now contains pointers to are updated. The
72778** entry corresponding to the new right-child pointer of the root
72779** page is also updated.
72780**
72781** If successful, *ppChild is set to contain a reference to the child
72782** page and SQLITE_OK is returned. In this case the caller is required
72783** to call releasePage() on *ppChild exactly once. If an error occurs,
72784** an error code is returned and *ppChild is set to 0.
72785*/
72786static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
72787 int rc; /* Return value from subprocedures */
72788 MemPage *pChild = 0; /* Pointer to a new child page */
72789 Pgno pgnoChild = 0; /* Page number of the new child page */
72790 BtShared *pBt = pRoot->pBt; /* The BTree */
72791
72792 assert( pRoot->nOverflow>0 );
72793 assert( sqlite3_mutex_held(pBt->mutex) );
72794
72795 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
72796 ** page that will become the new right-child of pPage. Copy the contents
72797 ** of the node stored on pRoot into the new child page.
72798 */
72799 rc = sqlite3PagerWrite(pRoot->pDbPage);
72800 if( rc==SQLITE_OK ){
72801 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
72802 copyNodeContent(pRoot, pChild, &rc);
72803 if( ISAUTOVACUUM ){
72804 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
72805 }
72806 }
72807 if( rc ){
72808 *ppChild = 0;
72809 releasePage(pChild);
72810 return rc;
72811 }
72812 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
72813 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
72814 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
72815
72816 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
72817
72818 /* Copy the overflow cells from pRoot to pChild */
72819 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
72820 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
72821 memcpy(pChild->apOvfl, pRoot->apOvfl,
72822 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
72823 pChild->nOverflow = pRoot->nOverflow;
72824
72825 /* Zero the contents of pRoot. Then install pChild as the right-child. */
72826 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
72827 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
72828
72829 *ppChild = pChild;
72830 return SQLITE_OK;
72831}
72832
72833/*
72834** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
72835** on the same B-tree as pCur.
72836**
72837** This can if a database is corrupt with two or more SQL tables
72838** pointing to the same b-tree. If an insert occurs on one SQL table
72839** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
72840** table linked to the same b-tree. If the secondary insert causes a
72841** rebalance, that can change content out from under the cursor on the
72842** first SQL table, violating invariants on the first insert.
72843*/
72844static int anotherValidCursor(BtCursor *pCur){
72845 BtCursor *pOther;
72846 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
72847 if( pOther!=pCur
72848 && pOther->eState==CURSOR_VALID
72849 && pOther->pPage==pCur->pPage
72850 ){
72851 return SQLITE_CORRUPT_BKPT;
72852 }
72853 }
72854 return SQLITE_OK;
72855}
72856
72857/*
72858** The page that pCur currently points to has just been modified in
72859** some way. This function figures out if this modification means the
72860** tree needs to be balanced, and if so calls the appropriate balancing
72861** routine. Balancing routines are:
72862**
72863** balance_quick()
72864** balance_deeper()
72865** balance_nonroot()
72866*/
72867static int balance(BtCursor *pCur){
72868 int rc = SQLITE_OK;
72869 const int nMin = pCur->pBt->usableSize * 2 / 3;
72870 u8 aBalanceQuickSpace[13];
72871 u8 *pFree = 0;
72872
72873 VVA_ONLY( int balance_quick_called = 0 );
72874 VVA_ONLY( int balance_deeper_called = 0 );
72875
72876 do {
72877 int iPage;
72878 MemPage *pPage = pCur->pPage;
72879
72880 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
72881 if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
72882 break;
72883 }else if( (iPage = pCur->iPage)==0 ){
72884 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
72885 /* The root page of the b-tree is overfull. In this case call the
72886 ** balance_deeper() function to create a new child for the root-page
72887 ** and copy the current contents of the root-page to it. The
72888 ** next iteration of the do-loop will balance the child page.
72889 */
72890 assert( balance_deeper_called==0 );
72891 VVA_ONLY( balance_deeper_called++ );
72892 rc = balance_deeper(pPage, &pCur->apPage[1]);
72893 if( rc==SQLITE_OK ){
72894 pCur->iPage = 1;
72895 pCur->ix = 0;
72896 pCur->aiIdx[0] = 0;
72897 pCur->apPage[0] = pPage;
72898 pCur->pPage = pCur->apPage[1];
72899 assert( pCur->pPage->nOverflow );
72900 }
72901 }else{
72902 break;
72903 }
72904 }else{
72905 MemPage * const pParent = pCur->apPage[iPage-1];
72906 int const iIdx = pCur->aiIdx[iPage-1];
72907
72908 rc = sqlite3PagerWrite(pParent->pDbPage);
72909 if( rc==SQLITE_OK && pParent->nFree<0 ){
72910 rc = btreeComputeFreeSpace(pParent);
72911 }
72912 if( rc==SQLITE_OK ){
72913#ifndef SQLITE_OMIT_QUICKBALANCE
72914 if( pPage->intKeyLeaf
72915 && pPage->nOverflow==1
72916 && pPage->aiOvfl[0]==pPage->nCell
72917 && pParent->pgno!=1
72918 && pParent->nCell==iIdx
72919 ){
72920 /* Call balance_quick() to create a new sibling of pPage on which
72921 ** to store the overflow cell. balance_quick() inserts a new cell
72922 ** into pParent, which may cause pParent overflow. If this
72923 ** happens, the next iteration of the do-loop will balance pParent
72924 ** use either balance_nonroot() or balance_deeper(). Until this
72925 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
72926 ** buffer.
72927 **
72928 ** The purpose of the following assert() is to check that only a
72929 ** single call to balance_quick() is made for each call to this
72930 ** function. If this were not verified, a subtle bug involving reuse
72931 ** of the aBalanceQuickSpace[] might sneak in.
72932 */
72933 assert( balance_quick_called==0 );
72934 VVA_ONLY( balance_quick_called++ );
72935 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
72936 }else
72937#endif
72938 {
72939 /* In this case, call balance_nonroot() to redistribute cells
72940 ** between pPage and up to 2 of its sibling pages. This involves
72941 ** modifying the contents of pParent, which may cause pParent to
72942 ** become overfull or underfull. The next iteration of the do-loop
72943 ** will balance the parent page to correct this.
72944 **
72945 ** If the parent page becomes overfull, the overflow cell or cells
72946 ** are stored in the pSpace buffer allocated immediately below.
72947 ** A subsequent iteration of the do-loop will deal with this by
72948 ** calling balance_nonroot() (balance_deeper() may be called first,
72949 ** but it doesn't deal with overflow cells - just moves them to a
72950 ** different page). Once this subsequent call to balance_nonroot()
72951 ** has completed, it is safe to release the pSpace buffer used by
72952 ** the previous call, as the overflow cell data will have been
72953 ** copied either into the body of a database page or into the new
72954 ** pSpace buffer passed to the latter call to balance_nonroot().
72955 */
72956 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
72957 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
72958 pCur->hints&BTREE_BULKLOAD);
72959 if( pFree ){
72960 /* If pFree is not NULL, it points to the pSpace buffer used
72961 ** by a previous call to balance_nonroot(). Its contents are
72962 ** now stored either on real database pages or within the
72963 ** new pSpace buffer, so it may be safely freed here. */
72964 sqlite3PageFree(pFree);
72965 }
72966
72967 /* The pSpace buffer will be freed after the next call to
72968 ** balance_nonroot(), or just before this function returns, whichever
72969 ** comes first. */
72970 pFree = pSpace;
72971 }
72972 }
72973
72974 pPage->nOverflow = 0;
72975
72976 /* The next iteration of the do-loop balances the parent page. */
72977 releasePage(pPage);
72978 pCur->iPage--;
72979 assert( pCur->iPage>=0 );
72980 pCur->pPage = pCur->apPage[pCur->iPage];
72981 }
72982 }while( rc==SQLITE_OK );
72983
72984 if( pFree ){
72985 sqlite3PageFree(pFree);
72986 }
72987 return rc;
72988}
72989
72990/* Overwrite content from pX into pDest. Only do the write if the
72991** content is different from what is already there.
72992*/
72993static int btreeOverwriteContent(
72994 MemPage *pPage, /* MemPage on which writing will occur */
72995 u8 *pDest, /* Pointer to the place to start writing */
72996 const BtreePayload *pX, /* Source of data to write */
72997 int iOffset, /* Offset of first byte to write */
72998 int iAmt /* Number of bytes to be written */
72999){
73000 int nData = pX->nData - iOffset;
73001 if( nData<=0 ){
73002 /* Overwritting with zeros */
73003 int i;
73004 for(i=0; i<iAmt && pDest[i]==0; i++){}
73005 if( i<iAmt ){
73006 int rc = sqlite3PagerWrite(pPage->pDbPage);
73007 if( rc ) return rc;
73008 memset(pDest + i, 0, iAmt - i);
73009 }
73010 }else{
73011 if( nData<iAmt ){
73012 /* Mixed read data and zeros at the end. Make a recursive call
73013 ** to write the zeros then fall through to write the real data */
73014 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
73015 iAmt-nData);
73016 if( rc ) return rc;
73017 iAmt = nData;
73018 }
73019 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
73020 int rc = sqlite3PagerWrite(pPage->pDbPage);
73021 if( rc ) return rc;
73022 /* In a corrupt database, it is possible for the source and destination
73023 ** buffers to overlap. This is harmless since the database is already
73024 ** corrupt but it does cause valgrind and ASAN warnings. So use
73025 ** memmove(). */
73026 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
73027 }
73028 }
73029 return SQLITE_OK;
73030}
73031
73032/*
73033** Overwrite the cell that cursor pCur is pointing to with fresh content
73034** contained in pX.
73035*/
73036static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
73037 int iOffset; /* Next byte of pX->pData to write */
73038 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
73039 int rc; /* Return code */
73040 MemPage *pPage = pCur->pPage; /* Page being written */
73041 BtShared *pBt; /* Btree */
73042 Pgno ovflPgno; /* Next overflow page to write */
73043 u32 ovflPageSize; /* Size to write on overflow page */
73044
73045 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
73046 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
73047 ){
73048 return SQLITE_CORRUPT_BKPT;
73049 }
73050 /* Overwrite the local portion first */
73051 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
73052 0, pCur->info.nLocal);
73053 if( rc ) return rc;
73054 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
73055
73056 /* Now overwrite the overflow pages */
73057 iOffset = pCur->info.nLocal;
73058 assert( nTotal>=0 );
73059 assert( iOffset>=0 );
73060 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
73061 pBt = pPage->pBt;
73062 ovflPageSize = pBt->usableSize - 4;
73063 do{
73064 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
73065 if( rc ) return rc;
73066 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
73067 rc = SQLITE_CORRUPT_BKPT;
73068 }else{
73069 if( iOffset+ovflPageSize<(u32)nTotal ){
73070 ovflPgno = get4byte(pPage->aData);
73071 }else{
73072 ovflPageSize = nTotal - iOffset;
73073 }
73074 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
73075 iOffset, ovflPageSize);
73076 }
73077 sqlite3PagerUnref(pPage->pDbPage);
73078 if( rc ) return rc;
73079 iOffset += ovflPageSize;
73080 }while( iOffset<nTotal );
73081 return SQLITE_OK;
73082}
73083
73084
73085/*
73086** Insert a new record into the BTree. The content of the new record
73087** is described by the pX object. The pCur cursor is used only to
73088** define what table the record should be inserted into, and is left
73089** pointing at a random location.
73090**
73091** For a table btree (used for rowid tables), only the pX.nKey value of
73092** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
73093** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
73094** hold the content of the row.
73095**
73096** For an index btree (used for indexes and WITHOUT ROWID tables), the
73097** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
73098** pX.pData,nData,nZero fields must be zero.
73099**
73100** If the seekResult parameter is non-zero, then a successful call to
73101** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
73102** been performed. In other words, if seekResult!=0 then the cursor
73103** is currently pointing to a cell that will be adjacent to the cell
73104** to be inserted. If seekResult<0 then pCur points to a cell that is
73105** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
73106** that is larger than (pKey,nKey).
73107**
73108** If seekResult==0, that means pCur is pointing at some unknown location.
73109** In that case, this routine must seek the cursor to the correct insertion
73110** point for (pKey,nKey) before doing the insertion. For index btrees,
73111** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
73112** key values and pX->aMem can be used instead of pX->pKey to avoid having
73113** to decode the key.
73114*/
73115SQLITE_PRIVATE int sqlite3BtreeInsert(
73116 BtCursor *pCur, /* Insert data into the table of this cursor */
73117 const BtreePayload *pX, /* Content of the row to be inserted */
73118 int flags, /* True if this is likely an append */
73119 int seekResult /* Result of prior MovetoUnpacked() call */
73120){
73121 int rc;
73122 int loc = seekResult; /* -1: before desired location +1: after */
73123 int szNew = 0;
73124 int idx;
73125 MemPage *pPage;
73126 Btree *p = pCur->pBtree;
73127 BtShared *pBt = p->pBt;
73128 unsigned char *oldCell;
73129 unsigned char *newCell = 0;
73130
73131 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
73132
73133 if( pCur->eState==CURSOR_FAULT ){
73134 assert( pCur->skipNext!=SQLITE_OK );
73135 return pCur->skipNext;
73136 }
73137
73138 assert( cursorOwnsBtShared(pCur) );
73139 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
73140 && pBt->inTransaction==TRANS_WRITE
73141 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
73142 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
73143
73144 /* Assert that the caller has been consistent. If this cursor was opened
73145 ** expecting an index b-tree, then the caller should be inserting blob
73146 ** keys with no associated data. If the cursor was opened expecting an
73147 ** intkey table, the caller should be inserting integer keys with a
73148 ** blob of associated data. */
73149 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
73150
73151 /* Save the positions of any other cursors open on this table.
73152 **
73153 ** In some cases, the call to btreeMoveto() below is a no-op. For
73154 ** example, when inserting data into a table with auto-generated integer
73155 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
73156 ** integer key to use. It then calls this function to actually insert the
73157 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
73158 ** that the cursor is already where it needs to be and returns without
73159 ** doing any work. To avoid thwarting these optimizations, it is important
73160 ** not to clear the cursor here.
73161 */
73162 if( pCur->curFlags & BTCF_Multiple ){
73163 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
73164 if( rc ) return rc;
73165 }
73166
73167 if( pCur->pKeyInfo==0 ){
73168 assert( pX->pKey==0 );
73169 /* If this is an insert into a table b-tree, invalidate any incrblob
73170 ** cursors open on the row being replaced */
73171 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
73172
73173 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
73174 ** to a row with the same key as the new entry being inserted.
73175 */
73176#ifdef SQLITE_DEBUG
73177 if( flags & BTREE_SAVEPOSITION ){
73178 assert( pCur->curFlags & BTCF_ValidNKey );
73179 assert( pX->nKey==pCur->info.nKey );
73180 assert( loc==0 );
73181 }
73182#endif
73183
73184 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
73185 ** that the cursor is not pointing to a row to be overwritten.
73186 ** So do a complete check.
73187 */
73188 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
73189 /* The cursor is pointing to the entry that is to be
73190 ** overwritten */
73191 assert( pX->nData>=0 && pX->nZero>=0 );
73192 if( pCur->info.nSize!=0
73193 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
73194 ){
73195 /* New entry is the same size as the old. Do an overwrite */
73196 return btreeOverwriteCell(pCur, pX);
73197 }
73198 assert( loc==0 );
73199 }else if( loc==0 ){
73200 /* The cursor is *not* pointing to the cell to be overwritten, nor
73201 ** to an adjacent cell. Move the cursor so that it is pointing either
73202 ** to the cell to be overwritten or an adjacent cell.
73203 */
73204 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
73205 if( rc ) return rc;
73206 }
73207 }else{
73208 /* This is an index or a WITHOUT ROWID table */
73209
73210 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
73211 ** to a row with the same key as the new entry being inserted.
73212 */
73213 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
73214
73215 /* If the cursor is not already pointing either to the cell to be
73216 ** overwritten, or if a new cell is being inserted, if the cursor is
73217 ** not pointing to an immediately adjacent cell, then move the cursor
73218 ** so that it does.
73219 */
73220 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
73221 if( pX->nMem ){
73222 UnpackedRecord r;
73223 r.pKeyInfo = pCur->pKeyInfo;
73224 r.aMem = pX->aMem;
73225 r.nField = pX->nMem;
73226 r.default_rc = 0;
73227 r.errCode = 0;
73228 r.r1 = 0;
73229 r.r2 = 0;
73230 r.eqSeen = 0;
73231 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
73232 }else{
73233 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
73234 }
73235 if( rc ) return rc;
73236 }
73237
73238 /* If the cursor is currently pointing to an entry to be overwritten
73239 ** and the new content is the same as as the old, then use the
73240 ** overwrite optimization.
73241 */
73242 if( loc==0 ){
73243 getCellInfo(pCur);
73244 if( pCur->info.nKey==pX->nKey ){
73245 BtreePayload x2;
73246 x2.pData = pX->pKey;
73247 x2.nData = pX->nKey;
73248 x2.nZero = 0;
73249 return btreeOverwriteCell(pCur, &x2);
73250 }
73251 }
73252
73253 }
73254 assert( pCur->eState==CURSOR_VALID
73255 || (pCur->eState==CURSOR_INVALID && loc)
73256 || CORRUPT_DB );
73257
73258 pPage = pCur->pPage;
73259 assert( pPage->intKey || pX->nKey>=0 );
73260 assert( pPage->leaf || !pPage->intKey );
73261 if( pPage->nFree<0 ){
73262 if( pCur->eState>CURSOR_INVALID ){
73263 rc = SQLITE_CORRUPT_BKPT;
73264 }else{
73265 rc = btreeComputeFreeSpace(pPage);
73266 }
73267 if( rc ) return rc;
73268 }
73269
73270 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
73271 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
73272 loc==0 ? "overwrite" : "new entry"));
73273 assert( pPage->isInit );
73274 newCell = pBt->pTmpSpace;
73275 assert( newCell!=0 );
73276 rc = fillInCell(pPage, newCell, pX, &szNew);
73277 if( rc ) goto end_insert;
73278 assert( szNew==pPage->xCellSize(pPage, newCell) );
73279 assert( szNew <= MX_CELL_SIZE(pBt) );
73280 idx = pCur->ix;
73281 if( loc==0 ){
73282 CellInfo info;
73283 assert( idx<pPage->nCell );
73284 rc = sqlite3PagerWrite(pPage->pDbPage);
73285 if( rc ){
73286 goto end_insert;
73287 }
73288 oldCell = findCell(pPage, idx);
73289 if( !pPage->leaf ){
73290 memcpy(newCell, oldCell, 4);
73291 }
73292 rc = clearCell(pPage, oldCell, &info);
73293 testcase( pCur->curFlags & BTCF_ValidOvfl );
73294 invalidateOverflowCache(pCur);
73295 if( info.nSize==szNew && info.nLocal==info.nPayload
73296 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
73297 ){
73298 /* Overwrite the old cell with the new if they are the same size.
73299 ** We could also try to do this if the old cell is smaller, then add
73300 ** the leftover space to the free list. But experiments show that
73301 ** doing that is no faster then skipping this optimization and just
73302 ** calling dropCell() and insertCell().
73303 **
73304 ** This optimization cannot be used on an autovacuum database if the
73305 ** new entry uses overflow pages, as the insertCell() call below is
73306 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
73307 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
73308 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
73309 return SQLITE_CORRUPT_BKPT;
73310 }
73311 if( oldCell+szNew > pPage->aDataEnd ){
73312 return SQLITE_CORRUPT_BKPT;
73313 }
73314 memcpy(oldCell, newCell, szNew);
73315 return SQLITE_OK;
73316 }
73317 dropCell(pPage, idx, info.nSize, &rc);
73318 if( rc ) goto end_insert;
73319 }else if( loc<0 && pPage->nCell>0 ){
73320 assert( pPage->leaf );
73321 idx = ++pCur->ix;
73322 pCur->curFlags &= ~BTCF_ValidNKey;
73323 }else{
73324 assert( pPage->leaf );
73325 }
73326 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
73327 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
73328 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
73329
73330 /* If no error has occurred and pPage has an overflow cell, call balance()
73331 ** to redistribute the cells within the tree. Since balance() may move
73332 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
73333 ** variables.
73334 **
73335 ** Previous versions of SQLite called moveToRoot() to move the cursor
73336 ** back to the root page as balance() used to invalidate the contents
73337 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
73338 ** set the cursor state to "invalid". This makes common insert operations
73339 ** slightly faster.
73340 **
73341 ** There is a subtle but important optimization here too. When inserting
73342 ** multiple records into an intkey b-tree using a single cursor (as can
73343 ** happen while processing an "INSERT INTO ... SELECT" statement), it
73344 ** is advantageous to leave the cursor pointing to the last entry in
73345 ** the b-tree if possible. If the cursor is left pointing to the last
73346 ** entry in the table, and the next row inserted has an integer key
73347 ** larger than the largest existing key, it is possible to insert the
73348 ** row without seeking the cursor. This can be a big performance boost.
73349 */
73350 pCur->info.nSize = 0;
73351 if( pPage->nOverflow ){
73352 assert( rc==SQLITE_OK );
73353 pCur->curFlags &= ~(BTCF_ValidNKey);
73354 rc = balance(pCur);
73355
73356 /* Must make sure nOverflow is reset to zero even if the balance()
73357 ** fails. Internal data structure corruption will result otherwise.
73358 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
73359 ** from trying to save the current position of the cursor. */
73360 pCur->pPage->nOverflow = 0;
73361 pCur->eState = CURSOR_INVALID;
73362 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
73363 btreeReleaseAllCursorPages(pCur);
73364 if( pCur->pKeyInfo ){
73365 assert( pCur->pKey==0 );
73366 pCur->pKey = sqlite3Malloc( pX->nKey );
73367 if( pCur->pKey==0 ){
73368 rc = SQLITE_NOMEM;
73369 }else{
73370 memcpy(pCur->pKey, pX->pKey, pX->nKey);
73371 }
73372 }
73373 pCur->eState = CURSOR_REQUIRESEEK;
73374 pCur->nKey = pX->nKey;
73375 }
73376 }
73377 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
73378
73379end_insert:
73380 return rc;
73381}
73382
73383/*
73384** Delete the entry that the cursor is pointing to.
73385**
73386** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
73387** the cursor is left pointing at an arbitrary location after the delete.
73388** But if that bit is set, then the cursor is left in a state such that
73389** the next call to BtreeNext() or BtreePrev() moves it to the same row
73390** as it would have been on if the call to BtreeDelete() had been omitted.
73391**
73392** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
73393** associated with a single table entry and its indexes. Only one of those
73394** deletes is considered the "primary" delete. The primary delete occurs
73395** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
73396** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
73397** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
73398** but which might be used by alternative storage engines.
73399*/
73400SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
73401 Btree *p = pCur->pBtree;
73402 BtShared *pBt = p->pBt;
73403 int rc; /* Return code */
73404 MemPage *pPage; /* Page to delete cell from */
73405 unsigned char *pCell; /* Pointer to cell to delete */
73406 int iCellIdx; /* Index of cell to delete */
73407 int iCellDepth; /* Depth of node containing pCell */
73408 CellInfo info; /* Size of the cell being deleted */
73409 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
73410 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
73411
73412 assert( cursorOwnsBtShared(pCur) );
73413 assert( pBt->inTransaction==TRANS_WRITE );
73414 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73415 assert( pCur->curFlags & BTCF_WriteFlag );
73416 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
73417 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
73418 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
73419 if( pCur->eState==CURSOR_REQUIRESEEK ){
73420 rc = btreeRestoreCursorPosition(pCur);
73421 if( rc ) return rc;
73422 }
73423 assert( pCur->eState==CURSOR_VALID );
73424
73425 iCellDepth = pCur->iPage;
73426 iCellIdx = pCur->ix;
73427 pPage = pCur->pPage;
73428 pCell = findCell(pPage, iCellIdx);
73429 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
73430
73431 /* If the bPreserve flag is set to true, then the cursor position must
73432 ** be preserved following this delete operation. If the current delete
73433 ** will cause a b-tree rebalance, then this is done by saving the cursor
73434 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
73435 ** returning.
73436 **
73437 ** Or, if the current delete will not cause a rebalance, then the cursor
73438 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
73439 ** before or after the deleted entry. In this case set bSkipnext to true. */
73440 if( bPreserve ){
73441 if( !pPage->leaf
73442 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
73443 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
73444 ){
73445 /* A b-tree rebalance will be required after deleting this entry.
73446 ** Save the cursor key. */
73447 rc = saveCursorKey(pCur);
73448 if( rc ) return rc;
73449 }else{
73450 bSkipnext = 1;
73451 }
73452 }
73453
73454 /* If the page containing the entry to delete is not a leaf page, move
73455 ** the cursor to the largest entry in the tree that is smaller than
73456 ** the entry being deleted. This cell will replace the cell being deleted
73457 ** from the internal node. The 'previous' entry is used for this instead
73458 ** of the 'next' entry, as the previous entry is always a part of the
73459 ** sub-tree headed by the child page of the cell being deleted. This makes
73460 ** balancing the tree following the delete operation easier. */
73461 if( !pPage->leaf ){
73462 rc = sqlite3BtreePrevious(pCur, 0);
73463 assert( rc!=SQLITE_DONE );
73464 if( rc ) return rc;
73465 }
73466
73467 /* Save the positions of any other cursors open on this table before
73468 ** making any modifications. */
73469 if( pCur->curFlags & BTCF_Multiple ){
73470 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
73471 if( rc ) return rc;
73472 }
73473
73474 /* If this is a delete operation to remove a row from a table b-tree,
73475 ** invalidate any incrblob cursors open on the row being deleted. */
73476 if( pCur->pKeyInfo==0 ){
73477 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
73478 }
73479
73480 /* Make the page containing the entry to be deleted writable. Then free any
73481 ** overflow pages associated with the entry and finally remove the cell
73482 ** itself from within the page. */
73483 rc = sqlite3PagerWrite(pPage->pDbPage);
73484 if( rc ) return rc;
73485 rc = clearCell(pPage, pCell, &info);
73486 dropCell(pPage, iCellIdx, info.nSize, &rc);
73487 if( rc ) return rc;
73488
73489 /* If the cell deleted was not located on a leaf page, then the cursor
73490 ** is currently pointing to the largest entry in the sub-tree headed
73491 ** by the child-page of the cell that was just deleted from an internal
73492 ** node. The cell from the leaf node needs to be moved to the internal
73493 ** node to replace the deleted cell. */
73494 if( !pPage->leaf ){
73495 MemPage *pLeaf = pCur->pPage;
73496 int nCell;
73497 Pgno n;
73498 unsigned char *pTmp;
73499
73500 if( pLeaf->nFree<0 ){
73501 rc = btreeComputeFreeSpace(pLeaf);
73502 if( rc ) return rc;
73503 }
73504 if( iCellDepth<pCur->iPage-1 ){
73505 n = pCur->apPage[iCellDepth+1]->pgno;
73506 }else{
73507 n = pCur->pPage->pgno;
73508 }
73509 pCell = findCell(pLeaf, pLeaf->nCell-1);
73510 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
73511 nCell = pLeaf->xCellSize(pLeaf, pCell);
73512 assert( MX_CELL_SIZE(pBt) >= nCell );
73513 pTmp = pBt->pTmpSpace;
73514 assert( pTmp!=0 );
73515 rc = sqlite3PagerWrite(pLeaf->pDbPage);
73516 if( rc==SQLITE_OK ){
73517 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
73518 }
73519 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
73520 if( rc ) return rc;
73521 }
73522
73523 /* Balance the tree. If the entry deleted was located on a leaf page,
73524 ** then the cursor still points to that page. In this case the first
73525 ** call to balance() repairs the tree, and the if(...) condition is
73526 ** never true.
73527 **
73528 ** Otherwise, if the entry deleted was on an internal node page, then
73529 ** pCur is pointing to the leaf page from which a cell was removed to
73530 ** replace the cell deleted from the internal node. This is slightly
73531 ** tricky as the leaf node may be underfull, and the internal node may
73532 ** be either under or overfull. In this case run the balancing algorithm
73533 ** on the leaf node first. If the balance proceeds far enough up the
73534 ** tree that we can be sure that any problem in the internal node has
73535 ** been corrected, so be it. Otherwise, after balancing the leaf node,
73536 ** walk the cursor up the tree to the internal node and balance it as
73537 ** well. */
73538 rc = balance(pCur);
73539 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
73540 releasePageNotNull(pCur->pPage);
73541 pCur->iPage--;
73542 while( pCur->iPage>iCellDepth ){
73543 releasePage(pCur->apPage[pCur->iPage--]);
73544 }
73545 pCur->pPage = pCur->apPage[pCur->iPage];
73546 rc = balance(pCur);
73547 }
73548
73549 if( rc==SQLITE_OK ){
73550 if( bSkipnext ){
73551 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
73552 assert( pPage==pCur->pPage || CORRUPT_DB );
73553 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
73554 pCur->eState = CURSOR_SKIPNEXT;
73555 if( iCellIdx>=pPage->nCell ){
73556 pCur->skipNext = -1;
73557 pCur->ix = pPage->nCell-1;
73558 }else{
73559 pCur->skipNext = 1;
73560 }
73561 }else{
73562 rc = moveToRoot(pCur);
73563 if( bPreserve ){
73564 btreeReleaseAllCursorPages(pCur);
73565 pCur->eState = CURSOR_REQUIRESEEK;
73566 }
73567 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
73568 }
73569 }
73570 return rc;
73571}
73572
73573/*
73574** Create a new BTree table. Write into *piTable the page
73575** number for the root page of the new table.
73576**
73577** The type of type is determined by the flags parameter. Only the
73578** following values of flags are currently in use. Other values for
73579** flags might not work:
73580**
73581** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
73582** BTREE_ZERODATA Used for SQL indices
73583*/
73584static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
73585 BtShared *pBt = p->pBt;
73586 MemPage *pRoot;
73587 Pgno pgnoRoot;
73588 int rc;
73589 int ptfFlags; /* Page-type flage for the root page of new table */
73590
73591 assert( sqlite3BtreeHoldsMutex(p) );
73592 assert( pBt->inTransaction==TRANS_WRITE );
73593 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73594
73595#ifdef SQLITE_OMIT_AUTOVACUUM
73596 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
73597 if( rc ){
73598 return rc;
73599 }
73600#else
73601 if( pBt->autoVacuum ){
73602 Pgno pgnoMove; /* Move a page here to make room for the root-page */
73603 MemPage *pPageMove; /* The page to move to. */
73604
73605 /* Creating a new table may probably require moving an existing database
73606 ** to make room for the new tables root page. In case this page turns
73607 ** out to be an overflow page, delete all overflow page-map caches
73608 ** held by open cursors.
73609 */
73610 invalidateAllOverflowCache(pBt);
73611
73612 /* Read the value of meta[3] from the database to determine where the
73613 ** root page of the new table should go. meta[3] is the largest root-page
73614 ** created so far, so the new root-page is (meta[3]+1).
73615 */
73616 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
73617 if( pgnoRoot>btreePagecount(pBt) ){
73618 return SQLITE_CORRUPT_BKPT;
73619 }
73620 pgnoRoot++;
73621
73622 /* The new root-page may not be allocated on a pointer-map page, or the
73623 ** PENDING_BYTE page.
73624 */
73625 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
73626 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
73627 pgnoRoot++;
73628 }
73629 assert( pgnoRoot>=3 );
73630
73631 /* Allocate a page. The page that currently resides at pgnoRoot will
73632 ** be moved to the allocated page (unless the allocated page happens
73633 ** to reside at pgnoRoot).
73634 */
73635 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
73636 if( rc!=SQLITE_OK ){
73637 return rc;
73638 }
73639
73640 if( pgnoMove!=pgnoRoot ){
73641 /* pgnoRoot is the page that will be used for the root-page of
73642 ** the new table (assuming an error did not occur). But we were
73643 ** allocated pgnoMove. If required (i.e. if it was not allocated
73644 ** by extending the file), the current page at position pgnoMove
73645 ** is already journaled.
73646 */
73647 u8 eType = 0;
73648 Pgno iPtrPage = 0;
73649
73650 /* Save the positions of any open cursors. This is required in
73651 ** case they are holding a reference to an xFetch reference
73652 ** corresponding to page pgnoRoot. */
73653 rc = saveAllCursors(pBt, 0, 0);
73654 releasePage(pPageMove);
73655 if( rc!=SQLITE_OK ){
73656 return rc;
73657 }
73658
73659 /* Move the page currently at pgnoRoot to pgnoMove. */
73660 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
73661 if( rc!=SQLITE_OK ){
73662 return rc;
73663 }
73664 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
73665 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
73666 rc = SQLITE_CORRUPT_BKPT;
73667 }
73668 if( rc!=SQLITE_OK ){
73669 releasePage(pRoot);
73670 return rc;
73671 }
73672 assert( eType!=PTRMAP_ROOTPAGE );
73673 assert( eType!=PTRMAP_FREEPAGE );
73674 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
73675 releasePage(pRoot);
73676
73677 /* Obtain the page at pgnoRoot */
73678 if( rc!=SQLITE_OK ){
73679 return rc;
73680 }
73681 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
73682 if( rc!=SQLITE_OK ){
73683 return rc;
73684 }
73685 rc = sqlite3PagerWrite(pRoot->pDbPage);
73686 if( rc!=SQLITE_OK ){
73687 releasePage(pRoot);
73688 return rc;
73689 }
73690 }else{
73691 pRoot = pPageMove;
73692 }
73693
73694 /* Update the pointer-map and meta-data with the new root-page number. */
73695 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
73696 if( rc ){
73697 releasePage(pRoot);
73698 return rc;
73699 }
73700
73701 /* When the new root page was allocated, page 1 was made writable in
73702 ** order either to increase the database filesize, or to decrement the
73703 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
73704 */
73705 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
73706 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
73707 if( NEVER(rc) ){
73708 releasePage(pRoot);
73709 return rc;
73710 }
73711
73712 }else{
73713 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
73714 if( rc ) return rc;
73715 }
73716#endif
73717 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
73718 if( createTabFlags & BTREE_INTKEY ){
73719 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
73720 }else{
73721 ptfFlags = PTF_ZERODATA | PTF_LEAF;
73722 }
73723 zeroPage(pRoot, ptfFlags);
73724 sqlite3PagerUnref(pRoot->pDbPage);
73725 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
73726 *piTable = pgnoRoot;
73727 return SQLITE_OK;
73728}
73729SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
73730 int rc;
73731 sqlite3BtreeEnter(p);
73732 rc = btreeCreateTable(p, piTable, flags);
73733 sqlite3BtreeLeave(p);
73734 return rc;
73735}
73736
73737/*
73738** Erase the given database page and all its children. Return
73739** the page to the freelist.
73740*/
73741static int clearDatabasePage(
73742 BtShared *pBt, /* The BTree that contains the table */
73743 Pgno pgno, /* Page number to clear */
73744 int freePageFlag, /* Deallocate page if true */
73745 int *pnChange /* Add number of Cells freed to this counter */
73746){
73747 MemPage *pPage;
73748 int rc;
73749 unsigned char *pCell;
73750 int i;
73751 int hdr;
73752 CellInfo info;
73753
73754 assert( sqlite3_mutex_held(pBt->mutex) );
73755 if( pgno>btreePagecount(pBt) ){
73756 return SQLITE_CORRUPT_BKPT;
73757 }
73758 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
73759 if( rc ) return rc;
73760 if( pPage->bBusy ){
73761 rc = SQLITE_CORRUPT_BKPT;
73762 goto cleardatabasepage_out;
73763 }
73764 pPage->bBusy = 1;
73765 hdr = pPage->hdrOffset;
73766 for(i=0; i<pPage->nCell; i++){
73767 pCell = findCell(pPage, i);
73768 if( !pPage->leaf ){
73769 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
73770 if( rc ) goto cleardatabasepage_out;
73771 }
73772 rc = clearCell(pPage, pCell, &info);
73773 if( rc ) goto cleardatabasepage_out;
73774 }
73775 if( !pPage->leaf ){
73776 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
73777 if( rc ) goto cleardatabasepage_out;
73778 }else if( pnChange ){
73779 assert( pPage->intKey || CORRUPT_DB );
73780 testcase( !pPage->intKey );
73781 *pnChange += pPage->nCell;
73782 }
73783 if( freePageFlag ){
73784 freePage(pPage, &rc);
73785 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
73786 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
73787 }
73788
73789cleardatabasepage_out:
73790 pPage->bBusy = 0;
73791 releasePage(pPage);
73792 return rc;
73793}
73794
73795/*
73796** Delete all information from a single table in the database. iTable is
73797** the page number of the root of the table. After this routine returns,
73798** the root page is empty, but still exists.
73799**
73800** This routine will fail with SQLITE_LOCKED if there are any open
73801** read cursors on the table. Open write cursors are moved to the
73802** root of the table.
73803**
73804** If pnChange is not NULL, then table iTable must be an intkey table. The
73805** integer value pointed to by pnChange is incremented by the number of
73806** entries in the table.
73807*/
73808SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
73809 int rc;
73810 BtShared *pBt = p->pBt;
73811 sqlite3BtreeEnter(p);
73812 assert( p->inTrans==TRANS_WRITE );
73813
73814 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
73815
73816 if( SQLITE_OK==rc ){
73817 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
73818 ** is the root of a table b-tree - if it is not, the following call is
73819 ** a no-op). */
73820 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
73821 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
73822 }
73823 sqlite3BtreeLeave(p);
73824 return rc;
73825}
73826
73827/*
73828** Delete all information from the single table that pCur is open on.
73829**
73830** This routine only work for pCur on an ephemeral table.
73831*/
73832SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
73833 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
73834}
73835
73836/*
73837** Erase all information in a table and add the root of the table to
73838** the freelist. Except, the root of the principle table (the one on
73839** page 1) is never added to the freelist.
73840**
73841** This routine will fail with SQLITE_LOCKED if there are any open
73842** cursors on the table.
73843**
73844** If AUTOVACUUM is enabled and the page at iTable is not the last
73845** root page in the database file, then the last root page
73846** in the database file is moved into the slot formerly occupied by
73847** iTable and that last slot formerly occupied by the last root page
73848** is added to the freelist instead of iTable. In this say, all
73849** root pages are kept at the beginning of the database file, which
73850** is necessary for AUTOVACUUM to work right. *piMoved is set to the
73851** page number that used to be the last root page in the file before
73852** the move. If no page gets moved, *piMoved is set to 0.
73853** The last root page is recorded in meta[3] and the value of
73854** meta[3] is updated by this procedure.
73855*/
73856static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
73857 int rc;
73858 MemPage *pPage = 0;
73859 BtShared *pBt = p->pBt;
73860
73861 assert( sqlite3BtreeHoldsMutex(p) );
73862 assert( p->inTrans==TRANS_WRITE );
73863 assert( iTable>=2 );
73864 if( iTable>btreePagecount(pBt) ){
73865 return SQLITE_CORRUPT_BKPT;
73866 }
73867
73868 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
73869 if( rc ) return rc;
73870 rc = sqlite3BtreeClearTable(p, iTable, 0);
73871 if( rc ){
73872 releasePage(pPage);
73873 return rc;
73874 }
73875
73876 *piMoved = 0;
73877
73878#ifdef SQLITE_OMIT_AUTOVACUUM
73879 freePage(pPage, &rc);
73880 releasePage(pPage);
73881#else
73882 if( pBt->autoVacuum ){
73883 Pgno maxRootPgno;
73884 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
73885
73886 if( iTable==maxRootPgno ){
73887 /* If the table being dropped is the table with the largest root-page
73888 ** number in the database, put the root page on the free list.
73889 */
73890 freePage(pPage, &rc);
73891 releasePage(pPage);
73892 if( rc!=SQLITE_OK ){
73893 return rc;
73894 }
73895 }else{
73896 /* The table being dropped does not have the largest root-page
73897 ** number in the database. So move the page that does into the
73898 ** gap left by the deleted root-page.
73899 */
73900 MemPage *pMove;
73901 releasePage(pPage);
73902 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
73903 if( rc!=SQLITE_OK ){
73904 return rc;
73905 }
73906 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
73907 releasePage(pMove);
73908 if( rc!=SQLITE_OK ){
73909 return rc;
73910 }
73911 pMove = 0;
73912 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
73913 freePage(pMove, &rc);
73914 releasePage(pMove);
73915 if( rc!=SQLITE_OK ){
73916 return rc;
73917 }
73918 *piMoved = maxRootPgno;
73919 }
73920
73921 /* Set the new 'max-root-page' value in the database header. This
73922 ** is the old value less one, less one more if that happens to
73923 ** be a root-page number, less one again if that is the
73924 ** PENDING_BYTE_PAGE.
73925 */
73926 maxRootPgno--;
73927 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
73928 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
73929 maxRootPgno--;
73930 }
73931 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
73932
73933 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
73934 }else{
73935 freePage(pPage, &rc);
73936 releasePage(pPage);
73937 }
73938#endif
73939 return rc;
73940}
73941SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
73942 int rc;
73943 sqlite3BtreeEnter(p);
73944 rc = btreeDropTable(p, iTable, piMoved);
73945 sqlite3BtreeLeave(p);
73946 return rc;
73947}
73948
73949
73950/*
73951** This function may only be called if the b-tree connection already
73952** has a read or write transaction open on the database.
73953**
73954** Read the meta-information out of a database file. Meta[0]
73955** is the number of free pages currently in the database. Meta[1]
73956** through meta[15] are available for use by higher layers. Meta[0]
73957** is read-only, the others are read/write.
73958**
73959** The schema layer numbers meta values differently. At the schema
73960** layer (and the SetCookie and ReadCookie opcodes) the number of
73961** free pages is not visible. So Cookie[0] is the same as Meta[1].
73962**
73963** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
73964** of reading the value out of the header, it instead loads the "DataVersion"
73965** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
73966** database file. It is a number computed by the pager. But its access
73967** pattern is the same as header meta values, and so it is convenient to
73968** read it from this routine.
73969*/
73970SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
73971 BtShared *pBt = p->pBt;
73972
73973 sqlite3BtreeEnter(p);
73974 assert( p->inTrans>TRANS_NONE );
73975 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) );
73976 assert( pBt->pPage1 );
73977 assert( idx>=0 && idx<=15 );
73978
73979 if( idx==BTREE_DATA_VERSION ){
73980 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
73981 }else{
73982 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
73983 }
73984
73985 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
73986 ** database, mark the database as read-only. */
73987#ifdef SQLITE_OMIT_AUTOVACUUM
73988 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
73989 pBt->btsFlags |= BTS_READ_ONLY;
73990 }
73991#endif
73992
73993 sqlite3BtreeLeave(p);
73994}
73995
73996/*
73997** Write meta-information back into the database. Meta[0] is
73998** read-only and may not be written.
73999*/
74000SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
74001 BtShared *pBt = p->pBt;
74002 unsigned char *pP1;
74003 int rc;
74004 assert( idx>=1 && idx<=15 );
74005 sqlite3BtreeEnter(p);
74006 assert( p->inTrans==TRANS_WRITE );
74007 assert( pBt->pPage1!=0 );
74008 pP1 = pBt->pPage1->aData;
74009 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74010 if( rc==SQLITE_OK ){
74011 put4byte(&pP1[36 + idx*4], iMeta);
74012#ifndef SQLITE_OMIT_AUTOVACUUM
74013 if( idx==BTREE_INCR_VACUUM ){
74014 assert( pBt->autoVacuum || iMeta==0 );
74015 assert( iMeta==0 || iMeta==1 );
74016 pBt->incrVacuum = (u8)iMeta;
74017 }
74018#endif
74019 }
74020 sqlite3BtreeLeave(p);
74021 return rc;
74022}
74023
74024/*
74025** The first argument, pCur, is a cursor opened on some b-tree. Count the
74026** number of entries in the b-tree and write the result to *pnEntry.
74027**
74028** SQLITE_OK is returned if the operation is successfully executed.
74029** Otherwise, if an error is encountered (i.e. an IO error or database
74030** corruption) an SQLite error code is returned.
74031*/
74032SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
74033 i64 nEntry = 0; /* Value to return in *pnEntry */
74034 int rc; /* Return code */
74035
74036 rc = moveToRoot(pCur);
74037 if( rc==SQLITE_EMPTY ){
74038 *pnEntry = 0;
74039 return SQLITE_OK;
74040 }
74041
74042 /* Unless an error occurs, the following loop runs one iteration for each
74043 ** page in the B-Tree structure (not including overflow pages).
74044 */
74045 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
74046 int iIdx; /* Index of child node in parent */
74047 MemPage *pPage; /* Current page of the b-tree */
74048
74049 /* If this is a leaf page or the tree is not an int-key tree, then
74050 ** this page contains countable entries. Increment the entry counter
74051 ** accordingly.
74052 */
74053 pPage = pCur->pPage;
74054 if( pPage->leaf || !pPage->intKey ){
74055 nEntry += pPage->nCell;
74056 }
74057
74058 /* pPage is a leaf node. This loop navigates the cursor so that it
74059 ** points to the first interior cell that it points to the parent of
74060 ** the next page in the tree that has not yet been visited. The
74061 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
74062 ** of the page, or to the number of cells in the page if the next page
74063 ** to visit is the right-child of its parent.
74064 **
74065 ** If all pages in the tree have been visited, return SQLITE_OK to the
74066 ** caller.
74067 */
74068 if( pPage->leaf ){
74069 do {
74070 if( pCur->iPage==0 ){
74071 /* All pages of the b-tree have been visited. Return successfully. */
74072 *pnEntry = nEntry;
74073 return moveToRoot(pCur);
74074 }
74075 moveToParent(pCur);
74076 }while ( pCur->ix>=pCur->pPage->nCell );
74077
74078 pCur->ix++;
74079 pPage = pCur->pPage;
74080 }
74081
74082 /* Descend to the child node of the cell that the cursor currently
74083 ** points at. This is the right-child if (iIdx==pPage->nCell).
74084 */
74085 iIdx = pCur->ix;
74086 if( iIdx==pPage->nCell ){
74087 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
74088 }else{
74089 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
74090 }
74091 }
74092
74093 /* An error has occurred. Return an error code. */
74094 return rc;
74095}
74096
74097/*
74098** Return the pager associated with a BTree. This routine is used for
74099** testing and debugging only.
74100*/
74101SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
74102 return p->pBt->pPager;
74103}
74104
74105#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74106/*
74107** Append a message to the error message string.
74108*/
74109static void checkAppendMsg(
74110 IntegrityCk *pCheck,
74111 const char *zFormat,
74112 ...
74113){
74114 va_list ap;
74115 if( !pCheck->mxErr ) return;
74116 pCheck->mxErr--;
74117 pCheck->nErr++;
74118 va_start(ap, zFormat);
74119 if( pCheck->errMsg.nChar ){
74120 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
74121 }
74122 if( pCheck->zPfx ){
74123 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
74124 }
74125 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
74126 va_end(ap);
74127 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
74128 pCheck->bOomFault = 1;
74129 }
74130}
74131#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74132
74133#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74134
74135/*
74136** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
74137** corresponds to page iPg is already set.
74138*/
74139static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
74140 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
74141 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
74142}
74143
74144/*
74145** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
74146*/
74147static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
74148 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
74149 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
74150}
74151
74152
74153/*
74154** Add 1 to the reference count for page iPage. If this is the second
74155** reference to the page, add an error message to pCheck->zErrMsg.
74156** Return 1 if there are 2 or more references to the page and 0 if
74157** if this is the first reference to the page.
74158**
74159** Also check that the page number is in bounds.
74160*/
74161static int checkRef(IntegrityCk *pCheck, Pgno iPage){
74162 if( iPage>pCheck->nPage || iPage==0 ){
74163 checkAppendMsg(pCheck, "invalid page number %d", iPage);
74164 return 1;
74165 }
74166 if( getPageReferenced(pCheck, iPage) ){
74167 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
74168 return 1;
74169 }
74170 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
74171 setPageReferenced(pCheck, iPage);
74172 return 0;
74173}
74174
74175#ifndef SQLITE_OMIT_AUTOVACUUM
74176/*
74177** Check that the entry in the pointer-map for page iChild maps to
74178** page iParent, pointer type ptrType. If not, append an error message
74179** to pCheck.
74180*/
74181static void checkPtrmap(
74182 IntegrityCk *pCheck, /* Integrity check context */
74183 Pgno iChild, /* Child page number */
74184 u8 eType, /* Expected pointer map type */
74185 Pgno iParent /* Expected pointer map parent page number */
74186){
74187 int rc;
74188 u8 ePtrmapType;
74189 Pgno iPtrmapParent;
74190
74191 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
74192 if( rc!=SQLITE_OK ){
74193 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
74194 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
74195 return;
74196 }
74197
74198 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
74199 checkAppendMsg(pCheck,
74200 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
74201 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
74202 }
74203}
74204#endif
74205
74206/*
74207** Check the integrity of the freelist or of an overflow page list.
74208** Verify that the number of pages on the list is N.
74209*/
74210static void checkList(
74211 IntegrityCk *pCheck, /* Integrity checking context */
74212 int isFreeList, /* True for a freelist. False for overflow page list */
74213 Pgno iPage, /* Page number for first page in the list */
74214 u32 N /* Expected number of pages in the list */
74215){
74216 int i;
74217 u32 expected = N;
74218 int nErrAtStart = pCheck->nErr;
74219 while( iPage!=0 && pCheck->mxErr ){
74220 DbPage *pOvflPage;
74221 unsigned char *pOvflData;
74222 if( checkRef(pCheck, iPage) ) break;
74223 N--;
74224 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
74225 checkAppendMsg(pCheck, "failed to get page %d", iPage);
74226 break;
74227 }
74228 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
74229 if( isFreeList ){
74230 u32 n = (u32)get4byte(&pOvflData[4]);
74231#ifndef SQLITE_OMIT_AUTOVACUUM
74232 if( pCheck->pBt->autoVacuum ){
74233 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
74234 }
74235#endif
74236 if( n>pCheck->pBt->usableSize/4-2 ){
74237 checkAppendMsg(pCheck,
74238 "freelist leaf count too big on page %d", iPage);
74239 N--;
74240 }else{
74241 for(i=0; i<(int)n; i++){
74242 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
74243#ifndef SQLITE_OMIT_AUTOVACUUM
74244 if( pCheck->pBt->autoVacuum ){
74245 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
74246 }
74247#endif
74248 checkRef(pCheck, iFreePage);
74249 }
74250 N -= n;
74251 }
74252 }
74253#ifndef SQLITE_OMIT_AUTOVACUUM
74254 else{
74255 /* If this database supports auto-vacuum and iPage is not the last
74256 ** page in this overflow list, check that the pointer-map entry for
74257 ** the following page matches iPage.
74258 */
74259 if( pCheck->pBt->autoVacuum && N>0 ){
74260 i = get4byte(pOvflData);
74261 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
74262 }
74263 }
74264#endif
74265 iPage = get4byte(pOvflData);
74266 sqlite3PagerUnref(pOvflPage);
74267 }
74268 if( N && nErrAtStart==pCheck->nErr ){
74269 checkAppendMsg(pCheck,
74270 "%s is %d but should be %d",
74271 isFreeList ? "size" : "overflow list length",
74272 expected-N, expected);
74273 }
74274}
74275#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74276
74277/*
74278** An implementation of a min-heap.
74279**
74280** aHeap[0] is the number of elements on the heap. aHeap[1] is the
74281** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
74282** and aHeap[N*2+1].
74283**
74284** The heap property is this: Every node is less than or equal to both
74285** of its daughter nodes. A consequence of the heap property is that the
74286** root node aHeap[1] is always the minimum value currently in the heap.
74287**
74288** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
74289** the heap, preserving the heap property. The btreeHeapPull() routine
74290** removes the root element from the heap (the minimum value in the heap)
74291** and then moves other nodes around as necessary to preserve the heap
74292** property.
74293**
74294** This heap is used for cell overlap and coverage testing. Each u32
74295** entry represents the span of a cell or freeblock on a btree page.
74296** The upper 16 bits are the index of the first byte of a range and the
74297** lower 16 bits are the index of the last byte of that range.
74298*/
74299static void btreeHeapInsert(u32 *aHeap, u32 x){
74300 u32 j, i = ++aHeap[0];
74301 aHeap[i] = x;
74302 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
74303 x = aHeap[j];
74304 aHeap[j] = aHeap[i];
74305 aHeap[i] = x;
74306 i = j;
74307 }
74308}
74309static int btreeHeapPull(u32 *aHeap, u32 *pOut){
74310 u32 j, i, x;
74311 if( (x = aHeap[0])==0 ) return 0;
74312 *pOut = aHeap[1];
74313 aHeap[1] = aHeap[x];
74314 aHeap[x] = 0xffffffff;
74315 aHeap[0]--;
74316 i = 1;
74317 while( (j = i*2)<=aHeap[0] ){
74318 if( aHeap[j]>aHeap[j+1] ) j++;
74319 if( aHeap[i]<aHeap[j] ) break;
74320 x = aHeap[i];
74321 aHeap[i] = aHeap[j];
74322 aHeap[j] = x;
74323 i = j;
74324 }
74325 return 1;
74326}
74327
74328#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74329/*
74330** Do various sanity checks on a single page of a tree. Return
74331** the tree depth. Root pages return 0. Parents of root pages
74332** return 1, and so forth.
74333**
74334** These checks are done:
74335**
74336** 1. Make sure that cells and freeblocks do not overlap
74337** but combine to completely cover the page.
74338** 2. Make sure integer cell keys are in order.
74339** 3. Check the integrity of overflow pages.
74340** 4. Recursively call checkTreePage on all children.
74341** 5. Verify that the depth of all children is the same.
74342*/
74343static int checkTreePage(
74344 IntegrityCk *pCheck, /* Context for the sanity check */
74345 Pgno iPage, /* Page number of the page to check */
74346 i64 *piMinKey, /* Write minimum integer primary key here */
74347 i64 maxKey /* Error if integer primary key greater than this */
74348){
74349 MemPage *pPage = 0; /* The page being analyzed */
74350 int i; /* Loop counter */
74351 int rc; /* Result code from subroutine call */
74352 int depth = -1, d2; /* Depth of a subtree */
74353 int pgno; /* Page number */
74354 int nFrag; /* Number of fragmented bytes on the page */
74355 int hdr; /* Offset to the page header */
74356 int cellStart; /* Offset to the start of the cell pointer array */
74357 int nCell; /* Number of cells */
74358 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
74359 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
74360 ** False if IPK must be strictly less than maxKey */
74361 u8 *data; /* Page content */
74362 u8 *pCell; /* Cell content */
74363 u8 *pCellIdx; /* Next element of the cell pointer array */
74364 BtShared *pBt; /* The BtShared object that owns pPage */
74365 u32 pc; /* Address of a cell */
74366 u32 usableSize; /* Usable size of the page */
74367 u32 contentOffset; /* Offset to the start of the cell content area */
74368 u32 *heap = 0; /* Min-heap used for checking cell coverage */
74369 u32 x, prev = 0; /* Next and previous entry on the min-heap */
74370 const char *saved_zPfx = pCheck->zPfx;
74371 int saved_v1 = pCheck->v1;
74372 int saved_v2 = pCheck->v2;
74373 u8 savedIsInit = 0;
74374
74375 /* Check that the page exists
74376 */
74377 pBt = pCheck->pBt;
74378 usableSize = pBt->usableSize;
74379 if( iPage==0 ) return 0;
74380 if( checkRef(pCheck, iPage) ) return 0;
74381 pCheck->zPfx = "Page %u: ";
74382 pCheck->v1 = iPage;
74383 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
74384 checkAppendMsg(pCheck,
74385 "unable to get the page. error code=%d", rc);
74386 goto end_of_check;
74387 }
74388
74389 /* Clear MemPage.isInit to make sure the corruption detection code in
74390 ** btreeInitPage() is executed. */
74391 savedIsInit = pPage->isInit;
74392 pPage->isInit = 0;
74393 if( (rc = btreeInitPage(pPage))!=0 ){
74394 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
74395 checkAppendMsg(pCheck,
74396 "btreeInitPage() returns error code %d", rc);
74397 goto end_of_check;
74398 }
74399 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
74400 assert( rc==SQLITE_CORRUPT );
74401 checkAppendMsg(pCheck, "free space corruption", rc);
74402 goto end_of_check;
74403 }
74404 data = pPage->aData;
74405 hdr = pPage->hdrOffset;
74406
74407 /* Set up for cell analysis */
74408 pCheck->zPfx = "On tree page %u cell %d: ";
74409 contentOffset = get2byteNotZero(&data[hdr+5]);
74410 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
74411
74412 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
74413 ** number of cells on the page. */
74414 nCell = get2byte(&data[hdr+3]);
74415 assert( pPage->nCell==nCell );
74416
74417 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
74418 ** immediately follows the b-tree page header. */
74419 cellStart = hdr + 12 - 4*pPage->leaf;
74420 assert( pPage->aCellIdx==&data[cellStart] );
74421 pCellIdx = &data[cellStart + 2*(nCell-1)];
74422
74423 if( !pPage->leaf ){
74424 /* Analyze the right-child page of internal pages */
74425 pgno = get4byte(&data[hdr+8]);
74426#ifndef SQLITE_OMIT_AUTOVACUUM
74427 if( pBt->autoVacuum ){
74428 pCheck->zPfx = "On page %u at right child: ";
74429 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
74430 }
74431#endif
74432 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
74433 keyCanBeEqual = 0;
74434 }else{
74435 /* For leaf pages, the coverage check will occur in the same loop
74436 ** as the other cell checks, so initialize the heap. */
74437 heap = pCheck->heap;
74438 heap[0] = 0;
74439 }
74440
74441 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
74442 ** integer offsets to the cell contents. */
74443 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
74444 CellInfo info;
74445
74446 /* Check cell size */
74447 pCheck->v2 = i;
74448 assert( pCellIdx==&data[cellStart + i*2] );
74449 pc = get2byteAligned(pCellIdx);
74450 pCellIdx -= 2;
74451 if( pc<contentOffset || pc>usableSize-4 ){
74452 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
74453 pc, contentOffset, usableSize-4);
74454 doCoverageCheck = 0;
74455 continue;
74456 }
74457 pCell = &data[pc];
74458 pPage->xParseCell(pPage, pCell, &info);
74459 if( pc+info.nSize>usableSize ){
74460 checkAppendMsg(pCheck, "Extends off end of page");
74461 doCoverageCheck = 0;
74462 continue;
74463 }
74464
74465 /* Check for integer primary key out of range */
74466 if( pPage->intKey ){
74467 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
74468 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
74469 }
74470 maxKey = info.nKey;
74471 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
74472 }
74473
74474 /* Check the content overflow list */
74475 if( info.nPayload>info.nLocal ){
74476 u32 nPage; /* Number of pages on the overflow chain */
74477 Pgno pgnoOvfl; /* First page of the overflow chain */
74478 assert( pc + info.nSize - 4 <= usableSize );
74479 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
74480 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
74481#ifndef SQLITE_OMIT_AUTOVACUUM
74482 if( pBt->autoVacuum ){
74483 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
74484 }
74485#endif
74486 checkList(pCheck, 0, pgnoOvfl, nPage);
74487 }
74488
74489 if( !pPage->leaf ){
74490 /* Check sanity of left child page for internal pages */
74491 pgno = get4byte(pCell);
74492#ifndef SQLITE_OMIT_AUTOVACUUM
74493 if( pBt->autoVacuum ){
74494 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
74495 }
74496#endif
74497 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
74498 keyCanBeEqual = 0;
74499 if( d2!=depth ){
74500 checkAppendMsg(pCheck, "Child page depth differs");
74501 depth = d2;
74502 }
74503 }else{
74504 /* Populate the coverage-checking heap for leaf pages */
74505 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
74506 }
74507 }
74508 *piMinKey = maxKey;
74509
74510 /* Check for complete coverage of the page
74511 */
74512 pCheck->zPfx = 0;
74513 if( doCoverageCheck && pCheck->mxErr>0 ){
74514 /* For leaf pages, the min-heap has already been initialized and the
74515 ** cells have already been inserted. But for internal pages, that has
74516 ** not yet been done, so do it now */
74517 if( !pPage->leaf ){
74518 heap = pCheck->heap;
74519 heap[0] = 0;
74520 for(i=nCell-1; i>=0; i--){
74521 u32 size;
74522 pc = get2byteAligned(&data[cellStart+i*2]);
74523 size = pPage->xCellSize(pPage, &data[pc]);
74524 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
74525 }
74526 }
74527 /* Add the freeblocks to the min-heap
74528 **
74529 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
74530 ** is the offset of the first freeblock, or zero if there are no
74531 ** freeblocks on the page.
74532 */
74533 i = get2byte(&data[hdr+1]);
74534 while( i>0 ){
74535 int size, j;
74536 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
74537 size = get2byte(&data[i+2]);
74538 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
74539 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
74540 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
74541 ** big-endian integer which is the offset in the b-tree page of the next
74542 ** freeblock in the chain, or zero if the freeblock is the last on the
74543 ** chain. */
74544 j = get2byte(&data[i]);
74545 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
74546 ** increasing offset. */
74547 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
74548 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
74549 i = j;
74550 }
74551 /* Analyze the min-heap looking for overlap between cells and/or
74552 ** freeblocks, and counting the number of untracked bytes in nFrag.
74553 **
74554 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
74555 ** There is an implied first entry the covers the page header, the cell
74556 ** pointer index, and the gap between the cell pointer index and the start
74557 ** of cell content.
74558 **
74559 ** The loop below pulls entries from the min-heap in order and compares
74560 ** the start_address against the previous end_address. If there is an
74561 ** overlap, that means bytes are used multiple times. If there is a gap,
74562 ** that gap is added to the fragmentation count.
74563 */
74564 nFrag = 0;
74565 prev = contentOffset - 1; /* Implied first min-heap entry */
74566 while( btreeHeapPull(heap,&x) ){
74567 if( (prev&0xffff)>=(x>>16) ){
74568 checkAppendMsg(pCheck,
74569 "Multiple uses for byte %u of page %u", x>>16, iPage);
74570 break;
74571 }else{
74572 nFrag += (x>>16) - (prev&0xffff) - 1;
74573 prev = x;
74574 }
74575 }
74576 nFrag += usableSize - (prev&0xffff) - 1;
74577 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
74578 ** is stored in the fifth field of the b-tree page header.
74579 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
74580 ** number of fragmented free bytes within the cell content area.
74581 */
74582 if( heap[0]==0 && nFrag!=data[hdr+7] ){
74583 checkAppendMsg(pCheck,
74584 "Fragmentation of %d bytes reported as %d on page %u",
74585 nFrag, data[hdr+7], iPage);
74586 }
74587 }
74588
74589end_of_check:
74590 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
74591 releasePage(pPage);
74592 pCheck->zPfx = saved_zPfx;
74593 pCheck->v1 = saved_v1;
74594 pCheck->v2 = saved_v2;
74595 return depth+1;
74596}
74597#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74598
74599#ifndef SQLITE_OMIT_INTEGRITY_CHECK
74600/*
74601** This routine does a complete check of the given BTree file. aRoot[] is
74602** an array of pages numbers were each page number is the root page of
74603** a table. nRoot is the number of entries in aRoot.
74604**
74605** A read-only or read-write transaction must be opened before calling
74606** this function.
74607**
74608** Write the number of error seen in *pnErr. Except for some memory
74609** allocation errors, an error message held in memory obtained from
74610** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
74611** returned. If a memory allocation error occurs, NULL is returned.
74612**
74613** If the first entry in aRoot[] is 0, that indicates that the list of
74614** root pages is incomplete. This is a "partial integrity-check". This
74615** happens when performing an integrity check on a single table. The
74616** zero is skipped, of course. But in addition, the freelist checks
74617** and the checks to make sure every page is referenced are also skipped,
74618** since obviously it is not possible to know which pages are covered by
74619** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
74620** checks are still performed.
74621*/
74622SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
74623 sqlite3 *db, /* Database connection that is running the check */
74624 Btree *p, /* The btree to be checked */
74625 Pgno *aRoot, /* An array of root pages numbers for individual trees */
74626 int nRoot, /* Number of entries in aRoot[] */
74627 int mxErr, /* Stop reporting errors after this many */
74628 int *pnErr /* Write number of errors seen to this variable */
74629){
74630 Pgno i;
74631 IntegrityCk sCheck;
74632 BtShared *pBt = p->pBt;
74633 u64 savedDbFlags = pBt->db->flags;
74634 char zErr[100];
74635 int bPartial = 0; /* True if not checking all btrees */
74636 int bCkFreelist = 1; /* True to scan the freelist */
74637 VVA_ONLY( int nRef );
74638 assert( nRoot>0 );
74639
74640 /* aRoot[0]==0 means this is a partial check */
74641 if( aRoot[0]==0 ){
74642 assert( nRoot>1 );
74643 bPartial = 1;
74644 if( aRoot[1]!=1 ) bCkFreelist = 0;
74645 }
74646
74647 sqlite3BtreeEnter(p);
74648 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
74649 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
74650 assert( nRef>=0 );
74651 sCheck.db = db;
74652 sCheck.pBt = pBt;
74653 sCheck.pPager = pBt->pPager;
74654 sCheck.nPage = btreePagecount(sCheck.pBt);
74655 sCheck.mxErr = mxErr;
74656 sCheck.nErr = 0;
74657 sCheck.bOomFault = 0;
74658 sCheck.zPfx = 0;
74659 sCheck.v1 = 0;
74660 sCheck.v2 = 0;
74661 sCheck.aPgRef = 0;
74662 sCheck.heap = 0;
74663 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
74664 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
74665 if( sCheck.nPage==0 ){
74666 goto integrity_ck_cleanup;
74667 }
74668
74669 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
74670 if( !sCheck.aPgRef ){
74671 sCheck.bOomFault = 1;
74672 goto integrity_ck_cleanup;
74673 }
74674 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
74675 if( sCheck.heap==0 ){
74676 sCheck.bOomFault = 1;
74677 goto integrity_ck_cleanup;
74678 }
74679
74680 i = PENDING_BYTE_PAGE(pBt);
74681 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
74682
74683 /* Check the integrity of the freelist
74684 */
74685 if( bCkFreelist ){
74686 sCheck.zPfx = "Main freelist: ";
74687 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
74688 get4byte(&pBt->pPage1->aData[36]));
74689 sCheck.zPfx = 0;
74690 }
74691
74692 /* Check all the tables.
74693 */
74694#ifndef SQLITE_OMIT_AUTOVACUUM
74695 if( !bPartial ){
74696 if( pBt->autoVacuum ){
74697 Pgno mx = 0;
74698 Pgno mxInHdr;
74699 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
74700 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
74701 if( mx!=mxInHdr ){
74702 checkAppendMsg(&sCheck,
74703 "max rootpage (%d) disagrees with header (%d)",
74704 mx, mxInHdr
74705 );
74706 }
74707 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
74708 checkAppendMsg(&sCheck,
74709 "incremental_vacuum enabled with a max rootpage of zero"
74710 );
74711 }
74712 }
74713#endif
74714 testcase( pBt->db->flags & SQLITE_CellSizeCk );
74715 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
74716 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
74717 i64 notUsed;
74718 if( aRoot[i]==0 ) continue;
74719#ifndef SQLITE_OMIT_AUTOVACUUM
74720 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
74721 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
74722 }
74723#endif
74724 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
74725 }
74726 pBt->db->flags = savedDbFlags;
74727
74728 /* Make sure every page in the file is referenced
74729 */
74730 if( !bPartial ){
74731 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
74732#ifdef SQLITE_OMIT_AUTOVACUUM
74733 if( getPageReferenced(&sCheck, i)==0 ){
74734 checkAppendMsg(&sCheck, "Page %d is never used", i);
74735 }
74736#else
74737 /* If the database supports auto-vacuum, make sure no tables contain
74738 ** references to pointer-map pages.
74739 */
74740 if( getPageReferenced(&sCheck, i)==0 &&
74741 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
74742 checkAppendMsg(&sCheck, "Page %d is never used", i);
74743 }
74744 if( getPageReferenced(&sCheck, i)!=0 &&
74745 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
74746 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
74747 }
74748#endif
74749 }
74750 }
74751
74752 /* Clean up and report errors.
74753 */
74754integrity_ck_cleanup:
74755 sqlite3PageFree(sCheck.heap);
74756 sqlite3_free(sCheck.aPgRef);
74757 if( sCheck.bOomFault ){
74758 sqlite3_str_reset(&sCheck.errMsg);
74759 sCheck.nErr++;
74760 }
74761 *pnErr = sCheck.nErr;
74762 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
74763 /* Make sure this analysis did not leave any unref() pages. */
74764 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
74765 sqlite3BtreeLeave(p);
74766 return sqlite3StrAccumFinish(&sCheck.errMsg);
74767}
74768#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
74769
74770/*
74771** Return the full pathname of the underlying database file. Return
74772** an empty string if the database is in-memory or a TEMP database.
74773**
74774** The pager filename is invariant as long as the pager is
74775** open so it is safe to access without the BtShared mutex.
74776*/
74777SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
74778 assert( p->pBt->pPager!=0 );
74779 return sqlite3PagerFilename(p->pBt->pPager, 1);
74780}
74781
74782/*
74783** Return the pathname of the journal file for this database. The return
74784** value of this routine is the same regardless of whether the journal file
74785** has been created or not.
74786**
74787** The pager journal filename is invariant as long as the pager is
74788** open so it is safe to access without the BtShared mutex.
74789*/
74790SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
74791 assert( p->pBt->pPager!=0 );
74792 return sqlite3PagerJournalname(p->pBt->pPager);
74793}
74794
74795/*
74796** Return non-zero if a transaction is active.
74797*/
74798SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
74799 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
74800 return (p && (p->inTrans==TRANS_WRITE));
74801}
74802
74803#ifndef SQLITE_OMIT_WAL
74804/*
74805** Run a checkpoint on the Btree passed as the first argument.
74806**
74807** Return SQLITE_LOCKED if this or any other connection has an open
74808** transaction on the shared-cache the argument Btree is connected to.
74809**
74810** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
74811*/
74812SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
74813 int rc = SQLITE_OK;
74814 if( p ){
74815 BtShared *pBt = p->pBt;
74816 sqlite3BtreeEnter(p);
74817 if( pBt->inTransaction!=TRANS_NONE ){
74818 rc = SQLITE_LOCKED;
74819 }else{
74820 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
74821 }
74822 sqlite3BtreeLeave(p);
74823 }
74824 return rc;
74825}
74826#endif
74827
74828/*
74829** Return non-zero if a read (or write) transaction is active.
74830*/
74831SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
74832 assert( p );
74833 assert( sqlite3_mutex_held(p->db->mutex) );
74834 return p->inTrans!=TRANS_NONE;
74835}
74836
74837SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
74838 assert( p );
74839 assert( sqlite3_mutex_held(p->db->mutex) );
74840 return p->nBackup!=0;
74841}
74842
74843/*
74844** This function returns a pointer to a blob of memory associated with
74845** a single shared-btree. The memory is used by client code for its own
74846** purposes (for example, to store a high-level schema associated with
74847** the shared-btree). The btree layer manages reference counting issues.
74848**
74849** The first time this is called on a shared-btree, nBytes bytes of memory
74850** are allocated, zeroed, and returned to the caller. For each subsequent
74851** call the nBytes parameter is ignored and a pointer to the same blob
74852** of memory returned.
74853**
74854** If the nBytes parameter is 0 and the blob of memory has not yet been
74855** allocated, a null pointer is returned. If the blob has already been
74856** allocated, it is returned as normal.
74857**
74858** Just before the shared-btree is closed, the function passed as the
74859** xFree argument when the memory allocation was made is invoked on the
74860** blob of allocated memory. The xFree function should not call sqlite3_free()
74861** on the memory, the btree layer does that.
74862*/
74863SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
74864 BtShared *pBt = p->pBt;
74865 sqlite3BtreeEnter(p);
74866 if( !pBt->pSchema && nBytes ){
74867 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
74868 pBt->xFreeSchema = xFree;
74869 }
74870 sqlite3BtreeLeave(p);
74871 return pBt->pSchema;
74872}
74873
74874/*
74875** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
74876** btree as the argument handle holds an exclusive lock on the
74877** sqlite_schema table. Otherwise SQLITE_OK.
74878*/
74879SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
74880 int rc;
74881 assert( sqlite3_mutex_held(p->db->mutex) );
74882 sqlite3BtreeEnter(p);
74883 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
74884 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
74885 sqlite3BtreeLeave(p);
74886 return rc;
74887}
74888
74889
74890#ifndef SQLITE_OMIT_SHARED_CACHE
74891/*
74892** Obtain a lock on the table whose root page is iTab. The
74893** lock is a write lock if isWritelock is true or a read lock
74894** if it is false.
74895*/
74896SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
74897 int rc = SQLITE_OK;
74898 assert( p->inTrans!=TRANS_NONE );
74899 if( p->sharable ){
74900 u8 lockType = READ_LOCK + isWriteLock;
74901 assert( READ_LOCK+1==WRITE_LOCK );
74902 assert( isWriteLock==0 || isWriteLock==1 );
74903
74904 sqlite3BtreeEnter(p);
74905 rc = querySharedCacheTableLock(p, iTab, lockType);
74906 if( rc==SQLITE_OK ){
74907 rc = setSharedCacheTableLock(p, iTab, lockType);
74908 }
74909 sqlite3BtreeLeave(p);
74910 }
74911 return rc;
74912}
74913#endif
74914
74915#ifndef SQLITE_OMIT_INCRBLOB
74916/*
74917** Argument pCsr must be a cursor opened for writing on an
74918** INTKEY table currently pointing at a valid table entry.
74919** This function modifies the data stored as part of that entry.
74920**
74921** Only the data content may only be modified, it is not possible to
74922** change the length of the data stored. If this function is called with
74923** parameters that attempt to write past the end of the existing data,
74924** no modifications are made and SQLITE_CORRUPT is returned.
74925*/
74926SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
74927 int rc;
74928 assert( cursorOwnsBtShared(pCsr) );
74929 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
74930 assert( pCsr->curFlags & BTCF_Incrblob );
74931
74932 rc = restoreCursorPosition(pCsr);
74933 if( rc!=SQLITE_OK ){
74934 return rc;
74935 }
74936 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
74937 if( pCsr->eState!=CURSOR_VALID ){
74938 return SQLITE_ABORT;
74939 }
74940
74941 /* Save the positions of all other cursors open on this table. This is
74942 ** required in case any of them are holding references to an xFetch
74943 ** version of the b-tree page modified by the accessPayload call below.
74944 **
74945 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
74946 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
74947 ** saveAllCursors can only return SQLITE_OK.
74948 */
74949 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
74950 assert( rc==SQLITE_OK );
74951
74952 /* Check some assumptions:
74953 ** (a) the cursor is open for writing,
74954 ** (b) there is a read/write transaction open,
74955 ** (c) the connection holds a write-lock on the table (if required),
74956 ** (d) there are no conflicting read-locks, and
74957 ** (e) the cursor points at a valid row of an intKey table.
74958 */
74959 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
74960 return SQLITE_READONLY;
74961 }
74962 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
74963 && pCsr->pBt->inTransaction==TRANS_WRITE );
74964 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
74965 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
74966 assert( pCsr->pPage->intKey );
74967
74968 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
74969}
74970
74971/*
74972** Mark this cursor as an incremental blob cursor.
74973*/
74974SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
74975 pCur->curFlags |= BTCF_Incrblob;
74976 pCur->pBtree->hasIncrblobCur = 1;
74977}
74978#endif
74979
74980/*
74981** Set both the "read version" (single byte at byte offset 18) and
74982** "write version" (single byte at byte offset 19) fields in the database
74983** header to iVersion.
74984*/
74985SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
74986 BtShared *pBt = pBtree->pBt;
74987 int rc; /* Return code */
74988
74989 assert( iVersion==1 || iVersion==2 );
74990
74991 /* If setting the version fields to 1, do not automatically open the
74992 ** WAL connection, even if the version fields are currently set to 2.
74993 */
74994 pBt->btsFlags &= ~BTS_NO_WAL;
74995 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
74996
74997 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
74998 if( rc==SQLITE_OK ){
74999 u8 *aData = pBt->pPage1->aData;
75000 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
75001 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
75002 if( rc==SQLITE_OK ){
75003 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75004 if( rc==SQLITE_OK ){
75005 aData[18] = (u8)iVersion;
75006 aData[19] = (u8)iVersion;
75007 }
75008 }
75009 }
75010 }
75011
75012 pBt->btsFlags &= ~BTS_NO_WAL;
75013 return rc;
75014}
75015
75016/*
75017** Return true if the cursor has a hint specified. This routine is
75018** only used from within assert() statements
75019*/
75020SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
75021 return (pCsr->hints & mask)!=0;
75022}
75023
75024/*
75025** Return true if the given Btree is read-only.
75026*/
75027SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
75028 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
75029}
75030
75031/*
75032** Return the size of the header added to each page by this module.
75033*/
75034SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
75035
75036#if !defined(SQLITE_OMIT_SHARED_CACHE)
75037/*
75038** Return true if the Btree passed as the only argument is sharable.
75039*/
75040SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
75041 return p->sharable;
75042}
75043
75044/*
75045** Return the number of connections to the BtShared object accessed by
75046** the Btree handle passed as the only argument. For private caches
75047** this is always 1. For shared caches it may be 1 or greater.
75048*/
75049SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
75050 testcase( p->sharable );
75051 return p->pBt->nRef;
75052}
75053#endif
75054
75055/************** End of btree.c ***********************************************/
75056/************** Begin file backup.c ******************************************/
75057/*
75058** 2009 January 28
75059**
75060** The author disclaims copyright to this source code. In place of
75061** a legal notice, here is a blessing:
75062**
75063** May you do good and not evil.
75064** May you find forgiveness for yourself and forgive others.
75065** May you share freely, never taking more than you give.
75066**
75067*************************************************************************
75068** This file contains the implementation of the sqlite3_backup_XXX()
75069** API functions and the related features.
75070*/
75071/* #include "sqliteInt.h" */
75072/* #include "btreeInt.h" */
75073
75074/*
75075** Structure allocated for each backup operation.
75076*/
75077struct sqlite3_backup {
75078 sqlite3* pDestDb; /* Destination database handle */
75079 Btree *pDest; /* Destination b-tree file */
75080 u32 iDestSchema; /* Original schema cookie in destination */
75081 int bDestLocked; /* True once a write-transaction is open on pDest */
75082
75083 Pgno iNext; /* Page number of the next source page to copy */
75084 sqlite3* pSrcDb; /* Source database handle */
75085 Btree *pSrc; /* Source b-tree file */
75086
75087 int rc; /* Backup process error code */
75088
75089 /* These two variables are set by every call to backup_step(). They are
75090 ** read by calls to backup_remaining() and backup_pagecount().
75091 */
75092 Pgno nRemaining; /* Number of pages left to copy */
75093 Pgno nPagecount; /* Total number of pages to copy */
75094
75095 int isAttached; /* True once backup has been registered with pager */
75096 sqlite3_backup *pNext; /* Next backup associated with source pager */
75097};
75098
75099/*
75100** THREAD SAFETY NOTES:
75101**
75102** Once it has been created using backup_init(), a single sqlite3_backup
75103** structure may be accessed via two groups of thread-safe entry points:
75104**
75105** * Via the sqlite3_backup_XXX() API function backup_step() and
75106** backup_finish(). Both these functions obtain the source database
75107** handle mutex and the mutex associated with the source BtShared
75108** structure, in that order.
75109**
75110** * Via the BackupUpdate() and BackupRestart() functions, which are
75111** invoked by the pager layer to report various state changes in
75112** the page cache associated with the source database. The mutex
75113** associated with the source database BtShared structure will always
75114** be held when either of these functions are invoked.
75115**
75116** The other sqlite3_backup_XXX() API functions, backup_remaining() and
75117** backup_pagecount() are not thread-safe functions. If they are called
75118** while some other thread is calling backup_step() or backup_finish(),
75119** the values returned may be invalid. There is no way for a call to
75120** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
75121** or backup_pagecount().
75122**
75123** Depending on the SQLite configuration, the database handles and/or
75124** the Btree objects may have their own mutexes that require locking.
75125** Non-sharable Btrees (in-memory databases for example), do not have
75126** associated mutexes.
75127*/
75128
75129/*
75130** Return a pointer corresponding to database zDb (i.e. "main", "temp")
75131** in connection handle pDb. If such a database cannot be found, return
75132** a NULL pointer and write an error message to pErrorDb.
75133**
75134** If the "temp" database is requested, it may need to be opened by this
75135** function. If an error occurs while doing so, return 0 and write an
75136** error message to pErrorDb.
75137*/
75138static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
75139 int i = sqlite3FindDbName(pDb, zDb);
75140
75141 if( i==1 ){
75142 Parse sParse;
75143 int rc = 0;
75144 memset(&sParse, 0, sizeof(sParse));
75145 sParse.db = pDb;
75146 if( sqlite3OpenTempDatabase(&sParse) ){
75147 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
75148 rc = SQLITE_ERROR;
75149 }
75150 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
75151 sqlite3ParserReset(&sParse);
75152 if( rc ){
75153 return 0;
75154 }
75155 }
75156
75157 if( i<0 ){
75158 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
75159 return 0;
75160 }
75161
75162 return pDb->aDb[i].pBt;
75163}
75164
75165/*
75166** Attempt to set the page size of the destination to match the page size
75167** of the source.
75168*/
75169static int setDestPgsz(sqlite3_backup *p){
75170 int rc;
75171 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
75172 return rc;
75173}
75174
75175/*
75176** Check that there is no open read-transaction on the b-tree passed as the
75177** second argument. If there is not, return SQLITE_OK. Otherwise, if there
75178** is an open read-transaction, return SQLITE_ERROR and leave an error
75179** message in database handle db.
75180*/
75181static int checkReadTransaction(sqlite3 *db, Btree *p){
75182 if( sqlite3BtreeIsInReadTrans(p) ){
75183 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
75184 return SQLITE_ERROR;
75185 }
75186 return SQLITE_OK;
75187}
75188
75189/*
75190** Create an sqlite3_backup process to copy the contents of zSrcDb from
75191** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
75192** a pointer to the new sqlite3_backup object.
75193**
75194** If an error occurs, NULL is returned and an error code and error message
75195** stored in database handle pDestDb.
75196*/
75197SQLITE_API sqlite3_backup *sqlite3_backup_init(
75198 sqlite3* pDestDb, /* Database to write to */
75199 const char *zDestDb, /* Name of database within pDestDb */
75200 sqlite3* pSrcDb, /* Database connection to read from */
75201 const char *zSrcDb /* Name of database within pSrcDb */
75202){
75203 sqlite3_backup *p; /* Value to return */
75204
75205#ifdef SQLITE_ENABLE_API_ARMOR
75206 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
75207 (void)SQLITE_MISUSE_BKPT;
75208 return 0;
75209 }
75210#endif
75211
75212 /* Lock the source database handle. The destination database
75213 ** handle is not locked in this routine, but it is locked in
75214 ** sqlite3_backup_step(). The user is required to ensure that no
75215 ** other thread accesses the destination handle for the duration
75216 ** of the backup operation. Any attempt to use the destination
75217 ** database connection while a backup is in progress may cause
75218 ** a malfunction or a deadlock.
75219 */
75220 sqlite3_mutex_enter(pSrcDb->mutex);
75221 sqlite3_mutex_enter(pDestDb->mutex);
75222
75223 if( pSrcDb==pDestDb ){
75224 sqlite3ErrorWithMsg(
75225 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
75226 );
75227 p = 0;
75228 }else {
75229 /* Allocate space for a new sqlite3_backup object...
75230 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
75231 ** call to sqlite3_backup_init() and is destroyed by a call to
75232 ** sqlite3_backup_finish(). */
75233 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
75234 if( !p ){
75235 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
75236 }
75237 }
75238
75239 /* If the allocation succeeded, populate the new object. */
75240 if( p ){
75241 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
75242 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
75243 p->pDestDb = pDestDb;
75244 p->pSrcDb = pSrcDb;
75245 p->iNext = 1;
75246 p->isAttached = 0;
75247
75248 if( 0==p->pSrc || 0==p->pDest
75249 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
75250 ){
75251 /* One (or both) of the named databases did not exist or an OOM
75252 ** error was hit. Or there is a transaction open on the destination
75253 ** database. The error has already been written into the pDestDb
75254 ** handle. All that is left to do here is free the sqlite3_backup
75255 ** structure. */
75256 sqlite3_free(p);
75257 p = 0;
75258 }
75259 }
75260 if( p ){
75261 p->pSrc->nBackup++;
75262 }
75263
75264 sqlite3_mutex_leave(pDestDb->mutex);
75265 sqlite3_mutex_leave(pSrcDb->mutex);
75266 return p;
75267}
75268
75269/*
75270** Argument rc is an SQLite error code. Return true if this error is
75271** considered fatal if encountered during a backup operation. All errors
75272** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
75273*/
75274static int isFatalError(int rc){
75275 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
75276}
75277
75278/*
75279** Parameter zSrcData points to a buffer containing the data for
75280** page iSrcPg from the source database. Copy this data into the
75281** destination database.
75282*/
75283static int backupOnePage(
75284 sqlite3_backup *p, /* Backup handle */
75285 Pgno iSrcPg, /* Source database page to backup */
75286 const u8 *zSrcData, /* Source database page data */
75287 int bUpdate /* True for an update, false otherwise */
75288){
75289 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
75290 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
75291 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
75292 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
75293 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
75294 int rc = SQLITE_OK;
75295 i64 iOff;
75296
75297 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
75298 assert( p->bDestLocked );
75299 assert( !isFatalError(p->rc) );
75300 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
75301 assert( zSrcData );
75302
75303 /* Catch the case where the destination is an in-memory database and the
75304 ** page sizes of the source and destination differ.
75305 */
75306 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
75307 rc = SQLITE_READONLY;
75308 }
75309
75310 /* This loop runs once for each destination page spanned by the source
75311 ** page. For each iteration, variable iOff is set to the byte offset
75312 ** of the destination page.
75313 */
75314 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
75315 DbPage *pDestPg = 0;
75316 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
75317 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
75318 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
75319 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
75320 ){
75321 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
75322 u8 *zDestData = sqlite3PagerGetData(pDestPg);
75323 u8 *zOut = &zDestData[iOff%nDestPgsz];
75324
75325 /* Copy the data from the source page into the destination page.
75326 ** Then clear the Btree layer MemPage.isInit flag. Both this module
75327 ** and the pager code use this trick (clearing the first byte
75328 ** of the page 'extra' space to invalidate the Btree layers
75329 ** cached parse of the page). MemPage.isInit is marked
75330 ** "MUST BE FIRST" for this purpose.
75331 */
75332 memcpy(zOut, zIn, nCopy);
75333 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
75334 if( iOff==0 && bUpdate==0 ){
75335 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
75336 }
75337 }
75338 sqlite3PagerUnref(pDestPg);
75339 }
75340
75341 return rc;
75342}
75343
75344/*
75345** If pFile is currently larger than iSize bytes, then truncate it to
75346** exactly iSize bytes. If pFile is not larger than iSize bytes, then
75347** this function is a no-op.
75348**
75349** Return SQLITE_OK if everything is successful, or an SQLite error
75350** code if an error occurs.
75351*/
75352static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
75353 i64 iCurrent;
75354 int rc = sqlite3OsFileSize(pFile, &iCurrent);
75355 if( rc==SQLITE_OK && iCurrent>iSize ){
75356 rc = sqlite3OsTruncate(pFile, iSize);
75357 }
75358 return rc;
75359}
75360
75361/*
75362** Register this backup object with the associated source pager for
75363** callbacks when pages are changed or the cache invalidated.
75364*/
75365static void attachBackupObject(sqlite3_backup *p){
75366 sqlite3_backup **pp;
75367 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
75368 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
75369 p->pNext = *pp;
75370 *pp = p;
75371 p->isAttached = 1;
75372}
75373
75374/*
75375** Copy nPage pages from the source b-tree to the destination.
75376*/
75377SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
75378 int rc;
75379 int destMode; /* Destination journal mode */
75380 int pgszSrc = 0; /* Source page size */
75381 int pgszDest = 0; /* Destination page size */
75382
75383#ifdef SQLITE_ENABLE_API_ARMOR
75384 if( p==0 ) return SQLITE_MISUSE_BKPT;
75385#endif
75386 sqlite3_mutex_enter(p->pSrcDb->mutex);
75387 sqlite3BtreeEnter(p->pSrc);
75388 if( p->pDestDb ){
75389 sqlite3_mutex_enter(p->pDestDb->mutex);
75390 }
75391
75392 rc = p->rc;
75393 if( !isFatalError(rc) ){
75394 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
75395 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
75396 int ii; /* Iterator variable */
75397 int nSrcPage = -1; /* Size of source db in pages */
75398 int bCloseTrans = 0; /* True if src db requires unlocking */
75399
75400 /* If the source pager is currently in a write-transaction, return
75401 ** SQLITE_BUSY immediately.
75402 */
75403 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
75404 rc = SQLITE_BUSY;
75405 }else{
75406 rc = SQLITE_OK;
75407 }
75408
75409 /* If there is no open read-transaction on the source database, open
75410 ** one now. If a transaction is opened here, then it will be closed
75411 ** before this function exits.
75412 */
75413 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
75414 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
75415 bCloseTrans = 1;
75416 }
75417
75418 /* If the destination database has not yet been locked (i.e. if this
75419 ** is the first call to backup_step() for the current backup operation),
75420 ** try to set its page size to the same as the source database. This
75421 ** is especially important on ZipVFS systems, as in that case it is
75422 ** not possible to create a database file that uses one page size by
75423 ** writing to it with another. */
75424 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
75425 rc = SQLITE_NOMEM;
75426 }
75427
75428 /* Lock the destination database, if it is not locked already. */
75429 if( SQLITE_OK==rc && p->bDestLocked==0
75430 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
75431 (int*)&p->iDestSchema))
75432 ){
75433 p->bDestLocked = 1;
75434 }
75435
75436 /* Do not allow backup if the destination database is in WAL mode
75437 ** and the page sizes are different between source and destination */
75438 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
75439 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
75440 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
75441 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
75442 rc = SQLITE_READONLY;
75443 }
75444
75445 /* Now that there is a read-lock on the source database, query the
75446 ** source pager for the number of pages in the database.
75447 */
75448 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
75449 assert( nSrcPage>=0 );
75450 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
75451 const Pgno iSrcPg = p->iNext; /* Source page number */
75452 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
75453 DbPage *pSrcPg; /* Source page object */
75454 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
75455 if( rc==SQLITE_OK ){
75456 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
75457 sqlite3PagerUnref(pSrcPg);
75458 }
75459 }
75460 p->iNext++;
75461 }
75462 if( rc==SQLITE_OK ){
75463 p->nPagecount = nSrcPage;
75464 p->nRemaining = nSrcPage+1-p->iNext;
75465 if( p->iNext>(Pgno)nSrcPage ){
75466 rc = SQLITE_DONE;
75467 }else if( !p->isAttached ){
75468 attachBackupObject(p);
75469 }
75470 }
75471
75472 /* Update the schema version field in the destination database. This
75473 ** is to make sure that the schema-version really does change in
75474 ** the case where the source and destination databases have the
75475 ** same schema version.
75476 */
75477 if( rc==SQLITE_DONE ){
75478 if( nSrcPage==0 ){
75479 rc = sqlite3BtreeNewDb(p->pDest);
75480 nSrcPage = 1;
75481 }
75482 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
75483 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
75484 }
75485 if( rc==SQLITE_OK ){
75486 if( p->pDestDb ){
75487 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
75488 }
75489 if( destMode==PAGER_JOURNALMODE_WAL ){
75490 rc = sqlite3BtreeSetVersion(p->pDest, 2);
75491 }
75492 }
75493 if( rc==SQLITE_OK ){
75494 int nDestTruncate;
75495 /* Set nDestTruncate to the final number of pages in the destination
75496 ** database. The complication here is that the destination page
75497 ** size may be different to the source page size.
75498 **
75499 ** If the source page size is smaller than the destination page size,
75500 ** round up. In this case the call to sqlite3OsTruncate() below will
75501 ** fix the size of the file. However it is important to call
75502 ** sqlite3PagerTruncateImage() here so that any pages in the
75503 ** destination file that lie beyond the nDestTruncate page mark are
75504 ** journalled by PagerCommitPhaseOne() before they are destroyed
75505 ** by the file truncation.
75506 */
75507 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
75508 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
75509 if( pgszSrc<pgszDest ){
75510 int ratio = pgszDest/pgszSrc;
75511 nDestTruncate = (nSrcPage+ratio-1)/ratio;
75512 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
75513 nDestTruncate--;
75514 }
75515 }else{
75516 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
75517 }
75518 assert( nDestTruncate>0 );
75519
75520 if( pgszSrc<pgszDest ){
75521 /* If the source page-size is smaller than the destination page-size,
75522 ** two extra things may need to happen:
75523 **
75524 ** * The destination may need to be truncated, and
75525 **
75526 ** * Data stored on the pages immediately following the
75527 ** pending-byte page in the source database may need to be
75528 ** copied into the destination database.
75529 */
75530 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
75531 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
75532 Pgno iPg;
75533 int nDstPage;
75534 i64 iOff;
75535 i64 iEnd;
75536
75537 assert( pFile );
75538 assert( nDestTruncate==0
75539 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
75540 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
75541 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
75542 ));
75543
75544 /* This block ensures that all data required to recreate the original
75545 ** database has been stored in the journal for pDestPager and the
75546 ** journal synced to disk. So at this point we may safely modify
75547 ** the database file in any way, knowing that if a power failure
75548 ** occurs, the original database will be reconstructed from the
75549 ** journal file. */
75550 sqlite3PagerPagecount(pDestPager, &nDstPage);
75551 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
75552 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
75553 DbPage *pPg;
75554 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
75555 if( rc==SQLITE_OK ){
75556 rc = sqlite3PagerWrite(pPg);
75557 sqlite3PagerUnref(pPg);
75558 }
75559 }
75560 }
75561 if( rc==SQLITE_OK ){
75562 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
75563 }
75564
75565 /* Write the extra pages and truncate the database file as required */
75566 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
75567 for(
75568 iOff=PENDING_BYTE+pgszSrc;
75569 rc==SQLITE_OK && iOff<iEnd;
75570 iOff+=pgszSrc
75571 ){
75572 PgHdr *pSrcPg = 0;
75573 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
75574 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
75575 if( rc==SQLITE_OK ){
75576 u8 *zData = sqlite3PagerGetData(pSrcPg);
75577 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
75578 }
75579 sqlite3PagerUnref(pSrcPg);
75580 }
75581 if( rc==SQLITE_OK ){
75582 rc = backupTruncateFile(pFile, iSize);
75583 }
75584
75585 /* Sync the database file to disk. */
75586 if( rc==SQLITE_OK ){
75587 rc = sqlite3PagerSync(pDestPager, 0);
75588 }
75589 }else{
75590 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
75591 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
75592 }
75593
75594 /* Finish committing the transaction to the destination database. */
75595 if( SQLITE_OK==rc
75596 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
75597 ){
75598 rc = SQLITE_DONE;
75599 }
75600 }
75601 }
75602
75603 /* If bCloseTrans is true, then this function opened a read transaction
75604 ** on the source database. Close the read transaction here. There is
75605 ** no need to check the return values of the btree methods here, as
75606 ** "committing" a read-only transaction cannot fail.
75607 */
75608 if( bCloseTrans ){
75609 TESTONLY( int rc2 );
75610 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
75611 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
75612 assert( rc2==SQLITE_OK );
75613 }
75614
75615 if( rc==SQLITE_IOERR_NOMEM ){
75616 rc = SQLITE_NOMEM_BKPT;
75617 }
75618 p->rc = rc;
75619 }
75620 if( p->pDestDb ){
75621 sqlite3_mutex_leave(p->pDestDb->mutex);
75622 }
75623 sqlite3BtreeLeave(p->pSrc);
75624 sqlite3_mutex_leave(p->pSrcDb->mutex);
75625 return rc;
75626}
75627
75628/*
75629** Release all resources associated with an sqlite3_backup* handle.
75630*/
75631SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
75632 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
75633 sqlite3 *pSrcDb; /* Source database connection */
75634 int rc; /* Value to return */
75635
75636 /* Enter the mutexes */
75637 if( p==0 ) return SQLITE_OK;
75638 pSrcDb = p->pSrcDb;
75639 sqlite3_mutex_enter(pSrcDb->mutex);
75640 sqlite3BtreeEnter(p->pSrc);
75641 if( p->pDestDb ){
75642 sqlite3_mutex_enter(p->pDestDb->mutex);
75643 }
75644
75645 /* Detach this backup from the source pager. */
75646 if( p->pDestDb ){
75647 p->pSrc->nBackup--;
75648 }
75649 if( p->isAttached ){
75650 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
75651 assert( pp!=0 );
75652 while( *pp!=p ){
75653 pp = &(*pp)->pNext;
75654 assert( pp!=0 );
75655 }
75656 *pp = p->pNext;
75657 }
75658
75659 /* If a transaction is still open on the Btree, roll it back. */
75660 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
75661
75662 /* Set the error code of the destination database handle. */
75663 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
75664 if( p->pDestDb ){
75665 sqlite3Error(p->pDestDb, rc);
75666
75667 /* Exit the mutexes and free the backup context structure. */
75668 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
75669 }
75670 sqlite3BtreeLeave(p->pSrc);
75671 if( p->pDestDb ){
75672 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
75673 ** call to sqlite3_backup_init() and is destroyed by a call to
75674 ** sqlite3_backup_finish(). */
75675 sqlite3_free(p);
75676 }
75677 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
75678 return rc;
75679}
75680
75681/*
75682** Return the number of pages still to be backed up as of the most recent
75683** call to sqlite3_backup_step().
75684*/
75685SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
75686#ifdef SQLITE_ENABLE_API_ARMOR
75687 if( p==0 ){
75688 (void)SQLITE_MISUSE_BKPT;
75689 return 0;
75690 }
75691#endif
75692 return p->nRemaining;
75693}
75694
75695/*
75696** Return the total number of pages in the source database as of the most
75697** recent call to sqlite3_backup_step().
75698*/
75699SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
75700#ifdef SQLITE_ENABLE_API_ARMOR
75701 if( p==0 ){
75702 (void)SQLITE_MISUSE_BKPT;
75703 return 0;
75704 }
75705#endif
75706 return p->nPagecount;
75707}
75708
75709/*
75710** This function is called after the contents of page iPage of the
75711** source database have been modified. If page iPage has already been
75712** copied into the destination database, then the data written to the
75713** destination is now invalidated. The destination copy of iPage needs
75714** to be updated with the new data before the backup operation is
75715** complete.
75716**
75717** It is assumed that the mutex associated with the BtShared object
75718** corresponding to the source database is held when this function is
75719** called.
75720*/
75721static SQLITE_NOINLINE void backupUpdate(
75722 sqlite3_backup *p,
75723 Pgno iPage,
75724 const u8 *aData
75725){
75726 assert( p!=0 );
75727 do{
75728 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
75729 if( !isFatalError(p->rc) && iPage<p->iNext ){
75730 /* The backup process p has already copied page iPage. But now it
75731 ** has been modified by a transaction on the source pager. Copy
75732 ** the new data into the backup.
75733 */
75734 int rc;
75735 assert( p->pDestDb );
75736 sqlite3_mutex_enter(p->pDestDb->mutex);
75737 rc = backupOnePage(p, iPage, aData, 1);
75738 sqlite3_mutex_leave(p->pDestDb->mutex);
75739 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
75740 if( rc!=SQLITE_OK ){
75741 p->rc = rc;
75742 }
75743 }
75744 }while( (p = p->pNext)!=0 );
75745}
75746SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
75747 if( pBackup ) backupUpdate(pBackup, iPage, aData);
75748}
75749
75750/*
75751** Restart the backup process. This is called when the pager layer
75752** detects that the database has been modified by an external database
75753** connection. In this case there is no way of knowing which of the
75754** pages that have been copied into the destination database are still
75755** valid and which are not, so the entire process needs to be restarted.
75756**
75757** It is assumed that the mutex associated with the BtShared object
75758** corresponding to the source database is held when this function is
75759** called.
75760*/
75761SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
75762 sqlite3_backup *p; /* Iterator variable */
75763 for(p=pBackup; p; p=p->pNext){
75764 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
75765 p->iNext = 1;
75766 }
75767}
75768
75769#ifndef SQLITE_OMIT_VACUUM
75770/*
75771** Copy the complete content of pBtFrom into pBtTo. A transaction
75772** must be active for both files.
75773**
75774** The size of file pTo may be reduced by this operation. If anything
75775** goes wrong, the transaction on pTo is rolled back. If successful, the
75776** transaction is committed before returning.
75777*/
75778SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
75779 int rc;
75780 sqlite3_file *pFd; /* File descriptor for database pTo */
75781 sqlite3_backup b;
75782 sqlite3BtreeEnter(pTo);
75783 sqlite3BtreeEnter(pFrom);
75784
75785 assert( sqlite3BtreeIsInTrans(pTo) );
75786 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
75787 if( pFd->pMethods ){
75788 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
75789 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
75790 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
75791 if( rc ) goto copy_finished;
75792 }
75793
75794 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
75795 ** to 0. This is used by the implementations of sqlite3_backup_step()
75796 ** and sqlite3_backup_finish() to detect that they are being called
75797 ** from this function, not directly by the user.
75798 */
75799 memset(&b, 0, sizeof(b));
75800 b.pSrcDb = pFrom->db;
75801 b.pSrc = pFrom;
75802 b.pDest = pTo;
75803 b.iNext = 1;
75804
75805 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
75806 ** file. By passing this as the number of pages to copy to
75807 ** sqlite3_backup_step(), we can guarantee that the copy finishes
75808 ** within a single call (unless an error occurs). The assert() statement
75809 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
75810 ** or an error code. */
75811 sqlite3_backup_step(&b, 0x7FFFFFFF);
75812 assert( b.rc!=SQLITE_OK );
75813
75814 rc = sqlite3_backup_finish(&b);
75815 if( rc==SQLITE_OK ){
75816 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
75817 }else{
75818 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
75819 }
75820
75821 assert( sqlite3BtreeIsInTrans(pTo)==0 );
75822copy_finished:
75823 sqlite3BtreeLeave(pFrom);
75824 sqlite3BtreeLeave(pTo);
75825 return rc;
75826}
75827#endif /* SQLITE_OMIT_VACUUM */
75828
75829/************** End of backup.c **********************************************/
75830/************** Begin file vdbemem.c *****************************************/
75831/*
75832** 2004 May 26
75833**
75834** The author disclaims copyright to this source code. In place of
75835** a legal notice, here is a blessing:
75836**
75837** May you do good and not evil.
75838** May you find forgiveness for yourself and forgive others.
75839** May you share freely, never taking more than you give.
75840**
75841*************************************************************************
75842**
75843** This file contains code use to manipulate "Mem" structure. A "Mem"
75844** stores a single value in the VDBE. Mem is an opaque structure visible
75845** only within the VDBE. Interface routines refer to a Mem using the
75846** name sqlite_value
75847*/
75848/* #include "sqliteInt.h" */
75849/* #include "vdbeInt.h" */
75850
75851/* True if X is a power of two. 0 is considered a power of two here.
75852** In other words, return true if X has at most one bit set.
75853*/
75854#define ISPOWEROF2(X) (((X)&((X)-1))==0)
75855
75856#ifdef SQLITE_DEBUG
75857/*
75858** Check invariants on a Mem object.
75859**
75860** This routine is intended for use inside of assert() statements, like
75861** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
75862*/
75863SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
75864 /* If MEM_Dyn is set then Mem.xDel!=0.
75865 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
75866 */
75867 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
75868
75869 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
75870 ** ensure that if Mem.szMalloc>0 then it is safe to do
75871 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
75872 ** That saves a few cycles in inner loops. */
75873 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
75874
75875 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
75876 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
75877
75878 if( p->flags & MEM_Null ){
75879 /* Cannot be both MEM_Null and some other type */
75880 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
75881
75882 /* If MEM_Null is set, then either the value is a pure NULL (the usual
75883 ** case) or it is a pointer set using sqlite3_bind_pointer() or
75884 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
75885 ** set.
75886 */
75887 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
75888 /* This is a pointer type. There may be a flag to indicate what to
75889 ** do with the pointer. */
75890 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
75891 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
75892 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
75893
75894 /* No other bits set */
75895 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
75896 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
75897 }else{
75898 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
75899 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
75900 }
75901 }else{
75902 /* The MEM_Cleared bit is only allowed on NULLs */
75903 assert( (p->flags & MEM_Cleared)==0 );
75904 }
75905
75906 /* The szMalloc field holds the correct memory allocation size */
75907 assert( p->szMalloc==0
75908 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
75909
75910 /* If p holds a string or blob, the Mem.z must point to exactly
75911 ** one of the following:
75912 **
75913 ** (1) Memory in Mem.zMalloc and managed by the Mem object
75914 ** (2) Memory to be freed using Mem.xDel
75915 ** (3) An ephemeral string or blob
75916 ** (4) A static string or blob
75917 */
75918 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
75919 assert(
75920 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
75921 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
75922 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
75923 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
75924 );
75925 }
75926 return 1;
75927}
75928#endif
75929
75930/*
75931** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
75932** into a buffer.
75933*/
75934static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
75935 StrAccum acc;
75936 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
75937 assert( sz>22 );
75938 if( p->flags & MEM_Int ){
75939#if GCC_VERSION>=7000000
75940 /* Work-around for GCC bug
75941 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
75942 i64 x;
75943 assert( (p->flags&MEM_Int)*2==sizeof(x) );
75944 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
75945 sqlite3Int64ToText(x, zBuf);
75946#else
75947 sqlite3Int64ToText(p->u.i, zBuf);
75948#endif
75949 }else{
75950 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
75951 sqlite3_str_appendf(&acc, "%!.15g",
75952 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
75953 assert( acc.zText==zBuf && acc.mxAlloc<=0 );
75954 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
75955 }
75956}
75957
75958#ifdef SQLITE_DEBUG
75959/*
75960** Validity checks on pMem. pMem holds a string.
75961**
75962** (1) Check that string value of pMem agrees with its integer or real value.
75963** (2) Check that the string is correctly zero terminated
75964**
75965** A single int or real value always converts to the same strings. But
75966** many different strings can be converted into the same int or real.
75967** If a table contains a numeric value and an index is based on the
75968** corresponding string value, then it is important that the string be
75969** derived from the numeric value, not the other way around, to ensure
75970** that the index and table are consistent. See ticket
75971** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
75972** an example.
75973**
75974** This routine looks at pMem to verify that if it has both a numeric
75975** representation and a string representation then the string rep has
75976** been derived from the numeric and not the other way around. It returns
75977** true if everything is ok and false if there is a problem.
75978**
75979** This routine is for use inside of assert() statements only.
75980*/
75981SQLITE_PRIVATE int sqlite3VdbeMemValidStrRep(Mem *p){
75982 char zBuf[100];
75983 char *z;
75984 int i, j, incr;
75985 if( (p->flags & MEM_Str)==0 ) return 1;
75986 if( p->flags & MEM_Term ){
75987 /* Insure that the string is properly zero-terminated. Pay particular
75988 ** attention to the case where p->n is odd */
75989 if( p->szMalloc>0 && p->z==p->zMalloc ){
75990 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
75991 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
75992 }
75993 assert( p->z[p->n]==0 );
75994 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
75995 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
75996 }
75997 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
75998 vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
75999 z = p->z;
76000 i = j = 0;
76001 incr = 1;
76002 if( p->enc!=SQLITE_UTF8 ){
76003 incr = 2;
76004 if( p->enc==SQLITE_UTF16BE ) z++;
76005 }
76006 while( zBuf[j] ){
76007 if( zBuf[j++]!=z[i] ) return 0;
76008 i += incr;
76009 }
76010 return 1;
76011}
76012#endif /* SQLITE_DEBUG */
76013
76014/*
76015** If pMem is an object with a valid string representation, this routine
76016** ensures the internal encoding for the string representation is
76017** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
76018**
76019** If pMem is not a string object, or the encoding of the string
76020** representation is already stored using the requested encoding, then this
76021** routine is a no-op.
76022**
76023** SQLITE_OK is returned if the conversion is successful (or not required).
76024** SQLITE_NOMEM may be returned if a malloc() fails during conversion
76025** between formats.
76026*/
76027SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
76028#ifndef SQLITE_OMIT_UTF16
76029 int rc;
76030#endif
76031 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76032 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
76033 || desiredEnc==SQLITE_UTF16BE );
76034 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
76035 return SQLITE_OK;
76036 }
76037 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76038#ifdef SQLITE_OMIT_UTF16
76039 return SQLITE_ERROR;
76040#else
76041
76042 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
76043 ** then the encoding of the value may not have changed.
76044 */
76045 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
76046 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
76047 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
76048 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
76049 return rc;
76050#endif
76051}
76052
76053/*
76054** Make sure pMem->z points to a writable allocation of at least n bytes.
76055**
76056** If the bPreserve argument is true, then copy of the content of
76057** pMem->z into the new allocation. pMem must be either a string or
76058** blob if bPreserve is true. If bPreserve is false, any prior content
76059** in pMem->z is discarded.
76060*/
76061SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
76062 assert( sqlite3VdbeCheckMemInvariants(pMem) );
76063 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76064 testcase( pMem->db==0 );
76065
76066 /* If the bPreserve flag is set to true, then the memory cell must already
76067 ** contain a valid string or blob value. */
76068 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
76069 testcase( bPreserve && pMem->z==0 );
76070
76071 assert( pMem->szMalloc==0
76072 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
76073 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
76074 if( pMem->db ){
76075 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
76076 }else{
76077 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
76078 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
76079 pMem->z = pMem->zMalloc;
76080 }
76081 bPreserve = 0;
76082 }else{
76083 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
76084 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
76085 }
76086 if( pMem->zMalloc==0 ){
76087 sqlite3VdbeMemSetNull(pMem);
76088 pMem->z = 0;
76089 pMem->szMalloc = 0;
76090 return SQLITE_NOMEM_BKPT;
76091 }else{
76092 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
76093 }
76094
76095 if( bPreserve && pMem->z ){
76096 assert( pMem->z!=pMem->zMalloc );
76097 memcpy(pMem->zMalloc, pMem->z, pMem->n);
76098 }
76099 if( (pMem->flags&MEM_Dyn)!=0 ){
76100 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
76101 pMem->xDel((void *)(pMem->z));
76102 }
76103
76104 pMem->z = pMem->zMalloc;
76105 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
76106 return SQLITE_OK;
76107}
76108
76109/*
76110** Change the pMem->zMalloc allocation to be at least szNew bytes.
76111** If pMem->zMalloc already meets or exceeds the requested size, this
76112** routine is a no-op.
76113**
76114** Any prior string or blob content in the pMem object may be discarded.
76115** The pMem->xDel destructor is called, if it exists. Though MEM_Str
76116** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
76117** and MEM_Null values are preserved.
76118**
76119** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
76120** if unable to complete the resizing.
76121*/
76122SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
76123 assert( CORRUPT_DB || szNew>0 );
76124 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
76125 if( pMem->szMalloc<szNew ){
76126 return sqlite3VdbeMemGrow(pMem, szNew, 0);
76127 }
76128 assert( (pMem->flags & MEM_Dyn)==0 );
76129 pMem->z = pMem->zMalloc;
76130 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
76131 return SQLITE_OK;
76132}
76133
76134/*
76135** It is already known that pMem contains an unterminated string.
76136** Add the zero terminator.
76137**
76138** Three bytes of zero are added. In this way, there is guaranteed
76139** to be a double-zero byte at an even byte boundary in order to
76140** terminate a UTF16 string, even if the initial size of the buffer
76141** is an odd number of bytes.
76142*/
76143static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
76144 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
76145 return SQLITE_NOMEM_BKPT;
76146 }
76147 pMem->z[pMem->n] = 0;
76148 pMem->z[pMem->n+1] = 0;
76149 pMem->z[pMem->n+2] = 0;
76150 pMem->flags |= MEM_Term;
76151 return SQLITE_OK;
76152}
76153
76154/*
76155** Change pMem so that its MEM_Str or MEM_Blob value is stored in
76156** MEM.zMalloc, where it can be safely written.
76157**
76158** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
76159*/
76160SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
76161 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76162 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76163 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
76164 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
76165 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
76166 int rc = vdbeMemAddTerminator(pMem);
76167 if( rc ) return rc;
76168 }
76169 }
76170 pMem->flags &= ~MEM_Ephem;
76171#ifdef SQLITE_DEBUG
76172 pMem->pScopyFrom = 0;
76173#endif
76174
76175 return SQLITE_OK;
76176}
76177
76178/*
76179** If the given Mem* has a zero-filled tail, turn it into an ordinary
76180** blob stored in dynamically allocated space.
76181*/
76182#ifndef SQLITE_OMIT_INCRBLOB
76183SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
76184 int nByte;
76185 assert( pMem->flags & MEM_Zero );
76186 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
76187 testcase( sqlite3_value_nochange(pMem) );
76188 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76189 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76190
76191 /* Set nByte to the number of bytes required to store the expanded blob. */
76192 nByte = pMem->n + pMem->u.nZero;
76193 if( nByte<=0 ){
76194 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
76195 nByte = 1;
76196 }
76197 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
76198 return SQLITE_NOMEM_BKPT;
76199 }
76200
76201 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
76202 pMem->n += pMem->u.nZero;
76203 pMem->flags &= ~(MEM_Zero|MEM_Term);
76204 return SQLITE_OK;
76205}
76206#endif
76207
76208/*
76209** Make sure the given Mem is \u0000 terminated.
76210*/
76211SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
76212 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76213 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
76214 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
76215 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
76216 return SQLITE_OK; /* Nothing to do */
76217 }else{
76218 return vdbeMemAddTerminator(pMem);
76219 }
76220}
76221
76222/*
76223** Add MEM_Str to the set of representations for the given Mem. This
76224** routine is only called if pMem is a number of some kind, not a NULL
76225** or a BLOB.
76226**
76227** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
76228** if bForce is true but are retained if bForce is false.
76229**
76230** A MEM_Null value will never be passed to this function. This function is
76231** used for converting values to text for returning to the user (i.e. via
76232** sqlite3_value_text()), or for ensuring that values to be used as btree
76233** keys are strings. In the former case a NULL pointer is returned the
76234** user and the latter is an internal programming error.
76235*/
76236SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
76237 const int nByte = 32;
76238
76239 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76240 assert( !(pMem->flags&MEM_Zero) );
76241 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
76242 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
76243 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76244 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76245
76246
76247 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
76248 pMem->enc = 0;
76249 return SQLITE_NOMEM_BKPT;
76250 }
76251
76252 vdbeMemRenderNum(nByte, pMem->z, pMem);
76253 assert( pMem->z!=0 );
76254 pMem->n = sqlite3Strlen30NN(pMem->z);
76255 pMem->enc = SQLITE_UTF8;
76256 pMem->flags |= MEM_Str|MEM_Term;
76257 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
76258 sqlite3VdbeChangeEncoding(pMem, enc);
76259 return SQLITE_OK;
76260}
76261
76262/*
76263** Memory cell pMem contains the context of an aggregate function.
76264** This routine calls the finalize method for that function. The
76265** result of the aggregate is stored back into pMem.
76266**
76267** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
76268** otherwise.
76269*/
76270SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
76271 sqlite3_context ctx;
76272 Mem t;
76273 assert( pFunc!=0 );
76274 assert( pFunc->xFinalize!=0 );
76275 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
76276 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76277 memset(&ctx, 0, sizeof(ctx));
76278 memset(&t, 0, sizeof(t));
76279 t.flags = MEM_Null;
76280 t.db = pMem->db;
76281 ctx.pOut = &t;
76282 ctx.pMem = pMem;
76283 ctx.pFunc = pFunc;
76284 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
76285 assert( (pMem->flags & MEM_Dyn)==0 );
76286 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
76287 memcpy(pMem, &t, sizeof(t));
76288 return ctx.isError;
76289}
76290
76291/*
76292** Memory cell pAccum contains the context of an aggregate function.
76293** This routine calls the xValue method for that function and stores
76294** the results in memory cell pMem.
76295**
76296** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
76297** otherwise.
76298*/
76299#ifndef SQLITE_OMIT_WINDOWFUNC
76300SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
76301 sqlite3_context ctx;
76302 assert( pFunc!=0 );
76303 assert( pFunc->xValue!=0 );
76304 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
76305 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
76306 memset(&ctx, 0, sizeof(ctx));
76307 sqlite3VdbeMemSetNull(pOut);
76308 ctx.pOut = pOut;
76309 ctx.pMem = pAccum;
76310 ctx.pFunc = pFunc;
76311 pFunc->xValue(&ctx);
76312 return ctx.isError;
76313}
76314#endif /* SQLITE_OMIT_WINDOWFUNC */
76315
76316/*
76317** If the memory cell contains a value that must be freed by
76318** invoking the external callback in Mem.xDel, then this routine
76319** will free that value. It also sets Mem.flags to MEM_Null.
76320**
76321** This is a helper routine for sqlite3VdbeMemSetNull() and
76322** for sqlite3VdbeMemRelease(). Use those other routines as the
76323** entry point for releasing Mem resources.
76324*/
76325static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
76326 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
76327 assert( VdbeMemDynamic(p) );
76328 if( p->flags&MEM_Agg ){
76329 sqlite3VdbeMemFinalize(p, p->u.pDef);
76330 assert( (p->flags & MEM_Agg)==0 );
76331 testcase( p->flags & MEM_Dyn );
76332 }
76333 if( p->flags&MEM_Dyn ){
76334 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
76335 p->xDel((void *)p->z);
76336 }
76337 p->flags = MEM_Null;
76338}
76339
76340/*
76341** Release memory held by the Mem p, both external memory cleared
76342** by p->xDel and memory in p->zMalloc.
76343**
76344** This is a helper routine invoked by sqlite3VdbeMemRelease() in
76345** the unusual case where there really is memory in p that needs
76346** to be freed.
76347*/
76348static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
76349 if( VdbeMemDynamic(p) ){
76350 vdbeMemClearExternAndSetNull(p);
76351 }
76352 if( p->szMalloc ){
76353 sqlite3DbFreeNN(p->db, p->zMalloc);
76354 p->szMalloc = 0;
76355 }
76356 p->z = 0;
76357}
76358
76359/*
76360** Release any memory resources held by the Mem. Both the memory that is
76361** free by Mem.xDel and the Mem.zMalloc allocation are freed.
76362**
76363** Use this routine prior to clean up prior to abandoning a Mem, or to
76364** reset a Mem back to its minimum memory utilization.
76365**
76366** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
76367** prior to inserting new content into the Mem.
76368*/
76369SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
76370 assert( sqlite3VdbeCheckMemInvariants(p) );
76371 if( VdbeMemDynamic(p) || p->szMalloc ){
76372 vdbeMemClear(p);
76373 }
76374}
76375
76376/*
76377** Convert a 64-bit IEEE double into a 64-bit signed integer.
76378** If the double is out of range of a 64-bit signed integer then
76379** return the closest available 64-bit signed integer.
76380*/
76381static SQLITE_NOINLINE i64 doubleToInt64(double r){
76382#ifdef SQLITE_OMIT_FLOATING_POINT
76383 /* When floating-point is omitted, double and int64 are the same thing */
76384 return r;
76385#else
76386 /*
76387 ** Many compilers we encounter do not define constants for the
76388 ** minimum and maximum 64-bit integers, or they define them
76389 ** inconsistently. And many do not understand the "LL" notation.
76390 ** So we define our own static constants here using nothing
76391 ** larger than a 32-bit integer constant.
76392 */
76393 static const i64 maxInt = LARGEST_INT64;
76394 static const i64 minInt = SMALLEST_INT64;
76395
76396 if( r<=(double)minInt ){
76397 return minInt;
76398 }else if( r>=(double)maxInt ){
76399 return maxInt;
76400 }else{
76401 return (i64)r;
76402 }
76403#endif
76404}
76405
76406/*
76407** Return some kind of integer value which is the best we can do
76408** at representing the value that *pMem describes as an integer.
76409** If pMem is an integer, then the value is exact. If pMem is
76410** a floating-point then the value returned is the integer part.
76411** If pMem is a string or blob, then we make an attempt to convert
76412** it into an integer and return that. If pMem represents an
76413** an SQL-NULL value, return 0.
76414**
76415** If pMem represents a string value, its encoding might be changed.
76416*/
76417static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
76418 i64 value = 0;
76419 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
76420 return value;
76421}
76422SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
76423 int flags;
76424 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76425 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76426 flags = pMem->flags;
76427 if( flags & (MEM_Int|MEM_IntReal) ){
76428 testcase( flags & MEM_IntReal );
76429 return pMem->u.i;
76430 }else if( flags & MEM_Real ){
76431 return doubleToInt64(pMem->u.r);
76432 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
76433 return memIntValue(pMem);
76434 }else{
76435 return 0;
76436 }
76437}
76438
76439/*
76440** Return the best representation of pMem that we can get into a
76441** double. If pMem is already a double or an integer, return its
76442** value. If it is a string or blob, try to convert it to a double.
76443** If it is a NULL, return 0.0.
76444*/
76445static SQLITE_NOINLINE double memRealValue(Mem *pMem){
76446 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
76447 double val = (double)0;
76448 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
76449 return val;
76450}
76451SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
76452 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76453 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76454 if( pMem->flags & MEM_Real ){
76455 return pMem->u.r;
76456 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
76457 testcase( pMem->flags & MEM_IntReal );
76458 return (double)pMem->u.i;
76459 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
76460 return memRealValue(pMem);
76461 }else{
76462 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
76463 return (double)0;
76464 }
76465}
76466
76467/*
76468** Return 1 if pMem represents true, and return 0 if pMem represents false.
76469** Return the value ifNull if pMem is NULL.
76470*/
76471SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
76472 testcase( pMem->flags & MEM_IntReal );
76473 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
76474 if( pMem->flags & MEM_Null ) return ifNull;
76475 return sqlite3VdbeRealValue(pMem)!=0.0;
76476}
76477
76478/*
76479** The MEM structure is already a MEM_Real. Try to also make it a
76480** MEM_Int if we can.
76481*/
76482SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
76483 i64 ix;
76484 assert( pMem->flags & MEM_Real );
76485 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76486 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76487 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76488
76489 ix = doubleToInt64(pMem->u.r);
76490
76491 /* Only mark the value as an integer if
76492 **
76493 ** (1) the round-trip conversion real->int->real is a no-op, and
76494 ** (2) The integer is neither the largest nor the smallest
76495 ** possible integer (ticket #3922)
76496 **
76497 ** The second and third terms in the following conditional enforces
76498 ** the second condition under the assumption that addition overflow causes
76499 ** values to wrap around.
76500 */
76501 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
76502 pMem->u.i = ix;
76503 MemSetTypeFlag(pMem, MEM_Int);
76504 }
76505}
76506
76507/*
76508** Convert pMem to type integer. Invalidate any prior representations.
76509*/
76510SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
76511 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76512 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76513 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76514
76515 pMem->u.i = sqlite3VdbeIntValue(pMem);
76516 MemSetTypeFlag(pMem, MEM_Int);
76517 return SQLITE_OK;
76518}
76519
76520/*
76521** Convert pMem so that it is of type MEM_Real.
76522** Invalidate any prior representations.
76523*/
76524SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
76525 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76526 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
76527
76528 pMem->u.r = sqlite3VdbeRealValue(pMem);
76529 MemSetTypeFlag(pMem, MEM_Real);
76530 return SQLITE_OK;
76531}
76532
76533/* Compare a floating point value to an integer. Return true if the two
76534** values are the same within the precision of the floating point value.
76535**
76536** This function assumes that i was obtained by assignment from r1.
76537**
76538** For some versions of GCC on 32-bit machines, if you do the more obvious
76539** comparison of "r1==(double)i" you sometimes get an answer of false even
76540** though the r1 and (double)i values are bit-for-bit the same.
76541*/
76542SQLITE_PRIVATE int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
76543 double r2 = (double)i;
76544 return r1==0.0
76545 || (memcmp(&r1, &r2, sizeof(r1))==0
76546 && i >= -2251799813685248LL && i < 2251799813685248LL);
76547}
76548
76549/*
76550** Convert pMem so that it has type MEM_Real or MEM_Int.
76551** Invalidate any prior representations.
76552**
76553** Every effort is made to force the conversion, even if the input
76554** is a string that does not look completely like a number. Convert
76555** as much of the string as we can and ignore the rest.
76556*/
76557SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
76558 testcase( pMem->flags & MEM_Int );
76559 testcase( pMem->flags & MEM_Real );
76560 testcase( pMem->flags & MEM_IntReal );
76561 testcase( pMem->flags & MEM_Null );
76562 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
76563 int rc;
76564 sqlite3_int64 ix;
76565 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
76566 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76567 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
76568 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
76569 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
76570 ){
76571 pMem->u.i = ix;
76572 MemSetTypeFlag(pMem, MEM_Int);
76573 }else{
76574 MemSetTypeFlag(pMem, MEM_Real);
76575 }
76576 }
76577 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
76578 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
76579 return SQLITE_OK;
76580}
76581
76582/*
76583** Cast the datatype of the value in pMem according to the affinity
76584** "aff". Casting is different from applying affinity in that a cast
76585** is forced. In other words, the value is converted into the desired
76586** affinity even if that results in loss of data. This routine is
76587** used (for example) to implement the SQL "cast()" operator.
76588*/
76589SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
76590 if( pMem->flags & MEM_Null ) return SQLITE_OK;
76591 switch( aff ){
76592 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
76593 if( (pMem->flags & MEM_Blob)==0 ){
76594 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
76595 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
76596 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
76597 }else{
76598 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
76599 }
76600 break;
76601 }
76602 case SQLITE_AFF_NUMERIC: {
76603 sqlite3VdbeMemNumerify(pMem);
76604 break;
76605 }
76606 case SQLITE_AFF_INTEGER: {
76607 sqlite3VdbeMemIntegerify(pMem);
76608 break;
76609 }
76610 case SQLITE_AFF_REAL: {
76611 sqlite3VdbeMemRealify(pMem);
76612 break;
76613 }
76614 default: {
76615 assert( aff==SQLITE_AFF_TEXT );
76616 assert( MEM_Str==(MEM_Blob>>3) );
76617 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
76618 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
76619 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
76620 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
76621 return sqlite3VdbeChangeEncoding(pMem, encoding);
76622 }
76623 }
76624 return SQLITE_OK;
76625}
76626
76627/*
76628** Initialize bulk memory to be a consistent Mem object.
76629**
76630** The minimum amount of initialization feasible is performed.
76631*/
76632SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
76633 assert( (flags & ~MEM_TypeMask)==0 );
76634 pMem->flags = flags;
76635 pMem->db = db;
76636 pMem->szMalloc = 0;
76637}
76638
76639
76640/*
76641** Delete any previous value and set the value stored in *pMem to NULL.
76642**
76643** This routine calls the Mem.xDel destructor to dispose of values that
76644** require the destructor. But it preserves the Mem.zMalloc memory allocation.
76645** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
76646** routine to invoke the destructor and deallocates Mem.zMalloc.
76647**
76648** Use this routine to reset the Mem prior to insert a new value.
76649**
76650** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
76651*/
76652SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
76653 if( VdbeMemDynamic(pMem) ){
76654 vdbeMemClearExternAndSetNull(pMem);
76655 }else{
76656 pMem->flags = MEM_Null;
76657 }
76658}
76659SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
76660 sqlite3VdbeMemSetNull((Mem*)p);
76661}
76662
76663/*
76664** Delete any previous value and set the value to be a BLOB of length
76665** n containing all zeros.
76666*/
76667SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
76668 sqlite3VdbeMemRelease(pMem);
76669 pMem->flags = MEM_Blob|MEM_Zero;
76670 pMem->n = 0;
76671 if( n<0 ) n = 0;
76672 pMem->u.nZero = n;
76673 pMem->enc = SQLITE_UTF8;
76674 pMem->z = 0;
76675}
76676
76677/*
76678** The pMem is known to contain content that needs to be destroyed prior
76679** to a value change. So invoke the destructor, then set the value to
76680** a 64-bit integer.
76681*/
76682static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
76683 sqlite3VdbeMemSetNull(pMem);
76684 pMem->u.i = val;
76685 pMem->flags = MEM_Int;
76686}
76687
76688/*
76689** Delete any previous value and set the value stored in *pMem to val,
76690** manifest type INTEGER.
76691*/
76692SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
76693 if( VdbeMemDynamic(pMem) ){
76694 vdbeReleaseAndSetInt64(pMem, val);
76695 }else{
76696 pMem->u.i = val;
76697 pMem->flags = MEM_Int;
76698 }
76699}
76700
76701/* A no-op destructor */
76702SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
76703
76704/*
76705** Set the value stored in *pMem should already be a NULL.
76706** Also store a pointer to go with it.
76707*/
76708SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
76709 Mem *pMem,
76710 void *pPtr,
76711 const char *zPType,
76712 void (*xDestructor)(void*)
76713){
76714 assert( pMem->flags==MEM_Null );
76715 pMem->u.zPType = zPType ? zPType : "";
76716 pMem->z = pPtr;
76717 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
76718 pMem->eSubtype = 'p';
76719 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
76720}
76721
76722#ifndef SQLITE_OMIT_FLOATING_POINT
76723/*
76724** Delete any previous value and set the value stored in *pMem to val,
76725** manifest type REAL.
76726*/
76727SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
76728 sqlite3VdbeMemSetNull(pMem);
76729 if( !sqlite3IsNaN(val) ){
76730 pMem->u.r = val;
76731 pMem->flags = MEM_Real;
76732 }
76733}
76734#endif
76735
76736#ifdef SQLITE_DEBUG
76737/*
76738** Return true if the Mem holds a RowSet object. This routine is intended
76739** for use inside of assert() statements.
76740*/
76741SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
76742 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
76743 && pMem->xDel==sqlite3RowSetDelete;
76744}
76745#endif
76746
76747/*
76748** Delete any previous value and set the value of pMem to be an
76749** empty boolean index.
76750**
76751** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
76752** error occurs.
76753*/
76754SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
76755 sqlite3 *db = pMem->db;
76756 RowSet *p;
76757 assert( db!=0 );
76758 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76759 sqlite3VdbeMemRelease(pMem);
76760 p = sqlite3RowSetInit(db);
76761 if( p==0 ) return SQLITE_NOMEM;
76762 pMem->z = (char*)p;
76763 pMem->flags = MEM_Blob|MEM_Dyn;
76764 pMem->xDel = sqlite3RowSetDelete;
76765 return SQLITE_OK;
76766}
76767
76768/*
76769** Return true if the Mem object contains a TEXT or BLOB that is
76770** too large - whose size exceeds SQLITE_MAX_LENGTH.
76771*/
76772SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
76773 assert( p->db!=0 );
76774 if( p->flags & (MEM_Str|MEM_Blob) ){
76775 int n = p->n;
76776 if( p->flags & MEM_Zero ){
76777 n += p->u.nZero;
76778 }
76779 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
76780 }
76781 return 0;
76782}
76783
76784#ifdef SQLITE_DEBUG
76785/*
76786** This routine prepares a memory cell for modification by breaking
76787** its link to a shallow copy and by marking any current shallow
76788** copies of this cell as invalid.
76789**
76790** This is used for testing and debugging only - to help ensure that shallow
76791** copies (created by OP_SCopy) are not misused.
76792*/
76793SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
76794 int i;
76795 Mem *pX;
76796 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
76797 if( pX->pScopyFrom==pMem ){
76798 u16 mFlags;
76799 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
76800 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
76801 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
76802 }
76803 /* If pX is marked as a shallow copy of pMem, then try to verify that
76804 ** no significant changes have been made to pX since the OP_SCopy.
76805 ** A significant change would indicated a missed call to this
76806 ** function for pX. Minor changes, such as adding or removing a
76807 ** dual type, are allowed, as long as the underlying value is the
76808 ** same. */
76809 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
76810 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
76811
76812 /* pMem is the register that is changing. But also mark pX as
76813 ** undefined so that we can quickly detect the shallow-copy error */
76814 pX->flags = MEM_Undefined;
76815 pX->pScopyFrom = 0;
76816 }
76817 }
76818 pMem->pScopyFrom = 0;
76819}
76820#endif /* SQLITE_DEBUG */
76821
76822/*
76823** Make an shallow copy of pFrom into pTo. Prior contents of
76824** pTo are freed. The pFrom->z field is not duplicated. If
76825** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
76826** and flags gets srcType (either MEM_Ephem or MEM_Static).
76827*/
76828static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
76829 vdbeMemClearExternAndSetNull(pTo);
76830 assert( !VdbeMemDynamic(pTo) );
76831 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
76832}
76833SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
76834 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
76835 assert( pTo->db==pFrom->db );
76836 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
76837 memcpy(pTo, pFrom, MEMCELLSIZE);
76838 if( (pFrom->flags&MEM_Static)==0 ){
76839 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
76840 assert( srcType==MEM_Ephem || srcType==MEM_Static );
76841 pTo->flags |= srcType;
76842 }
76843}
76844
76845/*
76846** Make a full copy of pFrom into pTo. Prior contents of pTo are
76847** freed before the copy is made.
76848*/
76849SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
76850 int rc = SQLITE_OK;
76851
76852 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
76853 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
76854 memcpy(pTo, pFrom, MEMCELLSIZE);
76855 pTo->flags &= ~MEM_Dyn;
76856 if( pTo->flags&(MEM_Str|MEM_Blob) ){
76857 if( 0==(pFrom->flags&MEM_Static) ){
76858 pTo->flags |= MEM_Ephem;
76859 rc = sqlite3VdbeMemMakeWriteable(pTo);
76860 }
76861 }
76862
76863 return rc;
76864}
76865
76866/*
76867** Transfer the contents of pFrom to pTo. Any existing value in pTo is
76868** freed. If pFrom contains ephemeral data, a copy is made.
76869**
76870** pFrom contains an SQL NULL when this routine returns.
76871*/
76872SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
76873 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
76874 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
76875 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
76876
76877 sqlite3VdbeMemRelease(pTo);
76878 memcpy(pTo, pFrom, sizeof(Mem));
76879 pFrom->flags = MEM_Null;
76880 pFrom->szMalloc = 0;
76881}
76882
76883/*
76884** Change the value of a Mem to be a string or a BLOB.
76885**
76886** The memory management strategy depends on the value of the xDel
76887** parameter. If the value passed is SQLITE_TRANSIENT, then the
76888** string is copied into a (possibly existing) buffer managed by the
76889** Mem structure. Otherwise, any existing buffer is freed and the
76890** pointer copied.
76891**
76892** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
76893** size limit) then no memory allocation occurs. If the string can be
76894** stored without allocating memory, then it is. If a memory allocation
76895** is required to store the string, then value of pMem is unchanged. In
76896** either case, SQLITE_TOOBIG is returned.
76897*/
76898SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
76899 Mem *pMem, /* Memory cell to set to string value */
76900 const char *z, /* String pointer */
76901 int n, /* Bytes in string, or negative */
76902 u8 enc, /* Encoding of z. 0 for BLOBs */
76903 void (*xDel)(void*) /* Destructor function */
76904){
76905 int nByte = n; /* New value for pMem->n */
76906 int iLimit; /* Maximum allowed string or blob size */
76907 u16 flags = 0; /* New value for pMem->flags */
76908
76909 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76910 assert( !sqlite3VdbeMemIsRowSet(pMem) );
76911
76912 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
76913 if( !z ){
76914 sqlite3VdbeMemSetNull(pMem);
76915 return SQLITE_OK;
76916 }
76917
76918 if( pMem->db ){
76919 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
76920 }else{
76921 iLimit = SQLITE_MAX_LENGTH;
76922 }
76923 flags = (enc==0?MEM_Blob:MEM_Str);
76924 if( nByte<0 ){
76925 assert( enc!=0 );
76926 if( enc==SQLITE_UTF8 ){
76927 nByte = 0x7fffffff & (int)strlen(z);
76928 }else{
76929 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
76930 }
76931 flags |= MEM_Term;
76932 }
76933
76934 /* The following block sets the new values of Mem.z and Mem.xDel. It
76935 ** also sets a flag in local variable "flags" to indicate the memory
76936 ** management (one of MEM_Dyn or MEM_Static).
76937 */
76938 if( xDel==SQLITE_TRANSIENT ){
76939 u32 nAlloc = nByte;
76940 if( flags&MEM_Term ){
76941 nAlloc += (enc==SQLITE_UTF8?1:2);
76942 }
76943 if( nByte>iLimit ){
76944 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
76945 }
76946 testcase( nAlloc==0 );
76947 testcase( nAlloc==31 );
76948 testcase( nAlloc==32 );
76949 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
76950 return SQLITE_NOMEM_BKPT;
76951 }
76952 memcpy(pMem->z, z, nAlloc);
76953 }else{
76954 sqlite3VdbeMemRelease(pMem);
76955 pMem->z = (char *)z;
76956 if( xDel==SQLITE_DYNAMIC ){
76957 pMem->zMalloc = pMem->z;
76958 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
76959 }else{
76960 pMem->xDel = xDel;
76961 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
76962 }
76963 }
76964
76965 pMem->n = nByte;
76966 pMem->flags = flags;
76967 if( enc ){
76968 pMem->enc = enc;
76969#ifdef SQLITE_ENABLE_SESSION
76970 }else if( pMem->db==0 ){
76971 pMem->enc = SQLITE_UTF8;
76972#endif
76973 }else{
76974 assert( pMem->db!=0 );
76975 pMem->enc = ENC(pMem->db);
76976 }
76977
76978#ifndef SQLITE_OMIT_UTF16
76979 if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
76980 return SQLITE_NOMEM_BKPT;
76981 }
76982#endif
76983
76984 if( nByte>iLimit ){
76985 return SQLITE_TOOBIG;
76986 }
76987
76988 return SQLITE_OK;
76989}
76990
76991/*
76992** Move data out of a btree key or data field and into a Mem structure.
76993** The data is payload from the entry that pCur is currently pointing
76994** to. offset and amt determine what portion of the data or key to retrieve.
76995** The result is written into the pMem element.
76996**
76997** The pMem object must have been initialized. This routine will use
76998** pMem->zMalloc to hold the content from the btree, if possible. New
76999** pMem->zMalloc space will be allocated if necessary. The calling routine
77000** is responsible for making sure that the pMem object is eventually
77001** destroyed.
77002**
77003** If this routine fails for any reason (malloc returns NULL or unable
77004** to read from the disk) then the pMem is left in an inconsistent state.
77005*/
77006SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
77007 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
77008 u32 offset, /* Offset from the start of data to return bytes from. */
77009 u32 amt, /* Number of bytes to return. */
77010 Mem *pMem /* OUT: Return data in this Mem structure. */
77011){
77012 int rc;
77013 pMem->flags = MEM_Null;
77014 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
77015 return SQLITE_CORRUPT_BKPT;
77016 }
77017 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
77018 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
77019 if( rc==SQLITE_OK ){
77020 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
77021 pMem->flags = MEM_Blob;
77022 pMem->n = (int)amt;
77023 }else{
77024 sqlite3VdbeMemRelease(pMem);
77025 }
77026 }
77027 return rc;
77028}
77029SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(
77030 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
77031 u32 amt, /* Number of bytes to return. */
77032 Mem *pMem /* OUT: Return data in this Mem structure. */
77033){
77034 u32 available = 0; /* Number of bytes available on the local btree page */
77035 int rc = SQLITE_OK; /* Return code */
77036
77037 assert( sqlite3BtreeCursorIsValid(pCur) );
77038 assert( !VdbeMemDynamic(pMem) );
77039
77040 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
77041 ** that both the BtShared and database handle mutexes are held. */
77042 assert( !sqlite3VdbeMemIsRowSet(pMem) );
77043 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
77044 assert( pMem->z!=0 );
77045
77046 if( amt<=available ){
77047 pMem->flags = MEM_Blob|MEM_Ephem;
77048 pMem->n = (int)amt;
77049 }else{
77050 rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem);
77051 }
77052
77053 return rc;
77054}
77055
77056/*
77057** The pVal argument is known to be a value other than NULL.
77058** Convert it into a string with encoding enc and return a pointer
77059** to a zero-terminated version of that string.
77060*/
77061static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
77062 assert( pVal!=0 );
77063 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
77064 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
77065 assert( !sqlite3VdbeMemIsRowSet(pVal) );
77066 assert( (pVal->flags & (MEM_Null))==0 );
77067 if( pVal->flags & (MEM_Blob|MEM_Str) ){
77068 if( ExpandBlob(pVal) ) return 0;
77069 pVal->flags |= MEM_Str;
77070 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
77071 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
77072 }
77073 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
77074 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
77075 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
77076 return 0;
77077 }
77078 }
77079 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
77080 }else{
77081 sqlite3VdbeMemStringify(pVal, enc, 0);
77082 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
77083 }
77084 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
77085 || pVal->db->mallocFailed );
77086 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
77087 assert( sqlite3VdbeMemValidStrRep(pVal) );
77088 return pVal->z;
77089 }else{
77090 return 0;
77091 }
77092}
77093
77094/* This function is only available internally, it is not part of the
77095** external API. It works in a similar way to sqlite3_value_text(),
77096** except the data returned is in the encoding specified by the second
77097** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
77098** SQLITE_UTF8.
77099**
77100** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
77101** If that is the case, then the result must be aligned on an even byte
77102** boundary.
77103*/
77104SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
77105 if( !pVal ) return 0;
77106 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
77107 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
77108 assert( !sqlite3VdbeMemIsRowSet(pVal) );
77109 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
77110 assert( sqlite3VdbeMemValidStrRep(pVal) );
77111 return pVal->z;
77112 }
77113 if( pVal->flags&MEM_Null ){
77114 return 0;
77115 }
77116 return valueToText(pVal, enc);
77117}
77118
77119/*
77120** Create a new sqlite3_value object.
77121*/
77122SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
77123 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
77124 if( p ){
77125 p->flags = MEM_Null;
77126 p->db = db;
77127 }
77128 return p;
77129}
77130
77131/*
77132** Context object passed by sqlite3Stat4ProbeSetValue() through to
77133** valueNew(). See comments above valueNew() for details.
77134*/
77135struct ValueNewStat4Ctx {
77136 Parse *pParse;
77137 Index *pIdx;
77138 UnpackedRecord **ppRec;
77139 int iVal;
77140};
77141
77142/*
77143** Allocate and return a pointer to a new sqlite3_value object. If
77144** the second argument to this function is NULL, the object is allocated
77145** by calling sqlite3ValueNew().
77146**
77147** Otherwise, if the second argument is non-zero, then this function is
77148** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
77149** already been allocated, allocate the UnpackedRecord structure that
77150** that function will return to its caller here. Then return a pointer to
77151** an sqlite3_value within the UnpackedRecord.a[] array.
77152*/
77153static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
77154#ifdef SQLITE_ENABLE_STAT4
77155 if( p ){
77156 UnpackedRecord *pRec = p->ppRec[0];
77157
77158 if( pRec==0 ){
77159 Index *pIdx = p->pIdx; /* Index being probed */
77160 int nByte; /* Bytes of space to allocate */
77161 int i; /* Counter variable */
77162 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
77163
77164 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
77165 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
77166 if( pRec ){
77167 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
77168 if( pRec->pKeyInfo ){
77169 assert( pRec->pKeyInfo->nAllField==nCol );
77170 assert( pRec->pKeyInfo->enc==ENC(db) );
77171 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
77172 for(i=0; i<nCol; i++){
77173 pRec->aMem[i].flags = MEM_Null;
77174 pRec->aMem[i].db = db;
77175 }
77176 }else{
77177 sqlite3DbFreeNN(db, pRec);
77178 pRec = 0;
77179 }
77180 }
77181 if( pRec==0 ) return 0;
77182 p->ppRec[0] = pRec;
77183 }
77184
77185 pRec->nField = p->iVal+1;
77186 return &pRec->aMem[p->iVal];
77187 }
77188#else
77189 UNUSED_PARAMETER(p);
77190#endif /* defined(SQLITE_ENABLE_STAT4) */
77191 return sqlite3ValueNew(db);
77192}
77193
77194/*
77195** The expression object indicated by the second argument is guaranteed
77196** to be a scalar SQL function. If
77197**
77198** * all function arguments are SQL literals,
77199** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
77200** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
77201**
77202** then this routine attempts to invoke the SQL function. Assuming no
77203** error occurs, output parameter (*ppVal) is set to point to a value
77204** object containing the result before returning SQLITE_OK.
77205**
77206** Affinity aff is applied to the result of the function before returning.
77207** If the result is a text value, the sqlite3_value object uses encoding
77208** enc.
77209**
77210** If the conditions above are not met, this function returns SQLITE_OK
77211** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
77212** NULL and an SQLite error code returned.
77213*/
77214#ifdef SQLITE_ENABLE_STAT4
77215static int valueFromFunction(
77216 sqlite3 *db, /* The database connection */
77217 Expr *p, /* The expression to evaluate */
77218 u8 enc, /* Encoding to use */
77219 u8 aff, /* Affinity to use */
77220 sqlite3_value **ppVal, /* Write the new value here */
77221 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
77222){
77223 sqlite3_context ctx; /* Context object for function invocation */
77224 sqlite3_value **apVal = 0; /* Function arguments */
77225 int nVal = 0; /* Size of apVal[] array */
77226 FuncDef *pFunc = 0; /* Function definition */
77227 sqlite3_value *pVal = 0; /* New value */
77228 int rc = SQLITE_OK; /* Return code */
77229 ExprList *pList = 0; /* Function arguments */
77230 int i; /* Iterator variable */
77231
77232 assert( pCtx!=0 );
77233 assert( (p->flags & EP_TokenOnly)==0 );
77234 pList = p->x.pList;
77235 if( pList ) nVal = pList->nExpr;
77236 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
77237 assert( pFunc );
77238 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
77239 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
77240 ){
77241 return SQLITE_OK;
77242 }
77243
77244 if( pList ){
77245 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
77246 if( apVal==0 ){
77247 rc = SQLITE_NOMEM_BKPT;
77248 goto value_from_function_out;
77249 }
77250 for(i=0; i<nVal; i++){
77251 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
77252 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
77253 }
77254 }
77255
77256 pVal = valueNew(db, pCtx);
77257 if( pVal==0 ){
77258 rc = SQLITE_NOMEM_BKPT;
77259 goto value_from_function_out;
77260 }
77261
77262 assert( pCtx->pParse->rc==SQLITE_OK );
77263 memset(&ctx, 0, sizeof(ctx));
77264 ctx.pOut = pVal;
77265 ctx.pFunc = pFunc;
77266 pFunc->xSFunc(&ctx, nVal, apVal);
77267 if( ctx.isError ){
77268 rc = ctx.isError;
77269 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
77270 }else{
77271 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
77272 assert( rc==SQLITE_OK );
77273 rc = sqlite3VdbeChangeEncoding(pVal, enc);
77274 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
77275 rc = SQLITE_TOOBIG;
77276 pCtx->pParse->nErr++;
77277 }
77278 }
77279 pCtx->pParse->rc = rc;
77280
77281 value_from_function_out:
77282 if( rc!=SQLITE_OK ){
77283 pVal = 0;
77284 }
77285 if( apVal ){
77286 for(i=0; i<nVal; i++){
77287 sqlite3ValueFree(apVal[i]);
77288 }
77289 sqlite3DbFreeNN(db, apVal);
77290 }
77291
77292 *ppVal = pVal;
77293 return rc;
77294}
77295#else
77296# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
77297#endif /* defined(SQLITE_ENABLE_STAT4) */
77298
77299/*
77300** Extract a value from the supplied expression in the manner described
77301** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
77302** using valueNew().
77303**
77304** If pCtx is NULL and an error occurs after the sqlite3_value object
77305** has been allocated, it is freed before returning. Or, if pCtx is not
77306** NULL, it is assumed that the caller will free any allocated object
77307** in all cases.
77308*/
77309static int valueFromExpr(
77310 sqlite3 *db, /* The database connection */
77311 Expr *pExpr, /* The expression to evaluate */
77312 u8 enc, /* Encoding to use */
77313 u8 affinity, /* Affinity to use */
77314 sqlite3_value **ppVal, /* Write the new value here */
77315 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
77316){
77317 int op;
77318 char *zVal = 0;
77319 sqlite3_value *pVal = 0;
77320 int negInt = 1;
77321 const char *zNeg = "";
77322 int rc = SQLITE_OK;
77323
77324 assert( pExpr!=0 );
77325 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
77326#if defined(SQLITE_ENABLE_STAT4)
77327 if( op==TK_REGISTER ) op = pExpr->op2;
77328#else
77329 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
77330#endif
77331
77332 /* Compressed expressions only appear when parsing the DEFAULT clause
77333 ** on a table column definition, and hence only when pCtx==0. This
77334 ** check ensures that an EP_TokenOnly expression is never passed down
77335 ** into valueFromFunction(). */
77336 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
77337
77338 if( op==TK_CAST ){
77339 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
77340 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
77341 testcase( rc!=SQLITE_OK );
77342 if( *ppVal ){
77343 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
77344 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
77345 }
77346 return rc;
77347 }
77348
77349 /* Handle negative integers in a single step. This is needed in the
77350 ** case when the value is -9223372036854775808.
77351 */
77352 if( op==TK_UMINUS
77353 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
77354 pExpr = pExpr->pLeft;
77355 op = pExpr->op;
77356 negInt = -1;
77357 zNeg = "-";
77358 }
77359
77360 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
77361 pVal = valueNew(db, pCtx);
77362 if( pVal==0 ) goto no_mem;
77363 if( ExprHasProperty(pExpr, EP_IntValue) ){
77364 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
77365 }else{
77366 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
77367 if( zVal==0 ) goto no_mem;
77368 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
77369 }
77370 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
77371 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
77372 }else{
77373 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
77374 }
77375 assert( (pVal->flags & MEM_IntReal)==0 );
77376 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
77377 testcase( pVal->flags & MEM_Int );
77378 testcase( pVal->flags & MEM_Real );
77379 pVal->flags &= ~MEM_Str;
77380 }
77381 if( enc!=SQLITE_UTF8 ){
77382 rc = sqlite3VdbeChangeEncoding(pVal, enc);
77383 }
77384 }else if( op==TK_UMINUS ) {
77385 /* This branch happens for multiple negative signs. Ex: -(-5) */
77386 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
77387 && pVal!=0
77388 ){
77389 sqlite3VdbeMemNumerify(pVal);
77390 if( pVal->flags & MEM_Real ){
77391 pVal->u.r = -pVal->u.r;
77392 }else if( pVal->u.i==SMALLEST_INT64 ){
77393#ifndef SQLITE_OMIT_FLOATING_POINT
77394 pVal->u.r = -(double)SMALLEST_INT64;
77395#else
77396 pVal->u.r = LARGEST_INT64;
77397#endif
77398 MemSetTypeFlag(pVal, MEM_Real);
77399 }else{
77400 pVal->u.i = -pVal->u.i;
77401 }
77402 sqlite3ValueApplyAffinity(pVal, affinity, enc);
77403 }
77404 }else if( op==TK_NULL ){
77405 pVal = valueNew(db, pCtx);
77406 if( pVal==0 ) goto no_mem;
77407 sqlite3VdbeMemSetNull(pVal);
77408 }
77409#ifndef SQLITE_OMIT_BLOB_LITERAL
77410 else if( op==TK_BLOB ){
77411 int nVal;
77412 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
77413 assert( pExpr->u.zToken[1]=='\'' );
77414 pVal = valueNew(db, pCtx);
77415 if( !pVal ) goto no_mem;
77416 zVal = &pExpr->u.zToken[2];
77417 nVal = sqlite3Strlen30(zVal)-1;
77418 assert( zVal[nVal]=='\'' );
77419 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
77420 0, SQLITE_DYNAMIC);
77421 }
77422#endif
77423#ifdef SQLITE_ENABLE_STAT4
77424 else if( op==TK_FUNCTION && pCtx!=0 ){
77425 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
77426 }
77427#endif
77428 else if( op==TK_TRUEFALSE ){
77429 pVal = valueNew(db, pCtx);
77430 if( pVal ){
77431 pVal->flags = MEM_Int;
77432 pVal->u.i = pExpr->u.zToken[4]==0;
77433 }
77434 }
77435
77436 *ppVal = pVal;
77437 return rc;
77438
77439no_mem:
77440#ifdef SQLITE_ENABLE_STAT4
77441 if( pCtx==0 || pCtx->pParse->nErr==0 )
77442#endif
77443 sqlite3OomFault(db);
77444 sqlite3DbFree(db, zVal);
77445 assert( *ppVal==0 );
77446#ifdef SQLITE_ENABLE_STAT4
77447 if( pCtx==0 ) sqlite3ValueFree(pVal);
77448#else
77449 assert( pCtx==0 ); sqlite3ValueFree(pVal);
77450#endif
77451 return SQLITE_NOMEM_BKPT;
77452}
77453
77454/*
77455** Create a new sqlite3_value object, containing the value of pExpr.
77456**
77457** This only works for very simple expressions that consist of one constant
77458** token (i.e. "5", "5.1", "'a string'"). If the expression can
77459** be converted directly into a value, then the value is allocated and
77460** a pointer written to *ppVal. The caller is responsible for deallocating
77461** the value by passing it to sqlite3ValueFree() later on. If the expression
77462** cannot be converted to a value, then *ppVal is set to NULL.
77463*/
77464SQLITE_PRIVATE int sqlite3ValueFromExpr(
77465 sqlite3 *db, /* The database connection */
77466 Expr *pExpr, /* The expression to evaluate */
77467 u8 enc, /* Encoding to use */
77468 u8 affinity, /* Affinity to use */
77469 sqlite3_value **ppVal /* Write the new value here */
77470){
77471 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
77472}
77473
77474#ifdef SQLITE_ENABLE_STAT4
77475/*
77476** Attempt to extract a value from pExpr and use it to construct *ppVal.
77477**
77478** If pAlloc is not NULL, then an UnpackedRecord object is created for
77479** pAlloc if one does not exist and the new value is added to the
77480** UnpackedRecord object.
77481**
77482** A value is extracted in the following cases:
77483**
77484** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
77485**
77486** * The expression is a bound variable, and this is a reprepare, or
77487**
77488** * The expression is a literal value.
77489**
77490** On success, *ppVal is made to point to the extracted value. The caller
77491** is responsible for ensuring that the value is eventually freed.
77492*/
77493static int stat4ValueFromExpr(
77494 Parse *pParse, /* Parse context */
77495 Expr *pExpr, /* The expression to extract a value from */
77496 u8 affinity, /* Affinity to use */
77497 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
77498 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
77499){
77500 int rc = SQLITE_OK;
77501 sqlite3_value *pVal = 0;
77502 sqlite3 *db = pParse->db;
77503
77504 /* Skip over any TK_COLLATE nodes */
77505 pExpr = sqlite3ExprSkipCollate(pExpr);
77506
77507 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
77508 if( !pExpr ){
77509 pVal = valueNew(db, pAlloc);
77510 if( pVal ){
77511 sqlite3VdbeMemSetNull((Mem*)pVal);
77512 }
77513 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
77514 Vdbe *v;
77515 int iBindVar = pExpr->iColumn;
77516 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
77517 if( (v = pParse->pReprepare)!=0 ){
77518 pVal = valueNew(db, pAlloc);
77519 if( pVal ){
77520 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
77521 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
77522 pVal->db = pParse->db;
77523 }
77524 }
77525 }else{
77526 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
77527 }
77528
77529 assert( pVal==0 || pVal->db==db );
77530 *ppVal = pVal;
77531 return rc;
77532}
77533
77534/*
77535** This function is used to allocate and populate UnpackedRecord
77536** structures intended to be compared against sample index keys stored
77537** in the sqlite_stat4 table.
77538**
77539** A single call to this function populates zero or more fields of the
77540** record starting with field iVal (fields are numbered from left to
77541** right starting with 0). A single field is populated if:
77542**
77543** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
77544**
77545** * The expression is a bound variable, and this is a reprepare, or
77546**
77547** * The sqlite3ValueFromExpr() function is able to extract a value
77548** from the expression (i.e. the expression is a literal value).
77549**
77550** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
77551** vector components that match either of the two latter criteria listed
77552** above.
77553**
77554** Before any value is appended to the record, the affinity of the
77555** corresponding column within index pIdx is applied to it. Before
77556** this function returns, output parameter *pnExtract is set to the
77557** number of values appended to the record.
77558**
77559** When this function is called, *ppRec must either point to an object
77560** allocated by an earlier call to this function, or must be NULL. If it
77561** is NULL and a value can be successfully extracted, a new UnpackedRecord
77562** is allocated (and *ppRec set to point to it) before returning.
77563**
77564** Unless an error is encountered, SQLITE_OK is returned. It is not an
77565** error if a value cannot be extracted from pExpr. If an error does
77566** occur, an SQLite error code is returned.
77567*/
77568SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
77569 Parse *pParse, /* Parse context */
77570 Index *pIdx, /* Index being probed */
77571 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
77572 Expr *pExpr, /* The expression to extract a value from */
77573 int nElem, /* Maximum number of values to append */
77574 int iVal, /* Array element to populate */
77575 int *pnExtract /* OUT: Values appended to the record */
77576){
77577 int rc = SQLITE_OK;
77578 int nExtract = 0;
77579
77580 if( pExpr==0 || pExpr->op!=TK_SELECT ){
77581 int i;
77582 struct ValueNewStat4Ctx alloc;
77583
77584 alloc.pParse = pParse;
77585 alloc.pIdx = pIdx;
77586 alloc.ppRec = ppRec;
77587
77588 for(i=0; i<nElem; i++){
77589 sqlite3_value *pVal = 0;
77590 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
77591 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
77592 alloc.iVal = iVal+i;
77593 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
77594 if( !pVal ) break;
77595 nExtract++;
77596 }
77597 }
77598
77599 *pnExtract = nExtract;
77600 return rc;
77601}
77602
77603/*
77604** Attempt to extract a value from expression pExpr using the methods
77605** as described for sqlite3Stat4ProbeSetValue() above.
77606**
77607** If successful, set *ppVal to point to a new value object and return
77608** SQLITE_OK. If no value can be extracted, but no other error occurs
77609** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
77610** does occur, return an SQLite error code. The final value of *ppVal
77611** is undefined in this case.
77612*/
77613SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
77614 Parse *pParse, /* Parse context */
77615 Expr *pExpr, /* The expression to extract a value from */
77616 u8 affinity, /* Affinity to use */
77617 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
77618){
77619 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
77620}
77621
77622/*
77623** Extract the iCol-th column from the nRec-byte record in pRec. Write
77624** the column value into *ppVal. If *ppVal is initially NULL then a new
77625** sqlite3_value object is allocated.
77626**
77627** If *ppVal is initially NULL then the caller is responsible for
77628** ensuring that the value written into *ppVal is eventually freed.
77629*/
77630SQLITE_PRIVATE int sqlite3Stat4Column(
77631 sqlite3 *db, /* Database handle */
77632 const void *pRec, /* Pointer to buffer containing record */
77633 int nRec, /* Size of buffer pRec in bytes */
77634 int iCol, /* Column to extract */
77635 sqlite3_value **ppVal /* OUT: Extracted value */
77636){
77637 u32 t = 0; /* a column type code */
77638 int nHdr; /* Size of the header in the record */
77639 int iHdr; /* Next unread header byte */
77640 int iField; /* Next unread data byte */
77641 int szField = 0; /* Size of the current data field */
77642 int i; /* Column index */
77643 u8 *a = (u8*)pRec; /* Typecast byte array */
77644 Mem *pMem = *ppVal; /* Write result into this Mem object */
77645
77646 assert( iCol>0 );
77647 iHdr = getVarint32(a, nHdr);
77648 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
77649 iField = nHdr;
77650 for(i=0; i<=iCol; i++){
77651 iHdr += getVarint32(&a[iHdr], t);
77652 testcase( iHdr==nHdr );
77653 testcase( iHdr==nHdr+1 );
77654 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
77655 szField = sqlite3VdbeSerialTypeLen(t);
77656 iField += szField;
77657 }
77658 testcase( iField==nRec );
77659 testcase( iField==nRec+1 );
77660 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
77661 if( pMem==0 ){
77662 pMem = *ppVal = sqlite3ValueNew(db);
77663 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
77664 }
77665 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
77666 pMem->enc = ENC(db);
77667 return SQLITE_OK;
77668}
77669
77670/*
77671** Unless it is NULL, the argument must be an UnpackedRecord object returned
77672** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
77673** the object.
77674*/
77675SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
77676 if( pRec ){
77677 int i;
77678 int nCol = pRec->pKeyInfo->nAllField;
77679 Mem *aMem = pRec->aMem;
77680 sqlite3 *db = aMem[0].db;
77681 for(i=0; i<nCol; i++){
77682 sqlite3VdbeMemRelease(&aMem[i]);
77683 }
77684 sqlite3KeyInfoUnref(pRec->pKeyInfo);
77685 sqlite3DbFreeNN(db, pRec);
77686 }
77687}
77688#endif /* ifdef SQLITE_ENABLE_STAT4 */
77689
77690/*
77691** Change the string value of an sqlite3_value object
77692*/
77693SQLITE_PRIVATE void sqlite3ValueSetStr(
77694 sqlite3_value *v, /* Value to be set */
77695 int n, /* Length of string z */
77696 const void *z, /* Text of the new string */
77697 u8 enc, /* Encoding to use */
77698 void (*xDel)(void*) /* Destructor for the string */
77699){
77700 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
77701}
77702
77703/*
77704** Free an sqlite3_value object
77705*/
77706SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
77707 if( !v ) return;
77708 sqlite3VdbeMemRelease((Mem *)v);
77709 sqlite3DbFreeNN(((Mem*)v)->db, v);
77710}
77711
77712/*
77713** The sqlite3ValueBytes() routine returns the number of bytes in the
77714** sqlite3_value object assuming that it uses the encoding "enc".
77715** The valueBytes() routine is a helper function.
77716*/
77717static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
77718 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
77719}
77720SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
77721 Mem *p = (Mem*)pVal;
77722 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
77723 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
77724 return p->n;
77725 }
77726 if( (p->flags & MEM_Blob)!=0 ){
77727 if( p->flags & MEM_Zero ){
77728 return p->n + p->u.nZero;
77729 }else{
77730 return p->n;
77731 }
77732 }
77733 if( p->flags & MEM_Null ) return 0;
77734 return valueBytes(pVal, enc);
77735}
77736
77737/************** End of vdbemem.c *********************************************/
77738/************** Begin file vdbeaux.c *****************************************/
77739/*
77740** 2003 September 6
77741**
77742** The author disclaims copyright to this source code. In place of
77743** a legal notice, here is a blessing:
77744**
77745** May you do good and not evil.
77746** May you find forgiveness for yourself and forgive others.
77747** May you share freely, never taking more than you give.
77748**
77749*************************************************************************
77750** This file contains code used for creating, destroying, and populating
77751** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
77752*/
77753/* #include "sqliteInt.h" */
77754/* #include "vdbeInt.h" */
77755
77756/* Forward references */
77757static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
77758static void vdbeFreeOpArray(sqlite3 *, Op *, int);
77759
77760/*
77761** Create a new virtual database engine.
77762*/
77763SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
77764 sqlite3 *db = pParse->db;
77765 Vdbe *p;
77766 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
77767 if( p==0 ) return 0;
77768 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
77769 p->db = db;
77770 if( db->pVdbe ){
77771 db->pVdbe->pPrev = p;
77772 }
77773 p->pNext = db->pVdbe;
77774 p->pPrev = 0;
77775 db->pVdbe = p;
77776 p->magic = VDBE_MAGIC_INIT;
77777 p->pParse = pParse;
77778 pParse->pVdbe = p;
77779 assert( pParse->aLabel==0 );
77780 assert( pParse->nLabel==0 );
77781 assert( p->nOpAlloc==0 );
77782 assert( pParse->szOpAlloc==0 );
77783 sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
77784 return p;
77785}
77786
77787/*
77788** Return the Parse object that owns a Vdbe object.
77789*/
77790SQLITE_PRIVATE Parse *sqlite3VdbeParser(Vdbe *p){
77791 return p->pParse;
77792}
77793
77794/*
77795** Change the error string stored in Vdbe.zErrMsg
77796*/
77797SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
77798 va_list ap;
77799 sqlite3DbFree(p->db, p->zErrMsg);
77800 va_start(ap, zFormat);
77801 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
77802 va_end(ap);
77803}
77804
77805/*
77806** Remember the SQL string for a prepared statement.
77807*/
77808SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
77809 if( p==0 ) return;
77810 p->prepFlags = prepFlags;
77811 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
77812 p->expmask = 0;
77813 }
77814 assert( p->zSql==0 );
77815 p->zSql = sqlite3DbStrNDup(p->db, z, n);
77816}
77817
77818#ifdef SQLITE_ENABLE_NORMALIZE
77819/*
77820** Add a new element to the Vdbe->pDblStr list.
77821*/
77822SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
77823 if( p ){
77824 int n = sqlite3Strlen30(z);
77825 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
77826 sizeof(*pStr)+n+1-sizeof(pStr->z));
77827 if( pStr ){
77828 pStr->pNextStr = p->pDblStr;
77829 p->pDblStr = pStr;
77830 memcpy(pStr->z, z, n+1);
77831 }
77832 }
77833}
77834#endif
77835
77836#ifdef SQLITE_ENABLE_NORMALIZE
77837/*
77838** zId of length nId is a double-quoted identifier. Check to see if
77839** that identifier is really used as a string literal.
77840*/
77841SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
77842 Vdbe *pVdbe, /* The prepared statement */
77843 const char *zId /* The double-quoted identifier, already dequoted */
77844){
77845 DblquoteStr *pStr;
77846 assert( zId!=0 );
77847 if( pVdbe->pDblStr==0 ) return 0;
77848 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
77849 if( strcmp(zId, pStr->z)==0 ) return 1;
77850 }
77851 return 0;
77852}
77853#endif
77854
77855/*
77856** Swap all content between two VDBE structures.
77857*/
77858SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
77859 Vdbe tmp, *pTmp;
77860 char *zTmp;
77861 assert( pA->db==pB->db );
77862 tmp = *pA;
77863 *pA = *pB;
77864 *pB = tmp;
77865 pTmp = pA->pNext;
77866 pA->pNext = pB->pNext;
77867 pB->pNext = pTmp;
77868 pTmp = pA->pPrev;
77869 pA->pPrev = pB->pPrev;
77870 pB->pPrev = pTmp;
77871 zTmp = pA->zSql;
77872 pA->zSql = pB->zSql;
77873 pB->zSql = zTmp;
77874#ifdef SQLITE_ENABLE_NORMALIZE
77875 zTmp = pA->zNormSql;
77876 pA->zNormSql = pB->zNormSql;
77877 pB->zNormSql = zTmp;
77878#endif
77879 pB->expmask = pA->expmask;
77880 pB->prepFlags = pA->prepFlags;
77881 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
77882 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
77883}
77884
77885/*
77886** Resize the Vdbe.aOp array so that it is at least nOp elements larger
77887** than its current size. nOp is guaranteed to be less than or equal
77888** to 1024/sizeof(Op).
77889**
77890** If an out-of-memory error occurs while resizing the array, return
77891** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
77892** unchanged (this is so that any opcodes already allocated can be
77893** correctly deallocated along with the rest of the Vdbe).
77894*/
77895static int growOpArray(Vdbe *v, int nOp){
77896 VdbeOp *pNew;
77897 Parse *p = v->pParse;
77898
77899 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
77900 ** more frequent reallocs and hence provide more opportunities for
77901 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
77902 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
77903 ** by the minimum* amount required until the size reaches 512. Normal
77904 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
77905 ** size of the op array or add 1KB of space, whichever is smaller. */
77906#ifdef SQLITE_TEST_REALLOC_STRESS
77907 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
77908 : (sqlite3_int64)v->nOpAlloc+nOp);
77909#else
77910 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
77911 : (sqlite3_int64)(1024/sizeof(Op)));
77912 UNUSED_PARAMETER(nOp);
77913#endif
77914
77915 /* Ensure that the size of a VDBE does not grow too large */
77916 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
77917 sqlite3OomFault(p->db);
77918 return SQLITE_NOMEM;
77919 }
77920
77921 assert( nOp<=(1024/sizeof(Op)) );
77922 assert( nNew>=(v->nOpAlloc+nOp) );
77923 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
77924 if( pNew ){
77925 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
77926 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
77927 v->aOp = pNew;
77928 }
77929 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
77930}
77931
77932#ifdef SQLITE_DEBUG
77933/* This routine is just a convenient place to set a breakpoint that will
77934** fire after each opcode is inserted and displayed using
77935** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
77936** pOp are available to make the breakpoint conditional.
77937**
77938** Other useful labels for breakpoints include:
77939** test_trace_breakpoint(pc,pOp)
77940** sqlite3CorruptError(lineno)
77941** sqlite3MisuseError(lineno)
77942** sqlite3CantopenError(lineno)
77943*/
77944static void test_addop_breakpoint(int pc, Op *pOp){
77945 static int n = 0;
77946 n++;
77947}
77948#endif
77949
77950/*
77951** Add a new instruction to the list of instructions current in the
77952** VDBE. Return the address of the new instruction.
77953**
77954** Parameters:
77955**
77956** p Pointer to the VDBE
77957**
77958** op The opcode for this instruction
77959**
77960** p1, p2, p3 Operands
77961**
77962** Use the sqlite3VdbeResolveLabel() function to fix an address and
77963** the sqlite3VdbeChangeP4() function to change the value of the P4
77964** operand.
77965*/
77966static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
77967 assert( p->nOpAlloc<=p->nOp );
77968 if( growOpArray(p, 1) ) return 1;
77969 assert( p->nOpAlloc>p->nOp );
77970 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
77971}
77972SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
77973 int i;
77974 VdbeOp *pOp;
77975
77976 i = p->nOp;
77977 assert( p->magic==VDBE_MAGIC_INIT );
77978 assert( op>=0 && op<0xff );
77979 if( p->nOpAlloc<=i ){
77980 return growOp3(p, op, p1, p2, p3);
77981 }
77982 p->nOp++;
77983 pOp = &p->aOp[i];
77984 pOp->opcode = (u8)op;
77985 pOp->p5 = 0;
77986 pOp->p1 = p1;
77987 pOp->p2 = p2;
77988 pOp->p3 = p3;
77989 pOp->p4.p = 0;
77990 pOp->p4type = P4_NOTUSED;
77991#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77992 pOp->zComment = 0;
77993#endif
77994#ifdef SQLITE_DEBUG
77995 if( p->db->flags & SQLITE_VdbeAddopTrace ){
77996 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
77997 test_addop_breakpoint(i, &p->aOp[i]);
77998 }
77999#endif
78000#ifdef VDBE_PROFILE
78001 pOp->cycles = 0;
78002 pOp->cnt = 0;
78003#endif
78004#ifdef SQLITE_VDBE_COVERAGE
78005 pOp->iSrcLine = 0;
78006#endif
78007 return i;
78008}
78009SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
78010 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
78011}
78012SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
78013 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
78014}
78015SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
78016 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
78017}
78018
78019/* Generate code for an unconditional jump to instruction iDest
78020*/
78021SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
78022 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
78023}
78024
78025/* Generate code to cause the string zStr to be loaded into
78026** register iDest
78027*/
78028SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
78029 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
78030}
78031
78032/*
78033** Generate code that initializes multiple registers to string or integer
78034** constants. The registers begin with iDest and increase consecutively.
78035** One register is initialized for each characgter in zTypes[]. For each
78036** "s" character in zTypes[], the register is a string if the argument is
78037** not NULL, or OP_Null if the value is a null pointer. For each "i" character
78038** in zTypes[], the register is initialized to an integer.
78039**
78040** If the input string does not end with "X" then an OP_ResultRow instruction
78041** is generated for the values inserted.
78042*/
78043SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
78044 va_list ap;
78045 int i;
78046 char c;
78047 va_start(ap, zTypes);
78048 for(i=0; (c = zTypes[i])!=0; i++){
78049 if( c=='s' ){
78050 const char *z = va_arg(ap, const char*);
78051 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
78052 }else if( c=='i' ){
78053 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
78054 }else{
78055 goto skip_op_resultrow;
78056 }
78057 }
78058 sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
78059skip_op_resultrow:
78060 va_end(ap);
78061}
78062
78063/*
78064** Add an opcode that includes the p4 value as a pointer.
78065*/
78066SQLITE_PRIVATE int sqlite3VdbeAddOp4(
78067 Vdbe *p, /* Add the opcode to this VM */
78068 int op, /* The new opcode */
78069 int p1, /* The P1 operand */
78070 int p2, /* The P2 operand */
78071 int p3, /* The P3 operand */
78072 const char *zP4, /* The P4 operand */
78073 int p4type /* P4 operand type */
78074){
78075 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
78076 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
78077 return addr;
78078}
78079
78080/*
78081** Add an OP_Function or OP_PureFunc opcode.
78082**
78083** The eCallCtx argument is information (typically taken from Expr.op2)
78084** that describes the calling context of the function. 0 means a general
78085** function call. NC_IsCheck means called by a check constraint,
78086** NC_IdxExpr means called as part of an index expression. NC_PartIdx
78087** means in the WHERE clause of a partial index. NC_GenCol means called
78088** while computing a generated column value. 0 is the usual case.
78089*/
78090SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
78091 Parse *pParse, /* Parsing context */
78092 int p1, /* Constant argument mask */
78093 int p2, /* First argument register */
78094 int p3, /* Register into which results are written */
78095 int nArg, /* Number of argument */
78096 const FuncDef *pFunc, /* The function to be invoked */
78097 int eCallCtx /* Calling context */
78098){
78099 Vdbe *v = pParse->pVdbe;
78100 int nByte;
78101 int addr;
78102 sqlite3_context *pCtx;
78103 assert( v );
78104 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
78105 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
78106 if( pCtx==0 ){
78107 assert( pParse->db->mallocFailed );
78108 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
78109 return 0;
78110 }
78111 pCtx->pOut = 0;
78112 pCtx->pFunc = (FuncDef*)pFunc;
78113 pCtx->pVdbe = 0;
78114 pCtx->isError = 0;
78115 pCtx->argc = nArg;
78116 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
78117 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
78118 p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
78119 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
78120 return addr;
78121}
78122
78123/*
78124** Add an opcode that includes the p4 value with a P4_INT64 or
78125** P4_REAL type.
78126*/
78127SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
78128 Vdbe *p, /* Add the opcode to this VM */
78129 int op, /* The new opcode */
78130 int p1, /* The P1 operand */
78131 int p2, /* The P2 operand */
78132 int p3, /* The P3 operand */
78133 const u8 *zP4, /* The P4 operand */
78134 int p4type /* P4 operand type */
78135){
78136 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
78137 if( p4copy ) memcpy(p4copy, zP4, 8);
78138 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
78139}
78140
78141#ifndef SQLITE_OMIT_EXPLAIN
78142/*
78143** Return the address of the current EXPLAIN QUERY PLAN baseline.
78144** 0 means "none".
78145*/
78146SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
78147 VdbeOp *pOp;
78148 if( pParse->addrExplain==0 ) return 0;
78149 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
78150 return pOp->p2;
78151}
78152
78153/*
78154** Set a debugger breakpoint on the following routine in order to
78155** monitor the EXPLAIN QUERY PLAN code generation.
78156*/
78157#if defined(SQLITE_DEBUG)
78158SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
78159 (void)z1;
78160 (void)z2;
78161}
78162#endif
78163
78164/*
78165** Add a new OP_Explain opcode.
78166**
78167** If the bPush flag is true, then make this opcode the parent for
78168** subsequent Explains until sqlite3VdbeExplainPop() is called.
78169*/
78170SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
78171#ifndef SQLITE_DEBUG
78172 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
78173 ** But omit them (for performance) during production builds */
78174 if( pParse->explain==2 )
78175#endif
78176 {
78177 char *zMsg;
78178 Vdbe *v;
78179 va_list ap;
78180 int iThis;
78181 va_start(ap, zFmt);
78182 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
78183 va_end(ap);
78184 v = pParse->pVdbe;
78185 iThis = v->nOp;
78186 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
78187 zMsg, P4_DYNAMIC);
78188 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
78189 if( bPush){
78190 pParse->addrExplain = iThis;
78191 }
78192 }
78193}
78194
78195/*
78196** Pop the EXPLAIN QUERY PLAN stack one level.
78197*/
78198SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
78199 sqlite3ExplainBreakpoint("POP", 0);
78200 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
78201}
78202#endif /* SQLITE_OMIT_EXPLAIN */
78203
78204/*
78205** Add an OP_ParseSchema opcode. This routine is broken out from
78206** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
78207** as having been used.
78208**
78209** The zWhere string must have been obtained from sqlite3_malloc().
78210** This routine will take ownership of the allocated memory.
78211*/
78212SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
78213 int j;
78214 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
78215 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
78216}
78217
78218/*
78219** Add an opcode that includes the p4 value as an integer.
78220*/
78221SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
78222 Vdbe *p, /* Add the opcode to this VM */
78223 int op, /* The new opcode */
78224 int p1, /* The P1 operand */
78225 int p2, /* The P2 operand */
78226 int p3, /* The P3 operand */
78227 int p4 /* The P4 operand as an integer */
78228){
78229 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
78230 if( p->db->mallocFailed==0 ){
78231 VdbeOp *pOp = &p->aOp[addr];
78232 pOp->p4type = P4_INT32;
78233 pOp->p4.i = p4;
78234 }
78235 return addr;
78236}
78237
78238/* Insert the end of a co-routine
78239*/
78240SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
78241 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
78242
78243 /* Clear the temporary register cache, thereby ensuring that each
78244 ** co-routine has its own independent set of registers, because co-routines
78245 ** might expect their registers to be preserved across an OP_Yield, and
78246 ** that could cause problems if two or more co-routines are using the same
78247 ** temporary register.
78248 */
78249 v->pParse->nTempReg = 0;
78250 v->pParse->nRangeReg = 0;
78251}
78252
78253/*
78254** Create a new symbolic label for an instruction that has yet to be
78255** coded. The symbolic label is really just a negative number. The
78256** label can be used as the P2 value of an operation. Later, when
78257** the label is resolved to a specific address, the VDBE will scan
78258** through its operation list and change all values of P2 which match
78259** the label into the resolved address.
78260**
78261** The VDBE knows that a P2 value is a label because labels are
78262** always negative and P2 values are suppose to be non-negative.
78263** Hence, a negative P2 value is a label that has yet to be resolved.
78264** (Later:) This is only true for opcodes that have the OPFLG_JUMP
78265** property.
78266**
78267** Variable usage notes:
78268**
78269** Parse.aLabel[x] Stores the address that the x-th label resolves
78270** into. For testing (SQLITE_DEBUG), unresolved
78271** labels stores -1, but that is not required.
78272** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
78273** Parse.nLabel The *negative* of the number of labels that have
78274** been issued. The negative is stored because
78275** that gives a performance improvement over storing
78276** the equivalent positive value.
78277*/
78278SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
78279 return --pParse->nLabel;
78280}
78281
78282/*
78283** Resolve label "x" to be the address of the next instruction to
78284** be inserted. The parameter "x" must have been obtained from
78285** a prior call to sqlite3VdbeMakeLabel().
78286*/
78287static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
78288 int nNewSize = 10 - p->nLabel;
78289 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
78290 nNewSize*sizeof(p->aLabel[0]));
78291 if( p->aLabel==0 ){
78292 p->nLabelAlloc = 0;
78293 }else{
78294#ifdef SQLITE_DEBUG
78295 int i;
78296 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
78297#endif
78298 p->nLabelAlloc = nNewSize;
78299 p->aLabel[j] = v->nOp;
78300 }
78301}
78302SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
78303 Parse *p = v->pParse;
78304 int j = ADDR(x);
78305 assert( v->magic==VDBE_MAGIC_INIT );
78306 assert( j<-p->nLabel );
78307 assert( j>=0 );
78308#ifdef SQLITE_DEBUG
78309 if( p->db->flags & SQLITE_VdbeAddopTrace ){
78310 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
78311 }
78312#endif
78313 if( p->nLabelAlloc + p->nLabel < 0 ){
78314 resizeResolveLabel(p,v,j);
78315 }else{
78316 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
78317 p->aLabel[j] = v->nOp;
78318 }
78319}
78320
78321/*
78322** Mark the VDBE as one that can only be run one time.
78323*/
78324SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
78325 p->runOnlyOnce = 1;
78326}
78327
78328/*
78329** Mark the VDBE as one that can only be run multiple times.
78330*/
78331SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
78332 p->runOnlyOnce = 0;
78333}
78334
78335#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
78336
78337/*
78338** The following type and function are used to iterate through all opcodes
78339** in a Vdbe main program and each of the sub-programs (triggers) it may
78340** invoke directly or indirectly. It should be used as follows:
78341**
78342** Op *pOp;
78343** VdbeOpIter sIter;
78344**
78345** memset(&sIter, 0, sizeof(sIter));
78346** sIter.v = v; // v is of type Vdbe*
78347** while( (pOp = opIterNext(&sIter)) ){
78348** // Do something with pOp
78349** }
78350** sqlite3DbFree(v->db, sIter.apSub);
78351**
78352*/
78353typedef struct VdbeOpIter VdbeOpIter;
78354struct VdbeOpIter {
78355 Vdbe *v; /* Vdbe to iterate through the opcodes of */
78356 SubProgram **apSub; /* Array of subprograms */
78357 int nSub; /* Number of entries in apSub */
78358 int iAddr; /* Address of next instruction to return */
78359 int iSub; /* 0 = main program, 1 = first sub-program etc. */
78360};
78361static Op *opIterNext(VdbeOpIter *p){
78362 Vdbe *v = p->v;
78363 Op *pRet = 0;
78364 Op *aOp;
78365 int nOp;
78366
78367 if( p->iSub<=p->nSub ){
78368
78369 if( p->iSub==0 ){
78370 aOp = v->aOp;
78371 nOp = v->nOp;
78372 }else{
78373 aOp = p->apSub[p->iSub-1]->aOp;
78374 nOp = p->apSub[p->iSub-1]->nOp;
78375 }
78376 assert( p->iAddr<nOp );
78377
78378 pRet = &aOp[p->iAddr];
78379 p->iAddr++;
78380 if( p->iAddr==nOp ){
78381 p->iSub++;
78382 p->iAddr = 0;
78383 }
78384
78385 if( pRet->p4type==P4_SUBPROGRAM ){
78386 int nByte = (p->nSub+1)*sizeof(SubProgram*);
78387 int j;
78388 for(j=0; j<p->nSub; j++){
78389 if( p->apSub[j]==pRet->p4.pProgram ) break;
78390 }
78391 if( j==p->nSub ){
78392 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
78393 if( !p->apSub ){
78394 pRet = 0;
78395 }else{
78396 p->apSub[p->nSub++] = pRet->p4.pProgram;
78397 }
78398 }
78399 }
78400 }
78401
78402 return pRet;
78403}
78404
78405/*
78406** Check if the program stored in the VM associated with pParse may
78407** throw an ABORT exception (causing the statement, but not entire transaction
78408** to be rolled back). This condition is true if the main program or any
78409** sub-programs contains any of the following:
78410**
78411** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
78412** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
78413** * OP_Destroy
78414** * OP_VUpdate
78415** * OP_VCreate
78416** * OP_VRename
78417** * OP_FkCounter with P2==0 (immediate foreign key constraint)
78418** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
78419** (for CREATE TABLE AS SELECT ...)
78420**
78421** Then check that the value of Parse.mayAbort is true if an
78422** ABORT may be thrown, or false otherwise. Return true if it does
78423** match, or false otherwise. This function is intended to be used as
78424** part of an assert statement in the compiler. Similar to:
78425**
78426** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
78427*/
78428SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
78429 int hasAbort = 0;
78430 int hasFkCounter = 0;
78431 int hasCreateTable = 0;
78432 int hasCreateIndex = 0;
78433 int hasInitCoroutine = 0;
78434 Op *pOp;
78435 VdbeOpIter sIter;
78436 memset(&sIter, 0, sizeof(sIter));
78437 sIter.v = v;
78438
78439 while( (pOp = opIterNext(&sIter))!=0 ){
78440 int opcode = pOp->opcode;
78441 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
78442 || opcode==OP_VDestroy
78443 || opcode==OP_VCreate
78444 || (opcode==OP_ParseSchema && pOp->p4.z==0)
78445 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
78446 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
78447 ){
78448 hasAbort = 1;
78449 break;
78450 }
78451 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
78452 if( mayAbort ){
78453 /* hasCreateIndex may also be set for some DELETE statements that use
78454 ** OP_Clear. So this routine may end up returning true in the case
78455 ** where a "DELETE FROM tbl" has a statement-journal but does not
78456 ** require one. This is not so bad - it is an inefficiency, not a bug. */
78457 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
78458 if( opcode==OP_Clear ) hasCreateIndex = 1;
78459 }
78460 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
78461#ifndef SQLITE_OMIT_FOREIGN_KEY
78462 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
78463 hasFkCounter = 1;
78464 }
78465#endif
78466 }
78467 sqlite3DbFree(v->db, sIter.apSub);
78468
78469 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
78470 ** If malloc failed, then the while() loop above may not have iterated
78471 ** through all opcodes and hasAbort may be set incorrectly. Return
78472 ** true for this case to prevent the assert() in the callers frame
78473 ** from failing. */
78474 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
78475 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
78476 );
78477}
78478#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
78479
78480#ifdef SQLITE_DEBUG
78481/*
78482** Increment the nWrite counter in the VDBE if the cursor is not an
78483** ephemeral cursor, or if the cursor argument is NULL.
78484*/
78485SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
78486 if( pC==0
78487 || (pC->eCurType!=CURTYPE_SORTER
78488 && pC->eCurType!=CURTYPE_PSEUDO
78489 && !pC->isEphemeral)
78490 ){
78491 p->nWrite++;
78492 }
78493}
78494#endif
78495
78496#ifdef SQLITE_DEBUG
78497/*
78498** Assert if an Abort at this point in time might result in a corrupt
78499** database.
78500*/
78501SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
78502 assert( p->nWrite==0 || p->usesStmtJournal );
78503}
78504#endif
78505
78506/*
78507** This routine is called after all opcodes have been inserted. It loops
78508** through all the opcodes and fixes up some details.
78509**
78510** (1) For each jump instruction with a negative P2 value (a label)
78511** resolve the P2 value to an actual address.
78512**
78513** (2) Compute the maximum number of arguments used by any SQL function
78514** and store that value in *pMaxFuncArgs.
78515**
78516** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
78517** indicate what the prepared statement actually does.
78518**
78519** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
78520**
78521** (5) Reclaim the memory allocated for storing labels.
78522**
78523** This routine will only function correctly if the mkopcodeh.tcl generator
78524** script numbers the opcodes correctly. Changes to this routine must be
78525** coordinated with changes to mkopcodeh.tcl.
78526*/
78527static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
78528 int nMaxArgs = *pMaxFuncArgs;
78529 Op *pOp;
78530 Parse *pParse = p->pParse;
78531 int *aLabel = pParse->aLabel;
78532 p->readOnly = 1;
78533 p->bIsReader = 0;
78534 pOp = &p->aOp[p->nOp-1];
78535 while(1){
78536
78537 /* Only JUMP opcodes and the short list of special opcodes in the switch
78538 ** below need to be considered. The mkopcodeh.tcl generator script groups
78539 ** all these opcodes together near the front of the opcode list. Skip
78540 ** any opcode that does not need processing by virtual of the fact that
78541 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
78542 */
78543 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
78544 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
78545 ** cases from this switch! */
78546 switch( pOp->opcode ){
78547 case OP_Transaction: {
78548 if( pOp->p2!=0 ) p->readOnly = 0;
78549 /* no break */ deliberate_fall_through
78550 }
78551 case OP_AutoCommit:
78552 case OP_Savepoint: {
78553 p->bIsReader = 1;
78554 break;
78555 }
78556#ifndef SQLITE_OMIT_WAL
78557 case OP_Checkpoint:
78558#endif
78559 case OP_Vacuum:
78560 case OP_JournalMode: {
78561 p->readOnly = 0;
78562 p->bIsReader = 1;
78563 break;
78564 }
78565 case OP_Next:
78566 case OP_SorterNext: {
78567 pOp->p4.xAdvance = sqlite3BtreeNext;
78568 pOp->p4type = P4_ADVANCE;
78569 /* The code generator never codes any of these opcodes as a jump
78570 ** to a label. They are always coded as a jump backwards to a
78571 ** known address */
78572 assert( pOp->p2>=0 );
78573 break;
78574 }
78575 case OP_Prev: {
78576 pOp->p4.xAdvance = sqlite3BtreePrevious;
78577 pOp->p4type = P4_ADVANCE;
78578 /* The code generator never codes any of these opcodes as a jump
78579 ** to a label. They are always coded as a jump backwards to a
78580 ** known address */
78581 assert( pOp->p2>=0 );
78582 break;
78583 }
78584#ifndef SQLITE_OMIT_VIRTUALTABLE
78585 case OP_VUpdate: {
78586 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
78587 break;
78588 }
78589 case OP_VFilter: {
78590 int n;
78591 assert( (pOp - p->aOp) >= 3 );
78592 assert( pOp[-1].opcode==OP_Integer );
78593 n = pOp[-1].p1;
78594 if( n>nMaxArgs ) nMaxArgs = n;
78595 /* Fall through into the default case */
78596 /* no break */ deliberate_fall_through
78597 }
78598#endif
78599 default: {
78600 if( pOp->p2<0 ){
78601 /* The mkopcodeh.tcl script has so arranged things that the only
78602 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
78603 ** have non-negative values for P2. */
78604 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
78605 assert( ADDR(pOp->p2)<-pParse->nLabel );
78606 pOp->p2 = aLabel[ADDR(pOp->p2)];
78607 }
78608 break;
78609 }
78610 }
78611 /* The mkopcodeh.tcl script has so arranged things that the only
78612 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
78613 ** have non-negative values for P2. */
78614 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
78615 }
78616 if( pOp==p->aOp ) break;
78617 pOp--;
78618 }
78619 sqlite3DbFree(p->db, pParse->aLabel);
78620 pParse->aLabel = 0;
78621 pParse->nLabel = 0;
78622 *pMaxFuncArgs = nMaxArgs;
78623 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
78624}
78625
78626/*
78627** Return the address of the next instruction to be inserted.
78628*/
78629SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
78630 assert( p->magic==VDBE_MAGIC_INIT );
78631 return p->nOp;
78632}
78633
78634/*
78635** Verify that at least N opcode slots are available in p without
78636** having to malloc for more space (except when compiled using
78637** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
78638** to verify that certain calls to sqlite3VdbeAddOpList() can never
78639** fail due to a OOM fault and hence that the return value from
78640** sqlite3VdbeAddOpList() will always be non-NULL.
78641*/
78642#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
78643SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
78644 assert( p->nOp + N <= p->nOpAlloc );
78645}
78646#endif
78647
78648/*
78649** Verify that the VM passed as the only argument does not contain
78650** an OP_ResultRow opcode. Fail an assert() if it does. This is used
78651** by code in pragma.c to ensure that the implementation of certain
78652** pragmas comports with the flags specified in the mkpragmatab.tcl
78653** script.
78654*/
78655#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
78656SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
78657 int i;
78658 for(i=0; i<p->nOp; i++){
78659 assert( p->aOp[i].opcode!=OP_ResultRow );
78660 }
78661}
78662#endif
78663
78664/*
78665** Generate code (a single OP_Abortable opcode) that will
78666** verify that the VDBE program can safely call Abort in the current
78667** context.
78668*/
78669#if defined(SQLITE_DEBUG)
78670SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
78671 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
78672}
78673#endif
78674
78675/*
78676** This function returns a pointer to the array of opcodes associated with
78677** the Vdbe passed as the first argument. It is the callers responsibility
78678** to arrange for the returned array to be eventually freed using the
78679** vdbeFreeOpArray() function.
78680**
78681** Before returning, *pnOp is set to the number of entries in the returned
78682** array. Also, *pnMaxArg is set to the larger of its current value and
78683** the number of entries in the Vdbe.apArg[] array required to execute the
78684** returned program.
78685*/
78686SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
78687 VdbeOp *aOp = p->aOp;
78688 assert( aOp && !p->db->mallocFailed );
78689
78690 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
78691 assert( DbMaskAllZero(p->btreeMask) );
78692
78693 resolveP2Values(p, pnMaxArg);
78694 *pnOp = p->nOp;
78695 p->aOp = 0;
78696 return aOp;
78697}
78698
78699/*
78700** Add a whole list of operations to the operation stack. Return a
78701** pointer to the first operation inserted.
78702**
78703** Non-zero P2 arguments to jump instructions are automatically adjusted
78704** so that the jump target is relative to the first operation inserted.
78705*/
78706SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
78707 Vdbe *p, /* Add opcodes to the prepared statement */
78708 int nOp, /* Number of opcodes to add */
78709 VdbeOpList const *aOp, /* The opcodes to be added */
78710 int iLineno /* Source-file line number of first opcode */
78711){
78712 int i;
78713 VdbeOp *pOut, *pFirst;
78714 assert( nOp>0 );
78715 assert( p->magic==VDBE_MAGIC_INIT );
78716 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
78717 return 0;
78718 }
78719 pFirst = pOut = &p->aOp[p->nOp];
78720 for(i=0; i<nOp; i++, aOp++, pOut++){
78721 pOut->opcode = aOp->opcode;
78722 pOut->p1 = aOp->p1;
78723 pOut->p2 = aOp->p2;
78724 assert( aOp->p2>=0 );
78725 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
78726 pOut->p2 += p->nOp;
78727 }
78728 pOut->p3 = aOp->p3;
78729 pOut->p4type = P4_NOTUSED;
78730 pOut->p4.p = 0;
78731 pOut->p5 = 0;
78732#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78733 pOut->zComment = 0;
78734#endif
78735#ifdef SQLITE_VDBE_COVERAGE
78736 pOut->iSrcLine = iLineno+i;
78737#else
78738 (void)iLineno;
78739#endif
78740#ifdef SQLITE_DEBUG
78741 if( p->db->flags & SQLITE_VdbeAddopTrace ){
78742 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
78743 }
78744#endif
78745 }
78746 p->nOp += nOp;
78747 return pFirst;
78748}
78749
78750#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
78751/*
78752** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
78753*/
78754SQLITE_PRIVATE void sqlite3VdbeScanStatus(
78755 Vdbe *p, /* VM to add scanstatus() to */
78756 int addrExplain, /* Address of OP_Explain (or 0) */
78757 int addrLoop, /* Address of loop counter */
78758 int addrVisit, /* Address of rows visited counter */
78759 LogEst nEst, /* Estimated number of output rows */
78760 const char *zName /* Name of table or index being scanned */
78761){
78762 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
78763 ScanStatus *aNew;
78764 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
78765 if( aNew ){
78766 ScanStatus *pNew = &aNew[p->nScan++];
78767 pNew->addrExplain = addrExplain;
78768 pNew->addrLoop = addrLoop;
78769 pNew->addrVisit = addrVisit;
78770 pNew->nEst = nEst;
78771 pNew->zName = sqlite3DbStrDup(p->db, zName);
78772 p->aScan = aNew;
78773 }
78774}
78775#endif
78776
78777
78778/*
78779** Change the value of the opcode, or P1, P2, P3, or P5 operands
78780** for a specific instruction.
78781*/
78782SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
78783 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
78784}
78785SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
78786 sqlite3VdbeGetOp(p,addr)->p1 = val;
78787}
78788SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
78789 sqlite3VdbeGetOp(p,addr)->p2 = val;
78790}
78791SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
78792 sqlite3VdbeGetOp(p,addr)->p3 = val;
78793}
78794SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
78795 assert( p->nOp>0 || p->db->mallocFailed );
78796 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
78797}
78798
78799/*
78800** Change the P2 operand of instruction addr so that it points to
78801** the address of the next instruction to be coded.
78802*/
78803SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
78804 sqlite3VdbeChangeP2(p, addr, p->nOp);
78805}
78806
78807/*
78808** Change the P2 operand of the jump instruction at addr so that
78809** the jump lands on the next opcode. Or if the jump instruction was
78810** the previous opcode (and is thus a no-op) then simply back up
78811** the next instruction counter by one slot so that the jump is
78812** overwritten by the next inserted opcode.
78813**
78814** This routine is an optimization of sqlite3VdbeJumpHere() that
78815** strives to omit useless byte-code like this:
78816**
78817** 7 Once 0 8 0
78818** 8 ...
78819*/
78820SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
78821 if( addr==p->nOp-1 ){
78822 assert( p->aOp[addr].opcode==OP_Once
78823 || p->aOp[addr].opcode==OP_If
78824 || p->aOp[addr].opcode==OP_FkIfZero );
78825 assert( p->aOp[addr].p4type==0 );
78826#ifdef SQLITE_VDBE_COVERAGE
78827 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
78828#endif
78829 p->nOp--;
78830 }else{
78831 sqlite3VdbeChangeP2(p, addr, p->nOp);
78832 }
78833}
78834
78835
78836/*
78837** If the input FuncDef structure is ephemeral, then free it. If
78838** the FuncDef is not ephermal, then do nothing.
78839*/
78840static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
78841 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
78842 sqlite3DbFreeNN(db, pDef);
78843 }
78844}
78845
78846/*
78847** Delete a P4 value if necessary.
78848*/
78849static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
78850 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
78851 sqlite3DbFreeNN(db, p);
78852}
78853static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
78854 freeEphemeralFunction(db, p->pFunc);
78855 sqlite3DbFreeNN(db, p);
78856}
78857static void freeP4(sqlite3 *db, int p4type, void *p4){
78858 assert( db );
78859 switch( p4type ){
78860 case P4_FUNCCTX: {
78861 freeP4FuncCtx(db, (sqlite3_context*)p4);
78862 break;
78863 }
78864 case P4_REAL:
78865 case P4_INT64:
78866 case P4_DYNAMIC:
78867 case P4_DYNBLOB:
78868 case P4_INTARRAY: {
78869 sqlite3DbFree(db, p4);
78870 break;
78871 }
78872 case P4_KEYINFO: {
78873 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
78874 break;
78875 }
78876#ifdef SQLITE_ENABLE_CURSOR_HINTS
78877 case P4_EXPR: {
78878 sqlite3ExprDelete(db, (Expr*)p4);
78879 break;
78880 }
78881#endif
78882 case P4_FUNCDEF: {
78883 freeEphemeralFunction(db, (FuncDef*)p4);
78884 break;
78885 }
78886 case P4_MEM: {
78887 if( db->pnBytesFreed==0 ){
78888 sqlite3ValueFree((sqlite3_value*)p4);
78889 }else{
78890 freeP4Mem(db, (Mem*)p4);
78891 }
78892 break;
78893 }
78894 case P4_VTAB : {
78895 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
78896 break;
78897 }
78898 }
78899}
78900
78901/*
78902** Free the space allocated for aOp and any p4 values allocated for the
78903** opcodes contained within. If aOp is not NULL it is assumed to contain
78904** nOp entries.
78905*/
78906static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
78907 if( aOp ){
78908 Op *pOp;
78909 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
78910 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
78911#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78912 sqlite3DbFree(db, pOp->zComment);
78913#endif
78914 }
78915 sqlite3DbFreeNN(db, aOp);
78916 }
78917}
78918
78919/*
78920** Link the SubProgram object passed as the second argument into the linked
78921** list at Vdbe.pSubProgram. This list is used to delete all sub-program
78922** objects when the VM is no longer required.
78923*/
78924SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
78925 p->pNext = pVdbe->pProgram;
78926 pVdbe->pProgram = p;
78927}
78928
78929/*
78930** Return true if the given Vdbe has any SubPrograms.
78931*/
78932SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
78933 return pVdbe->pProgram!=0;
78934}
78935
78936/*
78937** Change the opcode at addr into OP_Noop
78938*/
78939SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
78940 VdbeOp *pOp;
78941 if( p->db->mallocFailed ) return 0;
78942 assert( addr>=0 && addr<p->nOp );
78943 pOp = &p->aOp[addr];
78944 freeP4(p->db, pOp->p4type, pOp->p4.p);
78945 pOp->p4type = P4_NOTUSED;
78946 pOp->p4.z = 0;
78947 pOp->opcode = OP_Noop;
78948 return 1;
78949}
78950
78951/*
78952** If the last opcode is "op" and it is not a jump destination,
78953** then remove it. Return true if and only if an opcode was removed.
78954*/
78955SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
78956 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
78957 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
78958 }else{
78959 return 0;
78960 }
78961}
78962
78963#ifdef SQLITE_DEBUG
78964/*
78965** Generate an OP_ReleaseReg opcode to indicate that a range of
78966** registers, except any identified by mask, are no longer in use.
78967*/
78968SQLITE_PRIVATE void sqlite3VdbeReleaseRegisters(
78969 Parse *pParse, /* Parsing context */
78970 int iFirst, /* Index of first register to be released */
78971 int N, /* Number of registers to release */
78972 u32 mask, /* Mask of registers to NOT release */
78973 int bUndefine /* If true, mark registers as undefined */
78974){
78975 if( N==0 ) return;
78976 assert( pParse->pVdbe );
78977 assert( iFirst>=1 );
78978 assert( iFirst+N-1<=pParse->nMem );
78979 if( N<=31 && mask!=0 ){
78980 while( N>0 && (mask&1)!=0 ){
78981 mask >>= 1;
78982 iFirst++;
78983 N--;
78984 }
78985 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
78986 mask &= ~MASKBIT32(N-1);
78987 N--;
78988 }
78989 }
78990 if( N>0 ){
78991 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
78992 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
78993 }
78994}
78995#endif /* SQLITE_DEBUG */
78996
78997
78998/*
78999** Change the value of the P4 operand for a specific instruction.
79000** This routine is useful when a large program is loaded from a
79001** static array using sqlite3VdbeAddOpList but we want to make a
79002** few minor changes to the program.
79003**
79004** If n>=0 then the P4 operand is dynamic, meaning that a copy of
79005** the string is made into memory obtained from sqlite3_malloc().
79006** A value of n==0 means copy bytes of zP4 up to and including the
79007** first null byte. If n>0 then copy n+1 bytes of zP4.
79008**
79009** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
79010** to a string or structure that is guaranteed to exist for the lifetime of
79011** the Vdbe. In these cases we can just copy the pointer.
79012**
79013** If addr<0 then change P4 on the most recently inserted instruction.
79014*/
79015static void SQLITE_NOINLINE vdbeChangeP4Full(
79016 Vdbe *p,
79017 Op *pOp,
79018 const char *zP4,
79019 int n
79020){
79021 if( pOp->p4type ){
79022 freeP4(p->db, pOp->p4type, pOp->p4.p);
79023 pOp->p4type = 0;
79024 pOp->p4.p = 0;
79025 }
79026 if( n<0 ){
79027 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
79028 }else{
79029 if( n==0 ) n = sqlite3Strlen30(zP4);
79030 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
79031 pOp->p4type = P4_DYNAMIC;
79032 }
79033}
79034SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
79035 Op *pOp;
79036 sqlite3 *db;
79037 assert( p!=0 );
79038 db = p->db;
79039 assert( p->magic==VDBE_MAGIC_INIT );
79040 assert( p->aOp!=0 || db->mallocFailed );
79041 if( db->mallocFailed ){
79042 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
79043 return;
79044 }
79045 assert( p->nOp>0 );
79046 assert( addr<p->nOp );
79047 if( addr<0 ){
79048 addr = p->nOp - 1;
79049 }
79050 pOp = &p->aOp[addr];
79051 if( n>=0 || pOp->p4type ){
79052 vdbeChangeP4Full(p, pOp, zP4, n);
79053 return;
79054 }
79055 if( n==P4_INT32 ){
79056 /* Note: this cast is safe, because the origin data point was an int
79057 ** that was cast to a (const char *). */
79058 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
79059 pOp->p4type = P4_INT32;
79060 }else if( zP4!=0 ){
79061 assert( n<0 );
79062 pOp->p4.p = (void*)zP4;
79063 pOp->p4type = (signed char)n;
79064 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
79065 }
79066}
79067
79068/*
79069** Change the P4 operand of the most recently coded instruction
79070** to the value defined by the arguments. This is a high-speed
79071** version of sqlite3VdbeChangeP4().
79072**
79073** The P4 operand must not have been previously defined. And the new
79074** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
79075** those cases.
79076*/
79077SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
79078 VdbeOp *pOp;
79079 assert( n!=P4_INT32 && n!=P4_VTAB );
79080 assert( n<=0 );
79081 if( p->db->mallocFailed ){
79082 freeP4(p->db, n, pP4);
79083 }else{
79084 assert( pP4!=0 );
79085 assert( p->nOp>0 );
79086 pOp = &p->aOp[p->nOp-1];
79087 assert( pOp->p4type==P4_NOTUSED );
79088 pOp->p4type = n;
79089 pOp->p4.p = pP4;
79090 }
79091}
79092
79093/*
79094** Set the P4 on the most recently added opcode to the KeyInfo for the
79095** index given.
79096*/
79097SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
79098 Vdbe *v = pParse->pVdbe;
79099 KeyInfo *pKeyInfo;
79100 assert( v!=0 );
79101 assert( pIdx!=0 );
79102 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
79103 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
79104}
79105
79106#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79107/*
79108** Change the comment on the most recently coded instruction. Or
79109** insert a No-op and add the comment to that new instruction. This
79110** makes the code easier to read during debugging. None of this happens
79111** in a production build.
79112*/
79113static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
79114 assert( p->nOp>0 || p->aOp==0 );
79115 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed
79116 || p->pParse->nErr>0 );
79117 if( p->nOp ){
79118 assert( p->aOp );
79119 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
79120 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
79121 }
79122}
79123SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
79124 va_list ap;
79125 if( p ){
79126 va_start(ap, zFormat);
79127 vdbeVComment(p, zFormat, ap);
79128 va_end(ap);
79129 }
79130}
79131SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
79132 va_list ap;
79133 if( p ){
79134 sqlite3VdbeAddOp0(p, OP_Noop);
79135 va_start(ap, zFormat);
79136 vdbeVComment(p, zFormat, ap);
79137 va_end(ap);
79138 }
79139}
79140#endif /* NDEBUG */
79141
79142#ifdef SQLITE_VDBE_COVERAGE
79143/*
79144** Set the value if the iSrcLine field for the previously coded instruction.
79145*/
79146SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
79147 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
79148}
79149#endif /* SQLITE_VDBE_COVERAGE */
79150
79151/*
79152** Return the opcode for a given address. If the address is -1, then
79153** return the most recently inserted opcode.
79154**
79155** If a memory allocation error has occurred prior to the calling of this
79156** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
79157** is readable but not writable, though it is cast to a writable value.
79158** The return of a dummy opcode allows the call to continue functioning
79159** after an OOM fault without having to check to see if the return from
79160** this routine is a valid pointer. But because the dummy.opcode is 0,
79161** dummy will never be written to. This is verified by code inspection and
79162** by running with Valgrind.
79163*/
79164SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
79165 /* C89 specifies that the constant "dummy" will be initialized to all
79166 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
79167 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
79168 assert( p->magic==VDBE_MAGIC_INIT );
79169 if( addr<0 ){
79170 addr = p->nOp - 1;
79171 }
79172 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
79173 if( p->db->mallocFailed ){
79174 return (VdbeOp*)&dummy;
79175 }else{
79176 return &p->aOp[addr];
79177 }
79178}
79179
79180#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
79181/*
79182** Return an integer value for one of the parameters to the opcode pOp
79183** determined by character c.
79184*/
79185static int translateP(char c, const Op *pOp){
79186 if( c=='1' ) return pOp->p1;
79187 if( c=='2' ) return pOp->p2;
79188 if( c=='3' ) return pOp->p3;
79189 if( c=='4' ) return pOp->p4.i;
79190 return pOp->p5;
79191}
79192
79193/*
79194** Compute a string for the "comment" field of a VDBE opcode listing.
79195**
79196** The Synopsis: field in comments in the vdbe.c source file gets converted
79197** to an extra string that is appended to the sqlite3OpcodeName(). In the
79198** absence of other comments, this synopsis becomes the comment on the opcode.
79199** Some translation occurs:
79200**
79201** "PX" -> "r[X]"
79202** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
79203** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
79204** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
79205*/
79206SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(
79207 sqlite3 *db, /* Optional - Oom error reporting only */
79208 const Op *pOp, /* The opcode to be commented */
79209 const char *zP4 /* Previously obtained value for P4 */
79210){
79211 const char *zOpName;
79212 const char *zSynopsis;
79213 int nOpName;
79214 int ii;
79215 char zAlt[50];
79216 StrAccum x;
79217
79218 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
79219 zOpName = sqlite3OpcodeName(pOp->opcode);
79220 nOpName = sqlite3Strlen30(zOpName);
79221 if( zOpName[nOpName+1] ){
79222 int seenCom = 0;
79223 char c;
79224 zSynopsis = zOpName += nOpName + 1;
79225 if( strncmp(zSynopsis,"IF ",3)==0 ){
79226 if( pOp->p5 & SQLITE_STOREP2 ){
79227 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
79228 }else{
79229 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
79230 }
79231 zSynopsis = zAlt;
79232 }
79233 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
79234 if( c=='P' ){
79235 c = zSynopsis[++ii];
79236 if( c=='4' ){
79237 sqlite3_str_appendall(&x, zP4);
79238 }else if( c=='X' ){
79239 sqlite3_str_appendall(&x, pOp->zComment);
79240 seenCom = 1;
79241 }else{
79242 int v1 = translateP(c, pOp);
79243 int v2;
79244 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
79245 ii += 3;
79246 v2 = translateP(zSynopsis[ii], pOp);
79247 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
79248 ii += 2;
79249 v2++;
79250 }
79251 if( v2<2 ){
79252 sqlite3_str_appendf(&x, "%d", v1);
79253 }else{
79254 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
79255 }
79256 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
79257 sqlite3_context *pCtx = pOp->p4.pCtx;
79258 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
79259 sqlite3_str_appendf(&x, "%d", v1);
79260 }else if( pCtx->argc>1 ){
79261 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
79262 }else{
79263 assert( x.nChar>2 );
79264 x.nChar -= 2;
79265 ii++;
79266 }
79267 ii += 3;
79268 }else{
79269 sqlite3_str_appendf(&x, "%d", v1);
79270 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
79271 ii += 4;
79272 }
79273 }
79274 }
79275 }else{
79276 sqlite3_str_appendchar(&x, 1, c);
79277 }
79278 }
79279 if( !seenCom && pOp->zComment ){
79280 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
79281 }
79282 }else if( pOp->zComment ){
79283 sqlite3_str_appendall(&x, pOp->zComment);
79284 }
79285 if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){
79286 sqlite3OomFault(db);
79287 }
79288 return sqlite3StrAccumFinish(&x);
79289}
79290#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
79291
79292#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
79293/*
79294** Translate the P4.pExpr value for an OP_CursorHint opcode into text
79295** that can be displayed in the P4 column of EXPLAIN output.
79296*/
79297static void displayP4Expr(StrAccum *p, Expr *pExpr){
79298 const char *zOp = 0;
79299 switch( pExpr->op ){
79300 case TK_STRING:
79301 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
79302 break;
79303 case TK_INTEGER:
79304 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
79305 break;
79306 case TK_NULL:
79307 sqlite3_str_appendf(p, "NULL");
79308 break;
79309 case TK_REGISTER: {
79310 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
79311 break;
79312 }
79313 case TK_COLUMN: {
79314 if( pExpr->iColumn<0 ){
79315 sqlite3_str_appendf(p, "rowid");
79316 }else{
79317 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
79318 }
79319 break;
79320 }
79321 case TK_LT: zOp = "LT"; break;
79322 case TK_LE: zOp = "LE"; break;
79323 case TK_GT: zOp = "GT"; break;
79324 case TK_GE: zOp = "GE"; break;
79325 case TK_NE: zOp = "NE"; break;
79326 case TK_EQ: zOp = "EQ"; break;
79327 case TK_IS: zOp = "IS"; break;
79328 case TK_ISNOT: zOp = "ISNOT"; break;
79329 case TK_AND: zOp = "AND"; break;
79330 case TK_OR: zOp = "OR"; break;
79331 case TK_PLUS: zOp = "ADD"; break;
79332 case TK_STAR: zOp = "MUL"; break;
79333 case TK_MINUS: zOp = "SUB"; break;
79334 case TK_REM: zOp = "REM"; break;
79335 case TK_BITAND: zOp = "BITAND"; break;
79336 case TK_BITOR: zOp = "BITOR"; break;
79337 case TK_SLASH: zOp = "DIV"; break;
79338 case TK_LSHIFT: zOp = "LSHIFT"; break;
79339 case TK_RSHIFT: zOp = "RSHIFT"; break;
79340 case TK_CONCAT: zOp = "CONCAT"; break;
79341 case TK_UMINUS: zOp = "MINUS"; break;
79342 case TK_UPLUS: zOp = "PLUS"; break;
79343 case TK_BITNOT: zOp = "BITNOT"; break;
79344 case TK_NOT: zOp = "NOT"; break;
79345 case TK_ISNULL: zOp = "ISNULL"; break;
79346 case TK_NOTNULL: zOp = "NOTNULL"; break;
79347
79348 default:
79349 sqlite3_str_appendf(p, "%s", "expr");
79350 break;
79351 }
79352
79353 if( zOp ){
79354 sqlite3_str_appendf(p, "%s(", zOp);
79355 displayP4Expr(p, pExpr->pLeft);
79356 if( pExpr->pRight ){
79357 sqlite3_str_append(p, ",", 1);
79358 displayP4Expr(p, pExpr->pRight);
79359 }
79360 sqlite3_str_append(p, ")", 1);
79361 }
79362}
79363#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
79364
79365
79366#if VDBE_DISPLAY_P4
79367/*
79368** Compute a string that describes the P4 parameter for an opcode.
79369** Use zTemp for any required temporary buffer space.
79370*/
79371SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
79372 char *zP4 = 0;
79373 StrAccum x;
79374
79375 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
79376 switch( pOp->p4type ){
79377 case P4_KEYINFO: {
79378 int j;
79379 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
79380 assert( pKeyInfo->aSortFlags!=0 );
79381 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
79382 for(j=0; j<pKeyInfo->nKeyField; j++){
79383 CollSeq *pColl = pKeyInfo->aColl[j];
79384 const char *zColl = pColl ? pColl->zName : "";
79385 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
79386 sqlite3_str_appendf(&x, ",%s%s%s",
79387 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
79388 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
79389 zColl);
79390 }
79391 sqlite3_str_append(&x, ")", 1);
79392 break;
79393 }
79394#ifdef SQLITE_ENABLE_CURSOR_HINTS
79395 case P4_EXPR: {
79396 displayP4Expr(&x, pOp->p4.pExpr);
79397 break;
79398 }
79399#endif
79400 case P4_COLLSEQ: {
79401 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
79402 CollSeq *pColl = pOp->p4.pColl;
79403 assert( pColl->enc>=0 && pColl->enc<4 );
79404 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
79405 encnames[pColl->enc]);
79406 break;
79407 }
79408 case P4_FUNCDEF: {
79409 FuncDef *pDef = pOp->p4.pFunc;
79410 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
79411 break;
79412 }
79413 case P4_FUNCCTX: {
79414 FuncDef *pDef = pOp->p4.pCtx->pFunc;
79415 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
79416 break;
79417 }
79418 case P4_INT64: {
79419 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
79420 break;
79421 }
79422 case P4_INT32: {
79423 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
79424 break;
79425 }
79426 case P4_REAL: {
79427 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
79428 break;
79429 }
79430 case P4_MEM: {
79431 Mem *pMem = pOp->p4.pMem;
79432 if( pMem->flags & MEM_Str ){
79433 zP4 = pMem->z;
79434 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
79435 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
79436 }else if( pMem->flags & MEM_Real ){
79437 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
79438 }else if( pMem->flags & MEM_Null ){
79439 zP4 = "NULL";
79440 }else{
79441 assert( pMem->flags & MEM_Blob );
79442 zP4 = "(blob)";
79443 }
79444 break;
79445 }
79446#ifndef SQLITE_OMIT_VIRTUALTABLE
79447 case P4_VTAB: {
79448 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
79449 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
79450 break;
79451 }
79452#endif
79453 case P4_INTARRAY: {
79454 u32 i;
79455 u32 *ai = pOp->p4.ai;
79456 u32 n = ai[0]; /* The first element of an INTARRAY is always the
79457 ** count of the number of elements to follow */
79458 for(i=1; i<=n; i++){
79459 sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]);
79460 }
79461 sqlite3_str_append(&x, "]", 1);
79462 break;
79463 }
79464 case P4_SUBPROGRAM: {
79465 zP4 = "program";
79466 break;
79467 }
79468 case P4_DYNBLOB:
79469 case P4_ADVANCE: {
79470 break;
79471 }
79472 case P4_TABLE: {
79473 zP4 = pOp->p4.pTab->zName;
79474 break;
79475 }
79476 default: {
79477 zP4 = pOp->p4.z;
79478 }
79479 }
79480 if( zP4 ) sqlite3_str_appendall(&x, zP4);
79481 if( (x.accError & SQLITE_NOMEM)!=0 ){
79482 sqlite3OomFault(db);
79483 }
79484 return sqlite3StrAccumFinish(&x);
79485}
79486#endif /* VDBE_DISPLAY_P4 */
79487
79488/*
79489** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
79490**
79491** The prepared statements need to know in advance the complete set of
79492** attached databases that will be use. A mask of these databases
79493** is maintained in p->btreeMask. The p->lockMask value is the subset of
79494** p->btreeMask of databases that will require a lock.
79495*/
79496SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
79497 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
79498 assert( i<(int)sizeof(p->btreeMask)*8 );
79499 DbMaskSet(p->btreeMask, i);
79500 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
79501 DbMaskSet(p->lockMask, i);
79502 }
79503}
79504
79505#if !defined(SQLITE_OMIT_SHARED_CACHE)
79506/*
79507** If SQLite is compiled to support shared-cache mode and to be threadsafe,
79508** this routine obtains the mutex associated with each BtShared structure
79509** that may be accessed by the VM passed as an argument. In doing so it also
79510** sets the BtShared.db member of each of the BtShared structures, ensuring
79511** that the correct busy-handler callback is invoked if required.
79512**
79513** If SQLite is not threadsafe but does support shared-cache mode, then
79514** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
79515** of all of BtShared structures accessible via the database handle
79516** associated with the VM.
79517**
79518** If SQLite is not threadsafe and does not support shared-cache mode, this
79519** function is a no-op.
79520**
79521** The p->btreeMask field is a bitmask of all btrees that the prepared
79522** statement p will ever use. Let N be the number of bits in p->btreeMask
79523** corresponding to btrees that use shared cache. Then the runtime of
79524** this routine is N*N. But as N is rarely more than 1, this should not
79525** be a problem.
79526*/
79527SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
79528 int i;
79529 sqlite3 *db;
79530 Db *aDb;
79531 int nDb;
79532 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
79533 db = p->db;
79534 aDb = db->aDb;
79535 nDb = db->nDb;
79536 for(i=0; i<nDb; i++){
79537 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
79538 sqlite3BtreeEnter(aDb[i].pBt);
79539 }
79540 }
79541}
79542#endif
79543
79544#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
79545/*
79546** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
79547*/
79548static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
79549 int i;
79550 sqlite3 *db;
79551 Db *aDb;
79552 int nDb;
79553 db = p->db;
79554 aDb = db->aDb;
79555 nDb = db->nDb;
79556 for(i=0; i<nDb; i++){
79557 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
79558 sqlite3BtreeLeave(aDb[i].pBt);
79559 }
79560 }
79561}
79562SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
79563 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
79564 vdbeLeave(p);
79565}
79566#endif
79567
79568#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
79569/*
79570** Print a single opcode. This routine is used for debugging only.
79571*/
79572SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
79573 char *zP4;
79574 char *zCom;
79575 sqlite3 dummyDb;
79576 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
79577 if( pOut==0 ) pOut = stdout;
79578 sqlite3BeginBenignMalloc();
79579 dummyDb.mallocFailed = 1;
79580 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
79581#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79582 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
79583#else
79584 zCom = 0;
79585#endif
79586 /* NB: The sqlite3OpcodeName() function is implemented by code created
79587 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
79588 ** information from the vdbe.c source text */
79589 fprintf(pOut, zFormat1, pc,
79590 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
79591 zP4 ? zP4 : "", pOp->p5,
79592 zCom ? zCom : ""
79593 );
79594 fflush(pOut);
79595 sqlite3_free(zP4);
79596 sqlite3_free(zCom);
79597 sqlite3EndBenignMalloc();
79598}
79599#endif
79600
79601/*
79602** Initialize an array of N Mem element.
79603*/
79604static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
79605 while( (N--)>0 ){
79606 p->db = db;
79607 p->flags = flags;
79608 p->szMalloc = 0;
79609#ifdef SQLITE_DEBUG
79610 p->pScopyFrom = 0;
79611#endif
79612 p++;
79613 }
79614}
79615
79616/*
79617** Release an array of N Mem elements
79618*/
79619static void releaseMemArray(Mem *p, int N){
79620 if( p && N ){
79621 Mem *pEnd = &p[N];
79622 sqlite3 *db = p->db;
79623 if( db->pnBytesFreed ){
79624 do{
79625 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
79626 }while( (++p)<pEnd );
79627 return;
79628 }
79629 do{
79630 assert( (&p[1])==pEnd || p[0].db==p[1].db );
79631 assert( sqlite3VdbeCheckMemInvariants(p) );
79632
79633 /* This block is really an inlined version of sqlite3VdbeMemRelease()
79634 ** that takes advantage of the fact that the memory cell value is
79635 ** being set to NULL after releasing any dynamic resources.
79636 **
79637 ** The justification for duplicating code is that according to
79638 ** callgrind, this causes a certain test case to hit the CPU 4.7
79639 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
79640 ** sqlite3MemRelease() were called from here. With -O2, this jumps
79641 ** to 6.6 percent. The test case is inserting 1000 rows into a table
79642 ** with no indexes using a single prepared INSERT statement, bind()
79643 ** and reset(). Inserts are grouped into a transaction.
79644 */
79645 testcase( p->flags & MEM_Agg );
79646 testcase( p->flags & MEM_Dyn );
79647 testcase( p->xDel==sqlite3VdbeFrameMemDel );
79648 if( p->flags&(MEM_Agg|MEM_Dyn) ){
79649 sqlite3VdbeMemRelease(p);
79650 }else if( p->szMalloc ){
79651 sqlite3DbFreeNN(db, p->zMalloc);
79652 p->szMalloc = 0;
79653 }
79654
79655 p->flags = MEM_Undefined;
79656 }while( (++p)<pEnd );
79657 }
79658}
79659
79660#ifdef SQLITE_DEBUG
79661/*
79662** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
79663** and false if something is wrong.
79664**
79665** This routine is intended for use inside of assert() statements only.
79666*/
79667SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
79668 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
79669 return 1;
79670}
79671#endif
79672
79673
79674/*
79675** This is a destructor on a Mem object (which is really an sqlite3_value)
79676** that deletes the Frame object that is attached to it as a blob.
79677**
79678** This routine does not delete the Frame right away. It merely adds the
79679** frame to a list of frames to be deleted when the Vdbe halts.
79680*/
79681SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
79682 VdbeFrame *pFrame = (VdbeFrame*)pArg;
79683 assert( sqlite3VdbeFrameIsValid(pFrame) );
79684 pFrame->pParent = pFrame->v->pDelFrame;
79685 pFrame->v->pDelFrame = pFrame;
79686}
79687
79688#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
79689/*
79690** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
79691** QUERY PLAN output.
79692**
79693** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
79694** more opcodes to be displayed.
79695*/
79696SQLITE_PRIVATE int sqlite3VdbeNextOpcode(
79697 Vdbe *p, /* The statement being explained */
79698 Mem *pSub, /* Storage for keeping track of subprogram nesting */
79699 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
79700 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
79701 int *piAddr, /* OUT: Write index into (*paOp)[] here */
79702 Op **paOp /* OUT: Write the opcode array here */
79703){
79704 int nRow; /* Stop when row count reaches this */
79705 int nSub = 0; /* Number of sub-vdbes seen so far */
79706 SubProgram **apSub = 0; /* Array of sub-vdbes */
79707 int i; /* Next instruction address */
79708 int rc = SQLITE_OK; /* Result code */
79709 Op *aOp = 0; /* Opcode array */
79710 int iPc; /* Rowid. Copy of value in *piPc */
79711
79712 /* When the number of output rows reaches nRow, that means the
79713 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
79714 ** nRow is the sum of the number of rows in the main program, plus
79715 ** the sum of the number of rows in all trigger subprograms encountered
79716 ** so far. The nRow value will increase as new trigger subprograms are
79717 ** encountered, but p->pc will eventually catch up to nRow.
79718 */
79719 nRow = p->nOp;
79720 if( pSub!=0 ){
79721 if( pSub->flags&MEM_Blob ){
79722 /* pSub is initiallly NULL. It is initialized to a BLOB by
79723 ** the P4_SUBPROGRAM processing logic below */
79724 nSub = pSub->n/sizeof(Vdbe*);
79725 apSub = (SubProgram **)pSub->z;
79726 }
79727 for(i=0; i<nSub; i++){
79728 nRow += apSub[i]->nOp;
79729 }
79730 }
79731 iPc = *piPc;
79732 while(1){ /* Loop exits via break */
79733 i = iPc++;
79734 if( i>=nRow ){
79735 p->rc = SQLITE_OK;
79736 rc = SQLITE_DONE;
79737 break;
79738 }
79739 if( i<p->nOp ){
79740 /* The rowid is small enough that we are still in the
79741 ** main program. */
79742 aOp = p->aOp;
79743 }else{
79744 /* We are currently listing subprograms. Figure out which one and
79745 ** pick up the appropriate opcode. */
79746 int j;
79747 i -= p->nOp;
79748 assert( apSub!=0 );
79749 assert( nSub>0 );
79750 for(j=0; i>=apSub[j]->nOp; j++){
79751 i -= apSub[j]->nOp;
79752 assert( i<apSub[j]->nOp || j+1<nSub );
79753 }
79754 aOp = apSub[j]->aOp;
79755 }
79756
79757 /* When an OP_Program opcode is encounter (the only opcode that has
79758 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
79759 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
79760 ** has not already been seen.
79761 */
79762 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){
79763 int nByte = (nSub+1)*sizeof(SubProgram*);
79764 int j;
79765 for(j=0; j<nSub; j++){
79766 if( apSub[j]==aOp[i].p4.pProgram ) break;
79767 }
79768 if( j==nSub ){
79769 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
79770 if( p->rc!=SQLITE_OK ){
79771 rc = SQLITE_ERROR;
79772 break;
79773 }
79774 apSub = (SubProgram **)pSub->z;
79775 apSub[nSub++] = aOp[i].p4.pProgram;
79776 MemSetTypeFlag(pSub, MEM_Blob);
79777 pSub->n = nSub*sizeof(SubProgram*);
79778 nRow += aOp[i].p4.pProgram->nOp;
79779 }
79780 }
79781 if( eMode==0 ) break;
79782#ifdef SQLITE_ENABLE_BYTECODE_VTAB
79783 if( eMode==2 ){
79784 Op *pOp = aOp + i;
79785 if( pOp->opcode==OP_OpenRead ) break;
79786 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
79787 if( pOp->opcode==OP_ReopenIdx ) break;
79788 }else
79789#endif
79790 {
79791 assert( eMode==1 );
79792 if( aOp[i].opcode==OP_Explain ) break;
79793 if( aOp[i].opcode==OP_Init && iPc>1 ) break;
79794 }
79795 }
79796 *piPc = iPc;
79797 *piAddr = i;
79798 *paOp = aOp;
79799 return rc;
79800}
79801#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
79802
79803
79804/*
79805** Delete a VdbeFrame object and its contents. VdbeFrame objects are
79806** allocated by the OP_Program opcode in sqlite3VdbeExec().
79807*/
79808SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
79809 int i;
79810 Mem *aMem = VdbeFrameMem(p);
79811 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
79812 assert( sqlite3VdbeFrameIsValid(p) );
79813 for(i=0; i<p->nChildCsr; i++){
79814 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
79815 }
79816 releaseMemArray(aMem, p->nChildMem);
79817 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
79818 sqlite3DbFree(p->v->db, p);
79819}
79820
79821#ifndef SQLITE_OMIT_EXPLAIN
79822/*
79823** Give a listing of the program in the virtual machine.
79824**
79825** The interface is the same as sqlite3VdbeExec(). But instead of
79826** running the code, it invokes the callback once for each instruction.
79827** This feature is used to implement "EXPLAIN".
79828**
79829** When p->explain==1, each instruction is listed. When
79830** p->explain==2, only OP_Explain instructions are listed and these
79831** are shown in a different format. p->explain==2 is used to implement
79832** EXPLAIN QUERY PLAN.
79833** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
79834** are also shown, so that the boundaries between the main program and
79835** each trigger are clear.
79836**
79837** When p->explain==1, first the main program is listed, then each of
79838** the trigger subprograms are listed one by one.
79839*/
79840SQLITE_PRIVATE int sqlite3VdbeList(
79841 Vdbe *p /* The VDBE */
79842){
79843 Mem *pSub = 0; /* Memory cell hold array of subprogs */
79844 sqlite3 *db = p->db; /* The database connection */
79845 int i; /* Loop counter */
79846 int rc = SQLITE_OK; /* Return code */
79847 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
79848 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
79849 Op *aOp; /* Array of opcodes */
79850 Op *pOp; /* Current opcode */
79851
79852 assert( p->explain );
79853 assert( p->magic==VDBE_MAGIC_RUN );
79854 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
79855
79856 /* Even though this opcode does not use dynamic strings for
79857 ** the result, result columns may become dynamic if the user calls
79858 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
79859 */
79860 releaseMemArray(pMem, 8);
79861 p->pResultSet = 0;
79862
79863 if( p->rc==SQLITE_NOMEM ){
79864 /* This happens if a malloc() inside a call to sqlite3_column_text() or
79865 ** sqlite3_column_text16() failed. */
79866 sqlite3OomFault(db);
79867 return SQLITE_ERROR;
79868 }
79869
79870 if( bListSubprogs ){
79871 /* The first 8 memory cells are used for the result set. So we will
79872 ** commandeer the 9th cell to use as storage for an array of pointers
79873 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
79874 ** cells. */
79875 assert( p->nMem>9 );
79876 pSub = &p->aMem[9];
79877 }else{
79878 pSub = 0;
79879 }
79880
79881 /* Figure out which opcode is next to display */
79882 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
79883
79884 if( rc==SQLITE_OK ){
79885 pOp = aOp + i;
79886 if( AtomicLoad(&db->u1.isInterrupted) ){
79887 p->rc = SQLITE_INTERRUPT;
79888 rc = SQLITE_ERROR;
79889 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
79890 }else{
79891 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
79892 if( p->explain==2 ){
79893 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
79894 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
79895 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
79896 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
79897 p->nResColumn = 4;
79898 }else{
79899 sqlite3VdbeMemSetInt64(pMem+0, i);
79900 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
79901 -1, SQLITE_UTF8, SQLITE_STATIC);
79902 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
79903 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
79904 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
79905 /* pMem+5 for p4 is done last */
79906 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
79907#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
79908 {
79909 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
79910 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
79911 }
79912#else
79913 sqlite3VdbeMemSetNull(pMem+7);
79914#endif
79915 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
79916 p->nResColumn = 8;
79917 }
79918 p->pResultSet = pMem;
79919 if( db->mallocFailed ){
79920 p->rc = SQLITE_NOMEM;
79921 rc = SQLITE_ERROR;
79922 }else{
79923 p->rc = SQLITE_OK;
79924 rc = SQLITE_ROW;
79925 }
79926 }
79927 }
79928 return rc;
79929}
79930#endif /* SQLITE_OMIT_EXPLAIN */
79931
79932#ifdef SQLITE_DEBUG
79933/*
79934** Print the SQL that was used to generate a VDBE program.
79935*/
79936SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
79937 const char *z = 0;
79938 if( p->zSql ){
79939 z = p->zSql;
79940 }else if( p->nOp>=1 ){
79941 const VdbeOp *pOp = &p->aOp[0];
79942 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
79943 z = pOp->p4.z;
79944 while( sqlite3Isspace(*z) ) z++;
79945 }
79946 }
79947 if( z ) printf("SQL: [%s]\n", z);
79948}
79949#endif
79950
79951#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
79952/*
79953** Print an IOTRACE message showing SQL content.
79954*/
79955SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
79956 int nOp = p->nOp;
79957 VdbeOp *pOp;
79958 if( sqlite3IoTrace==0 ) return;
79959 if( nOp<1 ) return;
79960 pOp = &p->aOp[0];
79961 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
79962 int i, j;
79963 char z[1000];
79964 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
79965 for(i=0; sqlite3Isspace(z[i]); i++){}
79966 for(j=0; z[i]; i++){
79967 if( sqlite3Isspace(z[i]) ){
79968 if( z[i-1]!=' ' ){
79969 z[j++] = ' ';
79970 }
79971 }else{
79972 z[j++] = z[i];
79973 }
79974 }
79975 z[j] = 0;
79976 sqlite3IoTrace("SQL %s\n", z);
79977 }
79978}
79979#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
79980
79981/* An instance of this object describes bulk memory available for use
79982** by subcomponents of a prepared statement. Space is allocated out
79983** of a ReusableSpace object by the allocSpace() routine below.
79984*/
79985struct ReusableSpace {
79986 u8 *pSpace; /* Available memory */
79987 sqlite3_int64 nFree; /* Bytes of available memory */
79988 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
79989};
79990
79991/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
79992** from the ReusableSpace object. Return a pointer to the allocated
79993** memory on success. If insufficient memory is available in the
79994** ReusableSpace object, increase the ReusableSpace.nNeeded
79995** value by the amount needed and return NULL.
79996**
79997** If pBuf is not initially NULL, that means that the memory has already
79998** been allocated by a prior call to this routine, so just return a copy
79999** of pBuf and leave ReusableSpace unchanged.
80000**
80001** This allocator is employed to repurpose unused slots at the end of the
80002** opcode array of prepared state for other memory needs of the prepared
80003** statement.
80004*/
80005static void *allocSpace(
80006 struct ReusableSpace *p, /* Bulk memory available for allocation */
80007 void *pBuf, /* Pointer to a prior allocation */
80008 sqlite3_int64 nByte /* Bytes of memory needed */
80009){
80010 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
80011 if( pBuf==0 ){
80012 nByte = ROUND8(nByte);
80013 if( nByte <= p->nFree ){
80014 p->nFree -= nByte;
80015 pBuf = &p->pSpace[p->nFree];
80016 }else{
80017 p->nNeeded += nByte;
80018 }
80019 }
80020 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
80021 return pBuf;
80022}
80023
80024/*
80025** Rewind the VDBE back to the beginning in preparation for
80026** running it.
80027*/
80028SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
80029#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
80030 int i;
80031#endif
80032 assert( p!=0 );
80033 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
80034
80035 /* There should be at least one opcode.
80036 */
80037 assert( p->nOp>0 );
80038
80039 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
80040 p->magic = VDBE_MAGIC_RUN;
80041
80042#ifdef SQLITE_DEBUG
80043 for(i=0; i<p->nMem; i++){
80044 assert( p->aMem[i].db==p->db );
80045 }
80046#endif
80047 p->pc = -1;
80048 p->rc = SQLITE_OK;
80049 p->errorAction = OE_Abort;
80050 p->nChange = 0;
80051 p->cacheCtr = 1;
80052 p->minWriteFileFormat = 255;
80053 p->iStatement = 0;
80054 p->nFkConstraint = 0;
80055#ifdef VDBE_PROFILE
80056 for(i=0; i<p->nOp; i++){
80057 p->aOp[i].cnt = 0;
80058 p->aOp[i].cycles = 0;
80059 }
80060#endif
80061}
80062
80063/*
80064** Prepare a virtual machine for execution for the first time after
80065** creating the virtual machine. This involves things such
80066** as allocating registers and initializing the program counter.
80067** After the VDBE has be prepped, it can be executed by one or more
80068** calls to sqlite3VdbeExec().
80069**
80070** This function may be called exactly once on each virtual machine.
80071** After this routine is called the VM has been "packaged" and is ready
80072** to run. After this routine is called, further calls to
80073** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
80074** the Vdbe from the Parse object that helped generate it so that the
80075** the Vdbe becomes an independent entity and the Parse object can be
80076** destroyed.
80077**
80078** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
80079** to its initial state after it has been run.
80080*/
80081SQLITE_PRIVATE void sqlite3VdbeMakeReady(
80082 Vdbe *p, /* The VDBE */
80083 Parse *pParse /* Parsing context */
80084){
80085 sqlite3 *db; /* The database connection */
80086 int nVar; /* Number of parameters */
80087 int nMem; /* Number of VM memory registers */
80088 int nCursor; /* Number of cursors required */
80089 int nArg; /* Number of arguments in subprograms */
80090 int n; /* Loop counter */
80091 struct ReusableSpace x; /* Reusable bulk memory */
80092
80093 assert( p!=0 );
80094 assert( p->nOp>0 );
80095 assert( pParse!=0 );
80096 assert( p->magic==VDBE_MAGIC_INIT );
80097 assert( pParse==p->pParse );
80098 db = p->db;
80099 assert( db->mallocFailed==0 );
80100 nVar = pParse->nVar;
80101 nMem = pParse->nMem;
80102 nCursor = pParse->nTab;
80103 nArg = pParse->nMaxArg;
80104
80105 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
80106 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
80107 ** space at the end of aMem[] for cursors 1 and greater.
80108 ** See also: allocateCursor().
80109 */
80110 nMem += nCursor;
80111 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
80112
80113 /* Figure out how much reusable memory is available at the end of the
80114 ** opcode array. This extra memory will be reallocated for other elements
80115 ** of the prepared statement.
80116 */
80117 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
80118 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
80119 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
80120 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
80121 assert( x.nFree>=0 );
80122 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
80123
80124 resolveP2Values(p, &nArg);
80125 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
80126 if( pParse->explain ){
80127 static const char * const azColName[] = {
80128 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
80129 "id", "parent", "notused", "detail"
80130 };
80131 int iFirst, mx, i;
80132 if( nMem<10 ) nMem = 10;
80133 p->explain = pParse->explain;
80134 if( pParse->explain==2 ){
80135 sqlite3VdbeSetNumCols(p, 4);
80136 iFirst = 8;
80137 mx = 12;
80138 }else{
80139 sqlite3VdbeSetNumCols(p, 8);
80140 iFirst = 0;
80141 mx = 8;
80142 }
80143 for(i=iFirst; i<mx; i++){
80144 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
80145 azColName[i], SQLITE_STATIC);
80146 }
80147 }
80148 p->expired = 0;
80149
80150 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
80151 ** passes. On the first pass, we try to reuse unused memory at the
80152 ** end of the opcode array. If we are unable to satisfy all memory
80153 ** requirements by reusing the opcode array tail, then the second
80154 ** pass will fill in the remainder using a fresh memory allocation.
80155 **
80156 ** This two-pass approach that reuses as much memory as possible from
80157 ** the leftover memory at the end of the opcode array. This can significantly
80158 ** reduce the amount of memory held by a prepared statement.
80159 */
80160 x.nNeeded = 0;
80161 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
80162 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
80163 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
80164 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
80165#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80166 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
80167#endif
80168 if( x.nNeeded ){
80169 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
80170 x.nFree = x.nNeeded;
80171 if( !db->mallocFailed ){
80172 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
80173 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
80174 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
80175 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
80176#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80177 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
80178#endif
80179 }
80180 }
80181
80182 p->pVList = pParse->pVList;
80183 pParse->pVList = 0;
80184 if( db->mallocFailed ){
80185 p->nVar = 0;
80186 p->nCursor = 0;
80187 p->nMem = 0;
80188 }else{
80189 p->nCursor = nCursor;
80190 p->nVar = (ynVar)nVar;
80191 initMemArray(p->aVar, nVar, db, MEM_Null);
80192 p->nMem = nMem;
80193 initMemArray(p->aMem, nMem, db, MEM_Undefined);
80194 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
80195#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80196 memset(p->anExec, 0, p->nOp*sizeof(i64));
80197#endif
80198 }
80199 sqlite3VdbeRewind(p);
80200}
80201
80202/*
80203** Close a VDBE cursor and release all the resources that cursor
80204** happens to hold.
80205*/
80206SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
80207 if( pCx==0 ){
80208 return;
80209 }
80210 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
80211 switch( pCx->eCurType ){
80212 case CURTYPE_SORTER: {
80213 sqlite3VdbeSorterClose(p->db, pCx);
80214 break;
80215 }
80216 case CURTYPE_BTREE: {
80217 if( pCx->isEphemeral ){
80218 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
80219 /* The pCx->pCursor will be close automatically, if it exists, by
80220 ** the call above. */
80221 }else{
80222 assert( pCx->uc.pCursor!=0 );
80223 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
80224 }
80225 break;
80226 }
80227#ifndef SQLITE_OMIT_VIRTUALTABLE
80228 case CURTYPE_VTAB: {
80229 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
80230 const sqlite3_module *pModule = pVCur->pVtab->pModule;
80231 assert( pVCur->pVtab->nRef>0 );
80232 pVCur->pVtab->nRef--;
80233 pModule->xClose(pVCur);
80234 break;
80235 }
80236#endif
80237 }
80238}
80239
80240/*
80241** Close all cursors in the current frame.
80242*/
80243static void closeCursorsInFrame(Vdbe *p){
80244 if( p->apCsr ){
80245 int i;
80246 for(i=0; i<p->nCursor; i++){
80247 VdbeCursor *pC = p->apCsr[i];
80248 if( pC ){
80249 sqlite3VdbeFreeCursor(p, pC);
80250 p->apCsr[i] = 0;
80251 }
80252 }
80253 }
80254}
80255
80256/*
80257** Copy the values stored in the VdbeFrame structure to its Vdbe. This
80258** is used, for example, when a trigger sub-program is halted to restore
80259** control to the main program.
80260*/
80261SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
80262 Vdbe *v = pFrame->v;
80263 closeCursorsInFrame(v);
80264#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80265 v->anExec = pFrame->anExec;
80266#endif
80267 v->aOp = pFrame->aOp;
80268 v->nOp = pFrame->nOp;
80269 v->aMem = pFrame->aMem;
80270 v->nMem = pFrame->nMem;
80271 v->apCsr = pFrame->apCsr;
80272 v->nCursor = pFrame->nCursor;
80273 v->db->lastRowid = pFrame->lastRowid;
80274 v->nChange = pFrame->nChange;
80275 v->db->nChange = pFrame->nDbChange;
80276 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
80277 v->pAuxData = pFrame->pAuxData;
80278 pFrame->pAuxData = 0;
80279 return pFrame->pc;
80280}
80281
80282/*
80283** Close all cursors.
80284**
80285** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
80286** cell array. This is necessary as the memory cell array may contain
80287** pointers to VdbeFrame objects, which may in turn contain pointers to
80288** open cursors.
80289*/
80290static void closeAllCursors(Vdbe *p){
80291 if( p->pFrame ){
80292 VdbeFrame *pFrame;
80293 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
80294 sqlite3VdbeFrameRestore(pFrame);
80295 p->pFrame = 0;
80296 p->nFrame = 0;
80297 }
80298 assert( p->nFrame==0 );
80299 closeCursorsInFrame(p);
80300 if( p->aMem ){
80301 releaseMemArray(p->aMem, p->nMem);
80302 }
80303 while( p->pDelFrame ){
80304 VdbeFrame *pDel = p->pDelFrame;
80305 p->pDelFrame = pDel->pParent;
80306 sqlite3VdbeFrameDelete(pDel);
80307 }
80308
80309 /* Delete any auxdata allocations made by the VM */
80310 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
80311 assert( p->pAuxData==0 );
80312}
80313
80314/*
80315** Set the number of result columns that will be returned by this SQL
80316** statement. This is now set at compile time, rather than during
80317** execution of the vdbe program so that sqlite3_column_count() can
80318** be called on an SQL statement before sqlite3_step().
80319*/
80320SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
80321 int n;
80322 sqlite3 *db = p->db;
80323
80324 if( p->nResColumn ){
80325 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
80326 sqlite3DbFree(db, p->aColName);
80327 }
80328 n = nResColumn*COLNAME_N;
80329 p->nResColumn = (u16)nResColumn;
80330 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
80331 if( p->aColName==0 ) return;
80332 initMemArray(p->aColName, n, db, MEM_Null);
80333}
80334
80335/*
80336** Set the name of the idx'th column to be returned by the SQL statement.
80337** zName must be a pointer to a nul terminated string.
80338**
80339** This call must be made after a call to sqlite3VdbeSetNumCols().
80340**
80341** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
80342** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
80343** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
80344*/
80345SQLITE_PRIVATE int sqlite3VdbeSetColName(
80346 Vdbe *p, /* Vdbe being configured */
80347 int idx, /* Index of column zName applies to */
80348 int var, /* One of the COLNAME_* constants */
80349 const char *zName, /* Pointer to buffer containing name */
80350 void (*xDel)(void*) /* Memory management strategy for zName */
80351){
80352 int rc;
80353 Mem *pColName;
80354 assert( idx<p->nResColumn );
80355 assert( var<COLNAME_N );
80356 if( p->db->mallocFailed ){
80357 assert( !zName || xDel!=SQLITE_DYNAMIC );
80358 return SQLITE_NOMEM_BKPT;
80359 }
80360 assert( p->aColName!=0 );
80361 pColName = &(p->aColName[idx+var*p->nResColumn]);
80362 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
80363 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
80364 return rc;
80365}
80366
80367/*
80368** A read or write transaction may or may not be active on database handle
80369** db. If a transaction is active, commit it. If there is a
80370** write-transaction spanning more than one database file, this routine
80371** takes care of the super-journal trickery.
80372*/
80373static int vdbeCommit(sqlite3 *db, Vdbe *p){
80374 int i;
80375 int nTrans = 0; /* Number of databases with an active write-transaction
80376 ** that are candidates for a two-phase commit using a
80377 ** super-journal */
80378 int rc = SQLITE_OK;
80379 int needXcommit = 0;
80380
80381#ifdef SQLITE_OMIT_VIRTUALTABLE
80382 /* With this option, sqlite3VtabSync() is defined to be simply
80383 ** SQLITE_OK so p is not used.
80384 */
80385 UNUSED_PARAMETER(p);
80386#endif
80387
80388 /* Before doing anything else, call the xSync() callback for any
80389 ** virtual module tables written in this transaction. This has to
80390 ** be done before determining whether a super-journal file is
80391 ** required, as an xSync() callback may add an attached database
80392 ** to the transaction.
80393 */
80394 rc = sqlite3VtabSync(db, p);
80395
80396 /* This loop determines (a) if the commit hook should be invoked and
80397 ** (b) how many database files have open write transactions, not
80398 ** including the temp database. (b) is important because if more than
80399 ** one database file has an open write transaction, a super-journal
80400 ** file is required for an atomic commit.
80401 */
80402 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80403 Btree *pBt = db->aDb[i].pBt;
80404 if( sqlite3BtreeIsInTrans(pBt) ){
80405 /* Whether or not a database might need a super-journal depends upon
80406 ** its journal mode (among other things). This matrix determines which
80407 ** journal modes use a super-journal and which do not */
80408 static const u8 aMJNeeded[] = {
80409 /* DELETE */ 1,
80410 /* PERSIST */ 1,
80411 /* OFF */ 0,
80412 /* TRUNCATE */ 1,
80413 /* MEMORY */ 0,
80414 /* WAL */ 0
80415 };
80416 Pager *pPager; /* Pager associated with pBt */
80417 needXcommit = 1;
80418 sqlite3BtreeEnter(pBt);
80419 pPager = sqlite3BtreePager(pBt);
80420 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
80421 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
80422 && sqlite3PagerIsMemdb(pPager)==0
80423 ){
80424 assert( i!=1 );
80425 nTrans++;
80426 }
80427 rc = sqlite3PagerExclusiveLock(pPager);
80428 sqlite3BtreeLeave(pBt);
80429 }
80430 }
80431 if( rc!=SQLITE_OK ){
80432 return rc;
80433 }
80434
80435 /* If there are any write-transactions at all, invoke the commit hook */
80436 if( needXcommit && db->xCommitCallback ){
80437 rc = db->xCommitCallback(db->pCommitArg);
80438 if( rc ){
80439 return SQLITE_CONSTRAINT_COMMITHOOK;
80440 }
80441 }
80442
80443 /* The simple case - no more than one database file (not counting the
80444 ** TEMP database) has a transaction active. There is no need for the
80445 ** super-journal.
80446 **
80447 ** If the return value of sqlite3BtreeGetFilename() is a zero length
80448 ** string, it means the main database is :memory: or a temp file. In
80449 ** that case we do not support atomic multi-file commits, so use the
80450 ** simple case then too.
80451 */
80452 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
80453 || nTrans<=1
80454 ){
80455 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80456 Btree *pBt = db->aDb[i].pBt;
80457 if( pBt ){
80458 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
80459 }
80460 }
80461
80462 /* Do the commit only if all databases successfully complete phase 1.
80463 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
80464 ** IO error while deleting or truncating a journal file. It is unlikely,
80465 ** but could happen. In this case abandon processing and return the error.
80466 */
80467 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80468 Btree *pBt = db->aDb[i].pBt;
80469 if( pBt ){
80470 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
80471 }
80472 }
80473 if( rc==SQLITE_OK ){
80474 sqlite3VtabCommit(db);
80475 }
80476 }
80477
80478 /* The complex case - There is a multi-file write-transaction active.
80479 ** This requires a super-journal file to ensure the transaction is
80480 ** committed atomically.
80481 */
80482#ifndef SQLITE_OMIT_DISKIO
80483 else{
80484 sqlite3_vfs *pVfs = db->pVfs;
80485 char *zSuper = 0; /* File-name for the super-journal */
80486 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
80487 sqlite3_file *pSuperJrnl = 0;
80488 i64 offset = 0;
80489 int res;
80490 int retryCount = 0;
80491 int nMainFile;
80492
80493 /* Select a super-journal file name */
80494 nMainFile = sqlite3Strlen30(zMainFile);
80495 zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
80496 if( zSuper==0 ) return SQLITE_NOMEM_BKPT;
80497 zSuper += 4;
80498 do {
80499 u32 iRandom;
80500 if( retryCount ){
80501 if( retryCount>100 ){
80502 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zSuper);
80503 sqlite3OsDelete(pVfs, zSuper, 0);
80504 break;
80505 }else if( retryCount==1 ){
80506 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zSuper);
80507 }
80508 }
80509 retryCount++;
80510 sqlite3_randomness(sizeof(iRandom), &iRandom);
80511 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
80512 (iRandom>>8)&0xffffff, iRandom&0xff);
80513 /* The antipenultimate character of the super-journal name must
80514 ** be "9" to avoid name collisions when using 8+3 filenames. */
80515 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
80516 sqlite3FileSuffix3(zMainFile, zSuper);
80517 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
80518 }while( rc==SQLITE_OK && res );
80519 if( rc==SQLITE_OK ){
80520 /* Open the super-journal. */
80521 rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
80522 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
80523 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, 0
80524 );
80525 }
80526 if( rc!=SQLITE_OK ){
80527 sqlite3DbFree(db, zSuper-4);
80528 return rc;
80529 }
80530
80531 /* Write the name of each database file in the transaction into the new
80532 ** super-journal file. If an error occurs at this point close
80533 ** and delete the super-journal file. All the individual journal files
80534 ** still have 'null' as the super-journal pointer, so they will roll
80535 ** back independently if a failure occurs.
80536 */
80537 for(i=0; i<db->nDb; i++){
80538 Btree *pBt = db->aDb[i].pBt;
80539 if( sqlite3BtreeIsInTrans(pBt) ){
80540 char const *zFile = sqlite3BtreeGetJournalname(pBt);
80541 if( zFile==0 ){
80542 continue; /* Ignore TEMP and :memory: databases */
80543 }
80544 assert( zFile[0]!=0 );
80545 rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
80546 offset += sqlite3Strlen30(zFile)+1;
80547 if( rc!=SQLITE_OK ){
80548 sqlite3OsCloseFree(pSuperJrnl);
80549 sqlite3OsDelete(pVfs, zSuper, 0);
80550 sqlite3DbFree(db, zSuper-4);
80551 return rc;
80552 }
80553 }
80554 }
80555
80556 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
80557 ** flag is set this is not required.
80558 */
80559 if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL)
80560 && SQLITE_OK!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL))
80561 ){
80562 sqlite3OsCloseFree(pSuperJrnl);
80563 sqlite3OsDelete(pVfs, zSuper, 0);
80564 sqlite3DbFree(db, zSuper-4);
80565 return rc;
80566 }
80567
80568 /* Sync all the db files involved in the transaction. The same call
80569 ** sets the super-journal pointer in each individual journal. If
80570 ** an error occurs here, do not delete the super-journal file.
80571 **
80572 ** If the error occurs during the first call to
80573 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
80574 ** super-journal file will be orphaned. But we cannot delete it,
80575 ** in case the super-journal file name was written into the journal
80576 ** file before the failure occurred.
80577 */
80578 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
80579 Btree *pBt = db->aDb[i].pBt;
80580 if( pBt ){
80581 rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
80582 }
80583 }
80584 sqlite3OsCloseFree(pSuperJrnl);
80585 assert( rc!=SQLITE_BUSY );
80586 if( rc!=SQLITE_OK ){
80587 sqlite3DbFree(db, zSuper-4);
80588 return rc;
80589 }
80590
80591 /* Delete the super-journal file. This commits the transaction. After
80592 ** doing this the directory is synced again before any individual
80593 ** transaction files are deleted.
80594 */
80595 rc = sqlite3OsDelete(pVfs, zSuper, 1);
80596 sqlite3DbFree(db, zSuper-4);
80597 zSuper = 0;
80598 if( rc ){
80599 return rc;
80600 }
80601
80602 /* All files and directories have already been synced, so the following
80603 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
80604 ** deleting or truncating journals. If something goes wrong while
80605 ** this is happening we don't really care. The integrity of the
80606 ** transaction is already guaranteed, but some stray 'cold' journals
80607 ** may be lying around. Returning an error code won't help matters.
80608 */
80609 disable_simulated_io_errors();
80610 sqlite3BeginBenignMalloc();
80611 for(i=0; i<db->nDb; i++){
80612 Btree *pBt = db->aDb[i].pBt;
80613 if( pBt ){
80614 sqlite3BtreeCommitPhaseTwo(pBt, 1);
80615 }
80616 }
80617 sqlite3EndBenignMalloc();
80618 enable_simulated_io_errors();
80619
80620 sqlite3VtabCommit(db);
80621 }
80622#endif
80623
80624 return rc;
80625}
80626
80627/*
80628** This routine checks that the sqlite3.nVdbeActive count variable
80629** matches the number of vdbe's in the list sqlite3.pVdbe that are
80630** currently active. An assertion fails if the two counts do not match.
80631** This is an internal self-check only - it is not an essential processing
80632** step.
80633**
80634** This is a no-op if NDEBUG is defined.
80635*/
80636#ifndef NDEBUG
80637static void checkActiveVdbeCnt(sqlite3 *db){
80638 Vdbe *p;
80639 int cnt = 0;
80640 int nWrite = 0;
80641 int nRead = 0;
80642 p = db->pVdbe;
80643 while( p ){
80644 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
80645 cnt++;
80646 if( p->readOnly==0 ) nWrite++;
80647 if( p->bIsReader ) nRead++;
80648 }
80649 p = p->pNext;
80650 }
80651 assert( cnt==db->nVdbeActive );
80652 assert( nWrite==db->nVdbeWrite );
80653 assert( nRead==db->nVdbeRead );
80654}
80655#else
80656#define checkActiveVdbeCnt(x)
80657#endif
80658
80659/*
80660** If the Vdbe passed as the first argument opened a statement-transaction,
80661** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
80662** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
80663** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
80664** statement transaction is committed.
80665**
80666** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
80667** Otherwise SQLITE_OK.
80668*/
80669static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
80670 sqlite3 *const db = p->db;
80671 int rc = SQLITE_OK;
80672 int i;
80673 const int iSavepoint = p->iStatement-1;
80674
80675 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
80676 assert( db->nStatement>0 );
80677 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
80678
80679 for(i=0; i<db->nDb; i++){
80680 int rc2 = SQLITE_OK;
80681 Btree *pBt = db->aDb[i].pBt;
80682 if( pBt ){
80683 if( eOp==SAVEPOINT_ROLLBACK ){
80684 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
80685 }
80686 if( rc2==SQLITE_OK ){
80687 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
80688 }
80689 if( rc==SQLITE_OK ){
80690 rc = rc2;
80691 }
80692 }
80693 }
80694 db->nStatement--;
80695 p->iStatement = 0;
80696
80697 if( rc==SQLITE_OK ){
80698 if( eOp==SAVEPOINT_ROLLBACK ){
80699 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
80700 }
80701 if( rc==SQLITE_OK ){
80702 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
80703 }
80704 }
80705
80706 /* If the statement transaction is being rolled back, also restore the
80707 ** database handles deferred constraint counter to the value it had when
80708 ** the statement transaction was opened. */
80709 if( eOp==SAVEPOINT_ROLLBACK ){
80710 db->nDeferredCons = p->nStmtDefCons;
80711 db->nDeferredImmCons = p->nStmtDefImmCons;
80712 }
80713 return rc;
80714}
80715SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
80716 if( p->db->nStatement && p->iStatement ){
80717 return vdbeCloseStatement(p, eOp);
80718 }
80719 return SQLITE_OK;
80720}
80721
80722
80723/*
80724** This function is called when a transaction opened by the database
80725** handle associated with the VM passed as an argument is about to be
80726** committed. If there are outstanding deferred foreign key constraint
80727** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
80728**
80729** If there are outstanding FK violations and this function returns
80730** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
80731** and write an error message to it. Then return SQLITE_ERROR.
80732*/
80733#ifndef SQLITE_OMIT_FOREIGN_KEY
80734SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
80735 sqlite3 *db = p->db;
80736 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
80737 || (!deferred && p->nFkConstraint>0)
80738 ){
80739 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
80740 p->errorAction = OE_Abort;
80741 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
80742 return SQLITE_ERROR;
80743 }
80744 return SQLITE_OK;
80745}
80746#endif
80747
80748/*
80749** This routine is called the when a VDBE tries to halt. If the VDBE
80750** has made changes and is in autocommit mode, then commit those
80751** changes. If a rollback is needed, then do the rollback.
80752**
80753** This routine is the only way to move the state of a VM from
80754** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
80755** call this on a VM that is in the SQLITE_MAGIC_HALT state.
80756**
80757** Return an error code. If the commit could not complete because of
80758** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
80759** means the close did not happen and needs to be repeated.
80760*/
80761SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
80762 int rc; /* Used to store transient return codes */
80763 sqlite3 *db = p->db;
80764
80765 /* This function contains the logic that determines if a statement or
80766 ** transaction will be committed or rolled back as a result of the
80767 ** execution of this virtual machine.
80768 **
80769 ** If any of the following errors occur:
80770 **
80771 ** SQLITE_NOMEM
80772 ** SQLITE_IOERR
80773 ** SQLITE_FULL
80774 ** SQLITE_INTERRUPT
80775 **
80776 ** Then the internal cache might have been left in an inconsistent
80777 ** state. We need to rollback the statement transaction, if there is
80778 ** one, or the complete transaction if there is no statement transaction.
80779 */
80780
80781 if( p->magic!=VDBE_MAGIC_RUN ){
80782 return SQLITE_OK;
80783 }
80784 if( db->mallocFailed ){
80785 p->rc = SQLITE_NOMEM_BKPT;
80786 }
80787 closeAllCursors(p);
80788 checkActiveVdbeCnt(db);
80789
80790 /* No commit or rollback needed if the program never started or if the
80791 ** SQL statement does not read or write a database file. */
80792 if( p->pc>=0 && p->bIsReader ){
80793 int mrc; /* Primary error code from p->rc */
80794 int eStatementOp = 0;
80795 int isSpecialError; /* Set to true if a 'special' error */
80796
80797 /* Lock all btrees used by the statement */
80798 sqlite3VdbeEnter(p);
80799
80800 /* Check for one of the special errors */
80801 mrc = p->rc & 0xff;
80802 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
80803 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
80804 if( isSpecialError ){
80805 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
80806 ** no rollback is necessary. Otherwise, at least a savepoint
80807 ** transaction must be rolled back to restore the database to a
80808 ** consistent state.
80809 **
80810 ** Even if the statement is read-only, it is important to perform
80811 ** a statement or transaction rollback operation. If the error
80812 ** occurred while writing to the journal, sub-journal or database
80813 ** file as part of an effort to free up cache space (see function
80814 ** pagerStress() in pager.c), the rollback is required to restore
80815 ** the pager to a consistent state.
80816 */
80817 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
80818 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
80819 eStatementOp = SAVEPOINT_ROLLBACK;
80820 }else{
80821 /* We are forced to roll back the active transaction. Before doing
80822 ** so, abort any other statements this handle currently has active.
80823 */
80824 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80825 sqlite3CloseSavepoints(db);
80826 db->autoCommit = 1;
80827 p->nChange = 0;
80828 }
80829 }
80830 }
80831
80832 /* Check for immediate foreign key violations. */
80833 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
80834 sqlite3VdbeCheckFk(p, 0);
80835 }
80836
80837 /* If the auto-commit flag is set and this is the only active writer
80838 ** VM, then we do either a commit or rollback of the current transaction.
80839 **
80840 ** Note: This block also runs if one of the special errors handled
80841 ** above has occurred.
80842 */
80843 if( !sqlite3VtabInSync(db)
80844 && db->autoCommit
80845 && db->nVdbeWrite==(p->readOnly==0)
80846 ){
80847 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
80848 rc = sqlite3VdbeCheckFk(p, 1);
80849 if( rc!=SQLITE_OK ){
80850 if( NEVER(p->readOnly) ){
80851 sqlite3VdbeLeave(p);
80852 return SQLITE_ERROR;
80853 }
80854 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
80855 }else{
80856 /* The auto-commit flag is true, the vdbe program was successful
80857 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
80858 ** key constraints to hold up the transaction. This means a commit
80859 ** is required. */
80860 rc = vdbeCommit(db, p);
80861 }
80862 if( rc==SQLITE_BUSY && p->readOnly ){
80863 sqlite3VdbeLeave(p);
80864 return SQLITE_BUSY;
80865 }else if( rc!=SQLITE_OK ){
80866 p->rc = rc;
80867 sqlite3RollbackAll(db, SQLITE_OK);
80868 p->nChange = 0;
80869 }else{
80870 db->nDeferredCons = 0;
80871 db->nDeferredImmCons = 0;
80872 db->flags &= ~(u64)SQLITE_DeferFKs;
80873 sqlite3CommitInternalChanges(db);
80874 }
80875 }else{
80876 sqlite3RollbackAll(db, SQLITE_OK);
80877 p->nChange = 0;
80878 }
80879 db->nStatement = 0;
80880 }else if( eStatementOp==0 ){
80881 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
80882 eStatementOp = SAVEPOINT_RELEASE;
80883 }else if( p->errorAction==OE_Abort ){
80884 eStatementOp = SAVEPOINT_ROLLBACK;
80885 }else{
80886 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80887 sqlite3CloseSavepoints(db);
80888 db->autoCommit = 1;
80889 p->nChange = 0;
80890 }
80891 }
80892
80893 /* If eStatementOp is non-zero, then a statement transaction needs to
80894 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
80895 ** do so. If this operation returns an error, and the current statement
80896 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
80897 ** current statement error code.
80898 */
80899 if( eStatementOp ){
80900 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
80901 if( rc ){
80902 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
80903 p->rc = rc;
80904 sqlite3DbFree(db, p->zErrMsg);
80905 p->zErrMsg = 0;
80906 }
80907 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
80908 sqlite3CloseSavepoints(db);
80909 db->autoCommit = 1;
80910 p->nChange = 0;
80911 }
80912 }
80913
80914 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
80915 ** has been rolled back, update the database connection change-counter.
80916 */
80917 if( p->changeCntOn ){
80918 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
80919 sqlite3VdbeSetChanges(db, p->nChange);
80920 }else{
80921 sqlite3VdbeSetChanges(db, 0);
80922 }
80923 p->nChange = 0;
80924 }
80925
80926 /* Release the locks */
80927 sqlite3VdbeLeave(p);
80928 }
80929
80930 /* We have successfully halted and closed the VM. Record this fact. */
80931 if( p->pc>=0 ){
80932 db->nVdbeActive--;
80933 if( !p->readOnly ) db->nVdbeWrite--;
80934 if( p->bIsReader ) db->nVdbeRead--;
80935 assert( db->nVdbeActive>=db->nVdbeRead );
80936 assert( db->nVdbeRead>=db->nVdbeWrite );
80937 assert( db->nVdbeWrite>=0 );
80938 }
80939 p->magic = VDBE_MAGIC_HALT;
80940 checkActiveVdbeCnt(db);
80941 if( db->mallocFailed ){
80942 p->rc = SQLITE_NOMEM_BKPT;
80943 }
80944
80945 /* If the auto-commit flag is set to true, then any locks that were held
80946 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
80947 ** to invoke any required unlock-notify callbacks.
80948 */
80949 if( db->autoCommit ){
80950 sqlite3ConnectionUnlocked(db);
80951 }
80952
80953 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
80954 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
80955}
80956
80957
80958/*
80959** Each VDBE holds the result of the most recent sqlite3_step() call
80960** in p->rc. This routine sets that result back to SQLITE_OK.
80961*/
80962SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
80963 p->rc = SQLITE_OK;
80964}
80965
80966/*
80967** Copy the error code and error message belonging to the VDBE passed
80968** as the first argument to its database handle (so that they will be
80969** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
80970**
80971** This function does not clear the VDBE error code or message, just
80972** copies them to the database handle.
80973*/
80974SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
80975 sqlite3 *db = p->db;
80976 int rc = p->rc;
80977 if( p->zErrMsg ){
80978 db->bBenignMalloc++;
80979 sqlite3BeginBenignMalloc();
80980 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
80981 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
80982 sqlite3EndBenignMalloc();
80983 db->bBenignMalloc--;
80984 }else if( db->pErr ){
80985 sqlite3ValueSetNull(db->pErr);
80986 }
80987 db->errCode = rc;
80988 return rc;
80989}
80990
80991#ifdef SQLITE_ENABLE_SQLLOG
80992/*
80993** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
80994** invoke it.
80995*/
80996static void vdbeInvokeSqllog(Vdbe *v){
80997 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
80998 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
80999 assert( v->db->init.busy==0 );
81000 if( zExpanded ){
81001 sqlite3GlobalConfig.xSqllog(
81002 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
81003 );
81004 sqlite3DbFree(v->db, zExpanded);
81005 }
81006 }
81007}
81008#else
81009# define vdbeInvokeSqllog(x)
81010#endif
81011
81012/*
81013** Clean up a VDBE after execution but do not delete the VDBE just yet.
81014** Write any error messages into *pzErrMsg. Return the result code.
81015**
81016** After this routine is run, the VDBE should be ready to be executed
81017** again.
81018**
81019** To look at it another way, this routine resets the state of the
81020** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
81021** VDBE_MAGIC_INIT.
81022*/
81023SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
81024#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
81025 int i;
81026#endif
81027
81028 sqlite3 *db;
81029 db = p->db;
81030
81031 /* If the VM did not run to completion or if it encountered an
81032 ** error, then it might not have been halted properly. So halt
81033 ** it now.
81034 */
81035 sqlite3VdbeHalt(p);
81036
81037 /* If the VDBE has been run even partially, then transfer the error code
81038 ** and error message from the VDBE into the main database structure. But
81039 ** if the VDBE has just been set to run but has not actually executed any
81040 ** instructions yet, leave the main database error information unchanged.
81041 */
81042 if( p->pc>=0 ){
81043 vdbeInvokeSqllog(p);
81044 if( db->pErr || p->zErrMsg ){
81045 sqlite3VdbeTransferError(p);
81046 }else{
81047 db->errCode = p->rc;
81048 }
81049 if( p->runOnlyOnce ) p->expired = 1;
81050 }else if( p->rc && p->expired ){
81051 /* The expired flag was set on the VDBE before the first call
81052 ** to sqlite3_step(). For consistency (since sqlite3_step() was
81053 ** called), set the database error in this case as well.
81054 */
81055 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
81056 }
81057
81058 /* Reset register contents and reclaim error message memory.
81059 */
81060#ifdef SQLITE_DEBUG
81061 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
81062 ** Vdbe.aMem[] arrays have already been cleaned up. */
81063 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
81064 if( p->aMem ){
81065 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
81066 }
81067#endif
81068 if( p->zErrMsg ){
81069 sqlite3DbFree(db, p->zErrMsg);
81070 p->zErrMsg = 0;
81071 }
81072 p->pResultSet = 0;
81073#ifdef SQLITE_DEBUG
81074 p->nWrite = 0;
81075#endif
81076
81077 /* Save profiling information from this VDBE run.
81078 */
81079#ifdef VDBE_PROFILE
81080 {
81081 FILE *out = fopen("vdbe_profile.out", "a");
81082 if( out ){
81083 fprintf(out, "---- ");
81084 for(i=0; i<p->nOp; i++){
81085 fprintf(out, "%02x", p->aOp[i].opcode);
81086 }
81087 fprintf(out, "\n");
81088 if( p->zSql ){
81089 char c, pc = 0;
81090 fprintf(out, "-- ");
81091 for(i=0; (c = p->zSql[i])!=0; i++){
81092 if( pc=='\n' ) fprintf(out, "-- ");
81093 putc(c, out);
81094 pc = c;
81095 }
81096 if( pc!='\n' ) fprintf(out, "\n");
81097 }
81098 for(i=0; i<p->nOp; i++){
81099 char zHdr[100];
81100 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
81101 p->aOp[i].cnt,
81102 p->aOp[i].cycles,
81103 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
81104 );
81105 fprintf(out, "%s", zHdr);
81106 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
81107 }
81108 fclose(out);
81109 }
81110 }
81111#endif
81112 p->magic = VDBE_MAGIC_RESET;
81113 return p->rc & db->errMask;
81114}
81115
81116/*
81117** Clean up and delete a VDBE after execution. Return an integer which is
81118** the result code. Write any error message text into *pzErrMsg.
81119*/
81120SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
81121 int rc = SQLITE_OK;
81122 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
81123 rc = sqlite3VdbeReset(p);
81124 assert( (rc & p->db->errMask)==rc );
81125 }
81126 sqlite3VdbeDelete(p);
81127 return rc;
81128}
81129
81130/*
81131** If parameter iOp is less than zero, then invoke the destructor for
81132** all auxiliary data pointers currently cached by the VM passed as
81133** the first argument.
81134**
81135** Or, if iOp is greater than or equal to zero, then the destructor is
81136** only invoked for those auxiliary data pointers created by the user
81137** function invoked by the OP_Function opcode at instruction iOp of
81138** VM pVdbe, and only then if:
81139**
81140** * the associated function parameter is the 32nd or later (counting
81141** from left to right), or
81142**
81143** * the corresponding bit in argument mask is clear (where the first
81144** function parameter corresponds to bit 0 etc.).
81145*/
81146SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
81147 while( *pp ){
81148 AuxData *pAux = *pp;
81149 if( (iOp<0)
81150 || (pAux->iAuxOp==iOp
81151 && pAux->iAuxArg>=0
81152 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
81153 ){
81154 testcase( pAux->iAuxArg==31 );
81155 if( pAux->xDeleteAux ){
81156 pAux->xDeleteAux(pAux->pAux);
81157 }
81158 *pp = pAux->pNextAux;
81159 sqlite3DbFree(db, pAux);
81160 }else{
81161 pp= &pAux->pNextAux;
81162 }
81163 }
81164}
81165
81166/*
81167** Free all memory associated with the Vdbe passed as the second argument,
81168** except for object itself, which is preserved.
81169**
81170** The difference between this function and sqlite3VdbeDelete() is that
81171** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
81172** the database connection and frees the object itself.
81173*/
81174SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
81175 SubProgram *pSub, *pNext;
81176 assert( p->db==0 || p->db==db );
81177 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
81178 for(pSub=p->pProgram; pSub; pSub=pNext){
81179 pNext = pSub->pNext;
81180 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
81181 sqlite3DbFree(db, pSub);
81182 }
81183 if( p->magic!=VDBE_MAGIC_INIT ){
81184 releaseMemArray(p->aVar, p->nVar);
81185 sqlite3DbFree(db, p->pVList);
81186 sqlite3DbFree(db, p->pFree);
81187 }
81188 vdbeFreeOpArray(db, p->aOp, p->nOp);
81189 sqlite3DbFree(db, p->aColName);
81190 sqlite3DbFree(db, p->zSql);
81191#ifdef SQLITE_ENABLE_NORMALIZE
81192 sqlite3DbFree(db, p->zNormSql);
81193 {
81194 DblquoteStr *pThis, *pNext;
81195 for(pThis=p->pDblStr; pThis; pThis=pNext){
81196 pNext = pThis->pNextStr;
81197 sqlite3DbFree(db, pThis);
81198 }
81199 }
81200#endif
81201#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81202 {
81203 int i;
81204 for(i=0; i<p->nScan; i++){
81205 sqlite3DbFree(db, p->aScan[i].zName);
81206 }
81207 sqlite3DbFree(db, p->aScan);
81208 }
81209#endif
81210}
81211
81212/*
81213** Delete an entire VDBE.
81214*/
81215SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
81216 sqlite3 *db;
81217
81218 assert( p!=0 );
81219 db = p->db;
81220 assert( sqlite3_mutex_held(db->mutex) );
81221 sqlite3VdbeClearObject(db, p);
81222 if( p->pPrev ){
81223 p->pPrev->pNext = p->pNext;
81224 }else{
81225 assert( db->pVdbe==p );
81226 db->pVdbe = p->pNext;
81227 }
81228 if( p->pNext ){
81229 p->pNext->pPrev = p->pPrev;
81230 }
81231 p->magic = VDBE_MAGIC_DEAD;
81232 p->db = 0;
81233 sqlite3DbFreeNN(db, p);
81234}
81235
81236/*
81237** The cursor "p" has a pending seek operation that has not yet been
81238** carried out. Seek the cursor now. If an error occurs, return
81239** the appropriate error code.
81240*/
81241SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
81242 int res, rc;
81243#ifdef SQLITE_TEST
81244 extern int sqlite3_search_count;
81245#endif
81246 assert( p->deferredMoveto );
81247 assert( p->isTable );
81248 assert( p->eCurType==CURTYPE_BTREE );
81249 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
81250 if( rc ) return rc;
81251 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
81252#ifdef SQLITE_TEST
81253 sqlite3_search_count++;
81254#endif
81255 p->deferredMoveto = 0;
81256 p->cacheStatus = CACHE_STALE;
81257 return SQLITE_OK;
81258}
81259
81260/*
81261** Something has moved cursor "p" out of place. Maybe the row it was
81262** pointed to was deleted out from under it. Or maybe the btree was
81263** rebalanced. Whatever the cause, try to restore "p" to the place it
81264** is supposed to be pointing. If the row was deleted out from under the
81265** cursor, set the cursor to point to a NULL row.
81266*/
81267static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
81268 int isDifferentRow, rc;
81269 assert( p->eCurType==CURTYPE_BTREE );
81270 assert( p->uc.pCursor!=0 );
81271 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
81272 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
81273 p->cacheStatus = CACHE_STALE;
81274 if( isDifferentRow ) p->nullRow = 1;
81275 return rc;
81276}
81277
81278/*
81279** Check to ensure that the cursor is valid. Restore the cursor
81280** if need be. Return any I/O error from the restore operation.
81281*/
81282SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
81283 assert( p->eCurType==CURTYPE_BTREE );
81284 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81285 return handleMovedCursor(p);
81286 }
81287 return SQLITE_OK;
81288}
81289
81290/*
81291** Make sure the cursor p is ready to read or write the row to which it
81292** was last positioned. Return an error code if an OOM fault or I/O error
81293** prevents us from positioning the cursor to its correct position.
81294**
81295** If a MoveTo operation is pending on the given cursor, then do that
81296** MoveTo now. If no move is pending, check to see if the row has been
81297** deleted out from under the cursor and if it has, mark the row as
81298** a NULL row.
81299**
81300** If the cursor is already pointing to the correct row and that row has
81301** not been deleted out from under the cursor, then this routine is a no-op.
81302*/
81303SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, u32 *piCol){
81304 VdbeCursor *p = *pp;
81305 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
81306 if( p->deferredMoveto ){
81307 u32 iMap;
81308 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 && !p->nullRow ){
81309 *pp = p->pAltCursor;
81310 *piCol = iMap - 1;
81311 return SQLITE_OK;
81312 }
81313 return sqlite3VdbeFinishMoveto(p);
81314 }
81315 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81316 return handleMovedCursor(p);
81317 }
81318 return SQLITE_OK;
81319}
81320
81321/*
81322** The following functions:
81323**
81324** sqlite3VdbeSerialType()
81325** sqlite3VdbeSerialTypeLen()
81326** sqlite3VdbeSerialLen()
81327** sqlite3VdbeSerialPut()
81328** sqlite3VdbeSerialGet()
81329**
81330** encapsulate the code that serializes values for storage in SQLite
81331** data and index records. Each serialized value consists of a
81332** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
81333** integer, stored as a varint.
81334**
81335** In an SQLite index record, the serial type is stored directly before
81336** the blob of data that it corresponds to. In a table record, all serial
81337** types are stored at the start of the record, and the blobs of data at
81338** the end. Hence these functions allow the caller to handle the
81339** serial-type and data blob separately.
81340**
81341** The following table describes the various storage classes for data:
81342**
81343** serial type bytes of data type
81344** -------------- --------------- ---------------
81345** 0 0 NULL
81346** 1 1 signed integer
81347** 2 2 signed integer
81348** 3 3 signed integer
81349** 4 4 signed integer
81350** 5 6 signed integer
81351** 6 8 signed integer
81352** 7 8 IEEE float
81353** 8 0 Integer constant 0
81354** 9 0 Integer constant 1
81355** 10,11 reserved for expansion
81356** N>=12 and even (N-12)/2 BLOB
81357** N>=13 and odd (N-13)/2 text
81358**
81359** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
81360** of SQLite will not understand those serial types.
81361*/
81362
81363#if 0 /* Inlined into the OP_MakeRecord opcode */
81364/*
81365** Return the serial-type for the value stored in pMem.
81366**
81367** This routine might convert a large MEM_IntReal value into MEM_Real.
81368**
81369** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
81370** opcode in the byte-code engine. But by moving this routine in-line, we
81371** can omit some redundant tests and make that opcode a lot faster. So
81372** this routine is now only used by the STAT3 logic and STAT3 support has
81373** ended. The code is kept here for historical reference only.
81374*/
81375SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
81376 int flags = pMem->flags;
81377 u32 n;
81378
81379 assert( pLen!=0 );
81380 if( flags&MEM_Null ){
81381 *pLen = 0;
81382 return 0;
81383 }
81384 if( flags&(MEM_Int|MEM_IntReal) ){
81385 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
81386# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
81387 i64 i = pMem->u.i;
81388 u64 u;
81389 testcase( flags & MEM_Int );
81390 testcase( flags & MEM_IntReal );
81391 if( i<0 ){
81392 u = ~i;
81393 }else{
81394 u = i;
81395 }
81396 if( u<=127 ){
81397 if( (i&1)==i && file_format>=4 ){
81398 *pLen = 0;
81399 return 8+(u32)u;
81400 }else{
81401 *pLen = 1;
81402 return 1;
81403 }
81404 }
81405 if( u<=32767 ){ *pLen = 2; return 2; }
81406 if( u<=8388607 ){ *pLen = 3; return 3; }
81407 if( u<=2147483647 ){ *pLen = 4; return 4; }
81408 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
81409 *pLen = 8;
81410 if( flags&MEM_IntReal ){
81411 /* If the value is IntReal and is going to take up 8 bytes to store
81412 ** as an integer, then we might as well make it an 8-byte floating
81413 ** point value */
81414 pMem->u.r = (double)pMem->u.i;
81415 pMem->flags &= ~MEM_IntReal;
81416 pMem->flags |= MEM_Real;
81417 return 7;
81418 }
81419 return 6;
81420 }
81421 if( flags&MEM_Real ){
81422 *pLen = 8;
81423 return 7;
81424 }
81425 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
81426 assert( pMem->n>=0 );
81427 n = (u32)pMem->n;
81428 if( flags & MEM_Zero ){
81429 n += pMem->u.nZero;
81430 }
81431 *pLen = n;
81432 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
81433}
81434#endif /* inlined into OP_MakeRecord */
81435
81436/*
81437** The sizes for serial types less than 128
81438*/
81439static const u8 sqlite3SmallTypeSizes[] = {
81440 /* 0 1 2 3 4 5 6 7 8 9 */
81441/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
81442/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
81443/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
81444/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
81445/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
81446/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
81447/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
81448/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
81449/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
81450/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
81451/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
81452/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
81453/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
81454};
81455
81456/*
81457** Return the length of the data corresponding to the supplied serial-type.
81458*/
81459SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
81460 if( serial_type>=128 ){
81461 return (serial_type-12)/2;
81462 }else{
81463 assert( serial_type<12
81464 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
81465 return sqlite3SmallTypeSizes[serial_type];
81466 }
81467}
81468SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
81469 assert( serial_type<128 );
81470 return sqlite3SmallTypeSizes[serial_type];
81471}
81472
81473/*
81474** If we are on an architecture with mixed-endian floating
81475** points (ex: ARM7) then swap the lower 4 bytes with the
81476** upper 4 bytes. Return the result.
81477**
81478** For most architectures, this is a no-op.
81479**
81480** (later): It is reported to me that the mixed-endian problem
81481** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
81482** that early versions of GCC stored the two words of a 64-bit
81483** float in the wrong order. And that error has been propagated
81484** ever since. The blame is not necessarily with GCC, though.
81485** GCC might have just copying the problem from a prior compiler.
81486** I am also told that newer versions of GCC that follow a different
81487** ABI get the byte order right.
81488**
81489** Developers using SQLite on an ARM7 should compile and run their
81490** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
81491** enabled, some asserts below will ensure that the byte order of
81492** floating point values is correct.
81493**
81494** (2007-08-30) Frank van Vugt has studied this problem closely
81495** and has send his findings to the SQLite developers. Frank
81496** writes that some Linux kernels offer floating point hardware
81497** emulation that uses only 32-bit mantissas instead of a full
81498** 48-bits as required by the IEEE standard. (This is the
81499** CONFIG_FPE_FASTFPE option.) On such systems, floating point
81500** byte swapping becomes very complicated. To avoid problems,
81501** the necessary byte swapping is carried out using a 64-bit integer
81502** rather than a 64-bit float. Frank assures us that the code here
81503** works for him. We, the developers, have no way to independently
81504** verify this, but Frank seems to know what he is talking about
81505** so we trust him.
81506*/
81507#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
81508static u64 floatSwap(u64 in){
81509 union {
81510 u64 r;
81511 u32 i[2];
81512 } u;
81513 u32 t;
81514
81515 u.r = in;
81516 t = u.i[0];
81517 u.i[0] = u.i[1];
81518 u.i[1] = t;
81519 return u.r;
81520}
81521# define swapMixedEndianFloat(X) X = floatSwap(X)
81522#else
81523# define swapMixedEndianFloat(X)
81524#endif
81525
81526/*
81527** Write the serialized data blob for the value stored in pMem into
81528** buf. It is assumed that the caller has allocated sufficient space.
81529** Return the number of bytes written.
81530**
81531** nBuf is the amount of space left in buf[]. The caller is responsible
81532** for allocating enough space to buf[] to hold the entire field, exclusive
81533** of the pMem->u.nZero bytes for a MEM_Zero value.
81534**
81535** Return the number of bytes actually written into buf[]. The number
81536** of bytes in the zero-filled tail is included in the return value only
81537** if those bytes were zeroed in buf[].
81538*/
81539SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
81540 u32 len;
81541
81542 /* Integer and Real */
81543 if( serial_type<=7 && serial_type>0 ){
81544 u64 v;
81545 u32 i;
81546 if( serial_type==7 ){
81547 assert( sizeof(v)==sizeof(pMem->u.r) );
81548 memcpy(&v, &pMem->u.r, sizeof(v));
81549 swapMixedEndianFloat(v);
81550 }else{
81551 v = pMem->u.i;
81552 }
81553 len = i = sqlite3SmallTypeSizes[serial_type];
81554 assert( i>0 );
81555 do{
81556 buf[--i] = (u8)(v&0xFF);
81557 v >>= 8;
81558 }while( i );
81559 return len;
81560 }
81561
81562 /* String or blob */
81563 if( serial_type>=12 ){
81564 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
81565 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
81566 len = pMem->n;
81567 if( len>0 ) memcpy(buf, pMem->z, len);
81568 return len;
81569 }
81570
81571 /* NULL or constants 0 or 1 */
81572 return 0;
81573}
81574
81575/* Input "x" is a sequence of unsigned characters that represent a
81576** big-endian integer. Return the equivalent native integer
81577*/
81578#define ONE_BYTE_INT(x) ((i8)(x)[0])
81579#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
81580#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
81581#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
81582#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
81583
81584/*
81585** Deserialize the data blob pointed to by buf as serial type serial_type
81586** and store the result in pMem. Return the number of bytes read.
81587**
81588** This function is implemented as two separate routines for performance.
81589** The few cases that require local variables are broken out into a separate
81590** routine so that in most cases the overhead of moving the stack pointer
81591** is avoided.
81592*/
81593static u32 serialGet(
81594 const unsigned char *buf, /* Buffer to deserialize from */
81595 u32 serial_type, /* Serial type to deserialize */
81596 Mem *pMem /* Memory cell to write value into */
81597){
81598 u64 x = FOUR_BYTE_UINT(buf);
81599 u32 y = FOUR_BYTE_UINT(buf+4);
81600 x = (x<<32) + y;
81601 if( serial_type==6 ){
81602 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
81603 ** twos-complement integer. */
81604 pMem->u.i = *(i64*)&x;
81605 pMem->flags = MEM_Int;
81606 testcase( pMem->u.i<0 );
81607 }else{
81608 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
81609 ** floating point number. */
81610#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
81611 /* Verify that integers and floating point values use the same
81612 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
81613 ** defined that 64-bit floating point values really are mixed
81614 ** endian.
81615 */
81616 static const u64 t1 = ((u64)0x3ff00000)<<32;
81617 static const double r1 = 1.0;
81618 u64 t2 = t1;
81619 swapMixedEndianFloat(t2);
81620 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
81621#endif
81622 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
81623 swapMixedEndianFloat(x);
81624 memcpy(&pMem->u.r, &x, sizeof(x));
81625 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
81626 }
81627 return 8;
81628}
81629SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
81630 const unsigned char *buf, /* Buffer to deserialize from */
81631 u32 serial_type, /* Serial type to deserialize */
81632 Mem *pMem /* Memory cell to write value into */
81633){
81634 switch( serial_type ){
81635 case 10: { /* Internal use only: NULL with virtual table
81636 ** UPDATE no-change flag set */
81637 pMem->flags = MEM_Null|MEM_Zero;
81638 pMem->n = 0;
81639 pMem->u.nZero = 0;
81640 break;
81641 }
81642 case 11: /* Reserved for future use */
81643 case 0: { /* Null */
81644 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
81645 pMem->flags = MEM_Null;
81646 break;
81647 }
81648 case 1: {
81649 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
81650 ** integer. */
81651 pMem->u.i = ONE_BYTE_INT(buf);
81652 pMem->flags = MEM_Int;
81653 testcase( pMem->u.i<0 );
81654 return 1;
81655 }
81656 case 2: { /* 2-byte signed integer */
81657 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
81658 ** twos-complement integer. */
81659 pMem->u.i = TWO_BYTE_INT(buf);
81660 pMem->flags = MEM_Int;
81661 testcase( pMem->u.i<0 );
81662 return 2;
81663 }
81664 case 3: { /* 3-byte signed integer */
81665 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
81666 ** twos-complement integer. */
81667 pMem->u.i = THREE_BYTE_INT(buf);
81668 pMem->flags = MEM_Int;
81669 testcase( pMem->u.i<0 );
81670 return 3;
81671 }
81672 case 4: { /* 4-byte signed integer */
81673 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
81674 ** twos-complement integer. */
81675 pMem->u.i = FOUR_BYTE_INT(buf);
81676#ifdef __HP_cc
81677 /* Work around a sign-extension bug in the HP compiler for HP/UX */
81678 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
81679#endif
81680 pMem->flags = MEM_Int;
81681 testcase( pMem->u.i<0 );
81682 return 4;
81683 }
81684 case 5: { /* 6-byte signed integer */
81685 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
81686 ** twos-complement integer. */
81687 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
81688 pMem->flags = MEM_Int;
81689 testcase( pMem->u.i<0 );
81690 return 6;
81691 }
81692 case 6: /* 8-byte signed integer */
81693 case 7: { /* IEEE floating point */
81694 /* These use local variables, so do them in a separate routine
81695 ** to avoid having to move the frame pointer in the common case */
81696 return serialGet(buf,serial_type,pMem);
81697 }
81698 case 8: /* Integer 0 */
81699 case 9: { /* Integer 1 */
81700 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
81701 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
81702 pMem->u.i = serial_type-8;
81703 pMem->flags = MEM_Int;
81704 return 0;
81705 }
81706 default: {
81707 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
81708 ** length.
81709 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
81710 ** (N-13)/2 bytes in length. */
81711 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
81712 pMem->z = (char *)buf;
81713 pMem->n = (serial_type-12)/2;
81714 pMem->flags = aFlag[serial_type&1];
81715 return pMem->n;
81716 }
81717 }
81718 return 0;
81719}
81720/*
81721** This routine is used to allocate sufficient space for an UnpackedRecord
81722** structure large enough to be used with sqlite3VdbeRecordUnpack() if
81723** the first argument is a pointer to KeyInfo structure pKeyInfo.
81724**
81725** The space is either allocated using sqlite3DbMallocRaw() or from within
81726** the unaligned buffer passed via the second and third arguments (presumably
81727** stack space). If the former, then *ppFree is set to a pointer that should
81728** be eventually freed by the caller using sqlite3DbFree(). Or, if the
81729** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
81730** before returning.
81731**
81732** If an OOM error occurs, NULL is returned.
81733*/
81734SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
81735 KeyInfo *pKeyInfo /* Description of the record */
81736){
81737 UnpackedRecord *p; /* Unpacked record to return */
81738 int nByte; /* Number of bytes required for *p */
81739 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
81740 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
81741 if( !p ) return 0;
81742 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
81743 assert( pKeyInfo->aSortFlags!=0 );
81744 p->pKeyInfo = pKeyInfo;
81745 p->nField = pKeyInfo->nKeyField + 1;
81746 return p;
81747}
81748
81749/*
81750** Given the nKey-byte encoding of a record in pKey[], populate the
81751** UnpackedRecord structure indicated by the fourth argument with the
81752** contents of the decoded record.
81753*/
81754SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
81755 KeyInfo *pKeyInfo, /* Information about the record format */
81756 int nKey, /* Size of the binary record */
81757 const void *pKey, /* The binary record */
81758 UnpackedRecord *p /* Populate this structure before returning. */
81759){
81760 const unsigned char *aKey = (const unsigned char *)pKey;
81761 u32 d;
81762 u32 idx; /* Offset in aKey[] to read from */
81763 u16 u; /* Unsigned loop counter */
81764 u32 szHdr;
81765 Mem *pMem = p->aMem;
81766
81767 p->default_rc = 0;
81768 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
81769 idx = getVarint32(aKey, szHdr);
81770 d = szHdr;
81771 u = 0;
81772 while( idx<szHdr && d<=(u32)nKey ){
81773 u32 serial_type;
81774
81775 idx += getVarint32(&aKey[idx], serial_type);
81776 pMem->enc = pKeyInfo->enc;
81777 pMem->db = pKeyInfo->db;
81778 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
81779 pMem->szMalloc = 0;
81780 pMem->z = 0;
81781 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
81782 pMem++;
81783 if( (++u)>=p->nField ) break;
81784 }
81785 if( d>(u32)nKey && u ){
81786 assert( CORRUPT_DB );
81787 /* In a corrupt record entry, the last pMem might have been set up using
81788 ** uninitialized memory. Overwrite its value with NULL, to prevent
81789 ** warnings from MSAN. */
81790 sqlite3VdbeMemSetNull(pMem-1);
81791 }
81792 assert( u<=pKeyInfo->nKeyField + 1 );
81793 p->nField = u;
81794}
81795
81796#ifdef SQLITE_DEBUG
81797/*
81798** This function compares two index or table record keys in the same way
81799** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
81800** this function deserializes and compares values using the
81801** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
81802** in assert() statements to ensure that the optimized code in
81803** sqlite3VdbeRecordCompare() returns results with these two primitives.
81804**
81805** Return true if the result of comparison is equivalent to desiredResult.
81806** Return false if there is a disagreement.
81807*/
81808static int vdbeRecordCompareDebug(
81809 int nKey1, const void *pKey1, /* Left key */
81810 const UnpackedRecord *pPKey2, /* Right key */
81811 int desiredResult /* Correct answer */
81812){
81813 u32 d1; /* Offset into aKey[] of next data element */
81814 u32 idx1; /* Offset into aKey[] of next header element */
81815 u32 szHdr1; /* Number of bytes in header */
81816 int i = 0;
81817 int rc = 0;
81818 const unsigned char *aKey1 = (const unsigned char *)pKey1;
81819 KeyInfo *pKeyInfo;
81820 Mem mem1;
81821
81822 pKeyInfo = pPKey2->pKeyInfo;
81823 if( pKeyInfo->db==0 ) return 1;
81824 mem1.enc = pKeyInfo->enc;
81825 mem1.db = pKeyInfo->db;
81826 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
81827 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
81828
81829 /* Compilers may complain that mem1.u.i is potentially uninitialized.
81830 ** We could initialize it, as shown here, to silence those complaints.
81831 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
81832 ** the unnecessary initialization has a measurable negative performance
81833 ** impact, since this routine is a very high runner. And so, we choose
81834 ** to ignore the compiler warnings and leave this variable uninitialized.
81835 */
81836 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
81837
81838 idx1 = getVarint32(aKey1, szHdr1);
81839 if( szHdr1>98307 ) return SQLITE_CORRUPT;
81840 d1 = szHdr1;
81841 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
81842 assert( pKeyInfo->aSortFlags!=0 );
81843 assert( pKeyInfo->nKeyField>0 );
81844 assert( idx1<=szHdr1 || CORRUPT_DB );
81845 do{
81846 u32 serial_type1;
81847
81848 /* Read the serial types for the next element in each key. */
81849 idx1 += getVarint32( aKey1+idx1, serial_type1 );
81850
81851 /* Verify that there is enough key space remaining to avoid
81852 ** a buffer overread. The "d1+serial_type1+2" subexpression will
81853 ** always be greater than or equal to the amount of required key space.
81854 ** Use that approximation to avoid the more expensive call to
81855 ** sqlite3VdbeSerialTypeLen() in the common case.
81856 */
81857 if( d1+(u64)serial_type1+2>(u64)nKey1
81858 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
81859 ){
81860 break;
81861 }
81862
81863 /* Extract the values to be compared.
81864 */
81865 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
81866
81867 /* Do the comparison
81868 */
81869 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
81870 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
81871 if( rc!=0 ){
81872 assert( mem1.szMalloc==0 ); /* See comment below */
81873 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
81874 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
81875 ){
81876 rc = -rc;
81877 }
81878 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
81879 rc = -rc; /* Invert the result for DESC sort order. */
81880 }
81881 goto debugCompareEnd;
81882 }
81883 i++;
81884 }while( idx1<szHdr1 && i<pPKey2->nField );
81885
81886 /* No memory allocation is ever used on mem1. Prove this using
81887 ** the following assert(). If the assert() fails, it indicates a
81888 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
81889 */
81890 assert( mem1.szMalloc==0 );
81891
81892 /* rc==0 here means that one of the keys ran out of fields and
81893 ** all the fields up to that point were equal. Return the default_rc
81894 ** value. */
81895 rc = pPKey2->default_rc;
81896
81897debugCompareEnd:
81898 if( desiredResult==0 && rc==0 ) return 1;
81899 if( desiredResult<0 && rc<0 ) return 1;
81900 if( desiredResult>0 && rc>0 ) return 1;
81901 if( CORRUPT_DB ) return 1;
81902 if( pKeyInfo->db->mallocFailed ) return 1;
81903 return 0;
81904}
81905#endif
81906
81907#ifdef SQLITE_DEBUG
81908/*
81909** Count the number of fields (a.k.a. columns) in the record given by
81910** pKey,nKey. The verify that this count is less than or equal to the
81911** limit given by pKeyInfo->nAllField.
81912**
81913** If this constraint is not satisfied, it means that the high-speed
81914** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
81915** not work correctly. If this assert() ever fires, it probably means
81916** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
81917** incorrectly.
81918*/
81919static void vdbeAssertFieldCountWithinLimits(
81920 int nKey, const void *pKey, /* The record to verify */
81921 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
81922){
81923 int nField = 0;
81924 u32 szHdr;
81925 u32 idx;
81926 u32 notUsed;
81927 const unsigned char *aKey = (const unsigned char*)pKey;
81928
81929 if( CORRUPT_DB ) return;
81930 idx = getVarint32(aKey, szHdr);
81931 assert( nKey>=0 );
81932 assert( szHdr<=(u32)nKey );
81933 while( idx<szHdr ){
81934 idx += getVarint32(aKey+idx, notUsed);
81935 nField++;
81936 }
81937 assert( nField <= pKeyInfo->nAllField );
81938}
81939#else
81940# define vdbeAssertFieldCountWithinLimits(A,B,C)
81941#endif
81942
81943/*
81944** Both *pMem1 and *pMem2 contain string values. Compare the two values
81945** using the collation sequence pColl. As usual, return a negative , zero
81946** or positive value if *pMem1 is less than, equal to or greater than
81947** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
81948*/
81949static int vdbeCompareMemString(
81950 const Mem *pMem1,
81951 const Mem *pMem2,
81952 const CollSeq *pColl,
81953 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
81954){
81955 if( pMem1->enc==pColl->enc ){
81956 /* The strings are already in the correct encoding. Call the
81957 ** comparison function directly */
81958 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
81959 }else{
81960 int rc;
81961 const void *v1, *v2;
81962 Mem c1;
81963 Mem c2;
81964 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
81965 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
81966 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
81967 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
81968 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
81969 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
81970 if( (v1==0 || v2==0) ){
81971 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
81972 rc = 0;
81973 }else{
81974 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
81975 }
81976 sqlite3VdbeMemRelease(&c1);
81977 sqlite3VdbeMemRelease(&c2);
81978 return rc;
81979 }
81980}
81981
81982/*
81983** The input pBlob is guaranteed to be a Blob that is not marked
81984** with MEM_Zero. Return true if it could be a zero-blob.
81985*/
81986static int isAllZero(const char *z, int n){
81987 int i;
81988 for(i=0; i<n; i++){
81989 if( z[i] ) return 0;
81990 }
81991 return 1;
81992}
81993
81994/*
81995** Compare two blobs. Return negative, zero, or positive if the first
81996** is less than, equal to, or greater than the second, respectively.
81997** If one blob is a prefix of the other, then the shorter is the lessor.
81998*/
81999SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
82000 int c;
82001 int n1 = pB1->n;
82002 int n2 = pB2->n;
82003
82004 /* It is possible to have a Blob value that has some non-zero content
82005 ** followed by zero content. But that only comes up for Blobs formed
82006 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
82007 ** sqlite3MemCompare(). */
82008 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
82009 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
82010
82011 if( (pB1->flags|pB2->flags) & MEM_Zero ){
82012 if( pB1->flags & pB2->flags & MEM_Zero ){
82013 return pB1->u.nZero - pB2->u.nZero;
82014 }else if( pB1->flags & MEM_Zero ){
82015 if( !isAllZero(pB2->z, pB2->n) ) return -1;
82016 return pB1->u.nZero - n2;
82017 }else{
82018 if( !isAllZero(pB1->z, pB1->n) ) return +1;
82019 return n1 - pB2->u.nZero;
82020 }
82021 }
82022 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
82023 if( c ) return c;
82024 return n1 - n2;
82025}
82026
82027/*
82028** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
82029** number. Return negative, zero, or positive if the first (i64) is less than,
82030** equal to, or greater than the second (double).
82031*/
82032static int sqlite3IntFloatCompare(i64 i, double r){
82033 if( sizeof(LONGDOUBLE_TYPE)>8 ){
82034 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
82035 if( x<r ) return -1;
82036 if( x>r ) return +1;
82037 return 0;
82038 }else{
82039 i64 y;
82040 double s;
82041 if( r<-9223372036854775808.0 ) return +1;
82042 if( r>=9223372036854775808.0 ) return -1;
82043 y = (i64)r;
82044 if( i<y ) return -1;
82045 if( i>y ) return +1;
82046 s = (double)i;
82047 if( s<r ) return -1;
82048 if( s>r ) return +1;
82049 return 0;
82050 }
82051}
82052
82053/*
82054** Compare the values contained by the two memory cells, returning
82055** negative, zero or positive if pMem1 is less than, equal to, or greater
82056** than pMem2. Sorting order is NULL's first, followed by numbers (integers
82057** and reals) sorted numerically, followed by text ordered by the collating
82058** sequence pColl and finally blob's ordered by memcmp().
82059**
82060** Two NULL values are considered equal by this function.
82061*/
82062SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
82063 int f1, f2;
82064 int combined_flags;
82065
82066 f1 = pMem1->flags;
82067 f2 = pMem2->flags;
82068 combined_flags = f1|f2;
82069 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
82070
82071 /* If one value is NULL, it is less than the other. If both values
82072 ** are NULL, return 0.
82073 */
82074 if( combined_flags&MEM_Null ){
82075 return (f2&MEM_Null) - (f1&MEM_Null);
82076 }
82077
82078 /* At least one of the two values is a number
82079 */
82080 if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
82081 testcase( combined_flags & MEM_Int );
82082 testcase( combined_flags & MEM_Real );
82083 testcase( combined_flags & MEM_IntReal );
82084 if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
82085 testcase( f1 & f2 & MEM_Int );
82086 testcase( f1 & f2 & MEM_IntReal );
82087 if( pMem1->u.i < pMem2->u.i ) return -1;
82088 if( pMem1->u.i > pMem2->u.i ) return +1;
82089 return 0;
82090 }
82091 if( (f1 & f2 & MEM_Real)!=0 ){
82092 if( pMem1->u.r < pMem2->u.r ) return -1;
82093 if( pMem1->u.r > pMem2->u.r ) return +1;
82094 return 0;
82095 }
82096 if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
82097 testcase( f1 & MEM_Int );
82098 testcase( f1 & MEM_IntReal );
82099 if( (f2&MEM_Real)!=0 ){
82100 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
82101 }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
82102 if( pMem1->u.i < pMem2->u.i ) return -1;
82103 if( pMem1->u.i > pMem2->u.i ) return +1;
82104 return 0;
82105 }else{
82106 return -1;
82107 }
82108 }
82109 if( (f1&MEM_Real)!=0 ){
82110 if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
82111 testcase( f2 & MEM_Int );
82112 testcase( f2 & MEM_IntReal );
82113 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
82114 }else{
82115 return -1;
82116 }
82117 }
82118 return +1;
82119 }
82120
82121 /* If one value is a string and the other is a blob, the string is less.
82122 ** If both are strings, compare using the collating functions.
82123 */
82124 if( combined_flags&MEM_Str ){
82125 if( (f1 & MEM_Str)==0 ){
82126 return 1;
82127 }
82128 if( (f2 & MEM_Str)==0 ){
82129 return -1;
82130 }
82131
82132 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
82133 assert( pMem1->enc==SQLITE_UTF8 ||
82134 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
82135
82136 /* The collation sequence must be defined at this point, even if
82137 ** the user deletes the collation sequence after the vdbe program is
82138 ** compiled (this was not always the case).
82139 */
82140 assert( !pColl || pColl->xCmp );
82141
82142 if( pColl ){
82143 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
82144 }
82145 /* If a NULL pointer was passed as the collate function, fall through
82146 ** to the blob case and use memcmp(). */
82147 }
82148
82149 /* Both values must be blobs. Compare using memcmp(). */
82150 return sqlite3BlobCompare(pMem1, pMem2);
82151}
82152
82153
82154/*
82155** The first argument passed to this function is a serial-type that
82156** corresponds to an integer - all values between 1 and 9 inclusive
82157** except 7. The second points to a buffer containing an integer value
82158** serialized according to serial_type. This function deserializes
82159** and returns the value.
82160*/
82161static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
82162 u32 y;
82163 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
82164 switch( serial_type ){
82165 case 0:
82166 case 1:
82167 testcase( aKey[0]&0x80 );
82168 return ONE_BYTE_INT(aKey);
82169 case 2:
82170 testcase( aKey[0]&0x80 );
82171 return TWO_BYTE_INT(aKey);
82172 case 3:
82173 testcase( aKey[0]&0x80 );
82174 return THREE_BYTE_INT(aKey);
82175 case 4: {
82176 testcase( aKey[0]&0x80 );
82177 y = FOUR_BYTE_UINT(aKey);
82178 return (i64)*(int*)&y;
82179 }
82180 case 5: {
82181 testcase( aKey[0]&0x80 );
82182 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
82183 }
82184 case 6: {
82185 u64 x = FOUR_BYTE_UINT(aKey);
82186 testcase( aKey[0]&0x80 );
82187 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
82188 return (i64)*(i64*)&x;
82189 }
82190 }
82191
82192 return (serial_type - 8);
82193}
82194
82195/*
82196** This function compares the two table rows or index records
82197** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
82198** or positive integer if key1 is less than, equal to or
82199** greater than key2. The {nKey1, pKey1} key must be a blob
82200** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
82201** key must be a parsed key such as obtained from
82202** sqlite3VdbeParseRecord.
82203**
82204** If argument bSkip is non-zero, it is assumed that the caller has already
82205** determined that the first fields of the keys are equal.
82206**
82207** Key1 and Key2 do not have to contain the same number of fields. If all
82208** fields that appear in both keys are equal, then pPKey2->default_rc is
82209** returned.
82210**
82211** If database corruption is discovered, set pPKey2->errCode to
82212** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
82213** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
82214** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
82215*/
82216SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
82217 int nKey1, const void *pKey1, /* Left key */
82218 UnpackedRecord *pPKey2, /* Right key */
82219 int bSkip /* If true, skip the first field */
82220){
82221 u32 d1; /* Offset into aKey[] of next data element */
82222 int i; /* Index of next field to compare */
82223 u32 szHdr1; /* Size of record header in bytes */
82224 u32 idx1; /* Offset of first type in header */
82225 int rc = 0; /* Return value */
82226 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
82227 KeyInfo *pKeyInfo;
82228 const unsigned char *aKey1 = (const unsigned char *)pKey1;
82229 Mem mem1;
82230
82231 /* If bSkip is true, then the caller has already determined that the first
82232 ** two elements in the keys are equal. Fix the various stack variables so
82233 ** that this routine begins comparing at the second field. */
82234 if( bSkip ){
82235 u32 s1;
82236 idx1 = 1 + getVarint32(&aKey1[1], s1);
82237 szHdr1 = aKey1[0];
82238 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
82239 i = 1;
82240 pRhs++;
82241 }else{
82242 idx1 = getVarint32(aKey1, szHdr1);
82243 d1 = szHdr1;
82244 i = 0;
82245 }
82246 if( d1>(unsigned)nKey1 ){
82247 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82248 return 0; /* Corruption */
82249 }
82250
82251 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
82252 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
82253 || CORRUPT_DB );
82254 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
82255 assert( pPKey2->pKeyInfo->nKeyField>0 );
82256 assert( idx1<=szHdr1 || CORRUPT_DB );
82257 do{
82258 u32 serial_type;
82259
82260 /* RHS is an integer */
82261 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
82262 testcase( pRhs->flags & MEM_Int );
82263 testcase( pRhs->flags & MEM_IntReal );
82264 serial_type = aKey1[idx1];
82265 testcase( serial_type==12 );
82266 if( serial_type>=10 ){
82267 rc = +1;
82268 }else if( serial_type==0 ){
82269 rc = -1;
82270 }else if( serial_type==7 ){
82271 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
82272 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
82273 }else{
82274 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
82275 i64 rhs = pRhs->u.i;
82276 if( lhs<rhs ){
82277 rc = -1;
82278 }else if( lhs>rhs ){
82279 rc = +1;
82280 }
82281 }
82282 }
82283
82284 /* RHS is real */
82285 else if( pRhs->flags & MEM_Real ){
82286 serial_type = aKey1[idx1];
82287 if( serial_type>=10 ){
82288 /* Serial types 12 or greater are strings and blobs (greater than
82289 ** numbers). Types 10 and 11 are currently "reserved for future
82290 ** use", so it doesn't really matter what the results of comparing
82291 ** them to numberic values are. */
82292 rc = +1;
82293 }else if( serial_type==0 ){
82294 rc = -1;
82295 }else{
82296 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
82297 if( serial_type==7 ){
82298 if( mem1.u.r<pRhs->u.r ){
82299 rc = -1;
82300 }else if( mem1.u.r>pRhs->u.r ){
82301 rc = +1;
82302 }
82303 }else{
82304 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
82305 }
82306 }
82307 }
82308
82309 /* RHS is a string */
82310 else if( pRhs->flags & MEM_Str ){
82311 getVarint32NR(&aKey1[idx1], serial_type);
82312 testcase( serial_type==12 );
82313 if( serial_type<12 ){
82314 rc = -1;
82315 }else if( !(serial_type & 0x01) ){
82316 rc = +1;
82317 }else{
82318 mem1.n = (serial_type - 12) / 2;
82319 testcase( (d1+mem1.n)==(unsigned)nKey1 );
82320 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
82321 if( (d1+mem1.n) > (unsigned)nKey1
82322 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
82323 ){
82324 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82325 return 0; /* Corruption */
82326 }else if( pKeyInfo->aColl[i] ){
82327 mem1.enc = pKeyInfo->enc;
82328 mem1.db = pKeyInfo->db;
82329 mem1.flags = MEM_Str;
82330 mem1.z = (char*)&aKey1[d1];
82331 rc = vdbeCompareMemString(
82332 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
82333 );
82334 }else{
82335 int nCmp = MIN(mem1.n, pRhs->n);
82336 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
82337 if( rc==0 ) rc = mem1.n - pRhs->n;
82338 }
82339 }
82340 }
82341
82342 /* RHS is a blob */
82343 else if( pRhs->flags & MEM_Blob ){
82344 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
82345 getVarint32NR(&aKey1[idx1], serial_type);
82346 testcase( serial_type==12 );
82347 if( serial_type<12 || (serial_type & 0x01) ){
82348 rc = -1;
82349 }else{
82350 int nStr = (serial_type - 12) / 2;
82351 testcase( (d1+nStr)==(unsigned)nKey1 );
82352 testcase( (d1+nStr+1)==(unsigned)nKey1 );
82353 if( (d1+nStr) > (unsigned)nKey1 ){
82354 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82355 return 0; /* Corruption */
82356 }else if( pRhs->flags & MEM_Zero ){
82357 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
82358 rc = 1;
82359 }else{
82360 rc = nStr - pRhs->u.nZero;
82361 }
82362 }else{
82363 int nCmp = MIN(nStr, pRhs->n);
82364 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
82365 if( rc==0 ) rc = nStr - pRhs->n;
82366 }
82367 }
82368 }
82369
82370 /* RHS is null */
82371 else{
82372 serial_type = aKey1[idx1];
82373 rc = (serial_type!=0);
82374 }
82375
82376 if( rc!=0 ){
82377 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
82378 if( sortFlags ){
82379 if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0
82380 || ((sortFlags & KEYINFO_ORDER_DESC)
82381 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
82382 ){
82383 rc = -rc;
82384 }
82385 }
82386 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
82387 assert( mem1.szMalloc==0 ); /* See comment below */
82388 return rc;
82389 }
82390
82391 i++;
82392 if( i==pPKey2->nField ) break;
82393 pRhs++;
82394 d1 += sqlite3VdbeSerialTypeLen(serial_type);
82395 idx1 += sqlite3VarintLen(serial_type);
82396 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
82397
82398 /* No memory allocation is ever used on mem1. Prove this using
82399 ** the following assert(). If the assert() fails, it indicates a
82400 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
82401 assert( mem1.szMalloc==0 );
82402
82403 /* rc==0 here means that one or both of the keys ran out of fields and
82404 ** all the fields up to that point were equal. Return the default_rc
82405 ** value. */
82406 assert( CORRUPT_DB
82407 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
82408 || pPKey2->pKeyInfo->db->mallocFailed
82409 );
82410 pPKey2->eqSeen = 1;
82411 return pPKey2->default_rc;
82412}
82413SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
82414 int nKey1, const void *pKey1, /* Left key */
82415 UnpackedRecord *pPKey2 /* Right key */
82416){
82417 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
82418}
82419
82420
82421/*
82422** This function is an optimized version of sqlite3VdbeRecordCompare()
82423** that (a) the first field of pPKey2 is an integer, and (b) the
82424** size-of-header varint at the start of (pKey1/nKey1) fits in a single
82425** byte (i.e. is less than 128).
82426**
82427** To avoid concerns about buffer overreads, this routine is only used
82428** on schemas where the maximum valid header size is 63 bytes or less.
82429*/
82430static int vdbeRecordCompareInt(
82431 int nKey1, const void *pKey1, /* Left key */
82432 UnpackedRecord *pPKey2 /* Right key */
82433){
82434 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
82435 int serial_type = ((const u8*)pKey1)[1];
82436 int res;
82437 u32 y;
82438 u64 x;
82439 i64 v;
82440 i64 lhs;
82441
82442 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
82443 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
82444 switch( serial_type ){
82445 case 1: { /* 1-byte signed integer */
82446 lhs = ONE_BYTE_INT(aKey);
82447 testcase( lhs<0 );
82448 break;
82449 }
82450 case 2: { /* 2-byte signed integer */
82451 lhs = TWO_BYTE_INT(aKey);
82452 testcase( lhs<0 );
82453 break;
82454 }
82455 case 3: { /* 3-byte signed integer */
82456 lhs = THREE_BYTE_INT(aKey);
82457 testcase( lhs<0 );
82458 break;
82459 }
82460 case 4: { /* 4-byte signed integer */
82461 y = FOUR_BYTE_UINT(aKey);
82462 lhs = (i64)*(int*)&y;
82463 testcase( lhs<0 );
82464 break;
82465 }
82466 case 5: { /* 6-byte signed integer */
82467 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
82468 testcase( lhs<0 );
82469 break;
82470 }
82471 case 6: { /* 8-byte signed integer */
82472 x = FOUR_BYTE_UINT(aKey);
82473 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
82474 lhs = *(i64*)&x;
82475 testcase( lhs<0 );
82476 break;
82477 }
82478 case 8:
82479 lhs = 0;
82480 break;
82481 case 9:
82482 lhs = 1;
82483 break;
82484
82485 /* This case could be removed without changing the results of running
82486 ** this code. Including it causes gcc to generate a faster switch
82487 ** statement (since the range of switch targets now starts at zero and
82488 ** is contiguous) but does not cause any duplicate code to be generated
82489 ** (as gcc is clever enough to combine the two like cases). Other
82490 ** compilers might be similar. */
82491 case 0: case 7:
82492 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
82493
82494 default:
82495 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
82496 }
82497
82498 v = pPKey2->aMem[0].u.i;
82499 if( v>lhs ){
82500 res = pPKey2->r1;
82501 }else if( v<lhs ){
82502 res = pPKey2->r2;
82503 }else if( pPKey2->nField>1 ){
82504 /* The first fields of the two keys are equal. Compare the trailing
82505 ** fields. */
82506 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
82507 }else{
82508 /* The first fields of the two keys are equal and there are no trailing
82509 ** fields. Return pPKey2->default_rc in this case. */
82510 res = pPKey2->default_rc;
82511 pPKey2->eqSeen = 1;
82512 }
82513
82514 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
82515 return res;
82516}
82517
82518/*
82519** This function is an optimized version of sqlite3VdbeRecordCompare()
82520** that (a) the first field of pPKey2 is a string, that (b) the first field
82521** uses the collation sequence BINARY and (c) that the size-of-header varint
82522** at the start of (pKey1/nKey1) fits in a single byte.
82523*/
82524static int vdbeRecordCompareString(
82525 int nKey1, const void *pKey1, /* Left key */
82526 UnpackedRecord *pPKey2 /* Right key */
82527){
82528 const u8 *aKey1 = (const u8*)pKey1;
82529 int serial_type;
82530 int res;
82531
82532 assert( pPKey2->aMem[0].flags & MEM_Str );
82533 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
82534 serial_type = (u8)(aKey1[1]);
82535 if( serial_type >= 0x80 ){
82536 sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
82537 }
82538 if( serial_type<12 ){
82539 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
82540 }else if( !(serial_type & 0x01) ){
82541 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
82542 }else{
82543 int nCmp;
82544 int nStr;
82545 int szHdr = aKey1[0];
82546
82547 nStr = (serial_type-12) / 2;
82548 if( (szHdr + nStr) > nKey1 ){
82549 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82550 return 0; /* Corruption */
82551 }
82552 nCmp = MIN( pPKey2->aMem[0].n, nStr );
82553 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
82554
82555 if( res>0 ){
82556 res = pPKey2->r2;
82557 }else if( res<0 ){
82558 res = pPKey2->r1;
82559 }else{
82560 res = nStr - pPKey2->aMem[0].n;
82561 if( res==0 ){
82562 if( pPKey2->nField>1 ){
82563 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
82564 }else{
82565 res = pPKey2->default_rc;
82566 pPKey2->eqSeen = 1;
82567 }
82568 }else if( res>0 ){
82569 res = pPKey2->r2;
82570 }else{
82571 res = pPKey2->r1;
82572 }
82573 }
82574 }
82575
82576 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
82577 || CORRUPT_DB
82578 || pPKey2->pKeyInfo->db->mallocFailed
82579 );
82580 return res;
82581}
82582
82583/*
82584** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
82585** suitable for comparing serialized records to the unpacked record passed
82586** as the only argument.
82587*/
82588SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
82589 /* varintRecordCompareInt() and varintRecordCompareString() both assume
82590 ** that the size-of-header varint that occurs at the start of each record
82591 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
82592 ** also assumes that it is safe to overread a buffer by at least the
82593 ** maximum possible legal header size plus 8 bytes. Because there is
82594 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
82595 ** buffer passed to varintRecordCompareInt() this makes it convenient to
82596 ** limit the size of the header to 64 bytes in cases where the first field
82597 ** is an integer.
82598 **
82599 ** The easiest way to enforce this limit is to consider only records with
82600 ** 13 fields or less. If the first field is an integer, the maximum legal
82601 ** header size is (12*5 + 1 + 1) bytes. */
82602 if( p->pKeyInfo->nAllField<=13 ){
82603 int flags = p->aMem[0].flags;
82604 if( p->pKeyInfo->aSortFlags[0] ){
82605 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
82606 return sqlite3VdbeRecordCompare;
82607 }
82608 p->r1 = 1;
82609 p->r2 = -1;
82610 }else{
82611 p->r1 = -1;
82612 p->r2 = 1;
82613 }
82614 if( (flags & MEM_Int) ){
82615 return vdbeRecordCompareInt;
82616 }
82617 testcase( flags & MEM_Real );
82618 testcase( flags & MEM_Null );
82619 testcase( flags & MEM_Blob );
82620 if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
82621 && p->pKeyInfo->aColl[0]==0
82622 ){
82623 assert( flags & MEM_Str );
82624 return vdbeRecordCompareString;
82625 }
82626 }
82627
82628 return sqlite3VdbeRecordCompare;
82629}
82630
82631/*
82632** pCur points at an index entry created using the OP_MakeRecord opcode.
82633** Read the rowid (the last field in the record) and store it in *rowid.
82634** Return SQLITE_OK if everything works, or an error code otherwise.
82635**
82636** pCur might be pointing to text obtained from a corrupt database file.
82637** So the content cannot be trusted. Do appropriate checks on the content.
82638*/
82639SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
82640 i64 nCellKey = 0;
82641 int rc;
82642 u32 szHdr; /* Size of the header */
82643 u32 typeRowid; /* Serial type of the rowid */
82644 u32 lenRowid; /* Size of the rowid */
82645 Mem m, v;
82646
82647 /* Get the size of the index entry. Only indices entries of less
82648 ** than 2GiB are support - anything large must be database corruption.
82649 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
82650 ** this code can safely assume that nCellKey is 32-bits
82651 */
82652 assert( sqlite3BtreeCursorIsValid(pCur) );
82653 nCellKey = sqlite3BtreePayloadSize(pCur);
82654 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
82655
82656 /* Read in the complete content of the index entry */
82657 sqlite3VdbeMemInit(&m, db, 0);
82658 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
82659 if( rc ){
82660 return rc;
82661 }
82662
82663 /* The index entry must begin with a header size */
82664 getVarint32NR((u8*)m.z, szHdr);
82665 testcase( szHdr==3 );
82666 testcase( szHdr==m.n );
82667 testcase( szHdr>0x7fffffff );
82668 assert( m.n>=0 );
82669 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
82670 goto idx_rowid_corruption;
82671 }
82672
82673 /* The last field of the index should be an integer - the ROWID.
82674 ** Verify that the last entry really is an integer. */
82675 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
82676 testcase( typeRowid==1 );
82677 testcase( typeRowid==2 );
82678 testcase( typeRowid==3 );
82679 testcase( typeRowid==4 );
82680 testcase( typeRowid==5 );
82681 testcase( typeRowid==6 );
82682 testcase( typeRowid==8 );
82683 testcase( typeRowid==9 );
82684 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
82685 goto idx_rowid_corruption;
82686 }
82687 lenRowid = sqlite3SmallTypeSizes[typeRowid];
82688 testcase( (u32)m.n==szHdr+lenRowid );
82689 if( unlikely((u32)m.n<szHdr+lenRowid) ){
82690 goto idx_rowid_corruption;
82691 }
82692
82693 /* Fetch the integer off the end of the index record */
82694 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
82695 *rowid = v.u.i;
82696 sqlite3VdbeMemRelease(&m);
82697 return SQLITE_OK;
82698
82699 /* Jump here if database corruption is detected after m has been
82700 ** allocated. Free the m object and return SQLITE_CORRUPT. */
82701idx_rowid_corruption:
82702 testcase( m.szMalloc!=0 );
82703 sqlite3VdbeMemRelease(&m);
82704 return SQLITE_CORRUPT_BKPT;
82705}
82706
82707/*
82708** Compare the key of the index entry that cursor pC is pointing to against
82709** the key string in pUnpacked. Write into *pRes a number
82710** that is negative, zero, or positive if pC is less than, equal to,
82711** or greater than pUnpacked. Return SQLITE_OK on success.
82712**
82713** pUnpacked is either created without a rowid or is truncated so that it
82714** omits the rowid at the end. The rowid at the end of the index entry
82715** is ignored as well. Hence, this routine only compares the prefixes
82716** of the keys prior to the final rowid, not the entire key.
82717*/
82718SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
82719 sqlite3 *db, /* Database connection */
82720 VdbeCursor *pC, /* The cursor to compare against */
82721 UnpackedRecord *pUnpacked, /* Unpacked version of key */
82722 int *res /* Write the comparison result here */
82723){
82724 i64 nCellKey = 0;
82725 int rc;
82726 BtCursor *pCur;
82727 Mem m;
82728
82729 assert( pC->eCurType==CURTYPE_BTREE );
82730 pCur = pC->uc.pCursor;
82731 assert( sqlite3BtreeCursorIsValid(pCur) );
82732 nCellKey = sqlite3BtreePayloadSize(pCur);
82733 /* nCellKey will always be between 0 and 0xffffffff because of the way
82734 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
82735 if( nCellKey<=0 || nCellKey>0x7fffffff ){
82736 *res = 0;
82737 return SQLITE_CORRUPT_BKPT;
82738 }
82739 sqlite3VdbeMemInit(&m, db, 0);
82740 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
82741 if( rc ){
82742 return rc;
82743 }
82744 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
82745 sqlite3VdbeMemRelease(&m);
82746 return SQLITE_OK;
82747}
82748
82749/*
82750** This routine sets the value to be returned by subsequent calls to
82751** sqlite3_changes() on the database handle 'db'.
82752*/
82753SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
82754 assert( sqlite3_mutex_held(db->mutex) );
82755 db->nChange = nChange;
82756 db->nTotalChange += nChange;
82757}
82758
82759/*
82760** Set a flag in the vdbe to update the change counter when it is finalised
82761** or reset.
82762*/
82763SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
82764 v->changeCntOn = 1;
82765}
82766
82767/*
82768** Mark every prepared statement associated with a database connection
82769** as expired.
82770**
82771** An expired statement means that recompilation of the statement is
82772** recommend. Statements expire when things happen that make their
82773** programs obsolete. Removing user-defined functions or collating
82774** sequences, or changing an authorization function are the types of
82775** things that make prepared statements obsolete.
82776**
82777** If iCode is 1, then expiration is advisory. The statement should
82778** be reprepared before being restarted, but if it is already running
82779** it is allowed to run to completion.
82780**
82781** Internally, this function just sets the Vdbe.expired flag on all
82782** prepared statements. The flag is set to 1 for an immediate expiration
82783** and set to 2 for an advisory expiration.
82784*/
82785SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
82786 Vdbe *p;
82787 for(p = db->pVdbe; p; p=p->pNext){
82788 p->expired = iCode+1;
82789 }
82790}
82791
82792/*
82793** Return the database associated with the Vdbe.
82794*/
82795SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
82796 return v->db;
82797}
82798
82799/*
82800** Return the SQLITE_PREPARE flags for a Vdbe.
82801*/
82802SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
82803 return v->prepFlags;
82804}
82805
82806/*
82807** Return a pointer to an sqlite3_value structure containing the value bound
82808** parameter iVar of VM v. Except, if the value is an SQL NULL, return
82809** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
82810** constants) to the value before returning it.
82811**
82812** The returned value must be freed by the caller using sqlite3ValueFree().
82813*/
82814SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
82815 assert( iVar>0 );
82816 if( v ){
82817 Mem *pMem = &v->aVar[iVar-1];
82818 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
82819 if( 0==(pMem->flags & MEM_Null) ){
82820 sqlite3_value *pRet = sqlite3ValueNew(v->db);
82821 if( pRet ){
82822 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
82823 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
82824 }
82825 return pRet;
82826 }
82827 }
82828 return 0;
82829}
82830
82831/*
82832** Configure SQL variable iVar so that binding a new value to it signals
82833** to sqlite3_reoptimize() that re-preparing the statement may result
82834** in a better query plan.
82835*/
82836SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
82837 assert( iVar>0 );
82838 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
82839 if( iVar>=32 ){
82840 v->expmask |= 0x80000000;
82841 }else{
82842 v->expmask |= ((u32)1 << (iVar-1));
82843 }
82844}
82845
82846/*
82847** Cause a function to throw an error if it was call from OP_PureFunc
82848** rather than OP_Function.
82849**
82850** OP_PureFunc means that the function must be deterministic, and should
82851** throw an error if it is given inputs that would make it non-deterministic.
82852** This routine is invoked by date/time functions that use non-deterministic
82853** features such as 'now'.
82854*/
82855SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
82856 const VdbeOp *pOp;
82857#ifdef SQLITE_ENABLE_STAT4
82858 if( pCtx->pVdbe==0 ) return 1;
82859#endif
82860 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
82861 if( pOp->opcode==OP_PureFunc ){
82862 const char *zContext;
82863 char *zMsg;
82864 if( pOp->p5 & NC_IsCheck ){
82865 zContext = "a CHECK constraint";
82866 }else if( pOp->p5 & NC_GenCol ){
82867 zContext = "a generated column";
82868 }else{
82869 zContext = "an index";
82870 }
82871 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
82872 pCtx->pFunc->zName, zContext);
82873 sqlite3_result_error(pCtx, zMsg, -1);
82874 sqlite3_free(zMsg);
82875 return 0;
82876 }
82877 return 1;
82878}
82879
82880#ifndef SQLITE_OMIT_VIRTUALTABLE
82881/*
82882** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
82883** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
82884** in memory obtained from sqlite3DbMalloc).
82885*/
82886SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
82887 if( pVtab->zErrMsg ){
82888 sqlite3 *db = p->db;
82889 sqlite3DbFree(db, p->zErrMsg);
82890 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
82891 sqlite3_free(pVtab->zErrMsg);
82892 pVtab->zErrMsg = 0;
82893 }
82894}
82895#endif /* SQLITE_OMIT_VIRTUALTABLE */
82896
82897#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82898
82899/*
82900** If the second argument is not NULL, release any allocations associated
82901** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
82902** structure itself, using sqlite3DbFree().
82903**
82904** This function is used to free UnpackedRecord structures allocated by
82905** the vdbeUnpackRecord() function found in vdbeapi.c.
82906*/
82907static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
82908 if( p ){
82909 int i;
82910 for(i=0; i<nField; i++){
82911 Mem *pMem = &p->aMem[i];
82912 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
82913 }
82914 sqlite3DbFreeNN(db, p);
82915 }
82916}
82917#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82918
82919#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82920/*
82921** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
82922** then cursor passed as the second argument should point to the row about
82923** to be update or deleted. If the application calls sqlite3_preupdate_old(),
82924** the required value will be read from the row the cursor points to.
82925*/
82926SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
82927 Vdbe *v, /* Vdbe pre-update hook is invoked by */
82928 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
82929 int op, /* SQLITE_INSERT, UPDATE or DELETE */
82930 const char *zDb, /* Database name */
82931 Table *pTab, /* Modified table */
82932 i64 iKey1, /* Initial key value */
82933 int iReg /* Register for new.* record */
82934){
82935 sqlite3 *db = v->db;
82936 i64 iKey2;
82937 PreUpdate preupdate;
82938 const char *zTbl = pTab->zName;
82939 static const u8 fakeSortOrder = 0;
82940
82941 assert( db->pPreUpdate==0 );
82942 memset(&preupdate, 0, sizeof(PreUpdate));
82943 if( HasRowid(pTab)==0 ){
82944 iKey1 = iKey2 = 0;
82945 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
82946 }else{
82947 if( op==SQLITE_UPDATE ){
82948 iKey2 = v->aMem[iReg].u.i;
82949 }else{
82950 iKey2 = iKey1;
82951 }
82952 }
82953
82954 assert( pCsr->nField==pTab->nCol
82955 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
82956 );
82957
82958 preupdate.v = v;
82959 preupdate.pCsr = pCsr;
82960 preupdate.op = op;
82961 preupdate.iNewReg = iReg;
82962 preupdate.keyinfo.db = db;
82963 preupdate.keyinfo.enc = ENC(db);
82964 preupdate.keyinfo.nKeyField = pTab->nCol;
82965 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
82966 preupdate.iKey1 = iKey1;
82967 preupdate.iKey2 = iKey2;
82968 preupdate.pTab = pTab;
82969
82970 db->pPreUpdate = &preupdate;
82971 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
82972 db->pPreUpdate = 0;
82973 sqlite3DbFree(db, preupdate.aRecord);
82974 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
82975 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
82976 if( preupdate.aNew ){
82977 int i;
82978 for(i=0; i<pCsr->nField; i++){
82979 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
82980 }
82981 sqlite3DbFreeNN(db, preupdate.aNew);
82982 }
82983}
82984#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82985
82986/************** End of vdbeaux.c *********************************************/
82987/************** Begin file vdbeapi.c *****************************************/
82988/*
82989** 2004 May 26
82990**
82991** The author disclaims copyright to this source code. In place of
82992** a legal notice, here is a blessing:
82993**
82994** May you do good and not evil.
82995** May you find forgiveness for yourself and forgive others.
82996** May you share freely, never taking more than you give.
82997**
82998*************************************************************************
82999**
83000** This file contains code use to implement APIs that are part of the
83001** VDBE.
83002*/
83003/* #include "sqliteInt.h" */
83004/* #include "vdbeInt.h" */
83005
83006#ifndef SQLITE_OMIT_DEPRECATED
83007/*
83008** Return TRUE (non-zero) of the statement supplied as an argument needs
83009** to be recompiled. A statement needs to be recompiled whenever the
83010** execution environment changes in a way that would alter the program
83011** that sqlite3_prepare() generates. For example, if new functions or
83012** collating sequences are registered or if an authorizer function is
83013** added or changed.
83014*/
83015SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
83016 Vdbe *p = (Vdbe*)pStmt;
83017 return p==0 || p->expired;
83018}
83019#endif
83020
83021/*
83022** Check on a Vdbe to make sure it has not been finalized. Log
83023** an error and return true if it has been finalized (or is otherwise
83024** invalid). Return false if it is ok.
83025*/
83026static int vdbeSafety(Vdbe *p){
83027 if( p->db==0 ){
83028 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
83029 return 1;
83030 }else{
83031 return 0;
83032 }
83033}
83034static int vdbeSafetyNotNull(Vdbe *p){
83035 if( p==0 ){
83036 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
83037 return 1;
83038 }else{
83039 return vdbeSafety(p);
83040 }
83041}
83042
83043#ifndef SQLITE_OMIT_TRACE
83044/*
83045** Invoke the profile callback. This routine is only called if we already
83046** know that the profile callback is defined and needs to be invoked.
83047*/
83048static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
83049 sqlite3_int64 iNow;
83050 sqlite3_int64 iElapse;
83051 assert( p->startTime>0 );
83052 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
83053 assert( db->init.busy==0 );
83054 assert( p->zSql!=0 );
83055 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
83056 iElapse = (iNow - p->startTime)*1000000;
83057#ifndef SQLITE_OMIT_DEPRECATED
83058 if( db->xProfile ){
83059 db->xProfile(db->pProfileArg, p->zSql, iElapse);
83060 }
83061#endif
83062 if( db->mTrace & SQLITE_TRACE_PROFILE ){
83063 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
83064 }
83065 p->startTime = 0;
83066}
83067/*
83068** The checkProfileCallback(DB,P) macro checks to see if a profile callback
83069** is needed, and it invokes the callback if it is needed.
83070*/
83071# define checkProfileCallback(DB,P) \
83072 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
83073#else
83074# define checkProfileCallback(DB,P) /*no-op*/
83075#endif
83076
83077/*
83078** The following routine destroys a virtual machine that is created by
83079** the sqlite3_compile() routine. The integer returned is an SQLITE_
83080** success/failure code that describes the result of executing the virtual
83081** machine.
83082**
83083** This routine sets the error code and string returned by
83084** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
83085*/
83086SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
83087 int rc;
83088 if( pStmt==0 ){
83089 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
83090 ** pointer is a harmless no-op. */
83091 rc = SQLITE_OK;
83092 }else{
83093 Vdbe *v = (Vdbe*)pStmt;
83094 sqlite3 *db = v->db;
83095 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
83096 sqlite3_mutex_enter(db->mutex);
83097 checkProfileCallback(db, v);
83098 rc = sqlite3VdbeFinalize(v);
83099 rc = sqlite3ApiExit(db, rc);
83100 sqlite3LeaveMutexAndCloseZombie(db);
83101 }
83102 return rc;
83103}
83104
83105/*
83106** Terminate the current execution of an SQL statement and reset it
83107** back to its starting state so that it can be reused. A success code from
83108** the prior execution is returned.
83109**
83110** This routine sets the error code and string returned by
83111** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
83112*/
83113SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
83114 int rc;
83115 if( pStmt==0 ){
83116 rc = SQLITE_OK;
83117 }else{
83118 Vdbe *v = (Vdbe*)pStmt;
83119 sqlite3 *db = v->db;
83120 sqlite3_mutex_enter(db->mutex);
83121 checkProfileCallback(db, v);
83122 rc = sqlite3VdbeReset(v);
83123 sqlite3VdbeRewind(v);
83124 assert( (rc & (db->errMask))==rc );
83125 rc = sqlite3ApiExit(db, rc);
83126 sqlite3_mutex_leave(db->mutex);
83127 }
83128 return rc;
83129}
83130
83131/*
83132** Set all the parameters in the compiled SQL statement to NULL.
83133*/
83134SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
83135 int i;
83136 int rc = SQLITE_OK;
83137 Vdbe *p = (Vdbe*)pStmt;
83138#if SQLITE_THREADSAFE
83139 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
83140#endif
83141 sqlite3_mutex_enter(mutex);
83142 for(i=0; i<p->nVar; i++){
83143 sqlite3VdbeMemRelease(&p->aVar[i]);
83144 p->aVar[i].flags = MEM_Null;
83145 }
83146 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
83147 if( p->expmask ){
83148 p->expired = 1;
83149 }
83150 sqlite3_mutex_leave(mutex);
83151 return rc;
83152}
83153
83154
83155/**************************** sqlite3_value_ *******************************
83156** The following routines extract information from a Mem or sqlite3_value
83157** structure.
83158*/
83159SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
83160 Mem *p = (Mem*)pVal;
83161 if( p->flags & (MEM_Blob|MEM_Str) ){
83162 if( ExpandBlob(p)!=SQLITE_OK ){
83163 assert( p->flags==MEM_Null && p->z==0 );
83164 return 0;
83165 }
83166 p->flags |= MEM_Blob;
83167 return p->n ? p->z : 0;
83168 }else{
83169 return sqlite3_value_text(pVal);
83170 }
83171}
83172SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
83173 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
83174}
83175SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
83176 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
83177}
83178SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
83179 return sqlite3VdbeRealValue((Mem*)pVal);
83180}
83181SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
83182 return (int)sqlite3VdbeIntValue((Mem*)pVal);
83183}
83184SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
83185 return sqlite3VdbeIntValue((Mem*)pVal);
83186}
83187SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
83188 Mem *pMem = (Mem*)pVal;
83189 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
83190}
83191SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
83192 Mem *p = (Mem*)pVal;
83193 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
83194 (MEM_Null|MEM_Term|MEM_Subtype)
83195 && zPType!=0
83196 && p->eSubtype=='p'
83197 && strcmp(p->u.zPType, zPType)==0
83198 ){
83199 return (void*)p->z;
83200 }else{
83201 return 0;
83202 }
83203}
83204SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
83205 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
83206}
83207#ifndef SQLITE_OMIT_UTF16
83208SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
83209 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
83210}
83211SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
83212 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
83213}
83214SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
83215 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
83216}
83217#endif /* SQLITE_OMIT_UTF16 */
83218/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
83219** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
83220** point number string BLOB NULL
83221*/
83222SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
83223 static const u8 aType[] = {
83224 SQLITE_BLOB, /* 0x00 (not possible) */
83225 SQLITE_NULL, /* 0x01 NULL */
83226 SQLITE_TEXT, /* 0x02 TEXT */
83227 SQLITE_NULL, /* 0x03 (not possible) */
83228 SQLITE_INTEGER, /* 0x04 INTEGER */
83229 SQLITE_NULL, /* 0x05 (not possible) */
83230 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */
83231 SQLITE_NULL, /* 0x07 (not possible) */
83232 SQLITE_FLOAT, /* 0x08 FLOAT */
83233 SQLITE_NULL, /* 0x09 (not possible) */
83234 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */
83235 SQLITE_NULL, /* 0x0b (not possible) */
83236 SQLITE_INTEGER, /* 0x0c (not possible) */
83237 SQLITE_NULL, /* 0x0d (not possible) */
83238 SQLITE_INTEGER, /* 0x0e (not possible) */
83239 SQLITE_NULL, /* 0x0f (not possible) */
83240 SQLITE_BLOB, /* 0x10 BLOB */
83241 SQLITE_NULL, /* 0x11 (not possible) */
83242 SQLITE_TEXT, /* 0x12 (not possible) */
83243 SQLITE_NULL, /* 0x13 (not possible) */
83244 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */
83245 SQLITE_NULL, /* 0x15 (not possible) */
83246 SQLITE_INTEGER, /* 0x16 (not possible) */
83247 SQLITE_NULL, /* 0x17 (not possible) */
83248 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */
83249 SQLITE_NULL, /* 0x19 (not possible) */
83250 SQLITE_FLOAT, /* 0x1a (not possible) */
83251 SQLITE_NULL, /* 0x1b (not possible) */
83252 SQLITE_INTEGER, /* 0x1c (not possible) */
83253 SQLITE_NULL, /* 0x1d (not possible) */
83254 SQLITE_INTEGER, /* 0x1e (not possible) */
83255 SQLITE_NULL, /* 0x1f (not possible) */
83256 SQLITE_FLOAT, /* 0x20 INTREAL */
83257 SQLITE_NULL, /* 0x21 (not possible) */
83258 SQLITE_TEXT, /* 0x22 INTREAL + TEXT */
83259 SQLITE_NULL, /* 0x23 (not possible) */
83260 SQLITE_FLOAT, /* 0x24 (not possible) */
83261 SQLITE_NULL, /* 0x25 (not possible) */
83262 SQLITE_FLOAT, /* 0x26 (not possible) */
83263 SQLITE_NULL, /* 0x27 (not possible) */
83264 SQLITE_FLOAT, /* 0x28 (not possible) */
83265 SQLITE_NULL, /* 0x29 (not possible) */
83266 SQLITE_FLOAT, /* 0x2a (not possible) */
83267 SQLITE_NULL, /* 0x2b (not possible) */
83268 SQLITE_FLOAT, /* 0x2c (not possible) */
83269 SQLITE_NULL, /* 0x2d (not possible) */
83270 SQLITE_FLOAT, /* 0x2e (not possible) */
83271 SQLITE_NULL, /* 0x2f (not possible) */
83272 SQLITE_BLOB, /* 0x30 (not possible) */
83273 SQLITE_NULL, /* 0x31 (not possible) */
83274 SQLITE_TEXT, /* 0x32 (not possible) */
83275 SQLITE_NULL, /* 0x33 (not possible) */
83276 SQLITE_FLOAT, /* 0x34 (not possible) */
83277 SQLITE_NULL, /* 0x35 (not possible) */
83278 SQLITE_FLOAT, /* 0x36 (not possible) */
83279 SQLITE_NULL, /* 0x37 (not possible) */
83280 SQLITE_FLOAT, /* 0x38 (not possible) */
83281 SQLITE_NULL, /* 0x39 (not possible) */
83282 SQLITE_FLOAT, /* 0x3a (not possible) */
83283 SQLITE_NULL, /* 0x3b (not possible) */
83284 SQLITE_FLOAT, /* 0x3c (not possible) */
83285 SQLITE_NULL, /* 0x3d (not possible) */
83286 SQLITE_FLOAT, /* 0x3e (not possible) */
83287 SQLITE_NULL, /* 0x3f (not possible) */
83288 };
83289#ifdef SQLITE_DEBUG
83290 {
83291 int eType = SQLITE_BLOB;
83292 if( pVal->flags & MEM_Null ){
83293 eType = SQLITE_NULL;
83294 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
83295 eType = SQLITE_FLOAT;
83296 }else if( pVal->flags & MEM_Int ){
83297 eType = SQLITE_INTEGER;
83298 }else if( pVal->flags & MEM_Str ){
83299 eType = SQLITE_TEXT;
83300 }
83301 assert( eType == aType[pVal->flags&MEM_AffMask] );
83302 }
83303#endif
83304 return aType[pVal->flags&MEM_AffMask];
83305}
83306
83307/* Return true if a parameter to xUpdate represents an unchanged column */
83308SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
83309 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
83310}
83311
83312/* Return true if a parameter value originated from an sqlite3_bind() */
83313SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
83314 return (pVal->flags&MEM_FromBind)!=0;
83315}
83316
83317/* Make a copy of an sqlite3_value object
83318*/
83319SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
83320 sqlite3_value *pNew;
83321 if( pOrig==0 ) return 0;
83322 pNew = sqlite3_malloc( sizeof(*pNew) );
83323 if( pNew==0 ) return 0;
83324 memset(pNew, 0, sizeof(*pNew));
83325 memcpy(pNew, pOrig, MEMCELLSIZE);
83326 pNew->flags &= ~MEM_Dyn;
83327 pNew->db = 0;
83328 if( pNew->flags&(MEM_Str|MEM_Blob) ){
83329 pNew->flags &= ~(MEM_Static|MEM_Dyn);
83330 pNew->flags |= MEM_Ephem;
83331 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
83332 sqlite3ValueFree(pNew);
83333 pNew = 0;
83334 }
83335 }
83336 return pNew;
83337}
83338
83339/* Destroy an sqlite3_value object previously obtained from
83340** sqlite3_value_dup().
83341*/
83342SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
83343 sqlite3ValueFree(pOld);
83344}
83345
83346
83347/**************************** sqlite3_result_ *******************************
83348** The following routines are used by user-defined functions to specify
83349** the function result.
83350**
83351** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
83352** result as a string or blob but if the string or blob is too large, it
83353** then sets the error code to SQLITE_TOOBIG
83354**
83355** The invokeValueDestructor(P,X) routine invokes destructor function X()
83356** on value P is not going to be used and need to be destroyed.
83357*/
83358static void setResultStrOrError(
83359 sqlite3_context *pCtx, /* Function context */
83360 const char *z, /* String pointer */
83361 int n, /* Bytes in string, or negative */
83362 u8 enc, /* Encoding of z. 0 for BLOBs */
83363 void (*xDel)(void*) /* Destructor function */
83364){
83365 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
83366 sqlite3_result_error_toobig(pCtx);
83367 }
83368}
83369static int invokeValueDestructor(
83370 const void *p, /* Value to destroy */
83371 void (*xDel)(void*), /* The destructor */
83372 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
83373){
83374 assert( xDel!=SQLITE_DYNAMIC );
83375 if( xDel==0 ){
83376 /* noop */
83377 }else if( xDel==SQLITE_TRANSIENT ){
83378 /* noop */
83379 }else{
83380 xDel((void*)p);
83381 }
83382 if( pCtx ) sqlite3_result_error_toobig(pCtx);
83383 return SQLITE_TOOBIG;
83384}
83385SQLITE_API void sqlite3_result_blob(
83386 sqlite3_context *pCtx,
83387 const void *z,
83388 int n,
83389 void (*xDel)(void *)
83390){
83391 assert( n>=0 );
83392 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83393 setResultStrOrError(pCtx, z, n, 0, xDel);
83394}
83395SQLITE_API void sqlite3_result_blob64(
83396 sqlite3_context *pCtx,
83397 const void *z,
83398 sqlite3_uint64 n,
83399 void (*xDel)(void *)
83400){
83401 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83402 assert( xDel!=SQLITE_DYNAMIC );
83403 if( n>0x7fffffff ){
83404 (void)invokeValueDestructor(z, xDel, pCtx);
83405 }else{
83406 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
83407 }
83408}
83409SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
83410 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83411 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
83412}
83413SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
83414 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83415 pCtx->isError = SQLITE_ERROR;
83416 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
83417}
83418#ifndef SQLITE_OMIT_UTF16
83419SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
83420 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83421 pCtx->isError = SQLITE_ERROR;
83422 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
83423}
83424#endif
83425SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
83426 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83427 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
83428}
83429SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
83430 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83431 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
83432}
83433SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
83434 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83435 sqlite3VdbeMemSetNull(pCtx->pOut);
83436}
83437SQLITE_API void sqlite3_result_pointer(
83438 sqlite3_context *pCtx,
83439 void *pPtr,
83440 const char *zPType,
83441 void (*xDestructor)(void*)
83442){
83443 Mem *pOut = pCtx->pOut;
83444 assert( sqlite3_mutex_held(pOut->db->mutex) );
83445 sqlite3VdbeMemRelease(pOut);
83446 pOut->flags = MEM_Null;
83447 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
83448}
83449SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
83450 Mem *pOut = pCtx->pOut;
83451 assert( sqlite3_mutex_held(pOut->db->mutex) );
83452 pOut->eSubtype = eSubtype & 0xff;
83453 pOut->flags |= MEM_Subtype;
83454}
83455SQLITE_API void sqlite3_result_text(
83456 sqlite3_context *pCtx,
83457 const char *z,
83458 int n,
83459 void (*xDel)(void *)
83460){
83461 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83462 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
83463}
83464SQLITE_API void sqlite3_result_text64(
83465 sqlite3_context *pCtx,
83466 const char *z,
83467 sqlite3_uint64 n,
83468 void (*xDel)(void *),
83469 unsigned char enc
83470){
83471 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83472 assert( xDel!=SQLITE_DYNAMIC );
83473 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
83474 if( n>0x7fffffff ){
83475 (void)invokeValueDestructor(z, xDel, pCtx);
83476 }else{
83477 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
83478 }
83479}
83480#ifndef SQLITE_OMIT_UTF16
83481SQLITE_API void sqlite3_result_text16(
83482 sqlite3_context *pCtx,
83483 const void *z,
83484 int n,
83485 void (*xDel)(void *)
83486){
83487 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83488 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
83489}
83490SQLITE_API void sqlite3_result_text16be(
83491 sqlite3_context *pCtx,
83492 const void *z,
83493 int n,
83494 void (*xDel)(void *)
83495){
83496 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83497 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
83498}
83499SQLITE_API void sqlite3_result_text16le(
83500 sqlite3_context *pCtx,
83501 const void *z,
83502 int n,
83503 void (*xDel)(void *)
83504){
83505 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83506 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
83507}
83508#endif /* SQLITE_OMIT_UTF16 */
83509SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
83510 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83511 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
83512}
83513SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
83514 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83515 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
83516}
83517SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
83518 Mem *pOut = pCtx->pOut;
83519 assert( sqlite3_mutex_held(pOut->db->mutex) );
83520 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
83521 return SQLITE_TOOBIG;
83522 }
83523 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
83524 return SQLITE_OK;
83525}
83526SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
83527 pCtx->isError = errCode ? errCode : -1;
83528#ifdef SQLITE_DEBUG
83529 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
83530#endif
83531 if( pCtx->pOut->flags & MEM_Null ){
83532 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
83533 SQLITE_UTF8, SQLITE_STATIC);
83534 }
83535}
83536
83537/* Force an SQLITE_TOOBIG error. */
83538SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
83539 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83540 pCtx->isError = SQLITE_TOOBIG;
83541 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
83542 SQLITE_UTF8, SQLITE_STATIC);
83543}
83544
83545/* An SQLITE_NOMEM error. */
83546SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
83547 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83548 sqlite3VdbeMemSetNull(pCtx->pOut);
83549 pCtx->isError = SQLITE_NOMEM_BKPT;
83550 sqlite3OomFault(pCtx->pOut->db);
83551}
83552
83553#ifndef SQLITE_UNTESTABLE
83554/* Force the INT64 value currently stored as the result to be
83555** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
83556** test-control.
83557*/
83558SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context *pCtx){
83559 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83560 if( pCtx->pOut->flags & MEM_Int ){
83561 pCtx->pOut->flags &= ~MEM_Int;
83562 pCtx->pOut->flags |= MEM_IntReal;
83563 }
83564}
83565#endif
83566
83567
83568/*
83569** This function is called after a transaction has been committed. It
83570** invokes callbacks registered with sqlite3_wal_hook() as required.
83571*/
83572static int doWalCallbacks(sqlite3 *db){
83573 int rc = SQLITE_OK;
83574#ifndef SQLITE_OMIT_WAL
83575 int i;
83576 for(i=0; i<db->nDb; i++){
83577 Btree *pBt = db->aDb[i].pBt;
83578 if( pBt ){
83579 int nEntry;
83580 sqlite3BtreeEnter(pBt);
83581 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
83582 sqlite3BtreeLeave(pBt);
83583 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
83584 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
83585 }
83586 }
83587 }
83588#endif
83589 return rc;
83590}
83591
83592
83593/*
83594** Execute the statement pStmt, either until a row of data is ready, the
83595** statement is completely executed or an error occurs.
83596**
83597** This routine implements the bulk of the logic behind the sqlite_step()
83598** API. The only thing omitted is the automatic recompile if a
83599** schema change has occurred. That detail is handled by the
83600** outer sqlite3_step() wrapper procedure.
83601*/
83602static int sqlite3Step(Vdbe *p){
83603 sqlite3 *db;
83604 int rc;
83605
83606 assert(p);
83607 if( p->magic!=VDBE_MAGIC_RUN ){
83608 /* We used to require that sqlite3_reset() be called before retrying
83609 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
83610 ** with version 3.7.0, we changed this so that sqlite3_reset() would
83611 ** be called automatically instead of throwing the SQLITE_MISUSE error.
83612 ** This "automatic-reset" change is not technically an incompatibility,
83613 ** since any application that receives an SQLITE_MISUSE is broken by
83614 ** definition.
83615 **
83616 ** Nevertheless, some published applications that were originally written
83617 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
83618 ** returns, and those were broken by the automatic-reset change. As a
83619 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
83620 ** legacy behavior of returning SQLITE_MISUSE for cases where the
83621 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
83622 ** or SQLITE_BUSY error.
83623 */
83624#ifdef SQLITE_OMIT_AUTORESET
83625 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
83626 sqlite3_reset((sqlite3_stmt*)p);
83627 }else{
83628 return SQLITE_MISUSE_BKPT;
83629 }
83630#else
83631 sqlite3_reset((sqlite3_stmt*)p);
83632#endif
83633 }
83634
83635 /* Check that malloc() has not failed. If it has, return early. */
83636 db = p->db;
83637 if( db->mallocFailed ){
83638 p->rc = SQLITE_NOMEM;
83639 return SQLITE_NOMEM_BKPT;
83640 }
83641
83642 if( p->pc<0 && p->expired ){
83643 p->rc = SQLITE_SCHEMA;
83644 rc = SQLITE_ERROR;
83645 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
83646 /* If this statement was prepared using saved SQL and an
83647 ** error has occurred, then return the error code in p->rc to the
83648 ** caller. Set the error code in the database handle to the same value.
83649 */
83650 rc = sqlite3VdbeTransferError(p);
83651 }
83652 goto end_of_step;
83653 }
83654 if( p->pc<0 ){
83655 /* If there are no other statements currently running, then
83656 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
83657 ** from interrupting a statement that has not yet started.
83658 */
83659 if( db->nVdbeActive==0 ){
83660 AtomicStore(&db->u1.isInterrupted, 0);
83661 }
83662
83663 assert( db->nVdbeWrite>0 || db->autoCommit==0
83664 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
83665 );
83666
83667#ifndef SQLITE_OMIT_TRACE
83668 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
83669 && !db->init.busy && p->zSql ){
83670 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
83671 }else{
83672 assert( p->startTime==0 );
83673 }
83674#endif
83675
83676 db->nVdbeActive++;
83677 if( p->readOnly==0 ) db->nVdbeWrite++;
83678 if( p->bIsReader ) db->nVdbeRead++;
83679 p->pc = 0;
83680 }
83681#ifdef SQLITE_DEBUG
83682 p->rcApp = SQLITE_OK;
83683#endif
83684#ifndef SQLITE_OMIT_EXPLAIN
83685 if( p->explain ){
83686 rc = sqlite3VdbeList(p);
83687 }else
83688#endif /* SQLITE_OMIT_EXPLAIN */
83689 {
83690 db->nVdbeExec++;
83691 rc = sqlite3VdbeExec(p);
83692 db->nVdbeExec--;
83693 }
83694
83695 if( rc!=SQLITE_ROW ){
83696#ifndef SQLITE_OMIT_TRACE
83697 /* If the statement completed successfully, invoke the profile callback */
83698 checkProfileCallback(db, p);
83699#endif
83700
83701 if( rc==SQLITE_DONE && db->autoCommit ){
83702 assert( p->rc==SQLITE_OK );
83703 p->rc = doWalCallbacks(db);
83704 if( p->rc!=SQLITE_OK ){
83705 rc = SQLITE_ERROR;
83706 }
83707 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
83708 /* If this statement was prepared using saved SQL and an
83709 ** error has occurred, then return the error code in p->rc to the
83710 ** caller. Set the error code in the database handle to the same value.
83711 */
83712 rc = sqlite3VdbeTransferError(p);
83713 }
83714 }
83715
83716 db->errCode = rc;
83717 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
83718 p->rc = SQLITE_NOMEM_BKPT;
83719 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
83720 }
83721end_of_step:
83722 /* There are only a limited number of result codes allowed from the
83723 ** statements prepared using the legacy sqlite3_prepare() interface */
83724 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
83725 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
83726 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
83727 );
83728 return (rc&db->errMask);
83729}
83730
83731/*
83732** This is the top-level implementation of sqlite3_step(). Call
83733** sqlite3Step() to do most of the work. If a schema error occurs,
83734** call sqlite3Reprepare() and try again.
83735*/
83736SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
83737 int rc = SQLITE_OK; /* Result from sqlite3Step() */
83738 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
83739 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
83740 sqlite3 *db; /* The database connection */
83741
83742 if( vdbeSafetyNotNull(v) ){
83743 return SQLITE_MISUSE_BKPT;
83744 }
83745 db = v->db;
83746 sqlite3_mutex_enter(db->mutex);
83747 v->doingRerun = 0;
83748 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
83749 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
83750 int savedPc = v->pc;
83751 rc = sqlite3Reprepare(v);
83752 if( rc!=SQLITE_OK ){
83753 /* This case occurs after failing to recompile an sql statement.
83754 ** The error message from the SQL compiler has already been loaded
83755 ** into the database handle. This block copies the error message
83756 ** from the database handle into the statement and sets the statement
83757 ** program counter to 0 to ensure that when the statement is
83758 ** finalized or reset the parser error message is available via
83759 ** sqlite3_errmsg() and sqlite3_errcode().
83760 */
83761 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
83762 sqlite3DbFree(db, v->zErrMsg);
83763 if( !db->mallocFailed ){
83764 v->zErrMsg = sqlite3DbStrDup(db, zErr);
83765 v->rc = rc = sqlite3ApiExit(db, rc);
83766 } else {
83767 v->zErrMsg = 0;
83768 v->rc = rc = SQLITE_NOMEM_BKPT;
83769 }
83770 break;
83771 }
83772 sqlite3_reset(pStmt);
83773 if( savedPc>=0 ) v->doingRerun = 1;
83774 assert( v->expired==0 );
83775 }
83776 sqlite3_mutex_leave(db->mutex);
83777 return rc;
83778}
83779
83780
83781/*
83782** Extract the user data from a sqlite3_context structure and return a
83783** pointer to it.
83784*/
83785SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
83786 assert( p && p->pFunc );
83787 return p->pFunc->pUserData;
83788}
83789
83790/*
83791** Extract the user data from a sqlite3_context structure and return a
83792** pointer to it.
83793**
83794** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
83795** returns a copy of the pointer to the database connection (the 1st
83796** parameter) of the sqlite3_create_function() and
83797** sqlite3_create_function16() routines that originally registered the
83798** application defined function.
83799*/
83800SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
83801 assert( p && p->pOut );
83802 return p->pOut->db;
83803}
83804
83805/*
83806** If this routine is invoked from within an xColumn method of a virtual
83807** table, then it returns true if and only if the the call is during an
83808** UPDATE operation and the value of the column will not be modified
83809** by the UPDATE.
83810**
83811** If this routine is called from any context other than within the
83812** xColumn method of a virtual table, then the return value is meaningless
83813** and arbitrary.
83814**
83815** Virtual table implements might use this routine to optimize their
83816** performance by substituting a NULL result, or some other light-weight
83817** value, as a signal to the xUpdate routine that the column is unchanged.
83818*/
83819SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
83820 assert( p );
83821 return sqlite3_value_nochange(p->pOut);
83822}
83823
83824/*
83825** Return the current time for a statement. If the current time
83826** is requested more than once within the same run of a single prepared
83827** statement, the exact same time is returned for each invocation regardless
83828** of the amount of time that elapses between invocations. In other words,
83829** the time returned is always the time of the first call.
83830*/
83831SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
83832 int rc;
83833#ifndef SQLITE_ENABLE_STAT4
83834 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
83835 assert( p->pVdbe!=0 );
83836#else
83837 sqlite3_int64 iTime = 0;
83838 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
83839#endif
83840 if( *piTime==0 ){
83841 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
83842 if( rc ) *piTime = 0;
83843 }
83844 return *piTime;
83845}
83846
83847/*
83848** Create a new aggregate context for p and return a pointer to
83849** its pMem->z element.
83850*/
83851static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
83852 Mem *pMem = p->pMem;
83853 assert( (pMem->flags & MEM_Agg)==0 );
83854 if( nByte<=0 ){
83855 sqlite3VdbeMemSetNull(pMem);
83856 pMem->z = 0;
83857 }else{
83858 sqlite3VdbeMemClearAndResize(pMem, nByte);
83859 pMem->flags = MEM_Agg;
83860 pMem->u.pDef = p->pFunc;
83861 if( pMem->z ){
83862 memset(pMem->z, 0, nByte);
83863 }
83864 }
83865 return (void*)pMem->z;
83866}
83867
83868/*
83869** Allocate or return the aggregate context for a user function. A new
83870** context is allocated on the first call. Subsequent calls return the
83871** same context that was returned on prior calls.
83872*/
83873SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
83874 assert( p && p->pFunc && p->pFunc->xFinalize );
83875 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
83876 testcase( nByte<0 );
83877 if( (p->pMem->flags & MEM_Agg)==0 ){
83878 return createAggContext(p, nByte);
83879 }else{
83880 return (void*)p->pMem->z;
83881 }
83882}
83883
83884/*
83885** Return the auxiliary data pointer, if any, for the iArg'th argument to
83886** the user-function defined by pCtx.
83887**
83888** The left-most argument is 0.
83889**
83890** Undocumented behavior: If iArg is negative then access a cache of
83891** auxiliary data pointers that is available to all functions within a
83892** single prepared statement. The iArg values must match.
83893*/
83894SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
83895 AuxData *pAuxData;
83896
83897 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83898#if SQLITE_ENABLE_STAT4
83899 if( pCtx->pVdbe==0 ) return 0;
83900#else
83901 assert( pCtx->pVdbe!=0 );
83902#endif
83903 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
83904 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
83905 return pAuxData->pAux;
83906 }
83907 }
83908 return 0;
83909}
83910
83911/*
83912** Set the auxiliary data pointer and delete function, for the iArg'th
83913** argument to the user-function defined by pCtx. Any previous value is
83914** deleted by calling the delete function specified when it was set.
83915**
83916** The left-most argument is 0.
83917**
83918** Undocumented behavior: If iArg is negative then make the data available
83919** to all functions within the current prepared statement using iArg as an
83920** access code.
83921*/
83922SQLITE_API void sqlite3_set_auxdata(
83923 sqlite3_context *pCtx,
83924 int iArg,
83925 void *pAux,
83926 void (*xDelete)(void*)
83927){
83928 AuxData *pAuxData;
83929 Vdbe *pVdbe = pCtx->pVdbe;
83930
83931 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
83932#ifdef SQLITE_ENABLE_STAT4
83933 if( pVdbe==0 ) goto failed;
83934#else
83935 assert( pVdbe!=0 );
83936#endif
83937
83938 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
83939 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
83940 break;
83941 }
83942 }
83943 if( pAuxData==0 ){
83944 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
83945 if( !pAuxData ) goto failed;
83946 pAuxData->iAuxOp = pCtx->iOp;
83947 pAuxData->iAuxArg = iArg;
83948 pAuxData->pNextAux = pVdbe->pAuxData;
83949 pVdbe->pAuxData = pAuxData;
83950 if( pCtx->isError==0 ) pCtx->isError = -1;
83951 }else if( pAuxData->xDeleteAux ){
83952 pAuxData->xDeleteAux(pAuxData->pAux);
83953 }
83954
83955 pAuxData->pAux = pAux;
83956 pAuxData->xDeleteAux = xDelete;
83957 return;
83958
83959failed:
83960 if( xDelete ){
83961 xDelete(pAux);
83962 }
83963}
83964
83965#ifndef SQLITE_OMIT_DEPRECATED
83966/*
83967** Return the number of times the Step function of an aggregate has been
83968** called.
83969**
83970** This function is deprecated. Do not use it for new code. It is
83971** provide only to avoid breaking legacy code. New aggregate function
83972** implementations should keep their own counts within their aggregate
83973** context.
83974*/
83975SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
83976 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
83977 return p->pMem->n;
83978}
83979#endif
83980
83981/*
83982** Return the number of columns in the result set for the statement pStmt.
83983*/
83984SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
83985 Vdbe *pVm = (Vdbe *)pStmt;
83986 return pVm ? pVm->nResColumn : 0;
83987}
83988
83989/*
83990** Return the number of values available from the current row of the
83991** currently executing statement pStmt.
83992*/
83993SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
83994 Vdbe *pVm = (Vdbe *)pStmt;
83995 if( pVm==0 || pVm->pResultSet==0 ) return 0;
83996 return pVm->nResColumn;
83997}
83998
83999/*
84000** Return a pointer to static memory containing an SQL NULL value.
84001*/
84002static const Mem *columnNullValue(void){
84003 /* Even though the Mem structure contains an element
84004 ** of type i64, on certain architectures (x86) with certain compiler
84005 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
84006 ** instead of an 8-byte one. This all works fine, except that when
84007 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
84008 ** that a Mem structure is located on an 8-byte boundary. To prevent
84009 ** these assert()s from failing, when building with SQLITE_DEBUG defined
84010 ** using gcc, we force nullMem to be 8-byte aligned using the magical
84011 ** __attribute__((aligned(8))) macro. */
84012 static const Mem nullMem
84013#if defined(SQLITE_DEBUG) && defined(__GNUC__)
84014 __attribute__((aligned(8)))
84015#endif
84016 = {
84017 /* .u = */ {0},
84018 /* .flags = */ (u16)MEM_Null,
84019 /* .enc = */ (u8)0,
84020 /* .eSubtype = */ (u8)0,
84021 /* .n = */ (int)0,
84022 /* .z = */ (char*)0,
84023 /* .zMalloc = */ (char*)0,
84024 /* .szMalloc = */ (int)0,
84025 /* .uTemp = */ (u32)0,
84026 /* .db = */ (sqlite3*)0,
84027 /* .xDel = */ (void(*)(void*))0,
84028#ifdef SQLITE_DEBUG
84029 /* .pScopyFrom = */ (Mem*)0,
84030 /* .mScopyFlags= */ 0,
84031#endif
84032 };
84033 return &nullMem;
84034}
84035
84036/*
84037** Check to see if column iCol of the given statement is valid. If
84038** it is, return a pointer to the Mem for the value of that column.
84039** If iCol is not valid, return a pointer to a Mem which has a value
84040** of NULL.
84041*/
84042static Mem *columnMem(sqlite3_stmt *pStmt, int i){
84043 Vdbe *pVm;
84044 Mem *pOut;
84045
84046 pVm = (Vdbe *)pStmt;
84047 if( pVm==0 ) return (Mem*)columnNullValue();
84048 assert( pVm->db );
84049 sqlite3_mutex_enter(pVm->db->mutex);
84050 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
84051 pOut = &pVm->pResultSet[i];
84052 }else{
84053 sqlite3Error(pVm->db, SQLITE_RANGE);
84054 pOut = (Mem*)columnNullValue();
84055 }
84056 return pOut;
84057}
84058
84059/*
84060** This function is called after invoking an sqlite3_value_XXX function on a
84061** column value (i.e. a value returned by evaluating an SQL expression in the
84062** select list of a SELECT statement) that may cause a malloc() failure. If
84063** malloc() has failed, the threads mallocFailed flag is cleared and the result
84064** code of statement pStmt set to SQLITE_NOMEM.
84065**
84066** Specifically, this is called from within:
84067**
84068** sqlite3_column_int()
84069** sqlite3_column_int64()
84070** sqlite3_column_text()
84071** sqlite3_column_text16()
84072** sqlite3_column_real()
84073** sqlite3_column_bytes()
84074** sqlite3_column_bytes16()
84075** sqiite3_column_blob()
84076*/
84077static void columnMallocFailure(sqlite3_stmt *pStmt)
84078{
84079 /* If malloc() failed during an encoding conversion within an
84080 ** sqlite3_column_XXX API, then set the return code of the statement to
84081 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
84082 ** and _finalize() will return NOMEM.
84083 */
84084 Vdbe *p = (Vdbe *)pStmt;
84085 if( p ){
84086 assert( p->db!=0 );
84087 assert( sqlite3_mutex_held(p->db->mutex) );
84088 p->rc = sqlite3ApiExit(p->db, p->rc);
84089 sqlite3_mutex_leave(p->db->mutex);
84090 }
84091}
84092
84093/**************************** sqlite3_column_ *******************************
84094** The following routines are used to access elements of the current row
84095** in the result set.
84096*/
84097SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
84098 const void *val;
84099 val = sqlite3_value_blob( columnMem(pStmt,i) );
84100 /* Even though there is no encoding conversion, value_blob() might
84101 ** need to call malloc() to expand the result of a zeroblob()
84102 ** expression.
84103 */
84104 columnMallocFailure(pStmt);
84105 return val;
84106}
84107SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
84108 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
84109 columnMallocFailure(pStmt);
84110 return val;
84111}
84112SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
84113 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
84114 columnMallocFailure(pStmt);
84115 return val;
84116}
84117SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
84118 double val = sqlite3_value_double( columnMem(pStmt,i) );
84119 columnMallocFailure(pStmt);
84120 return val;
84121}
84122SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
84123 int val = sqlite3_value_int( columnMem(pStmt,i) );
84124 columnMallocFailure(pStmt);
84125 return val;
84126}
84127SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
84128 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
84129 columnMallocFailure(pStmt);
84130 return val;
84131}
84132SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
84133 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
84134 columnMallocFailure(pStmt);
84135 return val;
84136}
84137SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
84138 Mem *pOut = columnMem(pStmt, i);
84139 if( pOut->flags&MEM_Static ){
84140 pOut->flags &= ~MEM_Static;
84141 pOut->flags |= MEM_Ephem;
84142 }
84143 columnMallocFailure(pStmt);
84144 return (sqlite3_value *)pOut;
84145}
84146#ifndef SQLITE_OMIT_UTF16
84147SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
84148 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
84149 columnMallocFailure(pStmt);
84150 return val;
84151}
84152#endif /* SQLITE_OMIT_UTF16 */
84153SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
84154 int iType = sqlite3_value_type( columnMem(pStmt,i) );
84155 columnMallocFailure(pStmt);
84156 return iType;
84157}
84158
84159/*
84160** Convert the N-th element of pStmt->pColName[] into a string using
84161** xFunc() then return that string. If N is out of range, return 0.
84162**
84163** There are up to 5 names for each column. useType determines which
84164** name is returned. Here are the names:
84165**
84166** 0 The column name as it should be displayed for output
84167** 1 The datatype name for the column
84168** 2 The name of the database that the column derives from
84169** 3 The name of the table that the column derives from
84170** 4 The name of the table column that the result column derives from
84171**
84172** If the result is not a simple column reference (if it is an expression
84173** or a constant) then useTypes 2, 3, and 4 return NULL.
84174*/
84175static const void *columnName(
84176 sqlite3_stmt *pStmt, /* The statement */
84177 int N, /* Which column to get the name for */
84178 int useUtf16, /* True to return the name as UTF16 */
84179 int useType /* What type of name */
84180){
84181 const void *ret;
84182 Vdbe *p;
84183 int n;
84184 sqlite3 *db;
84185#ifdef SQLITE_ENABLE_API_ARMOR
84186 if( pStmt==0 ){
84187 (void)SQLITE_MISUSE_BKPT;
84188 return 0;
84189 }
84190#endif
84191 ret = 0;
84192 p = (Vdbe *)pStmt;
84193 db = p->db;
84194 assert( db!=0 );
84195 n = sqlite3_column_count(pStmt);
84196 if( N<n && N>=0 ){
84197 N += useType*n;
84198 sqlite3_mutex_enter(db->mutex);
84199 assert( db->mallocFailed==0 );
84200#ifndef SQLITE_OMIT_UTF16
84201 if( useUtf16 ){
84202 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
84203 }else
84204#endif
84205 {
84206 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
84207 }
84208 /* A malloc may have failed inside of the _text() call. If this
84209 ** is the case, clear the mallocFailed flag and return NULL.
84210 */
84211 if( db->mallocFailed ){
84212 sqlite3OomClear(db);
84213 ret = 0;
84214 }
84215 sqlite3_mutex_leave(db->mutex);
84216 }
84217 return ret;
84218}
84219
84220/*
84221** Return the name of the Nth column of the result set returned by SQL
84222** statement pStmt.
84223*/
84224SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
84225 return columnName(pStmt, N, 0, COLNAME_NAME);
84226}
84227#ifndef SQLITE_OMIT_UTF16
84228SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
84229 return columnName(pStmt, N, 1, COLNAME_NAME);
84230}
84231#endif
84232
84233/*
84234** Constraint: If you have ENABLE_COLUMN_METADATA then you must
84235** not define OMIT_DECLTYPE.
84236*/
84237#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
84238# error "Must not define both SQLITE_OMIT_DECLTYPE \
84239 and SQLITE_ENABLE_COLUMN_METADATA"
84240#endif
84241
84242#ifndef SQLITE_OMIT_DECLTYPE
84243/*
84244** Return the column declaration type (if applicable) of the 'i'th column
84245** of the result set of SQL statement pStmt.
84246*/
84247SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
84248 return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
84249}
84250#ifndef SQLITE_OMIT_UTF16
84251SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
84252 return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
84253}
84254#endif /* SQLITE_OMIT_UTF16 */
84255#endif /* SQLITE_OMIT_DECLTYPE */
84256
84257#ifdef SQLITE_ENABLE_COLUMN_METADATA
84258/*
84259** Return the name of the database from which a result column derives.
84260** NULL is returned if the result column is an expression or constant or
84261** anything else which is not an unambiguous reference to a database column.
84262*/
84263SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
84264 return columnName(pStmt, N, 0, COLNAME_DATABASE);
84265}
84266#ifndef SQLITE_OMIT_UTF16
84267SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
84268 return columnName(pStmt, N, 1, COLNAME_DATABASE);
84269}
84270#endif /* SQLITE_OMIT_UTF16 */
84271
84272/*
84273** Return the name of the table from which a result column derives.
84274** NULL is returned if the result column is an expression or constant or
84275** anything else which is not an unambiguous reference to a database column.
84276*/
84277SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
84278 return columnName(pStmt, N, 0, COLNAME_TABLE);
84279}
84280#ifndef SQLITE_OMIT_UTF16
84281SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
84282 return columnName(pStmt, N, 1, COLNAME_TABLE);
84283}
84284#endif /* SQLITE_OMIT_UTF16 */
84285
84286/*
84287** Return the name of the table column from which a result column derives.
84288** NULL is returned if the result column is an expression or constant or
84289** anything else which is not an unambiguous reference to a database column.
84290*/
84291SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
84292 return columnName(pStmt, N, 0, COLNAME_COLUMN);
84293}
84294#ifndef SQLITE_OMIT_UTF16
84295SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
84296 return columnName(pStmt, N, 1, COLNAME_COLUMN);
84297}
84298#endif /* SQLITE_OMIT_UTF16 */
84299#endif /* SQLITE_ENABLE_COLUMN_METADATA */
84300
84301
84302/******************************* sqlite3_bind_ ***************************
84303**
84304** Routines used to attach values to wildcards in a compiled SQL statement.
84305*/
84306/*
84307** Unbind the value bound to variable i in virtual machine p. This is the
84308** the same as binding a NULL value to the column. If the "i" parameter is
84309** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
84310**
84311** A successful evaluation of this routine acquires the mutex on p.
84312** the mutex is released if any kind of error occurs.
84313**
84314** The error code stored in database p->db is overwritten with the return
84315** value in any case.
84316*/
84317static int vdbeUnbind(Vdbe *p, int i){
84318 Mem *pVar;
84319 if( vdbeSafetyNotNull(p) ){
84320 return SQLITE_MISUSE_BKPT;
84321 }
84322 sqlite3_mutex_enter(p->db->mutex);
84323 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
84324 sqlite3Error(p->db, SQLITE_MISUSE);
84325 sqlite3_mutex_leave(p->db->mutex);
84326 sqlite3_log(SQLITE_MISUSE,
84327 "bind on a busy prepared statement: [%s]", p->zSql);
84328 return SQLITE_MISUSE_BKPT;
84329 }
84330 if( i<1 || i>p->nVar ){
84331 sqlite3Error(p->db, SQLITE_RANGE);
84332 sqlite3_mutex_leave(p->db->mutex);
84333 return SQLITE_RANGE;
84334 }
84335 i--;
84336 pVar = &p->aVar[i];
84337 sqlite3VdbeMemRelease(pVar);
84338 pVar->flags = MEM_Null;
84339 p->db->errCode = SQLITE_OK;
84340
84341 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
84342 ** binding a new value to this variable invalidates the current query plan.
84343 **
84344 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
84345 ** parameter in the WHERE clause might influence the choice of query plan
84346 ** for a statement, then the statement will be automatically recompiled,
84347 ** as if there had been a schema change, on the first sqlite3_step() call
84348 ** following any change to the bindings of that parameter.
84349 */
84350 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
84351 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
84352 p->expired = 1;
84353 }
84354 return SQLITE_OK;
84355}
84356
84357/*
84358** Bind a text or BLOB value.
84359*/
84360static int bindText(
84361 sqlite3_stmt *pStmt, /* The statement to bind against */
84362 int i, /* Index of the parameter to bind */
84363 const void *zData, /* Pointer to the data to be bound */
84364 int nData, /* Number of bytes of data to be bound */
84365 void (*xDel)(void*), /* Destructor for the data */
84366 u8 encoding /* Encoding for the data */
84367){
84368 Vdbe *p = (Vdbe *)pStmt;
84369 Mem *pVar;
84370 int rc;
84371
84372 rc = vdbeUnbind(p, i);
84373 if( rc==SQLITE_OK ){
84374 if( zData!=0 ){
84375 pVar = &p->aVar[i-1];
84376 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
84377 if( rc==SQLITE_OK && encoding!=0 ){
84378 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
84379 }
84380 if( rc ){
84381 sqlite3Error(p->db, rc);
84382 rc = sqlite3ApiExit(p->db, rc);
84383 }
84384 }
84385 sqlite3_mutex_leave(p->db->mutex);
84386 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
84387 xDel((void*)zData);
84388 }
84389 return rc;
84390}
84391
84392
84393/*
84394** Bind a blob value to an SQL statement variable.
84395*/
84396SQLITE_API int sqlite3_bind_blob(
84397 sqlite3_stmt *pStmt,
84398 int i,
84399 const void *zData,
84400 int nData,
84401 void (*xDel)(void*)
84402){
84403#ifdef SQLITE_ENABLE_API_ARMOR
84404 if( nData<0 ) return SQLITE_MISUSE_BKPT;
84405#endif
84406 return bindText(pStmt, i, zData, nData, xDel, 0);
84407}
84408SQLITE_API int sqlite3_bind_blob64(
84409 sqlite3_stmt *pStmt,
84410 int i,
84411 const void *zData,
84412 sqlite3_uint64 nData,
84413 void (*xDel)(void*)
84414){
84415 assert( xDel!=SQLITE_DYNAMIC );
84416 if( nData>0x7fffffff ){
84417 return invokeValueDestructor(zData, xDel, 0);
84418 }else{
84419 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
84420 }
84421}
84422SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
84423 int rc;
84424 Vdbe *p = (Vdbe *)pStmt;
84425 rc = vdbeUnbind(p, i);
84426 if( rc==SQLITE_OK ){
84427 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
84428 sqlite3_mutex_leave(p->db->mutex);
84429 }
84430 return rc;
84431}
84432SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
84433 return sqlite3_bind_int64(p, i, (i64)iValue);
84434}
84435SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
84436 int rc;
84437 Vdbe *p = (Vdbe *)pStmt;
84438 rc = vdbeUnbind(p, i);
84439 if( rc==SQLITE_OK ){
84440 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
84441 sqlite3_mutex_leave(p->db->mutex);
84442 }
84443 return rc;
84444}
84445SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
84446 int rc;
84447 Vdbe *p = (Vdbe*)pStmt;
84448 rc = vdbeUnbind(p, i);
84449 if( rc==SQLITE_OK ){
84450 sqlite3_mutex_leave(p->db->mutex);
84451 }
84452 return rc;
84453}
84454SQLITE_API int sqlite3_bind_pointer(
84455 sqlite3_stmt *pStmt,
84456 int i,
84457 void *pPtr,
84458 const char *zPTtype,
84459 void (*xDestructor)(void*)
84460){
84461 int rc;
84462 Vdbe *p = (Vdbe*)pStmt;
84463 rc = vdbeUnbind(p, i);
84464 if( rc==SQLITE_OK ){
84465 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
84466 sqlite3_mutex_leave(p->db->mutex);
84467 }else if( xDestructor ){
84468 xDestructor(pPtr);
84469 }
84470 return rc;
84471}
84472SQLITE_API int sqlite3_bind_text(
84473 sqlite3_stmt *pStmt,
84474 int i,
84475 const char *zData,
84476 int nData,
84477 void (*xDel)(void*)
84478){
84479 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
84480}
84481SQLITE_API int sqlite3_bind_text64(
84482 sqlite3_stmt *pStmt,
84483 int i,
84484 const char *zData,
84485 sqlite3_uint64 nData,
84486 void (*xDel)(void*),
84487 unsigned char enc
84488){
84489 assert( xDel!=SQLITE_DYNAMIC );
84490 if( nData>0x7fffffff ){
84491 return invokeValueDestructor(zData, xDel, 0);
84492 }else{
84493 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
84494 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
84495 }
84496}
84497#ifndef SQLITE_OMIT_UTF16
84498SQLITE_API int sqlite3_bind_text16(
84499 sqlite3_stmt *pStmt,
84500 int i,
84501 const void *zData,
84502 int nData,
84503 void (*xDel)(void*)
84504){
84505 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
84506}
84507#endif /* SQLITE_OMIT_UTF16 */
84508SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
84509 int rc;
84510 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
84511 case SQLITE_INTEGER: {
84512 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
84513 break;
84514 }
84515 case SQLITE_FLOAT: {
84516 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
84517 break;
84518 }
84519 case SQLITE_BLOB: {
84520 if( pValue->flags & MEM_Zero ){
84521 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
84522 }else{
84523 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
84524 }
84525 break;
84526 }
84527 case SQLITE_TEXT: {
84528 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
84529 pValue->enc);
84530 break;
84531 }
84532 default: {
84533 rc = sqlite3_bind_null(pStmt, i);
84534 break;
84535 }
84536 }
84537 return rc;
84538}
84539SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
84540 int rc;
84541 Vdbe *p = (Vdbe *)pStmt;
84542 rc = vdbeUnbind(p, i);
84543 if( rc==SQLITE_OK ){
84544 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
84545 sqlite3_mutex_leave(p->db->mutex);
84546 }
84547 return rc;
84548}
84549SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
84550 int rc;
84551 Vdbe *p = (Vdbe *)pStmt;
84552 sqlite3_mutex_enter(p->db->mutex);
84553 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
84554 rc = SQLITE_TOOBIG;
84555 }else{
84556 assert( (n & 0x7FFFFFFF)==n );
84557 rc = sqlite3_bind_zeroblob(pStmt, i, n);
84558 }
84559 rc = sqlite3ApiExit(p->db, rc);
84560 sqlite3_mutex_leave(p->db->mutex);
84561 return rc;
84562}
84563
84564/*
84565** Return the number of wildcards that can be potentially bound to.
84566** This routine is added to support DBD::SQLite.
84567*/
84568SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
84569 Vdbe *p = (Vdbe*)pStmt;
84570 return p ? p->nVar : 0;
84571}
84572
84573/*
84574** Return the name of a wildcard parameter. Return NULL if the index
84575** is out of range or if the wildcard is unnamed.
84576**
84577** The result is always UTF-8.
84578*/
84579SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
84580 Vdbe *p = (Vdbe*)pStmt;
84581 if( p==0 ) return 0;
84582 return sqlite3VListNumToName(p->pVList, i);
84583}
84584
84585/*
84586** Given a wildcard parameter name, return the index of the variable
84587** with that name. If there is no variable with the given name,
84588** return 0.
84589*/
84590SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
84591 if( p==0 || zName==0 ) return 0;
84592 return sqlite3VListNameToNum(p->pVList, zName, nName);
84593}
84594SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
84595 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
84596}
84597
84598/*
84599** Transfer all bindings from the first statement over to the second.
84600*/
84601SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
84602 Vdbe *pFrom = (Vdbe*)pFromStmt;
84603 Vdbe *pTo = (Vdbe*)pToStmt;
84604 int i;
84605 assert( pTo->db==pFrom->db );
84606 assert( pTo->nVar==pFrom->nVar );
84607 sqlite3_mutex_enter(pTo->db->mutex);
84608 for(i=0; i<pFrom->nVar; i++){
84609 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
84610 }
84611 sqlite3_mutex_leave(pTo->db->mutex);
84612 return SQLITE_OK;
84613}
84614
84615#ifndef SQLITE_OMIT_DEPRECATED
84616/*
84617** Deprecated external interface. Internal/core SQLite code
84618** should call sqlite3TransferBindings.
84619**
84620** It is misuse to call this routine with statements from different
84621** database connections. But as this is a deprecated interface, we
84622** will not bother to check for that condition.
84623**
84624** If the two statements contain a different number of bindings, then
84625** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
84626** SQLITE_OK is returned.
84627*/
84628SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
84629 Vdbe *pFrom = (Vdbe*)pFromStmt;
84630 Vdbe *pTo = (Vdbe*)pToStmt;
84631 if( pFrom->nVar!=pTo->nVar ){
84632 return SQLITE_ERROR;
84633 }
84634 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
84635 if( pTo->expmask ){
84636 pTo->expired = 1;
84637 }
84638 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
84639 if( pFrom->expmask ){
84640 pFrom->expired = 1;
84641 }
84642 return sqlite3TransferBindings(pFromStmt, pToStmt);
84643}
84644#endif
84645
84646/*
84647** Return the sqlite3* database handle to which the prepared statement given
84648** in the argument belongs. This is the same database handle that was
84649** the first argument to the sqlite3_prepare() that was used to create
84650** the statement in the first place.
84651*/
84652SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
84653 return pStmt ? ((Vdbe*)pStmt)->db : 0;
84654}
84655
84656/*
84657** Return true if the prepared statement is guaranteed to not modify the
84658** database.
84659*/
84660SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
84661 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
84662}
84663
84664/*
84665** Return 1 if the statement is an EXPLAIN and return 2 if the
84666** statement is an EXPLAIN QUERY PLAN
84667*/
84668SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
84669 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
84670}
84671
84672/*
84673** Return true if the prepared statement is in need of being reset.
84674*/
84675SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
84676 Vdbe *v = (Vdbe*)pStmt;
84677 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
84678}
84679
84680/*
84681** Return a pointer to the next prepared statement after pStmt associated
84682** with database connection pDb. If pStmt is NULL, return the first
84683** prepared statement for the database connection. Return NULL if there
84684** are no more.
84685*/
84686SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
84687 sqlite3_stmt *pNext;
84688#ifdef SQLITE_ENABLE_API_ARMOR
84689 if( !sqlite3SafetyCheckOk(pDb) ){
84690 (void)SQLITE_MISUSE_BKPT;
84691 return 0;
84692 }
84693#endif
84694 sqlite3_mutex_enter(pDb->mutex);
84695 if( pStmt==0 ){
84696 pNext = (sqlite3_stmt*)pDb->pVdbe;
84697 }else{
84698 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
84699 }
84700 sqlite3_mutex_leave(pDb->mutex);
84701 return pNext;
84702}
84703
84704/*
84705** Return the value of a status counter for a prepared statement
84706*/
84707SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
84708 Vdbe *pVdbe = (Vdbe*)pStmt;
84709 u32 v;
84710#ifdef SQLITE_ENABLE_API_ARMOR
84711 if( !pStmt
84712 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
84713 ){
84714 (void)SQLITE_MISUSE_BKPT;
84715 return 0;
84716 }
84717#endif
84718 if( op==SQLITE_STMTSTATUS_MEMUSED ){
84719 sqlite3 *db = pVdbe->db;
84720 sqlite3_mutex_enter(db->mutex);
84721 v = 0;
84722 db->pnBytesFreed = (int*)&v;
84723 sqlite3VdbeClearObject(db, pVdbe);
84724 sqlite3DbFree(db, pVdbe);
84725 db->pnBytesFreed = 0;
84726 sqlite3_mutex_leave(db->mutex);
84727 }else{
84728 v = pVdbe->aCounter[op];
84729 if( resetFlag ) pVdbe->aCounter[op] = 0;
84730 }
84731 return (int)v;
84732}
84733
84734/*
84735** Return the SQL associated with a prepared statement
84736*/
84737SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
84738 Vdbe *p = (Vdbe *)pStmt;
84739 return p ? p->zSql : 0;
84740}
84741
84742/*
84743** Return the SQL associated with a prepared statement with
84744** bound parameters expanded. Space to hold the returned string is
84745** obtained from sqlite3_malloc(). The caller is responsible for
84746** freeing the returned string by passing it to sqlite3_free().
84747**
84748** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
84749** expanded bound parameters.
84750*/
84751SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
84752#ifdef SQLITE_OMIT_TRACE
84753 return 0;
84754#else
84755 char *z = 0;
84756 const char *zSql = sqlite3_sql(pStmt);
84757 if( zSql ){
84758 Vdbe *p = (Vdbe *)pStmt;
84759 sqlite3_mutex_enter(p->db->mutex);
84760 z = sqlite3VdbeExpandSql(p, zSql);
84761 sqlite3_mutex_leave(p->db->mutex);
84762 }
84763 return z;
84764#endif
84765}
84766
84767#ifdef SQLITE_ENABLE_NORMALIZE
84768/*
84769** Return the normalized SQL associated with a prepared statement.
84770*/
84771SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
84772 Vdbe *p = (Vdbe *)pStmt;
84773 if( p==0 ) return 0;
84774 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
84775 sqlite3_mutex_enter(p->db->mutex);
84776 p->zNormSql = sqlite3Normalize(p, p->zSql);
84777 sqlite3_mutex_leave(p->db->mutex);
84778 }
84779 return p->zNormSql;
84780}
84781#endif /* SQLITE_ENABLE_NORMALIZE */
84782
84783#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84784/*
84785** Allocate and populate an UnpackedRecord structure based on the serialized
84786** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
84787** if successful, or a NULL pointer if an OOM error is encountered.
84788*/
84789static UnpackedRecord *vdbeUnpackRecord(
84790 KeyInfo *pKeyInfo,
84791 int nKey,
84792 const void *pKey
84793){
84794 UnpackedRecord *pRet; /* Return value */
84795
84796 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
84797 if( pRet ){
84798 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
84799 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
84800 }
84801 return pRet;
84802}
84803
84804/*
84805** This function is called from within a pre-update callback to retrieve
84806** a field of the row currently being updated or deleted.
84807*/
84808SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
84809 PreUpdate *p = db->pPreUpdate;
84810 Mem *pMem;
84811 int rc = SQLITE_OK;
84812
84813 /* Test that this call is being made from within an SQLITE_DELETE or
84814 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
84815 if( !p || p->op==SQLITE_INSERT ){
84816 rc = SQLITE_MISUSE_BKPT;
84817 goto preupdate_old_out;
84818 }
84819 if( p->pPk ){
84820 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
84821 }
84822 if( iIdx>=p->pCsr->nField || iIdx<0 ){
84823 rc = SQLITE_RANGE;
84824 goto preupdate_old_out;
84825 }
84826
84827 /* If the old.* record has not yet been loaded into memory, do so now. */
84828 if( p->pUnpacked==0 ){
84829 u32 nRec;
84830 u8 *aRec;
84831
84832 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
84833 aRec = sqlite3DbMallocRaw(db, nRec);
84834 if( !aRec ) goto preupdate_old_out;
84835 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
84836 if( rc==SQLITE_OK ){
84837 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
84838 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
84839 }
84840 if( rc!=SQLITE_OK ){
84841 sqlite3DbFree(db, aRec);
84842 goto preupdate_old_out;
84843 }
84844 p->aRecord = aRec;
84845 }
84846
84847 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
84848 if( iIdx==p->pTab->iPKey ){
84849 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
84850 }else if( iIdx>=p->pUnpacked->nField ){
84851 *ppValue = (sqlite3_value *)columnNullValue();
84852 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
84853 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84854 testcase( pMem->flags & MEM_Int );
84855 testcase( pMem->flags & MEM_IntReal );
84856 sqlite3VdbeMemRealify(pMem);
84857 }
84858 }
84859
84860 preupdate_old_out:
84861 sqlite3Error(db, rc);
84862 return sqlite3ApiExit(db, rc);
84863}
84864#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84865
84866#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84867/*
84868** This function is called from within a pre-update callback to retrieve
84869** the number of columns in the row being updated, deleted or inserted.
84870*/
84871SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
84872 PreUpdate *p = db->pPreUpdate;
84873 return (p ? p->keyinfo.nKeyField : 0);
84874}
84875#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84876
84877#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84878/*
84879** This function is designed to be called from within a pre-update callback
84880** only. It returns zero if the change that caused the callback was made
84881** immediately by a user SQL statement. Or, if the change was made by a
84882** trigger program, it returns the number of trigger programs currently
84883** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
84884** top-level trigger etc.).
84885**
84886** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
84887** or SET DEFAULT action is considered a trigger.
84888*/
84889SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
84890 PreUpdate *p = db->pPreUpdate;
84891 return (p ? p->v->nFrame : 0);
84892}
84893#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84894
84895#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
84896/*
84897** This function is called from within a pre-update callback to retrieve
84898** a field of the row currently being updated or inserted.
84899*/
84900SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
84901 PreUpdate *p = db->pPreUpdate;
84902 int rc = SQLITE_OK;
84903 Mem *pMem;
84904
84905 if( !p || p->op==SQLITE_DELETE ){
84906 rc = SQLITE_MISUSE_BKPT;
84907 goto preupdate_new_out;
84908 }
84909 if( p->pPk && p->op!=SQLITE_UPDATE ){
84910 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
84911 }
84912 if( iIdx>=p->pCsr->nField || iIdx<0 ){
84913 rc = SQLITE_RANGE;
84914 goto preupdate_new_out;
84915 }
84916
84917 if( p->op==SQLITE_INSERT ){
84918 /* For an INSERT, memory cell p->iNewReg contains the serialized record
84919 ** that is being inserted. Deserialize it. */
84920 UnpackedRecord *pUnpack = p->pNewUnpacked;
84921 if( !pUnpack ){
84922 Mem *pData = &p->v->aMem[p->iNewReg];
84923 rc = ExpandBlob(pData);
84924 if( rc!=SQLITE_OK ) goto preupdate_new_out;
84925 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
84926 if( !pUnpack ){
84927 rc = SQLITE_NOMEM;
84928 goto preupdate_new_out;
84929 }
84930 p->pNewUnpacked = pUnpack;
84931 }
84932 pMem = &pUnpack->aMem[iIdx];
84933 if( iIdx==p->pTab->iPKey ){
84934 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
84935 }else if( iIdx>=pUnpack->nField ){
84936 pMem = (sqlite3_value *)columnNullValue();
84937 }
84938 }else{
84939 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
84940 ** value. Make a copy of the cell contents and return a pointer to it.
84941 ** It is not safe to return a pointer to the memory cell itself as the
84942 ** caller may modify the value text encoding.
84943 */
84944 assert( p->op==SQLITE_UPDATE );
84945 if( !p->aNew ){
84946 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
84947 if( !p->aNew ){
84948 rc = SQLITE_NOMEM;
84949 goto preupdate_new_out;
84950 }
84951 }
84952 assert( iIdx>=0 && iIdx<p->pCsr->nField );
84953 pMem = &p->aNew[iIdx];
84954 if( pMem->flags==0 ){
84955 if( iIdx==p->pTab->iPKey ){
84956 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
84957 }else{
84958 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
84959 if( rc!=SQLITE_OK ) goto preupdate_new_out;
84960 }
84961 }
84962 }
84963 *ppValue = pMem;
84964
84965 preupdate_new_out:
84966 sqlite3Error(db, rc);
84967 return sqlite3ApiExit(db, rc);
84968}
84969#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
84970
84971#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
84972/*
84973** Return status data for a single loop within query pStmt.
84974*/
84975SQLITE_API int sqlite3_stmt_scanstatus(
84976 sqlite3_stmt *pStmt, /* Prepared statement being queried */
84977 int idx, /* Index of loop to report on */
84978 int iScanStatusOp, /* Which metric to return */
84979 void *pOut /* OUT: Write the answer here */
84980){
84981 Vdbe *p = (Vdbe*)pStmt;
84982 ScanStatus *pScan;
84983 if( idx<0 || idx>=p->nScan ) return 1;
84984 pScan = &p->aScan[idx];
84985 switch( iScanStatusOp ){
84986 case SQLITE_SCANSTAT_NLOOP: {
84987 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
84988 break;
84989 }
84990 case SQLITE_SCANSTAT_NVISIT: {
84991 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
84992 break;
84993 }
84994 case SQLITE_SCANSTAT_EST: {
84995 double r = 1.0;
84996 LogEst x = pScan->nEst;
84997 while( x<100 ){
84998 x += 10;
84999 r *= 0.5;
85000 }
85001 *(double*)pOut = r*sqlite3LogEstToInt(x);
85002 break;
85003 }
85004 case SQLITE_SCANSTAT_NAME: {
85005 *(const char**)pOut = pScan->zName;
85006 break;
85007 }
85008 case SQLITE_SCANSTAT_EXPLAIN: {
85009 if( pScan->addrExplain ){
85010 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
85011 }else{
85012 *(const char**)pOut = 0;
85013 }
85014 break;
85015 }
85016 case SQLITE_SCANSTAT_SELECTID: {
85017 if( pScan->addrExplain ){
85018 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
85019 }else{
85020 *(int*)pOut = -1;
85021 }
85022 break;
85023 }
85024 default: {
85025 return 1;
85026 }
85027 }
85028 return 0;
85029}
85030
85031/*
85032** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
85033*/
85034SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
85035 Vdbe *p = (Vdbe*)pStmt;
85036 memset(p->anExec, 0, p->nOp * sizeof(i64));
85037}
85038#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
85039
85040/************** End of vdbeapi.c *********************************************/
85041/************** Begin file vdbetrace.c ***************************************/
85042/*
85043** 2009 November 25
85044**
85045** The author disclaims copyright to this source code. In place of
85046** a legal notice, here is a blessing:
85047**
85048** May you do good and not evil.
85049** May you find forgiveness for yourself and forgive others.
85050** May you share freely, never taking more than you give.
85051**
85052*************************************************************************
85053**
85054** This file contains code used to insert the values of host parameters
85055** (aka "wildcards") into the SQL text output by sqlite3_trace().
85056**
85057** The Vdbe parse-tree explainer is also found here.
85058*/
85059/* #include "sqliteInt.h" */
85060/* #include "vdbeInt.h" */
85061
85062#ifndef SQLITE_OMIT_TRACE
85063
85064/*
85065** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
85066** bytes in this text up to but excluding the first character in
85067** a host parameter. If the text contains no host parameters, return
85068** the total number of bytes in the text.
85069*/
85070static int findNextHostParameter(const char *zSql, int *pnToken){
85071 int tokenType;
85072 int nTotal = 0;
85073 int n;
85074
85075 *pnToken = 0;
85076 while( zSql[0] ){
85077 n = sqlite3GetToken((u8*)zSql, &tokenType);
85078 assert( n>0 && tokenType!=TK_ILLEGAL );
85079 if( tokenType==TK_VARIABLE ){
85080 *pnToken = n;
85081 break;
85082 }
85083 nTotal += n;
85084 zSql += n;
85085 }
85086 return nTotal;
85087}
85088
85089/*
85090** This function returns a pointer to a nul-terminated string in memory
85091** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
85092** string contains a copy of zRawSql but with host parameters expanded to
85093** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
85094** then the returned string holds a copy of zRawSql with "-- " prepended
85095** to each line of text.
85096**
85097** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
85098** then long strings and blobs are truncated to that many bytes. This
85099** can be used to prevent unreasonably large trace strings when dealing
85100** with large (multi-megabyte) strings and blobs.
85101**
85102** The calling function is responsible for making sure the memory returned
85103** is eventually freed.
85104**
85105** ALGORITHM: Scan the input string looking for host parameters in any of
85106** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
85107** string literals, quoted identifier names, and comments. For text forms,
85108** the host parameter index is found by scanning the prepared
85109** statement for the corresponding OP_Variable opcode. Once the host
85110** parameter index is known, locate the value in p->aVar[]. Then render
85111** the value as a literal in place of the host parameter name.
85112*/
85113SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
85114 Vdbe *p, /* The prepared statement being evaluated */
85115 const char *zRawSql /* Raw text of the SQL statement */
85116){
85117 sqlite3 *db; /* The database connection */
85118 int idx = 0; /* Index of a host parameter */
85119 int nextIndex = 1; /* Index of next ? host parameter */
85120 int n; /* Length of a token prefix */
85121 int nToken; /* Length of the parameter token */
85122 int i; /* Loop counter */
85123 Mem *pVar; /* Value of a host parameter */
85124 StrAccum out; /* Accumulate the output here */
85125#ifndef SQLITE_OMIT_UTF16
85126 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
85127#endif
85128 char zBase[100]; /* Initial working space */
85129
85130 db = p->db;
85131 sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
85132 db->aLimit[SQLITE_LIMIT_LENGTH]);
85133 if( db->nVdbeExec>1 ){
85134 while( *zRawSql ){
85135 const char *zStart = zRawSql;
85136 while( *(zRawSql++)!='\n' && *zRawSql );
85137 sqlite3_str_append(&out, "-- ", 3);
85138 assert( (zRawSql - zStart) > 0 );
85139 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
85140 }
85141 }else if( p->nVar==0 ){
85142 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
85143 }else{
85144 while( zRawSql[0] ){
85145 n = findNextHostParameter(zRawSql, &nToken);
85146 assert( n>0 );
85147 sqlite3_str_append(&out, zRawSql, n);
85148 zRawSql += n;
85149 assert( zRawSql[0] || nToken==0 );
85150 if( nToken==0 ) break;
85151 if( zRawSql[0]=='?' ){
85152 if( nToken>1 ){
85153 assert( sqlite3Isdigit(zRawSql[1]) );
85154 sqlite3GetInt32(&zRawSql[1], &idx);
85155 }else{
85156 idx = nextIndex;
85157 }
85158 }else{
85159 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
85160 zRawSql[0]=='@' || zRawSql[0]=='#' );
85161 testcase( zRawSql[0]==':' );
85162 testcase( zRawSql[0]=='$' );
85163 testcase( zRawSql[0]=='@' );
85164 testcase( zRawSql[0]=='#' );
85165 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
85166 assert( idx>0 );
85167 }
85168 zRawSql += nToken;
85169 nextIndex = idx + 1;
85170 assert( idx>0 && idx<=p->nVar );
85171 pVar = &p->aVar[idx-1];
85172 if( pVar->flags & MEM_Null ){
85173 sqlite3_str_append(&out, "NULL", 4);
85174 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
85175 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
85176 }else if( pVar->flags & MEM_Real ){
85177 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
85178 }else if( pVar->flags & MEM_Str ){
85179 int nOut; /* Number of bytes of the string text to include in output */
85180#ifndef SQLITE_OMIT_UTF16
85181 u8 enc = ENC(db);
85182 if( enc!=SQLITE_UTF8 ){
85183 memset(&utf8, 0, sizeof(utf8));
85184 utf8.db = db;
85185 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
85186 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
85187 out.accError = SQLITE_NOMEM;
85188 out.nAlloc = 0;
85189 }
85190 pVar = &utf8;
85191 }
85192#endif
85193 nOut = pVar->n;
85194#ifdef SQLITE_TRACE_SIZE_LIMIT
85195 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
85196 nOut = SQLITE_TRACE_SIZE_LIMIT;
85197 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
85198 }
85199#endif
85200 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
85201#ifdef SQLITE_TRACE_SIZE_LIMIT
85202 if( nOut<pVar->n ){
85203 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85204 }
85205#endif
85206#ifndef SQLITE_OMIT_UTF16
85207 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
85208#endif
85209 }else if( pVar->flags & MEM_Zero ){
85210 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
85211 }else{
85212 int nOut; /* Number of bytes of the blob to include in output */
85213 assert( pVar->flags & MEM_Blob );
85214 sqlite3_str_append(&out, "x'", 2);
85215 nOut = pVar->n;
85216#ifdef SQLITE_TRACE_SIZE_LIMIT
85217 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
85218#endif
85219 for(i=0; i<nOut; i++){
85220 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
85221 }
85222 sqlite3_str_append(&out, "'", 1);
85223#ifdef SQLITE_TRACE_SIZE_LIMIT
85224 if( nOut<pVar->n ){
85225 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85226 }
85227#endif
85228 }
85229 }
85230 }
85231 if( out.accError ) sqlite3_str_reset(&out);
85232 return sqlite3StrAccumFinish(&out);
85233}
85234
85235#endif /* #ifndef SQLITE_OMIT_TRACE */
85236
85237/************** End of vdbetrace.c *******************************************/
85238/************** Begin file vdbe.c ********************************************/
85239/*
85240** 2001 September 15
85241**
85242** The author disclaims copyright to this source code. In place of
85243** a legal notice, here is a blessing:
85244**
85245** May you do good and not evil.
85246** May you find forgiveness for yourself and forgive others.
85247** May you share freely, never taking more than you give.
85248**
85249*************************************************************************
85250** The code in this file implements the function that runs the
85251** bytecode of a prepared statement.
85252**
85253** Various scripts scan this source file in order to generate HTML
85254** documentation, headers files, or other derived files. The formatting
85255** of the code in this file is, therefore, important. See other comments
85256** in this file for details. If in doubt, do not deviate from existing
85257** commenting and indentation practices when changing or adding code.
85258*/
85259/* #include "sqliteInt.h" */
85260/* #include "vdbeInt.h" */
85261
85262/*
85263** Invoke this macro on memory cells just prior to changing the
85264** value of the cell. This macro verifies that shallow copies are
85265** not misused. A shallow copy of a string or blob just copies a
85266** pointer to the string or blob, not the content. If the original
85267** is changed while the copy is still in use, the string or blob might
85268** be changed out from under the copy. This macro verifies that nothing
85269** like that ever happens.
85270*/
85271#ifdef SQLITE_DEBUG
85272# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
85273#else
85274# define memAboutToChange(P,M)
85275#endif
85276
85277/*
85278** The following global variable is incremented every time a cursor
85279** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
85280** procedures use this information to make sure that indices are
85281** working correctly. This variable has no function other than to
85282** help verify the correct operation of the library.
85283*/
85284#ifdef SQLITE_TEST
85285SQLITE_API int sqlite3_search_count = 0;
85286#endif
85287
85288/*
85289** When this global variable is positive, it gets decremented once before
85290** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
85291** field of the sqlite3 structure is set in order to simulate an interrupt.
85292**
85293** This facility is used for testing purposes only. It does not function
85294** in an ordinary build.
85295*/
85296#ifdef SQLITE_TEST
85297SQLITE_API int sqlite3_interrupt_count = 0;
85298#endif
85299
85300/*
85301** The next global variable is incremented each type the OP_Sort opcode
85302** is executed. The test procedures use this information to make sure that
85303** sorting is occurring or not occurring at appropriate times. This variable
85304** has no function other than to help verify the correct operation of the
85305** library.
85306*/
85307#ifdef SQLITE_TEST
85308SQLITE_API int sqlite3_sort_count = 0;
85309#endif
85310
85311/*
85312** The next global variable records the size of the largest MEM_Blob
85313** or MEM_Str that has been used by a VDBE opcode. The test procedures
85314** use this information to make sure that the zero-blob functionality
85315** is working correctly. This variable has no function other than to
85316** help verify the correct operation of the library.
85317*/
85318#ifdef SQLITE_TEST
85319SQLITE_API int sqlite3_max_blobsize = 0;
85320static void updateMaxBlobsize(Mem *p){
85321 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
85322 sqlite3_max_blobsize = p->n;
85323 }
85324}
85325#endif
85326
85327/*
85328** This macro evaluates to true if either the update hook or the preupdate
85329** hook are enabled for database connect DB.
85330*/
85331#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
85332# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
85333#else
85334# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
85335#endif
85336
85337/*
85338** The next global variable is incremented each time the OP_Found opcode
85339** is executed. This is used to test whether or not the foreign key
85340** operation implemented using OP_FkIsZero is working. This variable
85341** has no function other than to help verify the correct operation of the
85342** library.
85343*/
85344#ifdef SQLITE_TEST
85345SQLITE_API int sqlite3_found_count = 0;
85346#endif
85347
85348/*
85349** Test a register to see if it exceeds the current maximum blob size.
85350** If it does, record the new maximum blob size.
85351*/
85352#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
85353# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
85354#else
85355# define UPDATE_MAX_BLOBSIZE(P)
85356#endif
85357
85358#ifdef SQLITE_DEBUG
85359/* This routine provides a convenient place to set a breakpoint during
85360** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
85361** each opcode is printed. Variables "pc" (program counter) and pOp are
85362** available to add conditionals to the breakpoint. GDB example:
85363**
85364** break test_trace_breakpoint if pc=22
85365**
85366** Other useful labels for breakpoints include:
85367** test_addop_breakpoint(pc,pOp)
85368** sqlite3CorruptError(lineno)
85369** sqlite3MisuseError(lineno)
85370** sqlite3CantopenError(lineno)
85371*/
85372static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
85373 static int n = 0;
85374 n++;
85375}
85376#endif
85377
85378/*
85379** Invoke the VDBE coverage callback, if that callback is defined. This
85380** feature is used for test suite validation only and does not appear an
85381** production builds.
85382**
85383** M is the type of branch. I is the direction taken for this instance of
85384** the branch.
85385**
85386** M: 2 - two-way branch (I=0: fall-thru 1: jump )
85387** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
85388** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
85389**
85390** In other words, if M is 2, then I is either 0 (for fall-through) or
85391** 1 (for when the branch is taken). If M is 3, the I is 0 for an
85392** ordinary fall-through, I is 1 if the branch was taken, and I is 2
85393** if the result of comparison is NULL. For M=3, I=2 the jump may or
85394** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
85395** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
85396** depending on if the operands are less than, equal, or greater than.
85397**
85398** iSrcLine is the source code line (from the __LINE__ macro) that
85399** generated the VDBE instruction combined with flag bits. The source
85400** code line number is in the lower 24 bits of iSrcLine and the upper
85401** 8 bytes are flags. The lower three bits of the flags indicate
85402** values for I that should never occur. For example, if the branch is
85403** always taken, the flags should be 0x05 since the fall-through and
85404** alternate branch are never taken. If a branch is never taken then
85405** flags should be 0x06 since only the fall-through approach is allowed.
85406**
85407** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
85408** interested in equal or not-equal. In other words, I==0 and I==2
85409** should be treated as equivalent
85410**
85411** Since only a line number is retained, not the filename, this macro
85412** only works for amalgamation builds. But that is ok, since these macros
85413** should be no-ops except for special builds used to measure test coverage.
85414*/
85415#if !defined(SQLITE_VDBE_COVERAGE)
85416# define VdbeBranchTaken(I,M)
85417#else
85418# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
85419 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
85420 u8 mNever;
85421 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
85422 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
85423 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
85424 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
85425 I = 1<<I;
85426 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
85427 ** the flags indicate directions that the branch can never go. If
85428 ** a branch really does go in one of those directions, assert right
85429 ** away. */
85430 mNever = iSrcLine >> 24;
85431 assert( (I & mNever)==0 );
85432 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
85433 /* Invoke the branch coverage callback with three arguments:
85434 ** iSrcLine - the line number of the VdbeCoverage() macro, with
85435 ** flags removed.
85436 ** I - Mask of bits 0x07 indicating which cases are are
85437 ** fulfilled by this instance of the jump. 0x01 means
85438 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
85439 ** impossible cases (ex: if the comparison is never NULL)
85440 ** are filled in automatically so that the coverage
85441 ** measurement logic does not flag those impossible cases
85442 ** as missed coverage.
85443 ** M - Type of jump. Same as M argument above
85444 */
85445 I |= mNever;
85446 if( M==2 ) I |= 0x04;
85447 if( M==4 ){
85448 I |= 0x08;
85449 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
85450 }
85451 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
85452 iSrcLine&0xffffff, I, M);
85453 }
85454#endif
85455
85456/*
85457** An ephemeral string value (signified by the MEM_Ephem flag) contains
85458** a pointer to a dynamically allocated string where some other entity
85459** is responsible for deallocating that string. Because the register
85460** does not control the string, it might be deleted without the register
85461** knowing it.
85462**
85463** This routine converts an ephemeral string into a dynamically allocated
85464** string that the register itself controls. In other words, it
85465** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
85466*/
85467#define Deephemeralize(P) \
85468 if( ((P)->flags&MEM_Ephem)!=0 \
85469 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
85470
85471/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
85472#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
85473
85474/*
85475** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
85476** if we run out of memory.
85477*/
85478static VdbeCursor *allocateCursor(
85479 Vdbe *p, /* The virtual machine */
85480 int iCur, /* Index of the new VdbeCursor */
85481 int nField, /* Number of fields in the table or index */
85482 int iDb, /* Database the cursor belongs to, or -1 */
85483 u8 eCurType /* Type of the new cursor */
85484){
85485 /* Find the memory cell that will be used to store the blob of memory
85486 ** required for this VdbeCursor structure. It is convenient to use a
85487 ** vdbe memory cell to manage the memory allocation required for a
85488 ** VdbeCursor structure for the following reasons:
85489 **
85490 ** * Sometimes cursor numbers are used for a couple of different
85491 ** purposes in a vdbe program. The different uses might require
85492 ** different sized allocations. Memory cells provide growable
85493 ** allocations.
85494 **
85495 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
85496 ** be freed lazily via the sqlite3_release_memory() API. This
85497 ** minimizes the number of malloc calls made by the system.
85498 **
85499 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
85500 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
85501 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
85502 */
85503 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
85504
85505 int nByte;
85506 VdbeCursor *pCx = 0;
85507 nByte =
85508 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
85509 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
85510
85511 assert( iCur>=0 && iCur<p->nCursor );
85512 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
85513 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
85514 ** is clear. Otherwise, if this is an ephemeral cursor created by
85515 ** OP_OpenDup, the cursor will not be closed and will still be part
85516 ** of a BtShared.pCursor list. */
85517 if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
85518 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
85519 p->apCsr[iCur] = 0;
85520 }
85521 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
85522 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
85523 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
85524 pCx->eCurType = eCurType;
85525 pCx->iDb = iDb;
85526 pCx->nField = nField;
85527 pCx->aOffset = &pCx->aType[nField];
85528 if( eCurType==CURTYPE_BTREE ){
85529 pCx->uc.pCursor = (BtCursor*)
85530 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
85531 sqlite3BtreeCursorZero(pCx->uc.pCursor);
85532 }
85533 }
85534 return pCx;
85535}
85536
85537/*
85538** The string in pRec is known to look like an integer and to have a
85539** floating point value of rValue. Return true and set *piValue to the
85540** integer value if the string is in range to be an integer. Otherwise,
85541** return false.
85542*/
85543static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
85544 i64 iValue = (double)rValue;
85545 if( sqlite3RealSameAsInt(rValue,iValue) ){
85546 *piValue = iValue;
85547 return 1;
85548 }
85549 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
85550}
85551
85552/*
85553** Try to convert a value into a numeric representation if we can
85554** do so without loss of information. In other words, if the string
85555** looks like a number, convert it into a number. If it does not
85556** look like a number, leave it alone.
85557**
85558** If the bTryForInt flag is true, then extra effort is made to give
85559** an integer representation. Strings that look like floating point
85560** values but which have no fractional component (example: '48.00')
85561** will have a MEM_Int representation when bTryForInt is true.
85562**
85563** If bTryForInt is false, then if the input string contains a decimal
85564** point or exponential notation, the result is only MEM_Real, even
85565** if there is an exact integer representation of the quantity.
85566*/
85567static void applyNumericAffinity(Mem *pRec, int bTryForInt){
85568 double rValue;
85569 u8 enc = pRec->enc;
85570 int rc;
85571 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
85572 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
85573 if( rc<=0 ) return;
85574 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
85575 pRec->flags |= MEM_Int;
85576 }else{
85577 pRec->u.r = rValue;
85578 pRec->flags |= MEM_Real;
85579 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
85580 }
85581 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
85582 ** string representation after computing a numeric equivalent, because the
85583 ** string representation might not be the canonical representation for the
85584 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
85585 pRec->flags &= ~MEM_Str;
85586}
85587
85588/*
85589** Processing is determine by the affinity parameter:
85590**
85591** SQLITE_AFF_INTEGER:
85592** SQLITE_AFF_REAL:
85593** SQLITE_AFF_NUMERIC:
85594** Try to convert pRec to an integer representation or a
85595** floating-point representation if an integer representation
85596** is not possible. Note that the integer representation is
85597** always preferred, even if the affinity is REAL, because
85598** an integer representation is more space efficient on disk.
85599**
85600** SQLITE_AFF_TEXT:
85601** Convert pRec to a text representation.
85602**
85603** SQLITE_AFF_BLOB:
85604** SQLITE_AFF_NONE:
85605** No-op. pRec is unchanged.
85606*/
85607static void applyAffinity(
85608 Mem *pRec, /* The value to apply affinity to */
85609 char affinity, /* The affinity to be applied */
85610 u8 enc /* Use this text encoding */
85611){
85612 if( affinity>=SQLITE_AFF_NUMERIC ){
85613 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
85614 || affinity==SQLITE_AFF_NUMERIC );
85615 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
85616 if( (pRec->flags & MEM_Real)==0 ){
85617 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
85618 }else{
85619 sqlite3VdbeIntegerAffinity(pRec);
85620 }
85621 }
85622 }else if( affinity==SQLITE_AFF_TEXT ){
85623 /* Only attempt the conversion to TEXT if there is an integer or real
85624 ** representation (blob and NULL do not get converted) but no string
85625 ** representation. It would be harmless to repeat the conversion if
85626 ** there is already a string rep, but it is pointless to waste those
85627 ** CPU cycles. */
85628 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
85629 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
85630 testcase( pRec->flags & MEM_Int );
85631 testcase( pRec->flags & MEM_Real );
85632 testcase( pRec->flags & MEM_IntReal );
85633 sqlite3VdbeMemStringify(pRec, enc, 1);
85634 }
85635 }
85636 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
85637 }
85638}
85639
85640/*
85641** Try to convert the type of a function argument or a result column
85642** into a numeric representation. Use either INTEGER or REAL whichever
85643** is appropriate. But only do the conversion if it is possible without
85644** loss of information and return the revised type of the argument.
85645*/
85646SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
85647 int eType = sqlite3_value_type(pVal);
85648 if( eType==SQLITE_TEXT ){
85649 Mem *pMem = (Mem*)pVal;
85650 applyNumericAffinity(pMem, 0);
85651 eType = sqlite3_value_type(pVal);
85652 }
85653 return eType;
85654}
85655
85656/*
85657** Exported version of applyAffinity(). This one works on sqlite3_value*,
85658** not the internal Mem* type.
85659*/
85660SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
85661 sqlite3_value *pVal,
85662 u8 affinity,
85663 u8 enc
85664){
85665 applyAffinity((Mem *)pVal, affinity, enc);
85666}
85667
85668/*
85669** pMem currently only holds a string type (or maybe a BLOB that we can
85670** interpret as a string if we want to). Compute its corresponding
85671** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
85672** accordingly.
85673*/
85674static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
85675 int rc;
85676 sqlite3_int64 ix;
85677 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
85678 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
85679 ExpandBlob(pMem);
85680 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
85681 if( rc<=0 ){
85682 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
85683 pMem->u.i = ix;
85684 return MEM_Int;
85685 }else{
85686 return MEM_Real;
85687 }
85688 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
85689 pMem->u.i = ix;
85690 return MEM_Int;
85691 }
85692 return MEM_Real;
85693}
85694
85695/*
85696** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
85697** none.
85698**
85699** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
85700** But it does set pMem->u.r and pMem->u.i appropriately.
85701*/
85702static u16 numericType(Mem *pMem){
85703 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
85704 testcase( pMem->flags & MEM_Int );
85705 testcase( pMem->flags & MEM_Real );
85706 testcase( pMem->flags & MEM_IntReal );
85707 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
85708 }
85709 if( pMem->flags & (MEM_Str|MEM_Blob) ){
85710 testcase( pMem->flags & MEM_Str );
85711 testcase( pMem->flags & MEM_Blob );
85712 return computeNumericType(pMem);
85713 }
85714 return 0;
85715}
85716
85717#ifdef SQLITE_DEBUG
85718/*
85719** Write a nice string representation of the contents of cell pMem
85720** into buffer zBuf, length nBuf.
85721*/
85722SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
85723 int f = pMem->flags;
85724 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
85725 if( f&MEM_Blob ){
85726 int i;
85727 char c;
85728 if( f & MEM_Dyn ){
85729 c = 'z';
85730 assert( (f & (MEM_Static|MEM_Ephem))==0 );
85731 }else if( f & MEM_Static ){
85732 c = 't';
85733 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
85734 }else if( f & MEM_Ephem ){
85735 c = 'e';
85736 assert( (f & (MEM_Static|MEM_Dyn))==0 );
85737 }else{
85738 c = 's';
85739 }
85740 sqlite3_str_appendf(pStr, "%cx[", c);
85741 for(i=0; i<25 && i<pMem->n; i++){
85742 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
85743 }
85744 sqlite3_str_appendf(pStr, "|");
85745 for(i=0; i<25 && i<pMem->n; i++){
85746 char z = pMem->z[i];
85747 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
85748 }
85749 sqlite3_str_appendf(pStr,"]");
85750 if( f & MEM_Zero ){
85751 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
85752 }
85753 }else if( f & MEM_Str ){
85754 int j;
85755 u8 c;
85756 if( f & MEM_Dyn ){
85757 c = 'z';
85758 assert( (f & (MEM_Static|MEM_Ephem))==0 );
85759 }else if( f & MEM_Static ){
85760 c = 't';
85761 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
85762 }else if( f & MEM_Ephem ){
85763 c = 'e';
85764 assert( (f & (MEM_Static|MEM_Dyn))==0 );
85765 }else{
85766 c = 's';
85767 }
85768 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
85769 for(j=0; j<25 && j<pMem->n; j++){
85770 c = pMem->z[j];
85771 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
85772 }
85773 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
85774 }
85775}
85776#endif
85777
85778#ifdef SQLITE_DEBUG
85779/*
85780** Print the value of a register for tracing purposes:
85781*/
85782static void memTracePrint(Mem *p){
85783 if( p->flags & MEM_Undefined ){
85784 printf(" undefined");
85785 }else if( p->flags & MEM_Null ){
85786 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
85787 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
85788 printf(" si:%lld", p->u.i);
85789 }else if( (p->flags & (MEM_IntReal))!=0 ){
85790 printf(" ir:%lld", p->u.i);
85791 }else if( p->flags & MEM_Int ){
85792 printf(" i:%lld", p->u.i);
85793#ifndef SQLITE_OMIT_FLOATING_POINT
85794 }else if( p->flags & MEM_Real ){
85795 printf(" r:%.17g", p->u.r);
85796#endif
85797 }else if( sqlite3VdbeMemIsRowSet(p) ){
85798 printf(" (rowset)");
85799 }else{
85800 StrAccum acc;
85801 char zBuf[1000];
85802 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
85803 sqlite3VdbeMemPrettyPrint(p, &acc);
85804 printf(" %s", sqlite3StrAccumFinish(&acc));
85805 }
85806 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
85807}
85808static void registerTrace(int iReg, Mem *p){
85809 printf("R[%d] = ", iReg);
85810 memTracePrint(p);
85811 if( p->pScopyFrom ){
85812 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
85813 }
85814 printf("\n");
85815 sqlite3VdbeCheckMemInvariants(p);
85816}
85817#endif
85818
85819#ifdef SQLITE_DEBUG
85820/*
85821** Show the values of all registers in the virtual machine. Used for
85822** interactive debugging.
85823*/
85824SQLITE_PRIVATE void sqlite3VdbeRegisterDump(Vdbe *v){
85825 int i;
85826 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
85827}
85828#endif /* SQLITE_DEBUG */
85829
85830
85831#ifdef SQLITE_DEBUG
85832# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
85833#else
85834# define REGISTER_TRACE(R,M)
85835#endif
85836
85837
85838#ifdef VDBE_PROFILE
85839
85840/*
85841** hwtime.h contains inline assembler code for implementing
85842** high-performance timing routines.
85843*/
85844/************** Include hwtime.h in the middle of vdbe.c *********************/
85845/************** Begin file hwtime.h ******************************************/
85846/*
85847** 2008 May 27
85848**
85849** The author disclaims copyright to this source code. In place of
85850** a legal notice, here is a blessing:
85851**
85852** May you do good and not evil.
85853** May you find forgiveness for yourself and forgive others.
85854** May you share freely, never taking more than you give.
85855**
85856******************************************************************************
85857**
85858** This file contains inline asm code for retrieving "high-performance"
85859** counters for x86 and x86_64 class CPUs.
85860*/
85861#ifndef SQLITE_HWTIME_H
85862#define SQLITE_HWTIME_H
85863
85864/*
85865** The following routine only works on pentium-class (or newer) processors.
85866** It uses the RDTSC opcode to read the cycle count value out of the
85867** processor and returns that value. This can be used for high-res
85868** profiling.
85869*/
85870#if !defined(__STRICT_ANSI__) && \
85871 (defined(__GNUC__) || defined(_MSC_VER)) && \
85872 (defined(i386) || defined(__i386__) || defined(_M_IX86))
85873
85874 #if defined(__GNUC__)
85875
85876 __inline__ sqlite_uint64 sqlite3Hwtime(void){
85877 unsigned int lo, hi;
85878 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
85879 return (sqlite_uint64)hi << 32 | lo;
85880 }
85881
85882 #elif defined(_MSC_VER)
85883
85884 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
85885 __asm {
85886 rdtsc
85887 ret ; return value at EDX:EAX
85888 }
85889 }
85890
85891 #endif
85892
85893#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__))
85894
85895 __inline__ sqlite_uint64 sqlite3Hwtime(void){
85896 unsigned long val;
85897 __asm__ __volatile__ ("rdtsc" : "=A" (val));
85898 return val;
85899 }
85900
85901#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__))
85902
85903 __inline__ sqlite_uint64 sqlite3Hwtime(void){
85904 unsigned long long retval;
85905 unsigned long junk;
85906 __asm__ __volatile__ ("\n\
85907 1: mftbu %1\n\
85908 mftb %L0\n\
85909 mftbu %0\n\
85910 cmpw %0,%1\n\
85911 bne 1b"
85912 : "=r" (retval), "=r" (junk));
85913 return retval;
85914 }
85915
85916#else
85917
85918 /*
85919 ** asm() is needed for hardware timing support. Without asm(),
85920 ** disable the sqlite3Hwtime() routine.
85921 **
85922 ** sqlite3Hwtime() is only used for some obscure debugging
85923 ** and analysis configurations, not in any deliverable, so this
85924 ** should not be a great loss.
85925 */
85926SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
85927
85928#endif
85929
85930#endif /* !defined(SQLITE_HWTIME_H) */
85931
85932/************** End of hwtime.h **********************************************/
85933/************** Continuing where we left off in vdbe.c ***********************/
85934
85935#endif
85936
85937#ifndef NDEBUG
85938/*
85939** This function is only called from within an assert() expression. It
85940** checks that the sqlite3.nTransaction variable is correctly set to
85941** the number of non-transaction savepoints currently in the
85942** linked list starting at sqlite3.pSavepoint.
85943**
85944** Usage:
85945**
85946** assert( checkSavepointCount(db) );
85947*/
85948static int checkSavepointCount(sqlite3 *db){
85949 int n = 0;
85950 Savepoint *p;
85951 for(p=db->pSavepoint; p; p=p->pNext) n++;
85952 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
85953 return 1;
85954}
85955#endif
85956
85957/*
85958** Return the register of pOp->p2 after first preparing it to be
85959** overwritten with an integer value.
85960*/
85961static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
85962 sqlite3VdbeMemSetNull(pOut);
85963 pOut->flags = MEM_Int;
85964 return pOut;
85965}
85966static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
85967 Mem *pOut;
85968 assert( pOp->p2>0 );
85969 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
85970 pOut = &p->aMem[pOp->p2];
85971 memAboutToChange(p, pOut);
85972 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
85973 return out2PrereleaseWithClear(pOut);
85974 }else{
85975 pOut->flags = MEM_Int;
85976 return pOut;
85977 }
85978}
85979
85980
85981/*
85982** Execute as much of a VDBE program as we can.
85983** This is the core of sqlite3_step().
85984*/
85985SQLITE_PRIVATE int sqlite3VdbeExec(
85986 Vdbe *p /* The VDBE */
85987){
85988 Op *aOp = p->aOp; /* Copy of p->aOp */
85989 Op *pOp = aOp; /* Current operation */
85990#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
85991 Op *pOrigOp; /* Value of pOp at the top of the loop */
85992#endif
85993#ifdef SQLITE_DEBUG
85994 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
85995#endif
85996 int rc = SQLITE_OK; /* Value to return */
85997 sqlite3 *db = p->db; /* The database */
85998 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
85999 u8 encoding = ENC(db); /* The database encoding */
86000 int iCompare = 0; /* Result of last comparison */
86001 u64 nVmStep = 0; /* Number of virtual machine steps */
86002#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
86003 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
86004#endif
86005 Mem *aMem = p->aMem; /* Copy of p->aMem */
86006 Mem *pIn1 = 0; /* 1st input operand */
86007 Mem *pIn2 = 0; /* 2nd input operand */
86008 Mem *pIn3 = 0; /* 3rd input operand */
86009 Mem *pOut = 0; /* Output operand */
86010#ifdef VDBE_PROFILE
86011 u64 start; /* CPU clock count at start of opcode */
86012#endif
86013 /*** INSERT STACK UNION HERE ***/
86014
86015 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
86016 sqlite3VdbeEnter(p);
86017#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
86018 if( db->xProgress ){
86019 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
86020 assert( 0 < db->nProgressOps );
86021 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
86022 }else{
86023 nProgressLimit = LARGEST_UINT64;
86024 }
86025#endif
86026 if( p->rc==SQLITE_NOMEM ){
86027 /* This happens if a malloc() inside a call to sqlite3_column_text() or
86028 ** sqlite3_column_text16() failed. */
86029 goto no_mem;
86030 }
86031 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
86032 testcase( p->rc!=SQLITE_OK );
86033 p->rc = SQLITE_OK;
86034 assert( p->bIsReader || p->readOnly!=0 );
86035 p->iCurrentTime = 0;
86036 assert( p->explain==0 );
86037 p->pResultSet = 0;
86038 db->busyHandler.nBusy = 0;
86039 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
86040 sqlite3VdbeIOTraceSql(p);
86041#ifdef SQLITE_DEBUG
86042 sqlite3BeginBenignMalloc();
86043 if( p->pc==0
86044 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
86045 ){
86046 int i;
86047 int once = 1;
86048 sqlite3VdbePrintSql(p);
86049 if( p->db->flags & SQLITE_VdbeListing ){
86050 printf("VDBE Program Listing:\n");
86051 for(i=0; i<p->nOp; i++){
86052 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
86053 }
86054 }
86055 if( p->db->flags & SQLITE_VdbeEQP ){
86056 for(i=0; i<p->nOp; i++){
86057 if( aOp[i].opcode==OP_Explain ){
86058 if( once ) printf("VDBE Query Plan:\n");
86059 printf("%s\n", aOp[i].p4.z);
86060 once = 0;
86061 }
86062 }
86063 }
86064 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
86065 }
86066 sqlite3EndBenignMalloc();
86067#endif
86068 for(pOp=&aOp[p->pc]; 1; pOp++){
86069 /* Errors are detected by individual opcodes, with an immediate
86070 ** jumps to abort_due_to_error. */
86071 assert( rc==SQLITE_OK );
86072
86073 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
86074#ifdef VDBE_PROFILE
86075 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
86076#endif
86077 nVmStep++;
86078#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
86079 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
86080#endif
86081
86082 /* Only allow tracing if SQLITE_DEBUG is defined.
86083 */
86084#ifdef SQLITE_DEBUG
86085 if( db->flags & SQLITE_VdbeTrace ){
86086 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
86087 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
86088 }
86089#endif
86090
86091
86092 /* Check to see if we need to simulate an interrupt. This only happens
86093 ** if we have a special test build.
86094 */
86095#ifdef SQLITE_TEST
86096 if( sqlite3_interrupt_count>0 ){
86097 sqlite3_interrupt_count--;
86098 if( sqlite3_interrupt_count==0 ){
86099 sqlite3_interrupt(db);
86100 }
86101 }
86102#endif
86103
86104 /* Sanity checking on other operands */
86105#ifdef SQLITE_DEBUG
86106 {
86107 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
86108 if( (opProperty & OPFLG_IN1)!=0 ){
86109 assert( pOp->p1>0 );
86110 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
86111 assert( memIsValid(&aMem[pOp->p1]) );
86112 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
86113 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
86114 }
86115 if( (opProperty & OPFLG_IN2)!=0 ){
86116 assert( pOp->p2>0 );
86117 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
86118 assert( memIsValid(&aMem[pOp->p2]) );
86119 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
86120 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
86121 }
86122 if( (opProperty & OPFLG_IN3)!=0 ){
86123 assert( pOp->p3>0 );
86124 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86125 assert( memIsValid(&aMem[pOp->p3]) );
86126 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
86127 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
86128 }
86129 if( (opProperty & OPFLG_OUT2)!=0 ){
86130 assert( pOp->p2>0 );
86131 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
86132 memAboutToChange(p, &aMem[pOp->p2]);
86133 }
86134 if( (opProperty & OPFLG_OUT3)!=0 ){
86135 assert( pOp->p3>0 );
86136 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86137 memAboutToChange(p, &aMem[pOp->p3]);
86138 }
86139 }
86140#endif
86141#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
86142 pOrigOp = pOp;
86143#endif
86144
86145 switch( pOp->opcode ){
86146
86147/*****************************************************************************
86148** What follows is a massive switch statement where each case implements a
86149** separate instruction in the virtual machine. If we follow the usual
86150** indentation conventions, each case should be indented by 6 spaces. But
86151** that is a lot of wasted space on the left margin. So the code within
86152** the switch statement will break with convention and be flush-left. Another
86153** big comment (similar to this one) will mark the point in the code where
86154** we transition back to normal indentation.
86155**
86156** The formatting of each case is important. The makefile for SQLite
86157** generates two C files "opcodes.h" and "opcodes.c" by scanning this
86158** file looking for lines that begin with "case OP_". The opcodes.h files
86159** will be filled with #defines that give unique integer values to each
86160** opcode and the opcodes.c file is filled with an array of strings where
86161** each string is the symbolic name for the corresponding opcode. If the
86162** case statement is followed by a comment of the form "/# same as ... #/"
86163** that comment is used to determine the particular value of the opcode.
86164**
86165** Other keywords in the comment that follows each case are used to
86166** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
86167** Keywords include: in1, in2, in3, out2, out3. See
86168** the mkopcodeh.awk script for additional information.
86169**
86170** Documentation about VDBE opcodes is generated by scanning this file
86171** for lines of that contain "Opcode:". That line and all subsequent
86172** comment lines are used in the generation of the opcode.html documentation
86173** file.
86174**
86175** SUMMARY:
86176**
86177** Formatting is important to scripts that scan this file.
86178** Do not deviate from the formatting style currently in use.
86179**
86180*****************************************************************************/
86181
86182/* Opcode: Goto * P2 * * *
86183**
86184** An unconditional jump to address P2.
86185** The next instruction executed will be
86186** the one at index P2 from the beginning of
86187** the program.
86188**
86189** The P1 parameter is not actually used by this opcode. However, it
86190** is sometimes set to 1 instead of 0 as a hint to the command-line shell
86191** that this Goto is the bottom of a loop and that the lines from P2 down
86192** to the current line should be indented for EXPLAIN output.
86193*/
86194case OP_Goto: { /* jump */
86195
86196#ifdef SQLITE_DEBUG
86197 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
86198 ** means we should really jump back to the preceeding OP_ReleaseReg
86199 ** instruction. */
86200 if( pOp->p5 ){
86201 assert( pOp->p2 < (int)(pOp - aOp) );
86202 assert( pOp->p2 > 1 );
86203 pOp = &aOp[pOp->p2 - 2];
86204 assert( pOp[1].opcode==OP_ReleaseReg );
86205 goto check_for_interrupt;
86206 }
86207#endif
86208
86209jump_to_p2_and_check_for_interrupt:
86210 pOp = &aOp[pOp->p2 - 1];
86211
86212 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
86213 ** OP_VNext, or OP_SorterNext) all jump here upon
86214 ** completion. Check to see if sqlite3_interrupt() has been called
86215 ** or if the progress callback needs to be invoked.
86216 **
86217 ** This code uses unstructured "goto" statements and does not look clean.
86218 ** But that is not due to sloppy coding habits. The code is written this
86219 ** way for performance, to avoid having to run the interrupt and progress
86220 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
86221 ** faster according to "valgrind --tool=cachegrind" */
86222check_for_interrupt:
86223 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
86224#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
86225 /* Call the progress callback if it is configured and the required number
86226 ** of VDBE ops have been executed (either since this invocation of
86227 ** sqlite3VdbeExec() or since last time the progress callback was called).
86228 ** If the progress callback returns non-zero, exit the virtual machine with
86229 ** a return code SQLITE_ABORT.
86230 */
86231 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
86232 assert( db->nProgressOps!=0 );
86233 nProgressLimit += db->nProgressOps;
86234 if( db->xProgress(db->pProgressArg) ){
86235 nProgressLimit = LARGEST_UINT64;
86236 rc = SQLITE_INTERRUPT;
86237 goto abort_due_to_error;
86238 }
86239 }
86240#endif
86241
86242 break;
86243}
86244
86245/* Opcode: Gosub P1 P2 * * *
86246**
86247** Write the current address onto register P1
86248** and then jump to address P2.
86249*/
86250case OP_Gosub: { /* jump */
86251 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86252 pIn1 = &aMem[pOp->p1];
86253 assert( VdbeMemDynamic(pIn1)==0 );
86254 memAboutToChange(p, pIn1);
86255 pIn1->flags = MEM_Int;
86256 pIn1->u.i = (int)(pOp-aOp);
86257 REGISTER_TRACE(pOp->p1, pIn1);
86258
86259 /* Most jump operations do a goto to this spot in order to update
86260 ** the pOp pointer. */
86261jump_to_p2:
86262 pOp = &aOp[pOp->p2 - 1];
86263 break;
86264}
86265
86266/* Opcode: Return P1 * * * *
86267**
86268** Jump to the next instruction after the address in register P1. After
86269** the jump, register P1 becomes undefined.
86270*/
86271case OP_Return: { /* in1 */
86272 pIn1 = &aMem[pOp->p1];
86273 assert( pIn1->flags==MEM_Int );
86274 pOp = &aOp[pIn1->u.i];
86275 pIn1->flags = MEM_Undefined;
86276 break;
86277}
86278
86279/* Opcode: InitCoroutine P1 P2 P3 * *
86280**
86281** Set up register P1 so that it will Yield to the coroutine
86282** located at address P3.
86283**
86284** If P2!=0 then the coroutine implementation immediately follows
86285** this opcode. So jump over the coroutine implementation to
86286** address P2.
86287**
86288** See also: EndCoroutine
86289*/
86290case OP_InitCoroutine: { /* jump */
86291 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86292 assert( pOp->p2>=0 && pOp->p2<p->nOp );
86293 assert( pOp->p3>=0 && pOp->p3<p->nOp );
86294 pOut = &aMem[pOp->p1];
86295 assert( !VdbeMemDynamic(pOut) );
86296 pOut->u.i = pOp->p3 - 1;
86297 pOut->flags = MEM_Int;
86298 if( pOp->p2 ) goto jump_to_p2;
86299 break;
86300}
86301
86302/* Opcode: EndCoroutine P1 * * * *
86303**
86304** The instruction at the address in register P1 is a Yield.
86305** Jump to the P2 parameter of that Yield.
86306** After the jump, register P1 becomes undefined.
86307**
86308** See also: InitCoroutine
86309*/
86310case OP_EndCoroutine: { /* in1 */
86311 VdbeOp *pCaller;
86312 pIn1 = &aMem[pOp->p1];
86313 assert( pIn1->flags==MEM_Int );
86314 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
86315 pCaller = &aOp[pIn1->u.i];
86316 assert( pCaller->opcode==OP_Yield );
86317 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
86318 pOp = &aOp[pCaller->p2 - 1];
86319 pIn1->flags = MEM_Undefined;
86320 break;
86321}
86322
86323/* Opcode: Yield P1 P2 * * *
86324**
86325** Swap the program counter with the value in register P1. This
86326** has the effect of yielding to a coroutine.
86327**
86328** If the coroutine that is launched by this instruction ends with
86329** Yield or Return then continue to the next instruction. But if
86330** the coroutine launched by this instruction ends with
86331** EndCoroutine, then jump to P2 rather than continuing with the
86332** next instruction.
86333**
86334** See also: InitCoroutine
86335*/
86336case OP_Yield: { /* in1, jump */
86337 int pcDest;
86338 pIn1 = &aMem[pOp->p1];
86339 assert( VdbeMemDynamic(pIn1)==0 );
86340 pIn1->flags = MEM_Int;
86341 pcDest = (int)pIn1->u.i;
86342 pIn1->u.i = (int)(pOp - aOp);
86343 REGISTER_TRACE(pOp->p1, pIn1);
86344 pOp = &aOp[pcDest];
86345 break;
86346}
86347
86348/* Opcode: HaltIfNull P1 P2 P3 P4 P5
86349** Synopsis: if r[P3]=null halt
86350**
86351** Check the value in register P3. If it is NULL then Halt using
86352** parameter P1, P2, and P4 as if this were a Halt instruction. If the
86353** value in register P3 is not NULL, then this routine is a no-op.
86354** The P5 parameter should be 1.
86355*/
86356case OP_HaltIfNull: { /* in3 */
86357 pIn3 = &aMem[pOp->p3];
86358#ifdef SQLITE_DEBUG
86359 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
86360#endif
86361 if( (pIn3->flags & MEM_Null)==0 ) break;
86362 /* Fall through into OP_Halt */
86363 /* no break */ deliberate_fall_through
86364}
86365
86366/* Opcode: Halt P1 P2 * P4 P5
86367**
86368** Exit immediately. All open cursors, etc are closed
86369** automatically.
86370**
86371** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
86372** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
86373** For errors, it can be some other value. If P1!=0 then P2 will determine
86374** whether or not to rollback the current transaction. Do not rollback
86375** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
86376** then back out all changes that have occurred during this execution of the
86377** VDBE, but do not rollback the transaction.
86378**
86379** If P4 is not null then it is an error message string.
86380**
86381** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
86382**
86383** 0: (no change)
86384** 1: NOT NULL contraint failed: P4
86385** 2: UNIQUE constraint failed: P4
86386** 3: CHECK constraint failed: P4
86387** 4: FOREIGN KEY constraint failed: P4
86388**
86389** If P5 is not zero and P4 is NULL, then everything after the ":" is
86390** omitted.
86391**
86392** There is an implied "Halt 0 0 0" instruction inserted at the very end of
86393** every program. So a jump past the last instruction of the program
86394** is the same as executing Halt.
86395*/
86396case OP_Halt: {
86397 VdbeFrame *pFrame;
86398 int pcx;
86399
86400 pcx = (int)(pOp - aOp);
86401#ifdef SQLITE_DEBUG
86402 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
86403#endif
86404 if( pOp->p1==SQLITE_OK && p->pFrame ){
86405 /* Halt the sub-program. Return control to the parent frame. */
86406 pFrame = p->pFrame;
86407 p->pFrame = pFrame->pParent;
86408 p->nFrame--;
86409 sqlite3VdbeSetChanges(db, p->nChange);
86410 pcx = sqlite3VdbeFrameRestore(pFrame);
86411 if( pOp->p2==OE_Ignore ){
86412 /* Instruction pcx is the OP_Program that invoked the sub-program
86413 ** currently being halted. If the p2 instruction of this OP_Halt
86414 ** instruction is set to OE_Ignore, then the sub-program is throwing
86415 ** an IGNORE exception. In this case jump to the address specified
86416 ** as the p2 of the calling OP_Program. */
86417 pcx = p->aOp[pcx].p2-1;
86418 }
86419 aOp = p->aOp;
86420 aMem = p->aMem;
86421 pOp = &aOp[pcx];
86422 break;
86423 }
86424 p->rc = pOp->p1;
86425 p->errorAction = (u8)pOp->p2;
86426 p->pc = pcx;
86427 assert( pOp->p5<=4 );
86428 if( p->rc ){
86429 if( pOp->p5 ){
86430 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
86431 "FOREIGN KEY" };
86432 testcase( pOp->p5==1 );
86433 testcase( pOp->p5==2 );
86434 testcase( pOp->p5==3 );
86435 testcase( pOp->p5==4 );
86436 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
86437 if( pOp->p4.z ){
86438 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
86439 }
86440 }else{
86441 sqlite3VdbeError(p, "%s", pOp->p4.z);
86442 }
86443 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
86444 }
86445 rc = sqlite3VdbeHalt(p);
86446 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
86447 if( rc==SQLITE_BUSY ){
86448 p->rc = SQLITE_BUSY;
86449 }else{
86450 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
86451 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
86452 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
86453 }
86454 goto vdbe_return;
86455}
86456
86457/* Opcode: Integer P1 P2 * * *
86458** Synopsis: r[P2]=P1
86459**
86460** The 32-bit integer value P1 is written into register P2.
86461*/
86462case OP_Integer: { /* out2 */
86463 pOut = out2Prerelease(p, pOp);
86464 pOut->u.i = pOp->p1;
86465 break;
86466}
86467
86468/* Opcode: Int64 * P2 * P4 *
86469** Synopsis: r[P2]=P4
86470**
86471** P4 is a pointer to a 64-bit integer value.
86472** Write that value into register P2.
86473*/
86474case OP_Int64: { /* out2 */
86475 pOut = out2Prerelease(p, pOp);
86476 assert( pOp->p4.pI64!=0 );
86477 pOut->u.i = *pOp->p4.pI64;
86478 break;
86479}
86480
86481#ifndef SQLITE_OMIT_FLOATING_POINT
86482/* Opcode: Real * P2 * P4 *
86483** Synopsis: r[P2]=P4
86484**
86485** P4 is a pointer to a 64-bit floating point value.
86486** Write that value into register P2.
86487*/
86488case OP_Real: { /* same as TK_FLOAT, out2 */
86489 pOut = out2Prerelease(p, pOp);
86490 pOut->flags = MEM_Real;
86491 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
86492 pOut->u.r = *pOp->p4.pReal;
86493 break;
86494}
86495#endif
86496
86497/* Opcode: String8 * P2 * P4 *
86498** Synopsis: r[P2]='P4'
86499**
86500** P4 points to a nul terminated UTF-8 string. This opcode is transformed
86501** into a String opcode before it is executed for the first time. During
86502** this transformation, the length of string P4 is computed and stored
86503** as the P1 parameter.
86504*/
86505case OP_String8: { /* same as TK_STRING, out2 */
86506 assert( pOp->p4.z!=0 );
86507 pOut = out2Prerelease(p, pOp);
86508 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
86509
86510#ifndef SQLITE_OMIT_UTF16
86511 if( encoding!=SQLITE_UTF8 ){
86512 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
86513 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
86514 if( rc ) goto too_big;
86515 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
86516 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
86517 assert( VdbeMemDynamic(pOut)==0 );
86518 pOut->szMalloc = 0;
86519 pOut->flags |= MEM_Static;
86520 if( pOp->p4type==P4_DYNAMIC ){
86521 sqlite3DbFree(db, pOp->p4.z);
86522 }
86523 pOp->p4type = P4_DYNAMIC;
86524 pOp->p4.z = pOut->z;
86525 pOp->p1 = pOut->n;
86526 }
86527#endif
86528 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
86529 goto too_big;
86530 }
86531 pOp->opcode = OP_String;
86532 assert( rc==SQLITE_OK );
86533 /* Fall through to the next case, OP_String */
86534 /* no break */ deliberate_fall_through
86535}
86536
86537/* Opcode: String P1 P2 P3 P4 P5
86538** Synopsis: r[P2]='P4' (len=P1)
86539**
86540** The string value P4 of length P1 (bytes) is stored in register P2.
86541**
86542** If P3 is not zero and the content of register P3 is equal to P5, then
86543** the datatype of the register P2 is converted to BLOB. The content is
86544** the same sequence of bytes, it is merely interpreted as a BLOB instead
86545** of a string, as if it had been CAST. In other words:
86546**
86547** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
86548*/
86549case OP_String: { /* out2 */
86550 assert( pOp->p4.z!=0 );
86551 pOut = out2Prerelease(p, pOp);
86552 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
86553 pOut->z = pOp->p4.z;
86554 pOut->n = pOp->p1;
86555 pOut->enc = encoding;
86556 UPDATE_MAX_BLOBSIZE(pOut);
86557#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
86558 if( pOp->p3>0 ){
86559 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86560 pIn3 = &aMem[pOp->p3];
86561 assert( pIn3->flags & MEM_Int );
86562 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
86563 }
86564#endif
86565 break;
86566}
86567
86568/* Opcode: Null P1 P2 P3 * *
86569** Synopsis: r[P2..P3]=NULL
86570**
86571** Write a NULL into registers P2. If P3 greater than P2, then also write
86572** NULL into register P3 and every register in between P2 and P3. If P3
86573** is less than P2 (typically P3 is zero) then only register P2 is
86574** set to NULL.
86575**
86576** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
86577** NULL values will not compare equal even if SQLITE_NULLEQ is set on
86578** OP_Ne or OP_Eq.
86579*/
86580case OP_Null: { /* out2 */
86581 int cnt;
86582 u16 nullFlag;
86583 pOut = out2Prerelease(p, pOp);
86584 cnt = pOp->p3-pOp->p2;
86585 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86586 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
86587 pOut->n = 0;
86588#ifdef SQLITE_DEBUG
86589 pOut->uTemp = 0;
86590#endif
86591 while( cnt>0 ){
86592 pOut++;
86593 memAboutToChange(p, pOut);
86594 sqlite3VdbeMemSetNull(pOut);
86595 pOut->flags = nullFlag;
86596 pOut->n = 0;
86597 cnt--;
86598 }
86599 break;
86600}
86601
86602/* Opcode: SoftNull P1 * * * *
86603** Synopsis: r[P1]=NULL
86604**
86605** Set register P1 to have the value NULL as seen by the OP_MakeRecord
86606** instruction, but do not free any string or blob memory associated with
86607** the register, so that if the value was a string or blob that was
86608** previously copied using OP_SCopy, the copies will continue to be valid.
86609*/
86610case OP_SoftNull: {
86611 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86612 pOut = &aMem[pOp->p1];
86613 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
86614 break;
86615}
86616
86617/* Opcode: Blob P1 P2 * P4 *
86618** Synopsis: r[P2]=P4 (len=P1)
86619**
86620** P4 points to a blob of data P1 bytes long. Store this
86621** blob in register P2.
86622*/
86623case OP_Blob: { /* out2 */
86624 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
86625 pOut = out2Prerelease(p, pOp);
86626 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
86627 pOut->enc = encoding;
86628 UPDATE_MAX_BLOBSIZE(pOut);
86629 break;
86630}
86631
86632/* Opcode: Variable P1 P2 * P4 *
86633** Synopsis: r[P2]=parameter(P1,P4)
86634**
86635** Transfer the values of bound parameter P1 into register P2
86636**
86637** If the parameter is named, then its name appears in P4.
86638** The P4 value is used by sqlite3_bind_parameter_name().
86639*/
86640case OP_Variable: { /* out2 */
86641 Mem *pVar; /* Value being transferred */
86642
86643 assert( pOp->p1>0 && pOp->p1<=p->nVar );
86644 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
86645 pVar = &p->aVar[pOp->p1 - 1];
86646 if( sqlite3VdbeMemTooBig(pVar) ){
86647 goto too_big;
86648 }
86649 pOut = &aMem[pOp->p2];
86650 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
86651 memcpy(pOut, pVar, MEMCELLSIZE);
86652 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
86653 pOut->flags |= MEM_Static|MEM_FromBind;
86654 UPDATE_MAX_BLOBSIZE(pOut);
86655 break;
86656}
86657
86658/* Opcode: Move P1 P2 P3 * *
86659** Synopsis: r[P2@P3]=r[P1@P3]
86660**
86661** Move the P3 values in register P1..P1+P3-1 over into
86662** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
86663** left holding a NULL. It is an error for register ranges
86664** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
86665** for P3 to be less than 1.
86666*/
86667case OP_Move: {
86668 int n; /* Number of registers left to copy */
86669 int p1; /* Register to copy from */
86670 int p2; /* Register to copy to */
86671
86672 n = pOp->p3;
86673 p1 = pOp->p1;
86674 p2 = pOp->p2;
86675 assert( n>0 && p1>0 && p2>0 );
86676 assert( p1+n<=p2 || p2+n<=p1 );
86677
86678 pIn1 = &aMem[p1];
86679 pOut = &aMem[p2];
86680 do{
86681 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
86682 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
86683 assert( memIsValid(pIn1) );
86684 memAboutToChange(p, pOut);
86685 sqlite3VdbeMemMove(pOut, pIn1);
86686#ifdef SQLITE_DEBUG
86687 pIn1->pScopyFrom = 0;
86688 { int i;
86689 for(i=1; i<p->nMem; i++){
86690 if( aMem[i].pScopyFrom==pIn1 ){
86691 aMem[i].pScopyFrom = pOut;
86692 }
86693 }
86694 }
86695#endif
86696 Deephemeralize(pOut);
86697 REGISTER_TRACE(p2++, pOut);
86698 pIn1++;
86699 pOut++;
86700 }while( --n );
86701 break;
86702}
86703
86704/* Opcode: Copy P1 P2 P3 * *
86705** Synopsis: r[P2@P3+1]=r[P1@P3+1]
86706**
86707** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
86708**
86709** This instruction makes a deep copy of the value. A duplicate
86710** is made of any string or blob constant. See also OP_SCopy.
86711*/
86712case OP_Copy: {
86713 int n;
86714
86715 n = pOp->p3;
86716 pIn1 = &aMem[pOp->p1];
86717 pOut = &aMem[pOp->p2];
86718 assert( pOut!=pIn1 );
86719 while( 1 ){
86720 memAboutToChange(p, pOut);
86721 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
86722 Deephemeralize(pOut);
86723#ifdef SQLITE_DEBUG
86724 pOut->pScopyFrom = 0;
86725#endif
86726 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
86727 if( (n--)==0 ) break;
86728 pOut++;
86729 pIn1++;
86730 }
86731 break;
86732}
86733
86734/* Opcode: SCopy P1 P2 * * *
86735** Synopsis: r[P2]=r[P1]
86736**
86737** Make a shallow copy of register P1 into register P2.
86738**
86739** This instruction makes a shallow copy of the value. If the value
86740** is a string or blob, then the copy is only a pointer to the
86741** original and hence if the original changes so will the copy.
86742** Worse, if the original is deallocated, the copy becomes invalid.
86743** Thus the program must guarantee that the original will not change
86744** during the lifetime of the copy. Use OP_Copy to make a complete
86745** copy.
86746*/
86747case OP_SCopy: { /* out2 */
86748 pIn1 = &aMem[pOp->p1];
86749 pOut = &aMem[pOp->p2];
86750 assert( pOut!=pIn1 );
86751 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
86752#ifdef SQLITE_DEBUG
86753 pOut->pScopyFrom = pIn1;
86754 pOut->mScopyFlags = pIn1->flags;
86755#endif
86756 break;
86757}
86758
86759/* Opcode: IntCopy P1 P2 * * *
86760** Synopsis: r[P2]=r[P1]
86761**
86762** Transfer the integer value held in register P1 into register P2.
86763**
86764** This is an optimized version of SCopy that works only for integer
86765** values.
86766*/
86767case OP_IntCopy: { /* out2 */
86768 pIn1 = &aMem[pOp->p1];
86769 assert( (pIn1->flags & MEM_Int)!=0 );
86770 pOut = &aMem[pOp->p2];
86771 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
86772 break;
86773}
86774
86775/* Opcode: ResultRow P1 P2 * * *
86776** Synopsis: output=r[P1@P2]
86777**
86778** The registers P1 through P1+P2-1 contain a single row of
86779** results. This opcode causes the sqlite3_step() call to terminate
86780** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
86781** structure to provide access to the r(P1)..r(P1+P2-1) values as
86782** the result row.
86783*/
86784case OP_ResultRow: {
86785 Mem *pMem;
86786 int i;
86787 assert( p->nResColumn==pOp->p2 );
86788 assert( pOp->p1>0 );
86789 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
86790
86791 /* If this statement has violated immediate foreign key constraints, do
86792 ** not return the number of rows modified. And do not RELEASE the statement
86793 ** transaction. It needs to be rolled back. */
86794 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
86795 assert( db->flags&SQLITE_CountRows );
86796 assert( p->usesStmtJournal );
86797 goto abort_due_to_error;
86798 }
86799
86800 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
86801 ** DML statements invoke this opcode to return the number of rows
86802 ** modified to the user. This is the only way that a VM that
86803 ** opens a statement transaction may invoke this opcode.
86804 **
86805 ** In case this is such a statement, close any statement transaction
86806 ** opened by this VM before returning control to the user. This is to
86807 ** ensure that statement-transactions are always nested, not overlapping.
86808 ** If the open statement-transaction is not closed here, then the user
86809 ** may step another VM that opens its own statement transaction. This
86810 ** may lead to overlapping statement transactions.
86811 **
86812 ** The statement transaction is never a top-level transaction. Hence
86813 ** the RELEASE call below can never fail.
86814 */
86815 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
86816 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
86817 assert( rc==SQLITE_OK );
86818
86819 /* Invalidate all ephemeral cursor row caches */
86820 p->cacheCtr = (p->cacheCtr + 2)|1;
86821
86822 /* Make sure the results of the current row are \000 terminated
86823 ** and have an assigned type. The results are de-ephemeralized as
86824 ** a side effect.
86825 */
86826 pMem = p->pResultSet = &aMem[pOp->p1];
86827 for(i=0; i<pOp->p2; i++){
86828 assert( memIsValid(&pMem[i]) );
86829 Deephemeralize(&pMem[i]);
86830 assert( (pMem[i].flags & MEM_Ephem)==0
86831 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
86832 sqlite3VdbeMemNulTerminate(&pMem[i]);
86833 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
86834#ifdef SQLITE_DEBUG
86835 /* The registers in the result will not be used again when the
86836 ** prepared statement restarts. This is because sqlite3_column()
86837 ** APIs might have caused type conversions of made other changes to
86838 ** the register values. Therefore, we can go ahead and break any
86839 ** OP_SCopy dependencies. */
86840 pMem[i].pScopyFrom = 0;
86841#endif
86842 }
86843 if( db->mallocFailed ) goto no_mem;
86844
86845 if( db->mTrace & SQLITE_TRACE_ROW ){
86846 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
86847 }
86848
86849
86850 /* Return SQLITE_ROW
86851 */
86852 p->pc = (int)(pOp - aOp) + 1;
86853 rc = SQLITE_ROW;
86854 goto vdbe_return;
86855}
86856
86857/* Opcode: Concat P1 P2 P3 * *
86858** Synopsis: r[P3]=r[P2]+r[P1]
86859**
86860** Add the text in register P1 onto the end of the text in
86861** register P2 and store the result in register P3.
86862** If either the P1 or P2 text are NULL then store NULL in P3.
86863**
86864** P3 = P2 || P1
86865**
86866** It is illegal for P1 and P3 to be the same register. Sometimes,
86867** if P3 is the same register as P2, the implementation is able
86868** to avoid a memcpy().
86869*/
86870case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
86871 i64 nByte; /* Total size of the output string or blob */
86872 u16 flags1; /* Initial flags for P1 */
86873 u16 flags2; /* Initial flags for P2 */
86874
86875 pIn1 = &aMem[pOp->p1];
86876 pIn2 = &aMem[pOp->p2];
86877 pOut = &aMem[pOp->p3];
86878 testcase( pOut==pIn2 );
86879 assert( pIn1!=pOut );
86880 flags1 = pIn1->flags;
86881 testcase( flags1 & MEM_Null );
86882 testcase( pIn2->flags & MEM_Null );
86883 if( (flags1 | pIn2->flags) & MEM_Null ){
86884 sqlite3VdbeMemSetNull(pOut);
86885 break;
86886 }
86887 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
86888 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
86889 flags1 = pIn1->flags & ~MEM_Str;
86890 }else if( (flags1 & MEM_Zero)!=0 ){
86891 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
86892 flags1 = pIn1->flags & ~MEM_Str;
86893 }
86894 flags2 = pIn2->flags;
86895 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
86896 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
86897 flags2 = pIn2->flags & ~MEM_Str;
86898 }else if( (flags2 & MEM_Zero)!=0 ){
86899 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
86900 flags2 = pIn2->flags & ~MEM_Str;
86901 }
86902 nByte = pIn1->n + pIn2->n;
86903 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
86904 goto too_big;
86905 }
86906 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
86907 goto no_mem;
86908 }
86909 MemSetTypeFlag(pOut, MEM_Str);
86910 if( pOut!=pIn2 ){
86911 memcpy(pOut->z, pIn2->z, pIn2->n);
86912 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
86913 pIn2->flags = flags2;
86914 }
86915 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
86916 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
86917 pIn1->flags = flags1;
86918 pOut->z[nByte]=0;
86919 pOut->z[nByte+1] = 0;
86920 pOut->z[nByte+2] = 0;
86921 pOut->flags |= MEM_Term;
86922 pOut->n = (int)nByte;
86923 pOut->enc = encoding;
86924 UPDATE_MAX_BLOBSIZE(pOut);
86925 break;
86926}
86927
86928/* Opcode: Add P1 P2 P3 * *
86929** Synopsis: r[P3]=r[P1]+r[P2]
86930**
86931** Add the value in register P1 to the value in register P2
86932** and store the result in register P3.
86933** If either input is NULL, the result is NULL.
86934*/
86935/* Opcode: Multiply P1 P2 P3 * *
86936** Synopsis: r[P3]=r[P1]*r[P2]
86937**
86938**
86939** Multiply the value in register P1 by the value in register P2
86940** and store the result in register P3.
86941** If either input is NULL, the result is NULL.
86942*/
86943/* Opcode: Subtract P1 P2 P3 * *
86944** Synopsis: r[P3]=r[P2]-r[P1]
86945**
86946** Subtract the value in register P1 from the value in register P2
86947** and store the result in register P3.
86948** If either input is NULL, the result is NULL.
86949*/
86950/* Opcode: Divide P1 P2 P3 * *
86951** Synopsis: r[P3]=r[P2]/r[P1]
86952**
86953** Divide the value in register P1 by the value in register P2
86954** and store the result in register P3 (P3=P2/P1). If the value in
86955** register P1 is zero, then the result is NULL. If either input is
86956** NULL, the result is NULL.
86957*/
86958/* Opcode: Remainder P1 P2 P3 * *
86959** Synopsis: r[P3]=r[P2]%r[P1]
86960**
86961** Compute the remainder after integer register P2 is divided by
86962** register P1 and store the result in register P3.
86963** If the value in register P1 is zero the result is NULL.
86964** If either operand is NULL, the result is NULL.
86965*/
86966case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
86967case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
86968case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
86969case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
86970case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
86971 u16 flags; /* Combined MEM_* flags from both inputs */
86972 u16 type1; /* Numeric type of left operand */
86973 u16 type2; /* Numeric type of right operand */
86974 i64 iA; /* Integer value of left operand */
86975 i64 iB; /* Integer value of right operand */
86976 double rA; /* Real value of left operand */
86977 double rB; /* Real value of right operand */
86978
86979 pIn1 = &aMem[pOp->p1];
86980 type1 = numericType(pIn1);
86981 pIn2 = &aMem[pOp->p2];
86982 type2 = numericType(pIn2);
86983 pOut = &aMem[pOp->p3];
86984 flags = pIn1->flags | pIn2->flags;
86985 if( (type1 & type2 & MEM_Int)!=0 ){
86986 iA = pIn1->u.i;
86987 iB = pIn2->u.i;
86988 switch( pOp->opcode ){
86989 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
86990 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
86991 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
86992 case OP_Divide: {
86993 if( iA==0 ) goto arithmetic_result_is_null;
86994 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
86995 iB /= iA;
86996 break;
86997 }
86998 default: {
86999 if( iA==0 ) goto arithmetic_result_is_null;
87000 if( iA==-1 ) iA = 1;
87001 iB %= iA;
87002 break;
87003 }
87004 }
87005 pOut->u.i = iB;
87006 MemSetTypeFlag(pOut, MEM_Int);
87007 }else if( (flags & MEM_Null)!=0 ){
87008 goto arithmetic_result_is_null;
87009 }else{
87010fp_math:
87011 rA = sqlite3VdbeRealValue(pIn1);
87012 rB = sqlite3VdbeRealValue(pIn2);
87013 switch( pOp->opcode ){
87014 case OP_Add: rB += rA; break;
87015 case OP_Subtract: rB -= rA; break;
87016 case OP_Multiply: rB *= rA; break;
87017 case OP_Divide: {
87018 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
87019 if( rA==(double)0 ) goto arithmetic_result_is_null;
87020 rB /= rA;
87021 break;
87022 }
87023 default: {
87024 iA = sqlite3VdbeIntValue(pIn1);
87025 iB = sqlite3VdbeIntValue(pIn2);
87026 if( iA==0 ) goto arithmetic_result_is_null;
87027 if( iA==-1 ) iA = 1;
87028 rB = (double)(iB % iA);
87029 break;
87030 }
87031 }
87032#ifdef SQLITE_OMIT_FLOATING_POINT
87033 pOut->u.i = rB;
87034 MemSetTypeFlag(pOut, MEM_Int);
87035#else
87036 if( sqlite3IsNaN(rB) ){
87037 goto arithmetic_result_is_null;
87038 }
87039 pOut->u.r = rB;
87040 MemSetTypeFlag(pOut, MEM_Real);
87041#endif
87042 }
87043 break;
87044
87045arithmetic_result_is_null:
87046 sqlite3VdbeMemSetNull(pOut);
87047 break;
87048}
87049
87050/* Opcode: CollSeq P1 * * P4
87051**
87052** P4 is a pointer to a CollSeq object. If the next call to a user function
87053** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
87054** be returned. This is used by the built-in min(), max() and nullif()
87055** functions.
87056**
87057** If P1 is not zero, then it is a register that a subsequent min() or
87058** max() aggregate will set to 1 if the current row is not the minimum or
87059** maximum. The P1 register is initialized to 0 by this instruction.
87060**
87061** The interface used by the implementation of the aforementioned functions
87062** to retrieve the collation sequence set by this opcode is not available
87063** publicly. Only built-in functions have access to this feature.
87064*/
87065case OP_CollSeq: {
87066 assert( pOp->p4type==P4_COLLSEQ );
87067 if( pOp->p1 ){
87068 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
87069 }
87070 break;
87071}
87072
87073/* Opcode: BitAnd P1 P2 P3 * *
87074** Synopsis: r[P3]=r[P1]&r[P2]
87075**
87076** Take the bit-wise AND of the values in register P1 and P2 and
87077** store the result in register P3.
87078** If either input is NULL, the result is NULL.
87079*/
87080/* Opcode: BitOr P1 P2 P3 * *
87081** Synopsis: r[P3]=r[P1]|r[P2]
87082**
87083** Take the bit-wise OR of the values in register P1 and P2 and
87084** store the result in register P3.
87085** If either input is NULL, the result is NULL.
87086*/
87087/* Opcode: ShiftLeft P1 P2 P3 * *
87088** Synopsis: r[P3]=r[P2]<<r[P1]
87089**
87090** Shift the integer value in register P2 to the left by the
87091** number of bits specified by the integer in register P1.
87092** Store the result in register P3.
87093** If either input is NULL, the result is NULL.
87094*/
87095/* Opcode: ShiftRight P1 P2 P3 * *
87096** Synopsis: r[P3]=r[P2]>>r[P1]
87097**
87098** Shift the integer value in register P2 to the right by the
87099** number of bits specified by the integer in register P1.
87100** Store the result in register P3.
87101** If either input is NULL, the result is NULL.
87102*/
87103case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
87104case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
87105case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
87106case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
87107 i64 iA;
87108 u64 uA;
87109 i64 iB;
87110 u8 op;
87111
87112 pIn1 = &aMem[pOp->p1];
87113 pIn2 = &aMem[pOp->p2];
87114 pOut = &aMem[pOp->p3];
87115 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
87116 sqlite3VdbeMemSetNull(pOut);
87117 break;
87118 }
87119 iA = sqlite3VdbeIntValue(pIn2);
87120 iB = sqlite3VdbeIntValue(pIn1);
87121 op = pOp->opcode;
87122 if( op==OP_BitAnd ){
87123 iA &= iB;
87124 }else if( op==OP_BitOr ){
87125 iA |= iB;
87126 }else if( iB!=0 ){
87127 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
87128
87129 /* If shifting by a negative amount, shift in the other direction */
87130 if( iB<0 ){
87131 assert( OP_ShiftRight==OP_ShiftLeft+1 );
87132 op = 2*OP_ShiftLeft + 1 - op;
87133 iB = iB>(-64) ? -iB : 64;
87134 }
87135
87136 if( iB>=64 ){
87137 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
87138 }else{
87139 memcpy(&uA, &iA, sizeof(uA));
87140 if( op==OP_ShiftLeft ){
87141 uA <<= iB;
87142 }else{
87143 uA >>= iB;
87144 /* Sign-extend on a right shift of a negative number */
87145 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
87146 }
87147 memcpy(&iA, &uA, sizeof(iA));
87148 }
87149 }
87150 pOut->u.i = iA;
87151 MemSetTypeFlag(pOut, MEM_Int);
87152 break;
87153}
87154
87155/* Opcode: AddImm P1 P2 * * *
87156** Synopsis: r[P1]=r[P1]+P2
87157**
87158** Add the constant P2 to the value in register P1.
87159** The result is always an integer.
87160**
87161** To force any register to be an integer, just add 0.
87162*/
87163case OP_AddImm: { /* in1 */
87164 pIn1 = &aMem[pOp->p1];
87165 memAboutToChange(p, pIn1);
87166 sqlite3VdbeMemIntegerify(pIn1);
87167 pIn1->u.i += pOp->p2;
87168 break;
87169}
87170
87171/* Opcode: MustBeInt P1 P2 * * *
87172**
87173** Force the value in register P1 to be an integer. If the value
87174** in P1 is not an integer and cannot be converted into an integer
87175** without data loss, then jump immediately to P2, or if P2==0
87176** raise an SQLITE_MISMATCH exception.
87177*/
87178case OP_MustBeInt: { /* jump, in1 */
87179 pIn1 = &aMem[pOp->p1];
87180 if( (pIn1->flags & MEM_Int)==0 ){
87181 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
87182 if( (pIn1->flags & MEM_Int)==0 ){
87183 VdbeBranchTaken(1, 2);
87184 if( pOp->p2==0 ){
87185 rc = SQLITE_MISMATCH;
87186 goto abort_due_to_error;
87187 }else{
87188 goto jump_to_p2;
87189 }
87190 }
87191 }
87192 VdbeBranchTaken(0, 2);
87193 MemSetTypeFlag(pIn1, MEM_Int);
87194 break;
87195}
87196
87197#ifndef SQLITE_OMIT_FLOATING_POINT
87198/* Opcode: RealAffinity P1 * * * *
87199**
87200** If register P1 holds an integer convert it to a real value.
87201**
87202** This opcode is used when extracting information from a column that
87203** has REAL affinity. Such column values may still be stored as
87204** integers, for space efficiency, but after extraction we want them
87205** to have only a real value.
87206*/
87207case OP_RealAffinity: { /* in1 */
87208 pIn1 = &aMem[pOp->p1];
87209 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
87210 testcase( pIn1->flags & MEM_Int );
87211 testcase( pIn1->flags & MEM_IntReal );
87212 sqlite3VdbeMemRealify(pIn1);
87213 REGISTER_TRACE(pOp->p1, pIn1);
87214 }
87215 break;
87216}
87217#endif
87218
87219#ifndef SQLITE_OMIT_CAST
87220/* Opcode: Cast P1 P2 * * *
87221** Synopsis: affinity(r[P1])
87222**
87223** Force the value in register P1 to be the type defined by P2.
87224**
87225** <ul>
87226** <li> P2=='A' &rarr; BLOB
87227** <li> P2=='B' &rarr; TEXT
87228** <li> P2=='C' &rarr; NUMERIC
87229** <li> P2=='D' &rarr; INTEGER
87230** <li> P2=='E' &rarr; REAL
87231** </ul>
87232**
87233** A NULL value is not changed by this routine. It remains NULL.
87234*/
87235case OP_Cast: { /* in1 */
87236 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
87237 testcase( pOp->p2==SQLITE_AFF_TEXT );
87238 testcase( pOp->p2==SQLITE_AFF_BLOB );
87239 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
87240 testcase( pOp->p2==SQLITE_AFF_INTEGER );
87241 testcase( pOp->p2==SQLITE_AFF_REAL );
87242 pIn1 = &aMem[pOp->p1];
87243 memAboutToChange(p, pIn1);
87244 rc = ExpandBlob(pIn1);
87245 if( rc ) goto abort_due_to_error;
87246 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
87247 if( rc ) goto abort_due_to_error;
87248 UPDATE_MAX_BLOBSIZE(pIn1);
87249 REGISTER_TRACE(pOp->p1, pIn1);
87250 break;
87251}
87252#endif /* SQLITE_OMIT_CAST */
87253
87254/* Opcode: Eq P1 P2 P3 P4 P5
87255** Synopsis: IF r[P3]==r[P1]
87256**
87257** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
87258** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
87259** store the result of comparison in register P2.
87260**
87261** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87262** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
87263** to coerce both inputs according to this affinity before the
87264** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
87265** affinity is used. Note that the affinity conversions are stored
87266** back into the input registers P1 and P3. So this opcode can cause
87267** persistent changes to registers P1 and P3.
87268**
87269** Once any conversions have taken place, and neither value is NULL,
87270** the values are compared. If both values are blobs then memcmp() is
87271** used to determine the results of the comparison. If both values
87272** are text, then the appropriate collating function specified in
87273** P4 is used to do the comparison. If P4 is not specified then
87274** memcmp() is used to compare text string. If both values are
87275** numeric, then a numeric comparison is used. If the two values
87276** are of different types, then numbers are considered less than
87277** strings and strings are considered less than blobs.
87278**
87279** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
87280** true or false and is never NULL. If both operands are NULL then the result
87281** of comparison is true. If either operand is NULL then the result is false.
87282** If neither operand is NULL the result is the same as it would be if
87283** the SQLITE_NULLEQ flag were omitted from P5.
87284**
87285** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
87286** content of r[P2] is only changed if the new value is NULL or 0 (false).
87287** In other words, a prior r[P2] value will not be overwritten by 1 (true).
87288*/
87289/* Opcode: Ne P1 P2 P3 P4 P5
87290** Synopsis: IF r[P3]!=r[P1]
87291**
87292** This works just like the Eq opcode except that the jump is taken if
87293** the operands in registers P1 and P3 are not equal. See the Eq opcode for
87294** additional information.
87295**
87296** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
87297** content of r[P2] is only changed if the new value is NULL or 1 (true).
87298** In other words, a prior r[P2] value will not be overwritten by 0 (false).
87299*/
87300/* Opcode: Lt P1 P2 P3 P4 P5
87301** Synopsis: IF r[P3]<r[P1]
87302**
87303** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
87304** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
87305** the result of comparison (0 or 1 or NULL) into register P2.
87306**
87307** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
87308** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
87309** bit is clear then fall through if either operand is NULL.
87310**
87311** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87312** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
87313** to coerce both inputs according to this affinity before the
87314** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
87315** affinity is used. Note that the affinity conversions are stored
87316** back into the input registers P1 and P3. So this opcode can cause
87317** persistent changes to registers P1 and P3.
87318**
87319** Once any conversions have taken place, and neither value is NULL,
87320** the values are compared. If both values are blobs then memcmp() is
87321** used to determine the results of the comparison. If both values
87322** are text, then the appropriate collating function specified in
87323** P4 is used to do the comparison. If P4 is not specified then
87324** memcmp() is used to compare text string. If both values are
87325** numeric, then a numeric comparison is used. If the two values
87326** are of different types, then numbers are considered less than
87327** strings and strings are considered less than blobs.
87328*/
87329/* Opcode: Le P1 P2 P3 P4 P5
87330** Synopsis: IF r[P3]<=r[P1]
87331**
87332** This works just like the Lt opcode except that the jump is taken if
87333** the content of register P3 is less than or equal to the content of
87334** register P1. See the Lt opcode for additional information.
87335*/
87336/* Opcode: Gt P1 P2 P3 P4 P5
87337** Synopsis: IF r[P3]>r[P1]
87338**
87339** This works just like the Lt opcode except that the jump is taken if
87340** the content of register P3 is greater than the content of
87341** register P1. See the Lt opcode for additional information.
87342*/
87343/* Opcode: Ge P1 P2 P3 P4 P5
87344** Synopsis: IF r[P3]>=r[P1]
87345**
87346** This works just like the Lt opcode except that the jump is taken if
87347** the content of register P3 is greater than or equal to the content of
87348** register P1. See the Lt opcode for additional information.
87349*/
87350case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
87351case OP_Ne: /* same as TK_NE, jump, in1, in3 */
87352case OP_Lt: /* same as TK_LT, jump, in1, in3 */
87353case OP_Le: /* same as TK_LE, jump, in1, in3 */
87354case OP_Gt: /* same as TK_GT, jump, in1, in3 */
87355case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
87356 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
87357 char affinity; /* Affinity to use for comparison */
87358 u16 flags1; /* Copy of initial value of pIn1->flags */
87359 u16 flags3; /* Copy of initial value of pIn3->flags */
87360
87361 pIn1 = &aMem[pOp->p1];
87362 pIn3 = &aMem[pOp->p3];
87363 flags1 = pIn1->flags;
87364 flags3 = pIn3->flags;
87365 if( (flags1 | flags3)&MEM_Null ){
87366 /* One or both operands are NULL */
87367 if( pOp->p5 & SQLITE_NULLEQ ){
87368 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
87369 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
87370 ** or not both operands are null.
87371 */
87372 assert( (flags1 & MEM_Cleared)==0 );
87373 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
87374 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
87375 if( (flags1&flags3&MEM_Null)!=0
87376 && (flags3&MEM_Cleared)==0
87377 ){
87378 res = 0; /* Operands are equal */
87379 }else{
87380 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
87381 }
87382 }else{
87383 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
87384 ** then the result is always NULL.
87385 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
87386 */
87387 if( pOp->p5 & SQLITE_STOREP2 ){
87388 pOut = &aMem[pOp->p2];
87389 iCompare = 1; /* Operands are not equal */
87390 memAboutToChange(p, pOut);
87391 MemSetTypeFlag(pOut, MEM_Null);
87392 REGISTER_TRACE(pOp->p2, pOut);
87393 }else{
87394 VdbeBranchTaken(2,3);
87395 if( pOp->p5 & SQLITE_JUMPIFNULL ){
87396 goto jump_to_p2;
87397 }
87398 }
87399 break;
87400 }
87401 }else{
87402 /* Neither operand is NULL. Do a comparison. */
87403 affinity = pOp->p5 & SQLITE_AFF_MASK;
87404 if( affinity>=SQLITE_AFF_NUMERIC ){
87405 if( (flags1 | flags3)&MEM_Str ){
87406 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
87407 applyNumericAffinity(pIn1,0);
87408 testcase( flags3==pIn3->flags );
87409 flags3 = pIn3->flags;
87410 }
87411 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
87412 applyNumericAffinity(pIn3,0);
87413 }
87414 }
87415 /* Handle the common case of integer comparison here, as an
87416 ** optimization, to avoid a call to sqlite3MemCompare() */
87417 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
87418 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
87419 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
87420 res = 0;
87421 goto compare_op;
87422 }
87423 }else if( affinity==SQLITE_AFF_TEXT ){
87424 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
87425 testcase( pIn1->flags & MEM_Int );
87426 testcase( pIn1->flags & MEM_Real );
87427 testcase( pIn1->flags & MEM_IntReal );
87428 sqlite3VdbeMemStringify(pIn1, encoding, 1);
87429 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
87430 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
87431 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
87432 }
87433 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
87434 testcase( pIn3->flags & MEM_Int );
87435 testcase( pIn3->flags & MEM_Real );
87436 testcase( pIn3->flags & MEM_IntReal );
87437 sqlite3VdbeMemStringify(pIn3, encoding, 1);
87438 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
87439 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
87440 }
87441 }
87442 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
87443 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
87444 }
87445compare_op:
87446 /* At this point, res is negative, zero, or positive if reg[P1] is
87447 ** less than, equal to, or greater than reg[P3], respectively. Compute
87448 ** the answer to this operator in res2, depending on what the comparison
87449 ** operator actually is. The next block of code depends on the fact
87450 ** that the 6 comparison operators are consecutive integers in this
87451 ** order: NE, EQ, GT, LE, LT, GE */
87452 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
87453 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
87454 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
87455 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
87456 res2 = aLTb[pOp->opcode - OP_Ne];
87457 }else if( res==0 ){
87458 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
87459 res2 = aEQb[pOp->opcode - OP_Ne];
87460 }else{
87461 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
87462 res2 = aGTb[pOp->opcode - OP_Ne];
87463 }
87464
87465 /* Undo any changes made by applyAffinity() to the input registers. */
87466 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
87467 pIn3->flags = flags3;
87468 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
87469 pIn1->flags = flags1;
87470
87471 if( pOp->p5 & SQLITE_STOREP2 ){
87472 pOut = &aMem[pOp->p2];
87473 iCompare = res;
87474 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
87475 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
87476 ** and prevents OP_Ne from overwriting NULL with 0. This flag
87477 ** is only used in contexts where either:
87478 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
87479 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
87480 ** Therefore it is not necessary to check the content of r[P2] for
87481 ** NULL. */
87482 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
87483 assert( res2==0 || res2==1 );
87484 testcase( res2==0 && pOp->opcode==OP_Eq );
87485 testcase( res2==1 && pOp->opcode==OP_Eq );
87486 testcase( res2==0 && pOp->opcode==OP_Ne );
87487 testcase( res2==1 && pOp->opcode==OP_Ne );
87488 if( (pOp->opcode==OP_Eq)==res2 ) break;
87489 }
87490 memAboutToChange(p, pOut);
87491 MemSetTypeFlag(pOut, MEM_Int);
87492 pOut->u.i = res2;
87493 REGISTER_TRACE(pOp->p2, pOut);
87494 }else{
87495 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
87496 if( res2 ){
87497 goto jump_to_p2;
87498 }
87499 }
87500 break;
87501}
87502
87503/* Opcode: ElseNotEq * P2 * * *
87504**
87505** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
87506** can be zero or more OP_ReleaseReg opcodes intervening, but no other
87507** opcodes are allowed to occur between this instruction and the previous
87508** OP_Lt or OP_Gt. Furthermore, the prior OP_Lt or OP_Gt must have the
87509** SQLITE_STOREP2 bit set in the P5 field.
87510**
87511** If result of an OP_Eq comparison on the same two operands as the
87512** prior OP_Lt or OP_Gt would have been NULL or false (0), then then
87513** jump to P2. If the result of an OP_Eq comparison on the two previous
87514** operands would have been true (1), then fall through.
87515*/
87516case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
87517
87518#ifdef SQLITE_DEBUG
87519 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
87520 ** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening
87521 ** OP_ReleaseReg opcodes */
87522 int iAddr;
87523 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
87524 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
87525 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
87526 assert( aOp[iAddr].p5 & SQLITE_STOREP2 );
87527 break;
87528 }
87529#endif /* SQLITE_DEBUG */
87530 VdbeBranchTaken(iCompare!=0, 2);
87531 if( iCompare!=0 ) goto jump_to_p2;
87532 break;
87533}
87534
87535
87536/* Opcode: Permutation * * * P4 *
87537**
87538** Set the permutation used by the OP_Compare operator in the next
87539** instruction. The permutation is stored in the P4 operand.
87540**
87541** The permutation is only valid until the next OP_Compare that has
87542** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
87543** occur immediately prior to the OP_Compare.
87544**
87545** The first integer in the P4 integer array is the length of the array
87546** and does not become part of the permutation.
87547*/
87548case OP_Permutation: {
87549 assert( pOp->p4type==P4_INTARRAY );
87550 assert( pOp->p4.ai );
87551 assert( pOp[1].opcode==OP_Compare );
87552 assert( pOp[1].p5 & OPFLAG_PERMUTE );
87553 break;
87554}
87555
87556/* Opcode: Compare P1 P2 P3 P4 P5
87557** Synopsis: r[P1@P3] <-> r[P2@P3]
87558**
87559** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
87560** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
87561** the comparison for use by the next OP_Jump instruct.
87562**
87563** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
87564** determined by the most recent OP_Permutation operator. If the
87565** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
87566** order.
87567**
87568** P4 is a KeyInfo structure that defines collating sequences and sort
87569** orders for the comparison. The permutation applies to registers
87570** only. The KeyInfo elements are used sequentially.
87571**
87572** The comparison is a sort comparison, so NULLs compare equal,
87573** NULLs are less than numbers, numbers are less than strings,
87574** and strings are less than blobs.
87575*/
87576case OP_Compare: {
87577 int n;
87578 int i;
87579 int p1;
87580 int p2;
87581 const KeyInfo *pKeyInfo;
87582 u32 idx;
87583 CollSeq *pColl; /* Collating sequence to use on this term */
87584 int bRev; /* True for DESCENDING sort order */
87585 u32 *aPermute; /* The permutation */
87586
87587 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
87588 aPermute = 0;
87589 }else{
87590 assert( pOp>aOp );
87591 assert( pOp[-1].opcode==OP_Permutation );
87592 assert( pOp[-1].p4type==P4_INTARRAY );
87593 aPermute = pOp[-1].p4.ai + 1;
87594 assert( aPermute!=0 );
87595 }
87596 n = pOp->p3;
87597 pKeyInfo = pOp->p4.pKeyInfo;
87598 assert( n>0 );
87599 assert( pKeyInfo!=0 );
87600 p1 = pOp->p1;
87601 p2 = pOp->p2;
87602#ifdef SQLITE_DEBUG
87603 if( aPermute ){
87604 int k, mx = 0;
87605 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
87606 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
87607 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
87608 }else{
87609 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
87610 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
87611 }
87612#endif /* SQLITE_DEBUG */
87613 for(i=0; i<n; i++){
87614 idx = aPermute ? aPermute[i] : (u32)i;
87615 assert( memIsValid(&aMem[p1+idx]) );
87616 assert( memIsValid(&aMem[p2+idx]) );
87617 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
87618 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
87619 assert( i<pKeyInfo->nKeyField );
87620 pColl = pKeyInfo->aColl[i];
87621 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
87622 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
87623 if( iCompare ){
87624 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
87625 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
87626 ){
87627 iCompare = -iCompare;
87628 }
87629 if( bRev ) iCompare = -iCompare;
87630 break;
87631 }
87632 }
87633 break;
87634}
87635
87636/* Opcode: Jump P1 P2 P3 * *
87637**
87638** Jump to the instruction at address P1, P2, or P3 depending on whether
87639** in the most recent OP_Compare instruction the P1 vector was less than
87640** equal to, or greater than the P2 vector, respectively.
87641*/
87642case OP_Jump: { /* jump */
87643 if( iCompare<0 ){
87644 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
87645 }else if( iCompare==0 ){
87646 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
87647 }else{
87648 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
87649 }
87650 break;
87651}
87652
87653/* Opcode: And P1 P2 P3 * *
87654** Synopsis: r[P3]=(r[P1] && r[P2])
87655**
87656** Take the logical AND of the values in registers P1 and P2 and
87657** write the result into register P3.
87658**
87659** If either P1 or P2 is 0 (false) then the result is 0 even if
87660** the other input is NULL. A NULL and true or two NULLs give
87661** a NULL output.
87662*/
87663/* Opcode: Or P1 P2 P3 * *
87664** Synopsis: r[P3]=(r[P1] || r[P2])
87665**
87666** Take the logical OR of the values in register P1 and P2 and
87667** store the answer in register P3.
87668**
87669** If either P1 or P2 is nonzero (true) then the result is 1 (true)
87670** even if the other input is NULL. A NULL and false or two NULLs
87671** give a NULL output.
87672*/
87673case OP_And: /* same as TK_AND, in1, in2, out3 */
87674case OP_Or: { /* same as TK_OR, in1, in2, out3 */
87675 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
87676 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
87677
87678 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
87679 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
87680 if( pOp->opcode==OP_And ){
87681 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
87682 v1 = and_logic[v1*3+v2];
87683 }else{
87684 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
87685 v1 = or_logic[v1*3+v2];
87686 }
87687 pOut = &aMem[pOp->p3];
87688 if( v1==2 ){
87689 MemSetTypeFlag(pOut, MEM_Null);
87690 }else{
87691 pOut->u.i = v1;
87692 MemSetTypeFlag(pOut, MEM_Int);
87693 }
87694 break;
87695}
87696
87697/* Opcode: IsTrue P1 P2 P3 P4 *
87698** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
87699**
87700** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
87701** IS NOT FALSE operators.
87702**
87703** Interpret the value in register P1 as a boolean value. Store that
87704** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
87705** NULL, then the P3 is stored in register P2. Invert the answer if P4
87706** is 1.
87707**
87708** The logic is summarized like this:
87709**
87710** <ul>
87711** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
87712** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
87713** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
87714** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
87715** </ul>
87716*/
87717case OP_IsTrue: { /* in1, out2 */
87718 assert( pOp->p4type==P4_INT32 );
87719 assert( pOp->p4.i==0 || pOp->p4.i==1 );
87720 assert( pOp->p3==0 || pOp->p3==1 );
87721 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
87722 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
87723 break;
87724}
87725
87726/* Opcode: Not P1 P2 * * *
87727** Synopsis: r[P2]= !r[P1]
87728**
87729** Interpret the value in register P1 as a boolean value. Store the
87730** boolean complement in register P2. If the value in register P1 is
87731** NULL, then a NULL is stored in P2.
87732*/
87733case OP_Not: { /* same as TK_NOT, in1, out2 */
87734 pIn1 = &aMem[pOp->p1];
87735 pOut = &aMem[pOp->p2];
87736 if( (pIn1->flags & MEM_Null)==0 ){
87737 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
87738 }else{
87739 sqlite3VdbeMemSetNull(pOut);
87740 }
87741 break;
87742}
87743
87744/* Opcode: BitNot P1 P2 * * *
87745** Synopsis: r[P2]= ~r[P1]
87746**
87747** Interpret the content of register P1 as an integer. Store the
87748** ones-complement of the P1 value into register P2. If P1 holds
87749** a NULL then store a NULL in P2.
87750*/
87751case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
87752 pIn1 = &aMem[pOp->p1];
87753 pOut = &aMem[pOp->p2];
87754 sqlite3VdbeMemSetNull(pOut);
87755 if( (pIn1->flags & MEM_Null)==0 ){
87756 pOut->flags = MEM_Int;
87757 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
87758 }
87759 break;
87760}
87761
87762/* Opcode: Once P1 P2 * * *
87763**
87764** Fall through to the next instruction the first time this opcode is
87765** encountered on each invocation of the byte-code program. Jump to P2
87766** on the second and all subsequent encounters during the same invocation.
87767**
87768** Top-level programs determine first invocation by comparing the P1
87769** operand against the P1 operand on the OP_Init opcode at the beginning
87770** of the program. If the P1 values differ, then fall through and make
87771** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
87772** the same then take the jump.
87773**
87774** For subprograms, there is a bitmask in the VdbeFrame that determines
87775** whether or not the jump should be taken. The bitmask is necessary
87776** because the self-altering code trick does not work for recursive
87777** triggers.
87778*/
87779case OP_Once: { /* jump */
87780 u32 iAddr; /* Address of this instruction */
87781 assert( p->aOp[0].opcode==OP_Init );
87782 if( p->pFrame ){
87783 iAddr = (int)(pOp - p->aOp);
87784 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
87785 VdbeBranchTaken(1, 2);
87786 goto jump_to_p2;
87787 }
87788 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
87789 }else{
87790 if( p->aOp[0].p1==pOp->p1 ){
87791 VdbeBranchTaken(1, 2);
87792 goto jump_to_p2;
87793 }
87794 }
87795 VdbeBranchTaken(0, 2);
87796 pOp->p1 = p->aOp[0].p1;
87797 break;
87798}
87799
87800/* Opcode: If P1 P2 P3 * *
87801**
87802** Jump to P2 if the value in register P1 is true. The value
87803** is considered true if it is numeric and non-zero. If the value
87804** in P1 is NULL then take the jump if and only if P3 is non-zero.
87805*/
87806case OP_If: { /* jump, in1 */
87807 int c;
87808 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
87809 VdbeBranchTaken(c!=0, 2);
87810 if( c ) goto jump_to_p2;
87811 break;
87812}
87813
87814/* Opcode: IfNot P1 P2 P3 * *
87815**
87816** Jump to P2 if the value in register P1 is False. The value
87817** is considered false if it has a numeric value of zero. If the value
87818** in P1 is NULL then take the jump if and only if P3 is non-zero.
87819*/
87820case OP_IfNot: { /* jump, in1 */
87821 int c;
87822 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
87823 VdbeBranchTaken(c!=0, 2);
87824 if( c ) goto jump_to_p2;
87825 break;
87826}
87827
87828/* Opcode: IsNull P1 P2 * * *
87829** Synopsis: if r[P1]==NULL goto P2
87830**
87831** Jump to P2 if the value in register P1 is NULL.
87832*/
87833case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
87834 pIn1 = &aMem[pOp->p1];
87835 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
87836 if( (pIn1->flags & MEM_Null)!=0 ){
87837 goto jump_to_p2;
87838 }
87839 break;
87840}
87841
87842/* Opcode: NotNull P1 P2 * * *
87843** Synopsis: if r[P1]!=NULL goto P2
87844**
87845** Jump to P2 if the value in register P1 is not NULL.
87846*/
87847case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
87848 pIn1 = &aMem[pOp->p1];
87849 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
87850 if( (pIn1->flags & MEM_Null)==0 ){
87851 goto jump_to_p2;
87852 }
87853 break;
87854}
87855
87856/* Opcode: IfNullRow P1 P2 P3 * *
87857** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
87858**
87859** Check the cursor P1 to see if it is currently pointing at a NULL row.
87860** If it is, then set register P3 to NULL and jump immediately to P2.
87861** If P1 is not on a NULL row, then fall through without making any
87862** changes.
87863*/
87864case OP_IfNullRow: { /* jump */
87865 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87866 assert( p->apCsr[pOp->p1]!=0 );
87867 if( p->apCsr[pOp->p1]->nullRow ){
87868 sqlite3VdbeMemSetNull(aMem + pOp->p3);
87869 goto jump_to_p2;
87870 }
87871 break;
87872}
87873
87874#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
87875/* Opcode: Offset P1 P2 P3 * *
87876** Synopsis: r[P3] = sqlite_offset(P1)
87877**
87878** Store in register r[P3] the byte offset into the database file that is the
87879** start of the payload for the record at which that cursor P1 is currently
87880** pointing.
87881**
87882** P2 is the column number for the argument to the sqlite_offset() function.
87883** This opcode does not use P2 itself, but the P2 value is used by the
87884** code generator. The P1, P2, and P3 operands to this opcode are the
87885** same as for OP_Column.
87886**
87887** This opcode is only available if SQLite is compiled with the
87888** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
87889*/
87890case OP_Offset: { /* out3 */
87891 VdbeCursor *pC; /* The VDBE cursor */
87892 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87893 pC = p->apCsr[pOp->p1];
87894 pOut = &p->aMem[pOp->p3];
87895 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
87896 sqlite3VdbeMemSetNull(pOut);
87897 }else{
87898 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
87899 }
87900 break;
87901}
87902#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
87903
87904/* Opcode: Column P1 P2 P3 P4 P5
87905** Synopsis: r[P3]=PX
87906**
87907** Interpret the data that cursor P1 points to as a structure built using
87908** the MakeRecord instruction. (See the MakeRecord opcode for additional
87909** information about the format of the data.) Extract the P2-th column
87910** from this record. If there are less that (P2+1)
87911** values in the record, extract a NULL.
87912**
87913** The value extracted is stored in register P3.
87914**
87915** If the record contains fewer than P2 fields, then extract a NULL. Or,
87916** if the P4 argument is a P4_MEM use the value of the P4 argument as
87917** the result.
87918**
87919** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
87920** the result is guaranteed to only be used as the argument of a length()
87921** or typeof() function, respectively. The loading of large blobs can be
87922** skipped for length() and all content loading can be skipped for typeof().
87923*/
87924case OP_Column: {
87925 u32 p2; /* column number to retrieve */
87926 VdbeCursor *pC; /* The VDBE cursor */
87927 BtCursor *pCrsr; /* The BTree cursor */
87928 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
87929 int len; /* The length of the serialized data for the column */
87930 int i; /* Loop counter */
87931 Mem *pDest; /* Where to write the extracted value */
87932 Mem sMem; /* For storing the record being decoded */
87933 const u8 *zData; /* Part of the record being decoded */
87934 const u8 *zHdr; /* Next unparsed byte of the header */
87935 const u8 *zEndHdr; /* Pointer to first byte after the header */
87936 u64 offset64; /* 64-bit offset */
87937 u32 t; /* A type code from the record header */
87938 Mem *pReg; /* PseudoTable input register */
87939
87940 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87941 pC = p->apCsr[pOp->p1];
87942 assert( pC!=0 );
87943 p2 = (u32)pOp->p2;
87944
87945 /* If the cursor cache is stale (meaning it is not currently point at
87946 ** the correct row) then bring it up-to-date by doing the necessary
87947 ** B-Tree seek. */
87948 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
87949 if( rc ) goto abort_due_to_error;
87950
87951 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
87952 pDest = &aMem[pOp->p3];
87953 memAboutToChange(p, pDest);
87954 assert( pC!=0 );
87955 assert( p2<(u32)pC->nField );
87956 aOffset = pC->aOffset;
87957 assert( pC->eCurType!=CURTYPE_VTAB );
87958 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
87959 assert( pC->eCurType!=CURTYPE_SORTER );
87960
87961 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
87962 if( pC->nullRow ){
87963 if( pC->eCurType==CURTYPE_PSEUDO ){
87964 /* For the special case of as pseudo-cursor, the seekResult field
87965 ** identifies the register that holds the record */
87966 assert( pC->seekResult>0 );
87967 pReg = &aMem[pC->seekResult];
87968 assert( pReg->flags & MEM_Blob );
87969 assert( memIsValid(pReg) );
87970 pC->payloadSize = pC->szRow = pReg->n;
87971 pC->aRow = (u8*)pReg->z;
87972 }else{
87973 sqlite3VdbeMemSetNull(pDest);
87974 goto op_column_out;
87975 }
87976 }else{
87977 pCrsr = pC->uc.pCursor;
87978 assert( pC->eCurType==CURTYPE_BTREE );
87979 assert( pCrsr );
87980 assert( sqlite3BtreeCursorIsValid(pCrsr) );
87981 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
87982 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
87983 assert( pC->szRow<=pC->payloadSize );
87984 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
87985 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
87986 goto too_big;
87987 }
87988 }
87989 pC->cacheStatus = p->cacheCtr;
87990 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
87991 pC->nHdrParsed = 0;
87992
87993
87994 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
87995 /* pC->aRow does not have to hold the entire row, but it does at least
87996 ** need to cover the header of the record. If pC->aRow does not contain
87997 ** the complete header, then set it to zero, forcing the header to be
87998 ** dynamically allocated. */
87999 pC->aRow = 0;
88000 pC->szRow = 0;
88001
88002 /* Make sure a corrupt database has not given us an oversize header.
88003 ** Do this now to avoid an oversize memory allocation.
88004 **
88005 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
88006 ** types use so much data space that there can only be 4096 and 32 of
88007 ** them, respectively. So the maximum header length results from a
88008 ** 3-byte type for each of the maximum of 32768 columns plus three
88009 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
88010 */
88011 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
88012 goto op_column_corrupt;
88013 }
88014 }else{
88015 /* This is an optimization. By skipping over the first few tests
88016 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
88017 ** measurable performance gain.
88018 **
88019 ** This branch is taken even if aOffset[0]==0. Such a record is never
88020 ** generated by SQLite, and could be considered corruption, but we
88021 ** accept it for historical reasons. When aOffset[0]==0, the code this
88022 ** branch jumps to reads past the end of the record, but never more
88023 ** than a few bytes. Even if the record occurs at the end of the page
88024 ** content area, the "page header" comes after the page content and so
88025 ** this overread is harmless. Similar overreads can occur for a corrupt
88026 ** database file.
88027 */
88028 zData = pC->aRow;
88029 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
88030 testcase( aOffset[0]==0 );
88031 goto op_column_read_header;
88032 }
88033 }
88034
88035 /* Make sure at least the first p2+1 entries of the header have been
88036 ** parsed and valid information is in aOffset[] and pC->aType[].
88037 */
88038 if( pC->nHdrParsed<=p2 ){
88039 /* If there is more header available for parsing in the record, try
88040 ** to extract additional fields up through the p2+1-th field
88041 */
88042 if( pC->iHdrOffset<aOffset[0] ){
88043 /* Make sure zData points to enough of the record to cover the header. */
88044 if( pC->aRow==0 ){
88045 memset(&sMem, 0, sizeof(sMem));
88046 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
88047 if( rc!=SQLITE_OK ) goto abort_due_to_error;
88048 zData = (u8*)sMem.z;
88049 }else{
88050 zData = pC->aRow;
88051 }
88052
88053 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
88054 op_column_read_header:
88055 i = pC->nHdrParsed;
88056 offset64 = aOffset[i];
88057 zHdr = zData + pC->iHdrOffset;
88058 zEndHdr = zData + aOffset[0];
88059 testcase( zHdr>=zEndHdr );
88060 do{
88061 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
88062 zHdr++;
88063 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
88064 }else{
88065 zHdr += sqlite3GetVarint32(zHdr, &t);
88066 pC->aType[i] = t;
88067 offset64 += sqlite3VdbeSerialTypeLen(t);
88068 }
88069 aOffset[++i] = (u32)(offset64 & 0xffffffff);
88070 }while( (u32)i<=p2 && zHdr<zEndHdr );
88071
88072 /* The record is corrupt if any of the following are true:
88073 ** (1) the bytes of the header extend past the declared header size
88074 ** (2) the entire header was used but not all data was used
88075 ** (3) the end of the data extends beyond the end of the record.
88076 */
88077 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
88078 || (offset64 > pC->payloadSize)
88079 ){
88080 if( aOffset[0]==0 ){
88081 i = 0;
88082 zHdr = zEndHdr;
88083 }else{
88084 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
88085 goto op_column_corrupt;
88086 }
88087 }
88088
88089 pC->nHdrParsed = i;
88090 pC->iHdrOffset = (u32)(zHdr - zData);
88091 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
88092 }else{
88093 t = 0;
88094 }
88095
88096 /* If after trying to extract new entries from the header, nHdrParsed is
88097 ** still not up to p2, that means that the record has fewer than p2
88098 ** columns. So the result will be either the default value or a NULL.
88099 */
88100 if( pC->nHdrParsed<=p2 ){
88101 if( pOp->p4type==P4_MEM ){
88102 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
88103 }else{
88104 sqlite3VdbeMemSetNull(pDest);
88105 }
88106 goto op_column_out;
88107 }
88108 }else{
88109 t = pC->aType[p2];
88110 }
88111
88112 /* Extract the content for the p2+1-th column. Control can only
88113 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
88114 ** all valid.
88115 */
88116 assert( p2<pC->nHdrParsed );
88117 assert( rc==SQLITE_OK );
88118 assert( sqlite3VdbeCheckMemInvariants(pDest) );
88119 if( VdbeMemDynamic(pDest) ){
88120 sqlite3VdbeMemSetNull(pDest);
88121 }
88122 assert( t==pC->aType[p2] );
88123 if( pC->szRow>=aOffset[p2+1] ){
88124 /* This is the common case where the desired content fits on the original
88125 ** page - where the content is not on an overflow page */
88126 zData = pC->aRow + aOffset[p2];
88127 if( t<12 ){
88128 sqlite3VdbeSerialGet(zData, t, pDest);
88129 }else{
88130 /* If the column value is a string, we need a persistent value, not
88131 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
88132 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
88133 */
88134 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
88135 pDest->n = len = (t-12)/2;
88136 pDest->enc = encoding;
88137 if( pDest->szMalloc < len+2 ){
88138 pDest->flags = MEM_Null;
88139 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
88140 }else{
88141 pDest->z = pDest->zMalloc;
88142 }
88143 memcpy(pDest->z, zData, len);
88144 pDest->z[len] = 0;
88145 pDest->z[len+1] = 0;
88146 pDest->flags = aFlag[t&1];
88147 }
88148 }else{
88149 pDest->enc = encoding;
88150 /* This branch happens only when content is on overflow pages */
88151 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
88152 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
88153 || (len = sqlite3VdbeSerialTypeLen(t))==0
88154 ){
88155 /* Content is irrelevant for
88156 ** 1. the typeof() function,
88157 ** 2. the length(X) function if X is a blob, and
88158 ** 3. if the content length is zero.
88159 ** So we might as well use bogus content rather than reading
88160 ** content from disk.
88161 **
88162 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
88163 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
88164 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
88165 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
88166 ** and it begins with a bunch of zeros.
88167 */
88168 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
88169 }else{
88170 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
88171 if( rc!=SQLITE_OK ) goto abort_due_to_error;
88172 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
88173 pDest->flags &= ~MEM_Ephem;
88174 }
88175 }
88176
88177op_column_out:
88178 UPDATE_MAX_BLOBSIZE(pDest);
88179 REGISTER_TRACE(pOp->p3, pDest);
88180 break;
88181
88182op_column_corrupt:
88183 if( aOp[0].p3>0 ){
88184 pOp = &aOp[aOp[0].p3-1];
88185 break;
88186 }else{
88187 rc = SQLITE_CORRUPT_BKPT;
88188 goto abort_due_to_error;
88189 }
88190}
88191
88192/* Opcode: Affinity P1 P2 * P4 *
88193** Synopsis: affinity(r[P1@P2])
88194**
88195** Apply affinities to a range of P2 registers starting with P1.
88196**
88197** P4 is a string that is P2 characters long. The N-th character of the
88198** string indicates the column affinity that should be used for the N-th
88199** memory cell in the range.
88200*/
88201case OP_Affinity: {
88202 const char *zAffinity; /* The affinity to be applied */
88203
88204 zAffinity = pOp->p4.z;
88205 assert( zAffinity!=0 );
88206 assert( pOp->p2>0 );
88207 assert( zAffinity[pOp->p2]==0 );
88208 pIn1 = &aMem[pOp->p1];
88209 while( 1 /*exit-by-break*/ ){
88210 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
88211 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
88212 applyAffinity(pIn1, zAffinity[0], encoding);
88213 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
88214 /* When applying REAL affinity, if the result is still an MEM_Int
88215 ** that will fit in 6 bytes, then change the type to MEM_IntReal
88216 ** so that we keep the high-resolution integer value but know that
88217 ** the type really wants to be REAL. */
88218 testcase( pIn1->u.i==140737488355328LL );
88219 testcase( pIn1->u.i==140737488355327LL );
88220 testcase( pIn1->u.i==-140737488355328LL );
88221 testcase( pIn1->u.i==-140737488355329LL );
88222 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
88223 pIn1->flags |= MEM_IntReal;
88224 pIn1->flags &= ~MEM_Int;
88225 }else{
88226 pIn1->u.r = (double)pIn1->u.i;
88227 pIn1->flags |= MEM_Real;
88228 pIn1->flags &= ~MEM_Int;
88229 }
88230 }
88231 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
88232 zAffinity++;
88233 if( zAffinity[0]==0 ) break;
88234 pIn1++;
88235 }
88236 break;
88237}
88238
88239/* Opcode: MakeRecord P1 P2 P3 P4 *
88240** Synopsis: r[P3]=mkrec(r[P1@P2])
88241**
88242** Convert P2 registers beginning with P1 into the [record format]
88243** use as a data record in a database table or as a key
88244** in an index. The OP_Column opcode can decode the record later.
88245**
88246** P4 may be a string that is P2 characters long. The N-th character of the
88247** string indicates the column affinity that should be used for the N-th
88248** field of the index key.
88249**
88250** The mapping from character to affinity is given by the SQLITE_AFF_
88251** macros defined in sqliteInt.h.
88252**
88253** If P4 is NULL then all index fields have the affinity BLOB.
88254**
88255** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
88256** compile-time option is enabled:
88257**
88258** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
88259** of the right-most table that can be null-trimmed.
88260**
88261** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
88262** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
88263** accept no-change records with serial_type 10. This value is
88264** only used inside an assert() and does not affect the end result.
88265*/
88266case OP_MakeRecord: {
88267 Mem *pRec; /* The new record */
88268 u64 nData; /* Number of bytes of data space */
88269 int nHdr; /* Number of bytes of header space */
88270 i64 nByte; /* Data space required for this record */
88271 i64 nZero; /* Number of zero bytes at the end of the record */
88272 int nVarint; /* Number of bytes in a varint */
88273 u32 serial_type; /* Type field */
88274 Mem *pData0; /* First field to be combined into the record */
88275 Mem *pLast; /* Last field of the record */
88276 int nField; /* Number of fields in the record */
88277 char *zAffinity; /* The affinity string for the record */
88278 int file_format; /* File format to use for encoding */
88279 u32 len; /* Length of a field */
88280 u8 *zHdr; /* Where to write next byte of the header */
88281 u8 *zPayload; /* Where to write next byte of the payload */
88282
88283 /* Assuming the record contains N fields, the record format looks
88284 ** like this:
88285 **
88286 ** ------------------------------------------------------------------------
88287 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
88288 ** ------------------------------------------------------------------------
88289 **
88290 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
88291 ** and so forth.
88292 **
88293 ** Each type field is a varint representing the serial type of the
88294 ** corresponding data element (see sqlite3VdbeSerialType()). The
88295 ** hdr-size field is also a varint which is the offset from the beginning
88296 ** of the record to data0.
88297 */
88298 nData = 0; /* Number of bytes of data space */
88299 nHdr = 0; /* Number of bytes of header space */
88300 nZero = 0; /* Number of zero bytes at the end of the record */
88301 nField = pOp->p1;
88302 zAffinity = pOp->p4.z;
88303 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
88304 pData0 = &aMem[nField];
88305 nField = pOp->p2;
88306 pLast = &pData0[nField-1];
88307 file_format = p->minWriteFileFormat;
88308
88309 /* Identify the output register */
88310 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
88311 pOut = &aMem[pOp->p3];
88312 memAboutToChange(p, pOut);
88313
88314 /* Apply the requested affinity to all inputs
88315 */
88316 assert( pData0<=pLast );
88317 if( zAffinity ){
88318 pRec = pData0;
88319 do{
88320 applyAffinity(pRec, zAffinity[0], encoding);
88321 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
88322 pRec->flags |= MEM_IntReal;
88323 pRec->flags &= ~(MEM_Int);
88324 }
88325 REGISTER_TRACE((int)(pRec-aMem), pRec);
88326 zAffinity++;
88327 pRec++;
88328 assert( zAffinity[0]==0 || pRec<=pLast );
88329 }while( zAffinity[0] );
88330 }
88331
88332#ifdef SQLITE_ENABLE_NULL_TRIM
88333 /* NULLs can be safely trimmed from the end of the record, as long as
88334 ** as the schema format is 2 or more and none of the omitted columns
88335 ** have a non-NULL default value. Also, the record must be left with
88336 ** at least one field. If P5>0 then it will be one more than the
88337 ** index of the right-most column with a non-NULL default value */
88338 if( pOp->p5 ){
88339 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
88340 pLast--;
88341 nField--;
88342 }
88343 }
88344#endif
88345
88346 /* Loop through the elements that will make up the record to figure
88347 ** out how much space is required for the new record. After this loop,
88348 ** the Mem.uTemp field of each term should hold the serial-type that will
88349 ** be used for that term in the generated record:
88350 **
88351 ** Mem.uTemp value type
88352 ** --------------- ---------------
88353 ** 0 NULL
88354 ** 1 1-byte signed integer
88355 ** 2 2-byte signed integer
88356 ** 3 3-byte signed integer
88357 ** 4 4-byte signed integer
88358 ** 5 6-byte signed integer
88359 ** 6 8-byte signed integer
88360 ** 7 IEEE float
88361 ** 8 Integer constant 0
88362 ** 9 Integer constant 1
88363 ** 10,11 reserved for expansion
88364 ** N>=12 and even BLOB
88365 ** N>=13 and odd text
88366 **
88367 ** The following additional values are computed:
88368 ** nHdr Number of bytes needed for the record header
88369 ** nData Number of bytes of data space needed for the record
88370 ** nZero Zero bytes at the end of the record
88371 */
88372 pRec = pLast;
88373 do{
88374 assert( memIsValid(pRec) );
88375 if( pRec->flags & MEM_Null ){
88376 if( pRec->flags & MEM_Zero ){
88377 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
88378 ** table methods that never invoke sqlite3_result_xxxxx() while
88379 ** computing an unchanging column value in an UPDATE statement.
88380 ** Give such values a special internal-use-only serial-type of 10
88381 ** so that they can be passed through to xUpdate and have
88382 ** a true sqlite3_value_nochange(). */
88383#ifndef SQLITE_ENABLE_NULL_TRIM
88384 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
88385#endif
88386 pRec->uTemp = 10;
88387 }else{
88388 pRec->uTemp = 0;
88389 }
88390 nHdr++;
88391 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
88392 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
88393 i64 i = pRec->u.i;
88394 u64 uu;
88395 testcase( pRec->flags & MEM_Int );
88396 testcase( pRec->flags & MEM_IntReal );
88397 if( i<0 ){
88398 uu = ~i;
88399 }else{
88400 uu = i;
88401 }
88402 nHdr++;
88403 testcase( uu==127 ); testcase( uu==128 );
88404 testcase( uu==32767 ); testcase( uu==32768 );
88405 testcase( uu==8388607 ); testcase( uu==8388608 );
88406 testcase( uu==2147483647 ); testcase( uu==2147483648 );
88407 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
88408 if( uu<=127 ){
88409 if( (i&1)==i && file_format>=4 ){
88410 pRec->uTemp = 8+(u32)uu;
88411 }else{
88412 nData++;
88413 pRec->uTemp = 1;
88414 }
88415 }else if( uu<=32767 ){
88416 nData += 2;
88417 pRec->uTemp = 2;
88418 }else if( uu<=8388607 ){
88419 nData += 3;
88420 pRec->uTemp = 3;
88421 }else if( uu<=2147483647 ){
88422 nData += 4;
88423 pRec->uTemp = 4;
88424 }else if( uu<=140737488355327LL ){
88425 nData += 6;
88426 pRec->uTemp = 5;
88427 }else{
88428 nData += 8;
88429 if( pRec->flags & MEM_IntReal ){
88430 /* If the value is IntReal and is going to take up 8 bytes to store
88431 ** as an integer, then we might as well make it an 8-byte floating
88432 ** point value */
88433 pRec->u.r = (double)pRec->u.i;
88434 pRec->flags &= ~MEM_IntReal;
88435 pRec->flags |= MEM_Real;
88436 pRec->uTemp = 7;
88437 }else{
88438 pRec->uTemp = 6;
88439 }
88440 }
88441 }else if( pRec->flags & MEM_Real ){
88442 nHdr++;
88443 nData += 8;
88444 pRec->uTemp = 7;
88445 }else{
88446 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
88447 assert( pRec->n>=0 );
88448 len = (u32)pRec->n;
88449 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
88450 if( pRec->flags & MEM_Zero ){
88451 serial_type += pRec->u.nZero*2;
88452 if( nData ){
88453 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
88454 len += pRec->u.nZero;
88455 }else{
88456 nZero += pRec->u.nZero;
88457 }
88458 }
88459 nData += len;
88460 nHdr += sqlite3VarintLen(serial_type);
88461 pRec->uTemp = serial_type;
88462 }
88463 if( pRec==pData0 ) break;
88464 pRec--;
88465 }while(1);
88466
88467 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
88468 ** which determines the total number of bytes in the header. The varint
88469 ** value is the size of the header in bytes including the size varint
88470 ** itself. */
88471 testcase( nHdr==126 );
88472 testcase( nHdr==127 );
88473 if( nHdr<=126 ){
88474 /* The common case */
88475 nHdr += 1;
88476 }else{
88477 /* Rare case of a really large header */
88478 nVarint = sqlite3VarintLen(nHdr);
88479 nHdr += nVarint;
88480 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
88481 }
88482 nByte = nHdr+nData;
88483
88484 /* Make sure the output register has a buffer large enough to store
88485 ** the new record. The output register (pOp->p3) is not allowed to
88486 ** be one of the input registers (because the following call to
88487 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
88488 */
88489 if( nByte+nZero<=pOut->szMalloc ){
88490 /* The output register is already large enough to hold the record.
88491 ** No error checks or buffer enlargement is required */
88492 pOut->z = pOut->zMalloc;
88493 }else{
88494 /* Need to make sure that the output is not too big and then enlarge
88495 ** the output register to hold the full result */
88496 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
88497 goto too_big;
88498 }
88499 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
88500 goto no_mem;
88501 }
88502 }
88503 pOut->n = (int)nByte;
88504 pOut->flags = MEM_Blob;
88505 if( nZero ){
88506 pOut->u.nZero = nZero;
88507 pOut->flags |= MEM_Zero;
88508 }
88509 UPDATE_MAX_BLOBSIZE(pOut);
88510 zHdr = (u8 *)pOut->z;
88511 zPayload = zHdr + nHdr;
88512
88513 /* Write the record */
88514 zHdr += putVarint32(zHdr, nHdr);
88515 assert( pData0<=pLast );
88516 pRec = pData0;
88517 do{
88518 serial_type = pRec->uTemp;
88519 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
88520 ** additional varints, one per column. */
88521 zHdr += putVarint32(zHdr, serial_type); /* serial type */
88522 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
88523 ** immediately follow the header. */
88524 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
88525 }while( (++pRec)<=pLast );
88526 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
88527 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
88528
88529 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
88530 REGISTER_TRACE(pOp->p3, pOut);
88531 break;
88532}
88533
88534/* Opcode: Count P1 P2 p3 * *
88535** Synopsis: r[P2]=count()
88536**
88537** Store the number of entries (an integer value) in the table or index
88538** opened by cursor P1 in register P2.
88539**
88540** If P3==0, then an exact count is obtained, which involves visiting
88541** every btree page of the table. But if P3 is non-zero, an estimate
88542** is returned based on the current cursor position.
88543*/
88544case OP_Count: { /* out2 */
88545 i64 nEntry;
88546 BtCursor *pCrsr;
88547
88548 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
88549 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
88550 assert( pCrsr );
88551 if( pOp->p3 ){
88552 nEntry = sqlite3BtreeRowCountEst(pCrsr);
88553 }else{
88554 nEntry = 0; /* Not needed. Only used to silence a warning. */
88555 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
88556 if( rc ) goto abort_due_to_error;
88557 }
88558 pOut = out2Prerelease(p, pOp);
88559 pOut->u.i = nEntry;
88560 goto check_for_interrupt;
88561}
88562
88563/* Opcode: Savepoint P1 * * P4 *
88564**
88565** Open, release or rollback the savepoint named by parameter P4, depending
88566** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
88567** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
88568** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
88569*/
88570case OP_Savepoint: {
88571 int p1; /* Value of P1 operand */
88572 char *zName; /* Name of savepoint */
88573 int nName;
88574 Savepoint *pNew;
88575 Savepoint *pSavepoint;
88576 Savepoint *pTmp;
88577 int iSavepoint;
88578 int ii;
88579
88580 p1 = pOp->p1;
88581 zName = pOp->p4.z;
88582
88583 /* Assert that the p1 parameter is valid. Also that if there is no open
88584 ** transaction, then there cannot be any savepoints.
88585 */
88586 assert( db->pSavepoint==0 || db->autoCommit==0 );
88587 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
88588 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
88589 assert( checkSavepointCount(db) );
88590 assert( p->bIsReader );
88591
88592 if( p1==SAVEPOINT_BEGIN ){
88593 if( db->nVdbeWrite>0 ){
88594 /* A new savepoint cannot be created if there are active write
88595 ** statements (i.e. open read/write incremental blob handles).
88596 */
88597 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
88598 rc = SQLITE_BUSY;
88599 }else{
88600 nName = sqlite3Strlen30(zName);
88601
88602#ifndef SQLITE_OMIT_VIRTUALTABLE
88603 /* This call is Ok even if this savepoint is actually a transaction
88604 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
88605 ** If this is a transaction savepoint being opened, it is guaranteed
88606 ** that the db->aVTrans[] array is empty. */
88607 assert( db->autoCommit==0 || db->nVTrans==0 );
88608 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
88609 db->nStatement+db->nSavepoint);
88610 if( rc!=SQLITE_OK ) goto abort_due_to_error;
88611#endif
88612
88613 /* Create a new savepoint structure. */
88614 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
88615 if( pNew ){
88616 pNew->zName = (char *)&pNew[1];
88617 memcpy(pNew->zName, zName, nName+1);
88618
88619 /* If there is no open transaction, then mark this as a special
88620 ** "transaction savepoint". */
88621 if( db->autoCommit ){
88622 db->autoCommit = 0;
88623 db->isTransactionSavepoint = 1;
88624 }else{
88625 db->nSavepoint++;
88626 }
88627
88628 /* Link the new savepoint into the database handle's list. */
88629 pNew->pNext = db->pSavepoint;
88630 db->pSavepoint = pNew;
88631 pNew->nDeferredCons = db->nDeferredCons;
88632 pNew->nDeferredImmCons = db->nDeferredImmCons;
88633 }
88634 }
88635 }else{
88636 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
88637 iSavepoint = 0;
88638
88639 /* Find the named savepoint. If there is no such savepoint, then an
88640 ** an error is returned to the user. */
88641 for(
88642 pSavepoint = db->pSavepoint;
88643 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
88644 pSavepoint = pSavepoint->pNext
88645 ){
88646 iSavepoint++;
88647 }
88648 if( !pSavepoint ){
88649 sqlite3VdbeError(p, "no such savepoint: %s", zName);
88650 rc = SQLITE_ERROR;
88651 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
88652 /* It is not possible to release (commit) a savepoint if there are
88653 ** active write statements.
88654 */
88655 sqlite3VdbeError(p, "cannot release savepoint - "
88656 "SQL statements in progress");
88657 rc = SQLITE_BUSY;
88658 }else{
88659
88660 /* Determine whether or not this is a transaction savepoint. If so,
88661 ** and this is a RELEASE command, then the current transaction
88662 ** is committed.
88663 */
88664 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
88665 if( isTransaction && p1==SAVEPOINT_RELEASE ){
88666 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
88667 goto vdbe_return;
88668 }
88669 db->autoCommit = 1;
88670 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
88671 p->pc = (int)(pOp - aOp);
88672 db->autoCommit = 0;
88673 p->rc = rc = SQLITE_BUSY;
88674 goto vdbe_return;
88675 }
88676 rc = p->rc;
88677 if( rc ){
88678 db->autoCommit = 0;
88679 }else{
88680 db->isTransactionSavepoint = 0;
88681 }
88682 }else{
88683 int isSchemaChange;
88684 iSavepoint = db->nSavepoint - iSavepoint - 1;
88685 if( p1==SAVEPOINT_ROLLBACK ){
88686 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
88687 for(ii=0; ii<db->nDb; ii++){
88688 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
88689 SQLITE_ABORT_ROLLBACK,
88690 isSchemaChange==0);
88691 if( rc!=SQLITE_OK ) goto abort_due_to_error;
88692 }
88693 }else{
88694 assert( p1==SAVEPOINT_RELEASE );
88695 isSchemaChange = 0;
88696 }
88697 for(ii=0; ii<db->nDb; ii++){
88698 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
88699 if( rc!=SQLITE_OK ){
88700 goto abort_due_to_error;
88701 }
88702 }
88703 if( isSchemaChange ){
88704 sqlite3ExpirePreparedStatements(db, 0);
88705 sqlite3ResetAllSchemasOfConnection(db);
88706 db->mDbFlags |= DBFLAG_SchemaChange;
88707 }
88708 }
88709 if( rc ) goto abort_due_to_error;
88710
88711 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
88712 ** savepoints nested inside of the savepoint being operated on. */
88713 while( db->pSavepoint!=pSavepoint ){
88714 pTmp = db->pSavepoint;
88715 db->pSavepoint = pTmp->pNext;
88716 sqlite3DbFree(db, pTmp);
88717 db->nSavepoint--;
88718 }
88719
88720 /* If it is a RELEASE, then destroy the savepoint being operated on
88721 ** too. If it is a ROLLBACK TO, then set the number of deferred
88722 ** constraint violations present in the database to the value stored
88723 ** when the savepoint was created. */
88724 if( p1==SAVEPOINT_RELEASE ){
88725 assert( pSavepoint==db->pSavepoint );
88726 db->pSavepoint = pSavepoint->pNext;
88727 sqlite3DbFree(db, pSavepoint);
88728 if( !isTransaction ){
88729 db->nSavepoint--;
88730 }
88731 }else{
88732 assert( p1==SAVEPOINT_ROLLBACK );
88733 db->nDeferredCons = pSavepoint->nDeferredCons;
88734 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
88735 }
88736
88737 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
88738 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
88739 if( rc!=SQLITE_OK ) goto abort_due_to_error;
88740 }
88741 }
88742 }
88743 if( rc ) goto abort_due_to_error;
88744
88745 break;
88746}
88747
88748/* Opcode: AutoCommit P1 P2 * * *
88749**
88750** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
88751** back any currently active btree transactions. If there are any active
88752** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
88753** there are active writing VMs or active VMs that use shared cache.
88754**
88755** This instruction causes the VM to halt.
88756*/
88757case OP_AutoCommit: {
88758 int desiredAutoCommit;
88759 int iRollback;
88760
88761 desiredAutoCommit = pOp->p1;
88762 iRollback = pOp->p2;
88763 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
88764 assert( desiredAutoCommit==1 || iRollback==0 );
88765 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
88766 assert( p->bIsReader );
88767
88768 if( desiredAutoCommit!=db->autoCommit ){
88769 if( iRollback ){
88770 assert( desiredAutoCommit==1 );
88771 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
88772 db->autoCommit = 1;
88773 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
88774 /* If this instruction implements a COMMIT and other VMs are writing
88775 ** return an error indicating that the other VMs must complete first.
88776 */
88777 sqlite3VdbeError(p, "cannot commit transaction - "
88778 "SQL statements in progress");
88779 rc = SQLITE_BUSY;
88780 goto abort_due_to_error;
88781 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
88782 goto vdbe_return;
88783 }else{
88784 db->autoCommit = (u8)desiredAutoCommit;
88785 }
88786 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
88787 p->pc = (int)(pOp - aOp);
88788 db->autoCommit = (u8)(1-desiredAutoCommit);
88789 p->rc = rc = SQLITE_BUSY;
88790 goto vdbe_return;
88791 }
88792 sqlite3CloseSavepoints(db);
88793 if( p->rc==SQLITE_OK ){
88794 rc = SQLITE_DONE;
88795 }else{
88796 rc = SQLITE_ERROR;
88797 }
88798 goto vdbe_return;
88799 }else{
88800 sqlite3VdbeError(p,
88801 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
88802 (iRollback)?"cannot rollback - no transaction is active":
88803 "cannot commit - no transaction is active"));
88804
88805 rc = SQLITE_ERROR;
88806 goto abort_due_to_error;
88807 }
88808 /*NOTREACHED*/ assert(0);
88809}
88810
88811/* Opcode: Transaction P1 P2 P3 P4 P5
88812**
88813** Begin a transaction on database P1 if a transaction is not already
88814** active.
88815** If P2 is non-zero, then a write-transaction is started, or if a
88816** read-transaction is already active, it is upgraded to a write-transaction.
88817** If P2 is zero, then a read-transaction is started.
88818**
88819** P1 is the index of the database file on which the transaction is
88820** started. Index 0 is the main database file and index 1 is the
88821** file used for temporary tables. Indices of 2 or more are used for
88822** attached databases.
88823**
88824** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
88825** true (this flag is set if the Vdbe may modify more than one row and may
88826** throw an ABORT exception), a statement transaction may also be opened.
88827** More specifically, a statement transaction is opened iff the database
88828** connection is currently not in autocommit mode, or if there are other
88829** active statements. A statement transaction allows the changes made by this
88830** VDBE to be rolled back after an error without having to roll back the
88831** entire transaction. If no error is encountered, the statement transaction
88832** will automatically commit when the VDBE halts.
88833**
88834** If P5!=0 then this opcode also checks the schema cookie against P3
88835** and the schema generation counter against P4.
88836** The cookie changes its value whenever the database schema changes.
88837** This operation is used to detect when that the cookie has changed
88838** and that the current process needs to reread the schema. If the schema
88839** cookie in P3 differs from the schema cookie in the database header or
88840** if the schema generation counter in P4 differs from the current
88841** generation counter, then an SQLITE_SCHEMA error is raised and execution
88842** halts. The sqlite3_step() wrapper function might then reprepare the
88843** statement and rerun it from the beginning.
88844*/
88845case OP_Transaction: {
88846 Btree *pBt;
88847 int iMeta = 0;
88848
88849 assert( p->bIsReader );
88850 assert( p->readOnly==0 || pOp->p2==0 );
88851 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88852 assert( DbMaskTest(p->btreeMask, pOp->p1) );
88853 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
88854 rc = SQLITE_READONLY;
88855 goto abort_due_to_error;
88856 }
88857 pBt = db->aDb[pOp->p1].pBt;
88858
88859 if( pBt ){
88860 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
88861 testcase( rc==SQLITE_BUSY_SNAPSHOT );
88862 testcase( rc==SQLITE_BUSY_RECOVERY );
88863 if( rc!=SQLITE_OK ){
88864 if( (rc&0xff)==SQLITE_BUSY ){
88865 p->pc = (int)(pOp - aOp);
88866 p->rc = rc;
88867 goto vdbe_return;
88868 }
88869 goto abort_due_to_error;
88870 }
88871
88872 if( p->usesStmtJournal
88873 && pOp->p2
88874 && (db->autoCommit==0 || db->nVdbeRead>1)
88875 ){
88876 assert( sqlite3BtreeIsInTrans(pBt) );
88877 if( p->iStatement==0 ){
88878 assert( db->nStatement>=0 && db->nSavepoint>=0 );
88879 db->nStatement++;
88880 p->iStatement = db->nSavepoint + db->nStatement;
88881 }
88882
88883 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
88884 if( rc==SQLITE_OK ){
88885 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
88886 }
88887
88888 /* Store the current value of the database handles deferred constraint
88889 ** counter. If the statement transaction needs to be rolled back,
88890 ** the value of this counter needs to be restored too. */
88891 p->nStmtDefCons = db->nDeferredCons;
88892 p->nStmtDefImmCons = db->nDeferredImmCons;
88893 }
88894 }
88895 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
88896 if( pOp->p5
88897 && (iMeta!=pOp->p3
88898 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
88899 ){
88900 /*
88901 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
88902 ** version is checked to ensure that the schema has not changed since the
88903 ** SQL statement was prepared.
88904 */
88905 sqlite3DbFree(db, p->zErrMsg);
88906 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
88907 /* If the schema-cookie from the database file matches the cookie
88908 ** stored with the in-memory representation of the schema, do
88909 ** not reload the schema from the database file.
88910 **
88911 ** If virtual-tables are in use, this is not just an optimization.
88912 ** Often, v-tables store their data in other SQLite tables, which
88913 ** are queried from within xNext() and other v-table methods using
88914 ** prepared queries. If such a query is out-of-date, we do not want to
88915 ** discard the database schema, as the user code implementing the
88916 ** v-table would have to be ready for the sqlite3_vtab structure itself
88917 ** to be invalidated whenever sqlite3_step() is called from within
88918 ** a v-table method.
88919 */
88920 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
88921 sqlite3ResetOneSchema(db, pOp->p1);
88922 }
88923 p->expired = 1;
88924 rc = SQLITE_SCHEMA;
88925 }
88926 if( rc ) goto abort_due_to_error;
88927 break;
88928}
88929
88930/* Opcode: ReadCookie P1 P2 P3 * *
88931**
88932** Read cookie number P3 from database P1 and write it into register P2.
88933** P3==1 is the schema version. P3==2 is the database format.
88934** P3==3 is the recommended pager cache size, and so forth. P1==0 is
88935** the main database file and P1==1 is the database file used to store
88936** temporary tables.
88937**
88938** There must be a read-lock on the database (either a transaction
88939** must be started or there must be an open cursor) before
88940** executing this instruction.
88941*/
88942case OP_ReadCookie: { /* out2 */
88943 int iMeta;
88944 int iDb;
88945 int iCookie;
88946
88947 assert( p->bIsReader );
88948 iDb = pOp->p1;
88949 iCookie = pOp->p3;
88950 assert( pOp->p3<SQLITE_N_BTREE_META );
88951 assert( iDb>=0 && iDb<db->nDb );
88952 assert( db->aDb[iDb].pBt!=0 );
88953 assert( DbMaskTest(p->btreeMask, iDb) );
88954
88955 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
88956 pOut = out2Prerelease(p, pOp);
88957 pOut->u.i = iMeta;
88958 break;
88959}
88960
88961/* Opcode: SetCookie P1 P2 P3 * P5
88962**
88963** Write the integer value P3 into cookie number P2 of database P1.
88964** P2==1 is the schema version. P2==2 is the database format.
88965** P2==3 is the recommended pager cache
88966** size, and so forth. P1==0 is the main database file and P1==1 is the
88967** database file used to store temporary tables.
88968**
88969** A transaction must be started before executing this opcode.
88970**
88971** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
88972** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
88973** has P5 set to 1, so that the internal schema version will be different
88974** from the database schema version, resulting in a schema reset.
88975*/
88976case OP_SetCookie: {
88977 Db *pDb;
88978
88979 sqlite3VdbeIncrWriteCounter(p, 0);
88980 assert( pOp->p2<SQLITE_N_BTREE_META );
88981 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88982 assert( DbMaskTest(p->btreeMask, pOp->p1) );
88983 assert( p->readOnly==0 );
88984 pDb = &db->aDb[pOp->p1];
88985 assert( pDb->pBt!=0 );
88986 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
88987 /* See note about index shifting on OP_ReadCookie */
88988 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
88989 if( pOp->p2==BTREE_SCHEMA_VERSION ){
88990 /* When the schema cookie changes, record the new cookie internally */
88991 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
88992 db->mDbFlags |= DBFLAG_SchemaChange;
88993 }else if( pOp->p2==BTREE_FILE_FORMAT ){
88994 /* Record changes in the file format */
88995 pDb->pSchema->file_format = pOp->p3;
88996 }
88997 if( pOp->p1==1 ){
88998 /* Invalidate all prepared statements whenever the TEMP database
88999 ** schema is changed. Ticket #1644 */
89000 sqlite3ExpirePreparedStatements(db, 0);
89001 p->expired = 0;
89002 }
89003 if( rc ) goto abort_due_to_error;
89004 break;
89005}
89006
89007/* Opcode: OpenRead P1 P2 P3 P4 P5
89008** Synopsis: root=P2 iDb=P3
89009**
89010** Open a read-only cursor for the database table whose root page is
89011** P2 in a database file. The database file is determined by P3.
89012** P3==0 means the main database, P3==1 means the database used for
89013** temporary tables, and P3>1 means used the corresponding attached
89014** database. Give the new cursor an identifier of P1. The P1
89015** values need not be contiguous but all P1 values should be small integers.
89016** It is an error for P1 to be negative.
89017**
89018** Allowed P5 bits:
89019** <ul>
89020** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
89021** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
89022** of OP_SeekLE/OP_IdxLT)
89023** </ul>
89024**
89025** The P4 value may be either an integer (P4_INT32) or a pointer to
89026** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
89027** object, then table being opened must be an [index b-tree] where the
89028** KeyInfo object defines the content and collating
89029** sequence of that index b-tree. Otherwise, if P4 is an integer
89030** value, then the table being opened must be a [table b-tree] with a
89031** number of columns no less than the value of P4.
89032**
89033** See also: OpenWrite, ReopenIdx
89034*/
89035/* Opcode: ReopenIdx P1 P2 P3 P4 P5
89036** Synopsis: root=P2 iDb=P3
89037**
89038** The ReopenIdx opcode works like OP_OpenRead except that it first
89039** checks to see if the cursor on P1 is already open on the same
89040** b-tree and if it is this opcode becomes a no-op. In other words,
89041** if the cursor is already open, do not reopen it.
89042**
89043** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
89044** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
89045** be the same as every other ReopenIdx or OpenRead for the same cursor
89046** number.
89047**
89048** Allowed P5 bits:
89049** <ul>
89050** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
89051** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
89052** of OP_SeekLE/OP_IdxLT)
89053** </ul>
89054**
89055** See also: OP_OpenRead, OP_OpenWrite
89056*/
89057/* Opcode: OpenWrite P1 P2 P3 P4 P5
89058** Synopsis: root=P2 iDb=P3
89059**
89060** Open a read/write cursor named P1 on the table or index whose root
89061** page is P2 (or whose root page is held in register P2 if the
89062** OPFLAG_P2ISREG bit is set in P5 - see below).
89063**
89064** The P4 value may be either an integer (P4_INT32) or a pointer to
89065** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
89066** object, then table being opened must be an [index b-tree] where the
89067** KeyInfo object defines the content and collating
89068** sequence of that index b-tree. Otherwise, if P4 is an integer
89069** value, then the table being opened must be a [table b-tree] with a
89070** number of columns no less than the value of P4.
89071**
89072** Allowed P5 bits:
89073** <ul>
89074** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
89075** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
89076** of OP_SeekLE/OP_IdxLT)
89077** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
89078** and subsequently delete entries in an index btree. This is a
89079** hint to the storage engine that the storage engine is allowed to
89080** ignore. The hint is not used by the official SQLite b*tree storage
89081** engine, but is used by COMDB2.
89082** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
89083** as the root page, not the value of P2 itself.
89084** </ul>
89085**
89086** This instruction works like OpenRead except that it opens the cursor
89087** in read/write mode.
89088**
89089** See also: OP_OpenRead, OP_ReopenIdx
89090*/
89091case OP_ReopenIdx: {
89092 int nField;
89093 KeyInfo *pKeyInfo;
89094 u32 p2;
89095 int iDb;
89096 int wrFlag;
89097 Btree *pX;
89098 VdbeCursor *pCur;
89099 Db *pDb;
89100
89101 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
89102 assert( pOp->p4type==P4_KEYINFO );
89103 pCur = p->apCsr[pOp->p1];
89104 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
89105 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
89106 goto open_cursor_set_hints;
89107 }
89108 /* If the cursor is not currently open or is open on a different
89109 ** index, then fall through into OP_OpenRead to force a reopen */
89110case OP_OpenRead:
89111case OP_OpenWrite:
89112
89113 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
89114 assert( p->bIsReader );
89115 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
89116 || p->readOnly==0 );
89117
89118 if( p->expired==1 ){
89119 rc = SQLITE_ABORT_ROLLBACK;
89120 goto abort_due_to_error;
89121 }
89122
89123 nField = 0;
89124 pKeyInfo = 0;
89125 p2 = (u32)pOp->p2;
89126 iDb = pOp->p3;
89127 assert( iDb>=0 && iDb<db->nDb );
89128 assert( DbMaskTest(p->btreeMask, iDb) );
89129 pDb = &db->aDb[iDb];
89130 pX = pDb->pBt;
89131 assert( pX!=0 );
89132 if( pOp->opcode==OP_OpenWrite ){
89133 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
89134 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
89135 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
89136 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
89137 p->minWriteFileFormat = pDb->pSchema->file_format;
89138 }
89139 }else{
89140 wrFlag = 0;
89141 }
89142 if( pOp->p5 & OPFLAG_P2ISREG ){
89143 assert( p2>0 );
89144 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
89145 assert( pOp->opcode==OP_OpenWrite );
89146 pIn2 = &aMem[p2];
89147 assert( memIsValid(pIn2) );
89148 assert( (pIn2->flags & MEM_Int)!=0 );
89149 sqlite3VdbeMemIntegerify(pIn2);
89150 p2 = (int)pIn2->u.i;
89151 /* The p2 value always comes from a prior OP_CreateBtree opcode and
89152 ** that opcode will always set the p2 value to 2 or more or else fail.
89153 ** If there were a failure, the prepared statement would have halted
89154 ** before reaching this instruction. */
89155 assert( p2>=2 );
89156 }
89157 if( pOp->p4type==P4_KEYINFO ){
89158 pKeyInfo = pOp->p4.pKeyInfo;
89159 assert( pKeyInfo->enc==ENC(db) );
89160 assert( pKeyInfo->db==db );
89161 nField = pKeyInfo->nAllField;
89162 }else if( pOp->p4type==P4_INT32 ){
89163 nField = pOp->p4.i;
89164 }
89165 assert( pOp->p1>=0 );
89166 assert( nField>=0 );
89167 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
89168 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
89169 if( pCur==0 ) goto no_mem;
89170 pCur->nullRow = 1;
89171 pCur->isOrdered = 1;
89172 pCur->pgnoRoot = p2;
89173#ifdef SQLITE_DEBUG
89174 pCur->wrFlag = wrFlag;
89175#endif
89176 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
89177 pCur->pKeyInfo = pKeyInfo;
89178 /* Set the VdbeCursor.isTable variable. Previous versions of
89179 ** SQLite used to check if the root-page flags were sane at this point
89180 ** and report database corruption if they were not, but this check has
89181 ** since moved into the btree layer. */
89182 pCur->isTable = pOp->p4type!=P4_KEYINFO;
89183
89184open_cursor_set_hints:
89185 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
89186 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
89187 testcase( pOp->p5 & OPFLAG_BULKCSR );
89188 testcase( pOp->p2 & OPFLAG_SEEKEQ );
89189 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
89190 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
89191 if( rc ) goto abort_due_to_error;
89192 break;
89193}
89194
89195/* Opcode: OpenDup P1 P2 * * *
89196**
89197** Open a new cursor P1 that points to the same ephemeral table as
89198** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
89199** opcode. Only ephemeral cursors may be duplicated.
89200**
89201** Duplicate ephemeral cursors are used for self-joins of materialized views.
89202*/
89203case OP_OpenDup: {
89204 VdbeCursor *pOrig; /* The original cursor to be duplicated */
89205 VdbeCursor *pCx; /* The new cursor */
89206
89207 pOrig = p->apCsr[pOp->p2];
89208 assert( pOrig );
89209 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
89210
89211 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
89212 if( pCx==0 ) goto no_mem;
89213 pCx->nullRow = 1;
89214 pCx->isEphemeral = 1;
89215 pCx->pKeyInfo = pOrig->pKeyInfo;
89216 pCx->isTable = pOrig->isTable;
89217 pCx->pgnoRoot = pOrig->pgnoRoot;
89218 pCx->isOrdered = pOrig->isOrdered;
89219 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89220 pCx->pKeyInfo, pCx->uc.pCursor);
89221 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
89222 ** opened for a database. Since there is already an open cursor when this
89223 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
89224 assert( rc==SQLITE_OK );
89225 break;
89226}
89227
89228
89229/* Opcode: OpenEphemeral P1 P2 * P4 P5
89230** Synopsis: nColumn=P2
89231**
89232** Open a new cursor P1 to a transient table.
89233** The cursor is always opened read/write even if
89234** the main database is read-only. The ephemeral
89235** table is deleted automatically when the cursor is closed.
89236**
89237** If the cursor P1 is already opened on an ephemeral table, the table
89238** is cleared (all content is erased).
89239**
89240** P2 is the number of columns in the ephemeral table.
89241** The cursor points to a BTree table if P4==0 and to a BTree index
89242** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
89243** that defines the format of keys in the index.
89244**
89245** The P5 parameter can be a mask of the BTREE_* flags defined
89246** in btree.h. These flags control aspects of the operation of
89247** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
89248** added automatically.
89249*/
89250/* Opcode: OpenAutoindex P1 P2 * P4 *
89251** Synopsis: nColumn=P2
89252**
89253** This opcode works the same as OP_OpenEphemeral. It has a
89254** different name to distinguish its use. Tables created using
89255** by this opcode will be used for automatically created transient
89256** indices in joins.
89257*/
89258case OP_OpenAutoindex:
89259case OP_OpenEphemeral: {
89260 VdbeCursor *pCx;
89261 KeyInfo *pKeyInfo;
89262
89263 static const int vfsFlags =
89264 SQLITE_OPEN_READWRITE |
89265 SQLITE_OPEN_CREATE |
89266 SQLITE_OPEN_EXCLUSIVE |
89267 SQLITE_OPEN_DELETEONCLOSE |
89268 SQLITE_OPEN_TRANSIENT_DB;
89269 assert( pOp->p1>=0 );
89270 assert( pOp->p2>=0 );
89271 pCx = p->apCsr[pOp->p1];
89272 if( pCx && pCx->pBtx ){
89273 /* If the ephermeral table is already open, erase all existing content
89274 ** so that the table is empty again, rather than creating a new table. */
89275 assert( pCx->isEphemeral );
89276 pCx->seqCount = 0;
89277 pCx->cacheStatus = CACHE_STALE;
89278 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
89279 }else{
89280 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
89281 if( pCx==0 ) goto no_mem;
89282 pCx->isEphemeral = 1;
89283 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
89284 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
89285 vfsFlags);
89286 if( rc==SQLITE_OK ){
89287 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
89288 }
89289 if( rc==SQLITE_OK ){
89290 /* If a transient index is required, create it by calling
89291 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
89292 ** opening it. If a transient table is required, just use the
89293 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
89294 */
89295 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
89296 assert( pOp->p4type==P4_KEYINFO );
89297 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot,
89298 BTREE_BLOBKEY | pOp->p5);
89299 if( rc==SQLITE_OK ){
89300 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
89301 assert( pKeyInfo->db==db );
89302 assert( pKeyInfo->enc==ENC(db) );
89303 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89304 pKeyInfo, pCx->uc.pCursor);
89305 }
89306 pCx->isTable = 0;
89307 }else{
89308 pCx->pgnoRoot = SCHEMA_ROOT;
89309 rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR,
89310 0, pCx->uc.pCursor);
89311 pCx->isTable = 1;
89312 }
89313 }
89314 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
89315 }
89316 if( rc ) goto abort_due_to_error;
89317 pCx->nullRow = 1;
89318 break;
89319}
89320
89321/* Opcode: SorterOpen P1 P2 P3 P4 *
89322**
89323** This opcode works like OP_OpenEphemeral except that it opens
89324** a transient index that is specifically designed to sort large
89325** tables using an external merge-sort algorithm.
89326**
89327** If argument P3 is non-zero, then it indicates that the sorter may
89328** assume that a stable sort considering the first P3 fields of each
89329** key is sufficient to produce the required results.
89330*/
89331case OP_SorterOpen: {
89332 VdbeCursor *pCx;
89333
89334 assert( pOp->p1>=0 );
89335 assert( pOp->p2>=0 );
89336 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
89337 if( pCx==0 ) goto no_mem;
89338 pCx->pKeyInfo = pOp->p4.pKeyInfo;
89339 assert( pCx->pKeyInfo->db==db );
89340 assert( pCx->pKeyInfo->enc==ENC(db) );
89341 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
89342 if( rc ) goto abort_due_to_error;
89343 break;
89344}
89345
89346/* Opcode: SequenceTest P1 P2 * * *
89347** Synopsis: if( cursor[P1].ctr++ ) pc = P2
89348**
89349** P1 is a sorter cursor. If the sequence counter is currently zero, jump
89350** to P2. Regardless of whether or not the jump is taken, increment the
89351** the sequence value.
89352*/
89353case OP_SequenceTest: {
89354 VdbeCursor *pC;
89355 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89356 pC = p->apCsr[pOp->p1];
89357 assert( isSorter(pC) );
89358 if( (pC->seqCount++)==0 ){
89359 goto jump_to_p2;
89360 }
89361 break;
89362}
89363
89364/* Opcode: OpenPseudo P1 P2 P3 * *
89365** Synopsis: P3 columns in r[P2]
89366**
89367** Open a new cursor that points to a fake table that contains a single
89368** row of data. The content of that one row is the content of memory
89369** register P2. In other words, cursor P1 becomes an alias for the
89370** MEM_Blob content contained in register P2.
89371**
89372** A pseudo-table created by this opcode is used to hold a single
89373** row output from the sorter so that the row can be decomposed into
89374** individual columns using the OP_Column opcode. The OP_Column opcode
89375** is the only cursor opcode that works with a pseudo-table.
89376**
89377** P3 is the number of fields in the records that will be stored by
89378** the pseudo-table.
89379*/
89380case OP_OpenPseudo: {
89381 VdbeCursor *pCx;
89382
89383 assert( pOp->p1>=0 );
89384 assert( pOp->p3>=0 );
89385 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
89386 if( pCx==0 ) goto no_mem;
89387 pCx->nullRow = 1;
89388 pCx->seekResult = pOp->p2;
89389 pCx->isTable = 1;
89390 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
89391 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
89392 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
89393 ** which is a performance optimization */
89394 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
89395 assert( pOp->p5==0 );
89396 break;
89397}
89398
89399/* Opcode: Close P1 * * * *
89400**
89401** Close a cursor previously opened as P1. If P1 is not
89402** currently open, this instruction is a no-op.
89403*/
89404case OP_Close: {
89405 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89406 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
89407 p->apCsr[pOp->p1] = 0;
89408 break;
89409}
89410
89411#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
89412/* Opcode: ColumnsUsed P1 * * P4 *
89413**
89414** This opcode (which only exists if SQLite was compiled with
89415** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
89416** table or index for cursor P1 are used. P4 is a 64-bit integer
89417** (P4_INT64) in which the first 63 bits are one for each of the
89418** first 63 columns of the table or index that are actually used
89419** by the cursor. The high-order bit is set if any column after
89420** the 64th is used.
89421*/
89422case OP_ColumnsUsed: {
89423 VdbeCursor *pC;
89424 pC = p->apCsr[pOp->p1];
89425 assert( pC->eCurType==CURTYPE_BTREE );
89426 pC->maskUsed = *(u64*)pOp->p4.pI64;
89427 break;
89428}
89429#endif
89430
89431/* Opcode: SeekGE P1 P2 P3 P4 *
89432** Synopsis: key=r[P3@P4]
89433**
89434** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89435** use the value in register P3 as the key. If cursor P1 refers
89436** to an SQL index, then P3 is the first in an array of P4 registers
89437** that are used as an unpacked index key.
89438**
89439** Reposition cursor P1 so that it points to the smallest entry that
89440** is greater than or equal to the key value. If there are no records
89441** greater than or equal to the key and P2 is not zero, then jump to P2.
89442**
89443** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
89444** opcode will either land on a record that exactly matches the key, or
89445** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
89446** this opcode must be followed by an IdxLE opcode with the same arguments.
89447** The IdxGT opcode will be skipped if this opcode succeeds, but the
89448** IdxGT opcode will be used on subsequent loop iterations. The
89449** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
89450** is an equality search.
89451**
89452** This opcode leaves the cursor configured to move in forward order,
89453** from the beginning toward the end. In other words, the cursor is
89454** configured to use Next, not Prev.
89455**
89456** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
89457*/
89458/* Opcode: SeekGT P1 P2 P3 P4 *
89459** Synopsis: key=r[P3@P4]
89460**
89461** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89462** use the value in register P3 as a key. If cursor P1 refers
89463** to an SQL index, then P3 is the first in an array of P4 registers
89464** that are used as an unpacked index key.
89465**
89466** Reposition cursor P1 so that it points to the smallest entry that
89467** is greater than the key value. If there are no records greater than
89468** the key and P2 is not zero, then jump to P2.
89469**
89470** This opcode leaves the cursor configured to move in forward order,
89471** from the beginning toward the end. In other words, the cursor is
89472** configured to use Next, not Prev.
89473**
89474** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
89475*/
89476/* Opcode: SeekLT P1 P2 P3 P4 *
89477** Synopsis: key=r[P3@P4]
89478**
89479** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89480** use the value in register P3 as a key. If cursor P1 refers
89481** to an SQL index, then P3 is the first in an array of P4 registers
89482** that are used as an unpacked index key.
89483**
89484** Reposition cursor P1 so that it points to the largest entry that
89485** is less than the key value. If there are no records less than
89486** the key and P2 is not zero, then jump to P2.
89487**
89488** This opcode leaves the cursor configured to move in reverse order,
89489** from the end toward the beginning. In other words, the cursor is
89490** configured to use Prev, not Next.
89491**
89492** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
89493*/
89494/* Opcode: SeekLE P1 P2 P3 P4 *
89495** Synopsis: key=r[P3@P4]
89496**
89497** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
89498** use the value in register P3 as a key. If cursor P1 refers
89499** to an SQL index, then P3 is the first in an array of P4 registers
89500** that are used as an unpacked index key.
89501**
89502** Reposition cursor P1 so that it points to the largest entry that
89503** is less than or equal to the key value. If there are no records
89504** less than or equal to the key and P2 is not zero, then jump to P2.
89505**
89506** This opcode leaves the cursor configured to move in reverse order,
89507** from the end toward the beginning. In other words, the cursor is
89508** configured to use Prev, not Next.
89509**
89510** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
89511** opcode will either land on a record that exactly matches the key, or
89512** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
89513** this opcode must be followed by an IdxLE opcode with the same arguments.
89514** The IdxGE opcode will be skipped if this opcode succeeds, but the
89515** IdxGE opcode will be used on subsequent loop iterations. The
89516** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
89517** is an equality search.
89518**
89519** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
89520*/
89521case OP_SeekLT: /* jump, in3, group */
89522case OP_SeekLE: /* jump, in3, group */
89523case OP_SeekGE: /* jump, in3, group */
89524case OP_SeekGT: { /* jump, in3, group */
89525 int res; /* Comparison result */
89526 int oc; /* Opcode */
89527 VdbeCursor *pC; /* The cursor to seek */
89528 UnpackedRecord r; /* The key to seek for */
89529 int nField; /* Number of columns or fields in the key */
89530 i64 iKey; /* The rowid we are to seek to */
89531 int eqOnly; /* Only interested in == results */
89532
89533 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89534 assert( pOp->p2!=0 );
89535 pC = p->apCsr[pOp->p1];
89536 assert( pC!=0 );
89537 assert( pC->eCurType==CURTYPE_BTREE );
89538 assert( OP_SeekLE == OP_SeekLT+1 );
89539 assert( OP_SeekGE == OP_SeekLT+2 );
89540 assert( OP_SeekGT == OP_SeekLT+3 );
89541 assert( pC->isOrdered );
89542 assert( pC->uc.pCursor!=0 );
89543 oc = pOp->opcode;
89544 eqOnly = 0;
89545 pC->nullRow = 0;
89546#ifdef SQLITE_DEBUG
89547 pC->seekOp = pOp->opcode;
89548#endif
89549
89550 pC->deferredMoveto = 0;
89551 pC->cacheStatus = CACHE_STALE;
89552 if( pC->isTable ){
89553 u16 flags3, newType;
89554 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
89555 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
89556 || CORRUPT_DB );
89557
89558 /* The input value in P3 might be of any type: integer, real, string,
89559 ** blob, or NULL. But it needs to be an integer before we can do
89560 ** the seek, so convert it. */
89561 pIn3 = &aMem[pOp->p3];
89562 flags3 = pIn3->flags;
89563 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
89564 applyNumericAffinity(pIn3, 0);
89565 }
89566 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
89567 newType = pIn3->flags; /* Record the type after applying numeric affinity */
89568 pIn3->flags = flags3; /* But convert the type back to its original */
89569
89570 /* If the P3 value could not be converted into an integer without
89571 ** loss of information, then special processing is required... */
89572 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
89573 if( (newType & MEM_Real)==0 ){
89574 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
89575 VdbeBranchTaken(1,2);
89576 goto jump_to_p2;
89577 }else{
89578 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
89579 if( rc!=SQLITE_OK ) goto abort_due_to_error;
89580 goto seek_not_found;
89581 }
89582 }else
89583
89584 /* If the approximation iKey is larger than the actual real search
89585 ** term, substitute >= for > and < for <=. e.g. if the search term
89586 ** is 4.9 and the integer approximation 5:
89587 **
89588 ** (x > 4.9) -> (x >= 5)
89589 ** (x <= 4.9) -> (x < 5)
89590 */
89591 if( pIn3->u.r<(double)iKey ){
89592 assert( OP_SeekGE==(OP_SeekGT-1) );
89593 assert( OP_SeekLT==(OP_SeekLE-1) );
89594 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
89595 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
89596 }
89597
89598 /* If the approximation iKey is smaller than the actual real search
89599 ** term, substitute <= for < and > for >=. */
89600 else if( pIn3->u.r>(double)iKey ){
89601 assert( OP_SeekLE==(OP_SeekLT+1) );
89602 assert( OP_SeekGT==(OP_SeekGE+1) );
89603 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
89604 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
89605 }
89606 }
89607 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
89608 pC->movetoTarget = iKey; /* Used by OP_Delete */
89609 if( rc!=SQLITE_OK ){
89610 goto abort_due_to_error;
89611 }
89612 }else{
89613 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
89614 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
89615 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
89616 ** with the same key.
89617 */
89618 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
89619 eqOnly = 1;
89620 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
89621 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
89622 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
89623 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
89624 assert( pOp[1].p1==pOp[0].p1 );
89625 assert( pOp[1].p2==pOp[0].p2 );
89626 assert( pOp[1].p3==pOp[0].p3 );
89627 assert( pOp[1].p4.i==pOp[0].p4.i );
89628 }
89629
89630 nField = pOp->p4.i;
89631 assert( pOp->p4type==P4_INT32 );
89632 assert( nField>0 );
89633 r.pKeyInfo = pC->pKeyInfo;
89634 r.nField = (u16)nField;
89635
89636 /* The next line of code computes as follows, only faster:
89637 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
89638 ** r.default_rc = -1;
89639 ** }else{
89640 ** r.default_rc = +1;
89641 ** }
89642 */
89643 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
89644 assert( oc!=OP_SeekGT || r.default_rc==-1 );
89645 assert( oc!=OP_SeekLE || r.default_rc==-1 );
89646 assert( oc!=OP_SeekGE || r.default_rc==+1 );
89647 assert( oc!=OP_SeekLT || r.default_rc==+1 );
89648
89649 r.aMem = &aMem[pOp->p3];
89650#ifdef SQLITE_DEBUG
89651 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
89652#endif
89653 r.eqSeen = 0;
89654 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
89655 if( rc!=SQLITE_OK ){
89656 goto abort_due_to_error;
89657 }
89658 if( eqOnly && r.eqSeen==0 ){
89659 assert( res!=0 );
89660 goto seek_not_found;
89661 }
89662 }
89663#ifdef SQLITE_TEST
89664 sqlite3_search_count++;
89665#endif
89666 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
89667 if( res<0 || (res==0 && oc==OP_SeekGT) ){
89668 res = 0;
89669 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
89670 if( rc!=SQLITE_OK ){
89671 if( rc==SQLITE_DONE ){
89672 rc = SQLITE_OK;
89673 res = 1;
89674 }else{
89675 goto abort_due_to_error;
89676 }
89677 }
89678 }else{
89679 res = 0;
89680 }
89681 }else{
89682 assert( oc==OP_SeekLT || oc==OP_SeekLE );
89683 if( res>0 || (res==0 && oc==OP_SeekLT) ){
89684 res = 0;
89685 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
89686 if( rc!=SQLITE_OK ){
89687 if( rc==SQLITE_DONE ){
89688 rc = SQLITE_OK;
89689 res = 1;
89690 }else{
89691 goto abort_due_to_error;
89692 }
89693 }
89694 }else{
89695 /* res might be negative because the table is empty. Check to
89696 ** see if this is the case.
89697 */
89698 res = sqlite3BtreeEof(pC->uc.pCursor);
89699 }
89700 }
89701seek_not_found:
89702 assert( pOp->p2>0 );
89703 VdbeBranchTaken(res!=0,2);
89704 if( res ){
89705 goto jump_to_p2;
89706 }else if( eqOnly ){
89707 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
89708 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
89709 }
89710 break;
89711}
89712
89713/* Opcode: SeekHit P1 P2 * * *
89714** Synopsis: seekHit=P2
89715**
89716** Set the seekHit flag on cursor P1 to the value in P2.
89717** The seekHit flag is used by the IfNoHope opcode.
89718**
89719** P1 must be a valid b-tree cursor. P2 must be a boolean value,
89720** either 0 or 1.
89721*/
89722case OP_SeekHit: {
89723 VdbeCursor *pC;
89724 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89725 pC = p->apCsr[pOp->p1];
89726 assert( pC!=0 );
89727 assert( pOp->p2==0 || pOp->p2==1 );
89728 pC->seekHit = pOp->p2 & 1;
89729 break;
89730}
89731
89732/* Opcode: IfNotOpen P1 P2 * * *
89733** Synopsis: if( !csr[P1] ) goto P2
89734**
89735** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
89736*/
89737case OP_IfNotOpen: { /* jump */
89738 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89739 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
89740 if( !p->apCsr[pOp->p1] ){
89741 goto jump_to_p2_and_check_for_interrupt;
89742 }
89743 break;
89744}
89745
89746/* Opcode: Found P1 P2 P3 P4 *
89747** Synopsis: key=r[P3@P4]
89748**
89749** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
89750** P4>0 then register P3 is the first of P4 registers that form an unpacked
89751** record.
89752**
89753** Cursor P1 is on an index btree. If the record identified by P3 and P4
89754** is a prefix of any entry in P1 then a jump is made to P2 and
89755** P1 is left pointing at the matching entry.
89756**
89757** This operation leaves the cursor in a state where it can be
89758** advanced in the forward direction. The Next instruction will work,
89759** but not the Prev instruction.
89760**
89761** See also: NotFound, NoConflict, NotExists. SeekGe
89762*/
89763/* Opcode: NotFound P1 P2 P3 P4 *
89764** Synopsis: key=r[P3@P4]
89765**
89766** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
89767** P4>0 then register P3 is the first of P4 registers that form an unpacked
89768** record.
89769**
89770** Cursor P1 is on an index btree. If the record identified by P3 and P4
89771** is not the prefix of any entry in P1 then a jump is made to P2. If P1
89772** does contain an entry whose prefix matches the P3/P4 record then control
89773** falls through to the next instruction and P1 is left pointing at the
89774** matching entry.
89775**
89776** This operation leaves the cursor in a state where it cannot be
89777** advanced in either direction. In other words, the Next and Prev
89778** opcodes do not work after this operation.
89779**
89780** See also: Found, NotExists, NoConflict, IfNoHope
89781*/
89782/* Opcode: IfNoHope P1 P2 P3 P4 *
89783** Synopsis: key=r[P3@P4]
89784**
89785** Register P3 is the first of P4 registers that form an unpacked
89786** record.
89787**
89788** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
89789** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
89790** check to see if there is any entry in P1 that matches the
89791** prefix identified by P3 and P4. If no entry matches the prefix,
89792** jump to P2. Otherwise fall through.
89793**
89794** This opcode behaves like OP_NotFound if the seekHit
89795** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
89796**
89797** This opcode is used in IN clause processing for a multi-column key.
89798** If an IN clause is attached to an element of the key other than the
89799** left-most element, and if there are no matches on the most recent
89800** seek over the whole key, then it might be that one of the key element
89801** to the left is prohibiting a match, and hence there is "no hope" of
89802** any match regardless of how many IN clause elements are checked.
89803** In such a case, we abandon the IN clause search early, using this
89804** opcode. The opcode name comes from the fact that the
89805** jump is taken if there is "no hope" of achieving a match.
89806**
89807** See also: NotFound, SeekHit
89808*/
89809/* Opcode: NoConflict P1 P2 P3 P4 *
89810** Synopsis: key=r[P3@P4]
89811**
89812** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
89813** P4>0 then register P3 is the first of P4 registers that form an unpacked
89814** record.
89815**
89816** Cursor P1 is on an index btree. If the record identified by P3 and P4
89817** contains any NULL value, jump immediately to P2. If all terms of the
89818** record are not-NULL then a check is done to determine if any row in the
89819** P1 index btree has a matching key prefix. If there are no matches, jump
89820** immediately to P2. If there is a match, fall through and leave the P1
89821** cursor pointing to the matching row.
89822**
89823** This opcode is similar to OP_NotFound with the exceptions that the
89824** branch is always taken if any part of the search key input is NULL.
89825**
89826** This operation leaves the cursor in a state where it cannot be
89827** advanced in either direction. In other words, the Next and Prev
89828** opcodes do not work after this operation.
89829**
89830** See also: NotFound, Found, NotExists
89831*/
89832case OP_IfNoHope: { /* jump, in3 */
89833 VdbeCursor *pC;
89834 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89835 pC = p->apCsr[pOp->p1];
89836 assert( pC!=0 );
89837 if( pC->seekHit ) break;
89838 /* Fall through into OP_NotFound */
89839 /* no break */ deliberate_fall_through
89840}
89841case OP_NoConflict: /* jump, in3 */
89842case OP_NotFound: /* jump, in3 */
89843case OP_Found: { /* jump, in3 */
89844 int alreadyExists;
89845 int takeJump;
89846 int ii;
89847 VdbeCursor *pC;
89848 int res;
89849 UnpackedRecord *pFree;
89850 UnpackedRecord *pIdxKey;
89851 UnpackedRecord r;
89852
89853#ifdef SQLITE_TEST
89854 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
89855#endif
89856
89857 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89858 assert( pOp->p4type==P4_INT32 );
89859 pC = p->apCsr[pOp->p1];
89860 assert( pC!=0 );
89861#ifdef SQLITE_DEBUG
89862 pC->seekOp = pOp->opcode;
89863#endif
89864 pIn3 = &aMem[pOp->p3];
89865 assert( pC->eCurType==CURTYPE_BTREE );
89866 assert( pC->uc.pCursor!=0 );
89867 assert( pC->isTable==0 );
89868 if( pOp->p4.i>0 ){
89869 r.pKeyInfo = pC->pKeyInfo;
89870 r.nField = (u16)pOp->p4.i;
89871 r.aMem = pIn3;
89872#ifdef SQLITE_DEBUG
89873 for(ii=0; ii<r.nField; ii++){
89874 assert( memIsValid(&r.aMem[ii]) );
89875 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
89876 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
89877 }
89878#endif
89879 pIdxKey = &r;
89880 pFree = 0;
89881 }else{
89882 assert( pIn3->flags & MEM_Blob );
89883 rc = ExpandBlob(pIn3);
89884 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
89885 if( rc ) goto no_mem;
89886 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
89887 if( pIdxKey==0 ) goto no_mem;
89888 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
89889 }
89890 pIdxKey->default_rc = 0;
89891 takeJump = 0;
89892 if( pOp->opcode==OP_NoConflict ){
89893 /* For the OP_NoConflict opcode, take the jump if any of the
89894 ** input fields are NULL, since any key with a NULL will not
89895 ** conflict */
89896 for(ii=0; ii<pIdxKey->nField; ii++){
89897 if( pIdxKey->aMem[ii].flags & MEM_Null ){
89898 takeJump = 1;
89899 break;
89900 }
89901 }
89902 }
89903 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
89904 if( pFree ) sqlite3DbFreeNN(db, pFree);
89905 if( rc!=SQLITE_OK ){
89906 goto abort_due_to_error;
89907 }
89908 pC->seekResult = res;
89909 alreadyExists = (res==0);
89910 pC->nullRow = 1-alreadyExists;
89911 pC->deferredMoveto = 0;
89912 pC->cacheStatus = CACHE_STALE;
89913 if( pOp->opcode==OP_Found ){
89914 VdbeBranchTaken(alreadyExists!=0,2);
89915 if( alreadyExists ) goto jump_to_p2;
89916 }else{
89917 VdbeBranchTaken(takeJump||alreadyExists==0,2);
89918 if( takeJump || !alreadyExists ) goto jump_to_p2;
89919 }
89920 break;
89921}
89922
89923/* Opcode: SeekRowid P1 P2 P3 * *
89924** Synopsis: intkey=r[P3]
89925**
89926** P1 is the index of a cursor open on an SQL table btree (with integer
89927** keys). If register P3 does not contain an integer or if P1 does not
89928** contain a record with rowid P3 then jump immediately to P2.
89929** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
89930** a record with rowid P3 then
89931** leave the cursor pointing at that record and fall through to the next
89932** instruction.
89933**
89934** The OP_NotExists opcode performs the same operation, but with OP_NotExists
89935** the P3 register must be guaranteed to contain an integer value. With this
89936** opcode, register P3 might not contain an integer.
89937**
89938** The OP_NotFound opcode performs the same operation on index btrees
89939** (with arbitrary multi-value keys).
89940**
89941** This opcode leaves the cursor in a state where it cannot be advanced
89942** in either direction. In other words, the Next and Prev opcodes will
89943** not work following this opcode.
89944**
89945** See also: Found, NotFound, NoConflict, SeekRowid
89946*/
89947/* Opcode: NotExists P1 P2 P3 * *
89948** Synopsis: intkey=r[P3]
89949**
89950** P1 is the index of a cursor open on an SQL table btree (with integer
89951** keys). P3 is an integer rowid. If P1 does not contain a record with
89952** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
89953** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
89954** leave the cursor pointing at that record and fall through to the next
89955** instruction.
89956**
89957** The OP_SeekRowid opcode performs the same operation but also allows the
89958** P3 register to contain a non-integer value, in which case the jump is
89959** always taken. This opcode requires that P3 always contain an integer.
89960**
89961** The OP_NotFound opcode performs the same operation on index btrees
89962** (with arbitrary multi-value keys).
89963**
89964** This opcode leaves the cursor in a state where it cannot be advanced
89965** in either direction. In other words, the Next and Prev opcodes will
89966** not work following this opcode.
89967**
89968** See also: Found, NotFound, NoConflict, SeekRowid
89969*/
89970case OP_SeekRowid: { /* jump, in3 */
89971 VdbeCursor *pC;
89972 BtCursor *pCrsr;
89973 int res;
89974 u64 iKey;
89975
89976 pIn3 = &aMem[pOp->p3];
89977 testcase( pIn3->flags & MEM_Int );
89978 testcase( pIn3->flags & MEM_IntReal );
89979 testcase( pIn3->flags & MEM_Real );
89980 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
89981 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
89982 /* If pIn3->u.i does not contain an integer, compute iKey as the
89983 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
89984 ** into an integer without loss of information. Take care to avoid
89985 ** changing the datatype of pIn3, however, as it is used by other
89986 ** parts of the prepared statement. */
89987 Mem x = pIn3[0];
89988 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
89989 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
89990 iKey = x.u.i;
89991 goto notExistsWithKey;
89992 }
89993 /* Fall through into OP_NotExists */
89994 /* no break */ deliberate_fall_through
89995case OP_NotExists: /* jump, in3 */
89996 pIn3 = &aMem[pOp->p3];
89997 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
89998 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
89999 iKey = pIn3->u.i;
90000notExistsWithKey:
90001 pC = p->apCsr[pOp->p1];
90002 assert( pC!=0 );
90003#ifdef SQLITE_DEBUG
90004 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
90005#endif
90006 assert( pC->isTable );
90007 assert( pC->eCurType==CURTYPE_BTREE );
90008 pCrsr = pC->uc.pCursor;
90009 assert( pCrsr!=0 );
90010 res = 0;
90011 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
90012 assert( rc==SQLITE_OK || res==0 );
90013 pC->movetoTarget = iKey; /* Used by OP_Delete */
90014 pC->nullRow = 0;
90015 pC->cacheStatus = CACHE_STALE;
90016 pC->deferredMoveto = 0;
90017 VdbeBranchTaken(res!=0,2);
90018 pC->seekResult = res;
90019 if( res!=0 ){
90020 assert( rc==SQLITE_OK );
90021 if( pOp->p2==0 ){
90022 rc = SQLITE_CORRUPT_BKPT;
90023 }else{
90024 goto jump_to_p2;
90025 }
90026 }
90027 if( rc ) goto abort_due_to_error;
90028 break;
90029}
90030
90031/* Opcode: Sequence P1 P2 * * *
90032** Synopsis: r[P2]=cursor[P1].ctr++
90033**
90034** Find the next available sequence number for cursor P1.
90035** Write the sequence number into register P2.
90036** The sequence number on the cursor is incremented after this
90037** instruction.
90038*/
90039case OP_Sequence: { /* out2 */
90040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90041 assert( p->apCsr[pOp->p1]!=0 );
90042 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
90043 pOut = out2Prerelease(p, pOp);
90044 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
90045 break;
90046}
90047
90048
90049/* Opcode: NewRowid P1 P2 P3 * *
90050** Synopsis: r[P2]=rowid
90051**
90052** Get a new integer record number (a.k.a "rowid") used as the key to a table.
90053** The record number is not previously used as a key in the database
90054** table that cursor P1 points to. The new record number is written
90055** written to register P2.
90056**
90057** If P3>0 then P3 is a register in the root frame of this VDBE that holds
90058** the largest previously generated record number. No new record numbers are
90059** allowed to be less than this value. When this value reaches its maximum,
90060** an SQLITE_FULL error is generated. The P3 register is updated with the '
90061** generated record number. This P3 mechanism is used to help implement the
90062** AUTOINCREMENT feature.
90063*/
90064case OP_NewRowid: { /* out2 */
90065 i64 v; /* The new rowid */
90066 VdbeCursor *pC; /* Cursor of table to get the new rowid */
90067 int res; /* Result of an sqlite3BtreeLast() */
90068 int cnt; /* Counter to limit the number of searches */
90069 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
90070 VdbeFrame *pFrame; /* Root frame of VDBE */
90071
90072 v = 0;
90073 res = 0;
90074 pOut = out2Prerelease(p, pOp);
90075 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90076 pC = p->apCsr[pOp->p1];
90077 assert( pC!=0 );
90078 assert( pC->isTable );
90079 assert( pC->eCurType==CURTYPE_BTREE );
90080 assert( pC->uc.pCursor!=0 );
90081 {
90082 /* The next rowid or record number (different terms for the same
90083 ** thing) is obtained in a two-step algorithm.
90084 **
90085 ** First we attempt to find the largest existing rowid and add one
90086 ** to that. But if the largest existing rowid is already the maximum
90087 ** positive integer, we have to fall through to the second
90088 ** probabilistic algorithm
90089 **
90090 ** The second algorithm is to select a rowid at random and see if
90091 ** it already exists in the table. If it does not exist, we have
90092 ** succeeded. If the random rowid does exist, we select a new one
90093 ** and try again, up to 100 times.
90094 */
90095 assert( pC->isTable );
90096
90097#ifdef SQLITE_32BIT_ROWID
90098# define MAX_ROWID 0x7fffffff
90099#else
90100 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
90101 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
90102 ** to provide the constant while making all compilers happy.
90103 */
90104# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
90105#endif
90106
90107 if( !pC->useRandomRowid ){
90108 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
90109 if( rc!=SQLITE_OK ){
90110 goto abort_due_to_error;
90111 }
90112 if( res ){
90113 v = 1; /* IMP: R-61914-48074 */
90114 }else{
90115 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
90116 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90117 if( v>=MAX_ROWID ){
90118 pC->useRandomRowid = 1;
90119 }else{
90120 v++; /* IMP: R-29538-34987 */
90121 }
90122 }
90123 }
90124
90125#ifndef SQLITE_OMIT_AUTOINCREMENT
90126 if( pOp->p3 ){
90127 /* Assert that P3 is a valid memory cell. */
90128 assert( pOp->p3>0 );
90129 if( p->pFrame ){
90130 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
90131 /* Assert that P3 is a valid memory cell. */
90132 assert( pOp->p3<=pFrame->nMem );
90133 pMem = &pFrame->aMem[pOp->p3];
90134 }else{
90135 /* Assert that P3 is a valid memory cell. */
90136 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
90137 pMem = &aMem[pOp->p3];
90138 memAboutToChange(p, pMem);
90139 }
90140 assert( memIsValid(pMem) );
90141
90142 REGISTER_TRACE(pOp->p3, pMem);
90143 sqlite3VdbeMemIntegerify(pMem);
90144 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
90145 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
90146 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
90147 goto abort_due_to_error;
90148 }
90149 if( v<pMem->u.i+1 ){
90150 v = pMem->u.i + 1;
90151 }
90152 pMem->u.i = v;
90153 }
90154#endif
90155 if( pC->useRandomRowid ){
90156 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
90157 ** largest possible integer (9223372036854775807) then the database
90158 ** engine starts picking positive candidate ROWIDs at random until
90159 ** it finds one that is not previously used. */
90160 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
90161 ** an AUTOINCREMENT table. */
90162 cnt = 0;
90163 do{
90164 sqlite3_randomness(sizeof(v), &v);
90165 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
90166 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
90167 0, &res))==SQLITE_OK)
90168 && (res==0)
90169 && (++cnt<100));
90170 if( rc ) goto abort_due_to_error;
90171 if( res==0 ){
90172 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
90173 goto abort_due_to_error;
90174 }
90175 assert( v>0 ); /* EV: R-40812-03570 */
90176 }
90177 pC->deferredMoveto = 0;
90178 pC->cacheStatus = CACHE_STALE;
90179 }
90180 pOut->u.i = v;
90181 break;
90182}
90183
90184/* Opcode: Insert P1 P2 P3 P4 P5
90185** Synopsis: intkey=r[P3] data=r[P2]
90186**
90187** Write an entry into the table of cursor P1. A new entry is
90188** created if it doesn't already exist or the data for an existing
90189** entry is overwritten. The data is the value MEM_Blob stored in register
90190** number P2. The key is stored in register P3. The key must
90191** be a MEM_Int.
90192**
90193** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
90194** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
90195** then rowid is stored for subsequent return by the
90196** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
90197**
90198** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
90199** run faster by avoiding an unnecessary seek on cursor P1. However,
90200** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
90201** seeks on the cursor or if the most recent seek used a key equal to P3.
90202**
90203** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
90204** UPDATE operation. Otherwise (if the flag is clear) then this opcode
90205** is part of an INSERT operation. The difference is only important to
90206** the update hook.
90207**
90208** Parameter P4 may point to a Table structure, or may be NULL. If it is
90209** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
90210** following a successful insert.
90211**
90212** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
90213** allocated, then ownership of P2 is transferred to the pseudo-cursor
90214** and register P2 becomes ephemeral. If the cursor is changed, the
90215** value of register P2 will then change. Make sure this does not
90216** cause any problems.)
90217**
90218** This instruction only works on tables. The equivalent instruction
90219** for indices is OP_IdxInsert.
90220*/
90221case OP_Insert: {
90222 Mem *pData; /* MEM cell holding data for the record to be inserted */
90223 Mem *pKey; /* MEM cell holding key for the record */
90224 VdbeCursor *pC; /* Cursor to table into which insert is written */
90225 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
90226 const char *zDb; /* database name - used by the update hook */
90227 Table *pTab; /* Table structure - used by update and pre-update hooks */
90228 BtreePayload x; /* Payload to be inserted */
90229
90230 pData = &aMem[pOp->p2];
90231 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90232 assert( memIsValid(pData) );
90233 pC = p->apCsr[pOp->p1];
90234 assert( pC!=0 );
90235 assert( pC->eCurType==CURTYPE_BTREE );
90236 assert( pC->deferredMoveto==0 );
90237 assert( pC->uc.pCursor!=0 );
90238 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
90239 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
90240 REGISTER_TRACE(pOp->p2, pData);
90241 sqlite3VdbeIncrWriteCounter(p, pC);
90242
90243 pKey = &aMem[pOp->p3];
90244 assert( pKey->flags & MEM_Int );
90245 assert( memIsValid(pKey) );
90246 REGISTER_TRACE(pOp->p3, pKey);
90247 x.nKey = pKey->u.i;
90248
90249 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
90250 assert( pC->iDb>=0 );
90251 zDb = db->aDb[pC->iDb].zDbSName;
90252 pTab = pOp->p4.pTab;
90253 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
90254 }else{
90255 pTab = 0;
90256 zDb = 0; /* Not needed. Silence a compiler warning. */
90257 }
90258
90259#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90260 /* Invoke the pre-update hook, if any */
90261 if( pTab ){
90262 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
90263 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
90264 }
90265 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
90266 /* Prevent post-update hook from running in cases when it should not */
90267 pTab = 0;
90268 }
90269 }
90270 if( pOp->p5 & OPFLAG_ISNOOP ) break;
90271#endif
90272
90273 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
90274 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
90275 assert( pData->flags & (MEM_Blob|MEM_Str) );
90276 x.pData = pData->z;
90277 x.nData = pData->n;
90278 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
90279 if( pData->flags & MEM_Zero ){
90280 x.nZero = pData->u.nZero;
90281 }else{
90282 x.nZero = 0;
90283 }
90284 x.pKey = 0;
90285 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
90286 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
90287 );
90288 pC->deferredMoveto = 0;
90289 pC->cacheStatus = CACHE_STALE;
90290
90291 /* Invoke the update-hook if required. */
90292 if( rc ) goto abort_due_to_error;
90293 if( pTab ){
90294 assert( db->xUpdateCallback!=0 );
90295 assert( pTab->aCol!=0 );
90296 db->xUpdateCallback(db->pUpdateArg,
90297 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
90298 zDb, pTab->zName, x.nKey);
90299 }
90300 break;
90301}
90302
90303/* Opcode: Delete P1 P2 P3 P4 P5
90304**
90305** Delete the record at which the P1 cursor is currently pointing.
90306**
90307** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
90308** the cursor will be left pointing at either the next or the previous
90309** record in the table. If it is left pointing at the next record, then
90310** the next Next instruction will be a no-op. As a result, in this case
90311** it is ok to delete a record from within a Next loop. If
90312** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
90313** left in an undefined state.
90314**
90315** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
90316** delete one of several associated with deleting a table row and all its
90317** associated index entries. Exactly one of those deletes is the "primary"
90318** delete. The others are all on OPFLAG_FORDELETE cursors or else are
90319** marked with the AUXDELETE flag.
90320**
90321** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
90322** change count is incremented (otherwise not).
90323**
90324** P1 must not be pseudo-table. It has to be a real table with
90325** multiple rows.
90326**
90327** If P4 is not NULL then it points to a Table object. In this case either
90328** the update or pre-update hook, or both, may be invoked. The P1 cursor must
90329** have been positioned using OP_NotFound prior to invoking this opcode in
90330** this case. Specifically, if one is configured, the pre-update hook is
90331** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
90332** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
90333**
90334** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
90335** of the memory cell that contains the value that the rowid of the row will
90336** be set to by the update.
90337*/
90338case OP_Delete: {
90339 VdbeCursor *pC;
90340 const char *zDb;
90341 Table *pTab;
90342 int opflags;
90343
90344 opflags = pOp->p2;
90345 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90346 pC = p->apCsr[pOp->p1];
90347 assert( pC!=0 );
90348 assert( pC->eCurType==CURTYPE_BTREE );
90349 assert( pC->uc.pCursor!=0 );
90350 assert( pC->deferredMoveto==0 );
90351 sqlite3VdbeIncrWriteCounter(p, pC);
90352
90353#ifdef SQLITE_DEBUG
90354 if( pOp->p4type==P4_TABLE
90355 && HasRowid(pOp->p4.pTab)
90356 && pOp->p5==0
90357 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
90358 ){
90359 /* If p5 is zero, the seek operation that positioned the cursor prior to
90360 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
90361 ** the row that is being deleted */
90362 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90363 assert( CORRUPT_DB || pC->movetoTarget==iKey );
90364 }
90365#endif
90366
90367 /* If the update-hook or pre-update-hook will be invoked, set zDb to
90368 ** the name of the db to pass as to it. Also set local pTab to a copy
90369 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
90370 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
90371 ** VdbeCursor.movetoTarget to the current rowid. */
90372 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
90373 assert( pC->iDb>=0 );
90374 assert( pOp->p4.pTab!=0 );
90375 zDb = db->aDb[pC->iDb].zDbSName;
90376 pTab = pOp->p4.pTab;
90377 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
90378 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90379 }
90380 }else{
90381 zDb = 0; /* Not needed. Silence a compiler warning. */
90382 pTab = 0; /* Not needed. Silence a compiler warning. */
90383 }
90384
90385#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90386 /* Invoke the pre-update-hook if required. */
90387 if( db->xPreUpdateCallback && pOp->p4.pTab ){
90388 assert( !(opflags & OPFLAG_ISUPDATE)
90389 || HasRowid(pTab)==0
90390 || (aMem[pOp->p3].flags & MEM_Int)
90391 );
90392 sqlite3VdbePreUpdateHook(p, pC,
90393 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
90394 zDb, pTab, pC->movetoTarget,
90395 pOp->p3
90396 );
90397 }
90398 if( opflags & OPFLAG_ISNOOP ) break;
90399#endif
90400
90401 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
90402 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
90403 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
90404 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
90405
90406#ifdef SQLITE_DEBUG
90407 if( p->pFrame==0 ){
90408 if( pC->isEphemeral==0
90409 && (pOp->p5 & OPFLAG_AUXDELETE)==0
90410 && (pC->wrFlag & OPFLAG_FORDELETE)==0
90411 ){
90412 nExtraDelete++;
90413 }
90414 if( pOp->p2 & OPFLAG_NCHANGE ){
90415 nExtraDelete--;
90416 }
90417 }
90418#endif
90419
90420 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
90421 pC->cacheStatus = CACHE_STALE;
90422 pC->seekResult = 0;
90423 if( rc ) goto abort_due_to_error;
90424
90425 /* Invoke the update-hook if required. */
90426 if( opflags & OPFLAG_NCHANGE ){
90427 p->nChange++;
90428 if( db->xUpdateCallback && HasRowid(pTab) ){
90429 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
90430 pC->movetoTarget);
90431 assert( pC->iDb>=0 );
90432 }
90433 }
90434
90435 break;
90436}
90437/* Opcode: ResetCount * * * * *
90438**
90439** The value of the change counter is copied to the database handle
90440** change counter (returned by subsequent calls to sqlite3_changes()).
90441** Then the VMs internal change counter resets to 0.
90442** This is used by trigger programs.
90443*/
90444case OP_ResetCount: {
90445 sqlite3VdbeSetChanges(db, p->nChange);
90446 p->nChange = 0;
90447 break;
90448}
90449
90450/* Opcode: SorterCompare P1 P2 P3 P4
90451** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
90452**
90453** P1 is a sorter cursor. This instruction compares a prefix of the
90454** record blob in register P3 against a prefix of the entry that
90455** the sorter cursor currently points to. Only the first P4 fields
90456** of r[P3] and the sorter record are compared.
90457**
90458** If either P3 or the sorter contains a NULL in one of their significant
90459** fields (not counting the P4 fields at the end which are ignored) then
90460** the comparison is assumed to be equal.
90461**
90462** Fall through to next instruction if the two records compare equal to
90463** each other. Jump to P2 if they are different.
90464*/
90465case OP_SorterCompare: {
90466 VdbeCursor *pC;
90467 int res;
90468 int nKeyCol;
90469
90470 pC = p->apCsr[pOp->p1];
90471 assert( isSorter(pC) );
90472 assert( pOp->p4type==P4_INT32 );
90473 pIn3 = &aMem[pOp->p3];
90474 nKeyCol = pOp->p4.i;
90475 res = 0;
90476 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
90477 VdbeBranchTaken(res!=0,2);
90478 if( rc ) goto abort_due_to_error;
90479 if( res ) goto jump_to_p2;
90480 break;
90481};
90482
90483/* Opcode: SorterData P1 P2 P3 * *
90484** Synopsis: r[P2]=data
90485**
90486** Write into register P2 the current sorter data for sorter cursor P1.
90487** Then clear the column header cache on cursor P3.
90488**
90489** This opcode is normally use to move a record out of the sorter and into
90490** a register that is the source for a pseudo-table cursor created using
90491** OpenPseudo. That pseudo-table cursor is the one that is identified by
90492** parameter P3. Clearing the P3 column cache as part of this opcode saves
90493** us from having to issue a separate NullRow instruction to clear that cache.
90494*/
90495case OP_SorterData: {
90496 VdbeCursor *pC;
90497
90498 pOut = &aMem[pOp->p2];
90499 pC = p->apCsr[pOp->p1];
90500 assert( isSorter(pC) );
90501 rc = sqlite3VdbeSorterRowkey(pC, pOut);
90502 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
90503 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90504 if( rc ) goto abort_due_to_error;
90505 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
90506 break;
90507}
90508
90509/* Opcode: RowData P1 P2 P3 * *
90510** Synopsis: r[P2]=data
90511**
90512** Write into register P2 the complete row content for the row at
90513** which cursor P1 is currently pointing.
90514** There is no interpretation of the data.
90515** It is just copied onto the P2 register exactly as
90516** it is found in the database file.
90517**
90518** If cursor P1 is an index, then the content is the key of the row.
90519** If cursor P2 is a table, then the content extracted is the data.
90520**
90521** If the P1 cursor must be pointing to a valid row (not a NULL row)
90522** of a real table, not a pseudo-table.
90523**
90524** If P3!=0 then this opcode is allowed to make an ephemeral pointer
90525** into the database page. That means that the content of the output
90526** register will be invalidated as soon as the cursor moves - including
90527** moves caused by other cursors that "save" the current cursors
90528** position in order that they can write to the same table. If P3==0
90529** then a copy of the data is made into memory. P3!=0 is faster, but
90530** P3==0 is safer.
90531**
90532** If P3!=0 then the content of the P2 register is unsuitable for use
90533** in OP_Result and any OP_Result will invalidate the P2 register content.
90534** The P2 register content is invalidated by opcodes like OP_Function or
90535** by any use of another cursor pointing to the same table.
90536*/
90537case OP_RowData: {
90538 VdbeCursor *pC;
90539 BtCursor *pCrsr;
90540 u32 n;
90541
90542 pOut = out2Prerelease(p, pOp);
90543
90544 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90545 pC = p->apCsr[pOp->p1];
90546 assert( pC!=0 );
90547 assert( pC->eCurType==CURTYPE_BTREE );
90548 assert( isSorter(pC)==0 );
90549 assert( pC->nullRow==0 );
90550 assert( pC->uc.pCursor!=0 );
90551 pCrsr = pC->uc.pCursor;
90552
90553 /* The OP_RowData opcodes always follow OP_NotExists or
90554 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
90555 ** that might invalidate the cursor.
90556 ** If this where not the case, on of the following assert()s
90557 ** would fail. Should this ever change (because of changes in the code
90558 ** generator) then the fix would be to insert a call to
90559 ** sqlite3VdbeCursorMoveto().
90560 */
90561 assert( pC->deferredMoveto==0 );
90562 assert( sqlite3BtreeCursorIsValid(pCrsr) );
90563
90564 n = sqlite3BtreePayloadSize(pCrsr);
90565 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
90566 goto too_big;
90567 }
90568 testcase( n==0 );
90569 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
90570 if( rc ) goto abort_due_to_error;
90571 if( !pOp->p3 ) Deephemeralize(pOut);
90572 UPDATE_MAX_BLOBSIZE(pOut);
90573 REGISTER_TRACE(pOp->p2, pOut);
90574 break;
90575}
90576
90577/* Opcode: Rowid P1 P2 * * *
90578** Synopsis: r[P2]=rowid
90579**
90580** Store in register P2 an integer which is the key of the table entry that
90581** P1 is currently point to.
90582**
90583** P1 can be either an ordinary table or a virtual table. There used to
90584** be a separate OP_VRowid opcode for use with virtual tables, but this
90585** one opcode now works for both table types.
90586*/
90587case OP_Rowid: { /* out2 */
90588 VdbeCursor *pC;
90589 i64 v;
90590 sqlite3_vtab *pVtab;
90591 const sqlite3_module *pModule;
90592
90593 pOut = out2Prerelease(p, pOp);
90594 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90595 pC = p->apCsr[pOp->p1];
90596 assert( pC!=0 );
90597 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
90598 if( pC->nullRow ){
90599 pOut->flags = MEM_Null;
90600 break;
90601 }else if( pC->deferredMoveto ){
90602 v = pC->movetoTarget;
90603#ifndef SQLITE_OMIT_VIRTUALTABLE
90604 }else if( pC->eCurType==CURTYPE_VTAB ){
90605 assert( pC->uc.pVCur!=0 );
90606 pVtab = pC->uc.pVCur->pVtab;
90607 pModule = pVtab->pModule;
90608 assert( pModule->xRowid );
90609 rc = pModule->xRowid(pC->uc.pVCur, &v);
90610 sqlite3VtabImportErrmsg(p, pVtab);
90611 if( rc ) goto abort_due_to_error;
90612#endif /* SQLITE_OMIT_VIRTUALTABLE */
90613 }else{
90614 assert( pC->eCurType==CURTYPE_BTREE );
90615 assert( pC->uc.pCursor!=0 );
90616 rc = sqlite3VdbeCursorRestore(pC);
90617 if( rc ) goto abort_due_to_error;
90618 if( pC->nullRow ){
90619 pOut->flags = MEM_Null;
90620 break;
90621 }
90622 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90623 }
90624 pOut->u.i = v;
90625 break;
90626}
90627
90628/* Opcode: NullRow P1 * * * *
90629**
90630** Move the cursor P1 to a null row. Any OP_Column operations
90631** that occur while the cursor is on the null row will always
90632** write a NULL.
90633*/
90634case OP_NullRow: {
90635 VdbeCursor *pC;
90636
90637 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90638 pC = p->apCsr[pOp->p1];
90639 assert( pC!=0 );
90640 pC->nullRow = 1;
90641 pC->cacheStatus = CACHE_STALE;
90642 if( pC->eCurType==CURTYPE_BTREE ){
90643 assert( pC->uc.pCursor!=0 );
90644 sqlite3BtreeClearCursor(pC->uc.pCursor);
90645 }
90646#ifdef SQLITE_DEBUG
90647 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
90648#endif
90649 break;
90650}
90651
90652/* Opcode: SeekEnd P1 * * * *
90653**
90654** Position cursor P1 at the end of the btree for the purpose of
90655** appending a new entry onto the btree.
90656**
90657** It is assumed that the cursor is used only for appending and so
90658** if the cursor is valid, then the cursor must already be pointing
90659** at the end of the btree and so no changes are made to
90660** the cursor.
90661*/
90662/* Opcode: Last P1 P2 * * *
90663**
90664** The next use of the Rowid or Column or Prev instruction for P1
90665** will refer to the last entry in the database table or index.
90666** If the table or index is empty and P2>0, then jump immediately to P2.
90667** If P2 is 0 or if the table or index is not empty, fall through
90668** to the following instruction.
90669**
90670** This opcode leaves the cursor configured to move in reverse order,
90671** from the end toward the beginning. In other words, the cursor is
90672** configured to use Prev, not Next.
90673*/
90674case OP_SeekEnd:
90675case OP_Last: { /* jump */
90676 VdbeCursor *pC;
90677 BtCursor *pCrsr;
90678 int res;
90679
90680 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90681 pC = p->apCsr[pOp->p1];
90682 assert( pC!=0 );
90683 assert( pC->eCurType==CURTYPE_BTREE );
90684 pCrsr = pC->uc.pCursor;
90685 res = 0;
90686 assert( pCrsr!=0 );
90687#ifdef SQLITE_DEBUG
90688 pC->seekOp = pOp->opcode;
90689#endif
90690 if( pOp->opcode==OP_SeekEnd ){
90691 assert( pOp->p2==0 );
90692 pC->seekResult = -1;
90693 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
90694 break;
90695 }
90696 }
90697 rc = sqlite3BtreeLast(pCrsr, &res);
90698 pC->nullRow = (u8)res;
90699 pC->deferredMoveto = 0;
90700 pC->cacheStatus = CACHE_STALE;
90701 if( rc ) goto abort_due_to_error;
90702 if( pOp->p2>0 ){
90703 VdbeBranchTaken(res!=0,2);
90704 if( res ) goto jump_to_p2;
90705 }
90706 break;
90707}
90708
90709/* Opcode: IfSmaller P1 P2 P3 * *
90710**
90711** Estimate the number of rows in the table P1. Jump to P2 if that
90712** estimate is less than approximately 2**(0.1*P3).
90713*/
90714case OP_IfSmaller: { /* jump */
90715 VdbeCursor *pC;
90716 BtCursor *pCrsr;
90717 int res;
90718 i64 sz;
90719
90720 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90721 pC = p->apCsr[pOp->p1];
90722 assert( pC!=0 );
90723 pCrsr = pC->uc.pCursor;
90724 assert( pCrsr );
90725 rc = sqlite3BtreeFirst(pCrsr, &res);
90726 if( rc ) goto abort_due_to_error;
90727 if( res==0 ){
90728 sz = sqlite3BtreeRowCountEst(pCrsr);
90729 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
90730 }
90731 VdbeBranchTaken(res!=0,2);
90732 if( res ) goto jump_to_p2;
90733 break;
90734}
90735
90736
90737/* Opcode: SorterSort P1 P2 * * *
90738**
90739** After all records have been inserted into the Sorter object
90740** identified by P1, invoke this opcode to actually do the sorting.
90741** Jump to P2 if there are no records to be sorted.
90742**
90743** This opcode is an alias for OP_Sort and OP_Rewind that is used
90744** for Sorter objects.
90745*/
90746/* Opcode: Sort P1 P2 * * *
90747**
90748** This opcode does exactly the same thing as OP_Rewind except that
90749** it increments an undocumented global variable used for testing.
90750**
90751** Sorting is accomplished by writing records into a sorting index,
90752** then rewinding that index and playing it back from beginning to
90753** end. We use the OP_Sort opcode instead of OP_Rewind to do the
90754** rewinding so that the global variable will be incremented and
90755** regression tests can determine whether or not the optimizer is
90756** correctly optimizing out sorts.
90757*/
90758case OP_SorterSort: /* jump */
90759case OP_Sort: { /* jump */
90760#ifdef SQLITE_TEST
90761 sqlite3_sort_count++;
90762 sqlite3_search_count--;
90763#endif
90764 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
90765 /* Fall through into OP_Rewind */
90766 /* no break */ deliberate_fall_through
90767}
90768/* Opcode: Rewind P1 P2 * * *
90769**
90770** The next use of the Rowid or Column or Next instruction for P1
90771** will refer to the first entry in the database table or index.
90772** If the table or index is empty, jump immediately to P2.
90773** If the table or index is not empty, fall through to the following
90774** instruction.
90775**
90776** This opcode leaves the cursor configured to move in forward order,
90777** from the beginning toward the end. In other words, the cursor is
90778** configured to use Next, not Prev.
90779*/
90780case OP_Rewind: { /* jump */
90781 VdbeCursor *pC;
90782 BtCursor *pCrsr;
90783 int res;
90784
90785 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90786 assert( pOp->p5==0 );
90787 pC = p->apCsr[pOp->p1];
90788 assert( pC!=0 );
90789 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
90790 res = 1;
90791#ifdef SQLITE_DEBUG
90792 pC->seekOp = OP_Rewind;
90793#endif
90794 if( isSorter(pC) ){
90795 rc = sqlite3VdbeSorterRewind(pC, &res);
90796 }else{
90797 assert( pC->eCurType==CURTYPE_BTREE );
90798 pCrsr = pC->uc.pCursor;
90799 assert( pCrsr );
90800 rc = sqlite3BtreeFirst(pCrsr, &res);
90801 pC->deferredMoveto = 0;
90802 pC->cacheStatus = CACHE_STALE;
90803 }
90804 if( rc ) goto abort_due_to_error;
90805 pC->nullRow = (u8)res;
90806 assert( pOp->p2>0 && pOp->p2<p->nOp );
90807 VdbeBranchTaken(res!=0,2);
90808 if( res ) goto jump_to_p2;
90809 break;
90810}
90811
90812/* Opcode: Next P1 P2 P3 P4 P5
90813**
90814** Advance cursor P1 so that it points to the next key/data pair in its
90815** table or index. If there are no more key/value pairs then fall through
90816** to the following instruction. But if the cursor advance was successful,
90817** jump immediately to P2.
90818**
90819** The Next opcode is only valid following an SeekGT, SeekGE, or
90820** OP_Rewind opcode used to position the cursor. Next is not allowed
90821** to follow SeekLT, SeekLE, or OP_Last.
90822**
90823** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
90824** been opened prior to this opcode or the program will segfault.
90825**
90826** The P3 value is a hint to the btree implementation. If P3==1, that
90827** means P1 is an SQL index and that this instruction could have been
90828** omitted if that index had been unique. P3 is usually 0. P3 is
90829** always either 0 or 1.
90830**
90831** P4 is always of type P4_ADVANCE. The function pointer points to
90832** sqlite3BtreeNext().
90833**
90834** If P5 is positive and the jump is taken, then event counter
90835** number P5-1 in the prepared statement is incremented.
90836**
90837** See also: Prev
90838*/
90839/* Opcode: Prev P1 P2 P3 P4 P5
90840**
90841** Back up cursor P1 so that it points to the previous key/data pair in its
90842** table or index. If there is no previous key/value pairs then fall through
90843** to the following instruction. But if the cursor backup was successful,
90844** jump immediately to P2.
90845**
90846**
90847** The Prev opcode is only valid following an SeekLT, SeekLE, or
90848** OP_Last opcode used to position the cursor. Prev is not allowed
90849** to follow SeekGT, SeekGE, or OP_Rewind.
90850**
90851** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
90852** not open then the behavior is undefined.
90853**
90854** The P3 value is a hint to the btree implementation. If P3==1, that
90855** means P1 is an SQL index and that this instruction could have been
90856** omitted if that index had been unique. P3 is usually 0. P3 is
90857** always either 0 or 1.
90858**
90859** P4 is always of type P4_ADVANCE. The function pointer points to
90860** sqlite3BtreePrevious().
90861**
90862** If P5 is positive and the jump is taken, then event counter
90863** number P5-1 in the prepared statement is incremented.
90864*/
90865/* Opcode: SorterNext P1 P2 * * P5
90866**
90867** This opcode works just like OP_Next except that P1 must be a
90868** sorter object for which the OP_SorterSort opcode has been
90869** invoked. This opcode advances the cursor to the next sorted
90870** record, or jumps to P2 if there are no more sorted records.
90871*/
90872case OP_SorterNext: { /* jump */
90873 VdbeCursor *pC;
90874
90875 pC = p->apCsr[pOp->p1];
90876 assert( isSorter(pC) );
90877 rc = sqlite3VdbeSorterNext(db, pC);
90878 goto next_tail;
90879case OP_Prev: /* jump */
90880case OP_Next: /* jump */
90881 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90882 assert( pOp->p5<ArraySize(p->aCounter) );
90883 pC = p->apCsr[pOp->p1];
90884 assert( pC!=0 );
90885 assert( pC->deferredMoveto==0 );
90886 assert( pC->eCurType==CURTYPE_BTREE );
90887 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
90888 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
90889
90890 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
90891 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
90892 assert( pOp->opcode!=OP_Next
90893 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
90894 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
90895 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
90896 || pC->seekOp==OP_IfNoHope);
90897 assert( pOp->opcode!=OP_Prev
90898 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
90899 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
90900 || pC->seekOp==OP_NullRow);
90901
90902 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
90903next_tail:
90904 pC->cacheStatus = CACHE_STALE;
90905 VdbeBranchTaken(rc==SQLITE_OK,2);
90906 if( rc==SQLITE_OK ){
90907 pC->nullRow = 0;
90908 p->aCounter[pOp->p5]++;
90909#ifdef SQLITE_TEST
90910 sqlite3_search_count++;
90911#endif
90912 goto jump_to_p2_and_check_for_interrupt;
90913 }
90914 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
90915 rc = SQLITE_OK;
90916 pC->nullRow = 1;
90917 goto check_for_interrupt;
90918}
90919
90920/* Opcode: IdxInsert P1 P2 P3 P4 P5
90921** Synopsis: key=r[P2]
90922**
90923** Register P2 holds an SQL index key made using the
90924** MakeRecord instructions. This opcode writes that key
90925** into the index P1. Data for the entry is nil.
90926**
90927** If P4 is not zero, then it is the number of values in the unpacked
90928** key of reg(P2). In that case, P3 is the index of the first register
90929** for the unpacked key. The availability of the unpacked key can sometimes
90930** be an optimization.
90931**
90932** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
90933** that this insert is likely to be an append.
90934**
90935** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
90936** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
90937** then the change counter is unchanged.
90938**
90939** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
90940** run faster by avoiding an unnecessary seek on cursor P1. However,
90941** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
90942** seeks on the cursor or if the most recent seek used a key equivalent
90943** to P2.
90944**
90945** This instruction only works for indices. The equivalent instruction
90946** for tables is OP_Insert.
90947*/
90948case OP_IdxInsert: { /* in2 */
90949 VdbeCursor *pC;
90950 BtreePayload x;
90951
90952 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90953 pC = p->apCsr[pOp->p1];
90954 sqlite3VdbeIncrWriteCounter(p, pC);
90955 assert( pC!=0 );
90956 assert( !isSorter(pC) );
90957 pIn2 = &aMem[pOp->p2];
90958 assert( pIn2->flags & MEM_Blob );
90959 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
90960 assert( pC->eCurType==CURTYPE_BTREE );
90961 assert( pC->isTable==0 );
90962 rc = ExpandBlob(pIn2);
90963 if( rc ) goto abort_due_to_error;
90964 x.nKey = pIn2->n;
90965 x.pKey = pIn2->z;
90966 x.aMem = aMem + pOp->p3;
90967 x.nMem = (u16)pOp->p4.i;
90968 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
90969 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
90970 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
90971 );
90972 assert( pC->deferredMoveto==0 );
90973 pC->cacheStatus = CACHE_STALE;
90974 if( rc) goto abort_due_to_error;
90975 break;
90976}
90977
90978/* Opcode: SorterInsert P1 P2 * * *
90979** Synopsis: key=r[P2]
90980**
90981** Register P2 holds an SQL index key made using the
90982** MakeRecord instructions. This opcode writes that key
90983** into the sorter P1. Data for the entry is nil.
90984*/
90985case OP_SorterInsert: { /* in2 */
90986 VdbeCursor *pC;
90987
90988 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90989 pC = p->apCsr[pOp->p1];
90990 sqlite3VdbeIncrWriteCounter(p, pC);
90991 assert( pC!=0 );
90992 assert( isSorter(pC) );
90993 pIn2 = &aMem[pOp->p2];
90994 assert( pIn2->flags & MEM_Blob );
90995 assert( pC->isTable==0 );
90996 rc = ExpandBlob(pIn2);
90997 if( rc ) goto abort_due_to_error;
90998 rc = sqlite3VdbeSorterWrite(pC, pIn2);
90999 if( rc) goto abort_due_to_error;
91000 break;
91001}
91002
91003/* Opcode: IdxDelete P1 P2 P3 * P5
91004** Synopsis: key=r[P2@P3]
91005**
91006** The content of P3 registers starting at register P2 form
91007** an unpacked index key. This opcode removes that entry from the
91008** index opened by cursor P1.
91009**
91010** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
91011** if no matching index entry is found. This happens when running
91012** an UPDATE or DELETE statement and the index entry to be updated
91013** or deleted is not found. For some uses of IdxDelete
91014** (example: the EXCEPT operator) it does not matter that no matching
91015** entry is found. For those cases, P5 is zero.
91016*/
91017case OP_IdxDelete: {
91018 VdbeCursor *pC;
91019 BtCursor *pCrsr;
91020 int res;
91021 UnpackedRecord r;
91022
91023 assert( pOp->p3>0 );
91024 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
91025 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91026 pC = p->apCsr[pOp->p1];
91027 assert( pC!=0 );
91028 assert( pC->eCurType==CURTYPE_BTREE );
91029 sqlite3VdbeIncrWriteCounter(p, pC);
91030 pCrsr = pC->uc.pCursor;
91031 assert( pCrsr!=0 );
91032 r.pKeyInfo = pC->pKeyInfo;
91033 r.nField = (u16)pOp->p3;
91034 r.default_rc = 0;
91035 r.aMem = &aMem[pOp->p2];
91036 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
91037 if( rc ) goto abort_due_to_error;
91038 if( res==0 ){
91039 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
91040 if( rc ) goto abort_due_to_error;
91041 }else if( pOp->p5 ){
91042 rc = SQLITE_CORRUPT_INDEX;
91043 goto abort_due_to_error;
91044 }
91045 assert( pC->deferredMoveto==0 );
91046 pC->cacheStatus = CACHE_STALE;
91047 pC->seekResult = 0;
91048 break;
91049}
91050
91051/* Opcode: DeferredSeek P1 * P3 P4 *
91052** Synopsis: Move P3 to P1.rowid if needed
91053**
91054** P1 is an open index cursor and P3 is a cursor on the corresponding
91055** table. This opcode does a deferred seek of the P3 table cursor
91056** to the row that corresponds to the current row of P1.
91057**
91058** This is a deferred seek. Nothing actually happens until
91059** the cursor is used to read a record. That way, if no reads
91060** occur, no unnecessary I/O happens.
91061**
91062** P4 may be an array of integers (type P4_INTARRAY) containing
91063** one entry for each column in the P3 table. If array entry a(i)
91064** is non-zero, then reading column a(i)-1 from cursor P3 is
91065** equivalent to performing the deferred seek and then reading column i
91066** from P1. This information is stored in P3 and used to redirect
91067** reads against P3 over to P1, thus possibly avoiding the need to
91068** seek and read cursor P3.
91069*/
91070/* Opcode: IdxRowid P1 P2 * * *
91071** Synopsis: r[P2]=rowid
91072**
91073** Write into register P2 an integer which is the last entry in the record at
91074** the end of the index key pointed to by cursor P1. This integer should be
91075** the rowid of the table entry to which this index entry points.
91076**
91077** See also: Rowid, MakeRecord.
91078*/
91079case OP_DeferredSeek:
91080case OP_IdxRowid: { /* out2 */
91081 VdbeCursor *pC; /* The P1 index cursor */
91082 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
91083 i64 rowid; /* Rowid that P1 current points to */
91084
91085 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91086 pC = p->apCsr[pOp->p1];
91087 assert( pC!=0 );
91088 assert( pC->eCurType==CURTYPE_BTREE );
91089 assert( pC->uc.pCursor!=0 );
91090 assert( pC->isTable==0 );
91091 assert( pC->deferredMoveto==0 );
91092 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
91093
91094 /* The IdxRowid and Seek opcodes are combined because of the commonality
91095 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
91096 rc = sqlite3VdbeCursorRestore(pC);
91097
91098 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
91099 ** out from under the cursor. That will never happens for an IdxRowid
91100 ** or Seek opcode */
91101 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
91102
91103 if( !pC->nullRow ){
91104 rowid = 0; /* Not needed. Only used to silence a warning. */
91105 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
91106 if( rc!=SQLITE_OK ){
91107 goto abort_due_to_error;
91108 }
91109 if( pOp->opcode==OP_DeferredSeek ){
91110 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
91111 pTabCur = p->apCsr[pOp->p3];
91112 assert( pTabCur!=0 );
91113 assert( pTabCur->eCurType==CURTYPE_BTREE );
91114 assert( pTabCur->uc.pCursor!=0 );
91115 assert( pTabCur->isTable );
91116 pTabCur->nullRow = 0;
91117 pTabCur->movetoTarget = rowid;
91118 pTabCur->deferredMoveto = 1;
91119 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
91120 pTabCur->aAltMap = pOp->p4.ai;
91121 pTabCur->pAltCursor = pC;
91122 }else{
91123 pOut = out2Prerelease(p, pOp);
91124 pOut->u.i = rowid;
91125 }
91126 }else{
91127 assert( pOp->opcode==OP_IdxRowid );
91128 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
91129 }
91130 break;
91131}
91132
91133/* Opcode: FinishSeek P1 * * * *
91134**
91135** If cursor P1 was previously moved via OP_DeferredSeek, complete that
91136** seek operation now, without further delay. If the cursor seek has
91137** already occurred, this instruction is a no-op.
91138*/
91139case OP_FinishSeek: {
91140 VdbeCursor *pC; /* The P1 index cursor */
91141
91142 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91143 pC = p->apCsr[pOp->p1];
91144 if( pC->deferredMoveto ){
91145 rc = sqlite3VdbeFinishMoveto(pC);
91146 if( rc ) goto abort_due_to_error;
91147 }
91148 break;
91149}
91150
91151/* Opcode: IdxGE P1 P2 P3 P4 P5
91152** Synopsis: key=r[P3@P4]
91153**
91154** The P4 register values beginning with P3 form an unpacked index
91155** key that omits the PRIMARY KEY. Compare this key value against the index
91156** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
91157** fields at the end.
91158**
91159** If the P1 index entry is greater than or equal to the key value
91160** then jump to P2. Otherwise fall through to the next instruction.
91161*/
91162/* Opcode: IdxGT P1 P2 P3 P4 P5
91163** Synopsis: key=r[P3@P4]
91164**
91165** The P4 register values beginning with P3 form an unpacked index
91166** key that omits the PRIMARY KEY. Compare this key value against the index
91167** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
91168** fields at the end.
91169**
91170** If the P1 index entry is greater than the key value
91171** then jump to P2. Otherwise fall through to the next instruction.
91172*/
91173/* Opcode: IdxLT P1 P2 P3 P4 P5
91174** Synopsis: key=r[P3@P4]
91175**
91176** The P4 register values beginning with P3 form an unpacked index
91177** key that omits the PRIMARY KEY or ROWID. Compare this key value against
91178** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
91179** ROWID on the P1 index.
91180**
91181** If the P1 index entry is less than the key value then jump to P2.
91182** Otherwise fall through to the next instruction.
91183*/
91184/* Opcode: IdxLE P1 P2 P3 P4 P5
91185** Synopsis: key=r[P3@P4]
91186**
91187** The P4 register values beginning with P3 form an unpacked index
91188** key that omits the PRIMARY KEY or ROWID. Compare this key value against
91189** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
91190** ROWID on the P1 index.
91191**
91192** If the P1 index entry is less than or equal to the key value then jump
91193** to P2. Otherwise fall through to the next instruction.
91194*/
91195case OP_IdxLE: /* jump */
91196case OP_IdxGT: /* jump */
91197case OP_IdxLT: /* jump */
91198case OP_IdxGE: { /* jump */
91199 VdbeCursor *pC;
91200 int res;
91201 UnpackedRecord r;
91202
91203 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91204 pC = p->apCsr[pOp->p1];
91205 assert( pC!=0 );
91206 assert( pC->isOrdered );
91207 assert( pC->eCurType==CURTYPE_BTREE );
91208 assert( pC->uc.pCursor!=0);
91209 assert( pC->deferredMoveto==0 );
91210 assert( pOp->p5==0 || pOp->p5==1 );
91211 assert( pOp->p4type==P4_INT32 );
91212 r.pKeyInfo = pC->pKeyInfo;
91213 r.nField = (u16)pOp->p4.i;
91214 if( pOp->opcode<OP_IdxLT ){
91215 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
91216 r.default_rc = -1;
91217 }else{
91218 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
91219 r.default_rc = 0;
91220 }
91221 r.aMem = &aMem[pOp->p3];
91222#ifdef SQLITE_DEBUG
91223 {
91224 int i;
91225 for(i=0; i<r.nField; i++){
91226 assert( memIsValid(&r.aMem[i]) );
91227 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
91228 }
91229 }
91230#endif
91231 res = 0; /* Not needed. Only used to silence a warning. */
91232 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
91233 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
91234 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
91235 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
91236 res = -res;
91237 }else{
91238 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
91239 res++;
91240 }
91241 VdbeBranchTaken(res>0,2);
91242 if( rc ) goto abort_due_to_error;
91243 if( res>0 ) goto jump_to_p2;
91244 break;
91245}
91246
91247/* Opcode: Destroy P1 P2 P3 * *
91248**
91249** Delete an entire database table or index whose root page in the database
91250** file is given by P1.
91251**
91252** The table being destroyed is in the main database file if P3==0. If
91253** P3==1 then the table to be clear is in the auxiliary database file
91254** that is used to store tables create using CREATE TEMPORARY TABLE.
91255**
91256** If AUTOVACUUM is enabled then it is possible that another root page
91257** might be moved into the newly deleted root page in order to keep all
91258** root pages contiguous at the beginning of the database. The former
91259** value of the root page that moved - its value before the move occurred -
91260** is stored in register P2. If no page movement was required (because the
91261** table being dropped was already the last one in the database) then a
91262** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
91263** is stored in register P2.
91264**
91265** This opcode throws an error if there are any active reader VMs when
91266** it is invoked. This is done to avoid the difficulty associated with
91267** updating existing cursors when a root page is moved in an AUTOVACUUM
91268** database. This error is thrown even if the database is not an AUTOVACUUM
91269** db in order to avoid introducing an incompatibility between autovacuum
91270** and non-autovacuum modes.
91271**
91272** See also: Clear
91273*/
91274case OP_Destroy: { /* out2 */
91275 int iMoved;
91276 int iDb;
91277
91278 sqlite3VdbeIncrWriteCounter(p, 0);
91279 assert( p->readOnly==0 );
91280 assert( pOp->p1>1 );
91281 pOut = out2Prerelease(p, pOp);
91282 pOut->flags = MEM_Null;
91283 if( db->nVdbeRead > db->nVDestroy+1 ){
91284 rc = SQLITE_LOCKED;
91285 p->errorAction = OE_Abort;
91286 goto abort_due_to_error;
91287 }else{
91288 iDb = pOp->p3;
91289 assert( DbMaskTest(p->btreeMask, iDb) );
91290 iMoved = 0; /* Not needed. Only to silence a warning. */
91291 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
91292 pOut->flags = MEM_Int;
91293 pOut->u.i = iMoved;
91294 if( rc ) goto abort_due_to_error;
91295#ifndef SQLITE_OMIT_AUTOVACUUM
91296 if( iMoved!=0 ){
91297 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
91298 /* All OP_Destroy operations occur on the same btree */
91299 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
91300 resetSchemaOnFault = iDb+1;
91301 }
91302#endif
91303 }
91304 break;
91305}
91306
91307/* Opcode: Clear P1 P2 P3
91308**
91309** Delete all contents of the database table or index whose root page
91310** in the database file is given by P1. But, unlike Destroy, do not
91311** remove the table or index from the database file.
91312**
91313** The table being clear is in the main database file if P2==0. If
91314** P2==1 then the table to be clear is in the auxiliary database file
91315** that is used to store tables create using CREATE TEMPORARY TABLE.
91316**
91317** If the P3 value is non-zero, then the table referred to must be an
91318** intkey table (an SQL table, not an index). In this case the row change
91319** count is incremented by the number of rows in the table being cleared.
91320** If P3 is greater than zero, then the value stored in register P3 is
91321** also incremented by the number of rows in the table being cleared.
91322**
91323** See also: Destroy
91324*/
91325case OP_Clear: {
91326 int nChange;
91327
91328 sqlite3VdbeIncrWriteCounter(p, 0);
91329 nChange = 0;
91330 assert( p->readOnly==0 );
91331 assert( DbMaskTest(p->btreeMask, pOp->p2) );
91332 rc = sqlite3BtreeClearTable(
91333 db->aDb[pOp->p2].pBt, (u32)pOp->p1, (pOp->p3 ? &nChange : 0)
91334 );
91335 if( pOp->p3 ){
91336 p->nChange += nChange;
91337 if( pOp->p3>0 ){
91338 assert( memIsValid(&aMem[pOp->p3]) );
91339 memAboutToChange(p, &aMem[pOp->p3]);
91340 aMem[pOp->p3].u.i += nChange;
91341 }
91342 }
91343 if( rc ) goto abort_due_to_error;
91344 break;
91345}
91346
91347/* Opcode: ResetSorter P1 * * * *
91348**
91349** Delete all contents from the ephemeral table or sorter
91350** that is open on cursor P1.
91351**
91352** This opcode only works for cursors used for sorting and
91353** opened with OP_OpenEphemeral or OP_SorterOpen.
91354*/
91355case OP_ResetSorter: {
91356 VdbeCursor *pC;
91357
91358 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91359 pC = p->apCsr[pOp->p1];
91360 assert( pC!=0 );
91361 if( isSorter(pC) ){
91362 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
91363 }else{
91364 assert( pC->eCurType==CURTYPE_BTREE );
91365 assert( pC->isEphemeral );
91366 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
91367 if( rc ) goto abort_due_to_error;
91368 }
91369 break;
91370}
91371
91372/* Opcode: CreateBtree P1 P2 P3 * *
91373** Synopsis: r[P2]=root iDb=P1 flags=P3
91374**
91375** Allocate a new b-tree in the main database file if P1==0 or in the
91376** TEMP database file if P1==1 or in an attached database if
91377** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
91378** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
91379** The root page number of the new b-tree is stored in register P2.
91380*/
91381case OP_CreateBtree: { /* out2 */
91382 Pgno pgno;
91383 Db *pDb;
91384
91385 sqlite3VdbeIncrWriteCounter(p, 0);
91386 pOut = out2Prerelease(p, pOp);
91387 pgno = 0;
91388 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
91389 assert( pOp->p1>=0 && pOp->p1<db->nDb );
91390 assert( DbMaskTest(p->btreeMask, pOp->p1) );
91391 assert( p->readOnly==0 );
91392 pDb = &db->aDb[pOp->p1];
91393 assert( pDb->pBt!=0 );
91394 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
91395 if( rc ) goto abort_due_to_error;
91396 pOut->u.i = pgno;
91397 break;
91398}
91399
91400/* Opcode: SqlExec * * * P4 *
91401**
91402** Run the SQL statement or statements specified in the P4 string.
91403*/
91404case OP_SqlExec: {
91405 sqlite3VdbeIncrWriteCounter(p, 0);
91406 db->nSqlExec++;
91407 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
91408 db->nSqlExec--;
91409 if( rc ) goto abort_due_to_error;
91410 break;
91411}
91412
91413/* Opcode: ParseSchema P1 * * P4 *
91414**
91415** Read and parse all entries from the schema table of database P1
91416** that match the WHERE clause P4. If P4 is a NULL pointer, then the
91417** entire schema for P1 is reparsed.
91418**
91419** This opcode invokes the parser to create a new virtual machine,
91420** then runs the new virtual machine. It is thus a re-entrant opcode.
91421*/
91422case OP_ParseSchema: {
91423 int iDb;
91424 const char *zSchema;
91425 char *zSql;
91426 InitData initData;
91427
91428 /* Any prepared statement that invokes this opcode will hold mutexes
91429 ** on every btree. This is a prerequisite for invoking
91430 ** sqlite3InitCallback().
91431 */
91432#ifdef SQLITE_DEBUG
91433 for(iDb=0; iDb<db->nDb; iDb++){
91434 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
91435 }
91436#endif
91437
91438 iDb = pOp->p1;
91439 assert( iDb>=0 && iDb<db->nDb );
91440 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
91441
91442#ifndef SQLITE_OMIT_ALTERTABLE
91443 if( pOp->p4.z==0 ){
91444 sqlite3SchemaClear(db->aDb[iDb].pSchema);
91445 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
91446 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
91447 db->mDbFlags |= DBFLAG_SchemaChange;
91448 p->expired = 0;
91449 }else
91450#endif
91451 {
91452 zSchema = DFLT_SCHEMA_TABLE;
91453 initData.db = db;
91454 initData.iDb = iDb;
91455 initData.pzErrMsg = &p->zErrMsg;
91456 initData.mInitFlags = 0;
91457 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
91458 zSql = sqlite3MPrintf(db,
91459 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
91460 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
91461 if( zSql==0 ){
91462 rc = SQLITE_NOMEM_BKPT;
91463 }else{
91464 assert( db->init.busy==0 );
91465 db->init.busy = 1;
91466 initData.rc = SQLITE_OK;
91467 initData.nInitRow = 0;
91468 assert( !db->mallocFailed );
91469 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
91470 if( rc==SQLITE_OK ) rc = initData.rc;
91471 if( rc==SQLITE_OK && initData.nInitRow==0 ){
91472 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
91473 ** at least one SQL statement. Any less than that indicates that
91474 ** the sqlite_schema table is corrupt. */
91475 rc = SQLITE_CORRUPT_BKPT;
91476 }
91477 sqlite3DbFreeNN(db, zSql);
91478 db->init.busy = 0;
91479 }
91480 }
91481 if( rc ){
91482 sqlite3ResetAllSchemasOfConnection(db);
91483 if( rc==SQLITE_NOMEM ){
91484 goto no_mem;
91485 }
91486 goto abort_due_to_error;
91487 }
91488 break;
91489}
91490
91491#if !defined(SQLITE_OMIT_ANALYZE)
91492/* Opcode: LoadAnalysis P1 * * * *
91493**
91494** Read the sqlite_stat1 table for database P1 and load the content
91495** of that table into the internal index hash table. This will cause
91496** the analysis to be used when preparing all subsequent queries.
91497*/
91498case OP_LoadAnalysis: {
91499 assert( pOp->p1>=0 && pOp->p1<db->nDb );
91500 rc = sqlite3AnalysisLoad(db, pOp->p1);
91501 if( rc ) goto abort_due_to_error;
91502 break;
91503}
91504#endif /* !defined(SQLITE_OMIT_ANALYZE) */
91505
91506/* Opcode: DropTable P1 * * P4 *
91507**
91508** Remove the internal (in-memory) data structures that describe
91509** the table named P4 in database P1. This is called after a table
91510** is dropped from disk (using the Destroy opcode) in order to keep
91511** the internal representation of the
91512** schema consistent with what is on disk.
91513*/
91514case OP_DropTable: {
91515 sqlite3VdbeIncrWriteCounter(p, 0);
91516 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
91517 break;
91518}
91519
91520/* Opcode: DropIndex P1 * * P4 *
91521**
91522** Remove the internal (in-memory) data structures that describe
91523** the index named P4 in database P1. This is called after an index
91524** is dropped from disk (using the Destroy opcode)
91525** in order to keep the internal representation of the
91526** schema consistent with what is on disk.
91527*/
91528case OP_DropIndex: {
91529 sqlite3VdbeIncrWriteCounter(p, 0);
91530 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
91531 break;
91532}
91533
91534/* Opcode: DropTrigger P1 * * P4 *
91535**
91536** Remove the internal (in-memory) data structures that describe
91537** the trigger named P4 in database P1. This is called after a trigger
91538** is dropped from disk (using the Destroy opcode) in order to keep
91539** the internal representation of the
91540** schema consistent with what is on disk.
91541*/
91542case OP_DropTrigger: {
91543 sqlite3VdbeIncrWriteCounter(p, 0);
91544 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
91545 break;
91546}
91547
91548
91549#ifndef SQLITE_OMIT_INTEGRITY_CHECK
91550/* Opcode: IntegrityCk P1 P2 P3 P4 P5
91551**
91552** Do an analysis of the currently open database. Store in
91553** register P1 the text of an error message describing any problems.
91554** If no problems are found, store a NULL in register P1.
91555**
91556** The register P3 contains one less than the maximum number of allowed errors.
91557** At most reg(P3) errors will be reported.
91558** In other words, the analysis stops as soon as reg(P1) errors are
91559** seen. Reg(P1) is updated with the number of errors remaining.
91560**
91561** The root page numbers of all tables in the database are integers
91562** stored in P4_INTARRAY argument.
91563**
91564** If P5 is not zero, the check is done on the auxiliary database
91565** file, not the main database file.
91566**
91567** This opcode is used to implement the integrity_check pragma.
91568*/
91569case OP_IntegrityCk: {
91570 int nRoot; /* Number of tables to check. (Number of root pages.) */
91571 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
91572 int nErr; /* Number of errors reported */
91573 char *z; /* Text of the error report */
91574 Mem *pnErr; /* Register keeping track of errors remaining */
91575
91576 assert( p->bIsReader );
91577 nRoot = pOp->p2;
91578 aRoot = pOp->p4.ai;
91579 assert( nRoot>0 );
91580 assert( aRoot[0]==(Pgno)nRoot );
91581 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91582 pnErr = &aMem[pOp->p3];
91583 assert( (pnErr->flags & MEM_Int)!=0 );
91584 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
91585 pIn1 = &aMem[pOp->p1];
91586 assert( pOp->p5<db->nDb );
91587 assert( DbMaskTest(p->btreeMask, pOp->p5) );
91588 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
91589 (int)pnErr->u.i+1, &nErr);
91590 sqlite3VdbeMemSetNull(pIn1);
91591 if( nErr==0 ){
91592 assert( z==0 );
91593 }else if( z==0 ){
91594 goto no_mem;
91595 }else{
91596 pnErr->u.i -= nErr-1;
91597 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
91598 }
91599 UPDATE_MAX_BLOBSIZE(pIn1);
91600 sqlite3VdbeChangeEncoding(pIn1, encoding);
91601 goto check_for_interrupt;
91602}
91603#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
91604
91605/* Opcode: RowSetAdd P1 P2 * * *
91606** Synopsis: rowset(P1)=r[P2]
91607**
91608** Insert the integer value held by register P2 into a RowSet object
91609** held in register P1.
91610**
91611** An assertion fails if P2 is not an integer.
91612*/
91613case OP_RowSetAdd: { /* in1, in2 */
91614 pIn1 = &aMem[pOp->p1];
91615 pIn2 = &aMem[pOp->p2];
91616 assert( (pIn2->flags & MEM_Int)!=0 );
91617 if( (pIn1->flags & MEM_Blob)==0 ){
91618 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
91619 }
91620 assert( sqlite3VdbeMemIsRowSet(pIn1) );
91621 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
91622 break;
91623}
91624
91625/* Opcode: RowSetRead P1 P2 P3 * *
91626** Synopsis: r[P3]=rowset(P1)
91627**
91628** Extract the smallest value from the RowSet object in P1
91629** and put that value into register P3.
91630** Or, if RowSet object P1 is initially empty, leave P3
91631** unchanged and jump to instruction P2.
91632*/
91633case OP_RowSetRead: { /* jump, in1, out3 */
91634 i64 val;
91635
91636 pIn1 = &aMem[pOp->p1];
91637 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
91638 if( (pIn1->flags & MEM_Blob)==0
91639 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
91640 ){
91641 /* The boolean index is empty */
91642 sqlite3VdbeMemSetNull(pIn1);
91643 VdbeBranchTaken(1,2);
91644 goto jump_to_p2_and_check_for_interrupt;
91645 }else{
91646 /* A value was pulled from the index */
91647 VdbeBranchTaken(0,2);
91648 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
91649 }
91650 goto check_for_interrupt;
91651}
91652
91653/* Opcode: RowSetTest P1 P2 P3 P4
91654** Synopsis: if r[P3] in rowset(P1) goto P2
91655**
91656** Register P3 is assumed to hold a 64-bit integer value. If register P1
91657** contains a RowSet object and that RowSet object contains
91658** the value held in P3, jump to register P2. Otherwise, insert the
91659** integer in P3 into the RowSet and continue on to the
91660** next opcode.
91661**
91662** The RowSet object is optimized for the case where sets of integers
91663** are inserted in distinct phases, which each set contains no duplicates.
91664** Each set is identified by a unique P4 value. The first set
91665** must have P4==0, the final set must have P4==-1, and for all other sets
91666** must have P4>0.
91667**
91668** This allows optimizations: (a) when P4==0 there is no need to test
91669** the RowSet object for P3, as it is guaranteed not to contain it,
91670** (b) when P4==-1 there is no need to insert the value, as it will
91671** never be tested for, and (c) when a value that is part of set X is
91672** inserted, there is no need to search to see if the same value was
91673** previously inserted as part of set X (only if it was previously
91674** inserted as part of some other set).
91675*/
91676case OP_RowSetTest: { /* jump, in1, in3 */
91677 int iSet;
91678 int exists;
91679
91680 pIn1 = &aMem[pOp->p1];
91681 pIn3 = &aMem[pOp->p3];
91682 iSet = pOp->p4.i;
91683 assert( pIn3->flags&MEM_Int );
91684
91685 /* If there is anything other than a rowset object in memory cell P1,
91686 ** delete it now and initialize P1 with an empty rowset
91687 */
91688 if( (pIn1->flags & MEM_Blob)==0 ){
91689 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
91690 }
91691 assert( sqlite3VdbeMemIsRowSet(pIn1) );
91692 assert( pOp->p4type==P4_INT32 );
91693 assert( iSet==-1 || iSet>=0 );
91694 if( iSet ){
91695 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
91696 VdbeBranchTaken(exists!=0,2);
91697 if( exists ) goto jump_to_p2;
91698 }
91699 if( iSet>=0 ){
91700 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
91701 }
91702 break;
91703}
91704
91705
91706#ifndef SQLITE_OMIT_TRIGGER
91707
91708/* Opcode: Program P1 P2 P3 P4 P5
91709**
91710** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
91711**
91712** P1 contains the address of the memory cell that contains the first memory
91713** cell in an array of values used as arguments to the sub-program. P2
91714** contains the address to jump to if the sub-program throws an IGNORE
91715** exception using the RAISE() function. Register P3 contains the address
91716** of a memory cell in this (the parent) VM that is used to allocate the
91717** memory required by the sub-vdbe at runtime.
91718**
91719** P4 is a pointer to the VM containing the trigger program.
91720**
91721** If P5 is non-zero, then recursive program invocation is enabled.
91722*/
91723case OP_Program: { /* jump */
91724 int nMem; /* Number of memory registers for sub-program */
91725 int nByte; /* Bytes of runtime space required for sub-program */
91726 Mem *pRt; /* Register to allocate runtime space */
91727 Mem *pMem; /* Used to iterate through memory cells */
91728 Mem *pEnd; /* Last memory cell in new array */
91729 VdbeFrame *pFrame; /* New vdbe frame to execute in */
91730 SubProgram *pProgram; /* Sub-program to execute */
91731 void *t; /* Token identifying trigger */
91732
91733 pProgram = pOp->p4.pProgram;
91734 pRt = &aMem[pOp->p3];
91735 assert( pProgram->nOp>0 );
91736
91737 /* If the p5 flag is clear, then recursive invocation of triggers is
91738 ** disabled for backwards compatibility (p5 is set if this sub-program
91739 ** is really a trigger, not a foreign key action, and the flag set
91740 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
91741 **
91742 ** It is recursive invocation of triggers, at the SQL level, that is
91743 ** disabled. In some cases a single trigger may generate more than one
91744 ** SubProgram (if the trigger may be executed with more than one different
91745 ** ON CONFLICT algorithm). SubProgram structures associated with a
91746 ** single trigger all have the same value for the SubProgram.token
91747 ** variable. */
91748 if( pOp->p5 ){
91749 t = pProgram->token;
91750 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
91751 if( pFrame ) break;
91752 }
91753
91754 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
91755 rc = SQLITE_ERROR;
91756 sqlite3VdbeError(p, "too many levels of trigger recursion");
91757 goto abort_due_to_error;
91758 }
91759
91760 /* Register pRt is used to store the memory required to save the state
91761 ** of the current program, and the memory required at runtime to execute
91762 ** the trigger program. If this trigger has been fired before, then pRt
91763 ** is already allocated. Otherwise, it must be initialized. */
91764 if( (pRt->flags&MEM_Blob)==0 ){
91765 /* SubProgram.nMem is set to the number of memory cells used by the
91766 ** program stored in SubProgram.aOp. As well as these, one memory
91767 ** cell is required for each cursor used by the program. Set local
91768 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
91769 */
91770 nMem = pProgram->nMem + pProgram->nCsr;
91771 assert( nMem>0 );
91772 if( pProgram->nCsr==0 ) nMem++;
91773 nByte = ROUND8(sizeof(VdbeFrame))
91774 + nMem * sizeof(Mem)
91775 + pProgram->nCsr * sizeof(VdbeCursor*)
91776 + (pProgram->nOp + 7)/8;
91777 pFrame = sqlite3DbMallocZero(db, nByte);
91778 if( !pFrame ){
91779 goto no_mem;
91780 }
91781 sqlite3VdbeMemRelease(pRt);
91782 pRt->flags = MEM_Blob|MEM_Dyn;
91783 pRt->z = (char*)pFrame;
91784 pRt->n = nByte;
91785 pRt->xDel = sqlite3VdbeFrameMemDel;
91786
91787 pFrame->v = p;
91788 pFrame->nChildMem = nMem;
91789 pFrame->nChildCsr = pProgram->nCsr;
91790 pFrame->pc = (int)(pOp - aOp);
91791 pFrame->aMem = p->aMem;
91792 pFrame->nMem = p->nMem;
91793 pFrame->apCsr = p->apCsr;
91794 pFrame->nCursor = p->nCursor;
91795 pFrame->aOp = p->aOp;
91796 pFrame->nOp = p->nOp;
91797 pFrame->token = pProgram->token;
91798#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91799 pFrame->anExec = p->anExec;
91800#endif
91801#ifdef SQLITE_DEBUG
91802 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
91803#endif
91804
91805 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
91806 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
91807 pMem->flags = MEM_Undefined;
91808 pMem->db = db;
91809 }
91810 }else{
91811 pFrame = (VdbeFrame*)pRt->z;
91812 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
91813 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
91814 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
91815 assert( pProgram->nCsr==pFrame->nChildCsr );
91816 assert( (int)(pOp - aOp)==pFrame->pc );
91817 }
91818
91819 p->nFrame++;
91820 pFrame->pParent = p->pFrame;
91821 pFrame->lastRowid = db->lastRowid;
91822 pFrame->nChange = p->nChange;
91823 pFrame->nDbChange = p->db->nChange;
91824 assert( pFrame->pAuxData==0 );
91825 pFrame->pAuxData = p->pAuxData;
91826 p->pAuxData = 0;
91827 p->nChange = 0;
91828 p->pFrame = pFrame;
91829 p->aMem = aMem = VdbeFrameMem(pFrame);
91830 p->nMem = pFrame->nChildMem;
91831 p->nCursor = (u16)pFrame->nChildCsr;
91832 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
91833 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
91834 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
91835 p->aOp = aOp = pProgram->aOp;
91836 p->nOp = pProgram->nOp;
91837#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91838 p->anExec = 0;
91839#endif
91840#ifdef SQLITE_DEBUG
91841 /* Verify that second and subsequent executions of the same trigger do not
91842 ** try to reuse register values from the first use. */
91843 {
91844 int i;
91845 for(i=0; i<p->nMem; i++){
91846 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
91847 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
91848 }
91849 }
91850#endif
91851 pOp = &aOp[-1];
91852 goto check_for_interrupt;
91853}
91854
91855/* Opcode: Param P1 P2 * * *
91856**
91857** This opcode is only ever present in sub-programs called via the
91858** OP_Program instruction. Copy a value currently stored in a memory
91859** cell of the calling (parent) frame to cell P2 in the current frames
91860** address space. This is used by trigger programs to access the new.*
91861** and old.* values.
91862**
91863** The address of the cell in the parent frame is determined by adding
91864** the value of the P1 argument to the value of the P1 argument to the
91865** calling OP_Program instruction.
91866*/
91867case OP_Param: { /* out2 */
91868 VdbeFrame *pFrame;
91869 Mem *pIn;
91870 pOut = out2Prerelease(p, pOp);
91871 pFrame = p->pFrame;
91872 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
91873 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
91874 break;
91875}
91876
91877#endif /* #ifndef SQLITE_OMIT_TRIGGER */
91878
91879#ifndef SQLITE_OMIT_FOREIGN_KEY
91880/* Opcode: FkCounter P1 P2 * * *
91881** Synopsis: fkctr[P1]+=P2
91882**
91883** Increment a "constraint counter" by P2 (P2 may be negative or positive).
91884** If P1 is non-zero, the database constraint counter is incremented
91885** (deferred foreign key constraints). Otherwise, if P1 is zero, the
91886** statement counter is incremented (immediate foreign key constraints).
91887*/
91888case OP_FkCounter: {
91889 if( db->flags & SQLITE_DeferFKs ){
91890 db->nDeferredImmCons += pOp->p2;
91891 }else if( pOp->p1 ){
91892 db->nDeferredCons += pOp->p2;
91893 }else{
91894 p->nFkConstraint += pOp->p2;
91895 }
91896 break;
91897}
91898
91899/* Opcode: FkIfZero P1 P2 * * *
91900** Synopsis: if fkctr[P1]==0 goto P2
91901**
91902** This opcode tests if a foreign key constraint-counter is currently zero.
91903** If so, jump to instruction P2. Otherwise, fall through to the next
91904** instruction.
91905**
91906** If P1 is non-zero, then the jump is taken if the database constraint-counter
91907** is zero (the one that counts deferred constraint violations). If P1 is
91908** zero, the jump is taken if the statement constraint-counter is zero
91909** (immediate foreign key constraint violations).
91910*/
91911case OP_FkIfZero: { /* jump */
91912 if( pOp->p1 ){
91913 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
91914 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
91915 }else{
91916 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
91917 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
91918 }
91919 break;
91920}
91921#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
91922
91923#ifndef SQLITE_OMIT_AUTOINCREMENT
91924/* Opcode: MemMax P1 P2 * * *
91925** Synopsis: r[P1]=max(r[P1],r[P2])
91926**
91927** P1 is a register in the root frame of this VM (the root frame is
91928** different from the current frame if this instruction is being executed
91929** within a sub-program). Set the value of register P1 to the maximum of
91930** its current value and the value in register P2.
91931**
91932** This instruction throws an error if the memory cell is not initially
91933** an integer.
91934*/
91935case OP_MemMax: { /* in2 */
91936 VdbeFrame *pFrame;
91937 if( p->pFrame ){
91938 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
91939 pIn1 = &pFrame->aMem[pOp->p1];
91940 }else{
91941 pIn1 = &aMem[pOp->p1];
91942 }
91943 assert( memIsValid(pIn1) );
91944 sqlite3VdbeMemIntegerify(pIn1);
91945 pIn2 = &aMem[pOp->p2];
91946 sqlite3VdbeMemIntegerify(pIn2);
91947 if( pIn1->u.i<pIn2->u.i){
91948 pIn1->u.i = pIn2->u.i;
91949 }
91950 break;
91951}
91952#endif /* SQLITE_OMIT_AUTOINCREMENT */
91953
91954/* Opcode: IfPos P1 P2 P3 * *
91955** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
91956**
91957** Register P1 must contain an integer.
91958** If the value of register P1 is 1 or greater, subtract P3 from the
91959** value in P1 and jump to P2.
91960**
91961** If the initial value of register P1 is less than 1, then the
91962** value is unchanged and control passes through to the next instruction.
91963*/
91964case OP_IfPos: { /* jump, in1 */
91965 pIn1 = &aMem[pOp->p1];
91966 assert( pIn1->flags&MEM_Int );
91967 VdbeBranchTaken( pIn1->u.i>0, 2);
91968 if( pIn1->u.i>0 ){
91969 pIn1->u.i -= pOp->p3;
91970 goto jump_to_p2;
91971 }
91972 break;
91973}
91974
91975/* Opcode: OffsetLimit P1 P2 P3 * *
91976** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
91977**
91978** This opcode performs a commonly used computation associated with
91979** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
91980** holds the offset counter. The opcode computes the combined value
91981** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
91982** value computed is the total number of rows that will need to be
91983** visited in order to complete the query.
91984**
91985** If r[P3] is zero or negative, that means there is no OFFSET
91986** and r[P2] is set to be the value of the LIMIT, r[P1].
91987**
91988** if r[P1] is zero or negative, that means there is no LIMIT
91989** and r[P2] is set to -1.
91990**
91991** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
91992*/
91993case OP_OffsetLimit: { /* in1, out2, in3 */
91994 i64 x;
91995 pIn1 = &aMem[pOp->p1];
91996 pIn3 = &aMem[pOp->p3];
91997 pOut = out2Prerelease(p, pOp);
91998 assert( pIn1->flags & MEM_Int );
91999 assert( pIn3->flags & MEM_Int );
92000 x = pIn1->u.i;
92001 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
92002 /* If the LIMIT is less than or equal to zero, loop forever. This
92003 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
92004 ** also loop forever. This is undocumented. In fact, one could argue
92005 ** that the loop should terminate. But assuming 1 billion iterations
92006 ** per second (far exceeding the capabilities of any current hardware)
92007 ** it would take nearly 300 years to actually reach the limit. So
92008 ** looping forever is a reasonable approximation. */
92009 pOut->u.i = -1;
92010 }else{
92011 pOut->u.i = x;
92012 }
92013 break;
92014}
92015
92016/* Opcode: IfNotZero P1 P2 * * *
92017** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
92018**
92019** Register P1 must contain an integer. If the content of register P1 is
92020** initially greater than zero, then decrement the value in register P1.
92021** If it is non-zero (negative or positive) and then also jump to P2.
92022** If register P1 is initially zero, leave it unchanged and fall through.
92023*/
92024case OP_IfNotZero: { /* jump, in1 */
92025 pIn1 = &aMem[pOp->p1];
92026 assert( pIn1->flags&MEM_Int );
92027 VdbeBranchTaken(pIn1->u.i<0, 2);
92028 if( pIn1->u.i ){
92029 if( pIn1->u.i>0 ) pIn1->u.i--;
92030 goto jump_to_p2;
92031 }
92032 break;
92033}
92034
92035/* Opcode: DecrJumpZero P1 P2 * * *
92036** Synopsis: if (--r[P1])==0 goto P2
92037**
92038** Register P1 must hold an integer. Decrement the value in P1
92039** and jump to P2 if the new value is exactly zero.
92040*/
92041case OP_DecrJumpZero: { /* jump, in1 */
92042 pIn1 = &aMem[pOp->p1];
92043 assert( pIn1->flags&MEM_Int );
92044 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
92045 VdbeBranchTaken(pIn1->u.i==0, 2);
92046 if( pIn1->u.i==0 ) goto jump_to_p2;
92047 break;
92048}
92049
92050
92051/* Opcode: AggStep * P2 P3 P4 P5
92052** Synopsis: accum=r[P3] step(r[P2@P5])
92053**
92054** Execute the xStep function for an aggregate.
92055** The function has P5 arguments. P4 is a pointer to the
92056** FuncDef structure that specifies the function. Register P3 is the
92057** accumulator.
92058**
92059** The P5 arguments are taken from register P2 and its
92060** successors.
92061*/
92062/* Opcode: AggInverse * P2 P3 P4 P5
92063** Synopsis: accum=r[P3] inverse(r[P2@P5])
92064**
92065** Execute the xInverse function for an aggregate.
92066** The function has P5 arguments. P4 is a pointer to the
92067** FuncDef structure that specifies the function. Register P3 is the
92068** accumulator.
92069**
92070** The P5 arguments are taken from register P2 and its
92071** successors.
92072*/
92073/* Opcode: AggStep1 P1 P2 P3 P4 P5
92074** Synopsis: accum=r[P3] step(r[P2@P5])
92075**
92076** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
92077** aggregate. The function has P5 arguments. P4 is a pointer to the
92078** FuncDef structure that specifies the function. Register P3 is the
92079** accumulator.
92080**
92081** The P5 arguments are taken from register P2 and its
92082** successors.
92083**
92084** This opcode is initially coded as OP_AggStep0. On first evaluation,
92085** the FuncDef stored in P4 is converted into an sqlite3_context and
92086** the opcode is changed. In this way, the initialization of the
92087** sqlite3_context only happens once, instead of on each call to the
92088** step function.
92089*/
92090case OP_AggInverse:
92091case OP_AggStep: {
92092 int n;
92093 sqlite3_context *pCtx;
92094
92095 assert( pOp->p4type==P4_FUNCDEF );
92096 n = pOp->p5;
92097 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
92098 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
92099 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
92100 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
92101 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
92102 if( pCtx==0 ) goto no_mem;
92103 pCtx->pMem = 0;
92104 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
92105 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
92106 pCtx->pFunc = pOp->p4.pFunc;
92107 pCtx->iOp = (int)(pOp - aOp);
92108 pCtx->pVdbe = p;
92109 pCtx->skipFlag = 0;
92110 pCtx->isError = 0;
92111 pCtx->argc = n;
92112 pOp->p4type = P4_FUNCCTX;
92113 pOp->p4.pCtx = pCtx;
92114
92115 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
92116 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
92117
92118 pOp->opcode = OP_AggStep1;
92119 /* Fall through into OP_AggStep */
92120 /* no break */ deliberate_fall_through
92121}
92122case OP_AggStep1: {
92123 int i;
92124 sqlite3_context *pCtx;
92125 Mem *pMem;
92126
92127 assert( pOp->p4type==P4_FUNCCTX );
92128 pCtx = pOp->p4.pCtx;
92129 pMem = &aMem[pOp->p3];
92130
92131#ifdef SQLITE_DEBUG
92132 if( pOp->p1 ){
92133 /* This is an OP_AggInverse call. Verify that xStep has always
92134 ** been called at least once prior to any xInverse call. */
92135 assert( pMem->uTemp==0x1122e0e3 );
92136 }else{
92137 /* This is an OP_AggStep call. Mark it as such. */
92138 pMem->uTemp = 0x1122e0e3;
92139 }
92140#endif
92141
92142 /* If this function is inside of a trigger, the register array in aMem[]
92143 ** might change from one evaluation to the next. The next block of code
92144 ** checks to see if the register array has changed, and if so it
92145 ** reinitializes the relavant parts of the sqlite3_context object */
92146 if( pCtx->pMem != pMem ){
92147 pCtx->pMem = pMem;
92148 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
92149 }
92150
92151#ifdef SQLITE_DEBUG
92152 for(i=0; i<pCtx->argc; i++){
92153 assert( memIsValid(pCtx->argv[i]) );
92154 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
92155 }
92156#endif
92157
92158 pMem->n++;
92159 assert( pCtx->pOut->flags==MEM_Null );
92160 assert( pCtx->isError==0 );
92161 assert( pCtx->skipFlag==0 );
92162#ifndef SQLITE_OMIT_WINDOWFUNC
92163 if( pOp->p1 ){
92164 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
92165 }else
92166#endif
92167 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
92168
92169 if( pCtx->isError ){
92170 if( pCtx->isError>0 ){
92171 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
92172 rc = pCtx->isError;
92173 }
92174 if( pCtx->skipFlag ){
92175 assert( pOp[-1].opcode==OP_CollSeq );
92176 i = pOp[-1].p1;
92177 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
92178 pCtx->skipFlag = 0;
92179 }
92180 sqlite3VdbeMemRelease(pCtx->pOut);
92181 pCtx->pOut->flags = MEM_Null;
92182 pCtx->isError = 0;
92183 if( rc ) goto abort_due_to_error;
92184 }
92185 assert( pCtx->pOut->flags==MEM_Null );
92186 assert( pCtx->skipFlag==0 );
92187 break;
92188}
92189
92190/* Opcode: AggFinal P1 P2 * P4 *
92191** Synopsis: accum=r[P1] N=P2
92192**
92193** P1 is the memory location that is the accumulator for an aggregate
92194** or window function. Execute the finalizer function
92195** for an aggregate and store the result in P1.
92196**
92197** P2 is the number of arguments that the step function takes and
92198** P4 is a pointer to the FuncDef for this function. The P2
92199** argument is not used by this opcode. It is only there to disambiguate
92200** functions that can take varying numbers of arguments. The
92201** P4 argument is only needed for the case where
92202** the step function was not previously called.
92203*/
92204/* Opcode: AggValue * P2 P3 P4 *
92205** Synopsis: r[P3]=value N=P2
92206**
92207** Invoke the xValue() function and store the result in register P3.
92208**
92209** P2 is the number of arguments that the step function takes and
92210** P4 is a pointer to the FuncDef for this function. The P2
92211** argument is not used by this opcode. It is only there to disambiguate
92212** functions that can take varying numbers of arguments. The
92213** P4 argument is only needed for the case where
92214** the step function was not previously called.
92215*/
92216case OP_AggValue:
92217case OP_AggFinal: {
92218 Mem *pMem;
92219 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92220 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
92221 pMem = &aMem[pOp->p1];
92222 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
92223#ifndef SQLITE_OMIT_WINDOWFUNC
92224 if( pOp->p3 ){
92225 memAboutToChange(p, &aMem[pOp->p3]);
92226 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
92227 pMem = &aMem[pOp->p3];
92228 }else
92229#endif
92230 {
92231 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
92232 }
92233
92234 if( rc ){
92235 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
92236 goto abort_due_to_error;
92237 }
92238 sqlite3VdbeChangeEncoding(pMem, encoding);
92239 UPDATE_MAX_BLOBSIZE(pMem);
92240 if( sqlite3VdbeMemTooBig(pMem) ){
92241 goto too_big;
92242 }
92243 break;
92244}
92245
92246#ifndef SQLITE_OMIT_WAL
92247/* Opcode: Checkpoint P1 P2 P3 * *
92248**
92249** Checkpoint database P1. This is a no-op if P1 is not currently in
92250** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
92251** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
92252** SQLITE_BUSY or not, respectively. Write the number of pages in the
92253** WAL after the checkpoint into mem[P3+1] and the number of pages
92254** in the WAL that have been checkpointed after the checkpoint
92255** completes into mem[P3+2]. However on an error, mem[P3+1] and
92256** mem[P3+2] are initialized to -1.
92257*/
92258case OP_Checkpoint: {
92259 int i; /* Loop counter */
92260 int aRes[3]; /* Results */
92261 Mem *pMem; /* Write results here */
92262
92263 assert( p->readOnly==0 );
92264 aRes[0] = 0;
92265 aRes[1] = aRes[2] = -1;
92266 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
92267 || pOp->p2==SQLITE_CHECKPOINT_FULL
92268 || pOp->p2==SQLITE_CHECKPOINT_RESTART
92269 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
92270 );
92271 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
92272 if( rc ){
92273 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
92274 rc = SQLITE_OK;
92275 aRes[0] = 1;
92276 }
92277 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
92278 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
92279 }
92280 break;
92281};
92282#endif
92283
92284#ifndef SQLITE_OMIT_PRAGMA
92285/* Opcode: JournalMode P1 P2 P3 * *
92286**
92287** Change the journal mode of database P1 to P3. P3 must be one of the
92288** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
92289** modes (delete, truncate, persist, off and memory), this is a simple
92290** operation. No IO is required.
92291**
92292** If changing into or out of WAL mode the procedure is more complicated.
92293**
92294** Write a string containing the final journal-mode to register P2.
92295*/
92296case OP_JournalMode: { /* out2 */
92297 Btree *pBt; /* Btree to change journal mode of */
92298 Pager *pPager; /* Pager associated with pBt */
92299 int eNew; /* New journal mode */
92300 int eOld; /* The old journal mode */
92301#ifndef SQLITE_OMIT_WAL
92302 const char *zFilename; /* Name of database file for pPager */
92303#endif
92304
92305 pOut = out2Prerelease(p, pOp);
92306 eNew = pOp->p3;
92307 assert( eNew==PAGER_JOURNALMODE_DELETE
92308 || eNew==PAGER_JOURNALMODE_TRUNCATE
92309 || eNew==PAGER_JOURNALMODE_PERSIST
92310 || eNew==PAGER_JOURNALMODE_OFF
92311 || eNew==PAGER_JOURNALMODE_MEMORY
92312 || eNew==PAGER_JOURNALMODE_WAL
92313 || eNew==PAGER_JOURNALMODE_QUERY
92314 );
92315 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92316 assert( p->readOnly==0 );
92317
92318 pBt = db->aDb[pOp->p1].pBt;
92319 pPager = sqlite3BtreePager(pBt);
92320 eOld = sqlite3PagerGetJournalMode(pPager);
92321 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
92322 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
92323
92324#ifndef SQLITE_OMIT_WAL
92325 zFilename = sqlite3PagerFilename(pPager, 1);
92326
92327 /* Do not allow a transition to journal_mode=WAL for a database
92328 ** in temporary storage or if the VFS does not support shared memory
92329 */
92330 if( eNew==PAGER_JOURNALMODE_WAL
92331 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
92332 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
92333 ){
92334 eNew = eOld;
92335 }
92336
92337 if( (eNew!=eOld)
92338 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
92339 ){
92340 if( !db->autoCommit || db->nVdbeRead>1 ){
92341 rc = SQLITE_ERROR;
92342 sqlite3VdbeError(p,
92343 "cannot change %s wal mode from within a transaction",
92344 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
92345 );
92346 goto abort_due_to_error;
92347 }else{
92348
92349 if( eOld==PAGER_JOURNALMODE_WAL ){
92350 /* If leaving WAL mode, close the log file. If successful, the call
92351 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
92352 ** file. An EXCLUSIVE lock may still be held on the database file
92353 ** after a successful return.
92354 */
92355 rc = sqlite3PagerCloseWal(pPager, db);
92356 if( rc==SQLITE_OK ){
92357 sqlite3PagerSetJournalMode(pPager, eNew);
92358 }
92359 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
92360 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
92361 ** as an intermediate */
92362 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
92363 }
92364
92365 /* Open a transaction on the database file. Regardless of the journal
92366 ** mode, this transaction always uses a rollback journal.
92367 */
92368 assert( sqlite3BtreeIsInTrans(pBt)==0 );
92369 if( rc==SQLITE_OK ){
92370 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
92371 }
92372 }
92373 }
92374#endif /* ifndef SQLITE_OMIT_WAL */
92375
92376 if( rc ) eNew = eOld;
92377 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
92378
92379 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
92380 pOut->z = (char *)sqlite3JournalModename(eNew);
92381 pOut->n = sqlite3Strlen30(pOut->z);
92382 pOut->enc = SQLITE_UTF8;
92383 sqlite3VdbeChangeEncoding(pOut, encoding);
92384 if( rc ) goto abort_due_to_error;
92385 break;
92386};
92387#endif /* SQLITE_OMIT_PRAGMA */
92388
92389#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
92390/* Opcode: Vacuum P1 P2 * * *
92391**
92392** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
92393** for an attached database. The "temp" database may not be vacuumed.
92394**
92395** If P2 is not zero, then it is a register holding a string which is
92396** the file into which the result of vacuum should be written. When
92397** P2 is zero, the vacuum overwrites the original database.
92398*/
92399case OP_Vacuum: {
92400 assert( p->readOnly==0 );
92401 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
92402 pOp->p2 ? &aMem[pOp->p2] : 0);
92403 if( rc ) goto abort_due_to_error;
92404 break;
92405}
92406#endif
92407
92408#if !defined(SQLITE_OMIT_AUTOVACUUM)
92409/* Opcode: IncrVacuum P1 P2 * * *
92410**
92411** Perform a single step of the incremental vacuum procedure on
92412** the P1 database. If the vacuum has finished, jump to instruction
92413** P2. Otherwise, fall through to the next instruction.
92414*/
92415case OP_IncrVacuum: { /* jump */
92416 Btree *pBt;
92417
92418 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92419 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92420 assert( p->readOnly==0 );
92421 pBt = db->aDb[pOp->p1].pBt;
92422 rc = sqlite3BtreeIncrVacuum(pBt);
92423 VdbeBranchTaken(rc==SQLITE_DONE,2);
92424 if( rc ){
92425 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
92426 rc = SQLITE_OK;
92427 goto jump_to_p2;
92428 }
92429 break;
92430}
92431#endif
92432
92433/* Opcode: Expire P1 P2 * * *
92434**
92435** Cause precompiled statements to expire. When an expired statement
92436** is executed using sqlite3_step() it will either automatically
92437** reprepare itself (if it was originally created using sqlite3_prepare_v2())
92438** or it will fail with SQLITE_SCHEMA.
92439**
92440** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
92441** then only the currently executing statement is expired.
92442**
92443** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
92444** then running SQL statements are allowed to continue to run to completion.
92445** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
92446** that might help the statement run faster but which does not affect the
92447** correctness of operation.
92448*/
92449case OP_Expire: {
92450 assert( pOp->p2==0 || pOp->p2==1 );
92451 if( !pOp->p1 ){
92452 sqlite3ExpirePreparedStatements(db, pOp->p2);
92453 }else{
92454 p->expired = pOp->p2+1;
92455 }
92456 break;
92457}
92458
92459/* Opcode: CursorLock P1 * * * *
92460**
92461** Lock the btree to which cursor P1 is pointing so that the btree cannot be
92462** written by an other cursor.
92463*/
92464case OP_CursorLock: {
92465 VdbeCursor *pC;
92466 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92467 pC = p->apCsr[pOp->p1];
92468 assert( pC!=0 );
92469 assert( pC->eCurType==CURTYPE_BTREE );
92470 sqlite3BtreeCursorPin(pC->uc.pCursor);
92471 break;
92472}
92473
92474/* Opcode: CursorUnlock P1 * * * *
92475**
92476** Unlock the btree to which cursor P1 is pointing so that it can be
92477** written by other cursors.
92478*/
92479case OP_CursorUnlock: {
92480 VdbeCursor *pC;
92481 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92482 pC = p->apCsr[pOp->p1];
92483 assert( pC!=0 );
92484 assert( pC->eCurType==CURTYPE_BTREE );
92485 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
92486 break;
92487}
92488
92489#ifndef SQLITE_OMIT_SHARED_CACHE
92490/* Opcode: TableLock P1 P2 P3 P4 *
92491** Synopsis: iDb=P1 root=P2 write=P3
92492**
92493** Obtain a lock on a particular table. This instruction is only used when
92494** the shared-cache feature is enabled.
92495**
92496** P1 is the index of the database in sqlite3.aDb[] of the database
92497** on which the lock is acquired. A readlock is obtained if P3==0 or
92498** a write lock if P3==1.
92499**
92500** P2 contains the root-page of the table to lock.
92501**
92502** P4 contains a pointer to the name of the table being locked. This is only
92503** used to generate an error message if the lock cannot be obtained.
92504*/
92505case OP_TableLock: {
92506 u8 isWriteLock = (u8)pOp->p3;
92507 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
92508 int p1 = pOp->p1;
92509 assert( p1>=0 && p1<db->nDb );
92510 assert( DbMaskTest(p->btreeMask, p1) );
92511 assert( isWriteLock==0 || isWriteLock==1 );
92512 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
92513 if( rc ){
92514 if( (rc&0xFF)==SQLITE_LOCKED ){
92515 const char *z = pOp->p4.z;
92516 sqlite3VdbeError(p, "database table is locked: %s", z);
92517 }
92518 goto abort_due_to_error;
92519 }
92520 }
92521 break;
92522}
92523#endif /* SQLITE_OMIT_SHARED_CACHE */
92524
92525#ifndef SQLITE_OMIT_VIRTUALTABLE
92526/* Opcode: VBegin * * * P4 *
92527**
92528** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
92529** xBegin method for that table.
92530**
92531** Also, whether or not P4 is set, check that this is not being called from
92532** within a callback to a virtual table xSync() method. If it is, the error
92533** code will be set to SQLITE_LOCKED.
92534*/
92535case OP_VBegin: {
92536 VTable *pVTab;
92537 pVTab = pOp->p4.pVtab;
92538 rc = sqlite3VtabBegin(db, pVTab);
92539 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
92540 if( rc ) goto abort_due_to_error;
92541 break;
92542}
92543#endif /* SQLITE_OMIT_VIRTUALTABLE */
92544
92545#ifndef SQLITE_OMIT_VIRTUALTABLE
92546/* Opcode: VCreate P1 P2 * * *
92547**
92548** P2 is a register that holds the name of a virtual table in database
92549** P1. Call the xCreate method for that table.
92550*/
92551case OP_VCreate: {
92552 Mem sMem; /* For storing the record being decoded */
92553 const char *zTab; /* Name of the virtual table */
92554
92555 memset(&sMem, 0, sizeof(sMem));
92556 sMem.db = db;
92557 /* Because P2 is always a static string, it is impossible for the
92558 ** sqlite3VdbeMemCopy() to fail */
92559 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
92560 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
92561 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
92562 assert( rc==SQLITE_OK );
92563 zTab = (const char*)sqlite3_value_text(&sMem);
92564 assert( zTab || db->mallocFailed );
92565 if( zTab ){
92566 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
92567 }
92568 sqlite3VdbeMemRelease(&sMem);
92569 if( rc ) goto abort_due_to_error;
92570 break;
92571}
92572#endif /* SQLITE_OMIT_VIRTUALTABLE */
92573
92574#ifndef SQLITE_OMIT_VIRTUALTABLE
92575/* Opcode: VDestroy P1 * * P4 *
92576**
92577** P4 is the name of a virtual table in database P1. Call the xDestroy method
92578** of that table.
92579*/
92580case OP_VDestroy: {
92581 db->nVDestroy++;
92582 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
92583 db->nVDestroy--;
92584 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
92585 if( rc ) goto abort_due_to_error;
92586 break;
92587}
92588#endif /* SQLITE_OMIT_VIRTUALTABLE */
92589
92590#ifndef SQLITE_OMIT_VIRTUALTABLE
92591/* Opcode: VOpen P1 * * P4 *
92592**
92593** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92594** P1 is a cursor number. This opcode opens a cursor to the virtual
92595** table and stores that cursor in P1.
92596*/
92597case OP_VOpen: {
92598 VdbeCursor *pCur;
92599 sqlite3_vtab_cursor *pVCur;
92600 sqlite3_vtab *pVtab;
92601 const sqlite3_module *pModule;
92602
92603 assert( p->bIsReader );
92604 pCur = 0;
92605 pVCur = 0;
92606 pVtab = pOp->p4.pVtab->pVtab;
92607 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
92608 rc = SQLITE_LOCKED;
92609 goto abort_due_to_error;
92610 }
92611 pModule = pVtab->pModule;
92612 rc = pModule->xOpen(pVtab, &pVCur);
92613 sqlite3VtabImportErrmsg(p, pVtab);
92614 if( rc ) goto abort_due_to_error;
92615
92616 /* Initialize sqlite3_vtab_cursor base class */
92617 pVCur->pVtab = pVtab;
92618
92619 /* Initialize vdbe cursor object */
92620 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
92621 if( pCur ){
92622 pCur->uc.pVCur = pVCur;
92623 pVtab->nRef++;
92624 }else{
92625 assert( db->mallocFailed );
92626 pModule->xClose(pVCur);
92627 goto no_mem;
92628 }
92629 break;
92630}
92631#endif /* SQLITE_OMIT_VIRTUALTABLE */
92632
92633#ifndef SQLITE_OMIT_VIRTUALTABLE
92634/* Opcode: VFilter P1 P2 P3 P4 *
92635** Synopsis: iplan=r[P3] zplan='P4'
92636**
92637** P1 is a cursor opened using VOpen. P2 is an address to jump to if
92638** the filtered result set is empty.
92639**
92640** P4 is either NULL or a string that was generated by the xBestIndex
92641** method of the module. The interpretation of the P4 string is left
92642** to the module implementation.
92643**
92644** This opcode invokes the xFilter method on the virtual table specified
92645** by P1. The integer query plan parameter to xFilter is stored in register
92646** P3. Register P3+1 stores the argc parameter to be passed to the
92647** xFilter method. Registers P3+2..P3+1+argc are the argc
92648** additional parameters which are passed to
92649** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
92650**
92651** A jump is made to P2 if the result set after filtering would be empty.
92652*/
92653case OP_VFilter: { /* jump */
92654 int nArg;
92655 int iQuery;
92656 const sqlite3_module *pModule;
92657 Mem *pQuery;
92658 Mem *pArgc;
92659 sqlite3_vtab_cursor *pVCur;
92660 sqlite3_vtab *pVtab;
92661 VdbeCursor *pCur;
92662 int res;
92663 int i;
92664 Mem **apArg;
92665
92666 pQuery = &aMem[pOp->p3];
92667 pArgc = &pQuery[1];
92668 pCur = p->apCsr[pOp->p1];
92669 assert( memIsValid(pQuery) );
92670 REGISTER_TRACE(pOp->p3, pQuery);
92671 assert( pCur->eCurType==CURTYPE_VTAB );
92672 pVCur = pCur->uc.pVCur;
92673 pVtab = pVCur->pVtab;
92674 pModule = pVtab->pModule;
92675
92676 /* Grab the index number and argc parameters */
92677 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
92678 nArg = (int)pArgc->u.i;
92679 iQuery = (int)pQuery->u.i;
92680
92681 /* Invoke the xFilter method */
92682 res = 0;
92683 apArg = p->apArg;
92684 for(i = 0; i<nArg; i++){
92685 apArg[i] = &pArgc[i+1];
92686 }
92687 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
92688 sqlite3VtabImportErrmsg(p, pVtab);
92689 if( rc ) goto abort_due_to_error;
92690 res = pModule->xEof(pVCur);
92691 pCur->nullRow = 0;
92692 VdbeBranchTaken(res!=0,2);
92693 if( res ) goto jump_to_p2;
92694 break;
92695}
92696#endif /* SQLITE_OMIT_VIRTUALTABLE */
92697
92698#ifndef SQLITE_OMIT_VIRTUALTABLE
92699/* Opcode: VColumn P1 P2 P3 * P5
92700** Synopsis: r[P3]=vcolumn(P2)
92701**
92702** Store in register P3 the value of the P2-th column of
92703** the current row of the virtual-table of cursor P1.
92704**
92705** If the VColumn opcode is being used to fetch the value of
92706** an unchanging column during an UPDATE operation, then the P5
92707** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
92708** function to return true inside the xColumn method of the virtual
92709** table implementation. The P5 column might also contain other
92710** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
92711** unused by OP_VColumn.
92712*/
92713case OP_VColumn: {
92714 sqlite3_vtab *pVtab;
92715 const sqlite3_module *pModule;
92716 Mem *pDest;
92717 sqlite3_context sContext;
92718
92719 VdbeCursor *pCur = p->apCsr[pOp->p1];
92720 assert( pCur->eCurType==CURTYPE_VTAB );
92721 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
92722 pDest = &aMem[pOp->p3];
92723 memAboutToChange(p, pDest);
92724 if( pCur->nullRow ){
92725 sqlite3VdbeMemSetNull(pDest);
92726 break;
92727 }
92728 pVtab = pCur->uc.pVCur->pVtab;
92729 pModule = pVtab->pModule;
92730 assert( pModule->xColumn );
92731 memset(&sContext, 0, sizeof(sContext));
92732 sContext.pOut = pDest;
92733 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
92734 if( pOp->p5 & OPFLAG_NOCHNG ){
92735 sqlite3VdbeMemSetNull(pDest);
92736 pDest->flags = MEM_Null|MEM_Zero;
92737 pDest->u.nZero = 0;
92738 }else{
92739 MemSetTypeFlag(pDest, MEM_Null);
92740 }
92741 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
92742 sqlite3VtabImportErrmsg(p, pVtab);
92743 if( sContext.isError>0 ){
92744 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
92745 rc = sContext.isError;
92746 }
92747 sqlite3VdbeChangeEncoding(pDest, encoding);
92748 REGISTER_TRACE(pOp->p3, pDest);
92749 UPDATE_MAX_BLOBSIZE(pDest);
92750
92751 if( sqlite3VdbeMemTooBig(pDest) ){
92752 goto too_big;
92753 }
92754 if( rc ) goto abort_due_to_error;
92755 break;
92756}
92757#endif /* SQLITE_OMIT_VIRTUALTABLE */
92758
92759#ifndef SQLITE_OMIT_VIRTUALTABLE
92760/* Opcode: VNext P1 P2 * * *
92761**
92762** Advance virtual table P1 to the next row in its result set and
92763** jump to instruction P2. Or, if the virtual table has reached
92764** the end of its result set, then fall through to the next instruction.
92765*/
92766case OP_VNext: { /* jump */
92767 sqlite3_vtab *pVtab;
92768 const sqlite3_module *pModule;
92769 int res;
92770 VdbeCursor *pCur;
92771
92772 res = 0;
92773 pCur = p->apCsr[pOp->p1];
92774 assert( pCur->eCurType==CURTYPE_VTAB );
92775 if( pCur->nullRow ){
92776 break;
92777 }
92778 pVtab = pCur->uc.pVCur->pVtab;
92779 pModule = pVtab->pModule;
92780 assert( pModule->xNext );
92781
92782 /* Invoke the xNext() method of the module. There is no way for the
92783 ** underlying implementation to return an error if one occurs during
92784 ** xNext(). Instead, if an error occurs, true is returned (indicating that
92785 ** data is available) and the error code returned when xColumn or
92786 ** some other method is next invoked on the save virtual table cursor.
92787 */
92788 rc = pModule->xNext(pCur->uc.pVCur);
92789 sqlite3VtabImportErrmsg(p, pVtab);
92790 if( rc ) goto abort_due_to_error;
92791 res = pModule->xEof(pCur->uc.pVCur);
92792 VdbeBranchTaken(!res,2);
92793 if( !res ){
92794 /* If there is data, jump to P2 */
92795 goto jump_to_p2_and_check_for_interrupt;
92796 }
92797 goto check_for_interrupt;
92798}
92799#endif /* SQLITE_OMIT_VIRTUALTABLE */
92800
92801#ifndef SQLITE_OMIT_VIRTUALTABLE
92802/* Opcode: VRename P1 * * P4 *
92803**
92804** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92805** This opcode invokes the corresponding xRename method. The value
92806** in register P1 is passed as the zName argument to the xRename method.
92807*/
92808case OP_VRename: {
92809 sqlite3_vtab *pVtab;
92810 Mem *pName;
92811 int isLegacy;
92812
92813 isLegacy = (db->flags & SQLITE_LegacyAlter);
92814 db->flags |= SQLITE_LegacyAlter;
92815 pVtab = pOp->p4.pVtab->pVtab;
92816 pName = &aMem[pOp->p1];
92817 assert( pVtab->pModule->xRename );
92818 assert( memIsValid(pName) );
92819 assert( p->readOnly==0 );
92820 REGISTER_TRACE(pOp->p1, pName);
92821 assert( pName->flags & MEM_Str );
92822 testcase( pName->enc==SQLITE_UTF8 );
92823 testcase( pName->enc==SQLITE_UTF16BE );
92824 testcase( pName->enc==SQLITE_UTF16LE );
92825 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
92826 if( rc ) goto abort_due_to_error;
92827 rc = pVtab->pModule->xRename(pVtab, pName->z);
92828 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
92829 sqlite3VtabImportErrmsg(p, pVtab);
92830 p->expired = 0;
92831 if( rc ) goto abort_due_to_error;
92832 break;
92833}
92834#endif
92835
92836#ifndef SQLITE_OMIT_VIRTUALTABLE
92837/* Opcode: VUpdate P1 P2 P3 P4 P5
92838** Synopsis: data=r[P3@P2]
92839**
92840** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
92841** This opcode invokes the corresponding xUpdate method. P2 values
92842** are contiguous memory cells starting at P3 to pass to the xUpdate
92843** invocation. The value in register (P3+P2-1) corresponds to the
92844** p2th element of the argv array passed to xUpdate.
92845**
92846** The xUpdate method will do a DELETE or an INSERT or both.
92847** The argv[0] element (which corresponds to memory cell P3)
92848** is the rowid of a row to delete. If argv[0] is NULL then no
92849** deletion occurs. The argv[1] element is the rowid of the new
92850** row. This can be NULL to have the virtual table select the new
92851** rowid for itself. The subsequent elements in the array are
92852** the values of columns in the new row.
92853**
92854** If P2==1 then no insert is performed. argv[0] is the rowid of
92855** a row to delete.
92856**
92857** P1 is a boolean flag. If it is set to true and the xUpdate call
92858** is successful, then the value returned by sqlite3_last_insert_rowid()
92859** is set to the value of the rowid for the row just inserted.
92860**
92861** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
92862** apply in the case of a constraint failure on an insert or update.
92863*/
92864case OP_VUpdate: {
92865 sqlite3_vtab *pVtab;
92866 const sqlite3_module *pModule;
92867 int nArg;
92868 int i;
92869 sqlite_int64 rowid;
92870 Mem **apArg;
92871 Mem *pX;
92872
92873 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
92874 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
92875 );
92876 assert( p->readOnly==0 );
92877 if( db->mallocFailed ) goto no_mem;
92878 sqlite3VdbeIncrWriteCounter(p, 0);
92879 pVtab = pOp->p4.pVtab->pVtab;
92880 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
92881 rc = SQLITE_LOCKED;
92882 goto abort_due_to_error;
92883 }
92884 pModule = pVtab->pModule;
92885 nArg = pOp->p2;
92886 assert( pOp->p4type==P4_VTAB );
92887 if( ALWAYS(pModule->xUpdate) ){
92888 u8 vtabOnConflict = db->vtabOnConflict;
92889 apArg = p->apArg;
92890 pX = &aMem[pOp->p3];
92891 for(i=0; i<nArg; i++){
92892 assert( memIsValid(pX) );
92893 memAboutToChange(p, pX);
92894 apArg[i] = pX;
92895 pX++;
92896 }
92897 db->vtabOnConflict = pOp->p5;
92898 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
92899 db->vtabOnConflict = vtabOnConflict;
92900 sqlite3VtabImportErrmsg(p, pVtab);
92901 if( rc==SQLITE_OK && pOp->p1 ){
92902 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
92903 db->lastRowid = rowid;
92904 }
92905 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
92906 if( pOp->p5==OE_Ignore ){
92907 rc = SQLITE_OK;
92908 }else{
92909 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
92910 }
92911 }else{
92912 p->nChange++;
92913 }
92914 if( rc ) goto abort_due_to_error;
92915 }
92916 break;
92917}
92918#endif /* SQLITE_OMIT_VIRTUALTABLE */
92919
92920#ifndef SQLITE_OMIT_PAGER_PRAGMAS
92921/* Opcode: Pagecount P1 P2 * * *
92922**
92923** Write the current number of pages in database P1 to memory cell P2.
92924*/
92925case OP_Pagecount: { /* out2 */
92926 pOut = out2Prerelease(p, pOp);
92927 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
92928 break;
92929}
92930#endif
92931
92932
92933#ifndef SQLITE_OMIT_PAGER_PRAGMAS
92934/* Opcode: MaxPgcnt P1 P2 P3 * *
92935**
92936** Try to set the maximum page count for database P1 to the value in P3.
92937** Do not let the maximum page count fall below the current page count and
92938** do not change the maximum page count value if P3==0.
92939**
92940** Store the maximum page count after the change in register P2.
92941*/
92942case OP_MaxPgcnt: { /* out2 */
92943 unsigned int newMax;
92944 Btree *pBt;
92945
92946 pOut = out2Prerelease(p, pOp);
92947 pBt = db->aDb[pOp->p1].pBt;
92948 newMax = 0;
92949 if( pOp->p3 ){
92950 newMax = sqlite3BtreeLastPage(pBt);
92951 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
92952 }
92953 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
92954 break;
92955}
92956#endif
92957
92958/* Opcode: Function P1 P2 P3 P4 *
92959** Synopsis: r[P3]=func(r[P2@NP])
92960**
92961** Invoke a user function (P4 is a pointer to an sqlite3_context object that
92962** contains a pointer to the function to be run) with arguments taken
92963** from register P2 and successors. The number of arguments is in
92964** the sqlite3_context object that P4 points to.
92965** The result of the function is stored
92966** in register P3. Register P3 must not be one of the function inputs.
92967**
92968** P1 is a 32-bit bitmask indicating whether or not each argument to the
92969** function was determined to be constant at compile time. If the first
92970** argument was constant then bit 0 of P1 is set. This is used to determine
92971** whether meta data associated with a user function argument using the
92972** sqlite3_set_auxdata() API may be safely retained until the next
92973** invocation of this opcode.
92974**
92975** See also: AggStep, AggFinal, PureFunc
92976*/
92977/* Opcode: PureFunc P1 P2 P3 P4 *
92978** Synopsis: r[P3]=func(r[P2@NP])
92979**
92980** Invoke a user function (P4 is a pointer to an sqlite3_context object that
92981** contains a pointer to the function to be run) with arguments taken
92982** from register P2 and successors. The number of arguments is in
92983** the sqlite3_context object that P4 points to.
92984** The result of the function is stored
92985** in register P3. Register P3 must not be one of the function inputs.
92986**
92987** P1 is a 32-bit bitmask indicating whether or not each argument to the
92988** function was determined to be constant at compile time. If the first
92989** argument was constant then bit 0 of P1 is set. This is used to determine
92990** whether meta data associated with a user function argument using the
92991** sqlite3_set_auxdata() API may be safely retained until the next
92992** invocation of this opcode.
92993**
92994** This opcode works exactly like OP_Function. The only difference is in
92995** its name. This opcode is used in places where the function must be
92996** purely non-deterministic. Some built-in date/time functions can be
92997** either determinitic of non-deterministic, depending on their arguments.
92998** When those function are used in a non-deterministic way, they will check
92999** to see if they were called using OP_PureFunc instead of OP_Function, and
93000** if they were, they throw an error.
93001**
93002** See also: AggStep, AggFinal, Function
93003*/
93004case OP_PureFunc: /* group */
93005case OP_Function: { /* group */
93006 int i;
93007 sqlite3_context *pCtx;
93008
93009 assert( pOp->p4type==P4_FUNCCTX );
93010 pCtx = pOp->p4.pCtx;
93011
93012 /* If this function is inside of a trigger, the register array in aMem[]
93013 ** might change from one evaluation to the next. The next block of code
93014 ** checks to see if the register array has changed, and if so it
93015 ** reinitializes the relavant parts of the sqlite3_context object */
93016 pOut = &aMem[pOp->p3];
93017 if( pCtx->pOut != pOut ){
93018 pCtx->pVdbe = p;
93019 pCtx->pOut = pOut;
93020 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
93021 }
93022 assert( pCtx->pVdbe==p );
93023
93024 memAboutToChange(p, pOut);
93025#ifdef SQLITE_DEBUG
93026 for(i=0; i<pCtx->argc; i++){
93027 assert( memIsValid(pCtx->argv[i]) );
93028 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
93029 }
93030#endif
93031 MemSetTypeFlag(pOut, MEM_Null);
93032 assert( pCtx->isError==0 );
93033 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
93034
93035 /* If the function returned an error, throw an exception */
93036 if( pCtx->isError ){
93037 if( pCtx->isError>0 ){
93038 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
93039 rc = pCtx->isError;
93040 }
93041 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
93042 pCtx->isError = 0;
93043 if( rc ) goto abort_due_to_error;
93044 }
93045
93046 /* Copy the result of the function into register P3 */
93047 if( pOut->flags & (MEM_Str|MEM_Blob) ){
93048 sqlite3VdbeChangeEncoding(pOut, encoding);
93049 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
93050 }
93051
93052 REGISTER_TRACE(pOp->p3, pOut);
93053 UPDATE_MAX_BLOBSIZE(pOut);
93054 break;
93055}
93056
93057/* Opcode: Trace P1 P2 * P4 *
93058**
93059** Write P4 on the statement trace output if statement tracing is
93060** enabled.
93061**
93062** Operand P1 must be 0x7fffffff and P2 must positive.
93063*/
93064/* Opcode: Init P1 P2 P3 P4 *
93065** Synopsis: Start at P2
93066**
93067** Programs contain a single instance of this opcode as the very first
93068** opcode.
93069**
93070** If tracing is enabled (by the sqlite3_trace()) interface, then
93071** the UTF-8 string contained in P4 is emitted on the trace callback.
93072** Or if P4 is blank, use the string returned by sqlite3_sql().
93073**
93074** If P2 is not zero, jump to instruction P2.
93075**
93076** Increment the value of P1 so that OP_Once opcodes will jump the
93077** first time they are evaluated for this run.
93078**
93079** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
93080** error is encountered.
93081*/
93082case OP_Trace:
93083case OP_Init: { /* jump */
93084 int i;
93085#ifndef SQLITE_OMIT_TRACE
93086 char *zTrace;
93087#endif
93088
93089 /* If the P4 argument is not NULL, then it must be an SQL comment string.
93090 ** The "--" string is broken up to prevent false-positives with srcck1.c.
93091 **
93092 ** This assert() provides evidence for:
93093 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
93094 ** would have been returned by the legacy sqlite3_trace() interface by
93095 ** using the X argument when X begins with "--" and invoking
93096 ** sqlite3_expanded_sql(P) otherwise.
93097 */
93098 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
93099
93100 /* OP_Init is always instruction 0 */
93101 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
93102
93103#ifndef SQLITE_OMIT_TRACE
93104 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
93105 && !p->doingRerun
93106 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
93107 ){
93108#ifndef SQLITE_OMIT_DEPRECATED
93109 if( db->mTrace & SQLITE_TRACE_LEGACY ){
93110 char *z = sqlite3VdbeExpandSql(p, zTrace);
93111 db->trace.xLegacy(db->pTraceArg, z);
93112 sqlite3_free(z);
93113 }else
93114#endif
93115 if( db->nVdbeExec>1 ){
93116 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
93117 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
93118 sqlite3DbFree(db, z);
93119 }else{
93120 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
93121 }
93122 }
93123#ifdef SQLITE_USE_FCNTL_TRACE
93124 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
93125 if( zTrace ){
93126 int j;
93127 for(j=0; j<db->nDb; j++){
93128 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
93129 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
93130 }
93131 }
93132#endif /* SQLITE_USE_FCNTL_TRACE */
93133#ifdef SQLITE_DEBUG
93134 if( (db->flags & SQLITE_SqlTrace)!=0
93135 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
93136 ){
93137 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
93138 }
93139#endif /* SQLITE_DEBUG */
93140#endif /* SQLITE_OMIT_TRACE */
93141 assert( pOp->p2>0 );
93142 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
93143 if( pOp->opcode==OP_Trace ) break;
93144 for(i=1; i<p->nOp; i++){
93145 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
93146 }
93147 pOp->p1 = 0;
93148 }
93149 pOp->p1++;
93150 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
93151 goto jump_to_p2;
93152}
93153
93154#ifdef SQLITE_ENABLE_CURSOR_HINTS
93155/* Opcode: CursorHint P1 * * P4 *
93156**
93157** Provide a hint to cursor P1 that it only needs to return rows that
93158** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
93159** to values currently held in registers. TK_COLUMN terms in the P4
93160** expression refer to columns in the b-tree to which cursor P1 is pointing.
93161*/
93162case OP_CursorHint: {
93163 VdbeCursor *pC;
93164
93165 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93166 assert( pOp->p4type==P4_EXPR );
93167 pC = p->apCsr[pOp->p1];
93168 if( pC ){
93169 assert( pC->eCurType==CURTYPE_BTREE );
93170 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
93171 pOp->p4.pExpr, aMem);
93172 }
93173 break;
93174}
93175#endif /* SQLITE_ENABLE_CURSOR_HINTS */
93176
93177#ifdef SQLITE_DEBUG
93178/* Opcode: Abortable * * * * *
93179**
93180** Verify that an Abort can happen. Assert if an Abort at this point
93181** might cause database corruption. This opcode only appears in debugging
93182** builds.
93183**
93184** An Abort is safe if either there have been no writes, or if there is
93185** an active statement journal.
93186*/
93187case OP_Abortable: {
93188 sqlite3VdbeAssertAbortable(p);
93189 break;
93190}
93191#endif
93192
93193#ifdef SQLITE_DEBUG
93194/* Opcode: ReleaseReg P1 P2 P3 * P5
93195** Synopsis: release r[P1@P2] mask P3
93196**
93197** Release registers from service. Any content that was in the
93198** the registers is unreliable after this opcode completes.
93199**
93200** The registers released will be the P2 registers starting at P1,
93201** except if bit ii of P3 set, then do not release register P1+ii.
93202** In other words, P3 is a mask of registers to preserve.
93203**
93204** Releasing a register clears the Mem.pScopyFrom pointer. That means
93205** that if the content of the released register was set using OP_SCopy,
93206** a change to the value of the source register for the OP_SCopy will no longer
93207** generate an assertion fault in sqlite3VdbeMemAboutToChange().
93208**
93209** If P5 is set, then all released registers have their type set
93210** to MEM_Undefined so that any subsequent attempt to read the released
93211** register (before it is reinitialized) will generate an assertion fault.
93212**
93213** P5 ought to be set on every call to this opcode.
93214** However, there are places in the code generator will release registers
93215** before their are used, under the (valid) assumption that the registers
93216** will not be reallocated for some other purpose before they are used and
93217** hence are safe to release.
93218**
93219** This opcode is only available in testing and debugging builds. It is
93220** not generated for release builds. The purpose of this opcode is to help
93221** validate the generated bytecode. This opcode does not actually contribute
93222** to computing an answer.
93223*/
93224case OP_ReleaseReg: {
93225 Mem *pMem;
93226 int i;
93227 u32 constMask;
93228 assert( pOp->p1>0 );
93229 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
93230 pMem = &aMem[pOp->p1];
93231 constMask = pOp->p3;
93232 for(i=0; i<pOp->p2; i++, pMem++){
93233 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
93234 pMem->pScopyFrom = 0;
93235 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
93236 }
93237 }
93238 break;
93239}
93240#endif
93241
93242/* Opcode: Noop * * * * *
93243**
93244** Do nothing. This instruction is often useful as a jump
93245** destination.
93246*/
93247/*
93248** The magic Explain opcode are only inserted when explain==2 (which
93249** is to say when the EXPLAIN QUERY PLAN syntax is used.)
93250** This opcode records information from the optimizer. It is the
93251** the same as a no-op. This opcodesnever appears in a real VM program.
93252*/
93253default: { /* This is really OP_Noop, OP_Explain */
93254 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
93255
93256 break;
93257}
93258
93259/*****************************************************************************
93260** The cases of the switch statement above this line should all be indented
93261** by 6 spaces. But the left-most 6 spaces have been removed to improve the
93262** readability. From this point on down, the normal indentation rules are
93263** restored.
93264*****************************************************************************/
93265 }
93266
93267#ifdef VDBE_PROFILE
93268 {
93269 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
93270 if( endTime>start ) pOrigOp->cycles += endTime - start;
93271 pOrigOp->cnt++;
93272 }
93273#endif
93274
93275 /* The following code adds nothing to the actual functionality
93276 ** of the program. It is only here for testing and debugging.
93277 ** On the other hand, it does burn CPU cycles every time through
93278 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
93279 */
93280#ifndef NDEBUG
93281 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
93282
93283#ifdef SQLITE_DEBUG
93284 if( db->flags & SQLITE_VdbeTrace ){
93285 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
93286 if( rc!=0 ) printf("rc=%d\n",rc);
93287 if( opProperty & (OPFLG_OUT2) ){
93288 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
93289 }
93290 if( opProperty & OPFLG_OUT3 ){
93291 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
93292 }
93293 if( opProperty==0xff ){
93294 /* Never happens. This code exists to avoid a harmless linkage
93295 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
93296 ** used. */
93297 sqlite3VdbeRegisterDump(p);
93298 }
93299 }
93300#endif /* SQLITE_DEBUG */
93301#endif /* NDEBUG */
93302 } /* The end of the for(;;) loop the loops through opcodes */
93303
93304 /* If we reach this point, it means that execution is finished with
93305 ** an error of some kind.
93306 */
93307abort_due_to_error:
93308 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
93309 assert( rc );
93310 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
93311 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
93312 }
93313 p->rc = rc;
93314 sqlite3SystemError(db, rc);
93315 testcase( sqlite3GlobalConfig.xLog!=0 );
93316 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
93317 (int)(pOp - aOp), p->zSql, p->zErrMsg);
93318 sqlite3VdbeHalt(p);
93319 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
93320 rc = SQLITE_ERROR;
93321 if( resetSchemaOnFault>0 ){
93322 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
93323 }
93324
93325 /* This is the only way out of this procedure. We have to
93326 ** release the mutexes on btrees that were acquired at the
93327 ** top. */
93328vdbe_return:
93329#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
93330 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
93331 nProgressLimit += db->nProgressOps;
93332 if( db->xProgress(db->pProgressArg) ){
93333 nProgressLimit = LARGEST_UINT64;
93334 rc = SQLITE_INTERRUPT;
93335 goto abort_due_to_error;
93336 }
93337 }
93338#endif
93339 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
93340 sqlite3VdbeLeave(p);
93341 assert( rc!=SQLITE_OK || nExtraDelete==0
93342 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
93343 );
93344 return rc;
93345
93346 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
93347 ** is encountered.
93348 */
93349too_big:
93350 sqlite3VdbeError(p, "string or blob too big");
93351 rc = SQLITE_TOOBIG;
93352 goto abort_due_to_error;
93353
93354 /* Jump to here if a malloc() fails.
93355 */
93356no_mem:
93357 sqlite3OomFault(db);
93358 sqlite3VdbeError(p, "out of memory");
93359 rc = SQLITE_NOMEM_BKPT;
93360 goto abort_due_to_error;
93361
93362 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
93363 ** flag.
93364 */
93365abort_due_to_interrupt:
93366 assert( AtomicLoad(&db->u1.isInterrupted) );
93367 rc = SQLITE_INTERRUPT;
93368 goto abort_due_to_error;
93369}
93370
93371
93372/************** End of vdbe.c ************************************************/
93373/************** Begin file vdbeblob.c ****************************************/
93374/*
93375** 2007 May 1
93376**
93377** The author disclaims copyright to this source code. In place of
93378** a legal notice, here is a blessing:
93379**
93380** May you do good and not evil.
93381** May you find forgiveness for yourself and forgive others.
93382** May you share freely, never taking more than you give.
93383**
93384*************************************************************************
93385**
93386** This file contains code used to implement incremental BLOB I/O.
93387*/
93388
93389/* #include "sqliteInt.h" */
93390/* #include "vdbeInt.h" */
93391
93392#ifndef SQLITE_OMIT_INCRBLOB
93393
93394/*
93395** Valid sqlite3_blob* handles point to Incrblob structures.
93396*/
93397typedef struct Incrblob Incrblob;
93398struct Incrblob {
93399 int nByte; /* Size of open blob, in bytes */
93400 int iOffset; /* Byte offset of blob in cursor data */
93401 u16 iCol; /* Table column this handle is open on */
93402 BtCursor *pCsr; /* Cursor pointing at blob row */
93403 sqlite3_stmt *pStmt; /* Statement holding cursor open */
93404 sqlite3 *db; /* The associated database */
93405 char *zDb; /* Database name */
93406 Table *pTab; /* Table object */
93407};
93408
93409
93410/*
93411** This function is used by both blob_open() and blob_reopen(). It seeks
93412** the b-tree cursor associated with blob handle p to point to row iRow.
93413** If successful, SQLITE_OK is returned and subsequent calls to
93414** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
93415**
93416** If an error occurs, or if the specified row does not exist or does not
93417** contain a value of type TEXT or BLOB in the column nominated when the
93418** blob handle was opened, then an error code is returned and *pzErr may
93419** be set to point to a buffer containing an error message. It is the
93420** responsibility of the caller to free the error message buffer using
93421** sqlite3DbFree().
93422**
93423** If an error does occur, then the b-tree cursor is closed. All subsequent
93424** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
93425** immediately return SQLITE_ABORT.
93426*/
93427static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
93428 int rc; /* Error code */
93429 char *zErr = 0; /* Error message */
93430 Vdbe *v = (Vdbe *)p->pStmt;
93431
93432 /* Set the value of register r[1] in the SQL statement to integer iRow.
93433 ** This is done directly as a performance optimization
93434 */
93435 v->aMem[1].flags = MEM_Int;
93436 v->aMem[1].u.i = iRow;
93437
93438 /* If the statement has been run before (and is paused at the OP_ResultRow)
93439 ** then back it up to the point where it does the OP_NotExists. This could
93440 ** have been down with an extra OP_Goto, but simply setting the program
93441 ** counter is faster. */
93442 if( v->pc>4 ){
93443 v->pc = 4;
93444 assert( v->aOp[v->pc].opcode==OP_NotExists );
93445 rc = sqlite3VdbeExec(v);
93446 }else{
93447 rc = sqlite3_step(p->pStmt);
93448 }
93449 if( rc==SQLITE_ROW ){
93450 VdbeCursor *pC = v->apCsr[0];
93451 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
93452 testcase( pC->nHdrParsed==p->iCol );
93453 testcase( pC->nHdrParsed==p->iCol+1 );
93454 if( type<12 ){
93455 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
93456 type==0?"null": type==7?"real": "integer"
93457 );
93458 rc = SQLITE_ERROR;
93459 sqlite3_finalize(p->pStmt);
93460 p->pStmt = 0;
93461 }else{
93462 p->iOffset = pC->aType[p->iCol + pC->nField];
93463 p->nByte = sqlite3VdbeSerialTypeLen(type);
93464 p->pCsr = pC->uc.pCursor;
93465 sqlite3BtreeIncrblobCursor(p->pCsr);
93466 }
93467 }
93468
93469 if( rc==SQLITE_ROW ){
93470 rc = SQLITE_OK;
93471 }else if( p->pStmt ){
93472 rc = sqlite3_finalize(p->pStmt);
93473 p->pStmt = 0;
93474 if( rc==SQLITE_OK ){
93475 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
93476 rc = SQLITE_ERROR;
93477 }else{
93478 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
93479 }
93480 }
93481
93482 assert( rc!=SQLITE_OK || zErr==0 );
93483 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
93484
93485 *pzErr = zErr;
93486 return rc;
93487}
93488
93489/*
93490** Open a blob handle.
93491*/
93492SQLITE_API int sqlite3_blob_open(
93493 sqlite3* db, /* The database connection */
93494 const char *zDb, /* The attached database containing the blob */
93495 const char *zTable, /* The table containing the blob */
93496 const char *zColumn, /* The column containing the blob */
93497 sqlite_int64 iRow, /* The row containing the glob */
93498 int wrFlag, /* True -> read/write access, false -> read-only */
93499 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
93500){
93501 int nAttempt = 0;
93502 int iCol; /* Index of zColumn in row-record */
93503 int rc = SQLITE_OK;
93504 char *zErr = 0;
93505 Table *pTab;
93506 Incrblob *pBlob = 0;
93507 Parse sParse;
93508
93509#ifdef SQLITE_ENABLE_API_ARMOR
93510 if( ppBlob==0 ){
93511 return SQLITE_MISUSE_BKPT;
93512 }
93513#endif
93514 *ppBlob = 0;
93515#ifdef SQLITE_ENABLE_API_ARMOR
93516 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
93517 return SQLITE_MISUSE_BKPT;
93518 }
93519#endif
93520 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
93521
93522 sqlite3_mutex_enter(db->mutex);
93523
93524 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
93525 do {
93526 memset(&sParse, 0, sizeof(Parse));
93527 if( !pBlob ) goto blob_open_out;
93528 sParse.db = db;
93529 sqlite3DbFree(db, zErr);
93530 zErr = 0;
93531
93532 sqlite3BtreeEnterAll(db);
93533 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
93534 if( pTab && IsVirtual(pTab) ){
93535 pTab = 0;
93536 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
93537 }
93538 if( pTab && !HasRowid(pTab) ){
93539 pTab = 0;
93540 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
93541 }
93542#ifndef SQLITE_OMIT_VIEW
93543 if( pTab && pTab->pSelect ){
93544 pTab = 0;
93545 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
93546 }
93547#endif
93548 if( !pTab ){
93549 if( sParse.zErrMsg ){
93550 sqlite3DbFree(db, zErr);
93551 zErr = sParse.zErrMsg;
93552 sParse.zErrMsg = 0;
93553 }
93554 rc = SQLITE_ERROR;
93555 sqlite3BtreeLeaveAll(db);
93556 goto blob_open_out;
93557 }
93558 pBlob->pTab = pTab;
93559 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
93560
93561 /* Now search pTab for the exact column. */
93562 for(iCol=0; iCol<pTab->nCol; iCol++) {
93563 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
93564 break;
93565 }
93566 }
93567 if( iCol==pTab->nCol ){
93568 sqlite3DbFree(db, zErr);
93569 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
93570 rc = SQLITE_ERROR;
93571 sqlite3BtreeLeaveAll(db);
93572 goto blob_open_out;
93573 }
93574
93575 /* If the value is being opened for writing, check that the
93576 ** column is not indexed, and that it is not part of a foreign key.
93577 */
93578 if( wrFlag ){
93579 const char *zFault = 0;
93580 Index *pIdx;
93581#ifndef SQLITE_OMIT_FOREIGN_KEY
93582 if( db->flags&SQLITE_ForeignKeys ){
93583 /* Check that the column is not part of an FK child key definition. It
93584 ** is not necessary to check if it is part of a parent key, as parent
93585 ** key columns must be indexed. The check below will pick up this
93586 ** case. */
93587 FKey *pFKey;
93588 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
93589 int j;
93590 for(j=0; j<pFKey->nCol; j++){
93591 if( pFKey->aCol[j].iFrom==iCol ){
93592 zFault = "foreign key";
93593 }
93594 }
93595 }
93596 }
93597#endif
93598 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
93599 int j;
93600 for(j=0; j<pIdx->nKeyCol; j++){
93601 /* FIXME: Be smarter about indexes that use expressions */
93602 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
93603 zFault = "indexed";
93604 }
93605 }
93606 }
93607 if( zFault ){
93608 sqlite3DbFree(db, zErr);
93609 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
93610 rc = SQLITE_ERROR;
93611 sqlite3BtreeLeaveAll(db);
93612 goto blob_open_out;
93613 }
93614 }
93615
93616 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
93617 assert( pBlob->pStmt || db->mallocFailed );
93618 if( pBlob->pStmt ){
93619
93620 /* This VDBE program seeks a btree cursor to the identified
93621 ** db/table/row entry. The reason for using a vdbe program instead
93622 ** of writing code to use the b-tree layer directly is that the
93623 ** vdbe program will take advantage of the various transaction,
93624 ** locking and error handling infrastructure built into the vdbe.
93625 **
93626 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
93627 ** Code external to the Vdbe then "borrows" the b-tree cursor and
93628 ** uses it to implement the blob_read(), blob_write() and
93629 ** blob_bytes() functions.
93630 **
93631 ** The sqlite3_blob_close() function finalizes the vdbe program,
93632 ** which closes the b-tree cursor and (possibly) commits the
93633 ** transaction.
93634 */
93635 static const int iLn = VDBE_OFFSET_LINENO(2);
93636 static const VdbeOpList openBlob[] = {
93637 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
93638 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
93639 /* blobSeekToRow() will initialize r[1] to the desired rowid */
93640 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
93641 {OP_Column, 0, 0, 1}, /* 3 */
93642 {OP_ResultRow, 1, 0, 0}, /* 4 */
93643 {OP_Halt, 0, 0, 0}, /* 5 */
93644 };
93645 Vdbe *v = (Vdbe *)pBlob->pStmt;
93646 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
93647 VdbeOp *aOp;
93648
93649 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
93650 pTab->pSchema->schema_cookie,
93651 pTab->pSchema->iGeneration);
93652 sqlite3VdbeChangeP5(v, 1);
93653 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
93654 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
93655
93656 /* Make sure a mutex is held on the table to be accessed */
93657 sqlite3VdbeUsesBtree(v, iDb);
93658
93659 if( db->mallocFailed==0 ){
93660 assert( aOp!=0 );
93661 /* Configure the OP_TableLock instruction */
93662#ifdef SQLITE_OMIT_SHARED_CACHE
93663 aOp[0].opcode = OP_Noop;
93664#else
93665 aOp[0].p1 = iDb;
93666 aOp[0].p2 = pTab->tnum;
93667 aOp[0].p3 = wrFlag;
93668 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
93669 }
93670 if( db->mallocFailed==0 ){
93671#endif
93672
93673 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
93674 ** parameter of the other to pTab->tnum. */
93675 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
93676 aOp[1].p2 = pTab->tnum;
93677 aOp[1].p3 = iDb;
93678
93679 /* Configure the number of columns. Configure the cursor to
93680 ** think that the table has one more column than it really
93681 ** does. An OP_Column to retrieve this imaginary column will
93682 ** always return an SQL NULL. This is useful because it means
93683 ** we can invoke OP_Column to fill in the vdbe cursors type
93684 ** and offset cache without causing any IO.
93685 */
93686 aOp[1].p4type = P4_INT32;
93687 aOp[1].p4.i = pTab->nCol+1;
93688 aOp[3].p2 = pTab->nCol;
93689
93690 sParse.nVar = 0;
93691 sParse.nMem = 1;
93692 sParse.nTab = 1;
93693 sqlite3VdbeMakeReady(v, &sParse);
93694 }
93695 }
93696
93697 pBlob->iCol = iCol;
93698 pBlob->db = db;
93699 sqlite3BtreeLeaveAll(db);
93700 if( db->mallocFailed ){
93701 goto blob_open_out;
93702 }
93703 rc = blobSeekToRow(pBlob, iRow, &zErr);
93704 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
93705
93706blob_open_out:
93707 if( rc==SQLITE_OK && db->mallocFailed==0 ){
93708 *ppBlob = (sqlite3_blob *)pBlob;
93709 }else{
93710 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
93711 sqlite3DbFree(db, pBlob);
93712 }
93713 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
93714 sqlite3DbFree(db, zErr);
93715 sqlite3ParserReset(&sParse);
93716 rc = sqlite3ApiExit(db, rc);
93717 sqlite3_mutex_leave(db->mutex);
93718 return rc;
93719}
93720
93721/*
93722** Close a blob handle that was previously created using
93723** sqlite3_blob_open().
93724*/
93725SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
93726 Incrblob *p = (Incrblob *)pBlob;
93727 int rc;
93728 sqlite3 *db;
93729
93730 if( p ){
93731 sqlite3_stmt *pStmt = p->pStmt;
93732 db = p->db;
93733 sqlite3_mutex_enter(db->mutex);
93734 sqlite3DbFree(db, p);
93735 sqlite3_mutex_leave(db->mutex);
93736 rc = sqlite3_finalize(pStmt);
93737 }else{
93738 rc = SQLITE_OK;
93739 }
93740 return rc;
93741}
93742
93743/*
93744** Perform a read or write operation on a blob
93745*/
93746static int blobReadWrite(
93747 sqlite3_blob *pBlob,
93748 void *z,
93749 int n,
93750 int iOffset,
93751 int (*xCall)(BtCursor*, u32, u32, void*)
93752){
93753 int rc;
93754 Incrblob *p = (Incrblob *)pBlob;
93755 Vdbe *v;
93756 sqlite3 *db;
93757
93758 if( p==0 ) return SQLITE_MISUSE_BKPT;
93759 db = p->db;
93760 sqlite3_mutex_enter(db->mutex);
93761 v = (Vdbe*)p->pStmt;
93762
93763 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
93764 /* Request is out of range. Return a transient error. */
93765 rc = SQLITE_ERROR;
93766 }else if( v==0 ){
93767 /* If there is no statement handle, then the blob-handle has
93768 ** already been invalidated. Return SQLITE_ABORT in this case.
93769 */
93770 rc = SQLITE_ABORT;
93771 }else{
93772 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
93773 ** returned, clean-up the statement handle.
93774 */
93775 assert( db == v->db );
93776 sqlite3BtreeEnterCursor(p->pCsr);
93777
93778#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
93779 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
93780 /* If a pre-update hook is registered and this is a write cursor,
93781 ** invoke it here.
93782 **
93783 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
93784 ** operation should really be an SQLITE_UPDATE. This is probably
93785 ** incorrect, but is convenient because at this point the new.* values
93786 ** are not easily obtainable. And for the sessions module, an
93787 ** SQLITE_UPDATE where the PK columns do not change is handled in the
93788 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
93789 ** slightly more efficient). Since you cannot write to a PK column
93790 ** using the incremental-blob API, this works. For the sessions module
93791 ** anyhow.
93792 */
93793 sqlite3_int64 iKey;
93794 iKey = sqlite3BtreeIntegerKey(p->pCsr);
93795 sqlite3VdbePreUpdateHook(
93796 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
93797 );
93798 }
93799#endif
93800
93801 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
93802 sqlite3BtreeLeaveCursor(p->pCsr);
93803 if( rc==SQLITE_ABORT ){
93804 sqlite3VdbeFinalize(v);
93805 p->pStmt = 0;
93806 }else{
93807 v->rc = rc;
93808 }
93809 }
93810 sqlite3Error(db, rc);
93811 rc = sqlite3ApiExit(db, rc);
93812 sqlite3_mutex_leave(db->mutex);
93813 return rc;
93814}
93815
93816/*
93817** Read data from a blob handle.
93818*/
93819SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
93820 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
93821}
93822
93823/*
93824** Write data to a blob handle.
93825*/
93826SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
93827 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
93828}
93829
93830/*
93831** Query a blob handle for the size of the data.
93832**
93833** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
93834** so no mutex is required for access.
93835*/
93836SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
93837 Incrblob *p = (Incrblob *)pBlob;
93838 return (p && p->pStmt) ? p->nByte : 0;
93839}
93840
93841/*
93842** Move an existing blob handle to point to a different row of the same
93843** database table.
93844**
93845** If an error occurs, or if the specified row does not exist or does not
93846** contain a blob or text value, then an error code is returned and the
93847** database handle error code and message set. If this happens, then all
93848** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
93849** immediately return SQLITE_ABORT.
93850*/
93851SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
93852 int rc;
93853 Incrblob *p = (Incrblob *)pBlob;
93854 sqlite3 *db;
93855
93856 if( p==0 ) return SQLITE_MISUSE_BKPT;
93857 db = p->db;
93858 sqlite3_mutex_enter(db->mutex);
93859
93860 if( p->pStmt==0 ){
93861 /* If there is no statement handle, then the blob-handle has
93862 ** already been invalidated. Return SQLITE_ABORT in this case.
93863 */
93864 rc = SQLITE_ABORT;
93865 }else{
93866 char *zErr;
93867 rc = blobSeekToRow(p, iRow, &zErr);
93868 if( rc!=SQLITE_OK ){
93869 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
93870 sqlite3DbFree(db, zErr);
93871 }
93872 assert( rc!=SQLITE_SCHEMA );
93873 }
93874
93875 rc = sqlite3ApiExit(db, rc);
93876 assert( rc==SQLITE_OK || p->pStmt==0 );
93877 sqlite3_mutex_leave(db->mutex);
93878 return rc;
93879}
93880
93881#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
93882
93883/************** End of vdbeblob.c ********************************************/
93884/************** Begin file vdbesort.c ****************************************/
93885/*
93886** 2011-07-09
93887**
93888** The author disclaims copyright to this source code. In place of
93889** a legal notice, here is a blessing:
93890**
93891** May you do good and not evil.
93892** May you find forgiveness for yourself and forgive others.
93893** May you share freely, never taking more than you give.
93894**
93895*************************************************************************
93896** This file contains code for the VdbeSorter object, used in concert with
93897** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
93898** or by SELECT statements with ORDER BY clauses that cannot be satisfied
93899** using indexes and without LIMIT clauses.
93900**
93901** The VdbeSorter object implements a multi-threaded external merge sort
93902** algorithm that is efficient even if the number of elements being sorted
93903** exceeds the available memory.
93904**
93905** Here is the (internal, non-API) interface between this module and the
93906** rest of the SQLite system:
93907**
93908** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
93909**
93910** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
93911** object. The row is a binary blob in the
93912** OP_MakeRecord format that contains both
93913** the ORDER BY key columns and result columns
93914** in the case of a SELECT w/ ORDER BY, or
93915** the complete record for an index entry
93916** in the case of a CREATE INDEX.
93917**
93918** sqlite3VdbeSorterRewind() Sort all content previously added.
93919** Position the read cursor on the
93920** first sorted element.
93921**
93922** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
93923** element.
93924**
93925** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
93926** row currently under the read cursor.
93927**
93928** sqlite3VdbeSorterCompare() Compare the binary blob for the row
93929** currently under the read cursor against
93930** another binary blob X and report if
93931** X is strictly less than the read cursor.
93932** Used to enforce uniqueness in a
93933** CREATE UNIQUE INDEX statement.
93934**
93935** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
93936** all resources.
93937**
93938** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
93939** is like Close() followed by Init() only
93940** much faster.
93941**
93942** The interfaces above must be called in a particular order. Write() can
93943** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
93944** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
93945**
93946** Init()
93947** for each record: Write()
93948** Rewind()
93949** Rowkey()/Compare()
93950** Next()
93951** Close()
93952**
93953** Algorithm:
93954**
93955** Records passed to the sorter via calls to Write() are initially held
93956** unsorted in main memory. Assuming the amount of memory used never exceeds
93957** a threshold, when Rewind() is called the set of records is sorted using
93958** an in-memory merge sort. In this case, no temporary files are required
93959** and subsequent calls to Rowkey(), Next() and Compare() read records
93960** directly from main memory.
93961**
93962** If the amount of space used to store records in main memory exceeds the
93963** threshold, then the set of records currently in memory are sorted and
93964** written to a temporary file in "Packed Memory Array" (PMA) format.
93965** A PMA created at this point is known as a "level-0 PMA". Higher levels
93966** of PMAs may be created by merging existing PMAs together - for example
93967** merging two or more level-0 PMAs together creates a level-1 PMA.
93968**
93969** The threshold for the amount of main memory to use before flushing
93970** records to a PMA is roughly the same as the limit configured for the
93971** page-cache of the main database. Specifically, the threshold is set to
93972** the value returned by "PRAGMA main.page_size" multipled by
93973** that returned by "PRAGMA main.cache_size", in bytes.
93974**
93975** If the sorter is running in single-threaded mode, then all PMAs generated
93976** are appended to a single temporary file. Or, if the sorter is running in
93977** multi-threaded mode then up to (N+1) temporary files may be opened, where
93978** N is the configured number of worker threads. In this case, instead of
93979** sorting the records and writing the PMA to a temporary file itself, the
93980** calling thread usually launches a worker thread to do so. Except, if
93981** there are already N worker threads running, the main thread does the work
93982** itself.
93983**
93984** The sorter is running in multi-threaded mode if (a) the library was built
93985** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
93986** than zero, and (b) worker threads have been enabled at runtime by calling
93987** "PRAGMA threads=N" with some value of N greater than 0.
93988**
93989** When Rewind() is called, any data remaining in memory is flushed to a
93990** final PMA. So at this point the data is stored in some number of sorted
93991** PMAs within temporary files on disk.
93992**
93993** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
93994** sorter is running in single-threaded mode, then these PMAs are merged
93995** incrementally as keys are retreived from the sorter by the VDBE. The
93996** MergeEngine object, described in further detail below, performs this
93997** merge.
93998**
93999** Or, if running in multi-threaded mode, then a background thread is
94000** launched to merge the existing PMAs. Once the background thread has
94001** merged T bytes of data into a single sorted PMA, the main thread
94002** begins reading keys from that PMA while the background thread proceeds
94003** with merging the next T bytes of data. And so on.
94004**
94005** Parameter T is set to half the value of the memory threshold used
94006** by Write() above to determine when to create a new PMA.
94007**
94008** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
94009** Rewind() is called, then a hierarchy of incremental-merges is used.
94010** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
94011** disk are merged together. Then T bytes of data from the second set, and
94012** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
94013** PMAs at a time. This done is to improve locality.
94014**
94015** If running in multi-threaded mode and there are more than
94016** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
94017** than one background thread may be created. Specifically, there may be
94018** one background thread for each temporary file on disk, and one background
94019** thread to merge the output of each of the others to a single PMA for
94020** the main thread to read from.
94021*/
94022/* #include "sqliteInt.h" */
94023/* #include "vdbeInt.h" */
94024
94025/*
94026** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
94027** messages to stderr that may be helpful in understanding the performance
94028** characteristics of the sorter in multi-threaded mode.
94029*/
94030#if 0
94031# define SQLITE_DEBUG_SORTER_THREADS 1
94032#endif
94033
94034/*
94035** Hard-coded maximum amount of data to accumulate in memory before flushing
94036** to a level 0 PMA. The purpose of this limit is to prevent various integer
94037** overflows. 512MiB.
94038*/
94039#define SQLITE_MAX_PMASZ (1<<29)
94040
94041/*
94042** Private objects used by the sorter
94043*/
94044typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
94045typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
94046typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
94047typedef struct SorterRecord SorterRecord; /* A record being sorted */
94048typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
94049typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
94050typedef struct SorterList SorterList; /* In-memory list of records */
94051typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
94052
94053/*
94054** A container for a temp file handle and the current amount of data
94055** stored in the file.
94056*/
94057struct SorterFile {
94058 sqlite3_file *pFd; /* File handle */
94059 i64 iEof; /* Bytes of data stored in pFd */
94060};
94061
94062/*
94063** An in-memory list of objects to be sorted.
94064**
94065** If aMemory==0 then each object is allocated separately and the objects
94066** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
94067** are stored in the aMemory[] bulk memory, one right after the other, and
94068** are connected using SorterRecord.u.iNext.
94069*/
94070struct SorterList {
94071 SorterRecord *pList; /* Linked list of records */
94072 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
94073 int szPMA; /* Size of pList as PMA in bytes */
94074};
94075
94076/*
94077** The MergeEngine object is used to combine two or more smaller PMAs into
94078** one big PMA using a merge operation. Separate PMAs all need to be
94079** combined into one big PMA in order to be able to step through the sorted
94080** records in order.
94081**
94082** The aReadr[] array contains a PmaReader object for each of the PMAs being
94083** merged. An aReadr[] object either points to a valid key or else is at EOF.
94084** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
94085** For the purposes of the paragraphs below, we assume that the array is
94086** actually N elements in size, where N is the smallest power of 2 greater
94087** to or equal to the number of PMAs being merged. The extra aReadr[] elements
94088** are treated as if they are empty (always at EOF).
94089**
94090** The aTree[] array is also N elements in size. The value of N is stored in
94091** the MergeEngine.nTree variable.
94092**
94093** The final (N/2) elements of aTree[] contain the results of comparing
94094** pairs of PMA keys together. Element i contains the result of
94095** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
94096** aTree element is set to the index of it.
94097**
94098** For the purposes of this comparison, EOF is considered greater than any
94099** other key value. If the keys are equal (only possible with two EOF
94100** values), it doesn't matter which index is stored.
94101**
94102** The (N/4) elements of aTree[] that precede the final (N/2) described
94103** above contains the index of the smallest of each block of 4 PmaReaders
94104** And so on. So that aTree[1] contains the index of the PmaReader that
94105** currently points to the smallest key value. aTree[0] is unused.
94106**
94107** Example:
94108**
94109** aReadr[0] -> Banana
94110** aReadr[1] -> Feijoa
94111** aReadr[2] -> Elderberry
94112** aReadr[3] -> Currant
94113** aReadr[4] -> Grapefruit
94114** aReadr[5] -> Apple
94115** aReadr[6] -> Durian
94116** aReadr[7] -> EOF
94117**
94118** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
94119**
94120** The current element is "Apple" (the value of the key indicated by
94121** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
94122** be advanced to the next key in its segment. Say the next key is
94123** "Eggplant":
94124**
94125** aReadr[5] -> Eggplant
94126**
94127** The contents of aTree[] are updated first by comparing the new PmaReader
94128** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
94129** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
94130** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
94131** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
94132** so the value written into element 1 of the array is 0. As follows:
94133**
94134** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
94135**
94136** In other words, each time we advance to the next sorter element, log2(N)
94137** key comparison operations are required, where N is the number of segments
94138** being merged (rounded up to the next power of 2).
94139*/
94140struct MergeEngine {
94141 int nTree; /* Used size of aTree/aReadr (power of 2) */
94142 SortSubtask *pTask; /* Used by this thread only */
94143 int *aTree; /* Current state of incremental merge */
94144 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
94145};
94146
94147/*
94148** This object represents a single thread of control in a sort operation.
94149** Exactly VdbeSorter.nTask instances of this object are allocated
94150** as part of each VdbeSorter object. Instances are never allocated any
94151** other way. VdbeSorter.nTask is set to the number of worker threads allowed
94152** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
94153** single-threaded operation, there is exactly one instance of this object
94154** and for multi-threaded operation there are two or more instances.
94155**
94156** Essentially, this structure contains all those fields of the VdbeSorter
94157** structure for which each thread requires a separate instance. For example,
94158** each thread requries its own UnpackedRecord object to unpack records in
94159** as part of comparison operations.
94160**
94161** Before a background thread is launched, variable bDone is set to 0. Then,
94162** right before it exits, the thread itself sets bDone to 1. This is used for
94163** two purposes:
94164**
94165** 1. When flushing the contents of memory to a level-0 PMA on disk, to
94166** attempt to select a SortSubtask for which there is not already an
94167** active background thread (since doing so causes the main thread
94168** to block until it finishes).
94169**
94170** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
94171** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
94172** block provoke debugging output.
94173**
94174** In both cases, the effects of the main thread seeing (bDone==0) even
94175** after the thread has finished are not dire. So we don't worry about
94176** memory barriers and such here.
94177*/
94178typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
94179struct SortSubtask {
94180 SQLiteThread *pThread; /* Background thread, if any */
94181 int bDone; /* Set if thread is finished but not joined */
94182 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
94183 UnpackedRecord *pUnpacked; /* Space to unpack a record */
94184 SorterList list; /* List for thread to write to a PMA */
94185 int nPMA; /* Number of PMAs currently in file */
94186 SorterCompare xCompare; /* Compare function to use */
94187 SorterFile file; /* Temp file for level-0 PMAs */
94188 SorterFile file2; /* Space for other PMAs */
94189};
94190
94191
94192/*
94193** Main sorter structure. A single instance of this is allocated for each
94194** sorter cursor created by the VDBE.
94195**
94196** mxKeysize:
94197** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
94198** this variable is updated so as to be set to the size on disk of the
94199** largest record in the sorter.
94200*/
94201struct VdbeSorter {
94202 int mnPmaSize; /* Minimum PMA size, in bytes */
94203 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
94204 int mxKeysize; /* Largest serialized key seen so far */
94205 int pgsz; /* Main database page size */
94206 PmaReader *pReader; /* Readr data from here after Rewind() */
94207 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
94208 sqlite3 *db; /* Database connection */
94209 KeyInfo *pKeyInfo; /* How to compare records */
94210 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
94211 SorterList list; /* List of in-memory records */
94212 int iMemory; /* Offset of free space in list.aMemory */
94213 int nMemory; /* Size of list.aMemory allocation in bytes */
94214 u8 bUsePMA; /* True if one or more PMAs created */
94215 u8 bUseThreads; /* True to use background threads */
94216 u8 iPrev; /* Previous thread used to flush PMA */
94217 u8 nTask; /* Size of aTask[] array */
94218 u8 typeMask;
94219 SortSubtask aTask[1]; /* One or more subtasks */
94220};
94221
94222#define SORTER_TYPE_INTEGER 0x01
94223#define SORTER_TYPE_TEXT 0x02
94224
94225/*
94226** An instance of the following object is used to read records out of a
94227** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
94228** aKey might point into aMap or into aBuffer. If neither of those locations
94229** contain a contiguous representation of the key, then aAlloc is allocated
94230** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
94231**
94232** pFd==0 at EOF.
94233*/
94234struct PmaReader {
94235 i64 iReadOff; /* Current read offset */
94236 i64 iEof; /* 1 byte past EOF for this PmaReader */
94237 int nAlloc; /* Bytes of space at aAlloc */
94238 int nKey; /* Number of bytes in key */
94239 sqlite3_file *pFd; /* File handle we are reading from */
94240 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
94241 u8 *aKey; /* Pointer to current key */
94242 u8 *aBuffer; /* Current read buffer */
94243 int nBuffer; /* Size of read buffer in bytes */
94244 u8 *aMap; /* Pointer to mapping of entire file */
94245 IncrMerger *pIncr; /* Incremental merger */
94246};
94247
94248/*
94249** Normally, a PmaReader object iterates through an existing PMA stored
94250** within a temp file. However, if the PmaReader.pIncr variable points to
94251** an object of the following type, it may be used to iterate/merge through
94252** multiple PMAs simultaneously.
94253**
94254** There are two types of IncrMerger object - single (bUseThread==0) and
94255** multi-threaded (bUseThread==1).
94256**
94257** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
94258** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
94259** size. When the IncrMerger is initialized, it reads enough data from
94260** pMerger to populate aFile[0]. It then sets variables within the
94261** corresponding PmaReader object to read from that file and kicks off
94262** a background thread to populate aFile[1] with the next mxSz bytes of
94263** sorted record data from pMerger.
94264**
94265** When the PmaReader reaches the end of aFile[0], it blocks until the
94266** background thread has finished populating aFile[1]. It then exchanges
94267** the contents of the aFile[0] and aFile[1] variables within this structure,
94268** sets the PmaReader fields to read from the new aFile[0] and kicks off
94269** another background thread to populate the new aFile[1]. And so on, until
94270** the contents of pMerger are exhausted.
94271**
94272** A single-threaded IncrMerger does not open any temporary files of its
94273** own. Instead, it has exclusive access to mxSz bytes of space beginning
94274** at offset iStartOff of file pTask->file2. And instead of using a
94275** background thread to prepare data for the PmaReader, with a single
94276** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
94277** keys from pMerger by the calling thread whenever the PmaReader runs out
94278** of data.
94279*/
94280struct IncrMerger {
94281 SortSubtask *pTask; /* Task that owns this merger */
94282 MergeEngine *pMerger; /* Merge engine thread reads data from */
94283 i64 iStartOff; /* Offset to start writing file at */
94284 int mxSz; /* Maximum bytes of data to store */
94285 int bEof; /* Set to true when merge is finished */
94286 int bUseThread; /* True to use a bg thread for this object */
94287 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
94288};
94289
94290/*
94291** An instance of this object is used for writing a PMA.
94292**
94293** The PMA is written one record at a time. Each record is of an arbitrary
94294** size. But I/O is more efficient if it occurs in page-sized blocks where
94295** each block is aligned on a page boundary. This object caches writes to
94296** the PMA so that aligned, page-size blocks are written.
94297*/
94298struct PmaWriter {
94299 int eFWErr; /* Non-zero if in an error state */
94300 u8 *aBuffer; /* Pointer to write buffer */
94301 int nBuffer; /* Size of write buffer in bytes */
94302 int iBufStart; /* First byte of buffer to write */
94303 int iBufEnd; /* Last byte of buffer to write */
94304 i64 iWriteOff; /* Offset of start of buffer in file */
94305 sqlite3_file *pFd; /* File handle to write to */
94306};
94307
94308/*
94309** This object is the header on a single record while that record is being
94310** held in memory and prior to being written out as part of a PMA.
94311**
94312** How the linked list is connected depends on how memory is being managed
94313** by this module. If using a separate allocation for each in-memory record
94314** (VdbeSorter.list.aMemory==0), then the list is always connected using the
94315** SorterRecord.u.pNext pointers.
94316**
94317** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
94318** then while records are being accumulated the list is linked using the
94319** SorterRecord.u.iNext offset. This is because the aMemory[] array may
94320** be sqlite3Realloc()ed while records are being accumulated. Once the VM
94321** has finished passing records to the sorter, or when the in-memory buffer
94322** is full, the list is sorted. As part of the sorting process, it is
94323** converted to use the SorterRecord.u.pNext pointers. See function
94324** vdbeSorterSort() for details.
94325*/
94326struct SorterRecord {
94327 int nVal; /* Size of the record in bytes */
94328 union {
94329 SorterRecord *pNext; /* Pointer to next record in list */
94330 int iNext; /* Offset within aMemory of next record */
94331 } u;
94332 /* The data for the record immediately follows this header */
94333};
94334
94335/* Return a pointer to the buffer containing the record data for SorterRecord
94336** object p. Should be used as if:
94337**
94338** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
94339*/
94340#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
94341
94342
94343/* Maximum number of PMAs that a single MergeEngine can merge */
94344#define SORTER_MAX_MERGE_COUNT 16
94345
94346static int vdbeIncrSwap(IncrMerger*);
94347static void vdbeIncrFree(IncrMerger *);
94348
94349/*
94350** Free all memory belonging to the PmaReader object passed as the
94351** argument. All structure fields are set to zero before returning.
94352*/
94353static void vdbePmaReaderClear(PmaReader *pReadr){
94354 sqlite3_free(pReadr->aAlloc);
94355 sqlite3_free(pReadr->aBuffer);
94356 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
94357 vdbeIncrFree(pReadr->pIncr);
94358 memset(pReadr, 0, sizeof(PmaReader));
94359}
94360
94361/*
94362** Read the next nByte bytes of data from the PMA p.
94363** If successful, set *ppOut to point to a buffer containing the data
94364** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
94365** error code.
94366**
94367** The buffer returned in *ppOut is only valid until the
94368** next call to this function.
94369*/
94370static int vdbePmaReadBlob(
94371 PmaReader *p, /* PmaReader from which to take the blob */
94372 int nByte, /* Bytes of data to read */
94373 u8 **ppOut /* OUT: Pointer to buffer containing data */
94374){
94375 int iBuf; /* Offset within buffer to read from */
94376 int nAvail; /* Bytes of data available in buffer */
94377
94378 if( p->aMap ){
94379 *ppOut = &p->aMap[p->iReadOff];
94380 p->iReadOff += nByte;
94381 return SQLITE_OK;
94382 }
94383
94384 assert( p->aBuffer );
94385
94386 /* If there is no more data to be read from the buffer, read the next
94387 ** p->nBuffer bytes of data from the file into it. Or, if there are less
94388 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
94389 iBuf = p->iReadOff % p->nBuffer;
94390 if( iBuf==0 ){
94391 int nRead; /* Bytes to read from disk */
94392 int rc; /* sqlite3OsRead() return code */
94393
94394 /* Determine how many bytes of data to read. */
94395 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
94396 nRead = p->nBuffer;
94397 }else{
94398 nRead = (int)(p->iEof - p->iReadOff);
94399 }
94400 assert( nRead>0 );
94401
94402 /* Readr data from the file. Return early if an error occurs. */
94403 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
94404 assert( rc!=SQLITE_IOERR_SHORT_READ );
94405 if( rc!=SQLITE_OK ) return rc;
94406 }
94407 nAvail = p->nBuffer - iBuf;
94408
94409 if( nByte<=nAvail ){
94410 /* The requested data is available in the in-memory buffer. In this
94411 ** case there is no need to make a copy of the data, just return a
94412 ** pointer into the buffer to the caller. */
94413 *ppOut = &p->aBuffer[iBuf];
94414 p->iReadOff += nByte;
94415 }else{
94416 /* The requested data is not all available in the in-memory buffer.
94417 ** In this case, allocate space at p->aAlloc[] to copy the requested
94418 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
94419 int nRem; /* Bytes remaining to copy */
94420
94421 /* Extend the p->aAlloc[] allocation if required. */
94422 if( p->nAlloc<nByte ){
94423 u8 *aNew;
94424 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
94425 while( nByte>nNew ) nNew = nNew*2;
94426 aNew = sqlite3Realloc(p->aAlloc, nNew);
94427 if( !aNew ) return SQLITE_NOMEM_BKPT;
94428 p->nAlloc = nNew;
94429 p->aAlloc = aNew;
94430 }
94431
94432 /* Copy as much data as is available in the buffer into the start of
94433 ** p->aAlloc[]. */
94434 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
94435 p->iReadOff += nAvail;
94436 nRem = nByte - nAvail;
94437
94438 /* The following loop copies up to p->nBuffer bytes per iteration into
94439 ** the p->aAlloc[] buffer. */
94440 while( nRem>0 ){
94441 int rc; /* vdbePmaReadBlob() return code */
94442 int nCopy; /* Number of bytes to copy */
94443 u8 *aNext; /* Pointer to buffer to copy data from */
94444
94445 nCopy = nRem;
94446 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
94447 rc = vdbePmaReadBlob(p, nCopy, &aNext);
94448 if( rc!=SQLITE_OK ) return rc;
94449 assert( aNext!=p->aAlloc );
94450 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
94451 nRem -= nCopy;
94452 }
94453
94454 *ppOut = p->aAlloc;
94455 }
94456
94457 return SQLITE_OK;
94458}
94459
94460/*
94461** Read a varint from the stream of data accessed by p. Set *pnOut to
94462** the value read.
94463*/
94464static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
94465 int iBuf;
94466
94467 if( p->aMap ){
94468 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
94469 }else{
94470 iBuf = p->iReadOff % p->nBuffer;
94471 if( iBuf && (p->nBuffer-iBuf)>=9 ){
94472 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
94473 }else{
94474 u8 aVarint[16], *a;
94475 int i = 0, rc;
94476 do{
94477 rc = vdbePmaReadBlob(p, 1, &a);
94478 if( rc ) return rc;
94479 aVarint[(i++)&0xf] = a[0];
94480 }while( (a[0]&0x80)!=0 );
94481 sqlite3GetVarint(aVarint, pnOut);
94482 }
94483 }
94484
94485 return SQLITE_OK;
94486}
94487
94488/*
94489** Attempt to memory map file pFile. If successful, set *pp to point to the
94490** new mapping and return SQLITE_OK. If the mapping is not attempted
94491** (because the file is too large or the VFS layer is configured not to use
94492** mmap), return SQLITE_OK and set *pp to NULL.
94493**
94494** Or, if an error occurs, return an SQLite error code. The final value of
94495** *pp is undefined in this case.
94496*/
94497static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
94498 int rc = SQLITE_OK;
94499 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
94500 sqlite3_file *pFd = pFile->pFd;
94501 if( pFd->pMethods->iVersion>=3 ){
94502 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
94503 testcase( rc!=SQLITE_OK );
94504 }
94505 }
94506 return rc;
94507}
94508
94509/*
94510** Attach PmaReader pReadr to file pFile (if it is not already attached to
94511** that file) and seek it to offset iOff within the file. Return SQLITE_OK
94512** if successful, or an SQLite error code if an error occurs.
94513*/
94514static int vdbePmaReaderSeek(
94515 SortSubtask *pTask, /* Task context */
94516 PmaReader *pReadr, /* Reader whose cursor is to be moved */
94517 SorterFile *pFile, /* Sorter file to read from */
94518 i64 iOff /* Offset in pFile */
94519){
94520 int rc = SQLITE_OK;
94521
94522 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
94523
94524 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
94525 if( pReadr->aMap ){
94526 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
94527 pReadr->aMap = 0;
94528 }
94529 pReadr->iReadOff = iOff;
94530 pReadr->iEof = pFile->iEof;
94531 pReadr->pFd = pFile->pFd;
94532
94533 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
94534 if( rc==SQLITE_OK && pReadr->aMap==0 ){
94535 int pgsz = pTask->pSorter->pgsz;
94536 int iBuf = pReadr->iReadOff % pgsz;
94537 if( pReadr->aBuffer==0 ){
94538 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
94539 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
94540 pReadr->nBuffer = pgsz;
94541 }
94542 if( rc==SQLITE_OK && iBuf ){
94543 int nRead = pgsz - iBuf;
94544 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
94545 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
94546 }
94547 rc = sqlite3OsRead(
94548 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
94549 );
94550 testcase( rc!=SQLITE_OK );
94551 }
94552 }
94553
94554 return rc;
94555}
94556
94557/*
94558** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
94559** no error occurs, or an SQLite error code if one does.
94560*/
94561static int vdbePmaReaderNext(PmaReader *pReadr){
94562 int rc = SQLITE_OK; /* Return Code */
94563 u64 nRec = 0; /* Size of record in bytes */
94564
94565
94566 if( pReadr->iReadOff>=pReadr->iEof ){
94567 IncrMerger *pIncr = pReadr->pIncr;
94568 int bEof = 1;
94569 if( pIncr ){
94570 rc = vdbeIncrSwap(pIncr);
94571 if( rc==SQLITE_OK && pIncr->bEof==0 ){
94572 rc = vdbePmaReaderSeek(
94573 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
94574 );
94575 bEof = 0;
94576 }
94577 }
94578
94579 if( bEof ){
94580 /* This is an EOF condition */
94581 vdbePmaReaderClear(pReadr);
94582 testcase( rc!=SQLITE_OK );
94583 return rc;
94584 }
94585 }
94586
94587 if( rc==SQLITE_OK ){
94588 rc = vdbePmaReadVarint(pReadr, &nRec);
94589 }
94590 if( rc==SQLITE_OK ){
94591 pReadr->nKey = (int)nRec;
94592 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
94593 testcase( rc!=SQLITE_OK );
94594 }
94595
94596 return rc;
94597}
94598
94599/*
94600** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
94601** starting at offset iStart and ending at offset iEof-1. This function
94602** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
94603** PMA is empty).
94604**
94605** If the pnByte parameter is NULL, then it is assumed that the file
94606** contains a single PMA, and that that PMA omits the initial length varint.
94607*/
94608static int vdbePmaReaderInit(
94609 SortSubtask *pTask, /* Task context */
94610 SorterFile *pFile, /* Sorter file to read from */
94611 i64 iStart, /* Start offset in pFile */
94612 PmaReader *pReadr, /* PmaReader to populate */
94613 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
94614){
94615 int rc;
94616
94617 assert( pFile->iEof>iStart );
94618 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
94619 assert( pReadr->aBuffer==0 );
94620 assert( pReadr->aMap==0 );
94621
94622 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
94623 if( rc==SQLITE_OK ){
94624 u64 nByte = 0; /* Size of PMA in bytes */
94625 rc = vdbePmaReadVarint(pReadr, &nByte);
94626 pReadr->iEof = pReadr->iReadOff + nByte;
94627 *pnByte += nByte;
94628 }
94629
94630 if( rc==SQLITE_OK ){
94631 rc = vdbePmaReaderNext(pReadr);
94632 }
94633 return rc;
94634}
94635
94636/*
94637** A version of vdbeSorterCompare() that assumes that it has already been
94638** determined that the first field of key1 is equal to the first field of
94639** key2.
94640*/
94641static int vdbeSorterCompareTail(
94642 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
94643 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
94644 const void *pKey1, int nKey1, /* Left side of comparison */
94645 const void *pKey2, int nKey2 /* Right side of comparison */
94646){
94647 UnpackedRecord *r2 = pTask->pUnpacked;
94648 if( *pbKey2Cached==0 ){
94649 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
94650 *pbKey2Cached = 1;
94651 }
94652 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
94653}
94654
94655/*
94656** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
94657** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
94658** used by the comparison. Return the result of the comparison.
94659**
94660** If IN/OUT parameter *pbKey2Cached is true when this function is called,
94661** it is assumed that (pTask->pUnpacked) contains the unpacked version
94662** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
94663** version of key2 and *pbKey2Cached set to true before returning.
94664**
94665** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
94666** to SQLITE_NOMEM.
94667*/
94668static int vdbeSorterCompare(
94669 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
94670 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
94671 const void *pKey1, int nKey1, /* Left side of comparison */
94672 const void *pKey2, int nKey2 /* Right side of comparison */
94673){
94674 UnpackedRecord *r2 = pTask->pUnpacked;
94675 if( !*pbKey2Cached ){
94676 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
94677 *pbKey2Cached = 1;
94678 }
94679 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
94680}
94681
94682/*
94683** A specially optimized version of vdbeSorterCompare() that assumes that
94684** the first field of each key is a TEXT value and that the collation
94685** sequence to compare them with is BINARY.
94686*/
94687static int vdbeSorterCompareText(
94688 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
94689 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
94690 const void *pKey1, int nKey1, /* Left side of comparison */
94691 const void *pKey2, int nKey2 /* Right side of comparison */
94692){
94693 const u8 * const p1 = (const u8 * const)pKey1;
94694 const u8 * const p2 = (const u8 * const)pKey2;
94695 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
94696 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
94697
94698 int n1;
94699 int n2;
94700 int res;
94701
94702 getVarint32NR(&p1[1], n1);
94703 getVarint32NR(&p2[1], n2);
94704 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
94705 if( res==0 ){
94706 res = n1 - n2;
94707 }
94708
94709 if( res==0 ){
94710 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
94711 res = vdbeSorterCompareTail(
94712 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
94713 );
94714 }
94715 }else{
94716 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
94717 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
94718 res = res * -1;
94719 }
94720 }
94721
94722 return res;
94723}
94724
94725/*
94726** A specially optimized version of vdbeSorterCompare() that assumes that
94727** the first field of each key is an INTEGER value.
94728*/
94729static int vdbeSorterCompareInt(
94730 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
94731 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
94732 const void *pKey1, int nKey1, /* Left side of comparison */
94733 const void *pKey2, int nKey2 /* Right side of comparison */
94734){
94735 const u8 * const p1 = (const u8 * const)pKey1;
94736 const u8 * const p2 = (const u8 * const)pKey2;
94737 const int s1 = p1[1]; /* Left hand serial type */
94738 const int s2 = p2[1]; /* Right hand serial type */
94739 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
94740 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
94741 int res; /* Return value */
94742
94743 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
94744 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
94745
94746 if( s1==s2 ){
94747 /* The two values have the same sign. Compare using memcmp(). */
94748 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
94749 const u8 n = aLen[s1];
94750 int i;
94751 res = 0;
94752 for(i=0; i<n; i++){
94753 if( (res = v1[i] - v2[i])!=0 ){
94754 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
94755 res = v1[0] & 0x80 ? -1 : +1;
94756 }
94757 break;
94758 }
94759 }
94760 }else if( s1>7 && s2>7 ){
94761 res = s1 - s2;
94762 }else{
94763 if( s2>7 ){
94764 res = +1;
94765 }else if( s1>7 ){
94766 res = -1;
94767 }else{
94768 res = s1 - s2;
94769 }
94770 assert( res!=0 );
94771
94772 if( res>0 ){
94773 if( *v1 & 0x80 ) res = -1;
94774 }else{
94775 if( *v2 & 0x80 ) res = +1;
94776 }
94777 }
94778
94779 if( res==0 ){
94780 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
94781 res = vdbeSorterCompareTail(
94782 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
94783 );
94784 }
94785 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
94786 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
94787 res = res * -1;
94788 }
94789
94790 return res;
94791}
94792
94793/*
94794** Initialize the temporary index cursor just opened as a sorter cursor.
94795**
94796** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
94797** to determine the number of fields that should be compared from the
94798** records being sorted. However, if the value passed as argument nField
94799** is non-zero and the sorter is able to guarantee a stable sort, nField
94800** is used instead. This is used when sorting records for a CREATE INDEX
94801** statement. In this case, keys are always delivered to the sorter in
94802** order of the primary key, which happens to be make up the final part
94803** of the records being sorted. So if the sort is stable, there is never
94804** any reason to compare PK fields and they can be ignored for a small
94805** performance boost.
94806**
94807** The sorter can guarantee a stable sort when running in single-threaded
94808** mode, but not in multi-threaded mode.
94809**
94810** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
94811*/
94812SQLITE_PRIVATE int sqlite3VdbeSorterInit(
94813 sqlite3 *db, /* Database connection (for malloc()) */
94814 int nField, /* Number of key fields in each record */
94815 VdbeCursor *pCsr /* Cursor that holds the new sorter */
94816){
94817 int pgsz; /* Page size of main database */
94818 int i; /* Used to iterate through aTask[] */
94819 VdbeSorter *pSorter; /* The new sorter */
94820 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
94821 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
94822 int sz; /* Size of pSorter in bytes */
94823 int rc = SQLITE_OK;
94824#if SQLITE_MAX_WORKER_THREADS==0
94825# define nWorker 0
94826#else
94827 int nWorker;
94828#endif
94829
94830 /* Initialize the upper limit on the number of worker threads */
94831#if SQLITE_MAX_WORKER_THREADS>0
94832 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
94833 nWorker = 0;
94834 }else{
94835 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
94836 }
94837#endif
94838
94839 /* Do not allow the total number of threads (main thread + all workers)
94840 ** to exceed the maximum merge count */
94841#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
94842 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
94843 nWorker = SORTER_MAX_MERGE_COUNT-1;
94844 }
94845#endif
94846
94847 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
94848 assert( pCsr->eCurType==CURTYPE_SORTER );
94849 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
94850 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
94851
94852 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
94853 pCsr->uc.pSorter = pSorter;
94854 if( pSorter==0 ){
94855 rc = SQLITE_NOMEM_BKPT;
94856 }else{
94857 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
94858 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
94859 pKeyInfo->db = 0;
94860 if( nField && nWorker==0 ){
94861 pKeyInfo->nKeyField = nField;
94862 }
94863 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
94864 pSorter->nTask = nWorker + 1;
94865 pSorter->iPrev = (u8)(nWorker - 1);
94866 pSorter->bUseThreads = (pSorter->nTask>1);
94867 pSorter->db = db;
94868 for(i=0; i<pSorter->nTask; i++){
94869 SortSubtask *pTask = &pSorter->aTask[i];
94870 pTask->pSorter = pSorter;
94871 }
94872
94873 if( !sqlite3TempInMemory(db) ){
94874 i64 mxCache; /* Cache size in bytes*/
94875 u32 szPma = sqlite3GlobalConfig.szPma;
94876 pSorter->mnPmaSize = szPma * pgsz;
94877
94878 mxCache = db->aDb[0].pSchema->cache_size;
94879 if( mxCache<0 ){
94880 /* A negative cache-size value C indicates that the cache is abs(C)
94881 ** KiB in size. */
94882 mxCache = mxCache * -1024;
94883 }else{
94884 mxCache = mxCache * pgsz;
94885 }
94886 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
94887 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
94888
94889 /* Avoid large memory allocations if the application has requested
94890 ** SQLITE_CONFIG_SMALL_MALLOC. */
94891 if( sqlite3GlobalConfig.bSmallMalloc==0 ){
94892 assert( pSorter->iMemory==0 );
94893 pSorter->nMemory = pgsz;
94894 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
94895 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
94896 }
94897 }
94898
94899 if( pKeyInfo->nAllField<13
94900 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
94901 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
94902 ){
94903 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
94904 }
94905 }
94906
94907 return rc;
94908}
94909#undef nWorker /* Defined at the top of this function */
94910
94911/*
94912** Free the list of sorted records starting at pRecord.
94913*/
94914static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
94915 SorterRecord *p;
94916 SorterRecord *pNext;
94917 for(p=pRecord; p; p=pNext){
94918 pNext = p->u.pNext;
94919 sqlite3DbFree(db, p);
94920 }
94921}
94922
94923/*
94924** Free all resources owned by the object indicated by argument pTask. All
94925** fields of *pTask are zeroed before returning.
94926*/
94927static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
94928 sqlite3DbFree(db, pTask->pUnpacked);
94929#if SQLITE_MAX_WORKER_THREADS>0
94930 /* pTask->list.aMemory can only be non-zero if it was handed memory
94931 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
94932 if( pTask->list.aMemory ){
94933 sqlite3_free(pTask->list.aMemory);
94934 }else
94935#endif
94936 {
94937 assert( pTask->list.aMemory==0 );
94938 vdbeSorterRecordFree(0, pTask->list.pList);
94939 }
94940 if( pTask->file.pFd ){
94941 sqlite3OsCloseFree(pTask->file.pFd);
94942 }
94943 if( pTask->file2.pFd ){
94944 sqlite3OsCloseFree(pTask->file2.pFd);
94945 }
94946 memset(pTask, 0, sizeof(SortSubtask));
94947}
94948
94949#ifdef SQLITE_DEBUG_SORTER_THREADS
94950static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
94951 i64 t;
94952 int iTask = (pTask - pTask->pSorter->aTask);
94953 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94954 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
94955}
94956static void vdbeSorterRewindDebug(const char *zEvent){
94957 i64 t;
94958 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
94959 fprintf(stderr, "%lld:X %s\n", t, zEvent);
94960}
94961static void vdbeSorterPopulateDebug(
94962 SortSubtask *pTask,
94963 const char *zEvent
94964){
94965 i64 t;
94966 int iTask = (pTask - pTask->pSorter->aTask);
94967 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94968 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
94969}
94970static void vdbeSorterBlockDebug(
94971 SortSubtask *pTask,
94972 int bBlocked,
94973 const char *zEvent
94974){
94975 if( bBlocked ){
94976 i64 t;
94977 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
94978 fprintf(stderr, "%lld:main %s\n", t, zEvent);
94979 }
94980}
94981#else
94982# define vdbeSorterWorkDebug(x,y)
94983# define vdbeSorterRewindDebug(y)
94984# define vdbeSorterPopulateDebug(x,y)
94985# define vdbeSorterBlockDebug(x,y,z)
94986#endif
94987
94988#if SQLITE_MAX_WORKER_THREADS>0
94989/*
94990** Join thread pTask->thread.
94991*/
94992static int vdbeSorterJoinThread(SortSubtask *pTask){
94993 int rc = SQLITE_OK;
94994 if( pTask->pThread ){
94995#ifdef SQLITE_DEBUG_SORTER_THREADS
94996 int bDone = pTask->bDone;
94997#endif
94998 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
94999 vdbeSorterBlockDebug(pTask, !bDone, "enter");
95000 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
95001 vdbeSorterBlockDebug(pTask, !bDone, "exit");
95002 rc = SQLITE_PTR_TO_INT(pRet);
95003 assert( pTask->bDone==1 );
95004 pTask->bDone = 0;
95005 pTask->pThread = 0;
95006 }
95007 return rc;
95008}
95009
95010/*
95011** Launch a background thread to run xTask(pIn).
95012*/
95013static int vdbeSorterCreateThread(
95014 SortSubtask *pTask, /* Thread will use this task object */
95015 void *(*xTask)(void*), /* Routine to run in a separate thread */
95016 void *pIn /* Argument passed into xTask() */
95017){
95018 assert( pTask->pThread==0 && pTask->bDone==0 );
95019 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
95020}
95021
95022/*
95023** Join all outstanding threads launched by SorterWrite() to create
95024** level-0 PMAs.
95025*/
95026static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
95027 int rc = rcin;
95028 int i;
95029
95030 /* This function is always called by the main user thread.
95031 **
95032 ** If this function is being called after SorterRewind() has been called,
95033 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
95034 ** is currently attempt to join one of the other threads. To avoid a race
95035 ** condition where this thread also attempts to join the same object, join
95036 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
95037 for(i=pSorter->nTask-1; i>=0; i--){
95038 SortSubtask *pTask = &pSorter->aTask[i];
95039 int rc2 = vdbeSorterJoinThread(pTask);
95040 if( rc==SQLITE_OK ) rc = rc2;
95041 }
95042 return rc;
95043}
95044#else
95045# define vdbeSorterJoinAll(x,rcin) (rcin)
95046# define vdbeSorterJoinThread(pTask) SQLITE_OK
95047#endif
95048
95049/*
95050** Allocate a new MergeEngine object capable of handling up to
95051** nReader PmaReader inputs.
95052**
95053** nReader is automatically rounded up to the next power of two.
95054** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
95055*/
95056static MergeEngine *vdbeMergeEngineNew(int nReader){
95057 int N = 2; /* Smallest power of two >= nReader */
95058 int nByte; /* Total bytes of space to allocate */
95059 MergeEngine *pNew; /* Pointer to allocated object to return */
95060
95061 assert( nReader<=SORTER_MAX_MERGE_COUNT );
95062
95063 while( N<nReader ) N += N;
95064 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
95065
95066 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
95067 if( pNew ){
95068 pNew->nTree = N;
95069 pNew->pTask = 0;
95070 pNew->aReadr = (PmaReader*)&pNew[1];
95071 pNew->aTree = (int*)&pNew->aReadr[N];
95072 }
95073 return pNew;
95074}
95075
95076/*
95077** Free the MergeEngine object passed as the only argument.
95078*/
95079static void vdbeMergeEngineFree(MergeEngine *pMerger){
95080 int i;
95081 if( pMerger ){
95082 for(i=0; i<pMerger->nTree; i++){
95083 vdbePmaReaderClear(&pMerger->aReadr[i]);
95084 }
95085 }
95086 sqlite3_free(pMerger);
95087}
95088
95089/*
95090** Free all resources associated with the IncrMerger object indicated by
95091** the first argument.
95092*/
95093static void vdbeIncrFree(IncrMerger *pIncr){
95094 if( pIncr ){
95095#if SQLITE_MAX_WORKER_THREADS>0
95096 if( pIncr->bUseThread ){
95097 vdbeSorterJoinThread(pIncr->pTask);
95098 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
95099 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
95100 }
95101#endif
95102 vdbeMergeEngineFree(pIncr->pMerger);
95103 sqlite3_free(pIncr);
95104 }
95105}
95106
95107/*
95108** Reset a sorting cursor back to its original empty state.
95109*/
95110SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
95111 int i;
95112 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
95113 assert( pSorter->bUseThreads || pSorter->pReader==0 );
95114#if SQLITE_MAX_WORKER_THREADS>0
95115 if( pSorter->pReader ){
95116 vdbePmaReaderClear(pSorter->pReader);
95117 sqlite3DbFree(db, pSorter->pReader);
95118 pSorter->pReader = 0;
95119 }
95120#endif
95121 vdbeMergeEngineFree(pSorter->pMerger);
95122 pSorter->pMerger = 0;
95123 for(i=0; i<pSorter->nTask; i++){
95124 SortSubtask *pTask = &pSorter->aTask[i];
95125 vdbeSortSubtaskCleanup(db, pTask);
95126 pTask->pSorter = pSorter;
95127 }
95128 if( pSorter->list.aMemory==0 ){
95129 vdbeSorterRecordFree(0, pSorter->list.pList);
95130 }
95131 pSorter->list.pList = 0;
95132 pSorter->list.szPMA = 0;
95133 pSorter->bUsePMA = 0;
95134 pSorter->iMemory = 0;
95135 pSorter->mxKeysize = 0;
95136 sqlite3DbFree(db, pSorter->pUnpacked);
95137 pSorter->pUnpacked = 0;
95138}
95139
95140/*
95141** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
95142*/
95143SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
95144 VdbeSorter *pSorter;
95145 assert( pCsr->eCurType==CURTYPE_SORTER );
95146 pSorter = pCsr->uc.pSorter;
95147 if( pSorter ){
95148 sqlite3VdbeSorterReset(db, pSorter);
95149 sqlite3_free(pSorter->list.aMemory);
95150 sqlite3DbFree(db, pSorter);
95151 pCsr->uc.pSorter = 0;
95152 }
95153}
95154
95155#if SQLITE_MAX_MMAP_SIZE>0
95156/*
95157** The first argument is a file-handle open on a temporary file. The file
95158** is guaranteed to be nByte bytes or smaller in size. This function
95159** attempts to extend the file to nByte bytes in size and to ensure that
95160** the VFS has memory mapped it.
95161**
95162** Whether or not the file does end up memory mapped of course depends on
95163** the specific VFS implementation.
95164*/
95165static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
95166 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
95167 void *p = 0;
95168 int chunksize = 4*1024;
95169 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
95170 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
95171 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
95172 sqlite3OsUnfetch(pFd, 0, p);
95173 }
95174}
95175#else
95176# define vdbeSorterExtendFile(x,y,z)
95177#endif
95178
95179/*
95180** Allocate space for a file-handle and open a temporary file. If successful,
95181** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
95182** Otherwise, set *ppFd to 0 and return an SQLite error code.
95183*/
95184static int vdbeSorterOpenTempFile(
95185 sqlite3 *db, /* Database handle doing sort */
95186 i64 nExtend, /* Attempt to extend file to this size */
95187 sqlite3_file **ppFd
95188){
95189 int rc;
95190 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
95191 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
95192 SQLITE_OPEN_TEMP_JOURNAL |
95193 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
95194 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
95195 );
95196 if( rc==SQLITE_OK ){
95197 i64 max = SQLITE_MAX_MMAP_SIZE;
95198 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
95199 if( nExtend>0 ){
95200 vdbeSorterExtendFile(db, *ppFd, nExtend);
95201 }
95202 }
95203 return rc;
95204}
95205
95206/*
95207** If it has not already been allocated, allocate the UnpackedRecord
95208** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
95209** if no allocation was required), or SQLITE_NOMEM otherwise.
95210*/
95211static int vdbeSortAllocUnpacked(SortSubtask *pTask){
95212 if( pTask->pUnpacked==0 ){
95213 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
95214 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
95215 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
95216 pTask->pUnpacked->errCode = 0;
95217 }
95218 return SQLITE_OK;
95219}
95220
95221
95222/*
95223** Merge the two sorted lists p1 and p2 into a single list.
95224*/
95225static SorterRecord *vdbeSorterMerge(
95226 SortSubtask *pTask, /* Calling thread context */
95227 SorterRecord *p1, /* First list to merge */
95228 SorterRecord *p2 /* Second list to merge */
95229){
95230 SorterRecord *pFinal = 0;
95231 SorterRecord **pp = &pFinal;
95232 int bCached = 0;
95233
95234 assert( p1!=0 && p2!=0 );
95235 for(;;){
95236 int res;
95237 res = pTask->xCompare(
95238 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
95239 );
95240
95241 if( res<=0 ){
95242 *pp = p1;
95243 pp = &p1->u.pNext;
95244 p1 = p1->u.pNext;
95245 if( p1==0 ){
95246 *pp = p2;
95247 break;
95248 }
95249 }else{
95250 *pp = p2;
95251 pp = &p2->u.pNext;
95252 p2 = p2->u.pNext;
95253 bCached = 0;
95254 if( p2==0 ){
95255 *pp = p1;
95256 break;
95257 }
95258 }
95259 }
95260 return pFinal;
95261}
95262
95263/*
95264** Return the SorterCompare function to compare values collected by the
95265** sorter object passed as the only argument.
95266*/
95267static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
95268 if( p->typeMask==SORTER_TYPE_INTEGER ){
95269 return vdbeSorterCompareInt;
95270 }else if( p->typeMask==SORTER_TYPE_TEXT ){
95271 return vdbeSorterCompareText;
95272 }
95273 return vdbeSorterCompare;
95274}
95275
95276/*
95277** Sort the linked list of records headed at pTask->pList. Return
95278** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
95279** an error occurs.
95280*/
95281static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
95282 int i;
95283 SorterRecord *p;
95284 int rc;
95285 SorterRecord *aSlot[64];
95286
95287 rc = vdbeSortAllocUnpacked(pTask);
95288 if( rc!=SQLITE_OK ) return rc;
95289
95290 p = pList->pList;
95291 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
95292 memset(aSlot, 0, sizeof(aSlot));
95293
95294 while( p ){
95295 SorterRecord *pNext;
95296 if( pList->aMemory ){
95297 if( (u8*)p==pList->aMemory ){
95298 pNext = 0;
95299 }else{
95300 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
95301 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
95302 }
95303 }else{
95304 pNext = p->u.pNext;
95305 }
95306
95307 p->u.pNext = 0;
95308 for(i=0; aSlot[i]; i++){
95309 p = vdbeSorterMerge(pTask, p, aSlot[i]);
95310 aSlot[i] = 0;
95311 }
95312 aSlot[i] = p;
95313 p = pNext;
95314 }
95315
95316 p = 0;
95317 for(i=0; i<ArraySize(aSlot); i++){
95318 if( aSlot[i]==0 ) continue;
95319 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
95320 }
95321 pList->pList = p;
95322
95323 assert( pTask->pUnpacked->errCode==SQLITE_OK
95324 || pTask->pUnpacked->errCode==SQLITE_NOMEM
95325 );
95326 return pTask->pUnpacked->errCode;
95327}
95328
95329/*
95330** Initialize a PMA-writer object.
95331*/
95332static void vdbePmaWriterInit(
95333 sqlite3_file *pFd, /* File handle to write to */
95334 PmaWriter *p, /* Object to populate */
95335 int nBuf, /* Buffer size */
95336 i64 iStart /* Offset of pFd to begin writing at */
95337){
95338 memset(p, 0, sizeof(PmaWriter));
95339 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
95340 if( !p->aBuffer ){
95341 p->eFWErr = SQLITE_NOMEM_BKPT;
95342 }else{
95343 p->iBufEnd = p->iBufStart = (iStart % nBuf);
95344 p->iWriteOff = iStart - p->iBufStart;
95345 p->nBuffer = nBuf;
95346 p->pFd = pFd;
95347 }
95348}
95349
95350/*
95351** Write nData bytes of data to the PMA. Return SQLITE_OK
95352** if successful, or an SQLite error code if an error occurs.
95353*/
95354static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
95355 int nRem = nData;
95356 while( nRem>0 && p->eFWErr==0 ){
95357 int nCopy = nRem;
95358 if( nCopy>(p->nBuffer - p->iBufEnd) ){
95359 nCopy = p->nBuffer - p->iBufEnd;
95360 }
95361
95362 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
95363 p->iBufEnd += nCopy;
95364 if( p->iBufEnd==p->nBuffer ){
95365 p->eFWErr = sqlite3OsWrite(p->pFd,
95366 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
95367 p->iWriteOff + p->iBufStart
95368 );
95369 p->iBufStart = p->iBufEnd = 0;
95370 p->iWriteOff += p->nBuffer;
95371 }
95372 assert( p->iBufEnd<p->nBuffer );
95373
95374 nRem -= nCopy;
95375 }
95376}
95377
95378/*
95379** Flush any buffered data to disk and clean up the PMA-writer object.
95380** The results of using the PMA-writer after this call are undefined.
95381** Return SQLITE_OK if flushing the buffered data succeeds or is not
95382** required. Otherwise, return an SQLite error code.
95383**
95384** Before returning, set *piEof to the offset immediately following the
95385** last byte written to the file.
95386*/
95387static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
95388 int rc;
95389 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
95390 p->eFWErr = sqlite3OsWrite(p->pFd,
95391 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
95392 p->iWriteOff + p->iBufStart
95393 );
95394 }
95395 *piEof = (p->iWriteOff + p->iBufEnd);
95396 sqlite3_free(p->aBuffer);
95397 rc = p->eFWErr;
95398 memset(p, 0, sizeof(PmaWriter));
95399 return rc;
95400}
95401
95402/*
95403** Write value iVal encoded as a varint to the PMA. Return
95404** SQLITE_OK if successful, or an SQLite error code if an error occurs.
95405*/
95406static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
95407 int nByte;
95408 u8 aByte[10];
95409 nByte = sqlite3PutVarint(aByte, iVal);
95410 vdbePmaWriteBlob(p, aByte, nByte);
95411}
95412
95413/*
95414** Write the current contents of in-memory linked-list pList to a level-0
95415** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
95416** successful, or an SQLite error code otherwise.
95417**
95418** The format of a PMA is:
95419**
95420** * A varint. This varint contains the total number of bytes of content
95421** in the PMA (not including the varint itself).
95422**
95423** * One or more records packed end-to-end in order of ascending keys.
95424** Each record consists of a varint followed by a blob of data (the
95425** key). The varint is the number of bytes in the blob of data.
95426*/
95427static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
95428 sqlite3 *db = pTask->pSorter->db;
95429 int rc = SQLITE_OK; /* Return code */
95430 PmaWriter writer; /* Object used to write to the file */
95431
95432#ifdef SQLITE_DEBUG
95433 /* Set iSz to the expected size of file pTask->file after writing the PMA.
95434 ** This is used by an assert() statement at the end of this function. */
95435 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
95436#endif
95437
95438 vdbeSorterWorkDebug(pTask, "enter");
95439 memset(&writer, 0, sizeof(PmaWriter));
95440 assert( pList->szPMA>0 );
95441
95442 /* If the first temporary PMA file has not been opened, open it now. */
95443 if( pTask->file.pFd==0 ){
95444 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
95445 assert( rc!=SQLITE_OK || pTask->file.pFd );
95446 assert( pTask->file.iEof==0 );
95447 assert( pTask->nPMA==0 );
95448 }
95449
95450 /* Try to get the file to memory map */
95451 if( rc==SQLITE_OK ){
95452 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
95453 }
95454
95455 /* Sort the list */
95456 if( rc==SQLITE_OK ){
95457 rc = vdbeSorterSort(pTask, pList);
95458 }
95459
95460 if( rc==SQLITE_OK ){
95461 SorterRecord *p;
95462 SorterRecord *pNext = 0;
95463
95464 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
95465 pTask->file.iEof);
95466 pTask->nPMA++;
95467 vdbePmaWriteVarint(&writer, pList->szPMA);
95468 for(p=pList->pList; p; p=pNext){
95469 pNext = p->u.pNext;
95470 vdbePmaWriteVarint(&writer, p->nVal);
95471 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
95472 if( pList->aMemory==0 ) sqlite3_free(p);
95473 }
95474 pList->pList = p;
95475 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
95476 }
95477
95478 vdbeSorterWorkDebug(pTask, "exit");
95479 assert( rc!=SQLITE_OK || pList->pList==0 );
95480 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
95481 return rc;
95482}
95483
95484/*
95485** Advance the MergeEngine to its next entry.
95486** Set *pbEof to true there is no next entry because
95487** the MergeEngine has reached the end of all its inputs.
95488**
95489** Return SQLITE_OK if successful or an error code if an error occurs.
95490*/
95491static int vdbeMergeEngineStep(
95492 MergeEngine *pMerger, /* The merge engine to advance to the next row */
95493 int *pbEof /* Set TRUE at EOF. Set false for more content */
95494){
95495 int rc;
95496 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
95497 SortSubtask *pTask = pMerger->pTask;
95498
95499 /* Advance the current PmaReader */
95500 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
95501
95502 /* Update contents of aTree[] */
95503 if( rc==SQLITE_OK ){
95504 int i; /* Index of aTree[] to recalculate */
95505 PmaReader *pReadr1; /* First PmaReader to compare */
95506 PmaReader *pReadr2; /* Second PmaReader to compare */
95507 int bCached = 0;
95508
95509 /* Find the first two PmaReaders to compare. The one that was just
95510 ** advanced (iPrev) and the one next to it in the array. */
95511 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
95512 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
95513
95514 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
95515 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
95516 int iRes;
95517 if( pReadr1->pFd==0 ){
95518 iRes = +1;
95519 }else if( pReadr2->pFd==0 ){
95520 iRes = -1;
95521 }else{
95522 iRes = pTask->xCompare(pTask, &bCached,
95523 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
95524 );
95525 }
95526
95527 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
95528 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
95529 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
95530 ** pKey2 to point to the record belonging to pReadr2.
95531 **
95532 ** Alternatively, if pReadr2 contains the smaller of the two values,
95533 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
95534 ** was actually called above, then pTask->pUnpacked now contains
95535 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
95536 ** vdbeSorterCompare() from decoding pReadr2 again.
95537 **
95538 ** If the two values were equal, then the value from the oldest
95539 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
95540 ** is sorted from oldest to newest, so pReadr1 contains older values
95541 ** than pReadr2 iff (pReadr1<pReadr2). */
95542 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
95543 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
95544 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
95545 bCached = 0;
95546 }else{
95547 if( pReadr1->pFd ) bCached = 0;
95548 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
95549 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
95550 }
95551 }
95552 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
95553 }
95554
95555 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
95556}
95557
95558#if SQLITE_MAX_WORKER_THREADS>0
95559/*
95560** The main routine for background threads that write level-0 PMAs.
95561*/
95562static void *vdbeSorterFlushThread(void *pCtx){
95563 SortSubtask *pTask = (SortSubtask*)pCtx;
95564 int rc; /* Return code */
95565 assert( pTask->bDone==0 );
95566 rc = vdbeSorterListToPMA(pTask, &pTask->list);
95567 pTask->bDone = 1;
95568 return SQLITE_INT_TO_PTR(rc);
95569}
95570#endif /* SQLITE_MAX_WORKER_THREADS>0 */
95571
95572/*
95573** Flush the current contents of VdbeSorter.list to a new PMA, possibly
95574** using a background thread.
95575*/
95576static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
95577#if SQLITE_MAX_WORKER_THREADS==0
95578 pSorter->bUsePMA = 1;
95579 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
95580#else
95581 int rc = SQLITE_OK;
95582 int i;
95583 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
95584 int nWorker = (pSorter->nTask-1);
95585
95586 /* Set the flag to indicate that at least one PMA has been written.
95587 ** Or will be, anyhow. */
95588 pSorter->bUsePMA = 1;
95589
95590 /* Select a sub-task to sort and flush the current list of in-memory
95591 ** records to disk. If the sorter is running in multi-threaded mode,
95592 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
95593 ** the background thread from a sub-tasks previous turn is still running,
95594 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
95595 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
95596 ** sub-tasks are prefered as they use background threads - the final
95597 ** sub-task uses the main thread. */
95598 for(i=0; i<nWorker; i++){
95599 int iTest = (pSorter->iPrev + i + 1) % nWorker;
95600 pTask = &pSorter->aTask[iTest];
95601 if( pTask->bDone ){
95602 rc = vdbeSorterJoinThread(pTask);
95603 }
95604 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
95605 }
95606
95607 if( rc==SQLITE_OK ){
95608 if( i==nWorker ){
95609 /* Use the foreground thread for this operation */
95610 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
95611 }else{
95612 /* Launch a background thread for this operation */
95613 u8 *aMem;
95614 void *pCtx;
95615
95616 assert( pTask!=0 );
95617 assert( pTask->pThread==0 && pTask->bDone==0 );
95618 assert( pTask->list.pList==0 );
95619 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
95620
95621 aMem = pTask->list.aMemory;
95622 pCtx = (void*)pTask;
95623 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
95624 pTask->list = pSorter->list;
95625 pSorter->list.pList = 0;
95626 pSorter->list.szPMA = 0;
95627 if( aMem ){
95628 pSorter->list.aMemory = aMem;
95629 pSorter->nMemory = sqlite3MallocSize(aMem);
95630 }else if( pSorter->list.aMemory ){
95631 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
95632 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
95633 }
95634
95635 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
95636 }
95637 }
95638
95639 return rc;
95640#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
95641}
95642
95643/*
95644** Add a record to the sorter.
95645*/
95646SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
95647 const VdbeCursor *pCsr, /* Sorter cursor */
95648 Mem *pVal /* Memory cell containing record */
95649){
95650 VdbeSorter *pSorter;
95651 int rc = SQLITE_OK; /* Return Code */
95652 SorterRecord *pNew; /* New list element */
95653 int bFlush; /* True to flush contents of memory to PMA */
95654 int nReq; /* Bytes of memory required */
95655 int nPMA; /* Bytes of PMA space required */
95656 int t; /* serial type of first record field */
95657
95658 assert( pCsr->eCurType==CURTYPE_SORTER );
95659 pSorter = pCsr->uc.pSorter;
95660 getVarint32NR((const u8*)&pVal->z[1], t);
95661 if( t>0 && t<10 && t!=7 ){
95662 pSorter->typeMask &= SORTER_TYPE_INTEGER;
95663 }else if( t>10 && (t & 0x01) ){
95664 pSorter->typeMask &= SORTER_TYPE_TEXT;
95665 }else{
95666 pSorter->typeMask = 0;
95667 }
95668
95669 assert( pSorter );
95670
95671 /* Figure out whether or not the current contents of memory should be
95672 ** flushed to a PMA before continuing. If so, do so.
95673 **
95674 ** If using the single large allocation mode (pSorter->aMemory!=0), then
95675 ** flush the contents of memory to a new PMA if (a) at least one value is
95676 ** already in memory and (b) the new value will not fit in memory.
95677 **
95678 ** Or, if using separate allocations for each record, flush the contents
95679 ** of memory to a PMA if either of the following are true:
95680 **
95681 ** * The total memory allocated for the in-memory list is greater
95682 ** than (page-size * cache-size), or
95683 **
95684 ** * The total memory allocated for the in-memory list is greater
95685 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
95686 */
95687 nReq = pVal->n + sizeof(SorterRecord);
95688 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
95689 if( pSorter->mxPmaSize ){
95690 if( pSorter->list.aMemory ){
95691 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
95692 }else{
95693 bFlush = (
95694 (pSorter->list.szPMA > pSorter->mxPmaSize)
95695 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
95696 );
95697 }
95698 if( bFlush ){
95699 rc = vdbeSorterFlushPMA(pSorter);
95700 pSorter->list.szPMA = 0;
95701 pSorter->iMemory = 0;
95702 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
95703 }
95704 }
95705
95706 pSorter->list.szPMA += nPMA;
95707 if( nPMA>pSorter->mxKeysize ){
95708 pSorter->mxKeysize = nPMA;
95709 }
95710
95711 if( pSorter->list.aMemory ){
95712 int nMin = pSorter->iMemory + nReq;
95713
95714 if( nMin>pSorter->nMemory ){
95715 u8 *aNew;
95716 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
95717 int iListOff = -1;
95718 if( pSorter->list.pList ){
95719 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
95720 }
95721 while( nNew < nMin ) nNew = nNew*2;
95722 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
95723 if( nNew < nMin ) nNew = nMin;
95724 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
95725 if( !aNew ) return SQLITE_NOMEM_BKPT;
95726 if( iListOff>=0 ){
95727 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
95728 }
95729 pSorter->list.aMemory = aNew;
95730 pSorter->nMemory = nNew;
95731 }
95732
95733 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
95734 pSorter->iMemory += ROUND8(nReq);
95735 if( pSorter->list.pList ){
95736 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
95737 }
95738 }else{
95739 pNew = (SorterRecord *)sqlite3Malloc(nReq);
95740 if( pNew==0 ){
95741 return SQLITE_NOMEM_BKPT;
95742 }
95743 pNew->u.pNext = pSorter->list.pList;
95744 }
95745
95746 memcpy(SRVAL(pNew), pVal->z, pVal->n);
95747 pNew->nVal = pVal->n;
95748 pSorter->list.pList = pNew;
95749
95750 return rc;
95751}
95752
95753/*
95754** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
95755** of the data stored in aFile[1] is the same as that used by regular PMAs,
95756** except that the number-of-bytes varint is omitted from the start.
95757*/
95758static int vdbeIncrPopulate(IncrMerger *pIncr){
95759 int rc = SQLITE_OK;
95760 int rc2;
95761 i64 iStart = pIncr->iStartOff;
95762 SorterFile *pOut = &pIncr->aFile[1];
95763 SortSubtask *pTask = pIncr->pTask;
95764 MergeEngine *pMerger = pIncr->pMerger;
95765 PmaWriter writer;
95766 assert( pIncr->bEof==0 );
95767
95768 vdbeSorterPopulateDebug(pTask, "enter");
95769
95770 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
95771 while( rc==SQLITE_OK ){
95772 int dummy;
95773 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
95774 int nKey = pReader->nKey;
95775 i64 iEof = writer.iWriteOff + writer.iBufEnd;
95776
95777 /* Check if the output file is full or if the input has been exhausted.
95778 ** In either case exit the loop. */
95779 if( pReader->pFd==0 ) break;
95780 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
95781
95782 /* Write the next key to the output. */
95783 vdbePmaWriteVarint(&writer, nKey);
95784 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
95785 assert( pIncr->pMerger->pTask==pTask );
95786 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
95787 }
95788
95789 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
95790 if( rc==SQLITE_OK ) rc = rc2;
95791 vdbeSorterPopulateDebug(pTask, "exit");
95792 return rc;
95793}
95794
95795#if SQLITE_MAX_WORKER_THREADS>0
95796/*
95797** The main routine for background threads that populate aFile[1] of
95798** multi-threaded IncrMerger objects.
95799*/
95800static void *vdbeIncrPopulateThread(void *pCtx){
95801 IncrMerger *pIncr = (IncrMerger*)pCtx;
95802 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
95803 pIncr->pTask->bDone = 1;
95804 return pRet;
95805}
95806
95807/*
95808** Launch a background thread to populate aFile[1] of pIncr.
95809*/
95810static int vdbeIncrBgPopulate(IncrMerger *pIncr){
95811 void *p = (void*)pIncr;
95812 assert( pIncr->bUseThread );
95813 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
95814}
95815#endif
95816
95817/*
95818** This function is called when the PmaReader corresponding to pIncr has
95819** finished reading the contents of aFile[0]. Its purpose is to "refill"
95820** aFile[0] such that the PmaReader should start rereading it from the
95821** beginning.
95822**
95823** For single-threaded objects, this is accomplished by literally reading
95824** keys from pIncr->pMerger and repopulating aFile[0].
95825**
95826** For multi-threaded objects, all that is required is to wait until the
95827** background thread is finished (if it is not already) and then swap
95828** aFile[0] and aFile[1] in place. If the contents of pMerger have not
95829** been exhausted, this function also launches a new background thread
95830** to populate the new aFile[1].
95831**
95832** SQLITE_OK is returned on success, or an SQLite error code otherwise.
95833*/
95834static int vdbeIncrSwap(IncrMerger *pIncr){
95835 int rc = SQLITE_OK;
95836
95837#if SQLITE_MAX_WORKER_THREADS>0
95838 if( pIncr->bUseThread ){
95839 rc = vdbeSorterJoinThread(pIncr->pTask);
95840
95841 if( rc==SQLITE_OK ){
95842 SorterFile f0 = pIncr->aFile[0];
95843 pIncr->aFile[0] = pIncr->aFile[1];
95844 pIncr->aFile[1] = f0;
95845 }
95846
95847 if( rc==SQLITE_OK ){
95848 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
95849 pIncr->bEof = 1;
95850 }else{
95851 rc = vdbeIncrBgPopulate(pIncr);
95852 }
95853 }
95854 }else
95855#endif
95856 {
95857 rc = vdbeIncrPopulate(pIncr);
95858 pIncr->aFile[0] = pIncr->aFile[1];
95859 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
95860 pIncr->bEof = 1;
95861 }
95862 }
95863
95864 return rc;
95865}
95866
95867/*
95868** Allocate and return a new IncrMerger object to read data from pMerger.
95869**
95870** If an OOM condition is encountered, return NULL. In this case free the
95871** pMerger argument before returning.
95872*/
95873static int vdbeIncrMergerNew(
95874 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
95875 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
95876 IncrMerger **ppOut /* Write the new IncrMerger here */
95877){
95878 int rc = SQLITE_OK;
95879 IncrMerger *pIncr = *ppOut = (IncrMerger*)
95880 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
95881 if( pIncr ){
95882 pIncr->pMerger = pMerger;
95883 pIncr->pTask = pTask;
95884 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
95885 pTask->file2.iEof += pIncr->mxSz;
95886 }else{
95887 vdbeMergeEngineFree(pMerger);
95888 rc = SQLITE_NOMEM_BKPT;
95889 }
95890 return rc;
95891}
95892
95893#if SQLITE_MAX_WORKER_THREADS>0
95894/*
95895** Set the "use-threads" flag on object pIncr.
95896*/
95897static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
95898 pIncr->bUseThread = 1;
95899 pIncr->pTask->file2.iEof -= pIncr->mxSz;
95900}
95901#endif /* SQLITE_MAX_WORKER_THREADS>0 */
95902
95903
95904
95905/*
95906** Recompute pMerger->aTree[iOut] by comparing the next keys on the
95907** two PmaReaders that feed that entry. Neither of the PmaReaders
95908** are advanced. This routine merely does the comparison.
95909*/
95910static void vdbeMergeEngineCompare(
95911 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
95912 int iOut /* Store the result in pMerger->aTree[iOut] */
95913){
95914 int i1;
95915 int i2;
95916 int iRes;
95917 PmaReader *p1;
95918 PmaReader *p2;
95919
95920 assert( iOut<pMerger->nTree && iOut>0 );
95921
95922 if( iOut>=(pMerger->nTree/2) ){
95923 i1 = (iOut - pMerger->nTree/2) * 2;
95924 i2 = i1 + 1;
95925 }else{
95926 i1 = pMerger->aTree[iOut*2];
95927 i2 = pMerger->aTree[iOut*2+1];
95928 }
95929
95930 p1 = &pMerger->aReadr[i1];
95931 p2 = &pMerger->aReadr[i2];
95932
95933 if( p1->pFd==0 ){
95934 iRes = i2;
95935 }else if( p2->pFd==0 ){
95936 iRes = i1;
95937 }else{
95938 SortSubtask *pTask = pMerger->pTask;
95939 int bCached = 0;
95940 int res;
95941 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
95942 res = pTask->xCompare(
95943 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
95944 );
95945 if( res<=0 ){
95946 iRes = i1;
95947 }else{
95948 iRes = i2;
95949 }
95950 }
95951
95952 pMerger->aTree[iOut] = iRes;
95953}
95954
95955/*
95956** Allowed values for the eMode parameter to vdbeMergeEngineInit()
95957** and vdbePmaReaderIncrMergeInit().
95958**
95959** Only INCRINIT_NORMAL is valid in single-threaded builds (when
95960** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
95961** when there exists one or more separate worker threads.
95962*/
95963#define INCRINIT_NORMAL 0
95964#define INCRINIT_TASK 1
95965#define INCRINIT_ROOT 2
95966
95967/*
95968** Forward reference required as the vdbeIncrMergeInit() and
95969** vdbePmaReaderIncrInit() routines are called mutually recursively when
95970** building a merge tree.
95971*/
95972static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
95973
95974/*
95975** Initialize the MergeEngine object passed as the second argument. Once this
95976** function returns, the first key of merged data may be read from the
95977** MergeEngine object in the usual fashion.
95978**
95979** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
95980** objects attached to the PmaReader objects that the merger reads from have
95981** already been populated, but that they have not yet populated aFile[0] and
95982** set the PmaReader objects up to read from it. In this case all that is
95983** required is to call vdbePmaReaderNext() on each PmaReader to point it at
95984** its first key.
95985**
95986** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
95987** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
95988** to pMerger.
95989**
95990** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
95991*/
95992static int vdbeMergeEngineInit(
95993 SortSubtask *pTask, /* Thread that will run pMerger */
95994 MergeEngine *pMerger, /* MergeEngine to initialize */
95995 int eMode /* One of the INCRINIT_XXX constants */
95996){
95997 int rc = SQLITE_OK; /* Return code */
95998 int i; /* For looping over PmaReader objects */
95999 int nTree; /* Number of subtrees to merge */
96000
96001 /* Failure to allocate the merge would have been detected prior to
96002 ** invoking this routine */
96003 assert( pMerger!=0 );
96004
96005 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
96006 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
96007
96008 /* Verify that the MergeEngine is assigned to a single thread */
96009 assert( pMerger->pTask==0 );
96010 pMerger->pTask = pTask;
96011
96012 nTree = pMerger->nTree;
96013 for(i=0; i<nTree; i++){
96014 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
96015 /* PmaReaders should be normally initialized in order, as if they are
96016 ** reading from the same temp file this makes for more linear file IO.
96017 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
96018 ** in use it will block the vdbePmaReaderNext() call while it uses
96019 ** the main thread to fill its buffer. So calling PmaReaderNext()
96020 ** on this PmaReader before any of the multi-threaded PmaReaders takes
96021 ** better advantage of multi-processor hardware. */
96022 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
96023 }else{
96024 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
96025 }
96026 if( rc!=SQLITE_OK ) return rc;
96027 }
96028
96029 for(i=pMerger->nTree-1; i>0; i--){
96030 vdbeMergeEngineCompare(pMerger, i);
96031 }
96032 return pTask->pUnpacked->errCode;
96033}
96034
96035/*
96036** The PmaReader passed as the first argument is guaranteed to be an
96037** incremental-reader (pReadr->pIncr!=0). This function serves to open
96038** and/or initialize the temp file related fields of the IncrMerge
96039** object at (pReadr->pIncr).
96040**
96041** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
96042** in the sub-tree headed by pReadr are also initialized. Data is then
96043** loaded into the buffers belonging to pReadr and it is set to point to
96044** the first key in its range.
96045**
96046** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
96047** to be a multi-threaded PmaReader and this function is being called in a
96048** background thread. In this case all PmaReaders in the sub-tree are
96049** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
96050** pReadr is populated. However, pReadr itself is not set up to point
96051** to its first key. A call to vdbePmaReaderNext() is still required to do
96052** that.
96053**
96054** The reason this function does not call vdbePmaReaderNext() immediately
96055** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
96056** to block on thread (pTask->thread) before accessing aFile[1]. But, since
96057** this entire function is being run by thread (pTask->thread), that will
96058** lead to the current background thread attempting to join itself.
96059**
96060** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
96061** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
96062** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
96063** In this case vdbePmaReaderNext() is called on all child PmaReaders and
96064** the current PmaReader set to point to the first key in its range.
96065**
96066** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
96067*/
96068static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
96069 int rc = SQLITE_OK;
96070 IncrMerger *pIncr = pReadr->pIncr;
96071 SortSubtask *pTask = pIncr->pTask;
96072 sqlite3 *db = pTask->pSorter->db;
96073
96074 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
96075 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
96076
96077 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
96078
96079 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
96080 ** requires two temp files to itself, whereas a single-threaded object
96081 ** only requires a region of pTask->file2. */
96082 if( rc==SQLITE_OK ){
96083 int mxSz = pIncr->mxSz;
96084#if SQLITE_MAX_WORKER_THREADS>0
96085 if( pIncr->bUseThread ){
96086 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
96087 if( rc==SQLITE_OK ){
96088 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
96089 }
96090 }else
96091#endif
96092 /*if( !pIncr->bUseThread )*/{
96093 if( pTask->file2.pFd==0 ){
96094 assert( pTask->file2.iEof>0 );
96095 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
96096 pTask->file2.iEof = 0;
96097 }
96098 if( rc==SQLITE_OK ){
96099 pIncr->aFile[1].pFd = pTask->file2.pFd;
96100 pIncr->iStartOff = pTask->file2.iEof;
96101 pTask->file2.iEof += mxSz;
96102 }
96103 }
96104 }
96105
96106#if SQLITE_MAX_WORKER_THREADS>0
96107 if( rc==SQLITE_OK && pIncr->bUseThread ){
96108 /* Use the current thread to populate aFile[1], even though this
96109 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
96110 ** then this function is already running in background thread
96111 ** pIncr->pTask->thread.
96112 **
96113 ** If this is the INCRINIT_ROOT object, then it is running in the
96114 ** main VDBE thread. But that is Ok, as that thread cannot return
96115 ** control to the VDBE or proceed with anything useful until the
96116 ** first results are ready from this merger object anyway.
96117 */
96118 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
96119 rc = vdbeIncrPopulate(pIncr);
96120 }
96121#endif
96122
96123 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
96124 rc = vdbePmaReaderNext(pReadr);
96125 }
96126
96127 return rc;
96128}
96129
96130#if SQLITE_MAX_WORKER_THREADS>0
96131/*
96132** The main routine for vdbePmaReaderIncrMergeInit() operations run in
96133** background threads.
96134*/
96135static void *vdbePmaReaderBgIncrInit(void *pCtx){
96136 PmaReader *pReader = (PmaReader*)pCtx;
96137 void *pRet = SQLITE_INT_TO_PTR(
96138 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
96139 );
96140 pReader->pIncr->pTask->bDone = 1;
96141 return pRet;
96142}
96143#endif
96144
96145/*
96146** If the PmaReader passed as the first argument is not an incremental-reader
96147** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
96148** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
96149** this routine to initialize the incremental merge.
96150**
96151** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
96152** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
96153** Or, if the IncrMerger is single threaded, the same function is called
96154** using the current thread.
96155*/
96156static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
96157 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
96158 int rc = SQLITE_OK; /* Return code */
96159 if( pIncr ){
96160#if SQLITE_MAX_WORKER_THREADS>0
96161 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
96162 if( pIncr->bUseThread ){
96163 void *pCtx = (void*)pReadr;
96164 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
96165 }else
96166#endif
96167 {
96168 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
96169 }
96170 }
96171 return rc;
96172}
96173
96174/*
96175** Allocate a new MergeEngine object to merge the contents of nPMA level-0
96176** PMAs from pTask->file. If no error occurs, set *ppOut to point to
96177** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
96178** to NULL and return an SQLite error code.
96179**
96180** When this function is called, *piOffset is set to the offset of the
96181** first PMA to read from pTask->file. Assuming no error occurs, it is
96182** set to the offset immediately following the last byte of the last
96183** PMA before returning. If an error does occur, then the final value of
96184** *piOffset is undefined.
96185*/
96186static int vdbeMergeEngineLevel0(
96187 SortSubtask *pTask, /* Sorter task to read from */
96188 int nPMA, /* Number of PMAs to read */
96189 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
96190 MergeEngine **ppOut /* OUT: New merge-engine */
96191){
96192 MergeEngine *pNew; /* Merge engine to return */
96193 i64 iOff = *piOffset;
96194 int i;
96195 int rc = SQLITE_OK;
96196
96197 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
96198 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
96199
96200 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
96201 i64 nDummy = 0;
96202 PmaReader *pReadr = &pNew->aReadr[i];
96203 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
96204 iOff = pReadr->iEof;
96205 }
96206
96207 if( rc!=SQLITE_OK ){
96208 vdbeMergeEngineFree(pNew);
96209 *ppOut = 0;
96210 }
96211 *piOffset = iOff;
96212 return rc;
96213}
96214
96215/*
96216** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
96217** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
96218**
96219** i.e.
96220**
96221** nPMA<=16 -> TreeDepth() == 0
96222** nPMA<=256 -> TreeDepth() == 1
96223** nPMA<=65536 -> TreeDepth() == 2
96224*/
96225static int vdbeSorterTreeDepth(int nPMA){
96226 int nDepth = 0;
96227 i64 nDiv = SORTER_MAX_MERGE_COUNT;
96228 while( nDiv < (i64)nPMA ){
96229 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
96230 nDepth++;
96231 }
96232 return nDepth;
96233}
96234
96235/*
96236** pRoot is the root of an incremental merge-tree with depth nDepth (according
96237** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
96238** tree, counting from zero. This function adds pLeaf to the tree.
96239**
96240** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
96241** code is returned and pLeaf is freed.
96242*/
96243static int vdbeSorterAddToTree(
96244 SortSubtask *pTask, /* Task context */
96245 int nDepth, /* Depth of tree according to TreeDepth() */
96246 int iSeq, /* Sequence number of leaf within tree */
96247 MergeEngine *pRoot, /* Root of tree */
96248 MergeEngine *pLeaf /* Leaf to add to tree */
96249){
96250 int rc = SQLITE_OK;
96251 int nDiv = 1;
96252 int i;
96253 MergeEngine *p = pRoot;
96254 IncrMerger *pIncr;
96255
96256 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
96257
96258 for(i=1; i<nDepth; i++){
96259 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
96260 }
96261
96262 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
96263 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
96264 PmaReader *pReadr = &p->aReadr[iIter];
96265
96266 if( pReadr->pIncr==0 ){
96267 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
96268 if( pNew==0 ){
96269 rc = SQLITE_NOMEM_BKPT;
96270 }else{
96271 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
96272 }
96273 }
96274 if( rc==SQLITE_OK ){
96275 p = pReadr->pIncr->pMerger;
96276 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
96277 }
96278 }
96279
96280 if( rc==SQLITE_OK ){
96281 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
96282 }else{
96283 vdbeIncrFree(pIncr);
96284 }
96285 return rc;
96286}
96287
96288/*
96289** This function is called as part of a SorterRewind() operation on a sorter
96290** that has already written two or more level-0 PMAs to one or more temp
96291** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
96292** can be used to incrementally merge all PMAs on disk.
96293**
96294** If successful, SQLITE_OK is returned and *ppOut set to point to the
96295** MergeEngine object at the root of the tree before returning. Or, if an
96296** error occurs, an SQLite error code is returned and the final value
96297** of *ppOut is undefined.
96298*/
96299static int vdbeSorterMergeTreeBuild(
96300 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
96301 MergeEngine **ppOut /* Write the MergeEngine here */
96302){
96303 MergeEngine *pMain = 0;
96304 int rc = SQLITE_OK;
96305 int iTask;
96306
96307#if SQLITE_MAX_WORKER_THREADS>0
96308 /* If the sorter uses more than one task, then create the top-level
96309 ** MergeEngine here. This MergeEngine will read data from exactly
96310 ** one PmaReader per sub-task. */
96311 assert( pSorter->bUseThreads || pSorter->nTask==1 );
96312 if( pSorter->nTask>1 ){
96313 pMain = vdbeMergeEngineNew(pSorter->nTask);
96314 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
96315 }
96316#endif
96317
96318 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
96319 SortSubtask *pTask = &pSorter->aTask[iTask];
96320 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
96321 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
96322 MergeEngine *pRoot = 0; /* Root node of tree for this task */
96323 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
96324 i64 iReadOff = 0;
96325
96326 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
96327 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
96328 }else{
96329 int i;
96330 int iSeq = 0;
96331 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
96332 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
96333 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
96334 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
96335 int nReader; /* Number of level-0 PMAs to merge */
96336
96337 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
96338 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
96339 if( rc==SQLITE_OK ){
96340 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
96341 }
96342 }
96343 }
96344
96345 if( rc==SQLITE_OK ){
96346#if SQLITE_MAX_WORKER_THREADS>0
96347 if( pMain!=0 ){
96348 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
96349 }else
96350#endif
96351 {
96352 assert( pMain==0 );
96353 pMain = pRoot;
96354 }
96355 }else{
96356 vdbeMergeEngineFree(pRoot);
96357 }
96358 }
96359 }
96360
96361 if( rc!=SQLITE_OK ){
96362 vdbeMergeEngineFree(pMain);
96363 pMain = 0;
96364 }
96365 *ppOut = pMain;
96366 return rc;
96367}
96368
96369/*
96370** This function is called as part of an sqlite3VdbeSorterRewind() operation
96371** on a sorter that has written two or more PMAs to temporary files. It sets
96372** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
96373** (for multi-threaded sorters) so that it can be used to iterate through
96374** all records stored in the sorter.
96375**
96376** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
96377*/
96378static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
96379 int rc; /* Return code */
96380 SortSubtask *pTask0 = &pSorter->aTask[0];
96381 MergeEngine *pMain = 0;
96382#if SQLITE_MAX_WORKER_THREADS
96383 sqlite3 *db = pTask0->pSorter->db;
96384 int i;
96385 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
96386 for(i=0; i<pSorter->nTask; i++){
96387 pSorter->aTask[i].xCompare = xCompare;
96388 }
96389#endif
96390
96391 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
96392 if( rc==SQLITE_OK ){
96393#if SQLITE_MAX_WORKER_THREADS
96394 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
96395 if( pSorter->bUseThreads ){
96396 int iTask;
96397 PmaReader *pReadr = 0;
96398 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
96399 rc = vdbeSortAllocUnpacked(pLast);
96400 if( rc==SQLITE_OK ){
96401 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
96402 pSorter->pReader = pReadr;
96403 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
96404 }
96405 if( rc==SQLITE_OK ){
96406 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
96407 if( rc==SQLITE_OK ){
96408 vdbeIncrMergerSetThreads(pReadr->pIncr);
96409 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
96410 IncrMerger *pIncr;
96411 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
96412 vdbeIncrMergerSetThreads(pIncr);
96413 assert( pIncr->pTask!=pLast );
96414 }
96415 }
96416 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
96417 /* Check that:
96418 **
96419 ** a) The incremental merge object is configured to use the
96420 ** right task, and
96421 ** b) If it is using task (nTask-1), it is configured to run
96422 ** in single-threaded mode. This is important, as the
96423 ** root merge (INCRINIT_ROOT) will be using the same task
96424 ** object.
96425 */
96426 PmaReader *p = &pMain->aReadr[iTask];
96427 assert( p->pIncr==0 || (
96428 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
96429 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
96430 ));
96431 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
96432 }
96433 }
96434 pMain = 0;
96435 }
96436 if( rc==SQLITE_OK ){
96437 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
96438 }
96439 }else
96440#endif
96441 {
96442 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
96443 pSorter->pMerger = pMain;
96444 pMain = 0;
96445 }
96446 }
96447
96448 if( rc!=SQLITE_OK ){
96449 vdbeMergeEngineFree(pMain);
96450 }
96451 return rc;
96452}
96453
96454
96455/*
96456** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
96457** this function is called to prepare for iterating through the records
96458** in sorted order.
96459*/
96460SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
96461 VdbeSorter *pSorter;
96462 int rc = SQLITE_OK; /* Return code */
96463
96464 assert( pCsr->eCurType==CURTYPE_SORTER );
96465 pSorter = pCsr->uc.pSorter;
96466 assert( pSorter );
96467
96468 /* If no data has been written to disk, then do not do so now. Instead,
96469 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
96470 ** from the in-memory list. */
96471 if( pSorter->bUsePMA==0 ){
96472 if( pSorter->list.pList ){
96473 *pbEof = 0;
96474 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
96475 }else{
96476 *pbEof = 1;
96477 }
96478 return rc;
96479 }
96480
96481 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
96482 ** function flushes the contents of memory to disk, it immediately always
96483 ** creates a new list consisting of a single key immediately afterwards.
96484 ** So the list is never empty at this point. */
96485 assert( pSorter->list.pList );
96486 rc = vdbeSorterFlushPMA(pSorter);
96487
96488 /* Join all threads */
96489 rc = vdbeSorterJoinAll(pSorter, rc);
96490
96491 vdbeSorterRewindDebug("rewind");
96492
96493 /* Assuming no errors have occurred, set up a merger structure to
96494 ** incrementally read and merge all remaining PMAs. */
96495 assert( pSorter->pReader==0 );
96496 if( rc==SQLITE_OK ){
96497 rc = vdbeSorterSetupMerge(pSorter);
96498 *pbEof = 0;
96499 }
96500
96501 vdbeSorterRewindDebug("rewinddone");
96502 return rc;
96503}
96504
96505/*
96506** Advance to the next element in the sorter. Return value:
96507**
96508** SQLITE_OK success
96509** SQLITE_DONE end of data
96510** otherwise some kind of error.
96511*/
96512SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
96513 VdbeSorter *pSorter;
96514 int rc; /* Return code */
96515
96516 assert( pCsr->eCurType==CURTYPE_SORTER );
96517 pSorter = pCsr->uc.pSorter;
96518 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
96519 if( pSorter->bUsePMA ){
96520 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
96521 assert( pSorter->bUseThreads==0 || pSorter->pReader );
96522 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
96523#if SQLITE_MAX_WORKER_THREADS>0
96524 if( pSorter->bUseThreads ){
96525 rc = vdbePmaReaderNext(pSorter->pReader);
96526 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
96527 }else
96528#endif
96529 /*if( !pSorter->bUseThreads )*/ {
96530 int res = 0;
96531 assert( pSorter->pMerger!=0 );
96532 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
96533 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
96534 if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
96535 }
96536 }else{
96537 SorterRecord *pFree = pSorter->list.pList;
96538 pSorter->list.pList = pFree->u.pNext;
96539 pFree->u.pNext = 0;
96540 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
96541 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
96542 }
96543 return rc;
96544}
96545
96546/*
96547** Return a pointer to a buffer owned by the sorter that contains the
96548** current key.
96549*/
96550static void *vdbeSorterRowkey(
96551 const VdbeSorter *pSorter, /* Sorter object */
96552 int *pnKey /* OUT: Size of current key in bytes */
96553){
96554 void *pKey;
96555 if( pSorter->bUsePMA ){
96556 PmaReader *pReader;
96557#if SQLITE_MAX_WORKER_THREADS>0
96558 if( pSorter->bUseThreads ){
96559 pReader = pSorter->pReader;
96560 }else
96561#endif
96562 /*if( !pSorter->bUseThreads )*/{
96563 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
96564 }
96565 *pnKey = pReader->nKey;
96566 pKey = pReader->aKey;
96567 }else{
96568 *pnKey = pSorter->list.pList->nVal;
96569 pKey = SRVAL(pSorter->list.pList);
96570 }
96571 return pKey;
96572}
96573
96574/*
96575** Copy the current sorter key into the memory cell pOut.
96576*/
96577SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
96578 VdbeSorter *pSorter;
96579 void *pKey; int nKey; /* Sorter key to copy into pOut */
96580
96581 assert( pCsr->eCurType==CURTYPE_SORTER );
96582 pSorter = pCsr->uc.pSorter;
96583 pKey = vdbeSorterRowkey(pSorter, &nKey);
96584 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
96585 return SQLITE_NOMEM_BKPT;
96586 }
96587 pOut->n = nKey;
96588 MemSetTypeFlag(pOut, MEM_Blob);
96589 memcpy(pOut->z, pKey, nKey);
96590
96591 return SQLITE_OK;
96592}
96593
96594/*
96595** Compare the key in memory cell pVal with the key that the sorter cursor
96596** passed as the first argument currently points to. For the purposes of
96597** the comparison, ignore the rowid field at the end of each record.
96598**
96599** If the sorter cursor key contains any NULL values, consider it to be
96600** less than pVal. Even if pVal also contains NULL values.
96601**
96602** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
96603** Otherwise, set *pRes to a negative, zero or positive value if the
96604** key in pVal is smaller than, equal to or larger than the current sorter
96605** key.
96606**
96607** This routine forms the core of the OP_SorterCompare opcode, which in
96608** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
96609*/
96610SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
96611 const VdbeCursor *pCsr, /* Sorter cursor */
96612 Mem *pVal, /* Value to compare to current sorter key */
96613 int nKeyCol, /* Compare this many columns */
96614 int *pRes /* OUT: Result of comparison */
96615){
96616 VdbeSorter *pSorter;
96617 UnpackedRecord *r2;
96618 KeyInfo *pKeyInfo;
96619 int i;
96620 void *pKey; int nKey; /* Sorter key to compare pVal with */
96621
96622 assert( pCsr->eCurType==CURTYPE_SORTER );
96623 pSorter = pCsr->uc.pSorter;
96624 r2 = pSorter->pUnpacked;
96625 pKeyInfo = pCsr->pKeyInfo;
96626 if( r2==0 ){
96627 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
96628 if( r2==0 ) return SQLITE_NOMEM_BKPT;
96629 r2->nField = nKeyCol;
96630 }
96631 assert( r2->nField==nKeyCol );
96632
96633 pKey = vdbeSorterRowkey(pSorter, &nKey);
96634 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
96635 for(i=0; i<nKeyCol; i++){
96636 if( r2->aMem[i].flags & MEM_Null ){
96637 *pRes = -1;
96638 return SQLITE_OK;
96639 }
96640 }
96641
96642 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
96643 return SQLITE_OK;
96644}
96645
96646/************** End of vdbesort.c ********************************************/
96647/************** Begin file vdbevtab.c ****************************************/
96648/*
96649** 2020-03-23
96650**
96651** The author disclaims copyright to this source code. In place of
96652** a legal notice, here is a blessing:
96653**
96654** May you do good and not evil.
96655** May you find forgiveness for yourself and forgive others.
96656** May you share freely, never taking more than you give.
96657**
96658*************************************************************************
96659**
96660** This file implements virtual-tables for examining the bytecode content
96661** of a prepared statement.
96662*/
96663/* #include "sqliteInt.h" */
96664#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
96665/* #include "vdbeInt.h" */
96666
96667/* An instance of the bytecode() table-valued function.
96668*/
96669typedef struct bytecodevtab bytecodevtab;
96670struct bytecodevtab {
96671 sqlite3_vtab base; /* Base class - must be first */
96672 sqlite3 *db; /* Database connection */
96673 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
96674};
96675
96676/* A cursor for scanning through the bytecode
96677*/
96678typedef struct bytecodevtab_cursor bytecodevtab_cursor;
96679struct bytecodevtab_cursor {
96680 sqlite3_vtab_cursor base; /* Base class - must be first */
96681 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
96682 int iRowid; /* The rowid of the output table */
96683 int iAddr; /* Address */
96684 int needFinalize; /* Cursors owns pStmt and must finalize it */
96685 int showSubprograms; /* Provide a listing of subprograms */
96686 Op *aOp; /* Operand array */
96687 char *zP4; /* Rendered P4 value */
96688 const char *zType; /* tables_used.type */
96689 const char *zSchema; /* tables_used.schema */
96690 const char *zName; /* tables_used.name */
96691 Mem sub; /* Subprograms */
96692};
96693
96694/*
96695** Create a new bytecode() table-valued function.
96696*/
96697static int bytecodevtabConnect(
96698 sqlite3 *db,
96699 void *pAux,
96700 int argc, const char *const*argv,
96701 sqlite3_vtab **ppVtab,
96702 char **pzErr
96703){
96704 bytecodevtab *pNew;
96705 int rc;
96706 int isTabUsed = pAux!=0;
96707 const char *azSchema[2] = {
96708 /* bytecode() schema */
96709 "CREATE TABLE x("
96710 "addr INT,"
96711 "opcode TEXT,"
96712 "p1 INT,"
96713 "p2 INT,"
96714 "p3 INT,"
96715 "p4 TEXT,"
96716 "p5 INT,"
96717 "comment TEXT,"
96718 "subprog TEXT,"
96719 "stmt HIDDEN"
96720 ");",
96721
96722 /* Tables_used() schema */
96723 "CREATE TABLE x("
96724 "type TEXT,"
96725 "schema TEXT,"
96726 "name TEXT,"
96727 "wr INT,"
96728 "subprog TEXT,"
96729 "stmt HIDDEN"
96730 ");"
96731 };
96732
96733 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
96734 if( rc==SQLITE_OK ){
96735 pNew = sqlite3_malloc( sizeof(*pNew) );
96736 *ppVtab = (sqlite3_vtab*)pNew;
96737 if( pNew==0 ) return SQLITE_NOMEM;
96738 memset(pNew, 0, sizeof(*pNew));
96739 pNew->db = db;
96740 pNew->bTablesUsed = isTabUsed*2;
96741 }
96742 return rc;
96743}
96744
96745/*
96746** This method is the destructor for bytecodevtab objects.
96747*/
96748static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
96749 bytecodevtab *p = (bytecodevtab*)pVtab;
96750 sqlite3_free(p);
96751 return SQLITE_OK;
96752}
96753
96754/*
96755** Constructor for a new bytecodevtab_cursor object.
96756*/
96757static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
96758 bytecodevtab *pVTab = (bytecodevtab*)p;
96759 bytecodevtab_cursor *pCur;
96760 pCur = sqlite3_malloc( sizeof(*pCur) );
96761 if( pCur==0 ) return SQLITE_NOMEM;
96762 memset(pCur, 0, sizeof(*pCur));
96763 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
96764 *ppCursor = &pCur->base;
96765 return SQLITE_OK;
96766}
96767
96768/*
96769** Clear all internal content from a bytecodevtab cursor.
96770*/
96771static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
96772 sqlite3_free(pCur->zP4);
96773 pCur->zP4 = 0;
96774 sqlite3VdbeMemRelease(&pCur->sub);
96775 sqlite3VdbeMemSetNull(&pCur->sub);
96776 if( pCur->needFinalize ){
96777 sqlite3_finalize(pCur->pStmt);
96778 }
96779 pCur->pStmt = 0;
96780 pCur->needFinalize = 0;
96781 pCur->zType = 0;
96782 pCur->zSchema = 0;
96783 pCur->zName = 0;
96784}
96785
96786/*
96787** Destructor for a bytecodevtab_cursor.
96788*/
96789static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
96790 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96791 bytecodevtabCursorClear(pCur);
96792 sqlite3_free(pCur);
96793 return SQLITE_OK;
96794}
96795
96796
96797/*
96798** Advance a bytecodevtab_cursor to its next row of output.
96799*/
96800static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
96801 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96802 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
96803 int rc;
96804 if( pCur->zP4 ){
96805 sqlite3_free(pCur->zP4);
96806 pCur->zP4 = 0;
96807 }
96808 if( pCur->zName ){
96809 pCur->zName = 0;
96810 pCur->zType = 0;
96811 pCur->zSchema = 0;
96812 }
96813 rc = sqlite3VdbeNextOpcode(
96814 (Vdbe*)pCur->pStmt,
96815 pCur->showSubprograms ? &pCur->sub : 0,
96816 pTab->bTablesUsed,
96817 &pCur->iRowid,
96818 &pCur->iAddr,
96819 &pCur->aOp);
96820 if( rc!=SQLITE_OK ){
96821 sqlite3VdbeMemSetNull(&pCur->sub);
96822 pCur->aOp = 0;
96823 }
96824 return SQLITE_OK;
96825}
96826
96827/*
96828** Return TRUE if the cursor has been moved off of the last
96829** row of output.
96830*/
96831static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
96832 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96833 return pCur->aOp==0;
96834}
96835
96836/*
96837** Return values of columns for the row at which the bytecodevtab_cursor
96838** is currently pointing.
96839*/
96840static int bytecodevtabColumn(
96841 sqlite3_vtab_cursor *cur, /* The cursor */
96842 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
96843 int i /* Which column to return */
96844){
96845 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96846 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
96847 Op *pOp = pCur->aOp + pCur->iAddr;
96848 if( pVTab->bTablesUsed ){
96849 if( i==4 ){
96850 i = 8;
96851 }else{
96852 if( i<=2 && pCur->zType==0 ){
96853 Schema *pSchema;
96854 HashElem *k;
96855 int iDb = pOp->p3;
96856 Pgno iRoot = (Pgno)pOp->p2;
96857 sqlite3 *db = pVTab->db;
96858 pSchema = db->aDb[iDb].pSchema;
96859 pCur->zSchema = db->aDb[iDb].zDbSName;
96860 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
96861 Table *pTab = (Table*)sqliteHashData(k);
96862 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
96863 pCur->zName = pTab->zName;
96864 pCur->zType = "table";
96865 break;
96866 }
96867 }
96868 if( pCur->zName==0 ){
96869 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
96870 Index *pIdx = (Index*)sqliteHashData(k);
96871 if( pIdx->tnum==iRoot ){
96872 pCur->zName = pIdx->zName;
96873 pCur->zType = "index";
96874 }
96875 }
96876 }
96877 }
96878 i += 10;
96879 }
96880 }
96881 switch( i ){
96882 case 0: /* addr */
96883 sqlite3_result_int(ctx, pCur->iAddr);
96884 break;
96885 case 1: /* opcode */
96886 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
96887 -1, SQLITE_STATIC);
96888 break;
96889 case 2: /* p1 */
96890 sqlite3_result_int(ctx, pOp->p1);
96891 break;
96892 case 3: /* p2 */
96893 sqlite3_result_int(ctx, pOp->p2);
96894 break;
96895 case 4: /* p3 */
96896 sqlite3_result_int(ctx, pOp->p3);
96897 break;
96898 case 5: /* p4 */
96899 case 7: /* comment */
96900 if( pCur->zP4==0 ){
96901 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
96902 }
96903 if( i==5 ){
96904 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
96905 }else{
96906#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
96907 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
96908 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
96909#endif
96910 }
96911 break;
96912 case 6: /* p5 */
96913 sqlite3_result_int(ctx, pOp->p5);
96914 break;
96915 case 8: { /* subprog */
96916 Op *aOp = pCur->aOp;
96917 assert( aOp[0].opcode==OP_Init );
96918 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
96919 if( pCur->iRowid==pCur->iAddr+1 ){
96920 break; /* Result is NULL for the main program */
96921 }else if( aOp[0].p4.z!=0 ){
96922 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
96923 }else{
96924 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
96925 }
96926 break;
96927 }
96928 case 10: /* tables_used.type */
96929 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
96930 break;
96931 case 11: /* tables_used.schema */
96932 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
96933 break;
96934 case 12: /* tables_used.name */
96935 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
96936 break;
96937 case 13: /* tables_used.wr */
96938 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
96939 break;
96940 }
96941 return SQLITE_OK;
96942}
96943
96944/*
96945** Return the rowid for the current row. In this implementation, the
96946** rowid is the same as the output value.
96947*/
96948static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
96949 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
96950 *pRowid = pCur->iRowid;
96951 return SQLITE_OK;
96952}
96953
96954/*
96955** Initialize a cursor.
96956**
96957** idxNum==0 means show all subprograms
96958** idxNum==1 means show only the main bytecode and omit subprograms.
96959*/
96960static int bytecodevtabFilter(
96961 sqlite3_vtab_cursor *pVtabCursor,
96962 int idxNum, const char *idxStr,
96963 int argc, sqlite3_value **argv
96964){
96965 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
96966 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
96967 int rc = SQLITE_OK;
96968
96969 bytecodevtabCursorClear(pCur);
96970 pCur->iRowid = 0;
96971 pCur->iAddr = 0;
96972 pCur->showSubprograms = idxNum==0;
96973 assert( argc==1 );
96974 if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
96975 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
96976 if( zSql==0 ){
96977 rc = SQLITE_NOMEM;
96978 }else{
96979 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
96980 pCur->needFinalize = 1;
96981 }
96982 }else{
96983 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
96984 }
96985 if( pCur->pStmt==0 ){
96986 pVTab->base.zErrMsg = sqlite3_mprintf(
96987 "argument to %s() is not a valid SQL statement",
96988 pVTab->bTablesUsed ? "tables_used" : "bytecode"
96989 );
96990 rc = SQLITE_ERROR;
96991 }else{
96992 bytecodevtabNext(pVtabCursor);
96993 }
96994 return rc;
96995}
96996
96997/*
96998** We must have a single stmt=? constraint that will be passed through
96999** into the xFilter method. If there is no valid stmt=? constraint,
97000** then return an SQLITE_CONSTRAINT error.
97001*/
97002static int bytecodevtabBestIndex(
97003 sqlite3_vtab *tab,
97004 sqlite3_index_info *pIdxInfo
97005){
97006 int i;
97007 int rc = SQLITE_CONSTRAINT;
97008 struct sqlite3_index_constraint *p;
97009 bytecodevtab *pVTab = (bytecodevtab*)tab;
97010 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
97011 pIdxInfo->estimatedCost = (double)100;
97012 pIdxInfo->estimatedRows = 100;
97013 pIdxInfo->idxNum = 0;
97014 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
97015 if( p->usable==0 ) continue;
97016 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
97017 rc = SQLITE_OK;
97018 pIdxInfo->aConstraintUsage[i].omit = 1;
97019 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
97020 }
97021 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
97022 pIdxInfo->aConstraintUsage[i].omit = 1;
97023 pIdxInfo->idxNum = 1;
97024 }
97025 }
97026 return rc;
97027}
97028
97029/*
97030** This following structure defines all the methods for the
97031** virtual table.
97032*/
97033static sqlite3_module bytecodevtabModule = {
97034 /* iVersion */ 0,
97035 /* xCreate */ 0,
97036 /* xConnect */ bytecodevtabConnect,
97037 /* xBestIndex */ bytecodevtabBestIndex,
97038 /* xDisconnect */ bytecodevtabDisconnect,
97039 /* xDestroy */ 0,
97040 /* xOpen */ bytecodevtabOpen,
97041 /* xClose */ bytecodevtabClose,
97042 /* xFilter */ bytecodevtabFilter,
97043 /* xNext */ bytecodevtabNext,
97044 /* xEof */ bytecodevtabEof,
97045 /* xColumn */ bytecodevtabColumn,
97046 /* xRowid */ bytecodevtabRowid,
97047 /* xUpdate */ 0,
97048 /* xBegin */ 0,
97049 /* xSync */ 0,
97050 /* xCommit */ 0,
97051 /* xRollback */ 0,
97052 /* xFindMethod */ 0,
97053 /* xRename */ 0,
97054 /* xSavepoint */ 0,
97055 /* xRelease */ 0,
97056 /* xRollbackTo */ 0,
97057 /* xShadowName */ 0
97058};
97059
97060
97061SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
97062 int rc;
97063 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
97064 if( rc==SQLITE_OK ){
97065 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
97066 }
97067 return rc;
97068}
97069#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
97070SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
97071#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
97072
97073/************** End of vdbevtab.c ********************************************/
97074/************** Begin file memjournal.c **************************************/
97075/*
97076** 2008 October 7
97077**
97078** The author disclaims copyright to this source code. In place of
97079** a legal notice, here is a blessing:
97080**
97081** May you do good and not evil.
97082** May you find forgiveness for yourself and forgive others.
97083** May you share freely, never taking more than you give.
97084**
97085*************************************************************************
97086**
97087** This file contains code use to implement an in-memory rollback journal.
97088** The in-memory rollback journal is used to journal transactions for
97089** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
97090**
97091** Update: The in-memory journal is also used to temporarily cache
97092** smaller journals that are not critical for power-loss recovery.
97093** For example, statement journals that are not too big will be held
97094** entirely in memory, thus reducing the number of file I/O calls, and
97095** more importantly, reducing temporary file creation events. If these
97096** journals become too large for memory, they are spilled to disk. But
97097** in the common case, they are usually small and no file I/O needs to
97098** occur.
97099*/
97100/* #include "sqliteInt.h" */
97101
97102/* Forward references to internal structures */
97103typedef struct MemJournal MemJournal;
97104typedef struct FilePoint FilePoint;
97105typedef struct FileChunk FileChunk;
97106
97107/*
97108** The rollback journal is composed of a linked list of these structures.
97109**
97110** The zChunk array is always at least 8 bytes in size - usually much more.
97111** Its actual size is stored in the MemJournal.nChunkSize variable.
97112*/
97113struct FileChunk {
97114 FileChunk *pNext; /* Next chunk in the journal */
97115 u8 zChunk[8]; /* Content of this chunk */
97116};
97117
97118/*
97119** By default, allocate this many bytes of memory for each FileChunk object.
97120*/
97121#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
97122
97123/*
97124** For chunk size nChunkSize, return the number of bytes that should
97125** be allocated for each FileChunk structure.
97126*/
97127#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
97128
97129/*
97130** An instance of this object serves as a cursor into the rollback journal.
97131** The cursor can be either for reading or writing.
97132*/
97133struct FilePoint {
97134 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
97135 FileChunk *pChunk; /* Specific chunk into which cursor points */
97136};
97137
97138/*
97139** This structure is a subclass of sqlite3_file. Each open memory-journal
97140** is an instance of this class.
97141*/
97142struct MemJournal {
97143 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
97144 int nChunkSize; /* In-memory chunk-size */
97145
97146 int nSpill; /* Bytes of data before flushing */
97147 int nSize; /* Bytes of data currently in memory */
97148 FileChunk *pFirst; /* Head of in-memory chunk-list */
97149 FilePoint endpoint; /* Pointer to the end of the file */
97150 FilePoint readpoint; /* Pointer to the end of the last xRead() */
97151
97152 int flags; /* xOpen flags */
97153 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
97154 const char *zJournal; /* Name of the journal file */
97155};
97156
97157/*
97158** Read data from the in-memory journal file. This is the implementation
97159** of the sqlite3_vfs.xRead method.
97160*/
97161static int memjrnlRead(
97162 sqlite3_file *pJfd, /* The journal file from which to read */
97163 void *zBuf, /* Put the results here */
97164 int iAmt, /* Number of bytes to read */
97165 sqlite_int64 iOfst /* Begin reading at this offset */
97166){
97167 MemJournal *p = (MemJournal *)pJfd;
97168 u8 *zOut = zBuf;
97169 int nRead = iAmt;
97170 int iChunkOffset;
97171 FileChunk *pChunk;
97172
97173 if( (iAmt+iOfst)>p->endpoint.iOffset ){
97174 return SQLITE_IOERR_SHORT_READ;
97175 }
97176 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
97177 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
97178 sqlite3_int64 iOff = 0;
97179 for(pChunk=p->pFirst;
97180 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
97181 pChunk=pChunk->pNext
97182 ){
97183 iOff += p->nChunkSize;
97184 }
97185 }else{
97186 pChunk = p->readpoint.pChunk;
97187 assert( pChunk!=0 );
97188 }
97189
97190 iChunkOffset = (int)(iOfst%p->nChunkSize);
97191 do {
97192 int iSpace = p->nChunkSize - iChunkOffset;
97193 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
97194 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
97195 zOut += nCopy;
97196 nRead -= iSpace;
97197 iChunkOffset = 0;
97198 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
97199 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
97200 p->readpoint.pChunk = pChunk;
97201
97202 return SQLITE_OK;
97203}
97204
97205/*
97206** Free the list of FileChunk structures headed at MemJournal.pFirst.
97207*/
97208static void memjrnlFreeChunks(MemJournal *p){
97209 FileChunk *pIter;
97210 FileChunk *pNext;
97211 for(pIter=p->pFirst; pIter; pIter=pNext){
97212 pNext = pIter->pNext;
97213 sqlite3_free(pIter);
97214 }
97215 p->pFirst = 0;
97216}
97217
97218/*
97219** Flush the contents of memory to a real file on disk.
97220*/
97221static int memjrnlCreateFile(MemJournal *p){
97222 int rc;
97223 sqlite3_file *pReal = (sqlite3_file*)p;
97224 MemJournal copy = *p;
97225
97226 memset(p, 0, sizeof(MemJournal));
97227 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
97228 if( rc==SQLITE_OK ){
97229 int nChunk = copy.nChunkSize;
97230 i64 iOff = 0;
97231 FileChunk *pIter;
97232 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
97233 if( iOff + nChunk > copy.endpoint.iOffset ){
97234 nChunk = copy.endpoint.iOffset - iOff;
97235 }
97236 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
97237 if( rc ) break;
97238 iOff += nChunk;
97239 }
97240 if( rc==SQLITE_OK ){
97241 /* No error has occurred. Free the in-memory buffers. */
97242 memjrnlFreeChunks(&copy);
97243 }
97244 }
97245 if( rc!=SQLITE_OK ){
97246 /* If an error occurred while creating or writing to the file, restore
97247 ** the original before returning. This way, SQLite uses the in-memory
97248 ** journal data to roll back changes made to the internal page-cache
97249 ** before this function was called. */
97250 sqlite3OsClose(pReal);
97251 *p = copy;
97252 }
97253 return rc;
97254}
97255
97256
97257/*
97258** Write data to the file.
97259*/
97260static int memjrnlWrite(
97261 sqlite3_file *pJfd, /* The journal file into which to write */
97262 const void *zBuf, /* Take data to be written from here */
97263 int iAmt, /* Number of bytes to write */
97264 sqlite_int64 iOfst /* Begin writing at this offset into the file */
97265){
97266 MemJournal *p = (MemJournal *)pJfd;
97267 int nWrite = iAmt;
97268 u8 *zWrite = (u8 *)zBuf;
97269
97270 /* If the file should be created now, create it and write the new data
97271 ** into the file on disk. */
97272 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
97273 int rc = memjrnlCreateFile(p);
97274 if( rc==SQLITE_OK ){
97275 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
97276 }
97277 return rc;
97278 }
97279
97280 /* If the contents of this write should be stored in memory */
97281 else{
97282 /* An in-memory journal file should only ever be appended to. Random
97283 ** access writes are not required. The only exception to this is when
97284 ** the in-memory journal is being used by a connection using the
97285 ** atomic-write optimization. In this case the first 28 bytes of the
97286 ** journal file may be written as part of committing the transaction. */
97287 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
97288#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
97289 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
97290 if( iOfst==0 && p->pFirst ){
97291 assert( p->nChunkSize>iAmt );
97292 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
97293 }else
97294#else
97295 assert( iOfst>0 || p->pFirst==0 );
97296#endif
97297 {
97298 while( nWrite>0 ){
97299 FileChunk *pChunk = p->endpoint.pChunk;
97300 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
97301 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
97302
97303 if( iChunkOffset==0 ){
97304 /* New chunk is required to extend the file. */
97305 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
97306 if( !pNew ){
97307 return SQLITE_IOERR_NOMEM_BKPT;
97308 }
97309 pNew->pNext = 0;
97310 if( pChunk ){
97311 assert( p->pFirst );
97312 pChunk->pNext = pNew;
97313 }else{
97314 assert( !p->pFirst );
97315 p->pFirst = pNew;
97316 }
97317 p->endpoint.pChunk = pNew;
97318 }
97319
97320 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
97321 zWrite += iSpace;
97322 nWrite -= iSpace;
97323 p->endpoint.iOffset += iSpace;
97324 }
97325 p->nSize = iAmt + iOfst;
97326 }
97327 }
97328
97329 return SQLITE_OK;
97330}
97331
97332/*
97333** Truncate the file.
97334**
97335** If the journal file is already on disk, truncate it there. Or, if it
97336** is still in main memory but is being truncated to zero bytes in size,
97337** ignore
97338*/
97339static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
97340 MemJournal *p = (MemJournal *)pJfd;
97341 if( ALWAYS(size==0) ){
97342 memjrnlFreeChunks(p);
97343 p->nSize = 0;
97344 p->endpoint.pChunk = 0;
97345 p->endpoint.iOffset = 0;
97346 p->readpoint.pChunk = 0;
97347 p->readpoint.iOffset = 0;
97348 }
97349 return SQLITE_OK;
97350}
97351
97352/*
97353** Close the file.
97354*/
97355static int memjrnlClose(sqlite3_file *pJfd){
97356 MemJournal *p = (MemJournal *)pJfd;
97357 memjrnlFreeChunks(p);
97358 return SQLITE_OK;
97359}
97360
97361/*
97362** Sync the file.
97363**
97364** If the real file has been created, call its xSync method. Otherwise,
97365** syncing an in-memory journal is a no-op.
97366*/
97367static int memjrnlSync(sqlite3_file *pJfd, int flags){
97368 UNUSED_PARAMETER2(pJfd, flags);
97369 return SQLITE_OK;
97370}
97371
97372/*
97373** Query the size of the file in bytes.
97374*/
97375static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
97376 MemJournal *p = (MemJournal *)pJfd;
97377 *pSize = (sqlite_int64) p->endpoint.iOffset;
97378 return SQLITE_OK;
97379}
97380
97381/*
97382** Table of methods for MemJournal sqlite3_file object.
97383*/
97384static const struct sqlite3_io_methods MemJournalMethods = {
97385 1, /* iVersion */
97386 memjrnlClose, /* xClose */
97387 memjrnlRead, /* xRead */
97388 memjrnlWrite, /* xWrite */
97389 memjrnlTruncate, /* xTruncate */
97390 memjrnlSync, /* xSync */
97391 memjrnlFileSize, /* xFileSize */
97392 0, /* xLock */
97393 0, /* xUnlock */
97394 0, /* xCheckReservedLock */
97395 0, /* xFileControl */
97396 0, /* xSectorSize */
97397 0, /* xDeviceCharacteristics */
97398 0, /* xShmMap */
97399 0, /* xShmLock */
97400 0, /* xShmBarrier */
97401 0, /* xShmUnmap */
97402 0, /* xFetch */
97403 0 /* xUnfetch */
97404};
97405
97406/*
97407** Open a journal file.
97408**
97409** The behaviour of the journal file depends on the value of parameter
97410** nSpill. If nSpill is 0, then the journal file is always create and
97411** accessed using the underlying VFS. If nSpill is less than zero, then
97412** all content is always stored in main-memory. Finally, if nSpill is a
97413** positive value, then the journal file is initially created in-memory
97414** but may be flushed to disk later on. In this case the journal file is
97415** flushed to disk either when it grows larger than nSpill bytes in size,
97416** or when sqlite3JournalCreate() is called.
97417*/
97418SQLITE_PRIVATE int sqlite3JournalOpen(
97419 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
97420 const char *zName, /* Name of the journal file */
97421 sqlite3_file *pJfd, /* Preallocated, blank file handle */
97422 int flags, /* Opening flags */
97423 int nSpill /* Bytes buffered before opening the file */
97424){
97425 MemJournal *p = (MemJournal*)pJfd;
97426
97427 /* Zero the file-handle object. If nSpill was passed zero, initialize
97428 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
97429 ** case none of the code in this module is executed as a result of calls
97430 ** made on the journal file-handle. */
97431 memset(p, 0, sizeof(MemJournal));
97432 if( nSpill==0 ){
97433 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
97434 }
97435
97436 if( nSpill>0 ){
97437 p->nChunkSize = nSpill;
97438 }else{
97439 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
97440 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
97441 }
97442
97443 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
97444 p->nSpill = nSpill;
97445 p->flags = flags;
97446 p->zJournal = zName;
97447 p->pVfs = pVfs;
97448 return SQLITE_OK;
97449}
97450
97451/*
97452** Open an in-memory journal file.
97453*/
97454SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
97455 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
97456}
97457
97458#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
97459 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
97460/*
97461** If the argument p points to a MemJournal structure that is not an
97462** in-memory-only journal file (i.e. is one that was opened with a +ve
97463** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
97464** file has not yet been created, create it now.
97465*/
97466SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
97467 int rc = SQLITE_OK;
97468 MemJournal *p = (MemJournal*)pJfd;
97469 if( pJfd->pMethods==&MemJournalMethods && (
97470#ifdef SQLITE_ENABLE_ATOMIC_WRITE
97471 p->nSpill>0
97472#else
97473 /* While this appears to not be possible without ATOMIC_WRITE, the
97474 ** paths are complex, so it seems prudent to leave the test in as
97475 ** a NEVER(), in case our analysis is subtly flawed. */
97476 NEVER(p->nSpill>0)
97477#endif
97478#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
97479 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
97480#endif
97481 )){
97482 rc = memjrnlCreateFile(p);
97483 }
97484 return rc;
97485}
97486#endif
97487
97488/*
97489** The file-handle passed as the only argument is open on a journal file.
97490** Return true if this "journal file" is currently stored in heap memory,
97491** or false otherwise.
97492*/
97493SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
97494 return p->pMethods==&MemJournalMethods;
97495}
97496
97497/*
97498** Return the number of bytes required to store a JournalFile that uses vfs
97499** pVfs to create the underlying on-disk files.
97500*/
97501SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
97502 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
97503}
97504
97505/************** End of memjournal.c ******************************************/
97506/************** Begin file walker.c ******************************************/
97507/*
97508** 2008 August 16
97509**
97510** The author disclaims copyright to this source code. In place of
97511** a legal notice, here is a blessing:
97512**
97513** May you do good and not evil.
97514** May you find forgiveness for yourself and forgive others.
97515** May you share freely, never taking more than you give.
97516**
97517*************************************************************************
97518** This file contains routines used for walking the parser tree for
97519** an SQL statement.
97520*/
97521/* #include "sqliteInt.h" */
97522/* #include <stdlib.h> */
97523/* #include <string.h> */
97524
97525
97526#if !defined(SQLITE_OMIT_WINDOWFUNC)
97527/*
97528** Walk all expressions linked into the list of Window objects passed
97529** as the second argument.
97530*/
97531static int walkWindowList(Walker *pWalker, Window *pList){
97532 Window *pWin;
97533 for(pWin=pList; pWin; pWin=pWin->pNextWin){
97534 int rc;
97535 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
97536 if( rc ) return WRC_Abort;
97537 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
97538 if( rc ) return WRC_Abort;
97539 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
97540 if( rc ) return WRC_Abort;
97541
97542 /* The next two are purely for calls to sqlite3RenameExprUnmap()
97543 ** within sqlite3WindowOffsetExpr(). Because of constraints imposed
97544 ** by sqlite3WindowOffsetExpr(), they can never fail. The results do
97545 ** not matter anyhow. */
97546 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
97547 if( NEVER(rc) ) return WRC_Abort;
97548 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
97549 if( NEVER(rc) ) return WRC_Abort;
97550 }
97551 return WRC_Continue;
97552}
97553#endif
97554
97555/*
97556** Walk an expression tree. Invoke the callback once for each node
97557** of the expression, while descending. (In other words, the callback
97558** is invoked before visiting children.)
97559**
97560** The return value from the callback should be one of the WRC_*
97561** constants to specify how to proceed with the walk.
97562**
97563** WRC_Continue Continue descending down the tree.
97564**
97565** WRC_Prune Do not descend into child nodes, but allow
97566** the walk to continue with sibling nodes.
97567**
97568** WRC_Abort Do no more callbacks. Unwind the stack and
97569** return from the top-level walk call.
97570**
97571** The return value from this routine is WRC_Abort to abandon the tree walk
97572** and WRC_Continue to continue.
97573*/
97574static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
97575 int rc;
97576 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
97577 testcase( ExprHasProperty(pExpr, EP_Reduced) );
97578 while(1){
97579 rc = pWalker->xExprCallback(pWalker, pExpr);
97580 if( rc ) return rc & WRC_Abort;
97581 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
97582 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
97583 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
97584 if( pExpr->pRight ){
97585 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
97586 pExpr = pExpr->pRight;
97587 continue;
97588 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
97589 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
97590 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
97591 }else{
97592 if( pExpr->x.pList ){
97593 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
97594 }
97595#ifndef SQLITE_OMIT_WINDOWFUNC
97596 if( ExprHasProperty(pExpr, EP_WinFunc) ){
97597 if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
97598 }
97599#endif
97600 }
97601 }
97602 break;
97603 }
97604 return WRC_Continue;
97605}
97606SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
97607 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
97608}
97609
97610/*
97611** Call sqlite3WalkExpr() for every expression in list p or until
97612** an abort request is seen.
97613*/
97614SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
97615 int i;
97616 struct ExprList_item *pItem;
97617 if( p ){
97618 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
97619 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
97620 }
97621 }
97622 return WRC_Continue;
97623}
97624
97625/*
97626** Walk all expressions associated with SELECT statement p. Do
97627** not invoke the SELECT callback on p, but do (of course) invoke
97628** any expr callbacks and SELECT callbacks that come from subqueries.
97629** Return WRC_Abort or WRC_Continue.
97630*/
97631SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
97632 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
97633 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
97634 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
97635 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
97636 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
97637 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
97638#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
97639 {
97640 Parse *pParse = pWalker->pParse;
97641 if( pParse && IN_RENAME_OBJECT ){
97642 /* The following may return WRC_Abort if there are unresolvable
97643 ** symbols (e.g. a table that does not exist) in a window definition. */
97644 int rc = walkWindowList(pWalker, p->pWinDefn);
97645 return rc;
97646 }
97647 }
97648#endif
97649 return WRC_Continue;
97650}
97651
97652/*
97653** Walk the parse trees associated with all subqueries in the
97654** FROM clause of SELECT statement p. Do not invoke the select
97655** callback on p, but do invoke it on each FROM clause subquery
97656** and on any subqueries further down in the tree. Return
97657** WRC_Abort or WRC_Continue;
97658*/
97659SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
97660 SrcList *pSrc;
97661 int i;
97662 struct SrcList_item *pItem;
97663
97664 pSrc = p->pSrc;
97665 if( pSrc ){
97666 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
97667 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
97668 return WRC_Abort;
97669 }
97670 if( pItem->fg.isTabFunc
97671 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
97672 ){
97673 return WRC_Abort;
97674 }
97675 }
97676 }
97677 return WRC_Continue;
97678}
97679
97680/*
97681** Call sqlite3WalkExpr() for every expression in Select statement p.
97682** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
97683** on the compound select chain, p->pPrior.
97684**
97685** If it is not NULL, the xSelectCallback() callback is invoked before
97686** the walk of the expressions and FROM clause. The xSelectCallback2()
97687** method is invoked following the walk of the expressions and FROM clause,
97688** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
97689** and if the expressions and FROM clause both return WRC_Continue;
97690**
97691** Return WRC_Continue under normal conditions. Return WRC_Abort if
97692** there is an abort request.
97693**
97694** If the Walker does not have an xSelectCallback() then this routine
97695** is a no-op returning WRC_Continue.
97696*/
97697SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
97698 int rc;
97699 if( p==0 ) return WRC_Continue;
97700 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
97701 do{
97702 rc = pWalker->xSelectCallback(pWalker, p);
97703 if( rc ) return rc & WRC_Abort;
97704 if( sqlite3WalkSelectExpr(pWalker, p)
97705 || sqlite3WalkSelectFrom(pWalker, p)
97706 ){
97707 return WRC_Abort;
97708 }
97709 if( pWalker->xSelectCallback2 ){
97710 pWalker->xSelectCallback2(pWalker, p);
97711 }
97712 p = p->pPrior;
97713 }while( p!=0 );
97714 return WRC_Continue;
97715}
97716
97717/* Increase the walkerDepth when entering a subquery, and
97718** descrease when leaving the subquery.
97719*/
97720SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
97721 UNUSED_PARAMETER(pSelect);
97722 pWalker->walkerDepth++;
97723 return WRC_Continue;
97724}
97725SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
97726 UNUSED_PARAMETER(pSelect);
97727 pWalker->walkerDepth--;
97728}
97729
97730
97731/*
97732** No-op routine for the parse-tree walker.
97733**
97734** When this routine is the Walker.xExprCallback then expression trees
97735** are walked without any actions being taken at each node. Presumably,
97736** when this routine is used for Walker.xExprCallback then
97737** Walker.xSelectCallback is set to do something useful for every
97738** subquery in the parser tree.
97739*/
97740SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
97741 UNUSED_PARAMETER2(NotUsed, NotUsed2);
97742 return WRC_Continue;
97743}
97744
97745/*
97746** No-op routine for the parse-tree walker for SELECT statements.
97747** subquery in the parser tree.
97748*/
97749SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
97750 UNUSED_PARAMETER2(NotUsed, NotUsed2);
97751 return WRC_Continue;
97752}
97753
97754/************** End of walker.c **********************************************/
97755/************** Begin file resolve.c *****************************************/
97756/*
97757** 2008 August 18
97758**
97759** The author disclaims copyright to this source code. In place of
97760** a legal notice, here is a blessing:
97761**
97762** May you do good and not evil.
97763** May you find forgiveness for yourself and forgive others.
97764** May you share freely, never taking more than you give.
97765**
97766*************************************************************************
97767**
97768** This file contains routines used for walking the parser tree and
97769** resolve all identifiers by associating them with a particular
97770** table and column.
97771*/
97772/* #include "sqliteInt.h" */
97773
97774/*
97775** Magic table number to mean the EXCLUDED table in an UPSERT statement.
97776*/
97777#define EXCLUDED_TABLE_NUMBER 2
97778
97779/*
97780** Walk the expression tree pExpr and increase the aggregate function
97781** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
97782** This needs to occur when copying a TK_AGG_FUNCTION node from an
97783** outer query into an inner subquery.
97784**
97785** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
97786** is a helper function - a callback for the tree walker.
97787**
97788** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
97789*/
97790static int incrAggDepth(Walker *pWalker, Expr *pExpr){
97791 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
97792 return WRC_Continue;
97793}
97794static void incrAggFunctionDepth(Expr *pExpr, int N){
97795 if( N>0 ){
97796 Walker w;
97797 memset(&w, 0, sizeof(w));
97798 w.xExprCallback = incrAggDepth;
97799 w.u.n = N;
97800 sqlite3WalkExpr(&w, pExpr);
97801 }
97802}
97803
97804/*
97805** Turn the pExpr expression into an alias for the iCol-th column of the
97806** result set in pEList.
97807**
97808** If the reference is followed by a COLLATE operator, then make sure
97809** the COLLATE operator is preserved. For example:
97810**
97811** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
97812**
97813** Should be transformed into:
97814**
97815** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
97816**
97817** The nSubquery parameter specifies how many levels of subquery the
97818** alias is removed from the original expression. The usual value is
97819** zero but it might be more if the alias is contained within a subquery
97820** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
97821** structures must be increased by the nSubquery amount.
97822*/
97823static void resolveAlias(
97824 Parse *pParse, /* Parsing context */
97825 ExprList *pEList, /* A result set */
97826 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
97827 Expr *pExpr, /* Transform this into an alias to the result set */
97828 const char *zType, /* "GROUP" or "ORDER" or "" */
97829 int nSubquery /* Number of subqueries that the label is moving */
97830){
97831 Expr *pOrig; /* The iCol-th column of the result set */
97832 Expr *pDup; /* Copy of pOrig */
97833 sqlite3 *db; /* The database connection */
97834
97835 assert( iCol>=0 && iCol<pEList->nExpr );
97836 pOrig = pEList->a[iCol].pExpr;
97837 assert( pOrig!=0 );
97838 db = pParse->db;
97839 pDup = sqlite3ExprDup(db, pOrig, 0);
97840 if( pDup!=0 ){
97841 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
97842 if( pExpr->op==TK_COLLATE ){
97843 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
97844 }
97845
97846 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
97847 ** prevents ExprDelete() from deleting the Expr structure itself,
97848 ** allowing it to be repopulated by the memcpy() on the following line.
97849 ** The pExpr->u.zToken might point into memory that will be freed by the
97850 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
97851 ** make a copy of the token before doing the sqlite3DbFree().
97852 */
97853 ExprSetProperty(pExpr, EP_Static);
97854 sqlite3ExprDelete(db, pExpr);
97855 memcpy(pExpr, pDup, sizeof(*pExpr));
97856 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
97857 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
97858 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
97859 pExpr->flags |= EP_MemToken;
97860 }
97861 if( ExprHasProperty(pExpr, EP_WinFunc) ){
97862 if( pExpr->y.pWin!=0 ){
97863 pExpr->y.pWin->pOwner = pExpr;
97864 }else{
97865 assert( db->mallocFailed );
97866 }
97867 }
97868 sqlite3DbFree(db, pDup);
97869 }
97870 ExprSetProperty(pExpr, EP_Alias);
97871}
97872
97873
97874/*
97875** Return TRUE if the name zCol occurs anywhere in the USING clause.
97876**
97877** Return FALSE if the USING clause is NULL or if it does not contain
97878** zCol.
97879*/
97880static int nameInUsingClause(IdList *pUsing, const char *zCol){
97881 if( pUsing ){
97882 int k;
97883 for(k=0; k<pUsing->nId; k++){
97884 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
97885 }
97886 }
97887 return 0;
97888}
97889
97890/*
97891** Subqueries stores the original database, table and column names for their
97892** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
97893** Check to see if the zSpan given to this routine matches the zDb, zTab,
97894** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
97895** match anything.
97896*/
97897SQLITE_PRIVATE int sqlite3MatchEName(
97898 const struct ExprList_item *pItem,
97899 const char *zCol,
97900 const char *zTab,
97901 const char *zDb
97902){
97903 int n;
97904 const char *zSpan;
97905 if( pItem->eEName!=ENAME_TAB ) return 0;
97906 zSpan = pItem->zEName;
97907 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
97908 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
97909 return 0;
97910 }
97911 zSpan += n+1;
97912 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
97913 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
97914 return 0;
97915 }
97916 zSpan += n+1;
97917 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
97918 return 0;
97919 }
97920 return 1;
97921}
97922
97923/*
97924** Return TRUE if the double-quoted string mis-feature should be supported.
97925*/
97926static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
97927 if( db->init.busy ) return 1; /* Always support for legacy schemas */
97928 if( pTopNC->ncFlags & NC_IsDDL ){
97929 /* Currently parsing a DDL statement */
97930 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
97931 return 1;
97932 }
97933 return (db->flags & SQLITE_DqsDDL)!=0;
97934 }else{
97935 /* Currently parsing a DML statement */
97936 return (db->flags & SQLITE_DqsDML)!=0;
97937 }
97938}
97939
97940/*
97941** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
97942** return the appropriate colUsed mask.
97943*/
97944SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr *pExpr){
97945 int n;
97946 Table *pExTab;
97947
97948 n = pExpr->iColumn;
97949 pExTab = pExpr->y.pTab;
97950 assert( pExTab!=0 );
97951 if( (pExTab->tabFlags & TF_HasGenerated)!=0
97952 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
97953 ){
97954 testcase( pExTab->nCol==BMS-1 );
97955 testcase( pExTab->nCol==BMS );
97956 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
97957 }else{
97958 testcase( n==BMS-1 );
97959 testcase( n==BMS );
97960 if( n>=BMS ) n = BMS-1;
97961 return ((Bitmask)1)<<n;
97962 }
97963}
97964
97965/*
97966** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
97967** that name in the set of source tables in pSrcList and make the pExpr
97968** expression node refer back to that source column. The following changes
97969** are made to pExpr:
97970**
97971** pExpr->iDb Set the index in db->aDb[] of the database X
97972** (even if X is implied).
97973** pExpr->iTable Set to the cursor number for the table obtained
97974** from pSrcList.
97975** pExpr->y.pTab Points to the Table structure of X.Y (even if
97976** X and/or Y are implied.)
97977** pExpr->iColumn Set to the column number within the table.
97978** pExpr->op Set to TK_COLUMN.
97979** pExpr->pLeft Any expression this points to is deleted
97980** pExpr->pRight Any expression this points to is deleted.
97981**
97982** The zDb variable is the name of the database (the "X"). This value may be
97983** NULL meaning that name is of the form Y.Z or Z. Any available database
97984** can be used. The zTable variable is the name of the table (the "Y"). This
97985** value can be NULL if zDb is also NULL. If zTable is NULL it
97986** means that the form of the name is Z and that columns from any table
97987** can be used.
97988**
97989** If the name cannot be resolved unambiguously, leave an error message
97990** in pParse and return WRC_Abort. Return WRC_Prune on success.
97991*/
97992static int lookupName(
97993 Parse *pParse, /* The parsing context */
97994 const char *zDb, /* Name of the database containing table, or NULL */
97995 const char *zTab, /* Name of table containing column, or NULL */
97996 const char *zCol, /* Name of the column. */
97997 NameContext *pNC, /* The name context used to resolve the name */
97998 Expr *pExpr /* Make this EXPR node point to the selected column */
97999){
98000 int i, j; /* Loop counters */
98001 int cnt = 0; /* Number of matching column names */
98002 int cntTab = 0; /* Number of matching table names */
98003 int nSubquery = 0; /* How many levels of subquery */
98004 sqlite3 *db = pParse->db; /* The database connection */
98005 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
98006 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
98007 NameContext *pTopNC = pNC; /* First namecontext in the list */
98008 Schema *pSchema = 0; /* Schema of the expression */
98009 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
98010 Table *pTab = 0; /* Table hold the row */
98011 Column *pCol; /* A column of pTab */
98012
98013 assert( pNC ); /* the name context cannot be NULL. */
98014 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
98015 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
98016
98017 /* Initialize the node to no-match */
98018 pExpr->iTable = -1;
98019 ExprSetVVAProperty(pExpr, EP_NoReduce);
98020
98021 /* Translate the schema name in zDb into a pointer to the corresponding
98022 ** schema. If not found, pSchema will remain NULL and nothing will match
98023 ** resulting in an appropriate error message toward the end of this routine
98024 */
98025 if( zDb ){
98026 testcase( pNC->ncFlags & NC_PartIdx );
98027 testcase( pNC->ncFlags & NC_IsCheck );
98028 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
98029 /* Silently ignore database qualifiers inside CHECK constraints and
98030 ** partial indices. Do not raise errors because that might break
98031 ** legacy and because it does not hurt anything to just ignore the
98032 ** database name. */
98033 zDb = 0;
98034 }else{
98035 for(i=0; i<db->nDb; i++){
98036 assert( db->aDb[i].zDbSName );
98037 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
98038 pSchema = db->aDb[i].pSchema;
98039 break;
98040 }
98041 }
98042 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
98043 /* This branch is taken when the main database has been renamed
98044 ** using SQLITE_DBCONFIG_MAINDBNAME. */
98045 pSchema = db->aDb[0].pSchema;
98046 zDb = db->aDb[0].zDbSName;
98047 }
98048 }
98049 }
98050
98051 /* Start at the inner-most context and move outward until a match is found */
98052 assert( pNC && cnt==0 );
98053 do{
98054 ExprList *pEList;
98055 SrcList *pSrcList = pNC->pSrcList;
98056
98057 if( pSrcList ){
98058 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
98059 u8 hCol;
98060 pTab = pItem->pTab;
98061 assert( pTab!=0 && pTab->zName!=0 );
98062 assert( pTab->nCol>0 );
98063 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
98064 int hit = 0;
98065 pEList = pItem->pSelect->pEList;
98066 for(j=0; j<pEList->nExpr; j++){
98067 if( sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
98068 cnt++;
98069 cntTab = 2;
98070 pMatch = pItem;
98071 pExpr->iColumn = j;
98072 hit = 1;
98073 }
98074 }
98075 if( hit || zTab==0 ) continue;
98076 }
98077 if( zDb && pTab->pSchema!=pSchema ){
98078 continue;
98079 }
98080 if( zTab ){
98081 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
98082 assert( zTabName!=0 );
98083 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
98084 continue;
98085 }
98086 if( IN_RENAME_OBJECT && pItem->zAlias ){
98087 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
98088 }
98089 }
98090 if( 0==(cntTab++) ){
98091 pMatch = pItem;
98092 }
98093 hCol = sqlite3StrIHash(zCol);
98094 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
98095 if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
98096 /* If there has been exactly one prior match and this match
98097 ** is for the right-hand table of a NATURAL JOIN or is in a
98098 ** USING clause, then skip this match.
98099 */
98100 if( cnt==1 ){
98101 if( pItem->fg.jointype & JT_NATURAL ) continue;
98102 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
98103 }
98104 cnt++;
98105 pMatch = pItem;
98106 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
98107 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
98108 break;
98109 }
98110 }
98111 }
98112 if( pMatch ){
98113 pExpr->iTable = pMatch->iCursor;
98114 pExpr->y.pTab = pMatch->pTab;
98115 /* RIGHT JOIN not (yet) supported */
98116 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
98117 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
98118 ExprSetProperty(pExpr, EP_CanBeNull);
98119 }
98120 pSchema = pExpr->y.pTab->pSchema;
98121 }
98122 } /* if( pSrcList ) */
98123
98124#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
98125 /* If we have not already resolved the name, then maybe
98126 ** it is a new.* or old.* trigger argument reference. Or
98127 ** maybe it is an excluded.* from an upsert.
98128 */
98129 if( zDb==0 && zTab!=0 && cntTab==0 ){
98130 pTab = 0;
98131#ifndef SQLITE_OMIT_TRIGGER
98132 if( pParse->pTriggerTab!=0 ){
98133 int op = pParse->eTriggerOp;
98134 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
98135 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
98136 pExpr->iTable = 1;
98137 pTab = pParse->pTriggerTab;
98138 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
98139 pExpr->iTable = 0;
98140 pTab = pParse->pTriggerTab;
98141 }
98142 }
98143#endif /* SQLITE_OMIT_TRIGGER */
98144#ifndef SQLITE_OMIT_UPSERT
98145 if( (pNC->ncFlags & NC_UUpsert)!=0 ){
98146 Upsert *pUpsert = pNC->uNC.pUpsert;
98147 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
98148 pTab = pUpsert->pUpsertSrc->a[0].pTab;
98149 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
98150 }
98151 }
98152#endif /* SQLITE_OMIT_UPSERT */
98153
98154 if( pTab ){
98155 int iCol;
98156 u8 hCol = sqlite3StrIHash(zCol);
98157 pSchema = pTab->pSchema;
98158 cntTab++;
98159 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
98160 if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
98161 if( iCol==pTab->iPKey ){
98162 iCol = -1;
98163 }
98164 break;
98165 }
98166 }
98167 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
98168 /* IMP: R-51414-32910 */
98169 iCol = -1;
98170 }
98171 if( iCol<pTab->nCol ){
98172 cnt++;
98173#ifndef SQLITE_OMIT_UPSERT
98174 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
98175 testcase( iCol==(-1) );
98176 if( IN_RENAME_OBJECT ){
98177 pExpr->iColumn = iCol;
98178 pExpr->y.pTab = pTab;
98179 eNewExprOp = TK_COLUMN;
98180 }else{
98181 pExpr->iTable = pNC->uNC.pUpsert->regData +
98182 sqlite3TableColumnToStorage(pTab, iCol);
98183 eNewExprOp = TK_REGISTER;
98184 ExprSetProperty(pExpr, EP_Alias);
98185 }
98186 }else
98187#endif /* SQLITE_OMIT_UPSERT */
98188 {
98189#ifndef SQLITE_OMIT_TRIGGER
98190 if( iCol<0 ){
98191 pExpr->affExpr = SQLITE_AFF_INTEGER;
98192 }else if( pExpr->iTable==0 ){
98193 testcase( iCol==31 );
98194 testcase( iCol==32 );
98195 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
98196 }else{
98197 testcase( iCol==31 );
98198 testcase( iCol==32 );
98199 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
98200 }
98201 pExpr->y.pTab = pTab;
98202 pExpr->iColumn = (i16)iCol;
98203 eNewExprOp = TK_TRIGGER;
98204#endif /* SQLITE_OMIT_TRIGGER */
98205 }
98206 }
98207 }
98208 }
98209#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
98210
98211 /*
98212 ** Perhaps the name is a reference to the ROWID
98213 */
98214 if( cnt==0
98215 && cntTab==1
98216 && pMatch
98217 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
98218 && sqlite3IsRowid(zCol)
98219 && VisibleRowid(pMatch->pTab)
98220 ){
98221 cnt = 1;
98222 pExpr->iColumn = -1;
98223 pExpr->affExpr = SQLITE_AFF_INTEGER;
98224 }
98225
98226 /*
98227 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
98228 ** might refer to an result-set alias. This happens, for example, when
98229 ** we are resolving names in the WHERE clause of the following command:
98230 **
98231 ** SELECT a+b AS x FROM table WHERE x<10;
98232 **
98233 ** In cases like this, replace pExpr with a copy of the expression that
98234 ** forms the result set entry ("a+b" in the example) and return immediately.
98235 ** Note that the expression in the result set should have already been
98236 ** resolved by the time the WHERE clause is resolved.
98237 **
98238 ** The ability to use an output result-set column in the WHERE, GROUP BY,
98239 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
98240 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
98241 ** is supported for backwards compatibility only. Hence, we issue a warning
98242 ** on sqlite3_log() whenever the capability is used.
98243 */
98244 if( (pNC->ncFlags & NC_UEList)!=0
98245 && cnt==0
98246 && zTab==0
98247 ){
98248 pEList = pNC->uNC.pEList;
98249 assert( pEList!=0 );
98250 for(j=0; j<pEList->nExpr; j++){
98251 char *zAs = pEList->a[j].zEName;
98252 if( pEList->a[j].eEName==ENAME_NAME
98253 && sqlite3_stricmp(zAs, zCol)==0
98254 ){
98255 Expr *pOrig;
98256 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
98257 assert( pExpr->x.pList==0 );
98258 assert( pExpr->x.pSelect==0 );
98259 pOrig = pEList->a[j].pExpr;
98260 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
98261 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
98262 return WRC_Abort;
98263 }
98264 if( ExprHasProperty(pOrig, EP_Win)
98265 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
98266 ){
98267 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
98268 return WRC_Abort;
98269 }
98270 if( sqlite3ExprVectorSize(pOrig)!=1 ){
98271 sqlite3ErrorMsg(pParse, "row value misused");
98272 return WRC_Abort;
98273 }
98274 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
98275 cnt = 1;
98276 pMatch = 0;
98277 assert( zTab==0 && zDb==0 );
98278 if( IN_RENAME_OBJECT ){
98279 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
98280 }
98281 goto lookupname_end;
98282 }
98283 }
98284 }
98285
98286 /* Advance to the next name context. The loop will exit when either
98287 ** we have a match (cnt>0) or when we run out of name contexts.
98288 */
98289 if( cnt ) break;
98290 pNC = pNC->pNext;
98291 nSubquery++;
98292 }while( pNC );
98293
98294
98295 /*
98296 ** If X and Y are NULL (in other words if only the column name Z is
98297 ** supplied) and the value of Z is enclosed in double-quotes, then
98298 ** Z is a string literal if it doesn't match any column names. In that
98299 ** case, we need to return right away and not make any changes to
98300 ** pExpr.
98301 **
98302 ** Because no reference was made to outer contexts, the pNC->nRef
98303 ** fields are not changed in any context.
98304 */
98305 if( cnt==0 && zTab==0 ){
98306 assert( pExpr->op==TK_ID );
98307 if( ExprHasProperty(pExpr,EP_DblQuoted)
98308 && areDoubleQuotedStringsEnabled(db, pTopNC)
98309 ){
98310 /* If a double-quoted identifier does not match any known column name,
98311 ** then treat it as a string.
98312 **
98313 ** This hack was added in the early days of SQLite in a misguided attempt
98314 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
98315 ** I now sorely regret putting in this hack. The effect of this hack is
98316 ** that misspelled identifier names are silently converted into strings
98317 ** rather than causing an error, to the frustration of countless
98318 ** programmers. To all those frustrated programmers, my apologies.
98319 **
98320 ** Someday, I hope to get rid of this hack. Unfortunately there is
98321 ** a huge amount of legacy SQL that uses it. So for now, we just
98322 ** issue a warning.
98323 */
98324 sqlite3_log(SQLITE_WARNING,
98325 "double-quoted string literal: \"%w\"", zCol);
98326#ifdef SQLITE_ENABLE_NORMALIZE
98327 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
98328#endif
98329 pExpr->op = TK_STRING;
98330 pExpr->y.pTab = 0;
98331 return WRC_Prune;
98332 }
98333 if( sqlite3ExprIdToTrueFalse(pExpr) ){
98334 return WRC_Prune;
98335 }
98336 }
98337
98338 /*
98339 ** cnt==0 means there was not match. cnt>1 means there were two or
98340 ** more matches. Either way, we have an error.
98341 */
98342 if( cnt!=1 ){
98343 const char *zErr;
98344 zErr = cnt==0 ? "no such column" : "ambiguous column name";
98345 if( zDb ){
98346 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
98347 }else if( zTab ){
98348 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
98349 }else{
98350 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
98351 }
98352 pParse->checkSchema = 1;
98353 pTopNC->nErr++;
98354 }
98355
98356 /* If a column from a table in pSrcList is referenced, then record
98357 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
98358 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
98359 ** set if the 63rd or any subsequent column is used.
98360 **
98361 ** The colUsed mask is an optimization used to help determine if an
98362 ** index is a covering index. The correct answer is still obtained
98363 ** if the mask contains extra set bits. However, it is important to
98364 ** avoid setting bits beyond the maximum column number of the table.
98365 ** (See ticket [b92e5e8ec2cdbaa1]).
98366 **
98367 ** If a generated column is referenced, set bits for every column
98368 ** of the table.
98369 */
98370 if( pExpr->iColumn>=0 && pMatch!=0 ){
98371 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
98372 }
98373
98374 /* Clean up and return
98375 */
98376 sqlite3ExprDelete(db, pExpr->pLeft);
98377 pExpr->pLeft = 0;
98378 sqlite3ExprDelete(db, pExpr->pRight);
98379 pExpr->pRight = 0;
98380 pExpr->op = eNewExprOp;
98381 ExprSetProperty(pExpr, EP_Leaf);
98382lookupname_end:
98383 if( cnt==1 ){
98384 assert( pNC!=0 );
98385 if( !ExprHasProperty(pExpr, EP_Alias) ){
98386 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
98387 }
98388 /* Increment the nRef value on all name contexts from TopNC up to
98389 ** the point where the name matched. */
98390 for(;;){
98391 assert( pTopNC!=0 );
98392 pTopNC->nRef++;
98393 if( pTopNC==pNC ) break;
98394 pTopNC = pTopNC->pNext;
98395 }
98396 return WRC_Prune;
98397 } else {
98398 return WRC_Abort;
98399 }
98400}
98401
98402/*
98403** Allocate and return a pointer to an expression to load the column iCol
98404** from datasource iSrc in SrcList pSrc.
98405*/
98406SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
98407 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
98408 if( p ){
98409 struct SrcList_item *pItem = &pSrc->a[iSrc];
98410 Table *pTab = p->y.pTab = pItem->pTab;
98411 p->iTable = pItem->iCursor;
98412 if( p->y.pTab->iPKey==iCol ){
98413 p->iColumn = -1;
98414 }else{
98415 p->iColumn = (ynVar)iCol;
98416 if( (pTab->tabFlags & TF_HasGenerated)!=0
98417 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
98418 ){
98419 testcase( pTab->nCol==63 );
98420 testcase( pTab->nCol==64 );
98421 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
98422 }else{
98423 testcase( iCol==BMS );
98424 testcase( iCol==BMS-1 );
98425 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
98426 }
98427 }
98428 }
98429 return p;
98430}
98431
98432/*
98433** Report an error that an expression is not valid for some set of
98434** pNC->ncFlags values determined by validMask.
98435**
98436** static void notValid(
98437** Parse *pParse, // Leave error message here
98438** NameContext *pNC, // The name context
98439** const char *zMsg, // Type of error
98440** int validMask, // Set of contexts for which prohibited
98441** Expr *pExpr // Invalidate this expression on error
98442** ){...}
98443**
98444** As an optimization, since the conditional is almost always false
98445** (because errors are rare), the conditional is moved outside of the
98446** function call using a macro.
98447*/
98448static void notValidImpl(
98449 Parse *pParse, /* Leave error message here */
98450 NameContext *pNC, /* The name context */
98451 const char *zMsg, /* Type of error */
98452 Expr *pExpr /* Invalidate this expression on error */
98453){
98454 const char *zIn = "partial index WHERE clauses";
98455 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
98456#ifndef SQLITE_OMIT_CHECK
98457 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
98458#endif
98459#ifndef SQLITE_OMIT_GENERATED_COLUMNS
98460 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
98461#endif
98462 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
98463 if( pExpr ) pExpr->op = TK_NULL;
98464}
98465#define sqlite3ResolveNotValid(P,N,M,X,E) \
98466 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \
98467 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E);
98468
98469/*
98470** Expression p should encode a floating point value between 1.0 and 0.0.
98471** Return 1024 times this value. Or return -1 if p is not a floating point
98472** value between 1.0 and 0.0.
98473*/
98474static int exprProbability(Expr *p){
98475 double r = -1.0;
98476 if( p->op!=TK_FLOAT ) return -1;
98477 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
98478 assert( r>=0.0 );
98479 if( r>1.0 ) return -1;
98480 return (int)(r*134217728.0);
98481}
98482
98483/*
98484** This routine is callback for sqlite3WalkExpr().
98485**
98486** Resolve symbolic names into TK_COLUMN operators for the current
98487** node in the expression tree. Return 0 to continue the search down
98488** the tree or 2 to abort the tree walk.
98489**
98490** This routine also does error checking and name resolution for
98491** function names. The operator for aggregate functions is changed
98492** to TK_AGG_FUNCTION.
98493*/
98494static int resolveExprStep(Walker *pWalker, Expr *pExpr){
98495 NameContext *pNC;
98496 Parse *pParse;
98497
98498 pNC = pWalker->u.pNC;
98499 assert( pNC!=0 );
98500 pParse = pNC->pParse;
98501 assert( pParse==pWalker->pParse );
98502
98503#ifndef NDEBUG
98504 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
98505 SrcList *pSrcList = pNC->pSrcList;
98506 int i;
98507 for(i=0; i<pNC->pSrcList->nSrc; i++){
98508 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
98509 }
98510 }
98511#endif
98512 switch( pExpr->op ){
98513
98514 /* The special operator TK_ROW means use the rowid for the first
98515 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
98516 ** clause processing on UPDATE and DELETE statements, and by
98517 ** UPDATE ... FROM statement processing.
98518 */
98519 case TK_ROW: {
98520 SrcList *pSrcList = pNC->pSrcList;
98521 struct SrcList_item *pItem;
98522 assert( pSrcList && pSrcList->nSrc>=1 );
98523 pItem = pSrcList->a;
98524 pExpr->op = TK_COLUMN;
98525 pExpr->y.pTab = pItem->pTab;
98526 pExpr->iTable = pItem->iCursor;
98527 pExpr->iColumn--;
98528 pExpr->affExpr = SQLITE_AFF_INTEGER;
98529 break;
98530 }
98531
98532 /* A column name: ID
98533 ** Or table name and column name: ID.ID
98534 ** Or a database, table and column: ID.ID.ID
98535 **
98536 ** The TK_ID and TK_OUT cases are combined so that there will only
98537 ** be one call to lookupName(). Then the compiler will in-line
98538 ** lookupName() for a size reduction and performance increase.
98539 */
98540 case TK_ID:
98541 case TK_DOT: {
98542 const char *zColumn;
98543 const char *zTable;
98544 const char *zDb;
98545 Expr *pRight;
98546
98547 if( pExpr->op==TK_ID ){
98548 zDb = 0;
98549 zTable = 0;
98550 zColumn = pExpr->u.zToken;
98551 }else{
98552 Expr *pLeft = pExpr->pLeft;
98553 testcase( pNC->ncFlags & NC_IdxExpr );
98554 testcase( pNC->ncFlags & NC_GenCol );
98555 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",
98556 NC_IdxExpr|NC_GenCol, 0);
98557 pRight = pExpr->pRight;
98558 if( pRight->op==TK_ID ){
98559 zDb = 0;
98560 }else{
98561 assert( pRight->op==TK_DOT );
98562 zDb = pLeft->u.zToken;
98563 pLeft = pRight->pLeft;
98564 pRight = pRight->pRight;
98565 }
98566 zTable = pLeft->u.zToken;
98567 zColumn = pRight->u.zToken;
98568 if( IN_RENAME_OBJECT ){
98569 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
98570 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
98571 }
98572 }
98573 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
98574 }
98575
98576 /* Resolve function names
98577 */
98578 case TK_FUNCTION: {
98579 ExprList *pList = pExpr->x.pList; /* The argument list */
98580 int n = pList ? pList->nExpr : 0; /* Number of arguments */
98581 int no_such_func = 0; /* True if no such function exists */
98582 int wrong_num_args = 0; /* True if wrong number of arguments */
98583 int is_agg = 0; /* True if is an aggregate function */
98584 int nId; /* Number of characters in function name */
98585 const char *zId; /* The function name. */
98586 FuncDef *pDef; /* Information about the function */
98587 u8 enc = ENC(pParse->db); /* The database encoding */
98588 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
98589#ifndef SQLITE_OMIT_WINDOWFUNC
98590 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
98591#endif
98592 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
98593 zId = pExpr->u.zToken;
98594 nId = sqlite3Strlen30(zId);
98595 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
98596 if( pDef==0 ){
98597 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
98598 if( pDef==0 ){
98599 no_such_func = 1;
98600 }else{
98601 wrong_num_args = 1;
98602 }
98603 }else{
98604 is_agg = pDef->xFinalize!=0;
98605 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
98606 ExprSetProperty(pExpr, EP_Unlikely);
98607 if( n==2 ){
98608 pExpr->iTable = exprProbability(pList->a[1].pExpr);
98609 if( pExpr->iTable<0 ){
98610 sqlite3ErrorMsg(pParse,
98611 "second argument to likelihood() must be a "
98612 "constant between 0.0 and 1.0");
98613 pNC->nErr++;
98614 }
98615 }else{
98616 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
98617 ** equivalent to likelihood(X, 0.0625).
98618 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
98619 ** short-hand for likelihood(X,0.0625).
98620 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
98621 ** for likelihood(X,0.9375).
98622 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
98623 ** to likelihood(X,0.9375). */
98624 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
98625 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
98626 }
98627 }
98628#ifndef SQLITE_OMIT_AUTHORIZATION
98629 {
98630 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
98631 if( auth!=SQLITE_OK ){
98632 if( auth==SQLITE_DENY ){
98633 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
98634 pDef->zName);
98635 pNC->nErr++;
98636 }
98637 pExpr->op = TK_NULL;
98638 return WRC_Prune;
98639 }
98640 }
98641#endif
98642 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
98643 /* For the purposes of the EP_ConstFunc flag, date and time
98644 ** functions and other functions that change slowly are considered
98645 ** constant because they are constant for the duration of one query.
98646 ** This allows them to be factored out of inner loops. */
98647 ExprSetProperty(pExpr,EP_ConstFunc);
98648 }
98649 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
98650 /* Clearly non-deterministic functions like random(), but also
98651 ** date/time functions that use 'now', and other functions like
98652 ** sqlite_version() that might change over time cannot be used
98653 ** in an index or generated column. Curiously, they can be used
98654 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
98655 ** all this. */
98656 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
98657 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0);
98658 }else{
98659 assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
98660 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
98661 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
98662 }
98663 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
98664 && pParse->nested==0
98665 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
98666 ){
98667 /* Internal-use-only functions are disallowed unless the
98668 ** SQL is being compiled using sqlite3NestedParse() or
98669 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
98670 ** used to activate internal functionsn for testing purposes */
98671 no_such_func = 1;
98672 pDef = 0;
98673 }else
98674 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
98675 && !IN_RENAME_OBJECT
98676 ){
98677 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
98678 }
98679 }
98680
98681 if( 0==IN_RENAME_OBJECT ){
98682#ifndef SQLITE_OMIT_WINDOWFUNC
98683 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
98684 || (pDef->xValue==0 && pDef->xInverse==0)
98685 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
98686 );
98687 if( pDef && pDef->xValue==0 && pWin ){
98688 sqlite3ErrorMsg(pParse,
98689 "%.*s() may not be used as a window function", nId, zId
98690 );
98691 pNC->nErr++;
98692 }else if(
98693 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
98694 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
98695 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
98696 ){
98697 const char *zType;
98698 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
98699 zType = "window";
98700 }else{
98701 zType = "aggregate";
98702 }
98703 sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
98704 pNC->nErr++;
98705 is_agg = 0;
98706 }
98707#else
98708 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
98709 sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
98710 pNC->nErr++;
98711 is_agg = 0;
98712 }
98713#endif
98714 else if( no_such_func && pParse->db->init.busy==0
98715#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
98716 && pParse->explain==0
98717#endif
98718 ){
98719 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
98720 pNC->nErr++;
98721 }else if( wrong_num_args ){
98722 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
98723 nId, zId);
98724 pNC->nErr++;
98725 }
98726#ifndef SQLITE_OMIT_WINDOWFUNC
98727 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
98728 sqlite3ErrorMsg(pParse,
98729 "FILTER may not be used with non-aggregate %.*s()",
98730 nId, zId
98731 );
98732 pNC->nErr++;
98733 }
98734#endif
98735 if( is_agg ){
98736 /* Window functions may not be arguments of aggregate functions.
98737 ** Or arguments of other window functions. But aggregate functions
98738 ** may be arguments for window functions. */
98739#ifndef SQLITE_OMIT_WINDOWFUNC
98740 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
98741#else
98742 pNC->ncFlags &= ~NC_AllowAgg;
98743#endif
98744 }
98745 }
98746#ifndef SQLITE_OMIT_WINDOWFUNC
98747 else if( ExprHasProperty(pExpr, EP_WinFunc) ){
98748 is_agg = 1;
98749 }
98750#endif
98751 sqlite3WalkExprList(pWalker, pList);
98752 if( is_agg ){
98753#ifndef SQLITE_OMIT_WINDOWFUNC
98754 if( pWin ){
98755 Select *pSel = pNC->pWinSelect;
98756 assert( pWin==pExpr->y.pWin );
98757 if( IN_RENAME_OBJECT==0 ){
98758 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
98759 }
98760 sqlite3WalkExprList(pWalker, pWin->pPartition);
98761 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
98762 sqlite3WalkExpr(pWalker, pWin->pFilter);
98763 sqlite3WindowLink(pSel, pWin);
98764 pNC->ncFlags |= NC_HasWin;
98765 }else
98766#endif /* SQLITE_OMIT_WINDOWFUNC */
98767 {
98768 NameContext *pNC2 = pNC;
98769 pExpr->op = TK_AGG_FUNCTION;
98770 pExpr->op2 = 0;
98771#ifndef SQLITE_OMIT_WINDOWFUNC
98772 if( ExprHasProperty(pExpr, EP_WinFunc) ){
98773 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
98774 }
98775#endif
98776 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
98777 pExpr->op2++;
98778 pNC2 = pNC2->pNext;
98779 }
98780 assert( pDef!=0 || IN_RENAME_OBJECT );
98781 if( pNC2 && pDef ){
98782 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
98783 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
98784 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
98785
98786 }
98787 }
98788 pNC->ncFlags |= savedAllowFlags;
98789 }
98790 /* FIX ME: Compute pExpr->affinity based on the expected return
98791 ** type of the function
98792 */
98793 return WRC_Prune;
98794 }
98795#ifndef SQLITE_OMIT_SUBQUERY
98796 case TK_SELECT:
98797 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
98798#endif
98799 case TK_IN: {
98800 testcase( pExpr->op==TK_IN );
98801 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98802 int nRef = pNC->nRef;
98803 testcase( pNC->ncFlags & NC_IsCheck );
98804 testcase( pNC->ncFlags & NC_PartIdx );
98805 testcase( pNC->ncFlags & NC_IdxExpr );
98806 testcase( pNC->ncFlags & NC_GenCol );
98807 sqlite3ResolveNotValid(pParse, pNC, "subqueries",
98808 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr);
98809 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
98810 assert( pNC->nRef>=nRef );
98811 if( nRef!=pNC->nRef ){
98812 ExprSetProperty(pExpr, EP_VarSelect);
98813 pNC->ncFlags |= NC_VarSelect;
98814 }
98815 }
98816 break;
98817 }
98818 case TK_VARIABLE: {
98819 testcase( pNC->ncFlags & NC_IsCheck );
98820 testcase( pNC->ncFlags & NC_PartIdx );
98821 testcase( pNC->ncFlags & NC_IdxExpr );
98822 testcase( pNC->ncFlags & NC_GenCol );
98823 sqlite3ResolveNotValid(pParse, pNC, "parameters",
98824 NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr);
98825 break;
98826 }
98827 case TK_IS:
98828 case TK_ISNOT: {
98829 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
98830 assert( !ExprHasProperty(pExpr, EP_Reduced) );
98831 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
98832 ** and "x IS NOT FALSE". */
98833 if( pRight && pRight->op==TK_ID ){
98834 int rc = resolveExprStep(pWalker, pRight);
98835 if( rc==WRC_Abort ) return WRC_Abort;
98836 if( pRight->op==TK_TRUEFALSE ){
98837 pExpr->op2 = pExpr->op;
98838 pExpr->op = TK_TRUTH;
98839 return WRC_Continue;
98840 }
98841 }
98842 /* no break */ deliberate_fall_through
98843 }
98844 case TK_BETWEEN:
98845 case TK_EQ:
98846 case TK_NE:
98847 case TK_LT:
98848 case TK_LE:
98849 case TK_GT:
98850 case TK_GE: {
98851 int nLeft, nRight;
98852 if( pParse->db->mallocFailed ) break;
98853 assert( pExpr->pLeft!=0 );
98854 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
98855 if( pExpr->op==TK_BETWEEN ){
98856 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
98857 if( nRight==nLeft ){
98858 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
98859 }
98860 }else{
98861 assert( pExpr->pRight!=0 );
98862 nRight = sqlite3ExprVectorSize(pExpr->pRight);
98863 }
98864 if( nLeft!=nRight ){
98865 testcase( pExpr->op==TK_EQ );
98866 testcase( pExpr->op==TK_NE );
98867 testcase( pExpr->op==TK_LT );
98868 testcase( pExpr->op==TK_LE );
98869 testcase( pExpr->op==TK_GT );
98870 testcase( pExpr->op==TK_GE );
98871 testcase( pExpr->op==TK_IS );
98872 testcase( pExpr->op==TK_ISNOT );
98873 testcase( pExpr->op==TK_BETWEEN );
98874 sqlite3ErrorMsg(pParse, "row value misused");
98875 }
98876 break;
98877 }
98878 }
98879 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
98880}
98881
98882/*
98883** pEList is a list of expressions which are really the result set of the
98884** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
98885** This routine checks to see if pE is a simple identifier which corresponds
98886** to the AS-name of one of the terms of the expression list. If it is,
98887** this routine return an integer between 1 and N where N is the number of
98888** elements in pEList, corresponding to the matching entry. If there is
98889** no match, or if pE is not a simple identifier, then this routine
98890** return 0.
98891**
98892** pEList has been resolved. pE has not.
98893*/
98894static int resolveAsName(
98895 Parse *pParse, /* Parsing context for error messages */
98896 ExprList *pEList, /* List of expressions to scan */
98897 Expr *pE /* Expression we are trying to match */
98898){
98899 int i; /* Loop counter */
98900
98901 UNUSED_PARAMETER(pParse);
98902
98903 if( pE->op==TK_ID ){
98904 char *zCol = pE->u.zToken;
98905 for(i=0; i<pEList->nExpr; i++){
98906 if( pEList->a[i].eEName==ENAME_NAME
98907 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
98908 ){
98909 return i+1;
98910 }
98911 }
98912 }
98913 return 0;
98914}
98915
98916/*
98917** pE is a pointer to an expression which is a single term in the
98918** ORDER BY of a compound SELECT. The expression has not been
98919** name resolved.
98920**
98921** At the point this routine is called, we already know that the
98922** ORDER BY term is not an integer index into the result set. That
98923** case is handled by the calling routine.
98924**
98925** Attempt to match pE against result set columns in the left-most
98926** SELECT statement. Return the index i of the matching column,
98927** as an indication to the caller that it should sort by the i-th column.
98928** The left-most column is 1. In other words, the value returned is the
98929** same integer value that would be used in the SQL statement to indicate
98930** the column.
98931**
98932** If there is no match, return 0. Return -1 if an error occurs.
98933*/
98934static int resolveOrderByTermToExprList(
98935 Parse *pParse, /* Parsing context for error messages */
98936 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
98937 Expr *pE /* The specific ORDER BY term */
98938){
98939 int i; /* Loop counter */
98940 ExprList *pEList; /* The columns of the result set */
98941 NameContext nc; /* Name context for resolving pE */
98942 sqlite3 *db; /* Database connection */
98943 int rc; /* Return code from subprocedures */
98944 u8 savedSuppErr; /* Saved value of db->suppressErr */
98945
98946 assert( sqlite3ExprIsInteger(pE, &i)==0 );
98947 pEList = pSelect->pEList;
98948
98949 /* Resolve all names in the ORDER BY term expression
98950 */
98951 memset(&nc, 0, sizeof(nc));
98952 nc.pParse = pParse;
98953 nc.pSrcList = pSelect->pSrc;
98954 nc.uNC.pEList = pEList;
98955 nc.ncFlags = NC_AllowAgg|NC_UEList;
98956 nc.nErr = 0;
98957 db = pParse->db;
98958 savedSuppErr = db->suppressErr;
98959 if( IN_RENAME_OBJECT==0 ) db->suppressErr = 1;
98960 rc = sqlite3ResolveExprNames(&nc, pE);
98961 db->suppressErr = savedSuppErr;
98962 if( rc ) return 0;
98963
98964 /* Try to match the ORDER BY expression against an expression
98965 ** in the result set. Return an 1-based index of the matching
98966 ** result-set entry.
98967 */
98968 for(i=0; i<pEList->nExpr; i++){
98969 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
98970 return i+1;
98971 }
98972 }
98973
98974 /* If no match, return 0. */
98975 return 0;
98976}
98977
98978/*
98979** Generate an ORDER BY or GROUP BY term out-of-range error.
98980*/
98981static void resolveOutOfRangeError(
98982 Parse *pParse, /* The error context into which to write the error */
98983 const char *zType, /* "ORDER" or "GROUP" */
98984 int i, /* The index (1-based) of the term out of range */
98985 int mx /* Largest permissible value of i */
98986){
98987 sqlite3ErrorMsg(pParse,
98988 "%r %s BY term out of range - should be "
98989 "between 1 and %d", i, zType, mx);
98990}
98991
98992/*
98993** Analyze the ORDER BY clause in a compound SELECT statement. Modify
98994** each term of the ORDER BY clause is a constant integer between 1
98995** and N where N is the number of columns in the compound SELECT.
98996**
98997** ORDER BY terms that are already an integer between 1 and N are
98998** unmodified. ORDER BY terms that are integers outside the range of
98999** 1 through N generate an error. ORDER BY terms that are expressions
99000** are matched against result set expressions of compound SELECT
99001** beginning with the left-most SELECT and working toward the right.
99002** At the first match, the ORDER BY expression is transformed into
99003** the integer column number.
99004**
99005** Return the number of errors seen.
99006*/
99007static int resolveCompoundOrderBy(
99008 Parse *pParse, /* Parsing context. Leave error messages here */
99009 Select *pSelect /* The SELECT statement containing the ORDER BY */
99010){
99011 int i;
99012 ExprList *pOrderBy;
99013 ExprList *pEList;
99014 sqlite3 *db;
99015 int moreToDo = 1;
99016
99017 pOrderBy = pSelect->pOrderBy;
99018 if( pOrderBy==0 ) return 0;
99019 db = pParse->db;
99020 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
99021 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
99022 return 1;
99023 }
99024 for(i=0; i<pOrderBy->nExpr; i++){
99025 pOrderBy->a[i].done = 0;
99026 }
99027 pSelect->pNext = 0;
99028 while( pSelect->pPrior ){
99029 pSelect->pPrior->pNext = pSelect;
99030 pSelect = pSelect->pPrior;
99031 }
99032 while( pSelect && moreToDo ){
99033 struct ExprList_item *pItem;
99034 moreToDo = 0;
99035 pEList = pSelect->pEList;
99036 assert( pEList!=0 );
99037 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
99038 int iCol = -1;
99039 Expr *pE, *pDup;
99040 if( pItem->done ) continue;
99041 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
99042 if( sqlite3ExprIsInteger(pE, &iCol) ){
99043 if( iCol<=0 || iCol>pEList->nExpr ){
99044 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
99045 return 1;
99046 }
99047 }else{
99048 iCol = resolveAsName(pParse, pEList, pE);
99049 if( iCol==0 ){
99050 /* Now test if expression pE matches one of the values returned
99051 ** by pSelect. In the usual case this is done by duplicating the
99052 ** expression, resolving any symbols in it, and then comparing
99053 ** it against each expression returned by the SELECT statement.
99054 ** Once the comparisons are finished, the duplicate expression
99055 ** is deleted.
99056 **
99057 ** Or, if this is running as part of an ALTER TABLE operation,
99058 ** resolve the symbols in the actual expression, not a duplicate.
99059 ** And, if one of the comparisons is successful, leave the expression
99060 ** as is instead of transforming it to an integer as in the usual
99061 ** case. This allows the code in alter.c to modify column
99062 ** refererences within the ORDER BY expression as required. */
99063 if( IN_RENAME_OBJECT ){
99064 pDup = pE;
99065 }else{
99066 pDup = sqlite3ExprDup(db, pE, 0);
99067 }
99068 if( !db->mallocFailed ){
99069 assert(pDup);
99070 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
99071 }
99072 if( !IN_RENAME_OBJECT ){
99073 sqlite3ExprDelete(db, pDup);
99074 }
99075 }
99076 }
99077 if( iCol>0 ){
99078 /* Convert the ORDER BY term into an integer column number iCol,
99079 ** taking care to preserve the COLLATE clause if it exists */
99080 if( !IN_RENAME_OBJECT ){
99081 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
99082 if( pNew==0 ) return 1;
99083 pNew->flags |= EP_IntValue;
99084 pNew->u.iValue = iCol;
99085 if( pItem->pExpr==pE ){
99086 pItem->pExpr = pNew;
99087 }else{
99088 Expr *pParent = pItem->pExpr;
99089 assert( pParent->op==TK_COLLATE );
99090 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
99091 assert( pParent->pLeft==pE );
99092 pParent->pLeft = pNew;
99093 }
99094 sqlite3ExprDelete(db, pE);
99095 pItem->u.x.iOrderByCol = (u16)iCol;
99096 }
99097 pItem->done = 1;
99098 }else{
99099 moreToDo = 1;
99100 }
99101 }
99102 pSelect = pSelect->pNext;
99103 }
99104 for(i=0; i<pOrderBy->nExpr; i++){
99105 if( pOrderBy->a[i].done==0 ){
99106 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
99107 "column in the result set", i+1);
99108 return 1;
99109 }
99110 }
99111 return 0;
99112}
99113
99114/*
99115** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
99116** the SELECT statement pSelect. If any term is reference to a
99117** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
99118** field) then convert that term into a copy of the corresponding result set
99119** column.
99120**
99121** If any errors are detected, add an error message to pParse and
99122** return non-zero. Return zero if no errors are seen.
99123*/
99124SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
99125 Parse *pParse, /* Parsing context. Leave error messages here */
99126 Select *pSelect, /* The SELECT statement containing the clause */
99127 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
99128 const char *zType /* "ORDER" or "GROUP" */
99129){
99130 int i;
99131 sqlite3 *db = pParse->db;
99132 ExprList *pEList;
99133 struct ExprList_item *pItem;
99134
99135 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
99136 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
99137 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
99138 return 1;
99139 }
99140 pEList = pSelect->pEList;
99141 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
99142 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
99143 if( pItem->u.x.iOrderByCol ){
99144 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
99145 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
99146 return 1;
99147 }
99148 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
99149 zType,0);
99150 }
99151 }
99152 return 0;
99153}
99154
99155#ifndef SQLITE_OMIT_WINDOWFUNC
99156/*
99157** Walker callback for windowRemoveExprFromSelect().
99158*/
99159static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
99160 UNUSED_PARAMETER(pWalker);
99161 if( ExprHasProperty(pExpr, EP_WinFunc) ){
99162 Window *pWin = pExpr->y.pWin;
99163 sqlite3WindowUnlinkFromSelect(pWin);
99164 }
99165 return WRC_Continue;
99166}
99167
99168/*
99169** Remove any Window objects owned by the expression pExpr from the
99170** Select.pWin list of Select object pSelect.
99171*/
99172static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
99173 if( pSelect->pWin ){
99174 Walker sWalker;
99175 memset(&sWalker, 0, sizeof(Walker));
99176 sWalker.xExprCallback = resolveRemoveWindowsCb;
99177 sWalker.u.pSelect = pSelect;
99178 sqlite3WalkExpr(&sWalker, pExpr);
99179 }
99180}
99181#else
99182# define windowRemoveExprFromSelect(a, b)
99183#endif /* SQLITE_OMIT_WINDOWFUNC */
99184
99185/*
99186** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
99187** The Name context of the SELECT statement is pNC. zType is either
99188** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
99189**
99190** This routine resolves each term of the clause into an expression.
99191** If the order-by term is an integer I between 1 and N (where N is the
99192** number of columns in the result set of the SELECT) then the expression
99193** in the resolution is a copy of the I-th result-set expression. If
99194** the order-by term is an identifier that corresponds to the AS-name of
99195** a result-set expression, then the term resolves to a copy of the
99196** result-set expression. Otherwise, the expression is resolved in
99197** the usual way - using sqlite3ResolveExprNames().
99198**
99199** This routine returns the number of errors. If errors occur, then
99200** an appropriate error message might be left in pParse. (OOM errors
99201** excepted.)
99202*/
99203static int resolveOrderGroupBy(
99204 NameContext *pNC, /* The name context of the SELECT statement */
99205 Select *pSelect, /* The SELECT statement holding pOrderBy */
99206 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
99207 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
99208){
99209 int i, j; /* Loop counters */
99210 int iCol; /* Column number */
99211 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
99212 Parse *pParse; /* Parsing context */
99213 int nResult; /* Number of terms in the result set */
99214
99215 if( pOrderBy==0 ) return 0;
99216 nResult = pSelect->pEList->nExpr;
99217 pParse = pNC->pParse;
99218 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
99219 Expr *pE = pItem->pExpr;
99220 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
99221 if( zType[0]!='G' ){
99222 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
99223 if( iCol>0 ){
99224 /* If an AS-name match is found, mark this ORDER BY column as being
99225 ** a copy of the iCol-th result-set column. The subsequent call to
99226 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
99227 ** copy of the iCol-th result-set expression. */
99228 pItem->u.x.iOrderByCol = (u16)iCol;
99229 continue;
99230 }
99231 }
99232 if( sqlite3ExprIsInteger(pE2, &iCol) ){
99233 /* The ORDER BY term is an integer constant. Again, set the column
99234 ** number so that sqlite3ResolveOrderGroupBy() will convert the
99235 ** order-by term to a copy of the result-set expression */
99236 if( iCol<1 || iCol>0xffff ){
99237 resolveOutOfRangeError(pParse, zType, i+1, nResult);
99238 return 1;
99239 }
99240 pItem->u.x.iOrderByCol = (u16)iCol;
99241 continue;
99242 }
99243
99244 /* Otherwise, treat the ORDER BY term as an ordinary expression */
99245 pItem->u.x.iOrderByCol = 0;
99246 if( sqlite3ResolveExprNames(pNC, pE) ){
99247 return 1;
99248 }
99249 for(j=0; j<pSelect->pEList->nExpr; j++){
99250 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
99251 /* Since this expresion is being changed into a reference
99252 ** to an identical expression in the result set, remove all Window
99253 ** objects belonging to the expression from the Select.pWin list. */
99254 windowRemoveExprFromSelect(pSelect, pE);
99255 pItem->u.x.iOrderByCol = j+1;
99256 }
99257 }
99258 }
99259 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
99260}
99261
99262/*
99263** Resolve names in the SELECT statement p and all of its descendants.
99264*/
99265static int resolveSelectStep(Walker *pWalker, Select *p){
99266 NameContext *pOuterNC; /* Context that contains this SELECT */
99267 NameContext sNC; /* Name context of this SELECT */
99268 int isCompound; /* True if p is a compound select */
99269 int nCompound; /* Number of compound terms processed so far */
99270 Parse *pParse; /* Parsing context */
99271 int i; /* Loop counter */
99272 ExprList *pGroupBy; /* The GROUP BY clause */
99273 Select *pLeftmost; /* Left-most of SELECT of a compound */
99274 sqlite3 *db; /* Database connection */
99275
99276
99277 assert( p!=0 );
99278 if( p->selFlags & SF_Resolved ){
99279 return WRC_Prune;
99280 }
99281 pOuterNC = pWalker->u.pNC;
99282 pParse = pWalker->pParse;
99283 db = pParse->db;
99284
99285 /* Normally sqlite3SelectExpand() will be called first and will have
99286 ** already expanded this SELECT. However, if this is a subquery within
99287 ** an expression, sqlite3ResolveExprNames() will be called without a
99288 ** prior call to sqlite3SelectExpand(). When that happens, let
99289 ** sqlite3SelectPrep() do all of the processing for this SELECT.
99290 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
99291 ** this routine in the correct order.
99292 */
99293 if( (p->selFlags & SF_Expanded)==0 ){
99294 sqlite3SelectPrep(pParse, p, pOuterNC);
99295 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
99296 }
99297
99298 isCompound = p->pPrior!=0;
99299 nCompound = 0;
99300 pLeftmost = p;
99301 while( p ){
99302 assert( (p->selFlags & SF_Expanded)!=0 );
99303 assert( (p->selFlags & SF_Resolved)==0 );
99304 p->selFlags |= SF_Resolved;
99305
99306 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
99307 ** are not allowed to refer to any names, so pass an empty NameContext.
99308 */
99309 memset(&sNC, 0, sizeof(sNC));
99310 sNC.pParse = pParse;
99311 sNC.pWinSelect = p;
99312 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
99313 return WRC_Abort;
99314 }
99315
99316 /* If the SF_Converted flags is set, then this Select object was
99317 ** was created by the convertCompoundSelectToSubquery() function.
99318 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
99319 ** as if it were part of the sub-query, not the parent. This block
99320 ** moves the pOrderBy down to the sub-query. It will be moved back
99321 ** after the names have been resolved. */
99322 if( p->selFlags & SF_Converted ){
99323 Select *pSub = p->pSrc->a[0].pSelect;
99324 assert( p->pSrc->nSrc==1 && p->pOrderBy );
99325 assert( pSub->pPrior && pSub->pOrderBy==0 );
99326 pSub->pOrderBy = p->pOrderBy;
99327 p->pOrderBy = 0;
99328 }
99329
99330 /* Recursively resolve names in all subqueries
99331 */
99332 for(i=0; i<p->pSrc->nSrc; i++){
99333 struct SrcList_item *pItem = &p->pSrc->a[i];
99334 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
99335 NameContext *pNC; /* Used to iterate name contexts */
99336 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
99337 const char *zSavedContext = pParse->zAuthContext;
99338
99339 /* Count the total number of references to pOuterNC and all of its
99340 ** parent contexts. After resolving references to expressions in
99341 ** pItem->pSelect, check if this value has changed. If so, then
99342 ** SELECT statement pItem->pSelect must be correlated. Set the
99343 ** pItem->fg.isCorrelated flag if this is the case. */
99344 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
99345
99346 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
99347 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
99348 pParse->zAuthContext = zSavedContext;
99349 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
99350
99351 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
99352 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
99353 pItem->fg.isCorrelated = (nRef!=0);
99354 }
99355 }
99356
99357 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
99358 ** resolve the result-set expression list.
99359 */
99360 sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
99361 sNC.pSrcList = p->pSrc;
99362 sNC.pNext = pOuterNC;
99363
99364 /* Resolve names in the result set. */
99365 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
99366 sNC.ncFlags &= ~NC_AllowWin;
99367
99368 /* If there are no aggregate functions in the result-set, and no GROUP BY
99369 ** expression, do not allow aggregates in any of the other expressions.
99370 */
99371 assert( (p->selFlags & SF_Aggregate)==0 );
99372 pGroupBy = p->pGroupBy;
99373 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
99374 assert( NC_MinMaxAgg==SF_MinMaxAgg );
99375 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
99376 }else{
99377 sNC.ncFlags &= ~NC_AllowAgg;
99378 }
99379
99380 /* If a HAVING clause is present, then there must be a GROUP BY clause.
99381 */
99382 if( p->pHaving && !pGroupBy ){
99383 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
99384 return WRC_Abort;
99385 }
99386
99387 /* Add the output column list to the name-context before parsing the
99388 ** other expressions in the SELECT statement. This is so that
99389 ** expressions in the WHERE clause (etc.) can refer to expressions by
99390 ** aliases in the result set.
99391 **
99392 ** Minor point: If this is the case, then the expression will be
99393 ** re-evaluated for each reference to it.
99394 */
99395 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
99396 sNC.uNC.pEList = p->pEList;
99397 sNC.ncFlags |= NC_UEList;
99398 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
99399 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
99400
99401 /* Resolve names in table-valued-function arguments */
99402 for(i=0; i<p->pSrc->nSrc; i++){
99403 struct SrcList_item *pItem = &p->pSrc->a[i];
99404 if( pItem->fg.isTabFunc
99405 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
99406 ){
99407 return WRC_Abort;
99408 }
99409 }
99410
99411 /* The ORDER BY and GROUP BY clauses may not refer to terms in
99412 ** outer queries
99413 */
99414 sNC.pNext = 0;
99415 sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
99416
99417 /* If this is a converted compound query, move the ORDER BY clause from
99418 ** the sub-query back to the parent query. At this point each term
99419 ** within the ORDER BY clause has been transformed to an integer value.
99420 ** These integers will be replaced by copies of the corresponding result
99421 ** set expressions by the call to resolveOrderGroupBy() below. */
99422 if( p->selFlags & SF_Converted ){
99423 Select *pSub = p->pSrc->a[0].pSelect;
99424 p->pOrderBy = pSub->pOrderBy;
99425 pSub->pOrderBy = 0;
99426 }
99427
99428 /* Process the ORDER BY clause for singleton SELECT statements.
99429 ** The ORDER BY clause for compounds SELECT statements is handled
99430 ** below, after all of the result-sets for all of the elements of
99431 ** the compound have been resolved.
99432 **
99433 ** If there is an ORDER BY clause on a term of a compound-select other
99434 ** than the right-most term, then that is a syntax error. But the error
99435 ** is not detected until much later, and so we need to go ahead and
99436 ** resolve those symbols on the incorrect ORDER BY for consistency.
99437 */
99438 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
99439 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
99440 ){
99441 return WRC_Abort;
99442 }
99443 if( db->mallocFailed ){
99444 return WRC_Abort;
99445 }
99446 sNC.ncFlags &= ~NC_AllowWin;
99447
99448 /* Resolve the GROUP BY clause. At the same time, make sure
99449 ** the GROUP BY clause does not contain aggregate functions.
99450 */
99451 if( pGroupBy ){
99452 struct ExprList_item *pItem;
99453
99454 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
99455 return WRC_Abort;
99456 }
99457 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
99458 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
99459 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
99460 "the GROUP BY clause");
99461 return WRC_Abort;
99462 }
99463 }
99464 }
99465
99466#ifndef SQLITE_OMIT_WINDOWFUNC
99467 if( IN_RENAME_OBJECT ){
99468 Window *pWin;
99469 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
99470 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
99471 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
99472 ){
99473 return WRC_Abort;
99474 }
99475 }
99476 }
99477#endif
99478
99479 /* If this is part of a compound SELECT, check that it has the right
99480 ** number of expressions in the select list. */
99481 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
99482 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
99483 return WRC_Abort;
99484 }
99485
99486 /* Advance to the next term of the compound
99487 */
99488 p = p->pPrior;
99489 nCompound++;
99490 }
99491
99492 /* Resolve the ORDER BY on a compound SELECT after all terms of
99493 ** the compound have been resolved.
99494 */
99495 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
99496 return WRC_Abort;
99497 }
99498
99499 return WRC_Prune;
99500}
99501
99502/*
99503** This routine walks an expression tree and resolves references to
99504** table columns and result-set columns. At the same time, do error
99505** checking on function usage and set a flag if any aggregate functions
99506** are seen.
99507**
99508** To resolve table columns references we look for nodes (or subtrees) of the
99509** form X.Y.Z or Y.Z or just Z where
99510**
99511** X: The name of a database. Ex: "main" or "temp" or
99512** the symbolic name assigned to an ATTACH-ed database.
99513**
99514** Y: The name of a table in a FROM clause. Or in a trigger
99515** one of the special names "old" or "new".
99516**
99517** Z: The name of a column in table Y.
99518**
99519** The node at the root of the subtree is modified as follows:
99520**
99521** Expr.op Changed to TK_COLUMN
99522** Expr.pTab Points to the Table object for X.Y
99523** Expr.iColumn The column index in X.Y. -1 for the rowid.
99524** Expr.iTable The VDBE cursor number for X.Y
99525**
99526**
99527** To resolve result-set references, look for expression nodes of the
99528** form Z (with no X and Y prefix) where the Z matches the right-hand
99529** size of an AS clause in the result-set of a SELECT. The Z expression
99530** is replaced by a copy of the left-hand side of the result-set expression.
99531** Table-name and function resolution occurs on the substituted expression
99532** tree. For example, in:
99533**
99534** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
99535**
99536** The "x" term of the order by is replaced by "a+b" to render:
99537**
99538** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
99539**
99540** Function calls are checked to make sure that the function is
99541** defined and that the correct number of arguments are specified.
99542** If the function is an aggregate function, then the NC_HasAgg flag is
99543** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
99544** If an expression contains aggregate functions then the EP_Agg
99545** property on the expression is set.
99546**
99547** An error message is left in pParse if anything is amiss. The number
99548** if errors is returned.
99549*/
99550SQLITE_PRIVATE int sqlite3ResolveExprNames(
99551 NameContext *pNC, /* Namespace to resolve expressions in. */
99552 Expr *pExpr /* The expression to be analyzed. */
99553){
99554 int savedHasAgg;
99555 Walker w;
99556
99557 if( pExpr==0 ) return SQLITE_OK;
99558 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99559 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99560 w.pParse = pNC->pParse;
99561 w.xExprCallback = resolveExprStep;
99562 w.xSelectCallback = resolveSelectStep;
99563 w.xSelectCallback2 = 0;
99564 w.u.pNC = pNC;
99565#if SQLITE_MAX_EXPR_DEPTH>0
99566 w.pParse->nHeight += pExpr->nHeight;
99567 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
99568 return SQLITE_ERROR;
99569 }
99570#endif
99571 sqlite3WalkExpr(&w, pExpr);
99572#if SQLITE_MAX_EXPR_DEPTH>0
99573 w.pParse->nHeight -= pExpr->nHeight;
99574#endif
99575 assert( EP_Agg==NC_HasAgg );
99576 assert( EP_Win==NC_HasWin );
99577 testcase( pNC->ncFlags & NC_HasAgg );
99578 testcase( pNC->ncFlags & NC_HasWin );
99579 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
99580 pNC->ncFlags |= savedHasAgg;
99581 return pNC->nErr>0 || w.pParse->nErr>0;
99582}
99583
99584/*
99585** Resolve all names for all expression in an expression list. This is
99586** just like sqlite3ResolveExprNames() except that it works for an expression
99587** list rather than a single expression.
99588*/
99589SQLITE_PRIVATE int sqlite3ResolveExprListNames(
99590 NameContext *pNC, /* Namespace to resolve expressions in. */
99591 ExprList *pList /* The expression list to be analyzed. */
99592){
99593 int i;
99594 int savedHasAgg = 0;
99595 Walker w;
99596 if( pList==0 ) return WRC_Continue;
99597 w.pParse = pNC->pParse;
99598 w.xExprCallback = resolveExprStep;
99599 w.xSelectCallback = resolveSelectStep;
99600 w.xSelectCallback2 = 0;
99601 w.u.pNC = pNC;
99602 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99603 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99604 for(i=0; i<pList->nExpr; i++){
99605 Expr *pExpr = pList->a[i].pExpr;
99606 if( pExpr==0 ) continue;
99607#if SQLITE_MAX_EXPR_DEPTH>0
99608 w.pParse->nHeight += pExpr->nHeight;
99609 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
99610 return WRC_Abort;
99611 }
99612#endif
99613 sqlite3WalkExpr(&w, pExpr);
99614#if SQLITE_MAX_EXPR_DEPTH>0
99615 w.pParse->nHeight -= pExpr->nHeight;
99616#endif
99617 assert( EP_Agg==NC_HasAgg );
99618 assert( EP_Win==NC_HasWin );
99619 testcase( pNC->ncFlags & NC_HasAgg );
99620 testcase( pNC->ncFlags & NC_HasWin );
99621 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin) ){
99622 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
99623 savedHasAgg |= pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99624 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
99625 }
99626 if( pNC->nErr>0 || w.pParse->nErr>0 ) return WRC_Abort;
99627 }
99628 pNC->ncFlags |= savedHasAgg;
99629 return WRC_Continue;
99630}
99631
99632/*
99633** Resolve all names in all expressions of a SELECT and in all
99634** decendents of the SELECT, including compounds off of p->pPrior,
99635** subqueries in expressions, and subqueries used as FROM clause
99636** terms.
99637**
99638** See sqlite3ResolveExprNames() for a description of the kinds of
99639** transformations that occur.
99640**
99641** All SELECT statements should have been expanded using
99642** sqlite3SelectExpand() prior to invoking this routine.
99643*/
99644SQLITE_PRIVATE void sqlite3ResolveSelectNames(
99645 Parse *pParse, /* The parser context */
99646 Select *p, /* The SELECT statement being coded. */
99647 NameContext *pOuterNC /* Name context for parent SELECT statement */
99648){
99649 Walker w;
99650
99651 assert( p!=0 );
99652 w.xExprCallback = resolveExprStep;
99653 w.xSelectCallback = resolveSelectStep;
99654 w.xSelectCallback2 = 0;
99655 w.pParse = pParse;
99656 w.u.pNC = pOuterNC;
99657 sqlite3WalkSelect(&w, p);
99658}
99659
99660/*
99661** Resolve names in expressions that can only reference a single table
99662** or which cannot reference any tables at all. Examples:
99663**
99664** "type" flag
99665** ------------
99666** (1) CHECK constraints NC_IsCheck
99667** (2) WHERE clauses on partial indices NC_PartIdx
99668** (3) Expressions in indexes on expressions NC_IdxExpr
99669** (4) Expression arguments to VACUUM INTO. 0
99670** (5) GENERATED ALWAYS as expressions NC_GenCol
99671**
99672** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
99673** nodes of the expression is set to -1 and the Expr.iColumn value is
99674** set to the column number. In case (4), TK_COLUMN nodes cause an error.
99675**
99676** Any errors cause an error message to be set in pParse.
99677*/
99678SQLITE_PRIVATE int sqlite3ResolveSelfReference(
99679 Parse *pParse, /* Parsing context */
99680 Table *pTab, /* The table being referenced, or NULL */
99681 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
99682 Expr *pExpr, /* Expression to resolve. May be NULL. */
99683 ExprList *pList /* Expression list to resolve. May be NULL. */
99684){
99685 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
99686 NameContext sNC; /* Name context for pParse->pNewTable */
99687 int rc;
99688
99689 assert( type==0 || pTab!=0 );
99690 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
99691 || type==NC_GenCol || pTab==0 );
99692 memset(&sNC, 0, sizeof(sNC));
99693 memset(&sSrc, 0, sizeof(sSrc));
99694 if( pTab ){
99695 sSrc.nSrc = 1;
99696 sSrc.a[0].zName = pTab->zName;
99697 sSrc.a[0].pTab = pTab;
99698 sSrc.a[0].iCursor = -1;
99699 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
99700 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
99701 ** schema elements */
99702 type |= NC_FromDDL;
99703 }
99704 }
99705 sNC.pParse = pParse;
99706 sNC.pSrcList = &sSrc;
99707 sNC.ncFlags = type | NC_IsDDL;
99708 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc;
99709 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
99710 return rc;
99711}
99712
99713/************** End of resolve.c *********************************************/
99714/************** Begin file expr.c ********************************************/
99715/*
99716** 2001 September 15
99717**
99718** The author disclaims copyright to this source code. In place of
99719** a legal notice, here is a blessing:
99720**
99721** May you do good and not evil.
99722** May you find forgiveness for yourself and forgive others.
99723** May you share freely, never taking more than you give.
99724**
99725*************************************************************************
99726** This file contains routines used for analyzing expressions and
99727** for generating VDBE code that evaluates expressions in SQLite.
99728*/
99729/* #include "sqliteInt.h" */
99730
99731/* Forward declarations */
99732static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
99733static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
99734
99735/*
99736** Return the affinity character for a single column of a table.
99737*/
99738SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
99739 assert( iCol<pTab->nCol );
99740 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
99741}
99742
99743/*
99744** Return the 'affinity' of the expression pExpr if any.
99745**
99746** If pExpr is a column, a reference to a column via an 'AS' alias,
99747** or a sub-select with a column as the return value, then the
99748** affinity of that column is returned. Otherwise, 0x00 is returned,
99749** indicating no affinity for the expression.
99750**
99751** i.e. the WHERE clause expressions in the following statements all
99752** have an affinity:
99753**
99754** CREATE TABLE t1(a);
99755** SELECT * FROM t1 WHERE a;
99756** SELECT a AS b FROM t1 WHERE b;
99757** SELECT * FROM t1 WHERE (select a from t1);
99758*/
99759SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
99760 int op;
99761 while( ExprHasProperty(pExpr, EP_Skip) ){
99762 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99763 pExpr = pExpr->pLeft;
99764 assert( pExpr!=0 );
99765 }
99766 op = pExpr->op;
99767 if( op==TK_SELECT ){
99768 assert( pExpr->flags&EP_xIsSelect );
99769 assert( pExpr->x.pSelect!=0 );
99770 assert( pExpr->x.pSelect->pEList!=0 );
99771 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
99772 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
99773 }
99774 if( op==TK_REGISTER ) op = pExpr->op2;
99775#ifndef SQLITE_OMIT_CAST
99776 if( op==TK_CAST ){
99777 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99778 return sqlite3AffinityType(pExpr->u.zToken, 0);
99779 }
99780#endif
99781 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
99782 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
99783 }
99784 if( op==TK_SELECT_COLUMN ){
99785 assert( pExpr->pLeft->flags&EP_xIsSelect );
99786 return sqlite3ExprAffinity(
99787 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
99788 );
99789 }
99790 if( op==TK_VECTOR ){
99791 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
99792 }
99793 return pExpr->affExpr;
99794}
99795
99796/*
99797** Set the collating sequence for expression pExpr to be the collating
99798** sequence named by pToken. Return a pointer to a new Expr node that
99799** implements the COLLATE operator.
99800**
99801** If a memory allocation error occurs, that fact is recorded in pParse->db
99802** and the pExpr parameter is returned unchanged.
99803*/
99804SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
99805 Parse *pParse, /* Parsing context */
99806 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
99807 const Token *pCollName, /* Name of collating sequence */
99808 int dequote /* True to dequote pCollName */
99809){
99810 if( pCollName->n>0 ){
99811 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
99812 if( pNew ){
99813 pNew->pLeft = pExpr;
99814 pNew->flags |= EP_Collate|EP_Skip;
99815 pExpr = pNew;
99816 }
99817 }
99818 return pExpr;
99819}
99820SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
99821 Token s;
99822 assert( zC!=0 );
99823 sqlite3TokenInit(&s, (char*)zC);
99824 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
99825}
99826
99827/*
99828** Skip over any TK_COLLATE operators.
99829*/
99830SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
99831 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
99832 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99833 pExpr = pExpr->pLeft;
99834 }
99835 return pExpr;
99836}
99837
99838/*
99839** Skip over any TK_COLLATE operators and/or any unlikely()
99840** or likelihood() or likely() functions at the root of an
99841** expression.
99842*/
99843SQLITE_PRIVATE Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
99844 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
99845 if( ExprHasProperty(pExpr, EP_Unlikely) ){
99846 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99847 assert( pExpr->x.pList->nExpr>0 );
99848 assert( pExpr->op==TK_FUNCTION );
99849 pExpr = pExpr->x.pList->a[0].pExpr;
99850 }else{
99851 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW );
99852 pExpr = pExpr->pLeft;
99853 }
99854 }
99855 return pExpr;
99856}
99857
99858/*
99859** Return the collation sequence for the expression pExpr. If
99860** there is no defined collating sequence, return NULL.
99861**
99862** See also: sqlite3ExprNNCollSeq()
99863**
99864** The sqlite3ExprNNCollSeq() works the same exact that it returns the
99865** default collation if pExpr has no defined collation.
99866**
99867** The collating sequence might be determined by a COLLATE operator
99868** or by the presence of a column with a defined collating sequence.
99869** COLLATE operators take first precedence. Left operands take
99870** precedence over right operands.
99871*/
99872SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
99873 sqlite3 *db = pParse->db;
99874 CollSeq *pColl = 0;
99875 const Expr *p = pExpr;
99876 while( p ){
99877 int op = p->op;
99878 if( op==TK_REGISTER ) op = p->op2;
99879 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
99880 && p->y.pTab!=0
99881 ){
99882 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
99883 ** a TK_COLUMN but was previously evaluated and cached in a register */
99884 int j = p->iColumn;
99885 if( j>=0 ){
99886 const char *zColl = p->y.pTab->aCol[j].zColl;
99887 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
99888 }
99889 break;
99890 }
99891 if( op==TK_CAST || op==TK_UPLUS ){
99892 p = p->pLeft;
99893 continue;
99894 }
99895 if( op==TK_VECTOR ){
99896 p = p->x.pList->a[0].pExpr;
99897 continue;
99898 }
99899 if( op==TK_COLLATE ){
99900 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
99901 break;
99902 }
99903 if( p->flags & EP_Collate ){
99904 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
99905 p = p->pLeft;
99906 }else{
99907 Expr *pNext = p->pRight;
99908 /* The Expr.x union is never used at the same time as Expr.pRight */
99909 assert( p->x.pList==0 || p->pRight==0 );
99910 if( p->x.pList!=0
99911 && !db->mallocFailed
99912 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect))
99913 ){
99914 int i;
99915 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
99916 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
99917 pNext = p->x.pList->a[i].pExpr;
99918 break;
99919 }
99920 }
99921 }
99922 p = pNext;
99923 }
99924 }else{
99925 break;
99926 }
99927 }
99928 if( sqlite3CheckCollSeq(pParse, pColl) ){
99929 pColl = 0;
99930 }
99931 return pColl;
99932}
99933
99934/*
99935** Return the collation sequence for the expression pExpr. If
99936** there is no defined collating sequence, return a pointer to the
99937** defautl collation sequence.
99938**
99939** See also: sqlite3ExprCollSeq()
99940**
99941** The sqlite3ExprCollSeq() routine works the same except that it
99942** returns NULL if there is no defined collation.
99943*/
99944SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
99945 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
99946 if( p==0 ) p = pParse->db->pDfltColl;
99947 assert( p!=0 );
99948 return p;
99949}
99950
99951/*
99952** Return TRUE if the two expressions have equivalent collating sequences.
99953*/
99954SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
99955 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
99956 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
99957 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
99958}
99959
99960/*
99961** pExpr is an operand of a comparison operator. aff2 is the
99962** type affinity of the other operand. This routine returns the
99963** type affinity that should be used for the comparison operator.
99964*/
99965SQLITE_PRIVATE char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
99966 char aff1 = sqlite3ExprAffinity(pExpr);
99967 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
99968 /* Both sides of the comparison are columns. If one has numeric
99969 ** affinity, use that. Otherwise use no affinity.
99970 */
99971 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
99972 return SQLITE_AFF_NUMERIC;
99973 }else{
99974 return SQLITE_AFF_BLOB;
99975 }
99976 }else{
99977 /* One side is a column, the other is not. Use the columns affinity. */
99978 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
99979 return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
99980 }
99981}
99982
99983/*
99984** pExpr is a comparison operator. Return the type affinity that should
99985** be applied to both operands prior to doing the comparison.
99986*/
99987static char comparisonAffinity(const Expr *pExpr){
99988 char aff;
99989 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
99990 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
99991 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
99992 assert( pExpr->pLeft );
99993 aff = sqlite3ExprAffinity(pExpr->pLeft);
99994 if( pExpr->pRight ){
99995 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
99996 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
99997 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
99998 }else if( aff==0 ){
99999 aff = SQLITE_AFF_BLOB;
100000 }
100001 return aff;
100002}
100003
100004/*
100005** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
100006** idx_affinity is the affinity of an indexed column. Return true
100007** if the index with affinity idx_affinity may be used to implement
100008** the comparison in pExpr.
100009*/
100010SQLITE_PRIVATE int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
100011 char aff = comparisonAffinity(pExpr);
100012 if( aff<SQLITE_AFF_TEXT ){
100013 return 1;
100014 }
100015 if( aff==SQLITE_AFF_TEXT ){
100016 return idx_affinity==SQLITE_AFF_TEXT;
100017 }
100018 return sqlite3IsNumericAffinity(idx_affinity);
100019}
100020
100021/*
100022** Return the P5 value that should be used for a binary comparison
100023** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
100024*/
100025static u8 binaryCompareP5(
100026 const Expr *pExpr1, /* Left operand */
100027 const Expr *pExpr2, /* Right operand */
100028 int jumpIfNull /* Extra flags added to P5 */
100029){
100030 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
100031 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
100032 return aff;
100033}
100034
100035/*
100036** Return a pointer to the collation sequence that should be used by
100037** a binary comparison operator comparing pLeft and pRight.
100038**
100039** If the left hand expression has a collating sequence type, then it is
100040** used. Otherwise the collation sequence for the right hand expression
100041** is used, or the default (BINARY) if neither expression has a collating
100042** type.
100043**
100044** Argument pRight (but not pLeft) may be a null pointer. In this case,
100045** it is not considered.
100046*/
100047SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
100048 Parse *pParse,
100049 const Expr *pLeft,
100050 const Expr *pRight
100051){
100052 CollSeq *pColl;
100053 assert( pLeft );
100054 if( pLeft->flags & EP_Collate ){
100055 pColl = sqlite3ExprCollSeq(pParse, pLeft);
100056 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
100057 pColl = sqlite3ExprCollSeq(pParse, pRight);
100058 }else{
100059 pColl = sqlite3ExprCollSeq(pParse, pLeft);
100060 if( !pColl ){
100061 pColl = sqlite3ExprCollSeq(pParse, pRight);
100062 }
100063 }
100064 return pColl;
100065}
100066
100067/* Expresssion p is a comparison operator. Return a collation sequence
100068** appropriate for the comparison operator.
100069**
100070** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
100071** However, if the OP_Commuted flag is set, then the order of the operands
100072** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
100073** correct collating sequence is found.
100074*/
100075SQLITE_PRIVATE CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
100076 if( ExprHasProperty(p, EP_Commuted) ){
100077 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
100078 }else{
100079 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
100080 }
100081}
100082
100083/*
100084** Generate code for a comparison operator.
100085*/
100086static int codeCompare(
100087 Parse *pParse, /* The parsing (and code generating) context */
100088 Expr *pLeft, /* The left operand */
100089 Expr *pRight, /* The right operand */
100090 int opcode, /* The comparison opcode */
100091 int in1, int in2, /* Register holding operands */
100092 int dest, /* Jump here if true. */
100093 int jumpIfNull, /* If true, jump if either operand is NULL */
100094 int isCommuted /* The comparison has been commuted */
100095){
100096 int p5;
100097 int addr;
100098 CollSeq *p4;
100099
100100 if( pParse->nErr ) return 0;
100101 if( isCommuted ){
100102 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
100103 }else{
100104 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
100105 }
100106 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
100107 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
100108 (void*)p4, P4_COLLSEQ);
100109 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
100110 return addr;
100111}
100112
100113/*
100114** Return true if expression pExpr is a vector, or false otherwise.
100115**
100116** A vector is defined as any expression that results in two or more
100117** columns of result. Every TK_VECTOR node is an vector because the
100118** parser will not generate a TK_VECTOR with fewer than two entries.
100119** But a TK_SELECT might be either a vector or a scalar. It is only
100120** considered a vector if it has two or more result columns.
100121*/
100122SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
100123 return sqlite3ExprVectorSize(pExpr)>1;
100124}
100125
100126/*
100127** If the expression passed as the only argument is of type TK_VECTOR
100128** return the number of expressions in the vector. Or, if the expression
100129** is a sub-select, return the number of columns in the sub-select. For
100130** any other type of expression, return 1.
100131*/
100132SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
100133 u8 op = pExpr->op;
100134 if( op==TK_REGISTER ) op = pExpr->op2;
100135 if( op==TK_VECTOR ){
100136 return pExpr->x.pList->nExpr;
100137 }else if( op==TK_SELECT ){
100138 return pExpr->x.pSelect->pEList->nExpr;
100139 }else{
100140 return 1;
100141 }
100142}
100143
100144/*
100145** Return a pointer to a subexpression of pVector that is the i-th
100146** column of the vector (numbered starting with 0). The caller must
100147** ensure that i is within range.
100148**
100149** If pVector is really a scalar (and "scalar" here includes subqueries
100150** that return a single column!) then return pVector unmodified.
100151**
100152** pVector retains ownership of the returned subexpression.
100153**
100154** If the vector is a (SELECT ...) then the expression returned is
100155** just the expression for the i-th term of the result set, and may
100156** not be ready for evaluation because the table cursor has not yet
100157** been positioned.
100158*/
100159SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
100160 assert( i<sqlite3ExprVectorSize(pVector) );
100161 if( sqlite3ExprIsVector(pVector) ){
100162 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
100163 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
100164 return pVector->x.pSelect->pEList->a[i].pExpr;
100165 }else{
100166 return pVector->x.pList->a[i].pExpr;
100167 }
100168 }
100169 return pVector;
100170}
100171
100172/*
100173** Compute and return a new Expr object which when passed to
100174** sqlite3ExprCode() will generate all necessary code to compute
100175** the iField-th column of the vector expression pVector.
100176**
100177** It is ok for pVector to be a scalar (as long as iField==0).
100178** In that case, this routine works like sqlite3ExprDup().
100179**
100180** The caller owns the returned Expr object and is responsible for
100181** ensuring that the returned value eventually gets freed.
100182**
100183** The caller retains ownership of pVector. If pVector is a TK_SELECT,
100184** then the returned object will reference pVector and so pVector must remain
100185** valid for the life of the returned object. If pVector is a TK_VECTOR
100186** or a scalar expression, then it can be deleted as soon as this routine
100187** returns.
100188**
100189** A trick to cause a TK_SELECT pVector to be deleted together with
100190** the returned Expr object is to attach the pVector to the pRight field
100191** of the returned TK_SELECT_COLUMN Expr object.
100192*/
100193SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
100194 Parse *pParse, /* Parsing context */
100195 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
100196 int iField /* Which column of the vector to return */
100197){
100198 Expr *pRet;
100199 if( pVector->op==TK_SELECT ){
100200 assert( pVector->flags & EP_xIsSelect );
100201 /* The TK_SELECT_COLUMN Expr node:
100202 **
100203 ** pLeft: pVector containing TK_SELECT. Not deleted.
100204 ** pRight: not used. But recursively deleted.
100205 ** iColumn: Index of a column in pVector
100206 ** iTable: 0 or the number of columns on the LHS of an assignment
100207 ** pLeft->iTable: First in an array of register holding result, or 0
100208 ** if the result is not yet computed.
100209 **
100210 ** sqlite3ExprDelete() specifically skips the recursive delete of
100211 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
100212 ** can be attached to pRight to cause this node to take ownership of
100213 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
100214 ** with the same pLeft pointer to the pVector, but only one of them
100215 ** will own the pVector.
100216 */
100217 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
100218 if( pRet ){
100219 pRet->iColumn = iField;
100220 pRet->pLeft = pVector;
100221 }
100222 assert( pRet==0 || pRet->iTable==0 );
100223 }else{
100224 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
100225 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
100226 sqlite3RenameTokenRemap(pParse, pRet, pVector);
100227 }
100228 return pRet;
100229}
100230
100231/*
100232** If expression pExpr is of type TK_SELECT, generate code to evaluate
100233** it. Return the register in which the result is stored (or, if the
100234** sub-select returns more than one column, the first in an array
100235** of registers in which the result is stored).
100236**
100237** If pExpr is not a TK_SELECT expression, return 0.
100238*/
100239static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
100240 int reg = 0;
100241#ifndef SQLITE_OMIT_SUBQUERY
100242 if( pExpr->op==TK_SELECT ){
100243 reg = sqlite3CodeSubselect(pParse, pExpr);
100244 }
100245#endif
100246 return reg;
100247}
100248
100249/*
100250** Argument pVector points to a vector expression - either a TK_VECTOR
100251** or TK_SELECT that returns more than one column. This function returns
100252** the register number of a register that contains the value of
100253** element iField of the vector.
100254**
100255** If pVector is a TK_SELECT expression, then code for it must have
100256** already been generated using the exprCodeSubselect() routine. In this
100257** case parameter regSelect should be the first in an array of registers
100258** containing the results of the sub-select.
100259**
100260** If pVector is of type TK_VECTOR, then code for the requested field
100261** is generated. In this case (*pRegFree) may be set to the number of
100262** a temporary register to be freed by the caller before returning.
100263**
100264** Before returning, output parameter (*ppExpr) is set to point to the
100265** Expr object corresponding to element iElem of the vector.
100266*/
100267static int exprVectorRegister(
100268 Parse *pParse, /* Parse context */
100269 Expr *pVector, /* Vector to extract element from */
100270 int iField, /* Field to extract from pVector */
100271 int regSelect, /* First in array of registers */
100272 Expr **ppExpr, /* OUT: Expression element */
100273 int *pRegFree /* OUT: Temp register to free */
100274){
100275 u8 op = pVector->op;
100276 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
100277 if( op==TK_REGISTER ){
100278 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
100279 return pVector->iTable+iField;
100280 }
100281 if( op==TK_SELECT ){
100282 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
100283 return regSelect+iField;
100284 }
100285 *ppExpr = pVector->x.pList->a[iField].pExpr;
100286 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
100287}
100288
100289/*
100290** Expression pExpr is a comparison between two vector values. Compute
100291** the result of the comparison (1, 0, or NULL) and write that
100292** result into register dest.
100293**
100294** The caller must satisfy the following preconditions:
100295**
100296** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
100297** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
100298** otherwise: op==pExpr->op and p5==0
100299*/
100300static void codeVectorCompare(
100301 Parse *pParse, /* Code generator context */
100302 Expr *pExpr, /* The comparison operation */
100303 int dest, /* Write results into this register */
100304 u8 op, /* Comparison operator */
100305 u8 p5 /* SQLITE_NULLEQ or zero */
100306){
100307 Vdbe *v = pParse->pVdbe;
100308 Expr *pLeft = pExpr->pLeft;
100309 Expr *pRight = pExpr->pRight;
100310 int nLeft = sqlite3ExprVectorSize(pLeft);
100311 int i;
100312 int regLeft = 0;
100313 int regRight = 0;
100314 u8 opx = op;
100315 int addrDone = sqlite3VdbeMakeLabel(pParse);
100316 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
100317
100318 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
100319 if( pParse->nErr ) return;
100320 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
100321 sqlite3ErrorMsg(pParse, "row value misused");
100322 return;
100323 }
100324 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
100325 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
100326 || pExpr->op==TK_LT || pExpr->op==TK_GT
100327 || pExpr->op==TK_LE || pExpr->op==TK_GE
100328 );
100329 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
100330 || (pExpr->op==TK_ISNOT && op==TK_NE) );
100331 assert( p5==0 || pExpr->op!=op );
100332 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
100333
100334 p5 |= SQLITE_STOREP2;
100335 if( opx==TK_LE ) opx = TK_LT;
100336 if( opx==TK_GE ) opx = TK_GT;
100337
100338 regLeft = exprCodeSubselect(pParse, pLeft);
100339 regRight = exprCodeSubselect(pParse, pRight);
100340
100341 for(i=0; 1 /*Loop exits by "break"*/; i++){
100342 int regFree1 = 0, regFree2 = 0;
100343 Expr *pL, *pR;
100344 int r1, r2;
100345 assert( i>=0 && i<nLeft );
100346 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
100347 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
100348 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5, isCommuted);
100349 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100350 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100351 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100352 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100353 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
100354 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
100355 sqlite3ReleaseTempReg(pParse, regFree1);
100356 sqlite3ReleaseTempReg(pParse, regFree2);
100357 if( i==nLeft-1 ){
100358 break;
100359 }
100360 if( opx==TK_EQ ){
100361 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
100362 p5 |= SQLITE_KEEPNULL;
100363 }else if( opx==TK_NE ){
100364 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
100365 p5 |= SQLITE_KEEPNULL;
100366 }else{
100367 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
100368 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
100369 VdbeCoverageIf(v, op==TK_LT);
100370 VdbeCoverageIf(v, op==TK_GT);
100371 VdbeCoverageIf(v, op==TK_LE);
100372 VdbeCoverageIf(v, op==TK_GE);
100373 if( i==nLeft-2 ) opx = op;
100374 }
100375 }
100376 sqlite3VdbeResolveLabel(v, addrDone);
100377}
100378
100379#if SQLITE_MAX_EXPR_DEPTH>0
100380/*
100381** Check that argument nHeight is less than or equal to the maximum
100382** expression depth allowed. If it is not, leave an error message in
100383** pParse.
100384*/
100385SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
100386 int rc = SQLITE_OK;
100387 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
100388 if( nHeight>mxHeight ){
100389 sqlite3ErrorMsg(pParse,
100390 "Expression tree is too large (maximum depth %d)", mxHeight
100391 );
100392 rc = SQLITE_ERROR;
100393 }
100394 return rc;
100395}
100396
100397/* The following three functions, heightOfExpr(), heightOfExprList()
100398** and heightOfSelect(), are used to determine the maximum height
100399** of any expression tree referenced by the structure passed as the
100400** first argument.
100401**
100402** If this maximum height is greater than the current value pointed
100403** to by pnHeight, the second parameter, then set *pnHeight to that
100404** value.
100405*/
100406static void heightOfExpr(Expr *p, int *pnHeight){
100407 if( p ){
100408 if( p->nHeight>*pnHeight ){
100409 *pnHeight = p->nHeight;
100410 }
100411 }
100412}
100413static void heightOfExprList(ExprList *p, int *pnHeight){
100414 if( p ){
100415 int i;
100416 for(i=0; i<p->nExpr; i++){
100417 heightOfExpr(p->a[i].pExpr, pnHeight);
100418 }
100419 }
100420}
100421static void heightOfSelect(Select *pSelect, int *pnHeight){
100422 Select *p;
100423 for(p=pSelect; p; p=p->pPrior){
100424 heightOfExpr(p->pWhere, pnHeight);
100425 heightOfExpr(p->pHaving, pnHeight);
100426 heightOfExpr(p->pLimit, pnHeight);
100427 heightOfExprList(p->pEList, pnHeight);
100428 heightOfExprList(p->pGroupBy, pnHeight);
100429 heightOfExprList(p->pOrderBy, pnHeight);
100430 }
100431}
100432
100433/*
100434** Set the Expr.nHeight variable in the structure passed as an
100435** argument. An expression with no children, Expr.pList or
100436** Expr.pSelect member has a height of 1. Any other expression
100437** has a height equal to the maximum height of any other
100438** referenced Expr plus one.
100439**
100440** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
100441** if appropriate.
100442*/
100443static void exprSetHeight(Expr *p){
100444 int nHeight = 0;
100445 heightOfExpr(p->pLeft, &nHeight);
100446 heightOfExpr(p->pRight, &nHeight);
100447 if( ExprHasProperty(p, EP_xIsSelect) ){
100448 heightOfSelect(p->x.pSelect, &nHeight);
100449 }else if( p->x.pList ){
100450 heightOfExprList(p->x.pList, &nHeight);
100451 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
100452 }
100453 p->nHeight = nHeight + 1;
100454}
100455
100456/*
100457** Set the Expr.nHeight variable using the exprSetHeight() function. If
100458** the height is greater than the maximum allowed expression depth,
100459** leave an error in pParse.
100460**
100461** Also propagate all EP_Propagate flags from the Expr.x.pList into
100462** Expr.flags.
100463*/
100464SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
100465 if( pParse->nErr ) return;
100466 exprSetHeight(p);
100467 sqlite3ExprCheckHeight(pParse, p->nHeight);
100468}
100469
100470/*
100471** Return the maximum height of any expression tree referenced
100472** by the select statement passed as an argument.
100473*/
100474SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
100475 int nHeight = 0;
100476 heightOfSelect(p, &nHeight);
100477 return nHeight;
100478}
100479#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
100480/*
100481** Propagate all EP_Propagate flags from the Expr.x.pList into
100482** Expr.flags.
100483*/
100484SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
100485 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
100486 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
100487 }
100488}
100489#define exprSetHeight(y)
100490#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
100491
100492/*
100493** This routine is the core allocator for Expr nodes.
100494**
100495** Construct a new expression node and return a pointer to it. Memory
100496** for this node and for the pToken argument is a single allocation
100497** obtained from sqlite3DbMalloc(). The calling function
100498** is responsible for making sure the node eventually gets freed.
100499**
100500** If dequote is true, then the token (if it exists) is dequoted.
100501** If dequote is false, no dequoting is performed. The deQuote
100502** parameter is ignored if pToken is NULL or if the token does not
100503** appear to be quoted. If the quotes were of the form "..." (double-quotes)
100504** then the EP_DblQuoted flag is set on the expression node.
100505**
100506** Special case: If op==TK_INTEGER and pToken points to a string that
100507** can be translated into a 32-bit integer, then the token is not
100508** stored in u.zToken. Instead, the integer values is written
100509** into u.iValue and the EP_IntValue flag is set. No extra storage
100510** is allocated to hold the integer text and the dequote flag is ignored.
100511*/
100512SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
100513 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
100514 int op, /* Expression opcode */
100515 const Token *pToken, /* Token argument. Might be NULL */
100516 int dequote /* True to dequote */
100517){
100518 Expr *pNew;
100519 int nExtra = 0;
100520 int iValue = 0;
100521
100522 assert( db!=0 );
100523 if( pToken ){
100524 if( op!=TK_INTEGER || pToken->z==0
100525 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
100526 nExtra = pToken->n+1;
100527 assert( iValue>=0 );
100528 }
100529 }
100530 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
100531 if( pNew ){
100532 memset(pNew, 0, sizeof(Expr));
100533 pNew->op = (u8)op;
100534 pNew->iAgg = -1;
100535 if( pToken ){
100536 if( nExtra==0 ){
100537 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
100538 pNew->u.iValue = iValue;
100539 }else{
100540 pNew->u.zToken = (char*)&pNew[1];
100541 assert( pToken->z!=0 || pToken->n==0 );
100542 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
100543 pNew->u.zToken[pToken->n] = 0;
100544 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
100545 sqlite3DequoteExpr(pNew);
100546 }
100547 }
100548 }
100549#if SQLITE_MAX_EXPR_DEPTH>0
100550 pNew->nHeight = 1;
100551#endif
100552 }
100553 return pNew;
100554}
100555
100556/*
100557** Allocate a new expression node from a zero-terminated token that has
100558** already been dequoted.
100559*/
100560SQLITE_PRIVATE Expr *sqlite3Expr(
100561 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
100562 int op, /* Expression opcode */
100563 const char *zToken /* Token argument. Might be NULL */
100564){
100565 Token x;
100566 x.z = zToken;
100567 x.n = sqlite3Strlen30(zToken);
100568 return sqlite3ExprAlloc(db, op, &x, 0);
100569}
100570
100571/*
100572** Attach subtrees pLeft and pRight to the Expr node pRoot.
100573**
100574** If pRoot==NULL that means that a memory allocation error has occurred.
100575** In that case, delete the subtrees pLeft and pRight.
100576*/
100577SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
100578 sqlite3 *db,
100579 Expr *pRoot,
100580 Expr *pLeft,
100581 Expr *pRight
100582){
100583 if( pRoot==0 ){
100584 assert( db->mallocFailed );
100585 sqlite3ExprDelete(db, pLeft);
100586 sqlite3ExprDelete(db, pRight);
100587 }else{
100588 if( pRight ){
100589 pRoot->pRight = pRight;
100590 pRoot->flags |= EP_Propagate & pRight->flags;
100591 }
100592 if( pLeft ){
100593 pRoot->pLeft = pLeft;
100594 pRoot->flags |= EP_Propagate & pLeft->flags;
100595 }
100596 exprSetHeight(pRoot);
100597 }
100598}
100599
100600/*
100601** Allocate an Expr node which joins as many as two subtrees.
100602**
100603** One or both of the subtrees can be NULL. Return a pointer to the new
100604** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
100605** free the subtrees and return NULL.
100606*/
100607SQLITE_PRIVATE Expr *sqlite3PExpr(
100608 Parse *pParse, /* Parsing context */
100609 int op, /* Expression opcode */
100610 Expr *pLeft, /* Left operand */
100611 Expr *pRight /* Right operand */
100612){
100613 Expr *p;
100614 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
100615 if( p ){
100616 memset(p, 0, sizeof(Expr));
100617 p->op = op & 0xff;
100618 p->iAgg = -1;
100619 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
100620 sqlite3ExprCheckHeight(pParse, p->nHeight);
100621 }else{
100622 sqlite3ExprDelete(pParse->db, pLeft);
100623 sqlite3ExprDelete(pParse->db, pRight);
100624 }
100625 return p;
100626}
100627
100628/*
100629** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
100630** do a memory allocation failure) then delete the pSelect object.
100631*/
100632SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
100633 if( pExpr ){
100634 pExpr->x.pSelect = pSelect;
100635 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
100636 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
100637 }else{
100638 assert( pParse->db->mallocFailed );
100639 sqlite3SelectDelete(pParse->db, pSelect);
100640 }
100641}
100642
100643
100644/*
100645** Join two expressions using an AND operator. If either expression is
100646** NULL, then just return the other expression.
100647**
100648** If one side or the other of the AND is known to be false, then instead
100649** of returning an AND expression, just return a constant expression with
100650** a value of false.
100651*/
100652SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
100653 sqlite3 *db = pParse->db;
100654 if( pLeft==0 ){
100655 return pRight;
100656 }else if( pRight==0 ){
100657 return pLeft;
100658 }else if( (ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight))
100659 && !IN_RENAME_OBJECT
100660 ){
100661 sqlite3ExprDelete(db, pLeft);
100662 sqlite3ExprDelete(db, pRight);
100663 return sqlite3Expr(db, TK_INTEGER, "0");
100664 }else{
100665 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
100666 }
100667}
100668
100669/*
100670** Construct a new expression node for a function with multiple
100671** arguments.
100672*/
100673SQLITE_PRIVATE Expr *sqlite3ExprFunction(
100674 Parse *pParse, /* Parsing context */
100675 ExprList *pList, /* Argument list */
100676 Token *pToken, /* Name of the function */
100677 int eDistinct /* SF_Distinct or SF_ALL or 0 */
100678){
100679 Expr *pNew;
100680 sqlite3 *db = pParse->db;
100681 assert( pToken );
100682 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
100683 if( pNew==0 ){
100684 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
100685 return 0;
100686 }
100687 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
100688 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
100689 }
100690 pNew->x.pList = pList;
100691 ExprSetProperty(pNew, EP_HasFunc);
100692 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
100693 sqlite3ExprSetHeightAndFlags(pParse, pNew);
100694 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
100695 return pNew;
100696}
100697
100698/*
100699** Check to see if a function is usable according to current access
100700** rules:
100701**
100702** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
100703**
100704** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
100705** top-level SQL
100706**
100707** If the function is not usable, create an error.
100708*/
100709SQLITE_PRIVATE void sqlite3ExprFunctionUsable(
100710 Parse *pParse, /* Parsing and code generating context */
100711 Expr *pExpr, /* The function invocation */
100712 FuncDef *pDef /* The function being invoked */
100713){
100714 assert( !IN_RENAME_OBJECT );
100715 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
100716 if( ExprHasProperty(pExpr, EP_FromDDL) ){
100717 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
100718 || (pParse->db->flags & SQLITE_TrustedSchema)==0
100719 ){
100720 /* Functions prohibited in triggers and views if:
100721 ** (1) tagged with SQLITE_DIRECTONLY
100722 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
100723 ** is tagged with SQLITE_FUNC_UNSAFE) and
100724 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
100725 ** that the schema is possibly tainted).
100726 */
100727 sqlite3ErrorMsg(pParse, "unsafe use of %s()", pDef->zName);
100728 }
100729 }
100730}
100731
100732/*
100733** Assign a variable number to an expression that encodes a wildcard
100734** in the original SQL statement.
100735**
100736** Wildcards consisting of a single "?" are assigned the next sequential
100737** variable number.
100738**
100739** Wildcards of the form "?nnn" are assigned the number "nnn". We make
100740** sure "nnn" is not too big to avoid a denial of service attack when
100741** the SQL statement comes from an external source.
100742**
100743** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
100744** as the previous instance of the same wildcard. Or if this is the first
100745** instance of the wildcard, the next sequential variable number is
100746** assigned.
100747*/
100748SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
100749 sqlite3 *db = pParse->db;
100750 const char *z;
100751 ynVar x;
100752
100753 if( pExpr==0 ) return;
100754 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
100755 z = pExpr->u.zToken;
100756 assert( z!=0 );
100757 assert( z[0]!=0 );
100758 assert( n==(u32)sqlite3Strlen30(z) );
100759 if( z[1]==0 ){
100760 /* Wildcard of the form "?". Assign the next variable number */
100761 assert( z[0]=='?' );
100762 x = (ynVar)(++pParse->nVar);
100763 }else{
100764 int doAdd = 0;
100765 if( z[0]=='?' ){
100766 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
100767 ** use it as the variable number */
100768 i64 i;
100769 int bOk;
100770 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
100771 i = z[1]-'0'; /* The common case of ?N for a single digit N */
100772 bOk = 1;
100773 }else{
100774 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
100775 }
100776 testcase( i==0 );
100777 testcase( i==1 );
100778 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
100779 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
100780 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
100781 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
100782 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
100783 return;
100784 }
100785 x = (ynVar)i;
100786 if( x>pParse->nVar ){
100787 pParse->nVar = (int)x;
100788 doAdd = 1;
100789 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
100790 doAdd = 1;
100791 }
100792 }else{
100793 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
100794 ** number as the prior appearance of the same name, or if the name
100795 ** has never appeared before, reuse the same variable number
100796 */
100797 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
100798 if( x==0 ){
100799 x = (ynVar)(++pParse->nVar);
100800 doAdd = 1;
100801 }
100802 }
100803 if( doAdd ){
100804 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
100805 }
100806 }
100807 pExpr->iColumn = x;
100808 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
100809 sqlite3ErrorMsg(pParse, "too many SQL variables");
100810 }
100811}
100812
100813/*
100814** Recursively delete an expression tree.
100815*/
100816static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
100817 assert( p!=0 );
100818 /* Sanity check: Assert that the IntValue is non-negative if it exists */
100819 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
100820
100821 assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
100822 assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
100823 || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
100824#ifdef SQLITE_DEBUG
100825 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
100826 assert( p->pLeft==0 );
100827 assert( p->pRight==0 );
100828 assert( p->x.pSelect==0 );
100829 }
100830#endif
100831 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
100832 /* The Expr.x union is never used at the same time as Expr.pRight */
100833 assert( p->x.pList==0 || p->pRight==0 );
100834 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
100835 if( p->pRight ){
100836 assert( !ExprHasProperty(p, EP_WinFunc) );
100837 sqlite3ExprDeleteNN(db, p->pRight);
100838 }else if( ExprHasProperty(p, EP_xIsSelect) ){
100839 assert( !ExprHasProperty(p, EP_WinFunc) );
100840 sqlite3SelectDelete(db, p->x.pSelect);
100841 }else{
100842 sqlite3ExprListDelete(db, p->x.pList);
100843#ifndef SQLITE_OMIT_WINDOWFUNC
100844 if( ExprHasProperty(p, EP_WinFunc) ){
100845 sqlite3WindowDelete(db, p->y.pWin);
100846 }
100847#endif
100848 }
100849 }
100850 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
100851 if( !ExprHasProperty(p, EP_Static) ){
100852 sqlite3DbFreeNN(db, p);
100853 }
100854}
100855SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
100856 if( p ) sqlite3ExprDeleteNN(db, p);
100857}
100858
100859/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
100860** expression.
100861*/
100862SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
100863 if( p ){
100864 if( IN_RENAME_OBJECT ){
100865 sqlite3RenameExprUnmap(pParse, p);
100866 }
100867 sqlite3ExprDeleteNN(pParse->db, p);
100868 }
100869}
100870
100871/*
100872** Return the number of bytes allocated for the expression structure
100873** passed as the first argument. This is always one of EXPR_FULLSIZE,
100874** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
100875*/
100876static int exprStructSize(Expr *p){
100877 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
100878 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
100879 return EXPR_FULLSIZE;
100880}
100881
100882/*
100883** The dupedExpr*Size() routines each return the number of bytes required
100884** to store a copy of an expression or expression tree. They differ in
100885** how much of the tree is measured.
100886**
100887** dupedExprStructSize() Size of only the Expr structure
100888** dupedExprNodeSize() Size of Expr + space for token
100889** dupedExprSize() Expr + token + subtree components
100890**
100891***************************************************************************
100892**
100893** The dupedExprStructSize() function returns two values OR-ed together:
100894** (1) the space required for a copy of the Expr structure only and
100895** (2) the EP_xxx flags that indicate what the structure size should be.
100896** The return values is always one of:
100897**
100898** EXPR_FULLSIZE
100899** EXPR_REDUCEDSIZE | EP_Reduced
100900** EXPR_TOKENONLYSIZE | EP_TokenOnly
100901**
100902** The size of the structure can be found by masking the return value
100903** of this routine with 0xfff. The flags can be found by masking the
100904** return value with EP_Reduced|EP_TokenOnly.
100905**
100906** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
100907** (unreduced) Expr objects as they or originally constructed by the parser.
100908** During expression analysis, extra information is computed and moved into
100909** later parts of the Expr object and that extra information might get chopped
100910** off if the expression is reduced. Note also that it does not work to
100911** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
100912** to reduce a pristine expression tree from the parser. The implementation
100913** of dupedExprStructSize() contain multiple assert() statements that attempt
100914** to enforce this constraint.
100915*/
100916static int dupedExprStructSize(Expr *p, int flags){
100917 int nSize;
100918 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
100919 assert( EXPR_FULLSIZE<=0xfff );
100920 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
100921 if( 0==flags || p->op==TK_SELECT_COLUMN
100922#ifndef SQLITE_OMIT_WINDOWFUNC
100923 || ExprHasProperty(p, EP_WinFunc)
100924#endif
100925 ){
100926 nSize = EXPR_FULLSIZE;
100927 }else{
100928 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
100929 assert( !ExprHasProperty(p, EP_FromJoin) );
100930 assert( !ExprHasProperty(p, EP_MemToken) );
100931 assert( !ExprHasVVAProperty(p, EP_NoReduce) );
100932 if( p->pLeft || p->x.pList ){
100933 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
100934 }else{
100935 assert( p->pRight==0 );
100936 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
100937 }
100938 }
100939 return nSize;
100940}
100941
100942/*
100943** This function returns the space in bytes required to store the copy
100944** of the Expr structure and a copy of the Expr.u.zToken string (if that
100945** string is defined.)
100946*/
100947static int dupedExprNodeSize(Expr *p, int flags){
100948 int nByte = dupedExprStructSize(p, flags) & 0xfff;
100949 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
100950 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
100951 }
100952 return ROUND8(nByte);
100953}
100954
100955/*
100956** Return the number of bytes required to create a duplicate of the
100957** expression passed as the first argument. The second argument is a
100958** mask containing EXPRDUP_XXX flags.
100959**
100960** The value returned includes space to create a copy of the Expr struct
100961** itself and the buffer referred to by Expr.u.zToken, if any.
100962**
100963** If the EXPRDUP_REDUCE flag is set, then the return value includes
100964** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
100965** and Expr.pRight variables (but not for any structures pointed to or
100966** descended from the Expr.x.pList or Expr.x.pSelect variables).
100967*/
100968static int dupedExprSize(Expr *p, int flags){
100969 int nByte = 0;
100970 if( p ){
100971 nByte = dupedExprNodeSize(p, flags);
100972 if( flags&EXPRDUP_REDUCE ){
100973 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
100974 }
100975 }
100976 return nByte;
100977}
100978
100979/*
100980** This function is similar to sqlite3ExprDup(), except that if pzBuffer
100981** is not NULL then *pzBuffer is assumed to point to a buffer large enough
100982** to store the copy of expression p, the copies of p->u.zToken
100983** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
100984** if any. Before returning, *pzBuffer is set to the first byte past the
100985** portion of the buffer copied into by this function.
100986*/
100987static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
100988 Expr *pNew; /* Value to return */
100989 u8 *zAlloc; /* Memory space from which to build Expr object */
100990 u32 staticFlag; /* EP_Static if space not obtained from malloc */
100991
100992 assert( db!=0 );
100993 assert( p );
100994 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
100995 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
100996
100997 /* Figure out where to write the new Expr structure. */
100998 if( pzBuffer ){
100999 zAlloc = *pzBuffer;
101000 staticFlag = EP_Static;
101001 }else{
101002 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
101003 staticFlag = 0;
101004 }
101005 pNew = (Expr *)zAlloc;
101006
101007 if( pNew ){
101008 /* Set nNewSize to the size allocated for the structure pointed to
101009 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
101010 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
101011 ** by the copy of the p->u.zToken string (if any).
101012 */
101013 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
101014 const int nNewSize = nStructSize & 0xfff;
101015 int nToken;
101016 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
101017 nToken = sqlite3Strlen30(p->u.zToken) + 1;
101018 }else{
101019 nToken = 0;
101020 }
101021 if( dupFlags ){
101022 assert( ExprHasProperty(p, EP_Reduced)==0 );
101023 memcpy(zAlloc, p, nNewSize);
101024 }else{
101025 u32 nSize = (u32)exprStructSize(p);
101026 memcpy(zAlloc, p, nSize);
101027 if( nSize<EXPR_FULLSIZE ){
101028 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
101029 }
101030 }
101031
101032 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
101033 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
101034 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
101035 pNew->flags |= staticFlag;
101036 ExprClearVVAProperties(pNew);
101037 if( dupFlags ){
101038 ExprSetVVAProperty(pNew, EP_Immutable);
101039 }
101040
101041 /* Copy the p->u.zToken string, if any. */
101042 if( nToken ){
101043 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
101044 memcpy(zToken, p->u.zToken, nToken);
101045 }
101046
101047 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
101048 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
101049 if( ExprHasProperty(p, EP_xIsSelect) ){
101050 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
101051 }else{
101052 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
101053 }
101054 }
101055
101056 /* Fill in pNew->pLeft and pNew->pRight. */
101057 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
101058 zAlloc += dupedExprNodeSize(p, dupFlags);
101059 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
101060 pNew->pLeft = p->pLeft ?
101061 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
101062 pNew->pRight = p->pRight ?
101063 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
101064 }
101065#ifndef SQLITE_OMIT_WINDOWFUNC
101066 if( ExprHasProperty(p, EP_WinFunc) ){
101067 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
101068 assert( ExprHasProperty(pNew, EP_WinFunc) );
101069 }
101070#endif /* SQLITE_OMIT_WINDOWFUNC */
101071 if( pzBuffer ){
101072 *pzBuffer = zAlloc;
101073 }
101074 }else{
101075 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
101076 if( pNew->op==TK_SELECT_COLUMN ){
101077 pNew->pLeft = p->pLeft;
101078 assert( p->iColumn==0 || p->pRight==0 );
101079 assert( p->pRight==0 || p->pRight==p->pLeft );
101080 }else{
101081 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
101082 }
101083 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
101084 }
101085 }
101086 }
101087 return pNew;
101088}
101089
101090/*
101091** Create and return a deep copy of the object passed as the second
101092** argument. If an OOM condition is encountered, NULL is returned
101093** and the db->mallocFailed flag set.
101094*/
101095#ifndef SQLITE_OMIT_CTE
101096static With *withDup(sqlite3 *db, With *p){
101097 With *pRet = 0;
101098 if( p ){
101099 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
101100 pRet = sqlite3DbMallocZero(db, nByte);
101101 if( pRet ){
101102 int i;
101103 pRet->nCte = p->nCte;
101104 for(i=0; i<p->nCte; i++){
101105 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
101106 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
101107 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
101108 }
101109 }
101110 }
101111 return pRet;
101112}
101113#else
101114# define withDup(x,y) 0
101115#endif
101116
101117#ifndef SQLITE_OMIT_WINDOWFUNC
101118/*
101119** The gatherSelectWindows() procedure and its helper routine
101120** gatherSelectWindowsCallback() are used to scan all the expressions
101121** an a newly duplicated SELECT statement and gather all of the Window
101122** objects found there, assembling them onto the linked list at Select->pWin.
101123*/
101124static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
101125 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
101126 Select *pSelect = pWalker->u.pSelect;
101127 Window *pWin = pExpr->y.pWin;
101128 assert( pWin );
101129 assert( IsWindowFunc(pExpr) );
101130 assert( pWin->ppThis==0 );
101131 sqlite3WindowLink(pSelect, pWin);
101132 }
101133 return WRC_Continue;
101134}
101135static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
101136 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
101137}
101138static void gatherSelectWindows(Select *p){
101139 Walker w;
101140 w.xExprCallback = gatherSelectWindowsCallback;
101141 w.xSelectCallback = gatherSelectWindowsSelectCallback;
101142 w.xSelectCallback2 = 0;
101143 w.pParse = 0;
101144 w.u.pSelect = p;
101145 sqlite3WalkSelect(&w, p);
101146}
101147#endif
101148
101149
101150/*
101151** The following group of routines make deep copies of expressions,
101152** expression lists, ID lists, and select statements. The copies can
101153** be deleted (by being passed to their respective ...Delete() routines)
101154** without effecting the originals.
101155**
101156** The expression list, ID, and source lists return by sqlite3ExprListDup(),
101157** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
101158** by subsequent calls to sqlite*ListAppend() routines.
101159**
101160** Any tables that the SrcList might point to are not duplicated.
101161**
101162** The flags parameter contains a combination of the EXPRDUP_XXX flags.
101163** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
101164** truncated version of the usual Expr structure that will be stored as
101165** part of the in-memory representation of the database schema.
101166*/
101167SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
101168 assert( flags==0 || flags==EXPRDUP_REDUCE );
101169 return p ? exprDup(db, p, flags, 0) : 0;
101170}
101171SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
101172 ExprList *pNew;
101173 struct ExprList_item *pItem, *pOldItem;
101174 int i;
101175 Expr *pPriorSelectCol = 0;
101176 assert( db!=0 );
101177 if( p==0 ) return 0;
101178 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
101179 if( pNew==0 ) return 0;
101180 pNew->nExpr = p->nExpr;
101181 pItem = pNew->a;
101182 pOldItem = p->a;
101183 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
101184 Expr *pOldExpr = pOldItem->pExpr;
101185 Expr *pNewExpr;
101186 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
101187 if( pOldExpr
101188 && pOldExpr->op==TK_SELECT_COLUMN
101189 && (pNewExpr = pItem->pExpr)!=0
101190 ){
101191 assert( pNewExpr->iColumn==0 || i>0 );
101192 if( pNewExpr->iColumn==0 ){
101193 assert( pOldExpr->pLeft==pOldExpr->pRight );
101194 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
101195 }else{
101196 assert( i>0 );
101197 assert( pItem[-1].pExpr!=0 );
101198 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
101199 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
101200 pNewExpr->pLeft = pPriorSelectCol;
101201 }
101202 }
101203 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
101204 pItem->sortFlags = pOldItem->sortFlags;
101205 pItem->eEName = pOldItem->eEName;
101206 pItem->done = 0;
101207 pItem->bNulls = pOldItem->bNulls;
101208 pItem->bSorterRef = pOldItem->bSorterRef;
101209 pItem->u = pOldItem->u;
101210 }
101211 return pNew;
101212}
101213
101214/*
101215** If cursors, triggers, views and subqueries are all omitted from
101216** the build, then none of the following routines, except for
101217** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
101218** called with a NULL argument.
101219*/
101220#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
101221 || !defined(SQLITE_OMIT_SUBQUERY)
101222SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
101223 SrcList *pNew;
101224 int i;
101225 int nByte;
101226 assert( db!=0 );
101227 if( p==0 ) return 0;
101228 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
101229 pNew = sqlite3DbMallocRawNN(db, nByte );
101230 if( pNew==0 ) return 0;
101231 pNew->nSrc = pNew->nAlloc = p->nSrc;
101232 for(i=0; i<p->nSrc; i++){
101233 struct SrcList_item *pNewItem = &pNew->a[i];
101234 struct SrcList_item *pOldItem = &p->a[i];
101235 Table *pTab;
101236 pNewItem->pSchema = pOldItem->pSchema;
101237 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
101238 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
101239 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
101240 pNewItem->fg = pOldItem->fg;
101241 pNewItem->iCursor = pOldItem->iCursor;
101242 pNewItem->addrFillSub = pOldItem->addrFillSub;
101243 pNewItem->regReturn = pOldItem->regReturn;
101244 if( pNewItem->fg.isIndexedBy ){
101245 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
101246 }
101247 pNewItem->pIBIndex = pOldItem->pIBIndex;
101248 if( pNewItem->fg.isTabFunc ){
101249 pNewItem->u1.pFuncArg =
101250 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
101251 }
101252 pTab = pNewItem->pTab = pOldItem->pTab;
101253 if( pTab ){
101254 pTab->nTabRef++;
101255 }
101256 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
101257 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
101258 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
101259 pNewItem->colUsed = pOldItem->colUsed;
101260 }
101261 return pNew;
101262}
101263SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
101264 IdList *pNew;
101265 int i;
101266 assert( db!=0 );
101267 if( p==0 ) return 0;
101268 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
101269 if( pNew==0 ) return 0;
101270 pNew->nId = p->nId;
101271 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
101272 if( pNew->a==0 ){
101273 sqlite3DbFreeNN(db, pNew);
101274 return 0;
101275 }
101276 /* Note that because the size of the allocation for p->a[] is not
101277 ** necessarily a power of two, sqlite3IdListAppend() may not be called
101278 ** on the duplicate created by this function. */
101279 for(i=0; i<p->nId; i++){
101280 struct IdList_item *pNewItem = &pNew->a[i];
101281 struct IdList_item *pOldItem = &p->a[i];
101282 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
101283 pNewItem->idx = pOldItem->idx;
101284 }
101285 return pNew;
101286}
101287SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
101288 Select *pRet = 0;
101289 Select *pNext = 0;
101290 Select **pp = &pRet;
101291 Select *p;
101292
101293 assert( db!=0 );
101294 for(p=pDup; p; p=p->pPrior){
101295 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
101296 if( pNew==0 ) break;
101297 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
101298 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
101299 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
101300 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
101301 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
101302 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
101303 pNew->op = p->op;
101304 pNew->pNext = pNext;
101305 pNew->pPrior = 0;
101306 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
101307 pNew->iLimit = 0;
101308 pNew->iOffset = 0;
101309 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
101310 pNew->addrOpenEphm[0] = -1;
101311 pNew->addrOpenEphm[1] = -1;
101312 pNew->nSelectRow = p->nSelectRow;
101313 pNew->pWith = withDup(db, p->pWith);
101314#ifndef SQLITE_OMIT_WINDOWFUNC
101315 pNew->pWin = 0;
101316 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
101317 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
101318#endif
101319 pNew->selId = p->selId;
101320 *pp = pNew;
101321 pp = &pNew->pPrior;
101322 pNext = pNew;
101323 }
101324
101325 return pRet;
101326}
101327#else
101328SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
101329 assert( p==0 );
101330 return 0;
101331}
101332#endif
101333
101334
101335/*
101336** Add a new element to the end of an expression list. If pList is
101337** initially NULL, then create a new expression list.
101338**
101339** The pList argument must be either NULL or a pointer to an ExprList
101340** obtained from a prior call to sqlite3ExprListAppend(). This routine
101341** may not be used with an ExprList obtained from sqlite3ExprListDup().
101342** Reason: This routine assumes that the number of slots in pList->a[]
101343** is a power of two. That is true for sqlite3ExprListAppend() returns
101344** but is not necessarily true from the return value of sqlite3ExprListDup().
101345**
101346** If a memory allocation error occurs, the entire list is freed and
101347** NULL is returned. If non-NULL is returned, then it is guaranteed
101348** that the new entry was successfully appended.
101349*/
101350SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
101351 Parse *pParse, /* Parsing context */
101352 ExprList *pList, /* List to which to append. Might be NULL */
101353 Expr *pExpr /* Expression to be appended. Might be NULL */
101354){
101355 struct ExprList_item *pItem;
101356 sqlite3 *db = pParse->db;
101357 assert( db!=0 );
101358 if( pList==0 ){
101359 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
101360 if( pList==0 ){
101361 goto no_mem;
101362 }
101363 pList->nExpr = 0;
101364 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
101365 ExprList *pNew;
101366 pNew = sqlite3DbRealloc(db, pList,
101367 sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
101368 if( pNew==0 ){
101369 goto no_mem;
101370 }
101371 pList = pNew;
101372 }
101373 pItem = &pList->a[pList->nExpr++];
101374 assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
101375 assert( offsetof(struct ExprList_item,pExpr)==0 );
101376 memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
101377 pItem->pExpr = pExpr;
101378 return pList;
101379
101380no_mem:
101381 /* Avoid leaking memory if malloc has failed. */
101382 sqlite3ExprDelete(db, pExpr);
101383 sqlite3ExprListDelete(db, pList);
101384 return 0;
101385}
101386
101387/*
101388** pColumns and pExpr form a vector assignment which is part of the SET
101389** clause of an UPDATE statement. Like this:
101390**
101391** (a,b,c) = (expr1,expr2,expr3)
101392** Or: (a,b,c) = (SELECT x,y,z FROM ....)
101393**
101394** For each term of the vector assignment, append new entries to the
101395** expression list pList. In the case of a subquery on the RHS, append
101396** TK_SELECT_COLUMN expressions.
101397*/
101398SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
101399 Parse *pParse, /* Parsing context */
101400 ExprList *pList, /* List to which to append. Might be NULL */
101401 IdList *pColumns, /* List of names of LHS of the assignment */
101402 Expr *pExpr /* Vector expression to be appended. Might be NULL */
101403){
101404 sqlite3 *db = pParse->db;
101405 int n;
101406 int i;
101407 int iFirst = pList ? pList->nExpr : 0;
101408 /* pColumns can only be NULL due to an OOM but an OOM will cause an
101409 ** exit prior to this routine being invoked */
101410 if( NEVER(pColumns==0) ) goto vector_append_error;
101411 if( pExpr==0 ) goto vector_append_error;
101412
101413 /* If the RHS is a vector, then we can immediately check to see that
101414 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
101415 ** wildcards ("*") in the result set of the SELECT must be expanded before
101416 ** we can do the size check, so defer the size check until code generation.
101417 */
101418 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
101419 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
101420 pColumns->nId, n);
101421 goto vector_append_error;
101422 }
101423
101424 for(i=0; i<pColumns->nId; i++){
101425 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
101426 assert( pSubExpr!=0 || db->mallocFailed );
101427 assert( pSubExpr==0 || pSubExpr->iTable==0 );
101428 if( pSubExpr==0 ) continue;
101429 pSubExpr->iTable = pColumns->nId;
101430 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
101431 if( pList ){
101432 assert( pList->nExpr==iFirst+i+1 );
101433 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
101434 pColumns->a[i].zName = 0;
101435 }
101436 }
101437
101438 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
101439 Expr *pFirst = pList->a[iFirst].pExpr;
101440 assert( pFirst!=0 );
101441 assert( pFirst->op==TK_SELECT_COLUMN );
101442
101443 /* Store the SELECT statement in pRight so it will be deleted when
101444 ** sqlite3ExprListDelete() is called */
101445 pFirst->pRight = pExpr;
101446 pExpr = 0;
101447
101448 /* Remember the size of the LHS in iTable so that we can check that
101449 ** the RHS and LHS sizes match during code generation. */
101450 pFirst->iTable = pColumns->nId;
101451 }
101452
101453vector_append_error:
101454 sqlite3ExprUnmapAndDelete(pParse, pExpr);
101455 sqlite3IdListDelete(db, pColumns);
101456 return pList;
101457}
101458
101459/*
101460** Set the sort order for the last element on the given ExprList.
101461*/
101462SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
101463 struct ExprList_item *pItem;
101464 if( p==0 ) return;
101465 assert( p->nExpr>0 );
101466
101467 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
101468 assert( iSortOrder==SQLITE_SO_UNDEFINED
101469 || iSortOrder==SQLITE_SO_ASC
101470 || iSortOrder==SQLITE_SO_DESC
101471 );
101472 assert( eNulls==SQLITE_SO_UNDEFINED
101473 || eNulls==SQLITE_SO_ASC
101474 || eNulls==SQLITE_SO_DESC
101475 );
101476
101477 pItem = &p->a[p->nExpr-1];
101478 assert( pItem->bNulls==0 );
101479 if( iSortOrder==SQLITE_SO_UNDEFINED ){
101480 iSortOrder = SQLITE_SO_ASC;
101481 }
101482 pItem->sortFlags = (u8)iSortOrder;
101483
101484 if( eNulls!=SQLITE_SO_UNDEFINED ){
101485 pItem->bNulls = 1;
101486 if( iSortOrder!=eNulls ){
101487 pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
101488 }
101489 }
101490}
101491
101492/*
101493** Set the ExprList.a[].zEName element of the most recently added item
101494** on the expression list.
101495**
101496** pList might be NULL following an OOM error. But pName should never be
101497** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
101498** is set.
101499*/
101500SQLITE_PRIVATE void sqlite3ExprListSetName(
101501 Parse *pParse, /* Parsing context */
101502 ExprList *pList, /* List to which to add the span. */
101503 Token *pName, /* Name to be added */
101504 int dequote /* True to cause the name to be dequoted */
101505){
101506 assert( pList!=0 || pParse->db->mallocFailed!=0 );
101507 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
101508 if( pList ){
101509 struct ExprList_item *pItem;
101510 assert( pList->nExpr>0 );
101511 pItem = &pList->a[pList->nExpr-1];
101512 assert( pItem->zEName==0 );
101513 assert( pItem->eEName==ENAME_NAME );
101514 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
101515 if( dequote ){
101516 /* If dequote==0, then pName->z does not point to part of a DDL
101517 ** statement handled by the parser. And so no token need be added
101518 ** to the token-map. */
101519 sqlite3Dequote(pItem->zEName);
101520 if( IN_RENAME_OBJECT ){
101521 sqlite3RenameTokenMap(pParse, (void*)pItem->zEName, pName);
101522 }
101523 }
101524 }
101525}
101526
101527/*
101528** Set the ExprList.a[].zSpan element of the most recently added item
101529** on the expression list.
101530**
101531** pList might be NULL following an OOM error. But pSpan should never be
101532** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
101533** is set.
101534*/
101535SQLITE_PRIVATE void sqlite3ExprListSetSpan(
101536 Parse *pParse, /* Parsing context */
101537 ExprList *pList, /* List to which to add the span. */
101538 const char *zStart, /* Start of the span */
101539 const char *zEnd /* End of the span */
101540){
101541 sqlite3 *db = pParse->db;
101542 assert( pList!=0 || db->mallocFailed!=0 );
101543 if( pList ){
101544 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
101545 assert( pList->nExpr>0 );
101546 if( pItem->zEName==0 ){
101547 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
101548 pItem->eEName = ENAME_SPAN;
101549 }
101550 }
101551}
101552
101553/*
101554** If the expression list pEList contains more than iLimit elements,
101555** leave an error message in pParse.
101556*/
101557SQLITE_PRIVATE void sqlite3ExprListCheckLength(
101558 Parse *pParse,
101559 ExprList *pEList,
101560 const char *zObject
101561){
101562 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
101563 testcase( pEList && pEList->nExpr==mx );
101564 testcase( pEList && pEList->nExpr==mx+1 );
101565 if( pEList && pEList->nExpr>mx ){
101566 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
101567 }
101568}
101569
101570/*
101571** Delete an entire expression list.
101572*/
101573static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
101574 int i = pList->nExpr;
101575 struct ExprList_item *pItem = pList->a;
101576 assert( pList->nExpr>0 );
101577 do{
101578 sqlite3ExprDelete(db, pItem->pExpr);
101579 sqlite3DbFree(db, pItem->zEName);
101580 pItem++;
101581 }while( --i>0 );
101582 sqlite3DbFreeNN(db, pList);
101583}
101584SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
101585 if( pList ) exprListDeleteNN(db, pList);
101586}
101587
101588/*
101589** Return the bitwise-OR of all Expr.flags fields in the given
101590** ExprList.
101591*/
101592SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
101593 int i;
101594 u32 m = 0;
101595 assert( pList!=0 );
101596 for(i=0; i<pList->nExpr; i++){
101597 Expr *pExpr = pList->a[i].pExpr;
101598 assert( pExpr!=0 );
101599 m |= pExpr->flags;
101600 }
101601 return m;
101602}
101603
101604/*
101605** This is a SELECT-node callback for the expression walker that
101606** always "fails". By "fail" in this case, we mean set
101607** pWalker->eCode to zero and abort.
101608**
101609** This callback is used by multiple expression walkers.
101610*/
101611SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
101612 UNUSED_PARAMETER(NotUsed);
101613 pWalker->eCode = 0;
101614 return WRC_Abort;
101615}
101616
101617/*
101618** Check the input string to see if it is "true" or "false" (in any case).
101619**
101620** If the string is.... Return
101621** "true" EP_IsTrue
101622** "false" EP_IsFalse
101623** anything else 0
101624*/
101625SQLITE_PRIVATE u32 sqlite3IsTrueOrFalse(const char *zIn){
101626 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue;
101627 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse;
101628 return 0;
101629}
101630
101631
101632/*
101633** If the input expression is an ID with the name "true" or "false"
101634** then convert it into an TK_TRUEFALSE term. Return non-zero if
101635** the conversion happened, and zero if the expression is unaltered.
101636*/
101637SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
101638 u32 v;
101639 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
101640 if( !ExprHasProperty(pExpr, EP_Quoted)
101641 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
101642 ){
101643 pExpr->op = TK_TRUEFALSE;
101644 ExprSetProperty(pExpr, v);
101645 return 1;
101646 }
101647 return 0;
101648}
101649
101650/*
101651** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
101652** and 0 if it is FALSE.
101653*/
101654SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
101655 pExpr = sqlite3ExprSkipCollate((Expr*)pExpr);
101656 assert( pExpr->op==TK_TRUEFALSE );
101657 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
101658 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
101659 return pExpr->u.zToken[4]==0;
101660}
101661
101662/*
101663** If pExpr is an AND or OR expression, try to simplify it by eliminating
101664** terms that are always true or false. Return the simplified expression.
101665** Or return the original expression if no simplification is possible.
101666**
101667** Examples:
101668**
101669** (x<10) AND true => (x<10)
101670** (x<10) AND false => false
101671** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
101672** (x<10) AND (y=22 OR true) => (x<10)
101673** (y=22) OR true => true
101674*/
101675SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
101676 assert( pExpr!=0 );
101677 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
101678 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
101679 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
101680 if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
101681 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
101682 }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
101683 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
101684 }
101685 }
101686 return pExpr;
101687}
101688
101689
101690/*
101691** These routines are Walker callbacks used to check expressions to
101692** see if they are "constant" for some definition of constant. The
101693** Walker.eCode value determines the type of "constant" we are looking
101694** for.
101695**
101696** These callback routines are used to implement the following:
101697**
101698** sqlite3ExprIsConstant() pWalker->eCode==1
101699** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
101700** sqlite3ExprIsTableConstant() pWalker->eCode==3
101701** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
101702**
101703** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
101704** is found to not be a constant.
101705**
101706** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
101707** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
101708** when parsing an existing schema out of the sqlite_schema table and 4
101709** when processing a new CREATE TABLE statement. A bound parameter raises
101710** an error for new statements, but is silently converted
101711** to NULL for existing schemas. This allows sqlite_schema tables that
101712** contain a bound parameter because they were generated by older versions
101713** of SQLite to be parsed by newer versions of SQLite without raising a
101714** malformed schema error.
101715*/
101716static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
101717
101718 /* If pWalker->eCode is 2 then any term of the expression that comes from
101719 ** the ON or USING clauses of a left join disqualifies the expression
101720 ** from being considered constant. */
101721 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
101722 pWalker->eCode = 0;
101723 return WRC_Abort;
101724 }
101725
101726 switch( pExpr->op ){
101727 /* Consider functions to be constant if all their arguments are constant
101728 ** and either pWalker->eCode==4 or 5 or the function has the
101729 ** SQLITE_FUNC_CONST flag. */
101730 case TK_FUNCTION:
101731 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
101732 && !ExprHasProperty(pExpr, EP_WinFunc)
101733 ){
101734 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
101735 return WRC_Continue;
101736 }else{
101737 pWalker->eCode = 0;
101738 return WRC_Abort;
101739 }
101740 case TK_ID:
101741 /* Convert "true" or "false" in a DEFAULT clause into the
101742 ** appropriate TK_TRUEFALSE operator */
101743 if( sqlite3ExprIdToTrueFalse(pExpr) ){
101744 return WRC_Prune;
101745 }
101746 /* no break */ deliberate_fall_through
101747 case TK_COLUMN:
101748 case TK_AGG_FUNCTION:
101749 case TK_AGG_COLUMN:
101750 testcase( pExpr->op==TK_ID );
101751 testcase( pExpr->op==TK_COLUMN );
101752 testcase( pExpr->op==TK_AGG_FUNCTION );
101753 testcase( pExpr->op==TK_AGG_COLUMN );
101754 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
101755 return WRC_Continue;
101756 }
101757 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
101758 return WRC_Continue;
101759 }
101760 /* no break */ deliberate_fall_through
101761 case TK_IF_NULL_ROW:
101762 case TK_REGISTER:
101763 case TK_DOT:
101764 testcase( pExpr->op==TK_REGISTER );
101765 testcase( pExpr->op==TK_IF_NULL_ROW );
101766 testcase( pExpr->op==TK_DOT );
101767 pWalker->eCode = 0;
101768 return WRC_Abort;
101769 case TK_VARIABLE:
101770 if( pWalker->eCode==5 ){
101771 /* Silently convert bound parameters that appear inside of CREATE
101772 ** statements into a NULL when parsing the CREATE statement text out
101773 ** of the sqlite_schema table */
101774 pExpr->op = TK_NULL;
101775 }else if( pWalker->eCode==4 ){
101776 /* A bound parameter in a CREATE statement that originates from
101777 ** sqlite3_prepare() causes an error */
101778 pWalker->eCode = 0;
101779 return WRC_Abort;
101780 }
101781 /* no break */ deliberate_fall_through
101782 default:
101783 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
101784 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
101785 return WRC_Continue;
101786 }
101787}
101788static int exprIsConst(Expr *p, int initFlag, int iCur){
101789 Walker w;
101790 w.eCode = initFlag;
101791 w.xExprCallback = exprNodeIsConstant;
101792 w.xSelectCallback = sqlite3SelectWalkFail;
101793#ifdef SQLITE_DEBUG
101794 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
101795#endif
101796 w.u.iCur = iCur;
101797 sqlite3WalkExpr(&w, p);
101798 return w.eCode;
101799}
101800
101801/*
101802** Walk an expression tree. Return non-zero if the expression is constant
101803** and 0 if it involves variables or function calls.
101804**
101805** For the purposes of this function, a double-quoted string (ex: "abc")
101806** is considered a variable but a single-quoted string (ex: 'abc') is
101807** a constant.
101808*/
101809SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
101810 return exprIsConst(p, 1, 0);
101811}
101812
101813/*
101814** Walk an expression tree. Return non-zero if
101815**
101816** (1) the expression is constant, and
101817** (2) the expression does originate in the ON or USING clause
101818** of a LEFT JOIN, and
101819** (3) the expression does not contain any EP_FixedCol TK_COLUMN
101820** operands created by the constant propagation optimization.
101821**
101822** When this routine returns true, it indicates that the expression
101823** can be added to the pParse->pConstExpr list and evaluated once when
101824** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
101825*/
101826SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
101827 return exprIsConst(p, 2, 0);
101828}
101829
101830/*
101831** Walk an expression tree. Return non-zero if the expression is constant
101832** for any single row of the table with cursor iCur. In other words, the
101833** expression must not refer to any non-deterministic function nor any
101834** table other than iCur.
101835*/
101836SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
101837 return exprIsConst(p, 3, iCur);
101838}
101839
101840
101841/*
101842** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
101843*/
101844static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
101845 ExprList *pGroupBy = pWalker->u.pGroupBy;
101846 int i;
101847
101848 /* Check if pExpr is identical to any GROUP BY term. If so, consider
101849 ** it constant. */
101850 for(i=0; i<pGroupBy->nExpr; i++){
101851 Expr *p = pGroupBy->a[i].pExpr;
101852 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
101853 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
101854 if( sqlite3IsBinary(pColl) ){
101855 return WRC_Prune;
101856 }
101857 }
101858 }
101859
101860 /* Check if pExpr is a sub-select. If so, consider it variable. */
101861 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
101862 pWalker->eCode = 0;
101863 return WRC_Abort;
101864 }
101865
101866 return exprNodeIsConstant(pWalker, pExpr);
101867}
101868
101869/*
101870** Walk the expression tree passed as the first argument. Return non-zero
101871** if the expression consists entirely of constants or copies of terms
101872** in pGroupBy that sort with the BINARY collation sequence.
101873**
101874** This routine is used to determine if a term of the HAVING clause can
101875** be promoted into the WHERE clause. In order for such a promotion to work,
101876** the value of the HAVING clause term must be the same for all members of
101877** a "group". The requirement that the GROUP BY term must be BINARY
101878** assumes that no other collating sequence will have a finer-grained
101879** grouping than binary. In other words (A=B COLLATE binary) implies
101880** A=B in every other collating sequence. The requirement that the
101881** GROUP BY be BINARY is stricter than necessary. It would also work
101882** to promote HAVING clauses that use the same alternative collating
101883** sequence as the GROUP BY term, but that is much harder to check,
101884** alternative collating sequences are uncommon, and this is only an
101885** optimization, so we take the easy way out and simply require the
101886** GROUP BY to use the BINARY collating sequence.
101887*/
101888SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
101889 Walker w;
101890 w.eCode = 1;
101891 w.xExprCallback = exprNodeIsConstantOrGroupBy;
101892 w.xSelectCallback = 0;
101893 w.u.pGroupBy = pGroupBy;
101894 w.pParse = pParse;
101895 sqlite3WalkExpr(&w, p);
101896 return w.eCode;
101897}
101898
101899/*
101900** Walk an expression tree for the DEFAULT field of a column definition
101901** in a CREATE TABLE statement. Return non-zero if the expression is
101902** acceptable for use as a DEFAULT. That is to say, return non-zero if
101903** the expression is constant or a function call with constant arguments.
101904** Return and 0 if there are any variables.
101905**
101906** isInit is true when parsing from sqlite_schema. isInit is false when
101907** processing a new CREATE TABLE statement. When isInit is true, parameters
101908** (such as ? or $abc) in the expression are converted into NULL. When
101909** isInit is false, parameters raise an error. Parameters should not be
101910** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
101911** allowed it, so we need to support it when reading sqlite_schema for
101912** backwards compatibility.
101913**
101914** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
101915**
101916** For the purposes of this function, a double-quoted string (ex: "abc")
101917** is considered a variable but a single-quoted string (ex: 'abc') is
101918** a constant.
101919*/
101920SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
101921 assert( isInit==0 || isInit==1 );
101922 return exprIsConst(p, 4+isInit, 0);
101923}
101924
101925#ifdef SQLITE_ENABLE_CURSOR_HINTS
101926/*
101927** Walk an expression tree. Return 1 if the expression contains a
101928** subquery of some kind. Return 0 if there are no subqueries.
101929*/
101930SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
101931 Walker w;
101932 w.eCode = 1;
101933 w.xExprCallback = sqlite3ExprWalkNoop;
101934 w.xSelectCallback = sqlite3SelectWalkFail;
101935#ifdef SQLITE_DEBUG
101936 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
101937#endif
101938 sqlite3WalkExpr(&w, p);
101939 return w.eCode==0;
101940}
101941#endif
101942
101943/*
101944** If the expression p codes a constant integer that is small enough
101945** to fit in a 32-bit integer, return 1 and put the value of the integer
101946** in *pValue. If the expression is not an integer or if it is too big
101947** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
101948*/
101949SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
101950 int rc = 0;
101951 if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */
101952
101953 /* If an expression is an integer literal that fits in a signed 32-bit
101954 ** integer, then the EP_IntValue flag will have already been set */
101955 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
101956 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
101957
101958 if( p->flags & EP_IntValue ){
101959 *pValue = p->u.iValue;
101960 return 1;
101961 }
101962 switch( p->op ){
101963 case TK_UPLUS: {
101964 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
101965 break;
101966 }
101967 case TK_UMINUS: {
101968 int v;
101969 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
101970 assert( v!=(-2147483647-1) );
101971 *pValue = -v;
101972 rc = 1;
101973 }
101974 break;
101975 }
101976 default: break;
101977 }
101978 return rc;
101979}
101980
101981/*
101982** Return FALSE if there is no chance that the expression can be NULL.
101983**
101984** If the expression might be NULL or if the expression is too complex
101985** to tell return TRUE.
101986**
101987** This routine is used as an optimization, to skip OP_IsNull opcodes
101988** when we know that a value cannot be NULL. Hence, a false positive
101989** (returning TRUE when in fact the expression can never be NULL) might
101990** be a small performance hit but is otherwise harmless. On the other
101991** hand, a false negative (returning FALSE when the result could be NULL)
101992** will likely result in an incorrect answer. So when in doubt, return
101993** TRUE.
101994*/
101995SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
101996 u8 op;
101997 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
101998 p = p->pLeft;
101999 }
102000 op = p->op;
102001 if( op==TK_REGISTER ) op = p->op2;
102002 switch( op ){
102003 case TK_INTEGER:
102004 case TK_STRING:
102005 case TK_FLOAT:
102006 case TK_BLOB:
102007 return 0;
102008 case TK_COLUMN:
102009 return ExprHasProperty(p, EP_CanBeNull) ||
102010 p->y.pTab==0 || /* Reference to column of index on expression */
102011 (p->iColumn>=0
102012 && ALWAYS(p->y.pTab->aCol!=0) /* Defense against OOM problems */
102013 && p->y.pTab->aCol[p->iColumn].notNull==0);
102014 default:
102015 return 1;
102016 }
102017}
102018
102019/*
102020** Return TRUE if the given expression is a constant which would be
102021** unchanged by OP_Affinity with the affinity given in the second
102022** argument.
102023**
102024** This routine is used to determine if the OP_Affinity operation
102025** can be omitted. When in doubt return FALSE. A false negative
102026** is harmless. A false positive, however, can result in the wrong
102027** answer.
102028*/
102029SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
102030 u8 op;
102031 int unaryMinus = 0;
102032 if( aff==SQLITE_AFF_BLOB ) return 1;
102033 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
102034 if( p->op==TK_UMINUS ) unaryMinus = 1;
102035 p = p->pLeft;
102036 }
102037 op = p->op;
102038 if( op==TK_REGISTER ) op = p->op2;
102039 switch( op ){
102040 case TK_INTEGER: {
102041 return aff>=SQLITE_AFF_NUMERIC;
102042 }
102043 case TK_FLOAT: {
102044 return aff>=SQLITE_AFF_NUMERIC;
102045 }
102046 case TK_STRING: {
102047 return !unaryMinus && aff==SQLITE_AFF_TEXT;
102048 }
102049 case TK_BLOB: {
102050 return !unaryMinus;
102051 }
102052 case TK_COLUMN: {
102053 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
102054 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
102055 }
102056 default: {
102057 return 0;
102058 }
102059 }
102060}
102061
102062/*
102063** Return TRUE if the given string is a row-id column name.
102064*/
102065SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
102066 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
102067 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
102068 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
102069 return 0;
102070}
102071
102072/*
102073** pX is the RHS of an IN operator. If pX is a SELECT statement
102074** that can be simplified to a direct table access, then return
102075** a pointer to the SELECT statement. If pX is not a SELECT statement,
102076** or if the SELECT statement needs to be manifested into a transient
102077** table, then return NULL.
102078*/
102079#ifndef SQLITE_OMIT_SUBQUERY
102080static Select *isCandidateForInOpt(Expr *pX){
102081 Select *p;
102082 SrcList *pSrc;
102083 ExprList *pEList;
102084 Table *pTab;
102085 int i;
102086 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
102087 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
102088 p = pX->x.pSelect;
102089 if( p->pPrior ) return 0; /* Not a compound SELECT */
102090 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
102091 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
102092 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
102093 return 0; /* No DISTINCT keyword and no aggregate functions */
102094 }
102095 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
102096 if( p->pLimit ) return 0; /* Has no LIMIT clause */
102097 if( p->pWhere ) return 0; /* Has no WHERE clause */
102098 pSrc = p->pSrc;
102099 assert( pSrc!=0 );
102100 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
102101 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
102102 pTab = pSrc->a[0].pTab;
102103 assert( pTab!=0 );
102104 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
102105 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
102106 pEList = p->pEList;
102107 assert( pEList!=0 );
102108 /* All SELECT results must be columns. */
102109 for(i=0; i<pEList->nExpr; i++){
102110 Expr *pRes = pEList->a[i].pExpr;
102111 if( pRes->op!=TK_COLUMN ) return 0;
102112 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
102113 }
102114 return p;
102115}
102116#endif /* SQLITE_OMIT_SUBQUERY */
102117
102118#ifndef SQLITE_OMIT_SUBQUERY
102119/*
102120** Generate code that checks the left-most column of index table iCur to see if
102121** it contains any NULL entries. Cause the register at regHasNull to be set
102122** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
102123** to be set to NULL if iCur contains one or more NULL values.
102124*/
102125static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
102126 int addr1;
102127 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
102128 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
102129 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
102130 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
102131 VdbeComment((v, "first_entry_in(%d)", iCur));
102132 sqlite3VdbeJumpHere(v, addr1);
102133}
102134#endif
102135
102136
102137#ifndef SQLITE_OMIT_SUBQUERY
102138/*
102139** The argument is an IN operator with a list (not a subquery) on the
102140** right-hand side. Return TRUE if that list is constant.
102141*/
102142static int sqlite3InRhsIsConstant(Expr *pIn){
102143 Expr *pLHS;
102144 int res;
102145 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
102146 pLHS = pIn->pLeft;
102147 pIn->pLeft = 0;
102148 res = sqlite3ExprIsConstant(pIn);
102149 pIn->pLeft = pLHS;
102150 return res;
102151}
102152#endif
102153
102154/*
102155** This function is used by the implementation of the IN (...) operator.
102156** The pX parameter is the expression on the RHS of the IN operator, which
102157** might be either a list of expressions or a subquery.
102158**
102159** The job of this routine is to find or create a b-tree object that can
102160** be used either to test for membership in the RHS set or to iterate through
102161** all members of the RHS set, skipping duplicates.
102162**
102163** A cursor is opened on the b-tree object that is the RHS of the IN operator
102164** and pX->iTable is set to the index of that cursor.
102165**
102166** The returned value of this function indicates the b-tree type, as follows:
102167**
102168** IN_INDEX_ROWID - The cursor was opened on a database table.
102169** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
102170** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
102171** IN_INDEX_EPH - The cursor was opened on a specially created and
102172** populated epheremal table.
102173** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
102174** implemented as a sequence of comparisons.
102175**
102176** An existing b-tree might be used if the RHS expression pX is a simple
102177** subquery such as:
102178**
102179** SELECT <column1>, <column2>... FROM <table>
102180**
102181** If the RHS of the IN operator is a list or a more complex subquery, then
102182** an ephemeral table might need to be generated from the RHS and then
102183** pX->iTable made to point to the ephemeral table instead of an
102184** existing table.
102185**
102186** The inFlags parameter must contain, at a minimum, one of the bits
102187** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
102188** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
102189** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
102190** be used to loop over all values of the RHS of the IN operator.
102191**
102192** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
102193** through the set members) then the b-tree must not contain duplicates.
102194** An epheremal table will be created unless the selected columns are guaranteed
102195** to be unique - either because it is an INTEGER PRIMARY KEY or due to
102196** a UNIQUE constraint or index.
102197**
102198** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
102199** for fast set membership tests) then an epheremal table must
102200** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
102201** index can be found with the specified <columns> as its left-most.
102202**
102203** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
102204** if the RHS of the IN operator is a list (not a subquery) then this
102205** routine might decide that creating an ephemeral b-tree for membership
102206** testing is too expensive and return IN_INDEX_NOOP. In that case, the
102207** calling routine should implement the IN operator using a sequence
102208** of Eq or Ne comparison operations.
102209**
102210** When the b-tree is being used for membership tests, the calling function
102211** might need to know whether or not the RHS side of the IN operator
102212** contains a NULL. If prRhsHasNull is not a NULL pointer and
102213** if there is any chance that the (...) might contain a NULL value at
102214** runtime, then a register is allocated and the register number written
102215** to *prRhsHasNull. If there is no chance that the (...) contains a
102216** NULL value, then *prRhsHasNull is left unchanged.
102217**
102218** If a register is allocated and its location stored in *prRhsHasNull, then
102219** the value in that register will be NULL if the b-tree contains one or more
102220** NULL values, and it will be some non-NULL value if the b-tree contains no
102221** NULL values.
102222**
102223** If the aiMap parameter is not NULL, it must point to an array containing
102224** one element for each column returned by the SELECT statement on the RHS
102225** of the IN(...) operator. The i'th entry of the array is populated with the
102226** offset of the index column that matches the i'th column returned by the
102227** SELECT. For example, if the expression and selected index are:
102228**
102229** (?,?,?) IN (SELECT a, b, c FROM t1)
102230** CREATE INDEX i1 ON t1(b, c, a);
102231**
102232** then aiMap[] is populated with {2, 0, 1}.
102233*/
102234#ifndef SQLITE_OMIT_SUBQUERY
102235SQLITE_PRIVATE int sqlite3FindInIndex(
102236 Parse *pParse, /* Parsing context */
102237 Expr *pX, /* The IN expression */
102238 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
102239 int *prRhsHasNull, /* Register holding NULL status. See notes */
102240 int *aiMap, /* Mapping from Index fields to RHS fields */
102241 int *piTab /* OUT: index to use */
102242){
102243 Select *p; /* SELECT to the right of IN operator */
102244 int eType = 0; /* Type of RHS table. IN_INDEX_* */
102245 int iTab = pParse->nTab++; /* Cursor of the RHS table */
102246 int mustBeUnique; /* True if RHS must be unique */
102247 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
102248
102249 assert( pX->op==TK_IN );
102250 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
102251
102252 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
102253 ** whether or not the SELECT result contains NULL values, check whether
102254 ** or not NULL is actually possible (it may not be, for example, due
102255 ** to NOT NULL constraints in the schema). If no NULL values are possible,
102256 ** set prRhsHasNull to 0 before continuing. */
102257 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
102258 int i;
102259 ExprList *pEList = pX->x.pSelect->pEList;
102260 for(i=0; i<pEList->nExpr; i++){
102261 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
102262 }
102263 if( i==pEList->nExpr ){
102264 prRhsHasNull = 0;
102265 }
102266 }
102267
102268 /* Check to see if an existing table or index can be used to
102269 ** satisfy the query. This is preferable to generating a new
102270 ** ephemeral table. */
102271 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
102272 sqlite3 *db = pParse->db; /* Database connection */
102273 Table *pTab; /* Table <table>. */
102274 int iDb; /* Database idx for pTab */
102275 ExprList *pEList = p->pEList;
102276 int nExpr = pEList->nExpr;
102277
102278 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
102279 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
102280 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
102281 pTab = p->pSrc->a[0].pTab;
102282
102283 /* Code an OP_Transaction and OP_TableLock for <table>. */
102284 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102285 assert( iDb>=0 && iDb<SQLITE_MAX_ATTACHED );
102286 sqlite3CodeVerifySchema(pParse, iDb);
102287 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102288
102289 assert(v); /* sqlite3GetVdbe() has always been previously called */
102290 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
102291 /* The "x IN (SELECT rowid FROM table)" case */
102292 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
102293 VdbeCoverage(v);
102294
102295 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
102296 eType = IN_INDEX_ROWID;
102297 ExplainQueryPlan((pParse, 0,
102298 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
102299 sqlite3VdbeJumpHere(v, iAddr);
102300 }else{
102301 Index *pIdx; /* Iterator variable */
102302 int affinity_ok = 1;
102303 int i;
102304
102305 /* Check that the affinity that will be used to perform each
102306 ** comparison is the same as the affinity of each column in table
102307 ** on the RHS of the IN operator. If it not, it is not possible to
102308 ** use any index of the RHS table. */
102309 for(i=0; i<nExpr && affinity_ok; i++){
102310 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
102311 int iCol = pEList->a[i].pExpr->iColumn;
102312 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
102313 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
102314 testcase( cmpaff==SQLITE_AFF_BLOB );
102315 testcase( cmpaff==SQLITE_AFF_TEXT );
102316 switch( cmpaff ){
102317 case SQLITE_AFF_BLOB:
102318 break;
102319 case SQLITE_AFF_TEXT:
102320 /* sqlite3CompareAffinity() only returns TEXT if one side or the
102321 ** other has no affinity and the other side is TEXT. Hence,
102322 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
102323 ** and for the term on the LHS of the IN to have no affinity. */
102324 assert( idxaff==SQLITE_AFF_TEXT );
102325 break;
102326 default:
102327 affinity_ok = sqlite3IsNumericAffinity(idxaff);
102328 }
102329 }
102330
102331 if( affinity_ok ){
102332 /* Search for an existing index that will work for this IN operator */
102333 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
102334 Bitmask colUsed; /* Columns of the index used */
102335 Bitmask mCol; /* Mask for the current column */
102336 if( pIdx->nColumn<nExpr ) continue;
102337 if( pIdx->pPartIdxWhere!=0 ) continue;
102338 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
102339 ** BITMASK(nExpr) without overflowing */
102340 testcase( pIdx->nColumn==BMS-2 );
102341 testcase( pIdx->nColumn==BMS-1 );
102342 if( pIdx->nColumn>=BMS-1 ) continue;
102343 if( mustBeUnique ){
102344 if( pIdx->nKeyCol>nExpr
102345 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
102346 ){
102347 continue; /* This index is not unique over the IN RHS columns */
102348 }
102349 }
102350
102351 colUsed = 0; /* Columns of index used so far */
102352 for(i=0; i<nExpr; i++){
102353 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
102354 Expr *pRhs = pEList->a[i].pExpr;
102355 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
102356 int j;
102357
102358 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
102359 for(j=0; j<nExpr; j++){
102360 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
102361 assert( pIdx->azColl[j] );
102362 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
102363 continue;
102364 }
102365 break;
102366 }
102367 if( j==nExpr ) break;
102368 mCol = MASKBIT(j);
102369 if( mCol & colUsed ) break; /* Each column used only once */
102370 colUsed |= mCol;
102371 if( aiMap ) aiMap[i] = j;
102372 }
102373
102374 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
102375 if( colUsed==(MASKBIT(nExpr)-1) ){
102376 /* If we reach this point, that means the index pIdx is usable */
102377 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102378 ExplainQueryPlan((pParse, 0,
102379 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
102380 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
102381 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
102382 VdbeComment((v, "%s", pIdx->zName));
102383 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
102384 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
102385
102386 if( prRhsHasNull ){
102387#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
102388 i64 mask = (1<<nExpr)-1;
102389 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
102390 iTab, 0, 0, (u8*)&mask, P4_INT64);
102391#endif
102392 *prRhsHasNull = ++pParse->nMem;
102393 if( nExpr==1 ){
102394 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
102395 }
102396 }
102397 sqlite3VdbeJumpHere(v, iAddr);
102398 }
102399 } /* End loop over indexes */
102400 } /* End if( affinity_ok ) */
102401 } /* End if not an rowid index */
102402 } /* End attempt to optimize using an index */
102403
102404 /* If no preexisting index is available for the IN clause
102405 ** and IN_INDEX_NOOP is an allowed reply
102406 ** and the RHS of the IN operator is a list, not a subquery
102407 ** and the RHS is not constant or has two or fewer terms,
102408 ** then it is not worth creating an ephemeral table to evaluate
102409 ** the IN operator so return IN_INDEX_NOOP.
102410 */
102411 if( eType==0
102412 && (inFlags & IN_INDEX_NOOP_OK)
102413 && !ExprHasProperty(pX, EP_xIsSelect)
102414 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
102415 ){
102416 eType = IN_INDEX_NOOP;
102417 }
102418
102419 if( eType==0 ){
102420 /* Could not find an existing table or index to use as the RHS b-tree.
102421 ** We will have to generate an ephemeral table to do the job.
102422 */
102423 u32 savedNQueryLoop = pParse->nQueryLoop;
102424 int rMayHaveNull = 0;
102425 eType = IN_INDEX_EPH;
102426 if( inFlags & IN_INDEX_LOOP ){
102427 pParse->nQueryLoop = 0;
102428 }else if( prRhsHasNull ){
102429 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
102430 }
102431 assert( pX->op==TK_IN );
102432 sqlite3CodeRhsOfIN(pParse, pX, iTab);
102433 if( rMayHaveNull ){
102434 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
102435 }
102436 pParse->nQueryLoop = savedNQueryLoop;
102437 }
102438
102439 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
102440 int i, n;
102441 n = sqlite3ExprVectorSize(pX->pLeft);
102442 for(i=0; i<n; i++) aiMap[i] = i;
102443 }
102444 *piTab = iTab;
102445 return eType;
102446}
102447#endif
102448
102449#ifndef SQLITE_OMIT_SUBQUERY
102450/*
102451** Argument pExpr is an (?, ?...) IN(...) expression. This
102452** function allocates and returns a nul-terminated string containing
102453** the affinities to be used for each column of the comparison.
102454**
102455** It is the responsibility of the caller to ensure that the returned
102456** string is eventually freed using sqlite3DbFree().
102457*/
102458static char *exprINAffinity(Parse *pParse, Expr *pExpr){
102459 Expr *pLeft = pExpr->pLeft;
102460 int nVal = sqlite3ExprVectorSize(pLeft);
102461 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
102462 char *zRet;
102463
102464 assert( pExpr->op==TK_IN );
102465 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
102466 if( zRet ){
102467 int i;
102468 for(i=0; i<nVal; i++){
102469 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
102470 char a = sqlite3ExprAffinity(pA);
102471 if( pSelect ){
102472 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
102473 }else{
102474 zRet[i] = a;
102475 }
102476 }
102477 zRet[nVal] = '\0';
102478 }
102479 return zRet;
102480}
102481#endif
102482
102483#ifndef SQLITE_OMIT_SUBQUERY
102484/*
102485** Load the Parse object passed as the first argument with an error
102486** message of the form:
102487**
102488** "sub-select returns N columns - expected M"
102489*/
102490SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
102491 if( pParse->nErr==0 ){
102492 const char *zFmt = "sub-select returns %d columns - expected %d";
102493 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
102494 }
102495}
102496#endif
102497
102498/*
102499** Expression pExpr is a vector that has been used in a context where
102500** it is not permitted. If pExpr is a sub-select vector, this routine
102501** loads the Parse object with a message of the form:
102502**
102503** "sub-select returns N columns - expected 1"
102504**
102505** Or, if it is a regular scalar vector:
102506**
102507** "row value misused"
102508*/
102509SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
102510#ifndef SQLITE_OMIT_SUBQUERY
102511 if( pExpr->flags & EP_xIsSelect ){
102512 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
102513 }else
102514#endif
102515 {
102516 sqlite3ErrorMsg(pParse, "row value misused");
102517 }
102518}
102519
102520#ifndef SQLITE_OMIT_SUBQUERY
102521/*
102522** Generate code that will construct an ephemeral table containing all terms
102523** in the RHS of an IN operator. The IN operator can be in either of two
102524** forms:
102525**
102526** x IN (4,5,11) -- IN operator with list on right-hand side
102527** x IN (SELECT a FROM b) -- IN operator with subquery on the right
102528**
102529** The pExpr parameter is the IN operator. The cursor number for the
102530** constructed ephermeral table is returned. The first time the ephemeral
102531** table is computed, the cursor number is also stored in pExpr->iTable,
102532** however the cursor number returned might not be the same, as it might
102533** have been duplicated using OP_OpenDup.
102534**
102535** If the LHS expression ("x" in the examples) is a column value, or
102536** the SELECT statement returns a column value, then the affinity of that
102537** column is used to build the index keys. If both 'x' and the
102538** SELECT... statement are columns, then numeric affinity is used
102539** if either column has NUMERIC or INTEGER affinity. If neither
102540** 'x' nor the SELECT... statement are columns, then numeric affinity
102541** is used.
102542*/
102543SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
102544 Parse *pParse, /* Parsing context */
102545 Expr *pExpr, /* The IN operator */
102546 int iTab /* Use this cursor number */
102547){
102548 int addrOnce = 0; /* Address of the OP_Once instruction at top */
102549 int addr; /* Address of OP_OpenEphemeral instruction */
102550 Expr *pLeft; /* the LHS of the IN operator */
102551 KeyInfo *pKeyInfo = 0; /* Key information */
102552 int nVal; /* Size of vector pLeft */
102553 Vdbe *v; /* The prepared statement under construction */
102554
102555 v = pParse->pVdbe;
102556 assert( v!=0 );
102557
102558 /* The evaluation of the IN must be repeated every time it
102559 ** is encountered if any of the following is true:
102560 **
102561 ** * The right-hand side is a correlated subquery
102562 ** * The right-hand side is an expression list containing variables
102563 ** * We are inside a trigger
102564 **
102565 ** If all of the above are false, then we can compute the RHS just once
102566 ** and reuse it many names.
102567 */
102568 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
102569 /* Reuse of the RHS is allowed */
102570 /* If this routine has already been coded, but the previous code
102571 ** might not have been invoked yet, so invoke it now as a subroutine.
102572 */
102573 if( ExprHasProperty(pExpr, EP_Subrtn) ){
102574 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102575 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102576 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
102577 pExpr->x.pSelect->selId));
102578 }
102579 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
102580 pExpr->y.sub.iAddr);
102581 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
102582 sqlite3VdbeJumpHere(v, addrOnce);
102583 return;
102584 }
102585
102586 /* Begin coding the subroutine */
102587 ExprSetProperty(pExpr, EP_Subrtn);
102588 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
102589 pExpr->y.sub.regReturn = ++pParse->nMem;
102590 pExpr->y.sub.iAddr =
102591 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
102592 VdbeComment((v, "return address"));
102593
102594 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102595 }
102596
102597 /* Check to see if this is a vector IN operator */
102598 pLeft = pExpr->pLeft;
102599 nVal = sqlite3ExprVectorSize(pLeft);
102600
102601 /* Construct the ephemeral table that will contain the content of
102602 ** RHS of the IN operator.
102603 */
102604 pExpr->iTable = iTab;
102605 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
102606#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
102607 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102608 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
102609 }else{
102610 VdbeComment((v, "RHS of IN operator"));
102611 }
102612#endif
102613 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
102614
102615 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102616 /* Case 1: expr IN (SELECT ...)
102617 **
102618 ** Generate code to write the results of the select into the temporary
102619 ** table allocated and opened above.
102620 */
102621 Select *pSelect = pExpr->x.pSelect;
102622 ExprList *pEList = pSelect->pEList;
102623
102624 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
102625 addrOnce?"":"CORRELATED ", pSelect->selId
102626 ));
102627 /* If the LHS and RHS of the IN operator do not match, that
102628 ** error will have been caught long before we reach this point. */
102629 if( ALWAYS(pEList->nExpr==nVal) ){
102630 SelectDest dest;
102631 int i;
102632 sqlite3SelectDestInit(&dest, SRT_Set, iTab);
102633 dest.zAffSdst = exprINAffinity(pParse, pExpr);
102634 pSelect->iLimit = 0;
102635 testcase( pSelect->selFlags & SF_Distinct );
102636 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
102637 if( sqlite3Select(pParse, pSelect, &dest) ){
102638 sqlite3DbFree(pParse->db, dest.zAffSdst);
102639 sqlite3KeyInfoUnref(pKeyInfo);
102640 return;
102641 }
102642 sqlite3DbFree(pParse->db, dest.zAffSdst);
102643 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
102644 assert( pEList!=0 );
102645 assert( pEList->nExpr>0 );
102646 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
102647 for(i=0; i<nVal; i++){
102648 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
102649 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
102650 pParse, p, pEList->a[i].pExpr
102651 );
102652 }
102653 }
102654 }else if( ALWAYS(pExpr->x.pList!=0) ){
102655 /* Case 2: expr IN (exprlist)
102656 **
102657 ** For each expression, build an index key from the evaluation and
102658 ** store it in the temporary table. If <expr> is a column, then use
102659 ** that columns affinity when building index keys. If <expr> is not
102660 ** a column, use numeric affinity.
102661 */
102662 char affinity; /* Affinity of the LHS of the IN */
102663 int i;
102664 ExprList *pList = pExpr->x.pList;
102665 struct ExprList_item *pItem;
102666 int r1, r2;
102667 affinity = sqlite3ExprAffinity(pLeft);
102668 if( affinity<=SQLITE_AFF_NONE ){
102669 affinity = SQLITE_AFF_BLOB;
102670 }else if( affinity==SQLITE_AFF_REAL ){
102671 affinity = SQLITE_AFF_NUMERIC;
102672 }
102673 if( pKeyInfo ){
102674 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
102675 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
102676 }
102677
102678 /* Loop through each expression in <exprlist>. */
102679 r1 = sqlite3GetTempReg(pParse);
102680 r2 = sqlite3GetTempReg(pParse);
102681 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
102682 Expr *pE2 = pItem->pExpr;
102683
102684 /* If the expression is not constant then we will need to
102685 ** disable the test that was generated above that makes sure
102686 ** this code only executes once. Because for a non-constant
102687 ** expression we need to rerun this code each time.
102688 */
102689 if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
102690 sqlite3VdbeChangeToNoop(v, addrOnce);
102691 ExprClearProperty(pExpr, EP_Subrtn);
102692 addrOnce = 0;
102693 }
102694
102695 /* Evaluate the expression and insert it into the temp table */
102696 sqlite3ExprCode(pParse, pE2, r1);
102697 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
102698 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1);
102699 }
102700 sqlite3ReleaseTempReg(pParse, r1);
102701 sqlite3ReleaseTempReg(pParse, r2);
102702 }
102703 if( pKeyInfo ){
102704 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
102705 }
102706 if( addrOnce ){
102707 sqlite3VdbeJumpHere(v, addrOnce);
102708 /* Subroutine return */
102709 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
102710 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
102711 sqlite3ClearTempRegCache(pParse);
102712 }
102713}
102714#endif /* SQLITE_OMIT_SUBQUERY */
102715
102716/*
102717** Generate code for scalar subqueries used as a subquery expression
102718** or EXISTS operator:
102719**
102720** (SELECT a FROM b) -- subquery
102721** EXISTS (SELECT a FROM b) -- EXISTS subquery
102722**
102723** The pExpr parameter is the SELECT or EXISTS operator to be coded.
102724**
102725** Return the register that holds the result. For a multi-column SELECT,
102726** the result is stored in a contiguous array of registers and the
102727** return value is the register of the left-most result column.
102728** Return 0 if an error occurs.
102729*/
102730#ifndef SQLITE_OMIT_SUBQUERY
102731SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
102732 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
102733 int rReg = 0; /* Register storing resulting */
102734 Select *pSel; /* SELECT statement to encode */
102735 SelectDest dest; /* How to deal with SELECT result */
102736 int nReg; /* Registers to allocate */
102737 Expr *pLimit; /* New limit expression */
102738
102739 Vdbe *v = pParse->pVdbe;
102740 assert( v!=0 );
102741 testcase( pExpr->op==TK_EXISTS );
102742 testcase( pExpr->op==TK_SELECT );
102743 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
102744 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
102745 pSel = pExpr->x.pSelect;
102746
102747 /* The evaluation of the EXISTS/SELECT must be repeated every time it
102748 ** is encountered if any of the following is true:
102749 **
102750 ** * The right-hand side is a correlated subquery
102751 ** * The right-hand side is an expression list containing variables
102752 ** * We are inside a trigger
102753 **
102754 ** If all of the above are false, then we can run this code just once
102755 ** save the results, and reuse the same result on subsequent invocations.
102756 */
102757 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
102758 /* If this routine has already been coded, then invoke it as a
102759 ** subroutine. */
102760 if( ExprHasProperty(pExpr, EP_Subrtn) ){
102761 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
102762 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
102763 pExpr->y.sub.iAddr);
102764 return pExpr->iTable;
102765 }
102766
102767 /* Begin coding the subroutine */
102768 ExprSetProperty(pExpr, EP_Subrtn);
102769 pExpr->y.sub.regReturn = ++pParse->nMem;
102770 pExpr->y.sub.iAddr =
102771 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
102772 VdbeComment((v, "return address"));
102773
102774 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
102775 }
102776
102777 /* For a SELECT, generate code to put the values for all columns of
102778 ** the first row into an array of registers and return the index of
102779 ** the first register.
102780 **
102781 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
102782 ** into a register and return that register number.
102783 **
102784 ** In both cases, the query is augmented with "LIMIT 1". Any
102785 ** preexisting limit is discarded in place of the new LIMIT 1.
102786 */
102787 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
102788 addrOnce?"":"CORRELATED ", pSel->selId));
102789 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
102790 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
102791 pParse->nMem += nReg;
102792 if( pExpr->op==TK_SELECT ){
102793 dest.eDest = SRT_Mem;
102794 dest.iSdst = dest.iSDParm;
102795 dest.nSdst = nReg;
102796 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
102797 VdbeComment((v, "Init subquery result"));
102798 }else{
102799 dest.eDest = SRT_Exists;
102800 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
102801 VdbeComment((v, "Init EXISTS result"));
102802 }
102803 if( pSel->pLimit ){
102804 /* The subquery already has a limit. If the pre-existing limit is X
102805 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
102806 sqlite3 *db = pParse->db;
102807 pLimit = sqlite3Expr(db, TK_INTEGER, "0");
102808 if( pLimit ){
102809 pLimit->affExpr = SQLITE_AFF_NUMERIC;
102810 pLimit = sqlite3PExpr(pParse, TK_NE,
102811 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
102812 }
102813 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
102814 pSel->pLimit->pLeft = pLimit;
102815 }else{
102816 /* If there is no pre-existing limit add a limit of 1 */
102817 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
102818 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
102819 }
102820 pSel->iLimit = 0;
102821 if( sqlite3Select(pParse, pSel, &dest) ){
102822 return 0;
102823 }
102824 pExpr->iTable = rReg = dest.iSDParm;
102825 ExprSetVVAProperty(pExpr, EP_NoReduce);
102826 if( addrOnce ){
102827 sqlite3VdbeJumpHere(v, addrOnce);
102828
102829 /* Subroutine return */
102830 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
102831 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
102832 sqlite3ClearTempRegCache(pParse);
102833 }
102834
102835 return rReg;
102836}
102837#endif /* SQLITE_OMIT_SUBQUERY */
102838
102839#ifndef SQLITE_OMIT_SUBQUERY
102840/*
102841** Expr pIn is an IN(...) expression. This function checks that the
102842** sub-select on the RHS of the IN() operator has the same number of
102843** columns as the vector on the LHS. Or, if the RHS of the IN() is not
102844** a sub-query, that the LHS is a vector of size 1.
102845*/
102846SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
102847 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
102848 if( (pIn->flags & EP_xIsSelect) ){
102849 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
102850 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
102851 return 1;
102852 }
102853 }else if( nVector!=1 ){
102854 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
102855 return 1;
102856 }
102857 return 0;
102858}
102859#endif
102860
102861#ifndef SQLITE_OMIT_SUBQUERY
102862/*
102863** Generate code for an IN expression.
102864**
102865** x IN (SELECT ...)
102866** x IN (value, value, ...)
102867**
102868** The left-hand side (LHS) is a scalar or vector expression. The
102869** right-hand side (RHS) is an array of zero or more scalar values, or a
102870** subquery. If the RHS is a subquery, the number of result columns must
102871** match the number of columns in the vector on the LHS. If the RHS is
102872** a list of values, the LHS must be a scalar.
102873**
102874** The IN operator is true if the LHS value is contained within the RHS.
102875** The result is false if the LHS is definitely not in the RHS. The
102876** result is NULL if the presence of the LHS in the RHS cannot be
102877** determined due to NULLs.
102878**
102879** This routine generates code that jumps to destIfFalse if the LHS is not
102880** contained within the RHS. If due to NULLs we cannot determine if the LHS
102881** is contained in the RHS then jump to destIfNull. If the LHS is contained
102882** within the RHS then fall through.
102883**
102884** See the separate in-operator.md documentation file in the canonical
102885** SQLite source tree for additional information.
102886*/
102887static void sqlite3ExprCodeIN(
102888 Parse *pParse, /* Parsing and code generating context */
102889 Expr *pExpr, /* The IN expression */
102890 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
102891 int destIfNull /* Jump here if the results are unknown due to NULLs */
102892){
102893 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
102894 int eType; /* Type of the RHS */
102895 int rLhs; /* Register(s) holding the LHS values */
102896 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
102897 Vdbe *v; /* Statement under construction */
102898 int *aiMap = 0; /* Map from vector field to index column */
102899 char *zAff = 0; /* Affinity string for comparisons */
102900 int nVector; /* Size of vectors for this IN operator */
102901 int iDummy; /* Dummy parameter to exprCodeVector() */
102902 Expr *pLeft; /* The LHS of the IN operator */
102903 int i; /* loop counter */
102904 int destStep2; /* Where to jump when NULLs seen in step 2 */
102905 int destStep6 = 0; /* Start of code for Step 6 */
102906 int addrTruthOp; /* Address of opcode that determines the IN is true */
102907 int destNotNull; /* Jump here if a comparison is not true in step 6 */
102908 int addrTop; /* Top of the step-6 loop */
102909 int iTab = 0; /* Index to use */
102910 u8 okConstFactor = pParse->okConstFactor;
102911
102912 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
102913 pLeft = pExpr->pLeft;
102914 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
102915 zAff = exprINAffinity(pParse, pExpr);
102916 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
102917 aiMap = (int*)sqlite3DbMallocZero(
102918 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
102919 );
102920 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
102921
102922 /* Attempt to compute the RHS. After this step, if anything other than
102923 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
102924 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
102925 ** the RHS has not yet been coded. */
102926 v = pParse->pVdbe;
102927 assert( v!=0 ); /* OOM detected prior to this routine */
102928 VdbeNoopComment((v, "begin IN expr"));
102929 eType = sqlite3FindInIndex(pParse, pExpr,
102930 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
102931 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
102932 aiMap, &iTab);
102933
102934 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
102935 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
102936 );
102937#ifdef SQLITE_DEBUG
102938 /* Confirm that aiMap[] contains nVector integer values between 0 and
102939 ** nVector-1. */
102940 for(i=0; i<nVector; i++){
102941 int j, cnt;
102942 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
102943 assert( cnt==1 );
102944 }
102945#endif
102946
102947 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
102948 ** vector, then it is stored in an array of nVector registers starting
102949 ** at r1.
102950 **
102951 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
102952 ** so that the fields are in the same order as an existing index. The
102953 ** aiMap[] array contains a mapping from the original LHS field order to
102954 ** the field order that matches the RHS index.
102955 **
102956 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
102957 ** even if it is constant, as OP_Affinity may be used on the register
102958 ** by code generated below. */
102959 assert( pParse->okConstFactor==okConstFactor );
102960 pParse->okConstFactor = 0;
102961 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
102962 pParse->okConstFactor = okConstFactor;
102963 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
102964 if( i==nVector ){
102965 /* LHS fields are not reordered */
102966 rLhs = rLhsOrig;
102967 }else{
102968 /* Need to reorder the LHS fields according to aiMap */
102969 rLhs = sqlite3GetTempRange(pParse, nVector);
102970 for(i=0; i<nVector; i++){
102971 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
102972 }
102973 }
102974
102975 /* If sqlite3FindInIndex() did not find or create an index that is
102976 ** suitable for evaluating the IN operator, then evaluate using a
102977 ** sequence of comparisons.
102978 **
102979 ** This is step (1) in the in-operator.md optimized algorithm.
102980 */
102981 if( eType==IN_INDEX_NOOP ){
102982 ExprList *pList = pExpr->x.pList;
102983 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
102984 int labelOk = sqlite3VdbeMakeLabel(pParse);
102985 int r2, regToFree;
102986 int regCkNull = 0;
102987 int ii;
102988 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
102989 if( destIfNull!=destIfFalse ){
102990 regCkNull = sqlite3GetTempReg(pParse);
102991 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
102992 }
102993 for(ii=0; ii<pList->nExpr; ii++){
102994 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
102995 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
102996 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
102997 }
102998 sqlite3ReleaseTempReg(pParse, regToFree);
102999 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
103000 int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
103001 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
103002 (void*)pColl, P4_COLLSEQ);
103003 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
103004 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
103005 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
103006 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
103007 sqlite3VdbeChangeP5(v, zAff[0]);
103008 }else{
103009 int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
103010 assert( destIfNull==destIfFalse );
103011 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
103012 (void*)pColl, P4_COLLSEQ);
103013 VdbeCoverageIf(v, op==OP_Ne);
103014 VdbeCoverageIf(v, op==OP_IsNull);
103015 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
103016 }
103017 }
103018 if( regCkNull ){
103019 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
103020 sqlite3VdbeGoto(v, destIfFalse);
103021 }
103022 sqlite3VdbeResolveLabel(v, labelOk);
103023 sqlite3ReleaseTempReg(pParse, regCkNull);
103024 goto sqlite3ExprCodeIN_finished;
103025 }
103026
103027 /* Step 2: Check to see if the LHS contains any NULL columns. If the
103028 ** LHS does contain NULLs then the result must be either FALSE or NULL.
103029 ** We will then skip the binary search of the RHS.
103030 */
103031 if( destIfNull==destIfFalse ){
103032 destStep2 = destIfFalse;
103033 }else{
103034 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
103035 }
103036 if( pParse->nErr ) goto sqlite3ExprCodeIN_finished;
103037 for(i=0; i<nVector; i++){
103038 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
103039 if( sqlite3ExprCanBeNull(p) ){
103040 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
103041 VdbeCoverage(v);
103042 }
103043 }
103044
103045 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
103046 ** of the RHS using the LHS as a probe. If found, the result is
103047 ** true.
103048 */
103049 if( eType==IN_INDEX_ROWID ){
103050 /* In this case, the RHS is the ROWID of table b-tree and so we also
103051 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
103052 ** into a single opcode. */
103053 sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs);
103054 VdbeCoverage(v);
103055 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */
103056 }else{
103057 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
103058 if( destIfFalse==destIfNull ){
103059 /* Combine Step 3 and Step 5 into a single opcode */
103060 sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
103061 rLhs, nVector); VdbeCoverage(v);
103062 goto sqlite3ExprCodeIN_finished;
103063 }
103064 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
103065 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0,
103066 rLhs, nVector); VdbeCoverage(v);
103067 }
103068
103069 /* Step 4. If the RHS is known to be non-NULL and we did not find
103070 ** an match on the search above, then the result must be FALSE.
103071 */
103072 if( rRhsHasNull && nVector==1 ){
103073 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
103074 VdbeCoverage(v);
103075 }
103076
103077 /* Step 5. If we do not care about the difference between NULL and
103078 ** FALSE, then just return false.
103079 */
103080 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
103081
103082 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
103083 ** If any comparison is NULL, then the result is NULL. If all
103084 ** comparisons are FALSE then the final result is FALSE.
103085 **
103086 ** For a scalar LHS, it is sufficient to check just the first row
103087 ** of the RHS.
103088 */
103089 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
103090 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse);
103091 VdbeCoverage(v);
103092 if( nVector>1 ){
103093 destNotNull = sqlite3VdbeMakeLabel(pParse);
103094 }else{
103095 /* For nVector==1, combine steps 6 and 7 by immediately returning
103096 ** FALSE if the first comparison is not NULL */
103097 destNotNull = destIfFalse;
103098 }
103099 for(i=0; i<nVector; i++){
103100 Expr *p;
103101 CollSeq *pColl;
103102 int r3 = sqlite3GetTempReg(pParse);
103103 p = sqlite3VectorFieldSubexpr(pLeft, i);
103104 pColl = sqlite3ExprCollSeq(pParse, p);
103105 sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3);
103106 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
103107 (void*)pColl, P4_COLLSEQ);
103108 VdbeCoverage(v);
103109 sqlite3ReleaseTempReg(pParse, r3);
103110 }
103111 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
103112 if( nVector>1 ){
103113 sqlite3VdbeResolveLabel(v, destNotNull);
103114 sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1);
103115 VdbeCoverage(v);
103116
103117 /* Step 7: If we reach this point, we know that the result must
103118 ** be false. */
103119 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
103120 }
103121
103122 /* Jumps here in order to return true. */
103123 sqlite3VdbeJumpHere(v, addrTruthOp);
103124
103125sqlite3ExprCodeIN_finished:
103126 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
103127 VdbeComment((v, "end IN expr"));
103128sqlite3ExprCodeIN_oom_error:
103129 sqlite3DbFree(pParse->db, aiMap);
103130 sqlite3DbFree(pParse->db, zAff);
103131}
103132#endif /* SQLITE_OMIT_SUBQUERY */
103133
103134#ifndef SQLITE_OMIT_FLOATING_POINT
103135/*
103136** Generate an instruction that will put the floating point
103137** value described by z[0..n-1] into register iMem.
103138**
103139** The z[] string will probably not be zero-terminated. But the
103140** z[n] character is guaranteed to be something that does not look
103141** like the continuation of the number.
103142*/
103143static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
103144 if( ALWAYS(z!=0) ){
103145 double value;
103146 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
103147 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
103148 if( negateFlag ) value = -value;
103149 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
103150 }
103151}
103152#endif
103153
103154
103155/*
103156** Generate an instruction that will put the integer describe by
103157** text z[0..n-1] into register iMem.
103158**
103159** Expr.u.zToken is always UTF8 and zero-terminated.
103160*/
103161static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
103162 Vdbe *v = pParse->pVdbe;
103163 if( pExpr->flags & EP_IntValue ){
103164 int i = pExpr->u.iValue;
103165 assert( i>=0 );
103166 if( negFlag ) i = -i;
103167 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
103168 }else{
103169 int c;
103170 i64 value;
103171 const char *z = pExpr->u.zToken;
103172 assert( z!=0 );
103173 c = sqlite3DecOrHexToI64(z, &value);
103174 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
103175#ifdef SQLITE_OMIT_FLOATING_POINT
103176 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
103177#else
103178#ifndef SQLITE_OMIT_HEX_INTEGER
103179 if( sqlite3_strnicmp(z,"0x",2)==0 ){
103180 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
103181 }else
103182#endif
103183 {
103184 codeReal(v, z, negFlag, iMem);
103185 }
103186#endif
103187 }else{
103188 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
103189 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
103190 }
103191 }
103192}
103193
103194
103195/* Generate code that will load into register regOut a value that is
103196** appropriate for the iIdxCol-th column of index pIdx.
103197*/
103198SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
103199 Parse *pParse, /* The parsing context */
103200 Index *pIdx, /* The index whose column is to be loaded */
103201 int iTabCur, /* Cursor pointing to a table row */
103202 int iIdxCol, /* The column of the index to be loaded */
103203 int regOut /* Store the index column value in this register */
103204){
103205 i16 iTabCol = pIdx->aiColumn[iIdxCol];
103206 if( iTabCol==XN_EXPR ){
103207 assert( pIdx->aColExpr );
103208 assert( pIdx->aColExpr->nExpr>iIdxCol );
103209 pParse->iSelfTab = iTabCur + 1;
103210 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
103211 pParse->iSelfTab = 0;
103212 }else{
103213 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
103214 iTabCol, regOut);
103215 }
103216}
103217
103218#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103219/*
103220** Generate code that will compute the value of generated column pCol
103221** and store the result in register regOut
103222*/
103223SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(
103224 Parse *pParse,
103225 Column *pCol,
103226 int regOut
103227){
103228 int iAddr;
103229 Vdbe *v = pParse->pVdbe;
103230 assert( v!=0 );
103231 assert( pParse->iSelfTab!=0 );
103232 if( pParse->iSelfTab>0 ){
103233 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
103234 }else{
103235 iAddr = 0;
103236 }
103237 sqlite3ExprCodeCopy(pParse, pCol->pDflt, regOut);
103238 if( pCol->affinity>=SQLITE_AFF_TEXT ){
103239 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
103240 }
103241 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
103242}
103243#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
103244
103245/*
103246** Generate code to extract the value of the iCol-th column of a table.
103247*/
103248SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
103249 Vdbe *v, /* Parsing context */
103250 Table *pTab, /* The table containing the value */
103251 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
103252 int iCol, /* Index of the column to extract */
103253 int regOut /* Extract the value into this register */
103254){
103255 Column *pCol;
103256 assert( v!=0 );
103257 if( pTab==0 ){
103258 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
103259 return;
103260 }
103261 if( iCol<0 || iCol==pTab->iPKey ){
103262 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
103263 }else{
103264 int op;
103265 int x;
103266 if( IsVirtual(pTab) ){
103267 op = OP_VColumn;
103268 x = iCol;
103269#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103270 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
103271 Parse *pParse = sqlite3VdbeParser(v);
103272 if( pCol->colFlags & COLFLAG_BUSY ){
103273 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pCol->zName);
103274 }else{
103275 int savedSelfTab = pParse->iSelfTab;
103276 pCol->colFlags |= COLFLAG_BUSY;
103277 pParse->iSelfTab = iTabCur+1;
103278 sqlite3ExprCodeGeneratedColumn(pParse, pCol, regOut);
103279 pParse->iSelfTab = savedSelfTab;
103280 pCol->colFlags &= ~COLFLAG_BUSY;
103281 }
103282 return;
103283#endif
103284 }else if( !HasRowid(pTab) ){
103285 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
103286 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
103287 op = OP_Column;
103288 }else{
103289 x = sqlite3TableColumnToStorage(pTab,iCol);
103290 testcase( x!=iCol );
103291 op = OP_Column;
103292 }
103293 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
103294 sqlite3ColumnDefault(v, pTab, iCol, regOut);
103295 }
103296}
103297
103298/*
103299** Generate code that will extract the iColumn-th column from
103300** table pTab and store the column value in register iReg.
103301**
103302** There must be an open cursor to pTab in iTable when this routine
103303** is called. If iColumn<0 then code is generated that extracts the rowid.
103304*/
103305SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
103306 Parse *pParse, /* Parsing and code generating context */
103307 Table *pTab, /* Description of the table we are reading from */
103308 int iColumn, /* Index of the table column */
103309 int iTable, /* The cursor pointing to the table */
103310 int iReg, /* Store results here */
103311 u8 p5 /* P5 value for OP_Column + FLAGS */
103312){
103313 assert( pParse->pVdbe!=0 );
103314 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
103315 if( p5 ){
103316 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
103317 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
103318 }
103319 return iReg;
103320}
103321
103322/*
103323** Generate code to move content from registers iFrom...iFrom+nReg-1
103324** over to iTo..iTo+nReg-1.
103325*/
103326SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
103327 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
103328}
103329
103330/*
103331** Convert a scalar expression node to a TK_REGISTER referencing
103332** register iReg. The caller must ensure that iReg already contains
103333** the correct value for the expression.
103334*/
103335static void exprToRegister(Expr *pExpr, int iReg){
103336 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
103337 p->op2 = p->op;
103338 p->op = TK_REGISTER;
103339 p->iTable = iReg;
103340 ExprClearProperty(p, EP_Skip);
103341}
103342
103343/*
103344** Evaluate an expression (either a vector or a scalar expression) and store
103345** the result in continguous temporary registers. Return the index of
103346** the first register used to store the result.
103347**
103348** If the returned result register is a temporary scalar, then also write
103349** that register number into *piFreeable. If the returned result register
103350** is not a temporary or if the expression is a vector set *piFreeable
103351** to 0.
103352*/
103353static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
103354 int iResult;
103355 int nResult = sqlite3ExprVectorSize(p);
103356 if( nResult==1 ){
103357 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
103358 }else{
103359 *piFreeable = 0;
103360 if( p->op==TK_SELECT ){
103361#if SQLITE_OMIT_SUBQUERY
103362 iResult = 0;
103363#else
103364 iResult = sqlite3CodeSubselect(pParse, p);
103365#endif
103366 }else{
103367 int i;
103368 iResult = pParse->nMem+1;
103369 pParse->nMem += nResult;
103370 for(i=0; i<nResult; i++){
103371 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
103372 }
103373 }
103374 }
103375 return iResult;
103376}
103377
103378/*
103379** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
103380** so that a subsequent copy will not be merged into this one.
103381*/
103382static void setDoNotMergeFlagOnCopy(Vdbe *v){
103383 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
103384 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergable */
103385 }
103386}
103387
103388/*
103389** Generate code to implement special SQL functions that are implemented
103390** in-line rather than by using the usual callbacks.
103391*/
103392static int exprCodeInlineFunction(
103393 Parse *pParse, /* Parsing context */
103394 ExprList *pFarg, /* List of function arguments */
103395 int iFuncId, /* Function ID. One of the INTFUNC_... values */
103396 int target /* Store function result in this register */
103397){
103398 int nFarg;
103399 Vdbe *v = pParse->pVdbe;
103400 assert( v!=0 );
103401 assert( pFarg!=0 );
103402 nFarg = pFarg->nExpr;
103403 assert( nFarg>0 ); /* All in-line functions have at least one argument */
103404 switch( iFuncId ){
103405 case INLINEFUNC_coalesce: {
103406 /* Attempt a direct implementation of the built-in COALESCE() and
103407 ** IFNULL() functions. This avoids unnecessary evaluation of
103408 ** arguments past the first non-NULL argument.
103409 */
103410 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
103411 int i;
103412 assert( nFarg>=2 );
103413 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
103414 for(i=1; i<nFarg; i++){
103415 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
103416 VdbeCoverage(v);
103417 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
103418 }
103419 setDoNotMergeFlagOnCopy(v);
103420 sqlite3VdbeResolveLabel(v, endCoalesce);
103421 break;
103422 }
103423 case INLINEFUNC_iif: {
103424 Expr caseExpr;
103425 memset(&caseExpr, 0, sizeof(caseExpr));
103426 caseExpr.op = TK_CASE;
103427 caseExpr.x.pList = pFarg;
103428 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
103429 }
103430
103431 default: {
103432 /* The UNLIKELY() function is a no-op. The result is the value
103433 ** of the first argument.
103434 */
103435 assert( nFarg==1 || nFarg==2 );
103436 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
103437 break;
103438 }
103439
103440 /***********************************************************************
103441 ** Test-only SQL functions that are only usable if enabled
103442 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
103443 */
103444 case INLINEFUNC_expr_compare: {
103445 /* Compare two expressions using sqlite3ExprCompare() */
103446 assert( nFarg==2 );
103447 sqlite3VdbeAddOp2(v, OP_Integer,
103448 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
103449 target);
103450 break;
103451 }
103452
103453 case INLINEFUNC_expr_implies_expr: {
103454 /* Compare two expressions using sqlite3ExprImpliesExpr() */
103455 assert( nFarg==2 );
103456 sqlite3VdbeAddOp2(v, OP_Integer,
103457 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
103458 target);
103459 break;
103460 }
103461
103462 case INLINEFUNC_implies_nonnull_row: {
103463 /* REsult of sqlite3ExprImpliesNonNullRow() */
103464 Expr *pA1;
103465 assert( nFarg==2 );
103466 pA1 = pFarg->a[1].pExpr;
103467 if( pA1->op==TK_COLUMN ){
103468 sqlite3VdbeAddOp2(v, OP_Integer,
103469 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
103470 target);
103471 }else{
103472 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103473 }
103474 break;
103475 }
103476
103477#ifdef SQLITE_DEBUG
103478 case INLINEFUNC_affinity: {
103479 /* The AFFINITY() function evaluates to a string that describes
103480 ** the type affinity of the argument. This is used for testing of
103481 ** the SQLite type logic.
103482 */
103483 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
103484 char aff;
103485 assert( nFarg==1 );
103486 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
103487 sqlite3VdbeLoadString(v, target,
103488 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
103489 break;
103490 }
103491#endif
103492 }
103493 return target;
103494}
103495
103496
103497/*
103498** Generate code into the current Vdbe to evaluate the given
103499** expression. Attempt to store the results in register "target".
103500** Return the register where results are stored.
103501**
103502** With this routine, there is no guarantee that results will
103503** be stored in target. The result might be stored in some other
103504** register if it is convenient to do so. The calling function
103505** must check the return code and move the results to the desired
103506** register.
103507*/
103508SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
103509 Vdbe *v = pParse->pVdbe; /* The VM under construction */
103510 int op; /* The opcode being coded */
103511 int inReg = target; /* Results stored in register inReg */
103512 int regFree1 = 0; /* If non-zero free this temporary register */
103513 int regFree2 = 0; /* If non-zero free this temporary register */
103514 int r1, r2; /* Various register numbers */
103515 Expr tempX; /* Temporary expression node */
103516 int p5 = 0;
103517
103518 assert( target>0 && target<=pParse->nMem );
103519 assert( v!=0 );
103520
103521expr_code_doover:
103522 if( pExpr==0 ){
103523 op = TK_NULL;
103524 }else{
103525 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
103526 op = pExpr->op;
103527 }
103528 switch( op ){
103529 case TK_AGG_COLUMN: {
103530 AggInfo *pAggInfo = pExpr->pAggInfo;
103531 struct AggInfo_col *pCol;
103532 assert( pAggInfo!=0 );
103533 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
103534 pCol = &pAggInfo->aCol[pExpr->iAgg];
103535 if( !pAggInfo->directMode ){
103536 assert( pCol->iMem>0 );
103537 return pCol->iMem;
103538 }else if( pAggInfo->useSortingIdx ){
103539 Table *pTab = pCol->pTab;
103540 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
103541 pCol->iSorterColumn, target);
103542 if( pCol->iColumn<0 ){
103543 VdbeComment((v,"%s.rowid",pTab->zName));
103544 }else{
103545 VdbeComment((v,"%s.%s",pTab->zName,pTab->aCol[pCol->iColumn].zName));
103546 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
103547 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
103548 }
103549 }
103550 return target;
103551 }
103552 /* Otherwise, fall thru into the TK_COLUMN case */
103553 /* no break */ deliberate_fall_through
103554 }
103555 case TK_COLUMN: {
103556 int iTab = pExpr->iTable;
103557 int iReg;
103558 if( ExprHasProperty(pExpr, EP_FixedCol) ){
103559 /* This COLUMN expression is really a constant due to WHERE clause
103560 ** constraints, and that constant is coded by the pExpr->pLeft
103561 ** expresssion. However, make sure the constant has the correct
103562 ** datatype by applying the Affinity of the table column to the
103563 ** constant.
103564 */
103565 int aff;
103566 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
103567 if( pExpr->y.pTab ){
103568 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103569 }else{
103570 aff = pExpr->affExpr;
103571 }
103572 if( aff>SQLITE_AFF_BLOB ){
103573 static const char zAff[] = "B\000C\000D\000E";
103574 assert( SQLITE_AFF_BLOB=='A' );
103575 assert( SQLITE_AFF_TEXT=='B' );
103576 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
103577 &zAff[(aff-'B')*2], P4_STATIC);
103578 }
103579 return iReg;
103580 }
103581 if( iTab<0 ){
103582 if( pParse->iSelfTab<0 ){
103583 /* Other columns in the same row for CHECK constraints or
103584 ** generated columns or for inserting into partial index.
103585 ** The row is unpacked into registers beginning at
103586 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
103587 ** immediately prior to the first column.
103588 */
103589 Column *pCol;
103590 Table *pTab = pExpr->y.pTab;
103591 int iSrc;
103592 int iCol = pExpr->iColumn;
103593 assert( pTab!=0 );
103594 assert( iCol>=XN_ROWID );
103595 assert( iCol<pTab->nCol );
103596 if( iCol<0 ){
103597 return -1-pParse->iSelfTab;
103598 }
103599 pCol = pTab->aCol + iCol;
103600 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
103601 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
103602#ifndef SQLITE_OMIT_GENERATED_COLUMNS
103603 if( pCol->colFlags & COLFLAG_GENERATED ){
103604 if( pCol->colFlags & COLFLAG_BUSY ){
103605 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
103606 pCol->zName);
103607 return 0;
103608 }
103609 pCol->colFlags |= COLFLAG_BUSY;
103610 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
103611 sqlite3ExprCodeGeneratedColumn(pParse, pCol, iSrc);
103612 }
103613 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
103614 return iSrc;
103615 }else
103616#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
103617 if( pCol->affinity==SQLITE_AFF_REAL ){
103618 sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target);
103619 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
103620 return target;
103621 }else{
103622 return iSrc;
103623 }
103624 }else{
103625 /* Coding an expression that is part of an index where column names
103626 ** in the index refer to the table to which the index belongs */
103627 iTab = pParse->iSelfTab - 1;
103628 }
103629 }
103630 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
103631 pExpr->iColumn, iTab, target,
103632 pExpr->op2);
103633 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
103634 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
103635 }
103636 return iReg;
103637 }
103638 case TK_INTEGER: {
103639 codeInteger(pParse, pExpr, 0, target);
103640 return target;
103641 }
103642 case TK_TRUEFALSE: {
103643 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
103644 return target;
103645 }
103646#ifndef SQLITE_OMIT_FLOATING_POINT
103647 case TK_FLOAT: {
103648 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103649 codeReal(v, pExpr->u.zToken, 0, target);
103650 return target;
103651 }
103652#endif
103653 case TK_STRING: {
103654 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103655 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
103656 return target;
103657 }
103658 default: {
103659 /* Make NULL the default case so that if a bug causes an illegal
103660 ** Expr node to be passed into this function, it will be handled
103661 ** sanely and not crash. But keep the assert() to bring the problem
103662 ** to the attention of the developers. */
103663 assert( op==TK_NULL );
103664 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103665 return target;
103666 }
103667#ifndef SQLITE_OMIT_BLOB_LITERAL
103668 case TK_BLOB: {
103669 int n;
103670 const char *z;
103671 char *zBlob;
103672 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103673 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
103674 assert( pExpr->u.zToken[1]=='\'' );
103675 z = &pExpr->u.zToken[2];
103676 n = sqlite3Strlen30(z) - 1;
103677 assert( z[n]=='\'' );
103678 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
103679 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
103680 return target;
103681 }
103682#endif
103683 case TK_VARIABLE: {
103684 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103685 assert( pExpr->u.zToken!=0 );
103686 assert( pExpr->u.zToken[0]!=0 );
103687 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
103688 if( pExpr->u.zToken[1]!=0 ){
103689 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
103690 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
103691 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
103692 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
103693 }
103694 return target;
103695 }
103696 case TK_REGISTER: {
103697 return pExpr->iTable;
103698 }
103699#ifndef SQLITE_OMIT_CAST
103700 case TK_CAST: {
103701 /* Expressions of the form: CAST(pLeft AS token) */
103702 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
103703 if( inReg!=target ){
103704 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
103705 inReg = target;
103706 }
103707 sqlite3VdbeAddOp2(v, OP_Cast, target,
103708 sqlite3AffinityType(pExpr->u.zToken, 0));
103709 return inReg;
103710 }
103711#endif /* SQLITE_OMIT_CAST */
103712 case TK_IS:
103713 case TK_ISNOT:
103714 op = (op==TK_IS) ? TK_EQ : TK_NE;
103715 p5 = SQLITE_NULLEQ;
103716 /* fall-through */
103717 case TK_LT:
103718 case TK_LE:
103719 case TK_GT:
103720 case TK_GE:
103721 case TK_NE:
103722 case TK_EQ: {
103723 Expr *pLeft = pExpr->pLeft;
103724 if( sqlite3ExprIsVector(pLeft) ){
103725 codeVectorCompare(pParse, pExpr, target, op, p5);
103726 }else{
103727 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
103728 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
103729 codeCompare(pParse, pLeft, pExpr->pRight, op,
103730 r1, r2, inReg, SQLITE_STOREP2 | p5,
103731 ExprHasProperty(pExpr,EP_Commuted));
103732 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
103733 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
103734 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
103735 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
103736 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
103737 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
103738 testcase( regFree1==0 );
103739 testcase( regFree2==0 );
103740 }
103741 break;
103742 }
103743 case TK_AND:
103744 case TK_OR:
103745 case TK_PLUS:
103746 case TK_STAR:
103747 case TK_MINUS:
103748 case TK_REM:
103749 case TK_BITAND:
103750 case TK_BITOR:
103751 case TK_SLASH:
103752 case TK_LSHIFT:
103753 case TK_RSHIFT:
103754 case TK_CONCAT: {
103755 assert( TK_AND==OP_And ); testcase( op==TK_AND );
103756 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
103757 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
103758 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
103759 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
103760 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
103761 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
103762 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
103763 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
103764 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
103765 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
103766 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103767 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
103768 sqlite3VdbeAddOp3(v, op, r2, r1, target);
103769 testcase( regFree1==0 );
103770 testcase( regFree2==0 );
103771 break;
103772 }
103773 case TK_UMINUS: {
103774 Expr *pLeft = pExpr->pLeft;
103775 assert( pLeft );
103776 if( pLeft->op==TK_INTEGER ){
103777 codeInteger(pParse, pLeft, 1, target);
103778 return target;
103779#ifndef SQLITE_OMIT_FLOATING_POINT
103780 }else if( pLeft->op==TK_FLOAT ){
103781 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103782 codeReal(v, pLeft->u.zToken, 1, target);
103783 return target;
103784#endif
103785 }else{
103786 tempX.op = TK_INTEGER;
103787 tempX.flags = EP_IntValue|EP_TokenOnly;
103788 tempX.u.iValue = 0;
103789 ExprClearVVAProperties(&tempX);
103790 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
103791 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
103792 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
103793 testcase( regFree2==0 );
103794 }
103795 break;
103796 }
103797 case TK_BITNOT:
103798 case TK_NOT: {
103799 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
103800 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
103801 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103802 testcase( regFree1==0 );
103803 sqlite3VdbeAddOp2(v, op, r1, inReg);
103804 break;
103805 }
103806 case TK_TRUTH: {
103807 int isTrue; /* IS TRUE or IS NOT TRUE */
103808 int bNormal; /* IS TRUE or IS FALSE */
103809 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103810 testcase( regFree1==0 );
103811 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
103812 bNormal = pExpr->op2==TK_IS;
103813 testcase( isTrue && bNormal);
103814 testcase( !isTrue && bNormal);
103815 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
103816 break;
103817 }
103818 case TK_ISNULL:
103819 case TK_NOTNULL: {
103820 int addr;
103821 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
103822 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
103823 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
103824 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
103825 testcase( regFree1==0 );
103826 addr = sqlite3VdbeAddOp1(v, op, r1);
103827 VdbeCoverageIf(v, op==TK_ISNULL);
103828 VdbeCoverageIf(v, op==TK_NOTNULL);
103829 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
103830 sqlite3VdbeJumpHere(v, addr);
103831 break;
103832 }
103833 case TK_AGG_FUNCTION: {
103834 AggInfo *pInfo = pExpr->pAggInfo;
103835 if( pInfo==0
103836 || NEVER(pExpr->iAgg<0)
103837 || NEVER(pExpr->iAgg>=pInfo->nFunc)
103838 ){
103839 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103840 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
103841 }else{
103842 return pInfo->aFunc[pExpr->iAgg].iMem;
103843 }
103844 break;
103845 }
103846 case TK_FUNCTION: {
103847 ExprList *pFarg; /* List of function arguments */
103848 int nFarg; /* Number of function arguments */
103849 FuncDef *pDef; /* The function definition object */
103850 const char *zId; /* The function name */
103851 u32 constMask = 0; /* Mask of function arguments that are constant */
103852 int i; /* Loop counter */
103853 sqlite3 *db = pParse->db; /* The database connection */
103854 u8 enc = ENC(db); /* The text encoding used by this database */
103855 CollSeq *pColl = 0; /* A collating sequence */
103856
103857#ifndef SQLITE_OMIT_WINDOWFUNC
103858 if( ExprHasProperty(pExpr, EP_WinFunc) ){
103859 return pExpr->y.pWin->regResult;
103860 }
103861#endif
103862
103863 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
103864 /* SQL functions can be expensive. So try to avoid running them
103865 ** multiple times if we know they always give the same result */
103866 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
103867 }
103868 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
103869 assert( !ExprHasProperty(pExpr, EP_TokenOnly) );
103870 pFarg = pExpr->x.pList;
103871 nFarg = pFarg ? pFarg->nExpr : 0;
103872 assert( !ExprHasProperty(pExpr, EP_IntValue) );
103873 zId = pExpr->u.zToken;
103874 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
103875#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
103876 if( pDef==0 && pParse->explain ){
103877 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
103878 }
103879#endif
103880 if( pDef==0 || pDef->xFinalize!=0 ){
103881 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
103882 break;
103883 }
103884 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
103885 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
103886 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
103887 return exprCodeInlineFunction(pParse, pFarg,
103888 SQLITE_PTR_TO_INT(pDef->pUserData), target);
103889 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
103890 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
103891 }
103892
103893 for(i=0; i<nFarg; i++){
103894 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
103895 testcase( i==31 );
103896 constMask |= MASKBIT32(i);
103897 }
103898 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
103899 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
103900 }
103901 }
103902 if( pFarg ){
103903 if( constMask ){
103904 r1 = pParse->nMem+1;
103905 pParse->nMem += nFarg;
103906 }else{
103907 r1 = sqlite3GetTempRange(pParse, nFarg);
103908 }
103909
103910 /* For length() and typeof() functions with a column argument,
103911 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
103912 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
103913 ** loading.
103914 */
103915 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
103916 u8 exprOp;
103917 assert( nFarg==1 );
103918 assert( pFarg->a[0].pExpr!=0 );
103919 exprOp = pFarg->a[0].pExpr->op;
103920 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
103921 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
103922 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
103923 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
103924 pFarg->a[0].pExpr->op2 =
103925 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
103926 }
103927 }
103928
103929 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
103930 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
103931 }else{
103932 r1 = 0;
103933 }
103934#ifndef SQLITE_OMIT_VIRTUALTABLE
103935 /* Possibly overload the function if the first argument is
103936 ** a virtual table column.
103937 **
103938 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
103939 ** second argument, not the first, as the argument to test to
103940 ** see if it is a column in a virtual table. This is done because
103941 ** the left operand of infix functions (the operand we want to
103942 ** control overloading) ends up as the second argument to the
103943 ** function. The expression "A glob B" is equivalent to
103944 ** "glob(B,A). We want to use the A in "A glob B" to test
103945 ** for function overloading. But we use the B term in "glob(B,A)".
103946 */
103947 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
103948 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
103949 }else if( nFarg>0 ){
103950 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
103951 }
103952#endif
103953 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
103954 if( !pColl ) pColl = db->pDfltColl;
103955 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
103956 }
103957#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
103958 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
103959 Expr *pArg = pFarg->a[0].pExpr;
103960 if( pArg->op==TK_COLUMN ){
103961 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
103962 }else{
103963 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
103964 }
103965 }else
103966#endif
103967 {
103968 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
103969 pDef, pExpr->op2);
103970 }
103971 if( nFarg ){
103972 if( constMask==0 ){
103973 sqlite3ReleaseTempRange(pParse, r1, nFarg);
103974 }else{
103975 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
103976 }
103977 }
103978 return target;
103979 }
103980#ifndef SQLITE_OMIT_SUBQUERY
103981 case TK_EXISTS:
103982 case TK_SELECT: {
103983 int nCol;
103984 testcase( op==TK_EXISTS );
103985 testcase( op==TK_SELECT );
103986 if( pParse->db->mallocFailed ){
103987 return 0;
103988 }else if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
103989 sqlite3SubselectError(pParse, nCol, 1);
103990 }else{
103991 return sqlite3CodeSubselect(pParse, pExpr);
103992 }
103993 break;
103994 }
103995 case TK_SELECT_COLUMN: {
103996 int n;
103997 if( pExpr->pLeft->iTable==0 ){
103998 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
103999 }
104000 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
104001 if( pExpr->iTable!=0
104002 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
104003 ){
104004 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
104005 pExpr->iTable, n);
104006 }
104007 return pExpr->pLeft->iTable + pExpr->iColumn;
104008 }
104009 case TK_IN: {
104010 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
104011 int destIfNull = sqlite3VdbeMakeLabel(pParse);
104012 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
104013 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
104014 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
104015 sqlite3VdbeResolveLabel(v, destIfFalse);
104016 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
104017 sqlite3VdbeResolveLabel(v, destIfNull);
104018 return target;
104019 }
104020#endif /* SQLITE_OMIT_SUBQUERY */
104021
104022
104023 /*
104024 ** x BETWEEN y AND z
104025 **
104026 ** This is equivalent to
104027 **
104028 ** x>=y AND x<=z
104029 **
104030 ** X is stored in pExpr->pLeft.
104031 ** Y is stored in pExpr->pList->a[0].pExpr.
104032 ** Z is stored in pExpr->pList->a[1].pExpr.
104033 */
104034 case TK_BETWEEN: {
104035 exprCodeBetween(pParse, pExpr, target, 0, 0);
104036 return target;
104037 }
104038 case TK_SPAN:
104039 case TK_COLLATE:
104040 case TK_UPLUS: {
104041 pExpr = pExpr->pLeft;
104042 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
104043 }
104044
104045 case TK_TRIGGER: {
104046 /* If the opcode is TK_TRIGGER, then the expression is a reference
104047 ** to a column in the new.* or old.* pseudo-tables available to
104048 ** trigger programs. In this case Expr.iTable is set to 1 for the
104049 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
104050 ** is set to the column of the pseudo-table to read, or to -1 to
104051 ** read the rowid field.
104052 **
104053 ** The expression is implemented using an OP_Param opcode. The p1
104054 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
104055 ** to reference another column of the old.* pseudo-table, where
104056 ** i is the index of the column. For a new.rowid reference, p1 is
104057 ** set to (n+1), where n is the number of columns in each pseudo-table.
104058 ** For a reference to any other column in the new.* pseudo-table, p1
104059 ** is set to (n+2+i), where n and i are as defined previously. For
104060 ** example, if the table on which triggers are being fired is
104061 ** declared as:
104062 **
104063 ** CREATE TABLE t1(a, b);
104064 **
104065 ** Then p1 is interpreted as follows:
104066 **
104067 ** p1==0 -> old.rowid p1==3 -> new.rowid
104068 ** p1==1 -> old.a p1==4 -> new.a
104069 ** p1==2 -> old.b p1==5 -> new.b
104070 */
104071 Table *pTab = pExpr->y.pTab;
104072 int iCol = pExpr->iColumn;
104073 int p1 = pExpr->iTable * (pTab->nCol+1) + 1
104074 + sqlite3TableColumnToStorage(pTab, iCol);
104075
104076 assert( pExpr->iTable==0 || pExpr->iTable==1 );
104077 assert( iCol>=-1 && iCol<pTab->nCol );
104078 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
104079 assert( p1>=0 && p1<(pTab->nCol*2+2) );
104080
104081 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
104082 VdbeComment((v, "r[%d]=%s.%s", target,
104083 (pExpr->iTable ? "new" : "old"),
104084 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zName)
104085 ));
104086
104087#ifndef SQLITE_OMIT_FLOATING_POINT
104088 /* If the column has REAL affinity, it may currently be stored as an
104089 ** integer. Use OP_RealAffinity to make sure it is really real.
104090 **
104091 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
104092 ** floating point when extracting it from the record. */
104093 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
104094 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
104095 }
104096#endif
104097 break;
104098 }
104099
104100 case TK_VECTOR: {
104101 sqlite3ErrorMsg(pParse, "row value misused");
104102 break;
104103 }
104104
104105 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
104106 ** that derive from the right-hand table of a LEFT JOIN. The
104107 ** Expr.iTable value is the table number for the right-hand table.
104108 ** The expression is only evaluated if that table is not currently
104109 ** on a LEFT JOIN NULL row.
104110 */
104111 case TK_IF_NULL_ROW: {
104112 int addrINR;
104113 u8 okConstFactor = pParse->okConstFactor;
104114 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
104115 /* Temporarily disable factoring of constant expressions, since
104116 ** even though expressions may appear to be constant, they are not
104117 ** really constant because they originate from the right-hand side
104118 ** of a LEFT JOIN. */
104119 pParse->okConstFactor = 0;
104120 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
104121 pParse->okConstFactor = okConstFactor;
104122 sqlite3VdbeJumpHere(v, addrINR);
104123 sqlite3VdbeChangeP3(v, addrINR, inReg);
104124 break;
104125 }
104126
104127 /*
104128 ** Form A:
104129 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
104130 **
104131 ** Form B:
104132 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
104133 **
104134 ** Form A is can be transformed into the equivalent form B as follows:
104135 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
104136 ** WHEN x=eN THEN rN ELSE y END
104137 **
104138 ** X (if it exists) is in pExpr->pLeft.
104139 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
104140 ** odd. The Y is also optional. If the number of elements in x.pList
104141 ** is even, then Y is omitted and the "otherwise" result is NULL.
104142 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
104143 **
104144 ** The result of the expression is the Ri for the first matching Ei,
104145 ** or if there is no matching Ei, the ELSE term Y, or if there is
104146 ** no ELSE term, NULL.
104147 */
104148 case TK_CASE: {
104149 int endLabel; /* GOTO label for end of CASE stmt */
104150 int nextCase; /* GOTO label for next WHEN clause */
104151 int nExpr; /* 2x number of WHEN terms */
104152 int i; /* Loop counter */
104153 ExprList *pEList; /* List of WHEN terms */
104154 struct ExprList_item *aListelem; /* Array of WHEN terms */
104155 Expr opCompare; /* The X==Ei expression */
104156 Expr *pX; /* The X expression */
104157 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
104158 Expr *pDel = 0;
104159 sqlite3 *db = pParse->db;
104160
104161 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
104162 assert(pExpr->x.pList->nExpr > 0);
104163 pEList = pExpr->x.pList;
104164 aListelem = pEList->a;
104165 nExpr = pEList->nExpr;
104166 endLabel = sqlite3VdbeMakeLabel(pParse);
104167 if( (pX = pExpr->pLeft)!=0 ){
104168 pDel = sqlite3ExprDup(db, pX, 0);
104169 if( db->mallocFailed ){
104170 sqlite3ExprDelete(db, pDel);
104171 break;
104172 }
104173 testcase( pX->op==TK_COLUMN );
104174 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
104175 testcase( regFree1==0 );
104176 memset(&opCompare, 0, sizeof(opCompare));
104177 opCompare.op = TK_EQ;
104178 opCompare.pLeft = pDel;
104179 pTest = &opCompare;
104180 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
104181 ** The value in regFree1 might get SCopy-ed into the file result.
104182 ** So make sure that the regFree1 register is not reused for other
104183 ** purposes and possibly overwritten. */
104184 regFree1 = 0;
104185 }
104186 for(i=0; i<nExpr-1; i=i+2){
104187 if( pX ){
104188 assert( pTest!=0 );
104189 opCompare.pRight = aListelem[i].pExpr;
104190 }else{
104191 pTest = aListelem[i].pExpr;
104192 }
104193 nextCase = sqlite3VdbeMakeLabel(pParse);
104194 testcase( pTest->op==TK_COLUMN );
104195 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
104196 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
104197 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
104198 sqlite3VdbeGoto(v, endLabel);
104199 sqlite3VdbeResolveLabel(v, nextCase);
104200 }
104201 if( (nExpr&1)!=0 ){
104202 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
104203 }else{
104204 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
104205 }
104206 sqlite3ExprDelete(db, pDel);
104207 setDoNotMergeFlagOnCopy(v);
104208 sqlite3VdbeResolveLabel(v, endLabel);
104209 break;
104210 }
104211#ifndef SQLITE_OMIT_TRIGGER
104212 case TK_RAISE: {
104213 assert( pExpr->affExpr==OE_Rollback
104214 || pExpr->affExpr==OE_Abort
104215 || pExpr->affExpr==OE_Fail
104216 || pExpr->affExpr==OE_Ignore
104217 );
104218 if( !pParse->pTriggerTab && !pParse->nested ){
104219 sqlite3ErrorMsg(pParse,
104220 "RAISE() may only be used within a trigger-program");
104221 return 0;
104222 }
104223 if( pExpr->affExpr==OE_Abort ){
104224 sqlite3MayAbort(pParse);
104225 }
104226 assert( !ExprHasProperty(pExpr, EP_IntValue) );
104227 if( pExpr->affExpr==OE_Ignore ){
104228 sqlite3VdbeAddOp4(
104229 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
104230 VdbeCoverage(v);
104231 }else{
104232 sqlite3HaltConstraint(pParse,
104233 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
104234 pExpr->affExpr, pExpr->u.zToken, 0, 0);
104235 }
104236
104237 break;
104238 }
104239#endif
104240 }
104241 sqlite3ReleaseTempReg(pParse, regFree1);
104242 sqlite3ReleaseTempReg(pParse, regFree2);
104243 return inReg;
104244}
104245
104246/*
104247** Generate code that will evaluate expression pExpr just one time
104248** per prepared statement execution.
104249**
104250** If the expression uses functions (that might throw an exception) then
104251** guard them with an OP_Once opcode to ensure that the code is only executed
104252** once. If no functions are involved, then factor the code out and put it at
104253** the end of the prepared statement in the initialization section.
104254**
104255** If regDest>=0 then the result is always stored in that register and the
104256** result is not reusable. If regDest<0 then this routine is free to
104257** store the value whereever it wants. The register where the expression
104258** is stored is returned. When regDest<0, two identical expressions might
104259** code to the same register, if they do not contain function calls and hence
104260** are factored out into the initialization section at the end of the
104261** prepared statement.
104262*/
104263SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(
104264 Parse *pParse, /* Parsing context */
104265 Expr *pExpr, /* The expression to code when the VDBE initializes */
104266 int regDest /* Store the value in this register */
104267){
104268 ExprList *p;
104269 assert( ConstFactorOk(pParse) );
104270 p = pParse->pConstExpr;
104271 if( regDest<0 && p ){
104272 struct ExprList_item *pItem;
104273 int i;
104274 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
104275 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
104276 return pItem->u.iConstExprReg;
104277 }
104278 }
104279 }
104280 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
104281 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){
104282 Vdbe *v = pParse->pVdbe;
104283 int addr;
104284 assert( v );
104285 addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
104286 pParse->okConstFactor = 0;
104287 if( !pParse->db->mallocFailed ){
104288 if( regDest<0 ) regDest = ++pParse->nMem;
104289 sqlite3ExprCode(pParse, pExpr, regDest);
104290 }
104291 pParse->okConstFactor = 1;
104292 sqlite3ExprDelete(pParse->db, pExpr);
104293 sqlite3VdbeJumpHere(v, addr);
104294 }else{
104295 p = sqlite3ExprListAppend(pParse, p, pExpr);
104296 if( p ){
104297 struct ExprList_item *pItem = &p->a[p->nExpr-1];
104298 pItem->reusable = regDest<0;
104299 if( regDest<0 ) regDest = ++pParse->nMem;
104300 pItem->u.iConstExprReg = regDest;
104301 }
104302 pParse->pConstExpr = p;
104303 }
104304 return regDest;
104305}
104306
104307/*
104308** Generate code to evaluate an expression and store the results
104309** into a register. Return the register number where the results
104310** are stored.
104311**
104312** If the register is a temporary register that can be deallocated,
104313** then write its number into *pReg. If the result register is not
104314** a temporary, then set *pReg to zero.
104315**
104316** If pExpr is a constant, then this routine might generate this
104317** code to fill the register in the initialization section of the
104318** VDBE program, in order to factor it out of the evaluation loop.
104319*/
104320SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
104321 int r2;
104322 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
104323 if( ConstFactorOk(pParse)
104324 && pExpr->op!=TK_REGISTER
104325 && sqlite3ExprIsConstantNotJoin(pExpr)
104326 ){
104327 *pReg = 0;
104328 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
104329 }else{
104330 int r1 = sqlite3GetTempReg(pParse);
104331 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
104332 if( r2==r1 ){
104333 *pReg = r1;
104334 }else{
104335 sqlite3ReleaseTempReg(pParse, r1);
104336 *pReg = 0;
104337 }
104338 }
104339 return r2;
104340}
104341
104342/*
104343** Generate code that will evaluate expression pExpr and store the
104344** results in register target. The results are guaranteed to appear
104345** in register target.
104346*/
104347SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
104348 int inReg;
104349
104350 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) );
104351 assert( target>0 && target<=pParse->nMem );
104352 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
104353 if( pParse->pVdbe==0 ) return;
104354 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
104355 if( inReg!=target ){
104356 u8 op;
104357 if( ExprHasProperty(pExpr,EP_Subquery) ){
104358 op = OP_Copy;
104359 }else{
104360 op = OP_SCopy;
104361 }
104362 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
104363 }
104364}
104365
104366/*
104367** Make a transient copy of expression pExpr and then code it using
104368** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
104369** except that the input expression is guaranteed to be unchanged.
104370*/
104371SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
104372 sqlite3 *db = pParse->db;
104373 pExpr = sqlite3ExprDup(db, pExpr, 0);
104374 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
104375 sqlite3ExprDelete(db, pExpr);
104376}
104377
104378/*
104379** Generate code that will evaluate expression pExpr and store the
104380** results in register target. The results are guaranteed to appear
104381** in register target. If the expression is constant, then this routine
104382** might choose to code the expression at initialization time.
104383*/
104384SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
104385 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
104386 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
104387 }else{
104388 sqlite3ExprCodeCopy(pParse, pExpr, target);
104389 }
104390}
104391
104392/*
104393** Generate code that pushes the value of every element of the given
104394** expression list into a sequence of registers beginning at target.
104395**
104396** Return the number of elements evaluated. The number returned will
104397** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
104398** is defined.
104399**
104400** The SQLITE_ECEL_DUP flag prevents the arguments from being
104401** filled using OP_SCopy. OP_Copy must be used instead.
104402**
104403** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
104404** factored out into initialization code.
104405**
104406** The SQLITE_ECEL_REF flag means that expressions in the list with
104407** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
104408** in registers at srcReg, and so the value can be copied from there.
104409** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
104410** are simply omitted rather than being copied from srcReg.
104411*/
104412SQLITE_PRIVATE int sqlite3ExprCodeExprList(
104413 Parse *pParse, /* Parsing context */
104414 ExprList *pList, /* The expression list to be coded */
104415 int target, /* Where to write results */
104416 int srcReg, /* Source registers if SQLITE_ECEL_REF */
104417 u8 flags /* SQLITE_ECEL_* flags */
104418){
104419 struct ExprList_item *pItem;
104420 int i, j, n;
104421 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
104422 Vdbe *v = pParse->pVdbe;
104423 assert( pList!=0 );
104424 assert( target>0 );
104425 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
104426 n = pList->nExpr;
104427 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
104428 for(pItem=pList->a, i=0; i<n; i++, pItem++){
104429 Expr *pExpr = pItem->pExpr;
104430#ifdef SQLITE_ENABLE_SORTER_REFERENCES
104431 if( pItem->bSorterRef ){
104432 i--;
104433 n--;
104434 }else
104435#endif
104436 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
104437 if( flags & SQLITE_ECEL_OMITREF ){
104438 i--;
104439 n--;
104440 }else{
104441 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
104442 }
104443 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
104444 && sqlite3ExprIsConstantNotJoin(pExpr)
104445 ){
104446 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
104447 }else{
104448 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
104449 if( inReg!=target+i ){
104450 VdbeOp *pOp;
104451 if( copyOp==OP_Copy
104452 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
104453 && pOp->p1+pOp->p3+1==inReg
104454 && pOp->p2+pOp->p3+1==target+i
104455 && pOp->p5==0 /* The do-not-merge flag must be clear */
104456 ){
104457 pOp->p3++;
104458 }else{
104459 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
104460 }
104461 }
104462 }
104463 }
104464 return n;
104465}
104466
104467/*
104468** Generate code for a BETWEEN operator.
104469**
104470** x BETWEEN y AND z
104471**
104472** The above is equivalent to
104473**
104474** x>=y AND x<=z
104475**
104476** Code it as such, taking care to do the common subexpression
104477** elimination of x.
104478**
104479** The xJumpIf parameter determines details:
104480**
104481** NULL: Store the boolean result in reg[dest]
104482** sqlite3ExprIfTrue: Jump to dest if true
104483** sqlite3ExprIfFalse: Jump to dest if false
104484**
104485** The jumpIfNull parameter is ignored if xJumpIf is NULL.
104486*/
104487static void exprCodeBetween(
104488 Parse *pParse, /* Parsing and code generating context */
104489 Expr *pExpr, /* The BETWEEN expression */
104490 int dest, /* Jump destination or storage location */
104491 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
104492 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
104493){
104494 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
104495 Expr compLeft; /* The x>=y term */
104496 Expr compRight; /* The x<=z term */
104497 int regFree1 = 0; /* Temporary use register */
104498 Expr *pDel = 0;
104499 sqlite3 *db = pParse->db;
104500
104501 memset(&compLeft, 0, sizeof(Expr));
104502 memset(&compRight, 0, sizeof(Expr));
104503 memset(&exprAnd, 0, sizeof(Expr));
104504
104505 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
104506 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
104507 if( db->mallocFailed==0 ){
104508 exprAnd.op = TK_AND;
104509 exprAnd.pLeft = &compLeft;
104510 exprAnd.pRight = &compRight;
104511 compLeft.op = TK_GE;
104512 compLeft.pLeft = pDel;
104513 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
104514 compRight.op = TK_LE;
104515 compRight.pLeft = pDel;
104516 compRight.pRight = pExpr->x.pList->a[1].pExpr;
104517 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
104518 if( xJump ){
104519 xJump(pParse, &exprAnd, dest, jumpIfNull);
104520 }else{
104521 /* Mark the expression is being from the ON or USING clause of a join
104522 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
104523 ** it into the Parse.pConstExpr list. We should use a new bit for this,
104524 ** for clarity, but we are out of bits in the Expr.flags field so we
104525 ** have to reuse the EP_FromJoin bit. Bummer. */
104526 pDel->flags |= EP_FromJoin;
104527 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
104528 }
104529 sqlite3ReleaseTempReg(pParse, regFree1);
104530 }
104531 sqlite3ExprDelete(db, pDel);
104532
104533 /* Ensure adequate test coverage */
104534 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
104535 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
104536 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
104537 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
104538 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
104539 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
104540 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
104541 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
104542 testcase( xJump==0 );
104543}
104544
104545/*
104546** Generate code for a boolean expression such that a jump is made
104547** to the label "dest" if the expression is true but execution
104548** continues straight thru if the expression is false.
104549**
104550** If the expression evaluates to NULL (neither true nor false), then
104551** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
104552**
104553** This code depends on the fact that certain token values (ex: TK_EQ)
104554** are the same as opcode values (ex: OP_Eq) that implement the corresponding
104555** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
104556** the make process cause these values to align. Assert()s in the code
104557** below verify that the numbers are aligned correctly.
104558*/
104559SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
104560 Vdbe *v = pParse->pVdbe;
104561 int op = 0;
104562 int regFree1 = 0;
104563 int regFree2 = 0;
104564 int r1, r2;
104565
104566 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
104567 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
104568 if( NEVER(pExpr==0) ) return; /* No way this can happen */
104569 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) );
104570 op = pExpr->op;
104571 switch( op ){
104572 case TK_AND:
104573 case TK_OR: {
104574 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
104575 if( pAlt!=pExpr ){
104576 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
104577 }else if( op==TK_AND ){
104578 int d2 = sqlite3VdbeMakeLabel(pParse);
104579 testcase( jumpIfNull==0 );
104580 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
104581 jumpIfNull^SQLITE_JUMPIFNULL);
104582 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
104583 sqlite3VdbeResolveLabel(v, d2);
104584 }else{
104585 testcase( jumpIfNull==0 );
104586 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
104587 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
104588 }
104589 break;
104590 }
104591 case TK_NOT: {
104592 testcase( jumpIfNull==0 );
104593 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
104594 break;
104595 }
104596 case TK_TRUTH: {
104597 int isNot; /* IS NOT TRUE or IS NOT FALSE */
104598 int isTrue; /* IS TRUE or IS NOT TRUE */
104599 testcase( jumpIfNull==0 );
104600 isNot = pExpr->op2==TK_ISNOT;
104601 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
104602 testcase( isTrue && isNot );
104603 testcase( !isTrue && isNot );
104604 if( isTrue ^ isNot ){
104605 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
104606 isNot ? SQLITE_JUMPIFNULL : 0);
104607 }else{
104608 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
104609 isNot ? SQLITE_JUMPIFNULL : 0);
104610 }
104611 break;
104612 }
104613 case TK_IS:
104614 case TK_ISNOT:
104615 testcase( op==TK_IS );
104616 testcase( op==TK_ISNOT );
104617 op = (op==TK_IS) ? TK_EQ : TK_NE;
104618 jumpIfNull = SQLITE_NULLEQ;
104619 /* no break */ deliberate_fall_through
104620 case TK_LT:
104621 case TK_LE:
104622 case TK_GT:
104623 case TK_GE:
104624 case TK_NE:
104625 case TK_EQ: {
104626 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
104627 testcase( jumpIfNull==0 );
104628 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104629 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104630 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
104631 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted));
104632 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
104633 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
104634 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
104635 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
104636 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
104637 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
104638 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
104639 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
104640 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
104641 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
104642 testcase( regFree1==0 );
104643 testcase( regFree2==0 );
104644 break;
104645 }
104646 case TK_ISNULL:
104647 case TK_NOTNULL: {
104648 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
104649 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
104650 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104651 sqlite3VdbeAddOp2(v, op, r1, dest);
104652 VdbeCoverageIf(v, op==TK_ISNULL);
104653 VdbeCoverageIf(v, op==TK_NOTNULL);
104654 testcase( regFree1==0 );
104655 break;
104656 }
104657 case TK_BETWEEN: {
104658 testcase( jumpIfNull==0 );
104659 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
104660 break;
104661 }
104662#ifndef SQLITE_OMIT_SUBQUERY
104663 case TK_IN: {
104664 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
104665 int destIfNull = jumpIfNull ? dest : destIfFalse;
104666 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
104667 sqlite3VdbeGoto(v, dest);
104668 sqlite3VdbeResolveLabel(v, destIfFalse);
104669 break;
104670 }
104671#endif
104672 default: {
104673 default_expr:
104674 if( ExprAlwaysTrue(pExpr) ){
104675 sqlite3VdbeGoto(v, dest);
104676 }else if( ExprAlwaysFalse(pExpr) ){
104677 /* No-op */
104678 }else{
104679 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
104680 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
104681 VdbeCoverage(v);
104682 testcase( regFree1==0 );
104683 testcase( jumpIfNull==0 );
104684 }
104685 break;
104686 }
104687 }
104688 sqlite3ReleaseTempReg(pParse, regFree1);
104689 sqlite3ReleaseTempReg(pParse, regFree2);
104690}
104691
104692/*
104693** Generate code for a boolean expression such that a jump is made
104694** to the label "dest" if the expression is false but execution
104695** continues straight thru if the expression is true.
104696**
104697** If the expression evaluates to NULL (neither true nor false) then
104698** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
104699** is 0.
104700*/
104701SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
104702 Vdbe *v = pParse->pVdbe;
104703 int op = 0;
104704 int regFree1 = 0;
104705 int regFree2 = 0;
104706 int r1, r2;
104707
104708 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
104709 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
104710 if( pExpr==0 ) return;
104711 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
104712
104713 /* The value of pExpr->op and op are related as follows:
104714 **
104715 ** pExpr->op op
104716 ** --------- ----------
104717 ** TK_ISNULL OP_NotNull
104718 ** TK_NOTNULL OP_IsNull
104719 ** TK_NE OP_Eq
104720 ** TK_EQ OP_Ne
104721 ** TK_GT OP_Le
104722 ** TK_LE OP_Gt
104723 ** TK_GE OP_Lt
104724 ** TK_LT OP_Ge
104725 **
104726 ** For other values of pExpr->op, op is undefined and unused.
104727 ** The value of TK_ and OP_ constants are arranged such that we
104728 ** can compute the mapping above using the following expression.
104729 ** Assert()s verify that the computation is correct.
104730 */
104731 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
104732
104733 /* Verify correct alignment of TK_ and OP_ constants
104734 */
104735 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
104736 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
104737 assert( pExpr->op!=TK_NE || op==OP_Eq );
104738 assert( pExpr->op!=TK_EQ || op==OP_Ne );
104739 assert( pExpr->op!=TK_LT || op==OP_Ge );
104740 assert( pExpr->op!=TK_LE || op==OP_Gt );
104741 assert( pExpr->op!=TK_GT || op==OP_Le );
104742 assert( pExpr->op!=TK_GE || op==OP_Lt );
104743
104744 switch( pExpr->op ){
104745 case TK_AND:
104746 case TK_OR: {
104747 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
104748 if( pAlt!=pExpr ){
104749 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
104750 }else if( pExpr->op==TK_AND ){
104751 testcase( jumpIfNull==0 );
104752 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
104753 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
104754 }else{
104755 int d2 = sqlite3VdbeMakeLabel(pParse);
104756 testcase( jumpIfNull==0 );
104757 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
104758 jumpIfNull^SQLITE_JUMPIFNULL);
104759 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
104760 sqlite3VdbeResolveLabel(v, d2);
104761 }
104762 break;
104763 }
104764 case TK_NOT: {
104765 testcase( jumpIfNull==0 );
104766 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
104767 break;
104768 }
104769 case TK_TRUTH: {
104770 int isNot; /* IS NOT TRUE or IS NOT FALSE */
104771 int isTrue; /* IS TRUE or IS NOT TRUE */
104772 testcase( jumpIfNull==0 );
104773 isNot = pExpr->op2==TK_ISNOT;
104774 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
104775 testcase( isTrue && isNot );
104776 testcase( !isTrue && isNot );
104777 if( isTrue ^ isNot ){
104778 /* IS TRUE and IS NOT FALSE */
104779 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
104780 isNot ? 0 : SQLITE_JUMPIFNULL);
104781
104782 }else{
104783 /* IS FALSE and IS NOT TRUE */
104784 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
104785 isNot ? 0 : SQLITE_JUMPIFNULL);
104786 }
104787 break;
104788 }
104789 case TK_IS:
104790 case TK_ISNOT:
104791 testcase( pExpr->op==TK_IS );
104792 testcase( pExpr->op==TK_ISNOT );
104793 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
104794 jumpIfNull = SQLITE_NULLEQ;
104795 /* no break */ deliberate_fall_through
104796 case TK_LT:
104797 case TK_LE:
104798 case TK_GT:
104799 case TK_GE:
104800 case TK_NE:
104801 case TK_EQ: {
104802 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
104803 testcase( jumpIfNull==0 );
104804 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104805 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104806 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
104807 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted));
104808 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
104809 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
104810 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
104811 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
104812 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
104813 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
104814 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
104815 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
104816 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
104817 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
104818 testcase( regFree1==0 );
104819 testcase( regFree2==0 );
104820 break;
104821 }
104822 case TK_ISNULL:
104823 case TK_NOTNULL: {
104824 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104825 sqlite3VdbeAddOp2(v, op, r1, dest);
104826 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
104827 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
104828 testcase( regFree1==0 );
104829 break;
104830 }
104831 case TK_BETWEEN: {
104832 testcase( jumpIfNull==0 );
104833 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
104834 break;
104835 }
104836#ifndef SQLITE_OMIT_SUBQUERY
104837 case TK_IN: {
104838 if( jumpIfNull ){
104839 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
104840 }else{
104841 int destIfNull = sqlite3VdbeMakeLabel(pParse);
104842 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
104843 sqlite3VdbeResolveLabel(v, destIfNull);
104844 }
104845 break;
104846 }
104847#endif
104848 default: {
104849 default_expr:
104850 if( ExprAlwaysFalse(pExpr) ){
104851 sqlite3VdbeGoto(v, dest);
104852 }else if( ExprAlwaysTrue(pExpr) ){
104853 /* no-op */
104854 }else{
104855 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
104856 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
104857 VdbeCoverage(v);
104858 testcase( regFree1==0 );
104859 testcase( jumpIfNull==0 );
104860 }
104861 break;
104862 }
104863 }
104864 sqlite3ReleaseTempReg(pParse, regFree1);
104865 sqlite3ReleaseTempReg(pParse, regFree2);
104866}
104867
104868/*
104869** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
104870** code generation, and that copy is deleted after code generation. This
104871** ensures that the original pExpr is unchanged.
104872*/
104873SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
104874 sqlite3 *db = pParse->db;
104875 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
104876 if( db->mallocFailed==0 ){
104877 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
104878 }
104879 sqlite3ExprDelete(db, pCopy);
104880}
104881
104882/*
104883** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
104884** type of expression.
104885**
104886** If pExpr is a simple SQL value - an integer, real, string, blob
104887** or NULL value - then the VDBE currently being prepared is configured
104888** to re-prepare each time a new value is bound to variable pVar.
104889**
104890** Additionally, if pExpr is a simple SQL value and the value is the
104891** same as that currently bound to variable pVar, non-zero is returned.
104892** Otherwise, if the values are not the same or if pExpr is not a simple
104893** SQL value, zero is returned.
104894*/
104895static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
104896 int res = 0;
104897 int iVar;
104898 sqlite3_value *pL, *pR = 0;
104899
104900 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
104901 if( pR ){
104902 iVar = pVar->iColumn;
104903 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
104904 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
104905 if( pL ){
104906 if( sqlite3_value_type(pL)==SQLITE_TEXT ){
104907 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
104908 }
104909 res = 0==sqlite3MemCompare(pL, pR, 0);
104910 }
104911 sqlite3ValueFree(pR);
104912 sqlite3ValueFree(pL);
104913 }
104914
104915 return res;
104916}
104917
104918/*
104919** Do a deep comparison of two expression trees. Return 0 if the two
104920** expressions are completely identical. Return 1 if they differ only
104921** by a COLLATE operator at the top level. Return 2 if there are differences
104922** other than the top-level COLLATE operator.
104923**
104924** If any subelement of pB has Expr.iTable==(-1) then it is allowed
104925** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
104926**
104927** The pA side might be using TK_REGISTER. If that is the case and pB is
104928** not using TK_REGISTER but is otherwise equivalent, then still return 0.
104929**
104930** Sometimes this routine will return 2 even if the two expressions
104931** really are equivalent. If we cannot prove that the expressions are
104932** identical, we return 2 just to be safe. So if this routine
104933** returns 2, then you do not really know for certain if the two
104934** expressions are the same. But if you get a 0 or 1 return, then you
104935** can be sure the expressions are the same. In the places where
104936** this routine is used, it does not hurt to get an extra 2 - that
104937** just might result in some slightly slower code. But returning
104938** an incorrect 0 or 1 could lead to a malfunction.
104939**
104940** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
104941** pParse->pReprepare can be matched against literals in pB. The
104942** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
104943** If pParse is NULL (the normal case) then any TK_VARIABLE term in
104944** Argument pParse should normally be NULL. If it is not NULL and pA or
104945** pB causes a return value of 2.
104946*/
104947SQLITE_PRIVATE int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
104948 u32 combinedFlags;
104949 if( pA==0 || pB==0 ){
104950 return pB==pA ? 0 : 2;
104951 }
104952 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
104953 return 0;
104954 }
104955 combinedFlags = pA->flags | pB->flags;
104956 if( combinedFlags & EP_IntValue ){
104957 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
104958 return 0;
104959 }
104960 return 2;
104961 }
104962 if( pA->op!=pB->op || pA->op==TK_RAISE ){
104963 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
104964 return 1;
104965 }
104966 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
104967 return 1;
104968 }
104969 return 2;
104970 }
104971 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
104972 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
104973 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
104974#ifndef SQLITE_OMIT_WINDOWFUNC
104975 assert( pA->op==pB->op );
104976 if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
104977 return 2;
104978 }
104979 if( ExprHasProperty(pA,EP_WinFunc) ){
104980 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
104981 return 2;
104982 }
104983 }
104984#endif
104985 }else if( pA->op==TK_NULL ){
104986 return 0;
104987 }else if( pA->op==TK_COLLATE ){
104988 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
104989 }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
104990 return 2;
104991 }
104992 }
104993 if( (pA->flags & (EP_Distinct|EP_Commuted))
104994 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
104995 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
104996 if( combinedFlags & EP_xIsSelect ) return 2;
104997 if( (combinedFlags & EP_FixedCol)==0
104998 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
104999 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
105000 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
105001 if( pA->op!=TK_STRING
105002 && pA->op!=TK_TRUEFALSE
105003 && ALWAYS((combinedFlags & EP_Reduced)==0)
105004 ){
105005 if( pA->iColumn!=pB->iColumn ) return 2;
105006 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
105007 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
105008 return 2;
105009 }
105010 }
105011 }
105012 return 0;
105013}
105014
105015/*
105016** Compare two ExprList objects. Return 0 if they are identical, 1
105017** if they are certainly different, or 2 if it is not possible to
105018** determine if they are identical or not.
105019**
105020** If any subelement of pB has Expr.iTable==(-1) then it is allowed
105021** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
105022**
105023** This routine might return non-zero for equivalent ExprLists. The
105024** only consequence will be disabled optimizations. But this routine
105025** must never return 0 if the two ExprList objects are different, or
105026** a malfunction will result.
105027**
105028** Two NULL pointers are considered to be the same. But a NULL pointer
105029** always differs from a non-NULL pointer.
105030*/
105031SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
105032 int i;
105033 if( pA==0 && pB==0 ) return 0;
105034 if( pA==0 || pB==0 ) return 1;
105035 if( pA->nExpr!=pB->nExpr ) return 1;
105036 for(i=0; i<pA->nExpr; i++){
105037 int res;
105038 Expr *pExprA = pA->a[i].pExpr;
105039 Expr *pExprB = pB->a[i].pExpr;
105040 if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
105041 if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res;
105042 }
105043 return 0;
105044}
105045
105046/*
105047** Like sqlite3ExprCompare() except COLLATE operators at the top-level
105048** are ignored.
105049*/
105050SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
105051 return sqlite3ExprCompare(0,
105052 sqlite3ExprSkipCollateAndLikely(pA),
105053 sqlite3ExprSkipCollateAndLikely(pB),
105054 iTab);
105055}
105056
105057/*
105058** Return non-zero if Expr p can only be true if pNN is not NULL.
105059**
105060** Or if seenNot is true, return non-zero if Expr p can only be
105061** non-NULL if pNN is not NULL
105062*/
105063static int exprImpliesNotNull(
105064 Parse *pParse, /* Parsing context */
105065 Expr *p, /* The expression to be checked */
105066 Expr *pNN, /* The expression that is NOT NULL */
105067 int iTab, /* Table being evaluated */
105068 int seenNot /* Return true only if p can be any non-NULL value */
105069){
105070 assert( p );
105071 assert( pNN );
105072 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
105073 return pNN->op!=TK_NULL;
105074 }
105075 switch( p->op ){
105076 case TK_IN: {
105077 if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
105078 assert( ExprHasProperty(p,EP_xIsSelect)
105079 || (p->x.pList!=0 && p->x.pList->nExpr>0) );
105080 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
105081 }
105082 case TK_BETWEEN: {
105083 ExprList *pList = p->x.pList;
105084 assert( pList!=0 );
105085 assert( pList->nExpr==2 );
105086 if( seenNot ) return 0;
105087 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
105088 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
105089 ){
105090 return 1;
105091 }
105092 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
105093 }
105094 case TK_EQ:
105095 case TK_NE:
105096 case TK_LT:
105097 case TK_LE:
105098 case TK_GT:
105099 case TK_GE:
105100 case TK_PLUS:
105101 case TK_MINUS:
105102 case TK_BITOR:
105103 case TK_LSHIFT:
105104 case TK_RSHIFT:
105105 case TK_CONCAT:
105106 seenNot = 1;
105107 /* no break */ deliberate_fall_through
105108 case TK_STAR:
105109 case TK_REM:
105110 case TK_BITAND:
105111 case TK_SLASH: {
105112 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
105113 /* no break */ deliberate_fall_through
105114 }
105115 case TK_SPAN:
105116 case TK_COLLATE:
105117 case TK_UPLUS:
105118 case TK_UMINUS: {
105119 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
105120 }
105121 case TK_TRUTH: {
105122 if( seenNot ) return 0;
105123 if( p->op2!=TK_IS ) return 0;
105124 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
105125 }
105126 case TK_BITNOT:
105127 case TK_NOT: {
105128 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
105129 }
105130 }
105131 return 0;
105132}
105133
105134/*
105135** Return true if we can prove the pE2 will always be true if pE1 is
105136** true. Return false if we cannot complete the proof or if pE2 might
105137** be false. Examples:
105138**
105139** pE1: x==5 pE2: x==5 Result: true
105140** pE1: x>0 pE2: x==5 Result: false
105141** pE1: x=21 pE2: x=21 OR y=43 Result: true
105142** pE1: x!=123 pE2: x IS NOT NULL Result: true
105143** pE1: x!=?1 pE2: x IS NOT NULL Result: true
105144** pE1: x IS NULL pE2: x IS NOT NULL Result: false
105145** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
105146**
105147** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
105148** Expr.iTable<0 then assume a table number given by iTab.
105149**
105150** If pParse is not NULL, then the values of bound variables in pE1 are
105151** compared against literal values in pE2 and pParse->pVdbe->expmask is
105152** modified to record which bound variables are referenced. If pParse
105153** is NULL, then false will be returned if pE1 contains any bound variables.
105154**
105155** When in doubt, return false. Returning true might give a performance
105156** improvement. Returning false might cause a performance reduction, but
105157** it will always give the correct answer and is hence always safe.
105158*/
105159SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
105160 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
105161 return 1;
105162 }
105163 if( pE2->op==TK_OR
105164 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
105165 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
105166 ){
105167 return 1;
105168 }
105169 if( pE2->op==TK_NOTNULL
105170 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
105171 ){
105172 return 1;
105173 }
105174 return 0;
105175}
105176
105177/*
105178** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
105179** If the expression node requires that the table at pWalker->iCur
105180** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
105181**
105182** This routine controls an optimization. False positives (setting
105183** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
105184** (never setting pWalker->eCode) is a harmless missed optimization.
105185*/
105186static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
105187 testcase( pExpr->op==TK_AGG_COLUMN );
105188 testcase( pExpr->op==TK_AGG_FUNCTION );
105189 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
105190 switch( pExpr->op ){
105191 case TK_ISNOT:
105192 case TK_ISNULL:
105193 case TK_NOTNULL:
105194 case TK_IS:
105195 case TK_OR:
105196 case TK_VECTOR:
105197 case TK_CASE:
105198 case TK_IN:
105199 case TK_FUNCTION:
105200 case TK_TRUTH:
105201 testcase( pExpr->op==TK_ISNOT );
105202 testcase( pExpr->op==TK_ISNULL );
105203 testcase( pExpr->op==TK_NOTNULL );
105204 testcase( pExpr->op==TK_IS );
105205 testcase( pExpr->op==TK_OR );
105206 testcase( pExpr->op==TK_VECTOR );
105207 testcase( pExpr->op==TK_CASE );
105208 testcase( pExpr->op==TK_IN );
105209 testcase( pExpr->op==TK_FUNCTION );
105210 testcase( pExpr->op==TK_TRUTH );
105211 return WRC_Prune;
105212 case TK_COLUMN:
105213 if( pWalker->u.iCur==pExpr->iTable ){
105214 pWalker->eCode = 1;
105215 return WRC_Abort;
105216 }
105217 return WRC_Prune;
105218
105219 case TK_AND:
105220 if( pWalker->eCode==0 ){
105221 sqlite3WalkExpr(pWalker, pExpr->pLeft);
105222 if( pWalker->eCode ){
105223 pWalker->eCode = 0;
105224 sqlite3WalkExpr(pWalker, pExpr->pRight);
105225 }
105226 }
105227 return WRC_Prune;
105228
105229 case TK_BETWEEN:
105230 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
105231 assert( pWalker->eCode );
105232 return WRC_Abort;
105233 }
105234 return WRC_Prune;
105235
105236 /* Virtual tables are allowed to use constraints like x=NULL. So
105237 ** a term of the form x=y does not prove that y is not null if x
105238 ** is the column of a virtual table */
105239 case TK_EQ:
105240 case TK_NE:
105241 case TK_LT:
105242 case TK_LE:
105243 case TK_GT:
105244 case TK_GE: {
105245 Expr *pLeft = pExpr->pLeft;
105246 Expr *pRight = pExpr->pRight;
105247 testcase( pExpr->op==TK_EQ );
105248 testcase( pExpr->op==TK_NE );
105249 testcase( pExpr->op==TK_LT );
105250 testcase( pExpr->op==TK_LE );
105251 testcase( pExpr->op==TK_GT );
105252 testcase( pExpr->op==TK_GE );
105253 /* The y.pTab=0 assignment in wherecode.c always happens after the
105254 ** impliesNotNullRow() test */
105255 if( (pLeft->op==TK_COLUMN && ALWAYS(pLeft->y.pTab!=0)
105256 && IsVirtual(pLeft->y.pTab))
105257 || (pRight->op==TK_COLUMN && ALWAYS(pRight->y.pTab!=0)
105258 && IsVirtual(pRight->y.pTab))
105259 ){
105260 return WRC_Prune;
105261 }
105262 /* no break */ deliberate_fall_through
105263 }
105264 default:
105265 return WRC_Continue;
105266 }
105267}
105268
105269/*
105270** Return true (non-zero) if expression p can only be true if at least
105271** one column of table iTab is non-null. In other words, return true
105272** if expression p will always be NULL or false if every column of iTab
105273** is NULL.
105274**
105275** False negatives are acceptable. In other words, it is ok to return
105276** zero even if expression p will never be true of every column of iTab
105277** is NULL. A false negative is merely a missed optimization opportunity.
105278**
105279** False positives are not allowed, however. A false positive may result
105280** in an incorrect answer.
105281**
105282** Terms of p that are marked with EP_FromJoin (and hence that come from
105283** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
105284**
105285** This routine is used to check if a LEFT JOIN can be converted into
105286** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
105287** clause requires that some column of the right table of the LEFT JOIN
105288** be non-NULL, then the LEFT JOIN can be safely converted into an
105289** ordinary join.
105290*/
105291SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
105292 Walker w;
105293 p = sqlite3ExprSkipCollateAndLikely(p);
105294 if( p==0 ) return 0;
105295 if( p->op==TK_NOTNULL ){
105296 p = p->pLeft;
105297 }else{
105298 while( p->op==TK_AND ){
105299 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
105300 p = p->pRight;
105301 }
105302 }
105303 w.xExprCallback = impliesNotNullRow;
105304 w.xSelectCallback = 0;
105305 w.xSelectCallback2 = 0;
105306 w.eCode = 0;
105307 w.u.iCur = iTab;
105308 sqlite3WalkExpr(&w, p);
105309 return w.eCode;
105310}
105311
105312/*
105313** An instance of the following structure is used by the tree walker
105314** to determine if an expression can be evaluated by reference to the
105315** index only, without having to do a search for the corresponding
105316** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
105317** is the cursor for the table.
105318*/
105319struct IdxCover {
105320 Index *pIdx; /* The index to be tested for coverage */
105321 int iCur; /* Cursor number for the table corresponding to the index */
105322};
105323
105324/*
105325** Check to see if there are references to columns in table
105326** pWalker->u.pIdxCover->iCur can be satisfied using the index
105327** pWalker->u.pIdxCover->pIdx.
105328*/
105329static int exprIdxCover(Walker *pWalker, Expr *pExpr){
105330 if( pExpr->op==TK_COLUMN
105331 && pExpr->iTable==pWalker->u.pIdxCover->iCur
105332 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
105333 ){
105334 pWalker->eCode = 1;
105335 return WRC_Abort;
105336 }
105337 return WRC_Continue;
105338}
105339
105340/*
105341** Determine if an index pIdx on table with cursor iCur contains will
105342** the expression pExpr. Return true if the index does cover the
105343** expression and false if the pExpr expression references table columns
105344** that are not found in the index pIdx.
105345**
105346** An index covering an expression means that the expression can be
105347** evaluated using only the index and without having to lookup the
105348** corresponding table entry.
105349*/
105350SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
105351 Expr *pExpr, /* The index to be tested */
105352 int iCur, /* The cursor number for the corresponding table */
105353 Index *pIdx /* The index that might be used for coverage */
105354){
105355 Walker w;
105356 struct IdxCover xcov;
105357 memset(&w, 0, sizeof(w));
105358 xcov.iCur = iCur;
105359 xcov.pIdx = pIdx;
105360 w.xExprCallback = exprIdxCover;
105361 w.u.pIdxCover = &xcov;
105362 sqlite3WalkExpr(&w, pExpr);
105363 return !w.eCode;
105364}
105365
105366
105367/*
105368** An instance of the following structure is used by the tree walker
105369** to count references to table columns in the arguments of an
105370** aggregate function, in order to implement the
105371** sqlite3FunctionThisSrc() routine.
105372*/
105373struct SrcCount {
105374 SrcList *pSrc; /* One particular FROM clause in a nested query */
105375 int iSrcInner; /* Smallest cursor number in this context */
105376 int nThis; /* Number of references to columns in pSrcList */
105377 int nOther; /* Number of references to columns in other FROM clauses */
105378};
105379
105380/*
105381** xSelect callback for sqlite3FunctionUsesThisSrc(). If this is the first
105382** SELECT with a FROM clause encountered during this iteration, set
105383** SrcCount.iSrcInner to the cursor number of the leftmost object in
105384** the FROM cause.
105385*/
105386static int selectSrcCount(Walker *pWalker, Select *pSel){
105387 struct SrcCount *p = pWalker->u.pSrcCount;
105388 if( p->iSrcInner==0x7FFFFFFF && ALWAYS(pSel->pSrc) && pSel->pSrc->nSrc ){
105389 pWalker->u.pSrcCount->iSrcInner = pSel->pSrc->a[0].iCursor;
105390 }
105391 return WRC_Continue;
105392}
105393
105394/*
105395** Count the number of references to columns.
105396*/
105397static int exprSrcCount(Walker *pWalker, Expr *pExpr){
105398 /* There was once a NEVER() on the second term on the grounds that
105399 ** sqlite3FunctionUsesThisSrc() was always called before
105400 ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
105401 ** been converted into TK_AGG_COLUMN. But this is no longer true due
105402 ** to window functions - sqlite3WindowRewrite() may now indirectly call
105403 ** FunctionUsesThisSrc() when creating a new sub-select. */
105404 if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
105405 int i;
105406 struct SrcCount *p = pWalker->u.pSrcCount;
105407 SrcList *pSrc = p->pSrc;
105408 int nSrc = pSrc ? pSrc->nSrc : 0;
105409 for(i=0; i<nSrc; i++){
105410 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
105411 }
105412 if( i<nSrc ){
105413 p->nThis++;
105414 }else if( pExpr->iTable<p->iSrcInner ){
105415 /* In a well-formed parse tree (no name resolution errors),
105416 ** TK_COLUMN nodes with smaller Expr.iTable values are in an
105417 ** outer context. Those are the only ones to count as "other" */
105418 p->nOther++;
105419 }
105420 }
105421 return WRC_Continue;
105422}
105423
105424/*
105425** Determine if any of the arguments to the pExpr Function reference
105426** pSrcList. Return true if they do. Also return true if the function
105427** has no arguments or has only constant arguments. Return false if pExpr
105428** references columns but not columns of tables found in pSrcList.
105429*/
105430SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
105431 Walker w;
105432 struct SrcCount cnt;
105433 assert( pExpr->op==TK_AGG_FUNCTION );
105434 memset(&w, 0, sizeof(w));
105435 w.xExprCallback = exprSrcCount;
105436 w.xSelectCallback = selectSrcCount;
105437 w.u.pSrcCount = &cnt;
105438 cnt.pSrc = pSrcList;
105439 cnt.iSrcInner = (pSrcList&&pSrcList->nSrc)?pSrcList->a[0].iCursor:0x7FFFFFFF;
105440 cnt.nThis = 0;
105441 cnt.nOther = 0;
105442 sqlite3WalkExprList(&w, pExpr->x.pList);
105443#ifndef SQLITE_OMIT_WINDOWFUNC
105444 if( ExprHasProperty(pExpr, EP_WinFunc) ){
105445 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
105446 }
105447#endif
105448 return cnt.nThis>0 || cnt.nOther==0;
105449}
105450
105451/*
105452** This is a Walker expression node callback.
105453**
105454** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
105455** object that is referenced does not refer directly to the Expr. If
105456** it does, make a copy. This is done because the pExpr argument is
105457** subject to change.
105458**
105459** The copy is stored on pParse->pConstExpr with a register number of 0.
105460** This will cause the expression to be deleted automatically when the
105461** Parse object is destroyed, but the zero register number means that it
105462** will not generate any code in the preamble.
105463*/
105464static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
105465 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))
105466 && pExpr->pAggInfo!=0
105467 ){
105468 AggInfo *pAggInfo = pExpr->pAggInfo;
105469 int iAgg = pExpr->iAgg;
105470 Parse *pParse = pWalker->pParse;
105471 sqlite3 *db = pParse->db;
105472 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
105473 if( pExpr->op==TK_AGG_COLUMN ){
105474 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
105475 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
105476 pExpr = sqlite3ExprDup(db, pExpr, 0);
105477 if( pExpr ){
105478 pAggInfo->aCol[iAgg].pCExpr = pExpr;
105479 pParse->pConstExpr =
105480 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
105481 }
105482 }
105483 }else{
105484 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
105485 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
105486 pExpr = sqlite3ExprDup(db, pExpr, 0);
105487 if( pExpr ){
105488 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
105489 pParse->pConstExpr =
105490 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
105491 }
105492 }
105493 }
105494 }
105495 return WRC_Continue;
105496}
105497
105498/*
105499** Initialize a Walker object so that will persist AggInfo entries referenced
105500** by the tree that is walked.
105501*/
105502SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
105503 memset(pWalker, 0, sizeof(*pWalker));
105504 pWalker->pParse = pParse;
105505 pWalker->xExprCallback = agginfoPersistExprCb;
105506 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
105507}
105508
105509/*
105510** Add a new element to the pAggInfo->aCol[] array. Return the index of
105511** the new element. Return a negative number if malloc fails.
105512*/
105513static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
105514 int i;
105515 pInfo->aCol = sqlite3ArrayAllocate(
105516 db,
105517 pInfo->aCol,
105518 sizeof(pInfo->aCol[0]),
105519 &pInfo->nColumn,
105520 &i
105521 );
105522 return i;
105523}
105524
105525/*
105526** Add a new element to the pAggInfo->aFunc[] array. Return the index of
105527** the new element. Return a negative number if malloc fails.
105528*/
105529static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
105530 int i;
105531 pInfo->aFunc = sqlite3ArrayAllocate(
105532 db,
105533 pInfo->aFunc,
105534 sizeof(pInfo->aFunc[0]),
105535 &pInfo->nFunc,
105536 &i
105537 );
105538 return i;
105539}
105540
105541/*
105542** This is the xExprCallback for a tree walker. It is used to
105543** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
105544** for additional information.
105545*/
105546static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
105547 int i;
105548 NameContext *pNC = pWalker->u.pNC;
105549 Parse *pParse = pNC->pParse;
105550 SrcList *pSrcList = pNC->pSrcList;
105551 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
105552
105553 assert( pNC->ncFlags & NC_UAggInfo );
105554 switch( pExpr->op ){
105555 case TK_AGG_COLUMN:
105556 case TK_COLUMN: {
105557 testcase( pExpr->op==TK_AGG_COLUMN );
105558 testcase( pExpr->op==TK_COLUMN );
105559 /* Check to see if the column is in one of the tables in the FROM
105560 ** clause of the aggregate query */
105561 if( ALWAYS(pSrcList!=0) ){
105562 struct SrcList_item *pItem = pSrcList->a;
105563 for(i=0; i<pSrcList->nSrc; i++, pItem++){
105564 struct AggInfo_col *pCol;
105565 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
105566 if( pExpr->iTable==pItem->iCursor ){
105567 /* If we reach this point, it means that pExpr refers to a table
105568 ** that is in the FROM clause of the aggregate query.
105569 **
105570 ** Make an entry for the column in pAggInfo->aCol[] if there
105571 ** is not an entry there already.
105572 */
105573 int k;
105574 pCol = pAggInfo->aCol;
105575 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
105576 if( pCol->iTable==pExpr->iTable &&
105577 pCol->iColumn==pExpr->iColumn ){
105578 break;
105579 }
105580 }
105581 if( (k>=pAggInfo->nColumn)
105582 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
105583 ){
105584 pCol = &pAggInfo->aCol[k];
105585 pCol->pTab = pExpr->y.pTab;
105586 pCol->iTable = pExpr->iTable;
105587 pCol->iColumn = pExpr->iColumn;
105588 pCol->iMem = ++pParse->nMem;
105589 pCol->iSorterColumn = -1;
105590 pCol->pCExpr = pExpr;
105591 if( pAggInfo->pGroupBy ){
105592 int j, n;
105593 ExprList *pGB = pAggInfo->pGroupBy;
105594 struct ExprList_item *pTerm = pGB->a;
105595 n = pGB->nExpr;
105596 for(j=0; j<n; j++, pTerm++){
105597 Expr *pE = pTerm->pExpr;
105598 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
105599 pE->iColumn==pExpr->iColumn ){
105600 pCol->iSorterColumn = j;
105601 break;
105602 }
105603 }
105604 }
105605 if( pCol->iSorterColumn<0 ){
105606 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
105607 }
105608 }
105609 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
105610 ** because it was there before or because we just created it).
105611 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
105612 ** pAggInfo->aCol[] entry.
105613 */
105614 ExprSetVVAProperty(pExpr, EP_NoReduce);
105615 pExpr->pAggInfo = pAggInfo;
105616 pExpr->op = TK_AGG_COLUMN;
105617 pExpr->iAgg = (i16)k;
105618 break;
105619 } /* endif pExpr->iTable==pItem->iCursor */
105620 } /* end loop over pSrcList */
105621 }
105622 return WRC_Prune;
105623 }
105624 case TK_AGG_FUNCTION: {
105625 if( (pNC->ncFlags & NC_InAggFunc)==0
105626 && pWalker->walkerDepth==pExpr->op2
105627 ){
105628 /* Check to see if pExpr is a duplicate of another aggregate
105629 ** function that is already in the pAggInfo structure
105630 */
105631 struct AggInfo_func *pItem = pAggInfo->aFunc;
105632 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
105633 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
105634 break;
105635 }
105636 }
105637 if( i>=pAggInfo->nFunc ){
105638 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
105639 */
105640 u8 enc = ENC(pParse->db);
105641 i = addAggInfoFunc(pParse->db, pAggInfo);
105642 if( i>=0 ){
105643 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
105644 pItem = &pAggInfo->aFunc[i];
105645 pItem->pFExpr = pExpr;
105646 pItem->iMem = ++pParse->nMem;
105647 assert( !ExprHasProperty(pExpr, EP_IntValue) );
105648 pItem->pFunc = sqlite3FindFunction(pParse->db,
105649 pExpr->u.zToken,
105650 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
105651 if( pExpr->flags & EP_Distinct ){
105652 pItem->iDistinct = pParse->nTab++;
105653 }else{
105654 pItem->iDistinct = -1;
105655 }
105656 }
105657 }
105658 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
105659 */
105660 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
105661 ExprSetVVAProperty(pExpr, EP_NoReduce);
105662 pExpr->iAgg = (i16)i;
105663 pExpr->pAggInfo = pAggInfo;
105664 return WRC_Prune;
105665 }else{
105666 return WRC_Continue;
105667 }
105668 }
105669 }
105670 return WRC_Continue;
105671}
105672
105673/*
105674** Analyze the pExpr expression looking for aggregate functions and
105675** for variables that need to be added to AggInfo object that pNC->pAggInfo
105676** points to. Additional entries are made on the AggInfo object as
105677** necessary.
105678**
105679** This routine should only be called after the expression has been
105680** analyzed by sqlite3ResolveExprNames().
105681*/
105682SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
105683 Walker w;
105684 w.xExprCallback = analyzeAggregate;
105685 w.xSelectCallback = sqlite3WalkerDepthIncrease;
105686 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
105687 w.walkerDepth = 0;
105688 w.u.pNC = pNC;
105689 w.pParse = 0;
105690 assert( pNC->pSrcList!=0 );
105691 sqlite3WalkExpr(&w, pExpr);
105692}
105693
105694/*
105695** Call sqlite3ExprAnalyzeAggregates() for every expression in an
105696** expression list. Return the number of errors.
105697**
105698** If an error is found, the analysis is cut short.
105699*/
105700SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
105701 struct ExprList_item *pItem;
105702 int i;
105703 if( pList ){
105704 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
105705 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
105706 }
105707 }
105708}
105709
105710/*
105711** Allocate a single new register for use to hold some intermediate result.
105712*/
105713SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
105714 if( pParse->nTempReg==0 ){
105715 return ++pParse->nMem;
105716 }
105717 return pParse->aTempReg[--pParse->nTempReg];
105718}
105719
105720/*
105721** Deallocate a register, making available for reuse for some other
105722** purpose.
105723*/
105724SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
105725 if( iReg ){
105726 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
105727 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
105728 pParse->aTempReg[pParse->nTempReg++] = iReg;
105729 }
105730 }
105731}
105732
105733/*
105734** Allocate or deallocate a block of nReg consecutive registers.
105735*/
105736SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
105737 int i, n;
105738 if( nReg==1 ) return sqlite3GetTempReg(pParse);
105739 i = pParse->iRangeReg;
105740 n = pParse->nRangeReg;
105741 if( nReg<=n ){
105742 pParse->iRangeReg += nReg;
105743 pParse->nRangeReg -= nReg;
105744 }else{
105745 i = pParse->nMem+1;
105746 pParse->nMem += nReg;
105747 }
105748 return i;
105749}
105750SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
105751 if( nReg==1 ){
105752 sqlite3ReleaseTempReg(pParse, iReg);
105753 return;
105754 }
105755 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
105756 if( nReg>pParse->nRangeReg ){
105757 pParse->nRangeReg = nReg;
105758 pParse->iRangeReg = iReg;
105759 }
105760}
105761
105762/*
105763** Mark all temporary registers as being unavailable for reuse.
105764**
105765** Always invoke this procedure after coding a subroutine or co-routine
105766** that might be invoked from other parts of the code, to ensure that
105767** the sub/co-routine does not use registers in common with the code that
105768** invokes the sub/co-routine.
105769*/
105770SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
105771 pParse->nTempReg = 0;
105772 pParse->nRangeReg = 0;
105773}
105774
105775/*
105776** Validate that no temporary register falls within the range of
105777** iFirst..iLast, inclusive. This routine is only call from within assert()
105778** statements.
105779*/
105780#ifdef SQLITE_DEBUG
105781SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
105782 int i;
105783 if( pParse->nRangeReg>0
105784 && pParse->iRangeReg+pParse->nRangeReg > iFirst
105785 && pParse->iRangeReg <= iLast
105786 ){
105787 return 0;
105788 }
105789 for(i=0; i<pParse->nTempReg; i++){
105790 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
105791 return 0;
105792 }
105793 }
105794 return 1;
105795}
105796#endif /* SQLITE_DEBUG */
105797
105798/************** End of expr.c ************************************************/
105799/************** Begin file alter.c *******************************************/
105800/*
105801** 2005 February 15
105802**
105803** The author disclaims copyright to this source code. In place of
105804** a legal notice, here is a blessing:
105805**
105806** May you do good and not evil.
105807** May you find forgiveness for yourself and forgive others.
105808** May you share freely, never taking more than you give.
105809**
105810*************************************************************************
105811** This file contains C code routines that used to generate VDBE code
105812** that implements the ALTER TABLE command.
105813*/
105814/* #include "sqliteInt.h" */
105815
105816/*
105817** The code in this file only exists if we are not omitting the
105818** ALTER TABLE logic from the build.
105819*/
105820#ifndef SQLITE_OMIT_ALTERTABLE
105821
105822/*
105823** Parameter zName is the name of a table that is about to be altered
105824** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
105825** If the table is a system table, this function leaves an error message
105826** in pParse->zErr (system tables may not be altered) and returns non-zero.
105827**
105828** Or, if zName is not a system table, zero is returned.
105829*/
105830static int isAlterableTable(Parse *pParse, Table *pTab){
105831 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
105832#ifndef SQLITE_OMIT_VIRTUALTABLE
105833 || ( (pTab->tabFlags & TF_Shadow)!=0
105834 && sqlite3ReadOnlyShadowTables(pParse->db)
105835 )
105836#endif
105837 ){
105838 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
105839 return 1;
105840 }
105841 return 0;
105842}
105843
105844/*
105845** Generate code to verify that the schemas of database zDb and, if
105846** bTemp is not true, database "temp", can still be parsed. This is
105847** called at the end of the generation of an ALTER TABLE ... RENAME ...
105848** statement to ensure that the operation has not rendered any schema
105849** objects unusable.
105850*/
105851static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
105852 sqlite3NestedParse(pParse,
105853 "SELECT 1 "
105854 "FROM \"%w\"." DFLT_SCHEMA_TABLE " "
105855 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105856 " AND sql NOT LIKE 'create virtual%%'"
105857 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
105858 zDb,
105859 zDb, bTemp
105860 );
105861
105862 if( bTemp==0 ){
105863 sqlite3NestedParse(pParse,
105864 "SELECT 1 "
105865 "FROM temp." DFLT_SCHEMA_TABLE " "
105866 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105867 " AND sql NOT LIKE 'create virtual%%'"
105868 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
105869 zDb
105870 );
105871 }
105872}
105873
105874/*
105875** Generate code to reload the schema for database iDb. And, if iDb!=1, for
105876** the temp database as well.
105877*/
105878static void renameReloadSchema(Parse *pParse, int iDb){
105879 Vdbe *v = pParse->pVdbe;
105880 if( v ){
105881 sqlite3ChangeCookie(pParse, iDb);
105882 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
105883 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
105884 }
105885}
105886
105887/*
105888** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
105889** command.
105890*/
105891SQLITE_PRIVATE void sqlite3AlterRenameTable(
105892 Parse *pParse, /* Parser context. */
105893 SrcList *pSrc, /* The table to rename. */
105894 Token *pName /* The new table name. */
105895){
105896 int iDb; /* Database that contains the table */
105897 char *zDb; /* Name of database iDb */
105898 Table *pTab; /* Table being renamed */
105899 char *zName = 0; /* NULL-terminated version of pName */
105900 sqlite3 *db = pParse->db; /* Database connection */
105901 int nTabName; /* Number of UTF-8 characters in zTabName */
105902 const char *zTabName; /* Original name of the table */
105903 Vdbe *v;
105904 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
105905 u32 savedDbFlags; /* Saved value of db->mDbFlags */
105906
105907 savedDbFlags = db->mDbFlags;
105908 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
105909 assert( pSrc->nSrc==1 );
105910 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
105911
105912 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
105913 if( !pTab ) goto exit_rename_table;
105914 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
105915 zDb = db->aDb[iDb].zDbSName;
105916 db->mDbFlags |= DBFLAG_PreferBuiltin;
105917
105918 /* Get a NULL terminated version of the new table name. */
105919 zName = sqlite3NameFromToken(db, pName);
105920 if( !zName ) goto exit_rename_table;
105921
105922 /* Check that a table or index named 'zName' does not already exist
105923 ** in database iDb. If so, this is an error.
105924 */
105925 if( sqlite3FindTable(db, zName, zDb)
105926 || sqlite3FindIndex(db, zName, zDb)
105927 || sqlite3IsShadowTableOf(db, pTab, zName)
105928 ){
105929 sqlite3ErrorMsg(pParse,
105930 "there is already another table or index with this name: %s", zName);
105931 goto exit_rename_table;
105932 }
105933
105934 /* Make sure it is not a system table being altered, or a reserved name
105935 ** that the table is being renamed to.
105936 */
105937 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
105938 goto exit_rename_table;
105939 }
105940 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
105941 goto exit_rename_table;
105942 }
105943
105944#ifndef SQLITE_OMIT_VIEW
105945 if( pTab->pSelect ){
105946 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
105947 goto exit_rename_table;
105948 }
105949#endif
105950
105951#ifndef SQLITE_OMIT_AUTHORIZATION
105952 /* Invoke the authorization callback. */
105953 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
105954 goto exit_rename_table;
105955 }
105956#endif
105957
105958#ifndef SQLITE_OMIT_VIRTUALTABLE
105959 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
105960 goto exit_rename_table;
105961 }
105962 if( IsVirtual(pTab) ){
105963 pVTab = sqlite3GetVTable(db, pTab);
105964 if( pVTab->pVtab->pModule->xRename==0 ){
105965 pVTab = 0;
105966 }
105967 }
105968#endif
105969
105970 /* Begin a transaction for database iDb. Then modify the schema cookie
105971 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
105972 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
105973 ** nested SQL may raise an exception. */
105974 v = sqlite3GetVdbe(pParse);
105975 if( v==0 ){
105976 goto exit_rename_table;
105977 }
105978 sqlite3MayAbort(pParse);
105979
105980 /* figure out how many UTF-8 characters are in zName */
105981 zTabName = pTab->zName;
105982 nTabName = sqlite3Utf8CharLen(zTabName, -1);
105983
105984 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
105985 ** the schema to use the new table name. */
105986 sqlite3NestedParse(pParse,
105987 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
105988 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
105989 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
105990 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
105991 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
105992 );
105993
105994 /* Update the tbl_name and name columns of the sqlite_schema table
105995 ** as required. */
105996 sqlite3NestedParse(pParse,
105997 "UPDATE %Q." DFLT_SCHEMA_TABLE " SET "
105998 "tbl_name = %Q, "
105999 "name = CASE "
106000 "WHEN type='table' THEN %Q "
106001 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
106002 " AND type='index' THEN "
106003 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
106004 "ELSE name END "
106005 "WHERE tbl_name=%Q COLLATE nocase AND "
106006 "(type='table' OR type='index' OR type='trigger');",
106007 zDb,
106008 zName, zName, zName,
106009 nTabName, zTabName
106010 );
106011
106012#ifndef SQLITE_OMIT_AUTOINCREMENT
106013 /* If the sqlite_sequence table exists in this database, then update
106014 ** it with the new table name.
106015 */
106016 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
106017 sqlite3NestedParse(pParse,
106018 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
106019 zDb, zName, pTab->zName);
106020 }
106021#endif
106022
106023 /* If the table being renamed is not itself part of the temp database,
106024 ** edit view and trigger definitions within the temp database
106025 ** as required. */
106026 if( iDb!=1 ){
106027 sqlite3NestedParse(pParse,
106028 "UPDATE sqlite_temp_schema SET "
106029 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
106030 "tbl_name = "
106031 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
106032 " sqlite_rename_test(%Q, sql, type, name, 1) "
106033 "THEN %Q ELSE tbl_name END "
106034 "WHERE type IN ('view', 'trigger')"
106035 , zDb, zTabName, zName, zTabName, zDb, zName);
106036 }
106037
106038 /* If this is a virtual table, invoke the xRename() function if
106039 ** one is defined. The xRename() callback will modify the names
106040 ** of any resources used by the v-table implementation (including other
106041 ** SQLite tables) that are identified by the name of the virtual table.
106042 */
106043#ifndef SQLITE_OMIT_VIRTUALTABLE
106044 if( pVTab ){
106045 int i = ++pParse->nMem;
106046 sqlite3VdbeLoadString(v, i, zName);
106047 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
106048 }
106049#endif
106050
106051 renameReloadSchema(pParse, iDb);
106052 renameTestSchema(pParse, zDb, iDb==1);
106053
106054exit_rename_table:
106055 sqlite3SrcListDelete(db, pSrc);
106056 sqlite3DbFree(db, zName);
106057 db->mDbFlags = savedDbFlags;
106058}
106059
106060/*
106061** Write code that will raise an error if the table described by
106062** zDb and zTab is not empty.
106063*/
106064static void sqlite3ErrorIfNotEmpty(
106065 Parse *pParse, /* Parsing context */
106066 const char *zDb, /* Schema holding the table */
106067 const char *zTab, /* Table to check for empty */
106068 const char *zErr /* Error message text */
106069){
106070 sqlite3NestedParse(pParse,
106071 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
106072 zErr, zDb, zTab
106073 );
106074}
106075
106076/*
106077** This function is called after an "ALTER TABLE ... ADD" statement
106078** has been parsed. Argument pColDef contains the text of the new
106079** column definition.
106080**
106081** The Table structure pParse->pNewTable was extended to include
106082** the new column during parsing.
106083*/
106084SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
106085 Table *pNew; /* Copy of pParse->pNewTable */
106086 Table *pTab; /* Table being altered */
106087 int iDb; /* Database number */
106088 const char *zDb; /* Database name */
106089 const char *zTab; /* Table name */
106090 char *zCol; /* Null-terminated column definition */
106091 Column *pCol; /* The new column */
106092 Expr *pDflt; /* Default value for the new column */
106093 sqlite3 *db; /* The database connection; */
106094 Vdbe *v; /* The prepared statement under construction */
106095 int r1; /* Temporary registers */
106096
106097 db = pParse->db;
106098 if( pParse->nErr || db->mallocFailed ) return;
106099 pNew = pParse->pNewTable;
106100 assert( pNew );
106101
106102 assert( sqlite3BtreeHoldsAllMutexes(db) );
106103 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
106104 zDb = db->aDb[iDb].zDbSName;
106105 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
106106 pCol = &pNew->aCol[pNew->nCol-1];
106107 pDflt = pCol->pDflt;
106108 pTab = sqlite3FindTable(db, zTab, zDb);
106109 assert( pTab );
106110
106111#ifndef SQLITE_OMIT_AUTHORIZATION
106112 /* Invoke the authorization callback. */
106113 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
106114 return;
106115 }
106116#endif
106117
106118
106119 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
106120 ** If there is a NOT NULL constraint, then the default value for the
106121 ** column must not be NULL.
106122 */
106123 if( pCol->colFlags & COLFLAG_PRIMKEY ){
106124 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
106125 return;
106126 }
106127 if( pNew->pIndex ){
106128 sqlite3ErrorMsg(pParse,
106129 "Cannot add a UNIQUE column");
106130 return;
106131 }
106132 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
106133 /* If the default value for the new column was specified with a
106134 ** literal NULL, then set pDflt to 0. This simplifies checking
106135 ** for an SQL NULL default below.
106136 */
106137 assert( pDflt==0 || pDflt->op==TK_SPAN );
106138 if( pDflt && pDflt->pLeft->op==TK_NULL ){
106139 pDflt = 0;
106140 }
106141 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
106142 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
106143 "Cannot add a REFERENCES column with non-NULL default value");
106144 }
106145 if( pCol->notNull && !pDflt ){
106146 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
106147 "Cannot add a NOT NULL column with default value NULL");
106148 }
106149
106150
106151 /* Ensure the default expression is something that sqlite3ValueFromExpr()
106152 ** can handle (i.e. not CURRENT_TIME etc.)
106153 */
106154 if( pDflt ){
106155 sqlite3_value *pVal = 0;
106156 int rc;
106157 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
106158 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
106159 if( rc!=SQLITE_OK ){
106160 assert( db->mallocFailed == 1 );
106161 return;
106162 }
106163 if( !pVal ){
106164 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
106165 "Cannot add a column with non-constant default");
106166 }
106167 sqlite3ValueFree(pVal);
106168 }
106169 }else if( pCol->colFlags & COLFLAG_STORED ){
106170 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
106171 }
106172
106173
106174 /* Modify the CREATE TABLE statement. */
106175 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
106176 if( zCol ){
106177 char *zEnd = &zCol[pColDef->n-1];
106178 u32 savedDbFlags = db->mDbFlags;
106179 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
106180 *zEnd-- = '\0';
106181 }
106182 db->mDbFlags |= DBFLAG_PreferBuiltin;
106183 sqlite3NestedParse(pParse,
106184 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
106185 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
106186 "WHERE type = 'table' AND name = %Q",
106187 zDb, pNew->addColOffset, zCol, pNew->addColOffset+1,
106188 zTab
106189 );
106190 sqlite3DbFree(db, zCol);
106191 db->mDbFlags = savedDbFlags;
106192 }
106193
106194 /* Make sure the schema version is at least 3. But do not upgrade
106195 ** from less than 3 to 4, as that will corrupt any preexisting DESC
106196 ** index.
106197 */
106198 v = sqlite3GetVdbe(pParse);
106199 if( v ){
106200 r1 = sqlite3GetTempReg(pParse);
106201 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
106202 sqlite3VdbeUsesBtree(v, iDb);
106203 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
106204 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
106205 VdbeCoverage(v);
106206 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
106207 sqlite3ReleaseTempReg(pParse, r1);
106208 }
106209
106210 /* Reload the table definition */
106211 renameReloadSchema(pParse, iDb);
106212}
106213
106214/*
106215** This function is called by the parser after the table-name in
106216** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
106217** pSrc is the full-name of the table being altered.
106218**
106219** This routine makes a (partial) copy of the Table structure
106220** for the table being altered and sets Parse.pNewTable to point
106221** to it. Routines called by the parser as the column definition
106222** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
106223** the copy. The copy of the Table structure is deleted by tokenize.c
106224** after parsing is finished.
106225**
106226** Routine sqlite3AlterFinishAddColumn() will be called to complete
106227** coding the "ALTER TABLE ... ADD" statement.
106228*/
106229SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
106230 Table *pNew;
106231 Table *pTab;
106232 int iDb;
106233 int i;
106234 int nAlloc;
106235 sqlite3 *db = pParse->db;
106236
106237 /* Look up the table being altered. */
106238 assert( pParse->pNewTable==0 );
106239 assert( sqlite3BtreeHoldsAllMutexes(db) );
106240 if( db->mallocFailed ) goto exit_begin_add_column;
106241 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
106242 if( !pTab ) goto exit_begin_add_column;
106243
106244#ifndef SQLITE_OMIT_VIRTUALTABLE
106245 if( IsVirtual(pTab) ){
106246 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
106247 goto exit_begin_add_column;
106248 }
106249#endif
106250
106251 /* Make sure this is not an attempt to ALTER a view. */
106252 if( pTab->pSelect ){
106253 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
106254 goto exit_begin_add_column;
106255 }
106256 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
106257 goto exit_begin_add_column;
106258 }
106259
106260 sqlite3MayAbort(pParse);
106261 assert( pTab->addColOffset>0 );
106262 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106263
106264 /* Put a copy of the Table struct in Parse.pNewTable for the
106265 ** sqlite3AddColumn() function and friends to modify. But modify
106266 ** the name by adding an "sqlite_altertab_" prefix. By adding this
106267 ** prefix, we insure that the name will not collide with an existing
106268 ** table because user table are not allowed to have the "sqlite_"
106269 ** prefix on their name.
106270 */
106271 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
106272 if( !pNew ) goto exit_begin_add_column;
106273 pParse->pNewTable = pNew;
106274 pNew->nTabRef = 1;
106275 pNew->nCol = pTab->nCol;
106276 assert( pNew->nCol>0 );
106277 nAlloc = (((pNew->nCol-1)/8)*8)+8;
106278 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
106279 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
106280 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
106281 if( !pNew->aCol || !pNew->zName ){
106282 assert( db->mallocFailed );
106283 goto exit_begin_add_column;
106284 }
106285 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
106286 for(i=0; i<pNew->nCol; i++){
106287 Column *pCol = &pNew->aCol[i];
106288 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
106289 pCol->hName = sqlite3StrIHash(pCol->zName);
106290 pCol->zColl = 0;
106291 pCol->pDflt = 0;
106292 }
106293 pNew->pSchema = db->aDb[iDb].pSchema;
106294 pNew->addColOffset = pTab->addColOffset;
106295 pNew->nTabRef = 1;
106296
106297exit_begin_add_column:
106298 sqlite3SrcListDelete(db, pSrc);
106299 return;
106300}
106301
106302/*
106303** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
106304** command. This function checks if the table is a view or virtual
106305** table (columns of views or virtual tables may not be renamed). If so,
106306** it loads an error message into pParse and returns non-zero.
106307**
106308** Or, if pTab is not a view or virtual table, zero is returned.
106309*/
106310#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
106311static int isRealTable(Parse *pParse, Table *pTab){
106312 const char *zType = 0;
106313#ifndef SQLITE_OMIT_VIEW
106314 if( pTab->pSelect ){
106315 zType = "view";
106316 }
106317#endif
106318#ifndef SQLITE_OMIT_VIRTUALTABLE
106319 if( IsVirtual(pTab) ){
106320 zType = "virtual table";
106321 }
106322#endif
106323 if( zType ){
106324 sqlite3ErrorMsg(
106325 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
106326 );
106327 return 1;
106328 }
106329 return 0;
106330}
106331#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
106332# define isRealTable(x,y) (0)
106333#endif
106334
106335/*
106336** Handles the following parser reduction:
106337**
106338** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
106339*/
106340SQLITE_PRIVATE void sqlite3AlterRenameColumn(
106341 Parse *pParse, /* Parsing context */
106342 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
106343 Token *pOld, /* Name of column being changed */
106344 Token *pNew /* New column name */
106345){
106346 sqlite3 *db = pParse->db; /* Database connection */
106347 Table *pTab; /* Table being updated */
106348 int iCol; /* Index of column being renamed */
106349 char *zOld = 0; /* Old column name */
106350 char *zNew = 0; /* New column name */
106351 const char *zDb; /* Name of schema containing the table */
106352 int iSchema; /* Index of the schema */
106353 int bQuote; /* True to quote the new name */
106354
106355 /* Locate the table to be altered */
106356 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
106357 if( !pTab ) goto exit_rename_column;
106358
106359 /* Cannot alter a system table */
106360 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
106361 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
106362
106363 /* Which schema holds the table to be altered */
106364 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
106365 assert( iSchema>=0 );
106366 zDb = db->aDb[iSchema].zDbSName;
106367
106368#ifndef SQLITE_OMIT_AUTHORIZATION
106369 /* Invoke the authorization callback. */
106370 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
106371 goto exit_rename_column;
106372 }
106373#endif
106374
106375 /* Make sure the old name really is a column name in the table to be
106376 ** altered. Set iCol to be the index of the column being renamed */
106377 zOld = sqlite3NameFromToken(db, pOld);
106378 if( !zOld ) goto exit_rename_column;
106379 for(iCol=0; iCol<pTab->nCol; iCol++){
106380 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
106381 }
106382 if( iCol==pTab->nCol ){
106383 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
106384 goto exit_rename_column;
106385 }
106386
106387 /* Do the rename operation using a recursive UPDATE statement that
106388 ** uses the sqlite_rename_column() SQL function to compute the new
106389 ** CREATE statement text for the sqlite_schema table.
106390 */
106391 sqlite3MayAbort(pParse);
106392 zNew = sqlite3NameFromToken(db, pNew);
106393 if( !zNew ) goto exit_rename_column;
106394 assert( pNew->n>0 );
106395 bQuote = sqlite3Isquote(pNew->z[0]);
106396 sqlite3NestedParse(pParse,
106397 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
106398 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
106399 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
106400 " AND (type != 'index' OR tbl_name = %Q)"
106401 " AND sql NOT LIKE 'create virtual%%'",
106402 zDb,
106403 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
106404 pTab->zName
106405 );
106406
106407 sqlite3NestedParse(pParse,
106408 "UPDATE temp." DFLT_SCHEMA_TABLE " SET "
106409 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
106410 "WHERE type IN ('trigger', 'view')",
106411 zDb, pTab->zName, iCol, zNew, bQuote
106412 );
106413
106414 /* Drop and reload the database schema. */
106415 renameReloadSchema(pParse, iSchema);
106416 renameTestSchema(pParse, zDb, iSchema==1);
106417
106418 exit_rename_column:
106419 sqlite3SrcListDelete(db, pSrc);
106420 sqlite3DbFree(db, zOld);
106421 sqlite3DbFree(db, zNew);
106422 return;
106423}
106424
106425/*
106426** Each RenameToken object maps an element of the parse tree into
106427** the token that generated that element. The parse tree element
106428** might be one of:
106429**
106430** * A pointer to an Expr that represents an ID
106431** * The name of a table column in Column.zName
106432**
106433** A list of RenameToken objects can be constructed during parsing.
106434** Each new object is created by sqlite3RenameTokenMap().
106435** As the parse tree is transformed, the sqlite3RenameTokenRemap()
106436** routine is used to keep the mapping current.
106437**
106438** After the parse finishes, renameTokenFind() routine can be used
106439** to look up the actual token value that created some element in
106440** the parse tree.
106441*/
106442struct RenameToken {
106443 void *p; /* Parse tree element created by token t */
106444 Token t; /* The token that created parse tree element p */
106445 RenameToken *pNext; /* Next is a list of all RenameToken objects */
106446};
106447
106448/*
106449** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
106450** down into the Walker.
106451*/
106452typedef struct RenameCtx RenameCtx;
106453struct RenameCtx {
106454 RenameToken *pList; /* List of tokens to overwrite */
106455 int nList; /* Number of tokens in pList */
106456 int iCol; /* Index of column being renamed */
106457 Table *pTab; /* Table being ALTERed */
106458 const char *zOld; /* Old column name */
106459};
106460
106461#ifdef SQLITE_DEBUG
106462/*
106463** This function is only for debugging. It performs two tasks:
106464**
106465** 1. Checks that pointer pPtr does not already appear in the
106466** rename-token list.
106467**
106468** 2. Dereferences each pointer in the rename-token list.
106469**
106470** The second is most effective when debugging under valgrind or
106471** address-sanitizer or similar. If any of these pointers no longer
106472** point to valid objects, an exception is raised by the memory-checking
106473** tool.
106474**
106475** The point of this is to prevent comparisons of invalid pointer values.
106476** Even though this always seems to work, it is undefined according to the
106477** C standard. Example of undefined comparison:
106478**
106479** sqlite3_free(x);
106480** if( x==y ) ...
106481**
106482** Technically, as x no longer points into a valid object or to the byte
106483** following a valid object, it may not be used in comparison operations.
106484*/
106485static void renameTokenCheckAll(Parse *pParse, void *pPtr){
106486 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
106487 RenameToken *p;
106488 u8 i = 0;
106489 for(p=pParse->pRename; p; p=p->pNext){
106490 if( p->p ){
106491 assert( p->p!=pPtr );
106492 i += *(u8*)(p->p);
106493 }
106494 }
106495 }
106496}
106497#else
106498# define renameTokenCheckAll(x,y)
106499#endif
106500
106501/*
106502** Remember that the parser tree element pPtr was created using
106503** the token pToken.
106504**
106505** In other words, construct a new RenameToken object and add it
106506** to the list of RenameToken objects currently being built up
106507** in pParse->pRename.
106508**
106509** The pPtr argument is returned so that this routine can be used
106510** with tail recursion in tokenExpr() routine, for a small performance
106511** improvement.
106512*/
106513SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
106514 RenameToken *pNew;
106515 assert( pPtr || pParse->db->mallocFailed );
106516 renameTokenCheckAll(pParse, pPtr);
106517 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
106518 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
106519 if( pNew ){
106520 pNew->p = pPtr;
106521 pNew->t = *pToken;
106522 pNew->pNext = pParse->pRename;
106523 pParse->pRename = pNew;
106524 }
106525 }
106526
106527 return pPtr;
106528}
106529
106530/*
106531** It is assumed that there is already a RenameToken object associated
106532** with parse tree element pFrom. This function remaps the associated token
106533** to parse tree element pTo.
106534*/
106535SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
106536 RenameToken *p;
106537 renameTokenCheckAll(pParse, pTo);
106538 for(p=pParse->pRename; p; p=p->pNext){
106539 if( p->p==pFrom ){
106540 p->p = pTo;
106541 break;
106542 }
106543 }
106544}
106545
106546/*
106547** Walker callback used by sqlite3RenameExprUnmap().
106548*/
106549static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
106550 Parse *pParse = pWalker->pParse;
106551 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
106552 return WRC_Continue;
106553}
106554
106555/*
106556** Iterate through the Select objects that are part of WITH clauses attached
106557** to select statement pSelect.
106558*/
106559static void renameWalkWith(Walker *pWalker, Select *pSelect){
106560 With *pWith = pSelect->pWith;
106561 if( pWith ){
106562 int i;
106563 for(i=0; i<pWith->nCte; i++){
106564 Select *p = pWith->a[i].pSelect;
106565 NameContext sNC;
106566 memset(&sNC, 0, sizeof(sNC));
106567 sNC.pParse = pWalker->pParse;
106568 sqlite3SelectPrep(sNC.pParse, p, &sNC);
106569 sqlite3WalkSelect(pWalker, p);
106570 sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
106571 }
106572 }
106573}
106574
106575/*
106576** Unmap all tokens in the IdList object passed as the second argument.
106577*/
106578static void unmapColumnIdlistNames(
106579 Parse *pParse,
106580 IdList *pIdList
106581){
106582 if( pIdList ){
106583 int ii;
106584 for(ii=0; ii<pIdList->nId; ii++){
106585 sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
106586 }
106587 }
106588}
106589
106590/*
106591** Walker callback used by sqlite3RenameExprUnmap().
106592*/
106593static int renameUnmapSelectCb(Walker *pWalker, Select *p){
106594 Parse *pParse = pWalker->pParse;
106595 int i;
106596 if( pParse->nErr ) return WRC_Abort;
106597 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
106598 if( ALWAYS(p->pEList) ){
106599 ExprList *pList = p->pEList;
106600 for(i=0; i<pList->nExpr; i++){
106601 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
106602 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
106603 }
106604 }
106605 }
106606 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
106607 SrcList *pSrc = p->pSrc;
106608 for(i=0; i<pSrc->nSrc; i++){
106609 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
106610 if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
106611 unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
106612 }
106613 }
106614
106615 renameWalkWith(pWalker, p);
106616 return WRC_Continue;
106617}
106618
106619/*
106620** Remove all nodes that are part of expression pExpr from the rename list.
106621*/
106622SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
106623 u8 eMode = pParse->eParseMode;
106624 Walker sWalker;
106625 memset(&sWalker, 0, sizeof(Walker));
106626 sWalker.pParse = pParse;
106627 sWalker.xExprCallback = renameUnmapExprCb;
106628 sWalker.xSelectCallback = renameUnmapSelectCb;
106629 pParse->eParseMode = PARSE_MODE_UNMAP;
106630 sqlite3WalkExpr(&sWalker, pExpr);
106631 pParse->eParseMode = eMode;
106632}
106633
106634/*
106635** Remove all nodes that are part of expression-list pEList from the
106636** rename list.
106637*/
106638SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
106639 if( pEList ){
106640 int i;
106641 Walker sWalker;
106642 memset(&sWalker, 0, sizeof(Walker));
106643 sWalker.pParse = pParse;
106644 sWalker.xExprCallback = renameUnmapExprCb;
106645 sqlite3WalkExprList(&sWalker, pEList);
106646 for(i=0; i<pEList->nExpr; i++){
106647 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
106648 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
106649 }
106650 }
106651 }
106652}
106653
106654/*
106655** Free the list of RenameToken objects given in the second argument
106656*/
106657static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
106658 RenameToken *pNext;
106659 RenameToken *p;
106660 for(p=pToken; p; p=pNext){
106661 pNext = p->pNext;
106662 sqlite3DbFree(db, p);
106663 }
106664}
106665
106666/*
106667** Search the Parse object passed as the first argument for a RenameToken
106668** object associated with parse tree element pPtr. If found, remove it
106669** from the Parse object and add it to the list maintained by the
106670** RenameCtx object passed as the second argument.
106671*/
106672static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
106673 RenameToken **pp;
106674 assert( pPtr!=0 );
106675 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
106676 if( (*pp)->p==pPtr ){
106677 RenameToken *pToken = *pp;
106678 *pp = pToken->pNext;
106679 pToken->pNext = pCtx->pList;
106680 pCtx->pList = pToken;
106681 pCtx->nList++;
106682 break;
106683 }
106684 }
106685}
106686
106687/*
106688** This is a Walker select callback. It does nothing. It is only required
106689** because without a dummy callback, sqlite3WalkExpr() and similar do not
106690** descend into sub-select statements.
106691*/
106692static int renameColumnSelectCb(Walker *pWalker, Select *p){
106693 if( p->selFlags & SF_View ) return WRC_Prune;
106694 renameWalkWith(pWalker, p);
106695 return WRC_Continue;
106696}
106697
106698/*
106699** This is a Walker expression callback.
106700**
106701** For every TK_COLUMN node in the expression tree, search to see
106702** if the column being references is the column being renamed by an
106703** ALTER TABLE statement. If it is, then attach its associated
106704** RenameToken object to the list of RenameToken objects being
106705** constructed in RenameCtx object at pWalker->u.pRename.
106706*/
106707static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
106708 RenameCtx *p = pWalker->u.pRename;
106709 if( pExpr->op==TK_TRIGGER
106710 && pExpr->iColumn==p->iCol
106711 && pWalker->pParse->pTriggerTab==p->pTab
106712 ){
106713 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
106714 }else if( pExpr->op==TK_COLUMN
106715 && pExpr->iColumn==p->iCol
106716 && p->pTab==pExpr->y.pTab
106717 ){
106718 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
106719 }
106720 return WRC_Continue;
106721}
106722
106723/*
106724** The RenameCtx contains a list of tokens that reference a column that
106725** is being renamed by an ALTER TABLE statement. Return the "last"
106726** RenameToken in the RenameCtx and remove that RenameToken from the
106727** RenameContext. "Last" means the last RenameToken encountered when
106728** the input SQL is parsed from left to right. Repeated calls to this routine
106729** return all column name tokens in the order that they are encountered
106730** in the SQL statement.
106731*/
106732static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
106733 RenameToken *pBest = pCtx->pList;
106734 RenameToken *pToken;
106735 RenameToken **pp;
106736
106737 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
106738 if( pToken->t.z>pBest->t.z ) pBest = pToken;
106739 }
106740 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
106741 *pp = pBest->pNext;
106742
106743 return pBest;
106744}
106745
106746/*
106747** An error occured while parsing or otherwise processing a database
106748** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
106749** ALTER TABLE RENAME COLUMN program. The error message emitted by the
106750** sub-routine is currently stored in pParse->zErrMsg. This function
106751** adds context to the error message and then stores it in pCtx.
106752*/
106753static void renameColumnParseError(
106754 sqlite3_context *pCtx,
106755 int bPost,
106756 sqlite3_value *pType,
106757 sqlite3_value *pObject,
106758 Parse *pParse
106759){
106760 const char *zT = (const char*)sqlite3_value_text(pType);
106761 const char *zN = (const char*)sqlite3_value_text(pObject);
106762 char *zErr;
106763
106764 zErr = sqlite3_mprintf("error in %s %s%s: %s",
106765 zT, zN, (bPost ? " after rename" : ""),
106766 pParse->zErrMsg
106767 );
106768 sqlite3_result_error(pCtx, zErr, -1);
106769 sqlite3_free(zErr);
106770}
106771
106772/*
106773** For each name in the the expression-list pEList (i.e. each
106774** pEList->a[i].zName) that matches the string in zOld, extract the
106775** corresponding rename-token from Parse object pParse and add it
106776** to the RenameCtx pCtx.
106777*/
106778static void renameColumnElistNames(
106779 Parse *pParse,
106780 RenameCtx *pCtx,
106781 ExprList *pEList,
106782 const char *zOld
106783){
106784 if( pEList ){
106785 int i;
106786 for(i=0; i<pEList->nExpr; i++){
106787 char *zName = pEList->a[i].zEName;
106788 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
106789 && ALWAYS(zName!=0)
106790 && 0==sqlite3_stricmp(zName, zOld)
106791 ){
106792 renameTokenFind(pParse, pCtx, (void*)zName);
106793 }
106794 }
106795 }
106796}
106797
106798/*
106799** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
106800** that matches the string in zOld, extract the corresponding rename-token
106801** from Parse object pParse and add it to the RenameCtx pCtx.
106802*/
106803static void renameColumnIdlistNames(
106804 Parse *pParse,
106805 RenameCtx *pCtx,
106806 IdList *pIdList,
106807 const char *zOld
106808){
106809 if( pIdList ){
106810 int i;
106811 for(i=0; i<pIdList->nId; i++){
106812 char *zName = pIdList->a[i].zName;
106813 if( 0==sqlite3_stricmp(zName, zOld) ){
106814 renameTokenFind(pParse, pCtx, (void*)zName);
106815 }
106816 }
106817 }
106818}
106819
106820
106821/*
106822** Parse the SQL statement zSql using Parse object (*p). The Parse object
106823** is initialized by this function before it is used.
106824*/
106825static int renameParseSql(
106826 Parse *p, /* Memory to use for Parse object */
106827 const char *zDb, /* Name of schema SQL belongs to */
106828 sqlite3 *db, /* Database handle */
106829 const char *zSql, /* SQL to parse */
106830 int bTemp /* True if SQL is from temp schema */
106831){
106832 int rc;
106833 char *zErr = 0;
106834
106835 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
106836
106837 /* Parse the SQL statement passed as the first argument. If no error
106838 ** occurs and the parse does not result in a new table, index or
106839 ** trigger object, the database must be corrupt. */
106840 memset(p, 0, sizeof(Parse));
106841 p->eParseMode = PARSE_MODE_RENAME;
106842 p->db = db;
106843 p->nQueryLoop = 1;
106844 rc = sqlite3RunParser(p, zSql, &zErr);
106845 assert( p->zErrMsg==0 );
106846 assert( rc!=SQLITE_OK || zErr==0 );
106847 p->zErrMsg = zErr;
106848 if( db->mallocFailed ) rc = SQLITE_NOMEM;
106849 if( rc==SQLITE_OK
106850 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
106851 ){
106852 rc = SQLITE_CORRUPT_BKPT;
106853 }
106854
106855#ifdef SQLITE_DEBUG
106856 /* Ensure that all mappings in the Parse.pRename list really do map to
106857 ** a part of the input string. */
106858 if( rc==SQLITE_OK ){
106859 int nSql = sqlite3Strlen30(zSql);
106860 RenameToken *pToken;
106861 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
106862 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
106863 }
106864 }
106865#endif
106866
106867 db->init.iDb = 0;
106868 return rc;
106869}
106870
106871/*
106872** This function edits SQL statement zSql, replacing each token identified
106873** by the linked list pRename with the text of zNew. If argument bQuote is
106874** true, then zNew is always quoted first. If no error occurs, the result
106875** is loaded into context object pCtx as the result.
106876**
106877** Or, if an error occurs (i.e. an OOM condition), an error is left in
106878** pCtx and an SQLite error code returned.
106879*/
106880static int renameEditSql(
106881 sqlite3_context *pCtx, /* Return result here */
106882 RenameCtx *pRename, /* Rename context */
106883 const char *zSql, /* SQL statement to edit */
106884 const char *zNew, /* New token text */
106885 int bQuote /* True to always quote token */
106886){
106887 int nNew = sqlite3Strlen30(zNew);
106888 int nSql = sqlite3Strlen30(zSql);
106889 sqlite3 *db = sqlite3_context_db_handle(pCtx);
106890 int rc = SQLITE_OK;
106891 char *zQuot;
106892 char *zOut;
106893 int nQuot;
106894
106895 /* Set zQuot to point to a buffer containing a quoted copy of the
106896 ** identifier zNew. If the corresponding identifier in the original
106897 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
106898 ** point to zQuot so that all substitutions are made using the
106899 ** quoted version of the new column name. */
106900 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
106901 if( zQuot==0 ){
106902 return SQLITE_NOMEM;
106903 }else{
106904 nQuot = sqlite3Strlen30(zQuot);
106905 }
106906 if( bQuote ){
106907 zNew = zQuot;
106908 nNew = nQuot;
106909 }
106910
106911 /* At this point pRename->pList contains a list of RenameToken objects
106912 ** corresponding to all tokens in the input SQL that must be replaced
106913 ** with the new column name. All that remains is to construct and
106914 ** return the edited SQL string. */
106915 assert( nQuot>=nNew );
106916 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
106917 if( zOut ){
106918 int nOut = nSql;
106919 memcpy(zOut, zSql, nSql);
106920 while( pRename->pList ){
106921 int iOff; /* Offset of token to replace in zOut */
106922 RenameToken *pBest = renameColumnTokenNext(pRename);
106923
106924 u32 nReplace;
106925 const char *zReplace;
106926 if( sqlite3IsIdChar(*pBest->t.z) ){
106927 nReplace = nNew;
106928 zReplace = zNew;
106929 }else{
106930 nReplace = nQuot;
106931 zReplace = zQuot;
106932 }
106933
106934 iOff = pBest->t.z - zSql;
106935 if( pBest->t.n!=nReplace ){
106936 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
106937 nOut - (iOff + pBest->t.n)
106938 );
106939 nOut += nReplace - pBest->t.n;
106940 zOut[nOut] = '\0';
106941 }
106942 memcpy(&zOut[iOff], zReplace, nReplace);
106943 sqlite3DbFree(db, pBest);
106944 }
106945
106946 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
106947 sqlite3DbFree(db, zOut);
106948 }else{
106949 rc = SQLITE_NOMEM;
106950 }
106951
106952 sqlite3_free(zQuot);
106953 return rc;
106954}
106955
106956/*
106957** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
106958** it was read from the schema of database zDb. Return SQLITE_OK if
106959** successful. Otherwise, return an SQLite error code and leave an error
106960** message in the Parse object.
106961*/
106962static int renameResolveTrigger(Parse *pParse){
106963 sqlite3 *db = pParse->db;
106964 Trigger *pNew = pParse->pNewTrigger;
106965 TriggerStep *pStep;
106966 NameContext sNC;
106967 int rc = SQLITE_OK;
106968
106969 memset(&sNC, 0, sizeof(sNC));
106970 sNC.pParse = pParse;
106971 assert( pNew->pTabSchema );
106972 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
106973 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
106974 );
106975 pParse->eTriggerOp = pNew->op;
106976 /* ALWAYS() because if the table of the trigger does not exist, the
106977 ** error would have been hit before this point */
106978 if( ALWAYS(pParse->pTriggerTab) ){
106979 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
106980 }
106981
106982 /* Resolve symbols in WHEN clause */
106983 if( rc==SQLITE_OK && pNew->pWhen ){
106984 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
106985 }
106986
106987 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
106988 if( pStep->pSelect ){
106989 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
106990 if( pParse->nErr ) rc = pParse->rc;
106991 }
106992 if( rc==SQLITE_OK && pStep->zTarget ){
106993 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
106994 if( pSrc ){
106995 int i;
106996 for(i=0; i<pSrc->nSrc && rc==SQLITE_OK; i++){
106997 struct SrcList_item *p = &pSrc->a[i];
106998 p->pTab = sqlite3LocateTableItem(pParse, 0, p);
106999 p->iCursor = pParse->nTab++;
107000 if( p->pTab==0 ){
107001 rc = SQLITE_ERROR;
107002 }else{
107003 p->pTab->nTabRef++;
107004 rc = sqlite3ViewGetColumnNames(pParse, p->pTab);
107005 }
107006 }
107007 sNC.pSrcList = pSrc;
107008 if( rc==SQLITE_OK && pStep->pWhere ){
107009 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
107010 }
107011 if( rc==SQLITE_OK ){
107012 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
107013 }
107014 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
107015 if( pStep->pUpsert ){
107016 Upsert *pUpsert = pStep->pUpsert;
107017 assert( rc==SQLITE_OK );
107018 pUpsert->pUpsertSrc = pSrc;
107019 sNC.uNC.pUpsert = pUpsert;
107020 sNC.ncFlags = NC_UUpsert;
107021 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
107022 if( rc==SQLITE_OK ){
107023 ExprList *pUpsertSet = pUpsert->pUpsertSet;
107024 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
107025 }
107026 if( rc==SQLITE_OK ){
107027 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
107028 }
107029 if( rc==SQLITE_OK ){
107030 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
107031 }
107032 sNC.ncFlags = 0;
107033 }
107034 sNC.pSrcList = 0;
107035 sqlite3SrcListDelete(db, pSrc);
107036 }else{
107037 rc = SQLITE_NOMEM;
107038 }
107039 }
107040 }
107041 return rc;
107042}
107043
107044/*
107045** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
107046** objects that are part of the trigger passed as the second argument.
107047*/
107048static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
107049 TriggerStep *pStep;
107050
107051 /* Find tokens to edit in WHEN clause */
107052 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
107053
107054 /* Find tokens to edit in trigger steps */
107055 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
107056 sqlite3WalkSelect(pWalker, pStep->pSelect);
107057 sqlite3WalkExpr(pWalker, pStep->pWhere);
107058 sqlite3WalkExprList(pWalker, pStep->pExprList);
107059 if( pStep->pUpsert ){
107060 Upsert *pUpsert = pStep->pUpsert;
107061 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
107062 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
107063 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
107064 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
107065 }
107066 }
107067}
107068
107069/*
107070** Free the contents of Parse object (*pParse). Do not free the memory
107071** occupied by the Parse object itself.
107072*/
107073static void renameParseCleanup(Parse *pParse){
107074 sqlite3 *db = pParse->db;
107075 Index *pIdx;
107076 if( pParse->pVdbe ){
107077 sqlite3VdbeFinalize(pParse->pVdbe);
107078 }
107079 sqlite3DeleteTable(db, pParse->pNewTable);
107080 while( (pIdx = pParse->pNewIndex)!=0 ){
107081 pParse->pNewIndex = pIdx->pNext;
107082 sqlite3FreeIndex(db, pIdx);
107083 }
107084 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
107085 sqlite3DbFree(db, pParse->zErrMsg);
107086 renameTokenFree(db, pParse->pRename);
107087 sqlite3ParserReset(pParse);
107088}
107089
107090/*
107091** SQL function:
107092**
107093** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
107094**
107095** 0. zSql: SQL statement to rewrite
107096** 1. type: Type of object ("table", "view" etc.)
107097** 2. object: Name of object
107098** 3. Database: Database name (e.g. "main")
107099** 4. Table: Table name
107100** 5. iCol: Index of column to rename
107101** 6. zNew: New column name
107102** 7. bQuote: Non-zero if the new column name should be quoted.
107103** 8. bTemp: True if zSql comes from temp schema
107104**
107105** Do a column rename operation on the CREATE statement given in zSql.
107106** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
107107** into zNew. The name should be quoted if bQuote is true.
107108**
107109** This function is used internally by the ALTER TABLE RENAME COLUMN command.
107110** It is only accessible to SQL created using sqlite3NestedParse(). It is
107111** not reachable from ordinary SQL passed into sqlite3_prepare().
107112*/
107113static void renameColumnFunc(
107114 sqlite3_context *context,
107115 int NotUsed,
107116 sqlite3_value **argv
107117){
107118 sqlite3 *db = sqlite3_context_db_handle(context);
107119 RenameCtx sCtx;
107120 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
107121 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
107122 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
107123 int iCol = sqlite3_value_int(argv[5]);
107124 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
107125 int bQuote = sqlite3_value_int(argv[7]);
107126 int bTemp = sqlite3_value_int(argv[8]);
107127 const char *zOld;
107128 int rc;
107129 Parse sParse;
107130 Walker sWalker;
107131 Index *pIdx;
107132 int i;
107133 Table *pTab;
107134#ifndef SQLITE_OMIT_AUTHORIZATION
107135 sqlite3_xauth xAuth = db->xAuth;
107136#endif
107137
107138 UNUSED_PARAMETER(NotUsed);
107139 if( zSql==0 ) return;
107140 if( zTable==0 ) return;
107141 if( zNew==0 ) return;
107142 if( iCol<0 ) return;
107143 sqlite3BtreeEnterAll(db);
107144 pTab = sqlite3FindTable(db, zTable, zDb);
107145 if( pTab==0 || iCol>=pTab->nCol ){
107146 sqlite3BtreeLeaveAll(db);
107147 return;
107148 }
107149 zOld = pTab->aCol[iCol].zName;
107150 memset(&sCtx, 0, sizeof(sCtx));
107151 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
107152
107153#ifndef SQLITE_OMIT_AUTHORIZATION
107154 db->xAuth = 0;
107155#endif
107156 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
107157
107158 /* Find tokens that need to be replaced. */
107159 memset(&sWalker, 0, sizeof(Walker));
107160 sWalker.pParse = &sParse;
107161 sWalker.xExprCallback = renameColumnExprCb;
107162 sWalker.xSelectCallback = renameColumnSelectCb;
107163 sWalker.u.pRename = &sCtx;
107164
107165 sCtx.pTab = pTab;
107166 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
107167 if( sParse.pNewTable ){
107168 Select *pSelect = sParse.pNewTable->pSelect;
107169 if( pSelect ){
107170 pSelect->selFlags &= ~SF_View;
107171 sParse.rc = SQLITE_OK;
107172 sqlite3SelectPrep(&sParse, pSelect, 0);
107173 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
107174 if( rc==SQLITE_OK ){
107175 sqlite3WalkSelect(&sWalker, pSelect);
107176 }
107177 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
107178 }else{
107179 /* A regular table */
107180 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
107181 FKey *pFKey;
107182 assert( sParse.pNewTable->pSelect==0 );
107183 sCtx.pTab = sParse.pNewTable;
107184 if( bFKOnly==0 ){
107185 renameTokenFind(
107186 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
107187 );
107188 if( sCtx.iCol<0 ){
107189 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
107190 }
107191 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
107192 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
107193 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
107194 }
107195 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
107196 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
107197 }
107198 }
107199#ifndef SQLITE_OMIT_GENERATED_COLUMNS
107200 for(i=0; i<sParse.pNewTable->nCol; i++){
107201 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
107202 }
107203#endif
107204
107205 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
107206 for(i=0; i<pFKey->nCol; i++){
107207 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
107208 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
107209 }
107210 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
107211 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
107212 ){
107213 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
107214 }
107215 }
107216 }
107217 }
107218 }else if( sParse.pNewIndex ){
107219 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
107220 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
107221 }else{
107222 /* A trigger */
107223 TriggerStep *pStep;
107224 rc = renameResolveTrigger(&sParse);
107225 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
107226
107227 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
107228 if( pStep->zTarget ){
107229 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
107230 if( pTarget==pTab ){
107231 if( pStep->pUpsert ){
107232 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
107233 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
107234 }
107235 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
107236 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
107237 }
107238 }
107239 }
107240
107241
107242 /* Find tokens to edit in UPDATE OF clause */
107243 if( sParse.pTriggerTab==pTab ){
107244 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
107245 }
107246
107247 /* Find tokens to edit in various expressions and selects */
107248 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
107249 }
107250
107251 assert( rc==SQLITE_OK );
107252 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
107253
107254renameColumnFunc_done:
107255 if( rc!=SQLITE_OK ){
107256 if( sParse.zErrMsg ){
107257 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
107258 }else{
107259 sqlite3_result_error_code(context, rc);
107260 }
107261 }
107262
107263 renameParseCleanup(&sParse);
107264 renameTokenFree(db, sCtx.pList);
107265#ifndef SQLITE_OMIT_AUTHORIZATION
107266 db->xAuth = xAuth;
107267#endif
107268 sqlite3BtreeLeaveAll(db);
107269}
107270
107271/*
107272** Walker expression callback used by "RENAME TABLE".
107273*/
107274static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
107275 RenameCtx *p = pWalker->u.pRename;
107276 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
107277 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
107278 }
107279 return WRC_Continue;
107280}
107281
107282/*
107283** Walker select callback used by "RENAME TABLE".
107284*/
107285static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
107286 int i;
107287 RenameCtx *p = pWalker->u.pRename;
107288 SrcList *pSrc = pSelect->pSrc;
107289 if( pSelect->selFlags & SF_View ) return WRC_Prune;
107290 if( pSrc==0 ){
107291 assert( pWalker->pParse->db->mallocFailed );
107292 return WRC_Abort;
107293 }
107294 for(i=0; i<pSrc->nSrc; i++){
107295 struct SrcList_item *pItem = &pSrc->a[i];
107296 if( pItem->pTab==p->pTab ){
107297 renameTokenFind(pWalker->pParse, p, pItem->zName);
107298 }
107299 }
107300 renameWalkWith(pWalker, pSelect);
107301
107302 return WRC_Continue;
107303}
107304
107305
107306/*
107307** This C function implements an SQL user function that is used by SQL code
107308** generated by the ALTER TABLE ... RENAME command to modify the definition
107309** of any foreign key constraints that use the table being renamed as the
107310** parent table. It is passed three arguments:
107311**
107312** 0: The database containing the table being renamed.
107313** 1. type: Type of object ("table", "view" etc.)
107314** 2. object: Name of object
107315** 3: The complete text of the schema statement being modified,
107316** 4: The old name of the table being renamed, and
107317** 5: The new name of the table being renamed.
107318** 6: True if the schema statement comes from the temp db.
107319**
107320** It returns the new schema statement. For example:
107321**
107322** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
107323** -> 'CREATE TABLE t1(a REFERENCES t3)'
107324*/
107325static void renameTableFunc(
107326 sqlite3_context *context,
107327 int NotUsed,
107328 sqlite3_value **argv
107329){
107330 sqlite3 *db = sqlite3_context_db_handle(context);
107331 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
107332 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
107333 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
107334 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
107335 int bTemp = sqlite3_value_int(argv[6]);
107336 UNUSED_PARAMETER(NotUsed);
107337
107338 if( zInput && zOld && zNew ){
107339 Parse sParse;
107340 int rc;
107341 int bQuote = 1;
107342 RenameCtx sCtx;
107343 Walker sWalker;
107344
107345#ifndef SQLITE_OMIT_AUTHORIZATION
107346 sqlite3_xauth xAuth = db->xAuth;
107347 db->xAuth = 0;
107348#endif
107349
107350 sqlite3BtreeEnterAll(db);
107351
107352 memset(&sCtx, 0, sizeof(RenameCtx));
107353 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
107354 memset(&sWalker, 0, sizeof(Walker));
107355 sWalker.pParse = &sParse;
107356 sWalker.xExprCallback = renameTableExprCb;
107357 sWalker.xSelectCallback = renameTableSelectCb;
107358 sWalker.u.pRename = &sCtx;
107359
107360 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
107361
107362 if( rc==SQLITE_OK ){
107363 int isLegacy = (db->flags & SQLITE_LegacyAlter);
107364 if( sParse.pNewTable ){
107365 Table *pTab = sParse.pNewTable;
107366
107367 if( pTab->pSelect ){
107368 if( isLegacy==0 ){
107369 Select *pSelect = pTab->pSelect;
107370 NameContext sNC;
107371 memset(&sNC, 0, sizeof(sNC));
107372 sNC.pParse = &sParse;
107373
107374 assert( pSelect->selFlags & SF_View );
107375 pSelect->selFlags &= ~SF_View;
107376 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
107377 if( sParse.nErr ){
107378 rc = sParse.rc;
107379 }else{
107380 sqlite3WalkSelect(&sWalker, pTab->pSelect);
107381 }
107382 }
107383 }else{
107384 /* Modify any FK definitions to point to the new table. */
107385#ifndef SQLITE_OMIT_FOREIGN_KEY
107386 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
107387 FKey *pFKey;
107388 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
107389 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
107390 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
107391 }
107392 }
107393 }
107394#endif
107395
107396 /* If this is the table being altered, fix any table refs in CHECK
107397 ** expressions. Also update the name that appears right after the
107398 ** "CREATE [VIRTUAL] TABLE" bit. */
107399 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
107400 sCtx.pTab = pTab;
107401 if( isLegacy==0 ){
107402 sqlite3WalkExprList(&sWalker, pTab->pCheck);
107403 }
107404 renameTokenFind(&sParse, &sCtx, pTab->zName);
107405 }
107406 }
107407 }
107408
107409 else if( sParse.pNewIndex ){
107410 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
107411 if( isLegacy==0 ){
107412 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
107413 }
107414 }
107415
107416#ifndef SQLITE_OMIT_TRIGGER
107417 else{
107418 Trigger *pTrigger = sParse.pNewTrigger;
107419 TriggerStep *pStep;
107420 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
107421 && sCtx.pTab->pSchema==pTrigger->pTabSchema
107422 ){
107423 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
107424 }
107425
107426 if( isLegacy==0 ){
107427 rc = renameResolveTrigger(&sParse);
107428 if( rc==SQLITE_OK ){
107429 renameWalkTrigger(&sWalker, pTrigger);
107430 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
107431 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
107432 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
107433 }
107434 }
107435 }
107436 }
107437 }
107438#endif
107439 }
107440
107441 if( rc==SQLITE_OK ){
107442 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
107443 }
107444 if( rc!=SQLITE_OK ){
107445 if( sParse.zErrMsg ){
107446 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
107447 }else{
107448 sqlite3_result_error_code(context, rc);
107449 }
107450 }
107451
107452 renameParseCleanup(&sParse);
107453 renameTokenFree(db, sCtx.pList);
107454 sqlite3BtreeLeaveAll(db);
107455#ifndef SQLITE_OMIT_AUTHORIZATION
107456 db->xAuth = xAuth;
107457#endif
107458 }
107459
107460 return;
107461}
107462
107463/*
107464** An SQL user function that checks that there are no parse or symbol
107465** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
107466** After an ALTER TABLE .. RENAME operation is performed and the schema
107467** reloaded, this function is called on each SQL statement in the schema
107468** to ensure that it is still usable.
107469**
107470** 0: Database name ("main", "temp" etc.).
107471** 1: SQL statement.
107472** 2: Object type ("view", "table", "trigger" or "index").
107473** 3: Object name.
107474** 4: True if object is from temp schema.
107475**
107476** Unless it finds an error, this function normally returns NULL. However, it
107477** returns integer value 1 if:
107478**
107479** * the SQL argument creates a trigger, and
107480** * the table that the trigger is attached to is in database zDb.
107481*/
107482static void renameTableTest(
107483 sqlite3_context *context,
107484 int NotUsed,
107485 sqlite3_value **argv
107486){
107487 sqlite3 *db = sqlite3_context_db_handle(context);
107488 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
107489 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
107490 int bTemp = sqlite3_value_int(argv[4]);
107491 int isLegacy = (db->flags & SQLITE_LegacyAlter);
107492
107493#ifndef SQLITE_OMIT_AUTHORIZATION
107494 sqlite3_xauth xAuth = db->xAuth;
107495 db->xAuth = 0;
107496#endif
107497
107498 UNUSED_PARAMETER(NotUsed);
107499 if( zDb && zInput ){
107500 int rc;
107501 Parse sParse;
107502 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
107503 if( rc==SQLITE_OK ){
107504 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
107505 NameContext sNC;
107506 memset(&sNC, 0, sizeof(sNC));
107507 sNC.pParse = &sParse;
107508 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
107509 if( sParse.nErr ) rc = sParse.rc;
107510 }
107511
107512 else if( sParse.pNewTrigger ){
107513 if( isLegacy==0 ){
107514 rc = renameResolveTrigger(&sParse);
107515 }
107516 if( rc==SQLITE_OK ){
107517 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
107518 int i2 = sqlite3FindDbName(db, zDb);
107519 if( i1==i2 ) sqlite3_result_int(context, 1);
107520 }
107521 }
107522 }
107523
107524 if( rc!=SQLITE_OK ){
107525 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
107526 }
107527 renameParseCleanup(&sParse);
107528 }
107529
107530#ifndef SQLITE_OMIT_AUTHORIZATION
107531 db->xAuth = xAuth;
107532#endif
107533}
107534
107535/*
107536** Register built-in functions used to help implement ALTER TABLE
107537*/
107538SQLITE_PRIVATE void sqlite3AlterFunctions(void){
107539 static FuncDef aAlterTableFuncs[] = {
107540 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
107541 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
107542 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest),
107543 };
107544 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
107545}
107546#endif /* SQLITE_ALTER_TABLE */
107547
107548/************** End of alter.c ***********************************************/
107549/************** Begin file analyze.c *****************************************/
107550/*
107551** 2005-07-08
107552**
107553** The author disclaims copyright to this source code. In place of
107554** a legal notice, here is a blessing:
107555**
107556** May you do good and not evil.
107557** May you find forgiveness for yourself and forgive others.
107558** May you share freely, never taking more than you give.
107559**
107560*************************************************************************
107561** This file contains code associated with the ANALYZE command.
107562**
107563** The ANALYZE command gather statistics about the content of tables
107564** and indices. These statistics are made available to the query planner
107565** to help it make better decisions about how to perform queries.
107566**
107567** The following system tables are or have been supported:
107568**
107569** CREATE TABLE sqlite_stat1(tbl, idx, stat);
107570** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
107571** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
107572** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
107573**
107574** Additional tables might be added in future releases of SQLite.
107575** The sqlite_stat2 table is not created or used unless the SQLite version
107576** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
107577** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
107578** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
107579** created and used by SQLite versions 3.7.9 through 3.29.0 when
107580** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
107581** is a superset of sqlite_stat2 and is also now deprecated. The
107582** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
107583** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
107584** versions 3.8.1 and later. STAT4 is the only variant that is still
107585** supported.
107586**
107587** For most applications, sqlite_stat1 provides all the statistics required
107588** for the query planner to make good choices.
107589**
107590** Format of sqlite_stat1:
107591**
107592** There is normally one row per index, with the index identified by the
107593** name in the idx column. The tbl column is the name of the table to
107594** which the index belongs. In each such row, the stat column will be
107595** a string consisting of a list of integers. The first integer in this
107596** list is the number of rows in the index. (This is the same as the
107597** number of rows in the table, except for partial indices.) The second
107598** integer is the average number of rows in the index that have the same
107599** value in the first column of the index. The third integer is the average
107600** number of rows in the index that have the same value for the first two
107601** columns. The N-th integer (for N>1) is the average number of rows in
107602** the index which have the same value for the first N-1 columns. For
107603** a K-column index, there will be K+1 integers in the stat column. If
107604** the index is unique, then the last integer will be 1.
107605**
107606** The list of integers in the stat column can optionally be followed
107607** by the keyword "unordered". The "unordered" keyword, if it is present,
107608** must be separated from the last integer by a single space. If the
107609** "unordered" keyword is present, then the query planner assumes that
107610** the index is unordered and will not use the index for a range query.
107611**
107612** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
107613** column contains a single integer which is the (estimated) number of
107614** rows in the table identified by sqlite_stat1.tbl.
107615**
107616** Format of sqlite_stat2:
107617**
107618** The sqlite_stat2 is only created and is only used if SQLite is compiled
107619** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
107620** 3.6.18 and 3.7.8. The "stat2" table contains additional information
107621** about the distribution of keys within an index. The index is identified by
107622** the "idx" column and the "tbl" column is the name of the table to which
107623** the index belongs. There are usually 10 rows in the sqlite_stat2
107624** table for each index.
107625**
107626** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
107627** inclusive are samples of the left-most key value in the index taken at
107628** evenly spaced points along the index. Let the number of samples be S
107629** (10 in the standard build) and let C be the number of rows in the index.
107630** Then the sampled rows are given by:
107631**
107632** rownumber = (i*C*2 + C)/(S*2)
107633**
107634** For i between 0 and S-1. Conceptually, the index space is divided into
107635** S uniform buckets and the samples are the middle row from each bucket.
107636**
107637** The format for sqlite_stat2 is recorded here for legacy reference. This
107638** version of SQLite does not support sqlite_stat2. It neither reads nor
107639** writes the sqlite_stat2 table. This version of SQLite only supports
107640** sqlite_stat3.
107641**
107642** Format for sqlite_stat3:
107643**
107644** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
107645** sqlite_stat4 format will be described first. Further information
107646** about sqlite_stat3 follows the sqlite_stat4 description.
107647**
107648** Format for sqlite_stat4:
107649**
107650** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
107651** to aid the query planner in choosing good indices based on the values
107652** that indexed columns are compared against in the WHERE clauses of
107653** queries.
107654**
107655** The sqlite_stat4 table contains multiple entries for each index.
107656** The idx column names the index and the tbl column is the table of the
107657** index. If the idx and tbl columns are the same, then the sample is
107658** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
107659** binary encoding of a key from the index. The nEq column is a
107660** list of integers. The first integer is the approximate number
107661** of entries in the index whose left-most column exactly matches
107662** the left-most column of the sample. The second integer in nEq
107663** is the approximate number of entries in the index where the
107664** first two columns match the first two columns of the sample.
107665** And so forth. nLt is another list of integers that show the approximate
107666** number of entries that are strictly less than the sample. The first
107667** integer in nLt contains the number of entries in the index where the
107668** left-most column is less than the left-most column of the sample.
107669** The K-th integer in the nLt entry is the number of index entries
107670** where the first K columns are less than the first K columns of the
107671** sample. The nDLt column is like nLt except that it contains the
107672** number of distinct entries in the index that are less than the
107673** sample.
107674**
107675** There can be an arbitrary number of sqlite_stat4 entries per index.
107676** The ANALYZE command will typically generate sqlite_stat4 tables
107677** that contain between 10 and 40 samples which are distributed across
107678** the key space, though not uniformly, and which include samples with
107679** large nEq values.
107680**
107681** Format for sqlite_stat3 redux:
107682**
107683** The sqlite_stat3 table is like sqlite_stat4 except that it only
107684** looks at the left-most column of the index. The sqlite_stat3.sample
107685** column contains the actual value of the left-most column instead
107686** of a blob encoding of the complete index key as is found in
107687** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
107688** all contain just a single integer which is the same as the first
107689** integer in the equivalent columns in sqlite_stat4.
107690*/
107691#ifndef SQLITE_OMIT_ANALYZE
107692/* #include "sqliteInt.h" */
107693
107694#if defined(SQLITE_ENABLE_STAT4)
107695# define IsStat4 1
107696#else
107697# define IsStat4 0
107698# undef SQLITE_STAT4_SAMPLES
107699# define SQLITE_STAT4_SAMPLES 1
107700#endif
107701
107702/*
107703** This routine generates code that opens the sqlite_statN tables.
107704** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
107705** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
107706** appropriate compile-time options are provided.
107707**
107708** If the sqlite_statN tables do not previously exist, it is created.
107709**
107710** Argument zWhere may be a pointer to a buffer containing a table name,
107711** or it may be a NULL pointer. If it is not NULL, then all entries in
107712** the sqlite_statN tables associated with the named table are deleted.
107713** If zWhere==0, then code is generated to delete all stat table entries.
107714*/
107715static void openStatTable(
107716 Parse *pParse, /* Parsing context */
107717 int iDb, /* The database we are looking in */
107718 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
107719 const char *zWhere, /* Delete entries for this table or index */
107720 const char *zWhereType /* Either "tbl" or "idx" */
107721){
107722 static const struct {
107723 const char *zName;
107724 const char *zCols;
107725 } aTable[] = {
107726 { "sqlite_stat1", "tbl,idx,stat" },
107727#if defined(SQLITE_ENABLE_STAT4)
107728 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
107729#else
107730 { "sqlite_stat4", 0 },
107731#endif
107732 { "sqlite_stat3", 0 },
107733 };
107734 int i;
107735 sqlite3 *db = pParse->db;
107736 Db *pDb;
107737 Vdbe *v = sqlite3GetVdbe(pParse);
107738 u32 aRoot[ArraySize(aTable)];
107739 u8 aCreateTbl[ArraySize(aTable)];
107740#ifdef SQLITE_ENABLE_STAT4
107741 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1;
107742#else
107743 const int nToOpen = 1;
107744#endif
107745
107746 if( v==0 ) return;
107747 assert( sqlite3BtreeHoldsAllMutexes(db) );
107748 assert( sqlite3VdbeDb(v)==db );
107749 pDb = &db->aDb[iDb];
107750
107751 /* Create new statistic tables if they do not exist, or clear them
107752 ** if they do already exist.
107753 */
107754 for(i=0; i<ArraySize(aTable); i++){
107755 const char *zTab = aTable[i].zName;
107756 Table *pStat;
107757 aCreateTbl[i] = 0;
107758 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
107759 if( i<nToOpen ){
107760 /* The sqlite_statN table does not exist. Create it. Note that a
107761 ** side-effect of the CREATE TABLE statement is to leave the rootpage
107762 ** of the new table in register pParse->regRoot. This is important
107763 ** because the OpenWrite opcode below will be needing it. */
107764 sqlite3NestedParse(pParse,
107765 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
107766 );
107767 aRoot[i] = (u32)pParse->regRoot;
107768 aCreateTbl[i] = OPFLAG_P2ISREG;
107769 }
107770 }else{
107771 /* The table already exists. If zWhere is not NULL, delete all entries
107772 ** associated with the table zWhere. If zWhere is NULL, delete the
107773 ** entire contents of the table. */
107774 aRoot[i] = pStat->tnum;
107775 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
107776 if( zWhere ){
107777 sqlite3NestedParse(pParse,
107778 "DELETE FROM %Q.%s WHERE %s=%Q",
107779 pDb->zDbSName, zTab, zWhereType, zWhere
107780 );
107781#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
107782 }else if( db->xPreUpdateCallback ){
107783 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
107784#endif
107785 }else{
107786 /* The sqlite_stat[134] table already exists. Delete all rows. */
107787 sqlite3VdbeAddOp2(v, OP_Clear, (int)aRoot[i], iDb);
107788 }
107789 }
107790 }
107791
107792 /* Open the sqlite_stat[134] tables for writing. */
107793 for(i=0; i<nToOpen; i++){
107794 assert( i<ArraySize(aTable) );
107795 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, (int)aRoot[i], iDb, 3);
107796 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
107797 VdbeComment((v, aTable[i].zName));
107798 }
107799}
107800
107801/*
107802** Recommended number of samples for sqlite_stat4
107803*/
107804#ifndef SQLITE_STAT4_SAMPLES
107805# define SQLITE_STAT4_SAMPLES 24
107806#endif
107807
107808/*
107809** Three SQL functions - stat_init(), stat_push(), and stat_get() -
107810** share an instance of the following structure to hold their state
107811** information.
107812*/
107813typedef struct StatAccum StatAccum;
107814typedef struct StatSample StatSample;
107815struct StatSample {
107816 tRowcnt *anEq; /* sqlite_stat4.nEq */
107817 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
107818#ifdef SQLITE_ENABLE_STAT4
107819 tRowcnt *anLt; /* sqlite_stat4.nLt */
107820 union {
107821 i64 iRowid; /* Rowid in main table of the key */
107822 u8 *aRowid; /* Key for WITHOUT ROWID tables */
107823 } u;
107824 u32 nRowid; /* Sizeof aRowid[] */
107825 u8 isPSample; /* True if a periodic sample */
107826 int iCol; /* If !isPSample, the reason for inclusion */
107827 u32 iHash; /* Tiebreaker hash */
107828#endif
107829};
107830struct StatAccum {
107831 sqlite3 *db; /* Database connection, for malloc() */
107832 tRowcnt nEst; /* Estimated number of rows */
107833 tRowcnt nRow; /* Number of rows visited so far */
107834 int nLimit; /* Analysis row-scan limit */
107835 int nCol; /* Number of columns in index + pk/rowid */
107836 int nKeyCol; /* Number of index columns w/o the pk/rowid */
107837 u8 nSkipAhead; /* Number of times of skip-ahead */
107838 StatSample current; /* Current row as a StatSample */
107839#ifdef SQLITE_ENABLE_STAT4
107840 tRowcnt nPSample; /* How often to do a periodic sample */
107841 int mxSample; /* Maximum number of samples to accumulate */
107842 u32 iPrn; /* Pseudo-random number used for sampling */
107843 StatSample *aBest; /* Array of nCol best samples */
107844 int iMin; /* Index in a[] of entry with minimum score */
107845 int nSample; /* Current number of samples */
107846 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
107847 int iGet; /* Index of current sample accessed by stat_get() */
107848 StatSample *a; /* Array of mxSample StatSample objects */
107849#endif
107850};
107851
107852/* Reclaim memory used by a StatSample
107853*/
107854#ifdef SQLITE_ENABLE_STAT4
107855static void sampleClear(sqlite3 *db, StatSample *p){
107856 assert( db!=0 );
107857 if( p->nRowid ){
107858 sqlite3DbFree(db, p->u.aRowid);
107859 p->nRowid = 0;
107860 }
107861}
107862#endif
107863
107864/* Initialize the BLOB value of a ROWID
107865*/
107866#ifdef SQLITE_ENABLE_STAT4
107867static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
107868 assert( db!=0 );
107869 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
107870 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
107871 if( p->u.aRowid ){
107872 p->nRowid = n;
107873 memcpy(p->u.aRowid, pData, n);
107874 }else{
107875 p->nRowid = 0;
107876 }
107877}
107878#endif
107879
107880/* Initialize the INTEGER value of a ROWID.
107881*/
107882#ifdef SQLITE_ENABLE_STAT4
107883static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
107884 assert( db!=0 );
107885 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
107886 p->nRowid = 0;
107887 p->u.iRowid = iRowid;
107888}
107889#endif
107890
107891
107892/*
107893** Copy the contents of object (*pFrom) into (*pTo).
107894*/
107895#ifdef SQLITE_ENABLE_STAT4
107896static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
107897 pTo->isPSample = pFrom->isPSample;
107898 pTo->iCol = pFrom->iCol;
107899 pTo->iHash = pFrom->iHash;
107900 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
107901 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
107902 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
107903 if( pFrom->nRowid ){
107904 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
107905 }else{
107906 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
107907 }
107908}
107909#endif
107910
107911/*
107912** Reclaim all memory of a StatAccum structure.
107913*/
107914static void statAccumDestructor(void *pOld){
107915 StatAccum *p = (StatAccum*)pOld;
107916#ifdef SQLITE_ENABLE_STAT4
107917 if( p->mxSample ){
107918 int i;
107919 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
107920 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
107921 sampleClear(p->db, &p->current);
107922 }
107923#endif
107924 sqlite3DbFree(p->db, p);
107925}
107926
107927/*
107928** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
107929** are:
107930** N: The number of columns in the index including the rowid/pk (note 1)
107931** K: The number of columns in the index excluding the rowid/pk.
107932** C: Estimated number of rows in the index
107933** L: A limit on the number of rows to scan, or 0 for no-limit
107934**
107935** Note 1: In the special case of the covering index that implements a
107936** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
107937** total number of columns in the table.
107938**
107939** For indexes on ordinary rowid tables, N==K+1. But for indexes on
107940** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
107941** PRIMARY KEY of the table. The covering index that implements the
107942** original WITHOUT ROWID table as N==K as a special case.
107943**
107944** This routine allocates the StatAccum object in heap memory. The return
107945** value is a pointer to the StatAccum object. The datatype of the
107946** return value is BLOB, but it is really just a pointer to the StatAccum
107947** object.
107948*/
107949static void statInit(
107950 sqlite3_context *context,
107951 int argc,
107952 sqlite3_value **argv
107953){
107954 StatAccum *p;
107955 int nCol; /* Number of columns in index being sampled */
107956 int nKeyCol; /* Number of key columns */
107957 int nColUp; /* nCol rounded up for alignment */
107958 int n; /* Bytes of space to allocate */
107959 sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */
107960#ifdef SQLITE_ENABLE_STAT4
107961 /* Maximum number of samples. 0 if STAT4 data is not collected */
107962 int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0;
107963#endif
107964
107965 /* Decode the three function arguments */
107966 UNUSED_PARAMETER(argc);
107967 nCol = sqlite3_value_int(argv[0]);
107968 assert( nCol>0 );
107969 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
107970 nKeyCol = sqlite3_value_int(argv[1]);
107971 assert( nKeyCol<=nCol );
107972 assert( nKeyCol>0 );
107973
107974 /* Allocate the space required for the StatAccum object */
107975 n = sizeof(*p)
107976 + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */
107977 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
107978#ifdef SQLITE_ENABLE_STAT4
107979 if( mxSample ){
107980 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
107981 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
107982 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
107983 }
107984#endif
107985 db = sqlite3_context_db_handle(context);
107986 p = sqlite3DbMallocZero(db, n);
107987 if( p==0 ){
107988 sqlite3_result_error_nomem(context);
107989 return;
107990 }
107991
107992 p->db = db;
107993 p->nEst = sqlite3_value_int64(argv[2]);
107994 p->nRow = 0;
107995 p->nLimit = sqlite3_value_int64(argv[3]);
107996 p->nCol = nCol;
107997 p->nKeyCol = nKeyCol;
107998 p->nSkipAhead = 0;
107999 p->current.anDLt = (tRowcnt*)&p[1];
108000 p->current.anEq = &p->current.anDLt[nColUp];
108001
108002#ifdef SQLITE_ENABLE_STAT4
108003 p->mxSample = p->nLimit==0 ? mxSample : 0;
108004 if( mxSample ){
108005 u8 *pSpace; /* Allocated space not yet assigned */
108006 int i; /* Used to iterate through p->aSample[] */
108007
108008 p->iGet = -1;
108009 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
108010 p->current.anLt = &p->current.anEq[nColUp];
108011 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
108012
108013 /* Set up the StatAccum.a[] and aBest[] arrays */
108014 p->a = (struct StatSample*)&p->current.anLt[nColUp];
108015 p->aBest = &p->a[mxSample];
108016 pSpace = (u8*)(&p->a[mxSample+nCol]);
108017 for(i=0; i<(mxSample+nCol); i++){
108018 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
108019 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
108020 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
108021 }
108022 assert( (pSpace - (u8*)p)==n );
108023
108024 for(i=0; i<nCol; i++){
108025 p->aBest[i].iCol = i;
108026 }
108027 }
108028#endif
108029
108030 /* Return a pointer to the allocated object to the caller. Note that
108031 ** only the pointer (the 2nd parameter) matters. The size of the object
108032 ** (given by the 3rd parameter) is never used and can be any positive
108033 ** value. */
108034 sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
108035}
108036static const FuncDef statInitFuncdef = {
108037 4, /* nArg */
108038 SQLITE_UTF8, /* funcFlags */
108039 0, /* pUserData */
108040 0, /* pNext */
108041 statInit, /* xSFunc */
108042 0, /* xFinalize */
108043 0, 0, /* xValue, xInverse */
108044 "stat_init", /* zName */
108045 {0}
108046};
108047
108048#ifdef SQLITE_ENABLE_STAT4
108049/*
108050** pNew and pOld are both candidate non-periodic samples selected for
108051** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
108052** considering only any trailing columns and the sample hash value, this
108053** function returns true if sample pNew is to be preferred over pOld.
108054** In other words, if we assume that the cardinalities of the selected
108055** column for pNew and pOld are equal, is pNew to be preferred over pOld.
108056**
108057** This function assumes that for each argument sample, the contents of
108058** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
108059*/
108060static int sampleIsBetterPost(
108061 StatAccum *pAccum,
108062 StatSample *pNew,
108063 StatSample *pOld
108064){
108065 int nCol = pAccum->nCol;
108066 int i;
108067 assert( pNew->iCol==pOld->iCol );
108068 for(i=pNew->iCol+1; i<nCol; i++){
108069 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
108070 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
108071 }
108072 if( pNew->iHash>pOld->iHash ) return 1;
108073 return 0;
108074}
108075#endif
108076
108077#ifdef SQLITE_ENABLE_STAT4
108078/*
108079** Return true if pNew is to be preferred over pOld.
108080**
108081** This function assumes that for each argument sample, the contents of
108082** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
108083*/
108084static int sampleIsBetter(
108085 StatAccum *pAccum,
108086 StatSample *pNew,
108087 StatSample *pOld
108088){
108089 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
108090 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
108091
108092 assert( pOld->isPSample==0 && pNew->isPSample==0 );
108093 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
108094
108095 if( (nEqNew>nEqOld) ) return 1;
108096 if( nEqNew==nEqOld ){
108097 if( pNew->iCol<pOld->iCol ) return 1;
108098 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
108099 }
108100 return 0;
108101}
108102
108103/*
108104** Copy the contents of sample *pNew into the p->a[] array. If necessary,
108105** remove the least desirable sample from p->a[] to make room.
108106*/
108107static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
108108 StatSample *pSample = 0;
108109 int i;
108110
108111 assert( IsStat4 || nEqZero==0 );
108112
108113 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
108114 ** values in the anEq[] array of any sample in StatAccum.a[]. In
108115 ** other words, if nMaxEqZero is n, then it is guaranteed that there
108116 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
108117 if( nEqZero>p->nMaxEqZero ){
108118 p->nMaxEqZero = nEqZero;
108119 }
108120 if( pNew->isPSample==0 ){
108121 StatSample *pUpgrade = 0;
108122 assert( pNew->anEq[pNew->iCol]>0 );
108123
108124 /* This sample is being added because the prefix that ends in column
108125 ** iCol occurs many times in the table. However, if we have already
108126 ** added a sample that shares this prefix, there is no need to add
108127 ** this one. Instead, upgrade the priority of the highest priority
108128 ** existing sample that shares this prefix. */
108129 for(i=p->nSample-1; i>=0; i--){
108130 StatSample *pOld = &p->a[i];
108131 if( pOld->anEq[pNew->iCol]==0 ){
108132 if( pOld->isPSample ) return;
108133 assert( pOld->iCol>pNew->iCol );
108134 assert( sampleIsBetter(p, pNew, pOld) );
108135 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
108136 pUpgrade = pOld;
108137 }
108138 }
108139 }
108140 if( pUpgrade ){
108141 pUpgrade->iCol = pNew->iCol;
108142 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
108143 goto find_new_min;
108144 }
108145 }
108146
108147 /* If necessary, remove sample iMin to make room for the new sample. */
108148 if( p->nSample>=p->mxSample ){
108149 StatSample *pMin = &p->a[p->iMin];
108150 tRowcnt *anEq = pMin->anEq;
108151 tRowcnt *anLt = pMin->anLt;
108152 tRowcnt *anDLt = pMin->anDLt;
108153 sampleClear(p->db, pMin);
108154 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
108155 pSample = &p->a[p->nSample-1];
108156 pSample->nRowid = 0;
108157 pSample->anEq = anEq;
108158 pSample->anDLt = anDLt;
108159 pSample->anLt = anLt;
108160 p->nSample = p->mxSample-1;
108161 }
108162
108163 /* The "rows less-than" for the rowid column must be greater than that
108164 ** for the last sample in the p->a[] array. Otherwise, the samples would
108165 ** be out of order. */
108166 assert( p->nSample==0
108167 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
108168
108169 /* Insert the new sample */
108170 pSample = &p->a[p->nSample];
108171 sampleCopy(p, pSample, pNew);
108172 p->nSample++;
108173
108174 /* Zero the first nEqZero entries in the anEq[] array. */
108175 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
108176
108177find_new_min:
108178 if( p->nSample>=p->mxSample ){
108179 int iMin = -1;
108180 for(i=0; i<p->mxSample; i++){
108181 if( p->a[i].isPSample ) continue;
108182 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
108183 iMin = i;
108184 }
108185 }
108186 assert( iMin>=0 );
108187 p->iMin = iMin;
108188 }
108189}
108190#endif /* SQLITE_ENABLE_STAT4 */
108191
108192#ifdef SQLITE_ENABLE_STAT4
108193/*
108194** Field iChng of the index being scanned has changed. So at this point
108195** p->current contains a sample that reflects the previous row of the
108196** index. The value of anEq[iChng] and subsequent anEq[] elements are
108197** correct at this point.
108198*/
108199static void samplePushPrevious(StatAccum *p, int iChng){
108200 int i;
108201
108202 /* Check if any samples from the aBest[] array should be pushed
108203 ** into IndexSample.a[] at this point. */
108204 for(i=(p->nCol-2); i>=iChng; i--){
108205 StatSample *pBest = &p->aBest[i];
108206 pBest->anEq[i] = p->current.anEq[i];
108207 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
108208 sampleInsert(p, pBest, i);
108209 }
108210 }
108211
108212 /* Check that no sample contains an anEq[] entry with an index of
108213 ** p->nMaxEqZero or greater set to zero. */
108214 for(i=p->nSample-1; i>=0; i--){
108215 int j;
108216 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
108217 }
108218
108219 /* Update the anEq[] fields of any samples already collected. */
108220 if( iChng<p->nMaxEqZero ){
108221 for(i=p->nSample-1; i>=0; i--){
108222 int j;
108223 for(j=iChng; j<p->nCol; j++){
108224 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
108225 }
108226 }
108227 p->nMaxEqZero = iChng;
108228 }
108229}
108230#endif /* SQLITE_ENABLE_STAT4 */
108231
108232/*
108233** Implementation of the stat_push SQL function: stat_push(P,C,R)
108234** Arguments:
108235**
108236** P Pointer to the StatAccum object created by stat_init()
108237** C Index of left-most column to differ from previous row
108238** R Rowid for the current row. Might be a key record for
108239** WITHOUT ROWID tables.
108240**
108241** The purpose of this routine is to collect statistical data and/or
108242** samples from the index being analyzed into the StatAccum object.
108243** The stat_get() SQL function will be used afterwards to
108244** retrieve the information gathered.
108245**
108246** This SQL function usually returns NULL, but might return an integer
108247** if it wants the byte-code to do special processing.
108248**
108249** The R parameter is only used for STAT4
108250*/
108251static void statPush(
108252 sqlite3_context *context,
108253 int argc,
108254 sqlite3_value **argv
108255){
108256 int i;
108257
108258 /* The three function arguments */
108259 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
108260 int iChng = sqlite3_value_int(argv[1]);
108261
108262 UNUSED_PARAMETER( argc );
108263 UNUSED_PARAMETER( context );
108264 assert( p->nCol>0 );
108265 assert( iChng<p->nCol );
108266
108267 if( p->nRow==0 ){
108268 /* This is the first call to this function. Do initialization. */
108269 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
108270 }else{
108271 /* Second and subsequent calls get processed here */
108272#ifdef SQLITE_ENABLE_STAT4
108273 if( p->mxSample ) samplePushPrevious(p, iChng);
108274#endif
108275
108276 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
108277 ** to the current row of the index. */
108278 for(i=0; i<iChng; i++){
108279 p->current.anEq[i]++;
108280 }
108281 for(i=iChng; i<p->nCol; i++){
108282 p->current.anDLt[i]++;
108283#ifdef SQLITE_ENABLE_STAT4
108284 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
108285#endif
108286 p->current.anEq[i] = 1;
108287 }
108288 }
108289
108290 p->nRow++;
108291#ifdef SQLITE_ENABLE_STAT4
108292 if( p->mxSample ){
108293 tRowcnt nLt;
108294 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
108295 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
108296 }else{
108297 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
108298 sqlite3_value_blob(argv[2]));
108299 }
108300 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
108301
108302 nLt = p->current.anLt[p->nCol-1];
108303 /* Check if this is to be a periodic sample. If so, add it. */
108304 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
108305 p->current.isPSample = 1;
108306 p->current.iCol = 0;
108307 sampleInsert(p, &p->current, p->nCol-1);
108308 p->current.isPSample = 0;
108309 }
108310
108311 /* Update the aBest[] array. */
108312 for(i=0; i<(p->nCol-1); i++){
108313 p->current.iCol = i;
108314 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
108315 sampleCopy(p, &p->aBest[i], &p->current);
108316 }
108317 }
108318 }else
108319#endif
108320 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
108321 p->nSkipAhead++;
108322 sqlite3_result_int(context, p->current.anDLt[0]>0);
108323 }
108324}
108325
108326static const FuncDef statPushFuncdef = {
108327 2+IsStat4, /* nArg */
108328 SQLITE_UTF8, /* funcFlags */
108329 0, /* pUserData */
108330 0, /* pNext */
108331 statPush, /* xSFunc */
108332 0, /* xFinalize */
108333 0, 0, /* xValue, xInverse */
108334 "stat_push", /* zName */
108335 {0}
108336};
108337
108338#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
108339#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
108340#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
108341#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
108342#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
108343
108344/*
108345** Implementation of the stat_get(P,J) SQL function. This routine is
108346** used to query statistical information that has been gathered into
108347** the StatAccum object by prior calls to stat_push(). The P parameter
108348** has type BLOB but it is really just a pointer to the StatAccum object.
108349** The content to returned is determined by the parameter J
108350** which is one of the STAT_GET_xxxx values defined above.
108351**
108352** The stat_get(P,J) function is not available to generic SQL. It is
108353** inserted as part of a manually constructed bytecode program. (See
108354** the callStatGet() routine below.) It is guaranteed that the P
108355** parameter will always be a pointer to a StatAccum object, never a
108356** NULL.
108357**
108358** If STAT4 is not enabled, then J is always
108359** STAT_GET_STAT1 and is hence omitted and this routine becomes
108360** a one-parameter function, stat_get(P), that always returns the
108361** stat1 table entry information.
108362*/
108363static void statGet(
108364 sqlite3_context *context,
108365 int argc,
108366 sqlite3_value **argv
108367){
108368 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
108369#ifdef SQLITE_ENABLE_STAT4
108370 /* STAT4 has a parameter on this routine. */
108371 int eCall = sqlite3_value_int(argv[1]);
108372 assert( argc==2 );
108373 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
108374 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
108375 || eCall==STAT_GET_NDLT
108376 );
108377 assert( eCall==STAT_GET_STAT1 || p->mxSample );
108378 if( eCall==STAT_GET_STAT1 )
108379#else
108380 assert( argc==1 );
108381#endif
108382 {
108383 /* Return the value to store in the "stat" column of the sqlite_stat1
108384 ** table for this index.
108385 **
108386 ** The value is a string composed of a list of integers describing
108387 ** the index. The first integer in the list is the total number of
108388 ** entries in the index. There is one additional integer in the list
108389 ** for each indexed column. This additional integer is an estimate of
108390 ** the number of rows matched by a equality query on the index using
108391 ** a key with the corresponding number of fields. In other words,
108392 ** if the index is on columns (a,b) and the sqlite_stat1 value is
108393 ** "100 10 2", then SQLite estimates that:
108394 **
108395 ** * the index contains 100 rows,
108396 ** * "WHERE a=?" matches 10 rows, and
108397 ** * "WHERE a=? AND b=?" matches 2 rows.
108398 **
108399 ** If D is the count of distinct values and K is the total number of
108400 ** rows, then each estimate is computed as:
108401 **
108402 ** I = (K+D-1)/D
108403 */
108404 char *z;
108405 int i;
108406
108407 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
108408 if( zRet==0 ){
108409 sqlite3_result_error_nomem(context);
108410 return;
108411 }
108412
108413 sqlite3_snprintf(24, zRet, "%llu",
108414 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
108415 z = zRet + sqlite3Strlen30(zRet);
108416 for(i=0; i<p->nKeyCol; i++){
108417 u64 nDistinct = p->current.anDLt[i] + 1;
108418 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
108419 sqlite3_snprintf(24, z, " %llu", iVal);
108420 z += sqlite3Strlen30(z);
108421 assert( p->current.anEq[i] );
108422 }
108423 assert( z[0]=='\0' && z>zRet );
108424
108425 sqlite3_result_text(context, zRet, -1, sqlite3_free);
108426 }
108427#ifdef SQLITE_ENABLE_STAT4
108428 else if( eCall==STAT_GET_ROWID ){
108429 if( p->iGet<0 ){
108430 samplePushPrevious(p, 0);
108431 p->iGet = 0;
108432 }
108433 if( p->iGet<p->nSample ){
108434 StatSample *pS = p->a + p->iGet;
108435 if( pS->nRowid==0 ){
108436 sqlite3_result_int64(context, pS->u.iRowid);
108437 }else{
108438 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
108439 SQLITE_TRANSIENT);
108440 }
108441 }
108442 }else{
108443 tRowcnt *aCnt = 0;
108444
108445 assert( p->iGet<p->nSample );
108446 switch( eCall ){
108447 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
108448 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
108449 default: {
108450 aCnt = p->a[p->iGet].anDLt;
108451 p->iGet++;
108452 break;
108453 }
108454 }
108455
108456 {
108457 char *zRet = sqlite3MallocZero(p->nCol * 25);
108458 if( zRet==0 ){
108459 sqlite3_result_error_nomem(context);
108460 }else{
108461 int i;
108462 char *z = zRet;
108463 for(i=0; i<p->nCol; i++){
108464 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
108465 z += sqlite3Strlen30(z);
108466 }
108467 assert( z[0]=='\0' && z>zRet );
108468 z[-1] = '\0';
108469 sqlite3_result_text(context, zRet, -1, sqlite3_free);
108470 }
108471 }
108472 }
108473#endif /* SQLITE_ENABLE_STAT4 */
108474#ifndef SQLITE_DEBUG
108475 UNUSED_PARAMETER( argc );
108476#endif
108477}
108478static const FuncDef statGetFuncdef = {
108479 1+IsStat4, /* nArg */
108480 SQLITE_UTF8, /* funcFlags */
108481 0, /* pUserData */
108482 0, /* pNext */
108483 statGet, /* xSFunc */
108484 0, /* xFinalize */
108485 0, 0, /* xValue, xInverse */
108486 "stat_get", /* zName */
108487 {0}
108488};
108489
108490static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
108491#ifdef SQLITE_ENABLE_STAT4
108492 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
108493#elif SQLITE_DEBUG
108494 assert( iParam==STAT_GET_STAT1 );
108495#else
108496 UNUSED_PARAMETER( iParam );
108497#endif
108498 assert( regOut!=regStat && regOut!=regStat+1 );
108499 sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat4,
108500 &statGetFuncdef, 0);
108501}
108502
108503#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108504/* Add a comment to the most recent VDBE opcode that is the name
108505** of the k-th column of the pIdx index.
108506*/
108507static void analyzeVdbeCommentIndexWithColumnName(
108508 Vdbe *v, /* Prepared statement under construction */
108509 Index *pIdx, /* Index whose column is being loaded */
108510 int k /* Which column index */
108511){
108512 int i; /* Index of column in the table */
108513 assert( k>=0 && k<pIdx->nColumn );
108514 i = pIdx->aiColumn[k];
108515 if( NEVER(i==XN_ROWID) ){
108516 VdbeComment((v,"%s.rowid",pIdx->zName));
108517 }else if( i==XN_EXPR ){
108518 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
108519 }else{
108520 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zName));
108521 }
108522}
108523#else
108524# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
108525#endif /* SQLITE_DEBUG */
108526
108527/*
108528** Generate code to do an analysis of all indices associated with
108529** a single table.
108530*/
108531static void analyzeOneTable(
108532 Parse *pParse, /* Parser context */
108533 Table *pTab, /* Table whose indices are to be analyzed */
108534 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
108535 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
108536 int iMem, /* Available memory locations begin here */
108537 int iTab /* Next available cursor */
108538){
108539 sqlite3 *db = pParse->db; /* Database handle */
108540 Index *pIdx; /* An index to being analyzed */
108541 int iIdxCur; /* Cursor open on index being analyzed */
108542 int iTabCur; /* Table cursor */
108543 Vdbe *v; /* The virtual machine being built up */
108544 int i; /* Loop counter */
108545 int jZeroRows = -1; /* Jump from here if number of rows is zero */
108546 int iDb; /* Index of database containing pTab */
108547 u8 needTableCnt = 1; /* True to count the table */
108548 int regNewRowid = iMem++; /* Rowid for the inserted record */
108549 int regStat = iMem++; /* Register to hold StatAccum object */
108550 int regChng = iMem++; /* Index of changed index field */
108551 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
108552 int regTemp = iMem++; /* Temporary use register */
108553 int regTemp2 = iMem++; /* Second temporary use register */
108554 int regTabname = iMem++; /* Register containing table name */
108555 int regIdxname = iMem++; /* Register containing index name */
108556 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
108557 int regPrev = iMem; /* MUST BE LAST (see below) */
108558#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108559 Table *pStat1 = 0;
108560#endif
108561
108562 pParse->nMem = MAX(pParse->nMem, iMem);
108563 v = sqlite3GetVdbe(pParse);
108564 if( v==0 || NEVER(pTab==0) ){
108565 return;
108566 }
108567 if( pTab->tnum==0 ){
108568 /* Do not gather statistics on views or virtual tables */
108569 return;
108570 }
108571 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
108572 /* Do not gather statistics on system tables */
108573 return;
108574 }
108575 assert( sqlite3BtreeHoldsAllMutexes(db) );
108576 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108577 assert( iDb>=0 );
108578 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108579#ifndef SQLITE_OMIT_AUTHORIZATION
108580 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
108581 db->aDb[iDb].zDbSName ) ){
108582 return;
108583 }
108584#endif
108585
108586#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108587 if( db->xPreUpdateCallback ){
108588 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
108589 if( pStat1==0 ) return;
108590 pStat1->zName = (char*)&pStat1[1];
108591 memcpy(pStat1->zName, "sqlite_stat1", 13);
108592 pStat1->nCol = 3;
108593 pStat1->iPKey = -1;
108594 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
108595 }
108596#endif
108597
108598 /* Establish a read-lock on the table at the shared-cache level.
108599 ** Open a read-only cursor on the table. Also allocate a cursor number
108600 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
108601 ** this time though. */
108602 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108603 iTabCur = iTab++;
108604 iIdxCur = iTab++;
108605 pParse->nTab = MAX(pParse->nTab, iTab);
108606 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
108607 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
108608
108609 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108610 int nCol; /* Number of columns in pIdx. "N" */
108611 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
108612 int addrNextRow; /* Address of "next_row:" */
108613 const char *zIdxName; /* Name of the index */
108614 int nColTest; /* Number of columns to test for changes */
108615
108616 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
108617 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
108618 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
108619 nCol = pIdx->nKeyCol;
108620 zIdxName = pTab->zName;
108621 nColTest = nCol - 1;
108622 }else{
108623 nCol = pIdx->nColumn;
108624 zIdxName = pIdx->zName;
108625 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
108626 }
108627
108628 /* Populate the register containing the index name. */
108629 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
108630 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
108631
108632 /*
108633 ** Pseudo-code for loop that calls stat_push():
108634 **
108635 ** Rewind csr
108636 ** if eof(csr) goto end_of_scan;
108637 ** regChng = 0
108638 ** goto chng_addr_0;
108639 **
108640 ** next_row:
108641 ** regChng = 0
108642 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
108643 ** regChng = 1
108644 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
108645 ** ...
108646 ** regChng = N
108647 ** goto chng_addr_N
108648 **
108649 ** chng_addr_0:
108650 ** regPrev(0) = idx(0)
108651 ** chng_addr_1:
108652 ** regPrev(1) = idx(1)
108653 ** ...
108654 **
108655 ** endDistinctTest:
108656 ** regRowid = idx(rowid)
108657 ** stat_push(P, regChng, regRowid)
108658 ** Next csr
108659 ** if !eof(csr) goto next_row;
108660 **
108661 ** end_of_scan:
108662 */
108663
108664 /* Make sure there are enough memory cells allocated to accommodate
108665 ** the regPrev array and a trailing rowid (the rowid slot is required
108666 ** when building a record to insert into the sample column of
108667 ** the sqlite_stat4 table. */
108668 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
108669
108670 /* Open a read-only cursor on the index being analyzed. */
108671 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
108672 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
108673 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108674 VdbeComment((v, "%s", pIdx->zName));
108675
108676 /* Invoke the stat_init() function. The arguments are:
108677 **
108678 ** (1) the number of columns in the index including the rowid
108679 ** (or for a WITHOUT ROWID table, the number of PK columns),
108680 ** (2) the number of columns in the key without the rowid/pk
108681 ** (3) estimated number of rows in the index,
108682 */
108683 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1);
108684 assert( regRowid==regStat+2 );
108685 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
108686#ifdef SQLITE_ENABLE_STAT4
108687 if( OptimizationEnabled(db, SQLITE_Stat4) ){
108688 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp);
108689 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
108690 VdbeCoverage(v);
108691 }else
108692#endif
108693 {
108694 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
108695 VdbeCoverage(v);
108696 sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, 1);
108697 }
108698 assert( regTemp2==regStat+4 );
108699 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
108700 sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
108701 &statInitFuncdef, 0);
108702
108703 /* Implementation of the following:
108704 **
108705 ** Rewind csr
108706 ** if eof(csr) goto end_of_scan;
108707 ** regChng = 0
108708 ** goto next_push_0;
108709 **
108710 */
108711 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
108712 addrNextRow = sqlite3VdbeCurrentAddr(v);
108713
108714 if( nColTest>0 ){
108715 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
108716 int *aGotoChng; /* Array of jump instruction addresses */
108717 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
108718 if( aGotoChng==0 ) continue;
108719
108720 /*
108721 ** next_row:
108722 ** regChng = 0
108723 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
108724 ** regChng = 1
108725 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
108726 ** ...
108727 ** regChng = N
108728 ** goto endDistinctTest
108729 */
108730 sqlite3VdbeAddOp0(v, OP_Goto);
108731 addrNextRow = sqlite3VdbeCurrentAddr(v);
108732 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
108733 /* For a single-column UNIQUE index, once we have found a non-NULL
108734 ** row, we know that all the rest will be distinct, so skip
108735 ** subsequent distinctness tests. */
108736 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
108737 VdbeCoverage(v);
108738 }
108739 for(i=0; i<nColTest; i++){
108740 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
108741 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
108742 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
108743 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
108744 aGotoChng[i] =
108745 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
108746 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
108747 VdbeCoverage(v);
108748 }
108749 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
108750 sqlite3VdbeGoto(v, endDistinctTest);
108751
108752
108753 /*
108754 ** chng_addr_0:
108755 ** regPrev(0) = idx(0)
108756 ** chng_addr_1:
108757 ** regPrev(1) = idx(1)
108758 ** ...
108759 */
108760 sqlite3VdbeJumpHere(v, addrNextRow-1);
108761 for(i=0; i<nColTest; i++){
108762 sqlite3VdbeJumpHere(v, aGotoChng[i]);
108763 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
108764 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
108765 }
108766 sqlite3VdbeResolveLabel(v, endDistinctTest);
108767 sqlite3DbFree(db, aGotoChng);
108768 }
108769
108770 /*
108771 ** chng_addr_N:
108772 ** regRowid = idx(rowid) // STAT4 only
108773 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
108774 ** Next csr
108775 ** if !eof(csr) goto next_row;
108776 */
108777#ifdef SQLITE_ENABLE_STAT4
108778 if( OptimizationEnabled(db, SQLITE_Stat4) ){
108779 assert( regRowid==(regStat+2) );
108780 if( HasRowid(pTab) ){
108781 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
108782 }else{
108783 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
108784 int j, k, regKey;
108785 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
108786 for(j=0; j<pPk->nKeyCol; j++){
108787 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
108788 assert( k>=0 && k<pIdx->nColumn );
108789 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
108790 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
108791 }
108792 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
108793 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
108794 }
108795 }
108796#endif
108797 assert( regChng==(regStat+1) );
108798 {
108799 sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat4,
108800 &statPushFuncdef, 0);
108801 if( db->nAnalysisLimit ){
108802 int j1, j2, j3;
108803 j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regTemp); VdbeCoverage(v);
108804 j2 = sqlite3VdbeAddOp1(v, OP_If, regTemp); VdbeCoverage(v);
108805 j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT, iIdxCur, 0, regPrev, 1);
108806 VdbeCoverage(v);
108807 sqlite3VdbeJumpHere(v, j1);
108808 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
108809 sqlite3VdbeJumpHere(v, j2);
108810 sqlite3VdbeJumpHere(v, j3);
108811 }else{
108812 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
108813 }
108814 }
108815
108816 /* Add the entry to the stat1 table. */
108817 callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1);
108818 assert( "BBB"[0]==SQLITE_AFF_TEXT );
108819 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
108820 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108821 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108822#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108823 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108824#endif
108825 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108826
108827 /* Add the entries to the stat4 table. */
108828#ifdef SQLITE_ENABLE_STAT4
108829 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
108830 int regEq = regStat1;
108831 int regLt = regStat1+1;
108832 int regDLt = regStat1+2;
108833 int regSample = regStat1+3;
108834 int regCol = regStat1+4;
108835 int regSampleRowid = regCol + nCol;
108836 int addrNext;
108837 int addrIsNull;
108838 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
108839
108840 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
108841
108842 addrNext = sqlite3VdbeCurrentAddr(v);
108843 callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid);
108844 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
108845 VdbeCoverage(v);
108846 callStatGet(pParse, regStat, STAT_GET_NEQ, regEq);
108847 callStatGet(pParse, regStat, STAT_GET_NLT, regLt);
108848 callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt);
108849 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
108850 VdbeCoverage(v);
108851 for(i=0; i<nCol; i++){
108852 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
108853 }
108854 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
108855 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
108856 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
108857 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
108858 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
108859 sqlite3VdbeJumpHere(v, addrIsNull);
108860 }
108861#endif /* SQLITE_ENABLE_STAT4 */
108862
108863 /* End of analysis */
108864 sqlite3VdbeJumpHere(v, addrRewind);
108865 }
108866
108867
108868 /* Create a single sqlite_stat1 entry containing NULL as the index
108869 ** name and the row count as the content.
108870 */
108871 if( pOnlyIdx==0 && needTableCnt ){
108872 VdbeComment((v, "%s", pTab->zName));
108873 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
108874 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
108875 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
108876 assert( "BBB"[0]==SQLITE_AFF_TEXT );
108877 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
108878 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108879 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108880 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108881#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108882 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108883#endif
108884 sqlite3VdbeJumpHere(v, jZeroRows);
108885 }
108886}
108887
108888
108889/*
108890** Generate code that will cause the most recent index analysis to
108891** be loaded into internal hash tables where is can be used.
108892*/
108893static void loadAnalysis(Parse *pParse, int iDb){
108894 Vdbe *v = sqlite3GetVdbe(pParse);
108895 if( v ){
108896 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
108897 }
108898}
108899
108900/*
108901** Generate code that will do an analysis of an entire database
108902*/
108903static void analyzeDatabase(Parse *pParse, int iDb){
108904 sqlite3 *db = pParse->db;
108905 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
108906 HashElem *k;
108907 int iStatCur;
108908 int iMem;
108909 int iTab;
108910
108911 sqlite3BeginWriteOperation(pParse, 0, iDb);
108912 iStatCur = pParse->nTab;
108913 pParse->nTab += 3;
108914 openStatTable(pParse, iDb, iStatCur, 0, 0);
108915 iMem = pParse->nMem+1;
108916 iTab = pParse->nTab;
108917 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108918 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
108919 Table *pTab = (Table*)sqliteHashData(k);
108920 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
108921 }
108922 loadAnalysis(pParse, iDb);
108923}
108924
108925/*
108926** Generate code that will do an analysis of a single table in
108927** a database. If pOnlyIdx is not NULL then it is a single index
108928** in pTab that should be analyzed.
108929*/
108930static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
108931 int iDb;
108932 int iStatCur;
108933
108934 assert( pTab!=0 );
108935 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108936 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108937 sqlite3BeginWriteOperation(pParse, 0, iDb);
108938 iStatCur = pParse->nTab;
108939 pParse->nTab += 3;
108940 if( pOnlyIdx ){
108941 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
108942 }else{
108943 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
108944 }
108945 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
108946 loadAnalysis(pParse, iDb);
108947}
108948
108949/*
108950** Generate code for the ANALYZE command. The parser calls this routine
108951** when it recognizes an ANALYZE command.
108952**
108953** ANALYZE -- 1
108954** ANALYZE <database> -- 2
108955** ANALYZE ?<database>.?<tablename> -- 3
108956**
108957** Form 1 causes all indices in all attached databases to be analyzed.
108958** Form 2 analyzes all indices the single database named.
108959** Form 3 analyzes all indices associated with the named table.
108960*/
108961SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
108962 sqlite3 *db = pParse->db;
108963 int iDb;
108964 int i;
108965 char *z, *zDb;
108966 Table *pTab;
108967 Index *pIdx;
108968 Token *pTableName;
108969 Vdbe *v;
108970
108971 /* Read the database schema. If an error occurs, leave an error message
108972 ** and code in pParse and return NULL. */
108973 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108974 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108975 return;
108976 }
108977
108978 assert( pName2!=0 || pName1==0 );
108979 if( pName1==0 ){
108980 /* Form 1: Analyze everything */
108981 for(i=0; i<db->nDb; i++){
108982 if( i==1 ) continue; /* Do not analyze the TEMP database */
108983 analyzeDatabase(pParse, i);
108984 }
108985 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
108986 /* Analyze the schema named as the argument */
108987 analyzeDatabase(pParse, iDb);
108988 }else{
108989 /* Form 3: Analyze the table or index named as an argument */
108990 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
108991 if( iDb>=0 ){
108992 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
108993 z = sqlite3NameFromToken(db, pTableName);
108994 if( z ){
108995 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
108996 analyzeTable(pParse, pIdx->pTable, pIdx);
108997 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
108998 analyzeTable(pParse, pTab, 0);
108999 }
109000 sqlite3DbFree(db, z);
109001 }
109002 }
109003 }
109004 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
109005 sqlite3VdbeAddOp0(v, OP_Expire);
109006 }
109007}
109008
109009/*
109010** Used to pass information from the analyzer reader through to the
109011** callback routine.
109012*/
109013typedef struct analysisInfo analysisInfo;
109014struct analysisInfo {
109015 sqlite3 *db;
109016 const char *zDatabase;
109017};
109018
109019/*
109020** The first argument points to a nul-terminated string containing a
109021** list of space separated integers. Read the first nOut of these into
109022** the array aOut[].
109023*/
109024static void decodeIntArray(
109025 char *zIntArray, /* String containing int array to decode */
109026 int nOut, /* Number of slots in aOut[] */
109027 tRowcnt *aOut, /* Store integers here */
109028 LogEst *aLog, /* Or, if aOut==0, here */
109029 Index *pIndex /* Handle extra flags for this index, if not NULL */
109030){
109031 char *z = zIntArray;
109032 int c;
109033 int i;
109034 tRowcnt v;
109035
109036#ifdef SQLITE_ENABLE_STAT4
109037 if( z==0 ) z = "";
109038#else
109039 assert( z!=0 );
109040#endif
109041 for(i=0; *z && i<nOut; i++){
109042 v = 0;
109043 while( (c=z[0])>='0' && c<='9' ){
109044 v = v*10 + c - '0';
109045 z++;
109046 }
109047#ifdef SQLITE_ENABLE_STAT4
109048 if( aOut ) aOut[i] = v;
109049 if( aLog ) aLog[i] = sqlite3LogEst(v);
109050#else
109051 assert( aOut==0 );
109052 UNUSED_PARAMETER(aOut);
109053 assert( aLog!=0 );
109054 aLog[i] = sqlite3LogEst(v);
109055#endif
109056 if( *z==' ' ) z++;
109057 }
109058#ifndef SQLITE_ENABLE_STAT4
109059 assert( pIndex!=0 ); {
109060#else
109061 if( pIndex ){
109062#endif
109063 pIndex->bUnordered = 0;
109064 pIndex->noSkipScan = 0;
109065 while( z[0] ){
109066 if( sqlite3_strglob("unordered*", z)==0 ){
109067 pIndex->bUnordered = 1;
109068 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
109069 int sz = sqlite3Atoi(z+3);
109070 if( sz<2 ) sz = 2;
109071 pIndex->szIdxRow = sqlite3LogEst(sz);
109072 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
109073 pIndex->noSkipScan = 1;
109074 }
109075#ifdef SQLITE_ENABLE_COSTMULT
109076 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
109077 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
109078 }
109079#endif
109080 while( z[0]!=0 && z[0]!=' ' ) z++;
109081 while( z[0]==' ' ) z++;
109082 }
109083 }
109084}
109085
109086/*
109087** This callback is invoked once for each index when reading the
109088** sqlite_stat1 table.
109089**
109090** argv[0] = name of the table
109091** argv[1] = name of the index (might be NULL)
109092** argv[2] = results of analysis - on integer for each column
109093**
109094** Entries for which argv[1]==NULL simply record the number of rows in
109095** the table.
109096*/
109097static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
109098 analysisInfo *pInfo = (analysisInfo*)pData;
109099 Index *pIndex;
109100 Table *pTable;
109101 const char *z;
109102
109103 assert( argc==3 );
109104 UNUSED_PARAMETER2(NotUsed, argc);
109105
109106 if( argv==0 || argv[0]==0 || argv[2]==0 ){
109107 return 0;
109108 }
109109 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
109110 if( pTable==0 ){
109111 return 0;
109112 }
109113 if( argv[1]==0 ){
109114 pIndex = 0;
109115 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
109116 pIndex = sqlite3PrimaryKeyIndex(pTable);
109117 }else{
109118 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
109119 }
109120 z = argv[2];
109121
109122 if( pIndex ){
109123 tRowcnt *aiRowEst = 0;
109124 int nCol = pIndex->nKeyCol+1;
109125#ifdef SQLITE_ENABLE_STAT4
109126 /* Index.aiRowEst may already be set here if there are duplicate
109127 ** sqlite_stat1 entries for this index. In that case just clobber
109128 ** the old data with the new instead of allocating a new array. */
109129 if( pIndex->aiRowEst==0 ){
109130 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
109131 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
109132 }
109133 aiRowEst = pIndex->aiRowEst;
109134#endif
109135 pIndex->bUnordered = 0;
109136 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
109137 pIndex->hasStat1 = 1;
109138 if( pIndex->pPartIdxWhere==0 ){
109139 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
109140 pTable->tabFlags |= TF_HasStat1;
109141 }
109142 }else{
109143 Index fakeIdx;
109144 fakeIdx.szIdxRow = pTable->szTabRow;
109145#ifdef SQLITE_ENABLE_COSTMULT
109146 fakeIdx.pTable = pTable;
109147#endif
109148 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
109149 pTable->szTabRow = fakeIdx.szIdxRow;
109150 pTable->tabFlags |= TF_HasStat1;
109151 }
109152
109153 return 0;
109154}
109155
109156/*
109157** If the Index.aSample variable is not NULL, delete the aSample[] array
109158** and its contents.
109159*/
109160SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
109161#ifdef SQLITE_ENABLE_STAT4
109162 if( pIdx->aSample ){
109163 int j;
109164 for(j=0; j<pIdx->nSample; j++){
109165 IndexSample *p = &pIdx->aSample[j];
109166 sqlite3DbFree(db, p->p);
109167 }
109168 sqlite3DbFree(db, pIdx->aSample);
109169 }
109170 if( db && db->pnBytesFreed==0 ){
109171 pIdx->nSample = 0;
109172 pIdx->aSample = 0;
109173 }
109174#else
109175 UNUSED_PARAMETER(db);
109176 UNUSED_PARAMETER(pIdx);
109177#endif /* SQLITE_ENABLE_STAT4 */
109178}
109179
109180#ifdef SQLITE_ENABLE_STAT4
109181/*
109182** Populate the pIdx->aAvgEq[] array based on the samples currently
109183** stored in pIdx->aSample[].
109184*/
109185static void initAvgEq(Index *pIdx){
109186 if( pIdx ){
109187 IndexSample *aSample = pIdx->aSample;
109188 IndexSample *pFinal = &aSample[pIdx->nSample-1];
109189 int iCol;
109190 int nCol = 1;
109191 if( pIdx->nSampleCol>1 ){
109192 /* If this is stat4 data, then calculate aAvgEq[] values for all
109193 ** sample columns except the last. The last is always set to 1, as
109194 ** once the trailing PK fields are considered all index keys are
109195 ** unique. */
109196 nCol = pIdx->nSampleCol-1;
109197 pIdx->aAvgEq[nCol] = 1;
109198 }
109199 for(iCol=0; iCol<nCol; iCol++){
109200 int nSample = pIdx->nSample;
109201 int i; /* Used to iterate through samples */
109202 tRowcnt sumEq = 0; /* Sum of the nEq values */
109203 tRowcnt avgEq = 0;
109204 tRowcnt nRow; /* Number of rows in index */
109205 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
109206 i64 nDist100; /* Number of distinct values in index */
109207
109208 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
109209 nRow = pFinal->anLt[iCol];
109210 nDist100 = (i64)100 * pFinal->anDLt[iCol];
109211 nSample--;
109212 }else{
109213 nRow = pIdx->aiRowEst[0];
109214 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
109215 }
109216 pIdx->nRowEst0 = nRow;
109217
109218 /* Set nSum to the number of distinct (iCol+1) field prefixes that
109219 ** occur in the stat4 table for this index. Set sumEq to the sum of
109220 ** the nEq values for column iCol for the same set (adding the value
109221 ** only once where there exist duplicate prefixes). */
109222 for(i=0; i<nSample; i++){
109223 if( i==(pIdx->nSample-1)
109224 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
109225 ){
109226 sumEq += aSample[i].anEq[iCol];
109227 nSum100 += 100;
109228 }
109229 }
109230
109231 if( nDist100>nSum100 && sumEq<nRow ){
109232 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
109233 }
109234 if( avgEq==0 ) avgEq = 1;
109235 pIdx->aAvgEq[iCol] = avgEq;
109236 }
109237 }
109238}
109239
109240/*
109241** Look up an index by name. Or, if the name of a WITHOUT ROWID table
109242** is supplied instead, find the PRIMARY KEY index for that table.
109243*/
109244static Index *findIndexOrPrimaryKey(
109245 sqlite3 *db,
109246 const char *zName,
109247 const char *zDb
109248){
109249 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
109250 if( pIdx==0 ){
109251 Table *pTab = sqlite3FindTable(db, zName, zDb);
109252 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
109253 }
109254 return pIdx;
109255}
109256
109257/*
109258** Load the content from either the sqlite_stat4
109259** into the relevant Index.aSample[] arrays.
109260**
109261** Arguments zSql1 and zSql2 must point to SQL statements that return
109262** data equivalent to the following:
109263**
109264** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
109265** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
109266**
109267** where %Q is replaced with the database name before the SQL is executed.
109268*/
109269static int loadStatTbl(
109270 sqlite3 *db, /* Database handle */
109271 const char *zSql1, /* SQL statement 1 (see above) */
109272 const char *zSql2, /* SQL statement 2 (see above) */
109273 const char *zDb /* Database name (e.g. "main") */
109274){
109275 int rc; /* Result codes from subroutines */
109276 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
109277 char *zSql; /* Text of the SQL statement */
109278 Index *pPrevIdx = 0; /* Previous index in the loop */
109279 IndexSample *pSample; /* A slot in pIdx->aSample[] */
109280
109281 assert( db->lookaside.bDisable );
109282 zSql = sqlite3MPrintf(db, zSql1, zDb);
109283 if( !zSql ){
109284 return SQLITE_NOMEM_BKPT;
109285 }
109286 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
109287 sqlite3DbFree(db, zSql);
109288 if( rc ) return rc;
109289
109290 while( sqlite3_step(pStmt)==SQLITE_ROW ){
109291 int nIdxCol = 1; /* Number of columns in stat4 records */
109292
109293 char *zIndex; /* Index name */
109294 Index *pIdx; /* Pointer to the index object */
109295 int nSample; /* Number of samples */
109296 int nByte; /* Bytes of space required */
109297 int i; /* Bytes of space required */
109298 tRowcnt *pSpace;
109299
109300 zIndex = (char *)sqlite3_column_text(pStmt, 0);
109301 if( zIndex==0 ) continue;
109302 nSample = sqlite3_column_int(pStmt, 1);
109303 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
109304 assert( pIdx==0 || pIdx->nSample==0 );
109305 if( pIdx==0 ) continue;
109306 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
109307 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
109308 nIdxCol = pIdx->nKeyCol;
109309 }else{
109310 nIdxCol = pIdx->nColumn;
109311 }
109312 pIdx->nSampleCol = nIdxCol;
109313 nByte = sizeof(IndexSample) * nSample;
109314 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
109315 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
109316
109317 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
109318 if( pIdx->aSample==0 ){
109319 sqlite3_finalize(pStmt);
109320 return SQLITE_NOMEM_BKPT;
109321 }
109322 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
109323 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
109324 for(i=0; i<nSample; i++){
109325 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
109326 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
109327 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
109328 }
109329 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
109330 }
109331 rc = sqlite3_finalize(pStmt);
109332 if( rc ) return rc;
109333
109334 zSql = sqlite3MPrintf(db, zSql2, zDb);
109335 if( !zSql ){
109336 return SQLITE_NOMEM_BKPT;
109337 }
109338 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
109339 sqlite3DbFree(db, zSql);
109340 if( rc ) return rc;
109341
109342 while( sqlite3_step(pStmt)==SQLITE_ROW ){
109343 char *zIndex; /* Index name */
109344 Index *pIdx; /* Pointer to the index object */
109345 int nCol = 1; /* Number of columns in index */
109346
109347 zIndex = (char *)sqlite3_column_text(pStmt, 0);
109348 if( zIndex==0 ) continue;
109349 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
109350 if( pIdx==0 ) continue;
109351 /* This next condition is true if data has already been loaded from
109352 ** the sqlite_stat4 table. */
109353 nCol = pIdx->nSampleCol;
109354 if( pIdx!=pPrevIdx ){
109355 initAvgEq(pPrevIdx);
109356 pPrevIdx = pIdx;
109357 }
109358 pSample = &pIdx->aSample[pIdx->nSample];
109359 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
109360 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
109361 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
109362
109363 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
109364 ** This is in case the sample record is corrupted. In that case, the
109365 ** sqlite3VdbeRecordCompare() may read up to two varints past the
109366 ** end of the allocated buffer before it realizes it is dealing with
109367 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
109368 ** a buffer overread. */
109369 pSample->n = sqlite3_column_bytes(pStmt, 4);
109370 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
109371 if( pSample->p==0 ){
109372 sqlite3_finalize(pStmt);
109373 return SQLITE_NOMEM_BKPT;
109374 }
109375 if( pSample->n ){
109376 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
109377 }
109378 pIdx->nSample++;
109379 }
109380 rc = sqlite3_finalize(pStmt);
109381 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
109382 return rc;
109383}
109384
109385/*
109386** Load content from the sqlite_stat4 table into
109387** the Index.aSample[] arrays of all indices.
109388*/
109389static int loadStat4(sqlite3 *db, const char *zDb){
109390 int rc = SQLITE_OK; /* Result codes from subroutines */
109391
109392 assert( db->lookaside.bDisable );
109393 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
109394 rc = loadStatTbl(db,
109395 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
109396 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
109397 zDb
109398 );
109399 }
109400 return rc;
109401}
109402#endif /* SQLITE_ENABLE_STAT4 */
109403
109404/*
109405** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
109406** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
109407** arrays. The contents of sqlite_stat4 are used to populate the
109408** Index.aSample[] arrays.
109409**
109410** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
109411** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
109412** during compilation and the sqlite_stat4 table is present, no data is
109413** read from it.
109414**
109415** If SQLITE_ENABLE_STAT4 was defined during compilation and the
109416** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
109417** returned. However, in this case, data is read from the sqlite_stat1
109418** table (if it is present) before returning.
109419**
109420** If an OOM error occurs, this function always sets db->mallocFailed.
109421** This means if the caller does not care about other errors, the return
109422** code may be ignored.
109423*/
109424SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
109425 analysisInfo sInfo;
109426 HashElem *i;
109427 char *zSql;
109428 int rc = SQLITE_OK;
109429 Schema *pSchema = db->aDb[iDb].pSchema;
109430
109431 assert( iDb>=0 && iDb<db->nDb );
109432 assert( db->aDb[iDb].pBt!=0 );
109433
109434 /* Clear any prior statistics */
109435 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
109436 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
109437 Table *pTab = sqliteHashData(i);
109438 pTab->tabFlags &= ~TF_HasStat1;
109439 }
109440 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109441 Index *pIdx = sqliteHashData(i);
109442 pIdx->hasStat1 = 0;
109443#ifdef SQLITE_ENABLE_STAT4
109444 sqlite3DeleteIndexSamples(db, pIdx);
109445 pIdx->aSample = 0;
109446#endif
109447 }
109448
109449 /* Load new statistics out of the sqlite_stat1 table */
109450 sInfo.db = db;
109451 sInfo.zDatabase = db->aDb[iDb].zDbSName;
109452 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
109453 zSql = sqlite3MPrintf(db,
109454 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
109455 if( zSql==0 ){
109456 rc = SQLITE_NOMEM_BKPT;
109457 }else{
109458 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
109459 sqlite3DbFree(db, zSql);
109460 }
109461 }
109462
109463 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
109464 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
109465 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109466 Index *pIdx = sqliteHashData(i);
109467 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
109468 }
109469
109470 /* Load the statistics from the sqlite_stat4 table. */
109471#ifdef SQLITE_ENABLE_STAT4
109472 if( rc==SQLITE_OK ){
109473 DisableLookaside;
109474 rc = loadStat4(db, sInfo.zDatabase);
109475 EnableLookaside;
109476 }
109477 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
109478 Index *pIdx = sqliteHashData(i);
109479 sqlite3_free(pIdx->aiRowEst);
109480 pIdx->aiRowEst = 0;
109481 }
109482#endif
109483
109484 if( rc==SQLITE_NOMEM ){
109485 sqlite3OomFault(db);
109486 }
109487 return rc;
109488}
109489
109490
109491#endif /* SQLITE_OMIT_ANALYZE */
109492
109493/************** End of analyze.c *********************************************/
109494/************** Begin file attach.c ******************************************/
109495/*
109496** 2003 April 6
109497**
109498** The author disclaims copyright to this source code. In place of
109499** a legal notice, here is a blessing:
109500**
109501** May you do good and not evil.
109502** May you find forgiveness for yourself and forgive others.
109503** May you share freely, never taking more than you give.
109504**
109505*************************************************************************
109506** This file contains code used to implement the ATTACH and DETACH commands.
109507*/
109508/* #include "sqliteInt.h" */
109509
109510#ifndef SQLITE_OMIT_ATTACH
109511/*
109512** Resolve an expression that was part of an ATTACH or DETACH statement. This
109513** is slightly different from resolving a normal SQL expression, because simple
109514** identifiers are treated as strings, not possible column names or aliases.
109515**
109516** i.e. if the parser sees:
109517**
109518** ATTACH DATABASE abc AS def
109519**
109520** it treats the two expressions as literal strings 'abc' and 'def' instead of
109521** looking for columns of the same name.
109522**
109523** This only applies to the root node of pExpr, so the statement:
109524**
109525** ATTACH DATABASE abc||def AS 'db2'
109526**
109527** will fail because neither abc or def can be resolved.
109528*/
109529static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
109530{
109531 int rc = SQLITE_OK;
109532 if( pExpr ){
109533 if( pExpr->op!=TK_ID ){
109534 rc = sqlite3ResolveExprNames(pName, pExpr);
109535 }else{
109536 pExpr->op = TK_STRING;
109537 }
109538 }
109539 return rc;
109540}
109541
109542/*
109543** Return true if zName points to a name that may be used to refer to
109544** database iDb attached to handle db.
109545*/
109546SQLITE_PRIVATE int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
109547 return (
109548 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
109549 || (iDb==0 && sqlite3StrICmp("main", zName)==0)
109550 );
109551}
109552
109553/*
109554** An SQL user-function registered to do the work of an ATTACH statement. The
109555** three arguments to the function come directly from an attach statement:
109556**
109557** ATTACH DATABASE x AS y KEY z
109558**
109559** SELECT sqlite_attach(x, y, z)
109560**
109561** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
109562** third argument.
109563**
109564** If the db->init.reopenMemdb flags is set, then instead of attaching a
109565** new database, close the database on db->init.iDb and reopen it as an
109566** empty MemDB.
109567*/
109568static void attachFunc(
109569 sqlite3_context *context,
109570 int NotUsed,
109571 sqlite3_value **argv
109572){
109573 int i;
109574 int rc = 0;
109575 sqlite3 *db = sqlite3_context_db_handle(context);
109576 const char *zName;
109577 const char *zFile;
109578 char *zPath = 0;
109579 char *zErr = 0;
109580 unsigned int flags;
109581 Db *aNew; /* New array of Db pointers */
109582 Db *pNew; /* Db object for the newly attached database */
109583 char *zErrDyn = 0;
109584 sqlite3_vfs *pVfs;
109585
109586 UNUSED_PARAMETER(NotUsed);
109587 zFile = (const char *)sqlite3_value_text(argv[0]);
109588 zName = (const char *)sqlite3_value_text(argv[1]);
109589 if( zFile==0 ) zFile = "";
109590 if( zName==0 ) zName = "";
109591
109592#ifdef SQLITE_ENABLE_DESERIALIZE
109593# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
109594#else
109595# define REOPEN_AS_MEMDB(db) (0)
109596#endif
109597
109598 if( REOPEN_AS_MEMDB(db) ){
109599 /* This is not a real ATTACH. Instead, this routine is being called
109600 ** from sqlite3_deserialize() to close database db->init.iDb and
109601 ** reopen it as a MemDB */
109602 pVfs = sqlite3_vfs_find("memdb");
109603 if( pVfs==0 ) return;
109604 pNew = &db->aDb[db->init.iDb];
109605 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
109606 pNew->pBt = 0;
109607 pNew->pSchema = 0;
109608 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
109609 }else{
109610 /* This is a real ATTACH
109611 **
109612 ** Check for the following errors:
109613 **
109614 ** * Too many attached databases,
109615 ** * Transaction currently open
109616 ** * Specified database name already being used.
109617 */
109618 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
109619 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
109620 db->aLimit[SQLITE_LIMIT_ATTACHED]
109621 );
109622 goto attach_error;
109623 }
109624 for(i=0; i<db->nDb; i++){
109625 assert( zName );
109626 if( sqlite3DbIsNamed(db, i, zName) ){
109627 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
109628 goto attach_error;
109629 }
109630 }
109631
109632 /* Allocate the new entry in the db->aDb[] array and initialize the schema
109633 ** hash tables.
109634 */
109635 if( db->aDb==db->aDbStatic ){
109636 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
109637 if( aNew==0 ) return;
109638 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
109639 }else{
109640 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
109641 if( aNew==0 ) return;
109642 }
109643 db->aDb = aNew;
109644 pNew = &db->aDb[db->nDb];
109645 memset(pNew, 0, sizeof(*pNew));
109646
109647 /* Open the database file. If the btree is successfully opened, use
109648 ** it to obtain the database schema. At this point the schema may
109649 ** or may not be initialized.
109650 */
109651 flags = db->openFlags;
109652 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
109653 if( rc!=SQLITE_OK ){
109654 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
109655 sqlite3_result_error(context, zErr, -1);
109656 sqlite3_free(zErr);
109657 return;
109658 }
109659 assert( pVfs );
109660 flags |= SQLITE_OPEN_MAIN_DB;
109661 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
109662 db->nDb++;
109663 pNew->zDbSName = sqlite3DbStrDup(db, zName);
109664 }
109665 db->noSharedCache = 0;
109666 if( rc==SQLITE_CONSTRAINT ){
109667 rc = SQLITE_ERROR;
109668 zErrDyn = sqlite3MPrintf(db, "database is already attached");
109669 }else if( rc==SQLITE_OK ){
109670 Pager *pPager;
109671 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
109672 if( !pNew->pSchema ){
109673 rc = SQLITE_NOMEM_BKPT;
109674 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
109675 zErrDyn = sqlite3MPrintf(db,
109676 "attached databases must use the same text encoding as main database");
109677 rc = SQLITE_ERROR;
109678 }
109679 sqlite3BtreeEnter(pNew->pBt);
109680 pPager = sqlite3BtreePager(pNew->pBt);
109681 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
109682 sqlite3BtreeSecureDelete(pNew->pBt,
109683 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
109684#ifndef SQLITE_OMIT_PAGER_PRAGMAS
109685 sqlite3BtreeSetPagerFlags(pNew->pBt,
109686 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
109687#endif
109688 sqlite3BtreeLeave(pNew->pBt);
109689 }
109690 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
109691 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
109692 rc = SQLITE_NOMEM_BKPT;
109693 }
109694 sqlite3_free_filename( zPath );
109695
109696 /* If the file was opened successfully, read the schema for the new database.
109697 ** If this fails, or if opening the file failed, then close the file and
109698 ** remove the entry from the db->aDb[] array. i.e. put everything back the
109699 ** way we found it.
109700 */
109701 if( rc==SQLITE_OK ){
109702 sqlite3BtreeEnterAll(db);
109703 db->init.iDb = 0;
109704 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
109705 if( !REOPEN_AS_MEMDB(db) ){
109706 rc = sqlite3Init(db, &zErrDyn);
109707 }
109708 sqlite3BtreeLeaveAll(db);
109709 assert( zErrDyn==0 || rc!=SQLITE_OK );
109710 }
109711#ifdef SQLITE_USER_AUTHENTICATION
109712 if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
109713 u8 newAuth = 0;
109714 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
109715 if( newAuth<db->auth.authLevel ){
109716 rc = SQLITE_AUTH_USER;
109717 }
109718 }
109719#endif
109720 if( rc ){
109721 if( !REOPEN_AS_MEMDB(db) ){
109722 int iDb = db->nDb - 1;
109723 assert( iDb>=2 );
109724 if( db->aDb[iDb].pBt ){
109725 sqlite3BtreeClose(db->aDb[iDb].pBt);
109726 db->aDb[iDb].pBt = 0;
109727 db->aDb[iDb].pSchema = 0;
109728 }
109729 sqlite3ResetAllSchemasOfConnection(db);
109730 db->nDb = iDb;
109731 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109732 sqlite3OomFault(db);
109733 sqlite3DbFree(db, zErrDyn);
109734 zErrDyn = sqlite3MPrintf(db, "out of memory");
109735 }else if( zErrDyn==0 ){
109736 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
109737 }
109738 }
109739 goto attach_error;
109740 }
109741
109742 return;
109743
109744attach_error:
109745 /* Return an error if we get here */
109746 if( zErrDyn ){
109747 sqlite3_result_error(context, zErrDyn, -1);
109748 sqlite3DbFree(db, zErrDyn);
109749 }
109750 if( rc ) sqlite3_result_error_code(context, rc);
109751}
109752
109753/*
109754** An SQL user-function registered to do the work of an DETACH statement. The
109755** three arguments to the function come directly from a detach statement:
109756**
109757** DETACH DATABASE x
109758**
109759** SELECT sqlite_detach(x)
109760*/
109761static void detachFunc(
109762 sqlite3_context *context,
109763 int NotUsed,
109764 sqlite3_value **argv
109765){
109766 const char *zName = (const char *)sqlite3_value_text(argv[0]);
109767 sqlite3 *db = sqlite3_context_db_handle(context);
109768 int i;
109769 Db *pDb = 0;
109770 HashElem *pEntry;
109771 char zErr[128];
109772
109773 UNUSED_PARAMETER(NotUsed);
109774
109775 if( zName==0 ) zName = "";
109776 for(i=0; i<db->nDb; i++){
109777 pDb = &db->aDb[i];
109778 if( pDb->pBt==0 ) continue;
109779 if( sqlite3DbIsNamed(db, i, zName) ) break;
109780 }
109781
109782 if( i>=db->nDb ){
109783 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
109784 goto detach_error;
109785 }
109786 if( i<2 ){
109787 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
109788 goto detach_error;
109789 }
109790 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
109791 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
109792 goto detach_error;
109793 }
109794
109795 /* If any TEMP triggers reference the schema being detached, move those
109796 ** triggers to reference the TEMP schema itself. */
109797 assert( db->aDb[1].pSchema );
109798 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
109799 while( pEntry ){
109800 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
109801 if( pTrig->pTabSchema==pDb->pSchema ){
109802 pTrig->pTabSchema = pTrig->pSchema;
109803 }
109804 pEntry = sqliteHashNext(pEntry);
109805 }
109806
109807 sqlite3BtreeClose(pDb->pBt);
109808 pDb->pBt = 0;
109809 pDb->pSchema = 0;
109810 sqlite3CollapseDatabaseArray(db);
109811 return;
109812
109813detach_error:
109814 sqlite3_result_error(context, zErr, -1);
109815}
109816
109817/*
109818** This procedure generates VDBE code for a single invocation of either the
109819** sqlite_detach() or sqlite_attach() SQL user functions.
109820*/
109821static void codeAttach(
109822 Parse *pParse, /* The parser context */
109823 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
109824 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
109825 Expr *pAuthArg, /* Expression to pass to authorization callback */
109826 Expr *pFilename, /* Name of database file */
109827 Expr *pDbname, /* Name of the database to use internally */
109828 Expr *pKey /* Database key for encryption extension */
109829){
109830 int rc;
109831 NameContext sName;
109832 Vdbe *v;
109833 sqlite3* db = pParse->db;
109834 int regArgs;
109835
109836 if( pParse->nErr ) goto attach_end;
109837 memset(&sName, 0, sizeof(NameContext));
109838 sName.pParse = pParse;
109839
109840 if(
109841 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
109842 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
109843 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
109844 ){
109845 goto attach_end;
109846 }
109847
109848#ifndef SQLITE_OMIT_AUTHORIZATION
109849 if( pAuthArg ){
109850 char *zAuthArg;
109851 if( pAuthArg->op==TK_STRING ){
109852 zAuthArg = pAuthArg->u.zToken;
109853 }else{
109854 zAuthArg = 0;
109855 }
109856 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
109857 if(rc!=SQLITE_OK ){
109858 goto attach_end;
109859 }
109860 }
109861#endif /* SQLITE_OMIT_AUTHORIZATION */
109862
109863
109864 v = sqlite3GetVdbe(pParse);
109865 regArgs = sqlite3GetTempRange(pParse, 4);
109866 sqlite3ExprCode(pParse, pFilename, regArgs);
109867 sqlite3ExprCode(pParse, pDbname, regArgs+1);
109868 sqlite3ExprCode(pParse, pKey, regArgs+2);
109869
109870 assert( v || db->mallocFailed );
109871 if( v ){
109872 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
109873 pFunc->nArg, pFunc, 0);
109874 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
109875 ** statement only). For DETACH, set it to false (expire all existing
109876 ** statements).
109877 */
109878 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
109879 }
109880
109881attach_end:
109882 sqlite3ExprDelete(db, pFilename);
109883 sqlite3ExprDelete(db, pDbname);
109884 sqlite3ExprDelete(db, pKey);
109885}
109886
109887/*
109888** Called by the parser to compile a DETACH statement.
109889**
109890** DETACH pDbname
109891*/
109892SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
109893 static const FuncDef detach_func = {
109894 1, /* nArg */
109895 SQLITE_UTF8, /* funcFlags */
109896 0, /* pUserData */
109897 0, /* pNext */
109898 detachFunc, /* xSFunc */
109899 0, /* xFinalize */
109900 0, 0, /* xValue, xInverse */
109901 "sqlite_detach", /* zName */
109902 {0}
109903 };
109904 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
109905}
109906
109907/*
109908** Called by the parser to compile an ATTACH statement.
109909**
109910** ATTACH p AS pDbname KEY pKey
109911*/
109912SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
109913 static const FuncDef attach_func = {
109914 3, /* nArg */
109915 SQLITE_UTF8, /* funcFlags */
109916 0, /* pUserData */
109917 0, /* pNext */
109918 attachFunc, /* xSFunc */
109919 0, /* xFinalize */
109920 0, 0, /* xValue, xInverse */
109921 "sqlite_attach", /* zName */
109922 {0}
109923 };
109924 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
109925}
109926#endif /* SQLITE_OMIT_ATTACH */
109927
109928/*
109929** Initialize a DbFixer structure. This routine must be called prior
109930** to passing the structure to one of the sqliteFixAAAA() routines below.
109931*/
109932SQLITE_PRIVATE void sqlite3FixInit(
109933 DbFixer *pFix, /* The fixer to be initialized */
109934 Parse *pParse, /* Error messages will be written here */
109935 int iDb, /* This is the database that must be used */
109936 const char *zType, /* "view", "trigger", or "index" */
109937 const Token *pName /* Name of the view, trigger, or index */
109938){
109939 sqlite3 *db;
109940
109941 db = pParse->db;
109942 assert( db->nDb>iDb );
109943 pFix->pParse = pParse;
109944 pFix->zDb = db->aDb[iDb].zDbSName;
109945 pFix->pSchema = db->aDb[iDb].pSchema;
109946 pFix->zType = zType;
109947 pFix->pName = pName;
109948 pFix->bTemp = (iDb==1);
109949}
109950
109951/*
109952** The following set of routines walk through the parse tree and assign
109953** a specific database to all table references where the database name
109954** was left unspecified in the original SQL statement. The pFix structure
109955** must have been initialized by a prior call to sqlite3FixInit().
109956**
109957** These routines are used to make sure that an index, trigger, or
109958** view in one database does not refer to objects in a different database.
109959** (Exception: indices, triggers, and views in the TEMP database are
109960** allowed to refer to anything.) If a reference is explicitly made
109961** to an object in a different database, an error message is added to
109962** pParse->zErrMsg and these routines return non-zero. If everything
109963** checks out, these routines return 0.
109964*/
109965SQLITE_PRIVATE int sqlite3FixSrcList(
109966 DbFixer *pFix, /* Context of the fixation */
109967 SrcList *pList /* The Source list to check and modify */
109968){
109969 int i;
109970 struct SrcList_item *pItem;
109971 sqlite3 *db = pFix->pParse->db;
109972 int iDb = sqlite3FindDbName(db, pFix->zDb);
109973
109974 if( NEVER(pList==0) ) return 0;
109975
109976 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
109977 if( pFix->bTemp==0 ){
109978 if( pItem->zDatabase && iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
109979 sqlite3ErrorMsg(pFix->pParse,
109980 "%s %T cannot reference objects in database %s",
109981 pFix->zType, pFix->pName, pItem->zDatabase);
109982 return 1;
109983 }
109984 sqlite3DbFree(db, pItem->zDatabase);
109985 pItem->zDatabase = 0;
109986 pItem->pSchema = pFix->pSchema;
109987 pItem->fg.fromDDL = 1;
109988 }
109989#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
109990 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
109991 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
109992#endif
109993 if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
109994 return 1;
109995 }
109996 }
109997 return 0;
109998}
109999#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
110000SQLITE_PRIVATE int sqlite3FixSelect(
110001 DbFixer *pFix, /* Context of the fixation */
110002 Select *pSelect /* The SELECT statement to be fixed to one database */
110003){
110004 while( pSelect ){
110005 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
110006 return 1;
110007 }
110008 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
110009 return 1;
110010 }
110011 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
110012 return 1;
110013 }
110014 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
110015 return 1;
110016 }
110017 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
110018 return 1;
110019 }
110020 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
110021 return 1;
110022 }
110023 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
110024 return 1;
110025 }
110026 if( pSelect->pWith ){
110027 int i;
110028 for(i=0; i<pSelect->pWith->nCte; i++){
110029 if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
110030 return 1;
110031 }
110032 }
110033 }
110034 pSelect = pSelect->pPrior;
110035 }
110036 return 0;
110037}
110038SQLITE_PRIVATE int sqlite3FixExpr(
110039 DbFixer *pFix, /* Context of the fixation */
110040 Expr *pExpr /* The expression to be fixed to one database */
110041){
110042 while( pExpr ){
110043 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
110044 if( pExpr->op==TK_VARIABLE ){
110045 if( pFix->pParse->db->init.busy ){
110046 pExpr->op = TK_NULL;
110047 }else{
110048 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
110049 return 1;
110050 }
110051 }
110052 if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
110053 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110054 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
110055 }else{
110056 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
110057 }
110058 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
110059 return 1;
110060 }
110061 pExpr = pExpr->pLeft;
110062 }
110063 return 0;
110064}
110065SQLITE_PRIVATE int sqlite3FixExprList(
110066 DbFixer *pFix, /* Context of the fixation */
110067 ExprList *pList /* The expression to be fixed to one database */
110068){
110069 int i;
110070 struct ExprList_item *pItem;
110071 if( pList==0 ) return 0;
110072 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
110073 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
110074 return 1;
110075 }
110076 }
110077 return 0;
110078}
110079#endif
110080
110081#ifndef SQLITE_OMIT_TRIGGER
110082SQLITE_PRIVATE int sqlite3FixTriggerStep(
110083 DbFixer *pFix, /* Context of the fixation */
110084 TriggerStep *pStep /* The trigger step be fixed to one database */
110085){
110086 while( pStep ){
110087 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
110088 return 1;
110089 }
110090 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
110091 return 1;
110092 }
110093 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
110094 return 1;
110095 }
110096 if( pStep->pFrom && sqlite3FixSrcList(pFix, pStep->pFrom) ){
110097 return 1;
110098 }
110099#ifndef SQLITE_OMIT_UPSERT
110100 if( pStep->pUpsert ){
110101 Upsert *pUp = pStep->pUpsert;
110102 if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
110103 || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
110104 || sqlite3FixExprList(pFix, pUp->pUpsertSet)
110105 || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
110106 ){
110107 return 1;
110108 }
110109 }
110110#endif
110111 pStep = pStep->pNext;
110112 }
110113 return 0;
110114}
110115#endif
110116
110117/************** End of attach.c **********************************************/
110118/************** Begin file auth.c ********************************************/
110119/*
110120** 2003 January 11
110121**
110122** The author disclaims copyright to this source code. In place of
110123** a legal notice, here is a blessing:
110124**
110125** May you do good and not evil.
110126** May you find forgiveness for yourself and forgive others.
110127** May you share freely, never taking more than you give.
110128**
110129*************************************************************************
110130** This file contains code used to implement the sqlite3_set_authorizer()
110131** API. This facility is an optional feature of the library. Embedded
110132** systems that do not need this facility may omit it by recompiling
110133** the library with -DSQLITE_OMIT_AUTHORIZATION=1
110134*/
110135/* #include "sqliteInt.h" */
110136
110137/*
110138** All of the code in this file may be omitted by defining a single
110139** macro.
110140*/
110141#ifndef SQLITE_OMIT_AUTHORIZATION
110142
110143/*
110144** Set or clear the access authorization function.
110145**
110146** The access authorization function is be called during the compilation
110147** phase to verify that the user has read and/or write access permission on
110148** various fields of the database. The first argument to the auth function
110149** is a copy of the 3rd argument to this routine. The second argument
110150** to the auth function is one of these constants:
110151**
110152** SQLITE_CREATE_INDEX
110153** SQLITE_CREATE_TABLE
110154** SQLITE_CREATE_TEMP_INDEX
110155** SQLITE_CREATE_TEMP_TABLE
110156** SQLITE_CREATE_TEMP_TRIGGER
110157** SQLITE_CREATE_TEMP_VIEW
110158** SQLITE_CREATE_TRIGGER
110159** SQLITE_CREATE_VIEW
110160** SQLITE_DELETE
110161** SQLITE_DROP_INDEX
110162** SQLITE_DROP_TABLE
110163** SQLITE_DROP_TEMP_INDEX
110164** SQLITE_DROP_TEMP_TABLE
110165** SQLITE_DROP_TEMP_TRIGGER
110166** SQLITE_DROP_TEMP_VIEW
110167** SQLITE_DROP_TRIGGER
110168** SQLITE_DROP_VIEW
110169** SQLITE_INSERT
110170** SQLITE_PRAGMA
110171** SQLITE_READ
110172** SQLITE_SELECT
110173** SQLITE_TRANSACTION
110174** SQLITE_UPDATE
110175**
110176** The third and fourth arguments to the auth function are the name of
110177** the table and the column that are being accessed. The auth function
110178** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
110179** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
110180** means that the SQL statement will never-run - the sqlite3_exec() call
110181** will return with an error. SQLITE_IGNORE means that the SQL statement
110182** should run but attempts to read the specified column will return NULL
110183** and attempts to write the column will be ignored.
110184**
110185** Setting the auth function to NULL disables this hook. The default
110186** setting of the auth function is NULL.
110187*/
110188SQLITE_API int sqlite3_set_authorizer(
110189 sqlite3 *db,
110190 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
110191 void *pArg
110192){
110193#ifdef SQLITE_ENABLE_API_ARMOR
110194 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
110195#endif
110196 sqlite3_mutex_enter(db->mutex);
110197 db->xAuth = (sqlite3_xauth)xAuth;
110198 db->pAuthArg = pArg;
110199 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
110200 sqlite3_mutex_leave(db->mutex);
110201 return SQLITE_OK;
110202}
110203
110204/*
110205** Write an error message into pParse->zErrMsg that explains that the
110206** user-supplied authorization function returned an illegal value.
110207*/
110208static void sqliteAuthBadReturnCode(Parse *pParse){
110209 sqlite3ErrorMsg(pParse, "authorizer malfunction");
110210 pParse->rc = SQLITE_ERROR;
110211}
110212
110213/*
110214** Invoke the authorization callback for permission to read column zCol from
110215** table zTab in database zDb. This function assumes that an authorization
110216** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
110217**
110218** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
110219** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
110220** is treated as SQLITE_DENY. In this case an error is left in pParse.
110221*/
110222SQLITE_PRIVATE int sqlite3AuthReadCol(
110223 Parse *pParse, /* The parser context */
110224 const char *zTab, /* Table name */
110225 const char *zCol, /* Column name */
110226 int iDb /* Index of containing database. */
110227){
110228 sqlite3 *db = pParse->db; /* Database handle */
110229 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
110230 int rc; /* Auth callback return code */
110231
110232 if( db->init.busy ) return SQLITE_OK;
110233 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
110234#ifdef SQLITE_USER_AUTHENTICATION
110235 ,db->auth.zAuthUser
110236#endif
110237 );
110238 if( rc==SQLITE_DENY ){
110239 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
110240 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
110241 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
110242 pParse->rc = SQLITE_AUTH;
110243 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
110244 sqliteAuthBadReturnCode(pParse);
110245 }
110246 return rc;
110247}
110248
110249/*
110250** The pExpr should be a TK_COLUMN expression. The table referred to
110251** is in pTabList or else it is the NEW or OLD table of a trigger.
110252** Check to see if it is OK to read this particular column.
110253**
110254** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
110255** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
110256** then generate an error.
110257*/
110258SQLITE_PRIVATE void sqlite3AuthRead(
110259 Parse *pParse, /* The parser context */
110260 Expr *pExpr, /* The expression to check authorization on */
110261 Schema *pSchema, /* The schema of the expression */
110262 SrcList *pTabList /* All table that pExpr might refer to */
110263){
110264 sqlite3 *db = pParse->db;
110265 Table *pTab = 0; /* The table being read */
110266 const char *zCol; /* Name of the column of the table */
110267 int iSrc; /* Index in pTabList->a[] of table being read */
110268 int iDb; /* The index of the database the expression refers to */
110269 int iCol; /* Index of column in table */
110270
110271 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
110272 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
110273 if( db->xAuth==0 ) return;
110274 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
110275 if( iDb<0 ){
110276 /* An attempt to read a column out of a subquery or other
110277 ** temporary table. */
110278 return;
110279 }
110280
110281 if( pExpr->op==TK_TRIGGER ){
110282 pTab = pParse->pTriggerTab;
110283 }else{
110284 assert( pTabList );
110285 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
110286 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
110287 pTab = pTabList->a[iSrc].pTab;
110288 break;
110289 }
110290 }
110291 }
110292 iCol = pExpr->iColumn;
110293 if( NEVER(pTab==0) ) return;
110294
110295 if( iCol>=0 ){
110296 assert( iCol<pTab->nCol );
110297 zCol = pTab->aCol[iCol].zName;
110298 }else if( pTab->iPKey>=0 ){
110299 assert( pTab->iPKey<pTab->nCol );
110300 zCol = pTab->aCol[pTab->iPKey].zName;
110301 }else{
110302 zCol = "ROWID";
110303 }
110304 assert( iDb>=0 && iDb<db->nDb );
110305 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
110306 pExpr->op = TK_NULL;
110307 }
110308}
110309
110310/*
110311** Do an authorization check using the code and arguments given. Return
110312** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
110313** is returned, then the error count and error message in pParse are
110314** modified appropriately.
110315*/
110316SQLITE_PRIVATE int sqlite3AuthCheck(
110317 Parse *pParse,
110318 int code,
110319 const char *zArg1,
110320 const char *zArg2,
110321 const char *zArg3
110322){
110323 sqlite3 *db = pParse->db;
110324 int rc;
110325
110326 /* Don't do any authorization checks if the database is initialising
110327 ** or if the parser is being invoked from within sqlite3_declare_vtab.
110328 */
110329 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
110330 if( db->init.busy || IN_SPECIAL_PARSE ){
110331 return SQLITE_OK;
110332 }
110333
110334 if( db->xAuth==0 ){
110335 return SQLITE_OK;
110336 }
110337
110338 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
110339 ** callback are either NULL pointers or zero-terminated strings that
110340 ** contain additional details about the action to be authorized.
110341 **
110342 ** The following testcase() macros show that any of the 3rd through 6th
110343 ** parameters can be either NULL or a string. */
110344 testcase( zArg1==0 );
110345 testcase( zArg2==0 );
110346 testcase( zArg3==0 );
110347 testcase( pParse->zAuthContext==0 );
110348
110349 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
110350#ifdef SQLITE_USER_AUTHENTICATION
110351 ,db->auth.zAuthUser
110352#endif
110353 );
110354 if( rc==SQLITE_DENY ){
110355 sqlite3ErrorMsg(pParse, "not authorized");
110356 pParse->rc = SQLITE_AUTH;
110357 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
110358 rc = SQLITE_DENY;
110359 sqliteAuthBadReturnCode(pParse);
110360 }
110361 return rc;
110362}
110363
110364/*
110365** Push an authorization context. After this routine is called, the
110366** zArg3 argument to authorization callbacks will be zContext until
110367** popped. Or if pParse==0, this routine is a no-op.
110368*/
110369SQLITE_PRIVATE void sqlite3AuthContextPush(
110370 Parse *pParse,
110371 AuthContext *pContext,
110372 const char *zContext
110373){
110374 assert( pParse );
110375 pContext->pParse = pParse;
110376 pContext->zAuthContext = pParse->zAuthContext;
110377 pParse->zAuthContext = zContext;
110378}
110379
110380/*
110381** Pop an authorization context that was previously pushed
110382** by sqlite3AuthContextPush
110383*/
110384SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
110385 if( pContext->pParse ){
110386 pContext->pParse->zAuthContext = pContext->zAuthContext;
110387 pContext->pParse = 0;
110388 }
110389}
110390
110391#endif /* SQLITE_OMIT_AUTHORIZATION */
110392
110393/************** End of auth.c ************************************************/
110394/************** Begin file build.c *******************************************/
110395/*
110396** 2001 September 15
110397**
110398** The author disclaims copyright to this source code. In place of
110399** a legal notice, here is a blessing:
110400**
110401** May you do good and not evil.
110402** May you find forgiveness for yourself and forgive others.
110403** May you share freely, never taking more than you give.
110404**
110405*************************************************************************
110406** This file contains C code routines that are called by the SQLite parser
110407** when syntax rules are reduced. The routines in this file handle the
110408** following kinds of SQL syntax:
110409**
110410** CREATE TABLE
110411** DROP TABLE
110412** CREATE INDEX
110413** DROP INDEX
110414** creating ID lists
110415** BEGIN TRANSACTION
110416** COMMIT
110417** ROLLBACK
110418*/
110419/* #include "sqliteInt.h" */
110420
110421#ifndef SQLITE_OMIT_SHARED_CACHE
110422/*
110423** The TableLock structure is only used by the sqlite3TableLock() and
110424** codeTableLocks() functions.
110425*/
110426struct TableLock {
110427 int iDb; /* The database containing the table to be locked */
110428 Pgno iTab; /* The root page of the table to be locked */
110429 u8 isWriteLock; /* True for write lock. False for a read lock */
110430 const char *zLockName; /* Name of the table */
110431};
110432
110433/*
110434** Record the fact that we want to lock a table at run-time.
110435**
110436** The table to be locked has root page iTab and is found in database iDb.
110437** A read or a write lock can be taken depending on isWritelock.
110438**
110439** This routine just records the fact that the lock is desired. The
110440** code to make the lock occur is generated by a later call to
110441** codeTableLocks() which occurs during sqlite3FinishCoding().
110442*/
110443SQLITE_PRIVATE void sqlite3TableLock(
110444 Parse *pParse, /* Parsing context */
110445 int iDb, /* Index of the database containing the table to lock */
110446 Pgno iTab, /* Root page number of the table to be locked */
110447 u8 isWriteLock, /* True for a write lock */
110448 const char *zName /* Name of the table to be locked */
110449){
110450 Parse *pToplevel = sqlite3ParseToplevel(pParse);
110451 int i;
110452 int nBytes;
110453 TableLock *p;
110454 assert( iDb>=0 );
110455
110456 if( iDb==1 ) return;
110457 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
110458 for(i=0; i<pToplevel->nTableLock; i++){
110459 p = &pToplevel->aTableLock[i];
110460 if( p->iDb==iDb && p->iTab==iTab ){
110461 p->isWriteLock = (p->isWriteLock || isWriteLock);
110462 return;
110463 }
110464 }
110465
110466 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
110467 pToplevel->aTableLock =
110468 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
110469 if( pToplevel->aTableLock ){
110470 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
110471 p->iDb = iDb;
110472 p->iTab = iTab;
110473 p->isWriteLock = isWriteLock;
110474 p->zLockName = zName;
110475 }else{
110476 pToplevel->nTableLock = 0;
110477 sqlite3OomFault(pToplevel->db);
110478 }
110479}
110480
110481/*
110482** Code an OP_TableLock instruction for each table locked by the
110483** statement (configured by calls to sqlite3TableLock()).
110484*/
110485static void codeTableLocks(Parse *pParse){
110486 int i;
110487 Vdbe *pVdbe;
110488
110489 pVdbe = sqlite3GetVdbe(pParse);
110490 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
110491
110492 for(i=0; i<pParse->nTableLock; i++){
110493 TableLock *p = &pParse->aTableLock[i];
110494 int p1 = p->iDb;
110495 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
110496 p->zLockName, P4_STATIC);
110497 }
110498}
110499#else
110500 #define codeTableLocks(x)
110501#endif
110502
110503/*
110504** Return TRUE if the given yDbMask object is empty - if it contains no
110505** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
110506** macros when SQLITE_MAX_ATTACHED is greater than 30.
110507*/
110508#if SQLITE_MAX_ATTACHED>30
110509SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
110510 int i;
110511 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
110512 return 1;
110513}
110514#endif
110515
110516/*
110517** This routine is called after a single SQL statement has been
110518** parsed and a VDBE program to execute that statement has been
110519** prepared. This routine puts the finishing touches on the
110520** VDBE program and resets the pParse structure for the next
110521** parse.
110522**
110523** Note that if an error occurred, it might be the case that
110524** no VDBE code was generated.
110525*/
110526SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
110527 sqlite3 *db;
110528 Vdbe *v;
110529
110530 assert( pParse->pToplevel==0 );
110531 db = pParse->db;
110532 if( pParse->nested ) return;
110533 if( db->mallocFailed || pParse->nErr ){
110534 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
110535 return;
110536 }
110537
110538 /* Begin by generating some termination code at the end of the
110539 ** vdbe program
110540 */
110541 v = sqlite3GetVdbe(pParse);
110542 assert( !pParse->isMultiWrite
110543 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
110544 if( v ){
110545 sqlite3VdbeAddOp0(v, OP_Halt);
110546
110547#if SQLITE_USER_AUTHENTICATION
110548 if( pParse->nTableLock>0 && db->init.busy==0 ){
110549 sqlite3UserAuthInit(db);
110550 if( db->auth.authLevel<UAUTH_User ){
110551 sqlite3ErrorMsg(pParse, "user not authenticated");
110552 pParse->rc = SQLITE_AUTH_USER;
110553 return;
110554 }
110555 }
110556#endif
110557
110558 /* The cookie mask contains one bit for each database file open.
110559 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
110560 ** set for each database that is used. Generate code to start a
110561 ** transaction on each used database and to verify the schema cookie
110562 ** on each used database.
110563 */
110564 if( db->mallocFailed==0
110565 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
110566 ){
110567 int iDb, i;
110568 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
110569 sqlite3VdbeJumpHere(v, 0);
110570 for(iDb=0; iDb<db->nDb; iDb++){
110571 Schema *pSchema;
110572 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
110573 sqlite3VdbeUsesBtree(v, iDb);
110574 pSchema = db->aDb[iDb].pSchema;
110575 sqlite3VdbeAddOp4Int(v,
110576 OP_Transaction, /* Opcode */
110577 iDb, /* P1 */
110578 DbMaskTest(pParse->writeMask,iDb), /* P2 */
110579 pSchema->schema_cookie, /* P3 */
110580 pSchema->iGeneration /* P4 */
110581 );
110582 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
110583 VdbeComment((v,
110584 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
110585 }
110586#ifndef SQLITE_OMIT_VIRTUALTABLE
110587 for(i=0; i<pParse->nVtabLock; i++){
110588 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
110589 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
110590 }
110591 pParse->nVtabLock = 0;
110592#endif
110593
110594 /* Once all the cookies have been verified and transactions opened,
110595 ** obtain the required table-locks. This is a no-op unless the
110596 ** shared-cache feature is enabled.
110597 */
110598 codeTableLocks(pParse);
110599
110600 /* Initialize any AUTOINCREMENT data structures required.
110601 */
110602 sqlite3AutoincrementBegin(pParse);
110603
110604 /* Code constant expressions that where factored out of inner loops.
110605 **
110606 ** The pConstExpr list might also contain expressions that we simply
110607 ** want to keep around until the Parse object is deleted. Such
110608 ** expressions have iConstExprReg==0. Do not generate code for
110609 ** those expressions, of course.
110610 */
110611 if( pParse->pConstExpr ){
110612 ExprList *pEL = pParse->pConstExpr;
110613 pParse->okConstFactor = 0;
110614 for(i=0; i<pEL->nExpr; i++){
110615 int iReg = pEL->a[i].u.iConstExprReg;
110616 if( iReg>0 ){
110617 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
110618 }
110619 }
110620 }
110621
110622 /* Finally, jump back to the beginning of the executable code. */
110623 sqlite3VdbeGoto(v, 1);
110624 }
110625 }
110626
110627
110628 /* Get the VDBE program ready for execution
110629 */
110630 if( v && pParse->nErr==0 && !db->mallocFailed ){
110631 /* A minimum of one cursor is required if autoincrement is used
110632 * See ticket [a696379c1f08866] */
110633 assert( pParse->pAinc==0 || pParse->nTab>0 );
110634 sqlite3VdbeMakeReady(v, pParse);
110635 pParse->rc = SQLITE_DONE;
110636 }else{
110637 pParse->rc = SQLITE_ERROR;
110638 }
110639}
110640
110641/*
110642** Run the parser and code generator recursively in order to generate
110643** code for the SQL statement given onto the end of the pParse context
110644** currently under construction. When the parser is run recursively
110645** this way, the final OP_Halt is not appended and other initialization
110646** and finalization steps are omitted because those are handling by the
110647** outermost parser.
110648**
110649** Not everything is nestable. This facility is designed to permit
110650** INSERT, UPDATE, and DELETE operations against the schema table. Use
110651** care if you decide to try to use this routine for some other purposes.
110652*/
110653SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
110654 va_list ap;
110655 char *zSql;
110656 char *zErrMsg = 0;
110657 sqlite3 *db = pParse->db;
110658 char saveBuf[PARSE_TAIL_SZ];
110659
110660 if( pParse->nErr ) return;
110661 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
110662 va_start(ap, zFormat);
110663 zSql = sqlite3VMPrintf(db, zFormat, ap);
110664 va_end(ap);
110665 if( zSql==0 ){
110666 /* This can result either from an OOM or because the formatted string
110667 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
110668 ** an error */
110669 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
110670 pParse->nErr++;
110671 return;
110672 }
110673 pParse->nested++;
110674 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
110675 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
110676 sqlite3RunParser(pParse, zSql, &zErrMsg);
110677 sqlite3DbFree(db, zErrMsg);
110678 sqlite3DbFree(db, zSql);
110679 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
110680 pParse->nested--;
110681}
110682
110683#if SQLITE_USER_AUTHENTICATION
110684/*
110685** Return TRUE if zTable is the name of the system table that stores the
110686** list of users and their access credentials.
110687*/
110688SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
110689 return sqlite3_stricmp(zTable, "sqlite_user")==0;
110690}
110691#endif
110692
110693/*
110694** Locate the in-memory structure that describes a particular database
110695** table given the name of that table and (optionally) the name of the
110696** database containing the table. Return NULL if not found.
110697**
110698** If zDatabase is 0, all databases are searched for the table and the
110699** first matching table is returned. (No checking for duplicate table
110700** names is done.) The search order is TEMP first, then MAIN, then any
110701** auxiliary databases added using the ATTACH command.
110702**
110703** See also sqlite3LocateTable().
110704*/
110705SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
110706 Table *p = 0;
110707 int i;
110708
110709 /* All mutexes are required for schema access. Make sure we hold them. */
110710 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
110711#if SQLITE_USER_AUTHENTICATION
110712 /* Only the admin user is allowed to know that the sqlite_user table
110713 ** exists */
110714 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
110715 return 0;
110716 }
110717#endif
110718 if( zDatabase ){
110719 for(i=0; i<db->nDb; i++){
110720 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
110721 }
110722 if( i>=db->nDb ){
110723 /* No match against the official names. But always match "main"
110724 ** to schema 0 as a legacy fallback. */
110725 if( sqlite3StrICmp(zDatabase,"main")==0 ){
110726 i = 0;
110727 }else{
110728 return 0;
110729 }
110730 }
110731 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
110732 if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
110733 if( i==1 ){
110734 if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0
110735 || sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0
110736 || sqlite3StrICmp(zName+7, &DFLT_SCHEMA_TABLE[7])==0
110737 ){
110738 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
110739 DFLT_TEMP_SCHEMA_TABLE);
110740 }
110741 }else{
110742 if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){
110743 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
110744 DFLT_SCHEMA_TABLE);
110745 }
110746 }
110747 }
110748 }else{
110749 /* Match against TEMP first */
110750 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
110751 if( p ) return p;
110752 /* The main database is second */
110753 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
110754 if( p ) return p;
110755 /* Attached databases are in order of attachment */
110756 for(i=2; i<db->nDb; i++){
110757 assert( sqlite3SchemaMutexHeld(db, i, 0) );
110758 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
110759 if( p ) break;
110760 }
110761 if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
110762 if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){
110763 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, DFLT_SCHEMA_TABLE);
110764 }else if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 ){
110765 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
110766 DFLT_TEMP_SCHEMA_TABLE);
110767 }
110768 }
110769 }
110770 return p;
110771}
110772
110773/*
110774** Locate the in-memory structure that describes a particular database
110775** table given the name of that table and (optionally) the name of the
110776** database containing the table. Return NULL if not found. Also leave an
110777** error message in pParse->zErrMsg.
110778**
110779** The difference between this routine and sqlite3FindTable() is that this
110780** routine leaves an error message in pParse->zErrMsg where
110781** sqlite3FindTable() does not.
110782*/
110783SQLITE_PRIVATE Table *sqlite3LocateTable(
110784 Parse *pParse, /* context in which to report errors */
110785 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
110786 const char *zName, /* Name of the table we are looking for */
110787 const char *zDbase /* Name of the database. Might be NULL */
110788){
110789 Table *p;
110790 sqlite3 *db = pParse->db;
110791
110792 /* Read the database schema. If an error occurs, leave an error message
110793 ** and code in pParse and return NULL. */
110794 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
110795 && SQLITE_OK!=sqlite3ReadSchema(pParse)
110796 ){
110797 return 0;
110798 }
110799
110800 p = sqlite3FindTable(db, zName, zDbase);
110801 if( p==0 ){
110802#ifndef SQLITE_OMIT_VIRTUALTABLE
110803 /* If zName is the not the name of a table in the schema created using
110804 ** CREATE, then check to see if it is the name of an virtual table that
110805 ** can be an eponymous virtual table. */
110806 if( pParse->disableVtab==0 ){
110807 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
110808 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
110809 pMod = sqlite3PragmaVtabRegister(db, zName);
110810 }
110811 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
110812 return pMod->pEpoTab;
110813 }
110814 }
110815#endif
110816 if( flags & LOCATE_NOERR ) return 0;
110817 pParse->checkSchema = 1;
110818 }else if( IsVirtual(p) && pParse->disableVtab ){
110819 p = 0;
110820 }
110821
110822 if( p==0 ){
110823 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
110824 if( zDbase ){
110825 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
110826 }else{
110827 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
110828 }
110829 }
110830
110831 return p;
110832}
110833
110834/*
110835** Locate the table identified by *p.
110836**
110837** This is a wrapper around sqlite3LocateTable(). The difference between
110838** sqlite3LocateTable() and this function is that this function restricts
110839** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
110840** non-NULL if it is part of a view or trigger program definition. See
110841** sqlite3FixSrcList() for details.
110842*/
110843SQLITE_PRIVATE Table *sqlite3LocateTableItem(
110844 Parse *pParse,
110845 u32 flags,
110846 struct SrcList_item *p
110847){
110848 const char *zDb;
110849 assert( p->pSchema==0 || p->zDatabase==0 );
110850 if( p->pSchema ){
110851 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
110852 zDb = pParse->db->aDb[iDb].zDbSName;
110853 }else{
110854 zDb = p->zDatabase;
110855 }
110856 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
110857}
110858
110859/*
110860** Locate the in-memory structure that describes
110861** a particular index given the name of that index
110862** and the name of the database that contains the index.
110863** Return NULL if not found.
110864**
110865** If zDatabase is 0, all databases are searched for the
110866** table and the first matching index is returned. (No checking
110867** for duplicate index names is done.) The search order is
110868** TEMP first, then MAIN, then any auxiliary databases added
110869** using the ATTACH command.
110870*/
110871SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
110872 Index *p = 0;
110873 int i;
110874 /* All mutexes are required for schema access. Make sure we hold them. */
110875 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
110876 for(i=OMIT_TEMPDB; i<db->nDb; i++){
110877 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
110878 Schema *pSchema = db->aDb[j].pSchema;
110879 assert( pSchema );
110880 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
110881 assert( sqlite3SchemaMutexHeld(db, j, 0) );
110882 p = sqlite3HashFind(&pSchema->idxHash, zName);
110883 if( p ) break;
110884 }
110885 return p;
110886}
110887
110888/*
110889** Reclaim the memory used by an index
110890*/
110891SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
110892#ifndef SQLITE_OMIT_ANALYZE
110893 sqlite3DeleteIndexSamples(db, p);
110894#endif
110895 sqlite3ExprDelete(db, p->pPartIdxWhere);
110896 sqlite3ExprListDelete(db, p->aColExpr);
110897 sqlite3DbFree(db, p->zColAff);
110898 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
110899#ifdef SQLITE_ENABLE_STAT4
110900 sqlite3_free(p->aiRowEst);
110901#endif
110902 sqlite3DbFree(db, p);
110903}
110904
110905/*
110906** For the index called zIdxName which is found in the database iDb,
110907** unlike that index from its Table then remove the index from
110908** the index hash table and free all memory structures associated
110909** with the index.
110910*/
110911SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
110912 Index *pIndex;
110913 Hash *pHash;
110914
110915 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
110916 pHash = &db->aDb[iDb].pSchema->idxHash;
110917 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
110918 if( ALWAYS(pIndex) ){
110919 if( pIndex->pTable->pIndex==pIndex ){
110920 pIndex->pTable->pIndex = pIndex->pNext;
110921 }else{
110922 Index *p;
110923 /* Justification of ALWAYS(); The index must be on the list of
110924 ** indices. */
110925 p = pIndex->pTable->pIndex;
110926 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
110927 if( ALWAYS(p && p->pNext==pIndex) ){
110928 p->pNext = pIndex->pNext;
110929 }
110930 }
110931 sqlite3FreeIndex(db, pIndex);
110932 }
110933 db->mDbFlags |= DBFLAG_SchemaChange;
110934}
110935
110936/*
110937** Look through the list of open database files in db->aDb[] and if
110938** any have been closed, remove them from the list. Reallocate the
110939** db->aDb[] structure to a smaller size, if possible.
110940**
110941** Entry 0 (the "main" database) and entry 1 (the "temp" database)
110942** are never candidates for being collapsed.
110943*/
110944SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
110945 int i, j;
110946 for(i=j=2; i<db->nDb; i++){
110947 struct Db *pDb = &db->aDb[i];
110948 if( pDb->pBt==0 ){
110949 sqlite3DbFree(db, pDb->zDbSName);
110950 pDb->zDbSName = 0;
110951 continue;
110952 }
110953 if( j<i ){
110954 db->aDb[j] = db->aDb[i];
110955 }
110956 j++;
110957 }
110958 db->nDb = j;
110959 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
110960 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
110961 sqlite3DbFree(db, db->aDb);
110962 db->aDb = db->aDbStatic;
110963 }
110964}
110965
110966/*
110967** Reset the schema for the database at index iDb. Also reset the
110968** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
110969** Deferred resets may be run by calling with iDb<0.
110970*/
110971SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
110972 int i;
110973 assert( iDb<db->nDb );
110974
110975 if( iDb>=0 ){
110976 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
110977 DbSetProperty(db, iDb, DB_ResetWanted);
110978 DbSetProperty(db, 1, DB_ResetWanted);
110979 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
110980 }
110981
110982 if( db->nSchemaLock==0 ){
110983 for(i=0; i<db->nDb; i++){
110984 if( DbHasProperty(db, i, DB_ResetWanted) ){
110985 sqlite3SchemaClear(db->aDb[i].pSchema);
110986 }
110987 }
110988 }
110989}
110990
110991/*
110992** Erase all schema information from all attached databases (including
110993** "main" and "temp") for a single database connection.
110994*/
110995SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
110996 int i;
110997 sqlite3BtreeEnterAll(db);
110998 for(i=0; i<db->nDb; i++){
110999 Db *pDb = &db->aDb[i];
111000 if( pDb->pSchema ){
111001 if( db->nSchemaLock==0 ){
111002 sqlite3SchemaClear(pDb->pSchema);
111003 }else{
111004 DbSetProperty(db, i, DB_ResetWanted);
111005 }
111006 }
111007 }
111008 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
111009 sqlite3VtabUnlockList(db);
111010 sqlite3BtreeLeaveAll(db);
111011 if( db->nSchemaLock==0 ){
111012 sqlite3CollapseDatabaseArray(db);
111013 }
111014}
111015
111016/*
111017** This routine is called when a commit occurs.
111018*/
111019SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
111020 db->mDbFlags &= ~DBFLAG_SchemaChange;
111021}
111022
111023/*
111024** Delete memory allocated for the column names of a table or view (the
111025** Table.aCol[] array).
111026*/
111027SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
111028 int i;
111029 Column *pCol;
111030 assert( pTable!=0 );
111031 if( (pCol = pTable->aCol)!=0 ){
111032 for(i=0; i<pTable->nCol; i++, pCol++){
111033 assert( pCol->zName==0 || pCol->hName==sqlite3StrIHash(pCol->zName) );
111034 sqlite3DbFree(db, pCol->zName);
111035 sqlite3ExprDelete(db, pCol->pDflt);
111036 sqlite3DbFree(db, pCol->zColl);
111037 }
111038 sqlite3DbFree(db, pTable->aCol);
111039 }
111040}
111041
111042/*
111043** Remove the memory data structures associated with the given
111044** Table. No changes are made to disk by this routine.
111045**
111046** This routine just deletes the data structure. It does not unlink
111047** the table data structure from the hash table. But it does destroy
111048** memory structures of the indices and foreign keys associated with
111049** the table.
111050**
111051** The db parameter is optional. It is needed if the Table object
111052** contains lookaside memory. (Table objects in the schema do not use
111053** lookaside memory, but some ephemeral Table objects do.) Or the
111054** db parameter can be used with db->pnBytesFreed to measure the memory
111055** used by the Table object.
111056*/
111057static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
111058 Index *pIndex, *pNext;
111059
111060#ifdef SQLITE_DEBUG
111061 /* Record the number of outstanding lookaside allocations in schema Tables
111062 ** prior to doing any free() operations. Since schema Tables do not use
111063 ** lookaside, this number should not change.
111064 **
111065 ** If malloc has already failed, it may be that it failed while allocating
111066 ** a Table object that was going to be marked ephemeral. So do not check
111067 ** that no lookaside memory is used in this case either. */
111068 int nLookaside = 0;
111069 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
111070 nLookaside = sqlite3LookasideUsed(db, 0);
111071 }
111072#endif
111073
111074 /* Delete all indices associated with this table. */
111075 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
111076 pNext = pIndex->pNext;
111077 assert( pIndex->pSchema==pTable->pSchema
111078 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
111079 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
111080 char *zName = pIndex->zName;
111081 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
111082 &pIndex->pSchema->idxHash, zName, 0
111083 );
111084 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
111085 assert( pOld==pIndex || pOld==0 );
111086 }
111087 sqlite3FreeIndex(db, pIndex);
111088 }
111089
111090 /* Delete any foreign keys attached to this table. */
111091 sqlite3FkDelete(db, pTable);
111092
111093 /* Delete the Table structure itself.
111094 */
111095 sqlite3DeleteColumnNames(db, pTable);
111096 sqlite3DbFree(db, pTable->zName);
111097 sqlite3DbFree(db, pTable->zColAff);
111098 sqlite3SelectDelete(db, pTable->pSelect);
111099 sqlite3ExprListDelete(db, pTable->pCheck);
111100#ifndef SQLITE_OMIT_VIRTUALTABLE
111101 sqlite3VtabClear(db, pTable);
111102#endif
111103 sqlite3DbFree(db, pTable);
111104
111105 /* Verify that no lookaside memory was used by schema tables */
111106 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
111107}
111108SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
111109 /* Do not delete the table until the reference count reaches zero. */
111110 if( !pTable ) return;
111111 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
111112 deleteTable(db, pTable);
111113}
111114
111115
111116/*
111117** Unlink the given table from the hash tables and the delete the
111118** table structure with all its indices and foreign keys.
111119*/
111120SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
111121 Table *p;
111122 Db *pDb;
111123
111124 assert( db!=0 );
111125 assert( iDb>=0 && iDb<db->nDb );
111126 assert( zTabName );
111127 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111128 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
111129 pDb = &db->aDb[iDb];
111130 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
111131 sqlite3DeleteTable(db, p);
111132 db->mDbFlags |= DBFLAG_SchemaChange;
111133}
111134
111135/*
111136** Given a token, return a string that consists of the text of that
111137** token. Space to hold the returned string
111138** is obtained from sqliteMalloc() and must be freed by the calling
111139** function.
111140**
111141** Any quotation marks (ex: "name", 'name', [name], or `name`) that
111142** surround the body of the token are removed.
111143**
111144** Tokens are often just pointers into the original SQL text and so
111145** are not \000 terminated and are not persistent. The returned string
111146** is \000 terminated and is persistent.
111147*/
111148SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
111149 char *zName;
111150 if( pName ){
111151 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
111152 sqlite3Dequote(zName);
111153 }else{
111154 zName = 0;
111155 }
111156 return zName;
111157}
111158
111159/*
111160** Open the sqlite_schema table stored in database number iDb for
111161** writing. The table is opened using cursor 0.
111162*/
111163SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *p, int iDb){
111164 Vdbe *v = sqlite3GetVdbe(p);
111165 sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, DFLT_SCHEMA_TABLE);
111166 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5);
111167 if( p->nTab==0 ){
111168 p->nTab = 1;
111169 }
111170}
111171
111172/*
111173** Parameter zName points to a nul-terminated buffer containing the name
111174** of a database ("main", "temp" or the name of an attached db). This
111175** function returns the index of the named database in db->aDb[], or
111176** -1 if the named db cannot be found.
111177*/
111178SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
111179 int i = -1; /* Database number */
111180 if( zName ){
111181 Db *pDb;
111182 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
111183 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
111184 /* "main" is always an acceptable alias for the primary database
111185 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
111186 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
111187 }
111188 }
111189 return i;
111190}
111191
111192/*
111193** The token *pName contains the name of a database (either "main" or
111194** "temp" or the name of an attached db). This routine returns the
111195** index of the named database in db->aDb[], or -1 if the named db
111196** does not exist.
111197*/
111198SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
111199 int i; /* Database number */
111200 char *zName; /* Name we are searching for */
111201 zName = sqlite3NameFromToken(db, pName);
111202 i = sqlite3FindDbName(db, zName);
111203 sqlite3DbFree(db, zName);
111204 return i;
111205}
111206
111207/* The table or view or trigger name is passed to this routine via tokens
111208** pName1 and pName2. If the table name was fully qualified, for example:
111209**
111210** CREATE TABLE xxx.yyy (...);
111211**
111212** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
111213** the table name is not fully qualified, i.e.:
111214**
111215** CREATE TABLE yyy(...);
111216**
111217** Then pName1 is set to "yyy" and pName2 is "".
111218**
111219** This routine sets the *ppUnqual pointer to point at the token (pName1 or
111220** pName2) that stores the unqualified table name. The index of the
111221** database "xxx" is returned.
111222*/
111223SQLITE_PRIVATE int sqlite3TwoPartName(
111224 Parse *pParse, /* Parsing and code generating context */
111225 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
111226 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
111227 Token **pUnqual /* Write the unqualified object name here */
111228){
111229 int iDb; /* Database holding the object */
111230 sqlite3 *db = pParse->db;
111231
111232 assert( pName2!=0 );
111233 if( pName2->n>0 ){
111234 if( db->init.busy ) {
111235 sqlite3ErrorMsg(pParse, "corrupt database");
111236 return -1;
111237 }
111238 *pUnqual = pName2;
111239 iDb = sqlite3FindDb(db, pName1);
111240 if( iDb<0 ){
111241 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
111242 return -1;
111243 }
111244 }else{
111245 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
111246 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
111247 iDb = db->init.iDb;
111248 *pUnqual = pName1;
111249 }
111250 return iDb;
111251}
111252
111253/*
111254** True if PRAGMA writable_schema is ON
111255*/
111256SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
111257 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
111258 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
111259 SQLITE_WriteSchema );
111260 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
111261 SQLITE_Defensive );
111262 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
111263 (SQLITE_WriteSchema|SQLITE_Defensive) );
111264 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
111265}
111266
111267/*
111268** This routine is used to check if the UTF-8 string zName is a legal
111269** unqualified name for a new schema object (table, index, view or
111270** trigger). All names are legal except those that begin with the string
111271** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
111272** is reserved for internal use.
111273**
111274** When parsing the sqlite_schema table, this routine also checks to
111275** make sure the "type", "name", and "tbl_name" columns are consistent
111276** with the SQL.
111277*/
111278SQLITE_PRIVATE int sqlite3CheckObjectName(
111279 Parse *pParse, /* Parsing context */
111280 const char *zName, /* Name of the object to check */
111281 const char *zType, /* Type of this object */
111282 const char *zTblName /* Parent table name for triggers and indexes */
111283){
111284 sqlite3 *db = pParse->db;
111285 if( sqlite3WritableSchema(db)
111286 || db->init.imposterTable
111287 || !sqlite3Config.bExtraSchemaChecks
111288 ){
111289 /* Skip these error checks for writable_schema=ON */
111290 return SQLITE_OK;
111291 }
111292 if( db->init.busy ){
111293 if( sqlite3_stricmp(zType, db->init.azInit[0])
111294 || sqlite3_stricmp(zName, db->init.azInit[1])
111295 || sqlite3_stricmp(zTblName, db->init.azInit[2])
111296 ){
111297 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
111298 return SQLITE_ERROR;
111299 }
111300 }else{
111301 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
111302 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
111303 ){
111304 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
111305 zName);
111306 return SQLITE_ERROR;
111307 }
111308
111309 }
111310 return SQLITE_OK;
111311}
111312
111313/*
111314** Return the PRIMARY KEY index of a table
111315*/
111316SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
111317 Index *p;
111318 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
111319 return p;
111320}
111321
111322/*
111323** Convert an table column number into a index column number. That is,
111324** for the column iCol in the table (as defined by the CREATE TABLE statement)
111325** find the (first) offset of that column in index pIdx. Or return -1
111326** if column iCol is not used in index pIdx.
111327*/
111328SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
111329 int i;
111330 for(i=0; i<pIdx->nColumn; i++){
111331 if( iCol==pIdx->aiColumn[i] ) return i;
111332 }
111333 return -1;
111334}
111335
111336#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111337/* Convert a storage column number into a table column number.
111338**
111339** The storage column number (0,1,2,....) is the index of the value
111340** as it appears in the record on disk. The true column number
111341** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
111342**
111343** The storage column number is less than the table column number if
111344** and only there are VIRTUAL columns to the left.
111345**
111346** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
111347*/
111348SQLITE_PRIVATE i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
111349 if( pTab->tabFlags & TF_HasVirtual ){
111350 int i;
111351 for(i=0; i<=iCol; i++){
111352 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
111353 }
111354 }
111355 return iCol;
111356}
111357#endif
111358
111359#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111360/* Convert a table column number into a storage column number.
111361**
111362** The storage column number (0,1,2,....) is the index of the value
111363** as it appears in the record on disk. Or, if the input column is
111364** the N-th virtual column (zero-based) then the storage number is
111365** the number of non-virtual columns in the table plus N.
111366**
111367** The true column number is the index (0,1,2,...) of the column in
111368** the CREATE TABLE statement.
111369**
111370** If the input column is a VIRTUAL column, then it should not appear
111371** in storage. But the value sometimes is cached in registers that
111372** follow the range of registers used to construct storage. This
111373** avoids computing the same VIRTUAL column multiple times, and provides
111374** values for use by OP_Param opcodes in triggers. Hence, if the
111375** input column is a VIRTUAL table, put it after all the other columns.
111376**
111377** In the following, N means "normal column", S means STORED, and
111378** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
111379**
111380** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
111381** -- 0 1 2 3 4 5 6 7 8
111382**
111383** Then the mapping from this function is as follows:
111384**
111385** INPUTS: 0 1 2 3 4 5 6 7 8
111386** OUTPUTS: 0 1 6 2 3 7 4 5 8
111387**
111388** So, in other words, this routine shifts all the virtual columns to
111389** the end.
111390**
111391** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
111392** this routine is a no-op macro. If the pTab does not have any virtual
111393** columns, then this routine is no-op that always return iCol. If iCol
111394** is negative (indicating the ROWID column) then this routine return iCol.
111395*/
111396SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
111397 int i;
111398 i16 n;
111399 assert( iCol<pTab->nCol );
111400 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
111401 for(i=0, n=0; i<iCol; i++){
111402 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
111403 }
111404 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
111405 /* iCol is a virtual column itself */
111406 return pTab->nNVCol + i - n;
111407 }else{
111408 /* iCol is a normal or stored column */
111409 return n;
111410 }
111411}
111412#endif
111413
111414/*
111415** Begin constructing a new table representation in memory. This is
111416** the first of several action routines that get called in response
111417** to a CREATE TABLE statement. In particular, this routine is called
111418** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
111419** flag is true if the table should be stored in the auxiliary database
111420** file instead of in the main database file. This is normally the case
111421** when the "TEMP" or "TEMPORARY" keyword occurs in between
111422** CREATE and TABLE.
111423**
111424** The new table record is initialized and put in pParse->pNewTable.
111425** As more of the CREATE TABLE statement is parsed, additional action
111426** routines will be called to add more information to this record.
111427** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
111428** is called to complete the construction of the new table record.
111429*/
111430SQLITE_PRIVATE void sqlite3StartTable(
111431 Parse *pParse, /* Parser context */
111432 Token *pName1, /* First part of the name of the table or view */
111433 Token *pName2, /* Second part of the name of the table or view */
111434 int isTemp, /* True if this is a TEMP table */
111435 int isView, /* True if this is a VIEW */
111436 int isVirtual, /* True if this is a VIRTUAL table */
111437 int noErr /* Do nothing if table already exists */
111438){
111439 Table *pTable;
111440 char *zName = 0; /* The name of the new table */
111441 sqlite3 *db = pParse->db;
111442 Vdbe *v;
111443 int iDb; /* Database number to create the table in */
111444 Token *pName; /* Unqualified name of the table to create */
111445
111446 if( db->init.busy && db->init.newTnum==1 ){
111447 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
111448 iDb = db->init.iDb;
111449 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
111450 pName = pName1;
111451 }else{
111452 /* The common case */
111453 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
111454 if( iDb<0 ) return;
111455 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
111456 /* If creating a temp table, the name may not be qualified. Unless
111457 ** the database name is "temp" anyway. */
111458 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
111459 return;
111460 }
111461 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
111462 zName = sqlite3NameFromToken(db, pName);
111463 if( IN_RENAME_OBJECT ){
111464 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
111465 }
111466 }
111467 pParse->sNameToken = *pName;
111468 if( zName==0 ) return;
111469 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
111470 goto begin_table_error;
111471 }
111472 if( db->init.iDb==1 ) isTemp = 1;
111473#ifndef SQLITE_OMIT_AUTHORIZATION
111474 assert( isTemp==0 || isTemp==1 );
111475 assert( isView==0 || isView==1 );
111476 {
111477 static const u8 aCode[] = {
111478 SQLITE_CREATE_TABLE,
111479 SQLITE_CREATE_TEMP_TABLE,
111480 SQLITE_CREATE_VIEW,
111481 SQLITE_CREATE_TEMP_VIEW
111482 };
111483 char *zDb = db->aDb[iDb].zDbSName;
111484 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
111485 goto begin_table_error;
111486 }
111487 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
111488 zName, 0, zDb) ){
111489 goto begin_table_error;
111490 }
111491 }
111492#endif
111493
111494 /* Make sure the new table name does not collide with an existing
111495 ** index or table name in the same database. Issue an error message if
111496 ** it does. The exception is if the statement being parsed was passed
111497 ** to an sqlite3_declare_vtab() call. In that case only the column names
111498 ** and types will be used, so there is no need to test for namespace
111499 ** collisions.
111500 */
111501 if( !IN_SPECIAL_PARSE ){
111502 char *zDb = db->aDb[iDb].zDbSName;
111503 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
111504 goto begin_table_error;
111505 }
111506 pTable = sqlite3FindTable(db, zName, zDb);
111507 if( pTable ){
111508 if( !noErr ){
111509 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
111510 }else{
111511 assert( !db->init.busy || CORRUPT_DB );
111512 sqlite3CodeVerifySchema(pParse, iDb);
111513 }
111514 goto begin_table_error;
111515 }
111516 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
111517 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
111518 goto begin_table_error;
111519 }
111520 }
111521
111522 pTable = sqlite3DbMallocZero(db, sizeof(Table));
111523 if( pTable==0 ){
111524 assert( db->mallocFailed );
111525 pParse->rc = SQLITE_NOMEM_BKPT;
111526 pParse->nErr++;
111527 goto begin_table_error;
111528 }
111529 pTable->zName = zName;
111530 pTable->iPKey = -1;
111531 pTable->pSchema = db->aDb[iDb].pSchema;
111532 pTable->nTabRef = 1;
111533#ifdef SQLITE_DEFAULT_ROWEST
111534 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
111535#else
111536 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111537#endif
111538 assert( pParse->pNewTable==0 );
111539 pParse->pNewTable = pTable;
111540
111541 /* If this is the magic sqlite_sequence table used by autoincrement,
111542 ** then record a pointer to this table in the main database structure
111543 ** so that INSERT can find the table easily.
111544 */
111545#ifndef SQLITE_OMIT_AUTOINCREMENT
111546 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
111547 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
111548 pTable->pSchema->pSeqTab = pTable;
111549 }
111550#endif
111551
111552 /* Begin generating the code that will insert the table record into
111553 ** the schema table. Note in particular that we must go ahead
111554 ** and allocate the record number for the table entry now. Before any
111555 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
111556 ** indices to be created and the table record must come before the
111557 ** indices. Hence, the record number for the table must be allocated
111558 ** now.
111559 */
111560 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
111561 int addr1;
111562 int fileFormat;
111563 int reg1, reg2, reg3;
111564 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
111565 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
111566 sqlite3BeginWriteOperation(pParse, 1, iDb);
111567
111568#ifndef SQLITE_OMIT_VIRTUALTABLE
111569 if( isVirtual ){
111570 sqlite3VdbeAddOp0(v, OP_VBegin);
111571 }
111572#endif
111573
111574 /* If the file format and encoding in the database have not been set,
111575 ** set them now.
111576 */
111577 reg1 = pParse->regRowid = ++pParse->nMem;
111578 reg2 = pParse->regRoot = ++pParse->nMem;
111579 reg3 = ++pParse->nMem;
111580 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
111581 sqlite3VdbeUsesBtree(v, iDb);
111582 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
111583 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
111584 1 : SQLITE_MAX_FILE_FORMAT;
111585 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
111586 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
111587 sqlite3VdbeJumpHere(v, addr1);
111588
111589 /* This just creates a place-holder record in the sqlite_schema table.
111590 ** The record created does not contain anything yet. It will be replaced
111591 ** by the real entry in code generated at sqlite3EndTable().
111592 **
111593 ** The rowid for the new entry is left in register pParse->regRowid.
111594 ** The root page number of the new table is left in reg pParse->regRoot.
111595 ** The rowid and root page number values are needed by the code that
111596 ** sqlite3EndTable will generate.
111597 */
111598#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
111599 if( isView || isVirtual ){
111600 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
111601 }else
111602#endif
111603 {
111604 pParse->addrCrTab =
111605 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
111606 }
111607 sqlite3OpenSchemaTable(pParse, iDb);
111608 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
111609 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
111610 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
111611 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
111612 sqlite3VdbeAddOp0(v, OP_Close);
111613 }
111614
111615 /* Normal (non-error) return. */
111616 return;
111617
111618 /* If an error occurs, we jump here */
111619begin_table_error:
111620 sqlite3DbFree(db, zName);
111621 return;
111622}
111623
111624/* Set properties of a table column based on the (magical)
111625** name of the column.
111626*/
111627#if SQLITE_ENABLE_HIDDEN_COLUMNS
111628SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
111629 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
111630 pCol->colFlags |= COLFLAG_HIDDEN;
111631 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
111632 pTab->tabFlags |= TF_OOOHidden;
111633 }
111634}
111635#endif
111636
111637
111638/*
111639** Add a new column to the table currently being constructed.
111640**
111641** The parser calls this routine once for each column declaration
111642** in a CREATE TABLE statement. sqlite3StartTable() gets called
111643** first to get things going. Then this routine is called for each
111644** column.
111645*/
111646SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
111647 Table *p;
111648 int i;
111649 char *z;
111650 char *zType;
111651 Column *pCol;
111652 sqlite3 *db = pParse->db;
111653 if( (p = pParse->pNewTable)==0 ) return;
111654 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
111655 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
111656 return;
111657 }
111658 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
111659 if( z==0 ) return;
111660 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
111661 memcpy(z, pName->z, pName->n);
111662 z[pName->n] = 0;
111663 sqlite3Dequote(z);
111664 for(i=0; i<p->nCol; i++){
111665 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
111666 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
111667 sqlite3DbFree(db, z);
111668 return;
111669 }
111670 }
111671 if( (p->nCol & 0x7)==0 ){
111672 Column *aNew;
111673 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
111674 if( aNew==0 ){
111675 sqlite3DbFree(db, z);
111676 return;
111677 }
111678 p->aCol = aNew;
111679 }
111680 pCol = &p->aCol[p->nCol];
111681 memset(pCol, 0, sizeof(p->aCol[0]));
111682 pCol->zName = z;
111683 pCol->hName = sqlite3StrIHash(z);
111684 sqlite3ColumnPropertiesFromName(p, pCol);
111685
111686 if( pType->n==0 ){
111687 /* If there is no type specified, columns have the default affinity
111688 ** 'BLOB' with a default size of 4 bytes. */
111689 pCol->affinity = SQLITE_AFF_BLOB;
111690 pCol->szEst = 1;
111691#ifdef SQLITE_ENABLE_SORTER_REFERENCES
111692 if( 4>=sqlite3GlobalConfig.szSorterRef ){
111693 pCol->colFlags |= COLFLAG_SORTERREF;
111694 }
111695#endif
111696 }else{
111697 zType = z + sqlite3Strlen30(z) + 1;
111698 memcpy(zType, pType->z, pType->n);
111699 zType[pType->n] = 0;
111700 sqlite3Dequote(zType);
111701 pCol->affinity = sqlite3AffinityType(zType, pCol);
111702 pCol->colFlags |= COLFLAG_HASTYPE;
111703 }
111704 p->nCol++;
111705 p->nNVCol++;
111706 pParse->constraintName.n = 0;
111707}
111708
111709/*
111710** This routine is called by the parser while in the middle of
111711** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
111712** been seen on a column. This routine sets the notNull flag on
111713** the column currently under construction.
111714*/
111715SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
111716 Table *p;
111717 Column *pCol;
111718 p = pParse->pNewTable;
111719 if( p==0 || NEVER(p->nCol<1) ) return;
111720 pCol = &p->aCol[p->nCol-1];
111721 pCol->notNull = (u8)onError;
111722 p->tabFlags |= TF_HasNotNull;
111723
111724 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
111725 ** on this column. */
111726 if( pCol->colFlags & COLFLAG_UNIQUE ){
111727 Index *pIdx;
111728 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
111729 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
111730 if( pIdx->aiColumn[0]==p->nCol-1 ){
111731 pIdx->uniqNotNull = 1;
111732 }
111733 }
111734 }
111735}
111736
111737/*
111738** Scan the column type name zType (length nType) and return the
111739** associated affinity type.
111740**
111741** This routine does a case-independent search of zType for the
111742** substrings in the following table. If one of the substrings is
111743** found, the corresponding affinity is returned. If zType contains
111744** more than one of the substrings, entries toward the top of
111745** the table take priority. For example, if zType is 'BLOBINT',
111746** SQLITE_AFF_INTEGER is returned.
111747**
111748** Substring | Affinity
111749** --------------------------------
111750** 'INT' | SQLITE_AFF_INTEGER
111751** 'CHAR' | SQLITE_AFF_TEXT
111752** 'CLOB' | SQLITE_AFF_TEXT
111753** 'TEXT' | SQLITE_AFF_TEXT
111754** 'BLOB' | SQLITE_AFF_BLOB
111755** 'REAL' | SQLITE_AFF_REAL
111756** 'FLOA' | SQLITE_AFF_REAL
111757** 'DOUB' | SQLITE_AFF_REAL
111758**
111759** If none of the substrings in the above table are found,
111760** SQLITE_AFF_NUMERIC is returned.
111761*/
111762SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
111763 u32 h = 0;
111764 char aff = SQLITE_AFF_NUMERIC;
111765 const char *zChar = 0;
111766
111767 assert( zIn!=0 );
111768 while( zIn[0] ){
111769 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
111770 zIn++;
111771 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
111772 aff = SQLITE_AFF_TEXT;
111773 zChar = zIn;
111774 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
111775 aff = SQLITE_AFF_TEXT;
111776 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
111777 aff = SQLITE_AFF_TEXT;
111778 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
111779 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
111780 aff = SQLITE_AFF_BLOB;
111781 if( zIn[0]=='(' ) zChar = zIn;
111782#ifndef SQLITE_OMIT_FLOATING_POINT
111783 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
111784 && aff==SQLITE_AFF_NUMERIC ){
111785 aff = SQLITE_AFF_REAL;
111786 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
111787 && aff==SQLITE_AFF_NUMERIC ){
111788 aff = SQLITE_AFF_REAL;
111789 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
111790 && aff==SQLITE_AFF_NUMERIC ){
111791 aff = SQLITE_AFF_REAL;
111792#endif
111793 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
111794 aff = SQLITE_AFF_INTEGER;
111795 break;
111796 }
111797 }
111798
111799 /* If pCol is not NULL, store an estimate of the field size. The
111800 ** estimate is scaled so that the size of an integer is 1. */
111801 if( pCol ){
111802 int v = 0; /* default size is approx 4 bytes */
111803 if( aff<SQLITE_AFF_NUMERIC ){
111804 if( zChar ){
111805 while( zChar[0] ){
111806 if( sqlite3Isdigit(zChar[0]) ){
111807 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
111808 sqlite3GetInt32(zChar, &v);
111809 break;
111810 }
111811 zChar++;
111812 }
111813 }else{
111814 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
111815 }
111816 }
111817#ifdef SQLITE_ENABLE_SORTER_REFERENCES
111818 if( v>=sqlite3GlobalConfig.szSorterRef ){
111819 pCol->colFlags |= COLFLAG_SORTERREF;
111820 }
111821#endif
111822 v = v/4 + 1;
111823 if( v>255 ) v = 255;
111824 pCol->szEst = v;
111825 }
111826 return aff;
111827}
111828
111829/*
111830** The expression is the default value for the most recently added column
111831** of the table currently under construction.
111832**
111833** Default value expressions must be constant. Raise an exception if this
111834** is not the case.
111835**
111836** This routine is called by the parser while in the middle of
111837** parsing a CREATE TABLE statement.
111838*/
111839SQLITE_PRIVATE void sqlite3AddDefaultValue(
111840 Parse *pParse, /* Parsing context */
111841 Expr *pExpr, /* The parsed expression of the default value */
111842 const char *zStart, /* Start of the default value text */
111843 const char *zEnd /* First character past end of defaut value text */
111844){
111845 Table *p;
111846 Column *pCol;
111847 sqlite3 *db = pParse->db;
111848 p = pParse->pNewTable;
111849 if( p!=0 ){
111850 int isInit = db->init.busy && db->init.iDb!=1;
111851 pCol = &(p->aCol[p->nCol-1]);
111852 if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
111853 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
111854 pCol->zName);
111855#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111856 }else if( pCol->colFlags & COLFLAG_GENERATED ){
111857 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
111858 testcase( pCol->colFlags & COLFLAG_STORED );
111859 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
111860#endif
111861 }else{
111862 /* A copy of pExpr is used instead of the original, as pExpr contains
111863 ** tokens that point to volatile memory.
111864 */
111865 Expr x;
111866 sqlite3ExprDelete(db, pCol->pDflt);
111867 memset(&x, 0, sizeof(x));
111868 x.op = TK_SPAN;
111869 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
111870 x.pLeft = pExpr;
111871 x.flags = EP_Skip;
111872 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
111873 sqlite3DbFree(db, x.u.zToken);
111874 }
111875 }
111876 if( IN_RENAME_OBJECT ){
111877 sqlite3RenameExprUnmap(pParse, pExpr);
111878 }
111879 sqlite3ExprDelete(db, pExpr);
111880}
111881
111882/*
111883** Backwards Compatibility Hack:
111884**
111885** Historical versions of SQLite accepted strings as column names in
111886** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
111887**
111888** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
111889** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
111890**
111891** This is goofy. But to preserve backwards compatibility we continue to
111892** accept it. This routine does the necessary conversion. It converts
111893** the expression given in its argument from a TK_STRING into a TK_ID
111894** if the expression is just a TK_STRING with an optional COLLATE clause.
111895** If the expression is anything other than TK_STRING, the expression is
111896** unchanged.
111897*/
111898static void sqlite3StringToId(Expr *p){
111899 if( p->op==TK_STRING ){
111900 p->op = TK_ID;
111901 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
111902 p->pLeft->op = TK_ID;
111903 }
111904}
111905
111906/*
111907** Tag the given column as being part of the PRIMARY KEY
111908*/
111909static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
111910 pCol->colFlags |= COLFLAG_PRIMKEY;
111911#ifndef SQLITE_OMIT_GENERATED_COLUMNS
111912 if( pCol->colFlags & COLFLAG_GENERATED ){
111913 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
111914 testcase( pCol->colFlags & COLFLAG_STORED );
111915 sqlite3ErrorMsg(pParse,
111916 "generated columns cannot be part of the PRIMARY KEY");
111917 }
111918#endif
111919}
111920
111921/*
111922** Designate the PRIMARY KEY for the table. pList is a list of names
111923** of columns that form the primary key. If pList is NULL, then the
111924** most recently added column of the table is the primary key.
111925**
111926** A table can have at most one primary key. If the table already has
111927** a primary key (and this is the second primary key) then create an
111928** error.
111929**
111930** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
111931** then we will try to use that column as the rowid. Set the Table.iPKey
111932** field of the table under construction to be the index of the
111933** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
111934** no INTEGER PRIMARY KEY.
111935**
111936** If the key is not an INTEGER PRIMARY KEY, then create a unique
111937** index for the key. No index is created for INTEGER PRIMARY KEYs.
111938*/
111939SQLITE_PRIVATE void sqlite3AddPrimaryKey(
111940 Parse *pParse, /* Parsing context */
111941 ExprList *pList, /* List of field names to be indexed */
111942 int onError, /* What to do with a uniqueness conflict */
111943 int autoInc, /* True if the AUTOINCREMENT keyword is present */
111944 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
111945){
111946 Table *pTab = pParse->pNewTable;
111947 Column *pCol = 0;
111948 int iCol = -1, i;
111949 int nTerm;
111950 if( pTab==0 ) goto primary_key_exit;
111951 if( pTab->tabFlags & TF_HasPrimaryKey ){
111952 sqlite3ErrorMsg(pParse,
111953 "table \"%s\" has more than one primary key", pTab->zName);
111954 goto primary_key_exit;
111955 }
111956 pTab->tabFlags |= TF_HasPrimaryKey;
111957 if( pList==0 ){
111958 iCol = pTab->nCol - 1;
111959 pCol = &pTab->aCol[iCol];
111960 makeColumnPartOfPrimaryKey(pParse, pCol);
111961 nTerm = 1;
111962 }else{
111963 nTerm = pList->nExpr;
111964 for(i=0; i<nTerm; i++){
111965 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
111966 assert( pCExpr!=0 );
111967 sqlite3StringToId(pCExpr);
111968 if( pCExpr->op==TK_ID ){
111969 const char *zCName = pCExpr->u.zToken;
111970 for(iCol=0; iCol<pTab->nCol; iCol++){
111971 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
111972 pCol = &pTab->aCol[iCol];
111973 makeColumnPartOfPrimaryKey(pParse, pCol);
111974 break;
111975 }
111976 }
111977 }
111978 }
111979 }
111980 if( nTerm==1
111981 && pCol
111982 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
111983 && sortOrder!=SQLITE_SO_DESC
111984 ){
111985 if( IN_RENAME_OBJECT && pList ){
111986 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
111987 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
111988 }
111989 pTab->iPKey = iCol;
111990 pTab->keyConf = (u8)onError;
111991 assert( autoInc==0 || autoInc==1 );
111992 pTab->tabFlags |= autoInc*TF_Autoincrement;
111993 if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
111994 (void)sqlite3HasExplicitNulls(pParse, pList);
111995 }else if( autoInc ){
111996#ifndef SQLITE_OMIT_AUTOINCREMENT
111997 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
111998 "INTEGER PRIMARY KEY");
111999#endif
112000 }else{
112001 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
112002 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
112003 pList = 0;
112004 }
112005
112006primary_key_exit:
112007 sqlite3ExprListDelete(pParse->db, pList);
112008 return;
112009}
112010
112011/*
112012** Add a new CHECK constraint to the table currently under construction.
112013*/
112014SQLITE_PRIVATE void sqlite3AddCheckConstraint(
112015 Parse *pParse, /* Parsing context */
112016 Expr *pCheckExpr /* The check expression */
112017){
112018#ifndef SQLITE_OMIT_CHECK
112019 Table *pTab = pParse->pNewTable;
112020 sqlite3 *db = pParse->db;
112021 if( pTab && !IN_DECLARE_VTAB
112022 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
112023 ){
112024 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
112025 if( pParse->constraintName.n ){
112026 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
112027 }
112028 }else
112029#endif
112030 {
112031 sqlite3ExprDelete(pParse->db, pCheckExpr);
112032 }
112033}
112034
112035/*
112036** Set the collation function of the most recently parsed table column
112037** to the CollSeq given.
112038*/
112039SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
112040 Table *p;
112041 int i;
112042 char *zColl; /* Dequoted name of collation sequence */
112043 sqlite3 *db;
112044
112045 if( (p = pParse->pNewTable)==0 ) return;
112046 i = p->nCol-1;
112047 db = pParse->db;
112048 zColl = sqlite3NameFromToken(db, pToken);
112049 if( !zColl ) return;
112050
112051 if( sqlite3LocateCollSeq(pParse, zColl) ){
112052 Index *pIdx;
112053 sqlite3DbFree(db, p->aCol[i].zColl);
112054 p->aCol[i].zColl = zColl;
112055
112056 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
112057 ** then an index may have been created on this column before the
112058 ** collation type was added. Correct this if it is the case.
112059 */
112060 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
112061 assert( pIdx->nKeyCol==1 );
112062 if( pIdx->aiColumn[0]==i ){
112063 pIdx->azColl[0] = p->aCol[i].zColl;
112064 }
112065 }
112066 }else{
112067 sqlite3DbFree(db, zColl);
112068 }
112069}
112070
112071/* Change the most recently parsed column to be a GENERATED ALWAYS AS
112072** column.
112073*/
112074SQLITE_PRIVATE void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
112075#ifndef SQLITE_OMIT_GENERATED_COLUMNS
112076 u8 eType = COLFLAG_VIRTUAL;
112077 Table *pTab = pParse->pNewTable;
112078 Column *pCol;
112079 if( pTab==0 ){
112080 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
112081 goto generated_done;
112082 }
112083 pCol = &(pTab->aCol[pTab->nCol-1]);
112084 if( IN_DECLARE_VTAB ){
112085 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
112086 goto generated_done;
112087 }
112088 if( pCol->pDflt ) goto generated_error;
112089 if( pType ){
112090 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
112091 /* no-op */
112092 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
112093 eType = COLFLAG_STORED;
112094 }else{
112095 goto generated_error;
112096 }
112097 }
112098 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
112099 pCol->colFlags |= eType;
112100 assert( TF_HasVirtual==COLFLAG_VIRTUAL );
112101 assert( TF_HasStored==COLFLAG_STORED );
112102 pTab->tabFlags |= eType;
112103 if( pCol->colFlags & COLFLAG_PRIMKEY ){
112104 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
112105 }
112106 pCol->pDflt = pExpr;
112107 pExpr = 0;
112108 goto generated_done;
112109
112110generated_error:
112111 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
112112 pCol->zName);
112113generated_done:
112114 sqlite3ExprDelete(pParse->db, pExpr);
112115#else
112116 /* Throw and error for the GENERATED ALWAYS AS clause if the
112117 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
112118 sqlite3ErrorMsg(pParse, "generated columns not supported");
112119 sqlite3ExprDelete(pParse->db, pExpr);
112120#endif
112121}
112122
112123/*
112124** Generate code that will increment the schema cookie.
112125**
112126** The schema cookie is used to determine when the schema for the
112127** database changes. After each schema change, the cookie value
112128** changes. When a process first reads the schema it records the
112129** cookie. Thereafter, whenever it goes to access the database,
112130** it checks the cookie to make sure the schema has not changed
112131** since it was last read.
112132**
112133** This plan is not completely bullet-proof. It is possible for
112134** the schema to change multiple times and for the cookie to be
112135** set back to prior value. But schema changes are infrequent
112136** and the probability of hitting the same cookie value is only
112137** 1 chance in 2^32. So we're safe enough.
112138**
112139** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
112140** the schema-version whenever the schema changes.
112141*/
112142SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
112143 sqlite3 *db = pParse->db;
112144 Vdbe *v = pParse->pVdbe;
112145 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112146 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
112147 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
112148}
112149
112150/*
112151** Measure the number of characters needed to output the given
112152** identifier. The number returned includes any quotes used
112153** but does not include the null terminator.
112154**
112155** The estimate is conservative. It might be larger that what is
112156** really needed.
112157*/
112158static int identLength(const char *z){
112159 int n;
112160 for(n=0; *z; n++, z++){
112161 if( *z=='"' ){ n++; }
112162 }
112163 return n + 2;
112164}
112165
112166/*
112167** The first parameter is a pointer to an output buffer. The second
112168** parameter is a pointer to an integer that contains the offset at
112169** which to write into the output buffer. This function copies the
112170** nul-terminated string pointed to by the third parameter, zSignedIdent,
112171** to the specified offset in the buffer and updates *pIdx to refer
112172** to the first byte after the last byte written before returning.
112173**
112174** If the string zSignedIdent consists entirely of alpha-numeric
112175** characters, does not begin with a digit and is not an SQL keyword,
112176** then it is copied to the output buffer exactly as it is. Otherwise,
112177** it is quoted using double-quotes.
112178*/
112179static void identPut(char *z, int *pIdx, char *zSignedIdent){
112180 unsigned char *zIdent = (unsigned char*)zSignedIdent;
112181 int i, j, needQuote;
112182 i = *pIdx;
112183
112184 for(j=0; zIdent[j]; j++){
112185 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
112186 }
112187 needQuote = sqlite3Isdigit(zIdent[0])
112188 || sqlite3KeywordCode(zIdent, j)!=TK_ID
112189 || zIdent[j]!=0
112190 || j==0;
112191
112192 if( needQuote ) z[i++] = '"';
112193 for(j=0; zIdent[j]; j++){
112194 z[i++] = zIdent[j];
112195 if( zIdent[j]=='"' ) z[i++] = '"';
112196 }
112197 if( needQuote ) z[i++] = '"';
112198 z[i] = 0;
112199 *pIdx = i;
112200}
112201
112202/*
112203** Generate a CREATE TABLE statement appropriate for the given
112204** table. Memory to hold the text of the statement is obtained
112205** from sqliteMalloc() and must be freed by the calling function.
112206*/
112207static char *createTableStmt(sqlite3 *db, Table *p){
112208 int i, k, n;
112209 char *zStmt;
112210 char *zSep, *zSep2, *zEnd;
112211 Column *pCol;
112212 n = 0;
112213 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
112214 n += identLength(pCol->zName) + 5;
112215 }
112216 n += identLength(p->zName);
112217 if( n<50 ){
112218 zSep = "";
112219 zSep2 = ",";
112220 zEnd = ")";
112221 }else{
112222 zSep = "\n ";
112223 zSep2 = ",\n ";
112224 zEnd = "\n)";
112225 }
112226 n += 35 + 6*p->nCol;
112227 zStmt = sqlite3DbMallocRaw(0, n);
112228 if( zStmt==0 ){
112229 sqlite3OomFault(db);
112230 return 0;
112231 }
112232 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
112233 k = sqlite3Strlen30(zStmt);
112234 identPut(zStmt, &k, p->zName);
112235 zStmt[k++] = '(';
112236 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
112237 static const char * const azType[] = {
112238 /* SQLITE_AFF_BLOB */ "",
112239 /* SQLITE_AFF_TEXT */ " TEXT",
112240 /* SQLITE_AFF_NUMERIC */ " NUM",
112241 /* SQLITE_AFF_INTEGER */ " INT",
112242 /* SQLITE_AFF_REAL */ " REAL"
112243 };
112244 int len;
112245 const char *zType;
112246
112247 sqlite3_snprintf(n-k, &zStmt[k], zSep);
112248 k += sqlite3Strlen30(&zStmt[k]);
112249 zSep = zSep2;
112250 identPut(zStmt, &k, pCol->zName);
112251 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
112252 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
112253 testcase( pCol->affinity==SQLITE_AFF_BLOB );
112254 testcase( pCol->affinity==SQLITE_AFF_TEXT );
112255 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
112256 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
112257 testcase( pCol->affinity==SQLITE_AFF_REAL );
112258
112259 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
112260 len = sqlite3Strlen30(zType);
112261 assert( pCol->affinity==SQLITE_AFF_BLOB
112262 || pCol->affinity==sqlite3AffinityType(zType, 0) );
112263 memcpy(&zStmt[k], zType, len);
112264 k += len;
112265 assert( k<=n );
112266 }
112267 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
112268 return zStmt;
112269}
112270
112271/*
112272** Resize an Index object to hold N columns total. Return SQLITE_OK
112273** on success and SQLITE_NOMEM on an OOM error.
112274*/
112275static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
112276 char *zExtra;
112277 int nByte;
112278 if( pIdx->nColumn>=N ) return SQLITE_OK;
112279 assert( pIdx->isResized==0 );
112280 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
112281 zExtra = sqlite3DbMallocZero(db, nByte);
112282 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
112283 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
112284 pIdx->azColl = (const char**)zExtra;
112285 zExtra += sizeof(char*)*N;
112286 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
112287 pIdx->aiColumn = (i16*)zExtra;
112288 zExtra += sizeof(i16)*N;
112289 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
112290 pIdx->aSortOrder = (u8*)zExtra;
112291 pIdx->nColumn = N;
112292 pIdx->isResized = 1;
112293 return SQLITE_OK;
112294}
112295
112296/*
112297** Estimate the total row width for a table.
112298*/
112299static void estimateTableWidth(Table *pTab){
112300 unsigned wTable = 0;
112301 const Column *pTabCol;
112302 int i;
112303 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
112304 wTable += pTabCol->szEst;
112305 }
112306 if( pTab->iPKey<0 ) wTable++;
112307 pTab->szTabRow = sqlite3LogEst(wTable*4);
112308}
112309
112310/*
112311** Estimate the average size of a row for an index.
112312*/
112313static void estimateIndexWidth(Index *pIdx){
112314 unsigned wIndex = 0;
112315 int i;
112316 const Column *aCol = pIdx->pTable->aCol;
112317 for(i=0; i<pIdx->nColumn; i++){
112318 i16 x = pIdx->aiColumn[i];
112319 assert( x<pIdx->pTable->nCol );
112320 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
112321 }
112322 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
112323}
112324
112325/* Return true if column number x is any of the first nCol entries of aiCol[].
112326** This is used to determine if the column number x appears in any of the
112327** first nCol entries of an index.
112328*/
112329static int hasColumn(const i16 *aiCol, int nCol, int x){
112330 while( nCol-- > 0 ){
112331 assert( aiCol[0]>=0 );
112332 if( x==*(aiCol++) ){
112333 return 1;
112334 }
112335 }
112336 return 0;
112337}
112338
112339/*
112340** Return true if any of the first nKey entries of index pIdx exactly
112341** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
112342** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
112343** or may not be the same index as pPk.
112344**
112345** The first nKey entries of pIdx are guaranteed to be ordinary columns,
112346** not a rowid or expression.
112347**
112348** This routine differs from hasColumn() in that both the column and the
112349** collating sequence must match for this routine, but for hasColumn() only
112350** the column name must match.
112351*/
112352static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
112353 int i, j;
112354 assert( nKey<=pIdx->nColumn );
112355 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
112356 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
112357 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
112358 assert( pPk->pTable==pIdx->pTable );
112359 testcase( pPk==pIdx );
112360 j = pPk->aiColumn[iCol];
112361 assert( j!=XN_ROWID && j!=XN_EXPR );
112362 for(i=0; i<nKey; i++){
112363 assert( pIdx->aiColumn[i]>=0 || j>=0 );
112364 if( pIdx->aiColumn[i]==j
112365 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
112366 ){
112367 return 1;
112368 }
112369 }
112370 return 0;
112371}
112372
112373/* Recompute the colNotIdxed field of the Index.
112374**
112375** colNotIdxed is a bitmask that has a 0 bit representing each indexed
112376** columns that are within the first 63 columns of the table. The
112377** high-order bit of colNotIdxed is always 1. All unindexed columns
112378** of the table have a 1.
112379**
112380** 2019-10-24: For the purpose of this computation, virtual columns are
112381** not considered to be covered by the index, even if they are in the
112382** index, because we do not trust the logic in whereIndexExprTrans() to be
112383** able to find all instances of a reference to the indexed table column
112384** and convert them into references to the index. Hence we always want
112385** the actual table at hand in order to recompute the virtual column, if
112386** necessary.
112387**
112388** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
112389** to determine if the index is covering index.
112390*/
112391static void recomputeColumnsNotIndexed(Index *pIdx){
112392 Bitmask m = 0;
112393 int j;
112394 Table *pTab = pIdx->pTable;
112395 for(j=pIdx->nColumn-1; j>=0; j--){
112396 int x = pIdx->aiColumn[j];
112397 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
112398 testcase( x==BMS-1 );
112399 testcase( x==BMS-2 );
112400 if( x<BMS-1 ) m |= MASKBIT(x);
112401 }
112402 }
112403 pIdx->colNotIdxed = ~m;
112404 assert( (pIdx->colNotIdxed>>63)==1 );
112405}
112406
112407/*
112408** This routine runs at the end of parsing a CREATE TABLE statement that
112409** has a WITHOUT ROWID clause. The job of this routine is to convert both
112410** internal schema data structures and the generated VDBE code so that they
112411** are appropriate for a WITHOUT ROWID table instead of a rowid table.
112412** Changes include:
112413**
112414** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
112415** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
112416** into BTREE_BLOBKEY.
112417** (3) Bypass the creation of the sqlite_schema table entry
112418** for the PRIMARY KEY as the primary key index is now
112419** identified by the sqlite_schema table entry of the table itself.
112420** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
112421** schema to the rootpage from the main table.
112422** (5) Add all table columns to the PRIMARY KEY Index object
112423** so that the PRIMARY KEY is a covering index. The surplus
112424** columns are part of KeyInfo.nAllField and are not used for
112425** sorting or lookup or uniqueness checks.
112426** (6) Replace the rowid tail on all automatically generated UNIQUE
112427** indices with the PRIMARY KEY columns.
112428**
112429** For virtual tables, only (1) is performed.
112430*/
112431static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
112432 Index *pIdx;
112433 Index *pPk;
112434 int nPk;
112435 int nExtra;
112436 int i, j;
112437 sqlite3 *db = pParse->db;
112438 Vdbe *v = pParse->pVdbe;
112439
112440 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
112441 */
112442 if( !db->init.imposterTable ){
112443 for(i=0; i<pTab->nCol; i++){
112444 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
112445 pTab->aCol[i].notNull = OE_Abort;
112446 }
112447 }
112448 pTab->tabFlags |= TF_HasNotNull;
112449 }
112450
112451 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
112452 ** into BTREE_BLOBKEY.
112453 */
112454 if( pParse->addrCrTab ){
112455 assert( v );
112456 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
112457 }
112458
112459 /* Locate the PRIMARY KEY index. Or, if this table was originally
112460 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
112461 */
112462 if( pTab->iPKey>=0 ){
112463 ExprList *pList;
112464 Token ipkToken;
112465 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
112466 pList = sqlite3ExprListAppend(pParse, 0,
112467 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
112468 if( pList==0 ) return;
112469 if( IN_RENAME_OBJECT ){
112470 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
112471 }
112472 pList->a[0].sortFlags = pParse->iPkSortOrder;
112473 assert( pParse->pNewTable==pTab );
112474 pTab->iPKey = -1;
112475 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
112476 SQLITE_IDXTYPE_PRIMARYKEY);
112477 if( db->mallocFailed || pParse->nErr ) return;
112478 pPk = sqlite3PrimaryKeyIndex(pTab);
112479 assert( pPk->nKeyCol==1 );
112480 }else{
112481 pPk = sqlite3PrimaryKeyIndex(pTab);
112482 assert( pPk!=0 );
112483
112484 /*
112485 ** Remove all redundant columns from the PRIMARY KEY. For example, change
112486 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
112487 ** code assumes the PRIMARY KEY contains no repeated columns.
112488 */
112489 for(i=j=1; i<pPk->nKeyCol; i++){
112490 if( isDupColumn(pPk, j, pPk, i) ){
112491 pPk->nColumn--;
112492 }else{
112493 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
112494 pPk->azColl[j] = pPk->azColl[i];
112495 pPk->aSortOrder[j] = pPk->aSortOrder[i];
112496 pPk->aiColumn[j++] = pPk->aiColumn[i];
112497 }
112498 }
112499 pPk->nKeyCol = j;
112500 }
112501 assert( pPk!=0 );
112502 pPk->isCovering = 1;
112503 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
112504 nPk = pPk->nColumn = pPk->nKeyCol;
112505
112506 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
112507 ** table entry. This is only required if currently generating VDBE
112508 ** code for a CREATE TABLE (not when parsing one as part of reading
112509 ** a database schema). */
112510 if( v && pPk->tnum>0 ){
112511 assert( db->init.busy==0 );
112512 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
112513 }
112514
112515 /* The root page of the PRIMARY KEY is the table root page */
112516 pPk->tnum = pTab->tnum;
112517
112518 /* Update the in-memory representation of all UNIQUE indices by converting
112519 ** the final rowid column into one or more columns of the PRIMARY KEY.
112520 */
112521 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112522 int n;
112523 if( IsPrimaryKeyIndex(pIdx) ) continue;
112524 for(i=n=0; i<nPk; i++){
112525 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
112526 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
112527 n++;
112528 }
112529 }
112530 if( n==0 ){
112531 /* This index is a superset of the primary key */
112532 pIdx->nColumn = pIdx->nKeyCol;
112533 continue;
112534 }
112535 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
112536 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
112537 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
112538 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
112539 pIdx->aiColumn[j] = pPk->aiColumn[i];
112540 pIdx->azColl[j] = pPk->azColl[i];
112541 if( pPk->aSortOrder[i] ){
112542 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
112543 pIdx->bAscKeyBug = 1;
112544 }
112545 j++;
112546 }
112547 }
112548 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
112549 assert( pIdx->nColumn>=j );
112550 }
112551
112552 /* Add all table columns to the PRIMARY KEY index
112553 */
112554 nExtra = 0;
112555 for(i=0; i<pTab->nCol; i++){
112556 if( !hasColumn(pPk->aiColumn, nPk, i)
112557 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
112558 }
112559 if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
112560 for(i=0, j=nPk; i<pTab->nCol; i++){
112561 if( !hasColumn(pPk->aiColumn, j, i)
112562 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
112563 ){
112564 assert( j<pPk->nColumn );
112565 pPk->aiColumn[j] = i;
112566 pPk->azColl[j] = sqlite3StrBINARY;
112567 j++;
112568 }
112569 }
112570 assert( pPk->nColumn==j );
112571 assert( pTab->nNVCol<=j );
112572 recomputeColumnsNotIndexed(pPk);
112573}
112574
112575
112576#ifndef SQLITE_OMIT_VIRTUALTABLE
112577/*
112578** Return true if pTab is a virtual table and zName is a shadow table name
112579** for that virtual table.
112580*/
112581SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
112582 int nName; /* Length of zName */
112583 Module *pMod; /* Module for the virtual table */
112584
112585 if( !IsVirtual(pTab) ) return 0;
112586 nName = sqlite3Strlen30(pTab->zName);
112587 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
112588 if( zName[nName]!='_' ) return 0;
112589 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
112590 if( pMod==0 ) return 0;
112591 if( pMod->pModule->iVersion<3 ) return 0;
112592 if( pMod->pModule->xShadowName==0 ) return 0;
112593 return pMod->pModule->xShadowName(zName+nName+1);
112594}
112595#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
112596
112597#ifndef SQLITE_OMIT_VIRTUALTABLE
112598/*
112599** Return true if zName is a shadow table name in the current database
112600** connection.
112601**
112602** zName is temporarily modified while this routine is running, but is
112603** restored to its original value prior to this routine returning.
112604*/
112605SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
112606 char *zTail; /* Pointer to the last "_" in zName */
112607 Table *pTab; /* Table that zName is a shadow of */
112608 zTail = strrchr(zName, '_');
112609 if( zTail==0 ) return 0;
112610 *zTail = 0;
112611 pTab = sqlite3FindTable(db, zName, 0);
112612 *zTail = '_';
112613 if( pTab==0 ) return 0;
112614 if( !IsVirtual(pTab) ) return 0;
112615 return sqlite3IsShadowTableOf(db, pTab, zName);
112616}
112617#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
112618
112619
112620#ifdef SQLITE_DEBUG
112621/*
112622** Mark all nodes of an expression as EP_Immutable, indicating that
112623** they should not be changed. Expressions attached to a table or
112624** index definition are tagged this way to help ensure that we do
112625** not pass them into code generator routines by mistake.
112626*/
112627static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
112628 ExprSetVVAProperty(pExpr, EP_Immutable);
112629 return WRC_Continue;
112630}
112631static void markExprListImmutable(ExprList *pList){
112632 if( pList ){
112633 Walker w;
112634 memset(&w, 0, sizeof(w));
112635 w.xExprCallback = markImmutableExprStep;
112636 w.xSelectCallback = sqlite3SelectWalkNoop;
112637 w.xSelectCallback2 = 0;
112638 sqlite3WalkExprList(&w, pList);
112639 }
112640}
112641#else
112642#define markExprListImmutable(X) /* no-op */
112643#endif /* SQLITE_DEBUG */
112644
112645
112646/*
112647** This routine is called to report the final ")" that terminates
112648** a CREATE TABLE statement.
112649**
112650** The table structure that other action routines have been building
112651** is added to the internal hash tables, assuming no errors have
112652** occurred.
112653**
112654** An entry for the table is made in the schema table on disk, unless
112655** this is a temporary table or db->init.busy==1. When db->init.busy==1
112656** it means we are reading the sqlite_schema table because we just
112657** connected to the database or because the sqlite_schema table has
112658** recently changed, so the entry for this table already exists in
112659** the sqlite_schema table. We do not want to create it again.
112660**
112661** If the pSelect argument is not NULL, it means that this routine
112662** was called to create a table generated from a
112663** "CREATE TABLE ... AS SELECT ..." statement. The column names of
112664** the new table will match the result set of the SELECT.
112665*/
112666SQLITE_PRIVATE void sqlite3EndTable(
112667 Parse *pParse, /* Parse context */
112668 Token *pCons, /* The ',' token after the last column defn. */
112669 Token *pEnd, /* The ')' before options in the CREATE TABLE */
112670 u8 tabOpts, /* Extra table options. Usually 0. */
112671 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
112672){
112673 Table *p; /* The new table */
112674 sqlite3 *db = pParse->db; /* The database connection */
112675 int iDb; /* Database in which the table lives */
112676 Index *pIdx; /* An implied index of the table */
112677
112678 if( pEnd==0 && pSelect==0 ){
112679 return;
112680 }
112681 assert( !db->mallocFailed );
112682 p = pParse->pNewTable;
112683 if( p==0 ) return;
112684
112685 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
112686 p->tabFlags |= TF_Shadow;
112687 }
112688
112689 /* If the db->init.busy is 1 it means we are reading the SQL off the
112690 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
112691 ** So do not write to the disk again. Extract the root page number
112692 ** for the table from the db->init.newTnum field. (The page number
112693 ** should have been put there by the sqliteOpenCb routine.)
112694 **
112695 ** If the root page number is 1, that means this is the sqlite_schema
112696 ** table itself. So mark it read-only.
112697 */
112698 if( db->init.busy ){
112699 if( pSelect ){
112700 sqlite3ErrorMsg(pParse, "");
112701 return;
112702 }
112703 p->tnum = db->init.newTnum;
112704 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
112705 }
112706
112707 assert( (p->tabFlags & TF_HasPrimaryKey)==0
112708 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
112709 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
112710 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
112711
112712 /* Special processing for WITHOUT ROWID Tables */
112713 if( tabOpts & TF_WithoutRowid ){
112714 if( (p->tabFlags & TF_Autoincrement) ){
112715 sqlite3ErrorMsg(pParse,
112716 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
112717 return;
112718 }
112719 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
112720 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
112721 return;
112722 }
112723 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
112724 convertToWithoutRowidTable(pParse, p);
112725 }
112726 iDb = sqlite3SchemaToIndex(db, p->pSchema);
112727
112728#ifndef SQLITE_OMIT_CHECK
112729 /* Resolve names in all CHECK constraint expressions.
112730 */
112731 if( p->pCheck ){
112732 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
112733 if( pParse->nErr ){
112734 /* If errors are seen, delete the CHECK constraints now, else they might
112735 ** actually be used if PRAGMA writable_schema=ON is set. */
112736 sqlite3ExprListDelete(db, p->pCheck);
112737 p->pCheck = 0;
112738 }else{
112739 markExprListImmutable(p->pCheck);
112740 }
112741 }
112742#endif /* !defined(SQLITE_OMIT_CHECK) */
112743#ifndef SQLITE_OMIT_GENERATED_COLUMNS
112744 if( p->tabFlags & TF_HasGenerated ){
112745 int ii, nNG = 0;
112746 testcase( p->tabFlags & TF_HasVirtual );
112747 testcase( p->tabFlags & TF_HasStored );
112748 for(ii=0; ii<p->nCol; ii++){
112749 u32 colFlags = p->aCol[ii].colFlags;
112750 if( (colFlags & COLFLAG_GENERATED)!=0 ){
112751 Expr *pX = p->aCol[ii].pDflt;
112752 testcase( colFlags & COLFLAG_VIRTUAL );
112753 testcase( colFlags & COLFLAG_STORED );
112754 if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){
112755 /* If there are errors in resolving the expression, change the
112756 ** expression to a NULL. This prevents code generators that operate
112757 ** on the expression from inserting extra parts into the expression
112758 ** tree that have been allocated from lookaside memory, which is
112759 ** illegal in a schema and will lead to errors or heap corruption
112760 ** when the database connection closes. */
112761 sqlite3ExprDelete(db, pX);
112762 p->aCol[ii].pDflt = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
112763 }
112764 }else{
112765 nNG++;
112766 }
112767 }
112768 if( nNG==0 ){
112769 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
112770 return;
112771 }
112772 }
112773#endif
112774
112775 /* Estimate the average row size for the table and for all implied indices */
112776 estimateTableWidth(p);
112777 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
112778 estimateIndexWidth(pIdx);
112779 }
112780
112781 /* If not initializing, then create a record for the new table
112782 ** in the schema table of the database.
112783 **
112784 ** If this is a TEMPORARY table, write the entry into the auxiliary
112785 ** file instead of into the main database file.
112786 */
112787 if( !db->init.busy ){
112788 int n;
112789 Vdbe *v;
112790 char *zType; /* "view" or "table" */
112791 char *zType2; /* "VIEW" or "TABLE" */
112792 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
112793
112794 v = sqlite3GetVdbe(pParse);
112795 if( NEVER(v==0) ) return;
112796
112797 sqlite3VdbeAddOp1(v, OP_Close, 0);
112798
112799 /*
112800 ** Initialize zType for the new view or table.
112801 */
112802 if( p->pSelect==0 ){
112803 /* A regular table */
112804 zType = "table";
112805 zType2 = "TABLE";
112806#ifndef SQLITE_OMIT_VIEW
112807 }else{
112808 /* A view */
112809 zType = "view";
112810 zType2 = "VIEW";
112811#endif
112812 }
112813
112814 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
112815 ** statement to populate the new table. The root-page number for the
112816 ** new table is in register pParse->regRoot.
112817 **
112818 ** Once the SELECT has been coded by sqlite3Select(), it is in a
112819 ** suitable state to query for the column names and types to be used
112820 ** by the new table.
112821 **
112822 ** A shared-cache write-lock is not required to write to the new table,
112823 ** as a schema-lock must have already been obtained to create it. Since
112824 ** a schema-lock excludes all other database users, the write-lock would
112825 ** be redundant.
112826 */
112827 if( pSelect ){
112828 SelectDest dest; /* Where the SELECT should store results */
112829 int regYield; /* Register holding co-routine entry-point */
112830 int addrTop; /* Top of the co-routine */
112831 int regRec; /* A record to be insert into the new table */
112832 int regRowid; /* Rowid of the next row to insert */
112833 int addrInsLoop; /* Top of the loop for inserting rows */
112834 Table *pSelTab; /* A table that describes the SELECT results */
112835
112836 regYield = ++pParse->nMem;
112837 regRec = ++pParse->nMem;
112838 regRowid = ++pParse->nMem;
112839 assert(pParse->nTab==1);
112840 sqlite3MayAbort(pParse);
112841 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
112842 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
112843 pParse->nTab = 2;
112844 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
112845 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
112846 if( pParse->nErr ) return;
112847 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
112848 if( pSelTab==0 ) return;
112849 assert( p->aCol==0 );
112850 p->nCol = p->nNVCol = pSelTab->nCol;
112851 p->aCol = pSelTab->aCol;
112852 pSelTab->nCol = 0;
112853 pSelTab->aCol = 0;
112854 sqlite3DeleteTable(db, pSelTab);
112855 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
112856 sqlite3Select(pParse, pSelect, &dest);
112857 if( pParse->nErr ) return;
112858 sqlite3VdbeEndCoroutine(v, regYield);
112859 sqlite3VdbeJumpHere(v, addrTop - 1);
112860 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
112861 VdbeCoverage(v);
112862 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
112863 sqlite3TableAffinity(v, p, 0);
112864 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
112865 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
112866 sqlite3VdbeGoto(v, addrInsLoop);
112867 sqlite3VdbeJumpHere(v, addrInsLoop);
112868 sqlite3VdbeAddOp1(v, OP_Close, 1);
112869 }
112870
112871 /* Compute the complete text of the CREATE statement */
112872 if( pSelect ){
112873 zStmt = createTableStmt(db, p);
112874 }else{
112875 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
112876 n = (int)(pEnd2->z - pParse->sNameToken.z);
112877 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
112878 zStmt = sqlite3MPrintf(db,
112879 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
112880 );
112881 }
112882
112883 /* A slot for the record has already been allocated in the
112884 ** schema table. We just need to update that slot with all
112885 ** the information we've collected.
112886 */
112887 sqlite3NestedParse(pParse,
112888 "UPDATE %Q." DFLT_SCHEMA_TABLE
112889 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
112890 " WHERE rowid=#%d",
112891 db->aDb[iDb].zDbSName,
112892 zType,
112893 p->zName,
112894 p->zName,
112895 pParse->regRoot,
112896 zStmt,
112897 pParse->regRowid
112898 );
112899 sqlite3DbFree(db, zStmt);
112900 sqlite3ChangeCookie(pParse, iDb);
112901
112902#ifndef SQLITE_OMIT_AUTOINCREMENT
112903 /* Check to see if we need to create an sqlite_sequence table for
112904 ** keeping track of autoincrement keys.
112905 */
112906 if( (p->tabFlags & TF_Autoincrement)!=0 ){
112907 Db *pDb = &db->aDb[iDb];
112908 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112909 if( pDb->pSchema->pSeqTab==0 ){
112910 sqlite3NestedParse(pParse,
112911 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
112912 pDb->zDbSName
112913 );
112914 }
112915 }
112916#endif
112917
112918 /* Reparse everything to update our internal data structures */
112919 sqlite3VdbeAddParseSchemaOp(v, iDb,
112920 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
112921 }
112922
112923 /* Add the table to the in-memory representation of the database.
112924 */
112925 if( db->init.busy ){
112926 Table *pOld;
112927 Schema *pSchema = p->pSchema;
112928 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
112929 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
112930 if( pOld ){
112931 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
112932 sqlite3OomFault(db);
112933 return;
112934 }
112935 pParse->pNewTable = 0;
112936 db->mDbFlags |= DBFLAG_SchemaChange;
112937
112938#ifndef SQLITE_OMIT_ALTERTABLE
112939 if( !p->pSelect ){
112940 const char *zName = (const char *)pParse->sNameToken.z;
112941 int nName;
112942 assert( !pSelect && pCons && pEnd );
112943 if( pCons->z==0 ){
112944 pCons = pEnd;
112945 }
112946 nName = (int)((const char *)pCons->z - zName);
112947 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
112948 }
112949#endif
112950 }
112951}
112952
112953#ifndef SQLITE_OMIT_VIEW
112954/*
112955** The parser calls this routine in order to create a new VIEW
112956*/
112957SQLITE_PRIVATE void sqlite3CreateView(
112958 Parse *pParse, /* The parsing context */
112959 Token *pBegin, /* The CREATE token that begins the statement */
112960 Token *pName1, /* The token that holds the name of the view */
112961 Token *pName2, /* The token that holds the name of the view */
112962 ExprList *pCNames, /* Optional list of view column names */
112963 Select *pSelect, /* A SELECT statement that will become the new view */
112964 int isTemp, /* TRUE for a TEMPORARY view */
112965 int noErr /* Suppress error messages if VIEW already exists */
112966){
112967 Table *p;
112968 int n;
112969 const char *z;
112970 Token sEnd;
112971 DbFixer sFix;
112972 Token *pName = 0;
112973 int iDb;
112974 sqlite3 *db = pParse->db;
112975
112976 if( pParse->nVar>0 ){
112977 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
112978 goto create_view_fail;
112979 }
112980 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
112981 p = pParse->pNewTable;
112982 if( p==0 || pParse->nErr ) goto create_view_fail;
112983 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
112984 iDb = sqlite3SchemaToIndex(db, p->pSchema);
112985 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
112986 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
112987
112988 /* Make a copy of the entire SELECT statement that defines the view.
112989 ** This will force all the Expr.token.z values to be dynamically
112990 ** allocated rather than point to the input string - which means that
112991 ** they will persist after the current sqlite3_exec() call returns.
112992 */
112993 pSelect->selFlags |= SF_View;
112994 if( IN_RENAME_OBJECT ){
112995 p->pSelect = pSelect;
112996 pSelect = 0;
112997 }else{
112998 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
112999 }
113000 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
113001 if( db->mallocFailed ) goto create_view_fail;
113002
113003 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
113004 ** the end.
113005 */
113006 sEnd = pParse->sLastToken;
113007 assert( sEnd.z[0]!=0 || sEnd.n==0 );
113008 if( sEnd.z[0]!=';' ){
113009 sEnd.z += sEnd.n;
113010 }
113011 sEnd.n = 0;
113012 n = (int)(sEnd.z - pBegin->z);
113013 assert( n>0 );
113014 z = pBegin->z;
113015 while( sqlite3Isspace(z[n-1]) ){ n--; }
113016 sEnd.z = &z[n-1];
113017 sEnd.n = 1;
113018
113019 /* Use sqlite3EndTable() to add the view to the schema table */
113020 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
113021
113022create_view_fail:
113023 sqlite3SelectDelete(db, pSelect);
113024 if( IN_RENAME_OBJECT ){
113025 sqlite3RenameExprlistUnmap(pParse, pCNames);
113026 }
113027 sqlite3ExprListDelete(db, pCNames);
113028 return;
113029}
113030#endif /* SQLITE_OMIT_VIEW */
113031
113032#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
113033/*
113034** The Table structure pTable is really a VIEW. Fill in the names of
113035** the columns of the view in the pTable structure. Return the number
113036** of errors. If an error is seen leave an error message in pParse->zErrMsg.
113037*/
113038SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
113039 Table *pSelTab; /* A fake table from which we get the result set */
113040 Select *pSel; /* Copy of the SELECT that implements the view */
113041 int nErr = 0; /* Number of errors encountered */
113042 int n; /* Temporarily holds the number of cursors assigned */
113043 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
113044#ifndef SQLITE_OMIT_VIRTUALTABLE
113045 int rc;
113046#endif
113047#ifndef SQLITE_OMIT_AUTHORIZATION
113048 sqlite3_xauth xAuth; /* Saved xAuth pointer */
113049#endif
113050
113051 assert( pTable );
113052
113053#ifndef SQLITE_OMIT_VIRTUALTABLE
113054 db->nSchemaLock++;
113055 rc = sqlite3VtabCallConnect(pParse, pTable);
113056 db->nSchemaLock--;
113057 if( rc ){
113058 return 1;
113059 }
113060 if( IsVirtual(pTable) ) return 0;
113061#endif
113062
113063#ifndef SQLITE_OMIT_VIEW
113064 /* A positive nCol means the columns names for this view are
113065 ** already known.
113066 */
113067 if( pTable->nCol>0 ) return 0;
113068
113069 /* A negative nCol is a special marker meaning that we are currently
113070 ** trying to compute the column names. If we enter this routine with
113071 ** a negative nCol, it means two or more views form a loop, like this:
113072 **
113073 ** CREATE VIEW one AS SELECT * FROM two;
113074 ** CREATE VIEW two AS SELECT * FROM one;
113075 **
113076 ** Actually, the error above is now caught prior to reaching this point.
113077 ** But the following test is still important as it does come up
113078 ** in the following:
113079 **
113080 ** CREATE TABLE main.ex1(a);
113081 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
113082 ** SELECT * FROM temp.ex1;
113083 */
113084 if( pTable->nCol<0 ){
113085 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
113086 return 1;
113087 }
113088 assert( pTable->nCol>=0 );
113089
113090 /* If we get this far, it means we need to compute the table names.
113091 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
113092 ** "*" elements in the results set of the view and will assign cursors
113093 ** to the elements of the FROM clause. But we do not want these changes
113094 ** to be permanent. So the computation is done on a copy of the SELECT
113095 ** statement that defines the view.
113096 */
113097 assert( pTable->pSelect );
113098 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
113099 if( pSel ){
113100 u8 eParseMode = pParse->eParseMode;
113101 pParse->eParseMode = PARSE_MODE_NORMAL;
113102 n = pParse->nTab;
113103 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
113104 pTable->nCol = -1;
113105 DisableLookaside;
113106#ifndef SQLITE_OMIT_AUTHORIZATION
113107 xAuth = db->xAuth;
113108 db->xAuth = 0;
113109 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
113110 db->xAuth = xAuth;
113111#else
113112 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
113113#endif
113114 pParse->nTab = n;
113115 if( pSelTab==0 ){
113116 pTable->nCol = 0;
113117 nErr++;
113118 }else if( pTable->pCheck ){
113119 /* CREATE VIEW name(arglist) AS ...
113120 ** The names of the columns in the table are taken from
113121 ** arglist which is stored in pTable->pCheck. The pCheck field
113122 ** normally holds CHECK constraints on an ordinary table, but for
113123 ** a VIEW it holds the list of column names.
113124 */
113125 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
113126 &pTable->nCol, &pTable->aCol);
113127 if( db->mallocFailed==0
113128 && pParse->nErr==0
113129 && pTable->nCol==pSel->pEList->nExpr
113130 ){
113131 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
113132 SQLITE_AFF_NONE);
113133 }
113134 }else{
113135 /* CREATE VIEW name AS... without an argument list. Construct
113136 ** the column names from the SELECT statement that defines the view.
113137 */
113138 assert( pTable->aCol==0 );
113139 pTable->nCol = pSelTab->nCol;
113140 pTable->aCol = pSelTab->aCol;
113141 pSelTab->nCol = 0;
113142 pSelTab->aCol = 0;
113143 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
113144 }
113145 pTable->nNVCol = pTable->nCol;
113146 sqlite3DeleteTable(db, pSelTab);
113147 sqlite3SelectDelete(db, pSel);
113148 EnableLookaside;
113149 pParse->eParseMode = eParseMode;
113150 } else {
113151 nErr++;
113152 }
113153 pTable->pSchema->schemaFlags |= DB_UnresetViews;
113154 if( db->mallocFailed ){
113155 sqlite3DeleteColumnNames(db, pTable);
113156 pTable->aCol = 0;
113157 pTable->nCol = 0;
113158 }
113159#endif /* SQLITE_OMIT_VIEW */
113160 return nErr;
113161}
113162#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
113163
113164#ifndef SQLITE_OMIT_VIEW
113165/*
113166** Clear the column names from every VIEW in database idx.
113167*/
113168static void sqliteViewResetAll(sqlite3 *db, int idx){
113169 HashElem *i;
113170 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
113171 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
113172 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
113173 Table *pTab = sqliteHashData(i);
113174 if( pTab->pSelect ){
113175 sqlite3DeleteColumnNames(db, pTab);
113176 pTab->aCol = 0;
113177 pTab->nCol = 0;
113178 }
113179 }
113180 DbClearProperty(db, idx, DB_UnresetViews);
113181}
113182#else
113183# define sqliteViewResetAll(A,B)
113184#endif /* SQLITE_OMIT_VIEW */
113185
113186/*
113187** This function is called by the VDBE to adjust the internal schema
113188** used by SQLite when the btree layer moves a table root page. The
113189** root-page of a table or index in database iDb has changed from iFrom
113190** to iTo.
113191**
113192** Ticket #1728: The symbol table might still contain information
113193** on tables and/or indices that are the process of being deleted.
113194** If you are unlucky, one of those deleted indices or tables might
113195** have the same rootpage number as the real table or index that is
113196** being moved. So we cannot stop searching after the first match
113197** because the first match might be for one of the deleted indices
113198** or tables and not the table/index that is actually being moved.
113199** We must continue looping until all tables and indices with
113200** rootpage==iFrom have been converted to have a rootpage of iTo
113201** in order to be certain that we got the right one.
113202*/
113203#ifndef SQLITE_OMIT_AUTOVACUUM
113204SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
113205 HashElem *pElem;
113206 Hash *pHash;
113207 Db *pDb;
113208
113209 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113210 pDb = &db->aDb[iDb];
113211 pHash = &pDb->pSchema->tblHash;
113212 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
113213 Table *pTab = sqliteHashData(pElem);
113214 if( pTab->tnum==iFrom ){
113215 pTab->tnum = iTo;
113216 }
113217 }
113218 pHash = &pDb->pSchema->idxHash;
113219 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
113220 Index *pIdx = sqliteHashData(pElem);
113221 if( pIdx->tnum==iFrom ){
113222 pIdx->tnum = iTo;
113223 }
113224 }
113225}
113226#endif
113227
113228/*
113229** Write code to erase the table with root-page iTable from database iDb.
113230** Also write code to modify the sqlite_schema table and internal schema
113231** if a root-page of another table is moved by the btree-layer whilst
113232** erasing iTable (this can happen with an auto-vacuum database).
113233*/
113234static void destroyRootPage(Parse *pParse, int iTable, int iDb){
113235 Vdbe *v = sqlite3GetVdbe(pParse);
113236 int r1 = sqlite3GetTempReg(pParse);
113237 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
113238 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
113239 sqlite3MayAbort(pParse);
113240#ifndef SQLITE_OMIT_AUTOVACUUM
113241 /* OP_Destroy stores an in integer r1. If this integer
113242 ** is non-zero, then it is the root page number of a table moved to
113243 ** location iTable. The following code modifies the sqlite_schema table to
113244 ** reflect this.
113245 **
113246 ** The "#NNN" in the SQL is a special constant that means whatever value
113247 ** is in register NNN. See grammar rules associated with the TK_REGISTER
113248 ** token for additional information.
113249 */
113250 sqlite3NestedParse(pParse,
113251 "UPDATE %Q." DFLT_SCHEMA_TABLE
113252 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
113253 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
113254#endif
113255 sqlite3ReleaseTempReg(pParse, r1);
113256}
113257
113258/*
113259** Write VDBE code to erase table pTab and all associated indices on disk.
113260** Code to update the sqlite_schema tables and internal schema definitions
113261** in case a root-page belonging to another table is moved by the btree layer
113262** is also added (this can happen with an auto-vacuum database).
113263*/
113264static void destroyTable(Parse *pParse, Table *pTab){
113265 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
113266 ** is not defined), then it is important to call OP_Destroy on the
113267 ** table and index root-pages in order, starting with the numerically
113268 ** largest root-page number. This guarantees that none of the root-pages
113269 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
113270 ** following were coded:
113271 **
113272 ** OP_Destroy 4 0
113273 ** ...
113274 ** OP_Destroy 5 0
113275 **
113276 ** and root page 5 happened to be the largest root-page number in the
113277 ** database, then root page 5 would be moved to page 4 by the
113278 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
113279 ** a free-list page.
113280 */
113281 Pgno iTab = pTab->tnum;
113282 Pgno iDestroyed = 0;
113283
113284 while( 1 ){
113285 Index *pIdx;
113286 Pgno iLargest = 0;
113287
113288 if( iDestroyed==0 || iTab<iDestroyed ){
113289 iLargest = iTab;
113290 }
113291 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113292 Pgno iIdx = pIdx->tnum;
113293 assert( pIdx->pSchema==pTab->pSchema );
113294 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
113295 iLargest = iIdx;
113296 }
113297 }
113298 if( iLargest==0 ){
113299 return;
113300 }else{
113301 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113302 assert( iDb>=0 && iDb<pParse->db->nDb );
113303 destroyRootPage(pParse, iLargest, iDb);
113304 iDestroyed = iLargest;
113305 }
113306 }
113307}
113308
113309/*
113310** Remove entries from the sqlite_statN tables (for N in (1,2,3))
113311** after a DROP INDEX or DROP TABLE command.
113312*/
113313static void sqlite3ClearStatTables(
113314 Parse *pParse, /* The parsing context */
113315 int iDb, /* The database number */
113316 const char *zType, /* "idx" or "tbl" */
113317 const char *zName /* Name of index or table */
113318){
113319 int i;
113320 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
113321 for(i=1; i<=4; i++){
113322 char zTab[24];
113323 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
113324 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
113325 sqlite3NestedParse(pParse,
113326 "DELETE FROM %Q.%s WHERE %s=%Q",
113327 zDbName, zTab, zType, zName
113328 );
113329 }
113330 }
113331}
113332
113333/*
113334** Generate code to drop a table.
113335*/
113336SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
113337 Vdbe *v;
113338 sqlite3 *db = pParse->db;
113339 Trigger *pTrigger;
113340 Db *pDb = &db->aDb[iDb];
113341
113342 v = sqlite3GetVdbe(pParse);
113343 assert( v!=0 );
113344 sqlite3BeginWriteOperation(pParse, 1, iDb);
113345
113346#ifndef SQLITE_OMIT_VIRTUALTABLE
113347 if( IsVirtual(pTab) ){
113348 sqlite3VdbeAddOp0(v, OP_VBegin);
113349 }
113350#endif
113351
113352 /* Drop all triggers associated with the table being dropped. Code
113353 ** is generated to remove entries from sqlite_schema and/or
113354 ** sqlite_temp_schema if required.
113355 */
113356 pTrigger = sqlite3TriggerList(pParse, pTab);
113357 while( pTrigger ){
113358 assert( pTrigger->pSchema==pTab->pSchema ||
113359 pTrigger->pSchema==db->aDb[1].pSchema );
113360 sqlite3DropTriggerPtr(pParse, pTrigger);
113361 pTrigger = pTrigger->pNext;
113362 }
113363
113364#ifndef SQLITE_OMIT_AUTOINCREMENT
113365 /* Remove any entries of the sqlite_sequence table associated with
113366 ** the table being dropped. This is done before the table is dropped
113367 ** at the btree level, in case the sqlite_sequence table needs to
113368 ** move as a result of the drop (can happen in auto-vacuum mode).
113369 */
113370 if( pTab->tabFlags & TF_Autoincrement ){
113371 sqlite3NestedParse(pParse,
113372 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
113373 pDb->zDbSName, pTab->zName
113374 );
113375 }
113376#endif
113377
113378 /* Drop all entries in the schema table that refer to the
113379 ** table. The program name loops through the schema table and deletes
113380 ** every row that refers to a table of the same name as the one being
113381 ** dropped. Triggers are handled separately because a trigger can be
113382 ** created in the temp database that refers to a table in another
113383 ** database.
113384 */
113385 sqlite3NestedParse(pParse,
113386 "DELETE FROM %Q." DFLT_SCHEMA_TABLE
113387 " WHERE tbl_name=%Q and type!='trigger'",
113388 pDb->zDbSName, pTab->zName);
113389 if( !isView && !IsVirtual(pTab) ){
113390 destroyTable(pParse, pTab);
113391 }
113392
113393 /* Remove the table entry from SQLite's internal schema and modify
113394 ** the schema cookie.
113395 */
113396 if( IsVirtual(pTab) ){
113397 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
113398 sqlite3MayAbort(pParse);
113399 }
113400 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
113401 sqlite3ChangeCookie(pParse, iDb);
113402 sqliteViewResetAll(db, iDb);
113403}
113404
113405/*
113406** Return TRUE if shadow tables should be read-only in the current
113407** context.
113408*/
113409SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db){
113410#ifndef SQLITE_OMIT_VIRTUALTABLE
113411 if( (db->flags & SQLITE_Defensive)!=0
113412 && db->pVtabCtx==0
113413 && db->nVdbeExec==0
113414 ){
113415 return 1;
113416 }
113417#endif
113418 return 0;
113419}
113420
113421/*
113422** Return true if it is not allowed to drop the given table
113423*/
113424static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
113425 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
113426 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
113427 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
113428 return 1;
113429 }
113430 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
113431 return 1;
113432 }
113433 return 0;
113434}
113435
113436/*
113437** This routine is called to do the work of a DROP TABLE statement.
113438** pName is the name of the table to be dropped.
113439*/
113440SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
113441 Table *pTab;
113442 Vdbe *v;
113443 sqlite3 *db = pParse->db;
113444 int iDb;
113445
113446 if( db->mallocFailed ){
113447 goto exit_drop_table;
113448 }
113449 assert( pParse->nErr==0 );
113450 assert( pName->nSrc==1 );
113451 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
113452 if( noErr ) db->suppressErr++;
113453 assert( isView==0 || isView==LOCATE_VIEW );
113454 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
113455 if( noErr ) db->suppressErr--;
113456
113457 if( pTab==0 ){
113458 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
113459 goto exit_drop_table;
113460 }
113461 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113462 assert( iDb>=0 && iDb<db->nDb );
113463
113464 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
113465 ** it is initialized.
113466 */
113467 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
113468 goto exit_drop_table;
113469 }
113470#ifndef SQLITE_OMIT_AUTHORIZATION
113471 {
113472 int code;
113473 const char *zTab = SCHEMA_TABLE(iDb);
113474 const char *zDb = db->aDb[iDb].zDbSName;
113475 const char *zArg2 = 0;
113476 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
113477 goto exit_drop_table;
113478 }
113479 if( isView ){
113480 if( !OMIT_TEMPDB && iDb==1 ){
113481 code = SQLITE_DROP_TEMP_VIEW;
113482 }else{
113483 code = SQLITE_DROP_VIEW;
113484 }
113485#ifndef SQLITE_OMIT_VIRTUALTABLE
113486 }else if( IsVirtual(pTab) ){
113487 code = SQLITE_DROP_VTABLE;
113488 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
113489#endif
113490 }else{
113491 if( !OMIT_TEMPDB && iDb==1 ){
113492 code = SQLITE_DROP_TEMP_TABLE;
113493 }else{
113494 code = SQLITE_DROP_TABLE;
113495 }
113496 }
113497 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
113498 goto exit_drop_table;
113499 }
113500 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
113501 goto exit_drop_table;
113502 }
113503 }
113504#endif
113505 if( tableMayNotBeDropped(db, pTab) ){
113506 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
113507 goto exit_drop_table;
113508 }
113509
113510#ifndef SQLITE_OMIT_VIEW
113511 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
113512 ** on a table.
113513 */
113514 if( isView && pTab->pSelect==0 ){
113515 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
113516 goto exit_drop_table;
113517 }
113518 if( !isView && pTab->pSelect ){
113519 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
113520 goto exit_drop_table;
113521 }
113522#endif
113523
113524 /* Generate code to remove the table from the schema table
113525 ** on disk.
113526 */
113527 v = sqlite3GetVdbe(pParse);
113528 if( v ){
113529 sqlite3BeginWriteOperation(pParse, 1, iDb);
113530 if( !isView ){
113531 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
113532 sqlite3FkDropTable(pParse, pName, pTab);
113533 }
113534 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
113535 }
113536
113537exit_drop_table:
113538 sqlite3SrcListDelete(db, pName);
113539}
113540
113541/*
113542** This routine is called to create a new foreign key on the table
113543** currently under construction. pFromCol determines which columns
113544** in the current table point to the foreign key. If pFromCol==0 then
113545** connect the key to the last column inserted. pTo is the name of
113546** the table referred to (a.k.a the "parent" table). pToCol is a list
113547** of tables in the parent pTo table. flags contains all
113548** information about the conflict resolution algorithms specified
113549** in the ON DELETE, ON UPDATE and ON INSERT clauses.
113550**
113551** An FKey structure is created and added to the table currently
113552** under construction in the pParse->pNewTable field.
113553**
113554** The foreign key is set for IMMEDIATE processing. A subsequent call
113555** to sqlite3DeferForeignKey() might change this to DEFERRED.
113556*/
113557SQLITE_PRIVATE void sqlite3CreateForeignKey(
113558 Parse *pParse, /* Parsing context */
113559 ExprList *pFromCol, /* Columns in this table that point to other table */
113560 Token *pTo, /* Name of the other table */
113561 ExprList *pToCol, /* Columns in the other table */
113562 int flags /* Conflict resolution algorithms. */
113563){
113564 sqlite3 *db = pParse->db;
113565#ifndef SQLITE_OMIT_FOREIGN_KEY
113566 FKey *pFKey = 0;
113567 FKey *pNextTo;
113568 Table *p = pParse->pNewTable;
113569 int nByte;
113570 int i;
113571 int nCol;
113572 char *z;
113573
113574 assert( pTo!=0 );
113575 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
113576 if( pFromCol==0 ){
113577 int iCol = p->nCol-1;
113578 if( NEVER(iCol<0) ) goto fk_end;
113579 if( pToCol && pToCol->nExpr!=1 ){
113580 sqlite3ErrorMsg(pParse, "foreign key on %s"
113581 " should reference only one column of table %T",
113582 p->aCol[iCol].zName, pTo);
113583 goto fk_end;
113584 }
113585 nCol = 1;
113586 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
113587 sqlite3ErrorMsg(pParse,
113588 "number of columns in foreign key does not match the number of "
113589 "columns in the referenced table");
113590 goto fk_end;
113591 }else{
113592 nCol = pFromCol->nExpr;
113593 }
113594 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
113595 if( pToCol ){
113596 for(i=0; i<pToCol->nExpr; i++){
113597 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
113598 }
113599 }
113600 pFKey = sqlite3DbMallocZero(db, nByte );
113601 if( pFKey==0 ){
113602 goto fk_end;
113603 }
113604 pFKey->pFrom = p;
113605 pFKey->pNextFrom = p->pFKey;
113606 z = (char*)&pFKey->aCol[nCol];
113607 pFKey->zTo = z;
113608 if( IN_RENAME_OBJECT ){
113609 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
113610 }
113611 memcpy(z, pTo->z, pTo->n);
113612 z[pTo->n] = 0;
113613 sqlite3Dequote(z);
113614 z += pTo->n+1;
113615 pFKey->nCol = nCol;
113616 if( pFromCol==0 ){
113617 pFKey->aCol[0].iFrom = p->nCol-1;
113618 }else{
113619 for(i=0; i<nCol; i++){
113620 int j;
113621 for(j=0; j<p->nCol; j++){
113622 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zEName)==0 ){
113623 pFKey->aCol[i].iFrom = j;
113624 break;
113625 }
113626 }
113627 if( j>=p->nCol ){
113628 sqlite3ErrorMsg(pParse,
113629 "unknown column \"%s\" in foreign key definition",
113630 pFromCol->a[i].zEName);
113631 goto fk_end;
113632 }
113633 if( IN_RENAME_OBJECT ){
113634 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
113635 }
113636 }
113637 }
113638 if( pToCol ){
113639 for(i=0; i<nCol; i++){
113640 int n = sqlite3Strlen30(pToCol->a[i].zEName);
113641 pFKey->aCol[i].zCol = z;
113642 if( IN_RENAME_OBJECT ){
113643 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
113644 }
113645 memcpy(z, pToCol->a[i].zEName, n);
113646 z[n] = 0;
113647 z += n+1;
113648 }
113649 }
113650 pFKey->isDeferred = 0;
113651 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
113652 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
113653
113654 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
113655 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
113656 pFKey->zTo, (void *)pFKey
113657 );
113658 if( pNextTo==pFKey ){
113659 sqlite3OomFault(db);
113660 goto fk_end;
113661 }
113662 if( pNextTo ){
113663 assert( pNextTo->pPrevTo==0 );
113664 pFKey->pNextTo = pNextTo;
113665 pNextTo->pPrevTo = pFKey;
113666 }
113667
113668 /* Link the foreign key to the table as the last step.
113669 */
113670 p->pFKey = pFKey;
113671 pFKey = 0;
113672
113673fk_end:
113674 sqlite3DbFree(db, pFKey);
113675#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
113676 sqlite3ExprListDelete(db, pFromCol);
113677 sqlite3ExprListDelete(db, pToCol);
113678}
113679
113680/*
113681** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
113682** clause is seen as part of a foreign key definition. The isDeferred
113683** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
113684** The behavior of the most recently created foreign key is adjusted
113685** accordingly.
113686*/
113687SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
113688#ifndef SQLITE_OMIT_FOREIGN_KEY
113689 Table *pTab;
113690 FKey *pFKey;
113691 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
113692 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
113693 pFKey->isDeferred = (u8)isDeferred;
113694#endif
113695}
113696
113697/*
113698** Generate code that will erase and refill index *pIdx. This is
113699** used to initialize a newly created index or to recompute the
113700** content of an index in response to a REINDEX command.
113701**
113702** if memRootPage is not negative, it means that the index is newly
113703** created. The register specified by memRootPage contains the
113704** root page number of the index. If memRootPage is negative, then
113705** the index already exists and must be cleared before being refilled and
113706** the root page number of the index is taken from pIndex->tnum.
113707*/
113708static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
113709 Table *pTab = pIndex->pTable; /* The table that is indexed */
113710 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
113711 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
113712 int iSorter; /* Cursor opened by OpenSorter (if in use) */
113713 int addr1; /* Address of top of loop */
113714 int addr2; /* Address to jump to for next iteration */
113715 Pgno tnum; /* Root page of index */
113716 int iPartIdxLabel; /* Jump to this label to skip a row */
113717 Vdbe *v; /* Generate code into this virtual machine */
113718 KeyInfo *pKey; /* KeyInfo for index */
113719 int regRecord; /* Register holding assembled index record */
113720 sqlite3 *db = pParse->db; /* The database connection */
113721 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
113722
113723#ifndef SQLITE_OMIT_AUTHORIZATION
113724 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
113725 db->aDb[iDb].zDbSName ) ){
113726 return;
113727 }
113728#endif
113729
113730 /* Require a write-lock on the table to perform this operation */
113731 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
113732
113733 v = sqlite3GetVdbe(pParse);
113734 if( v==0 ) return;
113735 if( memRootPage>=0 ){
113736 tnum = (Pgno)memRootPage;
113737 }else{
113738 tnum = pIndex->tnum;
113739 }
113740 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
113741 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
113742
113743 /* Open the sorter cursor if we are to use one. */
113744 iSorter = pParse->nTab++;
113745 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
113746 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
113747
113748 /* Open the table. Loop through all rows of the table, inserting index
113749 ** records into the sorter. */
113750 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
113751 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
113752 regRecord = sqlite3GetTempReg(pParse);
113753 sqlite3MultiWrite(pParse);
113754
113755 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
113756 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
113757 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
113758 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
113759 sqlite3VdbeJumpHere(v, addr1);
113760 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
113761 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb,
113762 (char *)pKey, P4_KEYINFO);
113763 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
113764
113765 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
113766 if( IsUniqueIndex(pIndex) ){
113767 int j2 = sqlite3VdbeGoto(v, 1);
113768 addr2 = sqlite3VdbeCurrentAddr(v);
113769 sqlite3VdbeVerifyAbortable(v, OE_Abort);
113770 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
113771 pIndex->nKeyCol); VdbeCoverage(v);
113772 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
113773 sqlite3VdbeJumpHere(v, j2);
113774 }else{
113775 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
113776 ** abort. The exception is if one of the indexed expressions contains a
113777 ** user function that throws an exception when it is evaluated. But the
113778 ** overhead of adding a statement journal to a CREATE INDEX statement is
113779 ** very small (since most of the pages written do not contain content that
113780 ** needs to be restored if the statement aborts), so we call
113781 ** sqlite3MayAbort() for all CREATE INDEX statements. */
113782 sqlite3MayAbort(pParse);
113783 addr2 = sqlite3VdbeCurrentAddr(v);
113784 }
113785 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
113786 if( !pIndex->bAscKeyBug ){
113787 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
113788 ** faster by avoiding unnecessary seeks. But the optimization does
113789 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
113790 ** with DESC primary keys, since those indexes have there keys in
113791 ** a different order from the main table.
113792 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
113793 */
113794 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
113795 }
113796 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
113797 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
113798 sqlite3ReleaseTempReg(pParse, regRecord);
113799 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
113800 sqlite3VdbeJumpHere(v, addr1);
113801
113802 sqlite3VdbeAddOp1(v, OP_Close, iTab);
113803 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
113804 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
113805}
113806
113807/*
113808** Allocate heap space to hold an Index object with nCol columns.
113809**
113810** Increase the allocation size to provide an extra nExtra bytes
113811** of 8-byte aligned space after the Index object and return a
113812** pointer to this extra space in *ppExtra.
113813*/
113814SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
113815 sqlite3 *db, /* Database connection */
113816 i16 nCol, /* Total number of columns in the index */
113817 int nExtra, /* Number of bytes of extra space to alloc */
113818 char **ppExtra /* Pointer to the "extra" space */
113819){
113820 Index *p; /* Allocated index object */
113821 int nByte; /* Bytes of space for Index object + arrays */
113822
113823 nByte = ROUND8(sizeof(Index)) + /* Index structure */
113824 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
113825 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
113826 sizeof(i16)*nCol + /* Index.aiColumn */
113827 sizeof(u8)*nCol); /* Index.aSortOrder */
113828 p = sqlite3DbMallocZero(db, nByte + nExtra);
113829 if( p ){
113830 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
113831 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
113832 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
113833 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
113834 p->aSortOrder = (u8*)pExtra;
113835 p->nColumn = nCol;
113836 p->nKeyCol = nCol - 1;
113837 *ppExtra = ((char*)p) + nByte;
113838 }
113839 return p;
113840}
113841
113842/*
113843** If expression list pList contains an expression that was parsed with
113844** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
113845** pParse and return non-zero. Otherwise, return zero.
113846*/
113847SQLITE_PRIVATE int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
113848 if( pList ){
113849 int i;
113850 for(i=0; i<pList->nExpr; i++){
113851 if( pList->a[i].bNulls ){
113852 u8 sf = pList->a[i].sortFlags;
113853 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
113854 (sf==0 || sf==3) ? "FIRST" : "LAST"
113855 );
113856 return 1;
113857 }
113858 }
113859 }
113860 return 0;
113861}
113862
113863/*
113864** Create a new index for an SQL table. pName1.pName2 is the name of the index
113865** and pTblList is the name of the table that is to be indexed. Both will
113866** be NULL for a primary key or an index that is created to satisfy a
113867** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
113868** as the table to be indexed. pParse->pNewTable is a table that is
113869** currently being constructed by a CREATE TABLE statement.
113870**
113871** pList is a list of columns to be indexed. pList will be NULL if this
113872** is a primary key or unique-constraint on the most recent column added
113873** to the table currently under construction.
113874*/
113875SQLITE_PRIVATE void sqlite3CreateIndex(
113876 Parse *pParse, /* All information about this parse */
113877 Token *pName1, /* First part of index name. May be NULL */
113878 Token *pName2, /* Second part of index name. May be NULL */
113879 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
113880 ExprList *pList, /* A list of columns to be indexed */
113881 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
113882 Token *pStart, /* The CREATE token that begins this statement */
113883 Expr *pPIWhere, /* WHERE clause for partial indices */
113884 int sortOrder, /* Sort order of primary key when pList==NULL */
113885 int ifNotExist, /* Omit error if index already exists */
113886 u8 idxType /* The index type */
113887){
113888 Table *pTab = 0; /* Table to be indexed */
113889 Index *pIndex = 0; /* The index to be created */
113890 char *zName = 0; /* Name of the index */
113891 int nName; /* Number of characters in zName */
113892 int i, j;
113893 DbFixer sFix; /* For assigning database names to pTable */
113894 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
113895 sqlite3 *db = pParse->db;
113896 Db *pDb; /* The specific table containing the indexed database */
113897 int iDb; /* Index of the database that is being written */
113898 Token *pName = 0; /* Unqualified name of the index to create */
113899 struct ExprList_item *pListItem; /* For looping over pList */
113900 int nExtra = 0; /* Space allocated for zExtra[] */
113901 int nExtraCol; /* Number of extra columns needed */
113902 char *zExtra = 0; /* Extra space after the Index object */
113903 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
113904
113905 if( db->mallocFailed || pParse->nErr>0 ){
113906 goto exit_create_index;
113907 }
113908 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
113909 goto exit_create_index;
113910 }
113911 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
113912 goto exit_create_index;
113913 }
113914 if( sqlite3HasExplicitNulls(pParse, pList) ){
113915 goto exit_create_index;
113916 }
113917
113918 /*
113919 ** Find the table that is to be indexed. Return early if not found.
113920 */
113921 if( pTblName!=0 ){
113922
113923 /* Use the two-part index name to determine the database
113924 ** to search for the table. 'Fix' the table name to this db
113925 ** before looking up the table.
113926 */
113927 assert( pName1 && pName2 );
113928 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113929 if( iDb<0 ) goto exit_create_index;
113930 assert( pName && pName->z );
113931
113932#ifndef SQLITE_OMIT_TEMPDB
113933 /* If the index name was unqualified, check if the table
113934 ** is a temp table. If so, set the database to 1. Do not do this
113935 ** if initialising a database schema.
113936 */
113937 if( !db->init.busy ){
113938 pTab = sqlite3SrcListLookup(pParse, pTblName);
113939 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
113940 iDb = 1;
113941 }
113942 }
113943#endif
113944
113945 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
113946 if( sqlite3FixSrcList(&sFix, pTblName) ){
113947 /* Because the parser constructs pTblName from a single identifier,
113948 ** sqlite3FixSrcList can never fail. */
113949 assert(0);
113950 }
113951 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
113952 assert( db->mallocFailed==0 || pTab==0 );
113953 if( pTab==0 ) goto exit_create_index;
113954 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
113955 sqlite3ErrorMsg(pParse,
113956 "cannot create a TEMP index on non-TEMP table \"%s\"",
113957 pTab->zName);
113958 goto exit_create_index;
113959 }
113960 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
113961 }else{
113962 assert( pName==0 );
113963 assert( pStart==0 );
113964 pTab = pParse->pNewTable;
113965 if( !pTab ) goto exit_create_index;
113966 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113967 }
113968 pDb = &db->aDb[iDb];
113969
113970 assert( pTab!=0 );
113971 assert( pParse->nErr==0 );
113972 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
113973 && db->init.busy==0
113974 && pTblName!=0
113975#if SQLITE_USER_AUTHENTICATION
113976 && sqlite3UserAuthTable(pTab->zName)==0
113977#endif
113978 ){
113979 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
113980 goto exit_create_index;
113981 }
113982#ifndef SQLITE_OMIT_VIEW
113983 if( pTab->pSelect ){
113984 sqlite3ErrorMsg(pParse, "views may not be indexed");
113985 goto exit_create_index;
113986 }
113987#endif
113988#ifndef SQLITE_OMIT_VIRTUALTABLE
113989 if( IsVirtual(pTab) ){
113990 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
113991 goto exit_create_index;
113992 }
113993#endif
113994
113995 /*
113996 ** Find the name of the index. Make sure there is not already another
113997 ** index or table with the same name.
113998 **
113999 ** Exception: If we are reading the names of permanent indices from the
114000 ** sqlite_schema table (because some other process changed the schema) and
114001 ** one of the index names collides with the name of a temporary table or
114002 ** index, then we will continue to process this index.
114003 **
114004 ** If pName==0 it means that we are
114005 ** dealing with a primary key or UNIQUE constraint. We have to invent our
114006 ** own name.
114007 */
114008 if( pName ){
114009 zName = sqlite3NameFromToken(db, pName);
114010 if( zName==0 ) goto exit_create_index;
114011 assert( pName->z!=0 );
114012 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
114013 goto exit_create_index;
114014 }
114015 if( !IN_RENAME_OBJECT ){
114016 if( !db->init.busy ){
114017 if( sqlite3FindTable(db, zName, 0)!=0 ){
114018 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
114019 goto exit_create_index;
114020 }
114021 }
114022 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
114023 if( !ifNotExist ){
114024 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
114025 }else{
114026 assert( !db->init.busy );
114027 sqlite3CodeVerifySchema(pParse, iDb);
114028 }
114029 goto exit_create_index;
114030 }
114031 }
114032 }else{
114033 int n;
114034 Index *pLoop;
114035 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
114036 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
114037 if( zName==0 ){
114038 goto exit_create_index;
114039 }
114040
114041 /* Automatic index names generated from within sqlite3_declare_vtab()
114042 ** must have names that are distinct from normal automatic index names.
114043 ** The following statement converts "sqlite3_autoindex..." into
114044 ** "sqlite3_butoindex..." in order to make the names distinct.
114045 ** The "vtab_err.test" test demonstrates the need of this statement. */
114046 if( IN_SPECIAL_PARSE ) zName[7]++;
114047 }
114048
114049 /* Check for authorization to create an index.
114050 */
114051#ifndef SQLITE_OMIT_AUTHORIZATION
114052 if( !IN_RENAME_OBJECT ){
114053 const char *zDb = pDb->zDbSName;
114054 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
114055 goto exit_create_index;
114056 }
114057 i = SQLITE_CREATE_INDEX;
114058 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
114059 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
114060 goto exit_create_index;
114061 }
114062 }
114063#endif
114064
114065 /* If pList==0, it means this routine was called to make a primary
114066 ** key out of the last column added to the table under construction.
114067 ** So create a fake list to simulate this.
114068 */
114069 if( pList==0 ){
114070 Token prevCol;
114071 Column *pCol = &pTab->aCol[pTab->nCol-1];
114072 pCol->colFlags |= COLFLAG_UNIQUE;
114073 sqlite3TokenInit(&prevCol, pCol->zName);
114074 pList = sqlite3ExprListAppend(pParse, 0,
114075 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
114076 if( pList==0 ) goto exit_create_index;
114077 assert( pList->nExpr==1 );
114078 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED);
114079 }else{
114080 sqlite3ExprListCheckLength(pParse, pList, "index");
114081 if( pParse->nErr ) goto exit_create_index;
114082 }
114083
114084 /* Figure out how many bytes of space are required to store explicitly
114085 ** specified collation sequence names.
114086 */
114087 for(i=0; i<pList->nExpr; i++){
114088 Expr *pExpr = pList->a[i].pExpr;
114089 assert( pExpr!=0 );
114090 if( pExpr->op==TK_COLLATE ){
114091 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
114092 }
114093 }
114094
114095 /*
114096 ** Allocate the index structure.
114097 */
114098 nName = sqlite3Strlen30(zName);
114099 nExtraCol = pPk ? pPk->nKeyCol : 1;
114100 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
114101 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
114102 nName + nExtra + 1, &zExtra);
114103 if( db->mallocFailed ){
114104 goto exit_create_index;
114105 }
114106 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
114107 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
114108 pIndex->zName = zExtra;
114109 zExtra += nName + 1;
114110 memcpy(pIndex->zName, zName, nName+1);
114111 pIndex->pTable = pTab;
114112 pIndex->onError = (u8)onError;
114113 pIndex->uniqNotNull = onError!=OE_None;
114114 pIndex->idxType = idxType;
114115 pIndex->pSchema = db->aDb[iDb].pSchema;
114116 pIndex->nKeyCol = pList->nExpr;
114117 if( pPIWhere ){
114118 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
114119 pIndex->pPartIdxWhere = pPIWhere;
114120 pPIWhere = 0;
114121 }
114122 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
114123
114124 /* Check to see if we should honor DESC requests on index columns
114125 */
114126 if( pDb->pSchema->file_format>=4 ){
114127 sortOrderMask = -1; /* Honor DESC */
114128 }else{
114129 sortOrderMask = 0; /* Ignore DESC */
114130 }
114131
114132 /* Analyze the list of expressions that form the terms of the index and
114133 ** report any errors. In the common case where the expression is exactly
114134 ** a table column, store that column in aiColumn[]. For general expressions,
114135 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
114136 **
114137 ** TODO: Issue a warning if two or more columns of the index are identical.
114138 ** TODO: Issue a warning if the table primary key is used as part of the
114139 ** index key.
114140 */
114141 pListItem = pList->a;
114142 if( IN_RENAME_OBJECT ){
114143 pIndex->aColExpr = pList;
114144 pList = 0;
114145 }
114146 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
114147 Expr *pCExpr; /* The i-th index expression */
114148 int requestedSortOrder; /* ASC or DESC on the i-th expression */
114149 const char *zColl; /* Collation sequence name */
114150
114151 sqlite3StringToId(pListItem->pExpr);
114152 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
114153 if( pParse->nErr ) goto exit_create_index;
114154 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
114155 if( pCExpr->op!=TK_COLUMN ){
114156 if( pTab==pParse->pNewTable ){
114157 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
114158 "UNIQUE constraints");
114159 goto exit_create_index;
114160 }
114161 if( pIndex->aColExpr==0 ){
114162 pIndex->aColExpr = pList;
114163 pList = 0;
114164 }
114165 j = XN_EXPR;
114166 pIndex->aiColumn[i] = XN_EXPR;
114167 pIndex->uniqNotNull = 0;
114168 }else{
114169 j = pCExpr->iColumn;
114170 assert( j<=0x7fff );
114171 if( j<0 ){
114172 j = pTab->iPKey;
114173 }else{
114174 if( pTab->aCol[j].notNull==0 ){
114175 pIndex->uniqNotNull = 0;
114176 }
114177 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
114178 pIndex->bHasVCol = 1;
114179 }
114180 }
114181 pIndex->aiColumn[i] = (i16)j;
114182 }
114183 zColl = 0;
114184 if( pListItem->pExpr->op==TK_COLLATE ){
114185 int nColl;
114186 zColl = pListItem->pExpr->u.zToken;
114187 nColl = sqlite3Strlen30(zColl) + 1;
114188 assert( nExtra>=nColl );
114189 memcpy(zExtra, zColl, nColl);
114190 zColl = zExtra;
114191 zExtra += nColl;
114192 nExtra -= nColl;
114193 }else if( j>=0 ){
114194 zColl = pTab->aCol[j].zColl;
114195 }
114196 if( !zColl ) zColl = sqlite3StrBINARY;
114197 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
114198 goto exit_create_index;
114199 }
114200 pIndex->azColl[i] = zColl;
114201 requestedSortOrder = pListItem->sortFlags & sortOrderMask;
114202 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
114203 }
114204
114205 /* Append the table key to the end of the index. For WITHOUT ROWID
114206 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
114207 ** normal tables (when pPk==0) this will be the rowid.
114208 */
114209 if( pPk ){
114210 for(j=0; j<pPk->nKeyCol; j++){
114211 int x = pPk->aiColumn[j];
114212 assert( x>=0 );
114213 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
114214 pIndex->nColumn--;
114215 }else{
114216 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
114217 pIndex->aiColumn[i] = x;
114218 pIndex->azColl[i] = pPk->azColl[j];
114219 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
114220 i++;
114221 }
114222 }
114223 assert( i==pIndex->nColumn );
114224 }else{
114225 pIndex->aiColumn[i] = XN_ROWID;
114226 pIndex->azColl[i] = sqlite3StrBINARY;
114227 }
114228 sqlite3DefaultRowEst(pIndex);
114229 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
114230
114231 /* If this index contains every column of its table, then mark
114232 ** it as a covering index */
114233 assert( HasRowid(pTab)
114234 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
114235 recomputeColumnsNotIndexed(pIndex);
114236 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
114237 pIndex->isCovering = 1;
114238 for(j=0; j<pTab->nCol; j++){
114239 if( j==pTab->iPKey ) continue;
114240 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
114241 pIndex->isCovering = 0;
114242 break;
114243 }
114244 }
114245
114246 if( pTab==pParse->pNewTable ){
114247 /* This routine has been called to create an automatic index as a
114248 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
114249 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
114250 ** i.e. one of:
114251 **
114252 ** CREATE TABLE t(x PRIMARY KEY, y);
114253 ** CREATE TABLE t(x, y, UNIQUE(x, y));
114254 **
114255 ** Either way, check to see if the table already has such an index. If
114256 ** so, don't bother creating this one. This only applies to
114257 ** automatically created indices. Users can do as they wish with
114258 ** explicit indices.
114259 **
114260 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
114261 ** (and thus suppressing the second one) even if they have different
114262 ** sort orders.
114263 **
114264 ** If there are different collating sequences or if the columns of
114265 ** the constraint occur in different orders, then the constraints are
114266 ** considered distinct and both result in separate indices.
114267 */
114268 Index *pIdx;
114269 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114270 int k;
114271 assert( IsUniqueIndex(pIdx) );
114272 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
114273 assert( IsUniqueIndex(pIndex) );
114274
114275 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
114276 for(k=0; k<pIdx->nKeyCol; k++){
114277 const char *z1;
114278 const char *z2;
114279 assert( pIdx->aiColumn[k]>=0 );
114280 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
114281 z1 = pIdx->azColl[k];
114282 z2 = pIndex->azColl[k];
114283 if( sqlite3StrICmp(z1, z2) ) break;
114284 }
114285 if( k==pIdx->nKeyCol ){
114286 if( pIdx->onError!=pIndex->onError ){
114287 /* This constraint creates the same index as a previous
114288 ** constraint specified somewhere in the CREATE TABLE statement.
114289 ** However the ON CONFLICT clauses are different. If both this
114290 ** constraint and the previous equivalent constraint have explicit
114291 ** ON CONFLICT clauses this is an error. Otherwise, use the
114292 ** explicitly specified behavior for the index.
114293 */
114294 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
114295 sqlite3ErrorMsg(pParse,
114296 "conflicting ON CONFLICT clauses specified", 0);
114297 }
114298 if( pIdx->onError==OE_Default ){
114299 pIdx->onError = pIndex->onError;
114300 }
114301 }
114302 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
114303 if( IN_RENAME_OBJECT ){
114304 pIndex->pNext = pParse->pNewIndex;
114305 pParse->pNewIndex = pIndex;
114306 pIndex = 0;
114307 }
114308 goto exit_create_index;
114309 }
114310 }
114311 }
114312
114313 if( !IN_RENAME_OBJECT ){
114314
114315 /* Link the new Index structure to its table and to the other
114316 ** in-memory database structures.
114317 */
114318 assert( pParse->nErr==0 );
114319 if( db->init.busy ){
114320 Index *p;
114321 assert( !IN_SPECIAL_PARSE );
114322 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
114323 if( pTblName!=0 ){
114324 pIndex->tnum = db->init.newTnum;
114325 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
114326 sqlite3ErrorMsg(pParse, "invalid rootpage");
114327 pParse->rc = SQLITE_CORRUPT_BKPT;
114328 goto exit_create_index;
114329 }
114330 }
114331 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
114332 pIndex->zName, pIndex);
114333 if( p ){
114334 assert( p==pIndex ); /* Malloc must have failed */
114335 sqlite3OomFault(db);
114336 goto exit_create_index;
114337 }
114338 db->mDbFlags |= DBFLAG_SchemaChange;
114339 }
114340
114341 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
114342 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
114343 ** emit code to allocate the index rootpage on disk and make an entry for
114344 ** the index in the sqlite_schema table and populate the index with
114345 ** content. But, do not do this if we are simply reading the sqlite_schema
114346 ** table to parse the schema, or if this index is the PRIMARY KEY index
114347 ** of a WITHOUT ROWID table.
114348 **
114349 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
114350 ** or UNIQUE index in a CREATE TABLE statement. Since the table
114351 ** has just been created, it contains no data and the index initialization
114352 ** step can be skipped.
114353 */
114354 else if( HasRowid(pTab) || pTblName!=0 ){
114355 Vdbe *v;
114356 char *zStmt;
114357 int iMem = ++pParse->nMem;
114358
114359 v = sqlite3GetVdbe(pParse);
114360 if( v==0 ) goto exit_create_index;
114361
114362 sqlite3BeginWriteOperation(pParse, 1, iDb);
114363
114364 /* Create the rootpage for the index using CreateIndex. But before
114365 ** doing so, code a Noop instruction and store its address in
114366 ** Index.tnum. This is required in case this index is actually a
114367 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
114368 ** that case the convertToWithoutRowidTable() routine will replace
114369 ** the Noop with a Goto to jump over the VDBE code generated below. */
114370 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
114371 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
114372
114373 /* Gather the complete text of the CREATE INDEX statement into
114374 ** the zStmt variable
114375 */
114376 assert( pName!=0 || pStart==0 );
114377 if( pStart ){
114378 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
114379 if( pName->z[n-1]==';' ) n--;
114380 /* A named index with an explicit CREATE INDEX statement */
114381 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
114382 onError==OE_None ? "" : " UNIQUE", n, pName->z);
114383 }else{
114384 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
114385 /* zStmt = sqlite3MPrintf(""); */
114386 zStmt = 0;
114387 }
114388
114389 /* Add an entry in sqlite_schema for this index
114390 */
114391 sqlite3NestedParse(pParse,
114392 "INSERT INTO %Q." DFLT_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);",
114393 db->aDb[iDb].zDbSName,
114394 pIndex->zName,
114395 pTab->zName,
114396 iMem,
114397 zStmt
114398 );
114399 sqlite3DbFree(db, zStmt);
114400
114401 /* Fill the index with data and reparse the schema. Code an OP_Expire
114402 ** to invalidate all pre-compiled statements.
114403 */
114404 if( pTblName ){
114405 sqlite3RefillIndex(pParse, pIndex, iMem);
114406 sqlite3ChangeCookie(pParse, iDb);
114407 sqlite3VdbeAddParseSchemaOp(v, iDb,
114408 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
114409 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
114410 }
114411
114412 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
114413 }
114414 }
114415 if( db->init.busy || pTblName==0 ){
114416 pIndex->pNext = pTab->pIndex;
114417 pTab->pIndex = pIndex;
114418 pIndex = 0;
114419 }
114420 else if( IN_RENAME_OBJECT ){
114421 assert( pParse->pNewIndex==0 );
114422 pParse->pNewIndex = pIndex;
114423 pIndex = 0;
114424 }
114425
114426 /* Clean up before exiting */
114427exit_create_index:
114428 if( pIndex ) sqlite3FreeIndex(db, pIndex);
114429 if( pTab ){ /* Ensure all REPLACE indexes are at the end of the list */
114430 Index **ppFrom = &pTab->pIndex;
114431 Index *pThis;
114432 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
114433 Index *pNext;
114434 if( pThis->onError!=OE_Replace ) continue;
114435 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
114436 *ppFrom = pNext;
114437 pThis->pNext = pNext->pNext;
114438 pNext->pNext = pThis;
114439 ppFrom = &pNext->pNext;
114440 }
114441 break;
114442 }
114443 }
114444 sqlite3ExprDelete(db, pPIWhere);
114445 sqlite3ExprListDelete(db, pList);
114446 sqlite3SrcListDelete(db, pTblName);
114447 sqlite3DbFree(db, zName);
114448}
114449
114450/*
114451** Fill the Index.aiRowEst[] array with default information - information
114452** to be used when we have not run the ANALYZE command.
114453**
114454** aiRowEst[0] is supposed to contain the number of elements in the index.
114455** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
114456** number of rows in the table that match any particular value of the
114457** first column of the index. aiRowEst[2] is an estimate of the number
114458** of rows that match any particular combination of the first 2 columns
114459** of the index. And so forth. It must always be the case that
114460*
114461** aiRowEst[N]<=aiRowEst[N-1]
114462** aiRowEst[N]>=1
114463**
114464** Apart from that, we have little to go on besides intuition as to
114465** how aiRowEst[] should be initialized. The numbers generated here
114466** are based on typical values found in actual indices.
114467*/
114468SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
114469 /* 10, 9, 8, 7, 6 */
114470 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
114471 LogEst *a = pIdx->aiRowLogEst;
114472 LogEst x;
114473 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
114474 int i;
114475
114476 /* Indexes with default row estimates should not have stat1 data */
114477 assert( !pIdx->hasStat1 );
114478
114479 /* Set the first entry (number of rows in the index) to the estimated
114480 ** number of rows in the table, or half the number of rows in the table
114481 ** for a partial index.
114482 **
114483 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
114484 ** table but other parts we are having to guess at, then do not let the
114485 ** estimated number of rows in the table be less than 1000 (LogEst 99).
114486 ** Failure to do this can cause the indexes for which we do not have
114487 ** stat1 data to be ignored by the query planner.
114488 */
114489 x = pIdx->pTable->nRowLogEst;
114490 assert( 99==sqlite3LogEst(1000) );
114491 if( x<99 ){
114492 pIdx->pTable->nRowLogEst = x = 99;
114493 }
114494 if( pIdx->pPartIdxWhere!=0 ) x -= 10; assert( 10==sqlite3LogEst(2) );
114495 a[0] = x;
114496
114497 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
114498 ** 6 and each subsequent value (if any) is 5. */
114499 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
114500 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
114501 a[i] = 23; assert( 23==sqlite3LogEst(5) );
114502 }
114503
114504 assert( 0==sqlite3LogEst(1) );
114505 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
114506}
114507
114508/*
114509** This routine will drop an existing named index. This routine
114510** implements the DROP INDEX statement.
114511*/
114512SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
114513 Index *pIndex;
114514 Vdbe *v;
114515 sqlite3 *db = pParse->db;
114516 int iDb;
114517
114518 assert( pParse->nErr==0 ); /* Never called with prior errors */
114519 if( db->mallocFailed ){
114520 goto exit_drop_index;
114521 }
114522 assert( pName->nSrc==1 );
114523 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
114524 goto exit_drop_index;
114525 }
114526 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
114527 if( pIndex==0 ){
114528 if( !ifExists ){
114529 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
114530 }else{
114531 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
114532 }
114533 pParse->checkSchema = 1;
114534 goto exit_drop_index;
114535 }
114536 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
114537 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
114538 "or PRIMARY KEY constraint cannot be dropped", 0);
114539 goto exit_drop_index;
114540 }
114541 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
114542#ifndef SQLITE_OMIT_AUTHORIZATION
114543 {
114544 int code = SQLITE_DROP_INDEX;
114545 Table *pTab = pIndex->pTable;
114546 const char *zDb = db->aDb[iDb].zDbSName;
114547 const char *zTab = SCHEMA_TABLE(iDb);
114548 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
114549 goto exit_drop_index;
114550 }
114551 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
114552 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
114553 goto exit_drop_index;
114554 }
114555 }
114556#endif
114557
114558 /* Generate code to remove the index and from the schema table */
114559 v = sqlite3GetVdbe(pParse);
114560 if( v ){
114561 sqlite3BeginWriteOperation(pParse, 1, iDb);
114562 sqlite3NestedParse(pParse,
114563 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='index'",
114564 db->aDb[iDb].zDbSName, pIndex->zName
114565 );
114566 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
114567 sqlite3ChangeCookie(pParse, iDb);
114568 destroyRootPage(pParse, pIndex->tnum, iDb);
114569 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
114570 }
114571
114572exit_drop_index:
114573 sqlite3SrcListDelete(db, pName);
114574}
114575
114576/*
114577** pArray is a pointer to an array of objects. Each object in the
114578** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
114579** to extend the array so that there is space for a new object at the end.
114580**
114581** When this function is called, *pnEntry contains the current size of
114582** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
114583** in total).
114584**
114585** If the realloc() is successful (i.e. if no OOM condition occurs), the
114586** space allocated for the new object is zeroed, *pnEntry updated to
114587** reflect the new size of the array and a pointer to the new allocation
114588** returned. *pIdx is set to the index of the new array entry in this case.
114589**
114590** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
114591** unchanged and a copy of pArray returned.
114592*/
114593SQLITE_PRIVATE void *sqlite3ArrayAllocate(
114594 sqlite3 *db, /* Connection to notify of malloc failures */
114595 void *pArray, /* Array of objects. Might be reallocated */
114596 int szEntry, /* Size of each object in the array */
114597 int *pnEntry, /* Number of objects currently in use */
114598 int *pIdx /* Write the index of a new slot here */
114599){
114600 char *z;
114601 sqlite3_int64 n = *pIdx = *pnEntry;
114602 if( (n & (n-1))==0 ){
114603 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
114604 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
114605 if( pNew==0 ){
114606 *pIdx = -1;
114607 return pArray;
114608 }
114609 pArray = pNew;
114610 }
114611 z = (char*)pArray;
114612 memset(&z[n * szEntry], 0, szEntry);
114613 ++*pnEntry;
114614 return pArray;
114615}
114616
114617/*
114618** Append a new element to the given IdList. Create a new IdList if
114619** need be.
114620**
114621** A new IdList is returned, or NULL if malloc() fails.
114622*/
114623SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
114624 sqlite3 *db = pParse->db;
114625 int i;
114626 if( pList==0 ){
114627 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
114628 if( pList==0 ) return 0;
114629 }
114630 pList->a = sqlite3ArrayAllocate(
114631 db,
114632 pList->a,
114633 sizeof(pList->a[0]),
114634 &pList->nId,
114635 &i
114636 );
114637 if( i<0 ){
114638 sqlite3IdListDelete(db, pList);
114639 return 0;
114640 }
114641 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
114642 if( IN_RENAME_OBJECT && pList->a[i].zName ){
114643 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
114644 }
114645 return pList;
114646}
114647
114648/*
114649** Delete an IdList.
114650*/
114651SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
114652 int i;
114653 if( pList==0 ) return;
114654 for(i=0; i<pList->nId; i++){
114655 sqlite3DbFree(db, pList->a[i].zName);
114656 }
114657 sqlite3DbFree(db, pList->a);
114658 sqlite3DbFreeNN(db, pList);
114659}
114660
114661/*
114662** Return the index in pList of the identifier named zId. Return -1
114663** if not found.
114664*/
114665SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
114666 int i;
114667 if( pList==0 ) return -1;
114668 for(i=0; i<pList->nId; i++){
114669 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
114670 }
114671 return -1;
114672}
114673
114674/*
114675** Maximum size of a SrcList object.
114676** The SrcList object is used to represent the FROM clause of a
114677** SELECT statement, and the query planner cannot deal with more
114678** than 64 tables in a join. So any value larger than 64 here
114679** is sufficient for most uses. Smaller values, like say 10, are
114680** appropriate for small and memory-limited applications.
114681*/
114682#ifndef SQLITE_MAX_SRCLIST
114683# define SQLITE_MAX_SRCLIST 200
114684#endif
114685
114686/*
114687** Expand the space allocated for the given SrcList object by
114688** creating nExtra new slots beginning at iStart. iStart is zero based.
114689** New slots are zeroed.
114690**
114691** For example, suppose a SrcList initially contains two entries: A,B.
114692** To append 3 new entries onto the end, do this:
114693**
114694** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
114695**
114696** After the call above it would contain: A, B, nil, nil, nil.
114697** If the iStart argument had been 1 instead of 2, then the result
114698** would have been: A, nil, nil, nil, B. To prepend the new slots,
114699** the iStart value would be 0. The result then would
114700** be: nil, nil, nil, A, B.
114701**
114702** If a memory allocation fails or the SrcList becomes too large, leave
114703** the original SrcList unchanged, return NULL, and leave an error message
114704** in pParse.
114705*/
114706SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
114707 Parse *pParse, /* Parsing context into which errors are reported */
114708 SrcList *pSrc, /* The SrcList to be enlarged */
114709 int nExtra, /* Number of new slots to add to pSrc->a[] */
114710 int iStart /* Index in pSrc->a[] of first new slot */
114711){
114712 int i;
114713
114714 /* Sanity checking on calling parameters */
114715 assert( iStart>=0 );
114716 assert( nExtra>=1 );
114717 assert( pSrc!=0 );
114718 assert( iStart<=pSrc->nSrc );
114719
114720 /* Allocate additional space if needed */
114721 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
114722 SrcList *pNew;
114723 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
114724 sqlite3 *db = pParse->db;
114725
114726 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
114727 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
114728 SQLITE_MAX_SRCLIST);
114729 return 0;
114730 }
114731 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
114732 pNew = sqlite3DbRealloc(db, pSrc,
114733 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
114734 if( pNew==0 ){
114735 assert( db->mallocFailed );
114736 return 0;
114737 }
114738 pSrc = pNew;
114739 pSrc->nAlloc = nAlloc;
114740 }
114741
114742 /* Move existing slots that come after the newly inserted slots
114743 ** out of the way */
114744 for(i=pSrc->nSrc-1; i>=iStart; i--){
114745 pSrc->a[i+nExtra] = pSrc->a[i];
114746 }
114747 pSrc->nSrc += nExtra;
114748
114749 /* Zero the newly allocated slots */
114750 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
114751 for(i=iStart; i<iStart+nExtra; i++){
114752 pSrc->a[i].iCursor = -1;
114753 }
114754
114755 /* Return a pointer to the enlarged SrcList */
114756 return pSrc;
114757}
114758
114759
114760/*
114761** Append a new table name to the given SrcList. Create a new SrcList if
114762** need be. A new entry is created in the SrcList even if pTable is NULL.
114763**
114764** A SrcList is returned, or NULL if there is an OOM error or if the
114765** SrcList grows to large. The returned
114766** SrcList might be the same as the SrcList that was input or it might be
114767** a new one. If an OOM error does occurs, then the prior value of pList
114768** that is input to this routine is automatically freed.
114769**
114770** If pDatabase is not null, it means that the table has an optional
114771** database name prefix. Like this: "database.table". The pDatabase
114772** points to the table name and the pTable points to the database name.
114773** The SrcList.a[].zName field is filled with the table name which might
114774** come from pTable (if pDatabase is NULL) or from pDatabase.
114775** SrcList.a[].zDatabase is filled with the database name from pTable,
114776** or with NULL if no database is specified.
114777**
114778** In other words, if call like this:
114779**
114780** sqlite3SrcListAppend(D,A,B,0);
114781**
114782** Then B is a table name and the database name is unspecified. If called
114783** like this:
114784**
114785** sqlite3SrcListAppend(D,A,B,C);
114786**
114787** Then C is the table name and B is the database name. If C is defined
114788** then so is B. In other words, we never have a case where:
114789**
114790** sqlite3SrcListAppend(D,A,0,C);
114791**
114792** Both pTable and pDatabase are assumed to be quoted. They are dequoted
114793** before being added to the SrcList.
114794*/
114795SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
114796 Parse *pParse, /* Parsing context, in which errors are reported */
114797 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
114798 Token *pTable, /* Table to append */
114799 Token *pDatabase /* Database of the table */
114800){
114801 struct SrcList_item *pItem;
114802 sqlite3 *db;
114803 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
114804 assert( pParse!=0 );
114805 assert( pParse->db!=0 );
114806 db = pParse->db;
114807 if( pList==0 ){
114808 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
114809 if( pList==0 ) return 0;
114810 pList->nAlloc = 1;
114811 pList->nSrc = 1;
114812 memset(&pList->a[0], 0, sizeof(pList->a[0]));
114813 pList->a[0].iCursor = -1;
114814 }else{
114815 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
114816 if( pNew==0 ){
114817 sqlite3SrcListDelete(db, pList);
114818 return 0;
114819 }else{
114820 pList = pNew;
114821 }
114822 }
114823 pItem = &pList->a[pList->nSrc-1];
114824 if( pDatabase && pDatabase->z==0 ){
114825 pDatabase = 0;
114826 }
114827 if( pDatabase ){
114828 pItem->zName = sqlite3NameFromToken(db, pDatabase);
114829 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
114830 }else{
114831 pItem->zName = sqlite3NameFromToken(db, pTable);
114832 pItem->zDatabase = 0;
114833 }
114834 return pList;
114835}
114836
114837/*
114838** Assign VdbeCursor index numbers to all tables in a SrcList
114839*/
114840SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
114841 int i;
114842 struct SrcList_item *pItem;
114843 assert(pList || pParse->db->mallocFailed );
114844 if( pList ){
114845 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
114846 if( pItem->iCursor>=0 ) break;
114847 pItem->iCursor = pParse->nTab++;
114848 if( pItem->pSelect ){
114849 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
114850 }
114851 }
114852 }
114853}
114854
114855/*
114856** Delete an entire SrcList including all its substructure.
114857*/
114858SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
114859 int i;
114860 struct SrcList_item *pItem;
114861 if( pList==0 ) return;
114862 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
114863 sqlite3DbFree(db, pItem->zDatabase);
114864 sqlite3DbFree(db, pItem->zName);
114865 sqlite3DbFree(db, pItem->zAlias);
114866 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
114867 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
114868 sqlite3DeleteTable(db, pItem->pTab);
114869 sqlite3SelectDelete(db, pItem->pSelect);
114870 sqlite3ExprDelete(db, pItem->pOn);
114871 sqlite3IdListDelete(db, pItem->pUsing);
114872 }
114873 sqlite3DbFreeNN(db, pList);
114874}
114875
114876/*
114877** This routine is called by the parser to add a new term to the
114878** end of a growing FROM clause. The "p" parameter is the part of
114879** the FROM clause that has already been constructed. "p" is NULL
114880** if this is the first term of the FROM clause. pTable and pDatabase
114881** are the name of the table and database named in the FROM clause term.
114882** pDatabase is NULL if the database name qualifier is missing - the
114883** usual case. If the term has an alias, then pAlias points to the
114884** alias token. If the term is a subquery, then pSubquery is the
114885** SELECT statement that the subquery encodes. The pTable and
114886** pDatabase parameters are NULL for subqueries. The pOn and pUsing
114887** parameters are the content of the ON and USING clauses.
114888**
114889** Return a new SrcList which encodes is the FROM with the new
114890** term added.
114891*/
114892SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
114893 Parse *pParse, /* Parsing context */
114894 SrcList *p, /* The left part of the FROM clause already seen */
114895 Token *pTable, /* Name of the table to add to the FROM clause */
114896 Token *pDatabase, /* Name of the database containing pTable */
114897 Token *pAlias, /* The right-hand side of the AS subexpression */
114898 Select *pSubquery, /* A subquery used in place of a table name */
114899 Expr *pOn, /* The ON clause of a join */
114900 IdList *pUsing /* The USING clause of a join */
114901){
114902 struct SrcList_item *pItem;
114903 sqlite3 *db = pParse->db;
114904 if( !p && (pOn || pUsing) ){
114905 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
114906 (pOn ? "ON" : "USING")
114907 );
114908 goto append_from_error;
114909 }
114910 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
114911 if( p==0 ){
114912 goto append_from_error;
114913 }
114914 assert( p->nSrc>0 );
114915 pItem = &p->a[p->nSrc-1];
114916 assert( (pTable==0)==(pDatabase==0) );
114917 assert( pItem->zName==0 || pDatabase!=0 );
114918 if( IN_RENAME_OBJECT && pItem->zName ){
114919 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
114920 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
114921 }
114922 assert( pAlias!=0 );
114923 if( pAlias->n ){
114924 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
114925 }
114926 pItem->pSelect = pSubquery;
114927 pItem->pOn = pOn;
114928 pItem->pUsing = pUsing;
114929 return p;
114930
114931 append_from_error:
114932 assert( p==0 );
114933 sqlite3ExprDelete(db, pOn);
114934 sqlite3IdListDelete(db, pUsing);
114935 sqlite3SelectDelete(db, pSubquery);
114936 return 0;
114937}
114938
114939/*
114940** Add an INDEXED BY or NOT INDEXED clause to the most recently added
114941** element of the source-list passed as the second argument.
114942*/
114943SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
114944 assert( pIndexedBy!=0 );
114945 if( p && pIndexedBy->n>0 ){
114946 struct SrcList_item *pItem;
114947 assert( p->nSrc>0 );
114948 pItem = &p->a[p->nSrc-1];
114949 assert( pItem->fg.notIndexed==0 );
114950 assert( pItem->fg.isIndexedBy==0 );
114951 assert( pItem->fg.isTabFunc==0 );
114952 if( pIndexedBy->n==1 && !pIndexedBy->z ){
114953 /* A "NOT INDEXED" clause was supplied. See parse.y
114954 ** construct "indexed_opt" for details. */
114955 pItem->fg.notIndexed = 1;
114956 }else{
114957 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
114958 pItem->fg.isIndexedBy = 1;
114959 }
114960 }
114961}
114962
114963/*
114964** Append the contents of SrcList p2 to SrcList p1 and return the resulting
114965** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
114966** are deleted by this function.
114967*/
114968SQLITE_PRIVATE SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
114969 assert( p1 && p1->nSrc==1 );
114970 if( p2 ){
114971 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
114972 if( pNew==0 ){
114973 sqlite3SrcListDelete(pParse->db, p2);
114974 }else{
114975 p1 = pNew;
114976 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(struct SrcList_item));
114977 sqlite3DbFree(pParse->db, p2);
114978 }
114979 }
114980 return p1;
114981}
114982
114983/*
114984** Add the list of function arguments to the SrcList entry for a
114985** table-valued-function.
114986*/
114987SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
114988 if( p ){
114989 struct SrcList_item *pItem = &p->a[p->nSrc-1];
114990 assert( pItem->fg.notIndexed==0 );
114991 assert( pItem->fg.isIndexedBy==0 );
114992 assert( pItem->fg.isTabFunc==0 );
114993 pItem->u1.pFuncArg = pList;
114994 pItem->fg.isTabFunc = 1;
114995 }else{
114996 sqlite3ExprListDelete(pParse->db, pList);
114997 }
114998}
114999
115000/*
115001** When building up a FROM clause in the parser, the join operator
115002** is initially attached to the left operand. But the code generator
115003** expects the join operator to be on the right operand. This routine
115004** Shifts all join operators from left to right for an entire FROM
115005** clause.
115006**
115007** Example: Suppose the join is like this:
115008**
115009** A natural cross join B
115010**
115011** The operator is "natural cross join". The A and B operands are stored
115012** in p->a[0] and p->a[1], respectively. The parser initially stores the
115013** operator with A. This routine shifts that operator over to B.
115014*/
115015SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
115016 if( p ){
115017 int i;
115018 for(i=p->nSrc-1; i>0; i--){
115019 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
115020 }
115021 p->a[0].fg.jointype = 0;
115022 }
115023}
115024
115025/*
115026** Generate VDBE code for a BEGIN statement.
115027*/
115028SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
115029 sqlite3 *db;
115030 Vdbe *v;
115031 int i;
115032
115033 assert( pParse!=0 );
115034 db = pParse->db;
115035 assert( db!=0 );
115036 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
115037 return;
115038 }
115039 v = sqlite3GetVdbe(pParse);
115040 if( !v ) return;
115041 if( type!=TK_DEFERRED ){
115042 for(i=0; i<db->nDb; i++){
115043 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
115044 sqlite3VdbeUsesBtree(v, i);
115045 }
115046 }
115047 sqlite3VdbeAddOp0(v, OP_AutoCommit);
115048}
115049
115050/*
115051** Generate VDBE code for a COMMIT or ROLLBACK statement.
115052** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
115053** code is generated for a COMMIT.
115054*/
115055SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
115056 Vdbe *v;
115057 int isRollback;
115058
115059 assert( pParse!=0 );
115060 assert( pParse->db!=0 );
115061 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
115062 isRollback = eType==TK_ROLLBACK;
115063 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
115064 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
115065 return;
115066 }
115067 v = sqlite3GetVdbe(pParse);
115068 if( v ){
115069 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
115070 }
115071}
115072
115073/*
115074** This function is called by the parser when it parses a command to create,
115075** release or rollback an SQL savepoint.
115076*/
115077SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
115078 char *zName = sqlite3NameFromToken(pParse->db, pName);
115079 if( zName ){
115080 Vdbe *v = sqlite3GetVdbe(pParse);
115081#ifndef SQLITE_OMIT_AUTHORIZATION
115082 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
115083 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
115084#endif
115085 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
115086 sqlite3DbFree(pParse->db, zName);
115087 return;
115088 }
115089 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
115090 }
115091}
115092
115093/*
115094** Make sure the TEMP database is open and available for use. Return
115095** the number of errors. Leave any error messages in the pParse structure.
115096*/
115097SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
115098 sqlite3 *db = pParse->db;
115099 if( db->aDb[1].pBt==0 && !pParse->explain ){
115100 int rc;
115101 Btree *pBt;
115102 static const int flags =
115103 SQLITE_OPEN_READWRITE |
115104 SQLITE_OPEN_CREATE |
115105 SQLITE_OPEN_EXCLUSIVE |
115106 SQLITE_OPEN_DELETEONCLOSE |
115107 SQLITE_OPEN_TEMP_DB;
115108
115109 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
115110 if( rc!=SQLITE_OK ){
115111 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
115112 "file for storing temporary tables");
115113 pParse->rc = rc;
115114 return 1;
115115 }
115116 db->aDb[1].pBt = pBt;
115117 assert( db->aDb[1].pSchema );
115118 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
115119 sqlite3OomFault(db);
115120 return 1;
115121 }
115122 }
115123 return 0;
115124}
115125
115126/*
115127** Record the fact that the schema cookie will need to be verified
115128** for database iDb. The code to actually verify the schema cookie
115129** will occur at the end of the top-level VDBE and will be generated
115130** later, by sqlite3FinishCoding().
115131*/
115132SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
115133 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115134
115135 assert( iDb>=0 && iDb<pParse->db->nDb );
115136 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
115137 assert( iDb<SQLITE_MAX_ATTACHED+2 );
115138 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
115139 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
115140 DbMaskSet(pToplevel->cookieMask, iDb);
115141 if( !OMIT_TEMPDB && iDb==1 ){
115142 sqlite3OpenTempDatabase(pToplevel);
115143 }
115144 }
115145}
115146
115147/*
115148** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
115149** attached database. Otherwise, invoke it for the database named zDb only.
115150*/
115151SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
115152 sqlite3 *db = pParse->db;
115153 int i;
115154 for(i=0; i<db->nDb; i++){
115155 Db *pDb = &db->aDb[i];
115156 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
115157 sqlite3CodeVerifySchema(pParse, i);
115158 }
115159 }
115160}
115161
115162/*
115163** Generate VDBE code that prepares for doing an operation that
115164** might change the database.
115165**
115166** This routine starts a new transaction if we are not already within
115167** a transaction. If we are already within a transaction, then a checkpoint
115168** is set if the setStatement parameter is true. A checkpoint should
115169** be set for operations that might fail (due to a constraint) part of
115170** the way through and which will need to undo some writes without having to
115171** rollback the whole transaction. For operations where all constraints
115172** can be checked before any changes are made to the database, it is never
115173** necessary to undo a write and the checkpoint should not be set.
115174*/
115175SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
115176 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115177 sqlite3CodeVerifySchema(pParse, iDb);
115178 DbMaskSet(pToplevel->writeMask, iDb);
115179 pToplevel->isMultiWrite |= setStatement;
115180}
115181
115182/*
115183** Indicate that the statement currently under construction might write
115184** more than one entry (example: deleting one row then inserting another,
115185** inserting multiple rows in a table, or inserting a row and index entries.)
115186** If an abort occurs after some of these writes have completed, then it will
115187** be necessary to undo the completed writes.
115188*/
115189SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
115190 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115191 pToplevel->isMultiWrite = 1;
115192}
115193
115194/*
115195** The code generator calls this routine if is discovers that it is
115196** possible to abort a statement prior to completion. In order to
115197** perform this abort without corrupting the database, we need to make
115198** sure that the statement is protected by a statement transaction.
115199**
115200** Technically, we only need to set the mayAbort flag if the
115201** isMultiWrite flag was previously set. There is a time dependency
115202** such that the abort must occur after the multiwrite. This makes
115203** some statements involving the REPLACE conflict resolution algorithm
115204** go a little faster. But taking advantage of this time dependency
115205** makes it more difficult to prove that the code is correct (in
115206** particular, it prevents us from writing an effective
115207** implementation of sqlite3AssertMayAbort()) and so we have chosen
115208** to take the safe route and skip the optimization.
115209*/
115210SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
115211 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115212 pToplevel->mayAbort = 1;
115213}
115214
115215/*
115216** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
115217** error. The onError parameter determines which (if any) of the statement
115218** and/or current transaction is rolled back.
115219*/
115220SQLITE_PRIVATE void sqlite3HaltConstraint(
115221 Parse *pParse, /* Parsing context */
115222 int errCode, /* extended error code */
115223 int onError, /* Constraint type */
115224 char *p4, /* Error message */
115225 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
115226 u8 p5Errmsg /* P5_ErrMsg type */
115227){
115228 Vdbe *v = sqlite3GetVdbe(pParse);
115229 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
115230 if( onError==OE_Abort ){
115231 sqlite3MayAbort(pParse);
115232 }
115233 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
115234 sqlite3VdbeChangeP5(v, p5Errmsg);
115235}
115236
115237/*
115238** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
115239*/
115240SQLITE_PRIVATE void sqlite3UniqueConstraint(
115241 Parse *pParse, /* Parsing context */
115242 int onError, /* Constraint type */
115243 Index *pIdx /* The index that triggers the constraint */
115244){
115245 char *zErr;
115246 int j;
115247 StrAccum errMsg;
115248 Table *pTab = pIdx->pTable;
115249
115250 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
115251 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
115252 if( pIdx->aColExpr ){
115253 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
115254 }else{
115255 for(j=0; j<pIdx->nKeyCol; j++){
115256 char *zCol;
115257 assert( pIdx->aiColumn[j]>=0 );
115258 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
115259 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
115260 sqlite3_str_appendall(&errMsg, pTab->zName);
115261 sqlite3_str_append(&errMsg, ".", 1);
115262 sqlite3_str_appendall(&errMsg, zCol);
115263 }
115264 }
115265 zErr = sqlite3StrAccumFinish(&errMsg);
115266 sqlite3HaltConstraint(pParse,
115267 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
115268 : SQLITE_CONSTRAINT_UNIQUE,
115269 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
115270}
115271
115272
115273/*
115274** Code an OP_Halt due to non-unique rowid.
115275*/
115276SQLITE_PRIVATE void sqlite3RowidConstraint(
115277 Parse *pParse, /* Parsing context */
115278 int onError, /* Conflict resolution algorithm */
115279 Table *pTab /* The table with the non-unique rowid */
115280){
115281 char *zMsg;
115282 int rc;
115283 if( pTab->iPKey>=0 ){
115284 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
115285 pTab->aCol[pTab->iPKey].zName);
115286 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
115287 }else{
115288 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
115289 rc = SQLITE_CONSTRAINT_ROWID;
115290 }
115291 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
115292 P5_ConstraintUnique);
115293}
115294
115295/*
115296** Check to see if pIndex uses the collating sequence pColl. Return
115297** true if it does and false if it does not.
115298*/
115299#ifndef SQLITE_OMIT_REINDEX
115300static int collationMatch(const char *zColl, Index *pIndex){
115301 int i;
115302 assert( zColl!=0 );
115303 for(i=0; i<pIndex->nColumn; i++){
115304 const char *z = pIndex->azColl[i];
115305 assert( z!=0 || pIndex->aiColumn[i]<0 );
115306 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
115307 return 1;
115308 }
115309 }
115310 return 0;
115311}
115312#endif
115313
115314/*
115315** Recompute all indices of pTab that use the collating sequence pColl.
115316** If pColl==0 then recompute all indices of pTab.
115317*/
115318#ifndef SQLITE_OMIT_REINDEX
115319static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
115320 if( !IsVirtual(pTab) ){
115321 Index *pIndex; /* An index associated with pTab */
115322
115323 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
115324 if( zColl==0 || collationMatch(zColl, pIndex) ){
115325 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
115326 sqlite3BeginWriteOperation(pParse, 0, iDb);
115327 sqlite3RefillIndex(pParse, pIndex, -1);
115328 }
115329 }
115330 }
115331}
115332#endif
115333
115334/*
115335** Recompute all indices of all tables in all databases where the
115336** indices use the collating sequence pColl. If pColl==0 then recompute
115337** all indices everywhere.
115338*/
115339#ifndef SQLITE_OMIT_REINDEX
115340static void reindexDatabases(Parse *pParse, char const *zColl){
115341 Db *pDb; /* A single database */
115342 int iDb; /* The database index number */
115343 sqlite3 *db = pParse->db; /* The database connection */
115344 HashElem *k; /* For looping over tables in pDb */
115345 Table *pTab; /* A table in the database */
115346
115347 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
115348 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
115349 assert( pDb!=0 );
115350 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
115351 pTab = (Table*)sqliteHashData(k);
115352 reindexTable(pParse, pTab, zColl);
115353 }
115354 }
115355}
115356#endif
115357
115358/*
115359** Generate code for the REINDEX command.
115360**
115361** REINDEX -- 1
115362** REINDEX <collation> -- 2
115363** REINDEX ?<database>.?<tablename> -- 3
115364** REINDEX ?<database>.?<indexname> -- 4
115365**
115366** Form 1 causes all indices in all attached databases to be rebuilt.
115367** Form 2 rebuilds all indices in all databases that use the named
115368** collating function. Forms 3 and 4 rebuild the named index or all
115369** indices associated with the named table.
115370*/
115371#ifndef SQLITE_OMIT_REINDEX
115372SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
115373 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
115374 char *z; /* Name of a table or index */
115375 const char *zDb; /* Name of the database */
115376 Table *pTab; /* A table in the database */
115377 Index *pIndex; /* An index associated with pTab */
115378 int iDb; /* The database index number */
115379 sqlite3 *db = pParse->db; /* The database connection */
115380 Token *pObjName; /* Name of the table or index to be reindexed */
115381
115382 /* Read the database schema. If an error occurs, leave an error message
115383 ** and code in pParse and return NULL. */
115384 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
115385 return;
115386 }
115387
115388 if( pName1==0 ){
115389 reindexDatabases(pParse, 0);
115390 return;
115391 }else if( NEVER(pName2==0) || pName2->z==0 ){
115392 char *zColl;
115393 assert( pName1->z );
115394 zColl = sqlite3NameFromToken(pParse->db, pName1);
115395 if( !zColl ) return;
115396 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
115397 if( pColl ){
115398 reindexDatabases(pParse, zColl);
115399 sqlite3DbFree(db, zColl);
115400 return;
115401 }
115402 sqlite3DbFree(db, zColl);
115403 }
115404 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
115405 if( iDb<0 ) return;
115406 z = sqlite3NameFromToken(db, pObjName);
115407 if( z==0 ) return;
115408 zDb = db->aDb[iDb].zDbSName;
115409 pTab = sqlite3FindTable(db, z, zDb);
115410 if( pTab ){
115411 reindexTable(pParse, pTab, 0);
115412 sqlite3DbFree(db, z);
115413 return;
115414 }
115415 pIndex = sqlite3FindIndex(db, z, zDb);
115416 sqlite3DbFree(db, z);
115417 if( pIndex ){
115418 sqlite3BeginWriteOperation(pParse, 0, iDb);
115419 sqlite3RefillIndex(pParse, pIndex, -1);
115420 return;
115421 }
115422 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
115423}
115424#endif
115425
115426/*
115427** Return a KeyInfo structure that is appropriate for the given Index.
115428**
115429** The caller should invoke sqlite3KeyInfoUnref() on the returned object
115430** when it has finished using it.
115431*/
115432SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
115433 int i;
115434 int nCol = pIdx->nColumn;
115435 int nKey = pIdx->nKeyCol;
115436 KeyInfo *pKey;
115437 if( pParse->nErr ) return 0;
115438 if( pIdx->uniqNotNull ){
115439 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
115440 }else{
115441 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
115442 }
115443 if( pKey ){
115444 assert( sqlite3KeyInfoIsWriteable(pKey) );
115445 for(i=0; i<nCol; i++){
115446 const char *zColl = pIdx->azColl[i];
115447 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
115448 sqlite3LocateCollSeq(pParse, zColl);
115449 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
115450 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
115451 }
115452 if( pParse->nErr ){
115453 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
115454 if( pIdx->bNoQuery==0 ){
115455 /* Deactivate the index because it contains an unknown collating
115456 ** sequence. The only way to reactive the index is to reload the
115457 ** schema. Adding the missing collating sequence later does not
115458 ** reactive the index. The application had the chance to register
115459 ** the missing index using the collation-needed callback. For
115460 ** simplicity, SQLite will not give the application a second chance.
115461 */
115462 pIdx->bNoQuery = 1;
115463 pParse->rc = SQLITE_ERROR_RETRY;
115464 }
115465 sqlite3KeyInfoUnref(pKey);
115466 pKey = 0;
115467 }
115468 }
115469 return pKey;
115470}
115471
115472#ifndef SQLITE_OMIT_CTE
115473/*
115474** This routine is invoked once per CTE by the parser while parsing a
115475** WITH clause.
115476*/
115477SQLITE_PRIVATE With *sqlite3WithAdd(
115478 Parse *pParse, /* Parsing context */
115479 With *pWith, /* Existing WITH clause, or NULL */
115480 Token *pName, /* Name of the common-table */
115481 ExprList *pArglist, /* Optional column name list for the table */
115482 Select *pQuery /* Query used to initialize the table */
115483){
115484 sqlite3 *db = pParse->db;
115485 With *pNew;
115486 char *zName;
115487
115488 /* Check that the CTE name is unique within this WITH clause. If
115489 ** not, store an error in the Parse structure. */
115490 zName = sqlite3NameFromToken(pParse->db, pName);
115491 if( zName && pWith ){
115492 int i;
115493 for(i=0; i<pWith->nCte; i++){
115494 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
115495 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
115496 }
115497 }
115498 }
115499
115500 if( pWith ){
115501 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
115502 pNew = sqlite3DbRealloc(db, pWith, nByte);
115503 }else{
115504 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
115505 }
115506 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
115507
115508 if( db->mallocFailed ){
115509 sqlite3ExprListDelete(db, pArglist);
115510 sqlite3SelectDelete(db, pQuery);
115511 sqlite3DbFree(db, zName);
115512 pNew = pWith;
115513 }else{
115514 pNew->a[pNew->nCte].pSelect = pQuery;
115515 pNew->a[pNew->nCte].pCols = pArglist;
115516 pNew->a[pNew->nCte].zName = zName;
115517 pNew->a[pNew->nCte].zCteErr = 0;
115518 pNew->nCte++;
115519 }
115520
115521 return pNew;
115522}
115523
115524/*
115525** Free the contents of the With object passed as the second argument.
115526*/
115527SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
115528 if( pWith ){
115529 int i;
115530 for(i=0; i<pWith->nCte; i++){
115531 struct Cte *pCte = &pWith->a[i];
115532 sqlite3ExprListDelete(db, pCte->pCols);
115533 sqlite3SelectDelete(db, pCte->pSelect);
115534 sqlite3DbFree(db, pCte->zName);
115535 }
115536 sqlite3DbFree(db, pWith);
115537 }
115538}
115539#endif /* !defined(SQLITE_OMIT_CTE) */
115540
115541/************** End of build.c ***********************************************/
115542/************** Begin file callback.c ****************************************/
115543/*
115544** 2005 May 23
115545**
115546** The author disclaims copyright to this source code. In place of
115547** a legal notice, here is a blessing:
115548**
115549** May you do good and not evil.
115550** May you find forgiveness for yourself and forgive others.
115551** May you share freely, never taking more than you give.
115552**
115553*************************************************************************
115554**
115555** This file contains functions used to access the internal hash tables
115556** of user defined functions and collation sequences.
115557*/
115558
115559/* #include "sqliteInt.h" */
115560
115561/*
115562** Invoke the 'collation needed' callback to request a collation sequence
115563** in the encoding enc of name zName, length nName.
115564*/
115565static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
115566 assert( !db->xCollNeeded || !db->xCollNeeded16 );
115567 if( db->xCollNeeded ){
115568 char *zExternal = sqlite3DbStrDup(db, zName);
115569 if( !zExternal ) return;
115570 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
115571 sqlite3DbFree(db, zExternal);
115572 }
115573#ifndef SQLITE_OMIT_UTF16
115574 if( db->xCollNeeded16 ){
115575 char const *zExternal;
115576 sqlite3_value *pTmp = sqlite3ValueNew(db);
115577 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
115578 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
115579 if( zExternal ){
115580 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
115581 }
115582 sqlite3ValueFree(pTmp);
115583 }
115584#endif
115585}
115586
115587/*
115588** This routine is called if the collation factory fails to deliver a
115589** collation function in the best encoding but there may be other versions
115590** of this collation function (for other text encodings) available. Use one
115591** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
115592** possible.
115593*/
115594static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
115595 CollSeq *pColl2;
115596 char *z = pColl->zName;
115597 int i;
115598 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
115599 for(i=0; i<3; i++){
115600 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
115601 if( pColl2->xCmp!=0 ){
115602 memcpy(pColl, pColl2, sizeof(CollSeq));
115603 pColl->xDel = 0; /* Do not copy the destructor */
115604 return SQLITE_OK;
115605 }
115606 }
115607 return SQLITE_ERROR;
115608}
115609
115610/*
115611** This routine is called on a collation sequence before it is used to
115612** check that it is defined. An undefined collation sequence exists when
115613** a database is loaded that contains references to collation sequences
115614** that have not been defined by sqlite3_create_collation() etc.
115615**
115616** If required, this routine calls the 'collation needed' callback to
115617** request a definition of the collating sequence. If this doesn't work,
115618** an equivalent collating sequence that uses a text encoding different
115619** from the main database is substituted, if one is available.
115620*/
115621SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
115622 if( pColl && pColl->xCmp==0 ){
115623 const char *zName = pColl->zName;
115624 sqlite3 *db = pParse->db;
115625 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
115626 if( !p ){
115627 return SQLITE_ERROR;
115628 }
115629 assert( p==pColl );
115630 }
115631 return SQLITE_OK;
115632}
115633
115634
115635
115636/*
115637** Locate and return an entry from the db.aCollSeq hash table. If the entry
115638** specified by zName and nName is not found and parameter 'create' is
115639** true, then create a new entry. Otherwise return NULL.
115640**
115641** Each pointer stored in the sqlite3.aCollSeq hash table contains an
115642** array of three CollSeq structures. The first is the collation sequence
115643** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
115644**
115645** Stored immediately after the three collation sequences is a copy of
115646** the collation sequence name. A pointer to this string is stored in
115647** each collation sequence structure.
115648*/
115649static CollSeq *findCollSeqEntry(
115650 sqlite3 *db, /* Database connection */
115651 const char *zName, /* Name of the collating sequence */
115652 int create /* Create a new entry if true */
115653){
115654 CollSeq *pColl;
115655 pColl = sqlite3HashFind(&db->aCollSeq, zName);
115656
115657 if( 0==pColl && create ){
115658 int nName = sqlite3Strlen30(zName) + 1;
115659 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
115660 if( pColl ){
115661 CollSeq *pDel = 0;
115662 pColl[0].zName = (char*)&pColl[3];
115663 pColl[0].enc = SQLITE_UTF8;
115664 pColl[1].zName = (char*)&pColl[3];
115665 pColl[1].enc = SQLITE_UTF16LE;
115666 pColl[2].zName = (char*)&pColl[3];
115667 pColl[2].enc = SQLITE_UTF16BE;
115668 memcpy(pColl[0].zName, zName, nName);
115669 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
115670
115671 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
115672 ** return the pColl pointer to be deleted (because it wasn't added
115673 ** to the hash table).
115674 */
115675 assert( pDel==0 || pDel==pColl );
115676 if( pDel!=0 ){
115677 sqlite3OomFault(db);
115678 sqlite3DbFree(db, pDel);
115679 pColl = 0;
115680 }
115681 }
115682 }
115683 return pColl;
115684}
115685
115686/*
115687** Parameter zName points to a UTF-8 encoded string nName bytes long.
115688** Return the CollSeq* pointer for the collation sequence named zName
115689** for the encoding 'enc' from the database 'db'.
115690**
115691** If the entry specified is not found and 'create' is true, then create a
115692** new entry. Otherwise return NULL.
115693**
115694** A separate function sqlite3LocateCollSeq() is a wrapper around
115695** this routine. sqlite3LocateCollSeq() invokes the collation factory
115696** if necessary and generates an error message if the collating sequence
115697** cannot be found.
115698**
115699** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
115700*/
115701SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
115702 sqlite3 *db, /* Database connection to search */
115703 u8 enc, /* Desired text encoding */
115704 const char *zName, /* Name of the collating sequence. Might be NULL */
115705 int create /* True to create CollSeq if doesn't already exist */
115706){
115707 CollSeq *pColl;
115708 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
115709 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
115710 if( zName ){
115711 pColl = findCollSeqEntry(db, zName, create);
115712 if( pColl ) pColl += enc-1;
115713 }else{
115714 pColl = db->pDfltColl;
115715 }
115716 return pColl;
115717}
115718
115719/*
115720** Change the text encoding for a database connection. This means that
115721** the pDfltColl must change as well.
115722*/
115723SQLITE_PRIVATE void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
115724 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
115725 db->enc = enc;
115726 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
115727 ** strings is BINARY.
115728 */
115729 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
115730}
115731
115732/*
115733** This function is responsible for invoking the collation factory callback
115734** or substituting a collation sequence of a different encoding when the
115735** requested collation sequence is not available in the desired encoding.
115736**
115737** If it is not NULL, then pColl must point to the database native encoding
115738** collation sequence with name zName, length nName.
115739**
115740** The return value is either the collation sequence to be used in database
115741** db for collation type name zName, length nName, or NULL, if no collation
115742** sequence can be found. If no collation is found, leave an error message.
115743**
115744** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
115745*/
115746SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
115747 Parse *pParse, /* Parsing context */
115748 u8 enc, /* The desired encoding for the collating sequence */
115749 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
115750 const char *zName /* Collating sequence name */
115751){
115752 CollSeq *p;
115753 sqlite3 *db = pParse->db;
115754
115755 p = pColl;
115756 if( !p ){
115757 p = sqlite3FindCollSeq(db, enc, zName, 0);
115758 }
115759 if( !p || !p->xCmp ){
115760 /* No collation sequence of this type for this encoding is registered.
115761 ** Call the collation factory to see if it can supply us with one.
115762 */
115763 callCollNeeded(db, enc, zName);
115764 p = sqlite3FindCollSeq(db, enc, zName, 0);
115765 }
115766 if( p && !p->xCmp && synthCollSeq(db, p) ){
115767 p = 0;
115768 }
115769 assert( !p || p->xCmp );
115770 if( p==0 ){
115771 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
115772 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
115773 }
115774 return p;
115775}
115776
115777/*
115778** This function returns the collation sequence for database native text
115779** encoding identified by the string zName.
115780**
115781** If the requested collation sequence is not available, or not available
115782** in the database native encoding, the collation factory is invoked to
115783** request it. If the collation factory does not supply such a sequence,
115784** and the sequence is available in another text encoding, then that is
115785** returned instead.
115786**
115787** If no versions of the requested collations sequence are available, or
115788** another error occurs, NULL is returned and an error message written into
115789** pParse.
115790**
115791** This routine is a wrapper around sqlite3FindCollSeq(). This routine
115792** invokes the collation factory if the named collation cannot be found
115793** and generates an error message.
115794**
115795** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
115796*/
115797SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
115798 sqlite3 *db = pParse->db;
115799 u8 enc = ENC(db);
115800 u8 initbusy = db->init.busy;
115801 CollSeq *pColl;
115802
115803 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
115804 if( !initbusy && (!pColl || !pColl->xCmp) ){
115805 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
115806 }
115807
115808 return pColl;
115809}
115810
115811/* During the search for the best function definition, this procedure
115812** is called to test how well the function passed as the first argument
115813** matches the request for a function with nArg arguments in a system
115814** that uses encoding enc. The value returned indicates how well the
115815** request is matched. A higher value indicates a better match.
115816**
115817** If nArg is -1 that means to only return a match (non-zero) if p->nArg
115818** is also -1. In other words, we are searching for a function that
115819** takes a variable number of arguments.
115820**
115821** If nArg is -2 that means that we are searching for any function
115822** regardless of the number of arguments it uses, so return a positive
115823** match score for any
115824**
115825** The returned value is always between 0 and 6, as follows:
115826**
115827** 0: Not a match.
115828** 1: UTF8/16 conversion required and function takes any number of arguments.
115829** 2: UTF16 byte order change required and function takes any number of args.
115830** 3: encoding matches and function takes any number of arguments
115831** 4: UTF8/16 conversion required - argument count matches exactly
115832** 5: UTF16 byte order conversion required - argument count matches exactly
115833** 6: Perfect match: encoding and argument count match exactly.
115834**
115835** If nArg==(-2) then any function with a non-null xSFunc is
115836** a perfect match and any function with xSFunc NULL is
115837** a non-match.
115838*/
115839#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
115840static int matchQuality(
115841 FuncDef *p, /* The function we are evaluating for match quality */
115842 int nArg, /* Desired number of arguments. (-1)==any */
115843 u8 enc /* Desired text encoding */
115844){
115845 int match;
115846 assert( p->nArg>=-1 );
115847
115848 /* Wrong number of arguments means "no match" */
115849 if( p->nArg!=nArg ){
115850 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
115851 if( p->nArg>=0 ) return 0;
115852 }
115853
115854 /* Give a better score to a function with a specific number of arguments
115855 ** than to function that accepts any number of arguments. */
115856 if( p->nArg==nArg ){
115857 match = 4;
115858 }else{
115859 match = 1;
115860 }
115861
115862 /* Bonus points if the text encoding matches */
115863 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
115864 match += 2; /* Exact encoding match */
115865 }else if( (enc & p->funcFlags & 2)!=0 ){
115866 match += 1; /* Both are UTF16, but with different byte orders */
115867 }
115868
115869 return match;
115870}
115871
115872/*
115873** Search a FuncDefHash for a function with the given name. Return
115874** a pointer to the matching FuncDef if found, or 0 if there is no match.
115875*/
115876SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(
115877 int h, /* Hash of the name */
115878 const char *zFunc /* Name of function */
115879){
115880 FuncDef *p;
115881 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
115882 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
115883 return p;
115884 }
115885 }
115886 return 0;
115887}
115888
115889/*
115890** Insert a new FuncDef into a FuncDefHash hash table.
115891*/
115892SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
115893 FuncDef *aDef, /* List of global functions to be inserted */
115894 int nDef /* Length of the apDef[] list */
115895){
115896 int i;
115897 for(i=0; i<nDef; i++){
115898 FuncDef *pOther;
115899 const char *zName = aDef[i].zName;
115900 int nName = sqlite3Strlen30(zName);
115901 int h = SQLITE_FUNC_HASH(zName[0], nName);
115902 assert( zName[0]>='a' && zName[0]<='z' );
115903 pOther = sqlite3FunctionSearch(h, zName);
115904 if( pOther ){
115905 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
115906 aDef[i].pNext = pOther->pNext;
115907 pOther->pNext = &aDef[i];
115908 }else{
115909 aDef[i].pNext = 0;
115910 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
115911 sqlite3BuiltinFunctions.a[h] = &aDef[i];
115912 }
115913 }
115914}
115915
115916
115917
115918/*
115919** Locate a user function given a name, a number of arguments and a flag
115920** indicating whether the function prefers UTF-16 over UTF-8. Return a
115921** pointer to the FuncDef structure that defines that function, or return
115922** NULL if the function does not exist.
115923**
115924** If the createFlag argument is true, then a new (blank) FuncDef
115925** structure is created and liked into the "db" structure if a
115926** no matching function previously existed.
115927**
115928** If nArg is -2, then the first valid function found is returned. A
115929** function is valid if xSFunc is non-zero. The nArg==(-2)
115930** case is used to see if zName is a valid function name for some number
115931** of arguments. If nArg is -2, then createFlag must be 0.
115932**
115933** If createFlag is false, then a function with the required name and
115934** number of arguments may be returned even if the eTextRep flag does not
115935** match that requested.
115936*/
115937SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
115938 sqlite3 *db, /* An open database */
115939 const char *zName, /* Name of the function. zero-terminated */
115940 int nArg, /* Number of arguments. -1 means any number */
115941 u8 enc, /* Preferred text encoding */
115942 u8 createFlag /* Create new entry if true and does not otherwise exist */
115943){
115944 FuncDef *p; /* Iterator variable */
115945 FuncDef *pBest = 0; /* Best match found so far */
115946 int bestScore = 0; /* Score of best match */
115947 int h; /* Hash value */
115948 int nName; /* Length of the name */
115949
115950 assert( nArg>=(-2) );
115951 assert( nArg>=(-1) || createFlag==0 );
115952 nName = sqlite3Strlen30(zName);
115953
115954 /* First search for a match amongst the application-defined functions.
115955 */
115956 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
115957 while( p ){
115958 int score = matchQuality(p, nArg, enc);
115959 if( score>bestScore ){
115960 pBest = p;
115961 bestScore = score;
115962 }
115963 p = p->pNext;
115964 }
115965
115966 /* If no match is found, search the built-in functions.
115967 **
115968 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
115969 ** functions even if a prior app-defined function was found. And give
115970 ** priority to built-in functions.
115971 **
115972 ** Except, if createFlag is true, that means that we are trying to
115973 ** install a new function. Whatever FuncDef structure is returned it will
115974 ** have fields overwritten with new information appropriate for the
115975 ** new function. But the FuncDefs for built-in functions are read-only.
115976 ** So we must not search for built-ins when creating a new function.
115977 */
115978 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
115979 bestScore = 0;
115980 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
115981 p = sqlite3FunctionSearch(h, zName);
115982 while( p ){
115983 int score = matchQuality(p, nArg, enc);
115984 if( score>bestScore ){
115985 pBest = p;
115986 bestScore = score;
115987 }
115988 p = p->pNext;
115989 }
115990 }
115991
115992 /* If the createFlag parameter is true and the search did not reveal an
115993 ** exact match for the name, number of arguments and encoding, then add a
115994 ** new entry to the hash table and return it.
115995 */
115996 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
115997 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
115998 FuncDef *pOther;
115999 u8 *z;
116000 pBest->zName = (const char*)&pBest[1];
116001 pBest->nArg = (u16)nArg;
116002 pBest->funcFlags = enc;
116003 memcpy((char*)&pBest[1], zName, nName+1);
116004 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
116005 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
116006 if( pOther==pBest ){
116007 sqlite3DbFree(db, pBest);
116008 sqlite3OomFault(db);
116009 return 0;
116010 }else{
116011 pBest->pNext = pOther;
116012 }
116013 }
116014
116015 if( pBest && (pBest->xSFunc || createFlag) ){
116016 return pBest;
116017 }
116018 return 0;
116019}
116020
116021/*
116022** Free all resources held by the schema structure. The void* argument points
116023** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
116024** pointer itself, it just cleans up subsidiary resources (i.e. the contents
116025** of the schema hash tables).
116026**
116027** The Schema.cache_size variable is not cleared.
116028*/
116029SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
116030 Hash temp1;
116031 Hash temp2;
116032 HashElem *pElem;
116033 Schema *pSchema = (Schema *)p;
116034
116035 temp1 = pSchema->tblHash;
116036 temp2 = pSchema->trigHash;
116037 sqlite3HashInit(&pSchema->trigHash);
116038 sqlite3HashClear(&pSchema->idxHash);
116039 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
116040 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
116041 }
116042 sqlite3HashClear(&temp2);
116043 sqlite3HashInit(&pSchema->tblHash);
116044 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
116045 Table *pTab = sqliteHashData(pElem);
116046 sqlite3DeleteTable(0, pTab);
116047 }
116048 sqlite3HashClear(&temp1);
116049 sqlite3HashClear(&pSchema->fkeyHash);
116050 pSchema->pSeqTab = 0;
116051 if( pSchema->schemaFlags & DB_SchemaLoaded ){
116052 pSchema->iGeneration++;
116053 }
116054 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
116055}
116056
116057/*
116058** Find and return the schema associated with a BTree. Create
116059** a new one if necessary.
116060*/
116061SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
116062 Schema * p;
116063 if( pBt ){
116064 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
116065 }else{
116066 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
116067 }
116068 if( !p ){
116069 sqlite3OomFault(db);
116070 }else if ( 0==p->file_format ){
116071 sqlite3HashInit(&p->tblHash);
116072 sqlite3HashInit(&p->idxHash);
116073 sqlite3HashInit(&p->trigHash);
116074 sqlite3HashInit(&p->fkeyHash);
116075 p->enc = SQLITE_UTF8;
116076 }
116077 return p;
116078}
116079
116080/************** End of callback.c ********************************************/
116081/************** Begin file delete.c ******************************************/
116082/*
116083** 2001 September 15
116084**
116085** The author disclaims copyright to this source code. In place of
116086** a legal notice, here is a blessing:
116087**
116088** May you do good and not evil.
116089** May you find forgiveness for yourself and forgive others.
116090** May you share freely, never taking more than you give.
116091**
116092*************************************************************************
116093** This file contains C code routines that are called by the parser
116094** in order to generate code for DELETE FROM statements.
116095*/
116096/* #include "sqliteInt.h" */
116097
116098/*
116099** While a SrcList can in general represent multiple tables and subqueries
116100** (as in the FROM clause of a SELECT statement) in this case it contains
116101** the name of a single table, as one might find in an INSERT, DELETE,
116102** or UPDATE statement. Look up that table in the symbol table and
116103** return a pointer. Set an error message and return NULL if the table
116104** name is not found or if any other error occurs.
116105**
116106** The following fields are initialized appropriate in pSrc:
116107**
116108** pSrc->a[0].pTab Pointer to the Table object
116109** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
116110**
116111*/
116112SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
116113 struct SrcList_item *pItem = pSrc->a;
116114 Table *pTab;
116115 assert( pItem && pSrc->nSrc>=1 );
116116 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
116117 sqlite3DeleteTable(pParse->db, pItem->pTab);
116118 pItem->pTab = pTab;
116119 if( pTab ){
116120 pTab->nTabRef++;
116121 }
116122 if( sqlite3IndexedByLookup(pParse, pItem) ){
116123 pTab = 0;
116124 }
116125 return pTab;
116126}
116127
116128/* Return true if table pTab is read-only.
116129**
116130** A table is read-only if any of the following are true:
116131**
116132** 1) It is a virtual table and no implementation of the xUpdate method
116133** has been provided
116134**
116135** 2) It is a system table (i.e. sqlite_schema), this call is not
116136** part of a nested parse and writable_schema pragma has not
116137** been specified
116138**
116139** 3) The table is a shadow table, the database connection is in
116140** defensive mode, and the current sqlite3_prepare()
116141** is for a top-level SQL statement.
116142*/
116143static int tabIsReadOnly(Parse *pParse, Table *pTab){
116144 sqlite3 *db;
116145 if( IsVirtual(pTab) ){
116146 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
116147 }
116148 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
116149 db = pParse->db;
116150 if( (pTab->tabFlags & TF_Readonly)!=0 ){
116151 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
116152 }
116153 assert( pTab->tabFlags & TF_Shadow );
116154 return sqlite3ReadOnlyShadowTables(db);
116155}
116156
116157/*
116158** Check to make sure the given table is writable. If it is not
116159** writable, generate an error message and return 1. If it is
116160** writable return 0;
116161*/
116162SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
116163 if( tabIsReadOnly(pParse, pTab) ){
116164 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
116165 return 1;
116166 }
116167#ifndef SQLITE_OMIT_VIEW
116168 if( !viewOk && pTab->pSelect ){
116169 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
116170 return 1;
116171 }
116172#endif
116173 return 0;
116174}
116175
116176
116177#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
116178/*
116179** Evaluate a view and store its result in an ephemeral table. The
116180** pWhere argument is an optional WHERE clause that restricts the
116181** set of rows in the view that are to be added to the ephemeral table.
116182*/
116183SQLITE_PRIVATE void sqlite3MaterializeView(
116184 Parse *pParse, /* Parsing context */
116185 Table *pView, /* View definition */
116186 Expr *pWhere, /* Optional WHERE clause to be added */
116187 ExprList *pOrderBy, /* Optional ORDER BY clause */
116188 Expr *pLimit, /* Optional LIMIT clause */
116189 int iCur /* Cursor number for ephemeral table */
116190){
116191 SelectDest dest;
116192 Select *pSel;
116193 SrcList *pFrom;
116194 sqlite3 *db = pParse->db;
116195 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
116196 pWhere = sqlite3ExprDup(db, pWhere, 0);
116197 pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
116198 if( pFrom ){
116199 assert( pFrom->nSrc==1 );
116200 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
116201 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
116202 assert( pFrom->a[0].pOn==0 );
116203 assert( pFrom->a[0].pUsing==0 );
116204 }
116205 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
116206 SF_IncludeHidden, pLimit);
116207 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
116208 sqlite3Select(pParse, pSel, &dest);
116209 sqlite3SelectDelete(db, pSel);
116210}
116211#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
116212
116213#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
116214/*
116215** Generate an expression tree to implement the WHERE, ORDER BY,
116216** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
116217**
116218** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
116219** \__________________________/
116220** pLimitWhere (pInClause)
116221*/
116222SQLITE_PRIVATE Expr *sqlite3LimitWhere(
116223 Parse *pParse, /* The parser context */
116224 SrcList *pSrc, /* the FROM clause -- which tables to scan */
116225 Expr *pWhere, /* The WHERE clause. May be null */
116226 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
116227 Expr *pLimit, /* The LIMIT clause. May be null */
116228 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
116229){
116230 sqlite3 *db = pParse->db;
116231 Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */
116232 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
116233 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
116234 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
116235 Select *pSelect = NULL; /* Complete SELECT tree */
116236 Table *pTab;
116237
116238 /* Check that there isn't an ORDER BY without a LIMIT clause.
116239 */
116240 if( pOrderBy && pLimit==0 ) {
116241 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
116242 sqlite3ExprDelete(pParse->db, pWhere);
116243 sqlite3ExprListDelete(pParse->db, pOrderBy);
116244 return 0;
116245 }
116246
116247 /* We only need to generate a select expression if there
116248 ** is a limit/offset term to enforce.
116249 */
116250 if( pLimit == 0 ) {
116251 return pWhere;
116252 }
116253
116254 /* Generate a select expression tree to enforce the limit/offset
116255 ** term for the DELETE or UPDATE statement. For example:
116256 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
116257 ** becomes:
116258 ** DELETE FROM table_a WHERE rowid IN (
116259 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
116260 ** );
116261 */
116262
116263 pTab = pSrc->a[0].pTab;
116264 if( HasRowid(pTab) ){
116265 pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
116266 pEList = sqlite3ExprListAppend(
116267 pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
116268 );
116269 }else{
116270 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
116271 if( pPk->nKeyCol==1 ){
116272 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
116273 pLhs = sqlite3Expr(db, TK_ID, zName);
116274 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
116275 }else{
116276 int i;
116277 for(i=0; i<pPk->nKeyCol; i++){
116278 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
116279 pEList = sqlite3ExprListAppend(pParse, pEList, p);
116280 }
116281 pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
116282 if( pLhs ){
116283 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
116284 }
116285 }
116286 }
116287
116288 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
116289 ** and the SELECT subtree. */
116290 pSrc->a[0].pTab = 0;
116291 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
116292 pSrc->a[0].pTab = pTab;
116293 pSrc->a[0].pIBIndex = 0;
116294
116295 /* generate the SELECT expression tree. */
116296 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
116297 pOrderBy,0,pLimit
116298 );
116299
116300 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
116301 pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
116302 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
116303 return pInClause;
116304}
116305#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
116306 /* && !defined(SQLITE_OMIT_SUBQUERY) */
116307
116308/*
116309** Generate code for a DELETE FROM statement.
116310**
116311** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
116312** \________/ \________________/
116313** pTabList pWhere
116314*/
116315SQLITE_PRIVATE void sqlite3DeleteFrom(
116316 Parse *pParse, /* The parser context */
116317 SrcList *pTabList, /* The table from which we should delete things */
116318 Expr *pWhere, /* The WHERE clause. May be null */
116319 ExprList *pOrderBy, /* ORDER BY clause. May be null */
116320 Expr *pLimit /* LIMIT clause. May be null */
116321){
116322 Vdbe *v; /* The virtual database engine */
116323 Table *pTab; /* The table from which records will be deleted */
116324 int i; /* Loop counter */
116325 WhereInfo *pWInfo; /* Information about the WHERE clause */
116326 Index *pIdx; /* For looping over indices of the table */
116327 int iTabCur; /* Cursor number for the table */
116328 int iDataCur = 0; /* VDBE cursor for the canonical data source */
116329 int iIdxCur = 0; /* Cursor number of the first index */
116330 int nIdx; /* Number of indices */
116331 sqlite3 *db; /* Main database structure */
116332 AuthContext sContext; /* Authorization context */
116333 NameContext sNC; /* Name context to resolve expressions in */
116334 int iDb; /* Database number */
116335 int memCnt = 0; /* Memory cell used for change counting */
116336 int rcauth; /* Value returned by authorization callback */
116337 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
116338 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
116339 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
116340 Index *pPk; /* The PRIMARY KEY index on the table */
116341 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
116342 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
116343 int iKey; /* Memory cell holding key of row to be deleted */
116344 i16 nKey; /* Number of memory cells in the row key */
116345 int iEphCur = 0; /* Ephemeral table holding all primary key values */
116346 int iRowSet = 0; /* Register for rowset of rows to delete */
116347 int addrBypass = 0; /* Address of jump over the delete logic */
116348 int addrLoop = 0; /* Top of the delete loop */
116349 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
116350 int bComplex; /* True if there are triggers or FKs or
116351 ** subqueries in the WHERE clause */
116352
116353#ifndef SQLITE_OMIT_TRIGGER
116354 int isView; /* True if attempting to delete from a view */
116355 Trigger *pTrigger; /* List of table triggers, if required */
116356#endif
116357
116358 memset(&sContext, 0, sizeof(sContext));
116359 db = pParse->db;
116360 if( pParse->nErr || db->mallocFailed ){
116361 goto delete_from_cleanup;
116362 }
116363 assert( pTabList->nSrc==1 );
116364
116365
116366 /* Locate the table which we want to delete. This table has to be
116367 ** put in an SrcList structure because some of the subroutines we
116368 ** will be calling are designed to work with multiple tables and expect
116369 ** an SrcList* parameter instead of just a Table* parameter.
116370 */
116371 pTab = sqlite3SrcListLookup(pParse, pTabList);
116372 if( pTab==0 ) goto delete_from_cleanup;
116373
116374 /* Figure out if we have any triggers and if the table being
116375 ** deleted from is a view
116376 */
116377#ifndef SQLITE_OMIT_TRIGGER
116378 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
116379 isView = pTab->pSelect!=0;
116380#else
116381# define pTrigger 0
116382# define isView 0
116383#endif
116384 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
116385#ifdef SQLITE_OMIT_VIEW
116386# undef isView
116387# define isView 0
116388#endif
116389
116390#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
116391 if( !isView ){
116392 pWhere = sqlite3LimitWhere(
116393 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
116394 );
116395 pOrderBy = 0;
116396 pLimit = 0;
116397 }
116398#endif
116399
116400 /* If pTab is really a view, make sure it has been initialized.
116401 */
116402 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
116403 goto delete_from_cleanup;
116404 }
116405
116406 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
116407 goto delete_from_cleanup;
116408 }
116409 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116410 assert( iDb<db->nDb );
116411 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
116412 db->aDb[iDb].zDbSName);
116413 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
116414 if( rcauth==SQLITE_DENY ){
116415 goto delete_from_cleanup;
116416 }
116417 assert(!isView || pTrigger);
116418
116419 /* Assign cursor numbers to the table and all its indices.
116420 */
116421 assert( pTabList->nSrc==1 );
116422 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
116423 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
116424 pParse->nTab++;
116425 }
116426
116427 /* Start the view context
116428 */
116429 if( isView ){
116430 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
116431 }
116432
116433 /* Begin generating code.
116434 */
116435 v = sqlite3GetVdbe(pParse);
116436 if( v==0 ){
116437 goto delete_from_cleanup;
116438 }
116439 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
116440 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
116441
116442 /* If we are trying to delete from a view, realize that view into
116443 ** an ephemeral table.
116444 */
116445#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
116446 if( isView ){
116447 sqlite3MaterializeView(pParse, pTab,
116448 pWhere, pOrderBy, pLimit, iTabCur
116449 );
116450 iDataCur = iIdxCur = iTabCur;
116451 pOrderBy = 0;
116452 pLimit = 0;
116453 }
116454#endif
116455
116456 /* Resolve the column names in the WHERE clause.
116457 */
116458 memset(&sNC, 0, sizeof(sNC));
116459 sNC.pParse = pParse;
116460 sNC.pSrcList = pTabList;
116461 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
116462 goto delete_from_cleanup;
116463 }
116464
116465 /* Initialize the counter of the number of rows deleted, if
116466 ** we are counting rows.
116467 */
116468 if( (db->flags & SQLITE_CountRows)!=0
116469 && !pParse->nested
116470 && !pParse->pTriggerTab
116471 ){
116472 memCnt = ++pParse->nMem;
116473 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
116474 }
116475
116476#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
116477 /* Special case: A DELETE without a WHERE clause deletes everything.
116478 ** It is easier just to erase the whole table. Prior to version 3.6.5,
116479 ** this optimization caused the row change count (the value returned by
116480 ** API function sqlite3_count_changes) to be set incorrectly.
116481 **
116482 ** The "rcauth==SQLITE_OK" terms is the
116483 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
116484 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
116485 ** the truncate optimization is disabled and all rows are deleted
116486 ** individually.
116487 */
116488 if( rcauth==SQLITE_OK
116489 && pWhere==0
116490 && !bComplex
116491 && !IsVirtual(pTab)
116492#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
116493 && db->xPreUpdateCallback==0
116494#endif
116495 ){
116496 assert( !isView );
116497 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
116498 if( HasRowid(pTab) ){
116499 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
116500 pTab->zName, P4_STATIC);
116501 }
116502 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116503 assert( pIdx->pSchema==pTab->pSchema );
116504 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
116505 }
116506 }else
116507#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
116508 {
116509 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
116510 if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
116511 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
116512 if( HasRowid(pTab) ){
116513 /* For a rowid table, initialize the RowSet to an empty set */
116514 pPk = 0;
116515 nPk = 1;
116516 iRowSet = ++pParse->nMem;
116517 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
116518 }else{
116519 /* For a WITHOUT ROWID table, create an ephemeral table used to
116520 ** hold all primary keys for rows to be deleted. */
116521 pPk = sqlite3PrimaryKeyIndex(pTab);
116522 assert( pPk!=0 );
116523 nPk = pPk->nKeyCol;
116524 iPk = pParse->nMem+1;
116525 pParse->nMem += nPk;
116526 iEphCur = pParse->nTab++;
116527 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
116528 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
116529 }
116530
116531 /* Construct a query to find the rowid or primary key for every row
116532 ** to be deleted, based on the WHERE clause. Set variable eOnePass
116533 ** to indicate the strategy used to implement this delete:
116534 **
116535 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
116536 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
116537 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
116538 */
116539 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
116540 if( pWInfo==0 ) goto delete_from_cleanup;
116541 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
116542 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
116543 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
116544 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
116545
116546 /* Keep track of the number of rows to be deleted */
116547 if( memCnt ){
116548 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
116549 }
116550
116551 /* Extract the rowid or primary key for the current row */
116552 if( pPk ){
116553 for(i=0; i<nPk; i++){
116554 assert( pPk->aiColumn[i]>=0 );
116555 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
116556 pPk->aiColumn[i], iPk+i);
116557 }
116558 iKey = iPk;
116559 }else{
116560 iKey = ++pParse->nMem;
116561 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
116562 }
116563
116564 if( eOnePass!=ONEPASS_OFF ){
116565 /* For ONEPASS, no need to store the rowid/primary-key. There is only
116566 ** one, so just keep it in its register(s) and fall through to the
116567 ** delete code. */
116568 nKey = nPk; /* OP_Found will use an unpacked key */
116569 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
116570 if( aToOpen==0 ){
116571 sqlite3WhereEnd(pWInfo);
116572 goto delete_from_cleanup;
116573 }
116574 memset(aToOpen, 1, nIdx+1);
116575 aToOpen[nIdx+1] = 0;
116576 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
116577 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
116578 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
116579 }else{
116580 if( pPk ){
116581 /* Add the PK key for this row to the temporary table */
116582 iKey = ++pParse->nMem;
116583 nKey = 0; /* Zero tells OP_Found to use a composite key */
116584 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
116585 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
116586 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
116587 }else{
116588 /* Add the rowid of the row to be deleted to the RowSet */
116589 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
116590 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
116591 }
116592 }
116593
116594 /* If this DELETE cannot use the ONEPASS strategy, this is the
116595 ** end of the WHERE loop */
116596 if( eOnePass!=ONEPASS_OFF ){
116597 addrBypass = sqlite3VdbeMakeLabel(pParse);
116598 }else{
116599 sqlite3WhereEnd(pWInfo);
116600 }
116601
116602 /* Unless this is a view, open cursors for the table we are
116603 ** deleting from and all its indices. If this is a view, then the
116604 ** only effect this statement has is to fire the INSTEAD OF
116605 ** triggers.
116606 */
116607 if( !isView ){
116608 int iAddrOnce = 0;
116609 if( eOnePass==ONEPASS_MULTI ){
116610 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
116611 }
116612 testcase( IsVirtual(pTab) );
116613 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
116614 iTabCur, aToOpen, &iDataCur, &iIdxCur);
116615 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
116616 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
116617 if( eOnePass==ONEPASS_MULTI ){
116618 sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
116619 }
116620 }
116621
116622 /* Set up a loop over the rowids/primary-keys that were found in the
116623 ** where-clause loop above.
116624 */
116625 if( eOnePass!=ONEPASS_OFF ){
116626 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
116627 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
116628 assert( pPk!=0 || pTab->pSelect!=0 );
116629 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
116630 VdbeCoverage(v);
116631 }
116632 }else if( pPk ){
116633 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
116634 if( IsVirtual(pTab) ){
116635 sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
116636 }else{
116637 sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
116638 }
116639 assert( nKey==0 ); /* OP_Found will use a composite key */
116640 }else{
116641 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
116642 VdbeCoverage(v);
116643 assert( nKey==1 );
116644 }
116645
116646 /* Delete the row */
116647#ifndef SQLITE_OMIT_VIRTUALTABLE
116648 if( IsVirtual(pTab) ){
116649 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
116650 sqlite3VtabMakeWritable(pParse, pTab);
116651 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
116652 sqlite3MayAbort(pParse);
116653 if( eOnePass==ONEPASS_SINGLE ){
116654 sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
116655 if( sqlite3IsToplevel(pParse) ){
116656 pParse->isMultiWrite = 0;
116657 }
116658 }
116659 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
116660 sqlite3VdbeChangeP5(v, OE_Abort);
116661 }else
116662#endif
116663 {
116664 int count = (pParse->nested==0); /* True to count changes */
116665 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
116666 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
116667 }
116668
116669 /* End of the loop over all rowids/primary-keys. */
116670 if( eOnePass!=ONEPASS_OFF ){
116671 sqlite3VdbeResolveLabel(v, addrBypass);
116672 sqlite3WhereEnd(pWInfo);
116673 }else if( pPk ){
116674 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
116675 sqlite3VdbeJumpHere(v, addrLoop);
116676 }else{
116677 sqlite3VdbeGoto(v, addrLoop);
116678 sqlite3VdbeJumpHere(v, addrLoop);
116679 }
116680 } /* End non-truncate path */
116681
116682 /* Update the sqlite_sequence table by storing the content of the
116683 ** maximum rowid counter values recorded while inserting into
116684 ** autoincrement tables.
116685 */
116686 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
116687 sqlite3AutoincrementEnd(pParse);
116688 }
116689
116690 /* Return the number of rows that were deleted. If this routine is
116691 ** generating code because of a call to sqlite3NestedParse(), do not
116692 ** invoke the callback function.
116693 */
116694 if( memCnt ){
116695 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
116696 sqlite3VdbeSetNumCols(v, 1);
116697 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
116698 }
116699
116700delete_from_cleanup:
116701 sqlite3AuthContextPop(&sContext);
116702 sqlite3SrcListDelete(db, pTabList);
116703 sqlite3ExprDelete(db, pWhere);
116704#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
116705 sqlite3ExprListDelete(db, pOrderBy);
116706 sqlite3ExprDelete(db, pLimit);
116707#endif
116708 sqlite3DbFree(db, aToOpen);
116709 return;
116710}
116711/* Make sure "isView" and other macros defined above are undefined. Otherwise
116712** they may interfere with compilation of other functions in this file
116713** (or in another file, if this file becomes part of the amalgamation). */
116714#ifdef isView
116715 #undef isView
116716#endif
116717#ifdef pTrigger
116718 #undef pTrigger
116719#endif
116720
116721/*
116722** This routine generates VDBE code that causes a single row of a
116723** single table to be deleted. Both the original table entry and
116724** all indices are removed.
116725**
116726** Preconditions:
116727**
116728** 1. iDataCur is an open cursor on the btree that is the canonical data
116729** store for the table. (This will be either the table itself,
116730** in the case of a rowid table, or the PRIMARY KEY index in the case
116731** of a WITHOUT ROWID table.)
116732**
116733** 2. Read/write cursors for all indices of pTab must be open as
116734** cursor number iIdxCur+i for the i-th index.
116735**
116736** 3. The primary key for the row to be deleted must be stored in a
116737** sequence of nPk memory cells starting at iPk. If nPk==0 that means
116738** that a search record formed from OP_MakeRecord is contained in the
116739** single memory location iPk.
116740**
116741** eMode:
116742** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
116743** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
116744** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
116745** then this function must seek iDataCur to the entry identified by iPk
116746** and nPk before reading from it.
116747**
116748** If eMode is ONEPASS_MULTI, then this call is being made as part
116749** of a ONEPASS delete that affects multiple rows. In this case, if
116750** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
116751** iDataCur, then its position should be preserved following the delete
116752** operation. Or, if iIdxNoSeek is not a valid cursor number, the
116753** position of iDataCur should be preserved instead.
116754**
116755** iIdxNoSeek:
116756** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
116757** then it identifies an index cursor (from within array of cursors
116758** starting at iIdxCur) that already points to the index entry to be deleted.
116759** Except, this optimization is disabled if there are BEFORE triggers since
116760** the trigger body might have moved the cursor.
116761*/
116762SQLITE_PRIVATE void sqlite3GenerateRowDelete(
116763 Parse *pParse, /* Parsing context */
116764 Table *pTab, /* Table containing the row to be deleted */
116765 Trigger *pTrigger, /* List of triggers to (potentially) fire */
116766 int iDataCur, /* Cursor from which column data is extracted */
116767 int iIdxCur, /* First index cursor */
116768 int iPk, /* First memory cell containing the PRIMARY KEY */
116769 i16 nPk, /* Number of PRIMARY KEY memory cells */
116770 u8 count, /* If non-zero, increment the row change counter */
116771 u8 onconf, /* Default ON CONFLICT policy for triggers */
116772 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
116773 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
116774){
116775 Vdbe *v = pParse->pVdbe; /* Vdbe */
116776 int iOld = 0; /* First register in OLD.* array */
116777 int iLabel; /* Label resolved to end of generated code */
116778 u8 opSeek; /* Seek opcode */
116779
116780 /* Vdbe is guaranteed to have been allocated by this stage. */
116781 assert( v );
116782 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
116783 iDataCur, iIdxCur, iPk, (int)nPk));
116784
116785 /* Seek cursor iCur to the row to delete. If this row no longer exists
116786 ** (this can happen if a trigger program has already deleted it), do
116787 ** not attempt to delete it or fire any DELETE triggers. */
116788 iLabel = sqlite3VdbeMakeLabel(pParse);
116789 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
116790 if( eMode==ONEPASS_OFF ){
116791 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
116792 VdbeCoverageIf(v, opSeek==OP_NotExists);
116793 VdbeCoverageIf(v, opSeek==OP_NotFound);
116794 }
116795
116796 /* If there are any triggers to fire, allocate a range of registers to
116797 ** use for the old.* references in the triggers. */
116798 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
116799 u32 mask; /* Mask of OLD.* columns in use */
116800 int iCol; /* Iterator used while populating OLD.* */
116801 int addrStart; /* Start of BEFORE trigger programs */
116802
116803 /* TODO: Could use temporary registers here. Also could attempt to
116804 ** avoid copying the contents of the rowid register. */
116805 mask = sqlite3TriggerColmask(
116806 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
116807 );
116808 mask |= sqlite3FkOldmask(pParse, pTab);
116809 iOld = pParse->nMem+1;
116810 pParse->nMem += (1 + pTab->nCol);
116811
116812 /* Populate the OLD.* pseudo-table register array. These values will be
116813 ** used by any BEFORE and AFTER triggers that exist. */
116814 sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
116815 for(iCol=0; iCol<pTab->nCol; iCol++){
116816 testcase( mask!=0xffffffff && iCol==31 );
116817 testcase( mask!=0xffffffff && iCol==32 );
116818 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
116819 int kk = sqlite3TableColumnToStorage(pTab, iCol);
116820 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
116821 }
116822 }
116823
116824 /* Invoke BEFORE DELETE trigger programs. */
116825 addrStart = sqlite3VdbeCurrentAddr(v);
116826 sqlite3CodeRowTrigger(pParse, pTrigger,
116827 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
116828 );
116829
116830 /* If any BEFORE triggers were coded, then seek the cursor to the
116831 ** row to be deleted again. It may be that the BEFORE triggers moved
116832 ** the cursor or already deleted the row that the cursor was
116833 ** pointing to.
116834 **
116835 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
116836 ** may have moved that cursor.
116837 */
116838 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
116839 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
116840 VdbeCoverageIf(v, opSeek==OP_NotExists);
116841 VdbeCoverageIf(v, opSeek==OP_NotFound);
116842 testcase( iIdxNoSeek>=0 );
116843 iIdxNoSeek = -1;
116844 }
116845
116846 /* Do FK processing. This call checks that any FK constraints that
116847 ** refer to this table (i.e. constraints attached to other tables)
116848 ** are not violated by deleting this row. */
116849 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
116850 }
116851
116852 /* Delete the index and table entries. Skip this step if pTab is really
116853 ** a view (in which case the only effect of the DELETE statement is to
116854 ** fire the INSTEAD OF triggers).
116855 **
116856 ** If variable 'count' is non-zero, then this OP_Delete instruction should
116857 ** invoke the update-hook. The pre-update-hook, on the other hand should
116858 ** be invoked unless table pTab is a system table. The difference is that
116859 ** the update-hook is not invoked for rows removed by REPLACE, but the
116860 ** pre-update-hook is.
116861 */
116862 if( pTab->pSelect==0 ){
116863 u8 p5 = 0;
116864 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
116865 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
116866 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
116867 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
116868 }
116869 if( eMode!=ONEPASS_OFF ){
116870 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
116871 }
116872 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
116873 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
116874 }
116875 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
116876 sqlite3VdbeChangeP5(v, p5);
116877 }
116878
116879 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
116880 ** handle rows (possibly in other tables) that refer via a foreign key
116881 ** to the row just deleted. */
116882 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
116883
116884 /* Invoke AFTER DELETE trigger programs. */
116885 sqlite3CodeRowTrigger(pParse, pTrigger,
116886 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
116887 );
116888
116889 /* Jump here if the row had already been deleted before any BEFORE
116890 ** trigger programs were invoked. Or if a trigger program throws a
116891 ** RAISE(IGNORE) exception. */
116892 sqlite3VdbeResolveLabel(v, iLabel);
116893 VdbeModuleComment((v, "END: GenRowDel()"));
116894}
116895
116896/*
116897** This routine generates VDBE code that causes the deletion of all
116898** index entries associated with a single row of a single table, pTab
116899**
116900** Preconditions:
116901**
116902** 1. A read/write cursor "iDataCur" must be open on the canonical storage
116903** btree for the table pTab. (This will be either the table itself
116904** for rowid tables or to the primary key index for WITHOUT ROWID
116905** tables.)
116906**
116907** 2. Read/write cursors for all indices of pTab must be open as
116908** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
116909** index is the 0-th index.)
116910**
116911** 3. The "iDataCur" cursor must be already be positioned on the row
116912** that is to be deleted.
116913*/
116914SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
116915 Parse *pParse, /* Parsing and code generating context */
116916 Table *pTab, /* Table containing the row to be deleted */
116917 int iDataCur, /* Cursor of table holding data. */
116918 int iIdxCur, /* First index cursor */
116919 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
116920 int iIdxNoSeek /* Do not delete from this cursor */
116921){
116922 int i; /* Index loop counter */
116923 int r1 = -1; /* Register holding an index key */
116924 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
116925 Index *pIdx; /* Current index */
116926 Index *pPrior = 0; /* Prior index */
116927 Vdbe *v; /* The prepared statement under construction */
116928 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
116929
116930 v = pParse->pVdbe;
116931 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
116932 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
116933 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
116934 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
116935 if( pIdx==pPk ) continue;
116936 if( iIdxCur+i==iIdxNoSeek ) continue;
116937 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
116938 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
116939 &iPartIdxLabel, pPrior, r1);
116940 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
116941 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
116942 sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
116943 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
116944 pPrior = pIdx;
116945 }
116946}
116947
116948/*
116949** Generate code that will assemble an index key and stores it in register
116950** regOut. The key with be for index pIdx which is an index on pTab.
116951** iCur is the index of a cursor open on the pTab table and pointing to
116952** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
116953** iCur must be the cursor of the PRIMARY KEY index.
116954**
116955** Return a register number which is the first in a block of
116956** registers that holds the elements of the index key. The
116957** block of registers has already been deallocated by the time
116958** this routine returns.
116959**
116960** If *piPartIdxLabel is not NULL, fill it in with a label and jump
116961** to that label if pIdx is a partial index that should be skipped.
116962** The label should be resolved using sqlite3ResolvePartIdxLabel().
116963** A partial index should be skipped if its WHERE clause evaluates
116964** to false or null. If pIdx is not a partial index, *piPartIdxLabel
116965** will be set to zero which is an empty label that is ignored by
116966** sqlite3ResolvePartIdxLabel().
116967**
116968** The pPrior and regPrior parameters are used to implement a cache to
116969** avoid unnecessary register loads. If pPrior is not NULL, then it is
116970** a pointer to a different index for which an index key has just been
116971** computed into register regPrior. If the current pIdx index is generating
116972** its key into the same sequence of registers and if pPrior and pIdx share
116973** a column in common, then the register corresponding to that column already
116974** holds the correct value and the loading of that register is skipped.
116975** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
116976** on a table with multiple indices, and especially with the ROWID or
116977** PRIMARY KEY columns of the index.
116978*/
116979SQLITE_PRIVATE int sqlite3GenerateIndexKey(
116980 Parse *pParse, /* Parsing context */
116981 Index *pIdx, /* The index for which to generate a key */
116982 int iDataCur, /* Cursor number from which to take column data */
116983 int regOut, /* Put the new key into this register if not 0 */
116984 int prefixOnly, /* Compute only a unique prefix of the key */
116985 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
116986 Index *pPrior, /* Previously generated index key */
116987 int regPrior /* Register holding previous generated key */
116988){
116989 Vdbe *v = pParse->pVdbe;
116990 int j;
116991 int regBase;
116992 int nCol;
116993
116994 if( piPartIdxLabel ){
116995 if( pIdx->pPartIdxWhere ){
116996 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
116997 pParse->iSelfTab = iDataCur + 1;
116998 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
116999 SQLITE_JUMPIFNULL);
117000 pParse->iSelfTab = 0;
117001 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
117002 ** pPartIdxWhere may have corrupted regPrior registers */
117003 }else{
117004 *piPartIdxLabel = 0;
117005 }
117006 }
117007 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
117008 regBase = sqlite3GetTempRange(pParse, nCol);
117009 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
117010 for(j=0; j<nCol; j++){
117011 if( pPrior
117012 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
117013 && pPrior->aiColumn[j]!=XN_EXPR
117014 ){
117015 /* This column was already computed by the previous index */
117016 continue;
117017 }
117018 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
117019 /* If the column affinity is REAL but the number is an integer, then it
117020 ** might be stored in the table as an integer (using a compact
117021 ** representation) then converted to REAL by an OP_RealAffinity opcode.
117022 ** But we are getting ready to store this value back into an index, where
117023 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
117024 ** opcode if it is present */
117025 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
117026 }
117027 if( regOut ){
117028 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
117029 if( pIdx->pTable->pSelect ){
117030 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
117031 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
117032 }
117033 }
117034 sqlite3ReleaseTempRange(pParse, regBase, nCol);
117035 return regBase;
117036}
117037
117038/*
117039** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
117040** because it was a partial index, then this routine should be called to
117041** resolve that label.
117042*/
117043SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
117044 if( iLabel ){
117045 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
117046 }
117047}
117048
117049/************** End of delete.c **********************************************/
117050/************** Begin file func.c ********************************************/
117051/*
117052** 2002 February 23
117053**
117054** The author disclaims copyright to this source code. In place of
117055** a legal notice, here is a blessing:
117056**
117057** May you do good and not evil.
117058** May you find forgiveness for yourself and forgive others.
117059** May you share freely, never taking more than you give.
117060**
117061*************************************************************************
117062** This file contains the C-language implementations for many of the SQL
117063** functions of SQLite. (Some function, and in particular the date and
117064** time functions, are implemented separately.)
117065*/
117066/* #include "sqliteInt.h" */
117067/* #include <stdlib.h> */
117068/* #include <assert.h> */
117069#ifndef SQLITE_OMIT_FLOATING_POINT
117070/* #include <math.h> */
117071#endif
117072/* #include "vdbeInt.h" */
117073
117074/*
117075** Return the collating function associated with a function.
117076*/
117077static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
117078 VdbeOp *pOp;
117079 assert( context->pVdbe!=0 );
117080 pOp = &context->pVdbe->aOp[context->iOp-1];
117081 assert( pOp->opcode==OP_CollSeq );
117082 assert( pOp->p4type==P4_COLLSEQ );
117083 return pOp->p4.pColl;
117084}
117085
117086/*
117087** Indicate that the accumulator load should be skipped on this
117088** iteration of the aggregate loop.
117089*/
117090static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
117091 assert( context->isError<=0 );
117092 context->isError = -1;
117093 context->skipFlag = 1;
117094}
117095
117096/*
117097** Implementation of the non-aggregate min() and max() functions
117098*/
117099static void minmaxFunc(
117100 sqlite3_context *context,
117101 int argc,
117102 sqlite3_value **argv
117103){
117104 int i;
117105 int mask; /* 0 for min() or 0xffffffff for max() */
117106 int iBest;
117107 CollSeq *pColl;
117108
117109 assert( argc>1 );
117110 mask = sqlite3_user_data(context)==0 ? 0 : -1;
117111 pColl = sqlite3GetFuncCollSeq(context);
117112 assert( pColl );
117113 assert( mask==-1 || mask==0 );
117114 iBest = 0;
117115 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
117116 for(i=1; i<argc; i++){
117117 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
117118 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
117119 testcase( mask==0 );
117120 iBest = i;
117121 }
117122 }
117123 sqlite3_result_value(context, argv[iBest]);
117124}
117125
117126/*
117127** Return the type of the argument.
117128*/
117129static void typeofFunc(
117130 sqlite3_context *context,
117131 int NotUsed,
117132 sqlite3_value **argv
117133){
117134 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
117135 int i = sqlite3_value_type(argv[0]) - 1;
117136 UNUSED_PARAMETER(NotUsed);
117137 assert( i>=0 && i<ArraySize(azType) );
117138 assert( SQLITE_INTEGER==1 );
117139 assert( SQLITE_FLOAT==2 );
117140 assert( SQLITE_TEXT==3 );
117141 assert( SQLITE_BLOB==4 );
117142 assert( SQLITE_NULL==5 );
117143 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
117144 ** the datatype code for the initial datatype of the sqlite3_value object
117145 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
117146 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
117147 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
117148}
117149
117150
117151/*
117152** Implementation of the length() function
117153*/
117154static void lengthFunc(
117155 sqlite3_context *context,
117156 int argc,
117157 sqlite3_value **argv
117158){
117159 assert( argc==1 );
117160 UNUSED_PARAMETER(argc);
117161 switch( sqlite3_value_type(argv[0]) ){
117162 case SQLITE_BLOB:
117163 case SQLITE_INTEGER:
117164 case SQLITE_FLOAT: {
117165 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
117166 break;
117167 }
117168 case SQLITE_TEXT: {
117169 const unsigned char *z = sqlite3_value_text(argv[0]);
117170 const unsigned char *z0;
117171 unsigned char c;
117172 if( z==0 ) return;
117173 z0 = z;
117174 while( (c = *z)!=0 ){
117175 z++;
117176 if( c>=0xc0 ){
117177 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
117178 }
117179 }
117180 sqlite3_result_int(context, (int)(z-z0));
117181 break;
117182 }
117183 default: {
117184 sqlite3_result_null(context);
117185 break;
117186 }
117187 }
117188}
117189
117190/*
117191** Implementation of the abs() function.
117192**
117193** IMP: R-23979-26855 The abs(X) function returns the absolute value of
117194** the numeric argument X.
117195*/
117196static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117197 assert( argc==1 );
117198 UNUSED_PARAMETER(argc);
117199 switch( sqlite3_value_type(argv[0]) ){
117200 case SQLITE_INTEGER: {
117201 i64 iVal = sqlite3_value_int64(argv[0]);
117202 if( iVal<0 ){
117203 if( iVal==SMALLEST_INT64 ){
117204 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
117205 ** then abs(X) throws an integer overflow error since there is no
117206 ** equivalent positive 64-bit two complement value. */
117207 sqlite3_result_error(context, "integer overflow", -1);
117208 return;
117209 }
117210 iVal = -iVal;
117211 }
117212 sqlite3_result_int64(context, iVal);
117213 break;
117214 }
117215 case SQLITE_NULL: {
117216 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
117217 sqlite3_result_null(context);
117218 break;
117219 }
117220 default: {
117221 /* Because sqlite3_value_double() returns 0.0 if the argument is not
117222 ** something that can be converted into a number, we have:
117223 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
117224 ** that cannot be converted to a numeric value.
117225 */
117226 double rVal = sqlite3_value_double(argv[0]);
117227 if( rVal<0 ) rVal = -rVal;
117228 sqlite3_result_double(context, rVal);
117229 break;
117230 }
117231 }
117232}
117233
117234/*
117235** Implementation of the instr() function.
117236**
117237** instr(haystack,needle) finds the first occurrence of needle
117238** in haystack and returns the number of previous characters plus 1,
117239** or 0 if needle does not occur within haystack.
117240**
117241** If both haystack and needle are BLOBs, then the result is one more than
117242** the number of bytes in haystack prior to the first occurrence of needle,
117243** or 0 if needle never occurs in haystack.
117244*/
117245static void instrFunc(
117246 sqlite3_context *context,
117247 int argc,
117248 sqlite3_value **argv
117249){
117250 const unsigned char *zHaystack;
117251 const unsigned char *zNeedle;
117252 int nHaystack;
117253 int nNeedle;
117254 int typeHaystack, typeNeedle;
117255 int N = 1;
117256 int isText;
117257 unsigned char firstChar;
117258 sqlite3_value *pC1 = 0;
117259 sqlite3_value *pC2 = 0;
117260
117261 UNUSED_PARAMETER(argc);
117262 typeHaystack = sqlite3_value_type(argv[0]);
117263 typeNeedle = sqlite3_value_type(argv[1]);
117264 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
117265 nHaystack = sqlite3_value_bytes(argv[0]);
117266 nNeedle = sqlite3_value_bytes(argv[1]);
117267 if( nNeedle>0 ){
117268 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
117269 zHaystack = sqlite3_value_blob(argv[0]);
117270 zNeedle = sqlite3_value_blob(argv[1]);
117271 isText = 0;
117272 }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
117273 zHaystack = sqlite3_value_text(argv[0]);
117274 zNeedle = sqlite3_value_text(argv[1]);
117275 isText = 1;
117276 }else{
117277 pC1 = sqlite3_value_dup(argv[0]);
117278 zHaystack = sqlite3_value_text(pC1);
117279 if( zHaystack==0 ) goto endInstrOOM;
117280 nHaystack = sqlite3_value_bytes(pC1);
117281 pC2 = sqlite3_value_dup(argv[1]);
117282 zNeedle = sqlite3_value_text(pC2);
117283 if( zNeedle==0 ) goto endInstrOOM;
117284 nNeedle = sqlite3_value_bytes(pC2);
117285 isText = 1;
117286 }
117287 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
117288 firstChar = zNeedle[0];
117289 while( nNeedle<=nHaystack
117290 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
117291 ){
117292 N++;
117293 do{
117294 nHaystack--;
117295 zHaystack++;
117296 }while( isText && (zHaystack[0]&0xc0)==0x80 );
117297 }
117298 if( nNeedle>nHaystack ) N = 0;
117299 }
117300 sqlite3_result_int(context, N);
117301endInstr:
117302 sqlite3_value_free(pC1);
117303 sqlite3_value_free(pC2);
117304 return;
117305endInstrOOM:
117306 sqlite3_result_error_nomem(context);
117307 goto endInstr;
117308}
117309
117310/*
117311** Implementation of the printf() function.
117312*/
117313static void printfFunc(
117314 sqlite3_context *context,
117315 int argc,
117316 sqlite3_value **argv
117317){
117318 PrintfArguments x;
117319 StrAccum str;
117320 const char *zFormat;
117321 int n;
117322 sqlite3 *db = sqlite3_context_db_handle(context);
117323
117324 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
117325 x.nArg = argc-1;
117326 x.nUsed = 0;
117327 x.apArg = argv+1;
117328 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
117329 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
117330 sqlite3_str_appendf(&str, zFormat, &x);
117331 n = str.nChar;
117332 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
117333 SQLITE_DYNAMIC);
117334 }
117335}
117336
117337/*
117338** Implementation of the substr() function.
117339**
117340** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
117341** p1 is 1-indexed. So substr(x,1,1) returns the first character
117342** of x. If x is text, then we actually count UTF-8 characters.
117343** If x is a blob, then we count bytes.
117344**
117345** If p1 is negative, then we begin abs(p1) from the end of x[].
117346**
117347** If p2 is negative, return the p2 characters preceding p1.
117348*/
117349static void substrFunc(
117350 sqlite3_context *context,
117351 int argc,
117352 sqlite3_value **argv
117353){
117354 const unsigned char *z;
117355 const unsigned char *z2;
117356 int len;
117357 int p0type;
117358 i64 p1, p2;
117359 int negP2 = 0;
117360
117361 assert( argc==3 || argc==2 );
117362 if( sqlite3_value_type(argv[1])==SQLITE_NULL
117363 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
117364 ){
117365 return;
117366 }
117367 p0type = sqlite3_value_type(argv[0]);
117368 p1 = sqlite3_value_int(argv[1]);
117369 if( p0type==SQLITE_BLOB ){
117370 len = sqlite3_value_bytes(argv[0]);
117371 z = sqlite3_value_blob(argv[0]);
117372 if( z==0 ) return;
117373 assert( len==sqlite3_value_bytes(argv[0]) );
117374 }else{
117375 z = sqlite3_value_text(argv[0]);
117376 if( z==0 ) return;
117377 len = 0;
117378 if( p1<0 ){
117379 for(z2=z; *z2; len++){
117380 SQLITE_SKIP_UTF8(z2);
117381 }
117382 }
117383 }
117384#ifdef SQLITE_SUBSTR_COMPATIBILITY
117385 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
117386 ** as substr(X,1,N) - it returns the first N characters of X. This
117387 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
117388 ** from 2009-02-02 for compatibility of applications that exploited the
117389 ** old buggy behavior. */
117390 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
117391#endif
117392 if( argc==3 ){
117393 p2 = sqlite3_value_int(argv[2]);
117394 if( p2<0 ){
117395 p2 = -p2;
117396 negP2 = 1;
117397 }
117398 }else{
117399 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
117400 }
117401 if( p1<0 ){
117402 p1 += len;
117403 if( p1<0 ){
117404 p2 += p1;
117405 if( p2<0 ) p2 = 0;
117406 p1 = 0;
117407 }
117408 }else if( p1>0 ){
117409 p1--;
117410 }else if( p2>0 ){
117411 p2--;
117412 }
117413 if( negP2 ){
117414 p1 -= p2;
117415 if( p1<0 ){
117416 p2 += p1;
117417 p1 = 0;
117418 }
117419 }
117420 assert( p1>=0 && p2>=0 );
117421 if( p0type!=SQLITE_BLOB ){
117422 while( *z && p1 ){
117423 SQLITE_SKIP_UTF8(z);
117424 p1--;
117425 }
117426 for(z2=z; *z2 && p2; p2--){
117427 SQLITE_SKIP_UTF8(z2);
117428 }
117429 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
117430 SQLITE_UTF8);
117431 }else{
117432 if( p1+p2>len ){
117433 p2 = len-p1;
117434 if( p2<0 ) p2 = 0;
117435 }
117436 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
117437 }
117438}
117439
117440/*
117441** Implementation of the round() function
117442*/
117443#ifndef SQLITE_OMIT_FLOATING_POINT
117444static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117445 int n = 0;
117446 double r;
117447 char *zBuf;
117448 assert( argc==1 || argc==2 );
117449 if( argc==2 ){
117450 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
117451 n = sqlite3_value_int(argv[1]);
117452 if( n>30 ) n = 30;
117453 if( n<0 ) n = 0;
117454 }
117455 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
117456 r = sqlite3_value_double(argv[0]);
117457 /* If Y==0 and X will fit in a 64-bit int,
117458 ** handle the rounding directly,
117459 ** otherwise use printf.
117460 */
117461 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
117462 /* The value has no fractional part so there is nothing to round */
117463 }else if( n==0 ){
117464 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
117465 }else{
117466 zBuf = sqlite3_mprintf("%.*f",n,r);
117467 if( zBuf==0 ){
117468 sqlite3_result_error_nomem(context);
117469 return;
117470 }
117471 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
117472 sqlite3_free(zBuf);
117473 }
117474 sqlite3_result_double(context, r);
117475}
117476#endif
117477
117478/*
117479** Allocate nByte bytes of space using sqlite3Malloc(). If the
117480** allocation fails, call sqlite3_result_error_nomem() to notify
117481** the database handle that malloc() has failed and return NULL.
117482** If nByte is larger than the maximum string or blob length, then
117483** raise an SQLITE_TOOBIG exception and return NULL.
117484*/
117485static void *contextMalloc(sqlite3_context *context, i64 nByte){
117486 char *z;
117487 sqlite3 *db = sqlite3_context_db_handle(context);
117488 assert( nByte>0 );
117489 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
117490 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
117491 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
117492 sqlite3_result_error_toobig(context);
117493 z = 0;
117494 }else{
117495 z = sqlite3Malloc(nByte);
117496 if( !z ){
117497 sqlite3_result_error_nomem(context);
117498 }
117499 }
117500 return z;
117501}
117502
117503/*
117504** Implementation of the upper() and lower() SQL functions.
117505*/
117506static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117507 char *z1;
117508 const char *z2;
117509 int i, n;
117510 UNUSED_PARAMETER(argc);
117511 z2 = (char*)sqlite3_value_text(argv[0]);
117512 n = sqlite3_value_bytes(argv[0]);
117513 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
117514 assert( z2==(char*)sqlite3_value_text(argv[0]) );
117515 if( z2 ){
117516 z1 = contextMalloc(context, ((i64)n)+1);
117517 if( z1 ){
117518 for(i=0; i<n; i++){
117519 z1[i] = (char)sqlite3Toupper(z2[i]);
117520 }
117521 sqlite3_result_text(context, z1, n, sqlite3_free);
117522 }
117523 }
117524}
117525static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
117526 char *z1;
117527 const char *z2;
117528 int i, n;
117529 UNUSED_PARAMETER(argc);
117530 z2 = (char*)sqlite3_value_text(argv[0]);
117531 n = sqlite3_value_bytes(argv[0]);
117532 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
117533 assert( z2==(char*)sqlite3_value_text(argv[0]) );
117534 if( z2 ){
117535 z1 = contextMalloc(context, ((i64)n)+1);
117536 if( z1 ){
117537 for(i=0; i<n; i++){
117538 z1[i] = sqlite3Tolower(z2[i]);
117539 }
117540 sqlite3_result_text(context, z1, n, sqlite3_free);
117541 }
117542 }
117543}
117544
117545/*
117546** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
117547** as VDBE code so that unused argument values do not have to be computed.
117548** However, we still need some kind of function implementation for this
117549** routines in the function table. The noopFunc macro provides this.
117550** noopFunc will never be called so it doesn't matter what the implementation
117551** is. We might as well use the "version()" function as a substitute.
117552*/
117553#define noopFunc versionFunc /* Substitute function - never called */
117554
117555/*
117556** Implementation of random(). Return a random integer.
117557*/
117558static void randomFunc(
117559 sqlite3_context *context,
117560 int NotUsed,
117561 sqlite3_value **NotUsed2
117562){
117563 sqlite_int64 r;
117564 UNUSED_PARAMETER2(NotUsed, NotUsed2);
117565 sqlite3_randomness(sizeof(r), &r);
117566 if( r<0 ){
117567 /* We need to prevent a random number of 0x8000000000000000
117568 ** (or -9223372036854775808) since when you do abs() of that
117569 ** number of you get the same value back again. To do this
117570 ** in a way that is testable, mask the sign bit off of negative
117571 ** values, resulting in a positive value. Then take the
117572 ** 2s complement of that positive value. The end result can
117573 ** therefore be no less than -9223372036854775807.
117574 */
117575 r = -(r & LARGEST_INT64);
117576 }
117577 sqlite3_result_int64(context, r);
117578}
117579
117580/*
117581** Implementation of randomblob(N). Return a random blob
117582** that is N bytes long.
117583*/
117584static void randomBlob(
117585 sqlite3_context *context,
117586 int argc,
117587 sqlite3_value **argv
117588){
117589 sqlite3_int64 n;
117590 unsigned char *p;
117591 assert( argc==1 );
117592 UNUSED_PARAMETER(argc);
117593 n = sqlite3_value_int64(argv[0]);
117594 if( n<1 ){
117595 n = 1;
117596 }
117597 p = contextMalloc(context, n);
117598 if( p ){
117599 sqlite3_randomness(n, p);
117600 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
117601 }
117602}
117603
117604/*
117605** Implementation of the last_insert_rowid() SQL function. The return
117606** value is the same as the sqlite3_last_insert_rowid() API function.
117607*/
117608static void last_insert_rowid(
117609 sqlite3_context *context,
117610 int NotUsed,
117611 sqlite3_value **NotUsed2
117612){
117613 sqlite3 *db = sqlite3_context_db_handle(context);
117614 UNUSED_PARAMETER2(NotUsed, NotUsed2);
117615 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
117616 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
117617 ** function. */
117618 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
117619}
117620
117621/*
117622** Implementation of the changes() SQL function.
117623**
117624** IMP: R-62073-11209 The changes() SQL function is a wrapper
117625** around the sqlite3_changes() C/C++ function and hence follows the same
117626** rules for counting changes.
117627*/
117628static void changes(
117629 sqlite3_context *context,
117630 int NotUsed,
117631 sqlite3_value **NotUsed2
117632){
117633 sqlite3 *db = sqlite3_context_db_handle(context);
117634 UNUSED_PARAMETER2(NotUsed, NotUsed2);
117635 sqlite3_result_int(context, sqlite3_changes(db));
117636}
117637
117638/*
117639** Implementation of the total_changes() SQL function. The return value is
117640** the same as the sqlite3_total_changes() API function.
117641*/
117642static void total_changes(
117643 sqlite3_context *context,
117644 int NotUsed,
117645 sqlite3_value **NotUsed2
117646){
117647 sqlite3 *db = sqlite3_context_db_handle(context);
117648 UNUSED_PARAMETER2(NotUsed, NotUsed2);
117649 /* IMP: R-52756-41993 This function is a wrapper around the
117650 ** sqlite3_total_changes() C/C++ interface. */
117651 sqlite3_result_int(context, sqlite3_total_changes(db));
117652}
117653
117654/*
117655** A structure defining how to do GLOB-style comparisons.
117656*/
117657struct compareInfo {
117658 u8 matchAll; /* "*" or "%" */
117659 u8 matchOne; /* "?" or "_" */
117660 u8 matchSet; /* "[" or 0 */
117661 u8 noCase; /* true to ignore case differences */
117662};
117663
117664/*
117665** For LIKE and GLOB matching on EBCDIC machines, assume that every
117666** character is exactly one byte in size. Also, provde the Utf8Read()
117667** macro for fast reading of the next character in the common case where
117668** the next character is ASCII.
117669*/
117670#if defined(SQLITE_EBCDIC)
117671# define sqlite3Utf8Read(A) (*((*A)++))
117672# define Utf8Read(A) (*(A++))
117673#else
117674# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
117675#endif
117676
117677static const struct compareInfo globInfo = { '*', '?', '[', 0 };
117678/* The correct SQL-92 behavior is for the LIKE operator to ignore
117679** case. Thus 'a' LIKE 'A' would be true. */
117680static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
117681/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
117682** is case sensitive causing 'a' LIKE 'A' to be false */
117683static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
117684
117685/*
117686** Possible error returns from patternMatch()
117687*/
117688#define SQLITE_MATCH 0
117689#define SQLITE_NOMATCH 1
117690#define SQLITE_NOWILDCARDMATCH 2
117691
117692/*
117693** Compare two UTF-8 strings for equality where the first string is
117694** a GLOB or LIKE expression. Return values:
117695**
117696** SQLITE_MATCH: Match
117697** SQLITE_NOMATCH: No match
117698** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
117699**
117700** Globbing rules:
117701**
117702** '*' Matches any sequence of zero or more characters.
117703**
117704** '?' Matches exactly one character.
117705**
117706** [...] Matches one character from the enclosed list of
117707** characters.
117708**
117709** [^...] Matches one character not in the enclosed list.
117710**
117711** With the [...] and [^...] matching, a ']' character can be included
117712** in the list by making it the first character after '[' or '^'. A
117713** range of characters can be specified using '-'. Example:
117714** "[a-z]" matches any single lower-case letter. To match a '-', make
117715** it the last character in the list.
117716**
117717** Like matching rules:
117718**
117719** '%' Matches any sequence of zero or more characters
117720**
117721*** '_' Matches any one character
117722**
117723** Ec Where E is the "esc" character and c is any other
117724** character, including '%', '_', and esc, match exactly c.
117725**
117726** The comments within this routine usually assume glob matching.
117727**
117728** This routine is usually quick, but can be N**2 in the worst case.
117729*/
117730static int patternCompare(
117731 const u8 *zPattern, /* The glob pattern */
117732 const u8 *zString, /* The string to compare against the glob */
117733 const struct compareInfo *pInfo, /* Information about how to do the compare */
117734 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
117735){
117736 u32 c, c2; /* Next pattern and input string chars */
117737 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
117738 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
117739 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
117740 const u8 *zEscaped = 0; /* One past the last escaped input char */
117741
117742 while( (c = Utf8Read(zPattern))!=0 ){
117743 if( c==matchAll ){ /* Match "*" */
117744 /* Skip over multiple "*" characters in the pattern. If there
117745 ** are also "?" characters, skip those as well, but consume a
117746 ** single character of the input string for each "?" skipped */
117747 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
117748 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
117749 return SQLITE_NOWILDCARDMATCH;
117750 }
117751 }
117752 if( c==0 ){
117753 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
117754 }else if( c==matchOther ){
117755 if( pInfo->matchSet==0 ){
117756 c = sqlite3Utf8Read(&zPattern);
117757 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
117758 }else{
117759 /* "[...]" immediately follows the "*". We have to do a slow
117760 ** recursive search in this case, but it is an unusual case. */
117761 assert( matchOther<0x80 ); /* '[' is a single-byte character */
117762 while( *zString ){
117763 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
117764 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117765 SQLITE_SKIP_UTF8(zString);
117766 }
117767 return SQLITE_NOWILDCARDMATCH;
117768 }
117769 }
117770
117771 /* At this point variable c contains the first character of the
117772 ** pattern string past the "*". Search in the input string for the
117773 ** first matching character and recursively continue the match from
117774 ** that point.
117775 **
117776 ** For a case-insensitive search, set variable cx to be the same as
117777 ** c but in the other case and search the input string for either
117778 ** c or cx.
117779 */
117780 if( c<=0x80 ){
117781 char zStop[3];
117782 int bMatch;
117783 if( noCase ){
117784 zStop[0] = sqlite3Toupper(c);
117785 zStop[1] = sqlite3Tolower(c);
117786 zStop[2] = 0;
117787 }else{
117788 zStop[0] = c;
117789 zStop[1] = 0;
117790 }
117791 while(1){
117792 zString += strcspn((const char*)zString, zStop);
117793 if( zString[0]==0 ) break;
117794 zString++;
117795 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
117796 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117797 }
117798 }else{
117799 int bMatch;
117800 while( (c2 = Utf8Read(zString))!=0 ){
117801 if( c2!=c ) continue;
117802 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
117803 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
117804 }
117805 }
117806 return SQLITE_NOWILDCARDMATCH;
117807 }
117808 if( c==matchOther ){
117809 if( pInfo->matchSet==0 ){
117810 c = sqlite3Utf8Read(&zPattern);
117811 if( c==0 ) return SQLITE_NOMATCH;
117812 zEscaped = zPattern;
117813 }else{
117814 u32 prior_c = 0;
117815 int seen = 0;
117816 int invert = 0;
117817 c = sqlite3Utf8Read(&zString);
117818 if( c==0 ) return SQLITE_NOMATCH;
117819 c2 = sqlite3Utf8Read(&zPattern);
117820 if( c2=='^' ){
117821 invert = 1;
117822 c2 = sqlite3Utf8Read(&zPattern);
117823 }
117824 if( c2==']' ){
117825 if( c==']' ) seen = 1;
117826 c2 = sqlite3Utf8Read(&zPattern);
117827 }
117828 while( c2 && c2!=']' ){
117829 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
117830 c2 = sqlite3Utf8Read(&zPattern);
117831 if( c>=prior_c && c<=c2 ) seen = 1;
117832 prior_c = 0;
117833 }else{
117834 if( c==c2 ){
117835 seen = 1;
117836 }
117837 prior_c = c2;
117838 }
117839 c2 = sqlite3Utf8Read(&zPattern);
117840 }
117841 if( c2==0 || (seen ^ invert)==0 ){
117842 return SQLITE_NOMATCH;
117843 }
117844 continue;
117845 }
117846 }
117847 c2 = Utf8Read(zString);
117848 if( c==c2 ) continue;
117849 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
117850 continue;
117851 }
117852 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
117853 return SQLITE_NOMATCH;
117854 }
117855 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
117856}
117857
117858/*
117859** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
117860** non-zero if there is no match.
117861*/
117862SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
117863 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
117864}
117865
117866/*
117867** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
117868** a miss - like strcmp().
117869*/
117870SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
117871 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
117872}
117873
117874/*
117875** Count the number of times that the LIKE operator (or GLOB which is
117876** just a variation of LIKE) gets called. This is used for testing
117877** only.
117878*/
117879#ifdef SQLITE_TEST
117880SQLITE_API int sqlite3_like_count = 0;
117881#endif
117882
117883
117884/*
117885** Implementation of the like() SQL function. This function implements
117886** the build-in LIKE operator. The first argument to the function is the
117887** pattern and the second argument is the string. So, the SQL statements:
117888**
117889** A LIKE B
117890**
117891** is implemented as like(B,A).
117892**
117893** This same function (with a different compareInfo structure) computes
117894** the GLOB operator.
117895*/
117896static void likeFunc(
117897 sqlite3_context *context,
117898 int argc,
117899 sqlite3_value **argv
117900){
117901 const unsigned char *zA, *zB;
117902 u32 escape;
117903 int nPat;
117904 sqlite3 *db = sqlite3_context_db_handle(context);
117905 struct compareInfo *pInfo = sqlite3_user_data(context);
117906 struct compareInfo backupInfo;
117907
117908#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
117909 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
117910 || sqlite3_value_type(argv[1])==SQLITE_BLOB
117911 ){
117912#ifdef SQLITE_TEST
117913 sqlite3_like_count++;
117914#endif
117915 sqlite3_result_int(context, 0);
117916 return;
117917 }
117918#endif
117919
117920 /* Limit the length of the LIKE or GLOB pattern to avoid problems
117921 ** of deep recursion and N*N behavior in patternCompare().
117922 */
117923 nPat = sqlite3_value_bytes(argv[0]);
117924 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
117925 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
117926 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
117927 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
117928 return;
117929 }
117930 if( argc==3 ){
117931 /* The escape character string must consist of a single UTF-8 character.
117932 ** Otherwise, return an error.
117933 */
117934 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
117935 if( zEsc==0 ) return;
117936 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
117937 sqlite3_result_error(context,
117938 "ESCAPE expression must be a single character", -1);
117939 return;
117940 }
117941 escape = sqlite3Utf8Read(&zEsc);
117942 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
117943 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
117944 pInfo = &backupInfo;
117945 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
117946 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
117947 }
117948 }else{
117949 escape = pInfo->matchSet;
117950 }
117951 zB = sqlite3_value_text(argv[0]);
117952 zA = sqlite3_value_text(argv[1]);
117953 if( zA && zB ){
117954#ifdef SQLITE_TEST
117955 sqlite3_like_count++;
117956#endif
117957 sqlite3_result_int(context,
117958 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
117959 }
117960}
117961
117962/*
117963** Implementation of the NULLIF(x,y) function. The result is the first
117964** argument if the arguments are different. The result is NULL if the
117965** arguments are equal to each other.
117966*/
117967static void nullifFunc(
117968 sqlite3_context *context,
117969 int NotUsed,
117970 sqlite3_value **argv
117971){
117972 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
117973 UNUSED_PARAMETER(NotUsed);
117974 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
117975 sqlite3_result_value(context, argv[0]);
117976 }
117977}
117978
117979/*
117980** Implementation of the sqlite_version() function. The result is the version
117981** of the SQLite library that is running.
117982*/
117983static void versionFunc(
117984 sqlite3_context *context,
117985 int NotUsed,
117986 sqlite3_value **NotUsed2
117987){
117988 UNUSED_PARAMETER2(NotUsed, NotUsed2);
117989 /* IMP: R-48699-48617 This function is an SQL wrapper around the
117990 ** sqlite3_libversion() C-interface. */
117991 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
117992}
117993
117994/*
117995** Implementation of the sqlite_source_id() function. The result is a string
117996** that identifies the particular version of the source code used to build
117997** SQLite.
117998*/
117999static void sourceidFunc(
118000 sqlite3_context *context,
118001 int NotUsed,
118002 sqlite3_value **NotUsed2
118003){
118004 UNUSED_PARAMETER2(NotUsed, NotUsed2);
118005 /* IMP: R-24470-31136 This function is an SQL wrapper around the
118006 ** sqlite3_sourceid() C interface. */
118007 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
118008}
118009
118010/*
118011** Implementation of the sqlite_log() function. This is a wrapper around
118012** sqlite3_log(). The return value is NULL. The function exists purely for
118013** its side-effects.
118014*/
118015static void errlogFunc(
118016 sqlite3_context *context,
118017 int argc,
118018 sqlite3_value **argv
118019){
118020 UNUSED_PARAMETER(argc);
118021 UNUSED_PARAMETER(context);
118022 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
118023}
118024
118025/*
118026** Implementation of the sqlite_compileoption_used() function.
118027** The result is an integer that identifies if the compiler option
118028** was used to build SQLite.
118029*/
118030#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
118031static void compileoptionusedFunc(
118032 sqlite3_context *context,
118033 int argc,
118034 sqlite3_value **argv
118035){
118036 const char *zOptName;
118037 assert( argc==1 );
118038 UNUSED_PARAMETER(argc);
118039 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
118040 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
118041 ** function.
118042 */
118043 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
118044 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
118045 }
118046}
118047#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
118048
118049/*
118050** Implementation of the sqlite_compileoption_get() function.
118051** The result is a string that identifies the compiler options
118052** used to build SQLite.
118053*/
118054#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
118055static void compileoptiongetFunc(
118056 sqlite3_context *context,
118057 int argc,
118058 sqlite3_value **argv
118059){
118060 int n;
118061 assert( argc==1 );
118062 UNUSED_PARAMETER(argc);
118063 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
118064 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
118065 */
118066 n = sqlite3_value_int(argv[0]);
118067 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
118068}
118069#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
118070
118071/* Array for converting from half-bytes (nybbles) into ASCII hex
118072** digits. */
118073static const char hexdigits[] = {
118074 '0', '1', '2', '3', '4', '5', '6', '7',
118075 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
118076};
118077
118078/*
118079** Implementation of the QUOTE() function. This function takes a single
118080** argument. If the argument is numeric, the return value is the same as
118081** the argument. If the argument is NULL, the return value is the string
118082** "NULL". Otherwise, the argument is enclosed in single quotes with
118083** single-quote escapes.
118084*/
118085static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
118086 assert( argc==1 );
118087 UNUSED_PARAMETER(argc);
118088 switch( sqlite3_value_type(argv[0]) ){
118089 case SQLITE_FLOAT: {
118090 double r1, r2;
118091 char zBuf[50];
118092 r1 = sqlite3_value_double(argv[0]);
118093 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
118094 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
118095 if( r1!=r2 ){
118096 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
118097 }
118098 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
118099 break;
118100 }
118101 case SQLITE_INTEGER: {
118102 sqlite3_result_value(context, argv[0]);
118103 break;
118104 }
118105 case SQLITE_BLOB: {
118106 char *zText = 0;
118107 char const *zBlob = sqlite3_value_blob(argv[0]);
118108 int nBlob = sqlite3_value_bytes(argv[0]);
118109 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
118110 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
118111 if( zText ){
118112 int i;
118113 for(i=0; i<nBlob; i++){
118114 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
118115 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
118116 }
118117 zText[(nBlob*2)+2] = '\'';
118118 zText[(nBlob*2)+3] = '\0';
118119 zText[0] = 'X';
118120 zText[1] = '\'';
118121 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
118122 sqlite3_free(zText);
118123 }
118124 break;
118125 }
118126 case SQLITE_TEXT: {
118127 int i,j;
118128 u64 n;
118129 const unsigned char *zArg = sqlite3_value_text(argv[0]);
118130 char *z;
118131
118132 if( zArg==0 ) return;
118133 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
118134 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
118135 if( z ){
118136 z[0] = '\'';
118137 for(i=0, j=1; zArg[i]; i++){
118138 z[j++] = zArg[i];
118139 if( zArg[i]=='\'' ){
118140 z[j++] = '\'';
118141 }
118142 }
118143 z[j++] = '\'';
118144 z[j] = 0;
118145 sqlite3_result_text(context, z, j, sqlite3_free);
118146 }
118147 break;
118148 }
118149 default: {
118150 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
118151 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
118152 break;
118153 }
118154 }
118155}
118156
118157/*
118158** The unicode() function. Return the integer unicode code-point value
118159** for the first character of the input string.
118160*/
118161static void unicodeFunc(
118162 sqlite3_context *context,
118163 int argc,
118164 sqlite3_value **argv
118165){
118166 const unsigned char *z = sqlite3_value_text(argv[0]);
118167 (void)argc;
118168 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
118169}
118170
118171/*
118172** The char() function takes zero or more arguments, each of which is
118173** an integer. It constructs a string where each character of the string
118174** is the unicode character for the corresponding integer argument.
118175*/
118176static void charFunc(
118177 sqlite3_context *context,
118178 int argc,
118179 sqlite3_value **argv
118180){
118181 unsigned char *z, *zOut;
118182 int i;
118183 zOut = z = sqlite3_malloc64( argc*4+1 );
118184 if( z==0 ){
118185 sqlite3_result_error_nomem(context);
118186 return;
118187 }
118188 for(i=0; i<argc; i++){
118189 sqlite3_int64 x;
118190 unsigned c;
118191 x = sqlite3_value_int64(argv[i]);
118192 if( x<0 || x>0x10ffff ) x = 0xfffd;
118193 c = (unsigned)(x & 0x1fffff);
118194 if( c<0x00080 ){
118195 *zOut++ = (u8)(c&0xFF);
118196 }else if( c<0x00800 ){
118197 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
118198 *zOut++ = 0x80 + (u8)(c & 0x3F);
118199 }else if( c<0x10000 ){
118200 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
118201 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
118202 *zOut++ = 0x80 + (u8)(c & 0x3F);
118203 }else{
118204 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
118205 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
118206 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
118207 *zOut++ = 0x80 + (u8)(c & 0x3F);
118208 } \
118209 }
118210 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
118211}
118212
118213/*
118214** The hex() function. Interpret the argument as a blob. Return
118215** a hexadecimal rendering as text.
118216*/
118217static void hexFunc(
118218 sqlite3_context *context,
118219 int argc,
118220 sqlite3_value **argv
118221){
118222 int i, n;
118223 const unsigned char *pBlob;
118224 char *zHex, *z;
118225 assert( argc==1 );
118226 UNUSED_PARAMETER(argc);
118227 pBlob = sqlite3_value_blob(argv[0]);
118228 n = sqlite3_value_bytes(argv[0]);
118229 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
118230 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
118231 if( zHex ){
118232 for(i=0; i<n; i++, pBlob++){
118233 unsigned char c = *pBlob;
118234 *(z++) = hexdigits[(c>>4)&0xf];
118235 *(z++) = hexdigits[c&0xf];
118236 }
118237 *z = 0;
118238 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
118239 }
118240}
118241
118242/*
118243** The zeroblob(N) function returns a zero-filled blob of size N bytes.
118244*/
118245static void zeroblobFunc(
118246 sqlite3_context *context,
118247 int argc,
118248 sqlite3_value **argv
118249){
118250 i64 n;
118251 int rc;
118252 assert( argc==1 );
118253 UNUSED_PARAMETER(argc);
118254 n = sqlite3_value_int64(argv[0]);
118255 if( n<0 ) n = 0;
118256 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
118257 if( rc ){
118258 sqlite3_result_error_code(context, rc);
118259 }
118260}
118261
118262/*
118263** The replace() function. Three arguments are all strings: call
118264** them A, B, and C. The result is also a string which is derived
118265** from A by replacing every occurrence of B with C. The match
118266** must be exact. Collating sequences are not used.
118267*/
118268static void replaceFunc(
118269 sqlite3_context *context,
118270 int argc,
118271 sqlite3_value **argv
118272){
118273 const unsigned char *zStr; /* The input string A */
118274 const unsigned char *zPattern; /* The pattern string B */
118275 const unsigned char *zRep; /* The replacement string C */
118276 unsigned char *zOut; /* The output */
118277 int nStr; /* Size of zStr */
118278 int nPattern; /* Size of zPattern */
118279 int nRep; /* Size of zRep */
118280 i64 nOut; /* Maximum size of zOut */
118281 int loopLimit; /* Last zStr[] that might match zPattern[] */
118282 int i, j; /* Loop counters */
118283 unsigned cntExpand; /* Number zOut expansions */
118284 sqlite3 *db = sqlite3_context_db_handle(context);
118285
118286 assert( argc==3 );
118287 UNUSED_PARAMETER(argc);
118288 zStr = sqlite3_value_text(argv[0]);
118289 if( zStr==0 ) return;
118290 nStr = sqlite3_value_bytes(argv[0]);
118291 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
118292 zPattern = sqlite3_value_text(argv[1]);
118293 if( zPattern==0 ){
118294 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
118295 || sqlite3_context_db_handle(context)->mallocFailed );
118296 return;
118297 }
118298 if( zPattern[0]==0 ){
118299 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
118300 sqlite3_result_value(context, argv[0]);
118301 return;
118302 }
118303 nPattern = sqlite3_value_bytes(argv[1]);
118304 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
118305 zRep = sqlite3_value_text(argv[2]);
118306 if( zRep==0 ) return;
118307 nRep = sqlite3_value_bytes(argv[2]);
118308 assert( zRep==sqlite3_value_text(argv[2]) );
118309 nOut = nStr + 1;
118310 assert( nOut<SQLITE_MAX_LENGTH );
118311 zOut = contextMalloc(context, (i64)nOut);
118312 if( zOut==0 ){
118313 return;
118314 }
118315 loopLimit = nStr - nPattern;
118316 cntExpand = 0;
118317 for(i=j=0; i<=loopLimit; i++){
118318 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
118319 zOut[j++] = zStr[i];
118320 }else{
118321 if( nRep>nPattern ){
118322 nOut += nRep - nPattern;
118323 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
118324 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
118325 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
118326 sqlite3_result_error_toobig(context);
118327 sqlite3_free(zOut);
118328 return;
118329 }
118330 cntExpand++;
118331 if( (cntExpand&(cntExpand-1))==0 ){
118332 /* Grow the size of the output buffer only on substitutions
118333 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
118334 u8 *zOld;
118335 zOld = zOut;
118336 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
118337 if( zOut==0 ){
118338 sqlite3_result_error_nomem(context);
118339 sqlite3_free(zOld);
118340 return;
118341 }
118342 }
118343 }
118344 memcpy(&zOut[j], zRep, nRep);
118345 j += nRep;
118346 i += nPattern-1;
118347 }
118348 }
118349 assert( j+nStr-i+1<=nOut );
118350 memcpy(&zOut[j], &zStr[i], nStr-i);
118351 j += nStr - i;
118352 assert( j<=nOut );
118353 zOut[j] = 0;
118354 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
118355}
118356
118357/*
118358** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
118359** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
118360*/
118361static void trimFunc(
118362 sqlite3_context *context,
118363 int argc,
118364 sqlite3_value **argv
118365){
118366 const unsigned char *zIn; /* Input string */
118367 const unsigned char *zCharSet; /* Set of characters to trim */
118368 int nIn; /* Number of bytes in input */
118369 int flags; /* 1: trimleft 2: trimright 3: trim */
118370 int i; /* Loop counter */
118371 unsigned char *aLen = 0; /* Length of each character in zCharSet */
118372 unsigned char **azChar = 0; /* Individual characters in zCharSet */
118373 int nChar; /* Number of characters in zCharSet */
118374
118375 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
118376 return;
118377 }
118378 zIn = sqlite3_value_text(argv[0]);
118379 if( zIn==0 ) return;
118380 nIn = sqlite3_value_bytes(argv[0]);
118381 assert( zIn==sqlite3_value_text(argv[0]) );
118382 if( argc==1 ){
118383 static const unsigned char lenOne[] = { 1 };
118384 static unsigned char * const azOne[] = { (u8*)" " };
118385 nChar = 1;
118386 aLen = (u8*)lenOne;
118387 azChar = (unsigned char **)azOne;
118388 zCharSet = 0;
118389 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
118390 return;
118391 }else{
118392 const unsigned char *z;
118393 for(z=zCharSet, nChar=0; *z; nChar++){
118394 SQLITE_SKIP_UTF8(z);
118395 }
118396 if( nChar>0 ){
118397 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
118398 if( azChar==0 ){
118399 return;
118400 }
118401 aLen = (unsigned char*)&azChar[nChar];
118402 for(z=zCharSet, nChar=0; *z; nChar++){
118403 azChar[nChar] = (unsigned char *)z;
118404 SQLITE_SKIP_UTF8(z);
118405 aLen[nChar] = (u8)(z - azChar[nChar]);
118406 }
118407 }
118408 }
118409 if( nChar>0 ){
118410 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
118411 if( flags & 1 ){
118412 while( nIn>0 ){
118413 int len = 0;
118414 for(i=0; i<nChar; i++){
118415 len = aLen[i];
118416 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
118417 }
118418 if( i>=nChar ) break;
118419 zIn += len;
118420 nIn -= len;
118421 }
118422 }
118423 if( flags & 2 ){
118424 while( nIn>0 ){
118425 int len = 0;
118426 for(i=0; i<nChar; i++){
118427 len = aLen[i];
118428 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
118429 }
118430 if( i>=nChar ) break;
118431 nIn -= len;
118432 }
118433 }
118434 if( zCharSet ){
118435 sqlite3_free(azChar);
118436 }
118437 }
118438 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
118439}
118440
118441
118442#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
118443/*
118444** The "unknown" function is automatically substituted in place of
118445** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
118446** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
118447** When the "sqlite3" command-line shell is built using this functionality,
118448** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
118449** involving application-defined functions to be examined in a generic
118450** sqlite3 shell.
118451*/
118452static void unknownFunc(
118453 sqlite3_context *context,
118454 int argc,
118455 sqlite3_value **argv
118456){
118457 /* no-op */
118458}
118459#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
118460
118461
118462/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
118463** is only available if the SQLITE_SOUNDEX compile-time option is used
118464** when SQLite is built.
118465*/
118466#ifdef SQLITE_SOUNDEX
118467/*
118468** Compute the soundex encoding of a word.
118469**
118470** IMP: R-59782-00072 The soundex(X) function returns a string that is the
118471** soundex encoding of the string X.
118472*/
118473static void soundexFunc(
118474 sqlite3_context *context,
118475 int argc,
118476 sqlite3_value **argv
118477){
118478 char zResult[8];
118479 const u8 *zIn;
118480 int i, j;
118481 static const unsigned char iCode[] = {
118482 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118486 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
118487 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
118488 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
118489 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
118490 };
118491 assert( argc==1 );
118492 zIn = (u8*)sqlite3_value_text(argv[0]);
118493 if( zIn==0 ) zIn = (u8*)"";
118494 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
118495 if( zIn[i] ){
118496 u8 prevcode = iCode[zIn[i]&0x7f];
118497 zResult[0] = sqlite3Toupper(zIn[i]);
118498 for(j=1; j<4 && zIn[i]; i++){
118499 int code = iCode[zIn[i]&0x7f];
118500 if( code>0 ){
118501 if( code!=prevcode ){
118502 prevcode = code;
118503 zResult[j++] = code + '0';
118504 }
118505 }else{
118506 prevcode = 0;
118507 }
118508 }
118509 while( j<4 ){
118510 zResult[j++] = '0';
118511 }
118512 zResult[j] = 0;
118513 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
118514 }else{
118515 /* IMP: R-64894-50321 The string "?000" is returned if the argument
118516 ** is NULL or contains no ASCII alphabetic characters. */
118517 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
118518 }
118519}
118520#endif /* SQLITE_SOUNDEX */
118521
118522#ifndef SQLITE_OMIT_LOAD_EXTENSION
118523/*
118524** A function that loads a shared-library extension then returns NULL.
118525*/
118526static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
118527 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
118528 const char *zProc;
118529 sqlite3 *db = sqlite3_context_db_handle(context);
118530 char *zErrMsg = 0;
118531
118532 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
118533 ** flag is set. See the sqlite3_enable_load_extension() API.
118534 */
118535 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
118536 sqlite3_result_error(context, "not authorized", -1);
118537 return;
118538 }
118539
118540 if( argc==2 ){
118541 zProc = (const char *)sqlite3_value_text(argv[1]);
118542 }else{
118543 zProc = 0;
118544 }
118545 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
118546 sqlite3_result_error(context, zErrMsg, -1);
118547 sqlite3_free(zErrMsg);
118548 }
118549}
118550#endif
118551
118552
118553/*
118554** An instance of the following structure holds the context of a
118555** sum() or avg() aggregate computation.
118556*/
118557typedef struct SumCtx SumCtx;
118558struct SumCtx {
118559 double rSum; /* Floating point sum */
118560 i64 iSum; /* Integer sum */
118561 i64 cnt; /* Number of elements summed */
118562 u8 overflow; /* True if integer overflow seen */
118563 u8 approx; /* True if non-integer value was input to the sum */
118564};
118565
118566/*
118567** Routines used to compute the sum, average, and total.
118568**
118569** The SUM() function follows the (broken) SQL standard which means
118570** that it returns NULL if it sums over no inputs. TOTAL returns
118571** 0.0 in that case. In addition, TOTAL always returns a float where
118572** SUM might return an integer if it never encounters a floating point
118573** value. TOTAL never fails, but SUM might through an exception if
118574** it overflows an integer.
118575*/
118576static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
118577 SumCtx *p;
118578 int type;
118579 assert( argc==1 );
118580 UNUSED_PARAMETER(argc);
118581 p = sqlite3_aggregate_context(context, sizeof(*p));
118582 type = sqlite3_value_numeric_type(argv[0]);
118583 if( p && type!=SQLITE_NULL ){
118584 p->cnt++;
118585 if( type==SQLITE_INTEGER ){
118586 i64 v = sqlite3_value_int64(argv[0]);
118587 p->rSum += v;
118588 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
118589 p->approx = p->overflow = 1;
118590 }
118591 }else{
118592 p->rSum += sqlite3_value_double(argv[0]);
118593 p->approx = 1;
118594 }
118595 }
118596}
118597#ifndef SQLITE_OMIT_WINDOWFUNC
118598static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
118599 SumCtx *p;
118600 int type;
118601 assert( argc==1 );
118602 UNUSED_PARAMETER(argc);
118603 p = sqlite3_aggregate_context(context, sizeof(*p));
118604 type = sqlite3_value_numeric_type(argv[0]);
118605 /* p is always non-NULL because sumStep() will have been called first
118606 ** to initialize it */
118607 if( ALWAYS(p) && type!=SQLITE_NULL ){
118608 assert( p->cnt>0 );
118609 p->cnt--;
118610 assert( type==SQLITE_INTEGER || p->approx );
118611 if( type==SQLITE_INTEGER && p->approx==0 ){
118612 i64 v = sqlite3_value_int64(argv[0]);
118613 p->rSum -= v;
118614 p->iSum -= v;
118615 }else{
118616 p->rSum -= sqlite3_value_double(argv[0]);
118617 }
118618 }
118619}
118620#else
118621# define sumInverse 0
118622#endif /* SQLITE_OMIT_WINDOWFUNC */
118623static void sumFinalize(sqlite3_context *context){
118624 SumCtx *p;
118625 p = sqlite3_aggregate_context(context, 0);
118626 if( p && p->cnt>0 ){
118627 if( p->overflow ){
118628 sqlite3_result_error(context,"integer overflow",-1);
118629 }else if( p->approx ){
118630 sqlite3_result_double(context, p->rSum);
118631 }else{
118632 sqlite3_result_int64(context, p->iSum);
118633 }
118634 }
118635}
118636static void avgFinalize(sqlite3_context *context){
118637 SumCtx *p;
118638 p = sqlite3_aggregate_context(context, 0);
118639 if( p && p->cnt>0 ){
118640 sqlite3_result_double(context, p->rSum/(double)p->cnt);
118641 }
118642}
118643static void totalFinalize(sqlite3_context *context){
118644 SumCtx *p;
118645 p = sqlite3_aggregate_context(context, 0);
118646 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
118647 sqlite3_result_double(context, p ? p->rSum : (double)0);
118648}
118649
118650/*
118651** The following structure keeps track of state information for the
118652** count() aggregate function.
118653*/
118654typedef struct CountCtx CountCtx;
118655struct CountCtx {
118656 i64 n;
118657#ifdef SQLITE_DEBUG
118658 int bInverse; /* True if xInverse() ever called */
118659#endif
118660};
118661
118662/*
118663** Routines to implement the count() aggregate function.
118664*/
118665static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
118666 CountCtx *p;
118667 p = sqlite3_aggregate_context(context, sizeof(*p));
118668 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
118669 p->n++;
118670 }
118671
118672#ifndef SQLITE_OMIT_DEPRECATED
118673 /* The sqlite3_aggregate_count() function is deprecated. But just to make
118674 ** sure it still operates correctly, verify that its count agrees with our
118675 ** internal count when using count(*) and when the total count can be
118676 ** expressed as a 32-bit integer. */
118677 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
118678 || p->n==sqlite3_aggregate_count(context) );
118679#endif
118680}
118681static void countFinalize(sqlite3_context *context){
118682 CountCtx *p;
118683 p = sqlite3_aggregate_context(context, 0);
118684 sqlite3_result_int64(context, p ? p->n : 0);
118685}
118686#ifndef SQLITE_OMIT_WINDOWFUNC
118687static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
118688 CountCtx *p;
118689 p = sqlite3_aggregate_context(ctx, sizeof(*p));
118690 /* p is always non-NULL since countStep() will have been called first */
118691 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
118692 p->n--;
118693#ifdef SQLITE_DEBUG
118694 p->bInverse = 1;
118695#endif
118696 }
118697}
118698#else
118699# define countInverse 0
118700#endif /* SQLITE_OMIT_WINDOWFUNC */
118701
118702/*
118703** Routines to implement min() and max() aggregate functions.
118704*/
118705static void minmaxStep(
118706 sqlite3_context *context,
118707 int NotUsed,
118708 sqlite3_value **argv
118709){
118710 Mem *pArg = (Mem *)argv[0];
118711 Mem *pBest;
118712 UNUSED_PARAMETER(NotUsed);
118713
118714 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
118715 if( !pBest ) return;
118716
118717 if( sqlite3_value_type(pArg)==SQLITE_NULL ){
118718 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
118719 }else if( pBest->flags ){
118720 int max;
118721 int cmp;
118722 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
118723 /* This step function is used for both the min() and max() aggregates,
118724 ** the only difference between the two being that the sense of the
118725 ** comparison is inverted. For the max() aggregate, the
118726 ** sqlite3_user_data() function returns (void *)-1. For min() it
118727 ** returns (void *)db, where db is the sqlite3* database pointer.
118728 ** Therefore the next statement sets variable 'max' to 1 for the max()
118729 ** aggregate, or 0 for min().
118730 */
118731 max = sqlite3_user_data(context)!=0;
118732 cmp = sqlite3MemCompare(pBest, pArg, pColl);
118733 if( (max && cmp<0) || (!max && cmp>0) ){
118734 sqlite3VdbeMemCopy(pBest, pArg);
118735 }else{
118736 sqlite3SkipAccumulatorLoad(context);
118737 }
118738 }else{
118739 pBest->db = sqlite3_context_db_handle(context);
118740 sqlite3VdbeMemCopy(pBest, pArg);
118741 }
118742}
118743static void minMaxValueFinalize(sqlite3_context *context, int bValue){
118744 sqlite3_value *pRes;
118745 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
118746 if( pRes ){
118747 if( pRes->flags ){
118748 sqlite3_result_value(context, pRes);
118749 }
118750 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
118751 }
118752}
118753#ifndef SQLITE_OMIT_WINDOWFUNC
118754static void minMaxValue(sqlite3_context *context){
118755 minMaxValueFinalize(context, 1);
118756}
118757#else
118758# define minMaxValue 0
118759#endif /* SQLITE_OMIT_WINDOWFUNC */
118760static void minMaxFinalize(sqlite3_context *context){
118761 minMaxValueFinalize(context, 0);
118762}
118763
118764/*
118765** group_concat(EXPR, ?SEPARATOR?)
118766*/
118767static void groupConcatStep(
118768 sqlite3_context *context,
118769 int argc,
118770 sqlite3_value **argv
118771){
118772 const char *zVal;
118773 StrAccum *pAccum;
118774 const char *zSep;
118775 int nVal, nSep;
118776 assert( argc==1 || argc==2 );
118777 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
118778 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
118779
118780 if( pAccum ){
118781 sqlite3 *db = sqlite3_context_db_handle(context);
118782 int firstTerm = pAccum->mxAlloc==0;
118783 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
118784 if( !firstTerm ){
118785 if( argc==2 ){
118786 zSep = (char*)sqlite3_value_text(argv[1]);
118787 nSep = sqlite3_value_bytes(argv[1]);
118788 }else{
118789 zSep = ",";
118790 nSep = 1;
118791 }
118792 if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
118793 }
118794 zVal = (char*)sqlite3_value_text(argv[0]);
118795 nVal = sqlite3_value_bytes(argv[0]);
118796 if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
118797 }
118798}
118799#ifndef SQLITE_OMIT_WINDOWFUNC
118800static void groupConcatInverse(
118801 sqlite3_context *context,
118802 int argc,
118803 sqlite3_value **argv
118804){
118805 int n;
118806 StrAccum *pAccum;
118807 assert( argc==1 || argc==2 );
118808 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
118809 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
118810 /* pAccum is always non-NULL since groupConcatStep() will have always
118811 ** run frist to initialize it */
118812 if( ALWAYS(pAccum) ){
118813 n = sqlite3_value_bytes(argv[0]);
118814 if( argc==2 ){
118815 n += sqlite3_value_bytes(argv[1]);
118816 }else{
118817 n++;
118818 }
118819 if( n>=(int)pAccum->nChar ){
118820 pAccum->nChar = 0;
118821 }else{
118822 pAccum->nChar -= n;
118823 memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
118824 }
118825 if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
118826 }
118827}
118828#else
118829# define groupConcatInverse 0
118830#endif /* SQLITE_OMIT_WINDOWFUNC */
118831static void groupConcatFinalize(sqlite3_context *context){
118832 StrAccum *pAccum;
118833 pAccum = sqlite3_aggregate_context(context, 0);
118834 if( pAccum ){
118835 if( pAccum->accError==SQLITE_TOOBIG ){
118836 sqlite3_result_error_toobig(context);
118837 }else if( pAccum->accError==SQLITE_NOMEM ){
118838 sqlite3_result_error_nomem(context);
118839 }else{
118840 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
118841 sqlite3_free);
118842 }
118843 }
118844}
118845#ifndef SQLITE_OMIT_WINDOWFUNC
118846static void groupConcatValue(sqlite3_context *context){
118847 sqlite3_str *pAccum;
118848 pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
118849 if( pAccum ){
118850 if( pAccum->accError==SQLITE_TOOBIG ){
118851 sqlite3_result_error_toobig(context);
118852 }else if( pAccum->accError==SQLITE_NOMEM ){
118853 sqlite3_result_error_nomem(context);
118854 }else{
118855 const char *zText = sqlite3_str_value(pAccum);
118856 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
118857 }
118858 }
118859}
118860#else
118861# define groupConcatValue 0
118862#endif /* SQLITE_OMIT_WINDOWFUNC */
118863
118864/*
118865** This routine does per-connection function registration. Most
118866** of the built-in functions above are part of the global function set.
118867** This routine only deals with those that are not global.
118868*/
118869SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
118870 int rc = sqlite3_overload_function(db, "MATCH", 2);
118871 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
118872 if( rc==SQLITE_NOMEM ){
118873 sqlite3OomFault(db);
118874 }
118875}
118876
118877/*
118878** Re-register the built-in LIKE functions. The caseSensitive
118879** parameter determines whether or not the LIKE operator is case
118880** sensitive.
118881*/
118882SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
118883 struct compareInfo *pInfo;
118884 int flags;
118885 if( caseSensitive ){
118886 pInfo = (struct compareInfo*)&likeInfoAlt;
118887 flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
118888 }else{
118889 pInfo = (struct compareInfo*)&likeInfoNorm;
118890 flags = SQLITE_FUNC_LIKE;
118891 }
118892 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
118893 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
118894 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
118895 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
118896}
118897
118898/*
118899** pExpr points to an expression which implements a function. If
118900** it is appropriate to apply the LIKE optimization to that function
118901** then set aWc[0] through aWc[2] to the wildcard characters and the
118902** escape character and then return TRUE. If the function is not a
118903** LIKE-style function then return FALSE.
118904**
118905** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
118906** operator if c is a string literal that is exactly one byte in length.
118907** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
118908** no ESCAPE clause.
118909**
118910** *pIsNocase is set to true if uppercase and lowercase are equivalent for
118911** the function (default for LIKE). If the function makes the distinction
118912** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
118913** false.
118914*/
118915SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
118916 FuncDef *pDef;
118917 int nExpr;
118918 if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
118919 return 0;
118920 }
118921 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
118922 nExpr = pExpr->x.pList->nExpr;
118923 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
118924#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
118925 if( pDef==0 ) return 0;
118926#endif
118927 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
118928 return 0;
118929 }
118930
118931 /* The memcpy() statement assumes that the wildcard characters are
118932 ** the first three statements in the compareInfo structure. The
118933 ** asserts() that follow verify that assumption
118934 */
118935 memcpy(aWc, pDef->pUserData, 3);
118936 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
118937 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
118938 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
118939
118940 if( nExpr<3 ){
118941 aWc[3] = 0;
118942 }else{
118943 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
118944 char *zEscape;
118945 if( pEscape->op!=TK_STRING ) return 0;
118946 zEscape = pEscape->u.zToken;
118947 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
118948 if( zEscape[0]==aWc[0] ) return 0;
118949 if( zEscape[0]==aWc[1] ) return 0;
118950 aWc[3] = zEscape[0];
118951 }
118952
118953 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
118954 return 1;
118955}
118956
118957/*
118958** All of the FuncDef structures in the aBuiltinFunc[] array above
118959** to the global function hash table. This occurs at start-time (as
118960** a consequence of calling sqlite3_initialize()).
118961**
118962** After this routine runs
118963*/
118964SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
118965 /*
118966 ** The following array holds FuncDef structures for all of the functions
118967 ** defined in this file.
118968 **
118969 ** The array cannot be constant since changes are made to the
118970 ** FuncDef.pHash elements at start-time. The elements of this array
118971 ** are read-only after initialization is complete.
118972 **
118973 ** For peak efficiency, put the most frequently used function last.
118974 */
118975 static FuncDef aBuiltinFunc[] = {
118976/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
118977 TEST_FUNC(implies_nonnull_row, 2, INLINEFUNC_implies_nonnull_row, 0),
118978 TEST_FUNC(expr_compare, 2, INLINEFUNC_expr_compare, 0),
118979 TEST_FUNC(expr_implies_expr, 2, INLINEFUNC_expr_implies_expr, 0),
118980#ifdef SQLITE_DEBUG
118981 TEST_FUNC(affinity, 1, INLINEFUNC_affinity, 0),
118982#endif
118983/***** Regular functions *****/
118984#ifdef SQLITE_SOUNDEX
118985 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
118986#endif
118987#ifndef SQLITE_OMIT_LOAD_EXTENSION
118988 SFUNCTION(load_extension, 1, 0, 0, loadExt ),
118989 SFUNCTION(load_extension, 2, 0, 0, loadExt ),
118990#endif
118991#if SQLITE_USER_AUTHENTICATION
118992 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
118993#endif
118994#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
118995 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
118996 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
118997#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
118998 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
118999 INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
119000 INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
119001#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
119002 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|
119003 SQLITE_FUNC_TYPEOF),
119004#endif
119005 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
119006 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
119007 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
119008 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
119009 FUNCTION(trim, 1, 3, 0, trimFunc ),
119010 FUNCTION(trim, 2, 3, 0, trimFunc ),
119011 FUNCTION(min, -1, 0, 1, minmaxFunc ),
119012 FUNCTION(min, 0, 0, 1, 0 ),
119013 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
119014 SQLITE_FUNC_MINMAX ),
119015 FUNCTION(max, -1, 1, 1, minmaxFunc ),
119016 FUNCTION(max, 0, 1, 1, 0 ),
119017 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
119018 SQLITE_FUNC_MINMAX ),
119019 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
119020 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
119021 FUNCTION(instr, 2, 0, 0, instrFunc ),
119022 FUNCTION(printf, -1, 0, 0, printfFunc ),
119023 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
119024 FUNCTION(char, -1, 0, 0, charFunc ),
119025 FUNCTION(abs, 1, 0, 0, absFunc ),
119026#ifndef SQLITE_OMIT_FLOATING_POINT
119027 FUNCTION(round, 1, 0, 0, roundFunc ),
119028 FUNCTION(round, 2, 0, 0, roundFunc ),
119029#endif
119030 FUNCTION(upper, 1, 0, 0, upperFunc ),
119031 FUNCTION(lower, 1, 0, 0, lowerFunc ),
119032 FUNCTION(hex, 1, 0, 0, hexFunc ),
119033 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ),
119034 VFUNCTION(random, 0, 0, 0, randomFunc ),
119035 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
119036 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
119037 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
119038 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
119039 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
119040 FUNCTION(quote, 1, 0, 0, quoteFunc ),
119041 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
119042 VFUNCTION(changes, 0, 0, 0, changes ),
119043 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
119044 FUNCTION(replace, 3, 0, 0, replaceFunc ),
119045 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
119046 FUNCTION(substr, 2, 0, 0, substrFunc ),
119047 FUNCTION(substr, 3, 0, 0, substrFunc ),
119048 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
119049 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
119050 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
119051 WAGGREGATE(count, 0,0,0, countStep,
119052 countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ),
119053 WAGGREGATE(count, 1,0,0, countStep,
119054 countFinalize, countFinalize, countInverse, 0 ),
119055 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
119056 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
119057 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
119058 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
119059
119060 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
119061#ifdef SQLITE_CASE_SENSITIVE_LIKE
119062 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
119063 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
119064#else
119065 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
119066 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
119067#endif
119068#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
119069 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
119070#endif
119071 FUNCTION(coalesce, 1, 0, 0, 0 ),
119072 FUNCTION(coalesce, 0, 0, 0, 0 ),
119073 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
119074 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ),
119075 };
119076#ifndef SQLITE_OMIT_ALTERTABLE
119077 sqlite3AlterFunctions();
119078#endif
119079 sqlite3WindowFunctions();
119080 sqlite3RegisterDateTimeFunctions();
119081 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
119082
119083#if 0 /* Enable to print out how the built-in functions are hashed */
119084 {
119085 int i;
119086 FuncDef *p;
119087 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
119088 printf("FUNC-HASH %02d:", i);
119089 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
119090 int n = sqlite3Strlen30(p->zName);
119091 int h = p->zName[0] + n;
119092 printf(" %s(%d)", p->zName, h);
119093 }
119094 printf("\n");
119095 }
119096 }
119097#endif
119098}
119099
119100/************** End of func.c ************************************************/
119101/************** Begin file fkey.c ********************************************/
119102/*
119103**
119104** The author disclaims copyright to this source code. In place of
119105** a legal notice, here is a blessing:
119106**
119107** May you do good and not evil.
119108** May you find forgiveness for yourself and forgive others.
119109** May you share freely, never taking more than you give.
119110**
119111*************************************************************************
119112** This file contains code used by the compiler to add foreign key
119113** support to compiled SQL statements.
119114*/
119115/* #include "sqliteInt.h" */
119116
119117#ifndef SQLITE_OMIT_FOREIGN_KEY
119118#ifndef SQLITE_OMIT_TRIGGER
119119
119120/*
119121** Deferred and Immediate FKs
119122** --------------------------
119123**
119124** Foreign keys in SQLite come in two flavours: deferred and immediate.
119125** If an immediate foreign key constraint is violated,
119126** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
119127** statement transaction rolled back. If a
119128** deferred foreign key constraint is violated, no action is taken
119129** immediately. However if the application attempts to commit the
119130** transaction before fixing the constraint violation, the attempt fails.
119131**
119132** Deferred constraints are implemented using a simple counter associated
119133** with the database handle. The counter is set to zero each time a
119134** database transaction is opened. Each time a statement is executed
119135** that causes a foreign key violation, the counter is incremented. Each
119136** time a statement is executed that removes an existing violation from
119137** the database, the counter is decremented. When the transaction is
119138** committed, the commit fails if the current value of the counter is
119139** greater than zero. This scheme has two big drawbacks:
119140**
119141** * When a commit fails due to a deferred foreign key constraint,
119142** there is no way to tell which foreign constraint is not satisfied,
119143** or which row it is not satisfied for.
119144**
119145** * If the database contains foreign key violations when the
119146** transaction is opened, this may cause the mechanism to malfunction.
119147**
119148** Despite these problems, this approach is adopted as it seems simpler
119149** than the alternatives.
119150**
119151** INSERT operations:
119152**
119153** I.1) For each FK for which the table is the child table, search
119154** the parent table for a match. If none is found increment the
119155** constraint counter.
119156**
119157** I.2) For each FK for which the table is the parent table,
119158** search the child table for rows that correspond to the new
119159** row in the parent table. Decrement the counter for each row
119160** found (as the constraint is now satisfied).
119161**
119162** DELETE operations:
119163**
119164** D.1) For each FK for which the table is the child table,
119165** search the parent table for a row that corresponds to the
119166** deleted row in the child table. If such a row is not found,
119167** decrement the counter.
119168**
119169** D.2) For each FK for which the table is the parent table, search
119170** the child table for rows that correspond to the deleted row
119171** in the parent table. For each found increment the counter.
119172**
119173** UPDATE operations:
119174**
119175** An UPDATE command requires that all 4 steps above are taken, but only
119176** for FK constraints for which the affected columns are actually
119177** modified (values must be compared at runtime).
119178**
119179** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
119180** This simplifies the implementation a bit.
119181**
119182** For the purposes of immediate FK constraints, the OR REPLACE conflict
119183** resolution is considered to delete rows before the new row is inserted.
119184** If a delete caused by OR REPLACE violates an FK constraint, an exception
119185** is thrown, even if the FK constraint would be satisfied after the new
119186** row is inserted.
119187**
119188** Immediate constraints are usually handled similarly. The only difference
119189** is that the counter used is stored as part of each individual statement
119190** object (struct Vdbe). If, after the statement has run, its immediate
119191** constraint counter is greater than zero,
119192** it returns SQLITE_CONSTRAINT_FOREIGNKEY
119193** and the statement transaction is rolled back. An exception is an INSERT
119194** statement that inserts a single row only (no triggers). In this case,
119195** instead of using a counter, an exception is thrown immediately if the
119196** INSERT violates a foreign key constraint. This is necessary as such
119197** an INSERT does not open a statement transaction.
119198**
119199** TODO: How should dropping a table be handled? How should renaming a
119200** table be handled?
119201**
119202**
119203** Query API Notes
119204** ---------------
119205**
119206** Before coding an UPDATE or DELETE row operation, the code-generator
119207** for those two operations needs to know whether or not the operation
119208** requires any FK processing and, if so, which columns of the original
119209** row are required by the FK processing VDBE code (i.e. if FKs were
119210** implemented using triggers, which of the old.* columns would be
119211** accessed). No information is required by the code-generator before
119212** coding an INSERT operation. The functions used by the UPDATE/DELETE
119213** generation code to query for this information are:
119214**
119215** sqlite3FkRequired() - Test to see if FK processing is required.
119216** sqlite3FkOldmask() - Query for the set of required old.* columns.
119217**
119218**
119219** Externally accessible module functions
119220** --------------------------------------
119221**
119222** sqlite3FkCheck() - Check for foreign key violations.
119223** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
119224** sqlite3FkDelete() - Delete an FKey structure.
119225*/
119226
119227/*
119228** VDBE Calling Convention
119229** -----------------------
119230**
119231** Example:
119232**
119233** For the following INSERT statement:
119234**
119235** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
119236** INSERT INTO t1 VALUES(1, 2, 3.1);
119237**
119238** Register (x): 2 (type integer)
119239** Register (x+1): 1 (type integer)
119240** Register (x+2): NULL (type NULL)
119241** Register (x+3): 3.1 (type real)
119242*/
119243
119244/*
119245** A foreign key constraint requires that the key columns in the parent
119246** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
119247** Given that pParent is the parent table for foreign key constraint pFKey,
119248** search the schema for a unique index on the parent key columns.
119249**
119250** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
119251** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
119252** is set to point to the unique index.
119253**
119254** If the parent key consists of a single column (the foreign key constraint
119255** is not a composite foreign key), output variable *paiCol is set to NULL.
119256** Otherwise, it is set to point to an allocated array of size N, where
119257** N is the number of columns in the parent key. The first element of the
119258** array is the index of the child table column that is mapped by the FK
119259** constraint to the parent table column stored in the left-most column
119260** of index *ppIdx. The second element of the array is the index of the
119261** child table column that corresponds to the second left-most column of
119262** *ppIdx, and so on.
119263**
119264** If the required index cannot be found, either because:
119265**
119266** 1) The named parent key columns do not exist, or
119267**
119268** 2) The named parent key columns do exist, but are not subject to a
119269** UNIQUE or PRIMARY KEY constraint, or
119270**
119271** 3) No parent key columns were provided explicitly as part of the
119272** foreign key definition, and the parent table does not have a
119273** PRIMARY KEY, or
119274**
119275** 4) No parent key columns were provided explicitly as part of the
119276** foreign key definition, and the PRIMARY KEY of the parent table
119277** consists of a different number of columns to the child key in
119278** the child table.
119279**
119280** then non-zero is returned, and a "foreign key mismatch" error loaded
119281** into pParse. If an OOM error occurs, non-zero is returned and the
119282** pParse->db->mallocFailed flag is set.
119283*/
119284SQLITE_PRIVATE int sqlite3FkLocateIndex(
119285 Parse *pParse, /* Parse context to store any error in */
119286 Table *pParent, /* Parent table of FK constraint pFKey */
119287 FKey *pFKey, /* Foreign key to find index for */
119288 Index **ppIdx, /* OUT: Unique index on parent table */
119289 int **paiCol /* OUT: Map of index columns in pFKey */
119290){
119291 Index *pIdx = 0; /* Value to return via *ppIdx */
119292 int *aiCol = 0; /* Value to return via *paiCol */
119293 int nCol = pFKey->nCol; /* Number of columns in parent key */
119294 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
119295
119296 /* The caller is responsible for zeroing output parameters. */
119297 assert( ppIdx && *ppIdx==0 );
119298 assert( !paiCol || *paiCol==0 );
119299 assert( pParse );
119300
119301 /* If this is a non-composite (single column) foreign key, check if it
119302 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
119303 ** and *paiCol set to zero and return early.
119304 **
119305 ** Otherwise, for a composite foreign key (more than one column), allocate
119306 ** space for the aiCol array (returned via output parameter *paiCol).
119307 ** Non-composite foreign keys do not require the aiCol array.
119308 */
119309 if( nCol==1 ){
119310 /* The FK maps to the IPK if any of the following are true:
119311 **
119312 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
119313 ** mapped to the primary key of table pParent, or
119314 ** 2) The FK is explicitly mapped to a column declared as INTEGER
119315 ** PRIMARY KEY.
119316 */
119317 if( pParent->iPKey>=0 ){
119318 if( !zKey ) return 0;
119319 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
119320 }
119321 }else if( paiCol ){
119322 assert( nCol>1 );
119323 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
119324 if( !aiCol ) return 1;
119325 *paiCol = aiCol;
119326 }
119327
119328 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
119329 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
119330 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
119331 ** of columns. If each indexed column corresponds to a foreign key
119332 ** column of pFKey, then this index is a winner. */
119333
119334 if( zKey==0 ){
119335 /* If zKey is NULL, then this foreign key is implicitly mapped to
119336 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
119337 ** identified by the test. */
119338 if( IsPrimaryKeyIndex(pIdx) ){
119339 if( aiCol ){
119340 int i;
119341 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
119342 }
119343 break;
119344 }
119345 }else{
119346 /* If zKey is non-NULL, then this foreign key was declared to
119347 ** map to an explicit list of columns in table pParent. Check if this
119348 ** index matches those columns. Also, check that the index uses
119349 ** the default collation sequences for each column. */
119350 int i, j;
119351 for(i=0; i<nCol; i++){
119352 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
119353 const char *zDfltColl; /* Def. collation for column */
119354 char *zIdxCol; /* Name of indexed column */
119355
119356 if( iCol<0 ) break; /* No foreign keys against expression indexes */
119357
119358 /* If the index uses a collation sequence that is different from
119359 ** the default collation sequence for the column, this index is
119360 ** unusable. Bail out early in this case. */
119361 zDfltColl = pParent->aCol[iCol].zColl;
119362 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
119363 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
119364
119365 zIdxCol = pParent->aCol[iCol].zName;
119366 for(j=0; j<nCol; j++){
119367 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
119368 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
119369 break;
119370 }
119371 }
119372 if( j==nCol ) break;
119373 }
119374 if( i==nCol ) break; /* pIdx is usable */
119375 }
119376 }
119377 }
119378
119379 if( !pIdx ){
119380 if( !pParse->disableTriggers ){
119381 sqlite3ErrorMsg(pParse,
119382 "foreign key mismatch - \"%w\" referencing \"%w\"",
119383 pFKey->pFrom->zName, pFKey->zTo);
119384 }
119385 sqlite3DbFree(pParse->db, aiCol);
119386 return 1;
119387 }
119388
119389 *ppIdx = pIdx;
119390 return 0;
119391}
119392
119393/*
119394** This function is called when a row is inserted into or deleted from the
119395** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
119396** on the child table of pFKey, this function is invoked twice for each row
119397** affected - once to "delete" the old row, and then again to "insert" the
119398** new row.
119399**
119400** Each time it is called, this function generates VDBE code to locate the
119401** row in the parent table that corresponds to the row being inserted into
119402** or deleted from the child table. If the parent row can be found, no
119403** special action is taken. Otherwise, if the parent row can *not* be
119404** found in the parent table:
119405**
119406** Operation | FK type | Action taken
119407** --------------------------------------------------------------------------
119408** INSERT immediate Increment the "immediate constraint counter".
119409**
119410** DELETE immediate Decrement the "immediate constraint counter".
119411**
119412** INSERT deferred Increment the "deferred constraint counter".
119413**
119414** DELETE deferred Decrement the "deferred constraint counter".
119415**
119416** These operations are identified in the comment at the top of this file
119417** (fkey.c) as "I.1" and "D.1".
119418*/
119419static void fkLookupParent(
119420 Parse *pParse, /* Parse context */
119421 int iDb, /* Index of database housing pTab */
119422 Table *pTab, /* Parent table of FK pFKey */
119423 Index *pIdx, /* Unique index on parent key columns in pTab */
119424 FKey *pFKey, /* Foreign key constraint */
119425 int *aiCol, /* Map from parent key columns to child table columns */
119426 int regData, /* Address of array containing child table row */
119427 int nIncr, /* Increment constraint counter by this */
119428 int isIgnore /* If true, pretend pTab contains all NULL values */
119429){
119430 int i; /* Iterator variable */
119431 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
119432 int iCur = pParse->nTab - 1; /* Cursor number to use */
119433 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
119434
119435 sqlite3VdbeVerifyAbortable(v,
119436 (!pFKey->isDeferred
119437 && !(pParse->db->flags & SQLITE_DeferFKs)
119438 && !pParse->pToplevel
119439 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
119440
119441 /* If nIncr is less than zero, then check at runtime if there are any
119442 ** outstanding constraints to resolve. If there are not, there is no need
119443 ** to check if deleting this row resolves any outstanding violations.
119444 **
119445 ** Check if any of the key columns in the child table row are NULL. If
119446 ** any are, then the constraint is considered satisfied. No need to
119447 ** search for a matching row in the parent table. */
119448 if( nIncr<0 ){
119449 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
119450 VdbeCoverage(v);
119451 }
119452 for(i=0; i<pFKey->nCol; i++){
119453 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
119454 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
119455 }
119456
119457 if( isIgnore==0 ){
119458 if( pIdx==0 ){
119459 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
119460 ** column of the parent table (table pTab). */
119461 int iMustBeInt; /* Address of MustBeInt instruction */
119462 int regTemp = sqlite3GetTempReg(pParse);
119463
119464 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
119465 ** apply the affinity of the parent key). If this fails, then there
119466 ** is no matching parent key. Before using MustBeInt, make a copy of
119467 ** the value. Otherwise, the value inserted into the child key column
119468 ** will have INTEGER affinity applied to it, which may not be correct. */
119469 sqlite3VdbeAddOp2(v, OP_SCopy,
119470 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
119471 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
119472 VdbeCoverage(v);
119473
119474 /* If the parent table is the same as the child table, and we are about
119475 ** to increment the constraint-counter (i.e. this is an INSERT operation),
119476 ** then check if the row being inserted matches itself. If so, do not
119477 ** increment the constraint-counter. */
119478 if( pTab==pFKey->pFrom && nIncr==1 ){
119479 sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
119480 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
119481 }
119482
119483 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
119484 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
119485 sqlite3VdbeGoto(v, iOk);
119486 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
119487 sqlite3VdbeJumpHere(v, iMustBeInt);
119488 sqlite3ReleaseTempReg(pParse, regTemp);
119489 }else{
119490 int nCol = pFKey->nCol;
119491 int regTemp = sqlite3GetTempRange(pParse, nCol);
119492 int regRec = sqlite3GetTempReg(pParse);
119493
119494 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
119495 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
119496 for(i=0; i<nCol; i++){
119497 sqlite3VdbeAddOp2(v, OP_Copy,
119498 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
119499 regTemp+i);
119500 }
119501
119502 /* If the parent table is the same as the child table, and we are about
119503 ** to increment the constraint-counter (i.e. this is an INSERT operation),
119504 ** then check if the row being inserted matches itself. If so, do not
119505 ** increment the constraint-counter.
119506 **
119507 ** If any of the parent-key values are NULL, then the row cannot match
119508 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
119509 ** of the parent-key values are NULL (at this point it is known that
119510 ** none of the child key values are).
119511 */
119512 if( pTab==pFKey->pFrom && nIncr==1 ){
119513 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
119514 for(i=0; i<nCol; i++){
119515 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
119516 +1+regData;
119517 int iParent = 1+regData;
119518 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
119519 pIdx->aiColumn[i]);
119520 assert( pIdx->aiColumn[i]>=0 );
119521 assert( aiCol[i]!=pTab->iPKey );
119522 if( pIdx->aiColumn[i]==pTab->iPKey ){
119523 /* The parent key is a composite key that includes the IPK column */
119524 iParent = regData;
119525 }
119526 sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
119527 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
119528 }
119529 sqlite3VdbeGoto(v, iOk);
119530 }
119531
119532 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
119533 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
119534 sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
119535
119536 sqlite3ReleaseTempReg(pParse, regRec);
119537 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
119538 }
119539 }
119540
119541 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
119542 && !pParse->pToplevel
119543 && !pParse->isMultiWrite
119544 ){
119545 /* Special case: If this is an INSERT statement that will insert exactly
119546 ** one row into the table, raise a constraint immediately instead of
119547 ** incrementing a counter. This is necessary as the VM code is being
119548 ** generated for will not open a statement transaction. */
119549 assert( nIncr==1 );
119550 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
119551 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
119552 }else{
119553 if( nIncr>0 && pFKey->isDeferred==0 ){
119554 sqlite3MayAbort(pParse);
119555 }
119556 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
119557 }
119558
119559 sqlite3VdbeResolveLabel(v, iOk);
119560 sqlite3VdbeAddOp1(v, OP_Close, iCur);
119561}
119562
119563
119564/*
119565** Return an Expr object that refers to a memory register corresponding
119566** to column iCol of table pTab.
119567**
119568** regBase is the first of an array of register that contains the data
119569** for pTab. regBase itself holds the rowid. regBase+1 holds the first
119570** column. regBase+2 holds the second column, and so forth.
119571*/
119572static Expr *exprTableRegister(
119573 Parse *pParse, /* Parsing and code generating context */
119574 Table *pTab, /* The table whose content is at r[regBase]... */
119575 int regBase, /* Contents of table pTab */
119576 i16 iCol /* Which column of pTab is desired */
119577){
119578 Expr *pExpr;
119579 Column *pCol;
119580 const char *zColl;
119581 sqlite3 *db = pParse->db;
119582
119583 pExpr = sqlite3Expr(db, TK_REGISTER, 0);
119584 if( pExpr ){
119585 if( iCol>=0 && iCol!=pTab->iPKey ){
119586 pCol = &pTab->aCol[iCol];
119587 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
119588 pExpr->affExpr = pCol->affinity;
119589 zColl = pCol->zColl;
119590 if( zColl==0 ) zColl = db->pDfltColl->zName;
119591 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
119592 }else{
119593 pExpr->iTable = regBase;
119594 pExpr->affExpr = SQLITE_AFF_INTEGER;
119595 }
119596 }
119597 return pExpr;
119598}
119599
119600/*
119601** Return an Expr object that refers to column iCol of table pTab which
119602** has cursor iCur.
119603*/
119604static Expr *exprTableColumn(
119605 sqlite3 *db, /* The database connection */
119606 Table *pTab, /* The table whose column is desired */
119607 int iCursor, /* The open cursor on the table */
119608 i16 iCol /* The column that is wanted */
119609){
119610 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
119611 if( pExpr ){
119612 pExpr->y.pTab = pTab;
119613 pExpr->iTable = iCursor;
119614 pExpr->iColumn = iCol;
119615 }
119616 return pExpr;
119617}
119618
119619/*
119620** This function is called to generate code executed when a row is deleted
119621** from the parent table of foreign key constraint pFKey and, if pFKey is
119622** deferred, when a row is inserted into the same table. When generating
119623** code for an SQL UPDATE operation, this function may be called twice -
119624** once to "delete" the old row and once to "insert" the new row.
119625**
119626** Parameter nIncr is passed -1 when inserting a row (as this may decrease
119627** the number of FK violations in the db) or +1 when deleting one (as this
119628** may increase the number of FK constraint problems).
119629**
119630** The code generated by this function scans through the rows in the child
119631** table that correspond to the parent table row being deleted or inserted.
119632** For each child row found, one of the following actions is taken:
119633**
119634** Operation | FK type | Action taken
119635** --------------------------------------------------------------------------
119636** DELETE immediate Increment the "immediate constraint counter".
119637** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
119638** throw a "FOREIGN KEY constraint failed" exception.
119639**
119640** INSERT immediate Decrement the "immediate constraint counter".
119641**
119642** DELETE deferred Increment the "deferred constraint counter".
119643** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
119644** throw a "FOREIGN KEY constraint failed" exception.
119645**
119646** INSERT deferred Decrement the "deferred constraint counter".
119647**
119648** These operations are identified in the comment at the top of this file
119649** (fkey.c) as "I.2" and "D.2".
119650*/
119651static void fkScanChildren(
119652 Parse *pParse, /* Parse context */
119653 SrcList *pSrc, /* The child table to be scanned */
119654 Table *pTab, /* The parent table */
119655 Index *pIdx, /* Index on parent covering the foreign key */
119656 FKey *pFKey, /* The foreign key linking pSrc to pTab */
119657 int *aiCol, /* Map from pIdx cols to child table cols */
119658 int regData, /* Parent row data starts here */
119659 int nIncr /* Amount to increment deferred counter by */
119660){
119661 sqlite3 *db = pParse->db; /* Database handle */
119662 int i; /* Iterator variable */
119663 Expr *pWhere = 0; /* WHERE clause to scan with */
119664 NameContext sNameContext; /* Context used to resolve WHERE clause */
119665 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
119666 int iFkIfZero = 0; /* Address of OP_FkIfZero */
119667 Vdbe *v = sqlite3GetVdbe(pParse);
119668
119669 assert( pIdx==0 || pIdx->pTable==pTab );
119670 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
119671 assert( pIdx!=0 || pFKey->nCol==1 );
119672 assert( pIdx!=0 || HasRowid(pTab) );
119673
119674 if( nIncr<0 ){
119675 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
119676 VdbeCoverage(v);
119677 }
119678
119679 /* Create an Expr object representing an SQL expression like:
119680 **
119681 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
119682 **
119683 ** The collation sequence used for the comparison should be that of
119684 ** the parent key columns. The affinity of the parent key column should
119685 ** be applied to each child key value before the comparison takes place.
119686 */
119687 for(i=0; i<pFKey->nCol; i++){
119688 Expr *pLeft; /* Value from parent table row */
119689 Expr *pRight; /* Column ref to child table */
119690 Expr *pEq; /* Expression (pLeft = pRight) */
119691 i16 iCol; /* Index of column in child table */
119692 const char *zCol; /* Name of column in child table */
119693
119694 iCol = pIdx ? pIdx->aiColumn[i] : -1;
119695 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
119696 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
119697 assert( iCol>=0 );
119698 zCol = pFKey->pFrom->aCol[iCol].zName;
119699 pRight = sqlite3Expr(db, TK_ID, zCol);
119700 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
119701 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
119702 }
119703
119704 /* If the child table is the same as the parent table, then add terms
119705 ** to the WHERE clause that prevent this entry from being scanned.
119706 ** The added WHERE clause terms are like this:
119707 **
119708 ** $current_rowid!=rowid
119709 ** NOT( $current_a==a AND $current_b==b AND ... )
119710 **
119711 ** The first form is used for rowid tables. The second form is used
119712 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
119713 ** (a,b,...). Either the parent or primary key could be used to
119714 ** uniquely identify the current row, but the parent key is more convenient
119715 ** as the required values have already been loaded into registers
119716 ** by the caller.
119717 */
119718 if( pTab==pFKey->pFrom && nIncr>0 ){
119719 Expr *pNe; /* Expression (pLeft != pRight) */
119720 Expr *pLeft; /* Value from parent table row */
119721 Expr *pRight; /* Column ref to child table */
119722 if( HasRowid(pTab) ){
119723 pLeft = exprTableRegister(pParse, pTab, regData, -1);
119724 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
119725 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
119726 }else{
119727 Expr *pEq, *pAll = 0;
119728 assert( pIdx!=0 );
119729 for(i=0; i<pIdx->nKeyCol; i++){
119730 i16 iCol = pIdx->aiColumn[i];
119731 assert( iCol>=0 );
119732 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
119733 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName);
119734 pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
119735 pAll = sqlite3ExprAnd(pParse, pAll, pEq);
119736 }
119737 pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
119738 }
119739 pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
119740 }
119741
119742 /* Resolve the references in the WHERE clause. */
119743 memset(&sNameContext, 0, sizeof(NameContext));
119744 sNameContext.pSrcList = pSrc;
119745 sNameContext.pParse = pParse;
119746 sqlite3ResolveExprNames(&sNameContext, pWhere);
119747
119748 /* Create VDBE to loop through the entries in pSrc that match the WHERE
119749 ** clause. For each row found, increment either the deferred or immediate
119750 ** foreign key constraint counter. */
119751 if( pParse->nErr==0 ){
119752 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
119753 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
119754 if( pWInfo ){
119755 sqlite3WhereEnd(pWInfo);
119756 }
119757 }
119758
119759 /* Clean up the WHERE clause constructed above. */
119760 sqlite3ExprDelete(db, pWhere);
119761 if( iFkIfZero ){
119762 sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero);
119763 }
119764}
119765
119766/*
119767** This function returns a linked list of FKey objects (connected by
119768** FKey.pNextTo) holding all children of table pTab. For example,
119769** given the following schema:
119770**
119771** CREATE TABLE t1(a PRIMARY KEY);
119772** CREATE TABLE t2(b REFERENCES t1(a);
119773**
119774** Calling this function with table "t1" as an argument returns a pointer
119775** to the FKey structure representing the foreign key constraint on table
119776** "t2". Calling this function with "t2" as the argument would return a
119777** NULL pointer (as there are no FK constraints for which t2 is the parent
119778** table).
119779*/
119780SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
119781 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
119782}
119783
119784/*
119785** The second argument is a Trigger structure allocated by the
119786** fkActionTrigger() routine. This function deletes the Trigger structure
119787** and all of its sub-components.
119788**
119789** The Trigger structure or any of its sub-components may be allocated from
119790** the lookaside buffer belonging to database handle dbMem.
119791*/
119792static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
119793 if( p ){
119794 TriggerStep *pStep = p->step_list;
119795 sqlite3ExprDelete(dbMem, pStep->pWhere);
119796 sqlite3ExprListDelete(dbMem, pStep->pExprList);
119797 sqlite3SelectDelete(dbMem, pStep->pSelect);
119798 sqlite3ExprDelete(dbMem, p->pWhen);
119799 sqlite3DbFree(dbMem, p);
119800 }
119801}
119802
119803/*
119804** This function is called to generate code that runs when table pTab is
119805** being dropped from the database. The SrcList passed as the second argument
119806** to this function contains a single entry guaranteed to resolve to
119807** table pTab.
119808**
119809** Normally, no code is required. However, if either
119810**
119811** (a) The table is the parent table of a FK constraint, or
119812** (b) The table is the child table of a deferred FK constraint and it is
119813** determined at runtime that there are outstanding deferred FK
119814** constraint violations in the database,
119815**
119816** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
119817** the table from the database. Triggers are disabled while running this
119818** DELETE, but foreign key actions are not.
119819*/
119820SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
119821 sqlite3 *db = pParse->db;
119822 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
119823 int iSkip = 0;
119824 Vdbe *v = sqlite3GetVdbe(pParse);
119825
119826 assert( v ); /* VDBE has already been allocated */
119827 assert( pTab->pSelect==0 ); /* Not a view */
119828 if( sqlite3FkReferences(pTab)==0 ){
119829 /* Search for a deferred foreign key constraint for which this table
119830 ** is the child table. If one cannot be found, return without
119831 ** generating any VDBE code. If one can be found, then jump over
119832 ** the entire DELETE if there are no outstanding deferred constraints
119833 ** when this statement is run. */
119834 FKey *p;
119835 for(p=pTab->pFKey; p; p=p->pNextFrom){
119836 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
119837 }
119838 if( !p ) return;
119839 iSkip = sqlite3VdbeMakeLabel(pParse);
119840 sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
119841 }
119842
119843 pParse->disableTriggers = 1;
119844 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
119845 pParse->disableTriggers = 0;
119846
119847 /* If the DELETE has generated immediate foreign key constraint
119848 ** violations, halt the VDBE and return an error at this point, before
119849 ** any modifications to the schema are made. This is because statement
119850 ** transactions are not able to rollback schema changes.
119851 **
119852 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
119853 ** the statement transaction will not be rolled back even if FK
119854 ** constraints are violated.
119855 */
119856 if( (db->flags & SQLITE_DeferFKs)==0 ){
119857 sqlite3VdbeVerifyAbortable(v, OE_Abort);
119858 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
119859 VdbeCoverage(v);
119860 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
119861 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
119862 }
119863
119864 if( iSkip ){
119865 sqlite3VdbeResolveLabel(v, iSkip);
119866 }
119867 }
119868}
119869
119870
119871/*
119872** The second argument points to an FKey object representing a foreign key
119873** for which pTab is the child table. An UPDATE statement against pTab
119874** is currently being processed. For each column of the table that is
119875** actually updated, the corresponding element in the aChange[] array
119876** is zero or greater (if a column is unmodified the corresponding element
119877** is set to -1). If the rowid column is modified by the UPDATE statement
119878** the bChngRowid argument is non-zero.
119879**
119880** This function returns true if any of the columns that are part of the
119881** child key for FK constraint *p are modified.
119882*/
119883static int fkChildIsModified(
119884 Table *pTab, /* Table being updated */
119885 FKey *p, /* Foreign key for which pTab is the child */
119886 int *aChange, /* Array indicating modified columns */
119887 int bChngRowid /* True if rowid is modified by this update */
119888){
119889 int i;
119890 for(i=0; i<p->nCol; i++){
119891 int iChildKey = p->aCol[i].iFrom;
119892 if( aChange[iChildKey]>=0 ) return 1;
119893 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
119894 }
119895 return 0;
119896}
119897
119898/*
119899** The second argument points to an FKey object representing a foreign key
119900** for which pTab is the parent table. An UPDATE statement against pTab
119901** is currently being processed. For each column of the table that is
119902** actually updated, the corresponding element in the aChange[] array
119903** is zero or greater (if a column is unmodified the corresponding element
119904** is set to -1). If the rowid column is modified by the UPDATE statement
119905** the bChngRowid argument is non-zero.
119906**
119907** This function returns true if any of the columns that are part of the
119908** parent key for FK constraint *p are modified.
119909*/
119910static int fkParentIsModified(
119911 Table *pTab,
119912 FKey *p,
119913 int *aChange,
119914 int bChngRowid
119915){
119916 int i;
119917 for(i=0; i<p->nCol; i++){
119918 char *zKey = p->aCol[i].zCol;
119919 int iKey;
119920 for(iKey=0; iKey<pTab->nCol; iKey++){
119921 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
119922 Column *pCol = &pTab->aCol[iKey];
119923 if( zKey ){
119924 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
119925 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
119926 return 1;
119927 }
119928 }
119929 }
119930 }
119931 return 0;
119932}
119933
119934/*
119935** Return true if the parser passed as the first argument is being
119936** used to code a trigger that is really a "SET NULL" action belonging
119937** to trigger pFKey.
119938*/
119939static int isSetNullAction(Parse *pParse, FKey *pFKey){
119940 Parse *pTop = sqlite3ParseToplevel(pParse);
119941 if( pTop->pTriggerPrg ){
119942 Trigger *p = pTop->pTriggerPrg->pTrigger;
119943 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
119944 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
119945 ){
119946 return 1;
119947 }
119948 }
119949 return 0;
119950}
119951
119952/*
119953** This function is called when inserting, deleting or updating a row of
119954** table pTab to generate VDBE code to perform foreign key constraint
119955** processing for the operation.
119956**
119957** For a DELETE operation, parameter regOld is passed the index of the
119958** first register in an array of (pTab->nCol+1) registers containing the
119959** rowid of the row being deleted, followed by each of the column values
119960** of the row being deleted, from left to right. Parameter regNew is passed
119961** zero in this case.
119962**
119963** For an INSERT operation, regOld is passed zero and regNew is passed the
119964** first register of an array of (pTab->nCol+1) registers containing the new
119965** row data.
119966**
119967** For an UPDATE operation, this function is called twice. Once before
119968** the original record is deleted from the table using the calling convention
119969** described for DELETE. Then again after the original record is deleted
119970** but before the new record is inserted using the INSERT convention.
119971*/
119972SQLITE_PRIVATE void sqlite3FkCheck(
119973 Parse *pParse, /* Parse context */
119974 Table *pTab, /* Row is being deleted from this table */
119975 int regOld, /* Previous row data is stored here */
119976 int regNew, /* New row data is stored here */
119977 int *aChange, /* Array indicating UPDATEd columns (or 0) */
119978 int bChngRowid /* True if rowid is UPDATEd */
119979){
119980 sqlite3 *db = pParse->db; /* Database handle */
119981 FKey *pFKey; /* Used to iterate through FKs */
119982 int iDb; /* Index of database containing pTab */
119983 const char *zDb; /* Name of database containing pTab */
119984 int isIgnoreErrors = pParse->disableTriggers;
119985
119986 /* Exactly one of regOld and regNew should be non-zero. */
119987 assert( (regOld==0)!=(regNew==0) );
119988
119989 /* If foreign-keys are disabled, this function is a no-op. */
119990 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
119991
119992 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119993 zDb = db->aDb[iDb].zDbSName;
119994
119995 /* Loop through all the foreign key constraints for which pTab is the
119996 ** child table (the table that the foreign key definition is part of). */
119997 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
119998 Table *pTo; /* Parent table of foreign key pFKey */
119999 Index *pIdx = 0; /* Index on key columns in pTo */
120000 int *aiFree = 0;
120001 int *aiCol;
120002 int iCol;
120003 int i;
120004 int bIgnore = 0;
120005
120006 if( aChange
120007 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
120008 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
120009 ){
120010 continue;
120011 }
120012
120013 /* Find the parent table of this foreign key. Also find a unique index
120014 ** on the parent key columns in the parent table. If either of these
120015 ** schema items cannot be located, set an error in pParse and return
120016 ** early. */
120017 if( pParse->disableTriggers ){
120018 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
120019 }else{
120020 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
120021 }
120022 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
120023 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
120024 if( !isIgnoreErrors || db->mallocFailed ) return;
120025 if( pTo==0 ){
120026 /* If isIgnoreErrors is true, then a table is being dropped. In this
120027 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
120028 ** before actually dropping it in order to check FK constraints.
120029 ** If the parent table of an FK constraint on the current table is
120030 ** missing, behave as if it is empty. i.e. decrement the relevant
120031 ** FK counter for each row of the current table with non-NULL keys.
120032 */
120033 Vdbe *v = sqlite3GetVdbe(pParse);
120034 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
120035 for(i=0; i<pFKey->nCol; i++){
120036 int iFromCol, iReg;
120037 iFromCol = pFKey->aCol[i].iFrom;
120038 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
120039 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
120040 }
120041 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
120042 }
120043 continue;
120044 }
120045 assert( pFKey->nCol==1 || (aiFree && pIdx) );
120046
120047 if( aiFree ){
120048 aiCol = aiFree;
120049 }else{
120050 iCol = pFKey->aCol[0].iFrom;
120051 aiCol = &iCol;
120052 }
120053 for(i=0; i<pFKey->nCol; i++){
120054 if( aiCol[i]==pTab->iPKey ){
120055 aiCol[i] = -1;
120056 }
120057 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
120058#ifndef SQLITE_OMIT_AUTHORIZATION
120059 /* Request permission to read the parent key columns. If the
120060 ** authorization callback returns SQLITE_IGNORE, behave as if any
120061 ** values read from the parent table are NULL. */
120062 if( db->xAuth ){
120063 int rcauth;
120064 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
120065 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
120066 bIgnore = (rcauth==SQLITE_IGNORE);
120067 }
120068#endif
120069 }
120070
120071 /* Take a shared-cache advisory read-lock on the parent table. Allocate
120072 ** a cursor to use to search the unique index on the parent key columns
120073 ** in the parent table. */
120074 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
120075 pParse->nTab++;
120076
120077 if( regOld!=0 ){
120078 /* A row is being removed from the child table. Search for the parent.
120079 ** If the parent does not exist, removing the child row resolves an
120080 ** outstanding foreign key constraint violation. */
120081 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
120082 }
120083 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
120084 /* A row is being added to the child table. If a parent row cannot
120085 ** be found, adding the child row has violated the FK constraint.
120086 **
120087 ** If this operation is being performed as part of a trigger program
120088 ** that is actually a "SET NULL" action belonging to this very
120089 ** foreign key, then omit this scan altogether. As all child key
120090 ** values are guaranteed to be NULL, it is not possible for adding
120091 ** this row to cause an FK violation. */
120092 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
120093 }
120094
120095 sqlite3DbFree(db, aiFree);
120096 }
120097
120098 /* Loop through all the foreign key constraints that refer to this table.
120099 ** (the "child" constraints) */
120100 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
120101 Index *pIdx = 0; /* Foreign key index for pFKey */
120102 SrcList *pSrc;
120103 int *aiCol = 0;
120104
120105 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
120106 continue;
120107 }
120108
120109 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
120110 && !pParse->pToplevel && !pParse->isMultiWrite
120111 ){
120112 assert( regOld==0 && regNew!=0 );
120113 /* Inserting a single row into a parent table cannot cause (or fix)
120114 ** an immediate foreign key violation. So do nothing in this case. */
120115 continue;
120116 }
120117
120118 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
120119 if( !isIgnoreErrors || db->mallocFailed ) return;
120120 continue;
120121 }
120122 assert( aiCol || pFKey->nCol==1 );
120123
120124 /* Create a SrcList structure containing the child table. We need the
120125 ** child table as a SrcList for sqlite3WhereBegin() */
120126 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
120127 if( pSrc ){
120128 struct SrcList_item *pItem = pSrc->a;
120129 pItem->pTab = pFKey->pFrom;
120130 pItem->zName = pFKey->pFrom->zName;
120131 pItem->pTab->nTabRef++;
120132 pItem->iCursor = pParse->nTab++;
120133
120134 if( regNew!=0 ){
120135 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
120136 }
120137 if( regOld!=0 ){
120138 int eAction = pFKey->aAction[aChange!=0];
120139 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
120140 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
120141 ** action applies, then any foreign key violations caused by
120142 ** removing the parent key will be rectified by the action trigger.
120143 ** So do not set the "may-abort" flag in this case.
120144 **
120145 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
120146 ** may-abort flag will eventually be set on this statement anyway
120147 ** (when this function is called as part of processing the UPDATE
120148 ** within the action trigger).
120149 **
120150 ** Note 2: At first glance it may seem like SQLite could simply omit
120151 ** all OP_FkCounter related scans when either CASCADE or SET NULL
120152 ** applies. The trouble starts if the CASCADE or SET NULL action
120153 ** trigger causes other triggers or action rules attached to the
120154 ** child table to fire. In these cases the fk constraint counters
120155 ** might be set incorrectly if any OP_FkCounter related scans are
120156 ** omitted. */
120157 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
120158 sqlite3MayAbort(pParse);
120159 }
120160 }
120161 pItem->zName = 0;
120162 sqlite3SrcListDelete(db, pSrc);
120163 }
120164 sqlite3DbFree(db, aiCol);
120165 }
120166}
120167
120168#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
120169
120170/*
120171** This function is called before generating code to update or delete a
120172** row contained in table pTab.
120173*/
120174SQLITE_PRIVATE u32 sqlite3FkOldmask(
120175 Parse *pParse, /* Parse context */
120176 Table *pTab /* Table being modified */
120177){
120178 u32 mask = 0;
120179 if( pParse->db->flags&SQLITE_ForeignKeys ){
120180 FKey *p;
120181 int i;
120182 for(p=pTab->pFKey; p; p=p->pNextFrom){
120183 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
120184 }
120185 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
120186 Index *pIdx = 0;
120187 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
120188 if( pIdx ){
120189 for(i=0; i<pIdx->nKeyCol; i++){
120190 assert( pIdx->aiColumn[i]>=0 );
120191 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
120192 }
120193 }
120194 }
120195 }
120196 return mask;
120197}
120198
120199
120200/*
120201** This function is called before generating code to update or delete a
120202** row contained in table pTab. If the operation is a DELETE, then
120203** parameter aChange is passed a NULL value. For an UPDATE, aChange points
120204** to an array of size N, where N is the number of columns in table pTab.
120205** If the i'th column is not modified by the UPDATE, then the corresponding
120206** entry in the aChange[] array is set to -1. If the column is modified,
120207** the value is 0 or greater. Parameter chngRowid is set to true if the
120208** UPDATE statement modifies the rowid fields of the table.
120209**
120210** If any foreign key processing will be required, this function returns
120211** non-zero. If there is no foreign key related processing, this function
120212** returns zero.
120213**
120214** For an UPDATE, this function returns 2 if:
120215**
120216** * There are any FKs for which pTab is the child and the parent table, or
120217** * the UPDATE modifies one or more parent keys for which the action is
120218** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
120219**
120220** Or, assuming some other foreign key processing is required, 1.
120221*/
120222SQLITE_PRIVATE int sqlite3FkRequired(
120223 Parse *pParse, /* Parse context */
120224 Table *pTab, /* Table being modified */
120225 int *aChange, /* Non-NULL for UPDATE operations */
120226 int chngRowid /* True for UPDATE that affects rowid */
120227){
120228 int eRet = 0;
120229 if( pParse->db->flags&SQLITE_ForeignKeys ){
120230 if( !aChange ){
120231 /* A DELETE operation. Foreign key processing is required if the
120232 ** table in question is either the child or parent table for any
120233 ** foreign key constraint. */
120234 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
120235 }else{
120236 /* This is an UPDATE. Foreign key processing is only required if the
120237 ** operation modifies one or more child or parent key columns. */
120238 FKey *p;
120239
120240 /* Check if any child key columns are being modified. */
120241 for(p=pTab->pFKey; p; p=p->pNextFrom){
120242 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
120243 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
120244 eRet = 1;
120245 }
120246 }
120247
120248 /* Check if any parent key columns are being modified. */
120249 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
120250 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
120251 if( p->aAction[1]!=OE_None ) return 2;
120252 eRet = 1;
120253 }
120254 }
120255 }
120256 }
120257 return eRet;
120258}
120259
120260/*
120261** This function is called when an UPDATE or DELETE operation is being
120262** compiled on table pTab, which is the parent table of foreign-key pFKey.
120263** If the current operation is an UPDATE, then the pChanges parameter is
120264** passed a pointer to the list of columns being modified. If it is a
120265** DELETE, pChanges is passed a NULL pointer.
120266**
120267** It returns a pointer to a Trigger structure containing a trigger
120268** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
120269** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
120270** returned (these actions require no special handling by the triggers
120271** sub-system, code for them is created by fkScanChildren()).
120272**
120273** For example, if pFKey is the foreign key and pTab is table "p" in
120274** the following schema:
120275**
120276** CREATE TABLE p(pk PRIMARY KEY);
120277** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
120278**
120279** then the returned trigger structure is equivalent to:
120280**
120281** CREATE TRIGGER ... DELETE ON p BEGIN
120282** DELETE FROM c WHERE ck = old.pk;
120283** END;
120284**
120285** The returned pointer is cached as part of the foreign key object. It
120286** is eventually freed along with the rest of the foreign key object by
120287** sqlite3FkDelete().
120288*/
120289static Trigger *fkActionTrigger(
120290 Parse *pParse, /* Parse context */
120291 Table *pTab, /* Table being updated or deleted from */
120292 FKey *pFKey, /* Foreign key to get action for */
120293 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
120294){
120295 sqlite3 *db = pParse->db; /* Database handle */
120296 int action; /* One of OE_None, OE_Cascade etc. */
120297 Trigger *pTrigger; /* Trigger definition to return */
120298 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
120299
120300 action = pFKey->aAction[iAction];
120301 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
120302 return 0;
120303 }
120304 pTrigger = pFKey->apTrigger[iAction];
120305
120306 if( action!=OE_None && !pTrigger ){
120307 char const *zFrom; /* Name of child table */
120308 int nFrom; /* Length in bytes of zFrom */
120309 Index *pIdx = 0; /* Parent key index for this FK */
120310 int *aiCol = 0; /* child table cols -> parent key cols */
120311 TriggerStep *pStep = 0; /* First (only) step of trigger program */
120312 Expr *pWhere = 0; /* WHERE clause of trigger step */
120313 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
120314 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
120315 int i; /* Iterator variable */
120316 Expr *pWhen = 0; /* WHEN clause for the trigger */
120317
120318 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
120319 assert( aiCol || pFKey->nCol==1 );
120320
120321 for(i=0; i<pFKey->nCol; i++){
120322 Token tOld = { "old", 3 }; /* Literal "old" token */
120323 Token tNew = { "new", 3 }; /* Literal "new" token */
120324 Token tFromCol; /* Name of column in child table */
120325 Token tToCol; /* Name of column in parent table */
120326 int iFromCol; /* Idx of column in child table */
120327 Expr *pEq; /* tFromCol = OLD.tToCol */
120328
120329 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
120330 assert( iFromCol>=0 );
120331 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
120332 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
120333 sqlite3TokenInit(&tToCol,
120334 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
120335 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
120336
120337 /* Create the expression "OLD.zToCol = zFromCol". It is important
120338 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
120339 ** that the affinity and collation sequence associated with the
120340 ** parent table are used for the comparison. */
120341 pEq = sqlite3PExpr(pParse, TK_EQ,
120342 sqlite3PExpr(pParse, TK_DOT,
120343 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
120344 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
120345 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
120346 );
120347 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
120348
120349 /* For ON UPDATE, construct the next term of the WHEN clause.
120350 ** The final WHEN clause will be like this:
120351 **
120352 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
120353 */
120354 if( pChanges ){
120355 pEq = sqlite3PExpr(pParse, TK_IS,
120356 sqlite3PExpr(pParse, TK_DOT,
120357 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
120358 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
120359 sqlite3PExpr(pParse, TK_DOT,
120360 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
120361 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
120362 );
120363 pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
120364 }
120365
120366 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
120367 Expr *pNew;
120368 if( action==OE_Cascade ){
120369 pNew = sqlite3PExpr(pParse, TK_DOT,
120370 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
120371 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
120372 }else if( action==OE_SetDflt ){
120373 Column *pCol = pFKey->pFrom->aCol + iFromCol;
120374 Expr *pDflt;
120375 if( pCol->colFlags & COLFLAG_GENERATED ){
120376 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
120377 testcase( pCol->colFlags & COLFLAG_STORED );
120378 pDflt = 0;
120379 }else{
120380 pDflt = pCol->pDflt;
120381 }
120382 if( pDflt ){
120383 pNew = sqlite3ExprDup(db, pDflt, 0);
120384 }else{
120385 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
120386 }
120387 }else{
120388 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
120389 }
120390 pList = sqlite3ExprListAppend(pParse, pList, pNew);
120391 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
120392 }
120393 }
120394 sqlite3DbFree(db, aiCol);
120395
120396 zFrom = pFKey->pFrom->zName;
120397 nFrom = sqlite3Strlen30(zFrom);
120398
120399 if( action==OE_Restrict ){
120400 Token tFrom;
120401 Expr *pRaise;
120402
120403 tFrom.z = zFrom;
120404 tFrom.n = nFrom;
120405 pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
120406 if( pRaise ){
120407 pRaise->affExpr = OE_Abort;
120408 }
120409 pSelect = sqlite3SelectNew(pParse,
120410 sqlite3ExprListAppend(pParse, 0, pRaise),
120411 sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
120412 pWhere,
120413 0, 0, 0, 0, 0
120414 );
120415 pWhere = 0;
120416 }
120417
120418 /* Disable lookaside memory allocation */
120419 DisableLookaside;
120420
120421 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
120422 sizeof(Trigger) + /* struct Trigger */
120423 sizeof(TriggerStep) + /* Single step in trigger program */
120424 nFrom + 1 /* Space for pStep->zTarget */
120425 );
120426 if( pTrigger ){
120427 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
120428 pStep->zTarget = (char *)&pStep[1];
120429 memcpy((char *)pStep->zTarget, zFrom, nFrom);
120430
120431 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
120432 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
120433 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
120434 if( pWhen ){
120435 pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
120436 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
120437 }
120438 }
120439
120440 /* Re-enable the lookaside buffer, if it was disabled earlier. */
120441 EnableLookaside;
120442
120443 sqlite3ExprDelete(db, pWhere);
120444 sqlite3ExprDelete(db, pWhen);
120445 sqlite3ExprListDelete(db, pList);
120446 sqlite3SelectDelete(db, pSelect);
120447 if( db->mallocFailed==1 ){
120448 fkTriggerDelete(db, pTrigger);
120449 return 0;
120450 }
120451 assert( pStep!=0 );
120452 assert( pTrigger!=0 );
120453
120454 switch( action ){
120455 case OE_Restrict:
120456 pStep->op = TK_SELECT;
120457 break;
120458 case OE_Cascade:
120459 if( !pChanges ){
120460 pStep->op = TK_DELETE;
120461 break;
120462 }
120463 /* no break */ deliberate_fall_through
120464 default:
120465 pStep->op = TK_UPDATE;
120466 }
120467 pStep->pTrig = pTrigger;
120468 pTrigger->pSchema = pTab->pSchema;
120469 pTrigger->pTabSchema = pTab->pSchema;
120470 pFKey->apTrigger[iAction] = pTrigger;
120471 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
120472 }
120473
120474 return pTrigger;
120475}
120476
120477/*
120478** This function is called when deleting or updating a row to implement
120479** any required CASCADE, SET NULL or SET DEFAULT actions.
120480*/
120481SQLITE_PRIVATE void sqlite3FkActions(
120482 Parse *pParse, /* Parse context */
120483 Table *pTab, /* Table being updated or deleted from */
120484 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
120485 int regOld, /* Address of array containing old row */
120486 int *aChange, /* Array indicating UPDATEd columns (or 0) */
120487 int bChngRowid /* True if rowid is UPDATEd */
120488){
120489 /* If foreign-key support is enabled, iterate through all FKs that
120490 ** refer to table pTab. If there is an action associated with the FK
120491 ** for this operation (either update or delete), invoke the associated
120492 ** trigger sub-program. */
120493 if( pParse->db->flags&SQLITE_ForeignKeys ){
120494 FKey *pFKey; /* Iterator variable */
120495 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
120496 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
120497 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
120498 if( pAct ){
120499 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
120500 }
120501 }
120502 }
120503 }
120504}
120505
120506#endif /* ifndef SQLITE_OMIT_TRIGGER */
120507
120508/*
120509** Free all memory associated with foreign key definitions attached to
120510** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
120511** hash table.
120512*/
120513SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
120514 FKey *pFKey; /* Iterator variable */
120515 FKey *pNext; /* Copy of pFKey->pNextFrom */
120516
120517 assert( db==0 || IsVirtual(pTab)
120518 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
120519 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
120520
120521 /* Remove the FK from the fkeyHash hash table. */
120522 if( !db || db->pnBytesFreed==0 ){
120523 if( pFKey->pPrevTo ){
120524 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
120525 }else{
120526 void *p = (void *)pFKey->pNextTo;
120527 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
120528 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
120529 }
120530 if( pFKey->pNextTo ){
120531 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
120532 }
120533 }
120534
120535 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
120536 ** classified as either immediate or deferred.
120537 */
120538 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
120539
120540 /* Delete any triggers created to implement actions for this FK. */
120541#ifndef SQLITE_OMIT_TRIGGER
120542 fkTriggerDelete(db, pFKey->apTrigger[0]);
120543 fkTriggerDelete(db, pFKey->apTrigger[1]);
120544#endif
120545
120546 pNext = pFKey->pNextFrom;
120547 sqlite3DbFree(db, pFKey);
120548 }
120549}
120550#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
120551
120552/************** End of fkey.c ************************************************/
120553/************** Begin file insert.c ******************************************/
120554/*
120555** 2001 September 15
120556**
120557** The author disclaims copyright to this source code. In place of
120558** a legal notice, here is a blessing:
120559**
120560** May you do good and not evil.
120561** May you find forgiveness for yourself and forgive others.
120562** May you share freely, never taking more than you give.
120563**
120564*************************************************************************
120565** This file contains C code routines that are called by the parser
120566** to handle INSERT statements in SQLite.
120567*/
120568/* #include "sqliteInt.h" */
120569
120570/*
120571** Generate code that will
120572**
120573** (1) acquire a lock for table pTab then
120574** (2) open pTab as cursor iCur.
120575**
120576** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
120577** for that table that is actually opened.
120578*/
120579SQLITE_PRIVATE void sqlite3OpenTable(
120580 Parse *pParse, /* Generate code into this VDBE */
120581 int iCur, /* The cursor number of the table */
120582 int iDb, /* The database index in sqlite3.aDb[] */
120583 Table *pTab, /* The table to be opened */
120584 int opcode /* OP_OpenRead or OP_OpenWrite */
120585){
120586 Vdbe *v;
120587 assert( !IsVirtual(pTab) );
120588 v = sqlite3GetVdbe(pParse);
120589 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
120590 sqlite3TableLock(pParse, iDb, pTab->tnum,
120591 (opcode==OP_OpenWrite)?1:0, pTab->zName);
120592 if( HasRowid(pTab) ){
120593 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
120594 VdbeComment((v, "%s", pTab->zName));
120595 }else{
120596 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120597 assert( pPk!=0 );
120598 assert( pPk->tnum==pTab->tnum );
120599 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
120600 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
120601 VdbeComment((v, "%s", pTab->zName));
120602 }
120603}
120604
120605/*
120606** Return a pointer to the column affinity string associated with index
120607** pIdx. A column affinity string has one character for each column in
120608** the table, according to the affinity of the column:
120609**
120610** Character Column affinity
120611** ------------------------------
120612** 'A' BLOB
120613** 'B' TEXT
120614** 'C' NUMERIC
120615** 'D' INTEGER
120616** 'F' REAL
120617**
120618** An extra 'D' is appended to the end of the string to cover the
120619** rowid that appears as the last column in every index.
120620**
120621** Memory for the buffer containing the column index affinity string
120622** is managed along with the rest of the Index structure. It will be
120623** released when sqlite3DeleteIndex() is called.
120624*/
120625SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
120626 if( !pIdx->zColAff ){
120627 /* The first time a column affinity string for a particular index is
120628 ** required, it is allocated and populated here. It is then stored as
120629 ** a member of the Index structure for subsequent use.
120630 **
120631 ** The column affinity string will eventually be deleted by
120632 ** sqliteDeleteIndex() when the Index structure itself is cleaned
120633 ** up.
120634 */
120635 int n;
120636 Table *pTab = pIdx->pTable;
120637 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
120638 if( !pIdx->zColAff ){
120639 sqlite3OomFault(db);
120640 return 0;
120641 }
120642 for(n=0; n<pIdx->nColumn; n++){
120643 i16 x = pIdx->aiColumn[n];
120644 char aff;
120645 if( x>=0 ){
120646 aff = pTab->aCol[x].affinity;
120647 }else if( x==XN_ROWID ){
120648 aff = SQLITE_AFF_INTEGER;
120649 }else{
120650 assert( x==XN_EXPR );
120651 assert( pIdx->aColExpr!=0 );
120652 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
120653 }
120654 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
120655 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
120656 pIdx->zColAff[n] = aff;
120657 }
120658 pIdx->zColAff[n] = 0;
120659 }
120660
120661 return pIdx->zColAff;
120662}
120663
120664/*
120665** Compute the affinity string for table pTab, if it has not already been
120666** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
120667**
120668** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
120669** if iReg>0 then code an OP_Affinity opcode that will set the affinities
120670** for register iReg and following. Or if affinities exists and iReg==0,
120671** then just set the P4 operand of the previous opcode (which should be
120672** an OP_MakeRecord) to the affinity string.
120673**
120674** A column affinity string has one character per column:
120675**
120676** Character Column affinity
120677** ------------------------------
120678** 'A' BLOB
120679** 'B' TEXT
120680** 'C' NUMERIC
120681** 'D' INTEGER
120682** 'E' REAL
120683*/
120684SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
120685 int i, j;
120686 char *zColAff = pTab->zColAff;
120687 if( zColAff==0 ){
120688 sqlite3 *db = sqlite3VdbeDb(v);
120689 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
120690 if( !zColAff ){
120691 sqlite3OomFault(db);
120692 return;
120693 }
120694
120695 for(i=j=0; i<pTab->nCol; i++){
120696 assert( pTab->aCol[i].affinity!=0 );
120697 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
120698 zColAff[j++] = pTab->aCol[i].affinity;
120699 }
120700 }
120701 do{
120702 zColAff[j--] = 0;
120703 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
120704 pTab->zColAff = zColAff;
120705 }
120706 assert( zColAff!=0 );
120707 i = sqlite3Strlen30NN(zColAff);
120708 if( i ){
120709 if( iReg ){
120710 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
120711 }else{
120712 sqlite3VdbeChangeP4(v, -1, zColAff, i);
120713 }
120714 }
120715}
120716
120717/*
120718** Return non-zero if the table pTab in database iDb or any of its indices
120719** have been opened at any point in the VDBE program. This is used to see if
120720** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
120721** run without using a temporary table for the results of the SELECT.
120722*/
120723static int readsTable(Parse *p, int iDb, Table *pTab){
120724 Vdbe *v = sqlite3GetVdbe(p);
120725 int i;
120726 int iEnd = sqlite3VdbeCurrentAddr(v);
120727#ifndef SQLITE_OMIT_VIRTUALTABLE
120728 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
120729#endif
120730
120731 for(i=1; i<iEnd; i++){
120732 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
120733 assert( pOp!=0 );
120734 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
120735 Index *pIndex;
120736 Pgno tnum = pOp->p2;
120737 if( tnum==pTab->tnum ){
120738 return 1;
120739 }
120740 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
120741 if( tnum==pIndex->tnum ){
120742 return 1;
120743 }
120744 }
120745 }
120746#ifndef SQLITE_OMIT_VIRTUALTABLE
120747 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
120748 assert( pOp->p4.pVtab!=0 );
120749 assert( pOp->p4type==P4_VTAB );
120750 return 1;
120751 }
120752#endif
120753 }
120754 return 0;
120755}
120756
120757/* This walker callback will compute the union of colFlags flags for all
120758** referenced columns in a CHECK constraint or generated column expression.
120759*/
120760static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
120761 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
120762 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
120763 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
120764 }
120765 return WRC_Continue;
120766}
120767
120768#ifndef SQLITE_OMIT_GENERATED_COLUMNS
120769/*
120770** All regular columns for table pTab have been puts into registers
120771** starting with iRegStore. The registers that correspond to STORED
120772** or VIRTUAL columns have not yet been initialized. This routine goes
120773** back and computes the values for those columns based on the previously
120774** computed normal columns.
120775*/
120776SQLITE_PRIVATE void sqlite3ComputeGeneratedColumns(
120777 Parse *pParse, /* Parsing context */
120778 int iRegStore, /* Register holding the first column */
120779 Table *pTab /* The table */
120780){
120781 int i;
120782 Walker w;
120783 Column *pRedo;
120784 int eProgress;
120785 VdbeOp *pOp;
120786
120787 assert( pTab->tabFlags & TF_HasGenerated );
120788 testcase( pTab->tabFlags & TF_HasVirtual );
120789 testcase( pTab->tabFlags & TF_HasStored );
120790
120791 /* Before computing generated columns, first go through and make sure
120792 ** that appropriate affinity has been applied to the regular columns
120793 */
120794 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
120795 if( (pTab->tabFlags & TF_HasStored)!=0
120796 && (pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1))->opcode==OP_Affinity
120797 ){
120798 /* Change the OP_Affinity argument to '@' (NONE) for all stored
120799 ** columns. '@' is the no-op affinity and those columns have not
120800 ** yet been computed. */
120801 int ii, jj;
120802 char *zP4 = pOp->p4.z;
120803 assert( zP4!=0 );
120804 assert( pOp->p4type==P4_DYNAMIC );
120805 for(ii=jj=0; zP4[jj]; ii++){
120806 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
120807 continue;
120808 }
120809 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
120810 zP4[jj] = SQLITE_AFF_NONE;
120811 }
120812 jj++;
120813 }
120814 }
120815
120816 /* Because there can be multiple generated columns that refer to one another,
120817 ** this is a two-pass algorithm. On the first pass, mark all generated
120818 ** columns as "not available".
120819 */
120820 for(i=0; i<pTab->nCol; i++){
120821 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
120822 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
120823 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
120824 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
120825 }
120826 }
120827
120828 w.u.pTab = pTab;
120829 w.xExprCallback = exprColumnFlagUnion;
120830 w.xSelectCallback = 0;
120831 w.xSelectCallback2 = 0;
120832
120833 /* On the second pass, compute the value of each NOT-AVAILABLE column.
120834 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
120835 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
120836 ** they are needed.
120837 */
120838 pParse->iSelfTab = -iRegStore;
120839 do{
120840 eProgress = 0;
120841 pRedo = 0;
120842 for(i=0; i<pTab->nCol; i++){
120843 Column *pCol = pTab->aCol + i;
120844 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
120845 int x;
120846 pCol->colFlags |= COLFLAG_BUSY;
120847 w.eCode = 0;
120848 sqlite3WalkExpr(&w, pCol->pDflt);
120849 pCol->colFlags &= ~COLFLAG_BUSY;
120850 if( w.eCode & COLFLAG_NOTAVAIL ){
120851 pRedo = pCol;
120852 continue;
120853 }
120854 eProgress = 1;
120855 assert( pCol->colFlags & COLFLAG_GENERATED );
120856 x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
120857 sqlite3ExprCodeGeneratedColumn(pParse, pCol, x);
120858 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
120859 }
120860 }
120861 }while( pRedo && eProgress );
120862 if( pRedo ){
120863 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zName);
120864 }
120865 pParse->iSelfTab = 0;
120866}
120867#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
120868
120869
120870#ifndef SQLITE_OMIT_AUTOINCREMENT
120871/*
120872** Locate or create an AutoincInfo structure associated with table pTab
120873** which is in database iDb. Return the register number for the register
120874** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
120875** table. (Also return zero when doing a VACUUM since we do not want to
120876** update the AUTOINCREMENT counters during a VACUUM.)
120877**
120878** There is at most one AutoincInfo structure per table even if the
120879** same table is autoincremented multiple times due to inserts within
120880** triggers. A new AutoincInfo structure is created if this is the
120881** first use of table pTab. On 2nd and subsequent uses, the original
120882** AutoincInfo structure is used.
120883**
120884** Four consecutive registers are allocated:
120885**
120886** (1) The name of the pTab table.
120887** (2) The maximum ROWID of pTab.
120888** (3) The rowid in sqlite_sequence of pTab
120889** (4) The original value of the max ROWID in pTab, or NULL if none
120890**
120891** The 2nd register is the one that is returned. That is all the
120892** insert routine needs to know about.
120893*/
120894static int autoIncBegin(
120895 Parse *pParse, /* Parsing context */
120896 int iDb, /* Index of the database holding pTab */
120897 Table *pTab /* The table we are writing to */
120898){
120899 int memId = 0; /* Register holding maximum rowid */
120900 assert( pParse->db->aDb[iDb].pSchema!=0 );
120901 if( (pTab->tabFlags & TF_Autoincrement)!=0
120902 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
120903 ){
120904 Parse *pToplevel = sqlite3ParseToplevel(pParse);
120905 AutoincInfo *pInfo;
120906 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
120907
120908 /* Verify that the sqlite_sequence table exists and is an ordinary
120909 ** rowid table with exactly two columns.
120910 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
120911 if( pSeqTab==0
120912 || !HasRowid(pSeqTab)
120913 || IsVirtual(pSeqTab)
120914 || pSeqTab->nCol!=2
120915 ){
120916 pParse->nErr++;
120917 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
120918 return 0;
120919 }
120920
120921 pInfo = pToplevel->pAinc;
120922 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
120923 if( pInfo==0 ){
120924 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
120925 if( pInfo==0 ) return 0;
120926 pInfo->pNext = pToplevel->pAinc;
120927 pToplevel->pAinc = pInfo;
120928 pInfo->pTab = pTab;
120929 pInfo->iDb = iDb;
120930 pToplevel->nMem++; /* Register to hold name of table */
120931 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
120932 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
120933 }
120934 memId = pInfo->regCtr;
120935 }
120936 return memId;
120937}
120938
120939/*
120940** This routine generates code that will initialize all of the
120941** register used by the autoincrement tracker.
120942*/
120943SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
120944 AutoincInfo *p; /* Information about an AUTOINCREMENT */
120945 sqlite3 *db = pParse->db; /* The database connection */
120946 Db *pDb; /* Database only autoinc table */
120947 int memId; /* Register holding max rowid */
120948 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
120949
120950 /* This routine is never called during trigger-generation. It is
120951 ** only called from the top-level */
120952 assert( pParse->pTriggerTab==0 );
120953 assert( sqlite3IsToplevel(pParse) );
120954
120955 assert( v ); /* We failed long ago if this is not so */
120956 for(p = pParse->pAinc; p; p = p->pNext){
120957 static const int iLn = VDBE_OFFSET_LINENO(2);
120958 static const VdbeOpList autoInc[] = {
120959 /* 0 */ {OP_Null, 0, 0, 0},
120960 /* 1 */ {OP_Rewind, 0, 10, 0},
120961 /* 2 */ {OP_Column, 0, 0, 0},
120962 /* 3 */ {OP_Ne, 0, 9, 0},
120963 /* 4 */ {OP_Rowid, 0, 0, 0},
120964 /* 5 */ {OP_Column, 0, 1, 0},
120965 /* 6 */ {OP_AddImm, 0, 0, 0},
120966 /* 7 */ {OP_Copy, 0, 0, 0},
120967 /* 8 */ {OP_Goto, 0, 11, 0},
120968 /* 9 */ {OP_Next, 0, 2, 0},
120969 /* 10 */ {OP_Integer, 0, 0, 0},
120970 /* 11 */ {OP_Close, 0, 0, 0}
120971 };
120972 VdbeOp *aOp;
120973 pDb = &db->aDb[p->iDb];
120974 memId = p->regCtr;
120975 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
120976 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
120977 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
120978 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
120979 if( aOp==0 ) break;
120980 aOp[0].p2 = memId;
120981 aOp[0].p3 = memId+2;
120982 aOp[2].p3 = memId;
120983 aOp[3].p1 = memId-1;
120984 aOp[3].p3 = memId;
120985 aOp[3].p5 = SQLITE_JUMPIFNULL;
120986 aOp[4].p2 = memId+1;
120987 aOp[5].p3 = memId;
120988 aOp[6].p1 = memId;
120989 aOp[7].p2 = memId+2;
120990 aOp[7].p1 = memId;
120991 aOp[10].p2 = memId;
120992 if( pParse->nTab==0 ) pParse->nTab = 1;
120993 }
120994}
120995
120996/*
120997** Update the maximum rowid for an autoincrement calculation.
120998**
120999** This routine should be called when the regRowid register holds a
121000** new rowid that is about to be inserted. If that new rowid is
121001** larger than the maximum rowid in the memId memory cell, then the
121002** memory cell is updated.
121003*/
121004static void autoIncStep(Parse *pParse, int memId, int regRowid){
121005 if( memId>0 ){
121006 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
121007 }
121008}
121009
121010/*
121011** This routine generates the code needed to write autoincrement
121012** maximum rowid values back into the sqlite_sequence register.
121013** Every statement that might do an INSERT into an autoincrement
121014** table (either directly or through triggers) needs to call this
121015** routine just before the "exit" code.
121016*/
121017static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
121018 AutoincInfo *p;
121019 Vdbe *v = pParse->pVdbe;
121020 sqlite3 *db = pParse->db;
121021
121022 assert( v );
121023 for(p = pParse->pAinc; p; p = p->pNext){
121024 static const int iLn = VDBE_OFFSET_LINENO(2);
121025 static const VdbeOpList autoIncEnd[] = {
121026 /* 0 */ {OP_NotNull, 0, 2, 0},
121027 /* 1 */ {OP_NewRowid, 0, 0, 0},
121028 /* 2 */ {OP_MakeRecord, 0, 2, 0},
121029 /* 3 */ {OP_Insert, 0, 0, 0},
121030 /* 4 */ {OP_Close, 0, 0, 0}
121031 };
121032 VdbeOp *aOp;
121033 Db *pDb = &db->aDb[p->iDb];
121034 int iRec;
121035 int memId = p->regCtr;
121036
121037 iRec = sqlite3GetTempReg(pParse);
121038 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
121039 sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
121040 VdbeCoverage(v);
121041 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
121042 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
121043 if( aOp==0 ) break;
121044 aOp[0].p1 = memId+1;
121045 aOp[1].p2 = memId+1;
121046 aOp[2].p1 = memId-1;
121047 aOp[2].p3 = iRec;
121048 aOp[3].p2 = iRec;
121049 aOp[3].p3 = memId+1;
121050 aOp[3].p5 = OPFLAG_APPEND;
121051 sqlite3ReleaseTempReg(pParse, iRec);
121052 }
121053}
121054SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
121055 if( pParse->pAinc ) autoIncrementEnd(pParse);
121056}
121057#else
121058/*
121059** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
121060** above are all no-ops
121061*/
121062# define autoIncBegin(A,B,C) (0)
121063# define autoIncStep(A,B,C)
121064#endif /* SQLITE_OMIT_AUTOINCREMENT */
121065
121066
121067/* Forward declaration */
121068static int xferOptimization(
121069 Parse *pParse, /* Parser context */
121070 Table *pDest, /* The table we are inserting into */
121071 Select *pSelect, /* A SELECT statement to use as the data source */
121072 int onError, /* How to handle constraint errors */
121073 int iDbDest /* The database of pDest */
121074);
121075
121076/*
121077** This routine is called to handle SQL of the following forms:
121078**
121079** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
121080** insert into TABLE (IDLIST) select
121081** insert into TABLE (IDLIST) default values
121082**
121083** The IDLIST following the table name is always optional. If omitted,
121084** then a list of all (non-hidden) columns for the table is substituted.
121085** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
121086** is omitted.
121087**
121088** For the pSelect parameter holds the values to be inserted for the
121089** first two forms shown above. A VALUES clause is really just short-hand
121090** for a SELECT statement that omits the FROM clause and everything else
121091** that follows. If the pSelect parameter is NULL, that means that the
121092** DEFAULT VALUES form of the INSERT statement is intended.
121093**
121094** The code generated follows one of four templates. For a simple
121095** insert with data coming from a single-row VALUES clause, the code executes
121096** once straight down through. Pseudo-code follows (we call this
121097** the "1st template"):
121098**
121099** open write cursor to <table> and its indices
121100** put VALUES clause expressions into registers
121101** write the resulting record into <table>
121102** cleanup
121103**
121104** The three remaining templates assume the statement is of the form
121105**
121106** INSERT INTO <table> SELECT ...
121107**
121108** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
121109** in other words if the SELECT pulls all columns from a single table
121110** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
121111** if <table2> and <table1> are distinct tables but have identical
121112** schemas, including all the same indices, then a special optimization
121113** is invoked that copies raw records from <table2> over to <table1>.
121114** See the xferOptimization() function for the implementation of this
121115** template. This is the 2nd template.
121116**
121117** open a write cursor to <table>
121118** open read cursor on <table2>
121119** transfer all records in <table2> over to <table>
121120** close cursors
121121** foreach index on <table>
121122** open a write cursor on the <table> index
121123** open a read cursor on the corresponding <table2> index
121124** transfer all records from the read to the write cursors
121125** close cursors
121126** end foreach
121127**
121128** The 3rd template is for when the second template does not apply
121129** and the SELECT clause does not read from <table> at any time.
121130** The generated code follows this template:
121131**
121132** X <- A
121133** goto B
121134** A: setup for the SELECT
121135** loop over the rows in the SELECT
121136** load values into registers R..R+n
121137** yield X
121138** end loop
121139** cleanup after the SELECT
121140** end-coroutine X
121141** B: open write cursor to <table> and its indices
121142** C: yield X, at EOF goto D
121143** insert the select result into <table> from R..R+n
121144** goto C
121145** D: cleanup
121146**
121147** The 4th template is used if the insert statement takes its
121148** values from a SELECT but the data is being inserted into a table
121149** that is also read as part of the SELECT. In the third form,
121150** we have to use an intermediate table to store the results of
121151** the select. The template is like this:
121152**
121153** X <- A
121154** goto B
121155** A: setup for the SELECT
121156** loop over the tables in the SELECT
121157** load value into register R..R+n
121158** yield X
121159** end loop
121160** cleanup after the SELECT
121161** end co-routine R
121162** B: open temp table
121163** L: yield X, at EOF goto M
121164** insert row from R..R+n into temp table
121165** goto L
121166** M: open write cursor to <table> and its indices
121167** rewind temp table
121168** C: loop over rows of intermediate table
121169** transfer values form intermediate table into <table>
121170** end loop
121171** D: cleanup
121172*/
121173SQLITE_PRIVATE void sqlite3Insert(
121174 Parse *pParse, /* Parser context */
121175 SrcList *pTabList, /* Name of table into which we are inserting */
121176 Select *pSelect, /* A SELECT statement to use as the data source */
121177 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
121178 int onError, /* How to handle constraint errors */
121179 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
121180){
121181 sqlite3 *db; /* The main database structure */
121182 Table *pTab; /* The table to insert into. aka TABLE */
121183 int i, j; /* Loop counters */
121184 Vdbe *v; /* Generate code into this virtual machine */
121185 Index *pIdx; /* For looping over indices of the table */
121186 int nColumn; /* Number of columns in the data */
121187 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
121188 int iDataCur = 0; /* VDBE cursor that is the main data repository */
121189 int iIdxCur = 0; /* First index cursor */
121190 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
121191 int endOfLoop; /* Label for the end of the insertion loop */
121192 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
121193 int addrInsTop = 0; /* Jump to label "D" */
121194 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
121195 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
121196 int iDb; /* Index of database holding TABLE */
121197 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
121198 u8 appendFlag = 0; /* True if the insert is likely to be an append */
121199 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
121200 u8 bIdListInOrder; /* True if IDLIST is in table order */
121201 ExprList *pList = 0; /* List of VALUES() to be inserted */
121202 int iRegStore; /* Register in which to store next column */
121203
121204 /* Register allocations */
121205 int regFromSelect = 0;/* Base register for data coming from SELECT */
121206 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
121207 int regRowCount = 0; /* Memory cell used for the row counter */
121208 int regIns; /* Block of regs holding rowid+data being inserted */
121209 int regRowid; /* registers holding insert rowid */
121210 int regData; /* register holding first column to insert */
121211 int *aRegIdx = 0; /* One register allocated to each index */
121212
121213#ifndef SQLITE_OMIT_TRIGGER
121214 int isView; /* True if attempting to insert into a view */
121215 Trigger *pTrigger; /* List of triggers on pTab, if required */
121216 int tmask; /* Mask of trigger times */
121217#endif
121218
121219 db = pParse->db;
121220 if( pParse->nErr || db->mallocFailed ){
121221 goto insert_cleanup;
121222 }
121223 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
121224
121225 /* If the Select object is really just a simple VALUES() list with a
121226 ** single row (the common case) then keep that one row of values
121227 ** and discard the other (unused) parts of the pSelect object
121228 */
121229 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
121230 pList = pSelect->pEList;
121231 pSelect->pEList = 0;
121232 sqlite3SelectDelete(db, pSelect);
121233 pSelect = 0;
121234 }
121235
121236 /* Locate the table into which we will be inserting new information.
121237 */
121238 assert( pTabList->nSrc==1 );
121239 pTab = sqlite3SrcListLookup(pParse, pTabList);
121240 if( pTab==0 ){
121241 goto insert_cleanup;
121242 }
121243 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121244 assert( iDb<db->nDb );
121245 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
121246 db->aDb[iDb].zDbSName) ){
121247 goto insert_cleanup;
121248 }
121249 withoutRowid = !HasRowid(pTab);
121250
121251 /* Figure out if we have any triggers and if the table being
121252 ** inserted into is a view
121253 */
121254#ifndef SQLITE_OMIT_TRIGGER
121255 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
121256 isView = pTab->pSelect!=0;
121257#else
121258# define pTrigger 0
121259# define tmask 0
121260# define isView 0
121261#endif
121262#ifdef SQLITE_OMIT_VIEW
121263# undef isView
121264# define isView 0
121265#endif
121266 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
121267
121268 /* If pTab is really a view, make sure it has been initialized.
121269 ** ViewGetColumnNames() is a no-op if pTab is not a view.
121270 */
121271 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
121272 goto insert_cleanup;
121273 }
121274
121275 /* Cannot insert into a read-only table.
121276 */
121277 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
121278 goto insert_cleanup;
121279 }
121280
121281 /* Allocate a VDBE
121282 */
121283 v = sqlite3GetVdbe(pParse);
121284 if( v==0 ) goto insert_cleanup;
121285 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
121286 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
121287
121288#ifndef SQLITE_OMIT_XFER_OPT
121289 /* If the statement is of the form
121290 **
121291 ** INSERT INTO <table1> SELECT * FROM <table2>;
121292 **
121293 ** Then special optimizations can be applied that make the transfer
121294 ** very fast and which reduce fragmentation of indices.
121295 **
121296 ** This is the 2nd template.
121297 */
121298 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
121299 assert( !pTrigger );
121300 assert( pList==0 );
121301 goto insert_end;
121302 }
121303#endif /* SQLITE_OMIT_XFER_OPT */
121304
121305 /* If this is an AUTOINCREMENT table, look up the sequence number in the
121306 ** sqlite_sequence table and store it in memory cell regAutoinc.
121307 */
121308 regAutoinc = autoIncBegin(pParse, iDb, pTab);
121309
121310 /* Allocate a block registers to hold the rowid and the values
121311 ** for all columns of the new row.
121312 */
121313 regRowid = regIns = pParse->nMem+1;
121314 pParse->nMem += pTab->nCol + 1;
121315 if( IsVirtual(pTab) ){
121316 regRowid++;
121317 pParse->nMem++;
121318 }
121319 regData = regRowid+1;
121320
121321 /* If the INSERT statement included an IDLIST term, then make sure
121322 ** all elements of the IDLIST really are columns of the table and
121323 ** remember the column indices.
121324 **
121325 ** If the table has an INTEGER PRIMARY KEY column and that column
121326 ** is named in the IDLIST, then record in the ipkColumn variable
121327 ** the index into IDLIST of the primary key column. ipkColumn is
121328 ** the index of the primary key as it appears in IDLIST, not as
121329 ** is appears in the original table. (The index of the INTEGER
121330 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
121331 ** loop, if ipkColumn==(-1), that means that integer primary key
121332 ** is unspecified, and hence the table is either WITHOUT ROWID or
121333 ** it will automatically generated an integer primary key.
121334 **
121335 ** bIdListInOrder is true if the columns in IDLIST are in storage
121336 ** order. This enables an optimization that avoids shuffling the
121337 ** columns into storage order. False negatives are harmless,
121338 ** but false positives will cause database corruption.
121339 */
121340 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
121341 if( pColumn ){
121342 for(i=0; i<pColumn->nId; i++){
121343 pColumn->a[i].idx = -1;
121344 }
121345 for(i=0; i<pColumn->nId; i++){
121346 for(j=0; j<pTab->nCol; j++){
121347 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
121348 pColumn->a[i].idx = j;
121349 if( i!=j ) bIdListInOrder = 0;
121350 if( j==pTab->iPKey ){
121351 ipkColumn = i; assert( !withoutRowid );
121352 }
121353#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121354 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
121355 sqlite3ErrorMsg(pParse,
121356 "cannot INSERT into generated column \"%s\"",
121357 pTab->aCol[j].zName);
121358 goto insert_cleanup;
121359 }
121360#endif
121361 break;
121362 }
121363 }
121364 if( j>=pTab->nCol ){
121365 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
121366 ipkColumn = i;
121367 bIdListInOrder = 0;
121368 }else{
121369 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
121370 pTabList, 0, pColumn->a[i].zName);
121371 pParse->checkSchema = 1;
121372 goto insert_cleanup;
121373 }
121374 }
121375 }
121376 }
121377
121378 /* Figure out how many columns of data are supplied. If the data
121379 ** is coming from a SELECT statement, then generate a co-routine that
121380 ** produces a single row of the SELECT on each invocation. The
121381 ** co-routine is the common header to the 3rd and 4th templates.
121382 */
121383 if( pSelect ){
121384 /* Data is coming from a SELECT or from a multi-row VALUES clause.
121385 ** Generate a co-routine to run the SELECT. */
121386 int regYield; /* Register holding co-routine entry-point */
121387 int addrTop; /* Top of the co-routine */
121388 int rc; /* Result code */
121389
121390 regYield = ++pParse->nMem;
121391 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
121392 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
121393 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
121394 dest.iSdst = bIdListInOrder ? regData : 0;
121395 dest.nSdst = pTab->nCol;
121396 rc = sqlite3Select(pParse, pSelect, &dest);
121397 regFromSelect = dest.iSdst;
121398 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
121399 sqlite3VdbeEndCoroutine(v, regYield);
121400 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
121401 assert( pSelect->pEList );
121402 nColumn = pSelect->pEList->nExpr;
121403
121404 /* Set useTempTable to TRUE if the result of the SELECT statement
121405 ** should be written into a temporary table (template 4). Set to
121406 ** FALSE if each output row of the SELECT can be written directly into
121407 ** the destination table (template 3).
121408 **
121409 ** A temp table must be used if the table being updated is also one
121410 ** of the tables being read by the SELECT statement. Also use a
121411 ** temp table in the case of row triggers.
121412 */
121413 if( pTrigger || readsTable(pParse, iDb, pTab) ){
121414 useTempTable = 1;
121415 }
121416
121417 if( useTempTable ){
121418 /* Invoke the coroutine to extract information from the SELECT
121419 ** and add it to a transient table srcTab. The code generated
121420 ** here is from the 4th template:
121421 **
121422 ** B: open temp table
121423 ** L: yield X, goto M at EOF
121424 ** insert row from R..R+n into temp table
121425 ** goto L
121426 ** M: ...
121427 */
121428 int regRec; /* Register to hold packed record */
121429 int regTempRowid; /* Register to hold temp table ROWID */
121430 int addrL; /* Label "L" */
121431
121432 srcTab = pParse->nTab++;
121433 regRec = sqlite3GetTempReg(pParse);
121434 regTempRowid = sqlite3GetTempReg(pParse);
121435 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
121436 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
121437 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
121438 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
121439 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
121440 sqlite3VdbeGoto(v, addrL);
121441 sqlite3VdbeJumpHere(v, addrL);
121442 sqlite3ReleaseTempReg(pParse, regRec);
121443 sqlite3ReleaseTempReg(pParse, regTempRowid);
121444 }
121445 }else{
121446 /* This is the case if the data for the INSERT is coming from a
121447 ** single-row VALUES clause
121448 */
121449 NameContext sNC;
121450 memset(&sNC, 0, sizeof(sNC));
121451 sNC.pParse = pParse;
121452 srcTab = -1;
121453 assert( useTempTable==0 );
121454 if( pList ){
121455 nColumn = pList->nExpr;
121456 if( sqlite3ResolveExprListNames(&sNC, pList) ){
121457 goto insert_cleanup;
121458 }
121459 }else{
121460 nColumn = 0;
121461 }
121462 }
121463
121464 /* If there is no IDLIST term but the table has an integer primary
121465 ** key, the set the ipkColumn variable to the integer primary key
121466 ** column index in the original table definition.
121467 */
121468 if( pColumn==0 && nColumn>0 ){
121469 ipkColumn = pTab->iPKey;
121470#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121471 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
121472 testcase( pTab->tabFlags & TF_HasVirtual );
121473 testcase( pTab->tabFlags & TF_HasStored );
121474 for(i=ipkColumn-1; i>=0; i--){
121475 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
121476 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
121477 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
121478 ipkColumn--;
121479 }
121480 }
121481 }
121482#endif
121483 }
121484
121485 /* Make sure the number of columns in the source data matches the number
121486 ** of columns to be inserted into the table.
121487 */
121488 for(i=0; i<pTab->nCol; i++){
121489 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
121490 }
121491 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
121492 sqlite3ErrorMsg(pParse,
121493 "table %S has %d columns but %d values were supplied",
121494 pTabList, 0, pTab->nCol-nHidden, nColumn);
121495 goto insert_cleanup;
121496 }
121497 if( pColumn!=0 && nColumn!=pColumn->nId ){
121498 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
121499 goto insert_cleanup;
121500 }
121501
121502 /* Initialize the count of rows to be inserted
121503 */
121504 if( (db->flags & SQLITE_CountRows)!=0
121505 && !pParse->nested
121506 && !pParse->pTriggerTab
121507 ){
121508 regRowCount = ++pParse->nMem;
121509 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
121510 }
121511
121512 /* If this is not a view, open the table and and all indices */
121513 if( !isView ){
121514 int nIdx;
121515 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
121516 &iDataCur, &iIdxCur);
121517 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
121518 if( aRegIdx==0 ){
121519 goto insert_cleanup;
121520 }
121521 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
121522 assert( pIdx );
121523 aRegIdx[i] = ++pParse->nMem;
121524 pParse->nMem += pIdx->nColumn;
121525 }
121526 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
121527 }
121528#ifndef SQLITE_OMIT_UPSERT
121529 if( pUpsert ){
121530 if( IsVirtual(pTab) ){
121531 sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
121532 pTab->zName);
121533 goto insert_cleanup;
121534 }
121535 if( pTab->pSelect ){
121536 sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
121537 goto insert_cleanup;
121538 }
121539 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
121540 goto insert_cleanup;
121541 }
121542 pTabList->a[0].iCursor = iDataCur;
121543 pUpsert->pUpsertSrc = pTabList;
121544 pUpsert->regData = regData;
121545 pUpsert->iDataCur = iDataCur;
121546 pUpsert->iIdxCur = iIdxCur;
121547 if( pUpsert->pUpsertTarget ){
121548 sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
121549 }
121550 }
121551#endif
121552
121553
121554 /* This is the top of the main insertion loop */
121555 if( useTempTable ){
121556 /* This block codes the top of loop only. The complete loop is the
121557 ** following pseudocode (template 4):
121558 **
121559 ** rewind temp table, if empty goto D
121560 ** C: loop over rows of intermediate table
121561 ** transfer values form intermediate table into <table>
121562 ** end loop
121563 ** D: ...
121564 */
121565 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
121566 addrCont = sqlite3VdbeCurrentAddr(v);
121567 }else if( pSelect ){
121568 /* This block codes the top of loop only. The complete loop is the
121569 ** following pseudocode (template 3):
121570 **
121571 ** C: yield X, at EOF goto D
121572 ** insert the select result into <table> from R..R+n
121573 ** goto C
121574 ** D: ...
121575 */
121576 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
121577 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
121578 VdbeCoverage(v);
121579 if( ipkColumn>=0 ){
121580 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
121581 ** SELECT, go ahead and copy the value into the rowid slot now, so that
121582 ** the value does not get overwritten by a NULL at tag-20191021-002. */
121583 sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
121584 }
121585 }
121586
121587 /* Compute data for ordinary columns of the new entry. Values
121588 ** are written in storage order into registers starting with regData.
121589 ** Only ordinary columns are computed in this loop. The rowid
121590 ** (if there is one) is computed later and generated columns are
121591 ** computed after the rowid since they might depend on the value
121592 ** of the rowid.
121593 */
121594 nHidden = 0;
121595 iRegStore = regData; assert( regData==regRowid+1 );
121596 for(i=0; i<pTab->nCol; i++, iRegStore++){
121597 int k;
121598 u32 colFlags;
121599 assert( i>=nHidden );
121600 if( i==pTab->iPKey ){
121601 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
121602 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
121603 ** using excess space. The file format definition requires this extra
121604 ** NULL - we cannot optimize further by skipping the column completely */
121605 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
121606 continue;
121607 }
121608 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
121609 nHidden++;
121610 if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
121611 /* Virtual columns do not participate in OP_MakeRecord. So back up
121612 ** iRegStore by one slot to compensate for the iRegStore++ in the
121613 ** outer for() loop */
121614 iRegStore--;
121615 continue;
121616 }else if( (colFlags & COLFLAG_STORED)!=0 ){
121617 /* Stored columns are computed later. But if there are BEFORE
121618 ** triggers, the slots used for stored columns will be OP_Copy-ed
121619 ** to a second block of registers, so the register needs to be
121620 ** initialized to NULL to avoid an uninitialized register read */
121621 if( tmask & TRIGGER_BEFORE ){
121622 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
121623 }
121624 continue;
121625 }else if( pColumn==0 ){
121626 /* Hidden columns that are not explicitly named in the INSERT
121627 ** get there default value */
121628 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121629 continue;
121630 }
121631 }
121632 if( pColumn ){
121633 for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
121634 if( j>=pColumn->nId ){
121635 /* A column not named in the insert column list gets its
121636 ** default value */
121637 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121638 continue;
121639 }
121640 k = j;
121641 }else if( nColumn==0 ){
121642 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
121643 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
121644 continue;
121645 }else{
121646 k = i - nHidden;
121647 }
121648
121649 if( useTempTable ){
121650 sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
121651 }else if( pSelect ){
121652 if( regFromSelect!=regData ){
121653 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
121654 }
121655 }else{
121656 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
121657 }
121658 }
121659
121660
121661 /* Run the BEFORE and INSTEAD OF triggers, if there are any
121662 */
121663 endOfLoop = sqlite3VdbeMakeLabel(pParse);
121664 if( tmask & TRIGGER_BEFORE ){
121665 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
121666
121667 /* build the NEW.* reference row. Note that if there is an INTEGER
121668 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
121669 ** translated into a unique ID for the row. But on a BEFORE trigger,
121670 ** we do not know what the unique ID will be (because the insert has
121671 ** not happened yet) so we substitute a rowid of -1
121672 */
121673 if( ipkColumn<0 ){
121674 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
121675 }else{
121676 int addr1;
121677 assert( !withoutRowid );
121678 if( useTempTable ){
121679 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
121680 }else{
121681 assert( pSelect==0 ); /* Otherwise useTempTable is true */
121682 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
121683 }
121684 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
121685 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
121686 sqlite3VdbeJumpHere(v, addr1);
121687 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
121688 }
121689
121690 /* Cannot have triggers on a virtual table. If it were possible,
121691 ** this block would have to account for hidden column.
121692 */
121693 assert( !IsVirtual(pTab) );
121694
121695 /* Copy the new data already generated. */
121696 assert( pTab->nNVCol>0 );
121697 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
121698
121699#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121700 /* Compute the new value for generated columns after all other
121701 ** columns have already been computed. This must be done after
121702 ** computing the ROWID in case one of the generated columns
121703 ** refers to the ROWID. */
121704 if( pTab->tabFlags & TF_HasGenerated ){
121705 testcase( pTab->tabFlags & TF_HasVirtual );
121706 testcase( pTab->tabFlags & TF_HasStored );
121707 sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
121708 }
121709#endif
121710
121711 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
121712 ** do not attempt any conversions before assembling the record.
121713 ** If this is a real table, attempt conversions as required by the
121714 ** table column affinities.
121715 */
121716 if( !isView ){
121717 sqlite3TableAffinity(v, pTab, regCols+1);
121718 }
121719
121720 /* Fire BEFORE or INSTEAD OF triggers */
121721 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
121722 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
121723
121724 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
121725 }
121726
121727 if( !isView ){
121728 if( IsVirtual(pTab) ){
121729 /* The row that the VUpdate opcode will delete: none */
121730 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
121731 }
121732 if( ipkColumn>=0 ){
121733 /* Compute the new rowid */
121734 if( useTempTable ){
121735 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
121736 }else if( pSelect ){
121737 /* Rowid already initialized at tag-20191021-001 */
121738 }else{
121739 Expr *pIpk = pList->a[ipkColumn].pExpr;
121740 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
121741 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121742 appendFlag = 1;
121743 }else{
121744 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
121745 }
121746 }
121747 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
121748 ** to generate a unique primary key value.
121749 */
121750 if( !appendFlag ){
121751 int addr1;
121752 if( !IsVirtual(pTab) ){
121753 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
121754 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121755 sqlite3VdbeJumpHere(v, addr1);
121756 }else{
121757 addr1 = sqlite3VdbeCurrentAddr(v);
121758 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
121759 }
121760 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
121761 }
121762 }else if( IsVirtual(pTab) || withoutRowid ){
121763 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
121764 }else{
121765 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
121766 appendFlag = 1;
121767 }
121768 autoIncStep(pParse, regAutoinc, regRowid);
121769
121770#ifndef SQLITE_OMIT_GENERATED_COLUMNS
121771 /* Compute the new value for generated columns after all other
121772 ** columns have already been computed. This must be done after
121773 ** computing the ROWID in case one of the generated columns
121774 ** is derived from the INTEGER PRIMARY KEY. */
121775 if( pTab->tabFlags & TF_HasGenerated ){
121776 sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
121777 }
121778#endif
121779
121780 /* Generate code to check constraints and generate index keys and
121781 ** do the insertion.
121782 */
121783#ifndef SQLITE_OMIT_VIRTUALTABLE
121784 if( IsVirtual(pTab) ){
121785 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
121786 sqlite3VtabMakeWritable(pParse, pTab);
121787 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
121788 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
121789 sqlite3MayAbort(pParse);
121790 }else
121791#endif
121792 {
121793 int isReplace; /* Set to true if constraints may cause a replace */
121794 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
121795 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
121796 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
121797 );
121798 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
121799
121800 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
121801 ** constraints or (b) there are no triggers and this table is not a
121802 ** parent table in a foreign key constraint. It is safe to set the
121803 ** flag in the second case as if any REPLACE constraint is hit, an
121804 ** OP_Delete or OP_IdxDelete instruction will be executed on each
121805 ** cursor that is disturbed. And these instructions both clear the
121806 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
121807 ** functionality. */
121808 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
121809 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
121810 regIns, aRegIdx, 0, appendFlag, bUseSeek
121811 );
121812 }
121813 }
121814
121815 /* Update the count of rows that are inserted
121816 */
121817 if( regRowCount ){
121818 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
121819 }
121820
121821 if( pTrigger ){
121822 /* Code AFTER triggers */
121823 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
121824 pTab, regData-2-pTab->nCol, onError, endOfLoop);
121825 }
121826
121827 /* The bottom of the main insertion loop, if the data source
121828 ** is a SELECT statement.
121829 */
121830 sqlite3VdbeResolveLabel(v, endOfLoop);
121831 if( useTempTable ){
121832 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
121833 sqlite3VdbeJumpHere(v, addrInsTop);
121834 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
121835 }else if( pSelect ){
121836 sqlite3VdbeGoto(v, addrCont);
121837#ifdef SQLITE_DEBUG
121838 /* If we are jumping back to an OP_Yield that is preceded by an
121839 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
121840 ** OP_ReleaseReg will be included in the loop. */
121841 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
121842 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
121843 sqlite3VdbeChangeP5(v, 1);
121844 }
121845#endif
121846 sqlite3VdbeJumpHere(v, addrInsTop);
121847 }
121848
121849insert_end:
121850 /* Update the sqlite_sequence table by storing the content of the
121851 ** maximum rowid counter values recorded while inserting into
121852 ** autoincrement tables.
121853 */
121854 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
121855 sqlite3AutoincrementEnd(pParse);
121856 }
121857
121858 /*
121859 ** Return the number of rows inserted. If this routine is
121860 ** generating code because of a call to sqlite3NestedParse(), do not
121861 ** invoke the callback function.
121862 */
121863 if( regRowCount ){
121864 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
121865 sqlite3VdbeSetNumCols(v, 1);
121866 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
121867 }
121868
121869insert_cleanup:
121870 sqlite3SrcListDelete(db, pTabList);
121871 sqlite3ExprListDelete(db, pList);
121872 sqlite3UpsertDelete(db, pUpsert);
121873 sqlite3SelectDelete(db, pSelect);
121874 sqlite3IdListDelete(db, pColumn);
121875 sqlite3DbFree(db, aRegIdx);
121876}
121877
121878/* Make sure "isView" and other macros defined above are undefined. Otherwise
121879** they may interfere with compilation of other functions in this file
121880** (or in another file, if this file becomes part of the amalgamation). */
121881#ifdef isView
121882 #undef isView
121883#endif
121884#ifdef pTrigger
121885 #undef pTrigger
121886#endif
121887#ifdef tmask
121888 #undef tmask
121889#endif
121890
121891/*
121892** Meanings of bits in of pWalker->eCode for
121893** sqlite3ExprReferencesUpdatedColumn()
121894*/
121895#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
121896#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
121897
121898/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
121899* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
121900** expression node references any of the
121901** columns that are being modifed by an UPDATE statement.
121902*/
121903static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
121904 if( pExpr->op==TK_COLUMN ){
121905 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
121906 if( pExpr->iColumn>=0 ){
121907 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
121908 pWalker->eCode |= CKCNSTRNT_COLUMN;
121909 }
121910 }else{
121911 pWalker->eCode |= CKCNSTRNT_ROWID;
121912 }
121913 }
121914 return WRC_Continue;
121915}
121916
121917/*
121918** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
121919** only columns that are modified by the UPDATE are those for which
121920** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
121921**
121922** Return true if CHECK constraint pExpr uses any of the
121923** changing columns (or the rowid if it is changing). In other words,
121924** return true if this CHECK constraint must be validated for
121925** the new row in the UPDATE statement.
121926**
121927** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
121928** The operation of this routine is the same - return true if an only if
121929** the expression uses one or more of columns identified by the second and
121930** third arguments.
121931*/
121932SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
121933 Expr *pExpr, /* The expression to be checked */
121934 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
121935 int chngRowid /* True if UPDATE changes the rowid */
121936){
121937 Walker w;
121938 memset(&w, 0, sizeof(w));
121939 w.eCode = 0;
121940 w.xExprCallback = checkConstraintExprNode;
121941 w.u.aiCol = aiChng;
121942 sqlite3WalkExpr(&w, pExpr);
121943 if( !chngRowid ){
121944 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
121945 w.eCode &= ~CKCNSTRNT_ROWID;
121946 }
121947 testcase( w.eCode==0 );
121948 testcase( w.eCode==CKCNSTRNT_COLUMN );
121949 testcase( w.eCode==CKCNSTRNT_ROWID );
121950 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
121951 return w.eCode!=0;
121952}
121953
121954/*
121955** Generate code to do constraint checks prior to an INSERT or an UPDATE
121956** on table pTab.
121957**
121958** The regNewData parameter is the first register in a range that contains
121959** the data to be inserted or the data after the update. There will be
121960** pTab->nCol+1 registers in this range. The first register (the one
121961** that regNewData points to) will contain the new rowid, or NULL in the
121962** case of a WITHOUT ROWID table. The second register in the range will
121963** contain the content of the first table column. The third register will
121964** contain the content of the second table column. And so forth.
121965**
121966** The regOldData parameter is similar to regNewData except that it contains
121967** the data prior to an UPDATE rather than afterwards. regOldData is zero
121968** for an INSERT. This routine can distinguish between UPDATE and INSERT by
121969** checking regOldData for zero.
121970**
121971** For an UPDATE, the pkChng boolean is true if the true primary key (the
121972** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
121973** might be modified by the UPDATE. If pkChng is false, then the key of
121974** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
121975**
121976** For an INSERT, the pkChng boolean indicates whether or not the rowid
121977** was explicitly specified as part of the INSERT statement. If pkChng
121978** is zero, it means that the either rowid is computed automatically or
121979** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
121980** pkChng will only be true if the INSERT statement provides an integer
121981** value for either the rowid column or its INTEGER PRIMARY KEY alias.
121982**
121983** The code generated by this routine will store new index entries into
121984** registers identified by aRegIdx[]. No index entry is created for
121985** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
121986** the same as the order of indices on the linked list of indices
121987** at pTab->pIndex.
121988**
121989** (2019-05-07) The generated code also creates a new record for the
121990** main table, if pTab is a rowid table, and stores that record in the
121991** register identified by aRegIdx[nIdx] - in other words in the first
121992** entry of aRegIdx[] past the last index. It is important that the
121993** record be generated during constraint checks to avoid affinity changes
121994** to the register content that occur after constraint checks but before
121995** the new record is inserted.
121996**
121997** The caller must have already opened writeable cursors on the main
121998** table and all applicable indices (that is to say, all indices for which
121999** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
122000** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
122001** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
122002** for the first index in the pTab->pIndex list. Cursors for other indices
122003** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
122004**
122005** This routine also generates code to check constraints. NOT NULL,
122006** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
122007** then the appropriate action is performed. There are five possible
122008** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
122009**
122010** Constraint type Action What Happens
122011** --------------- ---------- ----------------------------------------
122012** any ROLLBACK The current transaction is rolled back and
122013** sqlite3_step() returns immediately with a
122014** return code of SQLITE_CONSTRAINT.
122015**
122016** any ABORT Back out changes from the current command
122017** only (do not do a complete rollback) then
122018** cause sqlite3_step() to return immediately
122019** with SQLITE_CONSTRAINT.
122020**
122021** any FAIL Sqlite3_step() returns immediately with a
122022** return code of SQLITE_CONSTRAINT. The
122023** transaction is not rolled back and any
122024** changes to prior rows are retained.
122025**
122026** any IGNORE The attempt in insert or update the current
122027** row is skipped, without throwing an error.
122028** Processing continues with the next row.
122029** (There is an immediate jump to ignoreDest.)
122030**
122031** NOT NULL REPLACE The NULL value is replace by the default
122032** value for that column. If the default value
122033** is NULL, the action is the same as ABORT.
122034**
122035** UNIQUE REPLACE The other row that conflicts with the row
122036** being inserted is removed.
122037**
122038** CHECK REPLACE Illegal. The results in an exception.
122039**
122040** Which action to take is determined by the overrideError parameter.
122041** Or if overrideError==OE_Default, then the pParse->onError parameter
122042** is used. Or if pParse->onError==OE_Default then the onError value
122043** for the constraint is used.
122044*/
122045SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
122046 Parse *pParse, /* The parser context */
122047 Table *pTab, /* The table being inserted or updated */
122048 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
122049 int iDataCur, /* Canonical data cursor (main table or PK index) */
122050 int iIdxCur, /* First index cursor */
122051 int regNewData, /* First register in a range holding values to insert */
122052 int regOldData, /* Previous content. 0 for INSERTs */
122053 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
122054 u8 overrideError, /* Override onError to this if not OE_Default */
122055 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
122056 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
122057 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
122058 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
122059){
122060 Vdbe *v; /* VDBE under constrution */
122061 Index *pIdx; /* Pointer to one of the indices */
122062 Index *pPk = 0; /* The PRIMARY KEY index */
122063 sqlite3 *db; /* Database connection */
122064 int i; /* loop counter */
122065 int ix; /* Index loop counter */
122066 int nCol; /* Number of columns */
122067 int onError; /* Conflict resolution strategy */
122068 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
122069 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
122070 Index *pUpIdx = 0; /* Index to which to apply the upsert */
122071 u8 isUpdate; /* True if this is an UPDATE operation */
122072 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
122073 int upsertBypass = 0; /* Address of Goto to bypass upsert subroutine */
122074 int upsertJump = 0; /* Address of Goto that jumps into upsert subroutine */
122075 int ipkTop = 0; /* Top of the IPK uniqueness check */
122076 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
122077 /* Variables associated with retesting uniqueness constraints after
122078 ** replace triggers fire have run */
122079 int regTrigCnt; /* Register used to count replace trigger invocations */
122080 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
122081 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
122082 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
122083 int nReplaceTrig = 0; /* Number of replace triggers coded */
122084
122085 isUpdate = regOldData!=0;
122086 db = pParse->db;
122087 v = sqlite3GetVdbe(pParse);
122088 assert( v!=0 );
122089 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
122090 nCol = pTab->nCol;
122091
122092 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
122093 ** normal rowid tables. nPkField is the number of key fields in the
122094 ** pPk index or 1 for a rowid table. In other words, nPkField is the
122095 ** number of fields in the true primary key of the table. */
122096 if( HasRowid(pTab) ){
122097 pPk = 0;
122098 nPkField = 1;
122099 }else{
122100 pPk = sqlite3PrimaryKeyIndex(pTab);
122101 nPkField = pPk->nKeyCol;
122102 }
122103
122104 /* Record that this module has started */
122105 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
122106 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
122107
122108 /* Test all NOT NULL constraints.
122109 */
122110 if( pTab->tabFlags & TF_HasNotNull ){
122111 int b2ndPass = 0; /* True if currently running 2nd pass */
122112 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
122113 int nGenerated = 0; /* Number of generated columns with NOT NULL */
122114 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
122115 for(i=0; i<nCol; i++){
122116 int iReg; /* Register holding column value */
122117 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
122118 int isGenerated; /* non-zero if column is generated */
122119 onError = pCol->notNull;
122120 if( onError==OE_None ) continue; /* No NOT NULL on this column */
122121 if( i==pTab->iPKey ){
122122 continue; /* ROWID is never NULL */
122123 }
122124 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
122125 if( isGenerated && !b2ndPass ){
122126 nGenerated++;
122127 continue; /* Generated columns processed on 2nd pass */
122128 }
122129 if( aiChng && aiChng[i]<0 && !isGenerated ){
122130 /* Do not check NOT NULL on columns that do not change */
122131 continue;
122132 }
122133 if( overrideError!=OE_Default ){
122134 onError = overrideError;
122135 }else if( onError==OE_Default ){
122136 onError = OE_Abort;
122137 }
122138 if( onError==OE_Replace ){
122139 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
122140 || pCol->pDflt==0 /* REPLACE is ABORT if no DEFAULT value */
122141 ){
122142 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
122143 testcase( pCol->colFlags & COLFLAG_STORED );
122144 testcase( pCol->colFlags & COLFLAG_GENERATED );
122145 onError = OE_Abort;
122146 }else{
122147 assert( !isGenerated );
122148 }
122149 }else if( b2ndPass && !isGenerated ){
122150 continue;
122151 }
122152 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
122153 || onError==OE_Ignore || onError==OE_Replace );
122154 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
122155 iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
122156 switch( onError ){
122157 case OE_Replace: {
122158 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
122159 VdbeCoverage(v);
122160 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
122161 nSeenReplace++;
122162 sqlite3ExprCodeCopy(pParse, pCol->pDflt, iReg);
122163 sqlite3VdbeJumpHere(v, addr1);
122164 break;
122165 }
122166 case OE_Abort:
122167 sqlite3MayAbort(pParse);
122168 /* no break */ deliberate_fall_through
122169 case OE_Rollback:
122170 case OE_Fail: {
122171 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
122172 pCol->zName);
122173 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
122174 onError, iReg);
122175 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
122176 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
122177 VdbeCoverage(v);
122178 break;
122179 }
122180 default: {
122181 assert( onError==OE_Ignore );
122182 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest);
122183 VdbeCoverage(v);
122184 break;
122185 }
122186 } /* end switch(onError) */
122187 } /* end loop i over columns */
122188 if( nGenerated==0 && nSeenReplace==0 ){
122189 /* If there are no generated columns with NOT NULL constraints
122190 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
122191 ** pass is sufficient */
122192 break;
122193 }
122194 if( b2ndPass ) break; /* Never need more than 2 passes */
122195 b2ndPass = 1;
122196#ifndef SQLITE_OMIT_GENERATED_COLUMNS
122197 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
122198 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
122199 ** first pass, recomputed values for all generated columns, as
122200 ** those values might depend on columns affected by the REPLACE.
122201 */
122202 sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
122203 }
122204#endif
122205 } /* end of 2-pass loop */
122206 } /* end if( has-not-null-constraints ) */
122207
122208 /* Test all CHECK constraints
122209 */
122210#ifndef SQLITE_OMIT_CHECK
122211 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
122212 ExprList *pCheck = pTab->pCheck;
122213 pParse->iSelfTab = -(regNewData+1);
122214 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
122215 for(i=0; i<pCheck->nExpr; i++){
122216 int allOk;
122217 Expr *pCopy;
122218 Expr *pExpr = pCheck->a[i].pExpr;
122219 if( aiChng
122220 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
122221 ){
122222 /* The check constraints do not reference any of the columns being
122223 ** updated so there is no point it verifying the check constraint */
122224 continue;
122225 }
122226 if( bAffinityDone==0 ){
122227 sqlite3TableAffinity(v, pTab, regNewData+1);
122228 bAffinityDone = 1;
122229 }
122230 allOk = sqlite3VdbeMakeLabel(pParse);
122231 sqlite3VdbeVerifyAbortable(v, onError);
122232 pCopy = sqlite3ExprDup(db, pExpr, 0);
122233 if( !db->mallocFailed ){
122234 sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
122235 }
122236 sqlite3ExprDelete(db, pCopy);
122237 if( onError==OE_Ignore ){
122238 sqlite3VdbeGoto(v, ignoreDest);
122239 }else{
122240 char *zName = pCheck->a[i].zEName;
122241 if( zName==0 ) zName = pTab->zName;
122242 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
122243 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
122244 onError, zName, P4_TRANSIENT,
122245 P5_ConstraintCheck);
122246 }
122247 sqlite3VdbeResolveLabel(v, allOk);
122248 }
122249 pParse->iSelfTab = 0;
122250 }
122251#endif /* !defined(SQLITE_OMIT_CHECK) */
122252
122253 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
122254 ** order:
122255 **
122256 ** (1) OE_Update
122257 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
122258 ** (3) OE_Replace
122259 **
122260 ** OE_Fail and OE_Ignore must happen before any changes are made.
122261 ** OE_Update guarantees that only a single row will change, so it
122262 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
122263 ** could happen in any order, but they are grouped up front for
122264 ** convenience.
122265 **
122266 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
122267 ** The order of constraints used to have OE_Update as (2) and OE_Abort
122268 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
122269 ** constraint before any others, so it had to be moved.
122270 **
122271 ** Constraint checking code is generated in this order:
122272 ** (A) The rowid constraint
122273 ** (B) Unique index constraints that do not have OE_Replace as their
122274 ** default conflict resolution strategy
122275 ** (C) Unique index that do use OE_Replace by default.
122276 **
122277 ** The ordering of (2) and (3) is accomplished by making sure the linked
122278 ** list of indexes attached to a table puts all OE_Replace indexes last
122279 ** in the list. See sqlite3CreateIndex() for where that happens.
122280 */
122281
122282 if( pUpsert ){
122283 if( pUpsert->pUpsertTarget==0 ){
122284 /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
122285 ** Make all unique constraint resolution be OE_Ignore */
122286 assert( pUpsert->pUpsertSet==0 );
122287 overrideError = OE_Ignore;
122288 pUpsert = 0;
122289 }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
122290 /* If the constraint-target uniqueness check must be run first.
122291 ** Jump to that uniqueness check now */
122292 upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
122293 VdbeComment((v, "UPSERT constraint goes first"));
122294 }
122295 }
122296
122297 /* Determine if it is possible that triggers (either explicitly coded
122298 ** triggers or FK resolution actions) might run as a result of deletes
122299 ** that happen when OE_Replace conflict resolution occurs. (Call these
122300 ** "replace triggers".) If any replace triggers run, we will need to
122301 ** recheck all of the uniqueness constraints after they have all run.
122302 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
122303 **
122304 ** If replace triggers are a possibility, then
122305 **
122306 ** (1) Allocate register regTrigCnt and initialize it to zero.
122307 ** That register will count the number of replace triggers that
122308 ** fire. Constraint recheck only occurs if the number is positive.
122309 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
122310 ** (3) Initialize addrRecheck and lblRecheckOk
122311 **
122312 ** The uniqueness rechecking code will create a series of tests to run
122313 ** in a second pass. The addrRecheck and lblRecheckOk variables are
122314 ** used to link together these tests which are separated from each other
122315 ** in the generate bytecode.
122316 */
122317 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
122318 /* There are not DELETE triggers nor FK constraints. No constraint
122319 ** rechecks are needed. */
122320 pTrigger = 0;
122321 regTrigCnt = 0;
122322 }else{
122323 if( db->flags&SQLITE_RecTriggers ){
122324 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
122325 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
122326 }else{
122327 pTrigger = 0;
122328 regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
122329 }
122330 if( regTrigCnt ){
122331 /* Replace triggers might exist. Allocate the counter and
122332 ** initialize it to zero. */
122333 regTrigCnt = ++pParse->nMem;
122334 sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt);
122335 VdbeComment((v, "trigger count"));
122336 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
122337 addrRecheck = lblRecheckOk;
122338 }
122339 }
122340
122341 /* If rowid is changing, make sure the new rowid does not previously
122342 ** exist in the table.
122343 */
122344 if( pkChng && pPk==0 ){
122345 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
122346
122347 /* Figure out what action to take in case of a rowid collision */
122348 onError = pTab->keyConf;
122349 if( overrideError!=OE_Default ){
122350 onError = overrideError;
122351 }else if( onError==OE_Default ){
122352 onError = OE_Abort;
122353 }
122354
122355 /* figure out whether or not upsert applies in this case */
122356 if( pUpsert && pUpsert->pUpsertIdx==0 ){
122357 if( pUpsert->pUpsertSet==0 ){
122358 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
122359 }else{
122360 onError = OE_Update; /* DO UPDATE */
122361 }
122362 }
122363
122364 /* If the response to a rowid conflict is REPLACE but the response
122365 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
122366 ** to defer the running of the rowid conflict checking until after
122367 ** the UNIQUE constraints have run.
122368 */
122369 if( onError==OE_Replace /* IPK rule is REPLACE */
122370 && onError!=overrideError /* Rules for other contraints are different */
122371 && pTab->pIndex /* There exist other constraints */
122372 ){
122373 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
122374 VdbeComment((v, "defer IPK REPLACE until last"));
122375 }
122376
122377 if( isUpdate ){
122378 /* pkChng!=0 does not mean that the rowid has changed, only that
122379 ** it might have changed. Skip the conflict logic below if the rowid
122380 ** is unchanged. */
122381 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
122382 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122383 VdbeCoverage(v);
122384 }
122385
122386 /* Check to see if the new rowid already exists in the table. Skip
122387 ** the following conflict logic if it does not. */
122388 VdbeNoopComment((v, "uniqueness check for ROWID"));
122389 sqlite3VdbeVerifyAbortable(v, onError);
122390 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
122391 VdbeCoverage(v);
122392
122393 switch( onError ){
122394 default: {
122395 onError = OE_Abort;
122396 /* no break */ deliberate_fall_through
122397 }
122398 case OE_Rollback:
122399 case OE_Abort:
122400 case OE_Fail: {
122401 testcase( onError==OE_Rollback );
122402 testcase( onError==OE_Abort );
122403 testcase( onError==OE_Fail );
122404 sqlite3RowidConstraint(pParse, onError, pTab);
122405 break;
122406 }
122407 case OE_Replace: {
122408 /* If there are DELETE triggers on this table and the
122409 ** recursive-triggers flag is set, call GenerateRowDelete() to
122410 ** remove the conflicting row from the table. This will fire
122411 ** the triggers and remove both the table and index b-tree entries.
122412 **
122413 ** Otherwise, if there are no triggers or the recursive-triggers
122414 ** flag is not set, but the table has one or more indexes, call
122415 ** GenerateRowIndexDelete(). This removes the index b-tree entries
122416 ** only. The table b-tree entry will be replaced by the new entry
122417 ** when it is inserted.
122418 **
122419 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
122420 ** also invoke MultiWrite() to indicate that this VDBE may require
122421 ** statement rollback (if the statement is aborted after the delete
122422 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
122423 ** but being more selective here allows statements like:
122424 **
122425 ** REPLACE INTO t(rowid) VALUES($newrowid)
122426 **
122427 ** to run without a statement journal if there are no indexes on the
122428 ** table.
122429 */
122430 if( regTrigCnt ){
122431 sqlite3MultiWrite(pParse);
122432 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
122433 regNewData, 1, 0, OE_Replace, 1, -1);
122434 sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
122435 nReplaceTrig++;
122436 }else{
122437#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
122438 assert( HasRowid(pTab) );
122439 /* This OP_Delete opcode fires the pre-update-hook only. It does
122440 ** not modify the b-tree. It is more efficient to let the coming
122441 ** OP_Insert replace the existing entry than it is to delete the
122442 ** existing entry and then insert a new one. */
122443 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
122444 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
122445#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
122446 if( pTab->pIndex ){
122447 sqlite3MultiWrite(pParse);
122448 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
122449 }
122450 }
122451 seenReplace = 1;
122452 break;
122453 }
122454#ifndef SQLITE_OMIT_UPSERT
122455 case OE_Update: {
122456 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
122457 /* no break */ deliberate_fall_through
122458 }
122459#endif
122460 case OE_Ignore: {
122461 testcase( onError==OE_Ignore );
122462 sqlite3VdbeGoto(v, ignoreDest);
122463 break;
122464 }
122465 }
122466 sqlite3VdbeResolveLabel(v, addrRowidOk);
122467 if( ipkTop ){
122468 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
122469 sqlite3VdbeJumpHere(v, ipkTop-1);
122470 }
122471 }
122472
122473 /* Test all UNIQUE constraints by creating entries for each UNIQUE
122474 ** index and making sure that duplicate entries do not already exist.
122475 ** Compute the revised record entries for indices as we go.
122476 **
122477 ** This loop also handles the case of the PRIMARY KEY index for a
122478 ** WITHOUT ROWID table.
122479 */
122480 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
122481 int regIdx; /* Range of registers hold conent for pIdx */
122482 int regR; /* Range of registers holding conflicting PK */
122483 int iThisCur; /* Cursor for this UNIQUE index */
122484 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
122485 int addrConflictCk; /* First opcode in the conflict check logic */
122486
122487 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
122488 if( pUpIdx==pIdx ){
122489 addrUniqueOk = upsertJump+1;
122490 upsertBypass = sqlite3VdbeGoto(v, 0);
122491 VdbeComment((v, "Skip upsert subroutine"));
122492 sqlite3VdbeJumpHere(v, upsertJump);
122493 }else{
122494 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
122495 }
122496 if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
122497 sqlite3TableAffinity(v, pTab, regNewData+1);
122498 bAffinityDone = 1;
122499 }
122500 VdbeNoopComment((v, "prep index %s", pIdx->zName));
122501 iThisCur = iIdxCur+ix;
122502
122503
122504 /* Skip partial indices for which the WHERE clause is not true */
122505 if( pIdx->pPartIdxWhere ){
122506 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
122507 pParse->iSelfTab = -(regNewData+1);
122508 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
122509 SQLITE_JUMPIFNULL);
122510 pParse->iSelfTab = 0;
122511 }
122512
122513 /* Create a record for this index entry as it should appear after
122514 ** the insert or update. Store that record in the aRegIdx[ix] register
122515 */
122516 regIdx = aRegIdx[ix]+1;
122517 for(i=0; i<pIdx->nColumn; i++){
122518 int iField = pIdx->aiColumn[i];
122519 int x;
122520 if( iField==XN_EXPR ){
122521 pParse->iSelfTab = -(regNewData+1);
122522 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
122523 pParse->iSelfTab = 0;
122524 VdbeComment((v, "%s column %d", pIdx->zName, i));
122525 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
122526 x = regNewData;
122527 sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i);
122528 VdbeComment((v, "rowid"));
122529 }else{
122530 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
122531 x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
122532 sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
122533 VdbeComment((v, "%s", pTab->aCol[iField].zName));
122534 }
122535 }
122536 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
122537 VdbeComment((v, "for %s", pIdx->zName));
122538#ifdef SQLITE_ENABLE_NULL_TRIM
122539 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
122540 sqlite3SetMakeRecordP5(v, pIdx->pTable);
122541 }
122542#endif
122543 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
122544
122545 /* In an UPDATE operation, if this index is the PRIMARY KEY index
122546 ** of a WITHOUT ROWID table and there has been no change the
122547 ** primary key, then no collision is possible. The collision detection
122548 ** logic below can all be skipped. */
122549 if( isUpdate && pPk==pIdx && pkChng==0 ){
122550 sqlite3VdbeResolveLabel(v, addrUniqueOk);
122551 continue;
122552 }
122553
122554 /* Find out what action to take in case there is a uniqueness conflict */
122555 onError = pIdx->onError;
122556 if( onError==OE_None ){
122557 sqlite3VdbeResolveLabel(v, addrUniqueOk);
122558 continue; /* pIdx is not a UNIQUE index */
122559 }
122560 if( overrideError!=OE_Default ){
122561 onError = overrideError;
122562 }else if( onError==OE_Default ){
122563 onError = OE_Abort;
122564 }
122565
122566 /* Figure out if the upsert clause applies to this index */
122567 if( pUpIdx==pIdx ){
122568 if( pUpsert->pUpsertSet==0 ){
122569 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
122570 }else{
122571 onError = OE_Update; /* DO UPDATE */
122572 }
122573 }
122574
122575 /* Collision detection may be omitted if all of the following are true:
122576 ** (1) The conflict resolution algorithm is REPLACE
122577 ** (2) The table is a WITHOUT ROWID table
122578 ** (3) There are no secondary indexes on the table
122579 ** (4) No delete triggers need to be fired if there is a conflict
122580 ** (5) No FK constraint counters need to be updated if a conflict occurs.
122581 **
122582 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
122583 ** must be explicitly deleted in order to ensure any pre-update hook
122584 ** is invoked. */
122585#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
122586 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
122587 && pPk==pIdx /* Condition 2 */
122588 && onError==OE_Replace /* Condition 1 */
122589 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
122590 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
122591 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
122592 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
122593 ){
122594 sqlite3VdbeResolveLabel(v, addrUniqueOk);
122595 continue;
122596 }
122597#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
122598
122599 /* Check to see if the new index entry will be unique */
122600 sqlite3VdbeVerifyAbortable(v, onError);
122601 addrConflictCk =
122602 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
122603 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
122604
122605 /* Generate code to handle collisions */
122606 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
122607 if( isUpdate || onError==OE_Replace ){
122608 if( HasRowid(pTab) ){
122609 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
122610 /* Conflict only if the rowid of the existing index entry
122611 ** is different from old-rowid */
122612 if( isUpdate ){
122613 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
122614 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122615 VdbeCoverage(v);
122616 }
122617 }else{
122618 int x;
122619 /* Extract the PRIMARY KEY from the end of the index entry and
122620 ** store it in registers regR..regR+nPk-1 */
122621 if( pIdx!=pPk ){
122622 for(i=0; i<pPk->nKeyCol; i++){
122623 assert( pPk->aiColumn[i]>=0 );
122624 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
122625 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
122626 VdbeComment((v, "%s.%s", pTab->zName,
122627 pTab->aCol[pPk->aiColumn[i]].zName));
122628 }
122629 }
122630 if( isUpdate ){
122631 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
122632 ** table, only conflict if the new PRIMARY KEY values are actually
122633 ** different from the old.
122634 **
122635 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
122636 ** of the matched index row are different from the original PRIMARY
122637 ** KEY values of this row before the update. */
122638 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
122639 int op = OP_Ne;
122640 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
122641
122642 for(i=0; i<pPk->nKeyCol; i++){
122643 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
122644 x = pPk->aiColumn[i];
122645 assert( x>=0 );
122646 if( i==(pPk->nKeyCol-1) ){
122647 addrJump = addrUniqueOk;
122648 op = OP_Eq;
122649 }
122650 x = sqlite3TableColumnToStorage(pTab, x);
122651 sqlite3VdbeAddOp4(v, op,
122652 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
122653 );
122654 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122655 VdbeCoverageIf(v, op==OP_Eq);
122656 VdbeCoverageIf(v, op==OP_Ne);
122657 }
122658 }
122659 }
122660 }
122661
122662 /* Generate code that executes if the new index entry is not unique */
122663 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
122664 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
122665 switch( onError ){
122666 case OE_Rollback:
122667 case OE_Abort:
122668 case OE_Fail: {
122669 testcase( onError==OE_Rollback );
122670 testcase( onError==OE_Abort );
122671 testcase( onError==OE_Fail );
122672 sqlite3UniqueConstraint(pParse, onError, pIdx);
122673 break;
122674 }
122675#ifndef SQLITE_OMIT_UPSERT
122676 case OE_Update: {
122677 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
122678 /* no break */ deliberate_fall_through
122679 }
122680#endif
122681 case OE_Ignore: {
122682 testcase( onError==OE_Ignore );
122683 sqlite3VdbeGoto(v, ignoreDest);
122684 break;
122685 }
122686 default: {
122687 int nConflictCk; /* Number of opcodes in conflict check logic */
122688
122689 assert( onError==OE_Replace );
122690 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
122691 assert( nConflictCk>0 );
122692 testcase( nConflictCk>1 );
122693 if( regTrigCnt ){
122694 sqlite3MultiWrite(pParse);
122695 nReplaceTrig++;
122696 }
122697 if( pTrigger && isUpdate ){
122698 sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur);
122699 }
122700 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
122701 regR, nPkField, 0, OE_Replace,
122702 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
122703 if( pTrigger && isUpdate ){
122704 sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur);
122705 }
122706 if( regTrigCnt ){
122707 int addrBypass; /* Jump destination to bypass recheck logic */
122708
122709 sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
122710 addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */
122711 VdbeComment((v, "bypass recheck"));
122712
122713 /* Here we insert code that will be invoked after all constraint
122714 ** checks have run, if and only if one or more replace triggers
122715 ** fired. */
122716 sqlite3VdbeResolveLabel(v, lblRecheckOk);
122717 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
122718 if( pIdx->pPartIdxWhere ){
122719 /* Bypass the recheck if this partial index is not defined
122720 ** for the current row */
122721 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
122722 VdbeCoverage(v);
122723 }
122724 /* Copy the constraint check code from above, except change
122725 ** the constraint-ok jump destination to be the address of
122726 ** the next retest block */
122727 while( nConflictCk>0 ){
122728 VdbeOp x; /* Conflict check opcode to copy */
122729 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
122730 ** Hence, make a complete copy of the opcode, rather than using
122731 ** a pointer to the opcode. */
122732 x = *sqlite3VdbeGetOp(v, addrConflictCk);
122733 if( x.opcode!=OP_IdxRowid ){
122734 int p2; /* New P2 value for copied conflict check opcode */
122735 const char *zP4;
122736 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
122737 p2 = lblRecheckOk;
122738 }else{
122739 p2 = x.p2;
122740 }
122741 zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
122742 sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
122743 sqlite3VdbeChangeP5(v, x.p5);
122744 VdbeCoverageIf(v, p2!=x.p2);
122745 }
122746 nConflictCk--;
122747 addrConflictCk++;
122748 }
122749 /* If the retest fails, issue an abort */
122750 sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
122751
122752 sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
122753 }
122754 seenReplace = 1;
122755 break;
122756 }
122757 }
122758 if( pUpIdx==pIdx ){
122759 sqlite3VdbeGoto(v, upsertJump+1);
122760 sqlite3VdbeJumpHere(v, upsertBypass);
122761 }else{
122762 sqlite3VdbeResolveLabel(v, addrUniqueOk);
122763 }
122764 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
122765 }
122766
122767 /* If the IPK constraint is a REPLACE, run it last */
122768 if( ipkTop ){
122769 sqlite3VdbeGoto(v, ipkTop);
122770 VdbeComment((v, "Do IPK REPLACE"));
122771 sqlite3VdbeJumpHere(v, ipkBottom);
122772 }
122773
122774 /* Recheck all uniqueness constraints after replace triggers have run */
122775 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
122776 assert( regTrigCnt!=0 || nReplaceTrig==0 );
122777 if( nReplaceTrig ){
122778 sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
122779 if( !pPk ){
122780 if( isUpdate ){
122781 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData);
122782 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
122783 VdbeCoverage(v);
122784 }
122785 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData);
122786 VdbeCoverage(v);
122787 sqlite3RowidConstraint(pParse, OE_Abort, pTab);
122788 }else{
122789 sqlite3VdbeGoto(v, addrRecheck);
122790 }
122791 sqlite3VdbeResolveLabel(v, lblRecheckOk);
122792 }
122793
122794 /* Generate the table record */
122795 if( HasRowid(pTab) ){
122796 int regRec = aRegIdx[ix];
122797 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
122798 sqlite3SetMakeRecordP5(v, pTab);
122799 if( !bAffinityDone ){
122800 sqlite3TableAffinity(v, pTab, 0);
122801 }
122802 }
122803
122804 *pbMayReplace = seenReplace;
122805 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
122806}
122807
122808#ifdef SQLITE_ENABLE_NULL_TRIM
122809/*
122810** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
122811** to be the number of columns in table pTab that must not be NULL-trimmed.
122812**
122813** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
122814*/
122815SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
122816 u16 i;
122817
122818 /* Records with omitted columns are only allowed for schema format
122819 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
122820 if( pTab->pSchema->file_format<2 ) return;
122821
122822 for(i=pTab->nCol-1; i>0; i--){
122823 if( pTab->aCol[i].pDflt!=0 ) break;
122824 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
122825 }
122826 sqlite3VdbeChangeP5(v, i+1);
122827}
122828#endif
122829
122830/*
122831** This routine generates code to finish the INSERT or UPDATE operation
122832** that was started by a prior call to sqlite3GenerateConstraintChecks.
122833** A consecutive range of registers starting at regNewData contains the
122834** rowid and the content to be inserted.
122835**
122836** The arguments to this routine should be the same as the first six
122837** arguments to sqlite3GenerateConstraintChecks.
122838*/
122839SQLITE_PRIVATE void sqlite3CompleteInsertion(
122840 Parse *pParse, /* The parser context */
122841 Table *pTab, /* the table into which we are inserting */
122842 int iDataCur, /* Cursor of the canonical data source */
122843 int iIdxCur, /* First index cursor */
122844 int regNewData, /* Range of content */
122845 int *aRegIdx, /* Register used by each index. 0 for unused indices */
122846 int update_flags, /* True for UPDATE, False for INSERT */
122847 int appendBias, /* True if this is likely to be an append */
122848 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
122849){
122850 Vdbe *v; /* Prepared statements under construction */
122851 Index *pIdx; /* An index being inserted or updated */
122852 u8 pik_flags; /* flag values passed to the btree insert */
122853 int i; /* Loop counter */
122854
122855 assert( update_flags==0
122856 || update_flags==OPFLAG_ISUPDATE
122857 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
122858 );
122859
122860 v = sqlite3GetVdbe(pParse);
122861 assert( v!=0 );
122862 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
122863 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
122864 /* All REPLACE indexes are at the end of the list */
122865 assert( pIdx->onError!=OE_Replace
122866 || pIdx->pNext==0
122867 || pIdx->pNext->onError==OE_Replace );
122868 if( aRegIdx[i]==0 ) continue;
122869 if( pIdx->pPartIdxWhere ){
122870 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
122871 VdbeCoverage(v);
122872 }
122873 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
122874 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
122875 assert( pParse->nested==0 );
122876 pik_flags |= OPFLAG_NCHANGE;
122877 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
122878#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
122879 if( update_flags==0 ){
122880 int r = sqlite3GetTempReg(pParse);
122881 sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
122882 sqlite3VdbeAddOp4(v, OP_Insert,
122883 iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
122884 );
122885 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
122886 sqlite3ReleaseTempReg(pParse, r);
122887 }
122888#endif
122889 }
122890 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
122891 aRegIdx[i]+1,
122892 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
122893 sqlite3VdbeChangeP5(v, pik_flags);
122894 }
122895 if( !HasRowid(pTab) ) return;
122896 if( pParse->nested ){
122897 pik_flags = 0;
122898 }else{
122899 pik_flags = OPFLAG_NCHANGE;
122900 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
122901 }
122902 if( appendBias ){
122903 pik_flags |= OPFLAG_APPEND;
122904 }
122905 if( useSeekResult ){
122906 pik_flags |= OPFLAG_USESEEKRESULT;
122907 }
122908 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
122909 if( !pParse->nested ){
122910 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
122911 }
122912 sqlite3VdbeChangeP5(v, pik_flags);
122913}
122914
122915/*
122916** Allocate cursors for the pTab table and all its indices and generate
122917** code to open and initialized those cursors.
122918**
122919** The cursor for the object that contains the complete data (normally
122920** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
122921** ROWID table) is returned in *piDataCur. The first index cursor is
122922** returned in *piIdxCur. The number of indices is returned.
122923**
122924** Use iBase as the first cursor (either the *piDataCur for rowid tables
122925** or the first index for WITHOUT ROWID tables) if it is non-negative.
122926** If iBase is negative, then allocate the next available cursor.
122927**
122928** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
122929** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
122930** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
122931** pTab->pIndex list.
122932**
122933** If pTab is a virtual table, then this routine is a no-op and the
122934** *piDataCur and *piIdxCur values are left uninitialized.
122935*/
122936SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
122937 Parse *pParse, /* Parsing context */
122938 Table *pTab, /* Table to be opened */
122939 int op, /* OP_OpenRead or OP_OpenWrite */
122940 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
122941 int iBase, /* Use this for the table cursor, if there is one */
122942 u8 *aToOpen, /* If not NULL: boolean for each table and index */
122943 int *piDataCur, /* Write the database source cursor number here */
122944 int *piIdxCur /* Write the first index cursor number here */
122945){
122946 int i;
122947 int iDb;
122948 int iDataCur;
122949 Index *pIdx;
122950 Vdbe *v;
122951
122952 assert( op==OP_OpenRead || op==OP_OpenWrite );
122953 assert( op==OP_OpenWrite || p5==0 );
122954 if( IsVirtual(pTab) ){
122955 /* This routine is a no-op for virtual tables. Leave the output
122956 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
122957 ** can detect if they are used by mistake in the caller. */
122958 return 0;
122959 }
122960 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
122961 v = sqlite3GetVdbe(pParse);
122962 assert( v!=0 );
122963 if( iBase<0 ) iBase = pParse->nTab;
122964 iDataCur = iBase++;
122965 if( piDataCur ) *piDataCur = iDataCur;
122966 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
122967 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
122968 }else{
122969 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
122970 }
122971 if( piIdxCur ) *piIdxCur = iBase;
122972 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
122973 int iIdxCur = iBase++;
122974 assert( pIdx->pSchema==pTab->pSchema );
122975 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
122976 if( piDataCur ) *piDataCur = iIdxCur;
122977 p5 = 0;
122978 }
122979 if( aToOpen==0 || aToOpen[i+1] ){
122980 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
122981 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
122982 sqlite3VdbeChangeP5(v, p5);
122983 VdbeComment((v, "%s", pIdx->zName));
122984 }
122985 }
122986 if( iBase>pParse->nTab ) pParse->nTab = iBase;
122987 return i;
122988}
122989
122990
122991#ifdef SQLITE_TEST
122992/*
122993** The following global variable is incremented whenever the
122994** transfer optimization is used. This is used for testing
122995** purposes only - to make sure the transfer optimization really
122996** is happening when it is supposed to.
122997*/
122998SQLITE_API int sqlite3_xferopt_count;
122999#endif /* SQLITE_TEST */
123000
123001
123002#ifndef SQLITE_OMIT_XFER_OPT
123003/*
123004** Check to see if index pSrc is compatible as a source of data
123005** for index pDest in an insert transfer optimization. The rules
123006** for a compatible index:
123007**
123008** * The index is over the same set of columns
123009** * The same DESC and ASC markings occurs on all columns
123010** * The same onError processing (OE_Abort, OE_Ignore, etc)
123011** * The same collating sequence on each column
123012** * The index has the exact same WHERE clause
123013*/
123014static int xferCompatibleIndex(Index *pDest, Index *pSrc){
123015 int i;
123016 assert( pDest && pSrc );
123017 assert( pDest->pTable!=pSrc->pTable );
123018 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
123019 return 0; /* Different number of columns */
123020 }
123021 if( pDest->onError!=pSrc->onError ){
123022 return 0; /* Different conflict resolution strategies */
123023 }
123024 for(i=0; i<pSrc->nKeyCol; i++){
123025 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
123026 return 0; /* Different columns indexed */
123027 }
123028 if( pSrc->aiColumn[i]==XN_EXPR ){
123029 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
123030 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
123031 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
123032 return 0; /* Different expressions in the index */
123033 }
123034 }
123035 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
123036 return 0; /* Different sort orders */
123037 }
123038 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
123039 return 0; /* Different collating sequences */
123040 }
123041 }
123042 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
123043 return 0; /* Different WHERE clauses */
123044 }
123045
123046 /* If no test above fails then the indices must be compatible */
123047 return 1;
123048}
123049
123050/*
123051** Attempt the transfer optimization on INSERTs of the form
123052**
123053** INSERT INTO tab1 SELECT * FROM tab2;
123054**
123055** The xfer optimization transfers raw records from tab2 over to tab1.
123056** Columns are not decoded and reassembled, which greatly improves
123057** performance. Raw index records are transferred in the same way.
123058**
123059** The xfer optimization is only attempted if tab1 and tab2 are compatible.
123060** There are lots of rules for determining compatibility - see comments
123061** embedded in the code for details.
123062**
123063** This routine returns TRUE if the optimization is guaranteed to be used.
123064** Sometimes the xfer optimization will only work if the destination table
123065** is empty - a factor that can only be determined at run-time. In that
123066** case, this routine generates code for the xfer optimization but also
123067** does a test to see if the destination table is empty and jumps over the
123068** xfer optimization code if the test fails. In that case, this routine
123069** returns FALSE so that the caller will know to go ahead and generate
123070** an unoptimized transfer. This routine also returns FALSE if there
123071** is no chance that the xfer optimization can be applied.
123072**
123073** This optimization is particularly useful at making VACUUM run faster.
123074*/
123075static int xferOptimization(
123076 Parse *pParse, /* Parser context */
123077 Table *pDest, /* The table we are inserting into */
123078 Select *pSelect, /* A SELECT statement to use as the data source */
123079 int onError, /* How to handle constraint errors */
123080 int iDbDest /* The database of pDest */
123081){
123082 sqlite3 *db = pParse->db;
123083 ExprList *pEList; /* The result set of the SELECT */
123084 Table *pSrc; /* The table in the FROM clause of SELECT */
123085 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
123086 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
123087 int i; /* Loop counter */
123088 int iDbSrc; /* The database of pSrc */
123089 int iSrc, iDest; /* Cursors from source and destination */
123090 int addr1, addr2; /* Loop addresses */
123091 int emptyDestTest = 0; /* Address of test for empty pDest */
123092 int emptySrcTest = 0; /* Address of test for empty pSrc */
123093 Vdbe *v; /* The VDBE we are building */
123094 int regAutoinc; /* Memory register used by AUTOINC */
123095 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
123096 int regData, regRowid; /* Registers holding data and rowid */
123097
123098 if( pSelect==0 ){
123099 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
123100 }
123101 if( pParse->pWith || pSelect->pWith ){
123102 /* Do not attempt to process this query if there are an WITH clauses
123103 ** attached to it. Proceeding may generate a false "no such table: xxx"
123104 ** error if pSelect reads from a CTE named "xxx". */
123105 return 0;
123106 }
123107 if( sqlite3TriggerList(pParse, pDest) ){
123108 return 0; /* tab1 must not have triggers */
123109 }
123110#ifndef SQLITE_OMIT_VIRTUALTABLE
123111 if( IsVirtual(pDest) ){
123112 return 0; /* tab1 must not be a virtual table */
123113 }
123114#endif
123115 if( onError==OE_Default ){
123116 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
123117 if( onError==OE_Default ) onError = OE_Abort;
123118 }
123119 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
123120 if( pSelect->pSrc->nSrc!=1 ){
123121 return 0; /* FROM clause must have exactly one term */
123122 }
123123 if( pSelect->pSrc->a[0].pSelect ){
123124 return 0; /* FROM clause cannot contain a subquery */
123125 }
123126 if( pSelect->pWhere ){
123127 return 0; /* SELECT may not have a WHERE clause */
123128 }
123129 if( pSelect->pOrderBy ){
123130 return 0; /* SELECT may not have an ORDER BY clause */
123131 }
123132 /* Do not need to test for a HAVING clause. If HAVING is present but
123133 ** there is no ORDER BY, we will get an error. */
123134 if( pSelect->pGroupBy ){
123135 return 0; /* SELECT may not have a GROUP BY clause */
123136 }
123137 if( pSelect->pLimit ){
123138 return 0; /* SELECT may not have a LIMIT clause */
123139 }
123140 if( pSelect->pPrior ){
123141 return 0; /* SELECT may not be a compound query */
123142 }
123143 if( pSelect->selFlags & SF_Distinct ){
123144 return 0; /* SELECT may not be DISTINCT */
123145 }
123146 pEList = pSelect->pEList;
123147 assert( pEList!=0 );
123148 if( pEList->nExpr!=1 ){
123149 return 0; /* The result set must have exactly one column */
123150 }
123151 assert( pEList->a[0].pExpr );
123152 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
123153 return 0; /* The result set must be the special operator "*" */
123154 }
123155
123156 /* At this point we have established that the statement is of the
123157 ** correct syntactic form to participate in this optimization. Now
123158 ** we have to check the semantics.
123159 */
123160 pItem = pSelect->pSrc->a;
123161 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
123162 if( pSrc==0 ){
123163 return 0; /* FROM clause does not contain a real table */
123164 }
123165 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
123166 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
123167 return 0; /* tab1 and tab2 may not be the same table */
123168 }
123169 if( HasRowid(pDest)!=HasRowid(pSrc) ){
123170 return 0; /* source and destination must both be WITHOUT ROWID or not */
123171 }
123172#ifndef SQLITE_OMIT_VIRTUALTABLE
123173 if( IsVirtual(pSrc) ){
123174 return 0; /* tab2 must not be a virtual table */
123175 }
123176#endif
123177 if( pSrc->pSelect ){
123178 return 0; /* tab2 may not be a view */
123179 }
123180 if( pDest->nCol!=pSrc->nCol ){
123181 return 0; /* Number of columns must be the same in tab1 and tab2 */
123182 }
123183 if( pDest->iPKey!=pSrc->iPKey ){
123184 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
123185 }
123186 for(i=0; i<pDest->nCol; i++){
123187 Column *pDestCol = &pDest->aCol[i];
123188 Column *pSrcCol = &pSrc->aCol[i];
123189#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
123190 if( (db->mDbFlags & DBFLAG_Vacuum)==0
123191 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
123192 ){
123193 return 0; /* Neither table may have __hidden__ columns */
123194 }
123195#endif
123196#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123197 /* Even if tables t1 and t2 have identical schemas, if they contain
123198 ** generated columns, then this statement is semantically incorrect:
123199 **
123200 ** INSERT INTO t2 SELECT * FROM t1;
123201 **
123202 ** The reason is that generated column values are returned by the
123203 ** the SELECT statement on the right but the INSERT statement on the
123204 ** left wants them to be omitted.
123205 **
123206 ** Nevertheless, this is a useful notational shorthand to tell SQLite
123207 ** to do a bulk transfer all of the content from t1 over to t2.
123208 **
123209 ** We could, in theory, disable this (except for internal use by the
123210 ** VACUUM command where it is actually needed). But why do that? It
123211 ** seems harmless enough, and provides a useful service.
123212 */
123213 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
123214 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
123215 return 0; /* Both columns have the same generated-column type */
123216 }
123217 /* But the transfer is only allowed if both the source and destination
123218 ** tables have the exact same expressions for generated columns.
123219 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
123220 */
123221 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
123222 if( sqlite3ExprCompare(0, pSrcCol->pDflt, pDestCol->pDflt, -1)!=0 ){
123223 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
123224 testcase( pDestCol->colFlags & COLFLAG_STORED );
123225 return 0; /* Different generator expressions */
123226 }
123227 }
123228#endif
123229 if( pDestCol->affinity!=pSrcCol->affinity ){
123230 return 0; /* Affinity must be the same on all columns */
123231 }
123232 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
123233 return 0; /* Collating sequence must be the same on all columns */
123234 }
123235 if( pDestCol->notNull && !pSrcCol->notNull ){
123236 return 0; /* tab2 must be NOT NULL if tab1 is */
123237 }
123238 /* Default values for second and subsequent columns need to match. */
123239 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
123240 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
123241 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
123242 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
123243 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
123244 pSrcCol->pDflt->u.zToken)!=0)
123245 ){
123246 return 0; /* Default values must be the same for all columns */
123247 }
123248 }
123249 }
123250 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
123251 if( IsUniqueIndex(pDestIdx) ){
123252 destHasUniqueIdx = 1;
123253 }
123254 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
123255 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
123256 }
123257 if( pSrcIdx==0 ){
123258 return 0; /* pDestIdx has no corresponding index in pSrc */
123259 }
123260 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
123261 && sqlite3FaultSim(411)==SQLITE_OK ){
123262 /* The sqlite3FaultSim() call allows this corruption test to be
123263 ** bypassed during testing, in order to exercise other corruption tests
123264 ** further downstream. */
123265 return 0; /* Corrupt schema - two indexes on the same btree */
123266 }
123267 }
123268#ifndef SQLITE_OMIT_CHECK
123269 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
123270 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
123271 }
123272#endif
123273#ifndef SQLITE_OMIT_FOREIGN_KEY
123274 /* Disallow the transfer optimization if the destination table constains
123275 ** any foreign key constraints. This is more restrictive than necessary.
123276 ** But the main beneficiary of the transfer optimization is the VACUUM
123277 ** command, and the VACUUM command disables foreign key constraints. So
123278 ** the extra complication to make this rule less restrictive is probably
123279 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
123280 */
123281 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
123282 return 0;
123283 }
123284#endif
123285 if( (db->flags & SQLITE_CountRows)!=0 ){
123286 return 0; /* xfer opt does not play well with PRAGMA count_changes */
123287 }
123288
123289 /* If we get this far, it means that the xfer optimization is at
123290 ** least a possibility, though it might only work if the destination
123291 ** table (tab1) is initially empty.
123292 */
123293#ifdef SQLITE_TEST
123294 sqlite3_xferopt_count++;
123295#endif
123296 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
123297 v = sqlite3GetVdbe(pParse);
123298 sqlite3CodeVerifySchema(pParse, iDbSrc);
123299 iSrc = pParse->nTab++;
123300 iDest = pParse->nTab++;
123301 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
123302 regData = sqlite3GetTempReg(pParse);
123303 regRowid = sqlite3GetTempReg(pParse);
123304 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
123305 assert( HasRowid(pDest) || destHasUniqueIdx );
123306 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
123307 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
123308 || destHasUniqueIdx /* (2) */
123309 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
123310 )){
123311 /* In some circumstances, we are able to run the xfer optimization
123312 ** only if the destination table is initially empty. Unless the
123313 ** DBFLAG_Vacuum flag is set, this block generates code to make
123314 ** that determination. If DBFLAG_Vacuum is set, then the destination
123315 ** table is always empty.
123316 **
123317 ** Conditions under which the destination must be empty:
123318 **
123319 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
123320 ** (If the destination is not initially empty, the rowid fields
123321 ** of index entries might need to change.)
123322 **
123323 ** (2) The destination has a unique index. (The xfer optimization
123324 ** is unable to test uniqueness.)
123325 **
123326 ** (3) onError is something other than OE_Abort and OE_Rollback.
123327 */
123328 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
123329 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
123330 sqlite3VdbeJumpHere(v, addr1);
123331 }
123332 if( HasRowid(pSrc) ){
123333 u8 insFlags;
123334 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
123335 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
123336 if( pDest->iPKey>=0 ){
123337 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
123338 sqlite3VdbeVerifyAbortable(v, onError);
123339 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
123340 VdbeCoverage(v);
123341 sqlite3RowidConstraint(pParse, onError, pDest);
123342 sqlite3VdbeJumpHere(v, addr2);
123343 autoIncStep(pParse, regAutoinc, regRowid);
123344 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
123345 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
123346 }else{
123347 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
123348 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
123349 }
123350 if( db->mDbFlags & DBFLAG_Vacuum ){
123351 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
123352 insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
123353 }else{
123354 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
123355 }
123356 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
123357 sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
123358 (char*)pDest, P4_TABLE);
123359 sqlite3VdbeChangeP5(v, insFlags);
123360 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
123361 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
123362 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123363 }else{
123364 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
123365 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
123366 }
123367 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
123368 u8 idxInsFlags = 0;
123369 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
123370 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
123371 }
123372 assert( pSrcIdx );
123373 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
123374 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
123375 VdbeComment((v, "%s", pSrcIdx->zName));
123376 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
123377 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
123378 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
123379 VdbeComment((v, "%s", pDestIdx->zName));
123380 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
123381 if( db->mDbFlags & DBFLAG_Vacuum ){
123382 /* This INSERT command is part of a VACUUM operation, which guarantees
123383 ** that the destination table is empty. If all indexed columns use
123384 ** collation sequence BINARY, then it can also be assumed that the
123385 ** index will be populated by inserting keys in strictly sorted
123386 ** order. In this case, instead of seeking within the b-tree as part
123387 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
123388 ** OP_IdxInsert to seek to the point within the b-tree where each key
123389 ** should be inserted. This is faster.
123390 **
123391 ** If any of the indexed columns use a collation sequence other than
123392 ** BINARY, this optimization is disabled. This is because the user
123393 ** might change the definition of a collation sequence and then run
123394 ** a VACUUM command. In that case keys may not be written in strictly
123395 ** sorted order. */
123396 for(i=0; i<pSrcIdx->nColumn; i++){
123397 const char *zColl = pSrcIdx->azColl[i];
123398 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
123399 }
123400 if( i==pSrcIdx->nColumn ){
123401 idxInsFlags = OPFLAG_USESEEKRESULT;
123402 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
123403 }
123404 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
123405 idxInsFlags |= OPFLAG_NCHANGE;
123406 }
123407 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
123408 sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
123409 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
123410 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
123411 sqlite3VdbeJumpHere(v, addr1);
123412 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
123413 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123414 }
123415 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
123416 sqlite3ReleaseTempReg(pParse, regRowid);
123417 sqlite3ReleaseTempReg(pParse, regData);
123418 if( emptyDestTest ){
123419 sqlite3AutoincrementEnd(pParse);
123420 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
123421 sqlite3VdbeJumpHere(v, emptyDestTest);
123422 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
123423 return 0;
123424 }else{
123425 return 1;
123426 }
123427}
123428#endif /* SQLITE_OMIT_XFER_OPT */
123429
123430/************** End of insert.c **********************************************/
123431/************** Begin file legacy.c ******************************************/
123432/*
123433** 2001 September 15
123434**
123435** The author disclaims copyright to this source code. In place of
123436** a legal notice, here is a blessing:
123437**
123438** May you do good and not evil.
123439** May you find forgiveness for yourself and forgive others.
123440** May you share freely, never taking more than you give.
123441**
123442*************************************************************************
123443** Main file for the SQLite library. The routines in this file
123444** implement the programmer interface to the library. Routines in
123445** other files are for internal use by SQLite and should not be
123446** accessed by users of the library.
123447*/
123448
123449/* #include "sqliteInt.h" */
123450
123451/*
123452** Execute SQL code. Return one of the SQLITE_ success/failure
123453** codes. Also write an error message into memory obtained from
123454** malloc() and make *pzErrMsg point to that message.
123455**
123456** If the SQL is a query, then for each row in the query result
123457** the xCallback() function is called. pArg becomes the first
123458** argument to xCallback(). If xCallback=NULL then no callback
123459** is invoked, even for queries.
123460*/
123461SQLITE_API int sqlite3_exec(
123462 sqlite3 *db, /* The database on which the SQL executes */
123463 const char *zSql, /* The SQL to be executed */
123464 sqlite3_callback xCallback, /* Invoke this callback routine */
123465 void *pArg, /* First argument to xCallback() */
123466 char **pzErrMsg /* Write error messages here */
123467){
123468 int rc = SQLITE_OK; /* Return code */
123469 const char *zLeftover; /* Tail of unprocessed SQL */
123470 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
123471 char **azCols = 0; /* Names of result columns */
123472 int callbackIsInit; /* True if callback data is initialized */
123473
123474 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
123475 if( zSql==0 ) zSql = "";
123476
123477 sqlite3_mutex_enter(db->mutex);
123478 sqlite3Error(db, SQLITE_OK);
123479 while( rc==SQLITE_OK && zSql[0] ){
123480 int nCol = 0;
123481 char **azVals = 0;
123482
123483 pStmt = 0;
123484 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
123485 assert( rc==SQLITE_OK || pStmt==0 );
123486 if( rc!=SQLITE_OK ){
123487 continue;
123488 }
123489 if( !pStmt ){
123490 /* this happens for a comment or white-space */
123491 zSql = zLeftover;
123492 continue;
123493 }
123494 callbackIsInit = 0;
123495
123496 while( 1 ){
123497 int i;
123498 rc = sqlite3_step(pStmt);
123499
123500 /* Invoke the callback function if required */
123501 if( xCallback && (SQLITE_ROW==rc ||
123502 (SQLITE_DONE==rc && !callbackIsInit
123503 && db->flags&SQLITE_NullCallback)) ){
123504 if( !callbackIsInit ){
123505 nCol = sqlite3_column_count(pStmt);
123506 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
123507 if( azCols==0 ){
123508 goto exec_out;
123509 }
123510 for(i=0; i<nCol; i++){
123511 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
123512 /* sqlite3VdbeSetColName() installs column names as UTF8
123513 ** strings so there is no way for sqlite3_column_name() to fail. */
123514 assert( azCols[i]!=0 );
123515 }
123516 callbackIsInit = 1;
123517 }
123518 if( rc==SQLITE_ROW ){
123519 azVals = &azCols[nCol];
123520 for(i=0; i<nCol; i++){
123521 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
123522 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
123523 sqlite3OomFault(db);
123524 goto exec_out;
123525 }
123526 }
123527 azVals[i] = 0;
123528 }
123529 if( xCallback(pArg, nCol, azVals, azCols) ){
123530 /* EVIDENCE-OF: R-38229-40159 If the callback function to
123531 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
123532 ** return SQLITE_ABORT. */
123533 rc = SQLITE_ABORT;
123534 sqlite3VdbeFinalize((Vdbe *)pStmt);
123535 pStmt = 0;
123536 sqlite3Error(db, SQLITE_ABORT);
123537 goto exec_out;
123538 }
123539 }
123540
123541 if( rc!=SQLITE_ROW ){
123542 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
123543 pStmt = 0;
123544 zSql = zLeftover;
123545 while( sqlite3Isspace(zSql[0]) ) zSql++;
123546 break;
123547 }
123548 }
123549
123550 sqlite3DbFree(db, azCols);
123551 azCols = 0;
123552 }
123553
123554exec_out:
123555 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
123556 sqlite3DbFree(db, azCols);
123557
123558 rc = sqlite3ApiExit(db, rc);
123559 if( rc!=SQLITE_OK && pzErrMsg ){
123560 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
123561 if( *pzErrMsg==0 ){
123562 rc = SQLITE_NOMEM_BKPT;
123563 sqlite3Error(db, SQLITE_NOMEM);
123564 }
123565 }else if( pzErrMsg ){
123566 *pzErrMsg = 0;
123567 }
123568
123569 assert( (rc&db->errMask)==rc );
123570 sqlite3_mutex_leave(db->mutex);
123571 return rc;
123572}
123573
123574/************** End of legacy.c **********************************************/
123575/************** Begin file loadext.c *****************************************/
123576/*
123577** 2006 June 7
123578**
123579** The author disclaims copyright to this source code. In place of
123580** a legal notice, here is a blessing:
123581**
123582** May you do good and not evil.
123583** May you find forgiveness for yourself and forgive others.
123584** May you share freely, never taking more than you give.
123585**
123586*************************************************************************
123587** This file contains code used to dynamically load extensions into
123588** the SQLite library.
123589*/
123590
123591#ifndef SQLITE_CORE
123592 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
123593#endif
123594/************** Include sqlite3ext.h in the middle of loadext.c **************/
123595/************** Begin file sqlite3ext.h **************************************/
123596/*
123597** 2006 June 7
123598**
123599** The author disclaims copyright to this source code. In place of
123600** a legal notice, here is a blessing:
123601**
123602** May you do good and not evil.
123603** May you find forgiveness for yourself and forgive others.
123604** May you share freely, never taking more than you give.
123605**
123606*************************************************************************
123607** This header file defines the SQLite interface for use by
123608** shared libraries that want to be imported as extensions into
123609** an SQLite instance. Shared libraries that intend to be loaded
123610** as extensions by SQLite should #include this file instead of
123611** sqlite3.h.
123612*/
123613#ifndef SQLITE3EXT_H
123614#define SQLITE3EXT_H
123615/* #include "sqlite3.h" */
123616
123617/*
123618** The following structure holds pointers to all of the SQLite API
123619** routines.
123620**
123621** WARNING: In order to maintain backwards compatibility, add new
123622** interfaces to the end of this structure only. If you insert new
123623** interfaces in the middle of this structure, then older different
123624** versions of SQLite will not be able to load each other's shared
123625** libraries!
123626*/
123627struct sqlite3_api_routines {
123628 void * (*aggregate_context)(sqlite3_context*,int nBytes);
123629 int (*aggregate_count)(sqlite3_context*);
123630 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
123631 int (*bind_double)(sqlite3_stmt*,int,double);
123632 int (*bind_int)(sqlite3_stmt*,int,int);
123633 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
123634 int (*bind_null)(sqlite3_stmt*,int);
123635 int (*bind_parameter_count)(sqlite3_stmt*);
123636 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
123637 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
123638 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
123639 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
123640 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
123641 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
123642 int (*busy_timeout)(sqlite3*,int ms);
123643 int (*changes)(sqlite3*);
123644 int (*close)(sqlite3*);
123645 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
123646 int eTextRep,const char*));
123647 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
123648 int eTextRep,const void*));
123649 const void * (*column_blob)(sqlite3_stmt*,int iCol);
123650 int (*column_bytes)(sqlite3_stmt*,int iCol);
123651 int (*column_bytes16)(sqlite3_stmt*,int iCol);
123652 int (*column_count)(sqlite3_stmt*pStmt);
123653 const char * (*column_database_name)(sqlite3_stmt*,int);
123654 const void * (*column_database_name16)(sqlite3_stmt*,int);
123655 const char * (*column_decltype)(sqlite3_stmt*,int i);
123656 const void * (*column_decltype16)(sqlite3_stmt*,int);
123657 double (*column_double)(sqlite3_stmt*,int iCol);
123658 int (*column_int)(sqlite3_stmt*,int iCol);
123659 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
123660 const char * (*column_name)(sqlite3_stmt*,int);
123661 const void * (*column_name16)(sqlite3_stmt*,int);
123662 const char * (*column_origin_name)(sqlite3_stmt*,int);
123663 const void * (*column_origin_name16)(sqlite3_stmt*,int);
123664 const char * (*column_table_name)(sqlite3_stmt*,int);
123665 const void * (*column_table_name16)(sqlite3_stmt*,int);
123666 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
123667 const void * (*column_text16)(sqlite3_stmt*,int iCol);
123668 int (*column_type)(sqlite3_stmt*,int iCol);
123669 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
123670 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
123671 int (*complete)(const char*sql);
123672 int (*complete16)(const void*sql);
123673 int (*create_collation)(sqlite3*,const char*,int,void*,
123674 int(*)(void*,int,const void*,int,const void*));
123675 int (*create_collation16)(sqlite3*,const void*,int,void*,
123676 int(*)(void*,int,const void*,int,const void*));
123677 int (*create_function)(sqlite3*,const char*,int,int,void*,
123678 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123679 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123680 void (*xFinal)(sqlite3_context*));
123681 int (*create_function16)(sqlite3*,const void*,int,int,void*,
123682 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123683 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123684 void (*xFinal)(sqlite3_context*));
123685 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
123686 int (*data_count)(sqlite3_stmt*pStmt);
123687 sqlite3 * (*db_handle)(sqlite3_stmt*);
123688 int (*declare_vtab)(sqlite3*,const char*);
123689 int (*enable_shared_cache)(int);
123690 int (*errcode)(sqlite3*db);
123691 const char * (*errmsg)(sqlite3*);
123692 const void * (*errmsg16)(sqlite3*);
123693 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
123694 int (*expired)(sqlite3_stmt*);
123695 int (*finalize)(sqlite3_stmt*pStmt);
123696 void (*free)(void*);
123697 void (*free_table)(char**result);
123698 int (*get_autocommit)(sqlite3*);
123699 void * (*get_auxdata)(sqlite3_context*,int);
123700 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
123701 int (*global_recover)(void);
123702 void (*interruptx)(sqlite3*);
123703 sqlite_int64 (*last_insert_rowid)(sqlite3*);
123704 const char * (*libversion)(void);
123705 int (*libversion_number)(void);
123706 void *(*malloc)(int);
123707 char * (*mprintf)(const char*,...);
123708 int (*open)(const char*,sqlite3**);
123709 int (*open16)(const void*,sqlite3**);
123710 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
123711 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
123712 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
123713 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
123714 void *(*realloc)(void*,int);
123715 int (*reset)(sqlite3_stmt*pStmt);
123716 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
123717 void (*result_double)(sqlite3_context*,double);
123718 void (*result_error)(sqlite3_context*,const char*,int);
123719 void (*result_error16)(sqlite3_context*,const void*,int);
123720 void (*result_int)(sqlite3_context*,int);
123721 void (*result_int64)(sqlite3_context*,sqlite_int64);
123722 void (*result_null)(sqlite3_context*);
123723 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
123724 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
123725 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
123726 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
123727 void (*result_value)(sqlite3_context*,sqlite3_value*);
123728 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
123729 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
123730 const char*,const char*),void*);
123731 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
123732 char * (*xsnprintf)(int,char*,const char*,...);
123733 int (*step)(sqlite3_stmt*);
123734 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
123735 char const**,char const**,int*,int*,int*);
123736 void (*thread_cleanup)(void);
123737 int (*total_changes)(sqlite3*);
123738 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
123739 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
123740 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
123741 sqlite_int64),void*);
123742 void * (*user_data)(sqlite3_context*);
123743 const void * (*value_blob)(sqlite3_value*);
123744 int (*value_bytes)(sqlite3_value*);
123745 int (*value_bytes16)(sqlite3_value*);
123746 double (*value_double)(sqlite3_value*);
123747 int (*value_int)(sqlite3_value*);
123748 sqlite_int64 (*value_int64)(sqlite3_value*);
123749 int (*value_numeric_type)(sqlite3_value*);
123750 const unsigned char * (*value_text)(sqlite3_value*);
123751 const void * (*value_text16)(sqlite3_value*);
123752 const void * (*value_text16be)(sqlite3_value*);
123753 const void * (*value_text16le)(sqlite3_value*);
123754 int (*value_type)(sqlite3_value*);
123755 char *(*vmprintf)(const char*,va_list);
123756 /* Added ??? */
123757 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
123758 /* Added by 3.3.13 */
123759 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
123760 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
123761 int (*clear_bindings)(sqlite3_stmt*);
123762 /* Added by 3.4.1 */
123763 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
123764 void (*xDestroy)(void *));
123765 /* Added by 3.5.0 */
123766 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
123767 int (*blob_bytes)(sqlite3_blob*);
123768 int (*blob_close)(sqlite3_blob*);
123769 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
123770 int,sqlite3_blob**);
123771 int (*blob_read)(sqlite3_blob*,void*,int,int);
123772 int (*blob_write)(sqlite3_blob*,const void*,int,int);
123773 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
123774 int(*)(void*,int,const void*,int,const void*),
123775 void(*)(void*));
123776 int (*file_control)(sqlite3*,const char*,int,void*);
123777 sqlite3_int64 (*memory_highwater)(int);
123778 sqlite3_int64 (*memory_used)(void);
123779 sqlite3_mutex *(*mutex_alloc)(int);
123780 void (*mutex_enter)(sqlite3_mutex*);
123781 void (*mutex_free)(sqlite3_mutex*);
123782 void (*mutex_leave)(sqlite3_mutex*);
123783 int (*mutex_try)(sqlite3_mutex*);
123784 int (*open_v2)(const char*,sqlite3**,int,const char*);
123785 int (*release_memory)(int);
123786 void (*result_error_nomem)(sqlite3_context*);
123787 void (*result_error_toobig)(sqlite3_context*);
123788 int (*sleep)(int);
123789 void (*soft_heap_limit)(int);
123790 sqlite3_vfs *(*vfs_find)(const char*);
123791 int (*vfs_register)(sqlite3_vfs*,int);
123792 int (*vfs_unregister)(sqlite3_vfs*);
123793 int (*xthreadsafe)(void);
123794 void (*result_zeroblob)(sqlite3_context*,int);
123795 void (*result_error_code)(sqlite3_context*,int);
123796 int (*test_control)(int, ...);
123797 void (*randomness)(int,void*);
123798 sqlite3 *(*context_db_handle)(sqlite3_context*);
123799 int (*extended_result_codes)(sqlite3*,int);
123800 int (*limit)(sqlite3*,int,int);
123801 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
123802 const char *(*sql)(sqlite3_stmt*);
123803 int (*status)(int,int*,int*,int);
123804 int (*backup_finish)(sqlite3_backup*);
123805 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
123806 int (*backup_pagecount)(sqlite3_backup*);
123807 int (*backup_remaining)(sqlite3_backup*);
123808 int (*backup_step)(sqlite3_backup*,int);
123809 const char *(*compileoption_get)(int);
123810 int (*compileoption_used)(const char*);
123811 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
123812 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
123813 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123814 void (*xFinal)(sqlite3_context*),
123815 void(*xDestroy)(void*));
123816 int (*db_config)(sqlite3*,int,...);
123817 sqlite3_mutex *(*db_mutex)(sqlite3*);
123818 int (*db_status)(sqlite3*,int,int*,int*,int);
123819 int (*extended_errcode)(sqlite3*);
123820 void (*log)(int,const char*,...);
123821 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
123822 const char *(*sourceid)(void);
123823 int (*stmt_status)(sqlite3_stmt*,int,int);
123824 int (*strnicmp)(const char*,const char*,int);
123825 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
123826 int (*wal_autocheckpoint)(sqlite3*,int);
123827 int (*wal_checkpoint)(sqlite3*,const char*);
123828 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
123829 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
123830 int (*vtab_config)(sqlite3*,int op,...);
123831 int (*vtab_on_conflict)(sqlite3*);
123832 /* Version 3.7.16 and later */
123833 int (*close_v2)(sqlite3*);
123834 const char *(*db_filename)(sqlite3*,const char*);
123835 int (*db_readonly)(sqlite3*,const char*);
123836 int (*db_release_memory)(sqlite3*);
123837 const char *(*errstr)(int);
123838 int (*stmt_busy)(sqlite3_stmt*);
123839 int (*stmt_readonly)(sqlite3_stmt*);
123840 int (*stricmp)(const char*,const char*);
123841 int (*uri_boolean)(const char*,const char*,int);
123842 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
123843 const char *(*uri_parameter)(const char*,const char*);
123844 char *(*xvsnprintf)(int,char*,const char*,va_list);
123845 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
123846 /* Version 3.8.7 and later */
123847 int (*auto_extension)(void(*)(void));
123848 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
123849 void(*)(void*));
123850 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
123851 void(*)(void*),unsigned char);
123852 int (*cancel_auto_extension)(void(*)(void));
123853 int (*load_extension)(sqlite3*,const char*,const char*,char**);
123854 void *(*malloc64)(sqlite3_uint64);
123855 sqlite3_uint64 (*msize)(void*);
123856 void *(*realloc64)(void*,sqlite3_uint64);
123857 void (*reset_auto_extension)(void);
123858 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
123859 void(*)(void*));
123860 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
123861 void(*)(void*), unsigned char);
123862 int (*strglob)(const char*,const char*);
123863 /* Version 3.8.11 and later */
123864 sqlite3_value *(*value_dup)(const sqlite3_value*);
123865 void (*value_free)(sqlite3_value*);
123866 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
123867 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
123868 /* Version 3.9.0 and later */
123869 unsigned int (*value_subtype)(sqlite3_value*);
123870 void (*result_subtype)(sqlite3_context*,unsigned int);
123871 /* Version 3.10.0 and later */
123872 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
123873 int (*strlike)(const char*,const char*,unsigned int);
123874 int (*db_cacheflush)(sqlite3*);
123875 /* Version 3.12.0 and later */
123876 int (*system_errno)(sqlite3*);
123877 /* Version 3.14.0 and later */
123878 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
123879 char *(*expanded_sql)(sqlite3_stmt*);
123880 /* Version 3.18.0 and later */
123881 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
123882 /* Version 3.20.0 and later */
123883 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
123884 sqlite3_stmt**,const char**);
123885 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
123886 sqlite3_stmt**,const void**);
123887 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
123888 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
123889 void *(*value_pointer)(sqlite3_value*,const char*);
123890 int (*vtab_nochange)(sqlite3_context*);
123891 int (*value_nochange)(sqlite3_value*);
123892 const char *(*vtab_collation)(sqlite3_index_info*,int);
123893 /* Version 3.24.0 and later */
123894 int (*keyword_count)(void);
123895 int (*keyword_name)(int,const char**,int*);
123896 int (*keyword_check)(const char*,int);
123897 sqlite3_str *(*str_new)(sqlite3*);
123898 char *(*str_finish)(sqlite3_str*);
123899 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
123900 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
123901 void (*str_append)(sqlite3_str*, const char *zIn, int N);
123902 void (*str_appendall)(sqlite3_str*, const char *zIn);
123903 void (*str_appendchar)(sqlite3_str*, int N, char C);
123904 void (*str_reset)(sqlite3_str*);
123905 int (*str_errcode)(sqlite3_str*);
123906 int (*str_length)(sqlite3_str*);
123907 char *(*str_value)(sqlite3_str*);
123908 /* Version 3.25.0 and later */
123909 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
123910 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
123911 void (*xFinal)(sqlite3_context*),
123912 void (*xValue)(sqlite3_context*),
123913 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
123914 void(*xDestroy)(void*));
123915 /* Version 3.26.0 and later */
123916 const char *(*normalized_sql)(sqlite3_stmt*);
123917 /* Version 3.28.0 and later */
123918 int (*stmt_isexplain)(sqlite3_stmt*);
123919 int (*value_frombind)(sqlite3_value*);
123920 /* Version 3.30.0 and later */
123921 int (*drop_modules)(sqlite3*,const char**);
123922 /* Version 3.31.0 and later */
123923 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
123924 const char *(*uri_key)(const char*,int);
123925 const char *(*filename_database)(const char*);
123926 const char *(*filename_journal)(const char*);
123927 const char *(*filename_wal)(const char*);
123928 /* Version 3.32.0 and later */
123929 char *(*create_filename)(const char*,const char*,const char*,
123930 int,const char**);
123931 void (*free_filename)(char*);
123932 sqlite3_file *(*database_file_object)(const char*);
123933};
123934
123935/*
123936** This is the function signature used for all extension entry points. It
123937** is also defined in the file "loadext.c".
123938*/
123939typedef int (*sqlite3_loadext_entry)(
123940 sqlite3 *db, /* Handle to the database. */
123941 char **pzErrMsg, /* Used to set error string on failure. */
123942 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
123943);
123944
123945/*
123946** The following macros redefine the API routines so that they are
123947** redirected through the global sqlite3_api structure.
123948**
123949** This header file is also used by the loadext.c source file
123950** (part of the main SQLite library - not an extension) so that
123951** it can get access to the sqlite3_api_routines structure
123952** definition. But the main library does not want to redefine
123953** the API. So the redefinition macros are only valid if the
123954** SQLITE_CORE macros is undefined.
123955*/
123956#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
123957#define sqlite3_aggregate_context sqlite3_api->aggregate_context
123958#ifndef SQLITE_OMIT_DEPRECATED
123959#define sqlite3_aggregate_count sqlite3_api->aggregate_count
123960#endif
123961#define sqlite3_bind_blob sqlite3_api->bind_blob
123962#define sqlite3_bind_double sqlite3_api->bind_double
123963#define sqlite3_bind_int sqlite3_api->bind_int
123964#define sqlite3_bind_int64 sqlite3_api->bind_int64
123965#define sqlite3_bind_null sqlite3_api->bind_null
123966#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
123967#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
123968#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
123969#define sqlite3_bind_text sqlite3_api->bind_text
123970#define sqlite3_bind_text16 sqlite3_api->bind_text16
123971#define sqlite3_bind_value sqlite3_api->bind_value
123972#define sqlite3_busy_handler sqlite3_api->busy_handler
123973#define sqlite3_busy_timeout sqlite3_api->busy_timeout
123974#define sqlite3_changes sqlite3_api->changes
123975#define sqlite3_close sqlite3_api->close
123976#define sqlite3_collation_needed sqlite3_api->collation_needed
123977#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
123978#define sqlite3_column_blob sqlite3_api->column_blob
123979#define sqlite3_column_bytes sqlite3_api->column_bytes
123980#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
123981#define sqlite3_column_count sqlite3_api->column_count
123982#define sqlite3_column_database_name sqlite3_api->column_database_name
123983#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
123984#define sqlite3_column_decltype sqlite3_api->column_decltype
123985#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
123986#define sqlite3_column_double sqlite3_api->column_double
123987#define sqlite3_column_int sqlite3_api->column_int
123988#define sqlite3_column_int64 sqlite3_api->column_int64
123989#define sqlite3_column_name sqlite3_api->column_name
123990#define sqlite3_column_name16 sqlite3_api->column_name16
123991#define sqlite3_column_origin_name sqlite3_api->column_origin_name
123992#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
123993#define sqlite3_column_table_name sqlite3_api->column_table_name
123994#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
123995#define sqlite3_column_text sqlite3_api->column_text
123996#define sqlite3_column_text16 sqlite3_api->column_text16
123997#define sqlite3_column_type sqlite3_api->column_type
123998#define sqlite3_column_value sqlite3_api->column_value
123999#define sqlite3_commit_hook sqlite3_api->commit_hook
124000#define sqlite3_complete sqlite3_api->complete
124001#define sqlite3_complete16 sqlite3_api->complete16
124002#define sqlite3_create_collation sqlite3_api->create_collation
124003#define sqlite3_create_collation16 sqlite3_api->create_collation16
124004#define sqlite3_create_function sqlite3_api->create_function
124005#define sqlite3_create_function16 sqlite3_api->create_function16
124006#define sqlite3_create_module sqlite3_api->create_module
124007#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
124008#define sqlite3_data_count sqlite3_api->data_count
124009#define sqlite3_db_handle sqlite3_api->db_handle
124010#define sqlite3_declare_vtab sqlite3_api->declare_vtab
124011#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
124012#define sqlite3_errcode sqlite3_api->errcode
124013#define sqlite3_errmsg sqlite3_api->errmsg
124014#define sqlite3_errmsg16 sqlite3_api->errmsg16
124015#define sqlite3_exec sqlite3_api->exec
124016#ifndef SQLITE_OMIT_DEPRECATED
124017#define sqlite3_expired sqlite3_api->expired
124018#endif
124019#define sqlite3_finalize sqlite3_api->finalize
124020#define sqlite3_free sqlite3_api->free
124021#define sqlite3_free_table sqlite3_api->free_table
124022#define sqlite3_get_autocommit sqlite3_api->get_autocommit
124023#define sqlite3_get_auxdata sqlite3_api->get_auxdata
124024#define sqlite3_get_table sqlite3_api->get_table
124025#ifndef SQLITE_OMIT_DEPRECATED
124026#define sqlite3_global_recover sqlite3_api->global_recover
124027#endif
124028#define sqlite3_interrupt sqlite3_api->interruptx
124029#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
124030#define sqlite3_libversion sqlite3_api->libversion
124031#define sqlite3_libversion_number sqlite3_api->libversion_number
124032#define sqlite3_malloc sqlite3_api->malloc
124033#define sqlite3_mprintf sqlite3_api->mprintf
124034#define sqlite3_open sqlite3_api->open
124035#define sqlite3_open16 sqlite3_api->open16
124036#define sqlite3_prepare sqlite3_api->prepare
124037#define sqlite3_prepare16 sqlite3_api->prepare16
124038#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
124039#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
124040#define sqlite3_profile sqlite3_api->profile
124041#define sqlite3_progress_handler sqlite3_api->progress_handler
124042#define sqlite3_realloc sqlite3_api->realloc
124043#define sqlite3_reset sqlite3_api->reset
124044#define sqlite3_result_blob sqlite3_api->result_blob
124045#define sqlite3_result_double sqlite3_api->result_double
124046#define sqlite3_result_error sqlite3_api->result_error
124047#define sqlite3_result_error16 sqlite3_api->result_error16
124048#define sqlite3_result_int sqlite3_api->result_int
124049#define sqlite3_result_int64 sqlite3_api->result_int64
124050#define sqlite3_result_null sqlite3_api->result_null
124051#define sqlite3_result_text sqlite3_api->result_text
124052#define sqlite3_result_text16 sqlite3_api->result_text16
124053#define sqlite3_result_text16be sqlite3_api->result_text16be
124054#define sqlite3_result_text16le sqlite3_api->result_text16le
124055#define sqlite3_result_value sqlite3_api->result_value
124056#define sqlite3_rollback_hook sqlite3_api->rollback_hook
124057#define sqlite3_set_authorizer sqlite3_api->set_authorizer
124058#define sqlite3_set_auxdata sqlite3_api->set_auxdata
124059#define sqlite3_snprintf sqlite3_api->xsnprintf
124060#define sqlite3_step sqlite3_api->step
124061#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
124062#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
124063#define sqlite3_total_changes sqlite3_api->total_changes
124064#define sqlite3_trace sqlite3_api->trace
124065#ifndef SQLITE_OMIT_DEPRECATED
124066#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
124067#endif
124068#define sqlite3_update_hook sqlite3_api->update_hook
124069#define sqlite3_user_data sqlite3_api->user_data
124070#define sqlite3_value_blob sqlite3_api->value_blob
124071#define sqlite3_value_bytes sqlite3_api->value_bytes
124072#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
124073#define sqlite3_value_double sqlite3_api->value_double
124074#define sqlite3_value_int sqlite3_api->value_int
124075#define sqlite3_value_int64 sqlite3_api->value_int64
124076#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
124077#define sqlite3_value_text sqlite3_api->value_text
124078#define sqlite3_value_text16 sqlite3_api->value_text16
124079#define sqlite3_value_text16be sqlite3_api->value_text16be
124080#define sqlite3_value_text16le sqlite3_api->value_text16le
124081#define sqlite3_value_type sqlite3_api->value_type
124082#define sqlite3_vmprintf sqlite3_api->vmprintf
124083#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
124084#define sqlite3_overload_function sqlite3_api->overload_function
124085#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
124086#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
124087#define sqlite3_clear_bindings sqlite3_api->clear_bindings
124088#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
124089#define sqlite3_blob_bytes sqlite3_api->blob_bytes
124090#define sqlite3_blob_close sqlite3_api->blob_close
124091#define sqlite3_blob_open sqlite3_api->blob_open
124092#define sqlite3_blob_read sqlite3_api->blob_read
124093#define sqlite3_blob_write sqlite3_api->blob_write
124094#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
124095#define sqlite3_file_control sqlite3_api->file_control
124096#define sqlite3_memory_highwater sqlite3_api->memory_highwater
124097#define sqlite3_memory_used sqlite3_api->memory_used
124098#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
124099#define sqlite3_mutex_enter sqlite3_api->mutex_enter
124100#define sqlite3_mutex_free sqlite3_api->mutex_free
124101#define sqlite3_mutex_leave sqlite3_api->mutex_leave
124102#define sqlite3_mutex_try sqlite3_api->mutex_try
124103#define sqlite3_open_v2 sqlite3_api->open_v2
124104#define sqlite3_release_memory sqlite3_api->release_memory
124105#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
124106#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
124107#define sqlite3_sleep sqlite3_api->sleep
124108#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
124109#define sqlite3_vfs_find sqlite3_api->vfs_find
124110#define sqlite3_vfs_register sqlite3_api->vfs_register
124111#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
124112#define sqlite3_threadsafe sqlite3_api->xthreadsafe
124113#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
124114#define sqlite3_result_error_code sqlite3_api->result_error_code
124115#define sqlite3_test_control sqlite3_api->test_control
124116#define sqlite3_randomness sqlite3_api->randomness
124117#define sqlite3_context_db_handle sqlite3_api->context_db_handle
124118#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
124119#define sqlite3_limit sqlite3_api->limit
124120#define sqlite3_next_stmt sqlite3_api->next_stmt
124121#define sqlite3_sql sqlite3_api->sql
124122#define sqlite3_status sqlite3_api->status
124123#define sqlite3_backup_finish sqlite3_api->backup_finish
124124#define sqlite3_backup_init sqlite3_api->backup_init
124125#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
124126#define sqlite3_backup_remaining sqlite3_api->backup_remaining
124127#define sqlite3_backup_step sqlite3_api->backup_step
124128#define sqlite3_compileoption_get sqlite3_api->compileoption_get
124129#define sqlite3_compileoption_used sqlite3_api->compileoption_used
124130#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
124131#define sqlite3_db_config sqlite3_api->db_config
124132#define sqlite3_db_mutex sqlite3_api->db_mutex
124133#define sqlite3_db_status sqlite3_api->db_status
124134#define sqlite3_extended_errcode sqlite3_api->extended_errcode
124135#define sqlite3_log sqlite3_api->log
124136#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
124137#define sqlite3_sourceid sqlite3_api->sourceid
124138#define sqlite3_stmt_status sqlite3_api->stmt_status
124139#define sqlite3_strnicmp sqlite3_api->strnicmp
124140#define sqlite3_unlock_notify sqlite3_api->unlock_notify
124141#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
124142#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
124143#define sqlite3_wal_hook sqlite3_api->wal_hook
124144#define sqlite3_blob_reopen sqlite3_api->blob_reopen
124145#define sqlite3_vtab_config sqlite3_api->vtab_config
124146#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
124147/* Version 3.7.16 and later */
124148#define sqlite3_close_v2 sqlite3_api->close_v2
124149#define sqlite3_db_filename sqlite3_api->db_filename
124150#define sqlite3_db_readonly sqlite3_api->db_readonly
124151#define sqlite3_db_release_memory sqlite3_api->db_release_memory
124152#define sqlite3_errstr sqlite3_api->errstr
124153#define sqlite3_stmt_busy sqlite3_api->stmt_busy
124154#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
124155#define sqlite3_stricmp sqlite3_api->stricmp
124156#define sqlite3_uri_boolean sqlite3_api->uri_boolean
124157#define sqlite3_uri_int64 sqlite3_api->uri_int64
124158#define sqlite3_uri_parameter sqlite3_api->uri_parameter
124159#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
124160#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
124161/* Version 3.8.7 and later */
124162#define sqlite3_auto_extension sqlite3_api->auto_extension
124163#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
124164#define sqlite3_bind_text64 sqlite3_api->bind_text64
124165#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
124166#define sqlite3_load_extension sqlite3_api->load_extension
124167#define sqlite3_malloc64 sqlite3_api->malloc64
124168#define sqlite3_msize sqlite3_api->msize
124169#define sqlite3_realloc64 sqlite3_api->realloc64
124170#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
124171#define sqlite3_result_blob64 sqlite3_api->result_blob64
124172#define sqlite3_result_text64 sqlite3_api->result_text64
124173#define sqlite3_strglob sqlite3_api->strglob
124174/* Version 3.8.11 and later */
124175#define sqlite3_value_dup sqlite3_api->value_dup
124176#define sqlite3_value_free sqlite3_api->value_free
124177#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
124178#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
124179/* Version 3.9.0 and later */
124180#define sqlite3_value_subtype sqlite3_api->value_subtype
124181#define sqlite3_result_subtype sqlite3_api->result_subtype
124182/* Version 3.10.0 and later */
124183#define sqlite3_status64 sqlite3_api->status64
124184#define sqlite3_strlike sqlite3_api->strlike
124185#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
124186/* Version 3.12.0 and later */
124187#define sqlite3_system_errno sqlite3_api->system_errno
124188/* Version 3.14.0 and later */
124189#define sqlite3_trace_v2 sqlite3_api->trace_v2
124190#define sqlite3_expanded_sql sqlite3_api->expanded_sql
124191/* Version 3.18.0 and later */
124192#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
124193/* Version 3.20.0 and later */
124194#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
124195#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
124196#define sqlite3_bind_pointer sqlite3_api->bind_pointer
124197#define sqlite3_result_pointer sqlite3_api->result_pointer
124198#define sqlite3_value_pointer sqlite3_api->value_pointer
124199/* Version 3.22.0 and later */
124200#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
124201#define sqlite3_value_nochange sqlite3_api->value_nochange
124202#define sqlite3_vtab_collation sqlite3_api->vtab_collation
124203/* Version 3.24.0 and later */
124204#define sqlite3_keyword_count sqlite3_api->keyword_count
124205#define sqlite3_keyword_name sqlite3_api->keyword_name
124206#define sqlite3_keyword_check sqlite3_api->keyword_check
124207#define sqlite3_str_new sqlite3_api->str_new
124208#define sqlite3_str_finish sqlite3_api->str_finish
124209#define sqlite3_str_appendf sqlite3_api->str_appendf
124210#define sqlite3_str_vappendf sqlite3_api->str_vappendf
124211#define sqlite3_str_append sqlite3_api->str_append
124212#define sqlite3_str_appendall sqlite3_api->str_appendall
124213#define sqlite3_str_appendchar sqlite3_api->str_appendchar
124214#define sqlite3_str_reset sqlite3_api->str_reset
124215#define sqlite3_str_errcode sqlite3_api->str_errcode
124216#define sqlite3_str_length sqlite3_api->str_length
124217#define sqlite3_str_value sqlite3_api->str_value
124218/* Version 3.25.0 and later */
124219#define sqlite3_create_window_function sqlite3_api->create_window_function
124220/* Version 3.26.0 and later */
124221#define sqlite3_normalized_sql sqlite3_api->normalized_sql
124222/* Version 3.28.0 and later */
124223#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
124224#define sqlite3_value_frombind sqlite3_api->value_frombind
124225/* Version 3.30.0 and later */
124226#define sqlite3_drop_modules sqlite3_api->drop_modules
124227/* Version 3.31.0 and later */
124228#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
124229#define sqlite3_uri_key sqlite3_api->uri_key
124230#define sqlite3_filename_database sqlite3_api->filename_database
124231#define sqlite3_filename_journal sqlite3_api->filename_journal
124232#define sqlite3_filename_wal sqlite3_api->filename_wal
124233/* Version 3.32.0 and later */
124234#define sqlite3_create_filename sqlite3_api->create_filename
124235#define sqlite3_free_filename sqlite3_api->free_filename
124236#define sqlite3_database_file_object sqlite3_api->database_file_object
124237#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
124238
124239#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
124240 /* This case when the file really is being compiled as a loadable
124241 ** extension */
124242# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
124243# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
124244# define SQLITE_EXTENSION_INIT3 \
124245 extern const sqlite3_api_routines *sqlite3_api;
124246#else
124247 /* This case when the file is being statically linked into the
124248 ** application */
124249# define SQLITE_EXTENSION_INIT1 /*no-op*/
124250# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
124251# define SQLITE_EXTENSION_INIT3 /*no-op*/
124252#endif
124253
124254#endif /* SQLITE3EXT_H */
124255
124256/************** End of sqlite3ext.h ******************************************/
124257/************** Continuing where we left off in loadext.c ********************/
124258/* #include "sqliteInt.h" */
124259
124260#ifndef SQLITE_OMIT_LOAD_EXTENSION
124261/*
124262** Some API routines are omitted when various features are
124263** excluded from a build of SQLite. Substitute a NULL pointer
124264** for any missing APIs.
124265*/
124266#ifndef SQLITE_ENABLE_COLUMN_METADATA
124267# define sqlite3_column_database_name 0
124268# define sqlite3_column_database_name16 0
124269# define sqlite3_column_table_name 0
124270# define sqlite3_column_table_name16 0
124271# define sqlite3_column_origin_name 0
124272# define sqlite3_column_origin_name16 0
124273#endif
124274
124275#ifdef SQLITE_OMIT_AUTHORIZATION
124276# define sqlite3_set_authorizer 0
124277#endif
124278
124279#ifdef SQLITE_OMIT_UTF16
124280# define sqlite3_bind_text16 0
124281# define sqlite3_collation_needed16 0
124282# define sqlite3_column_decltype16 0
124283# define sqlite3_column_name16 0
124284# define sqlite3_column_text16 0
124285# define sqlite3_complete16 0
124286# define sqlite3_create_collation16 0
124287# define sqlite3_create_function16 0
124288# define sqlite3_errmsg16 0
124289# define sqlite3_open16 0
124290# define sqlite3_prepare16 0
124291# define sqlite3_prepare16_v2 0
124292# define sqlite3_prepare16_v3 0
124293# define sqlite3_result_error16 0
124294# define sqlite3_result_text16 0
124295# define sqlite3_result_text16be 0
124296# define sqlite3_result_text16le 0
124297# define sqlite3_value_text16 0
124298# define sqlite3_value_text16be 0
124299# define sqlite3_value_text16le 0
124300# define sqlite3_column_database_name16 0
124301# define sqlite3_column_table_name16 0
124302# define sqlite3_column_origin_name16 0
124303#endif
124304
124305#ifdef SQLITE_OMIT_COMPLETE
124306# define sqlite3_complete 0
124307# define sqlite3_complete16 0
124308#endif
124309
124310#ifdef SQLITE_OMIT_DECLTYPE
124311# define sqlite3_column_decltype16 0
124312# define sqlite3_column_decltype 0
124313#endif
124314
124315#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
124316# define sqlite3_progress_handler 0
124317#endif
124318
124319#ifdef SQLITE_OMIT_VIRTUALTABLE
124320# define sqlite3_create_module 0
124321# define sqlite3_create_module_v2 0
124322# define sqlite3_declare_vtab 0
124323# define sqlite3_vtab_config 0
124324# define sqlite3_vtab_on_conflict 0
124325# define sqlite3_vtab_collation 0
124326#endif
124327
124328#ifdef SQLITE_OMIT_SHARED_CACHE
124329# define sqlite3_enable_shared_cache 0
124330#endif
124331
124332#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
124333# define sqlite3_profile 0
124334# define sqlite3_trace 0
124335#endif
124336
124337#ifdef SQLITE_OMIT_GET_TABLE
124338# define sqlite3_free_table 0
124339# define sqlite3_get_table 0
124340#endif
124341
124342#ifdef SQLITE_OMIT_INCRBLOB
124343#define sqlite3_bind_zeroblob 0
124344#define sqlite3_blob_bytes 0
124345#define sqlite3_blob_close 0
124346#define sqlite3_blob_open 0
124347#define sqlite3_blob_read 0
124348#define sqlite3_blob_write 0
124349#define sqlite3_blob_reopen 0
124350#endif
124351
124352#if defined(SQLITE_OMIT_TRACE)
124353# define sqlite3_trace_v2 0
124354#endif
124355
124356/*
124357** The following structure contains pointers to all SQLite API routines.
124358** A pointer to this structure is passed into extensions when they are
124359** loaded so that the extension can make calls back into the SQLite
124360** library.
124361**
124362** When adding new APIs, add them to the bottom of this structure
124363** in order to preserve backwards compatibility.
124364**
124365** Extensions that use newer APIs should first call the
124366** sqlite3_libversion_number() to make sure that the API they
124367** intend to use is supported by the library. Extensions should
124368** also check to make sure that the pointer to the function is
124369** not NULL before calling it.
124370*/
124371static const sqlite3_api_routines sqlite3Apis = {
124372 sqlite3_aggregate_context,
124373#ifndef SQLITE_OMIT_DEPRECATED
124374 sqlite3_aggregate_count,
124375#else
124376 0,
124377#endif
124378 sqlite3_bind_blob,
124379 sqlite3_bind_double,
124380 sqlite3_bind_int,
124381 sqlite3_bind_int64,
124382 sqlite3_bind_null,
124383 sqlite3_bind_parameter_count,
124384 sqlite3_bind_parameter_index,
124385 sqlite3_bind_parameter_name,
124386 sqlite3_bind_text,
124387 sqlite3_bind_text16,
124388 sqlite3_bind_value,
124389 sqlite3_busy_handler,
124390 sqlite3_busy_timeout,
124391 sqlite3_changes,
124392 sqlite3_close,
124393 sqlite3_collation_needed,
124394 sqlite3_collation_needed16,
124395 sqlite3_column_blob,
124396 sqlite3_column_bytes,
124397 sqlite3_column_bytes16,
124398 sqlite3_column_count,
124399 sqlite3_column_database_name,
124400 sqlite3_column_database_name16,
124401 sqlite3_column_decltype,
124402 sqlite3_column_decltype16,
124403 sqlite3_column_double,
124404 sqlite3_column_int,
124405 sqlite3_column_int64,
124406 sqlite3_column_name,
124407 sqlite3_column_name16,
124408 sqlite3_column_origin_name,
124409 sqlite3_column_origin_name16,
124410 sqlite3_column_table_name,
124411 sqlite3_column_table_name16,
124412 sqlite3_column_text,
124413 sqlite3_column_text16,
124414 sqlite3_column_type,
124415 sqlite3_column_value,
124416 sqlite3_commit_hook,
124417 sqlite3_complete,
124418 sqlite3_complete16,
124419 sqlite3_create_collation,
124420 sqlite3_create_collation16,
124421 sqlite3_create_function,
124422 sqlite3_create_function16,
124423 sqlite3_create_module,
124424 sqlite3_data_count,
124425 sqlite3_db_handle,
124426 sqlite3_declare_vtab,
124427 sqlite3_enable_shared_cache,
124428 sqlite3_errcode,
124429 sqlite3_errmsg,
124430 sqlite3_errmsg16,
124431 sqlite3_exec,
124432#ifndef SQLITE_OMIT_DEPRECATED
124433 sqlite3_expired,
124434#else
124435 0,
124436#endif
124437 sqlite3_finalize,
124438 sqlite3_free,
124439 sqlite3_free_table,
124440 sqlite3_get_autocommit,
124441 sqlite3_get_auxdata,
124442 sqlite3_get_table,
124443 0, /* Was sqlite3_global_recover(), but that function is deprecated */
124444 sqlite3_interrupt,
124445 sqlite3_last_insert_rowid,
124446 sqlite3_libversion,
124447 sqlite3_libversion_number,
124448 sqlite3_malloc,
124449 sqlite3_mprintf,
124450 sqlite3_open,
124451 sqlite3_open16,
124452 sqlite3_prepare,
124453 sqlite3_prepare16,
124454 sqlite3_profile,
124455 sqlite3_progress_handler,
124456 sqlite3_realloc,
124457 sqlite3_reset,
124458 sqlite3_result_blob,
124459 sqlite3_result_double,
124460 sqlite3_result_error,
124461 sqlite3_result_error16,
124462 sqlite3_result_int,
124463 sqlite3_result_int64,
124464 sqlite3_result_null,
124465 sqlite3_result_text,
124466 sqlite3_result_text16,
124467 sqlite3_result_text16be,
124468 sqlite3_result_text16le,
124469 sqlite3_result_value,
124470 sqlite3_rollback_hook,
124471 sqlite3_set_authorizer,
124472 sqlite3_set_auxdata,
124473 sqlite3_snprintf,
124474 sqlite3_step,
124475 sqlite3_table_column_metadata,
124476#ifndef SQLITE_OMIT_DEPRECATED
124477 sqlite3_thread_cleanup,
124478#else
124479 0,
124480#endif
124481 sqlite3_total_changes,
124482 sqlite3_trace,
124483#ifndef SQLITE_OMIT_DEPRECATED
124484 sqlite3_transfer_bindings,
124485#else
124486 0,
124487#endif
124488 sqlite3_update_hook,
124489 sqlite3_user_data,
124490 sqlite3_value_blob,
124491 sqlite3_value_bytes,
124492 sqlite3_value_bytes16,
124493 sqlite3_value_double,
124494 sqlite3_value_int,
124495 sqlite3_value_int64,
124496 sqlite3_value_numeric_type,
124497 sqlite3_value_text,
124498 sqlite3_value_text16,
124499 sqlite3_value_text16be,
124500 sqlite3_value_text16le,
124501 sqlite3_value_type,
124502 sqlite3_vmprintf,
124503 /*
124504 ** The original API set ends here. All extensions can call any
124505 ** of the APIs above provided that the pointer is not NULL. But
124506 ** before calling APIs that follow, extension should check the
124507 ** sqlite3_libversion_number() to make sure they are dealing with
124508 ** a library that is new enough to support that API.
124509 *************************************************************************
124510 */
124511 sqlite3_overload_function,
124512
124513 /*
124514 ** Added after 3.3.13
124515 */
124516 sqlite3_prepare_v2,
124517 sqlite3_prepare16_v2,
124518 sqlite3_clear_bindings,
124519
124520 /*
124521 ** Added for 3.4.1
124522 */
124523 sqlite3_create_module_v2,
124524
124525 /*
124526 ** Added for 3.5.0
124527 */
124528 sqlite3_bind_zeroblob,
124529 sqlite3_blob_bytes,
124530 sqlite3_blob_close,
124531 sqlite3_blob_open,
124532 sqlite3_blob_read,
124533 sqlite3_blob_write,
124534 sqlite3_create_collation_v2,
124535 sqlite3_file_control,
124536 sqlite3_memory_highwater,
124537 sqlite3_memory_used,
124538#ifdef SQLITE_MUTEX_OMIT
124539 0,
124540 0,
124541 0,
124542 0,
124543 0,
124544#else
124545 sqlite3_mutex_alloc,
124546 sqlite3_mutex_enter,
124547 sqlite3_mutex_free,
124548 sqlite3_mutex_leave,
124549 sqlite3_mutex_try,
124550#endif
124551 sqlite3_open_v2,
124552 sqlite3_release_memory,
124553 sqlite3_result_error_nomem,
124554 sqlite3_result_error_toobig,
124555 sqlite3_sleep,
124556 sqlite3_soft_heap_limit,
124557 sqlite3_vfs_find,
124558 sqlite3_vfs_register,
124559 sqlite3_vfs_unregister,
124560
124561 /*
124562 ** Added for 3.5.8
124563 */
124564 sqlite3_threadsafe,
124565 sqlite3_result_zeroblob,
124566 sqlite3_result_error_code,
124567 sqlite3_test_control,
124568 sqlite3_randomness,
124569 sqlite3_context_db_handle,
124570
124571 /*
124572 ** Added for 3.6.0
124573 */
124574 sqlite3_extended_result_codes,
124575 sqlite3_limit,
124576 sqlite3_next_stmt,
124577 sqlite3_sql,
124578 sqlite3_status,
124579
124580 /*
124581 ** Added for 3.7.4
124582 */
124583 sqlite3_backup_finish,
124584 sqlite3_backup_init,
124585 sqlite3_backup_pagecount,
124586 sqlite3_backup_remaining,
124587 sqlite3_backup_step,
124588#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
124589 sqlite3_compileoption_get,
124590 sqlite3_compileoption_used,
124591#else
124592 0,
124593 0,
124594#endif
124595 sqlite3_create_function_v2,
124596 sqlite3_db_config,
124597 sqlite3_db_mutex,
124598 sqlite3_db_status,
124599 sqlite3_extended_errcode,
124600 sqlite3_log,
124601 sqlite3_soft_heap_limit64,
124602 sqlite3_sourceid,
124603 sqlite3_stmt_status,
124604 sqlite3_strnicmp,
124605#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
124606 sqlite3_unlock_notify,
124607#else
124608 0,
124609#endif
124610#ifndef SQLITE_OMIT_WAL
124611 sqlite3_wal_autocheckpoint,
124612 sqlite3_wal_checkpoint,
124613 sqlite3_wal_hook,
124614#else
124615 0,
124616 0,
124617 0,
124618#endif
124619 sqlite3_blob_reopen,
124620 sqlite3_vtab_config,
124621 sqlite3_vtab_on_conflict,
124622 sqlite3_close_v2,
124623 sqlite3_db_filename,
124624 sqlite3_db_readonly,
124625 sqlite3_db_release_memory,
124626 sqlite3_errstr,
124627 sqlite3_stmt_busy,
124628 sqlite3_stmt_readonly,
124629 sqlite3_stricmp,
124630 sqlite3_uri_boolean,
124631 sqlite3_uri_int64,
124632 sqlite3_uri_parameter,
124633 sqlite3_vsnprintf,
124634 sqlite3_wal_checkpoint_v2,
124635 /* Version 3.8.7 and later */
124636 sqlite3_auto_extension,
124637 sqlite3_bind_blob64,
124638 sqlite3_bind_text64,
124639 sqlite3_cancel_auto_extension,
124640 sqlite3_load_extension,
124641 sqlite3_malloc64,
124642 sqlite3_msize,
124643 sqlite3_realloc64,
124644 sqlite3_reset_auto_extension,
124645 sqlite3_result_blob64,
124646 sqlite3_result_text64,
124647 sqlite3_strglob,
124648 /* Version 3.8.11 and later */
124649 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
124650 sqlite3_value_free,
124651 sqlite3_result_zeroblob64,
124652 sqlite3_bind_zeroblob64,
124653 /* Version 3.9.0 and later */
124654 sqlite3_value_subtype,
124655 sqlite3_result_subtype,
124656 /* Version 3.10.0 and later */
124657 sqlite3_status64,
124658 sqlite3_strlike,
124659 sqlite3_db_cacheflush,
124660 /* Version 3.12.0 and later */
124661 sqlite3_system_errno,
124662 /* Version 3.14.0 and later */
124663 sqlite3_trace_v2,
124664 sqlite3_expanded_sql,
124665 /* Version 3.18.0 and later */
124666 sqlite3_set_last_insert_rowid,
124667 /* Version 3.20.0 and later */
124668 sqlite3_prepare_v3,
124669 sqlite3_prepare16_v3,
124670 sqlite3_bind_pointer,
124671 sqlite3_result_pointer,
124672 sqlite3_value_pointer,
124673 /* Version 3.22.0 and later */
124674 sqlite3_vtab_nochange,
124675 sqlite3_value_nochange,
124676 sqlite3_vtab_collation,
124677 /* Version 3.24.0 and later */
124678 sqlite3_keyword_count,
124679 sqlite3_keyword_name,
124680 sqlite3_keyword_check,
124681 sqlite3_str_new,
124682 sqlite3_str_finish,
124683 sqlite3_str_appendf,
124684 sqlite3_str_vappendf,
124685 sqlite3_str_append,
124686 sqlite3_str_appendall,
124687 sqlite3_str_appendchar,
124688 sqlite3_str_reset,
124689 sqlite3_str_errcode,
124690 sqlite3_str_length,
124691 sqlite3_str_value,
124692 /* Version 3.25.0 and later */
124693 sqlite3_create_window_function,
124694 /* Version 3.26.0 and later */
124695#ifdef SQLITE_ENABLE_NORMALIZE
124696 sqlite3_normalized_sql,
124697#else
124698 0,
124699#endif
124700 /* Version 3.28.0 and later */
124701 sqlite3_stmt_isexplain,
124702 sqlite3_value_frombind,
124703 /* Version 3.30.0 and later */
124704#ifndef SQLITE_OMIT_VIRTUALTABLE
124705 sqlite3_drop_modules,
124706#else
124707 0,
124708#endif
124709 /* Version 3.31.0 and later */
124710 sqlite3_hard_heap_limit64,
124711 sqlite3_uri_key,
124712 sqlite3_filename_database,
124713 sqlite3_filename_journal,
124714 sqlite3_filename_wal,
124715 /* Version 3.32.0 and later */
124716 sqlite3_create_filename,
124717 sqlite3_free_filename,
124718 sqlite3_database_file_object,
124719};
124720
124721/* True if x is the directory separator character
124722*/
124723#if SQLITE_OS_WIN
124724# define DirSep(X) ((X)=='/'||(X)=='\\')
124725#else
124726# define DirSep(X) ((X)=='/')
124727#endif
124728
124729/*
124730** Attempt to load an SQLite extension library contained in the file
124731** zFile. The entry point is zProc. zProc may be 0 in which case a
124732** default entry point name (sqlite3_extension_init) is used. Use
124733** of the default name is recommended.
124734**
124735** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
124736**
124737** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
124738** error message text. The calling function should free this memory
124739** by calling sqlite3DbFree(db, ).
124740*/
124741static int sqlite3LoadExtension(
124742 sqlite3 *db, /* Load the extension into this database connection */
124743 const char *zFile, /* Name of the shared library containing extension */
124744 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
124745 char **pzErrMsg /* Put error message here if not 0 */
124746){
124747 sqlite3_vfs *pVfs = db->pVfs;
124748 void *handle;
124749 sqlite3_loadext_entry xInit;
124750 char *zErrmsg = 0;
124751 const char *zEntry;
124752 char *zAltEntry = 0;
124753 void **aHandle;
124754 u64 nMsg = 300 + sqlite3Strlen30(zFile);
124755 int ii;
124756 int rc;
124757
124758 /* Shared library endings to try if zFile cannot be loaded as written */
124759 static const char *azEndings[] = {
124760#if SQLITE_OS_WIN
124761 "dll"
124762#elif defined(__APPLE__)
124763 "dylib"
124764#else
124765 "so"
124766#endif
124767 };
124768
124769
124770 if( pzErrMsg ) *pzErrMsg = 0;
124771
124772 /* Ticket #1863. To avoid a creating security problems for older
124773 ** applications that relink against newer versions of SQLite, the
124774 ** ability to run load_extension is turned off by default. One
124775 ** must call either sqlite3_enable_load_extension(db) or
124776 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
124777 ** to turn on extension loading.
124778 */
124779 if( (db->flags & SQLITE_LoadExtension)==0 ){
124780 if( pzErrMsg ){
124781 *pzErrMsg = sqlite3_mprintf("not authorized");
124782 }
124783 return SQLITE_ERROR;
124784 }
124785
124786 zEntry = zProc ? zProc : "sqlite3_extension_init";
124787
124788 handle = sqlite3OsDlOpen(pVfs, zFile);
124789#if SQLITE_OS_UNIX || SQLITE_OS_WIN
124790 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
124791 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
124792 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
124793 handle = sqlite3OsDlOpen(pVfs, zAltFile);
124794 sqlite3_free(zAltFile);
124795 }
124796#endif
124797 if( handle==0 ){
124798 if( pzErrMsg ){
124799 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
124800 if( zErrmsg ){
124801 sqlite3_snprintf(nMsg, zErrmsg,
124802 "unable to open shared library [%s]", zFile);
124803 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
124804 }
124805 }
124806 return SQLITE_ERROR;
124807 }
124808 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
124809
124810 /* If no entry point was specified and the default legacy
124811 ** entry point name "sqlite3_extension_init" was not found, then
124812 ** construct an entry point name "sqlite3_X_init" where the X is
124813 ** replaced by the lowercase value of every ASCII alphabetic
124814 ** character in the filename after the last "/" upto the first ".",
124815 ** and eliding the first three characters if they are "lib".
124816 ** Examples:
124817 **
124818 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
124819 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
124820 */
124821 if( xInit==0 && zProc==0 ){
124822 int iFile, iEntry, c;
124823 int ncFile = sqlite3Strlen30(zFile);
124824 zAltEntry = sqlite3_malloc64(ncFile+30);
124825 if( zAltEntry==0 ){
124826 sqlite3OsDlClose(pVfs, handle);
124827 return SQLITE_NOMEM_BKPT;
124828 }
124829 memcpy(zAltEntry, "sqlite3_", 8);
124830 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
124831 iFile++;
124832 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
124833 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
124834 if( sqlite3Isalpha(c) ){
124835 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
124836 }
124837 }
124838 memcpy(zAltEntry+iEntry, "_init", 6);
124839 zEntry = zAltEntry;
124840 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
124841 }
124842 if( xInit==0 ){
124843 if( pzErrMsg ){
124844 nMsg += sqlite3Strlen30(zEntry);
124845 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
124846 if( zErrmsg ){
124847 sqlite3_snprintf(nMsg, zErrmsg,
124848 "no entry point [%s] in shared library [%s]", zEntry, zFile);
124849 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
124850 }
124851 }
124852 sqlite3OsDlClose(pVfs, handle);
124853 sqlite3_free(zAltEntry);
124854 return SQLITE_ERROR;
124855 }
124856 sqlite3_free(zAltEntry);
124857 rc = xInit(db, &zErrmsg, &sqlite3Apis);
124858 if( rc ){
124859 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
124860 if( pzErrMsg ){
124861 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
124862 }
124863 sqlite3_free(zErrmsg);
124864 sqlite3OsDlClose(pVfs, handle);
124865 return SQLITE_ERROR;
124866 }
124867
124868 /* Append the new shared library handle to the db->aExtension array. */
124869 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
124870 if( aHandle==0 ){
124871 return SQLITE_NOMEM_BKPT;
124872 }
124873 if( db->nExtension>0 ){
124874 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
124875 }
124876 sqlite3DbFree(db, db->aExtension);
124877 db->aExtension = aHandle;
124878
124879 db->aExtension[db->nExtension++] = handle;
124880 return SQLITE_OK;
124881}
124882SQLITE_API int sqlite3_load_extension(
124883 sqlite3 *db, /* Load the extension into this database connection */
124884 const char *zFile, /* Name of the shared library containing extension */
124885 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
124886 char **pzErrMsg /* Put error message here if not 0 */
124887){
124888 int rc;
124889 sqlite3_mutex_enter(db->mutex);
124890 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
124891 rc = sqlite3ApiExit(db, rc);
124892 sqlite3_mutex_leave(db->mutex);
124893 return rc;
124894}
124895
124896/*
124897** Call this routine when the database connection is closing in order
124898** to clean up loaded extensions
124899*/
124900SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
124901 int i;
124902 assert( sqlite3_mutex_held(db->mutex) );
124903 for(i=0; i<db->nExtension; i++){
124904 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
124905 }
124906 sqlite3DbFree(db, db->aExtension);
124907}
124908
124909/*
124910** Enable or disable extension loading. Extension loading is disabled by
124911** default so as not to open security holes in older applications.
124912*/
124913SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
124914 sqlite3_mutex_enter(db->mutex);
124915 if( onoff ){
124916 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
124917 }else{
124918 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
124919 }
124920 sqlite3_mutex_leave(db->mutex);
124921 return SQLITE_OK;
124922}
124923
124924#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
124925
124926/*
124927** The following object holds the list of automatically loaded
124928** extensions.
124929**
124930** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
124931** mutex must be held while accessing this list.
124932*/
124933typedef struct sqlite3AutoExtList sqlite3AutoExtList;
124934static SQLITE_WSD struct sqlite3AutoExtList {
124935 u32 nExt; /* Number of entries in aExt[] */
124936 void (**aExt)(void); /* Pointers to the extension init functions */
124937} sqlite3Autoext = { 0, 0 };
124938
124939/* The "wsdAutoext" macro will resolve to the autoextension
124940** state vector. If writable static data is unsupported on the target,
124941** we have to locate the state vector at run-time. In the more common
124942** case where writable static data is supported, wsdStat can refer directly
124943** to the "sqlite3Autoext" state vector declared above.
124944*/
124945#ifdef SQLITE_OMIT_WSD
124946# define wsdAutoextInit \
124947 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
124948# define wsdAutoext x[0]
124949#else
124950# define wsdAutoextInit
124951# define wsdAutoext sqlite3Autoext
124952#endif
124953
124954
124955/*
124956** Register a statically linked extension that is automatically
124957** loaded by every new database connection.
124958*/
124959SQLITE_API int sqlite3_auto_extension(
124960 void (*xInit)(void)
124961){
124962 int rc = SQLITE_OK;
124963#ifndef SQLITE_OMIT_AUTOINIT
124964 rc = sqlite3_initialize();
124965 if( rc ){
124966 return rc;
124967 }else
124968#endif
124969 {
124970 u32 i;
124971#if SQLITE_THREADSAFE
124972 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
124973#endif
124974 wsdAutoextInit;
124975 sqlite3_mutex_enter(mutex);
124976 for(i=0; i<wsdAutoext.nExt; i++){
124977 if( wsdAutoext.aExt[i]==xInit ) break;
124978 }
124979 if( i==wsdAutoext.nExt ){
124980 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
124981 void (**aNew)(void);
124982 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
124983 if( aNew==0 ){
124984 rc = SQLITE_NOMEM_BKPT;
124985 }else{
124986 wsdAutoext.aExt = aNew;
124987 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
124988 wsdAutoext.nExt++;
124989 }
124990 }
124991 sqlite3_mutex_leave(mutex);
124992 assert( (rc&0xff)==rc );
124993 return rc;
124994 }
124995}
124996
124997/*
124998** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
124999** set of routines that is invoked for each new database connection, if it
125000** is currently on the list. If xInit is not on the list, then this
125001** routine is a no-op.
125002**
125003** Return 1 if xInit was found on the list and removed. Return 0 if xInit
125004** was not on the list.
125005*/
125006SQLITE_API int sqlite3_cancel_auto_extension(
125007 void (*xInit)(void)
125008){
125009#if SQLITE_THREADSAFE
125010 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
125011#endif
125012 int i;
125013 int n = 0;
125014 wsdAutoextInit;
125015 sqlite3_mutex_enter(mutex);
125016 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
125017 if( wsdAutoext.aExt[i]==xInit ){
125018 wsdAutoext.nExt--;
125019 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
125020 n++;
125021 break;
125022 }
125023 }
125024 sqlite3_mutex_leave(mutex);
125025 return n;
125026}
125027
125028/*
125029** Reset the automatic extension loading mechanism.
125030*/
125031SQLITE_API void sqlite3_reset_auto_extension(void){
125032#ifndef SQLITE_OMIT_AUTOINIT
125033 if( sqlite3_initialize()==SQLITE_OK )
125034#endif
125035 {
125036#if SQLITE_THREADSAFE
125037 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
125038#endif
125039 wsdAutoextInit;
125040 sqlite3_mutex_enter(mutex);
125041 sqlite3_free(wsdAutoext.aExt);
125042 wsdAutoext.aExt = 0;
125043 wsdAutoext.nExt = 0;
125044 sqlite3_mutex_leave(mutex);
125045 }
125046}
125047
125048/*
125049** Load all automatic extensions.
125050**
125051** If anything goes wrong, set an error in the database connection.
125052*/
125053SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
125054 u32 i;
125055 int go = 1;
125056 int rc;
125057 sqlite3_loadext_entry xInit;
125058
125059 wsdAutoextInit;
125060 if( wsdAutoext.nExt==0 ){
125061 /* Common case: early out without every having to acquire a mutex */
125062 return;
125063 }
125064 for(i=0; go; i++){
125065 char *zErrmsg;
125066#if SQLITE_THREADSAFE
125067 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
125068#endif
125069#ifdef SQLITE_OMIT_LOAD_EXTENSION
125070 const sqlite3_api_routines *pThunk = 0;
125071#else
125072 const sqlite3_api_routines *pThunk = &sqlite3Apis;
125073#endif
125074 sqlite3_mutex_enter(mutex);
125075 if( i>=wsdAutoext.nExt ){
125076 xInit = 0;
125077 go = 0;
125078 }else{
125079 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
125080 }
125081 sqlite3_mutex_leave(mutex);
125082 zErrmsg = 0;
125083 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
125084 sqlite3ErrorWithMsg(db, rc,
125085 "automatic extension loading failed: %s", zErrmsg);
125086 go = 0;
125087 }
125088 sqlite3_free(zErrmsg);
125089 }
125090}
125091
125092/************** End of loadext.c *********************************************/
125093/************** Begin file pragma.c ******************************************/
125094/*
125095** 2003 April 6
125096**
125097** The author disclaims copyright to this source code. In place of
125098** a legal notice, here is a blessing:
125099**
125100** May you do good and not evil.
125101** May you find forgiveness for yourself and forgive others.
125102** May you share freely, never taking more than you give.
125103**
125104*************************************************************************
125105** This file contains code used to implement the PRAGMA command.
125106*/
125107/* #include "sqliteInt.h" */
125108
125109#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
125110# if defined(__APPLE__)
125111# define SQLITE_ENABLE_LOCKING_STYLE 1
125112# else
125113# define SQLITE_ENABLE_LOCKING_STYLE 0
125114# endif
125115#endif
125116
125117/***************************************************************************
125118** The "pragma.h" include file is an automatically generated file that
125119** that includes the PragType_XXXX macro definitions and the aPragmaName[]
125120** object. This ensures that the aPragmaName[] table is arranged in
125121** lexicographical order to facility a binary search of the pragma name.
125122** Do not edit pragma.h directly. Edit and rerun the script in at
125123** ../tool/mkpragmatab.tcl. */
125124/************** Include pragma.h in the middle of pragma.c *******************/
125125/************** Begin file pragma.h ******************************************/
125126/* DO NOT EDIT!
125127** This file is automatically generated by the script at
125128** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
125129** that script and rerun it.
125130*/
125131
125132/* The various pragma types */
125133#define PragTyp_ACTIVATE_EXTENSIONS 0
125134#define PragTyp_ANALYSIS_LIMIT 1
125135#define PragTyp_HEADER_VALUE 2
125136#define PragTyp_AUTO_VACUUM 3
125137#define PragTyp_FLAG 4
125138#define PragTyp_BUSY_TIMEOUT 5
125139#define PragTyp_CACHE_SIZE 6
125140#define PragTyp_CACHE_SPILL 7
125141#define PragTyp_CASE_SENSITIVE_LIKE 8
125142#define PragTyp_COLLATION_LIST 9
125143#define PragTyp_COMPILE_OPTIONS 10
125144#define PragTyp_DATA_STORE_DIRECTORY 11
125145#define PragTyp_DATABASE_LIST 12
125146#define PragTyp_DEFAULT_CACHE_SIZE 13
125147#define PragTyp_ENCODING 14
125148#define PragTyp_FOREIGN_KEY_CHECK 15
125149#define PragTyp_FOREIGN_KEY_LIST 16
125150#define PragTyp_FUNCTION_LIST 17
125151#define PragTyp_HARD_HEAP_LIMIT 18
125152#define PragTyp_INCREMENTAL_VACUUM 19
125153#define PragTyp_INDEX_INFO 20
125154#define PragTyp_INDEX_LIST 21
125155#define PragTyp_INTEGRITY_CHECK 22
125156#define PragTyp_JOURNAL_MODE 23
125157#define PragTyp_JOURNAL_SIZE_LIMIT 24
125158#define PragTyp_LOCK_PROXY_FILE 25
125159#define PragTyp_LOCKING_MODE 26
125160#define PragTyp_PAGE_COUNT 27
125161#define PragTyp_MMAP_SIZE 28
125162#define PragTyp_MODULE_LIST 29
125163#define PragTyp_OPTIMIZE 30
125164#define PragTyp_PAGE_SIZE 31
125165#define PragTyp_PRAGMA_LIST 32
125166#define PragTyp_SECURE_DELETE 33
125167#define PragTyp_SHRINK_MEMORY 34
125168#define PragTyp_SOFT_HEAP_LIMIT 35
125169#define PragTyp_SYNCHRONOUS 36
125170#define PragTyp_TABLE_INFO 37
125171#define PragTyp_TEMP_STORE 38
125172#define PragTyp_TEMP_STORE_DIRECTORY 39
125173#define PragTyp_THREADS 40
125174#define PragTyp_WAL_AUTOCHECKPOINT 41
125175#define PragTyp_WAL_CHECKPOINT 42
125176#define PragTyp_LOCK_STATUS 43
125177#define PragTyp_STATS 44
125178
125179/* Property flags associated with various pragma. */
125180#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
125181#define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
125182#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
125183#define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
125184#define PragFlg_Result0 0x10 /* Acts as query when no argument */
125185#define PragFlg_Result1 0x20 /* Acts as query when has one argument */
125186#define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
125187#define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
125188
125189/* Names of columns for pragmas that return multi-column result
125190** or that return single-column results where the name of the
125191** result column is different from the name of the pragma
125192*/
125193static const char *const pragCName[] = {
125194 /* 0 */ "id", /* Used by: foreign_key_list */
125195 /* 1 */ "seq",
125196 /* 2 */ "table",
125197 /* 3 */ "from",
125198 /* 4 */ "to",
125199 /* 5 */ "on_update",
125200 /* 6 */ "on_delete",
125201 /* 7 */ "match",
125202 /* 8 */ "cid", /* Used by: table_xinfo */
125203 /* 9 */ "name",
125204 /* 10 */ "type",
125205 /* 11 */ "notnull",
125206 /* 12 */ "dflt_value",
125207 /* 13 */ "pk",
125208 /* 14 */ "hidden",
125209 /* table_info reuses 8 */
125210 /* 15 */ "seqno", /* Used by: index_xinfo */
125211 /* 16 */ "cid",
125212 /* 17 */ "name",
125213 /* 18 */ "desc",
125214 /* 19 */ "coll",
125215 /* 20 */ "key",
125216 /* 21 */ "name", /* Used by: function_list */
125217 /* 22 */ "builtin",
125218 /* 23 */ "type",
125219 /* 24 */ "enc",
125220 /* 25 */ "narg",
125221 /* 26 */ "flags",
125222 /* 27 */ "tbl", /* Used by: stats */
125223 /* 28 */ "idx",
125224 /* 29 */ "wdth",
125225 /* 30 */ "hght",
125226 /* 31 */ "flgs",
125227 /* 32 */ "seq", /* Used by: index_list */
125228 /* 33 */ "name",
125229 /* 34 */ "unique",
125230 /* 35 */ "origin",
125231 /* 36 */ "partial",
125232 /* 37 */ "table", /* Used by: foreign_key_check */
125233 /* 38 */ "rowid",
125234 /* 39 */ "parent",
125235 /* 40 */ "fkid",
125236 /* index_info reuses 15 */
125237 /* 41 */ "seq", /* Used by: database_list */
125238 /* 42 */ "name",
125239 /* 43 */ "file",
125240 /* 44 */ "busy", /* Used by: wal_checkpoint */
125241 /* 45 */ "log",
125242 /* 46 */ "checkpointed",
125243 /* collation_list reuses 32 */
125244 /* 47 */ "database", /* Used by: lock_status */
125245 /* 48 */ "status",
125246 /* 49 */ "cache_size", /* Used by: default_cache_size */
125247 /* module_list pragma_list reuses 9 */
125248 /* 50 */ "timeout", /* Used by: busy_timeout */
125249};
125250
125251/* Definitions of all built-in pragmas */
125252typedef struct PragmaName {
125253 const char *const zName; /* Name of pragma */
125254 u8 ePragTyp; /* PragTyp_XXX value */
125255 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
125256 u8 iPragCName; /* Start of column names in pragCName[] */
125257 u8 nPragCName; /* Num of col names. 0 means use pragma name */
125258 u64 iArg; /* Extra argument */
125259} PragmaName;
125260static const PragmaName aPragmaName[] = {
125261#if defined(SQLITE_ENABLE_CEROD)
125262 {/* zName: */ "activate_extensions",
125263 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
125264 /* ePragFlg: */ 0,
125265 /* ColNames: */ 0, 0,
125266 /* iArg: */ 0 },
125267#endif
125268 {/* zName: */ "analysis_limit",
125269 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT,
125270 /* ePragFlg: */ PragFlg_Result0,
125271 /* ColNames: */ 0, 0,
125272 /* iArg: */ 0 },
125273#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125274 {/* zName: */ "application_id",
125275 /* ePragTyp: */ PragTyp_HEADER_VALUE,
125276 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
125277 /* ColNames: */ 0, 0,
125278 /* iArg: */ BTREE_APPLICATION_ID },
125279#endif
125280#if !defined(SQLITE_OMIT_AUTOVACUUM)
125281 {/* zName: */ "auto_vacuum",
125282 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
125283 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125284 /* ColNames: */ 0, 0,
125285 /* iArg: */ 0 },
125286#endif
125287#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125288#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
125289 {/* zName: */ "automatic_index",
125290 /* ePragTyp: */ PragTyp_FLAG,
125291 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125292 /* ColNames: */ 0, 0,
125293 /* iArg: */ SQLITE_AutoIndex },
125294#endif
125295#endif
125296 {/* zName: */ "busy_timeout",
125297 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
125298 /* ePragFlg: */ PragFlg_Result0,
125299 /* ColNames: */ 50, 1,
125300 /* iArg: */ 0 },
125301#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125302 {/* zName: */ "cache_size",
125303 /* ePragTyp: */ PragTyp_CACHE_SIZE,
125304 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125305 /* ColNames: */ 0, 0,
125306 /* iArg: */ 0 },
125307#endif
125308#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125309 {/* zName: */ "cache_spill",
125310 /* ePragTyp: */ PragTyp_CACHE_SPILL,
125311 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125312 /* ColNames: */ 0, 0,
125313 /* iArg: */ 0 },
125314#endif
125315#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
125316 {/* zName: */ "case_sensitive_like",
125317 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
125318 /* ePragFlg: */ PragFlg_NoColumns,
125319 /* ColNames: */ 0, 0,
125320 /* iArg: */ 0 },
125321#endif
125322 {/* zName: */ "cell_size_check",
125323 /* ePragTyp: */ PragTyp_FLAG,
125324 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125325 /* ColNames: */ 0, 0,
125326 /* iArg: */ SQLITE_CellSizeCk },
125327#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125328 {/* zName: */ "checkpoint_fullfsync",
125329 /* ePragTyp: */ PragTyp_FLAG,
125330 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125331 /* ColNames: */ 0, 0,
125332 /* iArg: */ SQLITE_CkptFullFSync },
125333#endif
125334#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125335 {/* zName: */ "collation_list",
125336 /* ePragTyp: */ PragTyp_COLLATION_LIST,
125337 /* ePragFlg: */ PragFlg_Result0,
125338 /* ColNames: */ 32, 2,
125339 /* iArg: */ 0 },
125340#endif
125341#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
125342 {/* zName: */ "compile_options",
125343 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
125344 /* ePragFlg: */ PragFlg_Result0,
125345 /* ColNames: */ 0, 0,
125346 /* iArg: */ 0 },
125347#endif
125348#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125349 {/* zName: */ "count_changes",
125350 /* ePragTyp: */ PragTyp_FLAG,
125351 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125352 /* ColNames: */ 0, 0,
125353 /* iArg: */ SQLITE_CountRows },
125354#endif
125355#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
125356 {/* zName: */ "data_store_directory",
125357 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
125358 /* ePragFlg: */ PragFlg_NoColumns1,
125359 /* ColNames: */ 0, 0,
125360 /* iArg: */ 0 },
125361#endif
125362#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125363 {/* zName: */ "data_version",
125364 /* ePragTyp: */ PragTyp_HEADER_VALUE,
125365 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
125366 /* ColNames: */ 0, 0,
125367 /* iArg: */ BTREE_DATA_VERSION },
125368#endif
125369#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125370 {/* zName: */ "database_list",
125371 /* ePragTyp: */ PragTyp_DATABASE_LIST,
125372 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
125373 /* ColNames: */ 41, 3,
125374 /* iArg: */ 0 },
125375#endif
125376#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
125377 {/* zName: */ "default_cache_size",
125378 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
125379 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125380 /* ColNames: */ 49, 1,
125381 /* iArg: */ 0 },
125382#endif
125383#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125384#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125385 {/* zName: */ "defer_foreign_keys",
125386 /* ePragTyp: */ PragTyp_FLAG,
125387 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125388 /* ColNames: */ 0, 0,
125389 /* iArg: */ SQLITE_DeferFKs },
125390#endif
125391#endif
125392#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125393 {/* zName: */ "empty_result_callbacks",
125394 /* ePragTyp: */ PragTyp_FLAG,
125395 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125396 /* ColNames: */ 0, 0,
125397 /* iArg: */ SQLITE_NullCallback },
125398#endif
125399#if !defined(SQLITE_OMIT_UTF16)
125400 {/* zName: */ "encoding",
125401 /* ePragTyp: */ PragTyp_ENCODING,
125402 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125403 /* ColNames: */ 0, 0,
125404 /* iArg: */ 0 },
125405#endif
125406#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125407 {/* zName: */ "foreign_key_check",
125408 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
125409 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1|PragFlg_SchemaOpt,
125410 /* ColNames: */ 37, 4,
125411 /* iArg: */ 0 },
125412#endif
125413#if !defined(SQLITE_OMIT_FOREIGN_KEY)
125414 {/* zName: */ "foreign_key_list",
125415 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
125416 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125417 /* ColNames: */ 0, 8,
125418 /* iArg: */ 0 },
125419#endif
125420#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125421#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
125422 {/* zName: */ "foreign_keys",
125423 /* ePragTyp: */ PragTyp_FLAG,
125424 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125425 /* ColNames: */ 0, 0,
125426 /* iArg: */ SQLITE_ForeignKeys },
125427#endif
125428#endif
125429#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125430 {/* zName: */ "freelist_count",
125431 /* ePragTyp: */ PragTyp_HEADER_VALUE,
125432 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
125433 /* ColNames: */ 0, 0,
125434 /* iArg: */ BTREE_FREE_PAGE_COUNT },
125435#endif
125436#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125437 {/* zName: */ "full_column_names",
125438 /* ePragTyp: */ PragTyp_FLAG,
125439 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125440 /* ColNames: */ 0, 0,
125441 /* iArg: */ SQLITE_FullColNames },
125442 {/* zName: */ "fullfsync",
125443 /* ePragTyp: */ PragTyp_FLAG,
125444 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125445 /* ColNames: */ 0, 0,
125446 /* iArg: */ SQLITE_FullFSync },
125447#endif
125448#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125449#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125450 {/* zName: */ "function_list",
125451 /* ePragTyp: */ PragTyp_FUNCTION_LIST,
125452 /* ePragFlg: */ PragFlg_Result0,
125453 /* ColNames: */ 21, 6,
125454 /* iArg: */ 0 },
125455#endif
125456#endif
125457 {/* zName: */ "hard_heap_limit",
125458 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT,
125459 /* ePragFlg: */ PragFlg_Result0,
125460 /* ColNames: */ 0, 0,
125461 /* iArg: */ 0 },
125462#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125463#if !defined(SQLITE_OMIT_CHECK)
125464 {/* zName: */ "ignore_check_constraints",
125465 /* ePragTyp: */ PragTyp_FLAG,
125466 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125467 /* ColNames: */ 0, 0,
125468 /* iArg: */ SQLITE_IgnoreChecks },
125469#endif
125470#endif
125471#if !defined(SQLITE_OMIT_AUTOVACUUM)
125472 {/* zName: */ "incremental_vacuum",
125473 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
125474 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
125475 /* ColNames: */ 0, 0,
125476 /* iArg: */ 0 },
125477#endif
125478#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125479 {/* zName: */ "index_info",
125480 /* ePragTyp: */ PragTyp_INDEX_INFO,
125481 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125482 /* ColNames: */ 15, 3,
125483 /* iArg: */ 0 },
125484 {/* zName: */ "index_list",
125485 /* ePragTyp: */ PragTyp_INDEX_LIST,
125486 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125487 /* ColNames: */ 32, 5,
125488 /* iArg: */ 0 },
125489 {/* zName: */ "index_xinfo",
125490 /* ePragTyp: */ PragTyp_INDEX_INFO,
125491 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125492 /* ColNames: */ 15, 6,
125493 /* iArg: */ 1 },
125494#endif
125495#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
125496 {/* zName: */ "integrity_check",
125497 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
125498 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
125499 /* ColNames: */ 0, 0,
125500 /* iArg: */ 0 },
125501#endif
125502#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125503 {/* zName: */ "journal_mode",
125504 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
125505 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125506 /* ColNames: */ 0, 0,
125507 /* iArg: */ 0 },
125508 {/* zName: */ "journal_size_limit",
125509 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
125510 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
125511 /* ColNames: */ 0, 0,
125512 /* iArg: */ 0 },
125513#endif
125514#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125515 {/* zName: */ "legacy_alter_table",
125516 /* ePragTyp: */ PragTyp_FLAG,
125517 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125518 /* ColNames: */ 0, 0,
125519 /* iArg: */ SQLITE_LegacyAlter },
125520#endif
125521#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
125522 {/* zName: */ "lock_proxy_file",
125523 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
125524 /* ePragFlg: */ PragFlg_NoColumns1,
125525 /* ColNames: */ 0, 0,
125526 /* iArg: */ 0 },
125527#endif
125528#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
125529 {/* zName: */ "lock_status",
125530 /* ePragTyp: */ PragTyp_LOCK_STATUS,
125531 /* ePragFlg: */ PragFlg_Result0,
125532 /* ColNames: */ 47, 2,
125533 /* iArg: */ 0 },
125534#endif
125535#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125536 {/* zName: */ "locking_mode",
125537 /* ePragTyp: */ PragTyp_LOCKING_MODE,
125538 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
125539 /* ColNames: */ 0, 0,
125540 /* iArg: */ 0 },
125541 {/* zName: */ "max_page_count",
125542 /* ePragTyp: */ PragTyp_PAGE_COUNT,
125543 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125544 /* ColNames: */ 0, 0,
125545 /* iArg: */ 0 },
125546 {/* zName: */ "mmap_size",
125547 /* ePragTyp: */ PragTyp_MMAP_SIZE,
125548 /* ePragFlg: */ 0,
125549 /* ColNames: */ 0, 0,
125550 /* iArg: */ 0 },
125551#endif
125552#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125553#if !defined(SQLITE_OMIT_VIRTUALTABLE)
125554#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125555 {/* zName: */ "module_list",
125556 /* ePragTyp: */ PragTyp_MODULE_LIST,
125557 /* ePragFlg: */ PragFlg_Result0,
125558 /* ColNames: */ 9, 1,
125559 /* iArg: */ 0 },
125560#endif
125561#endif
125562#endif
125563 {/* zName: */ "optimize",
125564 /* ePragTyp: */ PragTyp_OPTIMIZE,
125565 /* ePragFlg: */ PragFlg_Result1|PragFlg_NeedSchema,
125566 /* ColNames: */ 0, 0,
125567 /* iArg: */ 0 },
125568#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125569 {/* zName: */ "page_count",
125570 /* ePragTyp: */ PragTyp_PAGE_COUNT,
125571 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125572 /* ColNames: */ 0, 0,
125573 /* iArg: */ 0 },
125574 {/* zName: */ "page_size",
125575 /* ePragTyp: */ PragTyp_PAGE_SIZE,
125576 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125577 /* ColNames: */ 0, 0,
125578 /* iArg: */ 0 },
125579#endif
125580#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125581#if defined(SQLITE_DEBUG)
125582 {/* zName: */ "parser_trace",
125583 /* ePragTyp: */ PragTyp_FLAG,
125584 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125585 /* ColNames: */ 0, 0,
125586 /* iArg: */ SQLITE_ParserTrace },
125587#endif
125588#endif
125589#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
125590 {/* zName: */ "pragma_list",
125591 /* ePragTyp: */ PragTyp_PRAGMA_LIST,
125592 /* ePragFlg: */ PragFlg_Result0,
125593 /* ColNames: */ 9, 1,
125594 /* iArg: */ 0 },
125595#endif
125596#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125597 {/* zName: */ "query_only",
125598 /* ePragTyp: */ PragTyp_FLAG,
125599 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125600 /* ColNames: */ 0, 0,
125601 /* iArg: */ SQLITE_QueryOnly },
125602#endif
125603#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
125604 {/* zName: */ "quick_check",
125605 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
125606 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
125607 /* ColNames: */ 0, 0,
125608 /* iArg: */ 0 },
125609#endif
125610#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125611 {/* zName: */ "read_uncommitted",
125612 /* ePragTyp: */ PragTyp_FLAG,
125613 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125614 /* ColNames: */ 0, 0,
125615 /* iArg: */ SQLITE_ReadUncommit },
125616 {/* zName: */ "recursive_triggers",
125617 /* ePragTyp: */ PragTyp_FLAG,
125618 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125619 /* ColNames: */ 0, 0,
125620 /* iArg: */ SQLITE_RecTriggers },
125621 {/* zName: */ "reverse_unordered_selects",
125622 /* ePragTyp: */ PragTyp_FLAG,
125623 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125624 /* ColNames: */ 0, 0,
125625 /* iArg: */ SQLITE_ReverseOrder },
125626#endif
125627#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125628 {/* zName: */ "schema_version",
125629 /* ePragTyp: */ PragTyp_HEADER_VALUE,
125630 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
125631 /* ColNames: */ 0, 0,
125632 /* iArg: */ BTREE_SCHEMA_VERSION },
125633#endif
125634#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125635 {/* zName: */ "secure_delete",
125636 /* ePragTyp: */ PragTyp_SECURE_DELETE,
125637 /* ePragFlg: */ PragFlg_Result0,
125638 /* ColNames: */ 0, 0,
125639 /* iArg: */ 0 },
125640#endif
125641#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125642 {/* zName: */ "short_column_names",
125643 /* ePragTyp: */ PragTyp_FLAG,
125644 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125645 /* ColNames: */ 0, 0,
125646 /* iArg: */ SQLITE_ShortColNames },
125647#endif
125648 {/* zName: */ "shrink_memory",
125649 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
125650 /* ePragFlg: */ PragFlg_NoColumns,
125651 /* ColNames: */ 0, 0,
125652 /* iArg: */ 0 },
125653 {/* zName: */ "soft_heap_limit",
125654 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
125655 /* ePragFlg: */ PragFlg_Result0,
125656 /* ColNames: */ 0, 0,
125657 /* iArg: */ 0 },
125658#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125659#if defined(SQLITE_DEBUG)
125660 {/* zName: */ "sql_trace",
125661 /* ePragTyp: */ PragTyp_FLAG,
125662 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125663 /* ColNames: */ 0, 0,
125664 /* iArg: */ SQLITE_SqlTrace },
125665#endif
125666#endif
125667#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
125668 {/* zName: */ "stats",
125669 /* ePragTyp: */ PragTyp_STATS,
125670 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
125671 /* ColNames: */ 27, 5,
125672 /* iArg: */ 0 },
125673#endif
125674#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125675 {/* zName: */ "synchronous",
125676 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
125677 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
125678 /* ColNames: */ 0, 0,
125679 /* iArg: */ 0 },
125680#endif
125681#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
125682 {/* zName: */ "table_info",
125683 /* ePragTyp: */ PragTyp_TABLE_INFO,
125684 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125685 /* ColNames: */ 8, 6,
125686 /* iArg: */ 0 },
125687 {/* zName: */ "table_xinfo",
125688 /* ePragTyp: */ PragTyp_TABLE_INFO,
125689 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
125690 /* ColNames: */ 8, 7,
125691 /* iArg: */ 1 },
125692#endif
125693#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
125694 {/* zName: */ "temp_store",
125695 /* ePragTyp: */ PragTyp_TEMP_STORE,
125696 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125697 /* ColNames: */ 0, 0,
125698 /* iArg: */ 0 },
125699 {/* zName: */ "temp_store_directory",
125700 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
125701 /* ePragFlg: */ PragFlg_NoColumns1,
125702 /* ColNames: */ 0, 0,
125703 /* iArg: */ 0 },
125704#endif
125705 {/* zName: */ "threads",
125706 /* ePragTyp: */ PragTyp_THREADS,
125707 /* ePragFlg: */ PragFlg_Result0,
125708 /* ColNames: */ 0, 0,
125709 /* iArg: */ 0 },
125710#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125711 {/* zName: */ "trusted_schema",
125712 /* ePragTyp: */ PragTyp_FLAG,
125713 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125714 /* ColNames: */ 0, 0,
125715 /* iArg: */ SQLITE_TrustedSchema },
125716#endif
125717#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
125718 {/* zName: */ "user_version",
125719 /* ePragTyp: */ PragTyp_HEADER_VALUE,
125720 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
125721 /* ColNames: */ 0, 0,
125722 /* iArg: */ BTREE_USER_VERSION },
125723#endif
125724#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125725#if defined(SQLITE_DEBUG)
125726 {/* zName: */ "vdbe_addoptrace",
125727 /* ePragTyp: */ PragTyp_FLAG,
125728 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125729 /* ColNames: */ 0, 0,
125730 /* iArg: */ SQLITE_VdbeAddopTrace },
125731 {/* zName: */ "vdbe_debug",
125732 /* ePragTyp: */ PragTyp_FLAG,
125733 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125734 /* ColNames: */ 0, 0,
125735 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
125736 {/* zName: */ "vdbe_eqp",
125737 /* ePragTyp: */ PragTyp_FLAG,
125738 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125739 /* ColNames: */ 0, 0,
125740 /* iArg: */ SQLITE_VdbeEQP },
125741 {/* zName: */ "vdbe_listing",
125742 /* ePragTyp: */ PragTyp_FLAG,
125743 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125744 /* ColNames: */ 0, 0,
125745 /* iArg: */ SQLITE_VdbeListing },
125746 {/* zName: */ "vdbe_trace",
125747 /* ePragTyp: */ PragTyp_FLAG,
125748 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125749 /* ColNames: */ 0, 0,
125750 /* iArg: */ SQLITE_VdbeTrace },
125751#endif
125752#endif
125753#if !defined(SQLITE_OMIT_WAL)
125754 {/* zName: */ "wal_autocheckpoint",
125755 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
125756 /* ePragFlg: */ 0,
125757 /* ColNames: */ 0, 0,
125758 /* iArg: */ 0 },
125759 {/* zName: */ "wal_checkpoint",
125760 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
125761 /* ePragFlg: */ PragFlg_NeedSchema,
125762 /* ColNames: */ 44, 3,
125763 /* iArg: */ 0 },
125764#endif
125765#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
125766 {/* zName: */ "writable_schema",
125767 /* ePragTyp: */ PragTyp_FLAG,
125768 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
125769 /* ColNames: */ 0, 0,
125770 /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
125771#endif
125772};
125773/* Number of pragmas: 67 on by default, 77 total. */
125774
125775/************** End of pragma.h **********************************************/
125776/************** Continuing where we left off in pragma.c *********************/
125777
125778/*
125779** Interpret the given string as a safety level. Return 0 for OFF,
125780** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
125781** unrecognized string argument. The FULL and EXTRA option is disallowed
125782** if the omitFull parameter it 1.
125783**
125784** Note that the values returned are one less that the values that
125785** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
125786** to support legacy SQL code. The safety level used to be boolean
125787** and older scripts may have used numbers 0 for OFF and 1 for ON.
125788*/
125789static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
125790 /* 123456789 123456789 123 */
125791 static const char zText[] = "onoffalseyestruextrafull";
125792 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
125793 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
125794 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
125795 /* on no off false yes true extra full */
125796 int i, n;
125797 if( sqlite3Isdigit(*z) ){
125798 return (u8)sqlite3Atoi(z);
125799 }
125800 n = sqlite3Strlen30(z);
125801 for(i=0; i<ArraySize(iLength); i++){
125802 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
125803 && (!omitFull || iValue[i]<=1)
125804 ){
125805 return iValue[i];
125806 }
125807 }
125808 return dflt;
125809}
125810
125811/*
125812** Interpret the given string as a boolean value.
125813*/
125814SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
125815 return getSafetyLevel(z,1,dflt)!=0;
125816}
125817
125818/* The sqlite3GetBoolean() function is used by other modules but the
125819** remainder of this file is specific to PRAGMA processing. So omit
125820** the rest of the file if PRAGMAs are omitted from the build.
125821*/
125822#if !defined(SQLITE_OMIT_PRAGMA)
125823
125824/*
125825** Interpret the given string as a locking mode value.
125826*/
125827static int getLockingMode(const char *z){
125828 if( z ){
125829 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
125830 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
125831 }
125832 return PAGER_LOCKINGMODE_QUERY;
125833}
125834
125835#ifndef SQLITE_OMIT_AUTOVACUUM
125836/*
125837** Interpret the given string as an auto-vacuum mode value.
125838**
125839** The following strings, "none", "full" and "incremental" are
125840** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
125841*/
125842static int getAutoVacuum(const char *z){
125843 int i;
125844 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
125845 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
125846 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
125847 i = sqlite3Atoi(z);
125848 return (u8)((i>=0&&i<=2)?i:0);
125849}
125850#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
125851
125852#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125853/*
125854** Interpret the given string as a temp db location. Return 1 for file
125855** backed temporary databases, 2 for the Red-Black tree in memory database
125856** and 0 to use the compile-time default.
125857*/
125858static int getTempStore(const char *z){
125859 if( z[0]>='0' && z[0]<='2' ){
125860 return z[0] - '0';
125861 }else if( sqlite3StrICmp(z, "file")==0 ){
125862 return 1;
125863 }else if( sqlite3StrICmp(z, "memory")==0 ){
125864 return 2;
125865 }else{
125866 return 0;
125867 }
125868}
125869#endif /* SQLITE_PAGER_PRAGMAS */
125870
125871#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125872/*
125873** Invalidate temp storage, either when the temp storage is changed
125874** from default, or when 'file' and the temp_store_directory has changed
125875*/
125876static int invalidateTempStorage(Parse *pParse){
125877 sqlite3 *db = pParse->db;
125878 if( db->aDb[1].pBt!=0 ){
125879 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
125880 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
125881 "from within a transaction");
125882 return SQLITE_ERROR;
125883 }
125884 sqlite3BtreeClose(db->aDb[1].pBt);
125885 db->aDb[1].pBt = 0;
125886 sqlite3ResetAllSchemasOfConnection(db);
125887 }
125888 return SQLITE_OK;
125889}
125890#endif /* SQLITE_PAGER_PRAGMAS */
125891
125892#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125893/*
125894** If the TEMP database is open, close it and mark the database schema
125895** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
125896** or DEFAULT_TEMP_STORE pragmas.
125897*/
125898static int changeTempStorage(Parse *pParse, const char *zStorageType){
125899 int ts = getTempStore(zStorageType);
125900 sqlite3 *db = pParse->db;
125901 if( db->temp_store==ts ) return SQLITE_OK;
125902 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
125903 return SQLITE_ERROR;
125904 }
125905 db->temp_store = (u8)ts;
125906 return SQLITE_OK;
125907}
125908#endif /* SQLITE_PAGER_PRAGMAS */
125909
125910/*
125911** Set result column names for a pragma.
125912*/
125913static void setPragmaResultColumnNames(
125914 Vdbe *v, /* The query under construction */
125915 const PragmaName *pPragma /* The pragma */
125916){
125917 u8 n = pPragma->nPragCName;
125918 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
125919 if( n==0 ){
125920 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
125921 }else{
125922 int i, j;
125923 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
125924 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
125925 }
125926 }
125927}
125928
125929/*
125930** Generate code to return a single integer value.
125931*/
125932static void returnSingleInt(Vdbe *v, i64 value){
125933 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
125934 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
125935}
125936
125937/*
125938** Generate code to return a single text value.
125939*/
125940static void returnSingleText(
125941 Vdbe *v, /* Prepared statement under construction */
125942 const char *zValue /* Value to be returned */
125943){
125944 if( zValue ){
125945 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
125946 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
125947 }
125948}
125949
125950
125951/*
125952** Set the safety_level and pager flags for pager iDb. Or if iDb<0
125953** set these values for all pagers.
125954*/
125955#ifndef SQLITE_OMIT_PAGER_PRAGMAS
125956static void setAllPagerFlags(sqlite3 *db){
125957 if( db->autoCommit ){
125958 Db *pDb = db->aDb;
125959 int n = db->nDb;
125960 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
125961 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
125962 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
125963 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
125964 == PAGER_FLAGS_MASK );
125965 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
125966 while( (n--) > 0 ){
125967 if( pDb->pBt ){
125968 sqlite3BtreeSetPagerFlags(pDb->pBt,
125969 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
125970 }
125971 pDb++;
125972 }
125973 }
125974}
125975#else
125976# define setAllPagerFlags(X) /* no-op */
125977#endif
125978
125979
125980/*
125981** Return a human-readable name for a constraint resolution action.
125982*/
125983#ifndef SQLITE_OMIT_FOREIGN_KEY
125984static const char *actionName(u8 action){
125985 const char *zName;
125986 switch( action ){
125987 case OE_SetNull: zName = "SET NULL"; break;
125988 case OE_SetDflt: zName = "SET DEFAULT"; break;
125989 case OE_Cascade: zName = "CASCADE"; break;
125990 case OE_Restrict: zName = "RESTRICT"; break;
125991 default: zName = "NO ACTION";
125992 assert( action==OE_None ); break;
125993 }
125994 return zName;
125995}
125996#endif
125997
125998
125999/*
126000** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
126001** defined in pager.h. This function returns the associated lowercase
126002** journal-mode name.
126003*/
126004SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
126005 static char * const azModeName[] = {
126006 "delete", "persist", "off", "truncate", "memory"
126007#ifndef SQLITE_OMIT_WAL
126008 , "wal"
126009#endif
126010 };
126011 assert( PAGER_JOURNALMODE_DELETE==0 );
126012 assert( PAGER_JOURNALMODE_PERSIST==1 );
126013 assert( PAGER_JOURNALMODE_OFF==2 );
126014 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
126015 assert( PAGER_JOURNALMODE_MEMORY==4 );
126016 assert( PAGER_JOURNALMODE_WAL==5 );
126017 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
126018
126019 if( eMode==ArraySize(azModeName) ) return 0;
126020 return azModeName[eMode];
126021}
126022
126023/*
126024** Locate a pragma in the aPragmaName[] array.
126025*/
126026static const PragmaName *pragmaLocate(const char *zName){
126027 int upr, lwr, mid = 0, rc;
126028 lwr = 0;
126029 upr = ArraySize(aPragmaName)-1;
126030 while( lwr<=upr ){
126031 mid = (lwr+upr)/2;
126032 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
126033 if( rc==0 ) break;
126034 if( rc<0 ){
126035 upr = mid - 1;
126036 }else{
126037 lwr = mid + 1;
126038 }
126039 }
126040 return lwr>upr ? 0 : &aPragmaName[mid];
126041}
126042
126043/*
126044** Create zero or more entries in the output for the SQL functions
126045** defined by FuncDef p.
126046*/
126047static void pragmaFunclistLine(
126048 Vdbe *v, /* The prepared statement being created */
126049 FuncDef *p, /* A particular function definition */
126050 int isBuiltin, /* True if this is a built-in function */
126051 int showInternFuncs /* True if showing internal functions */
126052){
126053 for(; p; p=p->pNext){
126054 const char *zType;
126055 static const u32 mask =
126056 SQLITE_DETERMINISTIC |
126057 SQLITE_DIRECTONLY |
126058 SQLITE_SUBTYPE |
126059 SQLITE_INNOCUOUS |
126060 SQLITE_FUNC_INTERNAL
126061 ;
126062 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
126063
126064 assert( SQLITE_FUNC_ENCMASK==0x3 );
126065 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
126066 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
126067 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
126068
126069 if( p->xSFunc==0 ) continue;
126070 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
126071 && showInternFuncs==0
126072 ){
126073 continue;
126074 }
126075 if( p->xValue!=0 ){
126076 zType = "w";
126077 }else if( p->xFinalize!=0 ){
126078 zType = "a";
126079 }else{
126080 zType = "s";
126081 }
126082 sqlite3VdbeMultiLoad(v, 1, "sissii",
126083 p->zName, isBuiltin,
126084 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
126085 p->nArg,
126086 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
126087 );
126088 }
126089}
126090
126091
126092/*
126093** Helper subroutine for PRAGMA integrity_check:
126094**
126095** Generate code to output a single-column result row with a value of the
126096** string held in register 3. Decrement the result count in register 1
126097** and halt if the maximum number of result rows have been issued.
126098*/
126099static int integrityCheckResultRow(Vdbe *v){
126100 int addr;
126101 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
126102 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
126103 VdbeCoverage(v);
126104 sqlite3VdbeAddOp0(v, OP_Halt);
126105 return addr;
126106}
126107
126108/*
126109** Process a pragma statement.
126110**
126111** Pragmas are of this form:
126112**
126113** PRAGMA [schema.]id [= value]
126114**
126115** The identifier might also be a string. The value is a string, and
126116** identifier, or a number. If minusFlag is true, then the value is
126117** a number that was preceded by a minus sign.
126118**
126119** If the left side is "database.id" then pId1 is the database name
126120** and pId2 is the id. If the left side is just "id" then pId1 is the
126121** id and pId2 is any empty string.
126122*/
126123SQLITE_PRIVATE void sqlite3Pragma(
126124 Parse *pParse,
126125 Token *pId1, /* First part of [schema.]id field */
126126 Token *pId2, /* Second part of [schema.]id field, or NULL */
126127 Token *pValue, /* Token for <value>, or NULL */
126128 int minusFlag /* True if a '-' sign preceded <value> */
126129){
126130 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
126131 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
126132 const char *zDb = 0; /* The database name */
126133 Token *pId; /* Pointer to <id> token */
126134 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
126135 int iDb; /* Database index for <database> */
126136 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
126137 sqlite3 *db = pParse->db; /* The database connection */
126138 Db *pDb; /* The specific database being pragmaed */
126139 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
126140 const PragmaName *pPragma; /* The pragma */
126141
126142 if( v==0 ) return;
126143 sqlite3VdbeRunOnlyOnce(v);
126144 pParse->nMem = 2;
126145
126146 /* Interpret the [schema.] part of the pragma statement. iDb is the
126147 ** index of the database this pragma is being applied to in db.aDb[]. */
126148 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
126149 if( iDb<0 ) return;
126150 pDb = &db->aDb[iDb];
126151
126152 /* If the temp database has been explicitly named as part of the
126153 ** pragma, make sure it is open.
126154 */
126155 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
126156 return;
126157 }
126158
126159 zLeft = sqlite3NameFromToken(db, pId);
126160 if( !zLeft ) return;
126161 if( minusFlag ){
126162 zRight = sqlite3MPrintf(db, "-%T", pValue);
126163 }else{
126164 zRight = sqlite3NameFromToken(db, pValue);
126165 }
126166
126167 assert( pId2 );
126168 zDb = pId2->n>0 ? pDb->zDbSName : 0;
126169 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
126170 goto pragma_out;
126171 }
126172
126173 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
126174 ** connection. If it returns SQLITE_OK, then assume that the VFS
126175 ** handled the pragma and generate a no-op prepared statement.
126176 **
126177 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
126178 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
126179 ** object corresponding to the database file to which the pragma
126180 ** statement refers.
126181 **
126182 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
126183 ** file control is an array of pointers to strings (char**) in which the
126184 ** second element of the array is the name of the pragma and the third
126185 ** element is the argument to the pragma or NULL if the pragma has no
126186 ** argument.
126187 */
126188 aFcntl[0] = 0;
126189 aFcntl[1] = zLeft;
126190 aFcntl[2] = zRight;
126191 aFcntl[3] = 0;
126192 db->busyHandler.nBusy = 0;
126193 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
126194 if( rc==SQLITE_OK ){
126195 sqlite3VdbeSetNumCols(v, 1);
126196 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
126197 returnSingleText(v, aFcntl[0]);
126198 sqlite3_free(aFcntl[0]);
126199 goto pragma_out;
126200 }
126201 if( rc!=SQLITE_NOTFOUND ){
126202 if( aFcntl[0] ){
126203 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
126204 sqlite3_free(aFcntl[0]);
126205 }
126206 pParse->nErr++;
126207 pParse->rc = rc;
126208 goto pragma_out;
126209 }
126210
126211 /* Locate the pragma in the lookup table */
126212 pPragma = pragmaLocate(zLeft);
126213 if( pPragma==0 ) goto pragma_out;
126214
126215 /* Make sure the database schema is loaded if the pragma requires that */
126216 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
126217 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
126218 }
126219
126220 /* Register the result column names for pragmas that return results */
126221 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
126222 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
126223 ){
126224 setPragmaResultColumnNames(v, pPragma);
126225 }
126226
126227 /* Jump to the appropriate pragma handler */
126228 switch( pPragma->ePragTyp ){
126229
126230#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
126231 /*
126232 ** PRAGMA [schema.]default_cache_size
126233 ** PRAGMA [schema.]default_cache_size=N
126234 **
126235 ** The first form reports the current persistent setting for the
126236 ** page cache size. The value returned is the maximum number of
126237 ** pages in the page cache. The second form sets both the current
126238 ** page cache size value and the persistent page cache size value
126239 ** stored in the database file.
126240 **
126241 ** Older versions of SQLite would set the default cache size to a
126242 ** negative number to indicate synchronous=OFF. These days, synchronous
126243 ** is always on by default regardless of the sign of the default cache
126244 ** size. But continue to take the absolute value of the default cache
126245 ** size of historical compatibility.
126246 */
126247 case PragTyp_DEFAULT_CACHE_SIZE: {
126248 static const int iLn = VDBE_OFFSET_LINENO(2);
126249 static const VdbeOpList getCacheSize[] = {
126250 { OP_Transaction, 0, 0, 0}, /* 0 */
126251 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
126252 { OP_IfPos, 1, 8, 0},
126253 { OP_Integer, 0, 2, 0},
126254 { OP_Subtract, 1, 2, 1},
126255 { OP_IfPos, 1, 8, 0},
126256 { OP_Integer, 0, 1, 0}, /* 6 */
126257 { OP_Noop, 0, 0, 0},
126258 { OP_ResultRow, 1, 1, 0},
126259 };
126260 VdbeOp *aOp;
126261 sqlite3VdbeUsesBtree(v, iDb);
126262 if( !zRight ){
126263 pParse->nMem += 2;
126264 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
126265 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
126266 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
126267 aOp[0].p1 = iDb;
126268 aOp[1].p1 = iDb;
126269 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
126270 }else{
126271 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
126272 sqlite3BeginWriteOperation(pParse, 0, iDb);
126273 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
126274 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126275 pDb->pSchema->cache_size = size;
126276 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
126277 }
126278 break;
126279 }
126280#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
126281
126282#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
126283 /*
126284 ** PRAGMA [schema.]page_size
126285 ** PRAGMA [schema.]page_size=N
126286 **
126287 ** The first form reports the current setting for the
126288 ** database page size in bytes. The second form sets the
126289 ** database page size value. The value can only be set if
126290 ** the database has not yet been created.
126291 */
126292 case PragTyp_PAGE_SIZE: {
126293 Btree *pBt = pDb->pBt;
126294 assert( pBt!=0 );
126295 if( !zRight ){
126296 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
126297 returnSingleInt(v, size);
126298 }else{
126299 /* Malloc may fail when setting the page-size, as there is an internal
126300 ** buffer that the pager module resizes using sqlite3_realloc().
126301 */
126302 db->nextPagesize = sqlite3Atoi(zRight);
126303 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
126304 sqlite3OomFault(db);
126305 }
126306 }
126307 break;
126308 }
126309
126310 /*
126311 ** PRAGMA [schema.]secure_delete
126312 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
126313 **
126314 ** The first form reports the current setting for the
126315 ** secure_delete flag. The second form changes the secure_delete
126316 ** flag setting and reports the new value.
126317 */
126318 case PragTyp_SECURE_DELETE: {
126319 Btree *pBt = pDb->pBt;
126320 int b = -1;
126321 assert( pBt!=0 );
126322 if( zRight ){
126323 if( sqlite3_stricmp(zRight, "fast")==0 ){
126324 b = 2;
126325 }else{
126326 b = sqlite3GetBoolean(zRight, 0);
126327 }
126328 }
126329 if( pId2->n==0 && b>=0 ){
126330 int ii;
126331 for(ii=0; ii<db->nDb; ii++){
126332 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
126333 }
126334 }
126335 b = sqlite3BtreeSecureDelete(pBt, b);
126336 returnSingleInt(v, b);
126337 break;
126338 }
126339
126340 /*
126341 ** PRAGMA [schema.]max_page_count
126342 ** PRAGMA [schema.]max_page_count=N
126343 **
126344 ** The first form reports the current setting for the
126345 ** maximum number of pages in the database file. The
126346 ** second form attempts to change this setting. Both
126347 ** forms return the current setting.
126348 **
126349 ** The absolute value of N is used. This is undocumented and might
126350 ** change. The only purpose is to provide an easy way to test
126351 ** the sqlite3AbsInt32() function.
126352 **
126353 ** PRAGMA [schema.]page_count
126354 **
126355 ** Return the number of pages in the specified database.
126356 */
126357 case PragTyp_PAGE_COUNT: {
126358 int iReg;
126359 i64 x = 0;
126360 sqlite3CodeVerifySchema(pParse, iDb);
126361 iReg = ++pParse->nMem;
126362 if( sqlite3Tolower(zLeft[0])=='p' ){
126363 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
126364 }else{
126365 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
126366 if( x<0 ) x = 0;
126367 else if( x>0xfffffffe ) x = 0xfffffffe;
126368 }else{
126369 x = 0;
126370 }
126371 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
126372 }
126373 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
126374 break;
126375 }
126376
126377 /*
126378 ** PRAGMA [schema.]locking_mode
126379 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
126380 */
126381 case PragTyp_LOCKING_MODE: {
126382 const char *zRet = "normal";
126383 int eMode = getLockingMode(zRight);
126384
126385 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
126386 /* Simple "PRAGMA locking_mode;" statement. This is a query for
126387 ** the current default locking mode (which may be different to
126388 ** the locking-mode of the main database).
126389 */
126390 eMode = db->dfltLockMode;
126391 }else{
126392 Pager *pPager;
126393 if( pId2->n==0 ){
126394 /* This indicates that no database name was specified as part
126395 ** of the PRAGMA command. In this case the locking-mode must be
126396 ** set on all attached databases, as well as the main db file.
126397 **
126398 ** Also, the sqlite3.dfltLockMode variable is set so that
126399 ** any subsequently attached databases also use the specified
126400 ** locking mode.
126401 */
126402 int ii;
126403 assert(pDb==&db->aDb[0]);
126404 for(ii=2; ii<db->nDb; ii++){
126405 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
126406 sqlite3PagerLockingMode(pPager, eMode);
126407 }
126408 db->dfltLockMode = (u8)eMode;
126409 }
126410 pPager = sqlite3BtreePager(pDb->pBt);
126411 eMode = sqlite3PagerLockingMode(pPager, eMode);
126412 }
126413
126414 assert( eMode==PAGER_LOCKINGMODE_NORMAL
126415 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
126416 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
126417 zRet = "exclusive";
126418 }
126419 returnSingleText(v, zRet);
126420 break;
126421 }
126422
126423 /*
126424 ** PRAGMA [schema.]journal_mode
126425 ** PRAGMA [schema.]journal_mode =
126426 ** (delete|persist|off|truncate|memory|wal|off)
126427 */
126428 case PragTyp_JOURNAL_MODE: {
126429 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
126430 int ii; /* Loop counter */
126431
126432 if( zRight==0 ){
126433 /* If there is no "=MODE" part of the pragma, do a query for the
126434 ** current mode */
126435 eMode = PAGER_JOURNALMODE_QUERY;
126436 }else{
126437 const char *zMode;
126438 int n = sqlite3Strlen30(zRight);
126439 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
126440 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
126441 }
126442 if( !zMode ){
126443 /* If the "=MODE" part does not match any known journal mode,
126444 ** then do a query */
126445 eMode = PAGER_JOURNALMODE_QUERY;
126446 }
126447 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
126448 /* Do not allow journal-mode "OFF" in defensive since the database
126449 ** can become corrupted using ordinary SQL when the journal is off */
126450 eMode = PAGER_JOURNALMODE_QUERY;
126451 }
126452 }
126453 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
126454 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
126455 iDb = 0;
126456 pId2->n = 1;
126457 }
126458 for(ii=db->nDb-1; ii>=0; ii--){
126459 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
126460 sqlite3VdbeUsesBtree(v, ii);
126461 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
126462 }
126463 }
126464 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
126465 break;
126466 }
126467
126468 /*
126469 ** PRAGMA [schema.]journal_size_limit
126470 ** PRAGMA [schema.]journal_size_limit=N
126471 **
126472 ** Get or set the size limit on rollback journal files.
126473 */
126474 case PragTyp_JOURNAL_SIZE_LIMIT: {
126475 Pager *pPager = sqlite3BtreePager(pDb->pBt);
126476 i64 iLimit = -2;
126477 if( zRight ){
126478 sqlite3DecOrHexToI64(zRight, &iLimit);
126479 if( iLimit<-1 ) iLimit = -1;
126480 }
126481 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
126482 returnSingleInt(v, iLimit);
126483 break;
126484 }
126485
126486#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
126487
126488 /*
126489 ** PRAGMA [schema.]auto_vacuum
126490 ** PRAGMA [schema.]auto_vacuum=N
126491 **
126492 ** Get or set the value of the database 'auto-vacuum' parameter.
126493 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
126494 */
126495#ifndef SQLITE_OMIT_AUTOVACUUM
126496 case PragTyp_AUTO_VACUUM: {
126497 Btree *pBt = pDb->pBt;
126498 assert( pBt!=0 );
126499 if( !zRight ){
126500 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
126501 }else{
126502 int eAuto = getAutoVacuum(zRight);
126503 assert( eAuto>=0 && eAuto<=2 );
126504 db->nextAutovac = (u8)eAuto;
126505 /* Call SetAutoVacuum() to set initialize the internal auto and
126506 ** incr-vacuum flags. This is required in case this connection
126507 ** creates the database file. It is important that it is created
126508 ** as an auto-vacuum capable db.
126509 */
126510 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
126511 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
126512 /* When setting the auto_vacuum mode to either "full" or
126513 ** "incremental", write the value of meta[6] in the database
126514 ** file. Before writing to meta[6], check that meta[3] indicates
126515 ** that this really is an auto-vacuum capable database.
126516 */
126517 static const int iLn = VDBE_OFFSET_LINENO(2);
126518 static const VdbeOpList setMeta6[] = {
126519 { OP_Transaction, 0, 1, 0}, /* 0 */
126520 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
126521 { OP_If, 1, 0, 0}, /* 2 */
126522 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
126523 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
126524 };
126525 VdbeOp *aOp;
126526 int iAddr = sqlite3VdbeCurrentAddr(v);
126527 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
126528 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
126529 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
126530 aOp[0].p1 = iDb;
126531 aOp[1].p1 = iDb;
126532 aOp[2].p2 = iAddr+4;
126533 aOp[4].p1 = iDb;
126534 aOp[4].p3 = eAuto - 1;
126535 sqlite3VdbeUsesBtree(v, iDb);
126536 }
126537 }
126538 break;
126539 }
126540#endif
126541
126542 /*
126543 ** PRAGMA [schema.]incremental_vacuum(N)
126544 **
126545 ** Do N steps of incremental vacuuming on a database.
126546 */
126547#ifndef SQLITE_OMIT_AUTOVACUUM
126548 case PragTyp_INCREMENTAL_VACUUM: {
126549 int iLimit, addr;
126550 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
126551 iLimit = 0x7fffffff;
126552 }
126553 sqlite3BeginWriteOperation(pParse, 0, iDb);
126554 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
126555 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
126556 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
126557 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
126558 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
126559 sqlite3VdbeJumpHere(v, addr);
126560 break;
126561 }
126562#endif
126563
126564#ifndef SQLITE_OMIT_PAGER_PRAGMAS
126565 /*
126566 ** PRAGMA [schema.]cache_size
126567 ** PRAGMA [schema.]cache_size=N
126568 **
126569 ** The first form reports the current local setting for the
126570 ** page cache size. The second form sets the local
126571 ** page cache size value. If N is positive then that is the
126572 ** number of pages in the cache. If N is negative, then the
126573 ** number of pages is adjusted so that the cache uses -N kibibytes
126574 ** of memory.
126575 */
126576 case PragTyp_CACHE_SIZE: {
126577 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126578 if( !zRight ){
126579 returnSingleInt(v, pDb->pSchema->cache_size);
126580 }else{
126581 int size = sqlite3Atoi(zRight);
126582 pDb->pSchema->cache_size = size;
126583 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
126584 }
126585 break;
126586 }
126587
126588 /*
126589 ** PRAGMA [schema.]cache_spill
126590 ** PRAGMA cache_spill=BOOLEAN
126591 ** PRAGMA [schema.]cache_spill=N
126592 **
126593 ** The first form reports the current local setting for the
126594 ** page cache spill size. The second form turns cache spill on
126595 ** or off. When turnning cache spill on, the size is set to the
126596 ** current cache_size. The third form sets a spill size that
126597 ** may be different form the cache size.
126598 ** If N is positive then that is the
126599 ** number of pages in the cache. If N is negative, then the
126600 ** number of pages is adjusted so that the cache uses -N kibibytes
126601 ** of memory.
126602 **
126603 ** If the number of cache_spill pages is less then the number of
126604 ** cache_size pages, no spilling occurs until the page count exceeds
126605 ** the number of cache_size pages.
126606 **
126607 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
126608 ** not just the schema specified.
126609 */
126610 case PragTyp_CACHE_SPILL: {
126611 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126612 if( !zRight ){
126613 returnSingleInt(v,
126614 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
126615 sqlite3BtreeSetSpillSize(pDb->pBt,0));
126616 }else{
126617 int size = 1;
126618 if( sqlite3GetInt32(zRight, &size) ){
126619 sqlite3BtreeSetSpillSize(pDb->pBt, size);
126620 }
126621 if( sqlite3GetBoolean(zRight, size!=0) ){
126622 db->flags |= SQLITE_CacheSpill;
126623 }else{
126624 db->flags &= ~(u64)SQLITE_CacheSpill;
126625 }
126626 setAllPagerFlags(db);
126627 }
126628 break;
126629 }
126630
126631 /*
126632 ** PRAGMA [schema.]mmap_size(N)
126633 **
126634 ** Used to set mapping size limit. The mapping size limit is
126635 ** used to limit the aggregate size of all memory mapped regions of the
126636 ** database file. If this parameter is set to zero, then memory mapping
126637 ** is not used at all. If N is negative, then the default memory map
126638 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
126639 ** The parameter N is measured in bytes.
126640 **
126641 ** This value is advisory. The underlying VFS is free to memory map
126642 ** as little or as much as it wants. Except, if N is set to 0 then the
126643 ** upper layers will never invoke the xFetch interfaces to the VFS.
126644 */
126645 case PragTyp_MMAP_SIZE: {
126646 sqlite3_int64 sz;
126647#if SQLITE_MAX_MMAP_SIZE>0
126648 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
126649 if( zRight ){
126650 int ii;
126651 sqlite3DecOrHexToI64(zRight, &sz);
126652 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
126653 if( pId2->n==0 ) db->szMmap = sz;
126654 for(ii=db->nDb-1; ii>=0; ii--){
126655 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
126656 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
126657 }
126658 }
126659 }
126660 sz = -1;
126661 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
126662#else
126663 sz = 0;
126664 rc = SQLITE_OK;
126665#endif
126666 if( rc==SQLITE_OK ){
126667 returnSingleInt(v, sz);
126668 }else if( rc!=SQLITE_NOTFOUND ){
126669 pParse->nErr++;
126670 pParse->rc = rc;
126671 }
126672 break;
126673 }
126674
126675 /*
126676 ** PRAGMA temp_store
126677 ** PRAGMA temp_store = "default"|"memory"|"file"
126678 **
126679 ** Return or set the local value of the temp_store flag. Changing
126680 ** the local value does not make changes to the disk file and the default
126681 ** value will be restored the next time the database is opened.
126682 **
126683 ** Note that it is possible for the library compile-time options to
126684 ** override this setting
126685 */
126686 case PragTyp_TEMP_STORE: {
126687 if( !zRight ){
126688 returnSingleInt(v, db->temp_store);
126689 }else{
126690 changeTempStorage(pParse, zRight);
126691 }
126692 break;
126693 }
126694
126695 /*
126696 ** PRAGMA temp_store_directory
126697 ** PRAGMA temp_store_directory = ""|"directory_name"
126698 **
126699 ** Return or set the local value of the temp_store_directory flag. Changing
126700 ** the value sets a specific directory to be used for temporary files.
126701 ** Setting to a null string reverts to the default temporary directory search.
126702 ** If temporary directory is changed, then invalidateTempStorage.
126703 **
126704 */
126705 case PragTyp_TEMP_STORE_DIRECTORY: {
126706 if( !zRight ){
126707 returnSingleText(v, sqlite3_temp_directory);
126708 }else{
126709#ifndef SQLITE_OMIT_WSD
126710 if( zRight[0] ){
126711 int res;
126712 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
126713 if( rc!=SQLITE_OK || res==0 ){
126714 sqlite3ErrorMsg(pParse, "not a writable directory");
126715 goto pragma_out;
126716 }
126717 }
126718 if( SQLITE_TEMP_STORE==0
126719 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
126720 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
126721 ){
126722 invalidateTempStorage(pParse);
126723 }
126724 sqlite3_free(sqlite3_temp_directory);
126725 if( zRight[0] ){
126726 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
126727 }else{
126728 sqlite3_temp_directory = 0;
126729 }
126730#endif /* SQLITE_OMIT_WSD */
126731 }
126732 break;
126733 }
126734
126735#if SQLITE_OS_WIN
126736 /*
126737 ** PRAGMA data_store_directory
126738 ** PRAGMA data_store_directory = ""|"directory_name"
126739 **
126740 ** Return or set the local value of the data_store_directory flag. Changing
126741 ** the value sets a specific directory to be used for database files that
126742 ** were specified with a relative pathname. Setting to a null string reverts
126743 ** to the default database directory, which for database files specified with
126744 ** a relative path will probably be based on the current directory for the
126745 ** process. Database file specified with an absolute path are not impacted
126746 ** by this setting, regardless of its value.
126747 **
126748 */
126749 case PragTyp_DATA_STORE_DIRECTORY: {
126750 if( !zRight ){
126751 returnSingleText(v, sqlite3_data_directory);
126752 }else{
126753#ifndef SQLITE_OMIT_WSD
126754 if( zRight[0] ){
126755 int res;
126756 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
126757 if( rc!=SQLITE_OK || res==0 ){
126758 sqlite3ErrorMsg(pParse, "not a writable directory");
126759 goto pragma_out;
126760 }
126761 }
126762 sqlite3_free(sqlite3_data_directory);
126763 if( zRight[0] ){
126764 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
126765 }else{
126766 sqlite3_data_directory = 0;
126767 }
126768#endif /* SQLITE_OMIT_WSD */
126769 }
126770 break;
126771 }
126772#endif
126773
126774#if SQLITE_ENABLE_LOCKING_STYLE
126775 /*
126776 ** PRAGMA [schema.]lock_proxy_file
126777 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
126778 **
126779 ** Return or set the value of the lock_proxy_file flag. Changing
126780 ** the value sets a specific file to be used for database access locks.
126781 **
126782 */
126783 case PragTyp_LOCK_PROXY_FILE: {
126784 if( !zRight ){
126785 Pager *pPager = sqlite3BtreePager(pDb->pBt);
126786 char *proxy_file_path = NULL;
126787 sqlite3_file *pFile = sqlite3PagerFile(pPager);
126788 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
126789 &proxy_file_path);
126790 returnSingleText(v, proxy_file_path);
126791 }else{
126792 Pager *pPager = sqlite3BtreePager(pDb->pBt);
126793 sqlite3_file *pFile = sqlite3PagerFile(pPager);
126794 int res;
126795 if( zRight[0] ){
126796 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
126797 zRight);
126798 } else {
126799 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
126800 NULL);
126801 }
126802 if( res!=SQLITE_OK ){
126803 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
126804 goto pragma_out;
126805 }
126806 }
126807 break;
126808 }
126809#endif /* SQLITE_ENABLE_LOCKING_STYLE */
126810
126811 /*
126812 ** PRAGMA [schema.]synchronous
126813 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
126814 **
126815 ** Return or set the local value of the synchronous flag. Changing
126816 ** the local value does not make changes to the disk file and the
126817 ** default value will be restored the next time the database is
126818 ** opened.
126819 */
126820 case PragTyp_SYNCHRONOUS: {
126821 if( !zRight ){
126822 returnSingleInt(v, pDb->safety_level-1);
126823 }else{
126824 if( !db->autoCommit ){
126825 sqlite3ErrorMsg(pParse,
126826 "Safety level may not be changed inside a transaction");
126827 }else if( iDb!=1 ){
126828 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
126829 if( iLevel==0 ) iLevel = 1;
126830 pDb->safety_level = iLevel;
126831 pDb->bSyncSet = 1;
126832 setAllPagerFlags(db);
126833 }
126834 }
126835 break;
126836 }
126837#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
126838
126839#ifndef SQLITE_OMIT_FLAG_PRAGMAS
126840 case PragTyp_FLAG: {
126841 if( zRight==0 ){
126842 setPragmaResultColumnNames(v, pPragma);
126843 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
126844 }else{
126845 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
126846 if( db->autoCommit==0 ){
126847 /* Foreign key support may not be enabled or disabled while not
126848 ** in auto-commit mode. */
126849 mask &= ~(SQLITE_ForeignKeys);
126850 }
126851#if SQLITE_USER_AUTHENTICATION
126852 if( db->auth.authLevel==UAUTH_User ){
126853 /* Do not allow non-admin users to modify the schema arbitrarily */
126854 mask &= ~(SQLITE_WriteSchema);
126855 }
126856#endif
126857
126858 if( sqlite3GetBoolean(zRight, 0) ){
126859 db->flags |= mask;
126860 }else{
126861 db->flags &= ~mask;
126862 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
126863 }
126864
126865 /* Many of the flag-pragmas modify the code generated by the SQL
126866 ** compiler (eg. count_changes). So add an opcode to expire all
126867 ** compiled SQL statements after modifying a pragma value.
126868 */
126869 sqlite3VdbeAddOp0(v, OP_Expire);
126870 setAllPagerFlags(db);
126871 }
126872 break;
126873 }
126874#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
126875
126876#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
126877 /*
126878 ** PRAGMA table_info(<table>)
126879 **
126880 ** Return a single row for each column of the named table. The columns of
126881 ** the returned data set are:
126882 **
126883 ** cid: Column id (numbered from left to right, starting at 0)
126884 ** name: Column name
126885 ** type: Column declaration type.
126886 ** notnull: True if 'NOT NULL' is part of column declaration
126887 ** dflt_value: The default value for the column, if any.
126888 ** pk: Non-zero for PK fields.
126889 */
126890 case PragTyp_TABLE_INFO: if( zRight ){
126891 Table *pTab;
126892 sqlite3CodeVerifyNamedSchema(pParse, zDb);
126893 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
126894 if( pTab ){
126895 int i, k;
126896 int nHidden = 0;
126897 Column *pCol;
126898 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
126899 pParse->nMem = 7;
126900 sqlite3ViewGetColumnNames(pParse, pTab);
126901 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
126902 int isHidden = 0;
126903 if( pCol->colFlags & COLFLAG_NOINSERT ){
126904 if( pPragma->iArg==0 ){
126905 nHidden++;
126906 continue;
126907 }
126908 if( pCol->colFlags & COLFLAG_VIRTUAL ){
126909 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
126910 }else if( pCol->colFlags & COLFLAG_STORED ){
126911 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
126912 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
126913 isHidden = 1; /* HIDDEN */
126914 }
126915 }
126916 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
126917 k = 0;
126918 }else if( pPk==0 ){
126919 k = 1;
126920 }else{
126921 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
126922 }
126923 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN || isHidden>=2 );
126924 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
126925 i-nHidden,
126926 pCol->zName,
126927 sqlite3ColumnType(pCol,""),
126928 pCol->notNull ? 1 : 0,
126929 pCol->pDflt && isHidden<2 ? pCol->pDflt->u.zToken : 0,
126930 k,
126931 isHidden);
126932 }
126933 }
126934 }
126935 break;
126936
126937#ifdef SQLITE_DEBUG
126938 case PragTyp_STATS: {
126939 Index *pIdx;
126940 HashElem *i;
126941 pParse->nMem = 5;
126942 sqlite3CodeVerifySchema(pParse, iDb);
126943 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
126944 Table *pTab = sqliteHashData(i);
126945 sqlite3VdbeMultiLoad(v, 1, "ssiii",
126946 pTab->zName,
126947 0,
126948 pTab->szTabRow,
126949 pTab->nRowLogEst,
126950 pTab->tabFlags);
126951 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126952 sqlite3VdbeMultiLoad(v, 2, "siiiX",
126953 pIdx->zName,
126954 pIdx->szIdxRow,
126955 pIdx->aiRowLogEst[0],
126956 pIdx->hasStat1);
126957 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
126958 }
126959 }
126960 }
126961 break;
126962#endif
126963
126964 case PragTyp_INDEX_INFO: if( zRight ){
126965 Index *pIdx;
126966 Table *pTab;
126967 pIdx = sqlite3FindIndex(db, zRight, zDb);
126968 if( pIdx==0 ){
126969 /* If there is no index named zRight, check to see if there is a
126970 ** WITHOUT ROWID table named zRight, and if there is, show the
126971 ** structure of the PRIMARY KEY index for that table. */
126972 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
126973 if( pTab && !HasRowid(pTab) ){
126974 pIdx = sqlite3PrimaryKeyIndex(pTab);
126975 }
126976 }
126977 if( pIdx ){
126978 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
126979 int i;
126980 int mx;
126981 if( pPragma->iArg ){
126982 /* PRAGMA index_xinfo (newer version with more rows and columns) */
126983 mx = pIdx->nColumn;
126984 pParse->nMem = 6;
126985 }else{
126986 /* PRAGMA index_info (legacy version) */
126987 mx = pIdx->nKeyCol;
126988 pParse->nMem = 3;
126989 }
126990 pTab = pIdx->pTable;
126991 sqlite3CodeVerifySchema(pParse, iIdxDb);
126992 assert( pParse->nMem<=pPragma->nPragCName );
126993 for(i=0; i<mx; i++){
126994 i16 cnum = pIdx->aiColumn[i];
126995 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
126996 cnum<0 ? 0 : pTab->aCol[cnum].zName);
126997 if( pPragma->iArg ){
126998 sqlite3VdbeMultiLoad(v, 4, "isiX",
126999 pIdx->aSortOrder[i],
127000 pIdx->azColl[i],
127001 i<pIdx->nKeyCol);
127002 }
127003 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
127004 }
127005 }
127006 }
127007 break;
127008
127009 case PragTyp_INDEX_LIST: if( zRight ){
127010 Index *pIdx;
127011 Table *pTab;
127012 int i;
127013 pTab = sqlite3FindTable(db, zRight, zDb);
127014 if( pTab ){
127015 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
127016 pParse->nMem = 5;
127017 sqlite3CodeVerifySchema(pParse, iTabDb);
127018 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
127019 const char *azOrigin[] = { "c", "u", "pk" };
127020 sqlite3VdbeMultiLoad(v, 1, "isisi",
127021 i,
127022 pIdx->zName,
127023 IsUniqueIndex(pIdx),
127024 azOrigin[pIdx->idxType],
127025 pIdx->pPartIdxWhere!=0);
127026 }
127027 }
127028 }
127029 break;
127030
127031 case PragTyp_DATABASE_LIST: {
127032 int i;
127033 pParse->nMem = 3;
127034 for(i=0; i<db->nDb; i++){
127035 if( db->aDb[i].pBt==0 ) continue;
127036 assert( db->aDb[i].zDbSName!=0 );
127037 sqlite3VdbeMultiLoad(v, 1, "iss",
127038 i,
127039 db->aDb[i].zDbSName,
127040 sqlite3BtreeGetFilename(db->aDb[i].pBt));
127041 }
127042 }
127043 break;
127044
127045 case PragTyp_COLLATION_LIST: {
127046 int i = 0;
127047 HashElem *p;
127048 pParse->nMem = 2;
127049 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
127050 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
127051 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
127052 }
127053 }
127054 break;
127055
127056#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
127057 case PragTyp_FUNCTION_LIST: {
127058 int i;
127059 HashElem *j;
127060 FuncDef *p;
127061 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
127062 pParse->nMem = 6;
127063 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
127064 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
127065 pragmaFunclistLine(v, p, 1, showInternFunc);
127066 }
127067 }
127068 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
127069 p = (FuncDef*)sqliteHashData(j);
127070 pragmaFunclistLine(v, p, 0, showInternFunc);
127071 }
127072 }
127073 break;
127074
127075#ifndef SQLITE_OMIT_VIRTUALTABLE
127076 case PragTyp_MODULE_LIST: {
127077 HashElem *j;
127078 pParse->nMem = 1;
127079 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
127080 Module *pMod = (Module*)sqliteHashData(j);
127081 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
127082 }
127083 }
127084 break;
127085#endif /* SQLITE_OMIT_VIRTUALTABLE */
127086
127087 case PragTyp_PRAGMA_LIST: {
127088 int i;
127089 for(i=0; i<ArraySize(aPragmaName); i++){
127090 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
127091 }
127092 }
127093 break;
127094#endif /* SQLITE_INTROSPECTION_PRAGMAS */
127095
127096#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
127097
127098#ifndef SQLITE_OMIT_FOREIGN_KEY
127099 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
127100 FKey *pFK;
127101 Table *pTab;
127102 pTab = sqlite3FindTable(db, zRight, zDb);
127103 if( pTab ){
127104 pFK = pTab->pFKey;
127105 if( pFK ){
127106 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
127107 int i = 0;
127108 pParse->nMem = 8;
127109 sqlite3CodeVerifySchema(pParse, iTabDb);
127110 while(pFK){
127111 int j;
127112 for(j=0; j<pFK->nCol; j++){
127113 sqlite3VdbeMultiLoad(v, 1, "iissssss",
127114 i,
127115 j,
127116 pFK->zTo,
127117 pTab->aCol[pFK->aCol[j].iFrom].zName,
127118 pFK->aCol[j].zCol,
127119 actionName(pFK->aAction[1]), /* ON UPDATE */
127120 actionName(pFK->aAction[0]), /* ON DELETE */
127121 "NONE");
127122 }
127123 ++i;
127124 pFK = pFK->pNextFrom;
127125 }
127126 }
127127 }
127128 }
127129 break;
127130#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
127131
127132#ifndef SQLITE_OMIT_FOREIGN_KEY
127133#ifndef SQLITE_OMIT_TRIGGER
127134 case PragTyp_FOREIGN_KEY_CHECK: {
127135 FKey *pFK; /* A foreign key constraint */
127136 Table *pTab; /* Child table contain "REFERENCES" keyword */
127137 Table *pParent; /* Parent table that child points to */
127138 Index *pIdx; /* Index in the parent table */
127139 int i; /* Loop counter: Foreign key number for pTab */
127140 int j; /* Loop counter: Field of the foreign key */
127141 HashElem *k; /* Loop counter: Next table in schema */
127142 int x; /* result variable */
127143 int regResult; /* 3 registers to hold a result row */
127144 int regKey; /* Register to hold key for checking the FK */
127145 int regRow; /* Registers to hold a row from pTab */
127146 int addrTop; /* Top of a loop checking foreign keys */
127147 int addrOk; /* Jump here if the key is OK */
127148 int *aiCols; /* child to parent column mapping */
127149
127150 regResult = pParse->nMem+1;
127151 pParse->nMem += 4;
127152 regKey = ++pParse->nMem;
127153 regRow = ++pParse->nMem;
127154 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
127155 while( k ){
127156 if( zRight ){
127157 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
127158 k = 0;
127159 }else{
127160 pTab = (Table*)sqliteHashData(k);
127161 k = sqliteHashNext(k);
127162 }
127163 if( pTab==0 || pTab->pFKey==0 ) continue;
127164 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
127165 zDb = db->aDb[iDb].zDbSName;
127166 sqlite3CodeVerifySchema(pParse, iDb);
127167 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
127168 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
127169 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
127170 sqlite3VdbeLoadString(v, regResult, pTab->zName);
127171 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
127172 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
127173 if( pParent==0 ) continue;
127174 pIdx = 0;
127175 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
127176 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
127177 if( x==0 ){
127178 if( pIdx==0 ){
127179 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
127180 }else{
127181 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
127182 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
127183 }
127184 }else{
127185 k = 0;
127186 break;
127187 }
127188 }
127189 assert( pParse->nErr>0 || pFK==0 );
127190 if( pFK ) break;
127191 if( pParse->nTab<i ) pParse->nTab = i;
127192 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
127193 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
127194 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
127195 pIdx = 0;
127196 aiCols = 0;
127197 if( pParent ){
127198 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
127199 assert( x==0 );
127200 }
127201 addrOk = sqlite3VdbeMakeLabel(pParse);
127202
127203 /* Generate code to read the child key values into registers
127204 ** regRow..regRow+n. If any of the child key values are NULL, this
127205 ** row cannot cause an FK violation. Jump directly to addrOk in
127206 ** this case. */
127207 for(j=0; j<pFK->nCol; j++){
127208 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
127209 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
127210 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
127211 }
127212
127213 /* Generate code to query the parent index for a matching parent
127214 ** key. If a match is found, jump to addrOk. */
127215 if( pIdx ){
127216 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
127217 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
127218 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
127219 VdbeCoverage(v);
127220 }else if( pParent ){
127221 int jmp = sqlite3VdbeCurrentAddr(v)+2;
127222 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
127223 sqlite3VdbeGoto(v, addrOk);
127224 assert( pFK->nCol==1 );
127225 }
127226
127227 /* Generate code to report an FK violation to the caller. */
127228 if( HasRowid(pTab) ){
127229 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
127230 }else{
127231 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
127232 }
127233 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
127234 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
127235 sqlite3VdbeResolveLabel(v, addrOk);
127236 sqlite3DbFree(db, aiCols);
127237 }
127238 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
127239 sqlite3VdbeJumpHere(v, addrTop);
127240 }
127241 }
127242 break;
127243#endif /* !defined(SQLITE_OMIT_TRIGGER) */
127244#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
127245
127246#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
127247 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
127248 ** used will be case sensitive or not depending on the RHS.
127249 */
127250 case PragTyp_CASE_SENSITIVE_LIKE: {
127251 if( zRight ){
127252 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
127253 }
127254 }
127255 break;
127256#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
127257
127258#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
127259# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
127260#endif
127261
127262#ifndef SQLITE_OMIT_INTEGRITY_CHECK
127263 /* PRAGMA integrity_check
127264 ** PRAGMA integrity_check(N)
127265 ** PRAGMA quick_check
127266 ** PRAGMA quick_check(N)
127267 **
127268 ** Verify the integrity of the database.
127269 **
127270 ** The "quick_check" is reduced version of
127271 ** integrity_check designed to detect most database corruption
127272 ** without the overhead of cross-checking indexes. Quick_check
127273 ** is linear time wherease integrity_check is O(NlogN).
127274 **
127275 ** The maximum nubmer of errors is 100 by default. A different default
127276 ** can be specified using a numeric parameter N.
127277 **
127278 ** Or, the parameter N can be the name of a table. In that case, only
127279 ** the one table named is verified. The freelist is only verified if
127280 ** the named table is "sqlite_schema" (or one of its aliases).
127281 **
127282 ** All schemas are checked by default. To check just a single
127283 ** schema, use the form:
127284 **
127285 ** PRAGMA schema.integrity_check;
127286 */
127287 case PragTyp_INTEGRITY_CHECK: {
127288 int i, j, addr, mxErr;
127289 Table *pObjTab = 0; /* Check only this one table, if not NULL */
127290
127291 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
127292
127293 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
127294 ** then iDb is set to the index of the database identified by <db>.
127295 ** In this case, the integrity of database iDb only is verified by
127296 ** the VDBE created below.
127297 **
127298 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
127299 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
127300 ** to -1 here, to indicate that the VDBE should verify the integrity
127301 ** of all attached databases. */
127302 assert( iDb>=0 );
127303 assert( iDb==0 || pId2->z );
127304 if( pId2->z==0 ) iDb = -1;
127305
127306 /* Initialize the VDBE program */
127307 pParse->nMem = 6;
127308
127309 /* Set the maximum error count */
127310 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
127311 if( zRight ){
127312 if( sqlite3GetInt32(zRight, &mxErr) ){
127313 if( mxErr<=0 ){
127314 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
127315 }
127316 }else{
127317 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
127318 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
127319 }
127320 }
127321 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
127322
127323 /* Do an integrity check on each database file */
127324 for(i=0; i<db->nDb; i++){
127325 HashElem *x; /* For looping over tables in the schema */
127326 Hash *pTbls; /* Set of all tables in the schema */
127327 int *aRoot; /* Array of root page numbers of all btrees */
127328 int cnt = 0; /* Number of entries in aRoot[] */
127329 int mxIdx = 0; /* Maximum number of indexes for any table */
127330
127331 if( OMIT_TEMPDB && i==1 ) continue;
127332 if( iDb>=0 && i!=iDb ) continue;
127333
127334 sqlite3CodeVerifySchema(pParse, i);
127335
127336 /* Do an integrity check of the B-Tree
127337 **
127338 ** Begin by finding the root pages numbers
127339 ** for all tables and indices in the database.
127340 */
127341 assert( sqlite3SchemaMutexHeld(db, i, 0) );
127342 pTbls = &db->aDb[i].pSchema->tblHash;
127343 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127344 Table *pTab = sqliteHashData(x); /* Current table */
127345 Index *pIdx; /* An index on pTab */
127346 int nIdx; /* Number of indexes on pTab */
127347 if( pObjTab && pObjTab!=pTab ) continue;
127348 if( HasRowid(pTab) ) cnt++;
127349 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
127350 if( nIdx>mxIdx ) mxIdx = nIdx;
127351 }
127352 if( cnt==0 ) continue;
127353 if( pObjTab ) cnt++;
127354 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
127355 if( aRoot==0 ) break;
127356 cnt = 0;
127357 if( pObjTab ) aRoot[++cnt] = 0;
127358 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127359 Table *pTab = sqliteHashData(x);
127360 Index *pIdx;
127361 if( pObjTab && pObjTab!=pTab ) continue;
127362 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
127363 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127364 aRoot[++cnt] = pIdx->tnum;
127365 }
127366 }
127367 aRoot[0] = cnt;
127368
127369 /* Make sure sufficient number of registers have been allocated */
127370 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
127371 sqlite3ClearTempRegCache(pParse);
127372
127373 /* Do the b-tree integrity checks */
127374 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
127375 sqlite3VdbeChangeP5(v, (u8)i);
127376 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
127377 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
127378 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
127379 P4_DYNAMIC);
127380 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
127381 integrityCheckResultRow(v);
127382 sqlite3VdbeJumpHere(v, addr);
127383
127384 /* Make sure all the indices are constructed correctly.
127385 */
127386 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
127387 Table *pTab = sqliteHashData(x);
127388 Index *pIdx, *pPk;
127389 Index *pPrior = 0;
127390 int loopTop;
127391 int iDataCur, iIdxCur;
127392 int r1 = -1;
127393
127394 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
127395 if( pObjTab && pObjTab!=pTab ) continue;
127396 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
127397 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
127398 1, 0, &iDataCur, &iIdxCur);
127399 /* reg[7] counts the number of entries in the table.
127400 ** reg[8+i] counts the number of entries in the i-th index
127401 */
127402 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
127403 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127404 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
127405 }
127406 assert( pParse->nMem>=8+j );
127407 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
127408 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
127409 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
127410 if( !isQuick ){
127411 /* Sanity check on record header decoding */
127412 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
127413 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
127414 }
127415 /* Verify that all NOT NULL columns really are NOT NULL */
127416 for(j=0; j<pTab->nCol; j++){
127417 char *zErr;
127418 int jmp2;
127419 if( j==pTab->iPKey ) continue;
127420 if( pTab->aCol[j].notNull==0 ) continue;
127421 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
127422 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
127423 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
127424 }
127425 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
127426 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
127427 pTab->aCol[j].zName);
127428 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
127429 integrityCheckResultRow(v);
127430 sqlite3VdbeJumpHere(v, jmp2);
127431 }
127432 /* Verify CHECK constraints */
127433 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
127434 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
127435 if( db->mallocFailed==0 ){
127436 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
127437 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
127438 char *zErr;
127439 int k;
127440 pParse->iSelfTab = iDataCur + 1;
127441 for(k=pCheck->nExpr-1; k>0; k--){
127442 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
127443 }
127444 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
127445 SQLITE_JUMPIFNULL);
127446 sqlite3VdbeResolveLabel(v, addrCkFault);
127447 pParse->iSelfTab = 0;
127448 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
127449 pTab->zName);
127450 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
127451 integrityCheckResultRow(v);
127452 sqlite3VdbeResolveLabel(v, addrCkOk);
127453 }
127454 sqlite3ExprListDelete(db, pCheck);
127455 }
127456 if( !isQuick ){ /* Omit the remaining tests for quick_check */
127457 /* Validate index entries for the current row */
127458 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127459 int jmp2, jmp3, jmp4, jmp5;
127460 int ckUniq = sqlite3VdbeMakeLabel(pParse);
127461 if( pPk==pIdx ) continue;
127462 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
127463 pPrior, r1);
127464 pPrior = pIdx;
127465 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
127466 /* Verify that an index entry exists for the current table row */
127467 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
127468 pIdx->nColumn); VdbeCoverage(v);
127469 sqlite3VdbeLoadString(v, 3, "row ");
127470 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
127471 sqlite3VdbeLoadString(v, 4, " missing from index ");
127472 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
127473 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
127474 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
127475 jmp4 = integrityCheckResultRow(v);
127476 sqlite3VdbeJumpHere(v, jmp2);
127477 /* For UNIQUE indexes, verify that only one entry exists with the
127478 ** current key. The entry is unique if (1) any column is NULL
127479 ** or (2) the next entry has a different key */
127480 if( IsUniqueIndex(pIdx) ){
127481 int uniqOk = sqlite3VdbeMakeLabel(pParse);
127482 int jmp6;
127483 int kk;
127484 for(kk=0; kk<pIdx->nKeyCol; kk++){
127485 int iCol = pIdx->aiColumn[kk];
127486 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
127487 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
127488 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
127489 VdbeCoverage(v);
127490 }
127491 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
127492 sqlite3VdbeGoto(v, uniqOk);
127493 sqlite3VdbeJumpHere(v, jmp6);
127494 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
127495 pIdx->nKeyCol); VdbeCoverage(v);
127496 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
127497 sqlite3VdbeGoto(v, jmp5);
127498 sqlite3VdbeResolveLabel(v, uniqOk);
127499 }
127500 sqlite3VdbeJumpHere(v, jmp4);
127501 sqlite3ResolvePartIdxLabel(pParse, jmp3);
127502 }
127503 }
127504 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
127505 sqlite3VdbeJumpHere(v, loopTop-1);
127506 if( !isQuick ){
127507 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
127508 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
127509 if( pPk==pIdx ) continue;
127510 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
127511 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
127512 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
127513 sqlite3VdbeLoadString(v, 4, pIdx->zName);
127514 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
127515 integrityCheckResultRow(v);
127516 sqlite3VdbeJumpHere(v, addr);
127517 }
127518 }
127519 }
127520 }
127521 {
127522 static const int iLn = VDBE_OFFSET_LINENO(2);
127523 static const VdbeOpList endCode[] = {
127524 { OP_AddImm, 1, 0, 0}, /* 0 */
127525 { OP_IfNotZero, 1, 4, 0}, /* 1 */
127526 { OP_String8, 0, 3, 0}, /* 2 */
127527 { OP_ResultRow, 3, 1, 0}, /* 3 */
127528 { OP_Halt, 0, 0, 0}, /* 4 */
127529 { OP_String8, 0, 3, 0}, /* 5 */
127530 { OP_Goto, 0, 3, 0}, /* 6 */
127531 };
127532 VdbeOp *aOp;
127533
127534 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
127535 if( aOp ){
127536 aOp[0].p2 = 1-mxErr;
127537 aOp[2].p4type = P4_STATIC;
127538 aOp[2].p4.z = "ok";
127539 aOp[5].p4type = P4_STATIC;
127540 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
127541 }
127542 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
127543 }
127544 }
127545 break;
127546#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
127547
127548#ifndef SQLITE_OMIT_UTF16
127549 /*
127550 ** PRAGMA encoding
127551 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
127552 **
127553 ** In its first form, this pragma returns the encoding of the main
127554 ** database. If the database is not initialized, it is initialized now.
127555 **
127556 ** The second form of this pragma is a no-op if the main database file
127557 ** has not already been initialized. In this case it sets the default
127558 ** encoding that will be used for the main database file if a new file
127559 ** is created. If an existing main database file is opened, then the
127560 ** default text encoding for the existing database is used.
127561 **
127562 ** In all cases new databases created using the ATTACH command are
127563 ** created to use the same default text encoding as the main database. If
127564 ** the main database has not been initialized and/or created when ATTACH
127565 ** is executed, this is done before the ATTACH operation.
127566 **
127567 ** In the second form this pragma sets the text encoding to be used in
127568 ** new database files created using this database handle. It is only
127569 ** useful if invoked immediately after the main database i
127570 */
127571 case PragTyp_ENCODING: {
127572 static const struct EncName {
127573 char *zName;
127574 u8 enc;
127575 } encnames[] = {
127576 { "UTF8", SQLITE_UTF8 },
127577 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
127578 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
127579 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
127580 { "UTF16le", SQLITE_UTF16LE },
127581 { "UTF16be", SQLITE_UTF16BE },
127582 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
127583 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
127584 { 0, 0 }
127585 };
127586 const struct EncName *pEnc;
127587 if( !zRight ){ /* "PRAGMA encoding" */
127588 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
127589 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
127590 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
127591 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
127592 returnSingleText(v, encnames[ENC(pParse->db)].zName);
127593 }else{ /* "PRAGMA encoding = XXX" */
127594 /* Only change the value of sqlite.enc if the database handle is not
127595 ** initialized. If the main database exists, the new sqlite.enc value
127596 ** will be overwritten when the schema is next loaded. If it does not
127597 ** already exists, it will be created to use the new encoding value.
127598 */
127599 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
127600 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
127601 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
127602 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
127603 SCHEMA_ENC(db) = enc;
127604 sqlite3SetTextEncoding(db, enc);
127605 break;
127606 }
127607 }
127608 if( !pEnc->zName ){
127609 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
127610 }
127611 }
127612 }
127613 }
127614 break;
127615#endif /* SQLITE_OMIT_UTF16 */
127616
127617#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
127618 /*
127619 ** PRAGMA [schema.]schema_version
127620 ** PRAGMA [schema.]schema_version = <integer>
127621 **
127622 ** PRAGMA [schema.]user_version
127623 ** PRAGMA [schema.]user_version = <integer>
127624 **
127625 ** PRAGMA [schema.]freelist_count
127626 **
127627 ** PRAGMA [schema.]data_version
127628 **
127629 ** PRAGMA [schema.]application_id
127630 ** PRAGMA [schema.]application_id = <integer>
127631 **
127632 ** The pragma's schema_version and user_version are used to set or get
127633 ** the value of the schema-version and user-version, respectively. Both
127634 ** the schema-version and the user-version are 32-bit signed integers
127635 ** stored in the database header.
127636 **
127637 ** The schema-cookie is usually only manipulated internally by SQLite. It
127638 ** is incremented by SQLite whenever the database schema is modified (by
127639 ** creating or dropping a table or index). The schema version is used by
127640 ** SQLite each time a query is executed to ensure that the internal cache
127641 ** of the schema used when compiling the SQL query matches the schema of
127642 ** the database against which the compiled query is actually executed.
127643 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
127644 ** the schema-version is potentially dangerous and may lead to program
127645 ** crashes or database corruption. Use with caution!
127646 **
127647 ** The user-version is not used internally by SQLite. It may be used by
127648 ** applications for any purpose.
127649 */
127650 case PragTyp_HEADER_VALUE: {
127651 int iCookie = pPragma->iArg; /* Which cookie to read or write */
127652 sqlite3VdbeUsesBtree(v, iDb);
127653 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
127654 /* Write the specified cookie value */
127655 static const VdbeOpList setCookie[] = {
127656 { OP_Transaction, 0, 1, 0}, /* 0 */
127657 { OP_SetCookie, 0, 0, 0}, /* 1 */
127658 };
127659 VdbeOp *aOp;
127660 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
127661 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
127662 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
127663 aOp[0].p1 = iDb;
127664 aOp[1].p1 = iDb;
127665 aOp[1].p2 = iCookie;
127666 aOp[1].p3 = sqlite3Atoi(zRight);
127667 aOp[1].p5 = 1;
127668 }else{
127669 /* Read the specified cookie value */
127670 static const VdbeOpList readCookie[] = {
127671 { OP_Transaction, 0, 0, 0}, /* 0 */
127672 { OP_ReadCookie, 0, 1, 0}, /* 1 */
127673 { OP_ResultRow, 1, 1, 0}
127674 };
127675 VdbeOp *aOp;
127676 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
127677 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
127678 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
127679 aOp[0].p1 = iDb;
127680 aOp[1].p1 = iDb;
127681 aOp[1].p3 = iCookie;
127682 sqlite3VdbeReusable(v);
127683 }
127684 }
127685 break;
127686#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
127687
127688#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
127689 /*
127690 ** PRAGMA compile_options
127691 **
127692 ** Return the names of all compile-time options used in this build,
127693 ** one option per row.
127694 */
127695 case PragTyp_COMPILE_OPTIONS: {
127696 int i = 0;
127697 const char *zOpt;
127698 pParse->nMem = 1;
127699 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
127700 sqlite3VdbeLoadString(v, 1, zOpt);
127701 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
127702 }
127703 sqlite3VdbeReusable(v);
127704 }
127705 break;
127706#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
127707
127708#ifndef SQLITE_OMIT_WAL
127709 /*
127710 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
127711 **
127712 ** Checkpoint the database.
127713 */
127714 case PragTyp_WAL_CHECKPOINT: {
127715 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
127716 int eMode = SQLITE_CHECKPOINT_PASSIVE;
127717 if( zRight ){
127718 if( sqlite3StrICmp(zRight, "full")==0 ){
127719 eMode = SQLITE_CHECKPOINT_FULL;
127720 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
127721 eMode = SQLITE_CHECKPOINT_RESTART;
127722 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
127723 eMode = SQLITE_CHECKPOINT_TRUNCATE;
127724 }
127725 }
127726 pParse->nMem = 3;
127727 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
127728 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
127729 }
127730 break;
127731
127732 /*
127733 ** PRAGMA wal_autocheckpoint
127734 ** PRAGMA wal_autocheckpoint = N
127735 **
127736 ** Configure a database connection to automatically checkpoint a database
127737 ** after accumulating N frames in the log. Or query for the current value
127738 ** of N.
127739 */
127740 case PragTyp_WAL_AUTOCHECKPOINT: {
127741 if( zRight ){
127742 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
127743 }
127744 returnSingleInt(v,
127745 db->xWalCallback==sqlite3WalDefaultHook ?
127746 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
127747 }
127748 break;
127749#endif
127750
127751 /*
127752 ** PRAGMA shrink_memory
127753 **
127754 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
127755 ** connection on which it is invoked to free up as much memory as it
127756 ** can, by calling sqlite3_db_release_memory().
127757 */
127758 case PragTyp_SHRINK_MEMORY: {
127759 sqlite3_db_release_memory(db);
127760 break;
127761 }
127762
127763 /*
127764 ** PRAGMA optimize
127765 ** PRAGMA optimize(MASK)
127766 ** PRAGMA schema.optimize
127767 ** PRAGMA schema.optimize(MASK)
127768 **
127769 ** Attempt to optimize the database. All schemas are optimized in the first
127770 ** two forms, and only the specified schema is optimized in the latter two.
127771 **
127772 ** The details of optimizations performed by this pragma are expected
127773 ** to change and improve over time. Applications should anticipate that
127774 ** this pragma will perform new optimizations in future releases.
127775 **
127776 ** The optional argument is a bitmask of optimizations to perform:
127777 **
127778 ** 0x0001 Debugging mode. Do not actually perform any optimizations
127779 ** but instead return one line of text for each optimization
127780 ** that would have been done. Off by default.
127781 **
127782 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
127783 ** See below for additional information.
127784 **
127785 ** 0x0004 (Not yet implemented) Record usage and performance
127786 ** information from the current session in the
127787 ** database file so that it will be available to "optimize"
127788 ** pragmas run by future database connections.
127789 **
127790 ** 0x0008 (Not yet implemented) Create indexes that might have
127791 ** been helpful to recent queries
127792 **
127793 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
127794 ** of the optimizations listed above except Debug Mode, including new
127795 ** optimizations that have not yet been invented. If new optimizations are
127796 ** ever added that should be off by default, those off-by-default
127797 ** optimizations will have bitmasks of 0x10000 or larger.
127798 **
127799 ** DETERMINATION OF WHEN TO RUN ANALYZE
127800 **
127801 ** In the current implementation, a table is analyzed if only if all of
127802 ** the following are true:
127803 **
127804 ** (1) MASK bit 0x02 is set.
127805 **
127806 ** (2) The query planner used sqlite_stat1-style statistics for one or
127807 ** more indexes of the table at some point during the lifetime of
127808 ** the current connection.
127809 **
127810 ** (3) One or more indexes of the table are currently unanalyzed OR
127811 ** the number of rows in the table has increased by 25 times or more
127812 ** since the last time ANALYZE was run.
127813 **
127814 ** The rules for when tables are analyzed are likely to change in
127815 ** future releases.
127816 */
127817 case PragTyp_OPTIMIZE: {
127818 int iDbLast; /* Loop termination point for the schema loop */
127819 int iTabCur; /* Cursor for a table whose size needs checking */
127820 HashElem *k; /* Loop over tables of a schema */
127821 Schema *pSchema; /* The current schema */
127822 Table *pTab; /* A table in the schema */
127823 Index *pIdx; /* An index of the table */
127824 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
127825 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
127826 u32 opMask; /* Mask of operations to perform */
127827
127828 if( zRight ){
127829 opMask = (u32)sqlite3Atoi(zRight);
127830 if( (opMask & 0x02)==0 ) break;
127831 }else{
127832 opMask = 0xfffe;
127833 }
127834 iTabCur = pParse->nTab++;
127835 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
127836 if( iDb==1 ) continue;
127837 sqlite3CodeVerifySchema(pParse, iDb);
127838 pSchema = db->aDb[iDb].pSchema;
127839 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
127840 pTab = (Table*)sqliteHashData(k);
127841
127842 /* If table pTab has not been used in a way that would benefit from
127843 ** having analysis statistics during the current session, then skip it.
127844 ** This also has the effect of skipping virtual tables and views */
127845 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
127846
127847 /* Reanalyze if the table is 25 times larger than the last analysis */
127848 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
127849 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127850 if( !pIdx->hasStat1 ){
127851 szThreshold = 0; /* Always analyze if any index lacks statistics */
127852 break;
127853 }
127854 }
127855 if( szThreshold ){
127856 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
127857 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
127858 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
127859 VdbeCoverage(v);
127860 }
127861 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
127862 db->aDb[iDb].zDbSName, pTab->zName);
127863 if( opMask & 0x01 ){
127864 int r1 = sqlite3GetTempReg(pParse);
127865 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
127866 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
127867 }else{
127868 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
127869 }
127870 }
127871 }
127872 sqlite3VdbeAddOp0(v, OP_Expire);
127873 break;
127874 }
127875
127876 /*
127877 ** PRAGMA busy_timeout
127878 ** PRAGMA busy_timeout = N
127879 **
127880 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
127881 ** if one is set. If no busy handler or a different busy handler is set
127882 ** then 0 is returned. Setting the busy_timeout to 0 or negative
127883 ** disables the timeout.
127884 */
127885 /*case PragTyp_BUSY_TIMEOUT*/ default: {
127886 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
127887 if( zRight ){
127888 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
127889 }
127890 returnSingleInt(v, db->busyTimeout);
127891 break;
127892 }
127893
127894 /*
127895 ** PRAGMA soft_heap_limit
127896 ** PRAGMA soft_heap_limit = N
127897 **
127898 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
127899 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
127900 ** specified and is a non-negative integer.
127901 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
127902 ** returns the same integer that would be returned by the
127903 ** sqlite3_soft_heap_limit64(-1) C-language function.
127904 */
127905 case PragTyp_SOFT_HEAP_LIMIT: {
127906 sqlite3_int64 N;
127907 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
127908 sqlite3_soft_heap_limit64(N);
127909 }
127910 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
127911 break;
127912 }
127913
127914 /*
127915 ** PRAGMA hard_heap_limit
127916 ** PRAGMA hard_heap_limit = N
127917 **
127918 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
127919 ** limit. The hard heap limit can be activated or lowered by this
127920 ** pragma, but not raised or deactivated. Only the
127921 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
127922 ** the hard heap limit. This allows an application to set a heap limit
127923 ** constraint that cannot be relaxed by an untrusted SQL script.
127924 */
127925 case PragTyp_HARD_HEAP_LIMIT: {
127926 sqlite3_int64 N;
127927 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
127928 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
127929 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
127930 }
127931 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
127932 break;
127933 }
127934
127935 /*
127936 ** PRAGMA threads
127937 ** PRAGMA threads = N
127938 **
127939 ** Configure the maximum number of worker threads. Return the new
127940 ** maximum, which might be less than requested.
127941 */
127942 case PragTyp_THREADS: {
127943 sqlite3_int64 N;
127944 if( zRight
127945 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
127946 && N>=0
127947 ){
127948 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
127949 }
127950 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
127951 break;
127952 }
127953
127954 /*
127955 ** PRAGMA analysis_limit
127956 ** PRAGMA analysis_limit = N
127957 **
127958 ** Configure the maximum number of rows that ANALYZE will examine
127959 ** in each index that it looks at. Return the new limit.
127960 */
127961 case PragTyp_ANALYSIS_LIMIT: {
127962 sqlite3_int64 N;
127963 if( zRight
127964 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
127965 && N>=0
127966 ){
127967 db->nAnalysisLimit = (int)(N&0x7fffffff);
127968 }
127969 returnSingleInt(v, db->nAnalysisLimit);
127970 break;
127971 }
127972
127973#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
127974 /*
127975 ** Report the current state of file logs for all databases
127976 */
127977 case PragTyp_LOCK_STATUS: {
127978 static const char *const azLockName[] = {
127979 "unlocked", "shared", "reserved", "pending", "exclusive"
127980 };
127981 int i;
127982 pParse->nMem = 2;
127983 for(i=0; i<db->nDb; i++){
127984 Btree *pBt;
127985 const char *zState = "unknown";
127986 int j;
127987 if( db->aDb[i].zDbSName==0 ) continue;
127988 pBt = db->aDb[i].pBt;
127989 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
127990 zState = "closed";
127991 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
127992 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
127993 zState = azLockName[j];
127994 }
127995 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
127996 }
127997 break;
127998 }
127999#endif
128000
128001#if defined(SQLITE_ENABLE_CEROD)
128002 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
128003 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
128004 sqlite3_activate_cerod(&zRight[6]);
128005 }
128006 }
128007 break;
128008#endif
128009
128010 } /* End of the PRAGMA switch */
128011
128012 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
128013 ** purpose is to execute assert() statements to verify that if the
128014 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
128015 ** to the PRAGMA, the implementation has not added any OP_ResultRow
128016 ** instructions to the VM. */
128017 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
128018 sqlite3VdbeVerifyNoResultRow(v);
128019 }
128020
128021pragma_out:
128022 sqlite3DbFree(db, zLeft);
128023 sqlite3DbFree(db, zRight);
128024}
128025#ifndef SQLITE_OMIT_VIRTUALTABLE
128026/*****************************************************************************
128027** Implementation of an eponymous virtual table that runs a pragma.
128028**
128029*/
128030typedef struct PragmaVtab PragmaVtab;
128031typedef struct PragmaVtabCursor PragmaVtabCursor;
128032struct PragmaVtab {
128033 sqlite3_vtab base; /* Base class. Must be first */
128034 sqlite3 *db; /* The database connection to which it belongs */
128035 const PragmaName *pName; /* Name of the pragma */
128036 u8 nHidden; /* Number of hidden columns */
128037 u8 iHidden; /* Index of the first hidden column */
128038};
128039struct PragmaVtabCursor {
128040 sqlite3_vtab_cursor base; /* Base class. Must be first */
128041 sqlite3_stmt *pPragma; /* The pragma statement to run */
128042 sqlite_int64 iRowid; /* Current rowid */
128043 char *azArg[2]; /* Value of the argument and schema */
128044};
128045
128046/*
128047** Pragma virtual table module xConnect method.
128048*/
128049static int pragmaVtabConnect(
128050 sqlite3 *db,
128051 void *pAux,
128052 int argc, const char *const*argv,
128053 sqlite3_vtab **ppVtab,
128054 char **pzErr
128055){
128056 const PragmaName *pPragma = (const PragmaName*)pAux;
128057 PragmaVtab *pTab = 0;
128058 int rc;
128059 int i, j;
128060 char cSep = '(';
128061 StrAccum acc;
128062 char zBuf[200];
128063
128064 UNUSED_PARAMETER(argc);
128065 UNUSED_PARAMETER(argv);
128066 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
128067 sqlite3_str_appendall(&acc, "CREATE TABLE x");
128068 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
128069 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
128070 cSep = ',';
128071 }
128072 if( i==0 ){
128073 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
128074 i++;
128075 }
128076 j = 0;
128077 if( pPragma->mPragFlg & PragFlg_Result1 ){
128078 sqlite3_str_appendall(&acc, ",arg HIDDEN");
128079 j++;
128080 }
128081 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
128082 sqlite3_str_appendall(&acc, ",schema HIDDEN");
128083 j++;
128084 }
128085 sqlite3_str_append(&acc, ")", 1);
128086 sqlite3StrAccumFinish(&acc);
128087 assert( strlen(zBuf) < sizeof(zBuf)-1 );
128088 rc = sqlite3_declare_vtab(db, zBuf);
128089 if( rc==SQLITE_OK ){
128090 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
128091 if( pTab==0 ){
128092 rc = SQLITE_NOMEM;
128093 }else{
128094 memset(pTab, 0, sizeof(PragmaVtab));
128095 pTab->pName = pPragma;
128096 pTab->db = db;
128097 pTab->iHidden = i;
128098 pTab->nHidden = j;
128099 }
128100 }else{
128101 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
128102 }
128103
128104 *ppVtab = (sqlite3_vtab*)pTab;
128105 return rc;
128106}
128107
128108/*
128109** Pragma virtual table module xDisconnect method.
128110*/
128111static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
128112 PragmaVtab *pTab = (PragmaVtab*)pVtab;
128113 sqlite3_free(pTab);
128114 return SQLITE_OK;
128115}
128116
128117/* Figure out the best index to use to search a pragma virtual table.
128118**
128119** There are not really any index choices. But we want to encourage the
128120** query planner to give == constraints on as many hidden parameters as
128121** possible, and especially on the first hidden parameter. So return a
128122** high cost if hidden parameters are unconstrained.
128123*/
128124static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
128125 PragmaVtab *pTab = (PragmaVtab*)tab;
128126 const struct sqlite3_index_constraint *pConstraint;
128127 int i, j;
128128 int seen[2];
128129
128130 pIdxInfo->estimatedCost = (double)1;
128131 if( pTab->nHidden==0 ){ return SQLITE_OK; }
128132 pConstraint = pIdxInfo->aConstraint;
128133 seen[0] = 0;
128134 seen[1] = 0;
128135 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
128136 if( pConstraint->usable==0 ) continue;
128137 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
128138 if( pConstraint->iColumn < pTab->iHidden ) continue;
128139 j = pConstraint->iColumn - pTab->iHidden;
128140 assert( j < 2 );
128141 seen[j] = i+1;
128142 }
128143 if( seen[0]==0 ){
128144 pIdxInfo->estimatedCost = (double)2147483647;
128145 pIdxInfo->estimatedRows = 2147483647;
128146 return SQLITE_OK;
128147 }
128148 j = seen[0]-1;
128149 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
128150 pIdxInfo->aConstraintUsage[j].omit = 1;
128151 if( seen[1]==0 ) return SQLITE_OK;
128152 pIdxInfo->estimatedCost = (double)20;
128153 pIdxInfo->estimatedRows = 20;
128154 j = seen[1]-1;
128155 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
128156 pIdxInfo->aConstraintUsage[j].omit = 1;
128157 return SQLITE_OK;
128158}
128159
128160/* Create a new cursor for the pragma virtual table */
128161static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
128162 PragmaVtabCursor *pCsr;
128163 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
128164 if( pCsr==0 ) return SQLITE_NOMEM;
128165 memset(pCsr, 0, sizeof(PragmaVtabCursor));
128166 pCsr->base.pVtab = pVtab;
128167 *ppCursor = &pCsr->base;
128168 return SQLITE_OK;
128169}
128170
128171/* Clear all content from pragma virtual table cursor. */
128172static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
128173 int i;
128174 sqlite3_finalize(pCsr->pPragma);
128175 pCsr->pPragma = 0;
128176 for(i=0; i<ArraySize(pCsr->azArg); i++){
128177 sqlite3_free(pCsr->azArg[i]);
128178 pCsr->azArg[i] = 0;
128179 }
128180}
128181
128182/* Close a pragma virtual table cursor */
128183static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
128184 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
128185 pragmaVtabCursorClear(pCsr);
128186 sqlite3_free(pCsr);
128187 return SQLITE_OK;
128188}
128189
128190/* Advance the pragma virtual table cursor to the next row */
128191static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
128192 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
128193 int rc = SQLITE_OK;
128194
128195 /* Increment the xRowid value */
128196 pCsr->iRowid++;
128197 assert( pCsr->pPragma );
128198 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
128199 rc = sqlite3_finalize(pCsr->pPragma);
128200 pCsr->pPragma = 0;
128201 pragmaVtabCursorClear(pCsr);
128202 }
128203 return rc;
128204}
128205
128206/*
128207** Pragma virtual table module xFilter method.
128208*/
128209static int pragmaVtabFilter(
128210 sqlite3_vtab_cursor *pVtabCursor,
128211 int idxNum, const char *idxStr,
128212 int argc, sqlite3_value **argv
128213){
128214 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
128215 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
128216 int rc;
128217 int i, j;
128218 StrAccum acc;
128219 char *zSql;
128220
128221 UNUSED_PARAMETER(idxNum);
128222 UNUSED_PARAMETER(idxStr);
128223 pragmaVtabCursorClear(pCsr);
128224 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
128225 for(i=0; i<argc; i++, j++){
128226 const char *zText = (const char*)sqlite3_value_text(argv[i]);
128227 assert( j<ArraySize(pCsr->azArg) );
128228 assert( pCsr->azArg[j]==0 );
128229 if( zText ){
128230 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
128231 if( pCsr->azArg[j]==0 ){
128232 return SQLITE_NOMEM;
128233 }
128234 }
128235 }
128236 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
128237 sqlite3_str_appendall(&acc, "PRAGMA ");
128238 if( pCsr->azArg[1] ){
128239 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
128240 }
128241 sqlite3_str_appendall(&acc, pTab->pName->zName);
128242 if( pCsr->azArg[0] ){
128243 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
128244 }
128245 zSql = sqlite3StrAccumFinish(&acc);
128246 if( zSql==0 ) return SQLITE_NOMEM;
128247 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
128248 sqlite3_free(zSql);
128249 if( rc!=SQLITE_OK ){
128250 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
128251 return rc;
128252 }
128253 return pragmaVtabNext(pVtabCursor);
128254}
128255
128256/*
128257** Pragma virtual table module xEof method.
128258*/
128259static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
128260 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
128261 return (pCsr->pPragma==0);
128262}
128263
128264/* The xColumn method simply returns the corresponding column from
128265** the PRAGMA.
128266*/
128267static int pragmaVtabColumn(
128268 sqlite3_vtab_cursor *pVtabCursor,
128269 sqlite3_context *ctx,
128270 int i
128271){
128272 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
128273 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
128274 if( i<pTab->iHidden ){
128275 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
128276 }else{
128277 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
128278 }
128279 return SQLITE_OK;
128280}
128281
128282/*
128283** Pragma virtual table module xRowid method.
128284*/
128285static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
128286 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
128287 *p = pCsr->iRowid;
128288 return SQLITE_OK;
128289}
128290
128291/* The pragma virtual table object */
128292static const sqlite3_module pragmaVtabModule = {
128293 0, /* iVersion */
128294 0, /* xCreate - create a table */
128295 pragmaVtabConnect, /* xConnect - connect to an existing table */
128296 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
128297 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
128298 0, /* xDestroy - Drop a table */
128299 pragmaVtabOpen, /* xOpen - open a cursor */
128300 pragmaVtabClose, /* xClose - close a cursor */
128301 pragmaVtabFilter, /* xFilter - configure scan constraints */
128302 pragmaVtabNext, /* xNext - advance a cursor */
128303 pragmaVtabEof, /* xEof */
128304 pragmaVtabColumn, /* xColumn - read data */
128305 pragmaVtabRowid, /* xRowid - read data */
128306 0, /* xUpdate - write data */
128307 0, /* xBegin - begin transaction */
128308 0, /* xSync - sync transaction */
128309 0, /* xCommit - commit transaction */
128310 0, /* xRollback - rollback transaction */
128311 0, /* xFindFunction - function overloading */
128312 0, /* xRename - rename the table */
128313 0, /* xSavepoint */
128314 0, /* xRelease */
128315 0, /* xRollbackTo */
128316 0 /* xShadowName */
128317};
128318
128319/*
128320** Check to see if zTabName is really the name of a pragma. If it is,
128321** then register an eponymous virtual table for that pragma and return
128322** a pointer to the Module object for the new virtual table.
128323*/
128324SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
128325 const PragmaName *pName;
128326 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
128327 pName = pragmaLocate(zName+7);
128328 if( pName==0 ) return 0;
128329 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
128330 assert( sqlite3HashFind(&db->aModule, zName)==0 );
128331 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
128332}
128333
128334#endif /* SQLITE_OMIT_VIRTUALTABLE */
128335
128336#endif /* SQLITE_OMIT_PRAGMA */
128337
128338/************** End of pragma.c **********************************************/
128339/************** Begin file prepare.c *****************************************/
128340/*
128341** 2005 May 25
128342**
128343** The author disclaims copyright to this source code. In place of
128344** a legal notice, here is a blessing:
128345**
128346** May you do good and not evil.
128347** May you find forgiveness for yourself and forgive others.
128348** May you share freely, never taking more than you give.
128349**
128350*************************************************************************
128351** This file contains the implementation of the sqlite3_prepare()
128352** interface, and routines that contribute to loading the database schema
128353** from disk.
128354*/
128355/* #include "sqliteInt.h" */
128356
128357/*
128358** Fill the InitData structure with an error message that indicates
128359** that the database is corrupt.
128360*/
128361static void corruptSchema(
128362 InitData *pData, /* Initialization context */
128363 const char *zObj, /* Object being parsed at the point of error */
128364 const char *zExtra /* Error information */
128365){
128366 sqlite3 *db = pData->db;
128367 if( db->mallocFailed ){
128368 pData->rc = SQLITE_NOMEM_BKPT;
128369 }else if( pData->pzErrMsg[0]!=0 ){
128370 /* A error message has already been generated. Do not overwrite it */
128371 }else if( pData->mInitFlags & INITFLAG_AlterTable ){
128372 *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
128373 pData->rc = SQLITE_ERROR;
128374 }else if( db->flags & SQLITE_WriteSchema ){
128375 pData->rc = SQLITE_CORRUPT_BKPT;
128376 }else{
128377 char *z;
128378 if( zObj==0 ) zObj = "?";
128379 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
128380 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
128381 *pData->pzErrMsg = z;
128382 pData->rc = SQLITE_CORRUPT_BKPT;
128383 }
128384}
128385
128386/*
128387** Check to see if any sibling index (another index on the same table)
128388** of pIndex has the same root page number, and if it does, return true.
128389** This would indicate a corrupt schema.
128390*/
128391SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
128392 Index *p;
128393 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
128394 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
128395 }
128396 return 0;
128397}
128398
128399/* forward declaration */
128400static int sqlite3Prepare(
128401 sqlite3 *db, /* Database handle. */
128402 const char *zSql, /* UTF-8 encoded SQL statement. */
128403 int nBytes, /* Length of zSql in bytes. */
128404 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
128405 Vdbe *pReprepare, /* VM being reprepared */
128406 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
128407 const char **pzTail /* OUT: End of parsed string */
128408);
128409
128410
128411/*
128412** This is the callback routine for the code that initializes the
128413** database. See sqlite3Init() below for additional information.
128414** This routine is also called from the OP_ParseSchema opcode of the VDBE.
128415**
128416** Each callback contains the following information:
128417**
128418** argv[0] = type of object: "table", "index", "trigger", or "view".
128419** argv[1] = name of thing being created
128420** argv[2] = associated table if an index or trigger
128421** argv[3] = root page number for table or index. 0 for trigger or view.
128422** argv[4] = SQL text for the CREATE statement.
128423**
128424*/
128425SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
128426 InitData *pData = (InitData*)pInit;
128427 sqlite3 *db = pData->db;
128428 int iDb = pData->iDb;
128429
128430 assert( argc==5 );
128431 UNUSED_PARAMETER2(NotUsed, argc);
128432 assert( sqlite3_mutex_held(db->mutex) );
128433 db->mDbFlags |= DBFLAG_EncodingFixed;
128434 pData->nInitRow++;
128435 if( db->mallocFailed ){
128436 corruptSchema(pData, argv[1], 0);
128437 return 1;
128438 }
128439
128440 assert( iDb>=0 && iDb<db->nDb );
128441 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
128442 if( argv[3]==0 ){
128443 corruptSchema(pData, argv[1], 0);
128444 }else if( sqlite3_strnicmp(argv[4],"create ",7)==0 ){
128445 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
128446 ** But because db->init.busy is set to 1, no VDBE code is generated
128447 ** or executed. All the parser does is build the internal data
128448 ** structures that describe the table, index, or view.
128449 */
128450 int rc;
128451 u8 saved_iDb = db->init.iDb;
128452 sqlite3_stmt *pStmt;
128453 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
128454
128455 assert( db->init.busy );
128456 db->init.iDb = iDb;
128457 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
128458 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
128459 ){
128460 if( sqlite3Config.bExtraSchemaChecks ){
128461 corruptSchema(pData, argv[1], "invalid rootpage");
128462 }
128463 }
128464 db->init.orphanTrigger = 0;
128465 db->init.azInit = argv;
128466 pStmt = 0;
128467 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
128468 rc = db->errCode;
128469 assert( (rc&0xFF)==(rcp&0xFF) );
128470 db->init.iDb = saved_iDb;
128471 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
128472 if( SQLITE_OK!=rc ){
128473 if( db->init.orphanTrigger ){
128474 assert( iDb==1 );
128475 }else{
128476 if( rc > pData->rc ) pData->rc = rc;
128477 if( rc==SQLITE_NOMEM ){
128478 sqlite3OomFault(db);
128479 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
128480 corruptSchema(pData, argv[1], sqlite3_errmsg(db));
128481 }
128482 }
128483 }
128484 sqlite3_finalize(pStmt);
128485 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
128486 corruptSchema(pData, argv[1], 0);
128487 }else{
128488 /* If the SQL column is blank it means this is an index that
128489 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
128490 ** constraint for a CREATE TABLE. The index should have already
128491 ** been created when we processed the CREATE TABLE. All we have
128492 ** to do here is record the root page number for that index.
128493 */
128494 Index *pIndex;
128495 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
128496 if( pIndex==0 ){
128497 corruptSchema(pData, argv[1], "orphan index");
128498 }else
128499 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
128500 || pIndex->tnum<2
128501 || pIndex->tnum>pData->mxPage
128502 || sqlite3IndexHasDuplicateRootPage(pIndex)
128503 ){
128504 if( sqlite3Config.bExtraSchemaChecks ){
128505 corruptSchema(pData, argv[1], "invalid rootpage");
128506 }
128507 }
128508 }
128509 return 0;
128510}
128511
128512/*
128513** Attempt to read the database schema and initialize internal
128514** data structures for a single database file. The index of the
128515** database file is given by iDb. iDb==0 is used for the main
128516** database. iDb==1 should never be used. iDb>=2 is used for
128517** auxiliary databases. Return one of the SQLITE_ error codes to
128518** indicate success or failure.
128519*/
128520SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
128521 int rc;
128522 int i;
128523#ifndef SQLITE_OMIT_DEPRECATED
128524 int size;
128525#endif
128526 Db *pDb;
128527 char const *azArg[6];
128528 int meta[5];
128529 InitData initData;
128530 const char *zSchemaTabName;
128531 int openedTransaction = 0;
128532 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
128533
128534 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
128535 assert( iDb>=0 && iDb<db->nDb );
128536 assert( db->aDb[iDb].pSchema );
128537 assert( sqlite3_mutex_held(db->mutex) );
128538 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
128539
128540 db->init.busy = 1;
128541
128542 /* Construct the in-memory representation schema tables (sqlite_schema or
128543 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
128544 ** table name will be inserted automatically by the parser so we can just
128545 ** use the abbreviation "x" here. The parser will also automatically tag
128546 ** the schema table as read-only. */
128547 azArg[0] = "table";
128548 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
128549 azArg[2] = azArg[1];
128550 azArg[3] = "1";
128551 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
128552 "rootpage int,sql text)";
128553 azArg[5] = 0;
128554 initData.db = db;
128555 initData.iDb = iDb;
128556 initData.rc = SQLITE_OK;
128557 initData.pzErrMsg = pzErrMsg;
128558 initData.mInitFlags = mFlags;
128559 initData.nInitRow = 0;
128560 initData.mxPage = 0;
128561 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
128562 db->mDbFlags &= mask;
128563 if( initData.rc ){
128564 rc = initData.rc;
128565 goto error_out;
128566 }
128567
128568 /* Create a cursor to hold the database open
128569 */
128570 pDb = &db->aDb[iDb];
128571 if( pDb->pBt==0 ){
128572 assert( iDb==1 );
128573 DbSetProperty(db, 1, DB_SchemaLoaded);
128574 rc = SQLITE_OK;
128575 goto error_out;
128576 }
128577
128578 /* If there is not already a read-only (or read-write) transaction opened
128579 ** on the b-tree database, open one now. If a transaction is opened, it
128580 ** will be closed before this function returns. */
128581 sqlite3BtreeEnter(pDb->pBt);
128582 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
128583 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
128584 if( rc!=SQLITE_OK ){
128585 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
128586 goto initone_error_out;
128587 }
128588 openedTransaction = 1;
128589 }
128590
128591 /* Get the database meta information.
128592 **
128593 ** Meta values are as follows:
128594 ** meta[0] Schema cookie. Changes with each schema change.
128595 ** meta[1] File format of schema layer.
128596 ** meta[2] Size of the page cache.
128597 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
128598 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
128599 ** meta[5] User version
128600 ** meta[6] Incremental vacuum mode
128601 ** meta[7] unused
128602 ** meta[8] unused
128603 ** meta[9] unused
128604 **
128605 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
128606 ** the possible values of meta[4].
128607 */
128608 for(i=0; i<ArraySize(meta); i++){
128609 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
128610 }
128611 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
128612 memset(meta, 0, sizeof(meta));
128613 }
128614 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
128615
128616 /* If opening a non-empty database, check the text encoding. For the
128617 ** main database, set sqlite3.enc to the encoding of the main database.
128618 ** For an attached db, it is an error if the encoding is not the same
128619 ** as sqlite3.enc.
128620 */
128621 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
128622 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
128623 u8 encoding;
128624#ifndef SQLITE_OMIT_UTF16
128625 /* If opening the main database, set ENC(db). */
128626 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
128627 if( encoding==0 ) encoding = SQLITE_UTF8;
128628#else
128629 encoding = SQLITE_UTF8;
128630#endif
128631 sqlite3SetTextEncoding(db, encoding);
128632 }else{
128633 /* If opening an attached database, the encoding much match ENC(db) */
128634 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
128635 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
128636 " text encoding as main database");
128637 rc = SQLITE_ERROR;
128638 goto initone_error_out;
128639 }
128640 }
128641 }
128642 pDb->pSchema->enc = ENC(db);
128643
128644 if( pDb->pSchema->cache_size==0 ){
128645#ifndef SQLITE_OMIT_DEPRECATED
128646 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
128647 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
128648 pDb->pSchema->cache_size = size;
128649#else
128650 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
128651#endif
128652 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
128653 }
128654
128655 /*
128656 ** file_format==1 Version 3.0.0.
128657 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
128658 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
128659 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
128660 */
128661 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
128662 if( pDb->pSchema->file_format==0 ){
128663 pDb->pSchema->file_format = 1;
128664 }
128665 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
128666 sqlite3SetString(pzErrMsg, db, "unsupported file format");
128667 rc = SQLITE_ERROR;
128668 goto initone_error_out;
128669 }
128670
128671 /* Ticket #2804: When we open a database in the newer file format,
128672 ** clear the legacy_file_format pragma flag so that a VACUUM will
128673 ** not downgrade the database and thus invalidate any descending
128674 ** indices that the user might have created.
128675 */
128676 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
128677 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
128678 }
128679
128680 /* Read the schema information out of the schema tables
128681 */
128682 assert( db->init.busy );
128683 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
128684 {
128685 char *zSql;
128686 zSql = sqlite3MPrintf(db,
128687 "SELECT*FROM\"%w\".%s ORDER BY rowid",
128688 db->aDb[iDb].zDbSName, zSchemaTabName);
128689#ifndef SQLITE_OMIT_AUTHORIZATION
128690 {
128691 sqlite3_xauth xAuth;
128692 xAuth = db->xAuth;
128693 db->xAuth = 0;
128694#endif
128695 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
128696#ifndef SQLITE_OMIT_AUTHORIZATION
128697 db->xAuth = xAuth;
128698 }
128699#endif
128700 if( rc==SQLITE_OK ) rc = initData.rc;
128701 sqlite3DbFree(db, zSql);
128702#ifndef SQLITE_OMIT_ANALYZE
128703 if( rc==SQLITE_OK ){
128704 sqlite3AnalysisLoad(db, iDb);
128705 }
128706#endif
128707 }
128708 if( db->mallocFailed ){
128709 rc = SQLITE_NOMEM_BKPT;
128710 sqlite3ResetAllSchemasOfConnection(db);
128711 }
128712 if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
128713 /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
128714 ** the schema loaded, even if errors occurred. In this situation the
128715 ** current sqlite3_prepare() operation will fail, but the following one
128716 ** will attempt to compile the supplied statement against whatever subset
128717 ** of the schema was loaded before the error occurred. The primary
128718 ** purpose of this is to allow access to the sqlite_schema table
128719 ** even when its contents have been corrupted.
128720 */
128721 DbSetProperty(db, iDb, DB_SchemaLoaded);
128722 rc = SQLITE_OK;
128723 }
128724
128725 /* Jump here for an error that occurs after successfully allocating
128726 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
128727 ** before that point, jump to error_out.
128728 */
128729initone_error_out:
128730 if( openedTransaction ){
128731 sqlite3BtreeCommit(pDb->pBt);
128732 }
128733 sqlite3BtreeLeave(pDb->pBt);
128734
128735error_out:
128736 if( rc ){
128737 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
128738 sqlite3OomFault(db);
128739 }
128740 sqlite3ResetOneSchema(db, iDb);
128741 }
128742 db->init.busy = 0;
128743 return rc;
128744}
128745
128746/*
128747** Initialize all database files - the main database file, the file
128748** used to store temporary tables, and any additional database files
128749** created using ATTACH statements. Return a success code. If an
128750** error occurs, write an error message into *pzErrMsg.
128751**
128752** After a database is initialized, the DB_SchemaLoaded bit is set
128753** bit is set in the flags field of the Db structure.
128754*/
128755SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
128756 int i, rc;
128757 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
128758
128759 assert( sqlite3_mutex_held(db->mutex) );
128760 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
128761 assert( db->init.busy==0 );
128762 ENC(db) = SCHEMA_ENC(db);
128763 assert( db->nDb>0 );
128764 /* Do the main schema first */
128765 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
128766 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
128767 if( rc ) return rc;
128768 }
128769 /* All other schemas after the main schema. The "temp" schema must be last */
128770 for(i=db->nDb-1; i>0; i--){
128771 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
128772 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
128773 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
128774 if( rc ) return rc;
128775 }
128776 }
128777 if( commit_internal ){
128778 sqlite3CommitInternalChanges(db);
128779 }
128780 return SQLITE_OK;
128781}
128782
128783/*
128784** This routine is a no-op if the database schema is already initialized.
128785** Otherwise, the schema is loaded. An error code is returned.
128786*/
128787SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
128788 int rc = SQLITE_OK;
128789 sqlite3 *db = pParse->db;
128790 assert( sqlite3_mutex_held(db->mutex) );
128791 if( !db->init.busy ){
128792 rc = sqlite3Init(db, &pParse->zErrMsg);
128793 if( rc!=SQLITE_OK ){
128794 pParse->rc = rc;
128795 pParse->nErr++;
128796 }else if( db->noSharedCache ){
128797 db->mDbFlags |= DBFLAG_SchemaKnownOk;
128798 }
128799 }
128800 return rc;
128801}
128802
128803
128804/*
128805** Check schema cookies in all databases. If any cookie is out
128806** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
128807** make no changes to pParse->rc.
128808*/
128809static void schemaIsValid(Parse *pParse){
128810 sqlite3 *db = pParse->db;
128811 int iDb;
128812 int rc;
128813 int cookie;
128814
128815 assert( pParse->checkSchema );
128816 assert( sqlite3_mutex_held(db->mutex) );
128817 for(iDb=0; iDb<db->nDb; iDb++){
128818 int openedTransaction = 0; /* True if a transaction is opened */
128819 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
128820 if( pBt==0 ) continue;
128821
128822 /* If there is not already a read-only (or read-write) transaction opened
128823 ** on the b-tree database, open one now. If a transaction is opened, it
128824 ** will be closed immediately after reading the meta-value. */
128825 if( !sqlite3BtreeIsInReadTrans(pBt) ){
128826 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
128827 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
128828 sqlite3OomFault(db);
128829 }
128830 if( rc!=SQLITE_OK ) return;
128831 openedTransaction = 1;
128832 }
128833
128834 /* Read the schema cookie from the database. If it does not match the
128835 ** value stored as part of the in-memory schema representation,
128836 ** set Parse.rc to SQLITE_SCHEMA. */
128837 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
128838 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
128839 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
128840 sqlite3ResetOneSchema(db, iDb);
128841 pParse->rc = SQLITE_SCHEMA;
128842 }
128843
128844 /* Close the transaction, if one was opened. */
128845 if( openedTransaction ){
128846 sqlite3BtreeCommit(pBt);
128847 }
128848 }
128849}
128850
128851/*
128852** Convert a schema pointer into the iDb index that indicates
128853** which database file in db->aDb[] the schema refers to.
128854**
128855** If the same database is attached more than once, the first
128856** attached database is returned.
128857*/
128858SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
128859 int i = -32768;
128860
128861 /* If pSchema is NULL, then return -32768. This happens when code in
128862 ** expr.c is trying to resolve a reference to a transient table (i.e. one
128863 ** created by a sub-select). In this case the return value of this
128864 ** function should never be used.
128865 **
128866 ** We return -32768 instead of the more usual -1 simply because using
128867 ** -32768 as the incorrect index into db->aDb[] is much
128868 ** more likely to cause a segfault than -1 (of course there are assert()
128869 ** statements too, but it never hurts to play the odds) and
128870 ** -32768 will still fit into a 16-bit signed integer.
128871 */
128872 assert( sqlite3_mutex_held(db->mutex) );
128873 if( pSchema ){
128874 for(i=0; 1; i++){
128875 assert( i<db->nDb );
128876 if( db->aDb[i].pSchema==pSchema ){
128877 break;
128878 }
128879 }
128880 assert( i>=0 && i<db->nDb );
128881 }
128882 return i;
128883}
128884
128885/*
128886** Deallocate a single AggInfo object
128887*/
128888static void agginfoFree(sqlite3 *db, AggInfo *p){
128889 sqlite3DbFree(db, p->aCol);
128890 sqlite3DbFree(db, p->aFunc);
128891 sqlite3DbFree(db, p);
128892}
128893
128894/*
128895** Free all memory allocations in the pParse object
128896*/
128897SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
128898 sqlite3 *db = pParse->db;
128899 AggInfo *pThis = pParse->pAggList;
128900 while( pThis ){
128901 AggInfo *pNext = pThis->pNext;
128902 agginfoFree(db, pThis);
128903 pThis = pNext;
128904 }
128905 sqlite3DbFree(db, pParse->aLabel);
128906 sqlite3ExprListDelete(db, pParse->pConstExpr);
128907 if( db ){
128908 assert( db->lookaside.bDisable >= pParse->disableLookaside );
128909 db->lookaside.bDisable -= pParse->disableLookaside;
128910 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
128911 }
128912 pParse->disableLookaside = 0;
128913}
128914
128915/*
128916** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
128917*/
128918static int sqlite3Prepare(
128919 sqlite3 *db, /* Database handle. */
128920 const char *zSql, /* UTF-8 encoded SQL statement. */
128921 int nBytes, /* Length of zSql in bytes. */
128922 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
128923 Vdbe *pReprepare, /* VM being reprepared */
128924 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
128925 const char **pzTail /* OUT: End of parsed string */
128926){
128927 char *zErrMsg = 0; /* Error message */
128928 int rc = SQLITE_OK; /* Result code */
128929 int i; /* Loop counter */
128930 Parse sParse; /* Parsing context */
128931
128932 memset(&sParse, 0, PARSE_HDR_SZ);
128933 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
128934 sParse.pReprepare = pReprepare;
128935 assert( ppStmt && *ppStmt==0 );
128936 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
128937 assert( sqlite3_mutex_held(db->mutex) );
128938
128939 /* For a long-term use prepared statement avoid the use of
128940 ** lookaside memory.
128941 */
128942 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
128943 sParse.disableLookaside++;
128944 DisableLookaside;
128945 }
128946 sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
128947
128948 /* Check to verify that it is possible to get a read lock on all
128949 ** database schemas. The inability to get a read lock indicates that
128950 ** some other database connection is holding a write-lock, which in
128951 ** turn means that the other connection has made uncommitted changes
128952 ** to the schema.
128953 **
128954 ** Were we to proceed and prepare the statement against the uncommitted
128955 ** schema changes and if those schema changes are subsequently rolled
128956 ** back and different changes are made in their place, then when this
128957 ** prepared statement goes to run the schema cookie would fail to detect
128958 ** the schema change. Disaster would follow.
128959 **
128960 ** This thread is currently holding mutexes on all Btrees (because
128961 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
128962 ** is not possible for another thread to start a new schema change
128963 ** while this routine is running. Hence, we do not need to hold
128964 ** locks on the schema, we just need to make sure nobody else is
128965 ** holding them.
128966 **
128967 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
128968 ** but it does *not* override schema lock detection, so this all still
128969 ** works even if READ_UNCOMMITTED is set.
128970 */
128971 if( !db->noSharedCache ){
128972 for(i=0; i<db->nDb; i++) {
128973 Btree *pBt = db->aDb[i].pBt;
128974 if( pBt ){
128975 assert( sqlite3BtreeHoldsMutex(pBt) );
128976 rc = sqlite3BtreeSchemaLocked(pBt);
128977 if( rc ){
128978 const char *zDb = db->aDb[i].zDbSName;
128979 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
128980 testcase( db->flags & SQLITE_ReadUncommit );
128981 goto end_prepare;
128982 }
128983 }
128984 }
128985 }
128986
128987 sqlite3VtabUnlockList(db);
128988
128989 sParse.db = db;
128990 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
128991 char *zSqlCopy;
128992 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
128993 testcase( nBytes==mxLen );
128994 testcase( nBytes==mxLen+1 );
128995 if( nBytes>mxLen ){
128996 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
128997 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
128998 goto end_prepare;
128999 }
129000 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
129001 if( zSqlCopy ){
129002 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
129003 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
129004 sqlite3DbFree(db, zSqlCopy);
129005 }else{
129006 sParse.zTail = &zSql[nBytes];
129007 }
129008 }else{
129009 sqlite3RunParser(&sParse, zSql, &zErrMsg);
129010 }
129011 assert( 0==sParse.nQueryLoop );
129012
129013 if( sParse.rc==SQLITE_DONE ){
129014 sParse.rc = SQLITE_OK;
129015 }
129016 if( sParse.checkSchema ){
129017 schemaIsValid(&sParse);
129018 }
129019 if( pzTail ){
129020 *pzTail = sParse.zTail;
129021 }
129022
129023 if( db->init.busy==0 ){
129024 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
129025 }
129026 if( db->mallocFailed ){
129027 sParse.rc = SQLITE_NOMEM_BKPT;
129028 }
129029 rc = sParse.rc;
129030 if( rc!=SQLITE_OK ){
129031 if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
129032 assert(!(*ppStmt));
129033 }else{
129034 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
129035 }
129036
129037 if( zErrMsg ){
129038 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
129039 sqlite3DbFree(db, zErrMsg);
129040 }else{
129041 sqlite3Error(db, rc);
129042 }
129043
129044 /* Delete any TriggerPrg structures allocated while parsing this statement. */
129045 while( sParse.pTriggerPrg ){
129046 TriggerPrg *pT = sParse.pTriggerPrg;
129047 sParse.pTriggerPrg = pT->pNext;
129048 sqlite3DbFree(db, pT);
129049 }
129050
129051end_prepare:
129052
129053 sqlite3ParserReset(&sParse);
129054 return rc;
129055}
129056static int sqlite3LockAndPrepare(
129057 sqlite3 *db, /* Database handle. */
129058 const char *zSql, /* UTF-8 encoded SQL statement. */
129059 int nBytes, /* Length of zSql in bytes. */
129060 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
129061 Vdbe *pOld, /* VM being reprepared */
129062 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129063 const char **pzTail /* OUT: End of parsed string */
129064){
129065 int rc;
129066 int cnt = 0;
129067
129068#ifdef SQLITE_ENABLE_API_ARMOR
129069 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
129070#endif
129071 *ppStmt = 0;
129072 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
129073 return SQLITE_MISUSE_BKPT;
129074 }
129075 sqlite3_mutex_enter(db->mutex);
129076 sqlite3BtreeEnterAll(db);
129077 do{
129078 /* Make multiple attempts to compile the SQL, until it either succeeds
129079 ** or encounters a permanent error. A schema problem after one schema
129080 ** reset is considered a permanent error. */
129081 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
129082 assert( rc==SQLITE_OK || *ppStmt==0 );
129083 }while( rc==SQLITE_ERROR_RETRY
129084 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
129085 sqlite3BtreeLeaveAll(db);
129086 rc = sqlite3ApiExit(db, rc);
129087 assert( (rc&db->errMask)==rc );
129088 sqlite3_mutex_leave(db->mutex);
129089 return rc;
129090}
129091
129092
129093/*
129094** Rerun the compilation of a statement after a schema change.
129095**
129096** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
129097** if the statement cannot be recompiled because another connection has
129098** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
129099** occurs, return SQLITE_SCHEMA.
129100*/
129101SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
129102 int rc;
129103 sqlite3_stmt *pNew;
129104 const char *zSql;
129105 sqlite3 *db;
129106 u8 prepFlags;
129107
129108 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
129109 zSql = sqlite3_sql((sqlite3_stmt *)p);
129110 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
129111 db = sqlite3VdbeDb(p);
129112 assert( sqlite3_mutex_held(db->mutex) );
129113 prepFlags = sqlite3VdbePrepareFlags(p);
129114 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
129115 if( rc ){
129116 if( rc==SQLITE_NOMEM ){
129117 sqlite3OomFault(db);
129118 }
129119 assert( pNew==0 );
129120 return rc;
129121 }else{
129122 assert( pNew!=0 );
129123 }
129124 sqlite3VdbeSwap((Vdbe*)pNew, p);
129125 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
129126 sqlite3VdbeResetStepResult((Vdbe*)pNew);
129127 sqlite3VdbeFinalize((Vdbe*)pNew);
129128 return SQLITE_OK;
129129}
129130
129131
129132/*
129133** Two versions of the official API. Legacy and new use. In the legacy
129134** version, the original SQL text is not saved in the prepared statement
129135** and so if a schema change occurs, SQLITE_SCHEMA is returned by
129136** sqlite3_step(). In the new version, the original SQL text is retained
129137** and the statement is automatically recompiled if an schema change
129138** occurs.
129139*/
129140SQLITE_API int sqlite3_prepare(
129141 sqlite3 *db, /* Database handle. */
129142 const char *zSql, /* UTF-8 encoded SQL statement. */
129143 int nBytes, /* Length of zSql in bytes. */
129144 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129145 const char **pzTail /* OUT: End of parsed string */
129146){
129147 int rc;
129148 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
129149 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
129150 return rc;
129151}
129152SQLITE_API int sqlite3_prepare_v2(
129153 sqlite3 *db, /* Database handle. */
129154 const char *zSql, /* UTF-8 encoded SQL statement. */
129155 int nBytes, /* Length of zSql in bytes. */
129156 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129157 const char **pzTail /* OUT: End of parsed string */
129158){
129159 int rc;
129160 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
129161 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
129162 ** parameter.
129163 **
129164 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
129165 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
129166 ppStmt,pzTail);
129167 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
129168 return rc;
129169}
129170SQLITE_API int sqlite3_prepare_v3(
129171 sqlite3 *db, /* Database handle. */
129172 const char *zSql, /* UTF-8 encoded SQL statement. */
129173 int nBytes, /* Length of zSql in bytes. */
129174 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
129175 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129176 const char **pzTail /* OUT: End of parsed string */
129177){
129178 int rc;
129179 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
129180 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
129181 ** which is a bit array consisting of zero or more of the
129182 ** SQLITE_PREPARE_* flags.
129183 **
129184 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
129185 ** directly above. */
129186 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
129187 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
129188 0,ppStmt,pzTail);
129189 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
129190 return rc;
129191}
129192
129193
129194#ifndef SQLITE_OMIT_UTF16
129195/*
129196** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
129197*/
129198static int sqlite3Prepare16(
129199 sqlite3 *db, /* Database handle. */
129200 const void *zSql, /* UTF-16 encoded SQL statement. */
129201 int nBytes, /* Length of zSql in bytes. */
129202 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
129203 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129204 const void **pzTail /* OUT: End of parsed string */
129205){
129206 /* This function currently works by first transforming the UTF-16
129207 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
129208 ** tricky bit is figuring out the pointer to return in *pzTail.
129209 */
129210 char *zSql8;
129211 const char *zTail8 = 0;
129212 int rc = SQLITE_OK;
129213
129214#ifdef SQLITE_ENABLE_API_ARMOR
129215 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
129216#endif
129217 *ppStmt = 0;
129218 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
129219 return SQLITE_MISUSE_BKPT;
129220 }
129221 if( nBytes>=0 ){
129222 int sz;
129223 const char *z = (const char*)zSql;
129224 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
129225 nBytes = sz;
129226 }
129227 sqlite3_mutex_enter(db->mutex);
129228 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
129229 if( zSql8 ){
129230 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
129231 }
129232
129233 if( zTail8 && pzTail ){
129234 /* If sqlite3_prepare returns a tail pointer, we calculate the
129235 ** equivalent pointer into the UTF-16 string by counting the unicode
129236 ** characters between zSql8 and zTail8, and then returning a pointer
129237 ** the same number of characters into the UTF-16 string.
129238 */
129239 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
129240 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
129241 }
129242 sqlite3DbFree(db, zSql8);
129243 rc = sqlite3ApiExit(db, rc);
129244 sqlite3_mutex_leave(db->mutex);
129245 return rc;
129246}
129247
129248/*
129249** Two versions of the official API. Legacy and new use. In the legacy
129250** version, the original SQL text is not saved in the prepared statement
129251** and so if a schema change occurs, SQLITE_SCHEMA is returned by
129252** sqlite3_step(). In the new version, the original SQL text is retained
129253** and the statement is automatically recompiled if an schema change
129254** occurs.
129255*/
129256SQLITE_API int sqlite3_prepare16(
129257 sqlite3 *db, /* Database handle. */
129258 const void *zSql, /* UTF-16 encoded SQL statement. */
129259 int nBytes, /* Length of zSql in bytes. */
129260 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129261 const void **pzTail /* OUT: End of parsed string */
129262){
129263 int rc;
129264 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
129265 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
129266 return rc;
129267}
129268SQLITE_API int sqlite3_prepare16_v2(
129269 sqlite3 *db, /* Database handle. */
129270 const void *zSql, /* UTF-16 encoded SQL statement. */
129271 int nBytes, /* Length of zSql in bytes. */
129272 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129273 const void **pzTail /* OUT: End of parsed string */
129274){
129275 int rc;
129276 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
129277 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
129278 return rc;
129279}
129280SQLITE_API int sqlite3_prepare16_v3(
129281 sqlite3 *db, /* Database handle. */
129282 const void *zSql, /* UTF-16 encoded SQL statement. */
129283 int nBytes, /* Length of zSql in bytes. */
129284 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
129285 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
129286 const void **pzTail /* OUT: End of parsed string */
129287){
129288 int rc;
129289 rc = sqlite3Prepare16(db,zSql,nBytes,
129290 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
129291 ppStmt,pzTail);
129292 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
129293 return rc;
129294}
129295
129296#endif /* SQLITE_OMIT_UTF16 */
129297
129298/************** End of prepare.c *********************************************/
129299/************** Begin file select.c ******************************************/
129300/*
129301** 2001 September 15
129302**
129303** The author disclaims copyright to this source code. In place of
129304** a legal notice, here is a blessing:
129305**
129306** May you do good and not evil.
129307** May you find forgiveness for yourself and forgive others.
129308** May you share freely, never taking more than you give.
129309**
129310*************************************************************************
129311** This file contains C code routines that are called by the parser
129312** to handle SELECT statements in SQLite.
129313*/
129314/* #include "sqliteInt.h" */
129315
129316/*
129317** An instance of the following object is used to record information about
129318** how to process the DISTINCT keyword, to simplify passing that information
129319** into the selectInnerLoop() routine.
129320*/
129321typedef struct DistinctCtx DistinctCtx;
129322struct DistinctCtx {
129323 u8 isTnct; /* True if the DISTINCT keyword is present */
129324 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
129325 int tabTnct; /* Ephemeral table used for DISTINCT processing */
129326 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
129327};
129328
129329/*
129330** An instance of the following object is used to record information about
129331** the ORDER BY (or GROUP BY) clause of query is being coded.
129332**
129333** The aDefer[] array is used by the sorter-references optimization. For
129334** example, assuming there is no index that can be used for the ORDER BY,
129335** for the query:
129336**
129337** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
129338**
129339** it may be more efficient to add just the "a" values to the sorter, and
129340** retrieve the associated "bigblob" values directly from table t1 as the
129341** 10 smallest "a" values are extracted from the sorter.
129342**
129343** When the sorter-reference optimization is used, there is one entry in the
129344** aDefer[] array for each database table that may be read as values are
129345** extracted from the sorter.
129346*/
129347typedef struct SortCtx SortCtx;
129348struct SortCtx {
129349 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
129350 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
129351 int iECursor; /* Cursor number for the sorter */
129352 int regReturn; /* Register holding block-output return address */
129353 int labelBkOut; /* Start label for the block-output subroutine */
129354 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
129355 int labelDone; /* Jump here when done, ex: LIMIT reached */
129356 int labelOBLopt; /* Jump here when sorter is full */
129357 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
129358#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129359 u8 nDefer; /* Number of valid entries in aDefer[] */
129360 struct DeferredCsr {
129361 Table *pTab; /* Table definition */
129362 int iCsr; /* Cursor number for table */
129363 int nKey; /* Number of PK columns for table pTab (>=1) */
129364 } aDefer[4];
129365#endif
129366 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
129367};
129368#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
129369
129370/*
129371** Delete all the content of a Select structure. Deallocate the structure
129372** itself depending on the value of bFree
129373**
129374** If bFree==1, call sqlite3DbFree() on the p object.
129375** If bFree==0, Leave the first Select object unfreed
129376*/
129377static void clearSelect(sqlite3 *db, Select *p, int bFree){
129378 while( p ){
129379 Select *pPrior = p->pPrior;
129380 sqlite3ExprListDelete(db, p->pEList);
129381 sqlite3SrcListDelete(db, p->pSrc);
129382 sqlite3ExprDelete(db, p->pWhere);
129383 sqlite3ExprListDelete(db, p->pGroupBy);
129384 sqlite3ExprDelete(db, p->pHaving);
129385 sqlite3ExprListDelete(db, p->pOrderBy);
129386 sqlite3ExprDelete(db, p->pLimit);
129387#ifndef SQLITE_OMIT_WINDOWFUNC
129388 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
129389 sqlite3WindowListDelete(db, p->pWinDefn);
129390 }
129391#endif
129392 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
129393 if( bFree ) sqlite3DbFreeNN(db, p);
129394 p = pPrior;
129395 bFree = 1;
129396 }
129397}
129398
129399/*
129400** Initialize a SelectDest structure.
129401*/
129402SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
129403 pDest->eDest = (u8)eDest;
129404 pDest->iSDParm = iParm;
129405 pDest->iSDParm2 = 0;
129406 pDest->zAffSdst = 0;
129407 pDest->iSdst = 0;
129408 pDest->nSdst = 0;
129409}
129410
129411
129412/*
129413** Allocate a new Select structure and return a pointer to that
129414** structure.
129415*/
129416SQLITE_PRIVATE Select *sqlite3SelectNew(
129417 Parse *pParse, /* Parsing context */
129418 ExprList *pEList, /* which columns to include in the result */
129419 SrcList *pSrc, /* the FROM clause -- which tables to scan */
129420 Expr *pWhere, /* the WHERE clause */
129421 ExprList *pGroupBy, /* the GROUP BY clause */
129422 Expr *pHaving, /* the HAVING clause */
129423 ExprList *pOrderBy, /* the ORDER BY clause */
129424 u32 selFlags, /* Flag parameters, such as SF_Distinct */
129425 Expr *pLimit /* LIMIT value. NULL means not used */
129426){
129427 Select *pNew, *pAllocated;
129428 Select standin;
129429 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
129430 if( pNew==0 ){
129431 assert( pParse->db->mallocFailed );
129432 pNew = &standin;
129433 }
129434 if( pEList==0 ){
129435 pEList = sqlite3ExprListAppend(pParse, 0,
129436 sqlite3Expr(pParse->db,TK_ASTERISK,0));
129437 }
129438 pNew->pEList = pEList;
129439 pNew->op = TK_SELECT;
129440 pNew->selFlags = selFlags;
129441 pNew->iLimit = 0;
129442 pNew->iOffset = 0;
129443 pNew->selId = ++pParse->nSelect;
129444 pNew->addrOpenEphm[0] = -1;
129445 pNew->addrOpenEphm[1] = -1;
129446 pNew->nSelectRow = 0;
129447 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
129448 pNew->pSrc = pSrc;
129449 pNew->pWhere = pWhere;
129450 pNew->pGroupBy = pGroupBy;
129451 pNew->pHaving = pHaving;
129452 pNew->pOrderBy = pOrderBy;
129453 pNew->pPrior = 0;
129454 pNew->pNext = 0;
129455 pNew->pLimit = pLimit;
129456 pNew->pWith = 0;
129457#ifndef SQLITE_OMIT_WINDOWFUNC
129458 pNew->pWin = 0;
129459 pNew->pWinDefn = 0;
129460#endif
129461 if( pParse->db->mallocFailed ) {
129462 clearSelect(pParse->db, pNew, pNew!=&standin);
129463 pAllocated = 0;
129464 }else{
129465 assert( pNew->pSrc!=0 || pParse->nErr>0 );
129466 }
129467 return pAllocated;
129468}
129469
129470
129471/*
129472** Delete the given Select structure and all of its substructures.
129473*/
129474SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
129475 if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
129476}
129477
129478/*
129479** Return a pointer to the right-most SELECT statement in a compound.
129480*/
129481static Select *findRightmost(Select *p){
129482 while( p->pNext ) p = p->pNext;
129483 return p;
129484}
129485
129486/*
129487** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
129488** type of join. Return an integer constant that expresses that type
129489** in terms of the following bit values:
129490**
129491** JT_INNER
129492** JT_CROSS
129493** JT_OUTER
129494** JT_NATURAL
129495** JT_LEFT
129496** JT_RIGHT
129497**
129498** A full outer join is the combination of JT_LEFT and JT_RIGHT.
129499**
129500** If an illegal or unsupported join type is seen, then still return
129501** a join type, but put an error in the pParse structure.
129502*/
129503SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
129504 int jointype = 0;
129505 Token *apAll[3];
129506 Token *p;
129507 /* 0123456789 123456789 123456789 123 */
129508 static const char zKeyText[] = "naturaleftouterightfullinnercross";
129509 static const struct {
129510 u8 i; /* Beginning of keyword text in zKeyText[] */
129511 u8 nChar; /* Length of the keyword in characters */
129512 u8 code; /* Join type mask */
129513 } aKeyword[] = {
129514 /* natural */ { 0, 7, JT_NATURAL },
129515 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
129516 /* outer */ { 10, 5, JT_OUTER },
129517 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
129518 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
129519 /* inner */ { 23, 5, JT_INNER },
129520 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
129521 };
129522 int i, j;
129523 apAll[0] = pA;
129524 apAll[1] = pB;
129525 apAll[2] = pC;
129526 for(i=0; i<3 && apAll[i]; i++){
129527 p = apAll[i];
129528 for(j=0; j<ArraySize(aKeyword); j++){
129529 if( p->n==aKeyword[j].nChar
129530 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
129531 jointype |= aKeyword[j].code;
129532 break;
129533 }
129534 }
129535 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
129536 if( j>=ArraySize(aKeyword) ){
129537 jointype |= JT_ERROR;
129538 break;
129539 }
129540 }
129541 if(
129542 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
129543 (jointype & JT_ERROR)!=0
129544 ){
129545 const char *zSp = " ";
129546 assert( pB!=0 );
129547 if( pC==0 ){ zSp++; }
129548 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
129549 "%T %T%s%T", pA, pB, zSp, pC);
129550 jointype = JT_INNER;
129551 }else if( (jointype & JT_OUTER)!=0
129552 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
129553 sqlite3ErrorMsg(pParse,
129554 "RIGHT and FULL OUTER JOINs are not currently supported");
129555 jointype = JT_INNER;
129556 }
129557 return jointype;
129558}
129559
129560/*
129561** Return the index of a column in a table. Return -1 if the column
129562** is not contained in the table.
129563*/
129564static int columnIndex(Table *pTab, const char *zCol){
129565 int i;
129566 u8 h = sqlite3StrIHash(zCol);
129567 Column *pCol;
129568 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
129569 if( pCol->hName==h && sqlite3StrICmp(pCol->zName, zCol)==0 ) return i;
129570 }
129571 return -1;
129572}
129573
129574/*
129575** Search the first N tables in pSrc, from left to right, looking for a
129576** table that has a column named zCol.
129577**
129578** When found, set *piTab and *piCol to the table index and column index
129579** of the matching column and return TRUE.
129580**
129581** If not found, return FALSE.
129582*/
129583static int tableAndColumnIndex(
129584 SrcList *pSrc, /* Array of tables to search */
129585 int N, /* Number of tables in pSrc->a[] to search */
129586 const char *zCol, /* Name of the column we are looking for */
129587 int *piTab, /* Write index of pSrc->a[] here */
129588 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
129589 int bIgnoreHidden /* True to ignore hidden columns */
129590){
129591 int i; /* For looping over tables in pSrc */
129592 int iCol; /* Index of column matching zCol */
129593
129594 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
129595 for(i=0; i<N; i++){
129596 iCol = columnIndex(pSrc->a[i].pTab, zCol);
129597 if( iCol>=0
129598 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
129599 ){
129600 if( piTab ){
129601 *piTab = i;
129602 *piCol = iCol;
129603 }
129604 return 1;
129605 }
129606 }
129607 return 0;
129608}
129609
129610/*
129611** This function is used to add terms implied by JOIN syntax to the
129612** WHERE clause expression of a SELECT statement. The new term, which
129613** is ANDed with the existing WHERE clause, is of the form:
129614**
129615** (tab1.col1 = tab2.col2)
129616**
129617** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
129618** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
129619** column iColRight of tab2.
129620*/
129621static void addWhereTerm(
129622 Parse *pParse, /* Parsing context */
129623 SrcList *pSrc, /* List of tables in FROM clause */
129624 int iLeft, /* Index of first table to join in pSrc */
129625 int iColLeft, /* Index of column in first table */
129626 int iRight, /* Index of second table in pSrc */
129627 int iColRight, /* Index of column in second table */
129628 int isOuterJoin, /* True if this is an OUTER join */
129629 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
129630){
129631 sqlite3 *db = pParse->db;
129632 Expr *pE1;
129633 Expr *pE2;
129634 Expr *pEq;
129635
129636 assert( iLeft<iRight );
129637 assert( pSrc->nSrc>iRight );
129638 assert( pSrc->a[iLeft].pTab );
129639 assert( pSrc->a[iRight].pTab );
129640
129641 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
129642 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
129643
129644 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
129645 if( pEq && isOuterJoin ){
129646 ExprSetProperty(pEq, EP_FromJoin);
129647 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
129648 ExprSetVVAProperty(pEq, EP_NoReduce);
129649 pEq->iRightJoinTable = (i16)pE2->iTable;
129650 }
129651 *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
129652}
129653
129654/*
129655** Set the EP_FromJoin property on all terms of the given expression.
129656** And set the Expr.iRightJoinTable to iTable for every term in the
129657** expression.
129658**
129659** The EP_FromJoin property is used on terms of an expression to tell
129660** the LEFT OUTER JOIN processing logic that this term is part of the
129661** join restriction specified in the ON or USING clause and not a part
129662** of the more general WHERE clause. These terms are moved over to the
129663** WHERE clause during join processing but we need to remember that they
129664** originated in the ON or USING clause.
129665**
129666** The Expr.iRightJoinTable tells the WHERE clause processing that the
129667** expression depends on table iRightJoinTable even if that table is not
129668** explicitly mentioned in the expression. That information is needed
129669** for cases like this:
129670**
129671** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
129672**
129673** The where clause needs to defer the handling of the t1.x=5
129674** term until after the t2 loop of the join. In that way, a
129675** NULL t2 row will be inserted whenever t1.x!=5. If we do not
129676** defer the handling of t1.x=5, it will be processed immediately
129677** after the t1 loop and rows with t1.x!=5 will never appear in
129678** the output, which is incorrect.
129679*/
129680SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){
129681 while( p ){
129682 ExprSetProperty(p, EP_FromJoin);
129683 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
129684 ExprSetVVAProperty(p, EP_NoReduce);
129685 p->iRightJoinTable = (i16)iTable;
129686 if( p->op==TK_FUNCTION && p->x.pList ){
129687 int i;
129688 for(i=0; i<p->x.pList->nExpr; i++){
129689 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
129690 }
129691 }
129692 sqlite3SetJoinExpr(p->pLeft, iTable);
129693 p = p->pRight;
129694 }
129695}
129696
129697/* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every
129698** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
129699** an ordinary term that omits the EP_FromJoin mark.
129700**
129701** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
129702*/
129703static void unsetJoinExpr(Expr *p, int iTable){
129704 while( p ){
129705 if( ExprHasProperty(p, EP_FromJoin)
129706 && (iTable<0 || p->iRightJoinTable==iTable) ){
129707 ExprClearProperty(p, EP_FromJoin);
129708 }
129709 if( p->op==TK_FUNCTION && p->x.pList ){
129710 int i;
129711 for(i=0; i<p->x.pList->nExpr; i++){
129712 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
129713 }
129714 }
129715 unsetJoinExpr(p->pLeft, iTable);
129716 p = p->pRight;
129717 }
129718}
129719
129720/*
129721** This routine processes the join information for a SELECT statement.
129722** ON and USING clauses are converted into extra terms of the WHERE clause.
129723** NATURAL joins also create extra WHERE clause terms.
129724**
129725** The terms of a FROM clause are contained in the Select.pSrc structure.
129726** The left most table is the first entry in Select.pSrc. The right-most
129727** table is the last entry. The join operator is held in the entry to
129728** the left. Thus entry 0 contains the join operator for the join between
129729** entries 0 and 1. Any ON or USING clauses associated with the join are
129730** also attached to the left entry.
129731**
129732** This routine returns the number of errors encountered.
129733*/
129734static int sqliteProcessJoin(Parse *pParse, Select *p){
129735 SrcList *pSrc; /* All tables in the FROM clause */
129736 int i, j; /* Loop counters */
129737 struct SrcList_item *pLeft; /* Left table being joined */
129738 struct SrcList_item *pRight; /* Right table being joined */
129739
129740 pSrc = p->pSrc;
129741 pLeft = &pSrc->a[0];
129742 pRight = &pLeft[1];
129743 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
129744 Table *pRightTab = pRight->pTab;
129745 int isOuter;
129746
129747 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
129748 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
129749
129750 /* When the NATURAL keyword is present, add WHERE clause terms for
129751 ** every column that the two tables have in common.
129752 */
129753 if( pRight->fg.jointype & JT_NATURAL ){
129754 if( pRight->pOn || pRight->pUsing ){
129755 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
129756 "an ON or USING clause", 0);
129757 return 1;
129758 }
129759 for(j=0; j<pRightTab->nCol; j++){
129760 char *zName; /* Name of column in the right table */
129761 int iLeft; /* Matching left table */
129762 int iLeftCol; /* Matching column in the left table */
129763
129764 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
129765 zName = pRightTab->aCol[j].zName;
129766 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 1) ){
129767 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
129768 isOuter, &p->pWhere);
129769 }
129770 }
129771 }
129772
129773 /* Disallow both ON and USING clauses in the same join
129774 */
129775 if( pRight->pOn && pRight->pUsing ){
129776 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
129777 "clauses in the same join");
129778 return 1;
129779 }
129780
129781 /* Add the ON clause to the end of the WHERE clause, connected by
129782 ** an AND operator.
129783 */
129784 if( pRight->pOn ){
129785 if( isOuter ) sqlite3SetJoinExpr(pRight->pOn, pRight->iCursor);
129786 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
129787 pRight->pOn = 0;
129788 }
129789
129790 /* Create extra terms on the WHERE clause for each column named
129791 ** in the USING clause. Example: If the two tables to be joined are
129792 ** A and B and the USING clause names X, Y, and Z, then add this
129793 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
129794 ** Report an error if any column mentioned in the USING clause is
129795 ** not contained in both tables to be joined.
129796 */
129797 if( pRight->pUsing ){
129798 IdList *pList = pRight->pUsing;
129799 for(j=0; j<pList->nId; j++){
129800 char *zName; /* Name of the term in the USING clause */
129801 int iLeft; /* Table on the left with matching column name */
129802 int iLeftCol; /* Column number of matching column on the left */
129803 int iRightCol; /* Column number of matching column on the right */
129804
129805 zName = pList->a[j].zName;
129806 iRightCol = columnIndex(pRightTab, zName);
129807 if( iRightCol<0
129808 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 0)
129809 ){
129810 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
129811 "not present in both tables", zName);
129812 return 1;
129813 }
129814 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
129815 isOuter, &p->pWhere);
129816 }
129817 }
129818 }
129819 return 0;
129820}
129821
129822/*
129823** An instance of this object holds information (beyond pParse and pSelect)
129824** needed to load the next result row that is to be added to the sorter.
129825*/
129826typedef struct RowLoadInfo RowLoadInfo;
129827struct RowLoadInfo {
129828 int regResult; /* Store results in array of registers here */
129829 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
129830#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129831 ExprList *pExtra; /* Extra columns needed by sorter refs */
129832 int regExtraResult; /* Where to load the extra columns */
129833#endif
129834};
129835
129836/*
129837** This routine does the work of loading query data into an array of
129838** registers so that it can be added to the sorter.
129839*/
129840static void innerLoopLoadRow(
129841 Parse *pParse, /* Statement under construction */
129842 Select *pSelect, /* The query being coded */
129843 RowLoadInfo *pInfo /* Info needed to complete the row load */
129844){
129845 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
129846 0, pInfo->ecelFlags);
129847#ifdef SQLITE_ENABLE_SORTER_REFERENCES
129848 if( pInfo->pExtra ){
129849 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
129850 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
129851 }
129852#endif
129853}
129854
129855/*
129856** Code the OP_MakeRecord instruction that generates the entry to be
129857** added into the sorter.
129858**
129859** Return the register in which the result is stored.
129860*/
129861static int makeSorterRecord(
129862 Parse *pParse,
129863 SortCtx *pSort,
129864 Select *pSelect,
129865 int regBase,
129866 int nBase
129867){
129868 int nOBSat = pSort->nOBSat;
129869 Vdbe *v = pParse->pVdbe;
129870 int regOut = ++pParse->nMem;
129871 if( pSort->pDeferredRowLoad ){
129872 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
129873 }
129874 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
129875 return regOut;
129876}
129877
129878/*
129879** Generate code that will push the record in registers regData
129880** through regData+nData-1 onto the sorter.
129881*/
129882static void pushOntoSorter(
129883 Parse *pParse, /* Parser context */
129884 SortCtx *pSort, /* Information about the ORDER BY clause */
129885 Select *pSelect, /* The whole SELECT statement */
129886 int regData, /* First register holding data to be sorted */
129887 int regOrigData, /* First register holding data before packing */
129888 int nData, /* Number of elements in the regData data array */
129889 int nPrefixReg /* No. of reg prior to regData available for use */
129890){
129891 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
129892 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
129893 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
129894 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
129895 int regBase; /* Regs for sorter record */
129896 int regRecord = 0; /* Assembled sorter record */
129897 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
129898 int op; /* Opcode to add sorter record to sorter */
129899 int iLimit; /* LIMIT counter */
129900 int iSkip = 0; /* End of the sorter insert loop */
129901
129902 assert( bSeq==0 || bSeq==1 );
129903
129904 /* Three cases:
129905 ** (1) The data to be sorted has already been packed into a Record
129906 ** by a prior OP_MakeRecord. In this case nData==1 and regData
129907 ** will be completely unrelated to regOrigData.
129908 ** (2) All output columns are included in the sort record. In that
129909 ** case regData==regOrigData.
129910 ** (3) Some output columns are omitted from the sort record due to
129911 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
129912 ** SQLITE_ECEL_OMITREF optimization, or due to the
129913 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
129914 ** regOrigData is 0 to prevent this routine from trying to copy
129915 ** values that might not yet exist.
129916 */
129917 assert( nData==1 || regData==regOrigData || regOrigData==0 );
129918
129919 if( nPrefixReg ){
129920 assert( nPrefixReg==nExpr+bSeq );
129921 regBase = regData - nPrefixReg;
129922 }else{
129923 regBase = pParse->nMem + 1;
129924 pParse->nMem += nBase;
129925 }
129926 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
129927 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
129928 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
129929 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
129930 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
129931 if( bSeq ){
129932 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
129933 }
129934 if( nPrefixReg==0 && nData>0 ){
129935 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
129936 }
129937 if( nOBSat>0 ){
129938 int regPrevKey; /* The first nOBSat columns of the previous row */
129939 int addrFirst; /* Address of the OP_IfNot opcode */
129940 int addrJmp; /* Address of the OP_Jump opcode */
129941 VdbeOp *pOp; /* Opcode that opens the sorter */
129942 int nKey; /* Number of sorting key columns, including OP_Sequence */
129943 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
129944
129945 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
129946 regPrevKey = pParse->nMem+1;
129947 pParse->nMem += pSort->nOBSat;
129948 nKey = nExpr - pSort->nOBSat + bSeq;
129949 if( bSeq ){
129950 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
129951 }else{
129952 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
129953 }
129954 VdbeCoverage(v);
129955 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
129956 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
129957 if( pParse->db->mallocFailed ) return;
129958 pOp->p2 = nKey + nData;
129959 pKI = pOp->p4.pKeyInfo;
129960 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
129961 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
129962 testcase( pKI->nAllField > pKI->nKeyField+2 );
129963 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
129964 pKI->nAllField-pKI->nKeyField-1);
129965 pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
129966 addrJmp = sqlite3VdbeCurrentAddr(v);
129967 sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
129968 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
129969 pSort->regReturn = ++pParse->nMem;
129970 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
129971 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
129972 if( iLimit ){
129973 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
129974 VdbeCoverage(v);
129975 }
129976 sqlite3VdbeJumpHere(v, addrFirst);
129977 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
129978 sqlite3VdbeJumpHere(v, addrJmp);
129979 }
129980 if( iLimit ){
129981 /* At this point the values for the new sorter entry are stored
129982 ** in an array of registers. They need to be composed into a record
129983 ** and inserted into the sorter if either (a) there are currently
129984 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
129985 ** the largest record currently in the sorter. If (b) is true and there
129986 ** are already LIMIT+OFFSET items in the sorter, delete the largest
129987 ** entry before inserting the new one. This way there are never more
129988 ** than LIMIT+OFFSET items in the sorter.
129989 **
129990 ** If the new record does not need to be inserted into the sorter,
129991 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
129992 ** value is not zero, then it is a label of where to jump. Otherwise,
129993 ** just bypass the row insert logic. See the header comment on the
129994 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
129995 */
129996 int iCsr = pSort->iECursor;
129997 sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
129998 VdbeCoverage(v);
129999 sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
130000 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
130001 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
130002 VdbeCoverage(v);
130003 sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
130004 }
130005 if( regRecord==0 ){
130006 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
130007 }
130008 if( pSort->sortFlags & SORTFLAG_UseSorter ){
130009 op = OP_SorterInsert;
130010 }else{
130011 op = OP_IdxInsert;
130012 }
130013 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
130014 regBase+nOBSat, nBase-nOBSat);
130015 if( iSkip ){
130016 sqlite3VdbeChangeP2(v, iSkip,
130017 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
130018 }
130019}
130020
130021/*
130022** Add code to implement the OFFSET
130023*/
130024static void codeOffset(
130025 Vdbe *v, /* Generate code into this VM */
130026 int iOffset, /* Register holding the offset counter */
130027 int iContinue /* Jump here to skip the current record */
130028){
130029 if( iOffset>0 ){
130030 sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
130031 VdbeComment((v, "OFFSET"));
130032 }
130033}
130034
130035/*
130036** Add code that will check to make sure the N registers starting at iMem
130037** form a distinct entry. iTab is a sorting index that holds previously
130038** seen combinations of the N values. A new entry is made in iTab
130039** if the current N values are new.
130040**
130041** A jump to addrRepeat is made and the N+1 values are popped from the
130042** stack if the top N elements are not distinct.
130043*/
130044static void codeDistinct(
130045 Parse *pParse, /* Parsing and code generating context */
130046 int iTab, /* A sorting index used to test for distinctness */
130047 int addrRepeat, /* Jump to here if not distinct */
130048 int N, /* Number of elements */
130049 int iMem /* First element */
130050){
130051 Vdbe *v;
130052 int r1;
130053
130054 v = pParse->pVdbe;
130055 r1 = sqlite3GetTempReg(pParse);
130056 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
130057 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
130058 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
130059 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
130060 sqlite3ReleaseTempReg(pParse, r1);
130061}
130062
130063#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130064/*
130065** This function is called as part of inner-loop generation for a SELECT
130066** statement with an ORDER BY that is not optimized by an index. It
130067** determines the expressions, if any, that the sorter-reference
130068** optimization should be used for. The sorter-reference optimization
130069** is used for SELECT queries like:
130070**
130071** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
130072**
130073** If the optimization is used for expression "bigblob", then instead of
130074** storing values read from that column in the sorter records, the PK of
130075** the row from table t1 is stored instead. Then, as records are extracted from
130076** the sorter to return to the user, the required value of bigblob is
130077** retrieved directly from table t1. If the values are very large, this
130078** can be more efficient than storing them directly in the sorter records.
130079**
130080** The ExprList_item.bSorterRef flag is set for each expression in pEList
130081** for which the sorter-reference optimization should be enabled.
130082** Additionally, the pSort->aDefer[] array is populated with entries
130083** for all cursors required to evaluate all selected expressions. Finally.
130084** output variable (*ppExtra) is set to an expression list containing
130085** expressions for all extra PK values that should be stored in the
130086** sorter records.
130087*/
130088static void selectExprDefer(
130089 Parse *pParse, /* Leave any error here */
130090 SortCtx *pSort, /* Sorter context */
130091 ExprList *pEList, /* Expressions destined for sorter */
130092 ExprList **ppExtra /* Expressions to append to sorter record */
130093){
130094 int i;
130095 int nDefer = 0;
130096 ExprList *pExtra = 0;
130097 for(i=0; i<pEList->nExpr; i++){
130098 struct ExprList_item *pItem = &pEList->a[i];
130099 if( pItem->u.x.iOrderByCol==0 ){
130100 Expr *pExpr = pItem->pExpr;
130101 Table *pTab = pExpr->y.pTab;
130102 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
130103 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
130104 ){
130105 int j;
130106 for(j=0; j<nDefer; j++){
130107 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
130108 }
130109 if( j==nDefer ){
130110 if( nDefer==ArraySize(pSort->aDefer) ){
130111 continue;
130112 }else{
130113 int nKey = 1;
130114 int k;
130115 Index *pPk = 0;
130116 if( !HasRowid(pTab) ){
130117 pPk = sqlite3PrimaryKeyIndex(pTab);
130118 nKey = pPk->nKeyCol;
130119 }
130120 for(k=0; k<nKey; k++){
130121 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
130122 if( pNew ){
130123 pNew->iTable = pExpr->iTable;
130124 pNew->y.pTab = pExpr->y.pTab;
130125 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
130126 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
130127 }
130128 }
130129 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
130130 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
130131 pSort->aDefer[nDefer].nKey = nKey;
130132 nDefer++;
130133 }
130134 }
130135 pItem->bSorterRef = 1;
130136 }
130137 }
130138 }
130139 pSort->nDefer = (u8)nDefer;
130140 *ppExtra = pExtra;
130141}
130142#endif
130143
130144/*
130145** This routine generates the code for the inside of the inner loop
130146** of a SELECT.
130147**
130148** If srcTab is negative, then the p->pEList expressions
130149** are evaluated in order to get the data for this row. If srcTab is
130150** zero or more, then data is pulled from srcTab and p->pEList is used only
130151** to get the number of columns and the collation sequence for each column.
130152*/
130153static void selectInnerLoop(
130154 Parse *pParse, /* The parser context */
130155 Select *p, /* The complete select statement being coded */
130156 int srcTab, /* Pull data from this table if non-negative */
130157 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
130158 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
130159 SelectDest *pDest, /* How to dispose of the results */
130160 int iContinue, /* Jump here to continue with next row */
130161 int iBreak /* Jump here to break out of the inner loop */
130162){
130163 Vdbe *v = pParse->pVdbe;
130164 int i;
130165 int hasDistinct; /* True if the DISTINCT keyword is present */
130166 int eDest = pDest->eDest; /* How to dispose of results */
130167 int iParm = pDest->iSDParm; /* First argument to disposal method */
130168 int nResultCol; /* Number of result columns */
130169 int nPrefixReg = 0; /* Number of extra registers before regResult */
130170 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
130171
130172 /* Usually, regResult is the first cell in an array of memory cells
130173 ** containing the current result row. In this case regOrig is set to the
130174 ** same value. However, if the results are being sent to the sorter, the
130175 ** values for any expressions that are also part of the sort-key are omitted
130176 ** from this array. In this case regOrig is set to zero. */
130177 int regResult; /* Start of memory holding current results */
130178 int regOrig; /* Start of memory holding full result (or 0) */
130179
130180 assert( v );
130181 assert( p->pEList!=0 );
130182 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
130183 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
130184 if( pSort==0 && !hasDistinct ){
130185 assert( iContinue!=0 );
130186 codeOffset(v, p->iOffset, iContinue);
130187 }
130188
130189 /* Pull the requested columns.
130190 */
130191 nResultCol = p->pEList->nExpr;
130192
130193 if( pDest->iSdst==0 ){
130194 if( pSort ){
130195 nPrefixReg = pSort->pOrderBy->nExpr;
130196 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
130197 pParse->nMem += nPrefixReg;
130198 }
130199 pDest->iSdst = pParse->nMem+1;
130200 pParse->nMem += nResultCol;
130201 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
130202 /* This is an error condition that can result, for example, when a SELECT
130203 ** on the right-hand side of an INSERT contains more result columns than
130204 ** there are columns in the table on the left. The error will be caught
130205 ** and reported later. But we need to make sure enough memory is allocated
130206 ** to avoid other spurious errors in the meantime. */
130207 pParse->nMem += nResultCol;
130208 }
130209 pDest->nSdst = nResultCol;
130210 regOrig = regResult = pDest->iSdst;
130211 if( srcTab>=0 ){
130212 for(i=0; i<nResultCol; i++){
130213 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
130214 VdbeComment((v, "%s", p->pEList->a[i].zEName));
130215 }
130216 }else if( eDest!=SRT_Exists ){
130217#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130218 ExprList *pExtra = 0;
130219#endif
130220 /* If the destination is an EXISTS(...) expression, the actual
130221 ** values returned by the SELECT are not required.
130222 */
130223 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
130224 ExprList *pEList;
130225 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
130226 ecelFlags = SQLITE_ECEL_DUP;
130227 }else{
130228 ecelFlags = 0;
130229 }
130230 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
130231 /* For each expression in p->pEList that is a copy of an expression in
130232 ** the ORDER BY clause (pSort->pOrderBy), set the associated
130233 ** iOrderByCol value to one more than the index of the ORDER BY
130234 ** expression within the sort-key that pushOntoSorter() will generate.
130235 ** This allows the p->pEList field to be omitted from the sorted record,
130236 ** saving space and CPU cycles. */
130237 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
130238
130239 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
130240 int j;
130241 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
130242 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
130243 }
130244 }
130245#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130246 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
130247 if( pExtra && pParse->db->mallocFailed==0 ){
130248 /* If there are any extra PK columns to add to the sorter records,
130249 ** allocate extra memory cells and adjust the OpenEphemeral
130250 ** instruction to account for the larger records. This is only
130251 ** required if there are one or more WITHOUT ROWID tables with
130252 ** composite primary keys in the SortCtx.aDefer[] array. */
130253 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
130254 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
130255 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
130256 pParse->nMem += pExtra->nExpr;
130257 }
130258#endif
130259
130260 /* Adjust nResultCol to account for columns that are omitted
130261 ** from the sorter by the optimizations in this branch */
130262 pEList = p->pEList;
130263 for(i=0; i<pEList->nExpr; i++){
130264 if( pEList->a[i].u.x.iOrderByCol>0
130265#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130266 || pEList->a[i].bSorterRef
130267#endif
130268 ){
130269 nResultCol--;
130270 regOrig = 0;
130271 }
130272 }
130273
130274 testcase( regOrig );
130275 testcase( eDest==SRT_Set );
130276 testcase( eDest==SRT_Mem );
130277 testcase( eDest==SRT_Coroutine );
130278 testcase( eDest==SRT_Output );
130279 assert( eDest==SRT_Set || eDest==SRT_Mem
130280 || eDest==SRT_Coroutine || eDest==SRT_Output
130281 || eDest==SRT_Upfrom );
130282 }
130283 sRowLoadInfo.regResult = regResult;
130284 sRowLoadInfo.ecelFlags = ecelFlags;
130285#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130286 sRowLoadInfo.pExtra = pExtra;
130287 sRowLoadInfo.regExtraResult = regResult + nResultCol;
130288 if( pExtra ) nResultCol += pExtra->nExpr;
130289#endif
130290 if( p->iLimit
130291 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
130292 && nPrefixReg>0
130293 ){
130294 assert( pSort!=0 );
130295 assert( hasDistinct==0 );
130296 pSort->pDeferredRowLoad = &sRowLoadInfo;
130297 regOrig = 0;
130298 }else{
130299 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
130300 }
130301 }
130302
130303 /* If the DISTINCT keyword was present on the SELECT statement
130304 ** and this row has been seen before, then do not make this row
130305 ** part of the result.
130306 */
130307 if( hasDistinct ){
130308 switch( pDistinct->eTnctType ){
130309 case WHERE_DISTINCT_ORDERED: {
130310 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
130311 int iJump; /* Jump destination */
130312 int regPrev; /* Previous row content */
130313
130314 /* Allocate space for the previous row */
130315 regPrev = pParse->nMem+1;
130316 pParse->nMem += nResultCol;
130317
130318 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
130319 ** sets the MEM_Cleared bit on the first register of the
130320 ** previous value. This will cause the OP_Ne below to always
130321 ** fail on the first iteration of the loop even if the first
130322 ** row is all NULLs.
130323 */
130324 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
130325 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
130326 pOp->opcode = OP_Null;
130327 pOp->p1 = 1;
130328 pOp->p2 = regPrev;
130329 pOp = 0; /* Ensure pOp is not used after sqlite3VdbeAddOp() */
130330
130331 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
130332 for(i=0; i<nResultCol; i++){
130333 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
130334 if( i<nResultCol-1 ){
130335 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
130336 VdbeCoverage(v);
130337 }else{
130338 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
130339 VdbeCoverage(v);
130340 }
130341 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
130342 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
130343 }
130344 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
130345 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
130346 break;
130347 }
130348
130349 case WHERE_DISTINCT_UNIQUE: {
130350 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
130351 break;
130352 }
130353
130354 default: {
130355 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
130356 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
130357 regResult);
130358 break;
130359 }
130360 }
130361 if( pSort==0 ){
130362 codeOffset(v, p->iOffset, iContinue);
130363 }
130364 }
130365
130366 switch( eDest ){
130367 /* In this mode, write each query result to the key of the temporary
130368 ** table iParm.
130369 */
130370#ifndef SQLITE_OMIT_COMPOUND_SELECT
130371 case SRT_Union: {
130372 int r1;
130373 r1 = sqlite3GetTempReg(pParse);
130374 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
130375 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
130376 sqlite3ReleaseTempReg(pParse, r1);
130377 break;
130378 }
130379
130380 /* Construct a record from the query result, but instead of
130381 ** saving that record, use it as a key to delete elements from
130382 ** the temporary table iParm.
130383 */
130384 case SRT_Except: {
130385 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
130386 break;
130387 }
130388#endif /* SQLITE_OMIT_COMPOUND_SELECT */
130389
130390 /* Store the result as data using a unique key.
130391 */
130392 case SRT_Fifo:
130393 case SRT_DistFifo:
130394 case SRT_Table:
130395 case SRT_EphemTab: {
130396 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
130397 testcase( eDest==SRT_Table );
130398 testcase( eDest==SRT_EphemTab );
130399 testcase( eDest==SRT_Fifo );
130400 testcase( eDest==SRT_DistFifo );
130401 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
130402#ifndef SQLITE_OMIT_CTE
130403 if( eDest==SRT_DistFifo ){
130404 /* If the destination is DistFifo, then cursor (iParm+1) is open
130405 ** on an ephemeral index. If the current row is already present
130406 ** in the index, do not write it to the output. If not, add the
130407 ** current row to the index and proceed with writing it to the
130408 ** output table as well. */
130409 int addr = sqlite3VdbeCurrentAddr(v) + 4;
130410 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
130411 VdbeCoverage(v);
130412 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
130413 assert( pSort==0 );
130414 }
130415#endif
130416 if( pSort ){
130417 assert( regResult==regOrig );
130418 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
130419 }else{
130420 int r2 = sqlite3GetTempReg(pParse);
130421 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
130422 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
130423 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
130424 sqlite3ReleaseTempReg(pParse, r2);
130425 }
130426 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
130427 break;
130428 }
130429
130430 case SRT_Upfrom: {
130431 if( pSort ){
130432 pushOntoSorter(
130433 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
130434 }else{
130435 int i2 = pDest->iSDParm2;
130436 int r1 = sqlite3GetTempReg(pParse);
130437
130438 /* If the UPDATE FROM join is an aggregate that matches no rows, it
130439 ** might still be trying to return one row, because that is what
130440 ** aggregates do. Don't record that empty row in the output table. */
130441 sqlite3VdbeAddOp2(v, OP_IsNull, regResult, iBreak); VdbeCoverage(v);
130442
130443 sqlite3VdbeAddOp3(v, OP_MakeRecord,
130444 regResult+(i2<0), nResultCol-(i2<0), r1);
130445 if( i2<0 ){
130446 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regResult);
130447 }else{
130448 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, i2);
130449 }
130450 }
130451 break;
130452 }
130453
130454#ifndef SQLITE_OMIT_SUBQUERY
130455 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
130456 ** then there should be a single item on the stack. Write this
130457 ** item into the set table with bogus data.
130458 */
130459 case SRT_Set: {
130460 if( pSort ){
130461 /* At first glance you would think we could optimize out the
130462 ** ORDER BY in this case since the order of entries in the set
130463 ** does not matter. But there might be a LIMIT clause, in which
130464 ** case the order does matter */
130465 pushOntoSorter(
130466 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
130467 }else{
130468 int r1 = sqlite3GetTempReg(pParse);
130469 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
130470 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
130471 r1, pDest->zAffSdst, nResultCol);
130472 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
130473 sqlite3ReleaseTempReg(pParse, r1);
130474 }
130475 break;
130476 }
130477
130478
130479 /* If any row exist in the result set, record that fact and abort.
130480 */
130481 case SRT_Exists: {
130482 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
130483 /* The LIMIT clause will terminate the loop for us */
130484 break;
130485 }
130486
130487 /* If this is a scalar select that is part of an expression, then
130488 ** store the results in the appropriate memory cell or array of
130489 ** memory cells and break out of the scan loop.
130490 */
130491 case SRT_Mem: {
130492 if( pSort ){
130493 assert( nResultCol<=pDest->nSdst );
130494 pushOntoSorter(
130495 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
130496 }else{
130497 assert( nResultCol==pDest->nSdst );
130498 assert( regResult==iParm );
130499 /* The LIMIT clause will jump out of the loop for us */
130500 }
130501 break;
130502 }
130503#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
130504
130505 case SRT_Coroutine: /* Send data to a co-routine */
130506 case SRT_Output: { /* Return the results */
130507 testcase( eDest==SRT_Coroutine );
130508 testcase( eDest==SRT_Output );
130509 if( pSort ){
130510 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
130511 nPrefixReg);
130512 }else if( eDest==SRT_Coroutine ){
130513 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
130514 }else{
130515 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
130516 }
130517 break;
130518 }
130519
130520#ifndef SQLITE_OMIT_CTE
130521 /* Write the results into a priority queue that is order according to
130522 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
130523 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
130524 ** pSO->nExpr columns, then make sure all keys are unique by adding a
130525 ** final OP_Sequence column. The last column is the record as a blob.
130526 */
130527 case SRT_DistQueue:
130528 case SRT_Queue: {
130529 int nKey;
130530 int r1, r2, r3;
130531 int addrTest = 0;
130532 ExprList *pSO;
130533 pSO = pDest->pOrderBy;
130534 assert( pSO );
130535 nKey = pSO->nExpr;
130536 r1 = sqlite3GetTempReg(pParse);
130537 r2 = sqlite3GetTempRange(pParse, nKey+2);
130538 r3 = r2+nKey+1;
130539 if( eDest==SRT_DistQueue ){
130540 /* If the destination is DistQueue, then cursor (iParm+1) is open
130541 ** on a second ephemeral index that holds all values every previously
130542 ** added to the queue. */
130543 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
130544 regResult, nResultCol);
130545 VdbeCoverage(v);
130546 }
130547 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
130548 if( eDest==SRT_DistQueue ){
130549 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
130550 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
130551 }
130552 for(i=0; i<nKey; i++){
130553 sqlite3VdbeAddOp2(v, OP_SCopy,
130554 regResult + pSO->a[i].u.x.iOrderByCol - 1,
130555 r2+i);
130556 }
130557 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
130558 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
130559 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
130560 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
130561 sqlite3ReleaseTempReg(pParse, r1);
130562 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
130563 break;
130564 }
130565#endif /* SQLITE_OMIT_CTE */
130566
130567
130568
130569#if !defined(SQLITE_OMIT_TRIGGER)
130570 /* Discard the results. This is used for SELECT statements inside
130571 ** the body of a TRIGGER. The purpose of such selects is to call
130572 ** user-defined functions that have side effects. We do not care
130573 ** about the actual results of the select.
130574 */
130575 default: {
130576 assert( eDest==SRT_Discard );
130577 break;
130578 }
130579#endif
130580 }
130581
130582 /* Jump to the end of the loop if the LIMIT is reached. Except, if
130583 ** there is a sorter, in which case the sorter has already limited
130584 ** the output for us.
130585 */
130586 if( pSort==0 && p->iLimit ){
130587 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
130588 }
130589}
130590
130591/*
130592** Allocate a KeyInfo object sufficient for an index of N key columns and
130593** X extra columns.
130594*/
130595SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
130596 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
130597 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
130598 if( p ){
130599 p->aSortFlags = (u8*)&p->aColl[N+X];
130600 p->nKeyField = (u16)N;
130601 p->nAllField = (u16)(N+X);
130602 p->enc = ENC(db);
130603 p->db = db;
130604 p->nRef = 1;
130605 memset(&p[1], 0, nExtra);
130606 }else{
130607 sqlite3OomFault(db);
130608 }
130609 return p;
130610}
130611
130612/*
130613** Deallocate a KeyInfo object
130614*/
130615SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
130616 if( p ){
130617 assert( p->nRef>0 );
130618 p->nRef--;
130619 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
130620 }
130621}
130622
130623/*
130624** Make a new pointer to a KeyInfo object
130625*/
130626SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
130627 if( p ){
130628 assert( p->nRef>0 );
130629 p->nRef++;
130630 }
130631 return p;
130632}
130633
130634#ifdef SQLITE_DEBUG
130635/*
130636** Return TRUE if a KeyInfo object can be change. The KeyInfo object
130637** can only be changed if this is just a single reference to the object.
130638**
130639** This routine is used only inside of assert() statements.
130640*/
130641SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
130642#endif /* SQLITE_DEBUG */
130643
130644/*
130645** Given an expression list, generate a KeyInfo structure that records
130646** the collating sequence for each expression in that expression list.
130647**
130648** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
130649** KeyInfo structure is appropriate for initializing a virtual index to
130650** implement that clause. If the ExprList is the result set of a SELECT
130651** then the KeyInfo structure is appropriate for initializing a virtual
130652** index to implement a DISTINCT test.
130653**
130654** Space to hold the KeyInfo structure is obtained from malloc. The calling
130655** function is responsible for seeing that this structure is eventually
130656** freed.
130657*/
130658SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
130659 Parse *pParse, /* Parsing context */
130660 ExprList *pList, /* Form the KeyInfo object from this ExprList */
130661 int iStart, /* Begin with this column of pList */
130662 int nExtra /* Add this many extra columns to the end */
130663){
130664 int nExpr;
130665 KeyInfo *pInfo;
130666 struct ExprList_item *pItem;
130667 sqlite3 *db = pParse->db;
130668 int i;
130669
130670 nExpr = pList->nExpr;
130671 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
130672 if( pInfo ){
130673 assert( sqlite3KeyInfoIsWriteable(pInfo) );
130674 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
130675 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
130676 pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
130677 }
130678 }
130679 return pInfo;
130680}
130681
130682/*
130683** Name of the connection operator, used for error messages.
130684*/
130685static const char *selectOpName(int id){
130686 char *z;
130687 switch( id ){
130688 case TK_ALL: z = "UNION ALL"; break;
130689 case TK_INTERSECT: z = "INTERSECT"; break;
130690 case TK_EXCEPT: z = "EXCEPT"; break;
130691 default: z = "UNION"; break;
130692 }
130693 return z;
130694}
130695
130696#ifndef SQLITE_OMIT_EXPLAIN
130697/*
130698** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
130699** is a no-op. Otherwise, it adds a single row of output to the EQP result,
130700** where the caption is of the form:
130701**
130702** "USE TEMP B-TREE FOR xxx"
130703**
130704** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
130705** is determined by the zUsage argument.
130706*/
130707static void explainTempTable(Parse *pParse, const char *zUsage){
130708 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
130709}
130710
130711/*
130712** Assign expression b to lvalue a. A second, no-op, version of this macro
130713** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
130714** in sqlite3Select() to assign values to structure member variables that
130715** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
130716** code with #ifndef directives.
130717*/
130718# define explainSetInteger(a, b) a = b
130719
130720#else
130721/* No-op versions of the explainXXX() functions and macros. */
130722# define explainTempTable(y,z)
130723# define explainSetInteger(y,z)
130724#endif
130725
130726
130727/*
130728** If the inner loop was generated using a non-null pOrderBy argument,
130729** then the results were placed in a sorter. After the loop is terminated
130730** we need to run the sorter and output the results. The following
130731** routine generates the code needed to do that.
130732*/
130733static void generateSortTail(
130734 Parse *pParse, /* Parsing context */
130735 Select *p, /* The SELECT statement */
130736 SortCtx *pSort, /* Information on the ORDER BY clause */
130737 int nColumn, /* Number of columns of data */
130738 SelectDest *pDest /* Write the sorted results here */
130739){
130740 Vdbe *v = pParse->pVdbe; /* The prepared statement */
130741 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
130742 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
130743 int addr; /* Top of output loop. Jump for Next. */
130744 int addrOnce = 0;
130745 int iTab;
130746 ExprList *pOrderBy = pSort->pOrderBy;
130747 int eDest = pDest->eDest;
130748 int iParm = pDest->iSDParm;
130749 int regRow;
130750 int regRowid;
130751 int iCol;
130752 int nKey; /* Number of key columns in sorter record */
130753 int iSortTab; /* Sorter cursor to read from */
130754 int i;
130755 int bSeq; /* True if sorter record includes seq. no. */
130756 int nRefKey = 0;
130757 struct ExprList_item *aOutEx = p->pEList->a;
130758
130759 assert( addrBreak<0 );
130760 if( pSort->labelBkOut ){
130761 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
130762 sqlite3VdbeGoto(v, addrBreak);
130763 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
130764 }
130765
130766#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130767 /* Open any cursors needed for sorter-reference expressions */
130768 for(i=0; i<pSort->nDefer; i++){
130769 Table *pTab = pSort->aDefer[i].pTab;
130770 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
130771 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
130772 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
130773 }
130774#endif
130775
130776 iTab = pSort->iECursor;
130777 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
130778 regRowid = 0;
130779 regRow = pDest->iSdst;
130780 }else{
130781 regRowid = sqlite3GetTempReg(pParse);
130782 if( eDest==SRT_EphemTab || eDest==SRT_Table ){
130783 regRow = sqlite3GetTempReg(pParse);
130784 nColumn = 0;
130785 }else{
130786 regRow = sqlite3GetTempRange(pParse, nColumn);
130787 }
130788 }
130789 nKey = pOrderBy->nExpr - pSort->nOBSat;
130790 if( pSort->sortFlags & SORTFLAG_UseSorter ){
130791 int regSortOut = ++pParse->nMem;
130792 iSortTab = pParse->nTab++;
130793 if( pSort->labelBkOut ){
130794 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
130795 }
130796 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
130797 nKey+1+nColumn+nRefKey);
130798 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
130799 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
130800 VdbeCoverage(v);
130801 codeOffset(v, p->iOffset, addrContinue);
130802 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
130803 bSeq = 0;
130804 }else{
130805 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
130806 codeOffset(v, p->iOffset, addrContinue);
130807 iSortTab = iTab;
130808 bSeq = 1;
130809 }
130810 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
130811#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130812 if( aOutEx[i].bSorterRef ) continue;
130813#endif
130814 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
130815 }
130816#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130817 if( pSort->nDefer ){
130818 int iKey = iCol+1;
130819 int regKey = sqlite3GetTempRange(pParse, nRefKey);
130820
130821 for(i=0; i<pSort->nDefer; i++){
130822 int iCsr = pSort->aDefer[i].iCsr;
130823 Table *pTab = pSort->aDefer[i].pTab;
130824 int nKey = pSort->aDefer[i].nKey;
130825
130826 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
130827 if( HasRowid(pTab) ){
130828 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
130829 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
130830 sqlite3VdbeCurrentAddr(v)+1, regKey);
130831 }else{
130832 int k;
130833 int iJmp;
130834 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
130835 for(k=0; k<nKey; k++){
130836 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
130837 }
130838 iJmp = sqlite3VdbeCurrentAddr(v);
130839 sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
130840 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
130841 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
130842 }
130843 }
130844 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
130845 }
130846#endif
130847 for(i=nColumn-1; i>=0; i--){
130848#ifdef SQLITE_ENABLE_SORTER_REFERENCES
130849 if( aOutEx[i].bSorterRef ){
130850 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
130851 }else
130852#endif
130853 {
130854 int iRead;
130855 if( aOutEx[i].u.x.iOrderByCol ){
130856 iRead = aOutEx[i].u.x.iOrderByCol-1;
130857 }else{
130858 iRead = iCol--;
130859 }
130860 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
130861 VdbeComment((v, "%s", aOutEx[i].zEName));
130862 }
130863 }
130864 switch( eDest ){
130865 case SRT_Table:
130866 case SRT_EphemTab: {
130867 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
130868 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
130869 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
130870 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
130871 break;
130872 }
130873#ifndef SQLITE_OMIT_SUBQUERY
130874 case SRT_Set: {
130875 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
130876 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
130877 pDest->zAffSdst, nColumn);
130878 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
130879 break;
130880 }
130881 case SRT_Mem: {
130882 /* The LIMIT clause will terminate the loop for us */
130883 break;
130884 }
130885#endif
130886 case SRT_Upfrom: {
130887 int i2 = pDest->iSDParm2;
130888 int r1 = sqlite3GetTempReg(pParse);
130889 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
130890 if( i2<0 ){
130891 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regRow);
130892 }else{
130893 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regRow, i2);
130894 }
130895 break;
130896 }
130897 default: {
130898 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
130899 testcase( eDest==SRT_Output );
130900 testcase( eDest==SRT_Coroutine );
130901 if( eDest==SRT_Output ){
130902 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
130903 }else{
130904 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
130905 }
130906 break;
130907 }
130908 }
130909 if( regRowid ){
130910 if( eDest==SRT_Set ){
130911 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
130912 }else{
130913 sqlite3ReleaseTempReg(pParse, regRow);
130914 }
130915 sqlite3ReleaseTempReg(pParse, regRowid);
130916 }
130917 /* The bottom of the loop
130918 */
130919 sqlite3VdbeResolveLabel(v, addrContinue);
130920 if( pSort->sortFlags & SORTFLAG_UseSorter ){
130921 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
130922 }else{
130923 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
130924 }
130925 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
130926 sqlite3VdbeResolveLabel(v, addrBreak);
130927}
130928
130929/*
130930** Return a pointer to a string containing the 'declaration type' of the
130931** expression pExpr. The string may be treated as static by the caller.
130932**
130933** Also try to estimate the size of the returned value and return that
130934** result in *pEstWidth.
130935**
130936** The declaration type is the exact datatype definition extracted from the
130937** original CREATE TABLE statement if the expression is a column. The
130938** declaration type for a ROWID field is INTEGER. Exactly when an expression
130939** is considered a column can be complex in the presence of subqueries. The
130940** result-set expression in all of the following SELECT statements is
130941** considered a column by this function.
130942**
130943** SELECT col FROM tbl;
130944** SELECT (SELECT col FROM tbl;
130945** SELECT (SELECT col FROM tbl);
130946** SELECT abc FROM (SELECT col AS abc FROM tbl);
130947**
130948** The declaration type for any expression other than a column is NULL.
130949**
130950** This routine has either 3 or 6 parameters depending on whether or not
130951** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
130952*/
130953#ifdef SQLITE_ENABLE_COLUMN_METADATA
130954# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
130955#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
130956# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
130957#endif
130958static const char *columnTypeImpl(
130959 NameContext *pNC,
130960#ifndef SQLITE_ENABLE_COLUMN_METADATA
130961 Expr *pExpr
130962#else
130963 Expr *pExpr,
130964 const char **pzOrigDb,
130965 const char **pzOrigTab,
130966 const char **pzOrigCol
130967#endif
130968){
130969 char const *zType = 0;
130970 int j;
130971#ifdef SQLITE_ENABLE_COLUMN_METADATA
130972 char const *zOrigDb = 0;
130973 char const *zOrigTab = 0;
130974 char const *zOrigCol = 0;
130975#endif
130976
130977 assert( pExpr!=0 );
130978 assert( pNC->pSrcList!=0 );
130979 switch( pExpr->op ){
130980 case TK_COLUMN: {
130981 /* The expression is a column. Locate the table the column is being
130982 ** extracted from in NameContext.pSrcList. This table may be real
130983 ** database table or a subquery.
130984 */
130985 Table *pTab = 0; /* Table structure column is extracted from */
130986 Select *pS = 0; /* Select the column is extracted from */
130987 int iCol = pExpr->iColumn; /* Index of column in pTab */
130988 while( pNC && !pTab ){
130989 SrcList *pTabList = pNC->pSrcList;
130990 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
130991 if( j<pTabList->nSrc ){
130992 pTab = pTabList->a[j].pTab;
130993 pS = pTabList->a[j].pSelect;
130994 }else{
130995 pNC = pNC->pNext;
130996 }
130997 }
130998
130999 if( pTab==0 ){
131000 /* At one time, code such as "SELECT new.x" within a trigger would
131001 ** cause this condition to run. Since then, we have restructured how
131002 ** trigger code is generated and so this condition is no longer
131003 ** possible. However, it can still be true for statements like
131004 ** the following:
131005 **
131006 ** CREATE TABLE t1(col INTEGER);
131007 ** SELECT (SELECT t1.col) FROM FROM t1;
131008 **
131009 ** when columnType() is called on the expression "t1.col" in the
131010 ** sub-select. In this case, set the column type to NULL, even
131011 ** though it should really be "INTEGER".
131012 **
131013 ** This is not a problem, as the column type of "t1.col" is never
131014 ** used. When columnType() is called on the expression
131015 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
131016 ** branch below. */
131017 break;
131018 }
131019
131020 assert( pTab && pExpr->y.pTab==pTab );
131021 if( pS ){
131022 /* The "table" is actually a sub-select or a view in the FROM clause
131023 ** of the SELECT statement. Return the declaration type and origin
131024 ** data for the result-set column of the sub-select.
131025 */
131026 if( iCol>=0 && iCol<pS->pEList->nExpr ){
131027 /* If iCol is less than zero, then the expression requests the
131028 ** rowid of the sub-select or view. This expression is legal (see
131029 ** test case misc2.2.2) - it always evaluates to NULL.
131030 */
131031 NameContext sNC;
131032 Expr *p = pS->pEList->a[iCol].pExpr;
131033 sNC.pSrcList = pS->pSrc;
131034 sNC.pNext = pNC;
131035 sNC.pParse = pNC->pParse;
131036 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
131037 }
131038 }else{
131039 /* A real table or a CTE table */
131040 assert( !pS );
131041#ifdef SQLITE_ENABLE_COLUMN_METADATA
131042 if( iCol<0 ) iCol = pTab->iPKey;
131043 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
131044 if( iCol<0 ){
131045 zType = "INTEGER";
131046 zOrigCol = "rowid";
131047 }else{
131048 zOrigCol = pTab->aCol[iCol].zName;
131049 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
131050 }
131051 zOrigTab = pTab->zName;
131052 if( pNC->pParse && pTab->pSchema ){
131053 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
131054 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
131055 }
131056#else
131057 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
131058 if( iCol<0 ){
131059 zType = "INTEGER";
131060 }else{
131061 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
131062 }
131063#endif
131064 }
131065 break;
131066 }
131067#ifndef SQLITE_OMIT_SUBQUERY
131068 case TK_SELECT: {
131069 /* The expression is a sub-select. Return the declaration type and
131070 ** origin info for the single column in the result set of the SELECT
131071 ** statement.
131072 */
131073 NameContext sNC;
131074 Select *pS = pExpr->x.pSelect;
131075 Expr *p = pS->pEList->a[0].pExpr;
131076 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
131077 sNC.pSrcList = pS->pSrc;
131078 sNC.pNext = pNC;
131079 sNC.pParse = pNC->pParse;
131080 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
131081 break;
131082 }
131083#endif
131084 }
131085
131086#ifdef SQLITE_ENABLE_COLUMN_METADATA
131087 if( pzOrigDb ){
131088 assert( pzOrigTab && pzOrigCol );
131089 *pzOrigDb = zOrigDb;
131090 *pzOrigTab = zOrigTab;
131091 *pzOrigCol = zOrigCol;
131092 }
131093#endif
131094 return zType;
131095}
131096
131097/*
131098** Generate code that will tell the VDBE the declaration types of columns
131099** in the result set.
131100*/
131101static void generateColumnTypes(
131102 Parse *pParse, /* Parser context */
131103 SrcList *pTabList, /* List of tables */
131104 ExprList *pEList /* Expressions defining the result set */
131105){
131106#ifndef SQLITE_OMIT_DECLTYPE
131107 Vdbe *v = pParse->pVdbe;
131108 int i;
131109 NameContext sNC;
131110 sNC.pSrcList = pTabList;
131111 sNC.pParse = pParse;
131112 sNC.pNext = 0;
131113 for(i=0; i<pEList->nExpr; i++){
131114 Expr *p = pEList->a[i].pExpr;
131115 const char *zType;
131116#ifdef SQLITE_ENABLE_COLUMN_METADATA
131117 const char *zOrigDb = 0;
131118 const char *zOrigTab = 0;
131119 const char *zOrigCol = 0;
131120 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
131121
131122 /* The vdbe must make its own copy of the column-type and other
131123 ** column specific strings, in case the schema is reset before this
131124 ** virtual machine is deleted.
131125 */
131126 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
131127 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
131128 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
131129#else
131130 zType = columnType(&sNC, p, 0, 0, 0);
131131#endif
131132 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
131133 }
131134#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
131135}
131136
131137
131138/*
131139** Compute the column names for a SELECT statement.
131140**
131141** The only guarantee that SQLite makes about column names is that if the
131142** column has an AS clause assigning it a name, that will be the name used.
131143** That is the only documented guarantee. However, countless applications
131144** developed over the years have made baseless assumptions about column names
131145** and will break if those assumptions changes. Hence, use extreme caution
131146** when modifying this routine to avoid breaking legacy.
131147**
131148** See Also: sqlite3ColumnsFromExprList()
131149**
131150** The PRAGMA short_column_names and PRAGMA full_column_names settings are
131151** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
131152** applications should operate this way. Nevertheless, we need to support the
131153** other modes for legacy:
131154**
131155** short=OFF, full=OFF: Column name is the text of the expression has it
131156** originally appears in the SELECT statement. In
131157** other words, the zSpan of the result expression.
131158**
131159** short=ON, full=OFF: (This is the default setting). If the result
131160** refers directly to a table column, then the
131161** result column name is just the table column
131162** name: COLUMN. Otherwise use zSpan.
131163**
131164** full=ON, short=ANY: If the result refers directly to a table column,
131165** then the result column name with the table name
131166** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
131167*/
131168static void generateColumnNames(
131169 Parse *pParse, /* Parser context */
131170 Select *pSelect /* Generate column names for this SELECT statement */
131171){
131172 Vdbe *v = pParse->pVdbe;
131173 int i;
131174 Table *pTab;
131175 SrcList *pTabList;
131176 ExprList *pEList;
131177 sqlite3 *db = pParse->db;
131178 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
131179 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
131180
131181#ifndef SQLITE_OMIT_EXPLAIN
131182 /* If this is an EXPLAIN, skip this step */
131183 if( pParse->explain ){
131184 return;
131185 }
131186#endif
131187
131188 if( pParse->colNamesSet ) return;
131189 /* Column names are determined by the left-most term of a compound select */
131190 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
131191 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
131192 pTabList = pSelect->pSrc;
131193 pEList = pSelect->pEList;
131194 assert( v!=0 );
131195 assert( pTabList!=0 );
131196 pParse->colNamesSet = 1;
131197 fullName = (db->flags & SQLITE_FullColNames)!=0;
131198 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
131199 sqlite3VdbeSetNumCols(v, pEList->nExpr);
131200 for(i=0; i<pEList->nExpr; i++){
131201 Expr *p = pEList->a[i].pExpr;
131202
131203 assert( p!=0 );
131204 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
131205 assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
131206 if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
131207 /* An AS clause always takes first priority */
131208 char *zName = pEList->a[i].zEName;
131209 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
131210 }else if( srcName && p->op==TK_COLUMN ){
131211 char *zCol;
131212 int iCol = p->iColumn;
131213 pTab = p->y.pTab;
131214 assert( pTab!=0 );
131215 if( iCol<0 ) iCol = pTab->iPKey;
131216 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
131217 if( iCol<0 ){
131218 zCol = "rowid";
131219 }else{
131220 zCol = pTab->aCol[iCol].zName;
131221 }
131222 if( fullName ){
131223 char *zName = 0;
131224 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
131225 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
131226 }else{
131227 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
131228 }
131229 }else{
131230 const char *z = pEList->a[i].zEName;
131231 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
131232 sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
131233 }
131234 }
131235 generateColumnTypes(pParse, pTabList, pEList);
131236}
131237
131238/*
131239** Given an expression list (which is really the list of expressions
131240** that form the result set of a SELECT statement) compute appropriate
131241** column names for a table that would hold the expression list.
131242**
131243** All column names will be unique.
131244**
131245** Only the column names are computed. Column.zType, Column.zColl,
131246** and other fields of Column are zeroed.
131247**
131248** Return SQLITE_OK on success. If a memory allocation error occurs,
131249** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
131250**
131251** The only guarantee that SQLite makes about column names is that if the
131252** column has an AS clause assigning it a name, that will be the name used.
131253** That is the only documented guarantee. However, countless applications
131254** developed over the years have made baseless assumptions about column names
131255** and will break if those assumptions changes. Hence, use extreme caution
131256** when modifying this routine to avoid breaking legacy.
131257**
131258** See Also: generateColumnNames()
131259*/
131260SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
131261 Parse *pParse, /* Parsing context */
131262 ExprList *pEList, /* Expr list from which to derive column names */
131263 i16 *pnCol, /* Write the number of columns here */
131264 Column **paCol /* Write the new column list here */
131265){
131266 sqlite3 *db = pParse->db; /* Database connection */
131267 int i, j; /* Loop counters */
131268 u32 cnt; /* Index added to make the name unique */
131269 Column *aCol, *pCol; /* For looping over result columns */
131270 int nCol; /* Number of columns in the result set */
131271 char *zName; /* Column name */
131272 int nName; /* Size of name in zName[] */
131273 Hash ht; /* Hash table of column names */
131274
131275 sqlite3HashInit(&ht);
131276 if( pEList ){
131277 nCol = pEList->nExpr;
131278 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
131279 testcase( aCol==0 );
131280 if( nCol>32767 ) nCol = 32767;
131281 }else{
131282 nCol = 0;
131283 aCol = 0;
131284 }
131285 assert( nCol==(i16)nCol );
131286 *pnCol = nCol;
131287 *paCol = aCol;
131288
131289 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
131290 /* Get an appropriate name for the column
131291 */
131292 if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
131293 /* If the column contains an "AS <name>" phrase, use <name> as the name */
131294 }else{
131295 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
131296 while( pColExpr->op==TK_DOT ){
131297 pColExpr = pColExpr->pRight;
131298 assert( pColExpr!=0 );
131299 }
131300 if( pColExpr->op==TK_COLUMN ){
131301 /* For columns use the column name name */
131302 int iCol = pColExpr->iColumn;
131303 Table *pTab = pColExpr->y.pTab;
131304 assert( pTab!=0 );
131305 if( iCol<0 ) iCol = pTab->iPKey;
131306 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
131307 }else if( pColExpr->op==TK_ID ){
131308 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
131309 zName = pColExpr->u.zToken;
131310 }else{
131311 /* Use the original text of the column expression as its name */
131312 zName = pEList->a[i].zEName;
131313 }
131314 }
131315 if( zName && !sqlite3IsTrueOrFalse(zName) ){
131316 zName = sqlite3DbStrDup(db, zName);
131317 }else{
131318 zName = sqlite3MPrintf(db,"column%d",i+1);
131319 }
131320
131321 /* Make sure the column name is unique. If the name is not unique,
131322 ** append an integer to the name so that it becomes unique.
131323 */
131324 cnt = 0;
131325 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
131326 nName = sqlite3Strlen30(zName);
131327 if( nName>0 ){
131328 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
131329 if( zName[j]==':' ) nName = j;
131330 }
131331 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
131332 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
131333 }
131334 pCol->zName = zName;
131335 pCol->hName = sqlite3StrIHash(zName);
131336 sqlite3ColumnPropertiesFromName(0, pCol);
131337 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
131338 sqlite3OomFault(db);
131339 }
131340 }
131341 sqlite3HashClear(&ht);
131342 if( db->mallocFailed ){
131343 for(j=0; j<i; j++){
131344 sqlite3DbFree(db, aCol[j].zName);
131345 }
131346 sqlite3DbFree(db, aCol);
131347 *paCol = 0;
131348 *pnCol = 0;
131349 return SQLITE_NOMEM_BKPT;
131350 }
131351 return SQLITE_OK;
131352}
131353
131354/*
131355** Add type and collation information to a column list based on
131356** a SELECT statement.
131357**
131358** The column list presumably came from selectColumnNamesFromExprList().
131359** The column list has only names, not types or collations. This
131360** routine goes through and adds the types and collations.
131361**
131362** This routine requires that all identifiers in the SELECT
131363** statement be resolved.
131364*/
131365SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
131366 Parse *pParse, /* Parsing contexts */
131367 Table *pTab, /* Add column type information to this table */
131368 Select *pSelect, /* SELECT used to determine types and collations */
131369 char aff /* Default affinity for columns */
131370){
131371 sqlite3 *db = pParse->db;
131372 NameContext sNC;
131373 Column *pCol;
131374 CollSeq *pColl;
131375 int i;
131376 Expr *p;
131377 struct ExprList_item *a;
131378
131379 assert( pSelect!=0 );
131380 assert( (pSelect->selFlags & SF_Resolved)!=0 );
131381 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
131382 if( db->mallocFailed ) return;
131383 memset(&sNC, 0, sizeof(sNC));
131384 sNC.pSrcList = pSelect->pSrc;
131385 a = pSelect->pEList->a;
131386 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
131387 const char *zType;
131388 int n, m;
131389 p = a[i].pExpr;
131390 zType = columnType(&sNC, p, 0, 0, 0);
131391 /* pCol->szEst = ... // Column size est for SELECT tables never used */
131392 pCol->affinity = sqlite3ExprAffinity(p);
131393 if( zType ){
131394 m = sqlite3Strlen30(zType);
131395 n = sqlite3Strlen30(pCol->zName);
131396 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
131397 if( pCol->zName ){
131398 memcpy(&pCol->zName[n+1], zType, m+1);
131399 pCol->colFlags |= COLFLAG_HASTYPE;
131400 }
131401 }
131402 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
131403 pColl = sqlite3ExprCollSeq(pParse, p);
131404 if( pColl && pCol->zColl==0 ){
131405 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
131406 }
131407 }
131408 pTab->szTabRow = 1; /* Any non-zero value works */
131409}
131410
131411/*
131412** Given a SELECT statement, generate a Table structure that describes
131413** the result set of that SELECT.
131414*/
131415SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
131416 Table *pTab;
131417 sqlite3 *db = pParse->db;
131418 u64 savedFlags;
131419
131420 savedFlags = db->flags;
131421 db->flags &= ~(u64)SQLITE_FullColNames;
131422 db->flags |= SQLITE_ShortColNames;
131423 sqlite3SelectPrep(pParse, pSelect, 0);
131424 db->flags = savedFlags;
131425 if( pParse->nErr ) return 0;
131426 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
131427 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
131428 if( pTab==0 ){
131429 return 0;
131430 }
131431 pTab->nTabRef = 1;
131432 pTab->zName = 0;
131433 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
131434 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
131435 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
131436 pTab->iPKey = -1;
131437 if( db->mallocFailed ){
131438 sqlite3DeleteTable(db, pTab);
131439 return 0;
131440 }
131441 return pTab;
131442}
131443
131444/*
131445** Get a VDBE for the given parser context. Create a new one if necessary.
131446** If an error occurs, return NULL and leave a message in pParse.
131447*/
131448SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
131449 if( pParse->pVdbe ){
131450 return pParse->pVdbe;
131451 }
131452 if( pParse->pToplevel==0
131453 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
131454 ){
131455 pParse->okConstFactor = 1;
131456 }
131457 return sqlite3VdbeCreate(pParse);
131458}
131459
131460
131461/*
131462** Compute the iLimit and iOffset fields of the SELECT based on the
131463** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
131464** that appear in the original SQL statement after the LIMIT and OFFSET
131465** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
131466** are the integer memory register numbers for counters used to compute
131467** the limit and offset. If there is no limit and/or offset, then
131468** iLimit and iOffset are negative.
131469**
131470** This routine changes the values of iLimit and iOffset only if
131471** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
131472** and iOffset should have been preset to appropriate default values (zero)
131473** prior to calling this routine.
131474**
131475** The iOffset register (if it exists) is initialized to the value
131476** of the OFFSET. The iLimit register is initialized to LIMIT. Register
131477** iOffset+1 is initialized to LIMIT+OFFSET.
131478**
131479** Only if pLimit->pLeft!=0 do the limit registers get
131480** redefined. The UNION ALL operator uses this property to force
131481** the reuse of the same limit and offset registers across multiple
131482** SELECT statements.
131483*/
131484static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
131485 Vdbe *v = 0;
131486 int iLimit = 0;
131487 int iOffset;
131488 int n;
131489 Expr *pLimit = p->pLimit;
131490
131491 if( p->iLimit ) return;
131492
131493 /*
131494 ** "LIMIT -1" always shows all rows. There is some
131495 ** controversy about what the correct behavior should be.
131496 ** The current implementation interprets "LIMIT 0" to mean
131497 ** no rows.
131498 */
131499 if( pLimit ){
131500 assert( pLimit->op==TK_LIMIT );
131501 assert( pLimit->pLeft!=0 );
131502 p->iLimit = iLimit = ++pParse->nMem;
131503 v = sqlite3GetVdbe(pParse);
131504 assert( v!=0 );
131505 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
131506 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
131507 VdbeComment((v, "LIMIT counter"));
131508 if( n==0 ){
131509 sqlite3VdbeGoto(v, iBreak);
131510 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
131511 p->nSelectRow = sqlite3LogEst((u64)n);
131512 p->selFlags |= SF_FixedLimit;
131513 }
131514 }else{
131515 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
131516 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
131517 VdbeComment((v, "LIMIT counter"));
131518 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
131519 }
131520 if( pLimit->pRight ){
131521 p->iOffset = iOffset = ++pParse->nMem;
131522 pParse->nMem++; /* Allocate an extra register for limit+offset */
131523 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
131524 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
131525 VdbeComment((v, "OFFSET counter"));
131526 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
131527 VdbeComment((v, "LIMIT+OFFSET"));
131528 }
131529 }
131530}
131531
131532#ifndef SQLITE_OMIT_COMPOUND_SELECT
131533/*
131534** Return the appropriate collating sequence for the iCol-th column of
131535** the result set for the compound-select statement "p". Return NULL if
131536** the column has no default collating sequence.
131537**
131538** The collating sequence for the compound select is taken from the
131539** left-most term of the select that has a collating sequence.
131540*/
131541static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
131542 CollSeq *pRet;
131543 if( p->pPrior ){
131544 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
131545 }else{
131546 pRet = 0;
131547 }
131548 assert( iCol>=0 );
131549 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
131550 ** have been thrown during name resolution and we would not have gotten
131551 ** this far */
131552 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
131553 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
131554 }
131555 return pRet;
131556}
131557
131558/*
131559** The select statement passed as the second parameter is a compound SELECT
131560** with an ORDER BY clause. This function allocates and returns a KeyInfo
131561** structure suitable for implementing the ORDER BY.
131562**
131563** Space to hold the KeyInfo structure is obtained from malloc. The calling
131564** function is responsible for ensuring that this structure is eventually
131565** freed.
131566*/
131567static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
131568 ExprList *pOrderBy = p->pOrderBy;
131569 int nOrderBy = p->pOrderBy->nExpr;
131570 sqlite3 *db = pParse->db;
131571 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
131572 if( pRet ){
131573 int i;
131574 for(i=0; i<nOrderBy; i++){
131575 struct ExprList_item *pItem = &pOrderBy->a[i];
131576 Expr *pTerm = pItem->pExpr;
131577 CollSeq *pColl;
131578
131579 if( pTerm->flags & EP_Collate ){
131580 pColl = sqlite3ExprCollSeq(pParse, pTerm);
131581 }else{
131582 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
131583 if( pColl==0 ) pColl = db->pDfltColl;
131584 pOrderBy->a[i].pExpr =
131585 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
131586 }
131587 assert( sqlite3KeyInfoIsWriteable(pRet) );
131588 pRet->aColl[i] = pColl;
131589 pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
131590 }
131591 }
131592
131593 return pRet;
131594}
131595
131596#ifndef SQLITE_OMIT_CTE
131597/*
131598** This routine generates VDBE code to compute the content of a WITH RECURSIVE
131599** query of the form:
131600**
131601** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
131602** \___________/ \_______________/
131603** p->pPrior p
131604**
131605**
131606** There is exactly one reference to the recursive-table in the FROM clause
131607** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
131608**
131609** The setup-query runs once to generate an initial set of rows that go
131610** into a Queue table. Rows are extracted from the Queue table one by
131611** one. Each row extracted from Queue is output to pDest. Then the single
131612** extracted row (now in the iCurrent table) becomes the content of the
131613** recursive-table for a recursive-query run. The output of the recursive-query
131614** is added back into the Queue table. Then another row is extracted from Queue
131615** and the iteration continues until the Queue table is empty.
131616**
131617** If the compound query operator is UNION then no duplicate rows are ever
131618** inserted into the Queue table. The iDistinct table keeps a copy of all rows
131619** that have ever been inserted into Queue and causes duplicates to be
131620** discarded. If the operator is UNION ALL, then duplicates are allowed.
131621**
131622** If the query has an ORDER BY, then entries in the Queue table are kept in
131623** ORDER BY order and the first entry is extracted for each cycle. Without
131624** an ORDER BY, the Queue table is just a FIFO.
131625**
131626** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
131627** have been output to pDest. A LIMIT of zero means to output no rows and a
131628** negative LIMIT means to output all rows. If there is also an OFFSET clause
131629** with a positive value, then the first OFFSET outputs are discarded rather
131630** than being sent to pDest. The LIMIT count does not begin until after OFFSET
131631** rows have been skipped.
131632*/
131633static void generateWithRecursiveQuery(
131634 Parse *pParse, /* Parsing context */
131635 Select *p, /* The recursive SELECT to be coded */
131636 SelectDest *pDest /* What to do with query results */
131637){
131638 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
131639 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
131640 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
131641 Select *pSetup = p->pPrior; /* The setup query */
131642 int addrTop; /* Top of the loop */
131643 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
131644 int iCurrent = 0; /* The Current table */
131645 int regCurrent; /* Register holding Current table */
131646 int iQueue; /* The Queue table */
131647 int iDistinct = 0; /* To ensure unique results if UNION */
131648 int eDest = SRT_Fifo; /* How to write to Queue */
131649 SelectDest destQueue; /* SelectDest targetting the Queue table */
131650 int i; /* Loop counter */
131651 int rc; /* Result code */
131652 ExprList *pOrderBy; /* The ORDER BY clause */
131653 Expr *pLimit; /* Saved LIMIT and OFFSET */
131654 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
131655
131656#ifndef SQLITE_OMIT_WINDOWFUNC
131657 if( p->pWin ){
131658 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
131659 return;
131660 }
131661#endif
131662
131663 /* Obtain authorization to do a recursive query */
131664 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
131665
131666 /* Process the LIMIT and OFFSET clauses, if they exist */
131667 addrBreak = sqlite3VdbeMakeLabel(pParse);
131668 p->nSelectRow = 320; /* 4 billion rows */
131669 computeLimitRegisters(pParse, p, addrBreak);
131670 pLimit = p->pLimit;
131671 regLimit = p->iLimit;
131672 regOffset = p->iOffset;
131673 p->pLimit = 0;
131674 p->iLimit = p->iOffset = 0;
131675 pOrderBy = p->pOrderBy;
131676
131677 /* Locate the cursor number of the Current table */
131678 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
131679 if( pSrc->a[i].fg.isRecursive ){
131680 iCurrent = pSrc->a[i].iCursor;
131681 break;
131682 }
131683 }
131684
131685 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
131686 ** the Distinct table must be exactly one greater than Queue in order
131687 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
131688 iQueue = pParse->nTab++;
131689 if( p->op==TK_UNION ){
131690 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
131691 iDistinct = pParse->nTab++;
131692 }else{
131693 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
131694 }
131695 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
131696
131697 /* Allocate cursors for Current, Queue, and Distinct. */
131698 regCurrent = ++pParse->nMem;
131699 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
131700 if( pOrderBy ){
131701 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
131702 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
131703 (char*)pKeyInfo, P4_KEYINFO);
131704 destQueue.pOrderBy = pOrderBy;
131705 }else{
131706 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
131707 }
131708 VdbeComment((v, "Queue table"));
131709 if( iDistinct ){
131710 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
131711 p->selFlags |= SF_UsesEphemeral;
131712 }
131713
131714 /* Detach the ORDER BY clause from the compound SELECT */
131715 p->pOrderBy = 0;
131716
131717 /* Store the results of the setup-query in Queue. */
131718 pSetup->pNext = 0;
131719 ExplainQueryPlan((pParse, 1, "SETUP"));
131720 rc = sqlite3Select(pParse, pSetup, &destQueue);
131721 pSetup->pNext = p;
131722 if( rc ) goto end_of_recursive_query;
131723
131724 /* Find the next row in the Queue and output that row */
131725 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
131726
131727 /* Transfer the next row in Queue over to Current */
131728 sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
131729 if( pOrderBy ){
131730 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
131731 }else{
131732 sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
131733 }
131734 sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
131735
131736 /* Output the single row in Current */
131737 addrCont = sqlite3VdbeMakeLabel(pParse);
131738 codeOffset(v, regOffset, addrCont);
131739 selectInnerLoop(pParse, p, iCurrent,
131740 0, 0, pDest, addrCont, addrBreak);
131741 if( regLimit ){
131742 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
131743 VdbeCoverage(v);
131744 }
131745 sqlite3VdbeResolveLabel(v, addrCont);
131746
131747 /* Execute the recursive SELECT taking the single row in Current as
131748 ** the value for the recursive-table. Store the results in the Queue.
131749 */
131750 if( p->selFlags & SF_Aggregate ){
131751 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
131752 }else{
131753 p->pPrior = 0;
131754 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
131755 sqlite3Select(pParse, p, &destQueue);
131756 assert( p->pPrior==0 );
131757 p->pPrior = pSetup;
131758 }
131759
131760 /* Keep running the loop until the Queue is empty */
131761 sqlite3VdbeGoto(v, addrTop);
131762 sqlite3VdbeResolveLabel(v, addrBreak);
131763
131764end_of_recursive_query:
131765 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
131766 p->pOrderBy = pOrderBy;
131767 p->pLimit = pLimit;
131768 return;
131769}
131770#endif /* SQLITE_OMIT_CTE */
131771
131772/* Forward references */
131773static int multiSelectOrderBy(
131774 Parse *pParse, /* Parsing context */
131775 Select *p, /* The right-most of SELECTs to be coded */
131776 SelectDest *pDest /* What to do with query results */
131777);
131778
131779/*
131780** Handle the special case of a compound-select that originates from a
131781** VALUES clause. By handling this as a special case, we avoid deep
131782** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
131783** on a VALUES clause.
131784**
131785** Because the Select object originates from a VALUES clause:
131786** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
131787** (2) All terms are UNION ALL
131788** (3) There is no ORDER BY clause
131789**
131790** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
131791** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
131792** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
131793** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
131794*/
131795static int multiSelectValues(
131796 Parse *pParse, /* Parsing context */
131797 Select *p, /* The right-most of SELECTs to be coded */
131798 SelectDest *pDest /* What to do with query results */
131799){
131800 int nRow = 1;
131801 int rc = 0;
131802 int bShowAll = p->pLimit==0;
131803 assert( p->selFlags & SF_MultiValue );
131804 do{
131805 assert( p->selFlags & SF_Values );
131806 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
131807 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
131808#ifndef SQLITE_OMIT_WINDOWFUNC
131809 if( p->pWin ) return -1;
131810#endif
131811 if( p->pPrior==0 ) break;
131812 assert( p->pPrior->pNext==p );
131813 p = p->pPrior;
131814 nRow += bShowAll;
131815 }while(1);
131816 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
131817 nRow==1 ? "" : "S"));
131818 while( p ){
131819 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
131820 if( !bShowAll ) break;
131821 p->nSelectRow = nRow;
131822 p = p->pNext;
131823 }
131824 return rc;
131825}
131826
131827/*
131828** This routine is called to process a compound query form from
131829** two or more separate queries using UNION, UNION ALL, EXCEPT, or
131830** INTERSECT
131831**
131832** "p" points to the right-most of the two queries. the query on the
131833** left is p->pPrior. The left query could also be a compound query
131834** in which case this routine will be called recursively.
131835**
131836** The results of the total query are to be written into a destination
131837** of type eDest with parameter iParm.
131838**
131839** Example 1: Consider a three-way compound SQL statement.
131840**
131841** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
131842**
131843** This statement is parsed up as follows:
131844**
131845** SELECT c FROM t3
131846** |
131847** `-----> SELECT b FROM t2
131848** |
131849** `------> SELECT a FROM t1
131850**
131851** The arrows in the diagram above represent the Select.pPrior pointer.
131852** So if this routine is called with p equal to the t3 query, then
131853** pPrior will be the t2 query. p->op will be TK_UNION in this case.
131854**
131855** Notice that because of the way SQLite parses compound SELECTs, the
131856** individual selects always group from left to right.
131857*/
131858static int multiSelect(
131859 Parse *pParse, /* Parsing context */
131860 Select *p, /* The right-most of SELECTs to be coded */
131861 SelectDest *pDest /* What to do with query results */
131862){
131863 int rc = SQLITE_OK; /* Success code from a subroutine */
131864 Select *pPrior; /* Another SELECT immediately to our left */
131865 Vdbe *v; /* Generate code to this VDBE */
131866 SelectDest dest; /* Alternative data destination */
131867 Select *pDelete = 0; /* Chain of simple selects to delete */
131868 sqlite3 *db; /* Database connection */
131869
131870 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
131871 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
131872 */
131873 assert( p && p->pPrior ); /* Calling function guarantees this much */
131874 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
131875 assert( p->selFlags & SF_Compound );
131876 db = pParse->db;
131877 pPrior = p->pPrior;
131878 dest = *pDest;
131879 if( pPrior->pOrderBy || pPrior->pLimit ){
131880 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
131881 pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
131882 rc = 1;
131883 goto multi_select_end;
131884 }
131885
131886 v = sqlite3GetVdbe(pParse);
131887 assert( v!=0 ); /* The VDBE already created by calling function */
131888
131889 /* Create the destination temporary table if necessary
131890 */
131891 if( dest.eDest==SRT_EphemTab ){
131892 assert( p->pEList );
131893 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
131894 dest.eDest = SRT_Table;
131895 }
131896
131897 /* Special handling for a compound-select that originates as a VALUES clause.
131898 */
131899 if( p->selFlags & SF_MultiValue ){
131900 rc = multiSelectValues(pParse, p, &dest);
131901 if( rc>=0 ) goto multi_select_end;
131902 rc = SQLITE_OK;
131903 }
131904
131905 /* Make sure all SELECTs in the statement have the same number of elements
131906 ** in their result sets.
131907 */
131908 assert( p->pEList && pPrior->pEList );
131909 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
131910
131911#ifndef SQLITE_OMIT_CTE
131912 if( p->selFlags & SF_Recursive ){
131913 generateWithRecursiveQuery(pParse, p, &dest);
131914 }else
131915#endif
131916
131917 /* Compound SELECTs that have an ORDER BY clause are handled separately.
131918 */
131919 if( p->pOrderBy ){
131920 return multiSelectOrderBy(pParse, p, pDest);
131921 }else{
131922
131923#ifndef SQLITE_OMIT_EXPLAIN
131924 if( pPrior->pPrior==0 ){
131925 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
131926 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
131927 }
131928#endif
131929
131930 /* Generate code for the left and right SELECT statements.
131931 */
131932 switch( p->op ){
131933 case TK_ALL: {
131934 int addr = 0;
131935 int nLimit;
131936 assert( !pPrior->pLimit );
131937 pPrior->iLimit = p->iLimit;
131938 pPrior->iOffset = p->iOffset;
131939 pPrior->pLimit = p->pLimit;
131940 rc = sqlite3Select(pParse, pPrior, &dest);
131941 p->pLimit = 0;
131942 if( rc ){
131943 goto multi_select_end;
131944 }
131945 p->pPrior = 0;
131946 p->iLimit = pPrior->iLimit;
131947 p->iOffset = pPrior->iOffset;
131948 if( p->iLimit ){
131949 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
131950 VdbeComment((v, "Jump ahead if LIMIT reached"));
131951 if( p->iOffset ){
131952 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
131953 p->iLimit, p->iOffset+1, p->iOffset);
131954 }
131955 }
131956 ExplainQueryPlan((pParse, 1, "UNION ALL"));
131957 rc = sqlite3Select(pParse, p, &dest);
131958 testcase( rc!=SQLITE_OK );
131959 pDelete = p->pPrior;
131960 p->pPrior = pPrior;
131961 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
131962 if( pPrior->pLimit
131963 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
131964 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
131965 ){
131966 p->nSelectRow = sqlite3LogEst((u64)nLimit);
131967 }
131968 if( addr ){
131969 sqlite3VdbeJumpHere(v, addr);
131970 }
131971 break;
131972 }
131973 case TK_EXCEPT:
131974 case TK_UNION: {
131975 int unionTab; /* Cursor number of the temp table holding result */
131976 u8 op = 0; /* One of the SRT_ operations to apply to self */
131977 int priorOp; /* The SRT_ operation to apply to prior selects */
131978 Expr *pLimit; /* Saved values of p->nLimit */
131979 int addr;
131980 SelectDest uniondest;
131981
131982 testcase( p->op==TK_EXCEPT );
131983 testcase( p->op==TK_UNION );
131984 priorOp = SRT_Union;
131985 if( dest.eDest==priorOp ){
131986 /* We can reuse a temporary table generated by a SELECT to our
131987 ** right.
131988 */
131989 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
131990 unionTab = dest.iSDParm;
131991 }else{
131992 /* We will need to create our own temporary table to hold the
131993 ** intermediate results.
131994 */
131995 unionTab = pParse->nTab++;
131996 assert( p->pOrderBy==0 );
131997 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
131998 assert( p->addrOpenEphm[0] == -1 );
131999 p->addrOpenEphm[0] = addr;
132000 findRightmost(p)->selFlags |= SF_UsesEphemeral;
132001 assert( p->pEList );
132002 }
132003
132004 /* Code the SELECT statements to our left
132005 */
132006 assert( !pPrior->pOrderBy );
132007 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
132008 rc = sqlite3Select(pParse, pPrior, &uniondest);
132009 if( rc ){
132010 goto multi_select_end;
132011 }
132012
132013 /* Code the current SELECT statement
132014 */
132015 if( p->op==TK_EXCEPT ){
132016 op = SRT_Except;
132017 }else{
132018 assert( p->op==TK_UNION );
132019 op = SRT_Union;
132020 }
132021 p->pPrior = 0;
132022 pLimit = p->pLimit;
132023 p->pLimit = 0;
132024 uniondest.eDest = op;
132025 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
132026 selectOpName(p->op)));
132027 rc = sqlite3Select(pParse, p, &uniondest);
132028 testcase( rc!=SQLITE_OK );
132029 assert( p->pOrderBy==0 );
132030 pDelete = p->pPrior;
132031 p->pPrior = pPrior;
132032 p->pOrderBy = 0;
132033 if( p->op==TK_UNION ){
132034 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
132035 }
132036 sqlite3ExprDelete(db, p->pLimit);
132037 p->pLimit = pLimit;
132038 p->iLimit = 0;
132039 p->iOffset = 0;
132040
132041 /* Convert the data in the temporary table into whatever form
132042 ** it is that we currently need.
132043 */
132044 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
132045 assert( p->pEList || db->mallocFailed );
132046 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
132047 int iCont, iBreak, iStart;
132048 iBreak = sqlite3VdbeMakeLabel(pParse);
132049 iCont = sqlite3VdbeMakeLabel(pParse);
132050 computeLimitRegisters(pParse, p, iBreak);
132051 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
132052 iStart = sqlite3VdbeCurrentAddr(v);
132053 selectInnerLoop(pParse, p, unionTab,
132054 0, 0, &dest, iCont, iBreak);
132055 sqlite3VdbeResolveLabel(v, iCont);
132056 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
132057 sqlite3VdbeResolveLabel(v, iBreak);
132058 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
132059 }
132060 break;
132061 }
132062 default: assert( p->op==TK_INTERSECT ); {
132063 int tab1, tab2;
132064 int iCont, iBreak, iStart;
132065 Expr *pLimit;
132066 int addr;
132067 SelectDest intersectdest;
132068 int r1;
132069
132070 /* INTERSECT is different from the others since it requires
132071 ** two temporary tables. Hence it has its own case. Begin
132072 ** by allocating the tables we will need.
132073 */
132074 tab1 = pParse->nTab++;
132075 tab2 = pParse->nTab++;
132076 assert( p->pOrderBy==0 );
132077
132078 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
132079 assert( p->addrOpenEphm[0] == -1 );
132080 p->addrOpenEphm[0] = addr;
132081 findRightmost(p)->selFlags |= SF_UsesEphemeral;
132082 assert( p->pEList );
132083
132084 /* Code the SELECTs to our left into temporary table "tab1".
132085 */
132086 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
132087 rc = sqlite3Select(pParse, pPrior, &intersectdest);
132088 if( rc ){
132089 goto multi_select_end;
132090 }
132091
132092 /* Code the current SELECT into temporary table "tab2"
132093 */
132094 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
132095 assert( p->addrOpenEphm[1] == -1 );
132096 p->addrOpenEphm[1] = addr;
132097 p->pPrior = 0;
132098 pLimit = p->pLimit;
132099 p->pLimit = 0;
132100 intersectdest.iSDParm = tab2;
132101 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
132102 selectOpName(p->op)));
132103 rc = sqlite3Select(pParse, p, &intersectdest);
132104 testcase( rc!=SQLITE_OK );
132105 pDelete = p->pPrior;
132106 p->pPrior = pPrior;
132107 if( p->nSelectRow>pPrior->nSelectRow ){
132108 p->nSelectRow = pPrior->nSelectRow;
132109 }
132110 sqlite3ExprDelete(db, p->pLimit);
132111 p->pLimit = pLimit;
132112
132113 /* Generate code to take the intersection of the two temporary
132114 ** tables.
132115 */
132116 if( rc ) break;
132117 assert( p->pEList );
132118 iBreak = sqlite3VdbeMakeLabel(pParse);
132119 iCont = sqlite3VdbeMakeLabel(pParse);
132120 computeLimitRegisters(pParse, p, iBreak);
132121 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
132122 r1 = sqlite3GetTempReg(pParse);
132123 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
132124 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
132125 VdbeCoverage(v);
132126 sqlite3ReleaseTempReg(pParse, r1);
132127 selectInnerLoop(pParse, p, tab1,
132128 0, 0, &dest, iCont, iBreak);
132129 sqlite3VdbeResolveLabel(v, iCont);
132130 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
132131 sqlite3VdbeResolveLabel(v, iBreak);
132132 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
132133 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
132134 break;
132135 }
132136 }
132137
132138 #ifndef SQLITE_OMIT_EXPLAIN
132139 if( p->pNext==0 ){
132140 ExplainQueryPlanPop(pParse);
132141 }
132142 #endif
132143 }
132144 if( pParse->nErr ) goto multi_select_end;
132145
132146 /* Compute collating sequences used by
132147 ** temporary tables needed to implement the compound select.
132148 ** Attach the KeyInfo structure to all temporary tables.
132149 **
132150 ** This section is run by the right-most SELECT statement only.
132151 ** SELECT statements to the left always skip this part. The right-most
132152 ** SELECT might also skip this part if it has no ORDER BY clause and
132153 ** no temp tables are required.
132154 */
132155 if( p->selFlags & SF_UsesEphemeral ){
132156 int i; /* Loop counter */
132157 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
132158 Select *pLoop; /* For looping through SELECT statements */
132159 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
132160 int nCol; /* Number of columns in result set */
132161
132162 assert( p->pNext==0 );
132163 nCol = p->pEList->nExpr;
132164 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
132165 if( !pKeyInfo ){
132166 rc = SQLITE_NOMEM_BKPT;
132167 goto multi_select_end;
132168 }
132169 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
132170 *apColl = multiSelectCollSeq(pParse, p, i);
132171 if( 0==*apColl ){
132172 *apColl = db->pDfltColl;
132173 }
132174 }
132175
132176 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
132177 for(i=0; i<2; i++){
132178 int addr = pLoop->addrOpenEphm[i];
132179 if( addr<0 ){
132180 /* If [0] is unused then [1] is also unused. So we can
132181 ** always safely abort as soon as the first unused slot is found */
132182 assert( pLoop->addrOpenEphm[1]<0 );
132183 break;
132184 }
132185 sqlite3VdbeChangeP2(v, addr, nCol);
132186 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
132187 P4_KEYINFO);
132188 pLoop->addrOpenEphm[i] = -1;
132189 }
132190 }
132191 sqlite3KeyInfoUnref(pKeyInfo);
132192 }
132193
132194multi_select_end:
132195 pDest->iSdst = dest.iSdst;
132196 pDest->nSdst = dest.nSdst;
132197 sqlite3SelectDelete(db, pDelete);
132198 return rc;
132199}
132200#endif /* SQLITE_OMIT_COMPOUND_SELECT */
132201
132202/*
132203** Error message for when two or more terms of a compound select have different
132204** size result sets.
132205*/
132206SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
132207 if( p->selFlags & SF_Values ){
132208 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
132209 }else{
132210 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
132211 " do not have the same number of result columns", selectOpName(p->op));
132212 }
132213}
132214
132215/*
132216** Code an output subroutine for a coroutine implementation of a
132217** SELECT statment.
132218**
132219** The data to be output is contained in pIn->iSdst. There are
132220** pIn->nSdst columns to be output. pDest is where the output should
132221** be sent.
132222**
132223** regReturn is the number of the register holding the subroutine
132224** return address.
132225**
132226** If regPrev>0 then it is the first register in a vector that
132227** records the previous output. mem[regPrev] is a flag that is false
132228** if there has been no previous output. If regPrev>0 then code is
132229** generated to suppress duplicates. pKeyInfo is used for comparing
132230** keys.
132231**
132232** If the LIMIT found in p->iLimit is reached, jump immediately to
132233** iBreak.
132234*/
132235static int generateOutputSubroutine(
132236 Parse *pParse, /* Parsing context */
132237 Select *p, /* The SELECT statement */
132238 SelectDest *pIn, /* Coroutine supplying data */
132239 SelectDest *pDest, /* Where to send the data */
132240 int regReturn, /* The return address register */
132241 int regPrev, /* Previous result register. No uniqueness if 0 */
132242 KeyInfo *pKeyInfo, /* For comparing with previous entry */
132243 int iBreak /* Jump here if we hit the LIMIT */
132244){
132245 Vdbe *v = pParse->pVdbe;
132246 int iContinue;
132247 int addr;
132248
132249 addr = sqlite3VdbeCurrentAddr(v);
132250 iContinue = sqlite3VdbeMakeLabel(pParse);
132251
132252 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
132253 */
132254 if( regPrev ){
132255 int addr1, addr2;
132256 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
132257 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
132258 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
132259 sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
132260 sqlite3VdbeJumpHere(v, addr1);
132261 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
132262 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
132263 }
132264 if( pParse->db->mallocFailed ) return 0;
132265
132266 /* Suppress the first OFFSET entries if there is an OFFSET clause
132267 */
132268 codeOffset(v, p->iOffset, iContinue);
132269
132270 assert( pDest->eDest!=SRT_Exists );
132271 assert( pDest->eDest!=SRT_Table );
132272 switch( pDest->eDest ){
132273 /* Store the result as data using a unique key.
132274 */
132275 case SRT_EphemTab: {
132276 int r1 = sqlite3GetTempReg(pParse);
132277 int r2 = sqlite3GetTempReg(pParse);
132278 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
132279 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
132280 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
132281 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
132282 sqlite3ReleaseTempReg(pParse, r2);
132283 sqlite3ReleaseTempReg(pParse, r1);
132284 break;
132285 }
132286
132287#ifndef SQLITE_OMIT_SUBQUERY
132288 /* If we are creating a set for an "expr IN (SELECT ...)".
132289 */
132290 case SRT_Set: {
132291 int r1;
132292 testcase( pIn->nSdst>1 );
132293 r1 = sqlite3GetTempReg(pParse);
132294 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
132295 r1, pDest->zAffSdst, pIn->nSdst);
132296 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
132297 pIn->iSdst, pIn->nSdst);
132298 sqlite3ReleaseTempReg(pParse, r1);
132299 break;
132300 }
132301
132302 /* If this is a scalar select that is part of an expression, then
132303 ** store the results in the appropriate memory cell and break out
132304 ** of the scan loop. Note that the select might return multiple columns
132305 ** if it is the RHS of a row-value IN operator.
132306 */
132307 case SRT_Mem: {
132308 if( pParse->nErr==0 ){
132309 testcase( pIn->nSdst>1 );
132310 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
132311 }
132312 /* The LIMIT clause will jump out of the loop for us */
132313 break;
132314 }
132315#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
132316
132317 /* The results are stored in a sequence of registers
132318 ** starting at pDest->iSdst. Then the co-routine yields.
132319 */
132320 case SRT_Coroutine: {
132321 if( pDest->iSdst==0 ){
132322 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
132323 pDest->nSdst = pIn->nSdst;
132324 }
132325 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
132326 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
132327 break;
132328 }
132329
132330 /* If none of the above, then the result destination must be
132331 ** SRT_Output. This routine is never called with any other
132332 ** destination other than the ones handled above or SRT_Output.
132333 **
132334 ** For SRT_Output, results are stored in a sequence of registers.
132335 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
132336 ** return the next row of result.
132337 */
132338 default: {
132339 assert( pDest->eDest==SRT_Output );
132340 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
132341 break;
132342 }
132343 }
132344
132345 /* Jump to the end of the loop if the LIMIT is reached.
132346 */
132347 if( p->iLimit ){
132348 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
132349 }
132350
132351 /* Generate the subroutine return
132352 */
132353 sqlite3VdbeResolveLabel(v, iContinue);
132354 sqlite3VdbeAddOp1(v, OP_Return, regReturn);
132355
132356 return addr;
132357}
132358
132359/*
132360** Alternative compound select code generator for cases when there
132361** is an ORDER BY clause.
132362**
132363** We assume a query of the following form:
132364**
132365** <selectA> <operator> <selectB> ORDER BY <orderbylist>
132366**
132367** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
132368** is to code both <selectA> and <selectB> with the ORDER BY clause as
132369** co-routines. Then run the co-routines in parallel and merge the results
132370** into the output. In addition to the two coroutines (called selectA and
132371** selectB) there are 7 subroutines:
132372**
132373** outA: Move the output of the selectA coroutine into the output
132374** of the compound query.
132375**
132376** outB: Move the output of the selectB coroutine into the output
132377** of the compound query. (Only generated for UNION and
132378** UNION ALL. EXCEPT and INSERTSECT never output a row that
132379** appears only in B.)
132380**
132381** AltB: Called when there is data from both coroutines and A<B.
132382**
132383** AeqB: Called when there is data from both coroutines and A==B.
132384**
132385** AgtB: Called when there is data from both coroutines and A>B.
132386**
132387** EofA: Called when data is exhausted from selectA.
132388**
132389** EofB: Called when data is exhausted from selectB.
132390**
132391** The implementation of the latter five subroutines depend on which
132392** <operator> is used:
132393**
132394**
132395** UNION ALL UNION EXCEPT INTERSECT
132396** ------------- ----------------- -------------- -----------------
132397** AltB: outA, nextA outA, nextA outA, nextA nextA
132398**
132399** AeqB: outA, nextA nextA nextA outA, nextA
132400**
132401** AgtB: outB, nextB outB, nextB nextB nextB
132402**
132403** EofA: outB, nextB outB, nextB halt halt
132404**
132405** EofB: outA, nextA outA, nextA outA, nextA halt
132406**
132407** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
132408** causes an immediate jump to EofA and an EOF on B following nextB causes
132409** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
132410** following nextX causes a jump to the end of the select processing.
132411**
132412** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
132413** within the output subroutine. The regPrev register set holds the previously
132414** output value. A comparison is made against this value and the output
132415** is skipped if the next results would be the same as the previous.
132416**
132417** The implementation plan is to implement the two coroutines and seven
132418** subroutines first, then put the control logic at the bottom. Like this:
132419**
132420** goto Init
132421** coA: coroutine for left query (A)
132422** coB: coroutine for right query (B)
132423** outA: output one row of A
132424** outB: output one row of B (UNION and UNION ALL only)
132425** EofA: ...
132426** EofB: ...
132427** AltB: ...
132428** AeqB: ...
132429** AgtB: ...
132430** Init: initialize coroutine registers
132431** yield coA
132432** if eof(A) goto EofA
132433** yield coB
132434** if eof(B) goto EofB
132435** Cmpr: Compare A, B
132436** Jump AltB, AeqB, AgtB
132437** End: ...
132438**
132439** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
132440** actually called using Gosub and they do not Return. EofA and EofB loop
132441** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
132442** and AgtB jump to either L2 or to one of EofA or EofB.
132443*/
132444#ifndef SQLITE_OMIT_COMPOUND_SELECT
132445static int multiSelectOrderBy(
132446 Parse *pParse, /* Parsing context */
132447 Select *p, /* The right-most of SELECTs to be coded */
132448 SelectDest *pDest /* What to do with query results */
132449){
132450 int i, j; /* Loop counters */
132451 Select *pPrior; /* Another SELECT immediately to our left */
132452 Vdbe *v; /* Generate code to this VDBE */
132453 SelectDest destA; /* Destination for coroutine A */
132454 SelectDest destB; /* Destination for coroutine B */
132455 int regAddrA; /* Address register for select-A coroutine */
132456 int regAddrB; /* Address register for select-B coroutine */
132457 int addrSelectA; /* Address of the select-A coroutine */
132458 int addrSelectB; /* Address of the select-B coroutine */
132459 int regOutA; /* Address register for the output-A subroutine */
132460 int regOutB; /* Address register for the output-B subroutine */
132461 int addrOutA; /* Address of the output-A subroutine */
132462 int addrOutB = 0; /* Address of the output-B subroutine */
132463 int addrEofA; /* Address of the select-A-exhausted subroutine */
132464 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
132465 int addrEofB; /* Address of the select-B-exhausted subroutine */
132466 int addrAltB; /* Address of the A<B subroutine */
132467 int addrAeqB; /* Address of the A==B subroutine */
132468 int addrAgtB; /* Address of the A>B subroutine */
132469 int regLimitA; /* Limit register for select-A */
132470 int regLimitB; /* Limit register for select-A */
132471 int regPrev; /* A range of registers to hold previous output */
132472 int savedLimit; /* Saved value of p->iLimit */
132473 int savedOffset; /* Saved value of p->iOffset */
132474 int labelCmpr; /* Label for the start of the merge algorithm */
132475 int labelEnd; /* Label for the end of the overall SELECT stmt */
132476 int addr1; /* Jump instructions that get retargetted */
132477 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
132478 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
132479 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
132480 sqlite3 *db; /* Database connection */
132481 ExprList *pOrderBy; /* The ORDER BY clause */
132482 int nOrderBy; /* Number of terms in the ORDER BY clause */
132483 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
132484
132485 assert( p->pOrderBy!=0 );
132486 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
132487 db = pParse->db;
132488 v = pParse->pVdbe;
132489 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
132490 labelEnd = sqlite3VdbeMakeLabel(pParse);
132491 labelCmpr = sqlite3VdbeMakeLabel(pParse);
132492
132493
132494 /* Patch up the ORDER BY clause
132495 */
132496 op = p->op;
132497 pPrior = p->pPrior;
132498 assert( pPrior->pOrderBy==0 );
132499 pOrderBy = p->pOrderBy;
132500 assert( pOrderBy );
132501 nOrderBy = pOrderBy->nExpr;
132502
132503 /* For operators other than UNION ALL we have to make sure that
132504 ** the ORDER BY clause covers every term of the result set. Add
132505 ** terms to the ORDER BY clause as necessary.
132506 */
132507 if( op!=TK_ALL ){
132508 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
132509 struct ExprList_item *pItem;
132510 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
132511 assert( pItem->u.x.iOrderByCol>0 );
132512 if( pItem->u.x.iOrderByCol==i ) break;
132513 }
132514 if( j==nOrderBy ){
132515 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
132516 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
132517 pNew->flags |= EP_IntValue;
132518 pNew->u.iValue = i;
132519 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
132520 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
132521 }
132522 }
132523 }
132524
132525 /* Compute the comparison permutation and keyinfo that is used with
132526 ** the permutation used to determine if the next
132527 ** row of results comes from selectA or selectB. Also add explicit
132528 ** collations to the ORDER BY clause terms so that when the subqueries
132529 ** to the right and the left are evaluated, they use the correct
132530 ** collation.
132531 */
132532 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
132533 if( aPermute ){
132534 struct ExprList_item *pItem;
132535 aPermute[0] = nOrderBy;
132536 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
132537 assert( pItem->u.x.iOrderByCol>0 );
132538 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
132539 aPermute[i] = pItem->u.x.iOrderByCol - 1;
132540 }
132541 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
132542 }else{
132543 pKeyMerge = 0;
132544 }
132545
132546 /* Reattach the ORDER BY clause to the query.
132547 */
132548 p->pOrderBy = pOrderBy;
132549 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
132550
132551 /* Allocate a range of temporary registers and the KeyInfo needed
132552 ** for the logic that removes duplicate result rows when the
132553 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
132554 */
132555 if( op==TK_ALL ){
132556 regPrev = 0;
132557 }else{
132558 int nExpr = p->pEList->nExpr;
132559 assert( nOrderBy>=nExpr || db->mallocFailed );
132560 regPrev = pParse->nMem+1;
132561 pParse->nMem += nExpr+1;
132562 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
132563 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
132564 if( pKeyDup ){
132565 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
132566 for(i=0; i<nExpr; i++){
132567 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
132568 pKeyDup->aSortFlags[i] = 0;
132569 }
132570 }
132571 }
132572
132573 /* Separate the left and the right query from one another
132574 */
132575 p->pPrior = 0;
132576 pPrior->pNext = 0;
132577 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
132578 if( pPrior->pPrior==0 ){
132579 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
132580 }
132581
132582 /* Compute the limit registers */
132583 computeLimitRegisters(pParse, p, labelEnd);
132584 if( p->iLimit && op==TK_ALL ){
132585 regLimitA = ++pParse->nMem;
132586 regLimitB = ++pParse->nMem;
132587 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
132588 regLimitA);
132589 sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
132590 }else{
132591 regLimitA = regLimitB = 0;
132592 }
132593 sqlite3ExprDelete(db, p->pLimit);
132594 p->pLimit = 0;
132595
132596 regAddrA = ++pParse->nMem;
132597 regAddrB = ++pParse->nMem;
132598 regOutA = ++pParse->nMem;
132599 regOutB = ++pParse->nMem;
132600 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
132601 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
132602
132603 ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
132604
132605 /* Generate a coroutine to evaluate the SELECT statement to the
132606 ** left of the compound operator - the "A" select.
132607 */
132608 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
132609 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
132610 VdbeComment((v, "left SELECT"));
132611 pPrior->iLimit = regLimitA;
132612 ExplainQueryPlan((pParse, 1, "LEFT"));
132613 sqlite3Select(pParse, pPrior, &destA);
132614 sqlite3VdbeEndCoroutine(v, regAddrA);
132615 sqlite3VdbeJumpHere(v, addr1);
132616
132617 /* Generate a coroutine to evaluate the SELECT statement on
132618 ** the right - the "B" select
132619 */
132620 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
132621 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
132622 VdbeComment((v, "right SELECT"));
132623 savedLimit = p->iLimit;
132624 savedOffset = p->iOffset;
132625 p->iLimit = regLimitB;
132626 p->iOffset = 0;
132627 ExplainQueryPlan((pParse, 1, "RIGHT"));
132628 sqlite3Select(pParse, p, &destB);
132629 p->iLimit = savedLimit;
132630 p->iOffset = savedOffset;
132631 sqlite3VdbeEndCoroutine(v, regAddrB);
132632
132633 /* Generate a subroutine that outputs the current row of the A
132634 ** select as the next output row of the compound select.
132635 */
132636 VdbeNoopComment((v, "Output routine for A"));
132637 addrOutA = generateOutputSubroutine(pParse,
132638 p, &destA, pDest, regOutA,
132639 regPrev, pKeyDup, labelEnd);
132640
132641 /* Generate a subroutine that outputs the current row of the B
132642 ** select as the next output row of the compound select.
132643 */
132644 if( op==TK_ALL || op==TK_UNION ){
132645 VdbeNoopComment((v, "Output routine for B"));
132646 addrOutB = generateOutputSubroutine(pParse,
132647 p, &destB, pDest, regOutB,
132648 regPrev, pKeyDup, labelEnd);
132649 }
132650 sqlite3KeyInfoUnref(pKeyDup);
132651
132652 /* Generate a subroutine to run when the results from select A
132653 ** are exhausted and only data in select B remains.
132654 */
132655 if( op==TK_EXCEPT || op==TK_INTERSECT ){
132656 addrEofA_noB = addrEofA = labelEnd;
132657 }else{
132658 VdbeNoopComment((v, "eof-A subroutine"));
132659 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
132660 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
132661 VdbeCoverage(v);
132662 sqlite3VdbeGoto(v, addrEofA);
132663 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
132664 }
132665
132666 /* Generate a subroutine to run when the results from select B
132667 ** are exhausted and only data in select A remains.
132668 */
132669 if( op==TK_INTERSECT ){
132670 addrEofB = addrEofA;
132671 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
132672 }else{
132673 VdbeNoopComment((v, "eof-B subroutine"));
132674 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
132675 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
132676 sqlite3VdbeGoto(v, addrEofB);
132677 }
132678
132679 /* Generate code to handle the case of A<B
132680 */
132681 VdbeNoopComment((v, "A-lt-B subroutine"));
132682 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
132683 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
132684 sqlite3VdbeGoto(v, labelCmpr);
132685
132686 /* Generate code to handle the case of A==B
132687 */
132688 if( op==TK_ALL ){
132689 addrAeqB = addrAltB;
132690 }else if( op==TK_INTERSECT ){
132691 addrAeqB = addrAltB;
132692 addrAltB++;
132693 }else{
132694 VdbeNoopComment((v, "A-eq-B subroutine"));
132695 addrAeqB =
132696 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
132697 sqlite3VdbeGoto(v, labelCmpr);
132698 }
132699
132700 /* Generate code to handle the case of A>B
132701 */
132702 VdbeNoopComment((v, "A-gt-B subroutine"));
132703 addrAgtB = sqlite3VdbeCurrentAddr(v);
132704 if( op==TK_ALL || op==TK_UNION ){
132705 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
132706 }
132707 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
132708 sqlite3VdbeGoto(v, labelCmpr);
132709
132710 /* This code runs once to initialize everything.
132711 */
132712 sqlite3VdbeJumpHere(v, addr1);
132713 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
132714 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
132715
132716 /* Implement the main merge loop
132717 */
132718 sqlite3VdbeResolveLabel(v, labelCmpr);
132719 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
132720 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
132721 (char*)pKeyMerge, P4_KEYINFO);
132722 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
132723 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
132724
132725 /* Jump to the this point in order to terminate the query.
132726 */
132727 sqlite3VdbeResolveLabel(v, labelEnd);
132728
132729 /* Reassembly the compound query so that it will be freed correctly
132730 ** by the calling function */
132731 if( p->pPrior ){
132732 sqlite3SelectDelete(db, p->pPrior);
132733 }
132734 p->pPrior = pPrior;
132735 pPrior->pNext = p;
132736
132737 /*** TBD: Insert subroutine calls to close cursors on incomplete
132738 **** subqueries ****/
132739 ExplainQueryPlanPop(pParse);
132740 return pParse->nErr!=0;
132741}
132742#endif
132743
132744#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132745
132746/* An instance of the SubstContext object describes an substitution edit
132747** to be performed on a parse tree.
132748**
132749** All references to columns in table iTable are to be replaced by corresponding
132750** expressions in pEList.
132751*/
132752typedef struct SubstContext {
132753 Parse *pParse; /* The parsing context */
132754 int iTable; /* Replace references to this table */
132755 int iNewTable; /* New table number */
132756 int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
132757 ExprList *pEList; /* Replacement expressions */
132758} SubstContext;
132759
132760/* Forward Declarations */
132761static void substExprList(SubstContext*, ExprList*);
132762static void substSelect(SubstContext*, Select*, int);
132763
132764/*
132765** Scan through the expression pExpr. Replace every reference to
132766** a column in table number iTable with a copy of the iColumn-th
132767** entry in pEList. (But leave references to the ROWID column
132768** unchanged.)
132769**
132770** This routine is part of the flattening procedure. A subquery
132771** whose result set is defined by pEList appears as entry in the
132772** FROM clause of a SELECT such that the VDBE cursor assigned to that
132773** FORM clause entry is iTable. This routine makes the necessary
132774** changes to pExpr so that it refers directly to the source table
132775** of the subquery rather the result set of the subquery.
132776*/
132777static Expr *substExpr(
132778 SubstContext *pSubst, /* Description of the substitution */
132779 Expr *pExpr /* Expr in which substitution occurs */
132780){
132781 if( pExpr==0 ) return 0;
132782 if( ExprHasProperty(pExpr, EP_FromJoin)
132783 && pExpr->iRightJoinTable==pSubst->iTable
132784 ){
132785 pExpr->iRightJoinTable = pSubst->iNewTable;
132786 }
132787 if( pExpr->op==TK_COLUMN
132788 && pExpr->iTable==pSubst->iTable
132789 && !ExprHasProperty(pExpr, EP_FixedCol)
132790 ){
132791 if( pExpr->iColumn<0 ){
132792 pExpr->op = TK_NULL;
132793 }else{
132794 Expr *pNew;
132795 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
132796 Expr ifNullRow;
132797 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
132798 assert( pExpr->pRight==0 );
132799 if( sqlite3ExprIsVector(pCopy) ){
132800 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
132801 }else{
132802 sqlite3 *db = pSubst->pParse->db;
132803 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
132804 memset(&ifNullRow, 0, sizeof(ifNullRow));
132805 ifNullRow.op = TK_IF_NULL_ROW;
132806 ifNullRow.pLeft = pCopy;
132807 ifNullRow.iTable = pSubst->iNewTable;
132808 ifNullRow.flags = EP_Skip;
132809 pCopy = &ifNullRow;
132810 }
132811 testcase( ExprHasProperty(pCopy, EP_Subquery) );
132812 pNew = sqlite3ExprDup(db, pCopy, 0);
132813 if( pNew && pSubst->isLeftJoin ){
132814 ExprSetProperty(pNew, EP_CanBeNull);
132815 }
132816 if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
132817 pNew->iRightJoinTable = pExpr->iRightJoinTable;
132818 ExprSetProperty(pNew, EP_FromJoin);
132819 }
132820 sqlite3ExprDelete(db, pExpr);
132821 pExpr = pNew;
132822
132823 /* Ensure that the expression now has an implicit collation sequence,
132824 ** just as it did when it was a column of a view or sub-query. */
132825 if( pExpr ){
132826 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
132827 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
132828 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
132829 (pColl ? pColl->zName : "BINARY")
132830 );
132831 }
132832 ExprClearProperty(pExpr, EP_Collate);
132833 }
132834 }
132835 }
132836 }else{
132837 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
132838 pExpr->iTable = pSubst->iNewTable;
132839 }
132840 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
132841 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
132842 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
132843 substSelect(pSubst, pExpr->x.pSelect, 1);
132844 }else{
132845 substExprList(pSubst, pExpr->x.pList);
132846 }
132847#ifndef SQLITE_OMIT_WINDOWFUNC
132848 if( ExprHasProperty(pExpr, EP_WinFunc) ){
132849 Window *pWin = pExpr->y.pWin;
132850 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
132851 substExprList(pSubst, pWin->pPartition);
132852 substExprList(pSubst, pWin->pOrderBy);
132853 }
132854#endif
132855 }
132856 return pExpr;
132857}
132858static void substExprList(
132859 SubstContext *pSubst, /* Description of the substitution */
132860 ExprList *pList /* List to scan and in which to make substitutes */
132861){
132862 int i;
132863 if( pList==0 ) return;
132864 for(i=0; i<pList->nExpr; i++){
132865 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
132866 }
132867}
132868static void substSelect(
132869 SubstContext *pSubst, /* Description of the substitution */
132870 Select *p, /* SELECT statement in which to make substitutions */
132871 int doPrior /* Do substitutes on p->pPrior too */
132872){
132873 SrcList *pSrc;
132874 struct SrcList_item *pItem;
132875 int i;
132876 if( !p ) return;
132877 do{
132878 substExprList(pSubst, p->pEList);
132879 substExprList(pSubst, p->pGroupBy);
132880 substExprList(pSubst, p->pOrderBy);
132881 p->pHaving = substExpr(pSubst, p->pHaving);
132882 p->pWhere = substExpr(pSubst, p->pWhere);
132883 pSrc = p->pSrc;
132884 assert( pSrc!=0 );
132885 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
132886 substSelect(pSubst, pItem->pSelect, 1);
132887 if( pItem->fg.isTabFunc ){
132888 substExprList(pSubst, pItem->u1.pFuncArg);
132889 }
132890 }
132891 }while( doPrior && (p = p->pPrior)!=0 );
132892}
132893#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
132894
132895#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132896/*
132897** pSelect is a SELECT statement and pSrcItem is one item in the FROM
132898** clause of that SELECT.
132899**
132900** This routine scans the entire SELECT statement and recomputes the
132901** pSrcItem->colUsed mask.
132902*/
132903static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
132904 struct SrcList_item *pItem;
132905 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
132906 pItem = pWalker->u.pSrcItem;
132907 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
132908 if( pExpr->iColumn<0 ) return WRC_Continue;
132909 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
132910 return WRC_Continue;
132911}
132912static void recomputeColumnsUsed(
132913 Select *pSelect, /* The complete SELECT statement */
132914 struct SrcList_item *pSrcItem /* Which FROM clause item to recompute */
132915){
132916 Walker w;
132917 if( NEVER(pSrcItem->pTab==0) ) return;
132918 memset(&w, 0, sizeof(w));
132919 w.xExprCallback = recomputeColumnsUsedExpr;
132920 w.xSelectCallback = sqlite3SelectWalkNoop;
132921 w.u.pSrcItem = pSrcItem;
132922 pSrcItem->colUsed = 0;
132923 sqlite3WalkSelect(&w, pSelect);
132924}
132925#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
132926
132927#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
132928/*
132929** This routine attempts to flatten subqueries as a performance optimization.
132930** This routine returns 1 if it makes changes and 0 if no flattening occurs.
132931**
132932** To understand the concept of flattening, consider the following
132933** query:
132934**
132935** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
132936**
132937** The default way of implementing this query is to execute the
132938** subquery first and store the results in a temporary table, then
132939** run the outer query on that temporary table. This requires two
132940** passes over the data. Furthermore, because the temporary table
132941** has no indices, the WHERE clause on the outer query cannot be
132942** optimized.
132943**
132944** This routine attempts to rewrite queries such as the above into
132945** a single flat select, like this:
132946**
132947** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
132948**
132949** The code generated for this simplification gives the same result
132950** but only has to scan the data once. And because indices might
132951** exist on the table t1, a complete scan of the data might be
132952** avoided.
132953**
132954** Flattening is subject to the following constraints:
132955**
132956** (**) We no longer attempt to flatten aggregate subqueries. Was:
132957** The subquery and the outer query cannot both be aggregates.
132958**
132959** (**) We no longer attempt to flatten aggregate subqueries. Was:
132960** (2) If the subquery is an aggregate then
132961** (2a) the outer query must not be a join and
132962** (2b) the outer query must not use subqueries
132963** other than the one FROM-clause subquery that is a candidate
132964** for flattening. (This is due to ticket [2f7170d73bf9abf80]
132965** from 2015-02-09.)
132966**
132967** (3) If the subquery is the right operand of a LEFT JOIN then
132968** (3a) the subquery may not be a join and
132969** (3b) the FROM clause of the subquery may not contain a virtual
132970** table and
132971** (3c) the outer query may not be an aggregate.
132972** (3d) the outer query may not be DISTINCT.
132973**
132974** (4) The subquery can not be DISTINCT.
132975**
132976** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
132977** sub-queries that were excluded from this optimization. Restriction
132978** (4) has since been expanded to exclude all DISTINCT subqueries.
132979**
132980** (**) We no longer attempt to flatten aggregate subqueries. Was:
132981** If the subquery is aggregate, the outer query may not be DISTINCT.
132982**
132983** (7) The subquery must have a FROM clause. TODO: For subqueries without
132984** A FROM clause, consider adding a FROM clause with the special
132985** table sqlite_once that consists of a single row containing a
132986** single NULL.
132987**
132988** (8) If the subquery uses LIMIT then the outer query may not be a join.
132989**
132990** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
132991**
132992** (**) Restriction (10) was removed from the code on 2005-02-05 but we
132993** accidently carried the comment forward until 2014-09-15. Original
132994** constraint: "If the subquery is aggregate then the outer query
132995** may not use LIMIT."
132996**
132997** (11) The subquery and the outer query may not both have ORDER BY clauses.
132998**
132999** (**) Not implemented. Subsumed into restriction (3). Was previously
133000** a separate restriction deriving from ticket #350.
133001**
133002** (13) The subquery and outer query may not both use LIMIT.
133003**
133004** (14) The subquery may not use OFFSET.
133005**
133006** (15) If the outer query is part of a compound select, then the
133007** subquery may not use LIMIT.
133008** (See ticket #2339 and ticket [02a8e81d44]).
133009**
133010** (16) If the outer query is aggregate, then the subquery may not
133011** use ORDER BY. (Ticket #2942) This used to not matter
133012** until we introduced the group_concat() function.
133013**
133014** (17) If the subquery is a compound select, then
133015** (17a) all compound operators must be a UNION ALL, and
133016** (17b) no terms within the subquery compound may be aggregate
133017** or DISTINCT, and
133018** (17c) every term within the subquery compound must have a FROM clause
133019** (17d) the outer query may not be
133020** (17d1) aggregate, or
133021** (17d2) DISTINCT, or
133022** (17d3) a join.
133023** (17e) the subquery may not contain window functions
133024**
133025** The parent and sub-query may contain WHERE clauses. Subject to
133026** rules (11), (13) and (14), they may also contain ORDER BY,
133027** LIMIT and OFFSET clauses. The subquery cannot use any compound
133028** operator other than UNION ALL because all the other compound
133029** operators have an implied DISTINCT which is disallowed by
133030** restriction (4).
133031**
133032** Also, each component of the sub-query must return the same number
133033** of result columns. This is actually a requirement for any compound
133034** SELECT statement, but all the code here does is make sure that no
133035** such (illegal) sub-query is flattened. The caller will detect the
133036** syntax error and return a detailed message.
133037**
133038** (18) If the sub-query is a compound select, then all terms of the
133039** ORDER BY clause of the parent must be simple references to
133040** columns of the sub-query.
133041**
133042** (19) If the subquery uses LIMIT then the outer query may not
133043** have a WHERE clause.
133044**
133045** (20) If the sub-query is a compound select, then it must not use
133046** an ORDER BY clause. Ticket #3773. We could relax this constraint
133047** somewhat by saying that the terms of the ORDER BY clause must
133048** appear as unmodified result columns in the outer query. But we
133049** have other optimizations in mind to deal with that case.
133050**
133051** (21) If the subquery uses LIMIT then the outer query may not be
133052** DISTINCT. (See ticket [752e1646fc]).
133053**
133054** (22) The subquery may not be a recursive CTE.
133055**
133056** (**) Subsumed into restriction (17d3). Was: If the outer query is
133057** a recursive CTE, then the sub-query may not be a compound query.
133058** This restriction is because transforming the
133059** parent to a compound query confuses the code that handles
133060** recursive queries in multiSelect().
133061**
133062** (**) We no longer attempt to flatten aggregate subqueries. Was:
133063** The subquery may not be an aggregate that uses the built-in min() or
133064** or max() functions. (Without this restriction, a query like:
133065** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
133066** return the value X for which Y was maximal.)
133067**
133068** (25) If either the subquery or the parent query contains a window
133069** function in the select list or ORDER BY clause, flattening
133070** is not attempted.
133071**
133072**
133073** In this routine, the "p" parameter is a pointer to the outer query.
133074** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
133075** uses aggregates.
133076**
133077** If flattening is not attempted, this routine is a no-op and returns 0.
133078** If flattening is attempted this routine returns 1.
133079**
133080** All of the expression analysis must occur on both the outer query and
133081** the subquery before this routine runs.
133082*/
133083static int flattenSubquery(
133084 Parse *pParse, /* Parsing context */
133085 Select *p, /* The parent or outer SELECT statement */
133086 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
133087 int isAgg /* True if outer SELECT uses aggregate functions */
133088){
133089 const char *zSavedAuthContext = pParse->zAuthContext;
133090 Select *pParent; /* Current UNION ALL term of the other query */
133091 Select *pSub; /* The inner query or "subquery" */
133092 Select *pSub1; /* Pointer to the rightmost select in sub-query */
133093 SrcList *pSrc; /* The FROM clause of the outer query */
133094 SrcList *pSubSrc; /* The FROM clause of the subquery */
133095 int iParent; /* VDBE cursor number of the pSub result set temp table */
133096 int iNewParent = -1;/* Replacement table for iParent */
133097 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
133098 int i; /* Loop counter */
133099 Expr *pWhere; /* The WHERE clause */
133100 struct SrcList_item *pSubitem; /* The subquery */
133101 sqlite3 *db = pParse->db;
133102 Walker w; /* Walker to persist agginfo data */
133103
133104 /* Check to see if flattening is permitted. Return 0 if not.
133105 */
133106 assert( p!=0 );
133107 assert( p->pPrior==0 );
133108 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
133109 pSrc = p->pSrc;
133110 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
133111 pSubitem = &pSrc->a[iFrom];
133112 iParent = pSubitem->iCursor;
133113 pSub = pSubitem->pSelect;
133114 assert( pSub!=0 );
133115
133116#ifndef SQLITE_OMIT_WINDOWFUNC
133117 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
133118#endif
133119
133120 pSubSrc = pSub->pSrc;
133121 assert( pSubSrc );
133122 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
133123 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
133124 ** because they could be computed at compile-time. But when LIMIT and OFFSET
133125 ** became arbitrary expressions, we were forced to add restrictions (13)
133126 ** and (14). */
133127 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
133128 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
133129 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
133130 return 0; /* Restriction (15) */
133131 }
133132 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
133133 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
133134 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
133135 return 0; /* Restrictions (8)(9) */
133136 }
133137 if( p->pOrderBy && pSub->pOrderBy ){
133138 return 0; /* Restriction (11) */
133139 }
133140 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
133141 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
133142 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
133143 return 0; /* Restriction (21) */
133144 }
133145 if( pSub->selFlags & (SF_Recursive) ){
133146 return 0; /* Restrictions (22) */
133147 }
133148
133149 /*
133150 ** If the subquery is the right operand of a LEFT JOIN, then the
133151 ** subquery may not be a join itself (3a). Example of why this is not
133152 ** allowed:
133153 **
133154 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
133155 **
133156 ** If we flatten the above, we would get
133157 **
133158 ** (t1 LEFT OUTER JOIN t2) JOIN t3
133159 **
133160 ** which is not at all the same thing.
133161 **
133162 ** If the subquery is the right operand of a LEFT JOIN, then the outer
133163 ** query cannot be an aggregate. (3c) This is an artifact of the way
133164 ** aggregates are processed - there is no mechanism to determine if
133165 ** the LEFT JOIN table should be all-NULL.
133166 **
133167 ** See also tickets #306, #350, and #3300.
133168 */
133169 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
133170 isLeftJoin = 1;
133171 if( pSubSrc->nSrc>1 /* (3a) */
133172 || isAgg /* (3b) */
133173 || IsVirtual(pSubSrc->a[0].pTab) /* (3c) */
133174 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
133175 ){
133176 return 0;
133177 }
133178 }
133179#ifdef SQLITE_EXTRA_IFNULLROW
133180 else if( iFrom>0 && !isAgg ){
133181 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
133182 ** every reference to any result column from subquery in a join, even
133183 ** though they are not necessary. This will stress-test the OP_IfNullRow
133184 ** opcode. */
133185 isLeftJoin = -1;
133186 }
133187#endif
133188
133189 /* Restriction (17): If the sub-query is a compound SELECT, then it must
133190 ** use only the UNION ALL operator. And none of the simple select queries
133191 ** that make up the compound SELECT are allowed to be aggregate or distinct
133192 ** queries.
133193 */
133194 if( pSub->pPrior ){
133195 if( pSub->pOrderBy ){
133196 return 0; /* Restriction (20) */
133197 }
133198 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
133199 return 0; /* (17d1), (17d2), or (17d3) */
133200 }
133201 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
133202 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
133203 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
133204 assert( pSub->pSrc!=0 );
133205 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
133206 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
133207 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
133208 || pSub1->pSrc->nSrc<1 /* (17c) */
133209#ifndef SQLITE_OMIT_WINDOWFUNC
133210 || pSub1->pWin /* (17e) */
133211#endif
133212 ){
133213 return 0;
133214 }
133215 testcase( pSub1->pSrc->nSrc>1 );
133216 }
133217
133218 /* Restriction (18). */
133219 if( p->pOrderBy ){
133220 int ii;
133221 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
133222 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
133223 }
133224 }
133225 }
133226
133227 /* Ex-restriction (23):
133228 ** The only way that the recursive part of a CTE can contain a compound
133229 ** subquery is for the subquery to be one term of a join. But if the
133230 ** subquery is a join, then the flattening has already been stopped by
133231 ** restriction (17d3)
133232 */
133233 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
133234
133235 /***** If we reach this point, flattening is permitted. *****/
133236 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
133237 pSub->selId, pSub, iFrom));
133238
133239 /* Authorize the subquery */
133240 pParse->zAuthContext = pSubitem->zName;
133241 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
133242 testcase( i==SQLITE_DENY );
133243 pParse->zAuthContext = zSavedAuthContext;
133244
133245 /* If the sub-query is a compound SELECT statement, then (by restrictions
133246 ** 17 and 18 above) it must be a UNION ALL and the parent query must
133247 ** be of the form:
133248 **
133249 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
133250 **
133251 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
133252 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
133253 ** OFFSET clauses and joins them to the left-hand-side of the original
133254 ** using UNION ALL operators. In this case N is the number of simple
133255 ** select statements in the compound sub-query.
133256 **
133257 ** Example:
133258 **
133259 ** SELECT a+1 FROM (
133260 ** SELECT x FROM tab
133261 ** UNION ALL
133262 ** SELECT y FROM tab
133263 ** UNION ALL
133264 ** SELECT abs(z*2) FROM tab2
133265 ** ) WHERE a!=5 ORDER BY 1
133266 **
133267 ** Transformed into:
133268 **
133269 ** SELECT x+1 FROM tab WHERE x+1!=5
133270 ** UNION ALL
133271 ** SELECT y+1 FROM tab WHERE y+1!=5
133272 ** UNION ALL
133273 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
133274 ** ORDER BY 1
133275 **
133276 ** We call this the "compound-subquery flattening".
133277 */
133278 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
133279 Select *pNew;
133280 ExprList *pOrderBy = p->pOrderBy;
133281 Expr *pLimit = p->pLimit;
133282 Select *pPrior = p->pPrior;
133283 p->pOrderBy = 0;
133284 p->pSrc = 0;
133285 p->pPrior = 0;
133286 p->pLimit = 0;
133287 pNew = sqlite3SelectDup(db, p, 0);
133288 p->pLimit = pLimit;
133289 p->pOrderBy = pOrderBy;
133290 p->pSrc = pSrc;
133291 p->op = TK_ALL;
133292 if( pNew==0 ){
133293 p->pPrior = pPrior;
133294 }else{
133295 pNew->pPrior = pPrior;
133296 if( pPrior ) pPrior->pNext = pNew;
133297 pNew->pNext = p;
133298 p->pPrior = pNew;
133299 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
133300 " creates %u as peer\n",pNew->selId));
133301 }
133302 if( db->mallocFailed ) return 1;
133303 }
133304
133305 /* Begin flattening the iFrom-th entry of the FROM clause
133306 ** in the outer query.
133307 */
133308 pSub = pSub1 = pSubitem->pSelect;
133309
133310 /* Delete the transient table structure associated with the
133311 ** subquery
133312 */
133313 sqlite3DbFree(db, pSubitem->zDatabase);
133314 sqlite3DbFree(db, pSubitem->zName);
133315 sqlite3DbFree(db, pSubitem->zAlias);
133316 pSubitem->zDatabase = 0;
133317 pSubitem->zName = 0;
133318 pSubitem->zAlias = 0;
133319 pSubitem->pSelect = 0;
133320
133321 /* Defer deleting the Table object associated with the
133322 ** subquery until code generation is
133323 ** complete, since there may still exist Expr.pTab entries that
133324 ** refer to the subquery even after flattening. Ticket #3346.
133325 **
133326 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
133327 */
133328 if( ALWAYS(pSubitem->pTab!=0) ){
133329 Table *pTabToDel = pSubitem->pTab;
133330 if( pTabToDel->nTabRef==1 ){
133331 Parse *pToplevel = sqlite3ParseToplevel(pParse);
133332 pTabToDel->pNextZombie = pToplevel->pZombieTab;
133333 pToplevel->pZombieTab = pTabToDel;
133334 }else{
133335 pTabToDel->nTabRef--;
133336 }
133337 pSubitem->pTab = 0;
133338 }
133339
133340 /* The following loop runs once for each term in a compound-subquery
133341 ** flattening (as described above). If we are doing a different kind
133342 ** of flattening - a flattening other than a compound-subquery flattening -
133343 ** then this loop only runs once.
133344 **
133345 ** This loop moves all of the FROM elements of the subquery into the
133346 ** the FROM clause of the outer query. Before doing this, remember
133347 ** the cursor number for the original outer query FROM element in
133348 ** iParent. The iParent cursor will never be used. Subsequent code
133349 ** will scan expressions looking for iParent references and replace
133350 ** those references with expressions that resolve to the subquery FROM
133351 ** elements we are now copying in.
133352 */
133353 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
133354 int nSubSrc;
133355 u8 jointype = 0;
133356 assert( pSub!=0 );
133357 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
133358 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
133359 pSrc = pParent->pSrc; /* FROM clause of the outer query */
133360
133361 if( pSrc ){
133362 assert( pParent==p ); /* First time through the loop */
133363 jointype = pSubitem->fg.jointype;
133364 }else{
133365 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
133366 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
133367 if( pSrc==0 ) break;
133368 pParent->pSrc = pSrc;
133369 }
133370
133371 /* The subquery uses a single slot of the FROM clause of the outer
133372 ** query. If the subquery has more than one element in its FROM clause,
133373 ** then expand the outer query to make space for it to hold all elements
133374 ** of the subquery.
133375 **
133376 ** Example:
133377 **
133378 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
133379 **
133380 ** The outer query has 3 slots in its FROM clause. One slot of the
133381 ** outer query (the middle slot) is used by the subquery. The next
133382 ** block of code will expand the outer query FROM clause to 4 slots.
133383 ** The middle slot is expanded to two slots in order to make space
133384 ** for the two elements in the FROM clause of the subquery.
133385 */
133386 if( nSubSrc>1 ){
133387 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
133388 if( pSrc==0 ) break;
133389 pParent->pSrc = pSrc;
133390 }
133391
133392 /* Transfer the FROM clause terms from the subquery into the
133393 ** outer query.
133394 */
133395 for(i=0; i<nSubSrc; i++){
133396 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
133397 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
133398 pSrc->a[i+iFrom] = pSubSrc->a[i];
133399 iNewParent = pSubSrc->a[i].iCursor;
133400 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
133401 }
133402 pSrc->a[iFrom].fg.jointype = jointype;
133403
133404 /* Now begin substituting subquery result set expressions for
133405 ** references to the iParent in the outer query.
133406 **
133407 ** Example:
133408 **
133409 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
133410 ** \ \_____________ subquery __________/ /
133411 ** \_____________________ outer query ______________________________/
133412 **
133413 ** We look at every expression in the outer query and every place we see
133414 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
133415 */
133416 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
133417 /* At this point, any non-zero iOrderByCol values indicate that the
133418 ** ORDER BY column expression is identical to the iOrderByCol'th
133419 ** expression returned by SELECT statement pSub. Since these values
133420 ** do not necessarily correspond to columns in SELECT statement pParent,
133421 ** zero them before transfering the ORDER BY clause.
133422 **
133423 ** Not doing this may cause an error if a subsequent call to this
133424 ** function attempts to flatten a compound sub-query into pParent
133425 ** (the only way this can happen is if the compound sub-query is
133426 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
133427 ExprList *pOrderBy = pSub->pOrderBy;
133428 for(i=0; i<pOrderBy->nExpr; i++){
133429 pOrderBy->a[i].u.x.iOrderByCol = 0;
133430 }
133431 assert( pParent->pOrderBy==0 );
133432 pParent->pOrderBy = pOrderBy;
133433 pSub->pOrderBy = 0;
133434 }
133435 pWhere = pSub->pWhere;
133436 pSub->pWhere = 0;
133437 if( isLeftJoin>0 ){
133438 sqlite3SetJoinExpr(pWhere, iNewParent);
133439 }
133440 if( pWhere ){
133441 if( pParent->pWhere ){
133442 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
133443 }else{
133444 pParent->pWhere = pWhere;
133445 }
133446 }
133447 if( db->mallocFailed==0 ){
133448 SubstContext x;
133449 x.pParse = pParse;
133450 x.iTable = iParent;
133451 x.iNewTable = iNewParent;
133452 x.isLeftJoin = isLeftJoin;
133453 x.pEList = pSub->pEList;
133454 substSelect(&x, pParent, 0);
133455 }
133456
133457 /* The flattened query is a compound if either the inner or the
133458 ** outer query is a compound. */
133459 pParent->selFlags |= pSub->selFlags & SF_Compound;
133460 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
133461
133462 /*
133463 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
133464 **
133465 ** One is tempted to try to add a and b to combine the limits. But this
133466 ** does not work if either limit is negative.
133467 */
133468 if( pSub->pLimit ){
133469 pParent->pLimit = pSub->pLimit;
133470 pSub->pLimit = 0;
133471 }
133472
133473 /* Recompute the SrcList_item.colUsed masks for the flattened
133474 ** tables. */
133475 for(i=0; i<nSubSrc; i++){
133476 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
133477 }
133478 }
133479
133480 /* Finially, delete what is left of the subquery and return
133481 ** success.
133482 */
133483 sqlite3AggInfoPersistWalkerInit(&w, pParse);
133484 sqlite3WalkSelect(&w,pSub1);
133485 sqlite3SelectDelete(db, pSub1);
133486
133487#if SELECTTRACE_ENABLED
133488 if( sqlite3_unsupported_selecttrace & 0x100 ){
133489 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
133490 sqlite3TreeViewSelect(0, p, 0);
133491 }
133492#endif
133493
133494 return 1;
133495}
133496#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
133497
133498/*
133499** A structure to keep track of all of the column values that are fixed to
133500** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
133501*/
133502typedef struct WhereConst WhereConst;
133503struct WhereConst {
133504 Parse *pParse; /* Parsing context */
133505 int nConst; /* Number for COLUMN=CONSTANT terms */
133506 int nChng; /* Number of times a constant is propagated */
133507 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
133508};
133509
133510/*
133511** Add a new entry to the pConst object. Except, do not add duplicate
133512** pColumn entires. Also, do not add if doing so would not be appropriate.
133513**
133514** The caller guarantees the pColumn is a column and pValue is a constant.
133515** This routine has to do some additional checks before completing the
133516** insert.
133517*/
133518static void constInsert(
133519 WhereConst *pConst, /* The WhereConst into which we are inserting */
133520 Expr *pColumn, /* The COLUMN part of the constraint */
133521 Expr *pValue, /* The VALUE part of the constraint */
133522 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
133523){
133524 int i;
133525 assert( pColumn->op==TK_COLUMN );
133526 assert( sqlite3ExprIsConstant(pValue) );
133527
133528 if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
133529 if( sqlite3ExprAffinity(pValue)!=0 ) return;
133530 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
133531 return;
133532 }
133533
133534 /* 2018-10-25 ticket [cf5ed20f]
133535 ** Make sure the same pColumn is not inserted more than once */
133536 for(i=0; i<pConst->nConst; i++){
133537 const Expr *pE2 = pConst->apExpr[i*2];
133538 assert( pE2->op==TK_COLUMN );
133539 if( pE2->iTable==pColumn->iTable
133540 && pE2->iColumn==pColumn->iColumn
133541 ){
133542 return; /* Already present. Return without doing anything. */
133543 }
133544 }
133545
133546 pConst->nConst++;
133547 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
133548 pConst->nConst*2*sizeof(Expr*));
133549 if( pConst->apExpr==0 ){
133550 pConst->nConst = 0;
133551 }else{
133552 pConst->apExpr[pConst->nConst*2-2] = pColumn;
133553 pConst->apExpr[pConst->nConst*2-1] = pValue;
133554 }
133555}
133556
133557/*
133558** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
133559** is a constant expression and where the term must be true because it
133560** is part of the AND-connected terms of the expression. For each term
133561** found, add it to the pConst structure.
133562*/
133563static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
133564 Expr *pRight, *pLeft;
133565 if( pExpr==0 ) return;
133566 if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
133567 if( pExpr->op==TK_AND ){
133568 findConstInWhere(pConst, pExpr->pRight);
133569 findConstInWhere(pConst, pExpr->pLeft);
133570 return;
133571 }
133572 if( pExpr->op!=TK_EQ ) return;
133573 pRight = pExpr->pRight;
133574 pLeft = pExpr->pLeft;
133575 assert( pRight!=0 );
133576 assert( pLeft!=0 );
133577 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
133578 constInsert(pConst,pRight,pLeft,pExpr);
133579 }
133580 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
133581 constInsert(pConst,pLeft,pRight,pExpr);
133582 }
133583}
133584
133585/*
133586** This is a Walker expression callback. pExpr is a candidate expression
133587** to be replaced by a value. If pExpr is equivalent to one of the
133588** columns named in pWalker->u.pConst, then overwrite it with its
133589** corresponding value.
133590*/
133591static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
133592 int i;
133593 WhereConst *pConst;
133594 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
133595 if( ExprHasProperty(pExpr, EP_FixedCol|EP_FromJoin) ){
133596 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
133597 testcase( ExprHasProperty(pExpr, EP_FromJoin) );
133598 return WRC_Continue;
133599 }
133600 pConst = pWalker->u.pConst;
133601 for(i=0; i<pConst->nConst; i++){
133602 Expr *pColumn = pConst->apExpr[i*2];
133603 if( pColumn==pExpr ) continue;
133604 if( pColumn->iTable!=pExpr->iTable ) continue;
133605 if( pColumn->iColumn!=pExpr->iColumn ) continue;
133606 /* A match is found. Add the EP_FixedCol property */
133607 pConst->nChng++;
133608 ExprClearProperty(pExpr, EP_Leaf);
133609 ExprSetProperty(pExpr, EP_FixedCol);
133610 assert( pExpr->pLeft==0 );
133611 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
133612 break;
133613 }
133614 return WRC_Prune;
133615}
133616
133617/*
133618** The WHERE-clause constant propagation optimization.
133619**
133620** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
133621** CONSTANT=COLUMN that are top-level AND-connected terms that are not
133622** part of a ON clause from a LEFT JOIN, then throughout the query
133623** replace all other occurrences of COLUMN with CONSTANT.
133624**
133625** For example, the query:
133626**
133627** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
133628**
133629** Is transformed into
133630**
133631** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
133632**
133633** Return true if any transformations where made and false if not.
133634**
133635** Implementation note: Constant propagation is tricky due to affinity
133636** and collating sequence interactions. Consider this example:
133637**
133638** CREATE TABLE t1(a INT,b TEXT);
133639** INSERT INTO t1 VALUES(123,'0123');
133640** SELECT * FROM t1 WHERE a=123 AND b=a;
133641** SELECT * FROM t1 WHERE a=123 AND b=123;
133642**
133643** The two SELECT statements above should return different answers. b=a
133644** is alway true because the comparison uses numeric affinity, but b=123
133645** is false because it uses text affinity and '0123' is not the same as '123'.
133646** To work around this, the expression tree is not actually changed from
133647** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
133648** and the "123" value is hung off of the pLeft pointer. Code generator
133649** routines know to generate the constant "123" instead of looking up the
133650** column value. Also, to avoid collation problems, this optimization is
133651** only attempted if the "a=123" term uses the default BINARY collation.
133652*/
133653static int propagateConstants(
133654 Parse *pParse, /* The parsing context */
133655 Select *p /* The query in which to propagate constants */
133656){
133657 WhereConst x;
133658 Walker w;
133659 int nChng = 0;
133660 x.pParse = pParse;
133661 do{
133662 x.nConst = 0;
133663 x.nChng = 0;
133664 x.apExpr = 0;
133665 findConstInWhere(&x, p->pWhere);
133666 if( x.nConst ){
133667 memset(&w, 0, sizeof(w));
133668 w.pParse = pParse;
133669 w.xExprCallback = propagateConstantExprRewrite;
133670 w.xSelectCallback = sqlite3SelectWalkNoop;
133671 w.xSelectCallback2 = 0;
133672 w.walkerDepth = 0;
133673 w.u.pConst = &x;
133674 sqlite3WalkExpr(&w, p->pWhere);
133675 sqlite3DbFree(x.pParse->db, x.apExpr);
133676 nChng += x.nChng;
133677 }
133678 }while( x.nChng );
133679 return nChng;
133680}
133681
133682#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
133683/*
133684** Make copies of relevant WHERE clause terms of the outer query into
133685** the WHERE clause of subquery. Example:
133686**
133687** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
133688**
133689** Transformed into:
133690**
133691** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
133692** WHERE x=5 AND y=10;
133693**
133694** The hope is that the terms added to the inner query will make it more
133695** efficient.
133696**
133697** Do not attempt this optimization if:
133698**
133699** (1) (** This restriction was removed on 2017-09-29. We used to
133700** disallow this optimization for aggregate subqueries, but now
133701** it is allowed by putting the extra terms on the HAVING clause.
133702** The added HAVING clause is pointless if the subquery lacks
133703** a GROUP BY clause. But such a HAVING clause is also harmless
133704** so there does not appear to be any reason to add extra logic
133705** to suppress it. **)
133706**
133707** (2) The inner query is the recursive part of a common table expression.
133708**
133709** (3) The inner query has a LIMIT clause (since the changes to the WHERE
133710** clause would change the meaning of the LIMIT).
133711**
133712** (4) The inner query is the right operand of a LEFT JOIN and the
133713** expression to be pushed down does not come from the ON clause
133714** on that LEFT JOIN.
133715**
133716** (5) The WHERE clause expression originates in the ON or USING clause
133717** of a LEFT JOIN where iCursor is not the right-hand table of that
133718** left join. An example:
133719**
133720** SELECT *
133721** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
133722** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
133723** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
133724**
133725** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
133726** But if the (b2=2) term were to be pushed down into the bb subquery,
133727** then the (1,1,NULL) row would be suppressed.
133728**
133729** (6) The inner query features one or more window-functions (since
133730** changes to the WHERE clause of the inner query could change the
133731** window over which window functions are calculated).
133732**
133733** Return 0 if no changes are made and non-zero if one or more WHERE clause
133734** terms are duplicated into the subquery.
133735*/
133736static int pushDownWhereTerms(
133737 Parse *pParse, /* Parse context (for malloc() and error reporting) */
133738 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
133739 Expr *pWhere, /* The WHERE clause of the outer query */
133740 int iCursor, /* Cursor number of the subquery */
133741 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
133742){
133743 Expr *pNew;
133744 int nChng = 0;
133745 Select *pSel;
133746 if( pWhere==0 ) return 0;
133747 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
133748
133749#ifndef SQLITE_OMIT_WINDOWFUNC
133750 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
133751 if( pSel->pWin ) return 0; /* restriction (6) */
133752 }
133753#endif
133754
133755#ifdef SQLITE_DEBUG
133756 /* Only the first term of a compound can have a WITH clause. But make
133757 ** sure no other terms are marked SF_Recursive in case something changes
133758 ** in the future.
133759 */
133760 {
133761 Select *pX;
133762 for(pX=pSubq; pX; pX=pX->pPrior){
133763 assert( (pX->selFlags & (SF_Recursive))==0 );
133764 }
133765 }
133766#endif
133767
133768 if( pSubq->pLimit!=0 ){
133769 return 0; /* restriction (3) */
133770 }
133771 while( pWhere->op==TK_AND ){
133772 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
133773 iCursor, isLeftJoin);
133774 pWhere = pWhere->pLeft;
133775 }
133776 if( isLeftJoin
133777 && (ExprHasProperty(pWhere,EP_FromJoin)==0
133778 || pWhere->iRightJoinTable!=iCursor)
133779 ){
133780 return 0; /* restriction (4) */
133781 }
133782 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
133783 return 0; /* restriction (5) */
133784 }
133785 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
133786 nChng++;
133787 while( pSubq ){
133788 SubstContext x;
133789 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
133790 unsetJoinExpr(pNew, -1);
133791 x.pParse = pParse;
133792 x.iTable = iCursor;
133793 x.iNewTable = iCursor;
133794 x.isLeftJoin = 0;
133795 x.pEList = pSubq->pEList;
133796 pNew = substExpr(&x, pNew);
133797 if( pSubq->selFlags & SF_Aggregate ){
133798 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
133799 }else{
133800 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
133801 }
133802 pSubq = pSubq->pPrior;
133803 }
133804 }
133805 return nChng;
133806}
133807#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
133808
133809/*
133810** The pFunc is the only aggregate function in the query. Check to see
133811** if the query is a candidate for the min/max optimization.
133812**
133813** If the query is a candidate for the min/max optimization, then set
133814** *ppMinMax to be an ORDER BY clause to be used for the optimization
133815** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
133816** whether pFunc is a min() or max() function.
133817**
133818** If the query is not a candidate for the min/max optimization, return
133819** WHERE_ORDERBY_NORMAL (which must be zero).
133820**
133821** This routine must be called after aggregate functions have been
133822** located but before their arguments have been subjected to aggregate
133823** analysis.
133824*/
133825static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
133826 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
133827 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
133828 const char *zFunc; /* Name of aggregate function pFunc */
133829 ExprList *pOrderBy;
133830 u8 sortFlags = 0;
133831
133832 assert( *ppMinMax==0 );
133833 assert( pFunc->op==TK_AGG_FUNCTION );
133834 assert( !IsWindowFunc(pFunc) );
133835 if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
133836 return eRet;
133837 }
133838 zFunc = pFunc->u.zToken;
133839 if( sqlite3StrICmp(zFunc, "min")==0 ){
133840 eRet = WHERE_ORDERBY_MIN;
133841 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
133842 sortFlags = KEYINFO_ORDER_BIGNULL;
133843 }
133844 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
133845 eRet = WHERE_ORDERBY_MAX;
133846 sortFlags = KEYINFO_ORDER_DESC;
133847 }else{
133848 return eRet;
133849 }
133850 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
133851 assert( pOrderBy!=0 || db->mallocFailed );
133852 if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
133853 return eRet;
133854}
133855
133856/*
133857** The select statement passed as the first argument is an aggregate query.
133858** The second argument is the associated aggregate-info object. This
133859** function tests if the SELECT is of the form:
133860**
133861** SELECT count(*) FROM <tbl>
133862**
133863** where table is a database table, not a sub-select or view. If the query
133864** does match this pattern, then a pointer to the Table object representing
133865** <tbl> is returned. Otherwise, 0 is returned.
133866*/
133867static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
133868 Table *pTab;
133869 Expr *pExpr;
133870
133871 assert( !p->pGroupBy );
133872
133873 if( p->pWhere || p->pEList->nExpr!=1
133874 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
133875 ){
133876 return 0;
133877 }
133878 pTab = p->pSrc->a[0].pTab;
133879 pExpr = p->pEList->a[0].pExpr;
133880 assert( pTab && !pTab->pSelect && pExpr );
133881
133882 if( IsVirtual(pTab) ) return 0;
133883 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
133884 if( NEVER(pAggInfo->nFunc==0) ) return 0;
133885 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
133886 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
133887
133888 return pTab;
133889}
133890
133891/*
133892** If the source-list item passed as an argument was augmented with an
133893** INDEXED BY clause, then try to locate the specified index. If there
133894** was such a clause and the named index cannot be found, return
133895** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
133896** pFrom->pIndex and return SQLITE_OK.
133897*/
133898SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
133899 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
133900 Table *pTab = pFrom->pTab;
133901 char *zIndexedBy = pFrom->u1.zIndexedBy;
133902 Index *pIdx;
133903 for(pIdx=pTab->pIndex;
133904 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
133905 pIdx=pIdx->pNext
133906 );
133907 if( !pIdx ){
133908 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
133909 pParse->checkSchema = 1;
133910 return SQLITE_ERROR;
133911 }
133912 pFrom->pIBIndex = pIdx;
133913 }
133914 return SQLITE_OK;
133915}
133916/*
133917** Detect compound SELECT statements that use an ORDER BY clause with
133918** an alternative collating sequence.
133919**
133920** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
133921**
133922** These are rewritten as a subquery:
133923**
133924** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
133925** ORDER BY ... COLLATE ...
133926**
133927** This transformation is necessary because the multiSelectOrderBy() routine
133928** above that generates the code for a compound SELECT with an ORDER BY clause
133929** uses a merge algorithm that requires the same collating sequence on the
133930** result columns as on the ORDER BY clause. See ticket
133931** http://www.sqlite.org/src/info/6709574d2a
133932**
133933** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
133934** The UNION ALL operator works fine with multiSelectOrderBy() even when
133935** there are COLLATE terms in the ORDER BY.
133936*/
133937static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
133938 int i;
133939 Select *pNew;
133940 Select *pX;
133941 sqlite3 *db;
133942 struct ExprList_item *a;
133943 SrcList *pNewSrc;
133944 Parse *pParse;
133945 Token dummy;
133946
133947 if( p->pPrior==0 ) return WRC_Continue;
133948 if( p->pOrderBy==0 ) return WRC_Continue;
133949 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
133950 if( pX==0 ) return WRC_Continue;
133951 a = p->pOrderBy->a;
133952#ifndef SQLITE_OMIT_WINDOWFUNC
133953 /* If iOrderByCol is already non-zero, then it has already been matched
133954 ** to a result column of the SELECT statement. This occurs when the
133955 ** SELECT is rewritten for window-functions processing and then passed
133956 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
133957 ** by this function is not required in this case. */
133958 if( a[0].u.x.iOrderByCol ) return WRC_Continue;
133959#endif
133960 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
133961 if( a[i].pExpr->flags & EP_Collate ) break;
133962 }
133963 if( i<0 ) return WRC_Continue;
133964
133965 /* If we reach this point, that means the transformation is required. */
133966
133967 pParse = pWalker->pParse;
133968 db = pParse->db;
133969 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
133970 if( pNew==0 ) return WRC_Abort;
133971 memset(&dummy, 0, sizeof(dummy));
133972 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
133973 if( pNewSrc==0 ) return WRC_Abort;
133974 *pNew = *p;
133975 p->pSrc = pNewSrc;
133976 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
133977 p->op = TK_SELECT;
133978 p->pWhere = 0;
133979 pNew->pGroupBy = 0;
133980 pNew->pHaving = 0;
133981 pNew->pOrderBy = 0;
133982 p->pPrior = 0;
133983 p->pNext = 0;
133984 p->pWith = 0;
133985#ifndef SQLITE_OMIT_WINDOWFUNC
133986 p->pWinDefn = 0;
133987#endif
133988 p->selFlags &= ~SF_Compound;
133989 assert( (p->selFlags & SF_Converted)==0 );
133990 p->selFlags |= SF_Converted;
133991 assert( pNew->pPrior!=0 );
133992 pNew->pPrior->pNext = pNew;
133993 pNew->pLimit = 0;
133994 return WRC_Continue;
133995}
133996
133997/*
133998** Check to see if the FROM clause term pFrom has table-valued function
133999** arguments. If it does, leave an error message in pParse and return
134000** non-zero, since pFrom is not allowed to be a table-valued function.
134001*/
134002static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
134003 if( pFrom->fg.isTabFunc ){
134004 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
134005 return 1;
134006 }
134007 return 0;
134008}
134009
134010#ifndef SQLITE_OMIT_CTE
134011/*
134012** Argument pWith (which may be NULL) points to a linked list of nested
134013** WITH contexts, from inner to outermost. If the table identified by
134014** FROM clause element pItem is really a common-table-expression (CTE)
134015** then return a pointer to the CTE definition for that table. Otherwise
134016** return NULL.
134017**
134018** If a non-NULL value is returned, set *ppContext to point to the With
134019** object that the returned CTE belongs to.
134020*/
134021static struct Cte *searchWith(
134022 With *pWith, /* Current innermost WITH clause */
134023 struct SrcList_item *pItem, /* FROM clause element to resolve */
134024 With **ppContext /* OUT: WITH clause return value belongs to */
134025){
134026 const char *zName;
134027 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
134028 With *p;
134029 for(p=pWith; p; p=p->pOuter){
134030 int i;
134031 for(i=0; i<p->nCte; i++){
134032 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
134033 *ppContext = p;
134034 return &p->a[i];
134035 }
134036 }
134037 }
134038 }
134039 return 0;
134040}
134041
134042/* The code generator maintains a stack of active WITH clauses
134043** with the inner-most WITH clause being at the top of the stack.
134044**
134045** This routine pushes the WITH clause passed as the second argument
134046** onto the top of the stack. If argument bFree is true, then this
134047** WITH clause will never be popped from the stack. In this case it
134048** should be freed along with the Parse object. In other cases, when
134049** bFree==0, the With object will be freed along with the SELECT
134050** statement with which it is associated.
134051*/
134052SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
134053 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
134054 if( pWith ){
134055 assert( pParse->pWith!=pWith );
134056 pWith->pOuter = pParse->pWith;
134057 pParse->pWith = pWith;
134058 if( bFree ) pParse->pWithToFree = pWith;
134059 }
134060}
134061
134062/*
134063** This function checks if argument pFrom refers to a CTE declared by
134064** a WITH clause on the stack currently maintained by the parser. And,
134065** if currently processing a CTE expression, if it is a recursive
134066** reference to the current CTE.
134067**
134068** If pFrom falls into either of the two categories above, pFrom->pTab
134069** and other fields are populated accordingly. The caller should check
134070** (pFrom->pTab!=0) to determine whether or not a successful match
134071** was found.
134072**
134073** Whether or not a match is found, SQLITE_OK is returned if no error
134074** occurs. If an error does occur, an error message is stored in the
134075** parser and some error code other than SQLITE_OK returned.
134076*/
134077static int withExpand(
134078 Walker *pWalker,
134079 struct SrcList_item *pFrom
134080){
134081 Parse *pParse = pWalker->pParse;
134082 sqlite3 *db = pParse->db;
134083 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
134084 With *pWith; /* WITH clause that pCte belongs to */
134085
134086 assert( pFrom->pTab==0 );
134087 if( pParse->nErr ){
134088 return SQLITE_ERROR;
134089 }
134090
134091 pCte = searchWith(pParse->pWith, pFrom, &pWith);
134092 if( pCte ){
134093 Table *pTab;
134094 ExprList *pEList;
134095 Select *pSel;
134096 Select *pLeft; /* Left-most SELECT statement */
134097 int bMayRecursive; /* True if compound joined by UNION [ALL] */
134098 With *pSavedWith; /* Initial value of pParse->pWith */
134099
134100 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
134101 ** recursive reference to CTE pCte. Leave an error in pParse and return
134102 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
134103 ** In this case, proceed. */
134104 if( pCte->zCteErr ){
134105 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
134106 return SQLITE_ERROR;
134107 }
134108 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
134109
134110 assert( pFrom->pTab==0 );
134111 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
134112 if( pTab==0 ) return WRC_Abort;
134113 pTab->nTabRef = 1;
134114 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
134115 pTab->iPKey = -1;
134116 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
134117 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
134118 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
134119 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
134120 assert( pFrom->pSelect );
134121
134122 /* Check if this is a recursive CTE. */
134123 pSel = pFrom->pSelect;
134124 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
134125 if( bMayRecursive ){
134126 int i;
134127 SrcList *pSrc = pFrom->pSelect->pSrc;
134128 for(i=0; i<pSrc->nSrc; i++){
134129 struct SrcList_item *pItem = &pSrc->a[i];
134130 if( pItem->zDatabase==0
134131 && pItem->zName!=0
134132 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
134133 ){
134134 pItem->pTab = pTab;
134135 pItem->fg.isRecursive = 1;
134136 pTab->nTabRef++;
134137 pSel->selFlags |= SF_Recursive;
134138 }
134139 }
134140 }
134141
134142 /* Only one recursive reference is permitted. */
134143 if( pTab->nTabRef>2 ){
134144 sqlite3ErrorMsg(
134145 pParse, "multiple references to recursive table: %s", pCte->zName
134146 );
134147 return SQLITE_ERROR;
134148 }
134149 assert( pTab->nTabRef==1 ||
134150 ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
134151
134152 pCte->zCteErr = "circular reference: %s";
134153 pSavedWith = pParse->pWith;
134154 pParse->pWith = pWith;
134155 if( bMayRecursive ){
134156 Select *pPrior = pSel->pPrior;
134157 assert( pPrior->pWith==0 );
134158 pPrior->pWith = pSel->pWith;
134159 sqlite3WalkSelect(pWalker, pPrior);
134160 pPrior->pWith = 0;
134161 }else{
134162 sqlite3WalkSelect(pWalker, pSel);
134163 }
134164 pParse->pWith = pWith;
134165
134166 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
134167 pEList = pLeft->pEList;
134168 if( pCte->pCols ){
134169 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
134170 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
134171 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
134172 );
134173 pParse->pWith = pSavedWith;
134174 return SQLITE_ERROR;
134175 }
134176 pEList = pCte->pCols;
134177 }
134178
134179 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
134180 if( bMayRecursive ){
134181 if( pSel->selFlags & SF_Recursive ){
134182 pCte->zCteErr = "multiple recursive references: %s";
134183 }else{
134184 pCte->zCteErr = "recursive reference in a subquery: %s";
134185 }
134186 sqlite3WalkSelect(pWalker, pSel);
134187 }
134188 pCte->zCteErr = 0;
134189 pParse->pWith = pSavedWith;
134190 }
134191
134192 return SQLITE_OK;
134193}
134194#endif
134195
134196#ifndef SQLITE_OMIT_CTE
134197/*
134198** If the SELECT passed as the second argument has an associated WITH
134199** clause, pop it from the stack stored as part of the Parse object.
134200**
134201** This function is used as the xSelectCallback2() callback by
134202** sqlite3SelectExpand() when walking a SELECT tree to resolve table
134203** names and other FROM clause elements.
134204*/
134205static void selectPopWith(Walker *pWalker, Select *p){
134206 Parse *pParse = pWalker->pParse;
134207 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
134208 With *pWith = findRightmost(p)->pWith;
134209 if( pWith!=0 ){
134210 assert( pParse->pWith==pWith || pParse->nErr );
134211 pParse->pWith = pWith->pOuter;
134212 }
134213 }
134214}
134215#else
134216#define selectPopWith 0
134217#endif
134218
134219/*
134220** The SrcList_item structure passed as the second argument represents a
134221** sub-query in the FROM clause of a SELECT statement. This function
134222** allocates and populates the SrcList_item.pTab object. If successful,
134223** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
134224** SQLITE_NOMEM.
134225*/
134226SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
134227 Select *pSel = pFrom->pSelect;
134228 Table *pTab;
134229
134230 assert( pSel );
134231 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
134232 if( pTab==0 ) return SQLITE_NOMEM;
134233 pTab->nTabRef = 1;
134234 if( pFrom->zAlias ){
134235 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
134236 }else{
134237 pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
134238 }
134239 while( pSel->pPrior ){ pSel = pSel->pPrior; }
134240 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
134241 pTab->iPKey = -1;
134242 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
134243 pTab->tabFlags |= TF_Ephemeral;
134244
134245 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
134246}
134247
134248/*
134249** This routine is a Walker callback for "expanding" a SELECT statement.
134250** "Expanding" means to do the following:
134251**
134252** (1) Make sure VDBE cursor numbers have been assigned to every
134253** element of the FROM clause.
134254**
134255** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
134256** defines FROM clause. When views appear in the FROM clause,
134257** fill pTabList->a[].pSelect with a copy of the SELECT statement
134258** that implements the view. A copy is made of the view's SELECT
134259** statement so that we can freely modify or delete that statement
134260** without worrying about messing up the persistent representation
134261** of the view.
134262**
134263** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
134264** on joins and the ON and USING clause of joins.
134265**
134266** (4) Scan the list of columns in the result set (pEList) looking
134267** for instances of the "*" operator or the TABLE.* operator.
134268** If found, expand each "*" to be every column in every table
134269** and TABLE.* to be every column in TABLE.
134270**
134271*/
134272static int selectExpander(Walker *pWalker, Select *p){
134273 Parse *pParse = pWalker->pParse;
134274 int i, j, k;
134275 SrcList *pTabList;
134276 ExprList *pEList;
134277 struct SrcList_item *pFrom;
134278 sqlite3 *db = pParse->db;
134279 Expr *pE, *pRight, *pExpr;
134280 u16 selFlags = p->selFlags;
134281 u32 elistFlags = 0;
134282
134283 p->selFlags |= SF_Expanded;
134284 if( db->mallocFailed ){
134285 return WRC_Abort;
134286 }
134287 assert( p->pSrc!=0 );
134288 if( (selFlags & SF_Expanded)!=0 ){
134289 return WRC_Prune;
134290 }
134291 if( pWalker->eCode ){
134292 /* Renumber selId because it has been copied from a view */
134293 p->selId = ++pParse->nSelect;
134294 }
134295 pTabList = p->pSrc;
134296 pEList = p->pEList;
134297 sqlite3WithPush(pParse, p->pWith, 0);
134298
134299 /* Make sure cursor numbers have been assigned to all entries in
134300 ** the FROM clause of the SELECT statement.
134301 */
134302 sqlite3SrcListAssignCursors(pParse, pTabList);
134303
134304 /* Look up every table named in the FROM clause of the select. If
134305 ** an entry of the FROM clause is a subquery instead of a table or view,
134306 ** then create a transient table structure to describe the subquery.
134307 */
134308 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
134309 Table *pTab;
134310 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
134311 if( pFrom->pTab ) continue;
134312 assert( pFrom->fg.isRecursive==0 );
134313#ifndef SQLITE_OMIT_CTE
134314 if( withExpand(pWalker, pFrom) ) return WRC_Abort;
134315 if( pFrom->pTab ) {} else
134316#endif
134317 if( pFrom->zName==0 ){
134318#ifndef SQLITE_OMIT_SUBQUERY
134319 Select *pSel = pFrom->pSelect;
134320 /* A sub-query in the FROM clause of a SELECT */
134321 assert( pSel!=0 );
134322 assert( pFrom->pTab==0 );
134323 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
134324 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
134325#endif
134326 }else{
134327 /* An ordinary table or view name in the FROM clause */
134328 assert( pFrom->pTab==0 );
134329 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
134330 if( pTab==0 ) return WRC_Abort;
134331 if( pTab->nTabRef>=0xffff ){
134332 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
134333 pTab->zName);
134334 pFrom->pTab = 0;
134335 return WRC_Abort;
134336 }
134337 pTab->nTabRef++;
134338 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
134339 return WRC_Abort;
134340 }
134341#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
134342 if( IsVirtual(pTab) || pTab->pSelect ){
134343 i16 nCol;
134344 u8 eCodeOrig = pWalker->eCode;
134345 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
134346 assert( pFrom->pSelect==0 );
134347 if( pTab->pSelect && (db->flags & SQLITE_EnableView)==0 ){
134348 sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
134349 pTab->zName);
134350 }
134351#ifndef SQLITE_OMIT_VIRTUALTABLE
134352 if( IsVirtual(pTab)
134353 && pFrom->fg.fromDDL
134354 && ALWAYS(pTab->pVTable!=0)
134355 && pTab->pVTable->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
134356 ){
134357 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
134358 pTab->zName);
134359 }
134360#endif
134361 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
134362 nCol = pTab->nCol;
134363 pTab->nCol = -1;
134364 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
134365 sqlite3WalkSelect(pWalker, pFrom->pSelect);
134366 pWalker->eCode = eCodeOrig;
134367 pTab->nCol = nCol;
134368 }
134369#endif
134370 }
134371
134372 /* Locate the index named by the INDEXED BY clause, if any. */
134373 if( sqlite3IndexedByLookup(pParse, pFrom) ){
134374 return WRC_Abort;
134375 }
134376 }
134377
134378 /* Process NATURAL keywords, and ON and USING clauses of joins.
134379 */
134380 if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
134381 return WRC_Abort;
134382 }
134383
134384 /* For every "*" that occurs in the column list, insert the names of
134385 ** all columns in all tables. And for every TABLE.* insert the names
134386 ** of all columns in TABLE. The parser inserted a special expression
134387 ** with the TK_ASTERISK operator for each "*" that it found in the column
134388 ** list. The following code just has to locate the TK_ASTERISK
134389 ** expressions and expand each one to the list of all columns in
134390 ** all tables.
134391 **
134392 ** The first loop just checks to see if there are any "*" operators
134393 ** that need expanding.
134394 */
134395 for(k=0; k<pEList->nExpr; k++){
134396 pE = pEList->a[k].pExpr;
134397 if( pE->op==TK_ASTERISK ) break;
134398 assert( pE->op!=TK_DOT || pE->pRight!=0 );
134399 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
134400 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
134401 elistFlags |= pE->flags;
134402 }
134403 if( k<pEList->nExpr ){
134404 /*
134405 ** If we get here it means the result set contains one or more "*"
134406 ** operators that need to be expanded. Loop through each expression
134407 ** in the result set and expand them one by one.
134408 */
134409 struct ExprList_item *a = pEList->a;
134410 ExprList *pNew = 0;
134411 int flags = pParse->db->flags;
134412 int longNames = (flags & SQLITE_FullColNames)!=0
134413 && (flags & SQLITE_ShortColNames)==0;
134414
134415 for(k=0; k<pEList->nExpr; k++){
134416 pE = a[k].pExpr;
134417 elistFlags |= pE->flags;
134418 pRight = pE->pRight;
134419 assert( pE->op!=TK_DOT || pRight!=0 );
134420 if( pE->op!=TK_ASTERISK
134421 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
134422 ){
134423 /* This particular expression does not need to be expanded.
134424 */
134425 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
134426 if( pNew ){
134427 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
134428 pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
134429 a[k].zEName = 0;
134430 }
134431 a[k].pExpr = 0;
134432 }else{
134433 /* This expression is a "*" or a "TABLE.*" and needs to be
134434 ** expanded. */
134435 int tableSeen = 0; /* Set to 1 when TABLE matches */
134436 char *zTName = 0; /* text of name of TABLE */
134437 if( pE->op==TK_DOT ){
134438 assert( pE->pLeft!=0 );
134439 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
134440 zTName = pE->pLeft->u.zToken;
134441 }
134442 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
134443 Table *pTab = pFrom->pTab;
134444 Select *pSub = pFrom->pSelect;
134445 char *zTabName = pFrom->zAlias;
134446 const char *zSchemaName = 0;
134447 int iDb;
134448 if( zTabName==0 ){
134449 zTabName = pTab->zName;
134450 }
134451 if( db->mallocFailed ) break;
134452 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
134453 pSub = 0;
134454 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
134455 continue;
134456 }
134457 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134458 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
134459 }
134460 for(j=0; j<pTab->nCol; j++){
134461 char *zName = pTab->aCol[j].zName;
134462 char *zColname; /* The computed column name */
134463 char *zToFree; /* Malloced string that needs to be freed */
134464 Token sColname; /* Computed column name as a token */
134465
134466 assert( zName );
134467 if( zTName && pSub
134468 && sqlite3MatchEName(&pSub->pEList->a[j], 0, zTName, 0)==0
134469 ){
134470 continue;
134471 }
134472
134473 /* If a column is marked as 'hidden', omit it from the expanded
134474 ** result-set list unless the SELECT has the SF_IncludeHidden
134475 ** bit set.
134476 */
134477 if( (p->selFlags & SF_IncludeHidden)==0
134478 && IsHiddenColumn(&pTab->aCol[j])
134479 ){
134480 continue;
134481 }
134482 tableSeen = 1;
134483
134484 if( i>0 && zTName==0 ){
134485 if( (pFrom->fg.jointype & JT_NATURAL)!=0
134486 && tableAndColumnIndex(pTabList, i, zName, 0, 0, 1)
134487 ){
134488 /* In a NATURAL join, omit the join columns from the
134489 ** table to the right of the join */
134490 continue;
134491 }
134492 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
134493 /* In a join with a USING clause, omit columns in the
134494 ** using clause from the table on the right. */
134495 continue;
134496 }
134497 }
134498 pRight = sqlite3Expr(db, TK_ID, zName);
134499 zColname = zName;
134500 zToFree = 0;
134501 if( longNames || pTabList->nSrc>1 ){
134502 Expr *pLeft;
134503 pLeft = sqlite3Expr(db, TK_ID, zTabName);
134504 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
134505 if( zSchemaName ){
134506 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
134507 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
134508 }
134509 if( longNames ){
134510 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
134511 zToFree = zColname;
134512 }
134513 }else{
134514 pExpr = pRight;
134515 }
134516 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
134517 sqlite3TokenInit(&sColname, zColname);
134518 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
134519 if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
134520 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
134521 sqlite3DbFree(db, pX->zEName);
134522 if( pSub ){
134523 pX->zEName = sqlite3DbStrDup(db, pSub->pEList->a[j].zEName);
134524 testcase( pX->zEName==0 );
134525 }else{
134526 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
134527 zSchemaName, zTabName, zColname);
134528 testcase( pX->zEName==0 );
134529 }
134530 pX->eEName = ENAME_TAB;
134531 }
134532 sqlite3DbFree(db, zToFree);
134533 }
134534 }
134535 if( !tableSeen ){
134536 if( zTName ){
134537 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
134538 }else{
134539 sqlite3ErrorMsg(pParse, "no tables specified");
134540 }
134541 }
134542 }
134543 }
134544 sqlite3ExprListDelete(db, pEList);
134545 p->pEList = pNew;
134546 }
134547 if( p->pEList ){
134548 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
134549 sqlite3ErrorMsg(pParse, "too many columns in result set");
134550 return WRC_Abort;
134551 }
134552 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
134553 p->selFlags |= SF_ComplexResult;
134554 }
134555 }
134556 return WRC_Continue;
134557}
134558
134559#if SQLITE_DEBUG
134560/*
134561** Always assert. This xSelectCallback2 implementation proves that the
134562** xSelectCallback2 is never invoked.
134563*/
134564SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
134565 UNUSED_PARAMETER2(NotUsed, NotUsed2);
134566 assert( 0 );
134567}
134568#endif
134569/*
134570** This routine "expands" a SELECT statement and all of its subqueries.
134571** For additional information on what it means to "expand" a SELECT
134572** statement, see the comment on the selectExpand worker callback above.
134573**
134574** Expanding a SELECT statement is the first step in processing a
134575** SELECT statement. The SELECT statement must be expanded before
134576** name resolution is performed.
134577**
134578** If anything goes wrong, an error message is written into pParse.
134579** The calling function can detect the problem by looking at pParse->nErr
134580** and/or pParse->db->mallocFailed.
134581*/
134582static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
134583 Walker w;
134584 w.xExprCallback = sqlite3ExprWalkNoop;
134585 w.pParse = pParse;
134586 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
134587 w.xSelectCallback = convertCompoundSelectToSubquery;
134588 w.xSelectCallback2 = 0;
134589 sqlite3WalkSelect(&w, pSelect);
134590 }
134591 w.xSelectCallback = selectExpander;
134592 w.xSelectCallback2 = selectPopWith;
134593 w.eCode = 0;
134594 sqlite3WalkSelect(&w, pSelect);
134595}
134596
134597
134598#ifndef SQLITE_OMIT_SUBQUERY
134599/*
134600** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
134601** interface.
134602**
134603** For each FROM-clause subquery, add Column.zType and Column.zColl
134604** information to the Table structure that represents the result set
134605** of that subquery.
134606**
134607** The Table structure that represents the result set was constructed
134608** by selectExpander() but the type and collation information was omitted
134609** at that point because identifiers had not yet been resolved. This
134610** routine is called after identifier resolution.
134611*/
134612static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
134613 Parse *pParse;
134614 int i;
134615 SrcList *pTabList;
134616 struct SrcList_item *pFrom;
134617
134618 assert( p->selFlags & SF_Resolved );
134619 if( p->selFlags & SF_HasTypeInfo ) return;
134620 p->selFlags |= SF_HasTypeInfo;
134621 pParse = pWalker->pParse;
134622 pTabList = p->pSrc;
134623 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
134624 Table *pTab = pFrom->pTab;
134625 assert( pTab!=0 );
134626 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
134627 /* A sub-query in the FROM clause of a SELECT */
134628 Select *pSel = pFrom->pSelect;
134629 if( pSel ){
134630 while( pSel->pPrior ) pSel = pSel->pPrior;
134631 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
134632 SQLITE_AFF_NONE);
134633 }
134634 }
134635 }
134636}
134637#endif
134638
134639
134640/*
134641** This routine adds datatype and collating sequence information to
134642** the Table structures of all FROM-clause subqueries in a
134643** SELECT statement.
134644**
134645** Use this routine after name resolution.
134646*/
134647static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
134648#ifndef SQLITE_OMIT_SUBQUERY
134649 Walker w;
134650 w.xSelectCallback = sqlite3SelectWalkNoop;
134651 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
134652 w.xExprCallback = sqlite3ExprWalkNoop;
134653 w.pParse = pParse;
134654 sqlite3WalkSelect(&w, pSelect);
134655#endif
134656}
134657
134658
134659/*
134660** This routine sets up a SELECT statement for processing. The
134661** following is accomplished:
134662**
134663** * VDBE Cursor numbers are assigned to all FROM-clause terms.
134664** * Ephemeral Table objects are created for all FROM-clause subqueries.
134665** * ON and USING clauses are shifted into WHERE statements
134666** * Wildcards "*" and "TABLE.*" in result sets are expanded.
134667** * Identifiers in expression are matched to tables.
134668**
134669** This routine acts recursively on all subqueries within the SELECT.
134670*/
134671SQLITE_PRIVATE void sqlite3SelectPrep(
134672 Parse *pParse, /* The parser context */
134673 Select *p, /* The SELECT statement being coded. */
134674 NameContext *pOuterNC /* Name context for container */
134675){
134676 assert( p!=0 || pParse->db->mallocFailed );
134677 if( pParse->db->mallocFailed ) return;
134678 if( p->selFlags & SF_HasTypeInfo ) return;
134679 sqlite3SelectExpand(pParse, p);
134680 if( pParse->nErr || pParse->db->mallocFailed ) return;
134681 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
134682 if( pParse->nErr || pParse->db->mallocFailed ) return;
134683 sqlite3SelectAddTypeInfo(pParse, p);
134684}
134685
134686/*
134687** Reset the aggregate accumulator.
134688**
134689** The aggregate accumulator is a set of memory cells that hold
134690** intermediate results while calculating an aggregate. This
134691** routine generates code that stores NULLs in all of those memory
134692** cells.
134693*/
134694static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
134695 Vdbe *v = pParse->pVdbe;
134696 int i;
134697 struct AggInfo_func *pFunc;
134698 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
134699 if( nReg==0 ) return;
134700 if( pParse->nErr || pParse->db->mallocFailed ) return;
134701#ifdef SQLITE_DEBUG
134702 /* Verify that all AggInfo registers are within the range specified by
134703 ** AggInfo.mnReg..AggInfo.mxReg */
134704 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
134705 for(i=0; i<pAggInfo->nColumn; i++){
134706 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
134707 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
134708 }
134709 for(i=0; i<pAggInfo->nFunc; i++){
134710 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
134711 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
134712 }
134713#endif
134714 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
134715 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
134716 if( pFunc->iDistinct>=0 ){
134717 Expr *pE = pFunc->pFExpr;
134718 assert( !ExprHasProperty(pE, EP_xIsSelect) );
134719 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
134720 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
134721 "argument");
134722 pFunc->iDistinct = -1;
134723 }else{
134724 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
134725 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
134726 (char*)pKeyInfo, P4_KEYINFO);
134727 }
134728 }
134729 }
134730}
134731
134732/*
134733** Invoke the OP_AggFinalize opcode for every aggregate function
134734** in the AggInfo structure.
134735*/
134736static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
134737 Vdbe *v = pParse->pVdbe;
134738 int i;
134739 struct AggInfo_func *pF;
134740 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
134741 ExprList *pList = pF->pFExpr->x.pList;
134742 assert( !ExprHasProperty(pF->pFExpr, EP_xIsSelect) );
134743 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
134744 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
134745 }
134746}
134747
134748
134749/*
134750** Update the accumulator memory cells for an aggregate based on
134751** the current cursor position.
134752**
134753** If regAcc is non-zero and there are no min() or max() aggregates
134754** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
134755** registers if register regAcc contains 0. The caller will take care
134756** of setting and clearing regAcc.
134757*/
134758static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
134759 Vdbe *v = pParse->pVdbe;
134760 int i;
134761 int regHit = 0;
134762 int addrHitTest = 0;
134763 struct AggInfo_func *pF;
134764 struct AggInfo_col *pC;
134765
134766 pAggInfo->directMode = 1;
134767 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
134768 int nArg;
134769 int addrNext = 0;
134770 int regAgg;
134771 ExprList *pList = pF->pFExpr->x.pList;
134772 assert( !ExprHasProperty(pF->pFExpr, EP_xIsSelect) );
134773 assert( !IsWindowFunc(pF->pFExpr) );
134774 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
134775 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
134776 if( pAggInfo->nAccumulator
134777 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
134778 && regAcc
134779 ){
134780 /* If regAcc==0, there there exists some min() or max() function
134781 ** without a FILTER clause that will ensure the magnet registers
134782 ** are populated. */
134783 if( regHit==0 ) regHit = ++pParse->nMem;
134784 /* If this is the first row of the group (regAcc contains 0), clear the
134785 ** "magnet" register regHit so that the accumulator registers
134786 ** are populated if the FILTER clause jumps over the the
134787 ** invocation of min() or max() altogether. Or, if this is not
134788 ** the first row (regAcc contains 1), set the magnet register so that
134789 ** the accumulators are not populated unless the min()/max() is invoked
134790 ** and indicates that they should be. */
134791 sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
134792 }
134793 addrNext = sqlite3VdbeMakeLabel(pParse);
134794 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
134795 }
134796 if( pList ){
134797 nArg = pList->nExpr;
134798 regAgg = sqlite3GetTempRange(pParse, nArg);
134799 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
134800 }else{
134801 nArg = 0;
134802 regAgg = 0;
134803 }
134804 if( pF->iDistinct>=0 ){
134805 if( addrNext==0 ){
134806 addrNext = sqlite3VdbeMakeLabel(pParse);
134807 }
134808 testcase( nArg==0 ); /* Error condition */
134809 testcase( nArg>1 ); /* Also an error */
134810 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
134811 }
134812 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
134813 CollSeq *pColl = 0;
134814 struct ExprList_item *pItem;
134815 int j;
134816 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
134817 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
134818 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
134819 }
134820 if( !pColl ){
134821 pColl = pParse->db->pDfltColl;
134822 }
134823 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
134824 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
134825 }
134826 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
134827 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
134828 sqlite3VdbeChangeP5(v, (u8)nArg);
134829 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
134830 if( addrNext ){
134831 sqlite3VdbeResolveLabel(v, addrNext);
134832 }
134833 }
134834 if( regHit==0 && pAggInfo->nAccumulator ){
134835 regHit = regAcc;
134836 }
134837 if( regHit ){
134838 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
134839 }
134840 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
134841 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
134842 }
134843
134844 pAggInfo->directMode = 0;
134845 if( addrHitTest ){
134846 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
134847 }
134848}
134849
134850/*
134851** Add a single OP_Explain instruction to the VDBE to explain a simple
134852** count(*) query ("SELECT count(*) FROM pTab").
134853*/
134854#ifndef SQLITE_OMIT_EXPLAIN
134855static void explainSimpleCount(
134856 Parse *pParse, /* Parse context */
134857 Table *pTab, /* Table being queried */
134858 Index *pIdx /* Index used to optimize scan, or NULL */
134859){
134860 if( pParse->explain==2 ){
134861 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
134862 sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
134863 pTab->zName,
134864 bCover ? " USING COVERING INDEX " : "",
134865 bCover ? pIdx->zName : ""
134866 );
134867 }
134868}
134869#else
134870# define explainSimpleCount(a,b,c)
134871#endif
134872
134873/*
134874** sqlite3WalkExpr() callback used by havingToWhere().
134875**
134876** If the node passed to the callback is a TK_AND node, return
134877** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
134878**
134879** Otherwise, return WRC_Prune. In this case, also check if the
134880** sub-expression matches the criteria for being moved to the WHERE
134881** clause. If so, add it to the WHERE clause and replace the sub-expression
134882** within the HAVING expression with a constant "1".
134883*/
134884static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
134885 if( pExpr->op!=TK_AND ){
134886 Select *pS = pWalker->u.pSelect;
134887 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
134888 sqlite3 *db = pWalker->pParse->db;
134889 Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
134890 if( pNew ){
134891 Expr *pWhere = pS->pWhere;
134892 SWAP(Expr, *pNew, *pExpr);
134893 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
134894 pS->pWhere = pNew;
134895 pWalker->eCode = 1;
134896 }
134897 }
134898 return WRC_Prune;
134899 }
134900 return WRC_Continue;
134901}
134902
134903/*
134904** Transfer eligible terms from the HAVING clause of a query, which is
134905** processed after grouping, to the WHERE clause, which is processed before
134906** grouping. For example, the query:
134907**
134908** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
134909**
134910** can be rewritten as:
134911**
134912** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
134913**
134914** A term of the HAVING expression is eligible for transfer if it consists
134915** entirely of constants and expressions that are also GROUP BY terms that
134916** use the "BINARY" collation sequence.
134917*/
134918static void havingToWhere(Parse *pParse, Select *p){
134919 Walker sWalker;
134920 memset(&sWalker, 0, sizeof(sWalker));
134921 sWalker.pParse = pParse;
134922 sWalker.xExprCallback = havingToWhereExprCb;
134923 sWalker.u.pSelect = p;
134924 sqlite3WalkExpr(&sWalker, p->pHaving);
134925#if SELECTTRACE_ENABLED
134926 if( sWalker.eCode && (sqlite3_unsupported_selecttrace & 0x100)!=0 ){
134927 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
134928 sqlite3TreeViewSelect(0, p, 0);
134929 }
134930#endif
134931}
134932
134933/*
134934** Check to see if the pThis entry of pTabList is a self-join of a prior view.
134935** If it is, then return the SrcList_item for the prior view. If it is not,
134936** then return 0.
134937*/
134938static struct SrcList_item *isSelfJoinView(
134939 SrcList *pTabList, /* Search for self-joins in this FROM clause */
134940 struct SrcList_item *pThis /* Search for prior reference to this subquery */
134941){
134942 struct SrcList_item *pItem;
134943 for(pItem = pTabList->a; pItem<pThis; pItem++){
134944 Select *pS1;
134945 if( pItem->pSelect==0 ) continue;
134946 if( pItem->fg.viaCoroutine ) continue;
134947 if( pItem->zName==0 ) continue;
134948 assert( pItem->pTab!=0 );
134949 assert( pThis->pTab!=0 );
134950 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
134951 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
134952 pS1 = pItem->pSelect;
134953 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
134954 /* The query flattener left two different CTE tables with identical
134955 ** names in the same FROM clause. */
134956 continue;
134957 }
134958 if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
134959 || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1)
134960 ){
134961 /* The view was modified by some other optimization such as
134962 ** pushDownWhereTerms() */
134963 continue;
134964 }
134965 return pItem;
134966 }
134967 return 0;
134968}
134969
134970#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
134971/*
134972** Attempt to transform a query of the form
134973**
134974** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
134975**
134976** Into this:
134977**
134978** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
134979**
134980** The transformation only works if all of the following are true:
134981**
134982** * The subquery is a UNION ALL of two or more terms
134983** * The subquery does not have a LIMIT clause
134984** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
134985** * The outer query is a simple count(*) with no WHERE clause or other
134986** extraneous syntax.
134987**
134988** Return TRUE if the optimization is undertaken.
134989*/
134990static int countOfViewOptimization(Parse *pParse, Select *p){
134991 Select *pSub, *pPrior;
134992 Expr *pExpr;
134993 Expr *pCount;
134994 sqlite3 *db;
134995 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
134996 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
134997 if( p->pWhere ) return 0;
134998 if( p->pGroupBy ) return 0;
134999 pExpr = p->pEList->a[0].pExpr;
135000 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
135001 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
135002 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
135003 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
135004 pSub = p->pSrc->a[0].pSelect;
135005 if( pSub==0 ) return 0; /* The FROM is a subquery */
135006 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
135007 do{
135008 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
135009 if( pSub->pWhere ) return 0; /* No WHERE clause */
135010 if( pSub->pLimit ) return 0; /* No LIMIT clause */
135011 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
135012 pSub = pSub->pPrior; /* Repeat over compound */
135013 }while( pSub );
135014
135015 /* If we reach this point then it is OK to perform the transformation */
135016
135017 db = pParse->db;
135018 pCount = pExpr;
135019 pExpr = 0;
135020 pSub = p->pSrc->a[0].pSelect;
135021 p->pSrc->a[0].pSelect = 0;
135022 sqlite3SrcListDelete(db, p->pSrc);
135023 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
135024 while( pSub ){
135025 Expr *pTerm;
135026 pPrior = pSub->pPrior;
135027 pSub->pPrior = 0;
135028 pSub->pNext = 0;
135029 pSub->selFlags |= SF_Aggregate;
135030 pSub->selFlags &= ~SF_Compound;
135031 pSub->nSelectRow = 0;
135032 sqlite3ExprListDelete(db, pSub->pEList);
135033 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
135034 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
135035 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
135036 sqlite3PExprAddSelect(pParse, pTerm, pSub);
135037 if( pExpr==0 ){
135038 pExpr = pTerm;
135039 }else{
135040 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
135041 }
135042 pSub = pPrior;
135043 }
135044 p->pEList->a[0].pExpr = pExpr;
135045 p->selFlags &= ~SF_Aggregate;
135046
135047#if SELECTTRACE_ENABLED
135048 if( sqlite3_unsupported_selecttrace & 0x400 ){
135049 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
135050 sqlite3TreeViewSelect(0, p, 0);
135051 }
135052#endif
135053 return 1;
135054}
135055#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
135056
135057/*
135058** Generate code for the SELECT statement given in the p argument.
135059**
135060** The results are returned according to the SelectDest structure.
135061** See comments in sqliteInt.h for further information.
135062**
135063** This routine returns the number of errors. If any errors are
135064** encountered, then an appropriate error message is left in
135065** pParse->zErrMsg.
135066**
135067** This routine does NOT free the Select structure passed in. The
135068** calling function needs to do that.
135069*/
135070SQLITE_PRIVATE int sqlite3Select(
135071 Parse *pParse, /* The parser context */
135072 Select *p, /* The SELECT statement being coded. */
135073 SelectDest *pDest /* What to do with the query results */
135074){
135075 int i, j; /* Loop counters */
135076 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
135077 Vdbe *v; /* The virtual machine under construction */
135078 int isAgg; /* True for select lists like "count(*)" */
135079 ExprList *pEList = 0; /* List of columns to extract. */
135080 SrcList *pTabList; /* List of tables to select from */
135081 Expr *pWhere; /* The WHERE clause. May be NULL */
135082 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
135083 Expr *pHaving; /* The HAVING clause. May be NULL */
135084 AggInfo *pAggInfo = 0; /* Aggregate information */
135085 int rc = 1; /* Value to return from this function */
135086 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
135087 SortCtx sSort; /* Info on how to code the ORDER BY clause */
135088 int iEnd; /* Address of the end of the query */
135089 sqlite3 *db; /* The database connection */
135090 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
135091 u8 minMaxFlag; /* Flag for min/max queries */
135092
135093 db = pParse->db;
135094 v = sqlite3GetVdbe(pParse);
135095 if( p==0 || db->mallocFailed || pParse->nErr ){
135096 return 1;
135097 }
135098 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
135099#if SELECTTRACE_ENABLED
135100 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
135101 if( sqlite3_unsupported_selecttrace & 0x100 ){
135102 sqlite3TreeViewSelect(0, p, 0);
135103 }
135104#endif
135105
135106 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
135107 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
135108 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
135109 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
135110 if( IgnorableOrderby(pDest) ){
135111 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
135112 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
135113 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
135114 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
135115 /* If ORDER BY makes no difference in the output then neither does
135116 ** DISTINCT so it can be removed too. */
135117 sqlite3ExprListDelete(db, p->pOrderBy);
135118 p->pOrderBy = 0;
135119 p->selFlags &= ~SF_Distinct;
135120 p->selFlags |= SF_NoopOrderBy;
135121 }
135122 sqlite3SelectPrep(pParse, p, 0);
135123 if( pParse->nErr || db->mallocFailed ){
135124 goto select_end;
135125 }
135126 assert( p->pEList!=0 );
135127#if SELECTTRACE_ENABLED
135128 if( sqlite3_unsupported_selecttrace & 0x104 ){
135129 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
135130 sqlite3TreeViewSelect(0, p, 0);
135131 }
135132#endif
135133
135134 /* If the SF_UpdateFrom flag is set, then this function is being called
135135 ** as part of populating the temp table for an UPDATE...FROM statement.
135136 ** In this case, it is an error if the target object (pSrc->a[0]) name
135137 ** or alias is duplicated within FROM clause (pSrc->a[1..n]). */
135138 if( p->selFlags & SF_UpdateFrom ){
135139 struct SrcList_item *p0 = &p->pSrc->a[0];
135140 for(i=1; i<p->pSrc->nSrc; i++){
135141 struct SrcList_item *p1 = &p->pSrc->a[i];
135142 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
135143 sqlite3ErrorMsg(pParse,
135144 "target object/alias may not appear in FROM clause: %s",
135145 p0->zAlias ? p0->zAlias : p0->pTab->zName
135146 );
135147 goto select_end;
135148 }
135149 }
135150 }
135151
135152 if( pDest->eDest==SRT_Output ){
135153 generateColumnNames(pParse, p);
135154 }
135155
135156#ifndef SQLITE_OMIT_WINDOWFUNC
135157 rc = sqlite3WindowRewrite(pParse, p);
135158 if( rc ){
135159 assert( db->mallocFailed || pParse->nErr>0 );
135160 goto select_end;
135161 }
135162#if SELECTTRACE_ENABLED
135163 if( p->pWin && (sqlite3_unsupported_selecttrace & 0x108)!=0 ){
135164 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
135165 sqlite3TreeViewSelect(0, p, 0);
135166 }
135167#endif
135168#endif /* SQLITE_OMIT_WINDOWFUNC */
135169 pTabList = p->pSrc;
135170 isAgg = (p->selFlags & SF_Aggregate)!=0;
135171 memset(&sSort, 0, sizeof(sSort));
135172 sSort.pOrderBy = p->pOrderBy;
135173
135174 /* Try to do various optimizations (flattening subqueries, and strength
135175 ** reduction of join operators) in the FROM clause up into the main query
135176 */
135177#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
135178 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
135179 struct SrcList_item *pItem = &pTabList->a[i];
135180 Select *pSub = pItem->pSelect;
135181 Table *pTab = pItem->pTab;
135182
135183 /* The expander should have already created transient Table objects
135184 ** even for FROM clause elements such as subqueries that do not correspond
135185 ** to a real table */
135186 assert( pTab!=0 );
135187
135188 /* Convert LEFT JOIN into JOIN if there are terms of the right table
135189 ** of the LEFT JOIN used in the WHERE clause.
135190 */
135191 if( (pItem->fg.jointype & JT_LEFT)!=0
135192 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
135193 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
135194 ){
135195 SELECTTRACE(0x100,pParse,p,
135196 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
135197 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
135198 unsetJoinExpr(p->pWhere, pItem->iCursor);
135199 }
135200
135201 /* No futher action if this term of the FROM clause is no a subquery */
135202 if( pSub==0 ) continue;
135203
135204 /* Catch mismatch in the declared columns of a view and the number of
135205 ** columns in the SELECT on the RHS */
135206 if( pTab->nCol!=pSub->pEList->nExpr ){
135207 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
135208 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
135209 goto select_end;
135210 }
135211
135212 /* Do not try to flatten an aggregate subquery.
135213 **
135214 ** Flattening an aggregate subquery is only possible if the outer query
135215 ** is not a join. But if the outer query is not a join, then the subquery
135216 ** will be implemented as a co-routine and there is no advantage to
135217 ** flattening in that case.
135218 */
135219 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
135220 assert( pSub->pGroupBy==0 );
135221
135222 /* If the outer query contains a "complex" result set (that is,
135223 ** if the result set of the outer query uses functions or subqueries)
135224 ** and if the subquery contains an ORDER BY clause and if
135225 ** it will be implemented as a co-routine, then do not flatten. This
135226 ** restriction allows SQL constructs like this:
135227 **
135228 ** SELECT expensive_function(x)
135229 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
135230 **
135231 ** The expensive_function() is only computed on the 10 rows that
135232 ** are output, rather than every row of the table.
135233 **
135234 ** The requirement that the outer query have a complex result set
135235 ** means that flattening does occur on simpler SQL constraints without
135236 ** the expensive_function() like:
135237 **
135238 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
135239 */
135240 if( pSub->pOrderBy!=0
135241 && i==0
135242 && (p->selFlags & SF_ComplexResult)!=0
135243 && (pTabList->nSrc==1
135244 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
135245 ){
135246 continue;
135247 }
135248
135249 if( flattenSubquery(pParse, p, i, isAgg) ){
135250 if( pParse->nErr ) goto select_end;
135251 /* This subquery can be absorbed into its parent. */
135252 i = -1;
135253 }
135254 pTabList = p->pSrc;
135255 if( db->mallocFailed ) goto select_end;
135256 if( !IgnorableOrderby(pDest) ){
135257 sSort.pOrderBy = p->pOrderBy;
135258 }
135259 }
135260#endif
135261
135262#ifndef SQLITE_OMIT_COMPOUND_SELECT
135263 /* Handle compound SELECT statements using the separate multiSelect()
135264 ** procedure.
135265 */
135266 if( p->pPrior ){
135267 rc = multiSelect(pParse, p, pDest);
135268#if SELECTTRACE_ENABLED
135269 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
135270 if( (sqlite3_unsupported_selecttrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
135271 sqlite3TreeViewSelect(0, p, 0);
135272 }
135273#endif
135274 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
135275 return rc;
135276 }
135277#endif
135278
135279 /* Do the WHERE-clause constant propagation optimization if this is
135280 ** a join. No need to speed time on this operation for non-join queries
135281 ** as the equivalent optimization will be handled by query planner in
135282 ** sqlite3WhereBegin().
135283 */
135284 if( pTabList->nSrc>1
135285 && OptimizationEnabled(db, SQLITE_PropagateConst)
135286 && propagateConstants(pParse, p)
135287 ){
135288#if SELECTTRACE_ENABLED
135289 if( sqlite3_unsupported_selecttrace & 0x100 ){
135290 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
135291 sqlite3TreeViewSelect(0, p, 0);
135292 }
135293#endif
135294 }else{
135295 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
135296 }
135297
135298#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
135299 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
135300 && countOfViewOptimization(pParse, p)
135301 ){
135302 if( db->mallocFailed ) goto select_end;
135303 pEList = p->pEList;
135304 pTabList = p->pSrc;
135305 }
135306#endif
135307
135308 /* For each term in the FROM clause, do two things:
135309 ** (1) Authorized unreferenced tables
135310 ** (2) Generate code for all sub-queries
135311 */
135312 for(i=0; i<pTabList->nSrc; i++){
135313 struct SrcList_item *pItem = &pTabList->a[i];
135314 SelectDest dest;
135315 Select *pSub;
135316#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
135317 const char *zSavedAuthContext;
135318#endif
135319
135320 /* Issue SQLITE_READ authorizations with a fake column name for any
135321 ** tables that are referenced but from which no values are extracted.
135322 ** Examples of where these kinds of null SQLITE_READ authorizations
135323 ** would occur:
135324 **
135325 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
135326 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
135327 **
135328 ** The fake column name is an empty string. It is possible for a table to
135329 ** have a column named by the empty string, in which case there is no way to
135330 ** distinguish between an unreferenced table and an actual reference to the
135331 ** "" column. The original design was for the fake column name to be a NULL,
135332 ** which would be unambiguous. But legacy authorization callbacks might
135333 ** assume the column name is non-NULL and segfault. The use of an empty
135334 ** string for the fake column name seems safer.
135335 */
135336 if( pItem->colUsed==0 && pItem->zName!=0 ){
135337 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
135338 }
135339
135340#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
135341 /* Generate code for all sub-queries in the FROM clause
135342 */
135343 pSub = pItem->pSelect;
135344 if( pSub==0 ) continue;
135345
135346 /* The code for a subquery should only be generated once, though it is
135347 ** technically harmless for it to be generated multiple times. The
135348 ** following assert() will detect if something changes to cause
135349 ** the same subquery to be coded multiple times, as a signal to the
135350 ** developers to try to optimize the situation.
135351 **
135352 ** Update 2019-07-24:
135353 ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
135354 ** The dbsqlfuzz fuzzer found a case where the same subquery gets
135355 ** coded twice. So this assert() now becomes a testcase(). It should
135356 ** be very rare, though.
135357 */
135358 testcase( pItem->addrFillSub!=0 );
135359
135360 /* Increment Parse.nHeight by the height of the largest expression
135361 ** tree referred to by this, the parent select. The child select
135362 ** may contain expression trees of at most
135363 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
135364 ** more conservative than necessary, but much easier than enforcing
135365 ** an exact limit.
135366 */
135367 pParse->nHeight += sqlite3SelectExprHeight(p);
135368
135369 /* Make copies of constant WHERE-clause terms in the outer query down
135370 ** inside the subquery. This can help the subquery to run more efficiently.
135371 */
135372 if( OptimizationEnabled(db, SQLITE_PushDown)
135373 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
135374 (pItem->fg.jointype & JT_OUTER)!=0)
135375 ){
135376#if SELECTTRACE_ENABLED
135377 if( sqlite3_unsupported_selecttrace & 0x100 ){
135378 SELECTTRACE(0x100,pParse,p,
135379 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
135380 sqlite3TreeViewSelect(0, p, 0);
135381 }
135382#endif
135383 }else{
135384 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
135385 }
135386
135387 zSavedAuthContext = pParse->zAuthContext;
135388 pParse->zAuthContext = pItem->zName;
135389
135390 /* Generate code to implement the subquery
135391 **
135392 ** The subquery is implemented as a co-routine if the subquery is
135393 ** guaranteed to be the outer loop (so that it does not need to be
135394 ** computed more than once)
135395 **
135396 ** TODO: Are there other reasons beside (1) to use a co-routine
135397 ** implementation?
135398 */
135399 if( i==0
135400 && (pTabList->nSrc==1
135401 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
135402 ){
135403 /* Implement a co-routine that will return a single row of the result
135404 ** set on each invocation.
135405 */
135406 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
135407
135408 pItem->regReturn = ++pParse->nMem;
135409 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
135410 VdbeComment((v, "%s", pItem->pTab->zName));
135411 pItem->addrFillSub = addrTop;
135412 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
135413 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
135414 sqlite3Select(pParse, pSub, &dest);
135415 pItem->pTab->nRowLogEst = pSub->nSelectRow;
135416 pItem->fg.viaCoroutine = 1;
135417 pItem->regResult = dest.iSdst;
135418 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
135419 sqlite3VdbeJumpHere(v, addrTop-1);
135420 sqlite3ClearTempRegCache(pParse);
135421 }else{
135422 /* Generate a subroutine that will fill an ephemeral table with
135423 ** the content of this subquery. pItem->addrFillSub will point
135424 ** to the address of the generated subroutine. pItem->regReturn
135425 ** is a register allocated to hold the subroutine return address
135426 */
135427 int topAddr;
135428 int onceAddr = 0;
135429 int retAddr;
135430 struct SrcList_item *pPrior;
135431
135432 testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
135433 pItem->regReturn = ++pParse->nMem;
135434 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
135435 pItem->addrFillSub = topAddr+1;
135436 if( pItem->fg.isCorrelated==0 ){
135437 /* If the subquery is not correlated and if we are not inside of
135438 ** a trigger, then we only need to compute the value of the subquery
135439 ** once. */
135440 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
135441 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
135442 }else{
135443 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
135444 }
135445 pPrior = isSelfJoinView(pTabList, pItem);
135446 if( pPrior ){
135447 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
135448 assert( pPrior->pSelect!=0 );
135449 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
135450 }else{
135451 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
135452 ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
135453 sqlite3Select(pParse, pSub, &dest);
135454 }
135455 pItem->pTab->nRowLogEst = pSub->nSelectRow;
135456 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
135457 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
135458 VdbeComment((v, "end %s", pItem->pTab->zName));
135459 sqlite3VdbeChangeP1(v, topAddr, retAddr);
135460 sqlite3ClearTempRegCache(pParse);
135461 }
135462 if( db->mallocFailed ) goto select_end;
135463 pParse->nHeight -= sqlite3SelectExprHeight(p);
135464 pParse->zAuthContext = zSavedAuthContext;
135465#endif
135466 }
135467
135468 /* Various elements of the SELECT copied into local variables for
135469 ** convenience */
135470 pEList = p->pEList;
135471 pWhere = p->pWhere;
135472 pGroupBy = p->pGroupBy;
135473 pHaving = p->pHaving;
135474 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
135475
135476#if SELECTTRACE_ENABLED
135477 if( sqlite3_unsupported_selecttrace & 0x400 ){
135478 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
135479 sqlite3TreeViewSelect(0, p, 0);
135480 }
135481#endif
135482
135483 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
135484 ** if the select-list is the same as the ORDER BY list, then this query
135485 ** can be rewritten as a GROUP BY. In other words, this:
135486 **
135487 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
135488 **
135489 ** is transformed to:
135490 **
135491 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
135492 **
135493 ** The second form is preferred as a single index (or temp-table) may be
135494 ** used for both the ORDER BY and DISTINCT processing. As originally
135495 ** written the query must use a temp-table for at least one of the ORDER
135496 ** BY and DISTINCT, and an index or separate temp-table for the other.
135497 */
135498 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
135499 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
135500#ifndef SQLITE_OMIT_WINDOWFUNC
135501 && p->pWin==0
135502#endif
135503 ){
135504 p->selFlags &= ~SF_Distinct;
135505 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
135506 p->selFlags |= SF_Aggregate;
135507 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
135508 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
135509 ** original setting of the SF_Distinct flag, not the current setting */
135510 assert( sDistinct.isTnct );
135511
135512#if SELECTTRACE_ENABLED
135513 if( sqlite3_unsupported_selecttrace & 0x400 ){
135514 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
135515 sqlite3TreeViewSelect(0, p, 0);
135516 }
135517#endif
135518 }
135519
135520 /* If there is an ORDER BY clause, then create an ephemeral index to
135521 ** do the sorting. But this sorting ephemeral index might end up
135522 ** being unused if the data can be extracted in pre-sorted order.
135523 ** If that is the case, then the OP_OpenEphemeral instruction will be
135524 ** changed to an OP_Noop once we figure out that the sorting index is
135525 ** not needed. The sSort.addrSortIndex variable is used to facilitate
135526 ** that change.
135527 */
135528 if( sSort.pOrderBy ){
135529 KeyInfo *pKeyInfo;
135530 pKeyInfo = sqlite3KeyInfoFromExprList(
135531 pParse, sSort.pOrderBy, 0, pEList->nExpr);
135532 sSort.iECursor = pParse->nTab++;
135533 sSort.addrSortIndex =
135534 sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
135535 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
135536 (char*)pKeyInfo, P4_KEYINFO
135537 );
135538 }else{
135539 sSort.addrSortIndex = -1;
135540 }
135541
135542 /* If the output is destined for a temporary table, open that table.
135543 */
135544 if( pDest->eDest==SRT_EphemTab ){
135545 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
135546 }
135547
135548 /* Set the limiter.
135549 */
135550 iEnd = sqlite3VdbeMakeLabel(pParse);
135551 if( (p->selFlags & SF_FixedLimit)==0 ){
135552 p->nSelectRow = 320; /* 4 billion rows */
135553 }
135554 computeLimitRegisters(pParse, p, iEnd);
135555 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
135556 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
135557 sSort.sortFlags |= SORTFLAG_UseSorter;
135558 }
135559
135560 /* Open an ephemeral index to use for the distinct set.
135561 */
135562 if( p->selFlags & SF_Distinct ){
135563 sDistinct.tabTnct = pParse->nTab++;
135564 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
135565 sDistinct.tabTnct, 0, 0,
135566 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
135567 P4_KEYINFO);
135568 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
135569 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
135570 }else{
135571 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
135572 }
135573
135574 if( !isAgg && pGroupBy==0 ){
135575 /* No aggregate functions and no GROUP BY clause */
135576 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
135577 | (p->selFlags & SF_FixedLimit);
135578#ifndef SQLITE_OMIT_WINDOWFUNC
135579 Window *pWin = p->pWin; /* Main window object (or NULL) */
135580 if( pWin ){
135581 sqlite3WindowCodeInit(pParse, p);
135582 }
135583#endif
135584 assert( WHERE_USE_LIMIT==SF_FixedLimit );
135585
135586
135587 /* Begin the database scan. */
135588 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
135589 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
135590 p->pEList, wctrlFlags, p->nSelectRow);
135591 if( pWInfo==0 ) goto select_end;
135592 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
135593 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
135594 }
135595 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
135596 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
135597 }
135598 if( sSort.pOrderBy ){
135599 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
135600 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
135601 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
135602 sSort.pOrderBy = 0;
135603 }
135604 }
135605
135606 /* If sorting index that was created by a prior OP_OpenEphemeral
135607 ** instruction ended up not being needed, then change the OP_OpenEphemeral
135608 ** into an OP_Noop.
135609 */
135610 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
135611 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
135612 }
135613
135614 assert( p->pEList==pEList );
135615#ifndef SQLITE_OMIT_WINDOWFUNC
135616 if( pWin ){
135617 int addrGosub = sqlite3VdbeMakeLabel(pParse);
135618 int iCont = sqlite3VdbeMakeLabel(pParse);
135619 int iBreak = sqlite3VdbeMakeLabel(pParse);
135620 int regGosub = ++pParse->nMem;
135621
135622 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
135623
135624 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
135625 sqlite3VdbeResolveLabel(v, addrGosub);
135626 VdbeNoopComment((v, "inner-loop subroutine"));
135627 sSort.labelOBLopt = 0;
135628 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
135629 sqlite3VdbeResolveLabel(v, iCont);
135630 sqlite3VdbeAddOp1(v, OP_Return, regGosub);
135631 VdbeComment((v, "end inner-loop subroutine"));
135632 sqlite3VdbeResolveLabel(v, iBreak);
135633 }else
135634#endif /* SQLITE_OMIT_WINDOWFUNC */
135635 {
135636 /* Use the standard inner loop. */
135637 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
135638 sqlite3WhereContinueLabel(pWInfo),
135639 sqlite3WhereBreakLabel(pWInfo));
135640
135641 /* End the database scan loop.
135642 */
135643 sqlite3WhereEnd(pWInfo);
135644 }
135645 }else{
135646 /* This case when there exist aggregate functions or a GROUP BY clause
135647 ** or both */
135648 NameContext sNC; /* Name context for processing aggregate information */
135649 int iAMem; /* First Mem address for storing current GROUP BY */
135650 int iBMem; /* First Mem address for previous GROUP BY */
135651 int iUseFlag; /* Mem address holding flag indicating that at least
135652 ** one row of the input to the aggregator has been
135653 ** processed */
135654 int iAbortFlag; /* Mem address which causes query abort if positive */
135655 int groupBySort; /* Rows come from source in GROUP BY order */
135656 int addrEnd; /* End of processing for this SELECT */
135657 int sortPTab = 0; /* Pseudotable used to decode sorting results */
135658 int sortOut = 0; /* Output register from the sorter */
135659 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
135660
135661 /* Remove any and all aliases between the result set and the
135662 ** GROUP BY clause.
135663 */
135664 if( pGroupBy ){
135665 int k; /* Loop counter */
135666 struct ExprList_item *pItem; /* For looping over expression in a list */
135667
135668 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
135669 pItem->u.x.iAlias = 0;
135670 }
135671 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
135672 pItem->u.x.iAlias = 0;
135673 }
135674 assert( 66==sqlite3LogEst(100) );
135675 if( p->nSelectRow>66 ) p->nSelectRow = 66;
135676
135677 /* If there is both a GROUP BY and an ORDER BY clause and they are
135678 ** identical, then it may be possible to disable the ORDER BY clause
135679 ** on the grounds that the GROUP BY will cause elements to come out
135680 ** in the correct order. It also may not - the GROUP BY might use a
135681 ** database index that causes rows to be grouped together as required
135682 ** but not actually sorted. Either way, record the fact that the
135683 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
135684 ** variable. */
135685 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
135686 int ii;
135687 /* The GROUP BY processing doesn't care whether rows are delivered in
135688 ** ASC or DESC order - only that each group is returned contiguously.
135689 ** So set the ASC/DESC flags in the GROUP BY to match those in the
135690 ** ORDER BY to maximize the chances of rows being delivered in an
135691 ** order that makes the ORDER BY redundant. */
135692 for(ii=0; ii<pGroupBy->nExpr; ii++){
135693 u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
135694 pGroupBy->a[ii].sortFlags = sortFlags;
135695 }
135696 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
135697 orderByGrp = 1;
135698 }
135699 }
135700 }else{
135701 assert( 0==sqlite3LogEst(1) );
135702 p->nSelectRow = 0;
135703 }
135704
135705 /* Create a label to jump to when we want to abort the query */
135706 addrEnd = sqlite3VdbeMakeLabel(pParse);
135707
135708 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
135709 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
135710 ** SELECT statement.
135711 */
135712 pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) );
135713 if( pAggInfo==0 ){
135714 goto select_end;
135715 }
135716 pAggInfo->pNext = pParse->pAggList;
135717 pParse->pAggList = pAggInfo;
135718 pAggInfo->selId = p->selId;
135719 memset(&sNC, 0, sizeof(sNC));
135720 sNC.pParse = pParse;
135721 sNC.pSrcList = pTabList;
135722 sNC.uNC.pAggInfo = pAggInfo;
135723 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
135724 pAggInfo->mnReg = pParse->nMem+1;
135725 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
135726 pAggInfo->pGroupBy = pGroupBy;
135727 sqlite3ExprAnalyzeAggList(&sNC, pEList);
135728 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
135729 if( pHaving ){
135730 if( pGroupBy ){
135731 assert( pWhere==p->pWhere );
135732 assert( pHaving==p->pHaving );
135733 assert( pGroupBy==p->pGroupBy );
135734 havingToWhere(pParse, p);
135735 pWhere = p->pWhere;
135736 }
135737 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
135738 }
135739 pAggInfo->nAccumulator = pAggInfo->nColumn;
135740 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
135741 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
135742 }else{
135743 minMaxFlag = WHERE_ORDERBY_NORMAL;
135744 }
135745 for(i=0; i<pAggInfo->nFunc; i++){
135746 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
135747 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
135748 sNC.ncFlags |= NC_InAggFunc;
135749 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
135750#ifndef SQLITE_OMIT_WINDOWFUNC
135751 assert( !IsWindowFunc(pExpr) );
135752 if( ExprHasProperty(pExpr, EP_WinFunc) ){
135753 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
135754 }
135755#endif
135756 sNC.ncFlags &= ~NC_InAggFunc;
135757 }
135758 pAggInfo->mxReg = pParse->nMem;
135759 if( db->mallocFailed ) goto select_end;
135760#if SELECTTRACE_ENABLED
135761 if( sqlite3_unsupported_selecttrace & 0x400 ){
135762 int ii;
135763 SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
135764 sqlite3TreeViewSelect(0, p, 0);
135765 for(ii=0; ii<pAggInfo->nColumn; ii++){
135766 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
135767 ii, pAggInfo->aCol[ii].iMem);
135768 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
135769 }
135770 for(ii=0; ii<pAggInfo->nFunc; ii++){
135771 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
135772 ii, pAggInfo->aFunc[ii].iMem);
135773 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
135774 }
135775 }
135776#endif
135777
135778
135779 /* Processing for aggregates with GROUP BY is very different and
135780 ** much more complex than aggregates without a GROUP BY.
135781 */
135782 if( pGroupBy ){
135783 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
135784 int addr1; /* A-vs-B comparision jump */
135785 int addrOutputRow; /* Start of subroutine that outputs a result row */
135786 int regOutputRow; /* Return address register for output subroutine */
135787 int addrSetAbort; /* Set the abort flag and return */
135788 int addrTopOfLoop; /* Top of the input loop */
135789 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
135790 int addrReset; /* Subroutine for resetting the accumulator */
135791 int regReset; /* Return address register for reset subroutine */
135792
135793 /* If there is a GROUP BY clause we might need a sorting index to
135794 ** implement it. Allocate that sorting index now. If it turns out
135795 ** that we do not need it after all, the OP_SorterOpen instruction
135796 ** will be converted into a Noop.
135797 */
135798 pAggInfo->sortingIdx = pParse->nTab++;
135799 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy,
135800 0, pAggInfo->nColumn);
135801 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
135802 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
135803 0, (char*)pKeyInfo, P4_KEYINFO);
135804
135805 /* Initialize memory locations used by GROUP BY aggregate processing
135806 */
135807 iUseFlag = ++pParse->nMem;
135808 iAbortFlag = ++pParse->nMem;
135809 regOutputRow = ++pParse->nMem;
135810 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
135811 regReset = ++pParse->nMem;
135812 addrReset = sqlite3VdbeMakeLabel(pParse);
135813 iAMem = pParse->nMem + 1;
135814 pParse->nMem += pGroupBy->nExpr;
135815 iBMem = pParse->nMem + 1;
135816 pParse->nMem += pGroupBy->nExpr;
135817 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
135818 VdbeComment((v, "clear abort flag"));
135819 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
135820
135821 /* Begin a loop that will extract all source rows in GROUP BY order.
135822 ** This might involve two separate loops with an OP_Sort in between, or
135823 ** it might be a single loop that uses an index to extract information
135824 ** in the right order to begin with.
135825 */
135826 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
135827 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
135828 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
135829 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
135830 );
135831 if( pWInfo==0 ) goto select_end;
135832 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
135833 /* The optimizer is able to deliver rows in group by order so
135834 ** we do not have to sort. The OP_OpenEphemeral table will be
135835 ** cancelled later because we still need to use the pKeyInfo
135836 */
135837 groupBySort = 0;
135838 }else{
135839 /* Rows are coming out in undetermined order. We have to push
135840 ** each row into a sorting index, terminate the first loop,
135841 ** then loop over the sorting index in order to get the output
135842 ** in sorted order
135843 */
135844 int regBase;
135845 int regRecord;
135846 int nCol;
135847 int nGroupBy;
135848
135849 explainTempTable(pParse,
135850 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
135851 "DISTINCT" : "GROUP BY");
135852
135853 groupBySort = 1;
135854 nGroupBy = pGroupBy->nExpr;
135855 nCol = nGroupBy;
135856 j = nGroupBy;
135857 for(i=0; i<pAggInfo->nColumn; i++){
135858 if( pAggInfo->aCol[i].iSorterColumn>=j ){
135859 nCol++;
135860 j++;
135861 }
135862 }
135863 regBase = sqlite3GetTempRange(pParse, nCol);
135864 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
135865 j = nGroupBy;
135866 for(i=0; i<pAggInfo->nColumn; i++){
135867 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
135868 if( pCol->iSorterColumn>=j ){
135869 int r1 = j + regBase;
135870 sqlite3ExprCodeGetColumnOfTable(v,
135871 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
135872 j++;
135873 }
135874 }
135875 regRecord = sqlite3GetTempReg(pParse);
135876 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
135877 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
135878 sqlite3ReleaseTempReg(pParse, regRecord);
135879 sqlite3ReleaseTempRange(pParse, regBase, nCol);
135880 sqlite3WhereEnd(pWInfo);
135881 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
135882 sortOut = sqlite3GetTempReg(pParse);
135883 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
135884 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
135885 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
135886 pAggInfo->useSortingIdx = 1;
135887 }
135888
135889 /* If the index or temporary table used by the GROUP BY sort
135890 ** will naturally deliver rows in the order required by the ORDER BY
135891 ** clause, cancel the ephemeral table open coded earlier.
135892 **
135893 ** This is an optimization - the correct answer should result regardless.
135894 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
135895 ** disable this optimization for testing purposes. */
135896 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
135897 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
135898 ){
135899 sSort.pOrderBy = 0;
135900 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
135901 }
135902
135903 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
135904 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
135905 ** Then compare the current GROUP BY terms against the GROUP BY terms
135906 ** from the previous row currently stored in a0, a1, a2...
135907 */
135908 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
135909 if( groupBySort ){
135910 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
135911 sortOut, sortPTab);
135912 }
135913 for(j=0; j<pGroupBy->nExpr; j++){
135914 if( groupBySort ){
135915 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
135916 }else{
135917 pAggInfo->directMode = 1;
135918 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
135919 }
135920 }
135921 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
135922 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
135923 addr1 = sqlite3VdbeCurrentAddr(v);
135924 sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
135925
135926 /* Generate code that runs whenever the GROUP BY changes.
135927 ** Changes in the GROUP BY are detected by the previous code
135928 ** block. If there were no changes, this block is skipped.
135929 **
135930 ** This code copies current group by terms in b0,b1,b2,...
135931 ** over to a0,a1,a2. It then calls the output subroutine
135932 ** and resets the aggregate accumulator registers in preparation
135933 ** for the next GROUP BY batch.
135934 */
135935 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
135936 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
135937 VdbeComment((v, "output one row"));
135938 sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
135939 VdbeComment((v, "check abort flag"));
135940 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
135941 VdbeComment((v, "reset accumulator"));
135942
135943 /* Update the aggregate accumulators based on the content of
135944 ** the current row
135945 */
135946 sqlite3VdbeJumpHere(v, addr1);
135947 updateAccumulator(pParse, iUseFlag, pAggInfo);
135948 sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
135949 VdbeComment((v, "indicate data in accumulator"));
135950
135951 /* End of the loop
135952 */
135953 if( groupBySort ){
135954 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx, addrTopOfLoop);
135955 VdbeCoverage(v);
135956 }else{
135957 sqlite3WhereEnd(pWInfo);
135958 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
135959 }
135960
135961 /* Output the final row of result
135962 */
135963 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
135964 VdbeComment((v, "output final row"));
135965
135966 /* Jump over the subroutines
135967 */
135968 sqlite3VdbeGoto(v, addrEnd);
135969
135970 /* Generate a subroutine that outputs a single row of the result
135971 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
135972 ** is less than or equal to zero, the subroutine is a no-op. If
135973 ** the processing calls for the query to abort, this subroutine
135974 ** increments the iAbortFlag memory location before returning in
135975 ** order to signal the caller to abort.
135976 */
135977 addrSetAbort = sqlite3VdbeCurrentAddr(v);
135978 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
135979 VdbeComment((v, "set abort flag"));
135980 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135981 sqlite3VdbeResolveLabel(v, addrOutputRow);
135982 addrOutputRow = sqlite3VdbeCurrentAddr(v);
135983 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
135984 VdbeCoverage(v);
135985 VdbeComment((v, "Groupby result generator entry point"));
135986 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135987 finalizeAggFunctions(pParse, pAggInfo);
135988 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
135989 selectInnerLoop(pParse, p, -1, &sSort,
135990 &sDistinct, pDest,
135991 addrOutputRow+1, addrSetAbort);
135992 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
135993 VdbeComment((v, "end groupby result generator"));
135994
135995 /* Generate a subroutine that will reset the group-by accumulator
135996 */
135997 sqlite3VdbeResolveLabel(v, addrReset);
135998 resetAccumulator(pParse, pAggInfo);
135999 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
136000 VdbeComment((v, "indicate accumulator empty"));
136001 sqlite3VdbeAddOp1(v, OP_Return, regReset);
136002
136003 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
136004 else {
136005 Table *pTab;
136006 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
136007 /* If isSimpleCount() returns a pointer to a Table structure, then
136008 ** the SQL statement is of the form:
136009 **
136010 ** SELECT count(*) FROM <tbl>
136011 **
136012 ** where the Table structure returned represents table <tbl>.
136013 **
136014 ** This statement is so common that it is optimized specially. The
136015 ** OP_Count instruction is executed either on the intkey table that
136016 ** contains the data for table <tbl> or on one of its indexes. It
136017 ** is better to execute the op on an index, as indexes are almost
136018 ** always spread across less pages than their corresponding tables.
136019 */
136020 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
136021 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
136022 Index *pIdx; /* Iterator variable */
136023 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
136024 Index *pBest = 0; /* Best index found so far */
136025 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
136026
136027 sqlite3CodeVerifySchema(pParse, iDb);
136028 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
136029
136030 /* Search for the index that has the lowest scan cost.
136031 **
136032 ** (2011-04-15) Do not do a full scan of an unordered index.
136033 **
136034 ** (2013-10-03) Do not count the entries in a partial index.
136035 **
136036 ** In practice the KeyInfo structure will not be used. It is only
136037 ** passed to keep OP_OpenRead happy.
136038 */
136039 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
136040 if( !p->pSrc->a[0].fg.notIndexed ){
136041 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136042 if( pIdx->bUnordered==0
136043 && pIdx->szIdxRow<pTab->szTabRow
136044 && pIdx->pPartIdxWhere==0
136045 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
136046 ){
136047 pBest = pIdx;
136048 }
136049 }
136050 }
136051 if( pBest ){
136052 iRoot = pBest->tnum;
136053 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
136054 }
136055
136056 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
136057 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1);
136058 if( pKeyInfo ){
136059 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
136060 }
136061 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
136062 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
136063 explainSimpleCount(pParse, pTab, pBest);
136064 }else{
136065 int regAcc = 0; /* "populate accumulators" flag */
136066 int addrSkip;
136067
136068 /* If there are accumulator registers but no min() or max() functions
136069 ** without FILTER clauses, allocate register regAcc. Register regAcc
136070 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
136071 ** The code generated by updateAccumulator() uses this to ensure
136072 ** that the accumulator registers are (a) updated only once if
136073 ** there are no min() or max functions or (b) always updated for the
136074 ** first row visited by the aggregate, so that they are updated at
136075 ** least once even if the FILTER clause means the min() or max()
136076 ** function visits zero rows. */
136077 if( pAggInfo->nAccumulator ){
136078 for(i=0; i<pAggInfo->nFunc; i++){
136079 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
136080 continue;
136081 }
136082 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
136083 break;
136084 }
136085 }
136086 if( i==pAggInfo->nFunc ){
136087 regAcc = ++pParse->nMem;
136088 sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
136089 }
136090 }
136091
136092 /* This case runs if the aggregate has no GROUP BY clause. The
136093 ** processing is much simpler since there is only a single row
136094 ** of output.
136095 */
136096 assert( p->pGroupBy==0 );
136097 resetAccumulator(pParse, pAggInfo);
136098
136099 /* If this query is a candidate for the min/max optimization, then
136100 ** minMaxFlag will have been previously set to either
136101 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
136102 ** be an appropriate ORDER BY expression for the optimization.
136103 */
136104 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
136105 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
136106
136107 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
136108 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
136109 0, minMaxFlag, 0);
136110 if( pWInfo==0 ){
136111 goto select_end;
136112 }
136113 updateAccumulator(pParse, regAcc, pAggInfo);
136114 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
136115 addrSkip = sqlite3WhereOrderByLimitOptLabel(pWInfo);
136116 if( addrSkip!=sqlite3WhereContinueLabel(pWInfo) ){
136117 sqlite3VdbeGoto(v, addrSkip);
136118 }
136119 sqlite3WhereEnd(pWInfo);
136120 finalizeAggFunctions(pParse, pAggInfo);
136121 }
136122
136123 sSort.pOrderBy = 0;
136124 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
136125 selectInnerLoop(pParse, p, -1, 0, 0,
136126 pDest, addrEnd, addrEnd);
136127 }
136128 sqlite3VdbeResolveLabel(v, addrEnd);
136129
136130 } /* endif aggregate query */
136131
136132 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
136133 explainTempTable(pParse, "DISTINCT");
136134 }
136135
136136 /* If there is an ORDER BY clause, then we need to sort the results
136137 ** and send them to the callback one by one.
136138 */
136139 if( sSort.pOrderBy ){
136140 explainTempTable(pParse,
136141 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
136142 assert( p->pEList==pEList );
136143 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
136144 }
136145
136146 /* Jump here to skip this query
136147 */
136148 sqlite3VdbeResolveLabel(v, iEnd);
136149
136150 /* The SELECT has been coded. If there is an error in the Parse structure,
136151 ** set the return code to 1. Otherwise 0. */
136152 rc = (pParse->nErr>0);
136153
136154 /* Control jumps to here if an error is encountered above, or upon
136155 ** successful coding of the SELECT.
136156 */
136157select_end:
136158 sqlite3ExprListDelete(db, pMinMaxOrderBy);
136159#ifdef SQLITE_DEBUG
136160 if( pAggInfo && !db->mallocFailed ){
136161 for(i=0; i<pAggInfo->nColumn; i++){
136162 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
136163 assert( pExpr!=0 || db->mallocFailed );
136164 if( pExpr==0 ) continue;
136165 assert( pExpr->pAggInfo==pAggInfo );
136166 assert( pExpr->iAgg==i );
136167 }
136168 for(i=0; i<pAggInfo->nFunc; i++){
136169 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
136170 assert( pExpr!=0 || db->mallocFailed );
136171 if( pExpr==0 ) continue;
136172 assert( pExpr->pAggInfo==pAggInfo );
136173 assert( pExpr->iAgg==i );
136174 }
136175 }
136176#endif
136177
136178#if SELECTTRACE_ENABLED
136179 SELECTTRACE(0x1,pParse,p,("end processing\n"));
136180 if( (sqlite3_unsupported_selecttrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
136181 sqlite3TreeViewSelect(0, p, 0);
136182 }
136183#endif
136184 ExplainQueryPlanPop(pParse);
136185 return rc;
136186}
136187
136188/************** End of select.c **********************************************/
136189/************** Begin file table.c *******************************************/
136190/*
136191** 2001 September 15
136192**
136193** The author disclaims copyright to this source code. In place of
136194** a legal notice, here is a blessing:
136195**
136196** May you do good and not evil.
136197** May you find forgiveness for yourself and forgive others.
136198** May you share freely, never taking more than you give.
136199**
136200*************************************************************************
136201** This file contains the sqlite3_get_table() and sqlite3_free_table()
136202** interface routines. These are just wrappers around the main
136203** interface routine of sqlite3_exec().
136204**
136205** These routines are in a separate files so that they will not be linked
136206** if they are not used.
136207*/
136208/* #include "sqliteInt.h" */
136209
136210#ifndef SQLITE_OMIT_GET_TABLE
136211
136212/*
136213** This structure is used to pass data from sqlite3_get_table() through
136214** to the callback function is uses to build the result.
136215*/
136216typedef struct TabResult {
136217 char **azResult; /* Accumulated output */
136218 char *zErrMsg; /* Error message text, if an error occurs */
136219 u32 nAlloc; /* Slots allocated for azResult[] */
136220 u32 nRow; /* Number of rows in the result */
136221 u32 nColumn; /* Number of columns in the result */
136222 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
136223 int rc; /* Return code from sqlite3_exec() */
136224} TabResult;
136225
136226/*
136227** This routine is called once for each row in the result table. Its job
136228** is to fill in the TabResult structure appropriately, allocating new
136229** memory as necessary.
136230*/
136231static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
136232 TabResult *p = (TabResult*)pArg; /* Result accumulator */
136233 int need; /* Slots needed in p->azResult[] */
136234 int i; /* Loop counter */
136235 char *z; /* A single column of result */
136236
136237 /* Make sure there is enough space in p->azResult to hold everything
136238 ** we need to remember from this invocation of the callback.
136239 */
136240 if( p->nRow==0 && argv!=0 ){
136241 need = nCol*2;
136242 }else{
136243 need = nCol;
136244 }
136245 if( p->nData + need > p->nAlloc ){
136246 char **azNew;
136247 p->nAlloc = p->nAlloc*2 + need;
136248 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
136249 if( azNew==0 ) goto malloc_failed;
136250 p->azResult = azNew;
136251 }
136252
136253 /* If this is the first row, then generate an extra row containing
136254 ** the names of all columns.
136255 */
136256 if( p->nRow==0 ){
136257 p->nColumn = nCol;
136258 for(i=0; i<nCol; i++){
136259 z = sqlite3_mprintf("%s", colv[i]);
136260 if( z==0 ) goto malloc_failed;
136261 p->azResult[p->nData++] = z;
136262 }
136263 }else if( (int)p->nColumn!=nCol ){
136264 sqlite3_free(p->zErrMsg);
136265 p->zErrMsg = sqlite3_mprintf(
136266 "sqlite3_get_table() called with two or more incompatible queries"
136267 );
136268 p->rc = SQLITE_ERROR;
136269 return 1;
136270 }
136271
136272 /* Copy over the row data
136273 */
136274 if( argv!=0 ){
136275 for(i=0; i<nCol; i++){
136276 if( argv[i]==0 ){
136277 z = 0;
136278 }else{
136279 int n = sqlite3Strlen30(argv[i])+1;
136280 z = sqlite3_malloc64( n );
136281 if( z==0 ) goto malloc_failed;
136282 memcpy(z, argv[i], n);
136283 }
136284 p->azResult[p->nData++] = z;
136285 }
136286 p->nRow++;
136287 }
136288 return 0;
136289
136290malloc_failed:
136291 p->rc = SQLITE_NOMEM_BKPT;
136292 return 1;
136293}
136294
136295/*
136296** Query the database. But instead of invoking a callback for each row,
136297** malloc() for space to hold the result and return the entire results
136298** at the conclusion of the call.
136299**
136300** The result that is written to ***pazResult is held in memory obtained
136301** from malloc(). But the caller cannot free this memory directly.
136302** Instead, the entire table should be passed to sqlite3_free_table() when
136303** the calling procedure is finished using it.
136304*/
136305SQLITE_API int sqlite3_get_table(
136306 sqlite3 *db, /* The database on which the SQL executes */
136307 const char *zSql, /* The SQL to be executed */
136308 char ***pazResult, /* Write the result table here */
136309 int *pnRow, /* Write the number of rows in the result here */
136310 int *pnColumn, /* Write the number of columns of result here */
136311 char **pzErrMsg /* Write error messages here */
136312){
136313 int rc;
136314 TabResult res;
136315
136316#ifdef SQLITE_ENABLE_API_ARMOR
136317 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
136318#endif
136319 *pazResult = 0;
136320 if( pnColumn ) *pnColumn = 0;
136321 if( pnRow ) *pnRow = 0;
136322 if( pzErrMsg ) *pzErrMsg = 0;
136323 res.zErrMsg = 0;
136324 res.nRow = 0;
136325 res.nColumn = 0;
136326 res.nData = 1;
136327 res.nAlloc = 20;
136328 res.rc = SQLITE_OK;
136329 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
136330 if( res.azResult==0 ){
136331 db->errCode = SQLITE_NOMEM;
136332 return SQLITE_NOMEM_BKPT;
136333 }
136334 res.azResult[0] = 0;
136335 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
136336 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
136337 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
136338 if( (rc&0xff)==SQLITE_ABORT ){
136339 sqlite3_free_table(&res.azResult[1]);
136340 if( res.zErrMsg ){
136341 if( pzErrMsg ){
136342 sqlite3_free(*pzErrMsg);
136343 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
136344 }
136345 sqlite3_free(res.zErrMsg);
136346 }
136347 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
136348 return res.rc;
136349 }
136350 sqlite3_free(res.zErrMsg);
136351 if( rc!=SQLITE_OK ){
136352 sqlite3_free_table(&res.azResult[1]);
136353 return rc;
136354 }
136355 if( res.nAlloc>res.nData ){
136356 char **azNew;
136357 azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
136358 if( azNew==0 ){
136359 sqlite3_free_table(&res.azResult[1]);
136360 db->errCode = SQLITE_NOMEM;
136361 return SQLITE_NOMEM_BKPT;
136362 }
136363 res.azResult = azNew;
136364 }
136365 *pazResult = &res.azResult[1];
136366 if( pnColumn ) *pnColumn = res.nColumn;
136367 if( pnRow ) *pnRow = res.nRow;
136368 return rc;
136369}
136370
136371/*
136372** This routine frees the space the sqlite3_get_table() malloced.
136373*/
136374SQLITE_API void sqlite3_free_table(
136375 char **azResult /* Result returned from sqlite3_get_table() */
136376){
136377 if( azResult ){
136378 int i, n;
136379 azResult--;
136380 assert( azResult!=0 );
136381 n = SQLITE_PTR_TO_INT(azResult[0]);
136382 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
136383 sqlite3_free(azResult);
136384 }
136385}
136386
136387#endif /* SQLITE_OMIT_GET_TABLE */
136388
136389/************** End of table.c ***********************************************/
136390/************** Begin file trigger.c *****************************************/
136391/*
136392**
136393** The author disclaims copyright to this source code. In place of
136394** a legal notice, here is a blessing:
136395**
136396** May you do good and not evil.
136397** May you find forgiveness for yourself and forgive others.
136398** May you share freely, never taking more than you give.
136399**
136400*************************************************************************
136401** This file contains the implementation for TRIGGERs
136402*/
136403/* #include "sqliteInt.h" */
136404
136405#ifndef SQLITE_OMIT_TRIGGER
136406/*
136407** Delete a linked list of TriggerStep structures.
136408*/
136409SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
136410 while( pTriggerStep ){
136411 TriggerStep * pTmp = pTriggerStep;
136412 pTriggerStep = pTriggerStep->pNext;
136413
136414 sqlite3ExprDelete(db, pTmp->pWhere);
136415 sqlite3ExprListDelete(db, pTmp->pExprList);
136416 sqlite3SelectDelete(db, pTmp->pSelect);
136417 sqlite3IdListDelete(db, pTmp->pIdList);
136418 sqlite3UpsertDelete(db, pTmp->pUpsert);
136419 sqlite3SrcListDelete(db, pTmp->pFrom);
136420 sqlite3DbFree(db, pTmp->zSpan);
136421
136422 sqlite3DbFree(db, pTmp);
136423 }
136424}
136425
136426/*
136427** Given table pTab, return a list of all the triggers attached to
136428** the table. The list is connected by Trigger.pNext pointers.
136429**
136430** All of the triggers on pTab that are in the same database as pTab
136431** are already attached to pTab->pTrigger. But there might be additional
136432** triggers on pTab in the TEMP schema. This routine prepends all
136433** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
136434** and returns the combined list.
136435**
136436** To state it another way: This routine returns a list of all triggers
136437** that fire off of pTab. The list will include any TEMP triggers on
136438** pTab as well as the triggers lised in pTab->pTrigger.
136439*/
136440SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
136441 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
136442 Trigger *pList = 0; /* List of triggers to return */
136443
136444 if( pParse->disableTriggers ){
136445 return 0;
136446 }
136447
136448 if( pTmpSchema!=pTab->pSchema ){
136449 HashElem *p;
136450 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
136451 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
136452 Trigger *pTrig = (Trigger *)sqliteHashData(p);
136453 if( pTrig->pTabSchema==pTab->pSchema
136454 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
136455 ){
136456 pTrig->pNext = (pList ? pList : pTab->pTrigger);
136457 pList = pTrig;
136458 }
136459 }
136460 }
136461
136462 return (pList ? pList : pTab->pTrigger);
136463}
136464
136465/*
136466** This is called by the parser when it sees a CREATE TRIGGER statement
136467** up to the point of the BEGIN before the trigger actions. A Trigger
136468** structure is generated based on the information available and stored
136469** in pParse->pNewTrigger. After the trigger actions have been parsed, the
136470** sqlite3FinishTrigger() function is called to complete the trigger
136471** construction process.
136472*/
136473SQLITE_PRIVATE void sqlite3BeginTrigger(
136474 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
136475 Token *pName1, /* The name of the trigger */
136476 Token *pName2, /* The name of the trigger */
136477 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
136478 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
136479 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
136480 SrcList *pTableName,/* The name of the table/view the trigger applies to */
136481 Expr *pWhen, /* WHEN clause */
136482 int isTemp, /* True if the TEMPORARY keyword is present */
136483 int noErr /* Suppress errors if the trigger already exists */
136484){
136485 Trigger *pTrigger = 0; /* The new trigger */
136486 Table *pTab; /* Table that the trigger fires off of */
136487 char *zName = 0; /* Name of the trigger */
136488 sqlite3 *db = pParse->db; /* The database connection */
136489 int iDb; /* The database to store the trigger in */
136490 Token *pName; /* The unqualified db name */
136491 DbFixer sFix; /* State vector for the DB fixer */
136492
136493 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
136494 assert( pName2!=0 );
136495 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
136496 assert( op>0 && op<0xff );
136497 if( isTemp ){
136498 /* If TEMP was specified, then the trigger name may not be qualified. */
136499 if( pName2->n>0 ){
136500 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
136501 goto trigger_cleanup;
136502 }
136503 iDb = 1;
136504 pName = pName1;
136505 }else{
136506 /* Figure out the db that the trigger will be created in */
136507 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
136508 if( iDb<0 ){
136509 goto trigger_cleanup;
136510 }
136511 }
136512 if( !pTableName || db->mallocFailed ){
136513 goto trigger_cleanup;
136514 }
136515
136516 /* A long-standing parser bug is that this syntax was allowed:
136517 **
136518 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
136519 ** ^^^^^^^^
136520 **
136521 ** To maintain backwards compatibility, ignore the database
136522 ** name on pTableName if we are reparsing out of the schema table
136523 */
136524 if( db->init.busy && iDb!=1 ){
136525 sqlite3DbFree(db, pTableName->a[0].zDatabase);
136526 pTableName->a[0].zDatabase = 0;
136527 }
136528
136529 /* If the trigger name was unqualified, and the table is a temp table,
136530 ** then set iDb to 1 to create the trigger in the temporary database.
136531 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
136532 ** exist, the error is caught by the block below.
136533 */
136534 pTab = sqlite3SrcListLookup(pParse, pTableName);
136535 if( db->init.busy==0 && pName2->n==0 && pTab
136536 && pTab->pSchema==db->aDb[1].pSchema ){
136537 iDb = 1;
136538 }
136539
136540 /* Ensure the table name matches database name and that the table exists */
136541 if( db->mallocFailed ) goto trigger_cleanup;
136542 assert( pTableName->nSrc==1 );
136543 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
136544 if( sqlite3FixSrcList(&sFix, pTableName) ){
136545 goto trigger_cleanup;
136546 }
136547 pTab = sqlite3SrcListLookup(pParse, pTableName);
136548 if( !pTab ){
136549 /* The table does not exist. */
136550 if( db->init.iDb==1 ){
136551 /* Ticket #3810.
136552 ** Normally, whenever a table is dropped, all associated triggers are
136553 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
136554 ** and the table is dropped by a different database connection, the
136555 ** trigger is not visible to the database connection that does the
136556 ** drop so the trigger cannot be dropped. This results in an
136557 ** "orphaned trigger" - a trigger whose associated table is missing.
136558 */
136559 db->init.orphanTrigger = 1;
136560 }
136561 goto trigger_cleanup;
136562 }
136563 if( IsVirtual(pTab) ){
136564 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
136565 goto trigger_cleanup;
136566 }
136567
136568 /* Check that the trigger name is not reserved and that no trigger of the
136569 ** specified name exists */
136570 zName = sqlite3NameFromToken(db, pName);
136571 if( zName==0 ){
136572 assert( db->mallocFailed );
136573 goto trigger_cleanup;
136574 }
136575 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
136576 goto trigger_cleanup;
136577 }
136578 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136579 if( !IN_RENAME_OBJECT ){
136580 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
136581 if( !noErr ){
136582 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
136583 }else{
136584 assert( !db->init.busy );
136585 sqlite3CodeVerifySchema(pParse, iDb);
136586 }
136587 goto trigger_cleanup;
136588 }
136589 }
136590
136591 /* Do not create a trigger on a system table */
136592 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
136593 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
136594 goto trigger_cleanup;
136595 }
136596
136597 /* INSTEAD of triggers are only for views and views only support INSTEAD
136598 ** of triggers.
136599 */
136600 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
136601 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
136602 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
136603 goto trigger_cleanup;
136604 }
136605 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
136606 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
136607 " trigger on table: %S", pTableName, 0);
136608 goto trigger_cleanup;
136609 }
136610
136611#ifndef SQLITE_OMIT_AUTHORIZATION
136612 if( !IN_RENAME_OBJECT ){
136613 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136614 int code = SQLITE_CREATE_TRIGGER;
136615 const char *zDb = db->aDb[iTabDb].zDbSName;
136616 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
136617 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
136618 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
136619 goto trigger_cleanup;
136620 }
136621 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
136622 goto trigger_cleanup;
136623 }
136624 }
136625#endif
136626
136627 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
136628 ** cannot appear on views. So we might as well translate every
136629 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
136630 ** elsewhere.
136631 */
136632 if (tr_tm == TK_INSTEAD){
136633 tr_tm = TK_BEFORE;
136634 }
136635
136636 /* Build the Trigger object */
136637 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
136638 if( pTrigger==0 ) goto trigger_cleanup;
136639 pTrigger->zName = zName;
136640 zName = 0;
136641 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
136642 pTrigger->pSchema = db->aDb[iDb].pSchema;
136643 pTrigger->pTabSchema = pTab->pSchema;
136644 pTrigger->op = (u8)op;
136645 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
136646 if( IN_RENAME_OBJECT ){
136647 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
136648 pTrigger->pWhen = pWhen;
136649 pWhen = 0;
136650 }else{
136651 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
136652 }
136653 pTrigger->pColumns = pColumns;
136654 pColumns = 0;
136655 assert( pParse->pNewTrigger==0 );
136656 pParse->pNewTrigger = pTrigger;
136657
136658trigger_cleanup:
136659 sqlite3DbFree(db, zName);
136660 sqlite3SrcListDelete(db, pTableName);
136661 sqlite3IdListDelete(db, pColumns);
136662 sqlite3ExprDelete(db, pWhen);
136663 if( !pParse->pNewTrigger ){
136664 sqlite3DeleteTrigger(db, pTrigger);
136665 }else{
136666 assert( pParse->pNewTrigger==pTrigger );
136667 }
136668}
136669
136670/*
136671** This routine is called after all of the trigger actions have been parsed
136672** in order to complete the process of building the trigger.
136673*/
136674SQLITE_PRIVATE void sqlite3FinishTrigger(
136675 Parse *pParse, /* Parser context */
136676 TriggerStep *pStepList, /* The triggered program */
136677 Token *pAll /* Token that describes the complete CREATE TRIGGER */
136678){
136679 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
136680 char *zName; /* Name of trigger */
136681 sqlite3 *db = pParse->db; /* The database */
136682 DbFixer sFix; /* Fixer object */
136683 int iDb; /* Database containing the trigger */
136684 Token nameToken; /* Trigger name for error reporting */
136685
136686 pParse->pNewTrigger = 0;
136687 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
136688 zName = pTrig->zName;
136689 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
136690 pTrig->step_list = pStepList;
136691 while( pStepList ){
136692 pStepList->pTrig = pTrig;
136693 pStepList = pStepList->pNext;
136694 }
136695 sqlite3TokenInit(&nameToken, pTrig->zName);
136696 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
136697 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
136698 || sqlite3FixExpr(&sFix, pTrig->pWhen)
136699 ){
136700 goto triggerfinish_cleanup;
136701 }
136702
136703#ifndef SQLITE_OMIT_ALTERTABLE
136704 if( IN_RENAME_OBJECT ){
136705 assert( !db->init.busy );
136706 pParse->pNewTrigger = pTrig;
136707 pTrig = 0;
136708 }else
136709#endif
136710
136711 /* if we are not initializing,
136712 ** build the sqlite_schema entry
136713 */
136714 if( !db->init.busy ){
136715 Vdbe *v;
136716 char *z;
136717
136718 /* Make an entry in the sqlite_schema table */
136719 v = sqlite3GetVdbe(pParse);
136720 if( v==0 ) goto triggerfinish_cleanup;
136721 sqlite3BeginWriteOperation(pParse, 0, iDb);
136722 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
136723 testcase( z==0 );
136724 sqlite3NestedParse(pParse,
136725 "INSERT INTO %Q." DFLT_SCHEMA_TABLE
136726 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
136727 db->aDb[iDb].zDbSName, zName,
136728 pTrig->table, z);
136729 sqlite3DbFree(db, z);
136730 sqlite3ChangeCookie(pParse, iDb);
136731 sqlite3VdbeAddParseSchemaOp(v, iDb,
136732 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
136733 }
136734
136735 if( db->init.busy ){
136736 Trigger *pLink = pTrig;
136737 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
136738 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
136739 assert( pLink!=0 );
136740 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
136741 if( pTrig ){
136742 sqlite3OomFault(db);
136743 }else if( pLink->pSchema==pLink->pTabSchema ){
136744 Table *pTab;
136745 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
136746 assert( pTab!=0 );
136747 pLink->pNext = pTab->pTrigger;
136748 pTab->pTrigger = pLink;
136749 }
136750 }
136751
136752triggerfinish_cleanup:
136753 sqlite3DeleteTrigger(db, pTrig);
136754 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
136755 sqlite3DeleteTriggerStep(db, pStepList);
136756}
136757
136758/*
136759** Duplicate a range of text from an SQL statement, then convert all
136760** whitespace characters into ordinary space characters.
136761*/
136762static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
136763 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
136764 int i;
136765 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
136766 return z;
136767}
136768
136769/*
136770** Turn a SELECT statement (that the pSelect parameter points to) into
136771** a trigger step. Return a pointer to a TriggerStep structure.
136772**
136773** The parser calls this routine when it finds a SELECT statement in
136774** body of a TRIGGER.
136775*/
136776SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
136777 sqlite3 *db, /* Database connection */
136778 Select *pSelect, /* The SELECT statement */
136779 const char *zStart, /* Start of SQL text */
136780 const char *zEnd /* End of SQL text */
136781){
136782 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
136783 if( pTriggerStep==0 ) {
136784 sqlite3SelectDelete(db, pSelect);
136785 return 0;
136786 }
136787 pTriggerStep->op = TK_SELECT;
136788 pTriggerStep->pSelect = pSelect;
136789 pTriggerStep->orconf = OE_Default;
136790 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
136791 return pTriggerStep;
136792}
136793
136794/*
136795** Allocate space to hold a new trigger step. The allocated space
136796** holds both the TriggerStep object and the TriggerStep.target.z string.
136797**
136798** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
136799*/
136800static TriggerStep *triggerStepAllocate(
136801 Parse *pParse, /* Parser context */
136802 u8 op, /* Trigger opcode */
136803 Token *pName, /* The target name */
136804 const char *zStart, /* Start of SQL text */
136805 const char *zEnd /* End of SQL text */
136806){
136807 sqlite3 *db = pParse->db;
136808 TriggerStep *pTriggerStep;
136809
136810 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
136811 if( pTriggerStep ){
136812 char *z = (char*)&pTriggerStep[1];
136813 memcpy(z, pName->z, pName->n);
136814 sqlite3Dequote(z);
136815 pTriggerStep->zTarget = z;
136816 pTriggerStep->op = op;
136817 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
136818 if( IN_RENAME_OBJECT ){
136819 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
136820 }
136821 }
136822 return pTriggerStep;
136823}
136824
136825/*
136826** Build a trigger step out of an INSERT statement. Return a pointer
136827** to the new trigger step.
136828**
136829** The parser calls this routine when it sees an INSERT inside the
136830** body of a trigger.
136831*/
136832SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
136833 Parse *pParse, /* Parser */
136834 Token *pTableName, /* Name of the table into which we insert */
136835 IdList *pColumn, /* List of columns in pTableName to insert into */
136836 Select *pSelect, /* A SELECT statement that supplies values */
136837 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
136838 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
136839 const char *zStart, /* Start of SQL text */
136840 const char *zEnd /* End of SQL text */
136841){
136842 sqlite3 *db = pParse->db;
136843 TriggerStep *pTriggerStep;
136844
136845 assert(pSelect != 0 || db->mallocFailed);
136846
136847 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
136848 if( pTriggerStep ){
136849 if( IN_RENAME_OBJECT ){
136850 pTriggerStep->pSelect = pSelect;
136851 pSelect = 0;
136852 }else{
136853 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
136854 }
136855 pTriggerStep->pIdList = pColumn;
136856 pTriggerStep->pUpsert = pUpsert;
136857 pTriggerStep->orconf = orconf;
136858 if( pUpsert ){
136859 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
136860 }
136861 }else{
136862 testcase( pColumn );
136863 sqlite3IdListDelete(db, pColumn);
136864 testcase( pUpsert );
136865 sqlite3UpsertDelete(db, pUpsert);
136866 }
136867 sqlite3SelectDelete(db, pSelect);
136868
136869 return pTriggerStep;
136870}
136871
136872/*
136873** Construct a trigger step that implements an UPDATE statement and return
136874** a pointer to that trigger step. The parser calls this routine when it
136875** sees an UPDATE statement inside the body of a CREATE TRIGGER.
136876*/
136877SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
136878 Parse *pParse, /* Parser */
136879 Token *pTableName, /* Name of the table to be updated */
136880 SrcList *pFrom,
136881 ExprList *pEList, /* The SET clause: list of column and new values */
136882 Expr *pWhere, /* The WHERE clause */
136883 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
136884 const char *zStart, /* Start of SQL text */
136885 const char *zEnd /* End of SQL text */
136886){
136887 sqlite3 *db = pParse->db;
136888 TriggerStep *pTriggerStep;
136889
136890 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
136891 if( pTriggerStep ){
136892 if( IN_RENAME_OBJECT ){
136893 pTriggerStep->pExprList = pEList;
136894 pTriggerStep->pWhere = pWhere;
136895 pTriggerStep->pFrom = pFrom;
136896 pEList = 0;
136897 pWhere = 0;
136898 pFrom = 0;
136899 }else{
136900 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
136901 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
136902 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
136903 }
136904 pTriggerStep->orconf = orconf;
136905 }
136906 sqlite3ExprListDelete(db, pEList);
136907 sqlite3ExprDelete(db, pWhere);
136908 sqlite3SrcListDelete(db, pFrom);
136909 return pTriggerStep;
136910}
136911
136912/*
136913** Construct a trigger step that implements a DELETE statement and return
136914** a pointer to that trigger step. The parser calls this routine when it
136915** sees a DELETE statement inside the body of a CREATE TRIGGER.
136916*/
136917SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
136918 Parse *pParse, /* Parser */
136919 Token *pTableName, /* The table from which rows are deleted */
136920 Expr *pWhere, /* The WHERE clause */
136921 const char *zStart, /* Start of SQL text */
136922 const char *zEnd /* End of SQL text */
136923){
136924 sqlite3 *db = pParse->db;
136925 TriggerStep *pTriggerStep;
136926
136927 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
136928 if( pTriggerStep ){
136929 if( IN_RENAME_OBJECT ){
136930 pTriggerStep->pWhere = pWhere;
136931 pWhere = 0;
136932 }else{
136933 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
136934 }
136935 pTriggerStep->orconf = OE_Default;
136936 }
136937 sqlite3ExprDelete(db, pWhere);
136938 return pTriggerStep;
136939}
136940
136941/*
136942** Recursively delete a Trigger structure
136943*/
136944SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
136945 if( pTrigger==0 ) return;
136946 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
136947 sqlite3DbFree(db, pTrigger->zName);
136948 sqlite3DbFree(db, pTrigger->table);
136949 sqlite3ExprDelete(db, pTrigger->pWhen);
136950 sqlite3IdListDelete(db, pTrigger->pColumns);
136951 sqlite3DbFree(db, pTrigger);
136952}
136953
136954/*
136955** This function is called to drop a trigger from the database schema.
136956**
136957** This may be called directly from the parser and therefore identifies
136958** the trigger by name. The sqlite3DropTriggerPtr() routine does the
136959** same job as this routine except it takes a pointer to the trigger
136960** instead of the trigger name.
136961**/
136962SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
136963 Trigger *pTrigger = 0;
136964 int i;
136965 const char *zDb;
136966 const char *zName;
136967 sqlite3 *db = pParse->db;
136968
136969 if( db->mallocFailed ) goto drop_trigger_cleanup;
136970 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
136971 goto drop_trigger_cleanup;
136972 }
136973
136974 assert( pName->nSrc==1 );
136975 zDb = pName->a[0].zDatabase;
136976 zName = pName->a[0].zName;
136977 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
136978 for(i=OMIT_TEMPDB; i<db->nDb; i++){
136979 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
136980 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
136981 assert( sqlite3SchemaMutexHeld(db, j, 0) );
136982 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
136983 if( pTrigger ) break;
136984 }
136985 if( !pTrigger ){
136986 if( !noErr ){
136987 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
136988 }else{
136989 sqlite3CodeVerifyNamedSchema(pParse, zDb);
136990 }
136991 pParse->checkSchema = 1;
136992 goto drop_trigger_cleanup;
136993 }
136994 sqlite3DropTriggerPtr(pParse, pTrigger);
136995
136996drop_trigger_cleanup:
136997 sqlite3SrcListDelete(db, pName);
136998}
136999
137000/*
137001** Return a pointer to the Table structure for the table that a trigger
137002** is set on.
137003*/
137004static Table *tableOfTrigger(Trigger *pTrigger){
137005 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
137006}
137007
137008
137009/*
137010** Drop a trigger given a pointer to that trigger.
137011*/
137012SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
137013 Table *pTable;
137014 Vdbe *v;
137015 sqlite3 *db = pParse->db;
137016 int iDb;
137017
137018 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
137019 assert( iDb>=0 && iDb<db->nDb );
137020 pTable = tableOfTrigger(pTrigger);
137021 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
137022#ifndef SQLITE_OMIT_AUTHORIZATION
137023 if( pTable ){
137024 int code = SQLITE_DROP_TRIGGER;
137025 const char *zDb = db->aDb[iDb].zDbSName;
137026 const char *zTab = SCHEMA_TABLE(iDb);
137027 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
137028 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
137029 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
137030 return;
137031 }
137032 }
137033#endif
137034
137035 /* Generate code to destroy the database record of the trigger.
137036 */
137037 if( (v = sqlite3GetVdbe(pParse))!=0 ){
137038 sqlite3NestedParse(pParse,
137039 "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'",
137040 db->aDb[iDb].zDbSName, pTrigger->zName
137041 );
137042 sqlite3ChangeCookie(pParse, iDb);
137043 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
137044 }
137045}
137046
137047/*
137048** Remove a trigger from the hash tables of the sqlite* pointer.
137049*/
137050SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
137051 Trigger *pTrigger;
137052 Hash *pHash;
137053
137054 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
137055 pHash = &(db->aDb[iDb].pSchema->trigHash);
137056 pTrigger = sqlite3HashInsert(pHash, zName, 0);
137057 if( ALWAYS(pTrigger) ){
137058 if( pTrigger->pSchema==pTrigger->pTabSchema ){
137059 Table *pTab = tableOfTrigger(pTrigger);
137060 if( pTab ){
137061 Trigger **pp;
137062 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
137063 if( *pp==pTrigger ){
137064 *pp = (*pp)->pNext;
137065 break;
137066 }
137067 }
137068 }
137069 }
137070 sqlite3DeleteTrigger(db, pTrigger);
137071 db->mDbFlags |= DBFLAG_SchemaChange;
137072 }
137073}
137074
137075/*
137076** pEList is the SET clause of an UPDATE statement. Each entry
137077** in pEList is of the format <id>=<expr>. If any of the entries
137078** in pEList have an <id> which matches an identifier in pIdList,
137079** then return TRUE. If pIdList==NULL, then it is considered a
137080** wildcard that matches anything. Likewise if pEList==NULL then
137081** it matches anything so always return true. Return false only
137082** if there is no match.
137083*/
137084static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
137085 int e;
137086 if( pIdList==0 || NEVER(pEList==0) ) return 1;
137087 for(e=0; e<pEList->nExpr; e++){
137088 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
137089 }
137090 return 0;
137091}
137092
137093/*
137094** Return a list of all triggers on table pTab if there exists at least
137095** one trigger that must be fired when an operation of type 'op' is
137096** performed on the table, and, if that operation is an UPDATE, if at
137097** least one of the columns in pChanges is being modified.
137098*/
137099SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
137100 Parse *pParse, /* Parse context */
137101 Table *pTab, /* The table the contains the triggers */
137102 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
137103 ExprList *pChanges, /* Columns that change in an UPDATE statement */
137104 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
137105){
137106 int mask = 0;
137107 Trigger *pList = 0;
137108 Trigger *p;
137109
137110 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
137111 pList = sqlite3TriggerList(pParse, pTab);
137112 }
137113 assert( pList==0 || IsVirtual(pTab)==0 );
137114 for(p=pList; p; p=p->pNext){
137115 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
137116 mask |= p->tr_tm;
137117 }
137118 }
137119 if( pMask ){
137120 *pMask = mask;
137121 }
137122 return (mask ? pList : 0);
137123}
137124
137125/*
137126** Convert the pStep->zTarget string into a SrcList and return a pointer
137127** to that SrcList.
137128**
137129** This routine adds a specific database name, if needed, to the target when
137130** forming the SrcList. This prevents a trigger in one database from
137131** referring to a target in another database. An exception is when the
137132** trigger is in TEMP in which case it can refer to any other database it
137133** wants.
137134*/
137135SQLITE_PRIVATE SrcList *sqlite3TriggerStepSrc(
137136 Parse *pParse, /* The parsing context */
137137 TriggerStep *pStep /* The trigger containing the target token */
137138){
137139 sqlite3 *db = pParse->db;
137140 SrcList *pSrc; /* SrcList to be returned */
137141 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
137142 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
137143 assert( pSrc==0 || pSrc->nSrc==1 );
137144 assert( zName || pSrc==0 );
137145 if( pSrc ){
137146 Schema *pSchema = pStep->pTrig->pSchema;
137147 pSrc->a[0].zName = zName;
137148 if( pSchema!=db->aDb[1].pSchema ){
137149 pSrc->a[0].pSchema = pSchema;
137150 }
137151 if( pStep->pFrom ){
137152 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
137153 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
137154 }
137155 }else{
137156 sqlite3DbFree(db, zName);
137157 }
137158 return pSrc;
137159}
137160
137161/*
137162** Generate VDBE code for the statements inside the body of a single
137163** trigger.
137164*/
137165static int codeTriggerProgram(
137166 Parse *pParse, /* The parser context */
137167 TriggerStep *pStepList, /* List of statements inside the trigger body */
137168 int orconf /* Conflict algorithm. (OE_Abort, etc) */
137169){
137170 TriggerStep *pStep;
137171 Vdbe *v = pParse->pVdbe;
137172 sqlite3 *db = pParse->db;
137173
137174 assert( pParse->pTriggerTab && pParse->pToplevel );
137175 assert( pStepList );
137176 assert( v!=0 );
137177 for(pStep=pStepList; pStep; pStep=pStep->pNext){
137178 /* Figure out the ON CONFLICT policy that will be used for this step
137179 ** of the trigger program. If the statement that caused this trigger
137180 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
137181 ** the ON CONFLICT policy that was specified as part of the trigger
137182 ** step statement. Example:
137183 **
137184 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
137185 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
137186 ** END;
137187 **
137188 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
137189 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
137190 */
137191 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
137192 assert( pParse->okConstFactor==0 );
137193
137194#ifndef SQLITE_OMIT_TRACE
137195 if( pStep->zSpan ){
137196 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
137197 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
137198 P4_DYNAMIC);
137199 }
137200#endif
137201
137202 switch( pStep->op ){
137203 case TK_UPDATE: {
137204 sqlite3Update(pParse,
137205 sqlite3TriggerStepSrc(pParse, pStep),
137206 sqlite3ExprListDup(db, pStep->pExprList, 0),
137207 sqlite3ExprDup(db, pStep->pWhere, 0),
137208 pParse->eOrconf, 0, 0, 0
137209 );
137210 break;
137211 }
137212 case TK_INSERT: {
137213 sqlite3Insert(pParse,
137214 sqlite3TriggerStepSrc(pParse, pStep),
137215 sqlite3SelectDup(db, pStep->pSelect, 0),
137216 sqlite3IdListDup(db, pStep->pIdList),
137217 pParse->eOrconf,
137218 sqlite3UpsertDup(db, pStep->pUpsert)
137219 );
137220 break;
137221 }
137222 case TK_DELETE: {
137223 sqlite3DeleteFrom(pParse,
137224 sqlite3TriggerStepSrc(pParse, pStep),
137225 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
137226 );
137227 break;
137228 }
137229 default: assert( pStep->op==TK_SELECT ); {
137230 SelectDest sDest;
137231 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
137232 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
137233 sqlite3Select(pParse, pSelect, &sDest);
137234 sqlite3SelectDelete(db, pSelect);
137235 break;
137236 }
137237 }
137238 if( pStep->op!=TK_SELECT ){
137239 sqlite3VdbeAddOp0(v, OP_ResetCount);
137240 }
137241 }
137242
137243 return 0;
137244}
137245
137246#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
137247/*
137248** This function is used to add VdbeComment() annotations to a VDBE
137249** program. It is not used in production code, only for debugging.
137250*/
137251static const char *onErrorText(int onError){
137252 switch( onError ){
137253 case OE_Abort: return "abort";
137254 case OE_Rollback: return "rollback";
137255 case OE_Fail: return "fail";
137256 case OE_Replace: return "replace";
137257 case OE_Ignore: return "ignore";
137258 case OE_Default: return "default";
137259 }
137260 return "n/a";
137261}
137262#endif
137263
137264/*
137265** Parse context structure pFrom has just been used to create a sub-vdbe
137266** (trigger program). If an error has occurred, transfer error information
137267** from pFrom to pTo.
137268*/
137269static void transferParseError(Parse *pTo, Parse *pFrom){
137270 assert( pFrom->zErrMsg==0 || pFrom->nErr );
137271 assert( pTo->zErrMsg==0 || pTo->nErr );
137272 if( pTo->nErr==0 ){
137273 pTo->zErrMsg = pFrom->zErrMsg;
137274 pTo->nErr = pFrom->nErr;
137275 pTo->rc = pFrom->rc;
137276 }else{
137277 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
137278 }
137279}
137280
137281/*
137282** Create and populate a new TriggerPrg object with a sub-program
137283** implementing trigger pTrigger with ON CONFLICT policy orconf.
137284*/
137285static TriggerPrg *codeRowTrigger(
137286 Parse *pParse, /* Current parse context */
137287 Trigger *pTrigger, /* Trigger to code */
137288 Table *pTab, /* The table pTrigger is attached to */
137289 int orconf /* ON CONFLICT policy to code trigger program with */
137290){
137291 Parse *pTop = sqlite3ParseToplevel(pParse);
137292 sqlite3 *db = pParse->db; /* Database handle */
137293 TriggerPrg *pPrg; /* Value to return */
137294 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
137295 Vdbe *v; /* Temporary VM */
137296 NameContext sNC; /* Name context for sub-vdbe */
137297 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
137298 Parse *pSubParse; /* Parse context for sub-vdbe */
137299 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
137300
137301 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
137302 assert( pTop->pVdbe );
137303
137304 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
137305 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
137306 ** list of the top-level Parse object sooner rather than later. */
137307 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
137308 if( !pPrg ) return 0;
137309 pPrg->pNext = pTop->pTriggerPrg;
137310 pTop->pTriggerPrg = pPrg;
137311 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
137312 if( !pProgram ) return 0;
137313 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
137314 pPrg->pTrigger = pTrigger;
137315 pPrg->orconf = orconf;
137316 pPrg->aColmask[0] = 0xffffffff;
137317 pPrg->aColmask[1] = 0xffffffff;
137318
137319 /* Allocate and populate a new Parse context to use for coding the
137320 ** trigger sub-program. */
137321 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
137322 if( !pSubParse ) return 0;
137323 memset(&sNC, 0, sizeof(sNC));
137324 sNC.pParse = pSubParse;
137325 pSubParse->db = db;
137326 pSubParse->pTriggerTab = pTab;
137327 pSubParse->pToplevel = pTop;
137328 pSubParse->zAuthContext = pTrigger->zName;
137329 pSubParse->eTriggerOp = pTrigger->op;
137330 pSubParse->nQueryLoop = pParse->nQueryLoop;
137331 pSubParse->disableVtab = pParse->disableVtab;
137332
137333 v = sqlite3GetVdbe(pSubParse);
137334 if( v ){
137335 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
137336 pTrigger->zName, onErrorText(orconf),
137337 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
137338 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
137339 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
137340 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
137341 pTab->zName
137342 ));
137343#ifndef SQLITE_OMIT_TRACE
137344 if( pTrigger->zName ){
137345 sqlite3VdbeChangeP4(v, -1,
137346 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
137347 );
137348 }
137349#endif
137350
137351 /* If one was specified, code the WHEN clause. If it evaluates to false
137352 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
137353 ** OP_Halt inserted at the end of the program. */
137354 if( pTrigger->pWhen ){
137355 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
137356 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
137357 && db->mallocFailed==0
137358 ){
137359 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
137360 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
137361 }
137362 sqlite3ExprDelete(db, pWhen);
137363 }
137364
137365 /* Code the trigger program into the sub-vdbe. */
137366 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
137367
137368 /* Insert an OP_Halt at the end of the sub-program. */
137369 if( iEndTrigger ){
137370 sqlite3VdbeResolveLabel(v, iEndTrigger);
137371 }
137372 sqlite3VdbeAddOp0(v, OP_Halt);
137373 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
137374
137375 transferParseError(pParse, pSubParse);
137376 if( db->mallocFailed==0 && pParse->nErr==0 ){
137377 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
137378 }
137379 pProgram->nMem = pSubParse->nMem;
137380 pProgram->nCsr = pSubParse->nTab;
137381 pProgram->token = (void *)pTrigger;
137382 pPrg->aColmask[0] = pSubParse->oldmask;
137383 pPrg->aColmask[1] = pSubParse->newmask;
137384 sqlite3VdbeDelete(v);
137385 }
137386
137387 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
137388 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
137389 sqlite3ParserReset(pSubParse);
137390 sqlite3StackFree(db, pSubParse);
137391
137392 return pPrg;
137393}
137394
137395/*
137396** Return a pointer to a TriggerPrg object containing the sub-program for
137397** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
137398** TriggerPrg object exists, a new object is allocated and populated before
137399** being returned.
137400*/
137401static TriggerPrg *getRowTrigger(
137402 Parse *pParse, /* Current parse context */
137403 Trigger *pTrigger, /* Trigger to code */
137404 Table *pTab, /* The table trigger pTrigger is attached to */
137405 int orconf /* ON CONFLICT algorithm. */
137406){
137407 Parse *pRoot = sqlite3ParseToplevel(pParse);
137408 TriggerPrg *pPrg;
137409
137410 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
137411
137412 /* It may be that this trigger has already been coded (or is in the
137413 ** process of being coded). If this is the case, then an entry with
137414 ** a matching TriggerPrg.pTrigger field will be present somewhere
137415 ** in the Parse.pTriggerPrg list. Search for such an entry. */
137416 for(pPrg=pRoot->pTriggerPrg;
137417 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
137418 pPrg=pPrg->pNext
137419 );
137420
137421 /* If an existing TriggerPrg could not be located, create a new one. */
137422 if( !pPrg ){
137423 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
137424 }
137425
137426 return pPrg;
137427}
137428
137429/*
137430** Generate code for the trigger program associated with trigger p on
137431** table pTab. The reg, orconf and ignoreJump parameters passed to this
137432** function are the same as those described in the header function for
137433** sqlite3CodeRowTrigger()
137434*/
137435SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
137436 Parse *pParse, /* Parse context */
137437 Trigger *p, /* Trigger to code */
137438 Table *pTab, /* The table to code triggers from */
137439 int reg, /* Reg array containing OLD.* and NEW.* values */
137440 int orconf, /* ON CONFLICT policy */
137441 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
137442){
137443 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
137444 TriggerPrg *pPrg;
137445 pPrg = getRowTrigger(pParse, p, pTab, orconf);
137446 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
137447
137448 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
137449 ** is a pointer to the sub-vdbe containing the trigger program. */
137450 if( pPrg ){
137451 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
137452
137453 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
137454 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
137455 VdbeComment(
137456 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
137457
137458 /* Set the P5 operand of the OP_Program instruction to non-zero if
137459 ** recursive invocation of this trigger program is disallowed. Recursive
137460 ** invocation is disallowed if (a) the sub-program is really a trigger,
137461 ** not a foreign key action, and (b) the flag to enable recursive triggers
137462 ** is clear. */
137463 sqlite3VdbeChangeP5(v, (u8)bRecursive);
137464 }
137465}
137466
137467/*
137468** This is called to code the required FOR EACH ROW triggers for an operation
137469** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
137470** is given by the op parameter. The tr_tm parameter determines whether the
137471** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
137472** parameter pChanges is passed the list of columns being modified.
137473**
137474** If there are no triggers that fire at the specified time for the specified
137475** operation on pTab, this function is a no-op.
137476**
137477** The reg argument is the address of the first in an array of registers
137478** that contain the values substituted for the new.* and old.* references
137479** in the trigger program. If N is the number of columns in table pTab
137480** (a copy of pTab->nCol), then registers are populated as follows:
137481**
137482** Register Contains
137483** ------------------------------------------------------
137484** reg+0 OLD.rowid
137485** reg+1 OLD.* value of left-most column of pTab
137486** ... ...
137487** reg+N OLD.* value of right-most column of pTab
137488** reg+N+1 NEW.rowid
137489** reg+N+2 OLD.* value of left-most column of pTab
137490** ... ...
137491** reg+N+N+1 NEW.* value of right-most column of pTab
137492**
137493** For ON DELETE triggers, the registers containing the NEW.* values will
137494** never be accessed by the trigger program, so they are not allocated or
137495** populated by the caller (there is no data to populate them with anyway).
137496** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
137497** are never accessed, and so are not allocated by the caller. So, for an
137498** ON INSERT trigger, the value passed to this function as parameter reg
137499** is not a readable register, although registers (reg+N) through
137500** (reg+N+N+1) are.
137501**
137502** Parameter orconf is the default conflict resolution algorithm for the
137503** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
137504** is the instruction that control should jump to if a trigger program
137505** raises an IGNORE exception.
137506*/
137507SQLITE_PRIVATE void sqlite3CodeRowTrigger(
137508 Parse *pParse, /* Parse context */
137509 Trigger *pTrigger, /* List of triggers on table pTab */
137510 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
137511 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
137512 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
137513 Table *pTab, /* The table to code triggers from */
137514 int reg, /* The first in an array of registers (see above) */
137515 int orconf, /* ON CONFLICT policy */
137516 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
137517){
137518 Trigger *p; /* Used to iterate through pTrigger list */
137519
137520 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
137521 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
137522 assert( (op==TK_UPDATE)==(pChanges!=0) );
137523
137524 for(p=pTrigger; p; p=p->pNext){
137525
137526 /* Sanity checking: The schema for the trigger and for the table are
137527 ** always defined. The trigger must be in the same schema as the table
137528 ** or else it must be a TEMP trigger. */
137529 assert( p->pSchema!=0 );
137530 assert( p->pTabSchema!=0 );
137531 assert( p->pSchema==p->pTabSchema
137532 || p->pSchema==pParse->db->aDb[1].pSchema );
137533
137534 /* Determine whether we should code this trigger */
137535 if( p->op==op
137536 && p->tr_tm==tr_tm
137537 && checkColumnOverlap(p->pColumns, pChanges)
137538 ){
137539 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
137540 }
137541 }
137542}
137543
137544/*
137545** Triggers may access values stored in the old.* or new.* pseudo-table.
137546** This function returns a 32-bit bitmask indicating which columns of the
137547** old.* or new.* tables actually are used by triggers. This information
137548** may be used by the caller, for example, to avoid having to load the entire
137549** old.* record into memory when executing an UPDATE or DELETE command.
137550**
137551** Bit 0 of the returned mask is set if the left-most column of the
137552** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
137553** the second leftmost column value is required, and so on. If there
137554** are more than 32 columns in the table, and at least one of the columns
137555** with an index greater than 32 may be accessed, 0xffffffff is returned.
137556**
137557** It is not possible to determine if the old.rowid or new.rowid column is
137558** accessed by triggers. The caller must always assume that it is.
137559**
137560** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
137561** applies to the old.* table. If 1, the new.* table.
137562**
137563** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
137564** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
137565** included in the returned mask if the TRIGGER_BEFORE bit is set in the
137566** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
137567** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
137568*/
137569SQLITE_PRIVATE u32 sqlite3TriggerColmask(
137570 Parse *pParse, /* Parse context */
137571 Trigger *pTrigger, /* List of triggers on table pTab */
137572 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
137573 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
137574 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
137575 Table *pTab, /* The table to code triggers from */
137576 int orconf /* Default ON CONFLICT policy for trigger steps */
137577){
137578 const int op = pChanges ? TK_UPDATE : TK_DELETE;
137579 u32 mask = 0;
137580 Trigger *p;
137581
137582 assert( isNew==1 || isNew==0 );
137583 for(p=pTrigger; p; p=p->pNext){
137584 if( p->op==op && (tr_tm&p->tr_tm)
137585 && checkColumnOverlap(p->pColumns,pChanges)
137586 ){
137587 TriggerPrg *pPrg;
137588 pPrg = getRowTrigger(pParse, p, pTab, orconf);
137589 if( pPrg ){
137590 mask |= pPrg->aColmask[isNew];
137591 }
137592 }
137593 }
137594
137595 return mask;
137596}
137597
137598#endif /* !defined(SQLITE_OMIT_TRIGGER) */
137599
137600/************** End of trigger.c *********************************************/
137601/************** Begin file update.c ******************************************/
137602/*
137603** 2001 September 15
137604**
137605** The author disclaims copyright to this source code. In place of
137606** a legal notice, here is a blessing:
137607**
137608** May you do good and not evil.
137609** May you find forgiveness for yourself and forgive others.
137610** May you share freely, never taking more than you give.
137611**
137612*************************************************************************
137613** This file contains C code routines that are called by the parser
137614** to handle UPDATE statements.
137615*/
137616/* #include "sqliteInt.h" */
137617
137618#ifndef SQLITE_OMIT_VIRTUALTABLE
137619/* Forward declaration */
137620static void updateVirtualTable(
137621 Parse *pParse, /* The parsing context */
137622 SrcList *pSrc, /* The virtual table to be modified */
137623 Table *pTab, /* The virtual table */
137624 ExprList *pChanges, /* The columns to change in the UPDATE statement */
137625 Expr *pRowidExpr, /* Expression used to recompute the rowid */
137626 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
137627 Expr *pWhere, /* WHERE clause of the UPDATE statement */
137628 int onError /* ON CONFLICT strategy */
137629);
137630#endif /* SQLITE_OMIT_VIRTUALTABLE */
137631
137632/*
137633** The most recently coded instruction was an OP_Column to retrieve the
137634** i-th column of table pTab. This routine sets the P4 parameter of the
137635** OP_Column to the default value, if any.
137636**
137637** The default value of a column is specified by a DEFAULT clause in the
137638** column definition. This was either supplied by the user when the table
137639** was created, or added later to the table definition by an ALTER TABLE
137640** command. If the latter, then the row-records in the table btree on disk
137641** may not contain a value for the column and the default value, taken
137642** from the P4 parameter of the OP_Column instruction, is returned instead.
137643** If the former, then all row-records are guaranteed to include a value
137644** for the column and the P4 value is not required.
137645**
137646** Column definitions created by an ALTER TABLE command may only have
137647** literal default values specified: a number, null or a string. (If a more
137648** complicated default expression value was provided, it is evaluated
137649** when the ALTER TABLE is executed and one of the literal values written
137650** into the sqlite_schema table.)
137651**
137652** Therefore, the P4 parameter is only required if the default value for
137653** the column is a literal number, string or null. The sqlite3ValueFromExpr()
137654** function is capable of transforming these types of expressions into
137655** sqlite3_value objects.
137656**
137657** If column as REAL affinity and the table is an ordinary b-tree table
137658** (not a virtual table) then the value might have been stored as an
137659** integer. In that case, add an OP_RealAffinity opcode to make sure
137660** it has been converted into REAL.
137661*/
137662SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
137663 assert( pTab!=0 );
137664 if( !pTab->pSelect ){
137665 sqlite3_value *pValue = 0;
137666 u8 enc = ENC(sqlite3VdbeDb(v));
137667 Column *pCol = &pTab->aCol[i];
137668 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
137669 assert( i<pTab->nCol );
137670 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
137671 pCol->affinity, &pValue);
137672 if( pValue ){
137673 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
137674 }
137675 }
137676#ifndef SQLITE_OMIT_FLOATING_POINT
137677 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
137678 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
137679 }
137680#endif
137681}
137682
137683/*
137684** Check to see if column iCol of index pIdx references any of the
137685** columns defined by aXRef and chngRowid. Return true if it does
137686** and false if not. This is an optimization. False-positives are a
137687** performance degradation, but false-negatives can result in a corrupt
137688** index and incorrect answers.
137689**
137690** aXRef[j] will be non-negative if column j of the original table is
137691** being updated. chngRowid will be true if the rowid of the table is
137692** being updated.
137693*/
137694static int indexColumnIsBeingUpdated(
137695 Index *pIdx, /* The index to check */
137696 int iCol, /* Which column of the index to check */
137697 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
137698 int chngRowid /* true if the rowid is being updated */
137699){
137700 i16 iIdxCol = pIdx->aiColumn[iCol];
137701 assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
137702 if( iIdxCol>=0 ){
137703 return aXRef[iIdxCol]>=0;
137704 }
137705 assert( iIdxCol==XN_EXPR );
137706 assert( pIdx->aColExpr!=0 );
137707 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
137708 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
137709 aXRef,chngRowid);
137710}
137711
137712/*
137713** Check to see if index pIdx is a partial index whose conditional
137714** expression might change values due to an UPDATE. Return true if
137715** the index is subject to change and false if the index is guaranteed
137716** to be unchanged. This is an optimization. False-positives are a
137717** performance degradation, but false-negatives can result in a corrupt
137718** index and incorrect answers.
137719**
137720** aXRef[j] will be non-negative if column j of the original table is
137721** being updated. chngRowid will be true if the rowid of the table is
137722** being updated.
137723*/
137724static int indexWhereClauseMightChange(
137725 Index *pIdx, /* The index to check */
137726 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
137727 int chngRowid /* true if the rowid is being updated */
137728){
137729 if( pIdx->pPartIdxWhere==0 ) return 0;
137730 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
137731 aXRef, chngRowid);
137732}
137733
137734/*
137735** Allocate and return a pointer to an expression of type TK_ROW with
137736** Expr.iColumn set to value (iCol+1). The resolver will modify the
137737** expression to be a TK_COLUMN reading column iCol of the first
137738** table in the source-list (pSrc->a[0]).
137739*/
137740static Expr *exprRowColumn(Parse *pParse, int iCol){
137741 Expr *pRet = sqlite3PExpr(pParse, TK_ROW, 0, 0);
137742 if( pRet ) pRet->iColumn = iCol+1;
137743 return pRet;
137744}
137745
137746/*
137747** Assuming both the pLimit and pOrderBy parameters are NULL, this function
137748** generates VM code to run the query:
137749**
137750** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
137751**
137752** and write the results to the ephemeral table already opened as cursor
137753** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
137754** this function, they must be deleted by the caller.
137755**
137756** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
137757**
137758** SELECT <other-columns>, pChanges FROM pTabList
137759** WHERE pWhere
137760** GROUP BY <other-columns>
137761** ORDER BY pOrderBy LIMIT pLimit
137762**
137763** If pTab is a view, the GROUP BY clause is omitted.
137764**
137765** Exactly how results are written to table iEph, and exactly what
137766** the <other-columns> in the query above are is determined by the type
137767** of table pTabList->a[0].pTab.
137768**
137769** If the table is a WITHOUT ROWID table, then argument pPk must be its
137770** PRIMARY KEY. In this case <other-columns> are the primary key columns
137771** of the table, in order. The results of the query are written to ephemeral
137772** table iEph as index keys, using OP_IdxInsert.
137773**
137774** If the table is actually a view, then <other-columns> are all columns of
137775** the view. The results are written to the ephemeral table iEph as records
137776** with automatically assigned integer keys.
137777**
137778** If the table is a virtual or ordinary intkey table, then <other-columns>
137779** is its rowid. For a virtual table, the results are written to iEph as
137780** records with automatically assigned integer keys For intkey tables, the
137781** rowid value in <other-columns> is used as the integer key, and the
137782** remaining fields make up the table record.
137783*/
137784static void updateFromSelect(
137785 Parse *pParse, /* Parse context */
137786 int iEph, /* Cursor for open eph. table */
137787 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
137788 ExprList *pChanges, /* List of expressions to return */
137789 SrcList *pTabList, /* List of tables to select from */
137790 Expr *pWhere, /* WHERE clause for query */
137791 ExprList *pOrderBy, /* ORDER BY clause */
137792 Expr *pLimit /* LIMIT clause */
137793){
137794 int i;
137795 SelectDest dest;
137796 Select *pSelect = 0;
137797 ExprList *pList = 0;
137798 ExprList *pGrp = 0;
137799 Expr *pLimit2 = 0;
137800 ExprList *pOrderBy2 = 0;
137801 sqlite3 *db = pParse->db;
137802 Table *pTab = pTabList->a[0].pTab;
137803 SrcList *pSrc;
137804 Expr *pWhere2;
137805 int eDest;
137806
137807#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
137808 if( pOrderBy && pLimit==0 ) {
137809 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
137810 return;
137811 }
137812 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
137813 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
137814#else
137815 UNUSED_PARAMETER(pOrderBy);
137816 UNUSED_PARAMETER(pLimit);
137817#endif
137818
137819 pSrc = sqlite3SrcListDup(db, pTabList, 0);
137820 pWhere2 = sqlite3ExprDup(db, pWhere, 0);
137821
137822 assert( pTabList->nSrc>1 );
137823 if( pSrc ){
137824 pSrc->a[0].iCursor = -1;
137825 pSrc->a[0].pTab->nTabRef--;
137826 pSrc->a[0].pTab = 0;
137827 }
137828 if( pPk ){
137829 for(i=0; i<pPk->nKeyCol; i++){
137830 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
137831#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
137832 if( pLimit ){
137833 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
137834 }
137835#endif
137836 pList = sqlite3ExprListAppend(pParse, pList, pNew);
137837 }
137838 eDest = SRT_Upfrom;
137839 }else if( pTab->pSelect ){
137840 for(i=0; i<pTab->nCol; i++){
137841 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
137842 }
137843 eDest = SRT_Table;
137844 }else{
137845 eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom;
137846 pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));
137847#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
137848 if( pLimit ){
137849 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0));
137850 }
137851#endif
137852 }
137853 if( ALWAYS(pChanges) ){
137854 for(i=0; i<pChanges->nExpr; i++){
137855 pList = sqlite3ExprListAppend(pParse, pList,
137856 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
137857 );
137858 }
137859 }
137860 pSelect = sqlite3SelectNew(pParse, pList,
137861 pSrc, pWhere2, pGrp, 0, pOrderBy2, SF_UpdateFrom|SF_IncludeHidden, pLimit2
137862 );
137863 sqlite3SelectDestInit(&dest, eDest, iEph);
137864 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
137865 sqlite3Select(pParse, pSelect, &dest);
137866 sqlite3SelectDelete(db, pSelect);
137867}
137868
137869/*
137870** Process an UPDATE statement.
137871**
137872** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
137873** \_______/ \_/ \______/ \_____/ \________________/
137874** onError | pChanges | pWhere
137875** \_______________________/
137876** pTabList
137877*/
137878SQLITE_PRIVATE void sqlite3Update(
137879 Parse *pParse, /* The parser context */
137880 SrcList *pTabList, /* The table in which we should change things */
137881 ExprList *pChanges, /* Things to be changed */
137882 Expr *pWhere, /* The WHERE clause. May be null */
137883 int onError, /* How to handle constraint errors */
137884 ExprList *pOrderBy, /* ORDER BY clause. May be null */
137885 Expr *pLimit, /* LIMIT clause. May be null */
137886 Upsert *pUpsert /* ON CONFLICT clause, or null */
137887){
137888 int i, j, k; /* Loop counters */
137889 Table *pTab; /* The table to be updated */
137890 int addrTop = 0; /* VDBE instruction address of the start of the loop */
137891 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
137892 Vdbe *v; /* The virtual database engine */
137893 Index *pIdx; /* For looping over indices */
137894 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
137895 int nIdx; /* Number of indices that need updating */
137896 int nAllIdx; /* Total number of indexes */
137897 int iBaseCur; /* Base cursor number */
137898 int iDataCur; /* Cursor for the canonical data btree */
137899 int iIdxCur; /* Cursor for the first index */
137900 sqlite3 *db; /* The database structure */
137901 int *aRegIdx = 0; /* Registers for to each index and the main table */
137902 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
137903 ** an expression for the i-th column of the table.
137904 ** aXRef[i]==-1 if the i-th column is not changed. */
137905 u8 *aToOpen; /* 1 for tables and indices to be opened */
137906 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
137907 u8 chngRowid; /* Rowid changed in a normal table */
137908 u8 chngKey; /* Either chngPk or chngRowid */
137909 Expr *pRowidExpr = 0; /* Expression defining the new record number */
137910 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
137911 AuthContext sContext; /* The authorization context */
137912 NameContext sNC; /* The name-context to resolve expressions in */
137913 int iDb; /* Database containing the table being updated */
137914 int eOnePass; /* ONEPASS_XXX value from where.c */
137915 int hasFK; /* True if foreign key processing is required */
137916 int labelBreak; /* Jump here to break out of UPDATE loop */
137917 int labelContinue; /* Jump here to continue next step of UPDATE loop */
137918 int flags; /* Flags for sqlite3WhereBegin() */
137919
137920#ifndef SQLITE_OMIT_TRIGGER
137921 int isView; /* True when updating a view (INSTEAD OF trigger) */
137922 Trigger *pTrigger; /* List of triggers on pTab, if required */
137923 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
137924#endif
137925 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
137926 int iEph = 0; /* Ephemeral table holding all primary key values */
137927 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
137928 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
137929 int addrOpen = 0; /* Address of OP_OpenEphemeral */
137930 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
137931 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
137932 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
137933 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
137934 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
137935
137936 /* Register Allocations */
137937 int regRowCount = 0; /* A count of rows changed */
137938 int regOldRowid = 0; /* The old rowid */
137939 int regNewRowid = 0; /* The new rowid */
137940 int regNew = 0; /* Content of the NEW.* table in triggers */
137941 int regOld = 0; /* Content of OLD.* table in triggers */
137942 int regRowSet = 0; /* Rowset of rows to be updated */
137943 int regKey = 0; /* composite PRIMARY KEY value */
137944
137945 memset(&sContext, 0, sizeof(sContext));
137946 db = pParse->db;
137947 if( pParse->nErr || db->mallocFailed ){
137948 goto update_cleanup;
137949 }
137950
137951 /* Locate the table which we want to update.
137952 */
137953 pTab = sqlite3SrcListLookup(pParse, pTabList);
137954 if( pTab==0 ) goto update_cleanup;
137955 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137956
137957 /* Figure out if we have any triggers and if the table being
137958 ** updated is a view.
137959 */
137960#ifndef SQLITE_OMIT_TRIGGER
137961 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
137962 isView = pTab->pSelect!=0;
137963 assert( pTrigger || tmask==0 );
137964#else
137965# define pTrigger 0
137966# define isView 0
137967# define tmask 0
137968#endif
137969#ifdef SQLITE_OMIT_VIEW
137970# undef isView
137971# define isView 0
137972#endif
137973
137974 /* If there was a FROM clause, set nChangeFrom to the number of expressions
137975 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
137976 ** clause if this function is being called to generate code for part of
137977 ** an UPSERT statement. */
137978 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
137979 assert( nChangeFrom==0 || pUpsert==0 );
137980
137981#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
137982 if( !isView && nChangeFrom==0 ){
137983 pWhere = sqlite3LimitWhere(
137984 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
137985 );
137986 pOrderBy = 0;
137987 pLimit = 0;
137988 }
137989#endif
137990
137991 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
137992 goto update_cleanup;
137993 }
137994 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
137995 goto update_cleanup;
137996 }
137997
137998 /* Allocate a cursors for the main database table and for all indices.
137999 ** The index cursors might not be used, but if they are used they
138000 ** need to occur right after the database cursor. So go ahead and
138001 ** allocate enough space, just in case.
138002 */
138003 iBaseCur = iDataCur = pParse->nTab++;
138004 iIdxCur = iDataCur+1;
138005 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
138006 testcase( pPk!=0 && pPk!=pTab->pIndex );
138007 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
138008 if( pPk==pIdx ){
138009 iDataCur = pParse->nTab;
138010 }
138011 pParse->nTab++;
138012 }
138013 if( pUpsert ){
138014 /* On an UPSERT, reuse the same cursors already opened by INSERT */
138015 iDataCur = pUpsert->iDataCur;
138016 iIdxCur = pUpsert->iIdxCur;
138017 pParse->nTab = iBaseCur;
138018 }
138019 pTabList->a[0].iCursor = iDataCur;
138020
138021 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
138022 ** Initialize aXRef[] and aToOpen[] to their default values.
138023 */
138024 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
138025 if( aXRef==0 ) goto update_cleanup;
138026 aRegIdx = aXRef+pTab->nCol;
138027 aToOpen = (u8*)(aRegIdx+nIdx+1);
138028 memset(aToOpen, 1, nIdx+1);
138029 aToOpen[nIdx+1] = 0;
138030 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
138031
138032 /* Initialize the name-context */
138033 memset(&sNC, 0, sizeof(sNC));
138034 sNC.pParse = pParse;
138035 sNC.pSrcList = pTabList;
138036 sNC.uNC.pUpsert = pUpsert;
138037 sNC.ncFlags = NC_UUpsert;
138038
138039 /* Begin generating code. */
138040 v = sqlite3GetVdbe(pParse);
138041 if( v==0 ) goto update_cleanup;
138042
138043 /* Resolve the column names in all the expressions of the
138044 ** of the UPDATE statement. Also find the column index
138045 ** for each column to be updated in the pChanges array. For each
138046 ** column to be updated, make sure we have authorization to change
138047 ** that column.
138048 */
138049 chngRowid = chngPk = 0;
138050 for(i=0; i<pChanges->nExpr; i++){
138051 /* If this is an UPDATE with a FROM clause, do not resolve expressions
138052 ** here. The call to sqlite3Select() below will do that. */
138053 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
138054 goto update_cleanup;
138055 }
138056 for(j=0; j<pTab->nCol; j++){
138057 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zEName)==0 ){
138058 if( j==pTab->iPKey ){
138059 chngRowid = 1;
138060 pRowidExpr = pChanges->a[i].pExpr;
138061 iRowidExpr = i;
138062 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
138063 chngPk = 1;
138064 }
138065#ifndef SQLITE_OMIT_GENERATED_COLUMNS
138066 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
138067 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
138068 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
138069 sqlite3ErrorMsg(pParse,
138070 "cannot UPDATE generated column \"%s\"",
138071 pTab->aCol[j].zName);
138072 goto update_cleanup;
138073 }
138074#endif
138075 aXRef[j] = i;
138076 break;
138077 }
138078 }
138079 if( j>=pTab->nCol ){
138080 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
138081 j = -1;
138082 chngRowid = 1;
138083 pRowidExpr = pChanges->a[i].pExpr;
138084 iRowidExpr = i;
138085 }else{
138086 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
138087 pParse->checkSchema = 1;
138088 goto update_cleanup;
138089 }
138090 }
138091#ifndef SQLITE_OMIT_AUTHORIZATION
138092 {
138093 int rc;
138094 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
138095 j<0 ? "ROWID" : pTab->aCol[j].zName,
138096 db->aDb[iDb].zDbSName);
138097 if( rc==SQLITE_DENY ){
138098 goto update_cleanup;
138099 }else if( rc==SQLITE_IGNORE ){
138100 aXRef[j] = -1;
138101 }
138102 }
138103#endif
138104 }
138105 assert( (chngRowid & chngPk)==0 );
138106 assert( chngRowid==0 || chngRowid==1 );
138107 assert( chngPk==0 || chngPk==1 );
138108 chngKey = chngRowid + chngPk;
138109
138110#ifndef SQLITE_OMIT_GENERATED_COLUMNS
138111 /* Mark generated columns as changing if their generator expressions
138112 ** reference any changing column. The actual aXRef[] value for
138113 ** generated expressions is not used, other than to check to see that it
138114 ** is non-negative, so the value of aXRef[] for generated columns can be
138115 ** set to any non-negative number. We use 99999 so that the value is
138116 ** obvious when looking at aXRef[] in a symbolic debugger.
138117 */
138118 if( pTab->tabFlags & TF_HasGenerated ){
138119 int bProgress;
138120 testcase( pTab->tabFlags & TF_HasVirtual );
138121 testcase( pTab->tabFlags & TF_HasStored );
138122 do{
138123 bProgress = 0;
138124 for(i=0; i<pTab->nCol; i++){
138125 if( aXRef[i]>=0 ) continue;
138126 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
138127 if( sqlite3ExprReferencesUpdatedColumn(pTab->aCol[i].pDflt,
138128 aXRef, chngRowid) ){
138129 aXRef[i] = 99999;
138130 bProgress = 1;
138131 }
138132 }
138133 }while( bProgress );
138134 }
138135#endif
138136
138137 /* The SET expressions are not actually used inside the WHERE loop.
138138 ** So reset the colUsed mask. Unless this is a virtual table. In that
138139 ** case, set all bits of the colUsed mask (to ensure that the virtual
138140 ** table implementation makes all columns available).
138141 */
138142 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
138143
138144 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
138145
138146 /* There is one entry in the aRegIdx[] array for each index on the table
138147 ** being updated. Fill in aRegIdx[] with a register number that will hold
138148 ** the key for accessing each index.
138149 */
138150 if( onError==OE_Replace ) bReplace = 1;
138151 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
138152 int reg;
138153 if( chngKey || hasFK>1 || pIdx==pPk
138154 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
138155 ){
138156 reg = ++pParse->nMem;
138157 pParse->nMem += pIdx->nColumn;
138158 }else{
138159 reg = 0;
138160 for(i=0; i<pIdx->nKeyCol; i++){
138161 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
138162 reg = ++pParse->nMem;
138163 pParse->nMem += pIdx->nColumn;
138164 if( onError==OE_Default && pIdx->onError==OE_Replace ){
138165 bReplace = 1;
138166 }
138167 break;
138168 }
138169 }
138170 }
138171 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
138172 aRegIdx[nAllIdx] = reg;
138173 }
138174 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
138175 if( bReplace ){
138176 /* If REPLACE conflict resolution might be invoked, open cursors on all
138177 ** indexes in case they are needed to delete records. */
138178 memset(aToOpen, 1, nIdx+1);
138179 }
138180
138181 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
138182 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
138183
138184 /* Allocate required registers. */
138185 if( !IsVirtual(pTab) ){
138186 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
138187 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
138188 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
138189 ** table record is written. regRowSet holds the RowSet for the
138190 ** two-pass update algorithm. */
138191 assert( aRegIdx[nAllIdx]==pParse->nMem );
138192 regRowSet = aRegIdx[nAllIdx];
138193 regOldRowid = regNewRowid = ++pParse->nMem;
138194 if( chngPk || pTrigger || hasFK ){
138195 regOld = pParse->nMem + 1;
138196 pParse->nMem += pTab->nCol;
138197 }
138198 if( chngKey || pTrigger || hasFK ){
138199 regNewRowid = ++pParse->nMem;
138200 }
138201 regNew = pParse->nMem + 1;
138202 pParse->nMem += pTab->nCol;
138203 }
138204
138205 /* Start the view context. */
138206 if( isView ){
138207 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
138208 }
138209
138210 /* If we are trying to update a view, realize that view into
138211 ** an ephemeral table.
138212 */
138213#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
138214 if( nChangeFrom==0 && isView ){
138215 sqlite3MaterializeView(pParse, pTab,
138216 pWhere, pOrderBy, pLimit, iDataCur
138217 );
138218 pOrderBy = 0;
138219 pLimit = 0;
138220 }
138221#endif
138222
138223 /* Resolve the column names in all the expressions in the
138224 ** WHERE clause.
138225 */
138226 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){
138227 goto update_cleanup;
138228 }
138229
138230#ifndef SQLITE_OMIT_VIRTUALTABLE
138231 /* Virtual tables must be handled separately */
138232 if( IsVirtual(pTab) ){
138233 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
138234 pWhere, onError);
138235 goto update_cleanup;
138236 }
138237#endif
138238
138239 /* Jump to labelBreak to abandon further processing of this UPDATE */
138240 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
138241
138242 /* Not an UPSERT. Normal processing. Begin by
138243 ** initialize the count of updated rows */
138244 if( (db->flags&SQLITE_CountRows)!=0
138245 && !pParse->pTriggerTab
138246 && !pParse->nested
138247 && pUpsert==0
138248 ){
138249 regRowCount = ++pParse->nMem;
138250 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
138251 }
138252
138253 if( nChangeFrom==0 && HasRowid(pTab) ){
138254 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
138255 }else{
138256 assert( pPk!=0 || HasRowid(pTab) );
138257 nPk = pPk ? pPk->nKeyCol : 0;
138258 iPk = pParse->nMem+1;
138259 pParse->nMem += nPk;
138260 pParse->nMem += nChangeFrom;
138261 regKey = ++pParse->nMem;
138262 if( pUpsert==0 ){
138263 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
138264 iEph = pParse->nTab++;
138265 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
138266 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nEphCol);
138267 if( pPk ){
138268 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk);
138269 if( pKeyInfo ){
138270 pKeyInfo->nAllField = nEphCol;
138271 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
138272 }
138273 }
138274 if( nChangeFrom ){
138275 updateFromSelect(
138276 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
138277 );
138278#ifndef SQLITE_OMIT_SUBQUERY
138279 if( isView ) iDataCur = iEph;
138280#endif
138281 }
138282 }
138283 }
138284
138285 if( nChangeFrom ){
138286 sqlite3MultiWrite(pParse);
138287 eOnePass = ONEPASS_OFF;
138288 nKey = nPk;
138289 regKey = iPk;
138290 }else{
138291 if( pUpsert ){
138292 /* If this is an UPSERT, then all cursors have already been opened by
138293 ** the outer INSERT and the data cursor should be pointing at the row
138294 ** that is to be updated. So bypass the code that searches for the
138295 ** row(s) to be updated.
138296 */
138297 pWInfo = 0;
138298 eOnePass = ONEPASS_SINGLE;
138299 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
138300 bFinishSeek = 0;
138301 }else{
138302 /* Begin the database scan.
138303 **
138304 ** Do not consider a single-pass strategy for a multi-row update if
138305 ** there are any triggers or foreign keys to process, or rows may
138306 ** be deleted as a result of REPLACE conflict handling. Any of these
138307 ** things might disturb a cursor being used to scan through the table
138308 ** or index, causing a single-pass approach to malfunction. */
138309 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
138310 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
138311 flags |= WHERE_ONEPASS_MULTIROW;
138312 }
138313 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur);
138314 if( pWInfo==0 ) goto update_cleanup;
138315
138316 /* A one-pass strategy that might update more than one row may not
138317 ** be used if any column of the index used for the scan is being
138318 ** updated. Otherwise, if there is an index on "b", statements like
138319 ** the following could create an infinite loop:
138320 **
138321 ** UPDATE t1 SET b=b+1 WHERE b>?
138322 **
138323 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
138324 ** strategy that uses an index for which one or more columns are being
138325 ** updated. */
138326 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
138327 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
138328 if( eOnePass!=ONEPASS_SINGLE ){
138329 sqlite3MultiWrite(pParse);
138330 if( eOnePass==ONEPASS_MULTI ){
138331 int iCur = aiCurOnePass[1];
138332 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
138333 eOnePass = ONEPASS_OFF;
138334 }
138335 assert( iCur!=iDataCur || !HasRowid(pTab) );
138336 }
138337 }
138338 }
138339
138340 if( HasRowid(pTab) ){
138341 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
138342 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
138343 ** leave it in register regOldRowid. */
138344 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
138345 if( eOnePass==ONEPASS_OFF ){
138346 /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */
138347 aRegIdx[nAllIdx] = ++pParse->nMem;
138348 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
138349 }
138350 }else{
138351 /* Read the PK of the current row into an array of registers. In
138352 ** ONEPASS_OFF mode, serialize the array into a record and store it in
138353 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
138354 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
138355 ** is not required) and leave the PK fields in the array of registers. */
138356 for(i=0; i<nPk; i++){
138357 assert( pPk->aiColumn[i]>=0 );
138358 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,
138359 pPk->aiColumn[i], iPk+i);
138360 }
138361 if( eOnePass ){
138362 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
138363 nKey = nPk;
138364 regKey = iPk;
138365 }else{
138366 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
138367 sqlite3IndexAffinityStr(db, pPk), nPk);
138368 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
138369 }
138370 }
138371 }
138372
138373 if( pUpsert==0 ){
138374 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI ){
138375 sqlite3WhereEnd(pWInfo);
138376 }
138377
138378 if( !isView ){
138379 int addrOnce = 0;
138380
138381 /* Open every index that needs updating. */
138382 if( eOnePass!=ONEPASS_OFF ){
138383 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
138384 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
138385 }
138386
138387 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
138388 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
138389 }
138390 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
138391 aToOpen, 0, 0);
138392 if( addrOnce ){
138393 sqlite3VdbeJumpHereOrPopInst(v, addrOnce);
138394 }
138395 }
138396
138397 /* Top of the update loop */
138398 if( eOnePass!=ONEPASS_OFF ){
138399 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
138400 assert( pPk );
138401 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
138402 VdbeCoverage(v);
138403 }
138404 if( eOnePass!=ONEPASS_SINGLE ){
138405 labelContinue = sqlite3VdbeMakeLabel(pParse);
138406 }
138407 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
138408 VdbeCoverageIf(v, pPk==0);
138409 VdbeCoverageIf(v, pPk!=0);
138410 }else if( pPk || nChangeFrom ){
138411 labelContinue = sqlite3VdbeMakeLabel(pParse);
138412 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
138413 addrTop = sqlite3VdbeCurrentAddr(v);
138414 if( nChangeFrom ){
138415 if( !isView ){
138416 if( pPk ){
138417 for(i=0; i<nPk; i++){
138418 sqlite3VdbeAddOp3(v, OP_Column, iEph, i, iPk+i);
138419 }
138420 sqlite3VdbeAddOp4Int(
138421 v, OP_NotFound, iDataCur, labelContinue, iPk, nPk
138422 ); VdbeCoverage(v);
138423 }else{
138424 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, regOldRowid);
138425 sqlite3VdbeAddOp3(
138426 v, OP_NotExists, iDataCur, labelContinue, regOldRowid
138427 ); VdbeCoverage(v);
138428 }
138429 }
138430 }else{
138431 sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
138432 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey,0);
138433 VdbeCoverage(v);
138434 }
138435 }else{
138436 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
138437 regOldRowid);
138438 VdbeCoverage(v);
138439 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
138440 VdbeCoverage(v);
138441 }
138442 }
138443
138444 /* If the rowid value will change, set register regNewRowid to
138445 ** contain the new value. If the rowid is not being modified,
138446 ** then regNewRowid is the same register as regOldRowid, which is
138447 ** already populated. */
138448 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
138449 if( chngRowid ){
138450 assert( iRowidExpr>=0 );
138451 if( nChangeFrom==0 ){
138452 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
138453 }else{
138454 sqlite3VdbeAddOp3(v, OP_Column, iEph, iRowidExpr, regNewRowid);
138455 }
138456 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
138457 }
138458
138459 /* Compute the old pre-UPDATE content of the row being changed, if that
138460 ** information is needed */
138461 if( chngPk || hasFK || pTrigger ){
138462 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
138463 oldmask |= sqlite3TriggerColmask(pParse,
138464 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
138465 );
138466 for(i=0; i<pTab->nCol; i++){
138467 u32 colFlags = pTab->aCol[i].colFlags;
138468 k = sqlite3TableColumnToStorage(pTab, i) + regOld;
138469 if( oldmask==0xffffffff
138470 || (i<32 && (oldmask & MASKBIT32(i))!=0)
138471 || (colFlags & COLFLAG_PRIMKEY)!=0
138472 ){
138473 testcase( oldmask!=0xffffffff && i==31 );
138474 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
138475 }else{
138476 sqlite3VdbeAddOp2(v, OP_Null, 0, k);
138477 }
138478 }
138479 if( chngRowid==0 && pPk==0 ){
138480 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
138481 }
138482 }
138483
138484 /* Populate the array of registers beginning at regNew with the new
138485 ** row data. This array is used to check constants, create the new
138486 ** table and index records, and as the values for any new.* references
138487 ** made by triggers.
138488 **
138489 ** If there are one or more BEFORE triggers, then do not populate the
138490 ** registers associated with columns that are (a) not modified by
138491 ** this UPDATE statement and (b) not accessed by new.* references. The
138492 ** values for registers not modified by the UPDATE must be reloaded from
138493 ** the database after the BEFORE triggers are fired anyway (as the trigger
138494 ** may have modified them). So not loading those that are not going to
138495 ** be used eliminates some redundant opcodes.
138496 */
138497 newmask = sqlite3TriggerColmask(
138498 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
138499 );
138500 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
138501 if( i==pTab->iPKey ){
138502 sqlite3VdbeAddOp2(v, OP_Null, 0, k);
138503 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
138504 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
138505 }else{
138506 j = aXRef[i];
138507 if( j>=0 ){
138508 if( nChangeFrom ){
138509 int nOff = (isView ? pTab->nCol : nPk);
138510 assert( eOnePass==ONEPASS_OFF );
138511 sqlite3VdbeAddOp3(v, OP_Column, iEph, nOff+j, k);
138512 }else{
138513 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
138514 }
138515 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
138516 /* This branch loads the value of a column that will not be changed
138517 ** into a register. This is done if there are no BEFORE triggers, or
138518 ** if there are one or more BEFORE triggers that use this value via
138519 ** a new.* reference in a trigger program.
138520 */
138521 testcase( i==31 );
138522 testcase( i==32 );
138523 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
138524 bFinishSeek = 0;
138525 }else{
138526 sqlite3VdbeAddOp2(v, OP_Null, 0, k);
138527 }
138528 }
138529 }
138530#ifndef SQLITE_OMIT_GENERATED_COLUMNS
138531 if( pTab->tabFlags & TF_HasGenerated ){
138532 testcase( pTab->tabFlags & TF_HasVirtual );
138533 testcase( pTab->tabFlags & TF_HasStored );
138534 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
138535 }
138536#endif
138537
138538 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
138539 ** verified. One could argue that this is wrong.
138540 */
138541 if( tmask&TRIGGER_BEFORE ){
138542 sqlite3TableAffinity(v, pTab, regNew);
138543 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
138544 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
138545
138546 if( !isView ){
138547 /* The row-trigger may have deleted the row being updated. In this
138548 ** case, jump to the next row. No updates or AFTER triggers are
138549 ** required. This behavior - what happens when the row being updated
138550 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
138551 ** documentation.
138552 */
138553 if( pPk ){
138554 sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey);
138555 VdbeCoverage(v);
138556 }else{
138557 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid);
138558 VdbeCoverage(v);
138559 }
138560
138561 /* After-BEFORE-trigger-reload-loop:
138562 ** If it did not delete it, the BEFORE trigger may still have modified
138563 ** some of the columns of the row being updated. Load the values for
138564 ** all columns not modified by the update statement into their registers
138565 ** in case this has happened. Only unmodified columns are reloaded.
138566 ** The values computed for modified columns use the values before the
138567 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
138568 ** for an example.
138569 */
138570 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
138571 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
138572 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
138573 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
138574 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
138575 }
138576 }
138577#ifndef SQLITE_OMIT_GENERATED_COLUMNS
138578 if( pTab->tabFlags & TF_HasGenerated ){
138579 testcase( pTab->tabFlags & TF_HasVirtual );
138580 testcase( pTab->tabFlags & TF_HasStored );
138581 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
138582 }
138583#endif
138584 }
138585 }
138586
138587 if( !isView ){
138588 /* Do constraint checks. */
138589 assert( regOldRowid>0 );
138590 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
138591 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
138592 aXRef, 0);
138593
138594 /* If REPLACE conflict handling may have been used, or if the PK of the
138595 ** row is changing, then the GenerateConstraintChecks() above may have
138596 ** moved cursor iDataCur. Reseek it. */
138597 if( bReplace || chngKey ){
138598 if( pPk ){
138599 sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey);
138600 }else{
138601 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid);
138602 }
138603 VdbeCoverageNeverTaken(v);
138604 }
138605
138606 /* Do FK constraint checks. */
138607 if( hasFK ){
138608 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
138609 }
138610
138611 /* Delete the index entries associated with the current record. */
138612 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
138613
138614 /* We must run the OP_FinishSeek opcode to resolve a prior
138615 ** OP_DeferredSeek if there is any possibility that there have been
138616 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
138617 ** we want to avoid the OP_FinishSeek if possible, as running it
138618 ** costs CPU cycles. */
138619 if( bFinishSeek ){
138620 sqlite3VdbeAddOp1(v, OP_FinishSeek, iDataCur);
138621 }
138622
138623 /* If changing the rowid value, or if there are foreign key constraints
138624 ** to process, delete the old record. Otherwise, add a noop OP_Delete
138625 ** to invoke the pre-update hook.
138626 **
138627 ** That (regNew==regnewRowid+1) is true is also important for the
138628 ** pre-update hook. If the caller invokes preupdate_new(), the returned
138629 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
138630 ** is the column index supplied by the user.
138631 */
138632 assert( regNew==regNewRowid+1 );
138633#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
138634 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
138635 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
138636 regNewRowid
138637 );
138638 if( eOnePass==ONEPASS_MULTI ){
138639 assert( hasFK==0 && chngKey==0 );
138640 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
138641 }
138642 if( !pParse->nested ){
138643 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
138644 }
138645#else
138646 if( hasFK>1 || chngKey ){
138647 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
138648 }
138649#endif
138650
138651 if( hasFK ){
138652 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
138653 }
138654
138655 /* Insert the new index entries and the new record. */
138656 sqlite3CompleteInsertion(
138657 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
138658 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
138659 0, 0
138660 );
138661
138662 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
138663 ** handle rows (possibly in other tables) that refer via a foreign key
138664 ** to the row just updated. */
138665 if( hasFK ){
138666 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
138667 }
138668 }
138669
138670 /* Increment the row counter
138671 */
138672 if( regRowCount ){
138673 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
138674 }
138675
138676 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
138677 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
138678
138679 /* Repeat the above with the next record to be updated, until
138680 ** all record selected by the WHERE clause have been updated.
138681 */
138682 if( eOnePass==ONEPASS_SINGLE ){
138683 /* Nothing to do at end-of-loop for a single-pass */
138684 }else if( eOnePass==ONEPASS_MULTI ){
138685 sqlite3VdbeResolveLabel(v, labelContinue);
138686 sqlite3WhereEnd(pWInfo);
138687 }else if( pPk || nChangeFrom ){
138688 sqlite3VdbeResolveLabel(v, labelContinue);
138689 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
138690 }else{
138691 sqlite3VdbeGoto(v, labelContinue);
138692 }
138693 sqlite3VdbeResolveLabel(v, labelBreak);
138694
138695 /* Update the sqlite_sequence table by storing the content of the
138696 ** maximum rowid counter values recorded while inserting into
138697 ** autoincrement tables.
138698 */
138699 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
138700 sqlite3AutoincrementEnd(pParse);
138701 }
138702
138703 /*
138704 ** Return the number of rows that were changed, if we are tracking
138705 ** that information.
138706 */
138707 if( regRowCount ){
138708 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
138709 sqlite3VdbeSetNumCols(v, 1);
138710 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
138711 }
138712
138713update_cleanup:
138714 sqlite3AuthContextPop(&sContext);
138715 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
138716 sqlite3SrcListDelete(db, pTabList);
138717 sqlite3ExprListDelete(db, pChanges);
138718 sqlite3ExprDelete(db, pWhere);
138719#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
138720 sqlite3ExprListDelete(db, pOrderBy);
138721 sqlite3ExprDelete(db, pLimit);
138722#endif
138723 return;
138724}
138725/* Make sure "isView" and other macros defined above are undefined. Otherwise
138726** they may interfere with compilation of other functions in this file
138727** (or in another file, if this file becomes part of the amalgamation). */
138728#ifdef isView
138729 #undef isView
138730#endif
138731#ifdef pTrigger
138732 #undef pTrigger
138733#endif
138734
138735#ifndef SQLITE_OMIT_VIRTUALTABLE
138736/*
138737** Generate code for an UPDATE of a virtual table.
138738**
138739** There are two possible strategies - the default and the special
138740** "onepass" strategy. Onepass is only used if the virtual table
138741** implementation indicates that pWhere may match at most one row.
138742**
138743** The default strategy is to create an ephemeral table that contains
138744** for each row to be changed:
138745**
138746** (A) The original rowid of that row.
138747** (B) The revised rowid for the row.
138748** (C) The content of every column in the row.
138749**
138750** Then loop through the contents of this ephemeral table executing a
138751** VUpdate for each row. When finished, drop the ephemeral table.
138752**
138753** The "onepass" strategy does not use an ephemeral table. Instead, it
138754** stores the same values (A, B and C above) in a register array and
138755** makes a single invocation of VUpdate.
138756*/
138757static void updateVirtualTable(
138758 Parse *pParse, /* The parsing context */
138759 SrcList *pSrc, /* The virtual table to be modified */
138760 Table *pTab, /* The virtual table */
138761 ExprList *pChanges, /* The columns to change in the UPDATE statement */
138762 Expr *pRowid, /* Expression used to recompute the rowid */
138763 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
138764 Expr *pWhere, /* WHERE clause of the UPDATE statement */
138765 int onError /* ON CONFLICT strategy */
138766){
138767 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
138768 int ephemTab; /* Table holding the result of the SELECT */
138769 int i; /* Loop counter */
138770 sqlite3 *db = pParse->db; /* Database connection */
138771 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
138772 WhereInfo *pWInfo = 0;
138773 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
138774 int regArg; /* First register in VUpdate arg array */
138775 int regRec; /* Register in which to assemble record */
138776 int regRowid; /* Register for ephem table rowid */
138777 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
138778 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
138779 int eOnePass; /* True to use onepass strategy */
138780 int addr; /* Address of OP_OpenEphemeral */
138781
138782 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
138783 ** create and open the ephemeral table in which the records created from
138784 ** these arguments will be temporarily stored. */
138785 assert( v );
138786 ephemTab = pParse->nTab++;
138787 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
138788 regArg = pParse->nMem + 1;
138789 pParse->nMem += nArg;
138790 if( pSrc->nSrc>1 ){
138791 Expr *pRow;
138792 ExprList *pList;
138793 if( pRowid ){
138794 pRow = sqlite3ExprDup(db, pRowid, 0);
138795 }else{
138796 pRow = sqlite3PExpr(pParse, TK_ROW, 0, 0);
138797 }
138798 pList = sqlite3ExprListAppend(pParse, 0, pRow);
138799
138800 for(i=0; i<pTab->nCol; i++){
138801 if( aXRef[i]>=0 ){
138802 pList = sqlite3ExprListAppend(pParse, pList,
138803 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
138804 );
138805 }else{
138806 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
138807 }
138808 }
138809
138810 updateFromSelect(pParse, ephemTab, 0, pList, pSrc, pWhere, 0, 0);
138811 sqlite3ExprListDelete(db, pList);
138812 eOnePass = ONEPASS_OFF;
138813 }else{
138814 regRec = ++pParse->nMem;
138815 regRowid = ++pParse->nMem;
138816
138817 /* Start scanning the virtual table */
138818 pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0);
138819 if( pWInfo==0 ) return;
138820
138821 /* Populate the argument registers. */
138822 for(i=0; i<pTab->nCol; i++){
138823 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
138824 if( aXRef[i]>=0 ){
138825 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
138826 }else{
138827 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
138828 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* For sqlite3_vtab_nochange() */
138829 }
138830 }
138831 if( HasRowid(pTab) ){
138832 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
138833 if( pRowid ){
138834 sqlite3ExprCode(pParse, pRowid, regArg+1);
138835 }else{
138836 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
138837 }
138838 }else{
138839 Index *pPk; /* PRIMARY KEY index */
138840 i16 iPk; /* PRIMARY KEY column */
138841 pPk = sqlite3PrimaryKeyIndex(pTab);
138842 assert( pPk!=0 );
138843 assert( pPk->nKeyCol==1 );
138844 iPk = pPk->aiColumn[0];
138845 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
138846 sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
138847 }
138848
138849 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
138850
138851 /* There is no ONEPASS_MULTI on virtual tables */
138852 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
138853
138854 if( eOnePass ){
138855 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
138856 ** above. */
138857 sqlite3VdbeChangeToNoop(v, addr);
138858 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
138859 }else{
138860 /* Create a record from the argument register contents and insert it into
138861 ** the ephemeral table. */
138862 sqlite3MultiWrite(pParse);
138863 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
138864#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
138865 /* Signal an assert() within OP_MakeRecord that it is allowed to
138866 ** accept no-change records with serial_type 10 */
138867 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
138868#endif
138869 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
138870 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
138871 }
138872 }
138873
138874
138875 if( eOnePass==ONEPASS_OFF ){
138876 /* End the virtual table scan */
138877 if( pSrc->nSrc==1 ){
138878 sqlite3WhereEnd(pWInfo);
138879 }
138880
138881 /* Begin scannning through the ephemeral table. */
138882 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
138883
138884 /* Extract arguments from the current row of the ephemeral table and
138885 ** invoke the VUpdate method. */
138886 for(i=0; i<nArg; i++){
138887 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
138888 }
138889 }
138890 sqlite3VtabMakeWritable(pParse, pTab);
138891 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
138892 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
138893 sqlite3MayAbort(pParse);
138894
138895 /* End of the ephemeral table scan. Or, if using the onepass strategy,
138896 ** jump to here if the scan visited zero rows. */
138897 if( eOnePass==ONEPASS_OFF ){
138898 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
138899 sqlite3VdbeJumpHere(v, addr);
138900 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
138901 }else{
138902 sqlite3WhereEnd(pWInfo);
138903 }
138904}
138905#endif /* SQLITE_OMIT_VIRTUALTABLE */
138906
138907/************** End of update.c **********************************************/
138908/************** Begin file upsert.c ******************************************/
138909/*
138910** 2018-04-12
138911**
138912** The author disclaims copyright to this source code. In place of
138913** a legal notice, here is a blessing:
138914**
138915** May you do good and not evil.
138916** May you find forgiveness for yourself and forgive others.
138917** May you share freely, never taking more than you give.
138918**
138919*************************************************************************
138920** This file contains code to implement various aspects of UPSERT
138921** processing and handling of the Upsert object.
138922*/
138923/* #include "sqliteInt.h" */
138924
138925#ifndef SQLITE_OMIT_UPSERT
138926/*
138927** Free a list of Upsert objects
138928*/
138929SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
138930 if( p ){
138931 sqlite3ExprListDelete(db, p->pUpsertTarget);
138932 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
138933 sqlite3ExprListDelete(db, p->pUpsertSet);
138934 sqlite3ExprDelete(db, p->pUpsertWhere);
138935 sqlite3DbFree(db, p);
138936 }
138937}
138938
138939/*
138940** Duplicate an Upsert object.
138941*/
138942SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
138943 if( p==0 ) return 0;
138944 return sqlite3UpsertNew(db,
138945 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
138946 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
138947 sqlite3ExprListDup(db, p->pUpsertSet, 0),
138948 sqlite3ExprDup(db, p->pUpsertWhere, 0)
138949 );
138950}
138951
138952/*
138953** Create a new Upsert object.
138954*/
138955SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
138956 sqlite3 *db, /* Determines which memory allocator to use */
138957 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
138958 Expr *pTargetWhere, /* Optional WHERE clause on the target */
138959 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
138960 Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
138961){
138962 Upsert *pNew;
138963 pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
138964 if( pNew==0 ){
138965 sqlite3ExprListDelete(db, pTarget);
138966 sqlite3ExprDelete(db, pTargetWhere);
138967 sqlite3ExprListDelete(db, pSet);
138968 sqlite3ExprDelete(db, pWhere);
138969 return 0;
138970 }else{
138971 pNew->pUpsertTarget = pTarget;
138972 pNew->pUpsertTargetWhere = pTargetWhere;
138973 pNew->pUpsertSet = pSet;
138974 pNew->pUpsertWhere = pWhere;
138975 pNew->pUpsertIdx = 0;
138976 }
138977 return pNew;
138978}
138979
138980/*
138981** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
138982** symbols in the conflict-target.
138983**
138984** Return SQLITE_OK if everything works, or an error code is something
138985** is wrong.
138986*/
138987SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
138988 Parse *pParse, /* The parsing context */
138989 SrcList *pTabList, /* Table into which we are inserting */
138990 Upsert *pUpsert /* The ON CONFLICT clauses */
138991){
138992 Table *pTab; /* That table into which we are inserting */
138993 int rc; /* Result code */
138994 int iCursor; /* Cursor used by pTab */
138995 Index *pIdx; /* One of the indexes of pTab */
138996 ExprList *pTarget; /* The conflict-target clause */
138997 Expr *pTerm; /* One term of the conflict-target clause */
138998 NameContext sNC; /* Context for resolving symbolic names */
138999 Expr sCol[2]; /* Index column converted into an Expr */
139000
139001 assert( pTabList->nSrc==1 );
139002 assert( pTabList->a[0].pTab!=0 );
139003 assert( pUpsert!=0 );
139004 assert( pUpsert->pUpsertTarget!=0 );
139005
139006 /* Resolve all symbolic names in the conflict-target clause, which
139007 ** includes both the list of columns and the optional partial-index
139008 ** WHERE clause.
139009 */
139010 memset(&sNC, 0, sizeof(sNC));
139011 sNC.pParse = pParse;
139012 sNC.pSrcList = pTabList;
139013 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
139014 if( rc ) return rc;
139015 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
139016 if( rc ) return rc;
139017
139018 /* Check to see if the conflict target matches the rowid. */
139019 pTab = pTabList->a[0].pTab;
139020 pTarget = pUpsert->pUpsertTarget;
139021 iCursor = pTabList->a[0].iCursor;
139022 if( HasRowid(pTab)
139023 && pTarget->nExpr==1
139024 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
139025 && pTerm->iColumn==XN_ROWID
139026 ){
139027 /* The conflict-target is the rowid of the primary table */
139028 assert( pUpsert->pUpsertIdx==0 );
139029 return SQLITE_OK;
139030 }
139031
139032 /* Initialize sCol[0..1] to be an expression parse tree for a
139033 ** single column of an index. The sCol[0] node will be the TK_COLLATE
139034 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
139035 ** will populate the specific collation and column number values
139036 ** prior to comparing against the conflict-target expression.
139037 */
139038 memset(sCol, 0, sizeof(sCol));
139039 sCol[0].op = TK_COLLATE;
139040 sCol[0].pLeft = &sCol[1];
139041 sCol[1].op = TK_COLUMN;
139042 sCol[1].iTable = pTabList->a[0].iCursor;
139043
139044 /* Check for matches against other indexes */
139045 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139046 int ii, jj, nn;
139047 if( !IsUniqueIndex(pIdx) ) continue;
139048 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
139049 if( pIdx->pPartIdxWhere ){
139050 if( pUpsert->pUpsertTargetWhere==0 ) continue;
139051 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
139052 pIdx->pPartIdxWhere, iCursor)!=0 ){
139053 continue;
139054 }
139055 }
139056 nn = pIdx->nKeyCol;
139057 for(ii=0; ii<nn; ii++){
139058 Expr *pExpr;
139059 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
139060 if( pIdx->aiColumn[ii]==XN_EXPR ){
139061 assert( pIdx->aColExpr!=0 );
139062 assert( pIdx->aColExpr->nExpr>ii );
139063 pExpr = pIdx->aColExpr->a[ii].pExpr;
139064 if( pExpr->op!=TK_COLLATE ){
139065 sCol[0].pLeft = pExpr;
139066 pExpr = &sCol[0];
139067 }
139068 }else{
139069 sCol[0].pLeft = &sCol[1];
139070 sCol[1].iColumn = pIdx->aiColumn[ii];
139071 pExpr = &sCol[0];
139072 }
139073 for(jj=0; jj<nn; jj++){
139074 if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
139075 break; /* Column ii of the index matches column jj of target */
139076 }
139077 }
139078 if( jj>=nn ){
139079 /* The target contains no match for column jj of the index */
139080 break;
139081 }
139082 }
139083 if( ii<nn ){
139084 /* Column ii of the index did not match any term of the conflict target.
139085 ** Continue the search with the next index. */
139086 continue;
139087 }
139088 pUpsert->pUpsertIdx = pIdx;
139089 return SQLITE_OK;
139090 }
139091 sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
139092 "PRIMARY KEY or UNIQUE constraint");
139093 return SQLITE_ERROR;
139094}
139095
139096/*
139097** Generate bytecode that does an UPDATE as part of an upsert.
139098**
139099** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
139100** In this case parameter iCur is a cursor open on the table b-tree that
139101** currently points to the conflicting table row. Otherwise, if pIdx
139102** is not NULL, then pIdx is the constraint that failed and iCur is a
139103** cursor points to the conflicting row.
139104*/
139105SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
139106 Parse *pParse, /* The parsing and code-generating context */
139107 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
139108 Table *pTab, /* The table being updated */
139109 Index *pIdx, /* The UNIQUE constraint that failed */
139110 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
139111){
139112 Vdbe *v = pParse->pVdbe;
139113 sqlite3 *db = pParse->db;
139114 SrcList *pSrc; /* FROM clause for the UPDATE */
139115 int iDataCur;
139116 int i;
139117
139118 assert( v!=0 );
139119 assert( pUpsert!=0 );
139120 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
139121 iDataCur = pUpsert->iDataCur;
139122 if( pIdx && iCur!=iDataCur ){
139123 if( HasRowid(pTab) ){
139124 int regRowid = sqlite3GetTempReg(pParse);
139125 sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
139126 sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
139127 VdbeCoverage(v);
139128 sqlite3ReleaseTempReg(pParse, regRowid);
139129 }else{
139130 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
139131 int nPk = pPk->nKeyCol;
139132 int iPk = pParse->nMem+1;
139133 pParse->nMem += nPk;
139134 for(i=0; i<nPk; i++){
139135 int k;
139136 assert( pPk->aiColumn[i]>=0 );
139137 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
139138 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
139139 VdbeComment((v, "%s.%s", pIdx->zName,
139140 pTab->aCol[pPk->aiColumn[i]].zName));
139141 }
139142 sqlite3VdbeVerifyAbortable(v, OE_Abort);
139143 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
139144 VdbeCoverage(v);
139145 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
139146 "corrupt database", P4_STATIC);
139147 sqlite3MayAbort(pParse);
139148 sqlite3VdbeJumpHere(v, i);
139149 }
139150 }
139151 /* pUpsert does not own pUpsertSrc - the outer INSERT statement does. So
139152 ** we have to make a copy before passing it down into sqlite3Update() */
139153 pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
139154 /* excluded.* columns of type REAL need to be converted to a hard real */
139155 for(i=0; i<pTab->nCol; i++){
139156 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
139157 sqlite3VdbeAddOp1(v, OP_RealAffinity, pUpsert->regData+i);
139158 }
139159 }
139160 sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
139161 pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
139162 pUpsert->pUpsertSet = 0; /* Will have been deleted by sqlite3Update() */
139163 pUpsert->pUpsertWhere = 0; /* Will have been deleted by sqlite3Update() */
139164 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
139165}
139166
139167#endif /* SQLITE_OMIT_UPSERT */
139168
139169/************** End of upsert.c **********************************************/
139170/************** Begin file vacuum.c ******************************************/
139171/*
139172** 2003 April 6
139173**
139174** The author disclaims copyright to this source code. In place of
139175** a legal notice, here is a blessing:
139176**
139177** May you do good and not evil.
139178** May you find forgiveness for yourself and forgive others.
139179** May you share freely, never taking more than you give.
139180**
139181*************************************************************************
139182** This file contains code used to implement the VACUUM command.
139183**
139184** Most of the code in this file may be omitted by defining the
139185** SQLITE_OMIT_VACUUM macro.
139186*/
139187/* #include "sqliteInt.h" */
139188/* #include "vdbeInt.h" */
139189
139190#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
139191
139192/*
139193** Execute zSql on database db.
139194**
139195** If zSql returns rows, then each row will have exactly one
139196** column. (This will only happen if zSql begins with "SELECT".)
139197** Take each row of result and call execSql() again recursively.
139198**
139199** The execSqlF() routine does the same thing, except it accepts
139200** a format string as its third argument
139201*/
139202static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
139203 sqlite3_stmt *pStmt;
139204 int rc;
139205
139206 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
139207 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
139208 if( rc!=SQLITE_OK ) return rc;
139209 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
139210 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
139211 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
139212 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
139213 ** or INSERT. Historically there have been attacks that first
139214 ** corrupt the sqlite_schema.sql field with other kinds of statements
139215 ** then run VACUUM to get those statements to execute at inappropriate
139216 ** times. */
139217 if( zSubSql
139218 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
139219 ){
139220 rc = execSql(db, pzErrMsg, zSubSql);
139221 if( rc!=SQLITE_OK ) break;
139222 }
139223 }
139224 assert( rc!=SQLITE_ROW );
139225 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
139226 if( rc ){
139227 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
139228 }
139229 (void)sqlite3_finalize(pStmt);
139230 return rc;
139231}
139232static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
139233 char *z;
139234 va_list ap;
139235 int rc;
139236 va_start(ap, zSql);
139237 z = sqlite3VMPrintf(db, zSql, ap);
139238 va_end(ap);
139239 if( z==0 ) return SQLITE_NOMEM;
139240 rc = execSql(db, pzErrMsg, z);
139241 sqlite3DbFree(db, z);
139242 return rc;
139243}
139244
139245/*
139246** The VACUUM command is used to clean up the database,
139247** collapse free space, etc. It is modelled after the VACUUM command
139248** in PostgreSQL. The VACUUM command works as follows:
139249**
139250** (1) Create a new transient database file
139251** (2) Copy all content from the database being vacuumed into
139252** the new transient database file
139253** (3) Copy content from the transient database back into the
139254** original database.
139255**
139256** The transient database requires temporary disk space approximately
139257** equal to the size of the original database. The copy operation of
139258** step (3) requires additional temporary disk space approximately equal
139259** to the size of the original database for the rollback journal.
139260** Hence, temporary disk space that is approximately 2x the size of the
139261** original database is required. Every page of the database is written
139262** approximately 3 times: Once for step (2) and twice for step (3).
139263** Two writes per page are required in step (3) because the original
139264** database content must be written into the rollback journal prior to
139265** overwriting the database with the vacuumed content.
139266**
139267** Only 1x temporary space and only 1x writes would be required if
139268** the copy of step (3) were replaced by deleting the original database
139269** and renaming the transient database as the original. But that will
139270** not work if other processes are attached to the original database.
139271** And a power loss in between deleting the original and renaming the
139272** transient would cause the database file to appear to be deleted
139273** following reboot.
139274*/
139275SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
139276 Vdbe *v = sqlite3GetVdbe(pParse);
139277 int iDb = 0;
139278 if( v==0 ) goto build_vacuum_end;
139279 if( pParse->nErr ) goto build_vacuum_end;
139280 if( pNm ){
139281#ifndef SQLITE_BUG_COMPATIBLE_20160819
139282 /* Default behavior: Report an error if the argument to VACUUM is
139283 ** not recognized */
139284 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
139285 if( iDb<0 ) goto build_vacuum_end;
139286#else
139287 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
139288 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
139289 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
139290 ** The buggy behavior is required for binary compatibility with some
139291 ** legacy applications. */
139292 iDb = sqlite3FindDb(pParse->db, pNm);
139293 if( iDb<0 ) iDb = 0;
139294#endif
139295 }
139296 if( iDb!=1 ){
139297 int iIntoReg = 0;
139298 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
139299 iIntoReg = ++pParse->nMem;
139300 sqlite3ExprCode(pParse, pInto, iIntoReg);
139301 }
139302 sqlite3VdbeAddOp2(v, OP_Vacuum, iDb, iIntoReg);
139303 sqlite3VdbeUsesBtree(v, iDb);
139304 }
139305build_vacuum_end:
139306 sqlite3ExprDelete(pParse->db, pInto);
139307 return;
139308}
139309
139310/*
139311** This routine implements the OP_Vacuum opcode of the VDBE.
139312*/
139313SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
139314 char **pzErrMsg, /* Write error message here */
139315 sqlite3 *db, /* Database connection */
139316 int iDb, /* Which attached DB to vacuum */
139317 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
139318){
139319 int rc = SQLITE_OK; /* Return code from service routines */
139320 Btree *pMain; /* The database being vacuumed */
139321 Btree *pTemp; /* The temporary database we vacuum into */
139322 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
139323 u64 saved_flags; /* Saved value of db->flags */
139324 int saved_nChange; /* Saved value of db->nChange */
139325 int saved_nTotalChange; /* Saved value of db->nTotalChange */
139326 u32 saved_openFlags; /* Saved value of db->openFlags */
139327 u8 saved_mTrace; /* Saved trace settings */
139328 Db *pDb = 0; /* Database to detach at end of vacuum */
139329 int isMemDb; /* True if vacuuming a :memory: database */
139330 int nRes; /* Bytes of reserved space at the end of each page */
139331 int nDb; /* Number of attached databases */
139332 const char *zDbMain; /* Schema name of database to vacuum */
139333 const char *zOut; /* Name of output file */
139334
139335 if( !db->autoCommit ){
139336 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
139337 return SQLITE_ERROR; /* IMP: R-12218-18073 */
139338 }
139339 if( db->nVdbeActive>1 ){
139340 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
139341 return SQLITE_ERROR; /* IMP: R-15610-35227 */
139342 }
139343 saved_openFlags = db->openFlags;
139344 if( pOut ){
139345 if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
139346 sqlite3SetString(pzErrMsg, db, "non-text filename");
139347 return SQLITE_ERROR;
139348 }
139349 zOut = (const char*)sqlite3_value_text(pOut);
139350 db->openFlags &= ~SQLITE_OPEN_READONLY;
139351 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
139352 }else{
139353 zOut = "";
139354 }
139355
139356 /* Save the current value of the database flags so that it can be
139357 ** restored before returning. Then set the writable-schema flag, and
139358 ** disable CHECK and foreign key constraints. */
139359 saved_flags = db->flags;
139360 saved_mDbFlags = db->mDbFlags;
139361 saved_nChange = db->nChange;
139362 saved_nTotalChange = db->nTotalChange;
139363 saved_mTrace = db->mTrace;
139364 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
139365 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
139366 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
139367 | SQLITE_Defensive | SQLITE_CountRows);
139368 db->mTrace = 0;
139369
139370 zDbMain = db->aDb[iDb].zDbSName;
139371 pMain = db->aDb[iDb].pBt;
139372 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
139373
139374 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
139375 ** can be set to 'off' for this file, as it is not recovered if a crash
139376 ** occurs anyway. The integrity of the database is maintained by a
139377 ** (possibly synchronous) transaction opened on the main database before
139378 ** sqlite3BtreeCopyFile() is called.
139379 **
139380 ** An optimisation would be to use a non-journaled pager.
139381 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
139382 ** that actually made the VACUUM run slower. Very little journalling
139383 ** actually occurs when doing a vacuum since the vacuum_db is initially
139384 ** empty. Only the journal header is written. Apparently it takes more
139385 ** time to parse and run the PRAGMA to turn journalling off than it does
139386 ** to write the journal header file.
139387 */
139388 nDb = db->nDb;
139389 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
139390 db->openFlags = saved_openFlags;
139391 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139392 assert( (db->nDb-1)==nDb );
139393 pDb = &db->aDb[nDb];
139394 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
139395 pTemp = pDb->pBt;
139396 if( pOut ){
139397 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
139398 i64 sz = 0;
139399 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
139400 rc = SQLITE_ERROR;
139401 sqlite3SetString(pzErrMsg, db, "output file already exists");
139402 goto end_of_vacuum;
139403 }
139404 db->mDbFlags |= DBFLAG_VacuumInto;
139405 }
139406 nRes = sqlite3BtreeGetRequestedReserve(pMain);
139407
139408 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
139409 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
139410 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
139411
139412 /* Begin a transaction and take an exclusive lock on the main database
139413 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
139414 ** to ensure that we do not try to change the page-size on a WAL database.
139415 */
139416 rc = execSql(db, pzErrMsg, "BEGIN");
139417 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139418 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
139419 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139420
139421 /* Do not attempt to change the page size for a WAL database */
139422 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
139423 ==PAGER_JOURNALMODE_WAL ){
139424 db->nextPagesize = 0;
139425 }
139426
139427 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
139428 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
139429 || NEVER(db->mallocFailed)
139430 ){
139431 rc = SQLITE_NOMEM_BKPT;
139432 goto end_of_vacuum;
139433 }
139434
139435#ifndef SQLITE_OMIT_AUTOVACUUM
139436 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
139437 sqlite3BtreeGetAutoVacuum(pMain));
139438#endif
139439
139440 /* Query the schema of the main database. Create a mirror schema
139441 ** in the temporary database.
139442 */
139443 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
139444 rc = execSqlF(db, pzErrMsg,
139445 "SELECT sql FROM \"%w\".sqlite_schema"
139446 " WHERE type='table'AND name<>'sqlite_sequence'"
139447 " AND coalesce(rootpage,1)>0",
139448 zDbMain
139449 );
139450 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139451 rc = execSqlF(db, pzErrMsg,
139452 "SELECT sql FROM \"%w\".sqlite_schema"
139453 " WHERE type='index'",
139454 zDbMain
139455 );
139456 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139457 db->init.iDb = 0;
139458
139459 /* Loop through the tables in the main database. For each, do
139460 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
139461 ** the contents to the temporary database.
139462 */
139463 rc = execSqlF(db, pzErrMsg,
139464 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
139465 "||' SELECT*FROM\"%w\".'||quote(name)"
139466 "FROM vacuum_db.sqlite_schema "
139467 "WHERE type='table'AND coalesce(rootpage,1)>0",
139468 zDbMain
139469 );
139470 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
139471 db->mDbFlags &= ~DBFLAG_Vacuum;
139472 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139473
139474 /* Copy the triggers, views, and virtual tables from the main database
139475 ** over to the temporary database. None of these objects has any
139476 ** associated storage, so all we have to do is copy their entries
139477 ** from the schema table.
139478 */
139479 rc = execSqlF(db, pzErrMsg,
139480 "INSERT INTO vacuum_db.sqlite_schema"
139481 " SELECT*FROM \"%w\".sqlite_schema"
139482 " WHERE type IN('view','trigger')"
139483 " OR(type='table'AND rootpage=0)",
139484 zDbMain
139485 );
139486 if( rc ) goto end_of_vacuum;
139487
139488 /* At this point, there is a write transaction open on both the
139489 ** vacuum database and the main database. Assuming no error occurs,
139490 ** both transactions are closed by this block - the main database
139491 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
139492 ** call to sqlite3BtreeCommit().
139493 */
139494 {
139495 u32 meta;
139496 int i;
139497
139498 /* This array determines which meta meta values are preserved in the
139499 ** vacuum. Even entries are the meta value number and odd entries
139500 ** are an increment to apply to the meta value after the vacuum.
139501 ** The increment is used to increase the schema cookie so that other
139502 ** connections to the same database will know to reread the schema.
139503 */
139504 static const unsigned char aCopy[] = {
139505 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
139506 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
139507 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
139508 BTREE_USER_VERSION, 0, /* Preserve the user version */
139509 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
139510 };
139511
139512 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
139513 assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) );
139514
139515 /* Copy Btree meta values */
139516 for(i=0; i<ArraySize(aCopy); i+=2){
139517 /* GetMeta() and UpdateMeta() cannot fail in this context because
139518 ** we already have page 1 loaded into cache and marked dirty. */
139519 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
139520 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
139521 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
139522 }
139523
139524 if( pOut==0 ){
139525 rc = sqlite3BtreeCopyFile(pMain, pTemp);
139526 }
139527 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139528 rc = sqlite3BtreeCommit(pTemp);
139529 if( rc!=SQLITE_OK ) goto end_of_vacuum;
139530#ifndef SQLITE_OMIT_AUTOVACUUM
139531 if( pOut==0 ){
139532 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
139533 }
139534#endif
139535 }
139536
139537 assert( rc==SQLITE_OK );
139538 if( pOut==0 ){
139539 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
139540 }
139541
139542end_of_vacuum:
139543 /* Restore the original value of db->flags */
139544 db->init.iDb = 0;
139545 db->mDbFlags = saved_mDbFlags;
139546 db->flags = saved_flags;
139547 db->nChange = saved_nChange;
139548 db->nTotalChange = saved_nTotalChange;
139549 db->mTrace = saved_mTrace;
139550 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
139551
139552 /* Currently there is an SQL level transaction open on the vacuum
139553 ** database. No locks are held on any other files (since the main file
139554 ** was committed at the btree level). So it safe to end the transaction
139555 ** by manually setting the autoCommit flag to true and detaching the
139556 ** vacuum database. The vacuum_db journal file is deleted when the pager
139557 ** is closed by the DETACH.
139558 */
139559 db->autoCommit = 1;
139560
139561 if( pDb ){
139562 sqlite3BtreeClose(pDb->pBt);
139563 pDb->pBt = 0;
139564 pDb->pSchema = 0;
139565 }
139566
139567 /* This both clears the schemas and reduces the size of the db->aDb[]
139568 ** array. */
139569 sqlite3ResetAllSchemasOfConnection(db);
139570
139571 return rc;
139572}
139573
139574#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
139575
139576/************** End of vacuum.c **********************************************/
139577/************** Begin file vtab.c ********************************************/
139578/*
139579** 2006 June 10
139580**
139581** The author disclaims copyright to this source code. In place of
139582** a legal notice, here is a blessing:
139583**
139584** May you do good and not evil.
139585** May you find forgiveness for yourself and forgive others.
139586** May you share freely, never taking more than you give.
139587**
139588*************************************************************************
139589** This file contains code used to help implement virtual tables.
139590*/
139591#ifndef SQLITE_OMIT_VIRTUALTABLE
139592/* #include "sqliteInt.h" */
139593
139594/*
139595** Before a virtual table xCreate() or xConnect() method is invoked, the
139596** sqlite3.pVtabCtx member variable is set to point to an instance of
139597** this struct allocated on the stack. It is used by the implementation of
139598** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
139599** are invoked only from within xCreate and xConnect methods.
139600*/
139601struct VtabCtx {
139602 VTable *pVTable; /* The virtual table being constructed */
139603 Table *pTab; /* The Table object to which the virtual table belongs */
139604 VtabCtx *pPrior; /* Parent context (if any) */
139605 int bDeclared; /* True after sqlite3_declare_vtab() is called */
139606};
139607
139608/*
139609** Construct and install a Module object for a virtual table. When this
139610** routine is called, it is guaranteed that all appropriate locks are held
139611** and the module is not already part of the connection.
139612**
139613** If there already exists a module with zName, replace it with the new one.
139614** If pModule==0, then delete the module zName if it exists.
139615*/
139616SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
139617 sqlite3 *db, /* Database in which module is registered */
139618 const char *zName, /* Name assigned to this module */
139619 const sqlite3_module *pModule, /* The definition of the module */
139620 void *pAux, /* Context pointer for xCreate/xConnect */
139621 void (*xDestroy)(void *) /* Module destructor function */
139622){
139623 Module *pMod;
139624 Module *pDel;
139625 char *zCopy;
139626 if( pModule==0 ){
139627 zCopy = (char*)zName;
139628 pMod = 0;
139629 }else{
139630 int nName = sqlite3Strlen30(zName);
139631 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
139632 if( pMod==0 ){
139633 sqlite3OomFault(db);
139634 return 0;
139635 }
139636 zCopy = (char *)(&pMod[1]);
139637 memcpy(zCopy, zName, nName+1);
139638 pMod->zName = zCopy;
139639 pMod->pModule = pModule;
139640 pMod->pAux = pAux;
139641 pMod->xDestroy = xDestroy;
139642 pMod->pEpoTab = 0;
139643 pMod->nRefModule = 1;
139644 }
139645 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
139646 if( pDel ){
139647 if( pDel==pMod ){
139648 sqlite3OomFault(db);
139649 sqlite3DbFree(db, pDel);
139650 pMod = 0;
139651 }else{
139652 sqlite3VtabEponymousTableClear(db, pDel);
139653 sqlite3VtabModuleUnref(db, pDel);
139654 }
139655 }
139656 return pMod;
139657}
139658
139659/*
139660** The actual function that does the work of creating a new module.
139661** This function implements the sqlite3_create_module() and
139662** sqlite3_create_module_v2() interfaces.
139663*/
139664static int createModule(
139665 sqlite3 *db, /* Database in which module is registered */
139666 const char *zName, /* Name assigned to this module */
139667 const sqlite3_module *pModule, /* The definition of the module */
139668 void *pAux, /* Context pointer for xCreate/xConnect */
139669 void (*xDestroy)(void *) /* Module destructor function */
139670){
139671 int rc = SQLITE_OK;
139672
139673 sqlite3_mutex_enter(db->mutex);
139674 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
139675 rc = sqlite3ApiExit(db, rc);
139676 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
139677 sqlite3_mutex_leave(db->mutex);
139678 return rc;
139679}
139680
139681
139682/*
139683** External API function used to create a new virtual-table module.
139684*/
139685SQLITE_API int sqlite3_create_module(
139686 sqlite3 *db, /* Database in which module is registered */
139687 const char *zName, /* Name assigned to this module */
139688 const sqlite3_module *pModule, /* The definition of the module */
139689 void *pAux /* Context pointer for xCreate/xConnect */
139690){
139691#ifdef SQLITE_ENABLE_API_ARMOR
139692 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
139693#endif
139694 return createModule(db, zName, pModule, pAux, 0);
139695}
139696
139697/*
139698** External API function used to create a new virtual-table module.
139699*/
139700SQLITE_API int sqlite3_create_module_v2(
139701 sqlite3 *db, /* Database in which module is registered */
139702 const char *zName, /* Name assigned to this module */
139703 const sqlite3_module *pModule, /* The definition of the module */
139704 void *pAux, /* Context pointer for xCreate/xConnect */
139705 void (*xDestroy)(void *) /* Module destructor function */
139706){
139707#ifdef SQLITE_ENABLE_API_ARMOR
139708 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
139709#endif
139710 return createModule(db, zName, pModule, pAux, xDestroy);
139711}
139712
139713/*
139714** External API to drop all virtual-table modules, except those named
139715** on the azNames list.
139716*/
139717SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
139718 HashElem *pThis, *pNext;
139719#ifdef SQLITE_ENABLE_API_ARMOR
139720 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
139721#endif
139722 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
139723 Module *pMod = (Module*)sqliteHashData(pThis);
139724 pNext = sqliteHashNext(pThis);
139725 if( azNames ){
139726 int ii;
139727 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
139728 if( azNames[ii]!=0 ) continue;
139729 }
139730 createModule(db, pMod->zName, 0, 0, 0);
139731 }
139732 return SQLITE_OK;
139733}
139734
139735/*
139736** Decrement the reference count on a Module object. Destroy the
139737** module when the reference count reaches zero.
139738*/
139739SQLITE_PRIVATE void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
139740 assert( pMod->nRefModule>0 );
139741 pMod->nRefModule--;
139742 if( pMod->nRefModule==0 ){
139743 if( pMod->xDestroy ){
139744 pMod->xDestroy(pMod->pAux);
139745 }
139746 assert( pMod->pEpoTab==0 );
139747 sqlite3DbFree(db, pMod);
139748 }
139749}
139750
139751/*
139752** Lock the virtual table so that it cannot be disconnected.
139753** Locks nest. Every lock should have a corresponding unlock.
139754** If an unlock is omitted, resources leaks will occur.
139755**
139756** If a disconnect is attempted while a virtual table is locked,
139757** the disconnect is deferred until all locks have been removed.
139758*/
139759SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
139760 pVTab->nRef++;
139761}
139762
139763
139764/*
139765** pTab is a pointer to a Table structure representing a virtual-table.
139766** Return a pointer to the VTable object used by connection db to access
139767** this virtual-table, if one has been created, or NULL otherwise.
139768*/
139769SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
139770 VTable *pVtab;
139771 assert( IsVirtual(pTab) );
139772 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
139773 return pVtab;
139774}
139775
139776/*
139777** Decrement the ref-count on a virtual table object. When the ref-count
139778** reaches zero, call the xDisconnect() method to delete the object.
139779*/
139780SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
139781 sqlite3 *db = pVTab->db;
139782
139783 assert( db );
139784 assert( pVTab->nRef>0 );
139785 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
139786
139787 pVTab->nRef--;
139788 if( pVTab->nRef==0 ){
139789 sqlite3_vtab *p = pVTab->pVtab;
139790 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
139791 if( p ){
139792 p->pModule->xDisconnect(p);
139793 }
139794 sqlite3DbFree(db, pVTab);
139795 }
139796}
139797
139798/*
139799** Table p is a virtual table. This function moves all elements in the
139800** p->pVTable list to the sqlite3.pDisconnect lists of their associated
139801** database connections to be disconnected at the next opportunity.
139802** Except, if argument db is not NULL, then the entry associated with
139803** connection db is left in the p->pVTable list.
139804*/
139805static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
139806 VTable *pRet = 0;
139807 VTable *pVTable = p->pVTable;
139808 p->pVTable = 0;
139809
139810 /* Assert that the mutex (if any) associated with the BtShared database
139811 ** that contains table p is held by the caller. See header comments
139812 ** above function sqlite3VtabUnlockList() for an explanation of why
139813 ** this makes it safe to access the sqlite3.pDisconnect list of any
139814 ** database connection that may have an entry in the p->pVTable list.
139815 */
139816 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
139817
139818 while( pVTable ){
139819 sqlite3 *db2 = pVTable->db;
139820 VTable *pNext = pVTable->pNext;
139821 assert( db2 );
139822 if( db2==db ){
139823 pRet = pVTable;
139824 p->pVTable = pRet;
139825 pRet->pNext = 0;
139826 }else{
139827 pVTable->pNext = db2->pDisconnect;
139828 db2->pDisconnect = pVTable;
139829 }
139830 pVTable = pNext;
139831 }
139832
139833 assert( !db || pRet );
139834 return pRet;
139835}
139836
139837/*
139838** Table *p is a virtual table. This function removes the VTable object
139839** for table *p associated with database connection db from the linked
139840** list in p->pVTab. It also decrements the VTable ref count. This is
139841** used when closing database connection db to free all of its VTable
139842** objects without disturbing the rest of the Schema object (which may
139843** be being used by other shared-cache connections).
139844*/
139845SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
139846 VTable **ppVTab;
139847
139848 assert( IsVirtual(p) );
139849 assert( sqlite3BtreeHoldsAllMutexes(db) );
139850 assert( sqlite3_mutex_held(db->mutex) );
139851
139852 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
139853 if( (*ppVTab)->db==db ){
139854 VTable *pVTab = *ppVTab;
139855 *ppVTab = pVTab->pNext;
139856 sqlite3VtabUnlock(pVTab);
139857 break;
139858 }
139859 }
139860}
139861
139862
139863/*
139864** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
139865**
139866** This function may only be called when the mutexes associated with all
139867** shared b-tree databases opened using connection db are held by the
139868** caller. This is done to protect the sqlite3.pDisconnect list. The
139869** sqlite3.pDisconnect list is accessed only as follows:
139870**
139871** 1) By this function. In this case, all BtShared mutexes and the mutex
139872** associated with the database handle itself must be held.
139873**
139874** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
139875** the sqlite3.pDisconnect list. In this case either the BtShared mutex
139876** associated with the database the virtual table is stored in is held
139877** or, if the virtual table is stored in a non-sharable database, then
139878** the database handle mutex is held.
139879**
139880** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
139881** by multiple threads. It is thread-safe.
139882*/
139883SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
139884 VTable *p = db->pDisconnect;
139885
139886 assert( sqlite3BtreeHoldsAllMutexes(db) );
139887 assert( sqlite3_mutex_held(db->mutex) );
139888
139889 if( p ){
139890 db->pDisconnect = 0;
139891 sqlite3ExpirePreparedStatements(db, 0);
139892 do {
139893 VTable *pNext = p->pNext;
139894 sqlite3VtabUnlock(p);
139895 p = pNext;
139896 }while( p );
139897 }
139898}
139899
139900/*
139901** Clear any and all virtual-table information from the Table record.
139902** This routine is called, for example, just before deleting the Table
139903** record.
139904**
139905** Since it is a virtual-table, the Table structure contains a pointer
139906** to the head of a linked list of VTable structures. Each VTable
139907** structure is associated with a single sqlite3* user of the schema.
139908** The reference count of the VTable structure associated with database
139909** connection db is decremented immediately (which may lead to the
139910** structure being xDisconnected and free). Any other VTable structures
139911** in the list are moved to the sqlite3.pDisconnect list of the associated
139912** database connection.
139913*/
139914SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
139915 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
139916 if( p->azModuleArg ){
139917 int i;
139918 for(i=0; i<p->nModuleArg; i++){
139919 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
139920 }
139921 sqlite3DbFree(db, p->azModuleArg);
139922 }
139923}
139924
139925/*
139926** Add a new module argument to pTable->azModuleArg[].
139927** The string is not copied - the pointer is stored. The
139928** string will be freed automatically when the table is
139929** deleted.
139930*/
139931static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
139932 sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
139933 char **azModuleArg;
139934 sqlite3 *db = pParse->db;
139935 if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
139936 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
139937 }
139938 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
139939 if( azModuleArg==0 ){
139940 sqlite3DbFree(db, zArg);
139941 }else{
139942 int i = pTable->nModuleArg++;
139943 azModuleArg[i] = zArg;
139944 azModuleArg[i+1] = 0;
139945 pTable->azModuleArg = azModuleArg;
139946 }
139947}
139948
139949/*
139950** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
139951** statement. The module name has been parsed, but the optional list
139952** of parameters that follow the module name are still pending.
139953*/
139954SQLITE_PRIVATE void sqlite3VtabBeginParse(
139955 Parse *pParse, /* Parsing context */
139956 Token *pName1, /* Name of new table, or database name */
139957 Token *pName2, /* Name of new table or NULL */
139958 Token *pModuleName, /* Name of the module for the virtual table */
139959 int ifNotExists /* No error if the table already exists */
139960){
139961 Table *pTable; /* The new virtual table */
139962 sqlite3 *db; /* Database connection */
139963
139964 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
139965 pTable = pParse->pNewTable;
139966 if( pTable==0 ) return;
139967 assert( 0==pTable->pIndex );
139968
139969 db = pParse->db;
139970
139971 assert( pTable->nModuleArg==0 );
139972 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
139973 addModuleArgument(pParse, pTable, 0);
139974 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
139975 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
139976 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
139977 );
139978 pParse->sNameToken.n = (int)(
139979 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
139980 );
139981
139982#ifndef SQLITE_OMIT_AUTHORIZATION
139983 /* Creating a virtual table invokes the authorization callback twice.
139984 ** The first invocation, to obtain permission to INSERT a row into the
139985 ** sqlite_schema table, has already been made by sqlite3StartTable().
139986 ** The second call, to obtain permission to create the table, is made now.
139987 */
139988 if( pTable->azModuleArg ){
139989 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
139990 assert( iDb>=0 ); /* The database the table is being created in */
139991 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
139992 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
139993 }
139994#endif
139995}
139996
139997/*
139998** This routine takes the module argument that has been accumulating
139999** in pParse->zArg[] and appends it to the list of arguments on the
140000** virtual table currently under construction in pParse->pTable.
140001*/
140002static void addArgumentToVtab(Parse *pParse){
140003 if( pParse->sArg.z && pParse->pNewTable ){
140004 const char *z = (const char*)pParse->sArg.z;
140005 int n = pParse->sArg.n;
140006 sqlite3 *db = pParse->db;
140007 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
140008 }
140009}
140010
140011/*
140012** The parser calls this routine after the CREATE VIRTUAL TABLE statement
140013** has been completely parsed.
140014*/
140015SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
140016 Table *pTab = pParse->pNewTable; /* The table being constructed */
140017 sqlite3 *db = pParse->db; /* The database connection */
140018
140019 if( pTab==0 ) return;
140020 addArgumentToVtab(pParse);
140021 pParse->sArg.z = 0;
140022 if( pTab->nModuleArg<1 ) return;
140023
140024 /* If the CREATE VIRTUAL TABLE statement is being entered for the
140025 ** first time (in other words if the virtual table is actually being
140026 ** created now instead of just being read out of sqlite_schema) then
140027 ** do additional initialization work and store the statement text
140028 ** in the sqlite_schema table.
140029 */
140030 if( !db->init.busy ){
140031 char *zStmt;
140032 char *zWhere;
140033 int iDb;
140034 int iReg;
140035 Vdbe *v;
140036
140037 sqlite3MayAbort(pParse);
140038
140039 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
140040 if( pEnd ){
140041 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
140042 }
140043 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
140044
140045 /* A slot for the record has already been allocated in the
140046 ** schema table. We just need to update that slot with all
140047 ** the information we've collected.
140048 **
140049 ** The VM register number pParse->regRowid holds the rowid of an
140050 ** entry in the sqlite_schema table tht was created for this vtab
140051 ** by sqlite3StartTable().
140052 */
140053 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140054 sqlite3NestedParse(pParse,
140055 "UPDATE %Q." DFLT_SCHEMA_TABLE " "
140056 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
140057 "WHERE rowid=#%d",
140058 db->aDb[iDb].zDbSName,
140059 pTab->zName,
140060 pTab->zName,
140061 zStmt,
140062 pParse->regRowid
140063 );
140064 v = sqlite3GetVdbe(pParse);
140065 sqlite3ChangeCookie(pParse, iDb);
140066
140067 sqlite3VdbeAddOp0(v, OP_Expire);
140068 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
140069 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
140070 sqlite3DbFree(db, zStmt);
140071
140072 iReg = ++pParse->nMem;
140073 sqlite3VdbeLoadString(v, iReg, pTab->zName);
140074 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
140075 }
140076
140077 /* If we are rereading the sqlite_schema table create the in-memory
140078 ** record of the table. The xConnect() method is not called until
140079 ** the first time the virtual table is used in an SQL statement. This
140080 ** allows a schema that contains virtual tables to be loaded before
140081 ** the required virtual table implementations are registered. */
140082 else {
140083 Table *pOld;
140084 Schema *pSchema = pTab->pSchema;
140085 const char *zName = pTab->zName;
140086 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
140087 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
140088 if( pOld ){
140089 sqlite3OomFault(db);
140090 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
140091 return;
140092 }
140093 pParse->pNewTable = 0;
140094 }
140095}
140096
140097/*
140098** The parser calls this routine when it sees the first token
140099** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
140100*/
140101SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
140102 addArgumentToVtab(pParse);
140103 pParse->sArg.z = 0;
140104 pParse->sArg.n = 0;
140105}
140106
140107/*
140108** The parser calls this routine for each token after the first token
140109** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
140110*/
140111SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
140112 Token *pArg = &pParse->sArg;
140113 if( pArg->z==0 ){
140114 pArg->z = p->z;
140115 pArg->n = p->n;
140116 }else{
140117 assert(pArg->z <= p->z);
140118 pArg->n = (int)(&p->z[p->n] - pArg->z);
140119 }
140120}
140121
140122/*
140123** Invoke a virtual table constructor (either xCreate or xConnect). The
140124** pointer to the function to invoke is passed as the fourth parameter
140125** to this procedure.
140126*/
140127static int vtabCallConstructor(
140128 sqlite3 *db,
140129 Table *pTab,
140130 Module *pMod,
140131 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
140132 char **pzErr
140133){
140134 VtabCtx sCtx;
140135 VTable *pVTable;
140136 int rc;
140137 const char *const*azArg = (const char *const*)pTab->azModuleArg;
140138 int nArg = pTab->nModuleArg;
140139 char *zErr = 0;
140140 char *zModuleName;
140141 int iDb;
140142 VtabCtx *pCtx;
140143
140144 /* Check that the virtual-table is not already being initialized */
140145 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
140146 if( pCtx->pTab==pTab ){
140147 *pzErr = sqlite3MPrintf(db,
140148 "vtable constructor called recursively: %s", pTab->zName
140149 );
140150 return SQLITE_LOCKED;
140151 }
140152 }
140153
140154 zModuleName = sqlite3DbStrDup(db, pTab->zName);
140155 if( !zModuleName ){
140156 return SQLITE_NOMEM_BKPT;
140157 }
140158
140159 pVTable = sqlite3MallocZero(sizeof(VTable));
140160 if( !pVTable ){
140161 sqlite3OomFault(db);
140162 sqlite3DbFree(db, zModuleName);
140163 return SQLITE_NOMEM_BKPT;
140164 }
140165 pVTable->db = db;
140166 pVTable->pMod = pMod;
140167 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
140168
140169 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140170 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
140171
140172 /* Invoke the virtual table constructor */
140173 assert( &db->pVtabCtx );
140174 assert( xConstruct );
140175 sCtx.pTab = pTab;
140176 sCtx.pVTable = pVTable;
140177 sCtx.pPrior = db->pVtabCtx;
140178 sCtx.bDeclared = 0;
140179 db->pVtabCtx = &sCtx;
140180 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
140181 db->pVtabCtx = sCtx.pPrior;
140182 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
140183 assert( sCtx.pTab==pTab );
140184
140185 if( SQLITE_OK!=rc ){
140186 if( zErr==0 ){
140187 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
140188 }else {
140189 *pzErr = sqlite3MPrintf(db, "%s", zErr);
140190 sqlite3_free(zErr);
140191 }
140192 sqlite3DbFree(db, pVTable);
140193 }else if( ALWAYS(pVTable->pVtab) ){
140194 /* Justification of ALWAYS(): A correct vtab constructor must allocate
140195 ** the sqlite3_vtab object if successful. */
140196 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
140197 pVTable->pVtab->pModule = pMod->pModule;
140198 pMod->nRefModule++;
140199 pVTable->nRef = 1;
140200 if( sCtx.bDeclared==0 ){
140201 const char *zFormat = "vtable constructor did not declare schema: %s";
140202 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
140203 sqlite3VtabUnlock(pVTable);
140204 rc = SQLITE_ERROR;
140205 }else{
140206 int iCol;
140207 u16 oooHidden = 0;
140208 /* If everything went according to plan, link the new VTable structure
140209 ** into the linked list headed by pTab->pVTable. Then loop through the
140210 ** columns of the table to see if any of them contain the token "hidden".
140211 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
140212 ** the type string. */
140213 pVTable->pNext = pTab->pVTable;
140214 pTab->pVTable = pVTable;
140215
140216 for(iCol=0; iCol<pTab->nCol; iCol++){
140217 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
140218 int nType;
140219 int i = 0;
140220 nType = sqlite3Strlen30(zType);
140221 for(i=0; i<nType; i++){
140222 if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
140223 && (i==0 || zType[i-1]==' ')
140224 && (zType[i+6]=='\0' || zType[i+6]==' ')
140225 ){
140226 break;
140227 }
140228 }
140229 if( i<nType ){
140230 int j;
140231 int nDel = 6 + (zType[i+6] ? 1 : 0);
140232 for(j=i; (j+nDel)<=nType; j++){
140233 zType[j] = zType[j+nDel];
140234 }
140235 if( zType[i]=='\0' && i>0 ){
140236 assert(zType[i-1]==' ');
140237 zType[i-1] = '\0';
140238 }
140239 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
140240 oooHidden = TF_OOOHidden;
140241 }else{
140242 pTab->tabFlags |= oooHidden;
140243 }
140244 }
140245 }
140246 }
140247
140248 sqlite3DbFree(db, zModuleName);
140249 return rc;
140250}
140251
140252/*
140253** This function is invoked by the parser to call the xConnect() method
140254** of the virtual table pTab. If an error occurs, an error code is returned
140255** and an error left in pParse.
140256**
140257** This call is a no-op if table pTab is not a virtual table.
140258*/
140259SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
140260 sqlite3 *db = pParse->db;
140261 const char *zMod;
140262 Module *pMod;
140263 int rc;
140264
140265 assert( pTab );
140266 if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
140267 return SQLITE_OK;
140268 }
140269
140270 /* Locate the required virtual table module */
140271 zMod = pTab->azModuleArg[0];
140272 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
140273
140274 if( !pMod ){
140275 const char *zModule = pTab->azModuleArg[0];
140276 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
140277 rc = SQLITE_ERROR;
140278 }else{
140279 char *zErr = 0;
140280 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
140281 if( rc!=SQLITE_OK ){
140282 sqlite3ErrorMsg(pParse, "%s", zErr);
140283 pParse->rc = rc;
140284 }
140285 sqlite3DbFree(db, zErr);
140286 }
140287
140288 return rc;
140289}
140290/*
140291** Grow the db->aVTrans[] array so that there is room for at least one
140292** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
140293*/
140294static int growVTrans(sqlite3 *db){
140295 const int ARRAY_INCR = 5;
140296
140297 /* Grow the sqlite3.aVTrans array if required */
140298 if( (db->nVTrans%ARRAY_INCR)==0 ){
140299 VTable **aVTrans;
140300 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
140301 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
140302 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
140303 if( !aVTrans ){
140304 return SQLITE_NOMEM_BKPT;
140305 }
140306 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
140307 db->aVTrans = aVTrans;
140308 }
140309
140310 return SQLITE_OK;
140311}
140312
140313/*
140314** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
140315** have already been reserved using growVTrans().
140316*/
140317static void addToVTrans(sqlite3 *db, VTable *pVTab){
140318 /* Add pVtab to the end of sqlite3.aVTrans */
140319 db->aVTrans[db->nVTrans++] = pVTab;
140320 sqlite3VtabLock(pVTab);
140321}
140322
140323/*
140324** This function is invoked by the vdbe to call the xCreate method
140325** of the virtual table named zTab in database iDb.
140326**
140327** If an error occurs, *pzErr is set to point to an English language
140328** description of the error and an SQLITE_XXX error code is returned.
140329** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
140330*/
140331SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
140332 int rc = SQLITE_OK;
140333 Table *pTab;
140334 Module *pMod;
140335 const char *zMod;
140336
140337 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
140338 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
140339
140340 /* Locate the required virtual table module */
140341 zMod = pTab->azModuleArg[0];
140342 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
140343
140344 /* If the module has been registered and includes a Create method,
140345 ** invoke it now. If the module has not been registered, return an
140346 ** error. Otherwise, do nothing.
140347 */
140348 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
140349 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
140350 rc = SQLITE_ERROR;
140351 }else{
140352 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
140353 }
140354
140355 /* Justification of ALWAYS(): The xConstructor method is required to
140356 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
140357 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
140358 rc = growVTrans(db);
140359 if( rc==SQLITE_OK ){
140360 addToVTrans(db, sqlite3GetVTable(db, pTab));
140361 }
140362 }
140363
140364 return rc;
140365}
140366
140367/*
140368** This function is used to set the schema of a virtual table. It is only
140369** valid to call this function from within the xCreate() or xConnect() of a
140370** virtual table module.
140371*/
140372SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
140373 VtabCtx *pCtx;
140374 int rc = SQLITE_OK;
140375 Table *pTab;
140376 char *zErr = 0;
140377 Parse sParse;
140378
140379#ifdef SQLITE_ENABLE_API_ARMOR
140380 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
140381 return SQLITE_MISUSE_BKPT;
140382 }
140383#endif
140384 sqlite3_mutex_enter(db->mutex);
140385 pCtx = db->pVtabCtx;
140386 if( !pCtx || pCtx->bDeclared ){
140387 sqlite3Error(db, SQLITE_MISUSE);
140388 sqlite3_mutex_leave(db->mutex);
140389 return SQLITE_MISUSE_BKPT;
140390 }
140391 pTab = pCtx->pTab;
140392 assert( IsVirtual(pTab) );
140393
140394 memset(&sParse, 0, sizeof(sParse));
140395 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
140396 sParse.db = db;
140397 sParse.nQueryLoop = 1;
140398 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
140399 && sParse.pNewTable
140400 && !db->mallocFailed
140401 && !sParse.pNewTable->pSelect
140402 && !IsVirtual(sParse.pNewTable)
140403 ){
140404 if( !pTab->aCol ){
140405 Table *pNew = sParse.pNewTable;
140406 Index *pIdx;
140407 pTab->aCol = pNew->aCol;
140408 pTab->nCol = pNew->nCol;
140409 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
140410 pNew->nCol = 0;
140411 pNew->aCol = 0;
140412 assert( pTab->pIndex==0 );
140413 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
140414 if( !HasRowid(pNew)
140415 && pCtx->pVTable->pMod->pModule->xUpdate!=0
140416 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
140417 ){
140418 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
140419 ** or else must have a single-column PRIMARY KEY */
140420 rc = SQLITE_ERROR;
140421 }
140422 pIdx = pNew->pIndex;
140423 if( pIdx ){
140424 assert( pIdx->pNext==0 );
140425 pTab->pIndex = pIdx;
140426 pNew->pIndex = 0;
140427 pIdx->pTable = pTab;
140428 }
140429 }
140430 pCtx->bDeclared = 1;
140431 }else{
140432 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
140433 sqlite3DbFree(db, zErr);
140434 rc = SQLITE_ERROR;
140435 }
140436 sParse.eParseMode = PARSE_MODE_NORMAL;
140437
140438 if( sParse.pVdbe ){
140439 sqlite3VdbeFinalize(sParse.pVdbe);
140440 }
140441 sqlite3DeleteTable(db, sParse.pNewTable);
140442 sqlite3ParserReset(&sParse);
140443
140444 assert( (rc&0xff)==rc );
140445 rc = sqlite3ApiExit(db, rc);
140446 sqlite3_mutex_leave(db->mutex);
140447 return rc;
140448}
140449
140450/*
140451** This function is invoked by the vdbe to call the xDestroy method
140452** of the virtual table named zTab in database iDb. This occurs
140453** when a DROP TABLE is mentioned.
140454**
140455** This call is a no-op if zTab is not a virtual table.
140456*/
140457SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
140458 int rc = SQLITE_OK;
140459 Table *pTab;
140460
140461 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
140462 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
140463 VTable *p;
140464 int (*xDestroy)(sqlite3_vtab *);
140465 for(p=pTab->pVTable; p; p=p->pNext){
140466 assert( p->pVtab );
140467 if( p->pVtab->nRef>0 ){
140468 return SQLITE_LOCKED;
140469 }
140470 }
140471 p = vtabDisconnectAll(db, pTab);
140472 xDestroy = p->pMod->pModule->xDestroy;
140473 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
140474 assert( xDestroy!=0 );
140475 pTab->nTabRef++;
140476 rc = xDestroy(p->pVtab);
140477 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
140478 if( rc==SQLITE_OK ){
140479 assert( pTab->pVTable==p && p->pNext==0 );
140480 p->pVtab = 0;
140481 pTab->pVTable = 0;
140482 sqlite3VtabUnlock(p);
140483 }
140484 sqlite3DeleteTable(db, pTab);
140485 }
140486
140487 return rc;
140488}
140489
140490/*
140491** This function invokes either the xRollback or xCommit method
140492** of each of the virtual tables in the sqlite3.aVTrans array. The method
140493** called is identified by the second argument, "offset", which is
140494** the offset of the method to call in the sqlite3_module structure.
140495**
140496** The array is cleared after invoking the callbacks.
140497*/
140498static void callFinaliser(sqlite3 *db, int offset){
140499 int i;
140500 if( db->aVTrans ){
140501 VTable **aVTrans = db->aVTrans;
140502 db->aVTrans = 0;
140503 for(i=0; i<db->nVTrans; i++){
140504 VTable *pVTab = aVTrans[i];
140505 sqlite3_vtab *p = pVTab->pVtab;
140506 if( p ){
140507 int (*x)(sqlite3_vtab *);
140508 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
140509 if( x ) x(p);
140510 }
140511 pVTab->iSavepoint = 0;
140512 sqlite3VtabUnlock(pVTab);
140513 }
140514 sqlite3DbFree(db, aVTrans);
140515 db->nVTrans = 0;
140516 }
140517}
140518
140519/*
140520** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
140521** array. Return the error code for the first error that occurs, or
140522** SQLITE_OK if all xSync operations are successful.
140523**
140524** If an error message is available, leave it in p->zErrMsg.
140525*/
140526SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
140527 int i;
140528 int rc = SQLITE_OK;
140529 VTable **aVTrans = db->aVTrans;
140530
140531 db->aVTrans = 0;
140532 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
140533 int (*x)(sqlite3_vtab *);
140534 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
140535 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
140536 rc = x(pVtab);
140537 sqlite3VtabImportErrmsg(p, pVtab);
140538 }
140539 }
140540 db->aVTrans = aVTrans;
140541 return rc;
140542}
140543
140544/*
140545** Invoke the xRollback method of all virtual tables in the
140546** sqlite3.aVTrans array. Then clear the array itself.
140547*/
140548SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
140549 callFinaliser(db, offsetof(sqlite3_module,xRollback));
140550 return SQLITE_OK;
140551}
140552
140553/*
140554** Invoke the xCommit method of all virtual tables in the
140555** sqlite3.aVTrans array. Then clear the array itself.
140556*/
140557SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
140558 callFinaliser(db, offsetof(sqlite3_module,xCommit));
140559 return SQLITE_OK;
140560}
140561
140562/*
140563** If the virtual table pVtab supports the transaction interface
140564** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
140565** not currently open, invoke the xBegin method now.
140566**
140567** If the xBegin call is successful, place the sqlite3_vtab pointer
140568** in the sqlite3.aVTrans array.
140569*/
140570SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
140571 int rc = SQLITE_OK;
140572 const sqlite3_module *pModule;
140573
140574 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
140575 ** than zero, then this function is being called from within a
140576 ** virtual module xSync() callback. It is illegal to write to
140577 ** virtual module tables in this case, so return SQLITE_LOCKED.
140578 */
140579 if( sqlite3VtabInSync(db) ){
140580 return SQLITE_LOCKED;
140581 }
140582 if( !pVTab ){
140583 return SQLITE_OK;
140584 }
140585 pModule = pVTab->pVtab->pModule;
140586
140587 if( pModule->xBegin ){
140588 int i;
140589
140590 /* If pVtab is already in the aVTrans array, return early */
140591 for(i=0; i<db->nVTrans; i++){
140592 if( db->aVTrans[i]==pVTab ){
140593 return SQLITE_OK;
140594 }
140595 }
140596
140597 /* Invoke the xBegin method. If successful, add the vtab to the
140598 ** sqlite3.aVTrans[] array. */
140599 rc = growVTrans(db);
140600 if( rc==SQLITE_OK ){
140601 rc = pModule->xBegin(pVTab->pVtab);
140602 if( rc==SQLITE_OK ){
140603 int iSvpt = db->nStatement + db->nSavepoint;
140604 addToVTrans(db, pVTab);
140605 if( iSvpt && pModule->xSavepoint ){
140606 pVTab->iSavepoint = iSvpt;
140607 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
140608 }
140609 }
140610 }
140611 }
140612 return rc;
140613}
140614
140615/*
140616** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
140617** virtual tables that currently have an open transaction. Pass iSavepoint
140618** as the second argument to the virtual table method invoked.
140619**
140620** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
140621** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
140622** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
140623** an open transaction is invoked.
140624**
140625** If any virtual table method returns an error code other than SQLITE_OK,
140626** processing is abandoned and the error returned to the caller of this
140627** function immediately. If all calls to virtual table methods are successful,
140628** SQLITE_OK is returned.
140629*/
140630SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
140631 int rc = SQLITE_OK;
140632
140633 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
140634 assert( iSavepoint>=-1 );
140635 if( db->aVTrans ){
140636 int i;
140637 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
140638 VTable *pVTab = db->aVTrans[i];
140639 const sqlite3_module *pMod = pVTab->pMod->pModule;
140640 if( pVTab->pVtab && pMod->iVersion>=2 ){
140641 int (*xMethod)(sqlite3_vtab *, int);
140642 sqlite3VtabLock(pVTab);
140643 switch( op ){
140644 case SAVEPOINT_BEGIN:
140645 xMethod = pMod->xSavepoint;
140646 pVTab->iSavepoint = iSavepoint+1;
140647 break;
140648 case SAVEPOINT_ROLLBACK:
140649 xMethod = pMod->xRollbackTo;
140650 break;
140651 default:
140652 xMethod = pMod->xRelease;
140653 break;
140654 }
140655 if( xMethod && pVTab->iSavepoint>iSavepoint ){
140656 rc = xMethod(pVTab->pVtab, iSavepoint);
140657 }
140658 sqlite3VtabUnlock(pVTab);
140659 }
140660 }
140661 }
140662 return rc;
140663}
140664
140665/*
140666** The first parameter (pDef) is a function implementation. The
140667** second parameter (pExpr) is the first argument to this function.
140668** If pExpr is a column in a virtual table, then let the virtual
140669** table implementation have an opportunity to overload the function.
140670**
140671** This routine is used to allow virtual table implementations to
140672** overload MATCH, LIKE, GLOB, and REGEXP operators.
140673**
140674** Return either the pDef argument (indicating no change) or a
140675** new FuncDef structure that is marked as ephemeral using the
140676** SQLITE_FUNC_EPHEM flag.
140677*/
140678SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
140679 sqlite3 *db, /* Database connection for reporting malloc problems */
140680 FuncDef *pDef, /* Function to possibly overload */
140681 int nArg, /* Number of arguments to the function */
140682 Expr *pExpr /* First argument to the function */
140683){
140684 Table *pTab;
140685 sqlite3_vtab *pVtab;
140686 sqlite3_module *pMod;
140687 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
140688 void *pArg = 0;
140689 FuncDef *pNew;
140690 int rc = 0;
140691
140692 /* Check to see the left operand is a column in a virtual table */
140693 if( NEVER(pExpr==0) ) return pDef;
140694 if( pExpr->op!=TK_COLUMN ) return pDef;
140695 pTab = pExpr->y.pTab;
140696 if( pTab==0 ) return pDef;
140697 if( !IsVirtual(pTab) ) return pDef;
140698 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
140699 assert( pVtab!=0 );
140700 assert( pVtab->pModule!=0 );
140701 pMod = (sqlite3_module *)pVtab->pModule;
140702 if( pMod->xFindFunction==0 ) return pDef;
140703
140704 /* Call the xFindFunction method on the virtual table implementation
140705 ** to see if the implementation wants to overload this function.
140706 **
140707 ** Though undocumented, we have historically always invoked xFindFunction
140708 ** with an all lower-case function name. Continue in this tradition to
140709 ** avoid any chance of an incompatibility.
140710 */
140711#ifdef SQLITE_DEBUG
140712 {
140713 int i;
140714 for(i=0; pDef->zName[i]; i++){
140715 unsigned char x = (unsigned char)pDef->zName[i];
140716 assert( x==sqlite3UpperToLower[x] );
140717 }
140718 }
140719#endif
140720 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
140721 if( rc==0 ){
140722 return pDef;
140723 }
140724
140725 /* Create a new ephemeral function definition for the overloaded
140726 ** function */
140727 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
140728 + sqlite3Strlen30(pDef->zName) + 1);
140729 if( pNew==0 ){
140730 return pDef;
140731 }
140732 *pNew = *pDef;
140733 pNew->zName = (const char*)&pNew[1];
140734 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
140735 pNew->xSFunc = xSFunc;
140736 pNew->pUserData = pArg;
140737 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
140738 return pNew;
140739}
140740
140741/*
140742** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
140743** array so that an OP_VBegin will get generated for it. Add pTab to the
140744** array if it is missing. If pTab is already in the array, this routine
140745** is a no-op.
140746*/
140747SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
140748 Parse *pToplevel = sqlite3ParseToplevel(pParse);
140749 int i, n;
140750 Table **apVtabLock;
140751
140752 assert( IsVirtual(pTab) );
140753 for(i=0; i<pToplevel->nVtabLock; i++){
140754 if( pTab==pToplevel->apVtabLock[i] ) return;
140755 }
140756 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
140757 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
140758 if( apVtabLock ){
140759 pToplevel->apVtabLock = apVtabLock;
140760 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
140761 }else{
140762 sqlite3OomFault(pToplevel->db);
140763 }
140764}
140765
140766/*
140767** Check to see if virtual table module pMod can be have an eponymous
140768** virtual table instance. If it can, create one if one does not already
140769** exist. Return non-zero if the eponymous virtual table instance exists
140770** when this routine returns, and return zero if it does not exist.
140771**
140772** An eponymous virtual table instance is one that is named after its
140773** module, and more importantly, does not require a CREATE VIRTUAL TABLE
140774** statement in order to come into existance. Eponymous virtual table
140775** instances always exist. They cannot be DROP-ed.
140776**
140777** Any virtual table module for which xConnect and xCreate are the same
140778** method can have an eponymous virtual table instance.
140779*/
140780SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
140781 const sqlite3_module *pModule = pMod->pModule;
140782 Table *pTab;
140783 char *zErr = 0;
140784 int rc;
140785 sqlite3 *db = pParse->db;
140786 if( pMod->pEpoTab ) return 1;
140787 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
140788 pTab = sqlite3DbMallocZero(db, sizeof(Table));
140789 if( pTab==0 ) return 0;
140790 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
140791 if( pTab->zName==0 ){
140792 sqlite3DbFree(db, pTab);
140793 return 0;
140794 }
140795 pMod->pEpoTab = pTab;
140796 pTab->nTabRef = 1;
140797 pTab->pSchema = db->aDb[0].pSchema;
140798 assert( pTab->nModuleArg==0 );
140799 pTab->iPKey = -1;
140800 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
140801 addModuleArgument(pParse, pTab, 0);
140802 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
140803 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
140804 if( rc ){
140805 sqlite3ErrorMsg(pParse, "%s", zErr);
140806 sqlite3DbFree(db, zErr);
140807 sqlite3VtabEponymousTableClear(db, pMod);
140808 return 0;
140809 }
140810 return 1;
140811}
140812
140813/*
140814** Erase the eponymous virtual table instance associated with
140815** virtual table module pMod, if it exists.
140816*/
140817SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
140818 Table *pTab = pMod->pEpoTab;
140819 if( pTab!=0 ){
140820 /* Mark the table as Ephemeral prior to deleting it, so that the
140821 ** sqlite3DeleteTable() routine will know that it is not stored in
140822 ** the schema. */
140823 pTab->tabFlags |= TF_Ephemeral;
140824 sqlite3DeleteTable(db, pTab);
140825 pMod->pEpoTab = 0;
140826 }
140827}
140828
140829/*
140830** Return the ON CONFLICT resolution mode in effect for the virtual
140831** table update operation currently in progress.
140832**
140833** The results of this routine are undefined unless it is called from
140834** within an xUpdate method.
140835*/
140836SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
140837 static const unsigned char aMap[] = {
140838 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
140839 };
140840#ifdef SQLITE_ENABLE_API_ARMOR
140841 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
140842#endif
140843 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
140844 assert( OE_Ignore==4 && OE_Replace==5 );
140845 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
140846 return (int)aMap[db->vtabOnConflict-1];
140847}
140848
140849/*
140850** Call from within the xCreate() or xConnect() methods to provide
140851** the SQLite core with additional information about the behavior
140852** of the virtual table being implemented.
140853*/
140854SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
140855 va_list ap;
140856 int rc = SQLITE_OK;
140857 VtabCtx *p;
140858
140859#ifdef SQLITE_ENABLE_API_ARMOR
140860 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
140861#endif
140862 sqlite3_mutex_enter(db->mutex);
140863 p = db->pVtabCtx;
140864 if( !p ){
140865 rc = SQLITE_MISUSE_BKPT;
140866 }else{
140867 assert( p->pTab==0 || IsVirtual(p->pTab) );
140868 va_start(ap, op);
140869 switch( op ){
140870 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
140871 p->pVTable->bConstraint = (u8)va_arg(ap, int);
140872 break;
140873 }
140874 case SQLITE_VTAB_INNOCUOUS: {
140875 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
140876 break;
140877 }
140878 case SQLITE_VTAB_DIRECTONLY: {
140879 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
140880 break;
140881 }
140882 default: {
140883 rc = SQLITE_MISUSE_BKPT;
140884 break;
140885 }
140886 }
140887 va_end(ap);
140888 }
140889
140890 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
140891 sqlite3_mutex_leave(db->mutex);
140892 return rc;
140893}
140894
140895#endif /* SQLITE_OMIT_VIRTUALTABLE */
140896
140897/************** End of vtab.c ************************************************/
140898/************** Begin file wherecode.c ***************************************/
140899/*
140900** 2015-06-06
140901**
140902** The author disclaims copyright to this source code. In place of
140903** a legal notice, here is a blessing:
140904**
140905** May you do good and not evil.
140906** May you find forgiveness for yourself and forgive others.
140907** May you share freely, never taking more than you give.
140908**
140909*************************************************************************
140910** This module contains C code that generates VDBE code used to process
140911** the WHERE clause of SQL statements.
140912**
140913** This file was split off from where.c on 2015-06-06 in order to reduce the
140914** size of where.c and make it easier to edit. This file contains the routines
140915** that actually generate the bulk of the WHERE loop code. The original where.c
140916** file retains the code that does query planning and analysis.
140917*/
140918/* #include "sqliteInt.h" */
140919/************** Include whereInt.h in the middle of wherecode.c **************/
140920/************** Begin file whereInt.h ****************************************/
140921/*
140922** 2013-11-12
140923**
140924** The author disclaims copyright to this source code. In place of
140925** a legal notice, here is a blessing:
140926**
140927** May you do good and not evil.
140928** May you find forgiveness for yourself and forgive others.
140929** May you share freely, never taking more than you give.
140930**
140931*************************************************************************
140932**
140933** This file contains structure and macro definitions for the query
140934** planner logic in "where.c". These definitions are broken out into
140935** a separate source file for easier editing.
140936*/
140937#ifndef SQLITE_WHEREINT_H
140938#define SQLITE_WHEREINT_H
140939
140940/*
140941** Trace output macros
140942*/
140943#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
140944/***/ extern int sqlite3WhereTrace;
140945#endif
140946#if defined(SQLITE_DEBUG) \
140947 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
140948# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
140949# define WHERETRACE_ENABLED 1
140950#else
140951# define WHERETRACE(K,X)
140952#endif
140953
140954/* Forward references
140955*/
140956typedef struct WhereClause WhereClause;
140957typedef struct WhereMaskSet WhereMaskSet;
140958typedef struct WhereOrInfo WhereOrInfo;
140959typedef struct WhereAndInfo WhereAndInfo;
140960typedef struct WhereLevel WhereLevel;
140961typedef struct WhereLoop WhereLoop;
140962typedef struct WherePath WherePath;
140963typedef struct WhereTerm WhereTerm;
140964typedef struct WhereLoopBuilder WhereLoopBuilder;
140965typedef struct WhereScan WhereScan;
140966typedef struct WhereOrCost WhereOrCost;
140967typedef struct WhereOrSet WhereOrSet;
140968
140969/*
140970** This object contains information needed to implement a single nested
140971** loop in WHERE clause.
140972**
140973** Contrast this object with WhereLoop. This object describes the
140974** implementation of the loop. WhereLoop describes the algorithm.
140975** This object contains a pointer to the WhereLoop algorithm as one of
140976** its elements.
140977**
140978** The WhereInfo object contains a single instance of this object for
140979** each term in the FROM clause (which is to say, for each of the
140980** nested loops as implemented). The order of WhereLevel objects determines
140981** the loop nested order, with WhereInfo.a[0] being the outer loop and
140982** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
140983*/
140984struct WhereLevel {
140985 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
140986 int iTabCur; /* The VDBE cursor used to access the table */
140987 int iIdxCur; /* The VDBE cursor used to access pIdx */
140988 int addrBrk; /* Jump here to break out of the loop */
140989 int addrNxt; /* Jump here to start the next IN combination */
140990 int addrSkip; /* Jump here for next iteration of skip-scan */
140991 int addrCont; /* Jump here to continue with the next loop cycle */
140992 int addrFirst; /* First instruction of interior of the loop */
140993 int addrBody; /* Beginning of the body of this loop */
140994 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
140995 int addrBignull; /* Jump here for next part of big-null scan */
140996#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
140997 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
140998 int addrLikeRep; /* LIKE range processing address */
140999#endif
141000 u8 iFrom; /* Which entry in the FROM clause */
141001 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
141002 int p1, p2; /* Operands of the opcode used to end the loop */
141003 union { /* Information that depends on pWLoop->wsFlags */
141004 struct {
141005 int nIn; /* Number of entries in aInLoop[] */
141006 struct InLoop {
141007 int iCur; /* The VDBE cursor used by this IN operator */
141008 int addrInTop; /* Top of the IN loop */
141009 int iBase; /* Base register of multi-key index record */
141010 int nPrefix; /* Number of prior entires in the key */
141011 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
141012 } *aInLoop; /* Information about each nested IN operator */
141013 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
141014 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
141015 } u;
141016 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
141017 Bitmask notReady; /* FROM entries not usable at this level */
141018#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141019 int addrVisit; /* Address at which row is visited */
141020#endif
141021};
141022
141023/*
141024** Each instance of this object represents an algorithm for evaluating one
141025** term of a join. Every term of the FROM clause will have at least
141026** one corresponding WhereLoop object (unless INDEXED BY constraints
141027** prevent a query solution - which is an error) and many terms of the
141028** FROM clause will have multiple WhereLoop objects, each describing a
141029** potential way of implementing that FROM-clause term, together with
141030** dependencies and cost estimates for using the chosen algorithm.
141031**
141032** Query planning consists of building up a collection of these WhereLoop
141033** objects, then computing a particular sequence of WhereLoop objects, with
141034** one WhereLoop object per FROM clause term, that satisfy all dependencies
141035** and that minimize the overall cost.
141036*/
141037struct WhereLoop {
141038 Bitmask prereq; /* Bitmask of other loops that must run first */
141039 Bitmask maskSelf; /* Bitmask identifying table iTab */
141040#ifdef SQLITE_DEBUG
141041 char cId; /* Symbolic ID of this loop for debugging use */
141042#endif
141043 u8 iTab; /* Position in FROM clause of table for this loop */
141044 u8 iSortIdx; /* Sorting index number. 0==None */
141045 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
141046 LogEst rRun; /* Cost of running each loop */
141047 LogEst nOut; /* Estimated number of output rows */
141048 union {
141049 struct { /* Information for internal btree tables */
141050 u16 nEq; /* Number of equality constraints */
141051 u16 nBtm; /* Size of BTM vector */
141052 u16 nTop; /* Size of TOP vector */
141053 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
141054 Index *pIndex; /* Index used, or NULL */
141055 } btree;
141056 struct { /* Information for virtual tables */
141057 int idxNum; /* Index number */
141058 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
141059 i8 isOrdered; /* True if satisfies ORDER BY */
141060 u16 omitMask; /* Terms that may be omitted */
141061 char *idxStr; /* Index identifier string */
141062 } vtab;
141063 } u;
141064 u32 wsFlags; /* WHERE_* flags describing the plan */
141065 u16 nLTerm; /* Number of entries in aLTerm[] */
141066 u16 nSkip; /* Number of NULL aLTerm[] entries */
141067 /**** whereLoopXfer() copies fields above ***********************/
141068# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
141069 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
141070 WhereTerm **aLTerm; /* WhereTerms used */
141071 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
141072 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
141073};
141074
141075/* This object holds the prerequisites and the cost of running a
141076** subquery on one operand of an OR operator in the WHERE clause.
141077** See WhereOrSet for additional information
141078*/
141079struct WhereOrCost {
141080 Bitmask prereq; /* Prerequisites */
141081 LogEst rRun; /* Cost of running this subquery */
141082 LogEst nOut; /* Number of outputs for this subquery */
141083};
141084
141085/* The WhereOrSet object holds a set of possible WhereOrCosts that
141086** correspond to the subquery(s) of OR-clause processing. Only the
141087** best N_OR_COST elements are retained.
141088*/
141089#define N_OR_COST 3
141090struct WhereOrSet {
141091 u16 n; /* Number of valid a[] entries */
141092 WhereOrCost a[N_OR_COST]; /* Set of best costs */
141093};
141094
141095/*
141096** Each instance of this object holds a sequence of WhereLoop objects
141097** that implement some or all of a query plan.
141098**
141099** Think of each WhereLoop object as a node in a graph with arcs
141100** showing dependencies and costs for travelling between nodes. (That is
141101** not a completely accurate description because WhereLoop costs are a
141102** vector, not a scalar, and because dependencies are many-to-one, not
141103** one-to-one as are graph nodes. But it is a useful visualization aid.)
141104** Then a WherePath object is a path through the graph that visits some
141105** or all of the WhereLoop objects once.
141106**
141107** The "solver" works by creating the N best WherePath objects of length
141108** 1. Then using those as a basis to compute the N best WherePath objects
141109** of length 2. And so forth until the length of WherePaths equals the
141110** number of nodes in the FROM clause. The best (lowest cost) WherePath
141111** at the end is the chosen query plan.
141112*/
141113struct WherePath {
141114 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
141115 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
141116 LogEst nRow; /* Estimated number of rows generated by this path */
141117 LogEst rCost; /* Total cost of this path */
141118 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
141119 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
141120 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
141121};
141122
141123/*
141124** The query generator uses an array of instances of this structure to
141125** help it analyze the subexpressions of the WHERE clause. Each WHERE
141126** clause subexpression is separated from the others by AND operators,
141127** usually, or sometimes subexpressions separated by OR.
141128**
141129** All WhereTerms are collected into a single WhereClause structure.
141130** The following identity holds:
141131**
141132** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
141133**
141134** When a term is of the form:
141135**
141136** X <op> <expr>
141137**
141138** where X is a column name and <op> is one of certain operators,
141139** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
141140** cursor number and column number for X. WhereTerm.eOperator records
141141** the <op> using a bitmask encoding defined by WO_xxx below. The
141142** use of a bitmask encoding for the operator allows us to search
141143** quickly for terms that match any of several different operators.
141144**
141145** A WhereTerm might also be two or more subterms connected by OR:
141146**
141147** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
141148**
141149** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
141150** and the WhereTerm.u.pOrInfo field points to auxiliary information that
141151** is collected about the OR clause.
141152**
141153** If a term in the WHERE clause does not match either of the two previous
141154** categories, then eOperator==0. The WhereTerm.pExpr field is still set
141155** to the original subexpression content and wtFlags is set up appropriately
141156** but no other fields in the WhereTerm object are meaningful.
141157**
141158** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
141159** but they do so indirectly. A single WhereMaskSet structure translates
141160** cursor number into bits and the translated bit is stored in the prereq
141161** fields. The translation is used in order to maximize the number of
141162** bits that will fit in a Bitmask. The VDBE cursor numbers might be
141163** spread out over the non-negative integers. For example, the cursor
141164** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
141165** translates these sparse cursor numbers into consecutive integers
141166** beginning with 0 in order to make the best possible use of the available
141167** bits in the Bitmask. So, in the example above, the cursor numbers
141168** would be mapped into integers 0 through 7.
141169**
141170** The number of terms in a join is limited by the number of bits
141171** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
141172** is only able to process joins with 64 or fewer tables.
141173*/
141174struct WhereTerm {
141175 Expr *pExpr; /* Pointer to the subexpression that is this term */
141176 WhereClause *pWC; /* The clause this term is part of */
141177 LogEst truthProb; /* Probability of truth for this expression */
141178 u16 wtFlags; /* TERM_xxx bit flags. See below */
141179 u16 eOperator; /* A WO_xx value describing <op> */
141180 u8 nChild; /* Number of children that must disable us */
141181 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
141182 int iParent; /* Disable pWC->a[iParent] when this term disabled */
141183 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
141184 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
141185 union {
141186 int leftColumn; /* Column number of X in "X <op> <expr>" */
141187 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
141188 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
141189 } u;
141190 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
141191 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
141192};
141193
141194/*
141195** Allowed values of WhereTerm.wtFlags
141196*/
141197#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
141198#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
141199#define TERM_CODED 0x0004 /* This term is already coded */
141200#define TERM_COPIED 0x0008 /* Has a child */
141201#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
141202#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
141203#define TERM_OR_OK 0x0040 /* Used during OR-clause processing */
141204#ifdef SQLITE_ENABLE_STAT4
141205# define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
141206#else
141207# define TERM_VNULL 0x0000 /* Disabled if not using stat4 */
141208#endif
141209#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
141210#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
141211#define TERM_LIKE 0x0400 /* The original LIKE operator */
141212#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
141213#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
141214#define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */
141215#ifdef SQLITE_ENABLE_STAT4
141216# define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */
141217#else
141218# define TERM_HIGHTRUTH 0 /* Only used with STAT4 */
141219#endif
141220
141221/*
141222** An instance of the WhereScan object is used as an iterator for locating
141223** terms in the WHERE clause that are useful to the query planner.
141224*/
141225struct WhereScan {
141226 WhereClause *pOrigWC; /* Original, innermost WhereClause */
141227 WhereClause *pWC; /* WhereClause currently being scanned */
141228 const char *zCollName; /* Required collating sequence, if not NULL */
141229 Expr *pIdxExpr; /* Search for this index expression */
141230 char idxaff; /* Must match this affinity, if zCollName!=NULL */
141231 unsigned char nEquiv; /* Number of entries in aEquiv[] */
141232 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
141233 u32 opMask; /* Acceptable operators */
141234 int k; /* Resume scanning at this->pWC->a[this->k] */
141235 int aiCur[11]; /* Cursors in the equivalence class */
141236 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
141237};
141238
141239/*
141240** An instance of the following structure holds all information about a
141241** WHERE clause. Mostly this is a container for one or more WhereTerms.
141242**
141243** Explanation of pOuter: For a WHERE clause of the form
141244**
141245** a AND ((b AND c) OR (d AND e)) AND f
141246**
141247** There are separate WhereClause objects for the whole clause and for
141248** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
141249** subclauses points to the WhereClause object for the whole clause.
141250*/
141251struct WhereClause {
141252 WhereInfo *pWInfo; /* WHERE clause processing context */
141253 WhereClause *pOuter; /* Outer conjunction */
141254 u8 op; /* Split operator. TK_AND or TK_OR */
141255 u8 hasOr; /* True if any a[].eOperator is WO_OR */
141256 int nTerm; /* Number of terms */
141257 int nSlot; /* Number of entries in a[] */
141258 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
141259#if defined(SQLITE_SMALL_STACK)
141260 WhereTerm aStatic[1]; /* Initial static space for a[] */
141261#else
141262 WhereTerm aStatic[8]; /* Initial static space for a[] */
141263#endif
141264};
141265
141266/*
141267** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
141268** a dynamically allocated instance of the following structure.
141269*/
141270struct WhereOrInfo {
141271 WhereClause wc; /* Decomposition into subterms */
141272 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
141273};
141274
141275/*
141276** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
141277** a dynamically allocated instance of the following structure.
141278*/
141279struct WhereAndInfo {
141280 WhereClause wc; /* The subexpression broken out */
141281};
141282
141283/*
141284** An instance of the following structure keeps track of a mapping
141285** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
141286**
141287** The VDBE cursor numbers are small integers contained in
141288** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
141289** clause, the cursor numbers might not begin with 0 and they might
141290** contain gaps in the numbering sequence. But we want to make maximum
141291** use of the bits in our bitmasks. This structure provides a mapping
141292** from the sparse cursor numbers into consecutive integers beginning
141293** with 0.
141294**
141295** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
141296** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
141297**
141298** For example, if the WHERE clause expression used these VDBE
141299** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
141300** would map those cursor numbers into bits 0 through 5.
141301**
141302** Note that the mapping is not necessarily ordered. In the example
141303** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
141304** 57->5, 73->4. Or one of 719 other combinations might be used. It
141305** does not really matter. What is important is that sparse cursor
141306** numbers all get mapped into bit numbers that begin with 0 and contain
141307** no gaps.
141308*/
141309struct WhereMaskSet {
141310 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
141311 int n; /* Number of assigned cursor values */
141312 int ix[BMS]; /* Cursor assigned to each bit */
141313};
141314
141315/*
141316** Initialize a WhereMaskSet object
141317*/
141318#define initMaskSet(P) (P)->n=0
141319
141320/*
141321** This object is a convenience wrapper holding all information needed
141322** to construct WhereLoop objects for a particular query.
141323*/
141324struct WhereLoopBuilder {
141325 WhereInfo *pWInfo; /* Information about this WHERE */
141326 WhereClause *pWC; /* WHERE clause terms */
141327 ExprList *pOrderBy; /* ORDER BY clause */
141328 WhereLoop *pNew; /* Template WhereLoop */
141329 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
141330#ifdef SQLITE_ENABLE_STAT4
141331 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
141332 int nRecValid; /* Number of valid fields currently in pRec */
141333#endif
141334 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
141335 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
141336 unsigned int iPlanLimit; /* Search limiter */
141337};
141338
141339/* Allowed values for WhereLoopBuider.bldFlags */
141340#define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */
141341#define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
141342
141343#define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */
141344
141345/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
141346** index+constraint combinations the query planner will consider for a
141347** particular query. If this parameter is unlimited, then certain
141348** pathological queries can spend excess time in the sqlite3WhereBegin()
141349** routine. The limit is high enough that is should not impact real-world
141350** queries.
141351**
141352** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
141353** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
141354** clause is processed, so that every table in a join is guaranteed to be
141355** able to propose a some index+constraint combinations even if the initial
141356** baseline limit was exhausted by prior tables of the join.
141357*/
141358#ifndef SQLITE_QUERY_PLANNER_LIMIT
141359# define SQLITE_QUERY_PLANNER_LIMIT 20000
141360#endif
141361#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
141362# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
141363#endif
141364
141365/*
141366** Each instance of this object records a change to a single node
141367** in an expression tree to cause that node to point to a column
141368** of an index rather than an expression or a virtual column. All
141369** such transformations need to be undone at the end of WHERE clause
141370** processing.
141371*/
141372typedef struct WhereExprMod WhereExprMod;
141373struct WhereExprMod {
141374 WhereExprMod *pNext; /* Next translation on a list of them all */
141375 Expr *pExpr; /* The Expr node that was transformed */
141376 Expr orig; /* Original value of the Expr node */
141377};
141378
141379/*
141380** The WHERE clause processing routine has two halves. The
141381** first part does the start of the WHERE loop and the second
141382** half does the tail of the WHERE loop. An instance of
141383** this structure is returned by the first half and passed
141384** into the second half to give some continuity.
141385**
141386** An instance of this object holds the complete state of the query
141387** planner.
141388*/
141389struct WhereInfo {
141390 Parse *pParse; /* Parsing and code generating context */
141391 SrcList *pTabList; /* List of tables in the join */
141392 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
141393 ExprList *pResultSet; /* Result set of the query */
141394 Expr *pWhere; /* The complete WHERE clause */
141395 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
141396 int iContinue; /* Jump here to continue with next record */
141397 int iBreak; /* Jump here to break out of the loop */
141398 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
141399 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
141400 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
141401 u8 nLevel; /* Number of nested loop */
141402 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
141403 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
141404 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
141405 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
141406 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
141407 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
141408 unsigned sorted :1; /* True if really sorted (not just grouped) */
141409 LogEst nRowOut; /* Estimated number of output rows */
141410 int iTop; /* The very beginning of the WHERE loop */
141411 WhereLoop *pLoops; /* List of all WhereLoop objects */
141412 WhereExprMod *pExprMods; /* Expression modifications */
141413 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
141414 WhereClause sWC; /* Decomposition of the WHERE clause */
141415 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
141416 WhereLevel a[1]; /* Information about each nest loop in WHERE */
141417};
141418
141419/*
141420** Private interfaces - callable only by other where.c routines.
141421**
141422** where.c:
141423*/
141424SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
141425#ifdef WHERETRACE_ENABLED
141426SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
141427SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
141428SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
141429#endif
141430SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
141431 WhereClause *pWC, /* The WHERE clause to be searched */
141432 int iCur, /* Cursor number of LHS */
141433 int iColumn, /* Column number of LHS */
141434 Bitmask notReady, /* RHS must not overlap with this mask */
141435 u32 op, /* Mask of WO_xx values describing operator */
141436 Index *pIdx /* Must be compatible with this index, if not NULL */
141437);
141438
141439/* wherecode.c: */
141440#ifndef SQLITE_OMIT_EXPLAIN
141441SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
141442 Parse *pParse, /* Parse context */
141443 SrcList *pTabList, /* Table list this loop refers to */
141444 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
141445 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
141446);
141447#else
141448# define sqlite3WhereExplainOneScan(u,v,w,x) 0
141449#endif /* SQLITE_OMIT_EXPLAIN */
141450#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141451SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
141452 Vdbe *v, /* Vdbe to add scanstatus entry to */
141453 SrcList *pSrclist, /* FROM clause pLvl reads data from */
141454 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
141455 int addrExplain /* Address of OP_Explain (or 0) */
141456);
141457#else
141458# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
141459#endif
141460SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
141461 Parse *pParse, /* Parsing context */
141462 Vdbe *v, /* Prepared statement under construction */
141463 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
141464 int iLevel, /* Which level of pWInfo->a[] should be coded */
141465 WhereLevel *pLevel, /* The current level pointer */
141466 Bitmask notReady /* Which tables are currently available */
141467);
141468
141469/* whereexpr.c: */
141470SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
141471SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
141472SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
141473SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
141474SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
141475SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
141476SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
141477SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
141478
141479
141480
141481
141482
141483/*
141484** Bitmasks for the operators on WhereTerm objects. These are all
141485** operators that are of interest to the query planner. An
141486** OR-ed combination of these values can be used when searching for
141487** particular WhereTerms within a WhereClause.
141488**
141489** Value constraints:
141490** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
141491** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
141492** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
141493** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
141494** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
141495*/
141496#define WO_IN 0x0001
141497#define WO_EQ 0x0002
141498#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
141499#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
141500#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
141501#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
141502#define WO_AUX 0x0040 /* Op useful to virtual tables only */
141503#define WO_IS 0x0080
141504#define WO_ISNULL 0x0100
141505#define WO_OR 0x0200 /* Two or more OR-connected terms */
141506#define WO_AND 0x0400 /* Two or more AND-connected terms */
141507#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
141508#define WO_NOOP 0x1000 /* This term does not restrict search space */
141509
141510#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
141511#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
141512
141513/*
141514** These are definitions of bits in the WhereLoop.wsFlags field.
141515** The particular combination of bits in each WhereLoop help to
141516** determine the algorithm that WhereLoop represents.
141517*/
141518#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
141519#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
141520#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
141521#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
141522#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
141523#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
141524#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
141525#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
141526#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
141527#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
141528#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
141529#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
141530#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
141531#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
141532#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
141533#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
141534#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
141535#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
141536#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
141537#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
141538#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
141539
141540#endif /* !defined(SQLITE_WHEREINT_H) */
141541
141542/************** End of whereInt.h ********************************************/
141543/************** Continuing where we left off in wherecode.c ******************/
141544
141545#ifndef SQLITE_OMIT_EXPLAIN
141546
141547/*
141548** Return the name of the i-th column of the pIdx index.
141549*/
141550static const char *explainIndexColumnName(Index *pIdx, int i){
141551 i = pIdx->aiColumn[i];
141552 if( i==XN_EXPR ) return "<expr>";
141553 if( i==XN_ROWID ) return "rowid";
141554 return pIdx->pTable->aCol[i].zName;
141555}
141556
141557/*
141558** This routine is a helper for explainIndexRange() below
141559**
141560** pStr holds the text of an expression that we are building up one term
141561** at a time. This routine adds a new term to the end of the expression.
141562** Terms are separated by AND so add the "AND" text for second and subsequent
141563** terms only.
141564*/
141565static void explainAppendTerm(
141566 StrAccum *pStr, /* The text expression being built */
141567 Index *pIdx, /* Index to read column names from */
141568 int nTerm, /* Number of terms */
141569 int iTerm, /* Zero-based index of first term. */
141570 int bAnd, /* Non-zero to append " AND " */
141571 const char *zOp /* Name of the operator */
141572){
141573 int i;
141574
141575 assert( nTerm>=1 );
141576 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
141577
141578 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
141579 for(i=0; i<nTerm; i++){
141580 if( i ) sqlite3_str_append(pStr, ",", 1);
141581 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
141582 }
141583 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
141584
141585 sqlite3_str_append(pStr, zOp, 1);
141586
141587 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
141588 for(i=0; i<nTerm; i++){
141589 if( i ) sqlite3_str_append(pStr, ",", 1);
141590 sqlite3_str_append(pStr, "?", 1);
141591 }
141592 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
141593}
141594
141595/*
141596** Argument pLevel describes a strategy for scanning table pTab. This
141597** function appends text to pStr that describes the subset of table
141598** rows scanned by the strategy in the form of an SQL expression.
141599**
141600** For example, if the query:
141601**
141602** SELECT * FROM t1 WHERE a=1 AND b>2;
141603**
141604** is run and there is an index on (a, b), then this function returns a
141605** string similar to:
141606**
141607** "a=? AND b>?"
141608*/
141609static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
141610 Index *pIndex = pLoop->u.btree.pIndex;
141611 u16 nEq = pLoop->u.btree.nEq;
141612 u16 nSkip = pLoop->nSkip;
141613 int i, j;
141614
141615 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
141616 sqlite3_str_append(pStr, " (", 2);
141617 for(i=0; i<nEq; i++){
141618 const char *z = explainIndexColumnName(pIndex, i);
141619 if( i ) sqlite3_str_append(pStr, " AND ", 5);
141620 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
141621 }
141622
141623 j = i;
141624 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
141625 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
141626 i = 1;
141627 }
141628 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
141629 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
141630 }
141631 sqlite3_str_append(pStr, ")", 1);
141632}
141633
141634/*
141635** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
141636** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
141637** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
141638** is added to the output to describe the table scan strategy in pLevel.
141639**
141640** If an OP_Explain opcode is added to the VM, its address is returned.
141641** Otherwise, if no OP_Explain is coded, zero is returned.
141642*/
141643SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
141644 Parse *pParse, /* Parse context */
141645 SrcList *pTabList, /* Table list this loop refers to */
141646 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
141647 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
141648){
141649 int ret = 0;
141650#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
141651 if( sqlite3ParseToplevel(pParse)->explain==2 )
141652#endif
141653 {
141654 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
141655 Vdbe *v = pParse->pVdbe; /* VM being constructed */
141656 sqlite3 *db = pParse->db; /* Database handle */
141657 int isSearch; /* True for a SEARCH. False for SCAN. */
141658 WhereLoop *pLoop; /* The controlling WhereLoop object */
141659 u32 flags; /* Flags that describe this loop */
141660 char *zMsg; /* Text to add to EQP output */
141661 StrAccum str; /* EQP output string */
141662 char zBuf[100]; /* Initial space for EQP output string */
141663
141664 pLoop = pLevel->pWLoop;
141665 flags = pLoop->wsFlags;
141666 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
141667
141668 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
141669 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
141670 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
141671
141672 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
141673 sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
141674 if( pItem->pSelect ){
141675 sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
141676 }else{
141677 sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
141678 }
141679
141680 if( pItem->zAlias ){
141681 sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
141682 }
141683 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
141684 const char *zFmt = 0;
141685 Index *pIdx;
141686
141687 assert( pLoop->u.btree.pIndex!=0 );
141688 pIdx = pLoop->u.btree.pIndex;
141689 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
141690 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
141691 if( isSearch ){
141692 zFmt = "PRIMARY KEY";
141693 }
141694 }else if( flags & WHERE_PARTIALIDX ){
141695 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
141696 }else if( flags & WHERE_AUTO_INDEX ){
141697 zFmt = "AUTOMATIC COVERING INDEX";
141698 }else if( flags & WHERE_IDX_ONLY ){
141699 zFmt = "COVERING INDEX %s";
141700 }else{
141701 zFmt = "INDEX %s";
141702 }
141703 if( zFmt ){
141704 sqlite3_str_append(&str, " USING ", 7);
141705 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
141706 explainIndexRange(&str, pLoop);
141707 }
141708 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
141709 const char *zRangeOp;
141710 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
141711 zRangeOp = "=";
141712 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
141713 zRangeOp = ">? AND rowid<";
141714 }else if( flags&WHERE_BTM_LIMIT ){
141715 zRangeOp = ">";
141716 }else{
141717 assert( flags&WHERE_TOP_LIMIT);
141718 zRangeOp = "<";
141719 }
141720 sqlite3_str_appendf(&str,
141721 " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
141722 }
141723#ifndef SQLITE_OMIT_VIRTUALTABLE
141724 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
141725 sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
141726 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
141727 }
141728#endif
141729#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
141730 if( pLoop->nOut>=10 ){
141731 sqlite3_str_appendf(&str, " (~%llu rows)",
141732 sqlite3LogEstToInt(pLoop->nOut));
141733 }else{
141734 sqlite3_str_append(&str, " (~1 row)", 9);
141735 }
141736#endif
141737 zMsg = sqlite3StrAccumFinish(&str);
141738 sqlite3ExplainBreakpoint("",zMsg);
141739 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
141740 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
141741 }
141742 return ret;
141743}
141744#endif /* SQLITE_OMIT_EXPLAIN */
141745
141746#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
141747/*
141748** Configure the VM passed as the first argument with an
141749** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
141750** implement level pLvl. Argument pSrclist is a pointer to the FROM
141751** clause that the scan reads data from.
141752**
141753** If argument addrExplain is not 0, it must be the address of an
141754** OP_Explain instruction that describes the same loop.
141755*/
141756SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
141757 Vdbe *v, /* Vdbe to add scanstatus entry to */
141758 SrcList *pSrclist, /* FROM clause pLvl reads data from */
141759 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
141760 int addrExplain /* Address of OP_Explain (or 0) */
141761){
141762 const char *zObj = 0;
141763 WhereLoop *pLoop = pLvl->pWLoop;
141764 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
141765 zObj = pLoop->u.btree.pIndex->zName;
141766 }else{
141767 zObj = pSrclist->a[pLvl->iFrom].zName;
141768 }
141769 sqlite3VdbeScanStatus(
141770 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
141771 );
141772}
141773#endif
141774
141775
141776/*
141777** Disable a term in the WHERE clause. Except, do not disable the term
141778** if it controls a LEFT OUTER JOIN and it did not originate in the ON
141779** or USING clause of that join.
141780**
141781** Consider the term t2.z='ok' in the following queries:
141782**
141783** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
141784** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
141785** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
141786**
141787** The t2.z='ok' is disabled in the in (2) because it originates
141788** in the ON clause. The term is disabled in (3) because it is not part
141789** of a LEFT OUTER JOIN. In (1), the term is not disabled.
141790**
141791** Disabling a term causes that term to not be tested in the inner loop
141792** of the join. Disabling is an optimization. When terms are satisfied
141793** by indices, we disable them to prevent redundant tests in the inner
141794** loop. We would get the correct results if nothing were ever disabled,
141795** but joins might run a little slower. The trick is to disable as much
141796** as we can without disabling too much. If we disabled in (1), we'd get
141797** the wrong answer. See ticket #813.
141798**
141799** If all the children of a term are disabled, then that term is also
141800** automatically disabled. In this way, terms get disabled if derived
141801** virtual terms are tested first. For example:
141802**
141803** x GLOB 'abc*' AND x>='abc' AND x<'acd'
141804** \___________/ \______/ \_____/
141805** parent child1 child2
141806**
141807** Only the parent term was in the original WHERE clause. The child1
141808** and child2 terms were added by the LIKE optimization. If both of
141809** the virtual child terms are valid, then testing of the parent can be
141810** skipped.
141811**
141812** Usually the parent term is marked as TERM_CODED. But if the parent
141813** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
141814** The TERM_LIKECOND marking indicates that the term should be coded inside
141815** a conditional such that is only evaluated on the second pass of a
141816** LIKE-optimization loop, when scanning BLOBs instead of strings.
141817*/
141818static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
141819 int nLoop = 0;
141820 assert( pTerm!=0 );
141821 while( (pTerm->wtFlags & TERM_CODED)==0
141822 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
141823 && (pLevel->notReady & pTerm->prereqAll)==0
141824 ){
141825 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
141826 pTerm->wtFlags |= TERM_LIKECOND;
141827 }else{
141828 pTerm->wtFlags |= TERM_CODED;
141829 }
141830 if( pTerm->iParent<0 ) break;
141831 pTerm = &pTerm->pWC->a[pTerm->iParent];
141832 assert( pTerm!=0 );
141833 pTerm->nChild--;
141834 if( pTerm->nChild!=0 ) break;
141835 nLoop++;
141836 }
141837}
141838
141839/*
141840** Code an OP_Affinity opcode to apply the column affinity string zAff
141841** to the n registers starting at base.
141842**
141843** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
141844** are no-ops) at the beginning and end of zAff are ignored. If all entries
141845** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
141846**
141847** This routine makes its own copy of zAff so that the caller is free
141848** to modify zAff after this routine returns.
141849*/
141850static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
141851 Vdbe *v = pParse->pVdbe;
141852 if( zAff==0 ){
141853 assert( pParse->db->mallocFailed );
141854 return;
141855 }
141856 assert( v!=0 );
141857
141858 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
141859 ** entries at the beginning and end of the affinity string.
141860 */
141861 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
141862 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
141863 n--;
141864 base++;
141865 zAff++;
141866 }
141867 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
141868 n--;
141869 }
141870
141871 /* Code the OP_Affinity opcode if there is anything left to do. */
141872 if( n>0 ){
141873 sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
141874 }
141875}
141876
141877/*
141878** Expression pRight, which is the RHS of a comparison operation, is
141879** either a vector of n elements or, if n==1, a scalar expression.
141880** Before the comparison operation, affinity zAff is to be applied
141881** to the pRight values. This function modifies characters within the
141882** affinity string to SQLITE_AFF_BLOB if either:
141883**
141884** * the comparison will be performed with no affinity, or
141885** * the affinity change in zAff is guaranteed not to change the value.
141886*/
141887static void updateRangeAffinityStr(
141888 Expr *pRight, /* RHS of comparison */
141889 int n, /* Number of vector elements in comparison */
141890 char *zAff /* Affinity string to modify */
141891){
141892 int i;
141893 for(i=0; i<n; i++){
141894 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
141895 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
141896 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
141897 ){
141898 zAff[i] = SQLITE_AFF_BLOB;
141899 }
141900 }
141901}
141902
141903
141904/*
141905** pX is an expression of the form: (vector) IN (SELECT ...)
141906** In other words, it is a vector IN operator with a SELECT clause on the
141907** LHS. But not all terms in the vector are indexable and the terms might
141908** not be in the correct order for indexing.
141909**
141910** This routine makes a copy of the input pX expression and then adjusts
141911** the vector on the LHS with corresponding changes to the SELECT so that
141912** the vector contains only index terms and those terms are in the correct
141913** order. The modified IN expression is returned. The caller is responsible
141914** for deleting the returned expression.
141915**
141916** Example:
141917**
141918** CREATE TABLE t1(a,b,c,d,e,f);
141919** CREATE INDEX t1x1 ON t1(e,c);
141920** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
141921** \_______________________________________/
141922** The pX expression
141923**
141924** Since only columns e and c can be used with the index, in that order,
141925** the modified IN expression that is returned will be:
141926**
141927** (e,c) IN (SELECT z,x FROM t2)
141928**
141929** The reduced pX is different from the original (obviously) and thus is
141930** only used for indexing, to improve performance. The original unaltered
141931** IN expression must also be run on each output row for correctness.
141932*/
141933static Expr *removeUnindexableInClauseTerms(
141934 Parse *pParse, /* The parsing context */
141935 int iEq, /* Look at loop terms starting here */
141936 WhereLoop *pLoop, /* The current loop */
141937 Expr *pX /* The IN expression to be reduced */
141938){
141939 sqlite3 *db = pParse->db;
141940 Expr *pNew;
141941 pNew = sqlite3ExprDup(db, pX, 0);
141942 if( db->mallocFailed==0 ){
141943 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
141944 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
141945 ExprList *pRhs = 0; /* New RHS after modifications */
141946 ExprList *pLhs = 0; /* New LHS after mods */
141947 int i; /* Loop counter */
141948 Select *pSelect; /* Pointer to the SELECT on the RHS */
141949
141950 for(i=iEq; i<pLoop->nLTerm; i++){
141951 if( pLoop->aLTerm[i]->pExpr==pX ){
141952 int iField = pLoop->aLTerm[i]->iField - 1;
141953 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
141954 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
141955 pOrigRhs->a[iField].pExpr = 0;
141956 assert( pOrigLhs->a[iField].pExpr!=0 );
141957 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
141958 pOrigLhs->a[iField].pExpr = 0;
141959 }
141960 }
141961 sqlite3ExprListDelete(db, pOrigRhs);
141962 sqlite3ExprListDelete(db, pOrigLhs);
141963 pNew->pLeft->x.pList = pLhs;
141964 pNew->x.pSelect->pEList = pRhs;
141965 if( pLhs && pLhs->nExpr==1 ){
141966 /* Take care here not to generate a TK_VECTOR containing only a
141967 ** single value. Since the parser never creates such a vector, some
141968 ** of the subroutines do not handle this case. */
141969 Expr *p = pLhs->a[0].pExpr;
141970 pLhs->a[0].pExpr = 0;
141971 sqlite3ExprDelete(db, pNew->pLeft);
141972 pNew->pLeft = p;
141973 }
141974 pSelect = pNew->x.pSelect;
141975 if( pSelect->pOrderBy ){
141976 /* If the SELECT statement has an ORDER BY clause, zero the
141977 ** iOrderByCol variables. These are set to non-zero when an
141978 ** ORDER BY term exactly matches one of the terms of the
141979 ** result-set. Since the result-set of the SELECT statement may
141980 ** have been modified or reordered, these variables are no longer
141981 ** set correctly. Since setting them is just an optimization,
141982 ** it's easiest just to zero them here. */
141983 ExprList *pOrderBy = pSelect->pOrderBy;
141984 for(i=0; i<pOrderBy->nExpr; i++){
141985 pOrderBy->a[i].u.x.iOrderByCol = 0;
141986 }
141987 }
141988
141989#if 0
141990 printf("For indexing, change the IN expr:\n");
141991 sqlite3TreeViewExpr(0, pX, 0);
141992 printf("Into:\n");
141993 sqlite3TreeViewExpr(0, pNew, 0);
141994#endif
141995 }
141996 return pNew;
141997}
141998
141999
142000/*
142001** Generate code for a single equality term of the WHERE clause. An equality
142002** term can be either X=expr or X IN (...). pTerm is the term to be
142003** coded.
142004**
142005** The current value for the constraint is left in a register, the index
142006** of which is returned. An attempt is made store the result in iTarget but
142007** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
142008** constraint is a TK_EQ or TK_IS, then the current value might be left in
142009** some other register and it is the caller's responsibility to compensate.
142010**
142011** For a constraint of the form X=expr, the expression is evaluated in
142012** straight-line code. For constraints of the form X IN (...)
142013** this routine sets up a loop that will iterate over all values of X.
142014*/
142015static int codeEqualityTerm(
142016 Parse *pParse, /* The parsing context */
142017 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
142018 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
142019 int iEq, /* Index of the equality term within this level */
142020 int bRev, /* True for reverse-order IN operations */
142021 int iTarget /* Attempt to leave results in this register */
142022){
142023 Expr *pX = pTerm->pExpr;
142024 Vdbe *v = pParse->pVdbe;
142025 int iReg; /* Register holding results */
142026
142027 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
142028 assert( iTarget>0 );
142029 if( pX->op==TK_EQ || pX->op==TK_IS ){
142030 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
142031 }else if( pX->op==TK_ISNULL ){
142032 iReg = iTarget;
142033 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
142034#ifndef SQLITE_OMIT_SUBQUERY
142035 }else{
142036 int eType = IN_INDEX_NOOP;
142037 int iTab;
142038 struct InLoop *pIn;
142039 WhereLoop *pLoop = pLevel->pWLoop;
142040 int i;
142041 int nEq = 0;
142042 int *aiMap = 0;
142043
142044 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
142045 && pLoop->u.btree.pIndex!=0
142046 && pLoop->u.btree.pIndex->aSortOrder[iEq]
142047 ){
142048 testcase( iEq==0 );
142049 testcase( bRev );
142050 bRev = !bRev;
142051 }
142052 assert( pX->op==TK_IN );
142053 iReg = iTarget;
142054
142055 for(i=0; i<iEq; i++){
142056 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
142057 disableTerm(pLevel, pTerm);
142058 return iTarget;
142059 }
142060 }
142061 for(i=iEq;i<pLoop->nLTerm; i++){
142062 assert( pLoop->aLTerm[i]!=0 );
142063 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
142064 }
142065
142066 iTab = 0;
142067 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
142068 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0, &iTab);
142069 }else{
142070 sqlite3 *db = pParse->db;
142071 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
142072
142073 if( !db->mallocFailed ){
142074 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
142075 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
142076 pTerm->pExpr->iTable = iTab;
142077 }
142078 sqlite3ExprDelete(db, pX);
142079 pX = pTerm->pExpr;
142080 }
142081
142082 if( eType==IN_INDEX_INDEX_DESC ){
142083 testcase( bRev );
142084 bRev = !bRev;
142085 }
142086 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
142087 VdbeCoverageIf(v, bRev);
142088 VdbeCoverageIf(v, !bRev);
142089 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
142090
142091 pLoop->wsFlags |= WHERE_IN_ABLE;
142092 if( pLevel->u.in.nIn==0 ){
142093 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
142094 }
142095
142096 i = pLevel->u.in.nIn;
142097 pLevel->u.in.nIn += nEq;
142098 pLevel->u.in.aInLoop =
142099 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
142100 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
142101 pIn = pLevel->u.in.aInLoop;
142102 if( pIn ){
142103 int iMap = 0; /* Index in aiMap[] */
142104 pIn += i;
142105 for(i=iEq;i<pLoop->nLTerm; i++){
142106 if( pLoop->aLTerm[i]->pExpr==pX ){
142107 int iOut = iReg + i - iEq;
142108 if( eType==IN_INDEX_ROWID ){
142109 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
142110 }else{
142111 int iCol = aiMap ? aiMap[iMap++] : 0;
142112 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
142113 }
142114 sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
142115 if( i==iEq ){
142116 pIn->iCur = iTab;
142117 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
142118 if( iEq>0 ){
142119 pIn->iBase = iReg - i;
142120 pIn->nPrefix = i;
142121 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
142122 }else{
142123 pIn->nPrefix = 0;
142124 }
142125 }else{
142126 pIn->eEndLoopOp = OP_Noop;
142127 }
142128 pIn++;
142129 }
142130 }
142131 }else{
142132 pLevel->u.in.nIn = 0;
142133 }
142134 sqlite3DbFree(pParse->db, aiMap);
142135#endif
142136 }
142137 disableTerm(pLevel, pTerm);
142138 return iReg;
142139}
142140
142141/*
142142** Generate code that will evaluate all == and IN constraints for an
142143** index scan.
142144**
142145** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
142146** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
142147** The index has as many as three equality constraints, but in this
142148** example, the third "c" value is an inequality. So only two
142149** constraints are coded. This routine will generate code to evaluate
142150** a==5 and b IN (1,2,3). The current values for a and b will be stored
142151** in consecutive registers and the index of the first register is returned.
142152**
142153** In the example above nEq==2. But this subroutine works for any value
142154** of nEq including 0. If nEq==0, this routine is nearly a no-op.
142155** The only thing it does is allocate the pLevel->iMem memory cell and
142156** compute the affinity string.
142157**
142158** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
142159** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
142160** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
142161** occurs after the nEq quality constraints.
142162**
142163** This routine allocates a range of nEq+nExtraReg memory cells and returns
142164** the index of the first memory cell in that range. The code that
142165** calls this routine will use that memory range to store keys for
142166** start and termination conditions of the loop.
142167** key value of the loop. If one or more IN operators appear, then
142168** this routine allocates an additional nEq memory cells for internal
142169** use.
142170**
142171** Before returning, *pzAff is set to point to a buffer containing a
142172** copy of the column affinity string of the index allocated using
142173** sqlite3DbMalloc(). Except, entries in the copy of the string associated
142174** with equality constraints that use BLOB or NONE affinity are set to
142175** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
142176**
142177** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
142178** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
142179**
142180** In the example above, the index on t1(a) has TEXT affinity. But since
142181** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
142182** no conversion should be attempted before using a t2.b value as part of
142183** a key to search the index. Hence the first byte in the returned affinity
142184** string in this example would be set to SQLITE_AFF_BLOB.
142185*/
142186static int codeAllEqualityTerms(
142187 Parse *pParse, /* Parsing context */
142188 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
142189 int bRev, /* Reverse the order of IN operators */
142190 int nExtraReg, /* Number of extra registers to allocate */
142191 char **pzAff /* OUT: Set to point to affinity string */
142192){
142193 u16 nEq; /* The number of == or IN constraints to code */
142194 u16 nSkip; /* Number of left-most columns to skip */
142195 Vdbe *v = pParse->pVdbe; /* The vm under construction */
142196 Index *pIdx; /* The index being used for this loop */
142197 WhereTerm *pTerm; /* A single constraint term */
142198 WhereLoop *pLoop; /* The WhereLoop object */
142199 int j; /* Loop counter */
142200 int regBase; /* Base register */
142201 int nReg; /* Number of registers to allocate */
142202 char *zAff; /* Affinity string to return */
142203
142204 /* This module is only called on query plans that use an index. */
142205 pLoop = pLevel->pWLoop;
142206 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
142207 nEq = pLoop->u.btree.nEq;
142208 nSkip = pLoop->nSkip;
142209 pIdx = pLoop->u.btree.pIndex;
142210 assert( pIdx!=0 );
142211
142212 /* Figure out how many memory cells we will need then allocate them.
142213 */
142214 regBase = pParse->nMem + 1;
142215 nReg = pLoop->u.btree.nEq + nExtraReg;
142216 pParse->nMem += nReg;
142217
142218 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
142219 assert( zAff!=0 || pParse->db->mallocFailed );
142220
142221 if( nSkip ){
142222 int iIdxCur = pLevel->iIdxCur;
142223 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
142224 VdbeCoverageIf(v, bRev==0);
142225 VdbeCoverageIf(v, bRev!=0);
142226 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
142227 j = sqlite3VdbeAddOp0(v, OP_Goto);
142228 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
142229 iIdxCur, 0, regBase, nSkip);
142230 VdbeCoverageIf(v, bRev==0);
142231 VdbeCoverageIf(v, bRev!=0);
142232 sqlite3VdbeJumpHere(v, j);
142233 for(j=0; j<nSkip; j++){
142234 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
142235 testcase( pIdx->aiColumn[j]==XN_EXPR );
142236 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
142237 }
142238 }
142239
142240 /* Evaluate the equality constraints
142241 */
142242 assert( zAff==0 || (int)strlen(zAff)>=nEq );
142243 for(j=nSkip; j<nEq; j++){
142244 int r1;
142245 pTerm = pLoop->aLTerm[j];
142246 assert( pTerm!=0 );
142247 /* The following testcase is true for indices with redundant columns.
142248 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
142249 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
142250 testcase( pTerm->wtFlags & TERM_VIRTUAL );
142251 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
142252 if( r1!=regBase+j ){
142253 if( nReg==1 ){
142254 sqlite3ReleaseTempReg(pParse, regBase);
142255 regBase = r1;
142256 }else{
142257 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
142258 }
142259 }
142260 if( pTerm->eOperator & WO_IN ){
142261 if( pTerm->pExpr->flags & EP_xIsSelect ){
142262 /* No affinity ever needs to be (or should be) applied to a value
142263 ** from the RHS of an "? IN (SELECT ...)" expression. The
142264 ** sqlite3FindInIndex() routine has already ensured that the
142265 ** affinity of the comparison has been applied to the value. */
142266 if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
142267 }
142268 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
142269 Expr *pRight = pTerm->pExpr->pRight;
142270 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
142271 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
142272 VdbeCoverage(v);
142273 }
142274 if( zAff ){
142275 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
142276 zAff[j] = SQLITE_AFF_BLOB;
142277 }
142278 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
142279 zAff[j] = SQLITE_AFF_BLOB;
142280 }
142281 }
142282 }
142283 }
142284 *pzAff = zAff;
142285 return regBase;
142286}
142287
142288#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
142289/*
142290** If the most recently coded instruction is a constant range constraint
142291** (a string literal) that originated from the LIKE optimization, then
142292** set P3 and P5 on the OP_String opcode so that the string will be cast
142293** to a BLOB at appropriate times.
142294**
142295** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
142296** expression: "x>='ABC' AND x<'abd'". But this requires that the range
142297** scan loop run twice, once for strings and a second time for BLOBs.
142298** The OP_String opcodes on the second pass convert the upper and lower
142299** bound string constants to blobs. This routine makes the necessary changes
142300** to the OP_String opcodes for that to happen.
142301**
142302** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
142303** only the one pass through the string space is required, so this routine
142304** becomes a no-op.
142305*/
142306static void whereLikeOptimizationStringFixup(
142307 Vdbe *v, /* prepared statement under construction */
142308 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
142309 WhereTerm *pTerm /* The upper or lower bound just coded */
142310){
142311 if( pTerm->wtFlags & TERM_LIKEOPT ){
142312 VdbeOp *pOp;
142313 assert( pLevel->iLikeRepCntr>0 );
142314 pOp = sqlite3VdbeGetOp(v, -1);
142315 assert( pOp!=0 );
142316 assert( pOp->opcode==OP_String8
142317 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
142318 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
142319 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
142320 }
142321}
142322#else
142323# define whereLikeOptimizationStringFixup(A,B,C)
142324#endif
142325
142326#ifdef SQLITE_ENABLE_CURSOR_HINTS
142327/*
142328** Information is passed from codeCursorHint() down to individual nodes of
142329** the expression tree (by sqlite3WalkExpr()) using an instance of this
142330** structure.
142331*/
142332struct CCurHint {
142333 int iTabCur; /* Cursor for the main table */
142334 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
142335 Index *pIdx; /* The index used to access the table */
142336};
142337
142338/*
142339** This function is called for every node of an expression that is a candidate
142340** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
142341** the table CCurHint.iTabCur, verify that the same column can be
142342** accessed through the index. If it cannot, then set pWalker->eCode to 1.
142343*/
142344static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
142345 struct CCurHint *pHint = pWalker->u.pCCurHint;
142346 assert( pHint->pIdx!=0 );
142347 if( pExpr->op==TK_COLUMN
142348 && pExpr->iTable==pHint->iTabCur
142349 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
142350 ){
142351 pWalker->eCode = 1;
142352 }
142353 return WRC_Continue;
142354}
142355
142356/*
142357** Test whether or not expression pExpr, which was part of a WHERE clause,
142358** should be included in the cursor-hint for a table that is on the rhs
142359** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
142360** expression is not suitable.
142361**
142362** An expression is unsuitable if it might evaluate to non NULL even if
142363** a TK_COLUMN node that does affect the value of the expression is set
142364** to NULL. For example:
142365**
142366** col IS NULL
142367** col IS NOT NULL
142368** coalesce(col, 1)
142369** CASE WHEN col THEN 0 ELSE 1 END
142370*/
142371static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
142372 if( pExpr->op==TK_IS
142373 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
142374 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
142375 ){
142376 pWalker->eCode = 1;
142377 }else if( pExpr->op==TK_FUNCTION ){
142378 int d1;
142379 char d2[4];
142380 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
142381 pWalker->eCode = 1;
142382 }
142383 }
142384
142385 return WRC_Continue;
142386}
142387
142388
142389/*
142390** This function is called on every node of an expression tree used as an
142391** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
142392** that accesses any table other than the one identified by
142393** CCurHint.iTabCur, then do the following:
142394**
142395** 1) allocate a register and code an OP_Column instruction to read
142396** the specified column into the new register, and
142397**
142398** 2) transform the expression node to a TK_REGISTER node that reads
142399** from the newly populated register.
142400**
142401** Also, if the node is a TK_COLUMN that does access the table idenified
142402** by pCCurHint.iTabCur, and an index is being used (which we will
142403** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
142404** an access of the index rather than the original table.
142405*/
142406static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
142407 int rc = WRC_Continue;
142408 struct CCurHint *pHint = pWalker->u.pCCurHint;
142409 if( pExpr->op==TK_COLUMN ){
142410 if( pExpr->iTable!=pHint->iTabCur ){
142411 int reg = ++pWalker->pParse->nMem; /* Register for column value */
142412 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
142413 pExpr->op = TK_REGISTER;
142414 pExpr->iTable = reg;
142415 }else if( pHint->pIdx!=0 ){
142416 pExpr->iTable = pHint->iIdxCur;
142417 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
142418 assert( pExpr->iColumn>=0 );
142419 }
142420 }else if( pExpr->op==TK_AGG_FUNCTION ){
142421 /* An aggregate function in the WHERE clause of a query means this must
142422 ** be a correlated sub-query, and expression pExpr is an aggregate from
142423 ** the parent context. Do not walk the function arguments in this case.
142424 **
142425 ** todo: It should be possible to replace this node with a TK_REGISTER
142426 ** expression, as the result of the expression must be stored in a
142427 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
142428 rc = WRC_Prune;
142429 }
142430 return rc;
142431}
142432
142433/*
142434** Insert an OP_CursorHint instruction if it is appropriate to do so.
142435*/
142436static void codeCursorHint(
142437 struct SrcList_item *pTabItem, /* FROM clause item */
142438 WhereInfo *pWInfo, /* The where clause */
142439 WhereLevel *pLevel, /* Which loop to provide hints for */
142440 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
142441){
142442 Parse *pParse = pWInfo->pParse;
142443 sqlite3 *db = pParse->db;
142444 Vdbe *v = pParse->pVdbe;
142445 Expr *pExpr = 0;
142446 WhereLoop *pLoop = pLevel->pWLoop;
142447 int iCur;
142448 WhereClause *pWC;
142449 WhereTerm *pTerm;
142450 int i, j;
142451 struct CCurHint sHint;
142452 Walker sWalker;
142453
142454 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
142455 iCur = pLevel->iTabCur;
142456 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
142457 sHint.iTabCur = iCur;
142458 sHint.iIdxCur = pLevel->iIdxCur;
142459 sHint.pIdx = pLoop->u.btree.pIndex;
142460 memset(&sWalker, 0, sizeof(sWalker));
142461 sWalker.pParse = pParse;
142462 sWalker.u.pCCurHint = &sHint;
142463 pWC = &pWInfo->sWC;
142464 for(i=0; i<pWC->nTerm; i++){
142465 pTerm = &pWC->a[i];
142466 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
142467 if( pTerm->prereqAll & pLevel->notReady ) continue;
142468
142469 /* Any terms specified as part of the ON(...) clause for any LEFT
142470 ** JOIN for which the current table is not the rhs are omitted
142471 ** from the cursor-hint.
142472 **
142473 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
142474 ** that were specified as part of the WHERE clause must be excluded.
142475 ** This is to address the following:
142476 **
142477 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
142478 **
142479 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
142480 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
142481 ** pushed down to the cursor, this row is filtered out, causing
142482 ** SQLite to synthesize a row of NULL values. Which does match the
142483 ** WHERE clause, and so the query returns a row. Which is incorrect.
142484 **
142485 ** For the same reason, WHERE terms such as:
142486 **
142487 ** WHERE 1 = (t2.c IS NULL)
142488 **
142489 ** are also excluded. See codeCursorHintIsOrFunction() for details.
142490 */
142491 if( pTabItem->fg.jointype & JT_LEFT ){
142492 Expr *pExpr = pTerm->pExpr;
142493 if( !ExprHasProperty(pExpr, EP_FromJoin)
142494 || pExpr->iRightJoinTable!=pTabItem->iCursor
142495 ){
142496 sWalker.eCode = 0;
142497 sWalker.xExprCallback = codeCursorHintIsOrFunction;
142498 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
142499 if( sWalker.eCode ) continue;
142500 }
142501 }else{
142502 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
142503 }
142504
142505 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
142506 ** the cursor. These terms are not needed as hints for a pure range
142507 ** scan (that has no == terms) so omit them. */
142508 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
142509 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
142510 if( j<pLoop->nLTerm ) continue;
142511 }
142512
142513 /* No subqueries or non-deterministic functions allowed */
142514 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
142515
142516 /* For an index scan, make sure referenced columns are actually in
142517 ** the index. */
142518 if( sHint.pIdx!=0 ){
142519 sWalker.eCode = 0;
142520 sWalker.xExprCallback = codeCursorHintCheckExpr;
142521 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
142522 if( sWalker.eCode ) continue;
142523 }
142524
142525 /* If we survive all prior tests, that means this term is worth hinting */
142526 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
142527 }
142528 if( pExpr!=0 ){
142529 sWalker.xExprCallback = codeCursorHintFixExpr;
142530 sqlite3WalkExpr(&sWalker, pExpr);
142531 sqlite3VdbeAddOp4(v, OP_CursorHint,
142532 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
142533 (const char*)pExpr, P4_EXPR);
142534 }
142535}
142536#else
142537# define codeCursorHint(A,B,C,D) /* No-op */
142538#endif /* SQLITE_ENABLE_CURSOR_HINTS */
142539
142540/*
142541** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
142542** a rowid value just read from cursor iIdxCur, open on index pIdx. This
142543** function generates code to do a deferred seek of cursor iCur to the
142544** rowid stored in register iRowid.
142545**
142546** Normally, this is just:
142547**
142548** OP_DeferredSeek $iCur $iRowid
142549**
142550** However, if the scan currently being coded is a branch of an OR-loop and
142551** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
142552** is set to iIdxCur and P4 is set to point to an array of integers
142553** containing one entry for each column of the table cursor iCur is open
142554** on. For each table column, if the column is the i'th column of the
142555** index, then the corresponding array entry is set to (i+1). If the column
142556** does not appear in the index at all, the array entry is set to 0.
142557*/
142558static void codeDeferredSeek(
142559 WhereInfo *pWInfo, /* Where clause context */
142560 Index *pIdx, /* Index scan is using */
142561 int iCur, /* Cursor for IPK b-tree */
142562 int iIdxCur /* Index cursor */
142563){
142564 Parse *pParse = pWInfo->pParse; /* Parse context */
142565 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
142566
142567 assert( iIdxCur>0 );
142568 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
142569
142570 pWInfo->bDeferredSeek = 1;
142571 sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
142572 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
142573 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
142574 ){
142575 int i;
142576 Table *pTab = pIdx->pTable;
142577 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
142578 if( ai ){
142579 ai[0] = pTab->nCol;
142580 for(i=0; i<pIdx->nColumn-1; i++){
142581 int x1, x2;
142582 assert( pIdx->aiColumn[i]<pTab->nCol );
142583 x1 = pIdx->aiColumn[i];
142584 x2 = sqlite3TableColumnToStorage(pTab, x1);
142585 testcase( x1!=x2 );
142586 if( x1>=0 ) ai[x2+1] = i+1;
142587 }
142588 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
142589 }
142590 }
142591}
142592
142593/*
142594** If the expression passed as the second argument is a vector, generate
142595** code to write the first nReg elements of the vector into an array
142596** of registers starting with iReg.
142597**
142598** If the expression is not a vector, then nReg must be passed 1. In
142599** this case, generate code to evaluate the expression and leave the
142600** result in register iReg.
142601*/
142602static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
142603 assert( nReg>0 );
142604 if( p && sqlite3ExprIsVector(p) ){
142605#ifndef SQLITE_OMIT_SUBQUERY
142606 if( (p->flags & EP_xIsSelect) ){
142607 Vdbe *v = pParse->pVdbe;
142608 int iSelect;
142609 assert( p->op==TK_SELECT );
142610 iSelect = sqlite3CodeSubselect(pParse, p);
142611 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
142612 }else
142613#endif
142614 {
142615 int i;
142616 ExprList *pList = p->x.pList;
142617 assert( nReg<=pList->nExpr );
142618 for(i=0; i<nReg; i++){
142619 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
142620 }
142621 }
142622 }else{
142623 assert( nReg==1 );
142624 sqlite3ExprCode(pParse, p, iReg);
142625 }
142626}
142627
142628/* An instance of the IdxExprTrans object carries information about a
142629** mapping from an expression on table columns into a column in an index
142630** down through the Walker.
142631*/
142632typedef struct IdxExprTrans {
142633 Expr *pIdxExpr; /* The index expression */
142634 int iTabCur; /* The cursor of the corresponding table */
142635 int iIdxCur; /* The cursor for the index */
142636 int iIdxCol; /* The column for the index */
142637 int iTabCol; /* The column for the table */
142638 WhereInfo *pWInfo; /* Complete WHERE clause information */
142639 sqlite3 *db; /* Database connection (for malloc()) */
142640} IdxExprTrans;
142641
142642/*
142643** Preserve pExpr on the WhereETrans list of the WhereInfo.
142644*/
142645static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
142646 WhereExprMod *pNew;
142647 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
142648 if( pNew==0 ) return;
142649 pNew->pNext = pTrans->pWInfo->pExprMods;
142650 pTrans->pWInfo->pExprMods = pNew;
142651 pNew->pExpr = pExpr;
142652 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
142653}
142654
142655/* The walker node callback used to transform matching expressions into
142656** a reference to an index column for an index on an expression.
142657**
142658** If pExpr matches, then transform it into a reference to the index column
142659** that contains the value of pExpr.
142660*/
142661static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
142662 IdxExprTrans *pX = p->u.pIdxTrans;
142663 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
142664 preserveExpr(pX, pExpr);
142665 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
142666 pExpr->op = TK_COLUMN;
142667 pExpr->iTable = pX->iIdxCur;
142668 pExpr->iColumn = pX->iIdxCol;
142669 pExpr->y.pTab = 0;
142670 testcase( ExprHasProperty(pExpr, EP_Skip) );
142671 testcase( ExprHasProperty(pExpr, EP_Unlikely) );
142672 ExprClearProperty(pExpr, EP_Skip|EP_Unlikely);
142673 return WRC_Prune;
142674 }else{
142675 return WRC_Continue;
142676 }
142677}
142678
142679#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142680/* A walker node callback that translates a column reference to a table
142681** into a corresponding column reference of an index.
142682*/
142683static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
142684 if( pExpr->op==TK_COLUMN ){
142685 IdxExprTrans *pX = p->u.pIdxTrans;
142686 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
142687 assert( pExpr->y.pTab!=0 );
142688 preserveExpr(pX, pExpr);
142689 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
142690 pExpr->iTable = pX->iIdxCur;
142691 pExpr->iColumn = pX->iIdxCol;
142692 pExpr->y.pTab = 0;
142693 }
142694 }
142695 return WRC_Continue;
142696}
142697#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
142698
142699/*
142700** For an indexes on expression X, locate every instance of expression X
142701** in pExpr and change that subexpression into a reference to the appropriate
142702** column of the index.
142703**
142704** 2019-10-24: Updated to also translate references to a VIRTUAL column in
142705** the table into references to the corresponding (stored) column of the
142706** index.
142707*/
142708static void whereIndexExprTrans(
142709 Index *pIdx, /* The Index */
142710 int iTabCur, /* Cursor of the table that is being indexed */
142711 int iIdxCur, /* Cursor of the index itself */
142712 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
142713){
142714 int iIdxCol; /* Column number of the index */
142715 ExprList *aColExpr; /* Expressions that are indexed */
142716 Table *pTab;
142717 Walker w;
142718 IdxExprTrans x;
142719 aColExpr = pIdx->aColExpr;
142720 if( aColExpr==0 && !pIdx->bHasVCol ){
142721 /* The index does not reference any expressions or virtual columns
142722 ** so no translations are needed. */
142723 return;
142724 }
142725 pTab = pIdx->pTable;
142726 memset(&w, 0, sizeof(w));
142727 w.u.pIdxTrans = &x;
142728 x.iTabCur = iTabCur;
142729 x.iIdxCur = iIdxCur;
142730 x.pWInfo = pWInfo;
142731 x.db = pWInfo->pParse->db;
142732 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
142733 i16 iRef = pIdx->aiColumn[iIdxCol];
142734 if( iRef==XN_EXPR ){
142735 assert( aColExpr->a[iIdxCol].pExpr!=0 );
142736 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
142737 if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue;
142738 w.xExprCallback = whereIndexExprTransNode;
142739#ifndef SQLITE_OMIT_GENERATED_COLUMNS
142740 }else if( iRef>=0
142741 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
142742 && (pTab->aCol[iRef].zColl==0
142743 || sqlite3StrICmp(pTab->aCol[iRef].zColl, sqlite3StrBINARY)==0)
142744 ){
142745 /* Check to see if there are direct references to generated columns
142746 ** that are contained in the index. Pulling the generated column
142747 ** out of the index is an optimization only - the main table is always
142748 ** available if the index cannot be used. To avoid unnecessary
142749 ** complication, omit this optimization if the collating sequence for
142750 ** the column is non-standard */
142751 x.iTabCol = iRef;
142752 w.xExprCallback = whereIndexExprTransColumn;
142753#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
142754 }else{
142755 continue;
142756 }
142757 x.iIdxCol = iIdxCol;
142758 sqlite3WalkExpr(&w, pWInfo->pWhere);
142759 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
142760 sqlite3WalkExprList(&w, pWInfo->pResultSet);
142761 }
142762}
142763
142764/*
142765** The pTruth expression is always true because it is the WHERE clause
142766** a partial index that is driving a query loop. Look through all of the
142767** WHERE clause terms on the query, and if any of those terms must be
142768** true because pTruth is true, then mark those WHERE clause terms as
142769** coded.
142770*/
142771static void whereApplyPartialIndexConstraints(
142772 Expr *pTruth,
142773 int iTabCur,
142774 WhereClause *pWC
142775){
142776 int i;
142777 WhereTerm *pTerm;
142778 while( pTruth->op==TK_AND ){
142779 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
142780 pTruth = pTruth->pRight;
142781 }
142782 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
142783 Expr *pExpr;
142784 if( pTerm->wtFlags & TERM_CODED ) continue;
142785 pExpr = pTerm->pExpr;
142786 if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
142787 pTerm->wtFlags |= TERM_CODED;
142788 }
142789 }
142790}
142791
142792/*
142793** Generate code for the start of the iLevel-th loop in the WHERE clause
142794** implementation described by pWInfo.
142795*/
142796SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
142797 Parse *pParse, /* Parsing context */
142798 Vdbe *v, /* Prepared statement under construction */
142799 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
142800 int iLevel, /* Which level of pWInfo->a[] should be coded */
142801 WhereLevel *pLevel, /* The current level pointer */
142802 Bitmask notReady /* Which tables are currently available */
142803){
142804 int j, k; /* Loop counters */
142805 int iCur; /* The VDBE cursor for the table */
142806 int addrNxt; /* Where to jump to continue with the next IN case */
142807 int bRev; /* True if we need to scan in reverse order */
142808 WhereLoop *pLoop; /* The WhereLoop object being coded */
142809 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
142810 WhereTerm *pTerm; /* A WHERE clause term */
142811 sqlite3 *db; /* Database connection */
142812 struct SrcList_item *pTabItem; /* FROM clause term being coded */
142813 int addrBrk; /* Jump here to break out of the loop */
142814 int addrHalt; /* addrBrk for the outermost loop */
142815 int addrCont; /* Jump here to continue with next cycle */
142816 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
142817 int iReleaseReg = 0; /* Temp register to free before returning */
142818 Index *pIdx = 0; /* Index used by loop (if any) */
142819 int iLoop; /* Iteration of constraint generator loop */
142820
142821 pWC = &pWInfo->sWC;
142822 db = pParse->db;
142823 pLoop = pLevel->pWLoop;
142824 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
142825 iCur = pTabItem->iCursor;
142826 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
142827 bRev = (pWInfo->revMask>>iLevel)&1;
142828 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
142829#if WHERETRACE_ENABLED /* 0x20800 */
142830 if( sqlite3WhereTrace & 0x800 ){
142831 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
142832 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
142833 sqlite3WhereLoopPrint(pLoop, pWC);
142834 }
142835 if( sqlite3WhereTrace & 0x20000 ){
142836 if( iLevel==0 ){
142837 sqlite3DebugPrintf("WHERE clause being coded:\n");
142838 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
142839 }
142840 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
142841 sqlite3WhereClausePrint(pWC);
142842 }
142843#endif
142844
142845 /* Create labels for the "break" and "continue" instructions
142846 ** for the current loop. Jump to addrBrk to break out of a loop.
142847 ** Jump to cont to go immediately to the next iteration of the
142848 ** loop.
142849 **
142850 ** When there is an IN operator, we also have a "addrNxt" label that
142851 ** means to continue with the next IN value combination. When
142852 ** there are no IN operators in the constraints, the "addrNxt" label
142853 ** is the same as "addrBrk".
142854 */
142855 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
142856 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
142857
142858 /* If this is the right table of a LEFT OUTER JOIN, allocate and
142859 ** initialize a memory cell that records if this table matches any
142860 ** row of the left table of the join.
142861 */
142862 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
142863 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
142864 );
142865 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
142866 pLevel->iLeftJoin = ++pParse->nMem;
142867 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
142868 VdbeComment((v, "init LEFT JOIN no-match flag"));
142869 }
142870
142871 /* Compute a safe address to jump to if we discover that the table for
142872 ** this loop is empty and can never contribute content. */
142873 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
142874 addrHalt = pWInfo->a[j].addrBrk;
142875
142876 /* Special case of a FROM clause subquery implemented as a co-routine */
142877 if( pTabItem->fg.viaCoroutine ){
142878 int regYield = pTabItem->regReturn;
142879 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
142880 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
142881 VdbeCoverage(v);
142882 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
142883 pLevel->op = OP_Goto;
142884 }else
142885
142886#ifndef SQLITE_OMIT_VIRTUALTABLE
142887 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
142888 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
142889 ** to access the data.
142890 */
142891 int iReg; /* P3 Value for OP_VFilter */
142892 int addrNotFound;
142893 int nConstraint = pLoop->nLTerm;
142894 int iIn; /* Counter for IN constraints */
142895
142896 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
142897 addrNotFound = pLevel->addrBrk;
142898 for(j=0; j<nConstraint; j++){
142899 int iTarget = iReg+j+2;
142900 pTerm = pLoop->aLTerm[j];
142901 if( NEVER(pTerm==0) ) continue;
142902 if( pTerm->eOperator & WO_IN ){
142903 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
142904 addrNotFound = pLevel->addrNxt;
142905 }else{
142906 Expr *pRight = pTerm->pExpr->pRight;
142907 codeExprOrVector(pParse, pRight, iTarget, 1);
142908 }
142909 }
142910 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
142911 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
142912 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
142913 pLoop->u.vtab.idxStr,
142914 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
142915 VdbeCoverage(v);
142916 pLoop->u.vtab.needFree = 0;
142917 pLevel->p1 = iCur;
142918 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
142919 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
142920 iIn = pLevel->u.in.nIn;
142921 for(j=nConstraint-1; j>=0; j--){
142922 pTerm = pLoop->aLTerm[j];
142923 if( (pTerm->eOperator & WO_IN)!=0 ) iIn--;
142924 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
142925 disableTerm(pLevel, pTerm);
142926 }else if( (pTerm->eOperator & WO_IN)!=0
142927 && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1
142928 ){
142929 Expr *pCompare; /* The comparison operator */
142930 Expr *pRight; /* RHS of the comparison */
142931 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
142932
142933 /* Reload the constraint value into reg[iReg+j+2]. The same value
142934 ** was loaded into the same register prior to the OP_VFilter, but
142935 ** the xFilter implementation might have changed the datatype or
142936 ** encoding of the value in the register, so it *must* be reloaded. */
142937 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
142938 if( !db->mallocFailed ){
142939 assert( iIn>=0 && iIn<pLevel->u.in.nIn );
142940 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
142941 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
142942 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
142943 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
142944 testcase( pOp->opcode==OP_Rowid );
142945 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
142946 }
142947
142948 /* Generate code that will continue to the next row if
142949 ** the IN constraint is not satisfied */
142950 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
142951 assert( pCompare!=0 || db->mallocFailed );
142952 if( pCompare ){
142953 pCompare->pLeft = pTerm->pExpr->pLeft;
142954 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
142955 if( pRight ){
142956 pRight->iTable = iReg+j+2;
142957 sqlite3ExprIfFalse(
142958 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
142959 );
142960 }
142961 pCompare->pLeft = 0;
142962 sqlite3ExprDelete(db, pCompare);
142963 }
142964 }
142965 }
142966 assert( iIn==0 || db->mallocFailed );
142967 /* These registers need to be preserved in case there is an IN operator
142968 ** loop. So we could deallocate the registers here (and potentially
142969 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
142970 ** simpler and safer to simply not reuse the registers.
142971 **
142972 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
142973 */
142974 }else
142975#endif /* SQLITE_OMIT_VIRTUALTABLE */
142976
142977 if( (pLoop->wsFlags & WHERE_IPK)!=0
142978 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
142979 ){
142980 /* Case 2: We can directly reference a single row using an
142981 ** equality comparison against the ROWID field. Or
142982 ** we reference multiple rows using a "rowid IN (...)"
142983 ** construct.
142984 */
142985 assert( pLoop->u.btree.nEq==1 );
142986 pTerm = pLoop->aLTerm[0];
142987 assert( pTerm!=0 );
142988 assert( pTerm->pExpr!=0 );
142989 testcase( pTerm->wtFlags & TERM_VIRTUAL );
142990 iReleaseReg = ++pParse->nMem;
142991 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
142992 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
142993 addrNxt = pLevel->addrNxt;
142994 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
142995 VdbeCoverage(v);
142996 pLevel->op = OP_Noop;
142997 if( (pTerm->prereqAll & pLevel->notReady)==0 ){
142998 pTerm->wtFlags |= TERM_CODED;
142999 }
143000 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
143001 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
143002 ){
143003 /* Case 3: We have an inequality comparison against the ROWID field.
143004 */
143005 int testOp = OP_Noop;
143006 int start;
143007 int memEndValue = 0;
143008 WhereTerm *pStart, *pEnd;
143009
143010 j = 0;
143011 pStart = pEnd = 0;
143012 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
143013 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
143014 assert( pStart!=0 || pEnd!=0 );
143015 if( bRev ){
143016 pTerm = pStart;
143017 pStart = pEnd;
143018 pEnd = pTerm;
143019 }
143020 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
143021 if( pStart ){
143022 Expr *pX; /* The expression that defines the start bound */
143023 int r1, rTemp; /* Registers for holding the start boundary */
143024 int op; /* Cursor seek operation */
143025
143026 /* The following constant maps TK_xx codes into corresponding
143027 ** seek opcodes. It depends on a particular ordering of TK_xx
143028 */
143029 const u8 aMoveOp[] = {
143030 /* TK_GT */ OP_SeekGT,
143031 /* TK_LE */ OP_SeekLE,
143032 /* TK_LT */ OP_SeekLT,
143033 /* TK_GE */ OP_SeekGE
143034 };
143035 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
143036 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
143037 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
143038
143039 assert( (pStart->wtFlags & TERM_VNULL)==0 );
143040 testcase( pStart->wtFlags & TERM_VIRTUAL );
143041 pX = pStart->pExpr;
143042 assert( pX!=0 );
143043 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
143044 if( sqlite3ExprIsVector(pX->pRight) ){
143045 r1 = rTemp = sqlite3GetTempReg(pParse);
143046 codeExprOrVector(pParse, pX->pRight, r1, 1);
143047 testcase( pX->op==TK_GT );
143048 testcase( pX->op==TK_GE );
143049 testcase( pX->op==TK_LT );
143050 testcase( pX->op==TK_LE );
143051 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
143052 assert( pX->op!=TK_GT || op==OP_SeekGE );
143053 assert( pX->op!=TK_GE || op==OP_SeekGE );
143054 assert( pX->op!=TK_LT || op==OP_SeekLE );
143055 assert( pX->op!=TK_LE || op==OP_SeekLE );
143056 }else{
143057 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
143058 disableTerm(pLevel, pStart);
143059 op = aMoveOp[(pX->op - TK_GT)];
143060 }
143061 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
143062 VdbeComment((v, "pk"));
143063 VdbeCoverageIf(v, pX->op==TK_GT);
143064 VdbeCoverageIf(v, pX->op==TK_LE);
143065 VdbeCoverageIf(v, pX->op==TK_LT);
143066 VdbeCoverageIf(v, pX->op==TK_GE);
143067 sqlite3ReleaseTempReg(pParse, rTemp);
143068 }else{
143069 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
143070 VdbeCoverageIf(v, bRev==0);
143071 VdbeCoverageIf(v, bRev!=0);
143072 }
143073 if( pEnd ){
143074 Expr *pX;
143075 pX = pEnd->pExpr;
143076 assert( pX!=0 );
143077 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
143078 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
143079 testcase( pEnd->wtFlags & TERM_VIRTUAL );
143080 memEndValue = ++pParse->nMem;
143081 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
143082 if( 0==sqlite3ExprIsVector(pX->pRight)
143083 && (pX->op==TK_LT || pX->op==TK_GT)
143084 ){
143085 testOp = bRev ? OP_Le : OP_Ge;
143086 }else{
143087 testOp = bRev ? OP_Lt : OP_Gt;
143088 }
143089 if( 0==sqlite3ExprIsVector(pX->pRight) ){
143090 disableTerm(pLevel, pEnd);
143091 }
143092 }
143093 start = sqlite3VdbeCurrentAddr(v);
143094 pLevel->op = bRev ? OP_Prev : OP_Next;
143095 pLevel->p1 = iCur;
143096 pLevel->p2 = start;
143097 assert( pLevel->p5==0 );
143098 if( testOp!=OP_Noop ){
143099 iRowidReg = ++pParse->nMem;
143100 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
143101 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
143102 VdbeCoverageIf(v, testOp==OP_Le);
143103 VdbeCoverageIf(v, testOp==OP_Lt);
143104 VdbeCoverageIf(v, testOp==OP_Ge);
143105 VdbeCoverageIf(v, testOp==OP_Gt);
143106 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
143107 }
143108 }else if( pLoop->wsFlags & WHERE_INDEXED ){
143109 /* Case 4: A scan using an index.
143110 **
143111 ** The WHERE clause may contain zero or more equality
143112 ** terms ("==" or "IN" operators) that refer to the N
143113 ** left-most columns of the index. It may also contain
143114 ** inequality constraints (>, <, >= or <=) on the indexed
143115 ** column that immediately follows the N equalities. Only
143116 ** the right-most column can be an inequality - the rest must
143117 ** use the "==" and "IN" operators. For example, if the
143118 ** index is on (x,y,z), then the following clauses are all
143119 ** optimized:
143120 **
143121 ** x=5
143122 ** x=5 AND y=10
143123 ** x=5 AND y<10
143124 ** x=5 AND y>5 AND y<10
143125 ** x=5 AND y=5 AND z<=10
143126 **
143127 ** The z<10 term of the following cannot be used, only
143128 ** the x=5 term:
143129 **
143130 ** x=5 AND z<10
143131 **
143132 ** N may be zero if there are inequality constraints.
143133 ** If there are no inequality constraints, then N is at
143134 ** least one.
143135 **
143136 ** This case is also used when there are no WHERE clause
143137 ** constraints but an index is selected anyway, in order
143138 ** to force the output order to conform to an ORDER BY.
143139 */
143140 static const u8 aStartOp[] = {
143141 0,
143142 0,
143143 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
143144 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
143145 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
143146 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
143147 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
143148 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
143149 };
143150 static const u8 aEndOp[] = {
143151 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
143152 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
143153 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
143154 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
143155 };
143156 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
143157 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
143158 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
143159 int regBase; /* Base register holding constraint values */
143160 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
143161 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
143162 int startEq; /* True if range start uses ==, >= or <= */
143163 int endEq; /* True if range end uses ==, >= or <= */
143164 int start_constraints; /* Start of range is constrained */
143165 int nConstraint; /* Number of constraint terms */
143166 int iIdxCur; /* The VDBE cursor for the index */
143167 int nExtraReg = 0; /* Number of extra registers needed */
143168 int op; /* Instruction opcode */
143169 char *zStartAff; /* Affinity for start of range constraint */
143170 char *zEndAff = 0; /* Affinity for end of range constraint */
143171 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
143172 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
143173 int omitTable; /* True if we use the index only */
143174 int regBignull = 0; /* big-null flag register */
143175
143176 pIdx = pLoop->u.btree.pIndex;
143177 iIdxCur = pLevel->iIdxCur;
143178 assert( nEq>=pLoop->nSkip );
143179
143180 /* Find any inequality constraint terms for the start and end
143181 ** of the range.
143182 */
143183 j = nEq;
143184 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
143185 pRangeStart = pLoop->aLTerm[j++];
143186 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
143187 /* Like optimization range constraints always occur in pairs */
143188 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
143189 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
143190 }
143191 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
143192 pRangeEnd = pLoop->aLTerm[j++];
143193 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
143194#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
143195 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
143196 assert( pRangeStart!=0 ); /* LIKE opt constraints */
143197 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
143198 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
143199 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
143200 VdbeComment((v, "LIKE loop counter"));
143201 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
143202 /* iLikeRepCntr actually stores 2x the counter register number. The
143203 ** bottom bit indicates whether the search order is ASC or DESC. */
143204 testcase( bRev );
143205 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
143206 assert( (bRev & ~1)==0 );
143207 pLevel->iLikeRepCntr <<=1;
143208 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
143209 }
143210#endif
143211 if( pRangeStart==0 ){
143212 j = pIdx->aiColumn[nEq];
143213 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
143214 bSeekPastNull = 1;
143215 }
143216 }
143217 }
143218 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
143219
143220 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
143221 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
143222 ** FIRST). In both cases separate ordered scans are made of those
143223 ** index entries for which the column is null and for those for which
143224 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
143225 ** For DESC, NULL entries are scanned first.
143226 */
143227 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
143228 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
143229 ){
143230 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 );
143231 assert( pRangeEnd==0 && pRangeStart==0 );
143232 testcase( pLoop->nSkip>0 );
143233 nExtraReg = 1;
143234 bSeekPastNull = 1;
143235 pLevel->regBignull = regBignull = ++pParse->nMem;
143236 if( pLevel->iLeftJoin ){
143237 sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull);
143238 }
143239 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
143240 }
143241
143242 /* If we are doing a reverse order scan on an ascending index, or
143243 ** a forward order scan on a descending index, interchange the
143244 ** start and end terms (pRangeStart and pRangeEnd).
143245 */
143246 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
143247 || (bRev && pIdx->nKeyCol==nEq)
143248 ){
143249 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
143250 SWAP(u8, bSeekPastNull, bStopAtNull);
143251 SWAP(u8, nBtm, nTop);
143252 }
143253
143254 /* Generate code to evaluate all constraint terms using == or IN
143255 ** and store the values of those terms in an array of registers
143256 ** starting at regBase.
143257 */
143258 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
143259 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
143260 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
143261 if( zStartAff && nTop ){
143262 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
143263 }
143264 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
143265
143266 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
143267 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
143268 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
143269 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
143270 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
143271 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
143272 start_constraints = pRangeStart || nEq>0;
143273
143274 /* Seek the index cursor to the start of the range. */
143275 nConstraint = nEq;
143276 if( pRangeStart ){
143277 Expr *pRight = pRangeStart->pExpr->pRight;
143278 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
143279 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
143280 if( (pRangeStart->wtFlags & TERM_VNULL)==0
143281 && sqlite3ExprCanBeNull(pRight)
143282 ){
143283 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
143284 VdbeCoverage(v);
143285 }
143286 if( zStartAff ){
143287 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
143288 }
143289 nConstraint += nBtm;
143290 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
143291 if( sqlite3ExprIsVector(pRight)==0 ){
143292 disableTerm(pLevel, pRangeStart);
143293 }else{
143294 startEq = 1;
143295 }
143296 bSeekPastNull = 0;
143297 }else if( bSeekPastNull ){
143298 startEq = 0;
143299 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
143300 start_constraints = 1;
143301 nConstraint++;
143302 }else if( regBignull ){
143303 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
143304 start_constraints = 1;
143305 nConstraint++;
143306 }
143307 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
143308 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
143309 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
143310 ** above has already left the cursor sitting on the correct row,
143311 ** so no further seeking is needed */
143312 }else{
143313 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
143314 sqlite3VdbeAddOp1(v, OP_SeekHit, iIdxCur);
143315 }
143316 if( regBignull ){
143317 sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull);
143318 VdbeComment((v, "NULL-scan pass ctr"));
143319 }
143320
143321 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
143322 assert( op!=0 );
143323 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
143324 VdbeCoverage(v);
143325 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
143326 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
143327 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
143328 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
143329 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
143330 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
143331
143332 assert( bSeekPastNull==0 || bStopAtNull==0 );
143333 if( regBignull ){
143334 assert( bSeekPastNull==1 || bStopAtNull==1 );
143335 assert( bSeekPastNull==!bStopAtNull );
143336 assert( bStopAtNull==startEq );
143337 sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2);
143338 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
143339 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
143340 nConstraint-startEq);
143341 VdbeCoverage(v);
143342 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
143343 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
143344 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
143345 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
143346 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE);
143347 }
143348 }
143349
143350 /* Load the value for the inequality constraint at the end of the
143351 ** range (if any).
143352 */
143353 nConstraint = nEq;
143354 if( pRangeEnd ){
143355 Expr *pRight = pRangeEnd->pExpr->pRight;
143356 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
143357 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
143358 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
143359 && sqlite3ExprCanBeNull(pRight)
143360 ){
143361 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
143362 VdbeCoverage(v);
143363 }
143364 if( zEndAff ){
143365 updateRangeAffinityStr(pRight, nTop, zEndAff);
143366 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
143367 }else{
143368 assert( pParse->db->mallocFailed );
143369 }
143370 nConstraint += nTop;
143371 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
143372
143373 if( sqlite3ExprIsVector(pRight)==0 ){
143374 disableTerm(pLevel, pRangeEnd);
143375 }else{
143376 endEq = 1;
143377 }
143378 }else if( bStopAtNull ){
143379 if( regBignull==0 ){
143380 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
143381 endEq = 0;
143382 }
143383 nConstraint++;
143384 }
143385 sqlite3DbFree(db, zStartAff);
143386 sqlite3DbFree(db, zEndAff);
143387
143388 /* Top of the loop body */
143389 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
143390
143391 /* Check if the index cursor is past the end of the range. */
143392 if( nConstraint ){
143393 if( regBignull ){
143394 /* Except, skip the end-of-range check while doing the NULL-scan */
143395 sqlite3VdbeAddOp2(v, OP_IfNot, regBignull, sqlite3VdbeCurrentAddr(v)+3);
143396 VdbeComment((v, "If NULL-scan 2nd pass"));
143397 VdbeCoverage(v);
143398 }
143399 op = aEndOp[bRev*2 + endEq];
143400 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
143401 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
143402 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
143403 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
143404 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
143405 }
143406 if( regBignull ){
143407 /* During a NULL-scan, check to see if we have reached the end of
143408 ** the NULLs */
143409 assert( bSeekPastNull==!bStopAtNull );
143410 assert( bSeekPastNull+bStopAtNull==1 );
143411 assert( nConstraint+bSeekPastNull>0 );
143412 sqlite3VdbeAddOp2(v, OP_If, regBignull, sqlite3VdbeCurrentAddr(v)+2);
143413 VdbeComment((v, "If NULL-scan 1st pass"));
143414 VdbeCoverage(v);
143415 op = aEndOp[bRev*2 + bSeekPastNull];
143416 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
143417 nConstraint+bSeekPastNull);
143418 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
143419 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
143420 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
143421 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
143422 }
143423
143424 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
143425 sqlite3VdbeAddOp2(v, OP_SeekHit, iIdxCur, 1);
143426 }
143427
143428 /* Seek the table cursor, if required */
143429 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
143430 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
143431 if( omitTable ){
143432 /* pIdx is a covering index. No need to access the main table. */
143433 }else if( HasRowid(pIdx->pTable) ){
143434 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)
143435 || ( (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)!=0
143436 && (pWInfo->eOnePass==ONEPASS_SINGLE || pLoop->nLTerm==0) )
143437 ){
143438 iRowidReg = ++pParse->nMem;
143439 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
143440 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
143441 VdbeCoverage(v);
143442 }else{
143443 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
143444 }
143445 }else if( iCur!=iIdxCur ){
143446 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
143447 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
143448 for(j=0; j<pPk->nKeyCol; j++){
143449 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
143450 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
143451 }
143452 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
143453 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
143454 }
143455
143456 if( pLevel->iLeftJoin==0 ){
143457 /* If pIdx is an index on one or more expressions, then look through
143458 ** all the expressions in pWInfo and try to transform matching expressions
143459 ** into reference to index columns. Also attempt to translate references
143460 ** to virtual columns in the table into references to (stored) columns
143461 ** of the index.
143462 **
143463 ** Do not do this for the RHS of a LEFT JOIN. This is because the
143464 ** expression may be evaluated after OP_NullRow has been executed on
143465 ** the cursor. In this case it is important to do the full evaluation,
143466 ** as the result of the expression may not be NULL, even if all table
143467 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
143468 **
143469 ** Also, do not do this when processing one index an a multi-index
143470 ** OR clause, since the transformation will become invalid once we
143471 ** move forward to the next index.
143472 ** https://sqlite.org/src/info/4e8e4857d32d401f
143473 */
143474 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
143475 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
143476 }
143477
143478 /* If a partial index is driving the loop, try to eliminate WHERE clause
143479 ** terms from the query that must be true due to the WHERE clause of
143480 ** the partial index.
143481 **
143482 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
143483 ** for a LEFT JOIN.
143484 */
143485 if( pIdx->pPartIdxWhere ){
143486 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
143487 }
143488 }else{
143489 testcase( pIdx->pPartIdxWhere );
143490 /* The following assert() is not a requirement, merely an observation:
143491 ** The OR-optimization doesn't work for the right hand table of
143492 ** a LEFT JOIN: */
143493 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 );
143494 }
143495
143496 /* Record the instruction used to terminate the loop. */
143497 if( pLoop->wsFlags & WHERE_ONEROW ){
143498 pLevel->op = OP_Noop;
143499 }else if( bRev ){
143500 pLevel->op = OP_Prev;
143501 }else{
143502 pLevel->op = OP_Next;
143503 }
143504 pLevel->p1 = iIdxCur;
143505 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
143506 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
143507 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
143508 }else{
143509 assert( pLevel->p5==0 );
143510 }
143511 if( omitTable ) pIdx = 0;
143512 }else
143513
143514#ifndef SQLITE_OMIT_OR_OPTIMIZATION
143515 if( pLoop->wsFlags & WHERE_MULTI_OR ){
143516 /* Case 5: Two or more separately indexed terms connected by OR
143517 **
143518 ** Example:
143519 **
143520 ** CREATE TABLE t1(a,b,c,d);
143521 ** CREATE INDEX i1 ON t1(a);
143522 ** CREATE INDEX i2 ON t1(b);
143523 ** CREATE INDEX i3 ON t1(c);
143524 **
143525 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
143526 **
143527 ** In the example, there are three indexed terms connected by OR.
143528 ** The top of the loop looks like this:
143529 **
143530 ** Null 1 # Zero the rowset in reg 1
143531 **
143532 ** Then, for each indexed term, the following. The arguments to
143533 ** RowSetTest are such that the rowid of the current row is inserted
143534 ** into the RowSet. If it is already present, control skips the
143535 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
143536 **
143537 ** sqlite3WhereBegin(<term>)
143538 ** RowSetTest # Insert rowid into rowset
143539 ** Gosub 2 A
143540 ** sqlite3WhereEnd()
143541 **
143542 ** Following the above, code to terminate the loop. Label A, the target
143543 ** of the Gosub above, jumps to the instruction right after the Goto.
143544 **
143545 ** Null 1 # Zero the rowset in reg 1
143546 ** Goto B # The loop is finished.
143547 **
143548 ** A: <loop body> # Return data, whatever.
143549 **
143550 ** Return 2 # Jump back to the Gosub
143551 **
143552 ** B: <after the loop>
143553 **
143554 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
143555 ** use an ephemeral index instead of a RowSet to record the primary
143556 ** keys of the rows we have already seen.
143557 **
143558 */
143559 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
143560 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
143561 Index *pCov = 0; /* Potential covering index (or NULL) */
143562 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
143563
143564 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
143565 int regRowset = 0; /* Register for RowSet object */
143566 int regRowid = 0; /* Register holding rowid */
143567 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
143568 int iRetInit; /* Address of regReturn init */
143569 int untestedTerms = 0; /* Some terms not completely tested */
143570 int ii; /* Loop counter */
143571 u16 wctrlFlags; /* Flags for sub-WHERE clause */
143572 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
143573 Table *pTab = pTabItem->pTab;
143574
143575 pTerm = pLoop->aLTerm[0];
143576 assert( pTerm!=0 );
143577 assert( pTerm->eOperator & WO_OR );
143578 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
143579 pOrWc = &pTerm->u.pOrInfo->wc;
143580 pLevel->op = OP_Return;
143581 pLevel->p1 = regReturn;
143582
143583 /* Set up a new SrcList in pOrTab containing the table being scanned
143584 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
143585 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
143586 */
143587 if( pWInfo->nLevel>1 ){
143588 int nNotReady; /* The number of notReady tables */
143589 struct SrcList_item *origSrc; /* Original list of tables */
143590 nNotReady = pWInfo->nLevel - iLevel - 1;
143591 pOrTab = sqlite3StackAllocRaw(db,
143592 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
143593 if( pOrTab==0 ) return notReady;
143594 pOrTab->nAlloc = (u8)(nNotReady + 1);
143595 pOrTab->nSrc = pOrTab->nAlloc;
143596 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
143597 origSrc = pWInfo->pTabList->a;
143598 for(k=1; k<=nNotReady; k++){
143599 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
143600 }
143601 }else{
143602 pOrTab = pWInfo->pTabList;
143603 }
143604
143605 /* Initialize the rowset register to contain NULL. An SQL NULL is
143606 ** equivalent to an empty rowset. Or, create an ephemeral index
143607 ** capable of holding primary keys in the case of a WITHOUT ROWID.
143608 **
143609 ** Also initialize regReturn to contain the address of the instruction
143610 ** immediately following the OP_Return at the bottom of the loop. This
143611 ** is required in a few obscure LEFT JOIN cases where control jumps
143612 ** over the top of the loop into the body of it. In this case the
143613 ** correct response for the end-of-loop code (the OP_Return) is to
143614 ** fall through to the next instruction, just as an OP_Next does if
143615 ** called on an uninitialized cursor.
143616 */
143617 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
143618 if( HasRowid(pTab) ){
143619 regRowset = ++pParse->nMem;
143620 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
143621 }else{
143622 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
143623 regRowset = pParse->nTab++;
143624 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
143625 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
143626 }
143627 regRowid = ++pParse->nMem;
143628 }
143629 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
143630
143631 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
143632 ** Then for every term xN, evaluate as the subexpression: xN AND z
143633 ** That way, terms in y that are factored into the disjunction will
143634 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
143635 **
143636 ** Actually, each subexpression is converted to "xN AND w" where w is
143637 ** the "interesting" terms of z - terms that did not originate in the
143638 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
143639 ** indices.
143640 **
143641 ** This optimization also only applies if the (x1 OR x2 OR ...) term
143642 ** is not contained in the ON clause of a LEFT JOIN.
143643 ** See ticket http://www.sqlite.org/src/info/f2369304e4
143644 */
143645 if( pWC->nTerm>1 ){
143646 int iTerm;
143647 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
143648 Expr *pExpr = pWC->a[iTerm].pExpr;
143649 if( &pWC->a[iTerm] == pTerm ) continue;
143650 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
143651 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
143652 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
143653 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
143654 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
143655 pExpr = sqlite3ExprDup(db, pExpr, 0);
143656 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
143657 }
143658 if( pAndExpr ){
143659 /* The extra 0x10000 bit on the opcode is masked off and does not
143660 ** become part of the new Expr.op. However, it does make the
143661 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
143662 ** prevents sqlite3PExpr() from implementing AND short-circuit
143663 ** optimization, which we do not want here. */
143664 pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
143665 }
143666 }
143667
143668 /* Run a separate WHERE clause for each term of the OR clause. After
143669 ** eliminating duplicates from other WHERE clauses, the action for each
143670 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
143671 */
143672 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
143673 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
143674 for(ii=0; ii<pOrWc->nTerm; ii++){
143675 WhereTerm *pOrTerm = &pOrWc->a[ii];
143676 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
143677 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
143678 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
143679 int jmp1 = 0; /* Address of jump operation */
143680 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
143681 && !ExprHasProperty(pOrExpr, EP_FromJoin)
143682 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
143683 if( pAndExpr ){
143684 pAndExpr->pLeft = pOrExpr;
143685 pOrExpr = pAndExpr;
143686 }
143687 /* Loop through table entries that match term pOrTerm. */
143688 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
143689 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
143690 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
143691 wctrlFlags, iCovCur);
143692 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
143693 if( pSubWInfo ){
143694 WhereLoop *pSubLoop;
143695 int addrExplain = sqlite3WhereExplainOneScan(
143696 pParse, pOrTab, &pSubWInfo->a[0], 0
143697 );
143698 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
143699
143700 /* This is the sub-WHERE clause body. First skip over
143701 ** duplicate rows from prior sub-WHERE clauses, and record the
143702 ** rowid (or PRIMARY KEY) for the current row so that the same
143703 ** row will be skipped in subsequent sub-WHERE clauses.
143704 */
143705 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
143706 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
143707 if( HasRowid(pTab) ){
143708 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
143709 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
143710 regRowid, iSet);
143711 VdbeCoverage(v);
143712 }else{
143713 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
143714 int nPk = pPk->nKeyCol;
143715 int iPk;
143716 int r;
143717
143718 /* Read the PK into an array of temp registers. */
143719 r = sqlite3GetTempRange(pParse, nPk);
143720 for(iPk=0; iPk<nPk; iPk++){
143721 int iCol = pPk->aiColumn[iPk];
143722 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
143723 }
143724
143725 /* Check if the temp table already contains this key. If so,
143726 ** the row has already been included in the result set and
143727 ** can be ignored (by jumping past the Gosub below). Otherwise,
143728 ** insert the key into the temp table and proceed with processing
143729 ** the row.
143730 **
143731 ** Use some of the same optimizations as OP_RowSetTest: If iSet
143732 ** is zero, assume that the key cannot already be present in
143733 ** the temp table. And if iSet is -1, assume that there is no
143734 ** need to insert the key into the temp table, as it will never
143735 ** be tested for. */
143736 if( iSet ){
143737 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
143738 VdbeCoverage(v);
143739 }
143740 if( iSet>=0 ){
143741 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
143742 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
143743 r, nPk);
143744 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
143745 }
143746
143747 /* Release the array of temp registers */
143748 sqlite3ReleaseTempRange(pParse, r, nPk);
143749 }
143750 }
143751
143752 /* Invoke the main loop body as a subroutine */
143753 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
143754
143755 /* Jump here (skipping the main loop body subroutine) if the
143756 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
143757 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
143758
143759 /* The pSubWInfo->untestedTerms flag means that this OR term
143760 ** contained one or more AND term from a notReady table. The
143761 ** terms from the notReady table could not be tested and will
143762 ** need to be tested later.
143763 */
143764 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
143765
143766 /* If all of the OR-connected terms are optimized using the same
143767 ** index, and the index is opened using the same cursor number
143768 ** by each call to sqlite3WhereBegin() made by this loop, it may
143769 ** be possible to use that index as a covering index.
143770 **
143771 ** If the call to sqlite3WhereBegin() above resulted in a scan that
143772 ** uses an index, and this is either the first OR-connected term
143773 ** processed or the index is the same as that used by all previous
143774 ** terms, set pCov to the candidate covering index. Otherwise, set
143775 ** pCov to NULL to indicate that no candidate covering index will
143776 ** be available.
143777 */
143778 pSubLoop = pSubWInfo->a[0].pWLoop;
143779 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
143780 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
143781 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
143782 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
143783 ){
143784 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
143785 pCov = pSubLoop->u.btree.pIndex;
143786 }else{
143787 pCov = 0;
143788 }
143789
143790 /* Finish the loop through table entries that match term pOrTerm. */
143791 sqlite3WhereEnd(pSubWInfo);
143792 ExplainQueryPlanPop(pParse);
143793 }
143794 }
143795 }
143796 ExplainQueryPlanPop(pParse);
143797 pLevel->u.pCovidx = pCov;
143798 if( pCov ) pLevel->iIdxCur = iCovCur;
143799 if( pAndExpr ){
143800 pAndExpr->pLeft = 0;
143801 sqlite3ExprDelete(db, pAndExpr);
143802 }
143803 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
143804 sqlite3VdbeGoto(v, pLevel->addrBrk);
143805 sqlite3VdbeResolveLabel(v, iLoopBody);
143806
143807 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
143808 if( !untestedTerms ) disableTerm(pLevel, pTerm);
143809 }else
143810#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
143811
143812 {
143813 /* Case 6: There is no usable index. We must do a complete
143814 ** scan of the entire table.
143815 */
143816 static const u8 aStep[] = { OP_Next, OP_Prev };
143817 static const u8 aStart[] = { OP_Rewind, OP_Last };
143818 assert( bRev==0 || bRev==1 );
143819 if( pTabItem->fg.isRecursive ){
143820 /* Tables marked isRecursive have only a single row that is stored in
143821 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
143822 pLevel->op = OP_Noop;
143823 }else{
143824 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
143825 pLevel->op = aStep[bRev];
143826 pLevel->p1 = iCur;
143827 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
143828 VdbeCoverageIf(v, bRev==0);
143829 VdbeCoverageIf(v, bRev!=0);
143830 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
143831 }
143832 }
143833
143834#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
143835 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
143836#endif
143837
143838 /* Insert code to test every subexpression that can be completely
143839 ** computed using the current set of tables.
143840 **
143841 ** This loop may run between one and three times, depending on the
143842 ** constraints to be generated. The value of stack variable iLoop
143843 ** determines the constraints coded by each iteration, as follows:
143844 **
143845 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
143846 ** iLoop==2: Code remaining expressions that do not contain correlated
143847 ** sub-queries.
143848 ** iLoop==3: Code all remaining expressions.
143849 **
143850 ** An effort is made to skip unnecessary iterations of the loop.
143851 */
143852 iLoop = (pIdx ? 1 : 2);
143853 do{
143854 int iNext = 0; /* Next value for iLoop */
143855 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
143856 Expr *pE;
143857 int skipLikeAddr = 0;
143858 testcase( pTerm->wtFlags & TERM_VIRTUAL );
143859 testcase( pTerm->wtFlags & TERM_CODED );
143860 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143861 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
143862 testcase( pWInfo->untestedTerms==0
143863 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
143864 pWInfo->untestedTerms = 1;
143865 continue;
143866 }
143867 pE = pTerm->pExpr;
143868 assert( pE!=0 );
143869 if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
143870 continue;
143871 }
143872
143873 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
143874 iNext = 2;
143875 continue;
143876 }
143877 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
143878 if( iNext==0 ) iNext = 3;
143879 continue;
143880 }
143881
143882 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
143883 /* If the TERM_LIKECOND flag is set, that means that the range search
143884 ** is sufficient to guarantee that the LIKE operator is true, so we
143885 ** can skip the call to the like(A,B) function. But this only works
143886 ** for strings. So do not skip the call to the function on the pass
143887 ** that compares BLOBs. */
143888#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
143889 continue;
143890#else
143891 u32 x = pLevel->iLikeRepCntr;
143892 if( x>0 ){
143893 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
143894 VdbeCoverageIf(v, (x&1)==1);
143895 VdbeCoverageIf(v, (x&1)==0);
143896 }
143897#endif
143898 }
143899#ifdef WHERETRACE_ENABLED /* 0xffff */
143900 if( sqlite3WhereTrace ){
143901 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
143902 pWC->nTerm-j, pTerm, iLoop));
143903 }
143904 if( sqlite3WhereTrace & 0x800 ){
143905 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
143906 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
143907 }
143908#endif
143909 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
143910 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
143911 pTerm->wtFlags |= TERM_CODED;
143912 }
143913 iLoop = iNext;
143914 }while( iLoop>0 );
143915
143916 /* Insert code to test for implied constraints based on transitivity
143917 ** of the "==" operator.
143918 **
143919 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
143920 ** and we are coding the t1 loop and the t2 loop has not yet coded,
143921 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
143922 ** the implied "t1.a=123" constraint.
143923 */
143924 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
143925 Expr *pE, sEAlt;
143926 WhereTerm *pAlt;
143927 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143928 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
143929 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
143930 if( pTerm->leftCursor!=iCur ) continue;
143931 if( pTabItem->fg.jointype & JT_LEFT ) continue;
143932 pE = pTerm->pExpr;
143933#ifdef WHERETRACE_ENABLED /* 0x800 */
143934 if( sqlite3WhereTrace & 0x800 ){
143935 sqlite3DebugPrintf("Coding transitive constraint:\n");
143936 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
143937 }
143938#endif
143939 assert( !ExprHasProperty(pE, EP_FromJoin) );
143940 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
143941 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
143942 WO_EQ|WO_IN|WO_IS, 0);
143943 if( pAlt==0 ) continue;
143944 if( pAlt->wtFlags & (TERM_CODED) ) continue;
143945 if( (pAlt->eOperator & WO_IN)
143946 && (pAlt->pExpr->flags & EP_xIsSelect)
143947 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
143948 ){
143949 continue;
143950 }
143951 testcase( pAlt->eOperator & WO_EQ );
143952 testcase( pAlt->eOperator & WO_IS );
143953 testcase( pAlt->eOperator & WO_IN );
143954 VdbeModuleComment((v, "begin transitive constraint"));
143955 sEAlt = *pAlt->pExpr;
143956 sEAlt.pLeft = pE->pLeft;
143957 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
143958 }
143959
143960 /* For a LEFT OUTER JOIN, generate code that will record the fact that
143961 ** at least one row of the right table has matched the left table.
143962 */
143963 if( pLevel->iLeftJoin ){
143964 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
143965 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
143966 VdbeComment((v, "record LEFT JOIN hit"));
143967 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
143968 testcase( pTerm->wtFlags & TERM_VIRTUAL );
143969 testcase( pTerm->wtFlags & TERM_CODED );
143970 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
143971 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
143972 assert( pWInfo->untestedTerms );
143973 continue;
143974 }
143975 assert( pTerm->pExpr );
143976 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
143977 pTerm->wtFlags |= TERM_CODED;
143978 }
143979 }
143980
143981#if WHERETRACE_ENABLED /* 0x20800 */
143982 if( sqlite3WhereTrace & 0x20000 ){
143983 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
143984 iLevel);
143985 sqlite3WhereClausePrint(pWC);
143986 }
143987 if( sqlite3WhereTrace & 0x800 ){
143988 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
143989 iLevel, (u64)pLevel->notReady);
143990 }
143991#endif
143992 return pLevel->notReady;
143993}
143994
143995/************** End of wherecode.c *******************************************/
143996/************** Begin file whereexpr.c ***************************************/
143997/*
143998** 2015-06-08
143999**
144000** The author disclaims copyright to this source code. In place of
144001** a legal notice, here is a blessing:
144002**
144003** May you do good and not evil.
144004** May you find forgiveness for yourself and forgive others.
144005** May you share freely, never taking more than you give.
144006**
144007*************************************************************************
144008** This module contains C code that generates VDBE code used to process
144009** the WHERE clause of SQL statements.
144010**
144011** This file was originally part of where.c but was split out to improve
144012** readability and editabiliity. This file contains utility routines for
144013** analyzing Expr objects in the WHERE clause.
144014*/
144015/* #include "sqliteInt.h" */
144016/* #include "whereInt.h" */
144017
144018/* Forward declarations */
144019static void exprAnalyze(SrcList*, WhereClause*, int);
144020
144021/*
144022** Deallocate all memory associated with a WhereOrInfo object.
144023*/
144024static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
144025 sqlite3WhereClauseClear(&p->wc);
144026 sqlite3DbFree(db, p);
144027}
144028
144029/*
144030** Deallocate all memory associated with a WhereAndInfo object.
144031*/
144032static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
144033 sqlite3WhereClauseClear(&p->wc);
144034 sqlite3DbFree(db, p);
144035}
144036
144037/*
144038** Add a single new WhereTerm entry to the WhereClause object pWC.
144039** The new WhereTerm object is constructed from Expr p and with wtFlags.
144040** The index in pWC->a[] of the new WhereTerm is returned on success.
144041** 0 is returned if the new WhereTerm could not be added due to a memory
144042** allocation error. The memory allocation failure will be recorded in
144043** the db->mallocFailed flag so that higher-level functions can detect it.
144044**
144045** This routine will increase the size of the pWC->a[] array as necessary.
144046**
144047** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
144048** for freeing the expression p is assumed by the WhereClause object pWC.
144049** This is true even if this routine fails to allocate a new WhereTerm.
144050**
144051** WARNING: This routine might reallocate the space used to store
144052** WhereTerms. All pointers to WhereTerms should be invalidated after
144053** calling this routine. Such pointers may be reinitialized by referencing
144054** the pWC->a[] array.
144055*/
144056static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
144057 WhereTerm *pTerm;
144058 int idx;
144059 testcase( wtFlags & TERM_VIRTUAL );
144060 if( pWC->nTerm>=pWC->nSlot ){
144061 WhereTerm *pOld = pWC->a;
144062 sqlite3 *db = pWC->pWInfo->pParse->db;
144063 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
144064 if( pWC->a==0 ){
144065 if( wtFlags & TERM_DYNAMIC ){
144066 sqlite3ExprDelete(db, p);
144067 }
144068 pWC->a = pOld;
144069 return 0;
144070 }
144071 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
144072 if( pOld!=pWC->aStatic ){
144073 sqlite3DbFree(db, pOld);
144074 }
144075 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
144076 }
144077 pTerm = &pWC->a[idx = pWC->nTerm++];
144078 if( p && ExprHasProperty(p, EP_Unlikely) ){
144079 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
144080 }else{
144081 pTerm->truthProb = 1;
144082 }
144083 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
144084 pTerm->wtFlags = wtFlags;
144085 pTerm->pWC = pWC;
144086 pTerm->iParent = -1;
144087 memset(&pTerm->eOperator, 0,
144088 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
144089 return idx;
144090}
144091
144092/*
144093** Return TRUE if the given operator is one of the operators that is
144094** allowed for an indexable WHERE clause term. The allowed operators are
144095** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
144096*/
144097static int allowedOp(int op){
144098 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
144099 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
144100 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
144101 assert( TK_GE==TK_EQ+4 );
144102 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
144103}
144104
144105/*
144106** Commute a comparison operator. Expressions of the form "X op Y"
144107** are converted into "Y op X".
144108*/
144109static u16 exprCommute(Parse *pParse, Expr *pExpr){
144110 if( pExpr->pLeft->op==TK_VECTOR
144111 || pExpr->pRight->op==TK_VECTOR
144112 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
144113 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
144114 ){
144115 pExpr->flags ^= EP_Commuted;
144116 }
144117 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
144118 if( pExpr->op>=TK_GT ){
144119 assert( TK_LT==TK_GT+2 );
144120 assert( TK_GE==TK_LE+2 );
144121 assert( TK_GT>TK_EQ );
144122 assert( TK_GT<TK_LE );
144123 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
144124 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
144125 }
144126 return 0;
144127}
144128
144129/*
144130** Translate from TK_xx operator to WO_xx bitmask.
144131*/
144132static u16 operatorMask(int op){
144133 u16 c;
144134 assert( allowedOp(op) );
144135 if( op==TK_IN ){
144136 c = WO_IN;
144137 }else if( op==TK_ISNULL ){
144138 c = WO_ISNULL;
144139 }else if( op==TK_IS ){
144140 c = WO_IS;
144141 }else{
144142 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
144143 c = (u16)(WO_EQ<<(op-TK_EQ));
144144 }
144145 assert( op!=TK_ISNULL || c==WO_ISNULL );
144146 assert( op!=TK_IN || c==WO_IN );
144147 assert( op!=TK_EQ || c==WO_EQ );
144148 assert( op!=TK_LT || c==WO_LT );
144149 assert( op!=TK_LE || c==WO_LE );
144150 assert( op!=TK_GT || c==WO_GT );
144151 assert( op!=TK_GE || c==WO_GE );
144152 assert( op!=TK_IS || c==WO_IS );
144153 return c;
144154}
144155
144156
144157#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
144158/*
144159** Check to see if the given expression is a LIKE or GLOB operator that
144160** can be optimized using inequality constraints. Return TRUE if it is
144161** so and false if not.
144162**
144163** In order for the operator to be optimizible, the RHS must be a string
144164** literal that does not begin with a wildcard. The LHS must be a column
144165** that may only be NULL, a string, or a BLOB, never a number. (This means
144166** that virtual tables cannot participate in the LIKE optimization.) The
144167** collating sequence for the column on the LHS must be appropriate for
144168** the operator.
144169*/
144170static int isLikeOrGlob(
144171 Parse *pParse, /* Parsing and code generating context */
144172 Expr *pExpr, /* Test this expression */
144173 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
144174 int *pisComplete, /* True if the only wildcard is % in the last character */
144175 int *pnoCase /* True if uppercase is equivalent to lowercase */
144176){
144177 const u8 *z = 0; /* String on RHS of LIKE operator */
144178 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
144179 ExprList *pList; /* List of operands to the LIKE operator */
144180 u8 c; /* One character in z[] */
144181 int cnt; /* Number of non-wildcard prefix characters */
144182 u8 wc[4]; /* Wildcard characters */
144183 sqlite3 *db = pParse->db; /* Database connection */
144184 sqlite3_value *pVal = 0;
144185 int op; /* Opcode of pRight */
144186 int rc; /* Result code to return */
144187
144188 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
144189 return 0;
144190 }
144191#ifdef SQLITE_EBCDIC
144192 if( *pnoCase ) return 0;
144193#endif
144194 pList = pExpr->x.pList;
144195 pLeft = pList->a[1].pExpr;
144196
144197 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
144198 op = pRight->op;
144199 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
144200 Vdbe *pReprepare = pParse->pReprepare;
144201 int iCol = pRight->iColumn;
144202 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
144203 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
144204 z = sqlite3_value_text(pVal);
144205 }
144206 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
144207 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
144208 }else if( op==TK_STRING ){
144209 z = (u8*)pRight->u.zToken;
144210 }
144211 if( z ){
144212
144213 /* Count the number of prefix characters prior to the first wildcard */
144214 cnt = 0;
144215 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
144216 cnt++;
144217 if( c==wc[3] && z[cnt]!=0 ) cnt++;
144218 }
144219
144220 /* The optimization is possible only if (1) the pattern does not begin
144221 ** with a wildcard and if (2) the non-wildcard prefix does not end with
144222 ** an (illegal 0xff) character, or (3) the pattern does not consist of
144223 ** a single escape character. The second condition is necessary so
144224 ** that we can increment the prefix key to find an upper bound for the
144225 ** range search. The third is because the caller assumes that the pattern
144226 ** consists of at least one character after all escapes have been
144227 ** removed. */
144228 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
144229 Expr *pPrefix;
144230
144231 /* A "complete" match if the pattern ends with "*" or "%" */
144232 *pisComplete = c==wc[0] && z[cnt+1]==0;
144233
144234 /* Get the pattern prefix. Remove all escapes from the prefix. */
144235 pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
144236 if( pPrefix ){
144237 int iFrom, iTo;
144238 char *zNew = pPrefix->u.zToken;
144239 zNew[cnt] = 0;
144240 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
144241 if( zNew[iFrom]==wc[3] ) iFrom++;
144242 zNew[iTo++] = zNew[iFrom];
144243 }
144244 zNew[iTo] = 0;
144245 assert( iTo>0 );
144246
144247 /* If the LHS is not an ordinary column with TEXT affinity, then the
144248 ** pattern prefix boundaries (both the start and end boundaries) must
144249 ** not look like a number. Otherwise the pattern might be treated as
144250 ** a number, which will invalidate the LIKE optimization.
144251 **
144252 ** Getting this right has been a persistent source of bugs in the
144253 ** LIKE optimization. See, for example:
144254 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
144255 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
144256 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
144257 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
144258 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
144259 */
144260 if( pLeft->op!=TK_COLUMN
144261 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
144262 || IsVirtual(pLeft->y.pTab) /* Value might be numeric */
144263 ){
144264 int isNum;
144265 double rDummy;
144266 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
144267 if( isNum<=0 ){
144268 if( iTo==1 && zNew[0]=='-' ){
144269 isNum = +1;
144270 }else{
144271 zNew[iTo-1]++;
144272 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
144273 zNew[iTo-1]--;
144274 }
144275 }
144276 if( isNum>0 ){
144277 sqlite3ExprDelete(db, pPrefix);
144278 sqlite3ValueFree(pVal);
144279 return 0;
144280 }
144281 }
144282 }
144283 *ppPrefix = pPrefix;
144284
144285 /* If the RHS pattern is a bound parameter, make arrangements to
144286 ** reprepare the statement when that parameter is rebound */
144287 if( op==TK_VARIABLE ){
144288 Vdbe *v = pParse->pVdbe;
144289 sqlite3VdbeSetVarmask(v, pRight->iColumn);
144290 if( *pisComplete && pRight->u.zToken[1] ){
144291 /* If the rhs of the LIKE expression is a variable, and the current
144292 ** value of the variable means there is no need to invoke the LIKE
144293 ** function, then no OP_Variable will be added to the program.
144294 ** This causes problems for the sqlite3_bind_parameter_name()
144295 ** API. To work around them, add a dummy OP_Variable here.
144296 */
144297 int r1 = sqlite3GetTempReg(pParse);
144298 sqlite3ExprCodeTarget(pParse, pRight, r1);
144299 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
144300 sqlite3ReleaseTempReg(pParse, r1);
144301 }
144302 }
144303 }else{
144304 z = 0;
144305 }
144306 }
144307
144308 rc = (z!=0);
144309 sqlite3ValueFree(pVal);
144310 return rc;
144311}
144312#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
144313
144314
144315#ifndef SQLITE_OMIT_VIRTUALTABLE
144316/*
144317** Check to see if the pExpr expression is a form that needs to be passed
144318** to the xBestIndex method of virtual tables. Forms of interest include:
144319**
144320** Expression Virtual Table Operator
144321** ----------------------- ---------------------------------
144322** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
144323** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
144324** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
144325** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
144326** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
144327** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
144328** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
144329** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
144330** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
144331**
144332** In every case, "column" must be a column of a virtual table. If there
144333** is a match, set *ppLeft to the "column" expression, set *ppRight to the
144334** "expr" expression (even though in forms (6) and (8) the column is on the
144335** right and the expression is on the left). Also set *peOp2 to the
144336** appropriate virtual table operator. The return value is 1 or 2 if there
144337** is a match. The usual return is 1, but if the RHS is also a column
144338** of virtual table in forms (5) or (7) then return 2.
144339**
144340** If the expression matches none of the patterns above, return 0.
144341*/
144342static int isAuxiliaryVtabOperator(
144343 sqlite3 *db, /* Parsing context */
144344 Expr *pExpr, /* Test this expression */
144345 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
144346 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
144347 Expr **ppRight /* Expression to left of MATCH/op2 */
144348){
144349 if( pExpr->op==TK_FUNCTION ){
144350 static const struct Op2 {
144351 const char *zOp;
144352 unsigned char eOp2;
144353 } aOp[] = {
144354 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
144355 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
144356 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
144357 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
144358 };
144359 ExprList *pList;
144360 Expr *pCol; /* Column reference */
144361 int i;
144362
144363 pList = pExpr->x.pList;
144364 if( pList==0 || pList->nExpr!=2 ){
144365 return 0;
144366 }
144367
144368 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
144369 ** virtual table on their second argument, which is the same as
144370 ** the left-hand side operand in their in-fix form.
144371 **
144372 ** vtab_column MATCH expression
144373 ** MATCH(expression,vtab_column)
144374 */
144375 pCol = pList->a[1].pExpr;
144376 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
144377 if( ExprIsVtab(pCol) ){
144378 for(i=0; i<ArraySize(aOp); i++){
144379 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
144380 *peOp2 = aOp[i].eOp2;
144381 *ppRight = pList->a[0].pExpr;
144382 *ppLeft = pCol;
144383 return 1;
144384 }
144385 }
144386 }
144387
144388 /* We can also match against the first column of overloaded
144389 ** functions where xFindFunction returns a value of at least
144390 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
144391 **
144392 ** OVERLOADED(vtab_column,expression)
144393 **
144394 ** Historically, xFindFunction expected to see lower-case function
144395 ** names. But for this use case, xFindFunction is expected to deal
144396 ** with function names in an arbitrary case.
144397 */
144398 pCol = pList->a[0].pExpr;
144399 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
144400 if( ExprIsVtab(pCol) ){
144401 sqlite3_vtab *pVtab;
144402 sqlite3_module *pMod;
144403 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
144404 void *pNotUsed;
144405 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
144406 assert( pVtab!=0 );
144407 assert( pVtab->pModule!=0 );
144408 pMod = (sqlite3_module *)pVtab->pModule;
144409 if( pMod->xFindFunction!=0 ){
144410 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
144411 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
144412 *peOp2 = i;
144413 *ppRight = pList->a[1].pExpr;
144414 *ppLeft = pCol;
144415 return 1;
144416 }
144417 }
144418 }
144419 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
144420 int res = 0;
144421 Expr *pLeft = pExpr->pLeft;
144422 Expr *pRight = pExpr->pRight;
144423 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
144424 if( ExprIsVtab(pLeft) ){
144425 res++;
144426 }
144427 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
144428 if( pRight && ExprIsVtab(pRight) ){
144429 res++;
144430 SWAP(Expr*, pLeft, pRight);
144431 }
144432 *ppLeft = pLeft;
144433 *ppRight = pRight;
144434 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
144435 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
144436 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
144437 return res;
144438 }
144439 return 0;
144440}
144441#endif /* SQLITE_OMIT_VIRTUALTABLE */
144442
144443/*
144444** If the pBase expression originated in the ON or USING clause of
144445** a join, then transfer the appropriate markings over to derived.
144446*/
144447static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
144448 if( pDerived ){
144449 pDerived->flags |= pBase->flags & EP_FromJoin;
144450 pDerived->iRightJoinTable = pBase->iRightJoinTable;
144451 }
144452}
144453
144454/*
144455** Mark term iChild as being a child of term iParent
144456*/
144457static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
144458 pWC->a[iChild].iParent = iParent;
144459 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
144460 pWC->a[iParent].nChild++;
144461}
144462
144463/*
144464** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
144465** a conjunction, then return just pTerm when N==0. If N is exceeds
144466** the number of available subterms, return NULL.
144467*/
144468static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
144469 if( pTerm->eOperator!=WO_AND ){
144470 return N==0 ? pTerm : 0;
144471 }
144472 if( N<pTerm->u.pAndInfo->wc.nTerm ){
144473 return &pTerm->u.pAndInfo->wc.a[N];
144474 }
144475 return 0;
144476}
144477
144478/*
144479** Subterms pOne and pTwo are contained within WHERE clause pWC. The
144480** two subterms are in disjunction - they are OR-ed together.
144481**
144482** If these two terms are both of the form: "A op B" with the same
144483** A and B values but different operators and if the operators are
144484** compatible (if one is = and the other is <, for example) then
144485** add a new virtual AND term to pWC that is the combination of the
144486** two.
144487**
144488** Some examples:
144489**
144490** x<y OR x=y --> x<=y
144491** x=y OR x=y --> x=y
144492** x<=y OR x<y --> x<=y
144493**
144494** The following is NOT generated:
144495**
144496** x<y OR x>y --> x!=y
144497*/
144498static void whereCombineDisjuncts(
144499 SrcList *pSrc, /* the FROM clause */
144500 WhereClause *pWC, /* The complete WHERE clause */
144501 WhereTerm *pOne, /* First disjunct */
144502 WhereTerm *pTwo /* Second disjunct */
144503){
144504 u16 eOp = pOne->eOperator | pTwo->eOperator;
144505 sqlite3 *db; /* Database connection (for malloc) */
144506 Expr *pNew; /* New virtual expression */
144507 int op; /* Operator for the combined expression */
144508 int idxNew; /* Index in pWC of the next virtual term */
144509
144510 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
144511 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
144512 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
144513 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
144514 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
144515 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
144516 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
144517 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
144518 /* If we reach this point, it means the two subterms can be combined */
144519 if( (eOp & (eOp-1))!=0 ){
144520 if( eOp & (WO_LT|WO_LE) ){
144521 eOp = WO_LE;
144522 }else{
144523 assert( eOp & (WO_GT|WO_GE) );
144524 eOp = WO_GE;
144525 }
144526 }
144527 db = pWC->pWInfo->pParse->db;
144528 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
144529 if( pNew==0 ) return;
144530 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
144531 pNew->op = op;
144532 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
144533 exprAnalyze(pSrc, pWC, idxNew);
144534}
144535
144536#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
144537/*
144538** Analyze a term that consists of two or more OR-connected
144539** subterms. So in:
144540**
144541** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
144542** ^^^^^^^^^^^^^^^^^^^^
144543**
144544** This routine analyzes terms such as the middle term in the above example.
144545** A WhereOrTerm object is computed and attached to the term under
144546** analysis, regardless of the outcome of the analysis. Hence:
144547**
144548** WhereTerm.wtFlags |= TERM_ORINFO
144549** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
144550**
144551** The term being analyzed must have two or more of OR-connected subterms.
144552** A single subterm might be a set of AND-connected sub-subterms.
144553** Examples of terms under analysis:
144554**
144555** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
144556** (B) x=expr1 OR expr2=x OR x=expr3
144557** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
144558** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
144559** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
144560** (F) x>A OR (x=A AND y>=B)
144561**
144562** CASE 1:
144563**
144564** If all subterms are of the form T.C=expr for some single column of C and
144565** a single table T (as shown in example B above) then create a new virtual
144566** term that is an equivalent IN expression. In other words, if the term
144567** being analyzed is:
144568**
144569** x = expr1 OR expr2 = x OR x = expr3
144570**
144571** then create a new virtual term like this:
144572**
144573** x IN (expr1,expr2,expr3)
144574**
144575** CASE 2:
144576**
144577** If there are exactly two disjuncts and one side has x>A and the other side
144578** has x=A (for the same x and A) then add a new virtual conjunct term to the
144579** WHERE clause of the form "x>=A". Example:
144580**
144581** x>A OR (x=A AND y>B) adds: x>=A
144582**
144583** The added conjunct can sometimes be helpful in query planning.
144584**
144585** CASE 3:
144586**
144587** If all subterms are indexable by a single table T, then set
144588**
144589** WhereTerm.eOperator = WO_OR
144590** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
144591**
144592** A subterm is "indexable" if it is of the form
144593** "T.C <op> <expr>" where C is any column of table T and
144594** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
144595** A subterm is also indexable if it is an AND of two or more
144596** subsubterms at least one of which is indexable. Indexable AND
144597** subterms have their eOperator set to WO_AND and they have
144598** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
144599**
144600** From another point of view, "indexable" means that the subterm could
144601** potentially be used with an index if an appropriate index exists.
144602** This analysis does not consider whether or not the index exists; that
144603** is decided elsewhere. This analysis only looks at whether subterms
144604** appropriate for indexing exist.
144605**
144606** All examples A through E above satisfy case 3. But if a term
144607** also satisfies case 1 (such as B) we know that the optimizer will
144608** always prefer case 1, so in that case we pretend that case 3 is not
144609** satisfied.
144610**
144611** It might be the case that multiple tables are indexable. For example,
144612** (E) above is indexable on tables P, Q, and R.
144613**
144614** Terms that satisfy case 3 are candidates for lookup by using
144615** separate indices to find rowids for each subterm and composing
144616** the union of all rowids using a RowSet object. This is similar
144617** to "bitmap indices" in other database engines.
144618**
144619** OTHERWISE:
144620**
144621** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
144622** zero. This term is not useful for search.
144623*/
144624static void exprAnalyzeOrTerm(
144625 SrcList *pSrc, /* the FROM clause */
144626 WhereClause *pWC, /* the complete WHERE clause */
144627 int idxTerm /* Index of the OR-term to be analyzed */
144628){
144629 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
144630 Parse *pParse = pWInfo->pParse; /* Parser context */
144631 sqlite3 *db = pParse->db; /* Database connection */
144632 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
144633 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
144634 int i; /* Loop counters */
144635 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
144636 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
144637 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
144638 Bitmask chngToIN; /* Tables that might satisfy case 1 */
144639 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
144640
144641 /*
144642 ** Break the OR clause into its separate subterms. The subterms are
144643 ** stored in a WhereClause structure containing within the WhereOrInfo
144644 ** object that is attached to the original OR clause term.
144645 */
144646 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
144647 assert( pExpr->op==TK_OR );
144648 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
144649 if( pOrInfo==0 ) return;
144650 pTerm->wtFlags |= TERM_ORINFO;
144651 pOrWc = &pOrInfo->wc;
144652 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
144653 sqlite3WhereClauseInit(pOrWc, pWInfo);
144654 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
144655 sqlite3WhereExprAnalyze(pSrc, pOrWc);
144656 if( db->mallocFailed ) return;
144657 assert( pOrWc->nTerm>=2 );
144658
144659 /*
144660 ** Compute the set of tables that might satisfy cases 1 or 3.
144661 */
144662 indexable = ~(Bitmask)0;
144663 chngToIN = ~(Bitmask)0;
144664 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
144665 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
144666 WhereAndInfo *pAndInfo;
144667 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
144668 chngToIN = 0;
144669 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
144670 if( pAndInfo ){
144671 WhereClause *pAndWC;
144672 WhereTerm *pAndTerm;
144673 int j;
144674 Bitmask b = 0;
144675 pOrTerm->u.pAndInfo = pAndInfo;
144676 pOrTerm->wtFlags |= TERM_ANDINFO;
144677 pOrTerm->eOperator = WO_AND;
144678 pAndWC = &pAndInfo->wc;
144679 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
144680 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
144681 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
144682 sqlite3WhereExprAnalyze(pSrc, pAndWC);
144683 pAndWC->pOuter = pWC;
144684 if( !db->mallocFailed ){
144685 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
144686 assert( pAndTerm->pExpr );
144687 if( allowedOp(pAndTerm->pExpr->op)
144688 || pAndTerm->eOperator==WO_AUX
144689 ){
144690 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
144691 }
144692 }
144693 }
144694 indexable &= b;
144695 }
144696 }else if( pOrTerm->wtFlags & TERM_COPIED ){
144697 /* Skip this term for now. We revisit it when we process the
144698 ** corresponding TERM_VIRTUAL term */
144699 }else{
144700 Bitmask b;
144701 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
144702 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
144703 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
144704 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
144705 }
144706 indexable &= b;
144707 if( (pOrTerm->eOperator & WO_EQ)==0 ){
144708 chngToIN = 0;
144709 }else{
144710 chngToIN &= b;
144711 }
144712 }
144713 }
144714
144715 /*
144716 ** Record the set of tables that satisfy case 3. The set might be
144717 ** empty.
144718 */
144719 pOrInfo->indexable = indexable;
144720 if( indexable ){
144721 pTerm->eOperator = WO_OR;
144722 pWC->hasOr = 1;
144723 }else{
144724 pTerm->eOperator = WO_OR;
144725 }
144726
144727 /* For a two-way OR, attempt to implementation case 2.
144728 */
144729 if( indexable && pOrWc->nTerm==2 ){
144730 int iOne = 0;
144731 WhereTerm *pOne;
144732 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
144733 int iTwo = 0;
144734 WhereTerm *pTwo;
144735 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
144736 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
144737 }
144738 }
144739 }
144740
144741 /*
144742 ** chngToIN holds a set of tables that *might* satisfy case 1. But
144743 ** we have to do some additional checking to see if case 1 really
144744 ** is satisfied.
144745 **
144746 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
144747 ** that there is no possibility of transforming the OR clause into an
144748 ** IN operator because one or more terms in the OR clause contain
144749 ** something other than == on a column in the single table. The 1-bit
144750 ** case means that every term of the OR clause is of the form
144751 ** "table.column=expr" for some single table. The one bit that is set
144752 ** will correspond to the common table. We still need to check to make
144753 ** sure the same column is used on all terms. The 2-bit case is when
144754 ** the all terms are of the form "table1.column=table2.column". It
144755 ** might be possible to form an IN operator with either table1.column
144756 ** or table2.column as the LHS if either is common to every term of
144757 ** the OR clause.
144758 **
144759 ** Note that terms of the form "table.column1=table.column2" (the
144760 ** same table on both sizes of the ==) cannot be optimized.
144761 */
144762 if( chngToIN ){
144763 int okToChngToIN = 0; /* True if the conversion to IN is valid */
144764 int iColumn = -1; /* Column index on lhs of IN operator */
144765 int iCursor = -1; /* Table cursor common to all terms */
144766 int j = 0; /* Loop counter */
144767
144768 /* Search for a table and column that appears on one side or the
144769 ** other of the == operator in every subterm. That table and column
144770 ** will be recorded in iCursor and iColumn. There might not be any
144771 ** such table and column. Set okToChngToIN if an appropriate table
144772 ** and column is found but leave okToChngToIN false if not found.
144773 */
144774 for(j=0; j<2 && !okToChngToIN; j++){
144775 Expr *pLeft = 0;
144776 pOrTerm = pOrWc->a;
144777 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
144778 assert( pOrTerm->eOperator & WO_EQ );
144779 pOrTerm->wtFlags &= ~TERM_OR_OK;
144780 if( pOrTerm->leftCursor==iCursor ){
144781 /* This is the 2-bit case and we are on the second iteration and
144782 ** current term is from the first iteration. So skip this term. */
144783 assert( j==1 );
144784 continue;
144785 }
144786 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
144787 pOrTerm->leftCursor))==0 ){
144788 /* This term must be of the form t1.a==t2.b where t2 is in the
144789 ** chngToIN set but t1 is not. This term will be either preceded
144790 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
144791 ** and use its inversion. */
144792 testcase( pOrTerm->wtFlags & TERM_COPIED );
144793 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
144794 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
144795 continue;
144796 }
144797 iColumn = pOrTerm->u.leftColumn;
144798 iCursor = pOrTerm->leftCursor;
144799 pLeft = pOrTerm->pExpr->pLeft;
144800 break;
144801 }
144802 if( i<0 ){
144803 /* No candidate table+column was found. This can only occur
144804 ** on the second iteration */
144805 assert( j==1 );
144806 assert( IsPowerOfTwo(chngToIN) );
144807 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
144808 break;
144809 }
144810 testcase( j==1 );
144811
144812 /* We have found a candidate table and column. Check to see if that
144813 ** table and column is common to every term in the OR clause */
144814 okToChngToIN = 1;
144815 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
144816 assert( pOrTerm->eOperator & WO_EQ );
144817 if( pOrTerm->leftCursor!=iCursor ){
144818 pOrTerm->wtFlags &= ~TERM_OR_OK;
144819 }else if( pOrTerm->u.leftColumn!=iColumn || (iColumn==XN_EXPR
144820 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
144821 )){
144822 okToChngToIN = 0;
144823 }else{
144824 int affLeft, affRight;
144825 /* If the right-hand side is also a column, then the affinities
144826 ** of both right and left sides must be such that no type
144827 ** conversions are required on the right. (Ticket #2249)
144828 */
144829 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
144830 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
144831 if( affRight!=0 && affRight!=affLeft ){
144832 okToChngToIN = 0;
144833 }else{
144834 pOrTerm->wtFlags |= TERM_OR_OK;
144835 }
144836 }
144837 }
144838 }
144839
144840 /* At this point, okToChngToIN is true if original pTerm satisfies
144841 ** case 1. In that case, construct a new virtual term that is
144842 ** pTerm converted into an IN operator.
144843 */
144844 if( okToChngToIN ){
144845 Expr *pDup; /* A transient duplicate expression */
144846 ExprList *pList = 0; /* The RHS of the IN operator */
144847 Expr *pLeft = 0; /* The LHS of the IN operator */
144848 Expr *pNew; /* The complete IN operator */
144849
144850 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
144851 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
144852 assert( pOrTerm->eOperator & WO_EQ );
144853 assert( pOrTerm->leftCursor==iCursor );
144854 assert( pOrTerm->u.leftColumn==iColumn );
144855 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
144856 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
144857 pLeft = pOrTerm->pExpr->pLeft;
144858 }
144859 assert( pLeft!=0 );
144860 pDup = sqlite3ExprDup(db, pLeft, 0);
144861 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
144862 if( pNew ){
144863 int idxNew;
144864 transferJoinMarkings(pNew, pExpr);
144865 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
144866 pNew->x.pList = pList;
144867 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
144868 testcase( idxNew==0 );
144869 exprAnalyze(pSrc, pWC, idxNew);
144870 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
144871 markTermAsChild(pWC, idxNew, idxTerm);
144872 }else{
144873 sqlite3ExprListDelete(db, pList);
144874 }
144875 }
144876 }
144877}
144878#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
144879
144880/*
144881** We already know that pExpr is a binary operator where both operands are
144882** column references. This routine checks to see if pExpr is an equivalence
144883** relation:
144884** 1. The SQLITE_Transitive optimization must be enabled
144885** 2. Must be either an == or an IS operator
144886** 3. Not originating in the ON clause of an OUTER JOIN
144887** 4. The affinities of A and B must be compatible
144888** 5a. Both operands use the same collating sequence OR
144889** 5b. The overall collating sequence is BINARY
144890** If this routine returns TRUE, that means that the RHS can be substituted
144891** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
144892** This is an optimization. No harm comes from returning 0. But if 1 is
144893** returned when it should not be, then incorrect answers might result.
144894*/
144895static int termIsEquivalence(Parse *pParse, Expr *pExpr){
144896 char aff1, aff2;
144897 CollSeq *pColl;
144898 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
144899 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
144900 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
144901 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
144902 aff2 = sqlite3ExprAffinity(pExpr->pRight);
144903 if( aff1!=aff2
144904 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
144905 ){
144906 return 0;
144907 }
144908 pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
144909 if( sqlite3IsBinary(pColl) ) return 1;
144910 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
144911}
144912
144913/*
144914** Recursively walk the expressions of a SELECT statement and generate
144915** a bitmask indicating which tables are used in that expression
144916** tree.
144917*/
144918static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
144919 Bitmask mask = 0;
144920 while( pS ){
144921 SrcList *pSrc = pS->pSrc;
144922 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
144923 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
144924 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
144925 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
144926 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
144927 if( ALWAYS(pSrc!=0) ){
144928 int i;
144929 for(i=0; i<pSrc->nSrc; i++){
144930 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
144931 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
144932 if( pSrc->a[i].fg.isTabFunc ){
144933 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
144934 }
144935 }
144936 }
144937 pS = pS->pPrior;
144938 }
144939 return mask;
144940}
144941
144942/*
144943** Expression pExpr is one operand of a comparison operator that might
144944** be useful for indexing. This routine checks to see if pExpr appears
144945** in any index. Return TRUE (1) if pExpr is an indexed term and return
144946** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
144947** number of the table that is indexed and aiCurCol[1] to the column number
144948** of the column that is indexed, or XN_EXPR (-2) if an expression is being
144949** indexed.
144950**
144951** If pExpr is a TK_COLUMN column reference, then this routine always returns
144952** true even if that particular column is not indexed, because the column
144953** might be added to an automatic index later.
144954*/
144955static SQLITE_NOINLINE int exprMightBeIndexed2(
144956 SrcList *pFrom, /* The FROM clause */
144957 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
144958 int *aiCurCol, /* Write the referenced table cursor and column here */
144959 Expr *pExpr /* An operand of a comparison operator */
144960){
144961 Index *pIdx;
144962 int i;
144963 int iCur;
144964 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
144965 iCur = pFrom->a[i].iCursor;
144966 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
144967 if( pIdx->aColExpr==0 ) continue;
144968 for(i=0; i<pIdx->nKeyCol; i++){
144969 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
144970 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
144971 aiCurCol[0] = iCur;
144972 aiCurCol[1] = XN_EXPR;
144973 return 1;
144974 }
144975 }
144976 }
144977 return 0;
144978}
144979static int exprMightBeIndexed(
144980 SrcList *pFrom, /* The FROM clause */
144981 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
144982 int *aiCurCol, /* Write the referenced table cursor & column here */
144983 Expr *pExpr, /* An operand of a comparison operator */
144984 int op /* The specific comparison operator */
144985){
144986 /* If this expression is a vector to the left or right of a
144987 ** inequality constraint (>, <, >= or <=), perform the processing
144988 ** on the first element of the vector. */
144989 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
144990 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
144991 assert( op<=TK_GE );
144992 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
144993 pExpr = pExpr->x.pList->a[0].pExpr;
144994 }
144995
144996 if( pExpr->op==TK_COLUMN ){
144997 aiCurCol[0] = pExpr->iTable;
144998 aiCurCol[1] = pExpr->iColumn;
144999 return 1;
145000 }
145001 if( mPrereq==0 ) return 0; /* No table references */
145002 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
145003 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
145004}
145005
145006/*
145007** The input to this routine is an WhereTerm structure with only the
145008** "pExpr" field filled in. The job of this routine is to analyze the
145009** subexpression and populate all the other fields of the WhereTerm
145010** structure.
145011**
145012** If the expression is of the form "<expr> <op> X" it gets commuted
145013** to the standard form of "X <op> <expr>".
145014**
145015** If the expression is of the form "X <op> Y" where both X and Y are
145016** columns, then the original expression is unchanged and a new virtual
145017** term of the form "Y <op> X" is added to the WHERE clause and
145018** analyzed separately. The original term is marked with TERM_COPIED
145019** and the new term is marked with TERM_DYNAMIC (because it's pExpr
145020** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
145021** is a commuted copy of a prior term.) The original term has nChild=1
145022** and the copy has idxParent set to the index of the original term.
145023*/
145024static void exprAnalyze(
145025 SrcList *pSrc, /* the FROM clause */
145026 WhereClause *pWC, /* the WHERE clause */
145027 int idxTerm /* Index of the term to be analyzed */
145028){
145029 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
145030 WhereTerm *pTerm; /* The term to be analyzed */
145031 WhereMaskSet *pMaskSet; /* Set of table index masks */
145032 Expr *pExpr; /* The expression to be analyzed */
145033 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
145034 Bitmask prereqAll; /* Prerequesites of pExpr */
145035 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
145036 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
145037 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
145038 int noCase = 0; /* uppercase equivalent to lowercase */
145039 int op; /* Top-level operator. pExpr->op */
145040 Parse *pParse = pWInfo->pParse; /* Parsing context */
145041 sqlite3 *db = pParse->db; /* Database connection */
145042 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
145043 int nLeft; /* Number of elements on left side vector */
145044
145045 if( db->mallocFailed ){
145046 return;
145047 }
145048 pTerm = &pWC->a[idxTerm];
145049 pMaskSet = &pWInfo->sMaskSet;
145050 pExpr = pTerm->pExpr;
145051 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
145052 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
145053 op = pExpr->op;
145054 if( op==TK_IN ){
145055 assert( pExpr->pRight==0 );
145056 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
145057 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
145058 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
145059 }else{
145060 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
145061 }
145062 }else if( op==TK_ISNULL ){
145063 pTerm->prereqRight = 0;
145064 }else{
145065 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
145066 }
145067 pMaskSet->bVarSelect = 0;
145068 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
145069 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
145070 if( ExprHasProperty(pExpr, EP_FromJoin) ){
145071 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
145072 prereqAll |= x;
145073 extraRight = x-1; /* ON clause terms may not be used with an index
145074 ** on left table of a LEFT JOIN. Ticket #3015 */
145075 if( (prereqAll>>1)>=x ){
145076 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
145077 return;
145078 }
145079 }
145080 pTerm->prereqAll = prereqAll;
145081 pTerm->leftCursor = -1;
145082 pTerm->iParent = -1;
145083 pTerm->eOperator = 0;
145084 if( allowedOp(op) ){
145085 int aiCurCol[2];
145086 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
145087 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
145088 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
145089
145090 if( pTerm->iField>0 ){
145091 assert( op==TK_IN );
145092 assert( pLeft->op==TK_VECTOR );
145093 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
145094 }
145095
145096 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
145097 pTerm->leftCursor = aiCurCol[0];
145098 pTerm->u.leftColumn = aiCurCol[1];
145099 pTerm->eOperator = operatorMask(op) & opMask;
145100 }
145101 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
145102 if( pRight
145103 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
145104 ){
145105 WhereTerm *pNew;
145106 Expr *pDup;
145107 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
145108 assert( pTerm->iField==0 );
145109 if( pTerm->leftCursor>=0 ){
145110 int idxNew;
145111 pDup = sqlite3ExprDup(db, pExpr, 0);
145112 if( db->mallocFailed ){
145113 sqlite3ExprDelete(db, pDup);
145114 return;
145115 }
145116 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
145117 if( idxNew==0 ) return;
145118 pNew = &pWC->a[idxNew];
145119 markTermAsChild(pWC, idxNew, idxTerm);
145120 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
145121 pTerm = &pWC->a[idxTerm];
145122 pTerm->wtFlags |= TERM_COPIED;
145123
145124 if( termIsEquivalence(pParse, pDup) ){
145125 pTerm->eOperator |= WO_EQUIV;
145126 eExtraOp = WO_EQUIV;
145127 }
145128 }else{
145129 pDup = pExpr;
145130 pNew = pTerm;
145131 }
145132 pNew->wtFlags |= exprCommute(pParse, pDup);
145133 pNew->leftCursor = aiCurCol[0];
145134 pNew->u.leftColumn = aiCurCol[1];
145135 testcase( (prereqLeft | extraRight) != prereqLeft );
145136 pNew->prereqRight = prereqLeft | extraRight;
145137 pNew->prereqAll = prereqAll;
145138 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
145139 }
145140 }
145141
145142#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
145143 /* If a term is the BETWEEN operator, create two new virtual terms
145144 ** that define the range that the BETWEEN implements. For example:
145145 **
145146 ** a BETWEEN b AND c
145147 **
145148 ** is converted into:
145149 **
145150 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
145151 **
145152 ** The two new terms are added onto the end of the WhereClause object.
145153 ** The new terms are "dynamic" and are children of the original BETWEEN
145154 ** term. That means that if the BETWEEN term is coded, the children are
145155 ** skipped. Or, if the children are satisfied by an index, the original
145156 ** BETWEEN term is skipped.
145157 */
145158 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
145159 ExprList *pList = pExpr->x.pList;
145160 int i;
145161 static const u8 ops[] = {TK_GE, TK_LE};
145162 assert( pList!=0 );
145163 assert( pList->nExpr==2 );
145164 for(i=0; i<2; i++){
145165 Expr *pNewExpr;
145166 int idxNew;
145167 pNewExpr = sqlite3PExpr(pParse, ops[i],
145168 sqlite3ExprDup(db, pExpr->pLeft, 0),
145169 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
145170 transferJoinMarkings(pNewExpr, pExpr);
145171 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
145172 testcase( idxNew==0 );
145173 exprAnalyze(pSrc, pWC, idxNew);
145174 pTerm = &pWC->a[idxTerm];
145175 markTermAsChild(pWC, idxNew, idxTerm);
145176 }
145177 }
145178#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
145179
145180#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
145181 /* Analyze a term that is composed of two or more subterms connected by
145182 ** an OR operator.
145183 */
145184 else if( pExpr->op==TK_OR ){
145185 assert( pWC->op==TK_AND );
145186 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
145187 pTerm = &pWC->a[idxTerm];
145188 }
145189#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
145190
145191#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
145192 /* Add constraints to reduce the search space on a LIKE or GLOB
145193 ** operator.
145194 **
145195 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
145196 **
145197 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
145198 **
145199 ** The last character of the prefix "abc" is incremented to form the
145200 ** termination condition "abd". If case is not significant (the default
145201 ** for LIKE) then the lower-bound is made all uppercase and the upper-
145202 ** bound is made all lowercase so that the bounds also work when comparing
145203 ** BLOBs.
145204 */
145205 if( pWC->op==TK_AND
145206 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
145207 ){
145208 Expr *pLeft; /* LHS of LIKE/GLOB operator */
145209 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
145210 Expr *pNewExpr1;
145211 Expr *pNewExpr2;
145212 int idxNew1;
145213 int idxNew2;
145214 const char *zCollSeqName; /* Name of collating sequence */
145215 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
145216
145217 pLeft = pExpr->x.pList->a[1].pExpr;
145218 pStr2 = sqlite3ExprDup(db, pStr1, 0);
145219
145220 /* Convert the lower bound to upper-case and the upper bound to
145221 ** lower-case (upper-case is less than lower-case in ASCII) so that
145222 ** the range constraints also work for BLOBs
145223 */
145224 if( noCase && !pParse->db->mallocFailed ){
145225 int i;
145226 char c;
145227 pTerm->wtFlags |= TERM_LIKE;
145228 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
145229 pStr1->u.zToken[i] = sqlite3Toupper(c);
145230 pStr2->u.zToken[i] = sqlite3Tolower(c);
145231 }
145232 }
145233
145234 if( !db->mallocFailed ){
145235 u8 c, *pC; /* Last character before the first wildcard */
145236 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
145237 c = *pC;
145238 if( noCase ){
145239 /* The point is to increment the last character before the first
145240 ** wildcard. But if we increment '@', that will push it into the
145241 ** alphabetic range where case conversions will mess up the
145242 ** inequality. To avoid this, make sure to also run the full
145243 ** LIKE on all candidate expressions by clearing the isComplete flag
145244 */
145245 if( c=='A'-1 ) isComplete = 0;
145246 c = sqlite3UpperToLower[c];
145247 }
145248 *pC = c + 1;
145249 }
145250 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
145251 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
145252 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
145253 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
145254 pStr1);
145255 transferJoinMarkings(pNewExpr1, pExpr);
145256 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
145257 testcase( idxNew1==0 );
145258 exprAnalyze(pSrc, pWC, idxNew1);
145259 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
145260 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
145261 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
145262 pStr2);
145263 transferJoinMarkings(pNewExpr2, pExpr);
145264 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
145265 testcase( idxNew2==0 );
145266 exprAnalyze(pSrc, pWC, idxNew2);
145267 pTerm = &pWC->a[idxTerm];
145268 if( isComplete ){
145269 markTermAsChild(pWC, idxNew1, idxTerm);
145270 markTermAsChild(pWC, idxNew2, idxTerm);
145271 }
145272 }
145273#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
145274
145275#ifndef SQLITE_OMIT_VIRTUALTABLE
145276 /* Add a WO_AUX auxiliary term to the constraint set if the
145277 ** current expression is of the form "column OP expr" where OP
145278 ** is an operator that gets passed into virtual tables but which is
145279 ** not normally optimized for ordinary tables. In other words, OP
145280 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
145281 ** This information is used by the xBestIndex methods of
145282 ** virtual tables. The native query optimizer does not attempt
145283 ** to do anything with MATCH functions.
145284 */
145285 if( pWC->op==TK_AND ){
145286 Expr *pRight = 0, *pLeft = 0;
145287 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
145288 while( res-- > 0 ){
145289 int idxNew;
145290 WhereTerm *pNewTerm;
145291 Bitmask prereqColumn, prereqExpr;
145292
145293 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
145294 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
145295 if( (prereqExpr & prereqColumn)==0 ){
145296 Expr *pNewExpr;
145297 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
145298 0, sqlite3ExprDup(db, pRight, 0));
145299 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
145300 ExprSetProperty(pNewExpr, EP_FromJoin);
145301 pNewExpr->iRightJoinTable = pExpr->iRightJoinTable;
145302 }
145303 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
145304 testcase( idxNew==0 );
145305 pNewTerm = &pWC->a[idxNew];
145306 pNewTerm->prereqRight = prereqExpr;
145307 pNewTerm->leftCursor = pLeft->iTable;
145308 pNewTerm->u.leftColumn = pLeft->iColumn;
145309 pNewTerm->eOperator = WO_AUX;
145310 pNewTerm->eMatchOp = eOp2;
145311 markTermAsChild(pWC, idxNew, idxTerm);
145312 pTerm = &pWC->a[idxTerm];
145313 pTerm->wtFlags |= TERM_COPIED;
145314 pNewTerm->prereqAll = pTerm->prereqAll;
145315 }
145316 SWAP(Expr*, pLeft, pRight);
145317 }
145318 }
145319#endif /* SQLITE_OMIT_VIRTUALTABLE */
145320
145321 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
145322 ** new terms for each component comparison - "a = ?" and "b = ?". The
145323 ** new terms completely replace the original vector comparison, which is
145324 ** no longer used.
145325 **
145326 ** This is only required if at least one side of the comparison operation
145327 ** is not a sub-select. */
145328 if( pWC->op==TK_AND
145329 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
145330 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
145331 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
145332 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
145333 || (pExpr->pRight->flags & EP_xIsSelect)==0)
145334 ){
145335 int i;
145336 for(i=0; i<nLeft; i++){
145337 int idxNew;
145338 Expr *pNew;
145339 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
145340 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
145341
145342 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
145343 transferJoinMarkings(pNew, pExpr);
145344 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
145345 exprAnalyze(pSrc, pWC, idxNew);
145346 }
145347 pTerm = &pWC->a[idxTerm];
145348 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
145349 pTerm->eOperator = 0;
145350 }
145351
145352 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
145353 ** a virtual term for each vector component. The expression object
145354 ** used by each such virtual term is pExpr (the full vector IN(...)
145355 ** expression). The WhereTerm.iField variable identifies the index within
145356 ** the vector on the LHS that the virtual term represents.
145357 **
145358 ** This only works if the RHS is a simple SELECT (not a compound) that does
145359 ** not use window functions.
145360 */
145361 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
145362 && pExpr->pLeft->op==TK_VECTOR
145363 && pExpr->x.pSelect->pPrior==0
145364#ifndef SQLITE_OMIT_WINDOWFUNC
145365 && pExpr->x.pSelect->pWin==0
145366#endif
145367 ){
145368 int i;
145369 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
145370 int idxNew;
145371 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
145372 pWC->a[idxNew].iField = i+1;
145373 exprAnalyze(pSrc, pWC, idxNew);
145374 markTermAsChild(pWC, idxNew, idxTerm);
145375 }
145376 }
145377
145378#ifdef SQLITE_ENABLE_STAT4
145379 /* When sqlite_stat4 histogram data is available an operator of the
145380 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
145381 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
145382 ** virtual term of that form.
145383 **
145384 ** Note that the virtual term must be tagged with TERM_VNULL.
145385 */
145386 if( pExpr->op==TK_NOTNULL
145387 && pExpr->pLeft->op==TK_COLUMN
145388 && pExpr->pLeft->iColumn>=0
145389 && !ExprHasProperty(pExpr, EP_FromJoin)
145390 && OptimizationEnabled(db, SQLITE_Stat4)
145391 ){
145392 Expr *pNewExpr;
145393 Expr *pLeft = pExpr->pLeft;
145394 int idxNew;
145395 WhereTerm *pNewTerm;
145396
145397 pNewExpr = sqlite3PExpr(pParse, TK_GT,
145398 sqlite3ExprDup(db, pLeft, 0),
145399 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
145400
145401 idxNew = whereClauseInsert(pWC, pNewExpr,
145402 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
145403 if( idxNew ){
145404 pNewTerm = &pWC->a[idxNew];
145405 pNewTerm->prereqRight = 0;
145406 pNewTerm->leftCursor = pLeft->iTable;
145407 pNewTerm->u.leftColumn = pLeft->iColumn;
145408 pNewTerm->eOperator = WO_GT;
145409 markTermAsChild(pWC, idxNew, idxTerm);
145410 pTerm = &pWC->a[idxTerm];
145411 pTerm->wtFlags |= TERM_COPIED;
145412 pNewTerm->prereqAll = pTerm->prereqAll;
145413 }
145414 }
145415#endif /* SQLITE_ENABLE_STAT4 */
145416
145417 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
145418 ** an index for tables to the left of the join.
145419 */
145420 testcase( pTerm!=&pWC->a[idxTerm] );
145421 pTerm = &pWC->a[idxTerm];
145422 pTerm->prereqRight |= extraRight;
145423}
145424
145425/***************************************************************************
145426** Routines with file scope above. Interface to the rest of the where.c
145427** subsystem follows.
145428***************************************************************************/
145429
145430/*
145431** This routine identifies subexpressions in the WHERE clause where
145432** each subexpression is separated by the AND operator or some other
145433** operator specified in the op parameter. The WhereClause structure
145434** is filled with pointers to subexpressions. For example:
145435**
145436** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
145437** \________/ \_______________/ \________________/
145438** slot[0] slot[1] slot[2]
145439**
145440** The original WHERE clause in pExpr is unaltered. All this routine
145441** does is make slot[] entries point to substructure within pExpr.
145442**
145443** In the previous sentence and in the diagram, "slot[]" refers to
145444** the WhereClause.a[] array. The slot[] array grows as needed to contain
145445** all terms of the WHERE clause.
145446*/
145447SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
145448 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
145449 pWC->op = op;
145450 if( pE2==0 ) return;
145451 if( pE2->op!=op ){
145452 whereClauseInsert(pWC, pExpr, 0);
145453 }else{
145454 sqlite3WhereSplit(pWC, pE2->pLeft, op);
145455 sqlite3WhereSplit(pWC, pE2->pRight, op);
145456 }
145457}
145458
145459/*
145460** Initialize a preallocated WhereClause structure.
145461*/
145462SQLITE_PRIVATE void sqlite3WhereClauseInit(
145463 WhereClause *pWC, /* The WhereClause to be initialized */
145464 WhereInfo *pWInfo /* The WHERE processing context */
145465){
145466 pWC->pWInfo = pWInfo;
145467 pWC->hasOr = 0;
145468 pWC->pOuter = 0;
145469 pWC->nTerm = 0;
145470 pWC->nSlot = ArraySize(pWC->aStatic);
145471 pWC->a = pWC->aStatic;
145472}
145473
145474/*
145475** Deallocate a WhereClause structure. The WhereClause structure
145476** itself is not freed. This routine is the inverse of
145477** sqlite3WhereClauseInit().
145478*/
145479SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
145480 int i;
145481 WhereTerm *a;
145482 sqlite3 *db = pWC->pWInfo->pParse->db;
145483 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
145484 if( a->wtFlags & TERM_DYNAMIC ){
145485 sqlite3ExprDelete(db, a->pExpr);
145486 }
145487 if( a->wtFlags & TERM_ORINFO ){
145488 whereOrInfoDelete(db, a->u.pOrInfo);
145489 }else if( a->wtFlags & TERM_ANDINFO ){
145490 whereAndInfoDelete(db, a->u.pAndInfo);
145491 }
145492 }
145493 if( pWC->a!=pWC->aStatic ){
145494 sqlite3DbFree(db, pWC->a);
145495 }
145496}
145497
145498
145499/*
145500** These routines walk (recursively) an expression tree and generate
145501** a bitmask indicating which tables are used in that expression
145502** tree.
145503*/
145504SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
145505 Bitmask mask;
145506 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
145507 return sqlite3WhereGetMask(pMaskSet, p->iTable);
145508 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
145509 assert( p->op!=TK_IF_NULL_ROW );
145510 return 0;
145511 }
145512 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
145513 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
145514 if( p->pRight ){
145515 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
145516 assert( p->x.pList==0 );
145517 }else if( ExprHasProperty(p, EP_xIsSelect) ){
145518 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
145519 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
145520 }else if( p->x.pList ){
145521 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
145522 }
145523#ifndef SQLITE_OMIT_WINDOWFUNC
145524 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && p->y.pWin ){
145525 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
145526 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
145527 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
145528 }
145529#endif
145530 return mask;
145531}
145532SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
145533 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
145534}
145535SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
145536 int i;
145537 Bitmask mask = 0;
145538 if( pList ){
145539 for(i=0; i<pList->nExpr; i++){
145540 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
145541 }
145542 }
145543 return mask;
145544}
145545
145546
145547/*
145548** Call exprAnalyze on all terms in a WHERE clause.
145549**
145550** Note that exprAnalyze() might add new virtual terms onto the
145551** end of the WHERE clause. We do not want to analyze these new
145552** virtual terms, so start analyzing at the end and work forward
145553** so that the added virtual terms are never processed.
145554*/
145555SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
145556 SrcList *pTabList, /* the FROM clause */
145557 WhereClause *pWC /* the WHERE clause to be analyzed */
145558){
145559 int i;
145560 for(i=pWC->nTerm-1; i>=0; i--){
145561 exprAnalyze(pTabList, pWC, i);
145562 }
145563}
145564
145565/*
145566** For table-valued-functions, transform the function arguments into
145567** new WHERE clause terms.
145568**
145569** Each function argument translates into an equality constraint against
145570** a HIDDEN column in the table.
145571*/
145572SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
145573 Parse *pParse, /* Parsing context */
145574 struct SrcList_item *pItem, /* The FROM clause term to process */
145575 WhereClause *pWC /* Xfer function arguments to here */
145576){
145577 Table *pTab;
145578 int j, k;
145579 ExprList *pArgs;
145580 Expr *pColRef;
145581 Expr *pTerm;
145582 if( pItem->fg.isTabFunc==0 ) return;
145583 pTab = pItem->pTab;
145584 assert( pTab!=0 );
145585 pArgs = pItem->u1.pFuncArg;
145586 if( pArgs==0 ) return;
145587 for(j=k=0; j<pArgs->nExpr; j++){
145588 Expr *pRhs;
145589 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
145590 if( k>=pTab->nCol ){
145591 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
145592 pTab->zName, j);
145593 return;
145594 }
145595 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
145596 if( pColRef==0 ) return;
145597 pColRef->iTable = pItem->iCursor;
145598 pColRef->iColumn = k++;
145599 pColRef->y.pTab = pTab;
145600 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
145601 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
145602 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
145603 if( pItem->fg.jointype & JT_LEFT ){
145604 sqlite3SetJoinExpr(pTerm, pItem->iCursor);
145605 }
145606 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
145607 }
145608}
145609
145610/************** End of whereexpr.c *******************************************/
145611/************** Begin file where.c *******************************************/
145612/*
145613** 2001 September 15
145614**
145615** The author disclaims copyright to this source code. In place of
145616** a legal notice, here is a blessing:
145617**
145618** May you do good and not evil.
145619** May you find forgiveness for yourself and forgive others.
145620** May you share freely, never taking more than you give.
145621**
145622*************************************************************************
145623** This module contains C code that generates VDBE code used to process
145624** the WHERE clause of SQL statements. This module is responsible for
145625** generating the code that loops through a table looking for applicable
145626** rows. Indices are selected and used to speed the search when doing
145627** so is applicable. Because this module is responsible for selecting
145628** indices, you might also think of this module as the "query optimizer".
145629*/
145630/* #include "sqliteInt.h" */
145631/* #include "whereInt.h" */
145632
145633/*
145634** Extra information appended to the end of sqlite3_index_info but not
145635** visible to the xBestIndex function, at least not directly. The
145636** sqlite3_vtab_collation() interface knows how to reach it, however.
145637**
145638** This object is not an API and can be changed from one release to the
145639** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
145640** agree on the structure, all will be well.
145641*/
145642typedef struct HiddenIndexInfo HiddenIndexInfo;
145643struct HiddenIndexInfo {
145644 WhereClause *pWC; /* The Where clause being analyzed */
145645 Parse *pParse; /* The parsing context */
145646};
145647
145648/* Forward declaration of methods */
145649static int whereLoopResize(sqlite3*, WhereLoop*, int);
145650
145651/* Test variable that can be set to enable WHERE tracing */
145652#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
145653/***/ int sqlite3WhereTrace = 0;
145654#endif
145655
145656
145657/*
145658** Return the estimated number of output rows from a WHERE clause
145659*/
145660SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
145661 return pWInfo->nRowOut;
145662}
145663
145664/*
145665** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
145666** WHERE clause returns outputs for DISTINCT processing.
145667*/
145668SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
145669 return pWInfo->eDistinct;
145670}
145671
145672/*
145673** Return the number of ORDER BY terms that are satisfied by the
145674** WHERE clause. A return of 0 means that the output must be
145675** completely sorted. A return equal to the number of ORDER BY
145676** terms means that no sorting is needed at all. A return that
145677** is positive but less than the number of ORDER BY terms means that
145678** block sorting is required.
145679*/
145680SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
145681 return pWInfo->nOBSat;
145682}
145683
145684/*
145685** In the ORDER BY LIMIT optimization, if the inner-most loop is known
145686** to emit rows in increasing order, and if the last row emitted by the
145687** inner-most loop did not fit within the sorter, then we can skip all
145688** subsequent rows for the current iteration of the inner loop (because they
145689** will not fit in the sorter either) and continue with the second inner
145690** loop - the loop immediately outside the inner-most.
145691**
145692** When a row does not fit in the sorter (because the sorter already
145693** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
145694** label returned by this function.
145695**
145696** If the ORDER BY LIMIT optimization applies, the jump destination should
145697** be the continuation for the second-inner-most loop. If the ORDER BY
145698** LIMIT optimization does not apply, then the jump destination should
145699** be the continuation for the inner-most loop.
145700**
145701** It is always safe for this routine to return the continuation of the
145702** inner-most loop, in the sense that a correct answer will result.
145703** Returning the continuation the second inner loop is an optimization
145704** that might make the code run a little faster, but should not change
145705** the final answer.
145706*/
145707SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
145708 WhereLevel *pInner;
145709 if( !pWInfo->bOrderedInnerLoop ){
145710 /* The ORDER BY LIMIT optimization does not apply. Jump to the
145711 ** continuation of the inner-most loop. */
145712 return pWInfo->iContinue;
145713 }
145714 pInner = &pWInfo->a[pWInfo->nLevel-1];
145715 assert( pInner->addrNxt!=0 );
145716 return pInner->addrNxt;
145717}
145718
145719/*
145720** Return the VDBE address or label to jump to in order to continue
145721** immediately with the next row of a WHERE clause.
145722*/
145723SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
145724 assert( pWInfo->iContinue!=0 );
145725 return pWInfo->iContinue;
145726}
145727
145728/*
145729** Return the VDBE address or label to jump to in order to break
145730** out of a WHERE loop.
145731*/
145732SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
145733 return pWInfo->iBreak;
145734}
145735
145736/*
145737** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
145738** operate directly on the rowids returned by a WHERE clause. Return
145739** ONEPASS_SINGLE (1) if the statement can operation directly because only
145740** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
145741** optimization can be used on multiple
145742**
145743** If the ONEPASS optimization is used (if this routine returns true)
145744** then also write the indices of open cursors used by ONEPASS
145745** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
145746** table and iaCur[1] gets the cursor used by an auxiliary index.
145747** Either value may be -1, indicating that cursor is not used.
145748** Any cursors returned will have been opened for writing.
145749**
145750** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
145751** unable to use the ONEPASS optimization.
145752*/
145753SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
145754 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
145755#ifdef WHERETRACE_ENABLED
145756 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
145757 sqlite3DebugPrintf("%s cursors: %d %d\n",
145758 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
145759 aiCur[0], aiCur[1]);
145760 }
145761#endif
145762 return pWInfo->eOnePass;
145763}
145764
145765/*
145766** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
145767** the data cursor to the row selected by the index cursor.
145768*/
145769SQLITE_PRIVATE int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
145770 return pWInfo->bDeferredSeek;
145771}
145772
145773/*
145774** Move the content of pSrc into pDest
145775*/
145776static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
145777 pDest->n = pSrc->n;
145778 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
145779}
145780
145781/*
145782** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
145783**
145784** The new entry might overwrite an existing entry, or it might be
145785** appended, or it might be discarded. Do whatever is the right thing
145786** so that pSet keeps the N_OR_COST best entries seen so far.
145787*/
145788static int whereOrInsert(
145789 WhereOrSet *pSet, /* The WhereOrSet to be updated */
145790 Bitmask prereq, /* Prerequisites of the new entry */
145791 LogEst rRun, /* Run-cost of the new entry */
145792 LogEst nOut /* Number of outputs for the new entry */
145793){
145794 u16 i;
145795 WhereOrCost *p;
145796 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
145797 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
145798 goto whereOrInsert_done;
145799 }
145800 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
145801 return 0;
145802 }
145803 }
145804 if( pSet->n<N_OR_COST ){
145805 p = &pSet->a[pSet->n++];
145806 p->nOut = nOut;
145807 }else{
145808 p = pSet->a;
145809 for(i=1; i<pSet->n; i++){
145810 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
145811 }
145812 if( p->rRun<=rRun ) return 0;
145813 }
145814whereOrInsert_done:
145815 p->prereq = prereq;
145816 p->rRun = rRun;
145817 if( p->nOut>nOut ) p->nOut = nOut;
145818 return 1;
145819}
145820
145821/*
145822** Return the bitmask for the given cursor number. Return 0 if
145823** iCursor is not in the set.
145824*/
145825SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
145826 int i;
145827 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
145828 for(i=0; i<pMaskSet->n; i++){
145829 if( pMaskSet->ix[i]==iCursor ){
145830 return MASKBIT(i);
145831 }
145832 }
145833 return 0;
145834}
145835
145836/*
145837** Create a new mask for cursor iCursor.
145838**
145839** There is one cursor per table in the FROM clause. The number of
145840** tables in the FROM clause is limited by a test early in the
145841** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
145842** array will never overflow.
145843*/
145844static void createMask(WhereMaskSet *pMaskSet, int iCursor){
145845 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
145846 pMaskSet->ix[pMaskSet->n++] = iCursor;
145847}
145848
145849/*
145850** Advance to the next WhereTerm that matches according to the criteria
145851** established when the pScan object was initialized by whereScanInit().
145852** Return NULL if there are no more matching WhereTerms.
145853*/
145854static WhereTerm *whereScanNext(WhereScan *pScan){
145855 int iCur; /* The cursor on the LHS of the term */
145856 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
145857 Expr *pX; /* An expression being tested */
145858 WhereClause *pWC; /* Shorthand for pScan->pWC */
145859 WhereTerm *pTerm; /* The term being tested */
145860 int k = pScan->k; /* Where to start scanning */
145861
145862 assert( pScan->iEquiv<=pScan->nEquiv );
145863 pWC = pScan->pWC;
145864 while(1){
145865 iColumn = pScan->aiColumn[pScan->iEquiv-1];
145866 iCur = pScan->aiCur[pScan->iEquiv-1];
145867 assert( pWC!=0 );
145868 do{
145869 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
145870 if( pTerm->leftCursor==iCur
145871 && pTerm->u.leftColumn==iColumn
145872 && (iColumn!=XN_EXPR
145873 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
145874 pScan->pIdxExpr,iCur)==0)
145875 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
145876 ){
145877 if( (pTerm->eOperator & WO_EQUIV)!=0
145878 && pScan->nEquiv<ArraySize(pScan->aiCur)
145879 && (pX = sqlite3ExprSkipCollateAndLikely(pTerm->pExpr->pRight))->op
145880 ==TK_COLUMN
145881 ){
145882 int j;
145883 for(j=0; j<pScan->nEquiv; j++){
145884 if( pScan->aiCur[j]==pX->iTable
145885 && pScan->aiColumn[j]==pX->iColumn ){
145886 break;
145887 }
145888 }
145889 if( j==pScan->nEquiv ){
145890 pScan->aiCur[j] = pX->iTable;
145891 pScan->aiColumn[j] = pX->iColumn;
145892 pScan->nEquiv++;
145893 }
145894 }
145895 if( (pTerm->eOperator & pScan->opMask)!=0 ){
145896 /* Verify the affinity and collating sequence match */
145897 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
145898 CollSeq *pColl;
145899 Parse *pParse = pWC->pWInfo->pParse;
145900 pX = pTerm->pExpr;
145901 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
145902 continue;
145903 }
145904 assert(pX->pLeft);
145905 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
145906 if( pColl==0 ) pColl = pParse->db->pDfltColl;
145907 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
145908 continue;
145909 }
145910 }
145911 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
145912 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
145913 && pX->iTable==pScan->aiCur[0]
145914 && pX->iColumn==pScan->aiColumn[0]
145915 ){
145916 testcase( pTerm->eOperator & WO_IS );
145917 continue;
145918 }
145919 pScan->pWC = pWC;
145920 pScan->k = k+1;
145921 return pTerm;
145922 }
145923 }
145924 }
145925 pWC = pWC->pOuter;
145926 k = 0;
145927 }while( pWC!=0 );
145928 if( pScan->iEquiv>=pScan->nEquiv ) break;
145929 pWC = pScan->pOrigWC;
145930 k = 0;
145931 pScan->iEquiv++;
145932 }
145933 return 0;
145934}
145935
145936/*
145937** This is whereScanInit() for the case of an index on an expression.
145938** It is factored out into a separate tail-recursion subroutine so that
145939** the normal whereScanInit() routine, which is a high-runner, does not
145940** need to push registers onto the stack as part of its prologue.
145941*/
145942static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
145943 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
145944 return whereScanNext(pScan);
145945}
145946
145947/*
145948** Initialize a WHERE clause scanner object. Return a pointer to the
145949** first match. Return NULL if there are no matches.
145950**
145951** The scanner will be searching the WHERE clause pWC. It will look
145952** for terms of the form "X <op> <expr>" where X is column iColumn of table
145953** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
145954** must be one of the indexes of table iCur.
145955**
145956** The <op> must be one of the operators described by opMask.
145957**
145958** If the search is for X and the WHERE clause contains terms of the
145959** form X=Y then this routine might also return terms of the form
145960** "Y <op> <expr>". The number of levels of transitivity is limited,
145961** but is enough to handle most commonly occurring SQL statements.
145962**
145963** If X is not the INTEGER PRIMARY KEY then X must be compatible with
145964** index pIdx.
145965*/
145966static WhereTerm *whereScanInit(
145967 WhereScan *pScan, /* The WhereScan object being initialized */
145968 WhereClause *pWC, /* The WHERE clause to be scanned */
145969 int iCur, /* Cursor to scan for */
145970 int iColumn, /* Column to scan for */
145971 u32 opMask, /* Operator(s) to scan for */
145972 Index *pIdx /* Must be compatible with this index */
145973){
145974 pScan->pOrigWC = pWC;
145975 pScan->pWC = pWC;
145976 pScan->pIdxExpr = 0;
145977 pScan->idxaff = 0;
145978 pScan->zCollName = 0;
145979 pScan->opMask = opMask;
145980 pScan->k = 0;
145981 pScan->aiCur[0] = iCur;
145982 pScan->nEquiv = 1;
145983 pScan->iEquiv = 1;
145984 if( pIdx ){
145985 int j = iColumn;
145986 iColumn = pIdx->aiColumn[j];
145987 if( iColumn==XN_EXPR ){
145988 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
145989 pScan->zCollName = pIdx->azColl[j];
145990 pScan->aiColumn[0] = XN_EXPR;
145991 return whereScanInitIndexExpr(pScan);
145992 }else if( iColumn==pIdx->pTable->iPKey ){
145993 iColumn = XN_ROWID;
145994 }else if( iColumn>=0 ){
145995 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
145996 pScan->zCollName = pIdx->azColl[j];
145997 }
145998 }else if( iColumn==XN_EXPR ){
145999 return 0;
146000 }
146001 pScan->aiColumn[0] = iColumn;
146002 return whereScanNext(pScan);
146003}
146004
146005/*
146006** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
146007** where X is a reference to the iColumn of table iCur or of index pIdx
146008** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
146009** the op parameter. Return a pointer to the term. Return 0 if not found.
146010**
146011** If pIdx!=0 then it must be one of the indexes of table iCur.
146012** Search for terms matching the iColumn-th column of pIdx
146013** rather than the iColumn-th column of table iCur.
146014**
146015** The term returned might by Y=<expr> if there is another constraint in
146016** the WHERE clause that specifies that X=Y. Any such constraints will be
146017** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
146018** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
146019** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
146020** other equivalent values. Hence a search for X will return <expr> if X=A1
146021** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
146022**
146023** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
146024** then try for the one with no dependencies on <expr> - in other words where
146025** <expr> is a constant expression of some kind. Only return entries of
146026** the form "X <op> Y" where Y is a column in another table if no terms of
146027** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
146028** exist, try to return a term that does not use WO_EQUIV.
146029*/
146030SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
146031 WhereClause *pWC, /* The WHERE clause to be searched */
146032 int iCur, /* Cursor number of LHS */
146033 int iColumn, /* Column number of LHS */
146034 Bitmask notReady, /* RHS must not overlap with this mask */
146035 u32 op, /* Mask of WO_xx values describing operator */
146036 Index *pIdx /* Must be compatible with this index, if not NULL */
146037){
146038 WhereTerm *pResult = 0;
146039 WhereTerm *p;
146040 WhereScan scan;
146041
146042 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
146043 op &= WO_EQ|WO_IS;
146044 while( p ){
146045 if( (p->prereqRight & notReady)==0 ){
146046 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
146047 testcase( p->eOperator & WO_IS );
146048 return p;
146049 }
146050 if( pResult==0 ) pResult = p;
146051 }
146052 p = whereScanNext(&scan);
146053 }
146054 return pResult;
146055}
146056
146057/*
146058** This function searches pList for an entry that matches the iCol-th column
146059** of index pIdx.
146060**
146061** If such an expression is found, its index in pList->a[] is returned. If
146062** no expression is found, -1 is returned.
146063*/
146064static int findIndexCol(
146065 Parse *pParse, /* Parse context */
146066 ExprList *pList, /* Expression list to search */
146067 int iBase, /* Cursor for table associated with pIdx */
146068 Index *pIdx, /* Index to match column of */
146069 int iCol /* Column of index to match */
146070){
146071 int i;
146072 const char *zColl = pIdx->azColl[iCol];
146073
146074 for(i=0; i<pList->nExpr; i++){
146075 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
146076 if( p->op==TK_COLUMN
146077 && p->iColumn==pIdx->aiColumn[iCol]
146078 && p->iTable==iBase
146079 ){
146080 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
146081 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
146082 return i;
146083 }
146084 }
146085 }
146086
146087 return -1;
146088}
146089
146090/*
146091** Return TRUE if the iCol-th column of index pIdx is NOT NULL
146092*/
146093static int indexColumnNotNull(Index *pIdx, int iCol){
146094 int j;
146095 assert( pIdx!=0 );
146096 assert( iCol>=0 && iCol<pIdx->nColumn );
146097 j = pIdx->aiColumn[iCol];
146098 if( j>=0 ){
146099 return pIdx->pTable->aCol[j].notNull;
146100 }else if( j==(-1) ){
146101 return 1;
146102 }else{
146103 assert( j==(-2) );
146104 return 0; /* Assume an indexed expression can always yield a NULL */
146105
146106 }
146107}
146108
146109/*
146110** Return true if the DISTINCT expression-list passed as the third argument
146111** is redundant.
146112**
146113** A DISTINCT list is redundant if any subset of the columns in the
146114** DISTINCT list are collectively unique and individually non-null.
146115*/
146116static int isDistinctRedundant(
146117 Parse *pParse, /* Parsing context */
146118 SrcList *pTabList, /* The FROM clause */
146119 WhereClause *pWC, /* The WHERE clause */
146120 ExprList *pDistinct /* The result set that needs to be DISTINCT */
146121){
146122 Table *pTab;
146123 Index *pIdx;
146124 int i;
146125 int iBase;
146126
146127 /* If there is more than one table or sub-select in the FROM clause of
146128 ** this query, then it will not be possible to show that the DISTINCT
146129 ** clause is redundant. */
146130 if( pTabList->nSrc!=1 ) return 0;
146131 iBase = pTabList->a[0].iCursor;
146132 pTab = pTabList->a[0].pTab;
146133
146134 /* If any of the expressions is an IPK column on table iBase, then return
146135 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
146136 ** current SELECT is a correlated sub-query.
146137 */
146138 for(i=0; i<pDistinct->nExpr; i++){
146139 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
146140 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
146141 }
146142
146143 /* Loop through all indices on the table, checking each to see if it makes
146144 ** the DISTINCT qualifier redundant. It does so if:
146145 **
146146 ** 1. The index is itself UNIQUE, and
146147 **
146148 ** 2. All of the columns in the index are either part of the pDistinct
146149 ** list, or else the WHERE clause contains a term of the form "col=X",
146150 ** where X is a constant value. The collation sequences of the
146151 ** comparison and select-list expressions must match those of the index.
146152 **
146153 ** 3. All of those index columns for which the WHERE clause does not
146154 ** contain a "col=X" term are subject to a NOT NULL constraint.
146155 */
146156 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146157 if( !IsUniqueIndex(pIdx) ) continue;
146158 for(i=0; i<pIdx->nKeyCol; i++){
146159 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
146160 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
146161 if( indexColumnNotNull(pIdx, i)==0 ) break;
146162 }
146163 }
146164 if( i==pIdx->nKeyCol ){
146165 /* This index implies that the DISTINCT qualifier is redundant. */
146166 return 1;
146167 }
146168 }
146169
146170 return 0;
146171}
146172
146173
146174/*
146175** Estimate the logarithm of the input value to base 2.
146176*/
146177static LogEst estLog(LogEst N){
146178 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
146179}
146180
146181/*
146182** Convert OP_Column opcodes to OP_Copy in previously generated code.
146183**
146184** This routine runs over generated VDBE code and translates OP_Column
146185** opcodes into OP_Copy when the table is being accessed via co-routine
146186** instead of via table lookup.
146187**
146188** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
146189** cursor iTabCur are transformed into OP_Sequence opcode for the
146190** iAutoidxCur cursor, in order to generate unique rowids for the
146191** automatic index being generated.
146192*/
146193static void translateColumnToCopy(
146194 Parse *pParse, /* Parsing context */
146195 int iStart, /* Translate from this opcode to the end */
146196 int iTabCur, /* OP_Column/OP_Rowid references to this table */
146197 int iRegister, /* The first column is in this register */
146198 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
146199){
146200 Vdbe *v = pParse->pVdbe;
146201 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
146202 int iEnd = sqlite3VdbeCurrentAddr(v);
146203 if( pParse->db->mallocFailed ) return;
146204 for(; iStart<iEnd; iStart++, pOp++){
146205 if( pOp->p1!=iTabCur ) continue;
146206 if( pOp->opcode==OP_Column ){
146207 pOp->opcode = OP_Copy;
146208 pOp->p1 = pOp->p2 + iRegister;
146209 pOp->p2 = pOp->p3;
146210 pOp->p3 = 0;
146211 }else if( pOp->opcode==OP_Rowid ){
146212 if( iAutoidxCur ){
146213 pOp->opcode = OP_Sequence;
146214 pOp->p1 = iAutoidxCur;
146215 }else{
146216 pOp->opcode = OP_Null;
146217 pOp->p1 = 0;
146218 pOp->p3 = 0;
146219 }
146220 }
146221 }
146222}
146223
146224/*
146225** Two routines for printing the content of an sqlite3_index_info
146226** structure. Used for testing and debugging only. If neither
146227** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
146228** are no-ops.
146229*/
146230#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
146231static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
146232 int i;
146233 if( !sqlite3WhereTrace ) return;
146234 for(i=0; i<p->nConstraint; i++){
146235 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
146236 i,
146237 p->aConstraint[i].iColumn,
146238 p->aConstraint[i].iTermOffset,
146239 p->aConstraint[i].op,
146240 p->aConstraint[i].usable);
146241 }
146242 for(i=0; i<p->nOrderBy; i++){
146243 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
146244 i,
146245 p->aOrderBy[i].iColumn,
146246 p->aOrderBy[i].desc);
146247 }
146248}
146249static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){
146250 int i;
146251 if( !sqlite3WhereTrace ) return;
146252 for(i=0; i<p->nConstraint; i++){
146253 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
146254 i,
146255 p->aConstraintUsage[i].argvIndex,
146256 p->aConstraintUsage[i].omit);
146257 }
146258 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
146259 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
146260 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
146261 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
146262 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
146263}
146264#else
146265#define whereTraceIndexInfoInputs(A)
146266#define whereTraceIndexInfoOutputs(A)
146267#endif
146268
146269#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
146270/*
146271** Return TRUE if the WHERE clause term pTerm is of a form where it
146272** could be used with an index to access pSrc, assuming an appropriate
146273** index existed.
146274*/
146275static int termCanDriveIndex(
146276 WhereTerm *pTerm, /* WHERE clause term to check */
146277 struct SrcList_item *pSrc, /* Table we are trying to access */
146278 Bitmask notReady /* Tables in outer loops of the join */
146279){
146280 char aff;
146281 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
146282 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
146283 if( (pSrc->fg.jointype & JT_LEFT)
146284 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
146285 && (pTerm->eOperator & WO_IS)
146286 ){
146287 /* Cannot use an IS term from the WHERE clause as an index driver for
146288 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
146289 ** the ON clause. */
146290 return 0;
146291 }
146292 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
146293 if( pTerm->u.leftColumn<0 ) return 0;
146294 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
146295 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
146296 testcase( pTerm->pExpr->op==TK_IS );
146297 return 1;
146298}
146299#endif
146300
146301
146302#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
146303/*
146304** Generate code to construct the Index object for an automatic index
146305** and to set up the WhereLevel object pLevel so that the code generator
146306** makes use of the automatic index.
146307*/
146308static void constructAutomaticIndex(
146309 Parse *pParse, /* The parsing context */
146310 WhereClause *pWC, /* The WHERE clause */
146311 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
146312 Bitmask notReady, /* Mask of cursors that are not available */
146313 WhereLevel *pLevel /* Write new index here */
146314){
146315 int nKeyCol; /* Number of columns in the constructed index */
146316 WhereTerm *pTerm; /* A single term of the WHERE clause */
146317 WhereTerm *pWCEnd; /* End of pWC->a[] */
146318 Index *pIdx; /* Object describing the transient index */
146319 Vdbe *v; /* Prepared statement under construction */
146320 int addrInit; /* Address of the initialization bypass jump */
146321 Table *pTable; /* The table being indexed */
146322 int addrTop; /* Top of the index fill loop */
146323 int regRecord; /* Register holding an index record */
146324 int n; /* Column counter */
146325 int i; /* Loop counter */
146326 int mxBitCol; /* Maximum column in pSrc->colUsed */
146327 CollSeq *pColl; /* Collating sequence to on a column */
146328 WhereLoop *pLoop; /* The Loop object */
146329 char *zNotUsed; /* Extra space on the end of pIdx */
146330 Bitmask idxCols; /* Bitmap of columns used for indexing */
146331 Bitmask extraCols; /* Bitmap of additional columns */
146332 u8 sentWarning = 0; /* True if a warnning has been issued */
146333 Expr *pPartial = 0; /* Partial Index Expression */
146334 int iContinue = 0; /* Jump here to skip excluded rows */
146335 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
146336 int addrCounter = 0; /* Address where integer counter is initialized */
146337 int regBase; /* Array of registers where record is assembled */
146338
146339 /* Generate code to skip over the creation and initialization of the
146340 ** transient index on 2nd and subsequent iterations of the loop. */
146341 v = pParse->pVdbe;
146342 assert( v!=0 );
146343 addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
146344
146345 /* Count the number of columns that will be added to the index
146346 ** and used to match WHERE clause constraints */
146347 nKeyCol = 0;
146348 pTable = pSrc->pTab;
146349 pWCEnd = &pWC->a[pWC->nTerm];
146350 pLoop = pLevel->pWLoop;
146351 idxCols = 0;
146352 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
146353 Expr *pExpr = pTerm->pExpr;
146354 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
146355 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
146356 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
146357 if( pLoop->prereq==0
146358 && (pTerm->wtFlags & TERM_VIRTUAL)==0
146359 && !ExprHasProperty(pExpr, EP_FromJoin)
146360 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
146361 pPartial = sqlite3ExprAnd(pParse, pPartial,
146362 sqlite3ExprDup(pParse->db, pExpr, 0));
146363 }
146364 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
146365 int iCol = pTerm->u.leftColumn;
146366 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
146367 testcase( iCol==BMS );
146368 testcase( iCol==BMS-1 );
146369 if( !sentWarning ){
146370 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
146371 "automatic index on %s(%s)", pTable->zName,
146372 pTable->aCol[iCol].zName);
146373 sentWarning = 1;
146374 }
146375 if( (idxCols & cMask)==0 ){
146376 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
146377 goto end_auto_index_create;
146378 }
146379 pLoop->aLTerm[nKeyCol++] = pTerm;
146380 idxCols |= cMask;
146381 }
146382 }
146383 }
146384 assert( nKeyCol>0 );
146385 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
146386 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
146387 | WHERE_AUTO_INDEX;
146388
146389 /* Count the number of additional columns needed to create a
146390 ** covering index. A "covering index" is an index that contains all
146391 ** columns that are needed by the query. With a covering index, the
146392 ** original table never needs to be accessed. Automatic indices must
146393 ** be a covering index because the index will not be updated if the
146394 ** original table changes and the index and table cannot both be used
146395 ** if they go out of sync.
146396 */
146397 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
146398 mxBitCol = MIN(BMS-1,pTable->nCol);
146399 testcase( pTable->nCol==BMS-1 );
146400 testcase( pTable->nCol==BMS-2 );
146401 for(i=0; i<mxBitCol; i++){
146402 if( extraCols & MASKBIT(i) ) nKeyCol++;
146403 }
146404 if( pSrc->colUsed & MASKBIT(BMS-1) ){
146405 nKeyCol += pTable->nCol - BMS + 1;
146406 }
146407
146408 /* Construct the Index object to describe this index */
146409 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
146410 if( pIdx==0 ) goto end_auto_index_create;
146411 pLoop->u.btree.pIndex = pIdx;
146412 pIdx->zName = "auto-index";
146413 pIdx->pTable = pTable;
146414 n = 0;
146415 idxCols = 0;
146416 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
146417 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
146418 int iCol = pTerm->u.leftColumn;
146419 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
146420 testcase( iCol==BMS-1 );
146421 testcase( iCol==BMS );
146422 if( (idxCols & cMask)==0 ){
146423 Expr *pX = pTerm->pExpr;
146424 idxCols |= cMask;
146425 pIdx->aiColumn[n] = pTerm->u.leftColumn;
146426 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
146427 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
146428 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
146429 n++;
146430 }
146431 }
146432 }
146433 assert( (u32)n==pLoop->u.btree.nEq );
146434
146435 /* Add additional columns needed to make the automatic index into
146436 ** a covering index */
146437 for(i=0; i<mxBitCol; i++){
146438 if( extraCols & MASKBIT(i) ){
146439 pIdx->aiColumn[n] = i;
146440 pIdx->azColl[n] = sqlite3StrBINARY;
146441 n++;
146442 }
146443 }
146444 if( pSrc->colUsed & MASKBIT(BMS-1) ){
146445 for(i=BMS-1; i<pTable->nCol; i++){
146446 pIdx->aiColumn[n] = i;
146447 pIdx->azColl[n] = sqlite3StrBINARY;
146448 n++;
146449 }
146450 }
146451 assert( n==nKeyCol );
146452 pIdx->aiColumn[n] = XN_ROWID;
146453 pIdx->azColl[n] = sqlite3StrBINARY;
146454
146455 /* Create the automatic index */
146456 assert( pLevel->iIdxCur>=0 );
146457 pLevel->iIdxCur = pParse->nTab++;
146458 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
146459 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
146460 VdbeComment((v, "for %s", pTable->zName));
146461
146462 /* Fill the automatic index with content */
146463 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
146464 if( pTabItem->fg.viaCoroutine ){
146465 int regYield = pTabItem->regReturn;
146466 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
146467 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
146468 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
146469 VdbeCoverage(v);
146470 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
146471 }else{
146472 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
146473 }
146474 if( pPartial ){
146475 iContinue = sqlite3VdbeMakeLabel(pParse);
146476 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
146477 pLoop->wsFlags |= WHERE_PARTIALIDX;
146478 }
146479 regRecord = sqlite3GetTempReg(pParse);
146480 regBase = sqlite3GenerateIndexKey(
146481 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
146482 );
146483 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
146484 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
146485 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
146486 if( pTabItem->fg.viaCoroutine ){
146487 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
146488 testcase( pParse->db->mallocFailed );
146489 assert( pLevel->iIdxCur>0 );
146490 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
146491 pTabItem->regResult, pLevel->iIdxCur);
146492 sqlite3VdbeGoto(v, addrTop);
146493 pTabItem->fg.viaCoroutine = 0;
146494 }else{
146495 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
146496 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
146497 }
146498 sqlite3VdbeJumpHere(v, addrTop);
146499 sqlite3ReleaseTempReg(pParse, regRecord);
146500
146501 /* Jump here when skipping the initialization */
146502 sqlite3VdbeJumpHere(v, addrInit);
146503
146504end_auto_index_create:
146505 sqlite3ExprDelete(pParse->db, pPartial);
146506}
146507#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
146508
146509#ifndef SQLITE_OMIT_VIRTUALTABLE
146510/*
146511** Allocate and populate an sqlite3_index_info structure. It is the
146512** responsibility of the caller to eventually release the structure
146513** by passing the pointer returned by this function to sqlite3_free().
146514*/
146515static sqlite3_index_info *allocateIndexInfo(
146516 Parse *pParse, /* The parsing context */
146517 WhereClause *pWC, /* The WHERE clause being analyzed */
146518 Bitmask mUnusable, /* Ignore terms with these prereqs */
146519 struct SrcList_item *pSrc, /* The FROM clause term that is the vtab */
146520 ExprList *pOrderBy, /* The ORDER BY clause */
146521 u16 *pmNoOmit /* Mask of terms not to omit */
146522){
146523 int i, j;
146524 int nTerm;
146525 struct sqlite3_index_constraint *pIdxCons;
146526 struct sqlite3_index_orderby *pIdxOrderBy;
146527 struct sqlite3_index_constraint_usage *pUsage;
146528 struct HiddenIndexInfo *pHidden;
146529 WhereTerm *pTerm;
146530 int nOrderBy;
146531 sqlite3_index_info *pIdxInfo;
146532 u16 mNoOmit = 0;
146533
146534 /* Count the number of possible WHERE clause constraints referring
146535 ** to this virtual table */
146536 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
146537 if( pTerm->leftCursor != pSrc->iCursor ) continue;
146538 if( pTerm->prereqRight & mUnusable ) continue;
146539 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
146540 testcase( pTerm->eOperator & WO_IN );
146541 testcase( pTerm->eOperator & WO_ISNULL );
146542 testcase( pTerm->eOperator & WO_IS );
146543 testcase( pTerm->eOperator & WO_ALL );
146544 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
146545 if( pTerm->wtFlags & TERM_VNULL ) continue;
146546 assert( pTerm->u.leftColumn>=(-1) );
146547 nTerm++;
146548 }
146549
146550 /* If the ORDER BY clause contains only columns in the current
146551 ** virtual table then allocate space for the aOrderBy part of
146552 ** the sqlite3_index_info structure.
146553 */
146554 nOrderBy = 0;
146555 if( pOrderBy ){
146556 int n = pOrderBy->nExpr;
146557 for(i=0; i<n; i++){
146558 Expr *pExpr = pOrderBy->a[i].pExpr;
146559 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
146560 if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
146561 }
146562 if( i==n){
146563 nOrderBy = n;
146564 }
146565 }
146566
146567 /* Allocate the sqlite3_index_info structure
146568 */
146569 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
146570 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
146571 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
146572 if( pIdxInfo==0 ){
146573 sqlite3ErrorMsg(pParse, "out of memory");
146574 return 0;
146575 }
146576 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
146577 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
146578 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
146579 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
146580 pIdxInfo->nOrderBy = nOrderBy;
146581 pIdxInfo->aConstraint = pIdxCons;
146582 pIdxInfo->aOrderBy = pIdxOrderBy;
146583 pIdxInfo->aConstraintUsage = pUsage;
146584 pHidden->pWC = pWC;
146585 pHidden->pParse = pParse;
146586 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
146587 u16 op;
146588 if( pTerm->leftCursor != pSrc->iCursor ) continue;
146589 if( pTerm->prereqRight & mUnusable ) continue;
146590 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
146591 testcase( pTerm->eOperator & WO_IN );
146592 testcase( pTerm->eOperator & WO_IS );
146593 testcase( pTerm->eOperator & WO_ISNULL );
146594 testcase( pTerm->eOperator & WO_ALL );
146595 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
146596 if( pTerm->wtFlags & TERM_VNULL ) continue;
146597
146598 /* tag-20191211-002: WHERE-clause constraints are not useful to the
146599 ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
146600 ** equivalent restriction for ordinary tables. */
146601 if( (pSrc->fg.jointype & JT_LEFT)!=0
146602 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
146603 ){
146604 continue;
146605 }
146606 assert( pTerm->u.leftColumn>=(-1) );
146607 pIdxCons[j].iColumn = pTerm->u.leftColumn;
146608 pIdxCons[j].iTermOffset = i;
146609 op = pTerm->eOperator & WO_ALL;
146610 if( op==WO_IN ) op = WO_EQ;
146611 if( op==WO_AUX ){
146612 pIdxCons[j].op = pTerm->eMatchOp;
146613 }else if( op & (WO_ISNULL|WO_IS) ){
146614 if( op==WO_ISNULL ){
146615 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
146616 }else{
146617 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
146618 }
146619 }else{
146620 pIdxCons[j].op = (u8)op;
146621 /* The direct assignment in the previous line is possible only because
146622 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
146623 ** following asserts verify this fact. */
146624 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
146625 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
146626 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
146627 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
146628 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
146629 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
146630
146631 if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
146632 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
146633 ){
146634 testcase( j!=i );
146635 if( j<16 ) mNoOmit |= (1 << j);
146636 if( op==WO_LT ) pIdxCons[j].op = WO_LE;
146637 if( op==WO_GT ) pIdxCons[j].op = WO_GE;
146638 }
146639 }
146640
146641 j++;
146642 }
146643 pIdxInfo->nConstraint = j;
146644 for(i=0; i<nOrderBy; i++){
146645 Expr *pExpr = pOrderBy->a[i].pExpr;
146646 pIdxOrderBy[i].iColumn = pExpr->iColumn;
146647 pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
146648 }
146649
146650 *pmNoOmit = mNoOmit;
146651 return pIdxInfo;
146652}
146653
146654/*
146655** The table object reference passed as the second argument to this function
146656** must represent a virtual table. This function invokes the xBestIndex()
146657** method of the virtual table with the sqlite3_index_info object that
146658** comes in as the 3rd argument to this function.
146659**
146660** If an error occurs, pParse is populated with an error message and an
146661** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
146662** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
146663** the current configuration of "unusable" flags in sqlite3_index_info can
146664** not result in a valid plan.
146665**
146666** Whether or not an error is returned, it is the responsibility of the
146667** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
146668** that this is required.
146669*/
146670static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
146671 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
146672 int rc;
146673
146674 whereTraceIndexInfoInputs(p);
146675 rc = pVtab->pModule->xBestIndex(pVtab, p);
146676 whereTraceIndexInfoOutputs(p);
146677
146678 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
146679 if( rc==SQLITE_NOMEM ){
146680 sqlite3OomFault(pParse->db);
146681 }else if( !pVtab->zErrMsg ){
146682 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
146683 }else{
146684 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
146685 }
146686 }
146687 sqlite3_free(pVtab->zErrMsg);
146688 pVtab->zErrMsg = 0;
146689 return rc;
146690}
146691#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
146692
146693#ifdef SQLITE_ENABLE_STAT4
146694/*
146695** Estimate the location of a particular key among all keys in an
146696** index. Store the results in aStat as follows:
146697**
146698** aStat[0] Est. number of rows less than pRec
146699** aStat[1] Est. number of rows equal to pRec
146700**
146701** Return the index of the sample that is the smallest sample that
146702** is greater than or equal to pRec. Note that this index is not an index
146703** into the aSample[] array - it is an index into a virtual set of samples
146704** based on the contents of aSample[] and the number of fields in record
146705** pRec.
146706*/
146707static int whereKeyStats(
146708 Parse *pParse, /* Database connection */
146709 Index *pIdx, /* Index to consider domain of */
146710 UnpackedRecord *pRec, /* Vector of values to consider */
146711 int roundUp, /* Round up if true. Round down if false */
146712 tRowcnt *aStat /* OUT: stats written here */
146713){
146714 IndexSample *aSample = pIdx->aSample;
146715 int iCol; /* Index of required stats in anEq[] etc. */
146716 int i; /* Index of first sample >= pRec */
146717 int iSample; /* Smallest sample larger than or equal to pRec */
146718 int iMin = 0; /* Smallest sample not yet tested */
146719 int iTest; /* Next sample to test */
146720 int res; /* Result of comparison operation */
146721 int nField; /* Number of fields in pRec */
146722 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
146723
146724#ifndef SQLITE_DEBUG
146725 UNUSED_PARAMETER( pParse );
146726#endif
146727 assert( pRec!=0 );
146728 assert( pIdx->nSample>0 );
146729 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
146730
146731 /* Do a binary search to find the first sample greater than or equal
146732 ** to pRec. If pRec contains a single field, the set of samples to search
146733 ** is simply the aSample[] array. If the samples in aSample[] contain more
146734 ** than one fields, all fields following the first are ignored.
146735 **
146736 ** If pRec contains N fields, where N is more than one, then as well as the
146737 ** samples in aSample[] (truncated to N fields), the search also has to
146738 ** consider prefixes of those samples. For example, if the set of samples
146739 ** in aSample is:
146740 **
146741 ** aSample[0] = (a, 5)
146742 ** aSample[1] = (a, 10)
146743 ** aSample[2] = (b, 5)
146744 ** aSample[3] = (c, 100)
146745 ** aSample[4] = (c, 105)
146746 **
146747 ** Then the search space should ideally be the samples above and the
146748 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
146749 ** the code actually searches this set:
146750 **
146751 ** 0: (a)
146752 ** 1: (a, 5)
146753 ** 2: (a, 10)
146754 ** 3: (a, 10)
146755 ** 4: (b)
146756 ** 5: (b, 5)
146757 ** 6: (c)
146758 ** 7: (c, 100)
146759 ** 8: (c, 105)
146760 ** 9: (c, 105)
146761 **
146762 ** For each sample in the aSample[] array, N samples are present in the
146763 ** effective sample array. In the above, samples 0 and 1 are based on
146764 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
146765 **
146766 ** Often, sample i of each block of N effective samples has (i+1) fields.
146767 ** Except, each sample may be extended to ensure that it is greater than or
146768 ** equal to the previous sample in the array. For example, in the above,
146769 ** sample 2 is the first sample of a block of N samples, so at first it
146770 ** appears that it should be 1 field in size. However, that would make it
146771 ** smaller than sample 1, so the binary search would not work. As a result,
146772 ** it is extended to two fields. The duplicates that this creates do not
146773 ** cause any problems.
146774 */
146775 nField = pRec->nField;
146776 iCol = 0;
146777 iSample = pIdx->nSample * nField;
146778 do{
146779 int iSamp; /* Index in aSample[] of test sample */
146780 int n; /* Number of fields in test sample */
146781
146782 iTest = (iMin+iSample)/2;
146783 iSamp = iTest / nField;
146784 if( iSamp>0 ){
146785 /* The proposed effective sample is a prefix of sample aSample[iSamp].
146786 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
146787 ** fields that is greater than the previous effective sample. */
146788 for(n=(iTest % nField) + 1; n<nField; n++){
146789 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
146790 }
146791 }else{
146792 n = iTest + 1;
146793 }
146794
146795 pRec->nField = n;
146796 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
146797 if( res<0 ){
146798 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
146799 iMin = iTest+1;
146800 }else if( res==0 && n<nField ){
146801 iLower = aSample[iSamp].anLt[n-1];
146802 iMin = iTest+1;
146803 res = -1;
146804 }else{
146805 iSample = iTest;
146806 iCol = n-1;
146807 }
146808 }while( res && iMin<iSample );
146809 i = iSample / nField;
146810
146811#ifdef SQLITE_DEBUG
146812 /* The following assert statements check that the binary search code
146813 ** above found the right answer. This block serves no purpose other
146814 ** than to invoke the asserts. */
146815 if( pParse->db->mallocFailed==0 ){
146816 if( res==0 ){
146817 /* If (res==0) is true, then pRec must be equal to sample i. */
146818 assert( i<pIdx->nSample );
146819 assert( iCol==nField-1 );
146820 pRec->nField = nField;
146821 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
146822 || pParse->db->mallocFailed
146823 );
146824 }else{
146825 /* Unless i==pIdx->nSample, indicating that pRec is larger than
146826 ** all samples in the aSample[] array, pRec must be smaller than the
146827 ** (iCol+1) field prefix of sample i. */
146828 assert( i<=pIdx->nSample && i>=0 );
146829 pRec->nField = iCol+1;
146830 assert( i==pIdx->nSample
146831 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
146832 || pParse->db->mallocFailed );
146833
146834 /* if i==0 and iCol==0, then record pRec is smaller than all samples
146835 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
146836 ** be greater than or equal to the (iCol) field prefix of sample i.
146837 ** If (i>0), then pRec must also be greater than sample (i-1). */
146838 if( iCol>0 ){
146839 pRec->nField = iCol;
146840 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
146841 || pParse->db->mallocFailed );
146842 }
146843 if( i>0 ){
146844 pRec->nField = nField;
146845 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
146846 || pParse->db->mallocFailed );
146847 }
146848 }
146849 }
146850#endif /* ifdef SQLITE_DEBUG */
146851
146852 if( res==0 ){
146853 /* Record pRec is equal to sample i */
146854 assert( iCol==nField-1 );
146855 aStat[0] = aSample[i].anLt[iCol];
146856 aStat[1] = aSample[i].anEq[iCol];
146857 }else{
146858 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
146859 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
146860 ** is larger than all samples in the array. */
146861 tRowcnt iUpper, iGap;
146862 if( i>=pIdx->nSample ){
146863 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
146864 }else{
146865 iUpper = aSample[i].anLt[iCol];
146866 }
146867
146868 if( iLower>=iUpper ){
146869 iGap = 0;
146870 }else{
146871 iGap = iUpper - iLower;
146872 }
146873 if( roundUp ){
146874 iGap = (iGap*2)/3;
146875 }else{
146876 iGap = iGap/3;
146877 }
146878 aStat[0] = iLower + iGap;
146879 aStat[1] = pIdx->aAvgEq[nField-1];
146880 }
146881
146882 /* Restore the pRec->nField value before returning. */
146883 pRec->nField = nField;
146884 return i;
146885}
146886#endif /* SQLITE_ENABLE_STAT4 */
146887
146888/*
146889** If it is not NULL, pTerm is a term that provides an upper or lower
146890** bound on a range scan. Without considering pTerm, it is estimated
146891** that the scan will visit nNew rows. This function returns the number
146892** estimated to be visited after taking pTerm into account.
146893**
146894** If the user explicitly specified a likelihood() value for this term,
146895** then the return value is the likelihood multiplied by the number of
146896** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
146897** has a likelihood of 0.50, and any other term a likelihood of 0.25.
146898*/
146899static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
146900 LogEst nRet = nNew;
146901 if( pTerm ){
146902 if( pTerm->truthProb<=0 ){
146903 nRet += pTerm->truthProb;
146904 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
146905 nRet -= 20; assert( 20==sqlite3LogEst(4) );
146906 }
146907 }
146908 return nRet;
146909}
146910
146911
146912#ifdef SQLITE_ENABLE_STAT4
146913/*
146914** Return the affinity for a single column of an index.
146915*/
146916SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
146917 assert( iCol>=0 && iCol<pIdx->nColumn );
146918 if( !pIdx->zColAff ){
146919 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
146920 }
146921 assert( pIdx->zColAff[iCol]!=0 );
146922 return pIdx->zColAff[iCol];
146923}
146924#endif
146925
146926
146927#ifdef SQLITE_ENABLE_STAT4
146928/*
146929** This function is called to estimate the number of rows visited by a
146930** range-scan on a skip-scan index. For example:
146931**
146932** CREATE INDEX i1 ON t1(a, b, c);
146933** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
146934**
146935** Value pLoop->nOut is currently set to the estimated number of rows
146936** visited for scanning (a=? AND b=?). This function reduces that estimate
146937** by some factor to account for the (c BETWEEN ? AND ?) expression based
146938** on the stat4 data for the index. this scan will be peformed multiple
146939** times (once for each (a,b) combination that matches a=?) is dealt with
146940** by the caller.
146941**
146942** It does this by scanning through all stat4 samples, comparing values
146943** extracted from pLower and pUpper with the corresponding column in each
146944** sample. If L and U are the number of samples found to be less than or
146945** equal to the values extracted from pLower and pUpper respectively, and
146946** N is the total number of samples, the pLoop->nOut value is adjusted
146947** as follows:
146948**
146949** nOut = nOut * ( min(U - L, 1) / N )
146950**
146951** If pLower is NULL, or a value cannot be extracted from the term, L is
146952** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
146953** U is set to N.
146954**
146955** Normally, this function sets *pbDone to 1 before returning. However,
146956** if no value can be extracted from either pLower or pUpper (and so the
146957** estimate of the number of rows delivered remains unchanged), *pbDone
146958** is left as is.
146959**
146960** If an error occurs, an SQLite error code is returned. Otherwise,
146961** SQLITE_OK.
146962*/
146963static int whereRangeSkipScanEst(
146964 Parse *pParse, /* Parsing & code generating context */
146965 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
146966 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
146967 WhereLoop *pLoop, /* Update the .nOut value of this loop */
146968 int *pbDone /* Set to true if at least one expr. value extracted */
146969){
146970 Index *p = pLoop->u.btree.pIndex;
146971 int nEq = pLoop->u.btree.nEq;
146972 sqlite3 *db = pParse->db;
146973 int nLower = -1;
146974 int nUpper = p->nSample+1;
146975 int rc = SQLITE_OK;
146976 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
146977 CollSeq *pColl;
146978
146979 sqlite3_value *p1 = 0; /* Value extracted from pLower */
146980 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
146981 sqlite3_value *pVal = 0; /* Value extracted from record */
146982
146983 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
146984 if( pLower ){
146985 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
146986 nLower = 0;
146987 }
146988 if( pUpper && rc==SQLITE_OK ){
146989 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
146990 nUpper = p2 ? 0 : p->nSample;
146991 }
146992
146993 if( p1 || p2 ){
146994 int i;
146995 int nDiff;
146996 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
146997 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
146998 if( rc==SQLITE_OK && p1 ){
146999 int res = sqlite3MemCompare(p1, pVal, pColl);
147000 if( res>=0 ) nLower++;
147001 }
147002 if( rc==SQLITE_OK && p2 ){
147003 int res = sqlite3MemCompare(p2, pVal, pColl);
147004 if( res>=0 ) nUpper++;
147005 }
147006 }
147007 nDiff = (nUpper - nLower);
147008 if( nDiff<=0 ) nDiff = 1;
147009
147010 /* If there is both an upper and lower bound specified, and the
147011 ** comparisons indicate that they are close together, use the fallback
147012 ** method (assume that the scan visits 1/64 of the rows) for estimating
147013 ** the number of rows visited. Otherwise, estimate the number of rows
147014 ** using the method described in the header comment for this function. */
147015 if( nDiff!=1 || pUpper==0 || pLower==0 ){
147016 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
147017 pLoop->nOut -= nAdjust;
147018 *pbDone = 1;
147019 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
147020 nLower, nUpper, nAdjust*-1, pLoop->nOut));
147021 }
147022
147023 }else{
147024 assert( *pbDone==0 );
147025 }
147026
147027 sqlite3ValueFree(p1);
147028 sqlite3ValueFree(p2);
147029 sqlite3ValueFree(pVal);
147030
147031 return rc;
147032}
147033#endif /* SQLITE_ENABLE_STAT4 */
147034
147035/*
147036** This function is used to estimate the number of rows that will be visited
147037** by scanning an index for a range of values. The range may have an upper
147038** bound, a lower bound, or both. The WHERE clause terms that set the upper
147039** and lower bounds are represented by pLower and pUpper respectively. For
147040** example, assuming that index p is on t1(a):
147041**
147042** ... FROM t1 WHERE a > ? AND a < ? ...
147043** |_____| |_____|
147044** | |
147045** pLower pUpper
147046**
147047** If either of the upper or lower bound is not present, then NULL is passed in
147048** place of the corresponding WhereTerm.
147049**
147050** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
147051** column subject to the range constraint. Or, equivalently, the number of
147052** equality constraints optimized by the proposed index scan. For example,
147053** assuming index p is on t1(a, b), and the SQL query is:
147054**
147055** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
147056**
147057** then nEq is set to 1 (as the range restricted column, b, is the second
147058** left-most column of the index). Or, if the query is:
147059**
147060** ... FROM t1 WHERE a > ? AND a < ? ...
147061**
147062** then nEq is set to 0.
147063**
147064** When this function is called, *pnOut is set to the sqlite3LogEst() of the
147065** number of rows that the index scan is expected to visit without
147066** considering the range constraints. If nEq is 0, then *pnOut is the number of
147067** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
147068** to account for the range constraints pLower and pUpper.
147069**
147070** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
147071** used, a single range inequality reduces the search space by a factor of 4.
147072** and a pair of constraints (x>? AND x<?) reduces the expected number of
147073** rows visited by a factor of 64.
147074*/
147075static int whereRangeScanEst(
147076 Parse *pParse, /* Parsing & code generating context */
147077 WhereLoopBuilder *pBuilder,
147078 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
147079 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
147080 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
147081){
147082 int rc = SQLITE_OK;
147083 int nOut = pLoop->nOut;
147084 LogEst nNew;
147085
147086#ifdef SQLITE_ENABLE_STAT4
147087 Index *p = pLoop->u.btree.pIndex;
147088 int nEq = pLoop->u.btree.nEq;
147089
147090 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
147091 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
147092 ){
147093 if( nEq==pBuilder->nRecValid ){
147094 UnpackedRecord *pRec = pBuilder->pRec;
147095 tRowcnt a[2];
147096 int nBtm = pLoop->u.btree.nBtm;
147097 int nTop = pLoop->u.btree.nTop;
147098
147099 /* Variable iLower will be set to the estimate of the number of rows in
147100 ** the index that are less than the lower bound of the range query. The
147101 ** lower bound being the concatenation of $P and $L, where $P is the
147102 ** key-prefix formed by the nEq values matched against the nEq left-most
147103 ** columns of the index, and $L is the value in pLower.
147104 **
147105 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
147106 ** is not a simple variable or literal value), the lower bound of the
147107 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
147108 ** if $L is available, whereKeyStats() is called for both ($P) and
147109 ** ($P:$L) and the larger of the two returned values is used.
147110 **
147111 ** Similarly, iUpper is to be set to the estimate of the number of rows
147112 ** less than the upper bound of the range query. Where the upper bound
147113 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
147114 ** of iUpper are requested of whereKeyStats() and the smaller used.
147115 **
147116 ** The number of rows between the two bounds is then just iUpper-iLower.
147117 */
147118 tRowcnt iLower; /* Rows less than the lower bound */
147119 tRowcnt iUpper; /* Rows less than the upper bound */
147120 int iLwrIdx = -2; /* aSample[] for the lower bound */
147121 int iUprIdx = -1; /* aSample[] for the upper bound */
147122
147123 if( pRec ){
147124 testcase( pRec->nField!=pBuilder->nRecValid );
147125 pRec->nField = pBuilder->nRecValid;
147126 }
147127 /* Determine iLower and iUpper using ($P) only. */
147128 if( nEq==0 ){
147129 iLower = 0;
147130 iUpper = p->nRowEst0;
147131 }else{
147132 /* Note: this call could be optimized away - since the same values must
147133 ** have been requested when testing key $P in whereEqualScanEst(). */
147134 whereKeyStats(pParse, p, pRec, 0, a);
147135 iLower = a[0];
147136 iUpper = a[0] + a[1];
147137 }
147138
147139 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
147140 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
147141 assert( p->aSortOrder!=0 );
147142 if( p->aSortOrder[nEq] ){
147143 /* The roles of pLower and pUpper are swapped for a DESC index */
147144 SWAP(WhereTerm*, pLower, pUpper);
147145 SWAP(int, nBtm, nTop);
147146 }
147147
147148 /* If possible, improve on the iLower estimate using ($P:$L). */
147149 if( pLower ){
147150 int n; /* Values extracted from pExpr */
147151 Expr *pExpr = pLower->pExpr->pRight;
147152 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
147153 if( rc==SQLITE_OK && n ){
147154 tRowcnt iNew;
147155 u16 mask = WO_GT|WO_LE;
147156 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
147157 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
147158 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
147159 if( iNew>iLower ) iLower = iNew;
147160 nOut--;
147161 pLower = 0;
147162 }
147163 }
147164
147165 /* If possible, improve on the iUpper estimate using ($P:$U). */
147166 if( pUpper ){
147167 int n; /* Values extracted from pExpr */
147168 Expr *pExpr = pUpper->pExpr->pRight;
147169 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
147170 if( rc==SQLITE_OK && n ){
147171 tRowcnt iNew;
147172 u16 mask = WO_GT|WO_LE;
147173 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
147174 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
147175 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
147176 if( iNew<iUpper ) iUpper = iNew;
147177 nOut--;
147178 pUpper = 0;
147179 }
147180 }
147181
147182 pBuilder->pRec = pRec;
147183 if( rc==SQLITE_OK ){
147184 if( iUpper>iLower ){
147185 nNew = sqlite3LogEst(iUpper - iLower);
147186 /* TUNING: If both iUpper and iLower are derived from the same
147187 ** sample, then assume they are 4x more selective. This brings
147188 ** the estimated selectivity more in line with what it would be
147189 ** if estimated without the use of STAT4 tables. */
147190 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
147191 }else{
147192 nNew = 10; assert( 10==sqlite3LogEst(2) );
147193 }
147194 if( nNew<nOut ){
147195 nOut = nNew;
147196 }
147197 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
147198 (u32)iLower, (u32)iUpper, nOut));
147199 }
147200 }else{
147201 int bDone = 0;
147202 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
147203 if( bDone ) return rc;
147204 }
147205 }
147206#else
147207 UNUSED_PARAMETER(pParse);
147208 UNUSED_PARAMETER(pBuilder);
147209 assert( pLower || pUpper );
147210#endif
147211 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
147212 nNew = whereRangeAdjust(pLower, nOut);
147213 nNew = whereRangeAdjust(pUpper, nNew);
147214
147215 /* TUNING: If there is both an upper and lower limit and neither limit
147216 ** has an application-defined likelihood(), assume the range is
147217 ** reduced by an additional 75%. This means that, by default, an open-ended
147218 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
147219 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
147220 ** match 1/64 of the index. */
147221 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
147222 nNew -= 20;
147223 }
147224
147225 nOut -= (pLower!=0) + (pUpper!=0);
147226 if( nNew<10 ) nNew = 10;
147227 if( nNew<nOut ) nOut = nNew;
147228#if defined(WHERETRACE_ENABLED)
147229 if( pLoop->nOut>nOut ){
147230 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
147231 pLoop->nOut, nOut));
147232 }
147233#endif
147234 pLoop->nOut = (LogEst)nOut;
147235 return rc;
147236}
147237
147238#ifdef SQLITE_ENABLE_STAT4
147239/*
147240** Estimate the number of rows that will be returned based on
147241** an equality constraint x=VALUE and where that VALUE occurs in
147242** the histogram data. This only works when x is the left-most
147243** column of an index and sqlite_stat4 histogram data is available
147244** for that index. When pExpr==NULL that means the constraint is
147245** "x IS NULL" instead of "x=VALUE".
147246**
147247** Write the estimated row count into *pnRow and return SQLITE_OK.
147248** If unable to make an estimate, leave *pnRow unchanged and return
147249** non-zero.
147250**
147251** This routine can fail if it is unable to load a collating sequence
147252** required for string comparison, or if unable to allocate memory
147253** for a UTF conversion required for comparison. The error is stored
147254** in the pParse structure.
147255*/
147256static int whereEqualScanEst(
147257 Parse *pParse, /* Parsing & code generating context */
147258 WhereLoopBuilder *pBuilder,
147259 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
147260 tRowcnt *pnRow /* Write the revised row estimate here */
147261){
147262 Index *p = pBuilder->pNew->u.btree.pIndex;
147263 int nEq = pBuilder->pNew->u.btree.nEq;
147264 UnpackedRecord *pRec = pBuilder->pRec;
147265 int rc; /* Subfunction return code */
147266 tRowcnt a[2]; /* Statistics */
147267 int bOk;
147268
147269 assert( nEq>=1 );
147270 assert( nEq<=p->nColumn );
147271 assert( p->aSample!=0 );
147272 assert( p->nSample>0 );
147273 assert( pBuilder->nRecValid<nEq );
147274
147275 /* If values are not available for all fields of the index to the left
147276 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
147277 if( pBuilder->nRecValid<(nEq-1) ){
147278 return SQLITE_NOTFOUND;
147279 }
147280
147281 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
147282 ** below would return the same value. */
147283 if( nEq>=p->nColumn ){
147284 *pnRow = 1;
147285 return SQLITE_OK;
147286 }
147287
147288 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
147289 pBuilder->pRec = pRec;
147290 if( rc!=SQLITE_OK ) return rc;
147291 if( bOk==0 ) return SQLITE_NOTFOUND;
147292 pBuilder->nRecValid = nEq;
147293
147294 whereKeyStats(pParse, p, pRec, 0, a);
147295 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
147296 p->zName, nEq-1, (int)a[1]));
147297 *pnRow = a[1];
147298
147299 return rc;
147300}
147301#endif /* SQLITE_ENABLE_STAT4 */
147302
147303#ifdef SQLITE_ENABLE_STAT4
147304/*
147305** Estimate the number of rows that will be returned based on
147306** an IN constraint where the right-hand side of the IN operator
147307** is a list of values. Example:
147308**
147309** WHERE x IN (1,2,3,4)
147310**
147311** Write the estimated row count into *pnRow and return SQLITE_OK.
147312** If unable to make an estimate, leave *pnRow unchanged and return
147313** non-zero.
147314**
147315** This routine can fail if it is unable to load a collating sequence
147316** required for string comparison, or if unable to allocate memory
147317** for a UTF conversion required for comparison. The error is stored
147318** in the pParse structure.
147319*/
147320static int whereInScanEst(
147321 Parse *pParse, /* Parsing & code generating context */
147322 WhereLoopBuilder *pBuilder,
147323 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
147324 tRowcnt *pnRow /* Write the revised row estimate here */
147325){
147326 Index *p = pBuilder->pNew->u.btree.pIndex;
147327 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
147328 int nRecValid = pBuilder->nRecValid;
147329 int rc = SQLITE_OK; /* Subfunction return code */
147330 tRowcnt nEst; /* Number of rows for a single term */
147331 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
147332 int i; /* Loop counter */
147333
147334 assert( p->aSample!=0 );
147335 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
147336 nEst = nRow0;
147337 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
147338 nRowEst += nEst;
147339 pBuilder->nRecValid = nRecValid;
147340 }
147341
147342 if( rc==SQLITE_OK ){
147343 if( nRowEst > nRow0 ) nRowEst = nRow0;
147344 *pnRow = nRowEst;
147345 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
147346 }
147347 assert( pBuilder->nRecValid==nRecValid );
147348 return rc;
147349}
147350#endif /* SQLITE_ENABLE_STAT4 */
147351
147352
147353#ifdef WHERETRACE_ENABLED
147354/*
147355** Print the content of a WhereTerm object
147356*/
147357SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
147358 if( pTerm==0 ){
147359 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
147360 }else{
147361 char zType[8];
147362 char zLeft[50];
147363 memcpy(zType, "....", 5);
147364 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
147365 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
147366 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
147367 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
147368 if( pTerm->eOperator & WO_SINGLE ){
147369 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
147370 pTerm->leftCursor, pTerm->u.leftColumn);
147371 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
147372 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld",
147373 pTerm->u.pOrInfo->indexable);
147374 }else{
147375 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
147376 }
147377 sqlite3DebugPrintf(
147378 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
147379 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
147380 /* The 0x10000 .wheretrace flag causes extra information to be
147381 ** shown about each Term */
147382 if( sqlite3WhereTrace & 0x10000 ){
147383 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
147384 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
147385 }
147386 if( pTerm->iField ){
147387 sqlite3DebugPrintf(" iField=%d", pTerm->iField);
147388 }
147389 if( pTerm->iParent>=0 ){
147390 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
147391 }
147392 sqlite3DebugPrintf("\n");
147393 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
147394 }
147395}
147396#endif
147397
147398#ifdef WHERETRACE_ENABLED
147399/*
147400** Show the complete content of a WhereClause
147401*/
147402SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
147403 int i;
147404 for(i=0; i<pWC->nTerm; i++){
147405 sqlite3WhereTermPrint(&pWC->a[i], i);
147406 }
147407}
147408#endif
147409
147410#ifdef WHERETRACE_ENABLED
147411/*
147412** Print a WhereLoop object for debugging purposes
147413*/
147414SQLITE_PRIVATE void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC){
147415 WhereInfo *pWInfo = pWC->pWInfo;
147416 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
147417 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
147418 Table *pTab = pItem->pTab;
147419 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
147420 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
147421 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
147422 sqlite3DebugPrintf(" %12s",
147423 pItem->zAlias ? pItem->zAlias : pTab->zName);
147424 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
147425 const char *zName;
147426 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
147427 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
147428 int i = sqlite3Strlen30(zName) - 1;
147429 while( zName[i]!='_' ) i--;
147430 zName += i;
147431 }
147432 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
147433 }else{
147434 sqlite3DebugPrintf("%20s","");
147435 }
147436 }else{
147437 char *z;
147438 if( p->u.vtab.idxStr ){
147439 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
147440 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
147441 }else{
147442 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
147443 }
147444 sqlite3DebugPrintf(" %-19s", z);
147445 sqlite3_free(z);
147446 }
147447 if( p->wsFlags & WHERE_SKIPSCAN ){
147448 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
147449 }else{
147450 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
147451 }
147452 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
147453 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
147454 int i;
147455 for(i=0; i<p->nLTerm; i++){
147456 sqlite3WhereTermPrint(p->aLTerm[i], i);
147457 }
147458 }
147459}
147460#endif
147461
147462/*
147463** Convert bulk memory into a valid WhereLoop that can be passed
147464** to whereLoopClear harmlessly.
147465*/
147466static void whereLoopInit(WhereLoop *p){
147467 p->aLTerm = p->aLTermSpace;
147468 p->nLTerm = 0;
147469 p->nLSlot = ArraySize(p->aLTermSpace);
147470 p->wsFlags = 0;
147471}
147472
147473/*
147474** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
147475*/
147476static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
147477 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
147478 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
147479 sqlite3_free(p->u.vtab.idxStr);
147480 p->u.vtab.needFree = 0;
147481 p->u.vtab.idxStr = 0;
147482 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
147483 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
147484 sqlite3DbFreeNN(db, p->u.btree.pIndex);
147485 p->u.btree.pIndex = 0;
147486 }
147487 }
147488}
147489
147490/*
147491** Deallocate internal memory used by a WhereLoop object
147492*/
147493static void whereLoopClear(sqlite3 *db, WhereLoop *p){
147494 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
147495 whereLoopClearUnion(db, p);
147496 whereLoopInit(p);
147497}
147498
147499/*
147500** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
147501*/
147502static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
147503 WhereTerm **paNew;
147504 if( p->nLSlot>=n ) return SQLITE_OK;
147505 n = (n+7)&~7;
147506 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
147507 if( paNew==0 ) return SQLITE_NOMEM_BKPT;
147508 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
147509 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
147510 p->aLTerm = paNew;
147511 p->nLSlot = n;
147512 return SQLITE_OK;
147513}
147514
147515/*
147516** Transfer content from the second pLoop into the first.
147517*/
147518static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
147519 whereLoopClearUnion(db, pTo);
147520 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
147521 memset(&pTo->u, 0, sizeof(pTo->u));
147522 return SQLITE_NOMEM_BKPT;
147523 }
147524 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
147525 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
147526 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
147527 pFrom->u.vtab.needFree = 0;
147528 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
147529 pFrom->u.btree.pIndex = 0;
147530 }
147531 return SQLITE_OK;
147532}
147533
147534/*
147535** Delete a WhereLoop object
147536*/
147537static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
147538 whereLoopClear(db, p);
147539 sqlite3DbFreeNN(db, p);
147540}
147541
147542/*
147543** Free a WhereInfo structure
147544*/
147545static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
147546 int i;
147547 assert( pWInfo!=0 );
147548 for(i=0; i<pWInfo->nLevel; i++){
147549 WhereLevel *pLevel = &pWInfo->a[i];
147550 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
147551 sqlite3DbFree(db, pLevel->u.in.aInLoop);
147552 }
147553 }
147554 sqlite3WhereClauseClear(&pWInfo->sWC);
147555 while( pWInfo->pLoops ){
147556 WhereLoop *p = pWInfo->pLoops;
147557 pWInfo->pLoops = p->pNextLoop;
147558 whereLoopDelete(db, p);
147559 }
147560 assert( pWInfo->pExprMods==0 );
147561 sqlite3DbFreeNN(db, pWInfo);
147562}
147563
147564/*
147565** Return TRUE if all of the following are true:
147566**
147567** (1) X has the same or lower cost that Y
147568** (2) X uses fewer WHERE clause terms than Y
147569** (3) Every WHERE clause term used by X is also used by Y
147570** (4) X skips at least as many columns as Y
147571** (5) If X is a covering index, than Y is too
147572**
147573** Conditions (2) and (3) mean that X is a "proper subset" of Y.
147574** If X is a proper subset of Y then Y is a better choice and ought
147575** to have a lower cost. This routine returns TRUE when that cost
147576** relationship is inverted and needs to be adjusted. Constraint (4)
147577** was added because if X uses skip-scan less than Y it still might
147578** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
147579** was added because a covering index probably deserves to have a lower cost
147580** than a non-covering index even if it is a proper subset.
147581*/
147582static int whereLoopCheaperProperSubset(
147583 const WhereLoop *pX, /* First WhereLoop to compare */
147584 const WhereLoop *pY /* Compare against this WhereLoop */
147585){
147586 int i, j;
147587 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
147588 return 0; /* X is not a subset of Y */
147589 }
147590 if( pY->nSkip > pX->nSkip ) return 0;
147591 if( pX->rRun >= pY->rRun ){
147592 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
147593 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
147594 }
147595 for(i=pX->nLTerm-1; i>=0; i--){
147596 if( pX->aLTerm[i]==0 ) continue;
147597 for(j=pY->nLTerm-1; j>=0; j--){
147598 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
147599 }
147600 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
147601 }
147602 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
147603 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
147604 return 0; /* Constraint (5) */
147605 }
147606 return 1; /* All conditions meet */
147607}
147608
147609/*
147610** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
147611** that:
147612**
147613** (1) pTemplate costs less than any other WhereLoops that are a proper
147614** subset of pTemplate
147615**
147616** (2) pTemplate costs more than any other WhereLoops for which pTemplate
147617** is a proper subset.
147618**
147619** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
147620** WHERE clause terms than Y and that every WHERE clause term used by X is
147621** also used by Y.
147622*/
147623static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
147624 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
147625 for(; p; p=p->pNextLoop){
147626 if( p->iTab!=pTemplate->iTab ) continue;
147627 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
147628 if( whereLoopCheaperProperSubset(p, pTemplate) ){
147629 /* Adjust pTemplate cost downward so that it is cheaper than its
147630 ** subset p. */
147631 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
147632 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
147633 pTemplate->rRun = p->rRun;
147634 pTemplate->nOut = p->nOut - 1;
147635 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
147636 /* Adjust pTemplate cost upward so that it is costlier than p since
147637 ** pTemplate is a proper subset of p */
147638 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
147639 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
147640 pTemplate->rRun = p->rRun;
147641 pTemplate->nOut = p->nOut + 1;
147642 }
147643 }
147644}
147645
147646/*
147647** Search the list of WhereLoops in *ppPrev looking for one that can be
147648** replaced by pTemplate.
147649**
147650** Return NULL if pTemplate does not belong on the WhereLoop list.
147651** In other words if pTemplate ought to be dropped from further consideration.
147652**
147653** If pX is a WhereLoop that pTemplate can replace, then return the
147654** link that points to pX.
147655**
147656** If pTemplate cannot replace any existing element of the list but needs
147657** to be added to the list as a new entry, then return a pointer to the
147658** tail of the list.
147659*/
147660static WhereLoop **whereLoopFindLesser(
147661 WhereLoop **ppPrev,
147662 const WhereLoop *pTemplate
147663){
147664 WhereLoop *p;
147665 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
147666 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
147667 /* If either the iTab or iSortIdx values for two WhereLoop are different
147668 ** then those WhereLoops need to be considered separately. Neither is
147669 ** a candidate to replace the other. */
147670 continue;
147671 }
147672 /* In the current implementation, the rSetup value is either zero
147673 ** or the cost of building an automatic index (NlogN) and the NlogN
147674 ** is the same for compatible WhereLoops. */
147675 assert( p->rSetup==0 || pTemplate->rSetup==0
147676 || p->rSetup==pTemplate->rSetup );
147677
147678 /* whereLoopAddBtree() always generates and inserts the automatic index
147679 ** case first. Hence compatible candidate WhereLoops never have a larger
147680 ** rSetup. Call this SETUP-INVARIANT */
147681 assert( p->rSetup>=pTemplate->rSetup );
147682
147683 /* Any loop using an appliation-defined index (or PRIMARY KEY or
147684 ** UNIQUE constraint) with one or more == constraints is better
147685 ** than an automatic index. Unless it is a skip-scan. */
147686 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
147687 && (pTemplate->nSkip)==0
147688 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
147689 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
147690 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
147691 ){
147692 break;
147693 }
147694
147695 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
147696 ** discarded. WhereLoop p is better if:
147697 ** (1) p has no more dependencies than pTemplate, and
147698 ** (2) p has an equal or lower cost than pTemplate
147699 */
147700 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
147701 && p->rSetup<=pTemplate->rSetup /* (2a) */
147702 && p->rRun<=pTemplate->rRun /* (2b) */
147703 && p->nOut<=pTemplate->nOut /* (2c) */
147704 ){
147705 return 0; /* Discard pTemplate */
147706 }
147707
147708 /* If pTemplate is always better than p, then cause p to be overwritten
147709 ** with pTemplate. pTemplate is better than p if:
147710 ** (1) pTemplate has no more dependences than p, and
147711 ** (2) pTemplate has an equal or lower cost than p.
147712 */
147713 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
147714 && p->rRun>=pTemplate->rRun /* (2a) */
147715 && p->nOut>=pTemplate->nOut /* (2b) */
147716 ){
147717 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
147718 break; /* Cause p to be overwritten by pTemplate */
147719 }
147720 }
147721 return ppPrev;
147722}
147723
147724/*
147725** Insert or replace a WhereLoop entry using the template supplied.
147726**
147727** An existing WhereLoop entry might be overwritten if the new template
147728** is better and has fewer dependencies. Or the template will be ignored
147729** and no insert will occur if an existing WhereLoop is faster and has
147730** fewer dependencies than the template. Otherwise a new WhereLoop is
147731** added based on the template.
147732**
147733** If pBuilder->pOrSet is not NULL then we care about only the
147734** prerequisites and rRun and nOut costs of the N best loops. That
147735** information is gathered in the pBuilder->pOrSet object. This special
147736** processing mode is used only for OR clause processing.
147737**
147738** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
147739** still might overwrite similar loops with the new template if the
147740** new template is better. Loops may be overwritten if the following
147741** conditions are met:
147742**
147743** (1) They have the same iTab.
147744** (2) They have the same iSortIdx.
147745** (3) The template has same or fewer dependencies than the current loop
147746** (4) The template has the same or lower cost than the current loop
147747*/
147748static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
147749 WhereLoop **ppPrev, *p;
147750 WhereInfo *pWInfo = pBuilder->pWInfo;
147751 sqlite3 *db = pWInfo->pParse->db;
147752 int rc;
147753
147754 /* Stop the search once we hit the query planner search limit */
147755 if( pBuilder->iPlanLimit==0 ){
147756 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
147757 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
147758 return SQLITE_DONE;
147759 }
147760 pBuilder->iPlanLimit--;
147761
147762 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
147763
147764 /* If pBuilder->pOrSet is defined, then only keep track of the costs
147765 ** and prereqs.
147766 */
147767 if( pBuilder->pOrSet!=0 ){
147768 if( pTemplate->nLTerm ){
147769#if WHERETRACE_ENABLED
147770 u16 n = pBuilder->pOrSet->n;
147771 int x =
147772#endif
147773 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
147774 pTemplate->nOut);
147775#if WHERETRACE_ENABLED /* 0x8 */
147776 if( sqlite3WhereTrace & 0x8 ){
147777 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
147778 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147779 }
147780#endif
147781 }
147782 return SQLITE_OK;
147783 }
147784
147785 /* Look for an existing WhereLoop to replace with pTemplate
147786 */
147787 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
147788
147789 if( ppPrev==0 ){
147790 /* There already exists a WhereLoop on the list that is better
147791 ** than pTemplate, so just ignore pTemplate */
147792#if WHERETRACE_ENABLED /* 0x8 */
147793 if( sqlite3WhereTrace & 0x8 ){
147794 sqlite3DebugPrintf(" skip: ");
147795 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147796 }
147797#endif
147798 return SQLITE_OK;
147799 }else{
147800 p = *ppPrev;
147801 }
147802
147803 /* If we reach this point it means that either p[] should be overwritten
147804 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
147805 ** WhereLoop and insert it.
147806 */
147807#if WHERETRACE_ENABLED /* 0x8 */
147808 if( sqlite3WhereTrace & 0x8 ){
147809 if( p!=0 ){
147810 sqlite3DebugPrintf("replace: ");
147811 sqlite3WhereLoopPrint(p, pBuilder->pWC);
147812 sqlite3DebugPrintf(" with: ");
147813 }else{
147814 sqlite3DebugPrintf(" add: ");
147815 }
147816 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
147817 }
147818#endif
147819 if( p==0 ){
147820 /* Allocate a new WhereLoop to add to the end of the list */
147821 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
147822 if( p==0 ) return SQLITE_NOMEM_BKPT;
147823 whereLoopInit(p);
147824 p->pNextLoop = 0;
147825 }else{
147826 /* We will be overwriting WhereLoop p[]. But before we do, first
147827 ** go through the rest of the list and delete any other entries besides
147828 ** p[] that are also supplated by pTemplate */
147829 WhereLoop **ppTail = &p->pNextLoop;
147830 WhereLoop *pToDel;
147831 while( *ppTail ){
147832 ppTail = whereLoopFindLesser(ppTail, pTemplate);
147833 if( ppTail==0 ) break;
147834 pToDel = *ppTail;
147835 if( pToDel==0 ) break;
147836 *ppTail = pToDel->pNextLoop;
147837#if WHERETRACE_ENABLED /* 0x8 */
147838 if( sqlite3WhereTrace & 0x8 ){
147839 sqlite3DebugPrintf(" delete: ");
147840 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
147841 }
147842#endif
147843 whereLoopDelete(db, pToDel);
147844 }
147845 }
147846 rc = whereLoopXfer(db, p, pTemplate);
147847 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
147848 Index *pIndex = p->u.btree.pIndex;
147849 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
147850 p->u.btree.pIndex = 0;
147851 }
147852 }
147853 return rc;
147854}
147855
147856/*
147857** Adjust the WhereLoop.nOut value downward to account for terms of the
147858** WHERE clause that reference the loop but which are not used by an
147859** index.
147860*
147861** For every WHERE clause term that is not used by the index
147862** and which has a truth probability assigned by one of the likelihood(),
147863** likely(), or unlikely() SQL functions, reduce the estimated number
147864** of output rows by the probability specified.
147865**
147866** TUNING: For every WHERE clause term that is not used by the index
147867** and which does not have an assigned truth probability, heuristics
147868** described below are used to try to estimate the truth probability.
147869** TODO --> Perhaps this is something that could be improved by better
147870** table statistics.
147871**
147872** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
147873** value corresponds to -1 in LogEst notation, so this means decrement
147874** the WhereLoop.nOut field for every such WHERE clause term.
147875**
147876** Heuristic 2: If there exists one or more WHERE clause terms of the
147877** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
147878** final output row estimate is no greater than 1/4 of the total number
147879** of rows in the table. In other words, assume that x==EXPR will filter
147880** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
147881** "x" column is boolean or else -1 or 0 or 1 is a common default value
147882** on the "x" column and so in that case only cap the output row estimate
147883** at 1/2 instead of 1/4.
147884*/
147885static void whereLoopOutputAdjust(
147886 WhereClause *pWC, /* The WHERE clause */
147887 WhereLoop *pLoop, /* The loop to adjust downward */
147888 LogEst nRow /* Number of rows in the entire table */
147889){
147890 WhereTerm *pTerm, *pX;
147891 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
147892 int i, j;
147893 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
147894
147895 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
147896 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
147897 assert( pTerm!=0 );
147898 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
147899 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
147900 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
147901 for(j=pLoop->nLTerm-1; j>=0; j--){
147902 pX = pLoop->aLTerm[j];
147903 if( pX==0 ) continue;
147904 if( pX==pTerm ) break;
147905 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
147906 }
147907 if( j<0 ){
147908 if( pTerm->truthProb<=0 ){
147909 /* If a truth probability is specified using the likelihood() hints,
147910 ** then use the probability provided by the application. */
147911 pLoop->nOut += pTerm->truthProb;
147912 }else{
147913 /* In the absence of explicit truth probabilities, use heuristics to
147914 ** guess a reasonable truth probability. */
147915 pLoop->nOut--;
147916 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
147917 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
147918 ){
147919 Expr *pRight = pTerm->pExpr->pRight;
147920 int k = 0;
147921 testcase( pTerm->pExpr->op==TK_IS );
147922 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
147923 k = 10;
147924 }else{
147925 k = 20;
147926 }
147927 if( iReduce<k ){
147928 pTerm->wtFlags |= TERM_HEURTRUTH;
147929 iReduce = k;
147930 }
147931 }
147932 }
147933 }
147934 }
147935 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
147936}
147937
147938/*
147939** Term pTerm is a vector range comparison operation. The first comparison
147940** in the vector can be optimized using column nEq of the index. This
147941** function returns the total number of vector elements that can be used
147942** as part of the range comparison.
147943**
147944** For example, if the query is:
147945**
147946** WHERE a = ? AND (b, c, d) > (?, ?, ?)
147947**
147948** and the index:
147949**
147950** CREATE INDEX ... ON (a, b, c, d, e)
147951**
147952** then this function would be invoked with nEq=1. The value returned in
147953** this case is 3.
147954*/
147955static int whereRangeVectorLen(
147956 Parse *pParse, /* Parsing context */
147957 int iCur, /* Cursor open on pIdx */
147958 Index *pIdx, /* The index to be used for a inequality constraint */
147959 int nEq, /* Number of prior equality constraints on same index */
147960 WhereTerm *pTerm /* The vector inequality constraint */
147961){
147962 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
147963 int i;
147964
147965 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
147966 for(i=1; i<nCmp; i++){
147967 /* Test if comparison i of pTerm is compatible with column (i+nEq)
147968 ** of the index. If not, exit the loop. */
147969 char aff; /* Comparison affinity */
147970 char idxaff = 0; /* Indexed columns affinity */
147971 CollSeq *pColl; /* Comparison collation sequence */
147972 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
147973 Expr *pRhs = pTerm->pExpr->pRight;
147974 if( pRhs->flags & EP_xIsSelect ){
147975 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
147976 }else{
147977 pRhs = pRhs->x.pList->a[i].pExpr;
147978 }
147979
147980 /* Check that the LHS of the comparison is a column reference to
147981 ** the right column of the right source table. And that the sort
147982 ** order of the index column is the same as the sort order of the
147983 ** leftmost index column. */
147984 if( pLhs->op!=TK_COLUMN
147985 || pLhs->iTable!=iCur
147986 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
147987 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
147988 ){
147989 break;
147990 }
147991
147992 testcase( pLhs->iColumn==XN_ROWID );
147993 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
147994 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
147995 if( aff!=idxaff ) break;
147996
147997 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
147998 if( pColl==0 ) break;
147999 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
148000 }
148001 return i;
148002}
148003
148004/*
148005** Adjust the cost C by the costMult facter T. This only occurs if
148006** compiled with -DSQLITE_ENABLE_COSTMULT
148007*/
148008#ifdef SQLITE_ENABLE_COSTMULT
148009# define ApplyCostMultiplier(C,T) C += T
148010#else
148011# define ApplyCostMultiplier(C,T)
148012#endif
148013
148014/*
148015** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
148016** index pIndex. Try to match one more.
148017**
148018** When this function is called, pBuilder->pNew->nOut contains the
148019** number of rows expected to be visited by filtering using the nEq
148020** terms only. If it is modified, this value is restored before this
148021** function returns.
148022**
148023** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
148024** a fake index used for the INTEGER PRIMARY KEY.
148025*/
148026static int whereLoopAddBtreeIndex(
148027 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
148028 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
148029 Index *pProbe, /* An index on pSrc */
148030 LogEst nInMul /* log(Number of iterations due to IN) */
148031){
148032 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
148033 Parse *pParse = pWInfo->pParse; /* Parsing context */
148034 sqlite3 *db = pParse->db; /* Database connection malloc context */
148035 WhereLoop *pNew; /* Template WhereLoop under construction */
148036 WhereTerm *pTerm; /* A WhereTerm under consideration */
148037 int opMask; /* Valid operators for constraints */
148038 WhereScan scan; /* Iterator for WHERE terms */
148039 Bitmask saved_prereq; /* Original value of pNew->prereq */
148040 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
148041 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
148042 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
148043 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
148044 u16 saved_nSkip; /* Original value of pNew->nSkip */
148045 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
148046 LogEst saved_nOut; /* Original value of pNew->nOut */
148047 int rc = SQLITE_OK; /* Return code */
148048 LogEst rSize; /* Number of rows in the table */
148049 LogEst rLogSize; /* Logarithm of table size */
148050 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
148051
148052 pNew = pBuilder->pNew;
148053 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
148054 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d\n",
148055 pProbe->pTable->zName,pProbe->zName,
148056 pNew->u.btree.nEq, pNew->nSkip));
148057
148058 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
148059 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
148060 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
148061 opMask = WO_LT|WO_LE;
148062 }else{
148063 assert( pNew->u.btree.nBtm==0 );
148064 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
148065 }
148066 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
148067
148068 assert( pNew->u.btree.nEq<pProbe->nColumn );
148069
148070 saved_nEq = pNew->u.btree.nEq;
148071 saved_nBtm = pNew->u.btree.nBtm;
148072 saved_nTop = pNew->u.btree.nTop;
148073 saved_nSkip = pNew->nSkip;
148074 saved_nLTerm = pNew->nLTerm;
148075 saved_wsFlags = pNew->wsFlags;
148076 saved_prereq = pNew->prereq;
148077 saved_nOut = pNew->nOut;
148078 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
148079 opMask, pProbe);
148080 pNew->rSetup = 0;
148081 rSize = pProbe->aiRowLogEst[0];
148082 rLogSize = estLog(rSize);
148083 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
148084 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
148085 LogEst rCostIdx;
148086 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
148087 int nIn = 0;
148088#ifdef SQLITE_ENABLE_STAT4
148089 int nRecValid = pBuilder->nRecValid;
148090#endif
148091 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
148092 && indexColumnNotNull(pProbe, saved_nEq)
148093 ){
148094 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
148095 }
148096 if( pTerm->prereqRight & pNew->maskSelf ) continue;
148097
148098 /* Do not allow the upper bound of a LIKE optimization range constraint
148099 ** to mix with a lower range bound from some other source */
148100 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
148101
148102 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
148103 ** be used by the right table of a LEFT JOIN. Only constraints in the
148104 ** ON clause are allowed. See tag-20191211-002 for the vtab equivalent. */
148105 if( (pSrc->fg.jointype & JT_LEFT)!=0
148106 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
148107 ){
148108 continue;
148109 }
148110
148111 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
148112 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
148113 }else{
148114 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
148115 }
148116 pNew->wsFlags = saved_wsFlags;
148117 pNew->u.btree.nEq = saved_nEq;
148118 pNew->u.btree.nBtm = saved_nBtm;
148119 pNew->u.btree.nTop = saved_nTop;
148120 pNew->nLTerm = saved_nLTerm;
148121 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
148122 pNew->aLTerm[pNew->nLTerm++] = pTerm;
148123 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
148124
148125 assert( nInMul==0
148126 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
148127 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
148128 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
148129 );
148130
148131 if( eOp & WO_IN ){
148132 Expr *pExpr = pTerm->pExpr;
148133 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
148134 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
148135 int i;
148136 nIn = 46; assert( 46==sqlite3LogEst(25) );
148137
148138 /* The expression may actually be of the form (x, y) IN (SELECT...).
148139 ** In this case there is a separate term for each of (x) and (y).
148140 ** However, the nIn multiplier should only be applied once, not once
148141 ** for each such term. The following loop checks that pTerm is the
148142 ** first such term in use, and sets nIn back to 0 if it is not. */
148143 for(i=0; i<pNew->nLTerm-1; i++){
148144 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
148145 }
148146 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
148147 /* "x IN (value, value, ...)" */
148148 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
148149 }
148150 if( pProbe->hasStat1 ){
148151 LogEst M, logK, safetyMargin;
148152 /* Let:
148153 ** N = the total number of rows in the table
148154 ** K = the number of entries on the RHS of the IN operator
148155 ** M = the number of rows in the table that match terms to the
148156 ** to the left in the same index. If the IN operator is on
148157 ** the left-most index column, M==N.
148158 **
148159 ** Given the definitions above, it is better to omit the IN operator
148160 ** from the index lookup and instead do a scan of the M elements,
148161 ** testing each scanned row against the IN operator separately, if:
148162 **
148163 ** M*log(K) < K*log(N)
148164 **
148165 ** Our estimates for M, K, and N might be inaccurate, so we build in
148166 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
148167 ** with the index, as using an index has better worst-case behavior.
148168 ** If we do not have real sqlite_stat1 data, always prefer to use
148169 ** the index.
148170 */
148171 M = pProbe->aiRowLogEst[saved_nEq];
148172 logK = estLog(nIn);
148173 safetyMargin = 10; /* TUNING: extra weight for indexed IN */
148174 if( M + logK + safetyMargin < nIn + rLogSize ){
148175 WHERETRACE(0x40,
148176 ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
148177 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
148178 continue;
148179 }else{
148180 WHERETRACE(0x40,
148181 ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
148182 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
148183 }
148184 }
148185 pNew->wsFlags |= WHERE_COLUMN_IN;
148186 }else if( eOp & (WO_EQ|WO_IS) ){
148187 int iCol = pProbe->aiColumn[saved_nEq];
148188 pNew->wsFlags |= WHERE_COLUMN_EQ;
148189 assert( saved_nEq==pNew->u.btree.nEq );
148190 if( iCol==XN_ROWID
148191 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
148192 ){
148193 if( iCol==XN_ROWID || pProbe->uniqNotNull
148194 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
148195 ){
148196 pNew->wsFlags |= WHERE_ONEROW;
148197 }else{
148198 pNew->wsFlags |= WHERE_UNQ_WANTED;
148199 }
148200 }
148201 }else if( eOp & WO_ISNULL ){
148202 pNew->wsFlags |= WHERE_COLUMN_NULL;
148203 }else if( eOp & (WO_GT|WO_GE) ){
148204 testcase( eOp & WO_GT );
148205 testcase( eOp & WO_GE );
148206 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
148207 pNew->u.btree.nBtm = whereRangeVectorLen(
148208 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
148209 );
148210 pBtm = pTerm;
148211 pTop = 0;
148212 if( pTerm->wtFlags & TERM_LIKEOPT ){
148213 /* Range contraints that come from the LIKE optimization are
148214 ** always used in pairs. */
148215 pTop = &pTerm[1];
148216 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
148217 assert( pTop->wtFlags & TERM_LIKEOPT );
148218 assert( pTop->eOperator==WO_LT );
148219 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
148220 pNew->aLTerm[pNew->nLTerm++] = pTop;
148221 pNew->wsFlags |= WHERE_TOP_LIMIT;
148222 pNew->u.btree.nTop = 1;
148223 }
148224 }else{
148225 assert( eOp & (WO_LT|WO_LE) );
148226 testcase( eOp & WO_LT );
148227 testcase( eOp & WO_LE );
148228 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
148229 pNew->u.btree.nTop = whereRangeVectorLen(
148230 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
148231 );
148232 pTop = pTerm;
148233 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
148234 pNew->aLTerm[pNew->nLTerm-2] : 0;
148235 }
148236
148237 /* At this point pNew->nOut is set to the number of rows expected to
148238 ** be visited by the index scan before considering term pTerm, or the
148239 ** values of nIn and nInMul. In other words, assuming that all
148240 ** "x IN(...)" terms are replaced with "x = ?". This block updates
148241 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
148242 assert( pNew->nOut==saved_nOut );
148243 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
148244 /* Adjust nOut using stat4 data. Or, if there is no stat4
148245 ** data, using some other estimate. */
148246 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
148247 }else{
148248 int nEq = ++pNew->u.btree.nEq;
148249 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
148250
148251 assert( pNew->nOut==saved_nOut );
148252 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
148253 assert( (eOp & WO_IN) || nIn==0 );
148254 testcase( eOp & WO_IN );
148255 pNew->nOut += pTerm->truthProb;
148256 pNew->nOut -= nIn;
148257 }else{
148258#ifdef SQLITE_ENABLE_STAT4
148259 tRowcnt nOut = 0;
148260 if( nInMul==0
148261 && pProbe->nSample
148262 && pNew->u.btree.nEq<=pProbe->nSampleCol
148263 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
148264 && OptimizationEnabled(db, SQLITE_Stat4)
148265 ){
148266 Expr *pExpr = pTerm->pExpr;
148267 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
148268 testcase( eOp & WO_EQ );
148269 testcase( eOp & WO_IS );
148270 testcase( eOp & WO_ISNULL );
148271 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
148272 }else{
148273 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
148274 }
148275 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
148276 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
148277 if( nOut ){
148278 pNew->nOut = sqlite3LogEst(nOut);
148279 if( nEq==1
148280 /* TUNING: Mark terms as "low selectivity" if they seem likely
148281 ** to be true for half or more of the rows in the table.
148282 ** See tag-202002240-1 */
148283 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
148284 ){
148285#if WHERETRACE_ENABLED /* 0x01 */
148286 if( sqlite3WhereTrace & 0x01 ){
148287 sqlite3DebugPrintf(
148288 "STAT4 determines term has low selectivity:\n");
148289 sqlite3WhereTermPrint(pTerm, 999);
148290 }
148291#endif
148292 pTerm->wtFlags |= TERM_HIGHTRUTH;
148293 if( pTerm->wtFlags & TERM_HEURTRUTH ){
148294 /* If the term has previously been used with an assumption of
148295 ** higher selectivity, then set the flag to rerun the
148296 ** loop computations. */
148297 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
148298 }
148299 }
148300 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
148301 pNew->nOut -= nIn;
148302 }
148303 }
148304 if( nOut==0 )
148305#endif
148306 {
148307 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
148308 if( eOp & WO_ISNULL ){
148309 /* TUNING: If there is no likelihood() value, assume that a
148310 ** "col IS NULL" expression matches twice as many rows
148311 ** as (col=?). */
148312 pNew->nOut += 10;
148313 }
148314 }
148315 }
148316 }
148317
148318 /* Set rCostIdx to the cost of visiting selected rows in index. Add
148319 ** it to pNew->rRun, which is currently set to the cost of the index
148320 ** seek only. Then, if this is a non-covering index, add the cost of
148321 ** visiting the rows in the main table. */
148322 assert( pSrc->pTab->szTabRow>0 );
148323 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
148324 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
148325 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
148326 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
148327 }
148328 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
148329
148330 nOutUnadjusted = pNew->nOut;
148331 pNew->rRun += nInMul + nIn;
148332 pNew->nOut += nInMul + nIn;
148333 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
148334 rc = whereLoopInsert(pBuilder, pNew);
148335
148336 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
148337 pNew->nOut = saved_nOut;
148338 }else{
148339 pNew->nOut = nOutUnadjusted;
148340 }
148341
148342 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
148343 && pNew->u.btree.nEq<pProbe->nColumn
148344 ){
148345 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
148346 }
148347 pNew->nOut = saved_nOut;
148348#ifdef SQLITE_ENABLE_STAT4
148349 pBuilder->nRecValid = nRecValid;
148350#endif
148351 }
148352 pNew->prereq = saved_prereq;
148353 pNew->u.btree.nEq = saved_nEq;
148354 pNew->u.btree.nBtm = saved_nBtm;
148355 pNew->u.btree.nTop = saved_nTop;
148356 pNew->nSkip = saved_nSkip;
148357 pNew->wsFlags = saved_wsFlags;
148358 pNew->nOut = saved_nOut;
148359 pNew->nLTerm = saved_nLTerm;
148360
148361 /* Consider using a skip-scan if there are no WHERE clause constraints
148362 ** available for the left-most terms of the index, and if the average
148363 ** number of repeats in the left-most terms is at least 18.
148364 **
148365 ** The magic number 18 is selected on the basis that scanning 17 rows
148366 ** is almost always quicker than an index seek (even though if the index
148367 ** contains fewer than 2^17 rows we assume otherwise in other parts of
148368 ** the code). And, even if it is not, it should not be too much slower.
148369 ** On the other hand, the extra seeks could end up being significantly
148370 ** more expensive. */
148371 assert( 42==sqlite3LogEst(18) );
148372 if( saved_nEq==saved_nSkip
148373 && saved_nEq+1<pProbe->nKeyCol
148374 && saved_nEq==pNew->nLTerm
148375 && pProbe->noSkipScan==0
148376 && pProbe->hasStat1!=0
148377 && OptimizationEnabled(db, SQLITE_SkipScan)
148378 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
148379 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
148380 ){
148381 LogEst nIter;
148382 pNew->u.btree.nEq++;
148383 pNew->nSkip++;
148384 pNew->aLTerm[pNew->nLTerm++] = 0;
148385 pNew->wsFlags |= WHERE_SKIPSCAN;
148386 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
148387 pNew->nOut -= nIter;
148388 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
148389 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
148390 nIter += 5;
148391 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
148392 pNew->nOut = saved_nOut;
148393 pNew->u.btree.nEq = saved_nEq;
148394 pNew->nSkip = saved_nSkip;
148395 pNew->wsFlags = saved_wsFlags;
148396 }
148397
148398 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
148399 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
148400 return rc;
148401}
148402
148403/*
148404** Return True if it is possible that pIndex might be useful in
148405** implementing the ORDER BY clause in pBuilder.
148406**
148407** Return False if pBuilder does not contain an ORDER BY clause or
148408** if there is no way for pIndex to be useful in implementing that
148409** ORDER BY clause.
148410*/
148411static int indexMightHelpWithOrderBy(
148412 WhereLoopBuilder *pBuilder,
148413 Index *pIndex,
148414 int iCursor
148415){
148416 ExprList *pOB;
148417 ExprList *aColExpr;
148418 int ii, jj;
148419
148420 if( pIndex->bUnordered ) return 0;
148421 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
148422 for(ii=0; ii<pOB->nExpr; ii++){
148423 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
148424 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
148425 if( pExpr->iColumn<0 ) return 1;
148426 for(jj=0; jj<pIndex->nKeyCol; jj++){
148427 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
148428 }
148429 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
148430 for(jj=0; jj<pIndex->nKeyCol; jj++){
148431 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
148432 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
148433 return 1;
148434 }
148435 }
148436 }
148437 }
148438 return 0;
148439}
148440
148441/* Check to see if a partial index with pPartIndexWhere can be used
148442** in the current query. Return true if it can be and false if not.
148443*/
148444static int whereUsablePartialIndex(
148445 int iTab, /* The table for which we want an index */
148446 int isLeft, /* True if iTab is the right table of a LEFT JOIN */
148447 WhereClause *pWC, /* The WHERE clause of the query */
148448 Expr *pWhere /* The WHERE clause from the partial index */
148449){
148450 int i;
148451 WhereTerm *pTerm;
148452 Parse *pParse = pWC->pWInfo->pParse;
148453 while( pWhere->op==TK_AND ){
148454 if( !whereUsablePartialIndex(iTab,isLeft,pWC,pWhere->pLeft) ) return 0;
148455 pWhere = pWhere->pRight;
148456 }
148457 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
148458 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
148459 Expr *pExpr;
148460 pExpr = pTerm->pExpr;
148461 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
148462 && (isLeft==0 || ExprHasProperty(pExpr, EP_FromJoin))
148463 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
148464 ){
148465 return 1;
148466 }
148467 }
148468 return 0;
148469}
148470
148471/*
148472** Add all WhereLoop objects for a single table of the join where the table
148473** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
148474** a b-tree table, not a virtual table.
148475**
148476** The costs (WhereLoop.rRun) of the b-tree loops added by this function
148477** are calculated as follows:
148478**
148479** For a full scan, assuming the table (or index) contains nRow rows:
148480**
148481** cost = nRow * 3.0 // full-table scan
148482** cost = nRow * K // scan of covering index
148483** cost = nRow * (K+3.0) // scan of non-covering index
148484**
148485** where K is a value between 1.1 and 3.0 set based on the relative
148486** estimated average size of the index and table records.
148487**
148488** For an index scan, where nVisit is the number of index rows visited
148489** by the scan, and nSeek is the number of seek operations required on
148490** the index b-tree:
148491**
148492** cost = nSeek * (log(nRow) + K * nVisit) // covering index
148493** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
148494**
148495** Normally, nSeek is 1. nSeek values greater than 1 come about if the
148496** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
148497** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
148498**
148499** The estimated values (nRow, nVisit, nSeek) often contain a large amount
148500** of uncertainty. For this reason, scoring is designed to pick plans that
148501** "do the least harm" if the estimates are inaccurate. For example, a
148502** log(nRow) factor is omitted from a non-covering index scan in order to
148503** bias the scoring in favor of using an index, since the worst-case
148504** performance of using an index is far better than the worst-case performance
148505** of a full table scan.
148506*/
148507static int whereLoopAddBtree(
148508 WhereLoopBuilder *pBuilder, /* WHERE clause information */
148509 Bitmask mPrereq /* Extra prerequesites for using this table */
148510){
148511 WhereInfo *pWInfo; /* WHERE analysis context */
148512 Index *pProbe; /* An index we are evaluating */
148513 Index sPk; /* A fake index object for the primary key */
148514 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
148515 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
148516 SrcList *pTabList; /* The FROM clause */
148517 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
148518 WhereLoop *pNew; /* Template WhereLoop object */
148519 int rc = SQLITE_OK; /* Return code */
148520 int iSortIdx = 1; /* Index number */
148521 int b; /* A boolean value */
148522 LogEst rSize; /* number of rows in the table */
148523 LogEst rLogSize; /* Logarithm of the number of rows in the table */
148524 WhereClause *pWC; /* The parsed WHERE clause */
148525 Table *pTab; /* Table being queried */
148526
148527 pNew = pBuilder->pNew;
148528 pWInfo = pBuilder->pWInfo;
148529 pTabList = pWInfo->pTabList;
148530 pSrc = pTabList->a + pNew->iTab;
148531 pTab = pSrc->pTab;
148532 pWC = pBuilder->pWC;
148533 assert( !IsVirtual(pSrc->pTab) );
148534
148535 if( pSrc->pIBIndex ){
148536 /* An INDEXED BY clause specifies a particular index to use */
148537 pProbe = pSrc->pIBIndex;
148538 }else if( !HasRowid(pTab) ){
148539 pProbe = pTab->pIndex;
148540 }else{
148541 /* There is no INDEXED BY clause. Create a fake Index object in local
148542 ** variable sPk to represent the rowid primary key index. Make this
148543 ** fake index the first in a chain of Index objects with all of the real
148544 ** indices to follow */
148545 Index *pFirst; /* First of real indices on the table */
148546 memset(&sPk, 0, sizeof(Index));
148547 sPk.nKeyCol = 1;
148548 sPk.nColumn = 1;
148549 sPk.aiColumn = &aiColumnPk;
148550 sPk.aiRowLogEst = aiRowEstPk;
148551 sPk.onError = OE_Replace;
148552 sPk.pTable = pTab;
148553 sPk.szIdxRow = pTab->szTabRow;
148554 sPk.idxType = SQLITE_IDXTYPE_IPK;
148555 aiRowEstPk[0] = pTab->nRowLogEst;
148556 aiRowEstPk[1] = 0;
148557 pFirst = pSrc->pTab->pIndex;
148558 if( pSrc->fg.notIndexed==0 ){
148559 /* The real indices of the table are only considered if the
148560 ** NOT INDEXED qualifier is omitted from the FROM clause */
148561 sPk.pNext = pFirst;
148562 }
148563 pProbe = &sPk;
148564 }
148565 rSize = pTab->nRowLogEst;
148566 rLogSize = estLog(rSize);
148567
148568#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
148569 /* Automatic indexes */
148570 if( !pBuilder->pOrSet /* Not part of an OR optimization */
148571 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
148572 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
148573 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
148574 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
148575 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
148576 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
148577 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
148578 ){
148579 /* Generate auto-index WhereLoops */
148580 WhereTerm *pTerm;
148581 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
148582 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
148583 if( pTerm->prereqRight & pNew->maskSelf ) continue;
148584 if( termCanDriveIndex(pTerm, pSrc, 0) ){
148585 pNew->u.btree.nEq = 1;
148586 pNew->nSkip = 0;
148587 pNew->u.btree.pIndex = 0;
148588 pNew->nLTerm = 1;
148589 pNew->aLTerm[0] = pTerm;
148590 /* TUNING: One-time cost for computing the automatic index is
148591 ** estimated to be X*N*log2(N) where N is the number of rows in
148592 ** the table being indexed and where X is 7 (LogEst=28) for normal
148593 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
148594 ** of X is smaller for views and subqueries so that the query planner
148595 ** will be more aggressive about generating automatic indexes for
148596 ** those objects, since there is no opportunity to add schema
148597 ** indexes on subqueries and views. */
148598 pNew->rSetup = rLogSize + rSize;
148599 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
148600 pNew->rSetup += 28;
148601 }else{
148602 pNew->rSetup -= 10;
148603 }
148604 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
148605 if( pNew->rSetup<0 ) pNew->rSetup = 0;
148606 /* TUNING: Each index lookup yields 20 rows in the table. This
148607 ** is more than the usual guess of 10 rows, since we have no way
148608 ** of knowing how selective the index will ultimately be. It would
148609 ** not be unreasonable to make this value much larger. */
148610 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
148611 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
148612 pNew->wsFlags = WHERE_AUTO_INDEX;
148613 pNew->prereq = mPrereq | pTerm->prereqRight;
148614 rc = whereLoopInsert(pBuilder, pNew);
148615 }
148616 }
148617 }
148618#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
148619
148620 /* Loop over all indices. If there was an INDEXED BY clause, then only
148621 ** consider index pProbe. */
148622 for(; rc==SQLITE_OK && pProbe;
148623 pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
148624 ){
148625 int isLeft = (pSrc->fg.jointype & JT_OUTER)!=0;
148626 if( pProbe->pPartIdxWhere!=0
148627 && !whereUsablePartialIndex(pSrc->iCursor, isLeft, pWC,
148628 pProbe->pPartIdxWhere)
148629 ){
148630 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
148631 continue; /* Partial index inappropriate for this query */
148632 }
148633 if( pProbe->bNoQuery ) continue;
148634 rSize = pProbe->aiRowLogEst[0];
148635 pNew->u.btree.nEq = 0;
148636 pNew->u.btree.nBtm = 0;
148637 pNew->u.btree.nTop = 0;
148638 pNew->nSkip = 0;
148639 pNew->nLTerm = 0;
148640 pNew->iSortIdx = 0;
148641 pNew->rSetup = 0;
148642 pNew->prereq = mPrereq;
148643 pNew->nOut = rSize;
148644 pNew->u.btree.pIndex = pProbe;
148645 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
148646
148647 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
148648 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
148649 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
148650 /* Integer primary key index */
148651 pNew->wsFlags = WHERE_IPK;
148652
148653 /* Full table scan */
148654 pNew->iSortIdx = b ? iSortIdx : 0;
148655 /* TUNING: Cost of full table scan is (N*3.0). */
148656 pNew->rRun = rSize + 16;
148657 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
148658 whereLoopOutputAdjust(pWC, pNew, rSize);
148659 rc = whereLoopInsert(pBuilder, pNew);
148660 pNew->nOut = rSize;
148661 if( rc ) break;
148662 }else{
148663 Bitmask m;
148664 if( pProbe->isCovering ){
148665 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
148666 m = 0;
148667 }else{
148668 m = pSrc->colUsed & pProbe->colNotIdxed;
148669 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
148670 }
148671
148672 /* Full scan via index */
148673 if( b
148674 || !HasRowid(pTab)
148675 || pProbe->pPartIdxWhere!=0
148676 || pSrc->fg.isIndexedBy
148677 || ( m==0
148678 && pProbe->bUnordered==0
148679 && (pProbe->szIdxRow<pTab->szTabRow)
148680 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
148681 && sqlite3GlobalConfig.bUseCis
148682 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
148683 )
148684 ){
148685 pNew->iSortIdx = b ? iSortIdx : 0;
148686
148687 /* The cost of visiting the index rows is N*K, where K is
148688 ** between 1.1 and 3.0, depending on the relative sizes of the
148689 ** index and table rows. */
148690 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
148691 if( m!=0 ){
148692 /* If this is a non-covering index scan, add in the cost of
148693 ** doing table lookups. The cost will be 3x the number of
148694 ** lookups. Take into account WHERE clause terms that can be
148695 ** satisfied using just the index, and that do not require a
148696 ** table lookup. */
148697 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
148698 int ii;
148699 int iCur = pSrc->iCursor;
148700 WhereClause *pWC2 = &pWInfo->sWC;
148701 for(ii=0; ii<pWC2->nTerm; ii++){
148702 WhereTerm *pTerm = &pWC2->a[ii];
148703 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
148704 break;
148705 }
148706 /* pTerm can be evaluated using just the index. So reduce
148707 ** the expected number of table lookups accordingly */
148708 if( pTerm->truthProb<=0 ){
148709 nLookup += pTerm->truthProb;
148710 }else{
148711 nLookup--;
148712 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
148713 }
148714 }
148715
148716 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
148717 }
148718 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
148719 whereLoopOutputAdjust(pWC, pNew, rSize);
148720 rc = whereLoopInsert(pBuilder, pNew);
148721 pNew->nOut = rSize;
148722 if( rc ) break;
148723 }
148724 }
148725
148726 pBuilder->bldFlags1 = 0;
148727 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
148728 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
148729 /* If a non-unique index is used, or if a prefix of the key for
148730 ** unique index is used (making the index functionally non-unique)
148731 ** then the sqlite_stat1 data becomes important for scoring the
148732 ** plan */
148733 pTab->tabFlags |= TF_StatsUsed;
148734 }
148735#ifdef SQLITE_ENABLE_STAT4
148736 sqlite3Stat4ProbeFree(pBuilder->pRec);
148737 pBuilder->nRecValid = 0;
148738 pBuilder->pRec = 0;
148739#endif
148740 }
148741 return rc;
148742}
148743
148744#ifndef SQLITE_OMIT_VIRTUALTABLE
148745
148746/*
148747** Argument pIdxInfo is already populated with all constraints that may
148748** be used by the virtual table identified by pBuilder->pNew->iTab. This
148749** function marks a subset of those constraints usable, invokes the
148750** xBestIndex method and adds the returned plan to pBuilder.
148751**
148752** A constraint is marked usable if:
148753**
148754** * Argument mUsable indicates that its prerequisites are available, and
148755**
148756** * It is not one of the operators specified in the mExclude mask passed
148757** as the fourth argument (which in practice is either WO_IN or 0).
148758**
148759** Argument mPrereq is a mask of tables that must be scanned before the
148760** virtual table in question. These are added to the plans prerequisites
148761** before it is added to pBuilder.
148762**
148763** Output parameter *pbIn is set to true if the plan added to pBuilder
148764** uses one or more WO_IN terms, or false otherwise.
148765*/
148766static int whereLoopAddVirtualOne(
148767 WhereLoopBuilder *pBuilder,
148768 Bitmask mPrereq, /* Mask of tables that must be used. */
148769 Bitmask mUsable, /* Mask of usable tables */
148770 u16 mExclude, /* Exclude terms using these operators */
148771 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
148772 u16 mNoOmit, /* Do not omit these constraints */
148773 int *pbIn /* OUT: True if plan uses an IN(...) op */
148774){
148775 WhereClause *pWC = pBuilder->pWC;
148776 struct sqlite3_index_constraint *pIdxCons;
148777 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
148778 int i;
148779 int mxTerm;
148780 int rc = SQLITE_OK;
148781 WhereLoop *pNew = pBuilder->pNew;
148782 Parse *pParse = pBuilder->pWInfo->pParse;
148783 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
148784 int nConstraint = pIdxInfo->nConstraint;
148785
148786 assert( (mUsable & mPrereq)==mPrereq );
148787 *pbIn = 0;
148788 pNew->prereq = mPrereq;
148789
148790 /* Set the usable flag on the subset of constraints identified by
148791 ** arguments mUsable and mExclude. */
148792 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
148793 for(i=0; i<nConstraint; i++, pIdxCons++){
148794 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
148795 pIdxCons->usable = 0;
148796 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
148797 && (pTerm->eOperator & mExclude)==0
148798 ){
148799 pIdxCons->usable = 1;
148800 }
148801 }
148802
148803 /* Initialize the output fields of the sqlite3_index_info structure */
148804 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
148805 assert( pIdxInfo->needToFreeIdxStr==0 );
148806 pIdxInfo->idxStr = 0;
148807 pIdxInfo->idxNum = 0;
148808 pIdxInfo->orderByConsumed = 0;
148809 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
148810 pIdxInfo->estimatedRows = 25;
148811 pIdxInfo->idxFlags = 0;
148812 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
148813
148814 /* Invoke the virtual table xBestIndex() method */
148815 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
148816 if( rc ){
148817 if( rc==SQLITE_CONSTRAINT ){
148818 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
148819 ** that the particular combination of parameters provided is unusable.
148820 ** Make no entries in the loop table.
148821 */
148822 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
148823 return SQLITE_OK;
148824 }
148825 return rc;
148826 }
148827
148828 mxTerm = -1;
148829 assert( pNew->nLSlot>=nConstraint );
148830 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
148831 pNew->u.vtab.omitMask = 0;
148832 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
148833 for(i=0; i<nConstraint; i++, pIdxCons++){
148834 int iTerm;
148835 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
148836 WhereTerm *pTerm;
148837 int j = pIdxCons->iTermOffset;
148838 if( iTerm>=nConstraint
148839 || j<0
148840 || j>=pWC->nTerm
148841 || pNew->aLTerm[iTerm]!=0
148842 || pIdxCons->usable==0
148843 ){
148844 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
148845 testcase( pIdxInfo->needToFreeIdxStr );
148846 return SQLITE_ERROR;
148847 }
148848 testcase( iTerm==nConstraint-1 );
148849 testcase( j==0 );
148850 testcase( j==pWC->nTerm-1 );
148851 pTerm = &pWC->a[j];
148852 pNew->prereq |= pTerm->prereqRight;
148853 assert( iTerm<pNew->nLSlot );
148854 pNew->aLTerm[iTerm] = pTerm;
148855 if( iTerm>mxTerm ) mxTerm = iTerm;
148856 testcase( iTerm==15 );
148857 testcase( iTerm==16 );
148858 if( pUsage[i].omit ){
148859 if( i<16 && ((1<<i)&mNoOmit)==0 ){
148860 testcase( i!=iTerm );
148861 pNew->u.vtab.omitMask |= 1<<iTerm;
148862 }else{
148863 testcase( i!=iTerm );
148864 }
148865 }
148866 if( (pTerm->eOperator & WO_IN)!=0 ){
148867 /* A virtual table that is constrained by an IN clause may not
148868 ** consume the ORDER BY clause because (1) the order of IN terms
148869 ** is not necessarily related to the order of output terms and
148870 ** (2) Multiple outputs from a single IN value will not merge
148871 ** together. */
148872 pIdxInfo->orderByConsumed = 0;
148873 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
148874 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
148875 }
148876 }
148877 }
148878
148879 pNew->nLTerm = mxTerm+1;
148880 for(i=0; i<=mxTerm; i++){
148881 if( pNew->aLTerm[i]==0 ){
148882 /* The non-zero argvIdx values must be contiguous. Raise an
148883 ** error if they are not */
148884 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
148885 testcase( pIdxInfo->needToFreeIdxStr );
148886 return SQLITE_ERROR;
148887 }
148888 }
148889 assert( pNew->nLTerm<=pNew->nLSlot );
148890 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
148891 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
148892 pIdxInfo->needToFreeIdxStr = 0;
148893 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
148894 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
148895 pIdxInfo->nOrderBy : 0);
148896 pNew->rSetup = 0;
148897 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
148898 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
148899
148900 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
148901 ** that the scan will visit at most one row. Clear it otherwise. */
148902 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
148903 pNew->wsFlags |= WHERE_ONEROW;
148904 }else{
148905 pNew->wsFlags &= ~WHERE_ONEROW;
148906 }
148907 rc = whereLoopInsert(pBuilder, pNew);
148908 if( pNew->u.vtab.needFree ){
148909 sqlite3_free(pNew->u.vtab.idxStr);
148910 pNew->u.vtab.needFree = 0;
148911 }
148912 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
148913 *pbIn, (sqlite3_uint64)mPrereq,
148914 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
148915
148916 return rc;
148917}
148918
148919/*
148920** If this function is invoked from within an xBestIndex() callback, it
148921** returns a pointer to a buffer containing the name of the collation
148922** sequence associated with element iCons of the sqlite3_index_info.aConstraint
148923** array. Or, if iCons is out of range or there is no active xBestIndex
148924** call, return NULL.
148925*/
148926SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
148927 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
148928 const char *zRet = 0;
148929 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
148930 CollSeq *pC = 0;
148931 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
148932 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
148933 if( pX->pLeft ){
148934 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
148935 }
148936 zRet = (pC ? pC->zName : sqlite3StrBINARY);
148937 }
148938 return zRet;
148939}
148940
148941/*
148942** Add all WhereLoop objects for a table of the join identified by
148943** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
148944**
148945** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
148946** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
148947** entries that occur before the virtual table in the FROM clause and are
148948** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
148949** mUnusable mask contains all FROM clause entries that occur after the
148950** virtual table and are separated from it by at least one LEFT or
148951** CROSS JOIN.
148952**
148953** For example, if the query were:
148954**
148955** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
148956**
148957** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
148958**
148959** All the tables in mPrereq must be scanned before the current virtual
148960** table. So any terms for which all prerequisites are satisfied by
148961** mPrereq may be specified as "usable" in all calls to xBestIndex.
148962** Conversely, all tables in mUnusable must be scanned after the current
148963** virtual table, so any terms for which the prerequisites overlap with
148964** mUnusable should always be configured as "not-usable" for xBestIndex.
148965*/
148966static int whereLoopAddVirtual(
148967 WhereLoopBuilder *pBuilder, /* WHERE clause information */
148968 Bitmask mPrereq, /* Tables that must be scanned before this one */
148969 Bitmask mUnusable /* Tables that must be scanned after this one */
148970){
148971 int rc = SQLITE_OK; /* Return code */
148972 WhereInfo *pWInfo; /* WHERE analysis context */
148973 Parse *pParse; /* The parsing context */
148974 WhereClause *pWC; /* The WHERE clause */
148975 struct SrcList_item *pSrc; /* The FROM clause term to search */
148976 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
148977 int nConstraint; /* Number of constraints in p */
148978 int bIn; /* True if plan uses IN(...) operator */
148979 WhereLoop *pNew;
148980 Bitmask mBest; /* Tables used by best possible plan */
148981 u16 mNoOmit;
148982
148983 assert( (mPrereq & mUnusable)==0 );
148984 pWInfo = pBuilder->pWInfo;
148985 pParse = pWInfo->pParse;
148986 pWC = pBuilder->pWC;
148987 pNew = pBuilder->pNew;
148988 pSrc = &pWInfo->pTabList->a[pNew->iTab];
148989 assert( IsVirtual(pSrc->pTab) );
148990 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
148991 &mNoOmit);
148992 if( p==0 ) return SQLITE_NOMEM_BKPT;
148993 pNew->rSetup = 0;
148994 pNew->wsFlags = WHERE_VIRTUALTABLE;
148995 pNew->nLTerm = 0;
148996 pNew->u.vtab.needFree = 0;
148997 nConstraint = p->nConstraint;
148998 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
148999 sqlite3DbFree(pParse->db, p);
149000 return SQLITE_NOMEM_BKPT;
149001 }
149002
149003 /* First call xBestIndex() with all constraints usable. */
149004 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
149005 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
149006 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
149007
149008 /* If the call to xBestIndex() with all terms enabled produced a plan
149009 ** that does not require any source tables (IOW: a plan with mBest==0)
149010 ** and does not use an IN(...) operator, then there is no point in making
149011 ** any further calls to xBestIndex() since they will all return the same
149012 ** result (if the xBestIndex() implementation is sane). */
149013 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
149014 int seenZero = 0; /* True if a plan with no prereqs seen */
149015 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
149016 Bitmask mPrev = 0;
149017 Bitmask mBestNoIn = 0;
149018
149019 /* If the plan produced by the earlier call uses an IN(...) term, call
149020 ** xBestIndex again, this time with IN(...) terms disabled. */
149021 if( bIn ){
149022 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
149023 rc = whereLoopAddVirtualOne(
149024 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
149025 assert( bIn==0 );
149026 mBestNoIn = pNew->prereq & ~mPrereq;
149027 if( mBestNoIn==0 ){
149028 seenZero = 1;
149029 seenZeroNoIN = 1;
149030 }
149031 }
149032
149033 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
149034 ** in the set of terms that apply to the current virtual table. */
149035 while( rc==SQLITE_OK ){
149036 int i;
149037 Bitmask mNext = ALLBITS;
149038 assert( mNext>0 );
149039 for(i=0; i<nConstraint; i++){
149040 Bitmask mThis = (
149041 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
149042 );
149043 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
149044 }
149045 mPrev = mNext;
149046 if( mNext==ALLBITS ) break;
149047 if( mNext==mBest || mNext==mBestNoIn ) continue;
149048 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
149049 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
149050 rc = whereLoopAddVirtualOne(
149051 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
149052 if( pNew->prereq==mPrereq ){
149053 seenZero = 1;
149054 if( bIn==0 ) seenZeroNoIN = 1;
149055 }
149056 }
149057
149058 /* If the calls to xBestIndex() in the above loop did not find a plan
149059 ** that requires no source tables at all (i.e. one guaranteed to be
149060 ** usable), make a call here with all source tables disabled */
149061 if( rc==SQLITE_OK && seenZero==0 ){
149062 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
149063 rc = whereLoopAddVirtualOne(
149064 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
149065 if( bIn==0 ) seenZeroNoIN = 1;
149066 }
149067
149068 /* If the calls to xBestIndex() have so far failed to find a plan
149069 ** that requires no source tables at all and does not use an IN(...)
149070 ** operator, make a final call to obtain one here. */
149071 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
149072 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
149073 rc = whereLoopAddVirtualOne(
149074 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
149075 }
149076 }
149077
149078 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
149079 sqlite3DbFreeNN(pParse->db, p);
149080 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
149081 return rc;
149082}
149083#endif /* SQLITE_OMIT_VIRTUALTABLE */
149084
149085/*
149086** Add WhereLoop entries to handle OR terms. This works for either
149087** btrees or virtual tables.
149088*/
149089static int whereLoopAddOr(
149090 WhereLoopBuilder *pBuilder,
149091 Bitmask mPrereq,
149092 Bitmask mUnusable
149093){
149094 WhereInfo *pWInfo = pBuilder->pWInfo;
149095 WhereClause *pWC;
149096 WhereLoop *pNew;
149097 WhereTerm *pTerm, *pWCEnd;
149098 int rc = SQLITE_OK;
149099 int iCur;
149100 WhereClause tempWC;
149101 WhereLoopBuilder sSubBuild;
149102 WhereOrSet sSum, sCur;
149103 struct SrcList_item *pItem;
149104
149105 pWC = pBuilder->pWC;
149106 pWCEnd = pWC->a + pWC->nTerm;
149107 pNew = pBuilder->pNew;
149108 memset(&sSum, 0, sizeof(sSum));
149109 pItem = pWInfo->pTabList->a + pNew->iTab;
149110 iCur = pItem->iCursor;
149111
149112 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
149113 if( (pTerm->eOperator & WO_OR)!=0
149114 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
149115 ){
149116 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
149117 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
149118 WhereTerm *pOrTerm;
149119 int once = 1;
149120 int i, j;
149121
149122 sSubBuild = *pBuilder;
149123 sSubBuild.pOrderBy = 0;
149124 sSubBuild.pOrSet = &sCur;
149125
149126 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
149127 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
149128 if( (pOrTerm->eOperator & WO_AND)!=0 ){
149129 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
149130 }else if( pOrTerm->leftCursor==iCur ){
149131 tempWC.pWInfo = pWC->pWInfo;
149132 tempWC.pOuter = pWC;
149133 tempWC.op = TK_AND;
149134 tempWC.nTerm = 1;
149135 tempWC.a = pOrTerm;
149136 sSubBuild.pWC = &tempWC;
149137 }else{
149138 continue;
149139 }
149140 sCur.n = 0;
149141#ifdef WHERETRACE_ENABLED
149142 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
149143 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
149144 if( sqlite3WhereTrace & 0x400 ){
149145 sqlite3WhereClausePrint(sSubBuild.pWC);
149146 }
149147#endif
149148#ifndef SQLITE_OMIT_VIRTUALTABLE
149149 if( IsVirtual(pItem->pTab) ){
149150 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
149151 }else
149152#endif
149153 {
149154 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
149155 }
149156 if( rc==SQLITE_OK ){
149157 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
149158 }
149159 assert( rc==SQLITE_OK || rc==SQLITE_DONE || sCur.n==0 );
149160 testcase( rc==SQLITE_DONE );
149161 if( sCur.n==0 ){
149162 sSum.n = 0;
149163 break;
149164 }else if( once ){
149165 whereOrMove(&sSum, &sCur);
149166 once = 0;
149167 }else{
149168 WhereOrSet sPrev;
149169 whereOrMove(&sPrev, &sSum);
149170 sSum.n = 0;
149171 for(i=0; i<sPrev.n; i++){
149172 for(j=0; j<sCur.n; j++){
149173 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
149174 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
149175 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
149176 }
149177 }
149178 }
149179 }
149180 pNew->nLTerm = 1;
149181 pNew->aLTerm[0] = pTerm;
149182 pNew->wsFlags = WHERE_MULTI_OR;
149183 pNew->rSetup = 0;
149184 pNew->iSortIdx = 0;
149185 memset(&pNew->u, 0, sizeof(pNew->u));
149186 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
149187 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
149188 ** of all sub-scans required by the OR-scan. However, due to rounding
149189 ** errors, it may be that the cost of the OR-scan is equal to its
149190 ** most expensive sub-scan. Add the smallest possible penalty
149191 ** (equivalent to multiplying the cost by 1.07) to ensure that
149192 ** this does not happen. Otherwise, for WHERE clauses such as the
149193 ** following where there is an index on "y":
149194 **
149195 ** WHERE likelihood(x=?, 0.99) OR y=?
149196 **
149197 ** the planner may elect to "OR" together a full-table scan and an
149198 ** index lookup. And other similarly odd results. */
149199 pNew->rRun = sSum.a[i].rRun + 1;
149200 pNew->nOut = sSum.a[i].nOut;
149201 pNew->prereq = sSum.a[i].prereq;
149202 rc = whereLoopInsert(pBuilder, pNew);
149203 }
149204 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
149205 }
149206 }
149207 return rc;
149208}
149209
149210/*
149211** Add all WhereLoop objects for all tables
149212*/
149213static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
149214 WhereInfo *pWInfo = pBuilder->pWInfo;
149215 Bitmask mPrereq = 0;
149216 Bitmask mPrior = 0;
149217 int iTab;
149218 SrcList *pTabList = pWInfo->pTabList;
149219 struct SrcList_item *pItem;
149220 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
149221 sqlite3 *db = pWInfo->pParse->db;
149222 int rc = SQLITE_OK;
149223 WhereLoop *pNew;
149224
149225 /* Loop over the tables in the join, from left to right */
149226 pNew = pBuilder->pNew;
149227 whereLoopInit(pNew);
149228 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
149229 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
149230 Bitmask mUnusable = 0;
149231 pNew->iTab = iTab;
149232 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
149233 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
149234 if( (pItem->fg.jointype & (JT_LEFT|JT_CROSS))!=0 ){
149235 /* This condition is true when pItem is the FROM clause term on the
149236 ** right-hand-side of a LEFT or CROSS JOIN. */
149237 mPrereq = mPrior;
149238 }else{
149239 mPrereq = 0;
149240 }
149241#ifndef SQLITE_OMIT_VIRTUALTABLE
149242 if( IsVirtual(pItem->pTab) ){
149243 struct SrcList_item *p;
149244 for(p=&pItem[1]; p<pEnd; p++){
149245 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
149246 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
149247 }
149248 }
149249 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
149250 }else
149251#endif /* SQLITE_OMIT_VIRTUALTABLE */
149252 {
149253 rc = whereLoopAddBtree(pBuilder, mPrereq);
149254 }
149255 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
149256 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
149257 }
149258 mPrior |= pNew->maskSelf;
149259 if( rc || db->mallocFailed ){
149260 if( rc==SQLITE_DONE ){
149261 /* We hit the query planner search limit set by iPlanLimit */
149262 sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
149263 rc = SQLITE_OK;
149264 }else{
149265 break;
149266 }
149267 }
149268 }
149269
149270 whereLoopClear(db, pNew);
149271 return rc;
149272}
149273
149274/*
149275** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
149276** parameters) to see if it outputs rows in the requested ORDER BY
149277** (or GROUP BY) without requiring a separate sort operation. Return N:
149278**
149279** N>0: N terms of the ORDER BY clause are satisfied
149280** N==0: No terms of the ORDER BY clause are satisfied
149281** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
149282**
149283** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
149284** strict. With GROUP BY and DISTINCT the only requirement is that
149285** equivalent rows appear immediately adjacent to one another. GROUP BY
149286** and DISTINCT do not require rows to appear in any particular order as long
149287** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
149288** the pOrderBy terms can be matched in any order. With ORDER BY, the
149289** pOrderBy terms must be matched in strict left-to-right order.
149290*/
149291static i8 wherePathSatisfiesOrderBy(
149292 WhereInfo *pWInfo, /* The WHERE clause */
149293 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
149294 WherePath *pPath, /* The WherePath to check */
149295 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
149296 u16 nLoop, /* Number of entries in pPath->aLoop[] */
149297 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
149298 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
149299){
149300 u8 revSet; /* True if rev is known */
149301 u8 rev; /* Composite sort order */
149302 u8 revIdx; /* Index sort order */
149303 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
149304 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
149305 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
149306 u16 eqOpMask; /* Allowed equality operators */
149307 u16 nKeyCol; /* Number of key columns in pIndex */
149308 u16 nColumn; /* Total number of ordered columns in the index */
149309 u16 nOrderBy; /* Number terms in the ORDER BY clause */
149310 int iLoop; /* Index of WhereLoop in pPath being processed */
149311 int i, j; /* Loop counters */
149312 int iCur; /* Cursor number for current WhereLoop */
149313 int iColumn; /* A column number within table iCur */
149314 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
149315 WhereTerm *pTerm; /* A single term of the WHERE clause */
149316 Expr *pOBExpr; /* An expression from the ORDER BY clause */
149317 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
149318 Index *pIndex; /* The index associated with pLoop */
149319 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
149320 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
149321 Bitmask obDone; /* Mask of all ORDER BY terms */
149322 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
149323 Bitmask ready; /* Mask of inner loops */
149324
149325 /*
149326 ** We say the WhereLoop is "one-row" if it generates no more than one
149327 ** row of output. A WhereLoop is one-row if all of the following are true:
149328 ** (a) All index columns match with WHERE_COLUMN_EQ.
149329 ** (b) The index is unique
149330 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
149331 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
149332 **
149333 ** We say the WhereLoop is "order-distinct" if the set of columns from
149334 ** that WhereLoop that are in the ORDER BY clause are different for every
149335 ** row of the WhereLoop. Every one-row WhereLoop is automatically
149336 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
149337 ** is not order-distinct. To be order-distinct is not quite the same as being
149338 ** UNIQUE since a UNIQUE column or index can have multiple rows that
149339 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
149340 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
149341 **
149342 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
149343 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
149344 ** automatically order-distinct.
149345 */
149346
149347 assert( pOrderBy!=0 );
149348 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
149349
149350 nOrderBy = pOrderBy->nExpr;
149351 testcase( nOrderBy==BMS-1 );
149352 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
149353 isOrderDistinct = 1;
149354 obDone = MASKBIT(nOrderBy)-1;
149355 orderDistinctMask = 0;
149356 ready = 0;
149357 eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
149358 if( wctrlFlags & (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MAX|WHERE_ORDERBY_MIN) ){
149359 eqOpMask |= WO_IN;
149360 }
149361 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
149362 if( iLoop>0 ) ready |= pLoop->maskSelf;
149363 if( iLoop<nLoop ){
149364 pLoop = pPath->aLoop[iLoop];
149365 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
149366 }else{
149367 pLoop = pLast;
149368 }
149369 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
149370 if( pLoop->u.vtab.isOrdered && (wctrlFlags & WHERE_DISTINCTBY)==0 ){
149371 obSat = obDone;
149372 }
149373 break;
149374 }else if( wctrlFlags & WHERE_DISTINCTBY ){
149375 pLoop->u.btree.nDistinctCol = 0;
149376 }
149377 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
149378
149379 /* Mark off any ORDER BY term X that is a column in the table of
149380 ** the current loop for which there is term in the WHERE
149381 ** clause of the form X IS NULL or X=? that reference only outer
149382 ** loops.
149383 */
149384 for(i=0; i<nOrderBy; i++){
149385 if( MASKBIT(i) & obSat ) continue;
149386 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
149387 if( pOBExpr->op!=TK_COLUMN ) continue;
149388 if( pOBExpr->iTable!=iCur ) continue;
149389 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
149390 ~ready, eqOpMask, 0);
149391 if( pTerm==0 ) continue;
149392 if( pTerm->eOperator==WO_IN ){
149393 /* IN terms are only valid for sorting in the ORDER BY LIMIT
149394 ** optimization, and then only if they are actually used
149395 ** by the query plan */
149396 assert( wctrlFlags &
149397 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) );
149398 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
149399 if( j>=pLoop->nLTerm ) continue;
149400 }
149401 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
149402 Parse *pParse = pWInfo->pParse;
149403 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
149404 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
149405 assert( pColl1 );
149406 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
149407 continue;
149408 }
149409 testcase( pTerm->pExpr->op==TK_IS );
149410 }
149411 obSat |= MASKBIT(i);
149412 }
149413
149414 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
149415 if( pLoop->wsFlags & WHERE_IPK ){
149416 pIndex = 0;
149417 nKeyCol = 0;
149418 nColumn = 1;
149419 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
149420 return 0;
149421 }else{
149422 nKeyCol = pIndex->nKeyCol;
149423 nColumn = pIndex->nColumn;
149424 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
149425 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
149426 || !HasRowid(pIndex->pTable));
149427 isOrderDistinct = IsUniqueIndex(pIndex)
149428 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
149429 }
149430
149431 /* Loop through all columns of the index and deal with the ones
149432 ** that are not constrained by == or IN.
149433 */
149434 rev = revSet = 0;
149435 distinctColumns = 0;
149436 for(j=0; j<nColumn; j++){
149437 u8 bOnce = 1; /* True to run the ORDER BY search loop */
149438
149439 assert( j>=pLoop->u.btree.nEq
149440 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
149441 );
149442 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
149443 u16 eOp = pLoop->aLTerm[j]->eOperator;
149444
149445 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
149446 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
149447 ** terms imply that the index is not UNIQUE NOT NULL in which case
149448 ** the loop need to be marked as not order-distinct because it can
149449 ** have repeated NULL rows.
149450 **
149451 ** If the current term is a column of an ((?,?) IN (SELECT...))
149452 ** expression for which the SELECT returns more than one column,
149453 ** check that it is the only column used by this loop. Otherwise,
149454 ** if it is one of two or more, none of the columns can be
149455 ** considered to match an ORDER BY term.
149456 */
149457 if( (eOp & eqOpMask)!=0 ){
149458 if( eOp & (WO_ISNULL|WO_IS) ){
149459 testcase( eOp & WO_ISNULL );
149460 testcase( eOp & WO_IS );
149461 testcase( isOrderDistinct );
149462 isOrderDistinct = 0;
149463 }
149464 continue;
149465 }else if( ALWAYS(eOp & WO_IN) ){
149466 /* ALWAYS() justification: eOp is an equality operator due to the
149467 ** j<pLoop->u.btree.nEq constraint above. Any equality other
149468 ** than WO_IN is captured by the previous "if". So this one
149469 ** always has to be WO_IN. */
149470 Expr *pX = pLoop->aLTerm[j]->pExpr;
149471 for(i=j+1; i<pLoop->u.btree.nEq; i++){
149472 if( pLoop->aLTerm[i]->pExpr==pX ){
149473 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
149474 bOnce = 0;
149475 break;
149476 }
149477 }
149478 }
149479 }
149480
149481 /* Get the column number in the table (iColumn) and sort order
149482 ** (revIdx) for the j-th column of the index.
149483 */
149484 if( pIndex ){
149485 iColumn = pIndex->aiColumn[j];
149486 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
149487 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
149488 }else{
149489 iColumn = XN_ROWID;
149490 revIdx = 0;
149491 }
149492
149493 /* An unconstrained column that might be NULL means that this
149494 ** WhereLoop is not well-ordered
149495 */
149496 if( isOrderDistinct
149497 && iColumn>=0
149498 && j>=pLoop->u.btree.nEq
149499 && pIndex->pTable->aCol[iColumn].notNull==0
149500 ){
149501 isOrderDistinct = 0;
149502 }
149503
149504 /* Find the ORDER BY term that corresponds to the j-th column
149505 ** of the index and mark that ORDER BY term off
149506 */
149507 isMatch = 0;
149508 for(i=0; bOnce && i<nOrderBy; i++){
149509 if( MASKBIT(i) & obSat ) continue;
149510 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
149511 testcase( wctrlFlags & WHERE_GROUPBY );
149512 testcase( wctrlFlags & WHERE_DISTINCTBY );
149513 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
149514 if( iColumn>=XN_ROWID ){
149515 if( pOBExpr->op!=TK_COLUMN ) continue;
149516 if( pOBExpr->iTable!=iCur ) continue;
149517 if( pOBExpr->iColumn!=iColumn ) continue;
149518 }else{
149519 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
149520 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
149521 continue;
149522 }
149523 }
149524 if( iColumn!=XN_ROWID ){
149525 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
149526 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
149527 }
149528 if( wctrlFlags & WHERE_DISTINCTBY ){
149529 pLoop->u.btree.nDistinctCol = j+1;
149530 }
149531 isMatch = 1;
149532 break;
149533 }
149534 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
149535 /* Make sure the sort order is compatible in an ORDER BY clause.
149536 ** Sort order is irrelevant for a GROUP BY clause. */
149537 if( revSet ){
149538 if( (rev ^ revIdx)!=(pOrderBy->a[i].sortFlags&KEYINFO_ORDER_DESC) ){
149539 isMatch = 0;
149540 }
149541 }else{
149542 rev = revIdx ^ (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC);
149543 if( rev ) *pRevMask |= MASKBIT(iLoop);
149544 revSet = 1;
149545 }
149546 }
149547 if( isMatch && (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL) ){
149548 if( j==pLoop->u.btree.nEq ){
149549 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
149550 }else{
149551 isMatch = 0;
149552 }
149553 }
149554 if( isMatch ){
149555 if( iColumn==XN_ROWID ){
149556 testcase( distinctColumns==0 );
149557 distinctColumns = 1;
149558 }
149559 obSat |= MASKBIT(i);
149560 }else{
149561 /* No match found */
149562 if( j==0 || j<nKeyCol ){
149563 testcase( isOrderDistinct!=0 );
149564 isOrderDistinct = 0;
149565 }
149566 break;
149567 }
149568 } /* end Loop over all index columns */
149569 if( distinctColumns ){
149570 testcase( isOrderDistinct==0 );
149571 isOrderDistinct = 1;
149572 }
149573 } /* end-if not one-row */
149574
149575 /* Mark off any other ORDER BY terms that reference pLoop */
149576 if( isOrderDistinct ){
149577 orderDistinctMask |= pLoop->maskSelf;
149578 for(i=0; i<nOrderBy; i++){
149579 Expr *p;
149580 Bitmask mTerm;
149581 if( MASKBIT(i) & obSat ) continue;
149582 p = pOrderBy->a[i].pExpr;
149583 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
149584 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
149585 if( (mTerm&~orderDistinctMask)==0 ){
149586 obSat |= MASKBIT(i);
149587 }
149588 }
149589 }
149590 } /* End the loop over all WhereLoops from outer-most down to inner-most */
149591 if( obSat==obDone ) return (i8)nOrderBy;
149592 if( !isOrderDistinct ){
149593 for(i=nOrderBy-1; i>0; i--){
149594 Bitmask m = MASKBIT(i) - 1;
149595 if( (obSat&m)==m ) return i;
149596 }
149597 return 0;
149598 }
149599 return -1;
149600}
149601
149602
149603/*
149604** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
149605** the planner assumes that the specified pOrderBy list is actually a GROUP
149606** BY clause - and so any order that groups rows as required satisfies the
149607** request.
149608**
149609** Normally, in this case it is not possible for the caller to determine
149610** whether or not the rows are really being delivered in sorted order, or
149611** just in some other order that provides the required grouping. However,
149612** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
149613** this function may be called on the returned WhereInfo object. It returns
149614** true if the rows really will be sorted in the specified order, or false
149615** otherwise.
149616**
149617** For example, assuming:
149618**
149619** CREATE INDEX i1 ON t1(x, Y);
149620**
149621** then
149622**
149623** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
149624** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
149625*/
149626SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
149627 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
149628 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
149629 return pWInfo->sorted;
149630}
149631
149632#ifdef WHERETRACE_ENABLED
149633/* For debugging use only: */
149634static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
149635 static char zName[65];
149636 int i;
149637 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
149638 if( pLast ) zName[i++] = pLast->cId;
149639 zName[i] = 0;
149640 return zName;
149641}
149642#endif
149643
149644/*
149645** Return the cost of sorting nRow rows, assuming that the keys have
149646** nOrderby columns and that the first nSorted columns are already in
149647** order.
149648*/
149649static LogEst whereSortingCost(
149650 WhereInfo *pWInfo,
149651 LogEst nRow,
149652 int nOrderBy,
149653 int nSorted
149654){
149655 /* TUNING: Estimated cost of a full external sort, where N is
149656 ** the number of rows to sort is:
149657 **
149658 ** cost = (3.0 * N * log(N)).
149659 **
149660 ** Or, if the order-by clause has X terms but only the last Y
149661 ** terms are out of order, then block-sorting will reduce the
149662 ** sorting cost to:
149663 **
149664 ** cost = (3.0 * N * log(N)) * (Y/X)
149665 **
149666 ** The (Y/X) term is implemented using stack variable rScale
149667 ** below. */
149668 LogEst rScale, rSortCost;
149669 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
149670 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
149671 rSortCost = nRow + rScale + 16;
149672
149673 /* Multiple by log(M) where M is the number of output rows.
149674 ** Use the LIMIT for M if it is smaller */
149675 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
149676 nRow = pWInfo->iLimit;
149677 }
149678 rSortCost += estLog(nRow);
149679 return rSortCost;
149680}
149681
149682/*
149683** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
149684** attempts to find the lowest cost path that visits each WhereLoop
149685** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
149686**
149687** Assume that the total number of output rows that will need to be sorted
149688** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
149689** costs if nRowEst==0.
149690**
149691** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
149692** error occurs.
149693*/
149694static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
149695 int mxChoice; /* Maximum number of simultaneous paths tracked */
149696 int nLoop; /* Number of terms in the join */
149697 Parse *pParse; /* Parsing context */
149698 sqlite3 *db; /* The database connection */
149699 int iLoop; /* Loop counter over the terms of the join */
149700 int ii, jj; /* Loop counters */
149701 int mxI = 0; /* Index of next entry to replace */
149702 int nOrderBy; /* Number of ORDER BY clause terms */
149703 LogEst mxCost = 0; /* Maximum cost of a set of paths */
149704 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
149705 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
149706 WherePath *aFrom; /* All nFrom paths at the previous level */
149707 WherePath *aTo; /* The nTo best paths at the current level */
149708 WherePath *pFrom; /* An element of aFrom[] that we are working on */
149709 WherePath *pTo; /* An element of aTo[] that we are working on */
149710 WhereLoop *pWLoop; /* One of the WhereLoop objects */
149711 WhereLoop **pX; /* Used to divy up the pSpace memory */
149712 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
149713 char *pSpace; /* Temporary memory used by this routine */
149714 int nSpace; /* Bytes of space allocated at pSpace */
149715
149716 pParse = pWInfo->pParse;
149717 db = pParse->db;
149718 nLoop = pWInfo->nLevel;
149719 /* TUNING: For simple queries, only the best path is tracked.
149720 ** For 2-way joins, the 5 best paths are followed.
149721 ** For joins of 3 or more tables, track the 10 best paths */
149722 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
149723 assert( nLoop<=pWInfo->pTabList->nSrc );
149724 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
149725
149726 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
149727 ** case the purpose of this call is to estimate the number of rows returned
149728 ** by the overall query. Once this estimate has been obtained, the caller
149729 ** will invoke this function a second time, passing the estimate as the
149730 ** nRowEst parameter. */
149731 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
149732 nOrderBy = 0;
149733 }else{
149734 nOrderBy = pWInfo->pOrderBy->nExpr;
149735 }
149736
149737 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
149738 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
149739 nSpace += sizeof(LogEst) * nOrderBy;
149740 pSpace = sqlite3DbMallocRawNN(db, nSpace);
149741 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
149742 aTo = (WherePath*)pSpace;
149743 aFrom = aTo+mxChoice;
149744 memset(aFrom, 0, sizeof(aFrom[0]));
149745 pX = (WhereLoop**)(aFrom+mxChoice);
149746 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
149747 pFrom->aLoop = pX;
149748 }
149749 if( nOrderBy ){
149750 /* If there is an ORDER BY clause and it is not being ignored, set up
149751 ** space for the aSortCost[] array. Each element of the aSortCost array
149752 ** is either zero - meaning it has not yet been initialized - or the
149753 ** cost of sorting nRowEst rows of data where the first X terms of
149754 ** the ORDER BY clause are already in order, where X is the array
149755 ** index. */
149756 aSortCost = (LogEst*)pX;
149757 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
149758 }
149759 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
149760 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
149761
149762 /* Seed the search with a single WherePath containing zero WhereLoops.
149763 **
149764 ** TUNING: Do not let the number of iterations go above 28. If the cost
149765 ** of computing an automatic index is not paid back within the first 28
149766 ** rows, then do not use the automatic index. */
149767 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
149768 nFrom = 1;
149769 assert( aFrom[0].isOrdered==0 );
149770 if( nOrderBy ){
149771 /* If nLoop is zero, then there are no FROM terms in the query. Since
149772 ** in this case the query may return a maximum of one row, the results
149773 ** are already in the requested order. Set isOrdered to nOrderBy to
149774 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
149775 ** -1, indicating that the result set may or may not be ordered,
149776 ** depending on the loops added to the current plan. */
149777 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
149778 }
149779
149780 /* Compute successively longer WherePaths using the previous generation
149781 ** of WherePaths as the basis for the next. Keep track of the mxChoice
149782 ** best paths at each generation */
149783 for(iLoop=0; iLoop<nLoop; iLoop++){
149784 nTo = 0;
149785 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
149786 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
149787 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
149788 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
149789 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
149790 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
149791 Bitmask maskNew; /* Mask of src visited by (..) */
149792 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
149793
149794 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
149795 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
149796 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
149797 /* Do not use an automatic index if the this loop is expected
149798 ** to run less than 1.25 times. It is tempting to also exclude
149799 ** automatic index usage on an outer loop, but sometimes an automatic
149800 ** index is useful in the outer loop of a correlated subquery. */
149801 assert( 10==sqlite3LogEst(2) );
149802 continue;
149803 }
149804
149805 /* At this point, pWLoop is a candidate to be the next loop.
149806 ** Compute its cost */
149807 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
149808 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
149809 nOut = pFrom->nRow + pWLoop->nOut;
149810 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
149811 if( isOrdered<0 ){
149812 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
149813 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
149814 iLoop, pWLoop, &revMask);
149815 }else{
149816 revMask = pFrom->revLoop;
149817 }
149818 if( isOrdered>=0 && isOrdered<nOrderBy ){
149819 if( aSortCost[isOrdered]==0 ){
149820 aSortCost[isOrdered] = whereSortingCost(
149821 pWInfo, nRowEst, nOrderBy, isOrdered
149822 );
149823 }
149824 /* TUNING: Add a small extra penalty (5) to sorting as an
149825 ** extra encouragment to the query planner to select a plan
149826 ** where the rows emerge in the correct order without any sorting
149827 ** required. */
149828 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
149829
149830 WHERETRACE(0x002,
149831 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
149832 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
149833 rUnsorted, rCost));
149834 }else{
149835 rCost = rUnsorted;
149836 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
149837 }
149838
149839 /* Check to see if pWLoop should be added to the set of
149840 ** mxChoice best-so-far paths.
149841 **
149842 ** First look for an existing path among best-so-far paths
149843 ** that covers the same set of loops and has the same isOrdered
149844 ** setting as the current path candidate.
149845 **
149846 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
149847 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
149848 ** of legal values for isOrdered, -1..64.
149849 */
149850 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
149851 if( pTo->maskLoop==maskNew
149852 && ((pTo->isOrdered^isOrdered)&0x80)==0
149853 ){
149854 testcase( jj==nTo-1 );
149855 break;
149856 }
149857 }
149858 if( jj>=nTo ){
149859 /* None of the existing best-so-far paths match the candidate. */
149860 if( nTo>=mxChoice
149861 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
149862 ){
149863 /* The current candidate is no better than any of the mxChoice
149864 ** paths currently in the best-so-far buffer. So discard
149865 ** this candidate as not viable. */
149866#ifdef WHERETRACE_ENABLED /* 0x4 */
149867 if( sqlite3WhereTrace&0x4 ){
149868 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
149869 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149870 isOrdered>=0 ? isOrdered+'0' : '?');
149871 }
149872#endif
149873 continue;
149874 }
149875 /* If we reach this points it means that the new candidate path
149876 ** needs to be added to the set of best-so-far paths. */
149877 if( nTo<mxChoice ){
149878 /* Increase the size of the aTo set by one */
149879 jj = nTo++;
149880 }else{
149881 /* New path replaces the prior worst to keep count below mxChoice */
149882 jj = mxI;
149883 }
149884 pTo = &aTo[jj];
149885#ifdef WHERETRACE_ENABLED /* 0x4 */
149886 if( sqlite3WhereTrace&0x4 ){
149887 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
149888 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149889 isOrdered>=0 ? isOrdered+'0' : '?');
149890 }
149891#endif
149892 }else{
149893 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
149894 ** same set of loops and has the same isOrdered setting as the
149895 ** candidate path. Check to see if the candidate should replace
149896 ** pTo or if the candidate should be skipped.
149897 **
149898 ** The conditional is an expanded vector comparison equivalent to:
149899 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
149900 */
149901 if( pTo->rCost<rCost
149902 || (pTo->rCost==rCost
149903 && (pTo->nRow<nOut
149904 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
149905 )
149906 )
149907 ){
149908#ifdef WHERETRACE_ENABLED /* 0x4 */
149909 if( sqlite3WhereTrace&0x4 ){
149910 sqlite3DebugPrintf(
149911 "Skip %s cost=%-3d,%3d,%3d order=%c",
149912 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149913 isOrdered>=0 ? isOrdered+'0' : '?');
149914 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
149915 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149916 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
149917 }
149918#endif
149919 /* Discard the candidate path from further consideration */
149920 testcase( pTo->rCost==rCost );
149921 continue;
149922 }
149923 testcase( pTo->rCost==rCost+1 );
149924 /* Control reaches here if the candidate path is better than the
149925 ** pTo path. Replace pTo with the candidate. */
149926#ifdef WHERETRACE_ENABLED /* 0x4 */
149927 if( sqlite3WhereTrace&0x4 ){
149928 sqlite3DebugPrintf(
149929 "Update %s cost=%-3d,%3d,%3d order=%c",
149930 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
149931 isOrdered>=0 ? isOrdered+'0' : '?');
149932 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
149933 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149934 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
149935 }
149936#endif
149937 }
149938 /* pWLoop is a winner. Add it to the set of best so far */
149939 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
149940 pTo->revLoop = revMask;
149941 pTo->nRow = nOut;
149942 pTo->rCost = rCost;
149943 pTo->rUnsorted = rUnsorted;
149944 pTo->isOrdered = isOrdered;
149945 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
149946 pTo->aLoop[iLoop] = pWLoop;
149947 if( nTo>=mxChoice ){
149948 mxI = 0;
149949 mxCost = aTo[0].rCost;
149950 mxUnsorted = aTo[0].nRow;
149951 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
149952 if( pTo->rCost>mxCost
149953 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
149954 ){
149955 mxCost = pTo->rCost;
149956 mxUnsorted = pTo->rUnsorted;
149957 mxI = jj;
149958 }
149959 }
149960 }
149961 }
149962 }
149963
149964#ifdef WHERETRACE_ENABLED /* >=2 */
149965 if( sqlite3WhereTrace & 0x02 ){
149966 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
149967 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
149968 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
149969 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
149970 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
149971 if( pTo->isOrdered>0 ){
149972 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
149973 }else{
149974 sqlite3DebugPrintf("\n");
149975 }
149976 }
149977 }
149978#endif
149979
149980 /* Swap the roles of aFrom and aTo for the next generation */
149981 pFrom = aTo;
149982 aTo = aFrom;
149983 aFrom = pFrom;
149984 nFrom = nTo;
149985 }
149986
149987 if( nFrom==0 ){
149988 sqlite3ErrorMsg(pParse, "no query solution");
149989 sqlite3DbFreeNN(db, pSpace);
149990 return SQLITE_ERROR;
149991 }
149992
149993 /* Find the lowest cost path. pFrom will be left pointing to that path */
149994 pFrom = aFrom;
149995 for(ii=1; ii<nFrom; ii++){
149996 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
149997 }
149998 assert( pWInfo->nLevel==nLoop );
149999 /* Load the lowest cost path into pWInfo */
150000 for(iLoop=0; iLoop<nLoop; iLoop++){
150001 WhereLevel *pLevel = pWInfo->a + iLoop;
150002 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
150003 pLevel->iFrom = pWLoop->iTab;
150004 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
150005 }
150006 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
150007 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
150008 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
150009 && nRowEst
150010 ){
150011 Bitmask notUsed;
150012 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
150013 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
150014 if( rc==pWInfo->pResultSet->nExpr ){
150015 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
150016 }
150017 }
150018 pWInfo->bOrderedInnerLoop = 0;
150019 if( pWInfo->pOrderBy ){
150020 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
150021 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
150022 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
150023 }
150024 }else{
150025 pWInfo->nOBSat = pFrom->isOrdered;
150026 pWInfo->revMask = pFrom->revLoop;
150027 if( pWInfo->nOBSat<=0 ){
150028 pWInfo->nOBSat = 0;
150029 if( nLoop>0 ){
150030 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
150031 if( (wsFlags & WHERE_ONEROW)==0
150032 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
150033 ){
150034 Bitmask m = 0;
150035 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
150036 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
150037 testcase( wsFlags & WHERE_IPK );
150038 testcase( wsFlags & WHERE_COLUMN_IN );
150039 if( rc==pWInfo->pOrderBy->nExpr ){
150040 pWInfo->bOrderedInnerLoop = 1;
150041 pWInfo->revMask = m;
150042 }
150043 }
150044 }
150045 }else if( nLoop
150046 && pWInfo->nOBSat==1
150047 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
150048 ){
150049 pWInfo->bOrderedInnerLoop = 1;
150050 }
150051 }
150052 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
150053 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
150054 ){
150055 Bitmask revMask = 0;
150056 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
150057 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
150058 );
150059 assert( pWInfo->sorted==0 );
150060 if( nOrder==pWInfo->pOrderBy->nExpr ){
150061 pWInfo->sorted = 1;
150062 pWInfo->revMask = revMask;
150063 }
150064 }
150065 }
150066
150067
150068 pWInfo->nRowOut = pFrom->nRow;
150069
150070 /* Free temporary memory and return success */
150071 sqlite3DbFreeNN(db, pSpace);
150072 return SQLITE_OK;
150073}
150074
150075/*
150076** Most queries use only a single table (they are not joins) and have
150077** simple == constraints against indexed fields. This routine attempts
150078** to plan those simple cases using much less ceremony than the
150079** general-purpose query planner, and thereby yield faster sqlite3_prepare()
150080** times for the common case.
150081**
150082** Return non-zero on success, if this query can be handled by this
150083** no-frills query planner. Return zero if this query needs the
150084** general-purpose query planner.
150085*/
150086static int whereShortCut(WhereLoopBuilder *pBuilder){
150087 WhereInfo *pWInfo;
150088 struct SrcList_item *pItem;
150089 WhereClause *pWC;
150090 WhereTerm *pTerm;
150091 WhereLoop *pLoop;
150092 int iCur;
150093 int j;
150094 Table *pTab;
150095 Index *pIdx;
150096
150097 pWInfo = pBuilder->pWInfo;
150098 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
150099 assert( pWInfo->pTabList->nSrc>=1 );
150100 pItem = pWInfo->pTabList->a;
150101 pTab = pItem->pTab;
150102 if( IsVirtual(pTab) ) return 0;
150103 if( pItem->fg.isIndexedBy ) return 0;
150104 iCur = pItem->iCursor;
150105 pWC = &pWInfo->sWC;
150106 pLoop = pBuilder->pNew;
150107 pLoop->wsFlags = 0;
150108 pLoop->nSkip = 0;
150109 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
150110 if( pTerm ){
150111 testcase( pTerm->eOperator & WO_IS );
150112 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
150113 pLoop->aLTerm[0] = pTerm;
150114 pLoop->nLTerm = 1;
150115 pLoop->u.btree.nEq = 1;
150116 /* TUNING: Cost of a rowid lookup is 10 */
150117 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
150118 }else{
150119 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
150120 int opMask;
150121 assert( pLoop->aLTermSpace==pLoop->aLTerm );
150122 if( !IsUniqueIndex(pIdx)
150123 || pIdx->pPartIdxWhere!=0
150124 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
150125 ) continue;
150126 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
150127 for(j=0; j<pIdx->nKeyCol; j++){
150128 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
150129 if( pTerm==0 ) break;
150130 testcase( pTerm->eOperator & WO_IS );
150131 pLoop->aLTerm[j] = pTerm;
150132 }
150133 if( j!=pIdx->nKeyCol ) continue;
150134 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
150135 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
150136 pLoop->wsFlags |= WHERE_IDX_ONLY;
150137 }
150138 pLoop->nLTerm = j;
150139 pLoop->u.btree.nEq = j;
150140 pLoop->u.btree.pIndex = pIdx;
150141 /* TUNING: Cost of a unique index lookup is 15 */
150142 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
150143 break;
150144 }
150145 }
150146 if( pLoop->wsFlags ){
150147 pLoop->nOut = (LogEst)1;
150148 pWInfo->a[0].pWLoop = pLoop;
150149 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
150150 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
150151 pWInfo->a[0].iTabCur = iCur;
150152 pWInfo->nRowOut = 1;
150153 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
150154 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
150155 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
150156 }
150157#ifdef SQLITE_DEBUG
150158 pLoop->cId = '0';
150159#endif
150160 return 1;
150161 }
150162 return 0;
150163}
150164
150165/*
150166** Helper function for exprIsDeterministic().
150167*/
150168static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
150169 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
150170 pWalker->eCode = 0;
150171 return WRC_Abort;
150172 }
150173 return WRC_Continue;
150174}
150175
150176/*
150177** Return true if the expression contains no non-deterministic SQL
150178** functions. Do not consider non-deterministic SQL functions that are
150179** part of sub-select statements.
150180*/
150181static int exprIsDeterministic(Expr *p){
150182 Walker w;
150183 memset(&w, 0, sizeof(w));
150184 w.eCode = 1;
150185 w.xExprCallback = exprNodeIsDeterministic;
150186 w.xSelectCallback = sqlite3SelectWalkFail;
150187 sqlite3WalkExpr(&w, p);
150188 return w.eCode;
150189}
150190
150191
150192#ifdef WHERETRACE_ENABLED
150193/*
150194** Display all WhereLoops in pWInfo
150195*/
150196static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
150197 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
150198 WhereLoop *p;
150199 int i;
150200 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
150201 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
150202 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
150203 p->cId = zLabel[i%(sizeof(zLabel)-1)];
150204 sqlite3WhereLoopPrint(p, pWC);
150205 }
150206 }
150207}
150208# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
150209#else
150210# define WHERETRACE_ALL_LOOPS(W,C)
150211#endif
150212
150213/*
150214** Generate the beginning of the loop used for WHERE clause processing.
150215** The return value is a pointer to an opaque structure that contains
150216** information needed to terminate the loop. Later, the calling routine
150217** should invoke sqlite3WhereEnd() with the return value of this function
150218** in order to complete the WHERE clause processing.
150219**
150220** If an error occurs, this routine returns NULL.
150221**
150222** The basic idea is to do a nested loop, one loop for each table in
150223** the FROM clause of a select. (INSERT and UPDATE statements are the
150224** same as a SELECT with only a single table in the FROM clause.) For
150225** example, if the SQL is this:
150226**
150227** SELECT * FROM t1, t2, t3 WHERE ...;
150228**
150229** Then the code generated is conceptually like the following:
150230**
150231** foreach row1 in t1 do \ Code generated
150232** foreach row2 in t2 do |-- by sqlite3WhereBegin()
150233** foreach row3 in t3 do /
150234** ...
150235** end \ Code generated
150236** end |-- by sqlite3WhereEnd()
150237** end /
150238**
150239** Note that the loops might not be nested in the order in which they
150240** appear in the FROM clause if a different order is better able to make
150241** use of indices. Note also that when the IN operator appears in
150242** the WHERE clause, it might result in additional nested loops for
150243** scanning through all values on the right-hand side of the IN.
150244**
150245** There are Btree cursors associated with each table. t1 uses cursor
150246** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
150247** And so forth. This routine generates code to open those VDBE cursors
150248** and sqlite3WhereEnd() generates the code to close them.
150249**
150250** The code that sqlite3WhereBegin() generates leaves the cursors named
150251** in pTabList pointing at their appropriate entries. The [...] code
150252** can use OP_Column and OP_Rowid opcodes on these cursors to extract
150253** data from the various tables of the loop.
150254**
150255** If the WHERE clause is empty, the foreach loops must each scan their
150256** entire tables. Thus a three-way join is an O(N^3) operation. But if
150257** the tables have indices and there are terms in the WHERE clause that
150258** refer to those indices, a complete table scan can be avoided and the
150259** code will run much faster. Most of the work of this routine is checking
150260** to see if there are indices that can be used to speed up the loop.
150261**
150262** Terms of the WHERE clause are also used to limit which rows actually
150263** make it to the "..." in the middle of the loop. After each "foreach",
150264** terms of the WHERE clause that use only terms in that loop and outer
150265** loops are evaluated and if false a jump is made around all subsequent
150266** inner loops (or around the "..." if the test occurs within the inner-
150267** most loop)
150268**
150269** OUTER JOINS
150270**
150271** An outer join of tables t1 and t2 is conceptally coded as follows:
150272**
150273** foreach row1 in t1 do
150274** flag = 0
150275** foreach row2 in t2 do
150276** start:
150277** ...
150278** flag = 1
150279** end
150280** if flag==0 then
150281** move the row2 cursor to a null row
150282** goto start
150283** fi
150284** end
150285**
150286** ORDER BY CLAUSE PROCESSING
150287**
150288** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
150289** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
150290** if there is one. If there is no ORDER BY clause or if this routine
150291** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
150292**
150293** The iIdxCur parameter is the cursor number of an index. If
150294** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
150295** to use for OR clause processing. The WHERE clause should use this
150296** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
150297** the first cursor in an array of cursors for all indices. iIdxCur should
150298** be used to compute the appropriate cursor depending on which index is
150299** used.
150300*/
150301SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
150302 Parse *pParse, /* The parser context */
150303 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
150304 Expr *pWhere, /* The WHERE clause */
150305 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
150306 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
150307 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
150308 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
150309 ** If WHERE_USE_LIMIT, then the limit amount */
150310){
150311 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
150312 int nTabList; /* Number of elements in pTabList */
150313 WhereInfo *pWInfo; /* Will become the return value of this function */
150314 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
150315 Bitmask notReady; /* Cursors that are not yet positioned */
150316 WhereLoopBuilder sWLB; /* The WhereLoop builder */
150317 WhereMaskSet *pMaskSet; /* The expression mask set */
150318 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
150319 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
150320 int ii; /* Loop counter */
150321 sqlite3 *db; /* Database connection */
150322 int rc; /* Return code */
150323 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
150324
150325 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
150326 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
150327 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
150328 ));
150329
150330 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
150331 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
150332 || (wctrlFlags & WHERE_USE_LIMIT)==0 );
150333
150334 /* Variable initialization */
150335 db = pParse->db;
150336 memset(&sWLB, 0, sizeof(sWLB));
150337
150338 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
150339 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
150340 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
150341 sWLB.pOrderBy = pOrderBy;
150342
150343 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
150344 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
150345 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
150346 wctrlFlags &= ~WHERE_WANT_DISTINCT;
150347 }
150348
150349 /* The number of tables in the FROM clause is limited by the number of
150350 ** bits in a Bitmask
150351 */
150352 testcase( pTabList->nSrc==BMS );
150353 if( pTabList->nSrc>BMS ){
150354 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
150355 return 0;
150356 }
150357
150358 /* This function normally generates a nested loop for all tables in
150359 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
150360 ** only generate code for the first table in pTabList and assume that
150361 ** any cursors associated with subsequent tables are uninitialized.
150362 */
150363 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
150364
150365 /* Allocate and initialize the WhereInfo structure that will become the
150366 ** return value. A single allocation is used to store the WhereInfo
150367 ** struct, the contents of WhereInfo.a[], the WhereClause structure
150368 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
150369 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
150370 ** some architectures. Hence the ROUND8() below.
150371 */
150372 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
150373 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
150374 if( db->mallocFailed ){
150375 sqlite3DbFree(db, pWInfo);
150376 pWInfo = 0;
150377 goto whereBeginError;
150378 }
150379 pWInfo->pParse = pParse;
150380 pWInfo->pTabList = pTabList;
150381 pWInfo->pOrderBy = pOrderBy;
150382 pWInfo->pWhere = pWhere;
150383 pWInfo->pResultSet = pResultSet;
150384 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
150385 pWInfo->nLevel = nTabList;
150386 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
150387 pWInfo->wctrlFlags = wctrlFlags;
150388 pWInfo->iLimit = iAuxArg;
150389 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
150390 memset(&pWInfo->nOBSat, 0,
150391 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
150392 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
150393 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
150394 pMaskSet = &pWInfo->sMaskSet;
150395 sWLB.pWInfo = pWInfo;
150396 sWLB.pWC = &pWInfo->sWC;
150397 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
150398 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
150399 whereLoopInit(sWLB.pNew);
150400#ifdef SQLITE_DEBUG
150401 sWLB.pNew->cId = '*';
150402#endif
150403
150404 /* Split the WHERE clause into separate subexpressions where each
150405 ** subexpression is separated by an AND operator.
150406 */
150407 initMaskSet(pMaskSet);
150408 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
150409 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
150410
150411 /* Special case: No FROM clause
150412 */
150413 if( nTabList==0 ){
150414 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
150415 if( wctrlFlags & WHERE_WANT_DISTINCT ){
150416 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
150417 }
150418 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
150419 }else{
150420 /* Assign a bit from the bitmask to every term in the FROM clause.
150421 **
150422 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
150423 **
150424 ** The rule of the previous sentence ensures thta if X is the bitmask for
150425 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
150426 ** Knowing the bitmask for all tables to the left of a left join is
150427 ** important. Ticket #3015.
150428 **
150429 ** Note that bitmasks are created for all pTabList->nSrc tables in
150430 ** pTabList, not just the first nTabList tables. nTabList is normally
150431 ** equal to pTabList->nSrc but might be shortened to 1 if the
150432 ** WHERE_OR_SUBCLAUSE flag is set.
150433 */
150434 ii = 0;
150435 do{
150436 createMask(pMaskSet, pTabList->a[ii].iCursor);
150437 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
150438 }while( (++ii)<pTabList->nSrc );
150439 #ifdef SQLITE_DEBUG
150440 {
150441 Bitmask mx = 0;
150442 for(ii=0; ii<pTabList->nSrc; ii++){
150443 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
150444 assert( m>=mx );
150445 mx = m;
150446 }
150447 }
150448 #endif
150449 }
150450
150451 /* Analyze all of the subexpressions. */
150452 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
150453 if( db->mallocFailed ) goto whereBeginError;
150454
150455 /* Special case: WHERE terms that do not refer to any tables in the join
150456 ** (constant expressions). Evaluate each such term, and jump over all the
150457 ** generated code if the result is not true.
150458 **
150459 ** Do not do this if the expression contains non-deterministic functions
150460 ** that are not within a sub-select. This is not strictly required, but
150461 ** preserves SQLite's legacy behaviour in the following two cases:
150462 **
150463 ** FROM ... WHERE random()>0; -- eval random() once per row
150464 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
150465 */
150466 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
150467 WhereTerm *pT = &sWLB.pWC->a[ii];
150468 if( pT->wtFlags & TERM_VIRTUAL ) continue;
150469 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
150470 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
150471 pT->wtFlags |= TERM_CODED;
150472 }
150473 }
150474
150475 if( wctrlFlags & WHERE_WANT_DISTINCT ){
150476 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
150477 /* The DISTINCT marking is pointless. Ignore it. */
150478 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
150479 }else if( pOrderBy==0 ){
150480 /* Try to ORDER BY the result set to make distinct processing easier */
150481 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
150482 pWInfo->pOrderBy = pResultSet;
150483 }
150484 }
150485
150486 /* Construct the WhereLoop objects */
150487#if defined(WHERETRACE_ENABLED)
150488 if( sqlite3WhereTrace & 0xffff ){
150489 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
150490 if( wctrlFlags & WHERE_USE_LIMIT ){
150491 sqlite3DebugPrintf(", limit: %d", iAuxArg);
150492 }
150493 sqlite3DebugPrintf(")\n");
150494 if( sqlite3WhereTrace & 0x100 ){
150495 Select sSelect;
150496 memset(&sSelect, 0, sizeof(sSelect));
150497 sSelect.selFlags = SF_WhereBegin;
150498 sSelect.pSrc = pTabList;
150499 sSelect.pWhere = pWhere;
150500 sSelect.pOrderBy = pOrderBy;
150501 sSelect.pEList = pResultSet;
150502 sqlite3TreeViewSelect(0, &sSelect, 0);
150503 }
150504 }
150505 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
150506 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
150507 sqlite3WhereClausePrint(sWLB.pWC);
150508 }
150509#endif
150510
150511 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
150512 rc = whereLoopAddAll(&sWLB);
150513 if( rc ) goto whereBeginError;
150514
150515#ifdef SQLITE_ENABLE_STAT4
150516 /* If one or more WhereTerm.truthProb values were used in estimating
150517 ** loop parameters, but then those truthProb values were subsequently
150518 ** changed based on STAT4 information while computing subsequent loops,
150519 ** then we need to rerun the whole loop building process so that all
150520 ** loops will be built using the revised truthProb values. */
150521 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
150522 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
150523 WHERETRACE(0xffff,
150524 ("**** Redo all loop computations due to"
150525 " TERM_HIGHTRUTH changes ****\n"));
150526 while( pWInfo->pLoops ){
150527 WhereLoop *p = pWInfo->pLoops;
150528 pWInfo->pLoops = p->pNextLoop;
150529 whereLoopDelete(db, p);
150530 }
150531 rc = whereLoopAddAll(&sWLB);
150532 if( rc ) goto whereBeginError;
150533 }
150534#endif
150535 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
150536
150537 wherePathSolver(pWInfo, 0);
150538 if( db->mallocFailed ) goto whereBeginError;
150539 if( pWInfo->pOrderBy ){
150540 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
150541 if( db->mallocFailed ) goto whereBeginError;
150542 }
150543 }
150544 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
150545 pWInfo->revMask = ALLBITS;
150546 }
150547 if( pParse->nErr || NEVER(db->mallocFailed) ){
150548 goto whereBeginError;
150549 }
150550#ifdef WHERETRACE_ENABLED
150551 if( sqlite3WhereTrace ){
150552 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
150553 if( pWInfo->nOBSat>0 ){
150554 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
150555 }
150556 switch( pWInfo->eDistinct ){
150557 case WHERE_DISTINCT_UNIQUE: {
150558 sqlite3DebugPrintf(" DISTINCT=unique");
150559 break;
150560 }
150561 case WHERE_DISTINCT_ORDERED: {
150562 sqlite3DebugPrintf(" DISTINCT=ordered");
150563 break;
150564 }
150565 case WHERE_DISTINCT_UNORDERED: {
150566 sqlite3DebugPrintf(" DISTINCT=unordered");
150567 break;
150568 }
150569 }
150570 sqlite3DebugPrintf("\n");
150571 for(ii=0; ii<pWInfo->nLevel; ii++){
150572 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
150573 }
150574 }
150575#endif
150576
150577 /* Attempt to omit tables from the join that do not affect the result.
150578 ** For a table to not affect the result, the following must be true:
150579 **
150580 ** 1) The query must not be an aggregate.
150581 ** 2) The table must be the RHS of a LEFT JOIN.
150582 ** 3) Either the query must be DISTINCT, or else the ON or USING clause
150583 ** must contain a constraint that limits the scan of the table to
150584 ** at most a single row.
150585 ** 4) The table must not be referenced by any part of the query apart
150586 ** from its own USING or ON clause.
150587 **
150588 ** For example, given:
150589 **
150590 ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
150591 ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
150592 ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
150593 **
150594 ** then table t2 can be omitted from the following:
150595 **
150596 ** SELECT v1, v3 FROM t1
150597 ** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
150598 ** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
150599 **
150600 ** or from:
150601 **
150602 ** SELECT DISTINCT v1, v3 FROM t1
150603 ** LEFT JOIN t2
150604 ** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
150605 */
150606 notReady = ~(Bitmask)0;
150607 if( pWInfo->nLevel>=2
150608 && pResultSet!=0 /* guarantees condition (1) above */
150609 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
150610 ){
150611 int i;
150612 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
150613 if( sWLB.pOrderBy ){
150614 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
150615 }
150616 for(i=pWInfo->nLevel-1; i>=1; i--){
150617 WhereTerm *pTerm, *pEnd;
150618 struct SrcList_item *pItem;
150619 pLoop = pWInfo->a[i].pWLoop;
150620 pItem = &pWInfo->pTabList->a[pLoop->iTab];
150621 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
150622 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
150623 && (pLoop->wsFlags & WHERE_ONEROW)==0
150624 ){
150625 continue;
150626 }
150627 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
150628 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
150629 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
150630 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
150631 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
150632 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
150633 ){
150634 break;
150635 }
150636 }
150637 }
150638 if( pTerm<pEnd ) continue;
150639 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
150640 notReady &= ~pLoop->maskSelf;
150641 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
150642 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
150643 pTerm->wtFlags |= TERM_CODED;
150644 }
150645 }
150646 if( i!=pWInfo->nLevel-1 ){
150647 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
150648 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
150649 }
150650 pWInfo->nLevel--;
150651 nTabList--;
150652 }
150653 }
150654#if defined(WHERETRACE_ENABLED)
150655 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
150656 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
150657 sqlite3WhereClausePrint(sWLB.pWC);
150658 }
150659 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
150660#endif
150661 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
150662
150663 /* If the caller is an UPDATE or DELETE statement that is requesting
150664 ** to use a one-pass algorithm, determine if this is appropriate.
150665 **
150666 ** A one-pass approach can be used if the caller has requested one
150667 ** and either (a) the scan visits at most one row or (b) each
150668 ** of the following are true:
150669 **
150670 ** * the caller has indicated that a one-pass approach can be used
150671 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
150672 ** * the table is not a virtual table, and
150673 ** * either the scan does not use the OR optimization or the caller
150674 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
150675 ** for DELETE).
150676 **
150677 ** The last qualification is because an UPDATE statement uses
150678 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
150679 ** use a one-pass approach, and this is not set accurately for scans
150680 ** that use the OR optimization.
150681 */
150682 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
150683 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
150684 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
150685 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
150686 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
150687 if( bOnerow || (
150688 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
150689 && !IsVirtual(pTabList->a[0].pTab)
150690 && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
150691 )){
150692 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
150693 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
150694 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
150695 bFordelete = OPFLAG_FORDELETE;
150696 }
150697 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
150698 }
150699 }
150700 }
150701
150702 /* Open all tables in the pTabList and any indices selected for
150703 ** searching those tables.
150704 */
150705 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
150706 Table *pTab; /* Table to open */
150707 int iDb; /* Index of database containing table/index */
150708 struct SrcList_item *pTabItem;
150709
150710 pTabItem = &pTabList->a[pLevel->iFrom];
150711 pTab = pTabItem->pTab;
150712 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
150713 pLoop = pLevel->pWLoop;
150714 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
150715 /* Do nothing */
150716 }else
150717#ifndef SQLITE_OMIT_VIRTUALTABLE
150718 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
150719 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
150720 int iCur = pTabItem->iCursor;
150721 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
150722 }else if( IsVirtual(pTab) ){
150723 /* noop */
150724 }else
150725#endif
150726 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
150727 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
150728 int op = OP_OpenRead;
150729 if( pWInfo->eOnePass!=ONEPASS_OFF ){
150730 op = OP_OpenWrite;
150731 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
150732 };
150733 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
150734 assert( pTabItem->iCursor==pLevel->iTabCur );
150735 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
150736 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
150737 if( pWInfo->eOnePass==ONEPASS_OFF
150738 && pTab->nCol<BMS
150739 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
150740 ){
150741 /* If we know that only a prefix of the record will be used,
150742 ** it is advantageous to reduce the "column count" field in
150743 ** the P4 operand of the OP_OpenRead/Write opcode. */
150744 Bitmask b = pTabItem->colUsed;
150745 int n = 0;
150746 for(; b; b=b>>1, n++){}
150747 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
150748 assert( n<=pTab->nCol );
150749 }
150750#ifdef SQLITE_ENABLE_CURSOR_HINTS
150751 if( pLoop->u.btree.pIndex!=0 ){
150752 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
150753 }else
150754#endif
150755 {
150756 sqlite3VdbeChangeP5(v, bFordelete);
150757 }
150758#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
150759 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
150760 (const u8*)&pTabItem->colUsed, P4_INT64);
150761#endif
150762 }else{
150763 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
150764 }
150765 if( pLoop->wsFlags & WHERE_INDEXED ){
150766 Index *pIx = pLoop->u.btree.pIndex;
150767 int iIndexCur;
150768 int op = OP_OpenRead;
150769 /* iAuxArg is always set to a positive value if ONEPASS is possible */
150770 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
150771 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
150772 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
150773 ){
150774 /* This is one term of an OR-optimization using the PRIMARY KEY of a
150775 ** WITHOUT ROWID table. No need for a separate index */
150776 iIndexCur = pLevel->iTabCur;
150777 op = 0;
150778 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
150779 Index *pJ = pTabItem->pTab->pIndex;
150780 iIndexCur = iAuxArg;
150781 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
150782 while( ALWAYS(pJ) && pJ!=pIx ){
150783 iIndexCur++;
150784 pJ = pJ->pNext;
150785 }
150786 op = OP_OpenWrite;
150787 pWInfo->aiCurOnePass[1] = iIndexCur;
150788 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
150789 iIndexCur = iAuxArg;
150790 op = OP_ReopenIdx;
150791 }else{
150792 iIndexCur = pParse->nTab++;
150793 }
150794 pLevel->iIdxCur = iIndexCur;
150795 assert( pIx->pSchema==pTab->pSchema );
150796 assert( iIndexCur>=0 );
150797 if( op ){
150798 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
150799 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
150800 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
150801 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
150802 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
150803 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
150804 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
150805 ){
150806 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ);
150807 }
150808 VdbeComment((v, "%s", pIx->zName));
150809#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
150810 {
150811 u64 colUsed = 0;
150812 int ii, jj;
150813 for(ii=0; ii<pIx->nColumn; ii++){
150814 jj = pIx->aiColumn[ii];
150815 if( jj<0 ) continue;
150816 if( jj>63 ) jj = 63;
150817 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
150818 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
150819 }
150820 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
150821 (u8*)&colUsed, P4_INT64);
150822 }
150823#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
150824 }
150825 }
150826 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
150827 }
150828 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
150829 if( db->mallocFailed ) goto whereBeginError;
150830
150831 /* Generate the code to do the search. Each iteration of the for
150832 ** loop below generates code for a single nested loop of the VM
150833 ** program.
150834 */
150835 for(ii=0; ii<nTabList; ii++){
150836 int addrExplain;
150837 int wsFlags;
150838 pLevel = &pWInfo->a[ii];
150839 wsFlags = pLevel->pWLoop->wsFlags;
150840#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
150841 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
150842 constructAutomaticIndex(pParse, &pWInfo->sWC,
150843 &pTabList->a[pLevel->iFrom], notReady, pLevel);
150844 if( db->mallocFailed ) goto whereBeginError;
150845 }
150846#endif
150847 addrExplain = sqlite3WhereExplainOneScan(
150848 pParse, pTabList, pLevel, wctrlFlags
150849 );
150850 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
150851 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
150852 pWInfo->iContinue = pLevel->addrCont;
150853 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
150854 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
150855 }
150856 }
150857
150858 /* Done. */
150859 VdbeModuleComment((v, "Begin WHERE-core"));
150860 return pWInfo;
150861
150862 /* Jump here if malloc fails */
150863whereBeginError:
150864 if( pWInfo ){
150865 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
150866 whereInfoFree(db, pWInfo);
150867 }
150868 return 0;
150869}
150870
150871/*
150872** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
150873** index rather than the main table. In SQLITE_DEBUG mode, we want
150874** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
150875** does that.
150876*/
150877#ifndef SQLITE_DEBUG
150878# define OpcodeRewriteTrace(D,K,P) /* no-op */
150879#else
150880# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
150881 static void sqlite3WhereOpcodeRewriteTrace(
150882 sqlite3 *db,
150883 int pc,
150884 VdbeOp *pOp
150885 ){
150886 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
150887 sqlite3VdbePrintOp(0, pc, pOp);
150888 }
150889#endif
150890
150891/*
150892** Generate the end of the WHERE loop. See comments on
150893** sqlite3WhereBegin() for additional information.
150894*/
150895SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
150896 Parse *pParse = pWInfo->pParse;
150897 Vdbe *v = pParse->pVdbe;
150898 int i;
150899 WhereLevel *pLevel;
150900 WhereLoop *pLoop;
150901 SrcList *pTabList = pWInfo->pTabList;
150902 sqlite3 *db = pParse->db;
150903
150904 /* Generate loop termination code.
150905 */
150906 VdbeModuleComment((v, "End WHERE-core"));
150907 for(i=pWInfo->nLevel-1; i>=0; i--){
150908 int addr;
150909 pLevel = &pWInfo->a[i];
150910 pLoop = pLevel->pWLoop;
150911 if( pLevel->op!=OP_Noop ){
150912#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
150913 int addrSeek = 0;
150914 Index *pIdx;
150915 int n;
150916 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
150917 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
150918 && (pLoop->wsFlags & WHERE_INDEXED)!=0
150919 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
150920 && (n = pLoop->u.btree.nDistinctCol)>0
150921 && pIdx->aiRowLogEst[n]>=36
150922 ){
150923 int r1 = pParse->nMem+1;
150924 int j, op;
150925 for(j=0; j<n; j++){
150926 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
150927 }
150928 pParse->nMem += n+1;
150929 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
150930 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
150931 VdbeCoverageIf(v, op==OP_SeekLT);
150932 VdbeCoverageIf(v, op==OP_SeekGT);
150933 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
150934 }
150935#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
150936 /* The common case: Advance to the next row */
150937 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
150938 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
150939 sqlite3VdbeChangeP5(v, pLevel->p5);
150940 VdbeCoverage(v);
150941 VdbeCoverageIf(v, pLevel->op==OP_Next);
150942 VdbeCoverageIf(v, pLevel->op==OP_Prev);
150943 VdbeCoverageIf(v, pLevel->op==OP_VNext);
150944 if( pLevel->regBignull ){
150945 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
150946 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
150947 VdbeCoverage(v);
150948 }
150949#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
150950 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
150951#endif
150952 }else{
150953 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
150954 }
150955 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
150956 struct InLoop *pIn;
150957 int j;
150958 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
150959 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
150960 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
150961 if( pIn->eEndLoopOp!=OP_Noop ){
150962 if( pIn->nPrefix ){
150963 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
150964 if( pLevel->iLeftJoin ){
150965 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
150966 ** opened yet. This occurs for WHERE clauses such as
150967 ** "a = ? AND b IN (...)", where the index is on (a, b). If
150968 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
150969 ** never have been coded, but the body of the loop run to
150970 ** return the null-row. So, if the cursor is not open yet,
150971 ** jump over the OP_Next or OP_Prev instruction about to
150972 ** be coded. */
150973 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
150974 sqlite3VdbeCurrentAddr(v) + 2 +
150975 ((pLoop->wsFlags & WHERE_VIRTUALTABLE)==0)
150976 );
150977 VdbeCoverage(v);
150978 }
150979 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
150980 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
150981 sqlite3VdbeCurrentAddr(v)+2,
150982 pIn->iBase, pIn->nPrefix);
150983 VdbeCoverage(v);
150984 }
150985 }
150986 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
150987 VdbeCoverage(v);
150988 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
150989 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
150990 }
150991 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
150992 }
150993 }
150994 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
150995 if( pLevel->addrSkip ){
150996 sqlite3VdbeGoto(v, pLevel->addrSkip);
150997 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
150998 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
150999 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
151000 }
151001#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
151002 if( pLevel->addrLikeRep ){
151003 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
151004 pLevel->addrLikeRep);
151005 VdbeCoverage(v);
151006 }
151007#endif
151008 if( pLevel->iLeftJoin ){
151009 int ws = pLoop->wsFlags;
151010 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
151011 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
151012 if( (ws & WHERE_IDX_ONLY)==0 ){
151013 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
151014 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
151015 }
151016 if( (ws & WHERE_INDEXED)
151017 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
151018 ){
151019 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
151020 }
151021 if( pLevel->op==OP_Return ){
151022 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
151023 }else{
151024 sqlite3VdbeGoto(v, pLevel->addrFirst);
151025 }
151026 sqlite3VdbeJumpHere(v, addr);
151027 }
151028 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
151029 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
151030 }
151031
151032 /* The "break" point is here, just past the end of the outer loop.
151033 ** Set it.
151034 */
151035 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
151036
151037 assert( pWInfo->nLevel<=pTabList->nSrc );
151038 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
151039 int k, last;
151040 VdbeOp *pOp;
151041 Index *pIdx = 0;
151042 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
151043 Table *pTab = pTabItem->pTab;
151044 assert( pTab!=0 );
151045 pLoop = pLevel->pWLoop;
151046
151047 /* For a co-routine, change all OP_Column references to the table of
151048 ** the co-routine into OP_Copy of result contained in a register.
151049 ** OP_Rowid becomes OP_Null.
151050 */
151051 if( pTabItem->fg.viaCoroutine ){
151052 testcase( pParse->db->mallocFailed );
151053 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
151054 pTabItem->regResult, 0);
151055 continue;
151056 }
151057
151058#ifdef SQLITE_ENABLE_EARLY_CURSOR_CLOSE
151059 /* Close all of the cursors that were opened by sqlite3WhereBegin.
151060 ** Except, do not close cursors that will be reused by the OR optimization
151061 ** (WHERE_OR_SUBCLAUSE). And do not close the OP_OpenWrite cursors
151062 ** created for the ONEPASS optimization.
151063 */
151064 if( (pTab->tabFlags & TF_Ephemeral)==0
151065 && pTab->pSelect==0
151066 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
151067 ){
151068 int ws = pLoop->wsFlags;
151069 if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
151070 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
151071 }
151072 if( (ws & WHERE_INDEXED)!=0
151073 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
151074 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
151075 ){
151076 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
151077 }
151078 }
151079#endif
151080
151081 /* If this scan uses an index, make VDBE code substitutions to read data
151082 ** from the index instead of from the table where possible. In some cases
151083 ** this optimization prevents the table from ever being read, which can
151084 ** yield a significant performance boost.
151085 **
151086 ** Calls to the code generator in between sqlite3WhereBegin and
151087 ** sqlite3WhereEnd will have created code that references the table
151088 ** directly. This loop scans all that code looking for opcodes
151089 ** that reference the table and converts them into opcodes that
151090 ** reference the index.
151091 */
151092 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
151093 pIdx = pLoop->u.btree.pIndex;
151094 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
151095 pIdx = pLevel->u.pCovidx;
151096 }
151097 if( pIdx
151098 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
151099 && !db->mallocFailed
151100 ){
151101 last = sqlite3VdbeCurrentAddr(v);
151102 k = pLevel->addrBody;
151103#ifdef SQLITE_DEBUG
151104 if( db->flags & SQLITE_VdbeAddopTrace ){
151105 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
151106 }
151107#endif
151108 pOp = sqlite3VdbeGetOp(v, k);
151109 for(; k<last; k++, pOp++){
151110 if( pOp->p1!=pLevel->iTabCur ) continue;
151111 if( pOp->opcode==OP_Column
151112#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
151113 || pOp->opcode==OP_Offset
151114#endif
151115 ){
151116 int x = pOp->p2;
151117 assert( pIdx->pTable==pTab );
151118 if( !HasRowid(pTab) ){
151119 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
151120 x = pPk->aiColumn[x];
151121 assert( x>=0 );
151122 }else{
151123 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
151124 x = sqlite3StorageColumnToTable(pTab,x);
151125 }
151126 x = sqlite3TableColumnToIndex(pIdx, x);
151127 if( x>=0 ){
151128 pOp->p2 = x;
151129 pOp->p1 = pLevel->iIdxCur;
151130 OpcodeRewriteTrace(db, k, pOp);
151131 }
151132 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
151133 || pWInfo->eOnePass );
151134 }else if( pOp->opcode==OP_Rowid ){
151135 pOp->p1 = pLevel->iIdxCur;
151136 pOp->opcode = OP_IdxRowid;
151137 OpcodeRewriteTrace(db, k, pOp);
151138 }else if( pOp->opcode==OP_IfNullRow ){
151139 pOp->p1 = pLevel->iIdxCur;
151140 OpcodeRewriteTrace(db, k, pOp);
151141 }
151142 }
151143#ifdef SQLITE_DEBUG
151144 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
151145#endif
151146 }
151147 }
151148
151149 /* Undo all Expr node modifications */
151150 while( pWInfo->pExprMods ){
151151 WhereExprMod *p = pWInfo->pExprMods;
151152 pWInfo->pExprMods = p->pNext;
151153 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
151154 sqlite3DbFree(db, p);
151155 }
151156
151157 /* Final cleanup
151158 */
151159 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
151160 whereInfoFree(db, pWInfo);
151161 return;
151162}
151163
151164/************** End of where.c ***********************************************/
151165/************** Begin file window.c ******************************************/
151166/*
151167** 2018 May 08
151168**
151169** The author disclaims copyright to this source code. In place of
151170** a legal notice, here is a blessing:
151171**
151172** May you do good and not evil.
151173** May you find forgiveness for yourself and forgive others.
151174** May you share freely, never taking more than you give.
151175**
151176*************************************************************************
151177*/
151178/* #include "sqliteInt.h" */
151179
151180#ifndef SQLITE_OMIT_WINDOWFUNC
151181
151182/*
151183** SELECT REWRITING
151184**
151185** Any SELECT statement that contains one or more window functions in
151186** either the select list or ORDER BY clause (the only two places window
151187** functions may be used) is transformed by function sqlite3WindowRewrite()
151188** in order to support window function processing. For example, with the
151189** schema:
151190**
151191** CREATE TABLE t1(a, b, c, d, e, f, g);
151192**
151193** the statement:
151194**
151195** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
151196**
151197** is transformed to:
151198**
151199** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
151200** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
151201** ) ORDER BY e;
151202**
151203** The flattening optimization is disabled when processing this transformed
151204** SELECT statement. This allows the implementation of the window function
151205** (in this case max()) to process rows sorted in order of (c, d), which
151206** makes things easier for obvious reasons. More generally:
151207**
151208** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
151209** the sub-query.
151210**
151211** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
151212**
151213** * Terminals from each of the expression trees that make up the
151214** select-list and ORDER BY expressions in the parent query are
151215** selected by the sub-query. For the purposes of the transformation,
151216** terminals are column references and aggregate functions.
151217**
151218** If there is more than one window function in the SELECT that uses
151219** the same window declaration (the OVER bit), then a single scan may
151220** be used to process more than one window function. For example:
151221**
151222** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
151223** min(e) OVER (PARTITION BY c ORDER BY d)
151224** FROM t1;
151225**
151226** is transformed in the same way as the example above. However:
151227**
151228** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
151229** min(e) OVER (PARTITION BY a ORDER BY b)
151230** FROM t1;
151231**
151232** Must be transformed to:
151233**
151234** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
151235** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
151236** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
151237** ) ORDER BY c, d
151238** ) ORDER BY e;
151239**
151240** so that both min() and max() may process rows in the order defined by
151241** their respective window declarations.
151242**
151243** INTERFACE WITH SELECT.C
151244**
151245** When processing the rewritten SELECT statement, code in select.c calls
151246** sqlite3WhereBegin() to begin iterating through the results of the
151247** sub-query, which is always implemented as a co-routine. It then calls
151248** sqlite3WindowCodeStep() to process rows and finish the scan by calling
151249** sqlite3WhereEnd().
151250**
151251** sqlite3WindowCodeStep() generates VM code so that, for each row returned
151252** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
151253** When the sub-routine is invoked:
151254**
151255** * The results of all window-functions for the row are stored
151256** in the associated Window.regResult registers.
151257**
151258** * The required terminal values are stored in the current row of
151259** temp table Window.iEphCsr.
151260**
151261** In some cases, depending on the window frame and the specific window
151262** functions invoked, sqlite3WindowCodeStep() caches each entire partition
151263** in a temp table before returning any rows. In other cases it does not.
151264** This detail is encapsulated within this file, the code generated by
151265** select.c is the same in either case.
151266**
151267** BUILT-IN WINDOW FUNCTIONS
151268**
151269** This implementation features the following built-in window functions:
151270**
151271** row_number()
151272** rank()
151273** dense_rank()
151274** percent_rank()
151275** cume_dist()
151276** ntile(N)
151277** lead(expr [, offset [, default]])
151278** lag(expr [, offset [, default]])
151279** first_value(expr)
151280** last_value(expr)
151281** nth_value(expr, N)
151282**
151283** These are the same built-in window functions supported by Postgres.
151284** Although the behaviour of aggregate window functions (functions that
151285** can be used as either aggregates or window funtions) allows them to
151286** be implemented using an API, built-in window functions are much more
151287** esoteric. Additionally, some window functions (e.g. nth_value())
151288** may only be implemented by caching the entire partition in memory.
151289** As such, some built-in window functions use the same API as aggregate
151290** window functions and some are implemented directly using VDBE
151291** instructions. Additionally, for those functions that use the API, the
151292** window frame is sometimes modified before the SELECT statement is
151293** rewritten. For example, regardless of the specified window frame, the
151294** row_number() function always uses:
151295**
151296** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
151297**
151298** See sqlite3WindowUpdate() for details.
151299**
151300** As well as some of the built-in window functions, aggregate window
151301** functions min() and max() are implemented using VDBE instructions if
151302** the start of the window frame is declared as anything other than
151303** UNBOUNDED PRECEDING.
151304*/
151305
151306/*
151307** Implementation of built-in window function row_number(). Assumes that the
151308** window frame has been coerced to:
151309**
151310** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
151311*/
151312static void row_numberStepFunc(
151313 sqlite3_context *pCtx,
151314 int nArg,
151315 sqlite3_value **apArg
151316){
151317 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151318 if( p ) (*p)++;
151319 UNUSED_PARAMETER(nArg);
151320 UNUSED_PARAMETER(apArg);
151321}
151322static void row_numberValueFunc(sqlite3_context *pCtx){
151323 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151324 sqlite3_result_int64(pCtx, (p ? *p : 0));
151325}
151326
151327/*
151328** Context object type used by rank(), dense_rank(), percent_rank() and
151329** cume_dist().
151330*/
151331struct CallCount {
151332 i64 nValue;
151333 i64 nStep;
151334 i64 nTotal;
151335};
151336
151337/*
151338** Implementation of built-in window function dense_rank(). Assumes that
151339** the window frame has been set to:
151340**
151341** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
151342*/
151343static void dense_rankStepFunc(
151344 sqlite3_context *pCtx,
151345 int nArg,
151346 sqlite3_value **apArg
151347){
151348 struct CallCount *p;
151349 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151350 if( p ) p->nStep = 1;
151351 UNUSED_PARAMETER(nArg);
151352 UNUSED_PARAMETER(apArg);
151353}
151354static void dense_rankValueFunc(sqlite3_context *pCtx){
151355 struct CallCount *p;
151356 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151357 if( p ){
151358 if( p->nStep ){
151359 p->nValue++;
151360 p->nStep = 0;
151361 }
151362 sqlite3_result_int64(pCtx, p->nValue);
151363 }
151364}
151365
151366/*
151367** Implementation of built-in window function nth_value(). This
151368** implementation is used in "slow mode" only - when the EXCLUDE clause
151369** is not set to the default value "NO OTHERS".
151370*/
151371struct NthValueCtx {
151372 i64 nStep;
151373 sqlite3_value *pValue;
151374};
151375static void nth_valueStepFunc(
151376 sqlite3_context *pCtx,
151377 int nArg,
151378 sqlite3_value **apArg
151379){
151380 struct NthValueCtx *p;
151381 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151382 if( p ){
151383 i64 iVal;
151384 switch( sqlite3_value_numeric_type(apArg[1]) ){
151385 case SQLITE_INTEGER:
151386 iVal = sqlite3_value_int64(apArg[1]);
151387 break;
151388 case SQLITE_FLOAT: {
151389 double fVal = sqlite3_value_double(apArg[1]);
151390 if( ((i64)fVal)!=fVal ) goto error_out;
151391 iVal = (i64)fVal;
151392 break;
151393 }
151394 default:
151395 goto error_out;
151396 }
151397 if( iVal<=0 ) goto error_out;
151398
151399 p->nStep++;
151400 if( iVal==p->nStep ){
151401 p->pValue = sqlite3_value_dup(apArg[0]);
151402 if( !p->pValue ){
151403 sqlite3_result_error_nomem(pCtx);
151404 }
151405 }
151406 }
151407 UNUSED_PARAMETER(nArg);
151408 UNUSED_PARAMETER(apArg);
151409 return;
151410
151411 error_out:
151412 sqlite3_result_error(
151413 pCtx, "second argument to nth_value must be a positive integer", -1
151414 );
151415}
151416static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
151417 struct NthValueCtx *p;
151418 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
151419 if( p && p->pValue ){
151420 sqlite3_result_value(pCtx, p->pValue);
151421 sqlite3_value_free(p->pValue);
151422 p->pValue = 0;
151423 }
151424}
151425#define nth_valueInvFunc noopStepFunc
151426#define nth_valueValueFunc noopValueFunc
151427
151428static void first_valueStepFunc(
151429 sqlite3_context *pCtx,
151430 int nArg,
151431 sqlite3_value **apArg
151432){
151433 struct NthValueCtx *p;
151434 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151435 if( p && p->pValue==0 ){
151436 p->pValue = sqlite3_value_dup(apArg[0]);
151437 if( !p->pValue ){
151438 sqlite3_result_error_nomem(pCtx);
151439 }
151440 }
151441 UNUSED_PARAMETER(nArg);
151442 UNUSED_PARAMETER(apArg);
151443}
151444static void first_valueFinalizeFunc(sqlite3_context *pCtx){
151445 struct NthValueCtx *p;
151446 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151447 if( p && p->pValue ){
151448 sqlite3_result_value(pCtx, p->pValue);
151449 sqlite3_value_free(p->pValue);
151450 p->pValue = 0;
151451 }
151452}
151453#define first_valueInvFunc noopStepFunc
151454#define first_valueValueFunc noopValueFunc
151455
151456/*
151457** Implementation of built-in window function rank(). Assumes that
151458** the window frame has been set to:
151459**
151460** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
151461*/
151462static void rankStepFunc(
151463 sqlite3_context *pCtx,
151464 int nArg,
151465 sqlite3_value **apArg
151466){
151467 struct CallCount *p;
151468 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151469 if( p ){
151470 p->nStep++;
151471 if( p->nValue==0 ){
151472 p->nValue = p->nStep;
151473 }
151474 }
151475 UNUSED_PARAMETER(nArg);
151476 UNUSED_PARAMETER(apArg);
151477}
151478static void rankValueFunc(sqlite3_context *pCtx){
151479 struct CallCount *p;
151480 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151481 if( p ){
151482 sqlite3_result_int64(pCtx, p->nValue);
151483 p->nValue = 0;
151484 }
151485}
151486
151487/*
151488** Implementation of built-in window function percent_rank(). Assumes that
151489** the window frame has been set to:
151490**
151491** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
151492*/
151493static void percent_rankStepFunc(
151494 sqlite3_context *pCtx,
151495 int nArg,
151496 sqlite3_value **apArg
151497){
151498 struct CallCount *p;
151499 UNUSED_PARAMETER(nArg); assert( nArg==0 );
151500 UNUSED_PARAMETER(apArg);
151501 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151502 if( p ){
151503 p->nTotal++;
151504 }
151505}
151506static void percent_rankInvFunc(
151507 sqlite3_context *pCtx,
151508 int nArg,
151509 sqlite3_value **apArg
151510){
151511 struct CallCount *p;
151512 UNUSED_PARAMETER(nArg); assert( nArg==0 );
151513 UNUSED_PARAMETER(apArg);
151514 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151515 p->nStep++;
151516}
151517static void percent_rankValueFunc(sqlite3_context *pCtx){
151518 struct CallCount *p;
151519 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151520 if( p ){
151521 p->nValue = p->nStep;
151522 if( p->nTotal>1 ){
151523 double r = (double)p->nValue / (double)(p->nTotal-1);
151524 sqlite3_result_double(pCtx, r);
151525 }else{
151526 sqlite3_result_double(pCtx, 0.0);
151527 }
151528 }
151529}
151530#define percent_rankFinalizeFunc percent_rankValueFunc
151531
151532/*
151533** Implementation of built-in window function cume_dist(). Assumes that
151534** the window frame has been set to:
151535**
151536** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
151537*/
151538static void cume_distStepFunc(
151539 sqlite3_context *pCtx,
151540 int nArg,
151541 sqlite3_value **apArg
151542){
151543 struct CallCount *p;
151544 UNUSED_PARAMETER(nArg); assert( nArg==0 );
151545 UNUSED_PARAMETER(apArg);
151546 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151547 if( p ){
151548 p->nTotal++;
151549 }
151550}
151551static void cume_distInvFunc(
151552 sqlite3_context *pCtx,
151553 int nArg,
151554 sqlite3_value **apArg
151555){
151556 struct CallCount *p;
151557 UNUSED_PARAMETER(nArg); assert( nArg==0 );
151558 UNUSED_PARAMETER(apArg);
151559 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151560 p->nStep++;
151561}
151562static void cume_distValueFunc(sqlite3_context *pCtx){
151563 struct CallCount *p;
151564 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
151565 if( p ){
151566 double r = (double)(p->nStep) / (double)(p->nTotal);
151567 sqlite3_result_double(pCtx, r);
151568 }
151569}
151570#define cume_distFinalizeFunc cume_distValueFunc
151571
151572/*
151573** Context object for ntile() window function.
151574*/
151575struct NtileCtx {
151576 i64 nTotal; /* Total rows in partition */
151577 i64 nParam; /* Parameter passed to ntile(N) */
151578 i64 iRow; /* Current row */
151579};
151580
151581/*
151582** Implementation of ntile(). This assumes that the window frame has
151583** been coerced to:
151584**
151585** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
151586*/
151587static void ntileStepFunc(
151588 sqlite3_context *pCtx,
151589 int nArg,
151590 sqlite3_value **apArg
151591){
151592 struct NtileCtx *p;
151593 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
151594 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151595 if( p ){
151596 if( p->nTotal==0 ){
151597 p->nParam = sqlite3_value_int64(apArg[0]);
151598 if( p->nParam<=0 ){
151599 sqlite3_result_error(
151600 pCtx, "argument of ntile must be a positive integer", -1
151601 );
151602 }
151603 }
151604 p->nTotal++;
151605 }
151606}
151607static void ntileInvFunc(
151608 sqlite3_context *pCtx,
151609 int nArg,
151610 sqlite3_value **apArg
151611){
151612 struct NtileCtx *p;
151613 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
151614 UNUSED_PARAMETER(apArg);
151615 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151616 p->iRow++;
151617}
151618static void ntileValueFunc(sqlite3_context *pCtx){
151619 struct NtileCtx *p;
151620 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151621 if( p && p->nParam>0 ){
151622 int nSize = (p->nTotal / p->nParam);
151623 if( nSize==0 ){
151624 sqlite3_result_int64(pCtx, p->iRow+1);
151625 }else{
151626 i64 nLarge = p->nTotal - p->nParam*nSize;
151627 i64 iSmall = nLarge*(nSize+1);
151628 i64 iRow = p->iRow;
151629
151630 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
151631
151632 if( iRow<iSmall ){
151633 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
151634 }else{
151635 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
151636 }
151637 }
151638 }
151639}
151640#define ntileFinalizeFunc ntileValueFunc
151641
151642/*
151643** Context object for last_value() window function.
151644*/
151645struct LastValueCtx {
151646 sqlite3_value *pVal;
151647 int nVal;
151648};
151649
151650/*
151651** Implementation of last_value().
151652*/
151653static void last_valueStepFunc(
151654 sqlite3_context *pCtx,
151655 int nArg,
151656 sqlite3_value **apArg
151657){
151658 struct LastValueCtx *p;
151659 UNUSED_PARAMETER(nArg);
151660 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151661 if( p ){
151662 sqlite3_value_free(p->pVal);
151663 p->pVal = sqlite3_value_dup(apArg[0]);
151664 if( p->pVal==0 ){
151665 sqlite3_result_error_nomem(pCtx);
151666 }else{
151667 p->nVal++;
151668 }
151669 }
151670}
151671static void last_valueInvFunc(
151672 sqlite3_context *pCtx,
151673 int nArg,
151674 sqlite3_value **apArg
151675){
151676 struct LastValueCtx *p;
151677 UNUSED_PARAMETER(nArg);
151678 UNUSED_PARAMETER(apArg);
151679 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151680 if( ALWAYS(p) ){
151681 p->nVal--;
151682 if( p->nVal==0 ){
151683 sqlite3_value_free(p->pVal);
151684 p->pVal = 0;
151685 }
151686 }
151687}
151688static void last_valueValueFunc(sqlite3_context *pCtx){
151689 struct LastValueCtx *p;
151690 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
151691 if( p && p->pVal ){
151692 sqlite3_result_value(pCtx, p->pVal);
151693 }
151694}
151695static void last_valueFinalizeFunc(sqlite3_context *pCtx){
151696 struct LastValueCtx *p;
151697 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
151698 if( p && p->pVal ){
151699 sqlite3_result_value(pCtx, p->pVal);
151700 sqlite3_value_free(p->pVal);
151701 p->pVal = 0;
151702 }
151703}
151704
151705/*
151706** Static names for the built-in window function names. These static
151707** names are used, rather than string literals, so that FuncDef objects
151708** can be associated with a particular window function by direct
151709** comparison of the zName pointer. Example:
151710**
151711** if( pFuncDef->zName==row_valueName ){ ... }
151712*/
151713static const char row_numberName[] = "row_number";
151714static const char dense_rankName[] = "dense_rank";
151715static const char rankName[] = "rank";
151716static const char percent_rankName[] = "percent_rank";
151717static const char cume_distName[] = "cume_dist";
151718static const char ntileName[] = "ntile";
151719static const char last_valueName[] = "last_value";
151720static const char nth_valueName[] = "nth_value";
151721static const char first_valueName[] = "first_value";
151722static const char leadName[] = "lead";
151723static const char lagName[] = "lag";
151724
151725/*
151726** No-op implementations of xStep() and xFinalize(). Used as place-holders
151727** for built-in window functions that never call those interfaces.
151728**
151729** The noopValueFunc() is called but is expected to do nothing. The
151730** noopStepFunc() is never called, and so it is marked with NO_TEST to
151731** let the test coverage routine know not to expect this function to be
151732** invoked.
151733*/
151734static void noopStepFunc( /*NO_TEST*/
151735 sqlite3_context *p, /*NO_TEST*/
151736 int n, /*NO_TEST*/
151737 sqlite3_value **a /*NO_TEST*/
151738){ /*NO_TEST*/
151739 UNUSED_PARAMETER(p); /*NO_TEST*/
151740 UNUSED_PARAMETER(n); /*NO_TEST*/
151741 UNUSED_PARAMETER(a); /*NO_TEST*/
151742 assert(0); /*NO_TEST*/
151743} /*NO_TEST*/
151744static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
151745
151746/* Window functions that use all window interfaces: xStep, xFinal,
151747** xValue, and xInverse */
151748#define WINDOWFUNCALL(name,nArg,extra) { \
151749 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
151750 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
151751 name ## InvFunc, name ## Name, {0} \
151752}
151753
151754/* Window functions that are implemented using bytecode and thus have
151755** no-op routines for their methods */
151756#define WINDOWFUNCNOOP(name,nArg,extra) { \
151757 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
151758 noopStepFunc, noopValueFunc, noopValueFunc, \
151759 noopStepFunc, name ## Name, {0} \
151760}
151761
151762/* Window functions that use all window interfaces: xStep, the
151763** same routine for xFinalize and xValue and which never call
151764** xInverse. */
151765#define WINDOWFUNCX(name,nArg,extra) { \
151766 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
151767 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
151768 noopStepFunc, name ## Name, {0} \
151769}
151770
151771
151772/*
151773** Register those built-in window functions that are not also aggregates.
151774*/
151775SQLITE_PRIVATE void sqlite3WindowFunctions(void){
151776 static FuncDef aWindowFuncs[] = {
151777 WINDOWFUNCX(row_number, 0, 0),
151778 WINDOWFUNCX(dense_rank, 0, 0),
151779 WINDOWFUNCX(rank, 0, 0),
151780 WINDOWFUNCALL(percent_rank, 0, 0),
151781 WINDOWFUNCALL(cume_dist, 0, 0),
151782 WINDOWFUNCALL(ntile, 1, 0),
151783 WINDOWFUNCALL(last_value, 1, 0),
151784 WINDOWFUNCALL(nth_value, 2, 0),
151785 WINDOWFUNCALL(first_value, 1, 0),
151786 WINDOWFUNCNOOP(lead, 1, 0),
151787 WINDOWFUNCNOOP(lead, 2, 0),
151788 WINDOWFUNCNOOP(lead, 3, 0),
151789 WINDOWFUNCNOOP(lag, 1, 0),
151790 WINDOWFUNCNOOP(lag, 2, 0),
151791 WINDOWFUNCNOOP(lag, 3, 0),
151792 };
151793 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
151794}
151795
151796static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
151797 Window *p;
151798 for(p=pList; p; p=p->pNextWin){
151799 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
151800 }
151801 if( p==0 ){
151802 sqlite3ErrorMsg(pParse, "no such window: %s", zName);
151803 }
151804 return p;
151805}
151806
151807/*
151808** This function is called immediately after resolving the function name
151809** for a window function within a SELECT statement. Argument pList is a
151810** linked list of WINDOW definitions for the current SELECT statement.
151811** Argument pFunc is the function definition just resolved and pWin
151812** is the Window object representing the associated OVER clause. This
151813** function updates the contents of pWin as follows:
151814**
151815** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
151816** search list pList for a matching WINDOW definition, and update pWin
151817** accordingly. If no such WINDOW clause can be found, leave an error
151818** in pParse.
151819**
151820** * If the function is a built-in window function that requires the
151821** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
151822** of this file), pWin is updated here.
151823*/
151824SQLITE_PRIVATE void sqlite3WindowUpdate(
151825 Parse *pParse,
151826 Window *pList, /* List of named windows for this SELECT */
151827 Window *pWin, /* Window frame to update */
151828 FuncDef *pFunc /* Window function definition */
151829){
151830 if( pWin->zName && pWin->eFrmType==0 ){
151831 Window *p = windowFind(pParse, pList, pWin->zName);
151832 if( p==0 ) return;
151833 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
151834 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
151835 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
151836 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
151837 pWin->eStart = p->eStart;
151838 pWin->eEnd = p->eEnd;
151839 pWin->eFrmType = p->eFrmType;
151840 pWin->eExclude = p->eExclude;
151841 }else{
151842 sqlite3WindowChain(pParse, pWin, pList);
151843 }
151844 if( (pWin->eFrmType==TK_RANGE)
151845 && (pWin->pStart || pWin->pEnd)
151846 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
151847 ){
151848 sqlite3ErrorMsg(pParse,
151849 "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
151850 );
151851 }else
151852 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
151853 sqlite3 *db = pParse->db;
151854 if( pWin->pFilter ){
151855 sqlite3ErrorMsg(pParse,
151856 "FILTER clause may only be used with aggregate window functions"
151857 );
151858 }else{
151859 struct WindowUpdate {
151860 const char *zFunc;
151861 int eFrmType;
151862 int eStart;
151863 int eEnd;
151864 } aUp[] = {
151865 { row_numberName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
151866 { dense_rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
151867 { rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT },
151868 { percent_rankName, TK_GROUPS, TK_CURRENT, TK_UNBOUNDED },
151869 { cume_distName, TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED },
151870 { ntileName, TK_ROWS, TK_CURRENT, TK_UNBOUNDED },
151871 { leadName, TK_ROWS, TK_UNBOUNDED, TK_UNBOUNDED },
151872 { lagName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT },
151873 };
151874 int i;
151875 for(i=0; i<ArraySize(aUp); i++){
151876 if( pFunc->zName==aUp[i].zFunc ){
151877 sqlite3ExprDelete(db, pWin->pStart);
151878 sqlite3ExprDelete(db, pWin->pEnd);
151879 pWin->pEnd = pWin->pStart = 0;
151880 pWin->eFrmType = aUp[i].eFrmType;
151881 pWin->eStart = aUp[i].eStart;
151882 pWin->eEnd = aUp[i].eEnd;
151883 pWin->eExclude = 0;
151884 if( pWin->eStart==TK_FOLLOWING ){
151885 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
151886 }
151887 break;
151888 }
151889 }
151890 }
151891 }
151892 pWin->pFunc = pFunc;
151893}
151894
151895/*
151896** Context object passed through sqlite3WalkExprList() to
151897** selectWindowRewriteExprCb() by selectWindowRewriteEList().
151898*/
151899typedef struct WindowRewrite WindowRewrite;
151900struct WindowRewrite {
151901 Window *pWin;
151902 SrcList *pSrc;
151903 ExprList *pSub;
151904 Table *pTab;
151905 Select *pSubSelect; /* Current sub-select, if any */
151906};
151907
151908/*
151909** Callback function used by selectWindowRewriteEList(). If necessary,
151910** this function appends to the output expression-list and updates
151911** expression (*ppExpr) in place.
151912*/
151913static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
151914 struct WindowRewrite *p = pWalker->u.pRewrite;
151915 Parse *pParse = pWalker->pParse;
151916 assert( p!=0 );
151917 assert( p->pWin!=0 );
151918
151919 /* If this function is being called from within a scalar sub-select
151920 ** that used by the SELECT statement being processed, only process
151921 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
151922 ** not process aggregates or window functions at all, as they belong
151923 ** to the scalar sub-select. */
151924 if( p->pSubSelect ){
151925 if( pExpr->op!=TK_COLUMN ){
151926 return WRC_Continue;
151927 }else{
151928 int nSrc = p->pSrc->nSrc;
151929 int i;
151930 for(i=0; i<nSrc; i++){
151931 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
151932 }
151933 if( i==nSrc ) return WRC_Continue;
151934 }
151935 }
151936
151937 switch( pExpr->op ){
151938
151939 case TK_FUNCTION:
151940 if( !ExprHasProperty(pExpr, EP_WinFunc) ){
151941 break;
151942 }else{
151943 Window *pWin;
151944 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
151945 if( pExpr->y.pWin==pWin ){
151946 assert( pWin->pOwner==pExpr );
151947 return WRC_Prune;
151948 }
151949 }
151950 }
151951 /* no break */ deliberate_fall_through
151952
151953 case TK_AGG_FUNCTION:
151954 case TK_COLUMN: {
151955 int iCol = -1;
151956 if( p->pSub ){
151957 int i;
151958 for(i=0; i<p->pSub->nExpr; i++){
151959 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
151960 iCol = i;
151961 break;
151962 }
151963 }
151964 }
151965 if( iCol<0 ){
151966 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
151967 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
151968 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
151969 }
151970 if( p->pSub ){
151971 int f = pExpr->flags & EP_Collate;
151972 assert( ExprHasProperty(pExpr, EP_Static)==0 );
151973 ExprSetProperty(pExpr, EP_Static);
151974 sqlite3ExprDelete(pParse->db, pExpr);
151975 ExprClearProperty(pExpr, EP_Static);
151976 memset(pExpr, 0, sizeof(Expr));
151977
151978 pExpr->op = TK_COLUMN;
151979 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
151980 pExpr->iTable = p->pWin->iEphCsr;
151981 pExpr->y.pTab = p->pTab;
151982 pExpr->flags = f;
151983 }
151984 if( pParse->db->mallocFailed ) return WRC_Abort;
151985 break;
151986 }
151987
151988 default: /* no-op */
151989 break;
151990 }
151991
151992 return WRC_Continue;
151993}
151994static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
151995 struct WindowRewrite *p = pWalker->u.pRewrite;
151996 Select *pSave = p->pSubSelect;
151997 if( pSave==pSelect ){
151998 return WRC_Continue;
151999 }else{
152000 p->pSubSelect = pSelect;
152001 sqlite3WalkSelect(pWalker, pSelect);
152002 p->pSubSelect = pSave;
152003 }
152004 return WRC_Prune;
152005}
152006
152007
152008/*
152009** Iterate through each expression in expression-list pEList. For each:
152010**
152011** * TK_COLUMN,
152012** * aggregate function, or
152013** * window function with a Window object that is not a member of the
152014** Window list passed as the second argument (pWin).
152015**
152016** Append the node to output expression-list (*ppSub). And replace it
152017** with a TK_COLUMN that reads the (N-1)th element of table
152018** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
152019** appending the new one.
152020*/
152021static void selectWindowRewriteEList(
152022 Parse *pParse,
152023 Window *pWin,
152024 SrcList *pSrc,
152025 ExprList *pEList, /* Rewrite expressions in this list */
152026 Table *pTab,
152027 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
152028){
152029 Walker sWalker;
152030 WindowRewrite sRewrite;
152031
152032 assert( pWin!=0 );
152033 memset(&sWalker, 0, sizeof(Walker));
152034 memset(&sRewrite, 0, sizeof(WindowRewrite));
152035
152036 sRewrite.pSub = *ppSub;
152037 sRewrite.pWin = pWin;
152038 sRewrite.pSrc = pSrc;
152039 sRewrite.pTab = pTab;
152040
152041 sWalker.pParse = pParse;
152042 sWalker.xExprCallback = selectWindowRewriteExprCb;
152043 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
152044 sWalker.u.pRewrite = &sRewrite;
152045
152046 (void)sqlite3WalkExprList(&sWalker, pEList);
152047
152048 *ppSub = sRewrite.pSub;
152049}
152050
152051/*
152052** Append a copy of each expression in expression-list pAppend to
152053** expression list pList. Return a pointer to the result list.
152054*/
152055static ExprList *exprListAppendList(
152056 Parse *pParse, /* Parsing context */
152057 ExprList *pList, /* List to which to append. Might be NULL */
152058 ExprList *pAppend, /* List of values to append. Might be NULL */
152059 int bIntToNull
152060){
152061 if( pAppend ){
152062 int i;
152063 int nInit = pList ? pList->nExpr : 0;
152064 for(i=0; i<pAppend->nExpr; i++){
152065 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
152066 assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) );
152067 if( bIntToNull && pDup ){
152068 int iDummy;
152069 Expr *pSub;
152070 for(pSub=pDup; ExprHasProperty(pSub, EP_Skip); pSub=pSub->pLeft){
152071 assert( pSub );
152072 }
152073 if( sqlite3ExprIsInteger(pSub, &iDummy) ){
152074 pSub->op = TK_NULL;
152075 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
152076 pSub->u.zToken = 0;
152077 }
152078 }
152079 pList = sqlite3ExprListAppend(pParse, pList, pDup);
152080 if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags;
152081 }
152082 }
152083 return pList;
152084}
152085
152086/*
152087** When rewriting a query, if the new subquery in the FROM clause
152088** contains TK_AGG_FUNCTION nodes that refer to an outer query,
152089** then we have to increase the Expr->op2 values of those nodes
152090** due to the extra subquery layer that was added.
152091**
152092** See also the incrAggDepth() routine in resolve.c
152093*/
152094static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
152095 if( pExpr->op==TK_AGG_FUNCTION
152096 && pExpr->op2>=pWalker->walkerDepth
152097 ){
152098 pExpr->op2++;
152099 }
152100 return WRC_Continue;
152101}
152102
152103/*
152104** If the SELECT statement passed as the second argument does not invoke
152105** any SQL window functions, this function is a no-op. Otherwise, it
152106** rewrites the SELECT statement so that window function xStep functions
152107** are invoked in the correct order as described under "SELECT REWRITING"
152108** at the top of this file.
152109*/
152110SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
152111 int rc = SQLITE_OK;
152112 if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
152113 Vdbe *v = sqlite3GetVdbe(pParse);
152114 sqlite3 *db = pParse->db;
152115 Select *pSub = 0; /* The subquery */
152116 SrcList *pSrc = p->pSrc;
152117 Expr *pWhere = p->pWhere;
152118 ExprList *pGroupBy = p->pGroupBy;
152119 Expr *pHaving = p->pHaving;
152120 ExprList *pSort = 0;
152121
152122 ExprList *pSublist = 0; /* Expression list for sub-query */
152123 Window *pMWin = p->pWin; /* Main window object */
152124 Window *pWin; /* Window object iterator */
152125 Table *pTab;
152126 Walker w;
152127
152128 u32 selFlags = p->selFlags;
152129
152130 pTab = sqlite3DbMallocZero(db, sizeof(Table));
152131 if( pTab==0 ){
152132 return sqlite3ErrorToParser(db, SQLITE_NOMEM);
152133 }
152134 sqlite3AggInfoPersistWalkerInit(&w, pParse);
152135 sqlite3WalkSelect(&w, p);
152136
152137 p->pSrc = 0;
152138 p->pWhere = 0;
152139 p->pGroupBy = 0;
152140 p->pHaving = 0;
152141 p->selFlags &= ~SF_Aggregate;
152142 p->selFlags |= SF_WinRewrite;
152143
152144 /* Create the ORDER BY clause for the sub-select. This is the concatenation
152145 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
152146 ** redundant, remove the ORDER BY from the parent SELECT. */
152147 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
152148 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
152149 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
152150 int nSave = pSort->nExpr;
152151 pSort->nExpr = p->pOrderBy->nExpr;
152152 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
152153 sqlite3ExprListDelete(db, p->pOrderBy);
152154 p->pOrderBy = 0;
152155 }
152156 pSort->nExpr = nSave;
152157 }
152158
152159 /* Assign a cursor number for the ephemeral table used to buffer rows.
152160 ** The OpenEphemeral instruction is coded later, after it is known how
152161 ** many columns the table will have. */
152162 pMWin->iEphCsr = pParse->nTab++;
152163 pParse->nTab += 3;
152164
152165 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
152166 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
152167 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
152168
152169 /* Append the PARTITION BY and ORDER BY expressions to the to the
152170 ** sub-select expression list. They are required to figure out where
152171 ** boundaries for partitions and sets of peer rows lie. */
152172 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
152173 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
152174
152175 /* Append the arguments passed to each window function to the
152176 ** sub-select expression list. Also allocate two registers for each
152177 ** window function - one for the accumulator, another for interim
152178 ** results. */
152179 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152180 ExprList *pArgs = pWin->pOwner->x.pList;
152181 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
152182 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
152183 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
152184 pWin->bExprArgs = 1;
152185 }else{
152186 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
152187 pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
152188 }
152189 if( pWin->pFilter ){
152190 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
152191 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
152192 }
152193 pWin->regAccum = ++pParse->nMem;
152194 pWin->regResult = ++pParse->nMem;
152195 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152196 }
152197
152198 /* If there is no ORDER BY or PARTITION BY clause, and the window
152199 ** function accepts zero arguments, and there are no other columns
152200 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
152201 ** that pSublist is still NULL here. Add a constant expression here to
152202 ** keep everything legal in this case.
152203 */
152204 if( pSublist==0 ){
152205 pSublist = sqlite3ExprListAppend(pParse, 0,
152206 sqlite3Expr(db, TK_INTEGER, "0")
152207 );
152208 }
152209
152210 pSub = sqlite3SelectNew(
152211 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
152212 );
152213 SELECTTRACE(1,pParse,pSub,
152214 ("New window-function subquery in FROM clause of (%u/%p)\n",
152215 p->selId, p));
152216 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
152217 if( p->pSrc ){
152218 Table *pTab2;
152219 p->pSrc->a[0].pSelect = pSub;
152220 sqlite3SrcListAssignCursors(pParse, p->pSrc);
152221 pSub->selFlags |= SF_Expanded;
152222 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
152223 pSub->selFlags |= (selFlags & SF_Aggregate);
152224 if( pTab2==0 ){
152225 /* Might actually be some other kind of error, but in that case
152226 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
152227 ** the correct error message regardless. */
152228 rc = SQLITE_NOMEM;
152229 }else{
152230 memcpy(pTab, pTab2, sizeof(Table));
152231 pTab->tabFlags |= TF_Ephemeral;
152232 p->pSrc->a[0].pTab = pTab;
152233 pTab = pTab2;
152234 memset(&w, 0, sizeof(w));
152235 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
152236 w.xSelectCallback = sqlite3WalkerDepthIncrease;
152237 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
152238 sqlite3WalkSelect(&w, pSub);
152239 }
152240 }else{
152241 sqlite3SelectDelete(db, pSub);
152242 }
152243 if( db->mallocFailed ) rc = SQLITE_NOMEM;
152244 sqlite3DbFree(db, pTab);
152245 }
152246
152247 if( rc ){
152248 if( pParse->nErr==0 ){
152249 assert( pParse->db->mallocFailed );
152250 sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM);
152251 }
152252 }
152253 return rc;
152254}
152255
152256/*
152257** Unlink the Window object from the Select to which it is attached,
152258** if it is attached.
152259*/
152260SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window *p){
152261 if( p->ppThis ){
152262 *p->ppThis = p->pNextWin;
152263 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
152264 p->ppThis = 0;
152265 }
152266}
152267
152268/*
152269** Free the Window object passed as the second argument.
152270*/
152271SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
152272 if( p ){
152273 sqlite3WindowUnlinkFromSelect(p);
152274 sqlite3ExprDelete(db, p->pFilter);
152275 sqlite3ExprListDelete(db, p->pPartition);
152276 sqlite3ExprListDelete(db, p->pOrderBy);
152277 sqlite3ExprDelete(db, p->pEnd);
152278 sqlite3ExprDelete(db, p->pStart);
152279 sqlite3DbFree(db, p->zName);
152280 sqlite3DbFree(db, p->zBase);
152281 sqlite3DbFree(db, p);
152282 }
152283}
152284
152285/*
152286** Free the linked list of Window objects starting at the second argument.
152287*/
152288SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
152289 while( p ){
152290 Window *pNext = p->pNextWin;
152291 sqlite3WindowDelete(db, p);
152292 p = pNext;
152293 }
152294}
152295
152296/*
152297** The argument expression is an PRECEDING or FOLLOWING offset. The
152298** value should be a non-negative integer. If the value is not a
152299** constant, change it to NULL. The fact that it is then a non-negative
152300** integer will be caught later. But it is important not to leave
152301** variable values in the expression tree.
152302*/
152303static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
152304 if( 0==sqlite3ExprIsConstant(pExpr) ){
152305 if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
152306 sqlite3ExprDelete(pParse->db, pExpr);
152307 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
152308 }
152309 return pExpr;
152310}
152311
152312/*
152313** Allocate and return a new Window object describing a Window Definition.
152314*/
152315SQLITE_PRIVATE Window *sqlite3WindowAlloc(
152316 Parse *pParse, /* Parsing context */
152317 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
152318 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
152319 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
152320 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
152321 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
152322 u8 eExclude /* EXCLUDE clause */
152323){
152324 Window *pWin = 0;
152325 int bImplicitFrame = 0;
152326
152327 /* Parser assures the following: */
152328 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
152329 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
152330 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
152331 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
152332 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
152333 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
152334 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
152335
152336 if( eType==0 ){
152337 bImplicitFrame = 1;
152338 eType = TK_RANGE;
152339 }
152340
152341 /* Additionally, the
152342 ** starting boundary type may not occur earlier in the following list than
152343 ** the ending boundary type:
152344 **
152345 ** UNBOUNDED PRECEDING
152346 ** <expr> PRECEDING
152347 ** CURRENT ROW
152348 ** <expr> FOLLOWING
152349 ** UNBOUNDED FOLLOWING
152350 **
152351 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
152352 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
152353 ** frame boundary.
152354 */
152355 if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
152356 || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
152357 ){
152358 sqlite3ErrorMsg(pParse, "unsupported frame specification");
152359 goto windowAllocErr;
152360 }
152361
152362 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
152363 if( pWin==0 ) goto windowAllocErr;
152364 pWin->eFrmType = eType;
152365 pWin->eStart = eStart;
152366 pWin->eEnd = eEnd;
152367 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
152368 eExclude = TK_NO;
152369 }
152370 pWin->eExclude = eExclude;
152371 pWin->bImplicitFrame = bImplicitFrame;
152372 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
152373 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
152374 return pWin;
152375
152376windowAllocErr:
152377 sqlite3ExprDelete(pParse->db, pEnd);
152378 sqlite3ExprDelete(pParse->db, pStart);
152379 return 0;
152380}
152381
152382/*
152383** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
152384** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
152385** equivalent nul-terminated string.
152386*/
152387SQLITE_PRIVATE Window *sqlite3WindowAssemble(
152388 Parse *pParse,
152389 Window *pWin,
152390 ExprList *pPartition,
152391 ExprList *pOrderBy,
152392 Token *pBase
152393){
152394 if( pWin ){
152395 pWin->pPartition = pPartition;
152396 pWin->pOrderBy = pOrderBy;
152397 if( pBase ){
152398 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
152399 }
152400 }else{
152401 sqlite3ExprListDelete(pParse->db, pPartition);
152402 sqlite3ExprListDelete(pParse->db, pOrderBy);
152403 }
152404 return pWin;
152405}
152406
152407/*
152408** Window *pWin has just been created from a WINDOW clause. Tokne pBase
152409** is the base window. Earlier windows from the same WINDOW clause are
152410** stored in the linked list starting at pWin->pNextWin. This function
152411** either updates *pWin according to the base specification, or else
152412** leaves an error in pParse.
152413*/
152414SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
152415 if( pWin->zBase ){
152416 sqlite3 *db = pParse->db;
152417 Window *pExist = windowFind(pParse, pList, pWin->zBase);
152418 if( pExist ){
152419 const char *zErr = 0;
152420 /* Check for errors */
152421 if( pWin->pPartition ){
152422 zErr = "PARTITION clause";
152423 }else if( pExist->pOrderBy && pWin->pOrderBy ){
152424 zErr = "ORDER BY clause";
152425 }else if( pExist->bImplicitFrame==0 ){
152426 zErr = "frame specification";
152427 }
152428 if( zErr ){
152429 sqlite3ErrorMsg(pParse,
152430 "cannot override %s of window: %s", zErr, pWin->zBase
152431 );
152432 }else{
152433 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
152434 if( pExist->pOrderBy ){
152435 assert( pWin->pOrderBy==0 );
152436 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
152437 }
152438 sqlite3DbFree(db, pWin->zBase);
152439 pWin->zBase = 0;
152440 }
152441 }
152442 }
152443}
152444
152445/*
152446** Attach window object pWin to expression p.
152447*/
152448SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
152449 if( p ){
152450 assert( p->op==TK_FUNCTION );
152451 assert( pWin );
152452 p->y.pWin = pWin;
152453 ExprSetProperty(p, EP_WinFunc);
152454 pWin->pOwner = p;
152455 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
152456 sqlite3ErrorMsg(pParse,
152457 "DISTINCT is not supported for window functions"
152458 );
152459 }
152460 }else{
152461 sqlite3WindowDelete(pParse->db, pWin);
152462 }
152463}
152464
152465/*
152466** Possibly link window pWin into the list at pSel->pWin (window functions
152467** to be processed as part of SELECT statement pSel). The window is linked
152468** in if either (a) there are no other windows already linked to this
152469** SELECT, or (b) the windows already linked use a compatible window frame.
152470*/
152471SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin){
152472 if( pSel!=0
152473 && (0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0))
152474 ){
152475 pWin->pNextWin = pSel->pWin;
152476 if( pSel->pWin ){
152477 pSel->pWin->ppThis = &pWin->pNextWin;
152478 }
152479 pSel->pWin = pWin;
152480 pWin->ppThis = &pSel->pWin;
152481 }
152482}
152483
152484/*
152485** Return 0 if the two window objects are identical, 1 if they are
152486** different, or 2 if it cannot be determined if the objects are identical
152487** or not. Identical window objects can be processed in a single scan.
152488*/
152489SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2, int bFilter){
152490 int res;
152491 if( NEVER(p1==0) || NEVER(p2==0) ) return 1;
152492 if( p1->eFrmType!=p2->eFrmType ) return 1;
152493 if( p1->eStart!=p2->eStart ) return 1;
152494 if( p1->eEnd!=p2->eEnd ) return 1;
152495 if( p1->eExclude!=p2->eExclude ) return 1;
152496 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
152497 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
152498 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
152499 return res;
152500 }
152501 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
152502 return res;
152503 }
152504 if( bFilter ){
152505 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
152506 return res;
152507 }
152508 }
152509 return 0;
152510}
152511
152512
152513/*
152514** This is called by code in select.c before it calls sqlite3WhereBegin()
152515** to begin iterating through the sub-query results. It is used to allocate
152516** and initialize registers and cursors used by sqlite3WindowCodeStep().
152517*/
152518SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
152519 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
152520 Window *pMWin = pSelect->pWin;
152521 Window *pWin;
152522 Vdbe *v = sqlite3GetVdbe(pParse);
152523
152524 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
152525 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
152526 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
152527 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
152528
152529 /* Allocate registers to use for PARTITION BY values, if any. Initialize
152530 ** said registers to NULL. */
152531 if( pMWin->pPartition ){
152532 int nExpr = pMWin->pPartition->nExpr;
152533 pMWin->regPart = pParse->nMem+1;
152534 pParse->nMem += nExpr;
152535 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
152536 }
152537
152538 pMWin->regOne = ++pParse->nMem;
152539 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
152540
152541 if( pMWin->eExclude ){
152542 pMWin->regStartRowid = ++pParse->nMem;
152543 pMWin->regEndRowid = ++pParse->nMem;
152544 pMWin->csrApp = pParse->nTab++;
152545 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
152546 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
152547 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
152548 return;
152549 }
152550
152551 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152552 FuncDef *p = pWin->pFunc;
152553 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
152554 /* The inline versions of min() and max() require a single ephemeral
152555 ** table and 3 registers. The registers are used as follows:
152556 **
152557 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
152558 ** regApp+1: integer value used to ensure keys are unique
152559 ** regApp+2: output of MakeRecord
152560 */
152561 ExprList *pList = pWin->pOwner->x.pList;
152562 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
152563 pWin->csrApp = pParse->nTab++;
152564 pWin->regApp = pParse->nMem+1;
152565 pParse->nMem += 3;
152566 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
152567 assert( pKeyInfo->aSortFlags[0]==0 );
152568 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
152569 }
152570 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
152571 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
152572 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
152573 }
152574 else if( p->zName==nth_valueName || p->zName==first_valueName ){
152575 /* Allocate two registers at pWin->regApp. These will be used to
152576 ** store the start and end index of the current frame. */
152577 pWin->regApp = pParse->nMem+1;
152578 pWin->csrApp = pParse->nTab++;
152579 pParse->nMem += 2;
152580 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
152581 }
152582 else if( p->zName==leadName || p->zName==lagName ){
152583 pWin->csrApp = pParse->nTab++;
152584 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
152585 }
152586 }
152587}
152588
152589#define WINDOW_STARTING_INT 0
152590#define WINDOW_ENDING_INT 1
152591#define WINDOW_NTH_VALUE_INT 2
152592#define WINDOW_STARTING_NUM 3
152593#define WINDOW_ENDING_NUM 4
152594
152595/*
152596** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
152597** value of the second argument to nth_value() (eCond==2) has just been
152598** evaluated and the result left in register reg. This function generates VM
152599** code to check that the value is a non-negative integer and throws an
152600** exception if it is not.
152601*/
152602static void windowCheckValue(Parse *pParse, int reg, int eCond){
152603 static const char *azErr[] = {
152604 "frame starting offset must be a non-negative integer",
152605 "frame ending offset must be a non-negative integer",
152606 "second argument to nth_value must be a positive integer",
152607 "frame starting offset must be a non-negative number",
152608 "frame ending offset must be a non-negative number",
152609 };
152610 static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
152611 Vdbe *v = sqlite3GetVdbe(pParse);
152612 int regZero = sqlite3GetTempReg(pParse);
152613 assert( eCond>=0 && eCond<ArraySize(azErr) );
152614 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
152615 if( eCond>=WINDOW_STARTING_NUM ){
152616 int regString = sqlite3GetTempReg(pParse);
152617 sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
152618 sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
152619 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
152620 VdbeCoverage(v);
152621 assert( eCond==3 || eCond==4 );
152622 VdbeCoverageIf(v, eCond==3);
152623 VdbeCoverageIf(v, eCond==4);
152624 }else{
152625 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
152626 VdbeCoverage(v);
152627 assert( eCond==0 || eCond==1 || eCond==2 );
152628 VdbeCoverageIf(v, eCond==0);
152629 VdbeCoverageIf(v, eCond==1);
152630 VdbeCoverageIf(v, eCond==2);
152631 }
152632 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
152633 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
152634 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
152635 VdbeCoverageNeverNullIf(v, eCond==2);
152636 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
152637 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
152638 sqlite3MayAbort(pParse);
152639 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
152640 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
152641 sqlite3ReleaseTempReg(pParse, regZero);
152642}
152643
152644/*
152645** Return the number of arguments passed to the window-function associated
152646** with the object passed as the only argument to this function.
152647*/
152648static int windowArgCount(Window *pWin){
152649 ExprList *pList = pWin->pOwner->x.pList;
152650 return (pList ? pList->nExpr : 0);
152651}
152652
152653typedef struct WindowCodeArg WindowCodeArg;
152654typedef struct WindowCsrAndReg WindowCsrAndReg;
152655
152656/*
152657** See comments above struct WindowCodeArg.
152658*/
152659struct WindowCsrAndReg {
152660 int csr; /* Cursor number */
152661 int reg; /* First in array of peer values */
152662};
152663
152664/*
152665** A single instance of this structure is allocated on the stack by
152666** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
152667** routines. This is to reduce the number of arguments required by each
152668** helper function.
152669**
152670** regArg:
152671** Each window function requires an accumulator register (just as an
152672** ordinary aggregate function does). This variable is set to the first
152673** in an array of accumulator registers - one for each window function
152674** in the WindowCodeArg.pMWin list.
152675**
152676** eDelete:
152677** The window functions implementation sometimes caches the input rows
152678** that it processes in a temporary table. If it is not zero, this
152679** variable indicates when rows may be removed from the temp table (in
152680** order to reduce memory requirements - it would always be safe just
152681** to leave them there). Possible values for eDelete are:
152682**
152683** WINDOW_RETURN_ROW:
152684** An input row can be discarded after it is returned to the caller.
152685**
152686** WINDOW_AGGINVERSE:
152687** An input row can be discarded after the window functions xInverse()
152688** callbacks have been invoked in it.
152689**
152690** WINDOW_AGGSTEP:
152691** An input row can be discarded after the window functions xStep()
152692** callbacks have been invoked in it.
152693**
152694** start,current,end
152695** Consider a window-frame similar to the following:
152696**
152697** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
152698**
152699** The windows functions implmentation caches the input rows in a temp
152700** table, sorted by "a, b" (it actually populates the cache lazily, and
152701** aggressively removes rows once they are no longer required, but that's
152702** a mere detail). It keeps three cursors open on the temp table. One
152703** (current) that points to the next row to return to the query engine
152704** once its window function values have been calculated. Another (end)
152705** points to the next row to call the xStep() method of each window function
152706** on (so that it is 2 groups ahead of current). And a third (start) that
152707** points to the next row to call the xInverse() method of each window
152708** function on.
152709**
152710** Each cursor (start, current and end) consists of a VDBE cursor
152711** (WindowCsrAndReg.csr) and an array of registers (starting at
152712** WindowCodeArg.reg) that always contains a copy of the peer values
152713** read from the corresponding cursor.
152714**
152715** Depending on the window-frame in question, all three cursors may not
152716** be required. In this case both WindowCodeArg.csr and reg are set to
152717** 0.
152718*/
152719struct WindowCodeArg {
152720 Parse *pParse; /* Parse context */
152721 Window *pMWin; /* First in list of functions being processed */
152722 Vdbe *pVdbe; /* VDBE object */
152723 int addrGosub; /* OP_Gosub to this address to return one row */
152724 int regGosub; /* Register used with OP_Gosub(addrGosub) */
152725 int regArg; /* First in array of accumulator registers */
152726 int eDelete; /* See above */
152727
152728 WindowCsrAndReg start;
152729 WindowCsrAndReg current;
152730 WindowCsrAndReg end;
152731};
152732
152733/*
152734** Generate VM code to read the window frames peer values from cursor csr into
152735** an array of registers starting at reg.
152736*/
152737static void windowReadPeerValues(
152738 WindowCodeArg *p,
152739 int csr,
152740 int reg
152741){
152742 Window *pMWin = p->pMWin;
152743 ExprList *pOrderBy = pMWin->pOrderBy;
152744 if( pOrderBy ){
152745 Vdbe *v = sqlite3GetVdbe(p->pParse);
152746 ExprList *pPart = pMWin->pPartition;
152747 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
152748 int i;
152749 for(i=0; i<pOrderBy->nExpr; i++){
152750 sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
152751 }
152752 }
152753}
152754
152755/*
152756** Generate VM code to invoke either xStep() (if bInverse is 0) or
152757** xInverse (if bInverse is non-zero) for each window function in the
152758** linked list starting at pMWin. Or, for built-in window functions
152759** that do not use the standard function API, generate the required
152760** inline VM code.
152761**
152762** If argument csr is greater than or equal to 0, then argument reg is
152763** the first register in an array of registers guaranteed to be large
152764** enough to hold the array of arguments for each function. In this case
152765** the arguments are extracted from the current row of csr into the
152766** array of registers before invoking OP_AggStep or OP_AggInverse
152767**
152768** Or, if csr is less than zero, then the array of registers at reg is
152769** already populated with all columns from the current row of the sub-query.
152770**
152771** If argument regPartSize is non-zero, then it is a register containing the
152772** number of rows in the current partition.
152773*/
152774static void windowAggStep(
152775 WindowCodeArg *p,
152776 Window *pMWin, /* Linked list of window functions */
152777 int csr, /* Read arguments from this cursor */
152778 int bInverse, /* True to invoke xInverse instead of xStep */
152779 int reg /* Array of registers */
152780){
152781 Parse *pParse = p->pParse;
152782 Vdbe *v = sqlite3GetVdbe(pParse);
152783 Window *pWin;
152784 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152785 FuncDef *pFunc = pWin->pFunc;
152786 int regArg;
152787 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
152788 int i;
152789
152790 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
152791
152792 /* All OVER clauses in the same window function aggregate step must
152793 ** be the same. */
152794 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 );
152795
152796 for(i=0; i<nArg; i++){
152797 if( i!=1 || pFunc->zName!=nth_valueName ){
152798 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
152799 }else{
152800 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
152801 }
152802 }
152803 regArg = reg;
152804
152805 if( pMWin->regStartRowid==0
152806 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
152807 && (pWin->eStart!=TK_UNBOUNDED)
152808 ){
152809 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
152810 VdbeCoverage(v);
152811 if( bInverse==0 ){
152812 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
152813 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
152814 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
152815 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
152816 }else{
152817 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
152818 VdbeCoverageNeverTaken(v);
152819 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
152820 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
152821 }
152822 sqlite3VdbeJumpHere(v, addrIsNull);
152823 }else if( pWin->regApp ){
152824 assert( pFunc->zName==nth_valueName
152825 || pFunc->zName==first_valueName
152826 );
152827 assert( bInverse==0 || bInverse==1 );
152828 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
152829 }else if( pFunc->xSFunc!=noopStepFunc ){
152830 int addrIf = 0;
152831 if( pWin->pFilter ){
152832 int regTmp;
152833 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
152834 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
152835 regTmp = sqlite3GetTempReg(pParse);
152836 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
152837 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
152838 VdbeCoverage(v);
152839 sqlite3ReleaseTempReg(pParse, regTmp);
152840 }
152841
152842 if( pWin->bExprArgs ){
152843 int iStart = sqlite3VdbeCurrentAddr(v);
152844 VdbeOp *pOp, *pEnd;
152845
152846 nArg = pWin->pOwner->x.pList->nExpr;
152847 regArg = sqlite3GetTempRange(pParse, nArg);
152848 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
152849
152850 pEnd = sqlite3VdbeGetOp(v, -1);
152851 for(pOp=sqlite3VdbeGetOp(v, iStart); pOp<=pEnd; pOp++){
152852 if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
152853 pOp->p1 = csr;
152854 }
152855 }
152856 }
152857 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
152858 CollSeq *pColl;
152859 assert( nArg>0 );
152860 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
152861 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
152862 }
152863 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
152864 bInverse, regArg, pWin->regAccum);
152865 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
152866 sqlite3VdbeChangeP5(v, (u8)nArg);
152867 if( pWin->bExprArgs ){
152868 sqlite3ReleaseTempRange(pParse, regArg, nArg);
152869 }
152870 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
152871 }
152872 }
152873}
152874
152875/*
152876** Values that may be passed as the second argument to windowCodeOp().
152877*/
152878#define WINDOW_RETURN_ROW 1
152879#define WINDOW_AGGINVERSE 2
152880#define WINDOW_AGGSTEP 3
152881
152882/*
152883** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
152884** (bFin==1) for each window function in the linked list starting at
152885** pMWin. Or, for built-in window-functions that do not use the standard
152886** API, generate the equivalent VM code.
152887*/
152888static void windowAggFinal(WindowCodeArg *p, int bFin){
152889 Parse *pParse = p->pParse;
152890 Window *pMWin = p->pMWin;
152891 Vdbe *v = sqlite3GetVdbe(pParse);
152892 Window *pWin;
152893
152894 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152895 if( pMWin->regStartRowid==0
152896 && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
152897 && (pWin->eStart!=TK_UNBOUNDED)
152898 ){
152899 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
152900 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
152901 VdbeCoverage(v);
152902 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
152903 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
152904 }else if( pWin->regApp ){
152905 assert( pMWin->regStartRowid==0 );
152906 }else{
152907 int nArg = windowArgCount(pWin);
152908 if( bFin ){
152909 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
152910 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
152911 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
152912 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152913 }else{
152914 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
152915 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
152916 }
152917 }
152918 }
152919}
152920
152921/*
152922** Generate code to calculate the current values of all window functions in the
152923** p->pMWin list by doing a full scan of the current window frame. Store the
152924** results in the Window.regResult registers, ready to return the upper
152925** layer.
152926*/
152927static void windowFullScan(WindowCodeArg *p){
152928 Window *pWin;
152929 Parse *pParse = p->pParse;
152930 Window *pMWin = p->pMWin;
152931 Vdbe *v = p->pVdbe;
152932
152933 int regCRowid = 0; /* Current rowid value */
152934 int regCPeer = 0; /* Current peer values */
152935 int regRowid = 0; /* AggStep rowid value */
152936 int regPeer = 0; /* AggStep peer values */
152937
152938 int nPeer;
152939 int lblNext;
152940 int lblBrk;
152941 int addrNext;
152942 int csr;
152943
152944 VdbeModuleComment((v, "windowFullScan begin"));
152945
152946 assert( pMWin!=0 );
152947 csr = pMWin->csrApp;
152948 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
152949
152950 lblNext = sqlite3VdbeMakeLabel(pParse);
152951 lblBrk = sqlite3VdbeMakeLabel(pParse);
152952
152953 regCRowid = sqlite3GetTempReg(pParse);
152954 regRowid = sqlite3GetTempReg(pParse);
152955 if( nPeer ){
152956 regCPeer = sqlite3GetTempRange(pParse, nPeer);
152957 regPeer = sqlite3GetTempRange(pParse, nPeer);
152958 }
152959
152960 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
152961 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
152962
152963 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
152964 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
152965 }
152966
152967 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
152968 VdbeCoverage(v);
152969 addrNext = sqlite3VdbeCurrentAddr(v);
152970 sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
152971 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
152972 VdbeCoverageNeverNull(v);
152973
152974 if( pMWin->eExclude==TK_CURRENT ){
152975 sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
152976 VdbeCoverageNeverNull(v);
152977 }else if( pMWin->eExclude!=TK_NO ){
152978 int addr;
152979 int addrEq = 0;
152980 KeyInfo *pKeyInfo = 0;
152981
152982 if( pMWin->pOrderBy ){
152983 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
152984 }
152985 if( pMWin->eExclude==TK_TIES ){
152986 addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
152987 VdbeCoverageNeverNull(v);
152988 }
152989 if( pKeyInfo ){
152990 windowReadPeerValues(p, csr, regPeer);
152991 sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
152992 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
152993 addr = sqlite3VdbeCurrentAddr(v)+1;
152994 sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
152995 VdbeCoverageEqNe(v);
152996 }else{
152997 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
152998 }
152999 if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
153000 }
153001
153002 windowAggStep(p, pMWin, csr, 0, p->regArg);
153003
153004 sqlite3VdbeResolveLabel(v, lblNext);
153005 sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
153006 VdbeCoverage(v);
153007 sqlite3VdbeJumpHere(v, addrNext-1);
153008 sqlite3VdbeJumpHere(v, addrNext+1);
153009 sqlite3ReleaseTempReg(pParse, regRowid);
153010 sqlite3ReleaseTempReg(pParse, regCRowid);
153011 if( nPeer ){
153012 sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
153013 sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
153014 }
153015
153016 windowAggFinal(p, 1);
153017 VdbeModuleComment((v, "windowFullScan end"));
153018}
153019
153020/*
153021** Invoke the sub-routine at regGosub (generated by code in select.c) to
153022** return the current row of Window.iEphCsr. If all window functions are
153023** aggregate window functions that use the standard API, a single
153024** OP_Gosub instruction is all that this routine generates. Extra VM code
153025** for per-row processing is only generated for the following built-in window
153026** functions:
153027**
153028** nth_value()
153029** first_value()
153030** lag()
153031** lead()
153032*/
153033static void windowReturnOneRow(WindowCodeArg *p){
153034 Window *pMWin = p->pMWin;
153035 Vdbe *v = p->pVdbe;
153036
153037 if( pMWin->regStartRowid ){
153038 windowFullScan(p);
153039 }else{
153040 Parse *pParse = p->pParse;
153041 Window *pWin;
153042
153043 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
153044 FuncDef *pFunc = pWin->pFunc;
153045 if( pFunc->zName==nth_valueName
153046 || pFunc->zName==first_valueName
153047 ){
153048 int csr = pWin->csrApp;
153049 int lbl = sqlite3VdbeMakeLabel(pParse);
153050 int tmpReg = sqlite3GetTempReg(pParse);
153051 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
153052
153053 if( pFunc->zName==nth_valueName ){
153054 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
153055 windowCheckValue(pParse, tmpReg, 2);
153056 }else{
153057 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
153058 }
153059 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
153060 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
153061 VdbeCoverageNeverNull(v);
153062 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
153063 VdbeCoverageNeverTaken(v);
153064 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
153065 sqlite3VdbeResolveLabel(v, lbl);
153066 sqlite3ReleaseTempReg(pParse, tmpReg);
153067 }
153068 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
153069 int nArg = pWin->pOwner->x.pList->nExpr;
153070 int csr = pWin->csrApp;
153071 int lbl = sqlite3VdbeMakeLabel(pParse);
153072 int tmpReg = sqlite3GetTempReg(pParse);
153073 int iEph = pMWin->iEphCsr;
153074
153075 if( nArg<3 ){
153076 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
153077 }else{
153078 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
153079 }
153080 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
153081 if( nArg<2 ){
153082 int val = (pFunc->zName==leadName ? 1 : -1);
153083 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
153084 }else{
153085 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
153086 int tmpReg2 = sqlite3GetTempReg(pParse);
153087 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
153088 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
153089 sqlite3ReleaseTempReg(pParse, tmpReg2);
153090 }
153091
153092 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
153093 VdbeCoverage(v);
153094 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
153095 sqlite3VdbeResolveLabel(v, lbl);
153096 sqlite3ReleaseTempReg(pParse, tmpReg);
153097 }
153098 }
153099 }
153100 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
153101}
153102
153103/*
153104** Generate code to set the accumulator register for each window function
153105** in the linked list passed as the second argument to NULL. And perform
153106** any equivalent initialization required by any built-in window functions
153107** in the list.
153108*/
153109static int windowInitAccum(Parse *pParse, Window *pMWin){
153110 Vdbe *v = sqlite3GetVdbe(pParse);
153111 int regArg;
153112 int nArg = 0;
153113 Window *pWin;
153114 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
153115 FuncDef *pFunc = pWin->pFunc;
153116 assert( pWin->regAccum );
153117 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
153118 nArg = MAX(nArg, windowArgCount(pWin));
153119 if( pMWin->regStartRowid==0 ){
153120 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
153121 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
153122 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
153123 }
153124
153125 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
153126 assert( pWin->eStart!=TK_UNBOUNDED );
153127 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
153128 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
153129 }
153130 }
153131 }
153132 regArg = pParse->nMem+1;
153133 pParse->nMem += nArg;
153134 return regArg;
153135}
153136
153137/*
153138** Return true if the current frame should be cached in the ephemeral table,
153139** even if there are no xInverse() calls required.
153140*/
153141static int windowCacheFrame(Window *pMWin){
153142 Window *pWin;
153143 if( pMWin->regStartRowid ) return 1;
153144 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
153145 FuncDef *pFunc = pWin->pFunc;
153146 if( (pFunc->zName==nth_valueName)
153147 || (pFunc->zName==first_valueName)
153148 || (pFunc->zName==leadName)
153149 || (pFunc->zName==lagName)
153150 ){
153151 return 1;
153152 }
153153 }
153154 return 0;
153155}
153156
153157/*
153158** regOld and regNew are each the first register in an array of size
153159** pOrderBy->nExpr. This function generates code to compare the two
153160** arrays of registers using the collation sequences and other comparison
153161** parameters specified by pOrderBy.
153162**
153163** If the two arrays are not equal, the contents of regNew is copied to
153164** regOld and control falls through. Otherwise, if the contents of the arrays
153165** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
153166*/
153167static void windowIfNewPeer(
153168 Parse *pParse,
153169 ExprList *pOrderBy,
153170 int regNew, /* First in array of new values */
153171 int regOld, /* First in array of old values */
153172 int addr /* Jump here */
153173){
153174 Vdbe *v = sqlite3GetVdbe(pParse);
153175 if( pOrderBy ){
153176 int nVal = pOrderBy->nExpr;
153177 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
153178 sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
153179 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
153180 sqlite3VdbeAddOp3(v, OP_Jump,
153181 sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
153182 );
153183 VdbeCoverageEqNe(v);
153184 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
153185 }else{
153186 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
153187 }
153188}
153189
153190/*
153191** This function is called as part of generating VM programs for RANGE
153192** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
153193** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
153194** code equivalent to:
153195**
153196** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
153197**
153198** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
153199** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
153200**
153201** If the sort-order for the ORDER BY term in the window is DESC, then the
153202** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
153203** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
153204** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
153205** is OP_Ge, the generated code is equivalent to:
153206**
153207** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
153208**
153209** A special type of arithmetic is used such that if csr1.peerVal is not
153210** a numeric type (real or integer), then the result of the addition addition
153211** or subtraction is a a copy of csr1.peerVal.
153212*/
153213static void windowCodeRangeTest(
153214 WindowCodeArg *p,
153215 int op, /* OP_Ge, OP_Gt, or OP_Le */
153216 int csr1, /* Cursor number for cursor 1 */
153217 int regVal, /* Register containing non-negative number */
153218 int csr2, /* Cursor number for cursor 2 */
153219 int lbl /* Jump destination if condition is true */
153220){
153221 Parse *pParse = p->pParse;
153222 Vdbe *v = sqlite3GetVdbe(pParse);
153223 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
153224 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
153225 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
153226 int regString = ++pParse->nMem; /* Reg. for constant value '' */
153227 int arith = OP_Add; /* OP_Add or OP_Subtract */
153228 int addrGe; /* Jump destination */
153229
153230 assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
153231 assert( pOrderBy && pOrderBy->nExpr==1 );
153232 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_DESC ){
153233 switch( op ){
153234 case OP_Ge: op = OP_Le; break;
153235 case OP_Gt: op = OP_Lt; break;
153236 default: assert( op==OP_Le ); op = OP_Ge; break;
153237 }
153238 arith = OP_Subtract;
153239 }
153240
153241 /* Read the peer-value from each cursor into a register */
153242 windowReadPeerValues(p, csr1, reg1);
153243 windowReadPeerValues(p, csr2, reg2);
153244
153245 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
153246 reg1, (arith==OP_Add ? "+" : "-"), regVal,
153247 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
153248 ));
153249
153250 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
153251 ** This block adds (or subtracts for DESC) the numeric value in regVal
153252 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
153253 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
153254 **
153255 ** if( reg1>='' ) goto addrGe;
153256 ** reg1 = reg1 +/- regVal
153257 ** addrGe:
153258 **
153259 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
153260 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
153261 ** then the arithmetic is performed, but since adding or subtracting from
153262 ** NULL is always NULL anyway, this case is handled as required too. */
153263 sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
153264 addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
153265 VdbeCoverage(v);
153266 sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
153267 sqlite3VdbeJumpHere(v, addrGe);
153268
153269 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
153270 ** consider NULL values to be larger than all other values, instead of
153271 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
153272 ** (and adding that capability causes a performance regression), so
153273 ** instead if the BIGNULL flag is set then cases where either reg1 or
153274 ** reg2 are NULL are handled separately in the following block. The code
153275 ** generated is equivalent to:
153276 **
153277 ** if( reg1 IS NULL ){
153278 ** if( op==OP_Ge ) goto lbl;
153279 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
153280 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
153281 ** }else if( reg2 IS NULL ){
153282 ** if( op==OP_Le ) goto lbl;
153283 ** }
153284 **
153285 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
153286 ** not taken, control jumps over the comparison operator coded below this
153287 ** block. */
153288 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_BIGNULL ){
153289 /* This block runs if reg1 contains a NULL. */
153290 int addr = sqlite3VdbeAddOp1(v, OP_NotNull, reg1); VdbeCoverage(v);
153291 switch( op ){
153292 case OP_Ge:
153293 sqlite3VdbeAddOp2(v, OP_Goto, 0, lbl);
153294 break;
153295 case OP_Gt:
153296 sqlite3VdbeAddOp2(v, OP_NotNull, reg2, lbl);
153297 VdbeCoverage(v);
153298 break;
153299 case OP_Le:
153300 sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl);
153301 VdbeCoverage(v);
153302 break;
153303 default: assert( op==OP_Lt ); /* no-op */ break;
153304 }
153305 sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
153306
153307 /* This block runs if reg1 is not NULL, but reg2 is. */
153308 sqlite3VdbeJumpHere(v, addr);
153309 sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); VdbeCoverage(v);
153310 if( op==OP_Gt || op==OP_Ge ){
153311 sqlite3VdbeChangeP2(v, -1, sqlite3VdbeCurrentAddr(v)+1);
153312 }
153313 }
153314
153315 /* Compare registers reg2 and reg1, taking the jump if required. Note that
153316 ** control skips over this test if the BIGNULL flag is set and either
153317 ** reg1 or reg2 contain a NULL value. */
153318 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
153319 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
153320
153321 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
153322 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
153323 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
153324 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
153325 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
153326 sqlite3ReleaseTempReg(pParse, reg1);
153327 sqlite3ReleaseTempReg(pParse, reg2);
153328
153329 VdbeModuleComment((v, "CodeRangeTest: end"));
153330}
153331
153332/*
153333** Helper function for sqlite3WindowCodeStep(). Each call to this function
153334** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
153335** operation. Refer to the header comment for sqlite3WindowCodeStep() for
153336** details.
153337*/
153338static int windowCodeOp(
153339 WindowCodeArg *p, /* Context object */
153340 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
153341 int regCountdown, /* Register for OP_IfPos countdown */
153342 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
153343){
153344 int csr, reg;
153345 Parse *pParse = p->pParse;
153346 Window *pMWin = p->pMWin;
153347 int ret = 0;
153348 Vdbe *v = p->pVdbe;
153349 int addrContinue = 0;
153350 int bPeer = (pMWin->eFrmType!=TK_ROWS);
153351
153352 int lblDone = sqlite3VdbeMakeLabel(pParse);
153353 int addrNextRange = 0;
153354
153355 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
153356 ** starts with UNBOUNDED PRECEDING. */
153357 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
153358 assert( regCountdown==0 && jumpOnEof==0 );
153359 return 0;
153360 }
153361
153362 if( regCountdown>0 ){
153363 if( pMWin->eFrmType==TK_RANGE ){
153364 addrNextRange = sqlite3VdbeCurrentAddr(v);
153365 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
153366 if( op==WINDOW_AGGINVERSE ){
153367 if( pMWin->eStart==TK_FOLLOWING ){
153368 windowCodeRangeTest(
153369 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
153370 );
153371 }else{
153372 windowCodeRangeTest(
153373 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
153374 );
153375 }
153376 }else{
153377 windowCodeRangeTest(
153378 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
153379 );
153380 }
153381 }else{
153382 sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, lblDone, 1);
153383 VdbeCoverage(v);
153384 }
153385 }
153386
153387 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
153388 windowAggFinal(p, 0);
153389 }
153390 addrContinue = sqlite3VdbeCurrentAddr(v);
153391
153392 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
153393 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
153394 ** start cursor does not advance past the end cursor within the
153395 ** temporary table. It otherwise might, if (a>b). */
153396 if( pMWin->eStart==pMWin->eEnd && regCountdown
153397 && pMWin->eFrmType==TK_RANGE && op==WINDOW_AGGINVERSE
153398 ){
153399 int regRowid1 = sqlite3GetTempReg(pParse);
153400 int regRowid2 = sqlite3GetTempReg(pParse);
153401 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
153402 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
153403 sqlite3VdbeAddOp3(v, OP_Ge, regRowid2, lblDone, regRowid1);
153404 VdbeCoverage(v);
153405 sqlite3ReleaseTempReg(pParse, regRowid1);
153406 sqlite3ReleaseTempReg(pParse, regRowid2);
153407 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
153408 }
153409
153410 switch( op ){
153411 case WINDOW_RETURN_ROW:
153412 csr = p->current.csr;
153413 reg = p->current.reg;
153414 windowReturnOneRow(p);
153415 break;
153416
153417 case WINDOW_AGGINVERSE:
153418 csr = p->start.csr;
153419 reg = p->start.reg;
153420 if( pMWin->regStartRowid ){
153421 assert( pMWin->regEndRowid );
153422 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
153423 }else{
153424 windowAggStep(p, pMWin, csr, 1, p->regArg);
153425 }
153426 break;
153427
153428 default:
153429 assert( op==WINDOW_AGGSTEP );
153430 csr = p->end.csr;
153431 reg = p->end.reg;
153432 if( pMWin->regStartRowid ){
153433 assert( pMWin->regEndRowid );
153434 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
153435 }else{
153436 windowAggStep(p, pMWin, csr, 0, p->regArg);
153437 }
153438 break;
153439 }
153440
153441 if( op==p->eDelete ){
153442 sqlite3VdbeAddOp1(v, OP_Delete, csr);
153443 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
153444 }
153445
153446 if( jumpOnEof ){
153447 sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
153448 VdbeCoverage(v);
153449 ret = sqlite3VdbeAddOp0(v, OP_Goto);
153450 }else{
153451 sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
153452 VdbeCoverage(v);
153453 if( bPeer ){
153454 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblDone);
153455 }
153456 }
153457
153458 if( bPeer ){
153459 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
153460 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
153461 windowReadPeerValues(p, csr, regTmp);
153462 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
153463 sqlite3ReleaseTempRange(pParse, regTmp, nReg);
153464 }
153465
153466 if( addrNextRange ){
153467 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
153468 }
153469 sqlite3VdbeResolveLabel(v, lblDone);
153470 return ret;
153471}
153472
153473
153474/*
153475** Allocate and return a duplicate of the Window object indicated by the
153476** third argument. Set the Window.pOwner field of the new object to
153477** pOwner.
153478*/
153479SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
153480 Window *pNew = 0;
153481 if( ALWAYS(p) ){
153482 pNew = sqlite3DbMallocZero(db, sizeof(Window));
153483 if( pNew ){
153484 pNew->zName = sqlite3DbStrDup(db, p->zName);
153485 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
153486 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
153487 pNew->pFunc = p->pFunc;
153488 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
153489 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
153490 pNew->eFrmType = p->eFrmType;
153491 pNew->eEnd = p->eEnd;
153492 pNew->eStart = p->eStart;
153493 pNew->eExclude = p->eExclude;
153494 pNew->regResult = p->regResult;
153495 pNew->regAccum = p->regAccum;
153496 pNew->iArgCol = p->iArgCol;
153497 pNew->iEphCsr = p->iEphCsr;
153498 pNew->bExprArgs = p->bExprArgs;
153499 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
153500 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
153501 pNew->pOwner = pOwner;
153502 pNew->bImplicitFrame = p->bImplicitFrame;
153503 }
153504 }
153505 return pNew;
153506}
153507
153508/*
153509** Return a copy of the linked list of Window objects passed as the
153510** second argument.
153511*/
153512SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
153513 Window *pWin;
153514 Window *pRet = 0;
153515 Window **pp = &pRet;
153516
153517 for(pWin=p; pWin; pWin=pWin->pNextWin){
153518 *pp = sqlite3WindowDup(db, 0, pWin);
153519 if( *pp==0 ) break;
153520 pp = &((*pp)->pNextWin);
153521 }
153522
153523 return pRet;
153524}
153525
153526/*
153527** Return true if it can be determined at compile time that expression
153528** pExpr evaluates to a value that, when cast to an integer, is greater
153529** than zero. False otherwise.
153530**
153531** If an OOM error occurs, this function sets the Parse.db.mallocFailed
153532** flag and returns zero.
153533*/
153534static int windowExprGtZero(Parse *pParse, Expr *pExpr){
153535 int ret = 0;
153536 sqlite3 *db = pParse->db;
153537 sqlite3_value *pVal = 0;
153538 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
153539 if( pVal && sqlite3_value_int(pVal)>0 ){
153540 ret = 1;
153541 }
153542 sqlite3ValueFree(pVal);
153543 return ret;
153544}
153545
153546/*
153547** sqlite3WhereBegin() has already been called for the SELECT statement
153548** passed as the second argument when this function is invoked. It generates
153549** code to populate the Window.regResult register for each window function
153550** and invoke the sub-routine at instruction addrGosub once for each row.
153551** sqlite3WhereEnd() is always called before returning.
153552**
153553** This function handles several different types of window frames, which
153554** require slightly different processing. The following pseudo code is
153555** used to implement window frames of the form:
153556**
153557** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
153558**
153559** Other window frame types use variants of the following:
153560**
153561** ... loop started by sqlite3WhereBegin() ...
153562** if( new partition ){
153563** Gosub flush
153564** }
153565** Insert new row into eph table.
153566**
153567** if( first row of partition ){
153568** // Rewind three cursors, all open on the eph table.
153569** Rewind(csrEnd);
153570** Rewind(csrStart);
153571** Rewind(csrCurrent);
153572**
153573** regEnd = <expr2> // FOLLOWING expression
153574** regStart = <expr1> // PRECEDING expression
153575** }else{
153576** // First time this branch is taken, the eph table contains two
153577** // rows. The first row in the partition, which all three cursors
153578** // currently point to, and the following row.
153579** AGGSTEP
153580** if( (regEnd--)<=0 ){
153581** RETURN_ROW
153582** if( (regStart--)<=0 ){
153583** AGGINVERSE
153584** }
153585** }
153586** }
153587** }
153588** flush:
153589** AGGSTEP
153590** while( 1 ){
153591** RETURN ROW
153592** if( csrCurrent is EOF ) break;
153593** if( (regStart--)<=0 ){
153594** AggInverse(csrStart)
153595** Next(csrStart)
153596** }
153597** }
153598**
153599** The pseudo-code above uses the following shorthand:
153600**
153601** AGGSTEP: invoke the aggregate xStep() function for each window function
153602** with arguments read from the current row of cursor csrEnd, then
153603** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
153604**
153605** RETURN_ROW: return a row to the caller based on the contents of the
153606** current row of csrCurrent and the current state of all
153607** aggregates. Then step cursor csrCurrent forward one row.
153608**
153609** AGGINVERSE: invoke the aggregate xInverse() function for each window
153610** functions with arguments read from the current row of cursor
153611** csrStart. Then step csrStart forward one row.
153612**
153613** There are two other ROWS window frames that are handled significantly
153614** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
153615** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
153616** cases because they change the order in which the three cursors (csrStart,
153617** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
153618** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
153619** three.
153620**
153621** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
153622**
153623** ... loop started by sqlite3WhereBegin() ...
153624** if( new partition ){
153625** Gosub flush
153626** }
153627** Insert new row into eph table.
153628** if( first row of partition ){
153629** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153630** regEnd = <expr2>
153631** regStart = <expr1>
153632** }else{
153633** if( (regEnd--)<=0 ){
153634** AGGSTEP
153635** }
153636** RETURN_ROW
153637** if( (regStart--)<=0 ){
153638** AGGINVERSE
153639** }
153640** }
153641** }
153642** flush:
153643** if( (regEnd--)<=0 ){
153644** AGGSTEP
153645** }
153646** RETURN_ROW
153647**
153648**
153649** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153650**
153651** ... loop started by sqlite3WhereBegin() ...
153652** if( new partition ){
153653** Gosub flush
153654** }
153655** Insert new row into eph table.
153656** if( first row of partition ){
153657** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153658** regEnd = <expr2>
153659** regStart = regEnd - <expr1>
153660** }else{
153661** AGGSTEP
153662** if( (regEnd--)<=0 ){
153663** RETURN_ROW
153664** }
153665** if( (regStart--)<=0 ){
153666** AGGINVERSE
153667** }
153668** }
153669** }
153670** flush:
153671** AGGSTEP
153672** while( 1 ){
153673** if( (regEnd--)<=0 ){
153674** RETURN_ROW
153675** if( eof ) break;
153676** }
153677** if( (regStart--)<=0 ){
153678** AGGINVERSE
153679** if( eof ) break
153680** }
153681** }
153682** while( !eof csrCurrent ){
153683** RETURN_ROW
153684** }
153685**
153686** For the most part, the patterns above are adapted to support UNBOUNDED by
153687** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
153688** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
153689** This is optimized of course - branches that will never be taken and
153690** conditions that are always true are omitted from the VM code. The only
153691** exceptional case is:
153692**
153693** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
153694**
153695** ... loop started by sqlite3WhereBegin() ...
153696** if( new partition ){
153697** Gosub flush
153698** }
153699** Insert new row into eph table.
153700** if( first row of partition ){
153701** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153702** regStart = <expr1>
153703** }else{
153704** AGGSTEP
153705** }
153706** }
153707** flush:
153708** AGGSTEP
153709** while( 1 ){
153710** if( (regStart--)<=0 ){
153711** AGGINVERSE
153712** if( eof ) break
153713** }
153714** RETURN_ROW
153715** }
153716** while( !eof csrCurrent ){
153717** RETURN_ROW
153718** }
153719**
153720** Also requiring special handling are the cases:
153721**
153722** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
153723** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153724**
153725** when (expr1 < expr2). This is detected at runtime, not by this function.
153726** To handle this case, the pseudo-code programs depicted above are modified
153727** slightly to be:
153728**
153729** ... loop started by sqlite3WhereBegin() ...
153730** if( new partition ){
153731** Gosub flush
153732** }
153733** Insert new row into eph table.
153734** if( first row of partition ){
153735** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153736** regEnd = <expr2>
153737** regStart = <expr1>
153738** if( regEnd < regStart ){
153739** RETURN_ROW
153740** delete eph table contents
153741** continue
153742** }
153743** ...
153744**
153745** The new "continue" statement in the above jumps to the next iteration
153746** of the outer loop - the one started by sqlite3WhereBegin().
153747**
153748** The various GROUPS cases are implemented using the same patterns as
153749** ROWS. The VM code is modified slightly so that:
153750**
153751** 1. The else branch in the main loop is only taken if the row just
153752** added to the ephemeral table is the start of a new group. In
153753** other words, it becomes:
153754**
153755** ... loop started by sqlite3WhereBegin() ...
153756** if( new partition ){
153757** Gosub flush
153758** }
153759** Insert new row into eph table.
153760** if( first row of partition ){
153761** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153762** regEnd = <expr2>
153763** regStart = <expr1>
153764** }else if( new group ){
153765** ...
153766** }
153767** }
153768**
153769** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
153770** AGGINVERSE step processes the current row of the relevant cursor and
153771** all subsequent rows belonging to the same group.
153772**
153773** RANGE window frames are a little different again. As for GROUPS, the
153774** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
153775** deal in groups instead of rows. As for ROWS and GROUPS, there are three
153776** basic cases:
153777**
153778** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
153779**
153780** ... loop started by sqlite3WhereBegin() ...
153781** if( new partition ){
153782** Gosub flush
153783** }
153784** Insert new row into eph table.
153785** if( first row of partition ){
153786** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153787** regEnd = <expr2>
153788** regStart = <expr1>
153789** }else{
153790** AGGSTEP
153791** while( (csrCurrent.key + regEnd) < csrEnd.key ){
153792** RETURN_ROW
153793** while( csrStart.key + regStart) < csrCurrent.key ){
153794** AGGINVERSE
153795** }
153796** }
153797** }
153798** }
153799** flush:
153800** AGGSTEP
153801** while( 1 ){
153802** RETURN ROW
153803** if( csrCurrent is EOF ) break;
153804** while( csrStart.key + regStart) < csrCurrent.key ){
153805** AGGINVERSE
153806** }
153807** }
153808** }
153809**
153810** In the above notation, "csr.key" means the current value of the ORDER BY
153811** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
153812** or <expr PRECEDING) read from cursor csr.
153813**
153814** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
153815**
153816** ... loop started by sqlite3WhereBegin() ...
153817** if( new partition ){
153818** Gosub flush
153819** }
153820** Insert new row into eph table.
153821** if( first row of partition ){
153822** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153823** regEnd = <expr2>
153824** regStart = <expr1>
153825** }else{
153826** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
153827** AGGSTEP
153828** }
153829** while( (csrStart.key + regStart) < csrCurrent.key ){
153830** AGGINVERSE
153831** }
153832** RETURN_ROW
153833** }
153834** }
153835** flush:
153836** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
153837** AGGSTEP
153838** }
153839** while( (csrStart.key + regStart) < csrCurrent.key ){
153840** AGGINVERSE
153841** }
153842** RETURN_ROW
153843**
153844** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
153845**
153846** ... loop started by sqlite3WhereBegin() ...
153847** if( new partition ){
153848** Gosub flush
153849** }
153850** Insert new row into eph table.
153851** if( first row of partition ){
153852** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
153853** regEnd = <expr2>
153854** regStart = <expr1>
153855** }else{
153856** AGGSTEP
153857** while( (csrCurrent.key + regEnd) < csrEnd.key ){
153858** while( (csrCurrent.key + regStart) > csrStart.key ){
153859** AGGINVERSE
153860** }
153861** RETURN_ROW
153862** }
153863** }
153864** }
153865** flush:
153866** AGGSTEP
153867** while( 1 ){
153868** while( (csrCurrent.key + regStart) > csrStart.key ){
153869** AGGINVERSE
153870** if( eof ) break "while( 1 )" loop.
153871** }
153872** RETURN_ROW
153873** }
153874** while( !eof csrCurrent ){
153875** RETURN_ROW
153876** }
153877**
153878** The text above leaves out many details. Refer to the code and comments
153879** below for a more complete picture.
153880*/
153881SQLITE_PRIVATE void sqlite3WindowCodeStep(
153882 Parse *pParse, /* Parse context */
153883 Select *p, /* Rewritten SELECT statement */
153884 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
153885 int regGosub, /* Register for OP_Gosub */
153886 int addrGosub /* OP_Gosub here to return each row */
153887){
153888 Window *pMWin = p->pWin;
153889 ExprList *pOrderBy = pMWin->pOrderBy;
153890 Vdbe *v = sqlite3GetVdbe(pParse);
153891 int csrWrite; /* Cursor used to write to eph. table */
153892 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
153893 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
153894 int iInput; /* To iterate through sub cols */
153895 int addrNe; /* Address of OP_Ne */
153896 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
153897 int addrInteger = 0; /* Address of OP_Integer */
153898 int addrEmpty; /* Address of OP_Rewind in flush: */
153899 int regNew; /* Array of registers holding new input row */
153900 int regRecord; /* regNew array in record form */
153901 int regRowid; /* Rowid for regRecord in eph table */
153902 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
153903 int regPeer = 0; /* Peer values for current row */
153904 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
153905 WindowCodeArg s; /* Context object for sub-routines */
153906 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
153907 int regStart = 0; /* Value of <expr> PRECEDING */
153908 int regEnd = 0; /* Value of <expr> FOLLOWING */
153909
153910 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
153911 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
153912 );
153913 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
153914 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
153915 );
153916 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
153917 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
153918 || pMWin->eExclude==TK_NO
153919 );
153920
153921 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
153922
153923 /* Fill in the context object */
153924 memset(&s, 0, sizeof(WindowCodeArg));
153925 s.pParse = pParse;
153926 s.pMWin = pMWin;
153927 s.pVdbe = v;
153928 s.regGosub = regGosub;
153929 s.addrGosub = addrGosub;
153930 s.current.csr = pMWin->iEphCsr;
153931 csrWrite = s.current.csr+1;
153932 s.start.csr = s.current.csr+2;
153933 s.end.csr = s.current.csr+3;
153934
153935 /* Figure out when rows may be deleted from the ephemeral table. There
153936 ** are four options - they may never be deleted (eDelete==0), they may
153937 ** be deleted as soon as they are no longer part of the window frame
153938 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
153939 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
153940 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
153941 switch( pMWin->eStart ){
153942 case TK_FOLLOWING:
153943 if( pMWin->eFrmType!=TK_RANGE
153944 && windowExprGtZero(pParse, pMWin->pStart)
153945 ){
153946 s.eDelete = WINDOW_RETURN_ROW;
153947 }
153948 break;
153949 case TK_UNBOUNDED:
153950 if( windowCacheFrame(pMWin)==0 ){
153951 if( pMWin->eEnd==TK_PRECEDING ){
153952 if( pMWin->eFrmType!=TK_RANGE
153953 && windowExprGtZero(pParse, pMWin->pEnd)
153954 ){
153955 s.eDelete = WINDOW_AGGSTEP;
153956 }
153957 }else{
153958 s.eDelete = WINDOW_RETURN_ROW;
153959 }
153960 }
153961 break;
153962 default:
153963 s.eDelete = WINDOW_AGGINVERSE;
153964 break;
153965 }
153966
153967 /* Allocate registers for the array of values from the sub-query, the
153968 ** samve values in record form, and the rowid used to insert said record
153969 ** into the ephemeral table. */
153970 regNew = pParse->nMem+1;
153971 pParse->nMem += nInput;
153972 regRecord = ++pParse->nMem;
153973 regRowid = ++pParse->nMem;
153974
153975 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
153976 ** clause, allocate registers to store the results of evaluating each
153977 ** <expr>. */
153978 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
153979 regStart = ++pParse->nMem;
153980 }
153981 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
153982 regEnd = ++pParse->nMem;
153983 }
153984
153985 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
153986 ** registers to store copies of the ORDER BY expressions (peer values)
153987 ** for the main loop, and for each cursor (start, current and end). */
153988 if( pMWin->eFrmType!=TK_ROWS ){
153989 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
153990 regNewPeer = regNew + pMWin->nBufferCol;
153991 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
153992 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
153993 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
153994 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
153995 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
153996 }
153997
153998 /* Load the column values for the row returned by the sub-select
153999 ** into an array of registers starting at regNew. Assemble them into
154000 ** a record in register regRecord. */
154001 for(iInput=0; iInput<nInput; iInput++){
154002 sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
154003 }
154004 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
154005
154006 /* An input row has just been read into an array of registers starting
154007 ** at regNew. If the window has a PARTITION clause, this block generates
154008 ** VM code to check if the input row is the start of a new partition.
154009 ** If so, it does an OP_Gosub to an address to be filled in later. The
154010 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
154011 if( pMWin->pPartition ){
154012 int addr;
154013 ExprList *pPart = pMWin->pPartition;
154014 int nPart = pPart->nExpr;
154015 int regNewPart = regNew + pMWin->nBufferCol;
154016 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
154017
154018 regFlushPart = ++pParse->nMem;
154019 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
154020 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
154021 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
154022 VdbeCoverageEqNe(v);
154023 addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
154024 VdbeComment((v, "call flush_partition"));
154025 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
154026 }
154027
154028 /* Insert the new row into the ephemeral table */
154029 sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
154030 sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
154031 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
154032 VdbeCoverageNeverNull(v);
154033
154034 /* This block is run for the first row of each partition */
154035 s.regArg = windowInitAccum(pParse, pMWin);
154036
154037 if( regStart ){
154038 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
154039 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
154040 }
154041 if( regEnd ){
154042 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
154043 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
154044 }
154045
154046 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
154047 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
154048 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
154049 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
154050 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
154051 windowAggFinal(&s, 0);
154052 sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
154053 VdbeCoverageNeverTaken(v);
154054 windowReturnOneRow(&s);
154055 sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
154056 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
154057 sqlite3VdbeJumpHere(v, addrGe);
154058 }
154059 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
154060 assert( pMWin->eEnd==TK_FOLLOWING );
154061 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
154062 }
154063
154064 if( pMWin->eStart!=TK_UNBOUNDED ){
154065 sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
154066 VdbeCoverageNeverTaken(v);
154067 }
154068 sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
154069 VdbeCoverageNeverTaken(v);
154070 sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
154071 VdbeCoverageNeverTaken(v);
154072 if( regPeer && pOrderBy ){
154073 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
154074 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
154075 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
154076 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
154077 }
154078
154079 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
154080
154081 sqlite3VdbeJumpHere(v, addrNe);
154082
154083 /* Beginning of the block executed for the second and subsequent rows. */
154084 if( regPeer ){
154085 windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
154086 }
154087 if( pMWin->eStart==TK_FOLLOWING ){
154088 windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
154089 if( pMWin->eEnd!=TK_UNBOUNDED ){
154090 if( pMWin->eFrmType==TK_RANGE ){
154091 int lbl = sqlite3VdbeMakeLabel(pParse);
154092 int addrNext = sqlite3VdbeCurrentAddr(v);
154093 windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
154094 windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154095 windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
154096 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
154097 sqlite3VdbeResolveLabel(v, lbl);
154098 }else{
154099 windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
154100 windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154101 }
154102 }
154103 }else
154104 if( pMWin->eEnd==TK_PRECEDING ){
154105 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
154106 windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
154107 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154108 windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
154109 if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154110 }else{
154111 int addr = 0;
154112 windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
154113 if( pMWin->eEnd!=TK_UNBOUNDED ){
154114 if( pMWin->eFrmType==TK_RANGE ){
154115 int lbl = 0;
154116 addr = sqlite3VdbeCurrentAddr(v);
154117 if( regEnd ){
154118 lbl = sqlite3VdbeMakeLabel(pParse);
154119 windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
154120 }
154121 windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
154122 windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154123 if( regEnd ){
154124 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
154125 sqlite3VdbeResolveLabel(v, lbl);
154126 }
154127 }else{
154128 if( regEnd ){
154129 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
154130 VdbeCoverage(v);
154131 }
154132 windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
154133 windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154134 if( regEnd ) sqlite3VdbeJumpHere(v, addr);
154135 }
154136 }
154137 }
154138
154139 /* End of the main input loop */
154140 sqlite3VdbeResolveLabel(v, lblWhereEnd);
154141 sqlite3WhereEnd(pWInfo);
154142
154143 /* Fall through */
154144 if( pMWin->pPartition ){
154145 addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
154146 sqlite3VdbeJumpHere(v, addrGosubFlush);
154147 }
154148
154149 addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
154150 VdbeCoverage(v);
154151 if( pMWin->eEnd==TK_PRECEDING ){
154152 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
154153 windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
154154 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154155 windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
154156 }else if( pMWin->eStart==TK_FOLLOWING ){
154157 int addrStart;
154158 int addrBreak1;
154159 int addrBreak2;
154160 int addrBreak3;
154161 windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
154162 if( pMWin->eFrmType==TK_RANGE ){
154163 addrStart = sqlite3VdbeCurrentAddr(v);
154164 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
154165 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
154166 }else
154167 if( pMWin->eEnd==TK_UNBOUNDED ){
154168 addrStart = sqlite3VdbeCurrentAddr(v);
154169 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
154170 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
154171 }else{
154172 assert( pMWin->eEnd==TK_FOLLOWING );
154173 addrStart = sqlite3VdbeCurrentAddr(v);
154174 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
154175 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
154176 }
154177 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
154178 sqlite3VdbeJumpHere(v, addrBreak2);
154179 addrStart = sqlite3VdbeCurrentAddr(v);
154180 addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
154181 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
154182 sqlite3VdbeJumpHere(v, addrBreak1);
154183 sqlite3VdbeJumpHere(v, addrBreak3);
154184 }else{
154185 int addrBreak;
154186 int addrStart;
154187 windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
154188 addrStart = sqlite3VdbeCurrentAddr(v);
154189 addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
154190 windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
154191 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
154192 sqlite3VdbeJumpHere(v, addrBreak);
154193 }
154194 sqlite3VdbeJumpHere(v, addrEmpty);
154195
154196 sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
154197 if( pMWin->pPartition ){
154198 if( pMWin->regStartRowid ){
154199 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
154200 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
154201 }
154202 sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
154203 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
154204 }
154205}
154206
154207#endif /* SQLITE_OMIT_WINDOWFUNC */
154208
154209/************** End of window.c **********************************************/
154210/************** Begin file parse.c *******************************************/
154211/*
154212** 2000-05-29
154213**
154214** The author disclaims copyright to this source code. In place of
154215** a legal notice, here is a blessing:
154216**
154217** May you do good and not evil.
154218** May you find forgiveness for yourself and forgive others.
154219** May you share freely, never taking more than you give.
154220**
154221*************************************************************************
154222** Driver template for the LEMON parser generator.
154223**
154224** The "lemon" program processes an LALR(1) input grammar file, then uses
154225** this template to construct a parser. The "lemon" program inserts text
154226** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
154227** interstitial "-" characters) contained in this template is changed into
154228** the value of the %name directive from the grammar. Otherwise, the content
154229** of this template is copied straight through into the generate parser
154230** source file.
154231**
154232** The following is the concatenation of all %include directives from the
154233** input grammar file:
154234*/
154235/* #include <stdio.h> */
154236/* #include <assert.h> */
154237/************ Begin %include sections from the grammar ************************/
154238
154239/* #include "sqliteInt.h" */
154240
154241/*
154242** Disable all error recovery processing in the parser push-down
154243** automaton.
154244*/
154245#define YYNOERRORRECOVERY 1
154246
154247/*
154248** Make yytestcase() the same as testcase()
154249*/
154250#define yytestcase(X) testcase(X)
154251
154252/*
154253** Indicate that sqlite3ParserFree() will never be called with a null
154254** pointer.
154255*/
154256#define YYPARSEFREENEVERNULL 1
154257
154258/*
154259** In the amalgamation, the parse.c file generated by lemon and the
154260** tokenize.c file are concatenated. In that case, sqlite3RunParser()
154261** has access to the the size of the yyParser object and so the parser
154262** engine can be allocated from stack. In that case, only the
154263** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
154264** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
154265** omitted.
154266*/
154267#ifdef SQLITE_AMALGAMATION
154268# define sqlite3Parser_ENGINEALWAYSONSTACK 1
154269#endif
154270
154271/*
154272** Alternative datatype for the argument to the malloc() routine passed
154273** into sqlite3ParserAlloc(). The default is size_t.
154274*/
154275#define YYMALLOCARGTYPE u64
154276
154277/*
154278** An instance of the following structure describes the event of a
154279** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
154280** TK_DELETE, or TK_INSTEAD. If the event is of the form
154281**
154282** UPDATE ON (a,b,c)
154283**
154284** Then the "b" IdList records the list "a,b,c".
154285*/
154286struct TrigEvent { int a; IdList * b; };
154287
154288struct FrameBound { int eType; Expr *pExpr; };
154289
154290/*
154291** Disable lookaside memory allocation for objects that might be
154292** shared across database connections.
154293*/
154294static void disableLookaside(Parse *pParse){
154295 sqlite3 *db = pParse->db;
154296 pParse->disableLookaside++;
154297 DisableLookaside;
154298}
154299
154300#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
154301 && defined(SQLITE_UDL_CAPABLE_PARSER)
154302/*
154303** Issue an error message if an ORDER BY or LIMIT clause occurs on an
154304** UPDATE or DELETE statement.
154305*/
154306static void updateDeleteLimitError(
154307 Parse *pParse,
154308 ExprList *pOrderBy,
154309 Expr *pLimit
154310){
154311 if( pOrderBy ){
154312 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
154313 }else{
154314 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
154315 }
154316 sqlite3ExprListDelete(pParse->db, pOrderBy);
154317 sqlite3ExprDelete(pParse->db, pLimit);
154318}
154319#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
154320
154321
154322 /*
154323 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
154324 ** all elements in the list. And make sure list length does not exceed
154325 ** SQLITE_LIMIT_COMPOUND_SELECT.
154326 */
154327 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
154328 assert( p!=0 );
154329 if( p->pPrior ){
154330 Select *pNext = 0, *pLoop;
154331 int mxSelect, cnt = 0;
154332 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
154333 pLoop->pNext = pNext;
154334 pLoop->selFlags |= SF_Compound;
154335 }
154336 if( (p->selFlags & SF_MultiValue)==0 &&
154337 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
154338 cnt>mxSelect
154339 ){
154340 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
154341 }
154342 }
154343 }
154344
154345
154346 /* Construct a new Expr object from a single identifier. Use the
154347 ** new Expr to populate pOut. Set the span of pOut to be the identifier
154348 ** that created the expression.
154349 */
154350 static Expr *tokenExpr(Parse *pParse, int op, Token t){
154351 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
154352 if( p ){
154353 /* memset(p, 0, sizeof(Expr)); */
154354 p->op = (u8)op;
154355 p->affExpr = 0;
154356 p->flags = EP_Leaf;
154357 ExprClearVVAProperties(p);
154358 p->iAgg = -1;
154359 p->pLeft = p->pRight = 0;
154360 p->x.pList = 0;
154361 p->pAggInfo = 0;
154362 p->y.pTab = 0;
154363 p->op2 = 0;
154364 p->iTable = 0;
154365 p->iColumn = 0;
154366 p->u.zToken = (char*)&p[1];
154367 memcpy(p->u.zToken, t.z, t.n);
154368 p->u.zToken[t.n] = 0;
154369 if( sqlite3Isquote(p->u.zToken[0]) ){
154370 sqlite3DequoteExpr(p);
154371 }
154372#if SQLITE_MAX_EXPR_DEPTH>0
154373 p->nHeight = 1;
154374#endif
154375 if( IN_RENAME_OBJECT ){
154376 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
154377 }
154378 }
154379 return p;
154380 }
154381
154382
154383 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
154384 ** unary TK_ISNULL or TK_NOTNULL expression. */
154385 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
154386 sqlite3 *db = pParse->db;
154387 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
154388 pA->op = (u8)op;
154389 sqlite3ExprDelete(db, pA->pRight);
154390 pA->pRight = 0;
154391 }
154392 }
154393
154394 /* Add a single new term to an ExprList that is used to store a
154395 ** list of identifiers. Report an error if the ID list contains
154396 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
154397 ** error while parsing a legacy schema.
154398 */
154399 static ExprList *parserAddExprIdListTerm(
154400 Parse *pParse,
154401 ExprList *pPrior,
154402 Token *pIdToken,
154403 int hasCollate,
154404 int sortOrder
154405 ){
154406 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
154407 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
154408 && pParse->db->init.busy==0
154409 ){
154410 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
154411 pIdToken->n, pIdToken->z);
154412 }
154413 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
154414 return p;
154415 }
154416
154417#if TK_SPAN>255
154418# error too many tokens in the grammar
154419#endif
154420/**************** End of %include directives **********************************/
154421/* These constants specify the various numeric values for terminal symbols
154422** in a format understandable to "makeheaders". This section is blank unless
154423** "lemon" is run with the "-m" command-line option.
154424***************** Begin makeheaders token definitions *************************/
154425/**************** End makeheaders token definitions ***************************/
154426
154427/* The next sections is a series of control #defines.
154428** various aspects of the generated parser.
154429** YYCODETYPE is the data type used to store the integer codes
154430** that represent terminal and non-terminal symbols.
154431** "unsigned char" is used if there are fewer than
154432** 256 symbols. Larger types otherwise.
154433** YYNOCODE is a number of type YYCODETYPE that is not used for
154434** any terminal or nonterminal symbol.
154435** YYFALLBACK If defined, this indicates that one or more tokens
154436** (also known as: "terminal symbols") have fall-back
154437** values which should be used if the original symbol
154438** would not parse. This permits keywords to sometimes
154439** be used as identifiers, for example.
154440** YYACTIONTYPE is the data type used for "action codes" - numbers
154441** that indicate what to do in response to the next
154442** token.
154443** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
154444** symbols. Background: A "minor type" is a semantic
154445** value associated with a terminal or non-terminal
154446** symbols. For example, for an "ID" terminal symbol,
154447** the minor type might be the name of the identifier.
154448** Each non-terminal can have a different minor type.
154449** Terminal symbols all have the same minor type, though.
154450** This macros defines the minor type for terminal
154451** symbols.
154452** YYMINORTYPE is the data type used for all minor types.
154453** This is typically a union of many types, one of
154454** which is sqlite3ParserTOKENTYPE. The entry in the union
154455** for terminal symbols is called "yy0".
154456** YYSTACKDEPTH is the maximum depth of the parser's stack. If
154457** zero the stack is dynamically sized using realloc()
154458** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
154459** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
154460** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
154461** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
154462** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
154463** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
154464** YYERRORSYMBOL is the code number of the error symbol. If not
154465** defined, then do no error processing.
154466** YYNSTATE the combined number of states.
154467** YYNRULE the number of rules in the grammar
154468** YYNTOKEN Number of terminal symbols
154469** YY_MAX_SHIFT Maximum value for shift actions
154470** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
154471** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
154472** YY_ERROR_ACTION The yy_action[] code for syntax error
154473** YY_ACCEPT_ACTION The yy_action[] code for accept
154474** YY_NO_ACTION The yy_action[] code for no-op
154475** YY_MIN_REDUCE Minimum value for reduce actions
154476** YY_MAX_REDUCE Maximum value for reduce actions
154477*/
154478#ifndef INTERFACE
154479# define INTERFACE 1
154480#endif
154481/************* Begin control #defines *****************************************/
154482#define YYCODETYPE unsigned short int
154483#define YYNOCODE 310
154484#define YYACTIONTYPE unsigned short int
154485#define YYWILDCARD 100
154486#define sqlite3ParserTOKENTYPE Token
154487typedef union {
154488 int yyinit;
154489 sqlite3ParserTOKENTYPE yy0;
154490 SrcList* yy47;
154491 u8 yy58;
154492 struct FrameBound yy77;
154493 With* yy131;
154494 int yy192;
154495 Expr* yy202;
154496 struct {int value; int mask;} yy207;
154497 struct TrigEvent yy230;
154498 ExprList* yy242;
154499 Window* yy303;
154500 Upsert* yy318;
154501 const char* yy436;
154502 TriggerStep* yy447;
154503 Select* yy539;
154504 IdList* yy600;
154505} YYMINORTYPE;
154506#ifndef YYSTACKDEPTH
154507#define YYSTACKDEPTH 100
154508#endif
154509#define sqlite3ParserARG_SDECL
154510#define sqlite3ParserARG_PDECL
154511#define sqlite3ParserARG_PARAM
154512#define sqlite3ParserARG_FETCH
154513#define sqlite3ParserARG_STORE
154514#define sqlite3ParserCTX_SDECL Parse *pParse;
154515#define sqlite3ParserCTX_PDECL ,Parse *pParse
154516#define sqlite3ParserCTX_PARAM ,pParse
154517#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
154518#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
154519#define YYFALLBACK 1
154520#define YYNSTATE 553
154521#define YYNRULE 385
154522#define YYNRULE_WITH_ACTION 325
154523#define YYNTOKEN 181
154524#define YY_MAX_SHIFT 552
154525#define YY_MIN_SHIFTREDUCE 803
154526#define YY_MAX_SHIFTREDUCE 1187
154527#define YY_ERROR_ACTION 1188
154528#define YY_ACCEPT_ACTION 1189
154529#define YY_NO_ACTION 1190
154530#define YY_MIN_REDUCE 1191
154531#define YY_MAX_REDUCE 1575
154532/************* End control #defines *******************************************/
154533#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
154534
154535/* Define the yytestcase() macro to be a no-op if is not already defined
154536** otherwise.
154537**
154538** Applications can choose to define yytestcase() in the %include section
154539** to a macro that can assist in verifying code coverage. For production
154540** code the yytestcase() macro should be turned off. But it is useful
154541** for testing.
154542*/
154543#ifndef yytestcase
154544# define yytestcase(X)
154545#endif
154546
154547
154548/* Next are the tables used to determine what action to take based on the
154549** current state and lookahead token. These tables are used to implement
154550** functions that take a state number and lookahead value and return an
154551** action integer.
154552**
154553** Suppose the action integer is N. Then the action is determined as
154554** follows
154555**
154556** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
154557** token onto the stack and goto state N.
154558**
154559** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
154560** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
154561**
154562** N == YY_ERROR_ACTION A syntax error has occurred.
154563**
154564** N == YY_ACCEPT_ACTION The parser accepts its input.
154565**
154566** N == YY_NO_ACTION No such action. Denotes unused
154567** slots in the yy_action[] table.
154568**
154569** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
154570** and YY_MAX_REDUCE
154571**
154572** The action table is constructed as a single large table named yy_action[].
154573** Given state S and lookahead X, the action is computed as either:
154574**
154575** (A) N = yy_action[ yy_shift_ofst[S] + X ]
154576** (B) N = yy_default[S]
154577**
154578** The (A) formula is preferred. The B formula is used instead if
154579** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
154580**
154581** The formulas above are for computing the action when the lookahead is
154582** a terminal symbol. If the lookahead is a non-terminal (as occurs after
154583** a reduce action) then the yy_reduce_ofst[] array is used in place of
154584** the yy_shift_ofst[] array.
154585**
154586** The following are the tables generated in this section:
154587**
154588** yy_action[] A single table containing all actions.
154589** yy_lookahead[] A table containing the lookahead for each entry in
154590** yy_action. Used to detect hash collisions.
154591** yy_shift_ofst[] For each state, the offset into yy_action for
154592** shifting terminals.
154593** yy_reduce_ofst[] For each state, the offset into yy_action for
154594** shifting non-terminals after a reduce.
154595** yy_default[] Default action for each state.
154596**
154597*********** Begin parsing tables **********************************************/
154598#define YY_ACTTAB_COUNT (1962)
154599static const YYACTIONTYPE yy_action[] = {
154600 /* 0 */ 546, 1222, 546, 451, 1260, 546, 1239, 546, 114, 111,
154601 /* 10 */ 211, 546, 1537, 546, 1260, 523, 114, 111, 211, 392,
154602 /* 20 */ 1232, 344, 42, 42, 42, 42, 1225, 42, 42, 71,
154603 /* 30 */ 71, 937, 1224, 71, 71, 71, 71, 1462, 1493, 938,
154604 /* 40 */ 820, 453, 6, 121, 122, 112, 1165, 1165, 1006, 1009,
154605 /* 50 */ 999, 999, 119, 119, 120, 120, 120, 120, 1543, 392,
154606 /* 60 */ 1358, 1517, 552, 2, 1193, 194, 528, 436, 143, 291,
154607 /* 70 */ 528, 136, 528, 371, 261, 504, 272, 385, 1273, 527,
154608 /* 80 */ 503, 493, 164, 121, 122, 112, 1165, 1165, 1006, 1009,
154609 /* 90 */ 999, 999, 119, 119, 120, 120, 120, 120, 1358, 442,
154610 /* 100 */ 1514, 118, 118, 118, 118, 117, 117, 116, 116, 116,
154611 /* 110 */ 115, 424, 266, 266, 266, 266, 1498, 358, 1500, 435,
154612 /* 120 */ 357, 1498, 517, 524, 1485, 543, 1114, 543, 1114, 392,
154613 /* 130 */ 405, 241, 208, 114, 111, 211, 98, 290, 537, 221,
154614 /* 140 */ 1029, 118, 118, 118, 118, 117, 117, 116, 116, 116,
154615 /* 150 */ 115, 424, 1142, 121, 122, 112, 1165, 1165, 1006, 1009,
154616 /* 160 */ 999, 999, 119, 119, 120, 120, 120, 120, 406, 428,
154617 /* 170 */ 117, 117, 116, 116, 116, 115, 424, 1418, 468, 123,
154618 /* 180 */ 118, 118, 118, 118, 117, 117, 116, 116, 116, 115,
154619 /* 190 */ 424, 116, 116, 116, 115, 424, 540, 540, 540, 392,
154620 /* 200 */ 505, 120, 120, 120, 120, 113, 1051, 1142, 1143, 1144,
154621 /* 210 */ 1051, 118, 118, 118, 118, 117, 117, 116, 116, 116,
154622 /* 220 */ 115, 424, 1461, 121, 122, 112, 1165, 1165, 1006, 1009,
154623 /* 230 */ 999, 999, 119, 119, 120, 120, 120, 120, 392, 444,
154624 /* 240 */ 316, 83, 463, 81, 359, 382, 1142, 80, 118, 118,
154625 /* 250 */ 118, 118, 117, 117, 116, 116, 116, 115, 424, 179,
154626 /* 260 */ 434, 424, 121, 122, 112, 1165, 1165, 1006, 1009, 999,
154627 /* 270 */ 999, 119, 119, 120, 120, 120, 120, 434, 433, 266,
154628 /* 280 */ 266, 118, 118, 118, 118, 117, 117, 116, 116, 116,
154629 /* 290 */ 115, 424, 543, 1109, 903, 506, 1142, 114, 111, 211,
154630 /* 300 */ 1431, 1142, 1143, 1144, 206, 491, 1109, 392, 449, 1109,
154631 /* 310 */ 545, 330, 120, 120, 120, 120, 298, 1431, 1433, 17,
154632 /* 320 */ 118, 118, 118, 118, 117, 117, 116, 116, 116, 115,
154633 /* 330 */ 424, 121, 122, 112, 1165, 1165, 1006, 1009, 999, 999,
154634 /* 340 */ 119, 119, 120, 120, 120, 120, 392, 1358, 434, 1142,
154635 /* 350 */ 482, 1142, 1143, 1144, 996, 996, 1007, 1010, 445, 118,
154636 /* 360 */ 118, 118, 118, 117, 117, 116, 116, 116, 115, 424,
154637 /* 370 */ 121, 122, 112, 1165, 1165, 1006, 1009, 999, 999, 119,
154638 /* 380 */ 119, 120, 120, 120, 120, 1054, 1054, 465, 1431, 118,
154639 /* 390 */ 118, 118, 118, 117, 117, 116, 116, 116, 115, 424,
154640 /* 400 */ 1142, 451, 546, 1426, 1142, 1143, 1144, 233, 966, 1142,
154641 /* 410 */ 481, 478, 477, 171, 360, 392, 164, 407, 414, 842,
154642 /* 420 */ 476, 164, 185, 334, 71, 71, 1243, 1000, 118, 118,
154643 /* 430 */ 118, 118, 117, 117, 116, 116, 116, 115, 424, 121,
154644 /* 440 */ 122, 112, 1165, 1165, 1006, 1009, 999, 999, 119, 119,
154645 /* 450 */ 120, 120, 120, 120, 392, 1142, 1143, 1144, 835, 12,
154646 /* 460 */ 314, 509, 163, 356, 1142, 1143, 1144, 114, 111, 211,
154647 /* 470 */ 508, 290, 537, 546, 276, 180, 290, 537, 121, 122,
154648 /* 480 */ 112, 1165, 1165, 1006, 1009, 999, 999, 119, 119, 120,
154649 /* 490 */ 120, 120, 120, 345, 484, 71, 71, 118, 118, 118,
154650 /* 500 */ 118, 117, 117, 116, 116, 116, 115, 424, 1142, 209,
154651 /* 510 */ 411, 523, 1142, 1109, 1571, 378, 252, 269, 342, 487,
154652 /* 520 */ 337, 486, 238, 392, 513, 364, 1109, 1127, 333, 1109,
154653 /* 530 */ 191, 409, 286, 32, 457, 443, 118, 118, 118, 118,
154654 /* 540 */ 117, 117, 116, 116, 116, 115, 424, 121, 122, 112,
154655 /* 550 */ 1165, 1165, 1006, 1009, 999, 999, 119, 119, 120, 120,
154656 /* 560 */ 120, 120, 392, 1142, 1143, 1144, 987, 1142, 1143, 1144,
154657 /* 570 */ 1142, 233, 492, 1492, 481, 478, 477, 6, 163, 546,
154658 /* 580 */ 512, 546, 115, 424, 476, 5, 121, 122, 112, 1165,
154659 /* 590 */ 1165, 1006, 1009, 999, 999, 119, 119, 120, 120, 120,
154660 /* 600 */ 120, 13, 13, 13, 13, 118, 118, 118, 118, 117,
154661 /* 610 */ 117, 116, 116, 116, 115, 424, 403, 502, 408, 546,
154662 /* 620 */ 1486, 544, 1142, 892, 892, 1142, 1143, 1144, 1473, 1142,
154663 /* 630 */ 275, 392, 808, 809, 810, 971, 422, 422, 422, 16,
154664 /* 640 */ 16, 55, 55, 1242, 118, 118, 118, 118, 117, 117,
154665 /* 650 */ 116, 116, 116, 115, 424, 121, 122, 112, 1165, 1165,
154666 /* 660 */ 1006, 1009, 999, 999, 119, 119, 120, 120, 120, 120,
154667 /* 670 */ 392, 1189, 1, 1, 552, 2, 1193, 1142, 1143, 1144,
154668 /* 680 */ 194, 291, 898, 136, 1142, 1143, 1144, 897, 521, 1492,
154669 /* 690 */ 1273, 3, 380, 6, 121, 122, 112, 1165, 1165, 1006,
154670 /* 700 */ 1009, 999, 999, 119, 119, 120, 120, 120, 120, 858,
154671 /* 710 */ 546, 924, 546, 118, 118, 118, 118, 117, 117, 116,
154672 /* 720 */ 116, 116, 115, 424, 266, 266, 1092, 1569, 1142, 551,
154673 /* 730 */ 1569, 1193, 13, 13, 13, 13, 291, 543, 136, 392,
154674 /* 740 */ 485, 421, 420, 966, 344, 1273, 468, 410, 859, 279,
154675 /* 750 */ 140, 221, 118, 118, 118, 118, 117, 117, 116, 116,
154676 /* 760 */ 116, 115, 424, 121, 122, 112, 1165, 1165, 1006, 1009,
154677 /* 770 */ 999, 999, 119, 119, 120, 120, 120, 120, 546, 266,
154678 /* 780 */ 266, 428, 392, 1142, 1143, 1144, 1172, 830, 1172, 468,
154679 /* 790 */ 431, 145, 543, 1146, 401, 314, 439, 302, 838, 1490,
154680 /* 800 */ 71, 71, 412, 6, 1090, 473, 221, 100, 112, 1165,
154681 /* 810 */ 1165, 1006, 1009, 999, 999, 119, 119, 120, 120, 120,
154682 /* 820 */ 120, 118, 118, 118, 118, 117, 117, 116, 116, 116,
154683 /* 830 */ 115, 424, 237, 1425, 546, 451, 428, 287, 986, 546,
154684 /* 840 */ 236, 235, 234, 830, 97, 529, 429, 1265, 1265, 1146,
154685 /* 850 */ 494, 307, 430, 838, 977, 546, 71, 71, 976, 1241,
154686 /* 860 */ 546, 51, 51, 300, 118, 118, 118, 118, 117, 117,
154687 /* 870 */ 116, 116, 116, 115, 424, 194, 103, 70, 70, 266,
154688 /* 880 */ 266, 546, 71, 71, 266, 266, 30, 391, 344, 976,
154689 /* 890 */ 976, 978, 543, 528, 1109, 328, 392, 543, 495, 397,
154690 /* 900 */ 1470, 195, 530, 13, 13, 1358, 240, 1109, 277, 280,
154691 /* 910 */ 1109, 280, 304, 457, 306, 333, 392, 31, 188, 419,
154692 /* 920 */ 121, 122, 112, 1165, 1165, 1006, 1009, 999, 999, 119,
154693 /* 930 */ 119, 120, 120, 120, 120, 142, 392, 365, 457, 986,
154694 /* 940 */ 121, 122, 112, 1165, 1165, 1006, 1009, 999, 999, 119,
154695 /* 950 */ 119, 120, 120, 120, 120, 977, 323, 1142, 326, 976,
154696 /* 960 */ 121, 110, 112, 1165, 1165, 1006, 1009, 999, 999, 119,
154697 /* 970 */ 119, 120, 120, 120, 120, 464, 377, 1185, 118, 118,
154698 /* 980 */ 118, 118, 117, 117, 116, 116, 116, 115, 424, 1142,
154699 /* 990 */ 976, 976, 978, 305, 9, 366, 244, 362, 118, 118,
154700 /* 1000 */ 118, 118, 117, 117, 116, 116, 116, 115, 424, 313,
154701 /* 1010 */ 546, 344, 1142, 1143, 1144, 299, 290, 537, 118, 118,
154702 /* 1020 */ 118, 118, 117, 117, 116, 116, 116, 115, 424, 1263,
154703 /* 1030 */ 1263, 1163, 13, 13, 278, 421, 420, 468, 392, 923,
154704 /* 1040 */ 260, 260, 289, 1169, 1142, 1143, 1144, 189, 1171, 266,
154705 /* 1050 */ 266, 468, 390, 543, 1186, 546, 1170, 263, 144, 489,
154706 /* 1060 */ 922, 546, 543, 122, 112, 1165, 1165, 1006, 1009, 999,
154707 /* 1070 */ 999, 119, 119, 120, 120, 120, 120, 71, 71, 1142,
154708 /* 1080 */ 1172, 1272, 1172, 13, 13, 898, 1070, 1163, 546, 468,
154709 /* 1090 */ 897, 107, 538, 1491, 4, 1268, 1109, 6, 525, 1049,
154710 /* 1100 */ 12, 1071, 1092, 1570, 312, 455, 1570, 520, 541, 1109,
154711 /* 1110 */ 56, 56, 1109, 1489, 423, 1358, 1072, 6, 345, 285,
154712 /* 1120 */ 118, 118, 118, 118, 117, 117, 116, 116, 116, 115,
154713 /* 1130 */ 424, 425, 1271, 321, 1142, 1143, 1144, 878, 266, 266,
154714 /* 1140 */ 1277, 107, 538, 535, 4, 1488, 293, 879, 1211, 6,
154715 /* 1150 */ 210, 543, 543, 164, 294, 496, 416, 204, 541, 267,
154716 /* 1160 */ 267, 1214, 398, 511, 499, 204, 266, 266, 396, 531,
154717 /* 1170 */ 8, 986, 543, 519, 546, 922, 458, 105, 105, 543,
154718 /* 1180 */ 1090, 425, 266, 266, 106, 417, 425, 548, 547, 266,
154719 /* 1190 */ 266, 976, 518, 535, 1373, 543, 15, 15, 266, 266,
154720 /* 1200 */ 456, 1120, 543, 266, 266, 1070, 1372, 515, 290, 537,
154721 /* 1210 */ 546, 543, 514, 97, 444, 316, 543, 546, 922, 125,
154722 /* 1220 */ 1071, 986, 976, 976, 978, 979, 27, 105, 105, 401,
154723 /* 1230 */ 343, 1511, 44, 44, 106, 1072, 425, 548, 547, 57,
154724 /* 1240 */ 57, 976, 343, 1511, 107, 538, 546, 4, 462, 401,
154725 /* 1250 */ 214, 1120, 459, 297, 377, 1091, 534, 1309, 546, 539,
154726 /* 1260 */ 398, 541, 290, 537, 104, 244, 102, 526, 58, 58,
154727 /* 1270 */ 546, 199, 976, 976, 978, 979, 27, 1516, 1131, 427,
154728 /* 1280 */ 59, 59, 270, 237, 425, 138, 95, 375, 375, 374,
154729 /* 1290 */ 255, 372, 60, 60, 817, 1180, 535, 546, 273, 546,
154730 /* 1300 */ 1163, 1308, 389, 388, 546, 438, 546, 215, 210, 296,
154731 /* 1310 */ 515, 849, 546, 265, 208, 516, 1476, 295, 274, 61,
154732 /* 1320 */ 61, 62, 62, 308, 986, 109, 45, 45, 46, 46,
154733 /* 1330 */ 105, 105, 1186, 922, 47, 47, 341, 106, 546, 425,
154734 /* 1340 */ 548, 547, 1542, 546, 976, 867, 340, 217, 546, 937,
154735 /* 1350 */ 397, 107, 538, 218, 4, 156, 1163, 938, 158, 546,
154736 /* 1360 */ 49, 49, 1162, 546, 268, 50, 50, 546, 541, 1450,
154737 /* 1370 */ 63, 63, 546, 1449, 216, 976, 976, 978, 979, 27,
154738 /* 1380 */ 446, 64, 64, 546, 460, 65, 65, 546, 318, 14,
154739 /* 1390 */ 14, 425, 1305, 546, 66, 66, 1087, 546, 141, 379,
154740 /* 1400 */ 38, 546, 963, 535, 322, 127, 127, 546, 393, 67,
154741 /* 1410 */ 67, 546, 325, 290, 537, 52, 52, 515, 546, 68,
154742 /* 1420 */ 68, 845, 514, 69, 69, 399, 165, 857, 856, 53,
154743 /* 1430 */ 53, 986, 311, 151, 151, 97, 432, 105, 105, 327,
154744 /* 1440 */ 152, 152, 526, 1048, 106, 1048, 425, 548, 547, 1131,
154745 /* 1450 */ 427, 976, 1032, 270, 968, 239, 329, 243, 375, 375,
154746 /* 1460 */ 374, 255, 372, 940, 941, 817, 1296, 546, 220, 546,
154747 /* 1470 */ 107, 538, 546, 4, 546, 1256, 199, 845, 215, 1036,
154748 /* 1480 */ 296, 1530, 976, 976, 978, 979, 27, 541, 295, 76,
154749 /* 1490 */ 76, 54, 54, 980, 72, 72, 128, 128, 864, 865,
154750 /* 1500 */ 107, 538, 546, 4, 1047, 546, 1047, 533, 469, 546,
154751 /* 1510 */ 425, 546, 450, 1240, 546, 243, 546, 541, 217, 546,
154752 /* 1520 */ 452, 197, 535, 243, 73, 73, 156, 129, 129, 158,
154753 /* 1530 */ 336, 130, 130, 126, 126, 1036, 150, 150, 149, 149,
154754 /* 1540 */ 425, 134, 134, 317, 474, 216, 97, 239, 331, 980,
154755 /* 1550 */ 986, 97, 535, 346, 347, 546, 105, 105, 902, 931,
154756 /* 1560 */ 546, 895, 243, 106, 109, 425, 548, 547, 546, 1505,
154757 /* 1570 */ 976, 828, 99, 538, 139, 4, 546, 133, 133, 393,
154758 /* 1580 */ 986, 1317, 131, 131, 290, 537, 105, 105, 1357, 541,
154759 /* 1590 */ 132, 132, 1292, 106, 1303, 425, 548, 547, 75, 75,
154760 /* 1600 */ 976, 976, 976, 978, 979, 27, 546, 432, 896, 1289,
154761 /* 1610 */ 532, 109, 425, 1363, 546, 1221, 1213, 1202, 258, 546,
154762 /* 1620 */ 349, 546, 1201, 11, 535, 1203, 1524, 351, 77, 77,
154763 /* 1630 */ 376, 976, 976, 978, 979, 27, 74, 74, 353, 213,
154764 /* 1640 */ 301, 43, 43, 48, 48, 437, 310, 201, 303, 1350,
154765 /* 1650 */ 315, 355, 986, 454, 479, 1239, 339, 192, 105, 105,
154766 /* 1660 */ 1422, 1421, 193, 536, 205, 106, 1527, 425, 548, 547,
154767 /* 1670 */ 1180, 167, 976, 270, 247, 1469, 1467, 1177, 375, 375,
154768 /* 1680 */ 374, 255, 372, 200, 369, 817, 400, 83, 79, 82,
154769 /* 1690 */ 1427, 448, 177, 95, 1342, 161, 169, 1339, 215, 440,
154770 /* 1700 */ 296, 172, 173, 976, 976, 978, 979, 27, 295, 174,
154771 /* 1710 */ 175, 441, 472, 223, 1347, 383, 35, 381, 36, 461,
154772 /* 1720 */ 88, 1353, 181, 447, 384, 1416, 227, 467, 259, 229,
154773 /* 1730 */ 186, 488, 470, 324, 1250, 230, 231, 320, 217, 1204,
154774 /* 1740 */ 1438, 1259, 386, 1258, 413, 90, 156, 849, 1541, 158,
154775 /* 1750 */ 206, 415, 1540, 507, 1300, 1257, 94, 348, 1229, 1301,
154776 /* 1760 */ 387, 1510, 1228, 338, 1227, 216, 350, 1539, 498, 283,
154777 /* 1770 */ 284, 1249, 501, 1299, 352, 245, 246, 418, 1298, 354,
154778 /* 1780 */ 1496, 1495, 124, 10, 526, 363, 101, 1324, 253, 96,
154779 /* 1790 */ 510, 1210, 34, 549, 1137, 254, 256, 257, 166, 393,
154780 /* 1800 */ 550, 1199, 1282, 361, 290, 537, 1281, 196, 367, 368,
154781 /* 1810 */ 1194, 153, 1454, 137, 281, 1323, 1455, 804, 154, 426,
154782 /* 1820 */ 198, 155, 1453, 1452, 292, 212, 202, 432, 1402, 203,
154783 /* 1830 */ 271, 135, 288, 78, 1046, 1044, 960, 168, 157, 881,
154784 /* 1840 */ 170, 219, 309, 222, 1060, 176, 964, 159, 402, 84,
154785 /* 1850 */ 178, 404, 85, 86, 87, 160, 1063, 224, 394, 395,
154786 /* 1860 */ 225, 1059, 146, 18, 226, 319, 243, 1174, 466, 228,
154787 /* 1870 */ 1052, 182, 183, 37, 819, 471, 340, 232, 332, 483,
154788 /* 1880 */ 184, 89, 162, 19, 20, 475, 91, 480, 847, 335,
154789 /* 1890 */ 147, 860, 282, 92, 490, 93, 1125, 148, 1012, 1095,
154790 /* 1900 */ 39, 497, 1096, 40, 500, 262, 207, 264, 930, 187,
154791 /* 1910 */ 925, 109, 1111, 1115, 1113, 7, 1099, 242, 33, 1119,
154792 /* 1920 */ 21, 522, 22, 23, 24, 1118, 25, 190, 97, 26,
154793 /* 1930 */ 1027, 1013, 1011, 1015, 1069, 1016, 1068, 249, 248, 28,
154794 /* 1940 */ 41, 891, 981, 829, 108, 29, 250, 542, 251, 370,
154795 /* 1950 */ 373, 1133, 1132, 1190, 1190, 1190, 1190, 1190, 1190, 1190,
154796 /* 1960 */ 1532, 1531,
154797};
154798static const YYCODETYPE yy_lookahead[] = {
154799 /* 0 */ 189, 211, 189, 189, 218, 189, 220, 189, 267, 268,
154800 /* 10 */ 269, 189, 210, 189, 228, 189, 267, 268, 269, 19,
154801 /* 20 */ 218, 189, 211, 212, 211, 212, 211, 211, 212, 211,
154802 /* 30 */ 212, 31, 211, 211, 212, 211, 212, 288, 300, 39,
154803 /* 40 */ 21, 189, 304, 43, 44, 45, 46, 47, 48, 49,
154804 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 225, 19,
154805 /* 60 */ 189, 183, 184, 185, 186, 189, 248, 263, 236, 191,
154806 /* 70 */ 248, 193, 248, 197, 208, 257, 262, 201, 200, 257,
154807 /* 80 */ 200, 257, 81, 43, 44, 45, 46, 47, 48, 49,
154808 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 189, 80,
154809 /* 100 */ 189, 101, 102, 103, 104, 105, 106, 107, 108, 109,
154810 /* 110 */ 110, 111, 234, 235, 234, 235, 305, 306, 305, 118,
154811 /* 120 */ 307, 305, 306, 297, 298, 247, 86, 247, 88, 19,
154812 /* 130 */ 259, 251, 252, 267, 268, 269, 26, 136, 137, 261,
154813 /* 140 */ 121, 101, 102, 103, 104, 105, 106, 107, 108, 109,
154814 /* 150 */ 110, 111, 59, 43, 44, 45, 46, 47, 48, 49,
154815 /* 160 */ 50, 51, 52, 53, 54, 55, 56, 57, 259, 291,
154816 /* 170 */ 105, 106, 107, 108, 109, 110, 111, 158, 189, 69,
154817 /* 180 */ 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
154818 /* 190 */ 111, 107, 108, 109, 110, 111, 205, 206, 207, 19,
154819 /* 200 */ 19, 54, 55, 56, 57, 58, 29, 114, 115, 116,
154820 /* 210 */ 33, 101, 102, 103, 104, 105, 106, 107, 108, 109,
154821 /* 220 */ 110, 111, 233, 43, 44, 45, 46, 47, 48, 49,
154822 /* 230 */ 50, 51, 52, 53, 54, 55, 56, 57, 19, 126,
154823 /* 240 */ 127, 148, 65, 24, 214, 200, 59, 67, 101, 102,
154824 /* 250 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 22,
154825 /* 260 */ 189, 111, 43, 44, 45, 46, 47, 48, 49, 50,
154826 /* 270 */ 51, 52, 53, 54, 55, 56, 57, 206, 207, 234,
154827 /* 280 */ 235, 101, 102, 103, 104, 105, 106, 107, 108, 109,
154828 /* 290 */ 110, 111, 247, 76, 107, 114, 59, 267, 268, 269,
154829 /* 300 */ 189, 114, 115, 116, 162, 163, 89, 19, 263, 92,
154830 /* 310 */ 189, 23, 54, 55, 56, 57, 189, 206, 207, 22,
154831 /* 320 */ 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
154832 /* 330 */ 111, 43, 44, 45, 46, 47, 48, 49, 50, 51,
154833 /* 340 */ 52, 53, 54, 55, 56, 57, 19, 189, 277, 59,
154834 /* 350 */ 23, 114, 115, 116, 46, 47, 48, 49, 61, 101,
154835 /* 360 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
154836 /* 370 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
154837 /* 380 */ 53, 54, 55, 56, 57, 125, 126, 127, 277, 101,
154838 /* 390 */ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
154839 /* 400 */ 59, 189, 189, 276, 114, 115, 116, 117, 73, 59,
154840 /* 410 */ 120, 121, 122, 72, 214, 19, 81, 259, 19, 23,
154841 /* 420 */ 130, 81, 72, 24, 211, 212, 221, 119, 101, 102,
154842 /* 430 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 43,
154843 /* 440 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
154844 /* 450 */ 54, 55, 56, 57, 19, 114, 115, 116, 23, 208,
154845 /* 460 */ 125, 248, 189, 189, 114, 115, 116, 267, 268, 269,
154846 /* 470 */ 189, 136, 137, 189, 262, 22, 136, 137, 43, 44,
154847 /* 480 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
154848 /* 490 */ 55, 56, 57, 189, 95, 211, 212, 101, 102, 103,
154849 /* 500 */ 104, 105, 106, 107, 108, 109, 110, 111, 59, 189,
154850 /* 510 */ 111, 189, 59, 76, 294, 295, 117, 118, 119, 120,
154851 /* 520 */ 121, 122, 123, 19, 87, 189, 89, 23, 129, 92,
154852 /* 530 */ 279, 227, 248, 22, 189, 284, 101, 102, 103, 104,
154853 /* 540 */ 105, 106, 107, 108, 109, 110, 111, 43, 44, 45,
154854 /* 550 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
154855 /* 560 */ 56, 57, 19, 114, 115, 116, 23, 114, 115, 116,
154856 /* 570 */ 59, 117, 299, 300, 120, 121, 122, 304, 189, 189,
154857 /* 580 */ 143, 189, 110, 111, 130, 22, 43, 44, 45, 46,
154858 /* 590 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
154859 /* 600 */ 57, 211, 212, 211, 212, 101, 102, 103, 104, 105,
154860 /* 610 */ 106, 107, 108, 109, 110, 111, 226, 189, 226, 189,
154861 /* 620 */ 298, 132, 59, 134, 135, 114, 115, 116, 189, 59,
154862 /* 630 */ 285, 19, 7, 8, 9, 23, 205, 206, 207, 211,
154863 /* 640 */ 212, 211, 212, 221, 101, 102, 103, 104, 105, 106,
154864 /* 650 */ 107, 108, 109, 110, 111, 43, 44, 45, 46, 47,
154865 /* 660 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
154866 /* 670 */ 19, 181, 182, 183, 184, 185, 186, 114, 115, 116,
154867 /* 680 */ 189, 191, 133, 193, 114, 115, 116, 138, 299, 300,
154868 /* 690 */ 200, 22, 201, 304, 43, 44, 45, 46, 47, 48,
154869 /* 700 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 35,
154870 /* 710 */ 189, 141, 189, 101, 102, 103, 104, 105, 106, 107,
154871 /* 720 */ 108, 109, 110, 111, 234, 235, 22, 23, 59, 184,
154872 /* 730 */ 26, 186, 211, 212, 211, 212, 191, 247, 193, 19,
154873 /* 740 */ 66, 105, 106, 73, 189, 200, 189, 226, 74, 226,
154874 /* 750 */ 22, 261, 101, 102, 103, 104, 105, 106, 107, 108,
154875 /* 760 */ 109, 110, 111, 43, 44, 45, 46, 47, 48, 49,
154876 /* 770 */ 50, 51, 52, 53, 54, 55, 56, 57, 189, 234,
154877 /* 780 */ 235, 291, 19, 114, 115, 116, 150, 59, 152, 189,
154878 /* 790 */ 233, 236, 247, 59, 189, 125, 126, 127, 59, 300,
154879 /* 800 */ 211, 212, 128, 304, 100, 19, 261, 156, 45, 46,
154880 /* 810 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
154881 /* 820 */ 57, 101, 102, 103, 104, 105, 106, 107, 108, 109,
154882 /* 830 */ 110, 111, 46, 233, 189, 189, 291, 248, 99, 189,
154883 /* 840 */ 125, 126, 127, 115, 26, 200, 289, 230, 231, 115,
154884 /* 850 */ 200, 16, 189, 114, 115, 189, 211, 212, 119, 221,
154885 /* 860 */ 189, 211, 212, 258, 101, 102, 103, 104, 105, 106,
154886 /* 870 */ 107, 108, 109, 110, 111, 189, 156, 211, 212, 234,
154887 /* 880 */ 235, 189, 211, 212, 234, 235, 22, 201, 189, 150,
154888 /* 890 */ 151, 152, 247, 248, 76, 16, 19, 247, 248, 113,
154889 /* 900 */ 189, 24, 257, 211, 212, 189, 26, 89, 262, 223,
154890 /* 910 */ 92, 225, 77, 189, 79, 129, 19, 53, 226, 248,
154891 /* 920 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
154892 /* 930 */ 53, 54, 55, 56, 57, 236, 19, 271, 189, 99,
154893 /* 940 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
154894 /* 950 */ 53, 54, 55, 56, 57, 115, 77, 59, 79, 119,
154895 /* 960 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
154896 /* 970 */ 53, 54, 55, 56, 57, 259, 22, 23, 101, 102,
154897 /* 980 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 59,
154898 /* 990 */ 150, 151, 152, 158, 22, 244, 24, 246, 101, 102,
154899 /* 1000 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 285,
154900 /* 1010 */ 189, 189, 114, 115, 116, 200, 136, 137, 101, 102,
154901 /* 1020 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 230,
154902 /* 1030 */ 231, 59, 211, 212, 285, 105, 106, 189, 19, 141,
154903 /* 1040 */ 234, 235, 239, 113, 114, 115, 116, 226, 118, 234,
154904 /* 1050 */ 235, 189, 249, 247, 100, 189, 126, 23, 236, 107,
154905 /* 1060 */ 26, 189, 247, 44, 45, 46, 47, 48, 49, 50,
154906 /* 1070 */ 51, 52, 53, 54, 55, 56, 57, 211, 212, 59,
154907 /* 1080 */ 150, 233, 152, 211, 212, 133, 12, 115, 189, 189,
154908 /* 1090 */ 138, 19, 20, 300, 22, 233, 76, 304, 226, 11,
154909 /* 1100 */ 208, 27, 22, 23, 200, 19, 26, 87, 36, 89,
154910 /* 1110 */ 211, 212, 92, 300, 248, 189, 42, 304, 189, 250,
154911 /* 1120 */ 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
154912 /* 1130 */ 111, 59, 200, 233, 114, 115, 116, 63, 234, 235,
154913 /* 1140 */ 235, 19, 20, 71, 22, 300, 189, 73, 200, 304,
154914 /* 1150 */ 116, 247, 247, 81, 189, 200, 227, 26, 36, 234,
154915 /* 1160 */ 235, 203, 204, 143, 200, 26, 234, 235, 194, 200,
154916 /* 1170 */ 48, 99, 247, 66, 189, 141, 284, 105, 106, 247,
154917 /* 1180 */ 100, 59, 234, 235, 112, 259, 114, 115, 116, 234,
154918 /* 1190 */ 235, 119, 85, 71, 266, 247, 211, 212, 234, 235,
154919 /* 1200 */ 114, 94, 247, 234, 235, 12, 266, 85, 136, 137,
154920 /* 1210 */ 189, 247, 90, 26, 126, 127, 247, 189, 26, 22,
154921 /* 1220 */ 27, 99, 150, 151, 152, 153, 154, 105, 106, 189,
154922 /* 1230 */ 302, 303, 211, 212, 112, 42, 114, 115, 116, 211,
154923 /* 1240 */ 212, 119, 302, 303, 19, 20, 189, 22, 274, 189,
154924 /* 1250 */ 15, 144, 278, 189, 22, 23, 63, 189, 189, 203,
154925 /* 1260 */ 204, 36, 136, 137, 155, 24, 157, 143, 211, 212,
154926 /* 1270 */ 189, 140, 150, 151, 152, 153, 154, 0, 1, 2,
154927 /* 1280 */ 211, 212, 5, 46, 59, 161, 147, 10, 11, 12,
154928 /* 1290 */ 13, 14, 211, 212, 17, 60, 71, 189, 258, 189,
154929 /* 1300 */ 59, 189, 105, 106, 189, 189, 189, 30, 116, 32,
154930 /* 1310 */ 85, 124, 189, 251, 252, 90, 189, 40, 258, 211,
154931 /* 1320 */ 212, 211, 212, 189, 99, 26, 211, 212, 211, 212,
154932 /* 1330 */ 105, 106, 100, 141, 211, 212, 119, 112, 189, 114,
154933 /* 1340 */ 115, 116, 23, 189, 119, 26, 129, 70, 189, 31,
154934 /* 1350 */ 113, 19, 20, 24, 22, 78, 115, 39, 81, 189,
154935 /* 1360 */ 211, 212, 26, 189, 22, 211, 212, 189, 36, 189,
154936 /* 1370 */ 211, 212, 189, 189, 97, 150, 151, 152, 153, 154,
154937 /* 1380 */ 127, 211, 212, 189, 189, 211, 212, 189, 189, 211,
154938 /* 1390 */ 212, 59, 189, 189, 211, 212, 23, 189, 22, 26,
154939 /* 1400 */ 24, 189, 149, 71, 189, 211, 212, 189, 131, 211,
154940 /* 1410 */ 212, 189, 189, 136, 137, 211, 212, 85, 189, 211,
154941 /* 1420 */ 212, 59, 90, 211, 212, 292, 293, 118, 119, 211,
154942 /* 1430 */ 212, 99, 23, 211, 212, 26, 159, 105, 106, 189,
154943 /* 1440 */ 211, 212, 143, 150, 112, 152, 114, 115, 116, 1,
154944 /* 1450 */ 2, 119, 23, 5, 23, 26, 189, 26, 10, 11,
154945 /* 1460 */ 12, 13, 14, 83, 84, 17, 253, 189, 139, 189,
154946 /* 1470 */ 19, 20, 189, 22, 189, 189, 140, 115, 30, 59,
154947 /* 1480 */ 32, 139, 150, 151, 152, 153, 154, 36, 40, 211,
154948 /* 1490 */ 212, 211, 212, 59, 211, 212, 211, 212, 7, 8,
154949 /* 1500 */ 19, 20, 189, 22, 150, 189, 152, 231, 281, 189,
154950 /* 1510 */ 59, 189, 23, 189, 189, 26, 189, 36, 70, 189,
154951 /* 1520 */ 23, 237, 71, 26, 211, 212, 78, 211, 212, 81,
154952 /* 1530 */ 189, 211, 212, 211, 212, 115, 211, 212, 211, 212,
154953 /* 1540 */ 59, 211, 212, 23, 23, 97, 26, 26, 23, 115,
154954 /* 1550 */ 99, 26, 71, 189, 189, 189, 105, 106, 107, 23,
154955 /* 1560 */ 189, 23, 26, 112, 26, 114, 115, 116, 189, 309,
154956 /* 1570 */ 119, 23, 19, 20, 26, 22, 189, 211, 212, 131,
154957 /* 1580 */ 99, 189, 211, 212, 136, 137, 105, 106, 189, 36,
154958 /* 1590 */ 211, 212, 189, 112, 189, 114, 115, 116, 211, 212,
154959 /* 1600 */ 119, 150, 151, 152, 153, 154, 189, 159, 23, 250,
154960 /* 1610 */ 189, 26, 59, 189, 189, 189, 189, 189, 280, 189,
154961 /* 1620 */ 250, 189, 189, 238, 71, 189, 189, 250, 211, 212,
154962 /* 1630 */ 187, 150, 151, 152, 153, 154, 211, 212, 250, 290,
154963 /* 1640 */ 240, 211, 212, 211, 212, 254, 286, 209, 254, 241,
154964 /* 1650 */ 240, 254, 99, 286, 215, 220, 214, 244, 105, 106,
154965 /* 1660 */ 214, 214, 244, 273, 224, 112, 192, 114, 115, 116,
154966 /* 1670 */ 60, 290, 119, 5, 139, 196, 196, 38, 10, 11,
154967 /* 1680 */ 12, 13, 14, 238, 240, 17, 196, 148, 287, 287,
154968 /* 1690 */ 276, 113, 22, 147, 241, 43, 229, 241, 30, 18,
154969 /* 1700 */ 32, 232, 232, 150, 151, 152, 153, 154, 40, 232,
154970 /* 1710 */ 232, 196, 18, 195, 265, 265, 264, 241, 264, 196,
154971 /* 1720 */ 155, 229, 229, 241, 241, 241, 195, 62, 196, 195,
154972 /* 1730 */ 22, 113, 216, 196, 222, 195, 195, 282, 70, 196,
154973 /* 1740 */ 283, 213, 216, 213, 64, 22, 78, 124, 219, 81,
154974 /* 1750 */ 162, 111, 219, 142, 256, 213, 113, 255, 213, 256,
154975 /* 1760 */ 216, 303, 215, 213, 213, 97, 255, 213, 216, 275,
154976 /* 1770 */ 275, 222, 216, 256, 255, 196, 91, 82, 256, 255,
154977 /* 1780 */ 308, 308, 146, 22, 143, 196, 155, 260, 25, 145,
154978 /* 1790 */ 144, 199, 26, 198, 13, 190, 190, 6, 293, 131,
154979 /* 1800 */ 188, 188, 245, 244, 136, 137, 245, 243, 242, 241,
154980 /* 1810 */ 188, 202, 208, 217, 217, 260, 208, 4, 202, 3,
154981 /* 1820 */ 22, 202, 208, 208, 160, 15, 209, 159, 270, 209,
154982 /* 1830 */ 98, 16, 272, 208, 23, 23, 137, 148, 128, 20,
154983 /* 1840 */ 140, 24, 16, 142, 1, 140, 149, 128, 61, 53,
154984 /* 1850 */ 148, 37, 53, 53, 53, 128, 114, 34, 296, 296,
154985 /* 1860 */ 139, 1, 5, 22, 113, 158, 26, 75, 41, 139,
154986 /* 1870 */ 68, 68, 113, 24, 20, 19, 129, 123, 23, 96,
154987 /* 1880 */ 22, 22, 37, 22, 22, 67, 22, 67, 59, 24,
154988 /* 1890 */ 23, 28, 67, 147, 22, 26, 23, 23, 23, 23,
154989 /* 1900 */ 22, 24, 23, 22, 24, 23, 139, 23, 114, 22,
154990 /* 1910 */ 141, 26, 88, 75, 86, 44, 23, 34, 22, 75,
154991 /* 1920 */ 34, 24, 34, 34, 34, 93, 34, 26, 26, 34,
154992 /* 1930 */ 23, 23, 23, 23, 23, 11, 23, 22, 26, 22,
154993 /* 1940 */ 22, 133, 23, 23, 22, 22, 139, 26, 139, 23,
154994 /* 1950 */ 15, 1, 1, 310, 310, 310, 310, 310, 310, 310,
154995 /* 1960 */ 139, 139, 310, 310, 310, 310, 310, 310, 310, 310,
154996 /* 1970 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
154997 /* 1980 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
154998 /* 1990 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
154999 /* 2000 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155000 /* 2010 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155001 /* 2020 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155002 /* 2030 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155003 /* 2040 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155004 /* 2050 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155005 /* 2060 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155006 /* 2070 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155007 /* 2080 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155008 /* 2090 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155009 /* 2100 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155010 /* 2110 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155011 /* 2120 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155012 /* 2130 */ 310, 310, 310, 310, 310, 310, 310, 310, 310, 310,
155013 /* 2140 */ 310, 310, 310,
155014};
155015#define YY_SHIFT_COUNT (552)
155016#define YY_SHIFT_MIN (0)
155017#define YY_SHIFT_MAX (1951)
155018static const unsigned short int yy_shift_ofst[] = {
155019 /* 0 */ 1448, 1277, 1668, 1072, 1072, 340, 1122, 1225, 1332, 1481,
155020 /* 10 */ 1481, 1481, 335, 0, 0, 180, 897, 1481, 1481, 1481,
155021 /* 20 */ 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
155022 /* 30 */ 930, 930, 1020, 1020, 290, 1, 340, 340, 340, 340,
155023 /* 40 */ 340, 340, 40, 110, 219, 288, 327, 396, 435, 504,
155024 /* 50 */ 543, 612, 651, 720, 877, 897, 897, 897, 897, 897,
155025 /* 60 */ 897, 897, 897, 897, 897, 897, 897, 897, 897, 897,
155026 /* 70 */ 897, 897, 897, 917, 897, 1019, 763, 763, 1451, 1481,
155027 /* 80 */ 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
155028 /* 90 */ 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
155029 /* 100 */ 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
155030 /* 110 */ 1481, 1481, 1553, 1481, 1481, 1481, 1481, 1481, 1481, 1481,
155031 /* 120 */ 1481, 1481, 1481, 1481, 1481, 1481, 147, 258, 258, 258,
155032 /* 130 */ 258, 258, 79, 65, 84, 449, 19, 786, 449, 636,
155033 /* 140 */ 636, 449, 880, 880, 880, 880, 113, 142, 142, 472,
155034 /* 150 */ 150, 1962, 1962, 399, 399, 399, 93, 237, 341, 237,
155035 /* 160 */ 237, 1074, 1074, 437, 350, 704, 1080, 449, 449, 449,
155036 /* 170 */ 449, 449, 449, 449, 449, 449, 449, 449, 449, 449,
155037 /* 180 */ 449, 449, 449, 449, 449, 449, 449, 449, 818, 818,
155038 /* 190 */ 449, 1088, 217, 217, 734, 734, 1124, 1126, 1962, 1962,
155039 /* 200 */ 1962, 739, 840, 840, 453, 454, 511, 187, 563, 570,
155040 /* 210 */ 898, 669, 449, 449, 449, 449, 449, 449, 449, 449,
155041 /* 220 */ 449, 670, 449, 449, 449, 449, 449, 449, 449, 449,
155042 /* 230 */ 449, 449, 449, 449, 674, 674, 674, 449, 449, 449,
155043 /* 240 */ 449, 1034, 449, 449, 449, 972, 1107, 449, 449, 1193,
155044 /* 250 */ 449, 449, 449, 449, 449, 449, 449, 449, 260, 177,
155045 /* 260 */ 489, 1241, 1241, 1241, 1241, 1192, 489, 489, 952, 1197,
155046 /* 270 */ 625, 1235, 1131, 181, 181, 1086, 1139, 1131, 1086, 1187,
155047 /* 280 */ 1319, 1237, 1318, 1318, 1318, 181, 1299, 1299, 1109, 1336,
155048 /* 290 */ 549, 1376, 1610, 1535, 1535, 1639, 1639, 1535, 1539, 1578,
155049 /* 300 */ 1670, 1546, 1652, 1546, 1681, 1681, 1681, 1681, 1535, 1694,
155050 /* 310 */ 1546, 1546, 1578, 1670, 1652, 1546, 1652, 1546, 1535, 1694,
155051 /* 320 */ 1565, 1665, 1535, 1694, 1708, 1535, 1694, 1535, 1694, 1708,
155052 /* 330 */ 1618, 1618, 1618, 1680, 1723, 1723, 1708, 1618, 1623, 1618,
155053 /* 340 */ 1680, 1618, 1618, 1588, 1708, 1640, 1640, 1708, 1611, 1643,
155054 /* 350 */ 1611, 1643, 1611, 1643, 1611, 1643, 1535, 1685, 1685, 1695,
155055 /* 360 */ 1695, 1636, 1641, 1761, 1535, 1631, 1636, 1644, 1646, 1546,
155056 /* 370 */ 1763, 1766, 1781, 1781, 1791, 1791, 1791, 1962, 1962, 1962,
155057 /* 380 */ 1962, 1962, 1962, 1962, 1962, 1962, 1962, 1962, 1962, 1962,
155058 /* 390 */ 1962, 1962, 308, 835, 954, 1232, 879, 715, 728, 1373,
155059 /* 400 */ 864, 1329, 1253, 1409, 297, 1431, 1489, 1497, 1520, 1521,
155060 /* 410 */ 1525, 1362, 1309, 1491, 1217, 1420, 1429, 1536, 1380, 1538,
155061 /* 420 */ 1293, 1354, 1548, 1585, 1434, 1342, 1813, 1816, 1798, 1664,
155062 /* 430 */ 1810, 1732, 1815, 1811, 1812, 1699, 1689, 1710, 1817, 1700,
155063 /* 440 */ 1819, 1701, 1826, 1843, 1705, 1697, 1719, 1787, 1814, 1702,
155064 /* 450 */ 1796, 1799, 1800, 1801, 1727, 1742, 1823, 1721, 1860, 1857,
155065 /* 460 */ 1841, 1751, 1707, 1802, 1840, 1803, 1792, 1827, 1730, 1759,
155066 /* 470 */ 1849, 1854, 1856, 1747, 1754, 1858, 1818, 1859, 1861, 1855,
155067 /* 480 */ 1862, 1820, 1829, 1865, 1783, 1863, 1864, 1825, 1845, 1867,
155068 /* 490 */ 1746, 1872, 1873, 1874, 1875, 1869, 1876, 1878, 1877, 1879,
155069 /* 500 */ 1881, 1880, 1767, 1882, 1884, 1794, 1883, 1887, 1769, 1885,
155070 /* 510 */ 1886, 1888, 1889, 1890, 1824, 1838, 1828, 1871, 1844, 1832,
155071 /* 520 */ 1892, 1893, 1896, 1897, 1901, 1902, 1895, 1907, 1885, 1908,
155072 /* 530 */ 1909, 1910, 1911, 1912, 1913, 1915, 1924, 1917, 1918, 1919,
155073 /* 540 */ 1920, 1922, 1923, 1921, 1808, 1807, 1809, 1821, 1822, 1926,
155074 /* 550 */ 1935, 1950, 1951,
155075};
155076#define YY_REDUCE_COUNT (391)
155077#define YY_REDUCE_MIN (-262)
155078#define YY_REDUCE_MAX (1625)
155079static const short yy_reduce_ofst[] = {
155080 /* 0 */ 490, -122, 545, 645, 650, -120, -189, -187, -184, -182,
155081 /* 10 */ -178, -176, 45, 30, 200, -251, -134, 390, 392, 521,
155082 /* 20 */ 523, 213, 692, 821, 284, 589, 872, 666, 671, 866,
155083 /* 30 */ 71, 111, 273, 389, 686, 815, 904, 932, 948, 955,
155084 /* 40 */ 964, 969, -259, -259, -259, -259, -259, -259, -259, -259,
155085 /* 50 */ -259, -259, -259, -259, -259, -259, -259, -259, -259, -259,
155086 /* 60 */ -259, -259, -259, -259, -259, -259, -259, -259, -259, -259,
155087 /* 70 */ -259, -259, -259, -259, -259, -259, -259, -259, 428, 430,
155088 /* 80 */ 899, 985, 1021, 1028, 1057, 1069, 1081, 1108, 1110, 1115,
155089 /* 90 */ 1117, 1123, 1149, 1154, 1159, 1170, 1174, 1178, 1183, 1194,
155090 /* 100 */ 1198, 1204, 1208, 1212, 1218, 1222, 1229, 1278, 1280, 1283,
155091 /* 110 */ 1285, 1313, 1316, 1320, 1322, 1325, 1327, 1330, 1366, 1371,
155092 /* 120 */ 1379, 1387, 1417, 1425, 1430, 1432, -259, -259, -259, -259,
155093 /* 130 */ -259, -259, -259, -259, -259, 557, 974, -214, -174, -9,
155094 /* 140 */ 431, -124, 806, 925, 806, 925, 251, 928, 940, -259,
155095 /* 150 */ -259, -259, -259, -198, -198, -198, 127, -186, -168, 212,
155096 /* 160 */ 646, 617, 799, -262, 555, 220, 220, 491, 605, 1040,
155097 /* 170 */ 1060, 699, -11, 600, 848, 862, 345, -129, 724, -91,
155098 /* 180 */ 158, 749, 716, 900, 304, 822, 929, 926, 499, 793,
155099 /* 190 */ 322, 892, 813, 845, 958, 1056, 751, 905, 1133, 1062,
155100 /* 200 */ 803, -210, -185, -179, -148, -167, -89, 121, 274, 281,
155101 /* 210 */ 320, 336, 439, 663, 711, 957, 965, 1064, 1068, 1112,
155102 /* 220 */ 1116, -196, 1127, 1134, 1180, 1184, 1195, 1199, 1203, 1215,
155103 /* 230 */ 1223, 1250, 1267, 1286, 205, 422, 638, 1324, 1341, 1364,
155104 /* 240 */ 1365, 1213, 1392, 1399, 1403, 869, 1260, 1405, 1421, 1276,
155105 /* 250 */ 1424, 121, 1426, 1427, 1428, 1433, 1436, 1437, 1227, 1338,
155106 /* 260 */ 1284, 1359, 1370, 1377, 1388, 1213, 1284, 1284, 1385, 1438,
155107 /* 270 */ 1443, 1349, 1400, 1391, 1394, 1360, 1408, 1410, 1367, 1439,
155108 /* 280 */ 1440, 1435, 1442, 1446, 1447, 1397, 1413, 1418, 1390, 1444,
155109 /* 290 */ 1445, 1474, 1381, 1479, 1480, 1401, 1402, 1490, 1414, 1449,
155110 /* 300 */ 1452, 1453, 1467, 1456, 1469, 1470, 1477, 1478, 1515, 1518,
155111 /* 310 */ 1476, 1482, 1450, 1454, 1492, 1483, 1493, 1484, 1523, 1531,
155112 /* 320 */ 1457, 1455, 1532, 1534, 1516, 1537, 1540, 1543, 1541, 1526,
155113 /* 330 */ 1528, 1530, 1542, 1512, 1529, 1533, 1544, 1545, 1547, 1550,
155114 /* 340 */ 1549, 1551, 1554, 1458, 1552, 1494, 1495, 1556, 1498, 1502,
155115 /* 350 */ 1503, 1511, 1517, 1519, 1522, 1524, 1579, 1472, 1473, 1527,
155116 /* 360 */ 1555, 1557, 1559, 1558, 1589, 1560, 1561, 1564, 1566, 1568,
155117 /* 370 */ 1592, 1595, 1605, 1606, 1612, 1613, 1622, 1562, 1563, 1505,
155118 /* 380 */ 1609, 1604, 1608, 1614, 1615, 1616, 1596, 1597, 1617, 1620,
155119 /* 390 */ 1625, 1619,
155120};
155121static const YYACTIONTYPE yy_default[] = {
155122 /* 0 */ 1575, 1575, 1575, 1411, 1188, 1297, 1188, 1188, 1188, 1411,
155123 /* 10 */ 1411, 1411, 1188, 1327, 1327, 1464, 1219, 1188, 1188, 1188,
155124 /* 20 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1410, 1188, 1188,
155125 /* 30 */ 1188, 1188, 1494, 1494, 1188, 1188, 1188, 1188, 1188, 1188,
155126 /* 40 */ 1188, 1188, 1188, 1336, 1188, 1188, 1188, 1188, 1188, 1188,
155127 /* 50 */ 1412, 1413, 1188, 1188, 1188, 1463, 1465, 1428, 1346, 1345,
155128 /* 60 */ 1344, 1343, 1446, 1314, 1341, 1334, 1338, 1406, 1407, 1405,
155129 /* 70 */ 1409, 1413, 1412, 1188, 1337, 1377, 1391, 1376, 1188, 1188,
155130 /* 80 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155131 /* 90 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155132 /* 100 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155133 /* 110 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155134 /* 120 */ 1188, 1188, 1188, 1188, 1188, 1188, 1385, 1390, 1396, 1389,
155135 /* 130 */ 1386, 1379, 1378, 1380, 1381, 1188, 1209, 1261, 1188, 1188,
155136 /* 140 */ 1188, 1188, 1482, 1481, 1188, 1188, 1219, 1371, 1370, 1382,
155137 /* 150 */ 1383, 1393, 1392, 1471, 1529, 1528, 1429, 1188, 1188, 1188,
155138 /* 160 */ 1188, 1188, 1188, 1494, 1188, 1188, 1188, 1188, 1188, 1188,
155139 /* 170 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155140 /* 180 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1494, 1494,
155141 /* 190 */ 1188, 1219, 1494, 1494, 1215, 1215, 1321, 1188, 1477, 1297,
155142 /* 200 */ 1288, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155143 /* 210 */ 1188, 1188, 1188, 1188, 1188, 1468, 1466, 1188, 1188, 1188,
155144 /* 220 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155145 /* 230 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155146 /* 240 */ 1188, 1188, 1188, 1188, 1188, 1293, 1188, 1188, 1188, 1188,
155147 /* 250 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1523, 1188, 1441,
155148 /* 260 */ 1275, 1293, 1293, 1293, 1293, 1295, 1276, 1274, 1287, 1220,
155149 /* 270 */ 1195, 1567, 1294, 1316, 1316, 1564, 1340, 1294, 1564, 1236,
155150 /* 280 */ 1545, 1231, 1327, 1327, 1327, 1316, 1321, 1321, 1408, 1294,
155151 /* 290 */ 1287, 1188, 1567, 1302, 1302, 1566, 1566, 1302, 1429, 1349,
155152 /* 300 */ 1355, 1340, 1264, 1340, 1270, 1270, 1270, 1270, 1302, 1206,
155153 /* 310 */ 1340, 1340, 1349, 1355, 1264, 1340, 1264, 1340, 1302, 1206,
155154 /* 320 */ 1445, 1561, 1302, 1206, 1419, 1302, 1206, 1302, 1206, 1419,
155155 /* 330 */ 1262, 1262, 1262, 1251, 1188, 1188, 1419, 1262, 1236, 1262,
155156 /* 340 */ 1251, 1262, 1262, 1512, 1419, 1423, 1423, 1419, 1320, 1315,
155157 /* 350 */ 1320, 1315, 1320, 1315, 1320, 1315, 1302, 1504, 1504, 1330,
155158 /* 360 */ 1330, 1335, 1321, 1414, 1302, 1188, 1335, 1333, 1331, 1340,
155159 /* 370 */ 1212, 1254, 1526, 1526, 1522, 1522, 1522, 1572, 1572, 1477,
155160 /* 380 */ 1538, 1219, 1219, 1219, 1219, 1538, 1238, 1238, 1220, 1220,
155161 /* 390 */ 1219, 1538, 1188, 1188, 1188, 1188, 1188, 1188, 1533, 1188,
155162 /* 400 */ 1430, 1306, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155163 /* 410 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155164 /* 420 */ 1188, 1188, 1188, 1188, 1188, 1360, 1188, 1191, 1474, 1188,
155165 /* 430 */ 1188, 1472, 1188, 1188, 1188, 1188, 1188, 1188, 1307, 1188,
155166 /* 440 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155167 /* 450 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1563, 1188, 1188,
155168 /* 460 */ 1188, 1188, 1188, 1188, 1444, 1443, 1188, 1188, 1304, 1188,
155169 /* 470 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155170 /* 480 */ 1188, 1188, 1234, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155171 /* 490 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155172 /* 500 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1332,
155173 /* 510 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155174 /* 520 */ 1188, 1188, 1188, 1188, 1509, 1322, 1188, 1188, 1554, 1188,
155175 /* 530 */ 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188, 1188,
155176 /* 540 */ 1188, 1188, 1188, 1549, 1278, 1362, 1188, 1361, 1365, 1188,
155177 /* 550 */ 1200, 1188, 1188,
155178};
155179/********** End of lemon-generated parsing tables *****************************/
155180
155181/* The next table maps tokens (terminal symbols) into fallback tokens.
155182** If a construct like the following:
155183**
155184** %fallback ID X Y Z.
155185**
155186** appears in the grammar, then ID becomes a fallback token for X, Y,
155187** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
155188** but it does not parse, the type of the token is changed to ID and
155189** the parse is retried before an error is thrown.
155190**
155191** This feature can be used, for example, to cause some keywords in a language
155192** to revert to identifiers if they keyword does not apply in the context where
155193** it appears.
155194*/
155195#ifdef YYFALLBACK
155196static const YYCODETYPE yyFallback[] = {
155197 0, /* $ => nothing */
155198 0, /* SEMI => nothing */
155199 59, /* EXPLAIN => ID */
155200 59, /* QUERY => ID */
155201 59, /* PLAN => ID */
155202 59, /* BEGIN => ID */
155203 0, /* TRANSACTION => nothing */
155204 59, /* DEFERRED => ID */
155205 59, /* IMMEDIATE => ID */
155206 59, /* EXCLUSIVE => ID */
155207 0, /* COMMIT => nothing */
155208 59, /* END => ID */
155209 59, /* ROLLBACK => ID */
155210 59, /* SAVEPOINT => ID */
155211 59, /* RELEASE => ID */
155212 0, /* TO => nothing */
155213 0, /* TABLE => nothing */
155214 0, /* CREATE => nothing */
155215 59, /* IF => ID */
155216 0, /* NOT => nothing */
155217 0, /* EXISTS => nothing */
155218 59, /* TEMP => ID */
155219 0, /* LP => nothing */
155220 0, /* RP => nothing */
155221 0, /* AS => nothing */
155222 59, /* WITHOUT => ID */
155223 0, /* COMMA => nothing */
155224 59, /* ABORT => ID */
155225 59, /* ACTION => ID */
155226 59, /* AFTER => ID */
155227 59, /* ANALYZE => ID */
155228 59, /* ASC => ID */
155229 59, /* ATTACH => ID */
155230 59, /* BEFORE => ID */
155231 59, /* BY => ID */
155232 59, /* CASCADE => ID */
155233 59, /* CAST => ID */
155234 59, /* CONFLICT => ID */
155235 59, /* DATABASE => ID */
155236 59, /* DESC => ID */
155237 59, /* DETACH => ID */
155238 59, /* EACH => ID */
155239 59, /* FAIL => ID */
155240 0, /* OR => nothing */
155241 0, /* AND => nothing */
155242 0, /* IS => nothing */
155243 59, /* MATCH => ID */
155244 59, /* LIKE_KW => ID */
155245 0, /* BETWEEN => nothing */
155246 0, /* IN => nothing */
155247 0, /* ISNULL => nothing */
155248 0, /* NOTNULL => nothing */
155249 0, /* NE => nothing */
155250 0, /* EQ => nothing */
155251 0, /* GT => nothing */
155252 0, /* LE => nothing */
155253 0, /* LT => nothing */
155254 0, /* GE => nothing */
155255 0, /* ESCAPE => nothing */
155256 0, /* ID => nothing */
155257 59, /* COLUMNKW => ID */
155258 59, /* DO => ID */
155259 59, /* FOR => ID */
155260 59, /* IGNORE => ID */
155261 59, /* INITIALLY => ID */
155262 59, /* INSTEAD => ID */
155263 59, /* NO => ID */
155264 59, /* KEY => ID */
155265 59, /* OF => ID */
155266 59, /* OFFSET => ID */
155267 59, /* PRAGMA => ID */
155268 59, /* RAISE => ID */
155269 59, /* RECURSIVE => ID */
155270 59, /* REPLACE => ID */
155271 59, /* RESTRICT => ID */
155272 59, /* ROW => ID */
155273 59, /* ROWS => ID */
155274 59, /* TRIGGER => ID */
155275 59, /* VACUUM => ID */
155276 59, /* VIEW => ID */
155277 59, /* VIRTUAL => ID */
155278 59, /* WITH => ID */
155279 59, /* NULLS => ID */
155280 59, /* FIRST => ID */
155281 59, /* LAST => ID */
155282 59, /* CURRENT => ID */
155283 59, /* FOLLOWING => ID */
155284 59, /* PARTITION => ID */
155285 59, /* PRECEDING => ID */
155286 59, /* RANGE => ID */
155287 59, /* UNBOUNDED => ID */
155288 59, /* EXCLUDE => ID */
155289 59, /* GROUPS => ID */
155290 59, /* OTHERS => ID */
155291 59, /* TIES => ID */
155292 59, /* GENERATED => ID */
155293 59, /* ALWAYS => ID */
155294 59, /* REINDEX => ID */
155295 59, /* RENAME => ID */
155296 59, /* CTIME_KW => ID */
155297 0, /* ANY => nothing */
155298 0, /* BITAND => nothing */
155299 0, /* BITOR => nothing */
155300 0, /* LSHIFT => nothing */
155301 0, /* RSHIFT => nothing */
155302 0, /* PLUS => nothing */
155303 0, /* MINUS => nothing */
155304 0, /* STAR => nothing */
155305 0, /* SLASH => nothing */
155306 0, /* REM => nothing */
155307 0, /* CONCAT => nothing */
155308 0, /* COLLATE => nothing */
155309 0, /* BITNOT => nothing */
155310 0, /* ON => nothing */
155311 0, /* INDEXED => nothing */
155312 0, /* STRING => nothing */
155313 0, /* JOIN_KW => nothing */
155314 0, /* CONSTRAINT => nothing */
155315 0, /* DEFAULT => nothing */
155316 0, /* NULL => nothing */
155317 0, /* PRIMARY => nothing */
155318 0, /* UNIQUE => nothing */
155319 0, /* CHECK => nothing */
155320 0, /* REFERENCES => nothing */
155321 0, /* AUTOINCR => nothing */
155322 0, /* INSERT => nothing */
155323 0, /* DELETE => nothing */
155324 0, /* UPDATE => nothing */
155325 0, /* SET => nothing */
155326 0, /* DEFERRABLE => nothing */
155327 0, /* FOREIGN => nothing */
155328 0, /* DROP => nothing */
155329 0, /* UNION => nothing */
155330 0, /* ALL => nothing */
155331 0, /* EXCEPT => nothing */
155332 0, /* INTERSECT => nothing */
155333 0, /* SELECT => nothing */
155334 0, /* VALUES => nothing */
155335 0, /* DISTINCT => nothing */
155336 0, /* DOT => nothing */
155337 0, /* FROM => nothing */
155338 0, /* JOIN => nothing */
155339 0, /* USING => nothing */
155340 0, /* ORDER => nothing */
155341 0, /* GROUP => nothing */
155342 0, /* HAVING => nothing */
155343 0, /* LIMIT => nothing */
155344 0, /* WHERE => nothing */
155345 0, /* INTO => nothing */
155346 0, /* NOTHING => nothing */
155347 0, /* FLOAT => nothing */
155348 0, /* BLOB => nothing */
155349 0, /* INTEGER => nothing */
155350 0, /* VARIABLE => nothing */
155351 0, /* CASE => nothing */
155352 0, /* WHEN => nothing */
155353 0, /* THEN => nothing */
155354 0, /* ELSE => nothing */
155355 0, /* INDEX => nothing */
155356 0, /* ALTER => nothing */
155357 0, /* ADD => nothing */
155358 0, /* WINDOW => nothing */
155359 0, /* OVER => nothing */
155360 0, /* FILTER => nothing */
155361 0, /* COLUMN => nothing */
155362 0, /* AGG_FUNCTION => nothing */
155363 0, /* AGG_COLUMN => nothing */
155364 0, /* TRUEFALSE => nothing */
155365 0, /* ISNOT => nothing */
155366 0, /* FUNCTION => nothing */
155367 0, /* UMINUS => nothing */
155368 0, /* UPLUS => nothing */
155369 0, /* TRUTH => nothing */
155370 0, /* REGISTER => nothing */
155371 0, /* VECTOR => nothing */
155372 0, /* SELECT_COLUMN => nothing */
155373 0, /* IF_NULL_ROW => nothing */
155374 0, /* ASTERISK => nothing */
155375 0, /* SPAN => nothing */
155376 0, /* SPACE => nothing */
155377 0, /* ILLEGAL => nothing */
155378};
155379#endif /* YYFALLBACK */
155380
155381/* The following structure represents a single element of the
155382** parser's stack. Information stored includes:
155383**
155384** + The state number for the parser at this level of the stack.
155385**
155386** + The value of the token stored at this level of the stack.
155387** (In other words, the "major" token.)
155388**
155389** + The semantic value stored at this level of the stack. This is
155390** the information used by the action routines in the grammar.
155391** It is sometimes called the "minor" token.
155392**
155393** After the "shift" half of a SHIFTREDUCE action, the stateno field
155394** actually contains the reduce action for the second half of the
155395** SHIFTREDUCE.
155396*/
155397struct yyStackEntry {
155398 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
155399 YYCODETYPE major; /* The major token value. This is the code
155400 ** number for the token at this stack level */
155401 YYMINORTYPE minor; /* The user-supplied minor token value. This
155402 ** is the value of the token */
155403};
155404typedef struct yyStackEntry yyStackEntry;
155405
155406/* The state of the parser is completely contained in an instance of
155407** the following structure */
155408struct yyParser {
155409 yyStackEntry *yytos; /* Pointer to top element of the stack */
155410#ifdef YYTRACKMAXSTACKDEPTH
155411 int yyhwm; /* High-water mark of the stack */
155412#endif
155413#ifndef YYNOERRORRECOVERY
155414 int yyerrcnt; /* Shifts left before out of the error */
155415#endif
155416 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
155417 sqlite3ParserCTX_SDECL /* A place to hold %extra_context */
155418#if YYSTACKDEPTH<=0
155419 int yystksz; /* Current side of the stack */
155420 yyStackEntry *yystack; /* The parser's stack */
155421 yyStackEntry yystk0; /* First stack entry */
155422#else
155423 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
155424 yyStackEntry *yystackEnd; /* Last entry in the stack */
155425#endif
155426};
155427typedef struct yyParser yyParser;
155428
155429#ifndef NDEBUG
155430/* #include <stdio.h> */
155431static FILE *yyTraceFILE = 0;
155432static char *yyTracePrompt = 0;
155433#endif /* NDEBUG */
155434
155435#ifndef NDEBUG
155436/*
155437** Turn parser tracing on by giving a stream to which to write the trace
155438** and a prompt to preface each trace message. Tracing is turned off
155439** by making either argument NULL
155440**
155441** Inputs:
155442** <ul>
155443** <li> A FILE* to which trace output should be written.
155444** If NULL, then tracing is turned off.
155445** <li> A prefix string written at the beginning of every
155446** line of trace output. If NULL, then tracing is
155447** turned off.
155448** </ul>
155449**
155450** Outputs:
155451** None.
155452*/
155453SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
155454 yyTraceFILE = TraceFILE;
155455 yyTracePrompt = zTracePrompt;
155456 if( yyTraceFILE==0 ) yyTracePrompt = 0;
155457 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
155458}
155459#endif /* NDEBUG */
155460
155461#if defined(YYCOVERAGE) || !defined(NDEBUG)
155462/* For tracing shifts, the names of all terminals and nonterminals
155463** are required. The following table supplies these names */
155464static const char *const yyTokenName[] = {
155465 /* 0 */ "$",
155466 /* 1 */ "SEMI",
155467 /* 2 */ "EXPLAIN",
155468 /* 3 */ "QUERY",
155469 /* 4 */ "PLAN",
155470 /* 5 */ "BEGIN",
155471 /* 6 */ "TRANSACTION",
155472 /* 7 */ "DEFERRED",
155473 /* 8 */ "IMMEDIATE",
155474 /* 9 */ "EXCLUSIVE",
155475 /* 10 */ "COMMIT",
155476 /* 11 */ "END",
155477 /* 12 */ "ROLLBACK",
155478 /* 13 */ "SAVEPOINT",
155479 /* 14 */ "RELEASE",
155480 /* 15 */ "TO",
155481 /* 16 */ "TABLE",
155482 /* 17 */ "CREATE",
155483 /* 18 */ "IF",
155484 /* 19 */ "NOT",
155485 /* 20 */ "EXISTS",
155486 /* 21 */ "TEMP",
155487 /* 22 */ "LP",
155488 /* 23 */ "RP",
155489 /* 24 */ "AS",
155490 /* 25 */ "WITHOUT",
155491 /* 26 */ "COMMA",
155492 /* 27 */ "ABORT",
155493 /* 28 */ "ACTION",
155494 /* 29 */ "AFTER",
155495 /* 30 */ "ANALYZE",
155496 /* 31 */ "ASC",
155497 /* 32 */ "ATTACH",
155498 /* 33 */ "BEFORE",
155499 /* 34 */ "BY",
155500 /* 35 */ "CASCADE",
155501 /* 36 */ "CAST",
155502 /* 37 */ "CONFLICT",
155503 /* 38 */ "DATABASE",
155504 /* 39 */ "DESC",
155505 /* 40 */ "DETACH",
155506 /* 41 */ "EACH",
155507 /* 42 */ "FAIL",
155508 /* 43 */ "OR",
155509 /* 44 */ "AND",
155510 /* 45 */ "IS",
155511 /* 46 */ "MATCH",
155512 /* 47 */ "LIKE_KW",
155513 /* 48 */ "BETWEEN",
155514 /* 49 */ "IN",
155515 /* 50 */ "ISNULL",
155516 /* 51 */ "NOTNULL",
155517 /* 52 */ "NE",
155518 /* 53 */ "EQ",
155519 /* 54 */ "GT",
155520 /* 55 */ "LE",
155521 /* 56 */ "LT",
155522 /* 57 */ "GE",
155523 /* 58 */ "ESCAPE",
155524 /* 59 */ "ID",
155525 /* 60 */ "COLUMNKW",
155526 /* 61 */ "DO",
155527 /* 62 */ "FOR",
155528 /* 63 */ "IGNORE",
155529 /* 64 */ "INITIALLY",
155530 /* 65 */ "INSTEAD",
155531 /* 66 */ "NO",
155532 /* 67 */ "KEY",
155533 /* 68 */ "OF",
155534 /* 69 */ "OFFSET",
155535 /* 70 */ "PRAGMA",
155536 /* 71 */ "RAISE",
155537 /* 72 */ "RECURSIVE",
155538 /* 73 */ "REPLACE",
155539 /* 74 */ "RESTRICT",
155540 /* 75 */ "ROW",
155541 /* 76 */ "ROWS",
155542 /* 77 */ "TRIGGER",
155543 /* 78 */ "VACUUM",
155544 /* 79 */ "VIEW",
155545 /* 80 */ "VIRTUAL",
155546 /* 81 */ "WITH",
155547 /* 82 */ "NULLS",
155548 /* 83 */ "FIRST",
155549 /* 84 */ "LAST",
155550 /* 85 */ "CURRENT",
155551 /* 86 */ "FOLLOWING",
155552 /* 87 */ "PARTITION",
155553 /* 88 */ "PRECEDING",
155554 /* 89 */ "RANGE",
155555 /* 90 */ "UNBOUNDED",
155556 /* 91 */ "EXCLUDE",
155557 /* 92 */ "GROUPS",
155558 /* 93 */ "OTHERS",
155559 /* 94 */ "TIES",
155560 /* 95 */ "GENERATED",
155561 /* 96 */ "ALWAYS",
155562 /* 97 */ "REINDEX",
155563 /* 98 */ "RENAME",
155564 /* 99 */ "CTIME_KW",
155565 /* 100 */ "ANY",
155566 /* 101 */ "BITAND",
155567 /* 102 */ "BITOR",
155568 /* 103 */ "LSHIFT",
155569 /* 104 */ "RSHIFT",
155570 /* 105 */ "PLUS",
155571 /* 106 */ "MINUS",
155572 /* 107 */ "STAR",
155573 /* 108 */ "SLASH",
155574 /* 109 */ "REM",
155575 /* 110 */ "CONCAT",
155576 /* 111 */ "COLLATE",
155577 /* 112 */ "BITNOT",
155578 /* 113 */ "ON",
155579 /* 114 */ "INDEXED",
155580 /* 115 */ "STRING",
155581 /* 116 */ "JOIN_KW",
155582 /* 117 */ "CONSTRAINT",
155583 /* 118 */ "DEFAULT",
155584 /* 119 */ "NULL",
155585 /* 120 */ "PRIMARY",
155586 /* 121 */ "UNIQUE",
155587 /* 122 */ "CHECK",
155588 /* 123 */ "REFERENCES",
155589 /* 124 */ "AUTOINCR",
155590 /* 125 */ "INSERT",
155591 /* 126 */ "DELETE",
155592 /* 127 */ "UPDATE",
155593 /* 128 */ "SET",
155594 /* 129 */ "DEFERRABLE",
155595 /* 130 */ "FOREIGN",
155596 /* 131 */ "DROP",
155597 /* 132 */ "UNION",
155598 /* 133 */ "ALL",
155599 /* 134 */ "EXCEPT",
155600 /* 135 */ "INTERSECT",
155601 /* 136 */ "SELECT",
155602 /* 137 */ "VALUES",
155603 /* 138 */ "DISTINCT",
155604 /* 139 */ "DOT",
155605 /* 140 */ "FROM",
155606 /* 141 */ "JOIN",
155607 /* 142 */ "USING",
155608 /* 143 */ "ORDER",
155609 /* 144 */ "GROUP",
155610 /* 145 */ "HAVING",
155611 /* 146 */ "LIMIT",
155612 /* 147 */ "WHERE",
155613 /* 148 */ "INTO",
155614 /* 149 */ "NOTHING",
155615 /* 150 */ "FLOAT",
155616 /* 151 */ "BLOB",
155617 /* 152 */ "INTEGER",
155618 /* 153 */ "VARIABLE",
155619 /* 154 */ "CASE",
155620 /* 155 */ "WHEN",
155621 /* 156 */ "THEN",
155622 /* 157 */ "ELSE",
155623 /* 158 */ "INDEX",
155624 /* 159 */ "ALTER",
155625 /* 160 */ "ADD",
155626 /* 161 */ "WINDOW",
155627 /* 162 */ "OVER",
155628 /* 163 */ "FILTER",
155629 /* 164 */ "COLUMN",
155630 /* 165 */ "AGG_FUNCTION",
155631 /* 166 */ "AGG_COLUMN",
155632 /* 167 */ "TRUEFALSE",
155633 /* 168 */ "ISNOT",
155634 /* 169 */ "FUNCTION",
155635 /* 170 */ "UMINUS",
155636 /* 171 */ "UPLUS",
155637 /* 172 */ "TRUTH",
155638 /* 173 */ "REGISTER",
155639 /* 174 */ "VECTOR",
155640 /* 175 */ "SELECT_COLUMN",
155641 /* 176 */ "IF_NULL_ROW",
155642 /* 177 */ "ASTERISK",
155643 /* 178 */ "SPAN",
155644 /* 179 */ "SPACE",
155645 /* 180 */ "ILLEGAL",
155646 /* 181 */ "input",
155647 /* 182 */ "cmdlist",
155648 /* 183 */ "ecmd",
155649 /* 184 */ "cmdx",
155650 /* 185 */ "explain",
155651 /* 186 */ "cmd",
155652 /* 187 */ "transtype",
155653 /* 188 */ "trans_opt",
155654 /* 189 */ "nm",
155655 /* 190 */ "savepoint_opt",
155656 /* 191 */ "create_table",
155657 /* 192 */ "create_table_args",
155658 /* 193 */ "createkw",
155659 /* 194 */ "temp",
155660 /* 195 */ "ifnotexists",
155661 /* 196 */ "dbnm",
155662 /* 197 */ "columnlist",
155663 /* 198 */ "conslist_opt",
155664 /* 199 */ "table_options",
155665 /* 200 */ "select",
155666 /* 201 */ "columnname",
155667 /* 202 */ "carglist",
155668 /* 203 */ "typetoken",
155669 /* 204 */ "typename",
155670 /* 205 */ "signed",
155671 /* 206 */ "plus_num",
155672 /* 207 */ "minus_num",
155673 /* 208 */ "scanpt",
155674 /* 209 */ "scantok",
155675 /* 210 */ "ccons",
155676 /* 211 */ "term",
155677 /* 212 */ "expr",
155678 /* 213 */ "onconf",
155679 /* 214 */ "sortorder",
155680 /* 215 */ "autoinc",
155681 /* 216 */ "eidlist_opt",
155682 /* 217 */ "refargs",
155683 /* 218 */ "defer_subclause",
155684 /* 219 */ "generated",
155685 /* 220 */ "refarg",
155686 /* 221 */ "refact",
155687 /* 222 */ "init_deferred_pred_opt",
155688 /* 223 */ "conslist",
155689 /* 224 */ "tconscomma",
155690 /* 225 */ "tcons",
155691 /* 226 */ "sortlist",
155692 /* 227 */ "eidlist",
155693 /* 228 */ "defer_subclause_opt",
155694 /* 229 */ "orconf",
155695 /* 230 */ "resolvetype",
155696 /* 231 */ "raisetype",
155697 /* 232 */ "ifexists",
155698 /* 233 */ "fullname",
155699 /* 234 */ "selectnowith",
155700 /* 235 */ "oneselect",
155701 /* 236 */ "wqlist",
155702 /* 237 */ "multiselect_op",
155703 /* 238 */ "distinct",
155704 /* 239 */ "selcollist",
155705 /* 240 */ "from",
155706 /* 241 */ "where_opt",
155707 /* 242 */ "groupby_opt",
155708 /* 243 */ "having_opt",
155709 /* 244 */ "orderby_opt",
155710 /* 245 */ "limit_opt",
155711 /* 246 */ "window_clause",
155712 /* 247 */ "values",
155713 /* 248 */ "nexprlist",
155714 /* 249 */ "sclp",
155715 /* 250 */ "as",
155716 /* 251 */ "seltablist",
155717 /* 252 */ "stl_prefix",
155718 /* 253 */ "joinop",
155719 /* 254 */ "indexed_opt",
155720 /* 255 */ "on_opt",
155721 /* 256 */ "using_opt",
155722 /* 257 */ "exprlist",
155723 /* 258 */ "xfullname",
155724 /* 259 */ "idlist",
155725 /* 260 */ "nulls",
155726 /* 261 */ "with",
155727 /* 262 */ "setlist",
155728 /* 263 */ "insert_cmd",
155729 /* 264 */ "idlist_opt",
155730 /* 265 */ "upsert",
155731 /* 266 */ "filter_over",
155732 /* 267 */ "likeop",
155733 /* 268 */ "between_op",
155734 /* 269 */ "in_op",
155735 /* 270 */ "paren_exprlist",
155736 /* 271 */ "case_operand",
155737 /* 272 */ "case_exprlist",
155738 /* 273 */ "case_else",
155739 /* 274 */ "uniqueflag",
155740 /* 275 */ "collate",
155741 /* 276 */ "vinto",
155742 /* 277 */ "nmnum",
155743 /* 278 */ "trigger_decl",
155744 /* 279 */ "trigger_cmd_list",
155745 /* 280 */ "trigger_time",
155746 /* 281 */ "trigger_event",
155747 /* 282 */ "foreach_clause",
155748 /* 283 */ "when_clause",
155749 /* 284 */ "trigger_cmd",
155750 /* 285 */ "trnm",
155751 /* 286 */ "tridxby",
155752 /* 287 */ "database_kw_opt",
155753 /* 288 */ "key_opt",
155754 /* 289 */ "add_column_fullname",
155755 /* 290 */ "kwcolumn_opt",
155756 /* 291 */ "create_vtab",
155757 /* 292 */ "vtabarglist",
155758 /* 293 */ "vtabarg",
155759 /* 294 */ "vtabargtoken",
155760 /* 295 */ "lp",
155761 /* 296 */ "anylist",
155762 /* 297 */ "windowdefn_list",
155763 /* 298 */ "windowdefn",
155764 /* 299 */ "window",
155765 /* 300 */ "frame_opt",
155766 /* 301 */ "part_opt",
155767 /* 302 */ "filter_clause",
155768 /* 303 */ "over_clause",
155769 /* 304 */ "range_or_rows",
155770 /* 305 */ "frame_bound",
155771 /* 306 */ "frame_bound_s",
155772 /* 307 */ "frame_bound_e",
155773 /* 308 */ "frame_exclude_opt",
155774 /* 309 */ "frame_exclude",
155775};
155776#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
155777
155778#ifndef NDEBUG
155779/* For tracing reduce actions, the names of all rules are required.
155780*/
155781static const char *const yyRuleName[] = {
155782 /* 0 */ "explain ::= EXPLAIN",
155783 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
155784 /* 2 */ "cmdx ::= cmd",
155785 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
155786 /* 4 */ "transtype ::=",
155787 /* 5 */ "transtype ::= DEFERRED",
155788 /* 6 */ "transtype ::= IMMEDIATE",
155789 /* 7 */ "transtype ::= EXCLUSIVE",
155790 /* 8 */ "cmd ::= COMMIT|END trans_opt",
155791 /* 9 */ "cmd ::= ROLLBACK trans_opt",
155792 /* 10 */ "cmd ::= SAVEPOINT nm",
155793 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
155794 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
155795 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
155796 /* 14 */ "createkw ::= CREATE",
155797 /* 15 */ "ifnotexists ::=",
155798 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
155799 /* 17 */ "temp ::= TEMP",
155800 /* 18 */ "temp ::=",
155801 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
155802 /* 20 */ "create_table_args ::= AS select",
155803 /* 21 */ "table_options ::=",
155804 /* 22 */ "table_options ::= WITHOUT nm",
155805 /* 23 */ "columnname ::= nm typetoken",
155806 /* 24 */ "typetoken ::=",
155807 /* 25 */ "typetoken ::= typename LP signed RP",
155808 /* 26 */ "typetoken ::= typename LP signed COMMA signed RP",
155809 /* 27 */ "typename ::= typename ID|STRING",
155810 /* 28 */ "scanpt ::=",
155811 /* 29 */ "scantok ::=",
155812 /* 30 */ "ccons ::= CONSTRAINT nm",
155813 /* 31 */ "ccons ::= DEFAULT scantok term",
155814 /* 32 */ "ccons ::= DEFAULT LP expr RP",
155815 /* 33 */ "ccons ::= DEFAULT PLUS scantok term",
155816 /* 34 */ "ccons ::= DEFAULT MINUS scantok term",
155817 /* 35 */ "ccons ::= DEFAULT scantok ID|INDEXED",
155818 /* 36 */ "ccons ::= NOT NULL onconf",
155819 /* 37 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
155820 /* 38 */ "ccons ::= UNIQUE onconf",
155821 /* 39 */ "ccons ::= CHECK LP expr RP",
155822 /* 40 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
155823 /* 41 */ "ccons ::= defer_subclause",
155824 /* 42 */ "ccons ::= COLLATE ID|STRING",
155825 /* 43 */ "generated ::= LP expr RP",
155826 /* 44 */ "generated ::= LP expr RP ID",
155827 /* 45 */ "autoinc ::=",
155828 /* 46 */ "autoinc ::= AUTOINCR",
155829 /* 47 */ "refargs ::=",
155830 /* 48 */ "refargs ::= refargs refarg",
155831 /* 49 */ "refarg ::= MATCH nm",
155832 /* 50 */ "refarg ::= ON INSERT refact",
155833 /* 51 */ "refarg ::= ON DELETE refact",
155834 /* 52 */ "refarg ::= ON UPDATE refact",
155835 /* 53 */ "refact ::= SET NULL",
155836 /* 54 */ "refact ::= SET DEFAULT",
155837 /* 55 */ "refact ::= CASCADE",
155838 /* 56 */ "refact ::= RESTRICT",
155839 /* 57 */ "refact ::= NO ACTION",
155840 /* 58 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
155841 /* 59 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
155842 /* 60 */ "init_deferred_pred_opt ::=",
155843 /* 61 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
155844 /* 62 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
155845 /* 63 */ "conslist_opt ::=",
155846 /* 64 */ "tconscomma ::= COMMA",
155847 /* 65 */ "tcons ::= CONSTRAINT nm",
155848 /* 66 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
155849 /* 67 */ "tcons ::= UNIQUE LP sortlist RP onconf",
155850 /* 68 */ "tcons ::= CHECK LP expr RP onconf",
155851 /* 69 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
155852 /* 70 */ "defer_subclause_opt ::=",
155853 /* 71 */ "onconf ::=",
155854 /* 72 */ "onconf ::= ON CONFLICT resolvetype",
155855 /* 73 */ "orconf ::=",
155856 /* 74 */ "orconf ::= OR resolvetype",
155857 /* 75 */ "resolvetype ::= IGNORE",
155858 /* 76 */ "resolvetype ::= REPLACE",
155859 /* 77 */ "cmd ::= DROP TABLE ifexists fullname",
155860 /* 78 */ "ifexists ::= IF EXISTS",
155861 /* 79 */ "ifexists ::=",
155862 /* 80 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
155863 /* 81 */ "cmd ::= DROP VIEW ifexists fullname",
155864 /* 82 */ "cmd ::= select",
155865 /* 83 */ "select ::= WITH wqlist selectnowith",
155866 /* 84 */ "select ::= WITH RECURSIVE wqlist selectnowith",
155867 /* 85 */ "select ::= selectnowith",
155868 /* 86 */ "selectnowith ::= selectnowith multiselect_op oneselect",
155869 /* 87 */ "multiselect_op ::= UNION",
155870 /* 88 */ "multiselect_op ::= UNION ALL",
155871 /* 89 */ "multiselect_op ::= EXCEPT|INTERSECT",
155872 /* 90 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
155873 /* 91 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
155874 /* 92 */ "values ::= VALUES LP nexprlist RP",
155875 /* 93 */ "values ::= values COMMA LP nexprlist RP",
155876 /* 94 */ "distinct ::= DISTINCT",
155877 /* 95 */ "distinct ::= ALL",
155878 /* 96 */ "distinct ::=",
155879 /* 97 */ "sclp ::=",
155880 /* 98 */ "selcollist ::= sclp scanpt expr scanpt as",
155881 /* 99 */ "selcollist ::= sclp scanpt STAR",
155882 /* 100 */ "selcollist ::= sclp scanpt nm DOT STAR",
155883 /* 101 */ "as ::= AS nm",
155884 /* 102 */ "as ::=",
155885 /* 103 */ "from ::=",
155886 /* 104 */ "from ::= FROM seltablist",
155887 /* 105 */ "stl_prefix ::= seltablist joinop",
155888 /* 106 */ "stl_prefix ::=",
155889 /* 107 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
155890 /* 108 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
155891 /* 109 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
155892 /* 110 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
155893 /* 111 */ "dbnm ::=",
155894 /* 112 */ "dbnm ::= DOT nm",
155895 /* 113 */ "fullname ::= nm",
155896 /* 114 */ "fullname ::= nm DOT nm",
155897 /* 115 */ "xfullname ::= nm",
155898 /* 116 */ "xfullname ::= nm DOT nm",
155899 /* 117 */ "xfullname ::= nm DOT nm AS nm",
155900 /* 118 */ "xfullname ::= nm AS nm",
155901 /* 119 */ "joinop ::= COMMA|JOIN",
155902 /* 120 */ "joinop ::= JOIN_KW JOIN",
155903 /* 121 */ "joinop ::= JOIN_KW nm JOIN",
155904 /* 122 */ "joinop ::= JOIN_KW nm nm JOIN",
155905 /* 123 */ "on_opt ::= ON expr",
155906 /* 124 */ "on_opt ::=",
155907 /* 125 */ "indexed_opt ::=",
155908 /* 126 */ "indexed_opt ::= INDEXED BY nm",
155909 /* 127 */ "indexed_opt ::= NOT INDEXED",
155910 /* 128 */ "using_opt ::= USING LP idlist RP",
155911 /* 129 */ "using_opt ::=",
155912 /* 130 */ "orderby_opt ::=",
155913 /* 131 */ "orderby_opt ::= ORDER BY sortlist",
155914 /* 132 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
155915 /* 133 */ "sortlist ::= expr sortorder nulls",
155916 /* 134 */ "sortorder ::= ASC",
155917 /* 135 */ "sortorder ::= DESC",
155918 /* 136 */ "sortorder ::=",
155919 /* 137 */ "nulls ::= NULLS FIRST",
155920 /* 138 */ "nulls ::= NULLS LAST",
155921 /* 139 */ "nulls ::=",
155922 /* 140 */ "groupby_opt ::=",
155923 /* 141 */ "groupby_opt ::= GROUP BY nexprlist",
155924 /* 142 */ "having_opt ::=",
155925 /* 143 */ "having_opt ::= HAVING expr",
155926 /* 144 */ "limit_opt ::=",
155927 /* 145 */ "limit_opt ::= LIMIT expr",
155928 /* 146 */ "limit_opt ::= LIMIT expr OFFSET expr",
155929 /* 147 */ "limit_opt ::= LIMIT expr COMMA expr",
155930 /* 148 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
155931 /* 149 */ "where_opt ::=",
155932 /* 150 */ "where_opt ::= WHERE expr",
155933 /* 151 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt",
155934 /* 152 */ "setlist ::= setlist COMMA nm EQ expr",
155935 /* 153 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
155936 /* 154 */ "setlist ::= nm EQ expr",
155937 /* 155 */ "setlist ::= LP idlist RP EQ expr",
155938 /* 156 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
155939 /* 157 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
155940 /* 158 */ "upsert ::=",
155941 /* 159 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
155942 /* 160 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
155943 /* 161 */ "upsert ::= ON CONFLICT DO NOTHING",
155944 /* 162 */ "insert_cmd ::= INSERT orconf",
155945 /* 163 */ "insert_cmd ::= REPLACE",
155946 /* 164 */ "idlist_opt ::=",
155947 /* 165 */ "idlist_opt ::= LP idlist RP",
155948 /* 166 */ "idlist ::= idlist COMMA nm",
155949 /* 167 */ "idlist ::= nm",
155950 /* 168 */ "expr ::= LP expr RP",
155951 /* 169 */ "expr ::= ID|INDEXED",
155952 /* 170 */ "expr ::= JOIN_KW",
155953 /* 171 */ "expr ::= nm DOT nm",
155954 /* 172 */ "expr ::= nm DOT nm DOT nm",
155955 /* 173 */ "term ::= NULL|FLOAT|BLOB",
155956 /* 174 */ "term ::= STRING",
155957 /* 175 */ "term ::= INTEGER",
155958 /* 176 */ "expr ::= VARIABLE",
155959 /* 177 */ "expr ::= expr COLLATE ID|STRING",
155960 /* 178 */ "expr ::= CAST LP expr AS typetoken RP",
155961 /* 179 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
155962 /* 180 */ "expr ::= ID|INDEXED LP STAR RP",
155963 /* 181 */ "expr ::= ID|INDEXED LP distinct exprlist RP filter_over",
155964 /* 182 */ "expr ::= ID|INDEXED LP STAR RP filter_over",
155965 /* 183 */ "term ::= CTIME_KW",
155966 /* 184 */ "expr ::= LP nexprlist COMMA expr RP",
155967 /* 185 */ "expr ::= expr AND expr",
155968 /* 186 */ "expr ::= expr OR expr",
155969 /* 187 */ "expr ::= expr LT|GT|GE|LE expr",
155970 /* 188 */ "expr ::= expr EQ|NE expr",
155971 /* 189 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
155972 /* 190 */ "expr ::= expr PLUS|MINUS expr",
155973 /* 191 */ "expr ::= expr STAR|SLASH|REM expr",
155974 /* 192 */ "expr ::= expr CONCAT expr",
155975 /* 193 */ "likeop ::= NOT LIKE_KW|MATCH",
155976 /* 194 */ "expr ::= expr likeop expr",
155977 /* 195 */ "expr ::= expr likeop expr ESCAPE expr",
155978 /* 196 */ "expr ::= expr ISNULL|NOTNULL",
155979 /* 197 */ "expr ::= expr NOT NULL",
155980 /* 198 */ "expr ::= expr IS expr",
155981 /* 199 */ "expr ::= expr IS NOT expr",
155982 /* 200 */ "expr ::= NOT expr",
155983 /* 201 */ "expr ::= BITNOT expr",
155984 /* 202 */ "expr ::= PLUS|MINUS expr",
155985 /* 203 */ "between_op ::= BETWEEN",
155986 /* 204 */ "between_op ::= NOT BETWEEN",
155987 /* 205 */ "expr ::= expr between_op expr AND expr",
155988 /* 206 */ "in_op ::= IN",
155989 /* 207 */ "in_op ::= NOT IN",
155990 /* 208 */ "expr ::= expr in_op LP exprlist RP",
155991 /* 209 */ "expr ::= LP select RP",
155992 /* 210 */ "expr ::= expr in_op LP select RP",
155993 /* 211 */ "expr ::= expr in_op nm dbnm paren_exprlist",
155994 /* 212 */ "expr ::= EXISTS LP select RP",
155995 /* 213 */ "expr ::= CASE case_operand case_exprlist case_else END",
155996 /* 214 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
155997 /* 215 */ "case_exprlist ::= WHEN expr THEN expr",
155998 /* 216 */ "case_else ::= ELSE expr",
155999 /* 217 */ "case_else ::=",
156000 /* 218 */ "case_operand ::= expr",
156001 /* 219 */ "case_operand ::=",
156002 /* 220 */ "exprlist ::=",
156003 /* 221 */ "nexprlist ::= nexprlist COMMA expr",
156004 /* 222 */ "nexprlist ::= expr",
156005 /* 223 */ "paren_exprlist ::=",
156006 /* 224 */ "paren_exprlist ::= LP exprlist RP",
156007 /* 225 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
156008 /* 226 */ "uniqueflag ::= UNIQUE",
156009 /* 227 */ "uniqueflag ::=",
156010 /* 228 */ "eidlist_opt ::=",
156011 /* 229 */ "eidlist_opt ::= LP eidlist RP",
156012 /* 230 */ "eidlist ::= eidlist COMMA nm collate sortorder",
156013 /* 231 */ "eidlist ::= nm collate sortorder",
156014 /* 232 */ "collate ::=",
156015 /* 233 */ "collate ::= COLLATE ID|STRING",
156016 /* 234 */ "cmd ::= DROP INDEX ifexists fullname",
156017 /* 235 */ "cmd ::= VACUUM vinto",
156018 /* 236 */ "cmd ::= VACUUM nm vinto",
156019 /* 237 */ "vinto ::= INTO expr",
156020 /* 238 */ "vinto ::=",
156021 /* 239 */ "cmd ::= PRAGMA nm dbnm",
156022 /* 240 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
156023 /* 241 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
156024 /* 242 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
156025 /* 243 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
156026 /* 244 */ "plus_num ::= PLUS INTEGER|FLOAT",
156027 /* 245 */ "minus_num ::= MINUS INTEGER|FLOAT",
156028 /* 246 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
156029 /* 247 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
156030 /* 248 */ "trigger_time ::= BEFORE|AFTER",
156031 /* 249 */ "trigger_time ::= INSTEAD OF",
156032 /* 250 */ "trigger_time ::=",
156033 /* 251 */ "trigger_event ::= DELETE|INSERT",
156034 /* 252 */ "trigger_event ::= UPDATE",
156035 /* 253 */ "trigger_event ::= UPDATE OF idlist",
156036 /* 254 */ "when_clause ::=",
156037 /* 255 */ "when_clause ::= WHEN expr",
156038 /* 256 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
156039 /* 257 */ "trigger_cmd_list ::= trigger_cmd SEMI",
156040 /* 258 */ "trnm ::= nm DOT nm",
156041 /* 259 */ "tridxby ::= INDEXED BY nm",
156042 /* 260 */ "tridxby ::= NOT INDEXED",
156043 /* 261 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
156044 /* 262 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
156045 /* 263 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
156046 /* 264 */ "trigger_cmd ::= scanpt select scanpt",
156047 /* 265 */ "expr ::= RAISE LP IGNORE RP",
156048 /* 266 */ "expr ::= RAISE LP raisetype COMMA nm RP",
156049 /* 267 */ "raisetype ::= ROLLBACK",
156050 /* 268 */ "raisetype ::= ABORT",
156051 /* 269 */ "raisetype ::= FAIL",
156052 /* 270 */ "cmd ::= DROP TRIGGER ifexists fullname",
156053 /* 271 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
156054 /* 272 */ "cmd ::= DETACH database_kw_opt expr",
156055 /* 273 */ "key_opt ::=",
156056 /* 274 */ "key_opt ::= KEY expr",
156057 /* 275 */ "cmd ::= REINDEX",
156058 /* 276 */ "cmd ::= REINDEX nm dbnm",
156059 /* 277 */ "cmd ::= ANALYZE",
156060 /* 278 */ "cmd ::= ANALYZE nm dbnm",
156061 /* 279 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
156062 /* 280 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
156063 /* 281 */ "add_column_fullname ::= fullname",
156064 /* 282 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
156065 /* 283 */ "cmd ::= create_vtab",
156066 /* 284 */ "cmd ::= create_vtab LP vtabarglist RP",
156067 /* 285 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
156068 /* 286 */ "vtabarg ::=",
156069 /* 287 */ "vtabargtoken ::= ANY",
156070 /* 288 */ "vtabargtoken ::= lp anylist RP",
156071 /* 289 */ "lp ::= LP",
156072 /* 290 */ "with ::= WITH wqlist",
156073 /* 291 */ "with ::= WITH RECURSIVE wqlist",
156074 /* 292 */ "wqlist ::= nm eidlist_opt AS LP select RP",
156075 /* 293 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
156076 /* 294 */ "windowdefn_list ::= windowdefn",
156077 /* 295 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
156078 /* 296 */ "windowdefn ::= nm AS LP window RP",
156079 /* 297 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
156080 /* 298 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
156081 /* 299 */ "window ::= ORDER BY sortlist frame_opt",
156082 /* 300 */ "window ::= nm ORDER BY sortlist frame_opt",
156083 /* 301 */ "window ::= frame_opt",
156084 /* 302 */ "window ::= nm frame_opt",
156085 /* 303 */ "frame_opt ::=",
156086 /* 304 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
156087 /* 305 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
156088 /* 306 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
156089 /* 307 */ "frame_bound_s ::= frame_bound",
156090 /* 308 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
156091 /* 309 */ "frame_bound_e ::= frame_bound",
156092 /* 310 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
156093 /* 311 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
156094 /* 312 */ "frame_bound ::= CURRENT ROW",
156095 /* 313 */ "frame_exclude_opt ::=",
156096 /* 314 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
156097 /* 315 */ "frame_exclude ::= NO OTHERS",
156098 /* 316 */ "frame_exclude ::= CURRENT ROW",
156099 /* 317 */ "frame_exclude ::= GROUP|TIES",
156100 /* 318 */ "window_clause ::= WINDOW windowdefn_list",
156101 /* 319 */ "filter_over ::= filter_clause over_clause",
156102 /* 320 */ "filter_over ::= over_clause",
156103 /* 321 */ "filter_over ::= filter_clause",
156104 /* 322 */ "over_clause ::= OVER LP window RP",
156105 /* 323 */ "over_clause ::= OVER nm",
156106 /* 324 */ "filter_clause ::= FILTER LP WHERE expr RP",
156107 /* 325 */ "input ::= cmdlist",
156108 /* 326 */ "cmdlist ::= cmdlist ecmd",
156109 /* 327 */ "cmdlist ::= ecmd",
156110 /* 328 */ "ecmd ::= SEMI",
156111 /* 329 */ "ecmd ::= cmdx SEMI",
156112 /* 330 */ "ecmd ::= explain cmdx SEMI",
156113 /* 331 */ "trans_opt ::=",
156114 /* 332 */ "trans_opt ::= TRANSACTION",
156115 /* 333 */ "trans_opt ::= TRANSACTION nm",
156116 /* 334 */ "savepoint_opt ::= SAVEPOINT",
156117 /* 335 */ "savepoint_opt ::=",
156118 /* 336 */ "cmd ::= create_table create_table_args",
156119 /* 337 */ "columnlist ::= columnlist COMMA columnname carglist",
156120 /* 338 */ "columnlist ::= columnname carglist",
156121 /* 339 */ "nm ::= ID|INDEXED",
156122 /* 340 */ "nm ::= STRING",
156123 /* 341 */ "nm ::= JOIN_KW",
156124 /* 342 */ "typetoken ::= typename",
156125 /* 343 */ "typename ::= ID|STRING",
156126 /* 344 */ "signed ::= plus_num",
156127 /* 345 */ "signed ::= minus_num",
156128 /* 346 */ "carglist ::= carglist ccons",
156129 /* 347 */ "carglist ::=",
156130 /* 348 */ "ccons ::= NULL onconf",
156131 /* 349 */ "ccons ::= GENERATED ALWAYS AS generated",
156132 /* 350 */ "ccons ::= AS generated",
156133 /* 351 */ "conslist_opt ::= COMMA conslist",
156134 /* 352 */ "conslist ::= conslist tconscomma tcons",
156135 /* 353 */ "conslist ::= tcons",
156136 /* 354 */ "tconscomma ::=",
156137 /* 355 */ "defer_subclause_opt ::= defer_subclause",
156138 /* 356 */ "resolvetype ::= raisetype",
156139 /* 357 */ "selectnowith ::= oneselect",
156140 /* 358 */ "oneselect ::= values",
156141 /* 359 */ "sclp ::= selcollist COMMA",
156142 /* 360 */ "as ::= ID|STRING",
156143 /* 361 */ "expr ::= term",
156144 /* 362 */ "likeop ::= LIKE_KW|MATCH",
156145 /* 363 */ "exprlist ::= nexprlist",
156146 /* 364 */ "nmnum ::= plus_num",
156147 /* 365 */ "nmnum ::= nm",
156148 /* 366 */ "nmnum ::= ON",
156149 /* 367 */ "nmnum ::= DELETE",
156150 /* 368 */ "nmnum ::= DEFAULT",
156151 /* 369 */ "plus_num ::= INTEGER|FLOAT",
156152 /* 370 */ "foreach_clause ::=",
156153 /* 371 */ "foreach_clause ::= FOR EACH ROW",
156154 /* 372 */ "trnm ::= nm",
156155 /* 373 */ "tridxby ::=",
156156 /* 374 */ "database_kw_opt ::= DATABASE",
156157 /* 375 */ "database_kw_opt ::=",
156158 /* 376 */ "kwcolumn_opt ::=",
156159 /* 377 */ "kwcolumn_opt ::= COLUMNKW",
156160 /* 378 */ "vtabarglist ::= vtabarg",
156161 /* 379 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
156162 /* 380 */ "vtabarg ::= vtabarg vtabargtoken",
156163 /* 381 */ "anylist ::=",
156164 /* 382 */ "anylist ::= anylist LP anylist RP",
156165 /* 383 */ "anylist ::= anylist ANY",
156166 /* 384 */ "with ::=",
156167};
156168#endif /* NDEBUG */
156169
156170
156171#if YYSTACKDEPTH<=0
156172/*
156173** Try to increase the size of the parser stack. Return the number
156174** of errors. Return 0 on success.
156175*/
156176static int yyGrowStack(yyParser *p){
156177 int newSize;
156178 int idx;
156179 yyStackEntry *pNew;
156180
156181 newSize = p->yystksz*2 + 100;
156182 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
156183 if( p->yystack==&p->yystk0 ){
156184 pNew = malloc(newSize*sizeof(pNew[0]));
156185 if( pNew ) pNew[0] = p->yystk0;
156186 }else{
156187 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
156188 }
156189 if( pNew ){
156190 p->yystack = pNew;
156191 p->yytos = &p->yystack[idx];
156192#ifndef NDEBUG
156193 if( yyTraceFILE ){
156194 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
156195 yyTracePrompt, p->yystksz, newSize);
156196 }
156197#endif
156198 p->yystksz = newSize;
156199 }
156200 return pNew==0;
156201}
156202#endif
156203
156204/* Datatype of the argument to the memory allocated passed as the
156205** second argument to sqlite3ParserAlloc() below. This can be changed by
156206** putting an appropriate #define in the %include section of the input
156207** grammar.
156208*/
156209#ifndef YYMALLOCARGTYPE
156210# define YYMALLOCARGTYPE size_t
156211#endif
156212
156213/* Initialize a new parser that has already been allocated.
156214*/
156215SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
156216 yyParser *yypParser = (yyParser*)yypRawParser;
156217 sqlite3ParserCTX_STORE
156218#ifdef YYTRACKMAXSTACKDEPTH
156219 yypParser->yyhwm = 0;
156220#endif
156221#if YYSTACKDEPTH<=0
156222 yypParser->yytos = NULL;
156223 yypParser->yystack = NULL;
156224 yypParser->yystksz = 0;
156225 if( yyGrowStack(yypParser) ){
156226 yypParser->yystack = &yypParser->yystk0;
156227 yypParser->yystksz = 1;
156228 }
156229#endif
156230#ifndef YYNOERRORRECOVERY
156231 yypParser->yyerrcnt = -1;
156232#endif
156233 yypParser->yytos = yypParser->yystack;
156234 yypParser->yystack[0].stateno = 0;
156235 yypParser->yystack[0].major = 0;
156236#if YYSTACKDEPTH>0
156237 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
156238#endif
156239}
156240
156241#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
156242/*
156243** This function allocates a new parser.
156244** The only argument is a pointer to a function which works like
156245** malloc.
156246**
156247** Inputs:
156248** A pointer to the function used to allocate memory.
156249**
156250** Outputs:
156251** A pointer to a parser. This pointer is used in subsequent calls
156252** to sqlite3Parser and sqlite3ParserFree.
156253*/
156254SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
156255 yyParser *yypParser;
156256 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
156257 if( yypParser ){
156258 sqlite3ParserCTX_STORE
156259 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
156260 }
156261 return (void*)yypParser;
156262}
156263#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
156264
156265
156266/* The following function deletes the "minor type" or semantic value
156267** associated with a symbol. The symbol can be either a terminal
156268** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
156269** a pointer to the value to be deleted. The code used to do the
156270** deletions is derived from the %destructor and/or %token_destructor
156271** directives of the input grammar.
156272*/
156273static void yy_destructor(
156274 yyParser *yypParser, /* The parser */
156275 YYCODETYPE yymajor, /* Type code for object to destroy */
156276 YYMINORTYPE *yypminor /* The object to be destroyed */
156277){
156278 sqlite3ParserARG_FETCH
156279 sqlite3ParserCTX_FETCH
156280 switch( yymajor ){
156281 /* Here is inserted the actions which take place when a
156282 ** terminal or non-terminal is destroyed. This can happen
156283 ** when the symbol is popped from the stack during a
156284 ** reduce or during error processing or when a parser is
156285 ** being destroyed before it is finished parsing.
156286 **
156287 ** Note: during a reduce, the only symbols destroyed are those
156288 ** which appear on the RHS of the rule, but which are *not* used
156289 ** inside the C code.
156290 */
156291/********* Begin destructor definitions ***************************************/
156292 case 200: /* select */
156293 case 234: /* selectnowith */
156294 case 235: /* oneselect */
156295 case 247: /* values */
156296{
156297sqlite3SelectDelete(pParse->db, (yypminor->yy539));
156298}
156299 break;
156300 case 211: /* term */
156301 case 212: /* expr */
156302 case 241: /* where_opt */
156303 case 243: /* having_opt */
156304 case 255: /* on_opt */
156305 case 271: /* case_operand */
156306 case 273: /* case_else */
156307 case 276: /* vinto */
156308 case 283: /* when_clause */
156309 case 288: /* key_opt */
156310 case 302: /* filter_clause */
156311{
156312sqlite3ExprDelete(pParse->db, (yypminor->yy202));
156313}
156314 break;
156315 case 216: /* eidlist_opt */
156316 case 226: /* sortlist */
156317 case 227: /* eidlist */
156318 case 239: /* selcollist */
156319 case 242: /* groupby_opt */
156320 case 244: /* orderby_opt */
156321 case 248: /* nexprlist */
156322 case 249: /* sclp */
156323 case 257: /* exprlist */
156324 case 262: /* setlist */
156325 case 270: /* paren_exprlist */
156326 case 272: /* case_exprlist */
156327 case 301: /* part_opt */
156328{
156329sqlite3ExprListDelete(pParse->db, (yypminor->yy242));
156330}
156331 break;
156332 case 233: /* fullname */
156333 case 240: /* from */
156334 case 251: /* seltablist */
156335 case 252: /* stl_prefix */
156336 case 258: /* xfullname */
156337{
156338sqlite3SrcListDelete(pParse->db, (yypminor->yy47));
156339}
156340 break;
156341 case 236: /* wqlist */
156342{
156343sqlite3WithDelete(pParse->db, (yypminor->yy131));
156344}
156345 break;
156346 case 246: /* window_clause */
156347 case 297: /* windowdefn_list */
156348{
156349sqlite3WindowListDelete(pParse->db, (yypminor->yy303));
156350}
156351 break;
156352 case 256: /* using_opt */
156353 case 259: /* idlist */
156354 case 264: /* idlist_opt */
156355{
156356sqlite3IdListDelete(pParse->db, (yypminor->yy600));
156357}
156358 break;
156359 case 266: /* filter_over */
156360 case 298: /* windowdefn */
156361 case 299: /* window */
156362 case 300: /* frame_opt */
156363 case 303: /* over_clause */
156364{
156365sqlite3WindowDelete(pParse->db, (yypminor->yy303));
156366}
156367 break;
156368 case 279: /* trigger_cmd_list */
156369 case 284: /* trigger_cmd */
156370{
156371sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy447));
156372}
156373 break;
156374 case 281: /* trigger_event */
156375{
156376sqlite3IdListDelete(pParse->db, (yypminor->yy230).b);
156377}
156378 break;
156379 case 305: /* frame_bound */
156380 case 306: /* frame_bound_s */
156381 case 307: /* frame_bound_e */
156382{
156383sqlite3ExprDelete(pParse->db, (yypminor->yy77).pExpr);
156384}
156385 break;
156386/********* End destructor definitions *****************************************/
156387 default: break; /* If no destructor action specified: do nothing */
156388 }
156389}
156390
156391/*
156392** Pop the parser's stack once.
156393**
156394** If there is a destructor routine associated with the token which
156395** is popped from the stack, then call it.
156396*/
156397static void yy_pop_parser_stack(yyParser *pParser){
156398 yyStackEntry *yytos;
156399 assert( pParser->yytos!=0 );
156400 assert( pParser->yytos > pParser->yystack );
156401 yytos = pParser->yytos--;
156402#ifndef NDEBUG
156403 if( yyTraceFILE ){
156404 fprintf(yyTraceFILE,"%sPopping %s\n",
156405 yyTracePrompt,
156406 yyTokenName[yytos->major]);
156407 }
156408#endif
156409 yy_destructor(pParser, yytos->major, &yytos->minor);
156410}
156411
156412/*
156413** Clear all secondary memory allocations from the parser
156414*/
156415SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
156416 yyParser *pParser = (yyParser*)p;
156417 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
156418#if YYSTACKDEPTH<=0
156419 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
156420#endif
156421}
156422
156423#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
156424/*
156425** Deallocate and destroy a parser. Destructors are called for
156426** all stack elements before shutting the parser down.
156427**
156428** If the YYPARSEFREENEVERNULL macro exists (for example because it
156429** is defined in a %include section of the input grammar) then it is
156430** assumed that the input pointer is never NULL.
156431*/
156432SQLITE_PRIVATE void sqlite3ParserFree(
156433 void *p, /* The parser to be deleted */
156434 void (*freeProc)(void*) /* Function used to reclaim memory */
156435){
156436#ifndef YYPARSEFREENEVERNULL
156437 if( p==0 ) return;
156438#endif
156439 sqlite3ParserFinalize(p);
156440 (*freeProc)(p);
156441}
156442#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
156443
156444/*
156445** Return the peak depth of the stack for a parser.
156446*/
156447#ifdef YYTRACKMAXSTACKDEPTH
156448SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
156449 yyParser *pParser = (yyParser*)p;
156450 return pParser->yyhwm;
156451}
156452#endif
156453
156454/* This array of booleans keeps track of the parser statement
156455** coverage. The element yycoverage[X][Y] is set when the parser
156456** is in state X and has a lookahead token Y. In a well-tested
156457** systems, every element of this matrix should end up being set.
156458*/
156459#if defined(YYCOVERAGE)
156460static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
156461#endif
156462
156463/*
156464** Write into out a description of every state/lookahead combination that
156465**
156466** (1) has not been used by the parser, and
156467** (2) is not a syntax error.
156468**
156469** Return the number of missed state/lookahead combinations.
156470*/
156471#if defined(YYCOVERAGE)
156472SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
156473 int stateno, iLookAhead, i;
156474 int nMissed = 0;
156475 for(stateno=0; stateno<YYNSTATE; stateno++){
156476 i = yy_shift_ofst[stateno];
156477 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
156478 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
156479 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
156480 if( out ){
156481 fprintf(out,"State %d lookahead %s %s\n", stateno,
156482 yyTokenName[iLookAhead],
156483 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
156484 }
156485 }
156486 }
156487 return nMissed;
156488}
156489#endif
156490
156491/*
156492** Find the appropriate action for a parser given the terminal
156493** look-ahead token iLookAhead.
156494*/
156495static YYACTIONTYPE yy_find_shift_action(
156496 YYCODETYPE iLookAhead, /* The look-ahead token */
156497 YYACTIONTYPE stateno /* Current state number */
156498){
156499 int i;
156500
156501 if( stateno>YY_MAX_SHIFT ) return stateno;
156502 assert( stateno <= YY_SHIFT_COUNT );
156503#if defined(YYCOVERAGE)
156504 yycoverage[stateno][iLookAhead] = 1;
156505#endif
156506 do{
156507 i = yy_shift_ofst[stateno];
156508 assert( i>=0 );
156509 assert( i<=YY_ACTTAB_COUNT );
156510 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
156511 assert( iLookAhead!=YYNOCODE );
156512 assert( iLookAhead < YYNTOKEN );
156513 i += iLookAhead;
156514 assert( i<(int)YY_NLOOKAHEAD );
156515 if( yy_lookahead[i]!=iLookAhead ){
156516#ifdef YYFALLBACK
156517 YYCODETYPE iFallback; /* Fallback token */
156518 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
156519 iFallback = yyFallback[iLookAhead];
156520 if( iFallback!=0 ){
156521#ifndef NDEBUG
156522 if( yyTraceFILE ){
156523 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
156524 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
156525 }
156526#endif
156527 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
156528 iLookAhead = iFallback;
156529 continue;
156530 }
156531#endif
156532#ifdef YYWILDCARD
156533 {
156534 int j = i - iLookAhead + YYWILDCARD;
156535 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
156536 if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
156537#ifndef NDEBUG
156538 if( yyTraceFILE ){
156539 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
156540 yyTracePrompt, yyTokenName[iLookAhead],
156541 yyTokenName[YYWILDCARD]);
156542 }
156543#endif /* NDEBUG */
156544 return yy_action[j];
156545 }
156546 }
156547#endif /* YYWILDCARD */
156548 return yy_default[stateno];
156549 }else{
156550 assert( i>=0 && i<sizeof(yy_action)/sizeof(yy_action[0]) );
156551 return yy_action[i];
156552 }
156553 }while(1);
156554}
156555
156556/*
156557** Find the appropriate action for a parser given the non-terminal
156558** look-ahead token iLookAhead.
156559*/
156560static YYACTIONTYPE yy_find_reduce_action(
156561 YYACTIONTYPE stateno, /* Current state number */
156562 YYCODETYPE iLookAhead /* The look-ahead token */
156563){
156564 int i;
156565#ifdef YYERRORSYMBOL
156566 if( stateno>YY_REDUCE_COUNT ){
156567 return yy_default[stateno];
156568 }
156569#else
156570 assert( stateno<=YY_REDUCE_COUNT );
156571#endif
156572 i = yy_reduce_ofst[stateno];
156573 assert( iLookAhead!=YYNOCODE );
156574 i += iLookAhead;
156575#ifdef YYERRORSYMBOL
156576 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
156577 return yy_default[stateno];
156578 }
156579#else
156580 assert( i>=0 && i<YY_ACTTAB_COUNT );
156581 assert( yy_lookahead[i]==iLookAhead );
156582#endif
156583 return yy_action[i];
156584}
156585
156586/*
156587** The following routine is called if the stack overflows.
156588*/
156589static void yyStackOverflow(yyParser *yypParser){
156590 sqlite3ParserARG_FETCH
156591 sqlite3ParserCTX_FETCH
156592#ifndef NDEBUG
156593 if( yyTraceFILE ){
156594 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
156595 }
156596#endif
156597 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
156598 /* Here code is inserted which will execute if the parser
156599 ** stack every overflows */
156600/******** Begin %stack_overflow code ******************************************/
156601
156602 sqlite3ErrorMsg(pParse, "parser stack overflow");
156603/******** End %stack_overflow code ********************************************/
156604 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
156605 sqlite3ParserCTX_STORE
156606}
156607
156608/*
156609** Print tracing information for a SHIFT action
156610*/
156611#ifndef NDEBUG
156612static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
156613 if( yyTraceFILE ){
156614 if( yyNewState<YYNSTATE ){
156615 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
156616 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
156617 yyNewState);
156618 }else{
156619 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
156620 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
156621 yyNewState - YY_MIN_REDUCE);
156622 }
156623 }
156624}
156625#else
156626# define yyTraceShift(X,Y,Z)
156627#endif
156628
156629/*
156630** Perform a shift action.
156631*/
156632static void yy_shift(
156633 yyParser *yypParser, /* The parser to be shifted */
156634 YYACTIONTYPE yyNewState, /* The new state to shift in */
156635 YYCODETYPE yyMajor, /* The major token to shift in */
156636 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */
156637){
156638 yyStackEntry *yytos;
156639 yypParser->yytos++;
156640#ifdef YYTRACKMAXSTACKDEPTH
156641 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
156642 yypParser->yyhwm++;
156643 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
156644 }
156645#endif
156646#if YYSTACKDEPTH>0
156647 if( yypParser->yytos>yypParser->yystackEnd ){
156648 yypParser->yytos--;
156649 yyStackOverflow(yypParser);
156650 return;
156651 }
156652#else
156653 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
156654 if( yyGrowStack(yypParser) ){
156655 yypParser->yytos--;
156656 yyStackOverflow(yypParser);
156657 return;
156658 }
156659 }
156660#endif
156661 if( yyNewState > YY_MAX_SHIFT ){
156662 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
156663 }
156664 yytos = yypParser->yytos;
156665 yytos->stateno = yyNewState;
156666 yytos->major = yyMajor;
156667 yytos->minor.yy0 = yyMinor;
156668 yyTraceShift(yypParser, yyNewState, "Shift");
156669}
156670
156671/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
156672** of that rule */
156673static const YYCODETYPE yyRuleInfoLhs[] = {
156674 185, /* (0) explain ::= EXPLAIN */
156675 185, /* (1) explain ::= EXPLAIN QUERY PLAN */
156676 184, /* (2) cmdx ::= cmd */
156677 186, /* (3) cmd ::= BEGIN transtype trans_opt */
156678 187, /* (4) transtype ::= */
156679 187, /* (5) transtype ::= DEFERRED */
156680 187, /* (6) transtype ::= IMMEDIATE */
156681 187, /* (7) transtype ::= EXCLUSIVE */
156682 186, /* (8) cmd ::= COMMIT|END trans_opt */
156683 186, /* (9) cmd ::= ROLLBACK trans_opt */
156684 186, /* (10) cmd ::= SAVEPOINT nm */
156685 186, /* (11) cmd ::= RELEASE savepoint_opt nm */
156686 186, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
156687 191, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
156688 193, /* (14) createkw ::= CREATE */
156689 195, /* (15) ifnotexists ::= */
156690 195, /* (16) ifnotexists ::= IF NOT EXISTS */
156691 194, /* (17) temp ::= TEMP */
156692 194, /* (18) temp ::= */
156693 192, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
156694 192, /* (20) create_table_args ::= AS select */
156695 199, /* (21) table_options ::= */
156696 199, /* (22) table_options ::= WITHOUT nm */
156697 201, /* (23) columnname ::= nm typetoken */
156698 203, /* (24) typetoken ::= */
156699 203, /* (25) typetoken ::= typename LP signed RP */
156700 203, /* (26) typetoken ::= typename LP signed COMMA signed RP */
156701 204, /* (27) typename ::= typename ID|STRING */
156702 208, /* (28) scanpt ::= */
156703 209, /* (29) scantok ::= */
156704 210, /* (30) ccons ::= CONSTRAINT nm */
156705 210, /* (31) ccons ::= DEFAULT scantok term */
156706 210, /* (32) ccons ::= DEFAULT LP expr RP */
156707 210, /* (33) ccons ::= DEFAULT PLUS scantok term */
156708 210, /* (34) ccons ::= DEFAULT MINUS scantok term */
156709 210, /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
156710 210, /* (36) ccons ::= NOT NULL onconf */
156711 210, /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
156712 210, /* (38) ccons ::= UNIQUE onconf */
156713 210, /* (39) ccons ::= CHECK LP expr RP */
156714 210, /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
156715 210, /* (41) ccons ::= defer_subclause */
156716 210, /* (42) ccons ::= COLLATE ID|STRING */
156717 219, /* (43) generated ::= LP expr RP */
156718 219, /* (44) generated ::= LP expr RP ID */
156719 215, /* (45) autoinc ::= */
156720 215, /* (46) autoinc ::= AUTOINCR */
156721 217, /* (47) refargs ::= */
156722 217, /* (48) refargs ::= refargs refarg */
156723 220, /* (49) refarg ::= MATCH nm */
156724 220, /* (50) refarg ::= ON INSERT refact */
156725 220, /* (51) refarg ::= ON DELETE refact */
156726 220, /* (52) refarg ::= ON UPDATE refact */
156727 221, /* (53) refact ::= SET NULL */
156728 221, /* (54) refact ::= SET DEFAULT */
156729 221, /* (55) refact ::= CASCADE */
156730 221, /* (56) refact ::= RESTRICT */
156731 221, /* (57) refact ::= NO ACTION */
156732 218, /* (58) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
156733 218, /* (59) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
156734 222, /* (60) init_deferred_pred_opt ::= */
156735 222, /* (61) init_deferred_pred_opt ::= INITIALLY DEFERRED */
156736 222, /* (62) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
156737 198, /* (63) conslist_opt ::= */
156738 224, /* (64) tconscomma ::= COMMA */
156739 225, /* (65) tcons ::= CONSTRAINT nm */
156740 225, /* (66) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
156741 225, /* (67) tcons ::= UNIQUE LP sortlist RP onconf */
156742 225, /* (68) tcons ::= CHECK LP expr RP onconf */
156743 225, /* (69) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
156744 228, /* (70) defer_subclause_opt ::= */
156745 213, /* (71) onconf ::= */
156746 213, /* (72) onconf ::= ON CONFLICT resolvetype */
156747 229, /* (73) orconf ::= */
156748 229, /* (74) orconf ::= OR resolvetype */
156749 230, /* (75) resolvetype ::= IGNORE */
156750 230, /* (76) resolvetype ::= REPLACE */
156751 186, /* (77) cmd ::= DROP TABLE ifexists fullname */
156752 232, /* (78) ifexists ::= IF EXISTS */
156753 232, /* (79) ifexists ::= */
156754 186, /* (80) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
156755 186, /* (81) cmd ::= DROP VIEW ifexists fullname */
156756 186, /* (82) cmd ::= select */
156757 200, /* (83) select ::= WITH wqlist selectnowith */
156758 200, /* (84) select ::= WITH RECURSIVE wqlist selectnowith */
156759 200, /* (85) select ::= selectnowith */
156760 234, /* (86) selectnowith ::= selectnowith multiselect_op oneselect */
156761 237, /* (87) multiselect_op ::= UNION */
156762 237, /* (88) multiselect_op ::= UNION ALL */
156763 237, /* (89) multiselect_op ::= EXCEPT|INTERSECT */
156764 235, /* (90) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
156765 235, /* (91) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
156766 247, /* (92) values ::= VALUES LP nexprlist RP */
156767 247, /* (93) values ::= values COMMA LP nexprlist RP */
156768 238, /* (94) distinct ::= DISTINCT */
156769 238, /* (95) distinct ::= ALL */
156770 238, /* (96) distinct ::= */
156771 249, /* (97) sclp ::= */
156772 239, /* (98) selcollist ::= sclp scanpt expr scanpt as */
156773 239, /* (99) selcollist ::= sclp scanpt STAR */
156774 239, /* (100) selcollist ::= sclp scanpt nm DOT STAR */
156775 250, /* (101) as ::= AS nm */
156776 250, /* (102) as ::= */
156777 240, /* (103) from ::= */
156778 240, /* (104) from ::= FROM seltablist */
156779 252, /* (105) stl_prefix ::= seltablist joinop */
156780 252, /* (106) stl_prefix ::= */
156781 251, /* (107) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
156782 251, /* (108) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
156783 251, /* (109) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
156784 251, /* (110) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
156785 196, /* (111) dbnm ::= */
156786 196, /* (112) dbnm ::= DOT nm */
156787 233, /* (113) fullname ::= nm */
156788 233, /* (114) fullname ::= nm DOT nm */
156789 258, /* (115) xfullname ::= nm */
156790 258, /* (116) xfullname ::= nm DOT nm */
156791 258, /* (117) xfullname ::= nm DOT nm AS nm */
156792 258, /* (118) xfullname ::= nm AS nm */
156793 253, /* (119) joinop ::= COMMA|JOIN */
156794 253, /* (120) joinop ::= JOIN_KW JOIN */
156795 253, /* (121) joinop ::= JOIN_KW nm JOIN */
156796 253, /* (122) joinop ::= JOIN_KW nm nm JOIN */
156797 255, /* (123) on_opt ::= ON expr */
156798 255, /* (124) on_opt ::= */
156799 254, /* (125) indexed_opt ::= */
156800 254, /* (126) indexed_opt ::= INDEXED BY nm */
156801 254, /* (127) indexed_opt ::= NOT INDEXED */
156802 256, /* (128) using_opt ::= USING LP idlist RP */
156803 256, /* (129) using_opt ::= */
156804 244, /* (130) orderby_opt ::= */
156805 244, /* (131) orderby_opt ::= ORDER BY sortlist */
156806 226, /* (132) sortlist ::= sortlist COMMA expr sortorder nulls */
156807 226, /* (133) sortlist ::= expr sortorder nulls */
156808 214, /* (134) sortorder ::= ASC */
156809 214, /* (135) sortorder ::= DESC */
156810 214, /* (136) sortorder ::= */
156811 260, /* (137) nulls ::= NULLS FIRST */
156812 260, /* (138) nulls ::= NULLS LAST */
156813 260, /* (139) nulls ::= */
156814 242, /* (140) groupby_opt ::= */
156815 242, /* (141) groupby_opt ::= GROUP BY nexprlist */
156816 243, /* (142) having_opt ::= */
156817 243, /* (143) having_opt ::= HAVING expr */
156818 245, /* (144) limit_opt ::= */
156819 245, /* (145) limit_opt ::= LIMIT expr */
156820 245, /* (146) limit_opt ::= LIMIT expr OFFSET expr */
156821 245, /* (147) limit_opt ::= LIMIT expr COMMA expr */
156822 186, /* (148) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
156823 241, /* (149) where_opt ::= */
156824 241, /* (150) where_opt ::= WHERE expr */
156825 186, /* (151) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt */
156826 262, /* (152) setlist ::= setlist COMMA nm EQ expr */
156827 262, /* (153) setlist ::= setlist COMMA LP idlist RP EQ expr */
156828 262, /* (154) setlist ::= nm EQ expr */
156829 262, /* (155) setlist ::= LP idlist RP EQ expr */
156830 186, /* (156) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
156831 186, /* (157) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
156832 265, /* (158) upsert ::= */
156833 265, /* (159) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
156834 265, /* (160) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
156835 265, /* (161) upsert ::= ON CONFLICT DO NOTHING */
156836 263, /* (162) insert_cmd ::= INSERT orconf */
156837 263, /* (163) insert_cmd ::= REPLACE */
156838 264, /* (164) idlist_opt ::= */
156839 264, /* (165) idlist_opt ::= LP idlist RP */
156840 259, /* (166) idlist ::= idlist COMMA nm */
156841 259, /* (167) idlist ::= nm */
156842 212, /* (168) expr ::= LP expr RP */
156843 212, /* (169) expr ::= ID|INDEXED */
156844 212, /* (170) expr ::= JOIN_KW */
156845 212, /* (171) expr ::= nm DOT nm */
156846 212, /* (172) expr ::= nm DOT nm DOT nm */
156847 211, /* (173) term ::= NULL|FLOAT|BLOB */
156848 211, /* (174) term ::= STRING */
156849 211, /* (175) term ::= INTEGER */
156850 212, /* (176) expr ::= VARIABLE */
156851 212, /* (177) expr ::= expr COLLATE ID|STRING */
156852 212, /* (178) expr ::= CAST LP expr AS typetoken RP */
156853 212, /* (179) expr ::= ID|INDEXED LP distinct exprlist RP */
156854 212, /* (180) expr ::= ID|INDEXED LP STAR RP */
156855 212, /* (181) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
156856 212, /* (182) expr ::= ID|INDEXED LP STAR RP filter_over */
156857 211, /* (183) term ::= CTIME_KW */
156858 212, /* (184) expr ::= LP nexprlist COMMA expr RP */
156859 212, /* (185) expr ::= expr AND expr */
156860 212, /* (186) expr ::= expr OR expr */
156861 212, /* (187) expr ::= expr LT|GT|GE|LE expr */
156862 212, /* (188) expr ::= expr EQ|NE expr */
156863 212, /* (189) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
156864 212, /* (190) expr ::= expr PLUS|MINUS expr */
156865 212, /* (191) expr ::= expr STAR|SLASH|REM expr */
156866 212, /* (192) expr ::= expr CONCAT expr */
156867 267, /* (193) likeop ::= NOT LIKE_KW|MATCH */
156868 212, /* (194) expr ::= expr likeop expr */
156869 212, /* (195) expr ::= expr likeop expr ESCAPE expr */
156870 212, /* (196) expr ::= expr ISNULL|NOTNULL */
156871 212, /* (197) expr ::= expr NOT NULL */
156872 212, /* (198) expr ::= expr IS expr */
156873 212, /* (199) expr ::= expr IS NOT expr */
156874 212, /* (200) expr ::= NOT expr */
156875 212, /* (201) expr ::= BITNOT expr */
156876 212, /* (202) expr ::= PLUS|MINUS expr */
156877 268, /* (203) between_op ::= BETWEEN */
156878 268, /* (204) between_op ::= NOT BETWEEN */
156879 212, /* (205) expr ::= expr between_op expr AND expr */
156880 269, /* (206) in_op ::= IN */
156881 269, /* (207) in_op ::= NOT IN */
156882 212, /* (208) expr ::= expr in_op LP exprlist RP */
156883 212, /* (209) expr ::= LP select RP */
156884 212, /* (210) expr ::= expr in_op LP select RP */
156885 212, /* (211) expr ::= expr in_op nm dbnm paren_exprlist */
156886 212, /* (212) expr ::= EXISTS LP select RP */
156887 212, /* (213) expr ::= CASE case_operand case_exprlist case_else END */
156888 272, /* (214) case_exprlist ::= case_exprlist WHEN expr THEN expr */
156889 272, /* (215) case_exprlist ::= WHEN expr THEN expr */
156890 273, /* (216) case_else ::= ELSE expr */
156891 273, /* (217) case_else ::= */
156892 271, /* (218) case_operand ::= expr */
156893 271, /* (219) case_operand ::= */
156894 257, /* (220) exprlist ::= */
156895 248, /* (221) nexprlist ::= nexprlist COMMA expr */
156896 248, /* (222) nexprlist ::= expr */
156897 270, /* (223) paren_exprlist ::= */
156898 270, /* (224) paren_exprlist ::= LP exprlist RP */
156899 186, /* (225) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
156900 274, /* (226) uniqueflag ::= UNIQUE */
156901 274, /* (227) uniqueflag ::= */
156902 216, /* (228) eidlist_opt ::= */
156903 216, /* (229) eidlist_opt ::= LP eidlist RP */
156904 227, /* (230) eidlist ::= eidlist COMMA nm collate sortorder */
156905 227, /* (231) eidlist ::= nm collate sortorder */
156906 275, /* (232) collate ::= */
156907 275, /* (233) collate ::= COLLATE ID|STRING */
156908 186, /* (234) cmd ::= DROP INDEX ifexists fullname */
156909 186, /* (235) cmd ::= VACUUM vinto */
156910 186, /* (236) cmd ::= VACUUM nm vinto */
156911 276, /* (237) vinto ::= INTO expr */
156912 276, /* (238) vinto ::= */
156913 186, /* (239) cmd ::= PRAGMA nm dbnm */
156914 186, /* (240) cmd ::= PRAGMA nm dbnm EQ nmnum */
156915 186, /* (241) cmd ::= PRAGMA nm dbnm LP nmnum RP */
156916 186, /* (242) cmd ::= PRAGMA nm dbnm EQ minus_num */
156917 186, /* (243) cmd ::= PRAGMA nm dbnm LP minus_num RP */
156918 206, /* (244) plus_num ::= PLUS INTEGER|FLOAT */
156919 207, /* (245) minus_num ::= MINUS INTEGER|FLOAT */
156920 186, /* (246) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
156921 278, /* (247) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
156922 280, /* (248) trigger_time ::= BEFORE|AFTER */
156923 280, /* (249) trigger_time ::= INSTEAD OF */
156924 280, /* (250) trigger_time ::= */
156925 281, /* (251) trigger_event ::= DELETE|INSERT */
156926 281, /* (252) trigger_event ::= UPDATE */
156927 281, /* (253) trigger_event ::= UPDATE OF idlist */
156928 283, /* (254) when_clause ::= */
156929 283, /* (255) when_clause ::= WHEN expr */
156930 279, /* (256) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
156931 279, /* (257) trigger_cmd_list ::= trigger_cmd SEMI */
156932 285, /* (258) trnm ::= nm DOT nm */
156933 286, /* (259) tridxby ::= INDEXED BY nm */
156934 286, /* (260) tridxby ::= NOT INDEXED */
156935 284, /* (261) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
156936 284, /* (262) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
156937 284, /* (263) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
156938 284, /* (264) trigger_cmd ::= scanpt select scanpt */
156939 212, /* (265) expr ::= RAISE LP IGNORE RP */
156940 212, /* (266) expr ::= RAISE LP raisetype COMMA nm RP */
156941 231, /* (267) raisetype ::= ROLLBACK */
156942 231, /* (268) raisetype ::= ABORT */
156943 231, /* (269) raisetype ::= FAIL */
156944 186, /* (270) cmd ::= DROP TRIGGER ifexists fullname */
156945 186, /* (271) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
156946 186, /* (272) cmd ::= DETACH database_kw_opt expr */
156947 288, /* (273) key_opt ::= */
156948 288, /* (274) key_opt ::= KEY expr */
156949 186, /* (275) cmd ::= REINDEX */
156950 186, /* (276) cmd ::= REINDEX nm dbnm */
156951 186, /* (277) cmd ::= ANALYZE */
156952 186, /* (278) cmd ::= ANALYZE nm dbnm */
156953 186, /* (279) cmd ::= ALTER TABLE fullname RENAME TO nm */
156954 186, /* (280) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
156955 289, /* (281) add_column_fullname ::= fullname */
156956 186, /* (282) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
156957 186, /* (283) cmd ::= create_vtab */
156958 186, /* (284) cmd ::= create_vtab LP vtabarglist RP */
156959 291, /* (285) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
156960 293, /* (286) vtabarg ::= */
156961 294, /* (287) vtabargtoken ::= ANY */
156962 294, /* (288) vtabargtoken ::= lp anylist RP */
156963 295, /* (289) lp ::= LP */
156964 261, /* (290) with ::= WITH wqlist */
156965 261, /* (291) with ::= WITH RECURSIVE wqlist */
156966 236, /* (292) wqlist ::= nm eidlist_opt AS LP select RP */
156967 236, /* (293) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
156968 297, /* (294) windowdefn_list ::= windowdefn */
156969 297, /* (295) windowdefn_list ::= windowdefn_list COMMA windowdefn */
156970 298, /* (296) windowdefn ::= nm AS LP window RP */
156971 299, /* (297) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
156972 299, /* (298) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
156973 299, /* (299) window ::= ORDER BY sortlist frame_opt */
156974 299, /* (300) window ::= nm ORDER BY sortlist frame_opt */
156975 299, /* (301) window ::= frame_opt */
156976 299, /* (302) window ::= nm frame_opt */
156977 300, /* (303) frame_opt ::= */
156978 300, /* (304) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
156979 300, /* (305) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
156980 304, /* (306) range_or_rows ::= RANGE|ROWS|GROUPS */
156981 306, /* (307) frame_bound_s ::= frame_bound */
156982 306, /* (308) frame_bound_s ::= UNBOUNDED PRECEDING */
156983 307, /* (309) frame_bound_e ::= frame_bound */
156984 307, /* (310) frame_bound_e ::= UNBOUNDED FOLLOWING */
156985 305, /* (311) frame_bound ::= expr PRECEDING|FOLLOWING */
156986 305, /* (312) frame_bound ::= CURRENT ROW */
156987 308, /* (313) frame_exclude_opt ::= */
156988 308, /* (314) frame_exclude_opt ::= EXCLUDE frame_exclude */
156989 309, /* (315) frame_exclude ::= NO OTHERS */
156990 309, /* (316) frame_exclude ::= CURRENT ROW */
156991 309, /* (317) frame_exclude ::= GROUP|TIES */
156992 246, /* (318) window_clause ::= WINDOW windowdefn_list */
156993 266, /* (319) filter_over ::= filter_clause over_clause */
156994 266, /* (320) filter_over ::= over_clause */
156995 266, /* (321) filter_over ::= filter_clause */
156996 303, /* (322) over_clause ::= OVER LP window RP */
156997 303, /* (323) over_clause ::= OVER nm */
156998 302, /* (324) filter_clause ::= FILTER LP WHERE expr RP */
156999 181, /* (325) input ::= cmdlist */
157000 182, /* (326) cmdlist ::= cmdlist ecmd */
157001 182, /* (327) cmdlist ::= ecmd */
157002 183, /* (328) ecmd ::= SEMI */
157003 183, /* (329) ecmd ::= cmdx SEMI */
157004 183, /* (330) ecmd ::= explain cmdx SEMI */
157005 188, /* (331) trans_opt ::= */
157006 188, /* (332) trans_opt ::= TRANSACTION */
157007 188, /* (333) trans_opt ::= TRANSACTION nm */
157008 190, /* (334) savepoint_opt ::= SAVEPOINT */
157009 190, /* (335) savepoint_opt ::= */
157010 186, /* (336) cmd ::= create_table create_table_args */
157011 197, /* (337) columnlist ::= columnlist COMMA columnname carglist */
157012 197, /* (338) columnlist ::= columnname carglist */
157013 189, /* (339) nm ::= ID|INDEXED */
157014 189, /* (340) nm ::= STRING */
157015 189, /* (341) nm ::= JOIN_KW */
157016 203, /* (342) typetoken ::= typename */
157017 204, /* (343) typename ::= ID|STRING */
157018 205, /* (344) signed ::= plus_num */
157019 205, /* (345) signed ::= minus_num */
157020 202, /* (346) carglist ::= carglist ccons */
157021 202, /* (347) carglist ::= */
157022 210, /* (348) ccons ::= NULL onconf */
157023 210, /* (349) ccons ::= GENERATED ALWAYS AS generated */
157024 210, /* (350) ccons ::= AS generated */
157025 198, /* (351) conslist_opt ::= COMMA conslist */
157026 223, /* (352) conslist ::= conslist tconscomma tcons */
157027 223, /* (353) conslist ::= tcons */
157028 224, /* (354) tconscomma ::= */
157029 228, /* (355) defer_subclause_opt ::= defer_subclause */
157030 230, /* (356) resolvetype ::= raisetype */
157031 234, /* (357) selectnowith ::= oneselect */
157032 235, /* (358) oneselect ::= values */
157033 249, /* (359) sclp ::= selcollist COMMA */
157034 250, /* (360) as ::= ID|STRING */
157035 212, /* (361) expr ::= term */
157036 267, /* (362) likeop ::= LIKE_KW|MATCH */
157037 257, /* (363) exprlist ::= nexprlist */
157038 277, /* (364) nmnum ::= plus_num */
157039 277, /* (365) nmnum ::= nm */
157040 277, /* (366) nmnum ::= ON */
157041 277, /* (367) nmnum ::= DELETE */
157042 277, /* (368) nmnum ::= DEFAULT */
157043 206, /* (369) plus_num ::= INTEGER|FLOAT */
157044 282, /* (370) foreach_clause ::= */
157045 282, /* (371) foreach_clause ::= FOR EACH ROW */
157046 285, /* (372) trnm ::= nm */
157047 286, /* (373) tridxby ::= */
157048 287, /* (374) database_kw_opt ::= DATABASE */
157049 287, /* (375) database_kw_opt ::= */
157050 290, /* (376) kwcolumn_opt ::= */
157051 290, /* (377) kwcolumn_opt ::= COLUMNKW */
157052 292, /* (378) vtabarglist ::= vtabarg */
157053 292, /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */
157054 293, /* (380) vtabarg ::= vtabarg vtabargtoken */
157055 296, /* (381) anylist ::= */
157056 296, /* (382) anylist ::= anylist LP anylist RP */
157057 296, /* (383) anylist ::= anylist ANY */
157058 261, /* (384) with ::= */
157059};
157060
157061/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
157062** of symbols on the right-hand side of that rule. */
157063static const signed char yyRuleInfoNRhs[] = {
157064 -1, /* (0) explain ::= EXPLAIN */
157065 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
157066 -1, /* (2) cmdx ::= cmd */
157067 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
157068 0, /* (4) transtype ::= */
157069 -1, /* (5) transtype ::= DEFERRED */
157070 -1, /* (6) transtype ::= IMMEDIATE */
157071 -1, /* (7) transtype ::= EXCLUSIVE */
157072 -2, /* (8) cmd ::= COMMIT|END trans_opt */
157073 -2, /* (9) cmd ::= ROLLBACK trans_opt */
157074 -2, /* (10) cmd ::= SAVEPOINT nm */
157075 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
157076 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
157077 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
157078 -1, /* (14) createkw ::= CREATE */
157079 0, /* (15) ifnotexists ::= */
157080 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
157081 -1, /* (17) temp ::= TEMP */
157082 0, /* (18) temp ::= */
157083 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
157084 -2, /* (20) create_table_args ::= AS select */
157085 0, /* (21) table_options ::= */
157086 -2, /* (22) table_options ::= WITHOUT nm */
157087 -2, /* (23) columnname ::= nm typetoken */
157088 0, /* (24) typetoken ::= */
157089 -4, /* (25) typetoken ::= typename LP signed RP */
157090 -6, /* (26) typetoken ::= typename LP signed COMMA signed RP */
157091 -2, /* (27) typename ::= typename ID|STRING */
157092 0, /* (28) scanpt ::= */
157093 0, /* (29) scantok ::= */
157094 -2, /* (30) ccons ::= CONSTRAINT nm */
157095 -3, /* (31) ccons ::= DEFAULT scantok term */
157096 -4, /* (32) ccons ::= DEFAULT LP expr RP */
157097 -4, /* (33) ccons ::= DEFAULT PLUS scantok term */
157098 -4, /* (34) ccons ::= DEFAULT MINUS scantok term */
157099 -3, /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
157100 -3, /* (36) ccons ::= NOT NULL onconf */
157101 -5, /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
157102 -2, /* (38) ccons ::= UNIQUE onconf */
157103 -4, /* (39) ccons ::= CHECK LP expr RP */
157104 -4, /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
157105 -1, /* (41) ccons ::= defer_subclause */
157106 -2, /* (42) ccons ::= COLLATE ID|STRING */
157107 -3, /* (43) generated ::= LP expr RP */
157108 -4, /* (44) generated ::= LP expr RP ID */
157109 0, /* (45) autoinc ::= */
157110 -1, /* (46) autoinc ::= AUTOINCR */
157111 0, /* (47) refargs ::= */
157112 -2, /* (48) refargs ::= refargs refarg */
157113 -2, /* (49) refarg ::= MATCH nm */
157114 -3, /* (50) refarg ::= ON INSERT refact */
157115 -3, /* (51) refarg ::= ON DELETE refact */
157116 -3, /* (52) refarg ::= ON UPDATE refact */
157117 -2, /* (53) refact ::= SET NULL */
157118 -2, /* (54) refact ::= SET DEFAULT */
157119 -1, /* (55) refact ::= CASCADE */
157120 -1, /* (56) refact ::= RESTRICT */
157121 -2, /* (57) refact ::= NO ACTION */
157122 -3, /* (58) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
157123 -2, /* (59) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
157124 0, /* (60) init_deferred_pred_opt ::= */
157125 -2, /* (61) init_deferred_pred_opt ::= INITIALLY DEFERRED */
157126 -2, /* (62) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
157127 0, /* (63) conslist_opt ::= */
157128 -1, /* (64) tconscomma ::= COMMA */
157129 -2, /* (65) tcons ::= CONSTRAINT nm */
157130 -7, /* (66) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
157131 -5, /* (67) tcons ::= UNIQUE LP sortlist RP onconf */
157132 -5, /* (68) tcons ::= CHECK LP expr RP onconf */
157133 -10, /* (69) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
157134 0, /* (70) defer_subclause_opt ::= */
157135 0, /* (71) onconf ::= */
157136 -3, /* (72) onconf ::= ON CONFLICT resolvetype */
157137 0, /* (73) orconf ::= */
157138 -2, /* (74) orconf ::= OR resolvetype */
157139 -1, /* (75) resolvetype ::= IGNORE */
157140 -1, /* (76) resolvetype ::= REPLACE */
157141 -4, /* (77) cmd ::= DROP TABLE ifexists fullname */
157142 -2, /* (78) ifexists ::= IF EXISTS */
157143 0, /* (79) ifexists ::= */
157144 -9, /* (80) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
157145 -4, /* (81) cmd ::= DROP VIEW ifexists fullname */
157146 -1, /* (82) cmd ::= select */
157147 -3, /* (83) select ::= WITH wqlist selectnowith */
157148 -4, /* (84) select ::= WITH RECURSIVE wqlist selectnowith */
157149 -1, /* (85) select ::= selectnowith */
157150 -3, /* (86) selectnowith ::= selectnowith multiselect_op oneselect */
157151 -1, /* (87) multiselect_op ::= UNION */
157152 -2, /* (88) multiselect_op ::= UNION ALL */
157153 -1, /* (89) multiselect_op ::= EXCEPT|INTERSECT */
157154 -9, /* (90) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
157155 -10, /* (91) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
157156 -4, /* (92) values ::= VALUES LP nexprlist RP */
157157 -5, /* (93) values ::= values COMMA LP nexprlist RP */
157158 -1, /* (94) distinct ::= DISTINCT */
157159 -1, /* (95) distinct ::= ALL */
157160 0, /* (96) distinct ::= */
157161 0, /* (97) sclp ::= */
157162 -5, /* (98) selcollist ::= sclp scanpt expr scanpt as */
157163 -3, /* (99) selcollist ::= sclp scanpt STAR */
157164 -5, /* (100) selcollist ::= sclp scanpt nm DOT STAR */
157165 -2, /* (101) as ::= AS nm */
157166 0, /* (102) as ::= */
157167 0, /* (103) from ::= */
157168 -2, /* (104) from ::= FROM seltablist */
157169 -2, /* (105) stl_prefix ::= seltablist joinop */
157170 0, /* (106) stl_prefix ::= */
157171 -7, /* (107) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
157172 -9, /* (108) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
157173 -7, /* (109) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
157174 -7, /* (110) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
157175 0, /* (111) dbnm ::= */
157176 -2, /* (112) dbnm ::= DOT nm */
157177 -1, /* (113) fullname ::= nm */
157178 -3, /* (114) fullname ::= nm DOT nm */
157179 -1, /* (115) xfullname ::= nm */
157180 -3, /* (116) xfullname ::= nm DOT nm */
157181 -5, /* (117) xfullname ::= nm DOT nm AS nm */
157182 -3, /* (118) xfullname ::= nm AS nm */
157183 -1, /* (119) joinop ::= COMMA|JOIN */
157184 -2, /* (120) joinop ::= JOIN_KW JOIN */
157185 -3, /* (121) joinop ::= JOIN_KW nm JOIN */
157186 -4, /* (122) joinop ::= JOIN_KW nm nm JOIN */
157187 -2, /* (123) on_opt ::= ON expr */
157188 0, /* (124) on_opt ::= */
157189 0, /* (125) indexed_opt ::= */
157190 -3, /* (126) indexed_opt ::= INDEXED BY nm */
157191 -2, /* (127) indexed_opt ::= NOT INDEXED */
157192 -4, /* (128) using_opt ::= USING LP idlist RP */
157193 0, /* (129) using_opt ::= */
157194 0, /* (130) orderby_opt ::= */
157195 -3, /* (131) orderby_opt ::= ORDER BY sortlist */
157196 -5, /* (132) sortlist ::= sortlist COMMA expr sortorder nulls */
157197 -3, /* (133) sortlist ::= expr sortorder nulls */
157198 -1, /* (134) sortorder ::= ASC */
157199 -1, /* (135) sortorder ::= DESC */
157200 0, /* (136) sortorder ::= */
157201 -2, /* (137) nulls ::= NULLS FIRST */
157202 -2, /* (138) nulls ::= NULLS LAST */
157203 0, /* (139) nulls ::= */
157204 0, /* (140) groupby_opt ::= */
157205 -3, /* (141) groupby_opt ::= GROUP BY nexprlist */
157206 0, /* (142) having_opt ::= */
157207 -2, /* (143) having_opt ::= HAVING expr */
157208 0, /* (144) limit_opt ::= */
157209 -2, /* (145) limit_opt ::= LIMIT expr */
157210 -4, /* (146) limit_opt ::= LIMIT expr OFFSET expr */
157211 -4, /* (147) limit_opt ::= LIMIT expr COMMA expr */
157212 -6, /* (148) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
157213 0, /* (149) where_opt ::= */
157214 -2, /* (150) where_opt ::= WHERE expr */
157215 -9, /* (151) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt */
157216 -5, /* (152) setlist ::= setlist COMMA nm EQ expr */
157217 -7, /* (153) setlist ::= setlist COMMA LP idlist RP EQ expr */
157218 -3, /* (154) setlist ::= nm EQ expr */
157219 -5, /* (155) setlist ::= LP idlist RP EQ expr */
157220 -7, /* (156) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
157221 -7, /* (157) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
157222 0, /* (158) upsert ::= */
157223 -11, /* (159) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
157224 -8, /* (160) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
157225 -4, /* (161) upsert ::= ON CONFLICT DO NOTHING */
157226 -2, /* (162) insert_cmd ::= INSERT orconf */
157227 -1, /* (163) insert_cmd ::= REPLACE */
157228 0, /* (164) idlist_opt ::= */
157229 -3, /* (165) idlist_opt ::= LP idlist RP */
157230 -3, /* (166) idlist ::= idlist COMMA nm */
157231 -1, /* (167) idlist ::= nm */
157232 -3, /* (168) expr ::= LP expr RP */
157233 -1, /* (169) expr ::= ID|INDEXED */
157234 -1, /* (170) expr ::= JOIN_KW */
157235 -3, /* (171) expr ::= nm DOT nm */
157236 -5, /* (172) expr ::= nm DOT nm DOT nm */
157237 -1, /* (173) term ::= NULL|FLOAT|BLOB */
157238 -1, /* (174) term ::= STRING */
157239 -1, /* (175) term ::= INTEGER */
157240 -1, /* (176) expr ::= VARIABLE */
157241 -3, /* (177) expr ::= expr COLLATE ID|STRING */
157242 -6, /* (178) expr ::= CAST LP expr AS typetoken RP */
157243 -5, /* (179) expr ::= ID|INDEXED LP distinct exprlist RP */
157244 -4, /* (180) expr ::= ID|INDEXED LP STAR RP */
157245 -6, /* (181) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
157246 -5, /* (182) expr ::= ID|INDEXED LP STAR RP filter_over */
157247 -1, /* (183) term ::= CTIME_KW */
157248 -5, /* (184) expr ::= LP nexprlist COMMA expr RP */
157249 -3, /* (185) expr ::= expr AND expr */
157250 -3, /* (186) expr ::= expr OR expr */
157251 -3, /* (187) expr ::= expr LT|GT|GE|LE expr */
157252 -3, /* (188) expr ::= expr EQ|NE expr */
157253 -3, /* (189) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
157254 -3, /* (190) expr ::= expr PLUS|MINUS expr */
157255 -3, /* (191) expr ::= expr STAR|SLASH|REM expr */
157256 -3, /* (192) expr ::= expr CONCAT expr */
157257 -2, /* (193) likeop ::= NOT LIKE_KW|MATCH */
157258 -3, /* (194) expr ::= expr likeop expr */
157259 -5, /* (195) expr ::= expr likeop expr ESCAPE expr */
157260 -2, /* (196) expr ::= expr ISNULL|NOTNULL */
157261 -3, /* (197) expr ::= expr NOT NULL */
157262 -3, /* (198) expr ::= expr IS expr */
157263 -4, /* (199) expr ::= expr IS NOT expr */
157264 -2, /* (200) expr ::= NOT expr */
157265 -2, /* (201) expr ::= BITNOT expr */
157266 -2, /* (202) expr ::= PLUS|MINUS expr */
157267 -1, /* (203) between_op ::= BETWEEN */
157268 -2, /* (204) between_op ::= NOT BETWEEN */
157269 -5, /* (205) expr ::= expr between_op expr AND expr */
157270 -1, /* (206) in_op ::= IN */
157271 -2, /* (207) in_op ::= NOT IN */
157272 -5, /* (208) expr ::= expr in_op LP exprlist RP */
157273 -3, /* (209) expr ::= LP select RP */
157274 -5, /* (210) expr ::= expr in_op LP select RP */
157275 -5, /* (211) expr ::= expr in_op nm dbnm paren_exprlist */
157276 -4, /* (212) expr ::= EXISTS LP select RP */
157277 -5, /* (213) expr ::= CASE case_operand case_exprlist case_else END */
157278 -5, /* (214) case_exprlist ::= case_exprlist WHEN expr THEN expr */
157279 -4, /* (215) case_exprlist ::= WHEN expr THEN expr */
157280 -2, /* (216) case_else ::= ELSE expr */
157281 0, /* (217) case_else ::= */
157282 -1, /* (218) case_operand ::= expr */
157283 0, /* (219) case_operand ::= */
157284 0, /* (220) exprlist ::= */
157285 -3, /* (221) nexprlist ::= nexprlist COMMA expr */
157286 -1, /* (222) nexprlist ::= expr */
157287 0, /* (223) paren_exprlist ::= */
157288 -3, /* (224) paren_exprlist ::= LP exprlist RP */
157289 -12, /* (225) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
157290 -1, /* (226) uniqueflag ::= UNIQUE */
157291 0, /* (227) uniqueflag ::= */
157292 0, /* (228) eidlist_opt ::= */
157293 -3, /* (229) eidlist_opt ::= LP eidlist RP */
157294 -5, /* (230) eidlist ::= eidlist COMMA nm collate sortorder */
157295 -3, /* (231) eidlist ::= nm collate sortorder */
157296 0, /* (232) collate ::= */
157297 -2, /* (233) collate ::= COLLATE ID|STRING */
157298 -4, /* (234) cmd ::= DROP INDEX ifexists fullname */
157299 -2, /* (235) cmd ::= VACUUM vinto */
157300 -3, /* (236) cmd ::= VACUUM nm vinto */
157301 -2, /* (237) vinto ::= INTO expr */
157302 0, /* (238) vinto ::= */
157303 -3, /* (239) cmd ::= PRAGMA nm dbnm */
157304 -5, /* (240) cmd ::= PRAGMA nm dbnm EQ nmnum */
157305 -6, /* (241) cmd ::= PRAGMA nm dbnm LP nmnum RP */
157306 -5, /* (242) cmd ::= PRAGMA nm dbnm EQ minus_num */
157307 -6, /* (243) cmd ::= PRAGMA nm dbnm LP minus_num RP */
157308 -2, /* (244) plus_num ::= PLUS INTEGER|FLOAT */
157309 -2, /* (245) minus_num ::= MINUS INTEGER|FLOAT */
157310 -5, /* (246) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
157311 -11, /* (247) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
157312 -1, /* (248) trigger_time ::= BEFORE|AFTER */
157313 -2, /* (249) trigger_time ::= INSTEAD OF */
157314 0, /* (250) trigger_time ::= */
157315 -1, /* (251) trigger_event ::= DELETE|INSERT */
157316 -1, /* (252) trigger_event ::= UPDATE */
157317 -3, /* (253) trigger_event ::= UPDATE OF idlist */
157318 0, /* (254) when_clause ::= */
157319 -2, /* (255) when_clause ::= WHEN expr */
157320 -3, /* (256) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
157321 -2, /* (257) trigger_cmd_list ::= trigger_cmd SEMI */
157322 -3, /* (258) trnm ::= nm DOT nm */
157323 -3, /* (259) tridxby ::= INDEXED BY nm */
157324 -2, /* (260) tridxby ::= NOT INDEXED */
157325 -9, /* (261) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
157326 -8, /* (262) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
157327 -6, /* (263) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
157328 -3, /* (264) trigger_cmd ::= scanpt select scanpt */
157329 -4, /* (265) expr ::= RAISE LP IGNORE RP */
157330 -6, /* (266) expr ::= RAISE LP raisetype COMMA nm RP */
157331 -1, /* (267) raisetype ::= ROLLBACK */
157332 -1, /* (268) raisetype ::= ABORT */
157333 -1, /* (269) raisetype ::= FAIL */
157334 -4, /* (270) cmd ::= DROP TRIGGER ifexists fullname */
157335 -6, /* (271) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
157336 -3, /* (272) cmd ::= DETACH database_kw_opt expr */
157337 0, /* (273) key_opt ::= */
157338 -2, /* (274) key_opt ::= KEY expr */
157339 -1, /* (275) cmd ::= REINDEX */
157340 -3, /* (276) cmd ::= REINDEX nm dbnm */
157341 -1, /* (277) cmd ::= ANALYZE */
157342 -3, /* (278) cmd ::= ANALYZE nm dbnm */
157343 -6, /* (279) cmd ::= ALTER TABLE fullname RENAME TO nm */
157344 -7, /* (280) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
157345 -1, /* (281) add_column_fullname ::= fullname */
157346 -8, /* (282) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
157347 -1, /* (283) cmd ::= create_vtab */
157348 -4, /* (284) cmd ::= create_vtab LP vtabarglist RP */
157349 -8, /* (285) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
157350 0, /* (286) vtabarg ::= */
157351 -1, /* (287) vtabargtoken ::= ANY */
157352 -3, /* (288) vtabargtoken ::= lp anylist RP */
157353 -1, /* (289) lp ::= LP */
157354 -2, /* (290) with ::= WITH wqlist */
157355 -3, /* (291) with ::= WITH RECURSIVE wqlist */
157356 -6, /* (292) wqlist ::= nm eidlist_opt AS LP select RP */
157357 -8, /* (293) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
157358 -1, /* (294) windowdefn_list ::= windowdefn */
157359 -3, /* (295) windowdefn_list ::= windowdefn_list COMMA windowdefn */
157360 -5, /* (296) windowdefn ::= nm AS LP window RP */
157361 -5, /* (297) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
157362 -6, /* (298) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
157363 -4, /* (299) window ::= ORDER BY sortlist frame_opt */
157364 -5, /* (300) window ::= nm ORDER BY sortlist frame_opt */
157365 -1, /* (301) window ::= frame_opt */
157366 -2, /* (302) window ::= nm frame_opt */
157367 0, /* (303) frame_opt ::= */
157368 -3, /* (304) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
157369 -6, /* (305) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
157370 -1, /* (306) range_or_rows ::= RANGE|ROWS|GROUPS */
157371 -1, /* (307) frame_bound_s ::= frame_bound */
157372 -2, /* (308) frame_bound_s ::= UNBOUNDED PRECEDING */
157373 -1, /* (309) frame_bound_e ::= frame_bound */
157374 -2, /* (310) frame_bound_e ::= UNBOUNDED FOLLOWING */
157375 -2, /* (311) frame_bound ::= expr PRECEDING|FOLLOWING */
157376 -2, /* (312) frame_bound ::= CURRENT ROW */
157377 0, /* (313) frame_exclude_opt ::= */
157378 -2, /* (314) frame_exclude_opt ::= EXCLUDE frame_exclude */
157379 -2, /* (315) frame_exclude ::= NO OTHERS */
157380 -2, /* (316) frame_exclude ::= CURRENT ROW */
157381 -1, /* (317) frame_exclude ::= GROUP|TIES */
157382 -2, /* (318) window_clause ::= WINDOW windowdefn_list */
157383 -2, /* (319) filter_over ::= filter_clause over_clause */
157384 -1, /* (320) filter_over ::= over_clause */
157385 -1, /* (321) filter_over ::= filter_clause */
157386 -4, /* (322) over_clause ::= OVER LP window RP */
157387 -2, /* (323) over_clause ::= OVER nm */
157388 -5, /* (324) filter_clause ::= FILTER LP WHERE expr RP */
157389 -1, /* (325) input ::= cmdlist */
157390 -2, /* (326) cmdlist ::= cmdlist ecmd */
157391 -1, /* (327) cmdlist ::= ecmd */
157392 -1, /* (328) ecmd ::= SEMI */
157393 -2, /* (329) ecmd ::= cmdx SEMI */
157394 -3, /* (330) ecmd ::= explain cmdx SEMI */
157395 0, /* (331) trans_opt ::= */
157396 -1, /* (332) trans_opt ::= TRANSACTION */
157397 -2, /* (333) trans_opt ::= TRANSACTION nm */
157398 -1, /* (334) savepoint_opt ::= SAVEPOINT */
157399 0, /* (335) savepoint_opt ::= */
157400 -2, /* (336) cmd ::= create_table create_table_args */
157401 -4, /* (337) columnlist ::= columnlist COMMA columnname carglist */
157402 -2, /* (338) columnlist ::= columnname carglist */
157403 -1, /* (339) nm ::= ID|INDEXED */
157404 -1, /* (340) nm ::= STRING */
157405 -1, /* (341) nm ::= JOIN_KW */
157406 -1, /* (342) typetoken ::= typename */
157407 -1, /* (343) typename ::= ID|STRING */
157408 -1, /* (344) signed ::= plus_num */
157409 -1, /* (345) signed ::= minus_num */
157410 -2, /* (346) carglist ::= carglist ccons */
157411 0, /* (347) carglist ::= */
157412 -2, /* (348) ccons ::= NULL onconf */
157413 -4, /* (349) ccons ::= GENERATED ALWAYS AS generated */
157414 -2, /* (350) ccons ::= AS generated */
157415 -2, /* (351) conslist_opt ::= COMMA conslist */
157416 -3, /* (352) conslist ::= conslist tconscomma tcons */
157417 -1, /* (353) conslist ::= tcons */
157418 0, /* (354) tconscomma ::= */
157419 -1, /* (355) defer_subclause_opt ::= defer_subclause */
157420 -1, /* (356) resolvetype ::= raisetype */
157421 -1, /* (357) selectnowith ::= oneselect */
157422 -1, /* (358) oneselect ::= values */
157423 -2, /* (359) sclp ::= selcollist COMMA */
157424 -1, /* (360) as ::= ID|STRING */
157425 -1, /* (361) expr ::= term */
157426 -1, /* (362) likeop ::= LIKE_KW|MATCH */
157427 -1, /* (363) exprlist ::= nexprlist */
157428 -1, /* (364) nmnum ::= plus_num */
157429 -1, /* (365) nmnum ::= nm */
157430 -1, /* (366) nmnum ::= ON */
157431 -1, /* (367) nmnum ::= DELETE */
157432 -1, /* (368) nmnum ::= DEFAULT */
157433 -1, /* (369) plus_num ::= INTEGER|FLOAT */
157434 0, /* (370) foreach_clause ::= */
157435 -3, /* (371) foreach_clause ::= FOR EACH ROW */
157436 -1, /* (372) trnm ::= nm */
157437 0, /* (373) tridxby ::= */
157438 -1, /* (374) database_kw_opt ::= DATABASE */
157439 0, /* (375) database_kw_opt ::= */
157440 0, /* (376) kwcolumn_opt ::= */
157441 -1, /* (377) kwcolumn_opt ::= COLUMNKW */
157442 -1, /* (378) vtabarglist ::= vtabarg */
157443 -3, /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */
157444 -2, /* (380) vtabarg ::= vtabarg vtabargtoken */
157445 0, /* (381) anylist ::= */
157446 -4, /* (382) anylist ::= anylist LP anylist RP */
157447 -2, /* (383) anylist ::= anylist ANY */
157448 0, /* (384) with ::= */
157449};
157450
157451static void yy_accept(yyParser*); /* Forward Declaration */
157452
157453/*
157454** Perform a reduce action and the shift that must immediately
157455** follow the reduce.
157456**
157457** The yyLookahead and yyLookaheadToken parameters provide reduce actions
157458** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
157459** if the lookahead token has already been consumed. As this procedure is
157460** only called from one place, optimizing compilers will in-line it, which
157461** means that the extra parameters have no performance impact.
157462*/
157463static YYACTIONTYPE yy_reduce(
157464 yyParser *yypParser, /* The parser */
157465 unsigned int yyruleno, /* Number of the rule by which to reduce */
157466 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
157467 sqlite3ParserTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
157468 sqlite3ParserCTX_PDECL /* %extra_context */
157469){
157470 int yygoto; /* The next state */
157471 YYACTIONTYPE yyact; /* The next action */
157472 yyStackEntry *yymsp; /* The top of the parser's stack */
157473 int yysize; /* Amount to pop the stack */
157474 sqlite3ParserARG_FETCH
157475 (void)yyLookahead;
157476 (void)yyLookaheadToken;
157477 yymsp = yypParser->yytos;
157478#ifndef NDEBUG
157479 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
157480 yysize = yyRuleInfoNRhs[yyruleno];
157481 if( yysize ){
157482 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
157483 yyTracePrompt,
157484 yyruleno, yyRuleName[yyruleno],
157485 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
157486 yymsp[yysize].stateno);
157487 }else{
157488 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
157489 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
157490 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
157491 }
157492 }
157493#endif /* NDEBUG */
157494
157495 /* Check that the stack is large enough to grow by a single entry
157496 ** if the RHS of the rule is empty. This ensures that there is room
157497 ** enough on the stack to push the LHS value */
157498 if( yyRuleInfoNRhs[yyruleno]==0 ){
157499#ifdef YYTRACKMAXSTACKDEPTH
157500 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
157501 yypParser->yyhwm++;
157502 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
157503 }
157504#endif
157505#if YYSTACKDEPTH>0
157506 if( yypParser->yytos>=yypParser->yystackEnd ){
157507 yyStackOverflow(yypParser);
157508 /* The call to yyStackOverflow() above pops the stack until it is
157509 ** empty, causing the main parser loop to exit. So the return value
157510 ** is never used and does not matter. */
157511 return 0;
157512 }
157513#else
157514 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
157515 if( yyGrowStack(yypParser) ){
157516 yyStackOverflow(yypParser);
157517 /* The call to yyStackOverflow() above pops the stack until it is
157518 ** empty, causing the main parser loop to exit. So the return value
157519 ** is never used and does not matter. */
157520 return 0;
157521 }
157522 yymsp = yypParser->yytos;
157523 }
157524#endif
157525 }
157526
157527 switch( yyruleno ){
157528 /* Beginning here are the reduction cases. A typical example
157529 ** follows:
157530 ** case 0:
157531 ** #line <lineno> <grammarfile>
157532 ** { ... } // User supplied code
157533 ** #line <lineno> <thisfile>
157534 ** break;
157535 */
157536/********** Begin reduce actions **********************************************/
157537 YYMINORTYPE yylhsminor;
157538 case 0: /* explain ::= EXPLAIN */
157539{ pParse->explain = 1; }
157540 break;
157541 case 1: /* explain ::= EXPLAIN QUERY PLAN */
157542{ pParse->explain = 2; }
157543 break;
157544 case 2: /* cmdx ::= cmd */
157545{ sqlite3FinishCoding(pParse); }
157546 break;
157547 case 3: /* cmd ::= BEGIN transtype trans_opt */
157548{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy192);}
157549 break;
157550 case 4: /* transtype ::= */
157551{yymsp[1].minor.yy192 = TK_DEFERRED;}
157552 break;
157553 case 5: /* transtype ::= DEFERRED */
157554 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
157555 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
157556 case 306: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==306);
157557{yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-X*/}
157558 break;
157559 case 8: /* cmd ::= COMMIT|END trans_opt */
157560 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
157561{sqlite3EndTransaction(pParse,yymsp[-1].major);}
157562 break;
157563 case 10: /* cmd ::= SAVEPOINT nm */
157564{
157565 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
157566}
157567 break;
157568 case 11: /* cmd ::= RELEASE savepoint_opt nm */
157569{
157570 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
157571}
157572 break;
157573 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
157574{
157575 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
157576}
157577 break;
157578 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
157579{
157580 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy192,0,0,yymsp[-2].minor.yy192);
157581}
157582 break;
157583 case 14: /* createkw ::= CREATE */
157584{disableLookaside(pParse);}
157585 break;
157586 case 15: /* ifnotexists ::= */
157587 case 18: /* temp ::= */ yytestcase(yyruleno==18);
157588 case 21: /* table_options ::= */ yytestcase(yyruleno==21);
157589 case 45: /* autoinc ::= */ yytestcase(yyruleno==45);
157590 case 60: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==60);
157591 case 70: /* defer_subclause_opt ::= */ yytestcase(yyruleno==70);
157592 case 79: /* ifexists ::= */ yytestcase(yyruleno==79);
157593 case 96: /* distinct ::= */ yytestcase(yyruleno==96);
157594 case 232: /* collate ::= */ yytestcase(yyruleno==232);
157595{yymsp[1].minor.yy192 = 0;}
157596 break;
157597 case 16: /* ifnotexists ::= IF NOT EXISTS */
157598{yymsp[-2].minor.yy192 = 1;}
157599 break;
157600 case 17: /* temp ::= TEMP */
157601 case 46: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==46);
157602{yymsp[0].minor.yy192 = 1;}
157603 break;
157604 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
157605{
157606 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy192,0);
157607}
157608 break;
157609 case 20: /* create_table_args ::= AS select */
157610{
157611 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy539);
157612 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy539);
157613}
157614 break;
157615 case 22: /* table_options ::= WITHOUT nm */
157616{
157617 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
157618 yymsp[-1].minor.yy192 = TF_WithoutRowid | TF_NoVisibleRowid;
157619 }else{
157620 yymsp[-1].minor.yy192 = 0;
157621 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
157622 }
157623}
157624 break;
157625 case 23: /* columnname ::= nm typetoken */
157626{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
157627 break;
157628 case 24: /* typetoken ::= */
157629 case 63: /* conslist_opt ::= */ yytestcase(yyruleno==63);
157630 case 102: /* as ::= */ yytestcase(yyruleno==102);
157631{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
157632 break;
157633 case 25: /* typetoken ::= typename LP signed RP */
157634{
157635 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
157636}
157637 break;
157638 case 26: /* typetoken ::= typename LP signed COMMA signed RP */
157639{
157640 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
157641}
157642 break;
157643 case 27: /* typename ::= typename ID|STRING */
157644{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
157645 break;
157646 case 28: /* scanpt ::= */
157647{
157648 assert( yyLookahead!=YYNOCODE );
157649 yymsp[1].minor.yy436 = yyLookaheadToken.z;
157650}
157651 break;
157652 case 29: /* scantok ::= */
157653{
157654 assert( yyLookahead!=YYNOCODE );
157655 yymsp[1].minor.yy0 = yyLookaheadToken;
157656}
157657 break;
157658 case 30: /* ccons ::= CONSTRAINT nm */
157659 case 65: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==65);
157660{pParse->constraintName = yymsp[0].minor.yy0;}
157661 break;
157662 case 31: /* ccons ::= DEFAULT scantok term */
157663{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy202,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
157664 break;
157665 case 32: /* ccons ::= DEFAULT LP expr RP */
157666{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy202,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
157667 break;
157668 case 33: /* ccons ::= DEFAULT PLUS scantok term */
157669{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy202,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
157670 break;
157671 case 34: /* ccons ::= DEFAULT MINUS scantok term */
157672{
157673 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy202, 0);
157674 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
157675}
157676 break;
157677 case 35: /* ccons ::= DEFAULT scantok ID|INDEXED */
157678{
157679 Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
157680 if( p ){
157681 sqlite3ExprIdToTrueFalse(p);
157682 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
157683 }
157684 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
157685}
157686 break;
157687 case 36: /* ccons ::= NOT NULL onconf */
157688{sqlite3AddNotNull(pParse, yymsp[0].minor.yy192);}
157689 break;
157690 case 37: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
157691{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy192,yymsp[0].minor.yy192,yymsp[-2].minor.yy192);}
157692 break;
157693 case 38: /* ccons ::= UNIQUE onconf */
157694{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy192,0,0,0,0,
157695 SQLITE_IDXTYPE_UNIQUE);}
157696 break;
157697 case 39: /* ccons ::= CHECK LP expr RP */
157698{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy202);}
157699 break;
157700 case 40: /* ccons ::= REFERENCES nm eidlist_opt refargs */
157701{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy242,yymsp[0].minor.yy192);}
157702 break;
157703 case 41: /* ccons ::= defer_subclause */
157704{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy192);}
157705 break;
157706 case 42: /* ccons ::= COLLATE ID|STRING */
157707{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
157708 break;
157709 case 43: /* generated ::= LP expr RP */
157710{sqlite3AddGenerated(pParse,yymsp[-1].minor.yy202,0);}
157711 break;
157712 case 44: /* generated ::= LP expr RP ID */
157713{sqlite3AddGenerated(pParse,yymsp[-2].minor.yy202,&yymsp[0].minor.yy0);}
157714 break;
157715 case 47: /* refargs ::= */
157716{ yymsp[1].minor.yy192 = OE_None*0x0101; /* EV: R-19803-45884 */}
157717 break;
157718 case 48: /* refargs ::= refargs refarg */
157719{ yymsp[-1].minor.yy192 = (yymsp[-1].minor.yy192 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
157720 break;
157721 case 49: /* refarg ::= MATCH nm */
157722{ yymsp[-1].minor.yy207.value = 0; yymsp[-1].minor.yy207.mask = 0x000000; }
157723 break;
157724 case 50: /* refarg ::= ON INSERT refact */
157725{ yymsp[-2].minor.yy207.value = 0; yymsp[-2].minor.yy207.mask = 0x000000; }
157726 break;
157727 case 51: /* refarg ::= ON DELETE refact */
157728{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy192; yymsp[-2].minor.yy207.mask = 0x0000ff; }
157729 break;
157730 case 52: /* refarg ::= ON UPDATE refact */
157731{ yymsp[-2].minor.yy207.value = yymsp[0].minor.yy192<<8; yymsp[-2].minor.yy207.mask = 0x00ff00; }
157732 break;
157733 case 53: /* refact ::= SET NULL */
157734{ yymsp[-1].minor.yy192 = OE_SetNull; /* EV: R-33326-45252 */}
157735 break;
157736 case 54: /* refact ::= SET DEFAULT */
157737{ yymsp[-1].minor.yy192 = OE_SetDflt; /* EV: R-33326-45252 */}
157738 break;
157739 case 55: /* refact ::= CASCADE */
157740{ yymsp[0].minor.yy192 = OE_Cascade; /* EV: R-33326-45252 */}
157741 break;
157742 case 56: /* refact ::= RESTRICT */
157743{ yymsp[0].minor.yy192 = OE_Restrict; /* EV: R-33326-45252 */}
157744 break;
157745 case 57: /* refact ::= NO ACTION */
157746{ yymsp[-1].minor.yy192 = OE_None; /* EV: R-33326-45252 */}
157747 break;
157748 case 58: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
157749{yymsp[-2].minor.yy192 = 0;}
157750 break;
157751 case 59: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
157752 case 74: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==74);
157753 case 162: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==162);
157754{yymsp[-1].minor.yy192 = yymsp[0].minor.yy192;}
157755 break;
157756 case 61: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
157757 case 78: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==78);
157758 case 204: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==204);
157759 case 207: /* in_op ::= NOT IN */ yytestcase(yyruleno==207);
157760 case 233: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==233);
157761{yymsp[-1].minor.yy192 = 1;}
157762 break;
157763 case 62: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
157764{yymsp[-1].minor.yy192 = 0;}
157765 break;
157766 case 64: /* tconscomma ::= COMMA */
157767{pParse->constraintName.n = 0;}
157768 break;
157769 case 66: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
157770{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy242,yymsp[0].minor.yy192,yymsp[-2].minor.yy192,0);}
157771 break;
157772 case 67: /* tcons ::= UNIQUE LP sortlist RP onconf */
157773{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy242,yymsp[0].minor.yy192,0,0,0,0,
157774 SQLITE_IDXTYPE_UNIQUE);}
157775 break;
157776 case 68: /* tcons ::= CHECK LP expr RP onconf */
157777{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy202);}
157778 break;
157779 case 69: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
157780{
157781 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy242, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy242, yymsp[-1].minor.yy192);
157782 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy192);
157783}
157784 break;
157785 case 71: /* onconf ::= */
157786 case 73: /* orconf ::= */ yytestcase(yyruleno==73);
157787{yymsp[1].minor.yy192 = OE_Default;}
157788 break;
157789 case 72: /* onconf ::= ON CONFLICT resolvetype */
157790{yymsp[-2].minor.yy192 = yymsp[0].minor.yy192;}
157791 break;
157792 case 75: /* resolvetype ::= IGNORE */
157793{yymsp[0].minor.yy192 = OE_Ignore;}
157794 break;
157795 case 76: /* resolvetype ::= REPLACE */
157796 case 163: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==163);
157797{yymsp[0].minor.yy192 = OE_Replace;}
157798 break;
157799 case 77: /* cmd ::= DROP TABLE ifexists fullname */
157800{
157801 sqlite3DropTable(pParse, yymsp[0].minor.yy47, 0, yymsp[-1].minor.yy192);
157802}
157803 break;
157804 case 80: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
157805{
157806 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy242, yymsp[0].minor.yy539, yymsp[-7].minor.yy192, yymsp[-5].minor.yy192);
157807}
157808 break;
157809 case 81: /* cmd ::= DROP VIEW ifexists fullname */
157810{
157811 sqlite3DropTable(pParse, yymsp[0].minor.yy47, 1, yymsp[-1].minor.yy192);
157812}
157813 break;
157814 case 82: /* cmd ::= select */
157815{
157816 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
157817 sqlite3Select(pParse, yymsp[0].minor.yy539, &dest);
157818 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy539);
157819}
157820 break;
157821 case 83: /* select ::= WITH wqlist selectnowith */
157822{
157823 Select *p = yymsp[0].minor.yy539;
157824 if( p ){
157825 p->pWith = yymsp[-1].minor.yy131;
157826 parserDoubleLinkSelect(pParse, p);
157827 }else{
157828 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy131);
157829 }
157830 yymsp[-2].minor.yy539 = p;
157831}
157832 break;
157833 case 84: /* select ::= WITH RECURSIVE wqlist selectnowith */
157834{
157835 Select *p = yymsp[0].minor.yy539;
157836 if( p ){
157837 p->pWith = yymsp[-1].minor.yy131;
157838 parserDoubleLinkSelect(pParse, p);
157839 }else{
157840 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy131);
157841 }
157842 yymsp[-3].minor.yy539 = p;
157843}
157844 break;
157845 case 85: /* select ::= selectnowith */
157846{
157847 Select *p = yymsp[0].minor.yy539;
157848 if( p ){
157849 parserDoubleLinkSelect(pParse, p);
157850 }
157851 yymsp[0].minor.yy539 = p; /*A-overwrites-X*/
157852}
157853 break;
157854 case 86: /* selectnowith ::= selectnowith multiselect_op oneselect */
157855{
157856 Select *pRhs = yymsp[0].minor.yy539;
157857 Select *pLhs = yymsp[-2].minor.yy539;
157858 if( pRhs && pRhs->pPrior ){
157859 SrcList *pFrom;
157860 Token x;
157861 x.n = 0;
157862 parserDoubleLinkSelect(pParse, pRhs);
157863 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
157864 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
157865 }
157866 if( pRhs ){
157867 pRhs->op = (u8)yymsp[-1].minor.yy192;
157868 pRhs->pPrior = pLhs;
157869 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
157870 pRhs->selFlags &= ~SF_MultiValue;
157871 if( yymsp[-1].minor.yy192!=TK_ALL ) pParse->hasCompound = 1;
157872 }else{
157873 sqlite3SelectDelete(pParse->db, pLhs);
157874 }
157875 yymsp[-2].minor.yy539 = pRhs;
157876}
157877 break;
157878 case 87: /* multiselect_op ::= UNION */
157879 case 89: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==89);
157880{yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-OP*/}
157881 break;
157882 case 88: /* multiselect_op ::= UNION ALL */
157883{yymsp[-1].minor.yy192 = TK_ALL;}
157884 break;
157885 case 90: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
157886{
157887 yymsp[-8].minor.yy539 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy242,yymsp[-5].minor.yy47,yymsp[-4].minor.yy202,yymsp[-3].minor.yy242,yymsp[-2].minor.yy202,yymsp[-1].minor.yy242,yymsp[-7].minor.yy192,yymsp[0].minor.yy202);
157888}
157889 break;
157890 case 91: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
157891{
157892 yymsp[-9].minor.yy539 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy242,yymsp[-6].minor.yy47,yymsp[-5].minor.yy202,yymsp[-4].minor.yy242,yymsp[-3].minor.yy202,yymsp[-1].minor.yy242,yymsp[-8].minor.yy192,yymsp[0].minor.yy202);
157893 if( yymsp[-9].minor.yy539 ){
157894 yymsp[-9].minor.yy539->pWinDefn = yymsp[-2].minor.yy303;
157895 }else{
157896 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy303);
157897 }
157898}
157899 break;
157900 case 92: /* values ::= VALUES LP nexprlist RP */
157901{
157902 yymsp[-3].minor.yy539 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy242,0,0,0,0,0,SF_Values,0);
157903}
157904 break;
157905 case 93: /* values ::= values COMMA LP nexprlist RP */
157906{
157907 Select *pRight, *pLeft = yymsp[-4].minor.yy539;
157908 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy242,0,0,0,0,0,SF_Values|SF_MultiValue,0);
157909 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
157910 if( pRight ){
157911 pRight->op = TK_ALL;
157912 pRight->pPrior = pLeft;
157913 yymsp[-4].minor.yy539 = pRight;
157914 }else{
157915 yymsp[-4].minor.yy539 = pLeft;
157916 }
157917}
157918 break;
157919 case 94: /* distinct ::= DISTINCT */
157920{yymsp[0].minor.yy192 = SF_Distinct;}
157921 break;
157922 case 95: /* distinct ::= ALL */
157923{yymsp[0].minor.yy192 = SF_All;}
157924 break;
157925 case 97: /* sclp ::= */
157926 case 130: /* orderby_opt ::= */ yytestcase(yyruleno==130);
157927 case 140: /* groupby_opt ::= */ yytestcase(yyruleno==140);
157928 case 220: /* exprlist ::= */ yytestcase(yyruleno==220);
157929 case 223: /* paren_exprlist ::= */ yytestcase(yyruleno==223);
157930 case 228: /* eidlist_opt ::= */ yytestcase(yyruleno==228);
157931{yymsp[1].minor.yy242 = 0;}
157932 break;
157933 case 98: /* selcollist ::= sclp scanpt expr scanpt as */
157934{
157935 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy242, yymsp[-2].minor.yy202);
157936 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy242, &yymsp[0].minor.yy0, 1);
157937 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy242,yymsp[-3].minor.yy436,yymsp[-1].minor.yy436);
157938}
157939 break;
157940 case 99: /* selcollist ::= sclp scanpt STAR */
157941{
157942 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
157943 yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy242, p);
157944}
157945 break;
157946 case 100: /* selcollist ::= sclp scanpt nm DOT STAR */
157947{
157948 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
157949 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
157950 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
157951 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, pDot);
157952}
157953 break;
157954 case 101: /* as ::= AS nm */
157955 case 112: /* dbnm ::= DOT nm */ yytestcase(yyruleno==112);
157956 case 244: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==244);
157957 case 245: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==245);
157958{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
157959 break;
157960 case 103: /* from ::= */
157961 case 106: /* stl_prefix ::= */ yytestcase(yyruleno==106);
157962{yymsp[1].minor.yy47 = 0;}
157963 break;
157964 case 104: /* from ::= FROM seltablist */
157965{
157966 yymsp[-1].minor.yy47 = yymsp[0].minor.yy47;
157967 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy47);
157968}
157969 break;
157970 case 105: /* stl_prefix ::= seltablist joinop */
157971{
157972 if( ALWAYS(yymsp[-1].minor.yy47 && yymsp[-1].minor.yy47->nSrc>0) ) yymsp[-1].minor.yy47->a[yymsp[-1].minor.yy47->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy192;
157973}
157974 break;
157975 case 107: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
157976{
157977 yymsp[-6].minor.yy47 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy47,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy202,yymsp[0].minor.yy600);
157978 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy47, &yymsp[-2].minor.yy0);
157979}
157980 break;
157981 case 108: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
157982{
157983 yymsp[-8].minor.yy47 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy47,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy202,yymsp[0].minor.yy600);
157984 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy47, yymsp[-4].minor.yy242);
157985}
157986 break;
157987 case 109: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
157988{
157989 yymsp[-6].minor.yy47 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy47,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy539,yymsp[-1].minor.yy202,yymsp[0].minor.yy600);
157990 }
157991 break;
157992 case 110: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
157993{
157994 if( yymsp[-6].minor.yy47==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy202==0 && yymsp[0].minor.yy600==0 ){
157995 yymsp[-6].minor.yy47 = yymsp[-4].minor.yy47;
157996 }else if( yymsp[-4].minor.yy47->nSrc==1 ){
157997 yymsp[-6].minor.yy47 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy47,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy202,yymsp[0].minor.yy600);
157998 if( yymsp[-6].minor.yy47 ){
157999 struct SrcList_item *pNew = &yymsp[-6].minor.yy47->a[yymsp[-6].minor.yy47->nSrc-1];
158000 struct SrcList_item *pOld = yymsp[-4].minor.yy47->a;
158001 pNew->zName = pOld->zName;
158002 pNew->zDatabase = pOld->zDatabase;
158003 pNew->pSelect = pOld->pSelect;
158004 if( pOld->fg.isTabFunc ){
158005 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
158006 pOld->u1.pFuncArg = 0;
158007 pOld->fg.isTabFunc = 0;
158008 pNew->fg.isTabFunc = 1;
158009 }
158010 pOld->zName = pOld->zDatabase = 0;
158011 pOld->pSelect = 0;
158012 }
158013 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy47);
158014 }else{
158015 Select *pSubquery;
158016 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy47);
158017 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy47,0,0,0,0,SF_NestedFrom,0);
158018 yymsp[-6].minor.yy47 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy47,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy202,yymsp[0].minor.yy600);
158019 }
158020 }
158021 break;
158022 case 111: /* dbnm ::= */
158023 case 125: /* indexed_opt ::= */ yytestcase(yyruleno==125);
158024{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
158025 break;
158026 case 113: /* fullname ::= nm */
158027{
158028 yylhsminor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
158029 if( IN_RENAME_OBJECT && yylhsminor.yy47 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy47->a[0].zName, &yymsp[0].minor.yy0);
158030}
158031 yymsp[0].minor.yy47 = yylhsminor.yy47;
158032 break;
158033 case 114: /* fullname ::= nm DOT nm */
158034{
158035 yylhsminor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
158036 if( IN_RENAME_OBJECT && yylhsminor.yy47 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy47->a[0].zName, &yymsp[0].minor.yy0);
158037}
158038 yymsp[-2].minor.yy47 = yylhsminor.yy47;
158039 break;
158040 case 115: /* xfullname ::= nm */
158041{yymsp[0].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
158042 break;
158043 case 116: /* xfullname ::= nm DOT nm */
158044{yymsp[-2].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
158045 break;
158046 case 117: /* xfullname ::= nm DOT nm AS nm */
158047{
158048 yymsp[-4].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
158049 if( yymsp[-4].minor.yy47 ) yymsp[-4].minor.yy47->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
158050}
158051 break;
158052 case 118: /* xfullname ::= nm AS nm */
158053{
158054 yymsp[-2].minor.yy47 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
158055 if( yymsp[-2].minor.yy47 ) yymsp[-2].minor.yy47->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
158056}
158057 break;
158058 case 119: /* joinop ::= COMMA|JOIN */
158059{ yymsp[0].minor.yy192 = JT_INNER; }
158060 break;
158061 case 120: /* joinop ::= JOIN_KW JOIN */
158062{yymsp[-1].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
158063 break;
158064 case 121: /* joinop ::= JOIN_KW nm JOIN */
158065{yymsp[-2].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
158066 break;
158067 case 122: /* joinop ::= JOIN_KW nm nm JOIN */
158068{yymsp[-3].minor.yy192 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
158069 break;
158070 case 123: /* on_opt ::= ON expr */
158071 case 143: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==143);
158072 case 150: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==150);
158073 case 216: /* case_else ::= ELSE expr */ yytestcase(yyruleno==216);
158074 case 237: /* vinto ::= INTO expr */ yytestcase(yyruleno==237);
158075{yymsp[-1].minor.yy202 = yymsp[0].minor.yy202;}
158076 break;
158077 case 124: /* on_opt ::= */
158078 case 142: /* having_opt ::= */ yytestcase(yyruleno==142);
158079 case 144: /* limit_opt ::= */ yytestcase(yyruleno==144);
158080 case 149: /* where_opt ::= */ yytestcase(yyruleno==149);
158081 case 217: /* case_else ::= */ yytestcase(yyruleno==217);
158082 case 219: /* case_operand ::= */ yytestcase(yyruleno==219);
158083 case 238: /* vinto ::= */ yytestcase(yyruleno==238);
158084{yymsp[1].minor.yy202 = 0;}
158085 break;
158086 case 126: /* indexed_opt ::= INDEXED BY nm */
158087{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
158088 break;
158089 case 127: /* indexed_opt ::= NOT INDEXED */
158090{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
158091 break;
158092 case 128: /* using_opt ::= USING LP idlist RP */
158093{yymsp[-3].minor.yy600 = yymsp[-1].minor.yy600;}
158094 break;
158095 case 129: /* using_opt ::= */
158096 case 164: /* idlist_opt ::= */ yytestcase(yyruleno==164);
158097{yymsp[1].minor.yy600 = 0;}
158098 break;
158099 case 131: /* orderby_opt ::= ORDER BY sortlist */
158100 case 141: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==141);
158101{yymsp[-2].minor.yy242 = yymsp[0].minor.yy242;}
158102 break;
158103 case 132: /* sortlist ::= sortlist COMMA expr sortorder nulls */
158104{
158105 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242,yymsp[-2].minor.yy202);
158106 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy242,yymsp[-1].minor.yy192,yymsp[0].minor.yy192);
158107}
158108 break;
158109 case 133: /* sortlist ::= expr sortorder nulls */
158110{
158111 yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy202); /*A-overwrites-Y*/
158112 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy242,yymsp[-1].minor.yy192,yymsp[0].minor.yy192);
158113}
158114 break;
158115 case 134: /* sortorder ::= ASC */
158116{yymsp[0].minor.yy192 = SQLITE_SO_ASC;}
158117 break;
158118 case 135: /* sortorder ::= DESC */
158119{yymsp[0].minor.yy192 = SQLITE_SO_DESC;}
158120 break;
158121 case 136: /* sortorder ::= */
158122 case 139: /* nulls ::= */ yytestcase(yyruleno==139);
158123{yymsp[1].minor.yy192 = SQLITE_SO_UNDEFINED;}
158124 break;
158125 case 137: /* nulls ::= NULLS FIRST */
158126{yymsp[-1].minor.yy192 = SQLITE_SO_ASC;}
158127 break;
158128 case 138: /* nulls ::= NULLS LAST */
158129{yymsp[-1].minor.yy192 = SQLITE_SO_DESC;}
158130 break;
158131 case 145: /* limit_opt ::= LIMIT expr */
158132{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy202,0);}
158133 break;
158134 case 146: /* limit_opt ::= LIMIT expr OFFSET expr */
158135{yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
158136 break;
158137 case 147: /* limit_opt ::= LIMIT expr COMMA expr */
158138{yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy202,yymsp[-2].minor.yy202);}
158139 break;
158140 case 148: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
158141{
158142 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy47, &yymsp[-1].minor.yy0);
158143 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy47,yymsp[0].minor.yy202,0,0);
158144}
158145 break;
158146 case 151: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt */
158147{
158148 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy47, &yymsp[-4].minor.yy0);
158149 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy242,"set list");
158150 yymsp[-5].minor.yy47 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy47, yymsp[-1].minor.yy47);
158151 sqlite3Update(pParse,yymsp[-5].minor.yy47,yymsp[-2].minor.yy242,yymsp[0].minor.yy202,yymsp[-6].minor.yy192,0,0,0);
158152}
158153 break;
158154 case 152: /* setlist ::= setlist COMMA nm EQ expr */
158155{
158156 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy242, yymsp[0].minor.yy202);
158157 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy242, &yymsp[-2].minor.yy0, 1);
158158}
158159 break;
158160 case 153: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
158161{
158162 yymsp[-6].minor.yy242 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy242, yymsp[-3].minor.yy600, yymsp[0].minor.yy202);
158163}
158164 break;
158165 case 154: /* setlist ::= nm EQ expr */
158166{
158167 yylhsminor.yy242 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy202);
158168 sqlite3ExprListSetName(pParse, yylhsminor.yy242, &yymsp[-2].minor.yy0, 1);
158169}
158170 yymsp[-2].minor.yy242 = yylhsminor.yy242;
158171 break;
158172 case 155: /* setlist ::= LP idlist RP EQ expr */
158173{
158174 yymsp[-4].minor.yy242 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy600, yymsp[0].minor.yy202);
158175}
158176 break;
158177 case 156: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
158178{
158179 sqlite3Insert(pParse, yymsp[-3].minor.yy47, yymsp[-1].minor.yy539, yymsp[-2].minor.yy600, yymsp[-5].minor.yy192, yymsp[0].minor.yy318);
158180}
158181 break;
158182 case 157: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
158183{
158184 sqlite3Insert(pParse, yymsp[-3].minor.yy47, 0, yymsp[-2].minor.yy600, yymsp[-5].minor.yy192, 0);
158185}
158186 break;
158187 case 158: /* upsert ::= */
158188{ yymsp[1].minor.yy318 = 0; }
158189 break;
158190 case 159: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
158191{ yymsp[-10].minor.yy318 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy242,yymsp[-5].minor.yy202,yymsp[-1].minor.yy242,yymsp[0].minor.yy202);}
158192 break;
158193 case 160: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
158194{ yymsp[-7].minor.yy318 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy242,yymsp[-2].minor.yy202,0,0); }
158195 break;
158196 case 161: /* upsert ::= ON CONFLICT DO NOTHING */
158197{ yymsp[-3].minor.yy318 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
158198 break;
158199 case 165: /* idlist_opt ::= LP idlist RP */
158200{yymsp[-2].minor.yy600 = yymsp[-1].minor.yy600;}
158201 break;
158202 case 166: /* idlist ::= idlist COMMA nm */
158203{yymsp[-2].minor.yy600 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy600,&yymsp[0].minor.yy0);}
158204 break;
158205 case 167: /* idlist ::= nm */
158206{yymsp[0].minor.yy600 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
158207 break;
158208 case 168: /* expr ::= LP expr RP */
158209{yymsp[-2].minor.yy202 = yymsp[-1].minor.yy202;}
158210 break;
158211 case 169: /* expr ::= ID|INDEXED */
158212 case 170: /* expr ::= JOIN_KW */ yytestcase(yyruleno==170);
158213{yymsp[0].minor.yy202=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
158214 break;
158215 case 171: /* expr ::= nm DOT nm */
158216{
158217 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
158218 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
158219 if( IN_RENAME_OBJECT ){
158220 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
158221 sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
158222 }
158223 yylhsminor.yy202 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
158224}
158225 yymsp[-2].minor.yy202 = yylhsminor.yy202;
158226 break;
158227 case 172: /* expr ::= nm DOT nm DOT nm */
158228{
158229 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
158230 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
158231 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
158232 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
158233 if( IN_RENAME_OBJECT ){
158234 sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
158235 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
158236 }
158237 yylhsminor.yy202 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
158238}
158239 yymsp[-4].minor.yy202 = yylhsminor.yy202;
158240 break;
158241 case 173: /* term ::= NULL|FLOAT|BLOB */
158242 case 174: /* term ::= STRING */ yytestcase(yyruleno==174);
158243{yymsp[0].minor.yy202=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
158244 break;
158245 case 175: /* term ::= INTEGER */
158246{
158247 yylhsminor.yy202 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
158248}
158249 yymsp[0].minor.yy202 = yylhsminor.yy202;
158250 break;
158251 case 176: /* expr ::= VARIABLE */
158252{
158253 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
158254 u32 n = yymsp[0].minor.yy0.n;
158255 yymsp[0].minor.yy202 = tokenExpr(pParse, TK_VARIABLE, yymsp[0].minor.yy0);
158256 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy202, n);
158257 }else{
158258 /* When doing a nested parse, one can include terms in an expression
158259 ** that look like this: #1 #2 ... These terms refer to registers
158260 ** in the virtual machine. #N is the N-th register. */
158261 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
158262 assert( t.n>=2 );
158263 if( pParse->nested==0 ){
158264 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
158265 yymsp[0].minor.yy202 = 0;
158266 }else{
158267 yymsp[0].minor.yy202 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
158268 if( yymsp[0].minor.yy202 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy202->iTable);
158269 }
158270 }
158271}
158272 break;
158273 case 177: /* expr ::= expr COLLATE ID|STRING */
158274{
158275 yymsp[-2].minor.yy202 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy202, &yymsp[0].minor.yy0, 1);
158276}
158277 break;
158278 case 178: /* expr ::= CAST LP expr AS typetoken RP */
158279{
158280 yymsp[-5].minor.yy202 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
158281 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy202, yymsp[-3].minor.yy202, 0);
158282}
158283 break;
158284 case 179: /* expr ::= ID|INDEXED LP distinct exprlist RP */
158285{
158286 yylhsminor.yy202 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy242, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy192);
158287}
158288 yymsp[-4].minor.yy202 = yylhsminor.yy202;
158289 break;
158290 case 180: /* expr ::= ID|INDEXED LP STAR RP */
158291{
158292 yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
158293}
158294 yymsp[-3].minor.yy202 = yylhsminor.yy202;
158295 break;
158296 case 181: /* expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
158297{
158298 yylhsminor.yy202 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy242, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy192);
158299 sqlite3WindowAttach(pParse, yylhsminor.yy202, yymsp[0].minor.yy303);
158300}
158301 yymsp[-5].minor.yy202 = yylhsminor.yy202;
158302 break;
158303 case 182: /* expr ::= ID|INDEXED LP STAR RP filter_over */
158304{
158305 yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
158306 sqlite3WindowAttach(pParse, yylhsminor.yy202, yymsp[0].minor.yy303);
158307}
158308 yymsp[-4].minor.yy202 = yylhsminor.yy202;
158309 break;
158310 case 183: /* term ::= CTIME_KW */
158311{
158312 yylhsminor.yy202 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
158313}
158314 yymsp[0].minor.yy202 = yylhsminor.yy202;
158315 break;
158316 case 184: /* expr ::= LP nexprlist COMMA expr RP */
158317{
158318 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy242, yymsp[-1].minor.yy202);
158319 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
158320 if( yymsp[-4].minor.yy202 ){
158321 yymsp[-4].minor.yy202->x.pList = pList;
158322 if( ALWAYS(pList->nExpr) ){
158323 yymsp[-4].minor.yy202->flags |= pList->a[0].pExpr->flags & EP_Propagate;
158324 }
158325 }else{
158326 sqlite3ExprListDelete(pParse->db, pList);
158327 }
158328}
158329 break;
158330 case 185: /* expr ::= expr AND expr */
158331{yymsp[-2].minor.yy202=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
158332 break;
158333 case 186: /* expr ::= expr OR expr */
158334 case 187: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==187);
158335 case 188: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==188);
158336 case 189: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==189);
158337 case 190: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==190);
158338 case 191: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==191);
158339 case 192: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==192);
158340{yymsp[-2].minor.yy202=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);}
158341 break;
158342 case 193: /* likeop ::= NOT LIKE_KW|MATCH */
158343{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
158344 break;
158345 case 194: /* expr ::= expr likeop expr */
158346{
158347 ExprList *pList;
158348 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
158349 yymsp[-1].minor.yy0.n &= 0x7fffffff;
158350 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy202);
158351 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy202);
158352 yymsp[-2].minor.yy202 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
158353 if( bNot ) yymsp[-2].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy202, 0);
158354 if( yymsp[-2].minor.yy202 ) yymsp[-2].minor.yy202->flags |= EP_InfixFunc;
158355}
158356 break;
158357 case 195: /* expr ::= expr likeop expr ESCAPE expr */
158358{
158359 ExprList *pList;
158360 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
158361 yymsp[-3].minor.yy0.n &= 0x7fffffff;
158362 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
158363 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy202);
158364 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy202);
158365 yymsp[-4].minor.yy202 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
158366 if( bNot ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158367 if( yymsp[-4].minor.yy202 ) yymsp[-4].minor.yy202->flags |= EP_InfixFunc;
158368}
158369 break;
158370 case 196: /* expr ::= expr ISNULL|NOTNULL */
158371{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy202,0);}
158372 break;
158373 case 197: /* expr ::= expr NOT NULL */
158374{yymsp[-2].minor.yy202 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy202,0);}
158375 break;
158376 case 198: /* expr ::= expr IS expr */
158377{
158378 yymsp[-2].minor.yy202 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy202,yymsp[0].minor.yy202);
158379 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy202, yymsp[-2].minor.yy202, TK_ISNULL);
158380}
158381 break;
158382 case 199: /* expr ::= expr IS NOT expr */
158383{
158384 yymsp[-3].minor.yy202 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy202,yymsp[0].minor.yy202);
158385 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy202, yymsp[-3].minor.yy202, TK_NOTNULL);
158386}
158387 break;
158388 case 200: /* expr ::= NOT expr */
158389 case 201: /* expr ::= BITNOT expr */ yytestcase(yyruleno==201);
158390{yymsp[-1].minor.yy202 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy202, 0);/*A-overwrites-B*/}
158391 break;
158392 case 202: /* expr ::= PLUS|MINUS expr */
158393{
158394 yymsp[-1].minor.yy202 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy202, 0);
158395 /*A-overwrites-B*/
158396}
158397 break;
158398 case 203: /* between_op ::= BETWEEN */
158399 case 206: /* in_op ::= IN */ yytestcase(yyruleno==206);
158400{yymsp[0].minor.yy192 = 0;}
158401 break;
158402 case 205: /* expr ::= expr between_op expr AND expr */
158403{
158404 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
158405 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy202);
158406 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy202, 0);
158407 if( yymsp[-4].minor.yy202 ){
158408 yymsp[-4].minor.yy202->x.pList = pList;
158409 }else{
158410 sqlite3ExprListDelete(pParse->db, pList);
158411 }
158412 if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158413}
158414 break;
158415 case 208: /* expr ::= expr in_op LP exprlist RP */
158416{
158417 if( yymsp[-1].minor.yy242==0 ){
158418 /* Expressions of the form
158419 **
158420 ** expr1 IN ()
158421 ** expr1 NOT IN ()
158422 **
158423 ** simplify to constants 0 (false) and 1 (true), respectively,
158424 ** regardless of the value of expr1.
158425 */
158426 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy202);
158427 yymsp[-4].minor.yy202 = sqlite3Expr(pParse->db, TK_INTEGER, yymsp[-3].minor.yy192 ? "1" : "0");
158428 }else if( yymsp[-1].minor.yy242->nExpr==1 && sqlite3ExprIsConstant(yymsp[-1].minor.yy242->a[0].pExpr) ){
158429 Expr *pRHS = yymsp[-1].minor.yy242->a[0].pExpr;
158430 yymsp[-1].minor.yy242->a[0].pExpr = 0;
158431 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy242);
158432 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
158433 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy202, pRHS);
158434 if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158435 }else{
158436 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
158437 if( yymsp[-4].minor.yy202 ){
158438 yymsp[-4].minor.yy202->x.pList = yymsp[-1].minor.yy242;
158439 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy202);
158440 }else{
158441 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy242);
158442 }
158443 if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158444 }
158445 }
158446 break;
158447 case 209: /* expr ::= LP select RP */
158448{
158449 yymsp[-2].minor.yy202 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
158450 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy202, yymsp[-1].minor.yy539);
158451 }
158452 break;
158453 case 210: /* expr ::= expr in_op LP select RP */
158454{
158455 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
158456 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy202, yymsp[-1].minor.yy539);
158457 if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158458 }
158459 break;
158460 case 211: /* expr ::= expr in_op nm dbnm paren_exprlist */
158461{
158462 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
158463 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
158464 if( yymsp[0].minor.yy242 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy242);
158465 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy202, 0);
158466 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy202, pSelect);
158467 if( yymsp[-3].minor.yy192 ) yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy202, 0);
158468 }
158469 break;
158470 case 212: /* expr ::= EXISTS LP select RP */
158471{
158472 Expr *p;
158473 p = yymsp[-3].minor.yy202 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
158474 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy539);
158475 }
158476 break;
158477 case 213: /* expr ::= CASE case_operand case_exprlist case_else END */
158478{
158479 yymsp[-4].minor.yy202 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy202, 0);
158480 if( yymsp[-4].minor.yy202 ){
158481 yymsp[-4].minor.yy202->x.pList = yymsp[-1].minor.yy202 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy242,yymsp[-1].minor.yy202) : yymsp[-2].minor.yy242;
158482 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy202);
158483 }else{
158484 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy242);
158485 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy202);
158486 }
158487}
158488 break;
158489 case 214: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
158490{
158491 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, yymsp[-2].minor.yy202);
158492 yymsp[-4].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy242, yymsp[0].minor.yy202);
158493}
158494 break;
158495 case 215: /* case_exprlist ::= WHEN expr THEN expr */
158496{
158497 yymsp[-3].minor.yy242 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy202);
158498 yymsp[-3].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy242, yymsp[0].minor.yy202);
158499}
158500 break;
158501 case 218: /* case_operand ::= expr */
158502{yymsp[0].minor.yy202 = yymsp[0].minor.yy202; /*A-overwrites-X*/}
158503 break;
158504 case 221: /* nexprlist ::= nexprlist COMMA expr */
158505{yymsp[-2].minor.yy242 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy242,yymsp[0].minor.yy202);}
158506 break;
158507 case 222: /* nexprlist ::= expr */
158508{yymsp[0].minor.yy242 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy202); /*A-overwrites-Y*/}
158509 break;
158510 case 224: /* paren_exprlist ::= LP exprlist RP */
158511 case 229: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==229);
158512{yymsp[-2].minor.yy242 = yymsp[-1].minor.yy242;}
158513 break;
158514 case 225: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
158515{
158516 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
158517 sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy242, yymsp[-10].minor.yy192,
158518 &yymsp[-11].minor.yy0, yymsp[0].minor.yy202, SQLITE_SO_ASC, yymsp[-8].minor.yy192, SQLITE_IDXTYPE_APPDEF);
158519 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
158520 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
158521 }
158522}
158523 break;
158524 case 226: /* uniqueflag ::= UNIQUE */
158525 case 268: /* raisetype ::= ABORT */ yytestcase(yyruleno==268);
158526{yymsp[0].minor.yy192 = OE_Abort;}
158527 break;
158528 case 227: /* uniqueflag ::= */
158529{yymsp[1].minor.yy192 = OE_None;}
158530 break;
158531 case 230: /* eidlist ::= eidlist COMMA nm collate sortorder */
158532{
158533 yymsp[-4].minor.yy242 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy242, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy192, yymsp[0].minor.yy192);
158534}
158535 break;
158536 case 231: /* eidlist ::= nm collate sortorder */
158537{
158538 yymsp[-2].minor.yy242 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy192, yymsp[0].minor.yy192); /*A-overwrites-Y*/
158539}
158540 break;
158541 case 234: /* cmd ::= DROP INDEX ifexists fullname */
158542{sqlite3DropIndex(pParse, yymsp[0].minor.yy47, yymsp[-1].minor.yy192);}
158543 break;
158544 case 235: /* cmd ::= VACUUM vinto */
158545{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy202);}
158546 break;
158547 case 236: /* cmd ::= VACUUM nm vinto */
158548{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy202);}
158549 break;
158550 case 239: /* cmd ::= PRAGMA nm dbnm */
158551{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
158552 break;
158553 case 240: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
158554{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
158555 break;
158556 case 241: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
158557{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
158558 break;
158559 case 242: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
158560{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
158561 break;
158562 case 243: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
158563{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
158564 break;
158565 case 246: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
158566{
158567 Token all;
158568 all.z = yymsp[-3].minor.yy0.z;
158569 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
158570 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy447, &all);
158571}
158572 break;
158573 case 247: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
158574{
158575 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy192, yymsp[-4].minor.yy230.a, yymsp[-4].minor.yy230.b, yymsp[-2].minor.yy47, yymsp[0].minor.yy202, yymsp[-10].minor.yy192, yymsp[-8].minor.yy192);
158576 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
158577}
158578 break;
158579 case 248: /* trigger_time ::= BEFORE|AFTER */
158580{ yymsp[0].minor.yy192 = yymsp[0].major; /*A-overwrites-X*/ }
158581 break;
158582 case 249: /* trigger_time ::= INSTEAD OF */
158583{ yymsp[-1].minor.yy192 = TK_INSTEAD;}
158584 break;
158585 case 250: /* trigger_time ::= */
158586{ yymsp[1].minor.yy192 = TK_BEFORE; }
158587 break;
158588 case 251: /* trigger_event ::= DELETE|INSERT */
158589 case 252: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==252);
158590{yymsp[0].minor.yy230.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy230.b = 0;}
158591 break;
158592 case 253: /* trigger_event ::= UPDATE OF idlist */
158593{yymsp[-2].minor.yy230.a = TK_UPDATE; yymsp[-2].minor.yy230.b = yymsp[0].minor.yy600;}
158594 break;
158595 case 254: /* when_clause ::= */
158596 case 273: /* key_opt ::= */ yytestcase(yyruleno==273);
158597{ yymsp[1].minor.yy202 = 0; }
158598 break;
158599 case 255: /* when_clause ::= WHEN expr */
158600 case 274: /* key_opt ::= KEY expr */ yytestcase(yyruleno==274);
158601{ yymsp[-1].minor.yy202 = yymsp[0].minor.yy202; }
158602 break;
158603 case 256: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
158604{
158605 assert( yymsp[-2].minor.yy447!=0 );
158606 yymsp[-2].minor.yy447->pLast->pNext = yymsp[-1].minor.yy447;
158607 yymsp[-2].minor.yy447->pLast = yymsp[-1].minor.yy447;
158608}
158609 break;
158610 case 257: /* trigger_cmd_list ::= trigger_cmd SEMI */
158611{
158612 assert( yymsp[-1].minor.yy447!=0 );
158613 yymsp[-1].minor.yy447->pLast = yymsp[-1].minor.yy447;
158614}
158615 break;
158616 case 258: /* trnm ::= nm DOT nm */
158617{
158618 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
158619 sqlite3ErrorMsg(pParse,
158620 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
158621 "statements within triggers");
158622}
158623 break;
158624 case 259: /* tridxby ::= INDEXED BY nm */
158625{
158626 sqlite3ErrorMsg(pParse,
158627 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
158628 "within triggers");
158629}
158630 break;
158631 case 260: /* tridxby ::= NOT INDEXED */
158632{
158633 sqlite3ErrorMsg(pParse,
158634 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
158635 "within triggers");
158636}
158637 break;
158638 case 261: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
158639{yylhsminor.yy447 = sqlite3TriggerUpdateStep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy47, yymsp[-3].minor.yy242, yymsp[-1].minor.yy202, yymsp[-7].minor.yy192, yymsp[-8].minor.yy0.z, yymsp[0].minor.yy436);}
158640 yymsp[-8].minor.yy447 = yylhsminor.yy447;
158641 break;
158642 case 262: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
158643{
158644 yylhsminor.yy447 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy600,yymsp[-2].minor.yy539,yymsp[-6].minor.yy192,yymsp[-1].minor.yy318,yymsp[-7].minor.yy436,yymsp[0].minor.yy436);/*yylhsminor.yy447-overwrites-yymsp[-6].minor.yy192*/
158645}
158646 yymsp[-7].minor.yy447 = yylhsminor.yy447;
158647 break;
158648 case 263: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
158649{yylhsminor.yy447 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy202, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy436);}
158650 yymsp[-5].minor.yy447 = yylhsminor.yy447;
158651 break;
158652 case 264: /* trigger_cmd ::= scanpt select scanpt */
158653{yylhsminor.yy447 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy539, yymsp[-2].minor.yy436, yymsp[0].minor.yy436); /*yylhsminor.yy447-overwrites-yymsp[-1].minor.yy539*/}
158654 yymsp[-2].minor.yy447 = yylhsminor.yy447;
158655 break;
158656 case 265: /* expr ::= RAISE LP IGNORE RP */
158657{
158658 yymsp[-3].minor.yy202 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
158659 if( yymsp[-3].minor.yy202 ){
158660 yymsp[-3].minor.yy202->affExpr = OE_Ignore;
158661 }
158662}
158663 break;
158664 case 266: /* expr ::= RAISE LP raisetype COMMA nm RP */
158665{
158666 yymsp[-5].minor.yy202 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
158667 if( yymsp[-5].minor.yy202 ) {
158668 yymsp[-5].minor.yy202->affExpr = (char)yymsp[-3].minor.yy192;
158669 }
158670}
158671 break;
158672 case 267: /* raisetype ::= ROLLBACK */
158673{yymsp[0].minor.yy192 = OE_Rollback;}
158674 break;
158675 case 269: /* raisetype ::= FAIL */
158676{yymsp[0].minor.yy192 = OE_Fail;}
158677 break;
158678 case 270: /* cmd ::= DROP TRIGGER ifexists fullname */
158679{
158680 sqlite3DropTrigger(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy192);
158681}
158682 break;
158683 case 271: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
158684{
158685 sqlite3Attach(pParse, yymsp[-3].minor.yy202, yymsp[-1].minor.yy202, yymsp[0].minor.yy202);
158686}
158687 break;
158688 case 272: /* cmd ::= DETACH database_kw_opt expr */
158689{
158690 sqlite3Detach(pParse, yymsp[0].minor.yy202);
158691}
158692 break;
158693 case 275: /* cmd ::= REINDEX */
158694{sqlite3Reindex(pParse, 0, 0);}
158695 break;
158696 case 276: /* cmd ::= REINDEX nm dbnm */
158697{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
158698 break;
158699 case 277: /* cmd ::= ANALYZE */
158700{sqlite3Analyze(pParse, 0, 0);}
158701 break;
158702 case 278: /* cmd ::= ANALYZE nm dbnm */
158703{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
158704 break;
158705 case 279: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
158706{
158707 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy47,&yymsp[0].minor.yy0);
158708}
158709 break;
158710 case 280: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
158711{
158712 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
158713 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
158714}
158715 break;
158716 case 281: /* add_column_fullname ::= fullname */
158717{
158718 disableLookaside(pParse);
158719 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy47);
158720}
158721 break;
158722 case 282: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
158723{
158724 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy47, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
158725}
158726 break;
158727 case 283: /* cmd ::= create_vtab */
158728{sqlite3VtabFinishParse(pParse,0);}
158729 break;
158730 case 284: /* cmd ::= create_vtab LP vtabarglist RP */
158731{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
158732 break;
158733 case 285: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
158734{
158735 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy192);
158736}
158737 break;
158738 case 286: /* vtabarg ::= */
158739{sqlite3VtabArgInit(pParse);}
158740 break;
158741 case 287: /* vtabargtoken ::= ANY */
158742 case 288: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==288);
158743 case 289: /* lp ::= LP */ yytestcase(yyruleno==289);
158744{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
158745 break;
158746 case 290: /* with ::= WITH wqlist */
158747 case 291: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==291);
158748{ sqlite3WithPush(pParse, yymsp[0].minor.yy131, 1); }
158749 break;
158750 case 292: /* wqlist ::= nm eidlist_opt AS LP select RP */
158751{
158752 yymsp[-5].minor.yy131 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy242, yymsp[-1].minor.yy539); /*A-overwrites-X*/
158753}
158754 break;
158755 case 293: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
158756{
158757 yymsp[-7].minor.yy131 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy131, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy242, yymsp[-1].minor.yy539);
158758}
158759 break;
158760 case 294: /* windowdefn_list ::= windowdefn */
158761{ yylhsminor.yy303 = yymsp[0].minor.yy303; }
158762 yymsp[0].minor.yy303 = yylhsminor.yy303;
158763 break;
158764 case 295: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
158765{
158766 assert( yymsp[0].minor.yy303!=0 );
158767 sqlite3WindowChain(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy303);
158768 yymsp[0].minor.yy303->pNextWin = yymsp[-2].minor.yy303;
158769 yylhsminor.yy303 = yymsp[0].minor.yy303;
158770}
158771 yymsp[-2].minor.yy303 = yylhsminor.yy303;
158772 break;
158773 case 296: /* windowdefn ::= nm AS LP window RP */
158774{
158775 if( ALWAYS(yymsp[-1].minor.yy303) ){
158776 yymsp[-1].minor.yy303->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
158777 }
158778 yylhsminor.yy303 = yymsp[-1].minor.yy303;
158779}
158780 yymsp[-4].minor.yy303 = yylhsminor.yy303;
158781 break;
158782 case 297: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
158783{
158784 yymsp[-4].minor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy242, yymsp[-1].minor.yy242, 0);
158785}
158786 break;
158787 case 298: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
158788{
158789 yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, yymsp[-2].minor.yy242, yymsp[-1].minor.yy242, &yymsp[-5].minor.yy0);
158790}
158791 yymsp[-5].minor.yy303 = yylhsminor.yy303;
158792 break;
158793 case 299: /* window ::= ORDER BY sortlist frame_opt */
158794{
158795 yymsp[-3].minor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, yymsp[-1].minor.yy242, 0);
158796}
158797 break;
158798 case 300: /* window ::= nm ORDER BY sortlist frame_opt */
158799{
158800 yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, yymsp[-1].minor.yy242, &yymsp[-4].minor.yy0);
158801}
158802 yymsp[-4].minor.yy303 = yylhsminor.yy303;
158803 break;
158804 case 301: /* window ::= frame_opt */
158805 case 320: /* filter_over ::= over_clause */ yytestcase(yyruleno==320);
158806{
158807 yylhsminor.yy303 = yymsp[0].minor.yy303;
158808}
158809 yymsp[0].minor.yy303 = yylhsminor.yy303;
158810 break;
158811 case 302: /* window ::= nm frame_opt */
158812{
158813 yylhsminor.yy303 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy303, 0, 0, &yymsp[-1].minor.yy0);
158814}
158815 yymsp[-1].minor.yy303 = yylhsminor.yy303;
158816 break;
158817 case 303: /* frame_opt ::= */
158818{
158819 yymsp[1].minor.yy303 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
158820}
158821 break;
158822 case 304: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
158823{
158824 yylhsminor.yy303 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy192, yymsp[-1].minor.yy77.eType, yymsp[-1].minor.yy77.pExpr, TK_CURRENT, 0, yymsp[0].minor.yy58);
158825}
158826 yymsp[-2].minor.yy303 = yylhsminor.yy303;
158827 break;
158828 case 305: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
158829{
158830 yylhsminor.yy303 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy192, yymsp[-3].minor.yy77.eType, yymsp[-3].minor.yy77.pExpr, yymsp[-1].minor.yy77.eType, yymsp[-1].minor.yy77.pExpr, yymsp[0].minor.yy58);
158831}
158832 yymsp[-5].minor.yy303 = yylhsminor.yy303;
158833 break;
158834 case 307: /* frame_bound_s ::= frame_bound */
158835 case 309: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==309);
158836{yylhsminor.yy77 = yymsp[0].minor.yy77;}
158837 yymsp[0].minor.yy77 = yylhsminor.yy77;
158838 break;
158839 case 308: /* frame_bound_s ::= UNBOUNDED PRECEDING */
158840 case 310: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==310);
158841 case 312: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==312);
158842{yylhsminor.yy77.eType = yymsp[-1].major; yylhsminor.yy77.pExpr = 0;}
158843 yymsp[-1].minor.yy77 = yylhsminor.yy77;
158844 break;
158845 case 311: /* frame_bound ::= expr PRECEDING|FOLLOWING */
158846{yylhsminor.yy77.eType = yymsp[0].major; yylhsminor.yy77.pExpr = yymsp[-1].minor.yy202;}
158847 yymsp[-1].minor.yy77 = yylhsminor.yy77;
158848 break;
158849 case 313: /* frame_exclude_opt ::= */
158850{yymsp[1].minor.yy58 = 0;}
158851 break;
158852 case 314: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
158853{yymsp[-1].minor.yy58 = yymsp[0].minor.yy58;}
158854 break;
158855 case 315: /* frame_exclude ::= NO OTHERS */
158856 case 316: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==316);
158857{yymsp[-1].minor.yy58 = yymsp[-1].major; /*A-overwrites-X*/}
158858 break;
158859 case 317: /* frame_exclude ::= GROUP|TIES */
158860{yymsp[0].minor.yy58 = yymsp[0].major; /*A-overwrites-X*/}
158861 break;
158862 case 318: /* window_clause ::= WINDOW windowdefn_list */
158863{ yymsp[-1].minor.yy303 = yymsp[0].minor.yy303; }
158864 break;
158865 case 319: /* filter_over ::= filter_clause over_clause */
158866{
158867 yymsp[0].minor.yy303->pFilter = yymsp[-1].minor.yy202;
158868 yylhsminor.yy303 = yymsp[0].minor.yy303;
158869}
158870 yymsp[-1].minor.yy303 = yylhsminor.yy303;
158871 break;
158872 case 321: /* filter_over ::= filter_clause */
158873{
158874 yylhsminor.yy303 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
158875 if( yylhsminor.yy303 ){
158876 yylhsminor.yy303->eFrmType = TK_FILTER;
158877 yylhsminor.yy303->pFilter = yymsp[0].minor.yy202;
158878 }else{
158879 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy202);
158880 }
158881}
158882 yymsp[0].minor.yy303 = yylhsminor.yy303;
158883 break;
158884 case 322: /* over_clause ::= OVER LP window RP */
158885{
158886 yymsp[-3].minor.yy303 = yymsp[-1].minor.yy303;
158887 assert( yymsp[-3].minor.yy303!=0 );
158888}
158889 break;
158890 case 323: /* over_clause ::= OVER nm */
158891{
158892 yymsp[-1].minor.yy303 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
158893 if( yymsp[-1].minor.yy303 ){
158894 yymsp[-1].minor.yy303->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
158895 }
158896}
158897 break;
158898 case 324: /* filter_clause ::= FILTER LP WHERE expr RP */
158899{ yymsp[-4].minor.yy202 = yymsp[-1].minor.yy202; }
158900 break;
158901 default:
158902 /* (325) input ::= cmdlist */ yytestcase(yyruleno==325);
158903 /* (326) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==326);
158904 /* (327) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=327);
158905 /* (328) ecmd ::= SEMI */ yytestcase(yyruleno==328);
158906 /* (329) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==329);
158907 /* (330) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=330);
158908 /* (331) trans_opt ::= */ yytestcase(yyruleno==331);
158909 /* (332) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==332);
158910 /* (333) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==333);
158911 /* (334) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==334);
158912 /* (335) savepoint_opt ::= */ yytestcase(yyruleno==335);
158913 /* (336) cmd ::= create_table create_table_args */ yytestcase(yyruleno==336);
158914 /* (337) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==337);
158915 /* (338) columnlist ::= columnname carglist */ yytestcase(yyruleno==338);
158916 /* (339) nm ::= ID|INDEXED */ yytestcase(yyruleno==339);
158917 /* (340) nm ::= STRING */ yytestcase(yyruleno==340);
158918 /* (341) nm ::= JOIN_KW */ yytestcase(yyruleno==341);
158919 /* (342) typetoken ::= typename */ yytestcase(yyruleno==342);
158920 /* (343) typename ::= ID|STRING */ yytestcase(yyruleno==343);
158921 /* (344) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=344);
158922 /* (345) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=345);
158923 /* (346) carglist ::= carglist ccons */ yytestcase(yyruleno==346);
158924 /* (347) carglist ::= */ yytestcase(yyruleno==347);
158925 /* (348) ccons ::= NULL onconf */ yytestcase(yyruleno==348);
158926 /* (349) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==349);
158927 /* (350) ccons ::= AS generated */ yytestcase(yyruleno==350);
158928 /* (351) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==351);
158929 /* (352) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==352);
158930 /* (353) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=353);
158931 /* (354) tconscomma ::= */ yytestcase(yyruleno==354);
158932 /* (355) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=355);
158933 /* (356) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=356);
158934 /* (357) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=357);
158935 /* (358) oneselect ::= values */ yytestcase(yyruleno==358);
158936 /* (359) sclp ::= selcollist COMMA */ yytestcase(yyruleno==359);
158937 /* (360) as ::= ID|STRING */ yytestcase(yyruleno==360);
158938 /* (361) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=361);
158939 /* (362) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==362);
158940 /* (363) exprlist ::= nexprlist */ yytestcase(yyruleno==363);
158941 /* (364) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=364);
158942 /* (365) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=365);
158943 /* (366) nmnum ::= ON */ yytestcase(yyruleno==366);
158944 /* (367) nmnum ::= DELETE */ yytestcase(yyruleno==367);
158945 /* (368) nmnum ::= DEFAULT */ yytestcase(yyruleno==368);
158946 /* (369) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==369);
158947 /* (370) foreach_clause ::= */ yytestcase(yyruleno==370);
158948 /* (371) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==371);
158949 /* (372) trnm ::= nm */ yytestcase(yyruleno==372);
158950 /* (373) tridxby ::= */ yytestcase(yyruleno==373);
158951 /* (374) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==374);
158952 /* (375) database_kw_opt ::= */ yytestcase(yyruleno==375);
158953 /* (376) kwcolumn_opt ::= */ yytestcase(yyruleno==376);
158954 /* (377) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==377);
158955 /* (378) vtabarglist ::= vtabarg */ yytestcase(yyruleno==378);
158956 /* (379) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==379);
158957 /* (380) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==380);
158958 /* (381) anylist ::= */ yytestcase(yyruleno==381);
158959 /* (382) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==382);
158960 /* (383) anylist ::= anylist ANY */ yytestcase(yyruleno==383);
158961 /* (384) with ::= */ yytestcase(yyruleno==384);
158962 break;
158963/********** End reduce actions ************************************************/
158964 };
158965 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
158966 yygoto = yyRuleInfoLhs[yyruleno];
158967 yysize = yyRuleInfoNRhs[yyruleno];
158968 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
158969
158970 /* There are no SHIFTREDUCE actions on nonterminals because the table
158971 ** generator has simplified them to pure REDUCE actions. */
158972 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
158973
158974 /* It is not possible for a REDUCE to be followed by an error */
158975 assert( yyact!=YY_ERROR_ACTION );
158976
158977 yymsp += yysize+1;
158978 yypParser->yytos = yymsp;
158979 yymsp->stateno = (YYACTIONTYPE)yyact;
158980 yymsp->major = (YYCODETYPE)yygoto;
158981 yyTraceShift(yypParser, yyact, "... then shift");
158982 return yyact;
158983}
158984
158985/*
158986** The following code executes when the parse fails
158987*/
158988#ifndef YYNOERRORRECOVERY
158989static void yy_parse_failed(
158990 yyParser *yypParser /* The parser */
158991){
158992 sqlite3ParserARG_FETCH
158993 sqlite3ParserCTX_FETCH
158994#ifndef NDEBUG
158995 if( yyTraceFILE ){
158996 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
158997 }
158998#endif
158999 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
159000 /* Here code is inserted which will be executed whenever the
159001 ** parser fails */
159002/************ Begin %parse_failure code ***************************************/
159003/************ End %parse_failure code *****************************************/
159004 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
159005 sqlite3ParserCTX_STORE
159006}
159007#endif /* YYNOERRORRECOVERY */
159008
159009/*
159010** The following code executes when a syntax error first occurs.
159011*/
159012static void yy_syntax_error(
159013 yyParser *yypParser, /* The parser */
159014 int yymajor, /* The major type of the error token */
159015 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */
159016){
159017 sqlite3ParserARG_FETCH
159018 sqlite3ParserCTX_FETCH
159019#define TOKEN yyminor
159020/************ Begin %syntax_error code ****************************************/
159021
159022 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
159023 if( TOKEN.z[0] ){
159024 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
159025 }else{
159026 sqlite3ErrorMsg(pParse, "incomplete input");
159027 }
159028/************ End %syntax_error code ******************************************/
159029 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
159030 sqlite3ParserCTX_STORE
159031}
159032
159033/*
159034** The following is executed when the parser accepts
159035*/
159036static void yy_accept(
159037 yyParser *yypParser /* The parser */
159038){
159039 sqlite3ParserARG_FETCH
159040 sqlite3ParserCTX_FETCH
159041#ifndef NDEBUG
159042 if( yyTraceFILE ){
159043 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
159044 }
159045#endif
159046#ifndef YYNOERRORRECOVERY
159047 yypParser->yyerrcnt = -1;
159048#endif
159049 assert( yypParser->yytos==yypParser->yystack );
159050 /* Here code is inserted which will be executed whenever the
159051 ** parser accepts */
159052/*********** Begin %parse_accept code *****************************************/
159053/*********** End %parse_accept code *******************************************/
159054 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
159055 sqlite3ParserCTX_STORE
159056}
159057
159058/* The main parser program.
159059** The first argument is a pointer to a structure obtained from
159060** "sqlite3ParserAlloc" which describes the current state of the parser.
159061** The second argument is the major token number. The third is
159062** the minor token. The fourth optional argument is whatever the
159063** user wants (and specified in the grammar) and is available for
159064** use by the action routines.
159065**
159066** Inputs:
159067** <ul>
159068** <li> A pointer to the parser (an opaque structure.)
159069** <li> The major token number.
159070** <li> The minor token number.
159071** <li> An option argument of a grammar-specified type.
159072** </ul>
159073**
159074** Outputs:
159075** None.
159076*/
159077SQLITE_PRIVATE void sqlite3Parser(
159078 void *yyp, /* The parser */
159079 int yymajor, /* The major token code number */
159080 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
159081 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
159082){
159083 YYMINORTYPE yyminorunion;
159084 YYACTIONTYPE yyact; /* The parser action. */
159085#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
159086 int yyendofinput; /* True if we are at the end of input */
159087#endif
159088#ifdef YYERRORSYMBOL
159089 int yyerrorhit = 0; /* True if yymajor has invoked an error */
159090#endif
159091 yyParser *yypParser = (yyParser*)yyp; /* The parser */
159092 sqlite3ParserCTX_FETCH
159093 sqlite3ParserARG_STORE
159094
159095 assert( yypParser->yytos!=0 );
159096#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
159097 yyendofinput = (yymajor==0);
159098#endif
159099
159100 yyact = yypParser->yytos->stateno;
159101#ifndef NDEBUG
159102 if( yyTraceFILE ){
159103 if( yyact < YY_MIN_REDUCE ){
159104 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
159105 yyTracePrompt,yyTokenName[yymajor],yyact);
159106 }else{
159107 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
159108 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
159109 }
159110 }
159111#endif
159112
159113 do{
159114 assert( yyact==yypParser->yytos->stateno );
159115 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
159116 if( yyact >= YY_MIN_REDUCE ){
159117 yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
159118 yyminor sqlite3ParserCTX_PARAM);
159119 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
159120 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
159121#ifndef YYNOERRORRECOVERY
159122 yypParser->yyerrcnt--;
159123#endif
159124 break;
159125 }else if( yyact==YY_ACCEPT_ACTION ){
159126 yypParser->yytos--;
159127 yy_accept(yypParser);
159128 return;
159129 }else{
159130 assert( yyact == YY_ERROR_ACTION );
159131 yyminorunion.yy0 = yyminor;
159132#ifdef YYERRORSYMBOL
159133 int yymx;
159134#endif
159135#ifndef NDEBUG
159136 if( yyTraceFILE ){
159137 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
159138 }
159139#endif
159140#ifdef YYERRORSYMBOL
159141 /* A syntax error has occurred.
159142 ** The response to an error depends upon whether or not the
159143 ** grammar defines an error token "ERROR".
159144 **
159145 ** This is what we do if the grammar does define ERROR:
159146 **
159147 ** * Call the %syntax_error function.
159148 **
159149 ** * Begin popping the stack until we enter a state where
159150 ** it is legal to shift the error symbol, then shift
159151 ** the error symbol.
159152 **
159153 ** * Set the error count to three.
159154 **
159155 ** * Begin accepting and shifting new tokens. No new error
159156 ** processing will occur until three tokens have been
159157 ** shifted successfully.
159158 **
159159 */
159160 if( yypParser->yyerrcnt<0 ){
159161 yy_syntax_error(yypParser,yymajor,yyminor);
159162 }
159163 yymx = yypParser->yytos->major;
159164 if( yymx==YYERRORSYMBOL || yyerrorhit ){
159165#ifndef NDEBUG
159166 if( yyTraceFILE ){
159167 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
159168 yyTracePrompt,yyTokenName[yymajor]);
159169 }
159170#endif
159171 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
159172 yymajor = YYNOCODE;
159173 }else{
159174 while( yypParser->yytos >= yypParser->yystack
159175 && (yyact = yy_find_reduce_action(
159176 yypParser->yytos->stateno,
159177 YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
159178 ){
159179 yy_pop_parser_stack(yypParser);
159180 }
159181 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
159182 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
159183 yy_parse_failed(yypParser);
159184#ifndef YYNOERRORRECOVERY
159185 yypParser->yyerrcnt = -1;
159186#endif
159187 yymajor = YYNOCODE;
159188 }else if( yymx!=YYERRORSYMBOL ){
159189 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
159190 }
159191 }
159192 yypParser->yyerrcnt = 3;
159193 yyerrorhit = 1;
159194 if( yymajor==YYNOCODE ) break;
159195 yyact = yypParser->yytos->stateno;
159196#elif defined(YYNOERRORRECOVERY)
159197 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
159198 ** do any kind of error recovery. Instead, simply invoke the syntax
159199 ** error routine and continue going as if nothing had happened.
159200 **
159201 ** Applications can set this macro (for example inside %include) if
159202 ** they intend to abandon the parse upon the first syntax error seen.
159203 */
159204 yy_syntax_error(yypParser,yymajor, yyminor);
159205 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
159206 break;
159207#else /* YYERRORSYMBOL is not defined */
159208 /* This is what we do if the grammar does not define ERROR:
159209 **
159210 ** * Report an error message, and throw away the input token.
159211 **
159212 ** * If the input token is $, then fail the parse.
159213 **
159214 ** As before, subsequent error messages are suppressed until
159215 ** three input tokens have been successfully shifted.
159216 */
159217 if( yypParser->yyerrcnt<=0 ){
159218 yy_syntax_error(yypParser,yymajor, yyminor);
159219 }
159220 yypParser->yyerrcnt = 3;
159221 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
159222 if( yyendofinput ){
159223 yy_parse_failed(yypParser);
159224#ifndef YYNOERRORRECOVERY
159225 yypParser->yyerrcnt = -1;
159226#endif
159227 }
159228 break;
159229#endif
159230 }
159231 }while( yypParser->yytos>yypParser->yystack );
159232#ifndef NDEBUG
159233 if( yyTraceFILE ){
159234 yyStackEntry *i;
159235 char cDiv = '[';
159236 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
159237 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
159238 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
159239 cDiv = ' ';
159240 }
159241 fprintf(yyTraceFILE,"]\n");
159242 }
159243#endif
159244 return;
159245}
159246
159247/*
159248** Return the fallback token corresponding to canonical token iToken, or
159249** 0 if iToken has no fallback.
159250*/
159251SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
159252#ifdef YYFALLBACK
159253 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
159254 return yyFallback[iToken];
159255#else
159256 (void)iToken;
159257 return 0;
159258#endif
159259}
159260
159261/************** End of parse.c ***********************************************/
159262/************** Begin file tokenize.c ****************************************/
159263/*
159264** 2001 September 15
159265**
159266** The author disclaims copyright to this source code. In place of
159267** a legal notice, here is a blessing:
159268**
159269** May you do good and not evil.
159270** May you find forgiveness for yourself and forgive others.
159271** May you share freely, never taking more than you give.
159272**
159273*************************************************************************
159274** An tokenizer for SQL
159275**
159276** This file contains C code that splits an SQL input string up into
159277** individual tokens and sends those tokens one-by-one over to the
159278** parser for analysis.
159279*/
159280/* #include "sqliteInt.h" */
159281/* #include <stdlib.h> */
159282
159283/* Character classes for tokenizing
159284**
159285** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
159286** using a lookup table, whereas a switch() directly on c uses a binary search.
159287** The lookup table is much faster. To maximize speed, and to ensure that
159288** a lookup table is used, all of the classes need to be small integers and
159289** all of them need to be used within the switch.
159290*/
159291#define CC_X 0 /* The letter 'x', or start of BLOB literal */
159292#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
159293#define CC_ID 2 /* unicode characters usable in IDs */
159294#define CC_DIGIT 3 /* Digits */
159295#define CC_DOLLAR 4 /* '$' */
159296#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
159297#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
159298#define CC_SPACE 7 /* Space characters */
159299#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
159300#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
159301#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
159302#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
159303#define CC_LT 12 /* '<'. Part of < or <= or <> */
159304#define CC_GT 13 /* '>'. Part of > or >= */
159305#define CC_EQ 14 /* '='. Part of = or == */
159306#define CC_BANG 15 /* '!'. Part of != */
159307#define CC_SLASH 16 /* '/'. / or c-style comment */
159308#define CC_LP 17 /* '(' */
159309#define CC_RP 18 /* ')' */
159310#define CC_SEMI 19 /* ';' */
159311#define CC_PLUS 20 /* '+' */
159312#define CC_STAR 21 /* '*' */
159313#define CC_PERCENT 22 /* '%' */
159314#define CC_COMMA 23 /* ',' */
159315#define CC_AND 24 /* '&' */
159316#define CC_TILDA 25 /* '~' */
159317#define CC_DOT 26 /* '.' */
159318#define CC_ILLEGAL 27 /* Illegal character */
159319#define CC_NUL 28 /* 0x00 */
159320
159321static const unsigned char aiClass[] = {
159322#ifdef SQLITE_ASCII
159323/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
159324/* 0x */ 28, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
159325/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
159326/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
159327/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
159328/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
159329/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
159330/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
159331/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
159332/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159333/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159334/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159335/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159336/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159337/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159338/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159339/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
159340#endif
159341#ifdef SQLITE_EBCDIC
159342/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
159343/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
159344/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
159345/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
159346/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
159347/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
159348/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
159349/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
159350/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
159351/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
159352/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
159353/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
159354/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
159355/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
159356/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
159357/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
159358/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
159359#endif
159360};
159361
159362/*
159363** The charMap() macro maps alphabetic characters (only) into their
159364** lower-case ASCII equivalent. On ASCII machines, this is just
159365** an upper-to-lower case map. On EBCDIC machines we also need
159366** to adjust the encoding. The mapping is only valid for alphabetics
159367** which are the only characters for which this feature is used.
159368**
159369** Used by keywordhash.h
159370*/
159371#ifdef SQLITE_ASCII
159372# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
159373#endif
159374#ifdef SQLITE_EBCDIC
159375# define charMap(X) ebcdicToAscii[(unsigned char)X]
159376const unsigned char ebcdicToAscii[] = {
159377/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
159378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
159379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
159380 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
159381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
159382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
159383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
159384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
159385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
159386 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
159387 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
159388 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
159389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
159390 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
159391 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
159392 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
159393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
159394};
159395#endif
159396
159397/*
159398** The sqlite3KeywordCode function looks up an identifier to determine if
159399** it is a keyword. If it is a keyword, the token code of that keyword is
159400** returned. If the input is not a keyword, TK_ID is returned.
159401**
159402** The implementation of this routine was generated by a program,
159403** mkkeywordhash.c, located in the tool subdirectory of the distribution.
159404** The output of the mkkeywordhash.c program is written into a file
159405** named keywordhash.h and then included into this source file by
159406** the #include below.
159407*/
159408/************** Include keywordhash.h in the middle of tokenize.c ************/
159409/************** Begin file keywordhash.h *************************************/
159410/***** This file contains automatically generated code ******
159411**
159412** The code in this file has been automatically generated by
159413**
159414** sqlite/tool/mkkeywordhash.c
159415**
159416** The code in this file implements a function that determines whether
159417** or not a given identifier is really an SQL keyword. The same thing
159418** might be implemented more directly using a hand-written hash table.
159419** But by using this automatically generated code, the size of the code
159420** is substantially reduced. This is important for embedded applications
159421** on platforms with limited memory.
159422*/
159423/* Hash score: 227 */
159424/* zKWText[] encodes 984 bytes of keyword text in 648 bytes */
159425/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
159426/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
159427/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
159428/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
159429/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
159430/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
159431/* PRAGMABORTUPDATEVALUESVIRTUALWAYSWHENWHERECURSIVEAFTERENAMEAND */
159432/* EFERREDISTINCTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS */
159433/* CURRENT_TIMESTAMPARTITIONDROPRECEDINGFAILASTFILTEREPLACEFIRST */
159434/* FOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVERIGHTROLLBACKROWS */
159435/* UNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBYINITIALLYPRIMARY */
159436static const char zKWText[647] = {
159437 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
159438 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
159439 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
159440 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
159441 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
159442 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
159443 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
159444 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
159445 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
159446 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
159447 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
159448 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
159449 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
159450 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
159451 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
159452 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
159453 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
159454 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
159455 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
159456 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
159457 'A','B','O','R','T','U','P','D','A','T','E','V','A','L','U','E','S','V',
159458 'I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H','E','R',
159459 'E','C','U','R','S','I','V','E','A','F','T','E','R','E','N','A','M','E',
159460 'A','N','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','A',
159461 'U','T','O','I','N','C','R','E','M','E','N','T','C','A','S','T','C','O',
159462 'L','U','M','N','C','O','M','M','I','T','C','O','N','F','L','I','C','T',
159463 'C','R','O','S','S','C','U','R','R','E','N','T','_','T','I','M','E','S',
159464 'T','A','M','P','A','R','T','I','T','I','O','N','D','R','O','P','R','E',
159465 'C','E','D','I','N','G','F','A','I','L','A','S','T','F','I','L','T','E',
159466 'R','E','P','L','A','C','E','F','I','R','S','T','F','O','L','L','O','W',
159467 'I','N','G','F','R','O','M','F','U','L','L','I','M','I','T','I','F','O',
159468 'R','D','E','R','E','S','T','R','I','C','T','O','T','H','E','R','S','O',
159469 'V','E','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O','W',
159470 'S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S','I',
159471 'N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W','B',
159472 'Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
159473};
159474/* aKWHash[i] is the hash value for the i-th keyword */
159475static const unsigned char aKWHash[127] = {
159476 84, 102, 132, 82, 114, 29, 0, 0, 91, 0, 85, 72, 0,
159477 53, 35, 86, 15, 0, 42, 94, 54, 126, 133, 19, 0, 0,
159478 138, 0, 40, 128, 0, 22, 104, 0, 9, 0, 0, 122, 80,
159479 0, 78, 6, 0, 65, 99, 145, 0, 134, 112, 0, 0, 48,
159480 0, 100, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 140,
159481 107, 121, 0, 73, 101, 71, 143, 61, 119, 74, 0, 49, 0,
159482 11, 41, 0, 110, 0, 0, 0, 106, 10, 108, 113, 124, 14,
159483 50, 123, 0, 89, 0, 18, 120, 142, 56, 129, 137, 88, 83,
159484 37, 30, 125, 0, 0, 105, 51, 130, 127, 0, 34, 0, 0,
159485 44, 0, 95, 38, 39, 0, 20, 45, 116, 90,
159486};
159487/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
159488** then the i-th keyword has no more hash collisions. Otherwise,
159489** the next keyword with the same hash is aKWHash[i]-1. */
159490static const unsigned char aKWNext[145] = {
159491 0, 0, 0, 0, 4, 0, 43, 0, 0, 103, 111, 0, 0,
159492 0, 2, 0, 0, 141, 0, 0, 0, 13, 0, 0, 0, 0,
159493 139, 0, 0, 118, 52, 0, 0, 135, 12, 0, 0, 62, 0,
159494 136, 0, 131, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
159495 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159496 0, 69, 0, 0, 0, 0, 0, 144, 3, 0, 58, 0, 1,
159497 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 64, 66,
159498 63, 0, 0, 0, 0, 46, 0, 16, 0, 115, 0, 0, 0,
159499 0, 0, 0, 0, 0, 0, 0, 81, 97, 0, 8, 0, 109,
159500 21, 7, 67, 0, 79, 93, 117, 0, 0, 68, 0, 0, 96,
159501 0, 55, 0, 76, 0, 92, 32, 33, 57, 25, 0, 98, 0,
159502 0, 87,
159503};
159504/* aKWLen[i] is the length (in bytes) of the i-th keyword */
159505static const unsigned char aKWLen[145] = {
159506 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
159507 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
159508 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
159509 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
159510 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
159511 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
159512 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 5, 6, 6,
159513 7, 6, 4, 5, 9, 5, 6, 3, 8, 8, 2, 13, 2,
159514 2, 4, 6, 6, 8, 5, 17, 12, 7, 9, 4, 9, 4,
159515 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6, 4,
159516 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2, 2, 9,
159517 3, 7,
159518};
159519/* aKWOffset[i] is the index into zKWText[] of the start of
159520** the text for the i-th keyword. */
159521static const unsigned short int aKWOffset[145] = {
159522 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
159523 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
159524 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
159525 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
159526 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
159527 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
159528 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 360, 365, 371,
159529 377, 382, 388, 392, 395, 404, 408, 414, 416, 423, 424, 431, 433,
159530 435, 444, 448, 454, 460, 468, 473, 473, 473, 489, 498, 501, 510,
159531 513, 517, 522, 529, 534, 543, 547, 550, 555, 557, 561, 569, 575,
159532 578, 583, 591, 591, 595, 604, 609, 614, 620, 623, 626, 629, 631,
159533 636, 640,
159534};
159535/* aKWCode[i] is the parser symbol code for the i-th keyword */
159536static const unsigned char aKWCode[145] = {
159537 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
159538 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
159539 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
159540 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
159541 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
159542 TK_EXCLUDE, TK_DELETE, TK_TEMP, TK_TEMP, TK_OR,
159543 TK_ISNULL, TK_NULLS, TK_SAVEPOINT, TK_INTERSECT, TK_TIES,
159544 TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW,
159545 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
159546 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_CONSTRAINT,
159547 TK_INTO, TK_OFFSET, TK_OF, TK_SET, TK_TRIGGER,
159548 TK_RANGE, TK_GENERATED, TK_DETACH, TK_HAVING, TK_LIKE_KW,
159549 TK_BEGIN, TK_JOIN_KW, TK_REFERENCES, TK_UNIQUE, TK_QUERY,
159550 TK_WITHOUT, TK_WITH, TK_JOIN_KW, TK_RELEASE, TK_ATTACH,
159551 TK_BETWEEN, TK_NOTHING, TK_GROUPS, TK_GROUP, TK_CASCADE,
159552 TK_ASC, TK_DEFAULT, TK_CASE, TK_COLLATE, TK_CREATE,
159553 TK_CTIME_KW, TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_MATCH,
159554 TK_PLAN, TK_ANALYZE, TK_PRAGMA, TK_ABORT, TK_UPDATE,
159555 TK_VALUES, TK_VIRTUAL, TK_ALWAYS, TK_WHEN, TK_WHERE,
159556 TK_RECURSIVE, TK_AFTER, TK_RENAME, TK_AND, TK_DEFERRED,
159557 TK_DISTINCT, TK_IS, TK_AUTOINCR, TK_TO, TK_IN,
159558 TK_CAST, TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW,
159559 TK_CTIME_KW, TK_CTIME_KW, TK_CURRENT, TK_PARTITION, TK_DROP,
159560 TK_PRECEDING, TK_FAIL, TK_LAST, TK_FILTER, TK_REPLACE,
159561 TK_FIRST, TK_FOLLOWING, TK_FROM, TK_JOIN_KW, TK_LIMIT,
159562 TK_IF, TK_ORDER, TK_RESTRICT, TK_OTHERS, TK_OVER,
159563 TK_JOIN_KW, TK_ROLLBACK, TK_ROWS, TK_ROW, TK_UNBOUNDED,
159564 TK_UNION, TK_USING, TK_VACUUM, TK_VIEW, TK_WINDOW,
159565 TK_DO, TK_BY, TK_INITIALLY, TK_ALL, TK_PRIMARY,
159566};
159567/* Hash table decoded:
159568** 0: INSERT
159569** 1: IS
159570** 2: ROLLBACK TRIGGER
159571** 3: IMMEDIATE
159572** 4: PARTITION
159573** 5: TEMP
159574** 6:
159575** 7:
159576** 8: VALUES WITHOUT
159577** 9:
159578** 10: MATCH
159579** 11: NOTHING
159580** 12:
159581** 13: OF
159582** 14: TIES IGNORE
159583** 15: PLAN
159584** 16: INSTEAD INDEXED
159585** 17:
159586** 18: TRANSACTION RIGHT
159587** 19: WHEN
159588** 20: SET HAVING
159589** 21: IF
159590** 22: ROWS
159591** 23: SELECT
159592** 24:
159593** 25:
159594** 26: VACUUM SAVEPOINT
159595** 27:
159596** 28: LIKE UNION VIRTUAL REFERENCES
159597** 29: RESTRICT
159598** 30:
159599** 31: THEN REGEXP
159600** 32: TO
159601** 33:
159602** 34: BEFORE
159603** 35:
159604** 36:
159605** 37: FOLLOWING COLLATE CASCADE
159606** 38: CREATE
159607** 39:
159608** 40: CASE REINDEX
159609** 41: EACH
159610** 42:
159611** 43: QUERY
159612** 44: AND ADD
159613** 45: PRIMARY ANALYZE
159614** 46:
159615** 47: ROW ASC DETACH
159616** 48: CURRENT_TIME CURRENT_DATE
159617** 49:
159618** 50:
159619** 51: EXCLUSIVE TEMPORARY
159620** 52:
159621** 53: DEFERRED
159622** 54: DEFERRABLE
159623** 55:
159624** 56: DATABASE
159625** 57:
159626** 58: DELETE VIEW GENERATED
159627** 59: ATTACH
159628** 60: END
159629** 61: EXCLUDE
159630** 62: ESCAPE DESC
159631** 63: GLOB
159632** 64: WINDOW ELSE
159633** 65: COLUMN
159634** 66: FIRST
159635** 67:
159636** 68: GROUPS ALL
159637** 69: DISTINCT DROP KEY
159638** 70: BETWEEN
159639** 71: INITIALLY
159640** 72: BEGIN
159641** 73: FILTER CHECK ACTION
159642** 74: GROUP INDEX
159643** 75:
159644** 76: EXISTS DEFAULT
159645** 77:
159646** 78: FOR CURRENT_TIMESTAMP
159647** 79: EXCEPT
159648** 80:
159649** 81: CROSS
159650** 82:
159651** 83:
159652** 84:
159653** 85: CAST
159654** 86: FOREIGN AUTOINCREMENT
159655** 87: COMMIT
159656** 88: CURRENT AFTER ALTER
159657** 89: FULL FAIL CONFLICT
159658** 90: EXPLAIN
159659** 91: CONSTRAINT
159660** 92: FROM ALWAYS
159661** 93:
159662** 94: ABORT
159663** 95:
159664** 96: AS DO
159665** 97: REPLACE WITH RELEASE
159666** 98: BY RENAME
159667** 99: RANGE RAISE
159668** 100: OTHERS
159669** 101: USING NULLS
159670** 102: PRAGMA
159671** 103: JOIN ISNULL OFFSET
159672** 104: NOT
159673** 105: OR LAST LEFT
159674** 106: LIMIT
159675** 107:
159676** 108:
159677** 109: IN
159678** 110: INTO
159679** 111: OVER RECURSIVE
159680** 112: ORDER OUTER
159681** 113:
159682** 114: INTERSECT UNBOUNDED
159683** 115:
159684** 116:
159685** 117: ON
159686** 118:
159687** 119: WHERE
159688** 120: NO INNER
159689** 121: NULL
159690** 122:
159691** 123: TABLE
159692** 124: NATURAL NOTNULL
159693** 125: PRECEDING
159694** 126: UPDATE UNIQUE
159695*/
159696/* Check to see if z[0..n-1] is a keyword. If it is, write the
159697** parser symbol code for that keyword into *pType. Always
159698** return the integer n (the length of the token). */
159699static int keywordCode(const char *z, int n, int *pType){
159700 int i, j;
159701 const char *zKW;
159702 if( n>=2 ){
159703 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
159704 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
159705 if( aKWLen[i]!=n ) continue;
159706 zKW = &zKWText[aKWOffset[i]];
159707#ifdef SQLITE_ASCII
159708 if( (z[0]&~0x20)!=zKW[0] ) continue;
159709 if( (z[1]&~0x20)!=zKW[1] ) continue;
159710 j = 2;
159711 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
159712#endif
159713#ifdef SQLITE_EBCDIC
159714 if( toupper(z[0])!=zKW[0] ) continue;
159715 if( toupper(z[1])!=zKW[1] ) continue;
159716 j = 2;
159717 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
159718#endif
159719 if( j<n ) continue;
159720 testcase( i==0 ); /* REINDEX */
159721 testcase( i==1 ); /* INDEXED */
159722 testcase( i==2 ); /* INDEX */
159723 testcase( i==3 ); /* DESC */
159724 testcase( i==4 ); /* ESCAPE */
159725 testcase( i==5 ); /* EACH */
159726 testcase( i==6 ); /* CHECK */
159727 testcase( i==7 ); /* KEY */
159728 testcase( i==8 ); /* BEFORE */
159729 testcase( i==9 ); /* FOREIGN */
159730 testcase( i==10 ); /* FOR */
159731 testcase( i==11 ); /* IGNORE */
159732 testcase( i==12 ); /* REGEXP */
159733 testcase( i==13 ); /* EXPLAIN */
159734 testcase( i==14 ); /* INSTEAD */
159735 testcase( i==15 ); /* ADD */
159736 testcase( i==16 ); /* DATABASE */
159737 testcase( i==17 ); /* AS */
159738 testcase( i==18 ); /* SELECT */
159739 testcase( i==19 ); /* TABLE */
159740 testcase( i==20 ); /* LEFT */
159741 testcase( i==21 ); /* THEN */
159742 testcase( i==22 ); /* END */
159743 testcase( i==23 ); /* DEFERRABLE */
159744 testcase( i==24 ); /* ELSE */
159745 testcase( i==25 ); /* EXCLUDE */
159746 testcase( i==26 ); /* DELETE */
159747 testcase( i==27 ); /* TEMPORARY */
159748 testcase( i==28 ); /* TEMP */
159749 testcase( i==29 ); /* OR */
159750 testcase( i==30 ); /* ISNULL */
159751 testcase( i==31 ); /* NULLS */
159752 testcase( i==32 ); /* SAVEPOINT */
159753 testcase( i==33 ); /* INTERSECT */
159754 testcase( i==34 ); /* TIES */
159755 testcase( i==35 ); /* NOTNULL */
159756 testcase( i==36 ); /* NOT */
159757 testcase( i==37 ); /* NO */
159758 testcase( i==38 ); /* NULL */
159759 testcase( i==39 ); /* LIKE */
159760 testcase( i==40 ); /* EXCEPT */
159761 testcase( i==41 ); /* TRANSACTION */
159762 testcase( i==42 ); /* ACTION */
159763 testcase( i==43 ); /* ON */
159764 testcase( i==44 ); /* NATURAL */
159765 testcase( i==45 ); /* ALTER */
159766 testcase( i==46 ); /* RAISE */
159767 testcase( i==47 ); /* EXCLUSIVE */
159768 testcase( i==48 ); /* EXISTS */
159769 testcase( i==49 ); /* CONSTRAINT */
159770 testcase( i==50 ); /* INTO */
159771 testcase( i==51 ); /* OFFSET */
159772 testcase( i==52 ); /* OF */
159773 testcase( i==53 ); /* SET */
159774 testcase( i==54 ); /* TRIGGER */
159775 testcase( i==55 ); /* RANGE */
159776 testcase( i==56 ); /* GENERATED */
159777 testcase( i==57 ); /* DETACH */
159778 testcase( i==58 ); /* HAVING */
159779 testcase( i==59 ); /* GLOB */
159780 testcase( i==60 ); /* BEGIN */
159781 testcase( i==61 ); /* INNER */
159782 testcase( i==62 ); /* REFERENCES */
159783 testcase( i==63 ); /* UNIQUE */
159784 testcase( i==64 ); /* QUERY */
159785 testcase( i==65 ); /* WITHOUT */
159786 testcase( i==66 ); /* WITH */
159787 testcase( i==67 ); /* OUTER */
159788 testcase( i==68 ); /* RELEASE */
159789 testcase( i==69 ); /* ATTACH */
159790 testcase( i==70 ); /* BETWEEN */
159791 testcase( i==71 ); /* NOTHING */
159792 testcase( i==72 ); /* GROUPS */
159793 testcase( i==73 ); /* GROUP */
159794 testcase( i==74 ); /* CASCADE */
159795 testcase( i==75 ); /* ASC */
159796 testcase( i==76 ); /* DEFAULT */
159797 testcase( i==77 ); /* CASE */
159798 testcase( i==78 ); /* COLLATE */
159799 testcase( i==79 ); /* CREATE */
159800 testcase( i==80 ); /* CURRENT_DATE */
159801 testcase( i==81 ); /* IMMEDIATE */
159802 testcase( i==82 ); /* JOIN */
159803 testcase( i==83 ); /* INSERT */
159804 testcase( i==84 ); /* MATCH */
159805 testcase( i==85 ); /* PLAN */
159806 testcase( i==86 ); /* ANALYZE */
159807 testcase( i==87 ); /* PRAGMA */
159808 testcase( i==88 ); /* ABORT */
159809 testcase( i==89 ); /* UPDATE */
159810 testcase( i==90 ); /* VALUES */
159811 testcase( i==91 ); /* VIRTUAL */
159812 testcase( i==92 ); /* ALWAYS */
159813 testcase( i==93 ); /* WHEN */
159814 testcase( i==94 ); /* WHERE */
159815 testcase( i==95 ); /* RECURSIVE */
159816 testcase( i==96 ); /* AFTER */
159817 testcase( i==97 ); /* RENAME */
159818 testcase( i==98 ); /* AND */
159819 testcase( i==99 ); /* DEFERRED */
159820 testcase( i==100 ); /* DISTINCT */
159821 testcase( i==101 ); /* IS */
159822 testcase( i==102 ); /* AUTOINCREMENT */
159823 testcase( i==103 ); /* TO */
159824 testcase( i==104 ); /* IN */
159825 testcase( i==105 ); /* CAST */
159826 testcase( i==106 ); /* COLUMN */
159827 testcase( i==107 ); /* COMMIT */
159828 testcase( i==108 ); /* CONFLICT */
159829 testcase( i==109 ); /* CROSS */
159830 testcase( i==110 ); /* CURRENT_TIMESTAMP */
159831 testcase( i==111 ); /* CURRENT_TIME */
159832 testcase( i==112 ); /* CURRENT */
159833 testcase( i==113 ); /* PARTITION */
159834 testcase( i==114 ); /* DROP */
159835 testcase( i==115 ); /* PRECEDING */
159836 testcase( i==116 ); /* FAIL */
159837 testcase( i==117 ); /* LAST */
159838 testcase( i==118 ); /* FILTER */
159839 testcase( i==119 ); /* REPLACE */
159840 testcase( i==120 ); /* FIRST */
159841 testcase( i==121 ); /* FOLLOWING */
159842 testcase( i==122 ); /* FROM */
159843 testcase( i==123 ); /* FULL */
159844 testcase( i==124 ); /* LIMIT */
159845 testcase( i==125 ); /* IF */
159846 testcase( i==126 ); /* ORDER */
159847 testcase( i==127 ); /* RESTRICT */
159848 testcase( i==128 ); /* OTHERS */
159849 testcase( i==129 ); /* OVER */
159850 testcase( i==130 ); /* RIGHT */
159851 testcase( i==131 ); /* ROLLBACK */
159852 testcase( i==132 ); /* ROWS */
159853 testcase( i==133 ); /* ROW */
159854 testcase( i==134 ); /* UNBOUNDED */
159855 testcase( i==135 ); /* UNION */
159856 testcase( i==136 ); /* USING */
159857 testcase( i==137 ); /* VACUUM */
159858 testcase( i==138 ); /* VIEW */
159859 testcase( i==139 ); /* WINDOW */
159860 testcase( i==140 ); /* DO */
159861 testcase( i==141 ); /* BY */
159862 testcase( i==142 ); /* INITIALLY */
159863 testcase( i==143 ); /* ALL */
159864 testcase( i==144 ); /* PRIMARY */
159865 *pType = aKWCode[i];
159866 break;
159867 }
159868 }
159869 return n;
159870}
159871SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
159872 int id = TK_ID;
159873 keywordCode((char*)z, n, &id);
159874 return id;
159875}
159876#define SQLITE_N_KEYWORD 145
159877SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
159878 if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
159879 *pzName = zKWText + aKWOffset[i];
159880 *pnName = aKWLen[i];
159881 return SQLITE_OK;
159882}
159883SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
159884SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
159885 return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);
159886}
159887
159888/************** End of keywordhash.h *****************************************/
159889/************** Continuing where we left off in tokenize.c *******************/
159890
159891
159892/*
159893** If X is a character that can be used in an identifier then
159894** IdChar(X) will be true. Otherwise it is false.
159895**
159896** For ASCII, any character with the high-order bit set is
159897** allowed in an identifier. For 7-bit characters,
159898** sqlite3IsIdChar[X] must be 1.
159899**
159900** For EBCDIC, the rules are more complex but have the same
159901** end result.
159902**
159903** Ticket #1066. the SQL standard does not allow '$' in the
159904** middle of identifiers. But many SQL implementations do.
159905** SQLite will allow '$' in identifiers for compatibility.
159906** But the feature is undocumented.
159907*/
159908#ifdef SQLITE_ASCII
159909#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
159910#endif
159911#ifdef SQLITE_EBCDIC
159912SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
159913/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
159914 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
159915 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
159916 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
159917 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
159918 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
159919 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
159920 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
159921 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
159922 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
159923 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
159924 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
159925 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
159926};
159927#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
159928#endif
159929
159930/* Make the IdChar function accessible from ctime.c and alter.c */
159931SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
159932
159933#ifndef SQLITE_OMIT_WINDOWFUNC
159934/*
159935** Return the id of the next token in string (*pz). Before returning, set
159936** (*pz) to point to the byte following the parsed token.
159937*/
159938static int getToken(const unsigned char **pz){
159939 const unsigned char *z = *pz;
159940 int t; /* Token type to return */
159941 do {
159942 z += sqlite3GetToken(z, &t);
159943 }while( t==TK_SPACE );
159944 if( t==TK_ID
159945 || t==TK_STRING
159946 || t==TK_JOIN_KW
159947 || t==TK_WINDOW
159948 || t==TK_OVER
159949 || sqlite3ParserFallback(t)==TK_ID
159950 ){
159951 t = TK_ID;
159952 }
159953 *pz = z;
159954 return t;
159955}
159956
159957/*
159958** The following three functions are called immediately after the tokenizer
159959** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
159960** whether the token should be treated as a keyword or an SQL identifier.
159961** This cannot be handled by the usual lemon %fallback method, due to
159962** the ambiguity in some constructions. e.g.
159963**
159964** SELECT sum(x) OVER ...
159965**
159966** In the above, "OVER" might be a keyword, or it might be an alias for the
159967** sum(x) expression. If a "%fallback ID OVER" directive were added to
159968** grammar, then SQLite would always treat "OVER" as an alias, making it
159969** impossible to call a window-function without a FILTER clause.
159970**
159971** WINDOW is treated as a keyword if:
159972**
159973** * the following token is an identifier, or a keyword that can fallback
159974** to being an identifier, and
159975** * the token after than one is TK_AS.
159976**
159977** OVER is a keyword if:
159978**
159979** * the previous token was TK_RP, and
159980** * the next token is either TK_LP or an identifier.
159981**
159982** FILTER is a keyword if:
159983**
159984** * the previous token was TK_RP, and
159985** * the next token is TK_LP.
159986*/
159987static int analyzeWindowKeyword(const unsigned char *z){
159988 int t;
159989 t = getToken(&z);
159990 if( t!=TK_ID ) return TK_ID;
159991 t = getToken(&z);
159992 if( t!=TK_AS ) return TK_ID;
159993 return TK_WINDOW;
159994}
159995static int analyzeOverKeyword(const unsigned char *z, int lastToken){
159996 if( lastToken==TK_RP ){
159997 int t = getToken(&z);
159998 if( t==TK_LP || t==TK_ID ) return TK_OVER;
159999 }
160000 return TK_ID;
160001}
160002static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
160003 if( lastToken==TK_RP && getToken(&z)==TK_LP ){
160004 return TK_FILTER;
160005 }
160006 return TK_ID;
160007}
160008#endif /* SQLITE_OMIT_WINDOWFUNC */
160009
160010/*
160011** Return the length (in bytes) of the token that begins at z[0].
160012** Store the token type in *tokenType before returning.
160013*/
160014SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
160015 int i, c;
160016 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
160017 ** of the token. See the comment on the CC_ defines
160018 ** above. */
160019 case CC_SPACE: {
160020 testcase( z[0]==' ' );
160021 testcase( z[0]=='\t' );
160022 testcase( z[0]=='\n' );
160023 testcase( z[0]=='\f' );
160024 testcase( z[0]=='\r' );
160025 for(i=1; sqlite3Isspace(z[i]); i++){}
160026 *tokenType = TK_SPACE;
160027 return i;
160028 }
160029 case CC_MINUS: {
160030 if( z[1]=='-' ){
160031 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
160032 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
160033 return i;
160034 }
160035 *tokenType = TK_MINUS;
160036 return 1;
160037 }
160038 case CC_LP: {
160039 *tokenType = TK_LP;
160040 return 1;
160041 }
160042 case CC_RP: {
160043 *tokenType = TK_RP;
160044 return 1;
160045 }
160046 case CC_SEMI: {
160047 *tokenType = TK_SEMI;
160048 return 1;
160049 }
160050 case CC_PLUS: {
160051 *tokenType = TK_PLUS;
160052 return 1;
160053 }
160054 case CC_STAR: {
160055 *tokenType = TK_STAR;
160056 return 1;
160057 }
160058 case CC_SLASH: {
160059 if( z[1]!='*' || z[2]==0 ){
160060 *tokenType = TK_SLASH;
160061 return 1;
160062 }
160063 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
160064 if( c ) i++;
160065 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
160066 return i;
160067 }
160068 case CC_PERCENT: {
160069 *tokenType = TK_REM;
160070 return 1;
160071 }
160072 case CC_EQ: {
160073 *tokenType = TK_EQ;
160074 return 1 + (z[1]=='=');
160075 }
160076 case CC_LT: {
160077 if( (c=z[1])=='=' ){
160078 *tokenType = TK_LE;
160079 return 2;
160080 }else if( c=='>' ){
160081 *tokenType = TK_NE;
160082 return 2;
160083 }else if( c=='<' ){
160084 *tokenType = TK_LSHIFT;
160085 return 2;
160086 }else{
160087 *tokenType = TK_LT;
160088 return 1;
160089 }
160090 }
160091 case CC_GT: {
160092 if( (c=z[1])=='=' ){
160093 *tokenType = TK_GE;
160094 return 2;
160095 }else if( c=='>' ){
160096 *tokenType = TK_RSHIFT;
160097 return 2;
160098 }else{
160099 *tokenType = TK_GT;
160100 return 1;
160101 }
160102 }
160103 case CC_BANG: {
160104 if( z[1]!='=' ){
160105 *tokenType = TK_ILLEGAL;
160106 return 1;
160107 }else{
160108 *tokenType = TK_NE;
160109 return 2;
160110 }
160111 }
160112 case CC_PIPE: {
160113 if( z[1]!='|' ){
160114 *tokenType = TK_BITOR;
160115 return 1;
160116 }else{
160117 *tokenType = TK_CONCAT;
160118 return 2;
160119 }
160120 }
160121 case CC_COMMA: {
160122 *tokenType = TK_COMMA;
160123 return 1;
160124 }
160125 case CC_AND: {
160126 *tokenType = TK_BITAND;
160127 return 1;
160128 }
160129 case CC_TILDA: {
160130 *tokenType = TK_BITNOT;
160131 return 1;
160132 }
160133 case CC_QUOTE: {
160134 int delim = z[0];
160135 testcase( delim=='`' );
160136 testcase( delim=='\'' );
160137 testcase( delim=='"' );
160138 for(i=1; (c=z[i])!=0; i++){
160139 if( c==delim ){
160140 if( z[i+1]==delim ){
160141 i++;
160142 }else{
160143 break;
160144 }
160145 }
160146 }
160147 if( c=='\'' ){
160148 *tokenType = TK_STRING;
160149 return i+1;
160150 }else if( c!=0 ){
160151 *tokenType = TK_ID;
160152 return i+1;
160153 }else{
160154 *tokenType = TK_ILLEGAL;
160155 return i;
160156 }
160157 }
160158 case CC_DOT: {
160159#ifndef SQLITE_OMIT_FLOATING_POINT
160160 if( !sqlite3Isdigit(z[1]) )
160161#endif
160162 {
160163 *tokenType = TK_DOT;
160164 return 1;
160165 }
160166 /* If the next character is a digit, this is a floating point
160167 ** number that begins with ".". Fall thru into the next case */
160168 /* no break */ deliberate_fall_through
160169 }
160170 case CC_DIGIT: {
160171 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
160172 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
160173 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
160174 testcase( z[0]=='9' );
160175 *tokenType = TK_INTEGER;
160176#ifndef SQLITE_OMIT_HEX_INTEGER
160177 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
160178 for(i=3; sqlite3Isxdigit(z[i]); i++){}
160179 return i;
160180 }
160181#endif
160182 for(i=0; sqlite3Isdigit(z[i]); i++){}
160183#ifndef SQLITE_OMIT_FLOATING_POINT
160184 if( z[i]=='.' ){
160185 i++;
160186 while( sqlite3Isdigit(z[i]) ){ i++; }
160187 *tokenType = TK_FLOAT;
160188 }
160189 if( (z[i]=='e' || z[i]=='E') &&
160190 ( sqlite3Isdigit(z[i+1])
160191 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
160192 )
160193 ){
160194 i += 2;
160195 while( sqlite3Isdigit(z[i]) ){ i++; }
160196 *tokenType = TK_FLOAT;
160197 }
160198#endif
160199 while( IdChar(z[i]) ){
160200 *tokenType = TK_ILLEGAL;
160201 i++;
160202 }
160203 return i;
160204 }
160205 case CC_QUOTE2: {
160206 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
160207 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
160208 return i;
160209 }
160210 case CC_VARNUM: {
160211 *tokenType = TK_VARIABLE;
160212 for(i=1; sqlite3Isdigit(z[i]); i++){}
160213 return i;
160214 }
160215 case CC_DOLLAR:
160216 case CC_VARALPHA: {
160217 int n = 0;
160218 testcase( z[0]=='$' ); testcase( z[0]=='@' );
160219 testcase( z[0]==':' ); testcase( z[0]=='#' );
160220 *tokenType = TK_VARIABLE;
160221 for(i=1; (c=z[i])!=0; i++){
160222 if( IdChar(c) ){
160223 n++;
160224#ifndef SQLITE_OMIT_TCL_VARIABLE
160225 }else if( c=='(' && n>0 ){
160226 do{
160227 i++;
160228 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
160229 if( c==')' ){
160230 i++;
160231 }else{
160232 *tokenType = TK_ILLEGAL;
160233 }
160234 break;
160235 }else if( c==':' && z[i+1]==':' ){
160236 i++;
160237#endif
160238 }else{
160239 break;
160240 }
160241 }
160242 if( n==0 ) *tokenType = TK_ILLEGAL;
160243 return i;
160244 }
160245 case CC_KYWD: {
160246 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
160247 if( IdChar(z[i]) ){
160248 /* This token started out using characters that can appear in keywords,
160249 ** but z[i] is a character not allowed within keywords, so this must
160250 ** be an identifier instead */
160251 i++;
160252 break;
160253 }
160254 *tokenType = TK_ID;
160255 return keywordCode((char*)z, i, tokenType);
160256 }
160257 case CC_X: {
160258#ifndef SQLITE_OMIT_BLOB_LITERAL
160259 testcase( z[0]=='x' ); testcase( z[0]=='X' );
160260 if( z[1]=='\'' ){
160261 *tokenType = TK_BLOB;
160262 for(i=2; sqlite3Isxdigit(z[i]); i++){}
160263 if( z[i]!='\'' || i%2 ){
160264 *tokenType = TK_ILLEGAL;
160265 while( z[i] && z[i]!='\'' ){ i++; }
160266 }
160267 if( z[i] ) i++;
160268 return i;
160269 }
160270#endif
160271 /* If it is not a BLOB literal, then it must be an ID, since no
160272 ** SQL keywords start with the letter 'x'. Fall through */
160273 /* no break */ deliberate_fall_through
160274 }
160275 case CC_ID: {
160276 i = 1;
160277 break;
160278 }
160279 case CC_NUL: {
160280 *tokenType = TK_ILLEGAL;
160281 return 0;
160282 }
160283 default: {
160284 *tokenType = TK_ILLEGAL;
160285 return 1;
160286 }
160287 }
160288 while( IdChar(z[i]) ){ i++; }
160289 *tokenType = TK_ID;
160290 return i;
160291}
160292
160293/*
160294** Run the parser on the given SQL string. The parser structure is
160295** passed in. An SQLITE_ status code is returned. If an error occurs
160296** then an and attempt is made to write an error message into
160297** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
160298** error message.
160299*/
160300SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
160301 int nErr = 0; /* Number of errors encountered */
160302 void *pEngine; /* The LEMON-generated LALR(1) parser */
160303 int n = 0; /* Length of the next token token */
160304 int tokenType; /* type of the next token */
160305 int lastTokenParsed = -1; /* type of the previous token */
160306 sqlite3 *db = pParse->db; /* The database connection */
160307 int mxSqlLen; /* Max length of an SQL string */
160308#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
160309 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
160310#endif
160311 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
160312
160313 assert( zSql!=0 );
160314 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
160315 if( db->nVdbeActive==0 ){
160316 AtomicStore(&db->u1.isInterrupted, 0);
160317 }
160318 pParse->rc = SQLITE_OK;
160319 pParse->zTail = zSql;
160320 assert( pzErrMsg!=0 );
160321#ifdef SQLITE_DEBUG
160322 if( db->flags & SQLITE_ParserTrace ){
160323 printf("parser: [[[%s]]]\n", zSql);
160324 sqlite3ParserTrace(stdout, "parser: ");
160325 }else{
160326 sqlite3ParserTrace(0, 0);
160327 }
160328#endif
160329#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
160330 pEngine = &sEngine;
160331 sqlite3ParserInit(pEngine, pParse);
160332#else
160333 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
160334 if( pEngine==0 ){
160335 sqlite3OomFault(db);
160336 return SQLITE_NOMEM_BKPT;
160337 }
160338#endif
160339 assert( pParse->pNewTable==0 );
160340 assert( pParse->pNewTrigger==0 );
160341 assert( pParse->nVar==0 );
160342 assert( pParse->pVList==0 );
160343 pParse->pParentParse = db->pParse;
160344 db->pParse = pParse;
160345 while( 1 ){
160346 n = sqlite3GetToken((u8*)zSql, &tokenType);
160347 mxSqlLen -= n;
160348 if( mxSqlLen<0 ){
160349 pParse->rc = SQLITE_TOOBIG;
160350 break;
160351 }
160352#ifndef SQLITE_OMIT_WINDOWFUNC
160353 if( tokenType>=TK_WINDOW ){
160354 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
160355 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
160356 );
160357#else
160358 if( tokenType>=TK_SPACE ){
160359 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
160360#endif /* SQLITE_OMIT_WINDOWFUNC */
160361 if( AtomicLoad(&db->u1.isInterrupted) ){
160362 pParse->rc = SQLITE_INTERRUPT;
160363 break;
160364 }
160365 if( tokenType==TK_SPACE ){
160366 zSql += n;
160367 continue;
160368 }
160369 if( zSql[0]==0 ){
160370 /* Upon reaching the end of input, call the parser two more times
160371 ** with tokens TK_SEMI and 0, in that order. */
160372 if( lastTokenParsed==TK_SEMI ){
160373 tokenType = 0;
160374 }else if( lastTokenParsed==0 ){
160375 break;
160376 }else{
160377 tokenType = TK_SEMI;
160378 }
160379 n = 0;
160380#ifndef SQLITE_OMIT_WINDOWFUNC
160381 }else if( tokenType==TK_WINDOW ){
160382 assert( n==6 );
160383 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
160384 }else if( tokenType==TK_OVER ){
160385 assert( n==4 );
160386 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
160387 }else if( tokenType==TK_FILTER ){
160388 assert( n==6 );
160389 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
160390#endif /* SQLITE_OMIT_WINDOWFUNC */
160391 }else{
160392 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
160393 break;
160394 }
160395 }
160396 pParse->sLastToken.z = zSql;
160397 pParse->sLastToken.n = n;
160398 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
160399 lastTokenParsed = tokenType;
160400 zSql += n;
160401 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
160402 if( pParse->rc!=SQLITE_OK ) break;
160403 }
160404 assert( nErr==0 );
160405#ifdef YYTRACKMAXSTACKDEPTH
160406 sqlite3_mutex_enter(sqlite3MallocMutex());
160407 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
160408 sqlite3ParserStackPeak(pEngine)
160409 );
160410 sqlite3_mutex_leave(sqlite3MallocMutex());
160411#endif /* YYDEBUG */
160412#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
160413 sqlite3ParserFinalize(pEngine);
160414#else
160415 sqlite3ParserFree(pEngine, sqlite3_free);
160416#endif
160417 if( db->mallocFailed ){
160418 pParse->rc = SQLITE_NOMEM_BKPT;
160419 }
160420 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
160421 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
160422 }
160423 assert( pzErrMsg!=0 );
160424 if( pParse->zErrMsg ){
160425 *pzErrMsg = pParse->zErrMsg;
160426 sqlite3_log(pParse->rc, "%s in \"%s\"",
160427 *pzErrMsg, pParse->zTail);
160428 pParse->zErrMsg = 0;
160429 nErr++;
160430 }
160431 pParse->zTail = zSql;
160432 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
160433 sqlite3VdbeDelete(pParse->pVdbe);
160434 pParse->pVdbe = 0;
160435 }
160436#ifndef SQLITE_OMIT_SHARED_CACHE
160437 if( pParse->nested==0 ){
160438 sqlite3DbFree(db, pParse->aTableLock);
160439 pParse->aTableLock = 0;
160440 pParse->nTableLock = 0;
160441 }
160442#endif
160443#ifndef SQLITE_OMIT_VIRTUALTABLE
160444 sqlite3_free(pParse->apVtabLock);
160445#endif
160446
160447 if( !IN_SPECIAL_PARSE ){
160448 /* If the pParse->declareVtab flag is set, do not delete any table
160449 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
160450 ** will take responsibility for freeing the Table structure.
160451 */
160452 sqlite3DeleteTable(db, pParse->pNewTable);
160453 }
160454 if( !IN_RENAME_OBJECT ){
160455 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
160456 }
160457
160458 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
160459 sqlite3DbFree(db, pParse->pVList);
160460 while( pParse->pAinc ){
160461 AutoincInfo *p = pParse->pAinc;
160462 pParse->pAinc = p->pNext;
160463 sqlite3DbFreeNN(db, p);
160464 }
160465 while( pParse->pZombieTab ){
160466 Table *p = pParse->pZombieTab;
160467 pParse->pZombieTab = p->pNextZombie;
160468 sqlite3DeleteTable(db, p);
160469 }
160470 db->pParse = pParse->pParentParse;
160471 pParse->pParentParse = 0;
160472 assert( nErr==0 || pParse->rc!=SQLITE_OK );
160473 return nErr;
160474}
160475
160476
160477#ifdef SQLITE_ENABLE_NORMALIZE
160478/*
160479** Insert a single space character into pStr if the current string
160480** ends with an identifier
160481*/
160482static void addSpaceSeparator(sqlite3_str *pStr){
160483 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
160484 sqlite3_str_append(pStr, " ", 1);
160485 }
160486}
160487
160488/*
160489** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
160490** the normalization in space obtained from sqlite3DbMalloc(). Or return
160491** NULL if anything goes wrong or if zSql is NULL.
160492*/
160493SQLITE_PRIVATE char *sqlite3Normalize(
160494 Vdbe *pVdbe, /* VM being reprepared */
160495 const char *zSql /* The original SQL string */
160496){
160497 sqlite3 *db; /* The database connection */
160498 int i; /* Next unread byte of zSql[] */
160499 int n; /* length of current token */
160500 int tokenType; /* type of current token */
160501 int prevType = 0; /* Previous non-whitespace token */
160502 int nParen; /* Number of nested levels of parentheses */
160503 int iStartIN; /* Start of RHS of IN operator in z[] */
160504 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
160505 u32 j; /* Bytes of normalized SQL generated so far */
160506 sqlite3_str *pStr; /* The normalized SQL string under construction */
160507
160508 db = sqlite3VdbeDb(pVdbe);
160509 tokenType = -1;
160510 nParen = iStartIN = nParenAtIN = 0;
160511 pStr = sqlite3_str_new(db);
160512 assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */
160513 for(i=0; zSql[i] && pStr->accError==0; i+=n){
160514 if( tokenType!=TK_SPACE ){
160515 prevType = tokenType;
160516 }
160517 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
160518 if( NEVER(n<=0) ) break;
160519 switch( tokenType ){
160520 case TK_SPACE: {
160521 break;
160522 }
160523 case TK_NULL: {
160524 if( prevType==TK_IS || prevType==TK_NOT ){
160525 sqlite3_str_append(pStr, " NULL", 5);
160526 break;
160527 }
160528 /* Fall through */
160529 }
160530 case TK_STRING:
160531 case TK_INTEGER:
160532 case TK_FLOAT:
160533 case TK_VARIABLE:
160534 case TK_BLOB: {
160535 sqlite3_str_append(pStr, "?", 1);
160536 break;
160537 }
160538 case TK_LP: {
160539 nParen++;
160540 if( prevType==TK_IN ){
160541 iStartIN = pStr->nChar;
160542 nParenAtIN = nParen;
160543 }
160544 sqlite3_str_append(pStr, "(", 1);
160545 break;
160546 }
160547 case TK_RP: {
160548 if( iStartIN>0 && nParen==nParenAtIN ){
160549 assert( pStr->nChar>=(u32)iStartIN );
160550 pStr->nChar = iStartIN+1;
160551 sqlite3_str_append(pStr, "?,?,?", 5);
160552 iStartIN = 0;
160553 }
160554 nParen--;
160555 sqlite3_str_append(pStr, ")", 1);
160556 break;
160557 }
160558 case TK_ID: {
160559 iStartIN = 0;
160560 j = pStr->nChar;
160561 if( sqlite3Isquote(zSql[i]) ){
160562 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
160563 int nId;
160564 int eType = 0;
160565 if( zId==0 ) break;
160566 sqlite3Dequote(zId);
160567 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
160568 sqlite3_str_append(pStr, "?", 1);
160569 sqlite3DbFree(db, zId);
160570 break;
160571 }
160572 nId = sqlite3Strlen30(zId);
160573 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
160574 addSpaceSeparator(pStr);
160575 sqlite3_str_append(pStr, zId, nId);
160576 }else{
160577 sqlite3_str_appendf(pStr, "\"%w\"", zId);
160578 }
160579 sqlite3DbFree(db, zId);
160580 }else{
160581 addSpaceSeparator(pStr);
160582 sqlite3_str_append(pStr, zSql+i, n);
160583 }
160584 while( j<pStr->nChar ){
160585 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
160586 j++;
160587 }
160588 break;
160589 }
160590 case TK_SELECT: {
160591 iStartIN = 0;
160592 /* fall through */
160593 }
160594 default: {
160595 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
160596 j = pStr->nChar;
160597 sqlite3_str_append(pStr, zSql+i, n);
160598 while( j<pStr->nChar ){
160599 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
160600 j++;
160601 }
160602 break;
160603 }
160604 }
160605 }
160606 if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
160607 return sqlite3_str_finish(pStr);
160608}
160609#endif /* SQLITE_ENABLE_NORMALIZE */
160610
160611/************** End of tokenize.c ********************************************/
160612/************** Begin file complete.c ****************************************/
160613/*
160614** 2001 September 15
160615**
160616** The author disclaims copyright to this source code. In place of
160617** a legal notice, here is a blessing:
160618**
160619** May you do good and not evil.
160620** May you find forgiveness for yourself and forgive others.
160621** May you share freely, never taking more than you give.
160622**
160623*************************************************************************
160624** An tokenizer for SQL
160625**
160626** This file contains C code that implements the sqlite3_complete() API.
160627** This code used to be part of the tokenizer.c source file. But by
160628** separating it out, the code will be automatically omitted from
160629** static links that do not use it.
160630*/
160631/* #include "sqliteInt.h" */
160632#ifndef SQLITE_OMIT_COMPLETE
160633
160634/*
160635** This is defined in tokenize.c. We just have to import the definition.
160636*/
160637#ifndef SQLITE_AMALGAMATION
160638#ifdef SQLITE_ASCII
160639#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
160640#endif
160641#ifdef SQLITE_EBCDIC
160642SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
160643#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
160644#endif
160645#endif /* SQLITE_AMALGAMATION */
160646
160647
160648/*
160649** Token types used by the sqlite3_complete() routine. See the header
160650** comments on that procedure for additional information.
160651*/
160652#define tkSEMI 0
160653#define tkWS 1
160654#define tkOTHER 2
160655#ifndef SQLITE_OMIT_TRIGGER
160656#define tkEXPLAIN 3
160657#define tkCREATE 4
160658#define tkTEMP 5
160659#define tkTRIGGER 6
160660#define tkEND 7
160661#endif
160662
160663/*
160664** Return TRUE if the given SQL string ends in a semicolon.
160665**
160666** Special handling is require for CREATE TRIGGER statements.
160667** Whenever the CREATE TRIGGER keywords are seen, the statement
160668** must end with ";END;".
160669**
160670** This implementation uses a state machine with 8 states:
160671**
160672** (0) INVALID We have not yet seen a non-whitespace character.
160673**
160674** (1) START At the beginning or end of an SQL statement. This routine
160675** returns 1 if it ends in the START state and 0 if it ends
160676** in any other state.
160677**
160678** (2) NORMAL We are in the middle of statement which ends with a single
160679** semicolon.
160680**
160681** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
160682** a statement.
160683**
160684** (4) CREATE The keyword CREATE has been seen at the beginning of a
160685** statement, possibly preceded by EXPLAIN and/or followed by
160686** TEMP or TEMPORARY
160687**
160688** (5) TRIGGER We are in the middle of a trigger definition that must be
160689** ended by a semicolon, the keyword END, and another semicolon.
160690**
160691** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
160692** the end of a trigger definition.
160693**
160694** (7) END We've seen the ";END" of the ";END;" that occurs at the end
160695** of a trigger definition.
160696**
160697** Transitions between states above are determined by tokens extracted
160698** from the input. The following tokens are significant:
160699**
160700** (0) tkSEMI A semicolon.
160701** (1) tkWS Whitespace.
160702** (2) tkOTHER Any other SQL token.
160703** (3) tkEXPLAIN The "explain" keyword.
160704** (4) tkCREATE The "create" keyword.
160705** (5) tkTEMP The "temp" or "temporary" keyword.
160706** (6) tkTRIGGER The "trigger" keyword.
160707** (7) tkEND The "end" keyword.
160708**
160709** Whitespace never causes a state transition and is always ignored.
160710** This means that a SQL string of all whitespace is invalid.
160711**
160712** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
160713** to recognize the end of a trigger can be omitted. All we have to do
160714** is look for a semicolon that is not part of an string or comment.
160715*/
160716SQLITE_API int sqlite3_complete(const char *zSql){
160717 u8 state = 0; /* Current state, using numbers defined in header comment */
160718 u8 token; /* Value of the next token */
160719
160720#ifndef SQLITE_OMIT_TRIGGER
160721 /* A complex statement machine used to detect the end of a CREATE TRIGGER
160722 ** statement. This is the normal case.
160723 */
160724 static const u8 trans[8][8] = {
160725 /* Token: */
160726 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
160727 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
160728 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
160729 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
160730 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
160731 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
160732 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
160733 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
160734 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
160735 };
160736#else
160737 /* If triggers are not supported by this compile then the statement machine
160738 ** used to detect the end of a statement is much simpler
160739 */
160740 static const u8 trans[3][3] = {
160741 /* Token: */
160742 /* State: ** SEMI WS OTHER */
160743 /* 0 INVALID: */ { 1, 0, 2, },
160744 /* 1 START: */ { 1, 1, 2, },
160745 /* 2 NORMAL: */ { 1, 2, 2, },
160746 };
160747#endif /* SQLITE_OMIT_TRIGGER */
160748
160749#ifdef SQLITE_ENABLE_API_ARMOR
160750 if( zSql==0 ){
160751 (void)SQLITE_MISUSE_BKPT;
160752 return 0;
160753 }
160754#endif
160755
160756 while( *zSql ){
160757 switch( *zSql ){
160758 case ';': { /* A semicolon */
160759 token = tkSEMI;
160760 break;
160761 }
160762 case ' ':
160763 case '\r':
160764 case '\t':
160765 case '\n':
160766 case '\f': { /* White space is ignored */
160767 token = tkWS;
160768 break;
160769 }
160770 case '/': { /* C-style comments */
160771 if( zSql[1]!='*' ){
160772 token = tkOTHER;
160773 break;
160774 }
160775 zSql += 2;
160776 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
160777 if( zSql[0]==0 ) return 0;
160778 zSql++;
160779 token = tkWS;
160780 break;
160781 }
160782 case '-': { /* SQL-style comments from "--" to end of line */
160783 if( zSql[1]!='-' ){
160784 token = tkOTHER;
160785 break;
160786 }
160787 while( *zSql && *zSql!='\n' ){ zSql++; }
160788 if( *zSql==0 ) return state==1;
160789 token = tkWS;
160790 break;
160791 }
160792 case '[': { /* Microsoft-style identifiers in [...] */
160793 zSql++;
160794 while( *zSql && *zSql!=']' ){ zSql++; }
160795 if( *zSql==0 ) return 0;
160796 token = tkOTHER;
160797 break;
160798 }
160799 case '`': /* Grave-accent quoted symbols used by MySQL */
160800 case '"': /* single- and double-quoted strings */
160801 case '\'': {
160802 int c = *zSql;
160803 zSql++;
160804 while( *zSql && *zSql!=c ){ zSql++; }
160805 if( *zSql==0 ) return 0;
160806 token = tkOTHER;
160807 break;
160808 }
160809 default: {
160810#ifdef SQLITE_EBCDIC
160811 unsigned char c;
160812#endif
160813 if( IdChar((u8)*zSql) ){
160814 /* Keywords and unquoted identifiers */
160815 int nId;
160816 for(nId=1; IdChar(zSql[nId]); nId++){}
160817#ifdef SQLITE_OMIT_TRIGGER
160818 token = tkOTHER;
160819#else
160820 switch( *zSql ){
160821 case 'c': case 'C': {
160822 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
160823 token = tkCREATE;
160824 }else{
160825 token = tkOTHER;
160826 }
160827 break;
160828 }
160829 case 't': case 'T': {
160830 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
160831 token = tkTRIGGER;
160832 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
160833 token = tkTEMP;
160834 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
160835 token = tkTEMP;
160836 }else{
160837 token = tkOTHER;
160838 }
160839 break;
160840 }
160841 case 'e': case 'E': {
160842 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
160843 token = tkEND;
160844 }else
160845#ifndef SQLITE_OMIT_EXPLAIN
160846 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
160847 token = tkEXPLAIN;
160848 }else
160849#endif
160850 {
160851 token = tkOTHER;
160852 }
160853 break;
160854 }
160855 default: {
160856 token = tkOTHER;
160857 break;
160858 }
160859 }
160860#endif /* SQLITE_OMIT_TRIGGER */
160861 zSql += nId-1;
160862 }else{
160863 /* Operators and special symbols */
160864 token = tkOTHER;
160865 }
160866 break;
160867 }
160868 }
160869 state = trans[state][token];
160870 zSql++;
160871 }
160872 return state==1;
160873}
160874
160875#ifndef SQLITE_OMIT_UTF16
160876/*
160877** This routine is the same as the sqlite3_complete() routine described
160878** above, except that the parameter is required to be UTF-16 encoded, not
160879** UTF-8.
160880*/
160881SQLITE_API int sqlite3_complete16(const void *zSql){
160882 sqlite3_value *pVal;
160883 char const *zSql8;
160884 int rc;
160885
160886#ifndef SQLITE_OMIT_AUTOINIT
160887 rc = sqlite3_initialize();
160888 if( rc ) return rc;
160889#endif
160890 pVal = sqlite3ValueNew(0);
160891 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
160892 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
160893 if( zSql8 ){
160894 rc = sqlite3_complete(zSql8);
160895 }else{
160896 rc = SQLITE_NOMEM_BKPT;
160897 }
160898 sqlite3ValueFree(pVal);
160899 return rc & 0xff;
160900}
160901#endif /* SQLITE_OMIT_UTF16 */
160902#endif /* SQLITE_OMIT_COMPLETE */
160903
160904/************** End of complete.c ********************************************/
160905/************** Begin file main.c ********************************************/
160906/*
160907** 2001 September 15
160908**
160909** The author disclaims copyright to this source code. In place of
160910** a legal notice, here is a blessing:
160911**
160912** May you do good and not evil.
160913** May you find forgiveness for yourself and forgive others.
160914** May you share freely, never taking more than you give.
160915**
160916*************************************************************************
160917** Main file for the SQLite library. The routines in this file
160918** implement the programmer interface to the library. Routines in
160919** other files are for internal use by SQLite and should not be
160920** accessed by users of the library.
160921*/
160922/* #include "sqliteInt.h" */
160923
160924#ifdef SQLITE_ENABLE_FTS3
160925/************** Include fts3.h in the middle of main.c ***********************/
160926/************** Begin file fts3.h ********************************************/
160927/*
160928** 2006 Oct 10
160929**
160930** The author disclaims copyright to this source code. In place of
160931** a legal notice, here is a blessing:
160932**
160933** May you do good and not evil.
160934** May you find forgiveness for yourself and forgive others.
160935** May you share freely, never taking more than you give.
160936**
160937******************************************************************************
160938**
160939** This header file is used by programs that want to link against the
160940** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
160941*/
160942/* #include "sqlite3.h" */
160943
160944#if 0
160945extern "C" {
160946#endif /* __cplusplus */
160947
160948SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
160949
160950#if 0
160951} /* extern "C" */
160952#endif /* __cplusplus */
160953
160954/************** End of fts3.h ************************************************/
160955/************** Continuing where we left off in main.c ***********************/
160956#endif
160957#ifdef SQLITE_ENABLE_RTREE
160958/************** Include rtree.h in the middle of main.c **********************/
160959/************** Begin file rtree.h *******************************************/
160960/*
160961** 2008 May 26
160962**
160963** The author disclaims copyright to this source code. In place of
160964** a legal notice, here is a blessing:
160965**
160966** May you do good and not evil.
160967** May you find forgiveness for yourself and forgive others.
160968** May you share freely, never taking more than you give.
160969**
160970******************************************************************************
160971**
160972** This header file is used by programs that want to link against the
160973** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
160974*/
160975/* #include "sqlite3.h" */
160976
160977#ifdef SQLITE_OMIT_VIRTUALTABLE
160978# undef SQLITE_ENABLE_RTREE
160979#endif
160980
160981#if 0
160982extern "C" {
160983#endif /* __cplusplus */
160984
160985SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
160986
160987#if 0
160988} /* extern "C" */
160989#endif /* __cplusplus */
160990
160991/************** End of rtree.h ***********************************************/
160992/************** Continuing where we left off in main.c ***********************/
160993#endif
160994#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
160995/************** Include sqliteicu.h in the middle of main.c ******************/
160996/************** Begin file sqliteicu.h ***************************************/
160997/*
160998** 2008 May 26
160999**
161000** The author disclaims copyright to this source code. In place of
161001** a legal notice, here is a blessing:
161002**
161003** May you do good and not evil.
161004** May you find forgiveness for yourself and forgive others.
161005** May you share freely, never taking more than you give.
161006**
161007******************************************************************************
161008**
161009** This header file is used by programs that want to link against the
161010** ICU extension. All it does is declare the sqlite3IcuInit() interface.
161011*/
161012/* #include "sqlite3.h" */
161013
161014#if 0
161015extern "C" {
161016#endif /* __cplusplus */
161017
161018SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
161019
161020#if 0
161021} /* extern "C" */
161022#endif /* __cplusplus */
161023
161024/************** End of sqliteicu.h *******************************************/
161025/************** Continuing where we left off in main.c ***********************/
161026#endif
161027
161028/*
161029** This is an extension initializer that is a no-op and always
161030** succeeds, except that it fails if the fault-simulation is set
161031** to 500.
161032*/
161033static int sqlite3TestExtInit(sqlite3 *db){
161034 (void)db;
161035 return sqlite3FaultSim(500);
161036}
161037
161038
161039/*
161040** Forward declarations of external module initializer functions
161041** for modules that need them.
161042*/
161043#ifdef SQLITE_ENABLE_FTS1
161044SQLITE_PRIVATE int sqlite3Fts1Init(sqlite3*);
161045#endif
161046#ifdef SQLITE_ENABLE_FTS2
161047SQLITE_PRIVATE int sqlite3Fts2Init(sqlite3*);
161048#endif
161049#ifdef SQLITE_ENABLE_FTS5
161050SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
161051#endif
161052#ifdef SQLITE_ENABLE_JSON1
161053SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
161054#endif
161055#ifdef SQLITE_ENABLE_STMTVTAB
161056SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
161057#endif
161058
161059/*
161060** An array of pointers to extension initializer functions for
161061** built-in extensions.
161062*/
161063static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
161064#ifdef SQLITE_ENABLE_FTS1
161065 sqlite3Fts1Init,
161066#endif
161067#ifdef SQLITE_ENABLE_FTS2
161068 sqlite3Fts2Init,
161069#endif
161070#ifdef SQLITE_ENABLE_FTS3
161071 sqlite3Fts3Init,
161072#endif
161073#ifdef SQLITE_ENABLE_FTS5
161074 sqlite3Fts5Init,
161075#endif
161076#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
161077 sqlite3IcuInit,
161078#endif
161079#ifdef SQLITE_ENABLE_RTREE
161080 sqlite3RtreeInit,
161081#endif
161082#ifdef SQLITE_ENABLE_DBPAGE_VTAB
161083 sqlite3DbpageRegister,
161084#endif
161085#ifdef SQLITE_ENABLE_DBSTAT_VTAB
161086 sqlite3DbstatRegister,
161087#endif
161088 sqlite3TestExtInit,
161089#ifdef SQLITE_ENABLE_JSON1
161090 sqlite3Json1Init,
161091#endif
161092#ifdef SQLITE_ENABLE_STMTVTAB
161093 sqlite3StmtVtabInit,
161094#endif
161095#ifdef SQLITE_ENABLE_BYTECODE_VTAB
161096 sqlite3VdbeBytecodeVtabInit,
161097#endif
161098};
161099
161100#ifndef SQLITE_AMALGAMATION
161101/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
161102** contains the text of SQLITE_VERSION macro.
161103*/
161104SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
161105#endif
161106
161107/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
161108** a pointer to the to the sqlite3_version[] string constant.
161109*/
161110SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
161111
161112/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
161113** pointer to a string constant whose value is the same as the
161114** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
161115** an edited copy of the amalgamation, then the last four characters of
161116** the hash might be different from SQLITE_SOURCE_ID.
161117*/
161118/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
161119
161120/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
161121** returns an integer equal to SQLITE_VERSION_NUMBER.
161122*/
161123SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
161124
161125/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
161126** zero if and only if SQLite was compiled with mutexing code omitted due to
161127** the SQLITE_THREADSAFE compile-time option being set to 0.
161128*/
161129SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
161130
161131/*
161132** When compiling the test fixture or with debugging enabled (on Win32),
161133** this variable being set to non-zero will cause OSTRACE macros to emit
161134** extra diagnostic information.
161135*/
161136#ifdef SQLITE_HAVE_OS_TRACE
161137# ifndef SQLITE_DEBUG_OS_TRACE
161138# define SQLITE_DEBUG_OS_TRACE 0
161139# endif
161140 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
161141#endif
161142
161143#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
161144/*
161145** If the following function pointer is not NULL and if
161146** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
161147** I/O active are written using this function. These messages
161148** are intended for debugging activity only.
161149*/
161150SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
161151#endif
161152
161153/*
161154** If the following global variable points to a string which is the
161155** name of a directory, then that directory will be used to store
161156** temporary files.
161157**
161158** See also the "PRAGMA temp_store_directory" SQL command.
161159*/
161160SQLITE_API char *sqlite3_temp_directory = 0;
161161
161162/*
161163** If the following global variable points to a string which is the
161164** name of a directory, then that directory will be used to store
161165** all database files specified with a relative pathname.
161166**
161167** See also the "PRAGMA data_store_directory" SQL command.
161168*/
161169SQLITE_API char *sqlite3_data_directory = 0;
161170
161171/*
161172** Initialize SQLite.
161173**
161174** This routine must be called to initialize the memory allocation,
161175** VFS, and mutex subsystems prior to doing any serious work with
161176** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
161177** this routine will be called automatically by key routines such as
161178** sqlite3_open().
161179**
161180** This routine is a no-op except on its very first call for the process,
161181** or for the first call after a call to sqlite3_shutdown.
161182**
161183** The first thread to call this routine runs the initialization to
161184** completion. If subsequent threads call this routine before the first
161185** thread has finished the initialization process, then the subsequent
161186** threads must block until the first thread finishes with the initialization.
161187**
161188** The first thread might call this routine recursively. Recursive
161189** calls to this routine should not block, of course. Otherwise the
161190** initialization process would never complete.
161191**
161192** Let X be the first thread to enter this routine. Let Y be some other
161193** thread. Then while the initial invocation of this routine by X is
161194** incomplete, it is required that:
161195**
161196** * Calls to this routine from Y must block until the outer-most
161197** call by X completes.
161198**
161199** * Recursive calls to this routine from thread X return immediately
161200** without blocking.
161201*/
161202SQLITE_API int sqlite3_initialize(void){
161203 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
161204 int rc; /* Result code */
161205#ifdef SQLITE_EXTRA_INIT
161206 int bRunExtraInit = 0; /* Extra initialization needed */
161207#endif
161208
161209#ifdef SQLITE_OMIT_WSD
161210 rc = sqlite3_wsd_init(4096, 24);
161211 if( rc!=SQLITE_OK ){
161212 return rc;
161213 }
161214#endif
161215
161216 /* If the following assert() fails on some obscure processor/compiler
161217 ** combination, the work-around is to set the correct pointer
161218 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
161219 assert( SQLITE_PTRSIZE==sizeof(char*) );
161220
161221 /* If SQLite is already completely initialized, then this call
161222 ** to sqlite3_initialize() should be a no-op. But the initialization
161223 ** must be complete. So isInit must not be set until the very end
161224 ** of this routine.
161225 */
161226 if( sqlite3GlobalConfig.isInit ){
161227 sqlite3MemoryBarrier();
161228 return SQLITE_OK;
161229 }
161230
161231 /* Make sure the mutex subsystem is initialized. If unable to
161232 ** initialize the mutex subsystem, return early with the error.
161233 ** If the system is so sick that we are unable to allocate a mutex,
161234 ** there is not much SQLite is going to be able to do.
161235 **
161236 ** The mutex subsystem must take care of serializing its own
161237 ** initialization.
161238 */
161239 rc = sqlite3MutexInit();
161240 if( rc ) return rc;
161241
161242 /* Initialize the malloc() system and the recursive pInitMutex mutex.
161243 ** This operation is protected by the STATIC_MAIN mutex. Note that
161244 ** MutexAlloc() is called for a static mutex prior to initializing the
161245 ** malloc subsystem - this implies that the allocation of a static
161246 ** mutex must not require support from the malloc subsystem.
161247 */
161248 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
161249 sqlite3_mutex_enter(pMainMtx);
161250 sqlite3GlobalConfig.isMutexInit = 1;
161251 if( !sqlite3GlobalConfig.isMallocInit ){
161252 rc = sqlite3MallocInit();
161253 }
161254 if( rc==SQLITE_OK ){
161255 sqlite3GlobalConfig.isMallocInit = 1;
161256 if( !sqlite3GlobalConfig.pInitMutex ){
161257 sqlite3GlobalConfig.pInitMutex =
161258 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
161259 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
161260 rc = SQLITE_NOMEM_BKPT;
161261 }
161262 }
161263 }
161264 if( rc==SQLITE_OK ){
161265 sqlite3GlobalConfig.nRefInitMutex++;
161266 }
161267 sqlite3_mutex_leave(pMainMtx);
161268
161269 /* If rc is not SQLITE_OK at this point, then either the malloc
161270 ** subsystem could not be initialized or the system failed to allocate
161271 ** the pInitMutex mutex. Return an error in either case. */
161272 if( rc!=SQLITE_OK ){
161273 return rc;
161274 }
161275
161276 /* Do the rest of the initialization under the recursive mutex so
161277 ** that we will be able to handle recursive calls into
161278 ** sqlite3_initialize(). The recursive calls normally come through
161279 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
161280 ** recursive calls might also be possible.
161281 **
161282 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
161283 ** to the xInit method, so the xInit method need not be threadsafe.
161284 **
161285 ** The following mutex is what serializes access to the appdef pcache xInit
161286 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
161287 ** call to sqlite3PcacheInitialize().
161288 */
161289 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
161290 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
161291 sqlite3GlobalConfig.inProgress = 1;
161292#ifdef SQLITE_ENABLE_SQLLOG
161293 {
161294 extern void sqlite3_init_sqllog(void);
161295 sqlite3_init_sqllog();
161296 }
161297#endif
161298 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
161299 sqlite3RegisterBuiltinFunctions();
161300 if( sqlite3GlobalConfig.isPCacheInit==0 ){
161301 rc = sqlite3PcacheInitialize();
161302 }
161303 if( rc==SQLITE_OK ){
161304 sqlite3GlobalConfig.isPCacheInit = 1;
161305 rc = sqlite3OsInit();
161306 }
161307#ifdef SQLITE_ENABLE_DESERIALIZE
161308 if( rc==SQLITE_OK ){
161309 rc = sqlite3MemdbInit();
161310 }
161311#endif
161312 if( rc==SQLITE_OK ){
161313 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
161314 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
161315 sqlite3MemoryBarrier();
161316 sqlite3GlobalConfig.isInit = 1;
161317#ifdef SQLITE_EXTRA_INIT
161318 bRunExtraInit = 1;
161319#endif
161320 }
161321 sqlite3GlobalConfig.inProgress = 0;
161322 }
161323 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
161324
161325 /* Go back under the static mutex and clean up the recursive
161326 ** mutex to prevent a resource leak.
161327 */
161328 sqlite3_mutex_enter(pMainMtx);
161329 sqlite3GlobalConfig.nRefInitMutex--;
161330 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
161331 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
161332 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
161333 sqlite3GlobalConfig.pInitMutex = 0;
161334 }
161335 sqlite3_mutex_leave(pMainMtx);
161336
161337 /* The following is just a sanity check to make sure SQLite has
161338 ** been compiled correctly. It is important to run this code, but
161339 ** we don't want to run it too often and soak up CPU cycles for no
161340 ** reason. So we run it once during initialization.
161341 */
161342#ifndef NDEBUG
161343#ifndef SQLITE_OMIT_FLOATING_POINT
161344 /* This section of code's only "output" is via assert() statements. */
161345 if( rc==SQLITE_OK ){
161346 u64 x = (((u64)1)<<63)-1;
161347 double y;
161348 assert(sizeof(x)==8);
161349 assert(sizeof(x)==sizeof(y));
161350 memcpy(&y, &x, 8);
161351 assert( sqlite3IsNaN(y) );
161352 }
161353#endif
161354#endif
161355
161356 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
161357 ** compile-time option.
161358 */
161359#ifdef SQLITE_EXTRA_INIT
161360 if( bRunExtraInit ){
161361 int SQLITE_EXTRA_INIT(const char*);
161362 rc = SQLITE_EXTRA_INIT(0);
161363 }
161364#endif
161365
161366 return rc;
161367}
161368
161369/*
161370** Undo the effects of sqlite3_initialize(). Must not be called while
161371** there are outstanding database connections or memory allocations or
161372** while any part of SQLite is otherwise in use in any thread. This
161373** routine is not threadsafe. But it is safe to invoke this routine
161374** on when SQLite is already shut down. If SQLite is already shut down
161375** when this routine is invoked, then this routine is a harmless no-op.
161376*/
161377SQLITE_API int sqlite3_shutdown(void){
161378#ifdef SQLITE_OMIT_WSD
161379 int rc = sqlite3_wsd_init(4096, 24);
161380 if( rc!=SQLITE_OK ){
161381 return rc;
161382 }
161383#endif
161384
161385 if( sqlite3GlobalConfig.isInit ){
161386#ifdef SQLITE_EXTRA_SHUTDOWN
161387 void SQLITE_EXTRA_SHUTDOWN(void);
161388 SQLITE_EXTRA_SHUTDOWN();
161389#endif
161390 sqlite3_os_end();
161391 sqlite3_reset_auto_extension();
161392 sqlite3GlobalConfig.isInit = 0;
161393 }
161394 if( sqlite3GlobalConfig.isPCacheInit ){
161395 sqlite3PcacheShutdown();
161396 sqlite3GlobalConfig.isPCacheInit = 0;
161397 }
161398 if( sqlite3GlobalConfig.isMallocInit ){
161399 sqlite3MallocEnd();
161400 sqlite3GlobalConfig.isMallocInit = 0;
161401
161402#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
161403 /* The heap subsystem has now been shutdown and these values are supposed
161404 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
161405 ** which would rely on that heap subsystem; therefore, make sure these
161406 ** values cannot refer to heap memory that was just invalidated when the
161407 ** heap subsystem was shutdown. This is only done if the current call to
161408 ** this function resulted in the heap subsystem actually being shutdown.
161409 */
161410 sqlite3_data_directory = 0;
161411 sqlite3_temp_directory = 0;
161412#endif
161413 }
161414 if( sqlite3GlobalConfig.isMutexInit ){
161415 sqlite3MutexEnd();
161416 sqlite3GlobalConfig.isMutexInit = 0;
161417 }
161418
161419 return SQLITE_OK;
161420}
161421
161422/*
161423** This API allows applications to modify the global configuration of
161424** the SQLite library at run-time.
161425**
161426** This routine should only be called when there are no outstanding
161427** database connections or memory allocations. This routine is not
161428** threadsafe. Failure to heed these warnings can lead to unpredictable
161429** behavior.
161430*/
161431SQLITE_API int sqlite3_config(int op, ...){
161432 va_list ap;
161433 int rc = SQLITE_OK;
161434
161435 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
161436 ** the SQLite library is in use. */
161437 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
161438
161439 va_start(ap, op);
161440 switch( op ){
161441
161442 /* Mutex configuration options are only available in a threadsafe
161443 ** compile.
161444 */
161445#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
161446 case SQLITE_CONFIG_SINGLETHREAD: {
161447 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
161448 ** Single-thread. */
161449 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
161450 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
161451 break;
161452 }
161453#endif
161454#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
161455 case SQLITE_CONFIG_MULTITHREAD: {
161456 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
161457 ** Multi-thread. */
161458 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
161459 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
161460 break;
161461 }
161462#endif
161463#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
161464 case SQLITE_CONFIG_SERIALIZED: {
161465 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
161466 ** Serialized. */
161467 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
161468 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
161469 break;
161470 }
161471#endif
161472#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
161473 case SQLITE_CONFIG_MUTEX: {
161474 /* Specify an alternative mutex implementation */
161475 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
161476 break;
161477 }
161478#endif
161479#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
161480 case SQLITE_CONFIG_GETMUTEX: {
161481 /* Retrieve the current mutex implementation */
161482 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
161483 break;
161484 }
161485#endif
161486
161487 case SQLITE_CONFIG_MALLOC: {
161488 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
161489 ** single argument which is a pointer to an instance of the
161490 ** sqlite3_mem_methods structure. The argument specifies alternative
161491 ** low-level memory allocation routines to be used in place of the memory
161492 ** allocation routines built into SQLite. */
161493 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
161494 break;
161495 }
161496 case SQLITE_CONFIG_GETMALLOC: {
161497 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
161498 ** single argument which is a pointer to an instance of the
161499 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
161500 ** filled with the currently defined memory allocation routines. */
161501 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
161502 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
161503 break;
161504 }
161505 case SQLITE_CONFIG_MEMSTATUS: {
161506 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
161507 ** single argument of type int, interpreted as a boolean, which enables
161508 ** or disables the collection of memory allocation statistics. */
161509 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
161510 break;
161511 }
161512 case SQLITE_CONFIG_SMALL_MALLOC: {
161513 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
161514 break;
161515 }
161516 case SQLITE_CONFIG_PAGECACHE: {
161517 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
161518 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
161519 ** the size of each page cache line (sz), and the number of cache lines
161520 ** (N). */
161521 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
161522 sqlite3GlobalConfig.szPage = va_arg(ap, int);
161523 sqlite3GlobalConfig.nPage = va_arg(ap, int);
161524 break;
161525 }
161526 case SQLITE_CONFIG_PCACHE_HDRSZ: {
161527 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
161528 ** a single parameter which is a pointer to an integer and writes into
161529 ** that integer the number of extra bytes per page required for each page
161530 ** in SQLITE_CONFIG_PAGECACHE. */
161531 *va_arg(ap, int*) =
161532 sqlite3HeaderSizeBtree() +
161533 sqlite3HeaderSizePcache() +
161534 sqlite3HeaderSizePcache1();
161535 break;
161536 }
161537
161538 case SQLITE_CONFIG_PCACHE: {
161539 /* no-op */
161540 break;
161541 }
161542 case SQLITE_CONFIG_GETPCACHE: {
161543 /* now an error */
161544 rc = SQLITE_ERROR;
161545 break;
161546 }
161547
161548 case SQLITE_CONFIG_PCACHE2: {
161549 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
161550 ** single argument which is a pointer to an sqlite3_pcache_methods2
161551 ** object. This object specifies the interface to a custom page cache
161552 ** implementation. */
161553 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
161554 break;
161555 }
161556 case SQLITE_CONFIG_GETPCACHE2: {
161557 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
161558 ** single argument which is a pointer to an sqlite3_pcache_methods2
161559 ** object. SQLite copies of the current page cache implementation into
161560 ** that object. */
161561 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
161562 sqlite3PCacheSetDefault();
161563 }
161564 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
161565 break;
161566 }
161567
161568/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
161569** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
161570** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
161571#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
161572 case SQLITE_CONFIG_HEAP: {
161573 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
161574 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
161575 ** number of bytes in the memory buffer, and the minimum allocation size.
161576 */
161577 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
161578 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
161579 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
161580
161581 if( sqlite3GlobalConfig.mnReq<1 ){
161582 sqlite3GlobalConfig.mnReq = 1;
161583 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
161584 /* cap min request size at 2^12 */
161585 sqlite3GlobalConfig.mnReq = (1<<12);
161586 }
161587
161588 if( sqlite3GlobalConfig.pHeap==0 ){
161589 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
161590 ** is NULL, then SQLite reverts to using its default memory allocator
161591 ** (the system malloc() implementation), undoing any prior invocation of
161592 ** SQLITE_CONFIG_MALLOC.
161593 **
161594 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
161595 ** revert to its default implementation when sqlite3_initialize() is run
161596 */
161597 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
161598 }else{
161599 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
161600 ** alternative memory allocator is engaged to handle all of SQLites
161601 ** memory allocation needs. */
161602#ifdef SQLITE_ENABLE_MEMSYS3
161603 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
161604#endif
161605#ifdef SQLITE_ENABLE_MEMSYS5
161606 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
161607#endif
161608 }
161609 break;
161610 }
161611#endif
161612
161613 case SQLITE_CONFIG_LOOKASIDE: {
161614 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
161615 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
161616 break;
161617 }
161618
161619 /* Record a pointer to the logger function and its first argument.
161620 ** The default is NULL. Logging is disabled if the function pointer is
161621 ** NULL.
161622 */
161623 case SQLITE_CONFIG_LOG: {
161624 /* MSVC is picky about pulling func ptrs from va lists.
161625 ** http://support.microsoft.com/kb/47961
161626 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
161627 */
161628 typedef void(*LOGFUNC_t)(void*,int,const char*);
161629 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
161630 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
161631 break;
161632 }
161633
161634 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
161635 ** can be changed at start-time using the
161636 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
161637 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
161638 */
161639 case SQLITE_CONFIG_URI: {
161640 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
161641 ** argument of type int. If non-zero, then URI handling is globally
161642 ** enabled. If the parameter is zero, then URI handling is globally
161643 ** disabled. */
161644 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
161645 break;
161646 }
161647
161648 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
161649 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
161650 ** option takes a single integer argument which is interpreted as a
161651 ** boolean in order to enable or disable the use of covering indices for
161652 ** full table scans in the query optimizer. */
161653 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
161654 break;
161655 }
161656
161657#ifdef SQLITE_ENABLE_SQLLOG
161658 case SQLITE_CONFIG_SQLLOG: {
161659 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
161660 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
161661 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
161662 break;
161663 }
161664#endif
161665
161666 case SQLITE_CONFIG_MMAP_SIZE: {
161667 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
161668 ** integer (sqlite3_int64) values that are the default mmap size limit
161669 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
161670 ** mmap size limit. */
161671 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
161672 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
161673 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
161674 ** negative, then that argument is changed to its compile-time default.
161675 **
161676 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
161677 ** silently truncated if necessary so that it does not exceed the
161678 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
161679 ** compile-time option.
161680 */
161681 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
161682 mxMmap = SQLITE_MAX_MMAP_SIZE;
161683 }
161684 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
161685 if( szMmap>mxMmap) szMmap = mxMmap;
161686 sqlite3GlobalConfig.mxMmap = mxMmap;
161687 sqlite3GlobalConfig.szMmap = szMmap;
161688 break;
161689 }
161690
161691#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
161692 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
161693 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
161694 ** unsigned integer value that specifies the maximum size of the created
161695 ** heap. */
161696 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
161697 break;
161698 }
161699#endif
161700
161701 case SQLITE_CONFIG_PMASZ: {
161702 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
161703 break;
161704 }
161705
161706 case SQLITE_CONFIG_STMTJRNL_SPILL: {
161707 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
161708 break;
161709 }
161710
161711#ifdef SQLITE_ENABLE_SORTER_REFERENCES
161712 case SQLITE_CONFIG_SORTERREF_SIZE: {
161713 int iVal = va_arg(ap, int);
161714 if( iVal<0 ){
161715 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
161716 }
161717 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
161718 break;
161719 }
161720#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
161721
161722#ifdef SQLITE_ENABLE_DESERIALIZE
161723 case SQLITE_CONFIG_MEMDB_MAXSIZE: {
161724 sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
161725 break;
161726 }
161727#endif /* SQLITE_ENABLE_DESERIALIZE */
161728
161729 default: {
161730 rc = SQLITE_ERROR;
161731 break;
161732 }
161733 }
161734 va_end(ap);
161735 return rc;
161736}
161737
161738/*
161739** Set up the lookaside buffers for a database connection.
161740** Return SQLITE_OK on success.
161741** If lookaside is already active, return SQLITE_BUSY.
161742**
161743** The sz parameter is the number of bytes in each lookaside slot.
161744** The cnt parameter is the number of slots. If pStart is NULL the
161745** space for the lookaside memory is obtained from sqlite3_malloc().
161746** If pStart is not NULL then it is sz*cnt bytes of memory to use for
161747** the lookaside memory.
161748*/
161749static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
161750#ifndef SQLITE_OMIT_LOOKASIDE
161751 void *pStart;
161752 sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
161753 int nBig; /* Number of full-size slots */
161754 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
161755
161756 if( sqlite3LookasideUsed(db,0)>0 ){
161757 return SQLITE_BUSY;
161758 }
161759 /* Free any existing lookaside buffer for this handle before
161760 ** allocating a new one so we don't have to have space for
161761 ** both at the same time.
161762 */
161763 if( db->lookaside.bMalloced ){
161764 sqlite3_free(db->lookaside.pStart);
161765 }
161766 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
161767 ** than a pointer to be useful.
161768 */
161769 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
161770 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
161771 if( cnt<0 ) cnt = 0;
161772 if( sz==0 || cnt==0 ){
161773 sz = 0;
161774 pStart = 0;
161775 }else if( pBuf==0 ){
161776 sqlite3BeginBenignMalloc();
161777 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
161778 sqlite3EndBenignMalloc();
161779 if( pStart ) szAlloc = sqlite3MallocSize(pStart);
161780 }else{
161781 pStart = pBuf;
161782 }
161783#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161784 if( sz>=LOOKASIDE_SMALL*3 ){
161785 nBig = szAlloc/(3*LOOKASIDE_SMALL+sz);
161786 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
161787 }else if( sz>=LOOKASIDE_SMALL*2 ){
161788 nBig = szAlloc/(LOOKASIDE_SMALL+sz);
161789 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
161790 }else
161791#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161792 if( sz>0 ){
161793 nBig = szAlloc/sz;
161794 nSm = 0;
161795 }else{
161796 nBig = nSm = 0;
161797 }
161798 db->lookaside.pStart = pStart;
161799 db->lookaside.pInit = 0;
161800 db->lookaside.pFree = 0;
161801 db->lookaside.sz = (u16)sz;
161802 db->lookaside.szTrue = (u16)sz;
161803 if( pStart ){
161804 int i;
161805 LookasideSlot *p;
161806 assert( sz > (int)sizeof(LookasideSlot*) );
161807 p = (LookasideSlot*)pStart;
161808 for(i=0; i<nBig; i++){
161809 p->pNext = db->lookaside.pInit;
161810 db->lookaside.pInit = p;
161811 p = (LookasideSlot*)&((u8*)p)[sz];
161812 }
161813#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161814 db->lookaside.pSmallInit = 0;
161815 db->lookaside.pSmallFree = 0;
161816 db->lookaside.pMiddle = p;
161817 for(i=0; i<nSm; i++){
161818 p->pNext = db->lookaside.pSmallInit;
161819 db->lookaside.pSmallInit = p;
161820 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL];
161821 }
161822#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161823 assert( ((uptr)p)<=szAlloc + (uptr)pStart );
161824 db->lookaside.pEnd = p;
161825 db->lookaside.bDisable = 0;
161826 db->lookaside.bMalloced = pBuf==0 ?1:0;
161827 db->lookaside.nSlot = nBig+nSm;
161828 }else{
161829 db->lookaside.pStart = db;
161830#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
161831 db->lookaside.pSmallInit = 0;
161832 db->lookaside.pSmallFree = 0;
161833 db->lookaside.pMiddle = db;
161834#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
161835 db->lookaside.pEnd = db;
161836 db->lookaside.bDisable = 1;
161837 db->lookaside.sz = 0;
161838 db->lookaside.bMalloced = 0;
161839 db->lookaside.nSlot = 0;
161840 }
161841 assert( sqlite3LookasideUsed(db,0)==0 );
161842#endif /* SQLITE_OMIT_LOOKASIDE */
161843 return SQLITE_OK;
161844}
161845
161846/*
161847** Return the mutex associated with a database connection.
161848*/
161849SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
161850#ifdef SQLITE_ENABLE_API_ARMOR
161851 if( !sqlite3SafetyCheckOk(db) ){
161852 (void)SQLITE_MISUSE_BKPT;
161853 return 0;
161854 }
161855#endif
161856 return db->mutex;
161857}
161858
161859/*
161860** Free up as much memory as we can from the given database
161861** connection.
161862*/
161863SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
161864 int i;
161865
161866#ifdef SQLITE_ENABLE_API_ARMOR
161867 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
161868#endif
161869 sqlite3_mutex_enter(db->mutex);
161870 sqlite3BtreeEnterAll(db);
161871 for(i=0; i<db->nDb; i++){
161872 Btree *pBt = db->aDb[i].pBt;
161873 if( pBt ){
161874 Pager *pPager = sqlite3BtreePager(pBt);
161875 sqlite3PagerShrink(pPager);
161876 }
161877 }
161878 sqlite3BtreeLeaveAll(db);
161879 sqlite3_mutex_leave(db->mutex);
161880 return SQLITE_OK;
161881}
161882
161883/*
161884** Flush any dirty pages in the pager-cache for any attached database
161885** to disk.
161886*/
161887SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
161888 int i;
161889 int rc = SQLITE_OK;
161890 int bSeenBusy = 0;
161891
161892#ifdef SQLITE_ENABLE_API_ARMOR
161893 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
161894#endif
161895 sqlite3_mutex_enter(db->mutex);
161896 sqlite3BtreeEnterAll(db);
161897 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
161898 Btree *pBt = db->aDb[i].pBt;
161899 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
161900 Pager *pPager = sqlite3BtreePager(pBt);
161901 rc = sqlite3PagerFlush(pPager);
161902 if( rc==SQLITE_BUSY ){
161903 bSeenBusy = 1;
161904 rc = SQLITE_OK;
161905 }
161906 }
161907 }
161908 sqlite3BtreeLeaveAll(db);
161909 sqlite3_mutex_leave(db->mutex);
161910 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
161911}
161912
161913/*
161914** Configuration settings for an individual database connection
161915*/
161916SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
161917 va_list ap;
161918 int rc;
161919 va_start(ap, op);
161920 switch( op ){
161921 case SQLITE_DBCONFIG_MAINDBNAME: {
161922 /* IMP: R-06824-28531 */
161923 /* IMP: R-36257-52125 */
161924 db->aDb[0].zDbSName = va_arg(ap,char*);
161925 rc = SQLITE_OK;
161926 break;
161927 }
161928 case SQLITE_DBCONFIG_LOOKASIDE: {
161929 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
161930 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
161931 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
161932 rc = setupLookaside(db, pBuf, sz, cnt);
161933 break;
161934 }
161935 default: {
161936 static const struct {
161937 int op; /* The opcode */
161938 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
161939 } aFlagOp[] = {
161940 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
161941 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
161942 { SQLITE_DBCONFIG_ENABLE_VIEW, SQLITE_EnableView },
161943 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
161944 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
161945 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
161946 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
161947 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
161948 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
161949 { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive },
161950 { SQLITE_DBCONFIG_WRITABLE_SCHEMA, SQLITE_WriteSchema|
161951 SQLITE_NoSchemaError },
161952 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, SQLITE_LegacyAlter },
161953 { SQLITE_DBCONFIG_DQS_DDL, SQLITE_DqsDDL },
161954 { SQLITE_DBCONFIG_DQS_DML, SQLITE_DqsDML },
161955 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT, SQLITE_LegacyFileFmt },
161956 { SQLITE_DBCONFIG_TRUSTED_SCHEMA, SQLITE_TrustedSchema },
161957 };
161958 unsigned int i;
161959 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
161960 for(i=0; i<ArraySize(aFlagOp); i++){
161961 if( aFlagOp[i].op==op ){
161962 int onoff = va_arg(ap, int);
161963 int *pRes = va_arg(ap, int*);
161964 u64 oldFlags = db->flags;
161965 if( onoff>0 ){
161966 db->flags |= aFlagOp[i].mask;
161967 }else if( onoff==0 ){
161968 db->flags &= ~(u64)aFlagOp[i].mask;
161969 }
161970 if( oldFlags!=db->flags ){
161971 sqlite3ExpirePreparedStatements(db, 0);
161972 }
161973 if( pRes ){
161974 *pRes = (db->flags & aFlagOp[i].mask)!=0;
161975 }
161976 rc = SQLITE_OK;
161977 break;
161978 }
161979 }
161980 break;
161981 }
161982 }
161983 va_end(ap);
161984 return rc;
161985}
161986
161987/*
161988** This is the default collating function named "BINARY" which is always
161989** available.
161990*/
161991static int binCollFunc(
161992 void *NotUsed,
161993 int nKey1, const void *pKey1,
161994 int nKey2, const void *pKey2
161995){
161996 int rc, n;
161997 UNUSED_PARAMETER(NotUsed);
161998 n = nKey1<nKey2 ? nKey1 : nKey2;
161999 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
162000 ** strings byte by byte using the memcmp() function from the standard C
162001 ** library. */
162002 assert( pKey1 && pKey2 );
162003 rc = memcmp(pKey1, pKey2, n);
162004 if( rc==0 ){
162005 rc = nKey1 - nKey2;
162006 }
162007 return rc;
162008}
162009
162010/*
162011** This is the collating function named "RTRIM" which is always
162012** available. Ignore trailing spaces.
162013*/
162014static int rtrimCollFunc(
162015 void *pUser,
162016 int nKey1, const void *pKey1,
162017 int nKey2, const void *pKey2
162018){
162019 const u8 *pK1 = (const u8*)pKey1;
162020 const u8 *pK2 = (const u8*)pKey2;
162021 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
162022 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
162023 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
162024}
162025
162026/*
162027** Return true if CollSeq is the default built-in BINARY.
162028*/
162029SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
162030 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
162031 return p==0 || p->xCmp==binCollFunc;
162032}
162033
162034/*
162035** Another built-in collating sequence: NOCASE.
162036**
162037** This collating sequence is intended to be used for "case independent
162038** comparison". SQLite's knowledge of upper and lower case equivalents
162039** extends only to the 26 characters used in the English language.
162040**
162041** At the moment there is only a UTF-8 implementation.
162042*/
162043static int nocaseCollatingFunc(
162044 void *NotUsed,
162045 int nKey1, const void *pKey1,
162046 int nKey2, const void *pKey2
162047){
162048 int r = sqlite3StrNICmp(
162049 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
162050 UNUSED_PARAMETER(NotUsed);
162051 if( 0==r ){
162052 r = nKey1-nKey2;
162053 }
162054 return r;
162055}
162056
162057/*
162058** Return the ROWID of the most recent insert
162059*/
162060SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
162061#ifdef SQLITE_ENABLE_API_ARMOR
162062 if( !sqlite3SafetyCheckOk(db) ){
162063 (void)SQLITE_MISUSE_BKPT;
162064 return 0;
162065 }
162066#endif
162067 return db->lastRowid;
162068}
162069
162070/*
162071** Set the value returned by the sqlite3_last_insert_rowid() API function.
162072*/
162073SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
162074#ifdef SQLITE_ENABLE_API_ARMOR
162075 if( !sqlite3SafetyCheckOk(db) ){
162076 (void)SQLITE_MISUSE_BKPT;
162077 return;
162078 }
162079#endif
162080 sqlite3_mutex_enter(db->mutex);
162081 db->lastRowid = iRowid;
162082 sqlite3_mutex_leave(db->mutex);
162083}
162084
162085/*
162086** Return the number of changes in the most recent call to sqlite3_exec().
162087*/
162088SQLITE_API int sqlite3_changes(sqlite3 *db){
162089#ifdef SQLITE_ENABLE_API_ARMOR
162090 if( !sqlite3SafetyCheckOk(db) ){
162091 (void)SQLITE_MISUSE_BKPT;
162092 return 0;
162093 }
162094#endif
162095 return db->nChange;
162096}
162097
162098/*
162099** Return the number of changes since the database handle was opened.
162100*/
162101SQLITE_API int sqlite3_total_changes(sqlite3 *db){
162102#ifdef SQLITE_ENABLE_API_ARMOR
162103 if( !sqlite3SafetyCheckOk(db) ){
162104 (void)SQLITE_MISUSE_BKPT;
162105 return 0;
162106 }
162107#endif
162108 return db->nTotalChange;
162109}
162110
162111/*
162112** Close all open savepoints. This function only manipulates fields of the
162113** database handle object, it does not close any savepoints that may be open
162114** at the b-tree/pager level.
162115*/
162116SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
162117 while( db->pSavepoint ){
162118 Savepoint *pTmp = db->pSavepoint;
162119 db->pSavepoint = pTmp->pNext;
162120 sqlite3DbFree(db, pTmp);
162121 }
162122 db->nSavepoint = 0;
162123 db->nStatement = 0;
162124 db->isTransactionSavepoint = 0;
162125}
162126
162127/*
162128** Invoke the destructor function associated with FuncDef p, if any. Except,
162129** if this is not the last copy of the function, do not invoke it. Multiple
162130** copies of a single function are created when create_function() is called
162131** with SQLITE_ANY as the encoding.
162132*/
162133static void functionDestroy(sqlite3 *db, FuncDef *p){
162134 FuncDestructor *pDestructor = p->u.pDestructor;
162135 if( pDestructor ){
162136 pDestructor->nRef--;
162137 if( pDestructor->nRef==0 ){
162138 pDestructor->xDestroy(pDestructor->pUserData);
162139 sqlite3DbFree(db, pDestructor);
162140 }
162141 }
162142}
162143
162144/*
162145** Disconnect all sqlite3_vtab objects that belong to database connection
162146** db. This is called when db is being closed.
162147*/
162148static void disconnectAllVtab(sqlite3 *db){
162149#ifndef SQLITE_OMIT_VIRTUALTABLE
162150 int i;
162151 HashElem *p;
162152 sqlite3BtreeEnterAll(db);
162153 for(i=0; i<db->nDb; i++){
162154 Schema *pSchema = db->aDb[i].pSchema;
162155 if( pSchema ){
162156 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
162157 Table *pTab = (Table *)sqliteHashData(p);
162158 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
162159 }
162160 }
162161 }
162162 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
162163 Module *pMod = (Module *)sqliteHashData(p);
162164 if( pMod->pEpoTab ){
162165 sqlite3VtabDisconnect(db, pMod->pEpoTab);
162166 }
162167 }
162168 sqlite3VtabUnlockList(db);
162169 sqlite3BtreeLeaveAll(db);
162170#else
162171 UNUSED_PARAMETER(db);
162172#endif
162173}
162174
162175/*
162176** Return TRUE if database connection db has unfinalized prepared
162177** statements or unfinished sqlite3_backup objects.
162178*/
162179static int connectionIsBusy(sqlite3 *db){
162180 int j;
162181 assert( sqlite3_mutex_held(db->mutex) );
162182 if( db->pVdbe ) return 1;
162183 for(j=0; j<db->nDb; j++){
162184 Btree *pBt = db->aDb[j].pBt;
162185 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
162186 }
162187 return 0;
162188}
162189
162190/*
162191** Close an existing SQLite database
162192*/
162193static int sqlite3Close(sqlite3 *db, int forceZombie){
162194 if( !db ){
162195 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
162196 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
162197 return SQLITE_OK;
162198 }
162199 if( !sqlite3SafetyCheckSickOrOk(db) ){
162200 return SQLITE_MISUSE_BKPT;
162201 }
162202 sqlite3_mutex_enter(db->mutex);
162203 if( db->mTrace & SQLITE_TRACE_CLOSE ){
162204 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
162205 }
162206
162207 /* Force xDisconnect calls on all virtual tables */
162208 disconnectAllVtab(db);
162209
162210 /* If a transaction is open, the disconnectAllVtab() call above
162211 ** will not have called the xDisconnect() method on any virtual
162212 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
162213 ** call will do so. We need to do this before the check for active
162214 ** SQL statements below, as the v-table implementation may be storing
162215 ** some prepared statements internally.
162216 */
162217 sqlite3VtabRollback(db);
162218
162219 /* Legacy behavior (sqlite3_close() behavior) is to return
162220 ** SQLITE_BUSY if the connection can not be closed immediately.
162221 */
162222 if( !forceZombie && connectionIsBusy(db) ){
162223 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
162224 "statements or unfinished backups");
162225 sqlite3_mutex_leave(db->mutex);
162226 return SQLITE_BUSY;
162227 }
162228
162229#ifdef SQLITE_ENABLE_SQLLOG
162230 if( sqlite3GlobalConfig.xSqllog ){
162231 /* Closing the handle. Fourth parameter is passed the value 2. */
162232 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
162233 }
162234#endif
162235
162236 /* Convert the connection into a zombie and then close it.
162237 */
162238 db->magic = SQLITE_MAGIC_ZOMBIE;
162239 sqlite3LeaveMutexAndCloseZombie(db);
162240 return SQLITE_OK;
162241}
162242
162243/*
162244** Two variations on the public interface for closing a database
162245** connection. The sqlite3_close() version returns SQLITE_BUSY and
162246** leaves the connection option if there are unfinalized prepared
162247** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
162248** version forces the connection to become a zombie if there are
162249** unclosed resources, and arranges for deallocation when the last
162250** prepare statement or sqlite3_backup closes.
162251*/
162252SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
162253SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
162254
162255
162256/*
162257** Close the mutex on database connection db.
162258**
162259** Furthermore, if database connection db is a zombie (meaning that there
162260** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
162261** every sqlite3_stmt has now been finalized and every sqlite3_backup has
162262** finished, then free all resources.
162263*/
162264SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
162265 HashElem *i; /* Hash table iterator */
162266 int j;
162267
162268 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
162269 ** or if the connection has not yet been closed by sqlite3_close_v2(),
162270 ** then just leave the mutex and return.
162271 */
162272 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
162273 sqlite3_mutex_leave(db->mutex);
162274 return;
162275 }
162276
162277 /* If we reach this point, it means that the database connection has
162278 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
162279 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
162280 ** go ahead and free all resources.
162281 */
162282
162283 /* If a transaction is open, roll it back. This also ensures that if
162284 ** any database schemas have been modified by an uncommitted transaction
162285 ** they are reset. And that the required b-tree mutex is held to make
162286 ** the pager rollback and schema reset an atomic operation. */
162287 sqlite3RollbackAll(db, SQLITE_OK);
162288
162289 /* Free any outstanding Savepoint structures. */
162290 sqlite3CloseSavepoints(db);
162291
162292 /* Close all database connections */
162293 for(j=0; j<db->nDb; j++){
162294 struct Db *pDb = &db->aDb[j];
162295 if( pDb->pBt ){
162296 sqlite3BtreeClose(pDb->pBt);
162297 pDb->pBt = 0;
162298 if( j!=1 ){
162299 pDb->pSchema = 0;
162300 }
162301 }
162302 }
162303 /* Clear the TEMP schema separately and last */
162304 if( db->aDb[1].pSchema ){
162305 sqlite3SchemaClear(db->aDb[1].pSchema);
162306 }
162307 sqlite3VtabUnlockList(db);
162308
162309 /* Free up the array of auxiliary databases */
162310 sqlite3CollapseDatabaseArray(db);
162311 assert( db->nDb<=2 );
162312 assert( db->aDb==db->aDbStatic );
162313
162314 /* Tell the code in notify.c that the connection no longer holds any
162315 ** locks and does not require any further unlock-notify callbacks.
162316 */
162317 sqlite3ConnectionClosed(db);
162318
162319 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
162320 FuncDef *pNext, *p;
162321 p = sqliteHashData(i);
162322 do{
162323 functionDestroy(db, p);
162324 pNext = p->pNext;
162325 sqlite3DbFree(db, p);
162326 p = pNext;
162327 }while( p );
162328 }
162329 sqlite3HashClear(&db->aFunc);
162330 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
162331 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
162332 /* Invoke any destructors registered for collation sequence user data. */
162333 for(j=0; j<3; j++){
162334 if( pColl[j].xDel ){
162335 pColl[j].xDel(pColl[j].pUser);
162336 }
162337 }
162338 sqlite3DbFree(db, pColl);
162339 }
162340 sqlite3HashClear(&db->aCollSeq);
162341#ifndef SQLITE_OMIT_VIRTUALTABLE
162342 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
162343 Module *pMod = (Module *)sqliteHashData(i);
162344 sqlite3VtabEponymousTableClear(db, pMod);
162345 sqlite3VtabModuleUnref(db, pMod);
162346 }
162347 sqlite3HashClear(&db->aModule);
162348#endif
162349
162350 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
162351 sqlite3ValueFree(db->pErr);
162352 sqlite3CloseExtensions(db);
162353#if SQLITE_USER_AUTHENTICATION
162354 sqlite3_free(db->auth.zAuthUser);
162355 sqlite3_free(db->auth.zAuthPW);
162356#endif
162357
162358 db->magic = SQLITE_MAGIC_ERROR;
162359
162360 /* The temp-database schema is allocated differently from the other schema
162361 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
162362 ** So it needs to be freed here. Todo: Why not roll the temp schema into
162363 ** the same sqliteMalloc() as the one that allocates the database
162364 ** structure?
162365 */
162366 sqlite3DbFree(db, db->aDb[1].pSchema);
162367 sqlite3_mutex_leave(db->mutex);
162368 db->magic = SQLITE_MAGIC_CLOSED;
162369 sqlite3_mutex_free(db->mutex);
162370 assert( sqlite3LookasideUsed(db,0)==0 );
162371 if( db->lookaside.bMalloced ){
162372 sqlite3_free(db->lookaside.pStart);
162373 }
162374 sqlite3_free(db);
162375}
162376
162377/*
162378** Rollback all database files. If tripCode is not SQLITE_OK, then
162379** any write cursors are invalidated ("tripped" - as in "tripping a circuit
162380** breaker") and made to return tripCode if there are any further
162381** attempts to use that cursor. Read cursors remain open and valid
162382** but are "saved" in case the table pages are moved around.
162383*/
162384SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
162385 int i;
162386 int inTrans = 0;
162387 int schemaChange;
162388 assert( sqlite3_mutex_held(db->mutex) );
162389 sqlite3BeginBenignMalloc();
162390
162391 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
162392 ** This is important in case the transaction being rolled back has
162393 ** modified the database schema. If the b-tree mutexes are not taken
162394 ** here, then another shared-cache connection might sneak in between
162395 ** the database rollback and schema reset, which can cause false
162396 ** corruption reports in some cases. */
162397 sqlite3BtreeEnterAll(db);
162398 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
162399
162400 for(i=0; i<db->nDb; i++){
162401 Btree *p = db->aDb[i].pBt;
162402 if( p ){
162403 if( sqlite3BtreeIsInTrans(p) ){
162404 inTrans = 1;
162405 }
162406 sqlite3BtreeRollback(p, tripCode, !schemaChange);
162407 }
162408 }
162409 sqlite3VtabRollback(db);
162410 sqlite3EndBenignMalloc();
162411
162412 if( schemaChange ){
162413 sqlite3ExpirePreparedStatements(db, 0);
162414 sqlite3ResetAllSchemasOfConnection(db);
162415 }
162416 sqlite3BtreeLeaveAll(db);
162417
162418 /* Any deferred constraint violations have now been resolved. */
162419 db->nDeferredCons = 0;
162420 db->nDeferredImmCons = 0;
162421 db->flags &= ~(u64)SQLITE_DeferFKs;
162422
162423 /* If one has been configured, invoke the rollback-hook callback */
162424 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
162425 db->xRollbackCallback(db->pRollbackArg);
162426 }
162427}
162428
162429/*
162430** Return a static string containing the name corresponding to the error code
162431** specified in the argument.
162432*/
162433#if defined(SQLITE_NEED_ERR_NAME)
162434SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
162435 const char *zName = 0;
162436 int i, origRc = rc;
162437 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
162438 switch( rc ){
162439 case SQLITE_OK: zName = "SQLITE_OK"; break;
162440 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
162441 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
162442 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
162443 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
162444 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
162445 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
162446 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
162447 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
162448 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
162449 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
162450 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
162451 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
162452 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
162453 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
162454 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
162455 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
162456 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
162457 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
162458 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
162459 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
162460 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
162461 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
162462 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
162463 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
162464 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
162465 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
162466 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
162467 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
162468 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
162469 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
162470 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
162471 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
162472 case SQLITE_IOERR_CHECKRESERVEDLOCK:
162473 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
162474 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
162475 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
162476 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
162477 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
162478 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
162479 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
162480 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
162481 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
162482 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
162483 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
162484 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
162485 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
162486 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
162487 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
162488 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
162489 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
162490 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
162491 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
162492 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
162493 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
162494 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
162495 case SQLITE_CANTOPEN_SYMLINK: zName = "SQLITE_CANTOPEN_SYMLINK"; break;
162496 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
162497 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
162498 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
162499 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
162500 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
162501 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
162502 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
162503 case SQLITE_CONSTRAINT_FOREIGNKEY:
162504 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
162505 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
162506 case SQLITE_CONSTRAINT_PRIMARYKEY:
162507 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
162508 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
162509 case SQLITE_CONSTRAINT_COMMITHOOK:
162510 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
162511 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
162512 case SQLITE_CONSTRAINT_FUNCTION:
162513 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
162514 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
162515 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
162516 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
162517 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
162518 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
162519 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
162520 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
162521 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
162522 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
162523 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
162524 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
162525 case SQLITE_NOTICE_RECOVER_ROLLBACK:
162526 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
162527 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
162528 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
162529 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
162530 }
162531 }
162532 if( zName==0 ){
162533 static char zBuf[50];
162534 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
162535 zName = zBuf;
162536 }
162537 return zName;
162538}
162539#endif
162540
162541/*
162542** Return a static string that describes the kind of error specified in the
162543** argument.
162544*/
162545SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
162546 static const char* const aMsg[] = {
162547 /* SQLITE_OK */ "not an error",
162548 /* SQLITE_ERROR */ "SQL logic error",
162549 /* SQLITE_INTERNAL */ 0,
162550 /* SQLITE_PERM */ "access permission denied",
162551 /* SQLITE_ABORT */ "query aborted",
162552 /* SQLITE_BUSY */ "database is locked",
162553 /* SQLITE_LOCKED */ "database table is locked",
162554 /* SQLITE_NOMEM */ "out of memory",
162555 /* SQLITE_READONLY */ "attempt to write a readonly database",
162556 /* SQLITE_INTERRUPT */ "interrupted",
162557 /* SQLITE_IOERR */ "disk I/O error",
162558 /* SQLITE_CORRUPT */ "database disk image is malformed",
162559 /* SQLITE_NOTFOUND */ "unknown operation",
162560 /* SQLITE_FULL */ "database or disk is full",
162561 /* SQLITE_CANTOPEN */ "unable to open database file",
162562 /* SQLITE_PROTOCOL */ "locking protocol",
162563 /* SQLITE_EMPTY */ 0,
162564 /* SQLITE_SCHEMA */ "database schema has changed",
162565 /* SQLITE_TOOBIG */ "string or blob too big",
162566 /* SQLITE_CONSTRAINT */ "constraint failed",
162567 /* SQLITE_MISMATCH */ "datatype mismatch",
162568 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
162569#ifdef SQLITE_DISABLE_LFS
162570 /* SQLITE_NOLFS */ "large file support is disabled",
162571#else
162572 /* SQLITE_NOLFS */ 0,
162573#endif
162574 /* SQLITE_AUTH */ "authorization denied",
162575 /* SQLITE_FORMAT */ 0,
162576 /* SQLITE_RANGE */ "column index out of range",
162577 /* SQLITE_NOTADB */ "file is not a database",
162578 /* SQLITE_NOTICE */ "notification message",
162579 /* SQLITE_WARNING */ "warning message",
162580 };
162581 const char *zErr = "unknown error";
162582 switch( rc ){
162583 case SQLITE_ABORT_ROLLBACK: {
162584 zErr = "abort due to ROLLBACK";
162585 break;
162586 }
162587 case SQLITE_ROW: {
162588 zErr = "another row available";
162589 break;
162590 }
162591 case SQLITE_DONE: {
162592 zErr = "no more rows available";
162593 break;
162594 }
162595 default: {
162596 rc &= 0xff;
162597 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
162598 zErr = aMsg[rc];
162599 }
162600 break;
162601 }
162602 }
162603 return zErr;
162604}
162605
162606/*
162607** This routine implements a busy callback that sleeps and tries
162608** again until a timeout value is reached. The timeout value is
162609** an integer number of milliseconds passed in as the first
162610** argument.
162611**
162612** Return non-zero to retry the lock. Return zero to stop trying
162613** and cause SQLite to return SQLITE_BUSY.
162614*/
162615static int sqliteDefaultBusyCallback(
162616 void *ptr, /* Database connection */
162617 int count /* Number of times table has been busy */
162618){
162619#if SQLITE_OS_WIN || HAVE_USLEEP
162620 /* This case is for systems that have support for sleeping for fractions of
162621 ** a second. Examples: All windows systems, unix systems with usleep() */
162622 static const u8 delays[] =
162623 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
162624 static const u8 totals[] =
162625 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
162626# define NDELAY ArraySize(delays)
162627 sqlite3 *db = (sqlite3 *)ptr;
162628 int tmout = db->busyTimeout;
162629 int delay, prior;
162630
162631 assert( count>=0 );
162632 if( count < NDELAY ){
162633 delay = delays[count];
162634 prior = totals[count];
162635 }else{
162636 delay = delays[NDELAY-1];
162637 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
162638 }
162639 if( prior + delay > tmout ){
162640 delay = tmout - prior;
162641 if( delay<=0 ) return 0;
162642 }
162643 sqlite3OsSleep(db->pVfs, delay*1000);
162644 return 1;
162645#else
162646 /* This case for unix systems that lack usleep() support. Sleeping
162647 ** must be done in increments of whole seconds */
162648 sqlite3 *db = (sqlite3 *)ptr;
162649 int tmout = ((sqlite3 *)ptr)->busyTimeout;
162650 if( (count+1)*1000 > tmout ){
162651 return 0;
162652 }
162653 sqlite3OsSleep(db->pVfs, 1000000);
162654 return 1;
162655#endif
162656}
162657
162658/*
162659** Invoke the given busy handler.
162660**
162661** This routine is called when an operation failed to acquire a
162662** lock on VFS file pFile.
162663**
162664** If this routine returns non-zero, the lock is retried. If it
162665** returns 0, the operation aborts with an SQLITE_BUSY error.
162666*/
162667SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
162668 int rc;
162669 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
162670 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
162671 if( rc==0 ){
162672 p->nBusy = -1;
162673 }else{
162674 p->nBusy++;
162675 }
162676 return rc;
162677}
162678
162679/*
162680** This routine sets the busy callback for an Sqlite database to the
162681** given callback function with the given argument.
162682*/
162683SQLITE_API int sqlite3_busy_handler(
162684 sqlite3 *db,
162685 int (*xBusy)(void*,int),
162686 void *pArg
162687){
162688#ifdef SQLITE_ENABLE_API_ARMOR
162689 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162690#endif
162691 sqlite3_mutex_enter(db->mutex);
162692 db->busyHandler.xBusyHandler = xBusy;
162693 db->busyHandler.pBusyArg = pArg;
162694 db->busyHandler.nBusy = 0;
162695 db->busyTimeout = 0;
162696 sqlite3_mutex_leave(db->mutex);
162697 return SQLITE_OK;
162698}
162699
162700#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
162701/*
162702** This routine sets the progress callback for an Sqlite database to the
162703** given callback function with the given argument. The progress callback will
162704** be invoked every nOps opcodes.
162705*/
162706SQLITE_API void sqlite3_progress_handler(
162707 sqlite3 *db,
162708 int nOps,
162709 int (*xProgress)(void*),
162710 void *pArg
162711){
162712#ifdef SQLITE_ENABLE_API_ARMOR
162713 if( !sqlite3SafetyCheckOk(db) ){
162714 (void)SQLITE_MISUSE_BKPT;
162715 return;
162716 }
162717#endif
162718 sqlite3_mutex_enter(db->mutex);
162719 if( nOps>0 ){
162720 db->xProgress = xProgress;
162721 db->nProgressOps = (unsigned)nOps;
162722 db->pProgressArg = pArg;
162723 }else{
162724 db->xProgress = 0;
162725 db->nProgressOps = 0;
162726 db->pProgressArg = 0;
162727 }
162728 sqlite3_mutex_leave(db->mutex);
162729}
162730#endif
162731
162732
162733/*
162734** This routine installs a default busy handler that waits for the
162735** specified number of milliseconds before returning 0.
162736*/
162737SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
162738#ifdef SQLITE_ENABLE_API_ARMOR
162739 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
162740#endif
162741 if( ms>0 ){
162742 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
162743 (void*)db);
162744 db->busyTimeout = ms;
162745 }else{
162746 sqlite3_busy_handler(db, 0, 0);
162747 }
162748 return SQLITE_OK;
162749}
162750
162751/*
162752** Cause any pending operation to stop at its earliest opportunity.
162753*/
162754SQLITE_API void sqlite3_interrupt(sqlite3 *db){
162755#ifdef SQLITE_ENABLE_API_ARMOR
162756 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
162757 (void)SQLITE_MISUSE_BKPT;
162758 return;
162759 }
162760#endif
162761 AtomicStore(&db->u1.isInterrupted, 1);
162762}
162763
162764
162765/*
162766** This function is exactly the same as sqlite3_create_function(), except
162767** that it is designed to be called by internal code. The difference is
162768** that if a malloc() fails in sqlite3_create_function(), an error code
162769** is returned and the mallocFailed flag cleared.
162770*/
162771SQLITE_PRIVATE int sqlite3CreateFunc(
162772 sqlite3 *db,
162773 const char *zFunctionName,
162774 int nArg,
162775 int enc,
162776 void *pUserData,
162777 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162778 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162779 void (*xFinal)(sqlite3_context*),
162780 void (*xValue)(sqlite3_context*),
162781 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
162782 FuncDestructor *pDestructor
162783){
162784 FuncDef *p;
162785 int nName;
162786 int extraFlags;
162787
162788 assert( sqlite3_mutex_held(db->mutex) );
162789 assert( xValue==0 || xSFunc==0 );
162790 if( zFunctionName==0 /* Must have a valid name */
162791 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
162792 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
162793 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
162794 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
162795 || (255<(nName = sqlite3Strlen30( zFunctionName)))
162796 ){
162797 return SQLITE_MISUSE_BKPT;
162798 }
162799
162800 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
162801 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
162802 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
162803 SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
162804 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
162805
162806 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
162807 ** the meaning is inverted. So flip the bit. */
162808 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
162809 extraFlags ^= SQLITE_FUNC_UNSAFE;
162810
162811
162812#ifndef SQLITE_OMIT_UTF16
162813 /* If SQLITE_UTF16 is specified as the encoding type, transform this
162814 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
162815 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
162816 **
162817 ** If SQLITE_ANY is specified, add three versions of the function
162818 ** to the hash table.
162819 */
162820 if( enc==SQLITE_UTF16 ){
162821 enc = SQLITE_UTF16NATIVE;
162822 }else if( enc==SQLITE_ANY ){
162823 int rc;
162824 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
162825 (SQLITE_UTF8|extraFlags)^SQLITE_FUNC_UNSAFE,
162826 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
162827 if( rc==SQLITE_OK ){
162828 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
162829 (SQLITE_UTF16LE|extraFlags)^SQLITE_FUNC_UNSAFE,
162830 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
162831 }
162832 if( rc!=SQLITE_OK ){
162833 return rc;
162834 }
162835 enc = SQLITE_UTF16BE;
162836 }
162837#else
162838 enc = SQLITE_UTF8;
162839#endif
162840
162841 /* Check if an existing function is being overridden or deleted. If so,
162842 ** and there are active VMs, then return SQLITE_BUSY. If a function
162843 ** is being overridden/deleted but there are no active VMs, allow the
162844 ** operation to continue but invalidate all precompiled statements.
162845 */
162846 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
162847 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
162848 if( db->nVdbeActive ){
162849 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
162850 "unable to delete/modify user-function due to active statements");
162851 assert( !db->mallocFailed );
162852 return SQLITE_BUSY;
162853 }else{
162854 sqlite3ExpirePreparedStatements(db, 0);
162855 }
162856 }
162857
162858 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
162859 assert(p || db->mallocFailed);
162860 if( !p ){
162861 return SQLITE_NOMEM_BKPT;
162862 }
162863
162864 /* If an older version of the function with a configured destructor is
162865 ** being replaced invoke the destructor function here. */
162866 functionDestroy(db, p);
162867
162868 if( pDestructor ){
162869 pDestructor->nRef++;
162870 }
162871 p->u.pDestructor = pDestructor;
162872 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
162873 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
162874 testcase( p->funcFlags & SQLITE_DIRECTONLY );
162875 p->xSFunc = xSFunc ? xSFunc : xStep;
162876 p->xFinalize = xFinal;
162877 p->xValue = xValue;
162878 p->xInverse = xInverse;
162879 p->pUserData = pUserData;
162880 p->nArg = (u16)nArg;
162881 return SQLITE_OK;
162882}
162883
162884/*
162885** Worker function used by utf-8 APIs that create new functions:
162886**
162887** sqlite3_create_function()
162888** sqlite3_create_function_v2()
162889** sqlite3_create_window_function()
162890*/
162891static int createFunctionApi(
162892 sqlite3 *db,
162893 const char *zFunc,
162894 int nArg,
162895 int enc,
162896 void *p,
162897 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
162898 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
162899 void (*xFinal)(sqlite3_context*),
162900 void (*xValue)(sqlite3_context*),
162901 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
162902 void(*xDestroy)(void*)
162903){
162904 int rc = SQLITE_ERROR;
162905 FuncDestructor *pArg = 0;
162906
162907#ifdef SQLITE_ENABLE_API_ARMOR
162908 if( !sqlite3SafetyCheckOk(db) ){
162909 return SQLITE_MISUSE_BKPT;
162910 }
162911#endif
162912 sqlite3_mutex_enter(db->mutex);
162913 if( xDestroy ){
162914 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
162915 if( !pArg ){
162916 sqlite3OomFault(db);
162917 xDestroy(p);
162918 goto out;
162919 }
162920 pArg->nRef = 0;
162921 pArg->xDestroy = xDestroy;
162922 pArg->pUserData = p;
162923 }
162924 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
162925 xSFunc, xStep, xFinal, xValue, xInverse, pArg
162926 );
162927 if( pArg && pArg->nRef==0 ){
162928 assert( rc!=SQLITE_OK );
162929 xDestroy(p);
162930 sqlite3_free(pArg);
162931 }
162932
162933 out:
162934 rc = sqlite3ApiExit(db, rc);
162935 sqlite3_mutex_leave(db->mutex);
162936 return rc;
162937}
162938
162939/*
162940** Create new user functions.
162941*/
162942SQLITE_API int sqlite3_create_function(
162943 sqlite3 *db,
162944 const char *zFunc,
162945 int nArg,
162946 int enc,
162947 void *p,
162948 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162949 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162950 void (*xFinal)(sqlite3_context*)
162951){
162952 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
162953 xFinal, 0, 0, 0);
162954}
162955SQLITE_API int sqlite3_create_function_v2(
162956 sqlite3 *db,
162957 const char *zFunc,
162958 int nArg,
162959 int enc,
162960 void *p,
162961 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
162962 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162963 void (*xFinal)(sqlite3_context*),
162964 void (*xDestroy)(void *)
162965){
162966 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
162967 xFinal, 0, 0, xDestroy);
162968}
162969SQLITE_API int sqlite3_create_window_function(
162970 sqlite3 *db,
162971 const char *zFunc,
162972 int nArg,
162973 int enc,
162974 void *p,
162975 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
162976 void (*xFinal)(sqlite3_context*),
162977 void (*xValue)(sqlite3_context*),
162978 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
162979 void (*xDestroy)(void *)
162980){
162981 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
162982 xFinal, xValue, xInverse, xDestroy);
162983}
162984
162985#ifndef SQLITE_OMIT_UTF16
162986SQLITE_API int sqlite3_create_function16(
162987 sqlite3 *db,
162988 const void *zFunctionName,
162989 int nArg,
162990 int eTextRep,
162991 void *p,
162992 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
162993 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
162994 void (*xFinal)(sqlite3_context*)
162995){
162996 int rc;
162997 char *zFunc8;
162998
162999#ifdef SQLITE_ENABLE_API_ARMOR
163000 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
163001#endif
163002 sqlite3_mutex_enter(db->mutex);
163003 assert( !db->mallocFailed );
163004 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
163005 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
163006 sqlite3DbFree(db, zFunc8);
163007 rc = sqlite3ApiExit(db, rc);
163008 sqlite3_mutex_leave(db->mutex);
163009 return rc;
163010}
163011#endif
163012
163013
163014/*
163015** The following is the implementation of an SQL function that always
163016** fails with an error message stating that the function is used in the
163017** wrong context. The sqlite3_overload_function() API might construct
163018** SQL function that use this routine so that the functions will exist
163019** for name resolution but are actually overloaded by the xFindFunction
163020** method of virtual tables.
163021*/
163022static void sqlite3InvalidFunction(
163023 sqlite3_context *context, /* The function calling context */
163024 int NotUsed, /* Number of arguments to the function */
163025 sqlite3_value **NotUsed2 /* Value of each argument */
163026){
163027 const char *zName = (const char*)sqlite3_user_data(context);
163028 char *zErr;
163029 UNUSED_PARAMETER2(NotUsed, NotUsed2);
163030 zErr = sqlite3_mprintf(
163031 "unable to use function %s in the requested context", zName);
163032 sqlite3_result_error(context, zErr, -1);
163033 sqlite3_free(zErr);
163034}
163035
163036/*
163037** Declare that a function has been overloaded by a virtual table.
163038**
163039** If the function already exists as a regular global function, then
163040** this routine is a no-op. If the function does not exist, then create
163041** a new one that always throws a run-time error.
163042**
163043** When virtual tables intend to provide an overloaded function, they
163044** should call this routine to make sure the global function exists.
163045** A global function must exist in order for name resolution to work
163046** properly.
163047*/
163048SQLITE_API int sqlite3_overload_function(
163049 sqlite3 *db,
163050 const char *zName,
163051 int nArg
163052){
163053 int rc;
163054 char *zCopy;
163055
163056#ifdef SQLITE_ENABLE_API_ARMOR
163057 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
163058 return SQLITE_MISUSE_BKPT;
163059 }
163060#endif
163061 sqlite3_mutex_enter(db->mutex);
163062 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
163063 sqlite3_mutex_leave(db->mutex);
163064 if( rc ) return SQLITE_OK;
163065 zCopy = sqlite3_mprintf(zName);
163066 if( zCopy==0 ) return SQLITE_NOMEM;
163067 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
163068 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
163069}
163070
163071#ifndef SQLITE_OMIT_TRACE
163072/*
163073** Register a trace function. The pArg from the previously registered trace
163074** is returned.
163075**
163076** A NULL trace function means that no tracing is executes. A non-NULL
163077** trace is a pointer to a function that is invoked at the start of each
163078** SQL statement.
163079*/
163080#ifndef SQLITE_OMIT_DEPRECATED
163081SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
163082 void *pOld;
163083
163084#ifdef SQLITE_ENABLE_API_ARMOR
163085 if( !sqlite3SafetyCheckOk(db) ){
163086 (void)SQLITE_MISUSE_BKPT;
163087 return 0;
163088 }
163089#endif
163090 sqlite3_mutex_enter(db->mutex);
163091 pOld = db->pTraceArg;
163092 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
163093 db->trace.xLegacy = xTrace;
163094 db->pTraceArg = pArg;
163095 sqlite3_mutex_leave(db->mutex);
163096 return pOld;
163097}
163098#endif /* SQLITE_OMIT_DEPRECATED */
163099
163100/* Register a trace callback using the version-2 interface.
163101*/
163102SQLITE_API int sqlite3_trace_v2(
163103 sqlite3 *db, /* Trace this connection */
163104 unsigned mTrace, /* Mask of events to be traced */
163105 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
163106 void *pArg /* Context */
163107){
163108#ifdef SQLITE_ENABLE_API_ARMOR
163109 if( !sqlite3SafetyCheckOk(db) ){
163110 return SQLITE_MISUSE_BKPT;
163111 }
163112#endif
163113 sqlite3_mutex_enter(db->mutex);
163114 if( mTrace==0 ) xTrace = 0;
163115 if( xTrace==0 ) mTrace = 0;
163116 db->mTrace = mTrace;
163117 db->trace.xV2 = xTrace;
163118 db->pTraceArg = pArg;
163119 sqlite3_mutex_leave(db->mutex);
163120 return SQLITE_OK;
163121}
163122
163123#ifndef SQLITE_OMIT_DEPRECATED
163124/*
163125** Register a profile function. The pArg from the previously registered
163126** profile function is returned.
163127**
163128** A NULL profile function means that no profiling is executes. A non-NULL
163129** profile is a pointer to a function that is invoked at the conclusion of
163130** each SQL statement that is run.
163131*/
163132SQLITE_API void *sqlite3_profile(
163133 sqlite3 *db,
163134 void (*xProfile)(void*,const char*,sqlite_uint64),
163135 void *pArg
163136){
163137 void *pOld;
163138
163139#ifdef SQLITE_ENABLE_API_ARMOR
163140 if( !sqlite3SafetyCheckOk(db) ){
163141 (void)SQLITE_MISUSE_BKPT;
163142 return 0;
163143 }
163144#endif
163145 sqlite3_mutex_enter(db->mutex);
163146 pOld = db->pProfileArg;
163147 db->xProfile = xProfile;
163148 db->pProfileArg = pArg;
163149 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
163150 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
163151 sqlite3_mutex_leave(db->mutex);
163152 return pOld;
163153}
163154#endif /* SQLITE_OMIT_DEPRECATED */
163155#endif /* SQLITE_OMIT_TRACE */
163156
163157/*
163158** Register a function to be invoked when a transaction commits.
163159** If the invoked function returns non-zero, then the commit becomes a
163160** rollback.
163161*/
163162SQLITE_API void *sqlite3_commit_hook(
163163 sqlite3 *db, /* Attach the hook to this database */
163164 int (*xCallback)(void*), /* Function to invoke on each commit */
163165 void *pArg /* Argument to the function */
163166){
163167 void *pOld;
163168
163169#ifdef SQLITE_ENABLE_API_ARMOR
163170 if( !sqlite3SafetyCheckOk(db) ){
163171 (void)SQLITE_MISUSE_BKPT;
163172 return 0;
163173 }
163174#endif
163175 sqlite3_mutex_enter(db->mutex);
163176 pOld = db->pCommitArg;
163177 db->xCommitCallback = xCallback;
163178 db->pCommitArg = pArg;
163179 sqlite3_mutex_leave(db->mutex);
163180 return pOld;
163181}
163182
163183/*
163184** Register a callback to be invoked each time a row is updated,
163185** inserted or deleted using this database connection.
163186*/
163187SQLITE_API void *sqlite3_update_hook(
163188 sqlite3 *db, /* Attach the hook to this database */
163189 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
163190 void *pArg /* Argument to the function */
163191){
163192 void *pRet;
163193
163194#ifdef SQLITE_ENABLE_API_ARMOR
163195 if( !sqlite3SafetyCheckOk(db) ){
163196 (void)SQLITE_MISUSE_BKPT;
163197 return 0;
163198 }
163199#endif
163200 sqlite3_mutex_enter(db->mutex);
163201 pRet = db->pUpdateArg;
163202 db->xUpdateCallback = xCallback;
163203 db->pUpdateArg = pArg;
163204 sqlite3_mutex_leave(db->mutex);
163205 return pRet;
163206}
163207
163208/*
163209** Register a callback to be invoked each time a transaction is rolled
163210** back by this database connection.
163211*/
163212SQLITE_API void *sqlite3_rollback_hook(
163213 sqlite3 *db, /* Attach the hook to this database */
163214 void (*xCallback)(void*), /* Callback function */
163215 void *pArg /* Argument to the function */
163216){
163217 void *pRet;
163218
163219#ifdef SQLITE_ENABLE_API_ARMOR
163220 if( !sqlite3SafetyCheckOk(db) ){
163221 (void)SQLITE_MISUSE_BKPT;
163222 return 0;
163223 }
163224#endif
163225 sqlite3_mutex_enter(db->mutex);
163226 pRet = db->pRollbackArg;
163227 db->xRollbackCallback = xCallback;
163228 db->pRollbackArg = pArg;
163229 sqlite3_mutex_leave(db->mutex);
163230 return pRet;
163231}
163232
163233#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
163234/*
163235** Register a callback to be invoked each time a row is updated,
163236** inserted or deleted using this database connection.
163237*/
163238SQLITE_API void *sqlite3_preupdate_hook(
163239 sqlite3 *db, /* Attach the hook to this database */
163240 void(*xCallback)( /* Callback function */
163241 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
163242 void *pArg /* First callback argument */
163243){
163244 void *pRet;
163245 sqlite3_mutex_enter(db->mutex);
163246 pRet = db->pPreUpdateArg;
163247 db->xPreUpdateCallback = xCallback;
163248 db->pPreUpdateArg = pArg;
163249 sqlite3_mutex_leave(db->mutex);
163250 return pRet;
163251}
163252#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
163253
163254#ifndef SQLITE_OMIT_WAL
163255/*
163256** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
163257** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
163258** is greater than sqlite3.pWalArg cast to an integer (the value configured by
163259** wal_autocheckpoint()).
163260*/
163261SQLITE_PRIVATE int sqlite3WalDefaultHook(
163262 void *pClientData, /* Argument */
163263 sqlite3 *db, /* Connection */
163264 const char *zDb, /* Database */
163265 int nFrame /* Size of WAL */
163266){
163267 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
163268 sqlite3BeginBenignMalloc();
163269 sqlite3_wal_checkpoint(db, zDb);
163270 sqlite3EndBenignMalloc();
163271 }
163272 return SQLITE_OK;
163273}
163274#endif /* SQLITE_OMIT_WAL */
163275
163276/*
163277** Configure an sqlite3_wal_hook() callback to automatically checkpoint
163278** a database after committing a transaction if there are nFrame or
163279** more frames in the log file. Passing zero or a negative value as the
163280** nFrame parameter disables automatic checkpoints entirely.
163281**
163282** The callback registered by this function replaces any existing callback
163283** registered using sqlite3_wal_hook(). Likewise, registering a callback
163284** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
163285** configured by this function.
163286*/
163287SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
163288#ifdef SQLITE_OMIT_WAL
163289 UNUSED_PARAMETER(db);
163290 UNUSED_PARAMETER(nFrame);
163291#else
163292#ifdef SQLITE_ENABLE_API_ARMOR
163293 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
163294#endif
163295 if( nFrame>0 ){
163296 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
163297 }else{
163298 sqlite3_wal_hook(db, 0, 0);
163299 }
163300#endif
163301 return SQLITE_OK;
163302}
163303
163304/*
163305** Register a callback to be invoked each time a transaction is written
163306** into the write-ahead-log by this database connection.
163307*/
163308SQLITE_API void *sqlite3_wal_hook(
163309 sqlite3 *db, /* Attach the hook to this db handle */
163310 int(*xCallback)(void *, sqlite3*, const char*, int),
163311 void *pArg /* First argument passed to xCallback() */
163312){
163313#ifndef SQLITE_OMIT_WAL
163314 void *pRet;
163315#ifdef SQLITE_ENABLE_API_ARMOR
163316 if( !sqlite3SafetyCheckOk(db) ){
163317 (void)SQLITE_MISUSE_BKPT;
163318 return 0;
163319 }
163320#endif
163321 sqlite3_mutex_enter(db->mutex);
163322 pRet = db->pWalArg;
163323 db->xWalCallback = xCallback;
163324 db->pWalArg = pArg;
163325 sqlite3_mutex_leave(db->mutex);
163326 return pRet;
163327#else
163328 return 0;
163329#endif
163330}
163331
163332/*
163333** Checkpoint database zDb.
163334*/
163335SQLITE_API int sqlite3_wal_checkpoint_v2(
163336 sqlite3 *db, /* Database handle */
163337 const char *zDb, /* Name of attached database (or NULL) */
163338 int eMode, /* SQLITE_CHECKPOINT_* value */
163339 int *pnLog, /* OUT: Size of WAL log in frames */
163340 int *pnCkpt /* OUT: Total number of frames checkpointed */
163341){
163342#ifdef SQLITE_OMIT_WAL
163343 return SQLITE_OK;
163344#else
163345 int rc; /* Return code */
163346 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
163347
163348#ifdef SQLITE_ENABLE_API_ARMOR
163349 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
163350#endif
163351
163352 /* Initialize the output variables to -1 in case an error occurs. */
163353 if( pnLog ) *pnLog = -1;
163354 if( pnCkpt ) *pnCkpt = -1;
163355
163356 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
163357 assert( SQLITE_CHECKPOINT_FULL==1 );
163358 assert( SQLITE_CHECKPOINT_RESTART==2 );
163359 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
163360 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
163361 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
163362 ** mode: */
163363 return SQLITE_MISUSE;
163364 }
163365
163366 sqlite3_mutex_enter(db->mutex);
163367 if( zDb && zDb[0] ){
163368 iDb = sqlite3FindDbName(db, zDb);
163369 }
163370 if( iDb<0 ){
163371 rc = SQLITE_ERROR;
163372 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
163373 }else{
163374 db->busyHandler.nBusy = 0;
163375 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
163376 sqlite3Error(db, rc);
163377 }
163378 rc = sqlite3ApiExit(db, rc);
163379
163380 /* If there are no active statements, clear the interrupt flag at this
163381 ** point. */
163382 if( db->nVdbeActive==0 ){
163383 AtomicStore(&db->u1.isInterrupted, 0);
163384 }
163385
163386 sqlite3_mutex_leave(db->mutex);
163387 return rc;
163388#endif
163389}
163390
163391
163392/*
163393** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
163394** to contains a zero-length string, all attached databases are
163395** checkpointed.
163396*/
163397SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
163398 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
163399 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
163400 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
163401}
163402
163403#ifndef SQLITE_OMIT_WAL
163404/*
163405** Run a checkpoint on database iDb. This is a no-op if database iDb is
163406** not currently open in WAL mode.
163407**
163408** If a transaction is open on the database being checkpointed, this
163409** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
163410** an error occurs while running the checkpoint, an SQLite error code is
163411** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
163412**
163413** The mutex on database handle db should be held by the caller. The mutex
163414** associated with the specific b-tree being checkpointed is taken by
163415** this function while the checkpoint is running.
163416**
163417** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
163418** checkpointed. If an error is encountered it is returned immediately -
163419** no attempt is made to checkpoint any remaining databases.
163420**
163421** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
163422** or TRUNCATE.
163423*/
163424SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
163425 int rc = SQLITE_OK; /* Return code */
163426 int i; /* Used to iterate through attached dbs */
163427 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
163428
163429 assert( sqlite3_mutex_held(db->mutex) );
163430 assert( !pnLog || *pnLog==-1 );
163431 assert( !pnCkpt || *pnCkpt==-1 );
163432
163433 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
163434 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
163435 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
163436 pnLog = 0;
163437 pnCkpt = 0;
163438 if( rc==SQLITE_BUSY ){
163439 bBusy = 1;
163440 rc = SQLITE_OK;
163441 }
163442 }
163443 }
163444
163445 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
163446}
163447#endif /* SQLITE_OMIT_WAL */
163448
163449/*
163450** This function returns true if main-memory should be used instead of
163451** a temporary file for transient pager files and statement journals.
163452** The value returned depends on the value of db->temp_store (runtime
163453** parameter) and the compile time value of SQLITE_TEMP_STORE. The
163454** following table describes the relationship between these two values
163455** and this functions return value.
163456**
163457** SQLITE_TEMP_STORE db->temp_store Location of temporary database
163458** ----------------- -------------- ------------------------------
163459** 0 any file (return 0)
163460** 1 1 file (return 0)
163461** 1 2 memory (return 1)
163462** 1 0 file (return 0)
163463** 2 1 file (return 0)
163464** 2 2 memory (return 1)
163465** 2 0 memory (return 1)
163466** 3 any memory (return 1)
163467*/
163468SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
163469#if SQLITE_TEMP_STORE==1
163470 return ( db->temp_store==2 );
163471#endif
163472#if SQLITE_TEMP_STORE==2
163473 return ( db->temp_store!=1 );
163474#endif
163475#if SQLITE_TEMP_STORE==3
163476 UNUSED_PARAMETER(db);
163477 return 1;
163478#endif
163479#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
163480 UNUSED_PARAMETER(db);
163481 return 0;
163482#endif
163483}
163484
163485/*
163486** Return UTF-8 encoded English language explanation of the most recent
163487** error.
163488*/
163489SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
163490 const char *z;
163491 if( !db ){
163492 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
163493 }
163494 if( !sqlite3SafetyCheckSickOrOk(db) ){
163495 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
163496 }
163497 sqlite3_mutex_enter(db->mutex);
163498 if( db->mallocFailed ){
163499 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
163500 }else{
163501 testcase( db->pErr==0 );
163502 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
163503 assert( !db->mallocFailed );
163504 if( z==0 ){
163505 z = sqlite3ErrStr(db->errCode);
163506 }
163507 }
163508 sqlite3_mutex_leave(db->mutex);
163509 return z;
163510}
163511
163512#ifndef SQLITE_OMIT_UTF16
163513/*
163514** Return UTF-16 encoded English language explanation of the most recent
163515** error.
163516*/
163517SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
163518 static const u16 outOfMem[] = {
163519 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
163520 };
163521 static const u16 misuse[] = {
163522 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
163523 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
163524 'm', 'i', 's', 'u', 's', 'e', 0
163525 };
163526
163527 const void *z;
163528 if( !db ){
163529 return (void *)outOfMem;
163530 }
163531 if( !sqlite3SafetyCheckSickOrOk(db) ){
163532 return (void *)misuse;
163533 }
163534 sqlite3_mutex_enter(db->mutex);
163535 if( db->mallocFailed ){
163536 z = (void *)outOfMem;
163537 }else{
163538 z = sqlite3_value_text16(db->pErr);
163539 if( z==0 ){
163540 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
163541 z = sqlite3_value_text16(db->pErr);
163542 }
163543 /* A malloc() may have failed within the call to sqlite3_value_text16()
163544 ** above. If this is the case, then the db->mallocFailed flag needs to
163545 ** be cleared before returning. Do this directly, instead of via
163546 ** sqlite3ApiExit(), to avoid setting the database handle error message.
163547 */
163548 sqlite3OomClear(db);
163549 }
163550 sqlite3_mutex_leave(db->mutex);
163551 return z;
163552}
163553#endif /* SQLITE_OMIT_UTF16 */
163554
163555/*
163556** Return the most recent error code generated by an SQLite routine. If NULL is
163557** passed to this function, we assume a malloc() failed during sqlite3_open().
163558*/
163559SQLITE_API int sqlite3_errcode(sqlite3 *db){
163560 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
163561 return SQLITE_MISUSE_BKPT;
163562 }
163563 if( !db || db->mallocFailed ){
163564 return SQLITE_NOMEM_BKPT;
163565 }
163566 return db->errCode & db->errMask;
163567}
163568SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
163569 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
163570 return SQLITE_MISUSE_BKPT;
163571 }
163572 if( !db || db->mallocFailed ){
163573 return SQLITE_NOMEM_BKPT;
163574 }
163575 return db->errCode;
163576}
163577SQLITE_API int sqlite3_system_errno(sqlite3 *db){
163578 return db ? db->iSysErrno : 0;
163579}
163580
163581/*
163582** Return a string that describes the kind of error specified in the
163583** argument. For now, this simply calls the internal sqlite3ErrStr()
163584** function.
163585*/
163586SQLITE_API const char *sqlite3_errstr(int rc){
163587 return sqlite3ErrStr(rc);
163588}
163589
163590/*
163591** Create a new collating function for database "db". The name is zName
163592** and the encoding is enc.
163593*/
163594static int createCollation(
163595 sqlite3* db,
163596 const char *zName,
163597 u8 enc,
163598 void* pCtx,
163599 int(*xCompare)(void*,int,const void*,int,const void*),
163600 void(*xDel)(void*)
163601){
163602 CollSeq *pColl;
163603 int enc2;
163604
163605 assert( sqlite3_mutex_held(db->mutex) );
163606
163607 /* If SQLITE_UTF16 is specified as the encoding type, transform this
163608 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
163609 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
163610 */
163611 enc2 = enc;
163612 testcase( enc2==SQLITE_UTF16 );
163613 testcase( enc2==SQLITE_UTF16_ALIGNED );
163614 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
163615 enc2 = SQLITE_UTF16NATIVE;
163616 }
163617 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
163618 return SQLITE_MISUSE_BKPT;
163619 }
163620
163621 /* Check if this call is removing or replacing an existing collation
163622 ** sequence. If so, and there are active VMs, return busy. If there
163623 ** are no active VMs, invalidate any pre-compiled statements.
163624 */
163625 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
163626 if( pColl && pColl->xCmp ){
163627 if( db->nVdbeActive ){
163628 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
163629 "unable to delete/modify collation sequence due to active statements");
163630 return SQLITE_BUSY;
163631 }
163632 sqlite3ExpirePreparedStatements(db, 0);
163633
163634 /* If collation sequence pColl was created directly by a call to
163635 ** sqlite3_create_collation, and not generated by synthCollSeq(),
163636 ** then any copies made by synthCollSeq() need to be invalidated.
163637 ** Also, collation destructor - CollSeq.xDel() - function may need
163638 ** to be called.
163639 */
163640 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
163641 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
163642 int j;
163643 for(j=0; j<3; j++){
163644 CollSeq *p = &aColl[j];
163645 if( p->enc==pColl->enc ){
163646 if( p->xDel ){
163647 p->xDel(p->pUser);
163648 }
163649 p->xCmp = 0;
163650 }
163651 }
163652 }
163653 }
163654
163655 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
163656 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
163657 pColl->xCmp = xCompare;
163658 pColl->pUser = pCtx;
163659 pColl->xDel = xDel;
163660 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
163661 sqlite3Error(db, SQLITE_OK);
163662 return SQLITE_OK;
163663}
163664
163665
163666/*
163667** This array defines hard upper bounds on limit values. The
163668** initializer must be kept in sync with the SQLITE_LIMIT_*
163669** #defines in sqlite3.h.
163670*/
163671static const int aHardLimit[] = {
163672 SQLITE_MAX_LENGTH,
163673 SQLITE_MAX_SQL_LENGTH,
163674 SQLITE_MAX_COLUMN,
163675 SQLITE_MAX_EXPR_DEPTH,
163676 SQLITE_MAX_COMPOUND_SELECT,
163677 SQLITE_MAX_VDBE_OP,
163678 SQLITE_MAX_FUNCTION_ARG,
163679 SQLITE_MAX_ATTACHED,
163680 SQLITE_MAX_LIKE_PATTERN_LENGTH,
163681 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
163682 SQLITE_MAX_TRIGGER_DEPTH,
163683 SQLITE_MAX_WORKER_THREADS,
163684};
163685
163686/*
163687** Make sure the hard limits are set to reasonable values
163688*/
163689#if SQLITE_MAX_LENGTH<100
163690# error SQLITE_MAX_LENGTH must be at least 100
163691#endif
163692#if SQLITE_MAX_SQL_LENGTH<100
163693# error SQLITE_MAX_SQL_LENGTH must be at least 100
163694#endif
163695#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
163696# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
163697#endif
163698#if SQLITE_MAX_COMPOUND_SELECT<2
163699# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
163700#endif
163701#if SQLITE_MAX_VDBE_OP<40
163702# error SQLITE_MAX_VDBE_OP must be at least 40
163703#endif
163704#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
163705# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
163706#endif
163707#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
163708# error SQLITE_MAX_ATTACHED must be between 0 and 125
163709#endif
163710#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
163711# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
163712#endif
163713#if SQLITE_MAX_COLUMN>32767
163714# error SQLITE_MAX_COLUMN must not exceed 32767
163715#endif
163716#if SQLITE_MAX_TRIGGER_DEPTH<1
163717# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
163718#endif
163719#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
163720# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
163721#endif
163722
163723
163724/*
163725** Change the value of a limit. Report the old value.
163726** If an invalid limit index is supplied, report -1.
163727** Make no changes but still report the old value if the
163728** new limit is negative.
163729**
163730** A new lower limit does not shrink existing constructs.
163731** It merely prevents new constructs that exceed the limit
163732** from forming.
163733*/
163734SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
163735 int oldLimit;
163736
163737#ifdef SQLITE_ENABLE_API_ARMOR
163738 if( !sqlite3SafetyCheckOk(db) ){
163739 (void)SQLITE_MISUSE_BKPT;
163740 return -1;
163741 }
163742#endif
163743
163744 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
163745 ** there is a hard upper bound set at compile-time by a C preprocessor
163746 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
163747 ** "_MAX_".)
163748 */
163749 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
163750 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
163751 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
163752 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
163753 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
163754 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
163755 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
163756 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
163757 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
163758 SQLITE_MAX_LIKE_PATTERN_LENGTH );
163759 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
163760 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
163761 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
163762 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
163763
163764
163765 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
163766 return -1;
163767 }
163768 oldLimit = db->aLimit[limitId];
163769 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
163770 if( newLimit>aHardLimit[limitId] ){
163771 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
163772 }
163773 db->aLimit[limitId] = newLimit;
163774 }
163775 return oldLimit; /* IMP: R-53341-35419 */
163776}
163777
163778/*
163779** This function is used to parse both URIs and non-URI filenames passed by the
163780** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
163781** URIs specified as part of ATTACH statements.
163782**
163783** The first argument to this function is the name of the VFS to use (or
163784** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
163785** query parameter. The second argument contains the URI (or non-URI filename)
163786** itself. When this function is called the *pFlags variable should contain
163787** the default flags to open the database handle with. The value stored in
163788** *pFlags may be updated before returning if the URI filename contains
163789** "cache=xxx" or "mode=xxx" query parameters.
163790**
163791** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
163792** the VFS that should be used to open the database file. *pzFile is set to
163793** point to a buffer containing the name of the file to open. The value
163794** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
163795** and is in the same format as names created using sqlite3_create_filename().
163796** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
163797** the value returned in *pzFile to avoid a memory leak.
163798**
163799** If an error occurs, then an SQLite error code is returned and *pzErrMsg
163800** may be set to point to a buffer containing an English language error
163801** message. It is the responsibility of the caller to eventually release
163802** this buffer by calling sqlite3_free().
163803*/
163804SQLITE_PRIVATE int sqlite3ParseUri(
163805 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
163806 const char *zUri, /* Nul-terminated URI to parse */
163807 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
163808 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
163809 char **pzFile, /* OUT: Filename component of URI */
163810 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
163811){
163812 int rc = SQLITE_OK;
163813 unsigned int flags = *pFlags;
163814 const char *zVfs = zDefaultVfs;
163815 char *zFile;
163816 char c;
163817 int nUri = sqlite3Strlen30(zUri);
163818
163819 assert( *pzErrMsg==0 );
163820
163821 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
163822 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
163823 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
163824 ){
163825 char *zOpt;
163826 int eState; /* Parser state when parsing URI */
163827 int iIn; /* Input character index */
163828 int iOut = 0; /* Output character index */
163829 u64 nByte = nUri+8; /* Bytes of space to allocate */
163830
163831 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
163832 ** method that there may be extra parameters following the file-name. */
163833 flags |= SQLITE_OPEN_URI;
163834
163835 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
163836 zFile = sqlite3_malloc64(nByte);
163837 if( !zFile ) return SQLITE_NOMEM_BKPT;
163838
163839 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
163840 zFile += 4;
163841
163842 iIn = 5;
163843#ifdef SQLITE_ALLOW_URI_AUTHORITY
163844 if( strncmp(zUri+5, "///", 3)==0 ){
163845 iIn = 7;
163846 /* The following condition causes URIs with five leading / characters
163847 ** like file://///host/path to be converted into UNCs like //host/path.
163848 ** The correct URI for that UNC has only two or four leading / characters
163849 ** file://host/path or file:////host/path. But 5 leading slashes is a
163850 ** common error, we are told, so we handle it as a special case. */
163851 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
163852 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
163853 iIn = 16;
163854 }
163855#else
163856 /* Discard the scheme and authority segments of the URI. */
163857 if( zUri[5]=='/' && zUri[6]=='/' ){
163858 iIn = 7;
163859 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
163860 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
163861 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
163862 iIn-7, &zUri[7]);
163863 rc = SQLITE_ERROR;
163864 goto parse_uri_out;
163865 }
163866 }
163867#endif
163868
163869 /* Copy the filename and any query parameters into the zFile buffer.
163870 ** Decode %HH escape codes along the way.
163871 **
163872 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
163873 ** on the parsing context. As follows:
163874 **
163875 ** 0: Parsing file-name.
163876 ** 1: Parsing name section of a name=value query parameter.
163877 ** 2: Parsing value section of a name=value query parameter.
163878 */
163879 eState = 0;
163880 while( (c = zUri[iIn])!=0 && c!='#' ){
163881 iIn++;
163882 if( c=='%'
163883 && sqlite3Isxdigit(zUri[iIn])
163884 && sqlite3Isxdigit(zUri[iIn+1])
163885 ){
163886 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
163887 octet += sqlite3HexToInt(zUri[iIn++]);
163888
163889 assert( octet>=0 && octet<256 );
163890 if( octet==0 ){
163891#ifndef SQLITE_ENABLE_URI_00_ERROR
163892 /* This branch is taken when "%00" appears within the URI. In this
163893 ** case we ignore all text in the remainder of the path, name or
163894 ** value currently being parsed. So ignore the current character
163895 ** and skip to the next "?", "=" or "&", as appropriate. */
163896 while( (c = zUri[iIn])!=0 && c!='#'
163897 && (eState!=0 || c!='?')
163898 && (eState!=1 || (c!='=' && c!='&'))
163899 && (eState!=2 || c!='&')
163900 ){
163901 iIn++;
163902 }
163903 continue;
163904#else
163905 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
163906 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
163907 rc = SQLITE_ERROR;
163908 goto parse_uri_out;
163909#endif
163910 }
163911 c = octet;
163912 }else if( eState==1 && (c=='&' || c=='=') ){
163913 if( zFile[iOut-1]==0 ){
163914 /* An empty option name. Ignore this option altogether. */
163915 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
163916 continue;
163917 }
163918 if( c=='&' ){
163919 zFile[iOut++] = '\0';
163920 }else{
163921 eState = 2;
163922 }
163923 c = 0;
163924 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
163925 c = 0;
163926 eState = 1;
163927 }
163928 zFile[iOut++] = c;
163929 }
163930 if( eState==1 ) zFile[iOut++] = '\0';
163931 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
163932
163933 /* Check if there were any options specified that should be interpreted
163934 ** here. Options that are interpreted here include "vfs" and those that
163935 ** correspond to flags that may be passed to the sqlite3_open_v2()
163936 ** method. */
163937 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
163938 while( zOpt[0] ){
163939 int nOpt = sqlite3Strlen30(zOpt);
163940 char *zVal = &zOpt[nOpt+1];
163941 int nVal = sqlite3Strlen30(zVal);
163942
163943 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
163944 zVfs = zVal;
163945 }else{
163946 struct OpenMode {
163947 const char *z;
163948 int mode;
163949 } *aMode = 0;
163950 char *zModeType = 0;
163951 int mask = 0;
163952 int limit = 0;
163953
163954 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
163955 static struct OpenMode aCacheMode[] = {
163956 { "shared", SQLITE_OPEN_SHAREDCACHE },
163957 { "private", SQLITE_OPEN_PRIVATECACHE },
163958 { 0, 0 }
163959 };
163960
163961 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
163962 aMode = aCacheMode;
163963 limit = mask;
163964 zModeType = "cache";
163965 }
163966 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
163967 static struct OpenMode aOpenMode[] = {
163968 { "ro", SQLITE_OPEN_READONLY },
163969 { "rw", SQLITE_OPEN_READWRITE },
163970 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
163971 { "memory", SQLITE_OPEN_MEMORY },
163972 { 0, 0 }
163973 };
163974
163975 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
163976 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
163977 aMode = aOpenMode;
163978 limit = mask & flags;
163979 zModeType = "access";
163980 }
163981
163982 if( aMode ){
163983 int i;
163984 int mode = 0;
163985 for(i=0; aMode[i].z; i++){
163986 const char *z = aMode[i].z;
163987 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
163988 mode = aMode[i].mode;
163989 break;
163990 }
163991 }
163992 if( mode==0 ){
163993 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
163994 rc = SQLITE_ERROR;
163995 goto parse_uri_out;
163996 }
163997 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
163998 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
163999 zModeType, zVal);
164000 rc = SQLITE_PERM;
164001 goto parse_uri_out;
164002 }
164003 flags = (flags & ~mask) | mode;
164004 }
164005 }
164006
164007 zOpt = &zVal[nVal+1];
164008 }
164009
164010 }else{
164011 zFile = sqlite3_malloc64(nUri+8);
164012 if( !zFile ) return SQLITE_NOMEM_BKPT;
164013 memset(zFile, 0, 4);
164014 zFile += 4;
164015 if( nUri ){
164016 memcpy(zFile, zUri, nUri);
164017 }
164018 memset(zFile+nUri, 0, 4);
164019 flags &= ~SQLITE_OPEN_URI;
164020 }
164021
164022 *ppVfs = sqlite3_vfs_find(zVfs);
164023 if( *ppVfs==0 ){
164024 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
164025 rc = SQLITE_ERROR;
164026 }
164027 parse_uri_out:
164028 if( rc!=SQLITE_OK ){
164029 sqlite3_free_filename(zFile);
164030 zFile = 0;
164031 }
164032 *pFlags = flags;
164033 *pzFile = zFile;
164034 return rc;
164035}
164036
164037/*
164038** This routine does the core work of extracting URI parameters from a
164039** database filename for the sqlite3_uri_parameter() interface.
164040*/
164041static const char *uriParameter(const char *zFilename, const char *zParam){
164042 zFilename += sqlite3Strlen30(zFilename) + 1;
164043 while( zFilename[0] ){
164044 int x = strcmp(zFilename, zParam);
164045 zFilename += sqlite3Strlen30(zFilename) + 1;
164046 if( x==0 ) return zFilename;
164047 zFilename += sqlite3Strlen30(zFilename) + 1;
164048 }
164049 return 0;
164050}
164051
164052
164053
164054/*
164055** This routine does the work of opening a database on behalf of
164056** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
164057** is UTF-8 encoded.
164058*/
164059static int openDatabase(
164060 const char *zFilename, /* Database filename UTF-8 encoded */
164061 sqlite3 **ppDb, /* OUT: Returned database handle */
164062 unsigned int flags, /* Operational flags */
164063 const char *zVfs /* Name of the VFS to use */
164064){
164065 sqlite3 *db; /* Store allocated handle here */
164066 int rc; /* Return code */
164067 int isThreadsafe; /* True for threadsafe connections */
164068 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
164069 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
164070 int i; /* Loop counter */
164071
164072#ifdef SQLITE_ENABLE_API_ARMOR
164073 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
164074#endif
164075 *ppDb = 0;
164076#ifndef SQLITE_OMIT_AUTOINIT
164077 rc = sqlite3_initialize();
164078 if( rc ) return rc;
164079#endif
164080
164081 if( sqlite3GlobalConfig.bCoreMutex==0 ){
164082 isThreadsafe = 0;
164083 }else if( flags & SQLITE_OPEN_NOMUTEX ){
164084 isThreadsafe = 0;
164085 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
164086 isThreadsafe = 1;
164087 }else{
164088 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
164089 }
164090
164091 if( flags & SQLITE_OPEN_PRIVATECACHE ){
164092 flags &= ~SQLITE_OPEN_SHAREDCACHE;
164093 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
164094 flags |= SQLITE_OPEN_SHAREDCACHE;
164095 }
164096
164097 /* Remove harmful bits from the flags parameter
164098 **
164099 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
164100 ** dealt with in the previous code block. Besides these, the only
164101 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
164102 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
164103 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
164104 ** off all other flags.
164105 */
164106 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
164107 SQLITE_OPEN_EXCLUSIVE |
164108 SQLITE_OPEN_MAIN_DB |
164109 SQLITE_OPEN_TEMP_DB |
164110 SQLITE_OPEN_TRANSIENT_DB |
164111 SQLITE_OPEN_MAIN_JOURNAL |
164112 SQLITE_OPEN_TEMP_JOURNAL |
164113 SQLITE_OPEN_SUBJOURNAL |
164114 SQLITE_OPEN_SUPER_JOURNAL |
164115 SQLITE_OPEN_NOMUTEX |
164116 SQLITE_OPEN_FULLMUTEX |
164117 SQLITE_OPEN_WAL
164118 );
164119
164120 /* Allocate the sqlite data structure */
164121 db = sqlite3MallocZero( sizeof(sqlite3) );
164122 if( db==0 ) goto opendb_out;
164123 if( isThreadsafe
164124#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
164125 || sqlite3GlobalConfig.bCoreMutex
164126#endif
164127 ){
164128 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
164129 if( db->mutex==0 ){
164130 sqlite3_free(db);
164131 db = 0;
164132 goto opendb_out;
164133 }
164134 if( isThreadsafe==0 ){
164135 sqlite3MutexWarnOnContention(db->mutex);
164136 }
164137 }
164138 sqlite3_mutex_enter(db->mutex);
164139 db->errMask = 0xff;
164140 db->nDb = 2;
164141 db->magic = SQLITE_MAGIC_BUSY;
164142 db->aDb = db->aDbStatic;
164143 db->lookaside.bDisable = 1;
164144 db->lookaside.sz = 0;
164145
164146 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
164147 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
164148 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
164149 db->autoCommit = 1;
164150 db->nextAutovac = -1;
164151 db->szMmap = sqlite3GlobalConfig.szMmap;
164152 db->nextPagesize = 0;
164153 db->nMaxSorterMmap = 0x7FFFFFFF;
164154 db->flags |= SQLITE_ShortColNames
164155 | SQLITE_EnableTrigger
164156 | SQLITE_EnableView
164157 | SQLITE_CacheSpill
164158#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
164159 | SQLITE_TrustedSchema
164160#endif
164161/* The SQLITE_DQS compile-time option determines the default settings
164162** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
164163**
164164** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
164165** ---------- ----------------------- -----------------------
164166** undefined on on
164167** 3 on on
164168** 2 on off
164169** 1 off on
164170** 0 off off
164171**
164172** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
164173** and so that is the default. But developers are encouranged to use
164174** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
164175*/
164176#if !defined(SQLITE_DQS)
164177# define SQLITE_DQS 3
164178#endif
164179#if (SQLITE_DQS&1)==1
164180 | SQLITE_DqsDML
164181#endif
164182#if (SQLITE_DQS&2)==2
164183 | SQLITE_DqsDDL
164184#endif
164185
164186#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
164187 | SQLITE_AutoIndex
164188#endif
164189#if SQLITE_DEFAULT_CKPTFULLFSYNC
164190 | SQLITE_CkptFullFSync
164191#endif
164192#if SQLITE_DEFAULT_FILE_FORMAT<4
164193 | SQLITE_LegacyFileFmt
164194#endif
164195#ifdef SQLITE_ENABLE_LOAD_EXTENSION
164196 | SQLITE_LoadExtension
164197#endif
164198#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
164199 | SQLITE_RecTriggers
164200#endif
164201#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
164202 | SQLITE_ForeignKeys
164203#endif
164204#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
164205 | SQLITE_ReverseOrder
164206#endif
164207#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
164208 | SQLITE_CellSizeCk
164209#endif
164210#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
164211 | SQLITE_Fts3Tokenizer
164212#endif
164213#if defined(SQLITE_ENABLE_QPSG)
164214 | SQLITE_EnableQPSG
164215#endif
164216#if defined(SQLITE_DEFAULT_DEFENSIVE)
164217 | SQLITE_Defensive
164218#endif
164219#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
164220 | SQLITE_LegacyAlter
164221#endif
164222 ;
164223 sqlite3HashInit(&db->aCollSeq);
164224#ifndef SQLITE_OMIT_VIRTUALTABLE
164225 sqlite3HashInit(&db->aModule);
164226#endif
164227
164228 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
164229 ** and UTF-16, so add a version for each to avoid any unnecessary
164230 ** conversions. The only error that can occur here is a malloc() failure.
164231 **
164232 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
164233 ** functions:
164234 */
164235 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
164236 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
164237 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
164238 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
164239 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
164240 if( db->mallocFailed ){
164241 goto opendb_out;
164242 }
164243
164244 /* Parse the filename/URI argument
164245 **
164246 ** Only allow sensible combinations of bits in the flags argument.
164247 ** Throw an error if any non-sense combination is used. If we
164248 ** do not block illegal combinations here, it could trigger
164249 ** assert() statements in deeper layers. Sensible combinations
164250 ** are:
164251 **
164252 ** 1: SQLITE_OPEN_READONLY
164253 ** 2: SQLITE_OPEN_READWRITE
164254 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
164255 */
164256 db->openFlags = flags;
164257 assert( SQLITE_OPEN_READONLY == 0x01 );
164258 assert( SQLITE_OPEN_READWRITE == 0x02 );
164259 assert( SQLITE_OPEN_CREATE == 0x04 );
164260 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
164261 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
164262 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
164263 if( ((1<<(flags&7)) & 0x46)==0 ){
164264 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
164265 }else{
164266 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
164267 }
164268 if( rc!=SQLITE_OK ){
164269 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
164270 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
164271 sqlite3_free(zErrMsg);
164272 goto opendb_out;
164273 }
164274
164275 /* Open the backend database driver */
164276 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
164277 flags | SQLITE_OPEN_MAIN_DB);
164278 if( rc!=SQLITE_OK ){
164279 if( rc==SQLITE_IOERR_NOMEM ){
164280 rc = SQLITE_NOMEM_BKPT;
164281 }
164282 sqlite3Error(db, rc);
164283 goto opendb_out;
164284 }
164285 sqlite3BtreeEnter(db->aDb[0].pBt);
164286 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
164287 if( !db->mallocFailed ){
164288 sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
164289 }
164290 sqlite3BtreeLeave(db->aDb[0].pBt);
164291 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
164292
164293 /* The default safety_level for the main database is FULL; for the temp
164294 ** database it is OFF. This matches the pager layer defaults.
164295 */
164296 db->aDb[0].zDbSName = "main";
164297 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
164298 db->aDb[1].zDbSName = "temp";
164299 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
164300
164301 db->magic = SQLITE_MAGIC_OPEN;
164302 if( db->mallocFailed ){
164303 goto opendb_out;
164304 }
164305
164306 /* Register all built-in functions, but do not attempt to read the
164307 ** database schema yet. This is delayed until the first time the database
164308 ** is accessed.
164309 */
164310 sqlite3Error(db, SQLITE_OK);
164311 sqlite3RegisterPerConnectionBuiltinFunctions(db);
164312 rc = sqlite3_errcode(db);
164313
164314
164315 /* Load compiled-in extensions */
164316 for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){
164317 rc = sqlite3BuiltinExtensions[i](db);
164318 }
164319
164320 /* Load automatic extensions - extensions that have been registered
164321 ** using the sqlite3_automatic_extension() API.
164322 */
164323 if( rc==SQLITE_OK ){
164324 sqlite3AutoLoadExtensions(db);
164325 rc = sqlite3_errcode(db);
164326 if( rc!=SQLITE_OK ){
164327 goto opendb_out;
164328 }
164329 }
164330
164331#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
164332 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
164333 ** option gives access to internal functions by default.
164334 ** Testing use only!!! */
164335 db->mDbFlags |= DBFLAG_InternalFunc;
164336#endif
164337
164338 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
164339 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
164340 ** mode. Doing nothing at all also makes NORMAL the default.
164341 */
164342#ifdef SQLITE_DEFAULT_LOCKING_MODE
164343 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
164344 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
164345 SQLITE_DEFAULT_LOCKING_MODE);
164346#endif
164347
164348 if( rc ) sqlite3Error(db, rc);
164349
164350 /* Enable the lookaside-malloc subsystem */
164351 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
164352 sqlite3GlobalConfig.nLookaside);
164353
164354 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
164355
164356opendb_out:
164357 if( db ){
164358 assert( db->mutex!=0 || isThreadsafe==0
164359 || sqlite3GlobalConfig.bFullMutex==0 );
164360 sqlite3_mutex_leave(db->mutex);
164361 }
164362 rc = sqlite3_errcode(db);
164363 assert( db!=0 || rc==SQLITE_NOMEM );
164364 if( rc==SQLITE_NOMEM ){
164365 sqlite3_close(db);
164366 db = 0;
164367 }else if( rc!=SQLITE_OK ){
164368 db->magic = SQLITE_MAGIC_SICK;
164369 }
164370 *ppDb = db;
164371#ifdef SQLITE_ENABLE_SQLLOG
164372 if( sqlite3GlobalConfig.xSqllog ){
164373 /* Opening a db handle. Fourth parameter is passed 0. */
164374 void *pArg = sqlite3GlobalConfig.pSqllogArg;
164375 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
164376 }
164377#endif
164378 sqlite3_free_filename(zOpen);
164379 return rc & 0xff;
164380}
164381
164382
164383/*
164384** Open a new database handle.
164385*/
164386SQLITE_API int sqlite3_open(
164387 const char *zFilename,
164388 sqlite3 **ppDb
164389){
164390 return openDatabase(zFilename, ppDb,
164391 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
164392}
164393SQLITE_API int sqlite3_open_v2(
164394 const char *filename, /* Database filename (UTF-8) */
164395 sqlite3 **ppDb, /* OUT: SQLite db handle */
164396 int flags, /* Flags */
164397 const char *zVfs /* Name of VFS module to use */
164398){
164399 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
164400}
164401
164402#ifndef SQLITE_OMIT_UTF16
164403/*
164404** Open a new database handle.
164405*/
164406SQLITE_API int sqlite3_open16(
164407 const void *zFilename,
164408 sqlite3 **ppDb
164409){
164410 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
164411 sqlite3_value *pVal;
164412 int rc;
164413
164414#ifdef SQLITE_ENABLE_API_ARMOR
164415 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
164416#endif
164417 *ppDb = 0;
164418#ifndef SQLITE_OMIT_AUTOINIT
164419 rc = sqlite3_initialize();
164420 if( rc ) return rc;
164421#endif
164422 if( zFilename==0 ) zFilename = "\000\000";
164423 pVal = sqlite3ValueNew(0);
164424 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
164425 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
164426 if( zFilename8 ){
164427 rc = openDatabase(zFilename8, ppDb,
164428 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
164429 assert( *ppDb || rc==SQLITE_NOMEM );
164430 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
164431 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
164432 }
164433 }else{
164434 rc = SQLITE_NOMEM_BKPT;
164435 }
164436 sqlite3ValueFree(pVal);
164437
164438 return rc & 0xff;
164439}
164440#endif /* SQLITE_OMIT_UTF16 */
164441
164442/*
164443** Register a new collation sequence with the database handle db.
164444*/
164445SQLITE_API int sqlite3_create_collation(
164446 sqlite3* db,
164447 const char *zName,
164448 int enc,
164449 void* pCtx,
164450 int(*xCompare)(void*,int,const void*,int,const void*)
164451){
164452 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
164453}
164454
164455/*
164456** Register a new collation sequence with the database handle db.
164457*/
164458SQLITE_API int sqlite3_create_collation_v2(
164459 sqlite3* db,
164460 const char *zName,
164461 int enc,
164462 void* pCtx,
164463 int(*xCompare)(void*,int,const void*,int,const void*),
164464 void(*xDel)(void*)
164465){
164466 int rc;
164467
164468#ifdef SQLITE_ENABLE_API_ARMOR
164469 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
164470#endif
164471 sqlite3_mutex_enter(db->mutex);
164472 assert( !db->mallocFailed );
164473 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
164474 rc = sqlite3ApiExit(db, rc);
164475 sqlite3_mutex_leave(db->mutex);
164476 return rc;
164477}
164478
164479#ifndef SQLITE_OMIT_UTF16
164480/*
164481** Register a new collation sequence with the database handle db.
164482*/
164483SQLITE_API int sqlite3_create_collation16(
164484 sqlite3* db,
164485 const void *zName,
164486 int enc,
164487 void* pCtx,
164488 int(*xCompare)(void*,int,const void*,int,const void*)
164489){
164490 int rc = SQLITE_OK;
164491 char *zName8;
164492
164493#ifdef SQLITE_ENABLE_API_ARMOR
164494 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
164495#endif
164496 sqlite3_mutex_enter(db->mutex);
164497 assert( !db->mallocFailed );
164498 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
164499 if( zName8 ){
164500 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
164501 sqlite3DbFree(db, zName8);
164502 }
164503 rc = sqlite3ApiExit(db, rc);
164504 sqlite3_mutex_leave(db->mutex);
164505 return rc;
164506}
164507#endif /* SQLITE_OMIT_UTF16 */
164508
164509/*
164510** Register a collation sequence factory callback with the database handle
164511** db. Replace any previously installed collation sequence factory.
164512*/
164513SQLITE_API int sqlite3_collation_needed(
164514 sqlite3 *db,
164515 void *pCollNeededArg,
164516 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
164517){
164518#ifdef SQLITE_ENABLE_API_ARMOR
164519 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164520#endif
164521 sqlite3_mutex_enter(db->mutex);
164522 db->xCollNeeded = xCollNeeded;
164523 db->xCollNeeded16 = 0;
164524 db->pCollNeededArg = pCollNeededArg;
164525 sqlite3_mutex_leave(db->mutex);
164526 return SQLITE_OK;
164527}
164528
164529#ifndef SQLITE_OMIT_UTF16
164530/*
164531** Register a collation sequence factory callback with the database handle
164532** db. Replace any previously installed collation sequence factory.
164533*/
164534SQLITE_API int sqlite3_collation_needed16(
164535 sqlite3 *db,
164536 void *pCollNeededArg,
164537 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
164538){
164539#ifdef SQLITE_ENABLE_API_ARMOR
164540 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164541#endif
164542 sqlite3_mutex_enter(db->mutex);
164543 db->xCollNeeded = 0;
164544 db->xCollNeeded16 = xCollNeeded16;
164545 db->pCollNeededArg = pCollNeededArg;
164546 sqlite3_mutex_leave(db->mutex);
164547 return SQLITE_OK;
164548}
164549#endif /* SQLITE_OMIT_UTF16 */
164550
164551#ifndef SQLITE_OMIT_DEPRECATED
164552/*
164553** This function is now an anachronism. It used to be used to recover from a
164554** malloc() failure, but SQLite now does this automatically.
164555*/
164556SQLITE_API int sqlite3_global_recover(void){
164557 return SQLITE_OK;
164558}
164559#endif
164560
164561/*
164562** Test to see whether or not the database connection is in autocommit
164563** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
164564** by default. Autocommit is disabled by a BEGIN statement and reenabled
164565** by the next COMMIT or ROLLBACK.
164566*/
164567SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
164568#ifdef SQLITE_ENABLE_API_ARMOR
164569 if( !sqlite3SafetyCheckOk(db) ){
164570 (void)SQLITE_MISUSE_BKPT;
164571 return 0;
164572 }
164573#endif
164574 return db->autoCommit;
164575}
164576
164577/*
164578** The following routines are substitutes for constants SQLITE_CORRUPT,
164579** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
164580** constants. They serve two purposes:
164581**
164582** 1. Serve as a convenient place to set a breakpoint in a debugger
164583** to detect when version error conditions occurs.
164584**
164585** 2. Invoke sqlite3_log() to provide the source code location where
164586** a low-level error is first detected.
164587*/
164588SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
164589 sqlite3_log(iErr, "%s at line %d of [%.10s]",
164590 zType, lineno, 20+sqlite3_sourceid());
164591 return iErr;
164592}
164593SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
164594 testcase( sqlite3GlobalConfig.xLog!=0 );
164595 return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
164596}
164597SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
164598 testcase( sqlite3GlobalConfig.xLog!=0 );
164599 return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
164600}
164601SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
164602 testcase( sqlite3GlobalConfig.xLog!=0 );
164603 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
164604}
164605#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
164606SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
164607 char zMsg[100];
164608 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
164609 testcase( sqlite3GlobalConfig.xLog!=0 );
164610 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
164611}
164612#endif
164613#ifdef SQLITE_DEBUG
164614SQLITE_PRIVATE int sqlite3NomemError(int lineno){
164615 testcase( sqlite3GlobalConfig.xLog!=0 );
164616 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
164617}
164618SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
164619 testcase( sqlite3GlobalConfig.xLog!=0 );
164620 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
164621}
164622#endif
164623
164624#ifndef SQLITE_OMIT_DEPRECATED
164625/*
164626** This is a convenience routine that makes sure that all thread-specific
164627** data for this thread has been deallocated.
164628**
164629** SQLite no longer uses thread-specific data so this routine is now a
164630** no-op. It is retained for historical compatibility.
164631*/
164632SQLITE_API void sqlite3_thread_cleanup(void){
164633}
164634#endif
164635
164636/*
164637** Return meta information about a specific column of a database table.
164638** See comment in sqlite3.h (sqlite.h.in) for details.
164639*/
164640SQLITE_API int sqlite3_table_column_metadata(
164641 sqlite3 *db, /* Connection handle */
164642 const char *zDbName, /* Database name or NULL */
164643 const char *zTableName, /* Table name */
164644 const char *zColumnName, /* Column name */
164645 char const **pzDataType, /* OUTPUT: Declared data type */
164646 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
164647 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
164648 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
164649 int *pAutoinc /* OUTPUT: True if column is auto-increment */
164650){
164651 int rc;
164652 char *zErrMsg = 0;
164653 Table *pTab = 0;
164654 Column *pCol = 0;
164655 int iCol = 0;
164656 char const *zDataType = 0;
164657 char const *zCollSeq = 0;
164658 int notnull = 0;
164659 int primarykey = 0;
164660 int autoinc = 0;
164661
164662
164663#ifdef SQLITE_ENABLE_API_ARMOR
164664 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
164665 return SQLITE_MISUSE_BKPT;
164666 }
164667#endif
164668
164669 /* Ensure the database schema has been loaded */
164670 sqlite3_mutex_enter(db->mutex);
164671 sqlite3BtreeEnterAll(db);
164672 rc = sqlite3Init(db, &zErrMsg);
164673 if( SQLITE_OK!=rc ){
164674 goto error_out;
164675 }
164676
164677 /* Locate the table in question */
164678 pTab = sqlite3FindTable(db, zTableName, zDbName);
164679 if( !pTab || pTab->pSelect ){
164680 pTab = 0;
164681 goto error_out;
164682 }
164683
164684 /* Find the column for which info is requested */
164685 if( zColumnName==0 ){
164686 /* Query for existance of table only */
164687 }else{
164688 for(iCol=0; iCol<pTab->nCol; iCol++){
164689 pCol = &pTab->aCol[iCol];
164690 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
164691 break;
164692 }
164693 }
164694 if( iCol==pTab->nCol ){
164695 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
164696 iCol = pTab->iPKey;
164697 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
164698 }else{
164699 pTab = 0;
164700 goto error_out;
164701 }
164702 }
164703 }
164704
164705 /* The following block stores the meta information that will be returned
164706 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
164707 ** and autoinc. At this point there are two possibilities:
164708 **
164709 ** 1. The specified column name was rowid", "oid" or "_rowid_"
164710 ** and there is no explicitly declared IPK column.
164711 **
164712 ** 2. The table is not a view and the column name identified an
164713 ** explicitly declared column. Copy meta information from *pCol.
164714 */
164715 if( pCol ){
164716 zDataType = sqlite3ColumnType(pCol,0);
164717 zCollSeq = pCol->zColl;
164718 notnull = pCol->notNull!=0;
164719 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
164720 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
164721 }else{
164722 zDataType = "INTEGER";
164723 primarykey = 1;
164724 }
164725 if( !zCollSeq ){
164726 zCollSeq = sqlite3StrBINARY;
164727 }
164728
164729error_out:
164730 sqlite3BtreeLeaveAll(db);
164731
164732 /* Whether the function call succeeded or failed, set the output parameters
164733 ** to whatever their local counterparts contain. If an error did occur,
164734 ** this has the effect of zeroing all output parameters.
164735 */
164736 if( pzDataType ) *pzDataType = zDataType;
164737 if( pzCollSeq ) *pzCollSeq = zCollSeq;
164738 if( pNotNull ) *pNotNull = notnull;
164739 if( pPrimaryKey ) *pPrimaryKey = primarykey;
164740 if( pAutoinc ) *pAutoinc = autoinc;
164741
164742 if( SQLITE_OK==rc && !pTab ){
164743 sqlite3DbFree(db, zErrMsg);
164744 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
164745 zColumnName);
164746 rc = SQLITE_ERROR;
164747 }
164748 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
164749 sqlite3DbFree(db, zErrMsg);
164750 rc = sqlite3ApiExit(db, rc);
164751 sqlite3_mutex_leave(db->mutex);
164752 return rc;
164753}
164754
164755/*
164756** Sleep for a little while. Return the amount of time slept.
164757*/
164758SQLITE_API int sqlite3_sleep(int ms){
164759 sqlite3_vfs *pVfs;
164760 int rc;
164761 pVfs = sqlite3_vfs_find(0);
164762 if( pVfs==0 ) return 0;
164763
164764 /* This function works in milliseconds, but the underlying OsSleep()
164765 ** API uses microseconds. Hence the 1000's.
164766 */
164767 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
164768 return rc;
164769}
164770
164771/*
164772** Enable or disable the extended result codes.
164773*/
164774SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
164775#ifdef SQLITE_ENABLE_API_ARMOR
164776 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164777#endif
164778 sqlite3_mutex_enter(db->mutex);
164779 db->errMask = onoff ? 0xffffffff : 0xff;
164780 sqlite3_mutex_leave(db->mutex);
164781 return SQLITE_OK;
164782}
164783
164784/*
164785** Invoke the xFileControl method on a particular database.
164786*/
164787SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
164788 int rc = SQLITE_ERROR;
164789 Btree *pBtree;
164790
164791#ifdef SQLITE_ENABLE_API_ARMOR
164792 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
164793#endif
164794 sqlite3_mutex_enter(db->mutex);
164795 pBtree = sqlite3DbNameToBtree(db, zDbName);
164796 if( pBtree ){
164797 Pager *pPager;
164798 sqlite3_file *fd;
164799 sqlite3BtreeEnter(pBtree);
164800 pPager = sqlite3BtreePager(pBtree);
164801 assert( pPager!=0 );
164802 fd = sqlite3PagerFile(pPager);
164803 assert( fd!=0 );
164804 if( op==SQLITE_FCNTL_FILE_POINTER ){
164805 *(sqlite3_file**)pArg = fd;
164806 rc = SQLITE_OK;
164807 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
164808 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
164809 rc = SQLITE_OK;
164810 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
164811 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
164812 rc = SQLITE_OK;
164813 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
164814 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
164815 rc = SQLITE_OK;
164816 }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){
164817 int iNew = *(int*)pArg;
164818 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
164819 if( iNew>=0 && iNew<=255 ){
164820 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
164821 }
164822 rc = SQLITE_OK;
164823 }else{
164824 rc = sqlite3OsFileControl(fd, op, pArg);
164825 }
164826 sqlite3BtreeLeave(pBtree);
164827 }
164828 sqlite3_mutex_leave(db->mutex);
164829 return rc;
164830}
164831
164832/*
164833** Interface to the testing logic.
164834*/
164835SQLITE_API int sqlite3_test_control(int op, ...){
164836 int rc = 0;
164837#ifdef SQLITE_UNTESTABLE
164838 UNUSED_PARAMETER(op);
164839#else
164840 va_list ap;
164841 va_start(ap, op);
164842 switch( op ){
164843
164844 /*
164845 ** Save the current state of the PRNG.
164846 */
164847 case SQLITE_TESTCTRL_PRNG_SAVE: {
164848 sqlite3PrngSaveState();
164849 break;
164850 }
164851
164852 /*
164853 ** Restore the state of the PRNG to the last state saved using
164854 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
164855 ** this verb acts like PRNG_RESET.
164856 */
164857 case SQLITE_TESTCTRL_PRNG_RESTORE: {
164858 sqlite3PrngRestoreState();
164859 break;
164860 }
164861
164862 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
164863 **
164864 ** Control the seed for the pseudo-random number generator (PRNG) that
164865 ** is built into SQLite. Cases:
164866 **
164867 ** x!=0 && db!=0 Seed the PRNG to the current value of the
164868 ** schema cookie in the main database for db, or
164869 ** x if the schema cookie is zero. This case
164870 ** is convenient to use with database fuzzers
164871 ** as it allows the fuzzer some control over the
164872 ** the PRNG seed.
164873 **
164874 ** x!=0 && db==0 Seed the PRNG to the value of x.
164875 **
164876 ** x==0 && db==0 Revert to default behavior of using the
164877 ** xRandomness method on the primary VFS.
164878 **
164879 ** This test-control also resets the PRNG so that the new seed will
164880 ** be used for the next call to sqlite3_randomness().
164881 */
164882#ifndef SQLITE_OMIT_WSD
164883 case SQLITE_TESTCTRL_PRNG_SEED: {
164884 int x = va_arg(ap, int);
164885 int y;
164886 sqlite3 *db = va_arg(ap, sqlite3*);
164887 assert( db==0 || db->aDb[0].pSchema!=0 );
164888 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
164889 sqlite3Config.iPrngSeed = x;
164890 sqlite3_randomness(0,0);
164891 break;
164892 }
164893#endif
164894
164895 /*
164896 ** sqlite3_test_control(BITVEC_TEST, size, program)
164897 **
164898 ** Run a test against a Bitvec object of size. The program argument
164899 ** is an array of integers that defines the test. Return -1 on a
164900 ** memory allocation error, 0 on success, or non-zero for an error.
164901 ** See the sqlite3BitvecBuiltinTest() for additional information.
164902 */
164903 case SQLITE_TESTCTRL_BITVEC_TEST: {
164904 int sz = va_arg(ap, int);
164905 int *aProg = va_arg(ap, int*);
164906 rc = sqlite3BitvecBuiltinTest(sz, aProg);
164907 break;
164908 }
164909
164910 /*
164911 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
164912 **
164913 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
164914 ** if xCallback is not NULL.
164915 **
164916 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
164917 ** is called immediately after installing the new callback and the return
164918 ** value from sqlite3FaultSim(0) becomes the return from
164919 ** sqlite3_test_control().
164920 */
164921 case SQLITE_TESTCTRL_FAULT_INSTALL: {
164922 /* MSVC is picky about pulling func ptrs from va lists.
164923 ** http://support.microsoft.com/kb/47961
164924 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
164925 */
164926 typedef int(*TESTCALLBACKFUNC_t)(int);
164927 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
164928 rc = sqlite3FaultSim(0);
164929 break;
164930 }
164931
164932 /*
164933 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
164934 **
164935 ** Register hooks to call to indicate which malloc() failures
164936 ** are benign.
164937 */
164938 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
164939 typedef void (*void_function)(void);
164940 void_function xBenignBegin;
164941 void_function xBenignEnd;
164942 xBenignBegin = va_arg(ap, void_function);
164943 xBenignEnd = va_arg(ap, void_function);
164944 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
164945 break;
164946 }
164947
164948 /*
164949 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
164950 **
164951 ** Set the PENDING byte to the value in the argument, if X>0.
164952 ** Make no changes if X==0. Return the value of the pending byte
164953 ** as it existing before this routine was called.
164954 **
164955 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
164956 ** an incompatible database file format. Changing the PENDING byte
164957 ** while any database connection is open results in undefined and
164958 ** deleterious behavior.
164959 */
164960 case SQLITE_TESTCTRL_PENDING_BYTE: {
164961 rc = PENDING_BYTE;
164962#ifndef SQLITE_OMIT_WSD
164963 {
164964 unsigned int newVal = va_arg(ap, unsigned int);
164965 if( newVal ) sqlite3PendingByte = newVal;
164966 }
164967#endif
164968 break;
164969 }
164970
164971 /*
164972 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
164973 **
164974 ** This action provides a run-time test to see whether or not
164975 ** assert() was enabled at compile-time. If X is true and assert()
164976 ** is enabled, then the return value is true. If X is true and
164977 ** assert() is disabled, then the return value is zero. If X is
164978 ** false and assert() is enabled, then the assertion fires and the
164979 ** process aborts. If X is false and assert() is disabled, then the
164980 ** return value is zero.
164981 */
164982 case SQLITE_TESTCTRL_ASSERT: {
164983 volatile int x = 0;
164984 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
164985 rc = x;
164986 break;
164987 }
164988
164989
164990 /*
164991 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
164992 **
164993 ** This action provides a run-time test to see how the ALWAYS and
164994 ** NEVER macros were defined at compile-time.
164995 **
164996 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
164997 **
164998 ** The recommended test is X==2. If the return value is 2, that means
164999 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
165000 ** default setting. If the return value is 1, then ALWAYS() is either
165001 ** hard-coded to true or else it asserts if its argument is false.
165002 ** The first behavior (hard-coded to true) is the case if
165003 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
165004 ** behavior (assert if the argument to ALWAYS() is false) is the case if
165005 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
165006 **
165007 ** The run-time test procedure might look something like this:
165008 **
165009 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
165010 ** // ALWAYS() and NEVER() are no-op pass-through macros
165011 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
165012 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
165013 ** }else{
165014 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
165015 ** }
165016 */
165017 case SQLITE_TESTCTRL_ALWAYS: {
165018 int x = va_arg(ap,int);
165019 rc = x ? ALWAYS(x) : 0;
165020 break;
165021 }
165022
165023 /*
165024 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
165025 **
165026 ** The integer returned reveals the byte-order of the computer on which
165027 ** SQLite is running:
165028 **
165029 ** 1 big-endian, determined at run-time
165030 ** 10 little-endian, determined at run-time
165031 ** 432101 big-endian, determined at compile-time
165032 ** 123410 little-endian, determined at compile-time
165033 */
165034 case SQLITE_TESTCTRL_BYTEORDER: {
165035 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
165036 break;
165037 }
165038
165039 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
165040 **
165041 ** Enable or disable various optimizations for testing purposes. The
165042 ** argument N is a bitmask of optimizations to be disabled. For normal
165043 ** operation N should be 0. The idea is that a test program (like the
165044 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
165045 ** with various optimizations disabled to verify that the same answer
165046 ** is obtained in every case.
165047 */
165048 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
165049 sqlite3 *db = va_arg(ap, sqlite3*);
165050 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
165051 break;
165052 }
165053
165054 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
165055 **
165056 ** If parameter onoff is non-zero, subsequent calls to localtime()
165057 ** and its variants fail. If onoff is zero, undo this setting.
165058 */
165059 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
165060 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
165061 break;
165062 }
165063
165064 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
165065 **
165066 ** Toggle the ability to use internal functions on or off for
165067 ** the database connection given in the argument.
165068 */
165069 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
165070 sqlite3 *db = va_arg(ap, sqlite3*);
165071 db->mDbFlags ^= DBFLAG_InternalFunc;
165072 break;
165073 }
165074
165075 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
165076 **
165077 ** Set or clear a flag that indicates that the database file is always well-
165078 ** formed and never corrupt. This flag is clear by default, indicating that
165079 ** database files might have arbitrary corruption. Setting the flag during
165080 ** testing causes certain assert() statements in the code to be activated
165081 ** that demonstrat invariants on well-formed database files.
165082 */
165083 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
165084 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
165085 break;
165086 }
165087
165088 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
165089 **
165090 ** Set or clear a flag that causes SQLite to verify that type, name,
165091 ** and tbl_name fields of the sqlite_schema table. This is normally
165092 ** on, but it is sometimes useful to turn it off for testing.
165093 **
165094 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
165095 ** verification of rootpage numbers when parsing the schema. This
165096 ** is useful to make it easier to reach strange internal error states
165097 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
165098 ** in production.
165099 */
165100 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
165101 sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
165102 break;
165103 }
165104
165105 /* Set the threshold at which OP_Once counters reset back to zero.
165106 ** By default this is 0x7ffffffe (over 2 billion), but that value is
165107 ** too big to test in a reasonable amount of time, so this control is
165108 ** provided to set a small and easily reachable reset value.
165109 */
165110 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
165111 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
165112 break;
165113 }
165114
165115 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
165116 **
165117 ** Set the VDBE coverage callback function to xCallback with context
165118 ** pointer ptr.
165119 */
165120 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
165121#ifdef SQLITE_VDBE_COVERAGE
165122 typedef void (*branch_callback)(void*,unsigned int,
165123 unsigned char,unsigned char);
165124 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
165125 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
165126#endif
165127 break;
165128 }
165129
165130 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
165131 case SQLITE_TESTCTRL_SORTER_MMAP: {
165132 sqlite3 *db = va_arg(ap, sqlite3*);
165133 db->nMaxSorterMmap = va_arg(ap, int);
165134 break;
165135 }
165136
165137 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
165138 **
165139 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
165140 ** not.
165141 */
165142 case SQLITE_TESTCTRL_ISINIT: {
165143 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
165144 break;
165145 }
165146
165147 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
165148 **
165149 ** This test control is used to create imposter tables. "db" is a pointer
165150 ** to the database connection. dbName is the database name (ex: "main" or
165151 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
165152 ** or off. "tnum" is the root page of the b-tree to which the imposter
165153 ** table should connect.
165154 **
165155 ** Enable imposter mode only when the schema has already been parsed. Then
165156 ** run a single CREATE TABLE statement to construct the imposter table in
165157 ** the parsed schema. Then turn imposter mode back off again.
165158 **
165159 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
165160 ** the schema to be reparsed the next time it is needed. This has the
165161 ** effect of erasing all imposter tables.
165162 */
165163 case SQLITE_TESTCTRL_IMPOSTER: {
165164 sqlite3 *db = va_arg(ap, sqlite3*);
165165 sqlite3_mutex_enter(db->mutex);
165166 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
165167 db->init.busy = db->init.imposterTable = va_arg(ap,int);
165168 db->init.newTnum = va_arg(ap,int);
165169 if( db->init.busy==0 && db->init.newTnum>0 ){
165170 sqlite3ResetAllSchemasOfConnection(db);
165171 }
165172 sqlite3_mutex_leave(db->mutex);
165173 break;
165174 }
165175
165176#if defined(YYCOVERAGE)
165177 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
165178 **
165179 ** This test control (only available when SQLite is compiled with
165180 ** -DYYCOVERAGE) writes a report onto "out" that shows all
165181 ** state/lookahead combinations in the parser state machine
165182 ** which are never exercised. If any state is missed, make the
165183 ** return code SQLITE_ERROR.
165184 */
165185 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
165186 FILE *out = va_arg(ap, FILE*);
165187 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
165188 break;
165189 }
165190#endif /* defined(YYCOVERAGE) */
165191
165192 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
165193 **
165194 ** This test-control causes the most recent sqlite3_result_int64() value
165195 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
165196 ** MEM_IntReal values only arise during an INSERT operation of integer
165197 ** values into a REAL column, so they can be challenging to test. This
165198 ** test-control enables us to write an intreal() SQL function that can
165199 ** inject an intreal() value at arbitrary places in an SQL statement,
165200 ** for testing purposes.
165201 */
165202 case SQLITE_TESTCTRL_RESULT_INTREAL: {
165203 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
165204 sqlite3ResultIntReal(pCtx);
165205 break;
165206 }
165207 }
165208 va_end(ap);
165209#endif /* SQLITE_UNTESTABLE */
165210 return rc;
165211}
165212
165213/*
165214** The Pager stores the Database filename, Journal filename, and WAL filename
165215** consecutively in memory, in that order. The database filename is prefixed
165216** by four zero bytes. Locate the start of the database filename by searching
165217** backwards for the first byte following four consecutive zero bytes.
165218**
165219** This only works if the filename passed in was obtained from the Pager.
165220*/
165221static const char *databaseName(const char *zName){
165222 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
165223 zName--;
165224 }
165225 return zName;
165226}
165227
165228/*
165229** Append text z[] to the end of p[]. Return a pointer to the first
165230** character after then zero terminator on the new text in p[].
165231*/
165232static char *appendText(char *p, const char *z){
165233 size_t n = strlen(z);
165234 memcpy(p, z, n+1);
165235 return p+n+1;
165236}
165237
165238/*
165239** Allocate memory to hold names for a database, journal file, WAL file,
165240** and query parameters. The pointer returned is valid for use by
165241** sqlite3_filename_database() and sqlite3_uri_parameter() and related
165242** functions.
165243**
165244** Memory layout must be compatible with that generated by the pager
165245** and expected by sqlite3_uri_parameter() and databaseName().
165246*/
165247SQLITE_API char *sqlite3_create_filename(
165248 const char *zDatabase,
165249 const char *zJournal,
165250 const char *zWal,
165251 int nParam,
165252 const char **azParam
165253){
165254 sqlite3_int64 nByte;
165255 int i;
165256 char *pResult, *p;
165257 nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
165258 for(i=0; i<nParam*2; i++){
165259 nByte += strlen(azParam[i])+1;
165260 }
165261 pResult = p = sqlite3_malloc64( nByte );
165262 if( p==0 ) return 0;
165263 memset(p, 0, 4);
165264 p += 4;
165265 p = appendText(p, zDatabase);
165266 for(i=0; i<nParam*2; i++){
165267 p = appendText(p, azParam[i]);
165268 }
165269 *(p++) = 0;
165270 p = appendText(p, zJournal);
165271 p = appendText(p, zWal);
165272 *(p++) = 0;
165273 *(p++) = 0;
165274 assert( (sqlite3_int64)(p - pResult)==nByte );
165275 return pResult + 4;
165276}
165277
165278/*
165279** Free memory obtained from sqlite3_create_filename(). It is a severe
165280** error to call this routine with any parameter other than a pointer
165281** previously obtained from sqlite3_create_filename() or a NULL pointer.
165282*/
165283SQLITE_API void sqlite3_free_filename(char *p){
165284 if( p==0 ) return;
165285 p = (char*)databaseName(p);
165286 sqlite3_free(p - 4);
165287}
165288
165289
165290/*
165291** This is a utility routine, useful to VFS implementations, that checks
165292** to see if a database file was a URI that contained a specific query
165293** parameter, and if so obtains the value of the query parameter.
165294**
165295** The zFilename argument is the filename pointer passed into the xOpen()
165296** method of a VFS implementation. The zParam argument is the name of the
165297** query parameter we seek. This routine returns the value of the zParam
165298** parameter if it exists. If the parameter does not exist, this routine
165299** returns a NULL pointer.
165300*/
165301SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
165302 if( zFilename==0 || zParam==0 ) return 0;
165303 zFilename = databaseName(zFilename);
165304 return uriParameter(zFilename, zParam);
165305}
165306
165307/*
165308** Return a pointer to the name of Nth query parameter of the filename.
165309*/
165310SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
165311 if( zFilename==0 || N<0 ) return 0;
165312 zFilename = databaseName(zFilename);
165313 zFilename += sqlite3Strlen30(zFilename) + 1;
165314 while( zFilename[0] && (N--)>0 ){
165315 zFilename += sqlite3Strlen30(zFilename) + 1;
165316 zFilename += sqlite3Strlen30(zFilename) + 1;
165317 }
165318 return zFilename[0] ? zFilename : 0;
165319}
165320
165321/*
165322** Return a boolean value for a query parameter.
165323*/
165324SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
165325 const char *z = sqlite3_uri_parameter(zFilename, zParam);
165326 bDflt = bDflt!=0;
165327 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
165328}
165329
165330/*
165331** Return a 64-bit integer value for a query parameter.
165332*/
165333SQLITE_API sqlite3_int64 sqlite3_uri_int64(
165334 const char *zFilename, /* Filename as passed to xOpen */
165335 const char *zParam, /* URI parameter sought */
165336 sqlite3_int64 bDflt /* return if parameter is missing */
165337){
165338 const char *z = sqlite3_uri_parameter(zFilename, zParam);
165339 sqlite3_int64 v;
165340 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
165341 bDflt = v;
165342 }
165343 return bDflt;
165344}
165345
165346/*
165347** Translate a filename that was handed to a VFS routine into the corresponding
165348** database, journal, or WAL file.
165349**
165350** It is an error to pass this routine a filename string that was not
165351** passed into the VFS from the SQLite core. Doing so is similar to
165352** passing free() a pointer that was not obtained from malloc() - it is
165353** an error that we cannot easily detect but that will likely cause memory
165354** corruption.
165355*/
165356SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
165357 return databaseName(zFilename);
165358}
165359SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
165360 zFilename = databaseName(zFilename);
165361 zFilename += sqlite3Strlen30(zFilename) + 1;
165362 while( zFilename[0] ){
165363 zFilename += sqlite3Strlen30(zFilename) + 1;
165364 zFilename += sqlite3Strlen30(zFilename) + 1;
165365 }
165366 return zFilename + 1;
165367}
165368SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
165369#ifdef SQLITE_OMIT_WAL
165370 return 0;
165371#else
165372 zFilename = sqlite3_filename_journal(zFilename);
165373 zFilename += sqlite3Strlen30(zFilename) + 1;
165374 return zFilename;
165375#endif
165376}
165377
165378/*
165379** Return the Btree pointer identified by zDbName. Return NULL if not found.
165380*/
165381SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
165382 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
165383 return iDb<0 ? 0 : db->aDb[iDb].pBt;
165384}
165385
165386/*
165387** Return the filename of the database associated with a database
165388** connection.
165389*/
165390SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
165391 Btree *pBt;
165392#ifdef SQLITE_ENABLE_API_ARMOR
165393 if( !sqlite3SafetyCheckOk(db) ){
165394 (void)SQLITE_MISUSE_BKPT;
165395 return 0;
165396 }
165397#endif
165398 pBt = sqlite3DbNameToBtree(db, zDbName);
165399 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
165400}
165401
165402/*
165403** Return 1 if database is read-only or 0 if read/write. Return -1 if
165404** no such database exists.
165405*/
165406SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
165407 Btree *pBt;
165408#ifdef SQLITE_ENABLE_API_ARMOR
165409 if( !sqlite3SafetyCheckOk(db) ){
165410 (void)SQLITE_MISUSE_BKPT;
165411 return -1;
165412 }
165413#endif
165414 pBt = sqlite3DbNameToBtree(db, zDbName);
165415 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
165416}
165417
165418#ifdef SQLITE_ENABLE_SNAPSHOT
165419/*
165420** Obtain a snapshot handle for the snapshot of database zDb currently
165421** being read by handle db.
165422*/
165423SQLITE_API int sqlite3_snapshot_get(
165424 sqlite3 *db,
165425 const char *zDb,
165426 sqlite3_snapshot **ppSnapshot
165427){
165428 int rc = SQLITE_ERROR;
165429#ifndef SQLITE_OMIT_WAL
165430
165431#ifdef SQLITE_ENABLE_API_ARMOR
165432 if( !sqlite3SafetyCheckOk(db) ){
165433 return SQLITE_MISUSE_BKPT;
165434 }
165435#endif
165436 sqlite3_mutex_enter(db->mutex);
165437
165438 if( db->autoCommit==0 ){
165439 int iDb = sqlite3FindDbName(db, zDb);
165440 if( iDb==0 || iDb>1 ){
165441 Btree *pBt = db->aDb[iDb].pBt;
165442 if( 0==sqlite3BtreeIsInTrans(pBt) ){
165443 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
165444 if( rc==SQLITE_OK ){
165445 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
165446 }
165447 }
165448 }
165449 }
165450
165451 sqlite3_mutex_leave(db->mutex);
165452#endif /* SQLITE_OMIT_WAL */
165453 return rc;
165454}
165455
165456/*
165457** Open a read-transaction on the snapshot idendified by pSnapshot.
165458*/
165459SQLITE_API int sqlite3_snapshot_open(
165460 sqlite3 *db,
165461 const char *zDb,
165462 sqlite3_snapshot *pSnapshot
165463){
165464 int rc = SQLITE_ERROR;
165465#ifndef SQLITE_OMIT_WAL
165466
165467#ifdef SQLITE_ENABLE_API_ARMOR
165468 if( !sqlite3SafetyCheckOk(db) ){
165469 return SQLITE_MISUSE_BKPT;
165470 }
165471#endif
165472 sqlite3_mutex_enter(db->mutex);
165473 if( db->autoCommit==0 ){
165474 int iDb;
165475 iDb = sqlite3FindDbName(db, zDb);
165476 if( iDb==0 || iDb>1 ){
165477 Btree *pBt = db->aDb[iDb].pBt;
165478 if( sqlite3BtreeIsInTrans(pBt)==0 ){
165479 Pager *pPager = sqlite3BtreePager(pBt);
165480 int bUnlock = 0;
165481 if( sqlite3BtreeIsInReadTrans(pBt) ){
165482 if( db->nVdbeActive==0 ){
165483 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
165484 if( rc==SQLITE_OK ){
165485 bUnlock = 1;
165486 rc = sqlite3BtreeCommit(pBt);
165487 }
165488 }
165489 }else{
165490 rc = SQLITE_OK;
165491 }
165492 if( rc==SQLITE_OK ){
165493 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
165494 }
165495 if( rc==SQLITE_OK ){
165496 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
165497 sqlite3PagerSnapshotOpen(pPager, 0);
165498 }
165499 if( bUnlock ){
165500 sqlite3PagerSnapshotUnlock(pPager);
165501 }
165502 }
165503 }
165504 }
165505
165506 sqlite3_mutex_leave(db->mutex);
165507#endif /* SQLITE_OMIT_WAL */
165508 return rc;
165509}
165510
165511/*
165512** Recover as many snapshots as possible from the wal file associated with
165513** schema zDb of database db.
165514*/
165515SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
165516 int rc = SQLITE_ERROR;
165517 int iDb;
165518#ifndef SQLITE_OMIT_WAL
165519
165520#ifdef SQLITE_ENABLE_API_ARMOR
165521 if( !sqlite3SafetyCheckOk(db) ){
165522 return SQLITE_MISUSE_BKPT;
165523 }
165524#endif
165525
165526 sqlite3_mutex_enter(db->mutex);
165527 iDb = sqlite3FindDbName(db, zDb);
165528 if( iDb==0 || iDb>1 ){
165529 Btree *pBt = db->aDb[iDb].pBt;
165530 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
165531 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
165532 if( rc==SQLITE_OK ){
165533 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
165534 sqlite3BtreeCommit(pBt);
165535 }
165536 }
165537 }
165538 sqlite3_mutex_leave(db->mutex);
165539#endif /* SQLITE_OMIT_WAL */
165540 return rc;
165541}
165542
165543/*
165544** Free a snapshot handle obtained from sqlite3_snapshot_get().
165545*/
165546SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
165547 sqlite3_free(pSnapshot);
165548}
165549#endif /* SQLITE_ENABLE_SNAPSHOT */
165550
165551#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
165552/*
165553** Given the name of a compile-time option, return true if that option
165554** was used and false if not.
165555**
165556** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
165557** is not required for a match.
165558*/
165559SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
165560 int i, n;
165561 int nOpt;
165562 const char **azCompileOpt;
165563
165564#if SQLITE_ENABLE_API_ARMOR
165565 if( zOptName==0 ){
165566 (void)SQLITE_MISUSE_BKPT;
165567 return 0;
165568 }
165569#endif
165570
165571 azCompileOpt = sqlite3CompileOptions(&nOpt);
165572
165573 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
165574 n = sqlite3Strlen30(zOptName);
165575
165576 /* Since nOpt is normally in single digits, a linear search is
165577 ** adequate. No need for a binary search. */
165578 for(i=0; i<nOpt; i++){
165579 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
165580 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
165581 ){
165582 return 1;
165583 }
165584 }
165585 return 0;
165586}
165587
165588/*
165589** Return the N-th compile-time option string. If N is out of range,
165590** return a NULL pointer.
165591*/
165592SQLITE_API const char *sqlite3_compileoption_get(int N){
165593 int nOpt;
165594 const char **azCompileOpt;
165595 azCompileOpt = sqlite3CompileOptions(&nOpt);
165596 if( N>=0 && N<nOpt ){
165597 return azCompileOpt[N];
165598 }
165599 return 0;
165600}
165601#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
165602
165603/************** End of main.c ************************************************/
165604/************** Begin file notify.c ******************************************/
165605/*
165606** 2009 March 3
165607**
165608** The author disclaims copyright to this source code. In place of
165609** a legal notice, here is a blessing:
165610**
165611** May you do good and not evil.
165612** May you find forgiveness for yourself and forgive others.
165613** May you share freely, never taking more than you give.
165614**
165615*************************************************************************
165616**
165617** This file contains the implementation of the sqlite3_unlock_notify()
165618** API method and its associated functionality.
165619*/
165620/* #include "sqliteInt.h" */
165621/* #include "btreeInt.h" */
165622
165623/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
165624#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
165625
165626/*
165627** Public interfaces:
165628**
165629** sqlite3ConnectionBlocked()
165630** sqlite3ConnectionUnlocked()
165631** sqlite3ConnectionClosed()
165632** sqlite3_unlock_notify()
165633*/
165634
165635#define assertMutexHeld() \
165636 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )
165637
165638/*
165639** Head of a linked list of all sqlite3 objects created by this process
165640** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
165641** is not NULL. This variable may only accessed while the STATIC_MAIN
165642** mutex is held.
165643*/
165644static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
165645
165646#ifndef NDEBUG
165647/*
165648** This function is a complex assert() that verifies the following
165649** properties of the blocked connections list:
165650**
165651** 1) Each entry in the list has a non-NULL value for either
165652** pUnlockConnection or pBlockingConnection, or both.
165653**
165654** 2) All entries in the list that share a common value for
165655** xUnlockNotify are grouped together.
165656**
165657** 3) If the argument db is not NULL, then none of the entries in the
165658** blocked connections list have pUnlockConnection or pBlockingConnection
165659** set to db. This is used when closing connection db.
165660*/
165661static void checkListProperties(sqlite3 *db){
165662 sqlite3 *p;
165663 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
165664 int seen = 0;
165665 sqlite3 *p2;
165666
165667 /* Verify property (1) */
165668 assert( p->pUnlockConnection || p->pBlockingConnection );
165669
165670 /* Verify property (2) */
165671 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
165672 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
165673 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
165674 assert( db==0 || p->pUnlockConnection!=db );
165675 assert( db==0 || p->pBlockingConnection!=db );
165676 }
165677 }
165678}
165679#else
165680# define checkListProperties(x)
165681#endif
165682
165683/*
165684** Remove connection db from the blocked connections list. If connection
165685** db is not currently a part of the list, this function is a no-op.
165686*/
165687static void removeFromBlockedList(sqlite3 *db){
165688 sqlite3 **pp;
165689 assertMutexHeld();
165690 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
165691 if( *pp==db ){
165692 *pp = (*pp)->pNextBlocked;
165693 break;
165694 }
165695 }
165696}
165697
165698/*
165699** Add connection db to the blocked connections list. It is assumed
165700** that it is not already a part of the list.
165701*/
165702static void addToBlockedList(sqlite3 *db){
165703 sqlite3 **pp;
165704 assertMutexHeld();
165705 for(
165706 pp=&sqlite3BlockedList;
165707 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
165708 pp=&(*pp)->pNextBlocked
165709 );
165710 db->pNextBlocked = *pp;
165711 *pp = db;
165712}
165713
165714/*
165715** Obtain the STATIC_MAIN mutex.
165716*/
165717static void enterMutex(void){
165718 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
165719 checkListProperties(0);
165720}
165721
165722/*
165723** Release the STATIC_MAIN mutex.
165724*/
165725static void leaveMutex(void){
165726 assertMutexHeld();
165727 checkListProperties(0);
165728 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN));
165729}
165730
165731/*
165732** Register an unlock-notify callback.
165733**
165734** This is called after connection "db" has attempted some operation
165735** but has received an SQLITE_LOCKED error because another connection
165736** (call it pOther) in the same process was busy using the same shared
165737** cache. pOther is found by looking at db->pBlockingConnection.
165738**
165739** If there is no blocking connection, the callback is invoked immediately,
165740** before this routine returns.
165741**
165742** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
165743** a deadlock.
165744**
165745** Otherwise, make arrangements to invoke xNotify when pOther drops
165746** its locks.
165747**
165748** Each call to this routine overrides any prior callbacks registered
165749** on the same "db". If xNotify==0 then any prior callbacks are immediately
165750** cancelled.
165751*/
165752SQLITE_API int sqlite3_unlock_notify(
165753 sqlite3 *db,
165754 void (*xNotify)(void **, int),
165755 void *pArg
165756){
165757 int rc = SQLITE_OK;
165758
165759 sqlite3_mutex_enter(db->mutex);
165760 enterMutex();
165761
165762 if( xNotify==0 ){
165763 removeFromBlockedList(db);
165764 db->pBlockingConnection = 0;
165765 db->pUnlockConnection = 0;
165766 db->xUnlockNotify = 0;
165767 db->pUnlockArg = 0;
165768 }else if( 0==db->pBlockingConnection ){
165769 /* The blocking transaction has been concluded. Or there never was a
165770 ** blocking transaction. In either case, invoke the notify callback
165771 ** immediately.
165772 */
165773 xNotify(&pArg, 1);
165774 }else{
165775 sqlite3 *p;
165776
165777 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
165778 if( p ){
165779 rc = SQLITE_LOCKED; /* Deadlock detected. */
165780 }else{
165781 db->pUnlockConnection = db->pBlockingConnection;
165782 db->xUnlockNotify = xNotify;
165783 db->pUnlockArg = pArg;
165784 removeFromBlockedList(db);
165785 addToBlockedList(db);
165786 }
165787 }
165788
165789 leaveMutex();
165790 assert( !db->mallocFailed );
165791 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
165792 sqlite3_mutex_leave(db->mutex);
165793 return rc;
165794}
165795
165796/*
165797** This function is called while stepping or preparing a statement
165798** associated with connection db. The operation will return SQLITE_LOCKED
165799** to the user because it requires a lock that will not be available
165800** until connection pBlocker concludes its current transaction.
165801*/
165802SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
165803 enterMutex();
165804 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
165805 addToBlockedList(db);
165806 }
165807 db->pBlockingConnection = pBlocker;
165808 leaveMutex();
165809}
165810
165811/*
165812** This function is called when
165813** the transaction opened by database db has just finished. Locks held
165814** by database connection db have been released.
165815**
165816** This function loops through each entry in the blocked connections
165817** list and does the following:
165818**
165819** 1) If the sqlite3.pBlockingConnection member of a list entry is
165820** set to db, then set pBlockingConnection=0.
165821**
165822** 2) If the sqlite3.pUnlockConnection member of a list entry is
165823** set to db, then invoke the configured unlock-notify callback and
165824** set pUnlockConnection=0.
165825**
165826** 3) If the two steps above mean that pBlockingConnection==0 and
165827** pUnlockConnection==0, remove the entry from the blocked connections
165828** list.
165829*/
165830SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
165831 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
165832 int nArg = 0; /* Number of entries in aArg[] */
165833 sqlite3 **pp; /* Iterator variable */
165834 void **aArg; /* Arguments to the unlock callback */
165835 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
165836 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
165837
165838 aArg = aStatic;
165839 enterMutex(); /* Enter STATIC_MAIN mutex */
165840
165841 /* This loop runs once for each entry in the blocked-connections list. */
165842 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
165843 sqlite3 *p = *pp;
165844
165845 /* Step 1. */
165846 if( p->pBlockingConnection==db ){
165847 p->pBlockingConnection = 0;
165848 }
165849
165850 /* Step 2. */
165851 if( p->pUnlockConnection==db ){
165852 assert( p->xUnlockNotify );
165853 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
165854 xUnlockNotify(aArg, nArg);
165855 nArg = 0;
165856 }
165857
165858 sqlite3BeginBenignMalloc();
165859 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
165860 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
165861 if( (!aDyn && nArg==(int)ArraySize(aStatic))
165862 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
165863 ){
165864 /* The aArg[] array needs to grow. */
165865 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
165866 if( pNew ){
165867 memcpy(pNew, aArg, nArg*sizeof(void *));
165868 sqlite3_free(aDyn);
165869 aDyn = aArg = pNew;
165870 }else{
165871 /* This occurs when the array of context pointers that need to
165872 ** be passed to the unlock-notify callback is larger than the
165873 ** aStatic[] array allocated on the stack and the attempt to
165874 ** allocate a larger array from the heap has failed.
165875 **
165876 ** This is a difficult situation to handle. Returning an error
165877 ** code to the caller is insufficient, as even if an error code
165878 ** is returned the transaction on connection db will still be
165879 ** closed and the unlock-notify callbacks on blocked connections
165880 ** will go unissued. This might cause the application to wait
165881 ** indefinitely for an unlock-notify callback that will never
165882 ** arrive.
165883 **
165884 ** Instead, invoke the unlock-notify callback with the context
165885 ** array already accumulated. We can then clear the array and
165886 ** begin accumulating any further context pointers without
165887 ** requiring any dynamic allocation. This is sub-optimal because
165888 ** it means that instead of one callback with a large array of
165889 ** context pointers the application will receive two or more
165890 ** callbacks with smaller arrays of context pointers, which will
165891 ** reduce the applications ability to prioritize multiple
165892 ** connections. But it is the best that can be done under the
165893 ** circumstances.
165894 */
165895 xUnlockNotify(aArg, nArg);
165896 nArg = 0;
165897 }
165898 }
165899 sqlite3EndBenignMalloc();
165900
165901 aArg[nArg++] = p->pUnlockArg;
165902 xUnlockNotify = p->xUnlockNotify;
165903 p->pUnlockConnection = 0;
165904 p->xUnlockNotify = 0;
165905 p->pUnlockArg = 0;
165906 }
165907
165908 /* Step 3. */
165909 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
165910 /* Remove connection p from the blocked connections list. */
165911 *pp = p->pNextBlocked;
165912 p->pNextBlocked = 0;
165913 }else{
165914 pp = &p->pNextBlocked;
165915 }
165916 }
165917
165918 if( nArg!=0 ){
165919 xUnlockNotify(aArg, nArg);
165920 }
165921 sqlite3_free(aDyn);
165922 leaveMutex(); /* Leave STATIC_MAIN mutex */
165923}
165924
165925/*
165926** This is called when the database connection passed as an argument is
165927** being closed. The connection is removed from the blocked list.
165928*/
165929SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
165930 sqlite3ConnectionUnlocked(db);
165931 enterMutex();
165932 removeFromBlockedList(db);
165933 checkListProperties(db);
165934 leaveMutex();
165935}
165936#endif
165937
165938/************** End of notify.c **********************************************/
165939/************** Begin file fts3.c ********************************************/
165940/*
165941** 2006 Oct 10
165942**
165943** The author disclaims copyright to this source code. In place of
165944** a legal notice, here is a blessing:
165945**
165946** May you do good and not evil.
165947** May you find forgiveness for yourself and forgive others.
165948** May you share freely, never taking more than you give.
165949**
165950******************************************************************************
165951**
165952** This is an SQLite module implementing full-text search.
165953*/
165954
165955/*
165956** The code in this file is only compiled if:
165957**
165958** * The FTS3 module is being built as an extension
165959** (in which case SQLITE_CORE is not defined), or
165960**
165961** * The FTS3 module is being built into the core of
165962** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
165963*/
165964
165965/* The full-text index is stored in a series of b+tree (-like)
165966** structures called segments which map terms to doclists. The
165967** structures are like b+trees in layout, but are constructed from the
165968** bottom up in optimal fashion and are not updatable. Since trees
165969** are built from the bottom up, things will be described from the
165970** bottom up.
165971**
165972**
165973**** Varints ****
165974** The basic unit of encoding is a variable-length integer called a
165975** varint. We encode variable-length integers in little-endian order
165976** using seven bits * per byte as follows:
165977**
165978** KEY:
165979** A = 0xxxxxxx 7 bits of data and one flag bit
165980** B = 1xxxxxxx 7 bits of data and one flag bit
165981**
165982** 7 bits - A
165983** 14 bits - BA
165984** 21 bits - BBA
165985** and so on.
165986**
165987** This is similar in concept to how sqlite encodes "varints" but
165988** the encoding is not the same. SQLite varints are big-endian
165989** are are limited to 9 bytes in length whereas FTS3 varints are
165990** little-endian and can be up to 10 bytes in length (in theory).
165991**
165992** Example encodings:
165993**
165994** 1: 0x01
165995** 127: 0x7f
165996** 128: 0x81 0x00
165997**
165998**
165999**** Document lists ****
166000** A doclist (document list) holds a docid-sorted list of hits for a
166001** given term. Doclists hold docids and associated token positions.
166002** A docid is the unique integer identifier for a single document.
166003** A position is the index of a word within the document. The first
166004** word of the document has a position of 0.
166005**
166006** FTS3 used to optionally store character offsets using a compile-time
166007** option. But that functionality is no longer supported.
166008**
166009** A doclist is stored like this:
166010**
166011** array {
166012** varint docid; (delta from previous doclist)
166013** array { (position list for column 0)
166014** varint position; (2 more than the delta from previous position)
166015** }
166016** array {
166017** varint POS_COLUMN; (marks start of position list for new column)
166018** varint column; (index of new column)
166019** array {
166020** varint position; (2 more than the delta from previous position)
166021** }
166022** }
166023** varint POS_END; (marks end of positions for this document.
166024** }
166025**
166026** Here, array { X } means zero or more occurrences of X, adjacent in
166027** memory. A "position" is an index of a token in the token stream
166028** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
166029** in the same logical place as the position element, and act as sentinals
166030** ending a position list array. POS_END is 0. POS_COLUMN is 1.
166031** The positions numbers are not stored literally but rather as two more
166032** than the difference from the prior position, or the just the position plus
166033** 2 for the first position. Example:
166034**
166035** label: A B C D E F G H I J K
166036** value: 123 5 9 1 1 14 35 0 234 72 0
166037**
166038** The 123 value is the first docid. For column zero in this document
166039** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
166040** at D signals the start of a new column; the 1 at E indicates that the
166041** new column is column number 1. There are two positions at 12 and 45
166042** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
166043** 234 at I is the delta to next docid (357). It has one position 70
166044** (72-2) and then terminates with the 0 at K.
166045**
166046** A "position-list" is the list of positions for multiple columns for
166047** a single docid. A "column-list" is the set of positions for a single
166048** column. Hence, a position-list consists of one or more column-lists,
166049** a document record consists of a docid followed by a position-list and
166050** a doclist consists of one or more document records.
166051**
166052** A bare doclist omits the position information, becoming an
166053** array of varint-encoded docids.
166054**
166055**** Segment leaf nodes ****
166056** Segment leaf nodes store terms and doclists, ordered by term. Leaf
166057** nodes are written using LeafWriter, and read using LeafReader (to
166058** iterate through a single leaf node's data) and LeavesReader (to
166059** iterate through a segment's entire leaf layer). Leaf nodes have
166060** the format:
166061**
166062** varint iHeight; (height from leaf level, always 0)
166063** varint nTerm; (length of first term)
166064** char pTerm[nTerm]; (content of first term)
166065** varint nDoclist; (length of term's associated doclist)
166066** char pDoclist[nDoclist]; (content of doclist)
166067** array {
166068** (further terms are delta-encoded)
166069** varint nPrefix; (length of prefix shared with previous term)
166070** varint nSuffix; (length of unshared suffix)
166071** char pTermSuffix[nSuffix];(unshared suffix of next term)
166072** varint nDoclist; (length of term's associated doclist)
166073** char pDoclist[nDoclist]; (content of doclist)
166074** }
166075**
166076** Here, array { X } means zero or more occurrences of X, adjacent in
166077** memory.
166078**
166079** Leaf nodes are broken into blocks which are stored contiguously in
166080** the %_segments table in sorted order. This means that when the end
166081** of a node is reached, the next term is in the node with the next
166082** greater node id.
166083**
166084** New data is spilled to a new leaf node when the current node
166085** exceeds LEAF_MAX bytes (default 2048). New data which itself is
166086** larger than STANDALONE_MIN (default 1024) is placed in a standalone
166087** node (a leaf node with a single term and doclist). The goal of
166088** these settings is to pack together groups of small doclists while
166089** making it efficient to directly access large doclists. The
166090** assumption is that large doclists represent terms which are more
166091** likely to be query targets.
166092**
166093** TODO(shess) It may be useful for blocking decisions to be more
166094** dynamic. For instance, it may make more sense to have a 2.5k leaf
166095** node rather than splitting into 2k and .5k nodes. My intuition is
166096** that this might extend through 2x or 4x the pagesize.
166097**
166098**
166099**** Segment interior nodes ****
166100** Segment interior nodes store blockids for subtree nodes and terms
166101** to describe what data is stored by the each subtree. Interior
166102** nodes are written using InteriorWriter, and read using
166103** InteriorReader. InteriorWriters are created as needed when
166104** SegmentWriter creates new leaf nodes, or when an interior node
166105** itself grows too big and must be split. The format of interior
166106** nodes:
166107**
166108** varint iHeight; (height from leaf level, always >0)
166109** varint iBlockid; (block id of node's leftmost subtree)
166110** optional {
166111** varint nTerm; (length of first term)
166112** char pTerm[nTerm]; (content of first term)
166113** array {
166114** (further terms are delta-encoded)
166115** varint nPrefix; (length of shared prefix with previous term)
166116** varint nSuffix; (length of unshared suffix)
166117** char pTermSuffix[nSuffix]; (unshared suffix of next term)
166118** }
166119** }
166120**
166121** Here, optional { X } means an optional element, while array { X }
166122** means zero or more occurrences of X, adjacent in memory.
166123**
166124** An interior node encodes n terms separating n+1 subtrees. The
166125** subtree blocks are contiguous, so only the first subtree's blockid
166126** is encoded. The subtree at iBlockid will contain all terms less
166127** than the first term encoded (or all terms if no term is encoded).
166128** Otherwise, for terms greater than or equal to pTerm[i] but less
166129** than pTerm[i+1], the subtree for that term will be rooted at
166130** iBlockid+i. Interior nodes only store enough term data to
166131** distinguish adjacent children (if the rightmost term of the left
166132** child is "something", and the leftmost term of the right child is
166133** "wicked", only "w" is stored).
166134**
166135** New data is spilled to a new interior node at the same height when
166136** the current node exceeds INTERIOR_MAX bytes (default 2048).
166137** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
166138** interior nodes and making the tree too skinny. The interior nodes
166139** at a given height are naturally tracked by interior nodes at
166140** height+1, and so on.
166141**
166142**
166143**** Segment directory ****
166144** The segment directory in table %_segdir stores meta-information for
166145** merging and deleting segments, and also the root node of the
166146** segment's tree.
166147**
166148** The root node is the top node of the segment's tree after encoding
166149** the entire segment, restricted to ROOT_MAX bytes (default 1024).
166150** This could be either a leaf node or an interior node. If the top
166151** node requires more than ROOT_MAX bytes, it is flushed to %_segments
166152** and a new root interior node is generated (which should always fit
166153** within ROOT_MAX because it only needs space for 2 varints, the
166154** height and the blockid of the previous root).
166155**
166156** The meta-information in the segment directory is:
166157** level - segment level (see below)
166158** idx - index within level
166159** - (level,idx uniquely identify a segment)
166160** start_block - first leaf node
166161** leaves_end_block - last leaf node
166162** end_block - last block (including interior nodes)
166163** root - contents of root node
166164**
166165** If the root node is a leaf node, then start_block,
166166** leaves_end_block, and end_block are all 0.
166167**
166168**
166169**** Segment merging ****
166170** To amortize update costs, segments are grouped into levels and
166171** merged in batches. Each increase in level represents exponentially
166172** more documents.
166173**
166174** New documents (actually, document updates) are tokenized and
166175** written individually (using LeafWriter) to a level 0 segment, with
166176** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
166177** level 0 segments are merged into a single level 1 segment. Level 1
166178** is populated like level 0, and eventually MERGE_COUNT level 1
166179** segments are merged to a single level 2 segment (representing
166180** MERGE_COUNT^2 updates), and so on.
166181**
166182** A segment merge traverses all segments at a given level in
166183** parallel, performing a straightforward sorted merge. Since segment
166184** leaf nodes are written in to the %_segments table in order, this
166185** merge traverses the underlying sqlite disk structures efficiently.
166186** After the merge, all segment blocks from the merged level are
166187** deleted.
166188**
166189** MERGE_COUNT controls how often we merge segments. 16 seems to be
166190** somewhat of a sweet spot for insertion performance. 32 and 64 show
166191** very similar performance numbers to 16 on insertion, though they're
166192** a tiny bit slower (perhaps due to more overhead in merge-time
166193** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
166194** 16, 2 about 66% slower than 16.
166195**
166196** At query time, high MERGE_COUNT increases the number of segments
166197** which need to be scanned and merged. For instance, with 100k docs
166198** inserted:
166199**
166200** MERGE_COUNT segments
166201** 16 25
166202** 8 12
166203** 4 10
166204** 2 6
166205**
166206** This appears to have only a moderate impact on queries for very
166207** frequent terms (which are somewhat dominated by segment merge
166208** costs), and infrequent and non-existent terms still seem to be fast
166209** even with many segments.
166210**
166211** TODO(shess) That said, it would be nice to have a better query-side
166212** argument for MERGE_COUNT of 16. Also, it is possible/likely that
166213** optimizations to things like doclist merging will swing the sweet
166214** spot around.
166215**
166216**
166217**
166218**** Handling of deletions and updates ****
166219** Since we're using a segmented structure, with no docid-oriented
166220** index into the term index, we clearly cannot simply update the term
166221** index when a document is deleted or updated. For deletions, we
166222** write an empty doclist (varint(docid) varint(POS_END)), for updates
166223** we simply write the new doclist. Segment merges overwrite older
166224** data for a particular docid with newer data, so deletes or updates
166225** will eventually overtake the earlier data and knock it out. The
166226** query logic likewise merges doclists so that newer data knocks out
166227** older data.
166228*/
166229
166230/************** Include fts3Int.h in the middle of fts3.c ********************/
166231/************** Begin file fts3Int.h *****************************************/
166232/*
166233** 2009 Nov 12
166234**
166235** The author disclaims copyright to this source code. In place of
166236** a legal notice, here is a blessing:
166237**
166238** May you do good and not evil.
166239** May you find forgiveness for yourself and forgive others.
166240** May you share freely, never taking more than you give.
166241**
166242******************************************************************************
166243**
166244*/
166245#ifndef _FTSINT_H
166246#define _FTSINT_H
166247
166248#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
166249# define NDEBUG 1
166250#endif
166251
166252/* FTS3/FTS4 require virtual tables */
166253#ifdef SQLITE_OMIT_VIRTUALTABLE
166254# undef SQLITE_ENABLE_FTS3
166255# undef SQLITE_ENABLE_FTS4
166256#endif
166257
166258/*
166259** FTS4 is really an extension for FTS3. It is enabled using the
166260** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
166261** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
166262*/
166263#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
166264# define SQLITE_ENABLE_FTS3
166265#endif
166266
166267#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166268
166269/* If not building as part of the core, include sqlite3ext.h. */
166270#ifndef SQLITE_CORE
166271/* # include "sqlite3ext.h" */
166272SQLITE_EXTENSION_INIT3
166273#endif
166274
166275/* #include "sqlite3.h" */
166276/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
166277/************** Begin file fts3_tokenizer.h **********************************/
166278/*
166279** 2006 July 10
166280**
166281** The author disclaims copyright to this source code.
166282**
166283*************************************************************************
166284** Defines the interface to tokenizers used by fulltext-search. There
166285** are three basic components:
166286**
166287** sqlite3_tokenizer_module is a singleton defining the tokenizer
166288** interface functions. This is essentially the class structure for
166289** tokenizers.
166290**
166291** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
166292** including customization information defined at creation time.
166293**
166294** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
166295** tokens from a particular input.
166296*/
166297#ifndef _FTS3_TOKENIZER_H_
166298#define _FTS3_TOKENIZER_H_
166299
166300/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
166301** If tokenizers are to be allowed to call sqlite3_*() functions, then
166302** we will need a way to register the API consistently.
166303*/
166304/* #include "sqlite3.h" */
166305
166306/*
166307** Structures used by the tokenizer interface. When a new tokenizer
166308** implementation is registered, the caller provides a pointer to
166309** an sqlite3_tokenizer_module containing pointers to the callback
166310** functions that make up an implementation.
166311**
166312** When an fts3 table is created, it passes any arguments passed to
166313** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
166314** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
166315** implementation. The xCreate() function in turn returns an
166316** sqlite3_tokenizer structure representing the specific tokenizer to
166317** be used for the fts3 table (customized by the tokenizer clause arguments).
166318**
166319** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
166320** method is called. It returns an sqlite3_tokenizer_cursor object
166321** that may be used to tokenize a specific input buffer based on
166322** the tokenization rules supplied by a specific sqlite3_tokenizer
166323** object.
166324*/
166325typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
166326typedef struct sqlite3_tokenizer sqlite3_tokenizer;
166327typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
166328
166329struct sqlite3_tokenizer_module {
166330
166331 /*
166332 ** Structure version. Should always be set to 0 or 1.
166333 */
166334 int iVersion;
166335
166336 /*
166337 ** Create a new tokenizer. The values in the argv[] array are the
166338 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
166339 ** TABLE statement that created the fts3 table. For example, if
166340 ** the following SQL is executed:
166341 **
166342 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
166343 **
166344 ** then argc is set to 2, and the argv[] array contains pointers
166345 ** to the strings "arg1" and "arg2".
166346 **
166347 ** This method should return either SQLITE_OK (0), or an SQLite error
166348 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
166349 ** to point at the newly created tokenizer structure. The generic
166350 ** sqlite3_tokenizer.pModule variable should not be initialized by
166351 ** this callback. The caller will do so.
166352 */
166353 int (*xCreate)(
166354 int argc, /* Size of argv array */
166355 const char *const*argv, /* Tokenizer argument strings */
166356 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
166357 );
166358
166359 /*
166360 ** Destroy an existing tokenizer. The fts3 module calls this method
166361 ** exactly once for each successful call to xCreate().
166362 */
166363 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
166364
166365 /*
166366 ** Create a tokenizer cursor to tokenize an input buffer. The caller
166367 ** is responsible for ensuring that the input buffer remains valid
166368 ** until the cursor is closed (using the xClose() method).
166369 */
166370 int (*xOpen)(
166371 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
166372 const char *pInput, int nBytes, /* Input buffer */
166373 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
166374 );
166375
166376 /*
166377 ** Destroy an existing tokenizer cursor. The fts3 module calls this
166378 ** method exactly once for each successful call to xOpen().
166379 */
166380 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
166381
166382 /*
166383 ** Retrieve the next token from the tokenizer cursor pCursor. This
166384 ** method should either return SQLITE_OK and set the values of the
166385 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
166386 ** the end of the buffer has been reached, or an SQLite error code.
166387 **
166388 ** *ppToken should be set to point at a buffer containing the
166389 ** normalized version of the token (i.e. after any case-folding and/or
166390 ** stemming has been performed). *pnBytes should be set to the length
166391 ** of this buffer in bytes. The input text that generated the token is
166392 ** identified by the byte offsets returned in *piStartOffset and
166393 ** *piEndOffset. *piStartOffset should be set to the index of the first
166394 ** byte of the token in the input buffer. *piEndOffset should be set
166395 ** to the index of the first byte just past the end of the token in
166396 ** the input buffer.
166397 **
166398 ** The buffer *ppToken is set to point at is managed by the tokenizer
166399 ** implementation. It is only required to be valid until the next call
166400 ** to xNext() or xClose().
166401 */
166402 /* TODO(shess) current implementation requires pInput to be
166403 ** nul-terminated. This should either be fixed, or pInput/nBytes
166404 ** should be converted to zInput.
166405 */
166406 int (*xNext)(
166407 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
166408 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
166409 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
166410 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
166411 int *piPosition /* OUT: Number of tokens returned before this one */
166412 );
166413
166414 /***********************************************************************
166415 ** Methods below this point are only available if iVersion>=1.
166416 */
166417
166418 /*
166419 ** Configure the language id of a tokenizer cursor.
166420 */
166421 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
166422};
166423
166424struct sqlite3_tokenizer {
166425 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
166426 /* Tokenizer implementations will typically add additional fields */
166427};
166428
166429struct sqlite3_tokenizer_cursor {
166430 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
166431 /* Tokenizer implementations will typically add additional fields */
166432};
166433
166434int fts3_global_term_cnt(int iTerm, int iCol);
166435int fts3_term_cnt(int iTerm, int iCol);
166436
166437
166438#endif /* _FTS3_TOKENIZER_H_ */
166439
166440/************** End of fts3_tokenizer.h **************************************/
166441/************** Continuing where we left off in fts3Int.h ********************/
166442/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
166443/************** Begin file fts3_hash.h ***************************************/
166444/*
166445** 2001 September 22
166446**
166447** The author disclaims copyright to this source code. In place of
166448** a legal notice, here is a blessing:
166449**
166450** May you do good and not evil.
166451** May you find forgiveness for yourself and forgive others.
166452** May you share freely, never taking more than you give.
166453**
166454*************************************************************************
166455** This is the header file for the generic hash-table implementation
166456** used in SQLite. We've modified it slightly to serve as a standalone
166457** hash table implementation for the full-text indexing module.
166458**
166459*/
166460#ifndef _FTS3_HASH_H_
166461#define _FTS3_HASH_H_
166462
166463/* Forward declarations of structures. */
166464typedef struct Fts3Hash Fts3Hash;
166465typedef struct Fts3HashElem Fts3HashElem;
166466
166467/* A complete hash table is an instance of the following structure.
166468** The internals of this structure are intended to be opaque -- client
166469** code should not attempt to access or modify the fields of this structure
166470** directly. Change this structure only by using the routines below.
166471** However, many of the "procedures" and "functions" for modifying and
166472** accessing this structure are really macros, so we can't really make
166473** this structure opaque.
166474*/
166475struct Fts3Hash {
166476 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
166477 char copyKey; /* True if copy of key made on insert */
166478 int count; /* Number of entries in this table */
166479 Fts3HashElem *first; /* The first element of the array */
166480 int htsize; /* Number of buckets in the hash table */
166481 struct _fts3ht { /* the hash table */
166482 int count; /* Number of entries with this hash */
166483 Fts3HashElem *chain; /* Pointer to first entry with this hash */
166484 } *ht;
166485};
166486
166487/* Each element in the hash table is an instance of the following
166488** structure. All elements are stored on a single doubly-linked list.
166489**
166490** Again, this structure is intended to be opaque, but it can't really
166491** be opaque because it is used by macros.
166492*/
166493struct Fts3HashElem {
166494 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
166495 void *data; /* Data associated with this element */
166496 void *pKey; int nKey; /* Key associated with this element */
166497};
166498
166499/*
166500** There are 2 different modes of operation for a hash table:
166501**
166502** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
166503** (including the null-terminator, if any). Case
166504** is respected in comparisons.
166505**
166506** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
166507** memcmp() is used to compare keys.
166508**
166509** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
166510*/
166511#define FTS3_HASH_STRING 1
166512#define FTS3_HASH_BINARY 2
166513
166514/*
166515** Access routines. To delete, insert a NULL pointer.
166516*/
166517SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
166518SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
166519SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
166520SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
166521SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
166522
166523/*
166524** Shorthand for the functions above
166525*/
166526#define fts3HashInit sqlite3Fts3HashInit
166527#define fts3HashInsert sqlite3Fts3HashInsert
166528#define fts3HashFind sqlite3Fts3HashFind
166529#define fts3HashClear sqlite3Fts3HashClear
166530#define fts3HashFindElem sqlite3Fts3HashFindElem
166531
166532/*
166533** Macros for looping over all elements of a hash table. The idiom is
166534** like this:
166535**
166536** Fts3Hash h;
166537** Fts3HashElem *p;
166538** ...
166539** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
166540** SomeStructure *pData = fts3HashData(p);
166541** // do something with pData
166542** }
166543*/
166544#define fts3HashFirst(H) ((H)->first)
166545#define fts3HashNext(E) ((E)->next)
166546#define fts3HashData(E) ((E)->data)
166547#define fts3HashKey(E) ((E)->pKey)
166548#define fts3HashKeysize(E) ((E)->nKey)
166549
166550/*
166551** Number of entries in a hash table
166552*/
166553#define fts3HashCount(H) ((H)->count)
166554
166555#endif /* _FTS3_HASH_H_ */
166556
166557/************** End of fts3_hash.h *******************************************/
166558/************** Continuing where we left off in fts3Int.h ********************/
166559
166560/*
166561** This constant determines the maximum depth of an FTS expression tree
166562** that the library will create and use. FTS uses recursion to perform
166563** various operations on the query tree, so the disadvantage of a large
166564** limit is that it may allow very large queries to use large amounts
166565** of stack space (perhaps causing a stack overflow).
166566*/
166567#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
166568# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
166569#endif
166570
166571
166572/*
166573** This constant controls how often segments are merged. Once there are
166574** FTS3_MERGE_COUNT segments of level N, they are merged into a single
166575** segment of level N+1.
166576*/
166577#define FTS3_MERGE_COUNT 16
166578
166579/*
166580** This is the maximum amount of data (in bytes) to store in the
166581** Fts3Table.pendingTerms hash table. Normally, the hash table is
166582** populated as documents are inserted/updated/deleted in a transaction
166583** and used to create a new segment when the transaction is committed.
166584** However if this limit is reached midway through a transaction, a new
166585** segment is created and the hash table cleared immediately.
166586*/
166587#define FTS3_MAX_PENDING_DATA (1*1024*1024)
166588
166589/*
166590** Macro to return the number of elements in an array. SQLite has a
166591** similar macro called ArraySize(). Use a different name to avoid
166592** a collision when building an amalgamation with built-in FTS3.
166593*/
166594#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
166595
166596
166597#ifndef MIN
166598# define MIN(x,y) ((x)<(y)?(x):(y))
166599#endif
166600#ifndef MAX
166601# define MAX(x,y) ((x)>(y)?(x):(y))
166602#endif
166603
166604/*
166605** Maximum length of a varint encoded integer. The varint format is different
166606** from that used by SQLite, so the maximum length is 10, not 9.
166607*/
166608#define FTS3_VARINT_MAX 10
166609
166610#define FTS3_BUFFER_PADDING 8
166611
166612/*
166613** FTS4 virtual tables may maintain multiple indexes - one index of all terms
166614** in the document set and zero or more prefix indexes. All indexes are stored
166615** as one or more b+-trees in the %_segments and %_segdir tables.
166616**
166617** It is possible to determine which index a b+-tree belongs to based on the
166618** value stored in the "%_segdir.level" column. Given this value L, the index
166619** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
166620** level values between 0 and 1023 (inclusive) belong to index 0, all levels
166621** between 1024 and 2047 to index 1, and so on.
166622**
166623** It is considered impossible for an index to use more than 1024 levels. In
166624** theory though this may happen, but only after at least
166625** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
166626*/
166627#define FTS3_SEGDIR_MAXLEVEL 1024
166628#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
166629
166630/*
166631** The testcase() macro is only used by the amalgamation. If undefined,
166632** make it a no-op.
166633*/
166634#ifndef testcase
166635# define testcase(X)
166636#endif
166637
166638/*
166639** Terminator values for position-lists and column-lists.
166640*/
166641#define POS_COLUMN (1) /* Column-list terminator */
166642#define POS_END (0) /* Position-list terminator */
166643
166644/*
166645** The assert_fts3_nc() macro is similar to the assert() macro, except that it
166646** is used for assert() conditions that are true only if it can be
166647** guranteed that the database is not corrupt.
166648*/
166649#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
166650SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
166651# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
166652#else
166653# define assert_fts3_nc(x) assert(x)
166654#endif
166655
166656/*
166657** This section provides definitions to allow the
166658** FTS3 extension to be compiled outside of the
166659** amalgamation.
166660*/
166661#ifndef SQLITE_AMALGAMATION
166662/*
166663** Macros indicating that conditional expressions are always true or
166664** false.
166665*/
166666#ifdef SQLITE_COVERAGE_TEST
166667# define ALWAYS(x) (1)
166668# define NEVER(X) (0)
166669#elif defined(SQLITE_DEBUG)
166670# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
166671# define NEVER(x) sqlite3Fts3Never((x)!=0)
166672SQLITE_PRIVATE int sqlite3Fts3Always(int b);
166673SQLITE_PRIVATE int sqlite3Fts3Never(int b);
166674#else
166675# define ALWAYS(x) (x)
166676# define NEVER(x) (x)
166677#endif
166678
166679/*
166680** Internal types used by SQLite.
166681*/
166682typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
166683typedef short int i16; /* 2-byte (or larger) signed integer */
166684typedef unsigned int u32; /* 4-byte unsigned integer */
166685typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
166686typedef sqlite3_int64 i64; /* 8-byte signed integer */
166687
166688/*
166689** Macro used to suppress compiler warnings for unused parameters.
166690*/
166691#define UNUSED_PARAMETER(x) (void)(x)
166692
166693/*
166694** Activate assert() only if SQLITE_TEST is enabled.
166695*/
166696#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
166697# define NDEBUG 1
166698#endif
166699
166700/*
166701** The TESTONLY macro is used to enclose variable declarations or
166702** other bits of code that are needed to support the arguments
166703** within testcase() and assert() macros.
166704*/
166705#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
166706# define TESTONLY(X) X
166707#else
166708# define TESTONLY(X)
166709#endif
166710
166711#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
166712#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
166713
166714#define deliberate_fall_through
166715
166716#endif /* SQLITE_AMALGAMATION */
166717
166718#ifdef SQLITE_DEBUG
166719SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
166720# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
166721#else
166722# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
166723#endif
166724
166725typedef struct Fts3Table Fts3Table;
166726typedef struct Fts3Cursor Fts3Cursor;
166727typedef struct Fts3Expr Fts3Expr;
166728typedef struct Fts3Phrase Fts3Phrase;
166729typedef struct Fts3PhraseToken Fts3PhraseToken;
166730
166731typedef struct Fts3Doclist Fts3Doclist;
166732typedef struct Fts3SegFilter Fts3SegFilter;
166733typedef struct Fts3DeferredToken Fts3DeferredToken;
166734typedef struct Fts3SegReader Fts3SegReader;
166735typedef struct Fts3MultiSegReader Fts3MultiSegReader;
166736
166737typedef struct MatchinfoBuffer MatchinfoBuffer;
166738
166739/*
166740** A connection to a fulltext index is an instance of the following
166741** structure. The xCreate and xConnect methods create an instance
166742** of this structure and xDestroy and xDisconnect free that instance.
166743** All other methods receive a pointer to the structure as one of their
166744** arguments.
166745*/
166746struct Fts3Table {
166747 sqlite3_vtab base; /* Base class used by SQLite core */
166748 sqlite3 *db; /* The database connection */
166749 const char *zDb; /* logical database name */
166750 const char *zName; /* virtual table name */
166751 int nColumn; /* number of named columns in virtual table */
166752 char **azColumn; /* column names. malloced */
166753 u8 *abNotindexed; /* True for 'notindexed' columns */
166754 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
166755 char *zContentTbl; /* content=xxx option, or NULL */
166756 char *zLanguageid; /* languageid=xxx option, or NULL */
166757 int nAutoincrmerge; /* Value configured by 'automerge' */
166758 u32 nLeafAdd; /* Number of leaf blocks added this trans */
166759 int bLock; /* Used to prevent recursive content= tbls */
166760
166761 /* Precompiled statements used by the implementation. Each of these
166762 ** statements is run and reset within a single virtual table API call.
166763 */
166764 sqlite3_stmt *aStmt[40];
166765 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
166766
166767 char *zReadExprlist;
166768 char *zWriteExprlist;
166769
166770 int nNodeSize; /* Soft limit for node size */
166771 u8 bFts4; /* True for FTS4, false for FTS3 */
166772 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
166773 u8 bHasDocsize; /* True if %_docsize table exists */
166774 u8 bDescIdx; /* True if doclists are in reverse order */
166775 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
166776 int nPgsz; /* Page size for host database */
166777 char *zSegmentsTbl; /* Name of %_segments table */
166778 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
166779
166780 /*
166781 ** The following array of hash tables is used to buffer pending index
166782 ** updates during transactions. All pending updates buffered at any one
166783 ** time must share a common language-id (see the FTS4 langid= feature).
166784 ** The current language id is stored in variable iPrevLangid.
166785 **
166786 ** A single FTS4 table may have multiple full-text indexes. For each index
166787 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
166788 ** terms that appear in the document set. Each subsequent index in aIndex[]
166789 ** is an index of prefixes of a specific length.
166790 **
166791 ** Variable nPendingData contains an estimate the memory consumed by the
166792 ** pending data structures, including hash table overhead, but not including
166793 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
166794 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
166795 ** recently inserted record.
166796 */
166797 int nIndex; /* Size of aIndex[] */
166798 struct Fts3Index {
166799 int nPrefix; /* Prefix length (0 for main terms index) */
166800 Fts3Hash hPending; /* Pending terms table for this index */
166801 } *aIndex;
166802 int nMaxPendingData; /* Max pending data before flush to disk */
166803 int nPendingData; /* Current bytes of pending data */
166804 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
166805 int iPrevLangid; /* Langid of recently inserted document */
166806 int bPrevDelete; /* True if last operation was a delete */
166807
166808#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
166809 /* State variables used for validating that the transaction control
166810 ** methods of the virtual table are called at appropriate times. These
166811 ** values do not contribute to FTS functionality; they are used for
166812 ** verifying the operation of the SQLite core.
166813 */
166814 int inTransaction; /* True after xBegin but before xCommit/xRollback */
166815 int mxSavepoint; /* Largest valid xSavepoint integer */
166816#endif
166817
166818#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
166819 /* True to disable the incremental doclist optimization. This is controled
166820 ** by special insert command 'test-no-incr-doclist'. */
166821 int bNoIncrDoclist;
166822
166823 /* Number of segments in a level */
166824 int nMergeCount;
166825#endif
166826};
166827
166828/* Macro to find the number of segments to merge */
166829#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
166830# define MergeCount(P) ((P)->nMergeCount)
166831#else
166832# define MergeCount(P) FTS3_MERGE_COUNT
166833#endif
166834
166835/*
166836** When the core wants to read from the virtual table, it creates a
166837** virtual table cursor (an instance of the following structure) using
166838** the xOpen method. Cursors are destroyed using the xClose method.
166839*/
166840struct Fts3Cursor {
166841 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
166842 i16 eSearch; /* Search strategy (see below) */
166843 u8 isEof; /* True if at End Of Results */
166844 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
166845 u8 bSeekStmt; /* True if pStmt is a seek */
166846 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
166847 Fts3Expr *pExpr; /* Parsed MATCH query string */
166848 int iLangid; /* Language being queried for */
166849 int nPhrase; /* Number of matchable phrases in query */
166850 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
166851 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
166852 char *pNextId; /* Pointer into the body of aDoclist */
166853 char *aDoclist; /* List of docids for full-text queries */
166854 int nDoclist; /* Size of buffer at aDoclist */
166855 u8 bDesc; /* True to sort in descending order */
166856 int eEvalmode; /* An FTS3_EVAL_XX constant */
166857 int nRowAvg; /* Average size of database rows, in pages */
166858 sqlite3_int64 nDoc; /* Documents in table */
166859 i64 iMinDocid; /* Minimum docid to return */
166860 i64 iMaxDocid; /* Maximum docid to return */
166861 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
166862 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
166863};
166864
166865#define FTS3_EVAL_FILTER 0
166866#define FTS3_EVAL_NEXT 1
166867#define FTS3_EVAL_MATCHINFO 2
166868
166869/*
166870** The Fts3Cursor.eSearch member is always set to one of the following.
166871** Actualy, Fts3Cursor.eSearch can be greater than or equal to
166872** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
166873** of the column to be searched. For example, in
166874**
166875** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
166876** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
166877**
166878** Because the LHS of the MATCH operator is 2nd column "b",
166879** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
166880** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
166881** indicating that all columns should be searched,
166882** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
166883*/
166884#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
166885#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
166886#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
166887
166888/*
166889** The lower 16-bits of the sqlite3_index_info.idxNum value set by
166890** the xBestIndex() method contains the Fts3Cursor.eSearch value described
166891** above. The upper 16-bits contain a combination of the following
166892** bits, used to describe extra constraints on full-text searches.
166893*/
166894#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
166895#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
166896#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
166897
166898struct Fts3Doclist {
166899 char *aAll; /* Array containing doclist (or NULL) */
166900 int nAll; /* Size of a[] in bytes */
166901 char *pNextDocid; /* Pointer to next docid */
166902
166903 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
166904 int bFreeList; /* True if pList should be sqlite3_free()d */
166905 char *pList; /* Pointer to position list following iDocid */
166906 int nList; /* Length of position list */
166907};
166908
166909/*
166910** A "phrase" is a sequence of one or more tokens that must match in
166911** sequence. A single token is the base case and the most common case.
166912** For a sequence of tokens contained in double-quotes (i.e. "one two three")
166913** nToken will be the number of tokens in the string.
166914*/
166915struct Fts3PhraseToken {
166916 char *z; /* Text of the token */
166917 int n; /* Number of bytes in buffer z */
166918 int isPrefix; /* True if token ends with a "*" character */
166919 int bFirst; /* True if token must appear at position 0 */
166920
166921 /* Variables above this point are populated when the expression is
166922 ** parsed (by code in fts3_expr.c). Below this point the variables are
166923 ** used when evaluating the expression. */
166924 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
166925 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
166926};
166927
166928struct Fts3Phrase {
166929 /* Cache of doclist for this phrase. */
166930 Fts3Doclist doclist;
166931 int bIncr; /* True if doclist is loaded incrementally */
166932 int iDoclistToken;
166933
166934 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
166935 ** OR condition. */
166936 char *pOrPoslist;
166937 i64 iOrDocid;
166938
166939 /* Variables below this point are populated by fts3_expr.c when parsing
166940 ** a MATCH expression. Everything above is part of the evaluation phase.
166941 */
166942 int nToken; /* Number of tokens in the phrase */
166943 int iColumn; /* Index of column this phrase must match */
166944 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
166945};
166946
166947/*
166948** A tree of these objects forms the RHS of a MATCH operator.
166949**
166950** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
166951** points to a malloced buffer, size nDoclist bytes, containing the results
166952** of this phrase query in FTS3 doclist format. As usual, the initial
166953** "Length" field found in doclists stored on disk is omitted from this
166954** buffer.
166955**
166956** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
166957** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
166958** where nCol is the number of columns in the queried FTS table. The array
166959** is populated as follows:
166960**
166961** aMI[iCol*3 + 0] = Undefined
166962** aMI[iCol*3 + 1] = Number of occurrences
166963** aMI[iCol*3 + 2] = Number of rows containing at least one instance
166964**
166965** The aMI array is allocated using sqlite3_malloc(). It should be freed
166966** when the expression node is.
166967*/
166968struct Fts3Expr {
166969 int eType; /* One of the FTSQUERY_XXX values defined below */
166970 int nNear; /* Valid if eType==FTSQUERY_NEAR */
166971 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
166972 Fts3Expr *pLeft; /* Left operand */
166973 Fts3Expr *pRight; /* Right operand */
166974 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
166975
166976 /* The following are used by the fts3_eval.c module. */
166977 sqlite3_int64 iDocid; /* Current docid */
166978 u8 bEof; /* True this expression is at EOF already */
166979 u8 bStart; /* True if iDocid is valid */
166980 u8 bDeferred; /* True if this expression is entirely deferred */
166981
166982 /* The following are used by the fts3_snippet.c module. */
166983 int iPhrase; /* Index of this phrase in matchinfo() results */
166984 u32 *aMI; /* See above */
166985};
166986
166987/*
166988** Candidate values for Fts3Query.eType. Note that the order of the first
166989** four values is in order of precedence when parsing expressions. For
166990** example, the following:
166991**
166992** "a OR b AND c NOT d NEAR e"
166993**
166994** is equivalent to:
166995**
166996** "a OR (b AND (c NOT (d NEAR e)))"
166997*/
166998#define FTSQUERY_NEAR 1
166999#define FTSQUERY_NOT 2
167000#define FTSQUERY_AND 3
167001#define FTSQUERY_OR 4
167002#define FTSQUERY_PHRASE 5
167003
167004
167005/* fts3_write.c */
167006SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
167007SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
167008SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
167009SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
167010SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
167011 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
167012SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
167013 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
167014SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
167015SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
167016SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
167017
167018SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
167019SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
167020
167021#ifndef SQLITE_DISABLE_FTS4_DEFERRED
167022SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
167023SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
167024SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
167025SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
167026SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
167027#else
167028# define sqlite3Fts3FreeDeferredTokens(x)
167029# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
167030# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
167031# define sqlite3Fts3FreeDeferredDoclists(x)
167032# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
167033#endif
167034
167035SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
167036SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
167037
167038/* Special values interpreted by sqlite3SegReaderCursor() */
167039#define FTS3_SEGCURSOR_PENDING -1
167040#define FTS3_SEGCURSOR_ALL -2
167041
167042SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
167043SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
167044SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
167045
167046SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
167047 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
167048
167049/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
167050#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
167051#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
167052#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
167053#define FTS3_SEGMENT_PREFIX 0x00000008
167054#define FTS3_SEGMENT_SCAN 0x00000010
167055#define FTS3_SEGMENT_FIRST 0x00000020
167056
167057/* Type passed as 4th argument to SegmentReaderIterate() */
167058struct Fts3SegFilter {
167059 const char *zTerm;
167060 int nTerm;
167061 int iCol;
167062 int flags;
167063};
167064
167065struct Fts3MultiSegReader {
167066 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
167067 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
167068 int nSegment; /* Size of apSegment array */
167069 int nAdvance; /* How many seg-readers to advance */
167070 Fts3SegFilter *pFilter; /* Pointer to filter object */
167071 char *aBuffer; /* Buffer to merge doclists in */
167072 int nBuffer; /* Allocated size of aBuffer[] in bytes */
167073
167074 int iColFilter; /* If >=0, filter for this column */
167075 int bRestart;
167076
167077 /* Used by fts3.c only. */
167078 int nCost; /* Cost of running iterator */
167079 int bLookup; /* True if a lookup of a single entry. */
167080
167081 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
167082 char *zTerm; /* Pointer to term buffer */
167083 int nTerm; /* Size of zTerm in bytes */
167084 char *aDoclist; /* Pointer to doclist buffer */
167085 int nDoclist; /* Size of aDoclist[] in bytes */
167086};
167087
167088SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
167089
167090#define fts3GetVarint32(p, piVal) ( \
167091 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
167092)
167093
167094/* fts3.c */
167095SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
167096SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
167097SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
167098SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
167099SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
167100SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
167101SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
167102SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
167103SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
167104SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
167105SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
167106SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
167107SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
167108SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut);
167109
167110/* fts3_tokenizer.c */
167111SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
167112SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
167113SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
167114 sqlite3_tokenizer **, char **
167115);
167116SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
167117
167118/* fts3_snippet.c */
167119SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
167120SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
167121 const char *, const char *, int, int
167122);
167123SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
167124SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
167125
167126/* fts3_expr.c */
167127SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
167128 char **, int, int, int, const char *, int, Fts3Expr **, char **
167129);
167130SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
167131#ifdef SQLITE_TEST
167132SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
167133SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
167134#endif
167135
167136SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
167137 sqlite3_tokenizer_cursor **
167138);
167139
167140/* fts3_aux.c */
167141SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
167142
167143SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
167144
167145SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
167146 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
167147SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
167148 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
167149SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
167150SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
167151SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
167152
167153/* fts3_tokenize_vtab.c */
167154SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
167155
167156/* fts3_unicode2.c (functions generated by parsing unicode text files) */
167157#ifndef SQLITE_DISABLE_FTS3_UNICODE
167158SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
167159SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
167160SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
167161#endif
167162
167163#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
167164#endif /* _FTSINT_H */
167165
167166/************** End of fts3Int.h *********************************************/
167167/************** Continuing where we left off in fts3.c ***********************/
167168#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167169
167170#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
167171# define SQLITE_CORE 1
167172#endif
167173
167174/* #include <assert.h> */
167175/* #include <stdlib.h> */
167176/* #include <stddef.h> */
167177/* #include <stdio.h> */
167178/* #include <string.h> */
167179/* #include <stdarg.h> */
167180
167181/* #include "fts3.h" */
167182#ifndef SQLITE_CORE
167183/* # include "sqlite3ext.h" */
167184 SQLITE_EXTENSION_INIT1
167185#endif
167186
167187static int fts3EvalNext(Fts3Cursor *pCsr);
167188static int fts3EvalStart(Fts3Cursor *pCsr);
167189static int fts3TermSegReaderCursor(
167190 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
167191
167192#ifndef SQLITE_AMALGAMATION
167193# if defined(SQLITE_DEBUG)
167194SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
167195SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; }
167196# endif
167197#endif
167198
167199/*
167200** This variable is set to false when running tests for which the on disk
167201** structures should not be corrupt. Otherwise, true. If it is false, extra
167202** assert() conditions in the fts3 code are activated - conditions that are
167203** only true if it is guaranteed that the fts3 database is not corrupt.
167204*/
167205SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
167206
167207/*
167208** Write a 64-bit variable-length integer to memory starting at p[0].
167209** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
167210** The number of bytes written is returned.
167211*/
167212SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
167213 unsigned char *q = (unsigned char *) p;
167214 sqlite_uint64 vu = v;
167215 do{
167216 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
167217 vu >>= 7;
167218 }while( vu!=0 );
167219 q[-1] &= 0x7f; /* turn off high bit in final byte */
167220 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
167221 return (int) (q - (unsigned char *)p);
167222}
167223
167224#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
167225 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
167226 if( (v & mask2)==0 ){ var = v; return ret; }
167227#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
167228 v = (*ptr++); \
167229 if( (v & mask2)==0 ){ var = v; return ret; }
167230
167231SQLITE_PRIVATE int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
167232 const unsigned char *p = (const unsigned char*)pBuf;
167233 const unsigned char *pStart = p;
167234 u32 a;
167235 u64 b;
167236 int shift;
167237
167238 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
167239 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
167240 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
167241 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
167242 b = (a & 0x0FFFFFFF );
167243
167244 for(shift=28; shift<=63; shift+=7){
167245 u64 c = *p++;
167246 b += (c&0x7F) << shift;
167247 if( (c & 0x80)==0 ) break;
167248 }
167249 *v = b;
167250 return (int)(p - pStart);
167251}
167252
167253/*
167254** Read a 64-bit variable-length integer from memory starting at p[0].
167255** Return the number of bytes read, or 0 on error.
167256** The value is stored in *v.
167257*/
167258SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
167259 return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
167260}
167261
167262/*
167263** Read a 64-bit variable-length integer from memory starting at p[0] and
167264** not extending past pEnd[-1].
167265** Return the number of bytes read, or 0 on error.
167266** The value is stored in *v.
167267*/
167268SQLITE_PRIVATE int sqlite3Fts3GetVarintBounded(
167269 const char *pBuf,
167270 const char *pEnd,
167271 sqlite_int64 *v
167272){
167273 const unsigned char *p = (const unsigned char*)pBuf;
167274 const unsigned char *pStart = p;
167275 const unsigned char *pX = (const unsigned char*)pEnd;
167276 u64 b = 0;
167277 int shift;
167278 for(shift=0; shift<=63; shift+=7){
167279 u64 c = p<pX ? *p : 0;
167280 p++;
167281 b += (c&0x7F) << shift;
167282 if( (c & 0x80)==0 ) break;
167283 }
167284 *v = b;
167285 return (int)(p - pStart);
167286}
167287
167288/*
167289** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
167290** a non-negative 32-bit integer before it is returned.
167291*/
167292SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
167293 const unsigned char *ptr = (const unsigned char*)p;
167294 u32 a;
167295
167296#ifndef fts3GetVarint32
167297 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1);
167298#else
167299 a = (*ptr++);
167300 assert( a & 0x80 );
167301#endif
167302
167303 GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2);
167304 GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3);
167305 GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
167306 a = (a & 0x0FFFFFFF );
167307 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
167308 assert( 0==(a & 0x80000000) );
167309 assert( *pi>=0 );
167310 return 5;
167311}
167312
167313/*
167314** Return the number of bytes required to encode v as a varint
167315*/
167316SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
167317 int i = 0;
167318 do{
167319 i++;
167320 v >>= 7;
167321 }while( v!=0 );
167322 return i;
167323}
167324
167325/*
167326** Convert an SQL-style quoted string into a normal string by removing
167327** the quote characters. The conversion is done in-place. If the
167328** input does not begin with a quote character, then this routine
167329** is a no-op.
167330**
167331** Examples:
167332**
167333** "abc" becomes abc
167334** 'xyz' becomes xyz
167335** [pqr] becomes pqr
167336** `mno` becomes mno
167337**
167338*/
167339SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
167340 char quote; /* Quote character (if any ) */
167341
167342 quote = z[0];
167343 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
167344 int iIn = 1; /* Index of next byte to read from input */
167345 int iOut = 0; /* Index of next byte to write to output */
167346
167347 /* If the first byte was a '[', then the close-quote character is a ']' */
167348 if( quote=='[' ) quote = ']';
167349
167350 while( z[iIn] ){
167351 if( z[iIn]==quote ){
167352 if( z[iIn+1]!=quote ) break;
167353 z[iOut++] = quote;
167354 iIn += 2;
167355 }else{
167356 z[iOut++] = z[iIn++];
167357 }
167358 }
167359 z[iOut] = '\0';
167360 }
167361}
167362
167363/*
167364** Read a single varint from the doclist at *pp and advance *pp to point
167365** to the first byte past the end of the varint. Add the value of the varint
167366** to *pVal.
167367*/
167368static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
167369 sqlite3_int64 iVal;
167370 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
167371 *pVal += iVal;
167372}
167373
167374/*
167375** When this function is called, *pp points to the first byte following a
167376** varint that is part of a doclist (or position-list, or any other list
167377** of varints). This function moves *pp to point to the start of that varint,
167378** and sets *pVal by the varint value.
167379**
167380** Argument pStart points to the first byte of the doclist that the
167381** varint is part of.
167382*/
167383static void fts3GetReverseVarint(
167384 char **pp,
167385 char *pStart,
167386 sqlite3_int64 *pVal
167387){
167388 sqlite3_int64 iVal;
167389 char *p;
167390
167391 /* Pointer p now points at the first byte past the varint we are
167392 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
167393 ** clear on character p[-1]. */
167394 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
167395 p++;
167396 *pp = p;
167397
167398 sqlite3Fts3GetVarint(p, &iVal);
167399 *pVal = iVal;
167400}
167401
167402/*
167403** The xDisconnect() virtual table method.
167404*/
167405static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
167406 Fts3Table *p = (Fts3Table *)pVtab;
167407 int i;
167408
167409 assert( p->nPendingData==0 );
167410 assert( p->pSegments==0 );
167411
167412 /* Free any prepared statements held */
167413 sqlite3_finalize(p->pSeekStmt);
167414 for(i=0; i<SizeofArray(p->aStmt); i++){
167415 sqlite3_finalize(p->aStmt[i]);
167416 }
167417 sqlite3_free(p->zSegmentsTbl);
167418 sqlite3_free(p->zReadExprlist);
167419 sqlite3_free(p->zWriteExprlist);
167420 sqlite3_free(p->zContentTbl);
167421 sqlite3_free(p->zLanguageid);
167422
167423 /* Invoke the tokenizer destructor to free the tokenizer. */
167424 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
167425
167426 sqlite3_free(p);
167427 return SQLITE_OK;
167428}
167429
167430/*
167431** Write an error message into *pzErr
167432*/
167433SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
167434 va_list ap;
167435 sqlite3_free(*pzErr);
167436 va_start(ap, zFormat);
167437 *pzErr = sqlite3_vmprintf(zFormat, ap);
167438 va_end(ap);
167439}
167440
167441/*
167442** Construct one or more SQL statements from the format string given
167443** and then evaluate those statements. The success code is written
167444** into *pRc.
167445**
167446** If *pRc is initially non-zero then this routine is a no-op.
167447*/
167448static void fts3DbExec(
167449 int *pRc, /* Success code */
167450 sqlite3 *db, /* Database in which to run SQL */
167451 const char *zFormat, /* Format string for SQL */
167452 ... /* Arguments to the format string */
167453){
167454 va_list ap;
167455 char *zSql;
167456 if( *pRc ) return;
167457 va_start(ap, zFormat);
167458 zSql = sqlite3_vmprintf(zFormat, ap);
167459 va_end(ap);
167460 if( zSql==0 ){
167461 *pRc = SQLITE_NOMEM;
167462 }else{
167463 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
167464 sqlite3_free(zSql);
167465 }
167466}
167467
167468/*
167469** The xDestroy() virtual table method.
167470*/
167471static int fts3DestroyMethod(sqlite3_vtab *pVtab){
167472 Fts3Table *p = (Fts3Table *)pVtab;
167473 int rc = SQLITE_OK; /* Return code */
167474 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
167475 sqlite3 *db = p->db; /* Database handle */
167476
167477 /* Drop the shadow tables */
167478 fts3DbExec(&rc, db,
167479 "DROP TABLE IF EXISTS %Q.'%q_segments';"
167480 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
167481 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
167482 "DROP TABLE IF EXISTS %Q.'%q_stat';"
167483 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
167484 zDb, p->zName,
167485 zDb, p->zName,
167486 zDb, p->zName,
167487 zDb, p->zName,
167488 (p->zContentTbl ? "--" : ""), zDb,p->zName
167489 );
167490
167491 /* If everything has worked, invoke fts3DisconnectMethod() to free the
167492 ** memory associated with the Fts3Table structure and return SQLITE_OK.
167493 ** Otherwise, return an SQLite error code.
167494 */
167495 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
167496}
167497
167498
167499/*
167500** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
167501** passed as the first argument. This is done as part of the xConnect()
167502** and xCreate() methods.
167503**
167504** If *pRc is non-zero when this function is called, it is a no-op.
167505** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
167506** before returning.
167507*/
167508static void fts3DeclareVtab(int *pRc, Fts3Table *p){
167509 if( *pRc==SQLITE_OK ){
167510 int i; /* Iterator variable */
167511 int rc; /* Return code */
167512 char *zSql; /* SQL statement passed to declare_vtab() */
167513 char *zCols; /* List of user defined columns */
167514 const char *zLanguageid;
167515
167516 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
167517 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
167518
167519 /* Create a list of user columns for the virtual table */
167520 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
167521 for(i=1; zCols && i<p->nColumn; i++){
167522 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
167523 }
167524
167525 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
167526 zSql = sqlite3_mprintf(
167527 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
167528 zCols, p->zName, zLanguageid
167529 );
167530 if( !zCols || !zSql ){
167531 rc = SQLITE_NOMEM;
167532 }else{
167533 rc = sqlite3_declare_vtab(p->db, zSql);
167534 }
167535
167536 sqlite3_free(zSql);
167537 sqlite3_free(zCols);
167538 *pRc = rc;
167539 }
167540}
167541
167542/*
167543** Create the %_stat table if it does not already exist.
167544*/
167545SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
167546 fts3DbExec(pRc, p->db,
167547 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
167548 "(id INTEGER PRIMARY KEY, value BLOB);",
167549 p->zDb, p->zName
167550 );
167551 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
167552}
167553
167554/*
167555** Create the backing store tables (%_content, %_segments and %_segdir)
167556** required by the FTS3 table passed as the only argument. This is done
167557** as part of the vtab xCreate() method.
167558**
167559** If the p->bHasDocsize boolean is true (indicating that this is an
167560** FTS4 table, not an FTS3 table) then also create the %_docsize and
167561** %_stat tables required by FTS4.
167562*/
167563static int fts3CreateTables(Fts3Table *p){
167564 int rc = SQLITE_OK; /* Return code */
167565 int i; /* Iterator variable */
167566 sqlite3 *db = p->db; /* The database connection */
167567
167568 if( p->zContentTbl==0 ){
167569 const char *zLanguageid = p->zLanguageid;
167570 char *zContentCols; /* Columns of %_content table */
167571
167572 /* Create a list of user columns for the content table */
167573 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
167574 for(i=0; zContentCols && i<p->nColumn; i++){
167575 char *z = p->azColumn[i];
167576 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
167577 }
167578 if( zLanguageid && zContentCols ){
167579 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
167580 }
167581 if( zContentCols==0 ) rc = SQLITE_NOMEM;
167582
167583 /* Create the content table */
167584 fts3DbExec(&rc, db,
167585 "CREATE TABLE %Q.'%q_content'(%s)",
167586 p->zDb, p->zName, zContentCols
167587 );
167588 sqlite3_free(zContentCols);
167589 }
167590
167591 /* Create other tables */
167592 fts3DbExec(&rc, db,
167593 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
167594 p->zDb, p->zName
167595 );
167596 fts3DbExec(&rc, db,
167597 "CREATE TABLE %Q.'%q_segdir'("
167598 "level INTEGER,"
167599 "idx INTEGER,"
167600 "start_block INTEGER,"
167601 "leaves_end_block INTEGER,"
167602 "end_block INTEGER,"
167603 "root BLOB,"
167604 "PRIMARY KEY(level, idx)"
167605 ");",
167606 p->zDb, p->zName
167607 );
167608 if( p->bHasDocsize ){
167609 fts3DbExec(&rc, db,
167610 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
167611 p->zDb, p->zName
167612 );
167613 }
167614 assert( p->bHasStat==p->bFts4 );
167615 if( p->bHasStat ){
167616 sqlite3Fts3CreateStatTable(&rc, p);
167617 }
167618 return rc;
167619}
167620
167621/*
167622** Store the current database page-size in bytes in p->nPgsz.
167623**
167624** If *pRc is non-zero when this function is called, it is a no-op.
167625** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
167626** before returning.
167627*/
167628static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
167629 if( *pRc==SQLITE_OK ){
167630 int rc; /* Return code */
167631 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
167632 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
167633
167634 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
167635 if( !zSql ){
167636 rc = SQLITE_NOMEM;
167637 }else{
167638 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
167639 if( rc==SQLITE_OK ){
167640 sqlite3_step(pStmt);
167641 p->nPgsz = sqlite3_column_int(pStmt, 0);
167642 rc = sqlite3_finalize(pStmt);
167643 }else if( rc==SQLITE_AUTH ){
167644 p->nPgsz = 1024;
167645 rc = SQLITE_OK;
167646 }
167647 }
167648 assert( p->nPgsz>0 || rc!=SQLITE_OK );
167649 sqlite3_free(zSql);
167650 *pRc = rc;
167651 }
167652}
167653
167654/*
167655** "Special" FTS4 arguments are column specifications of the following form:
167656**
167657** <key> = <value>
167658**
167659** There may not be whitespace surrounding the "=" character. The <value>
167660** term may be quoted, but the <key> may not.
167661*/
167662static int fts3IsSpecialColumn(
167663 const char *z,
167664 int *pnKey,
167665 char **pzValue
167666){
167667 char *zValue;
167668 const char *zCsr = z;
167669
167670 while( *zCsr!='=' ){
167671 if( *zCsr=='\0' ) return 0;
167672 zCsr++;
167673 }
167674
167675 *pnKey = (int)(zCsr-z);
167676 zValue = sqlite3_mprintf("%s", &zCsr[1]);
167677 if( zValue ){
167678 sqlite3Fts3Dequote(zValue);
167679 }
167680 *pzValue = zValue;
167681 return 1;
167682}
167683
167684/*
167685** Append the output of a printf() style formatting to an existing string.
167686*/
167687static void fts3Appendf(
167688 int *pRc, /* IN/OUT: Error code */
167689 char **pz, /* IN/OUT: Pointer to string buffer */
167690 const char *zFormat, /* Printf format string to append */
167691 ... /* Arguments for printf format string */
167692){
167693 if( *pRc==SQLITE_OK ){
167694 va_list ap;
167695 char *z;
167696 va_start(ap, zFormat);
167697 z = sqlite3_vmprintf(zFormat, ap);
167698 va_end(ap);
167699 if( z && *pz ){
167700 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
167701 sqlite3_free(z);
167702 z = z2;
167703 }
167704 if( z==0 ) *pRc = SQLITE_NOMEM;
167705 sqlite3_free(*pz);
167706 *pz = z;
167707 }
167708}
167709
167710/*
167711** Return a copy of input string zInput enclosed in double-quotes (") and
167712** with all double quote characters escaped. For example:
167713**
167714** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
167715**
167716** The pointer returned points to memory obtained from sqlite3_malloc(). It
167717** is the callers responsibility to call sqlite3_free() to release this
167718** memory.
167719*/
167720static char *fts3QuoteId(char const *zInput){
167721 sqlite3_int64 nRet;
167722 char *zRet;
167723 nRet = 2 + (int)strlen(zInput)*2 + 1;
167724 zRet = sqlite3_malloc64(nRet);
167725 if( zRet ){
167726 int i;
167727 char *z = zRet;
167728 *(z++) = '"';
167729 for(i=0; zInput[i]; i++){
167730 if( zInput[i]=='"' ) *(z++) = '"';
167731 *(z++) = zInput[i];
167732 }
167733 *(z++) = '"';
167734 *(z++) = '\0';
167735 }
167736 return zRet;
167737}
167738
167739/*
167740** Return a list of comma separated SQL expressions and a FROM clause that
167741** could be used in a SELECT statement such as the following:
167742**
167743** SELECT <list of expressions> FROM %_content AS x ...
167744**
167745** to return the docid, followed by each column of text data in order
167746** from left to write. If parameter zFunc is not NULL, then instead of
167747** being returned directly each column of text data is passed to an SQL
167748** function named zFunc first. For example, if zFunc is "unzip" and the
167749** table has the three user-defined columns "a", "b", and "c", the following
167750** string is returned:
167751**
167752** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
167753**
167754** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
167755** is the responsibility of the caller to eventually free it.
167756**
167757** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
167758** a NULL pointer is returned). Otherwise, if an OOM error is encountered
167759** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
167760** no error occurs, *pRc is left unmodified.
167761*/
167762static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
167763 char *zRet = 0;
167764 char *zFree = 0;
167765 char *zFunction;
167766 int i;
167767
167768 if( p->zContentTbl==0 ){
167769 if( !zFunc ){
167770 zFunction = "";
167771 }else{
167772 zFree = zFunction = fts3QuoteId(zFunc);
167773 }
167774 fts3Appendf(pRc, &zRet, "docid");
167775 for(i=0; i<p->nColumn; i++){
167776 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
167777 }
167778 if( p->zLanguageid ){
167779 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
167780 }
167781 sqlite3_free(zFree);
167782 }else{
167783 fts3Appendf(pRc, &zRet, "rowid");
167784 for(i=0; i<p->nColumn; i++){
167785 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
167786 }
167787 if( p->zLanguageid ){
167788 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
167789 }
167790 }
167791 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
167792 p->zDb,
167793 (p->zContentTbl ? p->zContentTbl : p->zName),
167794 (p->zContentTbl ? "" : "_content")
167795 );
167796 return zRet;
167797}
167798
167799/*
167800** Return a list of N comma separated question marks, where N is the number
167801** of columns in the %_content table (one for the docid plus one for each
167802** user-defined text column).
167803**
167804** If argument zFunc is not NULL, then all but the first question mark
167805** is preceded by zFunc and an open bracket, and followed by a closed
167806** bracket. For example, if zFunc is "zip" and the FTS3 table has three
167807** user-defined text columns, the following string is returned:
167808**
167809** "?, zip(?), zip(?), zip(?)"
167810**
167811** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
167812** is the responsibility of the caller to eventually free it.
167813**
167814** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
167815** a NULL pointer is returned). Otherwise, if an OOM error is encountered
167816** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
167817** no error occurs, *pRc is left unmodified.
167818*/
167819static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
167820 char *zRet = 0;
167821 char *zFree = 0;
167822 char *zFunction;
167823 int i;
167824
167825 if( !zFunc ){
167826 zFunction = "";
167827 }else{
167828 zFree = zFunction = fts3QuoteId(zFunc);
167829 }
167830 fts3Appendf(pRc, &zRet, "?");
167831 for(i=0; i<p->nColumn; i++){
167832 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
167833 }
167834 if( p->zLanguageid ){
167835 fts3Appendf(pRc, &zRet, ", ?");
167836 }
167837 sqlite3_free(zFree);
167838 return zRet;
167839}
167840
167841/*
167842** Buffer z contains a positive integer value encoded as utf-8 text.
167843** Decode this value and store it in *pnOut, returning the number of bytes
167844** consumed. If an overflow error occurs return a negative value.
167845*/
167846SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){
167847 u64 iVal = 0;
167848 int i;
167849 for(i=0; z[i]>='0' && z[i]<='9'; i++){
167850 iVal = iVal*10 + (z[i] - '0');
167851 if( iVal>0x7FFFFFFF ) return -1;
167852 }
167853 *pnOut = (int)iVal;
167854 return i;
167855}
167856
167857/*
167858** This function interprets the string at (*pp) as a non-negative integer
167859** value. It reads the integer and sets *pnOut to the value read, then
167860** sets *pp to point to the byte immediately following the last byte of
167861** the integer value.
167862**
167863** Only decimal digits ('0'..'9') may be part of an integer value.
167864**
167865** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
167866** the output value undefined. Otherwise SQLITE_OK is returned.
167867**
167868** This function is used when parsing the "prefix=" FTS4 parameter.
167869*/
167870static int fts3GobbleInt(const char **pp, int *pnOut){
167871 const int MAX_NPREFIX = 10000000;
167872 int nInt = 0; /* Output value */
167873 int nByte;
167874 nByte = sqlite3Fts3ReadInt(*pp, &nInt);
167875 if( nInt>MAX_NPREFIX ){
167876 nInt = 0;
167877 }
167878 if( nByte==0 ){
167879 return SQLITE_ERROR;
167880 }
167881 *pnOut = nInt;
167882 *pp += nByte;
167883 return SQLITE_OK;
167884}
167885
167886/*
167887** This function is called to allocate an array of Fts3Index structures
167888** representing the indexes maintained by the current FTS table. FTS tables
167889** always maintain the main "terms" index, but may also maintain one or
167890** more "prefix" indexes, depending on the value of the "prefix=" parameter
167891** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
167892**
167893** Argument zParam is passed the value of the "prefix=" option if one was
167894** specified, or NULL otherwise.
167895**
167896** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
167897** the allocated array. *pnIndex is set to the number of elements in the
167898** array. If an error does occur, an SQLite error code is returned.
167899**
167900** Regardless of whether or not an error is returned, it is the responsibility
167901** of the caller to call sqlite3_free() on the output array to free it.
167902*/
167903static int fts3PrefixParameter(
167904 const char *zParam, /* ABC in prefix=ABC parameter to parse */
167905 int *pnIndex, /* OUT: size of *apIndex[] array */
167906 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
167907){
167908 struct Fts3Index *aIndex; /* Allocated array */
167909 int nIndex = 1; /* Number of entries in array */
167910
167911 if( zParam && zParam[0] ){
167912 const char *p;
167913 nIndex++;
167914 for(p=zParam; *p; p++){
167915 if( *p==',' ) nIndex++;
167916 }
167917 }
167918
167919 aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
167920 *apIndex = aIndex;
167921 if( !aIndex ){
167922 return SQLITE_NOMEM;
167923 }
167924
167925 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
167926 if( zParam ){
167927 const char *p = zParam;
167928 int i;
167929 for(i=1; i<nIndex; i++){
167930 int nPrefix = 0;
167931 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
167932 assert( nPrefix>=0 );
167933 if( nPrefix==0 ){
167934 nIndex--;
167935 i--;
167936 }else{
167937 aIndex[i].nPrefix = nPrefix;
167938 }
167939 p++;
167940 }
167941 }
167942
167943 *pnIndex = nIndex;
167944 return SQLITE_OK;
167945}
167946
167947/*
167948** This function is called when initializing an FTS4 table that uses the
167949** content=xxx option. It determines the number of and names of the columns
167950** of the new FTS4 table.
167951**
167952** The third argument passed to this function is the value passed to the
167953** config=xxx option (i.e. "xxx"). This function queries the database for
167954** a table of that name. If found, the output variables are populated
167955** as follows:
167956**
167957** *pnCol: Set to the number of columns table xxx has,
167958**
167959** *pnStr: Set to the total amount of space required to store a copy
167960** of each columns name, including the nul-terminator.
167961**
167962** *pazCol: Set to point to an array of *pnCol strings. Each string is
167963** the name of the corresponding column in table xxx. The array
167964** and its contents are allocated using a single allocation. It
167965** is the responsibility of the caller to free this allocation
167966** by eventually passing the *pazCol value to sqlite3_free().
167967**
167968** If the table cannot be found, an error code is returned and the output
167969** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
167970** returned (and the output variables are undefined).
167971*/
167972static int fts3ContentColumns(
167973 sqlite3 *db, /* Database handle */
167974 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
167975 const char *zTbl, /* Name of content table */
167976 const char ***pazCol, /* OUT: Malloc'd array of column names */
167977 int *pnCol, /* OUT: Size of array *pazCol */
167978 int *pnStr, /* OUT: Bytes of string content */
167979 char **pzErr /* OUT: error message */
167980){
167981 int rc = SQLITE_OK; /* Return code */
167982 char *zSql; /* "SELECT *" statement on zTbl */
167983 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
167984
167985 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
167986 if( !zSql ){
167987 rc = SQLITE_NOMEM;
167988 }else{
167989 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
167990 if( rc!=SQLITE_OK ){
167991 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
167992 }
167993 }
167994 sqlite3_free(zSql);
167995
167996 if( rc==SQLITE_OK ){
167997 const char **azCol; /* Output array */
167998 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
167999 int nCol; /* Number of table columns */
168000 int i; /* Used to iterate through columns */
168001
168002 /* Loop through the returned columns. Set nStr to the number of bytes of
168003 ** space required to store a copy of each column name, including the
168004 ** nul-terminator byte. */
168005 nCol = sqlite3_column_count(pStmt);
168006 for(i=0; i<nCol; i++){
168007 const char *zCol = sqlite3_column_name(pStmt, i);
168008 nStr += strlen(zCol) + 1;
168009 }
168010
168011 /* Allocate and populate the array to return. */
168012 azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
168013 if( azCol==0 ){
168014 rc = SQLITE_NOMEM;
168015 }else{
168016 char *p = (char *)&azCol[nCol];
168017 for(i=0; i<nCol; i++){
168018 const char *zCol = sqlite3_column_name(pStmt, i);
168019 int n = (int)strlen(zCol)+1;
168020 memcpy(p, zCol, n);
168021 azCol[i] = p;
168022 p += n;
168023 }
168024 }
168025 sqlite3_finalize(pStmt);
168026
168027 /* Set the output variables. */
168028 *pnCol = nCol;
168029 *pnStr = nStr;
168030 *pazCol = azCol;
168031 }
168032
168033 return rc;
168034}
168035
168036/*
168037** This function is the implementation of both the xConnect and xCreate
168038** methods of the FTS3 virtual table.
168039**
168040** The argv[] array contains the following:
168041**
168042** argv[0] -> module name ("fts3" or "fts4")
168043** argv[1] -> database name
168044** argv[2] -> table name
168045** argv[...] -> "column name" and other module argument fields.
168046*/
168047static int fts3InitVtab(
168048 int isCreate, /* True for xCreate, false for xConnect */
168049 sqlite3 *db, /* The SQLite database connection */
168050 void *pAux, /* Hash table containing tokenizers */
168051 int argc, /* Number of elements in argv array */
168052 const char * const *argv, /* xCreate/xConnect argument array */
168053 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
168054 char **pzErr /* Write any error message here */
168055){
168056 Fts3Hash *pHash = (Fts3Hash *)pAux;
168057 Fts3Table *p = 0; /* Pointer to allocated vtab */
168058 int rc = SQLITE_OK; /* Return code */
168059 int i; /* Iterator variable */
168060 sqlite3_int64 nByte; /* Size of allocation used for *p */
168061 int iCol; /* Column index */
168062 int nString = 0; /* Bytes required to hold all column names */
168063 int nCol = 0; /* Number of columns in the FTS table */
168064 char *zCsr; /* Space for holding column names */
168065 int nDb; /* Bytes required to hold database name */
168066 int nName; /* Bytes required to hold table name */
168067 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
168068 const char **aCol; /* Array of column names */
168069 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
168070
168071 int nIndex = 0; /* Size of aIndex[] array */
168072 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
168073
168074 /* The results of parsing supported FTS4 key=value options: */
168075 int bNoDocsize = 0; /* True to omit %_docsize table */
168076 int bDescIdx = 0; /* True to store descending indexes */
168077 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
168078 char *zCompress = 0; /* compress=? parameter (or NULL) */
168079 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
168080 char *zContent = 0; /* content=? parameter (or NULL) */
168081 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
168082 char **azNotindexed = 0; /* The set of notindexed= columns */
168083 int nNotindexed = 0; /* Size of azNotindexed[] array */
168084
168085 assert( strlen(argv[0])==4 );
168086 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
168087 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
168088 );
168089
168090 nDb = (int)strlen(argv[1]) + 1;
168091 nName = (int)strlen(argv[2]) + 1;
168092
168093 nByte = sizeof(const char *) * (argc-2);
168094 aCol = (const char **)sqlite3_malloc64(nByte);
168095 if( aCol ){
168096 memset((void*)aCol, 0, nByte);
168097 azNotindexed = (char **)sqlite3_malloc64(nByte);
168098 }
168099 if( azNotindexed ){
168100 memset(azNotindexed, 0, nByte);
168101 }
168102 if( !aCol || !azNotindexed ){
168103 rc = SQLITE_NOMEM;
168104 goto fts3_init_out;
168105 }
168106
168107 /* Loop through all of the arguments passed by the user to the FTS3/4
168108 ** module (i.e. all the column names and special arguments). This loop
168109 ** does the following:
168110 **
168111 ** + Figures out the number of columns the FTSX table will have, and
168112 ** the number of bytes of space that must be allocated to store copies
168113 ** of the column names.
168114 **
168115 ** + If there is a tokenizer specification included in the arguments,
168116 ** initializes the tokenizer pTokenizer.
168117 */
168118 for(i=3; rc==SQLITE_OK && i<argc; i++){
168119 char const *z = argv[i];
168120 int nKey;
168121 char *zVal;
168122
168123 /* Check if this is a tokenizer specification */
168124 if( !pTokenizer
168125 && strlen(z)>8
168126 && 0==sqlite3_strnicmp(z, "tokenize", 8)
168127 && 0==sqlite3Fts3IsIdChar(z[8])
168128 ){
168129 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
168130 }
168131
168132 /* Check if it is an FTS4 special argument. */
168133 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
168134 struct Fts4Option {
168135 const char *zOpt;
168136 int nOpt;
168137 } aFts4Opt[] = {
168138 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
168139 { "prefix", 6 }, /* 1 -> PREFIX */
168140 { "compress", 8 }, /* 2 -> COMPRESS */
168141 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
168142 { "order", 5 }, /* 4 -> ORDER */
168143 { "content", 7 }, /* 5 -> CONTENT */
168144 { "languageid", 10 }, /* 6 -> LANGUAGEID */
168145 { "notindexed", 10 } /* 7 -> NOTINDEXED */
168146 };
168147
168148 int iOpt;
168149 if( !zVal ){
168150 rc = SQLITE_NOMEM;
168151 }else{
168152 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
168153 struct Fts4Option *pOp = &aFts4Opt[iOpt];
168154 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
168155 break;
168156 }
168157 }
168158 switch( iOpt ){
168159 case 0: /* MATCHINFO */
168160 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
168161 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
168162 rc = SQLITE_ERROR;
168163 }
168164 bNoDocsize = 1;
168165 break;
168166
168167 case 1: /* PREFIX */
168168 sqlite3_free(zPrefix);
168169 zPrefix = zVal;
168170 zVal = 0;
168171 break;
168172
168173 case 2: /* COMPRESS */
168174 sqlite3_free(zCompress);
168175 zCompress = zVal;
168176 zVal = 0;
168177 break;
168178
168179 case 3: /* UNCOMPRESS */
168180 sqlite3_free(zUncompress);
168181 zUncompress = zVal;
168182 zVal = 0;
168183 break;
168184
168185 case 4: /* ORDER */
168186 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
168187 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
168188 ){
168189 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
168190 rc = SQLITE_ERROR;
168191 }
168192 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
168193 break;
168194
168195 case 5: /* CONTENT */
168196 sqlite3_free(zContent);
168197 zContent = zVal;
168198 zVal = 0;
168199 break;
168200
168201 case 6: /* LANGUAGEID */
168202 assert( iOpt==6 );
168203 sqlite3_free(zLanguageid);
168204 zLanguageid = zVal;
168205 zVal = 0;
168206 break;
168207
168208 case 7: /* NOTINDEXED */
168209 azNotindexed[nNotindexed++] = zVal;
168210 zVal = 0;
168211 break;
168212
168213 default:
168214 assert( iOpt==SizeofArray(aFts4Opt) );
168215 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
168216 rc = SQLITE_ERROR;
168217 break;
168218 }
168219 sqlite3_free(zVal);
168220 }
168221 }
168222
168223 /* Otherwise, the argument is a column name. */
168224 else {
168225 nString += (int)(strlen(z) + 1);
168226 aCol[nCol++] = z;
168227 }
168228 }
168229
168230 /* If a content=xxx option was specified, the following:
168231 **
168232 ** 1. Ignore any compress= and uncompress= options.
168233 **
168234 ** 2. If no column names were specified as part of the CREATE VIRTUAL
168235 ** TABLE statement, use all columns from the content table.
168236 */
168237 if( rc==SQLITE_OK && zContent ){
168238 sqlite3_free(zCompress);
168239 sqlite3_free(zUncompress);
168240 zCompress = 0;
168241 zUncompress = 0;
168242 if( nCol==0 ){
168243 sqlite3_free((void*)aCol);
168244 aCol = 0;
168245 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
168246
168247 /* If a languageid= option was specified, remove the language id
168248 ** column from the aCol[] array. */
168249 if( rc==SQLITE_OK && zLanguageid ){
168250 int j;
168251 for(j=0; j<nCol; j++){
168252 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
168253 int k;
168254 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
168255 nCol--;
168256 break;
168257 }
168258 }
168259 }
168260 }
168261 }
168262 if( rc!=SQLITE_OK ) goto fts3_init_out;
168263
168264 if( nCol==0 ){
168265 assert( nString==0 );
168266 aCol[0] = "content";
168267 nString = 8;
168268 nCol = 1;
168269 }
168270
168271 if( pTokenizer==0 ){
168272 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
168273 if( rc!=SQLITE_OK ) goto fts3_init_out;
168274 }
168275 assert( pTokenizer );
168276
168277 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
168278 if( rc==SQLITE_ERROR ){
168279 assert( zPrefix );
168280 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
168281 }
168282 if( rc!=SQLITE_OK ) goto fts3_init_out;
168283
168284 /* Allocate and populate the Fts3Table structure. */
168285 nByte = sizeof(Fts3Table) + /* Fts3Table */
168286 nCol * sizeof(char *) + /* azColumn */
168287 nIndex * sizeof(struct Fts3Index) + /* aIndex */
168288 nCol * sizeof(u8) + /* abNotindexed */
168289 nName + /* zName */
168290 nDb + /* zDb */
168291 nString; /* Space for azColumn strings */
168292 p = (Fts3Table*)sqlite3_malloc64(nByte);
168293 if( p==0 ){
168294 rc = SQLITE_NOMEM;
168295 goto fts3_init_out;
168296 }
168297 memset(p, 0, nByte);
168298 p->db = db;
168299 p->nColumn = nCol;
168300 p->nPendingData = 0;
168301 p->azColumn = (char **)&p[1];
168302 p->pTokenizer = pTokenizer;
168303 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
168304 p->bHasDocsize = (isFts4 && bNoDocsize==0);
168305 p->bHasStat = (u8)isFts4;
168306 p->bFts4 = (u8)isFts4;
168307 p->bDescIdx = (u8)bDescIdx;
168308 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
168309 p->zContentTbl = zContent;
168310 p->zLanguageid = zLanguageid;
168311 zContent = 0;
168312 zLanguageid = 0;
168313 TESTONLY( p->inTransaction = -1 );
168314 TESTONLY( p->mxSavepoint = -1 );
168315
168316 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
168317 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
168318 p->nIndex = nIndex;
168319 for(i=0; i<nIndex; i++){
168320 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
168321 }
168322 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
168323
168324 /* Fill in the zName and zDb fields of the vtab structure. */
168325 zCsr = (char *)&p->abNotindexed[nCol];
168326 p->zName = zCsr;
168327 memcpy(zCsr, argv[2], nName);
168328 zCsr += nName;
168329 p->zDb = zCsr;
168330 memcpy(zCsr, argv[1], nDb);
168331 zCsr += nDb;
168332
168333 /* Fill in the azColumn array */
168334 for(iCol=0; iCol<nCol; iCol++){
168335 char *z;
168336 int n = 0;
168337 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
168338 if( n>0 ){
168339 memcpy(zCsr, z, n);
168340 }
168341 zCsr[n] = '\0';
168342 sqlite3Fts3Dequote(zCsr);
168343 p->azColumn[iCol] = zCsr;
168344 zCsr += n+1;
168345 assert( zCsr <= &((char *)p)[nByte] );
168346 }
168347
168348 /* Fill in the abNotindexed array */
168349 for(iCol=0; iCol<nCol; iCol++){
168350 int n = (int)strlen(p->azColumn[iCol]);
168351 for(i=0; i<nNotindexed; i++){
168352 char *zNot = azNotindexed[i];
168353 if( zNot && n==(int)strlen(zNot)
168354 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
168355 ){
168356 p->abNotindexed[iCol] = 1;
168357 sqlite3_free(zNot);
168358 azNotindexed[i] = 0;
168359 }
168360 }
168361 }
168362 for(i=0; i<nNotindexed; i++){
168363 if( azNotindexed[i] ){
168364 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
168365 rc = SQLITE_ERROR;
168366 }
168367 }
168368
168369 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
168370 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
168371 rc = SQLITE_ERROR;
168372 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
168373 }
168374 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
168375 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
168376 if( rc!=SQLITE_OK ) goto fts3_init_out;
168377
168378 /* If this is an xCreate call, create the underlying tables in the
168379 ** database. TODO: For xConnect(), it could verify that said tables exist.
168380 */
168381 if( isCreate ){
168382 rc = fts3CreateTables(p);
168383 }
168384
168385 /* Check to see if a legacy fts3 table has been "upgraded" by the
168386 ** addition of a %_stat table so that it can use incremental merge.
168387 */
168388 if( !isFts4 && !isCreate ){
168389 p->bHasStat = 2;
168390 }
168391
168392 /* Figure out the page-size for the database. This is required in order to
168393 ** estimate the cost of loading large doclists from the database. */
168394 fts3DatabasePageSize(&rc, p);
168395 p->nNodeSize = p->nPgsz-35;
168396
168397#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
168398 p->nMergeCount = FTS3_MERGE_COUNT;
168399#endif
168400
168401 /* Declare the table schema to SQLite. */
168402 fts3DeclareVtab(&rc, p);
168403
168404fts3_init_out:
168405 sqlite3_free(zPrefix);
168406 sqlite3_free(aIndex);
168407 sqlite3_free(zCompress);
168408 sqlite3_free(zUncompress);
168409 sqlite3_free(zContent);
168410 sqlite3_free(zLanguageid);
168411 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
168412 sqlite3_free((void *)aCol);
168413 sqlite3_free((void *)azNotindexed);
168414 if( rc!=SQLITE_OK ){
168415 if( p ){
168416 fts3DisconnectMethod((sqlite3_vtab *)p);
168417 }else if( pTokenizer ){
168418 pTokenizer->pModule->xDestroy(pTokenizer);
168419 }
168420 }else{
168421 assert( p->pSegments==0 );
168422 *ppVTab = &p->base;
168423 }
168424 return rc;
168425}
168426
168427/*
168428** The xConnect() and xCreate() methods for the virtual table. All the
168429** work is done in function fts3InitVtab().
168430*/
168431static int fts3ConnectMethod(
168432 sqlite3 *db, /* Database connection */
168433 void *pAux, /* Pointer to tokenizer hash table */
168434 int argc, /* Number of elements in argv array */
168435 const char * const *argv, /* xCreate/xConnect argument array */
168436 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
168437 char **pzErr /* OUT: sqlite3_malloc'd error message */
168438){
168439 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
168440}
168441static int fts3CreateMethod(
168442 sqlite3 *db, /* Database connection */
168443 void *pAux, /* Pointer to tokenizer hash table */
168444 int argc, /* Number of elements in argv array */
168445 const char * const *argv, /* xCreate/xConnect argument array */
168446 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
168447 char **pzErr /* OUT: sqlite3_malloc'd error message */
168448){
168449 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
168450}
168451
168452/*
168453** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
168454** extension is currently being used by a version of SQLite too old to
168455** support estimatedRows. In that case this function is a no-op.
168456*/
168457static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
168458#if SQLITE_VERSION_NUMBER>=3008002
168459 if( sqlite3_libversion_number()>=3008002 ){
168460 pIdxInfo->estimatedRows = nRow;
168461 }
168462#endif
168463}
168464
168465/*
168466** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
168467** extension is currently being used by a version of SQLite too old to
168468** support index-info flags. In that case this function is a no-op.
168469*/
168470static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
168471#if SQLITE_VERSION_NUMBER>=3008012
168472 if( sqlite3_libversion_number()>=3008012 ){
168473 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
168474 }
168475#endif
168476}
168477
168478/*
168479** Implementation of the xBestIndex method for FTS3 tables. There
168480** are three possible strategies, in order of preference:
168481**
168482** 1. Direct lookup by rowid or docid.
168483** 2. Full-text search using a MATCH operator on a non-docid column.
168484** 3. Linear scan of %_content table.
168485*/
168486static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
168487 Fts3Table *p = (Fts3Table *)pVTab;
168488 int i; /* Iterator variable */
168489 int iCons = -1; /* Index of constraint to use */
168490
168491 int iLangidCons = -1; /* Index of langid=x constraint, if present */
168492 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
168493 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
168494 int iIdx;
168495
168496 if( p->bLock ){
168497 return SQLITE_ERROR;
168498 }
168499
168500 /* By default use a full table scan. This is an expensive option,
168501 ** so search through the constraints to see if a more efficient
168502 ** strategy is possible.
168503 */
168504 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
168505 pInfo->estimatedCost = 5000000;
168506 for(i=0; i<pInfo->nConstraint; i++){
168507 int bDocid; /* True if this constraint is on docid */
168508 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
168509 if( pCons->usable==0 ){
168510 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
168511 /* There exists an unusable MATCH constraint. This means that if
168512 ** the planner does elect to use the results of this call as part
168513 ** of the overall query plan the user will see an "unable to use
168514 ** function MATCH in the requested context" error. To discourage
168515 ** this, return a very high cost here. */
168516 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
168517 pInfo->estimatedCost = 1e50;
168518 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
168519 return SQLITE_OK;
168520 }
168521 continue;
168522 }
168523
168524 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
168525
168526 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
168527 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
168528 pInfo->idxNum = FTS3_DOCID_SEARCH;
168529 pInfo->estimatedCost = 1.0;
168530 iCons = i;
168531 }
168532
168533 /* A MATCH constraint. Use a full-text search.
168534 **
168535 ** If there is more than one MATCH constraint available, use the first
168536 ** one encountered. If there is both a MATCH constraint and a direct
168537 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
168538 ** though the rowid/docid lookup is faster than a MATCH query, selecting
168539 ** it would lead to an "unable to use function MATCH in the requested
168540 ** context" error.
168541 */
168542 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
168543 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
168544 ){
168545 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
168546 pInfo->estimatedCost = 2.0;
168547 iCons = i;
168548 }
168549
168550 /* Equality constraint on the langid column */
168551 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
168552 && pCons->iColumn==p->nColumn + 2
168553 ){
168554 iLangidCons = i;
168555 }
168556
168557 if( bDocid ){
168558 switch( pCons->op ){
168559 case SQLITE_INDEX_CONSTRAINT_GE:
168560 case SQLITE_INDEX_CONSTRAINT_GT:
168561 iDocidGe = i;
168562 break;
168563
168564 case SQLITE_INDEX_CONSTRAINT_LE:
168565 case SQLITE_INDEX_CONSTRAINT_LT:
168566 iDocidLe = i;
168567 break;
168568 }
168569 }
168570 }
168571
168572 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
168573 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
168574
168575 iIdx = 1;
168576 if( iCons>=0 ){
168577 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
168578 pInfo->aConstraintUsage[iCons].omit = 1;
168579 }
168580 if( iLangidCons>=0 ){
168581 pInfo->idxNum |= FTS3_HAVE_LANGID;
168582 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
168583 }
168584 if( iDocidGe>=0 ){
168585 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
168586 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
168587 }
168588 if( iDocidLe>=0 ){
168589 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
168590 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
168591 }
168592
168593 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
168594 ** docid) order. Both ascending and descending are possible.
168595 */
168596 if( pInfo->nOrderBy==1 ){
168597 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
168598 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
168599 if( pOrder->desc ){
168600 pInfo->idxStr = "DESC";
168601 }else{
168602 pInfo->idxStr = "ASC";
168603 }
168604 pInfo->orderByConsumed = 1;
168605 }
168606 }
168607
168608 assert( p->pSegments==0 );
168609 return SQLITE_OK;
168610}
168611
168612/*
168613** Implementation of xOpen method.
168614*/
168615static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
168616 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
168617
168618 UNUSED_PARAMETER(pVTab);
168619
168620 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
168621 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
168622 ** if the allocation fails, return SQLITE_NOMEM.
168623 */
168624 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
168625 if( !pCsr ){
168626 return SQLITE_NOMEM;
168627 }
168628 memset(pCsr, 0, sizeof(Fts3Cursor));
168629 return SQLITE_OK;
168630}
168631
168632/*
168633** Finalize the statement handle at pCsr->pStmt.
168634**
168635** Or, if that statement handle is one created by fts3CursorSeekStmt(),
168636** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
168637** pointer there instead of finalizing it.
168638*/
168639static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
168640 if( pCsr->bSeekStmt ){
168641 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
168642 if( p->pSeekStmt==0 ){
168643 p->pSeekStmt = pCsr->pStmt;
168644 sqlite3_reset(pCsr->pStmt);
168645 pCsr->pStmt = 0;
168646 }
168647 pCsr->bSeekStmt = 0;
168648 }
168649 sqlite3_finalize(pCsr->pStmt);
168650}
168651
168652/*
168653** Free all resources currently held by the cursor passed as the only
168654** argument.
168655*/
168656static void fts3ClearCursor(Fts3Cursor *pCsr){
168657 fts3CursorFinalizeStmt(pCsr);
168658 sqlite3Fts3FreeDeferredTokens(pCsr);
168659 sqlite3_free(pCsr->aDoclist);
168660 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
168661 sqlite3Fts3ExprFree(pCsr->pExpr);
168662 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
168663}
168664
168665/*
168666** Close the cursor. For additional information see the documentation
168667** on the xClose method of the virtual table interface.
168668*/
168669static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
168670 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
168671 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
168672 fts3ClearCursor(pCsr);
168673 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
168674 sqlite3_free(pCsr);
168675 return SQLITE_OK;
168676}
168677
168678/*
168679** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
168680** compose and prepare an SQL statement of the form:
168681**
168682** "SELECT <columns> FROM %_content WHERE rowid = ?"
168683**
168684** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
168685** it. If an error occurs, return an SQLite error code.
168686*/
168687static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
168688 int rc = SQLITE_OK;
168689 if( pCsr->pStmt==0 ){
168690 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
168691 char *zSql;
168692 if( p->pSeekStmt ){
168693 pCsr->pStmt = p->pSeekStmt;
168694 p->pSeekStmt = 0;
168695 }else{
168696 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
168697 if( !zSql ) return SQLITE_NOMEM;
168698 p->bLock++;
168699 rc = sqlite3_prepare_v3(
168700 p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
168701 );
168702 p->bLock--;
168703 sqlite3_free(zSql);
168704 }
168705 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
168706 }
168707 return rc;
168708}
168709
168710/*
168711** Position the pCsr->pStmt statement so that it is on the row
168712** of the %_content table that contains the last match. Return
168713** SQLITE_OK on success.
168714*/
168715static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
168716 int rc = SQLITE_OK;
168717 if( pCsr->isRequireSeek ){
168718 rc = fts3CursorSeekStmt(pCsr);
168719 if( rc==SQLITE_OK ){
168720 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
168721 pTab->bLock++;
168722 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
168723 pCsr->isRequireSeek = 0;
168724 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
168725 pTab->bLock--;
168726 return SQLITE_OK;
168727 }else{
168728 pTab->bLock--;
168729 rc = sqlite3_reset(pCsr->pStmt);
168730 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
168731 /* If no row was found and no error has occurred, then the %_content
168732 ** table is missing a row that is present in the full-text index.
168733 ** The data structures are corrupt. */
168734 rc = FTS_CORRUPT_VTAB;
168735 pCsr->isEof = 1;
168736 }
168737 }
168738 }
168739 }
168740
168741 if( rc!=SQLITE_OK && pContext ){
168742 sqlite3_result_error_code(pContext, rc);
168743 }
168744 return rc;
168745}
168746
168747/*
168748** This function is used to process a single interior node when searching
168749** a b-tree for a term or term prefix. The node data is passed to this
168750** function via the zNode/nNode parameters. The term to search for is
168751** passed in zTerm/nTerm.
168752**
168753** If piFirst is not NULL, then this function sets *piFirst to the blockid
168754** of the child node that heads the sub-tree that may contain the term.
168755**
168756** If piLast is not NULL, then *piLast is set to the right-most child node
168757** that heads a sub-tree that may contain a term for which zTerm/nTerm is
168758** a prefix.
168759**
168760** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
168761*/
168762static int fts3ScanInteriorNode(
168763 const char *zTerm, /* Term to select leaves for */
168764 int nTerm, /* Size of term zTerm in bytes */
168765 const char *zNode, /* Buffer containing segment interior node */
168766 int nNode, /* Size of buffer at zNode */
168767 sqlite3_int64 *piFirst, /* OUT: Selected child node */
168768 sqlite3_int64 *piLast /* OUT: Selected child node */
168769){
168770 int rc = SQLITE_OK; /* Return code */
168771 const char *zCsr = zNode; /* Cursor to iterate through node */
168772 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
168773 char *zBuffer = 0; /* Buffer to load terms into */
168774 i64 nAlloc = 0; /* Size of allocated buffer */
168775 int isFirstTerm = 1; /* True when processing first term on page */
168776 sqlite3_int64 iChild; /* Block id of child node to descend to */
168777 int nBuffer = 0; /* Total term size */
168778
168779 /* Skip over the 'height' varint that occurs at the start of every
168780 ** interior node. Then load the blockid of the left-child of the b-tree
168781 ** node into variable iChild.
168782 **
168783 ** Even if the data structure on disk is corrupted, this (reading two
168784 ** varints from the buffer) does not risk an overread. If zNode is a
168785 ** root node, then the buffer comes from a SELECT statement. SQLite does
168786 ** not make this guarantee explicitly, but in practice there are always
168787 ** either more than 20 bytes of allocated space following the nNode bytes of
168788 ** contents, or two zero bytes. Or, if the node is read from the %_segments
168789 ** table, then there are always 20 bytes of zeroed padding following the
168790 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
168791 */
168792 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
168793 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
168794 if( zCsr>zEnd ){
168795 return FTS_CORRUPT_VTAB;
168796 }
168797
168798 while( zCsr<zEnd && (piFirst || piLast) ){
168799 int cmp; /* memcmp() result */
168800 int nSuffix; /* Size of term suffix */
168801 int nPrefix = 0; /* Size of term prefix */
168802
168803 /* Load the next term on the node into zBuffer. Use realloc() to expand
168804 ** the size of zBuffer if required. */
168805 if( !isFirstTerm ){
168806 zCsr += fts3GetVarint32(zCsr, &nPrefix);
168807 if( nPrefix>nBuffer ){
168808 rc = FTS_CORRUPT_VTAB;
168809 goto finish_scan;
168810 }
168811 }
168812 isFirstTerm = 0;
168813 zCsr += fts3GetVarint32(zCsr, &nSuffix);
168814
168815 assert( nPrefix>=0 && nSuffix>=0 );
168816 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
168817 rc = FTS_CORRUPT_VTAB;
168818 goto finish_scan;
168819 }
168820 if( (i64)nPrefix+nSuffix>nAlloc ){
168821 char *zNew;
168822 nAlloc = ((i64)nPrefix+nSuffix) * 2;
168823 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
168824 if( !zNew ){
168825 rc = SQLITE_NOMEM;
168826 goto finish_scan;
168827 }
168828 zBuffer = zNew;
168829 }
168830 assert( zBuffer );
168831 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
168832 nBuffer = nPrefix + nSuffix;
168833 zCsr += nSuffix;
168834
168835 /* Compare the term we are searching for with the term just loaded from
168836 ** the interior node. If the specified term is greater than or equal
168837 ** to the term from the interior node, then all terms on the sub-tree
168838 ** headed by node iChild are smaller than zTerm. No need to search
168839 ** iChild.
168840 **
168841 ** If the interior node term is larger than the specified term, then
168842 ** the tree headed by iChild may contain the specified term.
168843 */
168844 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
168845 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
168846 *piFirst = iChild;
168847 piFirst = 0;
168848 }
168849
168850 if( piLast && cmp<0 ){
168851 *piLast = iChild;
168852 piLast = 0;
168853 }
168854
168855 iChild++;
168856 };
168857
168858 if( piFirst ) *piFirst = iChild;
168859 if( piLast ) *piLast = iChild;
168860
168861 finish_scan:
168862 sqlite3_free(zBuffer);
168863 return rc;
168864}
168865
168866
168867/*
168868** The buffer pointed to by argument zNode (size nNode bytes) contains an
168869** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
168870** contains a term. This function searches the sub-tree headed by the zNode
168871** node for the range of leaf nodes that may contain the specified term
168872** or terms for which the specified term is a prefix.
168873**
168874** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
168875** left-most leaf node in the tree that may contain the specified term.
168876** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
168877** right-most leaf node that may contain a term for which the specified
168878** term is a prefix.
168879**
168880** It is possible that the range of returned leaf nodes does not contain
168881** the specified term or any terms for which it is a prefix. However, if the
168882** segment does contain any such terms, they are stored within the identified
168883** range. Because this function only inspects interior segment nodes (and
168884** never loads leaf nodes into memory), it is not possible to be sure.
168885**
168886** If an error occurs, an error code other than SQLITE_OK is returned.
168887*/
168888static int fts3SelectLeaf(
168889 Fts3Table *p, /* Virtual table handle */
168890 const char *zTerm, /* Term to select leaves for */
168891 int nTerm, /* Size of term zTerm in bytes */
168892 const char *zNode, /* Buffer containing segment interior node */
168893 int nNode, /* Size of buffer at zNode */
168894 sqlite3_int64 *piLeaf, /* Selected leaf node */
168895 sqlite3_int64 *piLeaf2 /* Selected leaf node */
168896){
168897 int rc = SQLITE_OK; /* Return code */
168898 int iHeight; /* Height of this node in tree */
168899
168900 assert( piLeaf || piLeaf2 );
168901
168902 fts3GetVarint32(zNode, &iHeight);
168903 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
168904 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
168905
168906 if( rc==SQLITE_OK && iHeight>1 ){
168907 char *zBlob = 0; /* Blob read from %_segments table */
168908 int nBlob = 0; /* Size of zBlob in bytes */
168909
168910 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
168911 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
168912 if( rc==SQLITE_OK ){
168913 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
168914 }
168915 sqlite3_free(zBlob);
168916 piLeaf = 0;
168917 zBlob = 0;
168918 }
168919
168920 if( rc==SQLITE_OK ){
168921 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
168922 }
168923 if( rc==SQLITE_OK ){
168924 int iNewHeight = 0;
168925 fts3GetVarint32(zBlob, &iNewHeight);
168926 if( iNewHeight>=iHeight ){
168927 rc = FTS_CORRUPT_VTAB;
168928 }else{
168929 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
168930 }
168931 }
168932 sqlite3_free(zBlob);
168933 }
168934
168935 return rc;
168936}
168937
168938/*
168939** This function is used to create delta-encoded serialized lists of FTS3
168940** varints. Each call to this function appends a single varint to a list.
168941*/
168942static void fts3PutDeltaVarint(
168943 char **pp, /* IN/OUT: Output pointer */
168944 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
168945 sqlite3_int64 iVal /* Write this value to the list */
168946){
168947 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
168948 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
168949 *piPrev = iVal;
168950}
168951
168952/*
168953** When this function is called, *ppPoslist is assumed to point to the
168954** start of a position-list. After it returns, *ppPoslist points to the
168955** first byte after the position-list.
168956**
168957** A position list is list of positions (delta encoded) and columns for
168958** a single document record of a doclist. So, in other words, this
168959** routine advances *ppPoslist so that it points to the next docid in
168960** the doclist, or to the first byte past the end of the doclist.
168961**
168962** If pp is not NULL, then the contents of the position list are copied
168963** to *pp. *pp is set to point to the first byte past the last byte copied
168964** before this function returns.
168965*/
168966static void fts3PoslistCopy(char **pp, char **ppPoslist){
168967 char *pEnd = *ppPoslist;
168968 char c = 0;
168969
168970 /* The end of a position list is marked by a zero encoded as an FTS3
168971 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
168972 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
168973 ** of some other, multi-byte, value.
168974 **
168975 ** The following while-loop moves pEnd to point to the first byte that is not
168976 ** immediately preceded by a byte with the 0x80 bit set. Then increments
168977 ** pEnd once more so that it points to the byte immediately following the
168978 ** last byte in the position-list.
168979 */
168980 while( *pEnd | c ){
168981 c = *pEnd++ & 0x80;
168982 testcase( c!=0 && (*pEnd)==0 );
168983 }
168984 pEnd++; /* Advance past the POS_END terminator byte */
168985
168986 if( pp ){
168987 int n = (int)(pEnd - *ppPoslist);
168988 char *p = *pp;
168989 memcpy(p, *ppPoslist, n);
168990 p += n;
168991 *pp = p;
168992 }
168993 *ppPoslist = pEnd;
168994}
168995
168996/*
168997** When this function is called, *ppPoslist is assumed to point to the
168998** start of a column-list. After it returns, *ppPoslist points to the
168999** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
169000**
169001** A column-list is list of delta-encoded positions for a single column
169002** within a single document within a doclist.
169003**
169004** The column-list is terminated either by a POS_COLUMN varint (1) or
169005** a POS_END varint (0). This routine leaves *ppPoslist pointing to
169006** the POS_COLUMN or POS_END that terminates the column-list.
169007**
169008** If pp is not NULL, then the contents of the column-list are copied
169009** to *pp. *pp is set to point to the first byte past the last byte copied
169010** before this function returns. The POS_COLUMN or POS_END terminator
169011** is not copied into *pp.
169012*/
169013static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
169014 char *pEnd = *ppPoslist;
169015 char c = 0;
169016
169017 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
169018 ** not part of a multi-byte varint.
169019 */
169020 while( 0xFE & (*pEnd | c) ){
169021 c = *pEnd++ & 0x80;
169022 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
169023 }
169024 if( pp ){
169025 int n = (int)(pEnd - *ppPoslist);
169026 char *p = *pp;
169027 memcpy(p, *ppPoslist, n);
169028 p += n;
169029 *pp = p;
169030 }
169031 *ppPoslist = pEnd;
169032}
169033
169034/*
169035** Value used to signify the end of an position-list. This must be
169036** as large or larger than any value that might appear on the
169037** position-list, even a position list that has been corrupted.
169038*/
169039#define POSITION_LIST_END LARGEST_INT64
169040
169041/*
169042** This function is used to help parse position-lists. When this function is
169043** called, *pp may point to the start of the next varint in the position-list
169044** being parsed, or it may point to 1 byte past the end of the position-list
169045** (in which case **pp will be a terminator bytes POS_END (0) or
169046** (1)).
169047**
169048** If *pp points past the end of the current position-list, set *pi to
169049** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
169050** increment the current value of *pi by the value read, and set *pp to
169051** point to the next value before returning.
169052**
169053** Before calling this routine *pi must be initialized to the value of
169054** the previous position, or zero if we are reading the first position
169055** in the position-list. Because positions are delta-encoded, the value
169056** of the previous position is needed in order to compute the value of
169057** the next position.
169058*/
169059static void fts3ReadNextPos(
169060 char **pp, /* IN/OUT: Pointer into position-list buffer */
169061 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
169062){
169063 if( (**pp)&0xFE ){
169064 int iVal;
169065 *pp += fts3GetVarint32((*pp), &iVal);
169066 *pi += iVal;
169067 *pi -= 2;
169068 }else{
169069 *pi = POSITION_LIST_END;
169070 }
169071}
169072
169073/*
169074** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
169075** the value of iCol encoded as a varint to *pp. This will start a new
169076** column list.
169077**
169078** Set *pp to point to the byte just after the last byte written before
169079** returning (do not modify it if iCol==0). Return the total number of bytes
169080** written (0 if iCol==0).
169081*/
169082static int fts3PutColNumber(char **pp, int iCol){
169083 int n = 0; /* Number of bytes written */
169084 if( iCol ){
169085 char *p = *pp; /* Output pointer */
169086 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
169087 *p = 0x01;
169088 *pp = &p[n];
169089 }
169090 return n;
169091}
169092
169093/*
169094** Compute the union of two position lists. The output written
169095** into *pp contains all positions of both *pp1 and *pp2 in sorted
169096** order and with any duplicates removed. All pointers are
169097** updated appropriately. The caller is responsible for insuring
169098** that there is enough space in *pp to hold the complete output.
169099*/
169100static int fts3PoslistMerge(
169101 char **pp, /* Output buffer */
169102 char **pp1, /* Left input list */
169103 char **pp2 /* Right input list */
169104){
169105 char *p = *pp;
169106 char *p1 = *pp1;
169107 char *p2 = *pp2;
169108
169109 while( *p1 || *p2 ){
169110 int iCol1; /* The current column index in pp1 */
169111 int iCol2; /* The current column index in pp2 */
169112
169113 if( *p1==POS_COLUMN ){
169114 fts3GetVarint32(&p1[1], &iCol1);
169115 if( iCol1==0 ) return FTS_CORRUPT_VTAB;
169116 }
169117 else if( *p1==POS_END ) iCol1 = 0x7fffffff;
169118 else iCol1 = 0;
169119
169120 if( *p2==POS_COLUMN ){
169121 fts3GetVarint32(&p2[1], &iCol2);
169122 if( iCol2==0 ) return FTS_CORRUPT_VTAB;
169123 }
169124 else if( *p2==POS_END ) iCol2 = 0x7fffffff;
169125 else iCol2 = 0;
169126
169127 if( iCol1==iCol2 ){
169128 sqlite3_int64 i1 = 0; /* Last position from pp1 */
169129 sqlite3_int64 i2 = 0; /* Last position from pp2 */
169130 sqlite3_int64 iPrev = 0;
169131 int n = fts3PutColNumber(&p, iCol1);
169132 p1 += n;
169133 p2 += n;
169134
169135 /* At this point, both p1 and p2 point to the start of column-lists
169136 ** for the same column (the column with index iCol1 and iCol2).
169137 ** A column-list is a list of non-negative delta-encoded varints, each
169138 ** incremented by 2 before being stored. Each list is terminated by a
169139 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
169140 ** and writes the results to buffer p. p is left pointing to the byte
169141 ** after the list written. No terminator (POS_END or POS_COLUMN) is
169142 ** written to the output.
169143 */
169144 fts3GetDeltaVarint(&p1, &i1);
169145 fts3GetDeltaVarint(&p2, &i2);
169146 if( i1<2 || i2<2 ){
169147 break;
169148 }
169149 do {
169150 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
169151 iPrev -= 2;
169152 if( i1==i2 ){
169153 fts3ReadNextPos(&p1, &i1);
169154 fts3ReadNextPos(&p2, &i2);
169155 }else if( i1<i2 ){
169156 fts3ReadNextPos(&p1, &i1);
169157 }else{
169158 fts3ReadNextPos(&p2, &i2);
169159 }
169160 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
169161 }else if( iCol1<iCol2 ){
169162 p1 += fts3PutColNumber(&p, iCol1);
169163 fts3ColumnlistCopy(&p, &p1);
169164 }else{
169165 p2 += fts3PutColNumber(&p, iCol2);
169166 fts3ColumnlistCopy(&p, &p2);
169167 }
169168 }
169169
169170 *p++ = POS_END;
169171 *pp = p;
169172 *pp1 = p1 + 1;
169173 *pp2 = p2 + 1;
169174 return SQLITE_OK;
169175}
169176
169177/*
169178** This function is used to merge two position lists into one. When it is
169179** called, *pp1 and *pp2 must both point to position lists. A position-list is
169180** the part of a doclist that follows each document id. For example, if a row
169181** contains:
169182**
169183** 'a b c'|'x y z'|'a b b a'
169184**
169185** Then the position list for this row for token 'b' would consist of:
169186**
169187** 0x02 0x01 0x02 0x03 0x03 0x00
169188**
169189** When this function returns, both *pp1 and *pp2 are left pointing to the
169190** byte following the 0x00 terminator of their respective position lists.
169191**
169192** If isSaveLeft is 0, an entry is added to the output position list for
169193** each position in *pp2 for which there exists one or more positions in
169194** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
169195** when the *pp1 token appears before the *pp2 token, but not more than nToken
169196** slots before it.
169197**
169198** e.g. nToken==1 searches for adjacent positions.
169199*/
169200static int fts3PoslistPhraseMerge(
169201 char **pp, /* IN/OUT: Preallocated output buffer */
169202 int nToken, /* Maximum difference in token positions */
169203 int isSaveLeft, /* Save the left position */
169204 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
169205 char **pp1, /* IN/OUT: Left input list */
169206 char **pp2 /* IN/OUT: Right input list */
169207){
169208 char *p = *pp;
169209 char *p1 = *pp1;
169210 char *p2 = *pp2;
169211 int iCol1 = 0;
169212 int iCol2 = 0;
169213
169214 /* Never set both isSaveLeft and isExact for the same invocation. */
169215 assert( isSaveLeft==0 || isExact==0 );
169216
169217 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
169218 if( *p1==POS_COLUMN ){
169219 p1++;
169220 p1 += fts3GetVarint32(p1, &iCol1);
169221 }
169222 if( *p2==POS_COLUMN ){
169223 p2++;
169224 p2 += fts3GetVarint32(p2, &iCol2);
169225 }
169226
169227 while( 1 ){
169228 if( iCol1==iCol2 ){
169229 char *pSave = p;
169230 sqlite3_int64 iPrev = 0;
169231 sqlite3_int64 iPos1 = 0;
169232 sqlite3_int64 iPos2 = 0;
169233
169234 if( iCol1 ){
169235 *p++ = POS_COLUMN;
169236 p += sqlite3Fts3PutVarint(p, iCol1);
169237 }
169238
169239 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
169240 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
169241 if( iPos1<0 || iPos2<0 ) break;
169242
169243 while( 1 ){
169244 if( iPos2==iPos1+nToken
169245 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
169246 ){
169247 sqlite3_int64 iSave;
169248 iSave = isSaveLeft ? iPos1 : iPos2;
169249 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
169250 pSave = 0;
169251 assert( p );
169252 }
169253 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
169254 if( (*p2&0xFE)==0 ) break;
169255 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
169256 }else{
169257 if( (*p1&0xFE)==0 ) break;
169258 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
169259 }
169260 }
169261
169262 if( pSave ){
169263 assert( pp && p );
169264 p = pSave;
169265 }
169266
169267 fts3ColumnlistCopy(0, &p1);
169268 fts3ColumnlistCopy(0, &p2);
169269 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
169270 if( 0==*p1 || 0==*p2 ) break;
169271
169272 p1++;
169273 p1 += fts3GetVarint32(p1, &iCol1);
169274 p2++;
169275 p2 += fts3GetVarint32(p2, &iCol2);
169276 }
169277
169278 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
169279 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
169280 ** end of the position list, or the 0x01 that precedes the next
169281 ** column-number in the position list.
169282 */
169283 else if( iCol1<iCol2 ){
169284 fts3ColumnlistCopy(0, &p1);
169285 if( 0==*p1 ) break;
169286 p1++;
169287 p1 += fts3GetVarint32(p1, &iCol1);
169288 }else{
169289 fts3ColumnlistCopy(0, &p2);
169290 if( 0==*p2 ) break;
169291 p2++;
169292 p2 += fts3GetVarint32(p2, &iCol2);
169293 }
169294 }
169295
169296 fts3PoslistCopy(0, &p2);
169297 fts3PoslistCopy(0, &p1);
169298 *pp1 = p1;
169299 *pp2 = p2;
169300 if( *pp==p ){
169301 return 0;
169302 }
169303 *p++ = 0x00;
169304 *pp = p;
169305 return 1;
169306}
169307
169308/*
169309** Merge two position-lists as required by the NEAR operator. The argument
169310** position lists correspond to the left and right phrases of an expression
169311** like:
169312**
169313** "phrase 1" NEAR "phrase number 2"
169314**
169315** Position list *pp1 corresponds to the left-hand side of the NEAR
169316** expression and *pp2 to the right. As usual, the indexes in the position
169317** lists are the offsets of the last token in each phrase (tokens "1" and "2"
169318** in the example above).
169319**
169320** The output position list - written to *pp - is a copy of *pp2 with those
169321** entries that are not sufficiently NEAR entries in *pp1 removed.
169322*/
169323static int fts3PoslistNearMerge(
169324 char **pp, /* Output buffer */
169325 char *aTmp, /* Temporary buffer space */
169326 int nRight, /* Maximum difference in token positions */
169327 int nLeft, /* Maximum difference in token positions */
169328 char **pp1, /* IN/OUT: Left input list */
169329 char **pp2 /* IN/OUT: Right input list */
169330){
169331 char *p1 = *pp1;
169332 char *p2 = *pp2;
169333
169334 char *pTmp1 = aTmp;
169335 char *pTmp2;
169336 char *aTmp2;
169337 int res = 1;
169338
169339 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
169340 aTmp2 = pTmp2 = pTmp1;
169341 *pp1 = p1;
169342 *pp2 = p2;
169343 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
169344 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
169345 fts3PoslistMerge(pp, &aTmp, &aTmp2);
169346 }else if( pTmp1!=aTmp ){
169347 fts3PoslistCopy(pp, &aTmp);
169348 }else if( pTmp2!=aTmp2 ){
169349 fts3PoslistCopy(pp, &aTmp2);
169350 }else{
169351 res = 0;
169352 }
169353
169354 return res;
169355}
169356
169357/*
169358** An instance of this function is used to merge together the (potentially
169359** large number of) doclists for each term that matches a prefix query.
169360** See function fts3TermSelectMerge() for details.
169361*/
169362typedef struct TermSelect TermSelect;
169363struct TermSelect {
169364 char *aaOutput[16]; /* Malloc'd output buffers */
169365 int anOutput[16]; /* Size each output buffer in bytes */
169366};
169367
169368/*
169369** This function is used to read a single varint from a buffer. Parameter
169370** pEnd points 1 byte past the end of the buffer. When this function is
169371** called, if *pp points to pEnd or greater, then the end of the buffer
169372** has been reached. In this case *pp is set to 0 and the function returns.
169373**
169374** If *pp does not point to or past pEnd, then a single varint is read
169375** from *pp. *pp is then set to point 1 byte past the end of the read varint.
169376**
169377** If bDescIdx is false, the value read is added to *pVal before returning.
169378** If it is true, the value read is subtracted from *pVal before this
169379** function returns.
169380*/
169381static void fts3GetDeltaVarint3(
169382 char **pp, /* IN/OUT: Point to read varint from */
169383 char *pEnd, /* End of buffer */
169384 int bDescIdx, /* True if docids are descending */
169385 sqlite3_int64 *pVal /* IN/OUT: Integer value */
169386){
169387 if( *pp>=pEnd ){
169388 *pp = 0;
169389 }else{
169390 u64 iVal;
169391 *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
169392 if( bDescIdx ){
169393 *pVal = (i64)((u64)*pVal - iVal);
169394 }else{
169395 *pVal = (i64)((u64)*pVal + iVal);
169396 }
169397 }
169398}
169399
169400/*
169401** This function is used to write a single varint to a buffer. The varint
169402** is written to *pp. Before returning, *pp is set to point 1 byte past the
169403** end of the value written.
169404**
169405** If *pbFirst is zero when this function is called, the value written to
169406** the buffer is that of parameter iVal.
169407**
169408** If *pbFirst is non-zero when this function is called, then the value
169409** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
169410** (if bDescIdx is non-zero).
169411**
169412** Before returning, this function always sets *pbFirst to 1 and *piPrev
169413** to the value of parameter iVal.
169414*/
169415static void fts3PutDeltaVarint3(
169416 char **pp, /* IN/OUT: Output pointer */
169417 int bDescIdx, /* True for descending docids */
169418 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
169419 int *pbFirst, /* IN/OUT: True after first int written */
169420 sqlite3_int64 iVal /* Write this value to the list */
169421){
169422 sqlite3_uint64 iWrite;
169423 if( bDescIdx==0 || *pbFirst==0 ){
169424 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev );
169425 iWrite = (u64)iVal - (u64)*piPrev;
169426 }else{
169427 assert_fts3_nc( *piPrev>=iVal );
169428 iWrite = (u64)*piPrev - (u64)iVal;
169429 }
169430 assert( *pbFirst || *piPrev==0 );
169431 assert_fts3_nc( *pbFirst==0 || iWrite>0 );
169432 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
169433 *piPrev = iVal;
169434 *pbFirst = 1;
169435}
169436
169437
169438/*
169439** This macro is used by various functions that merge doclists. The two
169440** arguments are 64-bit docid values. If the value of the stack variable
169441** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
169442** Otherwise, (i2-i1).
169443**
169444** Using this makes it easier to write code that can merge doclists that are
169445** sorted in either ascending or descending order.
169446*/
169447/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
169448#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
169449
169450/*
169451** This function does an "OR" merge of two doclists (output contains all
169452** positions contained in either argument doclist). If the docids in the
169453** input doclists are sorted in ascending order, parameter bDescDoclist
169454** should be false. If they are sorted in ascending order, it should be
169455** passed a non-zero value.
169456**
169457** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
169458** containing the output doclist and SQLITE_OK is returned. In this case
169459** *pnOut is set to the number of bytes in the output doclist.
169460**
169461** If an error occurs, an SQLite error code is returned. The output values
169462** are undefined in this case.
169463*/
169464static int fts3DoclistOrMerge(
169465 int bDescDoclist, /* True if arguments are desc */
169466 char *a1, int n1, /* First doclist */
169467 char *a2, int n2, /* Second doclist */
169468 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
169469){
169470 int rc = SQLITE_OK;
169471 sqlite3_int64 i1 = 0;
169472 sqlite3_int64 i2 = 0;
169473 sqlite3_int64 iPrev = 0;
169474 char *pEnd1 = &a1[n1];
169475 char *pEnd2 = &a2[n2];
169476 char *p1 = a1;
169477 char *p2 = a2;
169478 char *p;
169479 char *aOut;
169480 int bFirstOut = 0;
169481
169482 *paOut = 0;
169483 *pnOut = 0;
169484
169485 /* Allocate space for the output. Both the input and output doclists
169486 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
169487 ** then the first docid in each list is simply encoded as a varint. For
169488 ** each subsequent docid, the varint stored is the difference between the
169489 ** current and previous docid (a positive number - since the list is in
169490 ** ascending order).
169491 **
169492 ** The first docid written to the output is therefore encoded using the
169493 ** same number of bytes as it is in whichever of the input lists it is
169494 ** read from. And each subsequent docid read from the same input list
169495 ** consumes either the same or less bytes as it did in the input (since
169496 ** the difference between it and the previous value in the output must
169497 ** be a positive value less than or equal to the delta value read from
169498 ** the input list). The same argument applies to all but the first docid
169499 ** read from the 'other' list. And to the contents of all position lists
169500 ** that will be copied and merged from the input to the output.
169501 **
169502 ** However, if the first docid copied to the output is a negative number,
169503 ** then the encoding of the first docid from the 'other' input list may
169504 ** be larger in the output than it was in the input (since the delta value
169505 ** may be a larger positive integer than the actual docid).
169506 **
169507 ** The space required to store the output is therefore the sum of the
169508 ** sizes of the two inputs, plus enough space for exactly one of the input
169509 ** docids to grow.
169510 **
169511 ** A symetric argument may be made if the doclists are in descending
169512 ** order.
169513 */
169514 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
169515 if( !aOut ) return SQLITE_NOMEM;
169516
169517 p = aOut;
169518 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
169519 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
169520 while( p1 || p2 ){
169521 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
169522
169523 if( p2 && p1 && iDiff==0 ){
169524 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
169525 rc = fts3PoslistMerge(&p, &p1, &p2);
169526 if( rc ) break;
169527 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
169528 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
169529 }else if( !p2 || (p1 && iDiff<0) ){
169530 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
169531 fts3PoslistCopy(&p, &p1);
169532 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
169533 }else{
169534 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
169535 fts3PoslistCopy(&p, &p2);
169536 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
169537 }
169538
169539 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
169540 }
169541
169542 if( rc!=SQLITE_OK ){
169543 sqlite3_free(aOut);
169544 p = aOut = 0;
169545 }else{
169546 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
169547 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
169548 }
169549 *paOut = aOut;
169550 *pnOut = (int)(p-aOut);
169551 return rc;
169552}
169553
169554/*
169555** This function does a "phrase" merge of two doclists. In a phrase merge,
169556** the output contains a copy of each position from the right-hand input
169557** doclist for which there is a position in the left-hand input doclist
169558** exactly nDist tokens before it.
169559**
169560** If the docids in the input doclists are sorted in ascending order,
169561** parameter bDescDoclist should be false. If they are sorted in ascending
169562** order, it should be passed a non-zero value.
169563**
169564** The right-hand input doclist is overwritten by this function.
169565*/
169566static int fts3DoclistPhraseMerge(
169567 int bDescDoclist, /* True if arguments are desc */
169568 int nDist, /* Distance from left to right (1=adjacent) */
169569 char *aLeft, int nLeft, /* Left doclist */
169570 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
169571){
169572 sqlite3_int64 i1 = 0;
169573 sqlite3_int64 i2 = 0;
169574 sqlite3_int64 iPrev = 0;
169575 char *aRight = *paRight;
169576 char *pEnd1 = &aLeft[nLeft];
169577 char *pEnd2 = &aRight[*pnRight];
169578 char *p1 = aLeft;
169579 char *p2 = aRight;
169580 char *p;
169581 int bFirstOut = 0;
169582 char *aOut;
169583
169584 assert( nDist>0 );
169585 if( bDescDoclist ){
169586 aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
169587 if( aOut==0 ) return SQLITE_NOMEM;
169588 }else{
169589 aOut = aRight;
169590 }
169591 p = aOut;
169592
169593 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
169594 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
169595
169596 while( p1 && p2 ){
169597 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
169598 if( iDiff==0 ){
169599 char *pSave = p;
169600 sqlite3_int64 iPrevSave = iPrev;
169601 int bFirstOutSave = bFirstOut;
169602
169603 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
169604 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
169605 p = pSave;
169606 iPrev = iPrevSave;
169607 bFirstOut = bFirstOutSave;
169608 }
169609 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
169610 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
169611 }else if( iDiff<0 ){
169612 fts3PoslistCopy(0, &p1);
169613 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
169614 }else{
169615 fts3PoslistCopy(0, &p2);
169616 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
169617 }
169618 }
169619
169620 *pnRight = (int)(p - aOut);
169621 if( bDescDoclist ){
169622 sqlite3_free(aRight);
169623 *paRight = aOut;
169624 }
169625
169626 return SQLITE_OK;
169627}
169628
169629/*
169630** Argument pList points to a position list nList bytes in size. This
169631** function checks to see if the position list contains any entries for
169632** a token in position 0 (of any column). If so, it writes argument iDelta
169633** to the output buffer pOut, followed by a position list consisting only
169634** of the entries from pList at position 0, and terminated by an 0x00 byte.
169635** The value returned is the number of bytes written to pOut (if any).
169636*/
169637SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
169638 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
169639 char *pList, /* Position list (no 0x00 term) */
169640 int nList, /* Size of pList in bytes */
169641 char *pOut /* Write output here */
169642){
169643 int nOut = 0;
169644 int bWritten = 0; /* True once iDelta has been written */
169645 char *p = pList;
169646 char *pEnd = &pList[nList];
169647
169648 if( *p!=0x01 ){
169649 if( *p==0x02 ){
169650 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
169651 pOut[nOut++] = 0x02;
169652 bWritten = 1;
169653 }
169654 fts3ColumnlistCopy(0, &p);
169655 }
169656
169657 while( p<pEnd ){
169658 sqlite3_int64 iCol;
169659 p++;
169660 p += sqlite3Fts3GetVarint(p, &iCol);
169661 if( *p==0x02 ){
169662 if( bWritten==0 ){
169663 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
169664 bWritten = 1;
169665 }
169666 pOut[nOut++] = 0x01;
169667 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
169668 pOut[nOut++] = 0x02;
169669 }
169670 fts3ColumnlistCopy(0, &p);
169671 }
169672 if( bWritten ){
169673 pOut[nOut++] = 0x00;
169674 }
169675
169676 return nOut;
169677}
169678
169679
169680/*
169681** Merge all doclists in the TermSelect.aaOutput[] array into a single
169682** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
169683** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
169684**
169685** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
169686** the responsibility of the caller to free any doclists left in the
169687** TermSelect.aaOutput[] array.
169688*/
169689static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
169690 char *aOut = 0;
169691 int nOut = 0;
169692 int i;
169693
169694 /* Loop through the doclists in the aaOutput[] array. Merge them all
169695 ** into a single doclist.
169696 */
169697 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
169698 if( pTS->aaOutput[i] ){
169699 if( !aOut ){
169700 aOut = pTS->aaOutput[i];
169701 nOut = pTS->anOutput[i];
169702 pTS->aaOutput[i] = 0;
169703 }else{
169704 int nNew;
169705 char *aNew;
169706
169707 int rc = fts3DoclistOrMerge(p->bDescIdx,
169708 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
169709 );
169710 if( rc!=SQLITE_OK ){
169711 sqlite3_free(aOut);
169712 return rc;
169713 }
169714
169715 sqlite3_free(pTS->aaOutput[i]);
169716 sqlite3_free(aOut);
169717 pTS->aaOutput[i] = 0;
169718 aOut = aNew;
169719 nOut = nNew;
169720 }
169721 }
169722 }
169723
169724 pTS->aaOutput[0] = aOut;
169725 pTS->anOutput[0] = nOut;
169726 return SQLITE_OK;
169727}
169728
169729/*
169730** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
169731** as the first argument. The merge is an "OR" merge (see function
169732** fts3DoclistOrMerge() for details).
169733**
169734** This function is called with the doclist for each term that matches
169735** a queried prefix. It merges all these doclists into one, the doclist
169736** for the specified prefix. Since there can be a very large number of
169737** doclists to merge, the merging is done pair-wise using the TermSelect
169738** object.
169739**
169740** This function returns SQLITE_OK if the merge is successful, or an
169741** SQLite error code (SQLITE_NOMEM) if an error occurs.
169742*/
169743static int fts3TermSelectMerge(
169744 Fts3Table *p, /* FTS table handle */
169745 TermSelect *pTS, /* TermSelect object to merge into */
169746 char *aDoclist, /* Pointer to doclist */
169747 int nDoclist /* Size of aDoclist in bytes */
169748){
169749 if( pTS->aaOutput[0]==0 ){
169750 /* If this is the first term selected, copy the doclist to the output
169751 ** buffer using memcpy().
169752 **
169753 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
169754 ** allocation. This is so as to ensure that the buffer is big enough
169755 ** to hold the current doclist AND'd with any other doclist. If the
169756 ** doclists are stored in order=ASC order, this padding would not be
169757 ** required (since the size of [doclistA AND doclistB] is always less
169758 ** than or equal to the size of [doclistA] in that case). But this is
169759 ** not true for order=DESC. For example, a doclist containing (1, -1)
169760 ** may be smaller than (-1), as in the first example the -1 may be stored
169761 ** as a single-byte delta, whereas in the second it must be stored as a
169762 ** FTS3_VARINT_MAX byte varint.
169763 **
169764 ** Similar padding is added in the fts3DoclistOrMerge() function.
169765 */
169766 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
169767 pTS->anOutput[0] = nDoclist;
169768 if( pTS->aaOutput[0] ){
169769 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
169770 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
169771 }else{
169772 return SQLITE_NOMEM;
169773 }
169774 }else{
169775 char *aMerge = aDoclist;
169776 int nMerge = nDoclist;
169777 int iOut;
169778
169779 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
169780 if( pTS->aaOutput[iOut]==0 ){
169781 assert( iOut>0 );
169782 pTS->aaOutput[iOut] = aMerge;
169783 pTS->anOutput[iOut] = nMerge;
169784 break;
169785 }else{
169786 char *aNew;
169787 int nNew;
169788
169789 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
169790 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
169791 );
169792 if( rc!=SQLITE_OK ){
169793 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
169794 return rc;
169795 }
169796
169797 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
169798 sqlite3_free(pTS->aaOutput[iOut]);
169799 pTS->aaOutput[iOut] = 0;
169800
169801 aMerge = aNew;
169802 nMerge = nNew;
169803 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
169804 pTS->aaOutput[iOut] = aMerge;
169805 pTS->anOutput[iOut] = nMerge;
169806 }
169807 }
169808 }
169809 }
169810 return SQLITE_OK;
169811}
169812
169813/*
169814** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
169815*/
169816static int fts3SegReaderCursorAppend(
169817 Fts3MultiSegReader *pCsr,
169818 Fts3SegReader *pNew
169819){
169820 if( (pCsr->nSegment%16)==0 ){
169821 Fts3SegReader **apNew;
169822 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
169823 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
169824 if( !apNew ){
169825 sqlite3Fts3SegReaderFree(pNew);
169826 return SQLITE_NOMEM;
169827 }
169828 pCsr->apSegment = apNew;
169829 }
169830 pCsr->apSegment[pCsr->nSegment++] = pNew;
169831 return SQLITE_OK;
169832}
169833
169834/*
169835** Add seg-reader objects to the Fts3MultiSegReader object passed as the
169836** 8th argument.
169837**
169838** This function returns SQLITE_OK if successful, or an SQLite error code
169839** otherwise.
169840*/
169841static int fts3SegReaderCursor(
169842 Fts3Table *p, /* FTS3 table handle */
169843 int iLangid, /* Language id */
169844 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
169845 int iLevel, /* Level of segments to scan */
169846 const char *zTerm, /* Term to query for */
169847 int nTerm, /* Size of zTerm in bytes */
169848 int isPrefix, /* True for a prefix search */
169849 int isScan, /* True to scan from zTerm to EOF */
169850 Fts3MultiSegReader *pCsr /* Cursor object to populate */
169851){
169852 int rc = SQLITE_OK; /* Error code */
169853 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
169854 int rc2; /* Result of sqlite3_reset() */
169855
169856 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
169857 ** for the pending-terms. If this is a scan, then this call must be being
169858 ** made by an fts4aux module, not an FTS table. In this case calling
169859 ** Fts3SegReaderPending might segfault, as the data structures used by
169860 ** fts4aux are not completely populated. So it's easiest to filter these
169861 ** calls out here. */
169862 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
169863 Fts3SegReader *pSeg = 0;
169864 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
169865 if( rc==SQLITE_OK && pSeg ){
169866 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
169867 }
169868 }
169869
169870 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
169871 if( rc==SQLITE_OK ){
169872 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
169873 }
169874
169875 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
169876 Fts3SegReader *pSeg = 0;
169877
169878 /* Read the values returned by the SELECT into local variables. */
169879 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
169880 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
169881 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
169882 int nRoot = sqlite3_column_bytes(pStmt, 4);
169883 char const *zRoot = sqlite3_column_blob(pStmt, 4);
169884
169885 /* If zTerm is not NULL, and this segment is not stored entirely on its
169886 ** root node, the range of leaves scanned can be reduced. Do this. */
169887 if( iStartBlock && zTerm && zRoot ){
169888 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
169889 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
169890 if( rc!=SQLITE_OK ) goto finished;
169891 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
169892 }
169893
169894 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
169895 (isPrefix==0 && isScan==0),
169896 iStartBlock, iLeavesEndBlock,
169897 iEndBlock, zRoot, nRoot, &pSeg
169898 );
169899 if( rc!=SQLITE_OK ) goto finished;
169900 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
169901 }
169902 }
169903
169904 finished:
169905 rc2 = sqlite3_reset(pStmt);
169906 if( rc==SQLITE_DONE ) rc = rc2;
169907
169908 return rc;
169909}
169910
169911/*
169912** Set up a cursor object for iterating through a full-text index or a
169913** single level therein.
169914*/
169915SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
169916 Fts3Table *p, /* FTS3 table handle */
169917 int iLangid, /* Language-id to search */
169918 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
169919 int iLevel, /* Level of segments to scan */
169920 const char *zTerm, /* Term to query for */
169921 int nTerm, /* Size of zTerm in bytes */
169922 int isPrefix, /* True for a prefix search */
169923 int isScan, /* True to scan from zTerm to EOF */
169924 Fts3MultiSegReader *pCsr /* Cursor object to populate */
169925){
169926 assert( iIndex>=0 && iIndex<p->nIndex );
169927 assert( iLevel==FTS3_SEGCURSOR_ALL
169928 || iLevel==FTS3_SEGCURSOR_PENDING
169929 || iLevel>=0
169930 );
169931 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
169932 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
169933 assert( isPrefix==0 || isScan==0 );
169934
169935 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
169936 return fts3SegReaderCursor(
169937 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
169938 );
169939}
169940
169941/*
169942** In addition to its current configuration, have the Fts3MultiSegReader
169943** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
169944**
169945** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
169946*/
169947static int fts3SegReaderCursorAddZero(
169948 Fts3Table *p, /* FTS virtual table handle */
169949 int iLangid,
169950 const char *zTerm, /* Term to scan doclist of */
169951 int nTerm, /* Number of bytes in zTerm */
169952 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
169953){
169954 return fts3SegReaderCursor(p,
169955 iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
169956 );
169957}
169958
169959/*
169960** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
169961** if isPrefix is true, to scan the doclist for all terms for which
169962** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
169963** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
169964** an SQLite error code.
169965**
169966** It is the responsibility of the caller to free this object by eventually
169967** passing it to fts3SegReaderCursorFree()
169968**
169969** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
169970** Output parameter *ppSegcsr is set to 0 if an error occurs.
169971*/
169972static int fts3TermSegReaderCursor(
169973 Fts3Cursor *pCsr, /* Virtual table cursor handle */
169974 const char *zTerm, /* Term to query for */
169975 int nTerm, /* Size of zTerm in bytes */
169976 int isPrefix, /* True for a prefix search */
169977 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
169978){
169979 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
169980 int rc = SQLITE_NOMEM; /* Return code */
169981
169982 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
169983 if( pSegcsr ){
169984 int i;
169985 int bFound = 0; /* True once an index has been found */
169986 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
169987
169988 if( isPrefix ){
169989 for(i=1; bFound==0 && i<p->nIndex; i++){
169990 if( p->aIndex[i].nPrefix==nTerm ){
169991 bFound = 1;
169992 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
169993 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
169994 );
169995 pSegcsr->bLookup = 1;
169996 }
169997 }
169998
169999 for(i=1; bFound==0 && i<p->nIndex; i++){
170000 if( p->aIndex[i].nPrefix==nTerm+1 ){
170001 bFound = 1;
170002 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
170003 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
170004 );
170005 if( rc==SQLITE_OK ){
170006 rc = fts3SegReaderCursorAddZero(
170007 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
170008 );
170009 }
170010 }
170011 }
170012 }
170013
170014 if( bFound==0 ){
170015 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
170016 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
170017 );
170018 pSegcsr->bLookup = !isPrefix;
170019 }
170020 }
170021
170022 *ppSegcsr = pSegcsr;
170023 return rc;
170024}
170025
170026/*
170027** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
170028*/
170029static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
170030 sqlite3Fts3SegReaderFinish(pSegcsr);
170031 sqlite3_free(pSegcsr);
170032}
170033
170034/*
170035** This function retrieves the doclist for the specified term (or term
170036** prefix) from the database.
170037*/
170038static int fts3TermSelect(
170039 Fts3Table *p, /* Virtual table handle */
170040 Fts3PhraseToken *pTok, /* Token to query for */
170041 int iColumn, /* Column to query (or -ve for all columns) */
170042 int *pnOut, /* OUT: Size of buffer at *ppOut */
170043 char **ppOut /* OUT: Malloced result buffer */
170044){
170045 int rc; /* Return code */
170046 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
170047 TermSelect tsc; /* Object for pair-wise doclist merging */
170048 Fts3SegFilter filter; /* Segment term filter configuration */
170049
170050 pSegcsr = pTok->pSegcsr;
170051 memset(&tsc, 0, sizeof(TermSelect));
170052
170053 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
170054 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
170055 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
170056 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
170057 filter.iCol = iColumn;
170058 filter.zTerm = pTok->z;
170059 filter.nTerm = pTok->n;
170060
170061 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
170062 while( SQLITE_OK==rc
170063 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
170064 ){
170065 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
170066 }
170067
170068 if( rc==SQLITE_OK ){
170069 rc = fts3TermSelectFinishMerge(p, &tsc);
170070 }
170071 if( rc==SQLITE_OK ){
170072 *ppOut = tsc.aaOutput[0];
170073 *pnOut = tsc.anOutput[0];
170074 }else{
170075 int i;
170076 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
170077 sqlite3_free(tsc.aaOutput[i]);
170078 }
170079 }
170080
170081 fts3SegReaderCursorFree(pSegcsr);
170082 pTok->pSegcsr = 0;
170083 return rc;
170084}
170085
170086/*
170087** This function counts the total number of docids in the doclist stored
170088** in buffer aList[], size nList bytes.
170089**
170090** If the isPoslist argument is true, then it is assumed that the doclist
170091** contains a position-list following each docid. Otherwise, it is assumed
170092** that the doclist is simply a list of docids stored as delta encoded
170093** varints.
170094*/
170095static int fts3DoclistCountDocids(char *aList, int nList){
170096 int nDoc = 0; /* Return value */
170097 if( aList ){
170098 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
170099 char *p = aList; /* Cursor */
170100 while( p<aEnd ){
170101 nDoc++;
170102 while( (*p++)&0x80 ); /* Skip docid varint */
170103 fts3PoslistCopy(0, &p); /* Skip over position list */
170104 }
170105 }
170106
170107 return nDoc;
170108}
170109
170110/*
170111** Advance the cursor to the next row in the %_content table that
170112** matches the search criteria. For a MATCH search, this will be
170113** the next row that matches. For a full-table scan, this will be
170114** simply the next row in the %_content table. For a docid lookup,
170115** this routine simply sets the EOF flag.
170116**
170117** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
170118** even if we reach end-of-file. The fts3EofMethod() will be called
170119** subsequently to determine whether or not an EOF was hit.
170120*/
170121static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
170122 int rc;
170123 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
170124 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
170125 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
170126 pTab->bLock++;
170127 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
170128 pCsr->isEof = 1;
170129 rc = sqlite3_reset(pCsr->pStmt);
170130 }else{
170131 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
170132 rc = SQLITE_OK;
170133 }
170134 pTab->bLock--;
170135 }else{
170136 rc = fts3EvalNext((Fts3Cursor *)pCursor);
170137 }
170138 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
170139 return rc;
170140}
170141
170142/*
170143** If the numeric type of argument pVal is "integer", then return it
170144** converted to a 64-bit signed integer. Otherwise, return a copy of
170145** the second parameter, iDefault.
170146*/
170147static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
170148 if( pVal ){
170149 int eType = sqlite3_value_numeric_type(pVal);
170150 if( eType==SQLITE_INTEGER ){
170151 return sqlite3_value_int64(pVal);
170152 }
170153 }
170154 return iDefault;
170155}
170156
170157/*
170158** This is the xFilter interface for the virtual table. See
170159** the virtual table xFilter method documentation for additional
170160** information.
170161**
170162** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
170163** the %_content table.
170164**
170165** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
170166** in the %_content table.
170167**
170168** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
170169** column on the left-hand side of the MATCH operator is column
170170** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
170171** side of the MATCH operator.
170172*/
170173static int fts3FilterMethod(
170174 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
170175 int idxNum, /* Strategy index */
170176 const char *idxStr, /* Unused */
170177 int nVal, /* Number of elements in apVal */
170178 sqlite3_value **apVal /* Arguments for the indexing scheme */
170179){
170180 int rc = SQLITE_OK;
170181 char *zSql; /* SQL statement used to access %_content */
170182 int eSearch;
170183 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
170184 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
170185
170186 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
170187 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
170188 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
170189 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
170190 int iIdx;
170191
170192 UNUSED_PARAMETER(idxStr);
170193 UNUSED_PARAMETER(nVal);
170194
170195 if( p->bLock ){
170196 return SQLITE_ERROR;
170197 }
170198
170199 eSearch = (idxNum & 0x0000FFFF);
170200 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
170201 assert( p->pSegments==0 );
170202
170203 /* Collect arguments into local variables */
170204 iIdx = 0;
170205 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
170206 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
170207 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
170208 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
170209 assert( iIdx==nVal );
170210
170211 /* In case the cursor has been used before, clear it now. */
170212 fts3ClearCursor(pCsr);
170213
170214 /* Set the lower and upper bounds on docids to return */
170215 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
170216 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
170217
170218 if( idxStr ){
170219 pCsr->bDesc = (idxStr[0]=='D');
170220 }else{
170221 pCsr->bDesc = p->bDescIdx;
170222 }
170223 pCsr->eSearch = (i16)eSearch;
170224
170225 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
170226 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
170227 const char *zQuery = (const char *)sqlite3_value_text(pCons);
170228
170229 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
170230 return SQLITE_NOMEM;
170231 }
170232
170233 pCsr->iLangid = 0;
170234 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
170235
170236 assert( p->base.zErrMsg==0 );
170237 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
170238 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
170239 &p->base.zErrMsg
170240 );
170241 if( rc!=SQLITE_OK ){
170242 return rc;
170243 }
170244
170245 rc = fts3EvalStart(pCsr);
170246 sqlite3Fts3SegmentsClose(p);
170247 if( rc!=SQLITE_OK ) return rc;
170248 pCsr->pNextId = pCsr->aDoclist;
170249 pCsr->iPrevId = 0;
170250 }
170251
170252 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
170253 ** statement loops through all rows of the %_content table. For a
170254 ** full-text query or docid lookup, the statement retrieves a single
170255 ** row by docid.
170256 */
170257 if( eSearch==FTS3_FULLSCAN_SEARCH ){
170258 if( pDocidGe || pDocidLe ){
170259 zSql = sqlite3_mprintf(
170260 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
170261 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
170262 (pCsr->bDesc ? "DESC" : "ASC")
170263 );
170264 }else{
170265 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
170266 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
170267 );
170268 }
170269 if( zSql ){
170270 p->bLock++;
170271 rc = sqlite3_prepare_v3(
170272 p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
170273 );
170274 p->bLock--;
170275 sqlite3_free(zSql);
170276 }else{
170277 rc = SQLITE_NOMEM;
170278 }
170279 }else if( eSearch==FTS3_DOCID_SEARCH ){
170280 rc = fts3CursorSeekStmt(pCsr);
170281 if( rc==SQLITE_OK ){
170282 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
170283 }
170284 }
170285 if( rc!=SQLITE_OK ) return rc;
170286
170287 return fts3NextMethod(pCursor);
170288}
170289
170290/*
170291** This is the xEof method of the virtual table. SQLite calls this
170292** routine to find out if it has reached the end of a result set.
170293*/
170294static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
170295 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
170296 if( pCsr->isEof ){
170297 fts3ClearCursor(pCsr);
170298 pCsr->isEof = 1;
170299 }
170300 return pCsr->isEof;
170301}
170302
170303/*
170304** This is the xRowid method. The SQLite core calls this routine to
170305** retrieve the rowid for the current row of the result set. fts3
170306** exposes %_content.docid as the rowid for the virtual table. The
170307** rowid should be written to *pRowid.
170308*/
170309static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
170310 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
170311 *pRowid = pCsr->iPrevId;
170312 return SQLITE_OK;
170313}
170314
170315/*
170316** This is the xColumn method, called by SQLite to request a value from
170317** the row that the supplied cursor currently points to.
170318**
170319** If:
170320**
170321** (iCol < p->nColumn) -> The value of the iCol'th user column.
170322** (iCol == p->nColumn) -> Magic column with the same name as the table.
170323** (iCol == p->nColumn+1) -> Docid column
170324** (iCol == p->nColumn+2) -> Langid column
170325*/
170326static int fts3ColumnMethod(
170327 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
170328 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
170329 int iCol /* Index of column to read value from */
170330){
170331 int rc = SQLITE_OK; /* Return Code */
170332 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
170333 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
170334
170335 /* The column value supplied by SQLite must be in range. */
170336 assert( iCol>=0 && iCol<=p->nColumn+2 );
170337
170338 switch( iCol-p->nColumn ){
170339 case 0:
170340 /* The special 'table-name' column */
170341 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
170342 break;
170343
170344 case 1:
170345 /* The docid column */
170346 sqlite3_result_int64(pCtx, pCsr->iPrevId);
170347 break;
170348
170349 case 2:
170350 if( pCsr->pExpr ){
170351 sqlite3_result_int64(pCtx, pCsr->iLangid);
170352 break;
170353 }else if( p->zLanguageid==0 ){
170354 sqlite3_result_int(pCtx, 0);
170355 break;
170356 }else{
170357 iCol = p->nColumn;
170358 /* no break */ deliberate_fall_through
170359 }
170360
170361 default:
170362 /* A user column. Or, if this is a full-table scan, possibly the
170363 ** language-id column. Seek the cursor. */
170364 rc = fts3CursorSeek(0, pCsr);
170365 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
170366 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
170367 }
170368 break;
170369 }
170370
170371 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
170372 return rc;
170373}
170374
170375/*
170376** This function is the implementation of the xUpdate callback used by
170377** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
170378** inserted, updated or deleted.
170379*/
170380static int fts3UpdateMethod(
170381 sqlite3_vtab *pVtab, /* Virtual table handle */
170382 int nArg, /* Size of argument array */
170383 sqlite3_value **apVal, /* Array of arguments */
170384 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
170385){
170386 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
170387}
170388
170389/*
170390** Implementation of xSync() method. Flush the contents of the pending-terms
170391** hash-table to the database.
170392*/
170393static int fts3SyncMethod(sqlite3_vtab *pVtab){
170394
170395 /* Following an incremental-merge operation, assuming that the input
170396 ** segments are not completely consumed (the usual case), they are updated
170397 ** in place to remove the entries that have already been merged. This
170398 ** involves updating the leaf block that contains the smallest unmerged
170399 ** entry and each block (if any) between the leaf and the root node. So
170400 ** if the height of the input segment b-trees is N, and input segments
170401 ** are merged eight at a time, updating the input segments at the end
170402 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
170403 ** small - often between 0 and 2. So the overhead of the incremental
170404 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
170405 ** dwarfing the actual productive work accomplished, the incremental merge
170406 ** is only attempted if it will write at least 64 leaf blocks. Hence
170407 ** nMinMerge.
170408 **
170409 ** Of course, updating the input segments also involves deleting a bunch
170410 ** of blocks from the segments table. But this is not considered overhead
170411 ** as it would also be required by a crisis-merge that used the same input
170412 ** segments.
170413 */
170414 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
170415
170416 Fts3Table *p = (Fts3Table*)pVtab;
170417 int rc;
170418 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
170419
170420 rc = sqlite3Fts3PendingTermsFlush(p);
170421 if( rc==SQLITE_OK
170422 && p->nLeafAdd>(nMinMerge/16)
170423 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
170424 ){
170425 int mxLevel = 0; /* Maximum relative level value in db */
170426 int A; /* Incr-merge parameter A */
170427
170428 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
170429 assert( rc==SQLITE_OK || mxLevel==0 );
170430 A = p->nLeafAdd * mxLevel;
170431 A += (A/2);
170432 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
170433 }
170434 sqlite3Fts3SegmentsClose(p);
170435 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
170436 return rc;
170437}
170438
170439/*
170440** If it is currently unknown whether or not the FTS table has an %_stat
170441** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
170442** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
170443** if an error occurs.
170444*/
170445static int fts3SetHasStat(Fts3Table *p){
170446 int rc = SQLITE_OK;
170447 if( p->bHasStat==2 ){
170448 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
170449 if( zTbl ){
170450 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
170451 sqlite3_free(zTbl);
170452 p->bHasStat = (res==SQLITE_OK);
170453 }else{
170454 rc = SQLITE_NOMEM;
170455 }
170456 }
170457 return rc;
170458}
170459
170460/*
170461** Implementation of xBegin() method.
170462*/
170463static int fts3BeginMethod(sqlite3_vtab *pVtab){
170464 Fts3Table *p = (Fts3Table*)pVtab;
170465 UNUSED_PARAMETER(pVtab);
170466 assert( p->pSegments==0 );
170467 assert( p->nPendingData==0 );
170468 assert( p->inTransaction!=1 );
170469 TESTONLY( p->inTransaction = 1 );
170470 TESTONLY( p->mxSavepoint = -1; );
170471 p->nLeafAdd = 0;
170472 return fts3SetHasStat(p);
170473}
170474
170475/*
170476** Implementation of xCommit() method. This is a no-op. The contents of
170477** the pending-terms hash-table have already been flushed into the database
170478** by fts3SyncMethod().
170479*/
170480static int fts3CommitMethod(sqlite3_vtab *pVtab){
170481 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
170482 UNUSED_PARAMETER(pVtab);
170483 assert( p->nPendingData==0 );
170484 assert( p->inTransaction!=0 );
170485 assert( p->pSegments==0 );
170486 TESTONLY( p->inTransaction = 0 );
170487 TESTONLY( p->mxSavepoint = -1; );
170488 return SQLITE_OK;
170489}
170490
170491/*
170492** Implementation of xRollback(). Discard the contents of the pending-terms
170493** hash-table. Any changes made to the database are reverted by SQLite.
170494*/
170495static int fts3RollbackMethod(sqlite3_vtab *pVtab){
170496 Fts3Table *p = (Fts3Table*)pVtab;
170497 sqlite3Fts3PendingTermsClear(p);
170498 assert( p->inTransaction!=0 );
170499 TESTONLY( p->inTransaction = 0 );
170500 TESTONLY( p->mxSavepoint = -1; );
170501 return SQLITE_OK;
170502}
170503
170504/*
170505** When called, *ppPoslist must point to the byte immediately following the
170506** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
170507** moves *ppPoslist so that it instead points to the first byte of the
170508** same position list.
170509*/
170510static void fts3ReversePoslist(char *pStart, char **ppPoslist){
170511 char *p = &(*ppPoslist)[-2];
170512 char c = 0;
170513
170514 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
170515 while( p>pStart && (c=*p--)==0 );
170516
170517 /* Search backwards for a varint with value zero (the end of the previous
170518 ** poslist). This is an 0x00 byte preceded by some byte that does not
170519 ** have the 0x80 bit set. */
170520 while( p>pStart && (*p & 0x80) | c ){
170521 c = *p--;
170522 }
170523 assert( p==pStart || c==0 );
170524
170525 /* At this point p points to that preceding byte without the 0x80 bit
170526 ** set. So to find the start of the poslist, skip forward 2 bytes then
170527 ** over a varint.
170528 **
170529 ** Normally. The other case is that p==pStart and the poslist to return
170530 ** is the first in the doclist. In this case do not skip forward 2 bytes.
170531 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
170532 ** is required for cases where the first byte of a doclist and the
170533 ** doclist is empty. For example, if the first docid is 10, a doclist
170534 ** that begins with:
170535 **
170536 ** 0x0A 0x00 <next docid delta varint>
170537 */
170538 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
170539 while( *p++&0x80 );
170540 *ppPoslist = p;
170541}
170542
170543/*
170544** Helper function used by the implementation of the overloaded snippet(),
170545** offsets() and optimize() SQL functions.
170546**
170547** If the value passed as the third argument is a blob of size
170548** sizeof(Fts3Cursor*), then the blob contents are copied to the
170549** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
170550** message is written to context pContext and SQLITE_ERROR returned. The
170551** string passed via zFunc is used as part of the error message.
170552*/
170553static int fts3FunctionArg(
170554 sqlite3_context *pContext, /* SQL function call context */
170555 const char *zFunc, /* Function name */
170556 sqlite3_value *pVal, /* argv[0] passed to function */
170557 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
170558){
170559 int rc;
170560 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
170561 if( (*ppCsr)!=0 ){
170562 rc = SQLITE_OK;
170563 }else{
170564 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
170565 sqlite3_result_error(pContext, zErr, -1);
170566 sqlite3_free(zErr);
170567 rc = SQLITE_ERROR;
170568 }
170569 return rc;
170570}
170571
170572/*
170573** Implementation of the snippet() function for FTS3
170574*/
170575static void fts3SnippetFunc(
170576 sqlite3_context *pContext, /* SQLite function call context */
170577 int nVal, /* Size of apVal[] array */
170578 sqlite3_value **apVal /* Array of arguments */
170579){
170580 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
170581 const char *zStart = "<b>";
170582 const char *zEnd = "</b>";
170583 const char *zEllipsis = "<b>...</b>";
170584 int iCol = -1;
170585 int nToken = 15; /* Default number of tokens in snippet */
170586
170587 /* There must be at least one argument passed to this function (otherwise
170588 ** the non-overloaded version would have been called instead of this one).
170589 */
170590 assert( nVal>=1 );
170591
170592 if( nVal>6 ){
170593 sqlite3_result_error(pContext,
170594 "wrong number of arguments to function snippet()", -1);
170595 return;
170596 }
170597 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
170598
170599 switch( nVal ){
170600 case 6: nToken = sqlite3_value_int(apVal[5]);
170601 /* no break */ deliberate_fall_through
170602 case 5: iCol = sqlite3_value_int(apVal[4]);
170603 /* no break */ deliberate_fall_through
170604 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
170605 /* no break */ deliberate_fall_through
170606 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
170607 /* no break */ deliberate_fall_through
170608 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
170609 }
170610 if( !zEllipsis || !zEnd || !zStart ){
170611 sqlite3_result_error_nomem(pContext);
170612 }else if( nToken==0 ){
170613 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
170614 }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
170615 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
170616 }
170617}
170618
170619/*
170620** Implementation of the offsets() function for FTS3
170621*/
170622static void fts3OffsetsFunc(
170623 sqlite3_context *pContext, /* SQLite function call context */
170624 int nVal, /* Size of argument array */
170625 sqlite3_value **apVal /* Array of arguments */
170626){
170627 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
170628
170629 UNUSED_PARAMETER(nVal);
170630
170631 assert( nVal==1 );
170632 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
170633 assert( pCsr );
170634 if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
170635 sqlite3Fts3Offsets(pContext, pCsr);
170636 }
170637}
170638
170639/*
170640** Implementation of the special optimize() function for FTS3. This
170641** function merges all segments in the database to a single segment.
170642** Example usage is:
170643**
170644** SELECT optimize(t) FROM t LIMIT 1;
170645**
170646** where 't' is the name of an FTS3 table.
170647*/
170648static void fts3OptimizeFunc(
170649 sqlite3_context *pContext, /* SQLite function call context */
170650 int nVal, /* Size of argument array */
170651 sqlite3_value **apVal /* Array of arguments */
170652){
170653 int rc; /* Return code */
170654 Fts3Table *p; /* Virtual table handle */
170655 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
170656
170657 UNUSED_PARAMETER(nVal);
170658
170659 assert( nVal==1 );
170660 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
170661 p = (Fts3Table *)pCursor->base.pVtab;
170662 assert( p );
170663
170664 rc = sqlite3Fts3Optimize(p);
170665
170666 switch( rc ){
170667 case SQLITE_OK:
170668 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
170669 break;
170670 case SQLITE_DONE:
170671 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
170672 break;
170673 default:
170674 sqlite3_result_error_code(pContext, rc);
170675 break;
170676 }
170677}
170678
170679/*
170680** Implementation of the matchinfo() function for FTS3
170681*/
170682static void fts3MatchinfoFunc(
170683 sqlite3_context *pContext, /* SQLite function call context */
170684 int nVal, /* Size of argument array */
170685 sqlite3_value **apVal /* Array of arguments */
170686){
170687 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
170688 assert( nVal==1 || nVal==2 );
170689 if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
170690 const char *zArg = 0;
170691 if( nVal>1 ){
170692 zArg = (const char *)sqlite3_value_text(apVal[1]);
170693 }
170694 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
170695 }
170696}
170697
170698/*
170699** This routine implements the xFindFunction method for the FTS3
170700** virtual table.
170701*/
170702static int fts3FindFunctionMethod(
170703 sqlite3_vtab *pVtab, /* Virtual table handle */
170704 int nArg, /* Number of SQL function arguments */
170705 const char *zName, /* Name of SQL function */
170706 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
170707 void **ppArg /* Unused */
170708){
170709 struct Overloaded {
170710 const char *zName;
170711 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
170712 } aOverload[] = {
170713 { "snippet", fts3SnippetFunc },
170714 { "offsets", fts3OffsetsFunc },
170715 { "optimize", fts3OptimizeFunc },
170716 { "matchinfo", fts3MatchinfoFunc },
170717 };
170718 int i; /* Iterator variable */
170719
170720 UNUSED_PARAMETER(pVtab);
170721 UNUSED_PARAMETER(nArg);
170722 UNUSED_PARAMETER(ppArg);
170723
170724 for(i=0; i<SizeofArray(aOverload); i++){
170725 if( strcmp(zName, aOverload[i].zName)==0 ){
170726 *pxFunc = aOverload[i].xFunc;
170727 return 1;
170728 }
170729 }
170730
170731 /* No function of the specified name was found. Return 0. */
170732 return 0;
170733}
170734
170735/*
170736** Implementation of FTS3 xRename method. Rename an fts3 table.
170737*/
170738static int fts3RenameMethod(
170739 sqlite3_vtab *pVtab, /* Virtual table handle */
170740 const char *zName /* New name of table */
170741){
170742 Fts3Table *p = (Fts3Table *)pVtab;
170743 sqlite3 *db = p->db; /* Database connection */
170744 int rc; /* Return Code */
170745
170746 /* At this point it must be known if the %_stat table exists or not.
170747 ** So bHasStat may not be 2. */
170748 rc = fts3SetHasStat(p);
170749
170750 /* As it happens, the pending terms table is always empty here. This is
170751 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
170752 ** always opens a savepoint transaction. And the xSavepoint() method
170753 ** flushes the pending terms table. But leave the (no-op) call to
170754 ** PendingTermsFlush() in in case that changes.
170755 */
170756 assert( p->nPendingData==0 );
170757 if( rc==SQLITE_OK ){
170758 rc = sqlite3Fts3PendingTermsFlush(p);
170759 }
170760
170761 if( p->zContentTbl==0 ){
170762 fts3DbExec(&rc, db,
170763 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
170764 p->zDb, p->zName, zName
170765 );
170766 }
170767
170768 if( p->bHasDocsize ){
170769 fts3DbExec(&rc, db,
170770 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
170771 p->zDb, p->zName, zName
170772 );
170773 }
170774 if( p->bHasStat ){
170775 fts3DbExec(&rc, db,
170776 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
170777 p->zDb, p->zName, zName
170778 );
170779 }
170780 fts3DbExec(&rc, db,
170781 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
170782 p->zDb, p->zName, zName
170783 );
170784 fts3DbExec(&rc, db,
170785 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
170786 p->zDb, p->zName, zName
170787 );
170788 return rc;
170789}
170790
170791/*
170792** The xSavepoint() method.
170793**
170794** Flush the contents of the pending-terms table to disk.
170795*/
170796static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
170797 int rc = SQLITE_OK;
170798 UNUSED_PARAMETER(iSavepoint);
170799 assert( ((Fts3Table *)pVtab)->inTransaction );
170800 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
170801 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
170802 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
170803 rc = fts3SyncMethod(pVtab);
170804 }
170805 return rc;
170806}
170807
170808/*
170809** The xRelease() method.
170810**
170811** This is a no-op.
170812*/
170813static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
170814 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
170815 UNUSED_PARAMETER(iSavepoint);
170816 UNUSED_PARAMETER(pVtab);
170817 assert( p->inTransaction );
170818 assert( p->mxSavepoint >= iSavepoint );
170819 TESTONLY( p->mxSavepoint = iSavepoint-1 );
170820 return SQLITE_OK;
170821}
170822
170823/*
170824** The xRollbackTo() method.
170825**
170826** Discard the contents of the pending terms table.
170827*/
170828static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
170829 Fts3Table *p = (Fts3Table*)pVtab;
170830 UNUSED_PARAMETER(iSavepoint);
170831 assert( p->inTransaction );
170832 TESTONLY( p->mxSavepoint = iSavepoint );
170833 sqlite3Fts3PendingTermsClear(p);
170834 return SQLITE_OK;
170835}
170836
170837/*
170838** Return true if zName is the extension on one of the shadow tables used
170839** by this module.
170840*/
170841static int fts3ShadowName(const char *zName){
170842 static const char *azName[] = {
170843 "content", "docsize", "segdir", "segments", "stat",
170844 };
170845 unsigned int i;
170846 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
170847 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
170848 }
170849 return 0;
170850}
170851
170852static const sqlite3_module fts3Module = {
170853 /* iVersion */ 3,
170854 /* xCreate */ fts3CreateMethod,
170855 /* xConnect */ fts3ConnectMethod,
170856 /* xBestIndex */ fts3BestIndexMethod,
170857 /* xDisconnect */ fts3DisconnectMethod,
170858 /* xDestroy */ fts3DestroyMethod,
170859 /* xOpen */ fts3OpenMethod,
170860 /* xClose */ fts3CloseMethod,
170861 /* xFilter */ fts3FilterMethod,
170862 /* xNext */ fts3NextMethod,
170863 /* xEof */ fts3EofMethod,
170864 /* xColumn */ fts3ColumnMethod,
170865 /* xRowid */ fts3RowidMethod,
170866 /* xUpdate */ fts3UpdateMethod,
170867 /* xBegin */ fts3BeginMethod,
170868 /* xSync */ fts3SyncMethod,
170869 /* xCommit */ fts3CommitMethod,
170870 /* xRollback */ fts3RollbackMethod,
170871 /* xFindFunction */ fts3FindFunctionMethod,
170872 /* xRename */ fts3RenameMethod,
170873 /* xSavepoint */ fts3SavepointMethod,
170874 /* xRelease */ fts3ReleaseMethod,
170875 /* xRollbackTo */ fts3RollbackToMethod,
170876 /* xShadowName */ fts3ShadowName,
170877};
170878
170879/*
170880** This function is registered as the module destructor (called when an
170881** FTS3 enabled database connection is closed). It frees the memory
170882** allocated for the tokenizer hash table.
170883*/
170884static void hashDestroy(void *p){
170885 Fts3Hash *pHash = (Fts3Hash *)p;
170886 sqlite3Fts3HashClear(pHash);
170887 sqlite3_free(pHash);
170888}
170889
170890/*
170891** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
170892** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
170893** respectively. The following three forward declarations are for functions
170894** declared in these files used to retrieve the respective implementations.
170895**
170896** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
170897** to by the argument to point to the "simple" tokenizer implementation.
170898** And so on.
170899*/
170900SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170901SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170902#ifndef SQLITE_DISABLE_FTS3_UNICODE
170903SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
170904#endif
170905#ifdef SQLITE_ENABLE_ICU
170906SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170907#endif
170908
170909/*
170910** Initialize the fts3 extension. If this extension is built as part
170911** of the sqlite library, then this function is called directly by
170912** SQLite. If fts3 is built as a dynamically loadable extension, this
170913** function is called by the sqlite3_extension_init() entry point.
170914*/
170915SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
170916 int rc = SQLITE_OK;
170917 Fts3Hash *pHash = 0;
170918 const sqlite3_tokenizer_module *pSimple = 0;
170919 const sqlite3_tokenizer_module *pPorter = 0;
170920#ifndef SQLITE_DISABLE_FTS3_UNICODE
170921 const sqlite3_tokenizer_module *pUnicode = 0;
170922#endif
170923
170924#ifdef SQLITE_ENABLE_ICU
170925 const sqlite3_tokenizer_module *pIcu = 0;
170926 sqlite3Fts3IcuTokenizerModule(&pIcu);
170927#endif
170928
170929#ifndef SQLITE_DISABLE_FTS3_UNICODE
170930 sqlite3Fts3UnicodeTokenizer(&pUnicode);
170931#endif
170932
170933#ifdef SQLITE_TEST
170934 rc = sqlite3Fts3InitTerm(db);
170935 if( rc!=SQLITE_OK ) return rc;
170936#endif
170937
170938 rc = sqlite3Fts3InitAux(db);
170939 if( rc!=SQLITE_OK ) return rc;
170940
170941 sqlite3Fts3SimpleTokenizerModule(&pSimple);
170942 sqlite3Fts3PorterTokenizerModule(&pPorter);
170943
170944 /* Allocate and initialize the hash-table used to store tokenizers. */
170945 pHash = sqlite3_malloc(sizeof(Fts3Hash));
170946 if( !pHash ){
170947 rc = SQLITE_NOMEM;
170948 }else{
170949 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
170950 }
170951
170952 /* Load the built-in tokenizers into the hash table */
170953 if( rc==SQLITE_OK ){
170954 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
170955 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
170956
170957#ifndef SQLITE_DISABLE_FTS3_UNICODE
170958 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
170959#endif
170960#ifdef SQLITE_ENABLE_ICU
170961 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
170962#endif
170963 ){
170964 rc = SQLITE_NOMEM;
170965 }
170966 }
170967
170968#ifdef SQLITE_TEST
170969 if( rc==SQLITE_OK ){
170970 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
170971 }
170972#endif
170973
170974 /* Create the virtual table wrapper around the hash-table and overload
170975 ** the four scalar functions. If this is successful, register the
170976 ** module with sqlite.
170977 */
170978 if( SQLITE_OK==rc
170979 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
170980 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
170981 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
170982 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
170983 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
170984 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
170985 ){
170986 rc = sqlite3_create_module_v2(
170987 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
170988 );
170989 if( rc==SQLITE_OK ){
170990 rc = sqlite3_create_module_v2(
170991 db, "fts4", &fts3Module, (void *)pHash, 0
170992 );
170993 }
170994 if( rc==SQLITE_OK ){
170995 rc = sqlite3Fts3InitTok(db, (void *)pHash);
170996 }
170997 return rc;
170998 }
170999
171000
171001 /* An error has occurred. Delete the hash table and return the error code. */
171002 assert( rc!=SQLITE_OK );
171003 if( pHash ){
171004 sqlite3Fts3HashClear(pHash);
171005 sqlite3_free(pHash);
171006 }
171007 return rc;
171008}
171009
171010/*
171011** Allocate an Fts3MultiSegReader for each token in the expression headed
171012** by pExpr.
171013**
171014** An Fts3SegReader object is a cursor that can seek or scan a range of
171015** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
171016** Fts3SegReader objects internally to provide an interface to seek or scan
171017** within the union of all segments of a b-tree. Hence the name.
171018**
171019** If the allocated Fts3MultiSegReader just seeks to a single entry in a
171020** segment b-tree (if the term is not a prefix or it is a prefix for which
171021** there exists prefix b-tree of the right length) then it may be traversed
171022** and merged incrementally. Otherwise, it has to be merged into an in-memory
171023** doclist and then traversed.
171024*/
171025static void fts3EvalAllocateReaders(
171026 Fts3Cursor *pCsr, /* FTS cursor handle */
171027 Fts3Expr *pExpr, /* Allocate readers for this expression */
171028 int *pnToken, /* OUT: Total number of tokens in phrase. */
171029 int *pnOr, /* OUT: Total number of OR nodes in expr. */
171030 int *pRc /* IN/OUT: Error code */
171031){
171032 if( pExpr && SQLITE_OK==*pRc ){
171033 if( pExpr->eType==FTSQUERY_PHRASE ){
171034 int i;
171035 int nToken = pExpr->pPhrase->nToken;
171036 *pnToken += nToken;
171037 for(i=0; i<nToken; i++){
171038 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
171039 int rc = fts3TermSegReaderCursor(pCsr,
171040 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
171041 );
171042 if( rc!=SQLITE_OK ){
171043 *pRc = rc;
171044 return;
171045 }
171046 }
171047 assert( pExpr->pPhrase->iDoclistToken==0 );
171048 pExpr->pPhrase->iDoclistToken = -1;
171049 }else{
171050 *pnOr += (pExpr->eType==FTSQUERY_OR);
171051 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
171052 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
171053 }
171054 }
171055}
171056
171057/*
171058** Arguments pList/nList contain the doclist for token iToken of phrase p.
171059** It is merged into the main doclist stored in p->doclist.aAll/nAll.
171060**
171061** This function assumes that pList points to a buffer allocated using
171062** sqlite3_malloc(). This function takes responsibility for eventually
171063** freeing the buffer.
171064**
171065** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
171066*/
171067static int fts3EvalPhraseMergeToken(
171068 Fts3Table *pTab, /* FTS Table pointer */
171069 Fts3Phrase *p, /* Phrase to merge pList/nList into */
171070 int iToken, /* Token pList/nList corresponds to */
171071 char *pList, /* Pointer to doclist */
171072 int nList /* Number of bytes in pList */
171073){
171074 int rc = SQLITE_OK;
171075 assert( iToken!=p->iDoclistToken );
171076
171077 if( pList==0 ){
171078 sqlite3_free(p->doclist.aAll);
171079 p->doclist.aAll = 0;
171080 p->doclist.nAll = 0;
171081 }
171082
171083 else if( p->iDoclistToken<0 ){
171084 p->doclist.aAll = pList;
171085 p->doclist.nAll = nList;
171086 }
171087
171088 else if( p->doclist.aAll==0 ){
171089 sqlite3_free(pList);
171090 }
171091
171092 else {
171093 char *pLeft;
171094 char *pRight;
171095 int nLeft;
171096 int nRight;
171097 int nDiff;
171098
171099 if( p->iDoclistToken<iToken ){
171100 pLeft = p->doclist.aAll;
171101 nLeft = p->doclist.nAll;
171102 pRight = pList;
171103 nRight = nList;
171104 nDiff = iToken - p->iDoclistToken;
171105 }else{
171106 pRight = p->doclist.aAll;
171107 nRight = p->doclist.nAll;
171108 pLeft = pList;
171109 nLeft = nList;
171110 nDiff = p->iDoclistToken - iToken;
171111 }
171112
171113 rc = fts3DoclistPhraseMerge(
171114 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
171115 );
171116 sqlite3_free(pLeft);
171117 p->doclist.aAll = pRight;
171118 p->doclist.nAll = nRight;
171119 }
171120
171121 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
171122 return rc;
171123}
171124
171125/*
171126** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
171127** does not take deferred tokens into account.
171128**
171129** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
171130*/
171131static int fts3EvalPhraseLoad(
171132 Fts3Cursor *pCsr, /* FTS Cursor handle */
171133 Fts3Phrase *p /* Phrase object */
171134){
171135 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171136 int iToken;
171137 int rc = SQLITE_OK;
171138
171139 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
171140 Fts3PhraseToken *pToken = &p->aToken[iToken];
171141 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
171142
171143 if( pToken->pSegcsr ){
171144 int nThis = 0;
171145 char *pThis = 0;
171146 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
171147 if( rc==SQLITE_OK ){
171148 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
171149 }
171150 }
171151 assert( pToken->pSegcsr==0 );
171152 }
171153
171154 return rc;
171155}
171156
171157#ifndef SQLITE_DISABLE_FTS4_DEFERRED
171158/*
171159** This function is called on each phrase after the position lists for
171160** any deferred tokens have been loaded into memory. It updates the phrases
171161** current position list to include only those positions that are really
171162** instances of the phrase (after considering deferred tokens). If this
171163** means that the phrase does not appear in the current row, doclist.pList
171164** and doclist.nList are both zeroed.
171165**
171166** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
171167*/
171168static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
171169 int iToken; /* Used to iterate through phrase tokens */
171170 char *aPoslist = 0; /* Position list for deferred tokens */
171171 int nPoslist = 0; /* Number of bytes in aPoslist */
171172 int iPrev = -1; /* Token number of previous deferred token */
171173
171174 assert( pPhrase->doclist.bFreeList==0 );
171175
171176 for(iToken=0; iToken<pPhrase->nToken; iToken++){
171177 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
171178 Fts3DeferredToken *pDeferred = pToken->pDeferred;
171179
171180 if( pDeferred ){
171181 char *pList;
171182 int nList;
171183 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
171184 if( rc!=SQLITE_OK ) return rc;
171185
171186 if( pList==0 ){
171187 sqlite3_free(aPoslist);
171188 pPhrase->doclist.pList = 0;
171189 pPhrase->doclist.nList = 0;
171190 return SQLITE_OK;
171191
171192 }else if( aPoslist==0 ){
171193 aPoslist = pList;
171194 nPoslist = nList;
171195
171196 }else{
171197 char *aOut = pList;
171198 char *p1 = aPoslist;
171199 char *p2 = aOut;
171200
171201 assert( iPrev>=0 );
171202 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
171203 sqlite3_free(aPoslist);
171204 aPoslist = pList;
171205 nPoslist = (int)(aOut - aPoslist);
171206 if( nPoslist==0 ){
171207 sqlite3_free(aPoslist);
171208 pPhrase->doclist.pList = 0;
171209 pPhrase->doclist.nList = 0;
171210 return SQLITE_OK;
171211 }
171212 }
171213 iPrev = iToken;
171214 }
171215 }
171216
171217 if( iPrev>=0 ){
171218 int nMaxUndeferred = pPhrase->iDoclistToken;
171219 if( nMaxUndeferred<0 ){
171220 pPhrase->doclist.pList = aPoslist;
171221 pPhrase->doclist.nList = nPoslist;
171222 pPhrase->doclist.iDocid = pCsr->iPrevId;
171223 pPhrase->doclist.bFreeList = 1;
171224 }else{
171225 int nDistance;
171226 char *p1;
171227 char *p2;
171228 char *aOut;
171229
171230 if( nMaxUndeferred>iPrev ){
171231 p1 = aPoslist;
171232 p2 = pPhrase->doclist.pList;
171233 nDistance = nMaxUndeferred - iPrev;
171234 }else{
171235 p1 = pPhrase->doclist.pList;
171236 p2 = aPoslist;
171237 nDistance = iPrev - nMaxUndeferred;
171238 }
171239
171240 aOut = (char *)sqlite3_malloc(nPoslist+8);
171241 if( !aOut ){
171242 sqlite3_free(aPoslist);
171243 return SQLITE_NOMEM;
171244 }
171245
171246 pPhrase->doclist.pList = aOut;
171247 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
171248 pPhrase->doclist.bFreeList = 1;
171249 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
171250 }else{
171251 sqlite3_free(aOut);
171252 pPhrase->doclist.pList = 0;
171253 pPhrase->doclist.nList = 0;
171254 }
171255 sqlite3_free(aPoslist);
171256 }
171257 }
171258
171259 return SQLITE_OK;
171260}
171261#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
171262
171263/*
171264** Maximum number of tokens a phrase may have to be considered for the
171265** incremental doclists strategy.
171266*/
171267#define MAX_INCR_PHRASE_TOKENS 4
171268
171269/*
171270** This function is called for each Fts3Phrase in a full-text query
171271** expression to initialize the mechanism for returning rows. Once this
171272** function has been called successfully on an Fts3Phrase, it may be
171273** used with fts3EvalPhraseNext() to iterate through the matching docids.
171274**
171275** If parameter bOptOk is true, then the phrase may (or may not) use the
171276** incremental loading strategy. Otherwise, the entire doclist is loaded into
171277** memory within this call.
171278**
171279** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
171280*/
171281static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
171282 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171283 int rc = SQLITE_OK; /* Error code */
171284 int i;
171285
171286 /* Determine if doclists may be loaded from disk incrementally. This is
171287 ** possible if the bOptOk argument is true, the FTS doclists will be
171288 ** scanned in forward order, and the phrase consists of
171289 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
171290 ** tokens or prefix tokens that cannot use a prefix-index. */
171291 int bHaveIncr = 0;
171292 int bIncrOk = (bOptOk
171293 && pCsr->bDesc==pTab->bDescIdx
171294 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
171295#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
171296 && pTab->bNoIncrDoclist==0
171297#endif
171298 );
171299 for(i=0; bIncrOk==1 && i<p->nToken; i++){
171300 Fts3PhraseToken *pToken = &p->aToken[i];
171301 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
171302 bIncrOk = 0;
171303 }
171304 if( pToken->pSegcsr ) bHaveIncr = 1;
171305 }
171306
171307 if( bIncrOk && bHaveIncr ){
171308 /* Use the incremental approach. */
171309 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
171310 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
171311 Fts3PhraseToken *pToken = &p->aToken[i];
171312 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
171313 if( pSegcsr ){
171314 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
171315 }
171316 }
171317 p->bIncr = 1;
171318 }else{
171319 /* Load the full doclist for the phrase into memory. */
171320 rc = fts3EvalPhraseLoad(pCsr, p);
171321 p->bIncr = 0;
171322 }
171323
171324 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
171325 return rc;
171326}
171327
171328/*
171329** This function is used to iterate backwards (from the end to start)
171330** through doclists. It is used by this module to iterate through phrase
171331** doclists in reverse and by the fts3_write.c module to iterate through
171332** pending-terms lists when writing to databases with "order=desc".
171333**
171334** The doclist may be sorted in ascending (parameter bDescIdx==0) or
171335** descending (parameter bDescIdx==1) order of docid. Regardless, this
171336** function iterates from the end of the doclist to the beginning.
171337*/
171338SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
171339 int bDescIdx, /* True if the doclist is desc */
171340 char *aDoclist, /* Pointer to entire doclist */
171341 int nDoclist, /* Length of aDoclist in bytes */
171342 char **ppIter, /* IN/OUT: Iterator pointer */
171343 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
171344 int *pnList, /* OUT: List length pointer */
171345 u8 *pbEof /* OUT: End-of-file flag */
171346){
171347 char *p = *ppIter;
171348
171349 assert( nDoclist>0 );
171350 assert( *pbEof==0 );
171351 assert( p || *piDocid==0 );
171352 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
171353
171354 if( p==0 ){
171355 sqlite3_int64 iDocid = 0;
171356 char *pNext = 0;
171357 char *pDocid = aDoclist;
171358 char *pEnd = &aDoclist[nDoclist];
171359 int iMul = 1;
171360
171361 while( pDocid<pEnd ){
171362 sqlite3_int64 iDelta;
171363 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
171364 iDocid += (iMul * iDelta);
171365 pNext = pDocid;
171366 fts3PoslistCopy(0, &pDocid);
171367 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
171368 iMul = (bDescIdx ? -1 : 1);
171369 }
171370
171371 *pnList = (int)(pEnd - pNext);
171372 *ppIter = pNext;
171373 *piDocid = iDocid;
171374 }else{
171375 int iMul = (bDescIdx ? -1 : 1);
171376 sqlite3_int64 iDelta;
171377 fts3GetReverseVarint(&p, aDoclist, &iDelta);
171378 *piDocid -= (iMul * iDelta);
171379
171380 if( p==aDoclist ){
171381 *pbEof = 1;
171382 }else{
171383 char *pSave = p;
171384 fts3ReversePoslist(aDoclist, &p);
171385 *pnList = (int)(pSave - p);
171386 }
171387 *ppIter = p;
171388 }
171389}
171390
171391/*
171392** Iterate forwards through a doclist.
171393*/
171394SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
171395 int bDescIdx, /* True if the doclist is desc */
171396 char *aDoclist, /* Pointer to entire doclist */
171397 int nDoclist, /* Length of aDoclist in bytes */
171398 char **ppIter, /* IN/OUT: Iterator pointer */
171399 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
171400 u8 *pbEof /* OUT: End-of-file flag */
171401){
171402 char *p = *ppIter;
171403
171404 assert( nDoclist>0 );
171405 assert( *pbEof==0 );
171406 assert_fts3_nc( p || *piDocid==0 );
171407 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
171408
171409 if( p==0 ){
171410 p = aDoclist;
171411 p += sqlite3Fts3GetVarint(p, piDocid);
171412 }else{
171413 fts3PoslistCopy(0, &p);
171414 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
171415 if( p>=&aDoclist[nDoclist] ){
171416 *pbEof = 1;
171417 }else{
171418 sqlite3_int64 iVar;
171419 p += sqlite3Fts3GetVarint(p, &iVar);
171420 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
171421 }
171422 }
171423
171424 *ppIter = p;
171425}
171426
171427/*
171428** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
171429** to true if EOF is reached.
171430*/
171431static void fts3EvalDlPhraseNext(
171432 Fts3Table *pTab,
171433 Fts3Doclist *pDL,
171434 u8 *pbEof
171435){
171436 char *pIter; /* Used to iterate through aAll */
171437 char *pEnd; /* 1 byte past end of aAll */
171438
171439 if( pDL->pNextDocid ){
171440 pIter = pDL->pNextDocid;
171441 assert( pDL->aAll!=0 || pIter==0 );
171442 }else{
171443 pIter = pDL->aAll;
171444 }
171445
171446 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
171447 /* We have already reached the end of this doclist. EOF. */
171448 *pbEof = 1;
171449 }else{
171450 sqlite3_int64 iDelta;
171451 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
171452 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
171453 pDL->iDocid += iDelta;
171454 }else{
171455 pDL->iDocid -= iDelta;
171456 }
171457 pDL->pList = pIter;
171458 fts3PoslistCopy(0, &pIter);
171459 pDL->nList = (int)(pIter - pDL->pList);
171460
171461 /* pIter now points just past the 0x00 that terminates the position-
171462 ** list for document pDL->iDocid. However, if this position-list was
171463 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
171464 ** point to the start of the next docid value. The following line deals
171465 ** with this case by advancing pIter past the zero-padding added by
171466 ** fts3EvalNearTrim(). */
171467 while( pIter<pEnd && *pIter==0 ) pIter++;
171468
171469 pDL->pNextDocid = pIter;
171470 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
171471 *pbEof = 0;
171472 }
171473}
171474
171475/*
171476** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
171477*/
171478typedef struct TokenDoclist TokenDoclist;
171479struct TokenDoclist {
171480 int bIgnore;
171481 sqlite3_int64 iDocid;
171482 char *pList;
171483 int nList;
171484};
171485
171486/*
171487** Token pToken is an incrementally loaded token that is part of a
171488** multi-token phrase. Advance it to the next matching document in the
171489** database and populate output variable *p with the details of the new
171490** entry. Or, if the iterator has reached EOF, set *pbEof to true.
171491**
171492** If an error occurs, return an SQLite error code. Otherwise, return
171493** SQLITE_OK.
171494*/
171495static int incrPhraseTokenNext(
171496 Fts3Table *pTab, /* Virtual table handle */
171497 Fts3Phrase *pPhrase, /* Phrase to advance token of */
171498 int iToken, /* Specific token to advance */
171499 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
171500 u8 *pbEof /* OUT: True if iterator is at EOF */
171501){
171502 int rc = SQLITE_OK;
171503
171504 if( pPhrase->iDoclistToken==iToken ){
171505 assert( p->bIgnore==0 );
171506 assert( pPhrase->aToken[iToken].pSegcsr==0 );
171507 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
171508 p->pList = pPhrase->doclist.pList;
171509 p->nList = pPhrase->doclist.nList;
171510 p->iDocid = pPhrase->doclist.iDocid;
171511 }else{
171512 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
171513 assert( pToken->pDeferred==0 );
171514 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
171515 if( pToken->pSegcsr ){
171516 assert( p->bIgnore==0 );
171517 rc = sqlite3Fts3MsrIncrNext(
171518 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
171519 );
171520 if( p->pList==0 ) *pbEof = 1;
171521 }else{
171522 p->bIgnore = 1;
171523 }
171524 }
171525
171526 return rc;
171527}
171528
171529
171530/*
171531** The phrase iterator passed as the second argument:
171532**
171533** * features at least one token that uses an incremental doclist, and
171534**
171535** * does not contain any deferred tokens.
171536**
171537** Advance it to the next matching documnent in the database and populate
171538** the Fts3Doclist.pList and nList fields.
171539**
171540** If there is no "next" entry and no error occurs, then *pbEof is set to
171541** 1 before returning. Otherwise, if no error occurs and the iterator is
171542** successfully advanced, *pbEof is set to 0.
171543**
171544** If an error occurs, return an SQLite error code. Otherwise, return
171545** SQLITE_OK.
171546*/
171547static int fts3EvalIncrPhraseNext(
171548 Fts3Cursor *pCsr, /* FTS Cursor handle */
171549 Fts3Phrase *p, /* Phrase object to advance to next docid */
171550 u8 *pbEof /* OUT: Set to 1 if EOF */
171551){
171552 int rc = SQLITE_OK;
171553 Fts3Doclist *pDL = &p->doclist;
171554 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171555 u8 bEof = 0;
171556
171557 /* This is only called if it is guaranteed that the phrase has at least
171558 ** one incremental token. In which case the bIncr flag is set. */
171559 assert( p->bIncr==1 );
171560
171561 if( p->nToken==1 ){
171562 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
171563 &pDL->iDocid, &pDL->pList, &pDL->nList
171564 );
171565 if( pDL->pList==0 ) bEof = 1;
171566 }else{
171567 int bDescDoclist = pCsr->bDesc;
171568 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
171569
171570 memset(a, 0, sizeof(a));
171571 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
171572 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
171573
171574 while( bEof==0 ){
171575 int bMaxSet = 0;
171576 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
171577 int i; /* Used to iterate through tokens */
171578
171579 /* Advance the iterator for each token in the phrase once. */
171580 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
171581 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
171582 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
171583 iMax = a[i].iDocid;
171584 bMaxSet = 1;
171585 }
171586 }
171587 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
171588 assert( rc!=SQLITE_OK || bMaxSet );
171589
171590 /* Keep advancing iterators until they all point to the same document */
171591 for(i=0; i<p->nToken; i++){
171592 while( rc==SQLITE_OK && bEof==0
171593 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
171594 ){
171595 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
171596 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
171597 iMax = a[i].iDocid;
171598 i = 0;
171599 }
171600 }
171601 }
171602
171603 /* Check if the current entries really are a phrase match */
171604 if( bEof==0 ){
171605 int nList = 0;
171606 int nByte = a[p->nToken-1].nList;
171607 char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
171608 if( !aDoclist ) return SQLITE_NOMEM;
171609 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
171610 memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
171611
171612 for(i=0; i<(p->nToken-1); i++){
171613 if( a[i].bIgnore==0 ){
171614 char *pL = a[i].pList;
171615 char *pR = aDoclist;
171616 char *pOut = aDoclist;
171617 int nDist = p->nToken-1-i;
171618 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
171619 if( res==0 ) break;
171620 nList = (int)(pOut - aDoclist);
171621 }
171622 }
171623 if( i==(p->nToken-1) ){
171624 pDL->iDocid = iMax;
171625 pDL->pList = aDoclist;
171626 pDL->nList = nList;
171627 pDL->bFreeList = 1;
171628 break;
171629 }
171630 sqlite3_free(aDoclist);
171631 }
171632 }
171633 }
171634
171635 *pbEof = bEof;
171636 return rc;
171637}
171638
171639/*
171640** Attempt to move the phrase iterator to point to the next matching docid.
171641** If an error occurs, return an SQLite error code. Otherwise, return
171642** SQLITE_OK.
171643**
171644** If there is no "next" entry and no error occurs, then *pbEof is set to
171645** 1 before returning. Otherwise, if no error occurs and the iterator is
171646** successfully advanced, *pbEof is set to 0.
171647*/
171648static int fts3EvalPhraseNext(
171649 Fts3Cursor *pCsr, /* FTS Cursor handle */
171650 Fts3Phrase *p, /* Phrase object to advance to next docid */
171651 u8 *pbEof /* OUT: Set to 1 if EOF */
171652){
171653 int rc = SQLITE_OK;
171654 Fts3Doclist *pDL = &p->doclist;
171655 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171656
171657 if( p->bIncr ){
171658 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
171659 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
171660 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
171661 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
171662 );
171663 pDL->pList = pDL->pNextDocid;
171664 }else{
171665 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
171666 }
171667
171668 return rc;
171669}
171670
171671/*
171672**
171673** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
171674** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
171675** expression. Also the Fts3Expr.bDeferred variable is set to true for any
171676** expressions for which all descendent tokens are deferred.
171677**
171678** If parameter bOptOk is zero, then it is guaranteed that the
171679** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
171680** each phrase in the expression (subject to deferred token processing).
171681** Or, if bOptOk is non-zero, then one or more tokens within the expression
171682** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
171683**
171684** If an error occurs within this function, *pRc is set to an SQLite error
171685** code before returning.
171686*/
171687static void fts3EvalStartReaders(
171688 Fts3Cursor *pCsr, /* FTS Cursor handle */
171689 Fts3Expr *pExpr, /* Expression to initialize phrases in */
171690 int *pRc /* IN/OUT: Error code */
171691){
171692 if( pExpr && SQLITE_OK==*pRc ){
171693 if( pExpr->eType==FTSQUERY_PHRASE ){
171694 int nToken = pExpr->pPhrase->nToken;
171695 if( nToken ){
171696 int i;
171697 for(i=0; i<nToken; i++){
171698 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
171699 }
171700 pExpr->bDeferred = (i==nToken);
171701 }
171702 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
171703 }else{
171704 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
171705 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
171706 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
171707 }
171708 }
171709}
171710
171711/*
171712** An array of the following structures is assembled as part of the process
171713** of selecting tokens to defer before the query starts executing (as part
171714** of the xFilter() method). There is one element in the array for each
171715** token in the FTS expression.
171716**
171717** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
171718** to phrases that are connected only by AND and NEAR operators (not OR or
171719** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
171720** separately. The root of a tokens AND/NEAR cluster is stored in
171721** Fts3TokenAndCost.pRoot.
171722*/
171723typedef struct Fts3TokenAndCost Fts3TokenAndCost;
171724struct Fts3TokenAndCost {
171725 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
171726 int iToken; /* Position of token in phrase */
171727 Fts3PhraseToken *pToken; /* The token itself */
171728 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
171729 int nOvfl; /* Number of overflow pages to load doclist */
171730 int iCol; /* The column the token must match */
171731};
171732
171733/*
171734** This function is used to populate an allocated Fts3TokenAndCost array.
171735**
171736** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
171737** Otherwise, if an error occurs during execution, *pRc is set to an
171738** SQLite error code.
171739*/
171740static void fts3EvalTokenCosts(
171741 Fts3Cursor *pCsr, /* FTS Cursor handle */
171742 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
171743 Fts3Expr *pExpr, /* Expression to consider */
171744 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
171745 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
171746 int *pRc /* IN/OUT: Error code */
171747){
171748 if( *pRc==SQLITE_OK ){
171749 if( pExpr->eType==FTSQUERY_PHRASE ){
171750 Fts3Phrase *pPhrase = pExpr->pPhrase;
171751 int i;
171752 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
171753 Fts3TokenAndCost *pTC = (*ppTC)++;
171754 pTC->pPhrase = pPhrase;
171755 pTC->iToken = i;
171756 pTC->pRoot = pRoot;
171757 pTC->pToken = &pPhrase->aToken[i];
171758 pTC->iCol = pPhrase->iColumn;
171759 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
171760 }
171761 }else if( pExpr->eType!=FTSQUERY_NOT ){
171762 assert( pExpr->eType==FTSQUERY_OR
171763 || pExpr->eType==FTSQUERY_AND
171764 || pExpr->eType==FTSQUERY_NEAR
171765 );
171766 assert( pExpr->pLeft && pExpr->pRight );
171767 if( pExpr->eType==FTSQUERY_OR ){
171768 pRoot = pExpr->pLeft;
171769 **ppOr = pRoot;
171770 (*ppOr)++;
171771 }
171772 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
171773 if( pExpr->eType==FTSQUERY_OR ){
171774 pRoot = pExpr->pRight;
171775 **ppOr = pRoot;
171776 (*ppOr)++;
171777 }
171778 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
171779 }
171780 }
171781}
171782
171783/*
171784** Determine the average document (row) size in pages. If successful,
171785** write this value to *pnPage and return SQLITE_OK. Otherwise, return
171786** an SQLite error code.
171787**
171788** The average document size in pages is calculated by first calculating
171789** determining the average size in bytes, B. If B is less than the amount
171790** of data that will fit on a single leaf page of an intkey table in
171791** this database, then the average docsize is 1. Otherwise, it is 1 plus
171792** the number of overflow pages consumed by a record B bytes in size.
171793*/
171794static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
171795 int rc = SQLITE_OK;
171796 if( pCsr->nRowAvg==0 ){
171797 /* The average document size, which is required to calculate the cost
171798 ** of each doclist, has not yet been determined. Read the required
171799 ** data from the %_stat table to calculate it.
171800 **
171801 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
171802 ** varints, where nCol is the number of columns in the FTS3 table.
171803 ** The first varint is the number of documents currently stored in
171804 ** the table. The following nCol varints contain the total amount of
171805 ** data stored in all rows of each column of the table, from left
171806 ** to right.
171807 */
171808 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
171809 sqlite3_stmt *pStmt;
171810 sqlite3_int64 nDoc = 0;
171811 sqlite3_int64 nByte = 0;
171812 const char *pEnd;
171813 const char *a;
171814
171815 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
171816 if( rc!=SQLITE_OK ) return rc;
171817 a = sqlite3_column_blob(pStmt, 0);
171818 testcase( a==0 ); /* If %_stat.value set to X'' */
171819 if( a ){
171820 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
171821 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
171822 while( a<pEnd ){
171823 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
171824 }
171825 }
171826 if( nDoc==0 || nByte==0 ){
171827 sqlite3_reset(pStmt);
171828 return FTS_CORRUPT_VTAB;
171829 }
171830
171831 pCsr->nDoc = nDoc;
171832 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
171833 assert( pCsr->nRowAvg>0 );
171834 rc = sqlite3_reset(pStmt);
171835 }
171836
171837 *pnPage = pCsr->nRowAvg;
171838 return rc;
171839}
171840
171841/*
171842** This function is called to select the tokens (if any) that will be
171843** deferred. The array aTC[] has already been populated when this is
171844** called.
171845**
171846** This function is called once for each AND/NEAR cluster in the
171847** expression. Each invocation determines which tokens to defer within
171848** the cluster with root node pRoot. See comments above the definition
171849** of struct Fts3TokenAndCost for more details.
171850**
171851** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
171852** called on each token to defer. Otherwise, an SQLite error code is
171853** returned.
171854*/
171855static int fts3EvalSelectDeferred(
171856 Fts3Cursor *pCsr, /* FTS Cursor handle */
171857 Fts3Expr *pRoot, /* Consider tokens with this root node */
171858 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
171859 int nTC /* Number of entries in aTC[] */
171860){
171861 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171862 int nDocSize = 0; /* Number of pages per doc loaded */
171863 int rc = SQLITE_OK; /* Return code */
171864 int ii; /* Iterator variable for various purposes */
171865 int nOvfl = 0; /* Total overflow pages used by doclists */
171866 int nToken = 0; /* Total number of tokens in cluster */
171867
171868 int nMinEst = 0; /* The minimum count for any phrase so far. */
171869 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
171870
171871 /* Tokens are never deferred for FTS tables created using the content=xxx
171872 ** option. The reason being that it is not guaranteed that the content
171873 ** table actually contains the same data as the index. To prevent this from
171874 ** causing any problems, the deferred token optimization is completely
171875 ** disabled for content=xxx tables. */
171876 if( pTab->zContentTbl ){
171877 return SQLITE_OK;
171878 }
171879
171880 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
171881 ** associated with the tokens spill onto overflow pages, or if there is
171882 ** only 1 token, exit early. No tokens to defer in this case. */
171883 for(ii=0; ii<nTC; ii++){
171884 if( aTC[ii].pRoot==pRoot ){
171885 nOvfl += aTC[ii].nOvfl;
171886 nToken++;
171887 }
171888 }
171889 if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
171890
171891 /* Obtain the average docsize (in pages). */
171892 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
171893 assert( rc!=SQLITE_OK || nDocSize>0 );
171894
171895
171896 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
171897 ** of the number of overflow pages that will be loaded by the pager layer
171898 ** to retrieve the entire doclist for the token from the full-text index.
171899 ** Load the doclists for tokens that are either:
171900 **
171901 ** a. The cheapest token in the entire query (i.e. the one visited by the
171902 ** first iteration of this loop), or
171903 **
171904 ** b. Part of a multi-token phrase.
171905 **
171906 ** After each token doclist is loaded, merge it with the others from the
171907 ** same phrase and count the number of documents that the merged doclist
171908 ** contains. Set variable "nMinEst" to the smallest number of documents in
171909 ** any phrase doclist for which 1 or more token doclists have been loaded.
171910 ** Let nOther be the number of other phrases for which it is certain that
171911 ** one or more tokens will not be deferred.
171912 **
171913 ** Then, for each token, defer it if loading the doclist would result in
171914 ** loading N or more overflow pages into memory, where N is computed as:
171915 **
171916 ** (nMinEst + 4^nOther - 1) / (4^nOther)
171917 */
171918 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
171919 int iTC; /* Used to iterate through aTC[] array. */
171920 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
171921
171922 /* Set pTC to point to the cheapest remaining token. */
171923 for(iTC=0; iTC<nTC; iTC++){
171924 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
171925 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
171926 ){
171927 pTC = &aTC[iTC];
171928 }
171929 }
171930 assert( pTC );
171931
171932 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
171933 /* The number of overflow pages to load for this (and therefore all
171934 ** subsequent) tokens is greater than the estimated number of pages
171935 ** that will be loaded if all subsequent tokens are deferred.
171936 */
171937 Fts3PhraseToken *pToken = pTC->pToken;
171938 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
171939 fts3SegReaderCursorFree(pToken->pSegcsr);
171940 pToken->pSegcsr = 0;
171941 }else{
171942 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
171943 ** for-loop. Except, limit the value to 2^24 to prevent it from
171944 ** overflowing the 32-bit integer it is stored in. */
171945 if( ii<12 ) nLoad4 = nLoad4*4;
171946
171947 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
171948 /* Either this is the cheapest token in the entire query, or it is
171949 ** part of a multi-token phrase. Either way, the entire doclist will
171950 ** (eventually) be loaded into memory. It may as well be now. */
171951 Fts3PhraseToken *pToken = pTC->pToken;
171952 int nList = 0;
171953 char *pList = 0;
171954 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
171955 assert( rc==SQLITE_OK || pList==0 );
171956 if( rc==SQLITE_OK ){
171957 rc = fts3EvalPhraseMergeToken(
171958 pTab, pTC->pPhrase, pTC->iToken,pList,nList
171959 );
171960 }
171961 if( rc==SQLITE_OK ){
171962 int nCount;
171963 nCount = fts3DoclistCountDocids(
171964 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
171965 );
171966 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
171967 }
171968 }
171969 }
171970 pTC->pToken = 0;
171971 }
171972
171973 return rc;
171974}
171975
171976/*
171977** This function is called from within the xFilter method. It initializes
171978** the full-text query currently stored in pCsr->pExpr. To iterate through
171979** the results of a query, the caller does:
171980**
171981** fts3EvalStart(pCsr);
171982** while( 1 ){
171983** fts3EvalNext(pCsr);
171984** if( pCsr->bEof ) break;
171985** ... return row pCsr->iPrevId to the caller ...
171986** }
171987*/
171988static int fts3EvalStart(Fts3Cursor *pCsr){
171989 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
171990 int rc = SQLITE_OK;
171991 int nToken = 0;
171992 int nOr = 0;
171993
171994 /* Allocate a MultiSegReader for each token in the expression. */
171995 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
171996
171997 /* Determine which, if any, tokens in the expression should be deferred. */
171998#ifndef SQLITE_DISABLE_FTS4_DEFERRED
171999 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
172000 Fts3TokenAndCost *aTC;
172001 Fts3Expr **apOr;
172002 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
172003 sizeof(Fts3TokenAndCost) * nToken
172004 + sizeof(Fts3Expr *) * nOr * 2
172005 );
172006 apOr = (Fts3Expr **)&aTC[nToken];
172007
172008 if( !aTC ){
172009 rc = SQLITE_NOMEM;
172010 }else{
172011 int ii;
172012 Fts3TokenAndCost *pTC = aTC;
172013 Fts3Expr **ppOr = apOr;
172014
172015 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
172016 nToken = (int)(pTC-aTC);
172017 nOr = (int)(ppOr-apOr);
172018
172019 if( rc==SQLITE_OK ){
172020 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
172021 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
172022 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
172023 }
172024 }
172025
172026 sqlite3_free(aTC);
172027 }
172028 }
172029#endif
172030
172031 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
172032 return rc;
172033}
172034
172035/*
172036** Invalidate the current position list for phrase pPhrase.
172037*/
172038static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
172039 if( pPhrase->doclist.bFreeList ){
172040 sqlite3_free(pPhrase->doclist.pList);
172041 }
172042 pPhrase->doclist.pList = 0;
172043 pPhrase->doclist.nList = 0;
172044 pPhrase->doclist.bFreeList = 0;
172045}
172046
172047/*
172048** This function is called to edit the position list associated with
172049** the phrase object passed as the fifth argument according to a NEAR
172050** condition. For example:
172051**
172052** abc NEAR/5 "def ghi"
172053**
172054** Parameter nNear is passed the NEAR distance of the expression (5 in
172055** the example above). When this function is called, *paPoslist points to
172056** the position list, and *pnToken is the number of phrase tokens in the
172057** phrase on the other side of the NEAR operator to pPhrase. For example,
172058** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
172059** the position list associated with phrase "abc".
172060**
172061** All positions in the pPhrase position list that are not sufficiently
172062** close to a position in the *paPoslist position list are removed. If this
172063** leaves 0 positions, zero is returned. Otherwise, non-zero.
172064**
172065** Before returning, *paPoslist is set to point to the position lsit
172066** associated with pPhrase. And *pnToken is set to the number of tokens in
172067** pPhrase.
172068*/
172069static int fts3EvalNearTrim(
172070 int nNear, /* NEAR distance. As in "NEAR/nNear". */
172071 char *aTmp, /* Temporary space to use */
172072 char **paPoslist, /* IN/OUT: Position list */
172073 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
172074 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
172075){
172076 int nParam1 = nNear + pPhrase->nToken;
172077 int nParam2 = nNear + *pnToken;
172078 int nNew;
172079 char *p2;
172080 char *pOut;
172081 int res;
172082
172083 assert( pPhrase->doclist.pList );
172084
172085 p2 = pOut = pPhrase->doclist.pList;
172086 res = fts3PoslistNearMerge(
172087 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
172088 );
172089 if( res ){
172090 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
172091 if( nNew>=0 ){
172092 assert( pPhrase->doclist.pList[nNew]=='\0' );
172093 assert( nNew<=pPhrase->doclist.nList && nNew>0 );
172094 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
172095 pPhrase->doclist.nList = nNew;
172096 }
172097 *paPoslist = pPhrase->doclist.pList;
172098 *pnToken = pPhrase->nToken;
172099 }
172100
172101 return res;
172102}
172103
172104/*
172105** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
172106** Otherwise, it advances the expression passed as the second argument to
172107** point to the next matching row in the database. Expressions iterate through
172108** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
172109** or descending if it is non-zero.
172110**
172111** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
172112** successful, the following variables in pExpr are set:
172113**
172114** Fts3Expr.bEof (non-zero if EOF - there is no next row)
172115** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
172116**
172117** If the expression is of type FTSQUERY_PHRASE, and the expression is not
172118** at EOF, then the following variables are populated with the position list
172119** for the phrase for the visited row:
172120**
172121** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
172122** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
172123**
172124** It says above that this function advances the expression to the next
172125** matching row. This is usually true, but there are the following exceptions:
172126**
172127** 1. Deferred tokens are not taken into account. If a phrase consists
172128** entirely of deferred tokens, it is assumed to match every row in
172129** the db. In this case the position-list is not populated at all.
172130**
172131** Or, if a phrase contains one or more deferred tokens and one or
172132** more non-deferred tokens, then the expression is advanced to the
172133** next possible match, considering only non-deferred tokens. In other
172134** words, if the phrase is "A B C", and "B" is deferred, the expression
172135** is advanced to the next row that contains an instance of "A * C",
172136** where "*" may match any single token. The position list in this case
172137** is populated as for "A * C" before returning.
172138**
172139** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
172140** advanced to point to the next row that matches "x AND y".
172141**
172142** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
172143** really a match, taking into account deferred tokens and NEAR operators.
172144*/
172145static void fts3EvalNextRow(
172146 Fts3Cursor *pCsr, /* FTS Cursor handle */
172147 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
172148 int *pRc /* IN/OUT: Error code */
172149){
172150 if( *pRc==SQLITE_OK ){
172151 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
172152 assert( pExpr->bEof==0 );
172153 pExpr->bStart = 1;
172154
172155 switch( pExpr->eType ){
172156 case FTSQUERY_NEAR:
172157 case FTSQUERY_AND: {
172158 Fts3Expr *pLeft = pExpr->pLeft;
172159 Fts3Expr *pRight = pExpr->pRight;
172160 assert( !pLeft->bDeferred || !pRight->bDeferred );
172161
172162 if( pLeft->bDeferred ){
172163 /* LHS is entirely deferred. So we assume it matches every row.
172164 ** Advance the RHS iterator to find the next row visited. */
172165 fts3EvalNextRow(pCsr, pRight, pRc);
172166 pExpr->iDocid = pRight->iDocid;
172167 pExpr->bEof = pRight->bEof;
172168 }else if( pRight->bDeferred ){
172169 /* RHS is entirely deferred. So we assume it matches every row.
172170 ** Advance the LHS iterator to find the next row visited. */
172171 fts3EvalNextRow(pCsr, pLeft, pRc);
172172 pExpr->iDocid = pLeft->iDocid;
172173 pExpr->bEof = pLeft->bEof;
172174 }else{
172175 /* Neither the RHS or LHS are deferred. */
172176 fts3EvalNextRow(pCsr, pLeft, pRc);
172177 fts3EvalNextRow(pCsr, pRight, pRc);
172178 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
172179 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
172180 if( iDiff==0 ) break;
172181 if( iDiff<0 ){
172182 fts3EvalNextRow(pCsr, pLeft, pRc);
172183 }else{
172184 fts3EvalNextRow(pCsr, pRight, pRc);
172185 }
172186 }
172187 pExpr->iDocid = pLeft->iDocid;
172188 pExpr->bEof = (pLeft->bEof || pRight->bEof);
172189 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
172190 assert( pRight->eType==FTSQUERY_PHRASE );
172191 if( pRight->pPhrase->doclist.aAll ){
172192 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
172193 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
172194 memset(pDl->pList, 0, pDl->nList);
172195 fts3EvalNextRow(pCsr, pRight, pRc);
172196 }
172197 }
172198 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
172199 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
172200 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
172201 memset(pDl->pList, 0, pDl->nList);
172202 fts3EvalNextRow(pCsr, pLeft, pRc);
172203 }
172204 }
172205 pRight->bEof = pLeft->bEof = 1;
172206 }
172207 }
172208 break;
172209 }
172210
172211 case FTSQUERY_OR: {
172212 Fts3Expr *pLeft = pExpr->pLeft;
172213 Fts3Expr *pRight = pExpr->pRight;
172214 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
172215
172216 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
172217 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
172218
172219 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
172220 fts3EvalNextRow(pCsr, pLeft, pRc);
172221 }else if( pLeft->bEof || iCmp>0 ){
172222 fts3EvalNextRow(pCsr, pRight, pRc);
172223 }else{
172224 fts3EvalNextRow(pCsr, pLeft, pRc);
172225 fts3EvalNextRow(pCsr, pRight, pRc);
172226 }
172227
172228 pExpr->bEof = (pLeft->bEof && pRight->bEof);
172229 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
172230 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
172231 pExpr->iDocid = pLeft->iDocid;
172232 }else{
172233 pExpr->iDocid = pRight->iDocid;
172234 }
172235
172236 break;
172237 }
172238
172239 case FTSQUERY_NOT: {
172240 Fts3Expr *pLeft = pExpr->pLeft;
172241 Fts3Expr *pRight = pExpr->pRight;
172242
172243 if( pRight->bStart==0 ){
172244 fts3EvalNextRow(pCsr, pRight, pRc);
172245 assert( *pRc!=SQLITE_OK || pRight->bStart );
172246 }
172247
172248 fts3EvalNextRow(pCsr, pLeft, pRc);
172249 if( pLeft->bEof==0 ){
172250 while( !*pRc
172251 && !pRight->bEof
172252 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
172253 ){
172254 fts3EvalNextRow(pCsr, pRight, pRc);
172255 }
172256 }
172257 pExpr->iDocid = pLeft->iDocid;
172258 pExpr->bEof = pLeft->bEof;
172259 break;
172260 }
172261
172262 default: {
172263 Fts3Phrase *pPhrase = pExpr->pPhrase;
172264 fts3EvalInvalidatePoslist(pPhrase);
172265 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
172266 pExpr->iDocid = pPhrase->doclist.iDocid;
172267 break;
172268 }
172269 }
172270 }
172271}
172272
172273/*
172274** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
172275** cluster, then this function returns 1 immediately.
172276**
172277** Otherwise, it checks if the current row really does match the NEAR
172278** expression, using the data currently stored in the position lists
172279** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
172280**
172281** If the current row is a match, the position list associated with each
172282** phrase in the NEAR expression is edited in place to contain only those
172283** phrase instances sufficiently close to their peers to satisfy all NEAR
172284** constraints. In this case it returns 1. If the NEAR expression does not
172285** match the current row, 0 is returned. The position lists may or may not
172286** be edited if 0 is returned.
172287*/
172288static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
172289 int res = 1;
172290
172291 /* The following block runs if pExpr is the root of a NEAR query.
172292 ** For example, the query:
172293 **
172294 ** "w" NEAR "x" NEAR "y" NEAR "z"
172295 **
172296 ** which is represented in tree form as:
172297 **
172298 ** |
172299 ** +--NEAR--+ <-- root of NEAR query
172300 ** | |
172301 ** +--NEAR--+ "z"
172302 ** | |
172303 ** +--NEAR--+ "y"
172304 ** | |
172305 ** "w" "x"
172306 **
172307 ** The right-hand child of a NEAR node is always a phrase. The
172308 ** left-hand child may be either a phrase or a NEAR node. There are
172309 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
172310 */
172311 if( *pRc==SQLITE_OK
172312 && pExpr->eType==FTSQUERY_NEAR
172313 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
172314 ){
172315 Fts3Expr *p;
172316 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
172317 char *aTmp; /* Temp space for PoslistNearMerge() */
172318
172319 /* Allocate temporary working space. */
172320 for(p=pExpr; p->pLeft; p=p->pLeft){
172321 assert( p->pRight->pPhrase->doclist.nList>0 );
172322 nTmp += p->pRight->pPhrase->doclist.nList;
172323 }
172324 nTmp += p->pPhrase->doclist.nList;
172325 aTmp = sqlite3_malloc64(nTmp*2);
172326 if( !aTmp ){
172327 *pRc = SQLITE_NOMEM;
172328 res = 0;
172329 }else{
172330 char *aPoslist = p->pPhrase->doclist.pList;
172331 int nToken = p->pPhrase->nToken;
172332
172333 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
172334 Fts3Phrase *pPhrase = p->pRight->pPhrase;
172335 int nNear = p->nNear;
172336 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
172337 }
172338
172339 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
172340 nToken = pExpr->pRight->pPhrase->nToken;
172341 for(p=pExpr->pLeft; p && res; p=p->pLeft){
172342 int nNear;
172343 Fts3Phrase *pPhrase;
172344 assert( p->pParent && p->pParent->pLeft==p );
172345 nNear = p->pParent->nNear;
172346 pPhrase = (
172347 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
172348 );
172349 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
172350 }
172351 }
172352
172353 sqlite3_free(aTmp);
172354 }
172355
172356 return res;
172357}
172358
172359/*
172360** This function is a helper function for sqlite3Fts3EvalTestDeferred().
172361** Assuming no error occurs or has occurred, It returns non-zero if the
172362** expression passed as the second argument matches the row that pCsr
172363** currently points to, or zero if it does not.
172364**
172365** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
172366** If an error occurs during execution of this function, *pRc is set to
172367** the appropriate SQLite error code. In this case the returned value is
172368** undefined.
172369*/
172370static int fts3EvalTestExpr(
172371 Fts3Cursor *pCsr, /* FTS cursor handle */
172372 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
172373 int *pRc /* IN/OUT: Error code */
172374){
172375 int bHit = 1; /* Return value */
172376 if( *pRc==SQLITE_OK ){
172377 switch( pExpr->eType ){
172378 case FTSQUERY_NEAR:
172379 case FTSQUERY_AND:
172380 bHit = (
172381 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
172382 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
172383 && fts3EvalNearTest(pExpr, pRc)
172384 );
172385
172386 /* If the NEAR expression does not match any rows, zero the doclist for
172387 ** all phrases involved in the NEAR. This is because the snippet(),
172388 ** offsets() and matchinfo() functions are not supposed to recognize
172389 ** any instances of phrases that are part of unmatched NEAR queries.
172390 ** For example if this expression:
172391 **
172392 ** ... MATCH 'a OR (b NEAR c)'
172393 **
172394 ** is matched against a row containing:
172395 **
172396 ** 'a b d e'
172397 **
172398 ** then any snippet() should ony highlight the "a" term, not the "b"
172399 ** (as "b" is part of a non-matching NEAR clause).
172400 */
172401 if( bHit==0
172402 && pExpr->eType==FTSQUERY_NEAR
172403 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
172404 ){
172405 Fts3Expr *p;
172406 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
172407 if( p->pRight->iDocid==pCsr->iPrevId ){
172408 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
172409 }
172410 }
172411 if( p->iDocid==pCsr->iPrevId ){
172412 fts3EvalInvalidatePoslist(p->pPhrase);
172413 }
172414 }
172415
172416 break;
172417
172418 case FTSQUERY_OR: {
172419 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
172420 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
172421 bHit = bHit1 || bHit2;
172422 break;
172423 }
172424
172425 case FTSQUERY_NOT:
172426 bHit = (
172427 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
172428 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
172429 );
172430 break;
172431
172432 default: {
172433#ifndef SQLITE_DISABLE_FTS4_DEFERRED
172434 if( pCsr->pDeferred
172435 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
172436 ){
172437 Fts3Phrase *pPhrase = pExpr->pPhrase;
172438 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
172439 if( pExpr->bDeferred ){
172440 fts3EvalInvalidatePoslist(pPhrase);
172441 }
172442 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
172443 bHit = (pPhrase->doclist.pList!=0);
172444 pExpr->iDocid = pCsr->iPrevId;
172445 }else
172446#endif
172447 {
172448 bHit = (
172449 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
172450 && pExpr->pPhrase->doclist.nList>0
172451 );
172452 }
172453 break;
172454 }
172455 }
172456 }
172457 return bHit;
172458}
172459
172460/*
172461** This function is called as the second part of each xNext operation when
172462** iterating through the results of a full-text query. At this point the
172463** cursor points to a row that matches the query expression, with the
172464** following caveats:
172465**
172466** * Up until this point, "NEAR" operators in the expression have been
172467** treated as "AND".
172468**
172469** * Deferred tokens have not yet been considered.
172470**
172471** If *pRc is not SQLITE_OK when this function is called, it immediately
172472** returns 0. Otherwise, it tests whether or not after considering NEAR
172473** operators and deferred tokens the current row is still a match for the
172474** expression. It returns 1 if both of the following are true:
172475**
172476** 1. *pRc is SQLITE_OK when this function returns, and
172477**
172478** 2. After scanning the current FTS table row for the deferred tokens,
172479** it is determined that the row does *not* match the query.
172480**
172481** Or, if no error occurs and it seems the current row does match the FTS
172482** query, return 0.
172483*/
172484SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
172485 int rc = *pRc;
172486 int bMiss = 0;
172487 if( rc==SQLITE_OK ){
172488
172489 /* If there are one or more deferred tokens, load the current row into
172490 ** memory and scan it to determine the position list for each deferred
172491 ** token. Then, see if this row is really a match, considering deferred
172492 ** tokens and NEAR operators (neither of which were taken into account
172493 ** earlier, by fts3EvalNextRow()).
172494 */
172495 if( pCsr->pDeferred ){
172496 rc = fts3CursorSeek(0, pCsr);
172497 if( rc==SQLITE_OK ){
172498 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
172499 }
172500 }
172501 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
172502
172503 /* Free the position-lists accumulated for each deferred token above. */
172504 sqlite3Fts3FreeDeferredDoclists(pCsr);
172505 *pRc = rc;
172506 }
172507 return (rc==SQLITE_OK && bMiss);
172508}
172509
172510/*
172511** Advance to the next document that matches the FTS expression in
172512** Fts3Cursor.pExpr.
172513*/
172514static int fts3EvalNext(Fts3Cursor *pCsr){
172515 int rc = SQLITE_OK; /* Return Code */
172516 Fts3Expr *pExpr = pCsr->pExpr;
172517 assert( pCsr->isEof==0 );
172518 if( pExpr==0 ){
172519 pCsr->isEof = 1;
172520 }else{
172521 do {
172522 if( pCsr->isRequireSeek==0 ){
172523 sqlite3_reset(pCsr->pStmt);
172524 }
172525 assert( sqlite3_data_count(pCsr->pStmt)==0 );
172526 fts3EvalNextRow(pCsr, pExpr, &rc);
172527 pCsr->isEof = pExpr->bEof;
172528 pCsr->isRequireSeek = 1;
172529 pCsr->isMatchinfoNeeded = 1;
172530 pCsr->iPrevId = pExpr->iDocid;
172531 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
172532 }
172533
172534 /* Check if the cursor is past the end of the docid range specified
172535 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
172536 if( rc==SQLITE_OK && (
172537 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
172538 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
172539 )){
172540 pCsr->isEof = 1;
172541 }
172542
172543 return rc;
172544}
172545
172546/*
172547** Restart interation for expression pExpr so that the next call to
172548** fts3EvalNext() visits the first row. Do not allow incremental
172549** loading or merging of phrase doclists for this iteration.
172550**
172551** If *pRc is other than SQLITE_OK when this function is called, it is
172552** a no-op. If an error occurs within this function, *pRc is set to an
172553** SQLite error code before returning.
172554*/
172555static void fts3EvalRestart(
172556 Fts3Cursor *pCsr,
172557 Fts3Expr *pExpr,
172558 int *pRc
172559){
172560 if( pExpr && *pRc==SQLITE_OK ){
172561 Fts3Phrase *pPhrase = pExpr->pPhrase;
172562
172563 if( pPhrase ){
172564 fts3EvalInvalidatePoslist(pPhrase);
172565 if( pPhrase->bIncr ){
172566 int i;
172567 for(i=0; i<pPhrase->nToken; i++){
172568 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
172569 assert( pToken->pDeferred==0 );
172570 if( pToken->pSegcsr ){
172571 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
172572 }
172573 }
172574 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
172575 }
172576 pPhrase->doclist.pNextDocid = 0;
172577 pPhrase->doclist.iDocid = 0;
172578 pPhrase->pOrPoslist = 0;
172579 }
172580
172581 pExpr->iDocid = 0;
172582 pExpr->bEof = 0;
172583 pExpr->bStart = 0;
172584
172585 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
172586 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
172587 }
172588}
172589
172590/*
172591** After allocating the Fts3Expr.aMI[] array for each phrase in the
172592** expression rooted at pExpr, the cursor iterates through all rows matched
172593** by pExpr, calling this function for each row. This function increments
172594** the values in Fts3Expr.aMI[] according to the position-list currently
172595** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
172596** expression nodes.
172597*/
172598static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
172599 if( pExpr ){
172600 Fts3Phrase *pPhrase = pExpr->pPhrase;
172601 if( pPhrase && pPhrase->doclist.pList ){
172602 int iCol = 0;
172603 char *p = pPhrase->doclist.pList;
172604
172605 do{
172606 u8 c = 0;
172607 int iCnt = 0;
172608 while( 0xFE & (*p | c) ){
172609 if( (c&0x80)==0 ) iCnt++;
172610 c = *p++ & 0x80;
172611 }
172612
172613 /* aMI[iCol*3 + 1] = Number of occurrences
172614 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
172615 */
172616 pExpr->aMI[iCol*3 + 1] += iCnt;
172617 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
172618 if( *p==0x00 ) break;
172619 p++;
172620 p += fts3GetVarint32(p, &iCol);
172621 }while( iCol<nCol );
172622 }
172623
172624 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
172625 fts3EvalUpdateCounts(pExpr->pRight, nCol);
172626 }
172627}
172628
172629/*
172630** Expression pExpr must be of type FTSQUERY_PHRASE.
172631**
172632** If it is not already allocated and populated, this function allocates and
172633** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
172634** of a NEAR expression, then it also allocates and populates the same array
172635** for all other phrases that are part of the NEAR expression.
172636**
172637** SQLITE_OK is returned if the aMI[] array is successfully allocated and
172638** populated. Otherwise, if an error occurs, an SQLite error code is returned.
172639*/
172640static int fts3EvalGatherStats(
172641 Fts3Cursor *pCsr, /* Cursor object */
172642 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
172643){
172644 int rc = SQLITE_OK; /* Return code */
172645
172646 assert( pExpr->eType==FTSQUERY_PHRASE );
172647 if( pExpr->aMI==0 ){
172648 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
172649 Fts3Expr *pRoot; /* Root of NEAR expression */
172650 Fts3Expr *p; /* Iterator used for several purposes */
172651
172652 sqlite3_int64 iPrevId = pCsr->iPrevId;
172653 sqlite3_int64 iDocid;
172654 u8 bEof;
172655
172656 /* Find the root of the NEAR expression */
172657 pRoot = pExpr;
172658 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
172659 pRoot = pRoot->pParent;
172660 }
172661 iDocid = pRoot->iDocid;
172662 bEof = pRoot->bEof;
172663 assert( pRoot->bStart );
172664
172665 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
172666 for(p=pRoot; p; p=p->pLeft){
172667 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
172668 assert( pE->aMI==0 );
172669 pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
172670 if( !pE->aMI ) return SQLITE_NOMEM;
172671 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
172672 }
172673
172674 fts3EvalRestart(pCsr, pRoot, &rc);
172675
172676 while( pCsr->isEof==0 && rc==SQLITE_OK ){
172677
172678 do {
172679 /* Ensure the %_content statement is reset. */
172680 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
172681 assert( sqlite3_data_count(pCsr->pStmt)==0 );
172682
172683 /* Advance to the next document */
172684 fts3EvalNextRow(pCsr, pRoot, &rc);
172685 pCsr->isEof = pRoot->bEof;
172686 pCsr->isRequireSeek = 1;
172687 pCsr->isMatchinfoNeeded = 1;
172688 pCsr->iPrevId = pRoot->iDocid;
172689 }while( pCsr->isEof==0
172690 && pRoot->eType==FTSQUERY_NEAR
172691 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
172692 );
172693
172694 if( rc==SQLITE_OK && pCsr->isEof==0 ){
172695 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
172696 }
172697 }
172698
172699 pCsr->isEof = 0;
172700 pCsr->iPrevId = iPrevId;
172701
172702 if( bEof ){
172703 pRoot->bEof = bEof;
172704 }else{
172705 /* Caution: pRoot may iterate through docids in ascending or descending
172706 ** order. For this reason, even though it seems more defensive, the
172707 ** do loop can not be written:
172708 **
172709 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
172710 */
172711 fts3EvalRestart(pCsr, pRoot, &rc);
172712 do {
172713 fts3EvalNextRow(pCsr, pRoot, &rc);
172714 assert_fts3_nc( pRoot->bEof==0 );
172715 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB;
172716 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
172717 }
172718 }
172719 return rc;
172720}
172721
172722/*
172723** This function is used by the matchinfo() module to query a phrase
172724** expression node for the following information:
172725**
172726** 1. The total number of occurrences of the phrase in each column of
172727** the FTS table (considering all rows), and
172728**
172729** 2. For each column, the number of rows in the table for which the
172730** column contains at least one instance of the phrase.
172731**
172732** If no error occurs, SQLITE_OK is returned and the values for each column
172733** written into the array aiOut as follows:
172734**
172735** aiOut[iCol*3 + 1] = Number of occurrences
172736** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
172737**
172738** Caveats:
172739**
172740** * If a phrase consists entirely of deferred tokens, then all output
172741** values are set to the number of documents in the table. In other
172742** words we assume that very common tokens occur exactly once in each
172743** column of each row of the table.
172744**
172745** * If a phrase contains some deferred tokens (and some non-deferred
172746** tokens), count the potential occurrence identified by considering
172747** the non-deferred tokens instead of actual phrase occurrences.
172748**
172749** * If the phrase is part of a NEAR expression, then only phrase instances
172750** that meet the NEAR constraint are included in the counts.
172751*/
172752SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
172753 Fts3Cursor *pCsr, /* FTS cursor handle */
172754 Fts3Expr *pExpr, /* Phrase expression */
172755 u32 *aiOut /* Array to write results into (see above) */
172756){
172757 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
172758 int rc = SQLITE_OK;
172759 int iCol;
172760
172761 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
172762 assert( pCsr->nDoc>0 );
172763 for(iCol=0; iCol<pTab->nColumn; iCol++){
172764 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
172765 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
172766 }
172767 }else{
172768 rc = fts3EvalGatherStats(pCsr, pExpr);
172769 if( rc==SQLITE_OK ){
172770 assert( pExpr->aMI );
172771 for(iCol=0; iCol<pTab->nColumn; iCol++){
172772 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
172773 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
172774 }
172775 }
172776 }
172777
172778 return rc;
172779}
172780
172781/*
172782** The expression pExpr passed as the second argument to this function
172783** must be of type FTSQUERY_PHRASE.
172784**
172785** The returned value is either NULL or a pointer to a buffer containing
172786** a position-list indicating the occurrences of the phrase in column iCol
172787** of the current row.
172788**
172789** More specifically, the returned buffer contains 1 varint for each
172790** occurrence of the phrase in the column, stored using the normal (delta+2)
172791** compression and is terminated by either an 0x01 or 0x00 byte. For example,
172792** if the requested column contains "a b X c d X X" and the position-list
172793** for 'X' is requested, the buffer returned may contain:
172794**
172795** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
172796**
172797** This function works regardless of whether or not the phrase is deferred,
172798** incremental, or neither.
172799*/
172800SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
172801 Fts3Cursor *pCsr, /* FTS3 cursor object */
172802 Fts3Expr *pExpr, /* Phrase to return doclist for */
172803 int iCol, /* Column to return position list for */
172804 char **ppOut /* OUT: Pointer to position list */
172805){
172806 Fts3Phrase *pPhrase = pExpr->pPhrase;
172807 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
172808 char *pIter;
172809 int iThis;
172810 sqlite3_int64 iDocid;
172811
172812 /* If this phrase is applies specifically to some column other than
172813 ** column iCol, return a NULL pointer. */
172814 *ppOut = 0;
172815 assert( iCol>=0 && iCol<pTab->nColumn );
172816 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
172817 return SQLITE_OK;
172818 }
172819
172820 iDocid = pExpr->iDocid;
172821 pIter = pPhrase->doclist.pList;
172822 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
172823 int rc = SQLITE_OK;
172824 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
172825 int bOr = 0;
172826 u8 bTreeEof = 0;
172827 Fts3Expr *p; /* Used to iterate from pExpr to root */
172828 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
172829 int bMatch;
172830
172831 /* Check if this phrase descends from an OR expression node. If not,
172832 ** return NULL. Otherwise, the entry that corresponds to docid
172833 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
172834 ** tree that the node is part of has been marked as EOF, but the node
172835 ** itself is not EOF, then it may point to an earlier entry. */
172836 pNear = pExpr;
172837 for(p=pExpr->pParent; p; p=p->pParent){
172838 if( p->eType==FTSQUERY_OR ) bOr = 1;
172839 if( p->eType==FTSQUERY_NEAR ) pNear = p;
172840 if( p->bEof ) bTreeEof = 1;
172841 }
172842 if( bOr==0 ) return SQLITE_OK;
172843
172844 /* This is the descendent of an OR node. In this case we cannot use
172845 ** an incremental phrase. Load the entire doclist for the phrase
172846 ** into memory in this case. */
172847 if( pPhrase->bIncr ){
172848 int bEofSave = pNear->bEof;
172849 fts3EvalRestart(pCsr, pNear, &rc);
172850 while( rc==SQLITE_OK && !pNear->bEof ){
172851 fts3EvalNextRow(pCsr, pNear, &rc);
172852 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
172853 }
172854 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
172855 }
172856 if( bTreeEof ){
172857 while( rc==SQLITE_OK && !pNear->bEof ){
172858 fts3EvalNextRow(pCsr, pNear, &rc);
172859 }
172860 }
172861 if( rc!=SQLITE_OK ) return rc;
172862
172863 bMatch = 1;
172864 for(p=pNear; p; p=p->pLeft){
172865 u8 bEof = 0;
172866 Fts3Expr *pTest = p;
172867 Fts3Phrase *pPh;
172868 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
172869 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
172870 assert( pTest->eType==FTSQUERY_PHRASE );
172871 pPh = pTest->pPhrase;
172872
172873 pIter = pPh->pOrPoslist;
172874 iDocid = pPh->iOrDocid;
172875 if( pCsr->bDesc==bDescDoclist ){
172876 bEof = !pPh->doclist.nAll ||
172877 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
172878 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
172879 sqlite3Fts3DoclistNext(
172880 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
172881 &pIter, &iDocid, &bEof
172882 );
172883 }
172884 }else{
172885 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
172886 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
172887 int dummy;
172888 sqlite3Fts3DoclistPrev(
172889 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
172890 &pIter, &iDocid, &dummy, &bEof
172891 );
172892 }
172893 }
172894 pPh->pOrPoslist = pIter;
172895 pPh->iOrDocid = iDocid;
172896 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
172897 }
172898
172899 if( bMatch ){
172900 pIter = pPhrase->pOrPoslist;
172901 }else{
172902 pIter = 0;
172903 }
172904 }
172905 if( pIter==0 ) return SQLITE_OK;
172906
172907 if( *pIter==0x01 ){
172908 pIter++;
172909 pIter += fts3GetVarint32(pIter, &iThis);
172910 }else{
172911 iThis = 0;
172912 }
172913 while( iThis<iCol ){
172914 fts3ColumnlistCopy(0, &pIter);
172915 if( *pIter==0x00 ) return SQLITE_OK;
172916 pIter++;
172917 pIter += fts3GetVarint32(pIter, &iThis);
172918 }
172919 if( *pIter==0x00 ){
172920 pIter = 0;
172921 }
172922
172923 *ppOut = ((iCol==iThis)?pIter:0);
172924 return SQLITE_OK;
172925}
172926
172927/*
172928** Free all components of the Fts3Phrase structure that were allocated by
172929** the eval module. Specifically, this means to free:
172930**
172931** * the contents of pPhrase->doclist, and
172932** * any Fts3MultiSegReader objects held by phrase tokens.
172933*/
172934SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
172935 if( pPhrase ){
172936 int i;
172937 sqlite3_free(pPhrase->doclist.aAll);
172938 fts3EvalInvalidatePoslist(pPhrase);
172939 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
172940 for(i=0; i<pPhrase->nToken; i++){
172941 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
172942 pPhrase->aToken[i].pSegcsr = 0;
172943 }
172944 }
172945}
172946
172947
172948/*
172949** Return SQLITE_CORRUPT_VTAB.
172950*/
172951#ifdef SQLITE_DEBUG
172952SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
172953 return SQLITE_CORRUPT_VTAB;
172954}
172955#endif
172956
172957#if !SQLITE_CORE
172958/*
172959** Initialize API pointer table, if required.
172960*/
172961#ifdef _WIN32
172962__declspec(dllexport)
172963#endif
172964SQLITE_API int sqlite3_fts3_init(
172965 sqlite3 *db,
172966 char **pzErrMsg,
172967 const sqlite3_api_routines *pApi
172968){
172969 SQLITE_EXTENSION_INIT2(pApi)
172970 return sqlite3Fts3Init(db);
172971}
172972#endif
172973
172974#endif
172975
172976/************** End of fts3.c ************************************************/
172977/************** Begin file fts3_aux.c ****************************************/
172978/*
172979** 2011 Jan 27
172980**
172981** The author disclaims copyright to this source code. In place of
172982** a legal notice, here is a blessing:
172983**
172984** May you do good and not evil.
172985** May you find forgiveness for yourself and forgive others.
172986** May you share freely, never taking more than you give.
172987**
172988******************************************************************************
172989**
172990*/
172991/* #include "fts3Int.h" */
172992#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
172993
172994/* #include <string.h> */
172995/* #include <assert.h> */
172996
172997typedef struct Fts3auxTable Fts3auxTable;
172998typedef struct Fts3auxCursor Fts3auxCursor;
172999
173000struct Fts3auxTable {
173001 sqlite3_vtab base; /* Base class used by SQLite core */
173002 Fts3Table *pFts3Tab;
173003};
173004
173005struct Fts3auxCursor {
173006 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
173007 Fts3MultiSegReader csr; /* Must be right after "base" */
173008 Fts3SegFilter filter;
173009 char *zStop;
173010 int nStop; /* Byte-length of string zStop */
173011 int iLangid; /* Language id to query */
173012 int isEof; /* True if cursor is at EOF */
173013 sqlite3_int64 iRowid; /* Current rowid */
173014
173015 int iCol; /* Current value of 'col' column */
173016 int nStat; /* Size of aStat[] array */
173017 struct Fts3auxColstats {
173018 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
173019 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
173020 } *aStat;
173021};
173022
173023/*
173024** Schema of the terms table.
173025*/
173026#define FTS3_AUX_SCHEMA \
173027 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
173028
173029/*
173030** This function does all the work for both the xConnect and xCreate methods.
173031** These tables have no persistent representation of their own, so xConnect
173032** and xCreate are identical operations.
173033*/
173034static int fts3auxConnectMethod(
173035 sqlite3 *db, /* Database connection */
173036 void *pUnused, /* Unused */
173037 int argc, /* Number of elements in argv array */
173038 const char * const *argv, /* xCreate/xConnect argument array */
173039 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
173040 char **pzErr /* OUT: sqlite3_malloc'd error message */
173041){
173042 char const *zDb; /* Name of database (e.g. "main") */
173043 char const *zFts3; /* Name of fts3 table */
173044 int nDb; /* Result of strlen(zDb) */
173045 int nFts3; /* Result of strlen(zFts3) */
173046 sqlite3_int64 nByte; /* Bytes of space to allocate here */
173047 int rc; /* value returned by declare_vtab() */
173048 Fts3auxTable *p; /* Virtual table object to return */
173049
173050 UNUSED_PARAMETER(pUnused);
173051
173052 /* The user should invoke this in one of two forms:
173053 **
173054 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
173055 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
173056 */
173057 if( argc!=4 && argc!=5 ) goto bad_args;
173058
173059 zDb = argv[1];
173060 nDb = (int)strlen(zDb);
173061 if( argc==5 ){
173062 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
173063 zDb = argv[3];
173064 nDb = (int)strlen(zDb);
173065 zFts3 = argv[4];
173066 }else{
173067 goto bad_args;
173068 }
173069 }else{
173070 zFts3 = argv[3];
173071 }
173072 nFts3 = (int)strlen(zFts3);
173073
173074 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
173075 if( rc!=SQLITE_OK ) return rc;
173076
173077 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
173078 p = (Fts3auxTable *)sqlite3_malloc64(nByte);
173079 if( !p ) return SQLITE_NOMEM;
173080 memset(p, 0, nByte);
173081
173082 p->pFts3Tab = (Fts3Table *)&p[1];
173083 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
173084 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
173085 p->pFts3Tab->db = db;
173086 p->pFts3Tab->nIndex = 1;
173087
173088 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
173089 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
173090 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
173091
173092 *ppVtab = (sqlite3_vtab *)p;
173093 return SQLITE_OK;
173094
173095 bad_args:
173096 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
173097 return SQLITE_ERROR;
173098}
173099
173100/*
173101** This function does the work for both the xDisconnect and xDestroy methods.
173102** These tables have no persistent representation of their own, so xDisconnect
173103** and xDestroy are identical operations.
173104*/
173105static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
173106 Fts3auxTable *p = (Fts3auxTable *)pVtab;
173107 Fts3Table *pFts3 = p->pFts3Tab;
173108 int i;
173109
173110 /* Free any prepared statements held */
173111 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
173112 sqlite3_finalize(pFts3->aStmt[i]);
173113 }
173114 sqlite3_free(pFts3->zSegmentsTbl);
173115 sqlite3_free(p);
173116 return SQLITE_OK;
173117}
173118
173119#define FTS4AUX_EQ_CONSTRAINT 1
173120#define FTS4AUX_GE_CONSTRAINT 2
173121#define FTS4AUX_LE_CONSTRAINT 4
173122
173123/*
173124** xBestIndex - Analyze a WHERE and ORDER BY clause.
173125*/
173126static int fts3auxBestIndexMethod(
173127 sqlite3_vtab *pVTab,
173128 sqlite3_index_info *pInfo
173129){
173130 int i;
173131 int iEq = -1;
173132 int iGe = -1;
173133 int iLe = -1;
173134 int iLangid = -1;
173135 int iNext = 1; /* Next free argvIndex value */
173136
173137 UNUSED_PARAMETER(pVTab);
173138
173139 /* This vtab delivers always results in "ORDER BY term ASC" order. */
173140 if( pInfo->nOrderBy==1
173141 && pInfo->aOrderBy[0].iColumn==0
173142 && pInfo->aOrderBy[0].desc==0
173143 ){
173144 pInfo->orderByConsumed = 1;
173145 }
173146
173147 /* Search for equality and range constraints on the "term" column.
173148 ** And equality constraints on the hidden "languageid" column. */
173149 for(i=0; i<pInfo->nConstraint; i++){
173150 if( pInfo->aConstraint[i].usable ){
173151 int op = pInfo->aConstraint[i].op;
173152 int iCol = pInfo->aConstraint[i].iColumn;
173153
173154 if( iCol==0 ){
173155 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
173156 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
173157 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
173158 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
173159 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
173160 }
173161 if( iCol==4 ){
173162 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
173163 }
173164 }
173165 }
173166
173167 if( iEq>=0 ){
173168 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
173169 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
173170 pInfo->estimatedCost = 5;
173171 }else{
173172 pInfo->idxNum = 0;
173173 pInfo->estimatedCost = 20000;
173174 if( iGe>=0 ){
173175 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
173176 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
173177 pInfo->estimatedCost /= 2;
173178 }
173179 if( iLe>=0 ){
173180 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
173181 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
173182 pInfo->estimatedCost /= 2;
173183 }
173184 }
173185 if( iLangid>=0 ){
173186 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
173187 pInfo->estimatedCost--;
173188 }
173189
173190 return SQLITE_OK;
173191}
173192
173193/*
173194** xOpen - Open a cursor.
173195*/
173196static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
173197 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
173198
173199 UNUSED_PARAMETER(pVTab);
173200
173201 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
173202 if( !pCsr ) return SQLITE_NOMEM;
173203 memset(pCsr, 0, sizeof(Fts3auxCursor));
173204
173205 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
173206 return SQLITE_OK;
173207}
173208
173209/*
173210** xClose - Close a cursor.
173211*/
173212static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
173213 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
173214 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
173215
173216 sqlite3Fts3SegmentsClose(pFts3);
173217 sqlite3Fts3SegReaderFinish(&pCsr->csr);
173218 sqlite3_free((void *)pCsr->filter.zTerm);
173219 sqlite3_free(pCsr->zStop);
173220 sqlite3_free(pCsr->aStat);
173221 sqlite3_free(pCsr);
173222 return SQLITE_OK;
173223}
173224
173225static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
173226 if( nSize>pCsr->nStat ){
173227 struct Fts3auxColstats *aNew;
173228 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
173229 sizeof(struct Fts3auxColstats) * nSize
173230 );
173231 if( aNew==0 ) return SQLITE_NOMEM;
173232 memset(&aNew[pCsr->nStat], 0,
173233 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
173234 );
173235 pCsr->aStat = aNew;
173236 pCsr->nStat = nSize;
173237 }
173238 return SQLITE_OK;
173239}
173240
173241/*
173242** xNext - Advance the cursor to the next row, if any.
173243*/
173244static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
173245 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
173246 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
173247 int rc;
173248
173249 /* Increment our pretend rowid value. */
173250 pCsr->iRowid++;
173251
173252 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
173253 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
173254 }
173255
173256 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
173257 if( rc==SQLITE_ROW ){
173258 int i = 0;
173259 int nDoclist = pCsr->csr.nDoclist;
173260 char *aDoclist = pCsr->csr.aDoclist;
173261 int iCol;
173262
173263 int eState = 0;
173264
173265 if( pCsr->zStop ){
173266 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
173267 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
173268 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
173269 pCsr->isEof = 1;
173270 return SQLITE_OK;
173271 }
173272 }
173273
173274 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
173275 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
173276 iCol = 0;
173277
173278 while( i<nDoclist ){
173279 sqlite3_int64 v = 0;
173280
173281 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
173282 switch( eState ){
173283 /* State 0. In this state the integer just read was a docid. */
173284 case 0:
173285 pCsr->aStat[0].nDoc++;
173286 eState = 1;
173287 iCol = 0;
173288 break;
173289
173290 /* State 1. In this state we are expecting either a 1, indicating
173291 ** that the following integer will be a column number, or the
173292 ** start of a position list for column 0.
173293 **
173294 ** The only difference between state 1 and state 2 is that if the
173295 ** integer encountered in state 1 is not 0 or 1, then we need to
173296 ** increment the column 0 "nDoc" count for this term.
173297 */
173298 case 1:
173299 assert( iCol==0 );
173300 if( v>1 ){
173301 pCsr->aStat[1].nDoc++;
173302 }
173303 eState = 2;
173304 /* fall through */
173305
173306 case 2:
173307 if( v==0 ){ /* 0x00. Next integer will be a docid. */
173308 eState = 0;
173309 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
173310 eState = 3;
173311 }else{ /* 2 or greater. A position. */
173312 pCsr->aStat[iCol+1].nOcc++;
173313 pCsr->aStat[0].nOcc++;
173314 }
173315 break;
173316
173317 /* State 3. The integer just read is a column number. */
173318 default: assert( eState==3 );
173319 iCol = (int)v;
173320 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
173321 pCsr->aStat[iCol+1].nDoc++;
173322 eState = 2;
173323 break;
173324 }
173325 }
173326
173327 pCsr->iCol = 0;
173328 rc = SQLITE_OK;
173329 }else{
173330 pCsr->isEof = 1;
173331 }
173332 return rc;
173333}
173334
173335/*
173336** xFilter - Initialize a cursor to point at the start of its data.
173337*/
173338static int fts3auxFilterMethod(
173339 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
173340 int idxNum, /* Strategy index */
173341 const char *idxStr, /* Unused */
173342 int nVal, /* Number of elements in apVal */
173343 sqlite3_value **apVal /* Arguments for the indexing scheme */
173344){
173345 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
173346 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
173347 int rc;
173348 int isScan = 0;
173349 int iLangVal = 0; /* Language id to query */
173350
173351 int iEq = -1; /* Index of term=? value in apVal */
173352 int iGe = -1; /* Index of term>=? value in apVal */
173353 int iLe = -1; /* Index of term<=? value in apVal */
173354 int iLangid = -1; /* Index of languageid=? value in apVal */
173355 int iNext = 0;
173356
173357 UNUSED_PARAMETER(nVal);
173358 UNUSED_PARAMETER(idxStr);
173359
173360 assert( idxStr==0 );
173361 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
173362 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
173363 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
173364 );
173365
173366 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
173367 iEq = iNext++;
173368 }else{
173369 isScan = 1;
173370 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
173371 iGe = iNext++;
173372 }
173373 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
173374 iLe = iNext++;
173375 }
173376 }
173377 if( iNext<nVal ){
173378 iLangid = iNext++;
173379 }
173380
173381 /* In case this cursor is being reused, close and zero it. */
173382 testcase(pCsr->filter.zTerm);
173383 sqlite3Fts3SegReaderFinish(&pCsr->csr);
173384 sqlite3_free((void *)pCsr->filter.zTerm);
173385 sqlite3_free(pCsr->aStat);
173386 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
173387
173388 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
173389 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
173390
173391 if( iEq>=0 || iGe>=0 ){
173392 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
173393 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
173394 if( zStr ){
173395 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
173396 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
173397 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
173398 }
173399 }
173400
173401 if( iLe>=0 ){
173402 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
173403 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
173404 pCsr->nStop = (int)strlen(pCsr->zStop);
173405 }
173406
173407 if( iLangid>=0 ){
173408 iLangVal = sqlite3_value_int(apVal[iLangid]);
173409
173410 /* If the user specified a negative value for the languageid, use zero
173411 ** instead. This works, as the "languageid=?" constraint will also
173412 ** be tested by the VDBE layer. The test will always be false (since
173413 ** this module will not return a row with a negative languageid), and
173414 ** so the overall query will return zero rows. */
173415 if( iLangVal<0 ) iLangVal = 0;
173416 }
173417 pCsr->iLangid = iLangVal;
173418
173419 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
173420 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
173421 );
173422 if( rc==SQLITE_OK ){
173423 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
173424 }
173425
173426 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
173427 return rc;
173428}
173429
173430/*
173431** xEof - Return true if the cursor is at EOF, or false otherwise.
173432*/
173433static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
173434 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
173435 return pCsr->isEof;
173436}
173437
173438/*
173439** xColumn - Return a column value.
173440*/
173441static int fts3auxColumnMethod(
173442 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
173443 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
173444 int iCol /* Index of column to read value from */
173445){
173446 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
173447
173448 assert( p->isEof==0 );
173449 switch( iCol ){
173450 case 0: /* term */
173451 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
173452 break;
173453
173454 case 1: /* col */
173455 if( p->iCol ){
173456 sqlite3_result_int(pCtx, p->iCol-1);
173457 }else{
173458 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
173459 }
173460 break;
173461
173462 case 2: /* documents */
173463 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
173464 break;
173465
173466 case 3: /* occurrences */
173467 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
173468 break;
173469
173470 default: /* languageid */
173471 assert( iCol==4 );
173472 sqlite3_result_int(pCtx, p->iLangid);
173473 break;
173474 }
173475
173476 return SQLITE_OK;
173477}
173478
173479/*
173480** xRowid - Return the current rowid for the cursor.
173481*/
173482static int fts3auxRowidMethod(
173483 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
173484 sqlite_int64 *pRowid /* OUT: Rowid value */
173485){
173486 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
173487 *pRowid = pCsr->iRowid;
173488 return SQLITE_OK;
173489}
173490
173491/*
173492** Register the fts3aux module with database connection db. Return SQLITE_OK
173493** if successful or an error code if sqlite3_create_module() fails.
173494*/
173495SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
173496 static const sqlite3_module fts3aux_module = {
173497 0, /* iVersion */
173498 fts3auxConnectMethod, /* xCreate */
173499 fts3auxConnectMethod, /* xConnect */
173500 fts3auxBestIndexMethod, /* xBestIndex */
173501 fts3auxDisconnectMethod, /* xDisconnect */
173502 fts3auxDisconnectMethod, /* xDestroy */
173503 fts3auxOpenMethod, /* xOpen */
173504 fts3auxCloseMethod, /* xClose */
173505 fts3auxFilterMethod, /* xFilter */
173506 fts3auxNextMethod, /* xNext */
173507 fts3auxEofMethod, /* xEof */
173508 fts3auxColumnMethod, /* xColumn */
173509 fts3auxRowidMethod, /* xRowid */
173510 0, /* xUpdate */
173511 0, /* xBegin */
173512 0, /* xSync */
173513 0, /* xCommit */
173514 0, /* xRollback */
173515 0, /* xFindFunction */
173516 0, /* xRename */
173517 0, /* xSavepoint */
173518 0, /* xRelease */
173519 0, /* xRollbackTo */
173520 0 /* xShadowName */
173521 };
173522 int rc; /* Return code */
173523
173524 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
173525 return rc;
173526}
173527
173528#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
173529
173530/************** End of fts3_aux.c ********************************************/
173531/************** Begin file fts3_expr.c ***************************************/
173532/*
173533** 2008 Nov 28
173534**
173535** The author disclaims copyright to this source code. In place of
173536** a legal notice, here is a blessing:
173537**
173538** May you do good and not evil.
173539** May you find forgiveness for yourself and forgive others.
173540** May you share freely, never taking more than you give.
173541**
173542******************************************************************************
173543**
173544** This module contains code that implements a parser for fts3 query strings
173545** (the right-hand argument to the MATCH operator). Because the supported
173546** syntax is relatively simple, the whole tokenizer/parser system is
173547** hand-coded.
173548*/
173549/* #include "fts3Int.h" */
173550#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
173551
173552/*
173553** By default, this module parses the legacy syntax that has been
173554** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
173555** is defined, then it uses the new syntax. The differences between
173556** the new and the old syntaxes are:
173557**
173558** a) The new syntax supports parenthesis. The old does not.
173559**
173560** b) The new syntax supports the AND and NOT operators. The old does not.
173561**
173562** c) The old syntax supports the "-" token qualifier. This is not
173563** supported by the new syntax (it is replaced by the NOT operator).
173564**
173565** d) When using the old syntax, the OR operator has a greater precedence
173566** than an implicit AND. When using the new, both implicity and explicit
173567** AND operators have a higher precedence than OR.
173568**
173569** If compiled with SQLITE_TEST defined, then this module exports the
173570** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
173571** to zero causes the module to use the old syntax. If it is set to
173572** non-zero the new syntax is activated. This is so both syntaxes can
173573** be tested using a single build of testfixture.
173574**
173575** The following describes the syntax supported by the fts3 MATCH
173576** operator in a similar format to that used by the lemon parser
173577** generator. This module does not use actually lemon, it uses a
173578** custom parser.
173579**
173580** query ::= andexpr (OR andexpr)*.
173581**
173582** andexpr ::= notexpr (AND? notexpr)*.
173583**
173584** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
173585** notexpr ::= LP query RP.
173586**
173587** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
173588**
173589** distance_opt ::= .
173590** distance_opt ::= / INTEGER.
173591**
173592** phrase ::= TOKEN.
173593** phrase ::= COLUMN:TOKEN.
173594** phrase ::= "TOKEN TOKEN TOKEN...".
173595*/
173596
173597#ifdef SQLITE_TEST
173598SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
173599#else
173600# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
173601# define sqlite3_fts3_enable_parentheses 1
173602# else
173603# define sqlite3_fts3_enable_parentheses 0
173604# endif
173605#endif
173606
173607/*
173608** Default span for NEAR operators.
173609*/
173610#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
173611
173612/* #include <string.h> */
173613/* #include <assert.h> */
173614
173615/*
173616** isNot:
173617** This variable is used by function getNextNode(). When getNextNode() is
173618** called, it sets ParseContext.isNot to true if the 'next node' is a
173619** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
173620** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
173621** zero.
173622*/
173623typedef struct ParseContext ParseContext;
173624struct ParseContext {
173625 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
173626 int iLangid; /* Language id used with tokenizer */
173627 const char **azCol; /* Array of column names for fts3 table */
173628 int bFts4; /* True to allow FTS4-only syntax */
173629 int nCol; /* Number of entries in azCol[] */
173630 int iDefaultCol; /* Default column to query */
173631 int isNot; /* True if getNextNode() sees a unary - */
173632 sqlite3_context *pCtx; /* Write error message here */
173633 int nNest; /* Number of nested brackets */
173634};
173635
173636/*
173637** This function is equivalent to the standard isspace() function.
173638**
173639** The standard isspace() can be awkward to use safely, because although it
173640** is defined to accept an argument of type int, its behavior when passed
173641** an integer that falls outside of the range of the unsigned char type
173642** is undefined (and sometimes, "undefined" means segfault). This wrapper
173643** is defined to accept an argument of type char, and always returns 0 for
173644** any values that fall outside of the range of the unsigned char type (i.e.
173645** negative values).
173646*/
173647static int fts3isspace(char c){
173648 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
173649}
173650
173651/*
173652** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
173653** zero the memory before returning a pointer to it. If unsuccessful,
173654** return NULL.
173655*/
173656static void *fts3MallocZero(sqlite3_int64 nByte){
173657 void *pRet = sqlite3_malloc64(nByte);
173658 if( pRet ) memset(pRet, 0, nByte);
173659 return pRet;
173660}
173661
173662SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
173663 sqlite3_tokenizer *pTokenizer,
173664 int iLangid,
173665 const char *z,
173666 int n,
173667 sqlite3_tokenizer_cursor **ppCsr
173668){
173669 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
173670 sqlite3_tokenizer_cursor *pCsr = 0;
173671 int rc;
173672
173673 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
173674 assert( rc==SQLITE_OK || pCsr==0 );
173675 if( rc==SQLITE_OK ){
173676 pCsr->pTokenizer = pTokenizer;
173677 if( pModule->iVersion>=1 ){
173678 rc = pModule->xLanguageid(pCsr, iLangid);
173679 if( rc!=SQLITE_OK ){
173680 pModule->xClose(pCsr);
173681 pCsr = 0;
173682 }
173683 }
173684 }
173685 *ppCsr = pCsr;
173686 return rc;
173687}
173688
173689/*
173690** Function getNextNode(), which is called by fts3ExprParse(), may itself
173691** call fts3ExprParse(). So this forward declaration is required.
173692*/
173693static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
173694
173695/*
173696** Extract the next token from buffer z (length n) using the tokenizer
173697** and other information (column names etc.) in pParse. Create an Fts3Expr
173698** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
173699** single token and set *ppExpr to point to it. If the end of the buffer is
173700** reached before a token is found, set *ppExpr to zero. It is the
173701** responsibility of the caller to eventually deallocate the allocated
173702** Fts3Expr structure (if any) by passing it to sqlite3_free().
173703**
173704** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
173705** fails.
173706*/
173707static int getNextToken(
173708 ParseContext *pParse, /* fts3 query parse context */
173709 int iCol, /* Value for Fts3Phrase.iColumn */
173710 const char *z, int n, /* Input string */
173711 Fts3Expr **ppExpr, /* OUT: expression */
173712 int *pnConsumed /* OUT: Number of bytes consumed */
173713){
173714 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
173715 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
173716 int rc;
173717 sqlite3_tokenizer_cursor *pCursor;
173718 Fts3Expr *pRet = 0;
173719 int i = 0;
173720
173721 /* Set variable i to the maximum number of bytes of input to tokenize. */
173722 for(i=0; i<n; i++){
173723 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
173724 if( z[i]=='"' ) break;
173725 }
173726
173727 *pnConsumed = i;
173728 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
173729 if( rc==SQLITE_OK ){
173730 const char *zToken;
173731 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
173732 sqlite3_int64 nByte; /* total space to allocate */
173733
173734 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
173735 if( rc==SQLITE_OK ){
173736 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
173737 pRet = (Fts3Expr *)fts3MallocZero(nByte);
173738 if( !pRet ){
173739 rc = SQLITE_NOMEM;
173740 }else{
173741 pRet->eType = FTSQUERY_PHRASE;
173742 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
173743 pRet->pPhrase->nToken = 1;
173744 pRet->pPhrase->iColumn = iCol;
173745 pRet->pPhrase->aToken[0].n = nToken;
173746 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
173747 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
173748
173749 if( iEnd<n && z[iEnd]=='*' ){
173750 pRet->pPhrase->aToken[0].isPrefix = 1;
173751 iEnd++;
173752 }
173753
173754 while( 1 ){
173755 if( !sqlite3_fts3_enable_parentheses
173756 && iStart>0 && z[iStart-1]=='-'
173757 ){
173758 pParse->isNot = 1;
173759 iStart--;
173760 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
173761 pRet->pPhrase->aToken[0].bFirst = 1;
173762 iStart--;
173763 }else{
173764 break;
173765 }
173766 }
173767
173768 }
173769 *pnConsumed = iEnd;
173770 }else if( i && rc==SQLITE_DONE ){
173771 rc = SQLITE_OK;
173772 }
173773
173774 pModule->xClose(pCursor);
173775 }
173776
173777 *ppExpr = pRet;
173778 return rc;
173779}
173780
173781
173782/*
173783** Enlarge a memory allocation. If an out-of-memory allocation occurs,
173784** then free the old allocation.
173785*/
173786static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
173787 void *pRet = sqlite3_realloc64(pOrig, nNew);
173788 if( !pRet ){
173789 sqlite3_free(pOrig);
173790 }
173791 return pRet;
173792}
173793
173794/*
173795** Buffer zInput, length nInput, contains the contents of a quoted string
173796** that appeared as part of an fts3 query expression. Neither quote character
173797** is included in the buffer. This function attempts to tokenize the entire
173798** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
173799** containing the results.
173800**
173801** If successful, SQLITE_OK is returned and *ppExpr set to point at the
173802** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
173803** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
173804** to 0.
173805*/
173806static int getNextString(
173807 ParseContext *pParse, /* fts3 query parse context */
173808 const char *zInput, int nInput, /* Input string */
173809 Fts3Expr **ppExpr /* OUT: expression */
173810){
173811 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
173812 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
173813 int rc;
173814 Fts3Expr *p = 0;
173815 sqlite3_tokenizer_cursor *pCursor = 0;
173816 char *zTemp = 0;
173817 int nTemp = 0;
173818
173819 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
173820 int nToken = 0;
173821
173822 /* The final Fts3Expr data structure, including the Fts3Phrase,
173823 ** Fts3PhraseToken structures token buffers are all stored as a single
173824 ** allocation so that the expression can be freed with a single call to
173825 ** sqlite3_free(). Setting this up requires a two pass approach.
173826 **
173827 ** The first pass, in the block below, uses a tokenizer cursor to iterate
173828 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
173829 ** to assemble data in two dynamic buffers:
173830 **
173831 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
173832 ** structure, followed by the array of Fts3PhraseToken
173833 ** structures. This pass only populates the Fts3PhraseToken array.
173834 **
173835 ** Buffer zTemp: Contains copies of all tokens.
173836 **
173837 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
173838 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
173839 ** structures.
173840 */
173841 rc = sqlite3Fts3OpenTokenizer(
173842 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
173843 if( rc==SQLITE_OK ){
173844 int ii;
173845 for(ii=0; rc==SQLITE_OK; ii++){
173846 const char *zByte;
173847 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
173848 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
173849 if( rc==SQLITE_OK ){
173850 Fts3PhraseToken *pToken;
173851
173852 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
173853 if( !p ) goto no_mem;
173854
173855 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
173856 if( !zTemp ) goto no_mem;
173857
173858 assert( nToken==ii );
173859 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
173860 memset(pToken, 0, sizeof(Fts3PhraseToken));
173861
173862 memcpy(&zTemp[nTemp], zByte, nByte);
173863 nTemp += nByte;
173864
173865 pToken->n = nByte;
173866 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
173867 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
173868 nToken = ii+1;
173869 }
173870 }
173871
173872 pModule->xClose(pCursor);
173873 pCursor = 0;
173874 }
173875
173876 if( rc==SQLITE_DONE ){
173877 int jj;
173878 char *zBuf = 0;
173879
173880 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
173881 if( !p ) goto no_mem;
173882 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
173883 p->eType = FTSQUERY_PHRASE;
173884 p->pPhrase = (Fts3Phrase *)&p[1];
173885 p->pPhrase->iColumn = pParse->iDefaultCol;
173886 p->pPhrase->nToken = nToken;
173887
173888 zBuf = (char *)&p->pPhrase->aToken[nToken];
173889 if( zTemp ){
173890 memcpy(zBuf, zTemp, nTemp);
173891 sqlite3_free(zTemp);
173892 }else{
173893 assert( nTemp==0 );
173894 }
173895
173896 for(jj=0; jj<p->pPhrase->nToken; jj++){
173897 p->pPhrase->aToken[jj].z = zBuf;
173898 zBuf += p->pPhrase->aToken[jj].n;
173899 }
173900 rc = SQLITE_OK;
173901 }
173902
173903 *ppExpr = p;
173904 return rc;
173905no_mem:
173906
173907 if( pCursor ){
173908 pModule->xClose(pCursor);
173909 }
173910 sqlite3_free(zTemp);
173911 sqlite3_free(p);
173912 *ppExpr = 0;
173913 return SQLITE_NOMEM;
173914}
173915
173916/*
173917** The output variable *ppExpr is populated with an allocated Fts3Expr
173918** structure, or set to 0 if the end of the input buffer is reached.
173919**
173920** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
173921** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
173922** If SQLITE_ERROR is returned, pContext is populated with an error message.
173923*/
173924static int getNextNode(
173925 ParseContext *pParse, /* fts3 query parse context */
173926 const char *z, int n, /* Input string */
173927 Fts3Expr **ppExpr, /* OUT: expression */
173928 int *pnConsumed /* OUT: Number of bytes consumed */
173929){
173930 static const struct Fts3Keyword {
173931 char *z; /* Keyword text */
173932 unsigned char n; /* Length of the keyword */
173933 unsigned char parenOnly; /* Only valid in paren mode */
173934 unsigned char eType; /* Keyword code */
173935 } aKeyword[] = {
173936 { "OR" , 2, 0, FTSQUERY_OR },
173937 { "AND", 3, 1, FTSQUERY_AND },
173938 { "NOT", 3, 1, FTSQUERY_NOT },
173939 { "NEAR", 4, 0, FTSQUERY_NEAR }
173940 };
173941 int ii;
173942 int iCol;
173943 int iColLen;
173944 int rc;
173945 Fts3Expr *pRet = 0;
173946
173947 const char *zInput = z;
173948 int nInput = n;
173949
173950 pParse->isNot = 0;
173951
173952 /* Skip over any whitespace before checking for a keyword, an open or
173953 ** close bracket, or a quoted string.
173954 */
173955 while( nInput>0 && fts3isspace(*zInput) ){
173956 nInput--;
173957 zInput++;
173958 }
173959 if( nInput==0 ){
173960 return SQLITE_DONE;
173961 }
173962
173963 /* See if we are dealing with a keyword. */
173964 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
173965 const struct Fts3Keyword *pKey = &aKeyword[ii];
173966
173967 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
173968 continue;
173969 }
173970
173971 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
173972 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
173973 int nKey = pKey->n;
173974 char cNext;
173975
173976 /* If this is a "NEAR" keyword, check for an explicit nearness. */
173977 if( pKey->eType==FTSQUERY_NEAR ){
173978 assert( nKey==4 );
173979 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
173980 nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear);
173981 }
173982 }
173983
173984 /* At this point this is probably a keyword. But for that to be true,
173985 ** the next byte must contain either whitespace, an open or close
173986 ** parenthesis, a quote character, or EOF.
173987 */
173988 cNext = zInput[nKey];
173989 if( fts3isspace(cNext)
173990 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
173991 ){
173992 pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
173993 if( !pRet ){
173994 return SQLITE_NOMEM;
173995 }
173996 pRet->eType = pKey->eType;
173997 pRet->nNear = nNear;
173998 *ppExpr = pRet;
173999 *pnConsumed = (int)((zInput - z) + nKey);
174000 return SQLITE_OK;
174001 }
174002
174003 /* Turns out that wasn't a keyword after all. This happens if the
174004 ** user has supplied a token such as "ORacle". Continue.
174005 */
174006 }
174007 }
174008
174009 /* See if we are dealing with a quoted phrase. If this is the case, then
174010 ** search for the closing quote and pass the whole string to getNextString()
174011 ** for processing. This is easy to do, as fts3 has no syntax for escaping
174012 ** a quote character embedded in a string.
174013 */
174014 if( *zInput=='"' ){
174015 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
174016 *pnConsumed = (int)((zInput - z) + ii + 1);
174017 if( ii==nInput ){
174018 return SQLITE_ERROR;
174019 }
174020 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
174021 }
174022
174023 if( sqlite3_fts3_enable_parentheses ){
174024 if( *zInput=='(' ){
174025 int nConsumed = 0;
174026 pParse->nNest++;
174027 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
174028 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
174029 return rc;
174030 }else if( *zInput==')' ){
174031 pParse->nNest--;
174032 *pnConsumed = (int)((zInput - z) + 1);
174033 *ppExpr = 0;
174034 return SQLITE_DONE;
174035 }
174036 }
174037
174038 /* If control flows to this point, this must be a regular token, or
174039 ** the end of the input. Read a regular token using the sqlite3_tokenizer
174040 ** interface. Before doing so, figure out if there is an explicit
174041 ** column specifier for the token.
174042 **
174043 ** TODO: Strangely, it is not possible to associate a column specifier
174044 ** with a quoted phrase, only with a single token. Not sure if this was
174045 ** an implementation artifact or an intentional decision when fts3 was
174046 ** first implemented. Whichever it was, this module duplicates the
174047 ** limitation.
174048 */
174049 iCol = pParse->iDefaultCol;
174050 iColLen = 0;
174051 for(ii=0; ii<pParse->nCol; ii++){
174052 const char *zStr = pParse->azCol[ii];
174053 int nStr = (int)strlen(zStr);
174054 if( nInput>nStr && zInput[nStr]==':'
174055 && sqlite3_strnicmp(zStr, zInput, nStr)==0
174056 ){
174057 iCol = ii;
174058 iColLen = (int)((zInput - z) + nStr + 1);
174059 break;
174060 }
174061 }
174062 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
174063 *pnConsumed += iColLen;
174064 return rc;
174065}
174066
174067/*
174068** The argument is an Fts3Expr structure for a binary operator (any type
174069** except an FTSQUERY_PHRASE). Return an integer value representing the
174070** precedence of the operator. Lower values have a higher precedence (i.e.
174071** group more tightly). For example, in the C language, the == operator
174072** groups more tightly than ||, and would therefore have a higher precedence.
174073**
174074** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
174075** is defined), the order of the operators in precedence from highest to
174076** lowest is:
174077**
174078** NEAR
174079** NOT
174080** AND (including implicit ANDs)
174081** OR
174082**
174083** Note that when using the old query syntax, the OR operator has a higher
174084** precedence than the AND operator.
174085*/
174086static int opPrecedence(Fts3Expr *p){
174087 assert( p->eType!=FTSQUERY_PHRASE );
174088 if( sqlite3_fts3_enable_parentheses ){
174089 return p->eType;
174090 }else if( p->eType==FTSQUERY_NEAR ){
174091 return 1;
174092 }else if( p->eType==FTSQUERY_OR ){
174093 return 2;
174094 }
174095 assert( p->eType==FTSQUERY_AND );
174096 return 3;
174097}
174098
174099/*
174100** Argument ppHead contains a pointer to the current head of a query
174101** expression tree being parsed. pPrev is the expression node most recently
174102** inserted into the tree. This function adds pNew, which is always a binary
174103** operator node, into the expression tree based on the relative precedence
174104** of pNew and the existing nodes of the tree. This may result in the head
174105** of the tree changing, in which case *ppHead is set to the new root node.
174106*/
174107static void insertBinaryOperator(
174108 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
174109 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
174110 Fts3Expr *pNew /* New binary node to insert into expression tree */
174111){
174112 Fts3Expr *pSplit = pPrev;
174113 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
174114 pSplit = pSplit->pParent;
174115 }
174116
174117 if( pSplit->pParent ){
174118 assert( pSplit->pParent->pRight==pSplit );
174119 pSplit->pParent->pRight = pNew;
174120 pNew->pParent = pSplit->pParent;
174121 }else{
174122 *ppHead = pNew;
174123 }
174124 pNew->pLeft = pSplit;
174125 pSplit->pParent = pNew;
174126}
174127
174128/*
174129** Parse the fts3 query expression found in buffer z, length n. This function
174130** returns either when the end of the buffer is reached or an unmatched
174131** closing bracket - ')' - is encountered.
174132**
174133** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
174134** parsed form of the expression and *pnConsumed is set to the number of
174135** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
174136** (out of memory error) or SQLITE_ERROR (parse error) is returned.
174137*/
174138static int fts3ExprParse(
174139 ParseContext *pParse, /* fts3 query parse context */
174140 const char *z, int n, /* Text of MATCH query */
174141 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
174142 int *pnConsumed /* OUT: Number of bytes consumed */
174143){
174144 Fts3Expr *pRet = 0;
174145 Fts3Expr *pPrev = 0;
174146 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
174147 int nIn = n;
174148 const char *zIn = z;
174149 int rc = SQLITE_OK;
174150 int isRequirePhrase = 1;
174151
174152 while( rc==SQLITE_OK ){
174153 Fts3Expr *p = 0;
174154 int nByte = 0;
174155
174156 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
174157 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
174158 if( rc==SQLITE_OK ){
174159 if( p ){
174160 int isPhrase;
174161
174162 if( !sqlite3_fts3_enable_parentheses
174163 && p->eType==FTSQUERY_PHRASE && pParse->isNot
174164 ){
174165 /* Create an implicit NOT operator. */
174166 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
174167 if( !pNot ){
174168 sqlite3Fts3ExprFree(p);
174169 rc = SQLITE_NOMEM;
174170 goto exprparse_out;
174171 }
174172 pNot->eType = FTSQUERY_NOT;
174173 pNot->pRight = p;
174174 p->pParent = pNot;
174175 if( pNotBranch ){
174176 pNot->pLeft = pNotBranch;
174177 pNotBranch->pParent = pNot;
174178 }
174179 pNotBranch = pNot;
174180 p = pPrev;
174181 }else{
174182 int eType = p->eType;
174183 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
174184
174185 /* The isRequirePhrase variable is set to true if a phrase or
174186 ** an expression contained in parenthesis is required. If a
174187 ** binary operator (AND, OR, NOT or NEAR) is encounted when
174188 ** isRequirePhrase is set, this is a syntax error.
174189 */
174190 if( !isPhrase && isRequirePhrase ){
174191 sqlite3Fts3ExprFree(p);
174192 rc = SQLITE_ERROR;
174193 goto exprparse_out;
174194 }
174195
174196 if( isPhrase && !isRequirePhrase ){
174197 /* Insert an implicit AND operator. */
174198 Fts3Expr *pAnd;
174199 assert( pRet && pPrev );
174200 pAnd = fts3MallocZero(sizeof(Fts3Expr));
174201 if( !pAnd ){
174202 sqlite3Fts3ExprFree(p);
174203 rc = SQLITE_NOMEM;
174204 goto exprparse_out;
174205 }
174206 pAnd->eType = FTSQUERY_AND;
174207 insertBinaryOperator(&pRet, pPrev, pAnd);
174208 pPrev = pAnd;
174209 }
174210
174211 /* This test catches attempts to make either operand of a NEAR
174212 ** operator something other than a phrase. For example, either of
174213 ** the following:
174214 **
174215 ** (bracketed expression) NEAR phrase
174216 ** phrase NEAR (bracketed expression)
174217 **
174218 ** Return an error in either case.
174219 */
174220 if( pPrev && (
174221 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
174222 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
174223 )){
174224 sqlite3Fts3ExprFree(p);
174225 rc = SQLITE_ERROR;
174226 goto exprparse_out;
174227 }
174228
174229 if( isPhrase ){
174230 if( pRet ){
174231 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
174232 pPrev->pRight = p;
174233 p->pParent = pPrev;
174234 }else{
174235 pRet = p;
174236 }
174237 }else{
174238 insertBinaryOperator(&pRet, pPrev, p);
174239 }
174240 isRequirePhrase = !isPhrase;
174241 }
174242 pPrev = p;
174243 }
174244 assert( nByte>0 );
174245 }
174246 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
174247 nIn -= nByte;
174248 zIn += nByte;
174249 }
174250
174251 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
174252 rc = SQLITE_ERROR;
174253 }
174254
174255 if( rc==SQLITE_DONE ){
174256 rc = SQLITE_OK;
174257 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
174258 if( !pRet ){
174259 rc = SQLITE_ERROR;
174260 }else{
174261 Fts3Expr *pIter = pNotBranch;
174262 while( pIter->pLeft ){
174263 pIter = pIter->pLeft;
174264 }
174265 pIter->pLeft = pRet;
174266 pRet->pParent = pIter;
174267 pRet = pNotBranch;
174268 }
174269 }
174270 }
174271 *pnConsumed = n - nIn;
174272
174273exprparse_out:
174274 if( rc!=SQLITE_OK ){
174275 sqlite3Fts3ExprFree(pRet);
174276 sqlite3Fts3ExprFree(pNotBranch);
174277 pRet = 0;
174278 }
174279 *ppExpr = pRet;
174280 return rc;
174281}
174282
174283/*
174284** Return SQLITE_ERROR if the maximum depth of the expression tree passed
174285** as the only argument is more than nMaxDepth.
174286*/
174287static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
174288 int rc = SQLITE_OK;
174289 if( p ){
174290 if( nMaxDepth<0 ){
174291 rc = SQLITE_TOOBIG;
174292 }else{
174293 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
174294 if( rc==SQLITE_OK ){
174295 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
174296 }
174297 }
174298 }
174299 return rc;
174300}
174301
174302/*
174303** This function attempts to transform the expression tree at (*pp) to
174304** an equivalent but more balanced form. The tree is modified in place.
174305** If successful, SQLITE_OK is returned and (*pp) set to point to the
174306** new root expression node.
174307**
174308** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
174309**
174310** Otherwise, if an error occurs, an SQLite error code is returned and
174311** expression (*pp) freed.
174312*/
174313static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
174314 int rc = SQLITE_OK; /* Return code */
174315 Fts3Expr *pRoot = *pp; /* Initial root node */
174316 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
174317 int eType = pRoot->eType; /* Type of node in this tree */
174318
174319 if( nMaxDepth==0 ){
174320 rc = SQLITE_ERROR;
174321 }
174322
174323 if( rc==SQLITE_OK ){
174324 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
174325 Fts3Expr **apLeaf;
174326 apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
174327 if( 0==apLeaf ){
174328 rc = SQLITE_NOMEM;
174329 }else{
174330 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
174331 }
174332
174333 if( rc==SQLITE_OK ){
174334 int i;
174335 Fts3Expr *p;
174336
174337 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
174338 for(p=pRoot; p->eType==eType; p=p->pLeft){
174339 assert( p->pParent==0 || p->pParent->pLeft==p );
174340 assert( p->pLeft && p->pRight );
174341 }
174342
174343 /* This loop runs once for each leaf in the tree of eType nodes. */
174344 while( 1 ){
174345 int iLvl;
174346 Fts3Expr *pParent = p->pParent; /* Current parent of p */
174347
174348 assert( pParent==0 || pParent->pLeft==p );
174349 p->pParent = 0;
174350 if( pParent ){
174351 pParent->pLeft = 0;
174352 }else{
174353 pRoot = 0;
174354 }
174355 rc = fts3ExprBalance(&p, nMaxDepth-1);
174356 if( rc!=SQLITE_OK ) break;
174357
174358 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
174359 if( apLeaf[iLvl]==0 ){
174360 apLeaf[iLvl] = p;
174361 p = 0;
174362 }else{
174363 assert( pFree );
174364 pFree->pLeft = apLeaf[iLvl];
174365 pFree->pRight = p;
174366 pFree->pLeft->pParent = pFree;
174367 pFree->pRight->pParent = pFree;
174368
174369 p = pFree;
174370 pFree = pFree->pParent;
174371 p->pParent = 0;
174372 apLeaf[iLvl] = 0;
174373 }
174374 }
174375 if( p ){
174376 sqlite3Fts3ExprFree(p);
174377 rc = SQLITE_TOOBIG;
174378 break;
174379 }
174380
174381 /* If that was the last leaf node, break out of the loop */
174382 if( pParent==0 ) break;
174383
174384 /* Set $p to point to the next leaf in the tree of eType nodes */
174385 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
174386
174387 /* Remove pParent from the original tree. */
174388 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
174389 pParent->pRight->pParent = pParent->pParent;
174390 if( pParent->pParent ){
174391 pParent->pParent->pLeft = pParent->pRight;
174392 }else{
174393 assert( pParent==pRoot );
174394 pRoot = pParent->pRight;
174395 }
174396
174397 /* Link pParent into the free node list. It will be used as an
174398 ** internal node of the new tree. */
174399 pParent->pParent = pFree;
174400 pFree = pParent;
174401 }
174402
174403 if( rc==SQLITE_OK ){
174404 p = 0;
174405 for(i=0; i<nMaxDepth; i++){
174406 if( apLeaf[i] ){
174407 if( p==0 ){
174408 p = apLeaf[i];
174409 p->pParent = 0;
174410 }else{
174411 assert( pFree!=0 );
174412 pFree->pRight = p;
174413 pFree->pLeft = apLeaf[i];
174414 pFree->pLeft->pParent = pFree;
174415 pFree->pRight->pParent = pFree;
174416
174417 p = pFree;
174418 pFree = pFree->pParent;
174419 p->pParent = 0;
174420 }
174421 }
174422 }
174423 pRoot = p;
174424 }else{
174425 /* An error occurred. Delete the contents of the apLeaf[] array
174426 ** and pFree list. Everything else is cleaned up by the call to
174427 ** sqlite3Fts3ExprFree(pRoot) below. */
174428 Fts3Expr *pDel;
174429 for(i=0; i<nMaxDepth; i++){
174430 sqlite3Fts3ExprFree(apLeaf[i]);
174431 }
174432 while( (pDel=pFree)!=0 ){
174433 pFree = pDel->pParent;
174434 sqlite3_free(pDel);
174435 }
174436 }
174437
174438 assert( pFree==0 );
174439 sqlite3_free( apLeaf );
174440 }
174441 }else if( eType==FTSQUERY_NOT ){
174442 Fts3Expr *pLeft = pRoot->pLeft;
174443 Fts3Expr *pRight = pRoot->pRight;
174444
174445 pRoot->pLeft = 0;
174446 pRoot->pRight = 0;
174447 pLeft->pParent = 0;
174448 pRight->pParent = 0;
174449
174450 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
174451 if( rc==SQLITE_OK ){
174452 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
174453 }
174454
174455 if( rc!=SQLITE_OK ){
174456 sqlite3Fts3ExprFree(pRight);
174457 sqlite3Fts3ExprFree(pLeft);
174458 }else{
174459 assert( pLeft && pRight );
174460 pRoot->pLeft = pLeft;
174461 pLeft->pParent = pRoot;
174462 pRoot->pRight = pRight;
174463 pRight->pParent = pRoot;
174464 }
174465 }
174466 }
174467
174468 if( rc!=SQLITE_OK ){
174469 sqlite3Fts3ExprFree(pRoot);
174470 pRoot = 0;
174471 }
174472 *pp = pRoot;
174473 return rc;
174474}
174475
174476/*
174477** This function is similar to sqlite3Fts3ExprParse(), with the following
174478** differences:
174479**
174480** 1. It does not do expression rebalancing.
174481** 2. It does not check that the expression does not exceed the
174482** maximum allowable depth.
174483** 3. Even if it fails, *ppExpr may still be set to point to an
174484** expression tree. It should be deleted using sqlite3Fts3ExprFree()
174485** in this case.
174486*/
174487static int fts3ExprParseUnbalanced(
174488 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
174489 int iLangid, /* Language id for tokenizer */
174490 char **azCol, /* Array of column names for fts3 table */
174491 int bFts4, /* True to allow FTS4-only syntax */
174492 int nCol, /* Number of entries in azCol[] */
174493 int iDefaultCol, /* Default column to query */
174494 const char *z, int n, /* Text of MATCH query */
174495 Fts3Expr **ppExpr /* OUT: Parsed query structure */
174496){
174497 int nParsed;
174498 int rc;
174499 ParseContext sParse;
174500
174501 memset(&sParse, 0, sizeof(ParseContext));
174502 sParse.pTokenizer = pTokenizer;
174503 sParse.iLangid = iLangid;
174504 sParse.azCol = (const char **)azCol;
174505 sParse.nCol = nCol;
174506 sParse.iDefaultCol = iDefaultCol;
174507 sParse.bFts4 = bFts4;
174508 if( z==0 ){
174509 *ppExpr = 0;
174510 return SQLITE_OK;
174511 }
174512 if( n<0 ){
174513 n = (int)strlen(z);
174514 }
174515 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
174516 assert( rc==SQLITE_OK || *ppExpr==0 );
174517
174518 /* Check for mismatched parenthesis */
174519 if( rc==SQLITE_OK && sParse.nNest ){
174520 rc = SQLITE_ERROR;
174521 }
174522
174523 return rc;
174524}
174525
174526/*
174527** Parameters z and n contain a pointer to and length of a buffer containing
174528** an fts3 query expression, respectively. This function attempts to parse the
174529** query expression and create a tree of Fts3Expr structures representing the
174530** parsed expression. If successful, *ppExpr is set to point to the head
174531** of the parsed expression tree and SQLITE_OK is returned. If an error
174532** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
174533** error) is returned and *ppExpr is set to 0.
174534**
174535** If parameter n is a negative number, then z is assumed to point to a
174536** nul-terminated string and the length is determined using strlen().
174537**
174538** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
174539** use to normalize query tokens while parsing the expression. The azCol[]
174540** array, which is assumed to contain nCol entries, should contain the names
174541** of each column in the target fts3 table, in order from left to right.
174542** Column names must be nul-terminated strings.
174543**
174544** The iDefaultCol parameter should be passed the index of the table column
174545** that appears on the left-hand-side of the MATCH operator (the default
174546** column to match against for tokens for which a column name is not explicitly
174547** specified as part of the query string), or -1 if tokens may by default
174548** match any table column.
174549*/
174550SQLITE_PRIVATE int sqlite3Fts3ExprParse(
174551 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
174552 int iLangid, /* Language id for tokenizer */
174553 char **azCol, /* Array of column names for fts3 table */
174554 int bFts4, /* True to allow FTS4-only syntax */
174555 int nCol, /* Number of entries in azCol[] */
174556 int iDefaultCol, /* Default column to query */
174557 const char *z, int n, /* Text of MATCH query */
174558 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
174559 char **pzErr /* OUT: Error message (sqlite3_malloc) */
174560){
174561 int rc = fts3ExprParseUnbalanced(
174562 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
174563 );
174564
174565 /* Rebalance the expression. And check that its depth does not exceed
174566 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
174567 if( rc==SQLITE_OK && *ppExpr ){
174568 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
174569 if( rc==SQLITE_OK ){
174570 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
174571 }
174572 }
174573
174574 if( rc!=SQLITE_OK ){
174575 sqlite3Fts3ExprFree(*ppExpr);
174576 *ppExpr = 0;
174577 if( rc==SQLITE_TOOBIG ){
174578 sqlite3Fts3ErrMsg(pzErr,
174579 "FTS expression tree is too large (maximum depth %d)",
174580 SQLITE_FTS3_MAX_EXPR_DEPTH
174581 );
174582 rc = SQLITE_ERROR;
174583 }else if( rc==SQLITE_ERROR ){
174584 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
174585 }
174586 }
174587
174588 return rc;
174589}
174590
174591/*
174592** Free a single node of an expression tree.
174593*/
174594static void fts3FreeExprNode(Fts3Expr *p){
174595 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
174596 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
174597 sqlite3_free(p->aMI);
174598 sqlite3_free(p);
174599}
174600
174601/*
174602** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
174603**
174604** This function would be simpler if it recursively called itself. But
174605** that would mean passing a sufficiently large expression to ExprParse()
174606** could cause a stack overflow.
174607*/
174608SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
174609 Fts3Expr *p;
174610 assert( pDel==0 || pDel->pParent==0 );
174611 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
174612 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
174613 }
174614 while( p ){
174615 Fts3Expr *pParent = p->pParent;
174616 fts3FreeExprNode(p);
174617 if( pParent && p==pParent->pLeft && pParent->pRight ){
174618 p = pParent->pRight;
174619 while( p && (p->pLeft || p->pRight) ){
174620 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
174621 p = (p->pLeft ? p->pLeft : p->pRight);
174622 }
174623 }else{
174624 p = pParent;
174625 }
174626 }
174627}
174628
174629/****************************************************************************
174630*****************************************************************************
174631** Everything after this point is just test code.
174632*/
174633
174634#ifdef SQLITE_TEST
174635
174636/* #include <stdio.h> */
174637
174638/*
174639** Return a pointer to a buffer containing a text representation of the
174640** expression passed as the first argument. The buffer is obtained from
174641** sqlite3_malloc(). It is the responsibility of the caller to use
174642** sqlite3_free() to release the memory. If an OOM condition is encountered,
174643** NULL is returned.
174644**
174645** If the second argument is not NULL, then its contents are prepended to
174646** the returned expression text and then freed using sqlite3_free().
174647*/
174648static char *exprToString(Fts3Expr *pExpr, char *zBuf){
174649 if( pExpr==0 ){
174650 return sqlite3_mprintf("");
174651 }
174652 switch( pExpr->eType ){
174653 case FTSQUERY_PHRASE: {
174654 Fts3Phrase *pPhrase = pExpr->pPhrase;
174655 int i;
174656 zBuf = sqlite3_mprintf(
174657 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
174658 for(i=0; zBuf && i<pPhrase->nToken; i++){
174659 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
174660 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
174661 (pPhrase->aToken[i].isPrefix?"+":"")
174662 );
174663 }
174664 return zBuf;
174665 }
174666
174667 case FTSQUERY_NEAR:
174668 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
174669 break;
174670 case FTSQUERY_NOT:
174671 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
174672 break;
174673 case FTSQUERY_AND:
174674 zBuf = sqlite3_mprintf("%zAND ", zBuf);
174675 break;
174676 case FTSQUERY_OR:
174677 zBuf = sqlite3_mprintf("%zOR ", zBuf);
174678 break;
174679 }
174680
174681 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
174682 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
174683 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
174684
174685 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
174686 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
174687
174688 return zBuf;
174689}
174690
174691/*
174692** This is the implementation of a scalar SQL function used to test the
174693** expression parser. It should be called as follows:
174694**
174695** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
174696**
174697** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
174698** to parse the query expression (see README.tokenizers). The second argument
174699** is the query expression to parse. Each subsequent argument is the name
174700** of a column of the fts3 table that the query expression may refer to.
174701** For example:
174702**
174703** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
174704*/
174705static void fts3ExprTestCommon(
174706 int bRebalance,
174707 sqlite3_context *context,
174708 int argc,
174709 sqlite3_value **argv
174710){
174711 sqlite3_tokenizer *pTokenizer = 0;
174712 int rc;
174713 char **azCol = 0;
174714 const char *zExpr;
174715 int nExpr;
174716 int nCol;
174717 int ii;
174718 Fts3Expr *pExpr;
174719 char *zBuf = 0;
174720 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
174721 const char *zTokenizer = 0;
174722 char *zErr = 0;
174723
174724 if( argc<3 ){
174725 sqlite3_result_error(context,
174726 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
174727 );
174728 return;
174729 }
174730
174731 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
174732 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
174733 if( rc!=SQLITE_OK ){
174734 if( rc==SQLITE_NOMEM ){
174735 sqlite3_result_error_nomem(context);
174736 }else{
174737 sqlite3_result_error(context, zErr, -1);
174738 }
174739 sqlite3_free(zErr);
174740 return;
174741 }
174742
174743 zExpr = (const char *)sqlite3_value_text(argv[1]);
174744 nExpr = sqlite3_value_bytes(argv[1]);
174745 nCol = argc-2;
174746 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
174747 if( !azCol ){
174748 sqlite3_result_error_nomem(context);
174749 goto exprtest_out;
174750 }
174751 for(ii=0; ii<nCol; ii++){
174752 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
174753 }
174754
174755 if( bRebalance ){
174756 char *zDummy = 0;
174757 rc = sqlite3Fts3ExprParse(
174758 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
174759 );
174760 assert( rc==SQLITE_OK || pExpr==0 );
174761 sqlite3_free(zDummy);
174762 }else{
174763 rc = fts3ExprParseUnbalanced(
174764 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
174765 );
174766 }
174767
174768 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
174769 sqlite3Fts3ExprFree(pExpr);
174770 sqlite3_result_error(context, "Error parsing expression", -1);
174771 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
174772 sqlite3_result_error_nomem(context);
174773 }else{
174774 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
174775 sqlite3_free(zBuf);
174776 }
174777
174778 sqlite3Fts3ExprFree(pExpr);
174779
174780exprtest_out:
174781 if( pTokenizer ){
174782 rc = pTokenizer->pModule->xDestroy(pTokenizer);
174783 }
174784 sqlite3_free(azCol);
174785}
174786
174787static void fts3ExprTest(
174788 sqlite3_context *context,
174789 int argc,
174790 sqlite3_value **argv
174791){
174792 fts3ExprTestCommon(0, context, argc, argv);
174793}
174794static void fts3ExprTestRebalance(
174795 sqlite3_context *context,
174796 int argc,
174797 sqlite3_value **argv
174798){
174799 fts3ExprTestCommon(1, context, argc, argv);
174800}
174801
174802/*
174803** Register the query expression parser test function fts3_exprtest()
174804** with database connection db.
174805*/
174806SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
174807 int rc = sqlite3_create_function(
174808 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
174809 );
174810 if( rc==SQLITE_OK ){
174811 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
174812 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
174813 );
174814 }
174815 return rc;
174816}
174817
174818#endif
174819#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
174820
174821/************** End of fts3_expr.c *******************************************/
174822/************** Begin file fts3_hash.c ***************************************/
174823/*
174824** 2001 September 22
174825**
174826** The author disclaims copyright to this source code. In place of
174827** a legal notice, here is a blessing:
174828**
174829** May you do good and not evil.
174830** May you find forgiveness for yourself and forgive others.
174831** May you share freely, never taking more than you give.
174832**
174833*************************************************************************
174834** This is the implementation of generic hash-tables used in SQLite.
174835** We've modified it slightly to serve as a standalone hash table
174836** implementation for the full-text indexing module.
174837*/
174838
174839/*
174840** The code in this file is only compiled if:
174841**
174842** * The FTS3 module is being built as an extension
174843** (in which case SQLITE_CORE is not defined), or
174844**
174845** * The FTS3 module is being built into the core of
174846** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
174847*/
174848/* #include "fts3Int.h" */
174849#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
174850
174851/* #include <assert.h> */
174852/* #include <stdlib.h> */
174853/* #include <string.h> */
174854
174855/* #include "fts3_hash.h" */
174856
174857/*
174858** Malloc and Free functions
174859*/
174860static void *fts3HashMalloc(sqlite3_int64 n){
174861 void *p = sqlite3_malloc64(n);
174862 if( p ){
174863 memset(p, 0, n);
174864 }
174865 return p;
174866}
174867static void fts3HashFree(void *p){
174868 sqlite3_free(p);
174869}
174870
174871/* Turn bulk memory into a hash table object by initializing the
174872** fields of the Hash structure.
174873**
174874** "pNew" is a pointer to the hash table that is to be initialized.
174875** keyClass is one of the constants
174876** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
174877** determines what kind of key the hash table will use. "copyKey" is
174878** true if the hash table should make its own private copy of keys and
174879** false if it should just use the supplied pointer.
174880*/
174881SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
174882 assert( pNew!=0 );
174883 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
174884 pNew->keyClass = keyClass;
174885 pNew->copyKey = copyKey;
174886 pNew->first = 0;
174887 pNew->count = 0;
174888 pNew->htsize = 0;
174889 pNew->ht = 0;
174890}
174891
174892/* Remove all entries from a hash table. Reclaim all memory.
174893** Call this routine to delete a hash table or to reset a hash table
174894** to the empty state.
174895*/
174896SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
174897 Fts3HashElem *elem; /* For looping over all elements of the table */
174898
174899 assert( pH!=0 );
174900 elem = pH->first;
174901 pH->first = 0;
174902 fts3HashFree(pH->ht);
174903 pH->ht = 0;
174904 pH->htsize = 0;
174905 while( elem ){
174906 Fts3HashElem *next_elem = elem->next;
174907 if( pH->copyKey && elem->pKey ){
174908 fts3HashFree(elem->pKey);
174909 }
174910 fts3HashFree(elem);
174911 elem = next_elem;
174912 }
174913 pH->count = 0;
174914}
174915
174916/*
174917** Hash and comparison functions when the mode is FTS3_HASH_STRING
174918*/
174919static int fts3StrHash(const void *pKey, int nKey){
174920 const char *z = (const char *)pKey;
174921 unsigned h = 0;
174922 if( nKey<=0 ) nKey = (int) strlen(z);
174923 while( nKey > 0 ){
174924 h = (h<<3) ^ h ^ *z++;
174925 nKey--;
174926 }
174927 return (int)(h & 0x7fffffff);
174928}
174929static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
174930 if( n1!=n2 ) return 1;
174931 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
174932}
174933
174934/*
174935** Hash and comparison functions when the mode is FTS3_HASH_BINARY
174936*/
174937static int fts3BinHash(const void *pKey, int nKey){
174938 int h = 0;
174939 const char *z = (const char *)pKey;
174940 while( nKey-- > 0 ){
174941 h = (h<<3) ^ h ^ *(z++);
174942 }
174943 return h & 0x7fffffff;
174944}
174945static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
174946 if( n1!=n2 ) return 1;
174947 return memcmp(pKey1,pKey2,n1);
174948}
174949
174950/*
174951** Return a pointer to the appropriate hash function given the key class.
174952**
174953** The C syntax in this function definition may be unfamilar to some
174954** programmers, so we provide the following additional explanation:
174955**
174956** The name of the function is "ftsHashFunction". The function takes a
174957** single parameter "keyClass". The return value of ftsHashFunction()
174958** is a pointer to another function. Specifically, the return value
174959** of ftsHashFunction() is a pointer to a function that takes two parameters
174960** with types "const void*" and "int" and returns an "int".
174961*/
174962static int (*ftsHashFunction(int keyClass))(const void*,int){
174963 if( keyClass==FTS3_HASH_STRING ){
174964 return &fts3StrHash;
174965 }else{
174966 assert( keyClass==FTS3_HASH_BINARY );
174967 return &fts3BinHash;
174968 }
174969}
174970
174971/*
174972** Return a pointer to the appropriate hash function given the key class.
174973**
174974** For help in interpreted the obscure C code in the function definition,
174975** see the header comment on the previous function.
174976*/
174977static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
174978 if( keyClass==FTS3_HASH_STRING ){
174979 return &fts3StrCompare;
174980 }else{
174981 assert( keyClass==FTS3_HASH_BINARY );
174982 return &fts3BinCompare;
174983 }
174984}
174985
174986/* Link an element into the hash table
174987*/
174988static void fts3HashInsertElement(
174989 Fts3Hash *pH, /* The complete hash table */
174990 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
174991 Fts3HashElem *pNew /* The element to be inserted */
174992){
174993 Fts3HashElem *pHead; /* First element already in pEntry */
174994 pHead = pEntry->chain;
174995 if( pHead ){
174996 pNew->next = pHead;
174997 pNew->prev = pHead->prev;
174998 if( pHead->prev ){ pHead->prev->next = pNew; }
174999 else { pH->first = pNew; }
175000 pHead->prev = pNew;
175001 }else{
175002 pNew->next = pH->first;
175003 if( pH->first ){ pH->first->prev = pNew; }
175004 pNew->prev = 0;
175005 pH->first = pNew;
175006 }
175007 pEntry->count++;
175008 pEntry->chain = pNew;
175009}
175010
175011
175012/* Resize the hash table so that it cantains "new_size" buckets.
175013** "new_size" must be a power of 2. The hash table might fail
175014** to resize if sqliteMalloc() fails.
175015**
175016** Return non-zero if a memory allocation error occurs.
175017*/
175018static int fts3Rehash(Fts3Hash *pH, int new_size){
175019 struct _fts3ht *new_ht; /* The new hash table */
175020 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
175021 int (*xHash)(const void*,int); /* The hash function */
175022
175023 assert( (new_size & (new_size-1))==0 );
175024 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
175025 if( new_ht==0 ) return 1;
175026 fts3HashFree(pH->ht);
175027 pH->ht = new_ht;
175028 pH->htsize = new_size;
175029 xHash = ftsHashFunction(pH->keyClass);
175030 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
175031 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
175032 next_elem = elem->next;
175033 fts3HashInsertElement(pH, &new_ht[h], elem);
175034 }
175035 return 0;
175036}
175037
175038/* This function (for internal use only) locates an element in an
175039** hash table that matches the given key. The hash for this key has
175040** already been computed and is passed as the 4th parameter.
175041*/
175042static Fts3HashElem *fts3FindElementByHash(
175043 const Fts3Hash *pH, /* The pH to be searched */
175044 const void *pKey, /* The key we are searching for */
175045 int nKey,
175046 int h /* The hash for this key. */
175047){
175048 Fts3HashElem *elem; /* Used to loop thru the element list */
175049 int count; /* Number of elements left to test */
175050 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
175051
175052 if( pH->ht ){
175053 struct _fts3ht *pEntry = &pH->ht[h];
175054 elem = pEntry->chain;
175055 count = pEntry->count;
175056 xCompare = ftsCompareFunction(pH->keyClass);
175057 while( count-- && elem ){
175058 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
175059 return elem;
175060 }
175061 elem = elem->next;
175062 }
175063 }
175064 return 0;
175065}
175066
175067/* Remove a single entry from the hash table given a pointer to that
175068** element and a hash on the element's key.
175069*/
175070static void fts3RemoveElementByHash(
175071 Fts3Hash *pH, /* The pH containing "elem" */
175072 Fts3HashElem* elem, /* The element to be removed from the pH */
175073 int h /* Hash value for the element */
175074){
175075 struct _fts3ht *pEntry;
175076 if( elem->prev ){
175077 elem->prev->next = elem->next;
175078 }else{
175079 pH->first = elem->next;
175080 }
175081 if( elem->next ){
175082 elem->next->prev = elem->prev;
175083 }
175084 pEntry = &pH->ht[h];
175085 if( pEntry->chain==elem ){
175086 pEntry->chain = elem->next;
175087 }
175088 pEntry->count--;
175089 if( pEntry->count<=0 ){
175090 pEntry->chain = 0;
175091 }
175092 if( pH->copyKey && elem->pKey ){
175093 fts3HashFree(elem->pKey);
175094 }
175095 fts3HashFree( elem );
175096 pH->count--;
175097 if( pH->count<=0 ){
175098 assert( pH->first==0 );
175099 assert( pH->count==0 );
175100 fts3HashClear(pH);
175101 }
175102}
175103
175104SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
175105 const Fts3Hash *pH,
175106 const void *pKey,
175107 int nKey
175108){
175109 int h; /* A hash on key */
175110 int (*xHash)(const void*,int); /* The hash function */
175111
175112 if( pH==0 || pH->ht==0 ) return 0;
175113 xHash = ftsHashFunction(pH->keyClass);
175114 assert( xHash!=0 );
175115 h = (*xHash)(pKey,nKey);
175116 assert( (pH->htsize & (pH->htsize-1))==0 );
175117 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
175118}
175119
175120/*
175121** Attempt to locate an element of the hash table pH with a key
175122** that matches pKey,nKey. Return the data for this element if it is
175123** found, or NULL if there is no match.
175124*/
175125SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
175126 Fts3HashElem *pElem; /* The element that matches key (if any) */
175127
175128 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
175129 return pElem ? pElem->data : 0;
175130}
175131
175132/* Insert an element into the hash table pH. The key is pKey,nKey
175133** and the data is "data".
175134**
175135** If no element exists with a matching key, then a new
175136** element is created. A copy of the key is made if the copyKey
175137** flag is set. NULL is returned.
175138**
175139** If another element already exists with the same key, then the
175140** new data replaces the old data and the old data is returned.
175141** The key is not copied in this instance. If a malloc fails, then
175142** the new data is returned and the hash table is unchanged.
175143**
175144** If the "data" parameter to this function is NULL, then the
175145** element corresponding to "key" is removed from the hash table.
175146*/
175147SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
175148 Fts3Hash *pH, /* The hash table to insert into */
175149 const void *pKey, /* The key */
175150 int nKey, /* Number of bytes in the key */
175151 void *data /* The data */
175152){
175153 int hraw; /* Raw hash value of the key */
175154 int h; /* the hash of the key modulo hash table size */
175155 Fts3HashElem *elem; /* Used to loop thru the element list */
175156 Fts3HashElem *new_elem; /* New element added to the pH */
175157 int (*xHash)(const void*,int); /* The hash function */
175158
175159 assert( pH!=0 );
175160 xHash = ftsHashFunction(pH->keyClass);
175161 assert( xHash!=0 );
175162 hraw = (*xHash)(pKey, nKey);
175163 assert( (pH->htsize & (pH->htsize-1))==0 );
175164 h = hraw & (pH->htsize-1);
175165 elem = fts3FindElementByHash(pH,pKey,nKey,h);
175166 if( elem ){
175167 void *old_data = elem->data;
175168 if( data==0 ){
175169 fts3RemoveElementByHash(pH,elem,h);
175170 }else{
175171 elem->data = data;
175172 }
175173 return old_data;
175174 }
175175 if( data==0 ) return 0;
175176 if( (pH->htsize==0 && fts3Rehash(pH,8))
175177 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
175178 ){
175179 pH->count = 0;
175180 return data;
175181 }
175182 assert( pH->htsize>0 );
175183 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
175184 if( new_elem==0 ) return data;
175185 if( pH->copyKey && pKey!=0 ){
175186 new_elem->pKey = fts3HashMalloc( nKey );
175187 if( new_elem->pKey==0 ){
175188 fts3HashFree(new_elem);
175189 return data;
175190 }
175191 memcpy((void*)new_elem->pKey, pKey, nKey);
175192 }else{
175193 new_elem->pKey = (void*)pKey;
175194 }
175195 new_elem->nKey = nKey;
175196 pH->count++;
175197 assert( pH->htsize>0 );
175198 assert( (pH->htsize & (pH->htsize-1))==0 );
175199 h = hraw & (pH->htsize-1);
175200 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
175201 new_elem->data = data;
175202 return 0;
175203}
175204
175205#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175206
175207/************** End of fts3_hash.c *******************************************/
175208/************** Begin file fts3_porter.c *************************************/
175209/*
175210** 2006 September 30
175211**
175212** The author disclaims copyright to this source code. In place of
175213** a legal notice, here is a blessing:
175214**
175215** May you do good and not evil.
175216** May you find forgiveness for yourself and forgive others.
175217** May you share freely, never taking more than you give.
175218**
175219*************************************************************************
175220** Implementation of the full-text-search tokenizer that implements
175221** a Porter stemmer.
175222*/
175223
175224/*
175225** The code in this file is only compiled if:
175226**
175227** * The FTS3 module is being built as an extension
175228** (in which case SQLITE_CORE is not defined), or
175229**
175230** * The FTS3 module is being built into the core of
175231** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
175232*/
175233/* #include "fts3Int.h" */
175234#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175235
175236/* #include <assert.h> */
175237/* #include <stdlib.h> */
175238/* #include <stdio.h> */
175239/* #include <string.h> */
175240
175241/* #include "fts3_tokenizer.h" */
175242
175243/*
175244** Class derived from sqlite3_tokenizer
175245*/
175246typedef struct porter_tokenizer {
175247 sqlite3_tokenizer base; /* Base class */
175248} porter_tokenizer;
175249
175250/*
175251** Class derived from sqlite3_tokenizer_cursor
175252*/
175253typedef struct porter_tokenizer_cursor {
175254 sqlite3_tokenizer_cursor base;
175255 const char *zInput; /* input we are tokenizing */
175256 int nInput; /* size of the input */
175257 int iOffset; /* current position in zInput */
175258 int iToken; /* index of next token to be returned */
175259 char *zToken; /* storage for current token */
175260 int nAllocated; /* space allocated to zToken buffer */
175261} porter_tokenizer_cursor;
175262
175263
175264/*
175265** Create a new tokenizer instance.
175266*/
175267static int porterCreate(
175268 int argc, const char * const *argv,
175269 sqlite3_tokenizer **ppTokenizer
175270){
175271 porter_tokenizer *t;
175272
175273 UNUSED_PARAMETER(argc);
175274 UNUSED_PARAMETER(argv);
175275
175276 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
175277 if( t==NULL ) return SQLITE_NOMEM;
175278 memset(t, 0, sizeof(*t));
175279 *ppTokenizer = &t->base;
175280 return SQLITE_OK;
175281}
175282
175283/*
175284** Destroy a tokenizer
175285*/
175286static int porterDestroy(sqlite3_tokenizer *pTokenizer){
175287 sqlite3_free(pTokenizer);
175288 return SQLITE_OK;
175289}
175290
175291/*
175292** Prepare to begin tokenizing a particular string. The input
175293** string to be tokenized is zInput[0..nInput-1]. A cursor
175294** used to incrementally tokenize this string is returned in
175295** *ppCursor.
175296*/
175297static int porterOpen(
175298 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
175299 const char *zInput, int nInput, /* String to be tokenized */
175300 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
175301){
175302 porter_tokenizer_cursor *c;
175303
175304 UNUSED_PARAMETER(pTokenizer);
175305
175306 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
175307 if( c==NULL ) return SQLITE_NOMEM;
175308
175309 c->zInput = zInput;
175310 if( zInput==0 ){
175311 c->nInput = 0;
175312 }else if( nInput<0 ){
175313 c->nInput = (int)strlen(zInput);
175314 }else{
175315 c->nInput = nInput;
175316 }
175317 c->iOffset = 0; /* start tokenizing at the beginning */
175318 c->iToken = 0;
175319 c->zToken = NULL; /* no space allocated, yet. */
175320 c->nAllocated = 0;
175321
175322 *ppCursor = &c->base;
175323 return SQLITE_OK;
175324}
175325
175326/*
175327** Close a tokenization cursor previously opened by a call to
175328** porterOpen() above.
175329*/
175330static int porterClose(sqlite3_tokenizer_cursor *pCursor){
175331 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
175332 sqlite3_free(c->zToken);
175333 sqlite3_free(c);
175334 return SQLITE_OK;
175335}
175336/*
175337** Vowel or consonant
175338*/
175339static const char cType[] = {
175340 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
175341 1, 1, 1, 2, 1
175342};
175343
175344/*
175345** isConsonant() and isVowel() determine if their first character in
175346** the string they point to is a consonant or a vowel, according
175347** to Porter ruls.
175348**
175349** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
175350** 'Y' is a consonant unless it follows another consonant,
175351** in which case it is a vowel.
175352**
175353** In these routine, the letters are in reverse order. So the 'y' rule
175354** is that 'y' is a consonant unless it is followed by another
175355** consonent.
175356*/
175357static int isVowel(const char*);
175358static int isConsonant(const char *z){
175359 int j;
175360 char x = *z;
175361 if( x==0 ) return 0;
175362 assert( x>='a' && x<='z' );
175363 j = cType[x-'a'];
175364 if( j<2 ) return j;
175365 return z[1]==0 || isVowel(z + 1);
175366}
175367static int isVowel(const char *z){
175368 int j;
175369 char x = *z;
175370 if( x==0 ) return 0;
175371 assert( x>='a' && x<='z' );
175372 j = cType[x-'a'];
175373 if( j<2 ) return 1-j;
175374 return isConsonant(z + 1);
175375}
175376
175377/*
175378** Let any sequence of one or more vowels be represented by V and let
175379** C be sequence of one or more consonants. Then every word can be
175380** represented as:
175381**
175382** [C] (VC){m} [V]
175383**
175384** In prose: A word is an optional consonant followed by zero or
175385** vowel-consonant pairs followed by an optional vowel. "m" is the
175386** number of vowel consonant pairs. This routine computes the value
175387** of m for the first i bytes of a word.
175388**
175389** Return true if the m-value for z is 1 or more. In other words,
175390** return true if z contains at least one vowel that is followed
175391** by a consonant.
175392**
175393** In this routine z[] is in reverse order. So we are really looking
175394** for an instance of a consonant followed by a vowel.
175395*/
175396static int m_gt_0(const char *z){
175397 while( isVowel(z) ){ z++; }
175398 if( *z==0 ) return 0;
175399 while( isConsonant(z) ){ z++; }
175400 return *z!=0;
175401}
175402
175403/* Like mgt0 above except we are looking for a value of m which is
175404** exactly 1
175405*/
175406static int m_eq_1(const char *z){
175407 while( isVowel(z) ){ z++; }
175408 if( *z==0 ) return 0;
175409 while( isConsonant(z) ){ z++; }
175410 if( *z==0 ) return 0;
175411 while( isVowel(z) ){ z++; }
175412 if( *z==0 ) return 1;
175413 while( isConsonant(z) ){ z++; }
175414 return *z==0;
175415}
175416
175417/* Like mgt0 above except we are looking for a value of m>1 instead
175418** or m>0
175419*/
175420static int m_gt_1(const char *z){
175421 while( isVowel(z) ){ z++; }
175422 if( *z==0 ) return 0;
175423 while( isConsonant(z) ){ z++; }
175424 if( *z==0 ) return 0;
175425 while( isVowel(z) ){ z++; }
175426 if( *z==0 ) return 0;
175427 while( isConsonant(z) ){ z++; }
175428 return *z!=0;
175429}
175430
175431/*
175432** Return TRUE if there is a vowel anywhere within z[0..n-1]
175433*/
175434static int hasVowel(const char *z){
175435 while( isConsonant(z) ){ z++; }
175436 return *z!=0;
175437}
175438
175439/*
175440** Return TRUE if the word ends in a double consonant.
175441**
175442** The text is reversed here. So we are really looking at
175443** the first two characters of z[].
175444*/
175445static int doubleConsonant(const char *z){
175446 return isConsonant(z) && z[0]==z[1];
175447}
175448
175449/*
175450** Return TRUE if the word ends with three letters which
175451** are consonant-vowel-consonent and where the final consonant
175452** is not 'w', 'x', or 'y'.
175453**
175454** The word is reversed here. So we are really checking the
175455** first three letters and the first one cannot be in [wxy].
175456*/
175457static int star_oh(const char *z){
175458 return
175459 isConsonant(z) &&
175460 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
175461 isVowel(z+1) &&
175462 isConsonant(z+2);
175463}
175464
175465/*
175466** If the word ends with zFrom and xCond() is true for the stem
175467** of the word that preceeds the zFrom ending, then change the
175468** ending to zTo.
175469**
175470** The input word *pz and zFrom are both in reverse order. zTo
175471** is in normal order.
175472**
175473** Return TRUE if zFrom matches. Return FALSE if zFrom does not
175474** match. Not that TRUE is returned even if xCond() fails and
175475** no substitution occurs.
175476*/
175477static int stem(
175478 char **pz, /* The word being stemmed (Reversed) */
175479 const char *zFrom, /* If the ending matches this... (Reversed) */
175480 const char *zTo, /* ... change the ending to this (not reversed) */
175481 int (*xCond)(const char*) /* Condition that must be true */
175482){
175483 char *z = *pz;
175484 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
175485 if( *zFrom!=0 ) return 0;
175486 if( xCond && !xCond(z) ) return 1;
175487 while( *zTo ){
175488 *(--z) = *(zTo++);
175489 }
175490 *pz = z;
175491 return 1;
175492}
175493
175494/*
175495** This is the fallback stemmer used when the porter stemmer is
175496** inappropriate. The input word is copied into the output with
175497** US-ASCII case folding. If the input word is too long (more
175498** than 20 bytes if it contains no digits or more than 6 bytes if
175499** it contains digits) then word is truncated to 20 or 6 bytes
175500** by taking 10 or 3 bytes from the beginning and end.
175501*/
175502static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
175503 int i, mx, j;
175504 int hasDigit = 0;
175505 for(i=0; i<nIn; i++){
175506 char c = zIn[i];
175507 if( c>='A' && c<='Z' ){
175508 zOut[i] = c - 'A' + 'a';
175509 }else{
175510 if( c>='0' && c<='9' ) hasDigit = 1;
175511 zOut[i] = c;
175512 }
175513 }
175514 mx = hasDigit ? 3 : 10;
175515 if( nIn>mx*2 ){
175516 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
175517 zOut[j] = zOut[i];
175518 }
175519 i = j;
175520 }
175521 zOut[i] = 0;
175522 *pnOut = i;
175523}
175524
175525
175526/*
175527** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
175528** zOut is at least big enough to hold nIn bytes. Write the actual
175529** size of the output word (exclusive of the '\0' terminator) into *pnOut.
175530**
175531** Any upper-case characters in the US-ASCII character set ([A-Z])
175532** are converted to lower case. Upper-case UTF characters are
175533** unchanged.
175534**
175535** Words that are longer than about 20 bytes are stemmed by retaining
175536** a few bytes from the beginning and the end of the word. If the
175537** word contains digits, 3 bytes are taken from the beginning and
175538** 3 bytes from the end. For long words without digits, 10 bytes
175539** are taken from each end. US-ASCII case folding still applies.
175540**
175541** If the input word contains not digits but does characters not
175542** in [a-zA-Z] then no stemming is attempted and this routine just
175543** copies the input into the input into the output with US-ASCII
175544** case folding.
175545**
175546** Stemming never increases the length of the word. So there is
175547** no chance of overflowing the zOut buffer.
175548*/
175549static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
175550 int i, j;
175551 char zReverse[28];
175552 char *z, *z2;
175553 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
175554 /* The word is too big or too small for the porter stemmer.
175555 ** Fallback to the copy stemmer */
175556 copy_stemmer(zIn, nIn, zOut, pnOut);
175557 return;
175558 }
175559 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
175560 char c = zIn[i];
175561 if( c>='A' && c<='Z' ){
175562 zReverse[j] = c + 'a' - 'A';
175563 }else if( c>='a' && c<='z' ){
175564 zReverse[j] = c;
175565 }else{
175566 /* The use of a character not in [a-zA-Z] means that we fallback
175567 ** to the copy stemmer */
175568 copy_stemmer(zIn, nIn, zOut, pnOut);
175569 return;
175570 }
175571 }
175572 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
175573 z = &zReverse[j+1];
175574
175575
175576 /* Step 1a */
175577 if( z[0]=='s' ){
175578 if(
175579 !stem(&z, "sess", "ss", 0) &&
175580 !stem(&z, "sei", "i", 0) &&
175581 !stem(&z, "ss", "ss", 0)
175582 ){
175583 z++;
175584 }
175585 }
175586
175587 /* Step 1b */
175588 z2 = z;
175589 if( stem(&z, "dee", "ee", m_gt_0) ){
175590 /* Do nothing. The work was all in the test */
175591 }else if(
175592 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
175593 && z!=z2
175594 ){
175595 if( stem(&z, "ta", "ate", 0) ||
175596 stem(&z, "lb", "ble", 0) ||
175597 stem(&z, "zi", "ize", 0) ){
175598 /* Do nothing. The work was all in the test */
175599 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
175600 z++;
175601 }else if( m_eq_1(z) && star_oh(z) ){
175602 *(--z) = 'e';
175603 }
175604 }
175605
175606 /* Step 1c */
175607 if( z[0]=='y' && hasVowel(z+1) ){
175608 z[0] = 'i';
175609 }
175610
175611 /* Step 2 */
175612 switch( z[1] ){
175613 case 'a':
175614 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
175615 stem(&z, "lanoit", "tion", m_gt_0);
175616 }
175617 break;
175618 case 'c':
175619 if( !stem(&z, "icne", "ence", m_gt_0) ){
175620 stem(&z, "icna", "ance", m_gt_0);
175621 }
175622 break;
175623 case 'e':
175624 stem(&z, "rezi", "ize", m_gt_0);
175625 break;
175626 case 'g':
175627 stem(&z, "igol", "log", m_gt_0);
175628 break;
175629 case 'l':
175630 if( !stem(&z, "ilb", "ble", m_gt_0)
175631 && !stem(&z, "illa", "al", m_gt_0)
175632 && !stem(&z, "iltne", "ent", m_gt_0)
175633 && !stem(&z, "ile", "e", m_gt_0)
175634 ){
175635 stem(&z, "ilsuo", "ous", m_gt_0);
175636 }
175637 break;
175638 case 'o':
175639 if( !stem(&z, "noitazi", "ize", m_gt_0)
175640 && !stem(&z, "noita", "ate", m_gt_0)
175641 ){
175642 stem(&z, "rota", "ate", m_gt_0);
175643 }
175644 break;
175645 case 's':
175646 if( !stem(&z, "msila", "al", m_gt_0)
175647 && !stem(&z, "ssenevi", "ive", m_gt_0)
175648 && !stem(&z, "ssenluf", "ful", m_gt_0)
175649 ){
175650 stem(&z, "ssensuo", "ous", m_gt_0);
175651 }
175652 break;
175653 case 't':
175654 if( !stem(&z, "itila", "al", m_gt_0)
175655 && !stem(&z, "itivi", "ive", m_gt_0)
175656 ){
175657 stem(&z, "itilib", "ble", m_gt_0);
175658 }
175659 break;
175660 }
175661
175662 /* Step 3 */
175663 switch( z[0] ){
175664 case 'e':
175665 if( !stem(&z, "etaci", "ic", m_gt_0)
175666 && !stem(&z, "evita", "", m_gt_0)
175667 ){
175668 stem(&z, "ezila", "al", m_gt_0);
175669 }
175670 break;
175671 case 'i':
175672 stem(&z, "itici", "ic", m_gt_0);
175673 break;
175674 case 'l':
175675 if( !stem(&z, "laci", "ic", m_gt_0) ){
175676 stem(&z, "luf", "", m_gt_0);
175677 }
175678 break;
175679 case 's':
175680 stem(&z, "ssen", "", m_gt_0);
175681 break;
175682 }
175683
175684 /* Step 4 */
175685 switch( z[1] ){
175686 case 'a':
175687 if( z[0]=='l' && m_gt_1(z+2) ){
175688 z += 2;
175689 }
175690 break;
175691 case 'c':
175692 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
175693 z += 4;
175694 }
175695 break;
175696 case 'e':
175697 if( z[0]=='r' && m_gt_1(z+2) ){
175698 z += 2;
175699 }
175700 break;
175701 case 'i':
175702 if( z[0]=='c' && m_gt_1(z+2) ){
175703 z += 2;
175704 }
175705 break;
175706 case 'l':
175707 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
175708 z += 4;
175709 }
175710 break;
175711 case 'n':
175712 if( z[0]=='t' ){
175713 if( z[2]=='a' ){
175714 if( m_gt_1(z+3) ){
175715 z += 3;
175716 }
175717 }else if( z[2]=='e' ){
175718 if( !stem(&z, "tneme", "", m_gt_1)
175719 && !stem(&z, "tnem", "", m_gt_1)
175720 ){
175721 stem(&z, "tne", "", m_gt_1);
175722 }
175723 }
175724 }
175725 break;
175726 case 'o':
175727 if( z[0]=='u' ){
175728 if( m_gt_1(z+2) ){
175729 z += 2;
175730 }
175731 }else if( z[3]=='s' || z[3]=='t' ){
175732 stem(&z, "noi", "", m_gt_1);
175733 }
175734 break;
175735 case 's':
175736 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
175737 z += 3;
175738 }
175739 break;
175740 case 't':
175741 if( !stem(&z, "eta", "", m_gt_1) ){
175742 stem(&z, "iti", "", m_gt_1);
175743 }
175744 break;
175745 case 'u':
175746 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
175747 z += 3;
175748 }
175749 break;
175750 case 'v':
175751 case 'z':
175752 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
175753 z += 3;
175754 }
175755 break;
175756 }
175757
175758 /* Step 5a */
175759 if( z[0]=='e' ){
175760 if( m_gt_1(z+1) ){
175761 z++;
175762 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
175763 z++;
175764 }
175765 }
175766
175767 /* Step 5b */
175768 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
175769 z++;
175770 }
175771
175772 /* z[] is now the stemmed word in reverse order. Flip it back
175773 ** around into forward order and return.
175774 */
175775 *pnOut = i = (int)strlen(z);
175776 zOut[i] = 0;
175777 while( *z ){
175778 zOut[--i] = *(z++);
175779 }
175780}
175781
175782/*
175783** Characters that can be part of a token. We assume any character
175784** whose value is greater than 0x80 (any UTF character) can be
175785** part of a token. In other words, delimiters all must have
175786** values of 0x7f or lower.
175787*/
175788static const char porterIdChar[] = {
175789/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
175790 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
175791 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
175792 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
175793 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
175794 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
175795};
175796#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
175797
175798/*
175799** Extract the next token from a tokenization cursor. The cursor must
175800** have been opened by a prior call to porterOpen().
175801*/
175802static int porterNext(
175803 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
175804 const char **pzToken, /* OUT: *pzToken is the token text */
175805 int *pnBytes, /* OUT: Number of bytes in token */
175806 int *piStartOffset, /* OUT: Starting offset of token */
175807 int *piEndOffset, /* OUT: Ending offset of token */
175808 int *piPosition /* OUT: Position integer of token */
175809){
175810 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
175811 const char *z = c->zInput;
175812
175813 while( c->iOffset<c->nInput ){
175814 int iStartOffset, ch;
175815
175816 /* Scan past delimiter characters */
175817 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
175818 c->iOffset++;
175819 }
175820
175821 /* Count non-delimiter characters. */
175822 iStartOffset = c->iOffset;
175823 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
175824 c->iOffset++;
175825 }
175826
175827 if( c->iOffset>iStartOffset ){
175828 int n = c->iOffset-iStartOffset;
175829 if( n>c->nAllocated ){
175830 char *pNew;
175831 c->nAllocated = n+20;
175832 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
175833 if( !pNew ) return SQLITE_NOMEM;
175834 c->zToken = pNew;
175835 }
175836 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
175837 *pzToken = c->zToken;
175838 *piStartOffset = iStartOffset;
175839 *piEndOffset = c->iOffset;
175840 *piPosition = c->iToken++;
175841 return SQLITE_OK;
175842 }
175843 }
175844 return SQLITE_DONE;
175845}
175846
175847/*
175848** The set of routines that implement the porter-stemmer tokenizer
175849*/
175850static const sqlite3_tokenizer_module porterTokenizerModule = {
175851 0,
175852 porterCreate,
175853 porterDestroy,
175854 porterOpen,
175855 porterClose,
175856 porterNext,
175857 0
175858};
175859
175860/*
175861** Allocate a new porter tokenizer. Return a pointer to the new
175862** tokenizer in *ppModule
175863*/
175864SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
175865 sqlite3_tokenizer_module const**ppModule
175866){
175867 *ppModule = &porterTokenizerModule;
175868}
175869
175870#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175871
175872/************** End of fts3_porter.c *****************************************/
175873/************** Begin file fts3_tokenizer.c **********************************/
175874/*
175875** 2007 June 22
175876**
175877** The author disclaims copyright to this source code. In place of
175878** a legal notice, here is a blessing:
175879**
175880** May you do good and not evil.
175881** May you find forgiveness for yourself and forgive others.
175882** May you share freely, never taking more than you give.
175883**
175884******************************************************************************
175885**
175886** This is part of an SQLite module implementing full-text search.
175887** This particular file implements the generic tokenizer interface.
175888*/
175889
175890/*
175891** The code in this file is only compiled if:
175892**
175893** * The FTS3 module is being built as an extension
175894** (in which case SQLITE_CORE is not defined), or
175895**
175896** * The FTS3 module is being built into the core of
175897** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
175898*/
175899/* #include "fts3Int.h" */
175900#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
175901
175902/* #include <assert.h> */
175903/* #include <string.h> */
175904
175905/*
175906** Return true if the two-argument version of fts3_tokenizer()
175907** has been activated via a prior call to sqlite3_db_config(db,
175908** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
175909*/
175910static int fts3TokenizerEnabled(sqlite3_context *context){
175911 sqlite3 *db = sqlite3_context_db_handle(context);
175912 int isEnabled = 0;
175913 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
175914 return isEnabled;
175915}
175916
175917/*
175918** Implementation of the SQL scalar function for accessing the underlying
175919** hash table. This function may be called as follows:
175920**
175921** SELECT <function-name>(<key-name>);
175922** SELECT <function-name>(<key-name>, <pointer>);
175923**
175924** where <function-name> is the name passed as the second argument
175925** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
175926**
175927** If the <pointer> argument is specified, it must be a blob value
175928** containing a pointer to be stored as the hash data corresponding
175929** to the string <key-name>. If <pointer> is not specified, then
175930** the string <key-name> must already exist in the has table. Otherwise,
175931** an error is returned.
175932**
175933** Whether or not the <pointer> argument is specified, the value returned
175934** is a blob containing the pointer stored as the hash data corresponding
175935** to string <key-name> (after the hash-table is updated, if applicable).
175936*/
175937static void fts3TokenizerFunc(
175938 sqlite3_context *context,
175939 int argc,
175940 sqlite3_value **argv
175941){
175942 Fts3Hash *pHash;
175943 void *pPtr = 0;
175944 const unsigned char *zName;
175945 int nName;
175946
175947 assert( argc==1 || argc==2 );
175948
175949 pHash = (Fts3Hash *)sqlite3_user_data(context);
175950
175951 zName = sqlite3_value_text(argv[0]);
175952 nName = sqlite3_value_bytes(argv[0])+1;
175953
175954 if( argc==2 ){
175955 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
175956 void *pOld;
175957 int n = sqlite3_value_bytes(argv[1]);
175958 if( zName==0 || n!=sizeof(pPtr) ){
175959 sqlite3_result_error(context, "argument type mismatch", -1);
175960 return;
175961 }
175962 pPtr = *(void **)sqlite3_value_blob(argv[1]);
175963 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
175964 if( pOld==pPtr ){
175965 sqlite3_result_error(context, "out of memory", -1);
175966 }
175967 }else{
175968 sqlite3_result_error(context, "fts3tokenize disabled", -1);
175969 return;
175970 }
175971 }else{
175972 if( zName ){
175973 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
175974 }
175975 if( !pPtr ){
175976 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
175977 sqlite3_result_error(context, zErr, -1);
175978 sqlite3_free(zErr);
175979 return;
175980 }
175981 }
175982 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
175983 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
175984 }
175985}
175986
175987SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
175988 static const char isFtsIdChar[] = {
175989 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
175990 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
175991 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
175992 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
175993 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
175994 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
175995 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
175996 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
175997 };
175998 return (c&0x80 || isFtsIdChar[(int)(c)]);
175999}
176000
176001SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
176002 const char *z1;
176003 const char *z2 = 0;
176004
176005 /* Find the start of the next token. */
176006 z1 = zStr;
176007 while( z2==0 ){
176008 char c = *z1;
176009 switch( c ){
176010 case '\0': return 0; /* No more tokens here */
176011 case '\'':
176012 case '"':
176013 case '`': {
176014 z2 = z1;
176015 while( *++z2 && (*z2!=c || *++z2==c) );
176016 break;
176017 }
176018 case '[':
176019 z2 = &z1[1];
176020 while( *z2 && z2[0]!=']' ) z2++;
176021 if( *z2 ) z2++;
176022 break;
176023
176024 default:
176025 if( sqlite3Fts3IsIdChar(*z1) ){
176026 z2 = &z1[1];
176027 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
176028 }else{
176029 z1++;
176030 }
176031 }
176032 }
176033
176034 *pn = (int)(z2-z1);
176035 return z1;
176036}
176037
176038SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
176039 Fts3Hash *pHash, /* Tokenizer hash table */
176040 const char *zArg, /* Tokenizer name */
176041 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
176042 char **pzErr /* OUT: Set to malloced error message */
176043){
176044 int rc;
176045 char *z = (char *)zArg;
176046 int n = 0;
176047 char *zCopy;
176048 char *zEnd; /* Pointer to nul-term of zCopy */
176049 sqlite3_tokenizer_module *m;
176050
176051 zCopy = sqlite3_mprintf("%s", zArg);
176052 if( !zCopy ) return SQLITE_NOMEM;
176053 zEnd = &zCopy[strlen(zCopy)];
176054
176055 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
176056 if( z==0 ){
176057 assert( n==0 );
176058 z = zCopy;
176059 }
176060 z[n] = '\0';
176061 sqlite3Fts3Dequote(z);
176062
176063 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
176064 if( !m ){
176065 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
176066 rc = SQLITE_ERROR;
176067 }else{
176068 char const **aArg = 0;
176069 int iArg = 0;
176070 z = &z[n+1];
176071 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
176072 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
176073 char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
176074 if( !aNew ){
176075 sqlite3_free(zCopy);
176076 sqlite3_free((void *)aArg);
176077 return SQLITE_NOMEM;
176078 }
176079 aArg = aNew;
176080 aArg[iArg++] = z;
176081 z[n] = '\0';
176082 sqlite3Fts3Dequote(z);
176083 z = &z[n+1];
176084 }
176085 rc = m->xCreate(iArg, aArg, ppTok);
176086 assert( rc!=SQLITE_OK || *ppTok );
176087 if( rc!=SQLITE_OK ){
176088 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
176089 }else{
176090 (*ppTok)->pModule = m;
176091 }
176092 sqlite3_free((void *)aArg);
176093 }
176094
176095 sqlite3_free(zCopy);
176096 return rc;
176097}
176098
176099
176100#ifdef SQLITE_TEST
176101
176102#if defined(INCLUDE_SQLITE_TCL_H)
176103# include "sqlite_tcl.h"
176104#else
176105# include "tcl.h"
176106#endif
176107/* #include <string.h> */
176108
176109/*
176110** Implementation of a special SQL scalar function for testing tokenizers
176111** designed to be used in concert with the Tcl testing framework. This
176112** function must be called with two or more arguments:
176113**
176114** SELECT <function-name>(<key-name>, ..., <input-string>);
176115**
176116** where <function-name> is the name passed as the second argument
176117** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
176118** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
176119**
176120** The return value is a string that may be interpreted as a Tcl
176121** list. For each token in the <input-string>, three elements are
176122** added to the returned list. The first is the token position, the
176123** second is the token text (folded, stemmed, etc.) and the third is the
176124** substring of <input-string> associated with the token. For example,
176125** using the built-in "simple" tokenizer:
176126**
176127** SELECT fts_tokenizer_test('simple', 'I don't see how');
176128**
176129** will return the string:
176130**
176131** "{0 i I 1 dont don't 2 see see 3 how how}"
176132**
176133*/
176134static void testFunc(
176135 sqlite3_context *context,
176136 int argc,
176137 sqlite3_value **argv
176138){
176139 Fts3Hash *pHash;
176140 sqlite3_tokenizer_module *p;
176141 sqlite3_tokenizer *pTokenizer = 0;
176142 sqlite3_tokenizer_cursor *pCsr = 0;
176143
176144 const char *zErr = 0;
176145
176146 const char *zName;
176147 int nName;
176148 const char *zInput;
176149 int nInput;
176150
176151 const char *azArg[64];
176152
176153 const char *zToken;
176154 int nToken = 0;
176155 int iStart = 0;
176156 int iEnd = 0;
176157 int iPos = 0;
176158 int i;
176159
176160 Tcl_Obj *pRet;
176161
176162 if( argc<2 ){
176163 sqlite3_result_error(context, "insufficient arguments", -1);
176164 return;
176165 }
176166
176167 nName = sqlite3_value_bytes(argv[0]);
176168 zName = (const char *)sqlite3_value_text(argv[0]);
176169 nInput = sqlite3_value_bytes(argv[argc-1]);
176170 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
176171
176172 pHash = (Fts3Hash *)sqlite3_user_data(context);
176173 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
176174
176175 if( !p ){
176176 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
176177 sqlite3_result_error(context, zErr2, -1);
176178 sqlite3_free(zErr2);
176179 return;
176180 }
176181
176182 pRet = Tcl_NewObj();
176183 Tcl_IncrRefCount(pRet);
176184
176185 for(i=1; i<argc-1; i++){
176186 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
176187 }
176188
176189 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
176190 zErr = "error in xCreate()";
176191 goto finish;
176192 }
176193 pTokenizer->pModule = p;
176194 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
176195 zErr = "error in xOpen()";
176196 goto finish;
176197 }
176198
176199 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
176200 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
176201 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
176202 zToken = &zInput[iStart];
176203 nToken = iEnd-iStart;
176204 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
176205 }
176206
176207 if( SQLITE_OK!=p->xClose(pCsr) ){
176208 zErr = "error in xClose()";
176209 goto finish;
176210 }
176211 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
176212 zErr = "error in xDestroy()";
176213 goto finish;
176214 }
176215
176216finish:
176217 if( zErr ){
176218 sqlite3_result_error(context, zErr, -1);
176219 }else{
176220 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
176221 }
176222 Tcl_DecrRefCount(pRet);
176223}
176224
176225static
176226int registerTokenizer(
176227 sqlite3 *db,
176228 char *zName,
176229 const sqlite3_tokenizer_module *p
176230){
176231 int rc;
176232 sqlite3_stmt *pStmt;
176233 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
176234
176235 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
176236 if( rc!=SQLITE_OK ){
176237 return rc;
176238 }
176239
176240 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
176241 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
176242 sqlite3_step(pStmt);
176243
176244 return sqlite3_finalize(pStmt);
176245}
176246
176247
176248static
176249int queryTokenizer(
176250 sqlite3 *db,
176251 char *zName,
176252 const sqlite3_tokenizer_module **pp
176253){
176254 int rc;
176255 sqlite3_stmt *pStmt;
176256 const char zSql[] = "SELECT fts3_tokenizer(?)";
176257
176258 *pp = 0;
176259 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
176260 if( rc!=SQLITE_OK ){
176261 return rc;
176262 }
176263
176264 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
176265 if( SQLITE_ROW==sqlite3_step(pStmt) ){
176266 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB
176267 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
176268 ){
176269 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
176270 }
176271 }
176272
176273 return sqlite3_finalize(pStmt);
176274}
176275
176276SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
176277
176278/*
176279** Implementation of the scalar function fts3_tokenizer_internal_test().
176280** This function is used for testing only, it is not included in the
176281** build unless SQLITE_TEST is defined.
176282**
176283** The purpose of this is to test that the fts3_tokenizer() function
176284** can be used as designed by the C-code in the queryTokenizer and
176285** registerTokenizer() functions above. These two functions are repeated
176286** in the README.tokenizer file as an example, so it is important to
176287** test them.
176288**
176289** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
176290** function with no arguments. An assert() will fail if a problem is
176291** detected. i.e.:
176292**
176293** SELECT fts3_tokenizer_internal_test();
176294**
176295*/
176296static void intTestFunc(
176297 sqlite3_context *context,
176298 int argc,
176299 sqlite3_value **argv
176300){
176301 int rc;
176302 const sqlite3_tokenizer_module *p1;
176303 const sqlite3_tokenizer_module *p2;
176304 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
176305
176306 UNUSED_PARAMETER(argc);
176307 UNUSED_PARAMETER(argv);
176308
176309 /* Test the query function */
176310 sqlite3Fts3SimpleTokenizerModule(&p1);
176311 rc = queryTokenizer(db, "simple", &p2);
176312 assert( rc==SQLITE_OK );
176313 assert( p1==p2 );
176314 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
176315 assert( rc==SQLITE_ERROR );
176316 assert( p2==0 );
176317 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
176318
176319 /* Test the storage function */
176320 if( fts3TokenizerEnabled(context) ){
176321 rc = registerTokenizer(db, "nosuchtokenizer", p1);
176322 assert( rc==SQLITE_OK );
176323 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
176324 assert( rc==SQLITE_OK );
176325 assert( p2==p1 );
176326 }
176327
176328 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
176329}
176330
176331#endif
176332
176333/*
176334** Set up SQL objects in database db used to access the contents of
176335** the hash table pointed to by argument pHash. The hash table must
176336** been initialized to use string keys, and to take a private copy
176337** of the key when a value is inserted. i.e. by a call similar to:
176338**
176339** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
176340**
176341** This function adds a scalar function (see header comment above
176342** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
176343** defined at compilation time, a temporary virtual table (see header
176344** comment above struct HashTableVtab) to the database schema. Both
176345** provide read/write access to the contents of *pHash.
176346**
176347** The third argument to this function, zName, is used as the name
176348** of both the scalar and, if created, the virtual table.
176349*/
176350SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
176351 sqlite3 *db,
176352 Fts3Hash *pHash,
176353 const char *zName
176354){
176355 int rc = SQLITE_OK;
176356 void *p = (void *)pHash;
176357 const int any = SQLITE_UTF8|SQLITE_DIRECTONLY;
176358
176359#ifdef SQLITE_TEST
176360 char *zTest = 0;
176361 char *zTest2 = 0;
176362 void *pdb = (void *)db;
176363 zTest = sqlite3_mprintf("%s_test", zName);
176364 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
176365 if( !zTest || !zTest2 ){
176366 rc = SQLITE_NOMEM;
176367 }
176368#endif
176369
176370 if( SQLITE_OK==rc ){
176371 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
176372 }
176373 if( SQLITE_OK==rc ){
176374 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
176375 }
176376#ifdef SQLITE_TEST
176377 if( SQLITE_OK==rc ){
176378 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
176379 }
176380 if( SQLITE_OK==rc ){
176381 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
176382 }
176383#endif
176384
176385#ifdef SQLITE_TEST
176386 sqlite3_free(zTest);
176387 sqlite3_free(zTest2);
176388#endif
176389
176390 return rc;
176391}
176392
176393#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
176394
176395/************** End of fts3_tokenizer.c **************************************/
176396/************** Begin file fts3_tokenizer1.c *********************************/
176397/*
176398** 2006 Oct 10
176399**
176400** The author disclaims copyright to this source code. In place of
176401** a legal notice, here is a blessing:
176402**
176403** May you do good and not evil.
176404** May you find forgiveness for yourself and forgive others.
176405** May you share freely, never taking more than you give.
176406**
176407******************************************************************************
176408**
176409** Implementation of the "simple" full-text-search tokenizer.
176410*/
176411
176412/*
176413** The code in this file is only compiled if:
176414**
176415** * The FTS3 module is being built as an extension
176416** (in which case SQLITE_CORE is not defined), or
176417**
176418** * The FTS3 module is being built into the core of
176419** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
176420*/
176421/* #include "fts3Int.h" */
176422#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
176423
176424/* #include <assert.h> */
176425/* #include <stdlib.h> */
176426/* #include <stdio.h> */
176427/* #include <string.h> */
176428
176429/* #include "fts3_tokenizer.h" */
176430
176431typedef struct simple_tokenizer {
176432 sqlite3_tokenizer base;
176433 char delim[128]; /* flag ASCII delimiters */
176434} simple_tokenizer;
176435
176436typedef struct simple_tokenizer_cursor {
176437 sqlite3_tokenizer_cursor base;
176438 const char *pInput; /* input we are tokenizing */
176439 int nBytes; /* size of the input */
176440 int iOffset; /* current position in pInput */
176441 int iToken; /* index of next token to be returned */
176442 char *pToken; /* storage for current token */
176443 int nTokenAllocated; /* space allocated to zToken buffer */
176444} simple_tokenizer_cursor;
176445
176446
176447static int simpleDelim(simple_tokenizer *t, unsigned char c){
176448 return c<0x80 && t->delim[c];
176449}
176450static int fts3_isalnum(int x){
176451 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
176452}
176453
176454/*
176455** Create a new tokenizer instance.
176456*/
176457static int simpleCreate(
176458 int argc, const char * const *argv,
176459 sqlite3_tokenizer **ppTokenizer
176460){
176461 simple_tokenizer *t;
176462
176463 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
176464 if( t==NULL ) return SQLITE_NOMEM;
176465 memset(t, 0, sizeof(*t));
176466
176467 /* TODO(shess) Delimiters need to remain the same from run to run,
176468 ** else we need to reindex. One solution would be a meta-table to
176469 ** track such information in the database, then we'd only want this
176470 ** information on the initial create.
176471 */
176472 if( argc>1 ){
176473 int i, n = (int)strlen(argv[1]);
176474 for(i=0; i<n; i++){
176475 unsigned char ch = argv[1][i];
176476 /* We explicitly don't support UTF-8 delimiters for now. */
176477 if( ch>=0x80 ){
176478 sqlite3_free(t);
176479 return SQLITE_ERROR;
176480 }
176481 t->delim[ch] = 1;
176482 }
176483 } else {
176484 /* Mark non-alphanumeric ASCII characters as delimiters */
176485 int i;
176486 for(i=1; i<0x80; i++){
176487 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
176488 }
176489 }
176490
176491 *ppTokenizer = &t->base;
176492 return SQLITE_OK;
176493}
176494
176495/*
176496** Destroy a tokenizer
176497*/
176498static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
176499 sqlite3_free(pTokenizer);
176500 return SQLITE_OK;
176501}
176502
176503/*
176504** Prepare to begin tokenizing a particular string. The input
176505** string to be tokenized is pInput[0..nBytes-1]. A cursor
176506** used to incrementally tokenize this string is returned in
176507** *ppCursor.
176508*/
176509static int simpleOpen(
176510 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
176511 const char *pInput, int nBytes, /* String to be tokenized */
176512 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
176513){
176514 simple_tokenizer_cursor *c;
176515
176516 UNUSED_PARAMETER(pTokenizer);
176517
176518 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
176519 if( c==NULL ) return SQLITE_NOMEM;
176520
176521 c->pInput = pInput;
176522 if( pInput==0 ){
176523 c->nBytes = 0;
176524 }else if( nBytes<0 ){
176525 c->nBytes = (int)strlen(pInput);
176526 }else{
176527 c->nBytes = nBytes;
176528 }
176529 c->iOffset = 0; /* start tokenizing at the beginning */
176530 c->iToken = 0;
176531 c->pToken = NULL; /* no space allocated, yet. */
176532 c->nTokenAllocated = 0;
176533
176534 *ppCursor = &c->base;
176535 return SQLITE_OK;
176536}
176537
176538/*
176539** Close a tokenization cursor previously opened by a call to
176540** simpleOpen() above.
176541*/
176542static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
176543 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
176544 sqlite3_free(c->pToken);
176545 sqlite3_free(c);
176546 return SQLITE_OK;
176547}
176548
176549/*
176550** Extract the next token from a tokenization cursor. The cursor must
176551** have been opened by a prior call to simpleOpen().
176552*/
176553static int simpleNext(
176554 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
176555 const char **ppToken, /* OUT: *ppToken is the token text */
176556 int *pnBytes, /* OUT: Number of bytes in token */
176557 int *piStartOffset, /* OUT: Starting offset of token */
176558 int *piEndOffset, /* OUT: Ending offset of token */
176559 int *piPosition /* OUT: Position integer of token */
176560){
176561 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
176562 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
176563 unsigned char *p = (unsigned char *)c->pInput;
176564
176565 while( c->iOffset<c->nBytes ){
176566 int iStartOffset;
176567
176568 /* Scan past delimiter characters */
176569 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
176570 c->iOffset++;
176571 }
176572
176573 /* Count non-delimiter characters. */
176574 iStartOffset = c->iOffset;
176575 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
176576 c->iOffset++;
176577 }
176578
176579 if( c->iOffset>iStartOffset ){
176580 int i, n = c->iOffset-iStartOffset;
176581 if( n>c->nTokenAllocated ){
176582 char *pNew;
176583 c->nTokenAllocated = n+20;
176584 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
176585 if( !pNew ) return SQLITE_NOMEM;
176586 c->pToken = pNew;
176587 }
176588 for(i=0; i<n; i++){
176589 /* TODO(shess) This needs expansion to handle UTF-8
176590 ** case-insensitivity.
176591 */
176592 unsigned char ch = p[iStartOffset+i];
176593 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
176594 }
176595 *ppToken = c->pToken;
176596 *pnBytes = n;
176597 *piStartOffset = iStartOffset;
176598 *piEndOffset = c->iOffset;
176599 *piPosition = c->iToken++;
176600
176601 return SQLITE_OK;
176602 }
176603 }
176604 return SQLITE_DONE;
176605}
176606
176607/*
176608** The set of routines that implement the simple tokenizer
176609*/
176610static const sqlite3_tokenizer_module simpleTokenizerModule = {
176611 0,
176612 simpleCreate,
176613 simpleDestroy,
176614 simpleOpen,
176615 simpleClose,
176616 simpleNext,
176617 0,
176618};
176619
176620/*
176621** Allocate a new simple tokenizer. Return a pointer to the new
176622** tokenizer in *ppModule
176623*/
176624SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
176625 sqlite3_tokenizer_module const**ppModule
176626){
176627 *ppModule = &simpleTokenizerModule;
176628}
176629
176630#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
176631
176632/************** End of fts3_tokenizer1.c *************************************/
176633/************** Begin file fts3_tokenize_vtab.c ******************************/
176634/*
176635** 2013 Apr 22
176636**
176637** The author disclaims copyright to this source code. In place of
176638** a legal notice, here is a blessing:
176639**
176640** May you do good and not evil.
176641** May you find forgiveness for yourself and forgive others.
176642** May you share freely, never taking more than you give.
176643**
176644******************************************************************************
176645**
176646** This file contains code for the "fts3tokenize" virtual table module.
176647** An fts3tokenize virtual table is created as follows:
176648**
176649** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
176650** <tokenizer-name>, <arg-1>, ...
176651** );
176652**
176653** The table created has the following schema:
176654**
176655** CREATE TABLE <tbl>(input, token, start, end, position)
176656**
176657** When queried, the query must include a WHERE clause of type:
176658**
176659** input = <string>
176660**
176661** The virtual table module tokenizes this <string>, using the FTS3
176662** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
176663** statement and returns one row for each token in the result. With
176664** fields set as follows:
176665**
176666** input: Always set to a copy of <string>
176667** token: A token from the input.
176668** start: Byte offset of the token within the input <string>.
176669** end: Byte offset of the byte immediately following the end of the
176670** token within the input string.
176671** pos: Token offset of token within input.
176672**
176673*/
176674/* #include "fts3Int.h" */
176675#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
176676
176677/* #include <string.h> */
176678/* #include <assert.h> */
176679
176680typedef struct Fts3tokTable Fts3tokTable;
176681typedef struct Fts3tokCursor Fts3tokCursor;
176682
176683/*
176684** Virtual table structure.
176685*/
176686struct Fts3tokTable {
176687 sqlite3_vtab base; /* Base class used by SQLite core */
176688 const sqlite3_tokenizer_module *pMod;
176689 sqlite3_tokenizer *pTok;
176690};
176691
176692/*
176693** Virtual table cursor structure.
176694*/
176695struct Fts3tokCursor {
176696 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
176697 char *zInput; /* Input string */
176698 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
176699 int iRowid; /* Current 'rowid' value */
176700 const char *zToken; /* Current 'token' value */
176701 int nToken; /* Size of zToken in bytes */
176702 int iStart; /* Current 'start' value */
176703 int iEnd; /* Current 'end' value */
176704 int iPos; /* Current 'pos' value */
176705};
176706
176707/*
176708** Query FTS for the tokenizer implementation named zName.
176709*/
176710static int fts3tokQueryTokenizer(
176711 Fts3Hash *pHash,
176712 const char *zName,
176713 const sqlite3_tokenizer_module **pp,
176714 char **pzErr
176715){
176716 sqlite3_tokenizer_module *p;
176717 int nName = (int)strlen(zName);
176718
176719 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
176720 if( !p ){
176721 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
176722 return SQLITE_ERROR;
176723 }
176724
176725 *pp = p;
176726 return SQLITE_OK;
176727}
176728
176729/*
176730** The second argument, argv[], is an array of pointers to nul-terminated
176731** strings. This function makes a copy of the array and strings into a
176732** single block of memory. It then dequotes any of the strings that appear
176733** to be quoted.
176734**
176735** If successful, output parameter *pazDequote is set to point at the
176736** array of dequoted strings and SQLITE_OK is returned. The caller is
176737** responsible for eventually calling sqlite3_free() to free the array
176738** in this case. Or, if an error occurs, an SQLite error code is returned.
176739** The final value of *pazDequote is undefined in this case.
176740*/
176741static int fts3tokDequoteArray(
176742 int argc, /* Number of elements in argv[] */
176743 const char * const *argv, /* Input array */
176744 char ***pazDequote /* Output array */
176745){
176746 int rc = SQLITE_OK; /* Return code */
176747 if( argc==0 ){
176748 *pazDequote = 0;
176749 }else{
176750 int i;
176751 int nByte = 0;
176752 char **azDequote;
176753
176754 for(i=0; i<argc; i++){
176755 nByte += (int)(strlen(argv[i]) + 1);
176756 }
176757
176758 *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
176759 if( azDequote==0 ){
176760 rc = SQLITE_NOMEM;
176761 }else{
176762 char *pSpace = (char *)&azDequote[argc];
176763 for(i=0; i<argc; i++){
176764 int n = (int)strlen(argv[i]);
176765 azDequote[i] = pSpace;
176766 memcpy(pSpace, argv[i], n+1);
176767 sqlite3Fts3Dequote(pSpace);
176768 pSpace += (n+1);
176769 }
176770 }
176771 }
176772
176773 return rc;
176774}
176775
176776/*
176777** Schema of the tokenizer table.
176778*/
176779#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
176780
176781/*
176782** This function does all the work for both the xConnect and xCreate methods.
176783** These tables have no persistent representation of their own, so xConnect
176784** and xCreate are identical operations.
176785**
176786** argv[0]: module name
176787** argv[1]: database name
176788** argv[2]: table name
176789** argv[3]: first argument (tokenizer name)
176790*/
176791static int fts3tokConnectMethod(
176792 sqlite3 *db, /* Database connection */
176793 void *pHash, /* Hash table of tokenizers */
176794 int argc, /* Number of elements in argv array */
176795 const char * const *argv, /* xCreate/xConnect argument array */
176796 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
176797 char **pzErr /* OUT: sqlite3_malloc'd error message */
176798){
176799 Fts3tokTable *pTab = 0;
176800 const sqlite3_tokenizer_module *pMod = 0;
176801 sqlite3_tokenizer *pTok = 0;
176802 int rc;
176803 char **azDequote = 0;
176804 int nDequote;
176805
176806 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
176807 if( rc!=SQLITE_OK ) return rc;
176808
176809 nDequote = argc-3;
176810 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
176811
176812 if( rc==SQLITE_OK ){
176813 const char *zModule;
176814 if( nDequote<1 ){
176815 zModule = "simple";
176816 }else{
176817 zModule = azDequote[0];
176818 }
176819 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
176820 }
176821
176822 assert( (rc==SQLITE_OK)==(pMod!=0) );
176823 if( rc==SQLITE_OK ){
176824 const char * const *azArg = 0;
176825 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
176826 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
176827 }
176828
176829 if( rc==SQLITE_OK ){
176830 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
176831 if( pTab==0 ){
176832 rc = SQLITE_NOMEM;
176833 }
176834 }
176835
176836 if( rc==SQLITE_OK ){
176837 memset(pTab, 0, sizeof(Fts3tokTable));
176838 pTab->pMod = pMod;
176839 pTab->pTok = pTok;
176840 *ppVtab = &pTab->base;
176841 }else{
176842 if( pTok ){
176843 pMod->xDestroy(pTok);
176844 }
176845 }
176846
176847 sqlite3_free(azDequote);
176848 return rc;
176849}
176850
176851/*
176852** This function does the work for both the xDisconnect and xDestroy methods.
176853** These tables have no persistent representation of their own, so xDisconnect
176854** and xDestroy are identical operations.
176855*/
176856static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
176857 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
176858
176859 pTab->pMod->xDestroy(pTab->pTok);
176860 sqlite3_free(pTab);
176861 return SQLITE_OK;
176862}
176863
176864/*
176865** xBestIndex - Analyze a WHERE and ORDER BY clause.
176866*/
176867static int fts3tokBestIndexMethod(
176868 sqlite3_vtab *pVTab,
176869 sqlite3_index_info *pInfo
176870){
176871 int i;
176872 UNUSED_PARAMETER(pVTab);
176873
176874 for(i=0; i<pInfo->nConstraint; i++){
176875 if( pInfo->aConstraint[i].usable
176876 && pInfo->aConstraint[i].iColumn==0
176877 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
176878 ){
176879 pInfo->idxNum = 1;
176880 pInfo->aConstraintUsage[i].argvIndex = 1;
176881 pInfo->aConstraintUsage[i].omit = 1;
176882 pInfo->estimatedCost = 1;
176883 return SQLITE_OK;
176884 }
176885 }
176886
176887 pInfo->idxNum = 0;
176888 assert( pInfo->estimatedCost>1000000.0 );
176889
176890 return SQLITE_OK;
176891}
176892
176893/*
176894** xOpen - Open a cursor.
176895*/
176896static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
176897 Fts3tokCursor *pCsr;
176898 UNUSED_PARAMETER(pVTab);
176899
176900 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
176901 if( pCsr==0 ){
176902 return SQLITE_NOMEM;
176903 }
176904 memset(pCsr, 0, sizeof(Fts3tokCursor));
176905
176906 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
176907 return SQLITE_OK;
176908}
176909
176910/*
176911** Reset the tokenizer cursor passed as the only argument. As if it had
176912** just been returned by fts3tokOpenMethod().
176913*/
176914static void fts3tokResetCursor(Fts3tokCursor *pCsr){
176915 if( pCsr->pCsr ){
176916 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
176917 pTab->pMod->xClose(pCsr->pCsr);
176918 pCsr->pCsr = 0;
176919 }
176920 sqlite3_free(pCsr->zInput);
176921 pCsr->zInput = 0;
176922 pCsr->zToken = 0;
176923 pCsr->nToken = 0;
176924 pCsr->iStart = 0;
176925 pCsr->iEnd = 0;
176926 pCsr->iPos = 0;
176927 pCsr->iRowid = 0;
176928}
176929
176930/*
176931** xClose - Close a cursor.
176932*/
176933static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
176934 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176935
176936 fts3tokResetCursor(pCsr);
176937 sqlite3_free(pCsr);
176938 return SQLITE_OK;
176939}
176940
176941/*
176942** xNext - Advance the cursor to the next row, if any.
176943*/
176944static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
176945 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176946 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
176947 int rc; /* Return code */
176948
176949 pCsr->iRowid++;
176950 rc = pTab->pMod->xNext(pCsr->pCsr,
176951 &pCsr->zToken, &pCsr->nToken,
176952 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
176953 );
176954
176955 if( rc!=SQLITE_OK ){
176956 fts3tokResetCursor(pCsr);
176957 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
176958 }
176959
176960 return rc;
176961}
176962
176963/*
176964** xFilter - Initialize a cursor to point at the start of its data.
176965*/
176966static int fts3tokFilterMethod(
176967 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
176968 int idxNum, /* Strategy index */
176969 const char *idxStr, /* Unused */
176970 int nVal, /* Number of elements in apVal */
176971 sqlite3_value **apVal /* Arguments for the indexing scheme */
176972){
176973 int rc = SQLITE_ERROR;
176974 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
176975 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
176976 UNUSED_PARAMETER(idxStr);
176977 UNUSED_PARAMETER(nVal);
176978
176979 fts3tokResetCursor(pCsr);
176980 if( idxNum==1 ){
176981 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
176982 int nByte = sqlite3_value_bytes(apVal[0]);
176983 pCsr->zInput = sqlite3_malloc64(nByte+1);
176984 if( pCsr->zInput==0 ){
176985 rc = SQLITE_NOMEM;
176986 }else{
176987 if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
176988 pCsr->zInput[nByte] = 0;
176989 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
176990 if( rc==SQLITE_OK ){
176991 pCsr->pCsr->pTokenizer = pTab->pTok;
176992 }
176993 }
176994 }
176995
176996 if( rc!=SQLITE_OK ) return rc;
176997 return fts3tokNextMethod(pCursor);
176998}
176999
177000/*
177001** xEof - Return true if the cursor is at EOF, or false otherwise.
177002*/
177003static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
177004 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
177005 return (pCsr->zToken==0);
177006}
177007
177008/*
177009** xColumn - Return a column value.
177010*/
177011static int fts3tokColumnMethod(
177012 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
177013 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
177014 int iCol /* Index of column to read value from */
177015){
177016 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
177017
177018 /* CREATE TABLE x(input, token, start, end, position) */
177019 switch( iCol ){
177020 case 0:
177021 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
177022 break;
177023 case 1:
177024 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
177025 break;
177026 case 2:
177027 sqlite3_result_int(pCtx, pCsr->iStart);
177028 break;
177029 case 3:
177030 sqlite3_result_int(pCtx, pCsr->iEnd);
177031 break;
177032 default:
177033 assert( iCol==4 );
177034 sqlite3_result_int(pCtx, pCsr->iPos);
177035 break;
177036 }
177037 return SQLITE_OK;
177038}
177039
177040/*
177041** xRowid - Return the current rowid for the cursor.
177042*/
177043static int fts3tokRowidMethod(
177044 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
177045 sqlite_int64 *pRowid /* OUT: Rowid value */
177046){
177047 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
177048 *pRowid = (sqlite3_int64)pCsr->iRowid;
177049 return SQLITE_OK;
177050}
177051
177052/*
177053** Register the fts3tok module with database connection db. Return SQLITE_OK
177054** if successful or an error code if sqlite3_create_module() fails.
177055*/
177056SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
177057 static const sqlite3_module fts3tok_module = {
177058 0, /* iVersion */
177059 fts3tokConnectMethod, /* xCreate */
177060 fts3tokConnectMethod, /* xConnect */
177061 fts3tokBestIndexMethod, /* xBestIndex */
177062 fts3tokDisconnectMethod, /* xDisconnect */
177063 fts3tokDisconnectMethod, /* xDestroy */
177064 fts3tokOpenMethod, /* xOpen */
177065 fts3tokCloseMethod, /* xClose */
177066 fts3tokFilterMethod, /* xFilter */
177067 fts3tokNextMethod, /* xNext */
177068 fts3tokEofMethod, /* xEof */
177069 fts3tokColumnMethod, /* xColumn */
177070 fts3tokRowidMethod, /* xRowid */
177071 0, /* xUpdate */
177072 0, /* xBegin */
177073 0, /* xSync */
177074 0, /* xCommit */
177075 0, /* xRollback */
177076 0, /* xFindFunction */
177077 0, /* xRename */
177078 0, /* xSavepoint */
177079 0, /* xRelease */
177080 0, /* xRollbackTo */
177081 0 /* xShadowName */
177082 };
177083 int rc; /* Return code */
177084
177085 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
177086 return rc;
177087}
177088
177089#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
177090
177091/************** End of fts3_tokenize_vtab.c **********************************/
177092/************** Begin file fts3_write.c **************************************/
177093/*
177094** 2009 Oct 23
177095**
177096** The author disclaims copyright to this source code. In place of
177097** a legal notice, here is a blessing:
177098**
177099** May you do good and not evil.
177100** May you find forgiveness for yourself and forgive others.
177101** May you share freely, never taking more than you give.
177102**
177103******************************************************************************
177104**
177105** This file is part of the SQLite FTS3 extension module. Specifically,
177106** this file contains code to insert, update and delete rows from FTS3
177107** tables. It also contains code to merge FTS3 b-tree segments. Some
177108** of the sub-routines used to merge segments are also used by the query
177109** code in fts3.c.
177110*/
177111
177112/* #include "fts3Int.h" */
177113#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
177114
177115/* #include <string.h> */
177116/* #include <assert.h> */
177117/* #include <stdlib.h> */
177118/* #include <stdio.h> */
177119
177120#define FTS_MAX_APPENDABLE_HEIGHT 16
177121
177122/*
177123** When full-text index nodes are loaded from disk, the buffer that they
177124** are loaded into has the following number of bytes of padding at the end
177125** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
177126** of 920 bytes is allocated for it.
177127**
177128** This means that if we have a pointer into a buffer containing node data,
177129** it is always safe to read up to two varints from it without risking an
177130** overread, even if the node data is corrupted.
177131*/
177132#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
177133
177134/*
177135** Under certain circumstances, b-tree nodes (doclists) can be loaded into
177136** memory incrementally instead of all at once. This can be a big performance
177137** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
177138** method before retrieving all query results (as may happen, for example,
177139** if a query has a LIMIT clause).
177140**
177141** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
177142** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
177143** The code is written so that the hard lower-limit for each of these values
177144** is 1. Clearly such small values would be inefficient, but can be useful
177145** for testing purposes.
177146**
177147** If this module is built with SQLITE_TEST defined, these constants may
177148** be overridden at runtime for testing purposes. File fts3_test.c contains
177149** a Tcl interface to read and write the values.
177150*/
177151#ifdef SQLITE_TEST
177152int test_fts3_node_chunksize = (4*1024);
177153int test_fts3_node_chunk_threshold = (4*1024)*4;
177154# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
177155# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
177156#else
177157# define FTS3_NODE_CHUNKSIZE (4*1024)
177158# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
177159#endif
177160
177161/*
177162** The values that may be meaningfully bound to the :1 parameter in
177163** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
177164*/
177165#define FTS_STAT_DOCTOTAL 0
177166#define FTS_STAT_INCRMERGEHINT 1
177167#define FTS_STAT_AUTOINCRMERGE 2
177168
177169/*
177170** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
177171** and incremental merge operation that takes place. This is used for
177172** debugging FTS only, it should not usually be turned on in production
177173** systems.
177174*/
177175#ifdef FTS3_LOG_MERGES
177176static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
177177 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
177178}
177179#else
177180#define fts3LogMerge(x, y)
177181#endif
177182
177183
177184typedef struct PendingList PendingList;
177185typedef struct SegmentNode SegmentNode;
177186typedef struct SegmentWriter SegmentWriter;
177187
177188/*
177189** An instance of the following data structure is used to build doclists
177190** incrementally. See function fts3PendingListAppend() for details.
177191*/
177192struct PendingList {
177193 int nData;
177194 char *aData;
177195 int nSpace;
177196 sqlite3_int64 iLastDocid;
177197 sqlite3_int64 iLastCol;
177198 sqlite3_int64 iLastPos;
177199};
177200
177201
177202/*
177203** Each cursor has a (possibly empty) linked list of the following objects.
177204*/
177205struct Fts3DeferredToken {
177206 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
177207 int iCol; /* Column token must occur in */
177208 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
177209 PendingList *pList; /* Doclist is assembled here */
177210};
177211
177212/*
177213** An instance of this structure is used to iterate through the terms on
177214** a contiguous set of segment b-tree leaf nodes. Although the details of
177215** this structure are only manipulated by code in this file, opaque handles
177216** of type Fts3SegReader* are also used by code in fts3.c to iterate through
177217** terms when querying the full-text index. See functions:
177218**
177219** sqlite3Fts3SegReaderNew()
177220** sqlite3Fts3SegReaderFree()
177221** sqlite3Fts3SegReaderIterate()
177222**
177223** Methods used to manipulate Fts3SegReader structures:
177224**
177225** fts3SegReaderNext()
177226** fts3SegReaderFirstDocid()
177227** fts3SegReaderNextDocid()
177228*/
177229struct Fts3SegReader {
177230 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
177231 u8 bLookup; /* True for a lookup only */
177232 u8 rootOnly; /* True for a root-only reader */
177233
177234 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
177235 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
177236 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
177237 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
177238
177239 char *aNode; /* Pointer to node data (or NULL) */
177240 int nNode; /* Size of buffer at aNode (or 0) */
177241 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
177242 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
177243
177244 Fts3HashElem **ppNextElem;
177245
177246 /* Variables set by fts3SegReaderNext(). These may be read directly
177247 ** by the caller. They are valid from the time SegmentReaderNew() returns
177248 ** until SegmentReaderNext() returns something other than SQLITE_OK
177249 ** (i.e. SQLITE_DONE).
177250 */
177251 int nTerm; /* Number of bytes in current term */
177252 char *zTerm; /* Pointer to current term */
177253 int nTermAlloc; /* Allocated size of zTerm buffer */
177254 char *aDoclist; /* Pointer to doclist of current entry */
177255 int nDoclist; /* Size of doclist in current entry */
177256
177257 /* The following variables are used by fts3SegReaderNextDocid() to iterate
177258 ** through the current doclist (aDoclist/nDoclist).
177259 */
177260 char *pOffsetList;
177261 int nOffsetList; /* For descending pending seg-readers only */
177262 sqlite3_int64 iDocid;
177263};
177264
177265#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
177266#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
177267
177268/*
177269** An instance of this structure is used to create a segment b-tree in the
177270** database. The internal details of this type are only accessed by the
177271** following functions:
177272**
177273** fts3SegWriterAdd()
177274** fts3SegWriterFlush()
177275** fts3SegWriterFree()
177276*/
177277struct SegmentWriter {
177278 SegmentNode *pTree; /* Pointer to interior tree structure */
177279 sqlite3_int64 iFirst; /* First slot in %_segments written */
177280 sqlite3_int64 iFree; /* Next free slot in %_segments */
177281 char *zTerm; /* Pointer to previous term buffer */
177282 int nTerm; /* Number of bytes in zTerm */
177283 int nMalloc; /* Size of malloc'd buffer at zMalloc */
177284 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
177285 int nSize; /* Size of allocation at aData */
177286 int nData; /* Bytes of data in aData */
177287 char *aData; /* Pointer to block from malloc() */
177288 i64 nLeafData; /* Number of bytes of leaf data written */
177289};
177290
177291/*
177292** Type SegmentNode is used by the following three functions to create
177293** the interior part of the segment b+-tree structures (everything except
177294** the leaf nodes). These functions and type are only ever used by code
177295** within the fts3SegWriterXXX() family of functions described above.
177296**
177297** fts3NodeAddTerm()
177298** fts3NodeWrite()
177299** fts3NodeFree()
177300**
177301** When a b+tree is written to the database (either as a result of a merge
177302** or the pending-terms table being flushed), leaves are written into the
177303** database file as soon as they are completely populated. The interior of
177304** the tree is assembled in memory and written out only once all leaves have
177305** been populated and stored. This is Ok, as the b+-tree fanout is usually
177306** very large, meaning that the interior of the tree consumes relatively
177307** little memory.
177308*/
177309struct SegmentNode {
177310 SegmentNode *pParent; /* Parent node (or NULL for root node) */
177311 SegmentNode *pRight; /* Pointer to right-sibling */
177312 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
177313 int nEntry; /* Number of terms written to node so far */
177314 char *zTerm; /* Pointer to previous term buffer */
177315 int nTerm; /* Number of bytes in zTerm */
177316 int nMalloc; /* Size of malloc'd buffer at zMalloc */
177317 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
177318 int nData; /* Bytes of valid data so far */
177319 char *aData; /* Node data */
177320};
177321
177322/*
177323** Valid values for the second argument to fts3SqlStmt().
177324*/
177325#define SQL_DELETE_CONTENT 0
177326#define SQL_IS_EMPTY 1
177327#define SQL_DELETE_ALL_CONTENT 2
177328#define SQL_DELETE_ALL_SEGMENTS 3
177329#define SQL_DELETE_ALL_SEGDIR 4
177330#define SQL_DELETE_ALL_DOCSIZE 5
177331#define SQL_DELETE_ALL_STAT 6
177332#define SQL_SELECT_CONTENT_BY_ROWID 7
177333#define SQL_NEXT_SEGMENT_INDEX 8
177334#define SQL_INSERT_SEGMENTS 9
177335#define SQL_NEXT_SEGMENTS_ID 10
177336#define SQL_INSERT_SEGDIR 11
177337#define SQL_SELECT_LEVEL 12
177338#define SQL_SELECT_LEVEL_RANGE 13
177339#define SQL_SELECT_LEVEL_COUNT 14
177340#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
177341#define SQL_DELETE_SEGDIR_LEVEL 16
177342#define SQL_DELETE_SEGMENTS_RANGE 17
177343#define SQL_CONTENT_INSERT 18
177344#define SQL_DELETE_DOCSIZE 19
177345#define SQL_REPLACE_DOCSIZE 20
177346#define SQL_SELECT_DOCSIZE 21
177347#define SQL_SELECT_STAT 22
177348#define SQL_REPLACE_STAT 23
177349
177350#define SQL_SELECT_ALL_PREFIX_LEVEL 24
177351#define SQL_DELETE_ALL_TERMS_SEGDIR 25
177352#define SQL_DELETE_SEGDIR_RANGE 26
177353#define SQL_SELECT_ALL_LANGID 27
177354#define SQL_FIND_MERGE_LEVEL 28
177355#define SQL_MAX_LEAF_NODE_ESTIMATE 29
177356#define SQL_DELETE_SEGDIR_ENTRY 30
177357#define SQL_SHIFT_SEGDIR_ENTRY 31
177358#define SQL_SELECT_SEGDIR 32
177359#define SQL_CHOMP_SEGDIR 33
177360#define SQL_SEGMENT_IS_APPENDABLE 34
177361#define SQL_SELECT_INDEXES 35
177362#define SQL_SELECT_MXLEVEL 36
177363
177364#define SQL_SELECT_LEVEL_RANGE2 37
177365#define SQL_UPDATE_LEVEL_IDX 38
177366#define SQL_UPDATE_LEVEL 39
177367
177368/*
177369** This function is used to obtain an SQLite prepared statement handle
177370** for the statement identified by the second argument. If successful,
177371** *pp is set to the requested statement handle and SQLITE_OK returned.
177372** Otherwise, an SQLite error code is returned and *pp is set to 0.
177373**
177374** If argument apVal is not NULL, then it must point to an array with
177375** at least as many entries as the requested statement has bound
177376** parameters. The values are bound to the statements parameters before
177377** returning.
177378*/
177379static int fts3SqlStmt(
177380 Fts3Table *p, /* Virtual table handle */
177381 int eStmt, /* One of the SQL_XXX constants above */
177382 sqlite3_stmt **pp, /* OUT: Statement handle */
177383 sqlite3_value **apVal /* Values to bind to statement */
177384){
177385 const char *azSql[] = {
177386/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
177387/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
177388/* 2 */ "DELETE FROM %Q.'%q_content'",
177389/* 3 */ "DELETE FROM %Q.'%q_segments'",
177390/* 4 */ "DELETE FROM %Q.'%q_segdir'",
177391/* 5 */ "DELETE FROM %Q.'%q_docsize'",
177392/* 6 */ "DELETE FROM %Q.'%q_stat'",
177393/* 7 */ "SELECT %s WHERE rowid=?",
177394/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
177395/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
177396/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
177397/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
177398
177399 /* Return segments in order from oldest to newest.*/
177400/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
177401 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
177402/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
177403 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
177404 "ORDER BY level DESC, idx ASC",
177405
177406/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
177407/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
177408
177409/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
177410/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
177411/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
177412/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
177413/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
177414/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
177415/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
177416/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
177417/* 24 */ "",
177418/* 25 */ "",
177419
177420/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
177421/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
177422
177423/* This statement is used to determine which level to read the input from
177424** when performing an incremental merge. It returns the absolute level number
177425** of the oldest level in the db that contains at least ? segments. Or,
177426** if no level in the FTS index contains more than ? segments, the statement
177427** returns zero rows. */
177428/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
177429 " GROUP BY level HAVING cnt>=?"
177430 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
177431
177432/* Estimate the upper limit on the number of leaf nodes in a new segment
177433** created by merging the oldest :2 segments from absolute level :1. See
177434** function sqlite3Fts3Incrmerge() for details. */
177435/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
177436 " FROM (SELECT * FROM %Q.'%q_segdir' "
177437 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
177438 " )",
177439
177440/* SQL_DELETE_SEGDIR_ENTRY
177441** Delete the %_segdir entry on absolute level :1 with index :2. */
177442/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
177443
177444/* SQL_SHIFT_SEGDIR_ENTRY
177445** Modify the idx value for the segment with idx=:3 on absolute level :2
177446** to :1. */
177447/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
177448
177449/* SQL_SELECT_SEGDIR
177450** Read a single entry from the %_segdir table. The entry from absolute
177451** level :1 with index value :2. */
177452/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
177453 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
177454
177455/* SQL_CHOMP_SEGDIR
177456** Update the start_block (:1) and root (:2) fields of the %_segdir
177457** entry located on absolute level :3 with index :4. */
177458/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
177459 "WHERE level = ? AND idx = ?",
177460
177461/* SQL_SEGMENT_IS_APPENDABLE
177462** Return a single row if the segment with end_block=? is appendable. Or
177463** no rows otherwise. */
177464/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
177465
177466/* SQL_SELECT_INDEXES
177467** Return the list of valid segment indexes for absolute level ? */
177468/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
177469
177470/* SQL_SELECT_MXLEVEL
177471** Return the largest relative level in the FTS index or indexes. */
177472/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
177473
177474 /* Return segments in order from oldest to newest.*/
177475/* 37 */ "SELECT level, idx, end_block "
177476 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
177477 "ORDER BY level DESC, idx ASC",
177478
177479 /* Update statements used while promoting segments */
177480/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
177481 "WHERE level=? AND idx=?",
177482/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
177483
177484 };
177485 int rc = SQLITE_OK;
177486 sqlite3_stmt *pStmt;
177487
177488 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
177489 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
177490
177491 pStmt = p->aStmt[eStmt];
177492 if( !pStmt ){
177493 int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
177494 char *zSql;
177495 if( eStmt==SQL_CONTENT_INSERT ){
177496 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
177497 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
177498 f &= ~SQLITE_PREPARE_NO_VTAB;
177499 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
177500 }else{
177501 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
177502 }
177503 if( !zSql ){
177504 rc = SQLITE_NOMEM;
177505 }else{
177506 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL);
177507 sqlite3_free(zSql);
177508 assert( rc==SQLITE_OK || pStmt==0 );
177509 p->aStmt[eStmt] = pStmt;
177510 }
177511 }
177512 if( apVal ){
177513 int i;
177514 int nParam = sqlite3_bind_parameter_count(pStmt);
177515 for(i=0; rc==SQLITE_OK && i<nParam; i++){
177516 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
177517 }
177518 }
177519 *pp = pStmt;
177520 return rc;
177521}
177522
177523
177524static int fts3SelectDocsize(
177525 Fts3Table *pTab, /* FTS3 table handle */
177526 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
177527 sqlite3_stmt **ppStmt /* OUT: Statement handle */
177528){
177529 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
177530 int rc; /* Return code */
177531
177532 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
177533 if( rc==SQLITE_OK ){
177534 sqlite3_bind_int64(pStmt, 1, iDocid);
177535 rc = sqlite3_step(pStmt);
177536 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
177537 rc = sqlite3_reset(pStmt);
177538 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
177539 pStmt = 0;
177540 }else{
177541 rc = SQLITE_OK;
177542 }
177543 }
177544
177545 *ppStmt = pStmt;
177546 return rc;
177547}
177548
177549SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
177550 Fts3Table *pTab, /* Fts3 table handle */
177551 sqlite3_stmt **ppStmt /* OUT: Statement handle */
177552){
177553 sqlite3_stmt *pStmt = 0;
177554 int rc;
177555 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
177556 if( rc==SQLITE_OK ){
177557 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
177558 if( sqlite3_step(pStmt)!=SQLITE_ROW
177559 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
177560 ){
177561 rc = sqlite3_reset(pStmt);
177562 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
177563 pStmt = 0;
177564 }
177565 }
177566 *ppStmt = pStmt;
177567 return rc;
177568}
177569
177570SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
177571 Fts3Table *pTab, /* Fts3 table handle */
177572 sqlite3_int64 iDocid, /* Docid to read size data for */
177573 sqlite3_stmt **ppStmt /* OUT: Statement handle */
177574){
177575 return fts3SelectDocsize(pTab, iDocid, ppStmt);
177576}
177577
177578/*
177579** Similar to fts3SqlStmt(). Except, after binding the parameters in
177580** array apVal[] to the SQL statement identified by eStmt, the statement
177581** is executed.
177582**
177583** Returns SQLITE_OK if the statement is successfully executed, or an
177584** SQLite error code otherwise.
177585*/
177586static void fts3SqlExec(
177587 int *pRC, /* Result code */
177588 Fts3Table *p, /* The FTS3 table */
177589 int eStmt, /* Index of statement to evaluate */
177590 sqlite3_value **apVal /* Parameters to bind */
177591){
177592 sqlite3_stmt *pStmt;
177593 int rc;
177594 if( *pRC ) return;
177595 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
177596 if( rc==SQLITE_OK ){
177597 sqlite3_step(pStmt);
177598 rc = sqlite3_reset(pStmt);
177599 }
177600 *pRC = rc;
177601}
177602
177603
177604/*
177605** This function ensures that the caller has obtained an exclusive
177606** shared-cache table-lock on the %_segdir table. This is required before
177607** writing data to the fts3 table. If this lock is not acquired first, then
177608** the caller may end up attempting to take this lock as part of committing
177609** a transaction, causing SQLite to return SQLITE_LOCKED or
177610** LOCKED_SHAREDCACHEto a COMMIT command.
177611**
177612** It is best to avoid this because if FTS3 returns any error when
177613** committing a transaction, the whole transaction will be rolled back.
177614** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
177615** It can still happen if the user locks the underlying tables directly
177616** instead of accessing them via FTS.
177617*/
177618static int fts3Writelock(Fts3Table *p){
177619 int rc = SQLITE_OK;
177620
177621 if( p->nPendingData==0 ){
177622 sqlite3_stmt *pStmt;
177623 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
177624 if( rc==SQLITE_OK ){
177625 sqlite3_bind_null(pStmt, 1);
177626 sqlite3_step(pStmt);
177627 rc = sqlite3_reset(pStmt);
177628 }
177629 }
177630
177631 return rc;
177632}
177633
177634/*
177635** FTS maintains a separate indexes for each language-id (a 32-bit integer).
177636** Within each language id, a separate index is maintained to store the
177637** document terms, and each configured prefix size (configured the FTS
177638** "prefix=" option). And each index consists of multiple levels ("relative
177639** levels").
177640**
177641** All three of these values (the language id, the specific index and the
177642** level within the index) are encoded in 64-bit integer values stored
177643** in the %_segdir table on disk. This function is used to convert three
177644** separate component values into the single 64-bit integer value that
177645** can be used to query the %_segdir table.
177646**
177647** Specifically, each language-id/index combination is allocated 1024
177648** 64-bit integer level values ("absolute levels"). The main terms index
177649** for language-id 0 is allocate values 0-1023. The first prefix index
177650** (if any) for language-id 0 is allocated values 1024-2047. And so on.
177651** Language 1 indexes are allocated immediately following language 0.
177652**
177653** So, for a system with nPrefix prefix indexes configured, the block of
177654** absolute levels that corresponds to language-id iLangid and index
177655** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
177656*/
177657static sqlite3_int64 getAbsoluteLevel(
177658 Fts3Table *p, /* FTS3 table handle */
177659 int iLangid, /* Language id */
177660 int iIndex, /* Index in p->aIndex[] */
177661 int iLevel /* Level of segments */
177662){
177663 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
177664 assert_fts3_nc( iLangid>=0 );
177665 assert( p->nIndex>0 );
177666 assert( iIndex>=0 && iIndex<p->nIndex );
177667
177668 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
177669 return iBase + iLevel;
177670}
177671
177672/*
177673** Set *ppStmt to a statement handle that may be used to iterate through
177674** all rows in the %_segdir table, from oldest to newest. If successful,
177675** return SQLITE_OK. If an error occurs while preparing the statement,
177676** return an SQLite error code.
177677**
177678** There is only ever one instance of this SQL statement compiled for
177679** each FTS3 table.
177680**
177681** The statement returns the following columns from the %_segdir table:
177682**
177683** 0: idx
177684** 1: start_block
177685** 2: leaves_end_block
177686** 3: end_block
177687** 4: root
177688*/
177689SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
177690 Fts3Table *p, /* FTS3 table */
177691 int iLangid, /* Language being queried */
177692 int iIndex, /* Index for p->aIndex[] */
177693 int iLevel, /* Level to select (relative level) */
177694 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
177695){
177696 int rc;
177697 sqlite3_stmt *pStmt = 0;
177698
177699 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
177700 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
177701 assert( iIndex>=0 && iIndex<p->nIndex );
177702
177703 if( iLevel<0 ){
177704 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
177705 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
177706 if( rc==SQLITE_OK ){
177707 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
177708 sqlite3_bind_int64(pStmt, 2,
177709 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
177710 );
177711 }
177712 }else{
177713 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
177714 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
177715 if( rc==SQLITE_OK ){
177716 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
177717 }
177718 }
177719 *ppStmt = pStmt;
177720 return rc;
177721}
177722
177723
177724/*
177725** Append a single varint to a PendingList buffer. SQLITE_OK is returned
177726** if successful, or an SQLite error code otherwise.
177727**
177728** This function also serves to allocate the PendingList structure itself.
177729** For example, to create a new PendingList structure containing two
177730** varints:
177731**
177732** PendingList *p = 0;
177733** fts3PendingListAppendVarint(&p, 1);
177734** fts3PendingListAppendVarint(&p, 2);
177735*/
177736static int fts3PendingListAppendVarint(
177737 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
177738 sqlite3_int64 i /* Value to append to data */
177739){
177740 PendingList *p = *pp;
177741
177742 /* Allocate or grow the PendingList as required. */
177743 if( !p ){
177744 p = sqlite3_malloc(sizeof(*p) + 100);
177745 if( !p ){
177746 return SQLITE_NOMEM;
177747 }
177748 p->nSpace = 100;
177749 p->aData = (char *)&p[1];
177750 p->nData = 0;
177751 }
177752 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
177753 int nNew = p->nSpace * 2;
177754 p = sqlite3_realloc(p, sizeof(*p) + nNew);
177755 if( !p ){
177756 sqlite3_free(*pp);
177757 *pp = 0;
177758 return SQLITE_NOMEM;
177759 }
177760 p->nSpace = nNew;
177761 p->aData = (char *)&p[1];
177762 }
177763
177764 /* Append the new serialized varint to the end of the list. */
177765 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
177766 p->aData[p->nData] = '\0';
177767 *pp = p;
177768 return SQLITE_OK;
177769}
177770
177771/*
177772** Add a docid/column/position entry to a PendingList structure. Non-zero
177773** is returned if the structure is sqlite3_realloced as part of adding
177774** the entry. Otherwise, zero.
177775**
177776** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
177777** Zero is always returned in this case. Otherwise, if no OOM error occurs,
177778** it is set to SQLITE_OK.
177779*/
177780static int fts3PendingListAppend(
177781 PendingList **pp, /* IN/OUT: PendingList structure */
177782 sqlite3_int64 iDocid, /* Docid for entry to add */
177783 sqlite3_int64 iCol, /* Column for entry to add */
177784 sqlite3_int64 iPos, /* Position of term for entry to add */
177785 int *pRc /* OUT: Return code */
177786){
177787 PendingList *p = *pp;
177788 int rc = SQLITE_OK;
177789
177790 assert( !p || p->iLastDocid<=iDocid );
177791
177792 if( !p || p->iLastDocid!=iDocid ){
177793 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
177794 if( p ){
177795 assert( p->nData<p->nSpace );
177796 assert( p->aData[p->nData]==0 );
177797 p->nData++;
177798 }
177799 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
177800 goto pendinglistappend_out;
177801 }
177802 p->iLastCol = -1;
177803 p->iLastPos = 0;
177804 p->iLastDocid = iDocid;
177805 }
177806 if( iCol>0 && p->iLastCol!=iCol ){
177807 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
177808 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
177809 ){
177810 goto pendinglistappend_out;
177811 }
177812 p->iLastCol = iCol;
177813 p->iLastPos = 0;
177814 }
177815 if( iCol>=0 ){
177816 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
177817 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
177818 if( rc==SQLITE_OK ){
177819 p->iLastPos = iPos;
177820 }
177821 }
177822
177823 pendinglistappend_out:
177824 *pRc = rc;
177825 if( p!=*pp ){
177826 *pp = p;
177827 return 1;
177828 }
177829 return 0;
177830}
177831
177832/*
177833** Free a PendingList object allocated by fts3PendingListAppend().
177834*/
177835static void fts3PendingListDelete(PendingList *pList){
177836 sqlite3_free(pList);
177837}
177838
177839/*
177840** Add an entry to one of the pending-terms hash tables.
177841*/
177842static int fts3PendingTermsAddOne(
177843 Fts3Table *p,
177844 int iCol,
177845 int iPos,
177846 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
177847 const char *zToken,
177848 int nToken
177849){
177850 PendingList *pList;
177851 int rc = SQLITE_OK;
177852
177853 pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
177854 if( pList ){
177855 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
177856 }
177857 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
177858 if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
177859 /* Malloc failed while inserting the new entry. This can only
177860 ** happen if there was no previous entry for this token.
177861 */
177862 assert( 0==fts3HashFind(pHash, zToken, nToken) );
177863 sqlite3_free(pList);
177864 rc = SQLITE_NOMEM;
177865 }
177866 }
177867 if( rc==SQLITE_OK ){
177868 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
177869 }
177870 return rc;
177871}
177872
177873/*
177874** Tokenize the nul-terminated string zText and add all tokens to the
177875** pending-terms hash-table. The docid used is that currently stored in
177876** p->iPrevDocid, and the column is specified by argument iCol.
177877**
177878** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
177879*/
177880static int fts3PendingTermsAdd(
177881 Fts3Table *p, /* Table into which text will be inserted */
177882 int iLangid, /* Language id to use */
177883 const char *zText, /* Text of document to be inserted */
177884 int iCol, /* Column into which text is being inserted */
177885 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
177886){
177887 int rc;
177888 int iStart = 0;
177889 int iEnd = 0;
177890 int iPos = 0;
177891 int nWord = 0;
177892
177893 char const *zToken;
177894 int nToken = 0;
177895
177896 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
177897 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
177898 sqlite3_tokenizer_cursor *pCsr;
177899 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
177900 const char**,int*,int*,int*,int*);
177901
177902 assert( pTokenizer && pModule );
177903
177904 /* If the user has inserted a NULL value, this function may be called with
177905 ** zText==0. In this case, add zero token entries to the hash table and
177906 ** return early. */
177907 if( zText==0 ){
177908 *pnWord = 0;
177909 return SQLITE_OK;
177910 }
177911
177912 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
177913 if( rc!=SQLITE_OK ){
177914 return rc;
177915 }
177916
177917 xNext = pModule->xNext;
177918 while( SQLITE_OK==rc
177919 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
177920 ){
177921 int i;
177922 if( iPos>=nWord ) nWord = iPos+1;
177923
177924 /* Positions cannot be negative; we use -1 as a terminator internally.
177925 ** Tokens must have a non-zero length.
177926 */
177927 if( iPos<0 || !zToken || nToken<=0 ){
177928 rc = SQLITE_ERROR;
177929 break;
177930 }
177931
177932 /* Add the term to the terms index */
177933 rc = fts3PendingTermsAddOne(
177934 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
177935 );
177936
177937 /* Add the term to each of the prefix indexes that it is not too
177938 ** short for. */
177939 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
177940 struct Fts3Index *pIndex = &p->aIndex[i];
177941 if( nToken<pIndex->nPrefix ) continue;
177942 rc = fts3PendingTermsAddOne(
177943 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
177944 );
177945 }
177946 }
177947
177948 pModule->xClose(pCsr);
177949 *pnWord += nWord;
177950 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
177951}
177952
177953/*
177954** Calling this function indicates that subsequent calls to
177955** fts3PendingTermsAdd() are to add term/position-list pairs for the
177956** contents of the document with docid iDocid.
177957*/
177958static int fts3PendingTermsDocid(
177959 Fts3Table *p, /* Full-text table handle */
177960 int bDelete, /* True if this op is a delete */
177961 int iLangid, /* Language id of row being written */
177962 sqlite_int64 iDocid /* Docid of row being written */
177963){
177964 assert( iLangid>=0 );
177965 assert( bDelete==1 || bDelete==0 );
177966
177967 /* TODO(shess) Explore whether partially flushing the buffer on
177968 ** forced-flush would provide better performance. I suspect that if
177969 ** we ordered the doclists by size and flushed the largest until the
177970 ** buffer was half empty, that would let the less frequent terms
177971 ** generate longer doclists.
177972 */
177973 if( iDocid<p->iPrevDocid
177974 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
177975 || p->iPrevLangid!=iLangid
177976 || p->nPendingData>p->nMaxPendingData
177977 ){
177978 int rc = sqlite3Fts3PendingTermsFlush(p);
177979 if( rc!=SQLITE_OK ) return rc;
177980 }
177981 p->iPrevDocid = iDocid;
177982 p->iPrevLangid = iLangid;
177983 p->bPrevDelete = bDelete;
177984 return SQLITE_OK;
177985}
177986
177987/*
177988** Discard the contents of the pending-terms hash tables.
177989*/
177990SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
177991 int i;
177992 for(i=0; i<p->nIndex; i++){
177993 Fts3HashElem *pElem;
177994 Fts3Hash *pHash = &p->aIndex[i].hPending;
177995 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
177996 PendingList *pList = (PendingList *)fts3HashData(pElem);
177997 fts3PendingListDelete(pList);
177998 }
177999 fts3HashClear(pHash);
178000 }
178001 p->nPendingData = 0;
178002}
178003
178004/*
178005** This function is called by the xUpdate() method as part of an INSERT
178006** operation. It adds entries for each term in the new record to the
178007** pendingTerms hash table.
178008**
178009** Argument apVal is the same as the similarly named argument passed to
178010** fts3InsertData(). Parameter iDocid is the docid of the new row.
178011*/
178012static int fts3InsertTerms(
178013 Fts3Table *p,
178014 int iLangid,
178015 sqlite3_value **apVal,
178016 u32 *aSz
178017){
178018 int i; /* Iterator variable */
178019 for(i=2; i<p->nColumn+2; i++){
178020 int iCol = i-2;
178021 if( p->abNotindexed[iCol]==0 ){
178022 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
178023 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
178024 if( rc!=SQLITE_OK ){
178025 return rc;
178026 }
178027 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
178028 }
178029 }
178030 return SQLITE_OK;
178031}
178032
178033/*
178034** This function is called by the xUpdate() method for an INSERT operation.
178035** The apVal parameter is passed a copy of the apVal argument passed by
178036** SQLite to the xUpdate() method. i.e:
178037**
178038** apVal[0] Not used for INSERT.
178039** apVal[1] rowid
178040** apVal[2] Left-most user-defined column
178041** ...
178042** apVal[p->nColumn+1] Right-most user-defined column
178043** apVal[p->nColumn+2] Hidden column with same name as table
178044** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
178045** apVal[p->nColumn+4] Hidden languageid column
178046*/
178047static int fts3InsertData(
178048 Fts3Table *p, /* Full-text table */
178049 sqlite3_value **apVal, /* Array of values to insert */
178050 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
178051){
178052 int rc; /* Return code */
178053 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
178054
178055 if( p->zContentTbl ){
178056 sqlite3_value *pRowid = apVal[p->nColumn+3];
178057 if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
178058 pRowid = apVal[1];
178059 }
178060 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
178061 return SQLITE_CONSTRAINT;
178062 }
178063 *piDocid = sqlite3_value_int64(pRowid);
178064 return SQLITE_OK;
178065 }
178066
178067 /* Locate the statement handle used to insert data into the %_content
178068 ** table. The SQL for this statement is:
178069 **
178070 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
178071 **
178072 ** The statement features N '?' variables, where N is the number of user
178073 ** defined columns in the FTS3 table, plus one for the docid field.
178074 */
178075 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
178076 if( rc==SQLITE_OK && p->zLanguageid ){
178077 rc = sqlite3_bind_int(
178078 pContentInsert, p->nColumn+2,
178079 sqlite3_value_int(apVal[p->nColumn+4])
178080 );
178081 }
178082 if( rc!=SQLITE_OK ) return rc;
178083
178084 /* There is a quirk here. The users INSERT statement may have specified
178085 ** a value for the "rowid" field, for the "docid" field, or for both.
178086 ** Which is a problem, since "rowid" and "docid" are aliases for the
178087 ** same value. For example:
178088 **
178089 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
178090 **
178091 ** In FTS3, this is an error. It is an error to specify non-NULL values
178092 ** for both docid and some other rowid alias.
178093 */
178094 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
178095 if( SQLITE_NULL==sqlite3_value_type(apVal[0])
178096 && SQLITE_NULL!=sqlite3_value_type(apVal[1])
178097 ){
178098 /* A rowid/docid conflict. */
178099 return SQLITE_ERROR;
178100 }
178101 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
178102 if( rc!=SQLITE_OK ) return rc;
178103 }
178104
178105 /* Execute the statement to insert the record. Set *piDocid to the
178106 ** new docid value.
178107 */
178108 sqlite3_step(pContentInsert);
178109 rc = sqlite3_reset(pContentInsert);
178110
178111 *piDocid = sqlite3_last_insert_rowid(p->db);
178112 return rc;
178113}
178114
178115
178116
178117/*
178118** Remove all data from the FTS3 table. Clear the hash table containing
178119** pending terms.
178120*/
178121static int fts3DeleteAll(Fts3Table *p, int bContent){
178122 int rc = SQLITE_OK; /* Return code */
178123
178124 /* Discard the contents of the pending-terms hash table. */
178125 sqlite3Fts3PendingTermsClear(p);
178126
178127 /* Delete everything from the shadow tables. Except, leave %_content as
178128 ** is if bContent is false. */
178129 assert( p->zContentTbl==0 || bContent==0 );
178130 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
178131 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
178132 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
178133 if( p->bHasDocsize ){
178134 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
178135 }
178136 if( p->bHasStat ){
178137 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
178138 }
178139 return rc;
178140}
178141
178142/*
178143**
178144*/
178145static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
178146 int iLangid = 0;
178147 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
178148 return iLangid;
178149}
178150
178151/*
178152** The first element in the apVal[] array is assumed to contain the docid
178153** (an integer) of a row about to be deleted. Remove all terms from the
178154** full-text index.
178155*/
178156static void fts3DeleteTerms(
178157 int *pRC, /* Result code */
178158 Fts3Table *p, /* The FTS table to delete from */
178159 sqlite3_value *pRowid, /* The docid to be deleted */
178160 u32 *aSz, /* Sizes of deleted document written here */
178161 int *pbFound /* OUT: Set to true if row really does exist */
178162){
178163 int rc;
178164 sqlite3_stmt *pSelect;
178165
178166 assert( *pbFound==0 );
178167 if( *pRC ) return;
178168 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
178169 if( rc==SQLITE_OK ){
178170 if( SQLITE_ROW==sqlite3_step(pSelect) ){
178171 int i;
178172 int iLangid = langidFromSelect(p, pSelect);
178173 i64 iDocid = sqlite3_column_int64(pSelect, 0);
178174 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
178175 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
178176 int iCol = i-1;
178177 if( p->abNotindexed[iCol]==0 ){
178178 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
178179 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
178180 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
178181 }
178182 }
178183 if( rc!=SQLITE_OK ){
178184 sqlite3_reset(pSelect);
178185 *pRC = rc;
178186 return;
178187 }
178188 *pbFound = 1;
178189 }
178190 rc = sqlite3_reset(pSelect);
178191 }else{
178192 sqlite3_reset(pSelect);
178193 }
178194 *pRC = rc;
178195}
178196
178197/*
178198** Forward declaration to account for the circular dependency between
178199** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
178200*/
178201static int fts3SegmentMerge(Fts3Table *, int, int, int);
178202
178203/*
178204** This function allocates a new level iLevel index in the segdir table.
178205** Usually, indexes are allocated within a level sequentially starting
178206** with 0, so the allocated index is one greater than the value returned
178207** by:
178208**
178209** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
178210**
178211** However, if there are already FTS3_MERGE_COUNT indexes at the requested
178212** level, they are merged into a single level (iLevel+1) segment and the
178213** allocated index is 0.
178214**
178215** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
178216** returned. Otherwise, an SQLite error code is returned.
178217*/
178218static int fts3AllocateSegdirIdx(
178219 Fts3Table *p,
178220 int iLangid, /* Language id */
178221 int iIndex, /* Index for p->aIndex */
178222 int iLevel,
178223 int *piIdx
178224){
178225 int rc; /* Return Code */
178226 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
178227 int iNext = 0; /* Result of query pNextIdx */
178228
178229 assert( iLangid>=0 );
178230 assert( p->nIndex>=1 );
178231
178232 /* Set variable iNext to the next available segdir index at level iLevel. */
178233 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
178234 if( rc==SQLITE_OK ){
178235 sqlite3_bind_int64(
178236 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
178237 );
178238 if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
178239 iNext = sqlite3_column_int(pNextIdx, 0);
178240 }
178241 rc = sqlite3_reset(pNextIdx);
178242 }
178243
178244 if( rc==SQLITE_OK ){
178245 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
178246 ** full, merge all segments in level iLevel into a single iLevel+1
178247 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
178248 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
178249 */
178250 if( iNext>=MergeCount(p) ){
178251 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
178252 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
178253 *piIdx = 0;
178254 }else{
178255 *piIdx = iNext;
178256 }
178257 }
178258
178259 return rc;
178260}
178261
178262/*
178263** The %_segments table is declared as follows:
178264**
178265** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
178266**
178267** This function reads data from a single row of the %_segments table. The
178268** specific row is identified by the iBlockid parameter. If paBlob is not
178269** NULL, then a buffer is allocated using sqlite3_malloc() and populated
178270** with the contents of the blob stored in the "block" column of the
178271** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
178272** to the size of the blob in bytes before returning.
178273**
178274** If an error occurs, or the table does not contain the specified row,
178275** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
178276** paBlob is non-NULL, then it is the responsibility of the caller to
178277** eventually free the returned buffer.
178278**
178279** This function may leave an open sqlite3_blob* handle in the
178280** Fts3Table.pSegments variable. This handle is reused by subsequent calls
178281** to this function. The handle may be closed by calling the
178282** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
178283** performance improvement, but the blob handle should always be closed
178284** before control is returned to the user (to prevent a lock being held
178285** on the database file for longer than necessary). Thus, any virtual table
178286** method (xFilter etc.) that may directly or indirectly call this function
178287** must call sqlite3Fts3SegmentsClose() before returning.
178288*/
178289SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
178290 Fts3Table *p, /* FTS3 table handle */
178291 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
178292 char **paBlob, /* OUT: Blob data in malloc'd buffer */
178293 int *pnBlob, /* OUT: Size of blob data */
178294 int *pnLoad /* OUT: Bytes actually loaded */
178295){
178296 int rc; /* Return code */
178297
178298 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
178299 assert( pnBlob );
178300
178301 if( p->pSegments ){
178302 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
178303 }else{
178304 if( 0==p->zSegmentsTbl ){
178305 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
178306 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
178307 }
178308 rc = sqlite3_blob_open(
178309 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
178310 );
178311 }
178312
178313 if( rc==SQLITE_OK ){
178314 int nByte = sqlite3_blob_bytes(p->pSegments);
178315 *pnBlob = nByte;
178316 if( paBlob ){
178317 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
178318 if( !aByte ){
178319 rc = SQLITE_NOMEM;
178320 }else{
178321 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
178322 nByte = FTS3_NODE_CHUNKSIZE;
178323 *pnLoad = nByte;
178324 }
178325 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
178326 memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
178327 if( rc!=SQLITE_OK ){
178328 sqlite3_free(aByte);
178329 aByte = 0;
178330 }
178331 }
178332 *paBlob = aByte;
178333 }
178334 }else if( rc==SQLITE_ERROR ){
178335 rc = FTS_CORRUPT_VTAB;
178336 }
178337
178338 return rc;
178339}
178340
178341/*
178342** Close the blob handle at p->pSegments, if it is open. See comments above
178343** the sqlite3Fts3ReadBlock() function for details.
178344*/
178345SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
178346 sqlite3_blob_close(p->pSegments);
178347 p->pSegments = 0;
178348}
178349
178350static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
178351 int nRead; /* Number of bytes to read */
178352 int rc; /* Return code */
178353
178354 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
178355 rc = sqlite3_blob_read(
178356 pReader->pBlob,
178357 &pReader->aNode[pReader->nPopulate],
178358 nRead,
178359 pReader->nPopulate
178360 );
178361
178362 if( rc==SQLITE_OK ){
178363 pReader->nPopulate += nRead;
178364 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
178365 if( pReader->nPopulate==pReader->nNode ){
178366 sqlite3_blob_close(pReader->pBlob);
178367 pReader->pBlob = 0;
178368 pReader->nPopulate = 0;
178369 }
178370 }
178371 return rc;
178372}
178373
178374static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
178375 int rc = SQLITE_OK;
178376 assert( !pReader->pBlob
178377 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
178378 );
178379 while( pReader->pBlob && rc==SQLITE_OK
178380 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
178381 ){
178382 rc = fts3SegReaderIncrRead(pReader);
178383 }
178384 return rc;
178385}
178386
178387/*
178388** Set an Fts3SegReader cursor to point at EOF.
178389*/
178390static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
178391 if( !fts3SegReaderIsRootOnly(pSeg) ){
178392 sqlite3_free(pSeg->aNode);
178393 sqlite3_blob_close(pSeg->pBlob);
178394 pSeg->pBlob = 0;
178395 }
178396 pSeg->aNode = 0;
178397}
178398
178399/*
178400** Move the iterator passed as the first argument to the next term in the
178401** segment. If successful, SQLITE_OK is returned. If there is no next term,
178402** SQLITE_DONE. Otherwise, an SQLite error code.
178403*/
178404static int fts3SegReaderNext(
178405 Fts3Table *p,
178406 Fts3SegReader *pReader,
178407 int bIncr
178408){
178409 int rc; /* Return code of various sub-routines */
178410 char *pNext; /* Cursor variable */
178411 int nPrefix; /* Number of bytes in term prefix */
178412 int nSuffix; /* Number of bytes in term suffix */
178413
178414 if( !pReader->aDoclist ){
178415 pNext = pReader->aNode;
178416 }else{
178417 pNext = &pReader->aDoclist[pReader->nDoclist];
178418 }
178419
178420 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
178421
178422 if( fts3SegReaderIsPending(pReader) ){
178423 Fts3HashElem *pElem = *(pReader->ppNextElem);
178424 sqlite3_free(pReader->aNode);
178425 pReader->aNode = 0;
178426 if( pElem ){
178427 char *aCopy;
178428 PendingList *pList = (PendingList *)fts3HashData(pElem);
178429 int nCopy = pList->nData+1;
178430 pReader->zTerm = (char *)fts3HashKey(pElem);
178431 pReader->nTerm = fts3HashKeysize(pElem);
178432 aCopy = (char*)sqlite3_malloc(nCopy);
178433 if( !aCopy ) return SQLITE_NOMEM;
178434 memcpy(aCopy, pList->aData, nCopy);
178435 pReader->nNode = pReader->nDoclist = nCopy;
178436 pReader->aNode = pReader->aDoclist = aCopy;
178437 pReader->ppNextElem++;
178438 assert( pReader->aNode );
178439 }
178440 return SQLITE_OK;
178441 }
178442
178443 fts3SegReaderSetEof(pReader);
178444
178445 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
178446 ** blocks have already been traversed. */
178447#ifdef CORRUPT_DB
178448 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
178449#endif
178450 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
178451 return SQLITE_OK;
178452 }
178453
178454 rc = sqlite3Fts3ReadBlock(
178455 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
178456 (bIncr ? &pReader->nPopulate : 0)
178457 );
178458 if( rc!=SQLITE_OK ) return rc;
178459 assert( pReader->pBlob==0 );
178460 if( bIncr && pReader->nPopulate<pReader->nNode ){
178461 pReader->pBlob = p->pSegments;
178462 p->pSegments = 0;
178463 }
178464 pNext = pReader->aNode;
178465 }
178466
178467 assert( !fts3SegReaderIsPending(pReader) );
178468
178469 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
178470 if( rc!=SQLITE_OK ) return rc;
178471
178472 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
178473 ** safe (no risk of overread) even if the node data is corrupted. */
178474 pNext += fts3GetVarint32(pNext, &nPrefix);
178475 pNext += fts3GetVarint32(pNext, &nSuffix);
178476 if( nSuffix<=0
178477 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
178478 || nPrefix>pReader->nTerm
178479 ){
178480 return FTS_CORRUPT_VTAB;
178481 }
178482
178483 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
178484 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
178485 ** overflow - hence the (i64) casts. */
178486 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
178487 i64 nNew = ((i64)nPrefix+nSuffix)*2;
178488 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
178489 if( !zNew ){
178490 return SQLITE_NOMEM;
178491 }
178492 pReader->zTerm = zNew;
178493 pReader->nTermAlloc = nNew;
178494 }
178495
178496 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
178497 if( rc!=SQLITE_OK ) return rc;
178498
178499 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
178500 pReader->nTerm = nPrefix+nSuffix;
178501 pNext += nSuffix;
178502 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
178503 pReader->aDoclist = pNext;
178504 pReader->pOffsetList = 0;
178505
178506 /* Check that the doclist does not appear to extend past the end of the
178507 ** b-tree node. And that the final byte of the doclist is 0x00. If either
178508 ** of these statements is untrue, then the data structure is corrupt.
178509 */
178510 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
178511 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
178512 || pReader->nDoclist==0
178513 ){
178514 return FTS_CORRUPT_VTAB;
178515 }
178516 return SQLITE_OK;
178517}
178518
178519/*
178520** Set the SegReader to point to the first docid in the doclist associated
178521** with the current term.
178522*/
178523static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
178524 int rc = SQLITE_OK;
178525 assert( pReader->aDoclist );
178526 assert( !pReader->pOffsetList );
178527 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
178528 u8 bEof = 0;
178529 pReader->iDocid = 0;
178530 pReader->nOffsetList = 0;
178531 sqlite3Fts3DoclistPrev(0,
178532 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
178533 &pReader->iDocid, &pReader->nOffsetList, &bEof
178534 );
178535 }else{
178536 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
178537 if( rc==SQLITE_OK ){
178538 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
178539 pReader->pOffsetList = &pReader->aDoclist[n];
178540 }
178541 }
178542 return rc;
178543}
178544
178545/*
178546** Advance the SegReader to point to the next docid in the doclist
178547** associated with the current term.
178548**
178549** If arguments ppOffsetList and pnOffsetList are not NULL, then
178550** *ppOffsetList is set to point to the first column-offset list
178551** in the doclist entry (i.e. immediately past the docid varint).
178552** *pnOffsetList is set to the length of the set of column-offset
178553** lists, not including the nul-terminator byte. For example:
178554*/
178555static int fts3SegReaderNextDocid(
178556 Fts3Table *pTab,
178557 Fts3SegReader *pReader, /* Reader to advance to next docid */
178558 char **ppOffsetList, /* OUT: Pointer to current position-list */
178559 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
178560){
178561 int rc = SQLITE_OK;
178562 char *p = pReader->pOffsetList;
178563 char c = 0;
178564
178565 assert( p );
178566
178567 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
178568 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
178569 ** Pending-terms doclists are always built up in ascending order, so
178570 ** we have to iterate through them backwards here. */
178571 u8 bEof = 0;
178572 if( ppOffsetList ){
178573 *ppOffsetList = pReader->pOffsetList;
178574 *pnOffsetList = pReader->nOffsetList - 1;
178575 }
178576 sqlite3Fts3DoclistPrev(0,
178577 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
178578 &pReader->nOffsetList, &bEof
178579 );
178580 if( bEof ){
178581 pReader->pOffsetList = 0;
178582 }else{
178583 pReader->pOffsetList = p;
178584 }
178585 }else{
178586 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
178587
178588 /* Pointer p currently points at the first byte of an offset list. The
178589 ** following block advances it to point one byte past the end of
178590 ** the same offset list. */
178591 while( 1 ){
178592
178593 /* The following line of code (and the "p++" below the while() loop) is
178594 ** normally all that is required to move pointer p to the desired
178595 ** position. The exception is if this node is being loaded from disk
178596 ** incrementally and pointer "p" now points to the first byte past
178597 ** the populated part of pReader->aNode[].
178598 */
178599 while( *p | c ) c = *p++ & 0x80;
178600 assert( *p==0 );
178601
178602 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
178603 rc = fts3SegReaderIncrRead(pReader);
178604 if( rc!=SQLITE_OK ) return rc;
178605 }
178606 p++;
178607
178608 /* If required, populate the output variables with a pointer to and the
178609 ** size of the previous offset-list.
178610 */
178611 if( ppOffsetList ){
178612 *ppOffsetList = pReader->pOffsetList;
178613 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
178614 }
178615
178616 /* List may have been edited in place by fts3EvalNearTrim() */
178617 while( p<pEnd && *p==0 ) p++;
178618
178619 /* If there are no more entries in the doclist, set pOffsetList to
178620 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
178621 ** Fts3SegReader.pOffsetList to point to the next offset list before
178622 ** returning.
178623 */
178624 if( p>=pEnd ){
178625 pReader->pOffsetList = 0;
178626 }else{
178627 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
178628 if( rc==SQLITE_OK ){
178629 u64 iDelta;
178630 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
178631 if( pTab->bDescIdx ){
178632 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
178633 }else{
178634 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
178635 }
178636 }
178637 }
178638 }
178639
178640 return rc;
178641}
178642
178643
178644SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
178645 Fts3Cursor *pCsr,
178646 Fts3MultiSegReader *pMsr,
178647 int *pnOvfl
178648){
178649 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
178650 int nOvfl = 0;
178651 int ii;
178652 int rc = SQLITE_OK;
178653 int pgsz = p->nPgsz;
178654
178655 assert( p->bFts4 );
178656 assert( pgsz>0 );
178657
178658 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
178659 Fts3SegReader *pReader = pMsr->apSegment[ii];
178660 if( !fts3SegReaderIsPending(pReader)
178661 && !fts3SegReaderIsRootOnly(pReader)
178662 ){
178663 sqlite3_int64 jj;
178664 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
178665 int nBlob;
178666 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
178667 if( rc!=SQLITE_OK ) break;
178668 if( (nBlob+35)>pgsz ){
178669 nOvfl += (nBlob + 34)/pgsz;
178670 }
178671 }
178672 }
178673 }
178674 *pnOvfl = nOvfl;
178675 return rc;
178676}
178677
178678/*
178679** Free all allocations associated with the iterator passed as the
178680** second argument.
178681*/
178682SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
178683 if( pReader ){
178684 if( !fts3SegReaderIsPending(pReader) ){
178685 sqlite3_free(pReader->zTerm);
178686 }
178687 if( !fts3SegReaderIsRootOnly(pReader) ){
178688 sqlite3_free(pReader->aNode);
178689 }
178690 sqlite3_blob_close(pReader->pBlob);
178691 }
178692 sqlite3_free(pReader);
178693}
178694
178695/*
178696** Allocate a new SegReader object.
178697*/
178698SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
178699 int iAge, /* Segment "age". */
178700 int bLookup, /* True for a lookup only */
178701 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
178702 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
178703 sqlite3_int64 iEndBlock, /* Final block of segment */
178704 const char *zRoot, /* Buffer containing root node */
178705 int nRoot, /* Size of buffer containing root node */
178706 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
178707){
178708 Fts3SegReader *pReader; /* Newly allocated SegReader object */
178709 int nExtra = 0; /* Bytes to allocate segment root node */
178710
178711 assert( zRoot!=0 || nRoot==0 );
178712#ifdef CORRUPT_DB
178713 assert( zRoot!=0 || CORRUPT_DB );
178714#endif
178715
178716 if( iStartLeaf==0 ){
178717 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
178718 nExtra = nRoot + FTS3_NODE_PADDING;
178719 }
178720
178721 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
178722 if( !pReader ){
178723 return SQLITE_NOMEM;
178724 }
178725 memset(pReader, 0, sizeof(Fts3SegReader));
178726 pReader->iIdx = iAge;
178727 pReader->bLookup = bLookup!=0;
178728 pReader->iStartBlock = iStartLeaf;
178729 pReader->iLeafEndBlock = iEndLeaf;
178730 pReader->iEndBlock = iEndBlock;
178731
178732 if( nExtra ){
178733 /* The entire segment is stored in the root node. */
178734 pReader->aNode = (char *)&pReader[1];
178735 pReader->rootOnly = 1;
178736 pReader->nNode = nRoot;
178737 if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
178738 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
178739 }else{
178740 pReader->iCurrentBlock = iStartLeaf-1;
178741 }
178742 *ppReader = pReader;
178743 return SQLITE_OK;
178744}
178745
178746/*
178747** This is a comparison function used as a qsort() callback when sorting
178748** an array of pending terms by term. This occurs as part of flushing
178749** the contents of the pending-terms hash table to the database.
178750*/
178751static int SQLITE_CDECL fts3CompareElemByTerm(
178752 const void *lhs,
178753 const void *rhs
178754){
178755 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
178756 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
178757 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
178758 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
178759
178760 int n = (n1<n2 ? n1 : n2);
178761 int c = memcmp(z1, z2, n);
178762 if( c==0 ){
178763 c = n1 - n2;
178764 }
178765 return c;
178766}
178767
178768/*
178769** This function is used to allocate an Fts3SegReader that iterates through
178770** a subset of the terms stored in the Fts3Table.pendingTerms array.
178771**
178772** If the isPrefixIter parameter is zero, then the returned SegReader iterates
178773** through each term in the pending-terms table. Or, if isPrefixIter is
178774** non-zero, it iterates through each term and its prefixes. For example, if
178775** the pending terms hash table contains the terms "sqlite", "mysql" and
178776** "firebird", then the iterator visits the following 'terms' (in the order
178777** shown):
178778**
178779** f fi fir fire fireb firebi firebir firebird
178780** m my mys mysq mysql
178781** s sq sql sqli sqlit sqlite
178782**
178783** Whereas if isPrefixIter is zero, the terms visited are:
178784**
178785** firebird mysql sqlite
178786*/
178787SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
178788 Fts3Table *p, /* Virtual table handle */
178789 int iIndex, /* Index for p->aIndex */
178790 const char *zTerm, /* Term to search for */
178791 int nTerm, /* Size of buffer zTerm */
178792 int bPrefix, /* True for a prefix iterator */
178793 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
178794){
178795 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
178796 Fts3HashElem *pE; /* Iterator variable */
178797 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
178798 int nElem = 0; /* Size of array at aElem */
178799 int rc = SQLITE_OK; /* Return Code */
178800 Fts3Hash *pHash;
178801
178802 pHash = &p->aIndex[iIndex].hPending;
178803 if( bPrefix ){
178804 int nAlloc = 0; /* Size of allocated array at aElem */
178805
178806 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
178807 char *zKey = (char *)fts3HashKey(pE);
178808 int nKey = fts3HashKeysize(pE);
178809 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
178810 if( nElem==nAlloc ){
178811 Fts3HashElem **aElem2;
178812 nAlloc += 16;
178813 aElem2 = (Fts3HashElem **)sqlite3_realloc(
178814 aElem, nAlloc*sizeof(Fts3HashElem *)
178815 );
178816 if( !aElem2 ){
178817 rc = SQLITE_NOMEM;
178818 nElem = 0;
178819 break;
178820 }
178821 aElem = aElem2;
178822 }
178823
178824 aElem[nElem++] = pE;
178825 }
178826 }
178827
178828 /* If more than one term matches the prefix, sort the Fts3HashElem
178829 ** objects in term order using qsort(). This uses the same comparison
178830 ** callback as is used when flushing terms to disk.
178831 */
178832 if( nElem>1 ){
178833 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
178834 }
178835
178836 }else{
178837 /* The query is a simple term lookup that matches at most one term in
178838 ** the index. All that is required is a straight hash-lookup.
178839 **
178840 ** Because the stack address of pE may be accessed via the aElem pointer
178841 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
178842 ** within this entire function, not just this "else{...}" block.
178843 */
178844 pE = fts3HashFindElem(pHash, zTerm, nTerm);
178845 if( pE ){
178846 aElem = &pE;
178847 nElem = 1;
178848 }
178849 }
178850
178851 if( nElem>0 ){
178852 sqlite3_int64 nByte;
178853 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
178854 pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
178855 if( !pReader ){
178856 rc = SQLITE_NOMEM;
178857 }else{
178858 memset(pReader, 0, nByte);
178859 pReader->iIdx = 0x7FFFFFFF;
178860 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
178861 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
178862 }
178863 }
178864
178865 if( bPrefix ){
178866 sqlite3_free(aElem);
178867 }
178868 *ppReader = pReader;
178869 return rc;
178870}
178871
178872/*
178873** Compare the entries pointed to by two Fts3SegReader structures.
178874** Comparison is as follows:
178875**
178876** 1) EOF is greater than not EOF.
178877**
178878** 2) The current terms (if any) are compared using memcmp(). If one
178879** term is a prefix of another, the longer term is considered the
178880** larger.
178881**
178882** 3) By segment age. An older segment is considered larger.
178883*/
178884static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178885 int rc;
178886 if( pLhs->aNode && pRhs->aNode ){
178887 int rc2 = pLhs->nTerm - pRhs->nTerm;
178888 if( rc2<0 ){
178889 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
178890 }else{
178891 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
178892 }
178893 if( rc==0 ){
178894 rc = rc2;
178895 }
178896 }else{
178897 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
178898 }
178899 if( rc==0 ){
178900 rc = pRhs->iIdx - pLhs->iIdx;
178901 }
178902 assert( rc!=0 );
178903 return rc;
178904}
178905
178906/*
178907** A different comparison function for SegReader structures. In this
178908** version, it is assumed that each SegReader points to an entry in
178909** a doclist for identical terms. Comparison is made as follows:
178910**
178911** 1) EOF (end of doclist in this case) is greater than not EOF.
178912**
178913** 2) By current docid.
178914**
178915** 3) By segment age. An older segment is considered larger.
178916*/
178917static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178918 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
178919 if( rc==0 ){
178920 if( pLhs->iDocid==pRhs->iDocid ){
178921 rc = pRhs->iIdx - pLhs->iIdx;
178922 }else{
178923 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
178924 }
178925 }
178926 assert( pLhs->aNode && pRhs->aNode );
178927 return rc;
178928}
178929static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
178930 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
178931 if( rc==0 ){
178932 if( pLhs->iDocid==pRhs->iDocid ){
178933 rc = pRhs->iIdx - pLhs->iIdx;
178934 }else{
178935 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
178936 }
178937 }
178938 assert( pLhs->aNode && pRhs->aNode );
178939 return rc;
178940}
178941
178942/*
178943** Compare the term that the Fts3SegReader object passed as the first argument
178944** points to with the term specified by arguments zTerm and nTerm.
178945**
178946** If the pSeg iterator is already at EOF, return 0. Otherwise, return
178947** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
178948** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
178949*/
178950static int fts3SegReaderTermCmp(
178951 Fts3SegReader *pSeg, /* Segment reader object */
178952 const char *zTerm, /* Term to compare to */
178953 int nTerm /* Size of term zTerm in bytes */
178954){
178955 int res = 0;
178956 if( pSeg->aNode ){
178957 if( pSeg->nTerm>nTerm ){
178958 res = memcmp(pSeg->zTerm, zTerm, nTerm);
178959 }else{
178960 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
178961 }
178962 if( res==0 ){
178963 res = pSeg->nTerm-nTerm;
178964 }
178965 }
178966 return res;
178967}
178968
178969/*
178970** Argument apSegment is an array of nSegment elements. It is known that
178971** the final (nSegment-nSuspect) members are already in sorted order
178972** (according to the comparison function provided). This function shuffles
178973** the array around until all entries are in sorted order.
178974*/
178975static void fts3SegReaderSort(
178976 Fts3SegReader **apSegment, /* Array to sort entries of */
178977 int nSegment, /* Size of apSegment array */
178978 int nSuspect, /* Unsorted entry count */
178979 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
178980){
178981 int i; /* Iterator variable */
178982
178983 assert( nSuspect<=nSegment );
178984
178985 if( nSuspect==nSegment ) nSuspect--;
178986 for(i=nSuspect-1; i>=0; i--){
178987 int j;
178988 for(j=i; j<(nSegment-1); j++){
178989 Fts3SegReader *pTmp;
178990 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
178991 pTmp = apSegment[j+1];
178992 apSegment[j+1] = apSegment[j];
178993 apSegment[j] = pTmp;
178994 }
178995 }
178996
178997#ifndef NDEBUG
178998 /* Check that the list really is sorted now. */
178999 for(i=0; i<(nSuspect-1); i++){
179000 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
179001 }
179002#endif
179003}
179004
179005/*
179006** Insert a record into the %_segments table.
179007*/
179008static int fts3WriteSegment(
179009 Fts3Table *p, /* Virtual table handle */
179010 sqlite3_int64 iBlock, /* Block id for new block */
179011 char *z, /* Pointer to buffer containing block data */
179012 int n /* Size of buffer z in bytes */
179013){
179014 sqlite3_stmt *pStmt;
179015 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
179016 if( rc==SQLITE_OK ){
179017 sqlite3_bind_int64(pStmt, 1, iBlock);
179018 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
179019 sqlite3_step(pStmt);
179020 rc = sqlite3_reset(pStmt);
179021 sqlite3_bind_null(pStmt, 2);
179022 }
179023 return rc;
179024}
179025
179026/*
179027** Find the largest relative level number in the table. If successful, set
179028** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
179029** set *pnMax to zero and return an SQLite error code.
179030*/
179031SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
179032 int rc;
179033 int mxLevel = 0;
179034 sqlite3_stmt *pStmt = 0;
179035
179036 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
179037 if( rc==SQLITE_OK ){
179038 if( SQLITE_ROW==sqlite3_step(pStmt) ){
179039 mxLevel = sqlite3_column_int(pStmt, 0);
179040 }
179041 rc = sqlite3_reset(pStmt);
179042 }
179043 *pnMax = mxLevel;
179044 return rc;
179045}
179046
179047/*
179048** Insert a record into the %_segdir table.
179049*/
179050static int fts3WriteSegdir(
179051 Fts3Table *p, /* Virtual table handle */
179052 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
179053 int iIdx, /* Value for "idx" field */
179054 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
179055 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
179056 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
179057 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
179058 char *zRoot, /* Blob value for "root" field */
179059 int nRoot /* Number of bytes in buffer zRoot */
179060){
179061 sqlite3_stmt *pStmt;
179062 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
179063 if( rc==SQLITE_OK ){
179064 sqlite3_bind_int64(pStmt, 1, iLevel);
179065 sqlite3_bind_int(pStmt, 2, iIdx);
179066 sqlite3_bind_int64(pStmt, 3, iStartBlock);
179067 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
179068 if( nLeafData==0 ){
179069 sqlite3_bind_int64(pStmt, 5, iEndBlock);
179070 }else{
179071 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
179072 if( !zEnd ) return SQLITE_NOMEM;
179073 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
179074 }
179075 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
179076 sqlite3_step(pStmt);
179077 rc = sqlite3_reset(pStmt);
179078 sqlite3_bind_null(pStmt, 6);
179079 }
179080 return rc;
179081}
179082
179083/*
179084** Return the size of the common prefix (if any) shared by zPrev and
179085** zNext, in bytes. For example,
179086**
179087** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
179088** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
179089** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
179090*/
179091static int fts3PrefixCompress(
179092 const char *zPrev, /* Buffer containing previous term */
179093 int nPrev, /* Size of buffer zPrev in bytes */
179094 const char *zNext, /* Buffer containing next term */
179095 int nNext /* Size of buffer zNext in bytes */
179096){
179097 int n;
179098 UNUSED_PARAMETER(nNext);
179099 for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
179100 return n;
179101}
179102
179103/*
179104** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
179105** (according to memcmp) than the previous term.
179106*/
179107static int fts3NodeAddTerm(
179108 Fts3Table *p, /* Virtual table handle */
179109 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
179110 int isCopyTerm, /* True if zTerm/nTerm is transient */
179111 const char *zTerm, /* Pointer to buffer containing term */
179112 int nTerm /* Size of term in bytes */
179113){
179114 SegmentNode *pTree = *ppTree;
179115 int rc;
179116 SegmentNode *pNew;
179117
179118 /* First try to append the term to the current node. Return early if
179119 ** this is possible.
179120 */
179121 if( pTree ){
179122 int nData = pTree->nData; /* Current size of node in bytes */
179123 int nReq = nData; /* Required space after adding zTerm */
179124 int nPrefix; /* Number of bytes of prefix compression */
179125 int nSuffix; /* Suffix length */
179126
179127 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
179128 nSuffix = nTerm-nPrefix;
179129
179130 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
179131 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
179132 ** compared with BINARY collation. This indicates corruption. */
179133 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
179134
179135 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
179136 if( nReq<=p->nNodeSize || !pTree->zTerm ){
179137
179138 if( nReq>p->nNodeSize ){
179139 /* An unusual case: this is the first term to be added to the node
179140 ** and the static node buffer (p->nNodeSize bytes) is not large
179141 ** enough. Use a separately malloced buffer instead This wastes
179142 ** p->nNodeSize bytes, but since this scenario only comes about when
179143 ** the database contain two terms that share a prefix of almost 2KB,
179144 ** this is not expected to be a serious problem.
179145 */
179146 assert( pTree->aData==(char *)&pTree[1] );
179147 pTree->aData = (char *)sqlite3_malloc(nReq);
179148 if( !pTree->aData ){
179149 return SQLITE_NOMEM;
179150 }
179151 }
179152
179153 if( pTree->zTerm ){
179154 /* There is no prefix-length field for first term in a node */
179155 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
179156 }
179157
179158 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
179159 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
179160 pTree->nData = nData + nSuffix;
179161 pTree->nEntry++;
179162
179163 if( isCopyTerm ){
179164 if( pTree->nMalloc<nTerm ){
179165 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
179166 if( !zNew ){
179167 return SQLITE_NOMEM;
179168 }
179169 pTree->nMalloc = nTerm*2;
179170 pTree->zMalloc = zNew;
179171 }
179172 pTree->zTerm = pTree->zMalloc;
179173 memcpy(pTree->zTerm, zTerm, nTerm);
179174 pTree->nTerm = nTerm;
179175 }else{
179176 pTree->zTerm = (char *)zTerm;
179177 pTree->nTerm = nTerm;
179178 }
179179 return SQLITE_OK;
179180 }
179181 }
179182
179183 /* If control flows to here, it was not possible to append zTerm to the
179184 ** current node. Create a new node (a right-sibling of the current node).
179185 ** If this is the first node in the tree, the term is added to it.
179186 **
179187 ** Otherwise, the term is not added to the new node, it is left empty for
179188 ** now. Instead, the term is inserted into the parent of pTree. If pTree
179189 ** has no parent, one is created here.
179190 */
179191 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
179192 if( !pNew ){
179193 return SQLITE_NOMEM;
179194 }
179195 memset(pNew, 0, sizeof(SegmentNode));
179196 pNew->nData = 1 + FTS3_VARINT_MAX;
179197 pNew->aData = (char *)&pNew[1];
179198
179199 if( pTree ){
179200 SegmentNode *pParent = pTree->pParent;
179201 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
179202 if( pTree->pParent==0 ){
179203 pTree->pParent = pParent;
179204 }
179205 pTree->pRight = pNew;
179206 pNew->pLeftmost = pTree->pLeftmost;
179207 pNew->pParent = pParent;
179208 pNew->zMalloc = pTree->zMalloc;
179209 pNew->nMalloc = pTree->nMalloc;
179210 pTree->zMalloc = 0;
179211 }else{
179212 pNew->pLeftmost = pNew;
179213 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
179214 }
179215
179216 *ppTree = pNew;
179217 return rc;
179218}
179219
179220/*
179221** Helper function for fts3NodeWrite().
179222*/
179223static int fts3TreeFinishNode(
179224 SegmentNode *pTree,
179225 int iHeight,
179226 sqlite3_int64 iLeftChild
179227){
179228 int nStart;
179229 assert( iHeight>=1 && iHeight<128 );
179230 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
179231 pTree->aData[nStart] = (char)iHeight;
179232 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
179233 return nStart;
179234}
179235
179236/*
179237** Write the buffer for the segment node pTree and all of its peers to the
179238** database. Then call this function recursively to write the parent of
179239** pTree and its peers to the database.
179240**
179241** Except, if pTree is a root node, do not write it to the database. Instead,
179242** set output variables *paRoot and *pnRoot to contain the root node.
179243**
179244** If successful, SQLITE_OK is returned and output variable *piLast is
179245** set to the largest blockid written to the database (or zero if no
179246** blocks were written to the db). Otherwise, an SQLite error code is
179247** returned.
179248*/
179249static int fts3NodeWrite(
179250 Fts3Table *p, /* Virtual table handle */
179251 SegmentNode *pTree, /* SegmentNode handle */
179252 int iHeight, /* Height of this node in tree */
179253 sqlite3_int64 iLeaf, /* Block id of first leaf node */
179254 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
179255 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
179256 char **paRoot, /* OUT: Data for root node */
179257 int *pnRoot /* OUT: Size of root node in bytes */
179258){
179259 int rc = SQLITE_OK;
179260
179261 if( !pTree->pParent ){
179262 /* Root node of the tree. */
179263 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
179264 *piLast = iFree-1;
179265 *pnRoot = pTree->nData - nStart;
179266 *paRoot = &pTree->aData[nStart];
179267 }else{
179268 SegmentNode *pIter;
179269 sqlite3_int64 iNextFree = iFree;
179270 sqlite3_int64 iNextLeaf = iLeaf;
179271 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
179272 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
179273 int nWrite = pIter->nData - nStart;
179274
179275 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
179276 iNextFree++;
179277 iNextLeaf += (pIter->nEntry+1);
179278 }
179279 if( rc==SQLITE_OK ){
179280 assert( iNextLeaf==iFree );
179281 rc = fts3NodeWrite(
179282 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
179283 );
179284 }
179285 }
179286
179287 return rc;
179288}
179289
179290/*
179291** Free all memory allocations associated with the tree pTree.
179292*/
179293static void fts3NodeFree(SegmentNode *pTree){
179294 if( pTree ){
179295 SegmentNode *p = pTree->pLeftmost;
179296 fts3NodeFree(p->pParent);
179297 while( p ){
179298 SegmentNode *pRight = p->pRight;
179299 if( p->aData!=(char *)&p[1] ){
179300 sqlite3_free(p->aData);
179301 }
179302 assert( pRight==0 || p->zMalloc==0 );
179303 sqlite3_free(p->zMalloc);
179304 sqlite3_free(p);
179305 p = pRight;
179306 }
179307 }
179308}
179309
179310/*
179311** Add a term to the segment being constructed by the SegmentWriter object
179312** *ppWriter. When adding the first term to a segment, *ppWriter should
179313** be passed NULL. This function will allocate a new SegmentWriter object
179314** and return it via the input/output variable *ppWriter in this case.
179315**
179316** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
179317*/
179318static int fts3SegWriterAdd(
179319 Fts3Table *p, /* Virtual table handle */
179320 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
179321 int isCopyTerm, /* True if buffer zTerm must be copied */
179322 const char *zTerm, /* Pointer to buffer containing term */
179323 int nTerm, /* Size of term in bytes */
179324 const char *aDoclist, /* Pointer to buffer containing doclist */
179325 int nDoclist /* Size of doclist in bytes */
179326){
179327 int nPrefix; /* Size of term prefix in bytes */
179328 int nSuffix; /* Size of term suffix in bytes */
179329 int nReq; /* Number of bytes required on leaf page */
179330 int nData;
179331 SegmentWriter *pWriter = *ppWriter;
179332
179333 if( !pWriter ){
179334 int rc;
179335 sqlite3_stmt *pStmt;
179336
179337 /* Allocate the SegmentWriter structure */
179338 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
179339 if( !pWriter ) return SQLITE_NOMEM;
179340 memset(pWriter, 0, sizeof(SegmentWriter));
179341 *ppWriter = pWriter;
179342
179343 /* Allocate a buffer in which to accumulate data */
179344 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
179345 if( !pWriter->aData ) return SQLITE_NOMEM;
179346 pWriter->nSize = p->nNodeSize;
179347
179348 /* Find the next free blockid in the %_segments table */
179349 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
179350 if( rc!=SQLITE_OK ) return rc;
179351 if( SQLITE_ROW==sqlite3_step(pStmt) ){
179352 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
179353 pWriter->iFirst = pWriter->iFree;
179354 }
179355 rc = sqlite3_reset(pStmt);
179356 if( rc!=SQLITE_OK ) return rc;
179357 }
179358 nData = pWriter->nData;
179359
179360 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
179361 nSuffix = nTerm-nPrefix;
179362
179363 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
179364 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
179365 ** compared with BINARY collation. This indicates corruption. */
179366 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
179367
179368 /* Figure out how many bytes are required by this new entry */
179369 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
179370 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
179371 nSuffix + /* Term suffix */
179372 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
179373 nDoclist; /* Doclist data */
179374
179375 if( nData>0 && nData+nReq>p->nNodeSize ){
179376 int rc;
179377
179378 /* The current leaf node is full. Write it out to the database. */
179379 if( pWriter->iFree==LARGEST_INT64 ) return FTS_CORRUPT_VTAB;
179380 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
179381 if( rc!=SQLITE_OK ) return rc;
179382 p->nLeafAdd++;
179383
179384 /* Add the current term to the interior node tree. The term added to
179385 ** the interior tree must:
179386 **
179387 ** a) be greater than the largest term on the leaf node just written
179388 ** to the database (still available in pWriter->zTerm), and
179389 **
179390 ** b) be less than or equal to the term about to be added to the new
179391 ** leaf node (zTerm/nTerm).
179392 **
179393 ** In other words, it must be the prefix of zTerm 1 byte longer than
179394 ** the common prefix (if any) of zTerm and pWriter->zTerm.
179395 */
179396 assert( nPrefix<nTerm );
179397 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
179398 if( rc!=SQLITE_OK ) return rc;
179399
179400 nData = 0;
179401 pWriter->nTerm = 0;
179402
179403 nPrefix = 0;
179404 nSuffix = nTerm;
179405 nReq = 1 + /* varint containing prefix size */
179406 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
179407 nTerm + /* Term suffix */
179408 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
179409 nDoclist; /* Doclist data */
179410 }
179411
179412 /* Increase the total number of bytes written to account for the new entry. */
179413 pWriter->nLeafData += nReq;
179414
179415 /* If the buffer currently allocated is too small for this entry, realloc
179416 ** the buffer to make it large enough.
179417 */
179418 if( nReq>pWriter->nSize ){
179419 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
179420 if( !aNew ) return SQLITE_NOMEM;
179421 pWriter->aData = aNew;
179422 pWriter->nSize = nReq;
179423 }
179424 assert( nData+nReq<=pWriter->nSize );
179425
179426 /* Append the prefix-compressed term and doclist to the buffer. */
179427 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
179428 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
179429 assert( nSuffix>0 );
179430 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
179431 nData += nSuffix;
179432 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
179433 assert( nDoclist>0 );
179434 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
179435 pWriter->nData = nData + nDoclist;
179436
179437 /* Save the current term so that it can be used to prefix-compress the next.
179438 ** If the isCopyTerm parameter is true, then the buffer pointed to by
179439 ** zTerm is transient, so take a copy of the term data. Otherwise, just
179440 ** store a copy of the pointer.
179441 */
179442 if( isCopyTerm ){
179443 if( nTerm>pWriter->nMalloc ){
179444 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
179445 if( !zNew ){
179446 return SQLITE_NOMEM;
179447 }
179448 pWriter->nMalloc = nTerm*2;
179449 pWriter->zMalloc = zNew;
179450 pWriter->zTerm = zNew;
179451 }
179452 assert( pWriter->zTerm==pWriter->zMalloc );
179453 assert( nTerm>0 );
179454 memcpy(pWriter->zTerm, zTerm, nTerm);
179455 }else{
179456 pWriter->zTerm = (char *)zTerm;
179457 }
179458 pWriter->nTerm = nTerm;
179459
179460 return SQLITE_OK;
179461}
179462
179463/*
179464** Flush all data associated with the SegmentWriter object pWriter to the
179465** database. This function must be called after all terms have been added
179466** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
179467** returned. Otherwise, an SQLite error code.
179468*/
179469static int fts3SegWriterFlush(
179470 Fts3Table *p, /* Virtual table handle */
179471 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
179472 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
179473 int iIdx /* Value for 'idx' column of %_segdir */
179474){
179475 int rc; /* Return code */
179476 if( pWriter->pTree ){
179477 sqlite3_int64 iLast = 0; /* Largest block id written to database */
179478 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
179479 char *zRoot = NULL; /* Pointer to buffer containing root node */
179480 int nRoot = 0; /* Size of buffer zRoot */
179481
179482 iLastLeaf = pWriter->iFree;
179483 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
179484 if( rc==SQLITE_OK ){
179485 rc = fts3NodeWrite(p, pWriter->pTree, 1,
179486 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
179487 }
179488 if( rc==SQLITE_OK ){
179489 rc = fts3WriteSegdir(p, iLevel, iIdx,
179490 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
179491 }
179492 }else{
179493 /* The entire tree fits on the root node. Write it to the segdir table. */
179494 rc = fts3WriteSegdir(p, iLevel, iIdx,
179495 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
179496 }
179497 p->nLeafAdd++;
179498 return rc;
179499}
179500
179501/*
179502** Release all memory held by the SegmentWriter object passed as the
179503** first argument.
179504*/
179505static void fts3SegWriterFree(SegmentWriter *pWriter){
179506 if( pWriter ){
179507 sqlite3_free(pWriter->aData);
179508 sqlite3_free(pWriter->zMalloc);
179509 fts3NodeFree(pWriter->pTree);
179510 sqlite3_free(pWriter);
179511 }
179512}
179513
179514/*
179515** The first value in the apVal[] array is assumed to contain an integer.
179516** This function tests if there exist any documents with docid values that
179517** are different from that integer. i.e. if deleting the document with docid
179518** pRowid would mean the FTS3 table were empty.
179519**
179520** If successful, *pisEmpty is set to true if the table is empty except for
179521** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
179522** error occurs, an SQLite error code is returned.
179523*/
179524static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
179525 sqlite3_stmt *pStmt;
179526 int rc;
179527 if( p->zContentTbl ){
179528 /* If using the content=xxx option, assume the table is never empty */
179529 *pisEmpty = 0;
179530 rc = SQLITE_OK;
179531 }else{
179532 rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
179533 if( rc==SQLITE_OK ){
179534 if( SQLITE_ROW==sqlite3_step(pStmt) ){
179535 *pisEmpty = sqlite3_column_int(pStmt, 0);
179536 }
179537 rc = sqlite3_reset(pStmt);
179538 }
179539 }
179540 return rc;
179541}
179542
179543/*
179544** Set *pnMax to the largest segment level in the database for the index
179545** iIndex.
179546**
179547** Segment levels are stored in the 'level' column of the %_segdir table.
179548**
179549** Return SQLITE_OK if successful, or an SQLite error code if not.
179550*/
179551static int fts3SegmentMaxLevel(
179552 Fts3Table *p,
179553 int iLangid,
179554 int iIndex,
179555 sqlite3_int64 *pnMax
179556){
179557 sqlite3_stmt *pStmt;
179558 int rc;
179559 assert( iIndex>=0 && iIndex<p->nIndex );
179560
179561 /* Set pStmt to the compiled version of:
179562 **
179563 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
179564 **
179565 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
179566 */
179567 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
179568 if( rc!=SQLITE_OK ) return rc;
179569 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
179570 sqlite3_bind_int64(pStmt, 2,
179571 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
179572 );
179573 if( SQLITE_ROW==sqlite3_step(pStmt) ){
179574 *pnMax = sqlite3_column_int64(pStmt, 0);
179575 }
179576 return sqlite3_reset(pStmt);
179577}
179578
179579/*
179580** iAbsLevel is an absolute level that may be assumed to exist within
179581** the database. This function checks if it is the largest level number
179582** within its index. Assuming no error occurs, *pbMax is set to 1 if
179583** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
179584** is returned. If an error occurs, an error code is returned and the
179585** final value of *pbMax is undefined.
179586*/
179587static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
179588
179589 /* Set pStmt to the compiled version of:
179590 **
179591 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
179592 **
179593 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
179594 */
179595 sqlite3_stmt *pStmt;
179596 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
179597 if( rc!=SQLITE_OK ) return rc;
179598 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
179599 sqlite3_bind_int64(pStmt, 2,
179600 (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
179601 );
179602
179603 *pbMax = 0;
179604 if( SQLITE_ROW==sqlite3_step(pStmt) ){
179605 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
179606 }
179607 return sqlite3_reset(pStmt);
179608}
179609
179610/*
179611** Delete all entries in the %_segments table associated with the segment
179612** opened with seg-reader pSeg. This function does not affect the contents
179613** of the %_segdir table.
179614*/
179615static int fts3DeleteSegment(
179616 Fts3Table *p, /* FTS table handle */
179617 Fts3SegReader *pSeg /* Segment to delete */
179618){
179619 int rc = SQLITE_OK; /* Return code */
179620 if( pSeg->iStartBlock ){
179621 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
179622 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
179623 if( rc==SQLITE_OK ){
179624 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
179625 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
179626 sqlite3_step(pDelete);
179627 rc = sqlite3_reset(pDelete);
179628 }
179629 }
179630 return rc;
179631}
179632
179633/*
179634** This function is used after merging multiple segments into a single large
179635** segment to delete the old, now redundant, segment b-trees. Specifically,
179636** it:
179637**
179638** 1) Deletes all %_segments entries for the segments associated with
179639** each of the SegReader objects in the array passed as the third
179640** argument, and
179641**
179642** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
179643** entries regardless of level if (iLevel<0).
179644**
179645** SQLITE_OK is returned if successful, otherwise an SQLite error code.
179646*/
179647static int fts3DeleteSegdir(
179648 Fts3Table *p, /* Virtual table handle */
179649 int iLangid, /* Language id */
179650 int iIndex, /* Index for p->aIndex */
179651 int iLevel, /* Level of %_segdir entries to delete */
179652 Fts3SegReader **apSegment, /* Array of SegReader objects */
179653 int nReader /* Size of array apSegment */
179654){
179655 int rc = SQLITE_OK; /* Return Code */
179656 int i; /* Iterator variable */
179657 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
179658
179659 for(i=0; rc==SQLITE_OK && i<nReader; i++){
179660 rc = fts3DeleteSegment(p, apSegment[i]);
179661 }
179662 if( rc!=SQLITE_OK ){
179663 return rc;
179664 }
179665
179666 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
179667 if( iLevel==FTS3_SEGCURSOR_ALL ){
179668 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
179669 if( rc==SQLITE_OK ){
179670 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
179671 sqlite3_bind_int64(pDelete, 2,
179672 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
179673 );
179674 }
179675 }else{
179676 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
179677 if( rc==SQLITE_OK ){
179678 sqlite3_bind_int64(
179679 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
179680 );
179681 }
179682 }
179683
179684 if( rc==SQLITE_OK ){
179685 sqlite3_step(pDelete);
179686 rc = sqlite3_reset(pDelete);
179687 }
179688
179689 return rc;
179690}
179691
179692/*
179693** When this function is called, buffer *ppList (size *pnList bytes) contains
179694** a position list that may (or may not) feature multiple columns. This
179695** function adjusts the pointer *ppList and the length *pnList so that they
179696** identify the subset of the position list that corresponds to column iCol.
179697**
179698** If there are no entries in the input position list for column iCol, then
179699** *pnList is set to zero before returning.
179700**
179701** If parameter bZero is non-zero, then any part of the input list following
179702** the end of the output list is zeroed before returning.
179703*/
179704static void fts3ColumnFilter(
179705 int iCol, /* Column to filter on */
179706 int bZero, /* Zero out anything following *ppList */
179707 char **ppList, /* IN/OUT: Pointer to position list */
179708 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
179709){
179710 char *pList = *ppList;
179711 int nList = *pnList;
179712 char *pEnd = &pList[nList];
179713 int iCurrent = 0;
179714 char *p = pList;
179715
179716 assert( iCol>=0 );
179717 while( 1 ){
179718 char c = 0;
179719 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
179720
179721 if( iCol==iCurrent ){
179722 nList = (int)(p - pList);
179723 break;
179724 }
179725
179726 nList -= (int)(p - pList);
179727 pList = p;
179728 if( nList<=0 ){
179729 break;
179730 }
179731 p = &pList[1];
179732 p += fts3GetVarint32(p, &iCurrent);
179733 }
179734
179735 if( bZero && (pEnd - &pList[nList])>0){
179736 memset(&pList[nList], 0, pEnd - &pList[nList]);
179737 }
179738 *ppList = pList;
179739 *pnList = nList;
179740}
179741
179742/*
179743** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
179744** existing data). Grow the buffer if required.
179745**
179746** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
179747** trying to resize the buffer, return SQLITE_NOMEM.
179748*/
179749static int fts3MsrBufferData(
179750 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
179751 char *pList,
179752 int nList
179753){
179754 if( nList>pMsr->nBuffer ){
179755 char *pNew;
179756 pMsr->nBuffer = nList*2;
179757 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
179758 if( !pNew ) return SQLITE_NOMEM;
179759 pMsr->aBuffer = pNew;
179760 }
179761
179762 assert( nList>0 );
179763 memcpy(pMsr->aBuffer, pList, nList);
179764 return SQLITE_OK;
179765}
179766
179767SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
179768 Fts3Table *p, /* Virtual table handle */
179769 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
179770 sqlite3_int64 *piDocid, /* OUT: Docid value */
179771 char **paPoslist, /* OUT: Pointer to position list */
179772 int *pnPoslist /* OUT: Size of position list in bytes */
179773){
179774 int nMerge = pMsr->nAdvance;
179775 Fts3SegReader **apSegment = pMsr->apSegment;
179776 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179777 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179778 );
179779
179780 if( nMerge==0 ){
179781 *paPoslist = 0;
179782 return SQLITE_OK;
179783 }
179784
179785 while( 1 ){
179786 Fts3SegReader *pSeg;
179787 pSeg = pMsr->apSegment[0];
179788
179789 if( pSeg->pOffsetList==0 ){
179790 *paPoslist = 0;
179791 break;
179792 }else{
179793 int rc;
179794 char *pList;
179795 int nList;
179796 int j;
179797 sqlite3_int64 iDocid = apSegment[0]->iDocid;
179798
179799 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
179800 j = 1;
179801 while( rc==SQLITE_OK
179802 && j<nMerge
179803 && apSegment[j]->pOffsetList
179804 && apSegment[j]->iDocid==iDocid
179805 ){
179806 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
179807 j++;
179808 }
179809 if( rc!=SQLITE_OK ) return rc;
179810 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
179811
179812 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
179813 rc = fts3MsrBufferData(pMsr, pList, nList+1);
179814 if( rc!=SQLITE_OK ) return rc;
179815 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
179816 pList = pMsr->aBuffer;
179817 }
179818
179819 if( pMsr->iColFilter>=0 ){
179820 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
179821 }
179822
179823 if( nList>0 ){
179824 *paPoslist = pList;
179825 *piDocid = iDocid;
179826 *pnPoslist = nList;
179827 break;
179828 }
179829 }
179830 }
179831
179832 return SQLITE_OK;
179833}
179834
179835static int fts3SegReaderStart(
179836 Fts3Table *p, /* Virtual table handle */
179837 Fts3MultiSegReader *pCsr, /* Cursor object */
179838 const char *zTerm, /* Term searched for (or NULL) */
179839 int nTerm /* Length of zTerm in bytes */
179840){
179841 int i;
179842 int nSeg = pCsr->nSegment;
179843
179844 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
179845 ** for, then advance each segment iterator until it points to a term of
179846 ** equal or greater value than the specified term. This prevents many
179847 ** unnecessary merge/sort operations for the case where single segment
179848 ** b-tree leaf nodes contain more than one term.
179849 */
179850 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
179851 int res = 0;
179852 Fts3SegReader *pSeg = pCsr->apSegment[i];
179853 do {
179854 int rc = fts3SegReaderNext(p, pSeg, 0);
179855 if( rc!=SQLITE_OK ) return rc;
179856 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
179857
179858 if( pSeg->bLookup && res!=0 ){
179859 fts3SegReaderSetEof(pSeg);
179860 }
179861 }
179862 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
179863
179864 return SQLITE_OK;
179865}
179866
179867SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
179868 Fts3Table *p, /* Virtual table handle */
179869 Fts3MultiSegReader *pCsr, /* Cursor object */
179870 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
179871){
179872 pCsr->pFilter = pFilter;
179873 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
179874}
179875
179876SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
179877 Fts3Table *p, /* Virtual table handle */
179878 Fts3MultiSegReader *pCsr, /* Cursor object */
179879 int iCol, /* Column to match on. */
179880 const char *zTerm, /* Term to iterate through a doclist for */
179881 int nTerm /* Number of bytes in zTerm */
179882){
179883 int i;
179884 int rc;
179885 int nSegment = pCsr->nSegment;
179886 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179887 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179888 );
179889
179890 assert( pCsr->pFilter==0 );
179891 assert( zTerm && nTerm>0 );
179892
179893 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
179894 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
179895 if( rc!=SQLITE_OK ) return rc;
179896
179897 /* Determine how many of the segments actually point to zTerm/nTerm. */
179898 for(i=0; i<nSegment; i++){
179899 Fts3SegReader *pSeg = pCsr->apSegment[i];
179900 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
179901 break;
179902 }
179903 }
179904 pCsr->nAdvance = i;
179905
179906 /* Advance each of the segments to point to the first docid. */
179907 for(i=0; i<pCsr->nAdvance; i++){
179908 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
179909 if( rc!=SQLITE_OK ) return rc;
179910 }
179911 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
179912
179913 assert( iCol<0 || iCol<p->nColumn );
179914 pCsr->iColFilter = iCol;
179915
179916 return SQLITE_OK;
179917}
179918
179919/*
179920** This function is called on a MultiSegReader that has been started using
179921** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
179922** have been made. Calling this function puts the MultiSegReader in such
179923** a state that if the next two calls are:
179924**
179925** sqlite3Fts3SegReaderStart()
179926** sqlite3Fts3SegReaderStep()
179927**
179928** then the entire doclist for the term is available in
179929** MultiSegReader.aDoclist/nDoclist.
179930*/
179931SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
179932 int i; /* Used to iterate through segment-readers */
179933
179934 assert( pCsr->zTerm==0 );
179935 assert( pCsr->nTerm==0 );
179936 assert( pCsr->aDoclist==0 );
179937 assert( pCsr->nDoclist==0 );
179938
179939 pCsr->nAdvance = 0;
179940 pCsr->bRestart = 1;
179941 for(i=0; i<pCsr->nSegment; i++){
179942 pCsr->apSegment[i]->pOffsetList = 0;
179943 pCsr->apSegment[i]->nOffsetList = 0;
179944 pCsr->apSegment[i]->iDocid = 0;
179945 }
179946
179947 return SQLITE_OK;
179948}
179949
179950static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, int nReq){
179951 if( nReq>pCsr->nBuffer ){
179952 char *aNew;
179953 pCsr->nBuffer = nReq*2;
179954 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
179955 if( !aNew ){
179956 return SQLITE_NOMEM;
179957 }
179958 pCsr->aBuffer = aNew;
179959 }
179960 return SQLITE_OK;
179961}
179962
179963
179964SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
179965 Fts3Table *p, /* Virtual table handle */
179966 Fts3MultiSegReader *pCsr /* Cursor object */
179967){
179968 int rc = SQLITE_OK;
179969
179970 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
179971 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
179972 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
179973 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
179974 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
179975 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
179976
179977 Fts3SegReader **apSegment = pCsr->apSegment;
179978 int nSegment = pCsr->nSegment;
179979 Fts3SegFilter *pFilter = pCsr->pFilter;
179980 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
179981 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
179982 );
179983
179984 if( pCsr->nSegment==0 ) return SQLITE_OK;
179985
179986 do {
179987 int nMerge;
179988 int i;
179989
179990 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
179991 ** forward. Then sort the list in order of current term again.
179992 */
179993 for(i=0; i<pCsr->nAdvance; i++){
179994 Fts3SegReader *pSeg = apSegment[i];
179995 if( pSeg->bLookup ){
179996 fts3SegReaderSetEof(pSeg);
179997 }else{
179998 rc = fts3SegReaderNext(p, pSeg, 0);
179999 }
180000 if( rc!=SQLITE_OK ) return rc;
180001 }
180002 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
180003 pCsr->nAdvance = 0;
180004
180005 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
180006 assert( rc==SQLITE_OK );
180007 if( apSegment[0]->aNode==0 ) break;
180008
180009 pCsr->nTerm = apSegment[0]->nTerm;
180010 pCsr->zTerm = apSegment[0]->zTerm;
180011
180012 /* If this is a prefix-search, and if the term that apSegment[0] points
180013 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
180014 ** required callbacks have been made. In this case exit early.
180015 **
180016 ** Similarly, if this is a search for an exact match, and the first term
180017 ** of segment apSegment[0] is not a match, exit early.
180018 */
180019 if( pFilter->zTerm && !isScan ){
180020 if( pCsr->nTerm<pFilter->nTerm
180021 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
180022 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
180023 ){
180024 break;
180025 }
180026 }
180027
180028 nMerge = 1;
180029 while( nMerge<nSegment
180030 && apSegment[nMerge]->aNode
180031 && apSegment[nMerge]->nTerm==pCsr->nTerm
180032 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
180033 ){
180034 nMerge++;
180035 }
180036
180037 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
180038 if( nMerge==1
180039 && !isIgnoreEmpty
180040 && !isFirst
180041 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
180042 ){
180043 pCsr->nDoclist = apSegment[0]->nDoclist;
180044 if( fts3SegReaderIsPending(apSegment[0]) ){
180045 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
180046 pCsr->aDoclist = pCsr->aBuffer;
180047 }else{
180048 pCsr->aDoclist = apSegment[0]->aDoclist;
180049 }
180050 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
180051 }else{
180052 int nDoclist = 0; /* Size of doclist */
180053 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
180054
180055 /* The current term of the first nMerge entries in the array
180056 ** of Fts3SegReader objects is the same. The doclists must be merged
180057 ** and a single term returned with the merged doclist.
180058 */
180059 for(i=0; i<nMerge; i++){
180060 fts3SegReaderFirstDocid(p, apSegment[i]);
180061 }
180062 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
180063 while( apSegment[0]->pOffsetList ){
180064 int j; /* Number of segments that share a docid */
180065 char *pList = 0;
180066 int nList = 0;
180067 int nByte;
180068 sqlite3_int64 iDocid = apSegment[0]->iDocid;
180069 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
180070 j = 1;
180071 while( j<nMerge
180072 && apSegment[j]->pOffsetList
180073 && apSegment[j]->iDocid==iDocid
180074 ){
180075 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
180076 j++;
180077 }
180078
180079 if( isColFilter ){
180080 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
180081 }
180082
180083 if( !isIgnoreEmpty || nList>0 ){
180084
180085 /* Calculate the 'docid' delta value to write into the merged
180086 ** doclist. */
180087 sqlite3_int64 iDelta;
180088 if( p->bDescIdx && nDoclist>0 ){
180089 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB;
180090 iDelta = (i64)((u64)iPrev - (u64)iDocid);
180091 }else{
180092 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB;
180093 iDelta = (i64)((u64)iDocid - (u64)iPrev);
180094 }
180095
180096 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
180097
180098 rc = fts3GrowSegReaderBuffer(pCsr, nByte+nDoclist);
180099 if( rc ) return rc;
180100
180101 if( isFirst ){
180102 char *a = &pCsr->aBuffer[nDoclist];
180103 int nWrite;
180104
180105 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
180106 if( nWrite ){
180107 iPrev = iDocid;
180108 nDoclist += nWrite;
180109 }
180110 }else{
180111 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
180112 iPrev = iDocid;
180113 if( isRequirePos ){
180114 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
180115 nDoclist += nList;
180116 pCsr->aBuffer[nDoclist++] = '\0';
180117 }
180118 }
180119 }
180120
180121 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
180122 }
180123 if( nDoclist>0 ){
180124 rc = fts3GrowSegReaderBuffer(pCsr, nDoclist+FTS3_NODE_PADDING);
180125 if( rc ) return rc;
180126 memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING);
180127 pCsr->aDoclist = pCsr->aBuffer;
180128 pCsr->nDoclist = nDoclist;
180129 rc = SQLITE_ROW;
180130 }
180131 }
180132 pCsr->nAdvance = nMerge;
180133 }while( rc==SQLITE_OK );
180134
180135 return rc;
180136}
180137
180138
180139SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
180140 Fts3MultiSegReader *pCsr /* Cursor object */
180141){
180142 if( pCsr ){
180143 int i;
180144 for(i=0; i<pCsr->nSegment; i++){
180145 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
180146 }
180147 sqlite3_free(pCsr->apSegment);
180148 sqlite3_free(pCsr->aBuffer);
180149
180150 pCsr->nSegment = 0;
180151 pCsr->apSegment = 0;
180152 pCsr->aBuffer = 0;
180153 }
180154}
180155
180156/*
180157** Decode the "end_block" field, selected by column iCol of the SELECT
180158** statement passed as the first argument.
180159**
180160** The "end_block" field may contain either an integer, or a text field
180161** containing the text representation of two non-negative integers separated
180162** by one or more space (0x20) characters. In the first case, set *piEndBlock
180163** to the integer value and *pnByte to zero before returning. In the second,
180164** set *piEndBlock to the first value and *pnByte to the second.
180165*/
180166static void fts3ReadEndBlockField(
180167 sqlite3_stmt *pStmt,
180168 int iCol,
180169 i64 *piEndBlock,
180170 i64 *pnByte
180171){
180172 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
180173 if( zText ){
180174 int i;
180175 int iMul = 1;
180176 u64 iVal = 0;
180177 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
180178 iVal = iVal*10 + (zText[i] - '0');
180179 }
180180 *piEndBlock = (i64)iVal;
180181 while( zText[i]==' ' ) i++;
180182 iVal = 0;
180183 if( zText[i]=='-' ){
180184 i++;
180185 iMul = -1;
180186 }
180187 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
180188 iVal = iVal*10 + (zText[i] - '0');
180189 }
180190 *pnByte = ((i64)iVal * (i64)iMul);
180191 }
180192}
180193
180194
180195/*
180196** A segment of size nByte bytes has just been written to absolute level
180197** iAbsLevel. Promote any segments that should be promoted as a result.
180198*/
180199static int fts3PromoteSegments(
180200 Fts3Table *p, /* FTS table handle */
180201 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
180202 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
180203){
180204 int rc = SQLITE_OK;
180205 sqlite3_stmt *pRange;
180206
180207 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
180208
180209 if( rc==SQLITE_OK ){
180210 int bOk = 0;
180211 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
180212 i64 nLimit = (nByte*3)/2;
180213
180214 /* Loop through all entries in the %_segdir table corresponding to
180215 ** segments in this index on levels greater than iAbsLevel. If there is
180216 ** at least one such segment, and it is possible to determine that all
180217 ** such segments are smaller than nLimit bytes in size, they will be
180218 ** promoted to level iAbsLevel. */
180219 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
180220 sqlite3_bind_int64(pRange, 2, iLast);
180221 while( SQLITE_ROW==sqlite3_step(pRange) ){
180222 i64 nSize = 0, dummy;
180223 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
180224 if( nSize<=0 || nSize>nLimit ){
180225 /* If nSize==0, then the %_segdir.end_block field does not not
180226 ** contain a size value. This happens if it was written by an
180227 ** old version of FTS. In this case it is not possible to determine
180228 ** the size of the segment, and so segment promotion does not
180229 ** take place. */
180230 bOk = 0;
180231 break;
180232 }
180233 bOk = 1;
180234 }
180235 rc = sqlite3_reset(pRange);
180236
180237 if( bOk ){
180238 int iIdx = 0;
180239 sqlite3_stmt *pUpdate1 = 0;
180240 sqlite3_stmt *pUpdate2 = 0;
180241
180242 if( rc==SQLITE_OK ){
180243 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
180244 }
180245 if( rc==SQLITE_OK ){
180246 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
180247 }
180248
180249 if( rc==SQLITE_OK ){
180250
180251 /* Loop through all %_segdir entries for segments in this index with
180252 ** levels equal to or greater than iAbsLevel. As each entry is visited,
180253 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
180254 ** oldest segment in the range, 1 for the next oldest, and so on.
180255 **
180256 ** In other words, move all segments being promoted to level -1,
180257 ** setting the "idx" fields as appropriate to keep them in the same
180258 ** order. The contents of level -1 (which is never used, except
180259 ** transiently here), will be moved back to level iAbsLevel below. */
180260 sqlite3_bind_int64(pRange, 1, iAbsLevel);
180261 while( SQLITE_ROW==sqlite3_step(pRange) ){
180262 sqlite3_bind_int(pUpdate1, 1, iIdx++);
180263 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
180264 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
180265 sqlite3_step(pUpdate1);
180266 rc = sqlite3_reset(pUpdate1);
180267 if( rc!=SQLITE_OK ){
180268 sqlite3_reset(pRange);
180269 break;
180270 }
180271 }
180272 }
180273 if( rc==SQLITE_OK ){
180274 rc = sqlite3_reset(pRange);
180275 }
180276
180277 /* Move level -1 to level iAbsLevel */
180278 if( rc==SQLITE_OK ){
180279 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
180280 sqlite3_step(pUpdate2);
180281 rc = sqlite3_reset(pUpdate2);
180282 }
180283 }
180284 }
180285
180286
180287 return rc;
180288}
180289
180290/*
180291** Merge all level iLevel segments in the database into a single
180292** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
180293** single segment with a level equal to the numerically largest level
180294** currently present in the database.
180295**
180296** If this function is called with iLevel<0, but there is only one
180297** segment in the database, SQLITE_DONE is returned immediately.
180298** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
180299** an SQLite error code is returned.
180300*/
180301static int fts3SegmentMerge(
180302 Fts3Table *p,
180303 int iLangid, /* Language id to merge */
180304 int iIndex, /* Index in p->aIndex[] to merge */
180305 int iLevel /* Level to merge */
180306){
180307 int rc; /* Return code */
180308 int iIdx = 0; /* Index of new segment */
180309 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
180310 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
180311 Fts3SegFilter filter; /* Segment term filter condition */
180312 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
180313 int bIgnoreEmpty = 0; /* True to ignore empty segments */
180314 i64 iMaxLevel = 0; /* Max level number for this index/langid */
180315
180316 assert( iLevel==FTS3_SEGCURSOR_ALL
180317 || iLevel==FTS3_SEGCURSOR_PENDING
180318 || iLevel>=0
180319 );
180320 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
180321 assert( iIndex>=0 && iIndex<p->nIndex );
180322
180323 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
180324 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
180325
180326 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
180327 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
180328 if( rc!=SQLITE_OK ) goto finished;
180329 }
180330
180331 if( iLevel==FTS3_SEGCURSOR_ALL ){
180332 /* This call is to merge all segments in the database to a single
180333 ** segment. The level of the new segment is equal to the numerically
180334 ** greatest segment level currently present in the database for this
180335 ** index. The idx of the new segment is always 0. */
180336 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
180337 rc = SQLITE_DONE;
180338 goto finished;
180339 }
180340 iNewLevel = iMaxLevel;
180341 bIgnoreEmpty = 1;
180342
180343 }else{
180344 /* This call is to merge all segments at level iLevel. find the next
180345 ** available segment index at level iLevel+1. The call to
180346 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
180347 ** a single iLevel+2 segment if necessary. */
180348 assert( FTS3_SEGCURSOR_PENDING==-1 );
180349 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
180350 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
180351 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
180352 }
180353 if( rc!=SQLITE_OK ) goto finished;
180354
180355 assert( csr.nSegment>0 );
180356 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
180357 assert_fts3_nc(
180358 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)
180359 );
180360
180361 memset(&filter, 0, sizeof(Fts3SegFilter));
180362 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
180363 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
180364
180365 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
180366 while( SQLITE_OK==rc ){
180367 rc = sqlite3Fts3SegReaderStep(p, &csr);
180368 if( rc!=SQLITE_ROW ) break;
180369 rc = fts3SegWriterAdd(p, &pWriter, 1,
180370 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
180371 }
180372 if( rc!=SQLITE_OK ) goto finished;
180373 assert_fts3_nc( pWriter || bIgnoreEmpty );
180374
180375 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
180376 rc = fts3DeleteSegdir(
180377 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
180378 );
180379 if( rc!=SQLITE_OK ) goto finished;
180380 }
180381 if( pWriter ){
180382 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
180383 if( rc==SQLITE_OK ){
180384 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
180385 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
180386 }
180387 }
180388 }
180389
180390 finished:
180391 fts3SegWriterFree(pWriter);
180392 sqlite3Fts3SegReaderFinish(&csr);
180393 return rc;
180394}
180395
180396
180397/*
180398** Flush the contents of pendingTerms to level 0 segments.
180399*/
180400SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
180401 int rc = SQLITE_OK;
180402 int i;
180403
180404 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
180405 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
180406 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
180407 }
180408 sqlite3Fts3PendingTermsClear(p);
180409
180410 /* Determine the auto-incr-merge setting if unknown. If enabled,
180411 ** estimate the number of leaf blocks of content to be written
180412 */
180413 if( rc==SQLITE_OK && p->bHasStat
180414 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
180415 ){
180416 sqlite3_stmt *pStmt = 0;
180417 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
180418 if( rc==SQLITE_OK ){
180419 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
180420 rc = sqlite3_step(pStmt);
180421 if( rc==SQLITE_ROW ){
180422 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
180423 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
180424 }else if( rc==SQLITE_DONE ){
180425 p->nAutoincrmerge = 0;
180426 }
180427 rc = sqlite3_reset(pStmt);
180428 }
180429 }
180430 return rc;
180431}
180432
180433/*
180434** Encode N integers as varints into a blob.
180435*/
180436static void fts3EncodeIntArray(
180437 int N, /* The number of integers to encode */
180438 u32 *a, /* The integer values */
180439 char *zBuf, /* Write the BLOB here */
180440 int *pNBuf /* Write number of bytes if zBuf[] used here */
180441){
180442 int i, j;
180443 for(i=j=0; i<N; i++){
180444 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
180445 }
180446 *pNBuf = j;
180447}
180448
180449/*
180450** Decode a blob of varints into N integers
180451*/
180452static void fts3DecodeIntArray(
180453 int N, /* The number of integers to decode */
180454 u32 *a, /* Write the integer values */
180455 const char *zBuf, /* The BLOB containing the varints */
180456 int nBuf /* size of the BLOB */
180457){
180458 int i = 0;
180459 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
180460 int j;
180461 for(i=j=0; i<N && j<nBuf; i++){
180462 sqlite3_int64 x;
180463 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
180464 a[i] = (u32)(x & 0xffffffff);
180465 }
180466 }
180467 while( i<N ) a[i++] = 0;
180468}
180469
180470/*
180471** Insert the sizes (in tokens) for each column of the document
180472** with docid equal to p->iPrevDocid. The sizes are encoded as
180473** a blob of varints.
180474*/
180475static void fts3InsertDocsize(
180476 int *pRC, /* Result code */
180477 Fts3Table *p, /* Table into which to insert */
180478 u32 *aSz /* Sizes of each column, in tokens */
180479){
180480 char *pBlob; /* The BLOB encoding of the document size */
180481 int nBlob; /* Number of bytes in the BLOB */
180482 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
180483 int rc; /* Result code from subfunctions */
180484
180485 if( *pRC ) return;
180486 pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
180487 if( pBlob==0 ){
180488 *pRC = SQLITE_NOMEM;
180489 return;
180490 }
180491 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
180492 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
180493 if( rc ){
180494 sqlite3_free(pBlob);
180495 *pRC = rc;
180496 return;
180497 }
180498 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
180499 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
180500 sqlite3_step(pStmt);
180501 *pRC = sqlite3_reset(pStmt);
180502}
180503
180504/*
180505** Record 0 of the %_stat table contains a blob consisting of N varints,
180506** where N is the number of user defined columns in the fts3 table plus
180507** two. If nCol is the number of user defined columns, then values of the
180508** varints are set as follows:
180509**
180510** Varint 0: Total number of rows in the table.
180511**
180512** Varint 1..nCol: For each column, the total number of tokens stored in
180513** the column for all rows of the table.
180514**
180515** Varint 1+nCol: The total size, in bytes, of all text values in all
180516** columns of all rows of the table.
180517**
180518*/
180519static void fts3UpdateDocTotals(
180520 int *pRC, /* The result code */
180521 Fts3Table *p, /* Table being updated */
180522 u32 *aSzIns, /* Size increases */
180523 u32 *aSzDel, /* Size decreases */
180524 int nChng /* Change in the number of documents */
180525){
180526 char *pBlob; /* Storage for BLOB written into %_stat */
180527 int nBlob; /* Size of BLOB written into %_stat */
180528 u32 *a; /* Array of integers that becomes the BLOB */
180529 sqlite3_stmt *pStmt; /* Statement for reading and writing */
180530 int i; /* Loop counter */
180531 int rc; /* Result code from subfunctions */
180532
180533 const int nStat = p->nColumn+2;
180534
180535 if( *pRC ) return;
180536 a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
180537 if( a==0 ){
180538 *pRC = SQLITE_NOMEM;
180539 return;
180540 }
180541 pBlob = (char*)&a[nStat];
180542 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
180543 if( rc ){
180544 sqlite3_free(a);
180545 *pRC = rc;
180546 return;
180547 }
180548 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
180549 if( sqlite3_step(pStmt)==SQLITE_ROW ){
180550 fts3DecodeIntArray(nStat, a,
180551 sqlite3_column_blob(pStmt, 0),
180552 sqlite3_column_bytes(pStmt, 0));
180553 }else{
180554 memset(a, 0, sizeof(u32)*(nStat) );
180555 }
180556 rc = sqlite3_reset(pStmt);
180557 if( rc!=SQLITE_OK ){
180558 sqlite3_free(a);
180559 *pRC = rc;
180560 return;
180561 }
180562 if( nChng<0 && a[0]<(u32)(-nChng) ){
180563 a[0] = 0;
180564 }else{
180565 a[0] += nChng;
180566 }
180567 for(i=0; i<p->nColumn+1; i++){
180568 u32 x = a[i+1];
180569 if( x+aSzIns[i] < aSzDel[i] ){
180570 x = 0;
180571 }else{
180572 x = x + aSzIns[i] - aSzDel[i];
180573 }
180574 a[i+1] = x;
180575 }
180576 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
180577 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
180578 if( rc ){
180579 sqlite3_free(a);
180580 *pRC = rc;
180581 return;
180582 }
180583 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
180584 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
180585 sqlite3_step(pStmt);
180586 *pRC = sqlite3_reset(pStmt);
180587 sqlite3_bind_null(pStmt, 2);
180588 sqlite3_free(a);
180589}
180590
180591/*
180592** Merge the entire database so that there is one segment for each
180593** iIndex/iLangid combination.
180594*/
180595static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
180596 int bSeenDone = 0;
180597 int rc;
180598 sqlite3_stmt *pAllLangid = 0;
180599
180600 rc = sqlite3Fts3PendingTermsFlush(p);
180601 if( rc==SQLITE_OK ){
180602 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
180603 }
180604 if( rc==SQLITE_OK ){
180605 int rc2;
180606 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
180607 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
180608 while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
180609 int i;
180610 int iLangid = sqlite3_column_int(pAllLangid, 0);
180611 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
180612 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
180613 if( rc==SQLITE_DONE ){
180614 bSeenDone = 1;
180615 rc = SQLITE_OK;
180616 }
180617 }
180618 }
180619 rc2 = sqlite3_reset(pAllLangid);
180620 if( rc==SQLITE_OK ) rc = rc2;
180621 }
180622
180623 sqlite3Fts3SegmentsClose(p);
180624
180625 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
180626}
180627
180628/*
180629** This function is called when the user executes the following statement:
180630**
180631** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
180632**
180633** The entire FTS index is discarded and rebuilt. If the table is one
180634** created using the content=xxx option, then the new index is based on
180635** the current contents of the xxx table. Otherwise, it is rebuilt based
180636** on the contents of the %_content table.
180637*/
180638static int fts3DoRebuild(Fts3Table *p){
180639 int rc; /* Return Code */
180640
180641 rc = fts3DeleteAll(p, 0);
180642 if( rc==SQLITE_OK ){
180643 u32 *aSz = 0;
180644 u32 *aSzIns = 0;
180645 u32 *aSzDel = 0;
180646 sqlite3_stmt *pStmt = 0;
180647 int nEntry = 0;
180648
180649 /* Compose and prepare an SQL statement to loop through the content table */
180650 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
180651 if( !zSql ){
180652 rc = SQLITE_NOMEM;
180653 }else{
180654 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
180655 sqlite3_free(zSql);
180656 }
180657
180658 if( rc==SQLITE_OK ){
180659 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
180660 aSz = (u32 *)sqlite3_malloc64(nByte);
180661 if( aSz==0 ){
180662 rc = SQLITE_NOMEM;
180663 }else{
180664 memset(aSz, 0, nByte);
180665 aSzIns = &aSz[p->nColumn+1];
180666 aSzDel = &aSzIns[p->nColumn+1];
180667 }
180668 }
180669
180670 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
180671 int iCol;
180672 int iLangid = langidFromSelect(p, pStmt);
180673 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
180674 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
180675 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
180676 if( p->abNotindexed[iCol]==0 ){
180677 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
180678 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
180679 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
180680 }
180681 }
180682 if( p->bHasDocsize ){
180683 fts3InsertDocsize(&rc, p, aSz);
180684 }
180685 if( rc!=SQLITE_OK ){
180686 sqlite3_finalize(pStmt);
180687 pStmt = 0;
180688 }else{
180689 nEntry++;
180690 for(iCol=0; iCol<=p->nColumn; iCol++){
180691 aSzIns[iCol] += aSz[iCol];
180692 }
180693 }
180694 }
180695 if( p->bFts4 ){
180696 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
180697 }
180698 sqlite3_free(aSz);
180699
180700 if( pStmt ){
180701 int rc2 = sqlite3_finalize(pStmt);
180702 if( rc==SQLITE_OK ){
180703 rc = rc2;
180704 }
180705 }
180706 }
180707
180708 return rc;
180709}
180710
180711
180712/*
180713** This function opens a cursor used to read the input data for an
180714** incremental merge operation. Specifically, it opens a cursor to scan
180715** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
180716** level iAbsLevel.
180717*/
180718static int fts3IncrmergeCsr(
180719 Fts3Table *p, /* FTS3 table handle */
180720 sqlite3_int64 iAbsLevel, /* Absolute level to open */
180721 int nSeg, /* Number of segments to merge */
180722 Fts3MultiSegReader *pCsr /* Cursor object to populate */
180723){
180724 int rc; /* Return Code */
180725 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
180726 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
180727
180728 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
180729 memset(pCsr, 0, sizeof(*pCsr));
180730 nByte = sizeof(Fts3SegReader *) * nSeg;
180731 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
180732
180733 if( pCsr->apSegment==0 ){
180734 rc = SQLITE_NOMEM;
180735 }else{
180736 memset(pCsr->apSegment, 0, nByte);
180737 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
180738 }
180739 if( rc==SQLITE_OK ){
180740 int i;
180741 int rc2;
180742 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
180743 assert( pCsr->nSegment==0 );
180744 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
180745 rc = sqlite3Fts3SegReaderNew(i, 0,
180746 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
180747 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
180748 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
180749 sqlite3_column_blob(pStmt, 4), /* segdir.root */
180750 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
180751 &pCsr->apSegment[i]
180752 );
180753 pCsr->nSegment++;
180754 }
180755 rc2 = sqlite3_reset(pStmt);
180756 if( rc==SQLITE_OK ) rc = rc2;
180757 }
180758
180759 return rc;
180760}
180761
180762typedef struct IncrmergeWriter IncrmergeWriter;
180763typedef struct NodeWriter NodeWriter;
180764typedef struct Blob Blob;
180765typedef struct NodeReader NodeReader;
180766
180767/*
180768** An instance of the following structure is used as a dynamic buffer
180769** to build up nodes or other blobs of data in.
180770**
180771** The function blobGrowBuffer() is used to extend the allocation.
180772*/
180773struct Blob {
180774 char *a; /* Pointer to allocation */
180775 int n; /* Number of valid bytes of data in a[] */
180776 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
180777};
180778
180779/*
180780** This structure is used to build up buffers containing segment b-tree
180781** nodes (blocks).
180782*/
180783struct NodeWriter {
180784 sqlite3_int64 iBlock; /* Current block id */
180785 Blob key; /* Last key written to the current block */
180786 Blob block; /* Current block image */
180787};
180788
180789/*
180790** An object of this type contains the state required to create or append
180791** to an appendable b-tree segment.
180792*/
180793struct IncrmergeWriter {
180794 int nLeafEst; /* Space allocated for leaf blocks */
180795 int nWork; /* Number of leaf pages flushed */
180796 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
180797 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
180798 sqlite3_int64 iStart; /* Block number of first allocated block */
180799 sqlite3_int64 iEnd; /* Block number of last allocated block */
180800 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
180801 u8 bNoLeafData; /* If true, store 0 for segment size */
180802 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
180803};
180804
180805/*
180806** An object of the following type is used to read data from a single
180807** FTS segment node. See the following functions:
180808**
180809** nodeReaderInit()
180810** nodeReaderNext()
180811** nodeReaderRelease()
180812*/
180813struct NodeReader {
180814 const char *aNode;
180815 int nNode;
180816 int iOff; /* Current offset within aNode[] */
180817
180818 /* Output variables. Containing the current node entry. */
180819 sqlite3_int64 iChild; /* Pointer to child node */
180820 Blob term; /* Current term */
180821 const char *aDoclist; /* Pointer to doclist */
180822 int nDoclist; /* Size of doclist in bytes */
180823};
180824
180825/*
180826** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
180827** Otherwise, if the allocation at pBlob->a is not already at least nMin
180828** bytes in size, extend (realloc) it to be so.
180829**
180830** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
180831** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
180832** to reflect the new size of the pBlob->a[] buffer.
180833*/
180834static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
180835 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
180836 int nAlloc = nMin;
180837 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
180838 if( a ){
180839 pBlob->nAlloc = nAlloc;
180840 pBlob->a = a;
180841 }else{
180842 *pRc = SQLITE_NOMEM;
180843 }
180844 }
180845}
180846
180847/*
180848** Attempt to advance the node-reader object passed as the first argument to
180849** the next entry on the node.
180850**
180851** Return an error code if an error occurs (SQLITE_NOMEM is possible).
180852** Otherwise return SQLITE_OK. If there is no next entry on the node
180853** (e.g. because the current entry is the last) set NodeReader->aNode to
180854** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
180855** variables for the new entry.
180856*/
180857static int nodeReaderNext(NodeReader *p){
180858 int bFirst = (p->term.n==0); /* True for first term on the node */
180859 int nPrefix = 0; /* Bytes to copy from previous term */
180860 int nSuffix = 0; /* Bytes to append to the prefix */
180861 int rc = SQLITE_OK; /* Return code */
180862
180863 assert( p->aNode );
180864 if( p->iChild && bFirst==0 ) p->iChild++;
180865 if( p->iOff>=p->nNode ){
180866 /* EOF */
180867 p->aNode = 0;
180868 }else{
180869 if( bFirst==0 ){
180870 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
180871 }
180872 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
180873
180874 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
180875 return FTS_CORRUPT_VTAB;
180876 }
180877 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
180878 if( rc==SQLITE_OK ){
180879 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
180880 p->term.n = nPrefix+nSuffix;
180881 p->iOff += nSuffix;
180882 if( p->iChild==0 ){
180883 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
180884 if( (p->nNode-p->iOff)<p->nDoclist ){
180885 return FTS_CORRUPT_VTAB;
180886 }
180887 p->aDoclist = &p->aNode[p->iOff];
180888 p->iOff += p->nDoclist;
180889 }
180890 }
180891 }
180892
180893 assert_fts3_nc( p->iOff<=p->nNode );
180894 return rc;
180895}
180896
180897/*
180898** Release all dynamic resources held by node-reader object *p.
180899*/
180900static void nodeReaderRelease(NodeReader *p){
180901 sqlite3_free(p->term.a);
180902}
180903
180904/*
180905** Initialize a node-reader object to read the node in buffer aNode/nNode.
180906**
180907** If successful, SQLITE_OK is returned and the NodeReader object set to
180908** point to the first entry on the node (if any). Otherwise, an SQLite
180909** error code is returned.
180910*/
180911static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
180912 memset(p, 0, sizeof(NodeReader));
180913 p->aNode = aNode;
180914 p->nNode = nNode;
180915
180916 /* Figure out if this is a leaf or an internal node. */
180917 if( aNode && aNode[0] ){
180918 /* An internal node. */
180919 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
180920 }else{
180921 p->iOff = 1;
180922 }
180923
180924 return aNode ? nodeReaderNext(p) : SQLITE_OK;
180925}
180926
180927/*
180928** This function is called while writing an FTS segment each time a leaf o
180929** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
180930** to be greater than the largest key on the node just written, but smaller
180931** than or equal to the first key that will be written to the next leaf
180932** node.
180933**
180934** The block id of the leaf node just written to disk may be found in
180935** (pWriter->aNodeWriter[0].iBlock) when this function is called.
180936*/
180937static int fts3IncrmergePush(
180938 Fts3Table *p, /* Fts3 table handle */
180939 IncrmergeWriter *pWriter, /* Writer object */
180940 const char *zTerm, /* Term to write to internal node */
180941 int nTerm /* Bytes at zTerm */
180942){
180943 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
180944 int iLayer;
180945
180946 assert( nTerm>0 );
180947 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
180948 sqlite3_int64 iNextPtr = 0;
180949 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
180950 int rc = SQLITE_OK;
180951 int nPrefix;
180952 int nSuffix;
180953 int nSpace;
180954
180955 /* Figure out how much space the key will consume if it is written to
180956 ** the current node of layer iLayer. Due to the prefix compression,
180957 ** the space required changes depending on which node the key is to
180958 ** be added to. */
180959 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
180960 nSuffix = nTerm - nPrefix;
180961 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB;
180962 nSpace = sqlite3Fts3VarintLen(nPrefix);
180963 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
180964
180965 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
180966 /* If the current node of layer iLayer contains zero keys, or if adding
180967 ** the key to it will not cause it to grow to larger than nNodeSize
180968 ** bytes in size, write the key here. */
180969
180970 Blob *pBlk = &pNode->block;
180971 if( pBlk->n==0 ){
180972 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
180973 if( rc==SQLITE_OK ){
180974 pBlk->a[0] = (char)iLayer;
180975 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
180976 }
180977 }
180978 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
180979 blobGrowBuffer(&pNode->key, nTerm, &rc);
180980
180981 if( rc==SQLITE_OK ){
180982 if( pNode->key.n ){
180983 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
180984 }
180985 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
180986 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
180987 pBlk->n += nSuffix;
180988
180989 memcpy(pNode->key.a, zTerm, nTerm);
180990 pNode->key.n = nTerm;
180991 }
180992 }else{
180993 /* Otherwise, flush the current node of layer iLayer to disk.
180994 ** Then allocate a new, empty sibling node. The key will be written
180995 ** into the parent of this node. */
180996 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
180997
180998 assert( pNode->block.nAlloc>=p->nNodeSize );
180999 pNode->block.a[0] = (char)iLayer;
181000 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
181001
181002 iNextPtr = pNode->iBlock;
181003 pNode->iBlock++;
181004 pNode->key.n = 0;
181005 }
181006
181007 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
181008 iPtr = iNextPtr;
181009 }
181010
181011 assert( 0 );
181012 return 0;
181013}
181014
181015/*
181016** Append a term and (optionally) doclist to the FTS segment node currently
181017** stored in blob *pNode. The node need not contain any terms, but the
181018** header must be written before this function is called.
181019**
181020** A node header is a single 0x00 byte for a leaf node, or a height varint
181021** followed by the left-hand-child varint for an internal node.
181022**
181023** The term to be appended is passed via arguments zTerm/nTerm. For a
181024** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
181025** node, both aDoclist and nDoclist must be passed 0.
181026**
181027** If the size of the value in blob pPrev is zero, then this is the first
181028** term written to the node. Otherwise, pPrev contains a copy of the
181029** previous term. Before this function returns, it is updated to contain a
181030** copy of zTerm/nTerm.
181031**
181032** It is assumed that the buffer associated with pNode is already large
181033** enough to accommodate the new entry. The buffer associated with pPrev
181034** is extended by this function if requrired.
181035**
181036** If an error (i.e. OOM condition) occurs, an SQLite error code is
181037** returned. Otherwise, SQLITE_OK.
181038*/
181039static int fts3AppendToNode(
181040 Blob *pNode, /* Current node image to append to */
181041 Blob *pPrev, /* Buffer containing previous term written */
181042 const char *zTerm, /* New term to write */
181043 int nTerm, /* Size of zTerm in bytes */
181044 const char *aDoclist, /* Doclist (or NULL) to write */
181045 int nDoclist /* Size of aDoclist in bytes */
181046){
181047 int rc = SQLITE_OK; /* Return code */
181048 int bFirst = (pPrev->n==0); /* True if this is the first term written */
181049 int nPrefix; /* Size of term prefix in bytes */
181050 int nSuffix; /* Size of term suffix in bytes */
181051
181052 /* Node must have already been started. There must be a doclist for a
181053 ** leaf node, and there must not be a doclist for an internal node. */
181054 assert( pNode->n>0 );
181055 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
181056
181057 blobGrowBuffer(pPrev, nTerm, &rc);
181058 if( rc!=SQLITE_OK ) return rc;
181059
181060 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
181061 nSuffix = nTerm - nPrefix;
181062 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
181063 memcpy(pPrev->a, zTerm, nTerm);
181064 pPrev->n = nTerm;
181065
181066 if( bFirst==0 ){
181067 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
181068 }
181069 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
181070 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
181071 pNode->n += nSuffix;
181072
181073 if( aDoclist ){
181074 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
181075 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
181076 pNode->n += nDoclist;
181077 }
181078
181079 assert( pNode->n<=pNode->nAlloc );
181080
181081 return SQLITE_OK;
181082}
181083
181084/*
181085** Append the current term and doclist pointed to by cursor pCsr to the
181086** appendable b-tree segment opened for writing by pWriter.
181087**
181088** Return SQLITE_OK if successful, or an SQLite error code otherwise.
181089*/
181090static int fts3IncrmergeAppend(
181091 Fts3Table *p, /* Fts3 table handle */
181092 IncrmergeWriter *pWriter, /* Writer object */
181093 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
181094){
181095 const char *zTerm = pCsr->zTerm;
181096 int nTerm = pCsr->nTerm;
181097 const char *aDoclist = pCsr->aDoclist;
181098 int nDoclist = pCsr->nDoclist;
181099 int rc = SQLITE_OK; /* Return code */
181100 int nSpace; /* Total space in bytes required on leaf */
181101 int nPrefix; /* Size of prefix shared with previous term */
181102 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
181103 NodeWriter *pLeaf; /* Object used to write leaf nodes */
181104
181105 pLeaf = &pWriter->aNodeWriter[0];
181106 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
181107 nSuffix = nTerm - nPrefix;
181108
181109 nSpace = sqlite3Fts3VarintLen(nPrefix);
181110 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
181111 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
181112
181113 /* If the current block is not empty, and if adding this term/doclist
181114 ** to the current block would make it larger than Fts3Table.nNodeSize
181115 ** bytes, write this block out to the database. */
181116 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
181117 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
181118 pWriter->nWork++;
181119
181120 /* Add the current term to the parent node. The term added to the
181121 ** parent must:
181122 **
181123 ** a) be greater than the largest term on the leaf node just written
181124 ** to the database (still available in pLeaf->key), and
181125 **
181126 ** b) be less than or equal to the term about to be added to the new
181127 ** leaf node (zTerm/nTerm).
181128 **
181129 ** In other words, it must be the prefix of zTerm 1 byte longer than
181130 ** the common prefix (if any) of zTerm and pWriter->zTerm.
181131 */
181132 if( rc==SQLITE_OK ){
181133 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
181134 }
181135
181136 /* Advance to the next output block */
181137 pLeaf->iBlock++;
181138 pLeaf->key.n = 0;
181139 pLeaf->block.n = 0;
181140
181141 nSuffix = nTerm;
181142 nSpace = 1;
181143 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
181144 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
181145 }
181146
181147 pWriter->nLeafData += nSpace;
181148 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
181149 if( rc==SQLITE_OK ){
181150 if( pLeaf->block.n==0 ){
181151 pLeaf->block.n = 1;
181152 pLeaf->block.a[0] = '\0';
181153 }
181154 rc = fts3AppendToNode(
181155 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
181156 );
181157 }
181158
181159 return rc;
181160}
181161
181162/*
181163** This function is called to release all dynamic resources held by the
181164** merge-writer object pWriter, and if no error has occurred, to flush
181165** all outstanding node buffers held by pWriter to disk.
181166**
181167** If *pRc is not SQLITE_OK when this function is called, then no attempt
181168** is made to write any data to disk. Instead, this function serves only
181169** to release outstanding resources.
181170**
181171** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
181172** flushing buffers to disk, *pRc is set to an SQLite error code before
181173** returning.
181174*/
181175static void fts3IncrmergeRelease(
181176 Fts3Table *p, /* FTS3 table handle */
181177 IncrmergeWriter *pWriter, /* Merge-writer object */
181178 int *pRc /* IN/OUT: Error code */
181179){
181180 int i; /* Used to iterate through non-root layers */
181181 int iRoot; /* Index of root in pWriter->aNodeWriter */
181182 NodeWriter *pRoot; /* NodeWriter for root node */
181183 int rc = *pRc; /* Error code */
181184
181185 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
181186 ** root node. If the segment fits entirely on a single leaf node, iRoot
181187 ** will be set to 0. If the root node is the parent of the leaves, iRoot
181188 ** will be 1. And so on. */
181189 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
181190 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
181191 if( pNode->block.n>0 ) break;
181192 assert( *pRc || pNode->block.nAlloc==0 );
181193 assert( *pRc || pNode->key.nAlloc==0 );
181194 sqlite3_free(pNode->block.a);
181195 sqlite3_free(pNode->key.a);
181196 }
181197
181198 /* Empty output segment. This is a no-op. */
181199 if( iRoot<0 ) return;
181200
181201 /* The entire output segment fits on a single node. Normally, this means
181202 ** the node would be stored as a blob in the "root" column of the %_segdir
181203 ** table. However, this is not permitted in this case. The problem is that
181204 ** space has already been reserved in the %_segments table, and so the
181205 ** start_block and end_block fields of the %_segdir table must be populated.
181206 ** And, by design or by accident, released versions of FTS cannot handle
181207 ** segments that fit entirely on the root node with start_block!=0.
181208 **
181209 ** Instead, create a synthetic root node that contains nothing but a
181210 ** pointer to the single content node. So that the segment consists of a
181211 ** single leaf and a single interior (root) node.
181212 **
181213 ** Todo: Better might be to defer allocating space in the %_segments
181214 ** table until we are sure it is needed.
181215 */
181216 if( iRoot==0 ){
181217 Blob *pBlock = &pWriter->aNodeWriter[1].block;
181218 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
181219 if( rc==SQLITE_OK ){
181220 pBlock->a[0] = 0x01;
181221 pBlock->n = 1 + sqlite3Fts3PutVarint(
181222 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
181223 );
181224 }
181225 iRoot = 1;
181226 }
181227 pRoot = &pWriter->aNodeWriter[iRoot];
181228
181229 /* Flush all currently outstanding nodes to disk. */
181230 for(i=0; i<iRoot; i++){
181231 NodeWriter *pNode = &pWriter->aNodeWriter[i];
181232 if( pNode->block.n>0 && rc==SQLITE_OK ){
181233 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
181234 }
181235 sqlite3_free(pNode->block.a);
181236 sqlite3_free(pNode->key.a);
181237 }
181238
181239 /* Write the %_segdir record. */
181240 if( rc==SQLITE_OK ){
181241 rc = fts3WriteSegdir(p,
181242 pWriter->iAbsLevel+1, /* level */
181243 pWriter->iIdx, /* idx */
181244 pWriter->iStart, /* start_block */
181245 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
181246 pWriter->iEnd, /* end_block */
181247 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
181248 pRoot->block.a, pRoot->block.n /* root */
181249 );
181250 }
181251 sqlite3_free(pRoot->block.a);
181252 sqlite3_free(pRoot->key.a);
181253
181254 *pRc = rc;
181255}
181256
181257/*
181258** Compare the term in buffer zLhs (size in bytes nLhs) with that in
181259** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
181260** the other, it is considered to be smaller than the other.
181261**
181262** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
181263** if it is greater.
181264*/
181265static int fts3TermCmp(
181266 const char *zLhs, int nLhs, /* LHS of comparison */
181267 const char *zRhs, int nRhs /* RHS of comparison */
181268){
181269 int nCmp = MIN(nLhs, nRhs);
181270 int res;
181271
181272 res = (nCmp ? memcmp(zLhs, zRhs, nCmp) : 0);
181273 if( res==0 ) res = nLhs - nRhs;
181274
181275 return res;
181276}
181277
181278
181279/*
181280** Query to see if the entry in the %_segments table with blockid iEnd is
181281** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
181282** returning. Otherwise, set *pbRes to 0.
181283**
181284** Or, if an error occurs while querying the database, return an SQLite
181285** error code. The final value of *pbRes is undefined in this case.
181286**
181287** This is used to test if a segment is an "appendable" segment. If it
181288** is, then a NULL entry has been inserted into the %_segments table
181289** with blockid %_segdir.end_block.
181290*/
181291static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
181292 int bRes = 0; /* Result to set *pbRes to */
181293 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
181294 int rc; /* Return code */
181295
181296 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
181297 if( rc==SQLITE_OK ){
181298 sqlite3_bind_int64(pCheck, 1, iEnd);
181299 if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
181300 rc = sqlite3_reset(pCheck);
181301 }
181302
181303 *pbRes = bRes;
181304 return rc;
181305}
181306
181307/*
181308** This function is called when initializing an incremental-merge operation.
181309** It checks if the existing segment with index value iIdx at absolute level
181310** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
181311** merge-writer object *pWriter is initialized to write to it.
181312**
181313** An existing segment can be appended to by an incremental merge if:
181314**
181315** * It was initially created as an appendable segment (with all required
181316** space pre-allocated), and
181317**
181318** * The first key read from the input (arguments zKey and nKey) is
181319** greater than the largest key currently stored in the potential
181320** output segment.
181321*/
181322static int fts3IncrmergeLoad(
181323 Fts3Table *p, /* Fts3 table handle */
181324 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
181325 int iIdx, /* Index of candidate output segment */
181326 const char *zKey, /* First key to write */
181327 int nKey, /* Number of bytes in nKey */
181328 IncrmergeWriter *pWriter /* Populate this object */
181329){
181330 int rc; /* Return code */
181331 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
181332
181333 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
181334 if( rc==SQLITE_OK ){
181335 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
181336 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
181337 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
181338 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
181339 int nRoot = 0; /* Size of aRoot[] in bytes */
181340 int rc2; /* Return code from sqlite3_reset() */
181341 int bAppendable = 0; /* Set to true if segment is appendable */
181342
181343 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
181344 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
181345 sqlite3_bind_int(pSelect, 2, iIdx);
181346 if( sqlite3_step(pSelect)==SQLITE_ROW ){
181347 iStart = sqlite3_column_int64(pSelect, 1);
181348 iLeafEnd = sqlite3_column_int64(pSelect, 2);
181349 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
181350 if( pWriter->nLeafData<0 ){
181351 pWriter->nLeafData = pWriter->nLeafData * -1;
181352 }
181353 pWriter->bNoLeafData = (pWriter->nLeafData==0);
181354 nRoot = sqlite3_column_bytes(pSelect, 4);
181355 aRoot = sqlite3_column_blob(pSelect, 4);
181356 if( aRoot==0 ){
181357 sqlite3_reset(pSelect);
181358 return nRoot ? SQLITE_NOMEM : FTS_CORRUPT_VTAB;
181359 }
181360 }else{
181361 return sqlite3_reset(pSelect);
181362 }
181363
181364 /* Check for the zero-length marker in the %_segments table */
181365 rc = fts3IsAppendable(p, iEnd, &bAppendable);
181366
181367 /* Check that zKey/nKey is larger than the largest key the candidate */
181368 if( rc==SQLITE_OK && bAppendable ){
181369 char *aLeaf = 0;
181370 int nLeaf = 0;
181371
181372 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
181373 if( rc==SQLITE_OK ){
181374 NodeReader reader;
181375 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
181376 rc==SQLITE_OK && reader.aNode;
181377 rc = nodeReaderNext(&reader)
181378 ){
181379 assert( reader.aNode );
181380 }
181381 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
181382 bAppendable = 0;
181383 }
181384 nodeReaderRelease(&reader);
181385 }
181386 sqlite3_free(aLeaf);
181387 }
181388
181389 if( rc==SQLITE_OK && bAppendable ){
181390 /* It is possible to append to this segment. Set up the IncrmergeWriter
181391 ** object to do so. */
181392 int i;
181393 int nHeight = (int)aRoot[0];
181394 NodeWriter *pNode;
181395 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT ){
181396 sqlite3_reset(pSelect);
181397 return FTS_CORRUPT_VTAB;
181398 }
181399
181400 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
181401 pWriter->iStart = iStart;
181402 pWriter->iEnd = iEnd;
181403 pWriter->iAbsLevel = iAbsLevel;
181404 pWriter->iIdx = iIdx;
181405
181406 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
181407 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
181408 }
181409
181410 pNode = &pWriter->aNodeWriter[nHeight];
181411 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
181412 blobGrowBuffer(&pNode->block,
181413 MAX(nRoot, p->nNodeSize)+FTS3_NODE_PADDING, &rc
181414 );
181415 if( rc==SQLITE_OK ){
181416 memcpy(pNode->block.a, aRoot, nRoot);
181417 pNode->block.n = nRoot;
181418 memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING);
181419 }
181420
181421 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
181422 NodeReader reader;
181423 pNode = &pWriter->aNodeWriter[i];
181424
181425 if( pNode->block.a){
181426 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
181427 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
181428 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
181429 if( rc==SQLITE_OK ){
181430 memcpy(pNode->key.a, reader.term.a, reader.term.n);
181431 pNode->key.n = reader.term.n;
181432 if( i>0 ){
181433 char *aBlock = 0;
181434 int nBlock = 0;
181435 pNode = &pWriter->aNodeWriter[i-1];
181436 pNode->iBlock = reader.iChild;
181437 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
181438 blobGrowBuffer(&pNode->block,
181439 MAX(nBlock, p->nNodeSize)+FTS3_NODE_PADDING, &rc
181440 );
181441 if( rc==SQLITE_OK ){
181442 memcpy(pNode->block.a, aBlock, nBlock);
181443 pNode->block.n = nBlock;
181444 memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING);
181445 }
181446 sqlite3_free(aBlock);
181447 }
181448 }
181449 }
181450 nodeReaderRelease(&reader);
181451 }
181452 }
181453
181454 rc2 = sqlite3_reset(pSelect);
181455 if( rc==SQLITE_OK ) rc = rc2;
181456 }
181457
181458 return rc;
181459}
181460
181461/*
181462** Determine the largest segment index value that exists within absolute
181463** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
181464** one before returning SQLITE_OK. Or, if there are no segments at all
181465** within level iAbsLevel, set *piIdx to zero.
181466**
181467** If an error occurs, return an SQLite error code. The final value of
181468** *piIdx is undefined in this case.
181469*/
181470static int fts3IncrmergeOutputIdx(
181471 Fts3Table *p, /* FTS Table handle */
181472 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
181473 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
181474){
181475 int rc;
181476 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
181477
181478 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
181479 if( rc==SQLITE_OK ){
181480 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
181481 sqlite3_step(pOutputIdx);
181482 *piIdx = sqlite3_column_int(pOutputIdx, 0);
181483 rc = sqlite3_reset(pOutputIdx);
181484 }
181485
181486 return rc;
181487}
181488
181489/*
181490** Allocate an appendable output segment on absolute level iAbsLevel+1
181491** with idx value iIdx.
181492**
181493** In the %_segdir table, a segment is defined by the values in three
181494** columns:
181495**
181496** start_block
181497** leaves_end_block
181498** end_block
181499**
181500** When an appendable segment is allocated, it is estimated that the
181501** maximum number of leaf blocks that may be required is the sum of the
181502** number of leaf blocks consumed by the input segments, plus the number
181503** of input segments, multiplied by two. This value is stored in stack
181504** variable nLeafEst.
181505**
181506** A total of 16*nLeafEst blocks are allocated when an appendable segment
181507** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
181508** array of leaf nodes starts at the first block allocated. The array
181509** of interior nodes that are parents of the leaf nodes start at block
181510** (start_block + (1 + end_block - start_block) / 16). And so on.
181511**
181512** In the actual code below, the value "16" is replaced with the
181513** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
181514*/
181515static int fts3IncrmergeWriter(
181516 Fts3Table *p, /* Fts3 table handle */
181517 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
181518 int iIdx, /* Index of new output segment */
181519 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
181520 IncrmergeWriter *pWriter /* Populate this object */
181521){
181522 int rc; /* Return Code */
181523 int i; /* Iterator variable */
181524 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
181525 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
181526 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
181527
181528 /* Calculate nLeafEst. */
181529 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
181530 if( rc==SQLITE_OK ){
181531 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
181532 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
181533 if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
181534 nLeafEst = sqlite3_column_int(pLeafEst, 0);
181535 }
181536 rc = sqlite3_reset(pLeafEst);
181537 }
181538 if( rc!=SQLITE_OK ) return rc;
181539
181540 /* Calculate the first block to use in the output segment */
181541 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
181542 if( rc==SQLITE_OK ){
181543 if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
181544 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
181545 pWriter->iEnd = pWriter->iStart - 1;
181546 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
181547 }
181548 rc = sqlite3_reset(pFirstBlock);
181549 }
181550 if( rc!=SQLITE_OK ) return rc;
181551
181552 /* Insert the marker in the %_segments table to make sure nobody tries
181553 ** to steal the space just allocated. This is also used to identify
181554 ** appendable segments. */
181555 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
181556 if( rc!=SQLITE_OK ) return rc;
181557
181558 pWriter->iAbsLevel = iAbsLevel;
181559 pWriter->nLeafEst = nLeafEst;
181560 pWriter->iIdx = iIdx;
181561
181562 /* Set up the array of NodeWriter objects */
181563 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
181564 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
181565 }
181566 return SQLITE_OK;
181567}
181568
181569/*
181570** Remove an entry from the %_segdir table. This involves running the
181571** following two statements:
181572**
181573** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
181574** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
181575**
181576** The DELETE statement removes the specific %_segdir level. The UPDATE
181577** statement ensures that the remaining segments have contiguously allocated
181578** idx values.
181579*/
181580static int fts3RemoveSegdirEntry(
181581 Fts3Table *p, /* FTS3 table handle */
181582 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
181583 int iIdx /* Index of %_segdir entry to delete */
181584){
181585 int rc; /* Return code */
181586 sqlite3_stmt *pDelete = 0; /* DELETE statement */
181587
181588 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
181589 if( rc==SQLITE_OK ){
181590 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
181591 sqlite3_bind_int(pDelete, 2, iIdx);
181592 sqlite3_step(pDelete);
181593 rc = sqlite3_reset(pDelete);
181594 }
181595
181596 return rc;
181597}
181598
181599/*
181600** One or more segments have just been removed from absolute level iAbsLevel.
181601** Update the 'idx' values of the remaining segments in the level so that
181602** the idx values are a contiguous sequence starting from 0.
181603*/
181604static int fts3RepackSegdirLevel(
181605 Fts3Table *p, /* FTS3 table handle */
181606 sqlite3_int64 iAbsLevel /* Absolute level to repack */
181607){
181608 int rc; /* Return code */
181609 int *aIdx = 0; /* Array of remaining idx values */
181610 int nIdx = 0; /* Valid entries in aIdx[] */
181611 int nAlloc = 0; /* Allocated size of aIdx[] */
181612 int i; /* Iterator variable */
181613 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
181614 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
181615
181616 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
181617 if( rc==SQLITE_OK ){
181618 int rc2;
181619 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
181620 while( SQLITE_ROW==sqlite3_step(pSelect) ){
181621 if( nIdx>=nAlloc ){
181622 int *aNew;
181623 nAlloc += 16;
181624 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
181625 if( !aNew ){
181626 rc = SQLITE_NOMEM;
181627 break;
181628 }
181629 aIdx = aNew;
181630 }
181631 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
181632 }
181633 rc2 = sqlite3_reset(pSelect);
181634 if( rc==SQLITE_OK ) rc = rc2;
181635 }
181636
181637 if( rc==SQLITE_OK ){
181638 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
181639 }
181640 if( rc==SQLITE_OK ){
181641 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
181642 }
181643
181644 assert( p->bIgnoreSavepoint==0 );
181645 p->bIgnoreSavepoint = 1;
181646 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
181647 if( aIdx[i]!=i ){
181648 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
181649 sqlite3_bind_int(pUpdate, 1, i);
181650 sqlite3_step(pUpdate);
181651 rc = sqlite3_reset(pUpdate);
181652 }
181653 }
181654 p->bIgnoreSavepoint = 0;
181655
181656 sqlite3_free(aIdx);
181657 return rc;
181658}
181659
181660static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
181661 pNode->a[0] = (char)iHeight;
181662 if( iChild ){
181663 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
181664 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
181665 }else{
181666 assert( pNode->nAlloc>=1 );
181667 pNode->n = 1;
181668 }
181669}
181670
181671/*
181672** The first two arguments are a pointer to and the size of a segment b-tree
181673** node. The node may be a leaf or an internal node.
181674**
181675** This function creates a new node image in blob object *pNew by copying
181676** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
181677** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
181678*/
181679static int fts3TruncateNode(
181680 const char *aNode, /* Current node image */
181681 int nNode, /* Size of aNode in bytes */
181682 Blob *pNew, /* OUT: Write new node image here */
181683 const char *zTerm, /* Omit all terms smaller than this */
181684 int nTerm, /* Size of zTerm in bytes */
181685 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
181686){
181687 NodeReader reader; /* Reader object */
181688 Blob prev = {0, 0, 0}; /* Previous term written to new node */
181689 int rc = SQLITE_OK; /* Return code */
181690 int bLeaf; /* True for a leaf node */
181691
181692 if( nNode<1 ) return FTS_CORRUPT_VTAB;
181693 bLeaf = aNode[0]=='\0';
181694
181695 /* Allocate required output space */
181696 blobGrowBuffer(pNew, nNode, &rc);
181697 if( rc!=SQLITE_OK ) return rc;
181698 pNew->n = 0;
181699
181700 /* Populate new node buffer */
181701 for(rc = nodeReaderInit(&reader, aNode, nNode);
181702 rc==SQLITE_OK && reader.aNode;
181703 rc = nodeReaderNext(&reader)
181704 ){
181705 if( pNew->n==0 ){
181706 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
181707 if( res<0 || (bLeaf==0 && res==0) ) continue;
181708 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
181709 *piBlock = reader.iChild;
181710 }
181711 rc = fts3AppendToNode(
181712 pNew, &prev, reader.term.a, reader.term.n,
181713 reader.aDoclist, reader.nDoclist
181714 );
181715 if( rc!=SQLITE_OK ) break;
181716 }
181717 if( pNew->n==0 ){
181718 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
181719 *piBlock = reader.iChild;
181720 }
181721 assert( pNew->n<=pNew->nAlloc );
181722
181723 nodeReaderRelease(&reader);
181724 sqlite3_free(prev.a);
181725 return rc;
181726}
181727
181728/*
181729** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
181730** level iAbsLevel. This may involve deleting entries from the %_segments
181731** table, and modifying existing entries in both the %_segments and %_segdir
181732** tables.
181733**
181734** SQLITE_OK is returned if the segment is updated successfully. Or an
181735** SQLite error code otherwise.
181736*/
181737static int fts3TruncateSegment(
181738 Fts3Table *p, /* FTS3 table handle */
181739 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
181740 int iIdx, /* Index within level of segment to modify */
181741 const char *zTerm, /* Remove terms smaller than this */
181742 int nTerm /* Number of bytes in buffer zTerm */
181743){
181744 int rc = SQLITE_OK; /* Return code */
181745 Blob root = {0,0,0}; /* New root page image */
181746 Blob block = {0,0,0}; /* Buffer used for any other block */
181747 sqlite3_int64 iBlock = 0; /* Block id */
181748 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
181749 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
181750 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
181751
181752 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
181753 if( rc==SQLITE_OK ){
181754 int rc2; /* sqlite3_reset() return code */
181755 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
181756 sqlite3_bind_int(pFetch, 2, iIdx);
181757 if( SQLITE_ROW==sqlite3_step(pFetch) ){
181758 const char *aRoot = sqlite3_column_blob(pFetch, 4);
181759 int nRoot = sqlite3_column_bytes(pFetch, 4);
181760 iOldStart = sqlite3_column_int64(pFetch, 1);
181761 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
181762 }
181763 rc2 = sqlite3_reset(pFetch);
181764 if( rc==SQLITE_OK ) rc = rc2;
181765 }
181766
181767 while( rc==SQLITE_OK && iBlock ){
181768 char *aBlock = 0;
181769 int nBlock = 0;
181770 iNewStart = iBlock;
181771
181772 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
181773 if( rc==SQLITE_OK ){
181774 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
181775 }
181776 if( rc==SQLITE_OK ){
181777 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
181778 }
181779 sqlite3_free(aBlock);
181780 }
181781
181782 /* Variable iNewStart now contains the first valid leaf node. */
181783 if( rc==SQLITE_OK && iNewStart ){
181784 sqlite3_stmt *pDel = 0;
181785 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
181786 if( rc==SQLITE_OK ){
181787 sqlite3_bind_int64(pDel, 1, iOldStart);
181788 sqlite3_bind_int64(pDel, 2, iNewStart-1);
181789 sqlite3_step(pDel);
181790 rc = sqlite3_reset(pDel);
181791 }
181792 }
181793
181794 if( rc==SQLITE_OK ){
181795 sqlite3_stmt *pChomp = 0;
181796 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
181797 if( rc==SQLITE_OK ){
181798 sqlite3_bind_int64(pChomp, 1, iNewStart);
181799 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
181800 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
181801 sqlite3_bind_int(pChomp, 4, iIdx);
181802 sqlite3_step(pChomp);
181803 rc = sqlite3_reset(pChomp);
181804 sqlite3_bind_null(pChomp, 2);
181805 }
181806 }
181807
181808 sqlite3_free(root.a);
181809 sqlite3_free(block.a);
181810 return rc;
181811}
181812
181813/*
181814** This function is called after an incrmental-merge operation has run to
181815** merge (or partially merge) two or more segments from absolute level
181816** iAbsLevel.
181817**
181818** Each input segment is either removed from the db completely (if all of
181819** its data was copied to the output segment by the incrmerge operation)
181820** or modified in place so that it no longer contains those entries that
181821** have been duplicated in the output segment.
181822*/
181823static int fts3IncrmergeChomp(
181824 Fts3Table *p, /* FTS table handle */
181825 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
181826 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
181827 int *pnRem /* Number of segments not deleted */
181828){
181829 int i;
181830 int nRem = 0;
181831 int rc = SQLITE_OK;
181832
181833 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
181834 Fts3SegReader *pSeg = 0;
181835 int j;
181836
181837 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
181838 ** somewhere in the pCsr->apSegment[] array. */
181839 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
181840 pSeg = pCsr->apSegment[j];
181841 if( pSeg->iIdx==i ) break;
181842 }
181843 assert( j<pCsr->nSegment && pSeg->iIdx==i );
181844
181845 if( pSeg->aNode==0 ){
181846 /* Seg-reader is at EOF. Remove the entire input segment. */
181847 rc = fts3DeleteSegment(p, pSeg);
181848 if( rc==SQLITE_OK ){
181849 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
181850 }
181851 *pnRem = 0;
181852 }else{
181853 /* The incremental merge did not copy all the data from this
181854 ** segment to the upper level. The segment is modified in place
181855 ** so that it contains no keys smaller than zTerm/nTerm. */
181856 const char *zTerm = pSeg->zTerm;
181857 int nTerm = pSeg->nTerm;
181858 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
181859 nRem++;
181860 }
181861 }
181862
181863 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
181864 rc = fts3RepackSegdirLevel(p, iAbsLevel);
181865 }
181866
181867 *pnRem = nRem;
181868 return rc;
181869}
181870
181871/*
181872** Store an incr-merge hint in the database.
181873*/
181874static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
181875 sqlite3_stmt *pReplace = 0;
181876 int rc; /* Return code */
181877
181878 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
181879 if( rc==SQLITE_OK ){
181880 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
181881 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
181882 sqlite3_step(pReplace);
181883 rc = sqlite3_reset(pReplace);
181884 sqlite3_bind_null(pReplace, 2);
181885 }
181886
181887 return rc;
181888}
181889
181890/*
181891** Load an incr-merge hint from the database. The incr-merge hint, if one
181892** exists, is stored in the rowid==1 row of the %_stat table.
181893**
181894** If successful, populate blob *pHint with the value read from the %_stat
181895** table and return SQLITE_OK. Otherwise, if an error occurs, return an
181896** SQLite error code.
181897*/
181898static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
181899 sqlite3_stmt *pSelect = 0;
181900 int rc;
181901
181902 pHint->n = 0;
181903 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
181904 if( rc==SQLITE_OK ){
181905 int rc2;
181906 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
181907 if( SQLITE_ROW==sqlite3_step(pSelect) ){
181908 const char *aHint = sqlite3_column_blob(pSelect, 0);
181909 int nHint = sqlite3_column_bytes(pSelect, 0);
181910 if( aHint ){
181911 blobGrowBuffer(pHint, nHint, &rc);
181912 if( rc==SQLITE_OK ){
181913 memcpy(pHint->a, aHint, nHint);
181914 pHint->n = nHint;
181915 }
181916 }
181917 }
181918 rc2 = sqlite3_reset(pSelect);
181919 if( rc==SQLITE_OK ) rc = rc2;
181920 }
181921
181922 return rc;
181923}
181924
181925/*
181926** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
181927** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
181928** consists of two varints, the absolute level number of the input segments
181929** and the number of input segments.
181930**
181931** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
181932** set *pRc to an SQLite error code before returning.
181933*/
181934static void fts3IncrmergeHintPush(
181935 Blob *pHint, /* Hint blob to append to */
181936 i64 iAbsLevel, /* First varint to store in hint */
181937 int nInput, /* Second varint to store in hint */
181938 int *pRc /* IN/OUT: Error code */
181939){
181940 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
181941 if( *pRc==SQLITE_OK ){
181942 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
181943 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
181944 }
181945}
181946
181947/*
181948** Read the last entry (most recently pushed) from the hint blob *pHint
181949** and then remove the entry. Write the two values read to *piAbsLevel and
181950** *pnInput before returning.
181951**
181952** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
181953** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
181954*/
181955static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
181956 const int nHint = pHint->n;
181957 int i;
181958
181959 i = pHint->n-1;
181960 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB;
181961 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
181962 if( i==0 ) return FTS_CORRUPT_VTAB;
181963 i--;
181964 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
181965
181966 pHint->n = i;
181967 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
181968 i += fts3GetVarint32(&pHint->a[i], pnInput);
181969 assert( i<=nHint );
181970 if( i!=nHint ) return FTS_CORRUPT_VTAB;
181971
181972 return SQLITE_OK;
181973}
181974
181975
181976/*
181977** Attempt an incremental merge that writes nMerge leaf blocks.
181978**
181979** Incremental merges happen nMin segments at a time. The segments
181980** to be merged are the nMin oldest segments (the ones with the smallest
181981** values for the _segdir.idx field) in the highest level that contains
181982** at least nMin segments. Multiple merges might occur in an attempt to
181983** write the quota of nMerge leaf blocks.
181984*/
181985SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
181986 int rc; /* Return code */
181987 int nRem = nMerge; /* Number of leaf pages yet to be written */
181988 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
181989 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
181990 IncrmergeWriter *pWriter; /* Writer object */
181991 int nSeg = 0; /* Number of input segments */
181992 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
181993 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
181994 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
181995
181996 /* Allocate space for the cursor, filter and writer objects */
181997 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
181998 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
181999 if( !pWriter ) return SQLITE_NOMEM;
182000 pFilter = (Fts3SegFilter *)&pWriter[1];
182001 pCsr = (Fts3MultiSegReader *)&pFilter[1];
182002
182003 rc = fts3IncrmergeHintLoad(p, &hint);
182004 while( rc==SQLITE_OK && nRem>0 ){
182005 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
182006 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
182007 int bUseHint = 0; /* True if attempting to append */
182008 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
182009
182010 /* Search the %_segdir table for the absolute level with the smallest
182011 ** relative level number that contains at least nMin segments, if any.
182012 ** If one is found, set iAbsLevel to the absolute level number and
182013 ** nSeg to nMin. If no level with at least nMin segments can be found,
182014 ** set nSeg to -1.
182015 */
182016 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
182017 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
182018 if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
182019 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
182020 nSeg = sqlite3_column_int(pFindLevel, 1);
182021 assert( nSeg>=2 );
182022 }else{
182023 nSeg = -1;
182024 }
182025 rc = sqlite3_reset(pFindLevel);
182026
182027 /* If the hint read from the %_stat table is not empty, check if the
182028 ** last entry in it specifies a relative level smaller than or equal
182029 ** to the level identified by the block above (if any). If so, this
182030 ** iteration of the loop will work on merging at the hinted level.
182031 */
182032 if( rc==SQLITE_OK && hint.n ){
182033 int nHint = hint.n;
182034 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
182035 int nHintSeg = 0; /* Hint number of segments */
182036
182037 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
182038 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
182039 /* Based on the scan in the block above, it is known that there
182040 ** are no levels with a relative level smaller than that of
182041 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
182042 ** no levels with more than nMin segments. Use this to limit the
182043 ** value of nHintSeg to avoid a large memory allocation in case the
182044 ** merge-hint is corrupt*/
182045 iAbsLevel = iHintAbsLevel;
182046 nSeg = MIN(MAX(nMin,nSeg), nHintSeg);
182047 bUseHint = 1;
182048 bDirtyHint = 1;
182049 }else{
182050 /* This undoes the effect of the HintPop() above - so that no entry
182051 ** is removed from the hint blob. */
182052 hint.n = nHint;
182053 }
182054 }
182055
182056 /* If nSeg is less that zero, then there is no level with at least
182057 ** nMin segments and no hint in the %_stat table. No work to do.
182058 ** Exit early in this case. */
182059 if( nSeg<=0 ) break;
182060
182061 assert( nMod<=0x7FFFFFFF );
182062 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
182063 rc = FTS_CORRUPT_VTAB;
182064 break;
182065 }
182066
182067 /* Open a cursor to iterate through the contents of the oldest nSeg
182068 ** indexes of absolute level iAbsLevel. If this cursor is opened using
182069 ** the 'hint' parameters, it is possible that there are less than nSeg
182070 ** segments available in level iAbsLevel. In this case, no work is
182071 ** done on iAbsLevel - fall through to the next iteration of the loop
182072 ** to start work on some other level. */
182073 memset(pWriter, 0, nAlloc);
182074 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
182075
182076 if( rc==SQLITE_OK ){
182077 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
182078 assert( bUseHint==1 || bUseHint==0 );
182079 if( iIdx==0 || (bUseHint && iIdx==1) ){
182080 int bIgnore = 0;
182081 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
182082 if( bIgnore ){
182083 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
182084 }
182085 }
182086 }
182087
182088 if( rc==SQLITE_OK ){
182089 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
182090 }
182091 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
182092 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
182093 ){
182094 int bEmpty = 0;
182095 rc = sqlite3Fts3SegReaderStep(p, pCsr);
182096 if( rc==SQLITE_OK ){
182097 bEmpty = 1;
182098 }else if( rc!=SQLITE_ROW ){
182099 sqlite3Fts3SegReaderFinish(pCsr);
182100 break;
182101 }
182102 if( bUseHint && iIdx>0 ){
182103 const char *zKey = pCsr->zTerm;
182104 int nKey = pCsr->nTerm;
182105 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
182106 }else{
182107 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
182108 }
182109
182110 if( rc==SQLITE_OK && pWriter->nLeafEst ){
182111 fts3LogMerge(nSeg, iAbsLevel);
182112 if( bEmpty==0 ){
182113 do {
182114 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
182115 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
182116 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
182117 }while( rc==SQLITE_ROW );
182118 }
182119
182120 /* Update or delete the input segments */
182121 if( rc==SQLITE_OK ){
182122 nRem -= (1 + pWriter->nWork);
182123 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
182124 if( nSeg!=0 ){
182125 bDirtyHint = 1;
182126 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
182127 }
182128 }
182129 }
182130
182131 if( nSeg!=0 ){
182132 pWriter->nLeafData = pWriter->nLeafData * -1;
182133 }
182134 fts3IncrmergeRelease(p, pWriter, &rc);
182135 if( nSeg==0 && pWriter->bNoLeafData==0 ){
182136 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
182137 }
182138 }
182139
182140 sqlite3Fts3SegReaderFinish(pCsr);
182141 }
182142
182143 /* Write the hint values into the %_stat table for the next incr-merger */
182144 if( bDirtyHint && rc==SQLITE_OK ){
182145 rc = fts3IncrmergeHintStore(p, &hint);
182146 }
182147
182148 sqlite3_free(pWriter);
182149 sqlite3_free(hint.a);
182150 return rc;
182151}
182152
182153/*
182154** Convert the text beginning at *pz into an integer and return
182155** its value. Advance *pz to point to the first character past
182156** the integer.
182157**
182158** This function used for parameters to merge= and incrmerge=
182159** commands.
182160*/
182161static int fts3Getint(const char **pz){
182162 const char *z = *pz;
182163 int i = 0;
182164 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
182165 *pz = z;
182166 return i;
182167}
182168
182169/*
182170** Process statements of the form:
182171**
182172** INSERT INTO table(table) VALUES('merge=A,B');
182173**
182174** A and B are integers that decode to be the number of leaf pages
182175** written for the merge, and the minimum number of segments on a level
182176** before it will be selected for a merge, respectively.
182177*/
182178static int fts3DoIncrmerge(
182179 Fts3Table *p, /* FTS3 table handle */
182180 const char *zParam /* Nul-terminated string containing "A,B" */
182181){
182182 int rc;
182183 int nMin = (MergeCount(p) / 2);
182184 int nMerge = 0;
182185 const char *z = zParam;
182186
182187 /* Read the first integer value */
182188 nMerge = fts3Getint(&z);
182189
182190 /* If the first integer value is followed by a ',', read the second
182191 ** integer value. */
182192 if( z[0]==',' && z[1]!='\0' ){
182193 z++;
182194 nMin = fts3Getint(&z);
182195 }
182196
182197 if( z[0]!='\0' || nMin<2 ){
182198 rc = SQLITE_ERROR;
182199 }else{
182200 rc = SQLITE_OK;
182201 if( !p->bHasStat ){
182202 assert( p->bFts4==0 );
182203 sqlite3Fts3CreateStatTable(&rc, p);
182204 }
182205 if( rc==SQLITE_OK ){
182206 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
182207 }
182208 sqlite3Fts3SegmentsClose(p);
182209 }
182210 return rc;
182211}
182212
182213/*
182214** Process statements of the form:
182215**
182216** INSERT INTO table(table) VALUES('automerge=X');
182217**
182218** where X is an integer. X==0 means to turn automerge off. X!=0 means
182219** turn it on. The setting is persistent.
182220*/
182221static int fts3DoAutoincrmerge(
182222 Fts3Table *p, /* FTS3 table handle */
182223 const char *zParam /* Nul-terminated string containing boolean */
182224){
182225 int rc = SQLITE_OK;
182226 sqlite3_stmt *pStmt = 0;
182227 p->nAutoincrmerge = fts3Getint(&zParam);
182228 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){
182229 p->nAutoincrmerge = 8;
182230 }
182231 if( !p->bHasStat ){
182232 assert( p->bFts4==0 );
182233 sqlite3Fts3CreateStatTable(&rc, p);
182234 if( rc ) return rc;
182235 }
182236 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
182237 if( rc ) return rc;
182238 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
182239 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
182240 sqlite3_step(pStmt);
182241 rc = sqlite3_reset(pStmt);
182242 return rc;
182243}
182244
182245/*
182246** Return a 64-bit checksum for the FTS index entry specified by the
182247** arguments to this function.
182248*/
182249static u64 fts3ChecksumEntry(
182250 const char *zTerm, /* Pointer to buffer containing term */
182251 int nTerm, /* Size of zTerm in bytes */
182252 int iLangid, /* Language id for current row */
182253 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
182254 i64 iDocid, /* Docid for current row. */
182255 int iCol, /* Column number */
182256 int iPos /* Position */
182257){
182258 int i;
182259 u64 ret = (u64)iDocid;
182260
182261 ret += (ret<<3) + iLangid;
182262 ret += (ret<<3) + iIndex;
182263 ret += (ret<<3) + iCol;
182264 ret += (ret<<3) + iPos;
182265 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
182266
182267 return ret;
182268}
182269
182270/*
182271** Return a checksum of all entries in the FTS index that correspond to
182272** language id iLangid. The checksum is calculated by XORing the checksums
182273** of each individual entry (see fts3ChecksumEntry()) together.
182274**
182275** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
182276** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
182277** return value is undefined in this case.
182278*/
182279static u64 fts3ChecksumIndex(
182280 Fts3Table *p, /* FTS3 table handle */
182281 int iLangid, /* Language id to return cksum for */
182282 int iIndex, /* Index to cksum (0..p->nIndex-1) */
182283 int *pRc /* OUT: Return code */
182284){
182285 Fts3SegFilter filter;
182286 Fts3MultiSegReader csr;
182287 int rc;
182288 u64 cksum = 0;
182289
182290 assert( *pRc==SQLITE_OK );
182291
182292 memset(&filter, 0, sizeof(filter));
182293 memset(&csr, 0, sizeof(csr));
182294 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
182295 filter.flags |= FTS3_SEGMENT_SCAN;
182296
182297 rc = sqlite3Fts3SegReaderCursor(
182298 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
182299 );
182300 if( rc==SQLITE_OK ){
182301 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
182302 }
182303
182304 if( rc==SQLITE_OK ){
182305 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
182306 char *pCsr = csr.aDoclist;
182307 char *pEnd = &pCsr[csr.nDoclist];
182308
182309 i64 iDocid = 0;
182310 i64 iCol = 0;
182311 u64 iPos = 0;
182312
182313 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
182314 while( pCsr<pEnd ){
182315 u64 iVal = 0;
182316 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
182317 if( pCsr<pEnd ){
182318 if( iVal==0 || iVal==1 ){
182319 iCol = 0;
182320 iPos = 0;
182321 if( iVal ){
182322 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
182323 }else{
182324 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
182325 if( p->bDescIdx ){
182326 iDocid = (i64)((u64)iDocid - iVal);
182327 }else{
182328 iDocid = (i64)((u64)iDocid + iVal);
182329 }
182330 }
182331 }else{
182332 iPos += (iVal - 2);
182333 cksum = cksum ^ fts3ChecksumEntry(
182334 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
182335 (int)iCol, (int)iPos
182336 );
182337 }
182338 }
182339 }
182340 }
182341 }
182342 sqlite3Fts3SegReaderFinish(&csr);
182343
182344 *pRc = rc;
182345 return cksum;
182346}
182347
182348/*
182349** Check if the contents of the FTS index match the current contents of the
182350** content table. If no error occurs and the contents do match, set *pbOk
182351** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
182352** to false before returning.
182353**
182354** If an error occurs (e.g. an OOM or IO error), return an SQLite error
182355** code. The final value of *pbOk is undefined in this case.
182356*/
182357static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
182358 int rc = SQLITE_OK; /* Return code */
182359 u64 cksum1 = 0; /* Checksum based on FTS index contents */
182360 u64 cksum2 = 0; /* Checksum based on %_content contents */
182361 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
182362
182363 /* This block calculates the checksum according to the FTS index. */
182364 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
182365 if( rc==SQLITE_OK ){
182366 int rc2;
182367 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
182368 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
182369 while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
182370 int iLangid = sqlite3_column_int(pAllLangid, 0);
182371 int i;
182372 for(i=0; i<p->nIndex; i++){
182373 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
182374 }
182375 }
182376 rc2 = sqlite3_reset(pAllLangid);
182377 if( rc==SQLITE_OK ) rc = rc2;
182378 }
182379
182380 /* This block calculates the checksum according to the %_content table */
182381 if( rc==SQLITE_OK ){
182382 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
182383 sqlite3_stmt *pStmt = 0;
182384 char *zSql;
182385
182386 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
182387 if( !zSql ){
182388 rc = SQLITE_NOMEM;
182389 }else{
182390 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
182391 sqlite3_free(zSql);
182392 }
182393
182394 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
182395 i64 iDocid = sqlite3_column_int64(pStmt, 0);
182396 int iLang = langidFromSelect(p, pStmt);
182397 int iCol;
182398
182399 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
182400 if( p->abNotindexed[iCol]==0 ){
182401 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
182402 sqlite3_tokenizer_cursor *pT = 0;
182403
182404 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT);
182405 while( rc==SQLITE_OK ){
182406 char const *zToken; /* Buffer containing token */
182407 int nToken = 0; /* Number of bytes in token */
182408 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
182409 int iPos = 0; /* Position of token in zText */
182410
182411 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
182412 if( rc==SQLITE_OK ){
182413 int i;
182414 cksum2 = cksum2 ^ fts3ChecksumEntry(
182415 zToken, nToken, iLang, 0, iDocid, iCol, iPos
182416 );
182417 for(i=1; i<p->nIndex; i++){
182418 if( p->aIndex[i].nPrefix<=nToken ){
182419 cksum2 = cksum2 ^ fts3ChecksumEntry(
182420 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
182421 );
182422 }
182423 }
182424 }
182425 }
182426 if( pT ) pModule->xClose(pT);
182427 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
182428 }
182429 }
182430 }
182431
182432 sqlite3_finalize(pStmt);
182433 }
182434
182435 *pbOk = (cksum1==cksum2);
182436 return rc;
182437}
182438
182439/*
182440** Run the integrity-check. If no error occurs and the current contents of
182441** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
182442** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
182443**
182444** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
182445** error code.
182446**
182447** The integrity-check works as follows. For each token and indexed token
182448** prefix in the document set, a 64-bit checksum is calculated (by code
182449** in fts3ChecksumEntry()) based on the following:
182450**
182451** + The index number (0 for the main index, 1 for the first prefix
182452** index etc.),
182453** + The token (or token prefix) text itself,
182454** + The language-id of the row it appears in,
182455** + The docid of the row it appears in,
182456** + The column it appears in, and
182457** + The tokens position within that column.
182458**
182459** The checksums for all entries in the index are XORed together to create
182460** a single checksum for the entire index.
182461**
182462** The integrity-check code calculates the same checksum in two ways:
182463**
182464** 1. By scanning the contents of the FTS index, and
182465** 2. By scanning and tokenizing the content table.
182466**
182467** If the two checksums are identical, the integrity-check is deemed to have
182468** passed.
182469*/
182470static int fts3DoIntegrityCheck(
182471 Fts3Table *p /* FTS3 table handle */
182472){
182473 int rc;
182474 int bOk = 0;
182475 rc = fts3IntegrityCheck(p, &bOk);
182476 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
182477 return rc;
182478}
182479
182480/*
182481** Handle a 'special' INSERT of the form:
182482**
182483** "INSERT INTO tbl(tbl) VALUES(<expr>)"
182484**
182485** Argument pVal contains the result of <expr>. Currently the only
182486** meaningful value to insert is the text 'optimize'.
182487*/
182488static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
182489 int rc = SQLITE_ERROR; /* Return Code */
182490 const char *zVal = (const char *)sqlite3_value_text(pVal);
182491 int nVal = sqlite3_value_bytes(pVal);
182492
182493 if( !zVal ){
182494 return SQLITE_NOMEM;
182495 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
182496 rc = fts3DoOptimize(p, 0);
182497 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
182498 rc = fts3DoRebuild(p);
182499 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
182500 rc = fts3DoIntegrityCheck(p);
182501 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
182502 rc = fts3DoIncrmerge(p, &zVal[6]);
182503 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
182504 rc = fts3DoAutoincrmerge(p, &zVal[10]);
182505#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
182506 }else{
182507 int v;
182508 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
182509 v = atoi(&zVal[9]);
182510 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
182511 rc = SQLITE_OK;
182512 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
182513 v = atoi(&zVal[11]);
182514 if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v;
182515 rc = SQLITE_OK;
182516 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
182517 p->bNoIncrDoclist = atoi(&zVal[21]);
182518 rc = SQLITE_OK;
182519 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
182520 v = atoi(&zVal[11]);
182521 if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v;
182522 rc = SQLITE_OK;
182523 }
182524#endif
182525 }
182526 return rc;
182527}
182528
182529#ifndef SQLITE_DISABLE_FTS4_DEFERRED
182530/*
182531** Delete all cached deferred doclists. Deferred doclists are cached
182532** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
182533*/
182534SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
182535 Fts3DeferredToken *pDef;
182536 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
182537 fts3PendingListDelete(pDef->pList);
182538 pDef->pList = 0;
182539 }
182540}
182541
182542/*
182543** Free all entries in the pCsr->pDeffered list. Entries are added to
182544** this list using sqlite3Fts3DeferToken().
182545*/
182546SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
182547 Fts3DeferredToken *pDef;
182548 Fts3DeferredToken *pNext;
182549 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
182550 pNext = pDef->pNext;
182551 fts3PendingListDelete(pDef->pList);
182552 sqlite3_free(pDef);
182553 }
182554 pCsr->pDeferred = 0;
182555}
182556
182557/*
182558** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
182559** based on the row that pCsr currently points to.
182560**
182561** A deferred-doclist is like any other doclist with position information
182562** included, except that it only contains entries for a single row of the
182563** table, not for all rows.
182564*/
182565SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
182566 int rc = SQLITE_OK; /* Return code */
182567 if( pCsr->pDeferred ){
182568 int i; /* Used to iterate through table columns */
182569 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
182570 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
182571
182572 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
182573 sqlite3_tokenizer *pT = p->pTokenizer;
182574 sqlite3_tokenizer_module const *pModule = pT->pModule;
182575
182576 assert( pCsr->isRequireSeek==0 );
182577 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
182578
182579 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
182580 if( p->abNotindexed[i]==0 ){
182581 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
182582 sqlite3_tokenizer_cursor *pTC = 0;
182583
182584 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
182585 while( rc==SQLITE_OK ){
182586 char const *zToken; /* Buffer containing token */
182587 int nToken = 0; /* Number of bytes in token */
182588 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
182589 int iPos = 0; /* Position of token in zText */
182590
182591 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
182592 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
182593 Fts3PhraseToken *pPT = pDef->pToken;
182594 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
182595 && (pPT->bFirst==0 || iPos==0)
182596 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
182597 && (0==memcmp(zToken, pPT->z, pPT->n))
182598 ){
182599 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
182600 }
182601 }
182602 }
182603 if( pTC ) pModule->xClose(pTC);
182604 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
182605 }
182606 }
182607
182608 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
182609 if( pDef->pList ){
182610 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
182611 }
182612 }
182613 }
182614
182615 return rc;
182616}
182617
182618SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
182619 Fts3DeferredToken *p,
182620 char **ppData,
182621 int *pnData
182622){
182623 char *pRet;
182624 int nSkip;
182625 sqlite3_int64 dummy;
182626
182627 *ppData = 0;
182628 *pnData = 0;
182629
182630 if( p->pList==0 ){
182631 return SQLITE_OK;
182632 }
182633
182634 pRet = (char *)sqlite3_malloc(p->pList->nData);
182635 if( !pRet ) return SQLITE_NOMEM;
182636
182637 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
182638 *pnData = p->pList->nData - nSkip;
182639 *ppData = pRet;
182640
182641 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
182642 return SQLITE_OK;
182643}
182644
182645/*
182646** Add an entry for token pToken to the pCsr->pDeferred list.
182647*/
182648SQLITE_PRIVATE int sqlite3Fts3DeferToken(
182649 Fts3Cursor *pCsr, /* Fts3 table cursor */
182650 Fts3PhraseToken *pToken, /* Token to defer */
182651 int iCol /* Column that token must appear in (or -1) */
182652){
182653 Fts3DeferredToken *pDeferred;
182654 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
182655 if( !pDeferred ){
182656 return SQLITE_NOMEM;
182657 }
182658 memset(pDeferred, 0, sizeof(*pDeferred));
182659 pDeferred->pToken = pToken;
182660 pDeferred->pNext = pCsr->pDeferred;
182661 pDeferred->iCol = iCol;
182662 pCsr->pDeferred = pDeferred;
182663
182664 assert( pToken->pDeferred==0 );
182665 pToken->pDeferred = pDeferred;
182666
182667 return SQLITE_OK;
182668}
182669#endif
182670
182671/*
182672** SQLite value pRowid contains the rowid of a row that may or may not be
182673** present in the FTS3 table. If it is, delete it and adjust the contents
182674** of subsiduary data structures accordingly.
182675*/
182676static int fts3DeleteByRowid(
182677 Fts3Table *p,
182678 sqlite3_value *pRowid,
182679 int *pnChng, /* IN/OUT: Decrement if row is deleted */
182680 u32 *aSzDel
182681){
182682 int rc = SQLITE_OK; /* Return code */
182683 int bFound = 0; /* True if *pRowid really is in the table */
182684
182685 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
182686 if( bFound && rc==SQLITE_OK ){
182687 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
182688 rc = fts3IsEmpty(p, pRowid, &isEmpty);
182689 if( rc==SQLITE_OK ){
182690 if( isEmpty ){
182691 /* Deleting this row means the whole table is empty. In this case
182692 ** delete the contents of all three tables and throw away any
182693 ** data in the pendingTerms hash table. */
182694 rc = fts3DeleteAll(p, 1);
182695 *pnChng = 0;
182696 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
182697 }else{
182698 *pnChng = *pnChng - 1;
182699 if( p->zContentTbl==0 ){
182700 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
182701 }
182702 if( p->bHasDocsize ){
182703 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
182704 }
182705 }
182706 }
182707 }
182708
182709 return rc;
182710}
182711
182712/*
182713** This function does the work for the xUpdate method of FTS3 virtual
182714** tables. The schema of the virtual table being:
182715**
182716** CREATE TABLE <table name>(
182717** <user columns>,
182718** <table name> HIDDEN,
182719** docid HIDDEN,
182720** <langid> HIDDEN
182721** );
182722**
182723**
182724*/
182725SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
182726 sqlite3_vtab *pVtab, /* FTS3 vtab object */
182727 int nArg, /* Size of argument array */
182728 sqlite3_value **apVal, /* Array of arguments */
182729 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
182730){
182731 Fts3Table *p = (Fts3Table *)pVtab;
182732 int rc = SQLITE_OK; /* Return Code */
182733 u32 *aSzIns = 0; /* Sizes of inserted documents */
182734 u32 *aSzDel = 0; /* Sizes of deleted documents */
182735 int nChng = 0; /* Net change in number of documents */
182736 int bInsertDone = 0;
182737
182738 /* At this point it must be known if the %_stat table exists or not.
182739 ** So bHasStat may not be 2. */
182740 assert( p->bHasStat==0 || p->bHasStat==1 );
182741
182742 assert( p->pSegments==0 );
182743 assert(
182744 nArg==1 /* DELETE operations */
182745 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
182746 );
182747
182748 /* Check for a "special" INSERT operation. One of the form:
182749 **
182750 ** INSERT INTO xyz(xyz) VALUES('command');
182751 */
182752 if( nArg>1
182753 && sqlite3_value_type(apVal[0])==SQLITE_NULL
182754 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
182755 ){
182756 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
182757 goto update_out;
182758 }
182759
182760 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
182761 rc = SQLITE_CONSTRAINT;
182762 goto update_out;
182763 }
182764
182765 /* Allocate space to hold the change in document sizes */
182766 aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
182767 if( aSzDel==0 ){
182768 rc = SQLITE_NOMEM;
182769 goto update_out;
182770 }
182771 aSzIns = &aSzDel[p->nColumn+1];
182772 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
182773
182774 rc = fts3Writelock(p);
182775 if( rc!=SQLITE_OK ) goto update_out;
182776
182777 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
182778 ** value, then this operation requires constraint handling.
182779 **
182780 ** If the on-conflict mode is REPLACE, this means that the existing row
182781 ** should be deleted from the database before inserting the new row. Or,
182782 ** if the on-conflict mode is other than REPLACE, then this method must
182783 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
182784 ** modify the database file.
182785 */
182786 if( nArg>1 && p->zContentTbl==0 ){
182787 /* Find the value object that holds the new rowid value. */
182788 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
182789 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
182790 pNewRowid = apVal[1];
182791 }
182792
182793 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
182794 sqlite3_value_type(apVal[0])==SQLITE_NULL
182795 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
182796 )){
182797 /* The new rowid is not NULL (in this case the rowid will be
182798 ** automatically assigned and there is no chance of a conflict), and
182799 ** the statement is either an INSERT or an UPDATE that modifies the
182800 ** rowid column. So if the conflict mode is REPLACE, then delete any
182801 ** existing row with rowid=pNewRowid.
182802 **
182803 ** Or, if the conflict mode is not REPLACE, insert the new record into
182804 ** the %_content table. If we hit the duplicate rowid constraint (or any
182805 ** other error) while doing so, return immediately.
182806 **
182807 ** This branch may also run if pNewRowid contains a value that cannot
182808 ** be losslessly converted to an integer. In this case, the eventual
182809 ** call to fts3InsertData() (either just below or further on in this
182810 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
182811 ** invoked, it will delete zero rows (since no row will have
182812 ** docid=$pNewRowid if $pNewRowid is not an integer value).
182813 */
182814 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
182815 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
182816 }else{
182817 rc = fts3InsertData(p, apVal, pRowid);
182818 bInsertDone = 1;
182819 }
182820 }
182821 }
182822 if( rc!=SQLITE_OK ){
182823 goto update_out;
182824 }
182825
182826 /* If this is a DELETE or UPDATE operation, remove the old record. */
182827 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
182828 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
182829 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
182830 }
182831
182832 /* If this is an INSERT or UPDATE operation, insert the new record. */
182833 if( nArg>1 && rc==SQLITE_OK ){
182834 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
182835 if( bInsertDone==0 ){
182836 rc = fts3InsertData(p, apVal, pRowid);
182837 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
182838 rc = FTS_CORRUPT_VTAB;
182839 }
182840 }
182841 if( rc==SQLITE_OK ){
182842 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
182843 }
182844 if( rc==SQLITE_OK ){
182845 assert( p->iPrevDocid==*pRowid );
182846 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
182847 }
182848 if( p->bHasDocsize ){
182849 fts3InsertDocsize(&rc, p, aSzIns);
182850 }
182851 nChng++;
182852 }
182853
182854 if( p->bFts4 ){
182855 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
182856 }
182857
182858 update_out:
182859 sqlite3_free(aSzDel);
182860 sqlite3Fts3SegmentsClose(p);
182861 return rc;
182862}
182863
182864/*
182865** Flush any data in the pending-terms hash table to disk. If successful,
182866** merge all segments in the database (including the new segment, if
182867** there was any data to flush) into a single segment.
182868*/
182869SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
182870 int rc;
182871 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
182872 if( rc==SQLITE_OK ){
182873 rc = fts3DoOptimize(p, 1);
182874 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
182875 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
182876 if( rc2!=SQLITE_OK ) rc = rc2;
182877 }else{
182878 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
182879 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
182880 }
182881 }
182882 sqlite3Fts3SegmentsClose(p);
182883 return rc;
182884}
182885
182886#endif
182887
182888/************** End of fts3_write.c ******************************************/
182889/************** Begin file fts3_snippet.c ************************************/
182890/*
182891** 2009 Oct 23
182892**
182893** The author disclaims copyright to this source code. In place of
182894** a legal notice, here is a blessing:
182895**
182896** May you do good and not evil.
182897** May you find forgiveness for yourself and forgive others.
182898** May you share freely, never taking more than you give.
182899**
182900******************************************************************************
182901*/
182902
182903/* #include "fts3Int.h" */
182904#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
182905
182906/* #include <string.h> */
182907/* #include <assert.h> */
182908
182909/*
182910** Characters that may appear in the second argument to matchinfo().
182911*/
182912#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
182913#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
182914#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
182915#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
182916#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
182917#define FTS3_MATCHINFO_LCS 's' /* nCol values */
182918#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
182919#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
182920#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
182921
182922/*
182923** The default value for the second argument to matchinfo().
182924*/
182925#define FTS3_MATCHINFO_DEFAULT "pcx"
182926
182927
182928/*
182929** Used as an fts3ExprIterate() context when loading phrase doclists to
182930** Fts3Expr.aDoclist[]/nDoclist.
182931*/
182932typedef struct LoadDoclistCtx LoadDoclistCtx;
182933struct LoadDoclistCtx {
182934 Fts3Cursor *pCsr; /* FTS3 Cursor */
182935 int nPhrase; /* Number of phrases seen so far */
182936 int nToken; /* Number of tokens seen so far */
182937};
182938
182939/*
182940** The following types are used as part of the implementation of the
182941** fts3BestSnippet() routine.
182942*/
182943typedef struct SnippetIter SnippetIter;
182944typedef struct SnippetPhrase SnippetPhrase;
182945typedef struct SnippetFragment SnippetFragment;
182946
182947struct SnippetIter {
182948 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
182949 int iCol; /* Extract snippet from this column */
182950 int nSnippet; /* Requested snippet length (in tokens) */
182951 int nPhrase; /* Number of phrases in query */
182952 SnippetPhrase *aPhrase; /* Array of size nPhrase */
182953 int iCurrent; /* First token of current snippet */
182954};
182955
182956struct SnippetPhrase {
182957 int nToken; /* Number of tokens in phrase */
182958 char *pList; /* Pointer to start of phrase position list */
182959 int iHead; /* Next value in position list */
182960 char *pHead; /* Position list data following iHead */
182961 int iTail; /* Next value in trailing position list */
182962 char *pTail; /* Position list data following iTail */
182963};
182964
182965struct SnippetFragment {
182966 int iCol; /* Column snippet is extracted from */
182967 int iPos; /* Index of first token in snippet */
182968 u64 covered; /* Mask of query phrases covered */
182969 u64 hlmask; /* Mask of snippet terms to highlight */
182970};
182971
182972/*
182973** This type is used as an fts3ExprIterate() context object while
182974** accumulating the data returned by the matchinfo() function.
182975*/
182976typedef struct MatchInfo MatchInfo;
182977struct MatchInfo {
182978 Fts3Cursor *pCursor; /* FTS3 Cursor */
182979 int nCol; /* Number of columns in table */
182980 int nPhrase; /* Number of matchable phrases in query */
182981 sqlite3_int64 nDoc; /* Number of docs in database */
182982 char flag;
182983 u32 *aMatchinfo; /* Pre-allocated buffer */
182984};
182985
182986/*
182987** An instance of this structure is used to manage a pair of buffers, each
182988** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
182989** for details.
182990*/
182991struct MatchinfoBuffer {
182992 u8 aRef[3];
182993 int nElem;
182994 int bGlobal; /* Set if global data is loaded */
182995 char *zMatchinfo;
182996 u32 aMatchinfo[1];
182997};
182998
182999
183000/*
183001** The snippet() and offsets() functions both return text values. An instance
183002** of the following structure is used to accumulate those values while the
183003** functions are running. See fts3StringAppend() for details.
183004*/
183005typedef struct StrBuffer StrBuffer;
183006struct StrBuffer {
183007 char *z; /* Pointer to buffer containing string */
183008 int n; /* Length of z in bytes (excl. nul-term) */
183009 int nAlloc; /* Allocated size of buffer z in bytes */
183010};
183011
183012
183013/*************************************************************************
183014** Start of MatchinfoBuffer code.
183015*/
183016
183017/*
183018** Allocate a two-slot MatchinfoBuffer object.
183019*/
183020static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
183021 MatchinfoBuffer *pRet;
183022 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
183023 + sizeof(MatchinfoBuffer);
183024 sqlite3_int64 nStr = strlen(zMatchinfo);
183025
183026 pRet = sqlite3_malloc64(nByte + nStr+1);
183027 if( pRet ){
183028 memset(pRet, 0, nByte);
183029 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
183030 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
183031 + sizeof(u32)*((int)nElem+1);
183032 pRet->nElem = (int)nElem;
183033 pRet->zMatchinfo = ((char*)pRet) + nByte;
183034 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
183035 pRet->aRef[0] = 1;
183036 }
183037
183038 return pRet;
183039}
183040
183041static void fts3MIBufferFree(void *p){
183042 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
183043
183044 assert( (u32*)p==&pBuf->aMatchinfo[1]
183045 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
183046 );
183047 if( (u32*)p==&pBuf->aMatchinfo[1] ){
183048 pBuf->aRef[1] = 0;
183049 }else{
183050 pBuf->aRef[2] = 0;
183051 }
183052
183053 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
183054 sqlite3_free(pBuf);
183055 }
183056}
183057
183058static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
183059 void (*xRet)(void*) = 0;
183060 u32 *aOut = 0;
183061
183062 if( p->aRef[1]==0 ){
183063 p->aRef[1] = 1;
183064 aOut = &p->aMatchinfo[1];
183065 xRet = fts3MIBufferFree;
183066 }
183067 else if( p->aRef[2]==0 ){
183068 p->aRef[2] = 1;
183069 aOut = &p->aMatchinfo[p->nElem+2];
183070 xRet = fts3MIBufferFree;
183071 }else{
183072 aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
183073 if( aOut ){
183074 xRet = sqlite3_free;
183075 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
183076 }
183077 }
183078
183079 *paOut = aOut;
183080 return xRet;
183081}
183082
183083static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
183084 p->bGlobal = 1;
183085 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
183086}
183087
183088/*
183089** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
183090*/
183091SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
183092 if( p ){
183093 assert( p->aRef[0]==1 );
183094 p->aRef[0] = 0;
183095 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
183096 sqlite3_free(p);
183097 }
183098 }
183099}
183100
183101/*
183102** End of MatchinfoBuffer code.
183103*************************************************************************/
183104
183105
183106/*
183107** This function is used to help iterate through a position-list. A position
183108** list is a list of unique integers, sorted from smallest to largest. Each
183109** element of the list is represented by an FTS3 varint that takes the value
183110** of the difference between the current element and the previous one plus
183111** two. For example, to store the position-list:
183112**
183113** 4 9 113
183114**
183115** the three varints:
183116**
183117** 6 7 106
183118**
183119** are encoded.
183120**
183121** When this function is called, *pp points to the start of an element of
183122** the list. *piPos contains the value of the previous entry in the list.
183123** After it returns, *piPos contains the value of the next element of the
183124** list and *pp is advanced to the following varint.
183125*/
183126static void fts3GetDeltaPosition(char **pp, int *piPos){
183127 int iVal;
183128 *pp += fts3GetVarint32(*pp, &iVal);
183129 *piPos += (iVal-2);
183130}
183131
183132/*
183133** Helper function for fts3ExprIterate() (see below).
183134*/
183135static int fts3ExprIterate2(
183136 Fts3Expr *pExpr, /* Expression to iterate phrases of */
183137 int *piPhrase, /* Pointer to phrase counter */
183138 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
183139 void *pCtx /* Second argument to pass to callback */
183140){
183141 int rc; /* Return code */
183142 int eType = pExpr->eType; /* Type of expression node pExpr */
183143
183144 if( eType!=FTSQUERY_PHRASE ){
183145 assert( pExpr->pLeft && pExpr->pRight );
183146 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
183147 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
183148 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
183149 }
183150 }else{
183151 rc = x(pExpr, *piPhrase, pCtx);
183152 (*piPhrase)++;
183153 }
183154 return rc;
183155}
183156
183157/*
183158** Iterate through all phrase nodes in an FTS3 query, except those that
183159** are part of a sub-tree that is the right-hand-side of a NOT operator.
183160** For each phrase node found, the supplied callback function is invoked.
183161**
183162** If the callback function returns anything other than SQLITE_OK,
183163** the iteration is abandoned and the error code returned immediately.
183164** Otherwise, SQLITE_OK is returned after a callback has been made for
183165** all eligible phrase nodes.
183166*/
183167static int fts3ExprIterate(
183168 Fts3Expr *pExpr, /* Expression to iterate phrases of */
183169 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
183170 void *pCtx /* Second argument to pass to callback */
183171){
183172 int iPhrase = 0; /* Variable used as the phrase counter */
183173 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
183174}
183175
183176
183177/*
183178** This is an fts3ExprIterate() callback used while loading the doclists
183179** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
183180** fts3ExprLoadDoclists().
183181*/
183182static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
183183 int rc = SQLITE_OK;
183184 Fts3Phrase *pPhrase = pExpr->pPhrase;
183185 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
183186
183187 UNUSED_PARAMETER(iPhrase);
183188
183189 p->nPhrase++;
183190 p->nToken += pPhrase->nToken;
183191
183192 return rc;
183193}
183194
183195/*
183196** Load the doclists for each phrase in the query associated with FTS3 cursor
183197** pCsr.
183198**
183199** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
183200** phrases in the expression (all phrases except those directly or
183201** indirectly descended from the right-hand-side of a NOT operator). If
183202** pnToken is not NULL, then it is set to the number of tokens in all
183203** matchable phrases of the expression.
183204*/
183205static int fts3ExprLoadDoclists(
183206 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
183207 int *pnPhrase, /* OUT: Number of phrases in query */
183208 int *pnToken /* OUT: Number of tokens in query */
183209){
183210 int rc; /* Return Code */
183211 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
183212 sCtx.pCsr = pCsr;
183213 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
183214 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
183215 if( pnToken ) *pnToken = sCtx.nToken;
183216 return rc;
183217}
183218
183219static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
183220 (*(int *)ctx)++;
183221 pExpr->iPhrase = iPhrase;
183222 return SQLITE_OK;
183223}
183224static int fts3ExprPhraseCount(Fts3Expr *pExpr){
183225 int nPhrase = 0;
183226 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
183227 return nPhrase;
183228}
183229
183230/*
183231** Advance the position list iterator specified by the first two
183232** arguments so that it points to the first element with a value greater
183233** than or equal to parameter iNext.
183234*/
183235static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
183236 char *pIter = *ppIter;
183237 if( pIter ){
183238 int iIter = *piIter;
183239
183240 while( iIter<iNext ){
183241 if( 0==(*pIter & 0xFE) ){
183242 iIter = -1;
183243 pIter = 0;
183244 break;
183245 }
183246 fts3GetDeltaPosition(&pIter, &iIter);
183247 }
183248
183249 *piIter = iIter;
183250 *ppIter = pIter;
183251 }
183252}
183253
183254/*
183255** Advance the snippet iterator to the next candidate snippet.
183256*/
183257static int fts3SnippetNextCandidate(SnippetIter *pIter){
183258 int i; /* Loop counter */
183259
183260 if( pIter->iCurrent<0 ){
183261 /* The SnippetIter object has just been initialized. The first snippet
183262 ** candidate always starts at offset 0 (even if this candidate has a
183263 ** score of 0.0).
183264 */
183265 pIter->iCurrent = 0;
183266
183267 /* Advance the 'head' iterator of each phrase to the first offset that
183268 ** is greater than or equal to (iNext+nSnippet).
183269 */
183270 for(i=0; i<pIter->nPhrase; i++){
183271 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
183272 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
183273 }
183274 }else{
183275 int iStart;
183276 int iEnd = 0x7FFFFFFF;
183277
183278 for(i=0; i<pIter->nPhrase; i++){
183279 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
183280 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
183281 iEnd = pPhrase->iHead;
183282 }
183283 }
183284 if( iEnd==0x7FFFFFFF ){
183285 return 1;
183286 }
183287
183288 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
183289 for(i=0; i<pIter->nPhrase; i++){
183290 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
183291 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
183292 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
183293 }
183294 }
183295
183296 return 0;
183297}
183298
183299/*
183300** Retrieve information about the current candidate snippet of snippet
183301** iterator pIter.
183302*/
183303static void fts3SnippetDetails(
183304 SnippetIter *pIter, /* Snippet iterator */
183305 u64 mCovered, /* Bitmask of phrases already covered */
183306 int *piToken, /* OUT: First token of proposed snippet */
183307 int *piScore, /* OUT: "Score" for this snippet */
183308 u64 *pmCover, /* OUT: Bitmask of phrases covered */
183309 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
183310){
183311 int iStart = pIter->iCurrent; /* First token of snippet */
183312 int iScore = 0; /* Score of this snippet */
183313 int i; /* Loop counter */
183314 u64 mCover = 0; /* Mask of phrases covered by this snippet */
183315 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
183316
183317 for(i=0; i<pIter->nPhrase; i++){
183318 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
183319 if( pPhrase->pTail ){
183320 char *pCsr = pPhrase->pTail;
183321 int iCsr = pPhrase->iTail;
183322
183323 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
183324 int j;
183325 u64 mPhrase = (u64)1 << (i%64);
183326 u64 mPos = (u64)1 << (iCsr - iStart);
183327 assert( iCsr>=iStart && (iCsr - iStart)<=64 );
183328 assert( i>=0 );
183329 if( (mCover|mCovered)&mPhrase ){
183330 iScore++;
183331 }else{
183332 iScore += 1000;
183333 }
183334 mCover |= mPhrase;
183335
183336 for(j=0; j<pPhrase->nToken; j++){
183337 mHighlight |= (mPos>>j);
183338 }
183339
183340 if( 0==(*pCsr & 0x0FE) ) break;
183341 fts3GetDeltaPosition(&pCsr, &iCsr);
183342 }
183343 }
183344 }
183345
183346 /* Set the output variables before returning. */
183347 *piToken = iStart;
183348 *piScore = iScore;
183349 *pmCover = mCover;
183350 *pmHighlight = mHighlight;
183351}
183352
183353/*
183354** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
183355** Each invocation populates an element of the SnippetIter.aPhrase[] array.
183356*/
183357static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
183358 SnippetIter *p = (SnippetIter *)ctx;
183359 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
183360 char *pCsr;
183361 int rc;
183362
183363 pPhrase->nToken = pExpr->pPhrase->nToken;
183364 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
183365 assert( rc==SQLITE_OK || pCsr==0 );
183366 if( pCsr ){
183367 int iFirst = 0;
183368 pPhrase->pList = pCsr;
183369 fts3GetDeltaPosition(&pCsr, &iFirst);
183370 if( iFirst<0 ){
183371 rc = FTS_CORRUPT_VTAB;
183372 }else{
183373 pPhrase->pHead = pCsr;
183374 pPhrase->pTail = pCsr;
183375 pPhrase->iHead = iFirst;
183376 pPhrase->iTail = iFirst;
183377 }
183378 }else{
183379 assert( rc!=SQLITE_OK || (
183380 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
183381 ));
183382 }
183383
183384 return rc;
183385}
183386
183387/*
183388** Select the fragment of text consisting of nFragment contiguous tokens
183389** from column iCol that represent the "best" snippet. The best snippet
183390** is the snippet with the highest score, where scores are calculated
183391** by adding:
183392**
183393** (a) +1 point for each occurrence of a matchable phrase in the snippet.
183394**
183395** (b) +1000 points for the first occurrence of each matchable phrase in
183396** the snippet for which the corresponding mCovered bit is not set.
183397**
183398** The selected snippet parameters are stored in structure *pFragment before
183399** returning. The score of the selected snippet is stored in *piScore
183400** before returning.
183401*/
183402static int fts3BestSnippet(
183403 int nSnippet, /* Desired snippet length */
183404 Fts3Cursor *pCsr, /* Cursor to create snippet for */
183405 int iCol, /* Index of column to create snippet from */
183406 u64 mCovered, /* Mask of phrases already covered */
183407 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
183408 SnippetFragment *pFragment, /* OUT: Best snippet found */
183409 int *piScore /* OUT: Score of snippet pFragment */
183410){
183411 int rc; /* Return Code */
183412 int nList; /* Number of phrases in expression */
183413 SnippetIter sIter; /* Iterates through snippet candidates */
183414 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
183415 int iBestScore = -1; /* Best snippet score found so far */
183416 int i; /* Loop counter */
183417
183418 memset(&sIter, 0, sizeof(sIter));
183419
183420 /* Iterate through the phrases in the expression to count them. The same
183421 ** callback makes sure the doclists are loaded for each phrase.
183422 */
183423 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
183424 if( rc!=SQLITE_OK ){
183425 return rc;
183426 }
183427
183428 /* Now that it is known how many phrases there are, allocate and zero
183429 ** the required space using malloc().
183430 */
183431 nByte = sizeof(SnippetPhrase) * nList;
183432 sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc64(nByte);
183433 if( !sIter.aPhrase ){
183434 return SQLITE_NOMEM;
183435 }
183436 memset(sIter.aPhrase, 0, nByte);
183437
183438 /* Initialize the contents of the SnippetIter object. Then iterate through
183439 ** the set of phrases in the expression to populate the aPhrase[] array.
183440 */
183441 sIter.pCsr = pCsr;
183442 sIter.iCol = iCol;
183443 sIter.nSnippet = nSnippet;
183444 sIter.nPhrase = nList;
183445 sIter.iCurrent = -1;
183446 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
183447 if( rc==SQLITE_OK ){
183448
183449 /* Set the *pmSeen output variable. */
183450 for(i=0; i<nList; i++){
183451 if( sIter.aPhrase[i].pHead ){
183452 *pmSeen |= (u64)1 << (i%64);
183453 }
183454 }
183455
183456 /* Loop through all candidate snippets. Store the best snippet in
183457 ** *pFragment. Store its associated 'score' in iBestScore.
183458 */
183459 pFragment->iCol = iCol;
183460 while( !fts3SnippetNextCandidate(&sIter) ){
183461 int iPos;
183462 int iScore;
183463 u64 mCover;
183464 u64 mHighlite;
183465 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
183466 assert( iScore>=0 );
183467 if( iScore>iBestScore ){
183468 pFragment->iPos = iPos;
183469 pFragment->hlmask = mHighlite;
183470 pFragment->covered = mCover;
183471 iBestScore = iScore;
183472 }
183473 }
183474
183475 *piScore = iBestScore;
183476 }
183477 sqlite3_free(sIter.aPhrase);
183478 return rc;
183479}
183480
183481
183482/*
183483** Append a string to the string-buffer passed as the first argument.
183484**
183485** If nAppend is negative, then the length of the string zAppend is
183486** determined using strlen().
183487*/
183488static int fts3StringAppend(
183489 StrBuffer *pStr, /* Buffer to append to */
183490 const char *zAppend, /* Pointer to data to append to buffer */
183491 int nAppend /* Size of zAppend in bytes (or -1) */
183492){
183493 if( nAppend<0 ){
183494 nAppend = (int)strlen(zAppend);
183495 }
183496
183497 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
183498 ** to grow the buffer until so that it is big enough to accomadate the
183499 ** appended data.
183500 */
183501 if( pStr->n+nAppend+1>=pStr->nAlloc ){
183502 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
183503 char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
183504 if( !zNew ){
183505 return SQLITE_NOMEM;
183506 }
183507 pStr->z = zNew;
183508 pStr->nAlloc = nAlloc;
183509 }
183510 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
183511
183512 /* Append the data to the string buffer. */
183513 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
183514 pStr->n += nAppend;
183515 pStr->z[pStr->n] = '\0';
183516
183517 return SQLITE_OK;
183518}
183519
183520/*
183521** The fts3BestSnippet() function often selects snippets that end with a
183522** query term. That is, the final term of the snippet is always a term
183523** that requires highlighting. For example, if 'X' is a highlighted term
183524** and '.' is a non-highlighted term, BestSnippet() may select:
183525**
183526** ........X.....X
183527**
183528** This function "shifts" the beginning of the snippet forward in the
183529** document so that there are approximately the same number of
183530** non-highlighted terms to the right of the final highlighted term as there
183531** are to the left of the first highlighted term. For example, to this:
183532**
183533** ....X.....X....
183534**
183535** This is done as part of extracting the snippet text, not when selecting
183536** the snippet. Snippet selection is done based on doclists only, so there
183537** is no way for fts3BestSnippet() to know whether or not the document
183538** actually contains terms that follow the final highlighted term.
183539*/
183540static int fts3SnippetShift(
183541 Fts3Table *pTab, /* FTS3 table snippet comes from */
183542 int iLangid, /* Language id to use in tokenizing */
183543 int nSnippet, /* Number of tokens desired for snippet */
183544 const char *zDoc, /* Document text to extract snippet from */
183545 int nDoc, /* Size of buffer zDoc in bytes */
183546 int *piPos, /* IN/OUT: First token of snippet */
183547 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
183548){
183549 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
183550
183551 if( hlmask ){
183552 int nLeft; /* Tokens to the left of first highlight */
183553 int nRight; /* Tokens to the right of last highlight */
183554 int nDesired; /* Ideal number of tokens to shift forward */
183555
183556 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
183557 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
183558 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 );
183559 nDesired = (nLeft-nRight)/2;
183560
183561 /* Ideally, the start of the snippet should be pushed forward in the
183562 ** document nDesired tokens. This block checks if there are actually
183563 ** nDesired tokens to the right of the snippet. If so, *piPos and
183564 ** *pHlMask are updated to shift the snippet nDesired tokens to the
183565 ** right. Otherwise, the snippet is shifted by the number of tokens
183566 ** available.
183567 */
183568 if( nDesired>0 ){
183569 int nShift; /* Number of tokens to shift snippet by */
183570 int iCurrent = 0; /* Token counter */
183571 int rc; /* Return Code */
183572 sqlite3_tokenizer_module *pMod;
183573 sqlite3_tokenizer_cursor *pC;
183574 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
183575
183576 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
183577 ** or more tokens in zDoc/nDoc.
183578 */
183579 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
183580 if( rc!=SQLITE_OK ){
183581 return rc;
183582 }
183583 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
183584 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
183585 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
183586 }
183587 pMod->xClose(pC);
183588 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
183589
183590 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
183591 assert( nShift<=nDesired );
183592 if( nShift>0 ){
183593 *piPos += nShift;
183594 *pHlmask = hlmask >> nShift;
183595 }
183596 }
183597 }
183598 return SQLITE_OK;
183599}
183600
183601/*
183602** Extract the snippet text for fragment pFragment from cursor pCsr and
183603** append it to string buffer pOut.
183604*/
183605static int fts3SnippetText(
183606 Fts3Cursor *pCsr, /* FTS3 Cursor */
183607 SnippetFragment *pFragment, /* Snippet to extract */
183608 int iFragment, /* Fragment number */
183609 int isLast, /* True for final fragment in snippet */
183610 int nSnippet, /* Number of tokens in extracted snippet */
183611 const char *zOpen, /* String inserted before highlighted term */
183612 const char *zClose, /* String inserted after highlighted term */
183613 const char *zEllipsis, /* String inserted between snippets */
183614 StrBuffer *pOut /* Write output here */
183615){
183616 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
183617 int rc; /* Return code */
183618 const char *zDoc; /* Document text to extract snippet from */
183619 int nDoc; /* Size of zDoc in bytes */
183620 int iCurrent = 0; /* Current token number of document */
183621 int iEnd = 0; /* Byte offset of end of current token */
183622 int isShiftDone = 0; /* True after snippet is shifted */
183623 int iPos = pFragment->iPos; /* First token of snippet */
183624 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
183625 int iCol = pFragment->iCol+1; /* Query column to extract text from */
183626 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
183627 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
183628
183629 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
183630 if( zDoc==0 ){
183631 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
183632 return SQLITE_NOMEM;
183633 }
183634 return SQLITE_OK;
183635 }
183636 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
183637
183638 /* Open a token cursor on the document. */
183639 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
183640 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
183641 if( rc!=SQLITE_OK ){
183642 return rc;
183643 }
183644
183645 while( rc==SQLITE_OK ){
183646 const char *ZDUMMY; /* Dummy argument used with tokenizer */
183647 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
183648 int iBegin = 0; /* Offset in zDoc of start of token */
183649 int iFin = 0; /* Offset in zDoc of end of token */
183650 int isHighlight = 0; /* True for highlighted terms */
183651
183652 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
183653 ** in the FTS code the variable that the third argument to xNext points to
183654 ** is initialized to zero before the first (*but not necessarily
183655 ** subsequent*) call to xNext(). This is done for a particular application
183656 ** that needs to know whether or not the tokenizer is being used for
183657 ** snippet generation or for some other purpose.
183658 **
183659 ** Extreme care is required when writing code to depend on this
183660 ** initialization. It is not a documented part of the tokenizer interface.
183661 ** If a tokenizer is used directly by any code outside of FTS, this
183662 ** convention might not be respected. */
183663 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
183664 if( rc!=SQLITE_OK ){
183665 if( rc==SQLITE_DONE ){
183666 /* Special case - the last token of the snippet is also the last token
183667 ** of the column. Append any punctuation that occurred between the end
183668 ** of the previous token and the end of the document to the output.
183669 ** Then break out of the loop. */
183670 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
183671 }
183672 break;
183673 }
183674 if( iCurrent<iPos ){ continue; }
183675
183676 if( !isShiftDone ){
183677 int n = nDoc - iBegin;
183678 rc = fts3SnippetShift(
183679 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
183680 );
183681 isShiftDone = 1;
183682
183683 /* Now that the shift has been done, check if the initial "..." are
183684 ** required. They are required if (a) this is not the first fragment,
183685 ** or (b) this fragment does not begin at position 0 of its column.
183686 */
183687 if( rc==SQLITE_OK ){
183688 if( iPos>0 || iFragment>0 ){
183689 rc = fts3StringAppend(pOut, zEllipsis, -1);
183690 }else if( iBegin ){
183691 rc = fts3StringAppend(pOut, zDoc, iBegin);
183692 }
183693 }
183694 if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
183695 }
183696
183697 if( iCurrent>=(iPos+nSnippet) ){
183698 if( isLast ){
183699 rc = fts3StringAppend(pOut, zEllipsis, -1);
183700 }
183701 break;
183702 }
183703
183704 /* Set isHighlight to true if this term should be highlighted. */
183705 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
183706
183707 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
183708 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
183709 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
183710 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
183711
183712 iEnd = iFin;
183713 }
183714
183715 pMod->xClose(pC);
183716 return rc;
183717}
183718
183719
183720/*
183721** This function is used to count the entries in a column-list (a
183722** delta-encoded list of term offsets within a single column of a single
183723** row). When this function is called, *ppCollist should point to the
183724** beginning of the first varint in the column-list (the varint that
183725** contains the position of the first matching term in the column data).
183726** Before returning, *ppCollist is set to point to the first byte after
183727** the last varint in the column-list (either the 0x00 signifying the end
183728** of the position-list, or the 0x01 that precedes the column number of
183729** the next column in the position-list).
183730**
183731** The number of elements in the column-list is returned.
183732*/
183733static int fts3ColumnlistCount(char **ppCollist){
183734 char *pEnd = *ppCollist;
183735 char c = 0;
183736 int nEntry = 0;
183737
183738 /* A column-list is terminated by either a 0x01 or 0x00. */
183739 while( 0xFE & (*pEnd | c) ){
183740 c = *pEnd++ & 0x80;
183741 if( !c ) nEntry++;
183742 }
183743
183744 *ppCollist = pEnd;
183745 return nEntry;
183746}
183747
183748/*
183749** This function gathers 'y' or 'b' data for a single phrase.
183750*/
183751static int fts3ExprLHits(
183752 Fts3Expr *pExpr, /* Phrase expression node */
183753 MatchInfo *p /* Matchinfo context */
183754){
183755 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
183756 int iStart;
183757 Fts3Phrase *pPhrase = pExpr->pPhrase;
183758 char *pIter = pPhrase->doclist.pList;
183759 int iCol = 0;
183760
183761 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
183762 if( p->flag==FTS3_MATCHINFO_LHITS ){
183763 iStart = pExpr->iPhrase * p->nCol;
183764 }else{
183765 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
183766 }
183767
183768 if( pIter ) while( 1 ){
183769 int nHit = fts3ColumnlistCount(&pIter);
183770 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
183771 if( p->flag==FTS3_MATCHINFO_LHITS ){
183772 p->aMatchinfo[iStart + iCol] = (u32)nHit;
183773 }else if( nHit ){
183774 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
183775 }
183776 }
183777 assert( *pIter==0x00 || *pIter==0x01 );
183778 if( *pIter!=0x01 ) break;
183779 pIter++;
183780 pIter += fts3GetVarint32(pIter, &iCol);
183781 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
183782 }
183783 return SQLITE_OK;
183784}
183785
183786/*
183787** Gather the results for matchinfo directives 'y' and 'b'.
183788*/
183789static int fts3ExprLHitGather(
183790 Fts3Expr *pExpr,
183791 MatchInfo *p
183792){
183793 int rc = SQLITE_OK;
183794 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
183795 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
183796 if( pExpr->pLeft ){
183797 rc = fts3ExprLHitGather(pExpr->pLeft, p);
183798 if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr->pRight, p);
183799 }else{
183800 rc = fts3ExprLHits(pExpr, p);
183801 }
183802 }
183803 return rc;
183804}
183805
183806/*
183807** fts3ExprIterate() callback used to collect the "global" matchinfo stats
183808** for a single query.
183809**
183810** fts3ExprIterate() callback to load the 'global' elements of a
183811** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
183812** of the matchinfo array that are constant for all rows returned by the
183813** current query.
183814**
183815** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
183816** function populates Matchinfo.aMatchinfo[] as follows:
183817**
183818** for(iCol=0; iCol<nCol; iCol++){
183819** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
183820** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
183821** }
183822**
183823** where X is the number of matches for phrase iPhrase is column iCol of all
183824** rows of the table. Y is the number of rows for which column iCol contains
183825** at least one instance of phrase iPhrase.
183826**
183827** If the phrase pExpr consists entirely of deferred tokens, then all X and
183828** Y values are set to nDoc, where nDoc is the number of documents in the
183829** file system. This is done because the full-text index doclist is required
183830** to calculate these values properly, and the full-text index doclist is
183831** not available for deferred tokens.
183832*/
183833static int fts3ExprGlobalHitsCb(
183834 Fts3Expr *pExpr, /* Phrase expression node */
183835 int iPhrase, /* Phrase number (numbered from zero) */
183836 void *pCtx /* Pointer to MatchInfo structure */
183837){
183838 MatchInfo *p = (MatchInfo *)pCtx;
183839 return sqlite3Fts3EvalPhraseStats(
183840 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
183841 );
183842}
183843
183844/*
183845** fts3ExprIterate() callback used to collect the "local" part of the
183846** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
183847** array that are different for each row returned by the query.
183848*/
183849static int fts3ExprLocalHitsCb(
183850 Fts3Expr *pExpr, /* Phrase expression node */
183851 int iPhrase, /* Phrase number */
183852 void *pCtx /* Pointer to MatchInfo structure */
183853){
183854 int rc = SQLITE_OK;
183855 MatchInfo *p = (MatchInfo *)pCtx;
183856 int iStart = iPhrase * p->nCol * 3;
183857 int i;
183858
183859 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
183860 char *pCsr;
183861 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
183862 if( pCsr ){
183863 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
183864 }else{
183865 p->aMatchinfo[iStart+i*3] = 0;
183866 }
183867 }
183868
183869 return rc;
183870}
183871
183872static int fts3MatchinfoCheck(
183873 Fts3Table *pTab,
183874 char cArg,
183875 char **pzErr
183876){
183877 if( (cArg==FTS3_MATCHINFO_NPHRASE)
183878 || (cArg==FTS3_MATCHINFO_NCOL)
183879 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
183880 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
183881 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
183882 || (cArg==FTS3_MATCHINFO_LCS)
183883 || (cArg==FTS3_MATCHINFO_HITS)
183884 || (cArg==FTS3_MATCHINFO_LHITS)
183885 || (cArg==FTS3_MATCHINFO_LHITS_BM)
183886 ){
183887 return SQLITE_OK;
183888 }
183889 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
183890 return SQLITE_ERROR;
183891}
183892
183893static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
183894 size_t nVal; /* Number of integers output by cArg */
183895
183896 switch( cArg ){
183897 case FTS3_MATCHINFO_NDOC:
183898 case FTS3_MATCHINFO_NPHRASE:
183899 case FTS3_MATCHINFO_NCOL:
183900 nVal = 1;
183901 break;
183902
183903 case FTS3_MATCHINFO_AVGLENGTH:
183904 case FTS3_MATCHINFO_LENGTH:
183905 case FTS3_MATCHINFO_LCS:
183906 nVal = pInfo->nCol;
183907 break;
183908
183909 case FTS3_MATCHINFO_LHITS:
183910 nVal = pInfo->nCol * pInfo->nPhrase;
183911 break;
183912
183913 case FTS3_MATCHINFO_LHITS_BM:
183914 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
183915 break;
183916
183917 default:
183918 assert( cArg==FTS3_MATCHINFO_HITS );
183919 nVal = pInfo->nCol * pInfo->nPhrase * 3;
183920 break;
183921 }
183922
183923 return nVal;
183924}
183925
183926static int fts3MatchinfoSelectDoctotal(
183927 Fts3Table *pTab,
183928 sqlite3_stmt **ppStmt,
183929 sqlite3_int64 *pnDoc,
183930 const char **paLen,
183931 const char **ppEnd
183932){
183933 sqlite3_stmt *pStmt;
183934 const char *a;
183935 const char *pEnd;
183936 sqlite3_int64 nDoc;
183937 int n;
183938
183939
183940 if( !*ppStmt ){
183941 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
183942 if( rc!=SQLITE_OK ) return rc;
183943 }
183944 pStmt = *ppStmt;
183945 assert( sqlite3_data_count(pStmt)==1 );
183946
183947 n = sqlite3_column_bytes(pStmt, 0);
183948 a = sqlite3_column_blob(pStmt, 0);
183949 if( a==0 ){
183950 return FTS_CORRUPT_VTAB;
183951 }
183952 pEnd = a + n;
183953 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
183954 if( nDoc<=0 || a>pEnd ){
183955 return FTS_CORRUPT_VTAB;
183956 }
183957 *pnDoc = nDoc;
183958
183959 if( paLen ) *paLen = a;
183960 if( ppEnd ) *ppEnd = pEnd;
183961 return SQLITE_OK;
183962}
183963
183964/*
183965** An instance of the following structure is used to store state while
183966** iterating through a multi-column position-list corresponding to the
183967** hits for a single phrase on a single row in order to calculate the
183968** values for a matchinfo() FTS3_MATCHINFO_LCS request.
183969*/
183970typedef struct LcsIterator LcsIterator;
183971struct LcsIterator {
183972 Fts3Expr *pExpr; /* Pointer to phrase expression */
183973 int iPosOffset; /* Tokens count up to end of this phrase */
183974 char *pRead; /* Cursor used to iterate through aDoclist */
183975 int iPos; /* Current position */
183976};
183977
183978/*
183979** If LcsIterator.iCol is set to the following value, the iterator has
183980** finished iterating through all offsets for all columns.
183981*/
183982#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
183983
183984static int fts3MatchinfoLcsCb(
183985 Fts3Expr *pExpr, /* Phrase expression node */
183986 int iPhrase, /* Phrase number (numbered from zero) */
183987 void *pCtx /* Pointer to MatchInfo structure */
183988){
183989 LcsIterator *aIter = (LcsIterator *)pCtx;
183990 aIter[iPhrase].pExpr = pExpr;
183991 return SQLITE_OK;
183992}
183993
183994/*
183995** Advance the iterator passed as an argument to the next position. Return
183996** 1 if the iterator is at EOF or if it now points to the start of the
183997** position list for the next column.
183998*/
183999static int fts3LcsIteratorAdvance(LcsIterator *pIter){
184000 char *pRead = pIter->pRead;
184001 sqlite3_int64 iRead;
184002 int rc = 0;
184003
184004 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
184005 if( iRead==0 || iRead==1 ){
184006 pRead = 0;
184007 rc = 1;
184008 }else{
184009 pIter->iPos += (int)(iRead-2);
184010 }
184011
184012 pIter->pRead = pRead;
184013 return rc;
184014}
184015
184016/*
184017** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
184018**
184019** If the call is successful, the longest-common-substring lengths for each
184020** column are written into the first nCol elements of the pInfo->aMatchinfo[]
184021** array before returning. SQLITE_OK is returned in this case.
184022**
184023** Otherwise, if an error occurs, an SQLite error code is returned and the
184024** data written to the first nCol elements of pInfo->aMatchinfo[] is
184025** undefined.
184026*/
184027static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
184028 LcsIterator *aIter;
184029 int i;
184030 int iCol;
184031 int nToken = 0;
184032 int rc = SQLITE_OK;
184033
184034 /* Allocate and populate the array of LcsIterator objects. The array
184035 ** contains one element for each matchable phrase in the query.
184036 **/
184037 aIter = sqlite3_malloc64(sizeof(LcsIterator) * pCsr->nPhrase);
184038 if( !aIter ) return SQLITE_NOMEM;
184039 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
184040 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
184041
184042 for(i=0; i<pInfo->nPhrase; i++){
184043 LcsIterator *pIter = &aIter[i];
184044 nToken -= pIter->pExpr->pPhrase->nToken;
184045 pIter->iPosOffset = nToken;
184046 }
184047
184048 for(iCol=0; iCol<pInfo->nCol; iCol++){
184049 int nLcs = 0; /* LCS value for this column */
184050 int nLive = 0; /* Number of iterators in aIter not at EOF */
184051
184052 for(i=0; i<pInfo->nPhrase; i++){
184053 LcsIterator *pIt = &aIter[i];
184054 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
184055 if( rc!=SQLITE_OK ) goto matchinfo_lcs_out;
184056 if( pIt->pRead ){
184057 pIt->iPos = pIt->iPosOffset;
184058 fts3LcsIteratorAdvance(pIt);
184059 if( pIt->pRead==0 ){
184060 rc = FTS_CORRUPT_VTAB;
184061 goto matchinfo_lcs_out;
184062 }
184063 nLive++;
184064 }
184065 }
184066
184067 while( nLive>0 ){
184068 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
184069 int nThisLcs = 0; /* LCS for the current iterator positions */
184070
184071 for(i=0; i<pInfo->nPhrase; i++){
184072 LcsIterator *pIter = &aIter[i];
184073 if( pIter->pRead==0 ){
184074 /* This iterator is already at EOF for this column. */
184075 nThisLcs = 0;
184076 }else{
184077 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
184078 pAdv = pIter;
184079 }
184080 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
184081 nThisLcs++;
184082 }else{
184083 nThisLcs = 1;
184084 }
184085 if( nThisLcs>nLcs ) nLcs = nThisLcs;
184086 }
184087 }
184088 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
184089 }
184090
184091 pInfo->aMatchinfo[iCol] = nLcs;
184092 }
184093
184094 matchinfo_lcs_out:
184095 sqlite3_free(aIter);
184096 return rc;
184097}
184098
184099/*
184100** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
184101** be returned by the matchinfo() function. Argument zArg contains the
184102** format string passed as the second argument to matchinfo (or the
184103** default value "pcx" if no second argument was specified). The format
184104** string has already been validated and the pInfo->aMatchinfo[] array
184105** is guaranteed to be large enough for the output.
184106**
184107** If bGlobal is true, then populate all fields of the matchinfo() output.
184108** If it is false, then assume that those fields that do not change between
184109** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
184110** have already been populated.
184111**
184112** Return SQLITE_OK if successful, or an SQLite error code if an error
184113** occurs. If a value other than SQLITE_OK is returned, the state the
184114** pInfo->aMatchinfo[] buffer is left in is undefined.
184115*/
184116static int fts3MatchinfoValues(
184117 Fts3Cursor *pCsr, /* FTS3 cursor object */
184118 int bGlobal, /* True to grab the global stats */
184119 MatchInfo *pInfo, /* Matchinfo context object */
184120 const char *zArg /* Matchinfo format string */
184121){
184122 int rc = SQLITE_OK;
184123 int i;
184124 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
184125 sqlite3_stmt *pSelect = 0;
184126
184127 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
184128 pInfo->flag = zArg[i];
184129 switch( zArg[i] ){
184130 case FTS3_MATCHINFO_NPHRASE:
184131 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
184132 break;
184133
184134 case FTS3_MATCHINFO_NCOL:
184135 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
184136 break;
184137
184138 case FTS3_MATCHINFO_NDOC:
184139 if( bGlobal ){
184140 sqlite3_int64 nDoc = 0;
184141 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0);
184142 pInfo->aMatchinfo[0] = (u32)nDoc;
184143 }
184144 break;
184145
184146 case FTS3_MATCHINFO_AVGLENGTH:
184147 if( bGlobal ){
184148 sqlite3_int64 nDoc; /* Number of rows in table */
184149 const char *a; /* Aggregate column length array */
184150 const char *pEnd; /* First byte past end of length array */
184151
184152 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd);
184153 if( rc==SQLITE_OK ){
184154 int iCol;
184155 for(iCol=0; iCol<pInfo->nCol; iCol++){
184156 u32 iVal;
184157 sqlite3_int64 nToken;
184158 a += sqlite3Fts3GetVarint(a, &nToken);
184159 if( a>pEnd ){
184160 rc = SQLITE_CORRUPT_VTAB;
184161 break;
184162 }
184163 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
184164 pInfo->aMatchinfo[iCol] = iVal;
184165 }
184166 }
184167 }
184168 break;
184169
184170 case FTS3_MATCHINFO_LENGTH: {
184171 sqlite3_stmt *pSelectDocsize = 0;
184172 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
184173 if( rc==SQLITE_OK ){
184174 int iCol;
184175 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
184176 const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0);
184177 for(iCol=0; iCol<pInfo->nCol; iCol++){
184178 sqlite3_int64 nToken;
184179 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken);
184180 if( a>pEnd ){
184181 rc = SQLITE_CORRUPT_VTAB;
184182 break;
184183 }
184184 pInfo->aMatchinfo[iCol] = (u32)nToken;
184185 }
184186 }
184187 sqlite3_reset(pSelectDocsize);
184188 break;
184189 }
184190
184191 case FTS3_MATCHINFO_LCS:
184192 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
184193 if( rc==SQLITE_OK ){
184194 rc = fts3MatchinfoLcs(pCsr, pInfo);
184195 }
184196 break;
184197
184198 case FTS3_MATCHINFO_LHITS_BM:
184199 case FTS3_MATCHINFO_LHITS: {
184200 size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
184201 memset(pInfo->aMatchinfo, 0, nZero);
184202 rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
184203 break;
184204 }
184205
184206 default: {
184207 Fts3Expr *pExpr;
184208 assert( zArg[i]==FTS3_MATCHINFO_HITS );
184209 pExpr = pCsr->pExpr;
184210 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
184211 if( rc!=SQLITE_OK ) break;
184212 if( bGlobal ){
184213 if( pCsr->pDeferred ){
184214 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0);
184215 if( rc!=SQLITE_OK ) break;
184216 }
184217 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
184218 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
184219 if( rc!=SQLITE_OK ) break;
184220 }
184221 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
184222 break;
184223 }
184224 }
184225
184226 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
184227 }
184228
184229 sqlite3_reset(pSelect);
184230 return rc;
184231}
184232
184233
184234/*
184235** Populate pCsr->aMatchinfo[] with data for the current row. The
184236** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
184237*/
184238static void fts3GetMatchinfo(
184239 sqlite3_context *pCtx, /* Return results here */
184240 Fts3Cursor *pCsr, /* FTS3 Cursor object */
184241 const char *zArg /* Second argument to matchinfo() function */
184242){
184243 MatchInfo sInfo;
184244 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
184245 int rc = SQLITE_OK;
184246 int bGlobal = 0; /* Collect 'global' stats as well as local */
184247
184248 u32 *aOut = 0;
184249 void (*xDestroyOut)(void*) = 0;
184250
184251 memset(&sInfo, 0, sizeof(MatchInfo));
184252 sInfo.pCursor = pCsr;
184253 sInfo.nCol = pTab->nColumn;
184254
184255 /* If there is cached matchinfo() data, but the format string for the
184256 ** cache does not match the format string for this request, discard
184257 ** the cached data. */
184258 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
184259 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
184260 pCsr->pMIBuffer = 0;
184261 }
184262
184263 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
184264 ** matchinfo function has been called for this query. In this case
184265 ** allocate the array used to accumulate the matchinfo data and
184266 ** initialize those elements that are constant for every row.
184267 */
184268 if( pCsr->pMIBuffer==0 ){
184269 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
184270 int i; /* Used to iterate through zArg */
184271
184272 /* Determine the number of phrases in the query */
184273 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
184274 sInfo.nPhrase = pCsr->nPhrase;
184275
184276 /* Determine the number of integers in the buffer returned by this call. */
184277 for(i=0; zArg[i]; i++){
184278 char *zErr = 0;
184279 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
184280 sqlite3_result_error(pCtx, zErr, -1);
184281 sqlite3_free(zErr);
184282 return;
184283 }
184284 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
184285 }
184286
184287 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
184288 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
184289 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
184290
184291 pCsr->isMatchinfoNeeded = 1;
184292 bGlobal = 1;
184293 }
184294
184295 if( rc==SQLITE_OK ){
184296 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
184297 if( xDestroyOut==0 ){
184298 rc = SQLITE_NOMEM;
184299 }
184300 }
184301
184302 if( rc==SQLITE_OK ){
184303 sInfo.aMatchinfo = aOut;
184304 sInfo.nPhrase = pCsr->nPhrase;
184305 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
184306 if( bGlobal ){
184307 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
184308 }
184309 }
184310
184311 if( rc!=SQLITE_OK ){
184312 sqlite3_result_error_code(pCtx, rc);
184313 if( xDestroyOut ) xDestroyOut(aOut);
184314 }else{
184315 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
184316 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
184317 }
184318}
184319
184320/*
184321** Implementation of snippet() function.
184322*/
184323SQLITE_PRIVATE void sqlite3Fts3Snippet(
184324 sqlite3_context *pCtx, /* SQLite function call context */
184325 Fts3Cursor *pCsr, /* Cursor object */
184326 const char *zStart, /* Snippet start text - "<b>" */
184327 const char *zEnd, /* Snippet end text - "</b>" */
184328 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
184329 int iCol, /* Extract snippet from this column */
184330 int nToken /* Approximate number of tokens in snippet */
184331){
184332 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
184333 int rc = SQLITE_OK;
184334 int i;
184335 StrBuffer res = {0, 0, 0};
184336
184337 /* The returned text includes up to four fragments of text extracted from
184338 ** the data in the current row. The first iteration of the for(...) loop
184339 ** below attempts to locate a single fragment of text nToken tokens in
184340 ** size that contains at least one instance of all phrases in the query
184341 ** expression that appear in the current row. If such a fragment of text
184342 ** cannot be found, the second iteration of the loop attempts to locate
184343 ** a pair of fragments, and so on.
184344 */
184345 int nSnippet = 0; /* Number of fragments in this snippet */
184346 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
184347 int nFToken = -1; /* Number of tokens in each fragment */
184348
184349 if( !pCsr->pExpr ){
184350 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
184351 return;
184352 }
184353
184354 /* Limit the snippet length to 64 tokens. */
184355 if( nToken<-64 ) nToken = -64;
184356 if( nToken>+64 ) nToken = +64;
184357
184358 for(nSnippet=1; 1; nSnippet++){
184359
184360 int iSnip; /* Loop counter 0..nSnippet-1 */
184361 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
184362 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
184363
184364 if( nToken>=0 ){
184365 nFToken = (nToken+nSnippet-1) / nSnippet;
184366 }else{
184367 nFToken = -1 * nToken;
184368 }
184369
184370 for(iSnip=0; iSnip<nSnippet; iSnip++){
184371 int iBestScore = -1; /* Best score of columns checked so far */
184372 int iRead; /* Used to iterate through columns */
184373 SnippetFragment *pFragment = &aSnippet[iSnip];
184374
184375 memset(pFragment, 0, sizeof(*pFragment));
184376
184377 /* Loop through all columns of the table being considered for snippets.
184378 ** If the iCol argument to this function was negative, this means all
184379 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
184380 */
184381 for(iRead=0; iRead<pTab->nColumn; iRead++){
184382 SnippetFragment sF = {0, 0, 0, 0};
184383 int iS = 0;
184384 if( iCol>=0 && iRead!=iCol ) continue;
184385
184386 /* Find the best snippet of nFToken tokens in column iRead. */
184387 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
184388 if( rc!=SQLITE_OK ){
184389 goto snippet_out;
184390 }
184391 if( iS>iBestScore ){
184392 *pFragment = sF;
184393 iBestScore = iS;
184394 }
184395 }
184396
184397 mCovered |= pFragment->covered;
184398 }
184399
184400 /* If all query phrases seen by fts3BestSnippet() are present in at least
184401 ** one of the nSnippet snippet fragments, break out of the loop.
184402 */
184403 assert( (mCovered&mSeen)==mCovered );
184404 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
184405 }
184406
184407 assert( nFToken>0 );
184408
184409 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
184410 rc = fts3SnippetText(pCsr, &aSnippet[i],
184411 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
184412 );
184413 }
184414
184415 snippet_out:
184416 sqlite3Fts3SegmentsClose(pTab);
184417 if( rc!=SQLITE_OK ){
184418 sqlite3_result_error_code(pCtx, rc);
184419 sqlite3_free(res.z);
184420 }else{
184421 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
184422 }
184423}
184424
184425
184426typedef struct TermOffset TermOffset;
184427typedef struct TermOffsetCtx TermOffsetCtx;
184428
184429struct TermOffset {
184430 char *pList; /* Position-list */
184431 int iPos; /* Position just read from pList */
184432 int iOff; /* Offset of this term from read positions */
184433};
184434
184435struct TermOffsetCtx {
184436 Fts3Cursor *pCsr;
184437 int iCol; /* Column of table to populate aTerm for */
184438 int iTerm;
184439 sqlite3_int64 iDocid;
184440 TermOffset *aTerm;
184441};
184442
184443/*
184444** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
184445*/
184446static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
184447 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
184448 int nTerm; /* Number of tokens in phrase */
184449 int iTerm; /* For looping through nTerm phrase terms */
184450 char *pList; /* Pointer to position list for phrase */
184451 int iPos = 0; /* First position in position-list */
184452 int rc;
184453
184454 UNUSED_PARAMETER(iPhrase);
184455 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
184456 nTerm = pExpr->pPhrase->nToken;
184457 if( pList ){
184458 fts3GetDeltaPosition(&pList, &iPos);
184459 assert_fts3_nc( iPos>=0 );
184460 }
184461
184462 for(iTerm=0; iTerm<nTerm; iTerm++){
184463 TermOffset *pT = &p->aTerm[p->iTerm++];
184464 pT->iOff = nTerm-iTerm-1;
184465 pT->pList = pList;
184466 pT->iPos = iPos;
184467 }
184468
184469 return rc;
184470}
184471
184472/*
184473** Implementation of offsets() function.
184474*/
184475SQLITE_PRIVATE void sqlite3Fts3Offsets(
184476 sqlite3_context *pCtx, /* SQLite function call context */
184477 Fts3Cursor *pCsr /* Cursor object */
184478){
184479 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
184480 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
184481 int rc; /* Return Code */
184482 int nToken; /* Number of tokens in query */
184483 int iCol; /* Column currently being processed */
184484 StrBuffer res = {0, 0, 0}; /* Result string */
184485 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
184486
184487 if( !pCsr->pExpr ){
184488 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
184489 return;
184490 }
184491
184492 memset(&sCtx, 0, sizeof(sCtx));
184493 assert( pCsr->isRequireSeek==0 );
184494
184495 /* Count the number of terms in the query */
184496 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
184497 if( rc!=SQLITE_OK ) goto offsets_out;
184498
184499 /* Allocate the array of TermOffset iterators. */
184500 sCtx.aTerm = (TermOffset *)sqlite3_malloc64(sizeof(TermOffset)*nToken);
184501 if( 0==sCtx.aTerm ){
184502 rc = SQLITE_NOMEM;
184503 goto offsets_out;
184504 }
184505 sCtx.iDocid = pCsr->iPrevId;
184506 sCtx.pCsr = pCsr;
184507
184508 /* Loop through the table columns, appending offset information to
184509 ** string-buffer res for each column.
184510 */
184511 for(iCol=0; iCol<pTab->nColumn; iCol++){
184512 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
184513 const char *ZDUMMY; /* Dummy argument used with xNext() */
184514 int NDUMMY = 0; /* Dummy argument used with xNext() */
184515 int iStart = 0;
184516 int iEnd = 0;
184517 int iCurrent = 0;
184518 const char *zDoc;
184519 int nDoc;
184520
184521 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
184522 ** no way that this operation can fail, so the return code from
184523 ** fts3ExprIterate() can be discarded.
184524 */
184525 sCtx.iCol = iCol;
184526 sCtx.iTerm = 0;
184527 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
184528
184529 /* Retreive the text stored in column iCol. If an SQL NULL is stored
184530 ** in column iCol, jump immediately to the next iteration of the loop.
184531 ** If an OOM occurs while retrieving the data (this can happen if SQLite
184532 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
184533 ** to the caller.
184534 */
184535 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
184536 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
184537 if( zDoc==0 ){
184538 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
184539 continue;
184540 }
184541 rc = SQLITE_NOMEM;
184542 goto offsets_out;
184543 }
184544
184545 /* Initialize a tokenizer iterator to iterate through column iCol. */
184546 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
184547 zDoc, nDoc, &pC
184548 );
184549 if( rc!=SQLITE_OK ) goto offsets_out;
184550
184551 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
184552 while( rc==SQLITE_OK ){
184553 int i; /* Used to loop through terms */
184554 int iMinPos = 0x7FFFFFFF; /* Position of next token */
184555 TermOffset *pTerm = 0; /* TermOffset associated with next token */
184556
184557 for(i=0; i<nToken; i++){
184558 TermOffset *pT = &sCtx.aTerm[i];
184559 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
184560 iMinPos = pT->iPos-pT->iOff;
184561 pTerm = pT;
184562 }
184563 }
184564
184565 if( !pTerm ){
184566 /* All offsets for this column have been gathered. */
184567 rc = SQLITE_DONE;
184568 }else{
184569 assert_fts3_nc( iCurrent<=iMinPos );
184570 if( 0==(0xFE&*pTerm->pList) ){
184571 pTerm->pList = 0;
184572 }else{
184573 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
184574 }
184575 while( rc==SQLITE_OK && iCurrent<iMinPos ){
184576 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
184577 }
184578 if( rc==SQLITE_OK ){
184579 char aBuffer[64];
184580 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
184581 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
184582 );
184583 rc = fts3StringAppend(&res, aBuffer, -1);
184584 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
184585 rc = FTS_CORRUPT_VTAB;
184586 }
184587 }
184588 }
184589 if( rc==SQLITE_DONE ){
184590 rc = SQLITE_OK;
184591 }
184592
184593 pMod->xClose(pC);
184594 if( rc!=SQLITE_OK ) goto offsets_out;
184595 }
184596
184597 offsets_out:
184598 sqlite3_free(sCtx.aTerm);
184599 assert( rc!=SQLITE_DONE );
184600 sqlite3Fts3SegmentsClose(pTab);
184601 if( rc!=SQLITE_OK ){
184602 sqlite3_result_error_code(pCtx, rc);
184603 sqlite3_free(res.z);
184604 }else{
184605 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
184606 }
184607 return;
184608}
184609
184610/*
184611** Implementation of matchinfo() function.
184612*/
184613SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
184614 sqlite3_context *pContext, /* Function call context */
184615 Fts3Cursor *pCsr, /* FTS3 table cursor */
184616 const char *zArg /* Second arg to matchinfo() function */
184617){
184618 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
184619 const char *zFormat;
184620
184621 if( zArg ){
184622 zFormat = zArg;
184623 }else{
184624 zFormat = FTS3_MATCHINFO_DEFAULT;
184625 }
184626
184627 if( !pCsr->pExpr ){
184628 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
184629 return;
184630 }else{
184631 /* Retrieve matchinfo() data. */
184632 fts3GetMatchinfo(pContext, pCsr, zFormat);
184633 sqlite3Fts3SegmentsClose(pTab);
184634 }
184635}
184636
184637#endif
184638
184639/************** End of fts3_snippet.c ****************************************/
184640/************** Begin file fts3_unicode.c ************************************/
184641/*
184642** 2012 May 24
184643**
184644** The author disclaims copyright to this source code. In place of
184645** a legal notice, here is a blessing:
184646**
184647** May you do good and not evil.
184648** May you find forgiveness for yourself and forgive others.
184649** May you share freely, never taking more than you give.
184650**
184651******************************************************************************
184652**
184653** Implementation of the "unicode" full-text-search tokenizer.
184654*/
184655
184656#ifndef SQLITE_DISABLE_FTS3_UNICODE
184657
184658/* #include "fts3Int.h" */
184659#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
184660
184661/* #include <assert.h> */
184662/* #include <stdlib.h> */
184663/* #include <stdio.h> */
184664/* #include <string.h> */
184665
184666/* #include "fts3_tokenizer.h" */
184667
184668/*
184669** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
184670** from the sqlite3 source file utf.c. If this file is compiled as part
184671** of the amalgamation, they are not required.
184672*/
184673#ifndef SQLITE_AMALGAMATION
184674
184675static const unsigned char sqlite3Utf8Trans1[] = {
184676 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184677 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
184678 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
184679 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
184680 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184681 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
184682 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184683 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
184684};
184685
184686#define READ_UTF8(zIn, zTerm, c) \
184687 c = *(zIn++); \
184688 if( c>=0xc0 ){ \
184689 c = sqlite3Utf8Trans1[c-0xc0]; \
184690 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
184691 c = (c<<6) + (0x3f & *(zIn++)); \
184692 } \
184693 if( c<0x80 \
184694 || (c&0xFFFFF800)==0xD800 \
184695 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
184696 }
184697
184698#define WRITE_UTF8(zOut, c) { \
184699 if( c<0x00080 ){ \
184700 *zOut++ = (u8)(c&0xFF); \
184701 } \
184702 else if( c<0x00800 ){ \
184703 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
184704 *zOut++ = 0x80 + (u8)(c & 0x3F); \
184705 } \
184706 else if( c<0x10000 ){ \
184707 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
184708 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
184709 *zOut++ = 0x80 + (u8)(c & 0x3F); \
184710 }else{ \
184711 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
184712 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
184713 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
184714 *zOut++ = 0x80 + (u8)(c & 0x3F); \
184715 } \
184716}
184717
184718#endif /* ifndef SQLITE_AMALGAMATION */
184719
184720typedef struct unicode_tokenizer unicode_tokenizer;
184721typedef struct unicode_cursor unicode_cursor;
184722
184723struct unicode_tokenizer {
184724 sqlite3_tokenizer base;
184725 int eRemoveDiacritic;
184726 int nException;
184727 int *aiException;
184728};
184729
184730struct unicode_cursor {
184731 sqlite3_tokenizer_cursor base;
184732 const unsigned char *aInput; /* Input text being tokenized */
184733 int nInput; /* Size of aInput[] in bytes */
184734 int iOff; /* Current offset within aInput[] */
184735 int iToken; /* Index of next token to be returned */
184736 char *zToken; /* storage for current token */
184737 int nAlloc; /* space allocated at zToken */
184738};
184739
184740
184741/*
184742** Destroy a tokenizer allocated by unicodeCreate().
184743*/
184744static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
184745 if( pTokenizer ){
184746 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
184747 sqlite3_free(p->aiException);
184748 sqlite3_free(p);
184749 }
184750 return SQLITE_OK;
184751}
184752
184753/*
184754** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
184755** statement has specified that the tokenizer for this table shall consider
184756** all characters in string zIn/nIn to be separators (if bAlnum==0) or
184757** token characters (if bAlnum==1).
184758**
184759** For each codepoint in the zIn/nIn string, this function checks if the
184760** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
184761** If so, no action is taken. Otherwise, the codepoint is added to the
184762** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
184763** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
184764** codepoints in the aiException[] array.
184765**
184766** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
184767** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
184768** It is not possible to change the behavior of the tokenizer with respect
184769** to these codepoints.
184770*/
184771static int unicodeAddExceptions(
184772 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
184773 int bAlnum, /* Replace Isalnum() return value with this */
184774 const char *zIn, /* Array of characters to make exceptions */
184775 int nIn /* Length of z in bytes */
184776){
184777 const unsigned char *z = (const unsigned char *)zIn;
184778 const unsigned char *zTerm = &z[nIn];
184779 unsigned int iCode;
184780 int nEntry = 0;
184781
184782 assert( bAlnum==0 || bAlnum==1 );
184783
184784 while( z<zTerm ){
184785 READ_UTF8(z, zTerm, iCode);
184786 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
184787 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
184788 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
184789 ){
184790 nEntry++;
184791 }
184792 }
184793
184794 if( nEntry ){
184795 int *aNew; /* New aiException[] array */
184796 int nNew; /* Number of valid entries in array aNew[] */
184797
184798 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
184799 if( aNew==0 ) return SQLITE_NOMEM;
184800 nNew = p->nException;
184801
184802 z = (const unsigned char *)zIn;
184803 while( z<zTerm ){
184804 READ_UTF8(z, zTerm, iCode);
184805 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
184806 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
184807 ){
184808 int i, j;
184809 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
184810 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
184811 aNew[i] = (int)iCode;
184812 nNew++;
184813 }
184814 }
184815 p->aiException = aNew;
184816 p->nException = nNew;
184817 }
184818
184819 return SQLITE_OK;
184820}
184821
184822/*
184823** Return true if the p->aiException[] array contains the value iCode.
184824*/
184825static int unicodeIsException(unicode_tokenizer *p, int iCode){
184826 if( p->nException>0 ){
184827 int *a = p->aiException;
184828 int iLo = 0;
184829 int iHi = p->nException-1;
184830
184831 while( iHi>=iLo ){
184832 int iTest = (iHi + iLo) / 2;
184833 if( iCode==a[iTest] ){
184834 return 1;
184835 }else if( iCode>a[iTest] ){
184836 iLo = iTest+1;
184837 }else{
184838 iHi = iTest-1;
184839 }
184840 }
184841 }
184842
184843 return 0;
184844}
184845
184846/*
184847** Return true if, for the purposes of tokenization, codepoint iCode is
184848** considered a token character (not a separator).
184849*/
184850static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
184851 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
184852 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
184853}
184854
184855/*
184856** Create a new tokenizer instance.
184857*/
184858static int unicodeCreate(
184859 int nArg, /* Size of array argv[] */
184860 const char * const *azArg, /* Tokenizer creation arguments */
184861 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
184862){
184863 unicode_tokenizer *pNew; /* New tokenizer object */
184864 int i;
184865 int rc = SQLITE_OK;
184866
184867 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
184868 if( pNew==NULL ) return SQLITE_NOMEM;
184869 memset(pNew, 0, sizeof(unicode_tokenizer));
184870 pNew->eRemoveDiacritic = 1;
184871
184872 for(i=0; rc==SQLITE_OK && i<nArg; i++){
184873 const char *z = azArg[i];
184874 int n = (int)strlen(z);
184875
184876 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
184877 pNew->eRemoveDiacritic = 1;
184878 }
184879 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
184880 pNew->eRemoveDiacritic = 0;
184881 }
184882 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
184883 pNew->eRemoveDiacritic = 2;
184884 }
184885 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
184886 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
184887 }
184888 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
184889 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
184890 }
184891 else{
184892 /* Unrecognized argument */
184893 rc = SQLITE_ERROR;
184894 }
184895 }
184896
184897 if( rc!=SQLITE_OK ){
184898 unicodeDestroy((sqlite3_tokenizer *)pNew);
184899 pNew = 0;
184900 }
184901 *pp = (sqlite3_tokenizer *)pNew;
184902 return rc;
184903}
184904
184905/*
184906** Prepare to begin tokenizing a particular string. The input
184907** string to be tokenized is pInput[0..nBytes-1]. A cursor
184908** used to incrementally tokenize this string is returned in
184909** *ppCursor.
184910*/
184911static int unicodeOpen(
184912 sqlite3_tokenizer *p, /* The tokenizer */
184913 const char *aInput, /* Input string */
184914 int nInput, /* Size of string aInput in bytes */
184915 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
184916){
184917 unicode_cursor *pCsr;
184918
184919 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
184920 if( pCsr==0 ){
184921 return SQLITE_NOMEM;
184922 }
184923 memset(pCsr, 0, sizeof(unicode_cursor));
184924
184925 pCsr->aInput = (const unsigned char *)aInput;
184926 if( aInput==0 ){
184927 pCsr->nInput = 0;
184928 }else if( nInput<0 ){
184929 pCsr->nInput = (int)strlen(aInput);
184930 }else{
184931 pCsr->nInput = nInput;
184932 }
184933
184934 *pp = &pCsr->base;
184935 UNUSED_PARAMETER(p);
184936 return SQLITE_OK;
184937}
184938
184939/*
184940** Close a tokenization cursor previously opened by a call to
184941** simpleOpen() above.
184942*/
184943static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
184944 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
184945 sqlite3_free(pCsr->zToken);
184946 sqlite3_free(pCsr);
184947 return SQLITE_OK;
184948}
184949
184950/*
184951** Extract the next token from a tokenization cursor. The cursor must
184952** have been opened by a prior call to simpleOpen().
184953*/
184954static int unicodeNext(
184955 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
184956 const char **paToken, /* OUT: Token text */
184957 int *pnToken, /* OUT: Number of bytes at *paToken */
184958 int *piStart, /* OUT: Starting offset of token */
184959 int *piEnd, /* OUT: Ending offset of token */
184960 int *piPos /* OUT: Position integer of token */
184961){
184962 unicode_cursor *pCsr = (unicode_cursor *)pC;
184963 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
184964 unsigned int iCode = 0;
184965 char *zOut;
184966 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
184967 const unsigned char *zStart = z;
184968 const unsigned char *zEnd;
184969 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
184970
184971 /* Scan past any delimiter characters before the start of the next token.
184972 ** Return SQLITE_DONE early if this takes us all the way to the end of
184973 ** the input. */
184974 while( z<zTerm ){
184975 READ_UTF8(z, zTerm, iCode);
184976 if( unicodeIsAlnum(p, (int)iCode) ) break;
184977 zStart = z;
184978 }
184979 if( zStart>=zTerm ) return SQLITE_DONE;
184980
184981 zOut = pCsr->zToken;
184982 do {
184983 int iOut;
184984
184985 /* Grow the output buffer if required. */
184986 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
184987 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
184988 if( !zNew ) return SQLITE_NOMEM;
184989 zOut = &zNew[zOut - pCsr->zToken];
184990 pCsr->zToken = zNew;
184991 pCsr->nAlloc += 64;
184992 }
184993
184994 /* Write the folded case of the last character read to the output */
184995 zEnd = z;
184996 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
184997 if( iOut ){
184998 WRITE_UTF8(zOut, iOut);
184999 }
185000
185001 /* If the cursor is not at EOF, read the next character */
185002 if( z>=zTerm ) break;
185003 READ_UTF8(z, zTerm, iCode);
185004 }while( unicodeIsAlnum(p, (int)iCode)
185005 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
185006 );
185007
185008 /* Set the output variables and return. */
185009 pCsr->iOff = (int)(z - pCsr->aInput);
185010 *paToken = pCsr->zToken;
185011 *pnToken = (int)(zOut - pCsr->zToken);
185012 *piStart = (int)(zStart - pCsr->aInput);
185013 *piEnd = (int)(zEnd - pCsr->aInput);
185014 *piPos = pCsr->iToken++;
185015 return SQLITE_OK;
185016}
185017
185018/*
185019** Set *ppModule to a pointer to the sqlite3_tokenizer_module
185020** structure for the unicode tokenizer.
185021*/
185022SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
185023 static const sqlite3_tokenizer_module module = {
185024 0,
185025 unicodeCreate,
185026 unicodeDestroy,
185027 unicodeOpen,
185028 unicodeClose,
185029 unicodeNext,
185030 0,
185031 };
185032 *ppModule = &module;
185033}
185034
185035#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
185036#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
185037
185038/************** End of fts3_unicode.c ****************************************/
185039/************** Begin file fts3_unicode2.c ***********************************/
185040/*
185041** 2012-05-25
185042**
185043** The author disclaims copyright to this source code. In place of
185044** a legal notice, here is a blessing:
185045**
185046** May you do good and not evil.
185047** May you find forgiveness for yourself and forgive others.
185048** May you share freely, never taking more than you give.
185049**
185050******************************************************************************
185051*/
185052
185053/*
185054** DO NOT EDIT THIS MACHINE GENERATED FILE.
185055*/
185056
185057#ifndef SQLITE_DISABLE_FTS3_UNICODE
185058#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
185059
185060/* #include <assert.h> */
185061
185062/*
185063** Return true if the argument corresponds to a unicode codepoint
185064** classified as either a letter or a number. Otherwise false.
185065**
185066** The results are undefined if the value passed to this function
185067** is less than zero.
185068*/
185069SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
185070 /* Each unsigned integer in the following array corresponds to a contiguous
185071 ** range of unicode codepoints that are not either letters or numbers (i.e.
185072 ** codepoints for which this function should return 0).
185073 **
185074 ** The most significant 22 bits in each 32-bit value contain the first
185075 ** codepoint in the range. The least significant 10 bits are used to store
185076 ** the size of the range (always at least 1). In other words, the value
185077 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
185078 ** C. It is not possible to represent a range larger than 1023 codepoints
185079 ** using this format.
185080 */
185081 static const unsigned int aEntry[] = {
185082 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
185083 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
185084 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
185085 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
185086 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
185087 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
185088 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
185089 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
185090 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
185091 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
185092 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
185093 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
185094 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
185095 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
185096 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
185097 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
185098 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
185099 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
185100 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
185101 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
185102 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
185103 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
185104 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
185105 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
185106 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
185107 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
185108 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
185109 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
185110 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
185111 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
185112 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
185113 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
185114 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
185115 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
185116 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
185117 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
185118 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
185119 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
185120 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
185121 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
185122 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
185123 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
185124 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
185125 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
185126 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
185127 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
185128 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
185129 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
185130 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
185131 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
185132 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
185133 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
185134 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
185135 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
185136 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
185137 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
185138 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
185139 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
185140 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
185141 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
185142 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
185143 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
185144 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
185145 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
185146 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
185147 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
185148 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
185149 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
185150 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
185151 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
185152 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
185153 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
185154 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
185155 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
185156 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
185157 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
185158 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
185159 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
185160 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
185161 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
185162 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
185163 0x380400F0,
185164 };
185165 static const unsigned int aAscii[4] = {
185166 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
185167 };
185168
185169 if( (unsigned int)c<128 ){
185170 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
185171 }else if( (unsigned int)c<(1<<22) ){
185172 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
185173 int iRes = 0;
185174 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
185175 int iLo = 0;
185176 while( iHi>=iLo ){
185177 int iTest = (iHi + iLo) / 2;
185178 if( key >= aEntry[iTest] ){
185179 iRes = iTest;
185180 iLo = iTest+1;
185181 }else{
185182 iHi = iTest-1;
185183 }
185184 }
185185 assert( aEntry[0]<key );
185186 assert( key>=aEntry[iRes] );
185187 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
185188 }
185189 return 1;
185190}
185191
185192
185193/*
185194** If the argument is a codepoint corresponding to a lowercase letter
185195** in the ASCII range with a diacritic added, return the codepoint
185196** of the ASCII letter only. For example, if passed 235 - "LATIN
185197** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
185198** E"). The resuls of passing a codepoint that corresponds to an
185199** uppercase letter are undefined.
185200*/
185201static int remove_diacritic(int c, int bComplex){
185202 unsigned short aDia[] = {
185203 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
185204 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
185205 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
185206 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
185207 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
185208 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
185209 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
185210 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
185211 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
185212 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
185213 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
185214 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
185215 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
185216 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
185217 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
185218 63182, 63242, 63274, 63310, 63368, 63390,
185219 };
185220#define HIBIT ((unsigned char)0x80)
185221 unsigned char aChar[] = {
185222 '\0', 'a', 'c', 'e', 'i', 'n',
185223 'o', 'u', 'y', 'y', 'a', 'c',
185224 'd', 'e', 'e', 'g', 'h', 'i',
185225 'j', 'k', 'l', 'n', 'o', 'r',
185226 's', 't', 'u', 'u', 'w', 'y',
185227 'z', 'o', 'u', 'a', 'i', 'o',
185228 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
185229 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
185230 'e', 'i', 'o', 'r', 'u', 's',
185231 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
185232 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
185233 '\0', '\0', '\0', '\0', 'a', 'b',
185234 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
185235 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
185236 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
185237 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
185238 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
185239 'w', 'x', 'y', 'z', 'h', 't',
185240 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
185241 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
185242 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
185243 };
185244
185245 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
185246 int iRes = 0;
185247 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
185248 int iLo = 0;
185249 while( iHi>=iLo ){
185250 int iTest = (iHi + iLo) / 2;
185251 if( key >= aDia[iTest] ){
185252 iRes = iTest;
185253 iLo = iTest+1;
185254 }else{
185255 iHi = iTest-1;
185256 }
185257 }
185258 assert( key>=aDia[iRes] );
185259 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
185260 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
185261}
185262
185263
185264/*
185265** Return true if the argument interpreted as a unicode codepoint
185266** is a diacritical modifier character.
185267*/
185268SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
185269 unsigned int mask0 = 0x08029FDF;
185270 unsigned int mask1 = 0x000361F8;
185271 if( c<768 || c>817 ) return 0;
185272 return (c < 768+32) ?
185273 (mask0 & ((unsigned int)1 << (c-768))) :
185274 (mask1 & ((unsigned int)1 << (c-768-32)));
185275}
185276
185277
185278/*
185279** Interpret the argument as a unicode codepoint. If the codepoint
185280** is an upper case character that has a lower case equivalent,
185281** return the codepoint corresponding to the lower case version.
185282** Otherwise, return a copy of the argument.
185283**
185284** The results are undefined if the value passed to this function
185285** is less than zero.
185286*/
185287SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
185288 /* Each entry in the following array defines a rule for folding a range
185289 ** of codepoints to lower case. The rule applies to a range of nRange
185290 ** codepoints starting at codepoint iCode.
185291 **
185292 ** If the least significant bit in flags is clear, then the rule applies
185293 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
185294 ** need to be folded). Or, if it is set, then the rule only applies to
185295 ** every second codepoint in the range, starting with codepoint C.
185296 **
185297 ** The 7 most significant bits in flags are an index into the aiOff[]
185298 ** array. If a specific codepoint C does require folding, then its lower
185299 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
185300 **
185301 ** The contents of this array are generated by parsing the CaseFolding.txt
185302 ** file distributed as part of the "Unicode Character Database". See
185303 ** http://www.unicode.org for details.
185304 */
185305 static const struct TableEntry {
185306 unsigned short iCode;
185307 unsigned char flags;
185308 unsigned char nRange;
185309 } aEntry[] = {
185310 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
185311 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
185312 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
185313 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
185314 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
185315 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
185316 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
185317 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
185318 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
185319 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
185320 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
185321 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
185322 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
185323 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
185324 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
185325 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
185326 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
185327 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
185328 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
185329 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
185330 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
185331 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
185332 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
185333 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
185334 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
185335 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
185336 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
185337 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
185338 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
185339 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
185340 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
185341 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
185342 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
185343 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
185344 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
185345 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
185346 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
185347 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
185348 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
185349 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
185350 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
185351 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
185352 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
185353 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
185354 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
185355 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
185356 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
185357 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
185358 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
185359 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
185360 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
185361 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
185362 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
185363 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
185364 {65313, 14, 26},
185365 };
185366 static const unsigned short aiOff[] = {
185367 1, 2, 8, 15, 16, 26, 28, 32,
185368 37, 38, 40, 48, 63, 64, 69, 71,
185369 79, 80, 116, 202, 203, 205, 206, 207,
185370 209, 210, 211, 213, 214, 217, 218, 219,
185371 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
185372 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
185373 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
185374 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
185375 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
185376 65514, 65521, 65527, 65528, 65529,
185377 };
185378
185379 int ret = c;
185380
185381 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
185382
185383 if( c<128 ){
185384 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
185385 }else if( c<65536 ){
185386 const struct TableEntry *p;
185387 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
185388 int iLo = 0;
185389 int iRes = -1;
185390
185391 assert( c>aEntry[0].iCode );
185392 while( iHi>=iLo ){
185393 int iTest = (iHi + iLo) / 2;
185394 int cmp = (c - aEntry[iTest].iCode);
185395 if( cmp>=0 ){
185396 iRes = iTest;
185397 iLo = iTest+1;
185398 }else{
185399 iHi = iTest-1;
185400 }
185401 }
185402
185403 assert( iRes>=0 && c>=aEntry[iRes].iCode );
185404 p = &aEntry[iRes];
185405 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
185406 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
185407 assert( ret>0 );
185408 }
185409
185410 if( eRemoveDiacritic ){
185411 ret = remove_diacritic(ret, eRemoveDiacritic==2);
185412 }
185413 }
185414
185415 else if( c>=66560 && c<66600 ){
185416 ret = c + 40;
185417 }
185418
185419 return ret;
185420}
185421#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
185422#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
185423
185424/************** End of fts3_unicode2.c ***************************************/
185425/************** Begin file json1.c *******************************************/
185426/*
185427** 2015-08-12
185428**
185429** The author disclaims copyright to this source code. In place of
185430** a legal notice, here is a blessing:
185431**
185432** May you do good and not evil.
185433** May you find forgiveness for yourself and forgive others.
185434** May you share freely, never taking more than you give.
185435**
185436******************************************************************************
185437**
185438** This SQLite extension implements JSON functions. The interface is
185439** modeled after MySQL JSON functions:
185440**
185441** https://dev.mysql.com/doc/refman/5.7/en/json.html
185442**
185443** For the time being, all JSON is stored as pure text. (We might add
185444** a JSONB type in the future which stores a binary encoding of JSON in
185445** a BLOB, but there is no support for JSONB in the current implementation.
185446** This implementation parses JSON text at 250 MB/s, so it is hard to see
185447** how JSONB might improve on that.)
185448*/
185449#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
185450#if !defined(SQLITEINT_H)
185451/* #include "sqlite3ext.h" */
185452#endif
185453SQLITE_EXTENSION_INIT1
185454/* #include <assert.h> */
185455/* #include <string.h> */
185456/* #include <stdlib.h> */
185457/* #include <stdarg.h> */
185458
185459/* Mark a function parameter as unused, to suppress nuisance compiler
185460** warnings. */
185461#ifndef UNUSED_PARAM
185462# define UNUSED_PARAM(X) (void)(X)
185463#endif
185464
185465#ifndef LARGEST_INT64
185466# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
185467# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
185468#endif
185469
185470#ifndef deliberate_fall_through
185471# define deliberate_fall_through
185472#endif
185473
185474/*
185475** Versions of isspace(), isalnum() and isdigit() to which it is safe
185476** to pass signed char values.
185477*/
185478#ifdef sqlite3Isdigit
185479 /* Use the SQLite core versions if this routine is part of the
185480 ** SQLite amalgamation */
185481# define safe_isdigit(x) sqlite3Isdigit(x)
185482# define safe_isalnum(x) sqlite3Isalnum(x)
185483# define safe_isxdigit(x) sqlite3Isxdigit(x)
185484#else
185485 /* Use the standard library for separate compilation */
185486#include <ctype.h> /* amalgamator: keep */
185487# define safe_isdigit(x) isdigit((unsigned char)(x))
185488# define safe_isalnum(x) isalnum((unsigned char)(x))
185489# define safe_isxdigit(x) isxdigit((unsigned char)(x))
185490#endif
185491
185492/*
185493** Growing our own isspace() routine this way is twice as fast as
185494** the library isspace() function, resulting in a 7% overall performance
185495** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
185496*/
185497static const char jsonIsSpace[] = {
185498 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
185499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185500 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185501 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185502 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185503 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185504 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185505 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185506 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185507 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185508 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185509 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185510 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185511 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185512 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185514};
185515#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
185516
185517#ifndef SQLITE_AMALGAMATION
185518 /* Unsigned integer types. These are already defined in the sqliteInt.h,
185519 ** but the definitions need to be repeated for separate compilation. */
185520 typedef sqlite3_uint64 u64;
185521 typedef unsigned int u32;
185522 typedef unsigned short int u16;
185523 typedef unsigned char u8;
185524#endif
185525
185526/* Objects */
185527typedef struct JsonString JsonString;
185528typedef struct JsonNode JsonNode;
185529typedef struct JsonParse JsonParse;
185530
185531/* An instance of this object represents a JSON string
185532** under construction. Really, this is a generic string accumulator
185533** that can be and is used to create strings other than JSON.
185534*/
185535struct JsonString {
185536 sqlite3_context *pCtx; /* Function context - put error messages here */
185537 char *zBuf; /* Append JSON content here */
185538 u64 nAlloc; /* Bytes of storage available in zBuf[] */
185539 u64 nUsed; /* Bytes of zBuf[] currently used */
185540 u8 bStatic; /* True if zBuf is static space */
185541 u8 bErr; /* True if an error has been encountered */
185542 char zSpace[100]; /* Initial static space */
185543};
185544
185545/* JSON type values
185546*/
185547#define JSON_NULL 0
185548#define JSON_TRUE 1
185549#define JSON_FALSE 2
185550#define JSON_INT 3
185551#define JSON_REAL 4
185552#define JSON_STRING 5
185553#define JSON_ARRAY 6
185554#define JSON_OBJECT 7
185555
185556/* The "subtype" set for JSON values */
185557#define JSON_SUBTYPE 74 /* Ascii for "J" */
185558
185559/*
185560** Names of the various JSON types:
185561*/
185562static const char * const jsonType[] = {
185563 "null", "true", "false", "integer", "real", "text", "array", "object"
185564};
185565
185566/* Bit values for the JsonNode.jnFlag field
185567*/
185568#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
185569#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
185570#define JNODE_REMOVE 0x04 /* Do not output */
185571#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
185572#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
185573#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
185574#define JNODE_LABEL 0x40 /* Is a label of an object */
185575
185576
185577/* A single node of parsed JSON
185578*/
185579struct JsonNode {
185580 u8 eType; /* One of the JSON_ type values */
185581 u8 jnFlags; /* JNODE flags */
185582 u32 n; /* Bytes of content, or number of sub-nodes */
185583 union {
185584 const char *zJContent; /* Content for INT, REAL, and STRING */
185585 u32 iAppend; /* More terms for ARRAY and OBJECT */
185586 u32 iKey; /* Key for ARRAY objects in json_tree() */
185587 u32 iReplace; /* Replacement content for JNODE_REPLACE */
185588 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
185589 } u;
185590};
185591
185592/* A completely parsed JSON string
185593*/
185594struct JsonParse {
185595 u32 nNode; /* Number of slots of aNode[] used */
185596 u32 nAlloc; /* Number of slots of aNode[] allocated */
185597 JsonNode *aNode; /* Array of nodes containing the parse */
185598 const char *zJson; /* Original JSON string */
185599 u32 *aUp; /* Index of parent of each node */
185600 u8 oom; /* Set to true if out of memory */
185601 u8 nErr; /* Number of errors seen */
185602 u16 iDepth; /* Nesting depth */
185603 int nJson; /* Length of the zJson string in bytes */
185604 u32 iHold; /* Replace cache line with the lowest iHold value */
185605};
185606
185607/*
185608** Maximum nesting depth of JSON for this implementation.
185609**
185610** This limit is needed to avoid a stack overflow in the recursive
185611** descent parser. A depth of 2000 is far deeper than any sane JSON
185612** should go.
185613*/
185614#define JSON_MAX_DEPTH 2000
185615
185616/**************************************************************************
185617** Utility routines for dealing with JsonString objects
185618**************************************************************************/
185619
185620/* Set the JsonString object to an empty string
185621*/
185622static void jsonZero(JsonString *p){
185623 p->zBuf = p->zSpace;
185624 p->nAlloc = sizeof(p->zSpace);
185625 p->nUsed = 0;
185626 p->bStatic = 1;
185627}
185628
185629/* Initialize the JsonString object
185630*/
185631static void jsonInit(JsonString *p, sqlite3_context *pCtx){
185632 p->pCtx = pCtx;
185633 p->bErr = 0;
185634 jsonZero(p);
185635}
185636
185637
185638/* Free all allocated memory and reset the JsonString object back to its
185639** initial state.
185640*/
185641static void jsonReset(JsonString *p){
185642 if( !p->bStatic ) sqlite3_free(p->zBuf);
185643 jsonZero(p);
185644}
185645
185646
185647/* Report an out-of-memory (OOM) condition
185648*/
185649static void jsonOom(JsonString *p){
185650 p->bErr = 1;
185651 sqlite3_result_error_nomem(p->pCtx);
185652 jsonReset(p);
185653}
185654
185655/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
185656** Return zero on success. Return non-zero on an OOM error
185657*/
185658static int jsonGrow(JsonString *p, u32 N){
185659 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
185660 char *zNew;
185661 if( p->bStatic ){
185662 if( p->bErr ) return 1;
185663 zNew = sqlite3_malloc64(nTotal);
185664 if( zNew==0 ){
185665 jsonOom(p);
185666 return SQLITE_NOMEM;
185667 }
185668 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
185669 p->zBuf = zNew;
185670 p->bStatic = 0;
185671 }else{
185672 zNew = sqlite3_realloc64(p->zBuf, nTotal);
185673 if( zNew==0 ){
185674 jsonOom(p);
185675 return SQLITE_NOMEM;
185676 }
185677 p->zBuf = zNew;
185678 }
185679 p->nAlloc = nTotal;
185680 return SQLITE_OK;
185681}
185682
185683/* Append N bytes from zIn onto the end of the JsonString string.
185684*/
185685static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
185686 if( N==0 ) return;
185687 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
185688 memcpy(p->zBuf+p->nUsed, zIn, N);
185689 p->nUsed += N;
185690}
185691
185692/* Append formatted text (not to exceed N bytes) to the JsonString.
185693*/
185694static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
185695 va_list ap;
185696 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
185697 va_start(ap, zFormat);
185698 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
185699 va_end(ap);
185700 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
185701}
185702
185703/* Append a single character
185704*/
185705static void jsonAppendChar(JsonString *p, char c){
185706 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
185707 p->zBuf[p->nUsed++] = c;
185708}
185709
185710/* Append a comma separator to the output buffer, if the previous
185711** character is not '[' or '{'.
185712*/
185713static void jsonAppendSeparator(JsonString *p){
185714 char c;
185715 if( p->nUsed==0 ) return;
185716 c = p->zBuf[p->nUsed-1];
185717 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
185718}
185719
185720/* Append the N-byte string in zIn to the end of the JsonString string
185721** under construction. Enclose the string in "..." and escape
185722** any double-quotes or backslash characters contained within the
185723** string.
185724*/
185725static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
185726 u32 i;
185727 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
185728 p->zBuf[p->nUsed++] = '"';
185729 for(i=0; i<N; i++){
185730 unsigned char c = ((unsigned const char*)zIn)[i];
185731 if( c=='"' || c=='\\' ){
185732 json_simple_escape:
185733 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
185734 p->zBuf[p->nUsed++] = '\\';
185735 }else if( c<=0x1f ){
185736 static const char aSpecial[] = {
185737 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
185738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
185739 };
185740 assert( sizeof(aSpecial)==32 );
185741 assert( aSpecial['\b']=='b' );
185742 assert( aSpecial['\f']=='f' );
185743 assert( aSpecial['\n']=='n' );
185744 assert( aSpecial['\r']=='r' );
185745 assert( aSpecial['\t']=='t' );
185746 if( aSpecial[c] ){
185747 c = aSpecial[c];
185748 goto json_simple_escape;
185749 }
185750 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
185751 p->zBuf[p->nUsed++] = '\\';
185752 p->zBuf[p->nUsed++] = 'u';
185753 p->zBuf[p->nUsed++] = '0';
185754 p->zBuf[p->nUsed++] = '0';
185755 p->zBuf[p->nUsed++] = '0' + (c>>4);
185756 c = "0123456789abcdef"[c&0xf];
185757 }
185758 p->zBuf[p->nUsed++] = c;
185759 }
185760 p->zBuf[p->nUsed++] = '"';
185761 assert( p->nUsed<p->nAlloc );
185762}
185763
185764/*
185765** Append a function parameter value to the JSON string under
185766** construction.
185767*/
185768static void jsonAppendValue(
185769 JsonString *p, /* Append to this JSON string */
185770 sqlite3_value *pValue /* Value to append */
185771){
185772 switch( sqlite3_value_type(pValue) ){
185773 case SQLITE_NULL: {
185774 jsonAppendRaw(p, "null", 4);
185775 break;
185776 }
185777 case SQLITE_INTEGER:
185778 case SQLITE_FLOAT: {
185779 const char *z = (const char*)sqlite3_value_text(pValue);
185780 u32 n = (u32)sqlite3_value_bytes(pValue);
185781 jsonAppendRaw(p, z, n);
185782 break;
185783 }
185784 case SQLITE_TEXT: {
185785 const char *z = (const char*)sqlite3_value_text(pValue);
185786 u32 n = (u32)sqlite3_value_bytes(pValue);
185787 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
185788 jsonAppendRaw(p, z, n);
185789 }else{
185790 jsonAppendString(p, z, n);
185791 }
185792 break;
185793 }
185794 default: {
185795 if( p->bErr==0 ){
185796 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
185797 p->bErr = 2;
185798 jsonReset(p);
185799 }
185800 break;
185801 }
185802 }
185803}
185804
185805
185806/* Make the JSON in p the result of the SQL function.
185807*/
185808static void jsonResult(JsonString *p){
185809 if( p->bErr==0 ){
185810 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
185811 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
185812 SQLITE_UTF8);
185813 jsonZero(p);
185814 }
185815 assert( p->bStatic );
185816}
185817
185818/**************************************************************************
185819** Utility routines for dealing with JsonNode and JsonParse objects
185820**************************************************************************/
185821
185822/*
185823** Return the number of consecutive JsonNode slots need to represent
185824** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
185825** OBJECT types, the number might be larger.
185826**
185827** Appended elements are not counted. The value returned is the number
185828** by which the JsonNode counter should increment in order to go to the
185829** next peer value.
185830*/
185831static u32 jsonNodeSize(JsonNode *pNode){
185832 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
185833}
185834
185835/*
185836** Reclaim all memory allocated by a JsonParse object. But do not
185837** delete the JsonParse object itself.
185838*/
185839static void jsonParseReset(JsonParse *pParse){
185840 sqlite3_free(pParse->aNode);
185841 pParse->aNode = 0;
185842 pParse->nNode = 0;
185843 pParse->nAlloc = 0;
185844 sqlite3_free(pParse->aUp);
185845 pParse->aUp = 0;
185846}
185847
185848/*
185849** Free a JsonParse object that was obtained from sqlite3_malloc().
185850*/
185851static void jsonParseFree(JsonParse *pParse){
185852 jsonParseReset(pParse);
185853 sqlite3_free(pParse);
185854}
185855
185856/*
185857** Convert the JsonNode pNode into a pure JSON string and
185858** append to pOut. Subsubstructure is also included. Return
185859** the number of JsonNode objects that are encoded.
185860*/
185861static void jsonRenderNode(
185862 JsonNode *pNode, /* The node to render */
185863 JsonString *pOut, /* Write JSON here */
185864 sqlite3_value **aReplace /* Replacement values */
185865){
185866 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
185867 if( pNode->jnFlags & JNODE_REPLACE ){
185868 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
185869 return;
185870 }
185871 pNode = pNode->u.pPatch;
185872 }
185873 switch( pNode->eType ){
185874 default: {
185875 assert( pNode->eType==JSON_NULL );
185876 jsonAppendRaw(pOut, "null", 4);
185877 break;
185878 }
185879 case JSON_TRUE: {
185880 jsonAppendRaw(pOut, "true", 4);
185881 break;
185882 }
185883 case JSON_FALSE: {
185884 jsonAppendRaw(pOut, "false", 5);
185885 break;
185886 }
185887 case JSON_STRING: {
185888 if( pNode->jnFlags & JNODE_RAW ){
185889 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
185890 break;
185891 }
185892 /* no break */ deliberate_fall_through
185893 }
185894 case JSON_REAL:
185895 case JSON_INT: {
185896 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
185897 break;
185898 }
185899 case JSON_ARRAY: {
185900 u32 j = 1;
185901 jsonAppendChar(pOut, '[');
185902 for(;;){
185903 while( j<=pNode->n ){
185904 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
185905 jsonAppendSeparator(pOut);
185906 jsonRenderNode(&pNode[j], pOut, aReplace);
185907 }
185908 j += jsonNodeSize(&pNode[j]);
185909 }
185910 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
185911 pNode = &pNode[pNode->u.iAppend];
185912 j = 1;
185913 }
185914 jsonAppendChar(pOut, ']');
185915 break;
185916 }
185917 case JSON_OBJECT: {
185918 u32 j = 1;
185919 jsonAppendChar(pOut, '{');
185920 for(;;){
185921 while( j<=pNode->n ){
185922 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
185923 jsonAppendSeparator(pOut);
185924 jsonRenderNode(&pNode[j], pOut, aReplace);
185925 jsonAppendChar(pOut, ':');
185926 jsonRenderNode(&pNode[j+1], pOut, aReplace);
185927 }
185928 j += 1 + jsonNodeSize(&pNode[j+1]);
185929 }
185930 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
185931 pNode = &pNode[pNode->u.iAppend];
185932 j = 1;
185933 }
185934 jsonAppendChar(pOut, '}');
185935 break;
185936 }
185937 }
185938}
185939
185940/*
185941** Return a JsonNode and all its descendents as a JSON string.
185942*/
185943static void jsonReturnJson(
185944 JsonNode *pNode, /* Node to return */
185945 sqlite3_context *pCtx, /* Return value for this function */
185946 sqlite3_value **aReplace /* Array of replacement values */
185947){
185948 JsonString s;
185949 jsonInit(&s, pCtx);
185950 jsonRenderNode(pNode, &s, aReplace);
185951 jsonResult(&s);
185952 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
185953}
185954
185955/*
185956** Translate a single byte of Hex into an integer.
185957** This routine only works if h really is a valid hexadecimal
185958** character: 0..9a..fA..F
185959*/
185960static u8 jsonHexToInt(int h){
185961 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
185962#ifdef SQLITE_EBCDIC
185963 h += 9*(1&~(h>>4));
185964#else
185965 h += 9*(1&(h>>6));
185966#endif
185967 return (u8)(h & 0xf);
185968}
185969
185970/*
185971** Convert a 4-byte hex string into an integer
185972*/
185973static u32 jsonHexToInt4(const char *z){
185974 u32 v;
185975 assert( safe_isxdigit(z[0]) );
185976 assert( safe_isxdigit(z[1]) );
185977 assert( safe_isxdigit(z[2]) );
185978 assert( safe_isxdigit(z[3]) );
185979 v = (jsonHexToInt(z[0])<<12)
185980 + (jsonHexToInt(z[1])<<8)
185981 + (jsonHexToInt(z[2])<<4)
185982 + jsonHexToInt(z[3]);
185983 return v;
185984}
185985
185986/*
185987** Make the JsonNode the return value of the function.
185988*/
185989static void jsonReturn(
185990 JsonNode *pNode, /* Node to return */
185991 sqlite3_context *pCtx, /* Return value for this function */
185992 sqlite3_value **aReplace /* Array of replacement values */
185993){
185994 switch( pNode->eType ){
185995 default: {
185996 assert( pNode->eType==JSON_NULL );
185997 sqlite3_result_null(pCtx);
185998 break;
185999 }
186000 case JSON_TRUE: {
186001 sqlite3_result_int(pCtx, 1);
186002 break;
186003 }
186004 case JSON_FALSE: {
186005 sqlite3_result_int(pCtx, 0);
186006 break;
186007 }
186008 case JSON_INT: {
186009 sqlite3_int64 i = 0;
186010 const char *z = pNode->u.zJContent;
186011 if( z[0]=='-' ){ z++; }
186012 while( z[0]>='0' && z[0]<='9' ){
186013 unsigned v = *(z++) - '0';
186014 if( i>=LARGEST_INT64/10 ){
186015 if( i>LARGEST_INT64/10 ) goto int_as_real;
186016 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
186017 if( v==9 ) goto int_as_real;
186018 if( v==8 ){
186019 if( pNode->u.zJContent[0]=='-' ){
186020 sqlite3_result_int64(pCtx, SMALLEST_INT64);
186021 goto int_done;
186022 }else{
186023 goto int_as_real;
186024 }
186025 }
186026 }
186027 i = i*10 + v;
186028 }
186029 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
186030 sqlite3_result_int64(pCtx, i);
186031 int_done:
186032 break;
186033 int_as_real: i=0; /* no break */ deliberate_fall_through
186034 }
186035 case JSON_REAL: {
186036 double r;
186037#ifdef SQLITE_AMALGAMATION
186038 const char *z = pNode->u.zJContent;
186039 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
186040#else
186041 r = strtod(pNode->u.zJContent, 0);
186042#endif
186043 sqlite3_result_double(pCtx, r);
186044 break;
186045 }
186046 case JSON_STRING: {
186047#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
186048 ** json_insert() and json_replace() and those routines do not
186049 ** call jsonReturn() */
186050 if( pNode->jnFlags & JNODE_RAW ){
186051 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
186052 SQLITE_TRANSIENT);
186053 }else
186054#endif
186055 assert( (pNode->jnFlags & JNODE_RAW)==0 );
186056 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
186057 /* JSON formatted without any backslash-escapes */
186058 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
186059 SQLITE_TRANSIENT);
186060 }else{
186061 /* Translate JSON formatted string into raw text */
186062 u32 i;
186063 u32 n = pNode->n;
186064 const char *z = pNode->u.zJContent;
186065 char *zOut;
186066 u32 j;
186067 zOut = sqlite3_malloc( n+1 );
186068 if( zOut==0 ){
186069 sqlite3_result_error_nomem(pCtx);
186070 break;
186071 }
186072 for(i=1, j=0; i<n-1; i++){
186073 char c = z[i];
186074 if( c!='\\' ){
186075 zOut[j++] = c;
186076 }else{
186077 c = z[++i];
186078 if( c=='u' ){
186079 u32 v = jsonHexToInt4(z+i+1);
186080 i += 4;
186081 if( v==0 ) break;
186082 if( v<=0x7f ){
186083 zOut[j++] = (char)v;
186084 }else if( v<=0x7ff ){
186085 zOut[j++] = (char)(0xc0 | (v>>6));
186086 zOut[j++] = 0x80 | (v&0x3f);
186087 }else{
186088 u32 vlo;
186089 if( (v&0xfc00)==0xd800
186090 && i<n-6
186091 && z[i+1]=='\\'
186092 && z[i+2]=='u'
186093 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
186094 ){
186095 /* We have a surrogate pair */
186096 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
186097 i += 6;
186098 zOut[j++] = 0xf0 | (v>>18);
186099 zOut[j++] = 0x80 | ((v>>12)&0x3f);
186100 zOut[j++] = 0x80 | ((v>>6)&0x3f);
186101 zOut[j++] = 0x80 | (v&0x3f);
186102 }else{
186103 zOut[j++] = 0xe0 | (v>>12);
186104 zOut[j++] = 0x80 | ((v>>6)&0x3f);
186105 zOut[j++] = 0x80 | (v&0x3f);
186106 }
186107 }
186108 }else{
186109 if( c=='b' ){
186110 c = '\b';
186111 }else if( c=='f' ){
186112 c = '\f';
186113 }else if( c=='n' ){
186114 c = '\n';
186115 }else if( c=='r' ){
186116 c = '\r';
186117 }else if( c=='t' ){
186118 c = '\t';
186119 }
186120 zOut[j++] = c;
186121 }
186122 }
186123 }
186124 zOut[j] = 0;
186125 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
186126 }
186127 break;
186128 }
186129 case JSON_ARRAY:
186130 case JSON_OBJECT: {
186131 jsonReturnJson(pNode, pCtx, aReplace);
186132 break;
186133 }
186134 }
186135}
186136
186137/* Forward reference */
186138static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
186139
186140/*
186141** A macro to hint to the compiler that a function should not be
186142** inlined.
186143*/
186144#if defined(__GNUC__)
186145# define JSON_NOINLINE __attribute__((noinline))
186146#elif defined(_MSC_VER) && _MSC_VER>=1310
186147# define JSON_NOINLINE __declspec(noinline)
186148#else
186149# define JSON_NOINLINE
186150#endif
186151
186152
186153static JSON_NOINLINE int jsonParseAddNodeExpand(
186154 JsonParse *pParse, /* Append the node to this object */
186155 u32 eType, /* Node type */
186156 u32 n, /* Content size or sub-node count */
186157 const char *zContent /* Content */
186158){
186159 u32 nNew;
186160 JsonNode *pNew;
186161 assert( pParse->nNode>=pParse->nAlloc );
186162 if( pParse->oom ) return -1;
186163 nNew = pParse->nAlloc*2 + 10;
186164 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
186165 if( pNew==0 ){
186166 pParse->oom = 1;
186167 return -1;
186168 }
186169 pParse->nAlloc = nNew;
186170 pParse->aNode = pNew;
186171 assert( pParse->nNode<pParse->nAlloc );
186172 return jsonParseAddNode(pParse, eType, n, zContent);
186173}
186174
186175/*
186176** Create a new JsonNode instance based on the arguments and append that
186177** instance to the JsonParse. Return the index in pParse->aNode[] of the
186178** new node, or -1 if a memory allocation fails.
186179*/
186180static int jsonParseAddNode(
186181 JsonParse *pParse, /* Append the node to this object */
186182 u32 eType, /* Node type */
186183 u32 n, /* Content size or sub-node count */
186184 const char *zContent /* Content */
186185){
186186 JsonNode *p;
186187 if( pParse->nNode>=pParse->nAlloc ){
186188 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
186189 }
186190 p = &pParse->aNode[pParse->nNode];
186191 p->eType = (u8)eType;
186192 p->jnFlags = 0;
186193 p->n = n;
186194 p->u.zJContent = zContent;
186195 return pParse->nNode++;
186196}
186197
186198/*
186199** Return true if z[] begins with 4 (or more) hexadecimal digits
186200*/
186201static int jsonIs4Hex(const char *z){
186202 int i;
186203 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
186204 return 1;
186205}
186206
186207/*
186208** Parse a single JSON value which begins at pParse->zJson[i]. Return the
186209** index of the first character past the end of the value parsed.
186210**
186211** Return negative for a syntax error. Special cases: return -2 if the
186212** first non-whitespace character is '}' and return -3 if the first
186213** non-whitespace character is ']'.
186214*/
186215static int jsonParseValue(JsonParse *pParse, u32 i){
186216 char c;
186217 u32 j;
186218 int iThis;
186219 int x;
186220 JsonNode *pNode;
186221 const char *z = pParse->zJson;
186222 while( safe_isspace(z[i]) ){ i++; }
186223 if( (c = z[i])=='{' ){
186224 /* Parse object */
186225 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
186226 if( iThis<0 ) return -1;
186227 for(j=i+1;;j++){
186228 while( safe_isspace(z[j]) ){ j++; }
186229 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
186230 x = jsonParseValue(pParse, j);
186231 if( x<0 ){
186232 pParse->iDepth--;
186233 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
186234 return -1;
186235 }
186236 if( pParse->oom ) return -1;
186237 pNode = &pParse->aNode[pParse->nNode-1];
186238 if( pNode->eType!=JSON_STRING ) return -1;
186239 pNode->jnFlags |= JNODE_LABEL;
186240 j = x;
186241 while( safe_isspace(z[j]) ){ j++; }
186242 if( z[j]!=':' ) return -1;
186243 j++;
186244 x = jsonParseValue(pParse, j);
186245 pParse->iDepth--;
186246 if( x<0 ) return -1;
186247 j = x;
186248 while( safe_isspace(z[j]) ){ j++; }
186249 c = z[j];
186250 if( c==',' ) continue;
186251 if( c!='}' ) return -1;
186252 break;
186253 }
186254 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
186255 return j+1;
186256 }else if( c=='[' ){
186257 /* Parse array */
186258 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
186259 if( iThis<0 ) return -1;
186260 for(j=i+1;;j++){
186261 while( safe_isspace(z[j]) ){ j++; }
186262 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
186263 x = jsonParseValue(pParse, j);
186264 pParse->iDepth--;
186265 if( x<0 ){
186266 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
186267 return -1;
186268 }
186269 j = x;
186270 while( safe_isspace(z[j]) ){ j++; }
186271 c = z[j];
186272 if( c==',' ) continue;
186273 if( c!=']' ) return -1;
186274 break;
186275 }
186276 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
186277 return j+1;
186278 }else if( c=='"' ){
186279 /* Parse string */
186280 u8 jnFlags = 0;
186281 j = i+1;
186282 for(;;){
186283 c = z[j];
186284 if( (c & ~0x1f)==0 ){
186285 /* Control characters are not allowed in strings */
186286 return -1;
186287 }
186288 if( c=='\\' ){
186289 c = z[++j];
186290 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
186291 || c=='n' || c=='r' || c=='t'
186292 || (c=='u' && jsonIs4Hex(z+j+1)) ){
186293 jnFlags = JNODE_ESCAPE;
186294 }else{
186295 return -1;
186296 }
186297 }else if( c=='"' ){
186298 break;
186299 }
186300 j++;
186301 }
186302 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
186303 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
186304 return j+1;
186305 }else if( c=='n'
186306 && strncmp(z+i,"null",4)==0
186307 && !safe_isalnum(z[i+4]) ){
186308 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
186309 return i+4;
186310 }else if( c=='t'
186311 && strncmp(z+i,"true",4)==0
186312 && !safe_isalnum(z[i+4]) ){
186313 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
186314 return i+4;
186315 }else if( c=='f'
186316 && strncmp(z+i,"false",5)==0
186317 && !safe_isalnum(z[i+5]) ){
186318 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
186319 return i+5;
186320 }else if( c=='-' || (c>='0' && c<='9') ){
186321 /* Parse number */
186322 u8 seenDP = 0;
186323 u8 seenE = 0;
186324 assert( '-' < '0' );
186325 if( c<='0' ){
186326 j = c=='-' ? i+1 : i;
186327 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
186328 }
186329 j = i+1;
186330 for(;; j++){
186331 c = z[j];
186332 if( c>='0' && c<='9' ) continue;
186333 if( c=='.' ){
186334 if( z[j-1]=='-' ) return -1;
186335 if( seenDP ) return -1;
186336 seenDP = 1;
186337 continue;
186338 }
186339 if( c=='e' || c=='E' ){
186340 if( z[j-1]<'0' ) return -1;
186341 if( seenE ) return -1;
186342 seenDP = seenE = 1;
186343 c = z[j+1];
186344 if( c=='+' || c=='-' ){
186345 j++;
186346 c = z[j+1];
186347 }
186348 if( c<'0' || c>'9' ) return -1;
186349 continue;
186350 }
186351 break;
186352 }
186353 if( z[j-1]<'0' ) return -1;
186354 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
186355 j - i, &z[i]);
186356 return j;
186357 }else if( c=='}' ){
186358 return -2; /* End of {...} */
186359 }else if( c==']' ){
186360 return -3; /* End of [...] */
186361 }else if( c==0 ){
186362 return 0; /* End of file */
186363 }else{
186364 return -1; /* Syntax error */
186365 }
186366}
186367
186368/*
186369** Parse a complete JSON string. Return 0 on success or non-zero if there
186370** are any errors. If an error occurs, free all memory associated with
186371** pParse.
186372**
186373** pParse is uninitialized when this routine is called.
186374*/
186375static int jsonParse(
186376 JsonParse *pParse, /* Initialize and fill this JsonParse object */
186377 sqlite3_context *pCtx, /* Report errors here */
186378 const char *zJson /* Input JSON text to be parsed */
186379){
186380 int i;
186381 memset(pParse, 0, sizeof(*pParse));
186382 if( zJson==0 ) return 1;
186383 pParse->zJson = zJson;
186384 i = jsonParseValue(pParse, 0);
186385 if( pParse->oom ) i = -1;
186386 if( i>0 ){
186387 assert( pParse->iDepth==0 );
186388 while( safe_isspace(zJson[i]) ) i++;
186389 if( zJson[i] ) i = -1;
186390 }
186391 if( i<=0 ){
186392 if( pCtx!=0 ){
186393 if( pParse->oom ){
186394 sqlite3_result_error_nomem(pCtx);
186395 }else{
186396 sqlite3_result_error(pCtx, "malformed JSON", -1);
186397 }
186398 }
186399 jsonParseReset(pParse);
186400 return 1;
186401 }
186402 return 0;
186403}
186404
186405/* Mark node i of pParse as being a child of iParent. Call recursively
186406** to fill in all the descendants of node i.
186407*/
186408static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
186409 JsonNode *pNode = &pParse->aNode[i];
186410 u32 j;
186411 pParse->aUp[i] = iParent;
186412 switch( pNode->eType ){
186413 case JSON_ARRAY: {
186414 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
186415 jsonParseFillInParentage(pParse, i+j, i);
186416 }
186417 break;
186418 }
186419 case JSON_OBJECT: {
186420 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
186421 pParse->aUp[i+j] = i;
186422 jsonParseFillInParentage(pParse, i+j+1, i);
186423 }
186424 break;
186425 }
186426 default: {
186427 break;
186428 }
186429 }
186430}
186431
186432/*
186433** Compute the parentage of all nodes in a completed parse.
186434*/
186435static int jsonParseFindParents(JsonParse *pParse){
186436 u32 *aUp;
186437 assert( pParse->aUp==0 );
186438 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
186439 if( aUp==0 ){
186440 pParse->oom = 1;
186441 return SQLITE_NOMEM;
186442 }
186443 jsonParseFillInParentage(pParse, 0, 0);
186444 return SQLITE_OK;
186445}
186446
186447/*
186448** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
186449*/
186450#define JSON_CACHE_ID (-429938) /* First cache entry */
186451#define JSON_CACHE_SZ 4 /* Max number of cache entries */
186452
186453/*
186454** Obtain a complete parse of the JSON found in the first argument
186455** of the argv array. Use the sqlite3_get_auxdata() cache for this
186456** parse if it is available. If the cache is not available or if it
186457** is no longer valid, parse the JSON again and return the new parse,
186458** and also register the new parse so that it will be available for
186459** future sqlite3_get_auxdata() calls.
186460*/
186461static JsonParse *jsonParseCached(
186462 sqlite3_context *pCtx,
186463 sqlite3_value **argv,
186464 sqlite3_context *pErrCtx
186465){
186466 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
186467 int nJson = sqlite3_value_bytes(argv[0]);
186468 JsonParse *p;
186469 JsonParse *pMatch = 0;
186470 int iKey;
186471 int iMinKey = 0;
186472 u32 iMinHold = 0xffffffff;
186473 u32 iMaxHold = 0;
186474 if( zJson==0 ) return 0;
186475 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
186476 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
186477 if( p==0 ){
186478 iMinKey = iKey;
186479 break;
186480 }
186481 if( pMatch==0
186482 && p->nJson==nJson
186483 && memcmp(p->zJson,zJson,nJson)==0
186484 ){
186485 p->nErr = 0;
186486 pMatch = p;
186487 }else if( p->iHold<iMinHold ){
186488 iMinHold = p->iHold;
186489 iMinKey = iKey;
186490 }
186491 if( p->iHold>iMaxHold ){
186492 iMaxHold = p->iHold;
186493 }
186494 }
186495 if( pMatch ){
186496 pMatch->nErr = 0;
186497 pMatch->iHold = iMaxHold+1;
186498 return pMatch;
186499 }
186500 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
186501 if( p==0 ){
186502 sqlite3_result_error_nomem(pCtx);
186503 return 0;
186504 }
186505 memset(p, 0, sizeof(*p));
186506 p->zJson = (char*)&p[1];
186507 memcpy((char*)p->zJson, zJson, nJson+1);
186508 if( jsonParse(p, pErrCtx, p->zJson) ){
186509 sqlite3_free(p);
186510 return 0;
186511 }
186512 p->nJson = nJson;
186513 p->iHold = iMaxHold+1;
186514 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
186515 (void(*)(void*))jsonParseFree);
186516 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
186517}
186518
186519/*
186520** Compare the OBJECT label at pNode against zKey,nKey. Return true on
186521** a match.
186522*/
186523static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
186524 if( pNode->jnFlags & JNODE_RAW ){
186525 if( pNode->n!=nKey ) return 0;
186526 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
186527 }else{
186528 if( pNode->n!=nKey+2 ) return 0;
186529 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
186530 }
186531}
186532
186533/* forward declaration */
186534static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
186535
186536/*
186537** Search along zPath to find the node specified. Return a pointer
186538** to that node, or NULL if zPath is malformed or if there is no such
186539** node.
186540**
186541** If pApnd!=0, then try to append new nodes to complete zPath if it is
186542** possible to do so and if no existing node corresponds to zPath. If
186543** new nodes are appended *pApnd is set to 1.
186544*/
186545static JsonNode *jsonLookupStep(
186546 JsonParse *pParse, /* The JSON to search */
186547 u32 iRoot, /* Begin the search at this node */
186548 const char *zPath, /* The path to search */
186549 int *pApnd, /* Append nodes to complete path if not NULL */
186550 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
186551){
186552 u32 i, j, nKey;
186553 const char *zKey;
186554 JsonNode *pRoot = &pParse->aNode[iRoot];
186555 if( zPath[0]==0 ) return pRoot;
186556 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
186557 if( zPath[0]=='.' ){
186558 if( pRoot->eType!=JSON_OBJECT ) return 0;
186559 zPath++;
186560 if( zPath[0]=='"' ){
186561 zKey = zPath + 1;
186562 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
186563 nKey = i-1;
186564 if( zPath[i] ){
186565 i++;
186566 }else{
186567 *pzErr = zPath;
186568 return 0;
186569 }
186570 }else{
186571 zKey = zPath;
186572 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
186573 nKey = i;
186574 }
186575 if( nKey==0 ){
186576 *pzErr = zPath;
186577 return 0;
186578 }
186579 j = 1;
186580 for(;;){
186581 while( j<=pRoot->n ){
186582 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
186583 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
186584 }
186585 j++;
186586 j += jsonNodeSize(&pRoot[j]);
186587 }
186588 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
186589 iRoot += pRoot->u.iAppend;
186590 pRoot = &pParse->aNode[iRoot];
186591 j = 1;
186592 }
186593 if( pApnd ){
186594 u32 iStart, iLabel;
186595 JsonNode *pNode;
186596 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
186597 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
186598 zPath += i;
186599 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
186600 if( pParse->oom ) return 0;
186601 if( pNode ){
186602 pRoot = &pParse->aNode[iRoot];
186603 pRoot->u.iAppend = iStart - iRoot;
186604 pRoot->jnFlags |= JNODE_APPEND;
186605 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
186606 }
186607 return pNode;
186608 }
186609 }else if( zPath[0]=='[' ){
186610 i = 0;
186611 j = 1;
186612 while( safe_isdigit(zPath[j]) ){
186613 i = i*10 + zPath[j] - '0';
186614 j++;
186615 }
186616 if( j<2 || zPath[j]!=']' ){
186617 if( zPath[1]=='#' ){
186618 JsonNode *pBase = pRoot;
186619 int iBase = iRoot;
186620 if( pRoot->eType!=JSON_ARRAY ) return 0;
186621 for(;;){
186622 while( j<=pBase->n ){
186623 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
186624 j += jsonNodeSize(&pBase[j]);
186625 }
186626 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
186627 iBase += pBase->u.iAppend;
186628 pBase = &pParse->aNode[iBase];
186629 j = 1;
186630 }
186631 j = 2;
186632 if( zPath[2]=='-' && safe_isdigit(zPath[3]) ){
186633 unsigned int x = 0;
186634 j = 3;
186635 do{
186636 x = x*10 + zPath[j] - '0';
186637 j++;
186638 }while( safe_isdigit(zPath[j]) );
186639 if( x>i ) return 0;
186640 i -= x;
186641 }
186642 if( zPath[j]!=']' ){
186643 *pzErr = zPath;
186644 return 0;
186645 }
186646 }else{
186647 *pzErr = zPath;
186648 return 0;
186649 }
186650 }
186651 if( pRoot->eType!=JSON_ARRAY ) return 0;
186652 zPath += j + 1;
186653 j = 1;
186654 for(;;){
186655 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
186656 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
186657 j += jsonNodeSize(&pRoot[j]);
186658 }
186659 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
186660 iRoot += pRoot->u.iAppend;
186661 pRoot = &pParse->aNode[iRoot];
186662 j = 1;
186663 }
186664 if( j<=pRoot->n ){
186665 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
186666 }
186667 if( i==0 && pApnd ){
186668 u32 iStart;
186669 JsonNode *pNode;
186670 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
186671 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
186672 if( pParse->oom ) return 0;
186673 if( pNode ){
186674 pRoot = &pParse->aNode[iRoot];
186675 pRoot->u.iAppend = iStart - iRoot;
186676 pRoot->jnFlags |= JNODE_APPEND;
186677 }
186678 return pNode;
186679 }
186680 }else{
186681 *pzErr = zPath;
186682 }
186683 return 0;
186684}
186685
186686/*
186687** Append content to pParse that will complete zPath. Return a pointer
186688** to the inserted node, or return NULL if the append fails.
186689*/
186690static JsonNode *jsonLookupAppend(
186691 JsonParse *pParse, /* Append content to the JSON parse */
186692 const char *zPath, /* Description of content to append */
186693 int *pApnd, /* Set this flag to 1 */
186694 const char **pzErr /* Make this point to any syntax error */
186695){
186696 *pApnd = 1;
186697 if( zPath[0]==0 ){
186698 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
186699 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
186700 }
186701 if( zPath[0]=='.' ){
186702 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
186703 }else if( strncmp(zPath,"[0]",3)==0 ){
186704 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
186705 }else{
186706 return 0;
186707 }
186708 if( pParse->oom ) return 0;
186709 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
186710}
186711
186712/*
186713** Return the text of a syntax error message on a JSON path. Space is
186714** obtained from sqlite3_malloc().
186715*/
186716static char *jsonPathSyntaxError(const char *zErr){
186717 return sqlite3_mprintf("JSON path error near '%q'", zErr);
186718}
186719
186720/*
186721** Do a node lookup using zPath. Return a pointer to the node on success.
186722** Return NULL if not found or if there is an error.
186723**
186724** On an error, write an error message into pCtx and increment the
186725** pParse->nErr counter.
186726**
186727** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
186728** nodes are appended.
186729*/
186730static JsonNode *jsonLookup(
186731 JsonParse *pParse, /* The JSON to search */
186732 const char *zPath, /* The path to search */
186733 int *pApnd, /* Append nodes to complete path if not NULL */
186734 sqlite3_context *pCtx /* Report errors here, if not NULL */
186735){
186736 const char *zErr = 0;
186737 JsonNode *pNode = 0;
186738 char *zMsg;
186739
186740 if( zPath==0 ) return 0;
186741 if( zPath[0]!='$' ){
186742 zErr = zPath;
186743 goto lookup_err;
186744 }
186745 zPath++;
186746 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
186747 if( zErr==0 ) return pNode;
186748
186749lookup_err:
186750 pParse->nErr++;
186751 assert( zErr!=0 && pCtx!=0 );
186752 zMsg = jsonPathSyntaxError(zErr);
186753 if( zMsg ){
186754 sqlite3_result_error(pCtx, zMsg, -1);
186755 sqlite3_free(zMsg);
186756 }else{
186757 sqlite3_result_error_nomem(pCtx);
186758 }
186759 return 0;
186760}
186761
186762
186763/*
186764** Report the wrong number of arguments for json_insert(), json_replace()
186765** or json_set().
186766*/
186767static void jsonWrongNumArgs(
186768 sqlite3_context *pCtx,
186769 const char *zFuncName
186770){
186771 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
186772 zFuncName);
186773 sqlite3_result_error(pCtx, zMsg, -1);
186774 sqlite3_free(zMsg);
186775}
186776
186777/*
186778** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
186779*/
186780static void jsonRemoveAllNulls(JsonNode *pNode){
186781 int i, n;
186782 assert( pNode->eType==JSON_OBJECT );
186783 n = pNode->n;
186784 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
186785 switch( pNode[i].eType ){
186786 case JSON_NULL:
186787 pNode[i].jnFlags |= JNODE_REMOVE;
186788 break;
186789 case JSON_OBJECT:
186790 jsonRemoveAllNulls(&pNode[i]);
186791 break;
186792 }
186793 }
186794}
186795
186796
186797/****************************************************************************
186798** SQL functions used for testing and debugging
186799****************************************************************************/
186800
186801#ifdef SQLITE_DEBUG
186802/*
186803** The json_parse(JSON) function returns a string which describes
186804** a parse of the JSON provided. Or it returns NULL if JSON is not
186805** well-formed.
186806*/
186807static void jsonParseFunc(
186808 sqlite3_context *ctx,
186809 int argc,
186810 sqlite3_value **argv
186811){
186812 JsonString s; /* Output string - not real JSON */
186813 JsonParse x; /* The parse */
186814 u32 i;
186815
186816 assert( argc==1 );
186817 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
186818 jsonParseFindParents(&x);
186819 jsonInit(&s, ctx);
186820 for(i=0; i<x.nNode; i++){
186821 const char *zType;
186822 if( x.aNode[i].jnFlags & JNODE_LABEL ){
186823 assert( x.aNode[i].eType==JSON_STRING );
186824 zType = "label";
186825 }else{
186826 zType = jsonType[x.aNode[i].eType];
186827 }
186828 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
186829 i, zType, x.aNode[i].n, x.aUp[i]);
186830 if( x.aNode[i].u.zJContent!=0 ){
186831 jsonAppendRaw(&s, " ", 1);
186832 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
186833 }
186834 jsonAppendRaw(&s, "\n", 1);
186835 }
186836 jsonParseReset(&x);
186837 jsonResult(&s);
186838}
186839
186840/*
186841** The json_test1(JSON) function return true (1) if the input is JSON
186842** text generated by another json function. It returns (0) if the input
186843** is not known to be JSON.
186844*/
186845static void jsonTest1Func(
186846 sqlite3_context *ctx,
186847 int argc,
186848 sqlite3_value **argv
186849){
186850 UNUSED_PARAM(argc);
186851 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
186852}
186853#endif /* SQLITE_DEBUG */
186854
186855/****************************************************************************
186856** Scalar SQL function implementations
186857****************************************************************************/
186858
186859/*
186860** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
186861** corresponding to the SQL value input. Mostly this means putting
186862** double-quotes around strings and returning the unquoted string "null"
186863** when given a NULL input.
186864*/
186865static void jsonQuoteFunc(
186866 sqlite3_context *ctx,
186867 int argc,
186868 sqlite3_value **argv
186869){
186870 JsonString jx;
186871 UNUSED_PARAM(argc);
186872
186873 jsonInit(&jx, ctx);
186874 jsonAppendValue(&jx, argv[0]);
186875 jsonResult(&jx);
186876 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186877}
186878
186879/*
186880** Implementation of the json_array(VALUE,...) function. Return a JSON
186881** array that contains all values given in arguments. Or if any argument
186882** is a BLOB, throw an error.
186883*/
186884static void jsonArrayFunc(
186885 sqlite3_context *ctx,
186886 int argc,
186887 sqlite3_value **argv
186888){
186889 int i;
186890 JsonString jx;
186891
186892 jsonInit(&jx, ctx);
186893 jsonAppendChar(&jx, '[');
186894 for(i=0; i<argc; i++){
186895 jsonAppendSeparator(&jx);
186896 jsonAppendValue(&jx, argv[i]);
186897 }
186898 jsonAppendChar(&jx, ']');
186899 jsonResult(&jx);
186900 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186901}
186902
186903
186904/*
186905** json_array_length(JSON)
186906** json_array_length(JSON, PATH)
186907**
186908** Return the number of elements in the top-level JSON array.
186909** Return 0 if the input is not a well-formed JSON array.
186910*/
186911static void jsonArrayLengthFunc(
186912 sqlite3_context *ctx,
186913 int argc,
186914 sqlite3_value **argv
186915){
186916 JsonParse *p; /* The parse */
186917 sqlite3_int64 n = 0;
186918 u32 i;
186919 JsonNode *pNode;
186920
186921 p = jsonParseCached(ctx, argv, ctx);
186922 if( p==0 ) return;
186923 assert( p->nNode );
186924 if( argc==2 ){
186925 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
186926 pNode = jsonLookup(p, zPath, 0, ctx);
186927 }else{
186928 pNode = p->aNode;
186929 }
186930 if( pNode==0 ){
186931 return;
186932 }
186933 if( pNode->eType==JSON_ARRAY ){
186934 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
186935 for(i=1; i<=pNode->n; n++){
186936 i += jsonNodeSize(&pNode[i]);
186937 }
186938 }
186939 sqlite3_result_int64(ctx, n);
186940}
186941
186942/*
186943** json_extract(JSON, PATH, ...)
186944**
186945** Return the element described by PATH. Return NULL if there is no
186946** PATH element. If there are multiple PATHs, then return a JSON array
186947** with the result from each path. Throw an error if the JSON or any PATH
186948** is malformed.
186949*/
186950static void jsonExtractFunc(
186951 sqlite3_context *ctx,
186952 int argc,
186953 sqlite3_value **argv
186954){
186955 JsonParse *p; /* The parse */
186956 JsonNode *pNode;
186957 const char *zPath;
186958 JsonString jx;
186959 int i;
186960
186961 if( argc<2 ) return;
186962 p = jsonParseCached(ctx, argv, ctx);
186963 if( p==0 ) return;
186964 jsonInit(&jx, ctx);
186965 jsonAppendChar(&jx, '[');
186966 for(i=1; i<argc; i++){
186967 zPath = (const char*)sqlite3_value_text(argv[i]);
186968 pNode = jsonLookup(p, zPath, 0, ctx);
186969 if( p->nErr ) break;
186970 if( argc>2 ){
186971 jsonAppendSeparator(&jx);
186972 if( pNode ){
186973 jsonRenderNode(pNode, &jx, 0);
186974 }else{
186975 jsonAppendRaw(&jx, "null", 4);
186976 }
186977 }else if( pNode ){
186978 jsonReturn(pNode, ctx, 0);
186979 }
186980 }
186981 if( argc>2 && i==argc ){
186982 jsonAppendChar(&jx, ']');
186983 jsonResult(&jx);
186984 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
186985 }
186986 jsonReset(&jx);
186987}
186988
186989/* This is the RFC 7396 MergePatch algorithm.
186990*/
186991static JsonNode *jsonMergePatch(
186992 JsonParse *pParse, /* The JSON parser that contains the TARGET */
186993 u32 iTarget, /* Node of the TARGET in pParse */
186994 JsonNode *pPatch /* The PATCH */
186995){
186996 u32 i, j;
186997 u32 iRoot;
186998 JsonNode *pTarget;
186999 if( pPatch->eType!=JSON_OBJECT ){
187000 return pPatch;
187001 }
187002 assert( iTarget>=0 && iTarget<pParse->nNode );
187003 pTarget = &pParse->aNode[iTarget];
187004 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
187005 if( pTarget->eType!=JSON_OBJECT ){
187006 jsonRemoveAllNulls(pPatch);
187007 return pPatch;
187008 }
187009 iRoot = iTarget;
187010 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
187011 u32 nKey;
187012 const char *zKey;
187013 assert( pPatch[i].eType==JSON_STRING );
187014 assert( pPatch[i].jnFlags & JNODE_LABEL );
187015 nKey = pPatch[i].n;
187016 zKey = pPatch[i].u.zJContent;
187017 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
187018 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
187019 assert( pTarget[j].eType==JSON_STRING );
187020 assert( pTarget[j].jnFlags & JNODE_LABEL );
187021 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
187022 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
187023 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
187024 if( pPatch[i+1].eType==JSON_NULL ){
187025 pTarget[j+1].jnFlags |= JNODE_REMOVE;
187026 }else{
187027 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
187028 if( pNew==0 ) return 0;
187029 pTarget = &pParse->aNode[iTarget];
187030 if( pNew!=&pTarget[j+1] ){
187031 pTarget[j+1].u.pPatch = pNew;
187032 pTarget[j+1].jnFlags |= JNODE_PATCH;
187033 }
187034 }
187035 break;
187036 }
187037 }
187038 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
187039 int iStart, iPatch;
187040 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
187041 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
187042 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
187043 if( pParse->oom ) return 0;
187044 jsonRemoveAllNulls(pPatch);
187045 pTarget = &pParse->aNode[iTarget];
187046 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
187047 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
187048 iRoot = iStart;
187049 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
187050 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
187051 }
187052 }
187053 return pTarget;
187054}
187055
187056/*
187057** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
187058** object that is the result of running the RFC 7396 MergePatch() algorithm
187059** on the two arguments.
187060*/
187061static void jsonPatchFunc(
187062 sqlite3_context *ctx,
187063 int argc,
187064 sqlite3_value **argv
187065){
187066 JsonParse x; /* The JSON that is being patched */
187067 JsonParse y; /* The patch */
187068 JsonNode *pResult; /* The result of the merge */
187069
187070 UNUSED_PARAM(argc);
187071 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
187072 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
187073 jsonParseReset(&x);
187074 return;
187075 }
187076 pResult = jsonMergePatch(&x, 0, y.aNode);
187077 assert( pResult!=0 || x.oom );
187078 if( pResult ){
187079 jsonReturnJson(pResult, ctx, 0);
187080 }else{
187081 sqlite3_result_error_nomem(ctx);
187082 }
187083 jsonParseReset(&x);
187084 jsonParseReset(&y);
187085}
187086
187087
187088/*
187089** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
187090** object that contains all name/value given in arguments. Or if any name
187091** is not a string or if any value is a BLOB, throw an error.
187092*/
187093static void jsonObjectFunc(
187094 sqlite3_context *ctx,
187095 int argc,
187096 sqlite3_value **argv
187097){
187098 int i;
187099 JsonString jx;
187100 const char *z;
187101 u32 n;
187102
187103 if( argc&1 ){
187104 sqlite3_result_error(ctx, "json_object() requires an even number "
187105 "of arguments", -1);
187106 return;
187107 }
187108 jsonInit(&jx, ctx);
187109 jsonAppendChar(&jx, '{');
187110 for(i=0; i<argc; i+=2){
187111 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
187112 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
187113 jsonReset(&jx);
187114 return;
187115 }
187116 jsonAppendSeparator(&jx);
187117 z = (const char*)sqlite3_value_text(argv[i]);
187118 n = (u32)sqlite3_value_bytes(argv[i]);
187119 jsonAppendString(&jx, z, n);
187120 jsonAppendChar(&jx, ':');
187121 jsonAppendValue(&jx, argv[i+1]);
187122 }
187123 jsonAppendChar(&jx, '}');
187124 jsonResult(&jx);
187125 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
187126}
187127
187128
187129/*
187130** json_remove(JSON, PATH, ...)
187131**
187132** Remove the named elements from JSON and return the result. malformed
187133** JSON or PATH arguments result in an error.
187134*/
187135static void jsonRemoveFunc(
187136 sqlite3_context *ctx,
187137 int argc,
187138 sqlite3_value **argv
187139){
187140 JsonParse x; /* The parse */
187141 JsonNode *pNode;
187142 const char *zPath;
187143 u32 i;
187144
187145 if( argc<1 ) return;
187146 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
187147 assert( x.nNode );
187148 for(i=1; i<(u32)argc; i++){
187149 zPath = (const char*)sqlite3_value_text(argv[i]);
187150 if( zPath==0 ) goto remove_done;
187151 pNode = jsonLookup(&x, zPath, 0, ctx);
187152 if( x.nErr ) goto remove_done;
187153 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
187154 }
187155 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
187156 jsonReturnJson(x.aNode, ctx, 0);
187157 }
187158remove_done:
187159 jsonParseReset(&x);
187160}
187161
187162/*
187163** json_replace(JSON, PATH, VALUE, ...)
187164**
187165** Replace the value at PATH with VALUE. If PATH does not already exist,
187166** this routine is a no-op. If JSON or PATH is malformed, throw an error.
187167*/
187168static void jsonReplaceFunc(
187169 sqlite3_context *ctx,
187170 int argc,
187171 sqlite3_value **argv
187172){
187173 JsonParse x; /* The parse */
187174 JsonNode *pNode;
187175 const char *zPath;
187176 u32 i;
187177
187178 if( argc<1 ) return;
187179 if( (argc&1)==0 ) {
187180 jsonWrongNumArgs(ctx, "replace");
187181 return;
187182 }
187183 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
187184 assert( x.nNode );
187185 for(i=1; i<(u32)argc; i+=2){
187186 zPath = (const char*)sqlite3_value_text(argv[i]);
187187 pNode = jsonLookup(&x, zPath, 0, ctx);
187188 if( x.nErr ) goto replace_err;
187189 if( pNode ){
187190 pNode->jnFlags |= (u8)JNODE_REPLACE;
187191 pNode->u.iReplace = i + 1;
187192 }
187193 }
187194 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
187195 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
187196 }else{
187197 jsonReturnJson(x.aNode, ctx, argv);
187198 }
187199replace_err:
187200 jsonParseReset(&x);
187201}
187202
187203/*
187204** json_set(JSON, PATH, VALUE, ...)
187205**
187206** Set the value at PATH to VALUE. Create the PATH if it does not already
187207** exist. Overwrite existing values that do exist.
187208** If JSON or PATH is malformed, throw an error.
187209**
187210** json_insert(JSON, PATH, VALUE, ...)
187211**
187212** Create PATH and initialize it to VALUE. If PATH already exists, this
187213** routine is a no-op. If JSON or PATH is malformed, throw an error.
187214*/
187215static void jsonSetFunc(
187216 sqlite3_context *ctx,
187217 int argc,
187218 sqlite3_value **argv
187219){
187220 JsonParse x; /* The parse */
187221 JsonNode *pNode;
187222 const char *zPath;
187223 u32 i;
187224 int bApnd;
187225 int bIsSet = *(int*)sqlite3_user_data(ctx);
187226
187227 if( argc<1 ) return;
187228 if( (argc&1)==0 ) {
187229 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
187230 return;
187231 }
187232 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
187233 assert( x.nNode );
187234 for(i=1; i<(u32)argc; i+=2){
187235 zPath = (const char*)sqlite3_value_text(argv[i]);
187236 bApnd = 0;
187237 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
187238 if( x.oom ){
187239 sqlite3_result_error_nomem(ctx);
187240 goto jsonSetDone;
187241 }else if( x.nErr ){
187242 goto jsonSetDone;
187243 }else if( pNode && (bApnd || bIsSet) ){
187244 pNode->jnFlags |= (u8)JNODE_REPLACE;
187245 pNode->u.iReplace = i + 1;
187246 }
187247 }
187248 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
187249 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
187250 }else{
187251 jsonReturnJson(x.aNode, ctx, argv);
187252 }
187253jsonSetDone:
187254 jsonParseReset(&x);
187255}
187256
187257/*
187258** json_type(JSON)
187259** json_type(JSON, PATH)
187260**
187261** Return the top-level "type" of a JSON string. Throw an error if
187262** either the JSON or PATH inputs are not well-formed.
187263*/
187264static void jsonTypeFunc(
187265 sqlite3_context *ctx,
187266 int argc,
187267 sqlite3_value **argv
187268){
187269 JsonParse *p; /* The parse */
187270 const char *zPath;
187271 JsonNode *pNode;
187272
187273 p = jsonParseCached(ctx, argv, ctx);
187274 if( p==0 ) return;
187275 if( argc==2 ){
187276 zPath = (const char*)sqlite3_value_text(argv[1]);
187277 pNode = jsonLookup(p, zPath, 0, ctx);
187278 }else{
187279 pNode = p->aNode;
187280 }
187281 if( pNode ){
187282 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
187283 }
187284}
187285
187286/*
187287** json_valid(JSON)
187288**
187289** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
187290** Return 0 otherwise.
187291*/
187292static void jsonValidFunc(
187293 sqlite3_context *ctx,
187294 int argc,
187295 sqlite3_value **argv
187296){
187297 JsonParse *p; /* The parse */
187298 UNUSED_PARAM(argc);
187299 p = jsonParseCached(ctx, argv, 0);
187300 sqlite3_result_int(ctx, p!=0);
187301}
187302
187303
187304/****************************************************************************
187305** Aggregate SQL function implementations
187306****************************************************************************/
187307/*
187308** json_group_array(VALUE)
187309**
187310** Return a JSON array composed of all values in the aggregate.
187311*/
187312static void jsonArrayStep(
187313 sqlite3_context *ctx,
187314 int argc,
187315 sqlite3_value **argv
187316){
187317 JsonString *pStr;
187318 UNUSED_PARAM(argc);
187319 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
187320 if( pStr ){
187321 if( pStr->zBuf==0 ){
187322 jsonInit(pStr, ctx);
187323 jsonAppendChar(pStr, '[');
187324 }else if( pStr->nUsed>1 ){
187325 jsonAppendChar(pStr, ',');
187326 pStr->pCtx = ctx;
187327 }
187328 jsonAppendValue(pStr, argv[0]);
187329 }
187330}
187331static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
187332 JsonString *pStr;
187333 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
187334 if( pStr ){
187335 pStr->pCtx = ctx;
187336 jsonAppendChar(pStr, ']');
187337 if( pStr->bErr ){
187338 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
187339 assert( pStr->bStatic );
187340 }else if( isFinal ){
187341 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
187342 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
187343 pStr->bStatic = 1;
187344 }else{
187345 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
187346 pStr->nUsed--;
187347 }
187348 }else{
187349 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
187350 }
187351 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
187352}
187353static void jsonArrayValue(sqlite3_context *ctx){
187354 jsonArrayCompute(ctx, 0);
187355}
187356static void jsonArrayFinal(sqlite3_context *ctx){
187357 jsonArrayCompute(ctx, 1);
187358}
187359
187360#ifndef SQLITE_OMIT_WINDOWFUNC
187361/*
187362** This method works for both json_group_array() and json_group_object().
187363** It works by removing the first element of the group by searching forward
187364** to the first comma (",") that is not within a string and deleting all
187365** text through that comma.
187366*/
187367static void jsonGroupInverse(
187368 sqlite3_context *ctx,
187369 int argc,
187370 sqlite3_value **argv
187371){
187372 unsigned int i;
187373 int inStr = 0;
187374 int nNest = 0;
187375 char *z;
187376 char c;
187377 JsonString *pStr;
187378 UNUSED_PARAM(argc);
187379 UNUSED_PARAM(argv);
187380 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
187381#ifdef NEVER
187382 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
187383 ** always have been called to initalize it */
187384 if( NEVER(!pStr) ) return;
187385#endif
187386 z = pStr->zBuf;
187387 for(i=1; (c = z[i])!=',' || inStr || nNest; i++){
187388 if( i>=pStr->nUsed ){
187389 pStr->nUsed = 1;
187390 return;
187391 }
187392 if( c=='"' ){
187393 inStr = !inStr;
187394 }else if( c=='\\' ){
187395 i++;
187396 }else if( !inStr ){
187397 if( c=='{' || c=='[' ) nNest++;
187398 if( c=='}' || c==']' ) nNest--;
187399 }
187400 }
187401 pStr->nUsed -= i;
187402 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
187403}
187404#else
187405# define jsonGroupInverse 0
187406#endif
187407
187408
187409/*
187410** json_group_obj(NAME,VALUE)
187411**
187412** Return a JSON object composed of all names and values in the aggregate.
187413*/
187414static void jsonObjectStep(
187415 sqlite3_context *ctx,
187416 int argc,
187417 sqlite3_value **argv
187418){
187419 JsonString *pStr;
187420 const char *z;
187421 u32 n;
187422 UNUSED_PARAM(argc);
187423 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
187424 if( pStr ){
187425 if( pStr->zBuf==0 ){
187426 jsonInit(pStr, ctx);
187427 jsonAppendChar(pStr, '{');
187428 }else if( pStr->nUsed>1 ){
187429 jsonAppendChar(pStr, ',');
187430 pStr->pCtx = ctx;
187431 }
187432 z = (const char*)sqlite3_value_text(argv[0]);
187433 n = (u32)sqlite3_value_bytes(argv[0]);
187434 jsonAppendString(pStr, z, n);
187435 jsonAppendChar(pStr, ':');
187436 jsonAppendValue(pStr, argv[1]);
187437 }
187438}
187439static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
187440 JsonString *pStr;
187441 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
187442 if( pStr ){
187443 jsonAppendChar(pStr, '}');
187444 if( pStr->bErr ){
187445 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
187446 assert( pStr->bStatic );
187447 }else if( isFinal ){
187448 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
187449 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
187450 pStr->bStatic = 1;
187451 }else{
187452 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
187453 pStr->nUsed--;
187454 }
187455 }else{
187456 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
187457 }
187458 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
187459}
187460static void jsonObjectValue(sqlite3_context *ctx){
187461 jsonObjectCompute(ctx, 0);
187462}
187463static void jsonObjectFinal(sqlite3_context *ctx){
187464 jsonObjectCompute(ctx, 1);
187465}
187466
187467
187468
187469#ifndef SQLITE_OMIT_VIRTUALTABLE
187470/****************************************************************************
187471** The json_each virtual table
187472****************************************************************************/
187473typedef struct JsonEachCursor JsonEachCursor;
187474struct JsonEachCursor {
187475 sqlite3_vtab_cursor base; /* Base class - must be first */
187476 u32 iRowid; /* The rowid */
187477 u32 iBegin; /* The first node of the scan */
187478 u32 i; /* Index in sParse.aNode[] of current row */
187479 u32 iEnd; /* EOF when i equals or exceeds this value */
187480 u8 eType; /* Type of top-level element */
187481 u8 bRecursive; /* True for json_tree(). False for json_each() */
187482 char *zJson; /* Input JSON */
187483 char *zRoot; /* Path by which to filter zJson */
187484 JsonParse sParse; /* Parse of the input JSON */
187485};
187486
187487/* Constructor for the json_each virtual table */
187488static int jsonEachConnect(
187489 sqlite3 *db,
187490 void *pAux,
187491 int argc, const char *const*argv,
187492 sqlite3_vtab **ppVtab,
187493 char **pzErr
187494){
187495 sqlite3_vtab *pNew;
187496 int rc;
187497
187498/* Column numbers */
187499#define JEACH_KEY 0
187500#define JEACH_VALUE 1
187501#define JEACH_TYPE 2
187502#define JEACH_ATOM 3
187503#define JEACH_ID 4
187504#define JEACH_PARENT 5
187505#define JEACH_FULLKEY 6
187506#define JEACH_PATH 7
187507/* The xBestIndex method assumes that the JSON and ROOT columns are
187508** the last two columns in the table. Should this ever changes, be
187509** sure to update the xBestIndex method. */
187510#define JEACH_JSON 8
187511#define JEACH_ROOT 9
187512
187513 UNUSED_PARAM(pzErr);
187514 UNUSED_PARAM(argv);
187515 UNUSED_PARAM(argc);
187516 UNUSED_PARAM(pAux);
187517 rc = sqlite3_declare_vtab(db,
187518 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
187519 "json HIDDEN,root HIDDEN)");
187520 if( rc==SQLITE_OK ){
187521 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
187522 if( pNew==0 ) return SQLITE_NOMEM;
187523 memset(pNew, 0, sizeof(*pNew));
187524 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
187525 }
187526 return rc;
187527}
187528
187529/* destructor for json_each virtual table */
187530static int jsonEachDisconnect(sqlite3_vtab *pVtab){
187531 sqlite3_free(pVtab);
187532 return SQLITE_OK;
187533}
187534
187535/* constructor for a JsonEachCursor object for json_each(). */
187536static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
187537 JsonEachCursor *pCur;
187538
187539 UNUSED_PARAM(p);
187540 pCur = sqlite3_malloc( sizeof(*pCur) );
187541 if( pCur==0 ) return SQLITE_NOMEM;
187542 memset(pCur, 0, sizeof(*pCur));
187543 *ppCursor = &pCur->base;
187544 return SQLITE_OK;
187545}
187546
187547/* constructor for a JsonEachCursor object for json_tree(). */
187548static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
187549 int rc = jsonEachOpenEach(p, ppCursor);
187550 if( rc==SQLITE_OK ){
187551 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
187552 pCur->bRecursive = 1;
187553 }
187554 return rc;
187555}
187556
187557/* Reset a JsonEachCursor back to its original state. Free any memory
187558** held. */
187559static void jsonEachCursorReset(JsonEachCursor *p){
187560 sqlite3_free(p->zJson);
187561 sqlite3_free(p->zRoot);
187562 jsonParseReset(&p->sParse);
187563 p->iRowid = 0;
187564 p->i = 0;
187565 p->iEnd = 0;
187566 p->eType = 0;
187567 p->zJson = 0;
187568 p->zRoot = 0;
187569}
187570
187571/* Destructor for a jsonEachCursor object */
187572static int jsonEachClose(sqlite3_vtab_cursor *cur){
187573 JsonEachCursor *p = (JsonEachCursor*)cur;
187574 jsonEachCursorReset(p);
187575 sqlite3_free(cur);
187576 return SQLITE_OK;
187577}
187578
187579/* Return TRUE if the jsonEachCursor object has been advanced off the end
187580** of the JSON object */
187581static int jsonEachEof(sqlite3_vtab_cursor *cur){
187582 JsonEachCursor *p = (JsonEachCursor*)cur;
187583 return p->i >= p->iEnd;
187584}
187585
187586/* Advance the cursor to the next element for json_tree() */
187587static int jsonEachNext(sqlite3_vtab_cursor *cur){
187588 JsonEachCursor *p = (JsonEachCursor*)cur;
187589 if( p->bRecursive ){
187590 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
187591 p->i++;
187592 p->iRowid++;
187593 if( p->i<p->iEnd ){
187594 u32 iUp = p->sParse.aUp[p->i];
187595 JsonNode *pUp = &p->sParse.aNode[iUp];
187596 p->eType = pUp->eType;
187597 if( pUp->eType==JSON_ARRAY ){
187598 if( iUp==p->i-1 ){
187599 pUp->u.iKey = 0;
187600 }else{
187601 pUp->u.iKey++;
187602 }
187603 }
187604 }
187605 }else{
187606 switch( p->eType ){
187607 case JSON_ARRAY: {
187608 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
187609 p->iRowid++;
187610 break;
187611 }
187612 case JSON_OBJECT: {
187613 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
187614 p->iRowid++;
187615 break;
187616 }
187617 default: {
187618 p->i = p->iEnd;
187619 break;
187620 }
187621 }
187622 }
187623 return SQLITE_OK;
187624}
187625
187626/* Append the name of the path for element i to pStr
187627*/
187628static void jsonEachComputePath(
187629 JsonEachCursor *p, /* The cursor */
187630 JsonString *pStr, /* Write the path here */
187631 u32 i /* Path to this element */
187632){
187633 JsonNode *pNode, *pUp;
187634 u32 iUp;
187635 if( i==0 ){
187636 jsonAppendChar(pStr, '$');
187637 return;
187638 }
187639 iUp = p->sParse.aUp[i];
187640 jsonEachComputePath(p, pStr, iUp);
187641 pNode = &p->sParse.aNode[i];
187642 pUp = &p->sParse.aNode[iUp];
187643 if( pUp->eType==JSON_ARRAY ){
187644 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
187645 }else{
187646 assert( pUp->eType==JSON_OBJECT );
187647 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
187648 assert( pNode->eType==JSON_STRING );
187649 assert( pNode->jnFlags & JNODE_LABEL );
187650 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
187651 }
187652}
187653
187654/* Return the value of a column */
187655static int jsonEachColumn(
187656 sqlite3_vtab_cursor *cur, /* The cursor */
187657 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
187658 int i /* Which column to return */
187659){
187660 JsonEachCursor *p = (JsonEachCursor*)cur;
187661 JsonNode *pThis = &p->sParse.aNode[p->i];
187662 switch( i ){
187663 case JEACH_KEY: {
187664 if( p->i==0 ) break;
187665 if( p->eType==JSON_OBJECT ){
187666 jsonReturn(pThis, ctx, 0);
187667 }else if( p->eType==JSON_ARRAY ){
187668 u32 iKey;
187669 if( p->bRecursive ){
187670 if( p->iRowid==0 ) break;
187671 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
187672 }else{
187673 iKey = p->iRowid;
187674 }
187675 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
187676 }
187677 break;
187678 }
187679 case JEACH_VALUE: {
187680 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
187681 jsonReturn(pThis, ctx, 0);
187682 break;
187683 }
187684 case JEACH_TYPE: {
187685 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
187686 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
187687 break;
187688 }
187689 case JEACH_ATOM: {
187690 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
187691 if( pThis->eType>=JSON_ARRAY ) break;
187692 jsonReturn(pThis, ctx, 0);
187693 break;
187694 }
187695 case JEACH_ID: {
187696 sqlite3_result_int64(ctx,
187697 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
187698 break;
187699 }
187700 case JEACH_PARENT: {
187701 if( p->i>p->iBegin && p->bRecursive ){
187702 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
187703 }
187704 break;
187705 }
187706 case JEACH_FULLKEY: {
187707 JsonString x;
187708 jsonInit(&x, ctx);
187709 if( p->bRecursive ){
187710 jsonEachComputePath(p, &x, p->i);
187711 }else{
187712 if( p->zRoot ){
187713 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
187714 }else{
187715 jsonAppendChar(&x, '$');
187716 }
187717 if( p->eType==JSON_ARRAY ){
187718 jsonPrintf(30, &x, "[%d]", p->iRowid);
187719 }else if( p->eType==JSON_OBJECT ){
187720 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
187721 }
187722 }
187723 jsonResult(&x);
187724 break;
187725 }
187726 case JEACH_PATH: {
187727 if( p->bRecursive ){
187728 JsonString x;
187729 jsonInit(&x, ctx);
187730 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
187731 jsonResult(&x);
187732 break;
187733 }
187734 /* For json_each() path and root are the same so fall through
187735 ** into the root case */
187736 /* no break */ deliberate_fall_through
187737 }
187738 default: {
187739 const char *zRoot = p->zRoot;
187740 if( zRoot==0 ) zRoot = "$";
187741 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
187742 break;
187743 }
187744 case JEACH_JSON: {
187745 assert( i==JEACH_JSON );
187746 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
187747 break;
187748 }
187749 }
187750 return SQLITE_OK;
187751}
187752
187753/* Return the current rowid value */
187754static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
187755 JsonEachCursor *p = (JsonEachCursor*)cur;
187756 *pRowid = p->iRowid;
187757 return SQLITE_OK;
187758}
187759
187760/* The query strategy is to look for an equality constraint on the json
187761** column. Without such a constraint, the table cannot operate. idxNum is
187762** 1 if the constraint is found, 3 if the constraint and zRoot are found,
187763** and 0 otherwise.
187764*/
187765static int jsonEachBestIndex(
187766 sqlite3_vtab *tab,
187767 sqlite3_index_info *pIdxInfo
187768){
187769 int i; /* Loop counter or computed array index */
187770 int aIdx[2]; /* Index of constraints for JSON and ROOT */
187771 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
187772 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
187773 const struct sqlite3_index_constraint *pConstraint;
187774
187775 /* This implementation assumes that JSON and ROOT are the last two
187776 ** columns in the table */
187777 assert( JEACH_ROOT == JEACH_JSON+1 );
187778 UNUSED_PARAM(tab);
187779 aIdx[0] = aIdx[1] = -1;
187780 pConstraint = pIdxInfo->aConstraint;
187781 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
187782 int iCol;
187783 int iMask;
187784 if( pConstraint->iColumn < JEACH_JSON ) continue;
187785 iCol = pConstraint->iColumn - JEACH_JSON;
187786 assert( iCol==0 || iCol==1 );
187787 iMask = 1 << iCol;
187788 if( pConstraint->usable==0 ){
187789 unusableMask |= iMask;
187790 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
187791 aIdx[iCol] = i;
187792 idxMask |= iMask;
187793 }
187794 }
187795 if( (unusableMask & ~idxMask)!=0 ){
187796 /* If there are any unusable constraints on JSON or ROOT, then reject
187797 ** this entire plan */
187798 return SQLITE_CONSTRAINT;
187799 }
187800 if( aIdx[0]<0 ){
187801 /* No JSON input. Leave estimatedCost at the huge value that it was
187802 ** initialized to to discourage the query planner from selecting this
187803 ** plan. */
187804 pIdxInfo->idxNum = 0;
187805 }else{
187806 pIdxInfo->estimatedCost = 1.0;
187807 i = aIdx[0];
187808 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
187809 pIdxInfo->aConstraintUsage[i].omit = 1;
187810 if( aIdx[1]<0 ){
187811 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
187812 }else{
187813 i = aIdx[1];
187814 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
187815 pIdxInfo->aConstraintUsage[i].omit = 1;
187816 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
187817 }
187818 }
187819 return SQLITE_OK;
187820}
187821
187822/* Start a search on a new JSON string */
187823static int jsonEachFilter(
187824 sqlite3_vtab_cursor *cur,
187825 int idxNum, const char *idxStr,
187826 int argc, sqlite3_value **argv
187827){
187828 JsonEachCursor *p = (JsonEachCursor*)cur;
187829 const char *z;
187830 const char *zRoot = 0;
187831 sqlite3_int64 n;
187832
187833 UNUSED_PARAM(idxStr);
187834 UNUSED_PARAM(argc);
187835 jsonEachCursorReset(p);
187836 if( idxNum==0 ) return SQLITE_OK;
187837 z = (const char*)sqlite3_value_text(argv[0]);
187838 if( z==0 ) return SQLITE_OK;
187839 n = sqlite3_value_bytes(argv[0]);
187840 p->zJson = sqlite3_malloc64( n+1 );
187841 if( p->zJson==0 ) return SQLITE_NOMEM;
187842 memcpy(p->zJson, z, (size_t)n+1);
187843 if( jsonParse(&p->sParse, 0, p->zJson) ){
187844 int rc = SQLITE_NOMEM;
187845 if( p->sParse.oom==0 ){
187846 sqlite3_free(cur->pVtab->zErrMsg);
187847 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
187848 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
187849 }
187850 jsonEachCursorReset(p);
187851 return rc;
187852 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
187853 jsonEachCursorReset(p);
187854 return SQLITE_NOMEM;
187855 }else{
187856 JsonNode *pNode = 0;
187857 if( idxNum==3 ){
187858 const char *zErr = 0;
187859 zRoot = (const char*)sqlite3_value_text(argv[1]);
187860 if( zRoot==0 ) return SQLITE_OK;
187861 n = sqlite3_value_bytes(argv[1]);
187862 p->zRoot = sqlite3_malloc64( n+1 );
187863 if( p->zRoot==0 ) return SQLITE_NOMEM;
187864 memcpy(p->zRoot, zRoot, (size_t)n+1);
187865 if( zRoot[0]!='$' ){
187866 zErr = zRoot;
187867 }else{
187868 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
187869 }
187870 if( zErr ){
187871 sqlite3_free(cur->pVtab->zErrMsg);
187872 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
187873 jsonEachCursorReset(p);
187874 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
187875 }else if( pNode==0 ){
187876 return SQLITE_OK;
187877 }
187878 }else{
187879 pNode = p->sParse.aNode;
187880 }
187881 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
187882 p->eType = pNode->eType;
187883 if( p->eType>=JSON_ARRAY ){
187884 pNode->u.iKey = 0;
187885 p->iEnd = p->i + pNode->n + 1;
187886 if( p->bRecursive ){
187887 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
187888 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
187889 p->i--;
187890 }
187891 }else{
187892 p->i++;
187893 }
187894 }else{
187895 p->iEnd = p->i+1;
187896 }
187897 }
187898 return SQLITE_OK;
187899}
187900
187901/* The methods of the json_each virtual table */
187902static sqlite3_module jsonEachModule = {
187903 0, /* iVersion */
187904 0, /* xCreate */
187905 jsonEachConnect, /* xConnect */
187906 jsonEachBestIndex, /* xBestIndex */
187907 jsonEachDisconnect, /* xDisconnect */
187908 0, /* xDestroy */
187909 jsonEachOpenEach, /* xOpen - open a cursor */
187910 jsonEachClose, /* xClose - close a cursor */
187911 jsonEachFilter, /* xFilter - configure scan constraints */
187912 jsonEachNext, /* xNext - advance a cursor */
187913 jsonEachEof, /* xEof - check for end of scan */
187914 jsonEachColumn, /* xColumn - read data */
187915 jsonEachRowid, /* xRowid - read data */
187916 0, /* xUpdate */
187917 0, /* xBegin */
187918 0, /* xSync */
187919 0, /* xCommit */
187920 0, /* xRollback */
187921 0, /* xFindMethod */
187922 0, /* xRename */
187923 0, /* xSavepoint */
187924 0, /* xRelease */
187925 0, /* xRollbackTo */
187926 0 /* xShadowName */
187927};
187928
187929/* The methods of the json_tree virtual table. */
187930static sqlite3_module jsonTreeModule = {
187931 0, /* iVersion */
187932 0, /* xCreate */
187933 jsonEachConnect, /* xConnect */
187934 jsonEachBestIndex, /* xBestIndex */
187935 jsonEachDisconnect, /* xDisconnect */
187936 0, /* xDestroy */
187937 jsonEachOpenTree, /* xOpen - open a cursor */
187938 jsonEachClose, /* xClose - close a cursor */
187939 jsonEachFilter, /* xFilter - configure scan constraints */
187940 jsonEachNext, /* xNext - advance a cursor */
187941 jsonEachEof, /* xEof - check for end of scan */
187942 jsonEachColumn, /* xColumn - read data */
187943 jsonEachRowid, /* xRowid - read data */
187944 0, /* xUpdate */
187945 0, /* xBegin */
187946 0, /* xSync */
187947 0, /* xCommit */
187948 0, /* xRollback */
187949 0, /* xFindMethod */
187950 0, /* xRename */
187951 0, /* xSavepoint */
187952 0, /* xRelease */
187953 0, /* xRollbackTo */
187954 0 /* xShadowName */
187955};
187956#endif /* SQLITE_OMIT_VIRTUALTABLE */
187957
187958/****************************************************************************
187959** The following routines are the only publically visible identifiers in this
187960** file. Call the following routines in order to register the various SQL
187961** functions and the virtual table implemented by this file.
187962****************************************************************************/
187963
187964SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
187965 int rc = SQLITE_OK;
187966 unsigned int i;
187967 static const struct {
187968 const char *zName;
187969 int nArg;
187970 int flag;
187971 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
187972 } aFunc[] = {
187973 { "json", 1, 0, jsonRemoveFunc },
187974 { "json_array", -1, 0, jsonArrayFunc },
187975 { "json_array_length", 1, 0, jsonArrayLengthFunc },
187976 { "json_array_length", 2, 0, jsonArrayLengthFunc },
187977 { "json_extract", -1, 0, jsonExtractFunc },
187978 { "json_insert", -1, 0, jsonSetFunc },
187979 { "json_object", -1, 0, jsonObjectFunc },
187980 { "json_patch", 2, 0, jsonPatchFunc },
187981 { "json_quote", 1, 0, jsonQuoteFunc },
187982 { "json_remove", -1, 0, jsonRemoveFunc },
187983 { "json_replace", -1, 0, jsonReplaceFunc },
187984 { "json_set", -1, 1, jsonSetFunc },
187985 { "json_type", 1, 0, jsonTypeFunc },
187986 { "json_type", 2, 0, jsonTypeFunc },
187987 { "json_valid", 1, 0, jsonValidFunc },
187988
187989#if SQLITE_DEBUG
187990 /* DEBUG and TESTING functions */
187991 { "json_parse", 1, 0, jsonParseFunc },
187992 { "json_test1", 1, 0, jsonTest1Func },
187993#endif
187994 };
187995 static const struct {
187996 const char *zName;
187997 int nArg;
187998 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
187999 void (*xFinal)(sqlite3_context*);
188000 void (*xValue)(sqlite3_context*);
188001 } aAgg[] = {
188002 { "json_group_array", 1,
188003 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
188004 { "json_group_object", 2,
188005 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
188006 };
188007#ifndef SQLITE_OMIT_VIRTUALTABLE
188008 static const struct {
188009 const char *zName;
188010 sqlite3_module *pModule;
188011 } aMod[] = {
188012 { "json_each", &jsonEachModule },
188013 { "json_tree", &jsonTreeModule },
188014 };
188015#endif
188016 static const int enc =
188017 SQLITE_UTF8 |
188018 SQLITE_DETERMINISTIC |
188019 SQLITE_INNOCUOUS;
188020 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
188021 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, enc,
188022 (void*)&aFunc[i].flag,
188023 aFunc[i].xFunc, 0, 0);
188024 }
188025#ifndef SQLITE_OMIT_WINDOWFUNC
188026 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
188027 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
188028 SQLITE_SUBTYPE | enc, 0,
188029 aAgg[i].xStep, aAgg[i].xFinal,
188030 aAgg[i].xValue, jsonGroupInverse, 0);
188031 }
188032#endif
188033#ifndef SQLITE_OMIT_VIRTUALTABLE
188034 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
188035 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
188036 }
188037#endif
188038 return rc;
188039}
188040
188041
188042#ifndef SQLITE_CORE
188043#ifdef _WIN32
188044__declspec(dllexport)
188045#endif
188046SQLITE_API int sqlite3_json_init(
188047 sqlite3 *db,
188048 char **pzErrMsg,
188049 const sqlite3_api_routines *pApi
188050){
188051 SQLITE_EXTENSION_INIT2(pApi);
188052 (void)pzErrMsg; /* Unused parameter */
188053 return sqlite3Json1Init(db);
188054}
188055#endif
188056#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
188057
188058/************** End of json1.c ***********************************************/
188059/************** Begin file rtree.c *******************************************/
188060/*
188061** 2001 September 15
188062**
188063** The author disclaims copyright to this source code. In place of
188064** a legal notice, here is a blessing:
188065**
188066** May you do good and not evil.
188067** May you find forgiveness for yourself and forgive others.
188068** May you share freely, never taking more than you give.
188069**
188070*************************************************************************
188071** This file contains code for implementations of the r-tree and r*-tree
188072** algorithms packaged as an SQLite virtual table module.
188073*/
188074
188075/*
188076** Database Format of R-Tree Tables
188077** --------------------------------
188078**
188079** The data structure for a single virtual r-tree table is stored in three
188080** native SQLite tables declared as follows. In each case, the '%' character
188081** in the table name is replaced with the user-supplied name of the r-tree
188082** table.
188083**
188084** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
188085** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
188086** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
188087**
188088** The data for each node of the r-tree structure is stored in the %_node
188089** table. For each node that is not the root node of the r-tree, there is
188090** an entry in the %_parent table associating the node with its parent.
188091** And for each row of data in the table, there is an entry in the %_rowid
188092** table that maps from the entries rowid to the id of the node that it
188093** is stored on. If the r-tree contains auxiliary columns, those are stored
188094** on the end of the %_rowid table.
188095**
188096** The root node of an r-tree always exists, even if the r-tree table is
188097** empty. The nodeno of the root node is always 1. All other nodes in the
188098** table must be the same size as the root node. The content of each node
188099** is formatted as follows:
188100**
188101** 1. If the node is the root node (node 1), then the first 2 bytes
188102** of the node contain the tree depth as a big-endian integer.
188103** For non-root nodes, the first 2 bytes are left unused.
188104**
188105** 2. The next 2 bytes contain the number of entries currently
188106** stored in the node.
188107**
188108** 3. The remainder of the node contains the node entries. Each entry
188109** consists of a single 8-byte integer followed by an even number
188110** of 4-byte coordinates. For leaf nodes the integer is the rowid
188111** of a record. For internal nodes it is the node number of a
188112** child page.
188113*/
188114
188115#if !defined(SQLITE_CORE) \
188116 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
188117
188118#ifndef SQLITE_CORE
188119/* #include "sqlite3ext.h" */
188120 SQLITE_EXTENSION_INIT1
188121#else
188122/* #include "sqlite3.h" */
188123#endif
188124SQLITE_PRIVATE int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
188125
188126#ifndef SQLITE_AMALGAMATION
188127#include "sqlite3rtree.h"
188128typedef sqlite3_int64 i64;
188129typedef sqlite3_uint64 u64;
188130typedef unsigned char u8;
188131typedef unsigned short u16;
188132typedef unsigned int u32;
188133#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
188134# define NDEBUG 1
188135#endif
188136#if defined(NDEBUG) && defined(SQLITE_DEBUG)
188137# undef NDEBUG
188138#endif
188139#endif
188140
188141/* #include <string.h> */
188142/* #include <stdio.h> */
188143/* #include <assert.h> */
188144/* #include <stdlib.h> */
188145
188146/* The following macro is used to suppress compiler warnings.
188147*/
188148#ifndef UNUSED_PARAMETER
188149# define UNUSED_PARAMETER(x) (void)(x)
188150#endif
188151
188152typedef struct Rtree Rtree;
188153typedef struct RtreeCursor RtreeCursor;
188154typedef struct RtreeNode RtreeNode;
188155typedef struct RtreeCell RtreeCell;
188156typedef struct RtreeConstraint RtreeConstraint;
188157typedef struct RtreeMatchArg RtreeMatchArg;
188158typedef struct RtreeGeomCallback RtreeGeomCallback;
188159typedef union RtreeCoord RtreeCoord;
188160typedef struct RtreeSearchPoint RtreeSearchPoint;
188161
188162/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
188163#define RTREE_MAX_DIMENSIONS 5
188164
188165/* Maximum number of auxiliary columns */
188166#define RTREE_MAX_AUX_COLUMN 100
188167
188168/* Size of hash table Rtree.aHash. This hash table is not expected to
188169** ever contain very many entries, so a fixed number of buckets is
188170** used.
188171*/
188172#define HASHSIZE 97
188173
188174/* The xBestIndex method of this virtual table requires an estimate of
188175** the number of rows in the virtual table to calculate the costs of
188176** various strategies. If possible, this estimate is loaded from the
188177** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
188178** Otherwise, if no sqlite_stat1 entry is available, use
188179** RTREE_DEFAULT_ROWEST.
188180*/
188181#define RTREE_DEFAULT_ROWEST 1048576
188182#define RTREE_MIN_ROWEST 100
188183
188184/*
188185** An rtree virtual-table object.
188186*/
188187struct Rtree {
188188 sqlite3_vtab base; /* Base class. Must be first */
188189 sqlite3 *db; /* Host database connection */
188190 int iNodeSize; /* Size in bytes of each node in the node table */
188191 u8 nDim; /* Number of dimensions */
188192 u8 nDim2; /* Twice the number of dimensions */
188193 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
188194 u8 nBytesPerCell; /* Bytes consumed per cell */
188195 u8 inWrTrans; /* True if inside write transaction */
188196 u8 nAux; /* # of auxiliary columns in %_rowid */
188197 u8 nAuxNotNull; /* Number of initial not-null aux columns */
188198#ifdef SQLITE_DEBUG
188199 u8 bCorrupt; /* Shadow table corruption detected */
188200#endif
188201 int iDepth; /* Current depth of the r-tree structure */
188202 char *zDb; /* Name of database containing r-tree table */
188203 char *zName; /* Name of r-tree table */
188204 u32 nBusy; /* Current number of users of this structure */
188205 i64 nRowEst; /* Estimated number of rows in this table */
188206 u32 nCursor; /* Number of open cursors */
188207 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
188208 char *zReadAuxSql; /* SQL for statement to read aux data */
188209
188210 /* List of nodes removed during a CondenseTree operation. List is
188211 ** linked together via the pointer normally used for hash chains -
188212 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
188213 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
188214 */
188215 RtreeNode *pDeleted;
188216 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
188217
188218 /* Blob I/O on xxx_node */
188219 sqlite3_blob *pNodeBlob;
188220
188221 /* Statements to read/write/delete a record from xxx_node */
188222 sqlite3_stmt *pWriteNode;
188223 sqlite3_stmt *pDeleteNode;
188224
188225 /* Statements to read/write/delete a record from xxx_rowid */
188226 sqlite3_stmt *pReadRowid;
188227 sqlite3_stmt *pWriteRowid;
188228 sqlite3_stmt *pDeleteRowid;
188229
188230 /* Statements to read/write/delete a record from xxx_parent */
188231 sqlite3_stmt *pReadParent;
188232 sqlite3_stmt *pWriteParent;
188233 sqlite3_stmt *pDeleteParent;
188234
188235 /* Statement for writing to the "aux:" fields, if there are any */
188236 sqlite3_stmt *pWriteAux;
188237
188238 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
188239};
188240
188241/* Possible values for Rtree.eCoordType: */
188242#define RTREE_COORD_REAL32 0
188243#define RTREE_COORD_INT32 1
188244
188245/*
188246** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
188247** only deal with integer coordinates. No floating point operations
188248** will be done.
188249*/
188250#ifdef SQLITE_RTREE_INT_ONLY
188251 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
188252 typedef int RtreeValue; /* Low accuracy coordinate */
188253# define RTREE_ZERO 0
188254#else
188255 typedef double RtreeDValue; /* High accuracy coordinate */
188256 typedef float RtreeValue; /* Low accuracy coordinate */
188257# define RTREE_ZERO 0.0
188258#endif
188259
188260/*
188261** Set the Rtree.bCorrupt flag
188262*/
188263#ifdef SQLITE_DEBUG
188264# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
188265#else
188266# define RTREE_IS_CORRUPT(X)
188267#endif
188268
188269/*
188270** When doing a search of an r-tree, instances of the following structure
188271** record intermediate results from the tree walk.
188272**
188273** The id is always a node-id. For iLevel>=1 the id is the node-id of
188274** the node that the RtreeSearchPoint represents. When iLevel==0, however,
188275** the id is of the parent node and the cell that RtreeSearchPoint
188276** represents is the iCell-th entry in the parent node.
188277*/
188278struct RtreeSearchPoint {
188279 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
188280 sqlite3_int64 id; /* Node ID */
188281 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
188282 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
188283 u8 iCell; /* Cell index within the node */
188284};
188285
188286/*
188287** The minimum number of cells allowed for a node is a third of the
188288** maximum. In Gutman's notation:
188289**
188290** m = M/3
188291**
188292** If an R*-tree "Reinsert" operation is required, the same number of
188293** cells are removed from the overfull node and reinserted into the tree.
188294*/
188295#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
188296#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
188297#define RTREE_MAXCELLS 51
188298
188299/*
188300** The smallest possible node-size is (512-64)==448 bytes. And the largest
188301** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
188302** Therefore all non-root nodes must contain at least 3 entries. Since
188303** 3^40 is greater than 2^64, an r-tree structure always has a depth of
188304** 40 or less.
188305*/
188306#define RTREE_MAX_DEPTH 40
188307
188308
188309/*
188310** Number of entries in the cursor RtreeNode cache. The first entry is
188311** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
188312** entries cache the RtreeNode for the first elements of the priority queue.
188313*/
188314#define RTREE_CACHE_SZ 5
188315
188316/*
188317** An rtree cursor object.
188318*/
188319struct RtreeCursor {
188320 sqlite3_vtab_cursor base; /* Base class. Must be first */
188321 u8 atEOF; /* True if at end of search */
188322 u8 bPoint; /* True if sPoint is valid */
188323 u8 bAuxValid; /* True if pReadAux is valid */
188324 int iStrategy; /* Copy of idxNum search parameter */
188325 int nConstraint; /* Number of entries in aConstraint */
188326 RtreeConstraint *aConstraint; /* Search constraints. */
188327 int nPointAlloc; /* Number of slots allocated for aPoint[] */
188328 int nPoint; /* Number of slots used in aPoint[] */
188329 int mxLevel; /* iLevel value for root of the tree */
188330 RtreeSearchPoint *aPoint; /* Priority queue for search points */
188331 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
188332 RtreeSearchPoint sPoint; /* Cached next search point */
188333 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
188334 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
188335};
188336
188337/* Return the Rtree of a RtreeCursor */
188338#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
188339
188340/*
188341** A coordinate can be either a floating point number or a integer. All
188342** coordinates within a single R-Tree are always of the same time.
188343*/
188344union RtreeCoord {
188345 RtreeValue f; /* Floating point value */
188346 int i; /* Integer value */
188347 u32 u; /* Unsigned for byte-order conversions */
188348};
188349
188350/*
188351** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
188352** formatted as a RtreeDValue (double or int64). This macro assumes that local
188353** variable pRtree points to the Rtree structure associated with the
188354** RtreeCoord.
188355*/
188356#ifdef SQLITE_RTREE_INT_ONLY
188357# define DCOORD(coord) ((RtreeDValue)coord.i)
188358#else
188359# define DCOORD(coord) ( \
188360 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
188361 ((double)coord.f) : \
188362 ((double)coord.i) \
188363 )
188364#endif
188365
188366/*
188367** A search constraint.
188368*/
188369struct RtreeConstraint {
188370 int iCoord; /* Index of constrained coordinate */
188371 int op; /* Constraining operation */
188372 union {
188373 RtreeDValue rValue; /* Constraint value. */
188374 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
188375 int (*xQueryFunc)(sqlite3_rtree_query_info*);
188376 } u;
188377 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
188378};
188379
188380/* Possible values for RtreeConstraint.op */
188381#define RTREE_EQ 0x41 /* A */
188382#define RTREE_LE 0x42 /* B */
188383#define RTREE_LT 0x43 /* C */
188384#define RTREE_GE 0x44 /* D */
188385#define RTREE_GT 0x45 /* E */
188386#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
188387#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
188388
188389/* Special operators available only on cursors. Needs to be consecutive
188390** with the normal values above, but must be less than RTREE_MATCH. These
188391** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
188392** x<'xyz' (RTREE_TRUE) */
188393#define RTREE_TRUE 0x3f /* ? */
188394#define RTREE_FALSE 0x40 /* @ */
188395
188396/*
188397** An rtree structure node.
188398*/
188399struct RtreeNode {
188400 RtreeNode *pParent; /* Parent node */
188401 i64 iNode; /* The node number */
188402 int nRef; /* Number of references to this node */
188403 int isDirty; /* True if the node needs to be written to disk */
188404 u8 *zData; /* Content of the node, as should be on disk */
188405 RtreeNode *pNext; /* Next node in this hash collision chain */
188406};
188407
188408/* Return the number of cells in a node */
188409#define NCELL(pNode) readInt16(&(pNode)->zData[2])
188410
188411/*
188412** A single cell from a node, deserialized
188413*/
188414struct RtreeCell {
188415 i64 iRowid; /* Node or entry ID */
188416 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
188417};
188418
188419
188420/*
188421** This object becomes the sqlite3_user_data() for the SQL functions
188422** that are created by sqlite3_rtree_geometry_callback() and
188423** sqlite3_rtree_query_callback() and which appear on the right of MATCH
188424** operators in order to constrain a search.
188425**
188426** xGeom and xQueryFunc are the callback functions. Exactly one of
188427** xGeom and xQueryFunc fields is non-NULL, depending on whether the
188428** SQL function was created using sqlite3_rtree_geometry_callback() or
188429** sqlite3_rtree_query_callback().
188430**
188431** This object is deleted automatically by the destructor mechanism in
188432** sqlite3_create_function_v2().
188433*/
188434struct RtreeGeomCallback {
188435 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
188436 int (*xQueryFunc)(sqlite3_rtree_query_info*);
188437 void (*xDestructor)(void*);
188438 void *pContext;
188439};
188440
188441/*
188442** An instance of this structure (in the form of a BLOB) is returned by
188443** the SQL functions that sqlite3_rtree_geometry_callback() and
188444** sqlite3_rtree_query_callback() create, and is read as the right-hand
188445** operand to the MATCH operator of an R-Tree.
188446*/
188447struct RtreeMatchArg {
188448 u32 iSize; /* Size of this object */
188449 RtreeGeomCallback cb; /* Info about the callback functions */
188450 int nParam; /* Number of parameters to the SQL function */
188451 sqlite3_value **apSqlParam; /* Original SQL parameter values */
188452 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
188453};
188454
188455#ifndef MAX
188456# define MAX(x,y) ((x) < (y) ? (y) : (x))
188457#endif
188458#ifndef MIN
188459# define MIN(x,y) ((x) > (y) ? (y) : (x))
188460#endif
188461
188462/* What version of GCC is being used. 0 means GCC is not being used .
188463** Note that the GCC_VERSION macro will also be set correctly when using
188464** clang, since clang works hard to be gcc compatible. So the gcc
188465** optimizations will also work when compiling with clang.
188466*/
188467#ifndef GCC_VERSION
188468#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
188469# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
188470#else
188471# define GCC_VERSION 0
188472#endif
188473#endif
188474
188475/* The testcase() macro should already be defined in the amalgamation. If
188476** it is not, make it a no-op.
188477*/
188478#ifndef SQLITE_AMALGAMATION
188479# define testcase(X)
188480#endif
188481
188482/*
188483** Make sure that the compiler intrinsics we desire are enabled when
188484** compiling with an appropriate version of MSVC unless prevented by
188485** the SQLITE_DISABLE_INTRINSIC define.
188486*/
188487#if !defined(SQLITE_DISABLE_INTRINSIC)
188488# if defined(_MSC_VER) && _MSC_VER>=1400
188489# if !defined(_WIN32_WCE)
188490/* # include <intrin.h> */
188491# pragma intrinsic(_byteswap_ulong)
188492# pragma intrinsic(_byteswap_uint64)
188493# else
188494/* # include <cmnintrin.h> */
188495# endif
188496# endif
188497#endif
188498
188499/*
188500** Macros to determine whether the machine is big or little endian,
188501** and whether or not that determination is run-time or compile-time.
188502**
188503** For best performance, an attempt is made to guess at the byte-order
188504** using C-preprocessor macros. If that is unsuccessful, or if
188505** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
188506** at run-time.
188507*/
188508#ifndef SQLITE_BYTEORDER
188509#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
188510 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
188511 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
188512 defined(__arm__)
188513# define SQLITE_BYTEORDER 1234
188514#elif defined(sparc) || defined(__ppc__)
188515# define SQLITE_BYTEORDER 4321
188516#else
188517# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
188518#endif
188519#endif
188520
188521
188522/* What version of MSVC is being used. 0 means MSVC is not being used */
188523#ifndef MSVC_VERSION
188524#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
188525# define MSVC_VERSION _MSC_VER
188526#else
188527# define MSVC_VERSION 0
188528#endif
188529#endif
188530
188531/*
188532** Functions to deserialize a 16 bit integer, 32 bit real number and
188533** 64 bit integer. The deserialized value is returned.
188534*/
188535static int readInt16(u8 *p){
188536 return (p[0]<<8) + p[1];
188537}
188538static void readCoord(u8 *p, RtreeCoord *pCoord){
188539 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
188540#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
188541 pCoord->u = _byteswap_ulong(*(u32*)p);
188542#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
188543 pCoord->u = __builtin_bswap32(*(u32*)p);
188544#elif SQLITE_BYTEORDER==4321
188545 pCoord->u = *(u32*)p;
188546#else
188547 pCoord->u = (
188548 (((u32)p[0]) << 24) +
188549 (((u32)p[1]) << 16) +
188550 (((u32)p[2]) << 8) +
188551 (((u32)p[3]) << 0)
188552 );
188553#endif
188554}
188555static i64 readInt64(u8 *p){
188556#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
188557 u64 x;
188558 memcpy(&x, p, 8);
188559 return (i64)_byteswap_uint64(x);
188560#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
188561 u64 x;
188562 memcpy(&x, p, 8);
188563 return (i64)__builtin_bswap64(x);
188564#elif SQLITE_BYTEORDER==4321
188565 i64 x;
188566 memcpy(&x, p, 8);
188567 return x;
188568#else
188569 return (i64)(
188570 (((u64)p[0]) << 56) +
188571 (((u64)p[1]) << 48) +
188572 (((u64)p[2]) << 40) +
188573 (((u64)p[3]) << 32) +
188574 (((u64)p[4]) << 24) +
188575 (((u64)p[5]) << 16) +
188576 (((u64)p[6]) << 8) +
188577 (((u64)p[7]) << 0)
188578 );
188579#endif
188580}
188581
188582/*
188583** Functions to serialize a 16 bit integer, 32 bit real number and
188584** 64 bit integer. The value returned is the number of bytes written
188585** to the argument buffer (always 2, 4 and 8 respectively).
188586*/
188587static void writeInt16(u8 *p, int i){
188588 p[0] = (i>> 8)&0xFF;
188589 p[1] = (i>> 0)&0xFF;
188590}
188591static int writeCoord(u8 *p, RtreeCoord *pCoord){
188592 u32 i;
188593 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
188594 assert( sizeof(RtreeCoord)==4 );
188595 assert( sizeof(u32)==4 );
188596#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
188597 i = __builtin_bswap32(pCoord->u);
188598 memcpy(p, &i, 4);
188599#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
188600 i = _byteswap_ulong(pCoord->u);
188601 memcpy(p, &i, 4);
188602#elif SQLITE_BYTEORDER==4321
188603 i = pCoord->u;
188604 memcpy(p, &i, 4);
188605#else
188606 i = pCoord->u;
188607 p[0] = (i>>24)&0xFF;
188608 p[1] = (i>>16)&0xFF;
188609 p[2] = (i>> 8)&0xFF;
188610 p[3] = (i>> 0)&0xFF;
188611#endif
188612 return 4;
188613}
188614static int writeInt64(u8 *p, i64 i){
188615#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
188616 i = (i64)__builtin_bswap64((u64)i);
188617 memcpy(p, &i, 8);
188618#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
188619 i = (i64)_byteswap_uint64((u64)i);
188620 memcpy(p, &i, 8);
188621#elif SQLITE_BYTEORDER==4321
188622 memcpy(p, &i, 8);
188623#else
188624 p[0] = (i>>56)&0xFF;
188625 p[1] = (i>>48)&0xFF;
188626 p[2] = (i>>40)&0xFF;
188627 p[3] = (i>>32)&0xFF;
188628 p[4] = (i>>24)&0xFF;
188629 p[5] = (i>>16)&0xFF;
188630 p[6] = (i>> 8)&0xFF;
188631 p[7] = (i>> 0)&0xFF;
188632#endif
188633 return 8;
188634}
188635
188636/*
188637** Increment the reference count of node p.
188638*/
188639static void nodeReference(RtreeNode *p){
188640 if( p ){
188641 assert( p->nRef>0 );
188642 p->nRef++;
188643 }
188644}
188645
188646/*
188647** Clear the content of node p (set all bytes to 0x00).
188648*/
188649static void nodeZero(Rtree *pRtree, RtreeNode *p){
188650 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
188651 p->isDirty = 1;
188652}
188653
188654/*
188655** Given a node number iNode, return the corresponding key to use
188656** in the Rtree.aHash table.
188657*/
188658static unsigned int nodeHash(i64 iNode){
188659 return ((unsigned)iNode) % HASHSIZE;
188660}
188661
188662/*
188663** Search the node hash table for node iNode. If found, return a pointer
188664** to it. Otherwise, return 0.
188665*/
188666static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
188667 RtreeNode *p;
188668 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
188669 return p;
188670}
188671
188672/*
188673** Add node pNode to the node hash table.
188674*/
188675static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
188676 int iHash;
188677 assert( pNode->pNext==0 );
188678 iHash = nodeHash(pNode->iNode);
188679 pNode->pNext = pRtree->aHash[iHash];
188680 pRtree->aHash[iHash] = pNode;
188681}
188682
188683/*
188684** Remove node pNode from the node hash table.
188685*/
188686static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
188687 RtreeNode **pp;
188688 if( pNode->iNode!=0 ){
188689 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
188690 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
188691 *pp = pNode->pNext;
188692 pNode->pNext = 0;
188693 }
188694}
188695
188696/*
188697** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
188698** indicating that node has not yet been assigned a node number. It is
188699** assigned a node number when nodeWrite() is called to write the
188700** node contents out to the database.
188701*/
188702static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
188703 RtreeNode *pNode;
188704 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
188705 if( pNode ){
188706 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
188707 pNode->zData = (u8 *)&pNode[1];
188708 pNode->nRef = 1;
188709 pRtree->nNodeRef++;
188710 pNode->pParent = pParent;
188711 pNode->isDirty = 1;
188712 nodeReference(pParent);
188713 }
188714 return pNode;
188715}
188716
188717/*
188718** Clear the Rtree.pNodeBlob object
188719*/
188720static void nodeBlobReset(Rtree *pRtree){
188721 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
188722 sqlite3_blob *pBlob = pRtree->pNodeBlob;
188723 pRtree->pNodeBlob = 0;
188724 sqlite3_blob_close(pBlob);
188725 }
188726}
188727
188728/*
188729** Check to see if pNode is the same as pParent or any of the parents
188730** of pParent.
188731*/
188732static int nodeInParentChain(const RtreeNode *pNode, const RtreeNode *pParent){
188733 do{
188734 if( pNode==pParent ) return 1;
188735 pParent = pParent->pParent;
188736 }while( pParent );
188737 return 0;
188738}
188739
188740/*
188741** Obtain a reference to an r-tree node.
188742*/
188743static int nodeAcquire(
188744 Rtree *pRtree, /* R-tree structure */
188745 i64 iNode, /* Node number to load */
188746 RtreeNode *pParent, /* Either the parent node or NULL */
188747 RtreeNode **ppNode /* OUT: Acquired node */
188748){
188749 int rc = SQLITE_OK;
188750 RtreeNode *pNode = 0;
188751
188752 /* Check if the requested node is already in the hash table. If so,
188753 ** increase its reference count and return it.
188754 */
188755 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
188756 if( pParent && !pNode->pParent ){
188757 if( nodeInParentChain(pNode, pParent) ){
188758 RTREE_IS_CORRUPT(pRtree);
188759 return SQLITE_CORRUPT_VTAB;
188760 }
188761 pParent->nRef++;
188762 pNode->pParent = pParent;
188763 }else if( pParent && pNode->pParent && pParent!=pNode->pParent ){
188764 RTREE_IS_CORRUPT(pRtree);
188765 return SQLITE_CORRUPT_VTAB;
188766 }
188767 pNode->nRef++;
188768 *ppNode = pNode;
188769 return SQLITE_OK;
188770 }
188771
188772 if( pRtree->pNodeBlob ){
188773 sqlite3_blob *pBlob = pRtree->pNodeBlob;
188774 pRtree->pNodeBlob = 0;
188775 rc = sqlite3_blob_reopen(pBlob, iNode);
188776 pRtree->pNodeBlob = pBlob;
188777 if( rc ){
188778 nodeBlobReset(pRtree);
188779 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
188780 }
188781 }
188782 if( pRtree->pNodeBlob==0 ){
188783 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
188784 if( zTab==0 ) return SQLITE_NOMEM;
188785 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
188786 &pRtree->pNodeBlob);
188787 sqlite3_free(zTab);
188788 }
188789 if( rc ){
188790 nodeBlobReset(pRtree);
188791 *ppNode = 0;
188792 /* If unable to open an sqlite3_blob on the desired row, that can only
188793 ** be because the shadow tables hold erroneous data. */
188794 if( rc==SQLITE_ERROR ){
188795 rc = SQLITE_CORRUPT_VTAB;
188796 RTREE_IS_CORRUPT(pRtree);
188797 }
188798 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
188799 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
188800 if( !pNode ){
188801 rc = SQLITE_NOMEM;
188802 }else{
188803 pNode->pParent = pParent;
188804 pNode->zData = (u8 *)&pNode[1];
188805 pNode->nRef = 1;
188806 pRtree->nNodeRef++;
188807 pNode->iNode = iNode;
188808 pNode->isDirty = 0;
188809 pNode->pNext = 0;
188810 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
188811 pRtree->iNodeSize, 0);
188812 }
188813 }
188814
188815 /* If the root node was just loaded, set pRtree->iDepth to the height
188816 ** of the r-tree structure. A height of zero means all data is stored on
188817 ** the root node. A height of one means the children of the root node
188818 ** are the leaves, and so on. If the depth as specified on the root node
188819 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
188820 */
188821 if( pNode && iNode==1 ){
188822 pRtree->iDepth = readInt16(pNode->zData);
188823 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
188824 rc = SQLITE_CORRUPT_VTAB;
188825 RTREE_IS_CORRUPT(pRtree);
188826 }
188827 }
188828
188829 /* If no error has occurred so far, check if the "number of entries"
188830 ** field on the node is too large. If so, set the return code to
188831 ** SQLITE_CORRUPT_VTAB.
188832 */
188833 if( pNode && rc==SQLITE_OK ){
188834 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
188835 rc = SQLITE_CORRUPT_VTAB;
188836 RTREE_IS_CORRUPT(pRtree);
188837 }
188838 }
188839
188840 if( rc==SQLITE_OK ){
188841 if( pNode!=0 ){
188842 nodeReference(pParent);
188843 nodeHashInsert(pRtree, pNode);
188844 }else{
188845 rc = SQLITE_CORRUPT_VTAB;
188846 RTREE_IS_CORRUPT(pRtree);
188847 }
188848 *ppNode = pNode;
188849 }else{
188850 if( pNode ){
188851 pRtree->nNodeRef--;
188852 sqlite3_free(pNode);
188853 }
188854 *ppNode = 0;
188855 }
188856
188857 return rc;
188858}
188859
188860/*
188861** Overwrite cell iCell of node pNode with the contents of pCell.
188862*/
188863static void nodeOverwriteCell(
188864 Rtree *pRtree, /* The overall R-Tree */
188865 RtreeNode *pNode, /* The node into which the cell is to be written */
188866 RtreeCell *pCell, /* The cell to write */
188867 int iCell /* Index into pNode into which pCell is written */
188868){
188869 int ii;
188870 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
188871 p += writeInt64(p, pCell->iRowid);
188872 for(ii=0; ii<pRtree->nDim2; ii++){
188873 p += writeCoord(p, &pCell->aCoord[ii]);
188874 }
188875 pNode->isDirty = 1;
188876}
188877
188878/*
188879** Remove the cell with index iCell from node pNode.
188880*/
188881static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
188882 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
188883 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
188884 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
188885 memmove(pDst, pSrc, nByte);
188886 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
188887 pNode->isDirty = 1;
188888}
188889
188890/*
188891** Insert the contents of cell pCell into node pNode. If the insert
188892** is successful, return SQLITE_OK.
188893**
188894** If there is not enough free space in pNode, return SQLITE_FULL.
188895*/
188896static int nodeInsertCell(
188897 Rtree *pRtree, /* The overall R-Tree */
188898 RtreeNode *pNode, /* Write new cell into this node */
188899 RtreeCell *pCell /* The cell to be inserted */
188900){
188901 int nCell; /* Current number of cells in pNode */
188902 int nMaxCell; /* Maximum number of cells for pNode */
188903
188904 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
188905 nCell = NCELL(pNode);
188906
188907 assert( nCell<=nMaxCell );
188908 if( nCell<nMaxCell ){
188909 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
188910 writeInt16(&pNode->zData[2], nCell+1);
188911 pNode->isDirty = 1;
188912 }
188913
188914 return (nCell==nMaxCell);
188915}
188916
188917/*
188918** If the node is dirty, write it out to the database.
188919*/
188920static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
188921 int rc = SQLITE_OK;
188922 if( pNode->isDirty ){
188923 sqlite3_stmt *p = pRtree->pWriteNode;
188924 if( pNode->iNode ){
188925 sqlite3_bind_int64(p, 1, pNode->iNode);
188926 }else{
188927 sqlite3_bind_null(p, 1);
188928 }
188929 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
188930 sqlite3_step(p);
188931 pNode->isDirty = 0;
188932 rc = sqlite3_reset(p);
188933 sqlite3_bind_null(p, 2);
188934 if( pNode->iNode==0 && rc==SQLITE_OK ){
188935 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
188936 nodeHashInsert(pRtree, pNode);
188937 }
188938 }
188939 return rc;
188940}
188941
188942/*
188943** Release a reference to a node. If the node is dirty and the reference
188944** count drops to zero, the node data is written to the database.
188945*/
188946static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
188947 int rc = SQLITE_OK;
188948 if( pNode ){
188949 assert( pNode->nRef>0 );
188950 assert( pRtree->nNodeRef>0 );
188951 pNode->nRef--;
188952 if( pNode->nRef==0 ){
188953 pRtree->nNodeRef--;
188954 if( pNode->iNode==1 ){
188955 pRtree->iDepth = -1;
188956 }
188957 if( pNode->pParent ){
188958 rc = nodeRelease(pRtree, pNode->pParent);
188959 }
188960 if( rc==SQLITE_OK ){
188961 rc = nodeWrite(pRtree, pNode);
188962 }
188963 nodeHashDelete(pRtree, pNode);
188964 sqlite3_free(pNode);
188965 }
188966 }
188967 return rc;
188968}
188969
188970/*
188971** Return the 64-bit integer value associated with cell iCell of
188972** node pNode. If pNode is a leaf node, this is a rowid. If it is
188973** an internal node, then the 64-bit integer is a child page number.
188974*/
188975static i64 nodeGetRowid(
188976 Rtree *pRtree, /* The overall R-Tree */
188977 RtreeNode *pNode, /* The node from which to extract the ID */
188978 int iCell /* The cell index from which to extract the ID */
188979){
188980 assert( iCell<NCELL(pNode) );
188981 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
188982}
188983
188984/*
188985** Return coordinate iCoord from cell iCell in node pNode.
188986*/
188987static void nodeGetCoord(
188988 Rtree *pRtree, /* The overall R-Tree */
188989 RtreeNode *pNode, /* The node from which to extract a coordinate */
188990 int iCell, /* The index of the cell within the node */
188991 int iCoord, /* Which coordinate to extract */
188992 RtreeCoord *pCoord /* OUT: Space to write result to */
188993){
188994 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
188995}
188996
188997/*
188998** Deserialize cell iCell of node pNode. Populate the structure pointed
188999** to by pCell with the results.
189000*/
189001static void nodeGetCell(
189002 Rtree *pRtree, /* The overall R-Tree */
189003 RtreeNode *pNode, /* The node containing the cell to be read */
189004 int iCell, /* Index of the cell within the node */
189005 RtreeCell *pCell /* OUT: Write the cell contents here */
189006){
189007 u8 *pData;
189008 RtreeCoord *pCoord;
189009 int ii = 0;
189010 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
189011 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
189012 pCoord = pCell->aCoord;
189013 do{
189014 readCoord(pData, &pCoord[ii]);
189015 readCoord(pData+4, &pCoord[ii+1]);
189016 pData += 8;
189017 ii += 2;
189018 }while( ii<pRtree->nDim2 );
189019}
189020
189021
189022/* Forward declaration for the function that does the work of
189023** the virtual table module xCreate() and xConnect() methods.
189024*/
189025static int rtreeInit(
189026 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
189027);
189028
189029/*
189030** Rtree virtual table module xCreate method.
189031*/
189032static int rtreeCreate(
189033 sqlite3 *db,
189034 void *pAux,
189035 int argc, const char *const*argv,
189036 sqlite3_vtab **ppVtab,
189037 char **pzErr
189038){
189039 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
189040}
189041
189042/*
189043** Rtree virtual table module xConnect method.
189044*/
189045static int rtreeConnect(
189046 sqlite3 *db,
189047 void *pAux,
189048 int argc, const char *const*argv,
189049 sqlite3_vtab **ppVtab,
189050 char **pzErr
189051){
189052 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
189053}
189054
189055/*
189056** Increment the r-tree reference count.
189057*/
189058static void rtreeReference(Rtree *pRtree){
189059 pRtree->nBusy++;
189060}
189061
189062/*
189063** Decrement the r-tree reference count. When the reference count reaches
189064** zero the structure is deleted.
189065*/
189066static void rtreeRelease(Rtree *pRtree){
189067 pRtree->nBusy--;
189068 if( pRtree->nBusy==0 ){
189069 pRtree->inWrTrans = 0;
189070 assert( pRtree->nCursor==0 );
189071 nodeBlobReset(pRtree);
189072 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
189073 sqlite3_finalize(pRtree->pWriteNode);
189074 sqlite3_finalize(pRtree->pDeleteNode);
189075 sqlite3_finalize(pRtree->pReadRowid);
189076 sqlite3_finalize(pRtree->pWriteRowid);
189077 sqlite3_finalize(pRtree->pDeleteRowid);
189078 sqlite3_finalize(pRtree->pReadParent);
189079 sqlite3_finalize(pRtree->pWriteParent);
189080 sqlite3_finalize(pRtree->pDeleteParent);
189081 sqlite3_finalize(pRtree->pWriteAux);
189082 sqlite3_free(pRtree->zReadAuxSql);
189083 sqlite3_free(pRtree);
189084 }
189085}
189086
189087/*
189088** Rtree virtual table module xDisconnect method.
189089*/
189090static int rtreeDisconnect(sqlite3_vtab *pVtab){
189091 rtreeRelease((Rtree *)pVtab);
189092 return SQLITE_OK;
189093}
189094
189095/*
189096** Rtree virtual table module xDestroy method.
189097*/
189098static int rtreeDestroy(sqlite3_vtab *pVtab){
189099 Rtree *pRtree = (Rtree *)pVtab;
189100 int rc;
189101 char *zCreate = sqlite3_mprintf(
189102 "DROP TABLE '%q'.'%q_node';"
189103 "DROP TABLE '%q'.'%q_rowid';"
189104 "DROP TABLE '%q'.'%q_parent';",
189105 pRtree->zDb, pRtree->zName,
189106 pRtree->zDb, pRtree->zName,
189107 pRtree->zDb, pRtree->zName
189108 );
189109 if( !zCreate ){
189110 rc = SQLITE_NOMEM;
189111 }else{
189112 nodeBlobReset(pRtree);
189113 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
189114 sqlite3_free(zCreate);
189115 }
189116 if( rc==SQLITE_OK ){
189117 rtreeRelease(pRtree);
189118 }
189119
189120 return rc;
189121}
189122
189123/*
189124** Rtree virtual table module xOpen method.
189125*/
189126static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
189127 int rc = SQLITE_NOMEM;
189128 Rtree *pRtree = (Rtree *)pVTab;
189129 RtreeCursor *pCsr;
189130
189131 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
189132 if( pCsr ){
189133 memset(pCsr, 0, sizeof(RtreeCursor));
189134 pCsr->base.pVtab = pVTab;
189135 rc = SQLITE_OK;
189136 pRtree->nCursor++;
189137 }
189138 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
189139
189140 return rc;
189141}
189142
189143
189144/*
189145** Reset a cursor back to its initial state.
189146*/
189147static void resetCursor(RtreeCursor *pCsr){
189148 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
189149 int ii;
189150 sqlite3_stmt *pStmt;
189151 if( pCsr->aConstraint ){
189152 int i; /* Used to iterate through constraint array */
189153 for(i=0; i<pCsr->nConstraint; i++){
189154 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
189155 if( pInfo ){
189156 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
189157 sqlite3_free(pInfo);
189158 }
189159 }
189160 sqlite3_free(pCsr->aConstraint);
189161 pCsr->aConstraint = 0;
189162 }
189163 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
189164 sqlite3_free(pCsr->aPoint);
189165 pStmt = pCsr->pReadAux;
189166 memset(pCsr, 0, sizeof(RtreeCursor));
189167 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
189168 pCsr->pReadAux = pStmt;
189169
189170}
189171
189172/*
189173** Rtree virtual table module xClose method.
189174*/
189175static int rtreeClose(sqlite3_vtab_cursor *cur){
189176 Rtree *pRtree = (Rtree *)(cur->pVtab);
189177 RtreeCursor *pCsr = (RtreeCursor *)cur;
189178 assert( pRtree->nCursor>0 );
189179 resetCursor(pCsr);
189180 sqlite3_finalize(pCsr->pReadAux);
189181 sqlite3_free(pCsr);
189182 pRtree->nCursor--;
189183 nodeBlobReset(pRtree);
189184 return SQLITE_OK;
189185}
189186
189187/*
189188** Rtree virtual table module xEof method.
189189**
189190** Return non-zero if the cursor does not currently point to a valid
189191** record (i.e if the scan has finished), or zero otherwise.
189192*/
189193static int rtreeEof(sqlite3_vtab_cursor *cur){
189194 RtreeCursor *pCsr = (RtreeCursor *)cur;
189195 return pCsr->atEOF;
189196}
189197
189198/*
189199** Convert raw bits from the on-disk RTree record into a coordinate value.
189200** The on-disk format is big-endian and needs to be converted for little-
189201** endian platforms. The on-disk record stores integer coordinates if
189202** eInt is true and it stores 32-bit floating point records if eInt is
189203** false. a[] is the four bytes of the on-disk record to be decoded.
189204** Store the results in "r".
189205**
189206** There are five versions of this macro. The last one is generic. The
189207** other four are various architectures-specific optimizations.
189208*/
189209#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
189210#define RTREE_DECODE_COORD(eInt, a, r) { \
189211 RtreeCoord c; /* Coordinate decoded */ \
189212 c.u = _byteswap_ulong(*(u32*)a); \
189213 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
189214}
189215#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
189216#define RTREE_DECODE_COORD(eInt, a, r) { \
189217 RtreeCoord c; /* Coordinate decoded */ \
189218 c.u = __builtin_bswap32(*(u32*)a); \
189219 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
189220}
189221#elif SQLITE_BYTEORDER==1234
189222#define RTREE_DECODE_COORD(eInt, a, r) { \
189223 RtreeCoord c; /* Coordinate decoded */ \
189224 memcpy(&c.u,a,4); \
189225 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
189226 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
189227 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
189228}
189229#elif SQLITE_BYTEORDER==4321
189230#define RTREE_DECODE_COORD(eInt, a, r) { \
189231 RtreeCoord c; /* Coordinate decoded */ \
189232 memcpy(&c.u,a,4); \
189233 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
189234}
189235#else
189236#define RTREE_DECODE_COORD(eInt, a, r) { \
189237 RtreeCoord c; /* Coordinate decoded */ \
189238 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
189239 +((u32)a[2]<<8) + a[3]; \
189240 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
189241}
189242#endif
189243
189244/*
189245** Check the RTree node or entry given by pCellData and p against the MATCH
189246** constraint pConstraint.
189247*/
189248static int rtreeCallbackConstraint(
189249 RtreeConstraint *pConstraint, /* The constraint to test */
189250 int eInt, /* True if RTree holding integer coordinates */
189251 u8 *pCellData, /* Raw cell content */
189252 RtreeSearchPoint *pSearch, /* Container of this cell */
189253 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
189254 int *peWithin /* OUT: visibility of the cell */
189255){
189256 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
189257 int nCoord = pInfo->nCoord; /* No. of coordinates */
189258 int rc; /* Callback return code */
189259 RtreeCoord c; /* Translator union */
189260 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
189261
189262 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
189263 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
189264
189265 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
189266 pInfo->iRowid = readInt64(pCellData);
189267 }
189268 pCellData += 8;
189269#ifndef SQLITE_RTREE_INT_ONLY
189270 if( eInt==0 ){
189271 switch( nCoord ){
189272 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
189273 readCoord(pCellData+32, &c); aCoord[8] = c.f;
189274 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
189275 readCoord(pCellData+24, &c); aCoord[6] = c.f;
189276 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
189277 readCoord(pCellData+16, &c); aCoord[4] = c.f;
189278 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
189279 readCoord(pCellData+8, &c); aCoord[2] = c.f;
189280 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
189281 readCoord(pCellData, &c); aCoord[0] = c.f;
189282 }
189283 }else
189284#endif
189285 {
189286 switch( nCoord ){
189287 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
189288 readCoord(pCellData+32, &c); aCoord[8] = c.i;
189289 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
189290 readCoord(pCellData+24, &c); aCoord[6] = c.i;
189291 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
189292 readCoord(pCellData+16, &c); aCoord[4] = c.i;
189293 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
189294 readCoord(pCellData+8, &c); aCoord[2] = c.i;
189295 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
189296 readCoord(pCellData, &c); aCoord[0] = c.i;
189297 }
189298 }
189299 if( pConstraint->op==RTREE_MATCH ){
189300 int eWithin = 0;
189301 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
189302 nCoord, aCoord, &eWithin);
189303 if( eWithin==0 ) *peWithin = NOT_WITHIN;
189304 *prScore = RTREE_ZERO;
189305 }else{
189306 pInfo->aCoord = aCoord;
189307 pInfo->iLevel = pSearch->iLevel - 1;
189308 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
189309 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
189310 rc = pConstraint->u.xQueryFunc(pInfo);
189311 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
189312 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
189313 *prScore = pInfo->rScore;
189314 }
189315 }
189316 return rc;
189317}
189318
189319/*
189320** Check the internal RTree node given by pCellData against constraint p.
189321** If this constraint cannot be satisfied by any child within the node,
189322** set *peWithin to NOT_WITHIN.
189323*/
189324static void rtreeNonleafConstraint(
189325 RtreeConstraint *p, /* The constraint to test */
189326 int eInt, /* True if RTree holds integer coordinates */
189327 u8 *pCellData, /* Raw cell content as appears on disk */
189328 int *peWithin /* Adjust downward, as appropriate */
189329){
189330 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
189331
189332 /* p->iCoord might point to either a lower or upper bound coordinate
189333 ** in a coordinate pair. But make pCellData point to the lower bound.
189334 */
189335 pCellData += 8 + 4*(p->iCoord&0xfe);
189336
189337 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
189338 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
189339 || p->op==RTREE_FALSE );
189340 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
189341 switch( p->op ){
189342 case RTREE_TRUE: return; /* Always satisfied */
189343 case RTREE_FALSE: break; /* Never satisfied */
189344 case RTREE_LE:
189345 case RTREE_LT:
189346 case RTREE_EQ:
189347 RTREE_DECODE_COORD(eInt, pCellData, val);
189348 /* val now holds the lower bound of the coordinate pair */
189349 if( p->u.rValue>=val ) return;
189350 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
189351 /* Fall through for the RTREE_EQ case */
189352
189353 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
189354 pCellData += 4;
189355 RTREE_DECODE_COORD(eInt, pCellData, val);
189356 /* val now holds the upper bound of the coordinate pair */
189357 if( p->u.rValue<=val ) return;
189358 }
189359 *peWithin = NOT_WITHIN;
189360}
189361
189362/*
189363** Check the leaf RTree cell given by pCellData against constraint p.
189364** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
189365** If the constraint is satisfied, leave *peWithin unchanged.
189366**
189367** The constraint is of the form: xN op $val
189368**
189369** The op is given by p->op. The xN is p->iCoord-th coordinate in
189370** pCellData. $val is given by p->u.rValue.
189371*/
189372static void rtreeLeafConstraint(
189373 RtreeConstraint *p, /* The constraint to test */
189374 int eInt, /* True if RTree holds integer coordinates */
189375 u8 *pCellData, /* Raw cell content as appears on disk */
189376 int *peWithin /* Adjust downward, as appropriate */
189377){
189378 RtreeDValue xN; /* Coordinate value converted to a double */
189379
189380 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
189381 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
189382 || p->op==RTREE_FALSE );
189383 pCellData += 8 + p->iCoord*4;
189384 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
189385 RTREE_DECODE_COORD(eInt, pCellData, xN);
189386 switch( p->op ){
189387 case RTREE_TRUE: return; /* Always satisfied */
189388 case RTREE_FALSE: break; /* Never satisfied */
189389 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
189390 case RTREE_LT: if( xN < p->u.rValue ) return; break;
189391 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
189392 case RTREE_GT: if( xN > p->u.rValue ) return; break;
189393 default: if( xN == p->u.rValue ) return; break;
189394 }
189395 *peWithin = NOT_WITHIN;
189396}
189397
189398/*
189399** One of the cells in node pNode is guaranteed to have a 64-bit
189400** integer value equal to iRowid. Return the index of this cell.
189401*/
189402static int nodeRowidIndex(
189403 Rtree *pRtree,
189404 RtreeNode *pNode,
189405 i64 iRowid,
189406 int *piIndex
189407){
189408 int ii;
189409 int nCell = NCELL(pNode);
189410 assert( nCell<200 );
189411 for(ii=0; ii<nCell; ii++){
189412 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
189413 *piIndex = ii;
189414 return SQLITE_OK;
189415 }
189416 }
189417 RTREE_IS_CORRUPT(pRtree);
189418 return SQLITE_CORRUPT_VTAB;
189419}
189420
189421/*
189422** Return the index of the cell containing a pointer to node pNode
189423** in its parent. If pNode is the root node, return -1.
189424*/
189425static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
189426 RtreeNode *pParent = pNode->pParent;
189427 if( pParent ){
189428 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
189429 }
189430 *piIndex = -1;
189431 return SQLITE_OK;
189432}
189433
189434/*
189435** Compare two search points. Return negative, zero, or positive if the first
189436** is less than, equal to, or greater than the second.
189437**
189438** The rScore is the primary key. Smaller rScore values come first.
189439** If the rScore is a tie, then use iLevel as the tie breaker with smaller
189440** iLevel values coming first. In this way, if rScore is the same for all
189441** SearchPoints, then iLevel becomes the deciding factor and the result
189442** is a depth-first search, which is the desired default behavior.
189443*/
189444static int rtreeSearchPointCompare(
189445 const RtreeSearchPoint *pA,
189446 const RtreeSearchPoint *pB
189447){
189448 if( pA->rScore<pB->rScore ) return -1;
189449 if( pA->rScore>pB->rScore ) return +1;
189450 if( pA->iLevel<pB->iLevel ) return -1;
189451 if( pA->iLevel>pB->iLevel ) return +1;
189452 return 0;
189453}
189454
189455/*
189456** Interchange two search points in a cursor.
189457*/
189458static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
189459 RtreeSearchPoint t = p->aPoint[i];
189460 assert( i<j );
189461 p->aPoint[i] = p->aPoint[j];
189462 p->aPoint[j] = t;
189463 i++; j++;
189464 if( i<RTREE_CACHE_SZ ){
189465 if( j>=RTREE_CACHE_SZ ){
189466 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
189467 p->aNode[i] = 0;
189468 }else{
189469 RtreeNode *pTemp = p->aNode[i];
189470 p->aNode[i] = p->aNode[j];
189471 p->aNode[j] = pTemp;
189472 }
189473 }
189474}
189475
189476/*
189477** Return the search point with the lowest current score.
189478*/
189479static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
189480 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
189481}
189482
189483/*
189484** Get the RtreeNode for the search point with the lowest score.
189485*/
189486static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
189487 sqlite3_int64 id;
189488 int ii = 1 - pCur->bPoint;
189489 assert( ii==0 || ii==1 );
189490 assert( pCur->bPoint || pCur->nPoint );
189491 if( pCur->aNode[ii]==0 ){
189492 assert( pRC!=0 );
189493 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
189494 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
189495 }
189496 return pCur->aNode[ii];
189497}
189498
189499/*
189500** Push a new element onto the priority queue
189501*/
189502static RtreeSearchPoint *rtreeEnqueue(
189503 RtreeCursor *pCur, /* The cursor */
189504 RtreeDValue rScore, /* Score for the new search point */
189505 u8 iLevel /* Level for the new search point */
189506){
189507 int i, j;
189508 RtreeSearchPoint *pNew;
189509 if( pCur->nPoint>=pCur->nPointAlloc ){
189510 int nNew = pCur->nPointAlloc*2 + 8;
189511 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
189512 if( pNew==0 ) return 0;
189513 pCur->aPoint = pNew;
189514 pCur->nPointAlloc = nNew;
189515 }
189516 i = pCur->nPoint++;
189517 pNew = pCur->aPoint + i;
189518 pNew->rScore = rScore;
189519 pNew->iLevel = iLevel;
189520 assert( iLevel<=RTREE_MAX_DEPTH );
189521 while( i>0 ){
189522 RtreeSearchPoint *pParent;
189523 j = (i-1)/2;
189524 pParent = pCur->aPoint + j;
189525 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
189526 rtreeSearchPointSwap(pCur, j, i);
189527 i = j;
189528 pNew = pParent;
189529 }
189530 return pNew;
189531}
189532
189533/*
189534** Allocate a new RtreeSearchPoint and return a pointer to it. Return
189535** NULL if malloc fails.
189536*/
189537static RtreeSearchPoint *rtreeSearchPointNew(
189538 RtreeCursor *pCur, /* The cursor */
189539 RtreeDValue rScore, /* Score for the new search point */
189540 u8 iLevel /* Level for the new search point */
189541){
189542 RtreeSearchPoint *pNew, *pFirst;
189543 pFirst = rtreeSearchPointFirst(pCur);
189544 pCur->anQueue[iLevel]++;
189545 if( pFirst==0
189546 || pFirst->rScore>rScore
189547 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
189548 ){
189549 if( pCur->bPoint ){
189550 int ii;
189551 pNew = rtreeEnqueue(pCur, rScore, iLevel);
189552 if( pNew==0 ) return 0;
189553 ii = (int)(pNew - pCur->aPoint) + 1;
189554 if( ii<RTREE_CACHE_SZ ){
189555 assert( pCur->aNode[ii]==0 );
189556 pCur->aNode[ii] = pCur->aNode[0];
189557 }else{
189558 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
189559 }
189560 pCur->aNode[0] = 0;
189561 *pNew = pCur->sPoint;
189562 }
189563 pCur->sPoint.rScore = rScore;
189564 pCur->sPoint.iLevel = iLevel;
189565 pCur->bPoint = 1;
189566 return &pCur->sPoint;
189567 }else{
189568 return rtreeEnqueue(pCur, rScore, iLevel);
189569 }
189570}
189571
189572#if 0
189573/* Tracing routines for the RtreeSearchPoint queue */
189574static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
189575 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
189576 printf(" %d.%05lld.%02d %g %d",
189577 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
189578 );
189579 idx++;
189580 if( idx<RTREE_CACHE_SZ ){
189581 printf(" %p\n", pCur->aNode[idx]);
189582 }else{
189583 printf("\n");
189584 }
189585}
189586static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
189587 int ii;
189588 printf("=== %9s ", zPrefix);
189589 if( pCur->bPoint ){
189590 tracePoint(&pCur->sPoint, -1, pCur);
189591 }
189592 for(ii=0; ii<pCur->nPoint; ii++){
189593 if( ii>0 || pCur->bPoint ) printf(" ");
189594 tracePoint(&pCur->aPoint[ii], ii, pCur);
189595 }
189596}
189597# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
189598#else
189599# define RTREE_QUEUE_TRACE(A,B) /* no-op */
189600#endif
189601
189602/* Remove the search point with the lowest current score.
189603*/
189604static void rtreeSearchPointPop(RtreeCursor *p){
189605 int i, j, k, n;
189606 i = 1 - p->bPoint;
189607 assert( i==0 || i==1 );
189608 if( p->aNode[i] ){
189609 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
189610 p->aNode[i] = 0;
189611 }
189612 if( p->bPoint ){
189613 p->anQueue[p->sPoint.iLevel]--;
189614 p->bPoint = 0;
189615 }else if( p->nPoint ){
189616 p->anQueue[p->aPoint[0].iLevel]--;
189617 n = --p->nPoint;
189618 p->aPoint[0] = p->aPoint[n];
189619 if( n<RTREE_CACHE_SZ-1 ){
189620 p->aNode[1] = p->aNode[n+1];
189621 p->aNode[n+1] = 0;
189622 }
189623 i = 0;
189624 while( (j = i*2+1)<n ){
189625 k = j+1;
189626 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
189627 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
189628 rtreeSearchPointSwap(p, i, k);
189629 i = k;
189630 }else{
189631 break;
189632 }
189633 }else{
189634 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
189635 rtreeSearchPointSwap(p, i, j);
189636 i = j;
189637 }else{
189638 break;
189639 }
189640 }
189641 }
189642 }
189643}
189644
189645
189646/*
189647** Continue the search on cursor pCur until the front of the queue
189648** contains an entry suitable for returning as a result-set row,
189649** or until the RtreeSearchPoint queue is empty, indicating that the
189650** query has completed.
189651*/
189652static int rtreeStepToLeaf(RtreeCursor *pCur){
189653 RtreeSearchPoint *p;
189654 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
189655 RtreeNode *pNode;
189656 int eWithin;
189657 int rc = SQLITE_OK;
189658 int nCell;
189659 int nConstraint = pCur->nConstraint;
189660 int ii;
189661 int eInt;
189662 RtreeSearchPoint x;
189663
189664 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
189665 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
189666 u8 *pCellData;
189667 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
189668 if( rc ) return rc;
189669 nCell = NCELL(pNode);
189670 assert( nCell<200 );
189671 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
189672 while( p->iCell<nCell ){
189673 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
189674 eWithin = FULLY_WITHIN;
189675 for(ii=0; ii<nConstraint; ii++){
189676 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
189677 if( pConstraint->op>=RTREE_MATCH ){
189678 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
189679 &rScore, &eWithin);
189680 if( rc ) return rc;
189681 }else if( p->iLevel==1 ){
189682 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
189683 }else{
189684 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
189685 }
189686 if( eWithin==NOT_WITHIN ){
189687 p->iCell++;
189688 pCellData += pRtree->nBytesPerCell;
189689 break;
189690 }
189691 }
189692 if( eWithin==NOT_WITHIN ) continue;
189693 p->iCell++;
189694 x.iLevel = p->iLevel - 1;
189695 if( x.iLevel ){
189696 x.id = readInt64(pCellData);
189697 for(ii=0; ii<pCur->nPoint; ii++){
189698 if( pCur->aPoint[ii].id==x.id ){
189699 RTREE_IS_CORRUPT(pRtree);
189700 return SQLITE_CORRUPT_VTAB;
189701 }
189702 }
189703 x.iCell = 0;
189704 }else{
189705 x.id = p->id;
189706 x.iCell = p->iCell - 1;
189707 }
189708 if( p->iCell>=nCell ){
189709 RTREE_QUEUE_TRACE(pCur, "POP-S:");
189710 rtreeSearchPointPop(pCur);
189711 }
189712 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
189713 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
189714 if( p==0 ) return SQLITE_NOMEM;
189715 p->eWithin = (u8)eWithin;
189716 p->id = x.id;
189717 p->iCell = x.iCell;
189718 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
189719 break;
189720 }
189721 if( p->iCell>=nCell ){
189722 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
189723 rtreeSearchPointPop(pCur);
189724 }
189725 }
189726 pCur->atEOF = p==0;
189727 return SQLITE_OK;
189728}
189729
189730/*
189731** Rtree virtual table module xNext method.
189732*/
189733static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
189734 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189735 int rc = SQLITE_OK;
189736
189737 /* Move to the next entry that matches the configured constraints. */
189738 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
189739 if( pCsr->bAuxValid ){
189740 pCsr->bAuxValid = 0;
189741 sqlite3_reset(pCsr->pReadAux);
189742 }
189743 rtreeSearchPointPop(pCsr);
189744 rc = rtreeStepToLeaf(pCsr);
189745 return rc;
189746}
189747
189748/*
189749** Rtree virtual table module xRowid method.
189750*/
189751static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
189752 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189753 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
189754 int rc = SQLITE_OK;
189755 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
189756 if( rc==SQLITE_OK && p ){
189757 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
189758 }
189759 return rc;
189760}
189761
189762/*
189763** Rtree virtual table module xColumn method.
189764*/
189765static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
189766 Rtree *pRtree = (Rtree *)cur->pVtab;
189767 RtreeCursor *pCsr = (RtreeCursor *)cur;
189768 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
189769 RtreeCoord c;
189770 int rc = SQLITE_OK;
189771 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
189772
189773 if( rc ) return rc;
189774 if( p==0 ) return SQLITE_OK;
189775 if( i==0 ){
189776 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
189777 }else if( i<=pRtree->nDim2 ){
189778 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
189779#ifndef SQLITE_RTREE_INT_ONLY
189780 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
189781 sqlite3_result_double(ctx, c.f);
189782 }else
189783#endif
189784 {
189785 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
189786 sqlite3_result_int(ctx, c.i);
189787 }
189788 }else{
189789 if( !pCsr->bAuxValid ){
189790 if( pCsr->pReadAux==0 ){
189791 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
189792 &pCsr->pReadAux, 0);
189793 if( rc ) return rc;
189794 }
189795 sqlite3_bind_int64(pCsr->pReadAux, 1,
189796 nodeGetRowid(pRtree, pNode, p->iCell));
189797 rc = sqlite3_step(pCsr->pReadAux);
189798 if( rc==SQLITE_ROW ){
189799 pCsr->bAuxValid = 1;
189800 }else{
189801 sqlite3_reset(pCsr->pReadAux);
189802 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
189803 return rc;
189804 }
189805 }
189806 sqlite3_result_value(ctx,
189807 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
189808 }
189809 return SQLITE_OK;
189810}
189811
189812/*
189813** Use nodeAcquire() to obtain the leaf node containing the record with
189814** rowid iRowid. If successful, set *ppLeaf to point to the node and
189815** return SQLITE_OK. If there is no such record in the table, set
189816** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
189817** to zero and return an SQLite error code.
189818*/
189819static int findLeafNode(
189820 Rtree *pRtree, /* RTree to search */
189821 i64 iRowid, /* The rowid searching for */
189822 RtreeNode **ppLeaf, /* Write the node here */
189823 sqlite3_int64 *piNode /* Write the node-id here */
189824){
189825 int rc;
189826 *ppLeaf = 0;
189827 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
189828 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
189829 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
189830 if( piNode ) *piNode = iNode;
189831 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
189832 sqlite3_reset(pRtree->pReadRowid);
189833 }else{
189834 rc = sqlite3_reset(pRtree->pReadRowid);
189835 }
189836 return rc;
189837}
189838
189839/*
189840** This function is called to configure the RtreeConstraint object passed
189841** as the second argument for a MATCH constraint. The value passed as the
189842** first argument to this function is the right-hand operand to the MATCH
189843** operator.
189844*/
189845static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
189846 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
189847 sqlite3_rtree_query_info *pInfo; /* Callback information */
189848
189849 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
189850 if( pSrc==0 ) return SQLITE_ERROR;
189851 pInfo = (sqlite3_rtree_query_info*)
189852 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
189853 if( !pInfo ) return SQLITE_NOMEM;
189854 memset(pInfo, 0, sizeof(*pInfo));
189855 pBlob = (RtreeMatchArg*)&pInfo[1];
189856 memcpy(pBlob, pSrc, pSrc->iSize);
189857 pInfo->pContext = pBlob->cb.pContext;
189858 pInfo->nParam = pBlob->nParam;
189859 pInfo->aParam = pBlob->aParam;
189860 pInfo->apSqlParam = pBlob->apSqlParam;
189861
189862 if( pBlob->cb.xGeom ){
189863 pCons->u.xGeom = pBlob->cb.xGeom;
189864 }else{
189865 pCons->op = RTREE_QUERY;
189866 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
189867 }
189868 pCons->pInfo = pInfo;
189869 return SQLITE_OK;
189870}
189871
189872/*
189873** Rtree virtual table module xFilter method.
189874*/
189875static int rtreeFilter(
189876 sqlite3_vtab_cursor *pVtabCursor,
189877 int idxNum, const char *idxStr,
189878 int argc, sqlite3_value **argv
189879){
189880 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
189881 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
189882 RtreeNode *pRoot = 0;
189883 int ii;
189884 int rc = SQLITE_OK;
189885 int iCell = 0;
189886
189887 rtreeReference(pRtree);
189888
189889 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
189890 resetCursor(pCsr);
189891
189892 pCsr->iStrategy = idxNum;
189893 if( idxNum==1 ){
189894 /* Special case - lookup by rowid. */
189895 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
189896 RtreeSearchPoint *p; /* Search point for the leaf */
189897 i64 iRowid = sqlite3_value_int64(argv[0]);
189898 i64 iNode = 0;
189899 int eType = sqlite3_value_numeric_type(argv[0]);
189900 if( eType==SQLITE_INTEGER
189901 || (eType==SQLITE_FLOAT && sqlite3_value_double(argv[0])==iRowid)
189902 ){
189903 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
189904 }else{
189905 rc = SQLITE_OK;
189906 pLeaf = 0;
189907 }
189908 if( rc==SQLITE_OK && pLeaf!=0 ){
189909 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
189910 assert( p!=0 ); /* Always returns pCsr->sPoint */
189911 pCsr->aNode[0] = pLeaf;
189912 p->id = iNode;
189913 p->eWithin = PARTLY_WITHIN;
189914 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
189915 p->iCell = (u8)iCell;
189916 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
189917 }else{
189918 pCsr->atEOF = 1;
189919 }
189920 }else{
189921 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
189922 ** with the configured constraints.
189923 */
189924 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
189925 if( rc==SQLITE_OK && argc>0 ){
189926 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
189927 pCsr->nConstraint = argc;
189928 if( !pCsr->aConstraint ){
189929 rc = SQLITE_NOMEM;
189930 }else{
189931 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
189932 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
189933 assert( (idxStr==0 && argc==0)
189934 || (idxStr && (int)strlen(idxStr)==argc*2) );
189935 for(ii=0; ii<argc; ii++){
189936 RtreeConstraint *p = &pCsr->aConstraint[ii];
189937 int eType = sqlite3_value_numeric_type(argv[ii]);
189938 p->op = idxStr[ii*2];
189939 p->iCoord = idxStr[ii*2+1]-'0';
189940 if( p->op>=RTREE_MATCH ){
189941 /* A MATCH operator. The right-hand-side must be a blob that
189942 ** can be cast into an RtreeMatchArg object. One created using
189943 ** an sqlite3_rtree_geometry_callback() SQL user function.
189944 */
189945 rc = deserializeGeometry(argv[ii], p);
189946 if( rc!=SQLITE_OK ){
189947 break;
189948 }
189949 p->pInfo->nCoord = pRtree->nDim2;
189950 p->pInfo->anQueue = pCsr->anQueue;
189951 p->pInfo->mxLevel = pRtree->iDepth + 1;
189952 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
189953#ifdef SQLITE_RTREE_INT_ONLY
189954 p->u.rValue = sqlite3_value_int64(argv[ii]);
189955#else
189956 p->u.rValue = sqlite3_value_double(argv[ii]);
189957#endif
189958 }else{
189959 p->u.rValue = RTREE_ZERO;
189960 if( eType==SQLITE_NULL ){
189961 p->op = RTREE_FALSE;
189962 }else if( p->op==RTREE_LT || p->op==RTREE_LE ){
189963 p->op = RTREE_TRUE;
189964 }else{
189965 p->op = RTREE_FALSE;
189966 }
189967 }
189968 }
189969 }
189970 }
189971 if( rc==SQLITE_OK ){
189972 RtreeSearchPoint *pNew;
189973 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
189974 if( pNew==0 ) return SQLITE_NOMEM;
189975 pNew->id = 1;
189976 pNew->iCell = 0;
189977 pNew->eWithin = PARTLY_WITHIN;
189978 assert( pCsr->bPoint==1 );
189979 pCsr->aNode[0] = pRoot;
189980 pRoot = 0;
189981 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
189982 rc = rtreeStepToLeaf(pCsr);
189983 }
189984 }
189985
189986 nodeRelease(pRtree, pRoot);
189987 rtreeRelease(pRtree);
189988 return rc;
189989}
189990
189991/*
189992** Rtree virtual table module xBestIndex method. There are three
189993** table scan strategies to choose from (in order from most to
189994** least desirable):
189995**
189996** idxNum idxStr Strategy
189997** ------------------------------------------------
189998** 1 Unused Direct lookup by rowid.
189999** 2 See below R-tree query or full-table scan.
190000** ------------------------------------------------
190001**
190002** If strategy 1 is used, then idxStr is not meaningful. If strategy
190003** 2 is used, idxStr is formatted to contain 2 bytes for each
190004** constraint used. The first two bytes of idxStr correspond to
190005** the constraint in sqlite3_index_info.aConstraintUsage[] with
190006** (argvIndex==1) etc.
190007**
190008** The first of each pair of bytes in idxStr identifies the constraint
190009** operator as follows:
190010**
190011** Operator Byte Value
190012** ----------------------
190013** = 0x41 ('A')
190014** <= 0x42 ('B')
190015** < 0x43 ('C')
190016** >= 0x44 ('D')
190017** > 0x45 ('E')
190018** MATCH 0x46 ('F')
190019** ----------------------
190020**
190021** The second of each pair of bytes identifies the coordinate column
190022** to which the constraint applies. The leftmost coordinate column
190023** is 'a', the second from the left 'b' etc.
190024*/
190025static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
190026 Rtree *pRtree = (Rtree*)tab;
190027 int rc = SQLITE_OK;
190028 int ii;
190029 int bMatch = 0; /* True if there exists a MATCH constraint */
190030 i64 nRow; /* Estimated rows returned by this scan */
190031
190032 int iIdx = 0;
190033 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
190034 memset(zIdxStr, 0, sizeof(zIdxStr));
190035
190036 /* Check if there exists a MATCH constraint - even an unusable one. If there
190037 ** is, do not consider the lookup-by-rowid plan as using such a plan would
190038 ** require the VDBE to evaluate the MATCH constraint, which is not currently
190039 ** possible. */
190040 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
190041 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
190042 bMatch = 1;
190043 }
190044 }
190045
190046 assert( pIdxInfo->idxStr==0 );
190047 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
190048 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
190049
190050 if( bMatch==0 && p->usable
190051 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
190052 ){
190053 /* We have an equality constraint on the rowid. Use strategy 1. */
190054 int jj;
190055 for(jj=0; jj<ii; jj++){
190056 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
190057 pIdxInfo->aConstraintUsage[jj].omit = 0;
190058 }
190059 pIdxInfo->idxNum = 1;
190060 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
190061 pIdxInfo->aConstraintUsage[jj].omit = 1;
190062
190063 /* This strategy involves a two rowid lookups on an B-Tree structures
190064 ** and then a linear search of an R-Tree node. This should be
190065 ** considered almost as quick as a direct rowid lookup (for which
190066 ** sqlite uses an internal cost of 0.0). It is expected to return
190067 ** a single row.
190068 */
190069 pIdxInfo->estimatedCost = 30.0;
190070 pIdxInfo->estimatedRows = 1;
190071 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
190072 return SQLITE_OK;
190073 }
190074
190075 if( p->usable
190076 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
190077 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
190078 ){
190079 u8 op;
190080 switch( p->op ){
190081 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
190082 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
190083 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
190084 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
190085 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
190086 case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
190087 default: op = 0; break;
190088 }
190089 if( op ){
190090 zIdxStr[iIdx++] = op;
190091 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
190092 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
190093 pIdxInfo->aConstraintUsage[ii].omit = 1;
190094 }
190095 }
190096 }
190097
190098 pIdxInfo->idxNum = 2;
190099 pIdxInfo->needToFreeIdxStr = 1;
190100 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
190101 return SQLITE_NOMEM;
190102 }
190103
190104 nRow = pRtree->nRowEst >> (iIdx/2);
190105 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
190106 pIdxInfo->estimatedRows = nRow;
190107
190108 return rc;
190109}
190110
190111/*
190112** Return the N-dimensional volumn of the cell stored in *p.
190113*/
190114static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
190115 RtreeDValue area = (RtreeDValue)1;
190116 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
190117#ifndef SQLITE_RTREE_INT_ONLY
190118 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
190119 switch( pRtree->nDim ){
190120 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
190121 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
190122 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
190123 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
190124 default: area *= p->aCoord[1].f - p->aCoord[0].f;
190125 }
190126 }else
190127#endif
190128 {
190129 switch( pRtree->nDim ){
190130 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
190131 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
190132 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
190133 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
190134 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
190135 }
190136 }
190137 return area;
190138}
190139
190140/*
190141** Return the margin length of cell p. The margin length is the sum
190142** of the objects size in each dimension.
190143*/
190144static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
190145 RtreeDValue margin = 0;
190146 int ii = pRtree->nDim2 - 2;
190147 do{
190148 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
190149 ii -= 2;
190150 }while( ii>=0 );
190151 return margin;
190152}
190153
190154/*
190155** Store the union of cells p1 and p2 in p1.
190156*/
190157static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
190158 int ii = 0;
190159 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
190160 do{
190161 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
190162 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
190163 ii += 2;
190164 }while( ii<pRtree->nDim2 );
190165 }else{
190166 do{
190167 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
190168 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
190169 ii += 2;
190170 }while( ii<pRtree->nDim2 );
190171 }
190172}
190173
190174/*
190175** Return true if the area covered by p2 is a subset of the area covered
190176** by p1. False otherwise.
190177*/
190178static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
190179 int ii;
190180 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
190181 for(ii=0; ii<pRtree->nDim2; ii+=2){
190182 RtreeCoord *a1 = &p1->aCoord[ii];
190183 RtreeCoord *a2 = &p2->aCoord[ii];
190184 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
190185 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
190186 ){
190187 return 0;
190188 }
190189 }
190190 return 1;
190191}
190192
190193/*
190194** Return the amount cell p would grow by if it were unioned with pCell.
190195*/
190196static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
190197 RtreeDValue area;
190198 RtreeCell cell;
190199 memcpy(&cell, p, sizeof(RtreeCell));
190200 area = cellArea(pRtree, &cell);
190201 cellUnion(pRtree, &cell, pCell);
190202 return (cellArea(pRtree, &cell)-area);
190203}
190204
190205static RtreeDValue cellOverlap(
190206 Rtree *pRtree,
190207 RtreeCell *p,
190208 RtreeCell *aCell,
190209 int nCell
190210){
190211 int ii;
190212 RtreeDValue overlap = RTREE_ZERO;
190213 for(ii=0; ii<nCell; ii++){
190214 int jj;
190215 RtreeDValue o = (RtreeDValue)1;
190216 for(jj=0; jj<pRtree->nDim2; jj+=2){
190217 RtreeDValue x1, x2;
190218 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
190219 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
190220 if( x2<x1 ){
190221 o = (RtreeDValue)0;
190222 break;
190223 }else{
190224 o = o * (x2-x1);
190225 }
190226 }
190227 overlap += o;
190228 }
190229 return overlap;
190230}
190231
190232
190233/*
190234** This function implements the ChooseLeaf algorithm from Gutman[84].
190235** ChooseSubTree in r*tree terminology.
190236*/
190237static int ChooseLeaf(
190238 Rtree *pRtree, /* Rtree table */
190239 RtreeCell *pCell, /* Cell to insert into rtree */
190240 int iHeight, /* Height of sub-tree rooted at pCell */
190241 RtreeNode **ppLeaf /* OUT: Selected leaf page */
190242){
190243 int rc;
190244 int ii;
190245 RtreeNode *pNode = 0;
190246 rc = nodeAcquire(pRtree, 1, 0, &pNode);
190247
190248 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
190249 int iCell;
190250 sqlite3_int64 iBest = 0;
190251
190252 RtreeDValue fMinGrowth = RTREE_ZERO;
190253 RtreeDValue fMinArea = RTREE_ZERO;
190254
190255 int nCell = NCELL(pNode);
190256 RtreeCell cell;
190257 RtreeNode *pChild;
190258
190259 RtreeCell *aCell = 0;
190260
190261 /* Select the child node which will be enlarged the least if pCell
190262 ** is inserted into it. Resolve ties by choosing the entry with
190263 ** the smallest area.
190264 */
190265 for(iCell=0; iCell<nCell; iCell++){
190266 int bBest = 0;
190267 RtreeDValue growth;
190268 RtreeDValue area;
190269 nodeGetCell(pRtree, pNode, iCell, &cell);
190270 growth = cellGrowth(pRtree, &cell, pCell);
190271 area = cellArea(pRtree, &cell);
190272 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
190273 bBest = 1;
190274 }
190275 if( bBest ){
190276 fMinGrowth = growth;
190277 fMinArea = area;
190278 iBest = cell.iRowid;
190279 }
190280 }
190281
190282 sqlite3_free(aCell);
190283 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
190284 nodeRelease(pRtree, pNode);
190285 pNode = pChild;
190286 }
190287
190288 *ppLeaf = pNode;
190289 return rc;
190290}
190291
190292/*
190293** A cell with the same content as pCell has just been inserted into
190294** the node pNode. This function updates the bounding box cells in
190295** all ancestor elements.
190296*/
190297static int AdjustTree(
190298 Rtree *pRtree, /* Rtree table */
190299 RtreeNode *pNode, /* Adjust ancestry of this node. */
190300 RtreeCell *pCell /* This cell was just inserted */
190301){
190302 RtreeNode *p = pNode;
190303 int cnt = 0;
190304 while( p->pParent ){
190305 RtreeNode *pParent = p->pParent;
190306 RtreeCell cell;
190307 int iCell;
190308
190309 if( (++cnt)>1000 || nodeParentIndex(pRtree, p, &iCell) ){
190310 RTREE_IS_CORRUPT(pRtree);
190311 return SQLITE_CORRUPT_VTAB;
190312 }
190313
190314 nodeGetCell(pRtree, pParent, iCell, &cell);
190315 if( !cellContains(pRtree, &cell, pCell) ){
190316 cellUnion(pRtree, &cell, pCell);
190317 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
190318 }
190319
190320 p = pParent;
190321 }
190322 return SQLITE_OK;
190323}
190324
190325/*
190326** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
190327*/
190328static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
190329 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
190330 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
190331 sqlite3_step(pRtree->pWriteRowid);
190332 return sqlite3_reset(pRtree->pWriteRowid);
190333}
190334
190335/*
190336** Write mapping (iNode->iPar) to the <rtree>_parent table.
190337*/
190338static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
190339 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
190340 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
190341 sqlite3_step(pRtree->pWriteParent);
190342 return sqlite3_reset(pRtree->pWriteParent);
190343}
190344
190345static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
190346
190347
190348/*
190349** Arguments aIdx, aDistance and aSpare all point to arrays of size
190350** nIdx. The aIdx array contains the set of integers from 0 to
190351** (nIdx-1) in no particular order. This function sorts the values
190352** in aIdx according to the indexed values in aDistance. For
190353** example, assuming the inputs:
190354**
190355** aIdx = { 0, 1, 2, 3 }
190356** aDistance = { 5.0, 2.0, 7.0, 6.0 }
190357**
190358** this function sets the aIdx array to contain:
190359**
190360** aIdx = { 0, 1, 2, 3 }
190361**
190362** The aSpare array is used as temporary working space by the
190363** sorting algorithm.
190364*/
190365static void SortByDistance(
190366 int *aIdx,
190367 int nIdx,
190368 RtreeDValue *aDistance,
190369 int *aSpare
190370){
190371 if( nIdx>1 ){
190372 int iLeft = 0;
190373 int iRight = 0;
190374
190375 int nLeft = nIdx/2;
190376 int nRight = nIdx-nLeft;
190377 int *aLeft = aIdx;
190378 int *aRight = &aIdx[nLeft];
190379
190380 SortByDistance(aLeft, nLeft, aDistance, aSpare);
190381 SortByDistance(aRight, nRight, aDistance, aSpare);
190382
190383 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
190384 aLeft = aSpare;
190385
190386 while( iLeft<nLeft || iRight<nRight ){
190387 if( iLeft==nLeft ){
190388 aIdx[iLeft+iRight] = aRight[iRight];
190389 iRight++;
190390 }else if( iRight==nRight ){
190391 aIdx[iLeft+iRight] = aLeft[iLeft];
190392 iLeft++;
190393 }else{
190394 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
190395 RtreeDValue fRight = aDistance[aRight[iRight]];
190396 if( fLeft<fRight ){
190397 aIdx[iLeft+iRight] = aLeft[iLeft];
190398 iLeft++;
190399 }else{
190400 aIdx[iLeft+iRight] = aRight[iRight];
190401 iRight++;
190402 }
190403 }
190404 }
190405
190406#if 0
190407 /* Check that the sort worked */
190408 {
190409 int jj;
190410 for(jj=1; jj<nIdx; jj++){
190411 RtreeDValue left = aDistance[aIdx[jj-1]];
190412 RtreeDValue right = aDistance[aIdx[jj]];
190413 assert( left<=right );
190414 }
190415 }
190416#endif
190417 }
190418}
190419
190420/*
190421** Arguments aIdx, aCell and aSpare all point to arrays of size
190422** nIdx. The aIdx array contains the set of integers from 0 to
190423** (nIdx-1) in no particular order. This function sorts the values
190424** in aIdx according to dimension iDim of the cells in aCell. The
190425** minimum value of dimension iDim is considered first, the
190426** maximum used to break ties.
190427**
190428** The aSpare array is used as temporary working space by the
190429** sorting algorithm.
190430*/
190431static void SortByDimension(
190432 Rtree *pRtree,
190433 int *aIdx,
190434 int nIdx,
190435 int iDim,
190436 RtreeCell *aCell,
190437 int *aSpare
190438){
190439 if( nIdx>1 ){
190440
190441 int iLeft = 0;
190442 int iRight = 0;
190443
190444 int nLeft = nIdx/2;
190445 int nRight = nIdx-nLeft;
190446 int *aLeft = aIdx;
190447 int *aRight = &aIdx[nLeft];
190448
190449 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
190450 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
190451
190452 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
190453 aLeft = aSpare;
190454 while( iLeft<nLeft || iRight<nRight ){
190455 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
190456 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
190457 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
190458 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
190459 if( (iLeft!=nLeft) && ((iRight==nRight)
190460 || (xleft1<xright1)
190461 || (xleft1==xright1 && xleft2<xright2)
190462 )){
190463 aIdx[iLeft+iRight] = aLeft[iLeft];
190464 iLeft++;
190465 }else{
190466 aIdx[iLeft+iRight] = aRight[iRight];
190467 iRight++;
190468 }
190469 }
190470
190471#if 0
190472 /* Check that the sort worked */
190473 {
190474 int jj;
190475 for(jj=1; jj<nIdx; jj++){
190476 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
190477 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
190478 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
190479 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
190480 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
190481 }
190482 }
190483#endif
190484 }
190485}
190486
190487/*
190488** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
190489*/
190490static int splitNodeStartree(
190491 Rtree *pRtree,
190492 RtreeCell *aCell,
190493 int nCell,
190494 RtreeNode *pLeft,
190495 RtreeNode *pRight,
190496 RtreeCell *pBboxLeft,
190497 RtreeCell *pBboxRight
190498){
190499 int **aaSorted;
190500 int *aSpare;
190501 int ii;
190502
190503 int iBestDim = 0;
190504 int iBestSplit = 0;
190505 RtreeDValue fBestMargin = RTREE_ZERO;
190506
190507 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
190508
190509 aaSorted = (int **)sqlite3_malloc64(nByte);
190510 if( !aaSorted ){
190511 return SQLITE_NOMEM;
190512 }
190513
190514 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
190515 memset(aaSorted, 0, nByte);
190516 for(ii=0; ii<pRtree->nDim; ii++){
190517 int jj;
190518 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
190519 for(jj=0; jj<nCell; jj++){
190520 aaSorted[ii][jj] = jj;
190521 }
190522 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
190523 }
190524
190525 for(ii=0; ii<pRtree->nDim; ii++){
190526 RtreeDValue margin = RTREE_ZERO;
190527 RtreeDValue fBestOverlap = RTREE_ZERO;
190528 RtreeDValue fBestArea = RTREE_ZERO;
190529 int iBestLeft = 0;
190530 int nLeft;
190531
190532 for(
190533 nLeft=RTREE_MINCELLS(pRtree);
190534 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
190535 nLeft++
190536 ){
190537 RtreeCell left;
190538 RtreeCell right;
190539 int kk;
190540 RtreeDValue overlap;
190541 RtreeDValue area;
190542
190543 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
190544 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
190545 for(kk=1; kk<(nCell-1); kk++){
190546 if( kk<nLeft ){
190547 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
190548 }else{
190549 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
190550 }
190551 }
190552 margin += cellMargin(pRtree, &left);
190553 margin += cellMargin(pRtree, &right);
190554 overlap = cellOverlap(pRtree, &left, &right, 1);
190555 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
190556 if( (nLeft==RTREE_MINCELLS(pRtree))
190557 || (overlap<fBestOverlap)
190558 || (overlap==fBestOverlap && area<fBestArea)
190559 ){
190560 iBestLeft = nLeft;
190561 fBestOverlap = overlap;
190562 fBestArea = area;
190563 }
190564 }
190565
190566 if( ii==0 || margin<fBestMargin ){
190567 iBestDim = ii;
190568 fBestMargin = margin;
190569 iBestSplit = iBestLeft;
190570 }
190571 }
190572
190573 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
190574 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
190575 for(ii=0; ii<nCell; ii++){
190576 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
190577 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
190578 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
190579 nodeInsertCell(pRtree, pTarget, pCell);
190580 cellUnion(pRtree, pBbox, pCell);
190581 }
190582
190583 sqlite3_free(aaSorted);
190584 return SQLITE_OK;
190585}
190586
190587
190588static int updateMapping(
190589 Rtree *pRtree,
190590 i64 iRowid,
190591 RtreeNode *pNode,
190592 int iHeight
190593){
190594 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
190595 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
190596 if( iHeight>0 ){
190597 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
190598 if( pChild ){
190599 nodeRelease(pRtree, pChild->pParent);
190600 nodeReference(pNode);
190601 pChild->pParent = pNode;
190602 }
190603 }
190604 return xSetMapping(pRtree, iRowid, pNode->iNode);
190605}
190606
190607static int SplitNode(
190608 Rtree *pRtree,
190609 RtreeNode *pNode,
190610 RtreeCell *pCell,
190611 int iHeight
190612){
190613 int i;
190614 int newCellIsRight = 0;
190615
190616 int rc = SQLITE_OK;
190617 int nCell = NCELL(pNode);
190618 RtreeCell *aCell;
190619 int *aiUsed;
190620
190621 RtreeNode *pLeft = 0;
190622 RtreeNode *pRight = 0;
190623
190624 RtreeCell leftbbox;
190625 RtreeCell rightbbox;
190626
190627 /* Allocate an array and populate it with a copy of pCell and
190628 ** all cells from node pLeft. Then zero the original node.
190629 */
190630 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
190631 if( !aCell ){
190632 rc = SQLITE_NOMEM;
190633 goto splitnode_out;
190634 }
190635 aiUsed = (int *)&aCell[nCell+1];
190636 memset(aiUsed, 0, sizeof(int)*(nCell+1));
190637 for(i=0; i<nCell; i++){
190638 nodeGetCell(pRtree, pNode, i, &aCell[i]);
190639 }
190640 nodeZero(pRtree, pNode);
190641 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
190642 nCell++;
190643
190644 if( pNode->iNode==1 ){
190645 pRight = nodeNew(pRtree, pNode);
190646 pLeft = nodeNew(pRtree, pNode);
190647 pRtree->iDepth++;
190648 pNode->isDirty = 1;
190649 writeInt16(pNode->zData, pRtree->iDepth);
190650 }else{
190651 pLeft = pNode;
190652 pRight = nodeNew(pRtree, pLeft->pParent);
190653 pLeft->nRef++;
190654 }
190655
190656 if( !pLeft || !pRight ){
190657 rc = SQLITE_NOMEM;
190658 goto splitnode_out;
190659 }
190660
190661 memset(pLeft->zData, 0, pRtree->iNodeSize);
190662 memset(pRight->zData, 0, pRtree->iNodeSize);
190663
190664 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
190665 &leftbbox, &rightbbox);
190666 if( rc!=SQLITE_OK ){
190667 goto splitnode_out;
190668 }
190669
190670 /* Ensure both child nodes have node numbers assigned to them by calling
190671 ** nodeWrite(). Node pRight always needs a node number, as it was created
190672 ** by nodeNew() above. But node pLeft sometimes already has a node number.
190673 ** In this case avoid the all to nodeWrite().
190674 */
190675 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
190676 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
190677 ){
190678 goto splitnode_out;
190679 }
190680
190681 rightbbox.iRowid = pRight->iNode;
190682 leftbbox.iRowid = pLeft->iNode;
190683
190684 if( pNode->iNode==1 ){
190685 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
190686 if( rc!=SQLITE_OK ){
190687 goto splitnode_out;
190688 }
190689 }else{
190690 RtreeNode *pParent = pLeft->pParent;
190691 int iCell;
190692 rc = nodeParentIndex(pRtree, pLeft, &iCell);
190693 if( rc==SQLITE_OK ){
190694 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
190695 rc = AdjustTree(pRtree, pParent, &leftbbox);
190696 }
190697 if( rc!=SQLITE_OK ){
190698 goto splitnode_out;
190699 }
190700 }
190701 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
190702 goto splitnode_out;
190703 }
190704
190705 for(i=0; i<NCELL(pRight); i++){
190706 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
190707 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
190708 if( iRowid==pCell->iRowid ){
190709 newCellIsRight = 1;
190710 }
190711 if( rc!=SQLITE_OK ){
190712 goto splitnode_out;
190713 }
190714 }
190715 if( pNode->iNode==1 ){
190716 for(i=0; i<NCELL(pLeft); i++){
190717 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
190718 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
190719 if( rc!=SQLITE_OK ){
190720 goto splitnode_out;
190721 }
190722 }
190723 }else if( newCellIsRight==0 ){
190724 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
190725 }
190726
190727 if( rc==SQLITE_OK ){
190728 rc = nodeRelease(pRtree, pRight);
190729 pRight = 0;
190730 }
190731 if( rc==SQLITE_OK ){
190732 rc = nodeRelease(pRtree, pLeft);
190733 pLeft = 0;
190734 }
190735
190736splitnode_out:
190737 nodeRelease(pRtree, pRight);
190738 nodeRelease(pRtree, pLeft);
190739 sqlite3_free(aCell);
190740 return rc;
190741}
190742
190743/*
190744** If node pLeaf is not the root of the r-tree and its pParent pointer is
190745** still NULL, load all ancestor nodes of pLeaf into memory and populate
190746** the pLeaf->pParent chain all the way up to the root node.
190747**
190748** This operation is required when a row is deleted (or updated - an update
190749** is implemented as a delete followed by an insert). SQLite provides the
190750** rowid of the row to delete, which can be used to find the leaf on which
190751** the entry resides (argument pLeaf). Once the leaf is located, this
190752** function is called to determine its ancestry.
190753*/
190754static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
190755 int rc = SQLITE_OK;
190756 RtreeNode *pChild = pLeaf;
190757 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
190758 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
190759 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
190760 rc = sqlite3_step(pRtree->pReadParent);
190761 if( rc==SQLITE_ROW ){
190762 RtreeNode *pTest; /* Used to test for reference loops */
190763 i64 iNode; /* Node number of parent node */
190764
190765 /* Before setting pChild->pParent, test that we are not creating a
190766 ** loop of references (as we would if, say, pChild==pParent). We don't
190767 ** want to do this as it leads to a memory leak when trying to delete
190768 ** the referenced counted node structures.
190769 */
190770 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
190771 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
190772 if( !pTest ){
190773 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
190774 }
190775 }
190776 rc = sqlite3_reset(pRtree->pReadParent);
190777 if( rc==SQLITE_OK ) rc = rc2;
190778 if( rc==SQLITE_OK && !pChild->pParent ){
190779 RTREE_IS_CORRUPT(pRtree);
190780 rc = SQLITE_CORRUPT_VTAB;
190781 }
190782 pChild = pChild->pParent;
190783 }
190784 return rc;
190785}
190786
190787static int deleteCell(Rtree *, RtreeNode *, int, int);
190788
190789static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
190790 int rc;
190791 int rc2;
190792 RtreeNode *pParent = 0;
190793 int iCell;
190794
190795 assert( pNode->nRef==1 );
190796
190797 /* Remove the entry in the parent cell. */
190798 rc = nodeParentIndex(pRtree, pNode, &iCell);
190799 if( rc==SQLITE_OK ){
190800 pParent = pNode->pParent;
190801 pNode->pParent = 0;
190802 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
190803 }
190804 rc2 = nodeRelease(pRtree, pParent);
190805 if( rc==SQLITE_OK ){
190806 rc = rc2;
190807 }
190808 if( rc!=SQLITE_OK ){
190809 return rc;
190810 }
190811
190812 /* Remove the xxx_node entry. */
190813 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
190814 sqlite3_step(pRtree->pDeleteNode);
190815 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
190816 return rc;
190817 }
190818
190819 /* Remove the xxx_parent entry. */
190820 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
190821 sqlite3_step(pRtree->pDeleteParent);
190822 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
190823 return rc;
190824 }
190825
190826 /* Remove the node from the in-memory hash table and link it into
190827 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
190828 */
190829 nodeHashDelete(pRtree, pNode);
190830 pNode->iNode = iHeight;
190831 pNode->pNext = pRtree->pDeleted;
190832 pNode->nRef++;
190833 pRtree->pDeleted = pNode;
190834
190835 return SQLITE_OK;
190836}
190837
190838static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
190839 RtreeNode *pParent = pNode->pParent;
190840 int rc = SQLITE_OK;
190841 if( pParent ){
190842 int ii;
190843 int nCell = NCELL(pNode);
190844 RtreeCell box; /* Bounding box for pNode */
190845 nodeGetCell(pRtree, pNode, 0, &box);
190846 for(ii=1; ii<nCell; ii++){
190847 RtreeCell cell;
190848 nodeGetCell(pRtree, pNode, ii, &cell);
190849 cellUnion(pRtree, &box, &cell);
190850 }
190851 box.iRowid = pNode->iNode;
190852 rc = nodeParentIndex(pRtree, pNode, &ii);
190853 if( rc==SQLITE_OK ){
190854 nodeOverwriteCell(pRtree, pParent, &box, ii);
190855 rc = fixBoundingBox(pRtree, pParent);
190856 }
190857 }
190858 return rc;
190859}
190860
190861/*
190862** Delete the cell at index iCell of node pNode. After removing the
190863** cell, adjust the r-tree data structure if required.
190864*/
190865static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
190866 RtreeNode *pParent;
190867 int rc;
190868
190869 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
190870 return rc;
190871 }
190872
190873 /* Remove the cell from the node. This call just moves bytes around
190874 ** the in-memory node image, so it cannot fail.
190875 */
190876 nodeDeleteCell(pRtree, pNode, iCell);
190877
190878 /* If the node is not the tree root and now has less than the minimum
190879 ** number of cells, remove it from the tree. Otherwise, update the
190880 ** cell in the parent node so that it tightly contains the updated
190881 ** node.
190882 */
190883 pParent = pNode->pParent;
190884 assert( pParent || pNode->iNode==1 );
190885 if( pParent ){
190886 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
190887 rc = removeNode(pRtree, pNode, iHeight);
190888 }else{
190889 rc = fixBoundingBox(pRtree, pNode);
190890 }
190891 }
190892
190893 return rc;
190894}
190895
190896static int Reinsert(
190897 Rtree *pRtree,
190898 RtreeNode *pNode,
190899 RtreeCell *pCell,
190900 int iHeight
190901){
190902 int *aOrder;
190903 int *aSpare;
190904 RtreeCell *aCell;
190905 RtreeDValue *aDistance;
190906 int nCell;
190907 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
190908 int iDim;
190909 int ii;
190910 int rc = SQLITE_OK;
190911 int n;
190912
190913 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
190914
190915 nCell = NCELL(pNode)+1;
190916 n = (nCell+1)&(~1);
190917
190918 /* Allocate the buffers used by this operation. The allocation is
190919 ** relinquished before this function returns.
190920 */
190921 aCell = (RtreeCell *)sqlite3_malloc64(n * (
190922 sizeof(RtreeCell) + /* aCell array */
190923 sizeof(int) + /* aOrder array */
190924 sizeof(int) + /* aSpare array */
190925 sizeof(RtreeDValue) /* aDistance array */
190926 ));
190927 if( !aCell ){
190928 return SQLITE_NOMEM;
190929 }
190930 aOrder = (int *)&aCell[n];
190931 aSpare = (int *)&aOrder[n];
190932 aDistance = (RtreeDValue *)&aSpare[n];
190933
190934 for(ii=0; ii<nCell; ii++){
190935 if( ii==(nCell-1) ){
190936 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
190937 }else{
190938 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
190939 }
190940 aOrder[ii] = ii;
190941 for(iDim=0; iDim<pRtree->nDim; iDim++){
190942 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
190943 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
190944 }
190945 }
190946 for(iDim=0; iDim<pRtree->nDim; iDim++){
190947 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
190948 }
190949
190950 for(ii=0; ii<nCell; ii++){
190951 aDistance[ii] = RTREE_ZERO;
190952 for(iDim=0; iDim<pRtree->nDim; iDim++){
190953 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
190954 DCOORD(aCell[ii].aCoord[iDim*2]));
190955 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
190956 }
190957 }
190958
190959 SortByDistance(aOrder, nCell, aDistance, aSpare);
190960 nodeZero(pRtree, pNode);
190961
190962 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
190963 RtreeCell *p = &aCell[aOrder[ii]];
190964 nodeInsertCell(pRtree, pNode, p);
190965 if( p->iRowid==pCell->iRowid ){
190966 if( iHeight==0 ){
190967 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
190968 }else{
190969 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
190970 }
190971 }
190972 }
190973 if( rc==SQLITE_OK ){
190974 rc = fixBoundingBox(pRtree, pNode);
190975 }
190976 for(; rc==SQLITE_OK && ii<nCell; ii++){
190977 /* Find a node to store this cell in. pNode->iNode currently contains
190978 ** the height of the sub-tree headed by the cell.
190979 */
190980 RtreeNode *pInsert;
190981 RtreeCell *p = &aCell[aOrder[ii]];
190982 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
190983 if( rc==SQLITE_OK ){
190984 int rc2;
190985 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
190986 rc2 = nodeRelease(pRtree, pInsert);
190987 if( rc==SQLITE_OK ){
190988 rc = rc2;
190989 }
190990 }
190991 }
190992
190993 sqlite3_free(aCell);
190994 return rc;
190995}
190996
190997/*
190998** Insert cell pCell into node pNode. Node pNode is the head of a
190999** subtree iHeight high (leaf nodes have iHeight==0).
191000*/
191001static int rtreeInsertCell(
191002 Rtree *pRtree,
191003 RtreeNode *pNode,
191004 RtreeCell *pCell,
191005 int iHeight
191006){
191007 int rc = SQLITE_OK;
191008 if( iHeight>0 ){
191009 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
191010 if( pChild ){
191011 nodeRelease(pRtree, pChild->pParent);
191012 nodeReference(pNode);
191013 pChild->pParent = pNode;
191014 }
191015 }
191016 if( nodeInsertCell(pRtree, pNode, pCell) ){
191017 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
191018 rc = SplitNode(pRtree, pNode, pCell, iHeight);
191019 }else{
191020 pRtree->iReinsertHeight = iHeight;
191021 rc = Reinsert(pRtree, pNode, pCell, iHeight);
191022 }
191023 }else{
191024 rc = AdjustTree(pRtree, pNode, pCell);
191025 if( rc==SQLITE_OK ){
191026 if( iHeight==0 ){
191027 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
191028 }else{
191029 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
191030 }
191031 }
191032 }
191033 return rc;
191034}
191035
191036static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
191037 int ii;
191038 int rc = SQLITE_OK;
191039 int nCell = NCELL(pNode);
191040
191041 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
191042 RtreeNode *pInsert;
191043 RtreeCell cell;
191044 nodeGetCell(pRtree, pNode, ii, &cell);
191045
191046 /* Find a node to store this cell in. pNode->iNode currently contains
191047 ** the height of the sub-tree headed by the cell.
191048 */
191049 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
191050 if( rc==SQLITE_OK ){
191051 int rc2;
191052 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
191053 rc2 = nodeRelease(pRtree, pInsert);
191054 if( rc==SQLITE_OK ){
191055 rc = rc2;
191056 }
191057 }
191058 }
191059 return rc;
191060}
191061
191062/*
191063** Select a currently unused rowid for a new r-tree record.
191064*/
191065static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
191066 int rc;
191067 sqlite3_bind_null(pRtree->pWriteRowid, 1);
191068 sqlite3_bind_null(pRtree->pWriteRowid, 2);
191069 sqlite3_step(pRtree->pWriteRowid);
191070 rc = sqlite3_reset(pRtree->pWriteRowid);
191071 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
191072 return rc;
191073}
191074
191075/*
191076** Remove the entry with rowid=iDelete from the r-tree structure.
191077*/
191078static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
191079 int rc; /* Return code */
191080 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
191081 int iCell; /* Index of iDelete cell in pLeaf */
191082 RtreeNode *pRoot = 0; /* Root node of rtree structure */
191083
191084
191085 /* Obtain a reference to the root node to initialize Rtree.iDepth */
191086 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
191087
191088 /* Obtain a reference to the leaf node that contains the entry
191089 ** about to be deleted.
191090 */
191091 if( rc==SQLITE_OK ){
191092 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
191093 }
191094
191095#ifdef CORRUPT_DB
191096 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
191097#endif
191098
191099 /* Delete the cell in question from the leaf node. */
191100 if( rc==SQLITE_OK && pLeaf ){
191101 int rc2;
191102 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
191103 if( rc==SQLITE_OK ){
191104 rc = deleteCell(pRtree, pLeaf, iCell, 0);
191105 }
191106 rc2 = nodeRelease(pRtree, pLeaf);
191107 if( rc==SQLITE_OK ){
191108 rc = rc2;
191109 }
191110 }
191111
191112 /* Delete the corresponding entry in the <rtree>_rowid table. */
191113 if( rc==SQLITE_OK ){
191114 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
191115 sqlite3_step(pRtree->pDeleteRowid);
191116 rc = sqlite3_reset(pRtree->pDeleteRowid);
191117 }
191118
191119 /* Check if the root node now has exactly one child. If so, remove
191120 ** it, schedule the contents of the child for reinsertion and
191121 ** reduce the tree height by one.
191122 **
191123 ** This is equivalent to copying the contents of the child into
191124 ** the root node (the operation that Gutman's paper says to perform
191125 ** in this scenario).
191126 */
191127 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
191128 int rc2;
191129 RtreeNode *pChild = 0;
191130 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
191131 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
191132 if( rc==SQLITE_OK ){
191133 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
191134 }
191135 rc2 = nodeRelease(pRtree, pChild);
191136 if( rc==SQLITE_OK ) rc = rc2;
191137 if( rc==SQLITE_OK ){
191138 pRtree->iDepth--;
191139 writeInt16(pRoot->zData, pRtree->iDepth);
191140 pRoot->isDirty = 1;
191141 }
191142 }
191143
191144 /* Re-insert the contents of any underfull nodes removed from the tree. */
191145 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
191146 if( rc==SQLITE_OK ){
191147 rc = reinsertNodeContent(pRtree, pLeaf);
191148 }
191149 pRtree->pDeleted = pLeaf->pNext;
191150 pRtree->nNodeRef--;
191151 sqlite3_free(pLeaf);
191152 }
191153
191154 /* Release the reference to the root node. */
191155 if( rc==SQLITE_OK ){
191156 rc = nodeRelease(pRtree, pRoot);
191157 }else{
191158 nodeRelease(pRtree, pRoot);
191159 }
191160
191161 return rc;
191162}
191163
191164/*
191165** Rounding constants for float->double conversion.
191166*/
191167#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
191168#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
191169
191170#if !defined(SQLITE_RTREE_INT_ONLY)
191171/*
191172** Convert an sqlite3_value into an RtreeValue (presumably a float)
191173** while taking care to round toward negative or positive, respectively.
191174*/
191175static RtreeValue rtreeValueDown(sqlite3_value *v){
191176 double d = sqlite3_value_double(v);
191177 float f = (float)d;
191178 if( f>d ){
191179 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
191180 }
191181 return f;
191182}
191183static RtreeValue rtreeValueUp(sqlite3_value *v){
191184 double d = sqlite3_value_double(v);
191185 float f = (float)d;
191186 if( f<d ){
191187 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
191188 }
191189 return f;
191190}
191191#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
191192
191193/*
191194** A constraint has failed while inserting a row into an rtree table.
191195** Assuming no OOM error occurs, this function sets the error message
191196** (at pRtree->base.zErrMsg) to an appropriate value and returns
191197** SQLITE_CONSTRAINT.
191198**
191199** Parameter iCol is the index of the leftmost column involved in the
191200** constraint failure. If it is 0, then the constraint that failed is
191201** the unique constraint on the id column. Otherwise, it is the rtree
191202** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
191203**
191204** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
191205*/
191206static int rtreeConstraintError(Rtree *pRtree, int iCol){
191207 sqlite3_stmt *pStmt = 0;
191208 char *zSql;
191209 int rc;
191210
191211 assert( iCol==0 || iCol%2 );
191212 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
191213 if( zSql ){
191214 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
191215 }else{
191216 rc = SQLITE_NOMEM;
191217 }
191218 sqlite3_free(zSql);
191219
191220 if( rc==SQLITE_OK ){
191221 if( iCol==0 ){
191222 const char *zCol = sqlite3_column_name(pStmt, 0);
191223 pRtree->base.zErrMsg = sqlite3_mprintf(
191224 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
191225 );
191226 }else{
191227 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
191228 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
191229 pRtree->base.zErrMsg = sqlite3_mprintf(
191230 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
191231 );
191232 }
191233 }
191234
191235 sqlite3_finalize(pStmt);
191236 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
191237}
191238
191239
191240
191241/*
191242** The xUpdate method for rtree module virtual tables.
191243*/
191244static int rtreeUpdate(
191245 sqlite3_vtab *pVtab,
191246 int nData,
191247 sqlite3_value **aData,
191248 sqlite_int64 *pRowid
191249){
191250 Rtree *pRtree = (Rtree *)pVtab;
191251 int rc = SQLITE_OK;
191252 RtreeCell cell; /* New cell to insert if nData>1 */
191253 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
191254
191255 if( pRtree->nNodeRef ){
191256 /* Unable to write to the btree while another cursor is reading from it,
191257 ** since the write might do a rebalance which would disrupt the read
191258 ** cursor. */
191259 return SQLITE_LOCKED_VTAB;
191260 }
191261 rtreeReference(pRtree);
191262 assert(nData>=1);
191263
191264 cell.iRowid = 0; /* Used only to suppress a compiler warning */
191265
191266 /* Constraint handling. A write operation on an r-tree table may return
191267 ** SQLITE_CONSTRAINT for two reasons:
191268 **
191269 ** 1. A duplicate rowid value, or
191270 ** 2. The supplied data violates the "x2>=x1" constraint.
191271 **
191272 ** In the first case, if the conflict-handling mode is REPLACE, then
191273 ** the conflicting row can be removed before proceeding. In the second
191274 ** case, SQLITE_CONSTRAINT must be returned regardless of the
191275 ** conflict-handling mode specified by the user.
191276 */
191277 if( nData>1 ){
191278 int ii;
191279 int nn = nData - 4;
191280
191281 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
191282 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
191283 **
191284 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
191285 ** with "column" that are interpreted as table constraints.
191286 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
191287 ** This problem was discovered after years of use, so we silently ignore
191288 ** these kinds of misdeclared tables to avoid breaking any legacy.
191289 */
191290
191291#ifndef SQLITE_RTREE_INT_ONLY
191292 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
191293 for(ii=0; ii<nn; ii+=2){
191294 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
191295 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
191296 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
191297 rc = rtreeConstraintError(pRtree, ii+1);
191298 goto constraint;
191299 }
191300 }
191301 }else
191302#endif
191303 {
191304 for(ii=0; ii<nn; ii+=2){
191305 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
191306 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
191307 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
191308 rc = rtreeConstraintError(pRtree, ii+1);
191309 goto constraint;
191310 }
191311 }
191312 }
191313
191314 /* If a rowid value was supplied, check if it is already present in
191315 ** the table. If so, the constraint has failed. */
191316 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
191317 cell.iRowid = sqlite3_value_int64(aData[2]);
191318 if( sqlite3_value_type(aData[0])==SQLITE_NULL
191319 || sqlite3_value_int64(aData[0])!=cell.iRowid
191320 ){
191321 int steprc;
191322 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
191323 steprc = sqlite3_step(pRtree->pReadRowid);
191324 rc = sqlite3_reset(pRtree->pReadRowid);
191325 if( SQLITE_ROW==steprc ){
191326 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
191327 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
191328 }else{
191329 rc = rtreeConstraintError(pRtree, 0);
191330 goto constraint;
191331 }
191332 }
191333 }
191334 bHaveRowid = 1;
191335 }
191336 }
191337
191338 /* If aData[0] is not an SQL NULL value, it is the rowid of a
191339 ** record to delete from the r-tree table. The following block does
191340 ** just that.
191341 */
191342 if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
191343 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
191344 }
191345
191346 /* If the aData[] array contains more than one element, elements
191347 ** (aData[2]..aData[argc-1]) contain a new record to insert into
191348 ** the r-tree structure.
191349 */
191350 if( rc==SQLITE_OK && nData>1 ){
191351 /* Insert the new record into the r-tree */
191352 RtreeNode *pLeaf = 0;
191353
191354 /* Figure out the rowid of the new row. */
191355 if( bHaveRowid==0 ){
191356 rc = rtreeNewRowid(pRtree, &cell.iRowid);
191357 }
191358 *pRowid = cell.iRowid;
191359
191360 if( rc==SQLITE_OK ){
191361 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
191362 }
191363 if( rc==SQLITE_OK ){
191364 int rc2;
191365 pRtree->iReinsertHeight = -1;
191366 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
191367 rc2 = nodeRelease(pRtree, pLeaf);
191368 if( rc==SQLITE_OK ){
191369 rc = rc2;
191370 }
191371 }
191372 if( rc==SQLITE_OK && pRtree->nAux ){
191373 sqlite3_stmt *pUp = pRtree->pWriteAux;
191374 int jj;
191375 sqlite3_bind_int64(pUp, 1, *pRowid);
191376 for(jj=0; jj<pRtree->nAux; jj++){
191377 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
191378 }
191379 sqlite3_step(pUp);
191380 rc = sqlite3_reset(pUp);
191381 }
191382 }
191383
191384constraint:
191385 rtreeRelease(pRtree);
191386 return rc;
191387}
191388
191389/*
191390** Called when a transaction starts.
191391*/
191392static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
191393 Rtree *pRtree = (Rtree *)pVtab;
191394 assert( pRtree->inWrTrans==0 );
191395 pRtree->inWrTrans++;
191396 return SQLITE_OK;
191397}
191398
191399/*
191400** Called when a transaction completes (either by COMMIT or ROLLBACK).
191401** The sqlite3_blob object should be released at this point.
191402*/
191403static int rtreeEndTransaction(sqlite3_vtab *pVtab){
191404 Rtree *pRtree = (Rtree *)pVtab;
191405 pRtree->inWrTrans = 0;
191406 nodeBlobReset(pRtree);
191407 return SQLITE_OK;
191408}
191409
191410/*
191411** The xRename method for rtree module virtual tables.
191412*/
191413static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
191414 Rtree *pRtree = (Rtree *)pVtab;
191415 int rc = SQLITE_NOMEM;
191416 char *zSql = sqlite3_mprintf(
191417 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
191418 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
191419 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
191420 , pRtree->zDb, pRtree->zName, zNewName
191421 , pRtree->zDb, pRtree->zName, zNewName
191422 , pRtree->zDb, pRtree->zName, zNewName
191423 );
191424 if( zSql ){
191425 nodeBlobReset(pRtree);
191426 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
191427 sqlite3_free(zSql);
191428 }
191429 return rc;
191430}
191431
191432/*
191433** The xSavepoint method.
191434**
191435** This module does not need to do anything to support savepoints. However,
191436** it uses this hook to close any open blob handle. This is done because a
191437** DROP TABLE command - which fortunately always opens a savepoint - cannot
191438** succeed if there are any open blob handles. i.e. if the blob handle were
191439** not closed here, the following would fail:
191440**
191441** BEGIN;
191442** INSERT INTO rtree...
191443** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
191444** COMMIT;
191445*/
191446static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
191447 Rtree *pRtree = (Rtree *)pVtab;
191448 u8 iwt = pRtree->inWrTrans;
191449 UNUSED_PARAMETER(iSavepoint);
191450 pRtree->inWrTrans = 0;
191451 nodeBlobReset(pRtree);
191452 pRtree->inWrTrans = iwt;
191453 return SQLITE_OK;
191454}
191455
191456/*
191457** This function populates the pRtree->nRowEst variable with an estimate
191458** of the number of rows in the virtual table. If possible, this is based
191459** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
191460*/
191461static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
191462 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
191463 char *zSql;
191464 sqlite3_stmt *p;
191465 int rc;
191466 i64 nRow = 0;
191467
191468 rc = sqlite3_table_column_metadata(
191469 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
191470 );
191471 if( rc!=SQLITE_OK ){
191472 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
191473 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
191474 }
191475 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
191476 if( zSql==0 ){
191477 rc = SQLITE_NOMEM;
191478 }else{
191479 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
191480 if( rc==SQLITE_OK ){
191481 if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
191482 rc = sqlite3_finalize(p);
191483 }else if( rc!=SQLITE_NOMEM ){
191484 rc = SQLITE_OK;
191485 }
191486
191487 if( rc==SQLITE_OK ){
191488 if( nRow==0 ){
191489 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
191490 }else{
191491 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
191492 }
191493 }
191494 sqlite3_free(zSql);
191495 }
191496
191497 return rc;
191498}
191499
191500
191501/*
191502** Return true if zName is the extension on one of the shadow tables used
191503** by this module.
191504*/
191505static int rtreeShadowName(const char *zName){
191506 static const char *azName[] = {
191507 "node", "parent", "rowid"
191508 };
191509 unsigned int i;
191510 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
191511 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
191512 }
191513 return 0;
191514}
191515
191516static sqlite3_module rtreeModule = {
191517 3, /* iVersion */
191518 rtreeCreate, /* xCreate - create a table */
191519 rtreeConnect, /* xConnect - connect to an existing table */
191520 rtreeBestIndex, /* xBestIndex - Determine search strategy */
191521 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
191522 rtreeDestroy, /* xDestroy - Drop a table */
191523 rtreeOpen, /* xOpen - open a cursor */
191524 rtreeClose, /* xClose - close a cursor */
191525 rtreeFilter, /* xFilter - configure scan constraints */
191526 rtreeNext, /* xNext - advance a cursor */
191527 rtreeEof, /* xEof */
191528 rtreeColumn, /* xColumn - read data */
191529 rtreeRowid, /* xRowid - read data */
191530 rtreeUpdate, /* xUpdate - write data */
191531 rtreeBeginTransaction, /* xBegin - begin transaction */
191532 rtreeEndTransaction, /* xSync - sync transaction */
191533 rtreeEndTransaction, /* xCommit - commit transaction */
191534 rtreeEndTransaction, /* xRollback - rollback transaction */
191535 0, /* xFindFunction - function overloading */
191536 rtreeRename, /* xRename - rename the table */
191537 rtreeSavepoint, /* xSavepoint */
191538 0, /* xRelease */
191539 0, /* xRollbackTo */
191540 rtreeShadowName /* xShadowName */
191541};
191542
191543static int rtreeSqlInit(
191544 Rtree *pRtree,
191545 sqlite3 *db,
191546 const char *zDb,
191547 const char *zPrefix,
191548 int isCreate
191549){
191550 int rc = SQLITE_OK;
191551
191552 #define N_STATEMENT 8
191553 static const char *azSql[N_STATEMENT] = {
191554 /* Write the xxx_node table */
191555 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
191556 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
191557
191558 /* Read and write the xxx_rowid table */
191559 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
191560 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
191561 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
191562
191563 /* Read and write the xxx_parent table */
191564 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
191565 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
191566 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
191567 };
191568 sqlite3_stmt **appStmt[N_STATEMENT];
191569 int i;
191570 const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
191571
191572 pRtree->db = db;
191573
191574 if( isCreate ){
191575 char *zCreate;
191576 sqlite3_str *p = sqlite3_str_new(db);
191577 int ii;
191578 sqlite3_str_appendf(p,
191579 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
191580 zDb, zPrefix);
191581 for(ii=0; ii<pRtree->nAux; ii++){
191582 sqlite3_str_appendf(p,",a%d",ii);
191583 }
191584 sqlite3_str_appendf(p,
191585 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
191586 zDb, zPrefix);
191587 sqlite3_str_appendf(p,
191588 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
191589 zDb, zPrefix);
191590 sqlite3_str_appendf(p,
191591 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
191592 zDb, zPrefix, pRtree->iNodeSize);
191593 zCreate = sqlite3_str_finish(p);
191594 if( !zCreate ){
191595 return SQLITE_NOMEM;
191596 }
191597 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
191598 sqlite3_free(zCreate);
191599 if( rc!=SQLITE_OK ){
191600 return rc;
191601 }
191602 }
191603
191604 appStmt[0] = &pRtree->pWriteNode;
191605 appStmt[1] = &pRtree->pDeleteNode;
191606 appStmt[2] = &pRtree->pReadRowid;
191607 appStmt[3] = &pRtree->pWriteRowid;
191608 appStmt[4] = &pRtree->pDeleteRowid;
191609 appStmt[5] = &pRtree->pReadParent;
191610 appStmt[6] = &pRtree->pWriteParent;
191611 appStmt[7] = &pRtree->pDeleteParent;
191612
191613 rc = rtreeQueryStat1(db, pRtree);
191614 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
191615 char *zSql;
191616 const char *zFormat;
191617 if( i!=3 || pRtree->nAux==0 ){
191618 zFormat = azSql[i];
191619 }else {
191620 /* An UPSERT is very slightly slower than REPLACE, but it is needed
191621 ** if there are auxiliary columns */
191622 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
191623 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
191624 }
191625 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
191626 if( zSql ){
191627 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
191628 }else{
191629 rc = SQLITE_NOMEM;
191630 }
191631 sqlite3_free(zSql);
191632 }
191633 if( pRtree->nAux ){
191634 pRtree->zReadAuxSql = sqlite3_mprintf(
191635 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
191636 zDb, zPrefix);
191637 if( pRtree->zReadAuxSql==0 ){
191638 rc = SQLITE_NOMEM;
191639 }else{
191640 sqlite3_str *p = sqlite3_str_new(db);
191641 int ii;
191642 char *zSql;
191643 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
191644 for(ii=0; ii<pRtree->nAux; ii++){
191645 if( ii ) sqlite3_str_append(p, ",", 1);
191646 if( ii<pRtree->nAuxNotNull ){
191647 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
191648 }else{
191649 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
191650 }
191651 }
191652 sqlite3_str_appendf(p, " WHERE rowid=?1");
191653 zSql = sqlite3_str_finish(p);
191654 if( zSql==0 ){
191655 rc = SQLITE_NOMEM;
191656 }else{
191657 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
191658 sqlite3_free(zSql);
191659 }
191660 }
191661 }
191662
191663 return rc;
191664}
191665
191666/*
191667** The second argument to this function contains the text of an SQL statement
191668** that returns a single integer value. The statement is compiled and executed
191669** using database connection db. If successful, the integer value returned
191670** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
191671** code is returned and the value of *piVal after returning is not defined.
191672*/
191673static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
191674 int rc = SQLITE_NOMEM;
191675 if( zSql ){
191676 sqlite3_stmt *pStmt = 0;
191677 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
191678 if( rc==SQLITE_OK ){
191679 if( SQLITE_ROW==sqlite3_step(pStmt) ){
191680 *piVal = sqlite3_column_int(pStmt, 0);
191681 }
191682 rc = sqlite3_finalize(pStmt);
191683 }
191684 }
191685 return rc;
191686}
191687
191688/*
191689** This function is called from within the xConnect() or xCreate() method to
191690** determine the node-size used by the rtree table being created or connected
191691** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
191692** Otherwise, an SQLite error code is returned.
191693**
191694** If this function is being called as part of an xConnect(), then the rtree
191695** table already exists. In this case the node-size is determined by inspecting
191696** the root node of the tree.
191697**
191698** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
191699** This ensures that each node is stored on a single database page. If the
191700** database page-size is so large that more than RTREE_MAXCELLS entries
191701** would fit in a single node, use a smaller node-size.
191702*/
191703static int getNodeSize(
191704 sqlite3 *db, /* Database handle */
191705 Rtree *pRtree, /* Rtree handle */
191706 int isCreate, /* True for xCreate, false for xConnect */
191707 char **pzErr /* OUT: Error message, if any */
191708){
191709 int rc;
191710 char *zSql;
191711 if( isCreate ){
191712 int iPageSize = 0;
191713 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
191714 rc = getIntFromStmt(db, zSql, &iPageSize);
191715 if( rc==SQLITE_OK ){
191716 pRtree->iNodeSize = iPageSize-64;
191717 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
191718 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
191719 }
191720 }else{
191721 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191722 }
191723 }else{
191724 zSql = sqlite3_mprintf(
191725 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
191726 pRtree->zDb, pRtree->zName
191727 );
191728 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
191729 if( rc!=SQLITE_OK ){
191730 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191731 }else if( pRtree->iNodeSize<(512-64) ){
191732 rc = SQLITE_CORRUPT_VTAB;
191733 RTREE_IS_CORRUPT(pRtree);
191734 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
191735 pRtree->zName);
191736 }
191737 }
191738
191739 sqlite3_free(zSql);
191740 return rc;
191741}
191742
191743/*
191744** Return the length of a token
191745*/
191746static int rtreeTokenLength(const char *z){
191747 int dummy = 0;
191748 return sqlite3GetToken((const unsigned char*)z,&dummy);
191749}
191750
191751/*
191752** This function is the implementation of both the xConnect and xCreate
191753** methods of the r-tree virtual table.
191754**
191755** argv[0] -> module name
191756** argv[1] -> database name
191757** argv[2] -> table name
191758** argv[...] -> column names...
191759*/
191760static int rtreeInit(
191761 sqlite3 *db, /* Database connection */
191762 void *pAux, /* One of the RTREE_COORD_* constants */
191763 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
191764 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
191765 char **pzErr, /* OUT: Error message, if any */
191766 int isCreate /* True for xCreate, false for xConnect */
191767){
191768 int rc = SQLITE_OK;
191769 Rtree *pRtree;
191770 int nDb; /* Length of string argv[1] */
191771 int nName; /* Length of string argv[2] */
191772 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
191773 sqlite3_str *pSql;
191774 char *zSql;
191775 int ii = 4;
191776 int iErr;
191777
191778 const char *aErrMsg[] = {
191779 0, /* 0 */
191780 "Wrong number of columns for an rtree table", /* 1 */
191781 "Too few columns for an rtree table", /* 2 */
191782 "Too many columns for an rtree table", /* 3 */
191783 "Auxiliary rtree columns must be last" /* 4 */
191784 };
191785
191786 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
191787 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN+3 ){
191788 *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
191789 return SQLITE_ERROR;
191790 }
191791
191792 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
191793
191794 /* Allocate the sqlite3_vtab structure */
191795 nDb = (int)strlen(argv[1]);
191796 nName = (int)strlen(argv[2]);
191797 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
191798 if( !pRtree ){
191799 return SQLITE_NOMEM;
191800 }
191801 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
191802 pRtree->nBusy = 1;
191803 pRtree->base.pModule = &rtreeModule;
191804 pRtree->zDb = (char *)&pRtree[1];
191805 pRtree->zName = &pRtree->zDb[nDb+1];
191806 pRtree->eCoordType = (u8)eCoordType;
191807 memcpy(pRtree->zDb, argv[1], nDb);
191808 memcpy(pRtree->zName, argv[2], nName);
191809
191810
191811 /* Create/Connect to the underlying relational database schema. If
191812 ** that is successful, call sqlite3_declare_vtab() to configure
191813 ** the r-tree table schema.
191814 */
191815 pSql = sqlite3_str_new(db);
191816 sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
191817 rtreeTokenLength(argv[3]), argv[3]);
191818 for(ii=4; ii<argc; ii++){
191819 const char *zArg = argv[ii];
191820 if( zArg[0]=='+' ){
191821 pRtree->nAux++;
191822 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
191823 }else if( pRtree->nAux>0 ){
191824 break;
191825 }else{
191826 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
191827 pRtree->nDim2++;
191828 sqlite3_str_appendf(pSql, azFormat[eCoordType],
191829 rtreeTokenLength(zArg), zArg);
191830 }
191831 }
191832 sqlite3_str_appendf(pSql, ");");
191833 zSql = sqlite3_str_finish(pSql);
191834 if( !zSql ){
191835 rc = SQLITE_NOMEM;
191836 }else if( ii<argc ){
191837 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
191838 rc = SQLITE_ERROR;
191839 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
191840 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191841 }
191842 sqlite3_free(zSql);
191843 if( rc ) goto rtreeInit_fail;
191844 pRtree->nDim = pRtree->nDim2/2;
191845 if( pRtree->nDim<1 ){
191846 iErr = 2;
191847 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
191848 iErr = 3;
191849 }else if( pRtree->nDim2 % 2 ){
191850 iErr = 1;
191851 }else{
191852 iErr = 0;
191853 }
191854 if( iErr ){
191855 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
191856 goto rtreeInit_fail;
191857 }
191858 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
191859
191860 /* Figure out the node size to use. */
191861 rc = getNodeSize(db, pRtree, isCreate, pzErr);
191862 if( rc ) goto rtreeInit_fail;
191863 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
191864 if( rc ){
191865 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
191866 goto rtreeInit_fail;
191867 }
191868
191869 *ppVtab = (sqlite3_vtab *)pRtree;
191870 return SQLITE_OK;
191871
191872rtreeInit_fail:
191873 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
191874 assert( *ppVtab==0 );
191875 assert( pRtree->nBusy==1 );
191876 rtreeRelease(pRtree);
191877 return rc;
191878}
191879
191880
191881/*
191882** Implementation of a scalar function that decodes r-tree nodes to
191883** human readable strings. This can be used for debugging and analysis.
191884**
191885** The scalar function takes two arguments: (1) the number of dimensions
191886** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
191887** an r-tree node. For a two-dimensional r-tree structure called "rt", to
191888** deserialize all nodes, a statement like:
191889**
191890** SELECT rtreenode(2, data) FROM rt_node;
191891**
191892** The human readable string takes the form of a Tcl list with one
191893** entry for each cell in the r-tree node. Each entry is itself a
191894** list, containing the 8-byte rowid/pageno followed by the
191895** <num-dimension>*2 coordinates.
191896*/
191897static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
191898 RtreeNode node;
191899 Rtree tree;
191900 int ii;
191901 int nData;
191902 int errCode;
191903 sqlite3_str *pOut;
191904
191905 UNUSED_PARAMETER(nArg);
191906 memset(&node, 0, sizeof(RtreeNode));
191907 memset(&tree, 0, sizeof(Rtree));
191908 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
191909 if( tree.nDim<1 || tree.nDim>5 ) return;
191910 tree.nDim2 = tree.nDim*2;
191911 tree.nBytesPerCell = 8 + 8 * tree.nDim;
191912 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
191913 nData = sqlite3_value_bytes(apArg[1]);
191914 if( nData<4 ) return;
191915 if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
191916
191917 pOut = sqlite3_str_new(0);
191918 for(ii=0; ii<NCELL(&node); ii++){
191919 RtreeCell cell;
191920 int jj;
191921
191922 nodeGetCell(&tree, &node, ii, &cell);
191923 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
191924 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
191925 for(jj=0; jj<tree.nDim2; jj++){
191926#ifndef SQLITE_RTREE_INT_ONLY
191927 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
191928#else
191929 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
191930#endif
191931 }
191932 sqlite3_str_append(pOut, "}", 1);
191933 }
191934 errCode = sqlite3_str_errcode(pOut);
191935 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
191936 sqlite3_result_error_code(ctx, errCode);
191937}
191938
191939/* This routine implements an SQL function that returns the "depth" parameter
191940** from the front of a blob that is an r-tree node. For example:
191941**
191942** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
191943**
191944** The depth value is 0 for all nodes other than the root node, and the root
191945** node always has nodeno=1, so the example above is the primary use for this
191946** routine. This routine is intended for testing and analysis only.
191947*/
191948static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
191949 UNUSED_PARAMETER(nArg);
191950 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
191951 || sqlite3_value_bytes(apArg[0])<2
191952 ){
191953 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
191954 }else{
191955 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
191956 sqlite3_result_int(ctx, readInt16(zBlob));
191957 }
191958}
191959
191960/*
191961** Context object passed between the various routines that make up the
191962** implementation of integrity-check function rtreecheck().
191963*/
191964typedef struct RtreeCheck RtreeCheck;
191965struct RtreeCheck {
191966 sqlite3 *db; /* Database handle */
191967 const char *zDb; /* Database containing rtree table */
191968 const char *zTab; /* Name of rtree table */
191969 int bInt; /* True for rtree_i32 table */
191970 int nDim; /* Number of dimensions for this rtree tbl */
191971 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
191972 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
191973 int nLeaf; /* Number of leaf cells in table */
191974 int nNonLeaf; /* Number of non-leaf cells in table */
191975 int rc; /* Return code */
191976 char *zReport; /* Message to report */
191977 int nErr; /* Number of lines in zReport */
191978};
191979
191980#define RTREE_CHECK_MAX_ERROR 100
191981
191982/*
191983** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
191984** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
191985*/
191986static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
191987 int rc = sqlite3_reset(pStmt);
191988 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
191989}
191990
191991/*
191992** The second and subsequent arguments to this function are a format string
191993** and printf style arguments. This function formats the string and attempts
191994** to compile it as an SQL statement.
191995**
191996** If successful, a pointer to the new SQL statement is returned. Otherwise,
191997** NULL is returned and an error code left in RtreeCheck.rc.
191998*/
191999static sqlite3_stmt *rtreeCheckPrepare(
192000 RtreeCheck *pCheck, /* RtreeCheck object */
192001 const char *zFmt, ... /* Format string and trailing args */
192002){
192003 va_list ap;
192004 char *z;
192005 sqlite3_stmt *pRet = 0;
192006
192007 va_start(ap, zFmt);
192008 z = sqlite3_vmprintf(zFmt, ap);
192009
192010 if( pCheck->rc==SQLITE_OK ){
192011 if( z==0 ){
192012 pCheck->rc = SQLITE_NOMEM;
192013 }else{
192014 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
192015 }
192016 }
192017
192018 sqlite3_free(z);
192019 va_end(ap);
192020 return pRet;
192021}
192022
192023/*
192024** The second and subsequent arguments to this function are a printf()
192025** style format string and arguments. This function formats the string and
192026** appends it to the report being accumuated in pCheck.
192027*/
192028static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
192029 va_list ap;
192030 va_start(ap, zFmt);
192031 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
192032 char *z = sqlite3_vmprintf(zFmt, ap);
192033 if( z==0 ){
192034 pCheck->rc = SQLITE_NOMEM;
192035 }else{
192036 pCheck->zReport = sqlite3_mprintf("%z%s%z",
192037 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
192038 );
192039 if( pCheck->zReport==0 ){
192040 pCheck->rc = SQLITE_NOMEM;
192041 }
192042 }
192043 pCheck->nErr++;
192044 }
192045 va_end(ap);
192046}
192047
192048/*
192049** This function is a no-op if there is already an error code stored
192050** in the RtreeCheck object indicated by the first argument. NULL is
192051** returned in this case.
192052**
192053** Otherwise, the contents of rtree table node iNode are loaded from
192054** the database and copied into a buffer obtained from sqlite3_malloc().
192055** If no error occurs, a pointer to the buffer is returned and (*pnNode)
192056** is set to the size of the buffer in bytes.
192057**
192058** Or, if an error does occur, NULL is returned and an error code left
192059** in the RtreeCheck object. The final value of *pnNode is undefined in
192060** this case.
192061*/
192062static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
192063 u8 *pRet = 0; /* Return value */
192064
192065 if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
192066 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
192067 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
192068 pCheck->zDb, pCheck->zTab
192069 );
192070 }
192071
192072 if( pCheck->rc==SQLITE_OK ){
192073 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
192074 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
192075 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
192076 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
192077 pRet = sqlite3_malloc64(nNode);
192078 if( pRet==0 ){
192079 pCheck->rc = SQLITE_NOMEM;
192080 }else{
192081 memcpy(pRet, pNode, nNode);
192082 *pnNode = nNode;
192083 }
192084 }
192085 rtreeCheckReset(pCheck, pCheck->pGetNode);
192086 if( pCheck->rc==SQLITE_OK && pRet==0 ){
192087 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
192088 }
192089 }
192090
192091 return pRet;
192092}
192093
192094/*
192095** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
192096** (if bLeaf==1) table contains a specified entry. The schemas of the
192097** two tables are:
192098**
192099** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
192100** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
192101**
192102** In both cases, this function checks that there exists an entry with
192103** IPK value iKey and the second column set to iVal.
192104**
192105*/
192106static void rtreeCheckMapping(
192107 RtreeCheck *pCheck, /* RtreeCheck object */
192108 int bLeaf, /* True for a leaf cell, false for interior */
192109 i64 iKey, /* Key for mapping */
192110 i64 iVal /* Expected value for mapping */
192111){
192112 int rc;
192113 sqlite3_stmt *pStmt;
192114 const char *azSql[2] = {
192115 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
192116 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
192117 };
192118
192119 assert( bLeaf==0 || bLeaf==1 );
192120 if( pCheck->aCheckMapping[bLeaf]==0 ){
192121 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
192122 azSql[bLeaf], pCheck->zDb, pCheck->zTab
192123 );
192124 }
192125 if( pCheck->rc!=SQLITE_OK ) return;
192126
192127 pStmt = pCheck->aCheckMapping[bLeaf];
192128 sqlite3_bind_int64(pStmt, 1, iKey);
192129 rc = sqlite3_step(pStmt);
192130 if( rc==SQLITE_DONE ){
192131 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
192132 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
192133 );
192134 }else if( rc==SQLITE_ROW ){
192135 i64 ii = sqlite3_column_int64(pStmt, 0);
192136 if( ii!=iVal ){
192137 rtreeCheckAppendMsg(pCheck,
192138 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
192139 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
192140 );
192141 }
192142 }
192143 rtreeCheckReset(pCheck, pStmt);
192144}
192145
192146/*
192147** Argument pCell points to an array of coordinates stored on an rtree page.
192148** This function checks that the coordinates are internally consistent (no
192149** x1>x2 conditions) and adds an error message to the RtreeCheck object
192150** if they are not.
192151**
192152** Additionally, if pParent is not NULL, then it is assumed to point to
192153** the array of coordinates on the parent page that bound the page
192154** containing pCell. In this case it is also verified that the two
192155** sets of coordinates are mutually consistent and an error message added
192156** to the RtreeCheck object if they are not.
192157*/
192158static void rtreeCheckCellCoord(
192159 RtreeCheck *pCheck,
192160 i64 iNode, /* Node id to use in error messages */
192161 int iCell, /* Cell number to use in error messages */
192162 u8 *pCell, /* Pointer to cell coordinates */
192163 u8 *pParent /* Pointer to parent coordinates */
192164){
192165 RtreeCoord c1, c2;
192166 RtreeCoord p1, p2;
192167 int i;
192168
192169 for(i=0; i<pCheck->nDim; i++){
192170 readCoord(&pCell[4*2*i], &c1);
192171 readCoord(&pCell[4*(2*i + 1)], &c2);
192172
192173 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
192174 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
192175 rtreeCheckAppendMsg(pCheck,
192176 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
192177 );
192178 }
192179
192180 if( pParent ){
192181 readCoord(&pParent[4*2*i], &p1);
192182 readCoord(&pParent[4*(2*i + 1)], &p2);
192183
192184 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
192185 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
192186 ){
192187 rtreeCheckAppendMsg(pCheck,
192188 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
192189 , i, iCell, iNode
192190 );
192191 }
192192 }
192193 }
192194}
192195
192196/*
192197** Run rtreecheck() checks on node iNode, which is at depth iDepth within
192198** the r-tree structure. Argument aParent points to the array of coordinates
192199** that bound node iNode on the parent node.
192200**
192201** If any problems are discovered, an error message is appended to the
192202** report accumulated in the RtreeCheck object.
192203*/
192204static void rtreeCheckNode(
192205 RtreeCheck *pCheck,
192206 int iDepth, /* Depth of iNode (0==leaf) */
192207 u8 *aParent, /* Buffer containing parent coords */
192208 i64 iNode /* Node to check */
192209){
192210 u8 *aNode = 0;
192211 int nNode = 0;
192212
192213 assert( iNode==1 || aParent!=0 );
192214 assert( pCheck->nDim>0 );
192215
192216 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
192217 if( aNode ){
192218 if( nNode<4 ){
192219 rtreeCheckAppendMsg(pCheck,
192220 "Node %lld is too small (%d bytes)", iNode, nNode
192221 );
192222 }else{
192223 int nCell; /* Number of cells on page */
192224 int i; /* Used to iterate through cells */
192225 if( aParent==0 ){
192226 iDepth = readInt16(aNode);
192227 if( iDepth>RTREE_MAX_DEPTH ){
192228 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
192229 sqlite3_free(aNode);
192230 return;
192231 }
192232 }
192233 nCell = readInt16(&aNode[2]);
192234 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
192235 rtreeCheckAppendMsg(pCheck,
192236 "Node %lld is too small for cell count of %d (%d bytes)",
192237 iNode, nCell, nNode
192238 );
192239 }else{
192240 for(i=0; i<nCell; i++){
192241 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
192242 i64 iVal = readInt64(pCell);
192243 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
192244
192245 if( iDepth>0 ){
192246 rtreeCheckMapping(pCheck, 0, iVal, iNode);
192247 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
192248 pCheck->nNonLeaf++;
192249 }else{
192250 rtreeCheckMapping(pCheck, 1, iVal, iNode);
192251 pCheck->nLeaf++;
192252 }
192253 }
192254 }
192255 }
192256 sqlite3_free(aNode);
192257 }
192258}
192259
192260/*
192261** The second argument to this function must be either "_rowid" or
192262** "_parent". This function checks that the number of entries in the
192263** %_rowid or %_parent table is exactly nExpect. If not, it adds
192264** an error message to the report in the RtreeCheck object indicated
192265** by the first argument.
192266*/
192267static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
192268 if( pCheck->rc==SQLITE_OK ){
192269 sqlite3_stmt *pCount;
192270 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
192271 pCheck->zDb, pCheck->zTab, zTbl
192272 );
192273 if( pCount ){
192274 if( sqlite3_step(pCount)==SQLITE_ROW ){
192275 i64 nActual = sqlite3_column_int64(pCount, 0);
192276 if( nActual!=nExpect ){
192277 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
192278 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
192279 );
192280 }
192281 }
192282 pCheck->rc = sqlite3_finalize(pCount);
192283 }
192284 }
192285}
192286
192287/*
192288** This function does the bulk of the work for the rtree integrity-check.
192289** It is called by rtreecheck(), which is the SQL function implementation.
192290*/
192291static int rtreeCheckTable(
192292 sqlite3 *db, /* Database handle to access db through */
192293 const char *zDb, /* Name of db ("main", "temp" etc.) */
192294 const char *zTab, /* Name of rtree table to check */
192295 char **pzReport /* OUT: sqlite3_malloc'd report text */
192296){
192297 RtreeCheck check; /* Common context for various routines */
192298 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
192299 int bEnd = 0; /* True if transaction should be closed */
192300 int nAux = 0; /* Number of extra columns. */
192301
192302 /* Initialize the context object */
192303 memset(&check, 0, sizeof(check));
192304 check.db = db;
192305 check.zDb = zDb;
192306 check.zTab = zTab;
192307
192308 /* If there is not already an open transaction, open one now. This is
192309 ** to ensure that the queries run as part of this integrity-check operate
192310 ** on a consistent snapshot. */
192311 if( sqlite3_get_autocommit(db) ){
192312 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
192313 bEnd = 1;
192314 }
192315
192316 /* Find the number of auxiliary columns */
192317 if( check.rc==SQLITE_OK ){
192318 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
192319 if( pStmt ){
192320 nAux = sqlite3_column_count(pStmt) - 2;
192321 sqlite3_finalize(pStmt);
192322 }
192323 check.rc = SQLITE_OK;
192324 }
192325
192326 /* Find number of dimensions in the rtree table. */
192327 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
192328 if( pStmt ){
192329 int rc;
192330 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
192331 if( check.nDim<1 ){
192332 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
192333 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
192334 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
192335 }
192336 rc = sqlite3_finalize(pStmt);
192337 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
192338 }
192339
192340 /* Do the actual integrity-check */
192341 if( check.nDim>=1 ){
192342 if( check.rc==SQLITE_OK ){
192343 rtreeCheckNode(&check, 0, 0, 1);
192344 }
192345 rtreeCheckCount(&check, "_rowid", check.nLeaf);
192346 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
192347 }
192348
192349 /* Finalize SQL statements used by the integrity-check */
192350 sqlite3_finalize(check.pGetNode);
192351 sqlite3_finalize(check.aCheckMapping[0]);
192352 sqlite3_finalize(check.aCheckMapping[1]);
192353
192354 /* If one was opened, close the transaction */
192355 if( bEnd ){
192356 int rc = sqlite3_exec(db, "END", 0, 0, 0);
192357 if( check.rc==SQLITE_OK ) check.rc = rc;
192358 }
192359 *pzReport = check.zReport;
192360 return check.rc;
192361}
192362
192363/*
192364** Usage:
192365**
192366** rtreecheck(<rtree-table>);
192367** rtreecheck(<database>, <rtree-table>);
192368**
192369** Invoking this SQL function runs an integrity-check on the named rtree
192370** table. The integrity-check verifies the following:
192371**
192372** 1. For each cell in the r-tree structure (%_node table), that:
192373**
192374** a) for each dimension, (coord1 <= coord2).
192375**
192376** b) unless the cell is on the root node, that the cell is bounded
192377** by the parent cell on the parent node.
192378**
192379** c) for leaf nodes, that there is an entry in the %_rowid
192380** table corresponding to the cell's rowid value that
192381** points to the correct node.
192382**
192383** d) for cells on non-leaf nodes, that there is an entry in the
192384** %_parent table mapping from the cell's child node to the
192385** node that it resides on.
192386**
192387** 2. That there are the same number of entries in the %_rowid table
192388** as there are leaf cells in the r-tree structure, and that there
192389** is a leaf cell that corresponds to each entry in the %_rowid table.
192390**
192391** 3. That there are the same number of entries in the %_parent table
192392** as there are non-leaf cells in the r-tree structure, and that
192393** there is a non-leaf cell that corresponds to each entry in the
192394** %_parent table.
192395*/
192396static void rtreecheck(
192397 sqlite3_context *ctx,
192398 int nArg,
192399 sqlite3_value **apArg
192400){
192401 if( nArg!=1 && nArg!=2 ){
192402 sqlite3_result_error(ctx,
192403 "wrong number of arguments to function rtreecheck()", -1
192404 );
192405 }else{
192406 int rc;
192407 char *zReport = 0;
192408 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
192409 const char *zTab;
192410 if( nArg==1 ){
192411 zTab = zDb;
192412 zDb = "main";
192413 }else{
192414 zTab = (const char*)sqlite3_value_text(apArg[1]);
192415 }
192416 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
192417 if( rc==SQLITE_OK ){
192418 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
192419 }else{
192420 sqlite3_result_error_code(ctx, rc);
192421 }
192422 sqlite3_free(zReport);
192423 }
192424}
192425
192426/* Conditionally include the geopoly code */
192427#ifdef SQLITE_ENABLE_GEOPOLY
192428/************** Include geopoly.c in the middle of rtree.c *******************/
192429/************** Begin file geopoly.c *****************************************/
192430/*
192431** 2018-05-25
192432**
192433** The author disclaims copyright to this source code. In place of
192434** a legal notice, here is a blessing:
192435**
192436** May you do good and not evil.
192437** May you find forgiveness for yourself and forgive others.
192438** May you share freely, never taking more than you give.
192439**
192440******************************************************************************
192441**
192442** This file implements an alternative R-Tree virtual table that
192443** uses polygons to express the boundaries of 2-dimensional objects.
192444**
192445** This file is #include-ed onto the end of "rtree.c" so that it has
192446** access to all of the R-Tree internals.
192447*/
192448/* #include <stdlib.h> */
192449
192450/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
192451#ifdef GEOPOLY_ENABLE_DEBUG
192452 static int geo_debug = 0;
192453# define GEODEBUG(X) if(geo_debug)printf X
192454#else
192455# define GEODEBUG(X)
192456#endif
192457
192458#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
192459/*
192460** Versions of isspace(), isalnum() and isdigit() to which it is safe
192461** to pass signed char values.
192462*/
192463#ifdef sqlite3Isdigit
192464 /* Use the SQLite core versions if this routine is part of the
192465 ** SQLite amalgamation */
192466# define safe_isdigit(x) sqlite3Isdigit(x)
192467# define safe_isalnum(x) sqlite3Isalnum(x)
192468# define safe_isxdigit(x) sqlite3Isxdigit(x)
192469#else
192470 /* Use the standard library for separate compilation */
192471#include <ctype.h> /* amalgamator: keep */
192472# define safe_isdigit(x) isdigit((unsigned char)(x))
192473# define safe_isalnum(x) isalnum((unsigned char)(x))
192474# define safe_isxdigit(x) isxdigit((unsigned char)(x))
192475#endif
192476
192477/*
192478** Growing our own isspace() routine this way is twice as fast as
192479** the library isspace() function.
192480*/
192481static const char geopolyIsSpace[] = {
192482 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
192483 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192484 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192485 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192486 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192487 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192488 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192489 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192498};
192499#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
192500#endif /* JSON NULL - back to original code */
192501
192502/* Compiler and version */
192503#ifndef GCC_VERSION
192504#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
192505# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
192506#else
192507# define GCC_VERSION 0
192508#endif
192509#endif
192510#ifndef MSVC_VERSION
192511#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
192512# define MSVC_VERSION _MSC_VER
192513#else
192514# define MSVC_VERSION 0
192515#endif
192516#endif
192517
192518/* Datatype for coordinates
192519*/
192520typedef float GeoCoord;
192521
192522/*
192523** Internal representation of a polygon.
192524**
192525** The polygon consists of a sequence of vertexes. There is a line
192526** segment between each pair of vertexes, and one final segment from
192527** the last vertex back to the first. (This differs from the GeoJSON
192528** standard in which the final vertex is a repeat of the first.)
192529**
192530** The polygon follows the right-hand rule. The area to the right of
192531** each segment is "outside" and the area to the left is "inside".
192532**
192533** The on-disk representation consists of a 4-byte header followed by
192534** the values. The 4-byte header is:
192535**
192536** encoding (1 byte) 0=big-endian, 1=little-endian
192537** nvertex (3 bytes) Number of vertexes as a big-endian integer
192538**
192539** Enough space is allocated for 4 coordinates, to work around over-zealous
192540** warnings coming from some compiler (notably, clang). In reality, the size
192541** of each GeoPoly memory allocate is adjusted as necessary so that the
192542** GeoPoly.a[] array at the end is the appropriate size.
192543*/
192544typedef struct GeoPoly GeoPoly;
192545struct GeoPoly {
192546 int nVertex; /* Number of vertexes */
192547 unsigned char hdr[4]; /* Header for on-disk representation */
192548 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
192549};
192550
192551/* The size of a memory allocation needed for a GeoPoly object sufficient
192552** to hold N coordinate pairs.
192553*/
192554#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
192555
192556/* Macros to access coordinates of a GeoPoly.
192557** We have to use these macros, rather than just say p->a[i] in order
192558** to silence (incorrect) UBSAN warnings if the array index is too large.
192559*/
192560#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
192561#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
192562
192563
192564/*
192565** State of a parse of a GeoJSON input.
192566*/
192567typedef struct GeoParse GeoParse;
192568struct GeoParse {
192569 const unsigned char *z; /* Unparsed input */
192570 int nVertex; /* Number of vertexes in a[] */
192571 int nAlloc; /* Space allocated to a[] */
192572 int nErr; /* Number of errors encountered */
192573 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
192574};
192575
192576/* Do a 4-byte byte swap */
192577static void geopolySwab32(unsigned char *a){
192578 unsigned char t = a[0];
192579 a[0] = a[3];
192580 a[3] = t;
192581 t = a[1];
192582 a[1] = a[2];
192583 a[2] = t;
192584}
192585
192586/* Skip whitespace. Return the next non-whitespace character. */
192587static char geopolySkipSpace(GeoParse *p){
192588 while( safe_isspace(p->z[0]) ) p->z++;
192589 return p->z[0];
192590}
192591
192592/* Parse out a number. Write the value into *pVal if pVal!=0.
192593** return non-zero on success and zero if the next token is not a number.
192594*/
192595static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
192596 char c = geopolySkipSpace(p);
192597 const unsigned char *z = p->z;
192598 int j = 0;
192599 int seenDP = 0;
192600 int seenE = 0;
192601 if( c=='-' ){
192602 j = 1;
192603 c = z[j];
192604 }
192605 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
192606 for(;; j++){
192607 c = z[j];
192608 if( safe_isdigit(c) ) continue;
192609 if( c=='.' ){
192610 if( z[j-1]=='-' ) return 0;
192611 if( seenDP ) return 0;
192612 seenDP = 1;
192613 continue;
192614 }
192615 if( c=='e' || c=='E' ){
192616 if( z[j-1]<'0' ) return 0;
192617 if( seenE ) return -1;
192618 seenDP = seenE = 1;
192619 c = z[j+1];
192620 if( c=='+' || c=='-' ){
192621 j++;
192622 c = z[j+1];
192623 }
192624 if( c<'0' || c>'9' ) return 0;
192625 continue;
192626 }
192627 break;
192628 }
192629 if( z[j-1]<'0' ) return 0;
192630 if( pVal ){
192631#ifdef SQLITE_AMALGAMATION
192632 /* The sqlite3AtoF() routine is much much faster than atof(), if it
192633 ** is available */
192634 double r;
192635 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
192636 *pVal = r;
192637#else
192638 *pVal = (GeoCoord)atof((const char*)p->z);
192639#endif
192640 }
192641 p->z += j;
192642 return 1;
192643}
192644
192645/*
192646** If the input is a well-formed JSON array of coordinates with at least
192647** four coordinates and where each coordinate is itself a two-value array,
192648** then convert the JSON into a GeoPoly object and return a pointer to
192649** that object.
192650**
192651** If any error occurs, return NULL.
192652*/
192653static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
192654 GeoParse s;
192655 int rc = SQLITE_OK;
192656 memset(&s, 0, sizeof(s));
192657 s.z = z;
192658 if( geopolySkipSpace(&s)=='[' ){
192659 s.z++;
192660 while( geopolySkipSpace(&s)=='[' ){
192661 int ii = 0;
192662 char c;
192663 s.z++;
192664 if( s.nVertex>=s.nAlloc ){
192665 GeoCoord *aNew;
192666 s.nAlloc = s.nAlloc*2 + 16;
192667 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
192668 if( aNew==0 ){
192669 rc = SQLITE_NOMEM;
192670 s.nErr++;
192671 break;
192672 }
192673 s.a = aNew;
192674 }
192675 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
192676 ii++;
192677 if( ii==2 ) s.nVertex++;
192678 c = geopolySkipSpace(&s);
192679 s.z++;
192680 if( c==',' ) continue;
192681 if( c==']' && ii>=2 ) break;
192682 s.nErr++;
192683 rc = SQLITE_ERROR;
192684 goto parse_json_err;
192685 }
192686 if( geopolySkipSpace(&s)==',' ){
192687 s.z++;
192688 continue;
192689 }
192690 break;
192691 }
192692 if( geopolySkipSpace(&s)==']'
192693 && s.nVertex>=4
192694 && s.a[0]==s.a[s.nVertex*2-2]
192695 && s.a[1]==s.a[s.nVertex*2-1]
192696 && (s.z++, geopolySkipSpace(&s)==0)
192697 ){
192698 GeoPoly *pOut;
192699 int x = 1;
192700 s.nVertex--; /* Remove the redundant vertex at the end */
192701 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
192702 x = 1;
192703 if( pOut==0 ) goto parse_json_err;
192704 pOut->nVertex = s.nVertex;
192705 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
192706 pOut->hdr[0] = *(unsigned char*)&x;
192707 pOut->hdr[1] = (s.nVertex>>16)&0xff;
192708 pOut->hdr[2] = (s.nVertex>>8)&0xff;
192709 pOut->hdr[3] = s.nVertex&0xff;
192710 sqlite3_free(s.a);
192711 if( pRc ) *pRc = SQLITE_OK;
192712 return pOut;
192713 }else{
192714 s.nErr++;
192715 rc = SQLITE_ERROR;
192716 }
192717 }
192718parse_json_err:
192719 if( pRc ) *pRc = rc;
192720 sqlite3_free(s.a);
192721 return 0;
192722}
192723
192724/*
192725** Given a function parameter, try to interpret it as a polygon, either
192726** in the binary format or JSON text. Compute a GeoPoly object and
192727** return a pointer to that object. Or if the input is not a well-formed
192728** polygon, put an error message in sqlite3_context and return NULL.
192729*/
192730static GeoPoly *geopolyFuncParam(
192731 sqlite3_context *pCtx, /* Context for error messages */
192732 sqlite3_value *pVal, /* The value to decode */
192733 int *pRc /* Write error here */
192734){
192735 GeoPoly *p = 0;
192736 int nByte;
192737 if( sqlite3_value_type(pVal)==SQLITE_BLOB
192738 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
192739 ){
192740 const unsigned char *a = sqlite3_value_blob(pVal);
192741 int nVertex;
192742 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
192743 if( (a[0]==0 || a[0]==1)
192744 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
192745 ){
192746 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
192747 if( p==0 ){
192748 if( pRc ) *pRc = SQLITE_NOMEM;
192749 if( pCtx ) sqlite3_result_error_nomem(pCtx);
192750 }else{
192751 int x = 1;
192752 p->nVertex = nVertex;
192753 memcpy(p->hdr, a, nByte);
192754 if( a[0] != *(unsigned char*)&x ){
192755 int ii;
192756 for(ii=0; ii<nVertex; ii++){
192757 geopolySwab32((unsigned char*)&GeoX(p,ii));
192758 geopolySwab32((unsigned char*)&GeoY(p,ii));
192759 }
192760 p->hdr[0] ^= 1;
192761 }
192762 }
192763 }
192764 if( pRc ) *pRc = SQLITE_OK;
192765 return p;
192766 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
192767 const unsigned char *zJson = sqlite3_value_text(pVal);
192768 if( zJson==0 ){
192769 if( pRc ) *pRc = SQLITE_NOMEM;
192770 return 0;
192771 }
192772 return geopolyParseJson(zJson, pRc);
192773 }else{
192774 if( pRc ) *pRc = SQLITE_ERROR;
192775 return 0;
192776 }
192777}
192778
192779/*
192780** Implementation of the geopoly_blob(X) function.
192781**
192782** If the input is a well-formed Geopoly BLOB or JSON string
192783** then return the BLOB representation of the polygon. Otherwise
192784** return NULL.
192785*/
192786static void geopolyBlobFunc(
192787 sqlite3_context *context,
192788 int argc,
192789 sqlite3_value **argv
192790){
192791 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192792 if( p ){
192793 sqlite3_result_blob(context, p->hdr,
192794 4+8*p->nVertex, SQLITE_TRANSIENT);
192795 sqlite3_free(p);
192796 }
192797}
192798
192799/*
192800** SQL function: geopoly_json(X)
192801**
192802** Interpret X as a polygon and render it as a JSON array
192803** of coordinates. Or, if X is not a valid polygon, return NULL.
192804*/
192805static void geopolyJsonFunc(
192806 sqlite3_context *context,
192807 int argc,
192808 sqlite3_value **argv
192809){
192810 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192811 if( p ){
192812 sqlite3 *db = sqlite3_context_db_handle(context);
192813 sqlite3_str *x = sqlite3_str_new(db);
192814 int i;
192815 sqlite3_str_append(x, "[", 1);
192816 for(i=0; i<p->nVertex; i++){
192817 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
192818 }
192819 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
192820 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
192821 sqlite3_free(p);
192822 }
192823}
192824
192825/*
192826** SQL function: geopoly_svg(X, ....)
192827**
192828** Interpret X as a polygon and render it as a SVG <polyline>.
192829** Additional arguments are added as attributes to the <polyline>.
192830*/
192831static void geopolySvgFunc(
192832 sqlite3_context *context,
192833 int argc,
192834 sqlite3_value **argv
192835){
192836 GeoPoly *p;
192837 if( argc<1 ) return;
192838 p = geopolyFuncParam(context, argv[0], 0);
192839 if( p ){
192840 sqlite3 *db = sqlite3_context_db_handle(context);
192841 sqlite3_str *x = sqlite3_str_new(db);
192842 int i;
192843 char cSep = '\'';
192844 sqlite3_str_appendf(x, "<polyline points=");
192845 for(i=0; i<p->nVertex; i++){
192846 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
192847 cSep = ' ';
192848 }
192849 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
192850 for(i=1; i<argc; i++){
192851 const char *z = (const char*)sqlite3_value_text(argv[i]);
192852 if( z && z[0] ){
192853 sqlite3_str_appendf(x, " %s", z);
192854 }
192855 }
192856 sqlite3_str_appendf(x, "></polyline>");
192857 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
192858 sqlite3_free(p);
192859 }
192860}
192861
192862/*
192863** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
192864**
192865** Transform and/or translate a polygon as follows:
192866**
192867** x1 = A*x0 + B*y0 + E
192868** y1 = C*x0 + D*y0 + F
192869**
192870** For a translation:
192871**
192872** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
192873**
192874** Rotate by R around the point (0,0):
192875**
192876** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
192877*/
192878static void geopolyXformFunc(
192879 sqlite3_context *context,
192880 int argc,
192881 sqlite3_value **argv
192882){
192883 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192884 double A = sqlite3_value_double(argv[1]);
192885 double B = sqlite3_value_double(argv[2]);
192886 double C = sqlite3_value_double(argv[3]);
192887 double D = sqlite3_value_double(argv[4]);
192888 double E = sqlite3_value_double(argv[5]);
192889 double F = sqlite3_value_double(argv[6]);
192890 GeoCoord x1, y1, x0, y0;
192891 int ii;
192892 if( p ){
192893 for(ii=0; ii<p->nVertex; ii++){
192894 x0 = GeoX(p,ii);
192895 y0 = GeoY(p,ii);
192896 x1 = (GeoCoord)(A*x0 + B*y0 + E);
192897 y1 = (GeoCoord)(C*x0 + D*y0 + F);
192898 GeoX(p,ii) = x1;
192899 GeoY(p,ii) = y1;
192900 }
192901 sqlite3_result_blob(context, p->hdr,
192902 4+8*p->nVertex, SQLITE_TRANSIENT);
192903 sqlite3_free(p);
192904 }
192905}
192906
192907/*
192908** Compute the area enclosed by the polygon.
192909**
192910** This routine can also be used to detect polygons that rotate in
192911** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
192912** This routine returns a negative value for clockwise (CW) polygons.
192913*/
192914static double geopolyArea(GeoPoly *p){
192915 double rArea = 0.0;
192916 int ii;
192917 for(ii=0; ii<p->nVertex-1; ii++){
192918 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
192919 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
192920 * 0.5;
192921 }
192922 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
192923 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
192924 * 0.5;
192925 return rArea;
192926}
192927
192928/*
192929** Implementation of the geopoly_area(X) function.
192930**
192931** If the input is a well-formed Geopoly BLOB then return the area
192932** enclosed by the polygon. If the polygon circulates clockwise instead
192933** of counterclockwise (as it should) then return the negative of the
192934** enclosed area. Otherwise return NULL.
192935*/
192936static void geopolyAreaFunc(
192937 sqlite3_context *context,
192938 int argc,
192939 sqlite3_value **argv
192940){
192941 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192942 if( p ){
192943 sqlite3_result_double(context, geopolyArea(p));
192944 sqlite3_free(p);
192945 }
192946}
192947
192948/*
192949** Implementation of the geopoly_ccw(X) function.
192950**
192951** If the rotation of polygon X is clockwise (incorrect) instead of
192952** counter-clockwise (the correct winding order according to RFC7946)
192953** then reverse the order of the vertexes in polygon X.
192954**
192955** In other words, this routine returns a CCW polygon regardless of the
192956** winding order of its input.
192957**
192958** Use this routine to sanitize historical inputs that that sometimes
192959** contain polygons that wind in the wrong direction.
192960*/
192961static void geopolyCcwFunc(
192962 sqlite3_context *context,
192963 int argc,
192964 sqlite3_value **argv
192965){
192966 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
192967 if( p ){
192968 if( geopolyArea(p)<0.0 ){
192969 int ii, jj;
192970 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
192971 GeoCoord t = GeoX(p,ii);
192972 GeoX(p,ii) = GeoX(p,jj);
192973 GeoX(p,jj) = t;
192974 t = GeoY(p,ii);
192975 GeoY(p,ii) = GeoY(p,jj);
192976 GeoY(p,jj) = t;
192977 }
192978 }
192979 sqlite3_result_blob(context, p->hdr,
192980 4+8*p->nVertex, SQLITE_TRANSIENT);
192981 sqlite3_free(p);
192982 }
192983}
192984
192985#define GEOPOLY_PI 3.1415926535897932385
192986
192987/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
192988*/
192989static double geopolySine(double r){
192990 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
192991 if( r>=1.5*GEOPOLY_PI ){
192992 r -= 2.0*GEOPOLY_PI;
192993 }
192994 if( r>=0.5*GEOPOLY_PI ){
192995 return -geopolySine(r-GEOPOLY_PI);
192996 }else{
192997 double r2 = r*r;
192998 double r3 = r2*r;
192999 double r5 = r3*r2;
193000 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
193001 }
193002}
193003
193004/*
193005** Function: geopoly_regular(X,Y,R,N)
193006**
193007** Construct a simple, convex, regular polygon centered at X, Y
193008** with circumradius R and with N sides.
193009*/
193010static void geopolyRegularFunc(
193011 sqlite3_context *context,
193012 int argc,
193013 sqlite3_value **argv
193014){
193015 double x = sqlite3_value_double(argv[0]);
193016 double y = sqlite3_value_double(argv[1]);
193017 double r = sqlite3_value_double(argv[2]);
193018 int n = sqlite3_value_int(argv[3]);
193019 int i;
193020 GeoPoly *p;
193021
193022 if( n<3 || r<=0.0 ) return;
193023 if( n>1000 ) n = 1000;
193024 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
193025 if( p==0 ){
193026 sqlite3_result_error_nomem(context);
193027 return;
193028 }
193029 i = 1;
193030 p->hdr[0] = *(unsigned char*)&i;
193031 p->hdr[1] = 0;
193032 p->hdr[2] = (n>>8)&0xff;
193033 p->hdr[3] = n&0xff;
193034 for(i=0; i<n; i++){
193035 double rAngle = 2.0*GEOPOLY_PI*i/n;
193036 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
193037 GeoY(p,i) = y + r*geopolySine(rAngle);
193038 }
193039 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
193040 sqlite3_free(p);
193041}
193042
193043/*
193044** If pPoly is a polygon, compute its bounding box. Then:
193045**
193046** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
193047** (2) otherwise, compute a GeoPoly for the bounding box and return the
193048** new GeoPoly
193049**
193050** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
193051** the bounding box in aCoord and return a pointer to that GeoPoly.
193052*/
193053static GeoPoly *geopolyBBox(
193054 sqlite3_context *context, /* For recording the error */
193055 sqlite3_value *pPoly, /* The polygon */
193056 RtreeCoord *aCoord, /* Results here */
193057 int *pRc /* Error code here */
193058){
193059 GeoPoly *pOut = 0;
193060 GeoPoly *p;
193061 float mnX, mxX, mnY, mxY;
193062 if( pPoly==0 && aCoord!=0 ){
193063 p = 0;
193064 mnX = aCoord[0].f;
193065 mxX = aCoord[1].f;
193066 mnY = aCoord[2].f;
193067 mxY = aCoord[3].f;
193068 goto geopolyBboxFill;
193069 }else{
193070 p = geopolyFuncParam(context, pPoly, pRc);
193071 }
193072 if( p ){
193073 int ii;
193074 mnX = mxX = GeoX(p,0);
193075 mnY = mxY = GeoY(p,0);
193076 for(ii=1; ii<p->nVertex; ii++){
193077 double r = GeoX(p,ii);
193078 if( r<mnX ) mnX = (float)r;
193079 else if( r>mxX ) mxX = (float)r;
193080 r = GeoY(p,ii);
193081 if( r<mnY ) mnY = (float)r;
193082 else if( r>mxY ) mxY = (float)r;
193083 }
193084 if( pRc ) *pRc = SQLITE_OK;
193085 if( aCoord==0 ){
193086 geopolyBboxFill:
193087 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
193088 if( pOut==0 ){
193089 sqlite3_free(p);
193090 if( context ) sqlite3_result_error_nomem(context);
193091 if( pRc ) *pRc = SQLITE_NOMEM;
193092 return 0;
193093 }
193094 pOut->nVertex = 4;
193095 ii = 1;
193096 pOut->hdr[0] = *(unsigned char*)&ii;
193097 pOut->hdr[1] = 0;
193098 pOut->hdr[2] = 0;
193099 pOut->hdr[3] = 4;
193100 GeoX(pOut,0) = mnX;
193101 GeoY(pOut,0) = mnY;
193102 GeoX(pOut,1) = mxX;
193103 GeoY(pOut,1) = mnY;
193104 GeoX(pOut,2) = mxX;
193105 GeoY(pOut,2) = mxY;
193106 GeoX(pOut,3) = mnX;
193107 GeoY(pOut,3) = mxY;
193108 }else{
193109 sqlite3_free(p);
193110 aCoord[0].f = mnX;
193111 aCoord[1].f = mxX;
193112 aCoord[2].f = mnY;
193113 aCoord[3].f = mxY;
193114 }
193115 }else{
193116 memset(aCoord, 0, sizeof(RtreeCoord)*4);
193117 }
193118 return pOut;
193119}
193120
193121/*
193122** Implementation of the geopoly_bbox(X) SQL function.
193123*/
193124static void geopolyBBoxFunc(
193125 sqlite3_context *context,
193126 int argc,
193127 sqlite3_value **argv
193128){
193129 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
193130 if( p ){
193131 sqlite3_result_blob(context, p->hdr,
193132 4+8*p->nVertex, SQLITE_TRANSIENT);
193133 sqlite3_free(p);
193134 }
193135}
193136
193137/*
193138** State vector for the geopoly_group_bbox() aggregate function.
193139*/
193140typedef struct GeoBBox GeoBBox;
193141struct GeoBBox {
193142 int isInit;
193143 RtreeCoord a[4];
193144};
193145
193146
193147/*
193148** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
193149*/
193150static void geopolyBBoxStep(
193151 sqlite3_context *context,
193152 int argc,
193153 sqlite3_value **argv
193154){
193155 RtreeCoord a[4];
193156 int rc = SQLITE_OK;
193157 (void)geopolyBBox(context, argv[0], a, &rc);
193158 if( rc==SQLITE_OK ){
193159 GeoBBox *pBBox;
193160 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
193161 if( pBBox==0 ) return;
193162 if( pBBox->isInit==0 ){
193163 pBBox->isInit = 1;
193164 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
193165 }else{
193166 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
193167 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
193168 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
193169 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
193170 }
193171 }
193172}
193173static void geopolyBBoxFinal(
193174 sqlite3_context *context
193175){
193176 GeoPoly *p;
193177 GeoBBox *pBBox;
193178 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
193179 if( pBBox==0 ) return;
193180 p = geopolyBBox(context, 0, pBBox->a, 0);
193181 if( p ){
193182 sqlite3_result_blob(context, p->hdr,
193183 4+8*p->nVertex, SQLITE_TRANSIENT);
193184 sqlite3_free(p);
193185 }
193186}
193187
193188
193189/*
193190** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
193191** Returns:
193192**
193193** +2 x0,y0 is on the line segement
193194**
193195** +1 x0,y0 is beneath line segment
193196**
193197** 0 x0,y0 is not on or beneath the line segment or the line segment
193198** is vertical and x0,y0 is not on the line segment
193199**
193200** The left-most coordinate min(x1,x2) is not considered to be part of
193201** the line segment for the purposes of this analysis.
193202*/
193203static int pointBeneathLine(
193204 double x0, double y0,
193205 double x1, double y1,
193206 double x2, double y2
193207){
193208 double y;
193209 if( x0==x1 && y0==y1 ) return 2;
193210 if( x1<x2 ){
193211 if( x0<=x1 || x0>x2 ) return 0;
193212 }else if( x1>x2 ){
193213 if( x0<=x2 || x0>x1 ) return 0;
193214 }else{
193215 /* Vertical line segment */
193216 if( x0!=x1 ) return 0;
193217 if( y0<y1 && y0<y2 ) return 0;
193218 if( y0>y1 && y0>y2 ) return 0;
193219 return 2;
193220 }
193221 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
193222 if( y0==y ) return 2;
193223 if( y0<y ) return 1;
193224 return 0;
193225}
193226
193227/*
193228** SQL function: geopoly_contains_point(P,X,Y)
193229**
193230** Return +2 if point X,Y is within polygon P.
193231** Return +1 if point X,Y is on the polygon boundary.
193232** Return 0 if point X,Y is outside the polygon
193233*/
193234static void geopolyContainsPointFunc(
193235 sqlite3_context *context,
193236 int argc,
193237 sqlite3_value **argv
193238){
193239 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
193240 double x0 = sqlite3_value_double(argv[1]);
193241 double y0 = sqlite3_value_double(argv[2]);
193242 int v = 0;
193243 int cnt = 0;
193244 int ii;
193245 if( p1==0 ) return;
193246 for(ii=0; ii<p1->nVertex-1; ii++){
193247 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
193248 GeoX(p1,ii+1),GeoY(p1,ii+1));
193249 if( v==2 ) break;
193250 cnt += v;
193251 }
193252 if( v!=2 ){
193253 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
193254 GeoX(p1,0), GeoY(p1,0));
193255 }
193256 if( v==2 ){
193257 sqlite3_result_int(context, 1);
193258 }else if( ((v+cnt)&1)==0 ){
193259 sqlite3_result_int(context, 0);
193260 }else{
193261 sqlite3_result_int(context, 2);
193262 }
193263 sqlite3_free(p1);
193264}
193265
193266/* Forward declaration */
193267static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
193268
193269/*
193270** SQL function: geopoly_within(P1,P2)
193271**
193272** Return +2 if P1 and P2 are the same polygon
193273** Return +1 if P2 is contained within P1
193274** Return 0 if any part of P2 is on the outside of P1
193275**
193276*/
193277static void geopolyWithinFunc(
193278 sqlite3_context *context,
193279 int argc,
193280 sqlite3_value **argv
193281){
193282 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
193283 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
193284 if( p1 && p2 ){
193285 int x = geopolyOverlap(p1, p2);
193286 if( x<0 ){
193287 sqlite3_result_error_nomem(context);
193288 }else{
193289 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
193290 }
193291 }
193292 sqlite3_free(p1);
193293 sqlite3_free(p2);
193294}
193295
193296/* Objects used by the overlap algorihm. */
193297typedef struct GeoEvent GeoEvent;
193298typedef struct GeoSegment GeoSegment;
193299typedef struct GeoOverlap GeoOverlap;
193300struct GeoEvent {
193301 double x; /* X coordinate at which event occurs */
193302 int eType; /* 0 for ADD, 1 for REMOVE */
193303 GeoSegment *pSeg; /* The segment to be added or removed */
193304 GeoEvent *pNext; /* Next event in the sorted list */
193305};
193306struct GeoSegment {
193307 double C, B; /* y = C*x + B */
193308 double y; /* Current y value */
193309 float y0; /* Initial y value */
193310 unsigned char side; /* 1 for p1, 2 for p2 */
193311 unsigned int idx; /* Which segment within the side */
193312 GeoSegment *pNext; /* Next segment in a list sorted by y */
193313};
193314struct GeoOverlap {
193315 GeoEvent *aEvent; /* Array of all events */
193316 GeoSegment *aSegment; /* Array of all segments */
193317 int nEvent; /* Number of events */
193318 int nSegment; /* Number of segments */
193319};
193320
193321/*
193322** Add a single segment and its associated events.
193323*/
193324static void geopolyAddOneSegment(
193325 GeoOverlap *p,
193326 GeoCoord x0,
193327 GeoCoord y0,
193328 GeoCoord x1,
193329 GeoCoord y1,
193330 unsigned char side,
193331 unsigned int idx
193332){
193333 GeoSegment *pSeg;
193334 GeoEvent *pEvent;
193335 if( x0==x1 ) return; /* Ignore vertical segments */
193336 if( x0>x1 ){
193337 GeoCoord t = x0;
193338 x0 = x1;
193339 x1 = t;
193340 t = y0;
193341 y0 = y1;
193342 y1 = t;
193343 }
193344 pSeg = p->aSegment + p->nSegment;
193345 p->nSegment++;
193346 pSeg->C = (y1-y0)/(x1-x0);
193347 pSeg->B = y1 - x1*pSeg->C;
193348 pSeg->y0 = y0;
193349 pSeg->side = side;
193350 pSeg->idx = idx;
193351 pEvent = p->aEvent + p->nEvent;
193352 p->nEvent++;
193353 pEvent->x = x0;
193354 pEvent->eType = 0;
193355 pEvent->pSeg = pSeg;
193356 pEvent = p->aEvent + p->nEvent;
193357 p->nEvent++;
193358 pEvent->x = x1;
193359 pEvent->eType = 1;
193360 pEvent->pSeg = pSeg;
193361}
193362
193363
193364
193365/*
193366** Insert all segments and events for polygon pPoly.
193367*/
193368static void geopolyAddSegments(
193369 GeoOverlap *p, /* Add segments to this Overlap object */
193370 GeoPoly *pPoly, /* Take all segments from this polygon */
193371 unsigned char side /* The side of pPoly */
193372){
193373 unsigned int i;
193374 GeoCoord *x;
193375 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
193376 x = &GeoX(pPoly,i);
193377 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
193378 }
193379 x = &GeoX(pPoly,i);
193380 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
193381}
193382
193383/*
193384** Merge two lists of sorted events by X coordinate
193385*/
193386static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
193387 GeoEvent head, *pLast;
193388 head.pNext = 0;
193389 pLast = &head;
193390 while( pRight && pLeft ){
193391 if( pRight->x <= pLeft->x ){
193392 pLast->pNext = pRight;
193393 pLast = pRight;
193394 pRight = pRight->pNext;
193395 }else{
193396 pLast->pNext = pLeft;
193397 pLast = pLeft;
193398 pLeft = pLeft->pNext;
193399 }
193400 }
193401 pLast->pNext = pRight ? pRight : pLeft;
193402 return head.pNext;
193403}
193404
193405/*
193406** Sort an array of nEvent event objects into a list.
193407*/
193408static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
193409 int mx = 0;
193410 int i, j;
193411 GeoEvent *p;
193412 GeoEvent *a[50];
193413 for(i=0; i<nEvent; i++){
193414 p = &aEvent[i];
193415 p->pNext = 0;
193416 for(j=0; j<mx && a[j]; j++){
193417 p = geopolyEventMerge(a[j], p);
193418 a[j] = 0;
193419 }
193420 a[j] = p;
193421 if( j>=mx ) mx = j+1;
193422 }
193423 p = 0;
193424 for(i=0; i<mx; i++){
193425 p = geopolyEventMerge(a[i], p);
193426 }
193427 return p;
193428}
193429
193430/*
193431** Merge two lists of sorted segments by Y, and then by C.
193432*/
193433static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
193434 GeoSegment head, *pLast;
193435 head.pNext = 0;
193436 pLast = &head;
193437 while( pRight && pLeft ){
193438 double r = pRight->y - pLeft->y;
193439 if( r==0.0 ) r = pRight->C - pLeft->C;
193440 if( r<0.0 ){
193441 pLast->pNext = pRight;
193442 pLast = pRight;
193443 pRight = pRight->pNext;
193444 }else{
193445 pLast->pNext = pLeft;
193446 pLast = pLeft;
193447 pLeft = pLeft->pNext;
193448 }
193449 }
193450 pLast->pNext = pRight ? pRight : pLeft;
193451 return head.pNext;
193452}
193453
193454/*
193455** Sort a list of GeoSegments in order of increasing Y and in the event of
193456** a tie, increasing C (slope).
193457*/
193458static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
193459 int mx = 0;
193460 int i;
193461 GeoSegment *p;
193462 GeoSegment *a[50];
193463 while( pList ){
193464 p = pList;
193465 pList = pList->pNext;
193466 p->pNext = 0;
193467 for(i=0; i<mx && a[i]; i++){
193468 p = geopolySegmentMerge(a[i], p);
193469 a[i] = 0;
193470 }
193471 a[i] = p;
193472 if( i>=mx ) mx = i+1;
193473 }
193474 p = 0;
193475 for(i=0; i<mx; i++){
193476 p = geopolySegmentMerge(a[i], p);
193477 }
193478 return p;
193479}
193480
193481/*
193482** Determine the overlap between two polygons
193483*/
193484static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
193485 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
193486 GeoOverlap *p;
193487 sqlite3_int64 nByte;
193488 GeoEvent *pThisEvent;
193489 double rX;
193490 int rc = 0;
193491 int needSort = 0;
193492 GeoSegment *pActive = 0;
193493 GeoSegment *pSeg;
193494 unsigned char aOverlap[4];
193495
193496 nByte = sizeof(GeoEvent)*nVertex*2
193497 + sizeof(GeoSegment)*nVertex
193498 + sizeof(GeoOverlap);
193499 p = sqlite3_malloc64( nByte );
193500 if( p==0 ) return -1;
193501 p->aEvent = (GeoEvent*)&p[1];
193502 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
193503 p->nEvent = p->nSegment = 0;
193504 geopolyAddSegments(p, p1, 1);
193505 geopolyAddSegments(p, p2, 2);
193506 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
193507 rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
193508 memset(aOverlap, 0, sizeof(aOverlap));
193509 while( pThisEvent ){
193510 if( pThisEvent->x!=rX ){
193511 GeoSegment *pPrev = 0;
193512 int iMask = 0;
193513 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
193514 rX = pThisEvent->x;
193515 if( needSort ){
193516 GEODEBUG(("SORT\n"));
193517 pActive = geopolySortSegmentsByYAndC(pActive);
193518 needSort = 0;
193519 }
193520 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
193521 if( pPrev ){
193522 if( pPrev->y!=pSeg->y ){
193523 GEODEBUG(("MASK: %d\n", iMask));
193524 aOverlap[iMask] = 1;
193525 }
193526 }
193527 iMask ^= pSeg->side;
193528 pPrev = pSeg;
193529 }
193530 pPrev = 0;
193531 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
193532 double y = pSeg->C*rX + pSeg->B;
193533 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
193534 pSeg->y = y;
193535 if( pPrev ){
193536 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
193537 rc = 1;
193538 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
193539 pPrev->side, pPrev->idx,
193540 pSeg->side, pSeg->idx));
193541 goto geopolyOverlapDone;
193542 }else if( pPrev->y!=pSeg->y ){
193543 GEODEBUG(("MASK: %d\n", iMask));
193544 aOverlap[iMask] = 1;
193545 }
193546 }
193547 iMask ^= pSeg->side;
193548 pPrev = pSeg;
193549 }
193550 }
193551 GEODEBUG(("%s %d.%d C=%g B=%g\n",
193552 pThisEvent->eType ? "RM " : "ADD",
193553 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
193554 pThisEvent->pSeg->C,
193555 pThisEvent->pSeg->B));
193556 if( pThisEvent->eType==0 ){
193557 /* Add a segment */
193558 pSeg = pThisEvent->pSeg;
193559 pSeg->y = pSeg->y0;
193560 pSeg->pNext = pActive;
193561 pActive = pSeg;
193562 needSort = 1;
193563 }else{
193564 /* Remove a segment */
193565 if( pActive==pThisEvent->pSeg ){
193566 pActive = pActive->pNext;
193567 }else{
193568 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
193569 if( pSeg->pNext==pThisEvent->pSeg ){
193570 pSeg->pNext = pSeg->pNext->pNext;
193571 break;
193572 }
193573 }
193574 }
193575 }
193576 pThisEvent = pThisEvent->pNext;
193577 }
193578 if( aOverlap[3]==0 ){
193579 rc = 0;
193580 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
193581 rc = 3;
193582 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
193583 rc = 2;
193584 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
193585 rc = 4;
193586 }else{
193587 rc = 1;
193588 }
193589
193590geopolyOverlapDone:
193591 sqlite3_free(p);
193592 return rc;
193593}
193594
193595/*
193596** SQL function: geopoly_overlap(P1,P2)
193597**
193598** Determine whether or not P1 and P2 overlap. Return value:
193599**
193600** 0 The two polygons are disjoint
193601** 1 They overlap
193602** 2 P1 is completely contained within P2
193603** 3 P2 is completely contained within P1
193604** 4 P1 and P2 are the same polygon
193605** NULL Either P1 or P2 or both are not valid polygons
193606*/
193607static void geopolyOverlapFunc(
193608 sqlite3_context *context,
193609 int argc,
193610 sqlite3_value **argv
193611){
193612 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
193613 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
193614 if( p1 && p2 ){
193615 int x = geopolyOverlap(p1, p2);
193616 if( x<0 ){
193617 sqlite3_result_error_nomem(context);
193618 }else{
193619 sqlite3_result_int(context, x);
193620 }
193621 }
193622 sqlite3_free(p1);
193623 sqlite3_free(p2);
193624}
193625
193626/*
193627** Enable or disable debugging output
193628*/
193629static void geopolyDebugFunc(
193630 sqlite3_context *context,
193631 int argc,
193632 sqlite3_value **argv
193633){
193634#ifdef GEOPOLY_ENABLE_DEBUG
193635 geo_debug = sqlite3_value_int(argv[0]);
193636#endif
193637}
193638
193639/*
193640** This function is the implementation of both the xConnect and xCreate
193641** methods of the geopoly virtual table.
193642**
193643** argv[0] -> module name
193644** argv[1] -> database name
193645** argv[2] -> table name
193646** argv[...] -> column names...
193647*/
193648static int geopolyInit(
193649 sqlite3 *db, /* Database connection */
193650 void *pAux, /* One of the RTREE_COORD_* constants */
193651 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
193652 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
193653 char **pzErr, /* OUT: Error message, if any */
193654 int isCreate /* True for xCreate, false for xConnect */
193655){
193656 int rc = SQLITE_OK;
193657 Rtree *pRtree;
193658 sqlite3_int64 nDb; /* Length of string argv[1] */
193659 sqlite3_int64 nName; /* Length of string argv[2] */
193660 sqlite3_str *pSql;
193661 char *zSql;
193662 int ii;
193663
193664 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
193665
193666 /* Allocate the sqlite3_vtab structure */
193667 nDb = strlen(argv[1]);
193668 nName = strlen(argv[2]);
193669 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
193670 if( !pRtree ){
193671 return SQLITE_NOMEM;
193672 }
193673 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
193674 pRtree->nBusy = 1;
193675 pRtree->base.pModule = &rtreeModule;
193676 pRtree->zDb = (char *)&pRtree[1];
193677 pRtree->zName = &pRtree->zDb[nDb+1];
193678 pRtree->eCoordType = RTREE_COORD_REAL32;
193679 pRtree->nDim = 2;
193680 pRtree->nDim2 = 4;
193681 memcpy(pRtree->zDb, argv[1], nDb);
193682 memcpy(pRtree->zName, argv[2], nName);
193683
193684
193685 /* Create/Connect to the underlying relational database schema. If
193686 ** that is successful, call sqlite3_declare_vtab() to configure
193687 ** the r-tree table schema.
193688 */
193689 pSql = sqlite3_str_new(db);
193690 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
193691 pRtree->nAux = 1; /* Add one for _shape */
193692 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
193693 for(ii=3; ii<argc; ii++){
193694 pRtree->nAux++;
193695 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
193696 }
193697 sqlite3_str_appendf(pSql, ");");
193698 zSql = sqlite3_str_finish(pSql);
193699 if( !zSql ){
193700 rc = SQLITE_NOMEM;
193701 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
193702 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
193703 }
193704 sqlite3_free(zSql);
193705 if( rc ) goto geopolyInit_fail;
193706 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
193707
193708 /* Figure out the node size to use. */
193709 rc = getNodeSize(db, pRtree, isCreate, pzErr);
193710 if( rc ) goto geopolyInit_fail;
193711 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
193712 if( rc ){
193713 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
193714 goto geopolyInit_fail;
193715 }
193716
193717 *ppVtab = (sqlite3_vtab *)pRtree;
193718 return SQLITE_OK;
193719
193720geopolyInit_fail:
193721 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
193722 assert( *ppVtab==0 );
193723 assert( pRtree->nBusy==1 );
193724 rtreeRelease(pRtree);
193725 return rc;
193726}
193727
193728
193729/*
193730** GEOPOLY virtual table module xCreate method.
193731*/
193732static int geopolyCreate(
193733 sqlite3 *db,
193734 void *pAux,
193735 int argc, const char *const*argv,
193736 sqlite3_vtab **ppVtab,
193737 char **pzErr
193738){
193739 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
193740}
193741
193742/*
193743** GEOPOLY virtual table module xConnect method.
193744*/
193745static int geopolyConnect(
193746 sqlite3 *db,
193747 void *pAux,
193748 int argc, const char *const*argv,
193749 sqlite3_vtab **ppVtab,
193750 char **pzErr
193751){
193752 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
193753}
193754
193755
193756/*
193757** GEOPOLY virtual table module xFilter method.
193758**
193759** Query plans:
193760**
193761** 1 rowid lookup
193762** 2 search for objects overlapping the same bounding box
193763** that contains polygon argv[0]
193764** 3 search for objects overlapping the same bounding box
193765** that contains polygon argv[0]
193766** 4 full table scan
193767*/
193768static int geopolyFilter(
193769 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
193770 int idxNum, /* Query plan */
193771 const char *idxStr, /* Not Used */
193772 int argc, sqlite3_value **argv /* Parameters to the query plan */
193773){
193774 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
193775 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
193776 RtreeNode *pRoot = 0;
193777 int rc = SQLITE_OK;
193778 int iCell = 0;
193779
193780 rtreeReference(pRtree);
193781
193782 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
193783 resetCursor(pCsr);
193784
193785 pCsr->iStrategy = idxNum;
193786 if( idxNum==1 ){
193787 /* Special case - lookup by rowid. */
193788 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
193789 RtreeSearchPoint *p; /* Search point for the leaf */
193790 i64 iRowid = sqlite3_value_int64(argv[0]);
193791 i64 iNode = 0;
193792 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
193793 if( rc==SQLITE_OK && pLeaf!=0 ){
193794 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
193795 assert( p!=0 ); /* Always returns pCsr->sPoint */
193796 pCsr->aNode[0] = pLeaf;
193797 p->id = iNode;
193798 p->eWithin = PARTLY_WITHIN;
193799 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
193800 p->iCell = (u8)iCell;
193801 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
193802 }else{
193803 pCsr->atEOF = 1;
193804 }
193805 }else{
193806 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
193807 ** with the configured constraints.
193808 */
193809 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
193810 if( rc==SQLITE_OK && idxNum<=3 ){
193811 RtreeCoord bbox[4];
193812 RtreeConstraint *p;
193813 assert( argc==1 );
193814 geopolyBBox(0, argv[0], bbox, &rc);
193815 if( rc ){
193816 goto geopoly_filter_end;
193817 }
193818 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
193819 pCsr->nConstraint = 4;
193820 if( p==0 ){
193821 rc = SQLITE_NOMEM;
193822 }else{
193823 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
193824 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
193825 if( idxNum==2 ){
193826 /* Overlap query */
193827 p->op = 'B';
193828 p->iCoord = 0;
193829 p->u.rValue = bbox[1].f;
193830 p++;
193831 p->op = 'D';
193832 p->iCoord = 1;
193833 p->u.rValue = bbox[0].f;
193834 p++;
193835 p->op = 'B';
193836 p->iCoord = 2;
193837 p->u.rValue = bbox[3].f;
193838 p++;
193839 p->op = 'D';
193840 p->iCoord = 3;
193841 p->u.rValue = bbox[2].f;
193842 }else{
193843 /* Within query */
193844 p->op = 'D';
193845 p->iCoord = 0;
193846 p->u.rValue = bbox[0].f;
193847 p++;
193848 p->op = 'B';
193849 p->iCoord = 1;
193850 p->u.rValue = bbox[1].f;
193851 p++;
193852 p->op = 'D';
193853 p->iCoord = 2;
193854 p->u.rValue = bbox[2].f;
193855 p++;
193856 p->op = 'B';
193857 p->iCoord = 3;
193858 p->u.rValue = bbox[3].f;
193859 }
193860 }
193861 }
193862 if( rc==SQLITE_OK ){
193863 RtreeSearchPoint *pNew;
193864 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
193865 if( pNew==0 ){
193866 rc = SQLITE_NOMEM;
193867 goto geopoly_filter_end;
193868 }
193869 pNew->id = 1;
193870 pNew->iCell = 0;
193871 pNew->eWithin = PARTLY_WITHIN;
193872 assert( pCsr->bPoint==1 );
193873 pCsr->aNode[0] = pRoot;
193874 pRoot = 0;
193875 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
193876 rc = rtreeStepToLeaf(pCsr);
193877 }
193878 }
193879
193880geopoly_filter_end:
193881 nodeRelease(pRtree, pRoot);
193882 rtreeRelease(pRtree);
193883 return rc;
193884}
193885
193886/*
193887** Rtree virtual table module xBestIndex method. There are three
193888** table scan strategies to choose from (in order from most to
193889** least desirable):
193890**
193891** idxNum idxStr Strategy
193892** ------------------------------------------------
193893** 1 "rowid" Direct lookup by rowid.
193894** 2 "rtree" R-tree overlap query using geopoly_overlap()
193895** 3 "rtree" R-tree within query using geopoly_within()
193896** 4 "fullscan" full-table scan.
193897** ------------------------------------------------
193898*/
193899static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
193900 int ii;
193901 int iRowidTerm = -1;
193902 int iFuncTerm = -1;
193903 int idxNum = 0;
193904
193905 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
193906 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
193907 if( !p->usable ) continue;
193908 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
193909 iRowidTerm = ii;
193910 break;
193911 }
193912 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
193913 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
193914 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
193915 ** See geopolyFindFunction() */
193916 iFuncTerm = ii;
193917 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
193918 }
193919 }
193920
193921 if( iRowidTerm>=0 ){
193922 pIdxInfo->idxNum = 1;
193923 pIdxInfo->idxStr = "rowid";
193924 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
193925 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
193926 pIdxInfo->estimatedCost = 30.0;
193927 pIdxInfo->estimatedRows = 1;
193928 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
193929 return SQLITE_OK;
193930 }
193931 if( iFuncTerm>=0 ){
193932 pIdxInfo->idxNum = idxNum;
193933 pIdxInfo->idxStr = "rtree";
193934 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
193935 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
193936 pIdxInfo->estimatedCost = 300.0;
193937 pIdxInfo->estimatedRows = 10;
193938 return SQLITE_OK;
193939 }
193940 pIdxInfo->idxNum = 4;
193941 pIdxInfo->idxStr = "fullscan";
193942 pIdxInfo->estimatedCost = 3000000.0;
193943 pIdxInfo->estimatedRows = 100000;
193944 return SQLITE_OK;
193945}
193946
193947
193948/*
193949** GEOPOLY virtual table module xColumn method.
193950*/
193951static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
193952 Rtree *pRtree = (Rtree *)cur->pVtab;
193953 RtreeCursor *pCsr = (RtreeCursor *)cur;
193954 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
193955 int rc = SQLITE_OK;
193956 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
193957
193958 if( rc ) return rc;
193959 if( p==0 ) return SQLITE_OK;
193960 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
193961 if( i<=pRtree->nAux ){
193962 if( !pCsr->bAuxValid ){
193963 if( pCsr->pReadAux==0 ){
193964 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
193965 &pCsr->pReadAux, 0);
193966 if( rc ) return rc;
193967 }
193968 sqlite3_bind_int64(pCsr->pReadAux, 1,
193969 nodeGetRowid(pRtree, pNode, p->iCell));
193970 rc = sqlite3_step(pCsr->pReadAux);
193971 if( rc==SQLITE_ROW ){
193972 pCsr->bAuxValid = 1;
193973 }else{
193974 sqlite3_reset(pCsr->pReadAux);
193975 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
193976 return rc;
193977 }
193978 }
193979 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
193980 }
193981 return SQLITE_OK;
193982}
193983
193984
193985/*
193986** The xUpdate method for GEOPOLY module virtual tables.
193987**
193988** For DELETE:
193989**
193990** argv[0] = the rowid to be deleted
193991**
193992** For INSERT:
193993**
193994** argv[0] = SQL NULL
193995** argv[1] = rowid to insert, or an SQL NULL to select automatically
193996** argv[2] = _shape column
193997** argv[3] = first application-defined column....
193998**
193999** For UPDATE:
194000**
194001** argv[0] = rowid to modify. Never NULL
194002** argv[1] = rowid after the change. Never NULL
194003** argv[2] = new value for _shape
194004** argv[3] = new value for first application-defined column....
194005*/
194006static int geopolyUpdate(
194007 sqlite3_vtab *pVtab,
194008 int nData,
194009 sqlite3_value **aData,
194010 sqlite_int64 *pRowid
194011){
194012 Rtree *pRtree = (Rtree *)pVtab;
194013 int rc = SQLITE_OK;
194014 RtreeCell cell; /* New cell to insert if nData>1 */
194015 i64 oldRowid; /* The old rowid */
194016 int oldRowidValid; /* True if oldRowid is valid */
194017 i64 newRowid; /* The new rowid */
194018 int newRowidValid; /* True if newRowid is valid */
194019 int coordChange = 0; /* Change in coordinates */
194020
194021 if( pRtree->nNodeRef ){
194022 /* Unable to write to the btree while another cursor is reading from it,
194023 ** since the write might do a rebalance which would disrupt the read
194024 ** cursor. */
194025 return SQLITE_LOCKED_VTAB;
194026 }
194027 rtreeReference(pRtree);
194028 assert(nData>=1);
194029
194030 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
194031 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
194032 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
194033 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
194034 cell.iRowid = newRowid;
194035
194036 if( nData>1 /* not a DELETE */
194037 && (!oldRowidValid /* INSERT */
194038 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
194039 || oldRowid!=newRowid) /* Rowid change */
194040 ){
194041 geopolyBBox(0, aData[2], cell.aCoord, &rc);
194042 if( rc ){
194043 if( rc==SQLITE_ERROR ){
194044 pVtab->zErrMsg =
194045 sqlite3_mprintf("_shape does not contain a valid polygon");
194046 }
194047 goto geopoly_update_end;
194048 }
194049 coordChange = 1;
194050
194051 /* If a rowid value was supplied, check if it is already present in
194052 ** the table. If so, the constraint has failed. */
194053 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
194054 int steprc;
194055 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
194056 steprc = sqlite3_step(pRtree->pReadRowid);
194057 rc = sqlite3_reset(pRtree->pReadRowid);
194058 if( SQLITE_ROW==steprc ){
194059 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
194060 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
194061 }else{
194062 rc = rtreeConstraintError(pRtree, 0);
194063 }
194064 }
194065 }
194066 }
194067
194068 /* If aData[0] is not an SQL NULL value, it is the rowid of a
194069 ** record to delete from the r-tree table. The following block does
194070 ** just that.
194071 */
194072 if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
194073 rc = rtreeDeleteRowid(pRtree, oldRowid);
194074 }
194075
194076 /* If the aData[] array contains more than one element, elements
194077 ** (aData[2]..aData[argc-1]) contain a new record to insert into
194078 ** the r-tree structure.
194079 */
194080 if( rc==SQLITE_OK && nData>1 && coordChange ){
194081 /* Insert the new record into the r-tree */
194082 RtreeNode *pLeaf = 0;
194083 if( !newRowidValid ){
194084 rc = rtreeNewRowid(pRtree, &cell.iRowid);
194085 }
194086 *pRowid = cell.iRowid;
194087 if( rc==SQLITE_OK ){
194088 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
194089 }
194090 if( rc==SQLITE_OK ){
194091 int rc2;
194092 pRtree->iReinsertHeight = -1;
194093 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
194094 rc2 = nodeRelease(pRtree, pLeaf);
194095 if( rc==SQLITE_OK ){
194096 rc = rc2;
194097 }
194098 }
194099 }
194100
194101 /* Change the data */
194102 if( rc==SQLITE_OK && nData>1 ){
194103 sqlite3_stmt *pUp = pRtree->pWriteAux;
194104 int jj;
194105 int nChange = 0;
194106 sqlite3_bind_int64(pUp, 1, cell.iRowid);
194107 assert( pRtree->nAux>=1 );
194108 if( sqlite3_value_nochange(aData[2]) ){
194109 sqlite3_bind_null(pUp, 2);
194110 }else{
194111 GeoPoly *p = 0;
194112 if( sqlite3_value_type(aData[2])==SQLITE_TEXT
194113 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
194114 && rc==SQLITE_OK
194115 ){
194116 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
194117 }else{
194118 sqlite3_bind_value(pUp, 2, aData[2]);
194119 }
194120 sqlite3_free(p);
194121 nChange = 1;
194122 }
194123 for(jj=1; jj<pRtree->nAux; jj++){
194124 nChange++;
194125 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
194126 }
194127 if( nChange ){
194128 sqlite3_step(pUp);
194129 rc = sqlite3_reset(pUp);
194130 }
194131 }
194132
194133geopoly_update_end:
194134 rtreeRelease(pRtree);
194135 return rc;
194136}
194137
194138/*
194139** Report that geopoly_overlap() is an overloaded function suitable
194140** for use in xBestIndex.
194141*/
194142static int geopolyFindFunction(
194143 sqlite3_vtab *pVtab,
194144 int nArg,
194145 const char *zName,
194146 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
194147 void **ppArg
194148){
194149 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
194150 *pxFunc = geopolyOverlapFunc;
194151 *ppArg = 0;
194152 return SQLITE_INDEX_CONSTRAINT_FUNCTION;
194153 }
194154 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
194155 *pxFunc = geopolyWithinFunc;
194156 *ppArg = 0;
194157 return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
194158 }
194159 return 0;
194160}
194161
194162
194163static sqlite3_module geopolyModule = {
194164 3, /* iVersion */
194165 geopolyCreate, /* xCreate - create a table */
194166 geopolyConnect, /* xConnect - connect to an existing table */
194167 geopolyBestIndex, /* xBestIndex - Determine search strategy */
194168 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
194169 rtreeDestroy, /* xDestroy - Drop a table */
194170 rtreeOpen, /* xOpen - open a cursor */
194171 rtreeClose, /* xClose - close a cursor */
194172 geopolyFilter, /* xFilter - configure scan constraints */
194173 rtreeNext, /* xNext - advance a cursor */
194174 rtreeEof, /* xEof */
194175 geopolyColumn, /* xColumn - read data */
194176 rtreeRowid, /* xRowid - read data */
194177 geopolyUpdate, /* xUpdate - write data */
194178 rtreeBeginTransaction, /* xBegin - begin transaction */
194179 rtreeEndTransaction, /* xSync - sync transaction */
194180 rtreeEndTransaction, /* xCommit - commit transaction */
194181 rtreeEndTransaction, /* xRollback - rollback transaction */
194182 geopolyFindFunction, /* xFindFunction - function overloading */
194183 rtreeRename, /* xRename - rename the table */
194184 rtreeSavepoint, /* xSavepoint */
194185 0, /* xRelease */
194186 0, /* xRollbackTo */
194187 rtreeShadowName /* xShadowName */
194188};
194189
194190static int sqlite3_geopoly_init(sqlite3 *db){
194191 int rc = SQLITE_OK;
194192 static const struct {
194193 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
194194 signed char nArg;
194195 unsigned char bPure;
194196 const char *zName;
194197 } aFunc[] = {
194198 { geopolyAreaFunc, 1, 1, "geopoly_area" },
194199 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
194200 { geopolyJsonFunc, 1, 1, "geopoly_json" },
194201 { geopolySvgFunc, -1, 1, "geopoly_svg" },
194202 { geopolyWithinFunc, 2, 1, "geopoly_within" },
194203 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
194204 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
194205 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
194206 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
194207 { geopolyXformFunc, 7, 1, "geopoly_xform" },
194208 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
194209 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
194210 };
194211 static const struct {
194212 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
194213 void (*xFinal)(sqlite3_context*);
194214 const char *zName;
194215 } aAgg[] = {
194216 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
194217 };
194218 int i;
194219 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
194220 int enc;
194221 if( aFunc[i].bPure ){
194222 enc = SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS;
194223 }else{
194224 enc = SQLITE_UTF8|SQLITE_DIRECTONLY;
194225 }
194226 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
194227 enc, 0,
194228 aFunc[i].xFunc, 0, 0);
194229 }
194230 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
194231 rc = sqlite3_create_function(db, aAgg[i].zName, 1,
194232 SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, 0,
194233 0, aAgg[i].xStep, aAgg[i].xFinal);
194234 }
194235 if( rc==SQLITE_OK ){
194236 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
194237 }
194238 return rc;
194239}
194240
194241/************** End of geopoly.c *********************************************/
194242/************** Continuing where we left off in rtree.c **********************/
194243#endif
194244
194245/*
194246** Register the r-tree module with database handle db. This creates the
194247** virtual table module "rtree" and the debugging/analysis scalar
194248** function "rtreenode".
194249*/
194250SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
194251 const int utf8 = SQLITE_UTF8;
194252 int rc;
194253
194254 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
194255 if( rc==SQLITE_OK ){
194256 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
194257 }
194258 if( rc==SQLITE_OK ){
194259 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
194260 }
194261 if( rc==SQLITE_OK ){
194262#ifdef SQLITE_RTREE_INT_ONLY
194263 void *c = (void *)RTREE_COORD_INT32;
194264#else
194265 void *c = (void *)RTREE_COORD_REAL32;
194266#endif
194267 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
194268 }
194269 if( rc==SQLITE_OK ){
194270 void *c = (void *)RTREE_COORD_INT32;
194271 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
194272 }
194273#ifdef SQLITE_ENABLE_GEOPOLY
194274 if( rc==SQLITE_OK ){
194275 rc = sqlite3_geopoly_init(db);
194276 }
194277#endif
194278
194279 return rc;
194280}
194281
194282/*
194283** This routine deletes the RtreeGeomCallback object that was attached
194284** one of the SQL functions create by sqlite3_rtree_geometry_callback()
194285** or sqlite3_rtree_query_callback(). In other words, this routine is the
194286** destructor for an RtreeGeomCallback objecct. This routine is called when
194287** the corresponding SQL function is deleted.
194288*/
194289static void rtreeFreeCallback(void *p){
194290 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
194291 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
194292 sqlite3_free(p);
194293}
194294
194295/*
194296** This routine frees the BLOB that is returned by geomCallback().
194297*/
194298static void rtreeMatchArgFree(void *pArg){
194299 int i;
194300 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
194301 for(i=0; i<p->nParam; i++){
194302 sqlite3_value_free(p->apSqlParam[i]);
194303 }
194304 sqlite3_free(p);
194305}
194306
194307/*
194308** Each call to sqlite3_rtree_geometry_callback() or
194309** sqlite3_rtree_query_callback() creates an ordinary SQLite
194310** scalar function that is implemented by this routine.
194311**
194312** All this function does is construct an RtreeMatchArg object that
194313** contains the geometry-checking callback routines and a list of
194314** parameters to this function, then return that RtreeMatchArg object
194315** as a BLOB.
194316**
194317** The R-Tree MATCH operator will read the returned BLOB, deserialize
194318** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
194319** out which elements of the R-Tree should be returned by the query.
194320*/
194321static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
194322 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
194323 RtreeMatchArg *pBlob;
194324 sqlite3_int64 nBlob;
194325 int memErr = 0;
194326
194327 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
194328 + nArg*sizeof(sqlite3_value*);
194329 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
194330 if( !pBlob ){
194331 sqlite3_result_error_nomem(ctx);
194332 }else{
194333 int i;
194334 pBlob->iSize = nBlob;
194335 pBlob->cb = pGeomCtx[0];
194336 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
194337 pBlob->nParam = nArg;
194338 for(i=0; i<nArg; i++){
194339 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
194340 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
194341#ifdef SQLITE_RTREE_INT_ONLY
194342 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
194343#else
194344 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
194345#endif
194346 }
194347 if( memErr ){
194348 sqlite3_result_error_nomem(ctx);
194349 rtreeMatchArgFree(pBlob);
194350 }else{
194351 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
194352 }
194353 }
194354}
194355
194356/*
194357** Register a new geometry function for use with the r-tree MATCH operator.
194358*/
194359SQLITE_API int sqlite3_rtree_geometry_callback(
194360 sqlite3 *db, /* Register SQL function on this connection */
194361 const char *zGeom, /* Name of the new SQL function */
194362 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
194363 void *pContext /* Extra data associated with the callback */
194364){
194365 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
194366
194367 /* Allocate and populate the context object. */
194368 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
194369 if( !pGeomCtx ) return SQLITE_NOMEM;
194370 pGeomCtx->xGeom = xGeom;
194371 pGeomCtx->xQueryFunc = 0;
194372 pGeomCtx->xDestructor = 0;
194373 pGeomCtx->pContext = pContext;
194374 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
194375 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
194376 );
194377}
194378
194379/*
194380** Register a new 2nd-generation geometry function for use with the
194381** r-tree MATCH operator.
194382*/
194383SQLITE_API int sqlite3_rtree_query_callback(
194384 sqlite3 *db, /* Register SQL function on this connection */
194385 const char *zQueryFunc, /* Name of new SQL function */
194386 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
194387 void *pContext, /* Extra data passed into the callback */
194388 void (*xDestructor)(void*) /* Destructor for the extra data */
194389){
194390 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
194391
194392 /* Allocate and populate the context object. */
194393 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
194394 if( !pGeomCtx ) return SQLITE_NOMEM;
194395 pGeomCtx->xGeom = 0;
194396 pGeomCtx->xQueryFunc = xQueryFunc;
194397 pGeomCtx->xDestructor = xDestructor;
194398 pGeomCtx->pContext = pContext;
194399 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
194400 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
194401 );
194402}
194403
194404#if !SQLITE_CORE
194405#ifdef _WIN32
194406__declspec(dllexport)
194407#endif
194408SQLITE_API int sqlite3_rtree_init(
194409 sqlite3 *db,
194410 char **pzErrMsg,
194411 const sqlite3_api_routines *pApi
194412){
194413 SQLITE_EXTENSION_INIT2(pApi)
194414 return sqlite3RtreeInit(db);
194415}
194416#endif
194417
194418#endif
194419
194420/************** End of rtree.c ***********************************************/
194421/************** Begin file icu.c *********************************************/
194422/*
194423** 2007 May 6
194424**
194425** The author disclaims copyright to this source code. In place of
194426** a legal notice, here is a blessing:
194427**
194428** May you do good and not evil.
194429** May you find forgiveness for yourself and forgive others.
194430** May you share freely, never taking more than you give.
194431**
194432*************************************************************************
194433** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
194434**
194435** This file implements an integration between the ICU library
194436** ("International Components for Unicode", an open-source library
194437** for handling unicode data) and SQLite. The integration uses
194438** ICU to provide the following to SQLite:
194439**
194440** * An implementation of the SQL regexp() function (and hence REGEXP
194441** operator) using the ICU uregex_XX() APIs.
194442**
194443** * Implementations of the SQL scalar upper() and lower() functions
194444** for case mapping.
194445**
194446** * Integration of ICU and SQLite collation sequences.
194447**
194448** * An implementation of the LIKE operator that uses ICU to
194449** provide case-independent matching.
194450*/
194451
194452#if !defined(SQLITE_CORE) \
194453 || defined(SQLITE_ENABLE_ICU) \
194454 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
194455
194456/* Include ICU headers */
194457#include <unicode/utypes.h>
194458#include <unicode/uregex.h>
194459#include <unicode/ustring.h>
194460#include <unicode/ucol.h>
194461
194462/* #include <assert.h> */
194463
194464#ifndef SQLITE_CORE
194465/* #include "sqlite3ext.h" */
194466 SQLITE_EXTENSION_INIT1
194467#else
194468/* #include "sqlite3.h" */
194469#endif
194470
194471/*
194472** This function is called when an ICU function called from within
194473** the implementation of an SQL scalar function returns an error.
194474**
194475** The scalar function context passed as the first argument is
194476** loaded with an error message based on the following two args.
194477*/
194478static void icuFunctionError(
194479 sqlite3_context *pCtx, /* SQLite scalar function context */
194480 const char *zName, /* Name of ICU function that failed */
194481 UErrorCode e /* Error code returned by ICU function */
194482){
194483 char zBuf[128];
194484 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
194485 zBuf[127] = '\0';
194486 sqlite3_result_error(pCtx, zBuf, -1);
194487}
194488
194489#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
194490
194491/*
194492** Maximum length (in bytes) of the pattern in a LIKE or GLOB
194493** operator.
194494*/
194495#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
194496# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
194497#endif
194498
194499/*
194500** Version of sqlite3_free() that is always a function, never a macro.
194501*/
194502static void xFree(void *p){
194503 sqlite3_free(p);
194504}
194505
194506/*
194507** This lookup table is used to help decode the first byte of
194508** a multi-byte UTF8 character. It is copied here from SQLite source
194509** code file utf8.c.
194510*/
194511static const unsigned char icuUtf8Trans1[] = {
194512 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194513 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
194514 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
194515 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
194516 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194517 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
194518 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
194519 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
194520};
194521
194522#define SQLITE_ICU_READ_UTF8(zIn, c) \
194523 c = *(zIn++); \
194524 if( c>=0xc0 ){ \
194525 c = icuUtf8Trans1[c-0xc0]; \
194526 while( (*zIn & 0xc0)==0x80 ){ \
194527 c = (c<<6) + (0x3f & *(zIn++)); \
194528 } \
194529 }
194530
194531#define SQLITE_ICU_SKIP_UTF8(zIn) \
194532 assert( *zIn ); \
194533 if( *(zIn++)>=0xc0 ){ \
194534 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
194535 }
194536
194537
194538/*
194539** Compare two UTF-8 strings for equality where the first string is
194540** a "LIKE" expression. Return true (1) if they are the same and
194541** false (0) if they are different.
194542*/
194543static int icuLikeCompare(
194544 const uint8_t *zPattern, /* LIKE pattern */
194545 const uint8_t *zString, /* The UTF-8 string to compare against */
194546 const UChar32 uEsc /* The escape character */
194547){
194548 static const uint32_t MATCH_ONE = (uint32_t)'_';
194549 static const uint32_t MATCH_ALL = (uint32_t)'%';
194550
194551 int prevEscape = 0; /* True if the previous character was uEsc */
194552
194553 while( 1 ){
194554
194555 /* Read (and consume) the next character from the input pattern. */
194556 uint32_t uPattern;
194557 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
194558 if( uPattern==0 ) break;
194559
194560 /* There are now 4 possibilities:
194561 **
194562 ** 1. uPattern is an unescaped match-all character "%",
194563 ** 2. uPattern is an unescaped match-one character "_",
194564 ** 3. uPattern is an unescaped escape character, or
194565 ** 4. uPattern is to be handled as an ordinary character
194566 */
194567 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
194568 /* Case 1. */
194569 uint8_t c;
194570
194571 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
194572 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
194573 ** test string.
194574 */
194575 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
194576 if( c==MATCH_ONE ){
194577 if( *zString==0 ) return 0;
194578 SQLITE_ICU_SKIP_UTF8(zString);
194579 }
194580 zPattern++;
194581 }
194582
194583 if( *zPattern==0 ) return 1;
194584
194585 while( *zString ){
194586 if( icuLikeCompare(zPattern, zString, uEsc) ){
194587 return 1;
194588 }
194589 SQLITE_ICU_SKIP_UTF8(zString);
194590 }
194591 return 0;
194592
194593 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
194594 /* Case 2. */
194595 if( *zString==0 ) return 0;
194596 SQLITE_ICU_SKIP_UTF8(zString);
194597
194598 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
194599 /* Case 3. */
194600 prevEscape = 1;
194601
194602 }else{
194603 /* Case 4. */
194604 uint32_t uString;
194605 SQLITE_ICU_READ_UTF8(zString, uString);
194606 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
194607 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
194608 if( uString!=uPattern ){
194609 return 0;
194610 }
194611 prevEscape = 0;
194612 }
194613 }
194614
194615 return *zString==0;
194616}
194617
194618/*
194619** Implementation of the like() SQL function. This function implements
194620** the build-in LIKE operator. The first argument to the function is the
194621** pattern and the second argument is the string. So, the SQL statements:
194622**
194623** A LIKE B
194624**
194625** is implemented as like(B, A). If there is an escape character E,
194626**
194627** A LIKE B ESCAPE E
194628**
194629** is mapped to like(B, A, E).
194630*/
194631static void icuLikeFunc(
194632 sqlite3_context *context,
194633 int argc,
194634 sqlite3_value **argv
194635){
194636 const unsigned char *zA = sqlite3_value_text(argv[0]);
194637 const unsigned char *zB = sqlite3_value_text(argv[1]);
194638 UChar32 uEsc = 0;
194639
194640 /* Limit the length of the LIKE or GLOB pattern to avoid problems
194641 ** of deep recursion and N*N behavior in patternCompare().
194642 */
194643 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
194644 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
194645 return;
194646 }
194647
194648
194649 if( argc==3 ){
194650 /* The escape character string must consist of a single UTF-8 character.
194651 ** Otherwise, return an error.
194652 */
194653 int nE= sqlite3_value_bytes(argv[2]);
194654 const unsigned char *zE = sqlite3_value_text(argv[2]);
194655 int i = 0;
194656 if( zE==0 ) return;
194657 U8_NEXT(zE, i, nE, uEsc);
194658 if( i!=nE){
194659 sqlite3_result_error(context,
194660 "ESCAPE expression must be a single character", -1);
194661 return;
194662 }
194663 }
194664
194665 if( zA && zB ){
194666 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
194667 }
194668}
194669
194670/*
194671** Function to delete compiled regexp objects. Registered as
194672** a destructor function with sqlite3_set_auxdata().
194673*/
194674static void icuRegexpDelete(void *p){
194675 URegularExpression *pExpr = (URegularExpression *)p;
194676 uregex_close(pExpr);
194677}
194678
194679/*
194680** Implementation of SQLite REGEXP operator. This scalar function takes
194681** two arguments. The first is a regular expression pattern to compile
194682** the second is a string to match against that pattern. If either
194683** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
194684** is 1 if the string matches the pattern, or 0 otherwise.
194685**
194686** SQLite maps the regexp() function to the regexp() operator such
194687** that the following two are equivalent:
194688**
194689** zString REGEXP zPattern
194690** regexp(zPattern, zString)
194691**
194692** Uses the following ICU regexp APIs:
194693**
194694** uregex_open()
194695** uregex_matches()
194696** uregex_close()
194697*/
194698static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
194699 UErrorCode status = U_ZERO_ERROR;
194700 URegularExpression *pExpr;
194701 UBool res;
194702 const UChar *zString = sqlite3_value_text16(apArg[1]);
194703
194704 (void)nArg; /* Unused parameter */
194705
194706 /* If the left hand side of the regexp operator is NULL,
194707 ** then the result is also NULL.
194708 */
194709 if( !zString ){
194710 return;
194711 }
194712
194713 pExpr = sqlite3_get_auxdata(p, 0);
194714 if( !pExpr ){
194715 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
194716 if( !zPattern ){
194717 return;
194718 }
194719 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
194720
194721 if( U_SUCCESS(status) ){
194722 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
194723 }else{
194724 assert(!pExpr);
194725 icuFunctionError(p, "uregex_open", status);
194726 return;
194727 }
194728 }
194729
194730 /* Configure the text that the regular expression operates on. */
194731 uregex_setText(pExpr, zString, -1, &status);
194732 if( !U_SUCCESS(status) ){
194733 icuFunctionError(p, "uregex_setText", status);
194734 return;
194735 }
194736
194737 /* Attempt the match */
194738 res = uregex_matches(pExpr, 0, &status);
194739 if( !U_SUCCESS(status) ){
194740 icuFunctionError(p, "uregex_matches", status);
194741 return;
194742 }
194743
194744 /* Set the text that the regular expression operates on to a NULL
194745 ** pointer. This is not really necessary, but it is tidier than
194746 ** leaving the regular expression object configured with an invalid
194747 ** pointer after this function returns.
194748 */
194749 uregex_setText(pExpr, 0, 0, &status);
194750
194751 /* Return 1 or 0. */
194752 sqlite3_result_int(p, res ? 1 : 0);
194753}
194754
194755/*
194756** Implementations of scalar functions for case mapping - upper() and
194757** lower(). Function upper() converts its input to upper-case (ABC).
194758** Function lower() converts to lower-case (abc).
194759**
194760** ICU provides two types of case mapping, "general" case mapping and
194761** "language specific". Refer to ICU documentation for the differences
194762** between the two.
194763**
194764** To utilise "general" case mapping, the upper() or lower() scalar
194765** functions are invoked with one argument:
194766**
194767** upper('ABC') -> 'abc'
194768** lower('abc') -> 'ABC'
194769**
194770** To access ICU "language specific" case mapping, upper() or lower()
194771** should be invoked with two arguments. The second argument is the name
194772** of the locale to use. Passing an empty string ("") or SQL NULL value
194773** as the second argument is the same as invoking the 1 argument version
194774** of upper() or lower().
194775**
194776** lower('I', 'en_us') -> 'i'
194777** lower('I', 'tr_tr') -> '\u131' (small dotless i)
194778**
194779** http://www.icu-project.org/userguide/posix.html#case_mappings
194780*/
194781static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
194782 const UChar *zInput; /* Pointer to input string */
194783 UChar *zOutput = 0; /* Pointer to output buffer */
194784 int nInput; /* Size of utf-16 input string in bytes */
194785 int nOut; /* Size of output buffer in bytes */
194786 int cnt;
194787 int bToUpper; /* True for toupper(), false for tolower() */
194788 UErrorCode status;
194789 const char *zLocale = 0;
194790
194791 assert(nArg==1 || nArg==2);
194792 bToUpper = (sqlite3_user_data(p)!=0);
194793 if( nArg==2 ){
194794 zLocale = (const char *)sqlite3_value_text(apArg[1]);
194795 }
194796
194797 zInput = sqlite3_value_text16(apArg[0]);
194798 if( !zInput ){
194799 return;
194800 }
194801 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
194802 if( nOut==0 ){
194803 sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
194804 return;
194805 }
194806
194807 for(cnt=0; cnt<2; cnt++){
194808 UChar *zNew = sqlite3_realloc(zOutput, nOut);
194809 if( zNew==0 ){
194810 sqlite3_free(zOutput);
194811 sqlite3_result_error_nomem(p);
194812 return;
194813 }
194814 zOutput = zNew;
194815 status = U_ZERO_ERROR;
194816 if( bToUpper ){
194817 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
194818 }else{
194819 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
194820 }
194821
194822 if( U_SUCCESS(status) ){
194823 sqlite3_result_text16(p, zOutput, nOut, xFree);
194824 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
194825 assert( cnt==0 );
194826 continue;
194827 }else{
194828 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
194829 }
194830 return;
194831 }
194832 assert( 0 ); /* Unreachable */
194833}
194834
194835#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
194836
194837/*
194838** Collation sequence destructor function. The pCtx argument points to
194839** a UCollator structure previously allocated using ucol_open().
194840*/
194841static void icuCollationDel(void *pCtx){
194842 UCollator *p = (UCollator *)pCtx;
194843 ucol_close(p);
194844}
194845
194846/*
194847** Collation sequence comparison function. The pCtx argument points to
194848** a UCollator structure previously allocated using ucol_open().
194849*/
194850static int icuCollationColl(
194851 void *pCtx,
194852 int nLeft,
194853 const void *zLeft,
194854 int nRight,
194855 const void *zRight
194856){
194857 UCollationResult res;
194858 UCollator *p = (UCollator *)pCtx;
194859 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
194860 switch( res ){
194861 case UCOL_LESS: return -1;
194862 case UCOL_GREATER: return +1;
194863 case UCOL_EQUAL: return 0;
194864 }
194865 assert(!"Unexpected return value from ucol_strcoll()");
194866 return 0;
194867}
194868
194869/*
194870** Implementation of the scalar function icu_load_collation().
194871**
194872** This scalar function is used to add ICU collation based collation
194873** types to an SQLite database connection. It is intended to be called
194874** as follows:
194875**
194876** SELECT icu_load_collation(<locale>, <collation-name>);
194877**
194878** Where <locale> is a string containing an ICU locale identifier (i.e.
194879** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
194880** collation sequence to create.
194881*/
194882static void icuLoadCollation(
194883 sqlite3_context *p,
194884 int nArg,
194885 sqlite3_value **apArg
194886){
194887 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
194888 UErrorCode status = U_ZERO_ERROR;
194889 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
194890 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
194891 UCollator *pUCollator; /* ICU library collation object */
194892 int rc; /* Return code from sqlite3_create_collation_x() */
194893
194894 assert(nArg==2);
194895 (void)nArg; /* Unused parameter */
194896 zLocale = (const char *)sqlite3_value_text(apArg[0]);
194897 zName = (const char *)sqlite3_value_text(apArg[1]);
194898
194899 if( !zLocale || !zName ){
194900 return;
194901 }
194902
194903 pUCollator = ucol_open(zLocale, &status);
194904 if( !U_SUCCESS(status) ){
194905 icuFunctionError(p, "ucol_open", status);
194906 return;
194907 }
194908 assert(p);
194909
194910 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
194911 icuCollationColl, icuCollationDel
194912 );
194913 if( rc!=SQLITE_OK ){
194914 ucol_close(pUCollator);
194915 sqlite3_result_error(p, "Error registering collation function", -1);
194916 }
194917}
194918
194919/*
194920** Register the ICU extension functions with database db.
194921*/
194922SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
194923# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS)
194924 static const struct IcuScalar {
194925 const char *zName; /* Function name */
194926 unsigned char nArg; /* Number of arguments */
194927 unsigned int enc; /* Optimal text encoding */
194928 unsigned char iContext; /* sqlite3_user_data() context */
194929 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
194930 } scalars[] = {
194931 {"icu_load_collation",2,SQLITE_UTF8|SQLITE_DIRECTONLY,1, icuLoadCollation},
194932#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
194933 {"regexp", 2, SQLITE_ANY|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
194934 {"lower", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
194935 {"lower", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
194936 {"upper", 1, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
194937 {"upper", 2, SQLITE_UTF16|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
194938 {"lower", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
194939 {"lower", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
194940 {"upper", 1, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
194941 {"upper", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
194942 {"like", 2, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
194943 {"like", 3, SQLITE_UTF8|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
194944#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
194945 };
194946 int rc = SQLITE_OK;
194947 int i;
194948
194949 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
194950 const struct IcuScalar *p = &scalars[i];
194951 rc = sqlite3_create_function(
194952 db, p->zName, p->nArg, p->enc,
194953 p->iContext ? (void*)db : (void*)0,
194954 p->xFunc, 0, 0
194955 );
194956 }
194957
194958 return rc;
194959}
194960
194961#if !SQLITE_CORE
194962#ifdef _WIN32
194963__declspec(dllexport)
194964#endif
194965SQLITE_API int sqlite3_icu_init(
194966 sqlite3 *db,
194967 char **pzErrMsg,
194968 const sqlite3_api_routines *pApi
194969){
194970 SQLITE_EXTENSION_INIT2(pApi)
194971 return sqlite3IcuInit(db);
194972}
194973#endif
194974
194975#endif
194976
194977/************** End of icu.c *************************************************/
194978/************** Begin file fts3_icu.c ****************************************/
194979/*
194980** 2007 June 22
194981**
194982** The author disclaims copyright to this source code. In place of
194983** a legal notice, here is a blessing:
194984**
194985** May you do good and not evil.
194986** May you find forgiveness for yourself and forgive others.
194987** May you share freely, never taking more than you give.
194988**
194989*************************************************************************
194990** This file implements a tokenizer for fts3 based on the ICU library.
194991*/
194992/* #include "fts3Int.h" */
194993#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
194994#ifdef SQLITE_ENABLE_ICU
194995
194996/* #include <assert.h> */
194997/* #include <string.h> */
194998/* #include "fts3_tokenizer.h" */
194999
195000#include <unicode/ubrk.h>
195001/* #include <unicode/ucol.h> */
195002/* #include <unicode/ustring.h> */
195003#include <unicode/utf16.h>
195004
195005typedef struct IcuTokenizer IcuTokenizer;
195006typedef struct IcuCursor IcuCursor;
195007
195008struct IcuTokenizer {
195009 sqlite3_tokenizer base;
195010 char *zLocale;
195011};
195012
195013struct IcuCursor {
195014 sqlite3_tokenizer_cursor base;
195015
195016 UBreakIterator *pIter; /* ICU break-iterator object */
195017 int nChar; /* Number of UChar elements in pInput */
195018 UChar *aChar; /* Copy of input using utf-16 encoding */
195019 int *aOffset; /* Offsets of each character in utf-8 input */
195020
195021 int nBuffer;
195022 char *zBuffer;
195023
195024 int iToken;
195025};
195026
195027/*
195028** Create a new tokenizer instance.
195029*/
195030static int icuCreate(
195031 int argc, /* Number of entries in argv[] */
195032 const char * const *argv, /* Tokenizer creation arguments */
195033 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
195034){
195035 IcuTokenizer *p;
195036 int n = 0;
195037
195038 if( argc>0 ){
195039 n = strlen(argv[0])+1;
195040 }
195041 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
195042 if( !p ){
195043 return SQLITE_NOMEM;
195044 }
195045 memset(p, 0, sizeof(IcuTokenizer));
195046
195047 if( n ){
195048 p->zLocale = (char *)&p[1];
195049 memcpy(p->zLocale, argv[0], n);
195050 }
195051
195052 *ppTokenizer = (sqlite3_tokenizer *)p;
195053
195054 return SQLITE_OK;
195055}
195056
195057/*
195058** Destroy a tokenizer
195059*/
195060static int icuDestroy(sqlite3_tokenizer *pTokenizer){
195061 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
195062 sqlite3_free(p);
195063 return SQLITE_OK;
195064}
195065
195066/*
195067** Prepare to begin tokenizing a particular string. The input
195068** string to be tokenized is pInput[0..nBytes-1]. A cursor
195069** used to incrementally tokenize this string is returned in
195070** *ppCursor.
195071*/
195072static int icuOpen(
195073 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
195074 const char *zInput, /* Input string */
195075 int nInput, /* Length of zInput in bytes */
195076 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
195077){
195078 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
195079 IcuCursor *pCsr;
195080
195081 const int32_t opt = U_FOLD_CASE_DEFAULT;
195082 UErrorCode status = U_ZERO_ERROR;
195083 int nChar;
195084
195085 UChar32 c;
195086 int iInput = 0;
195087 int iOut = 0;
195088
195089 *ppCursor = 0;
195090
195091 if( zInput==0 ){
195092 nInput = 0;
195093 zInput = "";
195094 }else if( nInput<0 ){
195095 nInput = strlen(zInput);
195096 }
195097 nChar = nInput+1;
195098 pCsr = (IcuCursor *)sqlite3_malloc64(
195099 sizeof(IcuCursor) + /* IcuCursor */
195100 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
195101 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
195102 );
195103 if( !pCsr ){
195104 return SQLITE_NOMEM;
195105 }
195106 memset(pCsr, 0, sizeof(IcuCursor));
195107 pCsr->aChar = (UChar *)&pCsr[1];
195108 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
195109
195110 pCsr->aOffset[iOut] = iInput;
195111 U8_NEXT(zInput, iInput, nInput, c);
195112 while( c>0 ){
195113 int isError = 0;
195114 c = u_foldCase(c, opt);
195115 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
195116 if( isError ){
195117 sqlite3_free(pCsr);
195118 return SQLITE_ERROR;
195119 }
195120 pCsr->aOffset[iOut] = iInput;
195121
195122 if( iInput<nInput ){
195123 U8_NEXT(zInput, iInput, nInput, c);
195124 }else{
195125 c = 0;
195126 }
195127 }
195128
195129 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
195130 if( !U_SUCCESS(status) ){
195131 sqlite3_free(pCsr);
195132 return SQLITE_ERROR;
195133 }
195134 pCsr->nChar = iOut;
195135
195136 ubrk_first(pCsr->pIter);
195137 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
195138 return SQLITE_OK;
195139}
195140
195141/*
195142** Close a tokenization cursor previously opened by a call to icuOpen().
195143*/
195144static int icuClose(sqlite3_tokenizer_cursor *pCursor){
195145 IcuCursor *pCsr = (IcuCursor *)pCursor;
195146 ubrk_close(pCsr->pIter);
195147 sqlite3_free(pCsr->zBuffer);
195148 sqlite3_free(pCsr);
195149 return SQLITE_OK;
195150}
195151
195152/*
195153** Extract the next token from a tokenization cursor.
195154*/
195155static int icuNext(
195156 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
195157 const char **ppToken, /* OUT: *ppToken is the token text */
195158 int *pnBytes, /* OUT: Number of bytes in token */
195159 int *piStartOffset, /* OUT: Starting offset of token */
195160 int *piEndOffset, /* OUT: Ending offset of token */
195161 int *piPosition /* OUT: Position integer of token */
195162){
195163 IcuCursor *pCsr = (IcuCursor *)pCursor;
195164
195165 int iStart = 0;
195166 int iEnd = 0;
195167 int nByte = 0;
195168
195169 while( iStart==iEnd ){
195170 UChar32 c;
195171
195172 iStart = ubrk_current(pCsr->pIter);
195173 iEnd = ubrk_next(pCsr->pIter);
195174 if( iEnd==UBRK_DONE ){
195175 return SQLITE_DONE;
195176 }
195177
195178 while( iStart<iEnd ){
195179 int iWhite = iStart;
195180 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
195181 if( u_isspace(c) ){
195182 iStart = iWhite;
195183 }else{
195184 break;
195185 }
195186 }
195187 assert(iStart<=iEnd);
195188 }
195189
195190 do {
195191 UErrorCode status = U_ZERO_ERROR;
195192 if( nByte ){
195193 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
195194 if( !zNew ){
195195 return SQLITE_NOMEM;
195196 }
195197 pCsr->zBuffer = zNew;
195198 pCsr->nBuffer = nByte;
195199 }
195200
195201 u_strToUTF8(
195202 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
195203 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
195204 &status /* Output success/failure */
195205 );
195206 } while( nByte>pCsr->nBuffer );
195207
195208 *ppToken = pCsr->zBuffer;
195209 *pnBytes = nByte;
195210 *piStartOffset = pCsr->aOffset[iStart];
195211 *piEndOffset = pCsr->aOffset[iEnd];
195212 *piPosition = pCsr->iToken++;
195213
195214 return SQLITE_OK;
195215}
195216
195217/*
195218** The set of routines that implement the simple tokenizer
195219*/
195220static const sqlite3_tokenizer_module icuTokenizerModule = {
195221 0, /* iVersion */
195222 icuCreate, /* xCreate */
195223 icuDestroy, /* xCreate */
195224 icuOpen, /* xOpen */
195225 icuClose, /* xClose */
195226 icuNext, /* xNext */
195227 0, /* xLanguageid */
195228};
195229
195230/*
195231** Set *ppModule to point at the implementation of the ICU tokenizer.
195232*/
195233SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
195234 sqlite3_tokenizer_module const**ppModule
195235){
195236 *ppModule = &icuTokenizerModule;
195237}
195238
195239#endif /* defined(SQLITE_ENABLE_ICU) */
195240#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
195241
195242/************** End of fts3_icu.c ********************************************/
195243/************** Begin file sqlite3rbu.c **************************************/
195244/*
195245** 2014 August 30
195246**
195247** The author disclaims copyright to this source code. In place of
195248** a legal notice, here is a blessing:
195249**
195250** May you do good and not evil.
195251** May you find forgiveness for yourself and forgive others.
195252** May you share freely, never taking more than you give.
195253**
195254*************************************************************************
195255**
195256**
195257** OVERVIEW
195258**
195259** The RBU extension requires that the RBU update be packaged as an
195260** SQLite database. The tables it expects to find are described in
195261** sqlite3rbu.h. Essentially, for each table xyz in the target database
195262** that the user wishes to write to, a corresponding data_xyz table is
195263** created in the RBU database and populated with one row for each row to
195264** update, insert or delete from the target table.
195265**
195266** The update proceeds in three stages:
195267**
195268** 1) The database is updated. The modified database pages are written
195269** to a *-oal file. A *-oal file is just like a *-wal file, except
195270** that it is named "<database>-oal" instead of "<database>-wal".
195271** Because regular SQLite clients do not look for file named
195272** "<database>-oal", they go on using the original database in
195273** rollback mode while the *-oal file is being generated.
195274**
195275** During this stage RBU does not update the database by writing
195276** directly to the target tables. Instead it creates "imposter"
195277** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
195278** to update each b-tree individually. All updates required by each
195279** b-tree are completed before moving on to the next, and all
195280** updates are done in sorted key order.
195281**
195282** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
195283** location using a call to rename(2). Before doing this the RBU
195284** module takes an EXCLUSIVE lock on the database file, ensuring
195285** that there are no other active readers.
195286**
195287** Once the EXCLUSIVE lock is released, any other database readers
195288** detect the new *-wal file and read the database in wal mode. At
195289** this point they see the new version of the database - including
195290** the updates made as part of the RBU update.
195291**
195292** 3) The new *-wal file is checkpointed. This proceeds in the same way
195293** as a regular database checkpoint, except that a single frame is
195294** checkpointed each time sqlite3rbu_step() is called. If the RBU
195295** handle is closed before the entire *-wal file is checkpointed,
195296** the checkpoint progress is saved in the RBU database and the
195297** checkpoint can be resumed by another RBU client at some point in
195298** the future.
195299**
195300** POTENTIAL PROBLEMS
195301**
195302** The rename() call might not be portable. And RBU is not currently
195303** syncing the directory after renaming the file.
195304**
195305** When state is saved, any commit to the *-oal file and the commit to
195306** the RBU update database are not atomic. So if the power fails at the
195307** wrong moment they might get out of sync. As the main database will be
195308** committed before the RBU update database this will likely either just
195309** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
195310** constraint violations).
195311**
195312** If some client does modify the target database mid RBU update, or some
195313** other error occurs, the RBU extension will keep throwing errors. It's
195314** not really clear how to get out of this state. The system could just
195315** by delete the RBU update database and *-oal file and have the device
195316** download the update again and start over.
195317**
195318** At present, for an UPDATE, both the new.* and old.* records are
195319** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
195320** fields are collected. This means we're probably writing a lot more
195321** data to disk when saving the state of an ongoing update to the RBU
195322** update database than is strictly necessary.
195323**
195324*/
195325
195326/* #include <assert.h> */
195327/* #include <string.h> */
195328/* #include <stdio.h> */
195329
195330/* #include "sqlite3.h" */
195331
195332#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
195333/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
195334/************** Begin file sqlite3rbu.h **************************************/
195335/*
195336** 2014 August 30
195337**
195338** The author disclaims copyright to this source code. In place of
195339** a legal notice, here is a blessing:
195340**
195341** May you do good and not evil.
195342** May you find forgiveness for yourself and forgive others.
195343** May you share freely, never taking more than you give.
195344**
195345*************************************************************************
195346**
195347** This file contains the public interface for the RBU extension.
195348*/
195349
195350/*
195351** SUMMARY
195352**
195353** Writing a transaction containing a large number of operations on
195354** b-tree indexes that are collectively larger than the available cache
195355** memory can be very inefficient.
195356**
195357** The problem is that in order to update a b-tree, the leaf page (at least)
195358** containing the entry being inserted or deleted must be modified. If the
195359** working set of leaves is larger than the available cache memory, then a
195360** single leaf that is modified more than once as part of the transaction
195361** may be loaded from or written to the persistent media multiple times.
195362** Additionally, because the index updates are likely to be applied in
195363** random order, access to pages within the database is also likely to be in
195364** random order, which is itself quite inefficient.
195365**
195366** One way to improve the situation is to sort the operations on each index
195367** by index key before applying them to the b-tree. This leads to an IO
195368** pattern that resembles a single linear scan through the index b-tree,
195369** and all but guarantees each modified leaf page is loaded and stored
195370** exactly once. SQLite uses this trick to improve the performance of
195371** CREATE INDEX commands. This extension allows it to be used to improve
195372** the performance of large transactions on existing databases.
195373**
195374** Additionally, this extension allows the work involved in writing the
195375** large transaction to be broken down into sub-transactions performed
195376** sequentially by separate processes. This is useful if the system cannot
195377** guarantee that a single update process will run for long enough to apply
195378** the entire update, for example because the update is being applied on a
195379** mobile device that is frequently rebooted. Even after the writer process
195380** has committed one or more sub-transactions, other database clients continue
195381** to read from the original database snapshot. In other words, partially
195382** applied transactions are not visible to other clients.
195383**
195384** "RBU" stands for "Resumable Bulk Update". As in a large database update
195385** transmitted via a wireless network to a mobile device. A transaction
195386** applied using this extension is hence refered to as an "RBU update".
195387**
195388**
195389** LIMITATIONS
195390**
195391** An "RBU update" transaction is subject to the following limitations:
195392**
195393** * The transaction must consist of INSERT, UPDATE and DELETE operations
195394** only.
195395**
195396** * INSERT statements may not use any default values.
195397**
195398** * UPDATE and DELETE statements must identify their target rows by
195399** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
195400** KEY fields may not be updated or deleted. If the table being written
195401** has no PRIMARY KEY, affected rows must be identified by rowid.
195402**
195403** * UPDATE statements may not modify PRIMARY KEY columns.
195404**
195405** * No triggers will be fired.
195406**
195407** * No foreign key violations are detected or reported.
195408**
195409** * CHECK constraints are not enforced.
195410**
195411** * No constraint handling mode except for "OR ROLLBACK" is supported.
195412**
195413**
195414** PREPARATION
195415**
195416** An "RBU update" is stored as a separate SQLite database. A database
195417** containing an RBU update is an "RBU database". For each table in the
195418** target database to be updated, the RBU database should contain a table
195419** named "data_<target name>" containing the same set of columns as the
195420** target table, and one more - "rbu_control". The data_% table should
195421** have no PRIMARY KEY or UNIQUE constraints, but each column should have
195422** the same type as the corresponding column in the target database.
195423** The "rbu_control" column should have no type at all. For example, if
195424** the target database contains:
195425**
195426** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
195427**
195428** Then the RBU database should contain:
195429**
195430** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
195431**
195432** The order of the columns in the data_% table does not matter.
195433**
195434** Instead of a regular table, the RBU database may also contain virtual
195435** tables or view named using the data_<target> naming scheme.
195436**
195437** Instead of the plain data_<target> naming scheme, RBU database tables
195438** may also be named data<integer>_<target>, where <integer> is any sequence
195439** of zero or more numeric characters (0-9). This can be significant because
195440** tables within the RBU database are always processed in order sorted by
195441** name. By judicious selection of the <integer> portion of the names
195442** of the RBU tables the user can therefore control the order in which they
195443** are processed. This can be useful, for example, to ensure that "external
195444** content" FTS4 tables are updated before their underlying content tables.
195445**
195446** If the target database table is a virtual table or a table that has no
195447** PRIMARY KEY declaration, the data_% table must also contain a column
195448** named "rbu_rowid". This column is mapped to the tables implicit primary
195449** key column - "rowid". Virtual tables for which the "rowid" column does
195450** not function like a primary key value cannot be updated using RBU. For
195451** example, if the target db contains either of the following:
195452**
195453** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
195454** CREATE TABLE x1(a, b)
195455**
195456** then the RBU database should contain:
195457**
195458** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
195459**
195460** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
195461** target table must be present in the input table. For virtual tables,
195462** hidden columns are optional - they are updated by RBU if present in
195463** the input table, or not otherwise. For example, to write to an fts4
195464** table with a hidden languageid column such as:
195465**
195466** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
195467**
195468** Either of the following input table schemas may be used:
195469**
195470** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
195471** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
195472**
195473** For each row to INSERT into the target database as part of the RBU
195474** update, the corresponding data_% table should contain a single record
195475** with the "rbu_control" column set to contain integer value 0. The
195476** other columns should be set to the values that make up the new record
195477** to insert.
195478**
195479** If the target database table has an INTEGER PRIMARY KEY, it is not
195480** possible to insert a NULL value into the IPK column. Attempting to
195481** do so results in an SQLITE_MISMATCH error.
195482**
195483** For each row to DELETE from the target database as part of the RBU
195484** update, the corresponding data_% table should contain a single record
195485** with the "rbu_control" column set to contain integer value 1. The
195486** real primary key values of the row to delete should be stored in the
195487** corresponding columns of the data_% table. The values stored in the
195488** other columns are not used.
195489**
195490** For each row to UPDATE from the target database as part of the RBU
195491** update, the corresponding data_% table should contain a single record
195492** with the "rbu_control" column set to contain a value of type text.
195493** The real primary key values identifying the row to update should be
195494** stored in the corresponding columns of the data_% table row, as should
195495** the new values of all columns being update. The text value in the
195496** "rbu_control" column must contain the same number of characters as
195497** there are columns in the target database table, and must consist entirely
195498** of 'x' and '.' characters (or in some special cases 'd' - see below). For
195499** each column that is being updated, the corresponding character is set to
195500** 'x'. For those that remain as they are, the corresponding character of the
195501** rbu_control value should be set to '.'. For example, given the tables
195502** above, the update statement:
195503**
195504** UPDATE t1 SET c = 'usa' WHERE a = 4;
195505**
195506** is represented by the data_t1 row created by:
195507**
195508** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
195509**
195510** Instead of an 'x' character, characters of the rbu_control value specified
195511** for UPDATEs may also be set to 'd'. In this case, instead of updating the
195512** target table with the value stored in the corresponding data_% column, the
195513** user-defined SQL function "rbu_delta()" is invoked and the result stored in
195514** the target table column. rbu_delta() is invoked with two arguments - the
195515** original value currently stored in the target table column and the
195516** value specified in the data_xxx table.
195517**
195518** For example, this row:
195519**
195520** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
195521**
195522** is similar to an UPDATE statement such as:
195523**
195524** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
195525**
195526** Finally, if an 'f' character appears in place of a 'd' or 's' in an
195527** ota_control string, the contents of the data_xxx table column is assumed
195528** to be a "fossil delta" - a patch to be applied to a blob value in the
195529** format used by the fossil source-code management system. In this case
195530** the existing value within the target database table must be of type BLOB.
195531** It is replaced by the result of applying the specified fossil delta to
195532** itself.
195533**
195534** If the target database table is a virtual table or a table with no PRIMARY
195535** KEY, the rbu_control value should not include a character corresponding
195536** to the rbu_rowid value. For example, this:
195537**
195538** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
195539** VALUES(NULL, 'usa', 12, '.x');
195540**
195541** causes a result similar to:
195542**
195543** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
195544**
195545** The data_xxx tables themselves should have no PRIMARY KEY declarations.
195546** However, RBU is more efficient if reading the rows in from each data_xxx
195547** table in "rowid" order is roughly the same as reading them sorted by
195548** the PRIMARY KEY of the corresponding target database table. In other
195549** words, rows should be sorted using the destination table PRIMARY KEY
195550** fields before they are inserted into the data_xxx tables.
195551**
195552** USAGE
195553**
195554** The API declared below allows an application to apply an RBU update
195555** stored on disk to an existing target database. Essentially, the
195556** application:
195557**
195558** 1) Opens an RBU handle using the sqlite3rbu_open() function.
195559**
195560** 2) Registers any required virtual table modules with the database
195561** handle returned by sqlite3rbu_db(). Also, if required, register
195562** the rbu_delta() implementation.
195563**
195564** 3) Calls the sqlite3rbu_step() function one or more times on
195565** the new handle. Each call to sqlite3rbu_step() performs a single
195566** b-tree operation, so thousands of calls may be required to apply
195567** a complete update.
195568**
195569** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
195570** sqlite3rbu_step() has been called enough times to completely
195571** apply the update to the target database, then the RBU database
195572** is marked as fully applied. Otherwise, the state of the RBU
195573** update application is saved in the RBU database for later
195574** resumption.
195575**
195576** See comments below for more detail on APIs.
195577**
195578** If an update is only partially applied to the target database by the
195579** time sqlite3rbu_close() is called, various state information is saved
195580** within the RBU database. This allows subsequent processes to automatically
195581** resume the RBU update from where it left off.
195582**
195583** To remove all RBU extension state information, returning an RBU database
195584** to its original contents, it is sufficient to drop all tables that begin
195585** with the prefix "rbu_"
195586**
195587** DATABASE LOCKING
195588**
195589** An RBU update may not be applied to a database in WAL mode. Attempting
195590** to do so is an error (SQLITE_ERROR).
195591**
195592** While an RBU handle is open, a SHARED lock may be held on the target
195593** database file. This means it is possible for other clients to read the
195594** database, but not to write it.
195595**
195596** If an RBU update is started and then suspended before it is completed,
195597** then an external client writes to the database, then attempting to resume
195598** the suspended RBU update is also an error (SQLITE_BUSY).
195599*/
195600
195601#ifndef _SQLITE3RBU_H
195602#define _SQLITE3RBU_H
195603
195604/* #include "sqlite3.h" ** Required for error code definitions ** */
195605
195606#if 0
195607extern "C" {
195608#endif
195609
195610typedef struct sqlite3rbu sqlite3rbu;
195611
195612/*
195613** Open an RBU handle.
195614**
195615** Argument zTarget is the path to the target database. Argument zRbu is
195616** the path to the RBU database. Each call to this function must be matched
195617** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
195618** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
195619** or zRbu begin with "file:", it will be interpreted as an SQLite
195620** database URI, not a regular file name.
195621**
195622** If the zState argument is passed a NULL value, the RBU extension stores
195623** the current state of the update (how many rows have been updated, which
195624** indexes are yet to be updated etc.) within the RBU database itself. This
195625** can be convenient, as it means that the RBU application does not need to
195626** organize removing a separate state file after the update is concluded.
195627** Or, if zState is non-NULL, it must be a path to a database file in which
195628** the RBU extension can store the state of the update.
195629**
195630** When resuming an RBU update, the zState argument must be passed the same
195631** value as when the RBU update was started.
195632**
195633** Once the RBU update is finished, the RBU extension does not
195634** automatically remove any zState database file, even if it created it.
195635**
195636** By default, RBU uses the default VFS to access the files on disk. To
195637** use a VFS other than the default, an SQLite "file:" URI containing a
195638** "vfs=..." option may be passed as the zTarget option.
195639**
195640** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
195641** SQLite's built-in VFSs, including the multiplexor VFS. However it does
195642** not work out of the box with zipvfs. Refer to the comment describing
195643** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
195644*/
195645SQLITE_API sqlite3rbu *sqlite3rbu_open(
195646 const char *zTarget,
195647 const char *zRbu,
195648 const char *zState
195649);
195650
195651/*
195652** Open an RBU handle to perform an RBU vacuum on database file zTarget.
195653** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
195654** that it can be suspended and resumed like an RBU update.
195655**
195656** The second argument to this function identifies a database in which
195657** to store the state of the RBU vacuum operation if it is suspended. The
195658** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
195659** operation, the state database should either not exist or be empty
195660** (contain no tables). If an RBU vacuum is suspended by calling
195661** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
195662** returned SQLITE_DONE, the vacuum state is stored in the state database.
195663** The vacuum can be resumed by calling this function to open a new RBU
195664** handle specifying the same target and state databases.
195665**
195666** If the second argument passed to this function is NULL, then the
195667** name of the state database is "<database>-vacuum", where <database>
195668** is the name of the target database file. In this case, on UNIX, if the
195669** state database is not already present in the file-system, it is created
195670** with the same permissions as the target db is made.
195671**
195672** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
195673** state database ends with "-vactmp". This name is reserved for internal
195674** use.
195675**
195676** This function does not delete the state database after an RBU vacuum
195677** is completed, even if it created it. However, if the call to
195678** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
195679** of the state tables within the state database are zeroed. This way,
195680** the next call to sqlite3rbu_vacuum() opens a handle that starts a
195681** new RBU vacuum operation.
195682**
195683** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
195684** describing the sqlite3rbu_create_vfs() API function below for
195685** a description of the complications associated with using RBU with
195686** zipvfs databases.
195687*/
195688SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
195689 const char *zTarget,
195690 const char *zState
195691);
195692
195693/*
195694** Configure a limit for the amount of temp space that may be used by
195695** the RBU handle passed as the first argument. The new limit is specified
195696** in bytes by the second parameter. If it is positive, the limit is updated.
195697** If the second parameter to this function is passed zero, then the limit
195698** is removed entirely. If the second parameter is negative, the limit is
195699** not modified (this is useful for querying the current limit).
195700**
195701** In all cases the returned value is the current limit in bytes (zero
195702** indicates unlimited).
195703**
195704** If the temp space limit is exceeded during operation, an SQLITE_FULL
195705** error is returned.
195706*/
195707SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
195708
195709/*
195710** Return the current amount of temp file space, in bytes, currently used by
195711** the RBU handle passed as the only argument.
195712*/
195713SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
195714
195715/*
195716** Internally, each RBU connection uses a separate SQLite database
195717** connection to access the target and rbu update databases. This
195718** API allows the application direct access to these database handles.
195719**
195720** The first argument passed to this function must be a valid, open, RBU
195721** handle. The second argument should be passed zero to access the target
195722** database handle, or non-zero to access the rbu update database handle.
195723** Accessing the underlying database handles may be useful in the
195724** following scenarios:
195725**
195726** * If any target tables are virtual tables, it may be necessary to
195727** call sqlite3_create_module() on the target database handle to
195728** register the required virtual table implementations.
195729**
195730** * If the data_xxx tables in the RBU source database are virtual
195731** tables, the application may need to call sqlite3_create_module() on
195732** the rbu update db handle to any required virtual table
195733** implementations.
195734**
195735** * If the application uses the "rbu_delta()" feature described above,
195736** it must use sqlite3_create_function() or similar to register the
195737** rbu_delta() implementation with the target database handle.
195738**
195739** If an error has occurred, either while opening or stepping the RBU object,
195740** this function may return NULL. The error code and message may be collected
195741** when sqlite3rbu_close() is called.
195742**
195743** Database handles returned by this function remain valid until the next
195744** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
195745*/
195746SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
195747
195748/*
195749** Do some work towards applying the RBU update to the target db.
195750**
195751** Return SQLITE_DONE if the update has been completely applied, or
195752** SQLITE_OK if no error occurs but there remains work to do to apply
195753** the RBU update. If an error does occur, some other error code is
195754** returned.
195755**
195756** Once a call to sqlite3rbu_step() has returned a value other than
195757** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
195758** that immediately return the same value.
195759*/
195760SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
195761
195762/*
195763** Force RBU to save its state to disk.
195764**
195765** If a power failure or application crash occurs during an update, following
195766** system recovery RBU may resume the update from the point at which the state
195767** was last saved. In other words, from the most recent successful call to
195768** sqlite3rbu_close() or this function.
195769**
195770** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
195771*/
195772SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
195773
195774/*
195775** Close an RBU handle.
195776**
195777** If the RBU update has been completely applied, mark the RBU database
195778** as fully applied. Otherwise, assuming no error has occurred, save the
195779** current state of the RBU update appliation to the RBU database.
195780**
195781** If an error has already occurred as part of an sqlite3rbu_step()
195782** or sqlite3rbu_open() call, or if one occurs within this function, an
195783** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
195784** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
195785** English language error message. It is the responsibility of the caller to
195786** eventually free any such buffer using sqlite3_free().
195787**
195788** Otherwise, if no error occurs, this function returns SQLITE_OK if the
195789** update has been partially applied, or SQLITE_DONE if it has been
195790** completely applied.
195791*/
195792SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
195793
195794/*
195795** Return the total number of key-value operations (inserts, deletes or
195796** updates) that have been performed on the target database since the
195797** current RBU update was started.
195798*/
195799SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
195800
195801/*
195802** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
195803** progress indications for the two stages of an RBU update. This API may
195804** be useful for driving GUI progress indicators and similar.
195805**
195806** An RBU update is divided into two stages:
195807**
195808** * Stage 1, in which changes are accumulated in an oal/wal file, and
195809** * Stage 2, in which the contents of the wal file are copied into the
195810** main database.
195811**
195812** The update is visible to non-RBU clients during stage 2. During stage 1
195813** non-RBU reader clients may see the original database.
195814**
195815** If this API is called during stage 2 of the update, output variable
195816** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
195817** to a value between 0 and 10000 to indicate the permyriadage progress of
195818** stage 2. A value of 5000 indicates that stage 2 is half finished,
195819** 9000 indicates that it is 90% finished, and so on.
195820**
195821** If this API is called during stage 1 of the update, output variable
195822** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
195823** value to which (*pnOne) is set depends on whether or not the RBU
195824** database contains an "rbu_count" table. The rbu_count table, if it
195825** exists, must contain the same columns as the following:
195826**
195827** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
195828**
195829** There must be one row in the table for each source (data_xxx) table within
195830** the RBU database. The 'tbl' column should contain the name of the source
195831** table. The 'cnt' column should contain the number of rows within the
195832** source table.
195833**
195834** If the rbu_count table is present and populated correctly and this
195835** API is called during stage 1, the *pnOne output variable is set to the
195836** permyriadage progress of the same stage. If the rbu_count table does
195837** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
195838** table exists but is not correctly populated, the value of the *pnOne
195839** output variable during stage 1 is undefined.
195840*/
195841SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
195842
195843/*
195844** Obtain an indication as to the current stage of an RBU update or vacuum.
195845** This function always returns one of the SQLITE_RBU_STATE_XXX constants
195846** defined in this file. Return values should be interpreted as follows:
195847**
195848** SQLITE_RBU_STATE_OAL:
195849** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
195850** may either add further data to the *-oal file, or compute data that will
195851** be added by a subsequent call.
195852**
195853** SQLITE_RBU_STATE_MOVE:
195854** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
195855** will move the *-oal file to the equivalent *-wal path. If the current
195856** operation is an RBU update, then the updated version of the database
195857** file will become visible to ordinary SQLite clients following the next
195858** call to sqlite3rbu_step().
195859**
195860** SQLITE_RBU_STATE_CHECKPOINT:
195861** RBU is currently performing an incremental checkpoint. The next call to
195862** sqlite3rbu_step() will copy a page of data from the *-wal file into
195863** the target database file.
195864**
195865** SQLITE_RBU_STATE_DONE:
195866** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
195867** will immediately return SQLITE_DONE.
195868**
195869** SQLITE_RBU_STATE_ERROR:
195870** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
195871** immediately return the SQLite error code associated with the error.
195872*/
195873#define SQLITE_RBU_STATE_OAL 1
195874#define SQLITE_RBU_STATE_MOVE 2
195875#define SQLITE_RBU_STATE_CHECKPOINT 3
195876#define SQLITE_RBU_STATE_DONE 4
195877#define SQLITE_RBU_STATE_ERROR 5
195878
195879SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
195880
195881/*
195882** Create an RBU VFS named zName that accesses the underlying file-system
195883** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
195884** then the new RBU VFS uses the default system VFS to access the file-system.
195885** The new object is registered as a non-default VFS with SQLite before
195886** returning.
195887**
195888** Part of the RBU implementation uses a custom VFS object. Usually, this
195889** object is created and deleted automatically by RBU.
195890**
195891** The exception is for applications that also use zipvfs. In this case,
195892** the custom VFS must be explicitly created by the user before the RBU
195893** handle is opened. The RBU VFS should be installed so that the zipvfs
195894** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
195895** (for example multiplexor) to access the file-system. For example,
195896** to assemble an RBU enabled VFS stack that uses both zipvfs and
195897** multiplexor (error checking omitted):
195898**
195899** // Create a VFS named "multiplex" (not the default).
195900** sqlite3_multiplex_initialize(0, 0);
195901**
195902** // Create an rbu VFS named "rbu" that uses multiplexor. If the
195903** // second argument were replaced with NULL, the "rbu" VFS would
195904** // access the file-system via the system default VFS, bypassing the
195905** // multiplexor.
195906** sqlite3rbu_create_vfs("rbu", "multiplex");
195907**
195908** // Create a zipvfs VFS named "zipvfs" that uses rbu.
195909** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
195910**
195911** // Make zipvfs the default VFS.
195912** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
195913**
195914** Because the default VFS created above includes a RBU functionality, it
195915** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
195916** that does not include the RBU layer results in an error.
195917**
195918** The overhead of adding the "rbu" VFS to the system is negligible for
195919** non-RBU users. There is no harm in an application accessing the
195920** file-system via "rbu" all the time, even if it only uses RBU functionality
195921** occasionally.
195922*/
195923SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
195924
195925/*
195926** Deregister and destroy an RBU vfs created by an earlier call to
195927** sqlite3rbu_create_vfs().
195928**
195929** VFS objects are not reference counted. If a VFS object is destroyed
195930** before all database handles that use it have been closed, the results
195931** are undefined.
195932*/
195933SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
195934
195935#if 0
195936} /* end of the 'extern "C"' block */
195937#endif
195938
195939#endif /* _SQLITE3RBU_H */
195940
195941/************** End of sqlite3rbu.h ******************************************/
195942/************** Continuing where we left off in sqlite3rbu.c *****************/
195943
195944#if defined(_WIN32_WCE)
195945/* #include "windows.h" */
195946#endif
195947
195948/* Maximum number of prepared UPDATE statements held by this module */
195949#define SQLITE_RBU_UPDATE_CACHESIZE 16
195950
195951/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
195952** to enable checksum verification.
195953*/
195954#ifndef RBU_ENABLE_DELTA_CKSUM
195955# define RBU_ENABLE_DELTA_CKSUM 0
195956#endif
195957
195958/*
195959** Swap two objects of type TYPE.
195960*/
195961#if !defined(SQLITE_AMALGAMATION)
195962# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
195963#endif
195964
195965/*
195966** The rbu_state table is used to save the state of a partially applied
195967** update so that it can be resumed later. The table consists of integer
195968** keys mapped to values as follows:
195969**
195970** RBU_STATE_STAGE:
195971** May be set to integer values 1, 2, 4 or 5. As follows:
195972** 1: the *-rbu file is currently under construction.
195973** 2: the *-rbu file has been constructed, but not yet moved
195974** to the *-wal path.
195975** 4: the checkpoint is underway.
195976** 5: the rbu update has been checkpointed.
195977**
195978** RBU_STATE_TBL:
195979** Only valid if STAGE==1. The target database name of the table
195980** currently being written.
195981**
195982** RBU_STATE_IDX:
195983** Only valid if STAGE==1. The target database name of the index
195984** currently being written, or NULL if the main table is currently being
195985** updated.
195986**
195987** RBU_STATE_ROW:
195988** Only valid if STAGE==1. Number of rows already processed for the current
195989** table/index.
195990**
195991** RBU_STATE_PROGRESS:
195992** Trbul number of sqlite3rbu_step() calls made so far as part of this
195993** rbu update.
195994**
195995** RBU_STATE_CKPT:
195996** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
195997** header created by recovering the *-wal file. This is used to detect
195998** cases when another client appends frames to the *-wal file in the
195999** middle of an incremental checkpoint (an incremental checkpoint cannot
196000** be continued if this happens).
196001**
196002** RBU_STATE_COOKIE:
196003** Valid if STAGE==1. The current change-counter cookie value in the
196004** target db file.
196005**
196006** RBU_STATE_OALSZ:
196007** Valid if STAGE==1. The size in bytes of the *-oal file.
196008**
196009** RBU_STATE_DATATBL:
196010** Only valid if STAGE==1. The RBU database name of the table
196011** currently being read.
196012*/
196013#define RBU_STATE_STAGE 1
196014#define RBU_STATE_TBL 2
196015#define RBU_STATE_IDX 3
196016#define RBU_STATE_ROW 4
196017#define RBU_STATE_PROGRESS 5
196018#define RBU_STATE_CKPT 6
196019#define RBU_STATE_COOKIE 7
196020#define RBU_STATE_OALSZ 8
196021#define RBU_STATE_PHASEONESTEP 9
196022#define RBU_STATE_DATATBL 10
196023
196024#define RBU_STAGE_OAL 1
196025#define RBU_STAGE_MOVE 2
196026#define RBU_STAGE_CAPTURE 3
196027#define RBU_STAGE_CKPT 4
196028#define RBU_STAGE_DONE 5
196029
196030
196031#define RBU_CREATE_STATE \
196032 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
196033
196034typedef struct RbuFrame RbuFrame;
196035typedef struct RbuObjIter RbuObjIter;
196036typedef struct RbuState RbuState;
196037typedef struct RbuSpan RbuSpan;
196038typedef struct rbu_vfs rbu_vfs;
196039typedef struct rbu_file rbu_file;
196040typedef struct RbuUpdateStmt RbuUpdateStmt;
196041
196042#if !defined(SQLITE_AMALGAMATION)
196043typedef unsigned int u32;
196044typedef unsigned short u16;
196045typedef unsigned char u8;
196046typedef sqlite3_int64 i64;
196047#endif
196048
196049/*
196050** These values must match the values defined in wal.c for the equivalent
196051** locks. These are not magic numbers as they are part of the SQLite file
196052** format.
196053*/
196054#define WAL_LOCK_WRITE 0
196055#define WAL_LOCK_CKPT 1
196056#define WAL_LOCK_READ0 3
196057
196058#define SQLITE_FCNTL_RBUCNT 5149216
196059
196060/*
196061** A structure to store values read from the rbu_state table in memory.
196062*/
196063struct RbuState {
196064 int eStage;
196065 char *zTbl;
196066 char *zDataTbl;
196067 char *zIdx;
196068 i64 iWalCksum;
196069 int nRow;
196070 i64 nProgress;
196071 u32 iCookie;
196072 i64 iOalSz;
196073 i64 nPhaseOneStep;
196074};
196075
196076struct RbuUpdateStmt {
196077 char *zMask; /* Copy of update mask used with pUpdate */
196078 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
196079 RbuUpdateStmt *pNext;
196080};
196081
196082struct RbuSpan {
196083 const char *zSpan;
196084 int nSpan;
196085};
196086
196087/*
196088** An iterator of this type is used to iterate through all objects in
196089** the target database that require updating. For each such table, the
196090** iterator visits, in order:
196091**
196092** * the table itself,
196093** * each index of the table (zero or more points to visit), and
196094** * a special "cleanup table" state.
196095**
196096** abIndexed:
196097** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
196098** it points to an array of flags nTblCol elements in size. The flag is
196099** set for each column that is either a part of the PK or a part of an
196100** index. Or clear otherwise.
196101**
196102** If there are one or more partial indexes on the table, all fields of
196103** this array set set to 1. This is because in that case, the module has
196104** no way to tell which fields will be required to add and remove entries
196105** from the partial indexes.
196106**
196107*/
196108struct RbuObjIter {
196109 sqlite3_stmt *pTblIter; /* Iterate through tables */
196110 sqlite3_stmt *pIdxIter; /* Index iterator */
196111 int nTblCol; /* Size of azTblCol[] array */
196112 char **azTblCol; /* Array of unquoted target column names */
196113 char **azTblType; /* Array of target column types */
196114 int *aiSrcOrder; /* src table col -> target table col */
196115 u8 *abTblPk; /* Array of flags, set on target PK columns */
196116 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
196117 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
196118 int eType; /* Table type - an RBU_PK_XXX value */
196119
196120 /* Output variables. zTbl==0 implies EOF. */
196121 int bCleanup; /* True in "cleanup" state */
196122 const char *zTbl; /* Name of target db table */
196123 const char *zDataTbl; /* Name of rbu db table (or null) */
196124 const char *zIdx; /* Name of target db index (or null) */
196125 int iTnum; /* Root page of current object */
196126 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
196127 int bUnique; /* Current index is unique */
196128 int nIndex; /* Number of aux. indexes on table zTbl */
196129
196130 /* Statements created by rbuObjIterPrepareAll() */
196131 int nCol; /* Number of columns in current object */
196132 sqlite3_stmt *pSelect; /* Source data */
196133 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
196134 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
196135 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
196136 int nIdxCol;
196137 RbuSpan *aIdxCol;
196138 char *zIdxSql;
196139
196140 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
196141 RbuUpdateStmt *pRbuUpdate;
196142};
196143
196144/*
196145** Values for RbuObjIter.eType
196146**
196147** 0: Table does not exist (error)
196148** 1: Table has an implicit rowid.
196149** 2: Table has an explicit IPK column.
196150** 3: Table has an external PK index.
196151** 4: Table is WITHOUT ROWID.
196152** 5: Table is a virtual table.
196153*/
196154#define RBU_PK_NOTABLE 0
196155#define RBU_PK_NONE 1
196156#define RBU_PK_IPK 2
196157#define RBU_PK_EXTERNAL 3
196158#define RBU_PK_WITHOUT_ROWID 4
196159#define RBU_PK_VTAB 5
196160
196161
196162/*
196163** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
196164** one of the following operations.
196165*/
196166#define RBU_INSERT 1 /* Insert on a main table b-tree */
196167#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
196168#define RBU_REPLACE 3 /* Delete and then insert a row */
196169#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
196170#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
196171
196172#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
196173
196174/*
196175** A single step of an incremental checkpoint - frame iWalFrame of the wal
196176** file should be copied to page iDbPage of the database file.
196177*/
196178struct RbuFrame {
196179 u32 iDbPage;
196180 u32 iWalFrame;
196181};
196182
196183/*
196184** RBU handle.
196185**
196186** nPhaseOneStep:
196187** If the RBU database contains an rbu_count table, this value is set to
196188** a running estimate of the number of b-tree operations required to
196189** finish populating the *-oal file. This allows the sqlite3_bp_progress()
196190** API to calculate the permyriadage progress of populating the *-oal file
196191** using the formula:
196192**
196193** permyriadage = (10000 * nProgress) / nPhaseOneStep
196194**
196195** nPhaseOneStep is initialized to the sum of:
196196**
196197** nRow * (nIndex + 1)
196198**
196199** for all source tables in the RBU database, where nRow is the number
196200** of rows in the source table and nIndex the number of indexes on the
196201** corresponding target database table.
196202**
196203** This estimate is accurate if the RBU update consists entirely of
196204** INSERT operations. However, it is inaccurate if:
196205**
196206** * the RBU update contains any UPDATE operations. If the PK specified
196207** for an UPDATE operation does not exist in the target table, then
196208** no b-tree operations are required on index b-trees. Or if the
196209** specified PK does exist, then (nIndex*2) such operations are
196210** required (one delete and one insert on each index b-tree).
196211**
196212** * the RBU update contains any DELETE operations for which the specified
196213** PK does not exist. In this case no operations are required on index
196214** b-trees.
196215**
196216** * the RBU update contains REPLACE operations. These are similar to
196217** UPDATE operations.
196218**
196219** nPhaseOneStep is updated to account for the conditions above during the
196220** first pass of each source table. The updated nPhaseOneStep value is
196221** stored in the rbu_state table if the RBU update is suspended.
196222*/
196223struct sqlite3rbu {
196224 int eStage; /* Value of RBU_STATE_STAGE field */
196225 sqlite3 *dbMain; /* target database handle */
196226 sqlite3 *dbRbu; /* rbu database handle */
196227 char *zTarget; /* Path to target db */
196228 char *zRbu; /* Path to rbu db */
196229 char *zState; /* Path to state db (or NULL if zRbu) */
196230 char zStateDb[5]; /* Db name for state ("stat" or "main") */
196231 int rc; /* Value returned by last rbu_step() call */
196232 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
196233 int nStep; /* Rows processed for current object */
196234 int nProgress; /* Rows processed for all objects */
196235 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
196236 const char *zVfsName; /* Name of automatically created rbu vfs */
196237 rbu_file *pTargetFd; /* File handle open on target db */
196238 int nPagePerSector; /* Pages per sector for pTargetFd */
196239 i64 iOalSz;
196240 i64 nPhaseOneStep;
196241
196242 /* The following state variables are used as part of the incremental
196243 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
196244 ** function rbuSetupCheckpoint() for details. */
196245 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
196246 u32 mLock;
196247 int nFrame; /* Entries in aFrame[] array */
196248 int nFrameAlloc; /* Allocated size of aFrame[] array */
196249 RbuFrame *aFrame;
196250 int pgsz;
196251 u8 *aBuf;
196252 i64 iWalCksum;
196253 i64 szTemp; /* Current size of all temp files in use */
196254 i64 szTempLimit; /* Total size limit for temp files */
196255
196256 /* Used in RBU vacuum mode only */
196257 int nRbu; /* Number of RBU VFS in the stack */
196258 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
196259};
196260
196261/*
196262** An rbu VFS is implemented using an instance of this structure.
196263**
196264** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
196265** It is NULL for RBU VFS objects created explicitly using
196266** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
196267** space used by the RBU handle.
196268*/
196269struct rbu_vfs {
196270 sqlite3_vfs base; /* rbu VFS shim methods */
196271 sqlite3_vfs *pRealVfs; /* Underlying VFS */
196272 sqlite3_mutex *mutex; /* Mutex to protect pMain */
196273 sqlite3rbu *pRbu; /* Owner RBU object */
196274 rbu_file *pMain; /* List of main db files */
196275 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
196276};
196277
196278/*
196279** Each file opened by an rbu VFS is represented by an instance of
196280** the following structure.
196281**
196282** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
196283** "sz" is set to the current size of the database file.
196284*/
196285struct rbu_file {
196286 sqlite3_file base; /* sqlite3_file methods */
196287 sqlite3_file *pReal; /* Underlying file handle */
196288 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
196289 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
196290 i64 sz; /* Size of file in bytes (temp only) */
196291
196292 int openFlags; /* Flags this file was opened with */
196293 u32 iCookie; /* Cookie value for main db files */
196294 u8 iWriteVer; /* "write-version" value for main db files */
196295 u8 bNolock; /* True to fail EXCLUSIVE locks */
196296
196297 int nShm; /* Number of entries in apShm[] array */
196298 char **apShm; /* Array of mmap'd *-shm regions */
196299 char *zDel; /* Delete this when closing file */
196300
196301 const char *zWal; /* Wal filename for this main db file */
196302 rbu_file *pWalFd; /* Wal file descriptor for this main db */
196303 rbu_file *pMainNext; /* Next MAIN_DB file */
196304 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
196305};
196306
196307/*
196308** True for an RBU vacuum handle, or false otherwise.
196309*/
196310#define rbuIsVacuum(p) ((p)->zTarget==0)
196311
196312
196313/*************************************************************************
196314** The following three functions, found below:
196315**
196316** rbuDeltaGetInt()
196317** rbuDeltaChecksum()
196318** rbuDeltaApply()
196319**
196320** are lifted from the fossil source code (http://fossil-scm.org). They
196321** are used to implement the scalar SQL function rbu_fossil_delta().
196322*/
196323
196324/*
196325** Read bytes from *pz and convert them into a positive integer. When
196326** finished, leave *pz pointing to the first character past the end of
196327** the integer. The *pLen parameter holds the length of the string
196328** in *pz and is decremented once for each character in the integer.
196329*/
196330static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
196331 static const signed char zValue[] = {
196332 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
196333 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
196334 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
196335 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
196336 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
196337 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
196338 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
196339 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
196340 };
196341 unsigned int v = 0;
196342 int c;
196343 unsigned char *z = (unsigned char*)*pz;
196344 unsigned char *zStart = z;
196345 while( (c = zValue[0x7f&*(z++)])>=0 ){
196346 v = (v<<6) + c;
196347 }
196348 z--;
196349 *pLen -= z - zStart;
196350 *pz = (char*)z;
196351 return v;
196352}
196353
196354#if RBU_ENABLE_DELTA_CKSUM
196355/*
196356** Compute a 32-bit checksum on the N-byte buffer. Return the result.
196357*/
196358static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
196359 const unsigned char *z = (const unsigned char *)zIn;
196360 unsigned sum0 = 0;
196361 unsigned sum1 = 0;
196362 unsigned sum2 = 0;
196363 unsigned sum3 = 0;
196364 while(N >= 16){
196365 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
196366 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
196367 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
196368 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
196369 z += 16;
196370 N -= 16;
196371 }
196372 while(N >= 4){
196373 sum0 += z[0];
196374 sum1 += z[1];
196375 sum2 += z[2];
196376 sum3 += z[3];
196377 z += 4;
196378 N -= 4;
196379 }
196380 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
196381 switch(N){
196382 case 3: sum3 += (z[2] << 8);
196383 case 2: sum3 += (z[1] << 16);
196384 case 1: sum3 += (z[0] << 24);
196385 default: ;
196386 }
196387 return sum3;
196388}
196389#endif
196390
196391/*
196392** Apply a delta.
196393**
196394** The output buffer should be big enough to hold the whole output
196395** file and a NUL terminator at the end. The delta_output_size()
196396** routine will determine this size for you.
196397**
196398** The delta string should be null-terminated. But the delta string
196399** may contain embedded NUL characters (if the input and output are
196400** binary files) so we also have to pass in the length of the delta in
196401** the lenDelta parameter.
196402**
196403** This function returns the size of the output file in bytes (excluding
196404** the final NUL terminator character). Except, if the delta string is
196405** malformed or intended for use with a source file other than zSrc,
196406** then this routine returns -1.
196407**
196408** Refer to the delta_create() documentation above for a description
196409** of the delta file format.
196410*/
196411static int rbuDeltaApply(
196412 const char *zSrc, /* The source or pattern file */
196413 int lenSrc, /* Length of the source file */
196414 const char *zDelta, /* Delta to apply to the pattern */
196415 int lenDelta, /* Length of the delta */
196416 char *zOut /* Write the output into this preallocated buffer */
196417){
196418 unsigned int limit;
196419 unsigned int total = 0;
196420#if RBU_ENABLE_DELTA_CKSUM
196421 char *zOrigOut = zOut;
196422#endif
196423
196424 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
196425 if( *zDelta!='\n' ){
196426 /* ERROR: size integer not terminated by "\n" */
196427 return -1;
196428 }
196429 zDelta++; lenDelta--;
196430 while( *zDelta && lenDelta>0 ){
196431 unsigned int cnt, ofst;
196432 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
196433 switch( zDelta[0] ){
196434 case '@': {
196435 zDelta++; lenDelta--;
196436 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
196437 if( lenDelta>0 && zDelta[0]!=',' ){
196438 /* ERROR: copy command not terminated by ',' */
196439 return -1;
196440 }
196441 zDelta++; lenDelta--;
196442 total += cnt;
196443 if( total>limit ){
196444 /* ERROR: copy exceeds output file size */
196445 return -1;
196446 }
196447 if( (int)(ofst+cnt) > lenSrc ){
196448 /* ERROR: copy extends past end of input */
196449 return -1;
196450 }
196451 memcpy(zOut, &zSrc[ofst], cnt);
196452 zOut += cnt;
196453 break;
196454 }
196455 case ':': {
196456 zDelta++; lenDelta--;
196457 total += cnt;
196458 if( total>limit ){
196459 /* ERROR: insert command gives an output larger than predicted */
196460 return -1;
196461 }
196462 if( (int)cnt>lenDelta ){
196463 /* ERROR: insert count exceeds size of delta */
196464 return -1;
196465 }
196466 memcpy(zOut, zDelta, cnt);
196467 zOut += cnt;
196468 zDelta += cnt;
196469 lenDelta -= cnt;
196470 break;
196471 }
196472 case ';': {
196473 zDelta++; lenDelta--;
196474 zOut[0] = 0;
196475#if RBU_ENABLE_DELTA_CKSUM
196476 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
196477 /* ERROR: bad checksum */
196478 return -1;
196479 }
196480#endif
196481 if( total!=limit ){
196482 /* ERROR: generated size does not match predicted size */
196483 return -1;
196484 }
196485 return total;
196486 }
196487 default: {
196488 /* ERROR: unknown delta operator */
196489 return -1;
196490 }
196491 }
196492 }
196493 /* ERROR: unterminated delta */
196494 return -1;
196495}
196496
196497static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
196498 int size;
196499 size = rbuDeltaGetInt(&zDelta, &lenDelta);
196500 if( *zDelta!='\n' ){
196501 /* ERROR: size integer not terminated by "\n" */
196502 return -1;
196503 }
196504 return size;
196505}
196506
196507/*
196508** End of code taken from fossil.
196509*************************************************************************/
196510
196511/*
196512** Implementation of SQL scalar function rbu_fossil_delta().
196513**
196514** This function applies a fossil delta patch to a blob. Exactly two
196515** arguments must be passed to this function. The first is the blob to
196516** patch and the second the patch to apply. If no error occurs, this
196517** function returns the patched blob.
196518*/
196519static void rbuFossilDeltaFunc(
196520 sqlite3_context *context,
196521 int argc,
196522 sqlite3_value **argv
196523){
196524 const char *aDelta;
196525 int nDelta;
196526 const char *aOrig;
196527 int nOrig;
196528
196529 int nOut;
196530 int nOut2;
196531 char *aOut;
196532
196533 assert( argc==2 );
196534
196535 nOrig = sqlite3_value_bytes(argv[0]);
196536 aOrig = (const char*)sqlite3_value_blob(argv[0]);
196537 nDelta = sqlite3_value_bytes(argv[1]);
196538 aDelta = (const char*)sqlite3_value_blob(argv[1]);
196539
196540 /* Figure out the size of the output */
196541 nOut = rbuDeltaOutputSize(aDelta, nDelta);
196542 if( nOut<0 ){
196543 sqlite3_result_error(context, "corrupt fossil delta", -1);
196544 return;
196545 }
196546
196547 aOut = sqlite3_malloc(nOut+1);
196548 if( aOut==0 ){
196549 sqlite3_result_error_nomem(context);
196550 }else{
196551 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
196552 if( nOut2!=nOut ){
196553 sqlite3_free(aOut);
196554 sqlite3_result_error(context, "corrupt fossil delta", -1);
196555 }else{
196556 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
196557 }
196558 }
196559}
196560
196561
196562/*
196563** Prepare the SQL statement in buffer zSql against database handle db.
196564** If successful, set *ppStmt to point to the new statement and return
196565** SQLITE_OK.
196566**
196567** Otherwise, if an error does occur, set *ppStmt to NULL and return
196568** an SQLite error code. Additionally, set output variable *pzErrmsg to
196569** point to a buffer containing an error message. It is the responsibility
196570** of the caller to (eventually) free this buffer using sqlite3_free().
196571*/
196572static int prepareAndCollectError(
196573 sqlite3 *db,
196574 sqlite3_stmt **ppStmt,
196575 char **pzErrmsg,
196576 const char *zSql
196577){
196578 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
196579 if( rc!=SQLITE_OK ){
196580 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
196581 *ppStmt = 0;
196582 }
196583 return rc;
196584}
196585
196586/*
196587** Reset the SQL statement passed as the first argument. Return a copy
196588** of the value returned by sqlite3_reset().
196589**
196590** If an error has occurred, then set *pzErrmsg to point to a buffer
196591** containing an error message. It is the responsibility of the caller
196592** to eventually free this buffer using sqlite3_free().
196593*/
196594static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
196595 int rc = sqlite3_reset(pStmt);
196596 if( rc!=SQLITE_OK ){
196597 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
196598 }
196599 return rc;
196600}
196601
196602/*
196603** Unless it is NULL, argument zSql points to a buffer allocated using
196604** sqlite3_malloc containing an SQL statement. This function prepares the SQL
196605** statement against database db and frees the buffer. If statement
196606** compilation is successful, *ppStmt is set to point to the new statement
196607** handle and SQLITE_OK is returned.
196608**
196609** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
196610** returned. In this case, *pzErrmsg may also be set to point to an error
196611** message. It is the responsibility of the caller to free this error message
196612** buffer using sqlite3_free().
196613**
196614** If argument zSql is NULL, this function assumes that an OOM has occurred.
196615** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
196616*/
196617static int prepareFreeAndCollectError(
196618 sqlite3 *db,
196619 sqlite3_stmt **ppStmt,
196620 char **pzErrmsg,
196621 char *zSql
196622){
196623 int rc;
196624 assert( *pzErrmsg==0 );
196625 if( zSql==0 ){
196626 rc = SQLITE_NOMEM;
196627 *ppStmt = 0;
196628 }else{
196629 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
196630 sqlite3_free(zSql);
196631 }
196632 return rc;
196633}
196634
196635/*
196636** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
196637** by an earlier call to rbuObjIterCacheTableInfo().
196638*/
196639static void rbuObjIterFreeCols(RbuObjIter *pIter){
196640 int i;
196641 for(i=0; i<pIter->nTblCol; i++){
196642 sqlite3_free(pIter->azTblCol[i]);
196643 sqlite3_free(pIter->azTblType[i]);
196644 }
196645 sqlite3_free(pIter->azTblCol);
196646 pIter->azTblCol = 0;
196647 pIter->azTblType = 0;
196648 pIter->aiSrcOrder = 0;
196649 pIter->abTblPk = 0;
196650 pIter->abNotNull = 0;
196651 pIter->nTblCol = 0;
196652 pIter->eType = 0; /* Invalid value */
196653}
196654
196655/*
196656** Finalize all statements and free all allocations that are specific to
196657** the current object (table/index pair).
196658*/
196659static void rbuObjIterClearStatements(RbuObjIter *pIter){
196660 RbuUpdateStmt *pUp;
196661
196662 sqlite3_finalize(pIter->pSelect);
196663 sqlite3_finalize(pIter->pInsert);
196664 sqlite3_finalize(pIter->pDelete);
196665 sqlite3_finalize(pIter->pTmpInsert);
196666 pUp = pIter->pRbuUpdate;
196667 while( pUp ){
196668 RbuUpdateStmt *pTmp = pUp->pNext;
196669 sqlite3_finalize(pUp->pUpdate);
196670 sqlite3_free(pUp);
196671 pUp = pTmp;
196672 }
196673 sqlite3_free(pIter->aIdxCol);
196674 sqlite3_free(pIter->zIdxSql);
196675
196676 pIter->pSelect = 0;
196677 pIter->pInsert = 0;
196678 pIter->pDelete = 0;
196679 pIter->pRbuUpdate = 0;
196680 pIter->pTmpInsert = 0;
196681 pIter->nCol = 0;
196682 pIter->nIdxCol = 0;
196683 pIter->aIdxCol = 0;
196684 pIter->zIdxSql = 0;
196685}
196686
196687/*
196688** Clean up any resources allocated as part of the iterator object passed
196689** as the only argument.
196690*/
196691static void rbuObjIterFinalize(RbuObjIter *pIter){
196692 rbuObjIterClearStatements(pIter);
196693 sqlite3_finalize(pIter->pTblIter);
196694 sqlite3_finalize(pIter->pIdxIter);
196695 rbuObjIterFreeCols(pIter);
196696 memset(pIter, 0, sizeof(RbuObjIter));
196697}
196698
196699/*
196700** Advance the iterator to the next position.
196701**
196702** If no error occurs, SQLITE_OK is returned and the iterator is left
196703** pointing to the next entry. Otherwise, an error code and message is
196704** left in the RBU handle passed as the first argument. A copy of the
196705** error code is returned.
196706*/
196707static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
196708 int rc = p->rc;
196709 if( rc==SQLITE_OK ){
196710
196711 /* Free any SQLite statements used while processing the previous object */
196712 rbuObjIterClearStatements(pIter);
196713 if( pIter->zIdx==0 ){
196714 rc = sqlite3_exec(p->dbMain,
196715 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
196716 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
196717 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
196718 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
196719 , 0, 0, &p->zErrmsg
196720 );
196721 }
196722
196723 if( rc==SQLITE_OK ){
196724 if( pIter->bCleanup ){
196725 rbuObjIterFreeCols(pIter);
196726 pIter->bCleanup = 0;
196727 rc = sqlite3_step(pIter->pTblIter);
196728 if( rc!=SQLITE_ROW ){
196729 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
196730 pIter->zTbl = 0;
196731 }else{
196732 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
196733 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
196734 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
196735 }
196736 }else{
196737 if( pIter->zIdx==0 ){
196738 sqlite3_stmt *pIdx = pIter->pIdxIter;
196739 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
196740 }
196741 if( rc==SQLITE_OK ){
196742 rc = sqlite3_step(pIter->pIdxIter);
196743 if( rc!=SQLITE_ROW ){
196744 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
196745 pIter->bCleanup = 1;
196746 pIter->zIdx = 0;
196747 }else{
196748 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
196749 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
196750 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
196751 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
196752 }
196753 }
196754 }
196755 }
196756 }
196757
196758 if( rc!=SQLITE_OK ){
196759 rbuObjIterFinalize(pIter);
196760 p->rc = rc;
196761 }
196762 return rc;
196763}
196764
196765
196766/*
196767** The implementation of the rbu_target_name() SQL function. This function
196768** accepts one or two arguments. The first argument is the name of a table -
196769** the name of a table in the RBU database. The second, if it is present, is 1
196770** for a view or 0 for a table.
196771**
196772** For a non-vacuum RBU handle, if the table name matches the pattern:
196773**
196774** data[0-9]_<name>
196775**
196776** where <name> is any sequence of 1 or more characters, <name> is returned.
196777** Otherwise, if the only argument does not match the above pattern, an SQL
196778** NULL is returned.
196779**
196780** "data_t1" -> "t1"
196781** "data0123_t2" -> "t2"
196782** "dataAB_t3" -> NULL
196783**
196784** For an rbu vacuum handle, a copy of the first argument is returned if
196785** the second argument is either missing or 0 (not a view).
196786*/
196787static void rbuTargetNameFunc(
196788 sqlite3_context *pCtx,
196789 int argc,
196790 sqlite3_value **argv
196791){
196792 sqlite3rbu *p = sqlite3_user_data(pCtx);
196793 const char *zIn;
196794 assert( argc==1 || argc==2 );
196795
196796 zIn = (const char*)sqlite3_value_text(argv[0]);
196797 if( zIn ){
196798 if( rbuIsVacuum(p) ){
196799 assert( argc==2 || argc==1 );
196800 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
196801 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
196802 }
196803 }else{
196804 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
196805 int i;
196806 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
196807 if( zIn[i]=='_' && zIn[i+1] ){
196808 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
196809 }
196810 }
196811 }
196812 }
196813}
196814
196815/*
196816** Initialize the iterator structure passed as the second argument.
196817**
196818** If no error occurs, SQLITE_OK is returned and the iterator is left
196819** pointing to the first entry. Otherwise, an error code and message is
196820** left in the RBU handle passed as the first argument. A copy of the
196821** error code is returned.
196822*/
196823static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
196824 int rc;
196825 memset(pIter, 0, sizeof(RbuObjIter));
196826
196827 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
196828 sqlite3_mprintf(
196829 "SELECT rbu_target_name(name, type='view') AS target, name "
196830 "FROM sqlite_schema "
196831 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
196832 " %s "
196833 "ORDER BY name"
196834 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
196835
196836 if( rc==SQLITE_OK ){
196837 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
196838 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
196839 " FROM main.sqlite_schema "
196840 " WHERE type='index' AND tbl_name = ?"
196841 );
196842 }
196843
196844 pIter->bCleanup = 1;
196845 p->rc = rc;
196846 return rbuObjIterNext(p, pIter);
196847}
196848
196849/*
196850** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
196851** an error code is stored in the RBU handle passed as the first argument.
196852**
196853** If an error has already occurred (p->rc is already set to something other
196854** than SQLITE_OK), then this function returns NULL without modifying the
196855** stored error code. In this case it still calls sqlite3_free() on any
196856** printf() parameters associated with %z conversions.
196857*/
196858static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
196859 char *zSql = 0;
196860 va_list ap;
196861 va_start(ap, zFmt);
196862 zSql = sqlite3_vmprintf(zFmt, ap);
196863 if( p->rc==SQLITE_OK ){
196864 if( zSql==0 ) p->rc = SQLITE_NOMEM;
196865 }else{
196866 sqlite3_free(zSql);
196867 zSql = 0;
196868 }
196869 va_end(ap);
196870 return zSql;
196871}
196872
196873/*
196874** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
196875** arguments are the usual subsitution values. This function performs
196876** the printf() style substitutions and executes the result as an SQL
196877** statement on the RBU handles database.
196878**
196879** If an error occurs, an error code and error message is stored in the
196880** RBU handle. If an error has already occurred when this function is
196881** called, it is a no-op.
196882*/
196883static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
196884 va_list ap;
196885 char *zSql;
196886 va_start(ap, zFmt);
196887 zSql = sqlite3_vmprintf(zFmt, ap);
196888 if( p->rc==SQLITE_OK ){
196889 if( zSql==0 ){
196890 p->rc = SQLITE_NOMEM;
196891 }else{
196892 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
196893 }
196894 }
196895 sqlite3_free(zSql);
196896 va_end(ap);
196897 return p->rc;
196898}
196899
196900/*
196901** Attempt to allocate and return a pointer to a zeroed block of nByte
196902** bytes.
196903**
196904** If an error (i.e. an OOM condition) occurs, return NULL and leave an
196905** error code in the rbu handle passed as the first argument. Or, if an
196906** error has already occurred when this function is called, return NULL
196907** immediately without attempting the allocation or modifying the stored
196908** error code.
196909*/
196910static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
196911 void *pRet = 0;
196912 if( p->rc==SQLITE_OK ){
196913 assert( nByte>0 );
196914 pRet = sqlite3_malloc64(nByte);
196915 if( pRet==0 ){
196916 p->rc = SQLITE_NOMEM;
196917 }else{
196918 memset(pRet, 0, nByte);
196919 }
196920 }
196921 return pRet;
196922}
196923
196924
196925/*
196926** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
196927** there is room for at least nCol elements. If an OOM occurs, store an
196928** error code in the RBU handle passed as the first argument.
196929*/
196930static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
196931 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
196932 char **azNew;
196933
196934 azNew = (char**)rbuMalloc(p, nByte);
196935 if( azNew ){
196936 pIter->azTblCol = azNew;
196937 pIter->azTblType = &azNew[nCol];
196938 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
196939 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
196940 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
196941 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
196942 }
196943}
196944
196945/*
196946** The first argument must be a nul-terminated string. This function
196947** returns a copy of the string in memory obtained from sqlite3_malloc().
196948** It is the responsibility of the caller to eventually free this memory
196949** using sqlite3_free().
196950**
196951** If an OOM condition is encountered when attempting to allocate memory,
196952** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
196953** if the allocation succeeds, (*pRc) is left unchanged.
196954*/
196955static char *rbuStrndup(const char *zStr, int *pRc){
196956 char *zRet = 0;
196957
196958 if( *pRc==SQLITE_OK ){
196959 if( zStr ){
196960 size_t nCopy = strlen(zStr) + 1;
196961 zRet = (char*)sqlite3_malloc64(nCopy);
196962 if( zRet ){
196963 memcpy(zRet, zStr, nCopy);
196964 }else{
196965 *pRc = SQLITE_NOMEM;
196966 }
196967 }
196968 }
196969
196970 return zRet;
196971}
196972
196973/*
196974** Finalize the statement passed as the second argument.
196975**
196976** If the sqlite3_finalize() call indicates that an error occurs, and the
196977** rbu handle error code is not already set, set the error code and error
196978** message accordingly.
196979*/
196980static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
196981 sqlite3 *db = sqlite3_db_handle(pStmt);
196982 int rc = sqlite3_finalize(pStmt);
196983 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
196984 p->rc = rc;
196985 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
196986 }
196987}
196988
196989/* Determine the type of a table.
196990**
196991** peType is of type (int*), a pointer to an output parameter of type
196992** (int). This call sets the output parameter as follows, depending
196993** on the type of the table specified by parameters dbName and zTbl.
196994**
196995** RBU_PK_NOTABLE: No such table.
196996** RBU_PK_NONE: Table has an implicit rowid.
196997** RBU_PK_IPK: Table has an explicit IPK column.
196998** RBU_PK_EXTERNAL: Table has an external PK index.
196999** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
197000** RBU_PK_VTAB: Table is a virtual table.
197001**
197002** Argument *piPk is also of type (int*), and also points to an output
197003** parameter. Unless the table has an external primary key index
197004** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
197005** if the table does have an external primary key index, then *piPk
197006** is set to the root page number of the primary key index before
197007** returning.
197008**
197009** ALGORITHM:
197010**
197011** if( no entry exists in sqlite_schema ){
197012** return RBU_PK_NOTABLE
197013** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
197014** return RBU_PK_VTAB
197015** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
197016** if( the index that is the pk exists in sqlite_schema ){
197017** *piPK = rootpage of that index.
197018** return RBU_PK_EXTERNAL
197019** }else{
197020** return RBU_PK_WITHOUT_ROWID
197021** }
197022** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
197023** return RBU_PK_IPK
197024** }else{
197025** return RBU_PK_NONE
197026** }
197027*/
197028static void rbuTableType(
197029 sqlite3rbu *p,
197030 const char *zTab,
197031 int *peType,
197032 int *piTnum,
197033 int *piPk
197034){
197035 /*
197036 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
197037 ** 1) PRAGMA index_list = ?
197038 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
197039 ** 3) PRAGMA table_info = ?
197040 */
197041 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
197042
197043 *peType = RBU_PK_NOTABLE;
197044 *piPk = 0;
197045
197046 assert( p->rc==SQLITE_OK );
197047 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
197048 sqlite3_mprintf(
197049 "SELECT (sql LIKE 'create virtual%%'), rootpage"
197050 " FROM sqlite_schema"
197051 " WHERE name=%Q", zTab
197052 ));
197053 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
197054 /* Either an error, or no such table. */
197055 goto rbuTableType_end;
197056 }
197057 if( sqlite3_column_int(aStmt[0], 0) ){
197058 *peType = RBU_PK_VTAB; /* virtual table */
197059 goto rbuTableType_end;
197060 }
197061 *piTnum = sqlite3_column_int(aStmt[0], 1);
197062
197063 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
197064 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
197065 );
197066 if( p->rc ) goto rbuTableType_end;
197067 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
197068 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
197069 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
197070 if( zOrig && zIdx && zOrig[0]=='p' ){
197071 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
197072 sqlite3_mprintf(
197073 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
197074 ));
197075 if( p->rc==SQLITE_OK ){
197076 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
197077 *piPk = sqlite3_column_int(aStmt[2], 0);
197078 *peType = RBU_PK_EXTERNAL;
197079 }else{
197080 *peType = RBU_PK_WITHOUT_ROWID;
197081 }
197082 }
197083 goto rbuTableType_end;
197084 }
197085 }
197086
197087 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
197088 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
197089 );
197090 if( p->rc==SQLITE_OK ){
197091 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
197092 if( sqlite3_column_int(aStmt[3],5)>0 ){
197093 *peType = RBU_PK_IPK; /* explicit IPK column */
197094 goto rbuTableType_end;
197095 }
197096 }
197097 *peType = RBU_PK_NONE;
197098 }
197099
197100rbuTableType_end: {
197101 unsigned int i;
197102 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
197103 rbuFinalize(p, aStmt[i]);
197104 }
197105 }
197106}
197107
197108/*
197109** This is a helper function for rbuObjIterCacheTableInfo(). It populates
197110** the pIter->abIndexed[] array.
197111*/
197112static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
197113 sqlite3_stmt *pList = 0;
197114 int bIndex = 0;
197115
197116 if( p->rc==SQLITE_OK ){
197117 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
197118 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
197119 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
197120 );
197121 }
197122
197123 pIter->nIndex = 0;
197124 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
197125 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
197126 int bPartial = sqlite3_column_int(pList, 4);
197127 sqlite3_stmt *pXInfo = 0;
197128 if( zIdx==0 ) break;
197129 if( bPartial ){
197130 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
197131 }
197132 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197133 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
197134 );
197135 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197136 int iCid = sqlite3_column_int(pXInfo, 1);
197137 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
197138 if( iCid==-2 ){
197139 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
197140 }
197141 }
197142 rbuFinalize(p, pXInfo);
197143 bIndex = 1;
197144 pIter->nIndex++;
197145 }
197146
197147 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
197148 /* "PRAGMA index_list" includes the main PK b-tree */
197149 pIter->nIndex--;
197150 }
197151
197152 rbuFinalize(p, pList);
197153 if( bIndex==0 ) pIter->abIndexed = 0;
197154}
197155
197156
197157/*
197158** If they are not already populated, populate the pIter->azTblCol[],
197159** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
197160** the table (not index) that the iterator currently points to.
197161**
197162** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
197163** an error does occur, an error code and error message are also left in
197164** the RBU handle.
197165*/
197166static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
197167 if( pIter->azTblCol==0 ){
197168 sqlite3_stmt *pStmt = 0;
197169 int nCol = 0;
197170 int i; /* for() loop iterator variable */
197171 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
197172 int iOrder = 0;
197173 int iTnum = 0;
197174
197175 /* Figure out the type of table this step will deal with. */
197176 assert( pIter->eType==0 );
197177 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
197178 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
197179 p->rc = SQLITE_ERROR;
197180 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
197181 }
197182 if( p->rc ) return p->rc;
197183 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
197184
197185 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
197186 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
197187 || pIter->eType==RBU_PK_VTAB
197188 );
197189
197190 /* Populate the azTblCol[] and nTblCol variables based on the columns
197191 ** of the input table. Ignore any input table columns that begin with
197192 ** "rbu_". */
197193 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
197194 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
197195 );
197196 if( p->rc==SQLITE_OK ){
197197 nCol = sqlite3_column_count(pStmt);
197198 rbuAllocateIterArrays(p, pIter, nCol);
197199 }
197200 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
197201 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
197202 if( sqlite3_strnicmp("rbu_", zName, 4) ){
197203 char *zCopy = rbuStrndup(zName, &p->rc);
197204 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
197205 pIter->azTblCol[pIter->nTblCol++] = zCopy;
197206 }
197207 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
197208 bRbuRowid = 1;
197209 }
197210 }
197211 sqlite3_finalize(pStmt);
197212 pStmt = 0;
197213
197214 if( p->rc==SQLITE_OK
197215 && rbuIsVacuum(p)==0
197216 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
197217 ){
197218 p->rc = SQLITE_ERROR;
197219 p->zErrmsg = sqlite3_mprintf(
197220 "table %q %s rbu_rowid column", pIter->zDataTbl,
197221 (bRbuRowid ? "may not have" : "requires")
197222 );
197223 }
197224
197225 /* Check that all non-HIDDEN columns in the destination table are also
197226 ** present in the input table. Populate the abTblPk[], azTblType[] and
197227 ** aiTblOrder[] arrays at the same time. */
197228 if( p->rc==SQLITE_OK ){
197229 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
197230 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
197231 );
197232 }
197233 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
197234 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
197235 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
197236 for(i=iOrder; i<pIter->nTblCol; i++){
197237 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
197238 }
197239 if( i==pIter->nTblCol ){
197240 p->rc = SQLITE_ERROR;
197241 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
197242 pIter->zDataTbl, zName
197243 );
197244 }else{
197245 int iPk = sqlite3_column_int(pStmt, 5);
197246 int bNotNull = sqlite3_column_int(pStmt, 3);
197247 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
197248
197249 if( i!=iOrder ){
197250 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
197251 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
197252 }
197253
197254 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
197255 assert( iPk>=0 );
197256 pIter->abTblPk[iOrder] = (u8)iPk;
197257 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
197258 iOrder++;
197259 }
197260 }
197261
197262 rbuFinalize(p, pStmt);
197263 rbuObjIterCacheIndexedCols(p, pIter);
197264 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
197265 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
197266 }
197267
197268 return p->rc;
197269}
197270
197271/*
197272** This function constructs and returns a pointer to a nul-terminated
197273** string containing some SQL clause or list based on one or more of the
197274** column names currently stored in the pIter->azTblCol[] array.
197275*/
197276static char *rbuObjIterGetCollist(
197277 sqlite3rbu *p, /* RBU object */
197278 RbuObjIter *pIter /* Object iterator for column names */
197279){
197280 char *zList = 0;
197281 const char *zSep = "";
197282 int i;
197283 for(i=0; i<pIter->nTblCol; i++){
197284 const char *z = pIter->azTblCol[i];
197285 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
197286 zSep = ", ";
197287 }
197288 return zList;
197289}
197290
197291/*
197292** Return a comma separated list of the quoted PRIMARY KEY column names,
197293** in order, for the current table. Before each column name, add the text
197294** zPre. After each column name, add the zPost text. Use zSeparator as
197295** the separator text (usually ", ").
197296*/
197297static char *rbuObjIterGetPkList(
197298 sqlite3rbu *p, /* RBU object */
197299 RbuObjIter *pIter, /* Object iterator for column names */
197300 const char *zPre, /* Before each quoted column name */
197301 const char *zSeparator, /* Separator to use between columns */
197302 const char *zPost /* After each quoted column name */
197303){
197304 int iPk = 1;
197305 char *zRet = 0;
197306 const char *zSep = "";
197307 while( 1 ){
197308 int i;
197309 for(i=0; i<pIter->nTblCol; i++){
197310 if( (int)pIter->abTblPk[i]==iPk ){
197311 const char *zCol = pIter->azTblCol[i];
197312 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
197313 zSep = zSeparator;
197314 break;
197315 }
197316 }
197317 if( i==pIter->nTblCol ) break;
197318 iPk++;
197319 }
197320 return zRet;
197321}
197322
197323/*
197324** This function is called as part of restarting an RBU vacuum within
197325** stage 1 of the process (while the *-oal file is being built) while
197326** updating a table (not an index). The table may be a rowid table or
197327** a WITHOUT ROWID table. It queries the target database to find the
197328** largest key that has already been written to the target table and
197329** constructs a WHERE clause that can be used to extract the remaining
197330** rows from the source table. For a rowid table, the WHERE clause
197331** is of the form:
197332**
197333** "WHERE _rowid_ > ?"
197334**
197335** and for WITHOUT ROWID tables:
197336**
197337** "WHERE (key1, key2) > (?, ?)"
197338**
197339** Instead of "?" placeholders, the actual WHERE clauses created by
197340** this function contain literal SQL values.
197341*/
197342static char *rbuVacuumTableStart(
197343 sqlite3rbu *p, /* RBU handle */
197344 RbuObjIter *pIter, /* RBU iterator object */
197345 int bRowid, /* True for a rowid table */
197346 const char *zWrite /* Target table name prefix */
197347){
197348 sqlite3_stmt *pMax = 0;
197349 char *zRet = 0;
197350 if( bRowid ){
197351 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
197352 sqlite3_mprintf(
197353 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
197354 )
197355 );
197356 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
197357 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
197358 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
197359 }
197360 rbuFinalize(p, pMax);
197361 }else{
197362 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
197363 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
197364 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
197365
197366 if( p->rc==SQLITE_OK ){
197367 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
197368 sqlite3_mprintf(
197369 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
197370 zSelect, zWrite, pIter->zTbl, zOrder
197371 )
197372 );
197373 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
197374 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
197375 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
197376 }
197377 rbuFinalize(p, pMax);
197378 }
197379
197380 sqlite3_free(zOrder);
197381 sqlite3_free(zSelect);
197382 sqlite3_free(zList);
197383 }
197384 return zRet;
197385}
197386
197387/*
197388** This function is called as part of restating an RBU vacuum when the
197389** current operation is writing content to an index. If possible, it
197390** queries the target index b-tree for the largest key already written to
197391** it, then composes and returns an expression that can be used in a WHERE
197392** clause to select the remaining required rows from the source table.
197393** It is only possible to return such an expression if:
197394**
197395** * The index contains no DESC columns, and
197396** * The last key written to the index before the operation was
197397** suspended does not contain any NULL values.
197398**
197399** The expression is of the form:
197400**
197401** (index-field1, index-field2, ...) > (?, ?, ...)
197402**
197403** except that the "?" placeholders are replaced with literal values.
197404**
197405** If the expression cannot be created, NULL is returned. In this case,
197406** the caller has to use an OFFSET clause to extract only the required
197407** rows from the sourct table, just as it does for an RBU update operation.
197408*/
197409char *rbuVacuumIndexStart(
197410 sqlite3rbu *p, /* RBU handle */
197411 RbuObjIter *pIter /* RBU iterator object */
197412){
197413 char *zOrder = 0;
197414 char *zLhs = 0;
197415 char *zSelect = 0;
197416 char *zVector = 0;
197417 char *zRet = 0;
197418 int bFailed = 0;
197419 const char *zSep = "";
197420 int iCol = 0;
197421 sqlite3_stmt *pXInfo = 0;
197422
197423 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197424 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
197425 );
197426 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197427 int iCid = sqlite3_column_int(pXInfo, 1);
197428 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
197429 const char *zCol;
197430 if( sqlite3_column_int(pXInfo, 3) ){
197431 bFailed = 1;
197432 break;
197433 }
197434
197435 if( iCid<0 ){
197436 if( pIter->eType==RBU_PK_IPK ){
197437 int i;
197438 for(i=0; pIter->abTblPk[i]==0; i++);
197439 assert( i<pIter->nTblCol );
197440 zCol = pIter->azTblCol[i];
197441 }else{
197442 zCol = "_rowid_";
197443 }
197444 }else{
197445 zCol = pIter->azTblCol[iCid];
197446 }
197447
197448 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
197449 zLhs, zSep, zCol, zCollate
197450 );
197451 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
197452 zOrder, zSep, iCol, zCol, zCollate
197453 );
197454 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
197455 zSelect, zSep, iCol, zCol
197456 );
197457 zSep = ", ";
197458 iCol++;
197459 }
197460 rbuFinalize(p, pXInfo);
197461 if( bFailed ) goto index_start_out;
197462
197463 if( p->rc==SQLITE_OK ){
197464 sqlite3_stmt *pSel = 0;
197465
197466 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
197467 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
197468 zSelect, pIter->zTbl, zOrder
197469 )
197470 );
197471 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
197472 zSep = "";
197473 for(iCol=0; iCol<pIter->nCol; iCol++){
197474 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
197475 if( zQuoted[0]=='N' ){
197476 bFailed = 1;
197477 break;
197478 }
197479 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
197480 zSep = ", ";
197481 }
197482
197483 if( !bFailed ){
197484 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
197485 }
197486 }
197487 rbuFinalize(p, pSel);
197488 }
197489
197490 index_start_out:
197491 sqlite3_free(zOrder);
197492 sqlite3_free(zSelect);
197493 sqlite3_free(zVector);
197494 sqlite3_free(zLhs);
197495 return zRet;
197496}
197497
197498/*
197499** This function is used to create a SELECT list (the list of SQL
197500** expressions that follows a SELECT keyword) for a SELECT statement
197501** used to read from an data_xxx or rbu_tmp_xxx table while updating the
197502** index object currently indicated by the iterator object passed as the
197503** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
197504** to obtain the required information.
197505**
197506** If the index is of the following form:
197507**
197508** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
197509**
197510** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
197511** "ipk", the returned string is:
197512**
197513** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
197514**
197515** As well as the returned string, three other malloc'd strings are
197516** returned via output parameters. As follows:
197517**
197518** pzImposterCols: ...
197519** pzImposterPk: ...
197520** pzWhere: ...
197521*/
197522static char *rbuObjIterGetIndexCols(
197523 sqlite3rbu *p, /* RBU object */
197524 RbuObjIter *pIter, /* Object iterator for column names */
197525 char **pzImposterCols, /* OUT: Columns for imposter table */
197526 char **pzImposterPk, /* OUT: Imposter PK clause */
197527 char **pzWhere, /* OUT: WHERE clause */
197528 int *pnBind /* OUT: Trbul number of columns */
197529){
197530 int rc = p->rc; /* Error code */
197531 int rc2; /* sqlite3_finalize() return code */
197532 char *zRet = 0; /* String to return */
197533 char *zImpCols = 0; /* String to return via *pzImposterCols */
197534 char *zImpPK = 0; /* String to return via *pzImposterPK */
197535 char *zWhere = 0; /* String to return via *pzWhere */
197536 int nBind = 0; /* Value to return via *pnBind */
197537 const char *zCom = ""; /* Set to ", " later on */
197538 const char *zAnd = ""; /* Set to " AND " later on */
197539 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
197540
197541 if( rc==SQLITE_OK ){
197542 assert( p->zErrmsg==0 );
197543 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197544 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
197545 );
197546 }
197547
197548 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197549 int iCid = sqlite3_column_int(pXInfo, 1);
197550 int bDesc = sqlite3_column_int(pXInfo, 3);
197551 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
197552 const char *zCol = 0;
197553 const char *zType;
197554
197555 if( iCid==-2 ){
197556 int iSeq = sqlite3_column_int(pXInfo, 0);
197557 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
197558 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
197559 );
197560 zType = "";
197561 }else {
197562 if( iCid<0 ){
197563 /* An integer primary key. If the table has an explicit IPK, use
197564 ** its name. Otherwise, use "rbu_rowid". */
197565 if( pIter->eType==RBU_PK_IPK ){
197566 int i;
197567 for(i=0; pIter->abTblPk[i]==0; i++);
197568 assert( i<pIter->nTblCol );
197569 zCol = pIter->azTblCol[i];
197570 }else if( rbuIsVacuum(p) ){
197571 zCol = "_rowid_";
197572 }else{
197573 zCol = "rbu_rowid";
197574 }
197575 zType = "INTEGER";
197576 }else{
197577 zCol = pIter->azTblCol[iCid];
197578 zType = pIter->azTblType[iCid];
197579 }
197580 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
197581 }
197582
197583 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
197584 const char *zOrder = (bDesc ? " DESC" : "");
197585 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
197586 zImpPK, zCom, nBind, zCol, zOrder
197587 );
197588 }
197589 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
197590 zImpCols, zCom, nBind, zCol, zType, zCollate
197591 );
197592 zWhere = sqlite3_mprintf(
197593 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
197594 );
197595 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
197596 zCom = ", ";
197597 zAnd = " AND ";
197598 nBind++;
197599 }
197600
197601 rc2 = sqlite3_finalize(pXInfo);
197602 if( rc==SQLITE_OK ) rc = rc2;
197603
197604 if( rc!=SQLITE_OK ){
197605 sqlite3_free(zRet);
197606 sqlite3_free(zImpCols);
197607 sqlite3_free(zImpPK);
197608 sqlite3_free(zWhere);
197609 zRet = 0;
197610 zImpCols = 0;
197611 zImpPK = 0;
197612 zWhere = 0;
197613 p->rc = rc;
197614 }
197615
197616 *pzImposterCols = zImpCols;
197617 *pzImposterPk = zImpPK;
197618 *pzWhere = zWhere;
197619 *pnBind = nBind;
197620 return zRet;
197621}
197622
197623/*
197624** Assuming the current table columns are "a", "b" and "c", and the zObj
197625** paramter is passed "old", return a string of the form:
197626**
197627** "old.a, old.b, old.b"
197628**
197629** With the column names escaped.
197630**
197631** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
197632** the text ", old._rowid_" to the returned value.
197633*/
197634static char *rbuObjIterGetOldlist(
197635 sqlite3rbu *p,
197636 RbuObjIter *pIter,
197637 const char *zObj
197638){
197639 char *zList = 0;
197640 if( p->rc==SQLITE_OK && pIter->abIndexed ){
197641 const char *zS = "";
197642 int i;
197643 for(i=0; i<pIter->nTblCol; i++){
197644 if( pIter->abIndexed[i] ){
197645 const char *zCol = pIter->azTblCol[i];
197646 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
197647 }else{
197648 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
197649 }
197650 zS = ", ";
197651 if( zList==0 ){
197652 p->rc = SQLITE_NOMEM;
197653 break;
197654 }
197655 }
197656
197657 /* For a table with implicit rowids, append "old._rowid_" to the list. */
197658 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
197659 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
197660 }
197661 }
197662 return zList;
197663}
197664
197665/*
197666** Return an expression that can be used in a WHERE clause to match the
197667** primary key of the current table. For example, if the table is:
197668**
197669** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
197670**
197671** Return the string:
197672**
197673** "b = ?1 AND c = ?2"
197674*/
197675static char *rbuObjIterGetWhere(
197676 sqlite3rbu *p,
197677 RbuObjIter *pIter
197678){
197679 char *zList = 0;
197680 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
197681 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
197682 }else if( pIter->eType==RBU_PK_EXTERNAL ){
197683 const char *zSep = "";
197684 int i;
197685 for(i=0; i<pIter->nTblCol; i++){
197686 if( pIter->abTblPk[i] ){
197687 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
197688 zSep = " AND ";
197689 }
197690 }
197691 zList = rbuMPrintf(p,
197692 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
197693 );
197694
197695 }else{
197696 const char *zSep = "";
197697 int i;
197698 for(i=0; i<pIter->nTblCol; i++){
197699 if( pIter->abTblPk[i] ){
197700 const char *zCol = pIter->azTblCol[i];
197701 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
197702 zSep = " AND ";
197703 }
197704 }
197705 }
197706 return zList;
197707}
197708
197709/*
197710** The SELECT statement iterating through the keys for the current object
197711** (p->objiter.pSelect) currently points to a valid row. However, there
197712** is something wrong with the rbu_control value in the rbu_control value
197713** stored in the (p->nCol+1)'th column. Set the error code and error message
197714** of the RBU handle to something reflecting this.
197715*/
197716static void rbuBadControlError(sqlite3rbu *p){
197717 p->rc = SQLITE_ERROR;
197718 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
197719}
197720
197721
197722/*
197723** Return a nul-terminated string containing the comma separated list of
197724** assignments that should be included following the "SET" keyword of
197725** an UPDATE statement used to update the table object that the iterator
197726** passed as the second argument currently points to if the rbu_control
197727** column of the data_xxx table entry is set to zMask.
197728**
197729** The memory for the returned string is obtained from sqlite3_malloc().
197730** It is the responsibility of the caller to eventually free it using
197731** sqlite3_free().
197732**
197733** If an OOM error is encountered when allocating space for the new
197734** string, an error code is left in the rbu handle passed as the first
197735** argument and NULL is returned. Or, if an error has already occurred
197736** when this function is called, NULL is returned immediately, without
197737** attempting the allocation or modifying the stored error code.
197738*/
197739static char *rbuObjIterGetSetlist(
197740 sqlite3rbu *p,
197741 RbuObjIter *pIter,
197742 const char *zMask
197743){
197744 char *zList = 0;
197745 if( p->rc==SQLITE_OK ){
197746 int i;
197747
197748 if( (int)strlen(zMask)!=pIter->nTblCol ){
197749 rbuBadControlError(p);
197750 }else{
197751 const char *zSep = "";
197752 for(i=0; i<pIter->nTblCol; i++){
197753 char c = zMask[pIter->aiSrcOrder[i]];
197754 if( c=='x' ){
197755 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
197756 zList, zSep, pIter->azTblCol[i], i+1
197757 );
197758 zSep = ", ";
197759 }
197760 else if( c=='d' ){
197761 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
197762 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
197763 );
197764 zSep = ", ";
197765 }
197766 else if( c=='f' ){
197767 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
197768 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
197769 );
197770 zSep = ", ";
197771 }
197772 }
197773 }
197774 }
197775 return zList;
197776}
197777
197778/*
197779** Return a nul-terminated string consisting of nByte comma separated
197780** "?" expressions. For example, if nByte is 3, return a pointer to
197781** a buffer containing the string "?,?,?".
197782**
197783** The memory for the returned string is obtained from sqlite3_malloc().
197784** It is the responsibility of the caller to eventually free it using
197785** sqlite3_free().
197786**
197787** If an OOM error is encountered when allocating space for the new
197788** string, an error code is left in the rbu handle passed as the first
197789** argument and NULL is returned. Or, if an error has already occurred
197790** when this function is called, NULL is returned immediately, without
197791** attempting the allocation or modifying the stored error code.
197792*/
197793static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
197794 char *zRet = 0;
197795 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
197796
197797 zRet = (char*)rbuMalloc(p, nByte);
197798 if( zRet ){
197799 int i;
197800 for(i=0; i<nBind; i++){
197801 zRet[i*2] = '?';
197802 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
197803 }
197804 }
197805 return zRet;
197806}
197807
197808/*
197809** The iterator currently points to a table (not index) of type
197810** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
197811** declaration for the corresponding imposter table. For example,
197812** if the iterator points to a table created as:
197813**
197814** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
197815**
197816** this function returns:
197817**
197818** PRIMARY KEY("b", "a" DESC)
197819*/
197820static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
197821 char *z = 0;
197822 assert( pIter->zIdx==0 );
197823 if( p->rc==SQLITE_OK ){
197824 const char *zSep = "PRIMARY KEY(";
197825 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
197826 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
197827
197828 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
197829 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
197830 );
197831 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
197832 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
197833 if( zOrig && strcmp(zOrig, "pk")==0 ){
197834 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
197835 if( zIdx ){
197836 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197837 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
197838 );
197839 }
197840 break;
197841 }
197842 }
197843 rbuFinalize(p, pXList);
197844
197845 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197846 if( sqlite3_column_int(pXInfo, 5) ){
197847 /* int iCid = sqlite3_column_int(pXInfo, 0); */
197848 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
197849 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
197850 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
197851 zSep = ", ";
197852 }
197853 }
197854 z = rbuMPrintf(p, "%z)", z);
197855 rbuFinalize(p, pXInfo);
197856 }
197857 return z;
197858}
197859
197860/*
197861** This function creates the second imposter table used when writing to
197862** a table b-tree where the table has an external primary key. If the
197863** iterator passed as the second argument does not currently point to
197864** a table (not index) with an external primary key, this function is a
197865** no-op.
197866**
197867** Assuming the iterator does point to a table with an external PK, this
197868** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
197869** used to access that PK index. For example, if the target table is
197870** declared as follows:
197871**
197872** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
197873**
197874** then the imposter table schema is:
197875**
197876** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
197877**
197878*/
197879static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
197880 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
197881 int tnum = pIter->iPkTnum; /* Root page of PK index */
197882 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
197883 const char *zIdx = 0; /* Name of PK index */
197884 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
197885 const char *zComma = "";
197886 char *zCols = 0; /* Used to build up list of table cols */
197887 char *zPk = 0; /* Used to build up table PK declaration */
197888
197889 /* Figure out the name of the primary key index for the current table.
197890 ** This is needed for the argument to "PRAGMA index_xinfo". Set
197891 ** zIdx to point to a nul-terminated string containing this name. */
197892 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
197893 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
197894 );
197895 if( p->rc==SQLITE_OK ){
197896 sqlite3_bind_int(pQuery, 1, tnum);
197897 if( SQLITE_ROW==sqlite3_step(pQuery) ){
197898 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
197899 }
197900 }
197901 if( zIdx ){
197902 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
197903 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
197904 );
197905 }
197906 rbuFinalize(p, pQuery);
197907
197908 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
197909 int bKey = sqlite3_column_int(pXInfo, 5);
197910 if( bKey ){
197911 int iCid = sqlite3_column_int(pXInfo, 1);
197912 int bDesc = sqlite3_column_int(pXInfo, 3);
197913 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
197914 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
197915 iCid, pIter->azTblType[iCid], zCollate
197916 );
197917 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
197918 zComma = ", ";
197919 }
197920 }
197921 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
197922 rbuFinalize(p, pXInfo);
197923
197924 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
197925 rbuMPrintfExec(p, p->dbMain,
197926 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
197927 zCols, zPk
197928 );
197929 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
197930 }
197931}
197932
197933/*
197934** If an error has already occurred when this function is called, it
197935** immediately returns zero (without doing any work). Or, if an error
197936** occurs during the execution of this function, it sets the error code
197937** in the sqlite3rbu object indicated by the first argument and returns
197938** zero.
197939**
197940** The iterator passed as the second argument is guaranteed to point to
197941** a table (not an index) when this function is called. This function
197942** attempts to create any imposter table required to write to the main
197943** table b-tree of the table before returning. Non-zero is returned if
197944** an imposter table are created, or zero otherwise.
197945**
197946** An imposter table is required in all cases except RBU_PK_VTAB. Only
197947** virtual tables are written to directly. The imposter table has the
197948** same schema as the actual target table (less any UNIQUE constraints).
197949** More precisely, the "same schema" means the same columns, types,
197950** collation sequences. For tables that do not have an external PRIMARY
197951** KEY, it also means the same PRIMARY KEY declaration.
197952*/
197953static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
197954 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
197955 int tnum = pIter->iTnum;
197956 const char *zComma = "";
197957 char *zSql = 0;
197958 int iCol;
197959 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
197960
197961 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
197962 const char *zPk = "";
197963 const char *zCol = pIter->azTblCol[iCol];
197964 const char *zColl = 0;
197965
197966 p->rc = sqlite3_table_column_metadata(
197967 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
197968 );
197969
197970 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
197971 /* If the target table column is an "INTEGER PRIMARY KEY", add
197972 ** "PRIMARY KEY" to the imposter table column declaration. */
197973 zPk = "PRIMARY KEY ";
197974 }
197975 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
197976 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
197977 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
197978 );
197979 zComma = ", ";
197980 }
197981
197982 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
197983 char *zPk = rbuWithoutRowidPK(p, pIter);
197984 if( zPk ){
197985 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
197986 }
197987 }
197988
197989 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
197990 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
197991 pIter->zTbl, zSql,
197992 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
197993 );
197994 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
197995 }
197996}
197997
197998/*
197999** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
198000** Specifically a statement of the form:
198001**
198002** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
198003**
198004** The number of bound variables is equal to the number of columns in
198005** the target table, plus one (for the rbu_control column), plus one more
198006** (for the rbu_rowid column) if the target table is an implicit IPK or
198007** virtual table.
198008*/
198009static void rbuObjIterPrepareTmpInsert(
198010 sqlite3rbu *p,
198011 RbuObjIter *pIter,
198012 const char *zCollist,
198013 const char *zRbuRowid
198014){
198015 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
198016 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
198017 if( zBind ){
198018 assert( pIter->pTmpInsert==0 );
198019 p->rc = prepareFreeAndCollectError(
198020 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
198021 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
198022 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
198023 ));
198024 }
198025}
198026
198027static void rbuTmpInsertFunc(
198028 sqlite3_context *pCtx,
198029 int nVal,
198030 sqlite3_value **apVal
198031){
198032 sqlite3rbu *p = sqlite3_user_data(pCtx);
198033 int rc = SQLITE_OK;
198034 int i;
198035
198036 assert( sqlite3_value_int(apVal[0])!=0
198037 || p->objiter.eType==RBU_PK_EXTERNAL
198038 || p->objiter.eType==RBU_PK_NONE
198039 );
198040 if( sqlite3_value_int(apVal[0])!=0 ){
198041 p->nPhaseOneStep += p->objiter.nIndex;
198042 }
198043
198044 for(i=0; rc==SQLITE_OK && i<nVal; i++){
198045 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
198046 }
198047 if( rc==SQLITE_OK ){
198048 sqlite3_step(p->objiter.pTmpInsert);
198049 rc = sqlite3_reset(p->objiter.pTmpInsert);
198050 }
198051
198052 if( rc!=SQLITE_OK ){
198053 sqlite3_result_error_code(pCtx, rc);
198054 }
198055}
198056
198057static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
198058 sqlite3_stmt *pStmt = 0;
198059 int rc = p->rc;
198060 char *zRet = 0;
198061
198062 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
198063
198064 if( rc==SQLITE_OK ){
198065 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
198066 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
198067 );
198068 }
198069 if( rc==SQLITE_OK ){
198070 int rc2;
198071 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
198072 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
198073 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
198074 if( zSql ){
198075 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
198076 }
198077 if( zSql ){
198078 int nParen = 0; /* Number of open parenthesis */
198079 int i;
198080 int iIdxCol = 0;
198081 int nIdxAlloc = 0;
198082 for(i=0; zSql[i]; i++){
198083 char c = zSql[i];
198084
198085 /* If necessary, grow the pIter->aIdxCol[] array */
198086 if( iIdxCol==nIdxAlloc ){
198087 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
198088 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
198089 );
198090 if( aIdxCol==0 ){
198091 rc = SQLITE_NOMEM;
198092 break;
198093 }
198094 pIter->aIdxCol = aIdxCol;
198095 nIdxAlloc += 16;
198096 }
198097
198098 if( c=='(' ){
198099 if( nParen==0 ){
198100 assert( iIdxCol==0 );
198101 pIter->aIdxCol[0].zSpan = &zSql[i+1];
198102 }
198103 nParen++;
198104 }
198105 else if( c==')' ){
198106 nParen--;
198107 if( nParen==0 ){
198108 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
198109 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
198110 i++;
198111 break;
198112 }
198113 }else if( c==',' && nParen==1 ){
198114 int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
198115 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
198116 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
198117 }else if( c=='"' || c=='\'' || c=='`' ){
198118 for(i++; 1; i++){
198119 if( zSql[i]==c ){
198120 if( zSql[i+1]!=c ) break;
198121 i++;
198122 }
198123 }
198124 }else if( c=='[' ){
198125 for(i++; 1; i++){
198126 if( zSql[i]==']' ) break;
198127 }
198128 }else if( c=='-' && zSql[i+1]=='-' ){
198129 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
198130 if( zSql[i]=='\0' ) break;
198131 }else if( c=='/' && zSql[i+1]=='*' ){
198132 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
198133 if( zSql[i]=='\0' ) break;
198134 i++;
198135 }
198136 }
198137 if( zSql[i] ){
198138 zRet = rbuStrndup(&zSql[i], &rc);
198139 }
198140 pIter->nIdxCol = iIdxCol;
198141 }
198142 }
198143
198144 rc2 = sqlite3_finalize(pStmt);
198145 if( rc==SQLITE_OK ) rc = rc2;
198146 }
198147
198148 p->rc = rc;
198149 return zRet;
198150}
198151
198152/*
198153** Ensure that the SQLite statement handles required to update the
198154** target database object currently indicated by the iterator passed
198155** as the second argument are available.
198156*/
198157static int rbuObjIterPrepareAll(
198158 sqlite3rbu *p,
198159 RbuObjIter *pIter,
198160 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
198161){
198162 assert( pIter->bCleanup==0 );
198163 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
198164 const int tnum = pIter->iTnum;
198165 char *zCollist = 0; /* List of indexed columns */
198166 char **pz = &p->zErrmsg;
198167 const char *zIdx = pIter->zIdx;
198168 char *zLimit = 0;
198169
198170 if( nOffset ){
198171 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
198172 if( !zLimit ) p->rc = SQLITE_NOMEM;
198173 }
198174
198175 if( zIdx ){
198176 const char *zTbl = pIter->zTbl;
198177 char *zImposterCols = 0; /* Columns for imposter table */
198178 char *zImposterPK = 0; /* Primary key declaration for imposter */
198179 char *zWhere = 0; /* WHERE clause on PK columns */
198180 char *zBind = 0;
198181 char *zPart = 0;
198182 int nBind = 0;
198183
198184 assert( pIter->eType!=RBU_PK_VTAB );
198185 zPart = rbuObjIterGetIndexWhere(p, pIter);
198186 zCollist = rbuObjIterGetIndexCols(
198187 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
198188 );
198189 zBind = rbuObjIterGetBindlist(p, nBind);
198190
198191 /* Create the imposter table used to write to this index. */
198192 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
198193 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
198194 rbuMPrintfExec(p, p->dbMain,
198195 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
198196 zTbl, zImposterCols, zImposterPK
198197 );
198198 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
198199
198200 /* Create the statement to insert index entries */
198201 pIter->nCol = nBind;
198202 if( p->rc==SQLITE_OK ){
198203 p->rc = prepareFreeAndCollectError(
198204 p->dbMain, &pIter->pInsert, &p->zErrmsg,
198205 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
198206 );
198207 }
198208
198209 /* And to delete index entries */
198210 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
198211 p->rc = prepareFreeAndCollectError(
198212 p->dbMain, &pIter->pDelete, &p->zErrmsg,
198213 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
198214 );
198215 }
198216
198217 /* Create the SELECT statement to read keys in sorted order */
198218 if( p->rc==SQLITE_OK ){
198219 char *zSql;
198220 if( rbuIsVacuum(p) ){
198221 char *zStart = 0;
198222 if( nOffset ){
198223 zStart = rbuVacuumIndexStart(p, pIter);
198224 if( zStart ){
198225 sqlite3_free(zLimit);
198226 zLimit = 0;
198227 }
198228 }
198229
198230 zSql = sqlite3_mprintf(
198231 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
198232 zCollist,
198233 pIter->zDataTbl,
198234 zPart,
198235 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
198236 zCollist, zLimit
198237 );
198238 sqlite3_free(zStart);
198239 }else
198240
198241 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
198242 zSql = sqlite3_mprintf(
198243 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
198244 zCollist, p->zStateDb, pIter->zDataTbl,
198245 zPart, zCollist, zLimit
198246 );
198247 }else{
198248 zSql = sqlite3_mprintf(
198249 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
198250 "UNION ALL "
198251 "SELECT %s, rbu_control FROM '%q' "
198252 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
198253 "ORDER BY %s%s",
198254 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
198255 zCollist, pIter->zDataTbl,
198256 zPart,
198257 (zPart ? "AND" : "WHERE"),
198258 zCollist, zLimit
198259 );
198260 }
198261 if( p->rc==SQLITE_OK ){
198262 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
198263 }else{
198264 sqlite3_free(zSql);
198265 }
198266 }
198267
198268 sqlite3_free(zImposterCols);
198269 sqlite3_free(zImposterPK);
198270 sqlite3_free(zWhere);
198271 sqlite3_free(zBind);
198272 sqlite3_free(zPart);
198273 }else{
198274 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
198275 ||(pIter->eType==RBU_PK_NONE)
198276 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
198277 const char *zTbl = pIter->zTbl; /* Table this step applies to */
198278 const char *zWrite; /* Imposter table name */
198279
198280 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
198281 char *zWhere = rbuObjIterGetWhere(p, pIter);
198282 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
198283 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
198284
198285 zCollist = rbuObjIterGetCollist(p, pIter);
198286 pIter->nCol = pIter->nTblCol;
198287
198288 /* Create the imposter table or tables (if required). */
198289 rbuCreateImposterTable(p, pIter);
198290 rbuCreateImposterTable2(p, pIter);
198291 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
198292
198293 /* Create the INSERT statement to write to the target PK b-tree */
198294 if( p->rc==SQLITE_OK ){
198295 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
198296 sqlite3_mprintf(
198297 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
198298 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
198299 )
198300 );
198301 }
198302
198303 /* Create the DELETE statement to write to the target PK b-tree.
198304 ** Because it only performs INSERT operations, this is not required for
198305 ** an rbu vacuum handle. */
198306 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
198307 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
198308 sqlite3_mprintf(
198309 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
198310 )
198311 );
198312 }
198313
198314 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
198315 const char *zRbuRowid = "";
198316 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
198317 zRbuRowid = ", rbu_rowid";
198318 }
198319
198320 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
198321 rbuMPrintfExec(p, p->dbRbu,
198322 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
198323 "SELECT *%s FROM '%q' WHERE 0;"
198324 , p->zStateDb, pIter->zDataTbl
198325 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
198326 , pIter->zDataTbl
198327 );
198328
198329 rbuMPrintfExec(p, p->dbMain,
198330 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
198331 "BEGIN "
198332 " SELECT rbu_tmp_insert(3, %s);"
198333 "END;"
198334
198335 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
198336 "BEGIN "
198337 " SELECT rbu_tmp_insert(3, %s);"
198338 "END;"
198339
198340 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
198341 "BEGIN "
198342 " SELECT rbu_tmp_insert(4, %s);"
198343 "END;",
198344 zWrite, zTbl, zOldlist,
198345 zWrite, zTbl, zOldlist,
198346 zWrite, zTbl, zNewlist
198347 );
198348
198349 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
198350 rbuMPrintfExec(p, p->dbMain,
198351 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
198352 "BEGIN "
198353 " SELECT rbu_tmp_insert(0, %s);"
198354 "END;",
198355 zWrite, zTbl, zNewlist
198356 );
198357 }
198358
198359 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
198360 }
198361
198362 /* Create the SELECT statement to read keys from data_xxx */
198363 if( p->rc==SQLITE_OK ){
198364 const char *zRbuRowid = "";
198365 char *zStart = 0;
198366 char *zOrder = 0;
198367 if( bRbuRowid ){
198368 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
198369 }
198370
198371 if( rbuIsVacuum(p) ){
198372 if( nOffset ){
198373 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
198374 if( zStart ){
198375 sqlite3_free(zLimit);
198376 zLimit = 0;
198377 }
198378 }
198379 if( bRbuRowid ){
198380 zOrder = rbuMPrintf(p, "_rowid_");
198381 }else{
198382 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
198383 }
198384 }
198385
198386 if( p->rc==SQLITE_OK ){
198387 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
198388 sqlite3_mprintf(
198389 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
198390 zCollist,
198391 (rbuIsVacuum(p) ? "0 AS " : ""),
198392 zRbuRowid,
198393 pIter->zDataTbl, (zStart ? zStart : ""),
198394 (zOrder ? "ORDER BY" : ""), zOrder,
198395 zLimit
198396 )
198397 );
198398 }
198399 sqlite3_free(zStart);
198400 sqlite3_free(zOrder);
198401 }
198402
198403 sqlite3_free(zWhere);
198404 sqlite3_free(zOldlist);
198405 sqlite3_free(zNewlist);
198406 sqlite3_free(zBindings);
198407 }
198408 sqlite3_free(zCollist);
198409 sqlite3_free(zLimit);
198410 }
198411
198412 return p->rc;
198413}
198414
198415/*
198416** Set output variable *ppStmt to point to an UPDATE statement that may
198417** be used to update the imposter table for the main table b-tree of the
198418** table object that pIter currently points to, assuming that the
198419** rbu_control column of the data_xyz table contains zMask.
198420**
198421** If the zMask string does not specify any columns to update, then this
198422** is not an error. Output variable *ppStmt is set to NULL in this case.
198423*/
198424static int rbuGetUpdateStmt(
198425 sqlite3rbu *p, /* RBU handle */
198426 RbuObjIter *pIter, /* Object iterator */
198427 const char *zMask, /* rbu_control value ('x.x.') */
198428 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
198429){
198430 RbuUpdateStmt **pp;
198431 RbuUpdateStmt *pUp = 0;
198432 int nUp = 0;
198433
198434 /* In case an error occurs */
198435 *ppStmt = 0;
198436
198437 /* Search for an existing statement. If one is found, shift it to the front
198438 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
198439 ** to the number of statements currently in the cache and pUp to the
198440 ** last object in the list. */
198441 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
198442 pUp = *pp;
198443 if( strcmp(pUp->zMask, zMask)==0 ){
198444 *pp = pUp->pNext;
198445 pUp->pNext = pIter->pRbuUpdate;
198446 pIter->pRbuUpdate = pUp;
198447 *ppStmt = pUp->pUpdate;
198448 return SQLITE_OK;
198449 }
198450 nUp++;
198451 }
198452 assert( pUp==0 || pUp->pNext==0 );
198453
198454 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
198455 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
198456 *pp = 0;
198457 sqlite3_finalize(pUp->pUpdate);
198458 pUp->pUpdate = 0;
198459 }else{
198460 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
198461 }
198462
198463 if( pUp ){
198464 char *zWhere = rbuObjIterGetWhere(p, pIter);
198465 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
198466 char *zUpdate = 0;
198467
198468 pUp->zMask = (char*)&pUp[1];
198469 memcpy(pUp->zMask, zMask, pIter->nTblCol);
198470 pUp->pNext = pIter->pRbuUpdate;
198471 pIter->pRbuUpdate = pUp;
198472
198473 if( zSet ){
198474 const char *zPrefix = "";
198475
198476 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
198477 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
198478 zPrefix, pIter->zTbl, zSet, zWhere
198479 );
198480 p->rc = prepareFreeAndCollectError(
198481 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
198482 );
198483 *ppStmt = pUp->pUpdate;
198484 }
198485 sqlite3_free(zWhere);
198486 sqlite3_free(zSet);
198487 }
198488
198489 return p->rc;
198490}
198491
198492static sqlite3 *rbuOpenDbhandle(
198493 sqlite3rbu *p,
198494 const char *zName,
198495 int bUseVfs
198496){
198497 sqlite3 *db = 0;
198498 if( p->rc==SQLITE_OK ){
198499 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
198500 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
198501 if( p->rc ){
198502 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
198503 sqlite3_close(db);
198504 db = 0;
198505 }
198506 }
198507 return db;
198508}
198509
198510/*
198511** Free an RbuState object allocated by rbuLoadState().
198512*/
198513static void rbuFreeState(RbuState *p){
198514 if( p ){
198515 sqlite3_free(p->zTbl);
198516 sqlite3_free(p->zDataTbl);
198517 sqlite3_free(p->zIdx);
198518 sqlite3_free(p);
198519 }
198520}
198521
198522/*
198523** Allocate an RbuState object and load the contents of the rbu_state
198524** table into it. Return a pointer to the new object. It is the
198525** responsibility of the caller to eventually free the object using
198526** sqlite3_free().
198527**
198528** If an error occurs, leave an error code and message in the rbu handle
198529** and return NULL.
198530*/
198531static RbuState *rbuLoadState(sqlite3rbu *p){
198532 RbuState *pRet = 0;
198533 sqlite3_stmt *pStmt = 0;
198534 int rc;
198535 int rc2;
198536
198537 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
198538 if( pRet==0 ) return 0;
198539
198540 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
198541 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
198542 );
198543 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
198544 switch( sqlite3_column_int(pStmt, 0) ){
198545 case RBU_STATE_STAGE:
198546 pRet->eStage = sqlite3_column_int(pStmt, 1);
198547 if( pRet->eStage!=RBU_STAGE_OAL
198548 && pRet->eStage!=RBU_STAGE_MOVE
198549 && pRet->eStage!=RBU_STAGE_CKPT
198550 ){
198551 p->rc = SQLITE_CORRUPT;
198552 }
198553 break;
198554
198555 case RBU_STATE_TBL:
198556 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
198557 break;
198558
198559 case RBU_STATE_IDX:
198560 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
198561 break;
198562
198563 case RBU_STATE_ROW:
198564 pRet->nRow = sqlite3_column_int(pStmt, 1);
198565 break;
198566
198567 case RBU_STATE_PROGRESS:
198568 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
198569 break;
198570
198571 case RBU_STATE_CKPT:
198572 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
198573 break;
198574
198575 case RBU_STATE_COOKIE:
198576 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
198577 break;
198578
198579 case RBU_STATE_OALSZ:
198580 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
198581 break;
198582
198583 case RBU_STATE_PHASEONESTEP:
198584 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
198585 break;
198586
198587 case RBU_STATE_DATATBL:
198588 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
198589 break;
198590
198591 default:
198592 rc = SQLITE_CORRUPT;
198593 break;
198594 }
198595 }
198596 rc2 = sqlite3_finalize(pStmt);
198597 if( rc==SQLITE_OK ) rc = rc2;
198598
198599 p->rc = rc;
198600 return pRet;
198601}
198602
198603
198604/*
198605** Open the database handle and attach the RBU database as "rbu". If an
198606** error occurs, leave an error code and message in the RBU handle.
198607*/
198608static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
198609 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
198610 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
198611
198612 /* Open the RBU database */
198613 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
198614
198615 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
198616 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
198617 if( p->zState==0 ){
198618 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
198619 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
198620 }
198621 }
198622
198623 /* If using separate RBU and state databases, attach the state database to
198624 ** the RBU db handle now. */
198625 if( p->zState ){
198626 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
198627 memcpy(p->zStateDb, "stat", 4);
198628 }else{
198629 memcpy(p->zStateDb, "main", 4);
198630 }
198631
198632#if 0
198633 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
198634 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
198635 }
198636#endif
198637
198638 /* If it has not already been created, create the rbu_state table */
198639 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
198640
198641#if 0
198642 if( rbuIsVacuum(p) ){
198643 if( p->rc==SQLITE_OK ){
198644 int rc2;
198645 int bOk = 0;
198646 sqlite3_stmt *pCnt = 0;
198647 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
198648 "SELECT count(*) FROM stat.sqlite_schema"
198649 );
198650 if( p->rc==SQLITE_OK
198651 && sqlite3_step(pCnt)==SQLITE_ROW
198652 && 1==sqlite3_column_int(pCnt, 0)
198653 ){
198654 bOk = 1;
198655 }
198656 rc2 = sqlite3_finalize(pCnt);
198657 if( p->rc==SQLITE_OK ) p->rc = rc2;
198658
198659 if( p->rc==SQLITE_OK && bOk==0 ){
198660 p->rc = SQLITE_ERROR;
198661 p->zErrmsg = sqlite3_mprintf("invalid state database");
198662 }
198663
198664 if( p->rc==SQLITE_OK ){
198665 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
198666 }
198667 }
198668 }
198669#endif
198670
198671 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
198672 int bOpen = 0;
198673 int rc;
198674 p->nRbu = 0;
198675 p->pRbuFd = 0;
198676 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
198677 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
198678 if( p->eStage>=RBU_STAGE_MOVE ){
198679 bOpen = 1;
198680 }else{
198681 RbuState *pState = rbuLoadState(p);
198682 if( pState ){
198683 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
198684 rbuFreeState(pState);
198685 }
198686 }
198687 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
198688 }
198689
198690 p->eStage = 0;
198691 if( p->rc==SQLITE_OK && p->dbMain==0 ){
198692 if( !rbuIsVacuum(p) ){
198693 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
198694 }else if( p->pRbuFd->pWalFd ){
198695 if( pbRetry ){
198696 p->pRbuFd->bNolock = 0;
198697 sqlite3_close(p->dbRbu);
198698 sqlite3_close(p->dbMain);
198699 p->dbMain = 0;
198700 p->dbRbu = 0;
198701 *pbRetry = 1;
198702 return;
198703 }
198704 p->rc = SQLITE_ERROR;
198705 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
198706 }else{
198707 char *zTarget;
198708 char *zExtra = 0;
198709 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
198710 zExtra = &p->zRbu[5];
198711 while( *zExtra ){
198712 if( *zExtra++=='?' ) break;
198713 }
198714 if( *zExtra=='\0' ) zExtra = 0;
198715 }
198716
198717 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
198718 sqlite3_db_filename(p->dbRbu, "main"),
198719 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
198720 );
198721
198722 if( zTarget==0 ){
198723 p->rc = SQLITE_NOMEM;
198724 return;
198725 }
198726 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
198727 sqlite3_free(zTarget);
198728 }
198729 }
198730
198731 if( p->rc==SQLITE_OK ){
198732 p->rc = sqlite3_create_function(p->dbMain,
198733 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
198734 );
198735 }
198736
198737 if( p->rc==SQLITE_OK ){
198738 p->rc = sqlite3_create_function(p->dbMain,
198739 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
198740 );
198741 }
198742
198743 if( p->rc==SQLITE_OK ){
198744 p->rc = sqlite3_create_function(p->dbRbu,
198745 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
198746 );
198747 }
198748
198749 if( p->rc==SQLITE_OK ){
198750 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
198751 }
198752 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
198753
198754 /* Mark the database file just opened as an RBU target database. If
198755 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
198756 ** This is an error. */
198757 if( p->rc==SQLITE_OK ){
198758 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
198759 }
198760
198761 if( p->rc==SQLITE_NOTFOUND ){
198762 p->rc = SQLITE_ERROR;
198763 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
198764 }
198765}
198766
198767/*
198768** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
198769** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
198770**
198771** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
198772** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
198773** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
198774** three characters, then shorten the suffix on z[] to be the last three
198775** characters of the original suffix.
198776**
198777** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
198778** do the suffix shortening regardless of URI parameter.
198779**
198780** Examples:
198781**
198782** test.db-journal => test.nal
198783** test.db-wal => test.wal
198784** test.db-shm => test.shm
198785** test.db-mj7f3319fa => test.9fa
198786*/
198787static void rbuFileSuffix3(const char *zBase, char *z){
198788#ifdef SQLITE_ENABLE_8_3_NAMES
198789#if SQLITE_ENABLE_8_3_NAMES<2
198790 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
198791#endif
198792 {
198793 int i, sz;
198794 sz = (int)strlen(z)&0xffffff;
198795 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
198796 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
198797 }
198798#endif
198799}
198800
198801/*
198802** Return the current wal-index header checksum for the target database
198803** as a 64-bit integer.
198804**
198805** The checksum is store in the first page of xShmMap memory as an 8-byte
198806** blob starting at byte offset 40.
198807*/
198808static i64 rbuShmChecksum(sqlite3rbu *p){
198809 i64 iRet = 0;
198810 if( p->rc==SQLITE_OK ){
198811 sqlite3_file *pDb = p->pTargetFd->pReal;
198812 u32 volatile *ptr;
198813 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
198814 if( p->rc==SQLITE_OK ){
198815 iRet = ((i64)ptr[10] << 32) + ptr[11];
198816 }
198817 }
198818 return iRet;
198819}
198820
198821/*
198822** This function is called as part of initializing or reinitializing an
198823** incremental checkpoint.
198824**
198825** It populates the sqlite3rbu.aFrame[] array with the set of
198826** (wal frame -> db page) copy operations required to checkpoint the
198827** current wal file, and obtains the set of shm locks required to safely
198828** perform the copy operations directly on the file-system.
198829**
198830** If argument pState is not NULL, then the incremental checkpoint is
198831** being resumed. In this case, if the checksum of the wal-index-header
198832** following recovery is not the same as the checksum saved in the RbuState
198833** object, then the rbu handle is set to DONE state. This occurs if some
198834** other client appends a transaction to the wal file in the middle of
198835** an incremental checkpoint.
198836*/
198837static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
198838
198839 /* If pState is NULL, then the wal file may not have been opened and
198840 ** recovered. Running a read-statement here to ensure that doing so
198841 ** does not interfere with the "capture" process below. */
198842 if( pState==0 ){
198843 p->eStage = 0;
198844 if( p->rc==SQLITE_OK ){
198845 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
198846 }
198847 }
198848
198849 /* Assuming no error has occurred, run a "restart" checkpoint with the
198850 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
198851 ** special behaviour in the rbu VFS:
198852 **
198853 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
198854 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
198855 ** proceed with running a passive checkpoint instead of failing).
198856 **
198857 ** * Attempts to read from the *-wal file or write to the database file
198858 ** do not perform any IO. Instead, the frame/page combinations that
198859 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
198860 ** array.
198861 **
198862 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
198863 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
198864 ** no-ops. These locks will not be released until the connection
198865 ** is closed.
198866 **
198867 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
198868 ** error.
198869 **
198870 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
198871 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
198872 ** array populated with a set of (frame -> page) mappings. Because the
198873 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
198874 ** data from the wal file into the database file according to the
198875 ** contents of aFrame[].
198876 */
198877 if( p->rc==SQLITE_OK ){
198878 int rc2;
198879 p->eStage = RBU_STAGE_CAPTURE;
198880 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
198881 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
198882 }
198883
198884 if( p->rc==SQLITE_OK && p->nFrame>0 ){
198885 p->eStage = RBU_STAGE_CKPT;
198886 p->nStep = (pState ? pState->nRow : 0);
198887 p->aBuf = rbuMalloc(p, p->pgsz);
198888 p->iWalCksum = rbuShmChecksum(p);
198889 }
198890
198891 if( p->rc==SQLITE_OK ){
198892 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
198893 p->rc = SQLITE_DONE;
198894 p->eStage = RBU_STAGE_DONE;
198895 }else{
198896 int nSectorSize;
198897 sqlite3_file *pDb = p->pTargetFd->pReal;
198898 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
198899 assert( p->nPagePerSector==0 );
198900 nSectorSize = pDb->pMethods->xSectorSize(pDb);
198901 if( nSectorSize>p->pgsz ){
198902 p->nPagePerSector = nSectorSize / p->pgsz;
198903 }else{
198904 p->nPagePerSector = 1;
198905 }
198906
198907 /* Call xSync() on the wal file. This causes SQLite to sync the
198908 ** directory in which the target database and the wal file reside, in
198909 ** case it has not been synced since the rename() call in
198910 ** rbuMoveOalFile(). */
198911 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
198912 }
198913 }
198914}
198915
198916/*
198917** Called when iAmt bytes are read from offset iOff of the wal file while
198918** the rbu object is in capture mode. Record the frame number of the frame
198919** being read in the aFrame[] array.
198920*/
198921static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
198922 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
198923 u32 iFrame;
198924
198925 if( pRbu->mLock!=mReq ){
198926 pRbu->rc = SQLITE_BUSY;
198927 return SQLITE_INTERNAL;
198928 }
198929
198930 pRbu->pgsz = iAmt;
198931 if( pRbu->nFrame==pRbu->nFrameAlloc ){
198932 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
198933 RbuFrame *aNew;
198934 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
198935 if( aNew==0 ) return SQLITE_NOMEM;
198936 pRbu->aFrame = aNew;
198937 pRbu->nFrameAlloc = nNew;
198938 }
198939
198940 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
198941 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
198942 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
198943 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
198944 pRbu->nFrame++;
198945 return SQLITE_OK;
198946}
198947
198948/*
198949** Called when a page of data is written to offset iOff of the database
198950** file while the rbu handle is in capture mode. Record the page number
198951** of the page being written in the aFrame[] array.
198952*/
198953static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
198954 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
198955 return SQLITE_OK;
198956}
198957
198958/*
198959** This is called as part of an incremental checkpoint operation. Copy
198960** a single frame of data from the wal file into the database file, as
198961** indicated by the RbuFrame object.
198962*/
198963static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
198964 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
198965 sqlite3_file *pDb = p->pTargetFd->pReal;
198966 i64 iOff;
198967
198968 assert( p->rc==SQLITE_OK );
198969 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
198970 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
198971 if( p->rc ) return;
198972
198973 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
198974 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
198975}
198976
198977
198978/*
198979** Take an EXCLUSIVE lock on the database file.
198980*/
198981static void rbuLockDatabase(sqlite3rbu *p){
198982 sqlite3_file *pReal = p->pTargetFd->pReal;
198983 assert( p->rc==SQLITE_OK );
198984 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
198985 if( p->rc==SQLITE_OK ){
198986 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
198987 }
198988}
198989
198990#if defined(_WIN32_WCE)
198991static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
198992 int nChar;
198993 LPWSTR zWideFilename;
198994
198995 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
198996 if( nChar==0 ){
198997 return 0;
198998 }
198999 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
199000 if( zWideFilename==0 ){
199001 return 0;
199002 }
199003 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
199004 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
199005 nChar);
199006 if( nChar==0 ){
199007 sqlite3_free(zWideFilename);
199008 zWideFilename = 0;
199009 }
199010 return zWideFilename;
199011}
199012#endif
199013
199014/*
199015** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
199016** on the database file. This proc moves the *-oal file to the *-wal path,
199017** then reopens the database file (this time in vanilla, non-oal, WAL mode).
199018** If an error occurs, leave an error code and error message in the rbu
199019** handle.
199020*/
199021static void rbuMoveOalFile(sqlite3rbu *p){
199022 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
199023 const char *zMove = zBase;
199024 char *zOal;
199025 char *zWal;
199026
199027 if( rbuIsVacuum(p) ){
199028 zMove = sqlite3_db_filename(p->dbRbu, "main");
199029 }
199030 zOal = sqlite3_mprintf("%s-oal", zMove);
199031 zWal = sqlite3_mprintf("%s-wal", zMove);
199032
199033 assert( p->eStage==RBU_STAGE_MOVE );
199034 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
199035 if( zWal==0 || zOal==0 ){
199036 p->rc = SQLITE_NOMEM;
199037 }else{
199038 /* Move the *-oal file to *-wal. At this point connection p->db is
199039 ** holding a SHARED lock on the target database file (because it is
199040 ** in WAL mode). So no other connection may be writing the db.
199041 **
199042 ** In order to ensure that there are no database readers, an EXCLUSIVE
199043 ** lock is obtained here before the *-oal is moved to *-wal.
199044 */
199045 rbuLockDatabase(p);
199046 if( p->rc==SQLITE_OK ){
199047 rbuFileSuffix3(zBase, zWal);
199048 rbuFileSuffix3(zBase, zOal);
199049
199050 /* Re-open the databases. */
199051 rbuObjIterFinalize(&p->objiter);
199052 sqlite3_close(p->dbRbu);
199053 sqlite3_close(p->dbMain);
199054 p->dbMain = 0;
199055 p->dbRbu = 0;
199056
199057#if defined(_WIN32_WCE)
199058 {
199059 LPWSTR zWideOal;
199060 LPWSTR zWideWal;
199061
199062 zWideOal = rbuWinUtf8ToUnicode(zOal);
199063 if( zWideOal ){
199064 zWideWal = rbuWinUtf8ToUnicode(zWal);
199065 if( zWideWal ){
199066 if( MoveFileW(zWideOal, zWideWal) ){
199067 p->rc = SQLITE_OK;
199068 }else{
199069 p->rc = SQLITE_IOERR;
199070 }
199071 sqlite3_free(zWideWal);
199072 }else{
199073 p->rc = SQLITE_IOERR_NOMEM;
199074 }
199075 sqlite3_free(zWideOal);
199076 }else{
199077 p->rc = SQLITE_IOERR_NOMEM;
199078 }
199079 }
199080#else
199081 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
199082#endif
199083
199084 if( p->rc==SQLITE_OK ){
199085 rbuOpenDatabase(p, 0);
199086 rbuSetupCheckpoint(p, 0);
199087 }
199088 }
199089 }
199090
199091 sqlite3_free(zWal);
199092 sqlite3_free(zOal);
199093}
199094
199095/*
199096** The SELECT statement iterating through the keys for the current object
199097** (p->objiter.pSelect) currently points to a valid row. This function
199098** determines the type of operation requested by this row and returns
199099** one of the following values to indicate the result:
199100**
199101** * RBU_INSERT
199102** * RBU_DELETE
199103** * RBU_IDX_DELETE
199104** * RBU_UPDATE
199105**
199106** If RBU_UPDATE is returned, then output variable *pzMask is set to
199107** point to the text value indicating the columns to update.
199108**
199109** If the rbu_control field contains an invalid value, an error code and
199110** message are left in the RBU handle and zero returned.
199111*/
199112static int rbuStepType(sqlite3rbu *p, const char **pzMask){
199113 int iCol = p->objiter.nCol; /* Index of rbu_control column */
199114 int res = 0; /* Return value */
199115
199116 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
199117 case SQLITE_INTEGER: {
199118 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
199119 switch( iVal ){
199120 case 0: res = RBU_INSERT; break;
199121 case 1: res = RBU_DELETE; break;
199122 case 2: res = RBU_REPLACE; break;
199123 case 3: res = RBU_IDX_DELETE; break;
199124 case 4: res = RBU_IDX_INSERT; break;
199125 }
199126 break;
199127 }
199128
199129 case SQLITE_TEXT: {
199130 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
199131 if( z==0 ){
199132 p->rc = SQLITE_NOMEM;
199133 }else{
199134 *pzMask = (const char*)z;
199135 }
199136 res = RBU_UPDATE;
199137
199138 break;
199139 }
199140
199141 default:
199142 break;
199143 }
199144
199145 if( res==0 ){
199146 rbuBadControlError(p);
199147 }
199148 return res;
199149}
199150
199151#ifdef SQLITE_DEBUG
199152/*
199153** Assert that column iCol of statement pStmt is named zName.
199154*/
199155static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
199156 const char *zCol = sqlite3_column_name(pStmt, iCol);
199157 assert( 0==sqlite3_stricmp(zName, zCol) );
199158}
199159#else
199160# define assertColumnName(x,y,z)
199161#endif
199162
199163/*
199164** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
199165** RBU_IDX_DELETE. This function performs the work of a single
199166** sqlite3rbu_step() call for the type of operation specified by eType.
199167*/
199168static void rbuStepOneOp(sqlite3rbu *p, int eType){
199169 RbuObjIter *pIter = &p->objiter;
199170 sqlite3_value *pVal;
199171 sqlite3_stmt *pWriter;
199172 int i;
199173
199174 assert( p->rc==SQLITE_OK );
199175 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
199176 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
199177 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
199178 );
199179
199180 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
199181 ** statement below does actually delete a row, nPhaseOneStep will be
199182 ** incremented by the same amount when SQL function rbu_tmp_insert()
199183 ** is invoked by the trigger. */
199184 if( eType==RBU_DELETE ){
199185 p->nPhaseOneStep -= p->objiter.nIndex;
199186 }
199187
199188 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
199189 pWriter = pIter->pDelete;
199190 }else{
199191 pWriter = pIter->pInsert;
199192 }
199193
199194 for(i=0; i<pIter->nCol; i++){
199195 /* If this is an INSERT into a table b-tree and the table has an
199196 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
199197 ** to write a NULL into the IPK column. That is not permitted. */
199198 if( eType==RBU_INSERT
199199 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
199200 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
199201 ){
199202 p->rc = SQLITE_MISMATCH;
199203 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
199204 return;
199205 }
199206
199207 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
199208 continue;
199209 }
199210
199211 pVal = sqlite3_column_value(pIter->pSelect, i);
199212 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
199213 if( p->rc ) return;
199214 }
199215 if( pIter->zIdx==0 ){
199216 if( pIter->eType==RBU_PK_VTAB
199217 || pIter->eType==RBU_PK_NONE
199218 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
199219 ){
199220 /* For a virtual table, or a table with no primary key, the
199221 ** SELECT statement is:
199222 **
199223 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
199224 **
199225 ** Hence column_value(pIter->nCol+1).
199226 */
199227 assertColumnName(pIter->pSelect, pIter->nCol+1,
199228 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
199229 );
199230 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
199231 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
199232 }
199233 }
199234 if( p->rc==SQLITE_OK ){
199235 sqlite3_step(pWriter);
199236 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
199237 }
199238}
199239
199240/*
199241** This function does the work for an sqlite3rbu_step() call.
199242**
199243** The object-iterator (p->objiter) currently points to a valid object,
199244** and the input cursor (p->objiter.pSelect) currently points to a valid
199245** input row. Perform whatever processing is required and return.
199246**
199247** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
199248** and message is left in the RBU handle and a copy of the error code
199249** returned.
199250*/
199251static int rbuStep(sqlite3rbu *p){
199252 RbuObjIter *pIter = &p->objiter;
199253 const char *zMask = 0;
199254 int eType = rbuStepType(p, &zMask);
199255
199256 if( eType ){
199257 assert( eType==RBU_INSERT || eType==RBU_DELETE
199258 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
199259 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
199260 );
199261 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
199262
199263 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
199264 rbuBadControlError(p);
199265 }
199266 else if( eType==RBU_REPLACE ){
199267 if( pIter->zIdx==0 ){
199268 p->nPhaseOneStep += p->objiter.nIndex;
199269 rbuStepOneOp(p, RBU_DELETE);
199270 }
199271 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
199272 }
199273 else if( eType!=RBU_UPDATE ){
199274 rbuStepOneOp(p, eType);
199275 }
199276 else{
199277 sqlite3_value *pVal;
199278 sqlite3_stmt *pUpdate = 0;
199279 assert( eType==RBU_UPDATE );
199280 p->nPhaseOneStep -= p->objiter.nIndex;
199281 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
199282 if( pUpdate ){
199283 int i;
199284 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
199285 char c = zMask[pIter->aiSrcOrder[i]];
199286 pVal = sqlite3_column_value(pIter->pSelect, i);
199287 if( pIter->abTblPk[i] || c!='.' ){
199288 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
199289 }
199290 }
199291 if( p->rc==SQLITE_OK
199292 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
199293 ){
199294 /* Bind the rbu_rowid value to column _rowid_ */
199295 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
199296 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
199297 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
199298 }
199299 if( p->rc==SQLITE_OK ){
199300 sqlite3_step(pUpdate);
199301 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
199302 }
199303 }
199304 }
199305 }
199306 return p->rc;
199307}
199308
199309/*
199310** Increment the schema cookie of the main database opened by p->dbMain.
199311**
199312** Or, if this is an RBU vacuum, set the schema cookie of the main db
199313** opened by p->dbMain to one more than the schema cookie of the main
199314** db opened by p->dbRbu.
199315*/
199316static void rbuIncrSchemaCookie(sqlite3rbu *p){
199317 if( p->rc==SQLITE_OK ){
199318 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
199319 int iCookie = 1000000;
199320 sqlite3_stmt *pStmt;
199321
199322 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
199323 "PRAGMA schema_version"
199324 );
199325 if( p->rc==SQLITE_OK ){
199326 /* Coverage: it may be that this sqlite3_step() cannot fail. There
199327 ** is already a transaction open, so the prepared statement cannot
199328 ** throw an SQLITE_SCHEMA exception. The only database page the
199329 ** statement reads is page 1, which is guaranteed to be in the cache.
199330 ** And no memory allocations are required. */
199331 if( SQLITE_ROW==sqlite3_step(pStmt) ){
199332 iCookie = sqlite3_column_int(pStmt, 0);
199333 }
199334 rbuFinalize(p, pStmt);
199335 }
199336 if( p->rc==SQLITE_OK ){
199337 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
199338 }
199339 }
199340}
199341
199342/*
199343** Update the contents of the rbu_state table within the rbu database. The
199344** value stored in the RBU_STATE_STAGE column is eStage. All other values
199345** are determined by inspecting the rbu handle passed as the first argument.
199346*/
199347static void rbuSaveState(sqlite3rbu *p, int eStage){
199348 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
199349 sqlite3_stmt *pInsert = 0;
199350 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
199351 int rc;
199352
199353 assert( p->zErrmsg==0 );
199354 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
199355 sqlite3_mprintf(
199356 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
199357 "(%d, %d), "
199358 "(%d, %Q), "
199359 "(%d, %Q), "
199360 "(%d, %d), "
199361 "(%d, %d), "
199362 "(%d, %lld), "
199363 "(%d, %lld), "
199364 "(%d, %lld), "
199365 "(%d, %lld), "
199366 "(%d, %Q) ",
199367 p->zStateDb,
199368 RBU_STATE_STAGE, eStage,
199369 RBU_STATE_TBL, p->objiter.zTbl,
199370 RBU_STATE_IDX, p->objiter.zIdx,
199371 RBU_STATE_ROW, p->nStep,
199372 RBU_STATE_PROGRESS, p->nProgress,
199373 RBU_STATE_CKPT, p->iWalCksum,
199374 RBU_STATE_COOKIE, (i64)pFd->iCookie,
199375 RBU_STATE_OALSZ, p->iOalSz,
199376 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
199377 RBU_STATE_DATATBL, p->objiter.zDataTbl
199378 )
199379 );
199380 assert( pInsert==0 || rc==SQLITE_OK );
199381
199382 if( rc==SQLITE_OK ){
199383 sqlite3_step(pInsert);
199384 rc = sqlite3_finalize(pInsert);
199385 }
199386 if( rc!=SQLITE_OK ) p->rc = rc;
199387 }
199388}
199389
199390
199391/*
199392** The second argument passed to this function is the name of a PRAGMA
199393** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
199394** This function executes the following on sqlite3rbu.dbRbu:
199395**
199396** "PRAGMA main.$zPragma"
199397**
199398** where $zPragma is the string passed as the second argument, then
199399** on sqlite3rbu.dbMain:
199400**
199401** "PRAGMA main.$zPragma = $val"
199402**
199403** where $val is the value returned by the first PRAGMA invocation.
199404**
199405** In short, it copies the value of the specified PRAGMA setting from
199406** dbRbu to dbMain.
199407*/
199408static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
199409 if( p->rc==SQLITE_OK ){
199410 sqlite3_stmt *pPragma = 0;
199411 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
199412 sqlite3_mprintf("PRAGMA main.%s", zPragma)
199413 );
199414 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
199415 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
199416 zPragma, sqlite3_column_int(pPragma, 0)
199417 );
199418 }
199419 rbuFinalize(p, pPragma);
199420 }
199421}
199422
199423/*
199424** The RBU handle passed as the only argument has just been opened and
199425** the state database is empty. If this RBU handle was opened for an
199426** RBU vacuum operation, create the schema in the target db.
199427*/
199428static void rbuCreateTargetSchema(sqlite3rbu *p){
199429 sqlite3_stmt *pSql = 0;
199430 sqlite3_stmt *pInsert = 0;
199431
199432 assert( rbuIsVacuum(p) );
199433 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
199434 if( p->rc==SQLITE_OK ){
199435 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
199436 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
199437 " AND name!='sqlite_sequence' "
199438 " ORDER BY type DESC"
199439 );
199440 }
199441
199442 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
199443 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
199444 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
199445 }
199446 rbuFinalize(p, pSql);
199447 if( p->rc!=SQLITE_OK ) return;
199448
199449 if( p->rc==SQLITE_OK ){
199450 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
199451 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
199452 );
199453 }
199454
199455 if( p->rc==SQLITE_OK ){
199456 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
199457 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
199458 );
199459 }
199460
199461 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
199462 int i;
199463 for(i=0; i<5; i++){
199464 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
199465 }
199466 sqlite3_step(pInsert);
199467 p->rc = sqlite3_reset(pInsert);
199468 }
199469 if( p->rc==SQLITE_OK ){
199470 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
199471 }
199472
199473 rbuFinalize(p, pSql);
199474 rbuFinalize(p, pInsert);
199475}
199476
199477/*
199478** Step the RBU object.
199479*/
199480SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
199481 if( p ){
199482 switch( p->eStage ){
199483 case RBU_STAGE_OAL: {
199484 RbuObjIter *pIter = &p->objiter;
199485
199486 /* If this is an RBU vacuum operation and the state table was empty
199487 ** when this handle was opened, create the target database schema. */
199488 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
199489 rbuCreateTargetSchema(p);
199490 rbuCopyPragma(p, "user_version");
199491 rbuCopyPragma(p, "application_id");
199492 }
199493
199494 while( p->rc==SQLITE_OK && pIter->zTbl ){
199495
199496 if( pIter->bCleanup ){
199497 /* Clean up the rbu_tmp_xxx table for the previous table. It
199498 ** cannot be dropped as there are currently active SQL statements.
199499 ** But the contents can be deleted. */
199500 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
199501 rbuMPrintfExec(p, p->dbRbu,
199502 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
199503 );
199504 }
199505 }else{
199506 rbuObjIterPrepareAll(p, pIter, 0);
199507
199508 /* Advance to the next row to process. */
199509 if( p->rc==SQLITE_OK ){
199510 int rc = sqlite3_step(pIter->pSelect);
199511 if( rc==SQLITE_ROW ){
199512 p->nProgress++;
199513 p->nStep++;
199514 return rbuStep(p);
199515 }
199516 p->rc = sqlite3_reset(pIter->pSelect);
199517 p->nStep = 0;
199518 }
199519 }
199520
199521 rbuObjIterNext(p, pIter);
199522 }
199523
199524 if( p->rc==SQLITE_OK ){
199525 assert( pIter->zTbl==0 );
199526 rbuSaveState(p, RBU_STAGE_MOVE);
199527 rbuIncrSchemaCookie(p);
199528 if( p->rc==SQLITE_OK ){
199529 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
199530 }
199531 if( p->rc==SQLITE_OK ){
199532 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
199533 }
199534 p->eStage = RBU_STAGE_MOVE;
199535 }
199536 break;
199537 }
199538
199539 case RBU_STAGE_MOVE: {
199540 if( p->rc==SQLITE_OK ){
199541 rbuMoveOalFile(p);
199542 p->nProgress++;
199543 }
199544 break;
199545 }
199546
199547 case RBU_STAGE_CKPT: {
199548 if( p->rc==SQLITE_OK ){
199549 if( p->nStep>=p->nFrame ){
199550 sqlite3_file *pDb = p->pTargetFd->pReal;
199551
199552 /* Sync the db file */
199553 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
199554
199555 /* Update nBackfill */
199556 if( p->rc==SQLITE_OK ){
199557 void volatile *ptr;
199558 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
199559 if( p->rc==SQLITE_OK ){
199560 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
199561 }
199562 }
199563
199564 if( p->rc==SQLITE_OK ){
199565 p->eStage = RBU_STAGE_DONE;
199566 p->rc = SQLITE_DONE;
199567 }
199568 }else{
199569 /* At one point the following block copied a single frame from the
199570 ** wal file to the database file. So that one call to sqlite3rbu_step()
199571 ** checkpointed a single frame.
199572 **
199573 ** However, if the sector-size is larger than the page-size, and the
199574 ** application calls sqlite3rbu_savestate() or close() immediately
199575 ** after this step, then rbu_step() again, then a power failure occurs,
199576 ** then the database page written here may be damaged. Work around
199577 ** this by checkpointing frames until the next page in the aFrame[]
199578 ** lies on a different disk sector to the current one. */
199579 u32 iSector;
199580 do{
199581 RbuFrame *pFrame = &p->aFrame[p->nStep];
199582 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
199583 rbuCheckpointFrame(p, pFrame);
199584 p->nStep++;
199585 }while( p->nStep<p->nFrame
199586 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
199587 && p->rc==SQLITE_OK
199588 );
199589 }
199590 p->nProgress++;
199591 }
199592 break;
199593 }
199594
199595 default:
199596 break;
199597 }
199598 return p->rc;
199599 }else{
199600 return SQLITE_NOMEM;
199601 }
199602}
199603
199604/*
199605** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
199606** otherwise. Either or both argument may be NULL. Two NULL values are
199607** considered equal, and NULL is considered distinct from all other values.
199608*/
199609static int rbuStrCompare(const char *z1, const char *z2){
199610 if( z1==0 && z2==0 ) return 0;
199611 if( z1==0 || z2==0 ) return 1;
199612 return (sqlite3_stricmp(z1, z2)!=0);
199613}
199614
199615/*
199616** This function is called as part of sqlite3rbu_open() when initializing
199617** an rbu handle in OAL stage. If the rbu update has not started (i.e.
199618** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
199619** things so that the next call to sqlite3rbu_step() continues on from
199620** where the previous rbu handle left off.
199621**
199622** If an error occurs, an error code and error message are left in the
199623** rbu handle passed as the first argument.
199624*/
199625static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
199626 assert( p->rc==SQLITE_OK );
199627 if( pState->zTbl ){
199628 RbuObjIter *pIter = &p->objiter;
199629 int rc = SQLITE_OK;
199630
199631 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
199632 || rbuStrCompare(pIter->zIdx, pState->zIdx)
199633 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
199634 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
199635 )){
199636 rc = rbuObjIterNext(p, pIter);
199637 }
199638
199639 if( rc==SQLITE_OK && !pIter->zTbl ){
199640 rc = SQLITE_ERROR;
199641 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
199642 }
199643
199644 if( rc==SQLITE_OK ){
199645 p->nStep = pState->nRow;
199646 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
199647 }
199648
199649 p->rc = rc;
199650 }
199651}
199652
199653/*
199654** If there is a "*-oal" file in the file-system corresponding to the
199655** target database in the file-system, delete it. If an error occurs,
199656** leave an error code and error message in the rbu handle.
199657*/
199658static void rbuDeleteOalFile(sqlite3rbu *p){
199659 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
199660 if( zOal ){
199661 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
199662 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
199663 pVfs->xDelete(pVfs, zOal, 0);
199664 sqlite3_free(zOal);
199665 }
199666}
199667
199668/*
199669** Allocate a private rbu VFS for the rbu handle passed as the only
199670** argument. This VFS will be used unless the call to sqlite3rbu_open()
199671** specified a URI with a vfs=? option in place of a target database
199672** file name.
199673*/
199674static void rbuCreateVfs(sqlite3rbu *p){
199675 int rnd;
199676 char zRnd[64];
199677
199678 assert( p->rc==SQLITE_OK );
199679 sqlite3_randomness(sizeof(int), (void*)&rnd);
199680 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
199681 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
199682 if( p->rc==SQLITE_OK ){
199683 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
199684 assert( pVfs );
199685 p->zVfsName = pVfs->zName;
199686 ((rbu_vfs*)pVfs)->pRbu = p;
199687 }
199688}
199689
199690/*
199691** Destroy the private VFS created for the rbu handle passed as the only
199692** argument by an earlier call to rbuCreateVfs().
199693*/
199694static void rbuDeleteVfs(sqlite3rbu *p){
199695 if( p->zVfsName ){
199696 sqlite3rbu_destroy_vfs(p->zVfsName);
199697 p->zVfsName = 0;
199698 }
199699}
199700
199701/*
199702** This user-defined SQL function is invoked with a single argument - the
199703** name of a table expected to appear in the target database. It returns
199704** the number of auxilliary indexes on the table.
199705*/
199706static void rbuIndexCntFunc(
199707 sqlite3_context *pCtx,
199708 int nVal,
199709 sqlite3_value **apVal
199710){
199711 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
199712 sqlite3_stmt *pStmt = 0;
199713 char *zErrmsg = 0;
199714 int rc;
199715 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
199716
199717 assert( nVal==1 );
199718
199719 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
199720 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
199721 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
199722 );
199723 if( rc!=SQLITE_OK ){
199724 sqlite3_result_error(pCtx, zErrmsg, -1);
199725 }else{
199726 int nIndex = 0;
199727 if( SQLITE_ROW==sqlite3_step(pStmt) ){
199728 nIndex = sqlite3_column_int(pStmt, 0);
199729 }
199730 rc = sqlite3_finalize(pStmt);
199731 if( rc==SQLITE_OK ){
199732 sqlite3_result_int(pCtx, nIndex);
199733 }else{
199734 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
199735 }
199736 }
199737
199738 sqlite3_free(zErrmsg);
199739}
199740
199741/*
199742** If the RBU database contains the rbu_count table, use it to initialize
199743** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
199744** is assumed to contain the same columns as:
199745**
199746** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
199747**
199748** There should be one row in the table for each data_xxx table in the
199749** database. The 'tbl' column should contain the name of a data_xxx table,
199750** and the cnt column the number of rows it contains.
199751**
199752** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
199753** for all rows in the rbu_count table, where nIndex is the number of
199754** indexes on the corresponding target database table.
199755*/
199756static void rbuInitPhaseOneSteps(sqlite3rbu *p){
199757 if( p->rc==SQLITE_OK ){
199758 sqlite3_stmt *pStmt = 0;
199759 int bExists = 0; /* True if rbu_count exists */
199760
199761 p->nPhaseOneStep = -1;
199762
199763 p->rc = sqlite3_create_function(p->dbRbu,
199764 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
199765 );
199766
199767 /* Check for the rbu_count table. If it does not exist, or if an error
199768 ** occurs, nPhaseOneStep will be left set to -1. */
199769 if( p->rc==SQLITE_OK ){
199770 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
199771 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
199772 );
199773 }
199774 if( p->rc==SQLITE_OK ){
199775 if( SQLITE_ROW==sqlite3_step(pStmt) ){
199776 bExists = 1;
199777 }
199778 p->rc = sqlite3_finalize(pStmt);
199779 }
199780
199781 if( p->rc==SQLITE_OK && bExists ){
199782 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
199783 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
199784 "FROM rbu_count"
199785 );
199786 if( p->rc==SQLITE_OK ){
199787 if( SQLITE_ROW==sqlite3_step(pStmt) ){
199788 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
199789 }
199790 p->rc = sqlite3_finalize(pStmt);
199791 }
199792 }
199793 }
199794}
199795
199796
199797static sqlite3rbu *openRbuHandle(
199798 const char *zTarget,
199799 const char *zRbu,
199800 const char *zState
199801){
199802 sqlite3rbu *p;
199803 size_t nTarget = zTarget ? strlen(zTarget) : 0;
199804 size_t nRbu = strlen(zRbu);
199805 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
199806
199807 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
199808 if( p ){
199809 RbuState *pState = 0;
199810
199811 /* Create the custom VFS. */
199812 memset(p, 0, sizeof(sqlite3rbu));
199813 rbuCreateVfs(p);
199814
199815 /* Open the target, RBU and state databases */
199816 if( p->rc==SQLITE_OK ){
199817 char *pCsr = (char*)&p[1];
199818 int bRetry = 0;
199819 if( zTarget ){
199820 p->zTarget = pCsr;
199821 memcpy(p->zTarget, zTarget, nTarget+1);
199822 pCsr += nTarget+1;
199823 }
199824 p->zRbu = pCsr;
199825 memcpy(p->zRbu, zRbu, nRbu+1);
199826 pCsr += nRbu+1;
199827 if( zState ){
199828 p->zState = rbuMPrintf(p, "%s", zState);
199829 }
199830
199831 /* If the first attempt to open the database file fails and the bRetry
199832 ** flag it set, this means that the db was not opened because it seemed
199833 ** to be a wal-mode db. But, this may have happened due to an earlier
199834 ** RBU vacuum operation leaving an old wal file in the directory.
199835 ** If this is the case, it will have been checkpointed and deleted
199836 ** when the handle was closed and a second attempt to open the
199837 ** database may succeed. */
199838 rbuOpenDatabase(p, &bRetry);
199839 if( bRetry ){
199840 rbuOpenDatabase(p, 0);
199841 }
199842 }
199843
199844 if( p->rc==SQLITE_OK ){
199845 pState = rbuLoadState(p);
199846 assert( pState || p->rc!=SQLITE_OK );
199847 if( p->rc==SQLITE_OK ){
199848
199849 if( pState->eStage==0 ){
199850 rbuDeleteOalFile(p);
199851 rbuInitPhaseOneSteps(p);
199852 p->eStage = RBU_STAGE_OAL;
199853 }else{
199854 p->eStage = pState->eStage;
199855 p->nPhaseOneStep = pState->nPhaseOneStep;
199856 }
199857 p->nProgress = pState->nProgress;
199858 p->iOalSz = pState->iOalSz;
199859 }
199860 }
199861 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
199862
199863 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
199864 if( p->eStage==RBU_STAGE_OAL ){
199865 p->rc = SQLITE_ERROR;
199866 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
199867 }else if( p->eStage==RBU_STAGE_MOVE ){
199868 p->eStage = RBU_STAGE_CKPT;
199869 p->nStep = 0;
199870 }
199871 }
199872
199873 if( p->rc==SQLITE_OK
199874 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
199875 && pState->eStage!=0
199876 ){
199877 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
199878 if( pFd->iCookie!=pState->iCookie ){
199879 /* At this point (pTargetFd->iCookie) contains the value of the
199880 ** change-counter cookie (the thing that gets incremented when a
199881 ** transaction is committed in rollback mode) currently stored on
199882 ** page 1 of the database file. */
199883 p->rc = SQLITE_BUSY;
199884 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
199885 (rbuIsVacuum(p) ? "vacuum" : "update")
199886 );
199887 }
199888 }
199889
199890 if( p->rc==SQLITE_OK ){
199891 if( p->eStage==RBU_STAGE_OAL ){
199892 sqlite3 *db = p->dbMain;
199893 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
199894
199895 /* Point the object iterator at the first object */
199896 if( p->rc==SQLITE_OK ){
199897 p->rc = rbuObjIterFirst(p, &p->objiter);
199898 }
199899
199900 /* If the RBU database contains no data_xxx tables, declare the RBU
199901 ** update finished. */
199902 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
199903 p->rc = SQLITE_DONE;
199904 p->eStage = RBU_STAGE_DONE;
199905 }else{
199906 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
199907 rbuCopyPragma(p, "page_size");
199908 rbuCopyPragma(p, "auto_vacuum");
199909 }
199910
199911 /* Open transactions both databases. The *-oal file is opened or
199912 ** created at this point. */
199913 if( p->rc==SQLITE_OK ){
199914 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
199915 }
199916
199917 /* Check if the main database is a zipvfs db. If it is, set the upper
199918 ** level pager to use "journal_mode=off". This prevents it from
199919 ** generating a large journal using a temp file. */
199920 if( p->rc==SQLITE_OK ){
199921 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
199922 if( frc==SQLITE_OK ){
199923 p->rc = sqlite3_exec(
199924 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
199925 }
199926 }
199927
199928 if( p->rc==SQLITE_OK ){
199929 rbuSetupOal(p, pState);
199930 }
199931 }
199932 }else if( p->eStage==RBU_STAGE_MOVE ){
199933 /* no-op */
199934 }else if( p->eStage==RBU_STAGE_CKPT ){
199935 rbuSetupCheckpoint(p, pState);
199936 }else if( p->eStage==RBU_STAGE_DONE ){
199937 p->rc = SQLITE_DONE;
199938 }else{
199939 p->rc = SQLITE_CORRUPT;
199940 }
199941 }
199942
199943 rbuFreeState(pState);
199944 }
199945
199946 return p;
199947}
199948
199949/*
199950** Allocate and return an RBU handle with all fields zeroed except for the
199951** error code, which is set to SQLITE_MISUSE.
199952*/
199953static sqlite3rbu *rbuMisuseError(void){
199954 sqlite3rbu *pRet;
199955 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
199956 if( pRet ){
199957 memset(pRet, 0, sizeof(sqlite3rbu));
199958 pRet->rc = SQLITE_MISUSE;
199959 }
199960 return pRet;
199961}
199962
199963/*
199964** Open and return a new RBU handle.
199965*/
199966SQLITE_API sqlite3rbu *sqlite3rbu_open(
199967 const char *zTarget,
199968 const char *zRbu,
199969 const char *zState
199970){
199971 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
199972 /* TODO: Check that zTarget and zRbu are non-NULL */
199973 return openRbuHandle(zTarget, zRbu, zState);
199974}
199975
199976/*
199977** Open a handle to begin or resume an RBU VACUUM operation.
199978*/
199979SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
199980 const char *zTarget,
199981 const char *zState
199982){
199983 if( zTarget==0 ){ return rbuMisuseError(); }
199984 if( zState ){
199985 int n = strlen(zState);
199986 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
199987 return rbuMisuseError();
199988 }
199989 }
199990 /* TODO: Check that both arguments are non-NULL */
199991 return openRbuHandle(0, zTarget, zState);
199992}
199993
199994/*
199995** Return the database handle used by pRbu.
199996*/
199997SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
199998 sqlite3 *db = 0;
199999 if( pRbu ){
200000 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
200001 }
200002 return db;
200003}
200004
200005
200006/*
200007** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
200008** then edit any error message string so as to remove all occurrences of
200009** the pattern "rbu_imp_[0-9]*".
200010*/
200011static void rbuEditErrmsg(sqlite3rbu *p){
200012 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
200013 unsigned int i;
200014 size_t nErrmsg = strlen(p->zErrmsg);
200015 for(i=0; i<(nErrmsg-8); i++){
200016 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
200017 int nDel = 8;
200018 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
200019 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
200020 nErrmsg -= nDel;
200021 }
200022 }
200023 }
200024}
200025
200026/*
200027** Close the RBU handle.
200028*/
200029SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
200030 int rc;
200031 if( p ){
200032
200033 /* Commit the transaction to the *-oal file. */
200034 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
200035 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
200036 }
200037
200038 /* Sync the db file if currently doing an incremental checkpoint */
200039 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
200040 sqlite3_file *pDb = p->pTargetFd->pReal;
200041 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
200042 }
200043
200044 rbuSaveState(p, p->eStage);
200045
200046 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
200047 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
200048 }
200049
200050 /* Close any open statement handles. */
200051 rbuObjIterFinalize(&p->objiter);
200052
200053 /* If this is an RBU vacuum handle and the vacuum has either finished
200054 ** successfully or encountered an error, delete the contents of the
200055 ** state table. This causes the next call to sqlite3rbu_vacuum()
200056 ** specifying the current target and state databases to start a new
200057 ** vacuum from scratch. */
200058 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
200059 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
200060 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
200061 }
200062
200063 /* Close the open database handle and VFS object. */
200064 sqlite3_close(p->dbRbu);
200065 sqlite3_close(p->dbMain);
200066 assert( p->szTemp==0 );
200067 rbuDeleteVfs(p);
200068 sqlite3_free(p->aBuf);
200069 sqlite3_free(p->aFrame);
200070
200071 rbuEditErrmsg(p);
200072 rc = p->rc;
200073 if( pzErrmsg ){
200074 *pzErrmsg = p->zErrmsg;
200075 }else{
200076 sqlite3_free(p->zErrmsg);
200077 }
200078 sqlite3_free(p->zState);
200079 sqlite3_free(p);
200080 }else{
200081 rc = SQLITE_NOMEM;
200082 *pzErrmsg = 0;
200083 }
200084 return rc;
200085}
200086
200087/*
200088** Return the total number of key-value operations (inserts, deletes or
200089** updates) that have been performed on the target database since the
200090** current RBU update was started.
200091*/
200092SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
200093 return pRbu->nProgress;
200094}
200095
200096/*
200097** Return permyriadage progress indications for the two main stages of
200098** an RBU update.
200099*/
200100SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
200101 const int MAX_PROGRESS = 10000;
200102 switch( p->eStage ){
200103 case RBU_STAGE_OAL:
200104 if( p->nPhaseOneStep>0 ){
200105 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
200106 }else{
200107 *pnOne = -1;
200108 }
200109 *pnTwo = 0;
200110 break;
200111
200112 case RBU_STAGE_MOVE:
200113 *pnOne = MAX_PROGRESS;
200114 *pnTwo = 0;
200115 break;
200116
200117 case RBU_STAGE_CKPT:
200118 *pnOne = MAX_PROGRESS;
200119 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
200120 break;
200121
200122 case RBU_STAGE_DONE:
200123 *pnOne = MAX_PROGRESS;
200124 *pnTwo = MAX_PROGRESS;
200125 break;
200126
200127 default:
200128 assert( 0 );
200129 }
200130}
200131
200132/*
200133** Return the current state of the RBU vacuum or update operation.
200134*/
200135SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
200136 int aRes[] = {
200137 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
200138 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
200139 };
200140
200141 assert( RBU_STAGE_OAL==1 );
200142 assert( RBU_STAGE_MOVE==2 );
200143 assert( RBU_STAGE_CKPT==4 );
200144 assert( RBU_STAGE_DONE==5 );
200145 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
200146 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
200147 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
200148 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
200149
200150 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
200151 return SQLITE_RBU_STATE_ERROR;
200152 }else{
200153 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
200154 assert( p->eStage==RBU_STAGE_OAL
200155 || p->eStage==RBU_STAGE_MOVE
200156 || p->eStage==RBU_STAGE_CKPT
200157 || p->eStage==RBU_STAGE_DONE
200158 );
200159 return aRes[p->eStage];
200160 }
200161}
200162
200163SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
200164 int rc = p->rc;
200165 if( rc==SQLITE_DONE ) return SQLITE_OK;
200166
200167 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
200168 if( p->eStage==RBU_STAGE_OAL ){
200169 assert( rc!=SQLITE_DONE );
200170 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
200171 }
200172
200173 /* Sync the db file */
200174 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
200175 sqlite3_file *pDb = p->pTargetFd->pReal;
200176 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
200177 }
200178
200179 p->rc = rc;
200180 rbuSaveState(p, p->eStage);
200181 rc = p->rc;
200182
200183 if( p->eStage==RBU_STAGE_OAL ){
200184 assert( rc!=SQLITE_DONE );
200185 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
200186 if( rc==SQLITE_OK ){
200187 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
200188 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
200189 }
200190 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
200191 }
200192
200193 p->rc = rc;
200194 return rc;
200195}
200196
200197/**************************************************************************
200198** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
200199** of a standard VFS in the following ways:
200200**
200201** 1. Whenever the first page of a main database file is read or
200202** written, the value of the change-counter cookie is stored in
200203** rbu_file.iCookie. Similarly, the value of the "write-version"
200204** database header field is stored in rbu_file.iWriteVer. This ensures
200205** that the values are always trustworthy within an open transaction.
200206**
200207** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
200208** member variable of the associated database file descriptor is set
200209** to point to the new file. A mutex protected linked list of all main
200210** db fds opened using a particular RBU VFS is maintained at
200211** rbu_vfs.pMain to facilitate this.
200212**
200213** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
200214** object can be marked as the target database of an RBU update. This
200215** turns on the following extra special behaviour:
200216**
200217** 3a. If xAccess() is called to check if there exists a *-wal file
200218** associated with an RBU target database currently in RBU_STAGE_OAL
200219** stage (preparing the *-oal file), the following special handling
200220** applies:
200221**
200222** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
200223** target database may not be in wal mode already.
200224**
200225** * if the *-wal file does not exist, set the output parameter to
200226** non-zero (to tell SQLite that it does exist) anyway.
200227**
200228** Then, when xOpen() is called to open the *-wal file associated with
200229** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
200230** file, the rbu vfs opens the corresponding *-oal file instead.
200231**
200232** 3b. The *-shm pages returned by xShmMap() for a target db file in
200233** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
200234** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
200235** are no-ops on target database files in RBU_STAGE_OAL mode. This is
200236** because assert() statements in some VFS implementations fail if
200237** xShmLock() is called before xShmMap().
200238**
200239** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
200240** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
200241** fails with an SQLITE_BUSY error. This is to stop RBU connections
200242** from automatically checkpointing a *-wal (or *-oal) file from within
200243** sqlite3_close().
200244**
200245** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
200246** all xWrite() calls on the target database file perform no IO.
200247** Instead the frame and page numbers that would be read and written
200248** are recorded. Additionally, successful attempts to obtain exclusive
200249** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
200250** database file are recorded. xShmLock() calls to unlock the same
200251** locks are no-ops (so that once obtained, these locks are never
200252** relinquished). Finally, calls to xSync() on the target database
200253** file fail with SQLITE_INTERNAL errors.
200254*/
200255
200256static void rbuUnlockShm(rbu_file *p){
200257 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
200258 if( p->pRbu ){
200259 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
200260 int i;
200261 for(i=0; i<SQLITE_SHM_NLOCK;i++){
200262 if( (1<<i) & p->pRbu->mLock ){
200263 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
200264 }
200265 }
200266 p->pRbu->mLock = 0;
200267 }
200268}
200269
200270/*
200271*/
200272static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
200273 sqlite3rbu *pRbu = pFd->pRbu;
200274 i64 nDiff = nNew - pFd->sz;
200275 pRbu->szTemp += nDiff;
200276 pFd->sz = nNew;
200277 assert( pRbu->szTemp>=0 );
200278 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
200279 return SQLITE_OK;
200280}
200281
200282/*
200283** Add an item to the main-db lists, if it is not already present.
200284**
200285** There are two main-db lists. One for all file descriptors, and one
200286** for all file descriptors with rbu_file.pDb!=0. If the argument has
200287** rbu_file.pDb!=0, then it is assumed to already be present on the
200288** main list and is only added to the pDb!=0 list.
200289*/
200290static void rbuMainlistAdd(rbu_file *p){
200291 rbu_vfs *pRbuVfs = p->pRbuVfs;
200292 rbu_file *pIter;
200293 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
200294 sqlite3_mutex_enter(pRbuVfs->mutex);
200295 if( p->pRbu==0 ){
200296 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
200297 p->pMainNext = pRbuVfs->pMain;
200298 pRbuVfs->pMain = p;
200299 }else{
200300 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
200301 if( pIter==0 ){
200302 p->pMainRbuNext = pRbuVfs->pMainRbu;
200303 pRbuVfs->pMainRbu = p;
200304 }
200305 }
200306 sqlite3_mutex_leave(pRbuVfs->mutex);
200307}
200308
200309/*
200310** Remove an item from the main-db lists.
200311*/
200312static void rbuMainlistRemove(rbu_file *p){
200313 rbu_file **pp;
200314 sqlite3_mutex_enter(p->pRbuVfs->mutex);
200315 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
200316 if( *pp ) *pp = p->pMainNext;
200317 p->pMainNext = 0;
200318 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
200319 if( *pp ) *pp = p->pMainRbuNext;
200320 p->pMainRbuNext = 0;
200321 sqlite3_mutex_leave(p->pRbuVfs->mutex);
200322}
200323
200324/*
200325** Given that zWal points to a buffer containing a wal file name passed to
200326** either the xOpen() or xAccess() VFS method, search the main-db list for
200327** a file-handle opened by the same database connection on the corresponding
200328** database file.
200329**
200330** If parameter bRbu is true, only search for file-descriptors with
200331** rbu_file.pDb!=0.
200332*/
200333static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
200334 rbu_file *pDb;
200335 sqlite3_mutex_enter(pRbuVfs->mutex);
200336 if( bRbu ){
200337 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
200338 }else{
200339 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
200340 }
200341 sqlite3_mutex_leave(pRbuVfs->mutex);
200342 return pDb;
200343}
200344
200345/*
200346** Close an rbu file.
200347*/
200348static int rbuVfsClose(sqlite3_file *pFile){
200349 rbu_file *p = (rbu_file*)pFile;
200350 int rc;
200351 int i;
200352
200353 /* Free the contents of the apShm[] array. And the array itself. */
200354 for(i=0; i<p->nShm; i++){
200355 sqlite3_free(p->apShm[i]);
200356 }
200357 sqlite3_free(p->apShm);
200358 p->apShm = 0;
200359 sqlite3_free(p->zDel);
200360
200361 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
200362 rbuMainlistRemove(p);
200363 rbuUnlockShm(p);
200364 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
200365 }
200366 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
200367 rbuUpdateTempSize(p, 0);
200368 }
200369 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
200370
200371 /* Close the underlying file handle */
200372 rc = p->pReal->pMethods->xClose(p->pReal);
200373 return rc;
200374}
200375
200376
200377/*
200378** Read and return an unsigned 32-bit big-endian integer from the buffer
200379** passed as the only argument.
200380*/
200381static u32 rbuGetU32(u8 *aBuf){
200382 return ((u32)aBuf[0] << 24)
200383 + ((u32)aBuf[1] << 16)
200384 + ((u32)aBuf[2] << 8)
200385 + ((u32)aBuf[3]);
200386}
200387
200388/*
200389** Write an unsigned 32-bit value in big-endian format to the supplied
200390** buffer.
200391*/
200392static void rbuPutU32(u8 *aBuf, u32 iVal){
200393 aBuf[0] = (iVal >> 24) & 0xFF;
200394 aBuf[1] = (iVal >> 16) & 0xFF;
200395 aBuf[2] = (iVal >> 8) & 0xFF;
200396 aBuf[3] = (iVal >> 0) & 0xFF;
200397}
200398
200399static void rbuPutU16(u8 *aBuf, u16 iVal){
200400 aBuf[0] = (iVal >> 8) & 0xFF;
200401 aBuf[1] = (iVal >> 0) & 0xFF;
200402}
200403
200404/*
200405** Read data from an rbuVfs-file.
200406*/
200407static int rbuVfsRead(
200408 sqlite3_file *pFile,
200409 void *zBuf,
200410 int iAmt,
200411 sqlite_int64 iOfst
200412){
200413 rbu_file *p = (rbu_file*)pFile;
200414 sqlite3rbu *pRbu = p->pRbu;
200415 int rc;
200416
200417 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
200418 assert( p->openFlags & SQLITE_OPEN_WAL );
200419 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
200420 }else{
200421 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
200422 && (p->openFlags & SQLITE_OPEN_WAL)
200423 && iOfst>=pRbu->iOalSz
200424 ){
200425 rc = SQLITE_OK;
200426 memset(zBuf, 0, iAmt);
200427 }else{
200428 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
200429#if 1
200430 /* If this is being called to read the first page of the target
200431 ** database as part of an rbu vacuum operation, synthesize the
200432 ** contents of the first page if it does not yet exist. Otherwise,
200433 ** SQLite will not check for a *-wal file. */
200434 if( pRbu && rbuIsVacuum(pRbu)
200435 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
200436 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
200437 && pRbu->rc==SQLITE_OK
200438 ){
200439 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
200440 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
200441 if( rc==SQLITE_OK ){
200442 u8 *aBuf = (u8*)zBuf;
200443 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
200444 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
200445 rbuPutU32(&aBuf[36], 0); /* number of free pages */
200446 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
200447 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
200448 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
200449
200450 if( iAmt>100 ){
200451 memset(&aBuf[100], 0, iAmt-100);
200452 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
200453 aBuf[100] = 0x0D;
200454 }
200455 }
200456 }
200457#endif
200458 }
200459 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
200460 /* These look like magic numbers. But they are stable, as they are part
200461 ** of the definition of the SQLite file format, which may not change. */
200462 u8 *pBuf = (u8*)zBuf;
200463 p->iCookie = rbuGetU32(&pBuf[24]);
200464 p->iWriteVer = pBuf[19];
200465 }
200466 }
200467 return rc;
200468}
200469
200470/*
200471** Write data to an rbuVfs-file.
200472*/
200473static int rbuVfsWrite(
200474 sqlite3_file *pFile,
200475 const void *zBuf,
200476 int iAmt,
200477 sqlite_int64 iOfst
200478){
200479 rbu_file *p = (rbu_file*)pFile;
200480 sqlite3rbu *pRbu = p->pRbu;
200481 int rc;
200482
200483 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
200484 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
200485 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
200486 }else{
200487 if( pRbu ){
200488 if( pRbu->eStage==RBU_STAGE_OAL
200489 && (p->openFlags & SQLITE_OPEN_WAL)
200490 && iOfst>=pRbu->iOalSz
200491 ){
200492 pRbu->iOalSz = iAmt + iOfst;
200493 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
200494 i64 szNew = iAmt+iOfst;
200495 if( szNew>p->sz ){
200496 rc = rbuUpdateTempSize(p, szNew);
200497 if( rc!=SQLITE_OK ) return rc;
200498 }
200499 }
200500 }
200501 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
200502 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
200503 /* These look like magic numbers. But they are stable, as they are part
200504 ** of the definition of the SQLite file format, which may not change. */
200505 u8 *pBuf = (u8*)zBuf;
200506 p->iCookie = rbuGetU32(&pBuf[24]);
200507 p->iWriteVer = pBuf[19];
200508 }
200509 }
200510 return rc;
200511}
200512
200513/*
200514** Truncate an rbuVfs-file.
200515*/
200516static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
200517 rbu_file *p = (rbu_file*)pFile;
200518 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
200519 int rc = rbuUpdateTempSize(p, size);
200520 if( rc!=SQLITE_OK ) return rc;
200521 }
200522 return p->pReal->pMethods->xTruncate(p->pReal, size);
200523}
200524
200525/*
200526** Sync an rbuVfs-file.
200527*/
200528static int rbuVfsSync(sqlite3_file *pFile, int flags){
200529 rbu_file *p = (rbu_file *)pFile;
200530 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
200531 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
200532 return SQLITE_INTERNAL;
200533 }
200534 return SQLITE_OK;
200535 }
200536 return p->pReal->pMethods->xSync(p->pReal, flags);
200537}
200538
200539/*
200540** Return the current file-size of an rbuVfs-file.
200541*/
200542static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
200543 rbu_file *p = (rbu_file *)pFile;
200544 int rc;
200545 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
200546
200547 /* If this is an RBU vacuum operation and this is the target database,
200548 ** pretend that it has at least one page. Otherwise, SQLite will not
200549 ** check for the existance of a *-wal file. rbuVfsRead() contains
200550 ** similar logic. */
200551 if( rc==SQLITE_OK && *pSize==0
200552 && p->pRbu && rbuIsVacuum(p->pRbu)
200553 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
200554 ){
200555 *pSize = 1024;
200556 }
200557 return rc;
200558}
200559
200560/*
200561** Lock an rbuVfs-file.
200562*/
200563static int rbuVfsLock(sqlite3_file *pFile, int eLock){
200564 rbu_file *p = (rbu_file*)pFile;
200565 sqlite3rbu *pRbu = p->pRbu;
200566 int rc = SQLITE_OK;
200567
200568 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200569 if( eLock==SQLITE_LOCK_EXCLUSIVE
200570 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
200571 ){
200572 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
200573 ** prevents it from checkpointing the database from sqlite3_close(). */
200574 rc = SQLITE_BUSY;
200575 }else{
200576 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
200577 }
200578
200579 return rc;
200580}
200581
200582/*
200583** Unlock an rbuVfs-file.
200584*/
200585static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
200586 rbu_file *p = (rbu_file *)pFile;
200587 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
200588}
200589
200590/*
200591** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
200592*/
200593static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
200594 rbu_file *p = (rbu_file *)pFile;
200595 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
200596}
200597
200598/*
200599** File control method. For custom operations on an rbuVfs-file.
200600*/
200601static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
200602 rbu_file *p = (rbu_file *)pFile;
200603 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
200604 int rc;
200605
200606 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
200607 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
200608 );
200609 if( op==SQLITE_FCNTL_RBU ){
200610 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
200611
200612 /* First try to find another RBU vfs lower down in the vfs stack. If
200613 ** one is found, this vfs will operate in pass-through mode. The lower
200614 ** level vfs will do the special RBU handling. */
200615 rc = xControl(p->pReal, op, pArg);
200616
200617 if( rc==SQLITE_NOTFOUND ){
200618 /* Now search for a zipvfs instance lower down in the VFS stack. If
200619 ** one is found, this is an error. */
200620 void *dummy = 0;
200621 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
200622 if( rc==SQLITE_OK ){
200623 rc = SQLITE_ERROR;
200624 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
200625 }else if( rc==SQLITE_NOTFOUND ){
200626 pRbu->pTargetFd = p;
200627 p->pRbu = pRbu;
200628 rbuMainlistAdd(p);
200629 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
200630 rc = SQLITE_OK;
200631 }
200632 }
200633 return rc;
200634 }
200635 else if( op==SQLITE_FCNTL_RBUCNT ){
200636 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
200637 pRbu->nRbu++;
200638 pRbu->pRbuFd = p;
200639 p->bNolock = 1;
200640 }
200641
200642 rc = xControl(p->pReal, op, pArg);
200643 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
200644 rbu_vfs *pRbuVfs = p->pRbuVfs;
200645 char *zIn = *(char**)pArg;
200646 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
200647 *(char**)pArg = zOut;
200648 if( zOut==0 ) rc = SQLITE_NOMEM;
200649 }
200650
200651 return rc;
200652}
200653
200654/*
200655** Return the sector-size in bytes for an rbuVfs-file.
200656*/
200657static int rbuVfsSectorSize(sqlite3_file *pFile){
200658 rbu_file *p = (rbu_file *)pFile;
200659 return p->pReal->pMethods->xSectorSize(p->pReal);
200660}
200661
200662/*
200663** Return the device characteristic flags supported by an rbuVfs-file.
200664*/
200665static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
200666 rbu_file *p = (rbu_file *)pFile;
200667 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
200668}
200669
200670/*
200671** Take or release a shared-memory lock.
200672*/
200673static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
200674 rbu_file *p = (rbu_file*)pFile;
200675 sqlite3rbu *pRbu = p->pRbu;
200676 int rc = SQLITE_OK;
200677
200678#ifdef SQLITE_AMALGAMATION
200679 assert( WAL_CKPT_LOCK==1 );
200680#endif
200681
200682 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200683 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
200684 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
200685 ** taking this lock also prevents any checkpoints from occurring.
200686 ** todo: really, it's not clear why this might occur, as
200687 ** wal_autocheckpoint ought to be turned off. */
200688 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
200689 }else{
200690 int bCapture = 0;
200691 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
200692 bCapture = 1;
200693 }
200694
200695 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
200696 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
200697 if( bCapture && rc==SQLITE_OK ){
200698 pRbu->mLock |= (1 << ofst);
200699 }
200700 }
200701 }
200702
200703 return rc;
200704}
200705
200706/*
200707** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
200708*/
200709static int rbuVfsShmMap(
200710 sqlite3_file *pFile,
200711 int iRegion,
200712 int szRegion,
200713 int isWrite,
200714 void volatile **pp
200715){
200716 rbu_file *p = (rbu_file*)pFile;
200717 int rc = SQLITE_OK;
200718 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
200719
200720 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
200721 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
200722 ** instead of a file on disk. */
200723 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200724 if( eStage==RBU_STAGE_OAL ){
200725 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
200726 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
200727
200728 /* This is an RBU connection that uses its own heap memory for the
200729 ** pages of the *-shm file. Since no other process can have run
200730 ** recovery, the connection must request *-shm pages in order
200731 ** from start to finish. */
200732 assert( iRegion==p->nShm );
200733 if( apNew==0 ){
200734 rc = SQLITE_NOMEM;
200735 }else{
200736 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
200737 p->apShm = apNew;
200738 p->nShm = iRegion+1;
200739 }
200740
200741 if( rc==SQLITE_OK ){
200742 char *pNew = (char*)sqlite3_malloc64(szRegion);
200743 if( pNew==0 ){
200744 rc = SQLITE_NOMEM;
200745 }else{
200746 memset(pNew, 0, szRegion);
200747 p->apShm[iRegion] = pNew;
200748 }
200749 }
200750
200751 if( rc==SQLITE_OK ){
200752 *pp = p->apShm[iRegion];
200753 }else{
200754 *pp = 0;
200755 }
200756 }else{
200757 assert( p->apShm==0 );
200758 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
200759 }
200760
200761 return rc;
200762}
200763
200764/*
200765** Memory barrier.
200766*/
200767static void rbuVfsShmBarrier(sqlite3_file *pFile){
200768 rbu_file *p = (rbu_file *)pFile;
200769 p->pReal->pMethods->xShmBarrier(p->pReal);
200770}
200771
200772/*
200773** The xShmUnmap method.
200774*/
200775static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
200776 rbu_file *p = (rbu_file*)pFile;
200777 int rc = SQLITE_OK;
200778 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
200779
200780 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
200781 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
200782 /* no-op */
200783 }else{
200784 /* Release the checkpointer and writer locks */
200785 rbuUnlockShm(p);
200786 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
200787 }
200788 return rc;
200789}
200790
200791/*
200792** Open an rbu file handle.
200793*/
200794static int rbuVfsOpen(
200795 sqlite3_vfs *pVfs,
200796 const char *zName,
200797 sqlite3_file *pFile,
200798 int flags,
200799 int *pOutFlags
200800){
200801 static sqlite3_io_methods rbuvfs_io_methods = {
200802 2, /* iVersion */
200803 rbuVfsClose, /* xClose */
200804 rbuVfsRead, /* xRead */
200805 rbuVfsWrite, /* xWrite */
200806 rbuVfsTruncate, /* xTruncate */
200807 rbuVfsSync, /* xSync */
200808 rbuVfsFileSize, /* xFileSize */
200809 rbuVfsLock, /* xLock */
200810 rbuVfsUnlock, /* xUnlock */
200811 rbuVfsCheckReservedLock, /* xCheckReservedLock */
200812 rbuVfsFileControl, /* xFileControl */
200813 rbuVfsSectorSize, /* xSectorSize */
200814 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
200815 rbuVfsShmMap, /* xShmMap */
200816 rbuVfsShmLock, /* xShmLock */
200817 rbuVfsShmBarrier, /* xShmBarrier */
200818 rbuVfsShmUnmap, /* xShmUnmap */
200819 0, 0 /* xFetch, xUnfetch */
200820 };
200821 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
200822 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
200823 rbu_file *pFd = (rbu_file *)pFile;
200824 int rc = SQLITE_OK;
200825 const char *zOpen = zName;
200826 int oflags = flags;
200827
200828 memset(pFd, 0, sizeof(rbu_file));
200829 pFd->pReal = (sqlite3_file*)&pFd[1];
200830 pFd->pRbuVfs = pRbuVfs;
200831 pFd->openFlags = flags;
200832 if( zName ){
200833 if( flags & SQLITE_OPEN_MAIN_DB ){
200834 /* A main database has just been opened. The following block sets
200835 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
200836 ** the name of the *-wal file this db connection will use. SQLite
200837 ** happens to pass a pointer to this buffer when using xAccess()
200838 ** or xOpen() to operate on the *-wal file. */
200839 pFd->zWal = sqlite3_filename_wal(zName);
200840 }
200841 else if( flags & SQLITE_OPEN_WAL ){
200842 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
200843 if( pDb ){
200844 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
200845 /* This call is to open a *-wal file. Intead, open the *-oal. This
200846 ** code ensures that the string passed to xOpen() is terminated by a
200847 ** pair of '\0' bytes in case the VFS attempts to extract a URI
200848 ** parameter from it. */
200849 const char *zBase = zName;
200850 size_t nCopy;
200851 char *zCopy;
200852 if( rbuIsVacuum(pDb->pRbu) ){
200853 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
200854 zBase = sqlite3_filename_wal(zBase);
200855 }
200856 nCopy = strlen(zBase);
200857 zCopy = sqlite3_malloc64(nCopy+2);
200858 if( zCopy ){
200859 memcpy(zCopy, zBase, nCopy);
200860 zCopy[nCopy-3] = 'o';
200861 zCopy[nCopy] = '\0';
200862 zCopy[nCopy+1] = '\0';
200863 zOpen = (const char*)(pFd->zDel = zCopy);
200864 }else{
200865 rc = SQLITE_NOMEM;
200866 }
200867 pFd->pRbu = pDb->pRbu;
200868 }
200869 pDb->pWalFd = pFd;
200870 }
200871 }
200872 }else{
200873 pFd->pRbu = pRbuVfs->pRbu;
200874 }
200875
200876 if( oflags & SQLITE_OPEN_MAIN_DB
200877 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
200878 ){
200879 assert( oflags & SQLITE_OPEN_MAIN_DB );
200880 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
200881 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
200882 zOpen = 0;
200883 }
200884
200885 if( rc==SQLITE_OK ){
200886 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
200887 }
200888 if( pFd->pReal->pMethods ){
200889 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
200890 ** pointer and, if the file is a main database file, link it into the
200891 ** mutex protected linked list of all such files. */
200892 pFile->pMethods = &rbuvfs_io_methods;
200893 if( flags & SQLITE_OPEN_MAIN_DB ){
200894 rbuMainlistAdd(pFd);
200895 }
200896 }else{
200897 sqlite3_free(pFd->zDel);
200898 }
200899
200900 return rc;
200901}
200902
200903/*
200904** Delete the file located at zPath.
200905*/
200906static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
200907 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200908 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
200909}
200910
200911/*
200912** Test for access permissions. Return true if the requested permission
200913** is available, or false otherwise.
200914*/
200915static int rbuVfsAccess(
200916 sqlite3_vfs *pVfs,
200917 const char *zPath,
200918 int flags,
200919 int *pResOut
200920){
200921 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
200922 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
200923 int rc;
200924
200925 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
200926
200927 /* If this call is to check if a *-wal file associated with an RBU target
200928 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
200929 ** the following special handling is activated:
200930 **
200931 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
200932 ** ensures that the RBU extension never tries to update a database
200933 ** in wal mode, even if the first page of the database file has
200934 ** been damaged.
200935 **
200936 ** b) if the *-wal file does not exist, claim that it does anyway,
200937 ** causing SQLite to call xOpen() to open it. This call will also
200938 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
200939 ** file opened instead.
200940 */
200941 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
200942 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
200943 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
200944 assert( pDb->pRbu );
200945 if( *pResOut ){
200946 rc = SQLITE_CANTOPEN;
200947 }else{
200948 sqlite3_int64 sz = 0;
200949 rc = rbuVfsFileSize(&pDb->base, &sz);
200950 *pResOut = (sz>0);
200951 }
200952 }
200953 }
200954
200955 return rc;
200956}
200957
200958/*
200959** Populate buffer zOut with the full canonical pathname corresponding
200960** to the pathname in zPath. zOut is guaranteed to point to a buffer
200961** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
200962*/
200963static int rbuVfsFullPathname(
200964 sqlite3_vfs *pVfs,
200965 const char *zPath,
200966 int nOut,
200967 char *zOut
200968){
200969 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200970 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
200971}
200972
200973#ifndef SQLITE_OMIT_LOAD_EXTENSION
200974/*
200975** Open the dynamic library located at zPath and return a handle.
200976*/
200977static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
200978 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200979 return pRealVfs->xDlOpen(pRealVfs, zPath);
200980}
200981
200982/*
200983** Populate the buffer zErrMsg (size nByte bytes) with a human readable
200984** utf-8 string describing the most recent error encountered associated
200985** with dynamic libraries.
200986*/
200987static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
200988 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
200989 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
200990}
200991
200992/*
200993** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
200994*/
200995static void (*rbuVfsDlSym(
200996 sqlite3_vfs *pVfs,
200997 void *pArg,
200998 const char *zSym
200999))(void){
201000 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
201001 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
201002}
201003
201004/*
201005** Close the dynamic library handle pHandle.
201006*/
201007static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
201008 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
201009 pRealVfs->xDlClose(pRealVfs, pHandle);
201010}
201011#endif /* SQLITE_OMIT_LOAD_EXTENSION */
201012
201013/*
201014** Populate the buffer pointed to by zBufOut with nByte bytes of
201015** random data.
201016*/
201017static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
201018 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
201019 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
201020}
201021
201022/*
201023** Sleep for nMicro microseconds. Return the number of microseconds
201024** actually slept.
201025*/
201026static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
201027 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
201028 return pRealVfs->xSleep(pRealVfs, nMicro);
201029}
201030
201031/*
201032** Return the current time as a Julian Day number in *pTimeOut.
201033*/
201034static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
201035 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
201036 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
201037}
201038
201039/*
201040** No-op.
201041*/
201042static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
201043 return 0;
201044}
201045
201046/*
201047** Deregister and destroy an RBU vfs created by an earlier call to
201048** sqlite3rbu_create_vfs().
201049*/
201050SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
201051 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
201052 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
201053 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
201054 sqlite3_vfs_unregister(pVfs);
201055 sqlite3_free(pVfs);
201056 }
201057}
201058
201059/*
201060** Create an RBU VFS named zName that accesses the underlying file-system
201061** via existing VFS zParent. The new object is registered as a non-default
201062** VFS with SQLite before returning.
201063*/
201064SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
201065
201066 /* Template for VFS */
201067 static sqlite3_vfs vfs_template = {
201068 1, /* iVersion */
201069 0, /* szOsFile */
201070 0, /* mxPathname */
201071 0, /* pNext */
201072 0, /* zName */
201073 0, /* pAppData */
201074 rbuVfsOpen, /* xOpen */
201075 rbuVfsDelete, /* xDelete */
201076 rbuVfsAccess, /* xAccess */
201077 rbuVfsFullPathname, /* xFullPathname */
201078
201079#ifndef SQLITE_OMIT_LOAD_EXTENSION
201080 rbuVfsDlOpen, /* xDlOpen */
201081 rbuVfsDlError, /* xDlError */
201082 rbuVfsDlSym, /* xDlSym */
201083 rbuVfsDlClose, /* xDlClose */
201084#else
201085 0, 0, 0, 0,
201086#endif
201087
201088 rbuVfsRandomness, /* xRandomness */
201089 rbuVfsSleep, /* xSleep */
201090 rbuVfsCurrentTime, /* xCurrentTime */
201091 rbuVfsGetLastError, /* xGetLastError */
201092 0, /* xCurrentTimeInt64 (version 2) */
201093 0, 0, 0 /* Unimplemented version 3 methods */
201094 };
201095
201096 rbu_vfs *pNew = 0; /* Newly allocated VFS */
201097 int rc = SQLITE_OK;
201098 size_t nName;
201099 size_t nByte;
201100
201101 nName = strlen(zName);
201102 nByte = sizeof(rbu_vfs) + nName + 1;
201103 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
201104 if( pNew==0 ){
201105 rc = SQLITE_NOMEM;
201106 }else{
201107 sqlite3_vfs *pParent; /* Parent VFS */
201108 memset(pNew, 0, nByte);
201109 pParent = sqlite3_vfs_find(zParent);
201110 if( pParent==0 ){
201111 rc = SQLITE_NOTFOUND;
201112 }else{
201113 char *zSpace;
201114 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
201115 pNew->base.mxPathname = pParent->mxPathname;
201116 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
201117 pNew->pRealVfs = pParent;
201118 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
201119 memcpy(zSpace, zName, nName);
201120
201121 /* Allocate the mutex and register the new VFS (not as the default) */
201122 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
201123 if( pNew->mutex==0 ){
201124 rc = SQLITE_NOMEM;
201125 }else{
201126 rc = sqlite3_vfs_register(&pNew->base, 0);
201127 }
201128 }
201129
201130 if( rc!=SQLITE_OK ){
201131 sqlite3_mutex_free(pNew->mutex);
201132 sqlite3_free(pNew);
201133 }
201134 }
201135
201136 return rc;
201137}
201138
201139/*
201140** Configure the aggregate temp file size limit for this RBU handle.
201141*/
201142SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
201143 if( n>=0 ){
201144 pRbu->szTempLimit = n;
201145 }
201146 return pRbu->szTempLimit;
201147}
201148
201149SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
201150 return pRbu->szTemp;
201151}
201152
201153
201154/**************************************************************************/
201155
201156#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
201157
201158/************** End of sqlite3rbu.c ******************************************/
201159/************** Begin file dbstat.c ******************************************/
201160/*
201161** 2010 July 12
201162**
201163** The author disclaims copyright to this source code. In place of
201164** a legal notice, here is a blessing:
201165**
201166** May you do good and not evil.
201167** May you find forgiveness for yourself and forgive others.
201168** May you share freely, never taking more than you give.
201169**
201170******************************************************************************
201171**
201172** This file contains an implementation of the "dbstat" virtual table.
201173**
201174** The dbstat virtual table is used to extract low-level storage
201175** information from an SQLite database in order to implement the
201176** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
201177** for an example implementation.
201178**
201179** Additional information is available on the "dbstat.html" page of the
201180** official SQLite documentation.
201181*/
201182
201183/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
201184#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
201185 && !defined(SQLITE_OMIT_VIRTUALTABLE)
201186
201187/*
201188** Page paths:
201189**
201190** The value of the 'path' column describes the path taken from the
201191** root-node of the b-tree structure to each page. The value of the
201192** root-node path is '/'.
201193**
201194** The value of the path for the left-most child page of the root of
201195** a b-tree is '/000/'. (Btrees store content ordered from left to right
201196** so the pages to the left have smaller keys than the pages to the right.)
201197** The next to left-most child of the root page is
201198** '/001', and so on, each sibling page identified by a 3-digit hex
201199** value. The children of the 451st left-most sibling have paths such
201200** as '/1c2/000/, '/1c2/001/' etc.
201201**
201202** Overflow pages are specified by appending a '+' character and a
201203** six-digit hexadecimal value to the path to the cell they are linked
201204** from. For example, the three overflow pages in a chain linked from
201205** the left-most cell of the 450th child of the root page are identified
201206** by the paths:
201207**
201208** '/1c2/000+000000' // First page in overflow chain
201209** '/1c2/000+000001' // Second page in overflow chain
201210** '/1c2/000+000002' // Third page in overflow chain
201211**
201212** If the paths are sorted using the BINARY collation sequence, then
201213** the overflow pages associated with a cell will appear earlier in the
201214** sort-order than its child page:
201215**
201216** '/1c2/000/' // Left-most child of 451st child of root
201217*/
201218static const char zDbstatSchema[] =
201219 "CREATE TABLE x("
201220 " name TEXT," /* 0 Name of table or index */
201221 " path TEXT," /* 1 Path to page from root (NULL for agg) */
201222 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
201223 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
201224 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
201225 " payload INTEGER," /* 5 Bytes of payload on this page */
201226 " unused INTEGER," /* 6 Bytes of unused space on this page */
201227 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
201228 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
201229 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
201230 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
201231 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
201232 ")"
201233;
201234
201235/* Forward reference to data structured used in this module */
201236typedef struct StatTable StatTable;
201237typedef struct StatCursor StatCursor;
201238typedef struct StatPage StatPage;
201239typedef struct StatCell StatCell;
201240
201241/* Size information for a single cell within a btree page */
201242struct StatCell {
201243 int nLocal; /* Bytes of local payload */
201244 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
201245 int nOvfl; /* Entries in aOvfl[] */
201246 u32 *aOvfl; /* Array of overflow page numbers */
201247 int nLastOvfl; /* Bytes of payload on final overflow page */
201248 int iOvfl; /* Iterates through aOvfl[] */
201249};
201250
201251/* Size information for a single btree page */
201252struct StatPage {
201253 u32 iPgno; /* Page number */
201254 DbPage *pPg; /* Page content */
201255 int iCell; /* Current cell */
201256
201257 char *zPath; /* Path to this page */
201258
201259 /* Variables populated by statDecodePage(): */
201260 u8 flags; /* Copy of flags byte */
201261 int nCell; /* Number of cells on page */
201262 int nUnused; /* Number of unused bytes on page */
201263 StatCell *aCell; /* Array of parsed cells */
201264 u32 iRightChildPg; /* Right-child page number (or 0) */
201265 int nMxPayload; /* Largest payload of any cell on the page */
201266};
201267
201268/* The cursor for scanning the dbstat virtual table */
201269struct StatCursor {
201270 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
201271 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
201272 u8 isEof; /* After pStmt has returned SQLITE_DONE */
201273 u8 isAgg; /* Aggregate results for each table */
201274 int iDb; /* Schema used for this query */
201275
201276 StatPage aPage[32]; /* Pages in path to current page */
201277 int iPage; /* Current entry in aPage[] */
201278
201279 /* Values to return. */
201280 u32 iPageno; /* Value of 'pageno' column */
201281 char *zName; /* Value of 'name' column */
201282 char *zPath; /* Value of 'path' column */
201283 char *zPagetype; /* Value of 'pagetype' column */
201284 int nPage; /* Number of pages in current btree */
201285 int nCell; /* Value of 'ncell' column */
201286 int nMxPayload; /* Value of 'mx_payload' column */
201287 i64 nUnused; /* Value of 'unused' column */
201288 i64 nPayload; /* Value of 'payload' column */
201289 i64 iOffset; /* Value of 'pgOffset' column */
201290 i64 szPage; /* Value of 'pgSize' column */
201291};
201292
201293/* An instance of the DBSTAT virtual table */
201294struct StatTable {
201295 sqlite3_vtab base; /* base class. MUST BE FIRST! */
201296 sqlite3 *db; /* Database connection that owns this vtab */
201297 int iDb; /* Index of database to analyze */
201298};
201299
201300#ifndef get2byte
201301# define get2byte(x) ((x)[0]<<8 | (x)[1])
201302#endif
201303
201304/*
201305** Connect to or create a new DBSTAT virtual table.
201306*/
201307static int statConnect(
201308 sqlite3 *db,
201309 void *pAux,
201310 int argc, const char *const*argv,
201311 sqlite3_vtab **ppVtab,
201312 char **pzErr
201313){
201314 StatTable *pTab = 0;
201315 int rc = SQLITE_OK;
201316 int iDb;
201317
201318 if( argc>=4 ){
201319 Token nm;
201320 sqlite3TokenInit(&nm, (char*)argv[3]);
201321 iDb = sqlite3FindDb(db, &nm);
201322 if( iDb<0 ){
201323 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
201324 return SQLITE_ERROR;
201325 }
201326 }else{
201327 iDb = 0;
201328 }
201329 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
201330 rc = sqlite3_declare_vtab(db, zDbstatSchema);
201331 if( rc==SQLITE_OK ){
201332 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
201333 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
201334 }
201335
201336 assert( rc==SQLITE_OK || pTab==0 );
201337 if( rc==SQLITE_OK ){
201338 memset(pTab, 0, sizeof(StatTable));
201339 pTab->db = db;
201340 pTab->iDb = iDb;
201341 }
201342
201343 *ppVtab = (sqlite3_vtab*)pTab;
201344 return rc;
201345}
201346
201347/*
201348** Disconnect from or destroy the DBSTAT virtual table.
201349*/
201350static int statDisconnect(sqlite3_vtab *pVtab){
201351 sqlite3_free(pVtab);
201352 return SQLITE_OK;
201353}
201354
201355/*
201356** Compute the best query strategy and return the result in idxNum.
201357**
201358** idxNum-Bit Meaning
201359** ---------- ----------------------------------------------
201360** 0x01 There is a schema=? term in the WHERE clause
201361** 0x02 There is a name=? term in the WHERE clause
201362** 0x04 There is an aggregate=? term in the WHERE clause
201363** 0x08 Output should be ordered by name and path
201364*/
201365static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
201366 int i;
201367 int iSchema = -1;
201368 int iName = -1;
201369 int iAgg = -1;
201370
201371 /* Look for a valid schema=? constraint. If found, change the idxNum to
201372 ** 1 and request the value of that constraint be sent to xFilter. And
201373 ** lower the cost estimate to encourage the constrained version to be
201374 ** used.
201375 */
201376 for(i=0; i<pIdxInfo->nConstraint; i++){
201377 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
201378 if( pIdxInfo->aConstraint[i].usable==0 ){
201379 /* Force DBSTAT table should always be the right-most table in a join */
201380 return SQLITE_CONSTRAINT;
201381 }
201382 switch( pIdxInfo->aConstraint[i].iColumn ){
201383 case 0: { /* name */
201384 iName = i;
201385 break;
201386 }
201387 case 10: { /* schema */
201388 iSchema = i;
201389 break;
201390 }
201391 case 11: { /* aggregate */
201392 iAgg = i;
201393 break;
201394 }
201395 }
201396 }
201397 i = 0;
201398 if( iSchema>=0 ){
201399 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
201400 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
201401 pIdxInfo->idxNum |= 0x01;
201402 }
201403 if( iName>=0 ){
201404 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
201405 pIdxInfo->idxNum |= 0x02;
201406 }
201407 if( iAgg>=0 ){
201408 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
201409 pIdxInfo->idxNum |= 0x04;
201410 }
201411 pIdxInfo->estimatedCost = 1.0;
201412
201413 /* Records are always returned in ascending order of (name, path).
201414 ** If this will satisfy the client, set the orderByConsumed flag so that
201415 ** SQLite does not do an external sort.
201416 */
201417 if( ( pIdxInfo->nOrderBy==1
201418 && pIdxInfo->aOrderBy[0].iColumn==0
201419 && pIdxInfo->aOrderBy[0].desc==0
201420 ) ||
201421 ( pIdxInfo->nOrderBy==2
201422 && pIdxInfo->aOrderBy[0].iColumn==0
201423 && pIdxInfo->aOrderBy[0].desc==0
201424 && pIdxInfo->aOrderBy[1].iColumn==1
201425 && pIdxInfo->aOrderBy[1].desc==0
201426 )
201427 ){
201428 pIdxInfo->orderByConsumed = 1;
201429 pIdxInfo->idxNum |= 0x08;
201430 }
201431
201432 return SQLITE_OK;
201433}
201434
201435/*
201436** Open a new DBSTAT cursor.
201437*/
201438static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
201439 StatTable *pTab = (StatTable *)pVTab;
201440 StatCursor *pCsr;
201441
201442 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
201443 if( pCsr==0 ){
201444 return SQLITE_NOMEM_BKPT;
201445 }else{
201446 memset(pCsr, 0, sizeof(StatCursor));
201447 pCsr->base.pVtab = pVTab;
201448 pCsr->iDb = pTab->iDb;
201449 }
201450
201451 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
201452 return SQLITE_OK;
201453}
201454
201455static void statClearCells(StatPage *p){
201456 int i;
201457 if( p->aCell ){
201458 for(i=0; i<p->nCell; i++){
201459 sqlite3_free(p->aCell[i].aOvfl);
201460 }
201461 sqlite3_free(p->aCell);
201462 }
201463 p->nCell = 0;
201464 p->aCell = 0;
201465}
201466
201467static void statClearPage(StatPage *p){
201468 statClearCells(p);
201469 sqlite3PagerUnref(p->pPg);
201470 sqlite3_free(p->zPath);
201471 memset(p, 0, sizeof(StatPage));
201472}
201473
201474static void statResetCsr(StatCursor *pCsr){
201475 int i;
201476 sqlite3_reset(pCsr->pStmt);
201477 for(i=0; i<ArraySize(pCsr->aPage); i++){
201478 statClearPage(&pCsr->aPage[i]);
201479 }
201480 pCsr->iPage = 0;
201481 sqlite3_free(pCsr->zPath);
201482 pCsr->zPath = 0;
201483 pCsr->isEof = 0;
201484}
201485
201486/* Resize the space-used counters inside of the cursor */
201487static void statResetCounts(StatCursor *pCsr){
201488 pCsr->nCell = 0;
201489 pCsr->nMxPayload = 0;
201490 pCsr->nUnused = 0;
201491 pCsr->nPayload = 0;
201492 pCsr->szPage = 0;
201493 pCsr->nPage = 0;
201494}
201495
201496/*
201497** Close a DBSTAT cursor.
201498*/
201499static int statClose(sqlite3_vtab_cursor *pCursor){
201500 StatCursor *pCsr = (StatCursor *)pCursor;
201501 statResetCsr(pCsr);
201502 sqlite3_finalize(pCsr->pStmt);
201503 sqlite3_free(pCsr);
201504 return SQLITE_OK;
201505}
201506
201507/*
201508** For a single cell on a btree page, compute the number of bytes of
201509** content (payload) stored on that page. That is to say, compute the
201510** number of bytes of content not found on overflow pages.
201511*/
201512static int getLocalPayload(
201513 int nUsable, /* Usable bytes per page */
201514 u8 flags, /* Page flags */
201515 int nTotal /* Total record (payload) size */
201516){
201517 int nLocal;
201518 int nMinLocal;
201519 int nMaxLocal;
201520
201521 if( flags==0x0D ){ /* Table leaf node */
201522 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
201523 nMaxLocal = nUsable - 35;
201524 }else{ /* Index interior and leaf nodes */
201525 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
201526 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
201527 }
201528
201529 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
201530 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
201531 return nLocal;
201532}
201533
201534/* Populate the StatPage object with information about the all
201535** cells found on the page currently under analysis.
201536*/
201537static int statDecodePage(Btree *pBt, StatPage *p){
201538 int nUnused;
201539 int iOff;
201540 int nHdr;
201541 int isLeaf;
201542 int szPage;
201543
201544 u8 *aData = sqlite3PagerGetData(p->pPg);
201545 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
201546
201547 p->flags = aHdr[0];
201548 if( p->flags==0x0A || p->flags==0x0D ){
201549 isLeaf = 1;
201550 nHdr = 8;
201551 }else if( p->flags==0x05 || p->flags==0x02 ){
201552 isLeaf = 0;
201553 nHdr = 12;
201554 }else{
201555 goto statPageIsCorrupt;
201556 }
201557 if( p->iPgno==1 ) nHdr += 100;
201558 p->nCell = get2byte(&aHdr[3]);
201559 p->nMxPayload = 0;
201560 szPage = sqlite3BtreeGetPageSize(pBt);
201561
201562 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
201563 nUnused += (int)aHdr[7];
201564 iOff = get2byte(&aHdr[1]);
201565 while( iOff ){
201566 int iNext;
201567 if( iOff>=szPage ) goto statPageIsCorrupt;
201568 nUnused += get2byte(&aData[iOff+2]);
201569 iNext = get2byte(&aData[iOff]);
201570 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
201571 iOff = iNext;
201572 }
201573 p->nUnused = nUnused;
201574 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
201575
201576 if( p->nCell ){
201577 int i; /* Used to iterate through cells */
201578 int nUsable; /* Usable bytes per page */
201579
201580 sqlite3BtreeEnter(pBt);
201581 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
201582 sqlite3BtreeLeave(pBt);
201583 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
201584 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
201585 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
201586
201587 for(i=0; i<p->nCell; i++){
201588 StatCell *pCell = &p->aCell[i];
201589
201590 iOff = get2byte(&aData[nHdr+i*2]);
201591 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
201592 if( !isLeaf ){
201593 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
201594 iOff += 4;
201595 }
201596 if( p->flags==0x05 ){
201597 /* A table interior node. nPayload==0. */
201598 }else{
201599 u32 nPayload; /* Bytes of payload total (local+overflow) */
201600 int nLocal; /* Bytes of payload stored locally */
201601 iOff += getVarint32(&aData[iOff], nPayload);
201602 if( p->flags==0x0D ){
201603 u64 dummy;
201604 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
201605 }
201606 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
201607 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
201608 if( nLocal<0 ) goto statPageIsCorrupt;
201609 pCell->nLocal = nLocal;
201610 assert( nPayload>=(u32)nLocal );
201611 assert( nLocal<=(nUsable-35) );
201612 if( nPayload>(u32)nLocal ){
201613 int j;
201614 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
201615 if( iOff+nLocal>nUsable || nPayload>0x7fffffff ){
201616 goto statPageIsCorrupt;
201617 }
201618 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
201619 pCell->nOvfl = nOvfl;
201620 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
201621 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
201622 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
201623 for(j=1; j<nOvfl; j++){
201624 int rc;
201625 u32 iPrev = pCell->aOvfl[j-1];
201626 DbPage *pPg = 0;
201627 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
201628 if( rc!=SQLITE_OK ){
201629 assert( pPg==0 );
201630 return rc;
201631 }
201632 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
201633 sqlite3PagerUnref(pPg);
201634 }
201635 }
201636 }
201637 }
201638 }
201639
201640 return SQLITE_OK;
201641
201642statPageIsCorrupt:
201643 p->flags = 0;
201644 statClearCells(p);
201645 return SQLITE_OK;
201646}
201647
201648/*
201649** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
201650** the current value of pCsr->iPageno.
201651*/
201652static void statSizeAndOffset(StatCursor *pCsr){
201653 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
201654 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
201655 Pager *pPager = sqlite3BtreePager(pBt);
201656 sqlite3_file *fd;
201657 sqlite3_int64 x[2];
201658
201659 /* If connected to a ZIPVFS backend, find the page size and
201660 ** offset from ZIPVFS.
201661 */
201662 fd = sqlite3PagerFile(pPager);
201663 x[0] = pCsr->iPageno;
201664 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
201665 pCsr->iOffset = x[0];
201666 pCsr->szPage += x[1];
201667 }else{
201668 /* Not ZIPVFS: The default page size and offset */
201669 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
201670 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
201671 }
201672}
201673
201674/*
201675** Move a DBSTAT cursor to the next entry. Normally, the next
201676** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
201677** the next entry is the next btree.
201678*/
201679static int statNext(sqlite3_vtab_cursor *pCursor){
201680 int rc;
201681 int nPayload;
201682 char *z;
201683 StatCursor *pCsr = (StatCursor *)pCursor;
201684 StatTable *pTab = (StatTable *)pCursor->pVtab;
201685 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
201686 Pager *pPager = sqlite3BtreePager(pBt);
201687
201688 sqlite3_free(pCsr->zPath);
201689 pCsr->zPath = 0;
201690
201691statNextRestart:
201692 if( pCsr->aPage[0].pPg==0 ){
201693 /* Start measuring space on the next btree */
201694 statResetCounts(pCsr);
201695 rc = sqlite3_step(pCsr->pStmt);
201696 if( rc==SQLITE_ROW ){
201697 int nPage;
201698 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
201699 sqlite3PagerPagecount(pPager, &nPage);
201700 if( nPage==0 ){
201701 pCsr->isEof = 1;
201702 return sqlite3_reset(pCsr->pStmt);
201703 }
201704 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
201705 pCsr->aPage[0].iPgno = iRoot;
201706 pCsr->aPage[0].iCell = 0;
201707 if( !pCsr->isAgg ){
201708 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
201709 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
201710 }
201711 pCsr->iPage = 0;
201712 pCsr->nPage = 1;
201713 }else{
201714 pCsr->isEof = 1;
201715 return sqlite3_reset(pCsr->pStmt);
201716 }
201717 }else{
201718 /* Continue analyzing the btree previously started */
201719 StatPage *p = &pCsr->aPage[pCsr->iPage];
201720 if( !pCsr->isAgg ) statResetCounts(pCsr);
201721 while( p->iCell<p->nCell ){
201722 StatCell *pCell = &p->aCell[p->iCell];
201723 while( pCell->iOvfl<pCell->nOvfl ){
201724 int nUsable, iOvfl;
201725 sqlite3BtreeEnter(pBt);
201726 nUsable = sqlite3BtreeGetPageSize(pBt) -
201727 sqlite3BtreeGetReserveNoMutex(pBt);
201728 sqlite3BtreeLeave(pBt);
201729 pCsr->nPage++;
201730 statSizeAndOffset(pCsr);
201731 if( pCell->iOvfl<pCell->nOvfl-1 ){
201732 pCsr->nPayload += nUsable - 4;
201733 }else{
201734 pCsr->nPayload += pCell->nLastOvfl;
201735 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
201736 }
201737 iOvfl = pCell->iOvfl;
201738 pCell->iOvfl++;
201739 if( !pCsr->isAgg ){
201740 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
201741 pCsr->iPageno = pCell->aOvfl[iOvfl];
201742 pCsr->zPagetype = "overflow";
201743 pCsr->zPath = z = sqlite3_mprintf(
201744 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
201745 );
201746 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
201747 }
201748 }
201749 if( p->iRightChildPg ) break;
201750 p->iCell++;
201751 }
201752
201753 if( !p->iRightChildPg || p->iCell>p->nCell ){
201754 statClearPage(p);
201755 if( pCsr->iPage>0 ){
201756 pCsr->iPage--;
201757 }else if( pCsr->isAgg ){
201758 /* label-statNext-done: When computing aggregate space usage over
201759 ** an entire btree, this is the exit point from this function */
201760 return SQLITE_OK;
201761 }
201762 goto statNextRestart; /* Tail recursion */
201763 }
201764 pCsr->iPage++;
201765 if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
201766 statResetCsr(pCsr);
201767 return SQLITE_CORRUPT_BKPT;
201768 }
201769 assert( p==&pCsr->aPage[pCsr->iPage-1] );
201770
201771 if( p->iCell==p->nCell ){
201772 p[1].iPgno = p->iRightChildPg;
201773 }else{
201774 p[1].iPgno = p->aCell[p->iCell].iChildPg;
201775 }
201776 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
201777 pCsr->nPage++;
201778 p[1].iCell = 0;
201779 if( !pCsr->isAgg ){
201780 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
201781 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
201782 }
201783 p->iCell++;
201784 }
201785
201786
201787 /* Populate the StatCursor fields with the values to be returned
201788 ** by the xColumn() and xRowid() methods.
201789 */
201790 if( rc==SQLITE_OK ){
201791 int i;
201792 StatPage *p = &pCsr->aPage[pCsr->iPage];
201793 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
201794 pCsr->iPageno = p->iPgno;
201795
201796 rc = statDecodePage(pBt, p);
201797 if( rc==SQLITE_OK ){
201798 statSizeAndOffset(pCsr);
201799
201800 switch( p->flags ){
201801 case 0x05: /* table internal */
201802 case 0x02: /* index internal */
201803 pCsr->zPagetype = "internal";
201804 break;
201805 case 0x0D: /* table leaf */
201806 case 0x0A: /* index leaf */
201807 pCsr->zPagetype = "leaf";
201808 break;
201809 default:
201810 pCsr->zPagetype = "corrupted";
201811 break;
201812 }
201813 pCsr->nCell += p->nCell;
201814 pCsr->nUnused += p->nUnused;
201815 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
201816 if( !pCsr->isAgg ){
201817 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
201818 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
201819 }
201820 nPayload = 0;
201821 for(i=0; i<p->nCell; i++){
201822 nPayload += p->aCell[i].nLocal;
201823 }
201824 pCsr->nPayload += nPayload;
201825
201826 /* If computing aggregate space usage by btree, continue with the
201827 ** next page. The loop will exit via the return at label-statNext-done
201828 */
201829 if( pCsr->isAgg ) goto statNextRestart;
201830 }
201831 }
201832
201833 return rc;
201834}
201835
201836static int statEof(sqlite3_vtab_cursor *pCursor){
201837 StatCursor *pCsr = (StatCursor *)pCursor;
201838 return pCsr->isEof;
201839}
201840
201841/* Initialize a cursor according to the query plan idxNum using the
201842** arguments in argv[0]. See statBestIndex() for a description of the
201843** meaning of the bits in idxNum.
201844*/
201845static int statFilter(
201846 sqlite3_vtab_cursor *pCursor,
201847 int idxNum, const char *idxStr,
201848 int argc, sqlite3_value **argv
201849){
201850 StatCursor *pCsr = (StatCursor *)pCursor;
201851 StatTable *pTab = (StatTable*)(pCursor->pVtab);
201852 sqlite3_str *pSql; /* Query of btrees to analyze */
201853 char *zSql; /* String value of pSql */
201854 int iArg = 0; /* Count of argv[] parameters used so far */
201855 int rc = SQLITE_OK; /* Result of this operation */
201856 const char *zName = 0; /* Only provide analysis of this table */
201857
201858 statResetCsr(pCsr);
201859 sqlite3_finalize(pCsr->pStmt);
201860 pCsr->pStmt = 0;
201861 if( idxNum & 0x01 ){
201862 /* schema=? constraint is present. Get its value */
201863 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
201864 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
201865 if( pCsr->iDb<0 ){
201866 pCsr->iDb = 0;
201867 pCsr->isEof = 1;
201868 return SQLITE_OK;
201869 }
201870 }else{
201871 pCsr->iDb = pTab->iDb;
201872 }
201873 if( idxNum & 0x02 ){
201874 /* name=? constraint is present */
201875 zName = (const char*)sqlite3_value_text(argv[iArg++]);
201876 }
201877 if( idxNum & 0x04 ){
201878 /* aggregate=? constraint is present */
201879 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
201880 }else{
201881 pCsr->isAgg = 0;
201882 }
201883 pSql = sqlite3_str_new(pTab->db);
201884 sqlite3_str_appendf(pSql,
201885 "SELECT * FROM ("
201886 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
201887 " UNION ALL "
201888 "SELECT name,rootpage,type"
201889 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
201890 pTab->db->aDb[pCsr->iDb].zDbSName);
201891 if( zName ){
201892 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
201893 }
201894 if( idxNum & 0x08 ){
201895 sqlite3_str_appendf(pSql, " ORDER BY name");
201896 }
201897 zSql = sqlite3_str_finish(pSql);
201898 if( zSql==0 ){
201899 return SQLITE_NOMEM_BKPT;
201900 }else{
201901 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
201902 sqlite3_free(zSql);
201903 }
201904
201905 if( rc==SQLITE_OK ){
201906 rc = statNext(pCursor);
201907 }
201908 return rc;
201909}
201910
201911static int statColumn(
201912 sqlite3_vtab_cursor *pCursor,
201913 sqlite3_context *ctx,
201914 int i
201915){
201916 StatCursor *pCsr = (StatCursor *)pCursor;
201917 switch( i ){
201918 case 0: /* name */
201919 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
201920 break;
201921 case 1: /* path */
201922 if( !pCsr->isAgg ){
201923 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
201924 }
201925 break;
201926 case 2: /* pageno */
201927 if( pCsr->isAgg ){
201928 sqlite3_result_int64(ctx, pCsr->nPage);
201929 }else{
201930 sqlite3_result_int64(ctx, pCsr->iPageno);
201931 }
201932 break;
201933 case 3: /* pagetype */
201934 if( !pCsr->isAgg ){
201935 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
201936 }
201937 break;
201938 case 4: /* ncell */
201939 sqlite3_result_int(ctx, pCsr->nCell);
201940 break;
201941 case 5: /* payload */
201942 sqlite3_result_int(ctx, pCsr->nPayload);
201943 break;
201944 case 6: /* unused */
201945 sqlite3_result_int(ctx, pCsr->nUnused);
201946 break;
201947 case 7: /* mx_payload */
201948 sqlite3_result_int(ctx, pCsr->nMxPayload);
201949 break;
201950 case 8: /* pgoffset */
201951 if( !pCsr->isAgg ){
201952 sqlite3_result_int64(ctx, pCsr->iOffset);
201953 }
201954 break;
201955 case 9: /* pgsize */
201956 sqlite3_result_int(ctx, pCsr->szPage);
201957 break;
201958 case 10: { /* schema */
201959 sqlite3 *db = sqlite3_context_db_handle(ctx);
201960 int iDb = pCsr->iDb;
201961 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
201962 break;
201963 }
201964 default: { /* aggregate */
201965 sqlite3_result_int(ctx, pCsr->isAgg);
201966 break;
201967 }
201968 }
201969 return SQLITE_OK;
201970}
201971
201972static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
201973 StatCursor *pCsr = (StatCursor *)pCursor;
201974 *pRowid = pCsr->iPageno;
201975 return SQLITE_OK;
201976}
201977
201978/*
201979** Invoke this routine to register the "dbstat" virtual table module
201980*/
201981SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
201982 static sqlite3_module dbstat_module = {
201983 0, /* iVersion */
201984 statConnect, /* xCreate */
201985 statConnect, /* xConnect */
201986 statBestIndex, /* xBestIndex */
201987 statDisconnect, /* xDisconnect */
201988 statDisconnect, /* xDestroy */
201989 statOpen, /* xOpen - open a cursor */
201990 statClose, /* xClose - close a cursor */
201991 statFilter, /* xFilter - configure scan constraints */
201992 statNext, /* xNext - advance a cursor */
201993 statEof, /* xEof - check for end of scan */
201994 statColumn, /* xColumn - read data */
201995 statRowid, /* xRowid - read data */
201996 0, /* xUpdate */
201997 0, /* xBegin */
201998 0, /* xSync */
201999 0, /* xCommit */
202000 0, /* xRollback */
202001 0, /* xFindMethod */
202002 0, /* xRename */
202003 0, /* xSavepoint */
202004 0, /* xRelease */
202005 0, /* xRollbackTo */
202006 0 /* xShadowName */
202007 };
202008 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
202009}
202010#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
202011SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
202012#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
202013
202014/************** End of dbstat.c **********************************************/
202015/************** Begin file dbpage.c ******************************************/
202016/*
202017** 2017-10-11
202018**
202019** The author disclaims copyright to this source code. In place of
202020** a legal notice, here is a blessing:
202021**
202022** May you do good and not evil.
202023** May you find forgiveness for yourself and forgive others.
202024** May you share freely, never taking more than you give.
202025**
202026******************************************************************************
202027**
202028** This file contains an implementation of the "sqlite_dbpage" virtual table.
202029**
202030** The sqlite_dbpage virtual table is used to read or write whole raw
202031** pages of the database file. The pager interface is used so that
202032** uncommitted changes and changes recorded in the WAL file are correctly
202033** retrieved.
202034**
202035** Usage example:
202036**
202037** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
202038**
202039** This is an eponymous virtual table so it does not need to be created before
202040** use. The optional argument to the sqlite_dbpage() table name is the
202041** schema for the database file that is to be read. The default schema is
202042** "main".
202043**
202044** The data field of sqlite_dbpage table can be updated. The new
202045** value must be a BLOB which is the correct page size, otherwise the
202046** update fails. Rows may not be deleted or inserted.
202047*/
202048
202049/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
202050#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
202051 && !defined(SQLITE_OMIT_VIRTUALTABLE)
202052
202053typedef struct DbpageTable DbpageTable;
202054typedef struct DbpageCursor DbpageCursor;
202055
202056struct DbpageCursor {
202057 sqlite3_vtab_cursor base; /* Base class. Must be first */
202058 int pgno; /* Current page number */
202059 int mxPgno; /* Last page to visit on this scan */
202060 Pager *pPager; /* Pager being read/written */
202061 DbPage *pPage1; /* Page 1 of the database */
202062 int iDb; /* Index of database to analyze */
202063 int szPage; /* Size of each page in bytes */
202064};
202065
202066struct DbpageTable {
202067 sqlite3_vtab base; /* Base class. Must be first */
202068 sqlite3 *db; /* The database */
202069};
202070
202071/* Columns */
202072#define DBPAGE_COLUMN_PGNO 0
202073#define DBPAGE_COLUMN_DATA 1
202074#define DBPAGE_COLUMN_SCHEMA 2
202075
202076
202077
202078/*
202079** Connect to or create a dbpagevfs virtual table.
202080*/
202081static int dbpageConnect(
202082 sqlite3 *db,
202083 void *pAux,
202084 int argc, const char *const*argv,
202085 sqlite3_vtab **ppVtab,
202086 char **pzErr
202087){
202088 DbpageTable *pTab = 0;
202089 int rc = SQLITE_OK;
202090
202091 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
202092 rc = sqlite3_declare_vtab(db,
202093 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
202094 if( rc==SQLITE_OK ){
202095 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
202096 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
202097 }
202098
202099 assert( rc==SQLITE_OK || pTab==0 );
202100 if( rc==SQLITE_OK ){
202101 memset(pTab, 0, sizeof(DbpageTable));
202102 pTab->db = db;
202103 }
202104
202105 *ppVtab = (sqlite3_vtab*)pTab;
202106 return rc;
202107}
202108
202109/*
202110** Disconnect from or destroy a dbpagevfs virtual table.
202111*/
202112static int dbpageDisconnect(sqlite3_vtab *pVtab){
202113 sqlite3_free(pVtab);
202114 return SQLITE_OK;
202115}
202116
202117/*
202118** idxNum:
202119**
202120** 0 schema=main, full table scan
202121** 1 schema=main, pgno=?1
202122** 2 schema=?1, full table scan
202123** 3 schema=?1, pgno=?2
202124*/
202125static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
202126 int i;
202127 int iPlan = 0;
202128
202129 /* If there is a schema= constraint, it must be honored. Report a
202130 ** ridiculously large estimated cost if the schema= constraint is
202131 ** unavailable
202132 */
202133 for(i=0; i<pIdxInfo->nConstraint; i++){
202134 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
202135 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
202136 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
202137 if( !p->usable ){
202138 /* No solution. */
202139 return SQLITE_CONSTRAINT;
202140 }
202141 iPlan = 2;
202142 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
202143 pIdxInfo->aConstraintUsage[i].omit = 1;
202144 break;
202145 }
202146
202147 /* If we reach this point, it means that either there is no schema=
202148 ** constraint (in which case we use the "main" schema) or else the
202149 ** schema constraint was accepted. Lower the estimated cost accordingly
202150 */
202151 pIdxInfo->estimatedCost = 1.0e6;
202152
202153 /* Check for constraints against pgno */
202154 for(i=0; i<pIdxInfo->nConstraint; i++){
202155 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
202156 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
202157 pIdxInfo->estimatedRows = 1;
202158 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
202159 pIdxInfo->estimatedCost = 1.0;
202160 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
202161 pIdxInfo->aConstraintUsage[i].omit = 1;
202162 iPlan |= 1;
202163 break;
202164 }
202165 }
202166 pIdxInfo->idxNum = iPlan;
202167
202168 if( pIdxInfo->nOrderBy>=1
202169 && pIdxInfo->aOrderBy[0].iColumn<=0
202170 && pIdxInfo->aOrderBy[0].desc==0
202171 ){
202172 pIdxInfo->orderByConsumed = 1;
202173 }
202174 return SQLITE_OK;
202175}
202176
202177/*
202178** Open a new dbpagevfs cursor.
202179*/
202180static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
202181 DbpageCursor *pCsr;
202182
202183 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
202184 if( pCsr==0 ){
202185 return SQLITE_NOMEM_BKPT;
202186 }else{
202187 memset(pCsr, 0, sizeof(DbpageCursor));
202188 pCsr->base.pVtab = pVTab;
202189 pCsr->pgno = -1;
202190 }
202191
202192 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
202193 return SQLITE_OK;
202194}
202195
202196/*
202197** Close a dbpagevfs cursor.
202198*/
202199static int dbpageClose(sqlite3_vtab_cursor *pCursor){
202200 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202201 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
202202 sqlite3_free(pCsr);
202203 return SQLITE_OK;
202204}
202205
202206/*
202207** Move a dbpagevfs cursor to the next entry in the file.
202208*/
202209static int dbpageNext(sqlite3_vtab_cursor *pCursor){
202210 int rc = SQLITE_OK;
202211 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202212 pCsr->pgno++;
202213 return rc;
202214}
202215
202216static int dbpageEof(sqlite3_vtab_cursor *pCursor){
202217 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202218 return pCsr->pgno > pCsr->mxPgno;
202219}
202220
202221/*
202222** idxNum:
202223**
202224** 0 schema=main, full table scan
202225** 1 schema=main, pgno=?1
202226** 2 schema=?1, full table scan
202227** 3 schema=?1, pgno=?2
202228**
202229** idxStr is not used
202230*/
202231static int dbpageFilter(
202232 sqlite3_vtab_cursor *pCursor,
202233 int idxNum, const char *idxStr,
202234 int argc, sqlite3_value **argv
202235){
202236 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202237 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
202238 int rc;
202239 sqlite3 *db = pTab->db;
202240 Btree *pBt;
202241
202242 /* Default setting is no rows of result */
202243 pCsr->pgno = 1;
202244 pCsr->mxPgno = 0;
202245
202246 if( idxNum & 2 ){
202247 const char *zSchema;
202248 assert( argc>=1 );
202249 zSchema = (const char*)sqlite3_value_text(argv[0]);
202250 pCsr->iDb = sqlite3FindDbName(db, zSchema);
202251 if( pCsr->iDb<0 ) return SQLITE_OK;
202252 }else{
202253 pCsr->iDb = 0;
202254 }
202255 pBt = db->aDb[pCsr->iDb].pBt;
202256 if( pBt==0 ) return SQLITE_OK;
202257 pCsr->pPager = sqlite3BtreePager(pBt);
202258 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
202259 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
202260 if( idxNum & 1 ){
202261 assert( argc>(idxNum>>1) );
202262 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
202263 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
202264 pCsr->pgno = 1;
202265 pCsr->mxPgno = 0;
202266 }else{
202267 pCsr->mxPgno = pCsr->pgno;
202268 }
202269 }else{
202270 assert( pCsr->pgno==1 );
202271 }
202272 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
202273 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
202274 return rc;
202275}
202276
202277static int dbpageColumn(
202278 sqlite3_vtab_cursor *pCursor,
202279 sqlite3_context *ctx,
202280 int i
202281){
202282 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202283 int rc = SQLITE_OK;
202284 switch( i ){
202285 case 0: { /* pgno */
202286 sqlite3_result_int(ctx, pCsr->pgno);
202287 break;
202288 }
202289 case 1: { /* data */
202290 DbPage *pDbPage = 0;
202291 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
202292 if( rc==SQLITE_OK ){
202293 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
202294 SQLITE_TRANSIENT);
202295 }
202296 sqlite3PagerUnref(pDbPage);
202297 break;
202298 }
202299 default: { /* schema */
202300 sqlite3 *db = sqlite3_context_db_handle(ctx);
202301 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
202302 break;
202303 }
202304 }
202305 return SQLITE_OK;
202306}
202307
202308static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
202309 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
202310 *pRowid = pCsr->pgno;
202311 return SQLITE_OK;
202312}
202313
202314static int dbpageUpdate(
202315 sqlite3_vtab *pVtab,
202316 int argc,
202317 sqlite3_value **argv,
202318 sqlite_int64 *pRowid
202319){
202320 DbpageTable *pTab = (DbpageTable *)pVtab;
202321 Pgno pgno;
202322 DbPage *pDbPage = 0;
202323 int rc = SQLITE_OK;
202324 char *zErr = 0;
202325 const char *zSchema;
202326 int iDb;
202327 Btree *pBt;
202328 Pager *pPager;
202329 int szPage;
202330
202331 if( pTab->db->flags & SQLITE_Defensive ){
202332 zErr = "read-only";
202333 goto update_fail;
202334 }
202335 if( argc==1 ){
202336 zErr = "cannot delete";
202337 goto update_fail;
202338 }
202339 pgno = sqlite3_value_int(argv[0]);
202340 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
202341 zErr = "cannot insert";
202342 goto update_fail;
202343 }
202344 zSchema = (const char*)sqlite3_value_text(argv[4]);
202345 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
202346 if( iDb<0 ){
202347 zErr = "no such schema";
202348 goto update_fail;
202349 }
202350 pBt = pTab->db->aDb[iDb].pBt;
202351 if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
202352 zErr = "bad page number";
202353 goto update_fail;
202354 }
202355 szPage = sqlite3BtreeGetPageSize(pBt);
202356 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
202357 || sqlite3_value_bytes(argv[3])!=szPage
202358 ){
202359 zErr = "bad page value";
202360 goto update_fail;
202361 }
202362 pPager = sqlite3BtreePager(pBt);
202363 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
202364 if( rc==SQLITE_OK ){
202365 rc = sqlite3PagerWrite(pDbPage);
202366 if( rc==SQLITE_OK ){
202367 memcpy(sqlite3PagerGetData(pDbPage),
202368 sqlite3_value_blob(argv[3]),
202369 szPage);
202370 }
202371 }
202372 sqlite3PagerUnref(pDbPage);
202373 return rc;
202374
202375update_fail:
202376 sqlite3_free(pVtab->zErrMsg);
202377 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
202378 return SQLITE_ERROR;
202379}
202380
202381/* Since we do not know in advance which database files will be
202382** written by the sqlite_dbpage virtual table, start a write transaction
202383** on them all.
202384*/
202385static int dbpageBegin(sqlite3_vtab *pVtab){
202386 DbpageTable *pTab = (DbpageTable *)pVtab;
202387 sqlite3 *db = pTab->db;
202388 int i;
202389 for(i=0; i<db->nDb; i++){
202390 Btree *pBt = db->aDb[i].pBt;
202391 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
202392 }
202393 return SQLITE_OK;
202394}
202395
202396
202397/*
202398** Invoke this routine to register the "dbpage" virtual table module
202399*/
202400SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
202401 static sqlite3_module dbpage_module = {
202402 0, /* iVersion */
202403 dbpageConnect, /* xCreate */
202404 dbpageConnect, /* xConnect */
202405 dbpageBestIndex, /* xBestIndex */
202406 dbpageDisconnect, /* xDisconnect */
202407 dbpageDisconnect, /* xDestroy */
202408 dbpageOpen, /* xOpen - open a cursor */
202409 dbpageClose, /* xClose - close a cursor */
202410 dbpageFilter, /* xFilter - configure scan constraints */
202411 dbpageNext, /* xNext - advance a cursor */
202412 dbpageEof, /* xEof - check for end of scan */
202413 dbpageColumn, /* xColumn - read data */
202414 dbpageRowid, /* xRowid - read data */
202415 dbpageUpdate, /* xUpdate */
202416 dbpageBegin, /* xBegin */
202417 0, /* xSync */
202418 0, /* xCommit */
202419 0, /* xRollback */
202420 0, /* xFindMethod */
202421 0, /* xRename */
202422 0, /* xSavepoint */
202423 0, /* xRelease */
202424 0, /* xRollbackTo */
202425 0 /* xShadowName */
202426 };
202427 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
202428}
202429#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
202430SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
202431#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
202432
202433/************** End of dbpage.c **********************************************/
202434/************** Begin file sqlite3session.c **********************************/
202435
202436#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
202437/* #include "sqlite3session.h" */
202438/* #include <assert.h> */
202439/* #include <string.h> */
202440
202441#ifndef SQLITE_AMALGAMATION
202442/* # include "sqliteInt.h" */
202443/* # include "vdbeInt.h" */
202444#endif
202445
202446typedef struct SessionTable SessionTable;
202447typedef struct SessionChange SessionChange;
202448typedef struct SessionBuffer SessionBuffer;
202449typedef struct SessionInput SessionInput;
202450
202451/*
202452** Minimum chunk size used by streaming versions of functions.
202453*/
202454#ifndef SESSIONS_STRM_CHUNK_SIZE
202455# ifdef SQLITE_TEST
202456# define SESSIONS_STRM_CHUNK_SIZE 64
202457# else
202458# define SESSIONS_STRM_CHUNK_SIZE 1024
202459# endif
202460#endif
202461
202462static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
202463
202464typedef struct SessionHook SessionHook;
202465struct SessionHook {
202466 void *pCtx;
202467 int (*xOld)(void*,int,sqlite3_value**);
202468 int (*xNew)(void*,int,sqlite3_value**);
202469 int (*xCount)(void*);
202470 int (*xDepth)(void*);
202471};
202472
202473/*
202474** Session handle structure.
202475*/
202476struct sqlite3_session {
202477 sqlite3 *db; /* Database handle session is attached to */
202478 char *zDb; /* Name of database session is attached to */
202479 int bEnable; /* True if currently recording */
202480 int bIndirect; /* True if all changes are indirect */
202481 int bAutoAttach; /* True to auto-attach tables */
202482 int rc; /* Non-zero if an error has occurred */
202483 void *pFilterCtx; /* First argument to pass to xTableFilter */
202484 int (*xTableFilter)(void *pCtx, const char *zTab);
202485 sqlite3_value *pZeroBlob; /* Value containing X'' */
202486 sqlite3_session *pNext; /* Next session object on same db. */
202487 SessionTable *pTable; /* List of attached tables */
202488 SessionHook hook; /* APIs to grab new and old data with */
202489};
202490
202491/*
202492** Instances of this structure are used to build strings or binary records.
202493*/
202494struct SessionBuffer {
202495 u8 *aBuf; /* Pointer to changeset buffer */
202496 int nBuf; /* Size of buffer aBuf */
202497 int nAlloc; /* Size of allocation containing aBuf */
202498};
202499
202500/*
202501** An object of this type is used internally as an abstraction for
202502** input data. Input data may be supplied either as a single large buffer
202503** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
202504** sqlite3changeset_start_strm()).
202505*/
202506struct SessionInput {
202507 int bNoDiscard; /* If true, do not discard in InputBuffer() */
202508 int iCurrent; /* Offset in aData[] of current change */
202509 int iNext; /* Offset in aData[] of next change */
202510 u8 *aData; /* Pointer to buffer containing changeset */
202511 int nData; /* Number of bytes in aData */
202512
202513 SessionBuffer buf; /* Current read buffer */
202514 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
202515 void *pIn; /* First argument to xInput */
202516 int bEof; /* Set to true after xInput finished */
202517};
202518
202519/*
202520** Structure for changeset iterators.
202521*/
202522struct sqlite3_changeset_iter {
202523 SessionInput in; /* Input buffer or stream */
202524 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
202525 int bPatchset; /* True if this is a patchset */
202526 int bInvert; /* True to invert changeset */
202527 int rc; /* Iterator error code */
202528 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
202529 char *zTab; /* Current table */
202530 int nCol; /* Number of columns in zTab */
202531 int op; /* Current operation */
202532 int bIndirect; /* True if current change was indirect */
202533 u8 *abPK; /* Primary key array */
202534 sqlite3_value **apValue; /* old.* and new.* values */
202535};
202536
202537/*
202538** Each session object maintains a set of the following structures, one
202539** for each table the session object is monitoring. The structures are
202540** stored in a linked list starting at sqlite3_session.pTable.
202541**
202542** The keys of the SessionTable.aChange[] hash table are all rows that have
202543** been modified in any way since the session object was attached to the
202544** table.
202545**
202546** The data associated with each hash-table entry is a structure containing
202547** a subset of the initial values that the modified row contained at the
202548** start of the session. Or no initial values if the row was inserted.
202549*/
202550struct SessionTable {
202551 SessionTable *pNext;
202552 char *zName; /* Local name of table */
202553 int nCol; /* Number of columns in table zName */
202554 int bStat1; /* True if this is sqlite_stat1 */
202555 const char **azCol; /* Column names */
202556 u8 *abPK; /* Array of primary key flags */
202557 int nEntry; /* Total number of entries in hash table */
202558 int nChange; /* Size of apChange[] array */
202559 SessionChange **apChange; /* Hash table buckets */
202560};
202561
202562/*
202563** RECORD FORMAT:
202564**
202565** The following record format is similar to (but not compatible with) that
202566** used in SQLite database files. This format is used as part of the
202567** change-set binary format, and so must be architecture independent.
202568**
202569** Unlike the SQLite database record format, each field is self-contained -
202570** there is no separation of header and data. Each field begins with a
202571** single byte describing its type, as follows:
202572**
202573** 0x00: Undefined value.
202574** 0x01: Integer value.
202575** 0x02: Real value.
202576** 0x03: Text value.
202577** 0x04: Blob value.
202578** 0x05: SQL NULL value.
202579**
202580** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
202581** and so on in sqlite3.h. For undefined and NULL values, the field consists
202582** only of the single type byte. For other types of values, the type byte
202583** is followed by:
202584**
202585** Text values:
202586** A varint containing the number of bytes in the value (encoded using
202587** UTF-8). Followed by a buffer containing the UTF-8 representation
202588** of the text value. There is no nul terminator.
202589**
202590** Blob values:
202591** A varint containing the number of bytes in the value, followed by
202592** a buffer containing the value itself.
202593**
202594** Integer values:
202595** An 8-byte big-endian integer value.
202596**
202597** Real values:
202598** An 8-byte big-endian IEEE 754-2008 real value.
202599**
202600** Varint values are encoded in the same way as varints in the SQLite
202601** record format.
202602**
202603** CHANGESET FORMAT:
202604**
202605** A changeset is a collection of DELETE, UPDATE and INSERT operations on
202606** one or more tables. Operations on a single table are grouped together,
202607** but may occur in any order (i.e. deletes, updates and inserts are all
202608** mixed together).
202609**
202610** Each group of changes begins with a table header:
202611**
202612** 1 byte: Constant 0x54 (capital 'T')
202613** Varint: Number of columns in the table.
202614** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
202615** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
202616**
202617** Followed by one or more changes to the table.
202618**
202619** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
202620** 1 byte: The "indirect-change" flag.
202621** old.* record: (delete and update only)
202622** new.* record: (insert and update only)
202623**
202624** The "old.*" and "new.*" records, if present, are N field records in the
202625** format described above under "RECORD FORMAT", where N is the number of
202626** columns in the table. The i'th field of each record is associated with
202627** the i'th column of the table, counting from left to right in the order
202628** in which columns were declared in the CREATE TABLE statement.
202629**
202630** The new.* record that is part of each INSERT change contains the values
202631** that make up the new row. Similarly, the old.* record that is part of each
202632** DELETE change contains the values that made up the row that was deleted
202633** from the database. In the changeset format, the records that are part
202634** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
202635** fields.
202636**
202637** Within the old.* record associated with an UPDATE change, all fields
202638** associated with table columns that are not PRIMARY KEY columns and are
202639** not modified by the UPDATE change are set to "undefined". Other fields
202640** are set to the values that made up the row before the UPDATE that the
202641** change records took place. Within the new.* record, fields associated
202642** with table columns modified by the UPDATE change contain the new
202643** values. Fields associated with table columns that are not modified
202644** are set to "undefined".
202645**
202646** PATCHSET FORMAT:
202647**
202648** A patchset is also a collection of changes. It is similar to a changeset,
202649** but leaves undefined those fields that are not useful if no conflict
202650** resolution is required when applying the changeset.
202651**
202652** Each group of changes begins with a table header:
202653**
202654** 1 byte: Constant 0x50 (capital 'P')
202655** Varint: Number of columns in the table.
202656** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
202657** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
202658**
202659** Followed by one or more changes to the table.
202660**
202661** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
202662** 1 byte: The "indirect-change" flag.
202663** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
202664** full record for INSERT).
202665**
202666** As in the changeset format, each field of the single record that is part
202667** of a patchset change is associated with the correspondingly positioned
202668** table column, counting from left to right within the CREATE TABLE
202669** statement.
202670**
202671** For a DELETE change, all fields within the record except those associated
202672** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
202673** values identifying the row to delete.
202674**
202675** For an UPDATE change, all fields except those associated with PRIMARY KEY
202676** columns and columns that are modified by the UPDATE are set to "undefined".
202677** PRIMARY KEY fields contain the values identifying the table row to update,
202678** and fields associated with modified columns contain the new column values.
202679**
202680** The records associated with INSERT changes are in the same format as for
202681** changesets. It is not possible for a record associated with an INSERT
202682** change to contain a field set to "undefined".
202683**
202684** REBASE BLOB FORMAT:
202685**
202686** A rebase blob may be output by sqlite3changeset_apply_v2() and its
202687** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
202688** existing changesets. A rebase blob contains one entry for each conflict
202689** resolved using either the OMIT or REPLACE strategies within the apply_v2()
202690** call.
202691**
202692** The format used for a rebase blob is very similar to that used for
202693** changesets. All entries related to a single table are grouped together.
202694**
202695** Each group of entries begins with a table header in changeset format:
202696**
202697** 1 byte: Constant 0x54 (capital 'T')
202698** Varint: Number of columns in the table.
202699** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
202700** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
202701**
202702** Followed by one or more entries associated with the table.
202703**
202704** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
202705** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
202706** record: (in the record format defined above).
202707**
202708** In a rebase blob, the first field is set to SQLITE_INSERT if the change
202709** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
202710** it was a DELETE. The second field is set to 0x01 if the conflict
202711** resolution strategy was REPLACE, or 0x00 if it was OMIT.
202712**
202713** If the change that caused the conflict was a DELETE, then the single
202714** record is a copy of the old.* record from the original changeset. If it
202715** was an INSERT, then the single record is a copy of the new.* record. If
202716** the conflicting change was an UPDATE, then the single record is a copy
202717** of the new.* record with the PK fields filled in based on the original
202718** old.* record.
202719*/
202720
202721/*
202722** For each row modified during a session, there exists a single instance of
202723** this structure stored in a SessionTable.aChange[] hash table.
202724*/
202725struct SessionChange {
202726 int op; /* One of UPDATE, DELETE, INSERT */
202727 int bIndirect; /* True if this change is "indirect" */
202728 int nRecord; /* Number of bytes in buffer aRecord[] */
202729 u8 *aRecord; /* Buffer containing old.* record */
202730 SessionChange *pNext; /* For hash-table collisions */
202731};
202732
202733/*
202734** Write a varint with value iVal into the buffer at aBuf. Return the
202735** number of bytes written.
202736*/
202737static int sessionVarintPut(u8 *aBuf, int iVal){
202738 return putVarint32(aBuf, iVal);
202739}
202740
202741/*
202742** Return the number of bytes required to store value iVal as a varint.
202743*/
202744static int sessionVarintLen(int iVal){
202745 return sqlite3VarintLen(iVal);
202746}
202747
202748/*
202749** Read a varint value from aBuf[] into *piVal. Return the number of
202750** bytes read.
202751*/
202752static int sessionVarintGet(u8 *aBuf, int *piVal){
202753 return getVarint32(aBuf, *piVal);
202754}
202755
202756/* Load an unaligned and unsigned 32-bit integer */
202757#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
202758
202759/*
202760** Read a 64-bit big-endian integer value from buffer aRec[]. Return
202761** the value read.
202762*/
202763static sqlite3_int64 sessionGetI64(u8 *aRec){
202764 u64 x = SESSION_UINT32(aRec);
202765 u32 y = SESSION_UINT32(aRec+4);
202766 x = (x<<32) + y;
202767 return (sqlite3_int64)x;
202768}
202769
202770/*
202771** Write a 64-bit big-endian integer value to the buffer aBuf[].
202772*/
202773static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
202774 aBuf[0] = (i>>56) & 0xFF;
202775 aBuf[1] = (i>>48) & 0xFF;
202776 aBuf[2] = (i>>40) & 0xFF;
202777 aBuf[3] = (i>>32) & 0xFF;
202778 aBuf[4] = (i>>24) & 0xFF;
202779 aBuf[5] = (i>>16) & 0xFF;
202780 aBuf[6] = (i>> 8) & 0xFF;
202781 aBuf[7] = (i>> 0) & 0xFF;
202782}
202783
202784/*
202785** This function is used to serialize the contents of value pValue (see
202786** comment titled "RECORD FORMAT" above).
202787**
202788** If it is non-NULL, the serialized form of the value is written to
202789** buffer aBuf. *pnWrite is set to the number of bytes written before
202790** returning. Or, if aBuf is NULL, the only thing this function does is
202791** set *pnWrite.
202792**
202793** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
202794** within a call to sqlite3_value_text() (may fail if the db is utf-16))
202795** SQLITE_NOMEM is returned.
202796*/
202797static int sessionSerializeValue(
202798 u8 *aBuf, /* If non-NULL, write serialized value here */
202799 sqlite3_value *pValue, /* Value to serialize */
202800 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
202801){
202802 int nByte; /* Size of serialized value in bytes */
202803
202804 if( pValue ){
202805 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
202806
202807 eType = sqlite3_value_type(pValue);
202808 if( aBuf ) aBuf[0] = eType;
202809
202810 switch( eType ){
202811 case SQLITE_NULL:
202812 nByte = 1;
202813 break;
202814
202815 case SQLITE_INTEGER:
202816 case SQLITE_FLOAT:
202817 if( aBuf ){
202818 /* TODO: SQLite does something special to deal with mixed-endian
202819 ** floating point values (e.g. ARM7). This code probably should
202820 ** too. */
202821 u64 i;
202822 if( eType==SQLITE_INTEGER ){
202823 i = (u64)sqlite3_value_int64(pValue);
202824 }else{
202825 double r;
202826 assert( sizeof(double)==8 && sizeof(u64)==8 );
202827 r = sqlite3_value_double(pValue);
202828 memcpy(&i, &r, 8);
202829 }
202830 sessionPutI64(&aBuf[1], i);
202831 }
202832 nByte = 9;
202833 break;
202834
202835 default: {
202836 u8 *z;
202837 int n;
202838 int nVarint;
202839
202840 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
202841 if( eType==SQLITE_TEXT ){
202842 z = (u8 *)sqlite3_value_text(pValue);
202843 }else{
202844 z = (u8 *)sqlite3_value_blob(pValue);
202845 }
202846 n = sqlite3_value_bytes(pValue);
202847 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
202848 nVarint = sessionVarintLen(n);
202849
202850 if( aBuf ){
202851 sessionVarintPut(&aBuf[1], n);
202852 if( n ) memcpy(&aBuf[nVarint + 1], z, n);
202853 }
202854
202855 nByte = 1 + nVarint + n;
202856 break;
202857 }
202858 }
202859 }else{
202860 nByte = 1;
202861 if( aBuf ) aBuf[0] = '\0';
202862 }
202863
202864 if( pnWrite ) *pnWrite += nByte;
202865 return SQLITE_OK;
202866}
202867
202868
202869/*
202870** This macro is used to calculate hash key values for data structures. In
202871** order to use this macro, the entire data structure must be represented
202872** as a series of unsigned integers. In order to calculate a hash-key value
202873** for a data structure represented as three such integers, the macro may
202874** then be used as follows:
202875**
202876** int hash_key_value;
202877** hash_key_value = HASH_APPEND(0, <value 1>);
202878** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
202879** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
202880**
202881** In practice, the data structures this macro is used for are the primary
202882** key values of modified rows.
202883*/
202884#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
202885
202886/*
202887** Append the hash of the 64-bit integer passed as the second argument to the
202888** hash-key value passed as the first. Return the new hash-key value.
202889*/
202890static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
202891 h = HASH_APPEND(h, i & 0xFFFFFFFF);
202892 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
202893}
202894
202895/*
202896** Append the hash of the blob passed via the second and third arguments to
202897** the hash-key value passed as the first. Return the new hash-key value.
202898*/
202899static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
202900 int i;
202901 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
202902 return h;
202903}
202904
202905/*
202906** Append the hash of the data type passed as the second argument to the
202907** hash-key value passed as the first. Return the new hash-key value.
202908*/
202909static unsigned int sessionHashAppendType(unsigned int h, int eType){
202910 return HASH_APPEND(h, eType);
202911}
202912
202913/*
202914** This function may only be called from within a pre-update callback.
202915** It calculates a hash based on the primary key values of the old.* or
202916** new.* row currently available and, assuming no error occurs, writes it to
202917** *piHash before returning. If the primary key contains one or more NULL
202918** values, *pbNullPK is set to true before returning.
202919**
202920** If an error occurs, an SQLite error code is returned and the final values
202921** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
202922** and the output variables are set as described above.
202923*/
202924static int sessionPreupdateHash(
202925 sqlite3_session *pSession, /* Session object that owns pTab */
202926 SessionTable *pTab, /* Session table handle */
202927 int bNew, /* True to hash the new.* PK */
202928 int *piHash, /* OUT: Hash value */
202929 int *pbNullPK /* OUT: True if there are NULL values in PK */
202930){
202931 unsigned int h = 0; /* Hash value to return */
202932 int i; /* Used to iterate through columns */
202933
202934 assert( *pbNullPK==0 );
202935 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
202936 for(i=0; i<pTab->nCol; i++){
202937 if( pTab->abPK[i] ){
202938 int rc;
202939 int eType;
202940 sqlite3_value *pVal;
202941
202942 if( bNew ){
202943 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
202944 }else{
202945 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
202946 }
202947 if( rc!=SQLITE_OK ) return rc;
202948
202949 eType = sqlite3_value_type(pVal);
202950 h = sessionHashAppendType(h, eType);
202951 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
202952 i64 iVal;
202953 if( eType==SQLITE_INTEGER ){
202954 iVal = sqlite3_value_int64(pVal);
202955 }else{
202956 double rVal = sqlite3_value_double(pVal);
202957 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
202958 memcpy(&iVal, &rVal, 8);
202959 }
202960 h = sessionHashAppendI64(h, iVal);
202961 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
202962 const u8 *z;
202963 int n;
202964 if( eType==SQLITE_TEXT ){
202965 z = (const u8 *)sqlite3_value_text(pVal);
202966 }else{
202967 z = (const u8 *)sqlite3_value_blob(pVal);
202968 }
202969 n = sqlite3_value_bytes(pVal);
202970 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
202971 h = sessionHashAppendBlob(h, n, z);
202972 }else{
202973 assert( eType==SQLITE_NULL );
202974 assert( pTab->bStat1==0 || i!=1 );
202975 *pbNullPK = 1;
202976 }
202977 }
202978 }
202979
202980 *piHash = (h % pTab->nChange);
202981 return SQLITE_OK;
202982}
202983
202984/*
202985** The buffer that the argument points to contains a serialized SQL value.
202986** Return the number of bytes of space occupied by the value (including
202987** the type byte).
202988*/
202989static int sessionSerialLen(u8 *a){
202990 int e = *a;
202991 int n;
202992 if( e==0 || e==0xFF ) return 1;
202993 if( e==SQLITE_NULL ) return 1;
202994 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
202995 return sessionVarintGet(&a[1], &n) + 1 + n;
202996}
202997
202998/*
202999** Based on the primary key values stored in change aRecord, calculate a
203000** hash key. Assume the has table has nBucket buckets. The hash keys
203001** calculated by this function are compatible with those calculated by
203002** sessionPreupdateHash().
203003**
203004** The bPkOnly argument is non-zero if the record at aRecord[] is from
203005** a patchset DELETE. In this case the non-PK fields are omitted entirely.
203006*/
203007static unsigned int sessionChangeHash(
203008 SessionTable *pTab, /* Table handle */
203009 int bPkOnly, /* Record consists of PK fields only */
203010 u8 *aRecord, /* Change record */
203011 int nBucket /* Assume this many buckets in hash table */
203012){
203013 unsigned int h = 0; /* Value to return */
203014 int i; /* Used to iterate through columns */
203015 u8 *a = aRecord; /* Used to iterate through change record */
203016
203017 for(i=0; i<pTab->nCol; i++){
203018 int eType = *a;
203019 int isPK = pTab->abPK[i];
203020 if( bPkOnly && isPK==0 ) continue;
203021
203022 /* It is not possible for eType to be SQLITE_NULL here. The session
203023 ** module does not record changes for rows with NULL values stored in
203024 ** primary key columns. */
203025 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
203026 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
203027 || eType==SQLITE_NULL || eType==0
203028 );
203029 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
203030
203031 if( isPK ){
203032 a++;
203033 h = sessionHashAppendType(h, eType);
203034 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
203035 h = sessionHashAppendI64(h, sessionGetI64(a));
203036 a += 8;
203037 }else{
203038 int n;
203039 a += sessionVarintGet(a, &n);
203040 h = sessionHashAppendBlob(h, n, a);
203041 a += n;
203042 }
203043 }else{
203044 a += sessionSerialLen(a);
203045 }
203046 }
203047 return (h % nBucket);
203048}
203049
203050/*
203051** Arguments aLeft and aRight are pointers to change records for table pTab.
203052** This function returns true if the two records apply to the same row (i.e.
203053** have the same values stored in the primary key columns), or false
203054** otherwise.
203055*/
203056static int sessionChangeEqual(
203057 SessionTable *pTab, /* Table used for PK definition */
203058 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
203059 u8 *aLeft, /* Change record */
203060 int bRightPkOnly, /* True if aRight[] contains PK fields only */
203061 u8 *aRight /* Change record */
203062){
203063 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
203064 u8 *a2 = aRight; /* Cursor to iterate through aRight */
203065 int iCol; /* Used to iterate through table columns */
203066
203067 for(iCol=0; iCol<pTab->nCol; iCol++){
203068 if( pTab->abPK[iCol] ){
203069 int n1 = sessionSerialLen(a1);
203070 int n2 = sessionSerialLen(a2);
203071
203072 if( n1!=n2 || memcmp(a1, a2, n1) ){
203073 return 0;
203074 }
203075 a1 += n1;
203076 a2 += n2;
203077 }else{
203078 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
203079 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
203080 }
203081 }
203082
203083 return 1;
203084}
203085
203086/*
203087** Arguments aLeft and aRight both point to buffers containing change
203088** records with nCol columns. This function "merges" the two records into
203089** a single records which is written to the buffer at *paOut. *paOut is
203090** then set to point to one byte after the last byte written before
203091** returning.
203092**
203093** The merging of records is done as follows: For each column, if the
203094** aRight record contains a value for the column, copy the value from
203095** their. Otherwise, if aLeft contains a value, copy it. If neither
203096** record contains a value for a given column, then neither does the
203097** output record.
203098*/
203099static void sessionMergeRecord(
203100 u8 **paOut,
203101 int nCol,
203102 u8 *aLeft,
203103 u8 *aRight
203104){
203105 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
203106 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
203107 u8 *aOut = *paOut; /* Output cursor */
203108 int iCol; /* Used to iterate from 0 to nCol */
203109
203110 for(iCol=0; iCol<nCol; iCol++){
203111 int n1 = sessionSerialLen(a1);
203112 int n2 = sessionSerialLen(a2);
203113 if( *a2 ){
203114 memcpy(aOut, a2, n2);
203115 aOut += n2;
203116 }else{
203117 memcpy(aOut, a1, n1);
203118 aOut += n1;
203119 }
203120 a1 += n1;
203121 a2 += n2;
203122 }
203123
203124 *paOut = aOut;
203125}
203126
203127/*
203128** This is a helper function used by sessionMergeUpdate().
203129**
203130** When this function is called, both *paOne and *paTwo point to a value
203131** within a change record. Before it returns, both have been advanced so
203132** as to point to the next value in the record.
203133**
203134** If, when this function is called, *paTwo points to a valid value (i.e.
203135** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
203136** pointer is returned and *pnVal is set to the number of bytes in the
203137** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
203138** set to the number of bytes in the value at *paOne. If *paOne points
203139** to the "no value" placeholder, *pnVal is set to 1. In other words:
203140**
203141** if( *paTwo is valid ) return *paTwo;
203142** return *paOne;
203143**
203144*/
203145static u8 *sessionMergeValue(
203146 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
203147 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
203148 int *pnVal /* OUT: Bytes in returned value */
203149){
203150 u8 *a1 = *paOne;
203151 u8 *a2 = *paTwo;
203152 u8 *pRet = 0;
203153 int n1;
203154
203155 assert( a1 );
203156 if( a2 ){
203157 int n2 = sessionSerialLen(a2);
203158 if( *a2 ){
203159 *pnVal = n2;
203160 pRet = a2;
203161 }
203162 *paTwo = &a2[n2];
203163 }
203164
203165 n1 = sessionSerialLen(a1);
203166 if( pRet==0 ){
203167 *pnVal = n1;
203168 pRet = a1;
203169 }
203170 *paOne = &a1[n1];
203171
203172 return pRet;
203173}
203174
203175/*
203176** This function is used by changeset_concat() to merge two UPDATE changes
203177** on the same row.
203178*/
203179static int sessionMergeUpdate(
203180 u8 **paOut, /* IN/OUT: Pointer to output buffer */
203181 SessionTable *pTab, /* Table change pertains to */
203182 int bPatchset, /* True if records are patchset records */
203183 u8 *aOldRecord1, /* old.* record for first change */
203184 u8 *aOldRecord2, /* old.* record for second change */
203185 u8 *aNewRecord1, /* new.* record for first change */
203186 u8 *aNewRecord2 /* new.* record for second change */
203187){
203188 u8 *aOld1 = aOldRecord1;
203189 u8 *aOld2 = aOldRecord2;
203190 u8 *aNew1 = aNewRecord1;
203191 u8 *aNew2 = aNewRecord2;
203192
203193 u8 *aOut = *paOut;
203194 int i;
203195
203196 if( bPatchset==0 ){
203197 int bRequired = 0;
203198
203199 assert( aOldRecord1 && aNewRecord1 );
203200
203201 /* Write the old.* vector first. */
203202 for(i=0; i<pTab->nCol; i++){
203203 int nOld;
203204 u8 *aOld;
203205 int nNew;
203206 u8 *aNew;
203207
203208 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
203209 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
203210 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
203211 if( pTab->abPK[i]==0 ) bRequired = 1;
203212 memcpy(aOut, aOld, nOld);
203213 aOut += nOld;
203214 }else{
203215 *(aOut++) = '\0';
203216 }
203217 }
203218
203219 if( !bRequired ) return 0;
203220 }
203221
203222 /* Write the new.* vector */
203223 aOld1 = aOldRecord1;
203224 aOld2 = aOldRecord2;
203225 aNew1 = aNewRecord1;
203226 aNew2 = aNewRecord2;
203227 for(i=0; i<pTab->nCol; i++){
203228 int nOld;
203229 u8 *aOld;
203230 int nNew;
203231 u8 *aNew;
203232
203233 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
203234 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
203235 if( bPatchset==0
203236 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
203237 ){
203238 *(aOut++) = '\0';
203239 }else{
203240 memcpy(aOut, aNew, nNew);
203241 aOut += nNew;
203242 }
203243 }
203244
203245 *paOut = aOut;
203246 return 1;
203247}
203248
203249/*
203250** This function is only called from within a pre-update-hook callback.
203251** It determines if the current pre-update-hook change affects the same row
203252** as the change stored in argument pChange. If so, it returns true. Otherwise
203253** if the pre-update-hook does not affect the same row as pChange, it returns
203254** false.
203255*/
203256static int sessionPreupdateEqual(
203257 sqlite3_session *pSession, /* Session object that owns SessionTable */
203258 SessionTable *pTab, /* Table associated with change */
203259 SessionChange *pChange, /* Change to compare to */
203260 int op /* Current pre-update operation */
203261){
203262 int iCol; /* Used to iterate through columns */
203263 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
203264
203265 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
203266 for(iCol=0; iCol<pTab->nCol; iCol++){
203267 if( !pTab->abPK[iCol] ){
203268 a += sessionSerialLen(a);
203269 }else{
203270 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
203271 int rc; /* Error code from preupdate_new/old */
203272 int eType = *a++; /* Type of value from change record */
203273
203274 /* The following calls to preupdate_new() and preupdate_old() can not
203275 ** fail. This is because they cache their return values, and by the
203276 ** time control flows to here they have already been called once from
203277 ** within sessionPreupdateHash(). The first two asserts below verify
203278 ** this (that the method has already been called). */
203279 if( op==SQLITE_INSERT ){
203280 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
203281 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
203282 }else{
203283 /* assert( db->pPreUpdate->pUnpacked ); */
203284 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
203285 }
203286 assert( rc==SQLITE_OK );
203287 if( sqlite3_value_type(pVal)!=eType ) return 0;
203288
203289 /* A SessionChange object never has a NULL value in a PK column */
203290 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
203291 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
203292 );
203293
203294 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
203295 i64 iVal = sessionGetI64(a);
203296 a += 8;
203297 if( eType==SQLITE_INTEGER ){
203298 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
203299 }else{
203300 double rVal;
203301 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
203302 memcpy(&rVal, &iVal, 8);
203303 if( sqlite3_value_double(pVal)!=rVal ) return 0;
203304 }
203305 }else{
203306 int n;
203307 const u8 *z;
203308 a += sessionVarintGet(a, &n);
203309 if( sqlite3_value_bytes(pVal)!=n ) return 0;
203310 if( eType==SQLITE_TEXT ){
203311 z = sqlite3_value_text(pVal);
203312 }else{
203313 z = sqlite3_value_blob(pVal);
203314 }
203315 if( n>0 && memcmp(a, z, n) ) return 0;
203316 a += n;
203317 }
203318 }
203319 }
203320
203321 return 1;
203322}
203323
203324/*
203325** If required, grow the hash table used to store changes on table pTab
203326** (part of the session pSession). If a fatal OOM error occurs, set the
203327** session object to failed and return SQLITE_ERROR. Otherwise, return
203328** SQLITE_OK.
203329**
203330** It is possible that a non-fatal OOM error occurs in this function. In
203331** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
203332** Growing the hash table in this case is a performance optimization only,
203333** it is not required for correct operation.
203334*/
203335static int sessionGrowHash(int bPatchset, SessionTable *pTab){
203336 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
203337 int i;
203338 SessionChange **apNew;
203339 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
203340
203341 apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
203342 if( apNew==0 ){
203343 if( pTab->nChange==0 ){
203344 return SQLITE_ERROR;
203345 }
203346 return SQLITE_OK;
203347 }
203348 memset(apNew, 0, sizeof(SessionChange *) * nNew);
203349
203350 for(i=0; i<pTab->nChange; i++){
203351 SessionChange *p;
203352 SessionChange *pNext;
203353 for(p=pTab->apChange[i]; p; p=pNext){
203354 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
203355 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
203356 pNext = p->pNext;
203357 p->pNext = apNew[iHash];
203358 apNew[iHash] = p;
203359 }
203360 }
203361
203362 sqlite3_free(pTab->apChange);
203363 pTab->nChange = nNew;
203364 pTab->apChange = apNew;
203365 }
203366
203367 return SQLITE_OK;
203368}
203369
203370/*
203371** This function queries the database for the names of the columns of table
203372** zThis, in schema zDb.
203373**
203374** Otherwise, if they are not NULL, variable *pnCol is set to the number
203375** of columns in the database table and variable *pzTab is set to point to a
203376** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
203377** point to an array of pointers to column names. And *pabPK (again, if not
203378** NULL) is set to point to an array of booleans - true if the corresponding
203379** column is part of the primary key.
203380**
203381** For example, if the table is declared as:
203382**
203383** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
203384**
203385** Then the four output variables are populated as follows:
203386**
203387** *pnCol = 4
203388** *pzTab = "tbl1"
203389** *pazCol = {"w", "x", "y", "z"}
203390** *pabPK = {1, 0, 0, 1}
203391**
203392** All returned buffers are part of the same single allocation, which must
203393** be freed using sqlite3_free() by the caller
203394*/
203395static int sessionTableInfo(
203396 sqlite3 *db, /* Database connection */
203397 const char *zDb, /* Name of attached database (e.g. "main") */
203398 const char *zThis, /* Table name */
203399 int *pnCol, /* OUT: number of columns */
203400 const char **pzTab, /* OUT: Copy of zThis */
203401 const char ***pazCol, /* OUT: Array of column names for table */
203402 u8 **pabPK /* OUT: Array of booleans - true for PK col */
203403){
203404 char *zPragma;
203405 sqlite3_stmt *pStmt;
203406 int rc;
203407 sqlite3_int64 nByte;
203408 int nDbCol = 0;
203409 int nThis;
203410 int i;
203411 u8 *pAlloc = 0;
203412 char **azCol = 0;
203413 u8 *abPK = 0;
203414
203415 assert( pazCol && pabPK );
203416
203417 nThis = sqlite3Strlen30(zThis);
203418 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
203419 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
203420 if( rc==SQLITE_OK ){
203421 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
203422 zPragma = sqlite3_mprintf(
203423 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
203424 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
203425 "SELECT 2, 'stat', '', 0, '', 0"
203426 );
203427 }else if( rc==SQLITE_ERROR ){
203428 zPragma = sqlite3_mprintf("");
203429 }else{
203430 return rc;
203431 }
203432 }else{
203433 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
203434 }
203435 if( !zPragma ) return SQLITE_NOMEM;
203436
203437 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
203438 sqlite3_free(zPragma);
203439 if( rc!=SQLITE_OK ) return rc;
203440
203441 nByte = nThis + 1;
203442 while( SQLITE_ROW==sqlite3_step(pStmt) ){
203443 nByte += sqlite3_column_bytes(pStmt, 1);
203444 nDbCol++;
203445 }
203446 rc = sqlite3_reset(pStmt);
203447
203448 if( rc==SQLITE_OK ){
203449 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
203450 pAlloc = sqlite3_malloc64(nByte);
203451 if( pAlloc==0 ){
203452 rc = SQLITE_NOMEM;
203453 }
203454 }
203455 if( rc==SQLITE_OK ){
203456 azCol = (char **)pAlloc;
203457 pAlloc = (u8 *)&azCol[nDbCol];
203458 abPK = (u8 *)pAlloc;
203459 pAlloc = &abPK[nDbCol];
203460 if( pzTab ){
203461 memcpy(pAlloc, zThis, nThis+1);
203462 *pzTab = (char *)pAlloc;
203463 pAlloc += nThis+1;
203464 }
203465
203466 i = 0;
203467 while( SQLITE_ROW==sqlite3_step(pStmt) ){
203468 int nName = sqlite3_column_bytes(pStmt, 1);
203469 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
203470 if( zName==0 ) break;
203471 memcpy(pAlloc, zName, nName+1);
203472 azCol[i] = (char *)pAlloc;
203473 pAlloc += nName+1;
203474 abPK[i] = sqlite3_column_int(pStmt, 5);
203475 i++;
203476 }
203477 rc = sqlite3_reset(pStmt);
203478
203479 }
203480
203481 /* If successful, populate the output variables. Otherwise, zero them and
203482 ** free any allocation made. An error code will be returned in this case.
203483 */
203484 if( rc==SQLITE_OK ){
203485 *pazCol = (const char **)azCol;
203486 *pabPK = abPK;
203487 *pnCol = nDbCol;
203488 }else{
203489 *pazCol = 0;
203490 *pabPK = 0;
203491 *pnCol = 0;
203492 if( pzTab ) *pzTab = 0;
203493 sqlite3_free(azCol);
203494 }
203495 sqlite3_finalize(pStmt);
203496 return rc;
203497}
203498
203499/*
203500** This function is only called from within a pre-update handler for a
203501** write to table pTab, part of session pSession. If this is the first
203502** write to this table, initalize the SessionTable.nCol, azCol[] and
203503** abPK[] arrays accordingly.
203504**
203505** If an error occurs, an error code is stored in sqlite3_session.rc and
203506** non-zero returned. Or, if no error occurs but the table has no primary
203507** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
203508** indicate that updates on this table should be ignored. SessionTable.abPK
203509** is set to NULL in this case.
203510*/
203511static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
203512 if( pTab->nCol==0 ){
203513 u8 *abPK;
203514 assert( pTab->azCol==0 || pTab->abPK==0 );
203515 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
203516 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
203517 );
203518 if( pSession->rc==SQLITE_OK ){
203519 int i;
203520 for(i=0; i<pTab->nCol; i++){
203521 if( abPK[i] ){
203522 pTab->abPK = abPK;
203523 break;
203524 }
203525 }
203526 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
203527 pTab->bStat1 = 1;
203528 }
203529 }
203530 }
203531 return (pSession->rc || pTab->abPK==0);
203532}
203533
203534/*
203535** Versions of the four methods in object SessionHook for use with the
203536** sqlite_stat1 table. The purpose of this is to substitute a zero-length
203537** blob each time a NULL value is read from the "idx" column of the
203538** sqlite_stat1 table.
203539*/
203540typedef struct SessionStat1Ctx SessionStat1Ctx;
203541struct SessionStat1Ctx {
203542 SessionHook hook;
203543 sqlite3_session *pSession;
203544};
203545static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
203546 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
203547 sqlite3_value *pVal = 0;
203548 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
203549 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
203550 pVal = p->pSession->pZeroBlob;
203551 }
203552 *ppVal = pVal;
203553 return rc;
203554}
203555static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
203556 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
203557 sqlite3_value *pVal = 0;
203558 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
203559 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
203560 pVal = p->pSession->pZeroBlob;
203561 }
203562 *ppVal = pVal;
203563 return rc;
203564}
203565static int sessionStat1Count(void *pCtx){
203566 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
203567 return p->hook.xCount(p->hook.pCtx);
203568}
203569static int sessionStat1Depth(void *pCtx){
203570 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
203571 return p->hook.xDepth(p->hook.pCtx);
203572}
203573
203574
203575/*
203576** This function is only called from with a pre-update-hook reporting a
203577** change on table pTab (attached to session pSession). The type of change
203578** (UPDATE, INSERT, DELETE) is specified by the first argument.
203579**
203580** Unless one is already present or an error occurs, an entry is added
203581** to the changed-rows hash table associated with table pTab.
203582*/
203583static void sessionPreupdateOneChange(
203584 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
203585 sqlite3_session *pSession, /* Session object pTab is attached to */
203586 SessionTable *pTab /* Table that change applies to */
203587){
203588 int iHash;
203589 int bNull = 0;
203590 int rc = SQLITE_OK;
203591 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
203592
203593 if( pSession->rc ) return;
203594
203595 /* Load table details if required */
203596 if( sessionInitTable(pSession, pTab) ) return;
203597
203598 /* Check the number of columns in this xPreUpdate call matches the
203599 ** number of columns in the table. */
203600 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
203601 pSession->rc = SQLITE_SCHEMA;
203602 return;
203603 }
203604
203605 /* Grow the hash table if required */
203606 if( sessionGrowHash(0, pTab) ){
203607 pSession->rc = SQLITE_NOMEM;
203608 return;
203609 }
203610
203611 if( pTab->bStat1 ){
203612 stat1.hook = pSession->hook;
203613 stat1.pSession = pSession;
203614 pSession->hook.pCtx = (void*)&stat1;
203615 pSession->hook.xNew = sessionStat1New;
203616 pSession->hook.xOld = sessionStat1Old;
203617 pSession->hook.xCount = sessionStat1Count;
203618 pSession->hook.xDepth = sessionStat1Depth;
203619 if( pSession->pZeroBlob==0 ){
203620 sqlite3_value *p = sqlite3ValueNew(0);
203621 if( p==0 ){
203622 rc = SQLITE_NOMEM;
203623 goto error_out;
203624 }
203625 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
203626 pSession->pZeroBlob = p;
203627 }
203628 }
203629
203630 /* Calculate the hash-key for this change. If the primary key of the row
203631 ** includes a NULL value, exit early. Such changes are ignored by the
203632 ** session module. */
203633 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
203634 if( rc!=SQLITE_OK ) goto error_out;
203635
203636 if( bNull==0 ){
203637 /* Search the hash table for an existing record for this row. */
203638 SessionChange *pC;
203639 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
203640 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
203641 }
203642
203643 if( pC==0 ){
203644 /* Create a new change object containing all the old values (if
203645 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
203646 ** values (if this is an INSERT). */
203647 SessionChange *pChange; /* New change object */
203648 sqlite3_int64 nByte; /* Number of bytes to allocate */
203649 int i; /* Used to iterate through columns */
203650
203651 assert( rc==SQLITE_OK );
203652 pTab->nEntry++;
203653
203654 /* Figure out how large an allocation is required */
203655 nByte = sizeof(SessionChange);
203656 for(i=0; i<pTab->nCol; i++){
203657 sqlite3_value *p = 0;
203658 if( op!=SQLITE_INSERT ){
203659 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
203660 assert( trc==SQLITE_OK );
203661 }else if( pTab->abPK[i] ){
203662 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
203663 assert( trc==SQLITE_OK );
203664 }
203665
203666 /* This may fail if SQLite value p contains a utf-16 string that must
203667 ** be converted to utf-8 and an OOM error occurs while doing so. */
203668 rc = sessionSerializeValue(0, p, &nByte);
203669 if( rc!=SQLITE_OK ) goto error_out;
203670 }
203671
203672 /* Allocate the change object */
203673 pChange = (SessionChange *)sqlite3_malloc64(nByte);
203674 if( !pChange ){
203675 rc = SQLITE_NOMEM;
203676 goto error_out;
203677 }else{
203678 memset(pChange, 0, sizeof(SessionChange));
203679 pChange->aRecord = (u8 *)&pChange[1];
203680 }
203681
203682 /* Populate the change object. None of the preupdate_old(),
203683 ** preupdate_new() or SerializeValue() calls below may fail as all
203684 ** required values and encodings have already been cached in memory.
203685 ** It is not possible for an OOM to occur in this block. */
203686 nByte = 0;
203687 for(i=0; i<pTab->nCol; i++){
203688 sqlite3_value *p = 0;
203689 if( op!=SQLITE_INSERT ){
203690 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
203691 }else if( pTab->abPK[i] ){
203692 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
203693 }
203694 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
203695 }
203696
203697 /* Add the change to the hash-table */
203698 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
203699 pChange->bIndirect = 1;
203700 }
203701 pChange->nRecord = nByte;
203702 pChange->op = op;
203703 pChange->pNext = pTab->apChange[iHash];
203704 pTab->apChange[iHash] = pChange;
203705
203706 }else if( pC->bIndirect ){
203707 /* If the existing change is considered "indirect", but this current
203708 ** change is "direct", mark the change object as direct. */
203709 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
203710 && pSession->bIndirect==0
203711 ){
203712 pC->bIndirect = 0;
203713 }
203714 }
203715 }
203716
203717 /* If an error has occurred, mark the session object as failed. */
203718 error_out:
203719 if( pTab->bStat1 ){
203720 pSession->hook = stat1.hook;
203721 }
203722 if( rc!=SQLITE_OK ){
203723 pSession->rc = rc;
203724 }
203725}
203726
203727static int sessionFindTable(
203728 sqlite3_session *pSession,
203729 const char *zName,
203730 SessionTable **ppTab
203731){
203732 int rc = SQLITE_OK;
203733 int nName = sqlite3Strlen30(zName);
203734 SessionTable *pRet;
203735
203736 /* Search for an existing table */
203737 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
203738 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
203739 }
203740
203741 if( pRet==0 && pSession->bAutoAttach ){
203742 /* If there is a table-filter configured, invoke it. If it returns 0,
203743 ** do not automatically add the new table. */
203744 if( pSession->xTableFilter==0
203745 || pSession->xTableFilter(pSession->pFilterCtx, zName)
203746 ){
203747 rc = sqlite3session_attach(pSession, zName);
203748 if( rc==SQLITE_OK ){
203749 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
203750 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
203751 }
203752 }
203753 }
203754
203755 assert( rc==SQLITE_OK || pRet==0 );
203756 *ppTab = pRet;
203757 return rc;
203758}
203759
203760/*
203761** The 'pre-update' hook registered by this module with SQLite databases.
203762*/
203763static void xPreUpdate(
203764 void *pCtx, /* Copy of third arg to preupdate_hook() */
203765 sqlite3 *db, /* Database handle */
203766 int op, /* SQLITE_UPDATE, DELETE or INSERT */
203767 char const *zDb, /* Database name */
203768 char const *zName, /* Table name */
203769 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
203770 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
203771){
203772 sqlite3_session *pSession;
203773 int nDb = sqlite3Strlen30(zDb);
203774
203775 assert( sqlite3_mutex_held(db->mutex) );
203776
203777 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
203778 SessionTable *pTab;
203779
203780 /* If this session is attached to a different database ("main", "temp"
203781 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
203782 ** to the next session object attached to this database. */
203783 if( pSession->bEnable==0 ) continue;
203784 if( pSession->rc ) continue;
203785 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
203786
203787 pSession->rc = sessionFindTable(pSession, zName, &pTab);
203788 if( pTab ){
203789 assert( pSession->rc==SQLITE_OK );
203790 sessionPreupdateOneChange(op, pSession, pTab);
203791 if( op==SQLITE_UPDATE ){
203792 sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
203793 }
203794 }
203795 }
203796}
203797
203798/*
203799** The pre-update hook implementations.
203800*/
203801static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
203802 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
203803}
203804static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
203805 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
203806}
203807static int sessionPreupdateCount(void *pCtx){
203808 return sqlite3_preupdate_count((sqlite3*)pCtx);
203809}
203810static int sessionPreupdateDepth(void *pCtx){
203811 return sqlite3_preupdate_depth((sqlite3*)pCtx);
203812}
203813
203814/*
203815** Install the pre-update hooks on the session object passed as the only
203816** argument.
203817*/
203818static void sessionPreupdateHooks(
203819 sqlite3_session *pSession
203820){
203821 pSession->hook.pCtx = (void*)pSession->db;
203822 pSession->hook.xOld = sessionPreupdateOld;
203823 pSession->hook.xNew = sessionPreupdateNew;
203824 pSession->hook.xCount = sessionPreupdateCount;
203825 pSession->hook.xDepth = sessionPreupdateDepth;
203826}
203827
203828typedef struct SessionDiffCtx SessionDiffCtx;
203829struct SessionDiffCtx {
203830 sqlite3_stmt *pStmt;
203831 int nOldOff;
203832};
203833
203834/*
203835** The diff hook implementations.
203836*/
203837static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
203838 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203839 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
203840 return SQLITE_OK;
203841}
203842static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
203843 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203844 *ppVal = sqlite3_column_value(p->pStmt, iVal);
203845 return SQLITE_OK;
203846}
203847static int sessionDiffCount(void *pCtx){
203848 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
203849 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
203850}
203851static int sessionDiffDepth(void *pCtx){
203852 return 0;
203853}
203854
203855/*
203856** Install the diff hooks on the session object passed as the only
203857** argument.
203858*/
203859static void sessionDiffHooks(
203860 sqlite3_session *pSession,
203861 SessionDiffCtx *pDiffCtx
203862){
203863 pSession->hook.pCtx = (void*)pDiffCtx;
203864 pSession->hook.xOld = sessionDiffOld;
203865 pSession->hook.xNew = sessionDiffNew;
203866 pSession->hook.xCount = sessionDiffCount;
203867 pSession->hook.xDepth = sessionDiffDepth;
203868}
203869
203870static char *sessionExprComparePK(
203871 int nCol,
203872 const char *zDb1, const char *zDb2,
203873 const char *zTab,
203874 const char **azCol, u8 *abPK
203875){
203876 int i;
203877 const char *zSep = "";
203878 char *zRet = 0;
203879
203880 for(i=0; i<nCol; i++){
203881 if( abPK[i] ){
203882 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
203883 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
203884 );
203885 zSep = " AND ";
203886 if( zRet==0 ) break;
203887 }
203888 }
203889
203890 return zRet;
203891}
203892
203893static char *sessionExprCompareOther(
203894 int nCol,
203895 const char *zDb1, const char *zDb2,
203896 const char *zTab,
203897 const char **azCol, u8 *abPK
203898){
203899 int i;
203900 const char *zSep = "";
203901 char *zRet = 0;
203902 int bHave = 0;
203903
203904 for(i=0; i<nCol; i++){
203905 if( abPK[i]==0 ){
203906 bHave = 1;
203907 zRet = sqlite3_mprintf(
203908 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
203909 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
203910 );
203911 zSep = " OR ";
203912 if( zRet==0 ) break;
203913 }
203914 }
203915
203916 if( bHave==0 ){
203917 assert( zRet==0 );
203918 zRet = sqlite3_mprintf("0");
203919 }
203920
203921 return zRet;
203922}
203923
203924static char *sessionSelectFindNew(
203925 int nCol,
203926 const char *zDb1, /* Pick rows in this db only */
203927 const char *zDb2, /* But not in this one */
203928 const char *zTbl, /* Table name */
203929 const char *zExpr
203930){
203931 char *zRet = sqlite3_mprintf(
203932 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
203933 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
203934 ")",
203935 zDb1, zTbl, zDb2, zTbl, zExpr
203936 );
203937 return zRet;
203938}
203939
203940static int sessionDiffFindNew(
203941 int op,
203942 sqlite3_session *pSession,
203943 SessionTable *pTab,
203944 const char *zDb1,
203945 const char *zDb2,
203946 char *zExpr
203947){
203948 int rc = SQLITE_OK;
203949 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
203950
203951 if( zStmt==0 ){
203952 rc = SQLITE_NOMEM;
203953 }else{
203954 sqlite3_stmt *pStmt;
203955 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
203956 if( rc==SQLITE_OK ){
203957 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
203958 pDiffCtx->pStmt = pStmt;
203959 pDiffCtx->nOldOff = 0;
203960 while( SQLITE_ROW==sqlite3_step(pStmt) ){
203961 sessionPreupdateOneChange(op, pSession, pTab);
203962 }
203963 rc = sqlite3_finalize(pStmt);
203964 }
203965 sqlite3_free(zStmt);
203966 }
203967
203968 return rc;
203969}
203970
203971static int sessionDiffFindModified(
203972 sqlite3_session *pSession,
203973 SessionTable *pTab,
203974 const char *zFrom,
203975 const char *zExpr
203976){
203977 int rc = SQLITE_OK;
203978
203979 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
203980 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
203981 );
203982 if( zExpr2==0 ){
203983 rc = SQLITE_NOMEM;
203984 }else{
203985 char *zStmt = sqlite3_mprintf(
203986 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
203987 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
203988 );
203989 if( zStmt==0 ){
203990 rc = SQLITE_NOMEM;
203991 }else{
203992 sqlite3_stmt *pStmt;
203993 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
203994
203995 if( rc==SQLITE_OK ){
203996 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
203997 pDiffCtx->pStmt = pStmt;
203998 pDiffCtx->nOldOff = pTab->nCol;
203999 while( SQLITE_ROW==sqlite3_step(pStmt) ){
204000 sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
204001 }
204002 rc = sqlite3_finalize(pStmt);
204003 }
204004 sqlite3_free(zStmt);
204005 }
204006 }
204007
204008 return rc;
204009}
204010
204011SQLITE_API int sqlite3session_diff(
204012 sqlite3_session *pSession,
204013 const char *zFrom,
204014 const char *zTbl,
204015 char **pzErrMsg
204016){
204017 const char *zDb = pSession->zDb;
204018 int rc = pSession->rc;
204019 SessionDiffCtx d;
204020
204021 memset(&d, 0, sizeof(d));
204022 sessionDiffHooks(pSession, &d);
204023
204024 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204025 if( pzErrMsg ) *pzErrMsg = 0;
204026 if( rc==SQLITE_OK ){
204027 char *zExpr = 0;
204028 sqlite3 *db = pSession->db;
204029 SessionTable *pTo; /* Table zTbl */
204030
204031 /* Locate and if necessary initialize the target table object */
204032 rc = sessionFindTable(pSession, zTbl, &pTo);
204033 if( pTo==0 ) goto diff_out;
204034 if( sessionInitTable(pSession, pTo) ){
204035 rc = pSession->rc;
204036 goto diff_out;
204037 }
204038
204039 /* Check the table schemas match */
204040 if( rc==SQLITE_OK ){
204041 int bHasPk = 0;
204042 int bMismatch = 0;
204043 int nCol; /* Columns in zFrom.zTbl */
204044 u8 *abPK;
204045 const char **azCol = 0;
204046 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
204047 if( rc==SQLITE_OK ){
204048 if( pTo->nCol!=nCol ){
204049 bMismatch = 1;
204050 }else{
204051 int i;
204052 for(i=0; i<nCol; i++){
204053 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
204054 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
204055 if( abPK[i] ) bHasPk = 1;
204056 }
204057 }
204058 }
204059 sqlite3_free((char*)azCol);
204060 if( bMismatch ){
204061 if( pzErrMsg ){
204062 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
204063 }
204064 rc = SQLITE_SCHEMA;
204065 }
204066 if( bHasPk==0 ){
204067 /* Ignore tables with no primary keys */
204068 goto diff_out;
204069 }
204070 }
204071
204072 if( rc==SQLITE_OK ){
204073 zExpr = sessionExprComparePK(pTo->nCol,
204074 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
204075 );
204076 }
204077
204078 /* Find new rows */
204079 if( rc==SQLITE_OK ){
204080 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
204081 }
204082
204083 /* Find old rows */
204084 if( rc==SQLITE_OK ){
204085 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
204086 }
204087
204088 /* Find modified rows */
204089 if( rc==SQLITE_OK ){
204090 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
204091 }
204092
204093 sqlite3_free(zExpr);
204094 }
204095
204096 diff_out:
204097 sessionPreupdateHooks(pSession);
204098 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204099 return rc;
204100}
204101
204102/*
204103** Create a session object. This session object will record changes to
204104** database zDb attached to connection db.
204105*/
204106SQLITE_API int sqlite3session_create(
204107 sqlite3 *db, /* Database handle */
204108 const char *zDb, /* Name of db (e.g. "main") */
204109 sqlite3_session **ppSession /* OUT: New session object */
204110){
204111 sqlite3_session *pNew; /* Newly allocated session object */
204112 sqlite3_session *pOld; /* Session object already attached to db */
204113 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
204114
204115 /* Zero the output value in case an error occurs. */
204116 *ppSession = 0;
204117
204118 /* Allocate and populate the new session object. */
204119 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
204120 if( !pNew ) return SQLITE_NOMEM;
204121 memset(pNew, 0, sizeof(sqlite3_session));
204122 pNew->db = db;
204123 pNew->zDb = (char *)&pNew[1];
204124 pNew->bEnable = 1;
204125 memcpy(pNew->zDb, zDb, nDb+1);
204126 sessionPreupdateHooks(pNew);
204127
204128 /* Add the new session object to the linked list of session objects
204129 ** attached to database handle $db. Do this under the cover of the db
204130 ** handle mutex. */
204131 sqlite3_mutex_enter(sqlite3_db_mutex(db));
204132 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
204133 pNew->pNext = pOld;
204134 sqlite3_mutex_leave(sqlite3_db_mutex(db));
204135
204136 *ppSession = pNew;
204137 return SQLITE_OK;
204138}
204139
204140/*
204141** Free the list of table objects passed as the first argument. The contents
204142** of the changed-rows hash tables are also deleted.
204143*/
204144static void sessionDeleteTable(SessionTable *pList){
204145 SessionTable *pNext;
204146 SessionTable *pTab;
204147
204148 for(pTab=pList; pTab; pTab=pNext){
204149 int i;
204150 pNext = pTab->pNext;
204151 for(i=0; i<pTab->nChange; i++){
204152 SessionChange *p;
204153 SessionChange *pNextChange;
204154 for(p=pTab->apChange[i]; p; p=pNextChange){
204155 pNextChange = p->pNext;
204156 sqlite3_free(p);
204157 }
204158 }
204159 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
204160 sqlite3_free(pTab->apChange);
204161 sqlite3_free(pTab);
204162 }
204163}
204164
204165/*
204166** Delete a session object previously allocated using sqlite3session_create().
204167*/
204168SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
204169 sqlite3 *db = pSession->db;
204170 sqlite3_session *pHead;
204171 sqlite3_session **pp;
204172
204173 /* Unlink the session from the linked list of sessions attached to the
204174 ** database handle. Hold the db mutex while doing so. */
204175 sqlite3_mutex_enter(sqlite3_db_mutex(db));
204176 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
204177 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
204178 if( (*pp)==pSession ){
204179 *pp = (*pp)->pNext;
204180 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
204181 break;
204182 }
204183 }
204184 sqlite3_mutex_leave(sqlite3_db_mutex(db));
204185 sqlite3ValueFree(pSession->pZeroBlob);
204186
204187 /* Delete all attached table objects. And the contents of their
204188 ** associated hash-tables. */
204189 sessionDeleteTable(pSession->pTable);
204190
204191 /* Free the session object itself. */
204192 sqlite3_free(pSession);
204193}
204194
204195/*
204196** Set a table filter on a Session Object.
204197*/
204198SQLITE_API void sqlite3session_table_filter(
204199 sqlite3_session *pSession,
204200 int(*xFilter)(void*, const char*),
204201 void *pCtx /* First argument passed to xFilter */
204202){
204203 pSession->bAutoAttach = 1;
204204 pSession->pFilterCtx = pCtx;
204205 pSession->xTableFilter = xFilter;
204206}
204207
204208/*
204209** Attach a table to a session. All subsequent changes made to the table
204210** while the session object is enabled will be recorded.
204211**
204212** Only tables that have a PRIMARY KEY defined may be attached. It does
204213** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
204214** or not.
204215*/
204216SQLITE_API int sqlite3session_attach(
204217 sqlite3_session *pSession, /* Session object */
204218 const char *zName /* Table name */
204219){
204220 int rc = SQLITE_OK;
204221 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204222
204223 if( !zName ){
204224 pSession->bAutoAttach = 1;
204225 }else{
204226 SessionTable *pTab; /* New table object (if required) */
204227 int nName; /* Number of bytes in string zName */
204228
204229 /* First search for an existing entry. If one is found, this call is
204230 ** a no-op. Return early. */
204231 nName = sqlite3Strlen30(zName);
204232 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
204233 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
204234 }
204235
204236 if( !pTab ){
204237 /* Allocate new SessionTable object. */
204238 pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
204239 if( !pTab ){
204240 rc = SQLITE_NOMEM;
204241 }else{
204242 /* Populate the new SessionTable object and link it into the list.
204243 ** The new object must be linked onto the end of the list, not
204244 ** simply added to the start of it in order to ensure that tables
204245 ** appear in the correct order when a changeset or patchset is
204246 ** eventually generated. */
204247 SessionTable **ppTab;
204248 memset(pTab, 0, sizeof(SessionTable));
204249 pTab->zName = (char *)&pTab[1];
204250 memcpy(pTab->zName, zName, nName+1);
204251 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
204252 *ppTab = pTab;
204253 }
204254 }
204255 }
204256
204257 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204258 return rc;
204259}
204260
204261/*
204262** Ensure that there is room in the buffer to append nByte bytes of data.
204263** If not, use sqlite3_realloc() to grow the buffer so that there is.
204264**
204265** If successful, return zero. Otherwise, if an OOM condition is encountered,
204266** set *pRc to SQLITE_NOMEM and return non-zero.
204267*/
204268static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
204269 if( *pRc==SQLITE_OK && (size_t)(p->nAlloc-p->nBuf)<nByte ){
204270 u8 *aNew;
204271 i64 nNew = p->nAlloc ? p->nAlloc : 128;
204272 do {
204273 nNew = nNew*2;
204274 }while( (size_t)(nNew-p->nBuf)<nByte );
204275
204276 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
204277 if( 0==aNew ){
204278 *pRc = SQLITE_NOMEM;
204279 }else{
204280 p->aBuf = aNew;
204281 p->nAlloc = nNew;
204282 }
204283 }
204284 return (*pRc!=SQLITE_OK);
204285}
204286
204287/*
204288** Append the value passed as the second argument to the buffer passed
204289** as the first.
204290**
204291** This function is a no-op if *pRc is non-zero when it is called.
204292** Otherwise, if an error occurs, *pRc is set to an SQLite error code
204293** before returning.
204294*/
204295static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
204296 int rc = *pRc;
204297 if( rc==SQLITE_OK ){
204298 sqlite3_int64 nByte = 0;
204299 rc = sessionSerializeValue(0, pVal, &nByte);
204300 sessionBufferGrow(p, nByte, &rc);
204301 if( rc==SQLITE_OK ){
204302 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
204303 p->nBuf += nByte;
204304 }else{
204305 *pRc = rc;
204306 }
204307 }
204308}
204309
204310/*
204311** This function is a no-op if *pRc is other than SQLITE_OK when it is
204312** called. Otherwise, append a single byte to the buffer.
204313**
204314** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204315** returning.
204316*/
204317static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
204318 if( 0==sessionBufferGrow(p, 1, pRc) ){
204319 p->aBuf[p->nBuf++] = v;
204320 }
204321}
204322
204323/*
204324** This function is a no-op if *pRc is other than SQLITE_OK when it is
204325** called. Otherwise, append a single varint to the buffer.
204326**
204327** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204328** returning.
204329*/
204330static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
204331 if( 0==sessionBufferGrow(p, 9, pRc) ){
204332 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
204333 }
204334}
204335
204336/*
204337** This function is a no-op if *pRc is other than SQLITE_OK when it is
204338** called. Otherwise, append a blob of data to the buffer.
204339**
204340** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204341** returning.
204342*/
204343static void sessionAppendBlob(
204344 SessionBuffer *p,
204345 const u8 *aBlob,
204346 int nBlob,
204347 int *pRc
204348){
204349 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
204350 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
204351 p->nBuf += nBlob;
204352 }
204353}
204354
204355/*
204356** This function is a no-op if *pRc is other than SQLITE_OK when it is
204357** called. Otherwise, append a string to the buffer. All bytes in the string
204358** up to (but not including) the nul-terminator are written to the buffer.
204359**
204360** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204361** returning.
204362*/
204363static void sessionAppendStr(
204364 SessionBuffer *p,
204365 const char *zStr,
204366 int *pRc
204367){
204368 int nStr = sqlite3Strlen30(zStr);
204369 if( 0==sessionBufferGrow(p, nStr, pRc) ){
204370 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
204371 p->nBuf += nStr;
204372 }
204373}
204374
204375/*
204376** This function is a no-op if *pRc is other than SQLITE_OK when it is
204377** called. Otherwise, append the string representation of integer iVal
204378** to the buffer. No nul-terminator is written.
204379**
204380** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204381** returning.
204382*/
204383static void sessionAppendInteger(
204384 SessionBuffer *p, /* Buffer to append to */
204385 int iVal, /* Value to write the string rep. of */
204386 int *pRc /* IN/OUT: Error code */
204387){
204388 char aBuf[24];
204389 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
204390 sessionAppendStr(p, aBuf, pRc);
204391}
204392
204393/*
204394** This function is a no-op if *pRc is other than SQLITE_OK when it is
204395** called. Otherwise, append the string zStr enclosed in quotes (") and
204396** with any embedded quote characters escaped to the buffer. No
204397** nul-terminator byte is written.
204398**
204399** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
204400** returning.
204401*/
204402static void sessionAppendIdent(
204403 SessionBuffer *p, /* Buffer to a append to */
204404 const char *zStr, /* String to quote, escape and append */
204405 int *pRc /* IN/OUT: Error code */
204406){
204407 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
204408 if( 0==sessionBufferGrow(p, nStr, pRc) ){
204409 char *zOut = (char *)&p->aBuf[p->nBuf];
204410 const char *zIn = zStr;
204411 *zOut++ = '"';
204412 while( *zIn ){
204413 if( *zIn=='"' ) *zOut++ = '"';
204414 *zOut++ = *(zIn++);
204415 }
204416 *zOut++ = '"';
204417 p->nBuf = (int)((u8 *)zOut - p->aBuf);
204418 }
204419}
204420
204421/*
204422** This function is a no-op if *pRc is other than SQLITE_OK when it is
204423** called. Otherwse, it appends the serialized version of the value stored
204424** in column iCol of the row that SQL statement pStmt currently points
204425** to to the buffer.
204426*/
204427static void sessionAppendCol(
204428 SessionBuffer *p, /* Buffer to append to */
204429 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
204430 int iCol, /* Column to read value from */
204431 int *pRc /* IN/OUT: Error code */
204432){
204433 if( *pRc==SQLITE_OK ){
204434 int eType = sqlite3_column_type(pStmt, iCol);
204435 sessionAppendByte(p, (u8)eType, pRc);
204436 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
204437 sqlite3_int64 i;
204438 u8 aBuf[8];
204439 if( eType==SQLITE_INTEGER ){
204440 i = sqlite3_column_int64(pStmt, iCol);
204441 }else{
204442 double r = sqlite3_column_double(pStmt, iCol);
204443 memcpy(&i, &r, 8);
204444 }
204445 sessionPutI64(aBuf, i);
204446 sessionAppendBlob(p, aBuf, 8, pRc);
204447 }
204448 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
204449 u8 *z;
204450 int nByte;
204451 if( eType==SQLITE_BLOB ){
204452 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
204453 }else{
204454 z = (u8 *)sqlite3_column_text(pStmt, iCol);
204455 }
204456 nByte = sqlite3_column_bytes(pStmt, iCol);
204457 if( z || (eType==SQLITE_BLOB && nByte==0) ){
204458 sessionAppendVarint(p, nByte, pRc);
204459 sessionAppendBlob(p, z, nByte, pRc);
204460 }else{
204461 *pRc = SQLITE_NOMEM;
204462 }
204463 }
204464 }
204465}
204466
204467/*
204468**
204469** This function appends an update change to the buffer (see the comments
204470** under "CHANGESET FORMAT" at the top of the file). An update change
204471** consists of:
204472**
204473** 1 byte: SQLITE_UPDATE (0x17)
204474** n bytes: old.* record (see RECORD FORMAT)
204475** m bytes: new.* record (see RECORD FORMAT)
204476**
204477** The SessionChange object passed as the third argument contains the
204478** values that were stored in the row when the session began (the old.*
204479** values). The statement handle passed as the second argument points
204480** at the current version of the row (the new.* values).
204481**
204482** If all of the old.* values are equal to their corresponding new.* value
204483** (i.e. nothing has changed), then no data at all is appended to the buffer.
204484**
204485** Otherwise, the old.* record contains all primary key values and the
204486** original values of any fields that have been modified. The new.* record
204487** contains the new values of only those fields that have been modified.
204488*/
204489static int sessionAppendUpdate(
204490 SessionBuffer *pBuf, /* Buffer to append to */
204491 int bPatchset, /* True for "patchset", 0 for "changeset" */
204492 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
204493 SessionChange *p, /* Object containing old values */
204494 u8 *abPK /* Boolean array - true for PK columns */
204495){
204496 int rc = SQLITE_OK;
204497 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
204498 int bNoop = 1; /* Set to zero if any values are modified */
204499 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
204500 int i; /* Used to iterate through columns */
204501 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
204502
204503 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
204504 sessionAppendByte(pBuf, p->bIndirect, &rc);
204505 for(i=0; i<sqlite3_column_count(pStmt); i++){
204506 int bChanged = 0;
204507 int nAdvance;
204508 int eType = *pCsr;
204509 switch( eType ){
204510 case SQLITE_NULL:
204511 nAdvance = 1;
204512 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
204513 bChanged = 1;
204514 }
204515 break;
204516
204517 case SQLITE_FLOAT:
204518 case SQLITE_INTEGER: {
204519 nAdvance = 9;
204520 if( eType==sqlite3_column_type(pStmt, i) ){
204521 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
204522 if( eType==SQLITE_INTEGER ){
204523 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
204524 }else{
204525 double dVal;
204526 memcpy(&dVal, &iVal, 8);
204527 if( dVal==sqlite3_column_double(pStmt, i) ) break;
204528 }
204529 }
204530 bChanged = 1;
204531 break;
204532 }
204533
204534 default: {
204535 int n;
204536 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
204537 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
204538 nAdvance = nHdr + n;
204539 if( eType==sqlite3_column_type(pStmt, i)
204540 && n==sqlite3_column_bytes(pStmt, i)
204541 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
204542 ){
204543 break;
204544 }
204545 bChanged = 1;
204546 }
204547 }
204548
204549 /* If at least one field has been modified, this is not a no-op. */
204550 if( bChanged ) bNoop = 0;
204551
204552 /* Add a field to the old.* record. This is omitted if this modules is
204553 ** currently generating a patchset. */
204554 if( bPatchset==0 ){
204555 if( bChanged || abPK[i] ){
204556 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
204557 }else{
204558 sessionAppendByte(pBuf, 0, &rc);
204559 }
204560 }
204561
204562 /* Add a field to the new.* record. Or the only record if currently
204563 ** generating a patchset. */
204564 if( bChanged || (bPatchset && abPK[i]) ){
204565 sessionAppendCol(&buf2, pStmt, i, &rc);
204566 }else{
204567 sessionAppendByte(&buf2, 0, &rc);
204568 }
204569
204570 pCsr += nAdvance;
204571 }
204572
204573 if( bNoop ){
204574 pBuf->nBuf = nRewind;
204575 }else{
204576 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
204577 }
204578 sqlite3_free(buf2.aBuf);
204579
204580 return rc;
204581}
204582
204583/*
204584** Append a DELETE change to the buffer passed as the first argument. Use
204585** the changeset format if argument bPatchset is zero, or the patchset
204586** format otherwise.
204587*/
204588static int sessionAppendDelete(
204589 SessionBuffer *pBuf, /* Buffer to append to */
204590 int bPatchset, /* True for "patchset", 0 for "changeset" */
204591 SessionChange *p, /* Object containing old values */
204592 int nCol, /* Number of columns in table */
204593 u8 *abPK /* Boolean array - true for PK columns */
204594){
204595 int rc = SQLITE_OK;
204596
204597 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
204598 sessionAppendByte(pBuf, p->bIndirect, &rc);
204599
204600 if( bPatchset==0 ){
204601 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
204602 }else{
204603 int i;
204604 u8 *a = p->aRecord;
204605 for(i=0; i<nCol; i++){
204606 u8 *pStart = a;
204607 int eType = *a++;
204608
204609 switch( eType ){
204610 case 0:
204611 case SQLITE_NULL:
204612 assert( abPK[i]==0 );
204613 break;
204614
204615 case SQLITE_FLOAT:
204616 case SQLITE_INTEGER:
204617 a += 8;
204618 break;
204619
204620 default: {
204621 int n;
204622 a += sessionVarintGet(a, &n);
204623 a += n;
204624 break;
204625 }
204626 }
204627 if( abPK[i] ){
204628 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
204629 }
204630 }
204631 assert( (a - p->aRecord)==p->nRecord );
204632 }
204633
204634 return rc;
204635}
204636
204637/*
204638** Formulate and prepare a SELECT statement to retrieve a row from table
204639** zTab in database zDb based on its primary key. i.e.
204640**
204641** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
204642*/
204643static int sessionSelectStmt(
204644 sqlite3 *db, /* Database handle */
204645 const char *zDb, /* Database name */
204646 const char *zTab, /* Table name */
204647 int nCol, /* Number of columns in table */
204648 const char **azCol, /* Names of table columns */
204649 u8 *abPK, /* PRIMARY KEY array */
204650 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
204651){
204652 int rc = SQLITE_OK;
204653 char *zSql = 0;
204654 int nSql = -1;
204655
204656 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
204657 zSql = sqlite3_mprintf(
204658 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
204659 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
204660 );
204661 if( zSql==0 ) rc = SQLITE_NOMEM;
204662 }else{
204663 int i;
204664 const char *zSep = "";
204665 SessionBuffer buf = {0, 0, 0};
204666
204667 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
204668 sessionAppendIdent(&buf, zDb, &rc);
204669 sessionAppendStr(&buf, ".", &rc);
204670 sessionAppendIdent(&buf, zTab, &rc);
204671 sessionAppendStr(&buf, " WHERE ", &rc);
204672 for(i=0; i<nCol; i++){
204673 if( abPK[i] ){
204674 sessionAppendStr(&buf, zSep, &rc);
204675 sessionAppendIdent(&buf, azCol[i], &rc);
204676 sessionAppendStr(&buf, " IS ?", &rc);
204677 sessionAppendInteger(&buf, i+1, &rc);
204678 zSep = " AND ";
204679 }
204680 }
204681 zSql = (char*)buf.aBuf;
204682 nSql = buf.nBuf;
204683 }
204684
204685 if( rc==SQLITE_OK ){
204686 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
204687 }
204688 sqlite3_free(zSql);
204689 return rc;
204690}
204691
204692/*
204693** Bind the PRIMARY KEY values from the change passed in argument pChange
204694** to the SELECT statement passed as the first argument. The SELECT statement
204695** is as prepared by function sessionSelectStmt().
204696**
204697** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
204698** error code (e.g. SQLITE_NOMEM) otherwise.
204699*/
204700static int sessionSelectBind(
204701 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
204702 int nCol, /* Number of columns in table */
204703 u8 *abPK, /* PRIMARY KEY array */
204704 SessionChange *pChange /* Change structure */
204705){
204706 int i;
204707 int rc = SQLITE_OK;
204708 u8 *a = pChange->aRecord;
204709
204710 for(i=0; i<nCol && rc==SQLITE_OK; i++){
204711 int eType = *a++;
204712
204713 switch( eType ){
204714 case 0:
204715 case SQLITE_NULL:
204716 assert( abPK[i]==0 );
204717 break;
204718
204719 case SQLITE_INTEGER: {
204720 if( abPK[i] ){
204721 i64 iVal = sessionGetI64(a);
204722 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
204723 }
204724 a += 8;
204725 break;
204726 }
204727
204728 case SQLITE_FLOAT: {
204729 if( abPK[i] ){
204730 double rVal;
204731 i64 iVal = sessionGetI64(a);
204732 memcpy(&rVal, &iVal, 8);
204733 rc = sqlite3_bind_double(pSelect, i+1, rVal);
204734 }
204735 a += 8;
204736 break;
204737 }
204738
204739 case SQLITE_TEXT: {
204740 int n;
204741 a += sessionVarintGet(a, &n);
204742 if( abPK[i] ){
204743 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
204744 }
204745 a += n;
204746 break;
204747 }
204748
204749 default: {
204750 int n;
204751 assert( eType==SQLITE_BLOB );
204752 a += sessionVarintGet(a, &n);
204753 if( abPK[i] ){
204754 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
204755 }
204756 a += n;
204757 break;
204758 }
204759 }
204760 }
204761
204762 return rc;
204763}
204764
204765/*
204766** This function is a no-op if *pRc is set to other than SQLITE_OK when it
204767** is called. Otherwise, append a serialized table header (part of the binary
204768** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
204769** SQLite error code before returning.
204770*/
204771static void sessionAppendTableHdr(
204772 SessionBuffer *pBuf, /* Append header to this buffer */
204773 int bPatchset, /* Use the patchset format if true */
204774 SessionTable *pTab, /* Table object to append header for */
204775 int *pRc /* IN/OUT: Error code */
204776){
204777 /* Write a table header */
204778 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
204779 sessionAppendVarint(pBuf, pTab->nCol, pRc);
204780 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
204781 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
204782}
204783
204784/*
204785** Generate either a changeset (if argument bPatchset is zero) or a patchset
204786** (if it is non-zero) based on the current contents of the session object
204787** passed as the first argument.
204788**
204789** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
204790** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
204791** occurs, an SQLite error code is returned and both output variables set
204792** to 0.
204793*/
204794static int sessionGenerateChangeset(
204795 sqlite3_session *pSession, /* Session object */
204796 int bPatchset, /* True for patchset, false for changeset */
204797 int (*xOutput)(void *pOut, const void *pData, int nData),
204798 void *pOut, /* First argument for xOutput */
204799 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
204800 void **ppChangeset /* OUT: Buffer containing changeset */
204801){
204802 sqlite3 *db = pSession->db; /* Source database handle */
204803 SessionTable *pTab; /* Used to iterate through attached tables */
204804 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
204805 int rc; /* Return code */
204806
204807 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
204808
204809 /* Zero the output variables in case an error occurs. If this session
204810 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
204811 ** this call will be a no-op. */
204812 if( xOutput==0 ){
204813 *pnChangeset = 0;
204814 *ppChangeset = 0;
204815 }
204816
204817 if( pSession->rc ) return pSession->rc;
204818 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
204819 if( rc!=SQLITE_OK ) return rc;
204820
204821 sqlite3_mutex_enter(sqlite3_db_mutex(db));
204822
204823 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
204824 if( pTab->nEntry ){
204825 const char *zName = pTab->zName;
204826 int nCol; /* Number of columns in table */
204827 u8 *abPK; /* Primary key array */
204828 const char **azCol = 0; /* Table columns */
204829 int i; /* Used to iterate through hash buckets */
204830 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
204831 int nRewind = buf.nBuf; /* Initial size of write buffer */
204832 int nNoop; /* Size of buffer after writing tbl header */
204833
204834 /* Check the table schema is still Ok. */
204835 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
204836 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
204837 rc = SQLITE_SCHEMA;
204838 }
204839
204840 /* Write a table header */
204841 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
204842
204843 /* Build and compile a statement to execute: */
204844 if( rc==SQLITE_OK ){
204845 rc = sessionSelectStmt(
204846 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
204847 }
204848
204849 nNoop = buf.nBuf;
204850 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
204851 SessionChange *p; /* Used to iterate through changes */
204852
204853 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
204854 rc = sessionSelectBind(pSel, nCol, abPK, p);
204855 if( rc!=SQLITE_OK ) continue;
204856 if( sqlite3_step(pSel)==SQLITE_ROW ){
204857 if( p->op==SQLITE_INSERT ){
204858 int iCol;
204859 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
204860 sessionAppendByte(&buf, p->bIndirect, &rc);
204861 for(iCol=0; iCol<nCol; iCol++){
204862 sessionAppendCol(&buf, pSel, iCol, &rc);
204863 }
204864 }else{
204865 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
204866 }
204867 }else if( p->op!=SQLITE_INSERT ){
204868 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
204869 }
204870 if( rc==SQLITE_OK ){
204871 rc = sqlite3_reset(pSel);
204872 }
204873
204874 /* If the buffer is now larger than sessions_strm_chunk_size, pass
204875 ** its contents to the xOutput() callback. */
204876 if( xOutput
204877 && rc==SQLITE_OK
204878 && buf.nBuf>nNoop
204879 && buf.nBuf>sessions_strm_chunk_size
204880 ){
204881 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
204882 nNoop = -1;
204883 buf.nBuf = 0;
204884 }
204885
204886 }
204887 }
204888
204889 sqlite3_finalize(pSel);
204890 if( buf.nBuf==nNoop ){
204891 buf.nBuf = nRewind;
204892 }
204893 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
204894 }
204895 }
204896
204897 if( rc==SQLITE_OK ){
204898 if( xOutput==0 ){
204899 *pnChangeset = buf.nBuf;
204900 *ppChangeset = buf.aBuf;
204901 buf.aBuf = 0;
204902 }else if( buf.nBuf>0 ){
204903 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
204904 }
204905 }
204906
204907 sqlite3_free(buf.aBuf);
204908 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
204909 sqlite3_mutex_leave(sqlite3_db_mutex(db));
204910 return rc;
204911}
204912
204913/*
204914** Obtain a changeset object containing all changes recorded by the
204915** session object passed as the first argument.
204916**
204917** It is the responsibility of the caller to eventually free the buffer
204918** using sqlite3_free().
204919*/
204920SQLITE_API int sqlite3session_changeset(
204921 sqlite3_session *pSession, /* Session object */
204922 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
204923 void **ppChangeset /* OUT: Buffer containing changeset */
204924){
204925 return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
204926}
204927
204928/*
204929** Streaming version of sqlite3session_changeset().
204930*/
204931SQLITE_API int sqlite3session_changeset_strm(
204932 sqlite3_session *pSession,
204933 int (*xOutput)(void *pOut, const void *pData, int nData),
204934 void *pOut
204935){
204936 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
204937}
204938
204939/*
204940** Streaming version of sqlite3session_patchset().
204941*/
204942SQLITE_API int sqlite3session_patchset_strm(
204943 sqlite3_session *pSession,
204944 int (*xOutput)(void *pOut, const void *pData, int nData),
204945 void *pOut
204946){
204947 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
204948}
204949
204950/*
204951** Obtain a patchset object containing all changes recorded by the
204952** session object passed as the first argument.
204953**
204954** It is the responsibility of the caller to eventually free the buffer
204955** using sqlite3_free().
204956*/
204957SQLITE_API int sqlite3session_patchset(
204958 sqlite3_session *pSession, /* Session object */
204959 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
204960 void **ppPatchset /* OUT: Buffer containing changeset */
204961){
204962 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
204963}
204964
204965/*
204966** Enable or disable the session object passed as the first argument.
204967*/
204968SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
204969 int ret;
204970 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204971 if( bEnable>=0 ){
204972 pSession->bEnable = bEnable;
204973 }
204974 ret = pSession->bEnable;
204975 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204976 return ret;
204977}
204978
204979/*
204980** Enable or disable the session object passed as the first argument.
204981*/
204982SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
204983 int ret;
204984 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
204985 if( bIndirect>=0 ){
204986 pSession->bIndirect = bIndirect;
204987 }
204988 ret = pSession->bIndirect;
204989 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
204990 return ret;
204991}
204992
204993/*
204994** Return true if there have been no changes to monitored tables recorded
204995** by the session object passed as the only argument.
204996*/
204997SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
204998 int ret = 0;
204999 SessionTable *pTab;
205000
205001 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
205002 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
205003 ret = (pTab->nEntry>0);
205004 }
205005 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
205006
205007 return (ret==0);
205008}
205009
205010/*
205011** Do the work for either sqlite3changeset_start() or start_strm().
205012*/
205013static int sessionChangesetStart(
205014 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
205015 int (*xInput)(void *pIn, void *pData, int *pnData),
205016 void *pIn,
205017 int nChangeset, /* Size of buffer pChangeset in bytes */
205018 void *pChangeset, /* Pointer to buffer containing changeset */
205019 int bInvert /* True to invert changeset */
205020){
205021 sqlite3_changeset_iter *pRet; /* Iterator to return */
205022 int nByte; /* Number of bytes to allocate for iterator */
205023
205024 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
205025
205026 /* Zero the output variable in case an error occurs. */
205027 *pp = 0;
205028
205029 /* Allocate and initialize the iterator structure. */
205030 nByte = sizeof(sqlite3_changeset_iter);
205031 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
205032 if( !pRet ) return SQLITE_NOMEM;
205033 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
205034 pRet->in.aData = (u8 *)pChangeset;
205035 pRet->in.nData = nChangeset;
205036 pRet->in.xInput = xInput;
205037 pRet->in.pIn = pIn;
205038 pRet->in.bEof = (xInput ? 0 : 1);
205039 pRet->bInvert = bInvert;
205040
205041 /* Populate the output variable and return success. */
205042 *pp = pRet;
205043 return SQLITE_OK;
205044}
205045
205046/*
205047** Create an iterator used to iterate through the contents of a changeset.
205048*/
205049SQLITE_API int sqlite3changeset_start(
205050 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
205051 int nChangeset, /* Size of buffer pChangeset in bytes */
205052 void *pChangeset /* Pointer to buffer containing changeset */
205053){
205054 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
205055}
205056SQLITE_API int sqlite3changeset_start_v2(
205057 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
205058 int nChangeset, /* Size of buffer pChangeset in bytes */
205059 void *pChangeset, /* Pointer to buffer containing changeset */
205060 int flags
205061){
205062 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
205063 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
205064}
205065
205066/*
205067** Streaming version of sqlite3changeset_start().
205068*/
205069SQLITE_API int sqlite3changeset_start_strm(
205070 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
205071 int (*xInput)(void *pIn, void *pData, int *pnData),
205072 void *pIn
205073){
205074 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
205075}
205076SQLITE_API int sqlite3changeset_start_v2_strm(
205077 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
205078 int (*xInput)(void *pIn, void *pData, int *pnData),
205079 void *pIn,
205080 int flags
205081){
205082 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
205083 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
205084}
205085
205086/*
205087** If the SessionInput object passed as the only argument is a streaming
205088** object and the buffer is full, discard some data to free up space.
205089*/
205090static void sessionDiscardData(SessionInput *pIn){
205091 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
205092 int nMove = pIn->buf.nBuf - pIn->iNext;
205093 assert( nMove>=0 );
205094 if( nMove>0 ){
205095 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
205096 }
205097 pIn->buf.nBuf -= pIn->iNext;
205098 pIn->iNext = 0;
205099 pIn->nData = pIn->buf.nBuf;
205100 }
205101}
205102
205103/*
205104** Ensure that there are at least nByte bytes available in the buffer. Or,
205105** if there are not nByte bytes remaining in the input, that all available
205106** data is in the buffer.
205107**
205108** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
205109*/
205110static int sessionInputBuffer(SessionInput *pIn, int nByte){
205111 int rc = SQLITE_OK;
205112 if( pIn->xInput ){
205113 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
205114 int nNew = sessions_strm_chunk_size;
205115
205116 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
205117 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
205118 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
205119 if( nNew==0 ){
205120 pIn->bEof = 1;
205121 }else{
205122 pIn->buf.nBuf += nNew;
205123 }
205124 }
205125
205126 pIn->aData = pIn->buf.aBuf;
205127 pIn->nData = pIn->buf.nBuf;
205128 }
205129 }
205130 return rc;
205131}
205132
205133/*
205134** When this function is called, *ppRec points to the start of a record
205135** that contains nCol values. This function advances the pointer *ppRec
205136** until it points to the byte immediately following that record.
205137*/
205138static void sessionSkipRecord(
205139 u8 **ppRec, /* IN/OUT: Record pointer */
205140 int nCol /* Number of values in record */
205141){
205142 u8 *aRec = *ppRec;
205143 int i;
205144 for(i=0; i<nCol; i++){
205145 int eType = *aRec++;
205146 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
205147 int nByte;
205148 aRec += sessionVarintGet((u8*)aRec, &nByte);
205149 aRec += nByte;
205150 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
205151 aRec += 8;
205152 }
205153 }
205154
205155 *ppRec = aRec;
205156}
205157
205158/*
205159** This function sets the value of the sqlite3_value object passed as the
205160** first argument to a copy of the string or blob held in the aData[]
205161** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
205162** error occurs.
205163*/
205164static int sessionValueSetStr(
205165 sqlite3_value *pVal, /* Set the value of this object */
205166 u8 *aData, /* Buffer containing string or blob data */
205167 int nData, /* Size of buffer aData[] in bytes */
205168 u8 enc /* String encoding (0 for blobs) */
205169){
205170 /* In theory this code could just pass SQLITE_TRANSIENT as the final
205171 ** argument to sqlite3ValueSetStr() and have the copy created
205172 ** automatically. But doing so makes it difficult to detect any OOM
205173 ** error. Hence the code to create the copy externally. */
205174 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
205175 if( aCopy==0 ) return SQLITE_NOMEM;
205176 memcpy(aCopy, aData, nData);
205177 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
205178 return SQLITE_OK;
205179}
205180
205181/*
205182** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
205183** for details.
205184**
205185** When this function is called, *paChange points to the start of the record
205186** to deserialize. Assuming no error occurs, *paChange is set to point to
205187** one byte after the end of the same record before this function returns.
205188** If the argument abPK is NULL, then the record contains nCol values. Or,
205189** if abPK is other than NULL, then the record contains only the PK fields
205190** (in other words, it is a patchset DELETE record).
205191**
205192** If successful, each element of the apOut[] array (allocated by the caller)
205193** is set to point to an sqlite3_value object containing the value read
205194** from the corresponding position in the record. If that value is not
205195** included in the record (i.e. because the record is part of an UPDATE change
205196** and the field was not modified), the corresponding element of apOut[] is
205197** set to NULL.
205198**
205199** It is the responsibility of the caller to free all sqlite_value structures
205200** using sqlite3_free().
205201**
205202** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
205203** The apOut[] array may have been partially populated in this case.
205204*/
205205static int sessionReadRecord(
205206 SessionInput *pIn, /* Input data */
205207 int nCol, /* Number of values in record */
205208 u8 *abPK, /* Array of primary key flags, or NULL */
205209 sqlite3_value **apOut /* Write values to this array */
205210){
205211 int i; /* Used to iterate through columns */
205212 int rc = SQLITE_OK;
205213
205214 for(i=0; i<nCol && rc==SQLITE_OK; i++){
205215 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
205216 if( abPK && abPK[i]==0 ) continue;
205217 rc = sessionInputBuffer(pIn, 9);
205218 if( rc==SQLITE_OK ){
205219 if( pIn->iNext>=pIn->nData ){
205220 rc = SQLITE_CORRUPT_BKPT;
205221 }else{
205222 eType = pIn->aData[pIn->iNext++];
205223 assert( apOut[i]==0 );
205224 if( eType ){
205225 apOut[i] = sqlite3ValueNew(0);
205226 if( !apOut[i] ) rc = SQLITE_NOMEM;
205227 }
205228 }
205229 }
205230
205231 if( rc==SQLITE_OK ){
205232 u8 *aVal = &pIn->aData[pIn->iNext];
205233 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
205234 int nByte;
205235 pIn->iNext += sessionVarintGet(aVal, &nByte);
205236 rc = sessionInputBuffer(pIn, nByte);
205237 if( rc==SQLITE_OK ){
205238 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
205239 rc = SQLITE_CORRUPT_BKPT;
205240 }else{
205241 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
205242 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
205243 pIn->iNext += nByte;
205244 }
205245 }
205246 }
205247 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
205248 sqlite3_int64 v = sessionGetI64(aVal);
205249 if( eType==SQLITE_INTEGER ){
205250 sqlite3VdbeMemSetInt64(apOut[i], v);
205251 }else{
205252 double d;
205253 memcpy(&d, &v, 8);
205254 sqlite3VdbeMemSetDouble(apOut[i], d);
205255 }
205256 pIn->iNext += 8;
205257 }
205258 }
205259 }
205260
205261 return rc;
205262}
205263
205264/*
205265** The input pointer currently points to the second byte of a table-header.
205266** Specifically, to the following:
205267**
205268** + number of columns in table (varint)
205269** + array of PK flags (1 byte per column),
205270** + table name (nul terminated).
205271**
205272** This function ensures that all of the above is present in the input
205273** buffer (i.e. that it can be accessed without any calls to xInput()).
205274** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
205275** The input pointer is not moved.
205276*/
205277static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
205278 int rc = SQLITE_OK;
205279 int nCol = 0;
205280 int nRead = 0;
205281
205282 rc = sessionInputBuffer(pIn, 9);
205283 if( rc==SQLITE_OK ){
205284 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
205285 /* The hard upper limit for the number of columns in an SQLite
205286 ** database table is, according to sqliteLimit.h, 32676. So
205287 ** consider any table-header that purports to have more than 65536
205288 ** columns to be corrupt. This is convenient because otherwise,
205289 ** if the (nCol>65536) condition below were omitted, a sufficiently
205290 ** large value for nCol may cause nRead to wrap around and become
205291 ** negative. Leading to a crash. */
205292 if( nCol<0 || nCol>65536 ){
205293 rc = SQLITE_CORRUPT_BKPT;
205294 }else{
205295 rc = sessionInputBuffer(pIn, nRead+nCol+100);
205296 nRead += nCol;
205297 }
205298 }
205299
205300 while( rc==SQLITE_OK ){
205301 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
205302 nRead++;
205303 }
205304 if( (pIn->iNext + nRead)<pIn->nData ) break;
205305 rc = sessionInputBuffer(pIn, nRead + 100);
205306 }
205307 *pnByte = nRead+1;
205308 return rc;
205309}
205310
205311/*
205312** The input pointer currently points to the first byte of the first field
205313** of a record consisting of nCol columns. This function ensures the entire
205314** record is buffered. It does not move the input pointer.
205315**
205316** If successful, SQLITE_OK is returned and *pnByte is set to the size of
205317** the record in bytes. Otherwise, an SQLite error code is returned. The
205318** final value of *pnByte is undefined in this case.
205319*/
205320static int sessionChangesetBufferRecord(
205321 SessionInput *pIn, /* Input data */
205322 int nCol, /* Number of columns in record */
205323 int *pnByte /* OUT: Size of record in bytes */
205324){
205325 int rc = SQLITE_OK;
205326 int nByte = 0;
205327 int i;
205328 for(i=0; rc==SQLITE_OK && i<nCol; i++){
205329 int eType;
205330 rc = sessionInputBuffer(pIn, nByte + 10);
205331 if( rc==SQLITE_OK ){
205332 eType = pIn->aData[pIn->iNext + nByte++];
205333 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
205334 int n;
205335 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
205336 nByte += n;
205337 rc = sessionInputBuffer(pIn, nByte);
205338 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
205339 nByte += 8;
205340 }
205341 }
205342 }
205343 *pnByte = nByte;
205344 return rc;
205345}
205346
205347/*
205348** The input pointer currently points to the second byte of a table-header.
205349** Specifically, to the following:
205350**
205351** + number of columns in table (varint)
205352** + array of PK flags (1 byte per column),
205353** + table name (nul terminated).
205354**
205355** This function decodes the table-header and populates the p->nCol,
205356** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
205357** also allocated or resized according to the new value of p->nCol. The
205358** input pointer is left pointing to the byte following the table header.
205359**
205360** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
205361** is returned and the final values of the various fields enumerated above
205362** are undefined.
205363*/
205364static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
205365 int rc;
205366 int nCopy;
205367 assert( p->rc==SQLITE_OK );
205368
205369 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
205370 if( rc==SQLITE_OK ){
205371 int nByte;
205372 int nVarint;
205373 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
205374 if( p->nCol>0 ){
205375 nCopy -= nVarint;
205376 p->in.iNext += nVarint;
205377 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
205378 p->tblhdr.nBuf = 0;
205379 sessionBufferGrow(&p->tblhdr, nByte, &rc);
205380 }else{
205381 rc = SQLITE_CORRUPT_BKPT;
205382 }
205383 }
205384
205385 if( rc==SQLITE_OK ){
205386 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
205387 memset(p->tblhdr.aBuf, 0, iPK);
205388 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
205389 p->in.iNext += nCopy;
205390 }
205391
205392 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
205393 if( p->apValue==0 ){
205394 p->abPK = 0;
205395 p->zTab = 0;
205396 }else{
205397 p->abPK = (u8*)&p->apValue[p->nCol*2];
205398 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
205399 }
205400 return (p->rc = rc);
205401}
205402
205403/*
205404** Advance the changeset iterator to the next change.
205405**
205406** If both paRec and pnRec are NULL, then this function works like the public
205407** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
205408** sqlite3changeset_new() and old() APIs may be used to query for values.
205409**
205410** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
205411** record is written to *paRec before returning and the number of bytes in
205412** the record to *pnRec.
205413**
205414** Either way, this function returns SQLITE_ROW if the iterator is
205415** successfully advanced to the next change in the changeset, an SQLite
205416** error code if an error occurs, or SQLITE_DONE if there are no further
205417** changes in the changeset.
205418*/
205419static int sessionChangesetNext(
205420 sqlite3_changeset_iter *p, /* Changeset iterator */
205421 u8 **paRec, /* If non-NULL, store record pointer here */
205422 int *pnRec, /* If non-NULL, store size of record here */
205423 int *pbNew /* If non-NULL, true if new table */
205424){
205425 int i;
205426 u8 op;
205427
205428 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
205429
205430 /* If the iterator is in the error-state, return immediately. */
205431 if( p->rc!=SQLITE_OK ) return p->rc;
205432
205433 /* Free the current contents of p->apValue[], if any. */
205434 if( p->apValue ){
205435 for(i=0; i<p->nCol*2; i++){
205436 sqlite3ValueFree(p->apValue[i]);
205437 }
205438 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
205439 }
205440
205441 /* Make sure the buffer contains at least 10 bytes of input data, or all
205442 ** remaining data if there are less than 10 bytes available. This is
205443 ** sufficient either for the 'T' or 'P' byte and the varint that follows
205444 ** it, or for the two single byte values otherwise. */
205445 p->rc = sessionInputBuffer(&p->in, 2);
205446 if( p->rc!=SQLITE_OK ) return p->rc;
205447
205448 /* If the iterator is already at the end of the changeset, return DONE. */
205449 if( p->in.iNext>=p->in.nData ){
205450 return SQLITE_DONE;
205451 }
205452
205453 sessionDiscardData(&p->in);
205454 p->in.iCurrent = p->in.iNext;
205455
205456 op = p->in.aData[p->in.iNext++];
205457 while( op=='T' || op=='P' ){
205458 if( pbNew ) *pbNew = 1;
205459 p->bPatchset = (op=='P');
205460 if( sessionChangesetReadTblhdr(p) ) return p->rc;
205461 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
205462 p->in.iCurrent = p->in.iNext;
205463 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
205464 op = p->in.aData[p->in.iNext++];
205465 }
205466
205467 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
205468 /* The first record in the changeset is not a table header. Must be a
205469 ** corrupt changeset. */
205470 assert( p->in.iNext==1 || p->zTab );
205471 return (p->rc = SQLITE_CORRUPT_BKPT);
205472 }
205473
205474 p->op = op;
205475 p->bIndirect = p->in.aData[p->in.iNext++];
205476 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
205477 return (p->rc = SQLITE_CORRUPT_BKPT);
205478 }
205479
205480 if( paRec ){
205481 int nVal; /* Number of values to buffer */
205482 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
205483 nVal = p->nCol * 2;
205484 }else if( p->bPatchset && op==SQLITE_DELETE ){
205485 nVal = 0;
205486 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
205487 }else{
205488 nVal = p->nCol;
205489 }
205490 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
205491 if( p->rc!=SQLITE_OK ) return p->rc;
205492 *paRec = &p->in.aData[p->in.iNext];
205493 p->in.iNext += *pnRec;
205494 }else{
205495 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
205496 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
205497
205498 /* If this is an UPDATE or DELETE, read the old.* record. */
205499 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
205500 u8 *abPK = p->bPatchset ? p->abPK : 0;
205501 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
205502 if( p->rc!=SQLITE_OK ) return p->rc;
205503 }
205504
205505 /* If this is an INSERT or UPDATE, read the new.* record. */
205506 if( p->op!=SQLITE_DELETE ){
205507 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
205508 if( p->rc!=SQLITE_OK ) return p->rc;
205509 }
205510
205511 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
205512 /* If this is an UPDATE that is part of a patchset, then all PK and
205513 ** modified fields are present in the new.* record. The old.* record
205514 ** is currently completely empty. This block shifts the PK fields from
205515 ** new.* to old.*, to accommodate the code that reads these arrays. */
205516 for(i=0; i<p->nCol; i++){
205517 assert( p->bPatchset==0 || p->apValue[i]==0 );
205518 if( p->abPK[i] ){
205519 assert( p->apValue[i]==0 );
205520 p->apValue[i] = p->apValue[i+p->nCol];
205521 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
205522 p->apValue[i+p->nCol] = 0;
205523 }
205524 }
205525 }else if( p->bInvert ){
205526 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
205527 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
205528 }
205529 }
205530
205531 return SQLITE_ROW;
205532}
205533
205534/*
205535** Advance an iterator created by sqlite3changeset_start() to the next
205536** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
205537** or SQLITE_CORRUPT.
205538**
205539** This function may not be called on iterators passed to a conflict handler
205540** callback by changeset_apply().
205541*/
205542SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
205543 return sessionChangesetNext(p, 0, 0, 0);
205544}
205545
205546/*
205547** The following function extracts information on the current change
205548** from a changeset iterator. It may only be called after changeset_next()
205549** has returned SQLITE_ROW.
205550*/
205551SQLITE_API int sqlite3changeset_op(
205552 sqlite3_changeset_iter *pIter, /* Iterator handle */
205553 const char **pzTab, /* OUT: Pointer to table name */
205554 int *pnCol, /* OUT: Number of columns in table */
205555 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
205556 int *pbIndirect /* OUT: True if change is indirect */
205557){
205558 *pOp = pIter->op;
205559 *pnCol = pIter->nCol;
205560 *pzTab = pIter->zTab;
205561 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
205562 return SQLITE_OK;
205563}
205564
205565/*
205566** Return information regarding the PRIMARY KEY and number of columns in
205567** the database table affected by the change that pIter currently points
205568** to. This function may only be called after changeset_next() returns
205569** SQLITE_ROW.
205570*/
205571SQLITE_API int sqlite3changeset_pk(
205572 sqlite3_changeset_iter *pIter, /* Iterator object */
205573 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
205574 int *pnCol /* OUT: Number of entries in output array */
205575){
205576 *pabPK = pIter->abPK;
205577 if( pnCol ) *pnCol = pIter->nCol;
205578 return SQLITE_OK;
205579}
205580
205581/*
205582** This function may only be called while the iterator is pointing to an
205583** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
205584** Otherwise, SQLITE_MISUSE is returned.
205585**
205586** It sets *ppValue to point to an sqlite3_value structure containing the
205587** iVal'th value in the old.* record. Or, if that particular value is not
205588** included in the record (because the change is an UPDATE and the field
205589** was not modified and is not a PK column), set *ppValue to NULL.
205590**
205591** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
205592** not modified. Otherwise, SQLITE_OK.
205593*/
205594SQLITE_API int sqlite3changeset_old(
205595 sqlite3_changeset_iter *pIter, /* Changeset iterator */
205596 int iVal, /* Index of old.* value to retrieve */
205597 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
205598){
205599 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
205600 return SQLITE_MISUSE;
205601 }
205602 if( iVal<0 || iVal>=pIter->nCol ){
205603 return SQLITE_RANGE;
205604 }
205605 *ppValue = pIter->apValue[iVal];
205606 return SQLITE_OK;
205607}
205608
205609/*
205610** This function may only be called while the iterator is pointing to an
205611** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
205612** Otherwise, SQLITE_MISUSE is returned.
205613**
205614** It sets *ppValue to point to an sqlite3_value structure containing the
205615** iVal'th value in the new.* record. Or, if that particular value is not
205616** included in the record (because the change is an UPDATE and the field
205617** was not modified), set *ppValue to NULL.
205618**
205619** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
205620** not modified. Otherwise, SQLITE_OK.
205621*/
205622SQLITE_API int sqlite3changeset_new(
205623 sqlite3_changeset_iter *pIter, /* Changeset iterator */
205624 int iVal, /* Index of new.* value to retrieve */
205625 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
205626){
205627 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
205628 return SQLITE_MISUSE;
205629 }
205630 if( iVal<0 || iVal>=pIter->nCol ){
205631 return SQLITE_RANGE;
205632 }
205633 *ppValue = pIter->apValue[pIter->nCol+iVal];
205634 return SQLITE_OK;
205635}
205636
205637/*
205638** The following two macros are used internally. They are similar to the
205639** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
205640** they omit all error checking and return a pointer to the requested value.
205641*/
205642#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
205643#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
205644
205645/*
205646** This function may only be called with a changeset iterator that has been
205647** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
205648** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
205649**
205650** If successful, *ppValue is set to point to an sqlite3_value structure
205651** containing the iVal'th value of the conflicting record.
205652**
205653** If value iVal is out-of-range or some other error occurs, an SQLite error
205654** code is returned. Otherwise, SQLITE_OK.
205655*/
205656SQLITE_API int sqlite3changeset_conflict(
205657 sqlite3_changeset_iter *pIter, /* Changeset iterator */
205658 int iVal, /* Index of conflict record value to fetch */
205659 sqlite3_value **ppValue /* OUT: Value from conflicting row */
205660){
205661 if( !pIter->pConflict ){
205662 return SQLITE_MISUSE;
205663 }
205664 if( iVal<0 || iVal>=pIter->nCol ){
205665 return SQLITE_RANGE;
205666 }
205667 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
205668 return SQLITE_OK;
205669}
205670
205671/*
205672** This function may only be called with an iterator passed to an
205673** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
205674** it sets the output variable to the total number of known foreign key
205675** violations in the destination database and returns SQLITE_OK.
205676**
205677** In all other cases this function returns SQLITE_MISUSE.
205678*/
205679SQLITE_API int sqlite3changeset_fk_conflicts(
205680 sqlite3_changeset_iter *pIter, /* Changeset iterator */
205681 int *pnOut /* OUT: Number of FK violations */
205682){
205683 if( pIter->pConflict || pIter->apValue ){
205684 return SQLITE_MISUSE;
205685 }
205686 *pnOut = pIter->nCol;
205687 return SQLITE_OK;
205688}
205689
205690
205691/*
205692** Finalize an iterator allocated with sqlite3changeset_start().
205693**
205694** This function may not be called on iterators passed to a conflict handler
205695** callback by changeset_apply().
205696*/
205697SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
205698 int rc = SQLITE_OK;
205699 if( p ){
205700 int i; /* Used to iterate through p->apValue[] */
205701 rc = p->rc;
205702 if( p->apValue ){
205703 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
205704 }
205705 sqlite3_free(p->tblhdr.aBuf);
205706 sqlite3_free(p->in.buf.aBuf);
205707 sqlite3_free(p);
205708 }
205709 return rc;
205710}
205711
205712static int sessionChangesetInvert(
205713 SessionInput *pInput, /* Input changeset */
205714 int (*xOutput)(void *pOut, const void *pData, int nData),
205715 void *pOut,
205716 int *pnInverted, /* OUT: Number of bytes in output changeset */
205717 void **ppInverted /* OUT: Inverse of pChangeset */
205718){
205719 int rc = SQLITE_OK; /* Return value */
205720 SessionBuffer sOut; /* Output buffer */
205721 int nCol = 0; /* Number of cols in current table */
205722 u8 *abPK = 0; /* PK array for current table */
205723 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
205724 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
205725
205726 /* Initialize the output buffer */
205727 memset(&sOut, 0, sizeof(SessionBuffer));
205728
205729 /* Zero the output variables in case an error occurs. */
205730 if( ppInverted ){
205731 *ppInverted = 0;
205732 *pnInverted = 0;
205733 }
205734
205735 while( 1 ){
205736 u8 eType;
205737
205738 /* Test for EOF. */
205739 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
205740 if( pInput->iNext>=pInput->nData ) break;
205741 eType = pInput->aData[pInput->iNext];
205742
205743 switch( eType ){
205744 case 'T': {
205745 /* A 'table' record consists of:
205746 **
205747 ** * A constant 'T' character,
205748 ** * Number of columns in said table (a varint),
205749 ** * An array of nCol bytes (sPK),
205750 ** * A nul-terminated table name.
205751 */
205752 int nByte;
205753 int nVar;
205754 pInput->iNext++;
205755 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
205756 goto finished_invert;
205757 }
205758 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
205759 sPK.nBuf = 0;
205760 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
205761 sessionAppendByte(&sOut, eType, &rc);
205762 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
205763 if( rc ) goto finished_invert;
205764
205765 pInput->iNext += nByte;
205766 sqlite3_free(apVal);
205767 apVal = 0;
205768 abPK = sPK.aBuf;
205769 break;
205770 }
205771
205772 case SQLITE_INSERT:
205773 case SQLITE_DELETE: {
205774 int nByte;
205775 int bIndirect = pInput->aData[pInput->iNext+1];
205776 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
205777 pInput->iNext += 2;
205778 assert( rc==SQLITE_OK );
205779 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
205780 sessionAppendByte(&sOut, eType2, &rc);
205781 sessionAppendByte(&sOut, bIndirect, &rc);
205782 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
205783 pInput->iNext += nByte;
205784 if( rc ) goto finished_invert;
205785 break;
205786 }
205787
205788 case SQLITE_UPDATE: {
205789 int iCol;
205790
205791 if( 0==apVal ){
205792 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
205793 if( 0==apVal ){
205794 rc = SQLITE_NOMEM;
205795 goto finished_invert;
205796 }
205797 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
205798 }
205799
205800 /* Write the header for the new UPDATE change. Same as the original. */
205801 sessionAppendByte(&sOut, eType, &rc);
205802 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
205803
205804 /* Read the old.* and new.* records for the update change. */
205805 pInput->iNext += 2;
205806 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
205807 if( rc==SQLITE_OK ){
205808 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
205809 }
205810
205811 /* Write the new old.* record. Consists of the PK columns from the
205812 ** original old.* record, and the other values from the original
205813 ** new.* record. */
205814 for(iCol=0; iCol<nCol; iCol++){
205815 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
205816 sessionAppendValue(&sOut, pVal, &rc);
205817 }
205818
205819 /* Write the new new.* record. Consists of a copy of all values
205820 ** from the original old.* record, except for the PK columns, which
205821 ** are set to "undefined". */
205822 for(iCol=0; iCol<nCol; iCol++){
205823 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
205824 sessionAppendValue(&sOut, pVal, &rc);
205825 }
205826
205827 for(iCol=0; iCol<nCol*2; iCol++){
205828 sqlite3ValueFree(apVal[iCol]);
205829 }
205830 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
205831 if( rc!=SQLITE_OK ){
205832 goto finished_invert;
205833 }
205834
205835 break;
205836 }
205837
205838 default:
205839 rc = SQLITE_CORRUPT_BKPT;
205840 goto finished_invert;
205841 }
205842
205843 assert( rc==SQLITE_OK );
205844 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
205845 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
205846 sOut.nBuf = 0;
205847 if( rc!=SQLITE_OK ) goto finished_invert;
205848 }
205849 }
205850
205851 assert( rc==SQLITE_OK );
205852 if( pnInverted ){
205853 *pnInverted = sOut.nBuf;
205854 *ppInverted = sOut.aBuf;
205855 sOut.aBuf = 0;
205856 }else if( sOut.nBuf>0 ){
205857 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
205858 }
205859
205860 finished_invert:
205861 sqlite3_free(sOut.aBuf);
205862 sqlite3_free(apVal);
205863 sqlite3_free(sPK.aBuf);
205864 return rc;
205865}
205866
205867
205868/*
205869** Invert a changeset object.
205870*/
205871SQLITE_API int sqlite3changeset_invert(
205872 int nChangeset, /* Number of bytes in input */
205873 const void *pChangeset, /* Input changeset */
205874 int *pnInverted, /* OUT: Number of bytes in output changeset */
205875 void **ppInverted /* OUT: Inverse of pChangeset */
205876){
205877 SessionInput sInput;
205878
205879 /* Set up the input stream */
205880 memset(&sInput, 0, sizeof(SessionInput));
205881 sInput.nData = nChangeset;
205882 sInput.aData = (u8*)pChangeset;
205883
205884 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
205885}
205886
205887/*
205888** Streaming version of sqlite3changeset_invert().
205889*/
205890SQLITE_API int sqlite3changeset_invert_strm(
205891 int (*xInput)(void *pIn, void *pData, int *pnData),
205892 void *pIn,
205893 int (*xOutput)(void *pOut, const void *pData, int nData),
205894 void *pOut
205895){
205896 SessionInput sInput;
205897 int rc;
205898
205899 /* Set up the input stream */
205900 memset(&sInput, 0, sizeof(SessionInput));
205901 sInput.xInput = xInput;
205902 sInput.pIn = pIn;
205903
205904 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
205905 sqlite3_free(sInput.buf.aBuf);
205906 return rc;
205907}
205908
205909typedef struct SessionApplyCtx SessionApplyCtx;
205910struct SessionApplyCtx {
205911 sqlite3 *db;
205912 sqlite3_stmt *pDelete; /* DELETE statement */
205913 sqlite3_stmt *pUpdate; /* UPDATE statement */
205914 sqlite3_stmt *pInsert; /* INSERT statement */
205915 sqlite3_stmt *pSelect; /* SELECT statement */
205916 int nCol; /* Size of azCol[] and abPK[] arrays */
205917 const char **azCol; /* Array of column names */
205918 u8 *abPK; /* Boolean array - true if column is in PK */
205919 int bStat1; /* True if table is sqlite_stat1 */
205920 int bDeferConstraints; /* True to defer constraints */
205921 int bInvertConstraints; /* Invert when iterating constraints buffer */
205922 SessionBuffer constraints; /* Deferred constraints are stored here */
205923 SessionBuffer rebase; /* Rebase information (if any) here */
205924 u8 bRebaseStarted; /* If table header is already in rebase */
205925 u8 bRebase; /* True to collect rebase information */
205926};
205927
205928/*
205929** Formulate a statement to DELETE a row from database db. Assuming a table
205930** structure like this:
205931**
205932** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
205933**
205934** The DELETE statement looks like this:
205935**
205936** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
205937**
205938** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
205939** matching b and d values, or 1 otherwise. The second case comes up if the
205940** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
205941**
205942** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
205943** pointing to the prepared version of the SQL statement.
205944*/
205945static int sessionDeleteRow(
205946 sqlite3 *db, /* Database handle */
205947 const char *zTab, /* Table name */
205948 SessionApplyCtx *p /* Session changeset-apply context */
205949){
205950 int i;
205951 const char *zSep = "";
205952 int rc = SQLITE_OK;
205953 SessionBuffer buf = {0, 0, 0};
205954 int nPk = 0;
205955
205956 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
205957 sessionAppendIdent(&buf, zTab, &rc);
205958 sessionAppendStr(&buf, " WHERE ", &rc);
205959
205960 for(i=0; i<p->nCol; i++){
205961 if( p->abPK[i] ){
205962 nPk++;
205963 sessionAppendStr(&buf, zSep, &rc);
205964 sessionAppendIdent(&buf, p->azCol[i], &rc);
205965 sessionAppendStr(&buf, " = ?", &rc);
205966 sessionAppendInteger(&buf, i+1, &rc);
205967 zSep = " AND ";
205968 }
205969 }
205970
205971 if( nPk<p->nCol ){
205972 sessionAppendStr(&buf, " AND (?", &rc);
205973 sessionAppendInteger(&buf, p->nCol+1, &rc);
205974 sessionAppendStr(&buf, " OR ", &rc);
205975
205976 zSep = "";
205977 for(i=0; i<p->nCol; i++){
205978 if( !p->abPK[i] ){
205979 sessionAppendStr(&buf, zSep, &rc);
205980 sessionAppendIdent(&buf, p->azCol[i], &rc);
205981 sessionAppendStr(&buf, " IS ?", &rc);
205982 sessionAppendInteger(&buf, i+1, &rc);
205983 zSep = "AND ";
205984 }
205985 }
205986 sessionAppendStr(&buf, ")", &rc);
205987 }
205988
205989 if( rc==SQLITE_OK ){
205990 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
205991 }
205992 sqlite3_free(buf.aBuf);
205993
205994 return rc;
205995}
205996
205997/*
205998** Formulate and prepare a statement to UPDATE a row from database db.
205999** Assuming a table structure like this:
206000**
206001** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
206002**
206003** The UPDATE statement looks like this:
206004**
206005** UPDATE x SET
206006** a = CASE WHEN ?2 THEN ?3 ELSE a END,
206007** b = CASE WHEN ?5 THEN ?6 ELSE b END,
206008** c = CASE WHEN ?8 THEN ?9 ELSE c END,
206009** d = CASE WHEN ?11 THEN ?12 ELSE d END
206010** WHERE a = ?1 AND c = ?7 AND (?13 OR
206011** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
206012** )
206013**
206014** For each column in the table, there are three variables to bind:
206015**
206016** ?(i*3+1) The old.* value of the column, if any.
206017** ?(i*3+2) A boolean flag indicating that the value is being modified.
206018** ?(i*3+3) The new.* value of the column, if any.
206019**
206020** Also, a boolean flag that, if set to true, causes the statement to update
206021** a row even if the non-PK values do not match. This is required if the
206022** conflict-handler is invoked with CHANGESET_DATA and returns
206023** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
206024**
206025** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
206026** pointing to the prepared version of the SQL statement.
206027*/
206028static int sessionUpdateRow(
206029 sqlite3 *db, /* Database handle */
206030 const char *zTab, /* Table name */
206031 SessionApplyCtx *p /* Session changeset-apply context */
206032){
206033 int rc = SQLITE_OK;
206034 int i;
206035 const char *zSep = "";
206036 SessionBuffer buf = {0, 0, 0};
206037
206038 /* Append "UPDATE tbl SET " */
206039 sessionAppendStr(&buf, "UPDATE main.", &rc);
206040 sessionAppendIdent(&buf, zTab, &rc);
206041 sessionAppendStr(&buf, " SET ", &rc);
206042
206043 /* Append the assignments */
206044 for(i=0; i<p->nCol; i++){
206045 sessionAppendStr(&buf, zSep, &rc);
206046 sessionAppendIdent(&buf, p->azCol[i], &rc);
206047 sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
206048 sessionAppendInteger(&buf, i*3+2, &rc);
206049 sessionAppendStr(&buf, " THEN ?", &rc);
206050 sessionAppendInteger(&buf, i*3+3, &rc);
206051 sessionAppendStr(&buf, " ELSE ", &rc);
206052 sessionAppendIdent(&buf, p->azCol[i], &rc);
206053 sessionAppendStr(&buf, " END", &rc);
206054 zSep = ", ";
206055 }
206056
206057 /* Append the PK part of the WHERE clause */
206058 sessionAppendStr(&buf, " WHERE ", &rc);
206059 for(i=0; i<p->nCol; i++){
206060 if( p->abPK[i] ){
206061 sessionAppendIdent(&buf, p->azCol[i], &rc);
206062 sessionAppendStr(&buf, " = ?", &rc);
206063 sessionAppendInteger(&buf, i*3+1, &rc);
206064 sessionAppendStr(&buf, " AND ", &rc);
206065 }
206066 }
206067
206068 /* Append the non-PK part of the WHERE clause */
206069 sessionAppendStr(&buf, " (?", &rc);
206070 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
206071 sessionAppendStr(&buf, " OR 1", &rc);
206072 for(i=0; i<p->nCol; i++){
206073 if( !p->abPK[i] ){
206074 sessionAppendStr(&buf, " AND (?", &rc);
206075 sessionAppendInteger(&buf, i*3+2, &rc);
206076 sessionAppendStr(&buf, "=0 OR ", &rc);
206077 sessionAppendIdent(&buf, p->azCol[i], &rc);
206078 sessionAppendStr(&buf, " IS ?", &rc);
206079 sessionAppendInteger(&buf, i*3+1, &rc);
206080 sessionAppendStr(&buf, ")", &rc);
206081 }
206082 }
206083 sessionAppendStr(&buf, ")", &rc);
206084
206085 if( rc==SQLITE_OK ){
206086 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
206087 }
206088 sqlite3_free(buf.aBuf);
206089
206090 return rc;
206091}
206092
206093
206094/*
206095** Formulate and prepare an SQL statement to query table zTab by primary
206096** key. Assuming the following table structure:
206097**
206098** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
206099**
206100** The SELECT statement looks like this:
206101**
206102** SELECT * FROM x WHERE a = ?1 AND c = ?3
206103**
206104** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
206105** pointing to the prepared version of the SQL statement.
206106*/
206107static int sessionSelectRow(
206108 sqlite3 *db, /* Database handle */
206109 const char *zTab, /* Table name */
206110 SessionApplyCtx *p /* Session changeset-apply context */
206111){
206112 return sessionSelectStmt(
206113 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
206114}
206115
206116/*
206117** Formulate and prepare an INSERT statement to add a record to table zTab.
206118** For example:
206119**
206120** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
206121**
206122** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
206123** pointing to the prepared version of the SQL statement.
206124*/
206125static int sessionInsertRow(
206126 sqlite3 *db, /* Database handle */
206127 const char *zTab, /* Table name */
206128 SessionApplyCtx *p /* Session changeset-apply context */
206129){
206130 int rc = SQLITE_OK;
206131 int i;
206132 SessionBuffer buf = {0, 0, 0};
206133
206134 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
206135 sessionAppendIdent(&buf, zTab, &rc);
206136 sessionAppendStr(&buf, "(", &rc);
206137 for(i=0; i<p->nCol; i++){
206138 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
206139 sessionAppendIdent(&buf, p->azCol[i], &rc);
206140 }
206141
206142 sessionAppendStr(&buf, ") VALUES(?", &rc);
206143 for(i=1; i<p->nCol; i++){
206144 sessionAppendStr(&buf, ", ?", &rc);
206145 }
206146 sessionAppendStr(&buf, ")", &rc);
206147
206148 if( rc==SQLITE_OK ){
206149 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
206150 }
206151 sqlite3_free(buf.aBuf);
206152 return rc;
206153}
206154
206155static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
206156 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
206157}
206158
206159/*
206160** Prepare statements for applying changes to the sqlite_stat1 table.
206161** These are similar to those created by sessionSelectRow(),
206162** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
206163** other tables.
206164*/
206165static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
206166 int rc = sessionSelectRow(db, "sqlite_stat1", p);
206167 if( rc==SQLITE_OK ){
206168 rc = sessionPrepare(db, &p->pInsert,
206169 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
206170 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
206171 "?3)"
206172 );
206173 }
206174 if( rc==SQLITE_OK ){
206175 rc = sessionPrepare(db, &p->pUpdate,
206176 "UPDATE main.sqlite_stat1 SET "
206177 "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
206178 "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
206179 "stat = CASE WHEN ?8 THEN ?9 ELSE stat END "
206180 "WHERE tbl=?1 AND idx IS "
206181 "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
206182 "AND (?10 OR ?8=0 OR stat IS ?7)"
206183 );
206184 }
206185 if( rc==SQLITE_OK ){
206186 rc = sessionPrepare(db, &p->pDelete,
206187 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
206188 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
206189 "AND (?4 OR stat IS ?3)"
206190 );
206191 }
206192 return rc;
206193}
206194
206195/*
206196** A wrapper around sqlite3_bind_value() that detects an extra problem.
206197** See comments in the body of this function for details.
206198*/
206199static int sessionBindValue(
206200 sqlite3_stmt *pStmt, /* Statement to bind value to */
206201 int i, /* Parameter number to bind to */
206202 sqlite3_value *pVal /* Value to bind */
206203){
206204 int eType = sqlite3_value_type(pVal);
206205 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
206206 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
206207 ** the (pVal->z) variable remains as it was or the type of the value is
206208 ** set to SQLITE_NULL. */
206209 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
206210 /* This condition occurs when an earlier OOM in a call to
206211 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
206212 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
206213 return SQLITE_NOMEM;
206214 }
206215 return sqlite3_bind_value(pStmt, i, pVal);
206216}
206217
206218/*
206219** Iterator pIter must point to an SQLITE_INSERT entry. This function
206220** transfers new.* values from the current iterator entry to statement
206221** pStmt. The table being inserted into has nCol columns.
206222**
206223** New.* value $i from the iterator is bound to variable ($i+1) of
206224** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
206225** are transfered to the statement. Otherwise, if abPK is not NULL, it points
206226** to an array nCol elements in size. In this case only those values for
206227** which abPK[$i] is true are read from the iterator and bound to the
206228** statement.
206229**
206230** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
206231*/
206232static int sessionBindRow(
206233 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
206234 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
206235 int nCol, /* Number of columns */
206236 u8 *abPK, /* If not NULL, bind only if true */
206237 sqlite3_stmt *pStmt /* Bind values to this statement */
206238){
206239 int i;
206240 int rc = SQLITE_OK;
206241
206242 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
206243 ** argument iterator points to a suitable entry. Make sure that xValue
206244 ** is one of these to guarantee that it is safe to ignore the return
206245 ** in the code below. */
206246 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
206247
206248 for(i=0; rc==SQLITE_OK && i<nCol; i++){
206249 if( !abPK || abPK[i] ){
206250 sqlite3_value *pVal;
206251 (void)xValue(pIter, i, &pVal);
206252 if( pVal==0 ){
206253 /* The value in the changeset was "undefined". This indicates a
206254 ** corrupt changeset blob. */
206255 rc = SQLITE_CORRUPT_BKPT;
206256 }else{
206257 rc = sessionBindValue(pStmt, i+1, pVal);
206258 }
206259 }
206260 }
206261 return rc;
206262}
206263
206264/*
206265** SQL statement pSelect is as generated by the sessionSelectRow() function.
206266** This function binds the primary key values from the change that changeset
206267** iterator pIter points to to the SELECT and attempts to seek to the table
206268** entry. If a row is found, the SELECT statement left pointing at the row
206269** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
206270** has occured, the statement is reset and SQLITE_OK is returned. If an
206271** error occurs, the statement is reset and an SQLite error code is returned.
206272**
206273** If this function returns SQLITE_ROW, the caller must eventually reset()
206274** statement pSelect. If any other value is returned, the statement does
206275** not require a reset().
206276**
206277** If the iterator currently points to an INSERT record, bind values from the
206278** new.* record to the SELECT statement. Or, if it points to a DELETE or
206279** UPDATE, bind values from the old.* record.
206280*/
206281static int sessionSeekToRow(
206282 sqlite3 *db, /* Database handle */
206283 sqlite3_changeset_iter *pIter, /* Changeset iterator */
206284 u8 *abPK, /* Primary key flags array */
206285 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
206286){
206287 int rc; /* Return code */
206288 int nCol; /* Number of columns in table */
206289 int op; /* Changset operation (SQLITE_UPDATE etc.) */
206290 const char *zDummy; /* Unused */
206291
206292 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
206293 rc = sessionBindRow(pIter,
206294 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
206295 nCol, abPK, pSelect
206296 );
206297
206298 if( rc==SQLITE_OK ){
206299 rc = sqlite3_step(pSelect);
206300 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
206301 }
206302
206303 return rc;
206304}
206305
206306/*
206307** This function is called from within sqlite3changeset_apply_v2() when
206308** a conflict is encountered and resolved using conflict resolution
206309** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
206310** It adds a conflict resolution record to the buffer in
206311** SessionApplyCtx.rebase, which will eventually be returned to the caller
206312** of apply_v2() as the "rebase" buffer.
206313**
206314** Return SQLITE_OK if successful, or an SQLite error code otherwise.
206315*/
206316static int sessionRebaseAdd(
206317 SessionApplyCtx *p, /* Apply context */
206318 int eType, /* Conflict resolution (OMIT or REPLACE) */
206319 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
206320){
206321 int rc = SQLITE_OK;
206322 if( p->bRebase ){
206323 int i;
206324 int eOp = pIter->op;
206325 if( p->bRebaseStarted==0 ){
206326 /* Append a table-header to the rebase buffer */
206327 const char *zTab = pIter->zTab;
206328 sessionAppendByte(&p->rebase, 'T', &rc);
206329 sessionAppendVarint(&p->rebase, p->nCol, &rc);
206330 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
206331 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
206332 p->bRebaseStarted = 1;
206333 }
206334
206335 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
206336 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
206337
206338 sessionAppendByte(&p->rebase,
206339 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
206340 );
206341 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
206342 for(i=0; i<p->nCol; i++){
206343 sqlite3_value *pVal = 0;
206344 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
206345 sqlite3changeset_old(pIter, i, &pVal);
206346 }else{
206347 sqlite3changeset_new(pIter, i, &pVal);
206348 }
206349 sessionAppendValue(&p->rebase, pVal, &rc);
206350 }
206351 }
206352 return rc;
206353}
206354
206355/*
206356** Invoke the conflict handler for the change that the changeset iterator
206357** currently points to.
206358**
206359** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
206360** If argument pbReplace is NULL, then the type of conflict handler invoked
206361** depends solely on eType, as follows:
206362**
206363** eType value Value passed to xConflict
206364** -------------------------------------------------
206365** CHANGESET_DATA CHANGESET_NOTFOUND
206366** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
206367**
206368** Or, if pbReplace is not NULL, then an attempt is made to find an existing
206369** record with the same primary key as the record about to be deleted, updated
206370** or inserted. If such a record can be found, it is available to the conflict
206371** handler as the "conflicting" record. In this case the type of conflict
206372** handler invoked is as follows:
206373**
206374** eType value PK Record found? Value passed to xConflict
206375** ----------------------------------------------------------------
206376** CHANGESET_DATA Yes CHANGESET_DATA
206377** CHANGESET_DATA No CHANGESET_NOTFOUND
206378** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
206379** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
206380**
206381** If pbReplace is not NULL, and a record with a matching PK is found, and
206382** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
206383** is set to non-zero before returning SQLITE_OK.
206384**
206385** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
206386** returned. Or, if the conflict handler returns an invalid value,
206387** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
206388** this function returns SQLITE_OK.
206389*/
206390static int sessionConflictHandler(
206391 int eType, /* Either CHANGESET_DATA or CONFLICT */
206392 SessionApplyCtx *p, /* changeset_apply() context */
206393 sqlite3_changeset_iter *pIter, /* Changeset iterator */
206394 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
206395 void *pCtx, /* First argument for conflict handler */
206396 int *pbReplace /* OUT: Set to true if PK row is found */
206397){
206398 int res = 0; /* Value returned by conflict handler */
206399 int rc;
206400 int nCol;
206401 int op;
206402 const char *zDummy;
206403
206404 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
206405
206406 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
206407 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
206408 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
206409
206410 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
206411 if( pbReplace ){
206412 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
206413 }else{
206414 rc = SQLITE_OK;
206415 }
206416
206417 if( rc==SQLITE_ROW ){
206418 /* There exists another row with the new.* primary key. */
206419 pIter->pConflict = p->pSelect;
206420 res = xConflict(pCtx, eType, pIter);
206421 pIter->pConflict = 0;
206422 rc = sqlite3_reset(p->pSelect);
206423 }else if( rc==SQLITE_OK ){
206424 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
206425 /* Instead of invoking the conflict handler, append the change blob
206426 ** to the SessionApplyCtx.constraints buffer. */
206427 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
206428 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
206429 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
206430 return SQLITE_OK;
206431 }else{
206432 /* No other row with the new.* primary key. */
206433 res = xConflict(pCtx, eType+1, pIter);
206434 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
206435 }
206436 }
206437
206438 if( rc==SQLITE_OK ){
206439 switch( res ){
206440 case SQLITE_CHANGESET_REPLACE:
206441 assert( pbReplace );
206442 *pbReplace = 1;
206443 break;
206444
206445 case SQLITE_CHANGESET_OMIT:
206446 break;
206447
206448 case SQLITE_CHANGESET_ABORT:
206449 rc = SQLITE_ABORT;
206450 break;
206451
206452 default:
206453 rc = SQLITE_MISUSE;
206454 break;
206455 }
206456 if( rc==SQLITE_OK ){
206457 rc = sessionRebaseAdd(p, res, pIter);
206458 }
206459 }
206460
206461 return rc;
206462}
206463
206464/*
206465** Attempt to apply the change that the iterator passed as the first argument
206466** currently points to to the database. If a conflict is encountered, invoke
206467** the conflict handler callback.
206468**
206469** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
206470** one is encountered, update or delete the row with the matching primary key
206471** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
206472** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
206473** to true before returning. In this case the caller will invoke this function
206474** again, this time with pbRetry set to NULL.
206475**
206476** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
206477** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
206478** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
206479** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
206480** before retrying. In this case the caller attempts to remove the conflicting
206481** row before invoking this function again, this time with pbReplace set
206482** to NULL.
206483**
206484** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
206485** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
206486** returned.
206487*/
206488static int sessionApplyOneOp(
206489 sqlite3_changeset_iter *pIter, /* Changeset iterator */
206490 SessionApplyCtx *p, /* changeset_apply() context */
206491 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
206492 void *pCtx, /* First argument for the conflict handler */
206493 int *pbReplace, /* OUT: True to remove PK row and retry */
206494 int *pbRetry /* OUT: True to retry. */
206495){
206496 const char *zDummy;
206497 int op;
206498 int nCol;
206499 int rc = SQLITE_OK;
206500
206501 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
206502 assert( p->azCol && p->abPK );
206503 assert( !pbReplace || *pbReplace==0 );
206504
206505 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
206506
206507 if( op==SQLITE_DELETE ){
206508
206509 /* Bind values to the DELETE statement. If conflict handling is required,
206510 ** bind values for all columns and set bound variable (nCol+1) to true.
206511 ** Or, if conflict handling is not required, bind just the PK column
206512 ** values and, if it exists, set (nCol+1) to false. Conflict handling
206513 ** is not required if:
206514 **
206515 ** * this is a patchset, or
206516 ** * (pbRetry==0), or
206517 ** * all columns of the table are PK columns (in this case there is
206518 ** no (nCol+1) variable to bind to).
206519 */
206520 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
206521 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
206522 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
206523 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
206524 }
206525 if( rc!=SQLITE_OK ) return rc;
206526
206527 sqlite3_step(p->pDelete);
206528 rc = sqlite3_reset(p->pDelete);
206529 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
206530 rc = sessionConflictHandler(
206531 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
206532 );
206533 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
206534 rc = sessionConflictHandler(
206535 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
206536 );
206537 }
206538
206539 }else if( op==SQLITE_UPDATE ){
206540 int i;
206541
206542 /* Bind values to the UPDATE statement. */
206543 for(i=0; rc==SQLITE_OK && i<nCol; i++){
206544 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
206545 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
206546
206547 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
206548 if( pOld ){
206549 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
206550 }
206551 if( rc==SQLITE_OK && pNew ){
206552 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
206553 }
206554 }
206555 if( rc==SQLITE_OK ){
206556 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
206557 }
206558 if( rc!=SQLITE_OK ) return rc;
206559
206560 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
206561 ** the result will be SQLITE_OK with 0 rows modified. */
206562 sqlite3_step(p->pUpdate);
206563 rc = sqlite3_reset(p->pUpdate);
206564
206565 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
206566 /* A NOTFOUND or DATA error. Search the table to see if it contains
206567 ** a row with a matching primary key. If so, this is a DATA conflict.
206568 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
206569
206570 rc = sessionConflictHandler(
206571 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
206572 );
206573
206574 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
206575 /* This is always a CONSTRAINT conflict. */
206576 rc = sessionConflictHandler(
206577 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
206578 );
206579 }
206580
206581 }else{
206582 assert( op==SQLITE_INSERT );
206583 if( p->bStat1 ){
206584 /* Check if there is a conflicting row. For sqlite_stat1, this needs
206585 ** to be done using a SELECT, as there is no PRIMARY KEY in the
206586 ** database schema to throw an exception if a duplicate is inserted. */
206587 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
206588 if( rc==SQLITE_ROW ){
206589 rc = SQLITE_CONSTRAINT;
206590 sqlite3_reset(p->pSelect);
206591 }
206592 }
206593
206594 if( rc==SQLITE_OK ){
206595 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
206596 if( rc!=SQLITE_OK ) return rc;
206597
206598 sqlite3_step(p->pInsert);
206599 rc = sqlite3_reset(p->pInsert);
206600 }
206601
206602 if( (rc&0xff)==SQLITE_CONSTRAINT ){
206603 rc = sessionConflictHandler(
206604 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
206605 );
206606 }
206607 }
206608
206609 return rc;
206610}
206611
206612/*
206613** Attempt to apply the change that the iterator passed as the first argument
206614** currently points to to the database. If a conflict is encountered, invoke
206615** the conflict handler callback.
206616**
206617** The difference between this function and sessionApplyOne() is that this
206618** function handles the case where the conflict-handler is invoked and
206619** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
206620** retried in some manner.
206621*/
206622static int sessionApplyOneWithRetry(
206623 sqlite3 *db, /* Apply change to "main" db of this handle */
206624 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
206625 SessionApplyCtx *pApply, /* Apply context */
206626 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
206627 void *pCtx /* First argument passed to xConflict */
206628){
206629 int bReplace = 0;
206630 int bRetry = 0;
206631 int rc;
206632
206633 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
206634 if( rc==SQLITE_OK ){
206635 /* If the bRetry flag is set, the change has not been applied due to an
206636 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
206637 ** a row with the correct PK is present in the db, but one or more other
206638 ** fields do not contain the expected values) and the conflict handler
206639 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
206640 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
206641 ** the SQLITE_CHANGESET_DATA problem. */
206642 if( bRetry ){
206643 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
206644 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
206645 }
206646
206647 /* If the bReplace flag is set, the change is an INSERT that has not
206648 ** been performed because the database already contains a row with the
206649 ** specified primary key and the conflict handler returned
206650 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
206651 ** before reattempting the INSERT. */
206652 else if( bReplace ){
206653 assert( pIter->op==SQLITE_INSERT );
206654 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
206655 if( rc==SQLITE_OK ){
206656 rc = sessionBindRow(pIter,
206657 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
206658 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
206659 }
206660 if( rc==SQLITE_OK ){
206661 sqlite3_step(pApply->pDelete);
206662 rc = sqlite3_reset(pApply->pDelete);
206663 }
206664 if( rc==SQLITE_OK ){
206665 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
206666 }
206667 if( rc==SQLITE_OK ){
206668 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
206669 }
206670 }
206671 }
206672
206673 return rc;
206674}
206675
206676/*
206677** Retry the changes accumulated in the pApply->constraints buffer.
206678*/
206679static int sessionRetryConstraints(
206680 sqlite3 *db,
206681 int bPatchset,
206682 const char *zTab,
206683 SessionApplyCtx *pApply,
206684 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
206685 void *pCtx /* First argument passed to xConflict */
206686){
206687 int rc = SQLITE_OK;
206688
206689 while( pApply->constraints.nBuf ){
206690 sqlite3_changeset_iter *pIter2 = 0;
206691 SessionBuffer cons = pApply->constraints;
206692 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
206693
206694 rc = sessionChangesetStart(
206695 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints
206696 );
206697 if( rc==SQLITE_OK ){
206698 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
206699 int rc2;
206700 pIter2->bPatchset = bPatchset;
206701 pIter2->zTab = (char*)zTab;
206702 pIter2->nCol = pApply->nCol;
206703 pIter2->abPK = pApply->abPK;
206704 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
206705 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
206706 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
206707
206708 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
206709 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
206710 }
206711
206712 rc2 = sqlite3changeset_finalize(pIter2);
206713 if( rc==SQLITE_OK ) rc = rc2;
206714 }
206715 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
206716
206717 sqlite3_free(cons.aBuf);
206718 if( rc!=SQLITE_OK ) break;
206719 if( pApply->constraints.nBuf>=cons.nBuf ){
206720 /* No progress was made on the last round. */
206721 pApply->bDeferConstraints = 0;
206722 }
206723 }
206724
206725 return rc;
206726}
206727
206728/*
206729** Argument pIter is a changeset iterator that has been initialized, but
206730** not yet passed to sqlite3changeset_next(). This function applies the
206731** changeset to the main database attached to handle "db". The supplied
206732** conflict handler callback is invoked to resolve any conflicts encountered
206733** while applying the change.
206734*/
206735static int sessionChangesetApply(
206736 sqlite3 *db, /* Apply change to "main" db of this handle */
206737 sqlite3_changeset_iter *pIter, /* Changeset to apply */
206738 int(*xFilter)(
206739 void *pCtx, /* Copy of sixth arg to _apply() */
206740 const char *zTab /* Table name */
206741 ),
206742 int(*xConflict)(
206743 void *pCtx, /* Copy of fifth arg to _apply() */
206744 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
206745 sqlite3_changeset_iter *p /* Handle describing change and conflict */
206746 ),
206747 void *pCtx, /* First argument passed to xConflict */
206748 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
206749 int flags /* SESSION_APPLY_XXX flags */
206750){
206751 int schemaMismatch = 0;
206752 int rc = SQLITE_OK; /* Return code */
206753 const char *zTab = 0; /* Name of current table */
206754 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
206755 SessionApplyCtx sApply; /* changeset_apply() context object */
206756 int bPatchset;
206757
206758 assert( xConflict!=0 );
206759
206760 pIter->in.bNoDiscard = 1;
206761 memset(&sApply, 0, sizeof(sApply));
206762 sApply.bRebase = (ppRebase && pnRebase);
206763 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
206764 sqlite3_mutex_enter(sqlite3_db_mutex(db));
206765 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
206766 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
206767 }
206768 if( rc==SQLITE_OK ){
206769 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
206770 }
206771 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
206772 int nCol;
206773 int op;
206774 const char *zNew;
206775
206776 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
206777
206778 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
206779 u8 *abPK;
206780
206781 rc = sessionRetryConstraints(
206782 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
206783 );
206784 if( rc!=SQLITE_OK ) break;
206785
206786 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
206787 sqlite3_finalize(sApply.pDelete);
206788 sqlite3_finalize(sApply.pUpdate);
206789 sqlite3_finalize(sApply.pInsert);
206790 sqlite3_finalize(sApply.pSelect);
206791 sApply.db = db;
206792 sApply.pDelete = 0;
206793 sApply.pUpdate = 0;
206794 sApply.pInsert = 0;
206795 sApply.pSelect = 0;
206796 sApply.nCol = 0;
206797 sApply.azCol = 0;
206798 sApply.abPK = 0;
206799 sApply.bStat1 = 0;
206800 sApply.bDeferConstraints = 1;
206801 sApply.bRebaseStarted = 0;
206802 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
206803
206804 /* If an xFilter() callback was specified, invoke it now. If the
206805 ** xFilter callback returns zero, skip this table. If it returns
206806 ** non-zero, proceed. */
206807 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
206808 if( schemaMismatch ){
206809 zTab = sqlite3_mprintf("%s", zNew);
206810 if( zTab==0 ){
206811 rc = SQLITE_NOMEM;
206812 break;
206813 }
206814 nTab = (int)strlen(zTab);
206815 sApply.azCol = (const char **)zTab;
206816 }else{
206817 int nMinCol = 0;
206818 int i;
206819
206820 sqlite3changeset_pk(pIter, &abPK, 0);
206821 rc = sessionTableInfo(
206822 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
206823 );
206824 if( rc!=SQLITE_OK ) break;
206825 for(i=0; i<sApply.nCol; i++){
206826 if( sApply.abPK[i] ) nMinCol = i+1;
206827 }
206828
206829 if( sApply.nCol==0 ){
206830 schemaMismatch = 1;
206831 sqlite3_log(SQLITE_SCHEMA,
206832 "sqlite3changeset_apply(): no such table: %s", zTab
206833 );
206834 }
206835 else if( sApply.nCol<nCol ){
206836 schemaMismatch = 1;
206837 sqlite3_log(SQLITE_SCHEMA,
206838 "sqlite3changeset_apply(): table %s has %d columns, "
206839 "expected %d or more",
206840 zTab, sApply.nCol, nCol
206841 );
206842 }
206843 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
206844 schemaMismatch = 1;
206845 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
206846 "primary key mismatch for table %s", zTab
206847 );
206848 }
206849 else{
206850 sApply.nCol = nCol;
206851 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
206852 if( (rc = sessionStat1Sql(db, &sApply) ) ){
206853 break;
206854 }
206855 sApply.bStat1 = 1;
206856 }else{
206857 if((rc = sessionSelectRow(db, zTab, &sApply))
206858 || (rc = sessionUpdateRow(db, zTab, &sApply))
206859 || (rc = sessionDeleteRow(db, zTab, &sApply))
206860 || (rc = sessionInsertRow(db, zTab, &sApply))
206861 ){
206862 break;
206863 }
206864 sApply.bStat1 = 0;
206865 }
206866 }
206867 nTab = sqlite3Strlen30(zTab);
206868 }
206869 }
206870
206871 /* If there is a schema mismatch on the current table, proceed to the
206872 ** next change. A log message has already been issued. */
206873 if( schemaMismatch ) continue;
206874
206875 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
206876 }
206877
206878 bPatchset = pIter->bPatchset;
206879 if( rc==SQLITE_OK ){
206880 rc = sqlite3changeset_finalize(pIter);
206881 }else{
206882 sqlite3changeset_finalize(pIter);
206883 }
206884
206885 if( rc==SQLITE_OK ){
206886 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
206887 }
206888
206889 if( rc==SQLITE_OK ){
206890 int nFk, notUsed;
206891 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
206892 if( nFk!=0 ){
206893 int res = SQLITE_CHANGESET_ABORT;
206894 sqlite3_changeset_iter sIter;
206895 memset(&sIter, 0, sizeof(sIter));
206896 sIter.nCol = nFk;
206897 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
206898 if( res!=SQLITE_CHANGESET_OMIT ){
206899 rc = SQLITE_CONSTRAINT;
206900 }
206901 }
206902 }
206903 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
206904
206905 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
206906 if( rc==SQLITE_OK ){
206907 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
206908 }else{
206909 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
206910 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
206911 }
206912 }
206913
206914 assert( sApply.bRebase || sApply.rebase.nBuf==0 );
206915 if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
206916 *ppRebase = (void*)sApply.rebase.aBuf;
206917 *pnRebase = sApply.rebase.nBuf;
206918 sApply.rebase.aBuf = 0;
206919 }
206920 sqlite3_finalize(sApply.pInsert);
206921 sqlite3_finalize(sApply.pDelete);
206922 sqlite3_finalize(sApply.pUpdate);
206923 sqlite3_finalize(sApply.pSelect);
206924 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
206925 sqlite3_free((char*)sApply.constraints.aBuf);
206926 sqlite3_free((char*)sApply.rebase.aBuf);
206927 sqlite3_mutex_leave(sqlite3_db_mutex(db));
206928 return rc;
206929}
206930
206931/*
206932** Apply the changeset passed via pChangeset/nChangeset to the main
206933** database attached to handle "db".
206934*/
206935SQLITE_API int sqlite3changeset_apply_v2(
206936 sqlite3 *db, /* Apply change to "main" db of this handle */
206937 int nChangeset, /* Size of changeset in bytes */
206938 void *pChangeset, /* Changeset blob */
206939 int(*xFilter)(
206940 void *pCtx, /* Copy of sixth arg to _apply() */
206941 const char *zTab /* Table name */
206942 ),
206943 int(*xConflict)(
206944 void *pCtx, /* Copy of sixth arg to _apply() */
206945 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
206946 sqlite3_changeset_iter *p /* Handle describing change and conflict */
206947 ),
206948 void *pCtx, /* First argument passed to xConflict */
206949 void **ppRebase, int *pnRebase,
206950 int flags
206951){
206952 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
206953 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
206954 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
206955 if( rc==SQLITE_OK ){
206956 rc = sessionChangesetApply(
206957 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
206958 );
206959 }
206960 return rc;
206961}
206962
206963/*
206964** Apply the changeset passed via pChangeset/nChangeset to the main database
206965** attached to handle "db". Invoke the supplied conflict handler callback
206966** to resolve any conflicts encountered while applying the change.
206967*/
206968SQLITE_API int sqlite3changeset_apply(
206969 sqlite3 *db, /* Apply change to "main" db of this handle */
206970 int nChangeset, /* Size of changeset in bytes */
206971 void *pChangeset, /* Changeset blob */
206972 int(*xFilter)(
206973 void *pCtx, /* Copy of sixth arg to _apply() */
206974 const char *zTab /* Table name */
206975 ),
206976 int(*xConflict)(
206977 void *pCtx, /* Copy of fifth arg to _apply() */
206978 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
206979 sqlite3_changeset_iter *p /* Handle describing change and conflict */
206980 ),
206981 void *pCtx /* First argument passed to xConflict */
206982){
206983 return sqlite3changeset_apply_v2(
206984 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
206985 );
206986}
206987
206988/*
206989** Apply the changeset passed via xInput/pIn to the main database
206990** attached to handle "db". Invoke the supplied conflict handler callback
206991** to resolve any conflicts encountered while applying the change.
206992*/
206993SQLITE_API int sqlite3changeset_apply_v2_strm(
206994 sqlite3 *db, /* Apply change to "main" db of this handle */
206995 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
206996 void *pIn, /* First arg for xInput */
206997 int(*xFilter)(
206998 void *pCtx, /* Copy of sixth arg to _apply() */
206999 const char *zTab /* Table name */
207000 ),
207001 int(*xConflict)(
207002 void *pCtx, /* Copy of sixth arg to _apply() */
207003 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
207004 sqlite3_changeset_iter *p /* Handle describing change and conflict */
207005 ),
207006 void *pCtx, /* First argument passed to xConflict */
207007 void **ppRebase, int *pnRebase,
207008 int flags
207009){
207010 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
207011 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
207012 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
207013 if( rc==SQLITE_OK ){
207014 rc = sessionChangesetApply(
207015 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
207016 );
207017 }
207018 return rc;
207019}
207020SQLITE_API int sqlite3changeset_apply_strm(
207021 sqlite3 *db, /* Apply change to "main" db of this handle */
207022 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
207023 void *pIn, /* First arg for xInput */
207024 int(*xFilter)(
207025 void *pCtx, /* Copy of sixth arg to _apply() */
207026 const char *zTab /* Table name */
207027 ),
207028 int(*xConflict)(
207029 void *pCtx, /* Copy of sixth arg to _apply() */
207030 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
207031 sqlite3_changeset_iter *p /* Handle describing change and conflict */
207032 ),
207033 void *pCtx /* First argument passed to xConflict */
207034){
207035 return sqlite3changeset_apply_v2_strm(
207036 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
207037 );
207038}
207039
207040/*
207041** sqlite3_changegroup handle.
207042*/
207043struct sqlite3_changegroup {
207044 int rc; /* Error code */
207045 int bPatch; /* True to accumulate patchsets */
207046 SessionTable *pList; /* List of tables in current patch */
207047};
207048
207049/*
207050** This function is called to merge two changes to the same row together as
207051** part of an sqlite3changeset_concat() operation. A new change object is
207052** allocated and a pointer to it stored in *ppNew.
207053*/
207054static int sessionChangeMerge(
207055 SessionTable *pTab, /* Table structure */
207056 int bRebase, /* True for a rebase hash-table */
207057 int bPatchset, /* True for patchsets */
207058 SessionChange *pExist, /* Existing change */
207059 int op2, /* Second change operation */
207060 int bIndirect, /* True if second change is indirect */
207061 u8 *aRec, /* Second change record */
207062 int nRec, /* Number of bytes in aRec */
207063 SessionChange **ppNew /* OUT: Merged change */
207064){
207065 SessionChange *pNew = 0;
207066 int rc = SQLITE_OK;
207067
207068 if( !pExist ){
207069 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
207070 if( !pNew ){
207071 return SQLITE_NOMEM;
207072 }
207073 memset(pNew, 0, sizeof(SessionChange));
207074 pNew->op = op2;
207075 pNew->bIndirect = bIndirect;
207076 pNew->aRecord = (u8*)&pNew[1];
207077 if( bIndirect==0 || bRebase==0 ){
207078 pNew->nRecord = nRec;
207079 memcpy(pNew->aRecord, aRec, nRec);
207080 }else{
207081 int i;
207082 u8 *pIn = aRec;
207083 u8 *pOut = pNew->aRecord;
207084 for(i=0; i<pTab->nCol; i++){
207085 int nIn = sessionSerialLen(pIn);
207086 if( *pIn==0 ){
207087 *pOut++ = 0;
207088 }else if( pTab->abPK[i]==0 ){
207089 *pOut++ = 0xFF;
207090 }else{
207091 memcpy(pOut, pIn, nIn);
207092 pOut += nIn;
207093 }
207094 pIn += nIn;
207095 }
207096 pNew->nRecord = pOut - pNew->aRecord;
207097 }
207098 }else if( bRebase ){
207099 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
207100 *ppNew = pExist;
207101 }else{
207102 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
207103 pNew = (SessionChange*)sqlite3_malloc64(nByte);
207104 if( pNew==0 ){
207105 rc = SQLITE_NOMEM;
207106 }else{
207107 int i;
207108 u8 *a1 = pExist->aRecord;
207109 u8 *a2 = aRec;
207110 u8 *pOut;
207111
207112 memset(pNew, 0, nByte);
207113 pNew->bIndirect = bIndirect || pExist->bIndirect;
207114 pNew->op = op2;
207115 pOut = pNew->aRecord = (u8*)&pNew[1];
207116
207117 for(i=0; i<pTab->nCol; i++){
207118 int n1 = sessionSerialLen(a1);
207119 int n2 = sessionSerialLen(a2);
207120 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
207121 *pOut++ = 0xFF;
207122 }else if( *a2==0 ){
207123 memcpy(pOut, a1, n1);
207124 pOut += n1;
207125 }else{
207126 memcpy(pOut, a2, n2);
207127 pOut += n2;
207128 }
207129 a1 += n1;
207130 a2 += n2;
207131 }
207132 pNew->nRecord = pOut - pNew->aRecord;
207133 }
207134 sqlite3_free(pExist);
207135 }
207136 }else{
207137 int op1 = pExist->op;
207138
207139 /*
207140 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
207141 ** op1=INSERT, op2=UPDATE -> INSERT.
207142 ** op1=INSERT, op2=DELETE -> (none)
207143 **
207144 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
207145 ** op1=UPDATE, op2=UPDATE -> UPDATE.
207146 ** op1=UPDATE, op2=DELETE -> DELETE.
207147 **
207148 ** op1=DELETE, op2=INSERT -> UPDATE.
207149 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
207150 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
207151 */
207152 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
207153 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
207154 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
207155 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
207156 ){
207157 pNew = pExist;
207158 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
207159 sqlite3_free(pExist);
207160 assert( pNew==0 );
207161 }else{
207162 u8 *aExist = pExist->aRecord;
207163 sqlite3_int64 nByte;
207164 u8 *aCsr;
207165
207166 /* Allocate a new SessionChange object. Ensure that the aRecord[]
207167 ** buffer of the new object is large enough to hold any record that
207168 ** may be generated by combining the input records. */
207169 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
207170 pNew = (SessionChange *)sqlite3_malloc64(nByte);
207171 if( !pNew ){
207172 sqlite3_free(pExist);
207173 return SQLITE_NOMEM;
207174 }
207175 memset(pNew, 0, sizeof(SessionChange));
207176 pNew->bIndirect = (bIndirect && pExist->bIndirect);
207177 aCsr = pNew->aRecord = (u8 *)&pNew[1];
207178
207179 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
207180 u8 *a1 = aRec;
207181 assert( op2==SQLITE_UPDATE );
207182 pNew->op = SQLITE_INSERT;
207183 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
207184 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
207185 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
207186 assert( op2==SQLITE_INSERT );
207187 pNew->op = SQLITE_UPDATE;
207188 if( bPatchset ){
207189 memcpy(aCsr, aRec, nRec);
207190 aCsr += nRec;
207191 }else{
207192 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
207193 sqlite3_free(pNew);
207194 pNew = 0;
207195 }
207196 }
207197 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
207198 u8 *a1 = aExist;
207199 u8 *a2 = aRec;
207200 assert( op1==SQLITE_UPDATE );
207201 if( bPatchset==0 ){
207202 sessionSkipRecord(&a1, pTab->nCol);
207203 sessionSkipRecord(&a2, pTab->nCol);
207204 }
207205 pNew->op = SQLITE_UPDATE;
207206 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
207207 sqlite3_free(pNew);
207208 pNew = 0;
207209 }
207210 }else{ /* UPDATE + DELETE */
207211 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
207212 pNew->op = SQLITE_DELETE;
207213 if( bPatchset ){
207214 memcpy(aCsr, aRec, nRec);
207215 aCsr += nRec;
207216 }else{
207217 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
207218 }
207219 }
207220
207221 if( pNew ){
207222 pNew->nRecord = (int)(aCsr - pNew->aRecord);
207223 }
207224 sqlite3_free(pExist);
207225 }
207226 }
207227
207228 *ppNew = pNew;
207229 return rc;
207230}
207231
207232/*
207233** Add all changes in the changeset traversed by the iterator passed as
207234** the first argument to the changegroup hash tables.
207235*/
207236static int sessionChangesetToHash(
207237 sqlite3_changeset_iter *pIter, /* Iterator to read from */
207238 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
207239 int bRebase /* True if hash table is for rebasing */
207240){
207241 u8 *aRec;
207242 int nRec;
207243 int rc = SQLITE_OK;
207244 SessionTable *pTab = 0;
207245
207246 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
207247 const char *zNew;
207248 int nCol;
207249 int op;
207250 int iHash;
207251 int bIndirect;
207252 SessionChange *pChange;
207253 SessionChange *pExist = 0;
207254 SessionChange **pp;
207255
207256 if( pGrp->pList==0 ){
207257 pGrp->bPatch = pIter->bPatchset;
207258 }else if( pIter->bPatchset!=pGrp->bPatch ){
207259 rc = SQLITE_ERROR;
207260 break;
207261 }
207262
207263 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
207264 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
207265 /* Search the list for a matching table */
207266 int nNew = (int)strlen(zNew);
207267 u8 *abPK;
207268
207269 sqlite3changeset_pk(pIter, &abPK, 0);
207270 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
207271 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
207272 }
207273 if( !pTab ){
207274 SessionTable **ppTab;
207275
207276 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
207277 if( !pTab ){
207278 rc = SQLITE_NOMEM;
207279 break;
207280 }
207281 memset(pTab, 0, sizeof(SessionTable));
207282 pTab->nCol = nCol;
207283 pTab->abPK = (u8*)&pTab[1];
207284 memcpy(pTab->abPK, abPK, nCol);
207285 pTab->zName = (char*)&pTab->abPK[nCol];
207286 memcpy(pTab->zName, zNew, nNew+1);
207287
207288 /* The new object must be linked on to the end of the list, not
207289 ** simply added to the start of it. This is to ensure that the
207290 ** tables within the output of sqlite3changegroup_output() are in
207291 ** the right order. */
207292 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
207293 *ppTab = pTab;
207294 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
207295 rc = SQLITE_SCHEMA;
207296 break;
207297 }
207298 }
207299
207300 if( sessionGrowHash(pIter->bPatchset, pTab) ){
207301 rc = SQLITE_NOMEM;
207302 break;
207303 }
207304 iHash = sessionChangeHash(
207305 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
207306 );
207307
207308 /* Search for existing entry. If found, remove it from the hash table.
207309 ** Code below may link it back in.
207310 */
207311 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
207312 int bPkOnly1 = 0;
207313 int bPkOnly2 = 0;
207314 if( pIter->bPatchset ){
207315 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
207316 bPkOnly2 = op==SQLITE_DELETE;
207317 }
207318 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
207319 pExist = *pp;
207320 *pp = (*pp)->pNext;
207321 pTab->nEntry--;
207322 break;
207323 }
207324 }
207325
207326 rc = sessionChangeMerge(pTab, bRebase,
207327 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
207328 );
207329 if( rc ) break;
207330 if( pChange ){
207331 pChange->pNext = pTab->apChange[iHash];
207332 pTab->apChange[iHash] = pChange;
207333 pTab->nEntry++;
207334 }
207335 }
207336
207337 if( rc==SQLITE_OK ) rc = pIter->rc;
207338 return rc;
207339}
207340
207341/*
207342** Serialize a changeset (or patchset) based on all changesets (or patchsets)
207343** added to the changegroup object passed as the first argument.
207344**
207345** If xOutput is not NULL, then the changeset/patchset is returned to the
207346** user via one or more calls to xOutput, as with the other streaming
207347** interfaces.
207348**
207349** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
207350** buffer containing the output changeset before this function returns. In
207351** this case (*pnOut) is set to the size of the output buffer in bytes. It
207352** is the responsibility of the caller to free the output buffer using
207353** sqlite3_free() when it is no longer required.
207354**
207355** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
207356** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
207357** are both set to 0 before returning.
207358*/
207359static int sessionChangegroupOutput(
207360 sqlite3_changegroup *pGrp,
207361 int (*xOutput)(void *pOut, const void *pData, int nData),
207362 void *pOut,
207363 int *pnOut,
207364 void **ppOut
207365){
207366 int rc = SQLITE_OK;
207367 SessionBuffer buf = {0, 0, 0};
207368 SessionTable *pTab;
207369 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
207370
207371 /* Create the serialized output changeset based on the contents of the
207372 ** hash tables attached to the SessionTable objects in list p->pList.
207373 */
207374 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
207375 int i;
207376 if( pTab->nEntry==0 ) continue;
207377
207378 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
207379 for(i=0; i<pTab->nChange; i++){
207380 SessionChange *p;
207381 for(p=pTab->apChange[i]; p; p=p->pNext){
207382 sessionAppendByte(&buf, p->op, &rc);
207383 sessionAppendByte(&buf, p->bIndirect, &rc);
207384 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
207385 if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
207386 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
207387 buf.nBuf = 0;
207388 }
207389 }
207390 }
207391 }
207392
207393 if( rc==SQLITE_OK ){
207394 if( xOutput ){
207395 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
207396 }else{
207397 *ppOut = buf.aBuf;
207398 *pnOut = buf.nBuf;
207399 buf.aBuf = 0;
207400 }
207401 }
207402 sqlite3_free(buf.aBuf);
207403
207404 return rc;
207405}
207406
207407/*
207408** Allocate a new, empty, sqlite3_changegroup.
207409*/
207410SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
207411 int rc = SQLITE_OK; /* Return code */
207412 sqlite3_changegroup *p; /* New object */
207413 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
207414 if( p==0 ){
207415 rc = SQLITE_NOMEM;
207416 }else{
207417 memset(p, 0, sizeof(sqlite3_changegroup));
207418 }
207419 *pp = p;
207420 return rc;
207421}
207422
207423/*
207424** Add the changeset currently stored in buffer pData, size nData bytes,
207425** to changeset-group p.
207426*/
207427SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
207428 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
207429 int rc; /* Return code */
207430
207431 rc = sqlite3changeset_start(&pIter, nData, pData);
207432 if( rc==SQLITE_OK ){
207433 rc = sessionChangesetToHash(pIter, pGrp, 0);
207434 }
207435 sqlite3changeset_finalize(pIter);
207436 return rc;
207437}
207438
207439/*
207440** Obtain a buffer containing a changeset representing the concatenation
207441** of all changesets added to the group so far.
207442*/
207443SQLITE_API int sqlite3changegroup_output(
207444 sqlite3_changegroup *pGrp,
207445 int *pnData,
207446 void **ppData
207447){
207448 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
207449}
207450
207451/*
207452** Streaming versions of changegroup_add().
207453*/
207454SQLITE_API int sqlite3changegroup_add_strm(
207455 sqlite3_changegroup *pGrp,
207456 int (*xInput)(void *pIn, void *pData, int *pnData),
207457 void *pIn
207458){
207459 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
207460 int rc; /* Return code */
207461
207462 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
207463 if( rc==SQLITE_OK ){
207464 rc = sessionChangesetToHash(pIter, pGrp, 0);
207465 }
207466 sqlite3changeset_finalize(pIter);
207467 return rc;
207468}
207469
207470/*
207471** Streaming versions of changegroup_output().
207472*/
207473SQLITE_API int sqlite3changegroup_output_strm(
207474 sqlite3_changegroup *pGrp,
207475 int (*xOutput)(void *pOut, const void *pData, int nData),
207476 void *pOut
207477){
207478 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
207479}
207480
207481/*
207482** Delete a changegroup object.
207483*/
207484SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
207485 if( pGrp ){
207486 sessionDeleteTable(pGrp->pList);
207487 sqlite3_free(pGrp);
207488 }
207489}
207490
207491/*
207492** Combine two changesets together.
207493*/
207494SQLITE_API int sqlite3changeset_concat(
207495 int nLeft, /* Number of bytes in lhs input */
207496 void *pLeft, /* Lhs input changeset */
207497 int nRight /* Number of bytes in rhs input */,
207498 void *pRight, /* Rhs input changeset */
207499 int *pnOut, /* OUT: Number of bytes in output changeset */
207500 void **ppOut /* OUT: changeset (left <concat> right) */
207501){
207502 sqlite3_changegroup *pGrp;
207503 int rc;
207504
207505 rc = sqlite3changegroup_new(&pGrp);
207506 if( rc==SQLITE_OK ){
207507 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
207508 }
207509 if( rc==SQLITE_OK ){
207510 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
207511 }
207512 if( rc==SQLITE_OK ){
207513 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
207514 }
207515 sqlite3changegroup_delete(pGrp);
207516
207517 return rc;
207518}
207519
207520/*
207521** Streaming version of sqlite3changeset_concat().
207522*/
207523SQLITE_API int sqlite3changeset_concat_strm(
207524 int (*xInputA)(void *pIn, void *pData, int *pnData),
207525 void *pInA,
207526 int (*xInputB)(void *pIn, void *pData, int *pnData),
207527 void *pInB,
207528 int (*xOutput)(void *pOut, const void *pData, int nData),
207529 void *pOut
207530){
207531 sqlite3_changegroup *pGrp;
207532 int rc;
207533
207534 rc = sqlite3changegroup_new(&pGrp);
207535 if( rc==SQLITE_OK ){
207536 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
207537 }
207538 if( rc==SQLITE_OK ){
207539 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
207540 }
207541 if( rc==SQLITE_OK ){
207542 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
207543 }
207544 sqlite3changegroup_delete(pGrp);
207545
207546 return rc;
207547}
207548
207549/*
207550** Changeset rebaser handle.
207551*/
207552struct sqlite3_rebaser {
207553 sqlite3_changegroup grp; /* Hash table */
207554};
207555
207556/*
207557** Buffers a1 and a2 must both contain a sessions module record nCol
207558** fields in size. This function appends an nCol sessions module
207559** record to buffer pBuf that is a copy of a1, except that for
207560** each field that is undefined in a1[], swap in the field from a2[].
207561*/
207562static void sessionAppendRecordMerge(
207563 SessionBuffer *pBuf, /* Buffer to append to */
207564 int nCol, /* Number of columns in each record */
207565 u8 *a1, int n1, /* Record 1 */
207566 u8 *a2, int n2, /* Record 2 */
207567 int *pRc /* IN/OUT: error code */
207568){
207569 sessionBufferGrow(pBuf, n1+n2, pRc);
207570 if( *pRc==SQLITE_OK ){
207571 int i;
207572 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
207573 for(i=0; i<nCol; i++){
207574 int nn1 = sessionSerialLen(a1);
207575 int nn2 = sessionSerialLen(a2);
207576 if( *a1==0 || *a1==0xFF ){
207577 memcpy(pOut, a2, nn2);
207578 pOut += nn2;
207579 }else{
207580 memcpy(pOut, a1, nn1);
207581 pOut += nn1;
207582 }
207583 a1 += nn1;
207584 a2 += nn2;
207585 }
207586
207587 pBuf->nBuf = pOut-pBuf->aBuf;
207588 assert( pBuf->nBuf<=pBuf->nAlloc );
207589 }
207590}
207591
207592/*
207593** This function is called when rebasing a local UPDATE change against one
207594** or more remote UPDATE changes. The aRec/nRec buffer contains the current
207595** old.* and new.* records for the change. The rebase buffer (a single
207596** record) is in aChange/nChange. The rebased change is appended to buffer
207597** pBuf.
207598**
207599** Rebasing the UPDATE involves:
207600**
207601** * Removing any changes to fields for which the corresponding field
207602** in the rebase buffer is set to "replaced" (type 0xFF). If this
207603** means the UPDATE change updates no fields, nothing is appended
207604** to the output buffer.
207605**
207606** * For each field modified by the local change for which the
207607** corresponding field in the rebase buffer is not "undefined" (0x00)
207608** or "replaced" (0xFF), the old.* value is replaced by the value
207609** in the rebase buffer.
207610*/
207611static void sessionAppendPartialUpdate(
207612 SessionBuffer *pBuf, /* Append record here */
207613 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
207614 u8 *aRec, int nRec, /* Local change */
207615 u8 *aChange, int nChange, /* Record to rebase against */
207616 int *pRc /* IN/OUT: Return Code */
207617){
207618 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
207619 if( *pRc==SQLITE_OK ){
207620 int bData = 0;
207621 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
207622 int i;
207623 u8 *a1 = aRec;
207624 u8 *a2 = aChange;
207625
207626 *pOut++ = SQLITE_UPDATE;
207627 *pOut++ = pIter->bIndirect;
207628 for(i=0; i<pIter->nCol; i++){
207629 int n1 = sessionSerialLen(a1);
207630 int n2 = sessionSerialLen(a2);
207631 if( pIter->abPK[i] || a2[0]==0 ){
207632 if( !pIter->abPK[i] ) bData = 1;
207633 memcpy(pOut, a1, n1);
207634 pOut += n1;
207635 }else if( a2[0]!=0xFF ){
207636 bData = 1;
207637 memcpy(pOut, a2, n2);
207638 pOut += n2;
207639 }else{
207640 *pOut++ = '\0';
207641 }
207642 a1 += n1;
207643 a2 += n2;
207644 }
207645 if( bData ){
207646 a2 = aChange;
207647 for(i=0; i<pIter->nCol; i++){
207648 int n1 = sessionSerialLen(a1);
207649 int n2 = sessionSerialLen(a2);
207650 if( pIter->abPK[i] || a2[0]!=0xFF ){
207651 memcpy(pOut, a1, n1);
207652 pOut += n1;
207653 }else{
207654 *pOut++ = '\0';
207655 }
207656 a1 += n1;
207657 a2 += n2;
207658 }
207659 pBuf->nBuf = (pOut - pBuf->aBuf);
207660 }
207661 }
207662}
207663
207664/*
207665** pIter is configured to iterate through a changeset. This function rebases
207666** that changeset according to the current configuration of the rebaser
207667** object passed as the first argument. If no error occurs and argument xOutput
207668** is not NULL, then the changeset is returned to the caller by invoking
207669** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
207670** then (*ppOut) is set to point to a buffer containing the rebased changeset
207671** before this function returns. In this case (*pnOut) is set to the size of
207672** the buffer in bytes. It is the responsibility of the caller to eventually
207673** free the (*ppOut) buffer using sqlite3_free().
207674**
207675** If an error occurs, an SQLite error code is returned. If ppOut and
207676** pnOut are not NULL, then the two output parameters are set to 0 before
207677** returning.
207678*/
207679static int sessionRebase(
207680 sqlite3_rebaser *p, /* Rebaser hash table */
207681 sqlite3_changeset_iter *pIter, /* Input data */
207682 int (*xOutput)(void *pOut, const void *pData, int nData),
207683 void *pOut, /* Context for xOutput callback */
207684 int *pnOut, /* OUT: Number of bytes in output changeset */
207685 void **ppOut /* OUT: Inverse of pChangeset */
207686){
207687 int rc = SQLITE_OK;
207688 u8 *aRec = 0;
207689 int nRec = 0;
207690 int bNew = 0;
207691 SessionTable *pTab = 0;
207692 SessionBuffer sOut = {0,0,0};
207693
207694 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
207695 SessionChange *pChange = 0;
207696 int bDone = 0;
207697
207698 if( bNew ){
207699 const char *zTab = pIter->zTab;
207700 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
207701 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
207702 }
207703 bNew = 0;
207704
207705 /* A patchset may not be rebased */
207706 if( pIter->bPatchset ){
207707 rc = SQLITE_ERROR;
207708 }
207709
207710 /* Append a table header to the output for this new table */
207711 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
207712 sessionAppendVarint(&sOut, pIter->nCol, &rc);
207713 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
207714 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
207715 }
207716
207717 if( pTab && rc==SQLITE_OK ){
207718 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
207719
207720 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
207721 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
207722 break;
207723 }
207724 }
207725 }
207726
207727 if( pChange ){
207728 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
207729 switch( pIter->op ){
207730 case SQLITE_INSERT:
207731 if( pChange->op==SQLITE_INSERT ){
207732 bDone = 1;
207733 if( pChange->bIndirect==0 ){
207734 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
207735 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207736 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
207737 sessionAppendBlob(&sOut, aRec, nRec, &rc);
207738 }
207739 }
207740 break;
207741
207742 case SQLITE_UPDATE:
207743 bDone = 1;
207744 if( pChange->op==SQLITE_DELETE ){
207745 if( pChange->bIndirect==0 ){
207746 u8 *pCsr = aRec;
207747 sessionSkipRecord(&pCsr, pIter->nCol);
207748 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
207749 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207750 sessionAppendRecordMerge(&sOut, pIter->nCol,
207751 pCsr, nRec-(pCsr-aRec),
207752 pChange->aRecord, pChange->nRecord, &rc
207753 );
207754 }
207755 }else{
207756 sessionAppendPartialUpdate(&sOut, pIter,
207757 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
207758 );
207759 }
207760 break;
207761
207762 default:
207763 assert( pIter->op==SQLITE_DELETE );
207764 bDone = 1;
207765 if( pChange->op==SQLITE_INSERT ){
207766 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
207767 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207768 sessionAppendRecordMerge(&sOut, pIter->nCol,
207769 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
207770 );
207771 }
207772 break;
207773 }
207774 }
207775
207776 if( bDone==0 ){
207777 sessionAppendByte(&sOut, pIter->op, &rc);
207778 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
207779 sessionAppendBlob(&sOut, aRec, nRec, &rc);
207780 }
207781 if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
207782 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
207783 sOut.nBuf = 0;
207784 }
207785 if( rc ) break;
207786 }
207787
207788 if( rc!=SQLITE_OK ){
207789 sqlite3_free(sOut.aBuf);
207790 memset(&sOut, 0, sizeof(sOut));
207791 }
207792
207793 if( rc==SQLITE_OK ){
207794 if( xOutput ){
207795 if( sOut.nBuf>0 ){
207796 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
207797 }
207798 }else{
207799 *ppOut = (void*)sOut.aBuf;
207800 *pnOut = sOut.nBuf;
207801 sOut.aBuf = 0;
207802 }
207803 }
207804 sqlite3_free(sOut.aBuf);
207805 return rc;
207806}
207807
207808/*
207809** Create a new rebaser object.
207810*/
207811SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
207812 int rc = SQLITE_OK;
207813 sqlite3_rebaser *pNew;
207814
207815 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
207816 if( pNew==0 ){
207817 rc = SQLITE_NOMEM;
207818 }else{
207819 memset(pNew, 0, sizeof(sqlite3_rebaser));
207820 }
207821 *ppNew = pNew;
207822 return rc;
207823}
207824
207825/*
207826** Call this one or more times to configure a rebaser.
207827*/
207828SQLITE_API int sqlite3rebaser_configure(
207829 sqlite3_rebaser *p,
207830 int nRebase, const void *pRebase
207831){
207832 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
207833 int rc; /* Return code */
207834 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
207835 if( rc==SQLITE_OK ){
207836 rc = sessionChangesetToHash(pIter, &p->grp, 1);
207837 }
207838 sqlite3changeset_finalize(pIter);
207839 return rc;
207840}
207841
207842/*
207843** Rebase a changeset according to current rebaser configuration
207844*/
207845SQLITE_API int sqlite3rebaser_rebase(
207846 sqlite3_rebaser *p,
207847 int nIn, const void *pIn,
207848 int *pnOut, void **ppOut
207849){
207850 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
207851 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
207852
207853 if( rc==SQLITE_OK ){
207854 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
207855 sqlite3changeset_finalize(pIter);
207856 }
207857
207858 return rc;
207859}
207860
207861/*
207862** Rebase a changeset according to current rebaser configuration
207863*/
207864SQLITE_API int sqlite3rebaser_rebase_strm(
207865 sqlite3_rebaser *p,
207866 int (*xInput)(void *pIn, void *pData, int *pnData),
207867 void *pIn,
207868 int (*xOutput)(void *pOut, const void *pData, int nData),
207869 void *pOut
207870){
207871 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
207872 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
207873
207874 if( rc==SQLITE_OK ){
207875 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
207876 sqlite3changeset_finalize(pIter);
207877 }
207878
207879 return rc;
207880}
207881
207882/*
207883** Destroy a rebaser object
207884*/
207885SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
207886 if( p ){
207887 sessionDeleteTable(p->grp.pList);
207888 sqlite3_free(p);
207889 }
207890}
207891
207892/*
207893** Global configuration
207894*/
207895SQLITE_API int sqlite3session_config(int op, void *pArg){
207896 int rc = SQLITE_OK;
207897 switch( op ){
207898 case SQLITE_SESSION_CONFIG_STRMSIZE: {
207899 int *pInt = (int*)pArg;
207900 if( *pInt>0 ){
207901 sessions_strm_chunk_size = *pInt;
207902 }
207903 *pInt = sessions_strm_chunk_size;
207904 break;
207905 }
207906 default:
207907 rc = SQLITE_MISUSE;
207908 break;
207909 }
207910 return rc;
207911}
207912
207913#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
207914
207915/************** End of sqlite3session.c **************************************/
207916/************** Begin file fts5.c ********************************************/
207917
207918
207919#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
207920
207921#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
207922# define NDEBUG 1
207923#endif
207924#if defined(NDEBUG) && defined(SQLITE_DEBUG)
207925# undef NDEBUG
207926#endif
207927
207928/*
207929** 2014 May 31
207930**
207931** The author disclaims copyright to this source code. In place of
207932** a legal notice, here is a blessing:
207933**
207934** May you do good and not evil.
207935** May you find forgiveness for yourself and forgive others.
207936** May you share freely, never taking more than you give.
207937**
207938******************************************************************************
207939**
207940** Interfaces to extend FTS5. Using the interfaces defined in this file,
207941** FTS5 may be extended with:
207942**
207943** * custom tokenizers, and
207944** * custom auxiliary functions.
207945*/
207946
207947
207948#ifndef _FTS5_H
207949#define _FTS5_H
207950
207951/* #include "sqlite3.h" */
207952
207953#if 0
207954extern "C" {
207955#endif
207956
207957/*************************************************************************
207958** CUSTOM AUXILIARY FUNCTIONS
207959**
207960** Virtual table implementations may overload SQL functions by implementing
207961** the sqlite3_module.xFindFunction() method.
207962*/
207963
207964typedef struct Fts5ExtensionApi Fts5ExtensionApi;
207965typedef struct Fts5Context Fts5Context;
207966typedef struct Fts5PhraseIter Fts5PhraseIter;
207967
207968typedef void (*fts5_extension_function)(
207969 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
207970 Fts5Context *pFts, /* First arg to pass to pApi functions */
207971 sqlite3_context *pCtx, /* Context for returning result/error */
207972 int nVal, /* Number of values in apVal[] array */
207973 sqlite3_value **apVal /* Array of trailing arguments */
207974);
207975
207976struct Fts5PhraseIter {
207977 const unsigned char *a;
207978 const unsigned char *b;
207979};
207980
207981/*
207982** EXTENSION API FUNCTIONS
207983**
207984** xUserData(pFts):
207985** Return a copy of the context pointer the extension function was
207986** registered with.
207987**
207988** xColumnTotalSize(pFts, iCol, pnToken):
207989** If parameter iCol is less than zero, set output variable *pnToken
207990** to the total number of tokens in the FTS5 table. Or, if iCol is
207991** non-negative but less than the number of columns in the table, return
207992** the total number of tokens in column iCol, considering all rows in
207993** the FTS5 table.
207994**
207995** If parameter iCol is greater than or equal to the number of columns
207996** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
207997** an OOM condition or IO error), an appropriate SQLite error code is
207998** returned.
207999**
208000** xColumnCount(pFts):
208001** Return the number of columns in the table.
208002**
208003** xColumnSize(pFts, iCol, pnToken):
208004** If parameter iCol is less than zero, set output variable *pnToken
208005** to the total number of tokens in the current row. Or, if iCol is
208006** non-negative but less than the number of columns in the table, set
208007** *pnToken to the number of tokens in column iCol of the current row.
208008**
208009** If parameter iCol is greater than or equal to the number of columns
208010** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
208011** an OOM condition or IO error), an appropriate SQLite error code is
208012** returned.
208013**
208014** This function may be quite inefficient if used with an FTS5 table
208015** created with the "columnsize=0" option.
208016**
208017** xColumnText:
208018** This function attempts to retrieve the text of column iCol of the
208019** current document. If successful, (*pz) is set to point to a buffer
208020** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
208021** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
208022** if an error occurs, an SQLite error code is returned and the final values
208023** of (*pz) and (*pn) are undefined.
208024**
208025** xPhraseCount:
208026** Returns the number of phrases in the current query expression.
208027**
208028** xPhraseSize:
208029** Returns the number of tokens in phrase iPhrase of the query. Phrases
208030** are numbered starting from zero.
208031**
208032** xInstCount:
208033** Set *pnInst to the total number of occurrences of all phrases within
208034** the query within the current row. Return SQLITE_OK if successful, or
208035** an error code (i.e. SQLITE_NOMEM) if an error occurs.
208036**
208037** This API can be quite slow if used with an FTS5 table created with the
208038** "detail=none" or "detail=column" option. If the FTS5 table is created
208039** with either "detail=none" or "detail=column" and "content=" option
208040** (i.e. if it is a contentless table), then this API always returns 0.
208041**
208042** xInst:
208043** Query for the details of phrase match iIdx within the current row.
208044** Phrase matches are numbered starting from zero, so the iIdx argument
208045** should be greater than or equal to zero and smaller than the value
208046** output by xInstCount().
208047**
208048** Usually, output parameter *piPhrase is set to the phrase number, *piCol
208049** to the column in which it occurs and *piOff the token offset of the
208050** first token of the phrase. Returns SQLITE_OK if successful, or an error
208051** code (i.e. SQLITE_NOMEM) if an error occurs.
208052**
208053** This API can be quite slow if used with an FTS5 table created with the
208054** "detail=none" or "detail=column" option.
208055**
208056** xRowid:
208057** Returns the rowid of the current row.
208058**
208059** xTokenize:
208060** Tokenize text using the tokenizer belonging to the FTS5 table.
208061**
208062** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
208063** This API function is used to query the FTS table for phrase iPhrase
208064** of the current query. Specifically, a query equivalent to:
208065**
208066** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
208067**
208068** with $p set to a phrase equivalent to the phrase iPhrase of the
208069** current query is executed. Any column filter that applies to
208070** phrase iPhrase of the current query is included in $p. For each
208071** row visited, the callback function passed as the fourth argument
208072** is invoked. The context and API objects passed to the callback
208073** function may be used to access the properties of each matched row.
208074** Invoking Api.xUserData() returns a copy of the pointer passed as
208075** the third argument to pUserData.
208076**
208077** If the callback function returns any value other than SQLITE_OK, the
208078** query is abandoned and the xQueryPhrase function returns immediately.
208079** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
208080** Otherwise, the error code is propagated upwards.
208081**
208082** If the query runs to completion without incident, SQLITE_OK is returned.
208083** Or, if some error occurs before the query completes or is aborted by
208084** the callback, an SQLite error code is returned.
208085**
208086**
208087** xSetAuxdata(pFts5, pAux, xDelete)
208088**
208089** Save the pointer passed as the second argument as the extension function's
208090** "auxiliary data". The pointer may then be retrieved by the current or any
208091** future invocation of the same fts5 extension function made as part of
208092** the same MATCH query using the xGetAuxdata() API.
208093**
208094** Each extension function is allocated a single auxiliary data slot for
208095** each FTS query (MATCH expression). If the extension function is invoked
208096** more than once for a single FTS query, then all invocations share a
208097** single auxiliary data context.
208098**
208099** If there is already an auxiliary data pointer when this function is
208100** invoked, then it is replaced by the new pointer. If an xDelete callback
208101** was specified along with the original pointer, it is invoked at this
208102** point.
208103**
208104** The xDelete callback, if one is specified, is also invoked on the
208105** auxiliary data pointer after the FTS5 query has finished.
208106**
208107** If an error (e.g. an OOM condition) occurs within this function,
208108** the auxiliary data is set to NULL and an error code returned. If the
208109** xDelete parameter was not NULL, it is invoked on the auxiliary data
208110** pointer before returning.
208111**
208112**
208113** xGetAuxdata(pFts5, bClear)
208114**
208115** Returns the current auxiliary data pointer for the fts5 extension
208116** function. See the xSetAuxdata() method for details.
208117**
208118** If the bClear argument is non-zero, then the auxiliary data is cleared
208119** (set to NULL) before this function returns. In this case the xDelete,
208120** if any, is not invoked.
208121**
208122**
208123** xRowCount(pFts5, pnRow)
208124**
208125** This function is used to retrieve the total number of rows in the table.
208126** In other words, the same value that would be returned by:
208127**
208128** SELECT count(*) FROM ftstable;
208129**
208130** xPhraseFirst()
208131** This function is used, along with type Fts5PhraseIter and the xPhraseNext
208132** method, to iterate through all instances of a single query phrase within
208133** the current row. This is the same information as is accessible via the
208134** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
208135** to use, this API may be faster under some circumstances. To iterate
208136** through instances of phrase iPhrase, use the following code:
208137**
208138** Fts5PhraseIter iter;
208139** int iCol, iOff;
208140** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
208141** iCol>=0;
208142** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
208143** ){
208144** // An instance of phrase iPhrase at offset iOff of column iCol
208145** }
208146**
208147** The Fts5PhraseIter structure is defined above. Applications should not
208148** modify this structure directly - it should only be used as shown above
208149** with the xPhraseFirst() and xPhraseNext() API methods (and by
208150** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
208151**
208152** This API can be quite slow if used with an FTS5 table created with the
208153** "detail=none" or "detail=column" option. If the FTS5 table is created
208154** with either "detail=none" or "detail=column" and "content=" option
208155** (i.e. if it is a contentless table), then this API always iterates
208156** through an empty set (all calls to xPhraseFirst() set iCol to -1).
208157**
208158** xPhraseNext()
208159** See xPhraseFirst above.
208160**
208161** xPhraseFirstColumn()
208162** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
208163** and xPhraseNext() APIs described above. The difference is that instead
208164** of iterating through all instances of a phrase in the current row, these
208165** APIs are used to iterate through the set of columns in the current row
208166** that contain one or more instances of a specified phrase. For example:
208167**
208168** Fts5PhraseIter iter;
208169** int iCol;
208170** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
208171** iCol>=0;
208172** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
208173** ){
208174** // Column iCol contains at least one instance of phrase iPhrase
208175** }
208176**
208177** This API can be quite slow if used with an FTS5 table created with the
208178** "detail=none" option. If the FTS5 table is created with either
208179** "detail=none" "content=" option (i.e. if it is a contentless table),
208180** then this API always iterates through an empty set (all calls to
208181** xPhraseFirstColumn() set iCol to -1).
208182**
208183** The information accessed using this API and its companion
208184** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
208185** (or xInst/xInstCount). The chief advantage of this API is that it is
208186** significantly more efficient than those alternatives when used with
208187** "detail=column" tables.
208188**
208189** xPhraseNextColumn()
208190** See xPhraseFirstColumn above.
208191*/
208192struct Fts5ExtensionApi {
208193 int iVersion; /* Currently always set to 3 */
208194
208195 void *(*xUserData)(Fts5Context*);
208196
208197 int (*xColumnCount)(Fts5Context*);
208198 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
208199 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
208200
208201 int (*xTokenize)(Fts5Context*,
208202 const char *pText, int nText, /* Text to tokenize */
208203 void *pCtx, /* Context passed to xToken() */
208204 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
208205 );
208206
208207 int (*xPhraseCount)(Fts5Context*);
208208 int (*xPhraseSize)(Fts5Context*, int iPhrase);
208209
208210 int (*xInstCount)(Fts5Context*, int *pnInst);
208211 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
208212
208213 sqlite3_int64 (*xRowid)(Fts5Context*);
208214 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
208215 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
208216
208217 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
208218 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
208219 );
208220 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
208221 void *(*xGetAuxdata)(Fts5Context*, int bClear);
208222
208223 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
208224 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
208225
208226 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
208227 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
208228};
208229
208230/*
208231** CUSTOM AUXILIARY FUNCTIONS
208232*************************************************************************/
208233
208234/*************************************************************************
208235** CUSTOM TOKENIZERS
208236**
208237** Applications may also register custom tokenizer types. A tokenizer
208238** is registered by providing fts5 with a populated instance of the
208239** following structure. All structure methods must be defined, setting
208240** any member of the fts5_tokenizer struct to NULL leads to undefined
208241** behaviour. The structure methods are expected to function as follows:
208242**
208243** xCreate:
208244** This function is used to allocate and initialize a tokenizer instance.
208245** A tokenizer instance is required to actually tokenize text.
208246**
208247** The first argument passed to this function is a copy of the (void*)
208248** pointer provided by the application when the fts5_tokenizer object
208249** was registered with FTS5 (the third argument to xCreateTokenizer()).
208250** The second and third arguments are an array of nul-terminated strings
208251** containing the tokenizer arguments, if any, specified following the
208252** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
208253** to create the FTS5 table.
208254**
208255** The final argument is an output variable. If successful, (*ppOut)
208256** should be set to point to the new tokenizer handle and SQLITE_OK
208257** returned. If an error occurs, some value other than SQLITE_OK should
208258** be returned. In this case, fts5 assumes that the final value of *ppOut
208259** is undefined.
208260**
208261** xDelete:
208262** This function is invoked to delete a tokenizer handle previously
208263** allocated using xCreate(). Fts5 guarantees that this function will
208264** be invoked exactly once for each successful call to xCreate().
208265**
208266** xTokenize:
208267** This function is expected to tokenize the nText byte string indicated
208268** by argument pText. pText may or may not be nul-terminated. The first
208269** argument passed to this function is a pointer to an Fts5Tokenizer object
208270** returned by an earlier call to xCreate().
208271**
208272** The second argument indicates the reason that FTS5 is requesting
208273** tokenization of the supplied text. This is always one of the following
208274** four values:
208275**
208276** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
208277** or removed from the FTS table. The tokenizer is being invoked to
208278** determine the set of tokens to add to (or delete from) the
208279** FTS index.
208280**
208281** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
208282** against the FTS index. The tokenizer is being called to tokenize
208283** a bareword or quoted string specified as part of the query.
208284**
208285** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
208286** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
208287** followed by a "*" character, indicating that the last token
208288** returned by the tokenizer will be treated as a token prefix.
208289**
208290** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
208291** satisfy an fts5_api.xTokenize() request made by an auxiliary
208292** function. Or an fts5_api.xColumnSize() request made by the same
208293** on a columnsize=0 database.
208294** </ul>
208295**
208296** For each token in the input string, the supplied callback xToken() must
208297** be invoked. The first argument to it should be a copy of the pointer
208298** passed as the second argument to xTokenize(). The third and fourth
208299** arguments are a pointer to a buffer containing the token text, and the
208300** size of the token in bytes. The 4th and 5th arguments are the byte offsets
208301** of the first byte of and first byte immediately following the text from
208302** which the token is derived within the input.
208303**
208304** The second argument passed to the xToken() callback ("tflags") should
208305** normally be set to 0. The exception is if the tokenizer supports
208306** synonyms. In this case see the discussion below for details.
208307**
208308** FTS5 assumes the xToken() callback is invoked for each token in the
208309** order that they occur within the input text.
208310**
208311** If an xToken() callback returns any value other than SQLITE_OK, then
208312** the tokenization should be abandoned and the xTokenize() method should
208313** immediately return a copy of the xToken() return value. Or, if the
208314** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
208315** if an error occurs with the xTokenize() implementation itself, it
208316** may abandon the tokenization and return any error code other than
208317** SQLITE_OK or SQLITE_DONE.
208318**
208319** SYNONYM SUPPORT
208320**
208321** Custom tokenizers may also support synonyms. Consider a case in which a
208322** user wishes to query for a phrase such as "first place". Using the
208323** built-in tokenizers, the FTS5 query 'first + place' will match instances
208324** of "first place" within the document set, but not alternative forms
208325** such as "1st place". In some applications, it would be better to match
208326** all instances of "first place" or "1st place" regardless of which form
208327** the user specified in the MATCH query text.
208328**
208329** There are several ways to approach this in FTS5:
208330**
208331** <ol><li> By mapping all synonyms to a single token. In this case, using
208332** the above example, this means that the tokenizer returns the
208333** same token for inputs "first" and "1st". Say that token is in
208334** fact "first", so that when the user inserts the document "I won
208335** 1st place" entries are added to the index for tokens "i", "won",
208336** "first" and "place". If the user then queries for '1st + place',
208337** the tokenizer substitutes "first" for "1st" and the query works
208338** as expected.
208339**
208340** <li> By querying the index for all synonyms of each query term
208341** separately. In this case, when tokenizing query text, the
208342** tokenizer may provide multiple synonyms for a single term
208343** within the document. FTS5 then queries the index for each
208344** synonym individually. For example, faced with the query:
208345**
208346** <codeblock>
208347** ... MATCH 'first place'</codeblock>
208348**
208349** the tokenizer offers both "1st" and "first" as synonyms for the
208350** first token in the MATCH query and FTS5 effectively runs a query
208351** similar to:
208352**
208353** <codeblock>
208354** ... MATCH '(first OR 1st) place'</codeblock>
208355**
208356** except that, for the purposes of auxiliary functions, the query
208357** still appears to contain just two phrases - "(first OR 1st)"
208358** being treated as a single phrase.
208359**
208360** <li> By adding multiple synonyms for a single term to the FTS index.
208361** Using this method, when tokenizing document text, the tokenizer
208362** provides multiple synonyms for each token. So that when a
208363** document such as "I won first place" is tokenized, entries are
208364** added to the FTS index for "i", "won", "first", "1st" and
208365** "place".
208366**
208367** This way, even if the tokenizer does not provide synonyms
208368** when tokenizing query text (it should not - to do so would be
208369** inefficient), it doesn't matter if the user queries for
208370** 'first + place' or '1st + place', as there are entries in the
208371** FTS index corresponding to both forms of the first token.
208372** </ol>
208373**
208374** Whether it is parsing document or query text, any call to xToken that
208375** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
208376** is considered to supply a synonym for the previous token. For example,
208377** when parsing the document "I won first place", a tokenizer that supports
208378** synonyms would call xToken() 5 times, as follows:
208379**
208380** <codeblock>
208381** xToken(pCtx, 0, "i", 1, 0, 1);
208382** xToken(pCtx, 0, "won", 3, 2, 5);
208383** xToken(pCtx, 0, "first", 5, 6, 11);
208384** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
208385** xToken(pCtx, 0, "place", 5, 12, 17);
208386**</codeblock>
208387**
208388** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
208389** xToken() is called. Multiple synonyms may be specified for a single token
208390** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
208391** There is no limit to the number of synonyms that may be provided for a
208392** single token.
208393**
208394** In many cases, method (1) above is the best approach. It does not add
208395** extra data to the FTS index or require FTS5 to query for multiple terms,
208396** so it is efficient in terms of disk space and query speed. However, it
208397** does not support prefix queries very well. If, as suggested above, the
208398** token "first" is substituted for "1st" by the tokenizer, then the query:
208399**
208400** <codeblock>
208401** ... MATCH '1s*'</codeblock>
208402**
208403** will not match documents that contain the token "1st" (as the tokenizer
208404** will probably not map "1s" to any prefix of "first").
208405**
208406** For full prefix support, method (3) may be preferred. In this case,
208407** because the index contains entries for both "first" and "1st", prefix
208408** queries such as 'fi*' or '1s*' will match correctly. However, because
208409** extra entries are added to the FTS index, this method uses more space
208410** within the database.
208411**
208412** Method (2) offers a midpoint between (1) and (3). Using this method,
208413** a query such as '1s*' will match documents that contain the literal
208414** token "1st", but not "first" (assuming the tokenizer is not able to
208415** provide synonyms for prefixes). However, a non-prefix query like '1st'
208416** will match against "1st" and "first". This method does not require
208417** extra disk space, as no extra entries are added to the FTS index.
208418** On the other hand, it may require more CPU cycles to run MATCH queries,
208419** as separate queries of the FTS index are required for each synonym.
208420**
208421** When using methods (2) or (3), it is important that the tokenizer only
208422** provide synonyms when tokenizing document text (method (2)) or query
208423** text (method (3)), not both. Doing so will not cause any errors, but is
208424** inefficient.
208425*/
208426typedef struct Fts5Tokenizer Fts5Tokenizer;
208427typedef struct fts5_tokenizer fts5_tokenizer;
208428struct fts5_tokenizer {
208429 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
208430 void (*xDelete)(Fts5Tokenizer*);
208431 int (*xTokenize)(Fts5Tokenizer*,
208432 void *pCtx,
208433 int flags, /* Mask of FTS5_TOKENIZE_* flags */
208434 const char *pText, int nText,
208435 int (*xToken)(
208436 void *pCtx, /* Copy of 2nd argument to xTokenize() */
208437 int tflags, /* Mask of FTS5_TOKEN_* flags */
208438 const char *pToken, /* Pointer to buffer containing token */
208439 int nToken, /* Size of token in bytes */
208440 int iStart, /* Byte offset of token within input text */
208441 int iEnd /* Byte offset of end of token within input text */
208442 )
208443 );
208444};
208445
208446/* Flags that may be passed as the third argument to xTokenize() */
208447#define FTS5_TOKENIZE_QUERY 0x0001
208448#define FTS5_TOKENIZE_PREFIX 0x0002
208449#define FTS5_TOKENIZE_DOCUMENT 0x0004
208450#define FTS5_TOKENIZE_AUX 0x0008
208451
208452/* Flags that may be passed by the tokenizer implementation back to FTS5
208453** as the third argument to the supplied xToken callback. */
208454#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
208455
208456/*
208457** END OF CUSTOM TOKENIZERS
208458*************************************************************************/
208459
208460/*************************************************************************
208461** FTS5 EXTENSION REGISTRATION API
208462*/
208463typedef struct fts5_api fts5_api;
208464struct fts5_api {
208465 int iVersion; /* Currently always set to 2 */
208466
208467 /* Create a new tokenizer */
208468 int (*xCreateTokenizer)(
208469 fts5_api *pApi,
208470 const char *zName,
208471 void *pContext,
208472 fts5_tokenizer *pTokenizer,
208473 void (*xDestroy)(void*)
208474 );
208475
208476 /* Find an existing tokenizer */
208477 int (*xFindTokenizer)(
208478 fts5_api *pApi,
208479 const char *zName,
208480 void **ppContext,
208481 fts5_tokenizer *pTokenizer
208482 );
208483
208484 /* Create a new auxiliary function */
208485 int (*xCreateFunction)(
208486 fts5_api *pApi,
208487 const char *zName,
208488 void *pContext,
208489 fts5_extension_function xFunction,
208490 void (*xDestroy)(void*)
208491 );
208492};
208493
208494/*
208495** END OF REGISTRATION API
208496*************************************************************************/
208497
208498#if 0
208499} /* end of the 'extern "C"' block */
208500#endif
208501
208502#endif /* _FTS5_H */
208503
208504/*
208505** 2014 May 31
208506**
208507** The author disclaims copyright to this source code. In place of
208508** a legal notice, here is a blessing:
208509**
208510** May you do good and not evil.
208511** May you find forgiveness for yourself and forgive others.
208512** May you share freely, never taking more than you give.
208513**
208514******************************************************************************
208515**
208516*/
208517#ifndef _FTS5INT_H
208518#define _FTS5INT_H
208519
208520/* #include "fts5.h" */
208521/* #include "sqlite3ext.h" */
208522SQLITE_EXTENSION_INIT1
208523
208524/* #include <string.h> */
208525/* #include <assert.h> */
208526
208527#ifndef SQLITE_AMALGAMATION
208528
208529typedef unsigned char u8;
208530typedef unsigned int u32;
208531typedef unsigned short u16;
208532typedef short i16;
208533typedef sqlite3_int64 i64;
208534typedef sqlite3_uint64 u64;
208535
208536#ifndef ArraySize
208537# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
208538#endif
208539
208540#define testcase(x)
208541#define ALWAYS(x) 1
208542#define NEVER(x) 0
208543
208544#define MIN(x,y) (((x) < (y)) ? (x) : (y))
208545#define MAX(x,y) (((x) > (y)) ? (x) : (y))
208546
208547/*
208548** Constants for the largest and smallest possible 64-bit signed integers.
208549*/
208550# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
208551# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
208552
208553#endif
208554
208555/* Truncate very long tokens to this many bytes. Hard limit is
208556** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
208557** field that occurs at the start of each leaf page (see fts5_index.c). */
208558#define FTS5_MAX_TOKEN_SIZE 32768
208559
208560/*
208561** Maximum number of prefix indexes on single FTS5 table. This must be
208562** less than 32. If it is set to anything large than that, an #error
208563** directive in fts5_index.c will cause the build to fail.
208564*/
208565#define FTS5_MAX_PREFIX_INDEXES 31
208566
208567/*
208568** Maximum segments permitted in a single index
208569*/
208570#define FTS5_MAX_SEGMENT 2000
208571
208572#define FTS5_DEFAULT_NEARDIST 10
208573#define FTS5_DEFAULT_RANK "bm25"
208574
208575/* Name of rank and rowid columns */
208576#define FTS5_RANK_NAME "rank"
208577#define FTS5_ROWID_NAME "rowid"
208578
208579#ifdef SQLITE_DEBUG
208580# define FTS5_CORRUPT sqlite3Fts5Corrupt()
208581static int sqlite3Fts5Corrupt(void);
208582#else
208583# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
208584#endif
208585
208586/*
208587** The assert_nc() macro is similar to the assert() macro, except that it
208588** is used for assert() conditions that are true only if it can be
208589** guranteed that the database is not corrupt.
208590*/
208591#ifdef SQLITE_DEBUG
208592SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
208593# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
208594#else
208595# define assert_nc(x) assert(x)
208596#endif
208597
208598/*
208599** A version of memcmp() that does not cause asan errors if one of the pointer
208600** parameters is NULL and the number of bytes to compare is zero.
208601*/
208602#define fts5Memcmp(s1, s2, n) ((n)==0 ? 0 : memcmp((s1), (s2), (n)))
208603
208604/* Mark a function parameter as unused, to suppress nuisance compiler
208605** warnings. */
208606#ifndef UNUSED_PARAM
208607# define UNUSED_PARAM(X) (void)(X)
208608#endif
208609
208610#ifndef UNUSED_PARAM2
208611# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
208612#endif
208613
208614typedef struct Fts5Global Fts5Global;
208615typedef struct Fts5Colset Fts5Colset;
208616
208617/* If a NEAR() clump or phrase may only match a specific set of columns,
208618** then an object of the following type is used to record the set of columns.
208619** Each entry in the aiCol[] array is a column that may be matched.
208620**
208621** This object is used by fts5_expr.c and fts5_index.c.
208622*/
208623struct Fts5Colset {
208624 int nCol;
208625 int aiCol[1];
208626};
208627
208628
208629
208630/**************************************************************************
208631** Interface to code in fts5_config.c. fts5_config.c contains contains code
208632** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
208633*/
208634
208635typedef struct Fts5Config Fts5Config;
208636
208637/*
208638** An instance of the following structure encodes all information that can
208639** be gleaned from the CREATE VIRTUAL TABLE statement.
208640**
208641** And all information loaded from the %_config table.
208642**
208643** nAutomerge:
208644** The minimum number of segments that an auto-merge operation should
208645** attempt to merge together. A value of 1 sets the object to use the
208646** compile time default. Zero disables auto-merge altogether.
208647**
208648** zContent:
208649**
208650** zContentRowid:
208651** The value of the content_rowid= option, if one was specified. Or
208652** the string "rowid" otherwise. This text is not quoted - if it is
208653** used as part of an SQL statement it needs to be quoted appropriately.
208654**
208655** zContentExprlist:
208656**
208657** pzErrmsg:
208658** This exists in order to allow the fts5_index.c module to return a
208659** decent error message if it encounters a file-format version it does
208660** not understand.
208661**
208662** bColumnsize:
208663** True if the %_docsize table is created.
208664**
208665** bPrefixIndex:
208666** This is only used for debugging. If set to false, any prefix indexes
208667** are ignored. This value is configured using:
208668**
208669** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
208670**
208671*/
208672struct Fts5Config {
208673 sqlite3 *db; /* Database handle */
208674 char *zDb; /* Database holding FTS index (e.g. "main") */
208675 char *zName; /* Name of FTS index */
208676 int nCol; /* Number of columns */
208677 char **azCol; /* Column names */
208678 u8 *abUnindexed; /* True for unindexed columns */
208679 int nPrefix; /* Number of prefix indexes */
208680 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
208681 int eContent; /* An FTS5_CONTENT value */
208682 char *zContent; /* content table */
208683 char *zContentRowid; /* "content_rowid=" option value */
208684 int bColumnsize; /* "columnsize=" option value (dflt==1) */
208685 int eDetail; /* FTS5_DETAIL_XXX value */
208686 char *zContentExprlist;
208687 Fts5Tokenizer *pTok;
208688 fts5_tokenizer *pTokApi;
208689 int bLock; /* True when table is preparing statement */
208690
208691 /* Values loaded from the %_config table */
208692 int iCookie; /* Incremented when %_config is modified */
208693 int pgsz; /* Approximate page size used in %_data */
208694 int nAutomerge; /* 'automerge' setting */
208695 int nCrisisMerge; /* Maximum allowed segments per level */
208696 int nUsermerge; /* 'usermerge' setting */
208697 int nHashSize; /* Bytes of memory for in-memory hash */
208698 char *zRank; /* Name of rank function */
208699 char *zRankArgs; /* Arguments to rank function */
208700
208701 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
208702 char **pzErrmsg;
208703
208704#ifdef SQLITE_DEBUG
208705 int bPrefixIndex; /* True to use prefix-indexes */
208706#endif
208707};
208708
208709/* Current expected value of %_config table 'version' field */
208710#define FTS5_CURRENT_VERSION 4
208711
208712#define FTS5_CONTENT_NORMAL 0
208713#define FTS5_CONTENT_NONE 1
208714#define FTS5_CONTENT_EXTERNAL 2
208715
208716#define FTS5_DETAIL_FULL 0
208717#define FTS5_DETAIL_NONE 1
208718#define FTS5_DETAIL_COLUMNS 2
208719
208720
208721
208722static int sqlite3Fts5ConfigParse(
208723 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
208724);
208725static void sqlite3Fts5ConfigFree(Fts5Config*);
208726
208727static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
208728
208729static int sqlite3Fts5Tokenize(
208730 Fts5Config *pConfig, /* FTS5 Configuration object */
208731 int flags, /* FTS5_TOKENIZE_* flags */
208732 const char *pText, int nText, /* Text to tokenize */
208733 void *pCtx, /* Context passed to xToken() */
208734 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
208735);
208736
208737static void sqlite3Fts5Dequote(char *z);
208738
208739/* Load the contents of the %_config table */
208740static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
208741
208742/* Set the value of a single config attribute */
208743static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
208744
208745static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
208746
208747/*
208748** End of interface to code in fts5_config.c.
208749**************************************************************************/
208750
208751/**************************************************************************
208752** Interface to code in fts5_buffer.c.
208753*/
208754
208755/*
208756** Buffer object for the incremental building of string data.
208757*/
208758typedef struct Fts5Buffer Fts5Buffer;
208759struct Fts5Buffer {
208760 u8 *p;
208761 int n;
208762 int nSpace;
208763};
208764
208765static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
208766static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
208767static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
208768static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
208769static void sqlite3Fts5BufferFree(Fts5Buffer*);
208770static void sqlite3Fts5BufferZero(Fts5Buffer*);
208771static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
208772static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
208773
208774static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
208775
208776#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
208777#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
208778#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
208779#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
208780#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
208781
208782#define fts5BufferGrow(pRc,pBuf,nn) ( \
208783 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
208784 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
208785)
208786
208787/* Write and decode big-endian 32-bit integer values */
208788static void sqlite3Fts5Put32(u8*, int);
208789static int sqlite3Fts5Get32(const u8*);
208790
208791#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
208792#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
208793
208794typedef struct Fts5PoslistReader Fts5PoslistReader;
208795struct Fts5PoslistReader {
208796 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
208797 const u8 *a; /* Position list to iterate through */
208798 int n; /* Size of buffer at a[] in bytes */
208799 int i; /* Current offset in a[] */
208800
208801 u8 bFlag; /* For client use (any custom purpose) */
208802
208803 /* Output variables */
208804 u8 bEof; /* Set to true at EOF */
208805 i64 iPos; /* (iCol<<32) + iPos */
208806};
208807static int sqlite3Fts5PoslistReaderInit(
208808 const u8 *a, int n, /* Poslist buffer to iterate through */
208809 Fts5PoslistReader *pIter /* Iterator object to initialize */
208810);
208811static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
208812
208813typedef struct Fts5PoslistWriter Fts5PoslistWriter;
208814struct Fts5PoslistWriter {
208815 i64 iPrev;
208816};
208817static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
208818static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
208819
208820static int sqlite3Fts5PoslistNext64(
208821 const u8 *a, int n, /* Buffer containing poslist */
208822 int *pi, /* IN/OUT: Offset within a[] */
208823 i64 *piOff /* IN/OUT: Current offset */
208824);
208825
208826/* Malloc utility */
208827static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
208828static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
208829
208830/* Character set tests (like isspace(), isalpha() etc.) */
208831static int sqlite3Fts5IsBareword(char t);
208832
208833
208834/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
208835typedef struct Fts5Termset Fts5Termset;
208836static int sqlite3Fts5TermsetNew(Fts5Termset**);
208837static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
208838static void sqlite3Fts5TermsetFree(Fts5Termset*);
208839
208840/*
208841** End of interface to code in fts5_buffer.c.
208842**************************************************************************/
208843
208844/**************************************************************************
208845** Interface to code in fts5_index.c. fts5_index.c contains contains code
208846** to access the data stored in the %_data table.
208847*/
208848
208849typedef struct Fts5Index Fts5Index;
208850typedef struct Fts5IndexIter Fts5IndexIter;
208851
208852struct Fts5IndexIter {
208853 i64 iRowid;
208854 const u8 *pData;
208855 int nData;
208856 u8 bEof;
208857};
208858
208859#define sqlite3Fts5IterEof(x) ((x)->bEof)
208860
208861/*
208862** Values used as part of the flags argument passed to IndexQuery().
208863*/
208864#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
208865#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
208866#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
208867#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
208868
208869/* The following are used internally by the fts5_index.c module. They are
208870** defined here only to make it easier to avoid clashes with the flags
208871** above. */
208872#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
208873#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
208874
208875/*
208876** Create/destroy an Fts5Index object.
208877*/
208878static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
208879static int sqlite3Fts5IndexClose(Fts5Index *p);
208880
208881/*
208882** Return a simple checksum value based on the arguments.
208883*/
208884static u64 sqlite3Fts5IndexEntryCksum(
208885 i64 iRowid,
208886 int iCol,
208887 int iPos,
208888 int iIdx,
208889 const char *pTerm,
208890 int nTerm
208891);
208892
208893/*
208894** Argument p points to a buffer containing utf-8 text that is n bytes in
208895** size. Return the number of bytes in the nChar character prefix of the
208896** buffer, or 0 if there are less than nChar characters in total.
208897*/
208898static int sqlite3Fts5IndexCharlenToBytelen(
208899 const char *p,
208900 int nByte,
208901 int nChar
208902);
208903
208904/*
208905** Open a new iterator to iterate though all rowids that match the
208906** specified token or token prefix.
208907*/
208908static int sqlite3Fts5IndexQuery(
208909 Fts5Index *p, /* FTS index to query */
208910 const char *pToken, int nToken, /* Token (or prefix) to query for */
208911 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
208912 Fts5Colset *pColset, /* Match these columns only */
208913 Fts5IndexIter **ppIter /* OUT: New iterator object */
208914);
208915
208916/*
208917** The various operations on open token or token prefix iterators opened
208918** using sqlite3Fts5IndexQuery().
208919*/
208920static int sqlite3Fts5IterNext(Fts5IndexIter*);
208921static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
208922
208923/*
208924** Close an iterator opened by sqlite3Fts5IndexQuery().
208925*/
208926static void sqlite3Fts5IterClose(Fts5IndexIter*);
208927
208928/*
208929** Close the reader blob handle, if it is open.
208930*/
208931static void sqlite3Fts5IndexCloseReader(Fts5Index*);
208932
208933/*
208934** This interface is used by the fts5vocab module.
208935*/
208936static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
208937static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
208938
208939
208940/*
208941** Insert or remove data to or from the index. Each time a document is
208942** added to or removed from the index, this function is called one or more
208943** times.
208944**
208945** For an insert, it must be called once for each token in the new document.
208946** If the operation is a delete, it must be called (at least) once for each
208947** unique token in the document with an iCol value less than zero. The iPos
208948** argument is ignored for a delete.
208949*/
208950static int sqlite3Fts5IndexWrite(
208951 Fts5Index *p, /* Index to write to */
208952 int iCol, /* Column token appears in (-ve -> delete) */
208953 int iPos, /* Position of token within column */
208954 const char *pToken, int nToken /* Token to add or remove to or from index */
208955);
208956
208957/*
208958** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
208959** document iDocid.
208960*/
208961static int sqlite3Fts5IndexBeginWrite(
208962 Fts5Index *p, /* Index to write to */
208963 int bDelete, /* True if current operation is a delete */
208964 i64 iDocid /* Docid to add or remove data from */
208965);
208966
208967/*
208968** Flush any data stored in the in-memory hash tables to the database.
208969** Also close any open blob handles.
208970*/
208971static int sqlite3Fts5IndexSync(Fts5Index *p);
208972
208973/*
208974** Discard any data stored in the in-memory hash tables. Do not write it
208975** to the database. Additionally, assume that the contents of the %_data
208976** table may have changed on disk. So any in-memory caches of %_data
208977** records must be invalidated.
208978*/
208979static int sqlite3Fts5IndexRollback(Fts5Index *p);
208980
208981/*
208982** Get or set the "averages" values.
208983*/
208984static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
208985static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
208986
208987/*
208988** Functions called by the storage module as part of integrity-check.
208989*/
208990static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
208991
208992/*
208993** Called during virtual module initialization to register UDF
208994** fts5_decode() with SQLite
208995*/
208996static int sqlite3Fts5IndexInit(sqlite3*);
208997
208998static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
208999
209000/*
209001** Return the total number of entries read from the %_data table by
209002** this connection since it was created.
209003*/
209004static int sqlite3Fts5IndexReads(Fts5Index *p);
209005
209006static int sqlite3Fts5IndexReinit(Fts5Index *p);
209007static int sqlite3Fts5IndexOptimize(Fts5Index *p);
209008static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
209009static int sqlite3Fts5IndexReset(Fts5Index *p);
209010
209011static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
209012
209013/*
209014** End of interface to code in fts5_index.c.
209015**************************************************************************/
209016
209017/**************************************************************************
209018** Interface to code in fts5_varint.c.
209019*/
209020static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
209021static int sqlite3Fts5GetVarintLen(u32 iVal);
209022static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
209023static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
209024
209025#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
209026#define fts5GetVarint sqlite3Fts5GetVarint
209027
209028#define fts5FastGetVarint32(a, iOff, nVal) { \
209029 nVal = (a)[iOff++]; \
209030 if( nVal & 0x80 ){ \
209031 iOff--; \
209032 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
209033 } \
209034}
209035
209036
209037/*
209038** End of interface to code in fts5_varint.c.
209039**************************************************************************/
209040
209041
209042/**************************************************************************
209043** Interface to code in fts5_main.c.
209044*/
209045
209046/*
209047** Virtual-table object.
209048*/
209049typedef struct Fts5Table Fts5Table;
209050struct Fts5Table {
209051 sqlite3_vtab base; /* Base class used by SQLite core */
209052 Fts5Config *pConfig; /* Virtual table configuration */
209053 Fts5Index *pIndex; /* Full-text index */
209054};
209055
209056static int sqlite3Fts5GetTokenizer(
209057 Fts5Global*,
209058 const char **azArg,
209059 int nArg,
209060 Fts5Tokenizer**,
209061 fts5_tokenizer**,
209062 char **pzErr
209063);
209064
209065static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
209066
209067static int sqlite3Fts5FlushToDisk(Fts5Table*);
209068
209069/*
209070** End of interface to code in fts5.c.
209071**************************************************************************/
209072
209073/**************************************************************************
209074** Interface to code in fts5_hash.c.
209075*/
209076typedef struct Fts5Hash Fts5Hash;
209077
209078/*
209079** Create a hash table, free a hash table.
209080*/
209081static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
209082static void sqlite3Fts5HashFree(Fts5Hash*);
209083
209084static int sqlite3Fts5HashWrite(
209085 Fts5Hash*,
209086 i64 iRowid, /* Rowid for this entry */
209087 int iCol, /* Column token appears in (-ve -> delete) */
209088 int iPos, /* Position of token within column */
209089 char bByte,
209090 const char *pToken, int nToken /* Token to add or remove to or from index */
209091);
209092
209093/*
209094** Empty (but do not delete) a hash table.
209095*/
209096static void sqlite3Fts5HashClear(Fts5Hash*);
209097
209098static int sqlite3Fts5HashQuery(
209099 Fts5Hash*, /* Hash table to query */
209100 int nPre,
209101 const char *pTerm, int nTerm, /* Query term */
209102 void **ppObj, /* OUT: Pointer to doclist for pTerm */
209103 int *pnDoclist /* OUT: Size of doclist in bytes */
209104);
209105
209106static int sqlite3Fts5HashScanInit(
209107 Fts5Hash*, /* Hash table to query */
209108 const char *pTerm, int nTerm /* Query prefix */
209109);
209110static void sqlite3Fts5HashScanNext(Fts5Hash*);
209111static int sqlite3Fts5HashScanEof(Fts5Hash*);
209112static void sqlite3Fts5HashScanEntry(Fts5Hash *,
209113 const char **pzTerm, /* OUT: term (nul-terminated) */
209114 const u8 **ppDoclist, /* OUT: pointer to doclist */
209115 int *pnDoclist /* OUT: size of doclist in bytes */
209116);
209117
209118
209119/*
209120** End of interface to code in fts5_hash.c.
209121**************************************************************************/
209122
209123/**************************************************************************
209124** Interface to code in fts5_storage.c. fts5_storage.c contains contains
209125** code to access the data stored in the %_content and %_docsize tables.
209126*/
209127
209128#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
209129#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
209130#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
209131
209132typedef struct Fts5Storage Fts5Storage;
209133
209134static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
209135static int sqlite3Fts5StorageClose(Fts5Storage *p);
209136static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
209137
209138static int sqlite3Fts5DropAll(Fts5Config*);
209139static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
209140
209141static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
209142static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
209143static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
209144
209145static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
209146
209147static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
209148static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
209149
209150static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
209151static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
209152static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
209153
209154static int sqlite3Fts5StorageSync(Fts5Storage *p);
209155static int sqlite3Fts5StorageRollback(Fts5Storage *p);
209156
209157static int sqlite3Fts5StorageConfigValue(
209158 Fts5Storage *p, const char*, sqlite3_value*, int
209159);
209160
209161static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
209162static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
209163static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
209164static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
209165static int sqlite3Fts5StorageReset(Fts5Storage *p);
209166
209167/*
209168** End of interface to code in fts5_storage.c.
209169**************************************************************************/
209170
209171
209172/**************************************************************************
209173** Interface to code in fts5_expr.c.
209174*/
209175typedef struct Fts5Expr Fts5Expr;
209176typedef struct Fts5ExprNode Fts5ExprNode;
209177typedef struct Fts5Parse Fts5Parse;
209178typedef struct Fts5Token Fts5Token;
209179typedef struct Fts5ExprPhrase Fts5ExprPhrase;
209180typedef struct Fts5ExprNearset Fts5ExprNearset;
209181
209182struct Fts5Token {
209183 const char *p; /* Token text (not NULL terminated) */
209184 int n; /* Size of buffer p in bytes */
209185};
209186
209187/* Parse a MATCH expression. */
209188static int sqlite3Fts5ExprNew(
209189 Fts5Config *pConfig,
209190 int iCol, /* Column on LHS of MATCH operator */
209191 const char *zExpr,
209192 Fts5Expr **ppNew,
209193 char **pzErr
209194);
209195
209196/*
209197** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
209198** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
209199** rc = sqlite3Fts5ExprNext(pExpr)
209200** ){
209201** // The document with rowid iRowid matches the expression!
209202** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
209203** }
209204*/
209205static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
209206static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
209207static int sqlite3Fts5ExprEof(Fts5Expr*);
209208static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
209209
209210static void sqlite3Fts5ExprFree(Fts5Expr*);
209211static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
209212
209213/* Called during startup to register a UDF with SQLite */
209214static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
209215
209216static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
209217static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
209218static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
209219
209220typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
209221static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
209222static int sqlite3Fts5ExprPopulatePoslists(
209223 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
209224);
209225static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
209226
209227static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
209228
209229static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
209230
209231/*******************************************
209232** The fts5_expr.c API above this point is used by the other hand-written
209233** C code in this module. The interfaces below this point are called by
209234** the parser code in fts5parse.y. */
209235
209236static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
209237
209238static Fts5ExprNode *sqlite3Fts5ParseNode(
209239 Fts5Parse *pParse,
209240 int eType,
209241 Fts5ExprNode *pLeft,
209242 Fts5ExprNode *pRight,
209243 Fts5ExprNearset *pNear
209244);
209245
209246static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
209247 Fts5Parse *pParse,
209248 Fts5ExprNode *pLeft,
209249 Fts5ExprNode *pRight
209250);
209251
209252static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
209253 Fts5Parse *pParse,
209254 Fts5ExprPhrase *pPhrase,
209255 Fts5Token *pToken,
209256 int bPrefix
209257);
209258
209259static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
209260
209261static Fts5ExprNearset *sqlite3Fts5ParseNearset(
209262 Fts5Parse*,
209263 Fts5ExprNearset*,
209264 Fts5ExprPhrase*
209265);
209266
209267static Fts5Colset *sqlite3Fts5ParseColset(
209268 Fts5Parse*,
209269 Fts5Colset*,
209270 Fts5Token *
209271);
209272
209273static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
209274static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
209275static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
209276
209277static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
209278static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
209279static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
209280static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
209281static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
209282
209283/*
209284** End of interface to code in fts5_expr.c.
209285**************************************************************************/
209286
209287
209288
209289/**************************************************************************
209290** Interface to code in fts5_aux.c.
209291*/
209292
209293static int sqlite3Fts5AuxInit(fts5_api*);
209294/*
209295** End of interface to code in fts5_aux.c.
209296**************************************************************************/
209297
209298/**************************************************************************
209299** Interface to code in fts5_tokenizer.c.
209300*/
209301
209302static int sqlite3Fts5TokenizerInit(fts5_api*);
209303/*
209304** End of interface to code in fts5_tokenizer.c.
209305**************************************************************************/
209306
209307/**************************************************************************
209308** Interface to code in fts5_vocab.c.
209309*/
209310
209311static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
209312
209313/*
209314** End of interface to code in fts5_vocab.c.
209315**************************************************************************/
209316
209317
209318/**************************************************************************
209319** Interface to automatically generated code in fts5_unicode2.c.
209320*/
209321static int sqlite3Fts5UnicodeIsdiacritic(int c);
209322static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
209323
209324static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
209325static int sqlite3Fts5UnicodeCategory(u32 iCode);
209326static void sqlite3Fts5UnicodeAscii(u8*, u8*);
209327/*
209328** End of interface to code in fts5_unicode2.c.
209329**************************************************************************/
209330
209331#endif
209332
209333#define FTS5_OR 1
209334#define FTS5_AND 2
209335#define FTS5_NOT 3
209336#define FTS5_TERM 4
209337#define FTS5_COLON 5
209338#define FTS5_MINUS 6
209339#define FTS5_LCP 7
209340#define FTS5_RCP 8
209341#define FTS5_STRING 9
209342#define FTS5_LP 10
209343#define FTS5_RP 11
209344#define FTS5_CARET 12
209345#define FTS5_COMMA 13
209346#define FTS5_PLUS 14
209347#define FTS5_STAR 15
209348
209349/*
209350** 2000-05-29
209351**
209352** The author disclaims copyright to this source code. In place of
209353** a legal notice, here is a blessing:
209354**
209355** May you do good and not evil.
209356** May you find forgiveness for yourself and forgive others.
209357** May you share freely, never taking more than you give.
209358**
209359*************************************************************************
209360** Driver template for the LEMON parser generator.
209361**
209362** The "lemon" program processes an LALR(1) input grammar file, then uses
209363** this template to construct a parser. The "lemon" program inserts text
209364** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
209365** interstitial "-" characters) contained in this template is changed into
209366** the value of the %name directive from the grammar. Otherwise, the content
209367** of this template is copied straight through into the generate parser
209368** source file.
209369**
209370** The following is the concatenation of all %include directives from the
209371** input grammar file:
209372*/
209373/* #include <stdio.h> */
209374/* #include <assert.h> */
209375/************ Begin %include sections from the grammar ************************/
209376
209377/* #include "fts5Int.h" */
209378/* #include "fts5parse.h" */
209379
209380/*
209381** Disable all error recovery processing in the parser push-down
209382** automaton.
209383*/
209384#define fts5YYNOERRORRECOVERY 1
209385
209386/*
209387** Make fts5yytestcase() the same as testcase()
209388*/
209389#define fts5yytestcase(X) testcase(X)
209390
209391/*
209392** Indicate that sqlite3ParserFree() will never be called with a null
209393** pointer.
209394*/
209395#define fts5YYPARSEFREENOTNULL 1
209396
209397/*
209398** Alternative datatype for the argument to the malloc() routine passed
209399** into sqlite3ParserAlloc(). The default is size_t.
209400*/
209401#define fts5YYMALLOCARGTYPE u64
209402
209403/**************** End of %include directives **********************************/
209404/* These constants specify the various numeric values for terminal symbols
209405** in a format understandable to "makeheaders". This section is blank unless
209406** "lemon" is run with the "-m" command-line option.
209407***************** Begin makeheaders token definitions *************************/
209408/**************** End makeheaders token definitions ***************************/
209409
209410/* The next sections is a series of control #defines.
209411** various aspects of the generated parser.
209412** fts5YYCODETYPE is the data type used to store the integer codes
209413** that represent terminal and non-terminal symbols.
209414** "unsigned char" is used if there are fewer than
209415** 256 symbols. Larger types otherwise.
209416** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
209417** any terminal or nonterminal symbol.
209418** fts5YYFALLBACK If defined, this indicates that one or more tokens
209419** (also known as: "terminal symbols") have fall-back
209420** values which should be used if the original symbol
209421** would not parse. This permits keywords to sometimes
209422** be used as identifiers, for example.
209423** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
209424** that indicate what to do in response to the next
209425** token.
209426** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
209427** symbols. Background: A "minor type" is a semantic
209428** value associated with a terminal or non-terminal
209429** symbols. For example, for an "ID" terminal symbol,
209430** the minor type might be the name of the identifier.
209431** Each non-terminal can have a different minor type.
209432** Terminal symbols all have the same minor type, though.
209433** This macros defines the minor type for terminal
209434** symbols.
209435** fts5YYMINORTYPE is the data type used for all minor types.
209436** This is typically a union of many types, one of
209437** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
209438** for terminal symbols is called "fts5yy0".
209439** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
209440** zero the stack is dynamically sized using realloc()
209441** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
209442** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
209443** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
209444** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
209445** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
209446** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
209447** fts5YYERRORSYMBOL is the code number of the error symbol. If not
209448** defined, then do no error processing.
209449** fts5YYNSTATE the combined number of states.
209450** fts5YYNRULE the number of rules in the grammar
209451** fts5YYNFTS5TOKEN Number of terminal symbols
209452** fts5YY_MAX_SHIFT Maximum value for shift actions
209453** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
209454** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
209455** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
209456** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
209457** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
209458** fts5YY_MIN_REDUCE Minimum value for reduce actions
209459** fts5YY_MAX_REDUCE Maximum value for reduce actions
209460*/
209461#ifndef INTERFACE
209462# define INTERFACE 1
209463#endif
209464/************* Begin control #defines *****************************************/
209465#define fts5YYCODETYPE unsigned char
209466#define fts5YYNOCODE 27
209467#define fts5YYACTIONTYPE unsigned char
209468#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
209469typedef union {
209470 int fts5yyinit;
209471 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
209472 int fts5yy4;
209473 Fts5Colset* fts5yy11;
209474 Fts5ExprNode* fts5yy24;
209475 Fts5ExprNearset* fts5yy46;
209476 Fts5ExprPhrase* fts5yy53;
209477} fts5YYMINORTYPE;
209478#ifndef fts5YYSTACKDEPTH
209479#define fts5YYSTACKDEPTH 100
209480#endif
209481#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
209482#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
209483#define sqlite3Fts5ParserARG_PARAM ,pParse
209484#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
209485#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
209486#define sqlite3Fts5ParserCTX_SDECL
209487#define sqlite3Fts5ParserCTX_PDECL
209488#define sqlite3Fts5ParserCTX_PARAM
209489#define sqlite3Fts5ParserCTX_FETCH
209490#define sqlite3Fts5ParserCTX_STORE
209491#define fts5YYNSTATE 35
209492#define fts5YYNRULE 28
209493#define fts5YYNRULE_WITH_ACTION 28
209494#define fts5YYNFTS5TOKEN 16
209495#define fts5YY_MAX_SHIFT 34
209496#define fts5YY_MIN_SHIFTREDUCE 52
209497#define fts5YY_MAX_SHIFTREDUCE 79
209498#define fts5YY_ERROR_ACTION 80
209499#define fts5YY_ACCEPT_ACTION 81
209500#define fts5YY_NO_ACTION 82
209501#define fts5YY_MIN_REDUCE 83
209502#define fts5YY_MAX_REDUCE 110
209503/************* End control #defines *******************************************/
209504#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
209505
209506/* Define the fts5yytestcase() macro to be a no-op if is not already defined
209507** otherwise.
209508**
209509** Applications can choose to define fts5yytestcase() in the %include section
209510** to a macro that can assist in verifying code coverage. For production
209511** code the fts5yytestcase() macro should be turned off. But it is useful
209512** for testing.
209513*/
209514#ifndef fts5yytestcase
209515# define fts5yytestcase(X)
209516#endif
209517
209518
209519/* Next are the tables used to determine what action to take based on the
209520** current state and lookahead token. These tables are used to implement
209521** functions that take a state number and lookahead value and return an
209522** action integer.
209523**
209524** Suppose the action integer is N. Then the action is determined as
209525** follows
209526**
209527** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
209528** token onto the stack and goto state N.
209529**
209530** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
209531** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
209532**
209533** N == fts5YY_ERROR_ACTION A syntax error has occurred.
209534**
209535** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
209536**
209537** N == fts5YY_NO_ACTION No such action. Denotes unused
209538** slots in the fts5yy_action[] table.
209539**
209540** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
209541** and fts5YY_MAX_REDUCE
209542**
209543** The action table is constructed as a single large table named fts5yy_action[].
209544** Given state S and lookahead X, the action is computed as either:
209545**
209546** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
209547** (B) N = fts5yy_default[S]
209548**
209549** The (A) formula is preferred. The B formula is used instead if
209550** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
209551**
209552** The formulas above are for computing the action when the lookahead is
209553** a terminal symbol. If the lookahead is a non-terminal (as occurs after
209554** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
209555** the fts5yy_shift_ofst[] array.
209556**
209557** The following are the tables generated in this section:
209558**
209559** fts5yy_action[] A single table containing all actions.
209560** fts5yy_lookahead[] A table containing the lookahead for each entry in
209561** fts5yy_action. Used to detect hash collisions.
209562** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
209563** shifting terminals.
209564** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
209565** shifting non-terminals after a reduce.
209566** fts5yy_default[] Default action for each state.
209567**
209568*********** Begin parsing tables **********************************************/
209569#define fts5YY_ACTTAB_COUNT (105)
209570static const fts5YYACTIONTYPE fts5yy_action[] = {
209571 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
209572 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
209573 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
209574 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
209575 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
209576 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
209577 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
209578 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
209579 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
209580 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
209581 /* 100 */ 57, 15, 82, 82, 71,
209582};
209583static const fts5YYCODETYPE fts5yy_lookahead[] = {
209584 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
209585 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
209586 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
209587 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
209588 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
209589 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
209590 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
209591 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
209592 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
209593 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
209594 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
209595 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
209596 /* 120 */ 27,
209597};
209598#define fts5YY_SHIFT_COUNT (34)
209599#define fts5YY_SHIFT_MIN (0)
209600#define fts5YY_SHIFT_MAX (93)
209601static const unsigned char fts5yy_shift_ofst[] = {
209602 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
209603 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
209604 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
209605 /* 30 */ 53, 87, 92, 53, 93,
209606};
209607#define fts5YY_REDUCE_COUNT (17)
209608#define fts5YY_REDUCE_MIN (-17)
209609#define fts5YY_REDUCE_MAX (67)
209610static const signed char fts5yy_reduce_ofst[] = {
209611 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
209612 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
209613};
209614static const fts5YYACTIONTYPE fts5yy_default[] = {
209615 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
209616 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
209617 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
209618 /* 30 */ 103, 80, 80, 104, 80,
209619};
209620/********** End of lemon-generated parsing tables *****************************/
209621
209622/* The next table maps tokens (terminal symbols) into fallback tokens.
209623** If a construct like the following:
209624**
209625** %fallback ID X Y Z.
209626**
209627** appears in the grammar, then ID becomes a fallback token for X, Y,
209628** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
209629** but it does not parse, the type of the token is changed to ID and
209630** the parse is retried before an error is thrown.
209631**
209632** This feature can be used, for example, to cause some keywords in a language
209633** to revert to identifiers if they keyword does not apply in the context where
209634** it appears.
209635*/
209636#ifdef fts5YYFALLBACK
209637static const fts5YYCODETYPE fts5yyFallback[] = {
209638};
209639#endif /* fts5YYFALLBACK */
209640
209641/* The following structure represents a single element of the
209642** parser's stack. Information stored includes:
209643**
209644** + The state number for the parser at this level of the stack.
209645**
209646** + The value of the token stored at this level of the stack.
209647** (In other words, the "major" token.)
209648**
209649** + The semantic value stored at this level of the stack. This is
209650** the information used by the action routines in the grammar.
209651** It is sometimes called the "minor" token.
209652**
209653** After the "shift" half of a SHIFTREDUCE action, the stateno field
209654** actually contains the reduce action for the second half of the
209655** SHIFTREDUCE.
209656*/
209657struct fts5yyStackEntry {
209658 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
209659 fts5YYCODETYPE major; /* The major token value. This is the code
209660 ** number for the token at this stack level */
209661 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
209662 ** is the value of the token */
209663};
209664typedef struct fts5yyStackEntry fts5yyStackEntry;
209665
209666/* The state of the parser is completely contained in an instance of
209667** the following structure */
209668struct fts5yyParser {
209669 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
209670#ifdef fts5YYTRACKMAXSTACKDEPTH
209671 int fts5yyhwm; /* High-water mark of the stack */
209672#endif
209673#ifndef fts5YYNOERRORRECOVERY
209674 int fts5yyerrcnt; /* Shifts left before out of the error */
209675#endif
209676 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
209677 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
209678#if fts5YYSTACKDEPTH<=0
209679 int fts5yystksz; /* Current side of the stack */
209680 fts5yyStackEntry *fts5yystack; /* The parser's stack */
209681 fts5yyStackEntry fts5yystk0; /* First stack entry */
209682#else
209683 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
209684 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
209685#endif
209686};
209687typedef struct fts5yyParser fts5yyParser;
209688
209689#ifndef NDEBUG
209690/* #include <stdio.h> */
209691static FILE *fts5yyTraceFILE = 0;
209692static char *fts5yyTracePrompt = 0;
209693#endif /* NDEBUG */
209694
209695#ifndef NDEBUG
209696/*
209697** Turn parser tracing on by giving a stream to which to write the trace
209698** and a prompt to preface each trace message. Tracing is turned off
209699** by making either argument NULL
209700**
209701** Inputs:
209702** <ul>
209703** <li> A FILE* to which trace output should be written.
209704** If NULL, then tracing is turned off.
209705** <li> A prefix string written at the beginning of every
209706** line of trace output. If NULL, then tracing is
209707** turned off.
209708** </ul>
209709**
209710** Outputs:
209711** None.
209712*/
209713static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
209714 fts5yyTraceFILE = TraceFILE;
209715 fts5yyTracePrompt = zTracePrompt;
209716 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
209717 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
209718}
209719#endif /* NDEBUG */
209720
209721#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
209722/* For tracing shifts, the names of all terminals and nonterminals
209723** are required. The following table supplies these names */
209724static const char *const fts5yyTokenName[] = {
209725 /* 0 */ "$",
209726 /* 1 */ "OR",
209727 /* 2 */ "AND",
209728 /* 3 */ "NOT",
209729 /* 4 */ "TERM",
209730 /* 5 */ "COLON",
209731 /* 6 */ "MINUS",
209732 /* 7 */ "LCP",
209733 /* 8 */ "RCP",
209734 /* 9 */ "STRING",
209735 /* 10 */ "LP",
209736 /* 11 */ "RP",
209737 /* 12 */ "CARET",
209738 /* 13 */ "COMMA",
209739 /* 14 */ "PLUS",
209740 /* 15 */ "STAR",
209741 /* 16 */ "input",
209742 /* 17 */ "expr",
209743 /* 18 */ "cnearset",
209744 /* 19 */ "exprlist",
209745 /* 20 */ "colset",
209746 /* 21 */ "colsetlist",
209747 /* 22 */ "nearset",
209748 /* 23 */ "nearphrases",
209749 /* 24 */ "phrase",
209750 /* 25 */ "neardist_opt",
209751 /* 26 */ "star_opt",
209752};
209753#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
209754
209755#ifndef NDEBUG
209756/* For tracing reduce actions, the names of all rules are required.
209757*/
209758static const char *const fts5yyRuleName[] = {
209759 /* 0 */ "input ::= expr",
209760 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
209761 /* 2 */ "colset ::= LCP colsetlist RCP",
209762 /* 3 */ "colset ::= STRING",
209763 /* 4 */ "colset ::= MINUS STRING",
209764 /* 5 */ "colsetlist ::= colsetlist STRING",
209765 /* 6 */ "colsetlist ::= STRING",
209766 /* 7 */ "expr ::= expr AND expr",
209767 /* 8 */ "expr ::= expr OR expr",
209768 /* 9 */ "expr ::= expr NOT expr",
209769 /* 10 */ "expr ::= colset COLON LP expr RP",
209770 /* 11 */ "expr ::= LP expr RP",
209771 /* 12 */ "expr ::= exprlist",
209772 /* 13 */ "exprlist ::= cnearset",
209773 /* 14 */ "exprlist ::= exprlist cnearset",
209774 /* 15 */ "cnearset ::= nearset",
209775 /* 16 */ "cnearset ::= colset COLON nearset",
209776 /* 17 */ "nearset ::= phrase",
209777 /* 18 */ "nearset ::= CARET phrase",
209778 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
209779 /* 20 */ "nearphrases ::= phrase",
209780 /* 21 */ "nearphrases ::= nearphrases phrase",
209781 /* 22 */ "neardist_opt ::=",
209782 /* 23 */ "neardist_opt ::= COMMA STRING",
209783 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
209784 /* 25 */ "phrase ::= STRING star_opt",
209785 /* 26 */ "star_opt ::= STAR",
209786 /* 27 */ "star_opt ::=",
209787};
209788#endif /* NDEBUG */
209789
209790
209791#if fts5YYSTACKDEPTH<=0
209792/*
209793** Try to increase the size of the parser stack. Return the number
209794** of errors. Return 0 on success.
209795*/
209796static int fts5yyGrowStack(fts5yyParser *p){
209797 int newSize;
209798 int idx;
209799 fts5yyStackEntry *pNew;
209800
209801 newSize = p->fts5yystksz*2 + 100;
209802 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
209803 if( p->fts5yystack==&p->fts5yystk0 ){
209804 pNew = malloc(newSize*sizeof(pNew[0]));
209805 if( pNew ) pNew[0] = p->fts5yystk0;
209806 }else{
209807 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
209808 }
209809 if( pNew ){
209810 p->fts5yystack = pNew;
209811 p->fts5yytos = &p->fts5yystack[idx];
209812#ifndef NDEBUG
209813 if( fts5yyTraceFILE ){
209814 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
209815 fts5yyTracePrompt, p->fts5yystksz, newSize);
209816 }
209817#endif
209818 p->fts5yystksz = newSize;
209819 }
209820 return pNew==0;
209821}
209822#endif
209823
209824/* Datatype of the argument to the memory allocated passed as the
209825** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
209826** putting an appropriate #define in the %include section of the input
209827** grammar.
209828*/
209829#ifndef fts5YYMALLOCARGTYPE
209830# define fts5YYMALLOCARGTYPE size_t
209831#endif
209832
209833/* Initialize a new parser that has already been allocated.
209834*/
209835static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
209836 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
209837 sqlite3Fts5ParserCTX_STORE
209838#ifdef fts5YYTRACKMAXSTACKDEPTH
209839 fts5yypParser->fts5yyhwm = 0;
209840#endif
209841#if fts5YYSTACKDEPTH<=0
209842 fts5yypParser->fts5yytos = NULL;
209843 fts5yypParser->fts5yystack = NULL;
209844 fts5yypParser->fts5yystksz = 0;
209845 if( fts5yyGrowStack(fts5yypParser) ){
209846 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
209847 fts5yypParser->fts5yystksz = 1;
209848 }
209849#endif
209850#ifndef fts5YYNOERRORRECOVERY
209851 fts5yypParser->fts5yyerrcnt = -1;
209852#endif
209853 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
209854 fts5yypParser->fts5yystack[0].stateno = 0;
209855 fts5yypParser->fts5yystack[0].major = 0;
209856#if fts5YYSTACKDEPTH>0
209857 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
209858#endif
209859}
209860
209861#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
209862/*
209863** This function allocates a new parser.
209864** The only argument is a pointer to a function which works like
209865** malloc.
209866**
209867** Inputs:
209868** A pointer to the function used to allocate memory.
209869**
209870** Outputs:
209871** A pointer to a parser. This pointer is used in subsequent calls
209872** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
209873*/
209874static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
209875 fts5yyParser *fts5yypParser;
209876 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
209877 if( fts5yypParser ){
209878 sqlite3Fts5ParserCTX_STORE
209879 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
209880 }
209881 return (void*)fts5yypParser;
209882}
209883#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
209884
209885
209886/* The following function deletes the "minor type" or semantic value
209887** associated with a symbol. The symbol can be either a terminal
209888** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
209889** a pointer to the value to be deleted. The code used to do the
209890** deletions is derived from the %destructor and/or %token_destructor
209891** directives of the input grammar.
209892*/
209893static void fts5yy_destructor(
209894 fts5yyParser *fts5yypParser, /* The parser */
209895 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
209896 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
209897){
209898 sqlite3Fts5ParserARG_FETCH
209899 sqlite3Fts5ParserCTX_FETCH
209900 switch( fts5yymajor ){
209901 /* Here is inserted the actions which take place when a
209902 ** terminal or non-terminal is destroyed. This can happen
209903 ** when the symbol is popped from the stack during a
209904 ** reduce or during error processing or when a parser is
209905 ** being destroyed before it is finished parsing.
209906 **
209907 ** Note: during a reduce, the only symbols destroyed are those
209908 ** which appear on the RHS of the rule, but which are *not* used
209909 ** inside the C code.
209910 */
209911/********* Begin destructor definitions ***************************************/
209912 case 16: /* input */
209913{
209914 (void)pParse;
209915}
209916 break;
209917 case 17: /* expr */
209918 case 18: /* cnearset */
209919 case 19: /* exprlist */
209920{
209921 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
209922}
209923 break;
209924 case 20: /* colset */
209925 case 21: /* colsetlist */
209926{
209927 sqlite3_free((fts5yypminor->fts5yy11));
209928}
209929 break;
209930 case 22: /* nearset */
209931 case 23: /* nearphrases */
209932{
209933 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
209934}
209935 break;
209936 case 24: /* phrase */
209937{
209938 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
209939}
209940 break;
209941/********* End destructor definitions *****************************************/
209942 default: break; /* If no destructor action specified: do nothing */
209943 }
209944}
209945
209946/*
209947** Pop the parser's stack once.
209948**
209949** If there is a destructor routine associated with the token which
209950** is popped from the stack, then call it.
209951*/
209952static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
209953 fts5yyStackEntry *fts5yytos;
209954 assert( pParser->fts5yytos!=0 );
209955 assert( pParser->fts5yytos > pParser->fts5yystack );
209956 fts5yytos = pParser->fts5yytos--;
209957#ifndef NDEBUG
209958 if( fts5yyTraceFILE ){
209959 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
209960 fts5yyTracePrompt,
209961 fts5yyTokenName[fts5yytos->major]);
209962 }
209963#endif
209964 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
209965}
209966
209967/*
209968** Clear all secondary memory allocations from the parser
209969*/
209970static void sqlite3Fts5ParserFinalize(void *p){
209971 fts5yyParser *pParser = (fts5yyParser*)p;
209972 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
209973#if fts5YYSTACKDEPTH<=0
209974 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
209975#endif
209976}
209977
209978#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
209979/*
209980** Deallocate and destroy a parser. Destructors are called for
209981** all stack elements before shutting the parser down.
209982**
209983** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
209984** is defined in a %include section of the input grammar) then it is
209985** assumed that the input pointer is never NULL.
209986*/
209987static void sqlite3Fts5ParserFree(
209988 void *p, /* The parser to be deleted */
209989 void (*freeProc)(void*) /* Function used to reclaim memory */
209990){
209991#ifndef fts5YYPARSEFREENEVERNULL
209992 if( p==0 ) return;
209993#endif
209994 sqlite3Fts5ParserFinalize(p);
209995 (*freeProc)(p);
209996}
209997#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
209998
209999/*
210000** Return the peak depth of the stack for a parser.
210001*/
210002#ifdef fts5YYTRACKMAXSTACKDEPTH
210003static int sqlite3Fts5ParserStackPeak(void *p){
210004 fts5yyParser *pParser = (fts5yyParser*)p;
210005 return pParser->fts5yyhwm;
210006}
210007#endif
210008
210009/* This array of booleans keeps track of the parser statement
210010** coverage. The element fts5yycoverage[X][Y] is set when the parser
210011** is in state X and has a lookahead token Y. In a well-tested
210012** systems, every element of this matrix should end up being set.
210013*/
210014#if defined(fts5YYCOVERAGE)
210015static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
210016#endif
210017
210018/*
210019** Write into out a description of every state/lookahead combination that
210020**
210021** (1) has not been used by the parser, and
210022** (2) is not a syntax error.
210023**
210024** Return the number of missed state/lookahead combinations.
210025*/
210026#if defined(fts5YYCOVERAGE)
210027static int sqlite3Fts5ParserCoverage(FILE *out){
210028 int stateno, iLookAhead, i;
210029 int nMissed = 0;
210030 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
210031 i = fts5yy_shift_ofst[stateno];
210032 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
210033 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
210034 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
210035 if( out ){
210036 fprintf(out,"State %d lookahead %s %s\n", stateno,
210037 fts5yyTokenName[iLookAhead],
210038 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
210039 }
210040 }
210041 }
210042 return nMissed;
210043}
210044#endif
210045
210046/*
210047** Find the appropriate action for a parser given the terminal
210048** look-ahead token iLookAhead.
210049*/
210050static fts5YYACTIONTYPE fts5yy_find_shift_action(
210051 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
210052 fts5YYACTIONTYPE stateno /* Current state number */
210053){
210054 int i;
210055
210056 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
210057 assert( stateno <= fts5YY_SHIFT_COUNT );
210058#if defined(fts5YYCOVERAGE)
210059 fts5yycoverage[stateno][iLookAhead] = 1;
210060#endif
210061 do{
210062 i = fts5yy_shift_ofst[stateno];
210063 assert( i>=0 );
210064 assert( i<=fts5YY_ACTTAB_COUNT );
210065 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD );
210066 assert( iLookAhead!=fts5YYNOCODE );
210067 assert( iLookAhead < fts5YYNFTS5TOKEN );
210068 i += iLookAhead;
210069 assert( i<(int)fts5YY_NLOOKAHEAD );
210070 if( fts5yy_lookahead[i]!=iLookAhead ){
210071#ifdef fts5YYFALLBACK
210072 fts5YYCODETYPE iFallback; /* Fallback token */
210073 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) );
210074 iFallback = fts5yyFallback[iLookAhead];
210075 if( iFallback!=0 ){
210076#ifndef NDEBUG
210077 if( fts5yyTraceFILE ){
210078 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
210079 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
210080 }
210081#endif
210082 assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
210083 iLookAhead = iFallback;
210084 continue;
210085 }
210086#endif
210087#ifdef fts5YYWILDCARD
210088 {
210089 int j = i - iLookAhead + fts5YYWILDCARD;
210090 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) );
210091 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
210092#ifndef NDEBUG
210093 if( fts5yyTraceFILE ){
210094 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
210095 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
210096 fts5yyTokenName[fts5YYWILDCARD]);
210097 }
210098#endif /* NDEBUG */
210099 return fts5yy_action[j];
210100 }
210101 }
210102#endif /* fts5YYWILDCARD */
210103 return fts5yy_default[stateno];
210104 }else{
210105 assert( i>=0 && i<sizeof(fts5yy_action)/sizeof(fts5yy_action[0]) );
210106 return fts5yy_action[i];
210107 }
210108 }while(1);
210109}
210110
210111/*
210112** Find the appropriate action for a parser given the non-terminal
210113** look-ahead token iLookAhead.
210114*/
210115static fts5YYACTIONTYPE fts5yy_find_reduce_action(
210116 fts5YYACTIONTYPE stateno, /* Current state number */
210117 fts5YYCODETYPE iLookAhead /* The look-ahead token */
210118){
210119 int i;
210120#ifdef fts5YYERRORSYMBOL
210121 if( stateno>fts5YY_REDUCE_COUNT ){
210122 return fts5yy_default[stateno];
210123 }
210124#else
210125 assert( stateno<=fts5YY_REDUCE_COUNT );
210126#endif
210127 i = fts5yy_reduce_ofst[stateno];
210128 assert( iLookAhead!=fts5YYNOCODE );
210129 i += iLookAhead;
210130#ifdef fts5YYERRORSYMBOL
210131 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
210132 return fts5yy_default[stateno];
210133 }
210134#else
210135 assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
210136 assert( fts5yy_lookahead[i]==iLookAhead );
210137#endif
210138 return fts5yy_action[i];
210139}
210140
210141/*
210142** The following routine is called if the stack overflows.
210143*/
210144static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
210145 sqlite3Fts5ParserARG_FETCH
210146 sqlite3Fts5ParserCTX_FETCH
210147#ifndef NDEBUG
210148 if( fts5yyTraceFILE ){
210149 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
210150 }
210151#endif
210152 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
210153 /* Here code is inserted which will execute if the parser
210154 ** stack every overflows */
210155/******** Begin %stack_overflow code ******************************************/
210156
210157 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
210158/******** End %stack_overflow code ********************************************/
210159 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
210160 sqlite3Fts5ParserCTX_STORE
210161}
210162
210163/*
210164** Print tracing information for a SHIFT action
210165*/
210166#ifndef NDEBUG
210167static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
210168 if( fts5yyTraceFILE ){
210169 if( fts5yyNewState<fts5YYNSTATE ){
210170 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
210171 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
210172 fts5yyNewState);
210173 }else{
210174 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
210175 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
210176 fts5yyNewState - fts5YY_MIN_REDUCE);
210177 }
210178 }
210179}
210180#else
210181# define fts5yyTraceShift(X,Y,Z)
210182#endif
210183
210184/*
210185** Perform a shift action.
210186*/
210187static void fts5yy_shift(
210188 fts5yyParser *fts5yypParser, /* The parser to be shifted */
210189 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
210190 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
210191 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
210192){
210193 fts5yyStackEntry *fts5yytos;
210194 fts5yypParser->fts5yytos++;
210195#ifdef fts5YYTRACKMAXSTACKDEPTH
210196 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
210197 fts5yypParser->fts5yyhwm++;
210198 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
210199 }
210200#endif
210201#if fts5YYSTACKDEPTH>0
210202 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
210203 fts5yypParser->fts5yytos--;
210204 fts5yyStackOverflow(fts5yypParser);
210205 return;
210206 }
210207#else
210208 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
210209 if( fts5yyGrowStack(fts5yypParser) ){
210210 fts5yypParser->fts5yytos--;
210211 fts5yyStackOverflow(fts5yypParser);
210212 return;
210213 }
210214 }
210215#endif
210216 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
210217 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
210218 }
210219 fts5yytos = fts5yypParser->fts5yytos;
210220 fts5yytos->stateno = fts5yyNewState;
210221 fts5yytos->major = fts5yyMajor;
210222 fts5yytos->minor.fts5yy0 = fts5yyMinor;
210223 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
210224}
210225
210226/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
210227** of that rule */
210228static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
210229 16, /* (0) input ::= expr */
210230 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
210231 20, /* (2) colset ::= LCP colsetlist RCP */
210232 20, /* (3) colset ::= STRING */
210233 20, /* (4) colset ::= MINUS STRING */
210234 21, /* (5) colsetlist ::= colsetlist STRING */
210235 21, /* (6) colsetlist ::= STRING */
210236 17, /* (7) expr ::= expr AND expr */
210237 17, /* (8) expr ::= expr OR expr */
210238 17, /* (9) expr ::= expr NOT expr */
210239 17, /* (10) expr ::= colset COLON LP expr RP */
210240 17, /* (11) expr ::= LP expr RP */
210241 17, /* (12) expr ::= exprlist */
210242 19, /* (13) exprlist ::= cnearset */
210243 19, /* (14) exprlist ::= exprlist cnearset */
210244 18, /* (15) cnearset ::= nearset */
210245 18, /* (16) cnearset ::= colset COLON nearset */
210246 22, /* (17) nearset ::= phrase */
210247 22, /* (18) nearset ::= CARET phrase */
210248 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
210249 23, /* (20) nearphrases ::= phrase */
210250 23, /* (21) nearphrases ::= nearphrases phrase */
210251 25, /* (22) neardist_opt ::= */
210252 25, /* (23) neardist_opt ::= COMMA STRING */
210253 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
210254 24, /* (25) phrase ::= STRING star_opt */
210255 26, /* (26) star_opt ::= STAR */
210256 26, /* (27) star_opt ::= */
210257};
210258
210259/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
210260** of symbols on the right-hand side of that rule. */
210261static const signed char fts5yyRuleInfoNRhs[] = {
210262 -1, /* (0) input ::= expr */
210263 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
210264 -3, /* (2) colset ::= LCP colsetlist RCP */
210265 -1, /* (3) colset ::= STRING */
210266 -2, /* (4) colset ::= MINUS STRING */
210267 -2, /* (5) colsetlist ::= colsetlist STRING */
210268 -1, /* (6) colsetlist ::= STRING */
210269 -3, /* (7) expr ::= expr AND expr */
210270 -3, /* (8) expr ::= expr OR expr */
210271 -3, /* (9) expr ::= expr NOT expr */
210272 -5, /* (10) expr ::= colset COLON LP expr RP */
210273 -3, /* (11) expr ::= LP expr RP */
210274 -1, /* (12) expr ::= exprlist */
210275 -1, /* (13) exprlist ::= cnearset */
210276 -2, /* (14) exprlist ::= exprlist cnearset */
210277 -1, /* (15) cnearset ::= nearset */
210278 -3, /* (16) cnearset ::= colset COLON nearset */
210279 -1, /* (17) nearset ::= phrase */
210280 -2, /* (18) nearset ::= CARET phrase */
210281 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
210282 -1, /* (20) nearphrases ::= phrase */
210283 -2, /* (21) nearphrases ::= nearphrases phrase */
210284 0, /* (22) neardist_opt ::= */
210285 -2, /* (23) neardist_opt ::= COMMA STRING */
210286 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
210287 -2, /* (25) phrase ::= STRING star_opt */
210288 -1, /* (26) star_opt ::= STAR */
210289 0, /* (27) star_opt ::= */
210290};
210291
210292static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
210293
210294/*
210295** Perform a reduce action and the shift that must immediately
210296** follow the reduce.
210297**
210298** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
210299** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
210300** if the lookahead token has already been consumed. As this procedure is
210301** only called from one place, optimizing compilers will in-line it, which
210302** means that the extra parameters have no performance impact.
210303*/
210304static fts5YYACTIONTYPE fts5yy_reduce(
210305 fts5yyParser *fts5yypParser, /* The parser */
210306 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
210307 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
210308 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
210309 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
210310){
210311 int fts5yygoto; /* The next state */
210312 fts5YYACTIONTYPE fts5yyact; /* The next action */
210313 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
210314 int fts5yysize; /* Amount to pop the stack */
210315 sqlite3Fts5ParserARG_FETCH
210316 (void)fts5yyLookahead;
210317 (void)fts5yyLookaheadToken;
210318 fts5yymsp = fts5yypParser->fts5yytos;
210319#ifndef NDEBUG
210320 if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
210321 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
210322 if( fts5yysize ){
210323 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
210324 fts5yyTracePrompt,
210325 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
210326 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action",
210327 fts5yymsp[fts5yysize].stateno);
210328 }else{
210329 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
210330 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
210331 fts5yyruleno<fts5YYNRULE_WITH_ACTION ? "" : " without external action");
210332 }
210333 }
210334#endif /* NDEBUG */
210335
210336 /* Check that the stack is large enough to grow by a single entry
210337 ** if the RHS of the rule is empty. This ensures that there is room
210338 ** enough on the stack to push the LHS value */
210339 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
210340#ifdef fts5YYTRACKMAXSTACKDEPTH
210341 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
210342 fts5yypParser->fts5yyhwm++;
210343 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
210344 }
210345#endif
210346#if fts5YYSTACKDEPTH>0
210347 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
210348 fts5yyStackOverflow(fts5yypParser);
210349 /* The call to fts5yyStackOverflow() above pops the stack until it is
210350 ** empty, causing the main parser loop to exit. So the return value
210351 ** is never used and does not matter. */
210352 return 0;
210353 }
210354#else
210355 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
210356 if( fts5yyGrowStack(fts5yypParser) ){
210357 fts5yyStackOverflow(fts5yypParser);
210358 /* The call to fts5yyStackOverflow() above pops the stack until it is
210359 ** empty, causing the main parser loop to exit. So the return value
210360 ** is never used and does not matter. */
210361 return 0;
210362 }
210363 fts5yymsp = fts5yypParser->fts5yytos;
210364 }
210365#endif
210366 }
210367
210368 switch( fts5yyruleno ){
210369 /* Beginning here are the reduction cases. A typical example
210370 ** follows:
210371 ** case 0:
210372 ** #line <lineno> <grammarfile>
210373 ** { ... } // User supplied code
210374 ** #line <lineno> <thisfile>
210375 ** break;
210376 */
210377/********** Begin reduce actions **********************************************/
210378 fts5YYMINORTYPE fts5yylhsminor;
210379 case 0: /* input ::= expr */
210380{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
210381 break;
210382 case 1: /* colset ::= MINUS LCP colsetlist RCP */
210383{
210384 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
210385}
210386 break;
210387 case 2: /* colset ::= LCP colsetlist RCP */
210388{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
210389 break;
210390 case 3: /* colset ::= STRING */
210391{
210392 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
210393}
210394 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
210395 break;
210396 case 4: /* colset ::= MINUS STRING */
210397{
210398 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
210399 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
210400}
210401 break;
210402 case 5: /* colsetlist ::= colsetlist STRING */
210403{
210404 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
210405 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
210406 break;
210407 case 6: /* colsetlist ::= STRING */
210408{
210409 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
210410}
210411 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
210412 break;
210413 case 7: /* expr ::= expr AND expr */
210414{
210415 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
210416}
210417 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210418 break;
210419 case 8: /* expr ::= expr OR expr */
210420{
210421 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
210422}
210423 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210424 break;
210425 case 9: /* expr ::= expr NOT expr */
210426{
210427 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
210428}
210429 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210430 break;
210431 case 10: /* expr ::= colset COLON LP expr RP */
210432{
210433 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
210434 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
210435}
210436 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210437 break;
210438 case 11: /* expr ::= LP expr RP */
210439{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
210440 break;
210441 case 12: /* expr ::= exprlist */
210442 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
210443{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
210444 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210445 break;
210446 case 14: /* exprlist ::= exprlist cnearset */
210447{
210448 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
210449}
210450 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210451 break;
210452 case 15: /* cnearset ::= nearset */
210453{
210454 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
210455}
210456 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210457 break;
210458 case 16: /* cnearset ::= colset COLON nearset */
210459{
210460 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
210461 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
210462}
210463 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
210464 break;
210465 case 17: /* nearset ::= phrase */
210466{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
210467 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
210468 break;
210469 case 18: /* nearset ::= CARET phrase */
210470{
210471 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
210472 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
210473}
210474 break;
210475 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
210476{
210477 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
210478 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
210479 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
210480}
210481 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
210482 break;
210483 case 20: /* nearphrases ::= phrase */
210484{
210485 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
210486}
210487 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
210488 break;
210489 case 21: /* nearphrases ::= nearphrases phrase */
210490{
210491 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
210492}
210493 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
210494 break;
210495 case 22: /* neardist_opt ::= */
210496{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
210497 break;
210498 case 23: /* neardist_opt ::= COMMA STRING */
210499{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
210500 break;
210501 case 24: /* phrase ::= phrase PLUS STRING star_opt */
210502{
210503 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
210504}
210505 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
210506 break;
210507 case 25: /* phrase ::= STRING star_opt */
210508{
210509 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
210510}
210511 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
210512 break;
210513 case 26: /* star_opt ::= STAR */
210514{ fts5yymsp[0].minor.fts5yy4 = 1; }
210515 break;
210516 case 27: /* star_opt ::= */
210517{ fts5yymsp[1].minor.fts5yy4 = 0; }
210518 break;
210519 default:
210520 break;
210521/********** End reduce actions ************************************************/
210522 };
210523 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) );
210524 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
210525 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
210526 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
210527
210528 /* There are no SHIFTREDUCE actions on nonterminals because the table
210529 ** generator has simplified them to pure REDUCE actions. */
210530 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
210531
210532 /* It is not possible for a REDUCE to be followed by an error */
210533 assert( fts5yyact!=fts5YY_ERROR_ACTION );
210534
210535 fts5yymsp += fts5yysize+1;
210536 fts5yypParser->fts5yytos = fts5yymsp;
210537 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
210538 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
210539 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
210540 return fts5yyact;
210541}
210542
210543/*
210544** The following code executes when the parse fails
210545*/
210546#ifndef fts5YYNOERRORRECOVERY
210547static void fts5yy_parse_failed(
210548 fts5yyParser *fts5yypParser /* The parser */
210549){
210550 sqlite3Fts5ParserARG_FETCH
210551 sqlite3Fts5ParserCTX_FETCH
210552#ifndef NDEBUG
210553 if( fts5yyTraceFILE ){
210554 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
210555 }
210556#endif
210557 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
210558 /* Here code is inserted which will be executed whenever the
210559 ** parser fails */
210560/************ Begin %parse_failure code ***************************************/
210561/************ End %parse_failure code *****************************************/
210562 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
210563 sqlite3Fts5ParserCTX_STORE
210564}
210565#endif /* fts5YYNOERRORRECOVERY */
210566
210567/*
210568** The following code executes when a syntax error first occurs.
210569*/
210570static void fts5yy_syntax_error(
210571 fts5yyParser *fts5yypParser, /* The parser */
210572 int fts5yymajor, /* The major type of the error token */
210573 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
210574){
210575 sqlite3Fts5ParserARG_FETCH
210576 sqlite3Fts5ParserCTX_FETCH
210577#define FTS5TOKEN fts5yyminor
210578/************ Begin %syntax_error code ****************************************/
210579
210580 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
210581 sqlite3Fts5ParseError(
210582 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
210583 );
210584/************ End %syntax_error code ******************************************/
210585 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
210586 sqlite3Fts5ParserCTX_STORE
210587}
210588
210589/*
210590** The following is executed when the parser accepts
210591*/
210592static void fts5yy_accept(
210593 fts5yyParser *fts5yypParser /* The parser */
210594){
210595 sqlite3Fts5ParserARG_FETCH
210596 sqlite3Fts5ParserCTX_FETCH
210597#ifndef NDEBUG
210598 if( fts5yyTraceFILE ){
210599 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
210600 }
210601#endif
210602#ifndef fts5YYNOERRORRECOVERY
210603 fts5yypParser->fts5yyerrcnt = -1;
210604#endif
210605 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
210606 /* Here code is inserted which will be executed whenever the
210607 ** parser accepts */
210608/*********** Begin %parse_accept code *****************************************/
210609/*********** End %parse_accept code *******************************************/
210610 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
210611 sqlite3Fts5ParserCTX_STORE
210612}
210613
210614/* The main parser program.
210615** The first argument is a pointer to a structure obtained from
210616** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
210617** The second argument is the major token number. The third is
210618** the minor token. The fourth optional argument is whatever the
210619** user wants (and specified in the grammar) and is available for
210620** use by the action routines.
210621**
210622** Inputs:
210623** <ul>
210624** <li> A pointer to the parser (an opaque structure.)
210625** <li> The major token number.
210626** <li> The minor token number.
210627** <li> An option argument of a grammar-specified type.
210628** </ul>
210629**
210630** Outputs:
210631** None.
210632*/
210633static void sqlite3Fts5Parser(
210634 void *fts5yyp, /* The parser */
210635 int fts5yymajor, /* The major token code number */
210636 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
210637 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
210638){
210639 fts5YYMINORTYPE fts5yyminorunion;
210640 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
210641#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
210642 int fts5yyendofinput; /* True if we are at the end of input */
210643#endif
210644#ifdef fts5YYERRORSYMBOL
210645 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
210646#endif
210647 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
210648 sqlite3Fts5ParserCTX_FETCH
210649 sqlite3Fts5ParserARG_STORE
210650
210651 assert( fts5yypParser->fts5yytos!=0 );
210652#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
210653 fts5yyendofinput = (fts5yymajor==0);
210654#endif
210655
210656 fts5yyact = fts5yypParser->fts5yytos->stateno;
210657#ifndef NDEBUG
210658 if( fts5yyTraceFILE ){
210659 if( fts5yyact < fts5YY_MIN_REDUCE ){
210660 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
210661 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
210662 }else{
210663 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
210664 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
210665 }
210666 }
210667#endif
210668
210669 do{
210670 assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
210671 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
210672 if( fts5yyact >= fts5YY_MIN_REDUCE ){
210673 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
210674 fts5yyminor sqlite3Fts5ParserCTX_PARAM);
210675 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
210676 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
210677#ifndef fts5YYNOERRORRECOVERY
210678 fts5yypParser->fts5yyerrcnt--;
210679#endif
210680 break;
210681 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
210682 fts5yypParser->fts5yytos--;
210683 fts5yy_accept(fts5yypParser);
210684 return;
210685 }else{
210686 assert( fts5yyact == fts5YY_ERROR_ACTION );
210687 fts5yyminorunion.fts5yy0 = fts5yyminor;
210688#ifdef fts5YYERRORSYMBOL
210689 int fts5yymx;
210690#endif
210691#ifndef NDEBUG
210692 if( fts5yyTraceFILE ){
210693 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
210694 }
210695#endif
210696#ifdef fts5YYERRORSYMBOL
210697 /* A syntax error has occurred.
210698 ** The response to an error depends upon whether or not the
210699 ** grammar defines an error token "ERROR".
210700 **
210701 ** This is what we do if the grammar does define ERROR:
210702 **
210703 ** * Call the %syntax_error function.
210704 **
210705 ** * Begin popping the stack until we enter a state where
210706 ** it is legal to shift the error symbol, then shift
210707 ** the error symbol.
210708 **
210709 ** * Set the error count to three.
210710 **
210711 ** * Begin accepting and shifting new tokens. No new error
210712 ** processing will occur until three tokens have been
210713 ** shifted successfully.
210714 **
210715 */
210716 if( fts5yypParser->fts5yyerrcnt<0 ){
210717 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
210718 }
210719 fts5yymx = fts5yypParser->fts5yytos->major;
210720 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
210721#ifndef NDEBUG
210722 if( fts5yyTraceFILE ){
210723 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
210724 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
210725 }
210726#endif
210727 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
210728 fts5yymajor = fts5YYNOCODE;
210729 }else{
210730 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
210731 && (fts5yyact = fts5yy_find_reduce_action(
210732 fts5yypParser->fts5yytos->stateno,
210733 fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
210734 ){
210735 fts5yy_pop_parser_stack(fts5yypParser);
210736 }
210737 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
210738 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210739 fts5yy_parse_failed(fts5yypParser);
210740#ifndef fts5YYNOERRORRECOVERY
210741 fts5yypParser->fts5yyerrcnt = -1;
210742#endif
210743 fts5yymajor = fts5YYNOCODE;
210744 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
210745 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
210746 }
210747 }
210748 fts5yypParser->fts5yyerrcnt = 3;
210749 fts5yyerrorhit = 1;
210750 if( fts5yymajor==fts5YYNOCODE ) break;
210751 fts5yyact = fts5yypParser->fts5yytos->stateno;
210752#elif defined(fts5YYNOERRORRECOVERY)
210753 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
210754 ** do any kind of error recovery. Instead, simply invoke the syntax
210755 ** error routine and continue going as if nothing had happened.
210756 **
210757 ** Applications can set this macro (for example inside %include) if
210758 ** they intend to abandon the parse upon the first syntax error seen.
210759 */
210760 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
210761 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210762 break;
210763#else /* fts5YYERRORSYMBOL is not defined */
210764 /* This is what we do if the grammar does not define ERROR:
210765 **
210766 ** * Report an error message, and throw away the input token.
210767 **
210768 ** * If the input token is $, then fail the parse.
210769 **
210770 ** As before, subsequent error messages are suppressed until
210771 ** three input tokens have been successfully shifted.
210772 */
210773 if( fts5yypParser->fts5yyerrcnt<=0 ){
210774 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
210775 }
210776 fts5yypParser->fts5yyerrcnt = 3;
210777 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
210778 if( fts5yyendofinput ){
210779 fts5yy_parse_failed(fts5yypParser);
210780#ifndef fts5YYNOERRORRECOVERY
210781 fts5yypParser->fts5yyerrcnt = -1;
210782#endif
210783 }
210784 break;
210785#endif
210786 }
210787 }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
210788#ifndef NDEBUG
210789 if( fts5yyTraceFILE ){
210790 fts5yyStackEntry *i;
210791 char cDiv = '[';
210792 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
210793 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
210794 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
210795 cDiv = ' ';
210796 }
210797 fprintf(fts5yyTraceFILE,"]\n");
210798 }
210799#endif
210800 return;
210801}
210802
210803/*
210804** Return the fallback token corresponding to canonical token iToken, or
210805** 0 if iToken has no fallback.
210806*/
210807static int sqlite3Fts5ParserFallback(int iToken){
210808#ifdef fts5YYFALLBACK
210809 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) );
210810 return fts5yyFallback[iToken];
210811#else
210812 (void)iToken;
210813 return 0;
210814#endif
210815}
210816
210817/*
210818** 2014 May 31
210819**
210820** The author disclaims copyright to this source code. In place of
210821** a legal notice, here is a blessing:
210822**
210823** May you do good and not evil.
210824** May you find forgiveness for yourself and forgive others.
210825** May you share freely, never taking more than you give.
210826**
210827******************************************************************************
210828*/
210829
210830
210831/* #include "fts5Int.h" */
210832#include <math.h> /* amalgamator: keep */
210833
210834/*
210835** Object used to iterate through all "coalesced phrase instances" in
210836** a single column of the current row. If the phrase instances in the
210837** column being considered do not overlap, this object simply iterates
210838** through them. Or, if they do overlap (share one or more tokens in
210839** common), each set of overlapping instances is treated as a single
210840** match. See documentation for the highlight() auxiliary function for
210841** details.
210842**
210843** Usage is:
210844**
210845** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
210846** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
210847** rc = fts5CInstIterNext(&iter)
210848** ){
210849** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
210850** }
210851**
210852*/
210853typedef struct CInstIter CInstIter;
210854struct CInstIter {
210855 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
210856 Fts5Context *pFts; /* First arg to pass to pApi functions */
210857 int iCol; /* Column to search */
210858 int iInst; /* Next phrase instance index */
210859 int nInst; /* Total number of phrase instances */
210860
210861 /* Output variables */
210862 int iStart; /* First token in coalesced phrase instance */
210863 int iEnd; /* Last token in coalesced phrase instance */
210864};
210865
210866/*
210867** Advance the iterator to the next coalesced phrase instance. Return
210868** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
210869*/
210870static int fts5CInstIterNext(CInstIter *pIter){
210871 int rc = SQLITE_OK;
210872 pIter->iStart = -1;
210873 pIter->iEnd = -1;
210874
210875 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
210876 int ip; int ic; int io;
210877 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
210878 if( rc==SQLITE_OK ){
210879 if( ic==pIter->iCol ){
210880 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
210881 if( pIter->iStart<0 ){
210882 pIter->iStart = io;
210883 pIter->iEnd = iEnd;
210884 }else if( io<=pIter->iEnd ){
210885 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
210886 }else{
210887 break;
210888 }
210889 }
210890 pIter->iInst++;
210891 }
210892 }
210893
210894 return rc;
210895}
210896
210897/*
210898** Initialize the iterator object indicated by the final parameter to
210899** iterate through coalesced phrase instances in column iCol.
210900*/
210901static int fts5CInstIterInit(
210902 const Fts5ExtensionApi *pApi,
210903 Fts5Context *pFts,
210904 int iCol,
210905 CInstIter *pIter
210906){
210907 int rc;
210908
210909 memset(pIter, 0, sizeof(CInstIter));
210910 pIter->pApi = pApi;
210911 pIter->pFts = pFts;
210912 pIter->iCol = iCol;
210913 rc = pApi->xInstCount(pFts, &pIter->nInst);
210914
210915 if( rc==SQLITE_OK ){
210916 rc = fts5CInstIterNext(pIter);
210917 }
210918
210919 return rc;
210920}
210921
210922
210923
210924/*************************************************************************
210925** Start of highlight() implementation.
210926*/
210927typedef struct HighlightContext HighlightContext;
210928struct HighlightContext {
210929 CInstIter iter; /* Coalesced Instance Iterator */
210930 int iPos; /* Current token offset in zIn[] */
210931 int iRangeStart; /* First token to include */
210932 int iRangeEnd; /* If non-zero, last token to include */
210933 const char *zOpen; /* Opening highlight */
210934 const char *zClose; /* Closing highlight */
210935 const char *zIn; /* Input text */
210936 int nIn; /* Size of input text in bytes */
210937 int iOff; /* Current offset within zIn[] */
210938 char *zOut; /* Output value */
210939};
210940
210941/*
210942** Append text to the HighlightContext output string - p->zOut. Argument
210943** z points to a buffer containing n bytes of text to append. If n is
210944** negative, everything up until the first '\0' is appended to the output.
210945**
210946** If *pRc is set to any value other than SQLITE_OK when this function is
210947** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
210948** *pRc is set to an error code before returning.
210949*/
210950static void fts5HighlightAppend(
210951 int *pRc,
210952 HighlightContext *p,
210953 const char *z, int n
210954){
210955 if( *pRc==SQLITE_OK && z ){
210956 if( n<0 ) n = (int)strlen(z);
210957 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
210958 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
210959 }
210960}
210961
210962/*
210963** Tokenizer callback used by implementation of highlight() function.
210964*/
210965static int fts5HighlightCb(
210966 void *pContext, /* Pointer to HighlightContext object */
210967 int tflags, /* Mask of FTS5_TOKEN_* flags */
210968 const char *pToken, /* Buffer containing token */
210969 int nToken, /* Size of token in bytes */
210970 int iStartOff, /* Start offset of token */
210971 int iEndOff /* End offset of token */
210972){
210973 HighlightContext *p = (HighlightContext*)pContext;
210974 int rc = SQLITE_OK;
210975 int iPos;
210976
210977 UNUSED_PARAM2(pToken, nToken);
210978
210979 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
210980 iPos = p->iPos++;
210981
210982 if( p->iRangeEnd>0 ){
210983 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
210984 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
210985 }
210986
210987 if( iPos==p->iter.iStart ){
210988 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
210989 fts5HighlightAppend(&rc, p, p->zOpen, -1);
210990 p->iOff = iStartOff;
210991 }
210992
210993 if( iPos==p->iter.iEnd ){
210994 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
210995 fts5HighlightAppend(&rc, p, p->zOpen, -1);
210996 }
210997 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
210998 fts5HighlightAppend(&rc, p, p->zClose, -1);
210999 p->iOff = iEndOff;
211000 if( rc==SQLITE_OK ){
211001 rc = fts5CInstIterNext(&p->iter);
211002 }
211003 }
211004
211005 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
211006 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
211007 p->iOff = iEndOff;
211008 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
211009 fts5HighlightAppend(&rc, p, p->zClose, -1);
211010 }
211011 }
211012
211013 return rc;
211014}
211015
211016/*
211017** Implementation of highlight() function.
211018*/
211019static void fts5HighlightFunction(
211020 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
211021 Fts5Context *pFts, /* First arg to pass to pApi functions */
211022 sqlite3_context *pCtx, /* Context for returning result/error */
211023 int nVal, /* Number of values in apVal[] array */
211024 sqlite3_value **apVal /* Array of trailing arguments */
211025){
211026 HighlightContext ctx;
211027 int rc;
211028 int iCol;
211029
211030 if( nVal!=3 ){
211031 const char *zErr = "wrong number of arguments to function highlight()";
211032 sqlite3_result_error(pCtx, zErr, -1);
211033 return;
211034 }
211035
211036 iCol = sqlite3_value_int(apVal[0]);
211037 memset(&ctx, 0, sizeof(HighlightContext));
211038 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
211039 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
211040 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
211041
211042 if( ctx.zIn ){
211043 if( rc==SQLITE_OK ){
211044 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
211045 }
211046
211047 if( rc==SQLITE_OK ){
211048 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
211049 }
211050 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
211051
211052 if( rc==SQLITE_OK ){
211053 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
211054 }
211055 sqlite3_free(ctx.zOut);
211056 }
211057 if( rc!=SQLITE_OK ){
211058 sqlite3_result_error_code(pCtx, rc);
211059 }
211060}
211061/*
211062** End of highlight() implementation.
211063**************************************************************************/
211064
211065/*
211066** Context object passed to the fts5SentenceFinderCb() function.
211067*/
211068typedef struct Fts5SFinder Fts5SFinder;
211069struct Fts5SFinder {
211070 int iPos; /* Current token position */
211071 int nFirstAlloc; /* Allocated size of aFirst[] */
211072 int nFirst; /* Number of entries in aFirst[] */
211073 int *aFirst; /* Array of first token in each sentence */
211074 const char *zDoc; /* Document being tokenized */
211075};
211076
211077/*
211078** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
211079** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
211080** error occurs.
211081*/
211082static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
211083 if( p->nFirstAlloc==p->nFirst ){
211084 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
211085 int *aNew;
211086
211087 aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
211088 if( aNew==0 ) return SQLITE_NOMEM;
211089 p->aFirst = aNew;
211090 p->nFirstAlloc = nNew;
211091 }
211092 p->aFirst[p->nFirst++] = iAdd;
211093 return SQLITE_OK;
211094}
211095
211096/*
211097** This function is an xTokenize() callback used by the auxiliary snippet()
211098** function. Its job is to identify tokens that are the first in a sentence.
211099** For each such token, an entry is added to the SFinder.aFirst[] array.
211100*/
211101static int fts5SentenceFinderCb(
211102 void *pContext, /* Pointer to HighlightContext object */
211103 int tflags, /* Mask of FTS5_TOKEN_* flags */
211104 const char *pToken, /* Buffer containing token */
211105 int nToken, /* Size of token in bytes */
211106 int iStartOff, /* Start offset of token */
211107 int iEndOff /* End offset of token */
211108){
211109 int rc = SQLITE_OK;
211110
211111 UNUSED_PARAM2(pToken, nToken);
211112 UNUSED_PARAM(iEndOff);
211113
211114 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
211115 Fts5SFinder *p = (Fts5SFinder*)pContext;
211116 if( p->iPos>0 ){
211117 int i;
211118 char c = 0;
211119 for(i=iStartOff-1; i>=0; i--){
211120 c = p->zDoc[i];
211121 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
211122 }
211123 if( i!=iStartOff-1 && (c=='.' || c==':') ){
211124 rc = fts5SentenceFinderAdd(p, p->iPos);
211125 }
211126 }else{
211127 rc = fts5SentenceFinderAdd(p, 0);
211128 }
211129 p->iPos++;
211130 }
211131 return rc;
211132}
211133
211134static int fts5SnippetScore(
211135 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
211136 Fts5Context *pFts, /* First arg to pass to pApi functions */
211137 int nDocsize, /* Size of column in tokens */
211138 unsigned char *aSeen, /* Array with one element per query phrase */
211139 int iCol, /* Column to score */
211140 int iPos, /* Starting offset to score */
211141 int nToken, /* Max tokens per snippet */
211142 int *pnScore, /* OUT: Score */
211143 int *piPos /* OUT: Adjusted offset */
211144){
211145 int rc;
211146 int i;
211147 int ip = 0;
211148 int ic = 0;
211149 int iOff = 0;
211150 int iFirst = -1;
211151 int nInst;
211152 int nScore = 0;
211153 int iLast = 0;
211154 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
211155
211156 rc = pApi->xInstCount(pFts, &nInst);
211157 for(i=0; i<nInst && rc==SQLITE_OK; i++){
211158 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
211159 if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
211160 nScore += (aSeen[ip] ? 1 : 1000);
211161 aSeen[ip] = 1;
211162 if( iFirst<0 ) iFirst = iOff;
211163 iLast = iOff + pApi->xPhraseSize(pFts, ip);
211164 }
211165 }
211166
211167 *pnScore = nScore;
211168 if( piPos ){
211169 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
211170 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
211171 if( iAdj<0 ) iAdj = 0;
211172 *piPos = (int)iAdj;
211173 }
211174
211175 return rc;
211176}
211177
211178/*
211179** Return the value in pVal interpreted as utf-8 text. Except, if pVal
211180** contains a NULL value, return a pointer to a static string zero
211181** bytes in length instead of a NULL pointer.
211182*/
211183static const char *fts5ValueToText(sqlite3_value *pVal){
211184 const char *zRet = (const char*)sqlite3_value_text(pVal);
211185 return zRet ? zRet : "";
211186}
211187
211188/*
211189** Implementation of snippet() function.
211190*/
211191static void fts5SnippetFunction(
211192 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
211193 Fts5Context *pFts, /* First arg to pass to pApi functions */
211194 sqlite3_context *pCtx, /* Context for returning result/error */
211195 int nVal, /* Number of values in apVal[] array */
211196 sqlite3_value **apVal /* Array of trailing arguments */
211197){
211198 HighlightContext ctx;
211199 int rc = SQLITE_OK; /* Return code */
211200 int iCol; /* 1st argument to snippet() */
211201 const char *zEllips; /* 4th argument to snippet() */
211202 int nToken; /* 5th argument to snippet() */
211203 int nInst = 0; /* Number of instance matches this row */
211204 int i; /* Used to iterate through instances */
211205 int nPhrase; /* Number of phrases in query */
211206 unsigned char *aSeen; /* Array of "seen instance" flags */
211207 int iBestCol; /* Column containing best snippet */
211208 int iBestStart = 0; /* First token of best snippet */
211209 int nBestScore = 0; /* Score of best snippet */
211210 int nColSize = 0; /* Total size of iBestCol in tokens */
211211 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
211212 int nCol;
211213
211214 if( nVal!=5 ){
211215 const char *zErr = "wrong number of arguments to function snippet()";
211216 sqlite3_result_error(pCtx, zErr, -1);
211217 return;
211218 }
211219
211220 nCol = pApi->xColumnCount(pFts);
211221 memset(&ctx, 0, sizeof(HighlightContext));
211222 iCol = sqlite3_value_int(apVal[0]);
211223 ctx.zOpen = fts5ValueToText(apVal[1]);
211224 ctx.zClose = fts5ValueToText(apVal[2]);
211225 zEllips = fts5ValueToText(apVal[3]);
211226 nToken = sqlite3_value_int(apVal[4]);
211227
211228 iBestCol = (iCol>=0 ? iCol : 0);
211229 nPhrase = pApi->xPhraseCount(pFts);
211230 aSeen = sqlite3_malloc(nPhrase);
211231 if( aSeen==0 ){
211232 rc = SQLITE_NOMEM;
211233 }
211234 if( rc==SQLITE_OK ){
211235 rc = pApi->xInstCount(pFts, &nInst);
211236 }
211237
211238 memset(&sFinder, 0, sizeof(Fts5SFinder));
211239 for(i=0; i<nCol; i++){
211240 if( iCol<0 || iCol==i ){
211241 int nDoc;
211242 int nDocsize;
211243 int ii;
211244 sFinder.iPos = 0;
211245 sFinder.nFirst = 0;
211246 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
211247 if( rc!=SQLITE_OK ) break;
211248 rc = pApi->xTokenize(pFts,
211249 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
211250 );
211251 if( rc!=SQLITE_OK ) break;
211252 rc = pApi->xColumnSize(pFts, i, &nDocsize);
211253 if( rc!=SQLITE_OK ) break;
211254
211255 for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
211256 int ip, ic, io;
211257 int iAdj;
211258 int nScore;
211259 int jj;
211260
211261 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
211262 if( ic!=i ) continue;
211263 if( io>nDocsize ) rc = FTS5_CORRUPT;
211264 if( rc!=SQLITE_OK ) continue;
211265 memset(aSeen, 0, nPhrase);
211266 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
211267 io, nToken, &nScore, &iAdj
211268 );
211269 if( rc==SQLITE_OK && nScore>nBestScore ){
211270 nBestScore = nScore;
211271 iBestCol = i;
211272 iBestStart = iAdj;
211273 nColSize = nDocsize;
211274 }
211275
211276 if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
211277 for(jj=0; jj<(sFinder.nFirst-1); jj++){
211278 if( sFinder.aFirst[jj+1]>io ) break;
211279 }
211280
211281 if( sFinder.aFirst[jj]<io ){
211282 memset(aSeen, 0, nPhrase);
211283 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
211284 sFinder.aFirst[jj], nToken, &nScore, 0
211285 );
211286
211287 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
211288 if( rc==SQLITE_OK && nScore>nBestScore ){
211289 nBestScore = nScore;
211290 iBestCol = i;
211291 iBestStart = sFinder.aFirst[jj];
211292 nColSize = nDocsize;
211293 }
211294 }
211295 }
211296 }
211297 }
211298 }
211299
211300 if( rc==SQLITE_OK ){
211301 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
211302 }
211303 if( rc==SQLITE_OK && nColSize==0 ){
211304 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
211305 }
211306 if( ctx.zIn ){
211307 if( rc==SQLITE_OK ){
211308 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
211309 }
211310
211311 ctx.iRangeStart = iBestStart;
211312 ctx.iRangeEnd = iBestStart + nToken - 1;
211313
211314 if( iBestStart>0 ){
211315 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
211316 }
211317
211318 /* Advance iterator ctx.iter so that it points to the first coalesced
211319 ** phrase instance at or following position iBestStart. */
211320 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
211321 rc = fts5CInstIterNext(&ctx.iter);
211322 }
211323
211324 if( rc==SQLITE_OK ){
211325 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
211326 }
211327 if( ctx.iRangeEnd>=(nColSize-1) ){
211328 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
211329 }else{
211330 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
211331 }
211332 }
211333 if( rc==SQLITE_OK ){
211334 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
211335 }else{
211336 sqlite3_result_error_code(pCtx, rc);
211337 }
211338 sqlite3_free(ctx.zOut);
211339 sqlite3_free(aSeen);
211340 sqlite3_free(sFinder.aFirst);
211341}
211342
211343/************************************************************************/
211344
211345/*
211346** The first time the bm25() function is called for a query, an instance
211347** of the following structure is allocated and populated.
211348*/
211349typedef struct Fts5Bm25Data Fts5Bm25Data;
211350struct Fts5Bm25Data {
211351 int nPhrase; /* Number of phrases in query */
211352 double avgdl; /* Average number of tokens in each row */
211353 double *aIDF; /* IDF for each phrase */
211354 double *aFreq; /* Array used to calculate phrase freq. */
211355};
211356
211357/*
211358** Callback used by fts5Bm25GetData() to count the number of rows in the
211359** table matched by each individual phrase within the query.
211360*/
211361static int fts5CountCb(
211362 const Fts5ExtensionApi *pApi,
211363 Fts5Context *pFts,
211364 void *pUserData /* Pointer to sqlite3_int64 variable */
211365){
211366 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
211367 UNUSED_PARAM2(pApi, pFts);
211368 (*pn)++;
211369 return SQLITE_OK;
211370}
211371
211372/*
211373** Set *ppData to point to the Fts5Bm25Data object for the current query.
211374** If the object has not already been allocated, allocate and populate it
211375** now.
211376*/
211377static int fts5Bm25GetData(
211378 const Fts5ExtensionApi *pApi,
211379 Fts5Context *pFts,
211380 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
211381){
211382 int rc = SQLITE_OK; /* Return code */
211383 Fts5Bm25Data *p; /* Object to return */
211384
211385 p = pApi->xGetAuxdata(pFts, 0);
211386 if( p==0 ){
211387 int nPhrase; /* Number of phrases in query */
211388 sqlite3_int64 nRow = 0; /* Number of rows in table */
211389 sqlite3_int64 nToken = 0; /* Number of tokens in table */
211390 sqlite3_int64 nByte; /* Bytes of space to allocate */
211391 int i;
211392
211393 /* Allocate the Fts5Bm25Data object */
211394 nPhrase = pApi->xPhraseCount(pFts);
211395 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
211396 p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
211397 if( p==0 ){
211398 rc = SQLITE_NOMEM;
211399 }else{
211400 memset(p, 0, (size_t)nByte);
211401 p->nPhrase = nPhrase;
211402 p->aIDF = (double*)&p[1];
211403 p->aFreq = &p->aIDF[nPhrase];
211404 }
211405
211406 /* Calculate the average document length for this FTS5 table */
211407 if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
211408 assert( rc!=SQLITE_OK || nRow>0 );
211409 if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
211410 if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
211411
211412 /* Calculate an IDF for each phrase in the query */
211413 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
211414 sqlite3_int64 nHit = 0;
211415 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
211416 if( rc==SQLITE_OK ){
211417 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
211418 ** This is done using the standard BM25 formula as found on wikipedia:
211419 **
211420 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
211421 **
211422 ** where "N" is the total number of documents in the set and nHit
211423 ** is the number that contain at least one instance of the phrase
211424 ** under consideration.
211425 **
211426 ** The problem with this is that if (N < 2*nHit), the IDF is
211427 ** negative. Which is undesirable. So the mimimum allowable IDF is
211428 ** (1e-6) - roughly the same as a term that appears in just over
211429 ** half of set of 5,000,000 documents. */
211430 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
211431 if( idf<=0.0 ) idf = 1e-6;
211432 p->aIDF[i] = idf;
211433 }
211434 }
211435
211436 if( rc!=SQLITE_OK ){
211437 sqlite3_free(p);
211438 }else{
211439 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
211440 }
211441 if( rc!=SQLITE_OK ) p = 0;
211442 }
211443 *ppData = p;
211444 return rc;
211445}
211446
211447/*
211448** Implementation of bm25() function.
211449*/
211450static void fts5Bm25Function(
211451 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
211452 Fts5Context *pFts, /* First arg to pass to pApi functions */
211453 sqlite3_context *pCtx, /* Context for returning result/error */
211454 int nVal, /* Number of values in apVal[] array */
211455 sqlite3_value **apVal /* Array of trailing arguments */
211456){
211457 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
211458 const double b = 0.75; /* Constant "b" from BM25 formula */
211459 int rc = SQLITE_OK; /* Error code */
211460 double score = 0.0; /* SQL function return value */
211461 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
211462 int i; /* Iterator variable */
211463 int nInst = 0; /* Value returned by xInstCount() */
211464 double D = 0.0; /* Total number of tokens in row */
211465 double *aFreq = 0; /* Array of phrase freq. for current row */
211466
211467 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
211468 ** for each phrase in the query for the current row. */
211469 rc = fts5Bm25GetData(pApi, pFts, &pData);
211470 if( rc==SQLITE_OK ){
211471 aFreq = pData->aFreq;
211472 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
211473 rc = pApi->xInstCount(pFts, &nInst);
211474 }
211475 for(i=0; rc==SQLITE_OK && i<nInst; i++){
211476 int ip; int ic; int io;
211477 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
211478 if( rc==SQLITE_OK ){
211479 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
211480 aFreq[ip] += w;
211481 }
211482 }
211483
211484 /* Figure out the total size of the current row in tokens. */
211485 if( rc==SQLITE_OK ){
211486 int nTok;
211487 rc = pApi->xColumnSize(pFts, -1, &nTok);
211488 D = (double)nTok;
211489 }
211490
211491 /* Determine the BM25 score for the current row. */
211492 for(i=0; rc==SQLITE_OK && i<pData->nPhrase; i++){
211493 score += pData->aIDF[i] * (
211494 ( aFreq[i] * (k1 + 1.0) ) /
211495 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
211496 );
211497 }
211498
211499 /* If no error has occurred, return the calculated score. Otherwise,
211500 ** throw an SQL exception. */
211501 if( rc==SQLITE_OK ){
211502 sqlite3_result_double(pCtx, -1.0 * score);
211503 }else{
211504 sqlite3_result_error_code(pCtx, rc);
211505 }
211506}
211507
211508static int sqlite3Fts5AuxInit(fts5_api *pApi){
211509 struct Builtin {
211510 const char *zFunc; /* Function name (nul-terminated) */
211511 void *pUserData; /* User-data pointer */
211512 fts5_extension_function xFunc;/* Callback function */
211513 void (*xDestroy)(void*); /* Destructor function */
211514 } aBuiltin [] = {
211515 { "snippet", 0, fts5SnippetFunction, 0 },
211516 { "highlight", 0, fts5HighlightFunction, 0 },
211517 { "bm25", 0, fts5Bm25Function, 0 },
211518 };
211519 int rc = SQLITE_OK; /* Return code */
211520 int i; /* To iterate through builtin functions */
211521
211522 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
211523 rc = pApi->xCreateFunction(pApi,
211524 aBuiltin[i].zFunc,
211525 aBuiltin[i].pUserData,
211526 aBuiltin[i].xFunc,
211527 aBuiltin[i].xDestroy
211528 );
211529 }
211530
211531 return rc;
211532}
211533
211534/*
211535** 2014 May 31
211536**
211537** The author disclaims copyright to this source code. In place of
211538** a legal notice, here is a blessing:
211539**
211540** May you do good and not evil.
211541** May you find forgiveness for yourself and forgive others.
211542** May you share freely, never taking more than you give.
211543**
211544******************************************************************************
211545*/
211546
211547
211548
211549/* #include "fts5Int.h" */
211550
211551static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
211552 if( (u32)pBuf->nSpace<nByte ){
211553 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
211554 u8 *pNew;
211555 while( nNew<nByte ){
211556 nNew = nNew * 2;
211557 }
211558 pNew = sqlite3_realloc64(pBuf->p, nNew);
211559 if( pNew==0 ){
211560 *pRc = SQLITE_NOMEM;
211561 return 1;
211562 }else{
211563 pBuf->nSpace = (int)nNew;
211564 pBuf->p = pNew;
211565 }
211566 }
211567 return 0;
211568}
211569
211570
211571/*
211572** Encode value iVal as an SQLite varint and append it to the buffer object
211573** pBuf. If an OOM error occurs, set the error code in p.
211574*/
211575static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
211576 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
211577 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
211578}
211579
211580static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
211581 aBuf[0] = (iVal>>24) & 0x00FF;
211582 aBuf[1] = (iVal>>16) & 0x00FF;
211583 aBuf[2] = (iVal>> 8) & 0x00FF;
211584 aBuf[3] = (iVal>> 0) & 0x00FF;
211585}
211586
211587static int sqlite3Fts5Get32(const u8 *aBuf){
211588 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
211589}
211590
211591/*
211592** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
211593** the error code in p. If an error has already occurred when this function
211594** is called, it is a no-op.
211595*/
211596static void sqlite3Fts5BufferAppendBlob(
211597 int *pRc,
211598 Fts5Buffer *pBuf,
211599 u32 nData,
211600 const u8 *pData
211601){
211602 assert_nc( *pRc || nData>=0 );
211603 if( nData ){
211604 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
211605 memcpy(&pBuf->p[pBuf->n], pData, nData);
211606 pBuf->n += nData;
211607 }
211608}
211609
211610/*
211611** Append the nul-terminated string zStr to the buffer pBuf. This function
211612** ensures that the byte following the buffer data is set to 0x00, even
211613** though this byte is not included in the pBuf->n count.
211614*/
211615static void sqlite3Fts5BufferAppendString(
211616 int *pRc,
211617 Fts5Buffer *pBuf,
211618 const char *zStr
211619){
211620 int nStr = (int)strlen(zStr);
211621 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
211622 pBuf->n--;
211623}
211624
211625/*
211626** Argument zFmt is a printf() style format string. This function performs
211627** the printf() style processing, then appends the results to buffer pBuf.
211628**
211629** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
211630** following the buffer data is set to 0x00, even though this byte is not
211631** included in the pBuf->n count.
211632*/
211633static void sqlite3Fts5BufferAppendPrintf(
211634 int *pRc,
211635 Fts5Buffer *pBuf,
211636 char *zFmt, ...
211637){
211638 if( *pRc==SQLITE_OK ){
211639 char *zTmp;
211640 va_list ap;
211641 va_start(ap, zFmt);
211642 zTmp = sqlite3_vmprintf(zFmt, ap);
211643 va_end(ap);
211644
211645 if( zTmp==0 ){
211646 *pRc = SQLITE_NOMEM;
211647 }else{
211648 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
211649 sqlite3_free(zTmp);
211650 }
211651 }
211652}
211653
211654static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
211655 char *zRet = 0;
211656 if( *pRc==SQLITE_OK ){
211657 va_list ap;
211658 va_start(ap, zFmt);
211659 zRet = sqlite3_vmprintf(zFmt, ap);
211660 va_end(ap);
211661 if( zRet==0 ){
211662 *pRc = SQLITE_NOMEM;
211663 }
211664 }
211665 return zRet;
211666}
211667
211668
211669/*
211670** Free any buffer allocated by pBuf. Zero the structure before returning.
211671*/
211672static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
211673 sqlite3_free(pBuf->p);
211674 memset(pBuf, 0, sizeof(Fts5Buffer));
211675}
211676
211677/*
211678** Zero the contents of the buffer object. But do not free the associated
211679** memory allocation.
211680*/
211681static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
211682 pBuf->n = 0;
211683}
211684
211685/*
211686** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
211687** the error code in p. If an error has already occurred when this function
211688** is called, it is a no-op.
211689*/
211690static void sqlite3Fts5BufferSet(
211691 int *pRc,
211692 Fts5Buffer *pBuf,
211693 int nData,
211694 const u8 *pData
211695){
211696 pBuf->n = 0;
211697 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
211698}
211699
211700static int sqlite3Fts5PoslistNext64(
211701 const u8 *a, int n, /* Buffer containing poslist */
211702 int *pi, /* IN/OUT: Offset within a[] */
211703 i64 *piOff /* IN/OUT: Current offset */
211704){
211705 int i = *pi;
211706 if( i>=n ){
211707 /* EOF */
211708 *piOff = -1;
211709 return 1;
211710 }else{
211711 i64 iOff = *piOff;
211712 int iVal;
211713 fts5FastGetVarint32(a, i, iVal);
211714 if( iVal<=1 ){
211715 if( iVal==0 ){
211716 *pi = i;
211717 return 0;
211718 }
211719 fts5FastGetVarint32(a, i, iVal);
211720 iOff = ((i64)iVal) << 32;
211721 fts5FastGetVarint32(a, i, iVal);
211722 if( iVal<2 ){
211723 /* This is a corrupt record. So stop parsing it here. */
211724 *piOff = -1;
211725 return 1;
211726 }
211727 }
211728 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
211729 *pi = i;
211730 return 0;
211731 }
211732}
211733
211734
211735/*
211736** Advance the iterator object passed as the only argument. Return true
211737** if the iterator reaches EOF, or false otherwise.
211738*/
211739static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
211740 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
211741 pIter->bEof = 1;
211742 }
211743 return pIter->bEof;
211744}
211745
211746static int sqlite3Fts5PoslistReaderInit(
211747 const u8 *a, int n, /* Poslist buffer to iterate through */
211748 Fts5PoslistReader *pIter /* Iterator object to initialize */
211749){
211750 memset(pIter, 0, sizeof(*pIter));
211751 pIter->a = a;
211752 pIter->n = n;
211753 sqlite3Fts5PoslistReaderNext(pIter);
211754 return pIter->bEof;
211755}
211756
211757/*
211758** Append position iPos to the position list being accumulated in buffer
211759** pBuf, which must be already be large enough to hold the new data.
211760** The previous position written to this list is *piPrev. *piPrev is set
211761** to iPos before returning.
211762*/
211763static void sqlite3Fts5PoslistSafeAppend(
211764 Fts5Buffer *pBuf,
211765 i64 *piPrev,
211766 i64 iPos
211767){
211768 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
211769 if( (iPos & colmask) != (*piPrev & colmask) ){
211770 pBuf->p[pBuf->n++] = 1;
211771 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
211772 *piPrev = (iPos & colmask);
211773 }
211774 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
211775 *piPrev = iPos;
211776}
211777
211778static int sqlite3Fts5PoslistWriterAppend(
211779 Fts5Buffer *pBuf,
211780 Fts5PoslistWriter *pWriter,
211781 i64 iPos
211782){
211783 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
211784 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
211785 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
211786 return SQLITE_OK;
211787}
211788
211789static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
211790 void *pRet = 0;
211791 if( *pRc==SQLITE_OK ){
211792 pRet = sqlite3_malloc64(nByte);
211793 if( pRet==0 ){
211794 if( nByte>0 ) *pRc = SQLITE_NOMEM;
211795 }else{
211796 memset(pRet, 0, (size_t)nByte);
211797 }
211798 }
211799 return pRet;
211800}
211801
211802/*
211803** Return a nul-terminated copy of the string indicated by pIn. If nIn
211804** is non-negative, then it is the length of the string in bytes. Otherwise,
211805** the length of the string is determined using strlen().
211806**
211807** It is the responsibility of the caller to eventually free the returned
211808** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
211809*/
211810static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
211811 char *zRet = 0;
211812 if( *pRc==SQLITE_OK ){
211813 if( nIn<0 ){
211814 nIn = (int)strlen(pIn);
211815 }
211816 zRet = (char*)sqlite3_malloc(nIn+1);
211817 if( zRet ){
211818 memcpy(zRet, pIn, nIn);
211819 zRet[nIn] = '\0';
211820 }else{
211821 *pRc = SQLITE_NOMEM;
211822 }
211823 }
211824 return zRet;
211825}
211826
211827
211828/*
211829** Return true if character 't' may be part of an FTS5 bareword, or false
211830** otherwise. Characters that may be part of barewords:
211831**
211832** * All non-ASCII characters,
211833** * The 52 upper and lower case ASCII characters, and
211834** * The 10 integer ASCII characters.
211835** * The underscore character "_" (0x5F).
211836** * The unicode "subsitute" character (0x1A).
211837*/
211838static int sqlite3Fts5IsBareword(char t){
211839 u8 aBareword[128] = {
211840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
211841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
211842 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
211843 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
211844 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
211845 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
211846 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
211847 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
211848 };
211849
211850 return (t & 0x80) || aBareword[(int)t];
211851}
211852
211853
211854/*************************************************************************
211855*/
211856typedef struct Fts5TermsetEntry Fts5TermsetEntry;
211857struct Fts5TermsetEntry {
211858 char *pTerm;
211859 int nTerm;
211860 int iIdx; /* Index (main or aPrefix[] entry) */
211861 Fts5TermsetEntry *pNext;
211862};
211863
211864struct Fts5Termset {
211865 Fts5TermsetEntry *apHash[512];
211866};
211867
211868static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
211869 int rc = SQLITE_OK;
211870 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
211871 return rc;
211872}
211873
211874static int sqlite3Fts5TermsetAdd(
211875 Fts5Termset *p,
211876 int iIdx,
211877 const char *pTerm, int nTerm,
211878 int *pbPresent
211879){
211880 int rc = SQLITE_OK;
211881 *pbPresent = 0;
211882 if( p ){
211883 int i;
211884 u32 hash = 13;
211885 Fts5TermsetEntry *pEntry;
211886
211887 /* Calculate a hash value for this term. This is the same hash checksum
211888 ** used by the fts5_hash.c module. This is not important for correct
211889 ** operation of the module, but is necessary to ensure that some tests
211890 ** designed to produce hash table collisions really do work. */
211891 for(i=nTerm-1; i>=0; i--){
211892 hash = (hash << 3) ^ hash ^ pTerm[i];
211893 }
211894 hash = (hash << 3) ^ hash ^ iIdx;
211895 hash = hash % ArraySize(p->apHash);
211896
211897 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
211898 if( pEntry->iIdx==iIdx
211899 && pEntry->nTerm==nTerm
211900 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
211901 ){
211902 *pbPresent = 1;
211903 break;
211904 }
211905 }
211906
211907 if( pEntry==0 ){
211908 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
211909 if( pEntry ){
211910 pEntry->pTerm = (char*)&pEntry[1];
211911 pEntry->nTerm = nTerm;
211912 pEntry->iIdx = iIdx;
211913 memcpy(pEntry->pTerm, pTerm, nTerm);
211914 pEntry->pNext = p->apHash[hash];
211915 p->apHash[hash] = pEntry;
211916 }
211917 }
211918 }
211919
211920 return rc;
211921}
211922
211923static void sqlite3Fts5TermsetFree(Fts5Termset *p){
211924 if( p ){
211925 u32 i;
211926 for(i=0; i<ArraySize(p->apHash); i++){
211927 Fts5TermsetEntry *pEntry = p->apHash[i];
211928 while( pEntry ){
211929 Fts5TermsetEntry *pDel = pEntry;
211930 pEntry = pEntry->pNext;
211931 sqlite3_free(pDel);
211932 }
211933 }
211934 sqlite3_free(p);
211935 }
211936}
211937
211938/*
211939** 2014 Jun 09
211940**
211941** The author disclaims copyright to this source code. In place of
211942** a legal notice, here is a blessing:
211943**
211944** May you do good and not evil.
211945** May you find forgiveness for yourself and forgive others.
211946** May you share freely, never taking more than you give.
211947**
211948******************************************************************************
211949**
211950** This is an SQLite module implementing full-text search.
211951*/
211952
211953
211954/* #include "fts5Int.h" */
211955
211956#define FTS5_DEFAULT_PAGE_SIZE 4050
211957#define FTS5_DEFAULT_AUTOMERGE 4
211958#define FTS5_DEFAULT_USERMERGE 4
211959#define FTS5_DEFAULT_CRISISMERGE 16
211960#define FTS5_DEFAULT_HASHSIZE (1024*1024)
211961
211962/* Maximum allowed page size */
211963#define FTS5_MAX_PAGE_SIZE (64*1024)
211964
211965static int fts5_iswhitespace(char x){
211966 return (x==' ');
211967}
211968
211969static int fts5_isopenquote(char x){
211970 return (x=='"' || x=='\'' || x=='[' || x=='`');
211971}
211972
211973/*
211974** Argument pIn points to a character that is part of a nul-terminated
211975** string. Return a pointer to the first character following *pIn in
211976** the string that is not a white-space character.
211977*/
211978static const char *fts5ConfigSkipWhitespace(const char *pIn){
211979 const char *p = pIn;
211980 if( p ){
211981 while( fts5_iswhitespace(*p) ){ p++; }
211982 }
211983 return p;
211984}
211985
211986/*
211987** Argument pIn points to a character that is part of a nul-terminated
211988** string. Return a pointer to the first character following *pIn in
211989** the string that is not a "bareword" character.
211990*/
211991static const char *fts5ConfigSkipBareword(const char *pIn){
211992 const char *p = pIn;
211993 while ( sqlite3Fts5IsBareword(*p) ) p++;
211994 if( p==pIn ) p = 0;
211995 return p;
211996}
211997
211998static int fts5_isdigit(char a){
211999 return (a>='0' && a<='9');
212000}
212001
212002
212003
212004static const char *fts5ConfigSkipLiteral(const char *pIn){
212005 const char *p = pIn;
212006 switch( *p ){
212007 case 'n': case 'N':
212008 if( sqlite3_strnicmp("null", p, 4)==0 ){
212009 p = &p[4];
212010 }else{
212011 p = 0;
212012 }
212013 break;
212014
212015 case 'x': case 'X':
212016 p++;
212017 if( *p=='\'' ){
212018 p++;
212019 while( (*p>='a' && *p<='f')
212020 || (*p>='A' && *p<='F')
212021 || (*p>='0' && *p<='9')
212022 ){
212023 p++;
212024 }
212025 if( *p=='\'' && 0==((p-pIn)%2) ){
212026 p++;
212027 }else{
212028 p = 0;
212029 }
212030 }else{
212031 p = 0;
212032 }
212033 break;
212034
212035 case '\'':
212036 p++;
212037 while( p ){
212038 if( *p=='\'' ){
212039 p++;
212040 if( *p!='\'' ) break;
212041 }
212042 p++;
212043 if( *p==0 ) p = 0;
212044 }
212045 break;
212046
212047 default:
212048 /* maybe a number */
212049 if( *p=='+' || *p=='-' ) p++;
212050 while( fts5_isdigit(*p) ) p++;
212051
212052 /* At this point, if the literal was an integer, the parse is
212053 ** finished. Or, if it is a floating point value, it may continue
212054 ** with either a decimal point or an 'E' character. */
212055 if( *p=='.' && fts5_isdigit(p[1]) ){
212056 p += 2;
212057 while( fts5_isdigit(*p) ) p++;
212058 }
212059 if( p==pIn ) p = 0;
212060
212061 break;
212062 }
212063
212064 return p;
212065}
212066
212067/*
212068** The first character of the string pointed to by argument z is guaranteed
212069** to be an open-quote character (see function fts5_isopenquote()).
212070**
212071** This function searches for the corresponding close-quote character within
212072** the string and, if found, dequotes the string in place and adds a new
212073** nul-terminator byte.
212074**
212075** If the close-quote is found, the value returned is the byte offset of
212076** the character immediately following it. Or, if the close-quote is not
212077** found, -1 is returned. If -1 is returned, the buffer is left in an
212078** undefined state.
212079*/
212080static int fts5Dequote(char *z){
212081 char q;
212082 int iIn = 1;
212083 int iOut = 0;
212084 q = z[0];
212085
212086 /* Set stack variable q to the close-quote character */
212087 assert( q=='[' || q=='\'' || q=='"' || q=='`' );
212088 if( q=='[' ) q = ']';
212089
212090 while( z[iIn] ){
212091 if( z[iIn]==q ){
212092 if( z[iIn+1]!=q ){
212093 /* Character iIn was the close quote. */
212094 iIn++;
212095 break;
212096 }else{
212097 /* Character iIn and iIn+1 form an escaped quote character. Skip
212098 ** the input cursor past both and copy a single quote character
212099 ** to the output buffer. */
212100 iIn += 2;
212101 z[iOut++] = q;
212102 }
212103 }else{
212104 z[iOut++] = z[iIn++];
212105 }
212106 }
212107
212108 z[iOut] = '\0';
212109 return iIn;
212110}
212111
212112/*
212113** Convert an SQL-style quoted string into a normal string by removing
212114** the quote characters. The conversion is done in-place. If the
212115** input does not begin with a quote character, then this routine
212116** is a no-op.
212117**
212118** Examples:
212119**
212120** "abc" becomes abc
212121** 'xyz' becomes xyz
212122** [pqr] becomes pqr
212123** `mno` becomes mno
212124*/
212125static void sqlite3Fts5Dequote(char *z){
212126 char quote; /* Quote character (if any ) */
212127
212128 assert( 0==fts5_iswhitespace(z[0]) );
212129 quote = z[0];
212130 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
212131 fts5Dequote(z);
212132 }
212133}
212134
212135
212136struct Fts5Enum {
212137 const char *zName;
212138 int eVal;
212139};
212140typedef struct Fts5Enum Fts5Enum;
212141
212142static int fts5ConfigSetEnum(
212143 const Fts5Enum *aEnum,
212144 const char *zEnum,
212145 int *peVal
212146){
212147 int nEnum = (int)strlen(zEnum);
212148 int i;
212149 int iVal = -1;
212150
212151 for(i=0; aEnum[i].zName; i++){
212152 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
212153 if( iVal>=0 ) return SQLITE_ERROR;
212154 iVal = aEnum[i].eVal;
212155 }
212156 }
212157
212158 *peVal = iVal;
212159 return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
212160}
212161
212162/*
212163** Parse a "special" CREATE VIRTUAL TABLE directive and update
212164** configuration object pConfig as appropriate.
212165**
212166** If successful, object pConfig is updated and SQLITE_OK returned. If
212167** an error occurs, an SQLite error code is returned and an error message
212168** may be left in *pzErr. It is the responsibility of the caller to
212169** eventually free any such error message using sqlite3_free().
212170*/
212171static int fts5ConfigParseSpecial(
212172 Fts5Global *pGlobal,
212173 Fts5Config *pConfig, /* Configuration object to update */
212174 const char *zCmd, /* Special command to parse */
212175 const char *zArg, /* Argument to parse */
212176 char **pzErr /* OUT: Error message */
212177){
212178 int rc = SQLITE_OK;
212179 int nCmd = (int)strlen(zCmd);
212180 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
212181 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
212182 const char *p;
212183 int bFirst = 1;
212184 if( pConfig->aPrefix==0 ){
212185 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
212186 if( rc ) return rc;
212187 }
212188
212189 p = zArg;
212190 while( 1 ){
212191 int nPre = 0;
212192
212193 while( p[0]==' ' ) p++;
212194 if( bFirst==0 && p[0]==',' ){
212195 p++;
212196 while( p[0]==' ' ) p++;
212197 }else if( p[0]=='\0' ){
212198 break;
212199 }
212200 if( p[0]<'0' || p[0]>'9' ){
212201 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
212202 rc = SQLITE_ERROR;
212203 break;
212204 }
212205
212206 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
212207 *pzErr = sqlite3_mprintf(
212208 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
212209 );
212210 rc = SQLITE_ERROR;
212211 break;
212212 }
212213
212214 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
212215 nPre = nPre*10 + (p[0] - '0');
212216 p++;
212217 }
212218
212219 if( nPre<=0 || nPre>=1000 ){
212220 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
212221 rc = SQLITE_ERROR;
212222 break;
212223 }
212224
212225 pConfig->aPrefix[pConfig->nPrefix] = nPre;
212226 pConfig->nPrefix++;
212227 bFirst = 0;
212228 }
212229 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
212230 return rc;
212231 }
212232
212233 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
212234 const char *p = (const char*)zArg;
212235 sqlite3_int64 nArg = strlen(zArg) + 1;
212236 char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
212237 char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
212238 char *pSpace = pDel;
212239
212240 if( azArg && pSpace ){
212241 if( pConfig->pTok ){
212242 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
212243 rc = SQLITE_ERROR;
212244 }else{
212245 for(nArg=0; p && *p; nArg++){
212246 const char *p2 = fts5ConfigSkipWhitespace(p);
212247 if( *p2=='\'' ){
212248 p = fts5ConfigSkipLiteral(p2);
212249 }else{
212250 p = fts5ConfigSkipBareword(p2);
212251 }
212252 if( p ){
212253 memcpy(pSpace, p2, p-p2);
212254 azArg[nArg] = pSpace;
212255 sqlite3Fts5Dequote(pSpace);
212256 pSpace += (p - p2) + 1;
212257 p = fts5ConfigSkipWhitespace(p);
212258 }
212259 }
212260 if( p==0 ){
212261 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
212262 rc = SQLITE_ERROR;
212263 }else{
212264 rc = sqlite3Fts5GetTokenizer(pGlobal,
212265 (const char**)azArg, (int)nArg, &pConfig->pTok, &pConfig->pTokApi,
212266 pzErr
212267 );
212268 }
212269 }
212270 }
212271
212272 sqlite3_free(azArg);
212273 sqlite3_free(pDel);
212274 return rc;
212275 }
212276
212277 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
212278 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
212279 *pzErr = sqlite3_mprintf("multiple content=... directives");
212280 rc = SQLITE_ERROR;
212281 }else{
212282 if( zArg[0] ){
212283 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
212284 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
212285 }else{
212286 pConfig->eContent = FTS5_CONTENT_NONE;
212287 }
212288 }
212289 return rc;
212290 }
212291
212292 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
212293 if( pConfig->zContentRowid ){
212294 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
212295 rc = SQLITE_ERROR;
212296 }else{
212297 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
212298 }
212299 return rc;
212300 }
212301
212302 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
212303 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
212304 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
212305 rc = SQLITE_ERROR;
212306 }else{
212307 pConfig->bColumnsize = (zArg[0]=='1');
212308 }
212309 return rc;
212310 }
212311
212312 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
212313 const Fts5Enum aDetail[] = {
212314 { "none", FTS5_DETAIL_NONE },
212315 { "full", FTS5_DETAIL_FULL },
212316 { "columns", FTS5_DETAIL_COLUMNS },
212317 { 0, 0 }
212318 };
212319
212320 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
212321 *pzErr = sqlite3_mprintf("malformed detail=... directive");
212322 }
212323 return rc;
212324 }
212325
212326 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
212327 return SQLITE_ERROR;
212328}
212329
212330/*
212331** Allocate an instance of the default tokenizer ("simple") at
212332** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
212333** code if an error occurs.
212334*/
212335static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
212336 assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
212337 return sqlite3Fts5GetTokenizer(
212338 pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
212339 );
212340}
212341
212342/*
212343** Gobble up the first bareword or quoted word from the input buffer zIn.
212344** Return a pointer to the character immediately following the last in
212345** the gobbled word if successful, or a NULL pointer otherwise (failed
212346** to find close-quote character).
212347**
212348** Before returning, set pzOut to point to a new buffer containing a
212349** nul-terminated, dequoted copy of the gobbled word. If the word was
212350** quoted, *pbQuoted is also set to 1 before returning.
212351**
212352** If *pRc is other than SQLITE_OK when this function is called, it is
212353** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
212354** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
212355** set if a parse error (failed to find close quote) occurs.
212356*/
212357static const char *fts5ConfigGobbleWord(
212358 int *pRc, /* IN/OUT: Error code */
212359 const char *zIn, /* Buffer to gobble string/bareword from */
212360 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
212361 int *pbQuoted /* OUT: Set to true if dequoting required */
212362){
212363 const char *zRet = 0;
212364
212365 sqlite3_int64 nIn = strlen(zIn);
212366 char *zOut = sqlite3_malloc64(nIn+1);
212367
212368 assert( *pRc==SQLITE_OK );
212369 *pbQuoted = 0;
212370 *pzOut = 0;
212371
212372 if( zOut==0 ){
212373 *pRc = SQLITE_NOMEM;
212374 }else{
212375 memcpy(zOut, zIn, (size_t)(nIn+1));
212376 if( fts5_isopenquote(zOut[0]) ){
212377 int ii = fts5Dequote(zOut);
212378 zRet = &zIn[ii];
212379 *pbQuoted = 1;
212380 }else{
212381 zRet = fts5ConfigSkipBareword(zIn);
212382 if( zRet ){
212383 zOut[zRet-zIn] = '\0';
212384 }
212385 }
212386 }
212387
212388 if( zRet==0 ){
212389 sqlite3_free(zOut);
212390 }else{
212391 *pzOut = zOut;
212392 }
212393
212394 return zRet;
212395}
212396
212397static int fts5ConfigParseColumn(
212398 Fts5Config *p,
212399 char *zCol,
212400 char *zArg,
212401 char **pzErr
212402){
212403 int rc = SQLITE_OK;
212404 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
212405 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
212406 ){
212407 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
212408 rc = SQLITE_ERROR;
212409 }else if( zArg ){
212410 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
212411 p->abUnindexed[p->nCol] = 1;
212412 }else{
212413 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
212414 rc = SQLITE_ERROR;
212415 }
212416 }
212417
212418 p->azCol[p->nCol++] = zCol;
212419 return rc;
212420}
212421
212422/*
212423** Populate the Fts5Config.zContentExprlist string.
212424*/
212425static int fts5ConfigMakeExprlist(Fts5Config *p){
212426 int i;
212427 int rc = SQLITE_OK;
212428 Fts5Buffer buf = {0, 0, 0};
212429
212430 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
212431 if( p->eContent!=FTS5_CONTENT_NONE ){
212432 for(i=0; i<p->nCol; i++){
212433 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
212434 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
212435 }else{
212436 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
212437 }
212438 }
212439 }
212440
212441 assert( p->zContentExprlist==0 );
212442 p->zContentExprlist = (char*)buf.p;
212443 return rc;
212444}
212445
212446/*
212447** Arguments nArg/azArg contain the string arguments passed to the xCreate
212448** or xConnect method of the virtual table. This function attempts to
212449** allocate an instance of Fts5Config containing the results of parsing
212450** those arguments.
212451**
212452** If successful, SQLITE_OK is returned and *ppOut is set to point to the
212453** new Fts5Config object. If an error occurs, an SQLite error code is
212454** returned, *ppOut is set to NULL and an error message may be left in
212455** *pzErr. It is the responsibility of the caller to eventually free any
212456** such error message using sqlite3_free().
212457*/
212458static int sqlite3Fts5ConfigParse(
212459 Fts5Global *pGlobal,
212460 sqlite3 *db,
212461 int nArg, /* Number of arguments */
212462 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
212463 Fts5Config **ppOut, /* OUT: Results of parse */
212464 char **pzErr /* OUT: Error message */
212465){
212466 int rc = SQLITE_OK; /* Return code */
212467 Fts5Config *pRet; /* New object to return */
212468 int i;
212469 sqlite3_int64 nByte;
212470
212471 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
212472 if( pRet==0 ) return SQLITE_NOMEM;
212473 memset(pRet, 0, sizeof(Fts5Config));
212474 pRet->db = db;
212475 pRet->iCookie = -1;
212476
212477 nByte = nArg * (sizeof(char*) + sizeof(u8));
212478 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
212479 pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
212480 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
212481 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
212482 pRet->bColumnsize = 1;
212483 pRet->eDetail = FTS5_DETAIL_FULL;
212484#ifdef SQLITE_DEBUG
212485 pRet->bPrefixIndex = 1;
212486#endif
212487 if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
212488 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
212489 rc = SQLITE_ERROR;
212490 }
212491
212492 for(i=3; rc==SQLITE_OK && i<nArg; i++){
212493 const char *zOrig = azArg[i];
212494 const char *z;
212495 char *zOne = 0;
212496 char *zTwo = 0;
212497 int bOption = 0;
212498 int bMustBeCol = 0;
212499
212500 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
212501 z = fts5ConfigSkipWhitespace(z);
212502 if( z && *z=='=' ){
212503 bOption = 1;
212504 z++;
212505 if( bMustBeCol ) z = 0;
212506 }
212507 z = fts5ConfigSkipWhitespace(z);
212508 if( z && z[0] ){
212509 int bDummy;
212510 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
212511 if( z && z[0] ) z = 0;
212512 }
212513
212514 if( rc==SQLITE_OK ){
212515 if( z==0 ){
212516 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
212517 rc = SQLITE_ERROR;
212518 }else{
212519 if( bOption ){
212520 rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
212521 }else{
212522 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
212523 zOne = 0;
212524 }
212525 }
212526 }
212527
212528 sqlite3_free(zOne);
212529 sqlite3_free(zTwo);
212530 }
212531
212532 /* If a tokenizer= option was successfully parsed, the tokenizer has
212533 ** already been allocated. Otherwise, allocate an instance of the default
212534 ** tokenizer (unicode61) now. */
212535 if( rc==SQLITE_OK && pRet->pTok==0 ){
212536 rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
212537 }
212538
212539 /* If no zContent option was specified, fill in the default values. */
212540 if( rc==SQLITE_OK && pRet->zContent==0 ){
212541 const char *zTail = 0;
212542 assert( pRet->eContent==FTS5_CONTENT_NORMAL
212543 || pRet->eContent==FTS5_CONTENT_NONE
212544 );
212545 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
212546 zTail = "content";
212547 }else if( pRet->bColumnsize ){
212548 zTail = "docsize";
212549 }
212550
212551 if( zTail ){
212552 pRet->zContent = sqlite3Fts5Mprintf(
212553 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
212554 );
212555 }
212556 }
212557
212558 if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
212559 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
212560 }
212561
212562 /* Formulate the zContentExprlist text */
212563 if( rc==SQLITE_OK ){
212564 rc = fts5ConfigMakeExprlist(pRet);
212565 }
212566
212567 if( rc!=SQLITE_OK ){
212568 sqlite3Fts5ConfigFree(pRet);
212569 *ppOut = 0;
212570 }
212571 return rc;
212572}
212573
212574/*
212575** Free the configuration object passed as the only argument.
212576*/
212577static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
212578 if( pConfig ){
212579 int i;
212580 if( pConfig->pTok ){
212581 pConfig->pTokApi->xDelete(pConfig->pTok);
212582 }
212583 sqlite3_free(pConfig->zDb);
212584 sqlite3_free(pConfig->zName);
212585 for(i=0; i<pConfig->nCol; i++){
212586 sqlite3_free(pConfig->azCol[i]);
212587 }
212588 sqlite3_free(pConfig->azCol);
212589 sqlite3_free(pConfig->aPrefix);
212590 sqlite3_free(pConfig->zRank);
212591 sqlite3_free(pConfig->zRankArgs);
212592 sqlite3_free(pConfig->zContent);
212593 sqlite3_free(pConfig->zContentRowid);
212594 sqlite3_free(pConfig->zContentExprlist);
212595 sqlite3_free(pConfig);
212596 }
212597}
212598
212599/*
212600** Call sqlite3_declare_vtab() based on the contents of the configuration
212601** object passed as the only argument. Return SQLITE_OK if successful, or
212602** an SQLite error code if an error occurs.
212603*/
212604static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
212605 int i;
212606 int rc = SQLITE_OK;
212607 char *zSql;
212608
212609 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
212610 for(i=0; zSql && i<pConfig->nCol; i++){
212611 const char *zSep = (i==0?"":", ");
212612 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
212613 }
212614 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
212615 zSql, pConfig->zName, FTS5_RANK_NAME
212616 );
212617
212618 assert( zSql || rc==SQLITE_NOMEM );
212619 if( zSql ){
212620 rc = sqlite3_declare_vtab(pConfig->db, zSql);
212621 sqlite3_free(zSql);
212622 }
212623
212624 return rc;
212625}
212626
212627/*
212628** Tokenize the text passed via the second and third arguments.
212629**
212630** The callback is invoked once for each token in the input text. The
212631** arguments passed to it are, in order:
212632**
212633** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
212634** const char *pToken // Pointer to buffer containing token
212635** int nToken // Size of token in bytes
212636** int iStart // Byte offset of start of token within input text
212637** int iEnd // Byte offset of end of token within input text
212638** int iPos // Position of token in input (first token is 0)
212639**
212640** If the callback returns a non-zero value the tokenization is abandoned
212641** and no further callbacks are issued.
212642**
212643** This function returns SQLITE_OK if successful or an SQLite error code
212644** if an error occurs. If the tokenization was abandoned early because
212645** the callback returned SQLITE_DONE, this is not an error and this function
212646** still returns SQLITE_OK. Or, if the tokenization was abandoned early
212647** because the callback returned another non-zero value, it is assumed
212648** to be an SQLite error code and returned to the caller.
212649*/
212650static int sqlite3Fts5Tokenize(
212651 Fts5Config *pConfig, /* FTS5 Configuration object */
212652 int flags, /* FTS5_TOKENIZE_* flags */
212653 const char *pText, int nText, /* Text to tokenize */
212654 void *pCtx, /* Context passed to xToken() */
212655 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
212656){
212657 if( pText==0 ) return SQLITE_OK;
212658 return pConfig->pTokApi->xTokenize(
212659 pConfig->pTok, pCtx, flags, pText, nText, xToken
212660 );
212661}
212662
212663/*
212664** Argument pIn points to the first character in what is expected to be
212665** a comma-separated list of SQL literals followed by a ')' character.
212666** If it actually is this, return a pointer to the ')'. Otherwise, return
212667** NULL to indicate a parse error.
212668*/
212669static const char *fts5ConfigSkipArgs(const char *pIn){
212670 const char *p = pIn;
212671
212672 while( 1 ){
212673 p = fts5ConfigSkipWhitespace(p);
212674 p = fts5ConfigSkipLiteral(p);
212675 p = fts5ConfigSkipWhitespace(p);
212676 if( p==0 || *p==')' ) break;
212677 if( *p!=',' ){
212678 p = 0;
212679 break;
212680 }
212681 p++;
212682 }
212683
212684 return p;
212685}
212686
212687/*
212688** Parameter zIn contains a rank() function specification. The format of
212689** this is:
212690**
212691** + Bareword (function name)
212692** + Open parenthesis - "("
212693** + Zero or more SQL literals in a comma separated list
212694** + Close parenthesis - ")"
212695*/
212696static int sqlite3Fts5ConfigParseRank(
212697 const char *zIn, /* Input string */
212698 char **pzRank, /* OUT: Rank function name */
212699 char **pzRankArgs /* OUT: Rank function arguments */
212700){
212701 const char *p = zIn;
212702 const char *pRank;
212703 char *zRank = 0;
212704 char *zRankArgs = 0;
212705 int rc = SQLITE_OK;
212706
212707 *pzRank = 0;
212708 *pzRankArgs = 0;
212709
212710 if( p==0 ){
212711 rc = SQLITE_ERROR;
212712 }else{
212713 p = fts5ConfigSkipWhitespace(p);
212714 pRank = p;
212715 p = fts5ConfigSkipBareword(p);
212716
212717 if( p ){
212718 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
212719 if( zRank ) memcpy(zRank, pRank, p-pRank);
212720 }else{
212721 rc = SQLITE_ERROR;
212722 }
212723
212724 if( rc==SQLITE_OK ){
212725 p = fts5ConfigSkipWhitespace(p);
212726 if( *p!='(' ) rc = SQLITE_ERROR;
212727 p++;
212728 }
212729 if( rc==SQLITE_OK ){
212730 const char *pArgs;
212731 p = fts5ConfigSkipWhitespace(p);
212732 pArgs = p;
212733 if( *p!=')' ){
212734 p = fts5ConfigSkipArgs(p);
212735 if( p==0 ){
212736 rc = SQLITE_ERROR;
212737 }else{
212738 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
212739 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
212740 }
212741 }
212742 }
212743 }
212744
212745 if( rc!=SQLITE_OK ){
212746 sqlite3_free(zRank);
212747 assert( zRankArgs==0 );
212748 }else{
212749 *pzRank = zRank;
212750 *pzRankArgs = zRankArgs;
212751 }
212752 return rc;
212753}
212754
212755static int sqlite3Fts5ConfigSetValue(
212756 Fts5Config *pConfig,
212757 const char *zKey,
212758 sqlite3_value *pVal,
212759 int *pbBadkey
212760){
212761 int rc = SQLITE_OK;
212762
212763 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
212764 int pgsz = 0;
212765 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212766 pgsz = sqlite3_value_int(pVal);
212767 }
212768 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){
212769 *pbBadkey = 1;
212770 }else{
212771 pConfig->pgsz = pgsz;
212772 }
212773 }
212774
212775 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
212776 int nHashSize = -1;
212777 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212778 nHashSize = sqlite3_value_int(pVal);
212779 }
212780 if( nHashSize<=0 ){
212781 *pbBadkey = 1;
212782 }else{
212783 pConfig->nHashSize = nHashSize;
212784 }
212785 }
212786
212787 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
212788 int nAutomerge = -1;
212789 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212790 nAutomerge = sqlite3_value_int(pVal);
212791 }
212792 if( nAutomerge<0 || nAutomerge>64 ){
212793 *pbBadkey = 1;
212794 }else{
212795 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
212796 pConfig->nAutomerge = nAutomerge;
212797 }
212798 }
212799
212800 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
212801 int nUsermerge = -1;
212802 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212803 nUsermerge = sqlite3_value_int(pVal);
212804 }
212805 if( nUsermerge<2 || nUsermerge>16 ){
212806 *pbBadkey = 1;
212807 }else{
212808 pConfig->nUsermerge = nUsermerge;
212809 }
212810 }
212811
212812 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
212813 int nCrisisMerge = -1;
212814 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
212815 nCrisisMerge = sqlite3_value_int(pVal);
212816 }
212817 if( nCrisisMerge<0 ){
212818 *pbBadkey = 1;
212819 }else{
212820 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
212821 if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1;
212822 pConfig->nCrisisMerge = nCrisisMerge;
212823 }
212824 }
212825
212826 else if( 0==sqlite3_stricmp(zKey, "rank") ){
212827 const char *zIn = (const char*)sqlite3_value_text(pVal);
212828 char *zRank;
212829 char *zRankArgs;
212830 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
212831 if( rc==SQLITE_OK ){
212832 sqlite3_free(pConfig->zRank);
212833 sqlite3_free(pConfig->zRankArgs);
212834 pConfig->zRank = zRank;
212835 pConfig->zRankArgs = zRankArgs;
212836 }else if( rc==SQLITE_ERROR ){
212837 rc = SQLITE_OK;
212838 *pbBadkey = 1;
212839 }
212840 }else{
212841 *pbBadkey = 1;
212842 }
212843 return rc;
212844}
212845
212846/*
212847** Load the contents of the %_config table into memory.
212848*/
212849static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
212850 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
212851 char *zSql;
212852 sqlite3_stmt *p = 0;
212853 int rc = SQLITE_OK;
212854 int iVersion = 0;
212855
212856 /* Set default values */
212857 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
212858 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
212859 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
212860 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
212861 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
212862
212863 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
212864 if( zSql ){
212865 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
212866 sqlite3_free(zSql);
212867 }
212868
212869 assert( rc==SQLITE_OK || p==0 );
212870 if( rc==SQLITE_OK ){
212871 while( SQLITE_ROW==sqlite3_step(p) ){
212872 const char *zK = (const char*)sqlite3_column_text(p, 0);
212873 sqlite3_value *pVal = sqlite3_column_value(p, 1);
212874 if( 0==sqlite3_stricmp(zK, "version") ){
212875 iVersion = sqlite3_value_int(pVal);
212876 }else{
212877 int bDummy = 0;
212878 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
212879 }
212880 }
212881 rc = sqlite3_finalize(p);
212882 }
212883
212884 if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
212885 rc = SQLITE_ERROR;
212886 if( pConfig->pzErrmsg ){
212887 assert( 0==*pConfig->pzErrmsg );
212888 *pConfig->pzErrmsg = sqlite3_mprintf(
212889 "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
212890 iVersion, FTS5_CURRENT_VERSION
212891 );
212892 }
212893 }
212894
212895 if( rc==SQLITE_OK ){
212896 pConfig->iCookie = iCookie;
212897 }
212898 return rc;
212899}
212900
212901/*
212902** 2014 May 31
212903**
212904** The author disclaims copyright to this source code. In place of
212905** a legal notice, here is a blessing:
212906**
212907** May you do good and not evil.
212908** May you find forgiveness for yourself and forgive others.
212909** May you share freely, never taking more than you give.
212910**
212911******************************************************************************
212912**
212913*/
212914
212915
212916
212917/* #include "fts5Int.h" */
212918/* #include "fts5parse.h" */
212919
212920/*
212921** All token types in the generated fts5parse.h file are greater than 0.
212922*/
212923#define FTS5_EOF 0
212924
212925#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
212926
212927typedef struct Fts5ExprTerm Fts5ExprTerm;
212928
212929/*
212930** Functions generated by lemon from fts5parse.y.
212931*/
212932static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
212933static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
212934static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
212935#ifndef NDEBUG
212936/* #include <stdio.h> */
212937static void sqlite3Fts5ParserTrace(FILE*, char*);
212938#endif
212939static int sqlite3Fts5ParserFallback(int);
212940
212941
212942struct Fts5Expr {
212943 Fts5Index *pIndex;
212944 Fts5Config *pConfig;
212945 Fts5ExprNode *pRoot;
212946 int bDesc; /* Iterate in descending rowid order */
212947 int nPhrase; /* Number of phrases in expression */
212948 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
212949};
212950
212951/*
212952** eType:
212953** Expression node type. Always one of:
212954**
212955** FTS5_AND (nChild, apChild valid)
212956** FTS5_OR (nChild, apChild valid)
212957** FTS5_NOT (nChild, apChild valid)
212958** FTS5_STRING (pNear valid)
212959** FTS5_TERM (pNear valid)
212960*/
212961struct Fts5ExprNode {
212962 int eType; /* Node type */
212963 int bEof; /* True at EOF */
212964 int bNomatch; /* True if entry is not a match */
212965
212966 /* Next method for this node. */
212967 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
212968
212969 i64 iRowid; /* Current rowid */
212970 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
212971
212972 /* Child nodes. For a NOT node, this array always contains 2 entries. For
212973 ** AND or OR nodes, it contains 2 or more entries. */
212974 int nChild; /* Number of child nodes */
212975 Fts5ExprNode *apChild[1]; /* Array of child nodes */
212976};
212977
212978#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
212979
212980/*
212981** Invoke the xNext method of an Fts5ExprNode object. This macro should be
212982** used as if it has the same signature as the xNext() methods themselves.
212983*/
212984#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
212985
212986/*
212987** An instance of the following structure represents a single search term
212988** or term prefix.
212989*/
212990struct Fts5ExprTerm {
212991 u8 bPrefix; /* True for a prefix term */
212992 u8 bFirst; /* True if token must be first in column */
212993 char *zTerm; /* nul-terminated term */
212994 Fts5IndexIter *pIter; /* Iterator for this term */
212995 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
212996};
212997
212998/*
212999** A phrase. One or more terms that must appear in a contiguous sequence
213000** within a document for it to match.
213001*/
213002struct Fts5ExprPhrase {
213003 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
213004 Fts5Buffer poslist; /* Current position list */
213005 int nTerm; /* Number of entries in aTerm[] */
213006 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
213007};
213008
213009/*
213010** One or more phrases that must appear within a certain token distance of
213011** each other within each matching document.
213012*/
213013struct Fts5ExprNearset {
213014 int nNear; /* NEAR parameter */
213015 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
213016 int nPhrase; /* Number of entries in aPhrase[] array */
213017 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
213018};
213019
213020
213021/*
213022** Parse context.
213023*/
213024struct Fts5Parse {
213025 Fts5Config *pConfig;
213026 char *zErr;
213027 int rc;
213028 int nPhrase; /* Size of apPhrase array */
213029 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
213030 Fts5ExprNode *pExpr; /* Result of a successful parse */
213031};
213032
213033static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
213034 va_list ap;
213035 va_start(ap, zFmt);
213036 if( pParse->rc==SQLITE_OK ){
213037 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
213038 pParse->rc = SQLITE_ERROR;
213039 }
213040 va_end(ap);
213041}
213042
213043static int fts5ExprIsspace(char t){
213044 return t==' ' || t=='\t' || t=='\n' || t=='\r';
213045}
213046
213047/*
213048** Read the first token from the nul-terminated string at *pz.
213049*/
213050static int fts5ExprGetToken(
213051 Fts5Parse *pParse,
213052 const char **pz, /* IN/OUT: Pointer into buffer */
213053 Fts5Token *pToken
213054){
213055 const char *z = *pz;
213056 int tok;
213057
213058 /* Skip past any whitespace */
213059 while( fts5ExprIsspace(*z) ) z++;
213060
213061 pToken->p = z;
213062 pToken->n = 1;
213063 switch( *z ){
213064 case '(': tok = FTS5_LP; break;
213065 case ')': tok = FTS5_RP; break;
213066 case '{': tok = FTS5_LCP; break;
213067 case '}': tok = FTS5_RCP; break;
213068 case ':': tok = FTS5_COLON; break;
213069 case ',': tok = FTS5_COMMA; break;
213070 case '+': tok = FTS5_PLUS; break;
213071 case '*': tok = FTS5_STAR; break;
213072 case '-': tok = FTS5_MINUS; break;
213073 case '^': tok = FTS5_CARET; break;
213074 case '\0': tok = FTS5_EOF; break;
213075
213076 case '"': {
213077 const char *z2;
213078 tok = FTS5_STRING;
213079
213080 for(z2=&z[1]; 1; z2++){
213081 if( z2[0]=='"' ){
213082 z2++;
213083 if( z2[0]!='"' ) break;
213084 }
213085 if( z2[0]=='\0' ){
213086 sqlite3Fts5ParseError(pParse, "unterminated string");
213087 return FTS5_EOF;
213088 }
213089 }
213090 pToken->n = (z2 - z);
213091 break;
213092 }
213093
213094 default: {
213095 const char *z2;
213096 if( sqlite3Fts5IsBareword(z[0])==0 ){
213097 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
213098 return FTS5_EOF;
213099 }
213100 tok = FTS5_STRING;
213101 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
213102 pToken->n = (z2 - z);
213103 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
213104 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
213105 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
213106 break;
213107 }
213108 }
213109
213110 *pz = &pToken->p[pToken->n];
213111 return tok;
213112}
213113
213114static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
213115static void fts5ParseFree(void *p){ sqlite3_free(p); }
213116
213117static int sqlite3Fts5ExprNew(
213118 Fts5Config *pConfig, /* FTS5 Configuration */
213119 int iCol,
213120 const char *zExpr, /* Expression text */
213121 Fts5Expr **ppNew,
213122 char **pzErr
213123){
213124 Fts5Parse sParse;
213125 Fts5Token token;
213126 const char *z = zExpr;
213127 int t; /* Next token type */
213128 void *pEngine;
213129 Fts5Expr *pNew;
213130
213131 *ppNew = 0;
213132 *pzErr = 0;
213133 memset(&sParse, 0, sizeof(sParse));
213134 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
213135 if( pEngine==0 ){ return SQLITE_NOMEM; }
213136 sParse.pConfig = pConfig;
213137
213138 do {
213139 t = fts5ExprGetToken(&sParse, &z, &token);
213140 sqlite3Fts5Parser(pEngine, t, token, &sParse);
213141 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
213142 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
213143
213144 /* If the LHS of the MATCH expression was a user column, apply the
213145 ** implicit column-filter. */
213146 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
213147 int n = sizeof(Fts5Colset);
213148 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
213149 if( pColset ){
213150 pColset->nCol = 1;
213151 pColset->aiCol[0] = iCol;
213152 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
213153 }
213154 }
213155
213156 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
213157 if( sParse.rc==SQLITE_OK ){
213158 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
213159 if( pNew==0 ){
213160 sParse.rc = SQLITE_NOMEM;
213161 sqlite3Fts5ParseNodeFree(sParse.pExpr);
213162 }else{
213163 if( !sParse.pExpr ){
213164 const int nByte = sizeof(Fts5ExprNode);
213165 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
213166 if( pNew->pRoot ){
213167 pNew->pRoot->bEof = 1;
213168 }
213169 }else{
213170 pNew->pRoot = sParse.pExpr;
213171 }
213172 pNew->pIndex = 0;
213173 pNew->pConfig = pConfig;
213174 pNew->apExprPhrase = sParse.apPhrase;
213175 pNew->nPhrase = sParse.nPhrase;
213176 sParse.apPhrase = 0;
213177 }
213178 }else{
213179 sqlite3Fts5ParseNodeFree(sParse.pExpr);
213180 }
213181
213182 sqlite3_free(sParse.apPhrase);
213183 *pzErr = sParse.zErr;
213184 return sParse.rc;
213185}
213186
213187/*
213188** Free the expression node object passed as the only argument.
213189*/
213190static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
213191 if( p ){
213192 int i;
213193 for(i=0; i<p->nChild; i++){
213194 sqlite3Fts5ParseNodeFree(p->apChild[i]);
213195 }
213196 sqlite3Fts5ParseNearsetFree(p->pNear);
213197 sqlite3_free(p);
213198 }
213199}
213200
213201/*
213202** Free the expression object passed as the only argument.
213203*/
213204static void sqlite3Fts5ExprFree(Fts5Expr *p){
213205 if( p ){
213206 sqlite3Fts5ParseNodeFree(p->pRoot);
213207 sqlite3_free(p->apExprPhrase);
213208 sqlite3_free(p);
213209 }
213210}
213211
213212static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
213213 Fts5Parse sParse;
213214 memset(&sParse, 0, sizeof(sParse));
213215
213216 if( *pp1 ){
213217 Fts5Expr *p1 = *pp1;
213218 int nPhrase = p1->nPhrase + p2->nPhrase;
213219
213220 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
213221 p2->pRoot = 0;
213222
213223 if( sParse.rc==SQLITE_OK ){
213224 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
213225 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
213226 );
213227 if( ap==0 ){
213228 sParse.rc = SQLITE_NOMEM;
213229 }else{
213230 int i;
213231 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
213232 for(i=0; i<p2->nPhrase; i++){
213233 ap[i] = p2->apExprPhrase[i];
213234 }
213235 p1->nPhrase = nPhrase;
213236 p1->apExprPhrase = ap;
213237 }
213238 }
213239 sqlite3_free(p2->apExprPhrase);
213240 sqlite3_free(p2);
213241 }else{
213242 *pp1 = p2;
213243 }
213244
213245 return sParse.rc;
213246}
213247
213248/*
213249** Argument pTerm must be a synonym iterator. Return the current rowid
213250** that it points to.
213251*/
213252static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
213253 i64 iRet = 0;
213254 int bRetValid = 0;
213255 Fts5ExprTerm *p;
213256
213257 assert( pTerm->pSynonym );
213258 assert( bDesc==0 || bDesc==1 );
213259 for(p=pTerm; p; p=p->pSynonym){
213260 if( 0==sqlite3Fts5IterEof(p->pIter) ){
213261 i64 iRowid = p->pIter->iRowid;
213262 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
213263 iRet = iRowid;
213264 bRetValid = 1;
213265 }
213266 }
213267 }
213268
213269 if( pbEof && bRetValid==0 ) *pbEof = 1;
213270 return iRet;
213271}
213272
213273/*
213274** Argument pTerm must be a synonym iterator.
213275*/
213276static int fts5ExprSynonymList(
213277 Fts5ExprTerm *pTerm,
213278 i64 iRowid,
213279 Fts5Buffer *pBuf, /* Use this buffer for space if required */
213280 u8 **pa, int *pn
213281){
213282 Fts5PoslistReader aStatic[4];
213283 Fts5PoslistReader *aIter = aStatic;
213284 int nIter = 0;
213285 int nAlloc = 4;
213286 int rc = SQLITE_OK;
213287 Fts5ExprTerm *p;
213288
213289 assert( pTerm->pSynonym );
213290 for(p=pTerm; p; p=p->pSynonym){
213291 Fts5IndexIter *pIter = p->pIter;
213292 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
213293 if( pIter->nData==0 ) continue;
213294 if( nIter==nAlloc ){
213295 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
213296 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
213297 if( aNew==0 ){
213298 rc = SQLITE_NOMEM;
213299 goto synonym_poslist_out;
213300 }
213301 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
213302 nAlloc = nAlloc*2;
213303 if( aIter!=aStatic ) sqlite3_free(aIter);
213304 aIter = aNew;
213305 }
213306 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
213307 assert( aIter[nIter].bEof==0 );
213308 nIter++;
213309 }
213310 }
213311
213312 if( nIter==1 ){
213313 *pa = (u8*)aIter[0].a;
213314 *pn = aIter[0].n;
213315 }else{
213316 Fts5PoslistWriter writer = {0};
213317 i64 iPrev = -1;
213318 fts5BufferZero(pBuf);
213319 while( 1 ){
213320 int i;
213321 i64 iMin = FTS5_LARGEST_INT64;
213322 for(i=0; i<nIter; i++){
213323 if( aIter[i].bEof==0 ){
213324 if( aIter[i].iPos==iPrev ){
213325 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
213326 }
213327 if( aIter[i].iPos<iMin ){
213328 iMin = aIter[i].iPos;
213329 }
213330 }
213331 }
213332 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
213333 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
213334 iPrev = iMin;
213335 }
213336 if( rc==SQLITE_OK ){
213337 *pa = pBuf->p;
213338 *pn = pBuf->n;
213339 }
213340 }
213341
213342 synonym_poslist_out:
213343 if( aIter!=aStatic ) sqlite3_free(aIter);
213344 return rc;
213345}
213346
213347
213348/*
213349** All individual term iterators in pPhrase are guaranteed to be valid and
213350** pointing to the same rowid when this function is called. This function
213351** checks if the current rowid really is a match, and if so populates
213352** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
213353** is set to true if this is really a match, or false otherwise.
213354**
213355** SQLITE_OK is returned if an error occurs, or an SQLite error code
213356** otherwise. It is not considered an error code if the current rowid is
213357** not a match.
213358*/
213359static int fts5ExprPhraseIsMatch(
213360 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
213361 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
213362 int *pbMatch /* OUT: Set to true if really a match */
213363){
213364 Fts5PoslistWriter writer = {0};
213365 Fts5PoslistReader aStatic[4];
213366 Fts5PoslistReader *aIter = aStatic;
213367 int i;
213368 int rc = SQLITE_OK;
213369 int bFirst = pPhrase->aTerm[0].bFirst;
213370
213371 fts5BufferZero(&pPhrase->poslist);
213372
213373 /* If the aStatic[] array is not large enough, allocate a large array
213374 ** using sqlite3_malloc(). This approach could be improved upon. */
213375 if( pPhrase->nTerm>ArraySize(aStatic) ){
213376 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
213377 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
213378 if( !aIter ) return SQLITE_NOMEM;
213379 }
213380 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
213381
213382 /* Initialize a term iterator for each term in the phrase */
213383 for(i=0; i<pPhrase->nTerm; i++){
213384 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
213385 int n = 0;
213386 int bFlag = 0;
213387 u8 *a = 0;
213388 if( pTerm->pSynonym ){
213389 Fts5Buffer buf = {0, 0, 0};
213390 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
213391 if( rc ){
213392 sqlite3_free(a);
213393 goto ismatch_out;
213394 }
213395 if( a==buf.p ) bFlag = 1;
213396 }else{
213397 a = (u8*)pTerm->pIter->pData;
213398 n = pTerm->pIter->nData;
213399 }
213400 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
213401 aIter[i].bFlag = (u8)bFlag;
213402 if( aIter[i].bEof ) goto ismatch_out;
213403 }
213404
213405 while( 1 ){
213406 int bMatch;
213407 i64 iPos = aIter[0].iPos;
213408 do {
213409 bMatch = 1;
213410 for(i=0; i<pPhrase->nTerm; i++){
213411 Fts5PoslistReader *pPos = &aIter[i];
213412 i64 iAdj = iPos + i;
213413 if( pPos->iPos!=iAdj ){
213414 bMatch = 0;
213415 while( pPos->iPos<iAdj ){
213416 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
213417 }
213418 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
213419 }
213420 }
213421 }while( bMatch==0 );
213422
213423 /* Append position iPos to the output */
213424 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
213425 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
213426 if( rc!=SQLITE_OK ) goto ismatch_out;
213427 }
213428
213429 for(i=0; i<pPhrase->nTerm; i++){
213430 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
213431 }
213432 }
213433
213434 ismatch_out:
213435 *pbMatch = (pPhrase->poslist.n>0);
213436 for(i=0; i<pPhrase->nTerm; i++){
213437 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
213438 }
213439 if( aIter!=aStatic ) sqlite3_free(aIter);
213440 return rc;
213441}
213442
213443typedef struct Fts5LookaheadReader Fts5LookaheadReader;
213444struct Fts5LookaheadReader {
213445 const u8 *a; /* Buffer containing position list */
213446 int n; /* Size of buffer a[] in bytes */
213447 int i; /* Current offset in position list */
213448 i64 iPos; /* Current position */
213449 i64 iLookahead; /* Next position */
213450};
213451
213452#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
213453
213454static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
213455 p->iPos = p->iLookahead;
213456 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
213457 p->iLookahead = FTS5_LOOKAHEAD_EOF;
213458 }
213459 return (p->iPos==FTS5_LOOKAHEAD_EOF);
213460}
213461
213462static int fts5LookaheadReaderInit(
213463 const u8 *a, int n, /* Buffer to read position list from */
213464 Fts5LookaheadReader *p /* Iterator object to initialize */
213465){
213466 memset(p, 0, sizeof(Fts5LookaheadReader));
213467 p->a = a;
213468 p->n = n;
213469 fts5LookaheadReaderNext(p);
213470 return fts5LookaheadReaderNext(p);
213471}
213472
213473typedef struct Fts5NearTrimmer Fts5NearTrimmer;
213474struct Fts5NearTrimmer {
213475 Fts5LookaheadReader reader; /* Input iterator */
213476 Fts5PoslistWriter writer; /* Writer context */
213477 Fts5Buffer *pOut; /* Output poslist */
213478};
213479
213480/*
213481** The near-set object passed as the first argument contains more than
213482** one phrase. All phrases currently point to the same row. The
213483** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
213484** tests if the current row contains instances of each phrase sufficiently
213485** close together to meet the NEAR constraint. Non-zero is returned if it
213486** does, or zero otherwise.
213487**
213488** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
213489** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
213490** occurs within this function (*pRc) is set accordingly before returning.
213491** The return value is undefined in both these cases.
213492**
213493** If no error occurs and non-zero (a match) is returned, the position-list
213494** of each phrase object is edited to contain only those entries that
213495** meet the constraint before returning.
213496*/
213497static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
213498 Fts5NearTrimmer aStatic[4];
213499 Fts5NearTrimmer *a = aStatic;
213500 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
213501
213502 int i;
213503 int rc = *pRc;
213504 int bMatch;
213505
213506 assert( pNear->nPhrase>1 );
213507
213508 /* If the aStatic[] array is not large enough, allocate a large array
213509 ** using sqlite3_malloc(). This approach could be improved upon. */
213510 if( pNear->nPhrase>ArraySize(aStatic) ){
213511 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
213512 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
213513 }else{
213514 memset(aStatic, 0, sizeof(aStatic));
213515 }
213516 if( rc!=SQLITE_OK ){
213517 *pRc = rc;
213518 return 0;
213519 }
213520
213521 /* Initialize a lookahead iterator for each phrase. After passing the
213522 ** buffer and buffer size to the lookaside-reader init function, zero
213523 ** the phrase poslist buffer. The new poslist for the phrase (containing
213524 ** the same entries as the original with some entries removed on account
213525 ** of the NEAR constraint) is written over the original even as it is
213526 ** being read. This is safe as the entries for the new poslist are a
213527 ** subset of the old, so it is not possible for data yet to be read to
213528 ** be overwritten. */
213529 for(i=0; i<pNear->nPhrase; i++){
213530 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
213531 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
213532 pPoslist->n = 0;
213533 a[i].pOut = pPoslist;
213534 }
213535
213536 while( 1 ){
213537 int iAdv;
213538 i64 iMin;
213539 i64 iMax;
213540
213541 /* This block advances the phrase iterators until they point to a set of
213542 ** entries that together comprise a match. */
213543 iMax = a[0].reader.iPos;
213544 do {
213545 bMatch = 1;
213546 for(i=0; i<pNear->nPhrase; i++){
213547 Fts5LookaheadReader *pPos = &a[i].reader;
213548 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
213549 if( pPos->iPos<iMin || pPos->iPos>iMax ){
213550 bMatch = 0;
213551 while( pPos->iPos<iMin ){
213552 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
213553 }
213554 if( pPos->iPos>iMax ) iMax = pPos->iPos;
213555 }
213556 }
213557 }while( bMatch==0 );
213558
213559 /* Add an entry to each output position list */
213560 for(i=0; i<pNear->nPhrase; i++){
213561 i64 iPos = a[i].reader.iPos;
213562 Fts5PoslistWriter *pWriter = &a[i].writer;
213563 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
213564 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
213565 }
213566 }
213567
213568 iAdv = 0;
213569 iMin = a[0].reader.iLookahead;
213570 for(i=0; i<pNear->nPhrase; i++){
213571 if( a[i].reader.iLookahead < iMin ){
213572 iMin = a[i].reader.iLookahead;
213573 iAdv = i;
213574 }
213575 }
213576 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
213577 }
213578
213579 ismatch_out: {
213580 int bRet = a[0].pOut->n>0;
213581 *pRc = rc;
213582 if( a!=aStatic ) sqlite3_free(a);
213583 return bRet;
213584 }
213585}
213586
213587/*
213588** Advance iterator pIter until it points to a value equal to or laster
213589** than the initial value of *piLast. If this means the iterator points
213590** to a value laster than *piLast, update *piLast to the new lastest value.
213591**
213592** If the iterator reaches EOF, set *pbEof to true before returning. If
213593** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
213594** are set, return a non-zero value. Otherwise, return zero.
213595*/
213596static int fts5ExprAdvanceto(
213597 Fts5IndexIter *pIter, /* Iterator to advance */
213598 int bDesc, /* True if iterator is "rowid DESC" */
213599 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
213600 int *pRc, /* OUT: Error code */
213601 int *pbEof /* OUT: Set to true if EOF */
213602){
213603 i64 iLast = *piLast;
213604 i64 iRowid;
213605
213606 iRowid = pIter->iRowid;
213607 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
213608 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
213609 if( rc || sqlite3Fts5IterEof(pIter) ){
213610 *pRc = rc;
213611 *pbEof = 1;
213612 return 1;
213613 }
213614 iRowid = pIter->iRowid;
213615 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
213616 }
213617 *piLast = iRowid;
213618
213619 return 0;
213620}
213621
213622static int fts5ExprSynonymAdvanceto(
213623 Fts5ExprTerm *pTerm, /* Term iterator to advance */
213624 int bDesc, /* True if iterator is "rowid DESC" */
213625 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
213626 int *pRc /* OUT: Error code */
213627){
213628 int rc = SQLITE_OK;
213629 i64 iLast = *piLast;
213630 Fts5ExprTerm *p;
213631 int bEof = 0;
213632
213633 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
213634 if( sqlite3Fts5IterEof(p->pIter)==0 ){
213635 i64 iRowid = p->pIter->iRowid;
213636 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
213637 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
213638 }
213639 }
213640 }
213641
213642 if( rc!=SQLITE_OK ){
213643 *pRc = rc;
213644 bEof = 1;
213645 }else{
213646 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
213647 }
213648 return bEof;
213649}
213650
213651
213652static int fts5ExprNearTest(
213653 int *pRc,
213654 Fts5Expr *pExpr, /* Expression that pNear is a part of */
213655 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
213656){
213657 Fts5ExprNearset *pNear = pNode->pNear;
213658 int rc = *pRc;
213659
213660 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
213661 Fts5ExprTerm *pTerm;
213662 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
213663 pPhrase->poslist.n = 0;
213664 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
213665 Fts5IndexIter *pIter = pTerm->pIter;
213666 if( sqlite3Fts5IterEof(pIter)==0 ){
213667 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
213668 pPhrase->poslist.n = 1;
213669 }
213670 }
213671 }
213672 return pPhrase->poslist.n;
213673 }else{
213674 int i;
213675
213676 /* Check that each phrase in the nearset matches the current row.
213677 ** Populate the pPhrase->poslist buffers at the same time. If any
213678 ** phrase is not a match, break out of the loop early. */
213679 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
213680 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213681 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
213682 || pNear->pColset || pPhrase->aTerm[0].bFirst
213683 ){
213684 int bMatch = 0;
213685 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
213686 if( bMatch==0 ) break;
213687 }else{
213688 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
213689 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
213690 }
213691 }
213692
213693 *pRc = rc;
213694 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
213695 return 1;
213696 }
213697 return 0;
213698 }
213699}
213700
213701
213702/*
213703** Initialize all term iterators in the pNear object. If any term is found
213704** to match no documents at all, return immediately without initializing any
213705** further iterators.
213706**
213707** If an error occurs, return an SQLite error code. Otherwise, return
213708** SQLITE_OK. It is not considered an error if some term matches zero
213709** documents.
213710*/
213711static int fts5ExprNearInitAll(
213712 Fts5Expr *pExpr,
213713 Fts5ExprNode *pNode
213714){
213715 Fts5ExprNearset *pNear = pNode->pNear;
213716 int i;
213717
213718 assert( pNode->bNomatch==0 );
213719 for(i=0; i<pNear->nPhrase; i++){
213720 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213721 if( pPhrase->nTerm==0 ){
213722 pNode->bEof = 1;
213723 return SQLITE_OK;
213724 }else{
213725 int j;
213726 for(j=0; j<pPhrase->nTerm; j++){
213727 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
213728 Fts5ExprTerm *p;
213729 int bHit = 0;
213730
213731 for(p=pTerm; p; p=p->pSynonym){
213732 int rc;
213733 if( p->pIter ){
213734 sqlite3Fts5IterClose(p->pIter);
213735 p->pIter = 0;
213736 }
213737 rc = sqlite3Fts5IndexQuery(
213738 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
213739 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
213740 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
213741 pNear->pColset,
213742 &p->pIter
213743 );
213744 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
213745 if( rc!=SQLITE_OK ) return rc;
213746 if( 0==sqlite3Fts5IterEof(p->pIter) ){
213747 bHit = 1;
213748 }
213749 }
213750
213751 if( bHit==0 ){
213752 pNode->bEof = 1;
213753 return SQLITE_OK;
213754 }
213755 }
213756 }
213757 }
213758
213759 pNode->bEof = 0;
213760 return SQLITE_OK;
213761}
213762
213763/*
213764** If pExpr is an ASC iterator, this function returns a value with the
213765** same sign as:
213766**
213767** (iLhs - iRhs)
213768**
213769** Otherwise, if this is a DESC iterator, the opposite is returned:
213770**
213771** (iRhs - iLhs)
213772*/
213773static int fts5RowidCmp(
213774 Fts5Expr *pExpr,
213775 i64 iLhs,
213776 i64 iRhs
213777){
213778 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
213779 if( pExpr->bDesc==0 ){
213780 if( iLhs<iRhs ) return -1;
213781 return (iLhs > iRhs);
213782 }else{
213783 if( iLhs>iRhs ) return -1;
213784 return (iLhs < iRhs);
213785 }
213786}
213787
213788static void fts5ExprSetEof(Fts5ExprNode *pNode){
213789 int i;
213790 pNode->bEof = 1;
213791 pNode->bNomatch = 0;
213792 for(i=0; i<pNode->nChild; i++){
213793 fts5ExprSetEof(pNode->apChild[i]);
213794 }
213795}
213796
213797static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
213798 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
213799 Fts5ExprNearset *pNear = pNode->pNear;
213800 int i;
213801 for(i=0; i<pNear->nPhrase; i++){
213802 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213803 pPhrase->poslist.n = 0;
213804 }
213805 }else{
213806 int i;
213807 for(i=0; i<pNode->nChild; i++){
213808 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
213809 }
213810 }
213811}
213812
213813
213814
213815/*
213816** Compare the values currently indicated by the two nodes as follows:
213817**
213818** res = (*p1) - (*p2)
213819**
213820** Nodes that point to values that come later in the iteration order are
213821** considered to be larger. Nodes at EOF are the largest of all.
213822**
213823** This means that if the iteration order is ASC, then numerically larger
213824** rowids are considered larger. Or if it is the default DESC, numerically
213825** smaller rowids are larger.
213826*/
213827static int fts5NodeCompare(
213828 Fts5Expr *pExpr,
213829 Fts5ExprNode *p1,
213830 Fts5ExprNode *p2
213831){
213832 if( p2->bEof ) return -1;
213833 if( p1->bEof ) return +1;
213834 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
213835}
213836
213837/*
213838** All individual term iterators in pNear are guaranteed to be valid when
213839** this function is called. This function checks if all term iterators
213840** point to the same rowid, and if not, advances them until they do.
213841** If an EOF is reached before this happens, *pbEof is set to true before
213842** returning.
213843**
213844** SQLITE_OK is returned if an error occurs, or an SQLite error code
213845** otherwise. It is not considered an error code if an iterator reaches
213846** EOF.
213847*/
213848static int fts5ExprNodeTest_STRING(
213849 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
213850 Fts5ExprNode *pNode
213851){
213852 Fts5ExprNearset *pNear = pNode->pNear;
213853 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
213854 int rc = SQLITE_OK;
213855 i64 iLast; /* Lastest rowid any iterator points to */
213856 int i, j; /* Phrase and token index, respectively */
213857 int bMatch; /* True if all terms are at the same rowid */
213858 const int bDesc = pExpr->bDesc;
213859
213860 /* Check that this node should not be FTS5_TERM */
213861 assert( pNear->nPhrase>1
213862 || pNear->apPhrase[0]->nTerm>1
213863 || pNear->apPhrase[0]->aTerm[0].pSynonym
213864 || pNear->apPhrase[0]->aTerm[0].bFirst
213865 );
213866
213867 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
213868 ** iterator skips through rowids in the default ascending order, this means
213869 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
213870 ** means the minimum rowid. */
213871 if( pLeft->aTerm[0].pSynonym ){
213872 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
213873 }else{
213874 iLast = pLeft->aTerm[0].pIter->iRowid;
213875 }
213876
213877 do {
213878 bMatch = 1;
213879 for(i=0; i<pNear->nPhrase; i++){
213880 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
213881 for(j=0; j<pPhrase->nTerm; j++){
213882 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
213883 if( pTerm->pSynonym ){
213884 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
213885 if( iRowid==iLast ) continue;
213886 bMatch = 0;
213887 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
213888 pNode->bNomatch = 0;
213889 pNode->bEof = 1;
213890 return rc;
213891 }
213892 }else{
213893 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
213894 if( pIter->iRowid==iLast || pIter->bEof ) continue;
213895 bMatch = 0;
213896 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
213897 return rc;
213898 }
213899 }
213900 }
213901 }
213902 }while( bMatch==0 );
213903
213904 pNode->iRowid = iLast;
213905 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
213906 assert( pNode->bEof==0 || pNode->bNomatch==0 );
213907
213908 return rc;
213909}
213910
213911/*
213912** Advance the first term iterator in the first phrase of pNear. Set output
213913** variable *pbEof to true if it reaches EOF or if an error occurs.
213914**
213915** Return SQLITE_OK if successful, or an SQLite error code if an error
213916** occurs.
213917*/
213918static int fts5ExprNodeNext_STRING(
213919 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
213920 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
213921 int bFromValid,
213922 i64 iFrom
213923){
213924 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
213925 int rc = SQLITE_OK;
213926
213927 pNode->bNomatch = 0;
213928 if( pTerm->pSynonym ){
213929 int bEof = 1;
213930 Fts5ExprTerm *p;
213931
213932 /* Find the firstest rowid any synonym points to. */
213933 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
213934
213935 /* Advance each iterator that currently points to iRowid. Or, if iFrom
213936 ** is valid - each iterator that points to a rowid before iFrom. */
213937 for(p=pTerm; p; p=p->pSynonym){
213938 if( sqlite3Fts5IterEof(p->pIter)==0 ){
213939 i64 ii = p->pIter->iRowid;
213940 if( ii==iRowid
213941 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
213942 ){
213943 if( bFromValid ){
213944 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
213945 }else{
213946 rc = sqlite3Fts5IterNext(p->pIter);
213947 }
213948 if( rc!=SQLITE_OK ) break;
213949 if( sqlite3Fts5IterEof(p->pIter)==0 ){
213950 bEof = 0;
213951 }
213952 }else{
213953 bEof = 0;
213954 }
213955 }
213956 }
213957
213958 /* Set the EOF flag if either all synonym iterators are at EOF or an
213959 ** error has occurred. */
213960 pNode->bEof = (rc || bEof);
213961 }else{
213962 Fts5IndexIter *pIter = pTerm->pIter;
213963
213964 assert( Fts5NodeIsString(pNode) );
213965 if( bFromValid ){
213966 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
213967 }else{
213968 rc = sqlite3Fts5IterNext(pIter);
213969 }
213970
213971 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
213972 }
213973
213974 if( pNode->bEof==0 ){
213975 assert( rc==SQLITE_OK );
213976 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
213977 }
213978
213979 return rc;
213980}
213981
213982
213983static int fts5ExprNodeTest_TERM(
213984 Fts5Expr *pExpr, /* Expression that pNear is a part of */
213985 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
213986){
213987 /* As this "NEAR" object is actually a single phrase that consists
213988 ** of a single term only, grab pointers into the poslist managed by the
213989 ** fts5_index.c iterator object. This is much faster than synthesizing
213990 ** a new poslist the way we have to for more complicated phrase or NEAR
213991 ** expressions. */
213992 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
213993 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
213994
213995 assert( pNode->eType==FTS5_TERM );
213996 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
213997 assert( pPhrase->aTerm[0].pSynonym==0 );
213998
213999 pPhrase->poslist.n = pIter->nData;
214000 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
214001 pPhrase->poslist.p = (u8*)pIter->pData;
214002 }
214003 pNode->iRowid = pIter->iRowid;
214004 pNode->bNomatch = (pPhrase->poslist.n==0);
214005 return SQLITE_OK;
214006}
214007
214008/*
214009** xNext() method for a node of type FTS5_TERM.
214010*/
214011static int fts5ExprNodeNext_TERM(
214012 Fts5Expr *pExpr,
214013 Fts5ExprNode *pNode,
214014 int bFromValid,
214015 i64 iFrom
214016){
214017 int rc;
214018 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
214019
214020 assert( pNode->bEof==0 );
214021 if( bFromValid ){
214022 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
214023 }else{
214024 rc = sqlite3Fts5IterNext(pIter);
214025 }
214026 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
214027 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
214028 }else{
214029 pNode->bEof = 1;
214030 pNode->bNomatch = 0;
214031 }
214032 return rc;
214033}
214034
214035static void fts5ExprNodeTest_OR(
214036 Fts5Expr *pExpr, /* Expression of which pNode is a part */
214037 Fts5ExprNode *pNode /* Expression node to test */
214038){
214039 Fts5ExprNode *pNext = pNode->apChild[0];
214040 int i;
214041
214042 for(i=1; i<pNode->nChild; i++){
214043 Fts5ExprNode *pChild = pNode->apChild[i];
214044 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
214045 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
214046 pNext = pChild;
214047 }
214048 }
214049 pNode->iRowid = pNext->iRowid;
214050 pNode->bEof = pNext->bEof;
214051 pNode->bNomatch = pNext->bNomatch;
214052}
214053
214054static int fts5ExprNodeNext_OR(
214055 Fts5Expr *pExpr,
214056 Fts5ExprNode *pNode,
214057 int bFromValid,
214058 i64 iFrom
214059){
214060 int i;
214061 i64 iLast = pNode->iRowid;
214062
214063 for(i=0; i<pNode->nChild; i++){
214064 Fts5ExprNode *p1 = pNode->apChild[i];
214065 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
214066 if( p1->bEof==0 ){
214067 if( (p1->iRowid==iLast)
214068 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
214069 ){
214070 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
214071 if( rc!=SQLITE_OK ){
214072 pNode->bNomatch = 0;
214073 return rc;
214074 }
214075 }
214076 }
214077 }
214078
214079 fts5ExprNodeTest_OR(pExpr, pNode);
214080 return SQLITE_OK;
214081}
214082
214083/*
214084** Argument pNode is an FTS5_AND node.
214085*/
214086static int fts5ExprNodeTest_AND(
214087 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
214088 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
214089){
214090 int iChild;
214091 i64 iLast = pAnd->iRowid;
214092 int rc = SQLITE_OK;
214093 int bMatch;
214094
214095 assert( pAnd->bEof==0 );
214096 do {
214097 pAnd->bNomatch = 0;
214098 bMatch = 1;
214099 for(iChild=0; iChild<pAnd->nChild; iChild++){
214100 Fts5ExprNode *pChild = pAnd->apChild[iChild];
214101 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
214102 if( cmp>0 ){
214103 /* Advance pChild until it points to iLast or laster */
214104 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
214105 if( rc!=SQLITE_OK ){
214106 pAnd->bNomatch = 0;
214107 return rc;
214108 }
214109 }
214110
214111 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
214112 ** the child node is guaranteed to have advanced at least as far as
214113 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
214114 ** new lastest rowid seen so far. */
214115 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
214116 if( pChild->bEof ){
214117 fts5ExprSetEof(pAnd);
214118 bMatch = 1;
214119 break;
214120 }else if( iLast!=pChild->iRowid ){
214121 bMatch = 0;
214122 iLast = pChild->iRowid;
214123 }
214124
214125 if( pChild->bNomatch ){
214126 pAnd->bNomatch = 1;
214127 }
214128 }
214129 }while( bMatch==0 );
214130
214131 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
214132 fts5ExprNodeZeroPoslist(pAnd);
214133 }
214134 pAnd->iRowid = iLast;
214135 return SQLITE_OK;
214136}
214137
214138static int fts5ExprNodeNext_AND(
214139 Fts5Expr *pExpr,
214140 Fts5ExprNode *pNode,
214141 int bFromValid,
214142 i64 iFrom
214143){
214144 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
214145 if( rc==SQLITE_OK ){
214146 rc = fts5ExprNodeTest_AND(pExpr, pNode);
214147 }else{
214148 pNode->bNomatch = 0;
214149 }
214150 return rc;
214151}
214152
214153static int fts5ExprNodeTest_NOT(
214154 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
214155 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
214156){
214157 int rc = SQLITE_OK;
214158 Fts5ExprNode *p1 = pNode->apChild[0];
214159 Fts5ExprNode *p2 = pNode->apChild[1];
214160 assert( pNode->nChild==2 );
214161
214162 while( rc==SQLITE_OK && p1->bEof==0 ){
214163 int cmp = fts5NodeCompare(pExpr, p1, p2);
214164 if( cmp>0 ){
214165 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
214166 cmp = fts5NodeCompare(pExpr, p1, p2);
214167 }
214168 assert( rc!=SQLITE_OK || cmp<=0 );
214169 if( cmp || p2->bNomatch ) break;
214170 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
214171 }
214172 pNode->bEof = p1->bEof;
214173 pNode->bNomatch = p1->bNomatch;
214174 pNode->iRowid = p1->iRowid;
214175 if( p1->bEof ){
214176 fts5ExprNodeZeroPoslist(p2);
214177 }
214178 return rc;
214179}
214180
214181static int fts5ExprNodeNext_NOT(
214182 Fts5Expr *pExpr,
214183 Fts5ExprNode *pNode,
214184 int bFromValid,
214185 i64 iFrom
214186){
214187 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
214188 if( rc==SQLITE_OK ){
214189 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
214190 }
214191 if( rc!=SQLITE_OK ){
214192 pNode->bNomatch = 0;
214193 }
214194 return rc;
214195}
214196
214197/*
214198** If pNode currently points to a match, this function returns SQLITE_OK
214199** without modifying it. Otherwise, pNode is advanced until it does point
214200** to a match or EOF is reached.
214201*/
214202static int fts5ExprNodeTest(
214203 Fts5Expr *pExpr, /* Expression of which pNode is a part */
214204 Fts5ExprNode *pNode /* Expression node to test */
214205){
214206 int rc = SQLITE_OK;
214207 if( pNode->bEof==0 ){
214208 switch( pNode->eType ){
214209
214210 case FTS5_STRING: {
214211 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
214212 break;
214213 }
214214
214215 case FTS5_TERM: {
214216 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
214217 break;
214218 }
214219
214220 case FTS5_AND: {
214221 rc = fts5ExprNodeTest_AND(pExpr, pNode);
214222 break;
214223 }
214224
214225 case FTS5_OR: {
214226 fts5ExprNodeTest_OR(pExpr, pNode);
214227 break;
214228 }
214229
214230 default: assert( pNode->eType==FTS5_NOT ); {
214231 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
214232 break;
214233 }
214234 }
214235 }
214236 return rc;
214237}
214238
214239
214240/*
214241** Set node pNode, which is part of expression pExpr, to point to the first
214242** match. If there are no matches, set the Node.bEof flag to indicate EOF.
214243**
214244** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
214245** It is not an error if there are no matches.
214246*/
214247static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
214248 int rc = SQLITE_OK;
214249 pNode->bEof = 0;
214250 pNode->bNomatch = 0;
214251
214252 if( Fts5NodeIsString(pNode) ){
214253 /* Initialize all term iterators in the NEAR object. */
214254 rc = fts5ExprNearInitAll(pExpr, pNode);
214255 }else if( pNode->xNext==0 ){
214256 pNode->bEof = 1;
214257 }else{
214258 int i;
214259 int nEof = 0;
214260 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
214261 Fts5ExprNode *pChild = pNode->apChild[i];
214262 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
214263 assert( pChild->bEof==0 || pChild->bEof==1 );
214264 nEof += pChild->bEof;
214265 }
214266 pNode->iRowid = pNode->apChild[0]->iRowid;
214267
214268 switch( pNode->eType ){
214269 case FTS5_AND:
214270 if( nEof>0 ) fts5ExprSetEof(pNode);
214271 break;
214272
214273 case FTS5_OR:
214274 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
214275 break;
214276
214277 default:
214278 assert( pNode->eType==FTS5_NOT );
214279 pNode->bEof = pNode->apChild[0]->bEof;
214280 break;
214281 }
214282 }
214283
214284 if( rc==SQLITE_OK ){
214285 rc = fts5ExprNodeTest(pExpr, pNode);
214286 }
214287 return rc;
214288}
214289
214290
214291/*
214292** Begin iterating through the set of documents in index pIdx matched by
214293** the MATCH expression passed as the first argument. If the "bDesc"
214294** parameter is passed a non-zero value, iteration is in descending rowid
214295** order. Or, if it is zero, in ascending order.
214296**
214297** If iterating in ascending rowid order (bDesc==0), the first document
214298** visited is that with the smallest rowid that is larger than or equal
214299** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
214300** then the first document visited must have a rowid smaller than or
214301** equal to iFirst.
214302**
214303** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
214304** is not considered an error if the query does not match any documents.
214305*/
214306static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
214307 Fts5ExprNode *pRoot = p->pRoot;
214308 int rc; /* Return code */
214309
214310 p->pIndex = pIdx;
214311 p->bDesc = bDesc;
214312 rc = fts5ExprNodeFirst(p, pRoot);
214313
214314 /* If not at EOF but the current rowid occurs earlier than iFirst in
214315 ** the iteration order, move to document iFirst or later. */
214316 if( rc==SQLITE_OK
214317 && 0==pRoot->bEof
214318 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
214319 ){
214320 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
214321 }
214322
214323 /* If the iterator is not at a real match, skip forward until it is. */
214324 while( pRoot->bNomatch ){
214325 assert( pRoot->bEof==0 && rc==SQLITE_OK );
214326 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
214327 }
214328 return rc;
214329}
214330
214331/*
214332** Move to the next document
214333**
214334** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
214335** is not considered an error if the query does not match any documents.
214336*/
214337static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
214338 int rc;
214339 Fts5ExprNode *pRoot = p->pRoot;
214340 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
214341 do {
214342 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
214343 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
214344 }while( pRoot->bNomatch );
214345 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
214346 pRoot->bEof = 1;
214347 }
214348 return rc;
214349}
214350
214351static int sqlite3Fts5ExprEof(Fts5Expr *p){
214352 return p->pRoot->bEof;
214353}
214354
214355static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
214356 return p->pRoot->iRowid;
214357}
214358
214359static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
214360 int rc = SQLITE_OK;
214361 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
214362 return rc;
214363}
214364
214365/*
214366** Free the phrase object passed as the only argument.
214367*/
214368static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
214369 if( pPhrase ){
214370 int i;
214371 for(i=0; i<pPhrase->nTerm; i++){
214372 Fts5ExprTerm *pSyn;
214373 Fts5ExprTerm *pNext;
214374 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
214375 sqlite3_free(pTerm->zTerm);
214376 sqlite3Fts5IterClose(pTerm->pIter);
214377 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
214378 pNext = pSyn->pSynonym;
214379 sqlite3Fts5IterClose(pSyn->pIter);
214380 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
214381 sqlite3_free(pSyn);
214382 }
214383 }
214384 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
214385 sqlite3_free(pPhrase);
214386 }
214387}
214388
214389/*
214390** Set the "bFirst" flag on the first token of the phrase passed as the
214391** only argument.
214392*/
214393static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
214394 if( pPhrase && pPhrase->nTerm ){
214395 pPhrase->aTerm[0].bFirst = 1;
214396 }
214397}
214398
214399/*
214400** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
214401** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
214402** appended to it and the results returned.
214403**
214404** If an OOM error occurs, both the pNear and pPhrase objects are freed and
214405** NULL returned.
214406*/
214407static Fts5ExprNearset *sqlite3Fts5ParseNearset(
214408 Fts5Parse *pParse, /* Parse context */
214409 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
214410 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
214411){
214412 const int SZALLOC = 8;
214413 Fts5ExprNearset *pRet = 0;
214414
214415 if( pParse->rc==SQLITE_OK ){
214416 if( pPhrase==0 ){
214417 return pNear;
214418 }
214419 if( pNear==0 ){
214420 sqlite3_int64 nByte;
214421 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
214422 pRet = sqlite3_malloc64(nByte);
214423 if( pRet==0 ){
214424 pParse->rc = SQLITE_NOMEM;
214425 }else{
214426 memset(pRet, 0, (size_t)nByte);
214427 }
214428 }else if( (pNear->nPhrase % SZALLOC)==0 ){
214429 int nNew = pNear->nPhrase + SZALLOC;
214430 sqlite3_int64 nByte;
214431
214432 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
214433 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
214434 if( pRet==0 ){
214435 pParse->rc = SQLITE_NOMEM;
214436 }
214437 }else{
214438 pRet = pNear;
214439 }
214440 }
214441
214442 if( pRet==0 ){
214443 assert( pParse->rc!=SQLITE_OK );
214444 sqlite3Fts5ParseNearsetFree(pNear);
214445 sqlite3Fts5ParsePhraseFree(pPhrase);
214446 }else{
214447 if( pRet->nPhrase>0 ){
214448 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
214449 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
214450 if( pPhrase->nTerm==0 ){
214451 fts5ExprPhraseFree(pPhrase);
214452 pRet->nPhrase--;
214453 pParse->nPhrase--;
214454 pPhrase = pLast;
214455 }else if( pLast->nTerm==0 ){
214456 fts5ExprPhraseFree(pLast);
214457 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
214458 pParse->nPhrase--;
214459 pRet->nPhrase--;
214460 }
214461 }
214462 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
214463 }
214464 return pRet;
214465}
214466
214467typedef struct TokenCtx TokenCtx;
214468struct TokenCtx {
214469 Fts5ExprPhrase *pPhrase;
214470 int rc;
214471};
214472
214473/*
214474** Callback for tokenizing terms used by ParseTerm().
214475*/
214476static int fts5ParseTokenize(
214477 void *pContext, /* Pointer to Fts5InsertCtx object */
214478 int tflags, /* Mask of FTS5_TOKEN_* flags */
214479 const char *pToken, /* Buffer containing token */
214480 int nToken, /* Size of token in bytes */
214481 int iUnused1, /* Start offset of token */
214482 int iUnused2 /* End offset of token */
214483){
214484 int rc = SQLITE_OK;
214485 const int SZALLOC = 8;
214486 TokenCtx *pCtx = (TokenCtx*)pContext;
214487 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
214488
214489 UNUSED_PARAM2(iUnused1, iUnused2);
214490
214491 /* If an error has already occurred, this is a no-op */
214492 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
214493 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
214494
214495 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
214496 Fts5ExprTerm *pSyn;
214497 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
214498 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
214499 if( pSyn==0 ){
214500 rc = SQLITE_NOMEM;
214501 }else{
214502 memset(pSyn, 0, (size_t)nByte);
214503 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
214504 memcpy(pSyn->zTerm, pToken, nToken);
214505 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
214506 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
214507 }
214508 }else{
214509 Fts5ExprTerm *pTerm;
214510 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
214511 Fts5ExprPhrase *pNew;
214512 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
214513
214514 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
214515 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
214516 );
214517 if( pNew==0 ){
214518 rc = SQLITE_NOMEM;
214519 }else{
214520 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
214521 pCtx->pPhrase = pPhrase = pNew;
214522 pNew->nTerm = nNew - SZALLOC;
214523 }
214524 }
214525
214526 if( rc==SQLITE_OK ){
214527 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
214528 memset(pTerm, 0, sizeof(Fts5ExprTerm));
214529 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
214530 }
214531 }
214532
214533 pCtx->rc = rc;
214534 return rc;
214535}
214536
214537
214538/*
214539** Free the phrase object passed as the only argument.
214540*/
214541static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
214542 fts5ExprPhraseFree(pPhrase);
214543}
214544
214545/*
214546** Free the phrase object passed as the second argument.
214547*/
214548static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
214549 if( pNear ){
214550 int i;
214551 for(i=0; i<pNear->nPhrase; i++){
214552 fts5ExprPhraseFree(pNear->apPhrase[i]);
214553 }
214554 sqlite3_free(pNear->pColset);
214555 sqlite3_free(pNear);
214556 }
214557}
214558
214559static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
214560 assert( pParse->pExpr==0 );
214561 pParse->pExpr = p;
214562}
214563
214564/*
214565** This function is called by the parser to process a string token. The
214566** string may or may not be quoted. In any case it is tokenized and a
214567** phrase object consisting of all tokens returned.
214568*/
214569static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
214570 Fts5Parse *pParse, /* Parse context */
214571 Fts5ExprPhrase *pAppend, /* Phrase to append to */
214572 Fts5Token *pToken, /* String to tokenize */
214573 int bPrefix /* True if there is a trailing "*" */
214574){
214575 Fts5Config *pConfig = pParse->pConfig;
214576 TokenCtx sCtx; /* Context object passed to callback */
214577 int rc; /* Tokenize return code */
214578 char *z = 0;
214579
214580 memset(&sCtx, 0, sizeof(TokenCtx));
214581 sCtx.pPhrase = pAppend;
214582
214583 rc = fts5ParseStringFromToken(pToken, &z);
214584 if( rc==SQLITE_OK ){
214585 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
214586 int n;
214587 sqlite3Fts5Dequote(z);
214588 n = (int)strlen(z);
214589 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
214590 }
214591 sqlite3_free(z);
214592 if( rc || (rc = sCtx.rc) ){
214593 pParse->rc = rc;
214594 fts5ExprPhraseFree(sCtx.pPhrase);
214595 sCtx.pPhrase = 0;
214596 }else{
214597
214598 if( pAppend==0 ){
214599 if( (pParse->nPhrase % 8)==0 ){
214600 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
214601 Fts5ExprPhrase **apNew;
214602 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
214603 if( apNew==0 ){
214604 pParse->rc = SQLITE_NOMEM;
214605 fts5ExprPhraseFree(sCtx.pPhrase);
214606 return 0;
214607 }
214608 pParse->apPhrase = apNew;
214609 }
214610 pParse->nPhrase++;
214611 }
214612
214613 if( sCtx.pPhrase==0 ){
214614 /* This happens when parsing a token or quoted phrase that contains
214615 ** no token characters at all. (e.g ... MATCH '""'). */
214616 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
214617 }else if( sCtx.pPhrase->nTerm ){
214618 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
214619 }
214620 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
214621 }
214622
214623 return sCtx.pPhrase;
214624}
214625
214626/*
214627** Create a new FTS5 expression by cloning phrase iPhrase of the
214628** expression passed as the second argument.
214629*/
214630static int sqlite3Fts5ExprClonePhrase(
214631 Fts5Expr *pExpr,
214632 int iPhrase,
214633 Fts5Expr **ppNew
214634){
214635 int rc = SQLITE_OK; /* Return code */
214636 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
214637 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
214638 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
214639
214640 pOrig = pExpr->apExprPhrase[iPhrase];
214641 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
214642 if( rc==SQLITE_OK ){
214643 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
214644 sizeof(Fts5ExprPhrase*));
214645 }
214646 if( rc==SQLITE_OK ){
214647 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
214648 sizeof(Fts5ExprNode));
214649 }
214650 if( rc==SQLITE_OK ){
214651 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
214652 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
214653 }
214654 if( rc==SQLITE_OK ){
214655 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
214656 if( pColsetOrig ){
214657 sqlite3_int64 nByte;
214658 Fts5Colset *pColset;
214659 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
214660 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
214661 if( pColset ){
214662 memcpy(pColset, pColsetOrig, (size_t)nByte);
214663 }
214664 pNew->pRoot->pNear->pColset = pColset;
214665 }
214666 }
214667
214668 if( pOrig->nTerm ){
214669 int i; /* Used to iterate through phrase terms */
214670 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
214671 int tflags = 0;
214672 Fts5ExprTerm *p;
214673 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
214674 const char *zTerm = p->zTerm;
214675 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
214676 0, 0);
214677 tflags = FTS5_TOKEN_COLOCATED;
214678 }
214679 if( rc==SQLITE_OK ){
214680 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
214681 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
214682 }
214683 }
214684 }else{
214685 /* This happens when parsing a token or quoted phrase that contains
214686 ** no token characters at all. (e.g ... MATCH '""'). */
214687 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
214688 }
214689
214690 if( rc==SQLITE_OK ){
214691 /* All the allocations succeeded. Put the expression object together. */
214692 pNew->pIndex = pExpr->pIndex;
214693 pNew->pConfig = pExpr->pConfig;
214694 pNew->nPhrase = 1;
214695 pNew->apExprPhrase[0] = sCtx.pPhrase;
214696 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
214697 pNew->pRoot->pNear->nPhrase = 1;
214698 sCtx.pPhrase->pNode = pNew->pRoot;
214699
214700 if( pOrig->nTerm==1
214701 && pOrig->aTerm[0].pSynonym==0
214702 && pOrig->aTerm[0].bFirst==0
214703 ){
214704 pNew->pRoot->eType = FTS5_TERM;
214705 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
214706 }else{
214707 pNew->pRoot->eType = FTS5_STRING;
214708 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
214709 }
214710 }else{
214711 sqlite3Fts5ExprFree(pNew);
214712 fts5ExprPhraseFree(sCtx.pPhrase);
214713 pNew = 0;
214714 }
214715
214716 *ppNew = pNew;
214717 return rc;
214718}
214719
214720
214721/*
214722** Token pTok has appeared in a MATCH expression where the NEAR operator
214723** is expected. If token pTok does not contain "NEAR", store an error
214724** in the pParse object.
214725*/
214726static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
214727 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
214728 sqlite3Fts5ParseError(
214729 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
214730 );
214731 }
214732}
214733
214734static void sqlite3Fts5ParseSetDistance(
214735 Fts5Parse *pParse,
214736 Fts5ExprNearset *pNear,
214737 Fts5Token *p
214738){
214739 if( pNear ){
214740 int nNear = 0;
214741 int i;
214742 if( p->n ){
214743 for(i=0; i<p->n; i++){
214744 char c = (char)p->p[i];
214745 if( c<'0' || c>'9' ){
214746 sqlite3Fts5ParseError(
214747 pParse, "expected integer, got \"%.*s\"", p->n, p->p
214748 );
214749 return;
214750 }
214751 nNear = nNear * 10 + (p->p[i] - '0');
214752 }
214753 }else{
214754 nNear = FTS5_DEFAULT_NEARDIST;
214755 }
214756 pNear->nNear = nNear;
214757 }
214758}
214759
214760/*
214761** The second argument passed to this function may be NULL, or it may be
214762** an existing Fts5Colset object. This function returns a pointer to
214763** a new colset object containing the contents of (p) with new value column
214764** number iCol appended.
214765**
214766** If an OOM error occurs, store an error code in pParse and return NULL.
214767** The old colset object (if any) is not freed in this case.
214768*/
214769static Fts5Colset *fts5ParseColset(
214770 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
214771 Fts5Colset *p, /* Existing colset object */
214772 int iCol /* New column to add to colset object */
214773){
214774 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
214775 Fts5Colset *pNew; /* New colset object to return */
214776
214777 assert( pParse->rc==SQLITE_OK );
214778 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
214779
214780 pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
214781 if( pNew==0 ){
214782 pParse->rc = SQLITE_NOMEM;
214783 }else{
214784 int *aiCol = pNew->aiCol;
214785 int i, j;
214786 for(i=0; i<nCol; i++){
214787 if( aiCol[i]==iCol ) return pNew;
214788 if( aiCol[i]>iCol ) break;
214789 }
214790 for(j=nCol; j>i; j--){
214791 aiCol[j] = aiCol[j-1];
214792 }
214793 aiCol[i] = iCol;
214794 pNew->nCol = nCol+1;
214795
214796#ifndef NDEBUG
214797 /* Check that the array is in order and contains no duplicate entries. */
214798 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
214799#endif
214800 }
214801
214802 return pNew;
214803}
214804
214805/*
214806** Allocate and return an Fts5Colset object specifying the inverse of
214807** the colset passed as the second argument. Free the colset passed
214808** as the second argument before returning.
214809*/
214810static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
214811 Fts5Colset *pRet;
214812 int nCol = pParse->pConfig->nCol;
214813
214814 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
214815 sizeof(Fts5Colset) + sizeof(int)*nCol
214816 );
214817 if( pRet ){
214818 int i;
214819 int iOld = 0;
214820 for(i=0; i<nCol; i++){
214821 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
214822 pRet->aiCol[pRet->nCol++] = i;
214823 }else{
214824 iOld++;
214825 }
214826 }
214827 }
214828
214829 sqlite3_free(p);
214830 return pRet;
214831}
214832
214833static Fts5Colset *sqlite3Fts5ParseColset(
214834 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
214835 Fts5Colset *pColset, /* Existing colset object */
214836 Fts5Token *p
214837){
214838 Fts5Colset *pRet = 0;
214839 int iCol;
214840 char *z; /* Dequoted copy of token p */
214841
214842 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
214843 if( pParse->rc==SQLITE_OK ){
214844 Fts5Config *pConfig = pParse->pConfig;
214845 sqlite3Fts5Dequote(z);
214846 for(iCol=0; iCol<pConfig->nCol; iCol++){
214847 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
214848 }
214849 if( iCol==pConfig->nCol ){
214850 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
214851 }else{
214852 pRet = fts5ParseColset(pParse, pColset, iCol);
214853 }
214854 sqlite3_free(z);
214855 }
214856
214857 if( pRet==0 ){
214858 assert( pParse->rc!=SQLITE_OK );
214859 sqlite3_free(pColset);
214860 }
214861
214862 return pRet;
214863}
214864
214865/*
214866** If argument pOrig is NULL, or if (*pRc) is set to anything other than
214867** SQLITE_OK when this function is called, NULL is returned.
214868**
214869** Otherwise, a copy of (*pOrig) is made into memory obtained from
214870** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
214871** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
214872*/
214873static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
214874 Fts5Colset *pRet;
214875 if( pOrig ){
214876 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
214877 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
214878 if( pRet ){
214879 memcpy(pRet, pOrig, (size_t)nByte);
214880 }
214881 }else{
214882 pRet = 0;
214883 }
214884 return pRet;
214885}
214886
214887/*
214888** Remove from colset pColset any columns that are not also in colset pMerge.
214889*/
214890static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
214891 int iIn = 0; /* Next input in pColset */
214892 int iMerge = 0; /* Next input in pMerge */
214893 int iOut = 0; /* Next output slot in pColset */
214894
214895 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
214896 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
214897 if( iDiff==0 ){
214898 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
214899 iMerge++;
214900 iIn++;
214901 }else if( iDiff>0 ){
214902 iMerge++;
214903 }else{
214904 iIn++;
214905 }
214906 }
214907 pColset->nCol = iOut;
214908}
214909
214910/*
214911** Recursively apply colset pColset to expression node pNode and all of
214912** its decendents. If (*ppFree) is not NULL, it contains a spare copy
214913** of pColset. This function may use the spare copy and set (*ppFree) to
214914** zero, or it may create copies of pColset using fts5CloneColset().
214915*/
214916static void fts5ParseSetColset(
214917 Fts5Parse *pParse,
214918 Fts5ExprNode *pNode,
214919 Fts5Colset *pColset,
214920 Fts5Colset **ppFree
214921){
214922 if( pParse->rc==SQLITE_OK ){
214923 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
214924 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
214925 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
214926 );
214927 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
214928 Fts5ExprNearset *pNear = pNode->pNear;
214929 if( pNear->pColset ){
214930 fts5MergeColset(pNear->pColset, pColset);
214931 if( pNear->pColset->nCol==0 ){
214932 pNode->eType = FTS5_EOF;
214933 pNode->xNext = 0;
214934 }
214935 }else if( *ppFree ){
214936 pNear->pColset = pColset;
214937 *ppFree = 0;
214938 }else{
214939 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
214940 }
214941 }else{
214942 int i;
214943 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
214944 for(i=0; i<pNode->nChild; i++){
214945 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
214946 }
214947 }
214948 }
214949}
214950
214951/*
214952** Apply colset pColset to expression node pExpr and all of its descendents.
214953*/
214954static void sqlite3Fts5ParseSetColset(
214955 Fts5Parse *pParse,
214956 Fts5ExprNode *pExpr,
214957 Fts5Colset *pColset
214958){
214959 Fts5Colset *pFree = pColset;
214960 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
214961 pParse->rc = SQLITE_ERROR;
214962 pParse->zErr = sqlite3_mprintf(
214963 "fts5: column queries are not supported (detail=none)"
214964 );
214965 }else{
214966 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
214967 }
214968 sqlite3_free(pFree);
214969}
214970
214971static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
214972 switch( pNode->eType ){
214973 case FTS5_STRING: {
214974 Fts5ExprNearset *pNear = pNode->pNear;
214975 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
214976 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
214977 && pNear->apPhrase[0]->aTerm[0].bFirst==0
214978 ){
214979 pNode->eType = FTS5_TERM;
214980 pNode->xNext = fts5ExprNodeNext_TERM;
214981 }else{
214982 pNode->xNext = fts5ExprNodeNext_STRING;
214983 }
214984 break;
214985 };
214986
214987 case FTS5_OR: {
214988 pNode->xNext = fts5ExprNodeNext_OR;
214989 break;
214990 };
214991
214992 case FTS5_AND: {
214993 pNode->xNext = fts5ExprNodeNext_AND;
214994 break;
214995 };
214996
214997 default: assert( pNode->eType==FTS5_NOT ); {
214998 pNode->xNext = fts5ExprNodeNext_NOT;
214999 break;
215000 };
215001 }
215002}
215003
215004static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
215005 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
215006 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
215007 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
215008 p->nChild += pSub->nChild;
215009 sqlite3_free(pSub);
215010 }else{
215011 p->apChild[p->nChild++] = pSub;
215012 }
215013}
215014
215015/*
215016** Allocate and return a new expression object. If anything goes wrong (i.e.
215017** OOM error), leave an error code in pParse and return NULL.
215018*/
215019static Fts5ExprNode *sqlite3Fts5ParseNode(
215020 Fts5Parse *pParse, /* Parse context */
215021 int eType, /* FTS5_STRING, AND, OR or NOT */
215022 Fts5ExprNode *pLeft, /* Left hand child expression */
215023 Fts5ExprNode *pRight, /* Right hand child expression */
215024 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
215025){
215026 Fts5ExprNode *pRet = 0;
215027
215028 if( pParse->rc==SQLITE_OK ){
215029 int nChild = 0; /* Number of children of returned node */
215030 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
215031
215032 assert( (eType!=FTS5_STRING && !pNear)
215033 || (eType==FTS5_STRING && !pLeft && !pRight)
215034 );
215035 if( eType==FTS5_STRING && pNear==0 ) return 0;
215036 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
215037 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
215038
215039 if( eType==FTS5_NOT ){
215040 nChild = 2;
215041 }else if( eType==FTS5_AND || eType==FTS5_OR ){
215042 nChild = 2;
215043 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
215044 if( pRight->eType==eType ) nChild += pRight->nChild-1;
215045 }
215046
215047 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
215048 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
215049
215050 if( pRet ){
215051 pRet->eType = eType;
215052 pRet->pNear = pNear;
215053 fts5ExprAssignXNext(pRet);
215054 if( eType==FTS5_STRING ){
215055 int iPhrase;
215056 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
215057 pNear->apPhrase[iPhrase]->pNode = pRet;
215058 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
215059 pRet->xNext = 0;
215060 pRet->eType = FTS5_EOF;
215061 }
215062 }
215063
215064 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
215065 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
215066 if( pNear->nPhrase!=1
215067 || pPhrase->nTerm>1
215068 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
215069 ){
215070 assert( pParse->rc==SQLITE_OK );
215071 pParse->rc = SQLITE_ERROR;
215072 assert( pParse->zErr==0 );
215073 pParse->zErr = sqlite3_mprintf(
215074 "fts5: %s queries are not supported (detail!=full)",
215075 pNear->nPhrase==1 ? "phrase": "NEAR"
215076 );
215077 sqlite3_free(pRet);
215078 pRet = 0;
215079 }
215080 }
215081 }else{
215082 fts5ExprAddChildren(pRet, pLeft);
215083 fts5ExprAddChildren(pRet, pRight);
215084 }
215085 }
215086 }
215087
215088 if( pRet==0 ){
215089 assert( pParse->rc!=SQLITE_OK );
215090 sqlite3Fts5ParseNodeFree(pLeft);
215091 sqlite3Fts5ParseNodeFree(pRight);
215092 sqlite3Fts5ParseNearsetFree(pNear);
215093 }
215094 return pRet;
215095}
215096
215097static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
215098 Fts5Parse *pParse, /* Parse context */
215099 Fts5ExprNode *pLeft, /* Left hand child expression */
215100 Fts5ExprNode *pRight /* Right hand child expression */
215101){
215102 Fts5ExprNode *pRet = 0;
215103 Fts5ExprNode *pPrev;
215104
215105 if( pParse->rc ){
215106 sqlite3Fts5ParseNodeFree(pLeft);
215107 sqlite3Fts5ParseNodeFree(pRight);
215108 }else{
215109
215110 assert( pLeft->eType==FTS5_STRING
215111 || pLeft->eType==FTS5_TERM
215112 || pLeft->eType==FTS5_EOF
215113 || pLeft->eType==FTS5_AND
215114 );
215115 assert( pRight->eType==FTS5_STRING
215116 || pRight->eType==FTS5_TERM
215117 || pRight->eType==FTS5_EOF
215118 );
215119
215120 if( pLeft->eType==FTS5_AND ){
215121 pPrev = pLeft->apChild[pLeft->nChild-1];
215122 }else{
215123 pPrev = pLeft;
215124 }
215125 assert( pPrev->eType==FTS5_STRING
215126 || pPrev->eType==FTS5_TERM
215127 || pPrev->eType==FTS5_EOF
215128 );
215129
215130 if( pRight->eType==FTS5_EOF ){
215131 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
215132 sqlite3Fts5ParseNodeFree(pRight);
215133 pRet = pLeft;
215134 pParse->nPhrase--;
215135 }
215136 else if( pPrev->eType==FTS5_EOF ){
215137 Fts5ExprPhrase **ap;
215138
215139 if( pPrev==pLeft ){
215140 pRet = pRight;
215141 }else{
215142 pLeft->apChild[pLeft->nChild-1] = pRight;
215143 pRet = pLeft;
215144 }
215145
215146 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
215147 assert( ap[0]==pPrev->pNear->apPhrase[0] );
215148 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
215149 pParse->nPhrase--;
215150
215151 sqlite3Fts5ParseNodeFree(pPrev);
215152 }
215153 else{
215154 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
215155 }
215156 }
215157
215158 return pRet;
215159}
215160
215161static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
215162 sqlite3_int64 nByte = 0;
215163 Fts5ExprTerm *p;
215164 char *zQuoted;
215165
215166 /* Determine the maximum amount of space required. */
215167 for(p=pTerm; p; p=p->pSynonym){
215168 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
215169 }
215170 zQuoted = sqlite3_malloc64(nByte);
215171
215172 if( zQuoted ){
215173 int i = 0;
215174 for(p=pTerm; p; p=p->pSynonym){
215175 char *zIn = p->zTerm;
215176 zQuoted[i++] = '"';
215177 while( *zIn ){
215178 if( *zIn=='"' ) zQuoted[i++] = '"';
215179 zQuoted[i++] = *zIn++;
215180 }
215181 zQuoted[i++] = '"';
215182 if( p->pSynonym ) zQuoted[i++] = '|';
215183 }
215184 if( pTerm->bPrefix ){
215185 zQuoted[i++] = ' ';
215186 zQuoted[i++] = '*';
215187 }
215188 zQuoted[i++] = '\0';
215189 }
215190 return zQuoted;
215191}
215192
215193static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
215194 char *zNew;
215195 va_list ap;
215196 va_start(ap, zFmt);
215197 zNew = sqlite3_vmprintf(zFmt, ap);
215198 va_end(ap);
215199 if( zApp && zNew ){
215200 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
215201 sqlite3_free(zNew);
215202 zNew = zNew2;
215203 }
215204 sqlite3_free(zApp);
215205 return zNew;
215206}
215207
215208/*
215209** Compose a tcl-readable representation of expression pExpr. Return a
215210** pointer to a buffer containing that representation. It is the
215211** responsibility of the caller to at some point free the buffer using
215212** sqlite3_free().
215213*/
215214static char *fts5ExprPrintTcl(
215215 Fts5Config *pConfig,
215216 const char *zNearsetCmd,
215217 Fts5ExprNode *pExpr
215218){
215219 char *zRet = 0;
215220 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
215221 Fts5ExprNearset *pNear = pExpr->pNear;
215222 int i;
215223 int iTerm;
215224
215225 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
215226 if( zRet==0 ) return 0;
215227 if( pNear->pColset ){
215228 int *aiCol = pNear->pColset->aiCol;
215229 int nCol = pNear->pColset->nCol;
215230 if( nCol==1 ){
215231 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
215232 }else{
215233 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
215234 for(i=1; i<pNear->pColset->nCol; i++){
215235 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
215236 }
215237 zRet = fts5PrintfAppend(zRet, "} ");
215238 }
215239 if( zRet==0 ) return 0;
215240 }
215241
215242 if( pNear->nPhrase>1 ){
215243 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
215244 if( zRet==0 ) return 0;
215245 }
215246
215247 zRet = fts5PrintfAppend(zRet, "--");
215248 if( zRet==0 ) return 0;
215249
215250 for(i=0; i<pNear->nPhrase; i++){
215251 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
215252
215253 zRet = fts5PrintfAppend(zRet, " {");
215254 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
215255 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
215256 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
215257 if( pPhrase->aTerm[iTerm].bPrefix ){
215258 zRet = fts5PrintfAppend(zRet, "*");
215259 }
215260 }
215261
215262 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
215263 if( zRet==0 ) return 0;
215264 }
215265
215266 }else{
215267 char const *zOp = 0;
215268 int i;
215269 switch( pExpr->eType ){
215270 case FTS5_AND: zOp = "AND"; break;
215271 case FTS5_NOT: zOp = "NOT"; break;
215272 default:
215273 assert( pExpr->eType==FTS5_OR );
215274 zOp = "OR";
215275 break;
215276 }
215277
215278 zRet = sqlite3_mprintf("%s", zOp);
215279 for(i=0; zRet && i<pExpr->nChild; i++){
215280 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
215281 if( !z ){
215282 sqlite3_free(zRet);
215283 zRet = 0;
215284 }else{
215285 zRet = fts5PrintfAppend(zRet, " [%z]", z);
215286 }
215287 }
215288 }
215289
215290 return zRet;
215291}
215292
215293static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
215294 char *zRet = 0;
215295 if( pExpr->eType==0 ){
215296 return sqlite3_mprintf("\"\"");
215297 }else
215298 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
215299 Fts5ExprNearset *pNear = pExpr->pNear;
215300 int i;
215301 int iTerm;
215302
215303 if( pNear->pColset ){
215304 int iCol = pNear->pColset->aiCol[0];
215305 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
215306 if( zRet==0 ) return 0;
215307 }
215308
215309 if( pNear->nPhrase>1 ){
215310 zRet = fts5PrintfAppend(zRet, "NEAR(");
215311 if( zRet==0 ) return 0;
215312 }
215313
215314 for(i=0; i<pNear->nPhrase; i++){
215315 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
215316 if( i!=0 ){
215317 zRet = fts5PrintfAppend(zRet, " ");
215318 if( zRet==0 ) return 0;
215319 }
215320 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
215321 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
215322 if( zTerm ){
215323 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
215324 sqlite3_free(zTerm);
215325 }
215326 if( zTerm==0 || zRet==0 ){
215327 sqlite3_free(zRet);
215328 return 0;
215329 }
215330 }
215331 }
215332
215333 if( pNear->nPhrase>1 ){
215334 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
215335 if( zRet==0 ) return 0;
215336 }
215337
215338 }else{
215339 char const *zOp = 0;
215340 int i;
215341
215342 switch( pExpr->eType ){
215343 case FTS5_AND: zOp = " AND "; break;
215344 case FTS5_NOT: zOp = " NOT "; break;
215345 default:
215346 assert( pExpr->eType==FTS5_OR );
215347 zOp = " OR ";
215348 break;
215349 }
215350
215351 for(i=0; i<pExpr->nChild; i++){
215352 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
215353 if( z==0 ){
215354 sqlite3_free(zRet);
215355 zRet = 0;
215356 }else{
215357 int e = pExpr->apChild[i]->eType;
215358 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
215359 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
215360 (i==0 ? "" : zOp),
215361 (b?"(":""), z, (b?")":"")
215362 );
215363 }
215364 if( zRet==0 ) break;
215365 }
215366 }
215367
215368 return zRet;
215369}
215370
215371/*
215372** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
215373** and fts5_expr_tcl() (bTcl!=0).
215374*/
215375static void fts5ExprFunction(
215376 sqlite3_context *pCtx, /* Function call context */
215377 int nArg, /* Number of args */
215378 sqlite3_value **apVal, /* Function arguments */
215379 int bTcl
215380){
215381 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
215382 sqlite3 *db = sqlite3_context_db_handle(pCtx);
215383 const char *zExpr = 0;
215384 char *zErr = 0;
215385 Fts5Expr *pExpr = 0;
215386 int rc;
215387 int i;
215388
215389 const char **azConfig; /* Array of arguments for Fts5Config */
215390 const char *zNearsetCmd = "nearset";
215391 int nConfig; /* Size of azConfig[] */
215392 Fts5Config *pConfig = 0;
215393 int iArg = 1;
215394
215395 if( nArg<1 ){
215396 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
215397 bTcl ? "fts5_expr_tcl" : "fts5_expr"
215398 );
215399 sqlite3_result_error(pCtx, zErr, -1);
215400 sqlite3_free(zErr);
215401 return;
215402 }
215403
215404 if( bTcl && nArg>1 ){
215405 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
215406 iArg = 2;
215407 }
215408
215409 nConfig = 3 + (nArg-iArg);
215410 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
215411 if( azConfig==0 ){
215412 sqlite3_result_error_nomem(pCtx);
215413 return;
215414 }
215415 azConfig[0] = 0;
215416 azConfig[1] = "main";
215417 azConfig[2] = "tbl";
215418 for(i=3; iArg<nArg; iArg++){
215419 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
215420 azConfig[i++] = (z ? z : "");
215421 }
215422
215423 zExpr = (const char*)sqlite3_value_text(apVal[0]);
215424 if( zExpr==0 ) zExpr = "";
215425
215426 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
215427 if( rc==SQLITE_OK ){
215428 rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
215429 }
215430 if( rc==SQLITE_OK ){
215431 char *zText;
215432 if( pExpr->pRoot->xNext==0 ){
215433 zText = sqlite3_mprintf("");
215434 }else if( bTcl ){
215435 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
215436 }else{
215437 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
215438 }
215439 if( zText==0 ){
215440 rc = SQLITE_NOMEM;
215441 }else{
215442 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
215443 sqlite3_free(zText);
215444 }
215445 }
215446
215447 if( rc!=SQLITE_OK ){
215448 if( zErr ){
215449 sqlite3_result_error(pCtx, zErr, -1);
215450 sqlite3_free(zErr);
215451 }else{
215452 sqlite3_result_error_code(pCtx, rc);
215453 }
215454 }
215455 sqlite3_free((void *)azConfig);
215456 sqlite3Fts5ConfigFree(pConfig);
215457 sqlite3Fts5ExprFree(pExpr);
215458}
215459
215460static void fts5ExprFunctionHr(
215461 sqlite3_context *pCtx, /* Function call context */
215462 int nArg, /* Number of args */
215463 sqlite3_value **apVal /* Function arguments */
215464){
215465 fts5ExprFunction(pCtx, nArg, apVal, 0);
215466}
215467static void fts5ExprFunctionTcl(
215468 sqlite3_context *pCtx, /* Function call context */
215469 int nArg, /* Number of args */
215470 sqlite3_value **apVal /* Function arguments */
215471){
215472 fts5ExprFunction(pCtx, nArg, apVal, 1);
215473}
215474
215475/*
215476** The implementation of an SQLite user-defined-function that accepts a
215477** single integer as an argument. If the integer is an alpha-numeric
215478** unicode code point, 1 is returned. Otherwise 0.
215479*/
215480static void fts5ExprIsAlnum(
215481 sqlite3_context *pCtx, /* Function call context */
215482 int nArg, /* Number of args */
215483 sqlite3_value **apVal /* Function arguments */
215484){
215485 int iCode;
215486 u8 aArr[32];
215487 if( nArg!=1 ){
215488 sqlite3_result_error(pCtx,
215489 "wrong number of arguments to function fts5_isalnum", -1
215490 );
215491 return;
215492 }
215493 memset(aArr, 0, sizeof(aArr));
215494 sqlite3Fts5UnicodeCatParse("L*", aArr);
215495 sqlite3Fts5UnicodeCatParse("N*", aArr);
215496 sqlite3Fts5UnicodeCatParse("Co", aArr);
215497 iCode = sqlite3_value_int(apVal[0]);
215498 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
215499}
215500
215501static void fts5ExprFold(
215502 sqlite3_context *pCtx, /* Function call context */
215503 int nArg, /* Number of args */
215504 sqlite3_value **apVal /* Function arguments */
215505){
215506 if( nArg!=1 && nArg!=2 ){
215507 sqlite3_result_error(pCtx,
215508 "wrong number of arguments to function fts5_fold", -1
215509 );
215510 }else{
215511 int iCode;
215512 int bRemoveDiacritics = 0;
215513 iCode = sqlite3_value_int(apVal[0]);
215514 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
215515 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
215516 }
215517}
215518
215519/*
215520** This is called during initialization to register the fts5_expr() scalar
215521** UDF with the SQLite handle passed as the only argument.
215522*/
215523static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
215524 struct Fts5ExprFunc {
215525 const char *z;
215526 void (*x)(sqlite3_context*,int,sqlite3_value**);
215527 } aFunc[] = {
215528 { "fts5_expr", fts5ExprFunctionHr },
215529 { "fts5_expr_tcl", fts5ExprFunctionTcl },
215530 { "fts5_isalnum", fts5ExprIsAlnum },
215531 { "fts5_fold", fts5ExprFold },
215532 };
215533 int i;
215534 int rc = SQLITE_OK;
215535 void *pCtx = (void*)pGlobal;
215536
215537 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
215538 struct Fts5ExprFunc *p = &aFunc[i];
215539 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
215540 }
215541
215542 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
215543 ** sqlite3Fts5ParserFallback() are unused */
215544#ifndef NDEBUG
215545 (void)sqlite3Fts5ParserTrace;
215546#endif
215547 (void)sqlite3Fts5ParserFallback;
215548
215549 return rc;
215550}
215551
215552/*
215553** Return the number of phrases in expression pExpr.
215554*/
215555static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
215556 return (pExpr ? pExpr->nPhrase : 0);
215557}
215558
215559/*
215560** Return the number of terms in the iPhrase'th phrase in pExpr.
215561*/
215562static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
215563 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
215564 return pExpr->apExprPhrase[iPhrase]->nTerm;
215565}
215566
215567/*
215568** This function is used to access the current position list for phrase
215569** iPhrase.
215570*/
215571static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
215572 int nRet;
215573 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
215574 Fts5ExprNode *pNode = pPhrase->pNode;
215575 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
215576 *pa = pPhrase->poslist.p;
215577 nRet = pPhrase->poslist.n;
215578 }else{
215579 *pa = 0;
215580 nRet = 0;
215581 }
215582 return nRet;
215583}
215584
215585struct Fts5PoslistPopulator {
215586 Fts5PoslistWriter writer;
215587 int bOk; /* True if ok to populate */
215588 int bMiss;
215589};
215590
215591static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
215592 Fts5PoslistPopulator *pRet;
215593 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
215594 if( pRet ){
215595 int i;
215596 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
215597 for(i=0; i<pExpr->nPhrase; i++){
215598 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
215599 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
215600 assert( pExpr->apExprPhrase[i]->nTerm==1 );
215601 if( bLive &&
215602 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
215603 ){
215604 pRet[i].bMiss = 1;
215605 }else{
215606 pBuf->n = 0;
215607 }
215608 }
215609 }
215610 return pRet;
215611}
215612
215613struct Fts5ExprCtx {
215614 Fts5Expr *pExpr;
215615 Fts5PoslistPopulator *aPopulator;
215616 i64 iOff;
215617};
215618typedef struct Fts5ExprCtx Fts5ExprCtx;
215619
215620/*
215621** TODO: Make this more efficient!
215622*/
215623static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
215624 int i;
215625 for(i=0; i<pColset->nCol; i++){
215626 if( pColset->aiCol[i]==iCol ) return 1;
215627 }
215628 return 0;
215629}
215630
215631static int fts5ExprPopulatePoslistsCb(
215632 void *pCtx, /* Copy of 2nd argument to xTokenize() */
215633 int tflags, /* Mask of FTS5_TOKEN_* flags */
215634 const char *pToken, /* Pointer to buffer containing token */
215635 int nToken, /* Size of token in bytes */
215636 int iUnused1, /* Byte offset of token within input text */
215637 int iUnused2 /* Byte offset of end of token within input text */
215638){
215639 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
215640 Fts5Expr *pExpr = p->pExpr;
215641 int i;
215642
215643 UNUSED_PARAM2(iUnused1, iUnused2);
215644
215645 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
215646 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
215647 for(i=0; i<pExpr->nPhrase; i++){
215648 Fts5ExprTerm *pTerm;
215649 if( p->aPopulator[i].bOk==0 ) continue;
215650 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
215651 int nTerm = (int)strlen(pTerm->zTerm);
215652 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
215653 && memcmp(pTerm->zTerm, pToken, nTerm)==0
215654 ){
215655 int rc = sqlite3Fts5PoslistWriterAppend(
215656 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
215657 );
215658 if( rc ) return rc;
215659 break;
215660 }
215661 }
215662 }
215663 return SQLITE_OK;
215664}
215665
215666static int sqlite3Fts5ExprPopulatePoslists(
215667 Fts5Config *pConfig,
215668 Fts5Expr *pExpr,
215669 Fts5PoslistPopulator *aPopulator,
215670 int iCol,
215671 const char *z, int n
215672){
215673 int i;
215674 Fts5ExprCtx sCtx;
215675 sCtx.pExpr = pExpr;
215676 sCtx.aPopulator = aPopulator;
215677 sCtx.iOff = (((i64)iCol) << 32) - 1;
215678
215679 for(i=0; i<pExpr->nPhrase; i++){
215680 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
215681 Fts5Colset *pColset = pNode->pNear->pColset;
215682 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
215683 || aPopulator[i].bMiss
215684 ){
215685 aPopulator[i].bOk = 0;
215686 }else{
215687 aPopulator[i].bOk = 1;
215688 }
215689 }
215690
215691 return sqlite3Fts5Tokenize(pConfig,
215692 FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
215693 );
215694}
215695
215696static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
215697 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
215698 pNode->pNear->apPhrase[0]->poslist.n = 0;
215699 }else{
215700 int i;
215701 for(i=0; i<pNode->nChild; i++){
215702 fts5ExprClearPoslists(pNode->apChild[i]);
215703 }
215704 }
215705}
215706
215707static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
215708 pNode->iRowid = iRowid;
215709 pNode->bEof = 0;
215710 switch( pNode->eType ){
215711 case FTS5_TERM:
215712 case FTS5_STRING:
215713 return (pNode->pNear->apPhrase[0]->poslist.n>0);
215714
215715 case FTS5_AND: {
215716 int i;
215717 for(i=0; i<pNode->nChild; i++){
215718 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
215719 fts5ExprClearPoslists(pNode);
215720 return 0;
215721 }
215722 }
215723 break;
215724 }
215725
215726 case FTS5_OR: {
215727 int i;
215728 int bRet = 0;
215729 for(i=0; i<pNode->nChild; i++){
215730 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
215731 bRet = 1;
215732 }
215733 }
215734 return bRet;
215735 }
215736
215737 default: {
215738 assert( pNode->eType==FTS5_NOT );
215739 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
215740 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
215741 ){
215742 fts5ExprClearPoslists(pNode);
215743 return 0;
215744 }
215745 break;
215746 }
215747 }
215748 return 1;
215749}
215750
215751static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
215752 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
215753}
215754
215755/*
215756** This function is only called for detail=columns tables.
215757*/
215758static int sqlite3Fts5ExprPhraseCollist(
215759 Fts5Expr *pExpr,
215760 int iPhrase,
215761 const u8 **ppCollist,
215762 int *pnCollist
215763){
215764 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
215765 Fts5ExprNode *pNode = pPhrase->pNode;
215766 int rc = SQLITE_OK;
215767
215768 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
215769 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
215770
215771 if( pNode->bEof==0
215772 && pNode->iRowid==pExpr->pRoot->iRowid
215773 && pPhrase->poslist.n>0
215774 ){
215775 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
215776 if( pTerm->pSynonym ){
215777 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
215778 rc = fts5ExprSynonymList(
215779 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
215780 );
215781 }else{
215782 *ppCollist = pPhrase->aTerm[0].pIter->pData;
215783 *pnCollist = pPhrase->aTerm[0].pIter->nData;
215784 }
215785 }else{
215786 *ppCollist = 0;
215787 *pnCollist = 0;
215788 }
215789
215790 return rc;
215791}
215792
215793/*
215794** 2014 August 11
215795**
215796** The author disclaims copyright to this source code. In place of
215797** a legal notice, here is a blessing:
215798**
215799** May you do good and not evil.
215800** May you find forgiveness for yourself and forgive others.
215801** May you share freely, never taking more than you give.
215802**
215803******************************************************************************
215804**
215805*/
215806
215807
215808
215809/* #include "fts5Int.h" */
215810
215811typedef struct Fts5HashEntry Fts5HashEntry;
215812
215813/*
215814** This file contains the implementation of an in-memory hash table used
215815** to accumuluate "term -> doclist" content before it is flused to a level-0
215816** segment.
215817*/
215818
215819
215820struct Fts5Hash {
215821 int eDetail; /* Copy of Fts5Config.eDetail */
215822 int *pnByte; /* Pointer to bytes counter */
215823 int nEntry; /* Number of entries currently in hash */
215824 int nSlot; /* Size of aSlot[] array */
215825 Fts5HashEntry *pScan; /* Current ordered scan item */
215826 Fts5HashEntry **aSlot; /* Array of hash slots */
215827};
215828
215829/*
215830** Each entry in the hash table is represented by an object of the
215831** following type. Each object, its key (a nul-terminated string) and
215832** its current data are stored in a single memory allocation. The
215833** key immediately follows the object in memory. The position list
215834** data immediately follows the key data in memory.
215835**
215836** The data that follows the key is in a similar, but not identical format
215837** to the doclist data stored in the database. It is:
215838**
215839** * Rowid, as a varint
215840** * Position list, without 0x00 terminator.
215841** * Size of previous position list and rowid, as a 4 byte
215842** big-endian integer.
215843**
215844** iRowidOff:
215845** Offset of last rowid written to data area. Relative to first byte of
215846** structure.
215847**
215848** nData:
215849** Bytes of data written since iRowidOff.
215850*/
215851struct Fts5HashEntry {
215852 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
215853 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
215854
215855 int nAlloc; /* Total size of allocation */
215856 int iSzPoslist; /* Offset of space for 4-byte poslist size */
215857 int nData; /* Total bytes of data (incl. structure) */
215858 int nKey; /* Length of key in bytes */
215859 u8 bDel; /* Set delete-flag @ iSzPoslist */
215860 u8 bContent; /* Set content-flag (detail=none mode) */
215861 i16 iCol; /* Column of last value written */
215862 int iPos; /* Position of last value written */
215863 i64 iRowid; /* Rowid of last value written */
215864};
215865
215866/*
215867** Eqivalent to:
215868**
215869** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
215870*/
215871#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
215872
215873
215874/*
215875** Allocate a new hash table.
215876*/
215877static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
215878 int rc = SQLITE_OK;
215879 Fts5Hash *pNew;
215880
215881 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
215882 if( pNew==0 ){
215883 rc = SQLITE_NOMEM;
215884 }else{
215885 sqlite3_int64 nByte;
215886 memset(pNew, 0, sizeof(Fts5Hash));
215887 pNew->pnByte = pnByte;
215888 pNew->eDetail = pConfig->eDetail;
215889
215890 pNew->nSlot = 1024;
215891 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
215892 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
215893 if( pNew->aSlot==0 ){
215894 sqlite3_free(pNew);
215895 *ppNew = 0;
215896 rc = SQLITE_NOMEM;
215897 }else{
215898 memset(pNew->aSlot, 0, (size_t)nByte);
215899 }
215900 }
215901 return rc;
215902}
215903
215904/*
215905** Free a hash table object.
215906*/
215907static void sqlite3Fts5HashFree(Fts5Hash *pHash){
215908 if( pHash ){
215909 sqlite3Fts5HashClear(pHash);
215910 sqlite3_free(pHash->aSlot);
215911 sqlite3_free(pHash);
215912 }
215913}
215914
215915/*
215916** Empty (but do not delete) a hash table.
215917*/
215918static void sqlite3Fts5HashClear(Fts5Hash *pHash){
215919 int i;
215920 for(i=0; i<pHash->nSlot; i++){
215921 Fts5HashEntry *pNext;
215922 Fts5HashEntry *pSlot;
215923 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
215924 pNext = pSlot->pHashNext;
215925 sqlite3_free(pSlot);
215926 }
215927 }
215928 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
215929 pHash->nEntry = 0;
215930}
215931
215932static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
215933 int i;
215934 unsigned int h = 13;
215935 for(i=n-1; i>=0; i--){
215936 h = (h << 3) ^ h ^ p[i];
215937 }
215938 return (h % nSlot);
215939}
215940
215941static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
215942 int i;
215943 unsigned int h = 13;
215944 for(i=n-1; i>=0; i--){
215945 h = (h << 3) ^ h ^ p[i];
215946 }
215947 h = (h << 3) ^ h ^ b;
215948 return (h % nSlot);
215949}
215950
215951/*
215952** Resize the hash table by doubling the number of slots.
215953*/
215954static int fts5HashResize(Fts5Hash *pHash){
215955 int nNew = pHash->nSlot*2;
215956 int i;
215957 Fts5HashEntry **apNew;
215958 Fts5HashEntry **apOld = pHash->aSlot;
215959
215960 apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
215961 if( !apNew ) return SQLITE_NOMEM;
215962 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
215963
215964 for(i=0; i<pHash->nSlot; i++){
215965 while( apOld[i] ){
215966 unsigned int iHash;
215967 Fts5HashEntry *p = apOld[i];
215968 apOld[i] = p->pHashNext;
215969 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
215970 (int)strlen(fts5EntryKey(p)));
215971 p->pHashNext = apNew[iHash];
215972 apNew[iHash] = p;
215973 }
215974 }
215975
215976 sqlite3_free(apOld);
215977 pHash->nSlot = nNew;
215978 pHash->aSlot = apNew;
215979 return SQLITE_OK;
215980}
215981
215982static int fts5HashAddPoslistSize(
215983 Fts5Hash *pHash,
215984 Fts5HashEntry *p,
215985 Fts5HashEntry *p2
215986){
215987 int nRet = 0;
215988 if( p->iSzPoslist ){
215989 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
215990 int nData = p->nData;
215991 if( pHash->eDetail==FTS5_DETAIL_NONE ){
215992 assert( nData==p->iSzPoslist );
215993 if( p->bDel ){
215994 pPtr[nData++] = 0x00;
215995 if( p->bContent ){
215996 pPtr[nData++] = 0x00;
215997 }
215998 }
215999 }else{
216000 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
216001 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
216002
216003 assert( p->bDel==0 || p->bDel==1 );
216004 if( nPos<=127 ){
216005 pPtr[p->iSzPoslist] = (u8)nPos;
216006 }else{
216007 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
216008 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
216009 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
216010 nData += (nByte-1);
216011 }
216012 }
216013
216014 nRet = nData - p->nData;
216015 if( p2==0 ){
216016 p->iSzPoslist = 0;
216017 p->bDel = 0;
216018 p->bContent = 0;
216019 p->nData = nData;
216020 }
216021 }
216022 return nRet;
216023}
216024
216025/*
216026** Add an entry to the in-memory hash table. The key is the concatenation
216027** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
216028**
216029** (bByte || pToken) -> (iRowid,iCol,iPos)
216030**
216031** Or, if iCol is negative, then the value is a delete marker.
216032*/
216033static int sqlite3Fts5HashWrite(
216034 Fts5Hash *pHash,
216035 i64 iRowid, /* Rowid for this entry */
216036 int iCol, /* Column token appears in (-ve -> delete) */
216037 int iPos, /* Position of token within column */
216038 char bByte, /* First byte of token */
216039 const char *pToken, int nToken /* Token to add or remove to or from index */
216040){
216041 unsigned int iHash;
216042 Fts5HashEntry *p;
216043 u8 *pPtr;
216044 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
216045 int bNew; /* If non-delete entry should be written */
216046
216047 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
216048
216049 /* Attempt to locate an existing hash entry */
216050 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
216051 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
216052 char *zKey = fts5EntryKey(p);
216053 if( zKey[0]==bByte
216054 && p->nKey==nToken
216055 && memcmp(&zKey[1], pToken, nToken)==0
216056 ){
216057 break;
216058 }
216059 }
216060
216061 /* If an existing hash entry cannot be found, create a new one. */
216062 if( p==0 ){
216063 /* Figure out how much space to allocate */
216064 char *zKey;
216065 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
216066 if( nByte<128 ) nByte = 128;
216067
216068 /* Grow the Fts5Hash.aSlot[] array if necessary. */
216069 if( (pHash->nEntry*2)>=pHash->nSlot ){
216070 int rc = fts5HashResize(pHash);
216071 if( rc!=SQLITE_OK ) return rc;
216072 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
216073 }
216074
216075 /* Allocate new Fts5HashEntry and add it to the hash table. */
216076 p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
216077 if( !p ) return SQLITE_NOMEM;
216078 memset(p, 0, sizeof(Fts5HashEntry));
216079 p->nAlloc = (int)nByte;
216080 zKey = fts5EntryKey(p);
216081 zKey[0] = bByte;
216082 memcpy(&zKey[1], pToken, nToken);
216083 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
216084 p->nKey = nToken;
216085 zKey[nToken+1] = '\0';
216086 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
216087 p->pHashNext = pHash->aSlot[iHash];
216088 pHash->aSlot[iHash] = p;
216089 pHash->nEntry++;
216090
216091 /* Add the first rowid field to the hash-entry */
216092 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
216093 p->iRowid = iRowid;
216094
216095 p->iSzPoslist = p->nData;
216096 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
216097 p->nData += 1;
216098 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
216099 }
216100
216101 nIncr += p->nData;
216102 }else{
216103
216104 /* Appending to an existing hash-entry. Check that there is enough
216105 ** space to append the largest possible new entry. Worst case scenario
216106 ** is:
216107 **
216108 ** + 9 bytes for a new rowid,
216109 ** + 4 byte reserved for the "poslist size" varint.
216110 ** + 1 byte for a "new column" byte,
216111 ** + 3 bytes for a new column number (16-bit max) as a varint,
216112 ** + 5 bytes for the new position offset (32-bit max).
216113 */
216114 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
216115 sqlite3_int64 nNew = p->nAlloc * 2;
216116 Fts5HashEntry *pNew;
216117 Fts5HashEntry **pp;
216118 pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
216119 if( pNew==0 ) return SQLITE_NOMEM;
216120 pNew->nAlloc = (int)nNew;
216121 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
216122 *pp = pNew;
216123 p = pNew;
216124 }
216125 nIncr -= p->nData;
216126 }
216127 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
216128
216129 pPtr = (u8*)p;
216130
216131 /* If this is a new rowid, append the 4-byte size field for the previous
216132 ** entry, and the new rowid for this entry. */
216133 if( iRowid!=p->iRowid ){
216134 fts5HashAddPoslistSize(pHash, p, 0);
216135 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
216136 p->iRowid = iRowid;
216137 bNew = 1;
216138 p->iSzPoslist = p->nData;
216139 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
216140 p->nData += 1;
216141 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
216142 p->iPos = 0;
216143 }
216144 }
216145
216146 if( iCol>=0 ){
216147 if( pHash->eDetail==FTS5_DETAIL_NONE ){
216148 p->bContent = 1;
216149 }else{
216150 /* Append a new column value, if necessary */
216151 assert( iCol>=p->iCol );
216152 if( iCol!=p->iCol ){
216153 if( pHash->eDetail==FTS5_DETAIL_FULL ){
216154 pPtr[p->nData++] = 0x01;
216155 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
216156 p->iCol = (i16)iCol;
216157 p->iPos = 0;
216158 }else{
216159 bNew = 1;
216160 p->iCol = (i16)(iPos = iCol);
216161 }
216162 }
216163
216164 /* Append the new position offset, if necessary */
216165 if( bNew ){
216166 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
216167 p->iPos = iPos;
216168 }
216169 }
216170 }else{
216171 /* This is a delete. Set the delete flag. */
216172 p->bDel = 1;
216173 }
216174
216175 nIncr += p->nData;
216176 *pHash->pnByte += nIncr;
216177 return SQLITE_OK;
216178}
216179
216180
216181/*
216182** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
216183** each sorted in key order. This function merges the two lists into a
216184** single list and returns a pointer to its first element.
216185*/
216186static Fts5HashEntry *fts5HashEntryMerge(
216187 Fts5HashEntry *pLeft,
216188 Fts5HashEntry *pRight
216189){
216190 Fts5HashEntry *p1 = pLeft;
216191 Fts5HashEntry *p2 = pRight;
216192 Fts5HashEntry *pRet = 0;
216193 Fts5HashEntry **ppOut = &pRet;
216194
216195 while( p1 || p2 ){
216196 if( p1==0 ){
216197 *ppOut = p2;
216198 p2 = 0;
216199 }else if( p2==0 ){
216200 *ppOut = p1;
216201 p1 = 0;
216202 }else{
216203 int i = 0;
216204 char *zKey1 = fts5EntryKey(p1);
216205 char *zKey2 = fts5EntryKey(p2);
216206 while( zKey1[i]==zKey2[i] ) i++;
216207
216208 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
216209 /* p2 is smaller */
216210 *ppOut = p2;
216211 ppOut = &p2->pScanNext;
216212 p2 = p2->pScanNext;
216213 }else{
216214 /* p1 is smaller */
216215 *ppOut = p1;
216216 ppOut = &p1->pScanNext;
216217 p1 = p1->pScanNext;
216218 }
216219 *ppOut = 0;
216220 }
216221 }
216222
216223 return pRet;
216224}
216225
216226/*
216227** Extract all tokens from hash table iHash and link them into a list
216228** in sorted order. The hash table is cleared before returning. It is
216229** the responsibility of the caller to free the elements of the returned
216230** list.
216231*/
216232static int fts5HashEntrySort(
216233 Fts5Hash *pHash,
216234 const char *pTerm, int nTerm, /* Query prefix, if any */
216235 Fts5HashEntry **ppSorted
216236){
216237 const int nMergeSlot = 32;
216238 Fts5HashEntry **ap;
216239 Fts5HashEntry *pList;
216240 int iSlot;
216241 int i;
216242
216243 *ppSorted = 0;
216244 ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
216245 if( !ap ) return SQLITE_NOMEM;
216246 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
216247
216248 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
216249 Fts5HashEntry *pIter;
216250 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
216251 if( pTerm==0
216252 || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm))
216253 ){
216254 Fts5HashEntry *pEntry = pIter;
216255 pEntry->pScanNext = 0;
216256 for(i=0; ap[i]; i++){
216257 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
216258 ap[i] = 0;
216259 }
216260 ap[i] = pEntry;
216261 }
216262 }
216263 }
216264
216265 pList = 0;
216266 for(i=0; i<nMergeSlot; i++){
216267 pList = fts5HashEntryMerge(pList, ap[i]);
216268 }
216269
216270 pHash->nEntry = 0;
216271 sqlite3_free(ap);
216272 *ppSorted = pList;
216273 return SQLITE_OK;
216274}
216275
216276/*
216277** Query the hash table for a doclist associated with term pTerm/nTerm.
216278*/
216279static int sqlite3Fts5HashQuery(
216280 Fts5Hash *pHash, /* Hash table to query */
216281 int nPre,
216282 const char *pTerm, int nTerm, /* Query term */
216283 void **ppOut, /* OUT: Pointer to new object */
216284 int *pnDoclist /* OUT: Size of doclist in bytes */
216285){
216286 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
216287 char *zKey = 0;
216288 Fts5HashEntry *p;
216289
216290 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
216291 zKey = fts5EntryKey(p);
216292 assert( p->nKey+1==(int)strlen(zKey) );
216293 if( nTerm==p->nKey+1 && memcmp(zKey, pTerm, nTerm)==0 ) break;
216294 }
216295
216296 if( p ){
216297 int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
216298 int nList = p->nData - nHashPre;
216299 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
216300 if( pRet ){
216301 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
216302 memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
216303 nList += fts5HashAddPoslistSize(pHash, p, pFaux);
216304 *pnDoclist = nList;
216305 }else{
216306 *pnDoclist = 0;
216307 return SQLITE_NOMEM;
216308 }
216309 }else{
216310 *ppOut = 0;
216311 *pnDoclist = 0;
216312 }
216313
216314 return SQLITE_OK;
216315}
216316
216317static int sqlite3Fts5HashScanInit(
216318 Fts5Hash *p, /* Hash table to query */
216319 const char *pTerm, int nTerm /* Query prefix */
216320){
216321 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
216322}
216323
216324static void sqlite3Fts5HashScanNext(Fts5Hash *p){
216325 assert( !sqlite3Fts5HashScanEof(p) );
216326 p->pScan = p->pScan->pScanNext;
216327}
216328
216329static int sqlite3Fts5HashScanEof(Fts5Hash *p){
216330 return (p->pScan==0);
216331}
216332
216333static void sqlite3Fts5HashScanEntry(
216334 Fts5Hash *pHash,
216335 const char **pzTerm, /* OUT: term (nul-terminated) */
216336 const u8 **ppDoclist, /* OUT: pointer to doclist */
216337 int *pnDoclist /* OUT: size of doclist in bytes */
216338){
216339 Fts5HashEntry *p;
216340 if( (p = pHash->pScan) ){
216341 char *zKey = fts5EntryKey(p);
216342 int nTerm = (int)strlen(zKey);
216343 fts5HashAddPoslistSize(pHash, p, 0);
216344 *pzTerm = zKey;
216345 *ppDoclist = (const u8*)&zKey[nTerm+1];
216346 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
216347 }else{
216348 *pzTerm = 0;
216349 *ppDoclist = 0;
216350 *pnDoclist = 0;
216351 }
216352}
216353
216354/*
216355** 2014 May 31
216356**
216357** The author disclaims copyright to this source code. In place of
216358** a legal notice, here is a blessing:
216359**
216360** May you do good and not evil.
216361** May you find forgiveness for yourself and forgive others.
216362** May you share freely, never taking more than you give.
216363**
216364******************************************************************************
216365**
216366** Low level access to the FTS index stored in the database file. The
216367** routines in this file file implement all read and write access to the
216368** %_data table. Other parts of the system access this functionality via
216369** the interface defined in fts5Int.h.
216370*/
216371
216372
216373/* #include "fts5Int.h" */
216374
216375/*
216376** Overview:
216377**
216378** The %_data table contains all the FTS indexes for an FTS5 virtual table.
216379** As well as the main term index, there may be up to 31 prefix indexes.
216380** The format is similar to FTS3/4, except that:
216381**
216382** * all segment b-tree leaf data is stored in fixed size page records
216383** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
216384** taken to ensure it is possible to iterate in either direction through
216385** the entries in a doclist, or to seek to a specific entry within a
216386** doclist, without loading it into memory.
216387**
216388** * large doclists that span many pages have associated "doclist index"
216389** records that contain a copy of the first rowid on each page spanned by
216390** the doclist. This is used to speed up seek operations, and merges of
216391** large doclists with very small doclists.
216392**
216393** * extra fields in the "structure record" record the state of ongoing
216394** incremental merge operations.
216395**
216396*/
216397
216398
216399#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
216400#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
216401
216402#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
216403
216404#define FTS5_MAIN_PREFIX '0'
216405
216406#if FTS5_MAX_PREFIX_INDEXES > 31
216407# error "FTS5_MAX_PREFIX_INDEXES is too large"
216408#endif
216409
216410/*
216411** Details:
216412**
216413** The %_data table managed by this module,
216414**
216415** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
216416**
216417** , contains the following 5 types of records. See the comments surrounding
216418** the FTS5_*_ROWID macros below for a description of how %_data rowids are
216419** assigned to each fo them.
216420**
216421** 1. Structure Records:
216422**
216423** The set of segments that make up an index - the index structure - are
216424** recorded in a single record within the %_data table. The record consists
216425** of a single 32-bit configuration cookie value followed by a list of
216426** SQLite varints. If the FTS table features more than one index (because
216427** there are one or more prefix indexes), it is guaranteed that all share
216428** the same cookie value.
216429**
216430** Immediately following the configuration cookie, the record begins with
216431** three varints:
216432**
216433** + number of levels,
216434** + total number of segments on all levels,
216435** + value of write counter.
216436**
216437** Then, for each level from 0 to nMax:
216438**
216439** + number of input segments in ongoing merge.
216440** + total number of segments in level.
216441** + for each segment from oldest to newest:
216442** + segment id (always > 0)
216443** + first leaf page number (often 1, always greater than 0)
216444** + final leaf page number
216445**
216446** 2. The Averages Record:
216447**
216448** A single record within the %_data table. The data is a list of varints.
216449** The first value is the number of rows in the index. Then, for each column
216450** from left to right, the total number of tokens in the column for all
216451** rows of the table.
216452**
216453** 3. Segment leaves:
216454**
216455** TERM/DOCLIST FORMAT:
216456**
216457** Most of each segment leaf is taken up by term/doclist data. The
216458** general format of term/doclist, starting with the first term
216459** on the leaf page, is:
216460**
216461** varint : size of first term
216462** blob: first term data
216463** doclist: first doclist
216464** zero-or-more {
216465** varint: number of bytes in common with previous term
216466** varint: number of bytes of new term data (nNew)
216467** blob: nNew bytes of new term data
216468** doclist: next doclist
216469** }
216470**
216471** doclist format:
216472**
216473** varint: first rowid
216474** poslist: first poslist
216475** zero-or-more {
216476** varint: rowid delta (always > 0)
216477** poslist: next poslist
216478** }
216479**
216480** poslist format:
216481**
216482** varint: size of poslist in bytes multiplied by 2, not including
216483** this field. Plus 1 if this entry carries the "delete" flag.
216484** collist: collist for column 0
216485** zero-or-more {
216486** 0x01 byte
216487** varint: column number (I)
216488** collist: collist for column I
216489** }
216490**
216491** collist format:
216492**
216493** varint: first offset + 2
216494** zero-or-more {
216495** varint: offset delta + 2
216496** }
216497**
216498** PAGE FORMAT
216499**
216500** Each leaf page begins with a 4-byte header containing 2 16-bit
216501** unsigned integer fields in big-endian format. They are:
216502**
216503** * The byte offset of the first rowid on the page, if it exists
216504** and occurs before the first term (otherwise 0).
216505**
216506** * The byte offset of the start of the page footer. If the page
216507** footer is 0 bytes in size, then this field is the same as the
216508** size of the leaf page in bytes.
216509**
216510** The page footer consists of a single varint for each term located
216511** on the page. Each varint is the byte offset of the current term
216512** within the page, delta-compressed against the previous value. In
216513** other words, the first varint in the footer is the byte offset of
216514** the first term, the second is the byte offset of the second less that
216515** of the first, and so on.
216516**
216517** The term/doclist format described above is accurate if the entire
216518** term/doclist data fits on a single leaf page. If this is not the case,
216519** the format is changed in two ways:
216520**
216521** + if the first rowid on a page occurs before the first term, it
216522** is stored as a literal value:
216523**
216524** varint: first rowid
216525**
216526** + the first term on each page is stored in the same way as the
216527** very first term of the segment:
216528**
216529** varint : size of first term
216530** blob: first term data
216531**
216532** 5. Segment doclist indexes:
216533**
216534** Doclist indexes are themselves b-trees, however they usually consist of
216535** a single leaf record only. The format of each doclist index leaf page
216536** is:
216537**
216538** * Flags byte. Bits are:
216539** 0x01: Clear if leaf is also the root page, otherwise set.
216540**
216541** * Page number of fts index leaf page. As a varint.
216542**
216543** * First rowid on page indicated by previous field. As a varint.
216544**
216545** * A list of varints, one for each subsequent termless page. A
216546** positive delta if the termless page contains at least one rowid,
216547** or an 0x00 byte otherwise.
216548**
216549** Internal doclist index nodes are:
216550**
216551** * Flags byte. Bits are:
216552** 0x01: Clear for root page, otherwise set.
216553**
216554** * Page number of first child page. As a varint.
216555**
216556** * Copy of first rowid on page indicated by previous field. As a varint.
216557**
216558** * A list of delta-encoded varints - the first rowid on each subsequent
216559** child page.
216560**
216561*/
216562
216563/*
216564** Rowids for the averages and structure records in the %_data table.
216565*/
216566#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
216567#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
216568
216569/*
216570** Macros determining the rowids used by segment leaves and dlidx leaves
216571** and nodes. All nodes and leaves are stored in the %_data table with large
216572** positive rowids.
216573**
216574** Each segment has a unique non-zero 16-bit id.
216575**
216576** The rowid for each segment leaf is found by passing the segment id and
216577** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
216578** sequentially starting from 1.
216579*/
216580#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
216581#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
216582#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
216583#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
216584
216585#define fts5_dri(segid, dlidx, height, pgno) ( \
216586 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
216587 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
216588 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
216589 ((i64)(pgno)) \
216590)
216591
216592#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
216593#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
216594
216595#ifdef SQLITE_DEBUG
216596static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
216597#endif
216598
216599
216600/*
216601** Each time a blob is read from the %_data table, it is padded with this
216602** many zero bytes. This makes it easier to decode the various record formats
216603** without overreading if the records are corrupt.
216604*/
216605#define FTS5_DATA_ZERO_PADDING 8
216606#define FTS5_DATA_PADDING 20
216607
216608typedef struct Fts5Data Fts5Data;
216609typedef struct Fts5DlidxIter Fts5DlidxIter;
216610typedef struct Fts5DlidxLvl Fts5DlidxLvl;
216611typedef struct Fts5DlidxWriter Fts5DlidxWriter;
216612typedef struct Fts5Iter Fts5Iter;
216613typedef struct Fts5PageWriter Fts5PageWriter;
216614typedef struct Fts5SegIter Fts5SegIter;
216615typedef struct Fts5DoclistIter Fts5DoclistIter;
216616typedef struct Fts5SegWriter Fts5SegWriter;
216617typedef struct Fts5Structure Fts5Structure;
216618typedef struct Fts5StructureLevel Fts5StructureLevel;
216619typedef struct Fts5StructureSegment Fts5StructureSegment;
216620
216621struct Fts5Data {
216622 u8 *p; /* Pointer to buffer containing record */
216623 int nn; /* Size of record in bytes */
216624 int szLeaf; /* Size of leaf without page-index */
216625};
216626
216627/*
216628** One object per %_data table.
216629*/
216630struct Fts5Index {
216631 Fts5Config *pConfig; /* Virtual table configuration */
216632 char *zDataTbl; /* Name of %_data table */
216633 int nWorkUnit; /* Leaf pages in a "unit" of work */
216634
216635 /*
216636 ** Variables related to the accumulation of tokens and doclists within the
216637 ** in-memory hash tables before they are flushed to disk.
216638 */
216639 Fts5Hash *pHash; /* Hash table for in-memory data */
216640 int nPendingData; /* Current bytes of pending data */
216641 i64 iWriteRowid; /* Rowid for current doc being written */
216642 int bDelete; /* Current write is a delete */
216643
216644 /* Error state. */
216645 int rc; /* Current error code */
216646
216647 /* State used by the fts5DataXXX() functions. */
216648 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
216649 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
216650 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
216651 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
216652 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */
216653 sqlite3_stmt *pIdxSelect;
216654 int nRead; /* Total number of blocks read */
216655
216656 sqlite3_stmt *pDataVersion;
216657 i64 iStructVersion; /* data_version when pStruct read */
216658 Fts5Structure *pStruct; /* Current db structure (or NULL) */
216659};
216660
216661struct Fts5DoclistIter {
216662 u8 *aEof; /* Pointer to 1 byte past end of doclist */
216663
216664 /* Output variables. aPoslist==0 at EOF */
216665 i64 iRowid;
216666 u8 *aPoslist;
216667 int nPoslist;
216668 int nSize;
216669};
216670
216671/*
216672** The contents of the "structure" record for each index are represented
216673** using an Fts5Structure record in memory. Which uses instances of the
216674** other Fts5StructureXXX types as components.
216675*/
216676struct Fts5StructureSegment {
216677 int iSegid; /* Segment id */
216678 int pgnoFirst; /* First leaf page number in segment */
216679 int pgnoLast; /* Last leaf page number in segment */
216680};
216681struct Fts5StructureLevel {
216682 int nMerge; /* Number of segments in incr-merge */
216683 int nSeg; /* Total number of segments on level */
216684 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
216685};
216686struct Fts5Structure {
216687 int nRef; /* Object reference count */
216688 u64 nWriteCounter; /* Total leaves written to level 0 */
216689 int nSegment; /* Total segments in this structure */
216690 int nLevel; /* Number of levels in this index */
216691 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
216692};
216693
216694/*
216695** An object of type Fts5SegWriter is used to write to segments.
216696*/
216697struct Fts5PageWriter {
216698 int pgno; /* Page number for this page */
216699 int iPrevPgidx; /* Previous value written into pgidx */
216700 Fts5Buffer buf; /* Buffer containing leaf data */
216701 Fts5Buffer pgidx; /* Buffer containing page-index */
216702 Fts5Buffer term; /* Buffer containing previous term on page */
216703};
216704struct Fts5DlidxWriter {
216705 int pgno; /* Page number for this page */
216706 int bPrevValid; /* True if iPrev is valid */
216707 i64 iPrev; /* Previous rowid value written to page */
216708 Fts5Buffer buf; /* Buffer containing page data */
216709};
216710struct Fts5SegWriter {
216711 int iSegid; /* Segid to write to */
216712 Fts5PageWriter writer; /* PageWriter object */
216713 i64 iPrevRowid; /* Previous rowid written to current leaf */
216714 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
216715 u8 bFirstRowidInPage; /* True if next rowid is first in page */
216716 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
216717 u8 bFirstTermInPage; /* True if next term will be first in leaf */
216718 int nLeafWritten; /* Number of leaf pages written */
216719 int nEmpty; /* Number of contiguous term-less nodes */
216720
216721 int nDlidx; /* Allocated size of aDlidx[] array */
216722 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
216723
216724 /* Values to insert into the %_idx table */
216725 Fts5Buffer btterm; /* Next term to insert into %_idx table */
216726 int iBtPage; /* Page number corresponding to btterm */
216727};
216728
216729typedef struct Fts5CResult Fts5CResult;
216730struct Fts5CResult {
216731 u16 iFirst; /* aSeg[] index of firstest iterator */
216732 u8 bTermEq; /* True if the terms are equal */
216733};
216734
216735/*
216736** Object for iterating through a single segment, visiting each term/rowid
216737** pair in the segment.
216738**
216739** pSeg:
216740** The segment to iterate through.
216741**
216742** iLeafPgno:
216743** Current leaf page number within segment.
216744**
216745** iLeafOffset:
216746** Byte offset within the current leaf that is the first byte of the
216747** position list data (one byte passed the position-list size field).
216748** rowid field of the current entry. Usually this is the size field of the
216749** position list data. The exception is if the rowid for the current entry
216750** is the last thing on the leaf page.
216751**
216752** pLeaf:
216753** Buffer containing current leaf page data. Set to NULL at EOF.
216754**
216755** iTermLeafPgno, iTermLeafOffset:
216756** Leaf page number containing the last term read from the segment. And
216757** the offset immediately following the term data.
216758**
216759** flags:
216760** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
216761**
216762** FTS5_SEGITER_ONETERM:
216763** If set, set the iterator to point to EOF after the current doclist
216764** has been exhausted. Do not proceed to the next term in the segment.
216765**
216766** FTS5_SEGITER_REVERSE:
216767** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
216768** it is set, iterate through rowid in descending order instead of the
216769** default ascending order.
216770**
216771** iRowidOffset/nRowidOffset/aRowidOffset:
216772** These are used if the FTS5_SEGITER_REVERSE flag is set.
216773**
216774** For each rowid on the page corresponding to the current term, the
216775** corresponding aRowidOffset[] entry is set to the byte offset of the
216776** start of the "position-list-size" field within the page.
216777**
216778** iTermIdx:
216779** Index of current term on iTermLeafPgno.
216780*/
216781struct Fts5SegIter {
216782 Fts5StructureSegment *pSeg; /* Segment to iterate through */
216783 int flags; /* Mask of configuration flags */
216784 int iLeafPgno; /* Current leaf page number */
216785 Fts5Data *pLeaf; /* Current leaf data */
216786 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
216787 int iLeafOffset; /* Byte offset within current leaf */
216788
216789 /* Next method */
216790 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
216791
216792 /* The page and offset from which the current term was read. The offset
216793 ** is the offset of the first rowid in the current doclist. */
216794 int iTermLeafPgno;
216795 int iTermLeafOffset;
216796
216797 int iPgidxOff; /* Next offset in pgidx */
216798 int iEndofDoclist;
216799
216800 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
216801 int iRowidOffset; /* Current entry in aRowidOffset[] */
216802 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
216803 int *aRowidOffset; /* Array of offset to rowid fields */
216804
216805 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
216806
216807 /* Variables populated based on current entry. */
216808 Fts5Buffer term; /* Current term */
216809 i64 iRowid; /* Current rowid */
216810 int nPos; /* Number of bytes in current position list */
216811 u8 bDel; /* True if the delete flag is set */
216812};
216813
216814/*
216815** Argument is a pointer to an Fts5Data structure that contains a
216816** leaf page.
216817*/
216818#define ASSERT_SZLEAF_OK(x) assert( \
216819 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
216820)
216821
216822#define FTS5_SEGITER_ONETERM 0x01
216823#define FTS5_SEGITER_REVERSE 0x02
216824
216825/*
216826** Argument is a pointer to an Fts5Data structure that contains a leaf
216827** page. This macro evaluates to true if the leaf contains no terms, or
216828** false if it contains at least one term.
216829*/
216830#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
216831
216832#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
216833
216834#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
216835
216836/*
216837** Object for iterating through the merged results of one or more segments,
216838** visiting each term/rowid pair in the merged data.
216839**
216840** nSeg is always a power of two greater than or equal to the number of
216841** segments that this object is merging data from. Both the aSeg[] and
216842** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
216843** with zeroed objects - these are handled as if they were iterators opened
216844** on empty segments.
216845**
216846** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
216847** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
216848** comparison in this context is the index of the iterator that currently
216849** points to the smaller term/rowid combination. Iterators at EOF are
216850** considered to be greater than all other iterators.
216851**
216852** aFirst[1] contains the index in aSeg[] of the iterator that points to
216853** the smallest key overall. aFirst[0] is unused.
216854**
216855** poslist:
216856** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
216857** There is no way to tell if this is populated or not.
216858*/
216859struct Fts5Iter {
216860 Fts5IndexIter base; /* Base class containing output vars */
216861
216862 Fts5Index *pIndex; /* Index that owns this iterator */
216863 Fts5Buffer poslist; /* Buffer containing current poslist */
216864 Fts5Colset *pColset; /* Restrict matches to these columns */
216865
216866 /* Invoked to set output variables. */
216867 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
216868
216869 int nSeg; /* Size of aSeg[] array */
216870 int bRev; /* True to iterate in reverse order */
216871 u8 bSkipEmpty; /* True to skip deleted entries */
216872
216873 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
216874 Fts5CResult *aFirst; /* Current merge state (see above) */
216875 Fts5SegIter aSeg[1]; /* Array of segment iterators */
216876};
216877
216878
216879/*
216880** An instance of the following type is used to iterate through the contents
216881** of a doclist-index record.
216882**
216883** pData:
216884** Record containing the doclist-index data.
216885**
216886** bEof:
216887** Set to true once iterator has reached EOF.
216888**
216889** iOff:
216890** Set to the current offset within record pData.
216891*/
216892struct Fts5DlidxLvl {
216893 Fts5Data *pData; /* Data for current page of this level */
216894 int iOff; /* Current offset into pData */
216895 int bEof; /* At EOF already */
216896 int iFirstOff; /* Used by reverse iterators */
216897
216898 /* Output variables */
216899 int iLeafPgno; /* Page number of current leaf page */
216900 i64 iRowid; /* First rowid on leaf iLeafPgno */
216901};
216902struct Fts5DlidxIter {
216903 int nLvl;
216904 int iSegid;
216905 Fts5DlidxLvl aLvl[1];
216906};
216907
216908static void fts5PutU16(u8 *aOut, u16 iVal){
216909 aOut[0] = (iVal>>8);
216910 aOut[1] = (iVal&0xFF);
216911}
216912
216913static u16 fts5GetU16(const u8 *aIn){
216914 return ((u16)aIn[0] << 8) + aIn[1];
216915}
216916
216917/*
216918** Allocate and return a buffer at least nByte bytes in size.
216919**
216920** If an OOM error is encountered, return NULL and set the error code in
216921** the Fts5Index handle passed as the first argument.
216922*/
216923static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
216924 return sqlite3Fts5MallocZero(&p->rc, nByte);
216925}
216926
216927/*
216928** Compare the contents of the pLeft buffer with the pRight/nRight blob.
216929**
216930** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
216931** +ve if pRight is smaller than pLeft. In other words:
216932**
216933** res = *pLeft - *pRight
216934*/
216935#ifdef SQLITE_DEBUG
216936static int fts5BufferCompareBlob(
216937 Fts5Buffer *pLeft, /* Left hand side of comparison */
216938 const u8 *pRight, int nRight /* Right hand side of comparison */
216939){
216940 int nCmp = MIN(pLeft->n, nRight);
216941 int res = memcmp(pLeft->p, pRight, nCmp);
216942 return (res==0 ? (pLeft->n - nRight) : res);
216943}
216944#endif
216945
216946/*
216947** Compare the contents of the two buffers using memcmp(). If one buffer
216948** is a prefix of the other, it is considered the lesser.
216949**
216950** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
216951** +ve if pRight is smaller than pLeft. In other words:
216952**
216953** res = *pLeft - *pRight
216954*/
216955static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
216956 int nCmp = MIN(pLeft->n, pRight->n);
216957 int res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
216958 return (res==0 ? (pLeft->n - pRight->n) : res);
216959}
216960
216961static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
216962 int ret;
216963 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
216964 return ret;
216965}
216966
216967/*
216968** Close the read-only blob handle, if it is open.
216969*/
216970static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
216971 if( p->pReader ){
216972 sqlite3_blob *pReader = p->pReader;
216973 p->pReader = 0;
216974 sqlite3_blob_close(pReader);
216975 }
216976}
216977
216978/*
216979** Retrieve a record from the %_data table.
216980**
216981** If an error occurs, NULL is returned and an error left in the
216982** Fts5Index object.
216983*/
216984static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
216985 Fts5Data *pRet = 0;
216986 if( p->rc==SQLITE_OK ){
216987 int rc = SQLITE_OK;
216988
216989 if( p->pReader ){
216990 /* This call may return SQLITE_ABORT if there has been a savepoint
216991 ** rollback since it was last used. In this case a new blob handle
216992 ** is required. */
216993 sqlite3_blob *pBlob = p->pReader;
216994 p->pReader = 0;
216995 rc = sqlite3_blob_reopen(pBlob, iRowid);
216996 assert( p->pReader==0 );
216997 p->pReader = pBlob;
216998 if( rc!=SQLITE_OK ){
216999 sqlite3Fts5IndexCloseReader(p);
217000 }
217001 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
217002 }
217003
217004 /* If the blob handle is not open at this point, open it and seek
217005 ** to the requested entry. */
217006 if( p->pReader==0 && rc==SQLITE_OK ){
217007 Fts5Config *pConfig = p->pConfig;
217008 rc = sqlite3_blob_open(pConfig->db,
217009 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
217010 );
217011 }
217012
217013 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
217014 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
217015 ** All the reasons those functions might return SQLITE_ERROR - missing
217016 ** table, missing row, non-blob/text in block column - indicate
217017 ** backing store corruption. */
217018 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
217019
217020 if( rc==SQLITE_OK ){
217021 u8 *aOut = 0; /* Read blob data into this buffer */
217022 int nByte = sqlite3_blob_bytes(p->pReader);
217023 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
217024 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
217025 if( pRet ){
217026 pRet->nn = nByte;
217027 aOut = pRet->p = (u8*)&pRet[1];
217028 }else{
217029 rc = SQLITE_NOMEM;
217030 }
217031
217032 if( rc==SQLITE_OK ){
217033 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
217034 }
217035 if( rc!=SQLITE_OK ){
217036 sqlite3_free(pRet);
217037 pRet = 0;
217038 }else{
217039 /* TODO1: Fix this */
217040 pRet->p[nByte] = 0x00;
217041 pRet->p[nByte+1] = 0x00;
217042 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
217043 }
217044 }
217045 p->rc = rc;
217046 p->nRead++;
217047 }
217048
217049 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
217050 return pRet;
217051}
217052
217053/*
217054** Release a reference to data record returned by an earlier call to
217055** fts5DataRead().
217056*/
217057static void fts5DataRelease(Fts5Data *pData){
217058 sqlite3_free(pData);
217059}
217060
217061static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
217062 Fts5Data *pRet = fts5DataRead(p, iRowid);
217063 if( pRet ){
217064 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
217065 p->rc = FTS5_CORRUPT;
217066 fts5DataRelease(pRet);
217067 pRet = 0;
217068 }
217069 }
217070 return pRet;
217071}
217072
217073static int fts5IndexPrepareStmt(
217074 Fts5Index *p,
217075 sqlite3_stmt **ppStmt,
217076 char *zSql
217077){
217078 if( p->rc==SQLITE_OK ){
217079 if( zSql ){
217080 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
217081 SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
217082 ppStmt, 0);
217083 }else{
217084 p->rc = SQLITE_NOMEM;
217085 }
217086 }
217087 sqlite3_free(zSql);
217088 return p->rc;
217089}
217090
217091
217092/*
217093** INSERT OR REPLACE a record into the %_data table.
217094*/
217095static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
217096 if( p->rc!=SQLITE_OK ) return;
217097
217098 if( p->pWriter==0 ){
217099 Fts5Config *pConfig = p->pConfig;
217100 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
217101 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
217102 pConfig->zDb, pConfig->zName
217103 ));
217104 if( p->rc ) return;
217105 }
217106
217107 sqlite3_bind_int64(p->pWriter, 1, iRowid);
217108 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
217109 sqlite3_step(p->pWriter);
217110 p->rc = sqlite3_reset(p->pWriter);
217111 sqlite3_bind_null(p->pWriter, 2);
217112}
217113
217114/*
217115** Execute the following SQL:
217116**
217117** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
217118*/
217119static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
217120 if( p->rc!=SQLITE_OK ) return;
217121
217122 if( p->pDeleter==0 ){
217123 Fts5Config *pConfig = p->pConfig;
217124 char *zSql = sqlite3_mprintf(
217125 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
217126 pConfig->zDb, pConfig->zName
217127 );
217128 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
217129 }
217130
217131 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
217132 sqlite3_bind_int64(p->pDeleter, 2, iLast);
217133 sqlite3_step(p->pDeleter);
217134 p->rc = sqlite3_reset(p->pDeleter);
217135}
217136
217137/*
217138** Remove all records associated with segment iSegid.
217139*/
217140static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
217141 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
217142 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
217143 fts5DataDelete(p, iFirst, iLast);
217144 if( p->pIdxDeleter==0 ){
217145 Fts5Config *pConfig = p->pConfig;
217146 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
217147 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
217148 pConfig->zDb, pConfig->zName
217149 ));
217150 }
217151 if( p->rc==SQLITE_OK ){
217152 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
217153 sqlite3_step(p->pIdxDeleter);
217154 p->rc = sqlite3_reset(p->pIdxDeleter);
217155 }
217156}
217157
217158/*
217159** Release a reference to an Fts5Structure object returned by an earlier
217160** call to fts5StructureRead() or fts5StructureDecode().
217161*/
217162static void fts5StructureRelease(Fts5Structure *pStruct){
217163 if( pStruct && 0>=(--pStruct->nRef) ){
217164 int i;
217165 assert( pStruct->nRef==0 );
217166 for(i=0; i<pStruct->nLevel; i++){
217167 sqlite3_free(pStruct->aLevel[i].aSeg);
217168 }
217169 sqlite3_free(pStruct);
217170 }
217171}
217172
217173static void fts5StructureRef(Fts5Structure *pStruct){
217174 pStruct->nRef++;
217175}
217176
217177/*
217178** Deserialize and return the structure record currently stored in serialized
217179** form within buffer pData/nData.
217180**
217181** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
217182** are over-allocated by one slot. This allows the structure contents
217183** to be more easily edited.
217184**
217185** If an error occurs, *ppOut is set to NULL and an SQLite error code
217186** returned. Otherwise, *ppOut is set to point to the new object and
217187** SQLITE_OK returned.
217188*/
217189static int fts5StructureDecode(
217190 const u8 *pData, /* Buffer containing serialized structure */
217191 int nData, /* Size of buffer pData in bytes */
217192 int *piCookie, /* Configuration cookie value */
217193 Fts5Structure **ppOut /* OUT: Deserialized object */
217194){
217195 int rc = SQLITE_OK;
217196 int i = 0;
217197 int iLvl;
217198 int nLevel = 0;
217199 int nSegment = 0;
217200 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
217201 Fts5Structure *pRet = 0; /* Structure object to return */
217202
217203 /* Grab the cookie value */
217204 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
217205 i = 4;
217206
217207 /* Read the total number of levels and segments from the start of the
217208 ** structure record. */
217209 i += fts5GetVarint32(&pData[i], nLevel);
217210 i += fts5GetVarint32(&pData[i], nSegment);
217211 if( nLevel>FTS5_MAX_SEGMENT || nLevel<0
217212 || nSegment>FTS5_MAX_SEGMENT || nSegment<0
217213 ){
217214 return FTS5_CORRUPT;
217215 }
217216 nByte = (
217217 sizeof(Fts5Structure) + /* Main structure */
217218 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
217219 );
217220 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
217221
217222 if( pRet ){
217223 pRet->nRef = 1;
217224 pRet->nLevel = nLevel;
217225 pRet->nSegment = nSegment;
217226 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
217227
217228 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
217229 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
217230 int nTotal = 0;
217231 int iSeg;
217232
217233 if( i>=nData ){
217234 rc = FTS5_CORRUPT;
217235 }else{
217236 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
217237 i += fts5GetVarint32(&pData[i], nTotal);
217238 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
217239 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
217240 nTotal * sizeof(Fts5StructureSegment)
217241 );
217242 nSegment -= nTotal;
217243 }
217244
217245 if( rc==SQLITE_OK ){
217246 pLvl->nSeg = nTotal;
217247 for(iSeg=0; iSeg<nTotal; iSeg++){
217248 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
217249 if( i>=nData ){
217250 rc = FTS5_CORRUPT;
217251 break;
217252 }
217253 i += fts5GetVarint32(&pData[i], pSeg->iSegid);
217254 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
217255 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
217256 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
217257 rc = FTS5_CORRUPT;
217258 break;
217259 }
217260 }
217261 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
217262 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
217263 }
217264 }
217265 if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
217266
217267 if( rc!=SQLITE_OK ){
217268 fts5StructureRelease(pRet);
217269 pRet = 0;
217270 }
217271 }
217272
217273 *ppOut = pRet;
217274 return rc;
217275}
217276
217277/*
217278**
217279*/
217280static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
217281 if( *pRc==SQLITE_OK ){
217282 Fts5Structure *pStruct = *ppStruct;
217283 int nLevel = pStruct->nLevel;
217284 sqlite3_int64 nByte = (
217285 sizeof(Fts5Structure) + /* Main structure */
217286 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
217287 );
217288
217289 pStruct = sqlite3_realloc64(pStruct, nByte);
217290 if( pStruct ){
217291 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
217292 pStruct->nLevel++;
217293 *ppStruct = pStruct;
217294 }else{
217295 *pRc = SQLITE_NOMEM;
217296 }
217297 }
217298}
217299
217300/*
217301** Extend level iLvl so that there is room for at least nExtra more
217302** segments.
217303*/
217304static void fts5StructureExtendLevel(
217305 int *pRc,
217306 Fts5Structure *pStruct,
217307 int iLvl,
217308 int nExtra,
217309 int bInsert
217310){
217311 if( *pRc==SQLITE_OK ){
217312 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
217313 Fts5StructureSegment *aNew;
217314 sqlite3_int64 nByte;
217315
217316 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
217317 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
217318 if( aNew ){
217319 if( bInsert==0 ){
217320 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
217321 }else{
217322 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
217323 memmove(&aNew[nExtra], aNew, nMove);
217324 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
217325 }
217326 pLvl->aSeg = aNew;
217327 }else{
217328 *pRc = SQLITE_NOMEM;
217329 }
217330 }
217331}
217332
217333static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
217334 Fts5Structure *pRet = 0;
217335 Fts5Config *pConfig = p->pConfig;
217336 int iCookie; /* Configuration cookie */
217337 Fts5Data *pData;
217338
217339 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
217340 if( p->rc==SQLITE_OK ){
217341 /* TODO: Do we need this if the leaf-index is appended? Probably... */
217342 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
217343 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
217344 if( p->rc==SQLITE_OK && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
217345 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
217346 }
217347 fts5DataRelease(pData);
217348 if( p->rc!=SQLITE_OK ){
217349 fts5StructureRelease(pRet);
217350 pRet = 0;
217351 }
217352 }
217353
217354 return pRet;
217355}
217356
217357static i64 fts5IndexDataVersion(Fts5Index *p){
217358 i64 iVersion = 0;
217359
217360 if( p->rc==SQLITE_OK ){
217361 if( p->pDataVersion==0 ){
217362 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
217363 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
217364 );
217365 if( p->rc ) return 0;
217366 }
217367
217368 if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
217369 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
217370 }
217371 p->rc = sqlite3_reset(p->pDataVersion);
217372 }
217373
217374 return iVersion;
217375}
217376
217377/*
217378** Read, deserialize and return the structure record.
217379**
217380** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
217381** are over-allocated as described for function fts5StructureDecode()
217382** above.
217383**
217384** If an error occurs, NULL is returned and an error code left in the
217385** Fts5Index handle. If an error has already occurred when this function
217386** is called, it is a no-op.
217387*/
217388static Fts5Structure *fts5StructureRead(Fts5Index *p){
217389
217390 if( p->pStruct==0 ){
217391 p->iStructVersion = fts5IndexDataVersion(p);
217392 if( p->rc==SQLITE_OK ){
217393 p->pStruct = fts5StructureReadUncached(p);
217394 }
217395 }
217396
217397#if 0
217398 else{
217399 Fts5Structure *pTest = fts5StructureReadUncached(p);
217400 if( pTest ){
217401 int i, j;
217402 assert_nc( p->pStruct->nSegment==pTest->nSegment );
217403 assert_nc( p->pStruct->nLevel==pTest->nLevel );
217404 for(i=0; i<pTest->nLevel; i++){
217405 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
217406 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
217407 for(j=0; j<pTest->aLevel[i].nSeg; j++){
217408 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
217409 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
217410 assert_nc( p1->iSegid==p2->iSegid );
217411 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
217412 assert_nc( p1->pgnoLast==p2->pgnoLast );
217413 }
217414 }
217415 fts5StructureRelease(pTest);
217416 }
217417 }
217418#endif
217419
217420 if( p->rc!=SQLITE_OK ) return 0;
217421 assert( p->iStructVersion!=0 );
217422 assert( p->pStruct!=0 );
217423 fts5StructureRef(p->pStruct);
217424 return p->pStruct;
217425}
217426
217427static void fts5StructureInvalidate(Fts5Index *p){
217428 if( p->pStruct ){
217429 fts5StructureRelease(p->pStruct);
217430 p->pStruct = 0;
217431 }
217432}
217433
217434/*
217435** Return the total number of segments in index structure pStruct. This
217436** function is only ever used as part of assert() conditions.
217437*/
217438#ifdef SQLITE_DEBUG
217439static int fts5StructureCountSegments(Fts5Structure *pStruct){
217440 int nSegment = 0; /* Total number of segments */
217441 if( pStruct ){
217442 int iLvl; /* Used to iterate through levels */
217443 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
217444 nSegment += pStruct->aLevel[iLvl].nSeg;
217445 }
217446 }
217447
217448 return nSegment;
217449}
217450#endif
217451
217452#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
217453 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
217454 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
217455 (pBuf)->n += nBlob; \
217456}
217457
217458#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
217459 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
217460 assert( (pBuf)->nSpace>=(pBuf)->n ); \
217461}
217462
217463
217464/*
217465** Serialize and store the "structure" record.
217466**
217467** If an error occurs, leave an error code in the Fts5Index object. If an
217468** error has already occurred, this function is a no-op.
217469*/
217470static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
217471 if( p->rc==SQLITE_OK ){
217472 Fts5Buffer buf; /* Buffer to serialize record into */
217473 int iLvl; /* Used to iterate through levels */
217474 int iCookie; /* Cookie value to store */
217475
217476 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
217477 memset(&buf, 0, sizeof(Fts5Buffer));
217478
217479 /* Append the current configuration cookie */
217480 iCookie = p->pConfig->iCookie;
217481 if( iCookie<0 ) iCookie = 0;
217482
217483 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
217484 sqlite3Fts5Put32(buf.p, iCookie);
217485 buf.n = 4;
217486 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
217487 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
217488 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
217489 }
217490
217491 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
217492 int iSeg; /* Used to iterate through segments */
217493 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
217494 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
217495 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
217496 assert( pLvl->nMerge<=pLvl->nSeg );
217497
217498 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
217499 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
217500 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
217501 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
217502 }
217503 }
217504
217505 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
217506 fts5BufferFree(&buf);
217507 }
217508}
217509
217510#if 0
217511static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
217512static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
217513 int rc = SQLITE_OK;
217514 Fts5Buffer buf;
217515 memset(&buf, 0, sizeof(buf));
217516 fts5DebugStructure(&rc, &buf, pStruct);
217517 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
217518 fflush(stdout);
217519 fts5BufferFree(&buf);
217520}
217521#else
217522# define fts5PrintStructure(x,y)
217523#endif
217524
217525static int fts5SegmentSize(Fts5StructureSegment *pSeg){
217526 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
217527}
217528
217529/*
217530** Return a copy of index structure pStruct. Except, promote as many
217531** segments as possible to level iPromote. If an OOM occurs, NULL is
217532** returned.
217533*/
217534static void fts5StructurePromoteTo(
217535 Fts5Index *p,
217536 int iPromote,
217537 int szPromote,
217538 Fts5Structure *pStruct
217539){
217540 int il, is;
217541 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
217542
217543 if( pOut->nMerge==0 ){
217544 for(il=iPromote+1; il<pStruct->nLevel; il++){
217545 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
217546 if( pLvl->nMerge ) return;
217547 for(is=pLvl->nSeg-1; is>=0; is--){
217548 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
217549 if( sz>szPromote ) return;
217550 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
217551 if( p->rc ) return;
217552 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
217553 pOut->nSeg++;
217554 pLvl->nSeg--;
217555 }
217556 }
217557 }
217558}
217559
217560/*
217561** A new segment has just been written to level iLvl of index structure
217562** pStruct. This function determines if any segments should be promoted
217563** as a result. Segments are promoted in two scenarios:
217564**
217565** a) If the segment just written is smaller than one or more segments
217566** within the previous populated level, it is promoted to the previous
217567** populated level.
217568**
217569** b) If the segment just written is larger than the newest segment on
217570** the next populated level, then that segment, and any other adjacent
217571** segments that are also smaller than the one just written, are
217572** promoted.
217573**
217574** If one or more segments are promoted, the structure object is updated
217575** to reflect this.
217576*/
217577static void fts5StructurePromote(
217578 Fts5Index *p, /* FTS5 backend object */
217579 int iLvl, /* Index level just updated */
217580 Fts5Structure *pStruct /* Index structure */
217581){
217582 if( p->rc==SQLITE_OK ){
217583 int iTst;
217584 int iPromote = -1;
217585 int szPromote = 0; /* Promote anything this size or smaller */
217586 Fts5StructureSegment *pSeg; /* Segment just written */
217587 int szSeg; /* Size of segment just written */
217588 int nSeg = pStruct->aLevel[iLvl].nSeg;
217589
217590 if( nSeg==0 ) return;
217591 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
217592 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
217593
217594 /* Check for condition (a) */
217595 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
217596 if( iTst>=0 ){
217597 int i;
217598 int szMax = 0;
217599 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
217600 assert( pTst->nMerge==0 );
217601 for(i=0; i<pTst->nSeg; i++){
217602 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
217603 if( sz>szMax ) szMax = sz;
217604 }
217605 if( szMax>=szSeg ){
217606 /* Condition (a) is true. Promote the newest segment on level
217607 ** iLvl to level iTst. */
217608 iPromote = iTst;
217609 szPromote = szMax;
217610 }
217611 }
217612
217613 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
217614 ** is a no-op if it is not. */
217615 if( iPromote<0 ){
217616 iPromote = iLvl;
217617 szPromote = szSeg;
217618 }
217619 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
217620 }
217621}
217622
217623
217624/*
217625** Advance the iterator passed as the only argument. If the end of the
217626** doclist-index page is reached, return non-zero.
217627*/
217628static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
217629 Fts5Data *pData = pLvl->pData;
217630
217631 if( pLvl->iOff==0 ){
217632 assert( pLvl->bEof==0 );
217633 pLvl->iOff = 1;
217634 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
217635 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
217636 pLvl->iFirstOff = pLvl->iOff;
217637 }else{
217638 int iOff;
217639 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
217640 if( pData->p[iOff] ) break;
217641 }
217642
217643 if( iOff<pData->nn ){
217644 i64 iVal;
217645 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
217646 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
217647 pLvl->iRowid += iVal;
217648 pLvl->iOff = iOff;
217649 }else{
217650 pLvl->bEof = 1;
217651 }
217652 }
217653
217654 return pLvl->bEof;
217655}
217656
217657/*
217658** Advance the iterator passed as the only argument.
217659*/
217660static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
217661 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
217662
217663 assert( iLvl<pIter->nLvl );
217664 if( fts5DlidxLvlNext(pLvl) ){
217665 if( (iLvl+1) < pIter->nLvl ){
217666 fts5DlidxIterNextR(p, pIter, iLvl+1);
217667 if( pLvl[1].bEof==0 ){
217668 fts5DataRelease(pLvl->pData);
217669 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
217670 pLvl->pData = fts5DataRead(p,
217671 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
217672 );
217673 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
217674 }
217675 }
217676 }
217677
217678 return pIter->aLvl[0].bEof;
217679}
217680static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
217681 return fts5DlidxIterNextR(p, pIter, 0);
217682}
217683
217684/*
217685** The iterator passed as the first argument has the following fields set
217686** as follows. This function sets up the rest of the iterator so that it
217687** points to the first rowid in the doclist-index.
217688**
217689** pData:
217690** pointer to doclist-index record,
217691**
217692** When this function is called pIter->iLeafPgno is the page number the
217693** doclist is associated with (the one featuring the term).
217694*/
217695static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
217696 int i;
217697 for(i=0; i<pIter->nLvl; i++){
217698 fts5DlidxLvlNext(&pIter->aLvl[i]);
217699 }
217700 return pIter->aLvl[0].bEof;
217701}
217702
217703
217704static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
217705 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
217706}
217707
217708static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
217709 int i;
217710
217711 /* Advance each level to the last entry on the last page */
217712 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
217713 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
217714 while( fts5DlidxLvlNext(pLvl)==0 );
217715 pLvl->bEof = 0;
217716
217717 if( i>0 ){
217718 Fts5DlidxLvl *pChild = &pLvl[-1];
217719 fts5DataRelease(pChild->pData);
217720 memset(pChild, 0, sizeof(Fts5DlidxLvl));
217721 pChild->pData = fts5DataRead(p,
217722 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
217723 );
217724 }
217725 }
217726}
217727
217728/*
217729** Move the iterator passed as the only argument to the previous entry.
217730*/
217731static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
217732 int iOff = pLvl->iOff;
217733
217734 assert( pLvl->bEof==0 );
217735 if( iOff<=pLvl->iFirstOff ){
217736 pLvl->bEof = 1;
217737 }else{
217738 u8 *a = pLvl->pData->p;
217739 i64 iVal;
217740 int iLimit;
217741 int ii;
217742 int nZero = 0;
217743
217744 /* Currently iOff points to the first byte of a varint. This block
217745 ** decrements iOff until it points to the first byte of the previous
217746 ** varint. Taking care not to read any memory locations that occur
217747 ** before the buffer in memory. */
217748 iLimit = (iOff>9 ? iOff-9 : 0);
217749 for(iOff--; iOff>iLimit; iOff--){
217750 if( (a[iOff-1] & 0x80)==0 ) break;
217751 }
217752
217753 fts5GetVarint(&a[iOff], (u64*)&iVal);
217754 pLvl->iRowid -= iVal;
217755 pLvl->iLeafPgno--;
217756
217757 /* Skip backwards past any 0x00 varints. */
217758 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
217759 nZero++;
217760 }
217761 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
217762 /* The byte immediately before the last 0x00 byte has the 0x80 bit
217763 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
217764 ** bytes before a[ii]. */
217765 int bZero = 0; /* True if last 0x00 counts */
217766 if( (ii-8)>=pLvl->iFirstOff ){
217767 int j;
217768 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
217769 bZero = (j>8);
217770 }
217771 if( bZero==0 ) nZero--;
217772 }
217773 pLvl->iLeafPgno -= nZero;
217774 pLvl->iOff = iOff - nZero;
217775 }
217776
217777 return pLvl->bEof;
217778}
217779
217780static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
217781 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
217782
217783 assert( iLvl<pIter->nLvl );
217784 if( fts5DlidxLvlPrev(pLvl) ){
217785 if( (iLvl+1) < pIter->nLvl ){
217786 fts5DlidxIterPrevR(p, pIter, iLvl+1);
217787 if( pLvl[1].bEof==0 ){
217788 fts5DataRelease(pLvl->pData);
217789 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
217790 pLvl->pData = fts5DataRead(p,
217791 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
217792 );
217793 if( pLvl->pData ){
217794 while( fts5DlidxLvlNext(pLvl)==0 );
217795 pLvl->bEof = 0;
217796 }
217797 }
217798 }
217799 }
217800
217801 return pIter->aLvl[0].bEof;
217802}
217803static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
217804 return fts5DlidxIterPrevR(p, pIter, 0);
217805}
217806
217807/*
217808** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
217809*/
217810static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
217811 if( pIter ){
217812 int i;
217813 for(i=0; i<pIter->nLvl; i++){
217814 fts5DataRelease(pIter->aLvl[i].pData);
217815 }
217816 sqlite3_free(pIter);
217817 }
217818}
217819
217820static Fts5DlidxIter *fts5DlidxIterInit(
217821 Fts5Index *p, /* Fts5 Backend to iterate within */
217822 int bRev, /* True for ORDER BY ASC */
217823 int iSegid, /* Segment id */
217824 int iLeafPg /* Leaf page number to load dlidx for */
217825){
217826 Fts5DlidxIter *pIter = 0;
217827 int i;
217828 int bDone = 0;
217829
217830 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
217831 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
217832 Fts5DlidxIter *pNew;
217833
217834 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
217835 if( pNew==0 ){
217836 p->rc = SQLITE_NOMEM;
217837 }else{
217838 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
217839 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
217840 pIter = pNew;
217841 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
217842 pLvl->pData = fts5DataRead(p, iRowid);
217843 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
217844 bDone = 1;
217845 }
217846 pIter->nLvl = i+1;
217847 }
217848 }
217849
217850 if( p->rc==SQLITE_OK ){
217851 pIter->iSegid = iSegid;
217852 if( bRev==0 ){
217853 fts5DlidxIterFirst(pIter);
217854 }else{
217855 fts5DlidxIterLast(p, pIter);
217856 }
217857 }
217858
217859 if( p->rc!=SQLITE_OK ){
217860 fts5DlidxIterFree(pIter);
217861 pIter = 0;
217862 }
217863
217864 return pIter;
217865}
217866
217867static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
217868 return pIter->aLvl[0].iRowid;
217869}
217870static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
217871 return pIter->aLvl[0].iLeafPgno;
217872}
217873
217874/*
217875** Load the next leaf page into the segment iterator.
217876*/
217877static void fts5SegIterNextPage(
217878 Fts5Index *p, /* FTS5 backend object */
217879 Fts5SegIter *pIter /* Iterator to advance to next page */
217880){
217881 Fts5Data *pLeaf;
217882 Fts5StructureSegment *pSeg = pIter->pSeg;
217883 fts5DataRelease(pIter->pLeaf);
217884 pIter->iLeafPgno++;
217885 if( pIter->pNextLeaf ){
217886 pIter->pLeaf = pIter->pNextLeaf;
217887 pIter->pNextLeaf = 0;
217888 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
217889 pIter->pLeaf = fts5LeafRead(p,
217890 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
217891 );
217892 }else{
217893 pIter->pLeaf = 0;
217894 }
217895 pLeaf = pIter->pLeaf;
217896
217897 if( pLeaf ){
217898 pIter->iPgidxOff = pLeaf->szLeaf;
217899 if( fts5LeafIsTermless(pLeaf) ){
217900 pIter->iEndofDoclist = pLeaf->nn+1;
217901 }else{
217902 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
217903 pIter->iEndofDoclist
217904 );
217905 }
217906 }
217907}
217908
217909/*
217910** Argument p points to a buffer containing a varint to be interpreted as a
217911** position list size field. Read the varint and return the number of bytes
217912** read. Before returning, set *pnSz to the number of bytes in the position
217913** list, and *pbDel to true if the delete flag is set, or false otherwise.
217914*/
217915static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
217916 int nSz;
217917 int n = 0;
217918 fts5FastGetVarint32(p, n, nSz);
217919 assert_nc( nSz>=0 );
217920 *pnSz = nSz/2;
217921 *pbDel = nSz & 0x0001;
217922 return n;
217923}
217924
217925/*
217926** Fts5SegIter.iLeafOffset currently points to the first byte of a
217927** position-list size field. Read the value of the field and store it
217928** in the following variables:
217929**
217930** Fts5SegIter.nPos
217931** Fts5SegIter.bDel
217932**
217933** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
217934** position list content (if any).
217935*/
217936static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
217937 if( p->rc==SQLITE_OK ){
217938 int iOff = pIter->iLeafOffset; /* Offset to read at */
217939 ASSERT_SZLEAF_OK(pIter->pLeaf);
217940 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
217941 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
217942 pIter->bDel = 0;
217943 pIter->nPos = 1;
217944 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
217945 pIter->bDel = 1;
217946 iOff++;
217947 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
217948 pIter->nPos = 1;
217949 iOff++;
217950 }else{
217951 pIter->nPos = 0;
217952 }
217953 }
217954 }else{
217955 int nSz;
217956 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
217957 pIter->bDel = (nSz & 0x0001);
217958 pIter->nPos = nSz>>1;
217959 assert_nc( pIter->nPos>=0 );
217960 }
217961 pIter->iLeafOffset = iOff;
217962 }
217963}
217964
217965static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
217966 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
217967 int iOff = pIter->iLeafOffset;
217968
217969 ASSERT_SZLEAF_OK(pIter->pLeaf);
217970 if( iOff>=pIter->pLeaf->szLeaf ){
217971 fts5SegIterNextPage(p, pIter);
217972 if( pIter->pLeaf==0 ){
217973 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
217974 return;
217975 }
217976 iOff = 4;
217977 a = pIter->pLeaf->p;
217978 }
217979 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
217980 pIter->iLeafOffset = iOff;
217981}
217982
217983/*
217984** Fts5SegIter.iLeafOffset currently points to the first byte of the
217985** "nSuffix" field of a term. Function parameter nKeep contains the value
217986** of the "nPrefix" field (if there was one - it is passed 0 if this is
217987** the first term in the segment).
217988**
217989** This function populates:
217990**
217991** Fts5SegIter.term
217992** Fts5SegIter.rowid
217993**
217994** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
217995** the first position list. The position list belonging to document
217996** (Fts5SegIter.iRowid).
217997*/
217998static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
217999 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
218000 int iOff = pIter->iLeafOffset; /* Offset to read at */
218001 int nNew; /* Bytes of new data */
218002
218003 iOff += fts5GetVarint32(&a[iOff], nNew);
218004 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
218005 p->rc = FTS5_CORRUPT;
218006 return;
218007 }
218008 pIter->term.n = nKeep;
218009 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
218010 assert( pIter->term.n<=pIter->term.nSpace );
218011 iOff += nNew;
218012 pIter->iTermLeafOffset = iOff;
218013 pIter->iTermLeafPgno = pIter->iLeafPgno;
218014 pIter->iLeafOffset = iOff;
218015
218016 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
218017 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
218018 }else{
218019 int nExtra;
218020 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
218021 pIter->iEndofDoclist += nExtra;
218022 }
218023
218024 fts5SegIterLoadRowid(p, pIter);
218025}
218026
218027static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
218028static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
218029static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
218030
218031static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
218032 if( pIter->flags & FTS5_SEGITER_REVERSE ){
218033 pIter->xNext = fts5SegIterNext_Reverse;
218034 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
218035 pIter->xNext = fts5SegIterNext_None;
218036 }else{
218037 pIter->xNext = fts5SegIterNext;
218038 }
218039}
218040
218041/*
218042** Initialize the iterator object pIter to iterate through the entries in
218043** segment pSeg. The iterator is left pointing to the first entry when
218044** this function returns.
218045**
218046** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
218047** an error has already occurred when this function is called, it is a no-op.
218048*/
218049static void fts5SegIterInit(
218050 Fts5Index *p, /* FTS index object */
218051 Fts5StructureSegment *pSeg, /* Description of segment */
218052 Fts5SegIter *pIter /* Object to populate */
218053){
218054 if( pSeg->pgnoFirst==0 ){
218055 /* This happens if the segment is being used as an input to an incremental
218056 ** merge and all data has already been "trimmed". See function
218057 ** fts5TrimSegments() for details. In this case leave the iterator empty.
218058 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
218059 ** at EOF already. */
218060 assert( pIter->pLeaf==0 );
218061 return;
218062 }
218063
218064 if( p->rc==SQLITE_OK ){
218065 memset(pIter, 0, sizeof(*pIter));
218066 fts5SegIterSetNext(p, pIter);
218067 pIter->pSeg = pSeg;
218068 pIter->iLeafPgno = pSeg->pgnoFirst-1;
218069 fts5SegIterNextPage(p, pIter);
218070 }
218071
218072 if( p->rc==SQLITE_OK ){
218073 pIter->iLeafOffset = 4;
218074 assert_nc( pIter->pLeaf->nn>4 );
218075 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
218076 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
218077 fts5SegIterLoadTerm(p, pIter, 0);
218078 fts5SegIterLoadNPos(p, pIter);
218079 }
218080}
218081
218082/*
218083** This function is only ever called on iterators created by calls to
218084** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
218085**
218086** The iterator is in an unusual state when this function is called: the
218087** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
218088** the position-list size field for the first relevant rowid on the page.
218089** Fts5SegIter.rowid is set, but nPos and bDel are not.
218090**
218091** This function advances the iterator so that it points to the last
218092** relevant rowid on the page and, if necessary, initializes the
218093** aRowidOffset[] and iRowidOffset variables. At this point the iterator
218094** is in its regular state - Fts5SegIter.iLeafOffset points to the first
218095** byte of the position list content associated with said rowid.
218096*/
218097static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
218098 int eDetail = p->pConfig->eDetail;
218099 int n = pIter->pLeaf->szLeaf;
218100 int i = pIter->iLeafOffset;
218101 u8 *a = pIter->pLeaf->p;
218102 int iRowidOffset = 0;
218103
218104 if( n>pIter->iEndofDoclist ){
218105 n = pIter->iEndofDoclist;
218106 }
218107
218108 ASSERT_SZLEAF_OK(pIter->pLeaf);
218109 while( 1 ){
218110 i64 iDelta = 0;
218111
218112 if( eDetail==FTS5_DETAIL_NONE ){
218113 /* todo */
218114 if( i<n && a[i]==0 ){
218115 i++;
218116 if( i<n && a[i]==0 ) i++;
218117 }
218118 }else{
218119 int nPos;
218120 int bDummy;
218121 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
218122 i += nPos;
218123 }
218124 if( i>=n ) break;
218125 i += fts5GetVarint(&a[i], (u64*)&iDelta);
218126 pIter->iRowid += iDelta;
218127
218128 /* If necessary, grow the pIter->aRowidOffset[] array. */
218129 if( iRowidOffset>=pIter->nRowidOffset ){
218130 int nNew = pIter->nRowidOffset + 8;
218131 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
218132 if( aNew==0 ){
218133 p->rc = SQLITE_NOMEM;
218134 break;
218135 }
218136 pIter->aRowidOffset = aNew;
218137 pIter->nRowidOffset = nNew;
218138 }
218139
218140 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
218141 pIter->iLeafOffset = i;
218142 }
218143 pIter->iRowidOffset = iRowidOffset;
218144 fts5SegIterLoadNPos(p, pIter);
218145}
218146
218147/*
218148**
218149*/
218150static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
218151 assert( pIter->flags & FTS5_SEGITER_REVERSE );
218152 assert( pIter->flags & FTS5_SEGITER_ONETERM );
218153
218154 fts5DataRelease(pIter->pLeaf);
218155 pIter->pLeaf = 0;
218156 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
218157 Fts5Data *pNew;
218158 pIter->iLeafPgno--;
218159 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
218160 pIter->pSeg->iSegid, pIter->iLeafPgno
218161 ));
218162 if( pNew ){
218163 /* iTermLeafOffset may be equal to szLeaf if the term is the last
218164 ** thing on the page - i.e. the first rowid is on the following page.
218165 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
218166 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
218167 assert( pIter->pLeaf==0 );
218168 if( pIter->iTermLeafOffset<pNew->szLeaf ){
218169 pIter->pLeaf = pNew;
218170 pIter->iLeafOffset = pIter->iTermLeafOffset;
218171 }
218172 }else{
218173 int iRowidOff;
218174 iRowidOff = fts5LeafFirstRowidOff(pNew);
218175 if( iRowidOff ){
218176 pIter->pLeaf = pNew;
218177 pIter->iLeafOffset = iRowidOff;
218178 }
218179 }
218180
218181 if( pIter->pLeaf ){
218182 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
218183 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
218184 break;
218185 }else{
218186 fts5DataRelease(pNew);
218187 }
218188 }
218189 }
218190
218191 if( pIter->pLeaf ){
218192 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
218193 fts5SegIterReverseInitPage(p, pIter);
218194 }
218195}
218196
218197/*
218198** Return true if the iterator passed as the second argument currently
218199** points to a delete marker. A delete marker is an entry with a 0 byte
218200** position-list.
218201*/
218202static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
218203 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
218204 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
218205}
218206
218207/*
218208** Advance iterator pIter to the next entry.
218209**
218210** This version of fts5SegIterNext() is only used by reverse iterators.
218211*/
218212static void fts5SegIterNext_Reverse(
218213 Fts5Index *p, /* FTS5 backend object */
218214 Fts5SegIter *pIter, /* Iterator to advance */
218215 int *pbUnused /* Unused */
218216){
218217 assert( pIter->flags & FTS5_SEGITER_REVERSE );
218218 assert( pIter->pNextLeaf==0 );
218219 UNUSED_PARAM(pbUnused);
218220
218221 if( pIter->iRowidOffset>0 ){
218222 u8 *a = pIter->pLeaf->p;
218223 int iOff;
218224 i64 iDelta;
218225
218226 pIter->iRowidOffset--;
218227 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
218228 fts5SegIterLoadNPos(p, pIter);
218229 iOff = pIter->iLeafOffset;
218230 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
218231 iOff += pIter->nPos;
218232 }
218233 fts5GetVarint(&a[iOff], (u64*)&iDelta);
218234 pIter->iRowid -= iDelta;
218235 }else{
218236 fts5SegIterReverseNewPage(p, pIter);
218237 }
218238}
218239
218240/*
218241** Advance iterator pIter to the next entry.
218242**
218243** This version of fts5SegIterNext() is only used if detail=none and the
218244** iterator is not a reverse direction iterator.
218245*/
218246static void fts5SegIterNext_None(
218247 Fts5Index *p, /* FTS5 backend object */
218248 Fts5SegIter *pIter, /* Iterator to advance */
218249 int *pbNewTerm /* OUT: Set for new term */
218250){
218251 int iOff;
218252
218253 assert( p->rc==SQLITE_OK );
218254 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
218255 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
218256
218257 ASSERT_SZLEAF_OK(pIter->pLeaf);
218258 iOff = pIter->iLeafOffset;
218259
218260 /* Next entry is on the next page */
218261 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
218262 fts5SegIterNextPage(p, pIter);
218263 if( p->rc || pIter->pLeaf==0 ) return;
218264 pIter->iRowid = 0;
218265 iOff = 4;
218266 }
218267
218268 if( iOff<pIter->iEndofDoclist ){
218269 /* Next entry is on the current page */
218270 i64 iDelta;
218271 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
218272 pIter->iLeafOffset = iOff;
218273 pIter->iRowid += iDelta;
218274 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
218275 if( pIter->pSeg ){
218276 int nKeep = 0;
218277 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
218278 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
218279 }
218280 pIter->iLeafOffset = iOff;
218281 fts5SegIterLoadTerm(p, pIter, nKeep);
218282 }else{
218283 const u8 *pList = 0;
218284 const char *zTerm = 0;
218285 int nList;
218286 sqlite3Fts5HashScanNext(p->pHash);
218287 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
218288 if( pList==0 ) goto next_none_eof;
218289 pIter->pLeaf->p = (u8*)pList;
218290 pIter->pLeaf->nn = nList;
218291 pIter->pLeaf->szLeaf = nList;
218292 pIter->iEndofDoclist = nList;
218293 sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
218294 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
218295 }
218296
218297 if( pbNewTerm ) *pbNewTerm = 1;
218298 }else{
218299 goto next_none_eof;
218300 }
218301
218302 fts5SegIterLoadNPos(p, pIter);
218303
218304 return;
218305 next_none_eof:
218306 fts5DataRelease(pIter->pLeaf);
218307 pIter->pLeaf = 0;
218308}
218309
218310
218311/*
218312** Advance iterator pIter to the next entry.
218313**
218314** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
218315** is not considered an error if the iterator reaches EOF. If an error has
218316** already occurred when this function is called, it is a no-op.
218317*/
218318static void fts5SegIterNext(
218319 Fts5Index *p, /* FTS5 backend object */
218320 Fts5SegIter *pIter, /* Iterator to advance */
218321 int *pbNewTerm /* OUT: Set for new term */
218322){
218323 Fts5Data *pLeaf = pIter->pLeaf;
218324 int iOff;
218325 int bNewTerm = 0;
218326 int nKeep = 0;
218327 u8 *a;
218328 int n;
218329
218330 assert( pbNewTerm==0 || *pbNewTerm==0 );
218331 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
218332
218333 /* Search for the end of the position list within the current page. */
218334 a = pLeaf->p;
218335 n = pLeaf->szLeaf;
218336
218337 ASSERT_SZLEAF_OK(pLeaf);
218338 iOff = pIter->iLeafOffset + pIter->nPos;
218339
218340 if( iOff<n ){
218341 /* The next entry is on the current page. */
218342 assert_nc( iOff<=pIter->iEndofDoclist );
218343 if( iOff>=pIter->iEndofDoclist ){
218344 bNewTerm = 1;
218345 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
218346 iOff += fts5GetVarint32(&a[iOff], nKeep);
218347 }
218348 }else{
218349 u64 iDelta;
218350 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
218351 pIter->iRowid += iDelta;
218352 assert_nc( iDelta>0 );
218353 }
218354 pIter->iLeafOffset = iOff;
218355
218356 }else if( pIter->pSeg==0 ){
218357 const u8 *pList = 0;
218358 const char *zTerm = 0;
218359 int nList = 0;
218360 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
218361 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
218362 sqlite3Fts5HashScanNext(p->pHash);
218363 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
218364 }
218365 if( pList==0 ){
218366 fts5DataRelease(pIter->pLeaf);
218367 pIter->pLeaf = 0;
218368 }else{
218369 pIter->pLeaf->p = (u8*)pList;
218370 pIter->pLeaf->nn = nList;
218371 pIter->pLeaf->szLeaf = nList;
218372 pIter->iEndofDoclist = nList+1;
218373 sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
218374 (u8*)zTerm);
218375 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
218376 *pbNewTerm = 1;
218377 }
218378 }else{
218379 iOff = 0;
218380 /* Next entry is not on the current page */
218381 while( iOff==0 ){
218382 fts5SegIterNextPage(p, pIter);
218383 pLeaf = pIter->pLeaf;
218384 if( pLeaf==0 ) break;
218385 ASSERT_SZLEAF_OK(pLeaf);
218386 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
218387 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
218388 pIter->iLeafOffset = iOff;
218389
218390 if( pLeaf->nn>pLeaf->szLeaf ){
218391 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
218392 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
218393 );
218394 }
218395 }
218396 else if( pLeaf->nn>pLeaf->szLeaf ){
218397 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
218398 &pLeaf->p[pLeaf->szLeaf], iOff
218399 );
218400 pIter->iLeafOffset = iOff;
218401 pIter->iEndofDoclist = iOff;
218402 bNewTerm = 1;
218403 }
218404 assert_nc( iOff<pLeaf->szLeaf );
218405 if( iOff>pLeaf->szLeaf ){
218406 p->rc = FTS5_CORRUPT;
218407 return;
218408 }
218409 }
218410 }
218411
218412 /* Check if the iterator is now at EOF. If so, return early. */
218413 if( pIter->pLeaf ){
218414 if( bNewTerm ){
218415 if( pIter->flags & FTS5_SEGITER_ONETERM ){
218416 fts5DataRelease(pIter->pLeaf);
218417 pIter->pLeaf = 0;
218418 }else{
218419 fts5SegIterLoadTerm(p, pIter, nKeep);
218420 fts5SegIterLoadNPos(p, pIter);
218421 if( pbNewTerm ) *pbNewTerm = 1;
218422 }
218423 }else{
218424 /* The following could be done by calling fts5SegIterLoadNPos(). But
218425 ** this block is particularly performance critical, so equivalent
218426 ** code is inlined.
218427 **
218428 ** Later: Switched back to fts5SegIterLoadNPos() because it supports
218429 ** detail=none mode. Not ideal.
218430 */
218431 int nSz;
218432 assert( p->rc==SQLITE_OK );
218433 assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
218434 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
218435 pIter->bDel = (nSz & 0x0001);
218436 pIter->nPos = nSz>>1;
218437 assert_nc( pIter->nPos>=0 );
218438 }
218439 }
218440}
218441
218442#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
218443
218444#define fts5IndexSkipVarint(a, iOff) { \
218445 int iEnd = iOff+9; \
218446 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
218447}
218448
218449/*
218450** Iterator pIter currently points to the first rowid in a doclist. This
218451** function sets the iterator up so that iterates in reverse order through
218452** the doclist.
218453*/
218454static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
218455 Fts5DlidxIter *pDlidx = pIter->pDlidx;
218456 Fts5Data *pLast = 0;
218457 int pgnoLast = 0;
218458
218459 if( pDlidx ){
218460 int iSegid = pIter->pSeg->iSegid;
218461 pgnoLast = fts5DlidxIterPgno(pDlidx);
218462 pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
218463 }else{
218464 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
218465
218466 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
218467 ** position-list content for the current rowid. Back it up so that it
218468 ** points to the start of the position-list size field. */
218469 int iPoslist;
218470 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
218471 iPoslist = pIter->iTermLeafOffset;
218472 }else{
218473 iPoslist = 4;
218474 }
218475 fts5IndexSkipVarint(pLeaf->p, iPoslist);
218476 pIter->iLeafOffset = iPoslist;
218477
218478 /* If this condition is true then the largest rowid for the current
218479 ** term may not be stored on the current page. So search forward to
218480 ** see where said rowid really is. */
218481 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
218482 int pgno;
218483 Fts5StructureSegment *pSeg = pIter->pSeg;
218484
218485 /* The last rowid in the doclist may not be on the current page. Search
218486 ** forward to find the page containing the last rowid. */
218487 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
218488 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
218489 Fts5Data *pNew = fts5DataRead(p, iAbs);
218490 if( pNew ){
218491 int iRowid, bTermless;
218492 iRowid = fts5LeafFirstRowidOff(pNew);
218493 bTermless = fts5LeafIsTermless(pNew);
218494 if( iRowid ){
218495 SWAPVAL(Fts5Data*, pNew, pLast);
218496 pgnoLast = pgno;
218497 }
218498 fts5DataRelease(pNew);
218499 if( bTermless==0 ) break;
218500 }
218501 }
218502 }
218503 }
218504
218505 /* If pLast is NULL at this point, then the last rowid for this doclist
218506 ** lies on the page currently indicated by the iterator. In this case
218507 ** pIter->iLeafOffset is already set to point to the position-list size
218508 ** field associated with the first relevant rowid on the page.
218509 **
218510 ** Or, if pLast is non-NULL, then it is the page that contains the last
218511 ** rowid. In this case configure the iterator so that it points to the
218512 ** first rowid on this page.
218513 */
218514 if( pLast ){
218515 int iOff;
218516 fts5DataRelease(pIter->pLeaf);
218517 pIter->pLeaf = pLast;
218518 pIter->iLeafPgno = pgnoLast;
218519 iOff = fts5LeafFirstRowidOff(pLast);
218520 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
218521 pIter->iLeafOffset = iOff;
218522
218523 if( fts5LeafIsTermless(pLast) ){
218524 pIter->iEndofDoclist = pLast->nn+1;
218525 }else{
218526 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
218527 }
218528
218529 }
218530
218531 fts5SegIterReverseInitPage(p, pIter);
218532}
218533
218534/*
218535** Iterator pIter currently points to the first rowid of a doclist.
218536** There is a doclist-index associated with the final term on the current
218537** page. If the current term is the last term on the page, load the
218538** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
218539*/
218540static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
218541 int iSeg = pIter->pSeg->iSegid;
218542 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
218543 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
218544
218545 assert( pIter->flags & FTS5_SEGITER_ONETERM );
218546 assert( pIter->pDlidx==0 );
218547
218548 /* Check if the current doclist ends on this page. If it does, return
218549 ** early without loading the doclist-index (as it belongs to a different
218550 ** term. */
218551 if( pIter->iTermLeafPgno==pIter->iLeafPgno
218552 && pIter->iEndofDoclist<pLeaf->szLeaf
218553 ){
218554 return;
218555 }
218556
218557 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
218558}
218559
218560/*
218561** The iterator object passed as the second argument currently contains
218562** no valid values except for the Fts5SegIter.pLeaf member variable. This
218563** function searches the leaf page for a term matching (pTerm/nTerm).
218564**
218565** If the specified term is found on the page, then the iterator is left
218566** pointing to it. If argument bGe is zero and the term is not found,
218567** the iterator is left pointing at EOF.
218568**
218569** If bGe is non-zero and the specified term is not found, then the
218570** iterator is left pointing to the smallest term in the segment that
218571** is larger than the specified term, even if this term is not on the
218572** current page.
218573*/
218574static void fts5LeafSeek(
218575 Fts5Index *p, /* Leave any error code here */
218576 int bGe, /* True for a >= search */
218577 Fts5SegIter *pIter, /* Iterator to seek */
218578 const u8 *pTerm, int nTerm /* Term to search for */
218579){
218580 int iOff;
218581 const u8 *a = pIter->pLeaf->p;
218582 int szLeaf = pIter->pLeaf->szLeaf;
218583 int n = pIter->pLeaf->nn;
218584
218585 u32 nMatch = 0;
218586 u32 nKeep = 0;
218587 u32 nNew = 0;
218588 u32 iTermOff;
218589 int iPgidx; /* Current offset in pgidx */
218590 int bEndOfPage = 0;
218591
218592 assert( p->rc==SQLITE_OK );
218593
218594 iPgidx = szLeaf;
218595 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
218596 iOff = iTermOff;
218597 if( iOff>n ){
218598 p->rc = FTS5_CORRUPT;
218599 return;
218600 }
218601
218602 while( 1 ){
218603
218604 /* Figure out how many new bytes are in this term */
218605 fts5FastGetVarint32(a, iOff, nNew);
218606 if( nKeep<nMatch ){
218607 goto search_failed;
218608 }
218609
218610 assert( nKeep>=nMatch );
218611 if( nKeep==nMatch ){
218612 u32 nCmp;
218613 u32 i;
218614 nCmp = (u32)MIN(nNew, nTerm-nMatch);
218615 for(i=0; i<nCmp; i++){
218616 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
218617 }
218618 nMatch += i;
218619
218620 if( (u32)nTerm==nMatch ){
218621 if( i==nNew ){
218622 goto search_success;
218623 }else{
218624 goto search_failed;
218625 }
218626 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
218627 goto search_failed;
218628 }
218629 }
218630
218631 if( iPgidx>=n ){
218632 bEndOfPage = 1;
218633 break;
218634 }
218635
218636 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
218637 iTermOff += nKeep;
218638 iOff = iTermOff;
218639
218640 if( iOff>=n ){
218641 p->rc = FTS5_CORRUPT;
218642 return;
218643 }
218644
218645 /* Read the nKeep field of the next term. */
218646 fts5FastGetVarint32(a, iOff, nKeep);
218647 }
218648
218649 search_failed:
218650 if( bGe==0 ){
218651 fts5DataRelease(pIter->pLeaf);
218652 pIter->pLeaf = 0;
218653 return;
218654 }else if( bEndOfPage ){
218655 do {
218656 fts5SegIterNextPage(p, pIter);
218657 if( pIter->pLeaf==0 ) return;
218658 a = pIter->pLeaf->p;
218659 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
218660 iPgidx = pIter->pLeaf->szLeaf;
218661 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
218662 if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
218663 p->rc = FTS5_CORRUPT;
218664 return;
218665 }else{
218666 nKeep = 0;
218667 iTermOff = iOff;
218668 n = pIter->pLeaf->nn;
218669 iOff += fts5GetVarint32(&a[iOff], nNew);
218670 break;
218671 }
218672 }
218673 }while( 1 );
218674 }
218675
218676 search_success:
218677 if( (i64)iOff+nNew>n || nNew<1 ){
218678 p->rc = FTS5_CORRUPT;
218679 return;
218680 }
218681 pIter->iLeafOffset = iOff + nNew;
218682 pIter->iTermLeafOffset = pIter->iLeafOffset;
218683 pIter->iTermLeafPgno = pIter->iLeafPgno;
218684
218685 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
218686 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
218687
218688 if( iPgidx>=n ){
218689 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
218690 }else{
218691 int nExtra;
218692 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
218693 pIter->iEndofDoclist = iTermOff + nExtra;
218694 }
218695 pIter->iPgidxOff = iPgidx;
218696
218697 fts5SegIterLoadRowid(p, pIter);
218698 fts5SegIterLoadNPos(p, pIter);
218699}
218700
218701static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
218702 if( p->pIdxSelect==0 ){
218703 Fts5Config *pConfig = p->pConfig;
218704 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
218705 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
218706 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
218707 pConfig->zDb, pConfig->zName
218708 ));
218709 }
218710 return p->pIdxSelect;
218711}
218712
218713/*
218714** Initialize the object pIter to point to term pTerm/nTerm within segment
218715** pSeg. If there is no such term in the index, the iterator is set to EOF.
218716**
218717** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
218718** an error has already occurred when this function is called, it is a no-op.
218719*/
218720static void fts5SegIterSeekInit(
218721 Fts5Index *p, /* FTS5 backend */
218722 const u8 *pTerm, int nTerm, /* Term to seek to */
218723 int flags, /* Mask of FTS5INDEX_XXX flags */
218724 Fts5StructureSegment *pSeg, /* Description of segment */
218725 Fts5SegIter *pIter /* Object to populate */
218726){
218727 int iPg = 1;
218728 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
218729 int bDlidx = 0; /* True if there is a doclist-index */
218730 sqlite3_stmt *pIdxSelect = 0;
218731
218732 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
218733 assert( pTerm && nTerm );
218734 memset(pIter, 0, sizeof(*pIter));
218735 pIter->pSeg = pSeg;
218736
218737 /* This block sets stack variable iPg to the leaf page number that may
218738 ** contain term (pTerm/nTerm), if it is present in the segment. */
218739 pIdxSelect = fts5IdxSelectStmt(p);
218740 if( p->rc ) return;
218741 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
218742 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
218743 if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
218744 i64 val = sqlite3_column_int(pIdxSelect, 0);
218745 iPg = (int)(val>>1);
218746 bDlidx = (val & 0x0001);
218747 }
218748 p->rc = sqlite3_reset(pIdxSelect);
218749 sqlite3_bind_null(pIdxSelect, 2);
218750
218751 if( iPg<pSeg->pgnoFirst ){
218752 iPg = pSeg->pgnoFirst;
218753 bDlidx = 0;
218754 }
218755
218756 pIter->iLeafPgno = iPg - 1;
218757 fts5SegIterNextPage(p, pIter);
218758
218759 if( pIter->pLeaf ){
218760 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
218761 }
218762
218763 if( p->rc==SQLITE_OK && bGe==0 ){
218764 pIter->flags |= FTS5_SEGITER_ONETERM;
218765 if( pIter->pLeaf ){
218766 if( flags & FTS5INDEX_QUERY_DESC ){
218767 pIter->flags |= FTS5_SEGITER_REVERSE;
218768 }
218769 if( bDlidx ){
218770 fts5SegIterLoadDlidx(p, pIter);
218771 }
218772 if( flags & FTS5INDEX_QUERY_DESC ){
218773 fts5SegIterReverse(p, pIter);
218774 }
218775 }
218776 }
218777
218778 fts5SegIterSetNext(p, pIter);
218779
218780 /* Either:
218781 **
218782 ** 1) an error has occurred, or
218783 ** 2) the iterator points to EOF, or
218784 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
218785 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
218786 ** to an entry with a term greater than or equal to (pTerm/nTerm).
218787 */
218788 assert_nc( p->rc!=SQLITE_OK /* 1 */
218789 || pIter->pLeaf==0 /* 2 */
218790 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
218791 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
218792 );
218793}
218794
218795/*
218796** Initialize the object pIter to point to term pTerm/nTerm within the
218797** in-memory hash table. If there is no such term in the hash-table, the
218798** iterator is set to EOF.
218799**
218800** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
218801** an error has already occurred when this function is called, it is a no-op.
218802*/
218803static void fts5SegIterHashInit(
218804 Fts5Index *p, /* FTS5 backend */
218805 const u8 *pTerm, int nTerm, /* Term to seek to */
218806 int flags, /* Mask of FTS5INDEX_XXX flags */
218807 Fts5SegIter *pIter /* Object to populate */
218808){
218809 int nList = 0;
218810 const u8 *z = 0;
218811 int n = 0;
218812 Fts5Data *pLeaf = 0;
218813
218814 assert( p->pHash );
218815 assert( p->rc==SQLITE_OK );
218816
218817 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
218818 const u8 *pList = 0;
218819
218820 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
218821 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
218822 n = (z ? (int)strlen((const char*)z) : 0);
218823 if( pList ){
218824 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
218825 if( pLeaf ){
218826 pLeaf->p = (u8*)pList;
218827 }
218828 }
218829 }else{
218830 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
218831 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
218832 );
218833 if( pLeaf ){
218834 pLeaf->p = (u8*)&pLeaf[1];
218835 }
218836 z = pTerm;
218837 n = nTerm;
218838 pIter->flags |= FTS5_SEGITER_ONETERM;
218839 }
218840
218841 if( pLeaf ){
218842 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
218843 pLeaf->nn = pLeaf->szLeaf = nList;
218844 pIter->pLeaf = pLeaf;
218845 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
218846 pIter->iEndofDoclist = pLeaf->nn;
218847
218848 if( flags & FTS5INDEX_QUERY_DESC ){
218849 pIter->flags |= FTS5_SEGITER_REVERSE;
218850 fts5SegIterReverseInitPage(p, pIter);
218851 }else{
218852 fts5SegIterLoadNPos(p, pIter);
218853 }
218854 }
218855
218856 fts5SegIterSetNext(p, pIter);
218857}
218858
218859/*
218860** Zero the iterator passed as the only argument.
218861*/
218862static void fts5SegIterClear(Fts5SegIter *pIter){
218863 fts5BufferFree(&pIter->term);
218864 fts5DataRelease(pIter->pLeaf);
218865 fts5DataRelease(pIter->pNextLeaf);
218866 fts5DlidxIterFree(pIter->pDlidx);
218867 sqlite3_free(pIter->aRowidOffset);
218868 memset(pIter, 0, sizeof(Fts5SegIter));
218869}
218870
218871#ifdef SQLITE_DEBUG
218872
218873/*
218874** This function is used as part of the big assert() procedure implemented by
218875** fts5AssertMultiIterSetup(). It ensures that the result currently stored
218876** in *pRes is the correct result of comparing the current positions of the
218877** two iterators.
218878*/
218879static void fts5AssertComparisonResult(
218880 Fts5Iter *pIter,
218881 Fts5SegIter *p1,
218882 Fts5SegIter *p2,
218883 Fts5CResult *pRes
218884){
218885 int i1 = p1 - pIter->aSeg;
218886 int i2 = p2 - pIter->aSeg;
218887
218888 if( p1->pLeaf || p2->pLeaf ){
218889 if( p1->pLeaf==0 ){
218890 assert( pRes->iFirst==i2 );
218891 }else if( p2->pLeaf==0 ){
218892 assert( pRes->iFirst==i1 );
218893 }else{
218894 int nMin = MIN(p1->term.n, p2->term.n);
218895 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
218896 if( res==0 ) res = p1->term.n - p2->term.n;
218897
218898 if( res==0 ){
218899 assert( pRes->bTermEq==1 );
218900 assert( p1->iRowid!=p2->iRowid );
218901 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
218902 }else{
218903 assert( pRes->bTermEq==0 );
218904 }
218905
218906 if( res<0 ){
218907 assert( pRes->iFirst==i1 );
218908 }else{
218909 assert( pRes->iFirst==i2 );
218910 }
218911 }
218912 }
218913}
218914
218915/*
218916** This function is a no-op unless SQLITE_DEBUG is defined when this module
218917** is compiled. In that case, this function is essentially an assert()
218918** statement used to verify that the contents of the pIter->aFirst[] array
218919** are correct.
218920*/
218921static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
218922 if( p->rc==SQLITE_OK ){
218923 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
218924 int i;
218925
218926 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
218927
218928 /* Check that pIter->iSwitchRowid is set correctly. */
218929 for(i=0; i<pIter->nSeg; i++){
218930 Fts5SegIter *p1 = &pIter->aSeg[i];
218931 assert( p1==pFirst
218932 || p1->pLeaf==0
218933 || fts5BufferCompare(&pFirst->term, &p1->term)
218934 || p1->iRowid==pIter->iSwitchRowid
218935 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
218936 );
218937 }
218938
218939 for(i=0; i<pIter->nSeg; i+=2){
218940 Fts5SegIter *p1 = &pIter->aSeg[i];
218941 Fts5SegIter *p2 = &pIter->aSeg[i+1];
218942 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
218943 fts5AssertComparisonResult(pIter, p1, p2, pRes);
218944 }
218945
218946 for(i=1; i<(pIter->nSeg / 2); i+=2){
218947 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
218948 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
218949 Fts5CResult *pRes = &pIter->aFirst[i];
218950 fts5AssertComparisonResult(pIter, p1, p2, pRes);
218951 }
218952 }
218953}
218954#else
218955# define fts5AssertMultiIterSetup(x,y)
218956#endif
218957
218958/*
218959** Do the comparison necessary to populate pIter->aFirst[iOut].
218960**
218961** If the returned value is non-zero, then it is the index of an entry
218962** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
218963** to a key that is a duplicate of another, higher priority,
218964** segment-iterator in the pSeg->aSeg[] array.
218965*/
218966static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
218967 int i1; /* Index of left-hand Fts5SegIter */
218968 int i2; /* Index of right-hand Fts5SegIter */
218969 int iRes;
218970 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
218971 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
218972 Fts5CResult *pRes = &pIter->aFirst[iOut];
218973
218974 assert( iOut<pIter->nSeg && iOut>0 );
218975 assert( pIter->bRev==0 || pIter->bRev==1 );
218976
218977 if( iOut>=(pIter->nSeg/2) ){
218978 i1 = (iOut - pIter->nSeg/2) * 2;
218979 i2 = i1 + 1;
218980 }else{
218981 i1 = pIter->aFirst[iOut*2].iFirst;
218982 i2 = pIter->aFirst[iOut*2+1].iFirst;
218983 }
218984 p1 = &pIter->aSeg[i1];
218985 p2 = &pIter->aSeg[i2];
218986
218987 pRes->bTermEq = 0;
218988 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
218989 iRes = i2;
218990 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
218991 iRes = i1;
218992 }else{
218993 int res = fts5BufferCompare(&p1->term, &p2->term);
218994 if( res==0 ){
218995 assert_nc( i2>i1 );
218996 assert_nc( i2!=0 );
218997 pRes->bTermEq = 1;
218998 if( p1->iRowid==p2->iRowid ){
218999 p1->bDel = p2->bDel;
219000 return i2;
219001 }
219002 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
219003 }
219004 assert( res!=0 );
219005 if( res<0 ){
219006 iRes = i1;
219007 }else{
219008 iRes = i2;
219009 }
219010 }
219011
219012 pRes->iFirst = (u16)iRes;
219013 return 0;
219014}
219015
219016/*
219017** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
219018** It is an error if leaf iLeafPgno does not exist or contains no rowids.
219019*/
219020static void fts5SegIterGotoPage(
219021 Fts5Index *p, /* FTS5 backend object */
219022 Fts5SegIter *pIter, /* Iterator to advance */
219023 int iLeafPgno
219024){
219025 assert( iLeafPgno>pIter->iLeafPgno );
219026
219027 if( iLeafPgno>pIter->pSeg->pgnoLast ){
219028 p->rc = FTS5_CORRUPT;
219029 }else{
219030 fts5DataRelease(pIter->pNextLeaf);
219031 pIter->pNextLeaf = 0;
219032 pIter->iLeafPgno = iLeafPgno-1;
219033 fts5SegIterNextPage(p, pIter);
219034 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
219035
219036 if( p->rc==SQLITE_OK ){
219037 int iOff;
219038 u8 *a = pIter->pLeaf->p;
219039 int n = pIter->pLeaf->szLeaf;
219040
219041 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
219042 if( iOff<4 || iOff>=n ){
219043 p->rc = FTS5_CORRUPT;
219044 }else{
219045 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
219046 pIter->iLeafOffset = iOff;
219047 fts5SegIterLoadNPos(p, pIter);
219048 }
219049 }
219050 }
219051}
219052
219053/*
219054** Advance the iterator passed as the second argument until it is at or
219055** past rowid iFrom. Regardless of the value of iFrom, the iterator is
219056** always advanced at least once.
219057*/
219058static void fts5SegIterNextFrom(
219059 Fts5Index *p, /* FTS5 backend object */
219060 Fts5SegIter *pIter, /* Iterator to advance */
219061 i64 iMatch /* Advance iterator at least this far */
219062){
219063 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
219064 Fts5DlidxIter *pDlidx = pIter->pDlidx;
219065 int iLeafPgno = pIter->iLeafPgno;
219066 int bMove = 1;
219067
219068 assert( pIter->flags & FTS5_SEGITER_ONETERM );
219069 assert( pIter->pDlidx );
219070 assert( pIter->pLeaf );
219071
219072 if( bRev==0 ){
219073 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
219074 iLeafPgno = fts5DlidxIterPgno(pDlidx);
219075 fts5DlidxIterNext(p, pDlidx);
219076 }
219077 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
219078 if( iLeafPgno>pIter->iLeafPgno ){
219079 fts5SegIterGotoPage(p, pIter, iLeafPgno);
219080 bMove = 0;
219081 }
219082 }else{
219083 assert( pIter->pNextLeaf==0 );
219084 assert( iMatch<pIter->iRowid );
219085 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
219086 fts5DlidxIterPrev(p, pDlidx);
219087 }
219088 iLeafPgno = fts5DlidxIterPgno(pDlidx);
219089
219090 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
219091
219092 if( iLeafPgno<pIter->iLeafPgno ){
219093 pIter->iLeafPgno = iLeafPgno+1;
219094 fts5SegIterReverseNewPage(p, pIter);
219095 bMove = 0;
219096 }
219097 }
219098
219099 do{
219100 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
219101 if( pIter->pLeaf==0 ) break;
219102 if( bRev==0 && pIter->iRowid>=iMatch ) break;
219103 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
219104 bMove = 1;
219105 }while( p->rc==SQLITE_OK );
219106}
219107
219108
219109/*
219110** Free the iterator object passed as the second argument.
219111*/
219112static void fts5MultiIterFree(Fts5Iter *pIter){
219113 if( pIter ){
219114 int i;
219115 for(i=0; i<pIter->nSeg; i++){
219116 fts5SegIterClear(&pIter->aSeg[i]);
219117 }
219118 fts5BufferFree(&pIter->poslist);
219119 sqlite3_free(pIter);
219120 }
219121}
219122
219123static void fts5MultiIterAdvanced(
219124 Fts5Index *p, /* FTS5 backend to iterate within */
219125 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
219126 int iChanged, /* Index of sub-iterator just advanced */
219127 int iMinset /* Minimum entry in aFirst[] to set */
219128){
219129 int i;
219130 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
219131 int iEq;
219132 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
219133 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
219134 assert( p->rc==SQLITE_OK );
219135 pSeg->xNext(p, pSeg, 0);
219136 i = pIter->nSeg + iEq;
219137 }
219138 }
219139}
219140
219141/*
219142** Sub-iterator iChanged of iterator pIter has just been advanced. It still
219143** points to the same term though - just a different rowid. This function
219144** attempts to update the contents of the pIter->aFirst[] accordingly.
219145** If it does so successfully, 0 is returned. Otherwise 1.
219146**
219147** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
219148** on the iterator instead. That function does the same as this one, except
219149** that it deals with more complicated cases as well.
219150*/
219151static int fts5MultiIterAdvanceRowid(
219152 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
219153 int iChanged, /* Index of sub-iterator just advanced */
219154 Fts5SegIter **ppFirst
219155){
219156 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
219157
219158 if( pNew->iRowid==pIter->iSwitchRowid
219159 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
219160 ){
219161 int i;
219162 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
219163 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
219164 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
219165 Fts5CResult *pRes = &pIter->aFirst[i];
219166
219167 assert( pNew->pLeaf );
219168 assert( pRes->bTermEq==0 || pOther->pLeaf );
219169
219170 if( pRes->bTermEq ){
219171 if( pNew->iRowid==pOther->iRowid ){
219172 return 1;
219173 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
219174 pIter->iSwitchRowid = pOther->iRowid;
219175 pNew = pOther;
219176 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
219177 pIter->iSwitchRowid = pOther->iRowid;
219178 }
219179 }
219180 pRes->iFirst = (u16)(pNew - pIter->aSeg);
219181 if( i==1 ) break;
219182
219183 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
219184 }
219185 }
219186
219187 *ppFirst = pNew;
219188 return 0;
219189}
219190
219191/*
219192** Set the pIter->bEof variable based on the state of the sub-iterators.
219193*/
219194static void fts5MultiIterSetEof(Fts5Iter *pIter){
219195 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
219196 pIter->base.bEof = pSeg->pLeaf==0;
219197 pIter->iSwitchRowid = pSeg->iRowid;
219198}
219199
219200/*
219201** Move the iterator to the next entry.
219202**
219203** If an error occurs, an error code is left in Fts5Index.rc. It is not
219204** considered an error if the iterator reaches EOF, or if it is already at
219205** EOF when this function is called.
219206*/
219207static void fts5MultiIterNext(
219208 Fts5Index *p,
219209 Fts5Iter *pIter,
219210 int bFrom, /* True if argument iFrom is valid */
219211 i64 iFrom /* Advance at least as far as this */
219212){
219213 int bUseFrom = bFrom;
219214 assert( pIter->base.bEof==0 );
219215 while( p->rc==SQLITE_OK ){
219216 int iFirst = pIter->aFirst[1].iFirst;
219217 int bNewTerm = 0;
219218 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
219219 assert( p->rc==SQLITE_OK );
219220 if( bUseFrom && pSeg->pDlidx ){
219221 fts5SegIterNextFrom(p, pSeg, iFrom);
219222 }else{
219223 pSeg->xNext(p, pSeg, &bNewTerm);
219224 }
219225
219226 if( pSeg->pLeaf==0 || bNewTerm
219227 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
219228 ){
219229 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
219230 fts5MultiIterSetEof(pIter);
219231 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
219232 if( pSeg->pLeaf==0 ) return;
219233 }
219234
219235 fts5AssertMultiIterSetup(p, pIter);
219236 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
219237 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
219238 pIter->xSetOutputs(pIter, pSeg);
219239 return;
219240 }
219241 bUseFrom = 0;
219242 }
219243}
219244
219245static void fts5MultiIterNext2(
219246 Fts5Index *p,
219247 Fts5Iter *pIter,
219248 int *pbNewTerm /* OUT: True if *might* be new term */
219249){
219250 assert( pIter->bSkipEmpty );
219251 if( p->rc==SQLITE_OK ){
219252 *pbNewTerm = 0;
219253 do{
219254 int iFirst = pIter->aFirst[1].iFirst;
219255 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
219256 int bNewTerm = 0;
219257
219258 assert( p->rc==SQLITE_OK );
219259 pSeg->xNext(p, pSeg, &bNewTerm);
219260 if( pSeg->pLeaf==0 || bNewTerm
219261 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
219262 ){
219263 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
219264 fts5MultiIterSetEof(pIter);
219265 *pbNewTerm = 1;
219266 }
219267 fts5AssertMultiIterSetup(p, pIter);
219268
219269 }while( fts5MultiIterIsEmpty(p, pIter) );
219270 }
219271}
219272
219273static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
219274 UNUSED_PARAM2(pUnused1, pUnused2);
219275}
219276
219277static Fts5Iter *fts5MultiIterAlloc(
219278 Fts5Index *p, /* FTS5 backend to iterate within */
219279 int nSeg
219280){
219281 Fts5Iter *pNew;
219282 int nSlot; /* Power of two >= nSeg */
219283
219284 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
219285 pNew = fts5IdxMalloc(p,
219286 sizeof(Fts5Iter) + /* pNew */
219287 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
219288 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
219289 );
219290 if( pNew ){
219291 pNew->nSeg = nSlot;
219292 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
219293 pNew->pIndex = p;
219294 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
219295 }
219296 return pNew;
219297}
219298
219299static void fts5PoslistCallback(
219300 Fts5Index *pUnused,
219301 void *pContext,
219302 const u8 *pChunk, int nChunk
219303){
219304 UNUSED_PARAM(pUnused);
219305 assert_nc( nChunk>=0 );
219306 if( nChunk>0 ){
219307 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
219308 }
219309}
219310
219311typedef struct PoslistCallbackCtx PoslistCallbackCtx;
219312struct PoslistCallbackCtx {
219313 Fts5Buffer *pBuf; /* Append to this buffer */
219314 Fts5Colset *pColset; /* Restrict matches to this column */
219315 int eState; /* See above */
219316};
219317
219318typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
219319struct PoslistOffsetsCtx {
219320 Fts5Buffer *pBuf; /* Append to this buffer */
219321 Fts5Colset *pColset; /* Restrict matches to this column */
219322 int iRead;
219323 int iWrite;
219324};
219325
219326/*
219327** TODO: Make this more efficient!
219328*/
219329static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
219330 int i;
219331 for(i=0; i<pColset->nCol; i++){
219332 if( pColset->aiCol[i]==iCol ) return 1;
219333 }
219334 return 0;
219335}
219336
219337static void fts5PoslistOffsetsCallback(
219338 Fts5Index *pUnused,
219339 void *pContext,
219340 const u8 *pChunk, int nChunk
219341){
219342 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
219343 UNUSED_PARAM(pUnused);
219344 assert_nc( nChunk>=0 );
219345 if( nChunk>0 ){
219346 int i = 0;
219347 while( i<nChunk ){
219348 int iVal;
219349 i += fts5GetVarint32(&pChunk[i], iVal);
219350 iVal += pCtx->iRead - 2;
219351 pCtx->iRead = iVal;
219352 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
219353 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
219354 pCtx->iWrite = iVal;
219355 }
219356 }
219357 }
219358}
219359
219360static void fts5PoslistFilterCallback(
219361 Fts5Index *pUnused,
219362 void *pContext,
219363 const u8 *pChunk, int nChunk
219364){
219365 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
219366 UNUSED_PARAM(pUnused);
219367 assert_nc( nChunk>=0 );
219368 if( nChunk>0 ){
219369 /* Search through to find the first varint with value 1. This is the
219370 ** start of the next columns hits. */
219371 int i = 0;
219372 int iStart = 0;
219373
219374 if( pCtx->eState==2 ){
219375 int iCol;
219376 fts5FastGetVarint32(pChunk, i, iCol);
219377 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
219378 pCtx->eState = 1;
219379 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
219380 }else{
219381 pCtx->eState = 0;
219382 }
219383 }
219384
219385 do {
219386 while( i<nChunk && pChunk[i]!=0x01 ){
219387 while( pChunk[i] & 0x80 ) i++;
219388 i++;
219389 }
219390 if( pCtx->eState ){
219391 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
219392 }
219393 if( i<nChunk ){
219394 int iCol;
219395 iStart = i;
219396 i++;
219397 if( i>=nChunk ){
219398 pCtx->eState = 2;
219399 }else{
219400 fts5FastGetVarint32(pChunk, i, iCol);
219401 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
219402 if( pCtx->eState ){
219403 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
219404 iStart = i;
219405 }
219406 }
219407 }
219408 }while( i<nChunk );
219409 }
219410}
219411
219412static void fts5ChunkIterate(
219413 Fts5Index *p, /* Index object */
219414 Fts5SegIter *pSeg, /* Poslist of this iterator */
219415 void *pCtx, /* Context pointer for xChunk callback */
219416 void (*xChunk)(Fts5Index*, void*, const u8*, int)
219417){
219418 int nRem = pSeg->nPos; /* Number of bytes still to come */
219419 Fts5Data *pData = 0;
219420 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
219421 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
219422 int pgno = pSeg->iLeafPgno;
219423 int pgnoSave = 0;
219424
219425 /* This function does notmwork with detail=none databases. */
219426 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
219427
219428 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
219429 pgnoSave = pgno+1;
219430 }
219431
219432 while( 1 ){
219433 xChunk(p, pCtx, pChunk, nChunk);
219434 nRem -= nChunk;
219435 fts5DataRelease(pData);
219436 if( nRem<=0 ){
219437 break;
219438 }else{
219439 pgno++;
219440 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
219441 if( pData==0 ) break;
219442 pChunk = &pData->p[4];
219443 nChunk = MIN(nRem, pData->szLeaf - 4);
219444 if( pgno==pgnoSave ){
219445 assert( pSeg->pNextLeaf==0 );
219446 pSeg->pNextLeaf = pData;
219447 pData = 0;
219448 }
219449 }
219450 }
219451}
219452
219453/*
219454** Iterator pIter currently points to a valid entry (not EOF). This
219455** function appends the position list data for the current entry to
219456** buffer pBuf. It does not make a copy of the position-list size
219457** field.
219458*/
219459static void fts5SegiterPoslist(
219460 Fts5Index *p,
219461 Fts5SegIter *pSeg,
219462 Fts5Colset *pColset,
219463 Fts5Buffer *pBuf
219464){
219465 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
219466 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING);
219467 if( pColset==0 ){
219468 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
219469 }else{
219470 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
219471 PoslistCallbackCtx sCtx;
219472 sCtx.pBuf = pBuf;
219473 sCtx.pColset = pColset;
219474 sCtx.eState = fts5IndexColsetTest(pColset, 0);
219475 assert( sCtx.eState==0 || sCtx.eState==1 );
219476 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
219477 }else{
219478 PoslistOffsetsCtx sCtx;
219479 memset(&sCtx, 0, sizeof(sCtx));
219480 sCtx.pBuf = pBuf;
219481 sCtx.pColset = pColset;
219482 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
219483 }
219484 }
219485 }
219486}
219487
219488/*
219489** IN/OUT parameter (*pa) points to a position list n bytes in size. If
219490** the position list contains entries for column iCol, then (*pa) is set
219491** to point to the sub-position-list for that column and the number of
219492** bytes in it returned. Or, if the argument position list does not
219493** contain any entries for column iCol, return 0.
219494*/
219495static int fts5IndexExtractCol(
219496 const u8 **pa, /* IN/OUT: Pointer to poslist */
219497 int n, /* IN: Size of poslist in bytes */
219498 int iCol /* Column to extract from poslist */
219499){
219500 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
219501 const u8 *p = *pa;
219502 const u8 *pEnd = &p[n]; /* One byte past end of position list */
219503
219504 while( iCol>iCurrent ){
219505 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
219506 ** not part of a varint. Note that it is not possible for a negative
219507 ** or extremely large varint to occur within an uncorrupted position
219508 ** list. So the last byte of each varint may be assumed to have a clear
219509 ** 0x80 bit. */
219510 while( *p!=0x01 ){
219511 while( *p++ & 0x80 );
219512 if( p>=pEnd ) return 0;
219513 }
219514 *pa = p++;
219515 iCurrent = *p++;
219516 if( iCurrent & 0x80 ){
219517 p--;
219518 p += fts5GetVarint32(p, iCurrent);
219519 }
219520 }
219521 if( iCol!=iCurrent ) return 0;
219522
219523 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
219524 ** not part of a varint */
219525 while( p<pEnd && *p!=0x01 ){
219526 while( *p++ & 0x80 );
219527 }
219528
219529 return p - (*pa);
219530}
219531
219532static void fts5IndexExtractColset(
219533 int *pRc,
219534 Fts5Colset *pColset, /* Colset to filter on */
219535 const u8 *pPos, int nPos, /* Position list */
219536 Fts5Buffer *pBuf /* Output buffer */
219537){
219538 if( *pRc==SQLITE_OK ){
219539 int i;
219540 fts5BufferZero(pBuf);
219541 for(i=0; i<pColset->nCol; i++){
219542 const u8 *pSub = pPos;
219543 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
219544 if( nSub ){
219545 fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
219546 }
219547 }
219548 }
219549}
219550
219551/*
219552** xSetOutputs callback used by detail=none tables.
219553*/
219554static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
219555 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
219556 pIter->base.iRowid = pSeg->iRowid;
219557 pIter->base.nData = pSeg->nPos;
219558}
219559
219560/*
219561** xSetOutputs callback used by detail=full and detail=col tables when no
219562** column filters are specified.
219563*/
219564static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
219565 pIter->base.iRowid = pSeg->iRowid;
219566 pIter->base.nData = pSeg->nPos;
219567
219568 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
219569 assert( pIter->pColset==0 );
219570
219571 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
219572 /* All data is stored on the current page. Populate the output
219573 ** variables to point into the body of the page object. */
219574 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
219575 }else{
219576 /* The data is distributed over two or more pages. Copy it into the
219577 ** Fts5Iter.poslist buffer and then set the output pointer to point
219578 ** to this buffer. */
219579 fts5BufferZero(&pIter->poslist);
219580 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
219581 pIter->base.pData = pIter->poslist.p;
219582 }
219583}
219584
219585/*
219586** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
219587** against no columns at all).
219588*/
219589static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
219590 UNUSED_PARAM(pSeg);
219591 pIter->base.nData = 0;
219592}
219593
219594/*
219595** xSetOutputs callback used by detail=col when there is a column filter
219596** and there are 100 or more columns. Also called as a fallback from
219597** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
219598*/
219599static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
219600 fts5BufferZero(&pIter->poslist);
219601 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
219602 pIter->base.iRowid = pSeg->iRowid;
219603 pIter->base.pData = pIter->poslist.p;
219604 pIter->base.nData = pIter->poslist.n;
219605}
219606
219607/*
219608** xSetOutputs callback used when:
219609**
219610** * detail=col,
219611** * there is a column filter, and
219612** * the table contains 100 or fewer columns.
219613**
219614** The last point is to ensure all column numbers are stored as
219615** single-byte varints.
219616*/
219617static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
219618
219619 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
219620 assert( pIter->pColset );
219621
219622 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
219623 fts5IterSetOutputs_Col(pIter, pSeg);
219624 }else{
219625 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
219626 u8 *pEnd = (u8*)&a[pSeg->nPos];
219627 int iPrev = 0;
219628 int *aiCol = pIter->pColset->aiCol;
219629 int *aiColEnd = &aiCol[pIter->pColset->nCol];
219630
219631 u8 *aOut = pIter->poslist.p;
219632 int iPrevOut = 0;
219633
219634 pIter->base.iRowid = pSeg->iRowid;
219635
219636 while( a<pEnd ){
219637 iPrev += (int)a++[0] - 2;
219638 while( *aiCol<iPrev ){
219639 aiCol++;
219640 if( aiCol==aiColEnd ) goto setoutputs_col_out;
219641 }
219642 if( *aiCol==iPrev ){
219643 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
219644 iPrevOut = iPrev;
219645 }
219646 }
219647
219648setoutputs_col_out:
219649 pIter->base.pData = pIter->poslist.p;
219650 pIter->base.nData = aOut - pIter->poslist.p;
219651 }
219652}
219653
219654/*
219655** xSetOutputs callback used by detail=full when there is a column filter.
219656*/
219657static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
219658 Fts5Colset *pColset = pIter->pColset;
219659 pIter->base.iRowid = pSeg->iRowid;
219660
219661 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
219662 assert( pColset );
219663
219664 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
219665 /* All data is stored on the current page. Populate the output
219666 ** variables to point into the body of the page object. */
219667 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
219668 if( pColset->nCol==1 ){
219669 pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
219670 pIter->base.pData = a;
219671 }else{
219672 int *pRc = &pIter->pIndex->rc;
219673 fts5BufferZero(&pIter->poslist);
219674 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
219675 pIter->base.pData = pIter->poslist.p;
219676 pIter->base.nData = pIter->poslist.n;
219677 }
219678 }else{
219679 /* The data is distributed over two or more pages. Copy it into the
219680 ** Fts5Iter.poslist buffer and then set the output pointer to point
219681 ** to this buffer. */
219682 fts5BufferZero(&pIter->poslist);
219683 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
219684 pIter->base.pData = pIter->poslist.p;
219685 pIter->base.nData = pIter->poslist.n;
219686 }
219687}
219688
219689static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
219690 if( *pRc==SQLITE_OK ){
219691 Fts5Config *pConfig = pIter->pIndex->pConfig;
219692 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
219693 pIter->xSetOutputs = fts5IterSetOutputs_None;
219694 }
219695
219696 else if( pIter->pColset==0 ){
219697 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
219698 }
219699
219700 else if( pIter->pColset->nCol==0 ){
219701 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
219702 }
219703
219704 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
219705 pIter->xSetOutputs = fts5IterSetOutputs_Full;
219706 }
219707
219708 else{
219709 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
219710 if( pConfig->nCol<=100 ){
219711 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
219712 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
219713 }else{
219714 pIter->xSetOutputs = fts5IterSetOutputs_Col;
219715 }
219716 }
219717 }
219718}
219719
219720
219721/*
219722** Allocate a new Fts5Iter object.
219723**
219724** The new object will be used to iterate through data in structure pStruct.
219725** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
219726** is zero or greater, data from the first nSegment segments on level iLevel
219727** is merged.
219728**
219729** The iterator initially points to the first term/rowid entry in the
219730** iterated data.
219731*/
219732static void fts5MultiIterNew(
219733 Fts5Index *p, /* FTS5 backend to iterate within */
219734 Fts5Structure *pStruct, /* Structure of specific index */
219735 int flags, /* FTS5INDEX_QUERY_XXX flags */
219736 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
219737 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
219738 int iLevel, /* Level to iterate (-1 for all) */
219739 int nSegment, /* Number of segments to merge (iLevel>=0) */
219740 Fts5Iter **ppOut /* New object */
219741){
219742 int nSeg = 0; /* Number of segment-iters in use */
219743 int iIter = 0; /* */
219744 int iSeg; /* Used to iterate through segments */
219745 Fts5StructureLevel *pLvl;
219746 Fts5Iter *pNew;
219747
219748 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
219749
219750 /* Allocate space for the new multi-seg-iterator. */
219751 if( p->rc==SQLITE_OK ){
219752 if( iLevel<0 ){
219753 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
219754 nSeg = pStruct->nSegment;
219755 nSeg += (p->pHash ? 1 : 0);
219756 }else{
219757 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
219758 }
219759 }
219760 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
219761 if( pNew==0 ) return;
219762 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
219763 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
219764 pNew->pColset = pColset;
219765 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
219766 fts5IterSetOutputCb(&p->rc, pNew);
219767 }
219768
219769 /* Initialize each of the component segment iterators. */
219770 if( p->rc==SQLITE_OK ){
219771 if( iLevel<0 ){
219772 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
219773 if( p->pHash ){
219774 /* Add a segment iterator for the current contents of the hash table. */
219775 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
219776 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
219777 }
219778 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
219779 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
219780 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
219781 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
219782 if( pTerm==0 ){
219783 fts5SegIterInit(p, pSeg, pIter);
219784 }else{
219785 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
219786 }
219787 }
219788 }
219789 }else{
219790 pLvl = &pStruct->aLevel[iLevel];
219791 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
219792 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
219793 }
219794 }
219795 assert( iIter==nSeg );
219796 }
219797
219798 /* If the above was successful, each component iterators now points
219799 ** to the first entry in its segment. In this case initialize the
219800 ** aFirst[] array. Or, if an error has occurred, free the iterator
219801 ** object and set the output variable to NULL. */
219802 if( p->rc==SQLITE_OK ){
219803 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
219804 int iEq;
219805 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
219806 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
219807 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
219808 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
219809 }
219810 }
219811 fts5MultiIterSetEof(pNew);
219812 fts5AssertMultiIterSetup(p, pNew);
219813
219814 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
219815 fts5MultiIterNext(p, pNew, 0, 0);
219816 }else if( pNew->base.bEof==0 ){
219817 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
219818 pNew->xSetOutputs(pNew, pSeg);
219819 }
219820
219821 }else{
219822 fts5MultiIterFree(pNew);
219823 *ppOut = 0;
219824 }
219825}
219826
219827/*
219828** Create an Fts5Iter that iterates through the doclist provided
219829** as the second argument.
219830*/
219831static void fts5MultiIterNew2(
219832 Fts5Index *p, /* FTS5 backend to iterate within */
219833 Fts5Data *pData, /* Doclist to iterate through */
219834 int bDesc, /* True for descending rowid order */
219835 Fts5Iter **ppOut /* New object */
219836){
219837 Fts5Iter *pNew;
219838 pNew = fts5MultiIterAlloc(p, 2);
219839 if( pNew ){
219840 Fts5SegIter *pIter = &pNew->aSeg[1];
219841
219842 pIter->flags = FTS5_SEGITER_ONETERM;
219843 if( pData->szLeaf>0 ){
219844 pIter->pLeaf = pData;
219845 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
219846 pIter->iEndofDoclist = pData->nn;
219847 pNew->aFirst[1].iFirst = 1;
219848 if( bDesc ){
219849 pNew->bRev = 1;
219850 pIter->flags |= FTS5_SEGITER_REVERSE;
219851 fts5SegIterReverseInitPage(p, pIter);
219852 }else{
219853 fts5SegIterLoadNPos(p, pIter);
219854 }
219855 pData = 0;
219856 }else{
219857 pNew->base.bEof = 1;
219858 }
219859 fts5SegIterSetNext(p, pIter);
219860
219861 *ppOut = pNew;
219862 }
219863
219864 fts5DataRelease(pData);
219865}
219866
219867/*
219868** Return true if the iterator is at EOF or if an error has occurred.
219869** False otherwise.
219870*/
219871static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
219872 assert( p->rc
219873 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
219874 );
219875 return (p->rc || pIter->base.bEof);
219876}
219877
219878/*
219879** Return the rowid of the entry that the iterator currently points
219880** to. If the iterator points to EOF when this function is called the
219881** results are undefined.
219882*/
219883static i64 fts5MultiIterRowid(Fts5Iter *pIter){
219884 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
219885 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
219886}
219887
219888/*
219889** Move the iterator to the next entry at or following iMatch.
219890*/
219891static void fts5MultiIterNextFrom(
219892 Fts5Index *p,
219893 Fts5Iter *pIter,
219894 i64 iMatch
219895){
219896 while( 1 ){
219897 i64 iRowid;
219898 fts5MultiIterNext(p, pIter, 1, iMatch);
219899 if( fts5MultiIterEof(p, pIter) ) break;
219900 iRowid = fts5MultiIterRowid(pIter);
219901 if( pIter->bRev==0 && iRowid>=iMatch ) break;
219902 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
219903 }
219904}
219905
219906/*
219907** Return a pointer to a buffer containing the term associated with the
219908** entry that the iterator currently points to.
219909*/
219910static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
219911 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
219912 *pn = p->term.n;
219913 return p->term.p;
219914}
219915
219916/*
219917** Allocate a new segment-id for the structure pStruct. The new segment
219918** id must be between 1 and 65335 inclusive, and must not be used by
219919** any currently existing segment. If a free segment id cannot be found,
219920** SQLITE_FULL is returned.
219921**
219922** If an error has already occurred, this function is a no-op. 0 is
219923** returned in this case.
219924*/
219925static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
219926 int iSegid = 0;
219927
219928 if( p->rc==SQLITE_OK ){
219929 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
219930 p->rc = SQLITE_FULL;
219931 }else{
219932 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
219933 ** array is 63 elements, or 252 bytes, in size. */
219934 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
219935 int iLvl, iSeg;
219936 int i;
219937 u32 mask;
219938 memset(aUsed, 0, sizeof(aUsed));
219939 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
219940 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
219941 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
219942 if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
219943 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
219944 }
219945 }
219946 }
219947
219948 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
219949 mask = aUsed[i];
219950 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
219951 iSegid += 1 + i*32;
219952
219953#ifdef SQLITE_DEBUG
219954 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
219955 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
219956 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
219957 }
219958 }
219959 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
219960
219961 {
219962 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
219963 if( p->rc==SQLITE_OK ){
219964 u8 aBlob[2] = {0xff, 0xff};
219965 sqlite3_bind_int(pIdxSelect, 1, iSegid);
219966 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
219967 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
219968 p->rc = sqlite3_reset(pIdxSelect);
219969 sqlite3_bind_null(pIdxSelect, 2);
219970 }
219971 }
219972#endif
219973 }
219974 }
219975
219976 return iSegid;
219977}
219978
219979/*
219980** Discard all data currently cached in the hash-tables.
219981*/
219982static void fts5IndexDiscardData(Fts5Index *p){
219983 assert( p->pHash || p->nPendingData==0 );
219984 if( p->pHash ){
219985 sqlite3Fts5HashClear(p->pHash);
219986 p->nPendingData = 0;
219987 }
219988}
219989
219990/*
219991** Return the size of the prefix, in bytes, that buffer
219992** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
219993**
219994** Buffer (pNew/<length-unknown>) is guaranteed to be greater
219995** than buffer (pOld/nOld).
219996*/
219997static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
219998 int i;
219999 for(i=0; i<nOld; i++){
220000 if( pOld[i]!=pNew[i] ) break;
220001 }
220002 return i;
220003}
220004
220005static void fts5WriteDlidxClear(
220006 Fts5Index *p,
220007 Fts5SegWriter *pWriter,
220008 int bFlush /* If true, write dlidx to disk */
220009){
220010 int i;
220011 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
220012 for(i=0; i<pWriter->nDlidx; i++){
220013 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
220014 if( pDlidx->buf.n==0 ) break;
220015 if( bFlush ){
220016 assert( pDlidx->pgno!=0 );
220017 fts5DataWrite(p,
220018 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
220019 pDlidx->buf.p, pDlidx->buf.n
220020 );
220021 }
220022 sqlite3Fts5BufferZero(&pDlidx->buf);
220023 pDlidx->bPrevValid = 0;
220024 }
220025}
220026
220027/*
220028** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
220029** Any new array elements are zeroed before returning.
220030*/
220031static int fts5WriteDlidxGrow(
220032 Fts5Index *p,
220033 Fts5SegWriter *pWriter,
220034 int nLvl
220035){
220036 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
220037 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
220038 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
220039 );
220040 if( aDlidx==0 ){
220041 p->rc = SQLITE_NOMEM;
220042 }else{
220043 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
220044 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
220045 pWriter->aDlidx = aDlidx;
220046 pWriter->nDlidx = nLvl;
220047 }
220048 }
220049 return p->rc;
220050}
220051
220052/*
220053** If the current doclist-index accumulating in pWriter->aDlidx[] is large
220054** enough, flush it to disk and return 1. Otherwise discard it and return
220055** zero.
220056*/
220057static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
220058 int bFlag = 0;
220059
220060 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
220061 ** to the database, also write the doclist-index to disk. */
220062 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
220063 bFlag = 1;
220064 }
220065 fts5WriteDlidxClear(p, pWriter, bFlag);
220066 pWriter->nEmpty = 0;
220067 return bFlag;
220068}
220069
220070/*
220071** This function is called whenever processing of the doclist for the
220072** last term on leaf page (pWriter->iBtPage) is completed.
220073**
220074** The doclist-index for that term is currently stored in-memory within the
220075** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
220076** writes it out to disk. Or, if it is too small to bother with, discards
220077** it.
220078**
220079** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
220080*/
220081static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
220082 int bFlag;
220083
220084 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
220085 if( pWriter->iBtPage==0 ) return;
220086 bFlag = fts5WriteFlushDlidx(p, pWriter);
220087
220088 if( p->rc==SQLITE_OK ){
220089 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
220090 /* The following was already done in fts5WriteInit(): */
220091 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
220092 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
220093 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
220094 sqlite3_step(p->pIdxWriter);
220095 p->rc = sqlite3_reset(p->pIdxWriter);
220096 sqlite3_bind_null(p->pIdxWriter, 2);
220097 }
220098 pWriter->iBtPage = 0;
220099}
220100
220101/*
220102** This is called once for each leaf page except the first that contains
220103** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
220104** is larger than all terms written to earlier leaves, and equal to or
220105** smaller than the first term on the new leaf.
220106**
220107** If an error occurs, an error code is left in Fts5Index.rc. If an error
220108** has already occurred when this function is called, it is a no-op.
220109*/
220110static void fts5WriteBtreeTerm(
220111 Fts5Index *p, /* FTS5 backend object */
220112 Fts5SegWriter *pWriter, /* Writer object */
220113 int nTerm, const u8 *pTerm /* First term on new page */
220114){
220115 fts5WriteFlushBtree(p, pWriter);
220116 if( p->rc==SQLITE_OK ){
220117 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
220118 pWriter->iBtPage = pWriter->writer.pgno;
220119 }
220120}
220121
220122/*
220123** This function is called when flushing a leaf page that contains no
220124** terms at all to disk.
220125*/
220126static void fts5WriteBtreeNoTerm(
220127 Fts5Index *p, /* FTS5 backend object */
220128 Fts5SegWriter *pWriter /* Writer object */
220129){
220130 /* If there were no rowids on the leaf page either and the doclist-index
220131 ** has already been started, append an 0x00 byte to it. */
220132 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
220133 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
220134 assert( pDlidx->bPrevValid );
220135 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
220136 }
220137
220138 /* Increment the "number of sequential leaves without a term" counter. */
220139 pWriter->nEmpty++;
220140}
220141
220142static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
220143 i64 iRowid;
220144 int iOff;
220145
220146 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
220147 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
220148 return iRowid;
220149}
220150
220151/*
220152** Rowid iRowid has just been appended to the current leaf page. It is the
220153** first on the page. This function appends an appropriate entry to the current
220154** doclist-index.
220155*/
220156static void fts5WriteDlidxAppend(
220157 Fts5Index *p,
220158 Fts5SegWriter *pWriter,
220159 i64 iRowid
220160){
220161 int i;
220162 int bDone = 0;
220163
220164 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
220165 i64 iVal;
220166 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
220167
220168 if( pDlidx->buf.n>=p->pConfig->pgsz ){
220169 /* The current doclist-index page is full. Write it to disk and push
220170 ** a copy of iRowid (which will become the first rowid on the next
220171 ** doclist-index leaf page) up into the next level of the b-tree
220172 ** hierarchy. If the node being flushed is currently the root node,
220173 ** also push its first rowid upwards. */
220174 pDlidx->buf.p[0] = 0x01; /* Not the root node */
220175 fts5DataWrite(p,
220176 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
220177 pDlidx->buf.p, pDlidx->buf.n
220178 );
220179 fts5WriteDlidxGrow(p, pWriter, i+2);
220180 pDlidx = &pWriter->aDlidx[i];
220181 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
220182 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
220183
220184 /* This was the root node. Push its first rowid up to the new root. */
220185 pDlidx[1].pgno = pDlidx->pgno;
220186 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
220187 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
220188 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
220189 pDlidx[1].bPrevValid = 1;
220190 pDlidx[1].iPrev = iFirst;
220191 }
220192
220193 sqlite3Fts5BufferZero(&pDlidx->buf);
220194 pDlidx->bPrevValid = 0;
220195 pDlidx->pgno++;
220196 }else{
220197 bDone = 1;
220198 }
220199
220200 if( pDlidx->bPrevValid ){
220201 iVal = iRowid - pDlidx->iPrev;
220202 }else{
220203 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
220204 assert( pDlidx->buf.n==0 );
220205 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
220206 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
220207 iVal = iRowid;
220208 }
220209
220210 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
220211 pDlidx->bPrevValid = 1;
220212 pDlidx->iPrev = iRowid;
220213 }
220214}
220215
220216static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
220217 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
220218 Fts5PageWriter *pPage = &pWriter->writer;
220219 i64 iRowid;
220220
220221 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
220222
220223 /* Set the szLeaf header field. */
220224 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
220225 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
220226
220227 if( pWriter->bFirstTermInPage ){
220228 /* No term was written to this page. */
220229 assert( pPage->pgidx.n==0 );
220230 fts5WriteBtreeNoTerm(p, pWriter);
220231 }else{
220232 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
220233 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
220234 }
220235
220236 /* Write the page out to disk */
220237 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
220238 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
220239
220240 /* Initialize the next page. */
220241 fts5BufferZero(&pPage->buf);
220242 fts5BufferZero(&pPage->pgidx);
220243 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
220244 pPage->iPrevPgidx = 0;
220245 pPage->pgno++;
220246
220247 /* Increase the leaves written counter */
220248 pWriter->nLeafWritten++;
220249
220250 /* The new leaf holds no terms or rowids */
220251 pWriter->bFirstTermInPage = 1;
220252 pWriter->bFirstRowidInPage = 1;
220253}
220254
220255/*
220256** Append term pTerm/nTerm to the segment being written by the writer passed
220257** as the second argument.
220258**
220259** If an error occurs, set the Fts5Index.rc error code. If an error has
220260** already occurred, this function is a no-op.
220261*/
220262static void fts5WriteAppendTerm(
220263 Fts5Index *p,
220264 Fts5SegWriter *pWriter,
220265 int nTerm, const u8 *pTerm
220266){
220267 int nPrefix; /* Bytes of prefix compression for term */
220268 Fts5PageWriter *pPage = &pWriter->writer;
220269 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
220270 int nMin = MIN(pPage->term.n, nTerm);
220271
220272 assert( p->rc==SQLITE_OK );
220273 assert( pPage->buf.n>=4 );
220274 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
220275
220276 /* If the current leaf page is full, flush it to disk. */
220277 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
220278 if( pPage->buf.n>4 ){
220279 fts5WriteFlushLeaf(p, pWriter);
220280 if( p->rc!=SQLITE_OK ) return;
220281 }
220282 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
220283 }
220284
220285 /* TODO1: Updating pgidx here. */
220286 pPgidx->n += sqlite3Fts5PutVarint(
220287 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
220288 );
220289 pPage->iPrevPgidx = pPage->buf.n;
220290#if 0
220291 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
220292 pPgidx->n += 2;
220293#endif
220294
220295 if( pWriter->bFirstTermInPage ){
220296 nPrefix = 0;
220297 if( pPage->pgno!=1 ){
220298 /* This is the first term on a leaf that is not the leftmost leaf in
220299 ** the segment b-tree. In this case it is necessary to add a term to
220300 ** the b-tree hierarchy that is (a) larger than the largest term
220301 ** already written to the segment and (b) smaller than or equal to
220302 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
220303 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
220304 ** previous term.
220305 **
220306 ** Usually, the previous term is available in pPage->term. The exception
220307 ** is if this is the first term written in an incremental-merge step.
220308 ** In this case the previous term is not available, so just write a
220309 ** copy of (pTerm/nTerm) into the parent node. This is slightly
220310 ** inefficient, but still correct. */
220311 int n = nTerm;
220312 if( pPage->term.n ){
220313 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
220314 }
220315 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
220316 if( p->rc!=SQLITE_OK ) return;
220317 pPage = &pWriter->writer;
220318 }
220319 }else{
220320 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
220321 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
220322 }
220323
220324 /* Append the number of bytes of new data, then the term data itself
220325 ** to the page. */
220326 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
220327 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
220328
220329 /* Update the Fts5PageWriter.term field. */
220330 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
220331 pWriter->bFirstTermInPage = 0;
220332
220333 pWriter->bFirstRowidInPage = 0;
220334 pWriter->bFirstRowidInDoclist = 1;
220335
220336 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
220337 pWriter->aDlidx[0].pgno = pPage->pgno;
220338}
220339
220340/*
220341** Append a rowid and position-list size field to the writers output.
220342*/
220343static void fts5WriteAppendRowid(
220344 Fts5Index *p,
220345 Fts5SegWriter *pWriter,
220346 i64 iRowid
220347){
220348 if( p->rc==SQLITE_OK ){
220349 Fts5PageWriter *pPage = &pWriter->writer;
220350
220351 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
220352 fts5WriteFlushLeaf(p, pWriter);
220353 }
220354
220355 /* If this is to be the first rowid written to the page, set the
220356 ** rowid-pointer in the page-header. Also append a value to the dlidx
220357 ** buffer, in case a doclist-index is required. */
220358 if( pWriter->bFirstRowidInPage ){
220359 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
220360 fts5WriteDlidxAppend(p, pWriter, iRowid);
220361 }
220362
220363 /* Write the rowid. */
220364 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
220365 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
220366 }else{
220367 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
220368 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
220369 }
220370 pWriter->iPrevRowid = iRowid;
220371 pWriter->bFirstRowidInDoclist = 0;
220372 pWriter->bFirstRowidInPage = 0;
220373 }
220374}
220375
220376static void fts5WriteAppendPoslistData(
220377 Fts5Index *p,
220378 Fts5SegWriter *pWriter,
220379 const u8 *aData,
220380 int nData
220381){
220382 Fts5PageWriter *pPage = &pWriter->writer;
220383 const u8 *a = aData;
220384 int n = nData;
220385
220386 assert( p->pConfig->pgsz>0 );
220387 while( p->rc==SQLITE_OK
220388 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
220389 ){
220390 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
220391 int nCopy = 0;
220392 while( nCopy<nReq ){
220393 i64 dummy;
220394 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
220395 }
220396 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
220397 a += nCopy;
220398 n -= nCopy;
220399 fts5WriteFlushLeaf(p, pWriter);
220400 }
220401 if( n>0 ){
220402 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
220403 }
220404}
220405
220406/*
220407** Flush any data cached by the writer object to the database. Free any
220408** allocations associated with the writer.
220409*/
220410static void fts5WriteFinish(
220411 Fts5Index *p,
220412 Fts5SegWriter *pWriter, /* Writer object */
220413 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
220414){
220415 int i;
220416 Fts5PageWriter *pLeaf = &pWriter->writer;
220417 if( p->rc==SQLITE_OK ){
220418 assert( pLeaf->pgno>=1 );
220419 if( pLeaf->buf.n>4 ){
220420 fts5WriteFlushLeaf(p, pWriter);
220421 }
220422 *pnLeaf = pLeaf->pgno-1;
220423 if( pLeaf->pgno>1 ){
220424 fts5WriteFlushBtree(p, pWriter);
220425 }
220426 }
220427 fts5BufferFree(&pLeaf->term);
220428 fts5BufferFree(&pLeaf->buf);
220429 fts5BufferFree(&pLeaf->pgidx);
220430 fts5BufferFree(&pWriter->btterm);
220431
220432 for(i=0; i<pWriter->nDlidx; i++){
220433 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
220434 }
220435 sqlite3_free(pWriter->aDlidx);
220436}
220437
220438static void fts5WriteInit(
220439 Fts5Index *p,
220440 Fts5SegWriter *pWriter,
220441 int iSegid
220442){
220443 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
220444
220445 memset(pWriter, 0, sizeof(Fts5SegWriter));
220446 pWriter->iSegid = iSegid;
220447
220448 fts5WriteDlidxGrow(p, pWriter, 1);
220449 pWriter->writer.pgno = 1;
220450 pWriter->bFirstTermInPage = 1;
220451 pWriter->iBtPage = 1;
220452
220453 assert( pWriter->writer.buf.n==0 );
220454 assert( pWriter->writer.pgidx.n==0 );
220455
220456 /* Grow the two buffers to pgsz + padding bytes in size. */
220457 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
220458 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
220459
220460 if( p->pIdxWriter==0 ){
220461 Fts5Config *pConfig = p->pConfig;
220462 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
220463 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
220464 pConfig->zDb, pConfig->zName
220465 ));
220466 }
220467
220468 if( p->rc==SQLITE_OK ){
220469 /* Initialize the 4-byte leaf-page header to 0x00. */
220470 memset(pWriter->writer.buf.p, 0, 4);
220471 pWriter->writer.buf.n = 4;
220472
220473 /* Bind the current output segment id to the index-writer. This is an
220474 ** optimization over binding the same value over and over as rows are
220475 ** inserted into %_idx by the current writer. */
220476 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
220477 }
220478}
220479
220480/*
220481** Iterator pIter was used to iterate through the input segments of on an
220482** incremental merge operation. This function is called if the incremental
220483** merge step has finished but the input has not been completely exhausted.
220484*/
220485static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
220486 int i;
220487 Fts5Buffer buf;
220488 memset(&buf, 0, sizeof(Fts5Buffer));
220489 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
220490 Fts5SegIter *pSeg = &pIter->aSeg[i];
220491 if( pSeg->pSeg==0 ){
220492 /* no-op */
220493 }else if( pSeg->pLeaf==0 ){
220494 /* All keys from this input segment have been transfered to the output.
220495 ** Set both the first and last page-numbers to 0 to indicate that the
220496 ** segment is now empty. */
220497 pSeg->pSeg->pgnoLast = 0;
220498 pSeg->pSeg->pgnoFirst = 0;
220499 }else{
220500 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
220501 i64 iLeafRowid;
220502 Fts5Data *pData;
220503 int iId = pSeg->pSeg->iSegid;
220504 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
220505
220506 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
220507 pData = fts5LeafRead(p, iLeafRowid);
220508 if( pData ){
220509 if( iOff>pData->szLeaf ){
220510 /* This can occur if the pages that the segments occupy overlap - if
220511 ** a single page has been assigned to more than one segment. In
220512 ** this case a prior iteration of this loop may have corrupted the
220513 ** segment currently being trimmed. */
220514 p->rc = FTS5_CORRUPT;
220515 }else{
220516 fts5BufferZero(&buf);
220517 fts5BufferGrow(&p->rc, &buf, pData->nn);
220518 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
220519 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
220520 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
220521 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]);
220522 if( p->rc==SQLITE_OK ){
220523 /* Set the szLeaf field */
220524 fts5PutU16(&buf.p[2], (u16)buf.n);
220525 }
220526
220527 /* Set up the new page-index array */
220528 fts5BufferAppendVarint(&p->rc, &buf, 4);
220529 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
220530 && pSeg->iEndofDoclist<pData->szLeaf
220531 && pSeg->iPgidxOff<=pData->nn
220532 ){
220533 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
220534 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
220535 fts5BufferAppendBlob(&p->rc, &buf,
220536 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
220537 );
220538 }
220539
220540 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
220541 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
220542 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
220543 }
220544 fts5DataRelease(pData);
220545 }
220546 }
220547 }
220548 fts5BufferFree(&buf);
220549}
220550
220551static void fts5MergeChunkCallback(
220552 Fts5Index *p,
220553 void *pCtx,
220554 const u8 *pChunk, int nChunk
220555){
220556 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
220557 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
220558}
220559
220560/*
220561**
220562*/
220563static void fts5IndexMergeLevel(
220564 Fts5Index *p, /* FTS5 backend object */
220565 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
220566 int iLvl, /* Level to read input from */
220567 int *pnRem /* Write up to this many output leaves */
220568){
220569 Fts5Structure *pStruct = *ppStruct;
220570 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
220571 Fts5StructureLevel *pLvlOut;
220572 Fts5Iter *pIter = 0; /* Iterator to read input data */
220573 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
220574 int nInput; /* Number of input segments */
220575 Fts5SegWriter writer; /* Writer object */
220576 Fts5StructureSegment *pSeg; /* Output segment */
220577 Fts5Buffer term;
220578 int bOldest; /* True if the output segment is the oldest */
220579 int eDetail = p->pConfig->eDetail;
220580 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
220581 int bTermWritten = 0; /* True if current term already output */
220582
220583 assert( iLvl<pStruct->nLevel );
220584 assert( pLvl->nMerge<=pLvl->nSeg );
220585
220586 memset(&writer, 0, sizeof(Fts5SegWriter));
220587 memset(&term, 0, sizeof(Fts5Buffer));
220588 if( pLvl->nMerge ){
220589 pLvlOut = &pStruct->aLevel[iLvl+1];
220590 assert( pLvlOut->nSeg>0 );
220591 nInput = pLvl->nMerge;
220592 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
220593
220594 fts5WriteInit(p, &writer, pSeg->iSegid);
220595 writer.writer.pgno = pSeg->pgnoLast+1;
220596 writer.iBtPage = 0;
220597 }else{
220598 int iSegid = fts5AllocateSegid(p, pStruct);
220599
220600 /* Extend the Fts5Structure object as required to ensure the output
220601 ** segment exists. */
220602 if( iLvl==pStruct->nLevel-1 ){
220603 fts5StructureAddLevel(&p->rc, ppStruct);
220604 pStruct = *ppStruct;
220605 }
220606 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
220607 if( p->rc ) return;
220608 pLvl = &pStruct->aLevel[iLvl];
220609 pLvlOut = &pStruct->aLevel[iLvl+1];
220610
220611 fts5WriteInit(p, &writer, iSegid);
220612
220613 /* Add the new segment to the output level */
220614 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
220615 pLvlOut->nSeg++;
220616 pSeg->pgnoFirst = 1;
220617 pSeg->iSegid = iSegid;
220618 pStruct->nSegment++;
220619
220620 /* Read input from all segments in the input level */
220621 nInput = pLvl->nSeg;
220622 }
220623 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
220624
220625 assert( iLvl>=0 );
220626 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
220627 fts5MultiIterEof(p, pIter)==0;
220628 fts5MultiIterNext(p, pIter, 0, 0)
220629 ){
220630 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
220631 int nPos; /* position-list size field value */
220632 int nTerm;
220633 const u8 *pTerm;
220634
220635 pTerm = fts5MultiIterTerm(pIter, &nTerm);
220636 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
220637 if( pnRem && writer.nLeafWritten>nRem ){
220638 break;
220639 }
220640 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
220641 bTermWritten =0;
220642 }
220643
220644 /* Check for key annihilation. */
220645 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
220646
220647 if( p->rc==SQLITE_OK && bTermWritten==0 ){
220648 /* This is a new term. Append a term to the output segment. */
220649 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
220650 bTermWritten = 1;
220651 }
220652
220653 /* Append the rowid to the output */
220654 /* WRITEPOSLISTSIZE */
220655 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
220656
220657 if( eDetail==FTS5_DETAIL_NONE ){
220658 if( pSegIter->bDel ){
220659 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
220660 if( pSegIter->nPos>0 ){
220661 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
220662 }
220663 }
220664 }else{
220665 /* Append the position-list data to the output */
220666 nPos = pSegIter->nPos*2 + pSegIter->bDel;
220667 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
220668 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
220669 }
220670 }
220671
220672 /* Flush the last leaf page to disk. Set the output segment b-tree height
220673 ** and last leaf page number at the same time. */
220674 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
220675
220676 if( fts5MultiIterEof(p, pIter) ){
220677 int i;
220678
220679 /* Remove the redundant segments from the %_data table */
220680 for(i=0; i<nInput; i++){
220681 fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
220682 }
220683
220684 /* Remove the redundant segments from the input level */
220685 if( pLvl->nSeg!=nInput ){
220686 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
220687 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
220688 }
220689 pStruct->nSegment -= nInput;
220690 pLvl->nSeg -= nInput;
220691 pLvl->nMerge = 0;
220692 if( pSeg->pgnoLast==0 ){
220693 pLvlOut->nSeg--;
220694 pStruct->nSegment--;
220695 }
220696 }else{
220697 assert( pSeg->pgnoLast>0 );
220698 fts5TrimSegments(p, pIter);
220699 pLvl->nMerge = nInput;
220700 }
220701
220702 fts5MultiIterFree(pIter);
220703 fts5BufferFree(&term);
220704 if( pnRem ) *pnRem -= writer.nLeafWritten;
220705}
220706
220707/*
220708** Do up to nPg pages of automerge work on the index.
220709**
220710** Return true if any changes were actually made, or false otherwise.
220711*/
220712static int fts5IndexMerge(
220713 Fts5Index *p, /* FTS5 backend object */
220714 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
220715 int nPg, /* Pages of work to do */
220716 int nMin /* Minimum number of segments to merge */
220717){
220718 int nRem = nPg;
220719 int bRet = 0;
220720 Fts5Structure *pStruct = *ppStruct;
220721 while( nRem>0 && p->rc==SQLITE_OK ){
220722 int iLvl; /* To iterate through levels */
220723 int iBestLvl = 0; /* Level offering the most input segments */
220724 int nBest = 0; /* Number of input segments on best level */
220725
220726 /* Set iBestLvl to the level to read input segments from. */
220727 assert( pStruct->nLevel>0 );
220728 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
220729 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
220730 if( pLvl->nMerge ){
220731 if( pLvl->nMerge>nBest ){
220732 iBestLvl = iLvl;
220733 nBest = pLvl->nMerge;
220734 }
220735 break;
220736 }
220737 if( pLvl->nSeg>nBest ){
220738 nBest = pLvl->nSeg;
220739 iBestLvl = iLvl;
220740 }
220741 }
220742
220743 /* If nBest is still 0, then the index must be empty. */
220744#ifdef SQLITE_DEBUG
220745 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
220746 assert( pStruct->aLevel[iLvl].nSeg==0 );
220747 }
220748#endif
220749
220750 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
220751 break;
220752 }
220753 bRet = 1;
220754 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
220755 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
220756 fts5StructurePromote(p, iBestLvl+1, pStruct);
220757 }
220758 }
220759 *ppStruct = pStruct;
220760 return bRet;
220761}
220762
220763/*
220764** A total of nLeaf leaf pages of data has just been flushed to a level-0
220765** segment. This function updates the write-counter accordingly and, if
220766** necessary, performs incremental merge work.
220767**
220768** If an error occurs, set the Fts5Index.rc error code. If an error has
220769** already occurred, this function is a no-op.
220770*/
220771static void fts5IndexAutomerge(
220772 Fts5Index *p, /* FTS5 backend object */
220773 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
220774 int nLeaf /* Number of output leaves just written */
220775){
220776 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){
220777 Fts5Structure *pStruct = *ppStruct;
220778 u64 nWrite; /* Initial value of write-counter */
220779 int nWork; /* Number of work-quanta to perform */
220780 int nRem; /* Number of leaf pages left to write */
220781
220782 /* Update the write-counter. While doing so, set nWork. */
220783 nWrite = pStruct->nWriteCounter;
220784 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
220785 pStruct->nWriteCounter += nLeaf;
220786 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
220787
220788 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
220789 }
220790}
220791
220792static void fts5IndexCrisismerge(
220793 Fts5Index *p, /* FTS5 backend object */
220794 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
220795){
220796 const int nCrisis = p->pConfig->nCrisisMerge;
220797 Fts5Structure *pStruct = *ppStruct;
220798 int iLvl = 0;
220799
220800 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
220801 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
220802 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
220803 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
220804 fts5StructurePromote(p, iLvl+1, pStruct);
220805 iLvl++;
220806 }
220807 *ppStruct = pStruct;
220808}
220809
220810static int fts5IndexReturn(Fts5Index *p){
220811 int rc = p->rc;
220812 p->rc = SQLITE_OK;
220813 return rc;
220814}
220815
220816typedef struct Fts5FlushCtx Fts5FlushCtx;
220817struct Fts5FlushCtx {
220818 Fts5Index *pIdx;
220819 Fts5SegWriter writer;
220820};
220821
220822/*
220823** Buffer aBuf[] contains a list of varints, all small enough to fit
220824** in a 32-bit integer. Return the size of the largest prefix of this
220825** list nMax bytes or less in size.
220826*/
220827static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
220828 int ret;
220829 u32 dummy;
220830 ret = fts5GetVarint32(aBuf, dummy);
220831 if( ret<nMax ){
220832 while( 1 ){
220833 int i = fts5GetVarint32(&aBuf[ret], dummy);
220834 if( (ret + i) > nMax ) break;
220835 ret += i;
220836 }
220837 }
220838 return ret;
220839}
220840
220841/*
220842** Flush the contents of in-memory hash table iHash to a new level-0
220843** segment on disk. Also update the corresponding structure record.
220844**
220845** If an error occurs, set the Fts5Index.rc error code. If an error has
220846** already occurred, this function is a no-op.
220847*/
220848static void fts5FlushOneHash(Fts5Index *p){
220849 Fts5Hash *pHash = p->pHash;
220850 Fts5Structure *pStruct;
220851 int iSegid;
220852 int pgnoLast = 0; /* Last leaf page number in segment */
220853
220854 /* Obtain a reference to the index structure and allocate a new segment-id
220855 ** for the new level-0 segment. */
220856 pStruct = fts5StructureRead(p);
220857 iSegid = fts5AllocateSegid(p, pStruct);
220858 fts5StructureInvalidate(p);
220859
220860 if( iSegid ){
220861 const int pgsz = p->pConfig->pgsz;
220862 int eDetail = p->pConfig->eDetail;
220863 Fts5StructureSegment *pSeg; /* New segment within pStruct */
220864 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
220865 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
220866
220867 Fts5SegWriter writer;
220868 fts5WriteInit(p, &writer, iSegid);
220869
220870 pBuf = &writer.writer.buf;
220871 pPgidx = &writer.writer.pgidx;
220872
220873 /* fts5WriteInit() should have initialized the buffers to (most likely)
220874 ** the maximum space required. */
220875 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
220876 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
220877
220878 /* Begin scanning through hash table entries. This loop runs once for each
220879 ** term/doclist currently stored within the hash table. */
220880 if( p->rc==SQLITE_OK ){
220881 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
220882 }
220883 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
220884 const char *zTerm; /* Buffer containing term */
220885 const u8 *pDoclist; /* Pointer to doclist for this term */
220886 int nDoclist; /* Size of doclist in bytes */
220887
220888 /* Write the term for this entry to disk. */
220889 sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
220890 fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
220891 if( p->rc!=SQLITE_OK ) break;
220892
220893 assert( writer.bFirstRowidInPage==0 );
220894 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
220895 /* The entire doclist will fit on the current leaf. */
220896 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
220897 }else{
220898 i64 iRowid = 0;
220899 i64 iDelta = 0;
220900 int iOff = 0;
220901
220902 /* The entire doclist will not fit on this leaf. The following
220903 ** loop iterates through the poslists that make up the current
220904 ** doclist. */
220905 while( p->rc==SQLITE_OK && iOff<nDoclist ){
220906 iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
220907 iRowid += iDelta;
220908
220909 if( writer.bFirstRowidInPage ){
220910 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
220911 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
220912 writer.bFirstRowidInPage = 0;
220913 fts5WriteDlidxAppend(p, &writer, iRowid);
220914 if( p->rc!=SQLITE_OK ) break;
220915 }else{
220916 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
220917 }
220918 assert( pBuf->n<=pBuf->nSpace );
220919
220920 if( eDetail==FTS5_DETAIL_NONE ){
220921 if( iOff<nDoclist && pDoclist[iOff]==0 ){
220922 pBuf->p[pBuf->n++] = 0;
220923 iOff++;
220924 if( iOff<nDoclist && pDoclist[iOff]==0 ){
220925 pBuf->p[pBuf->n++] = 0;
220926 iOff++;
220927 }
220928 }
220929 if( (pBuf->n + pPgidx->n)>=pgsz ){
220930 fts5WriteFlushLeaf(p, &writer);
220931 }
220932 }else{
220933 int bDummy;
220934 int nPos;
220935 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
220936 nCopy += nPos;
220937 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
220938 /* The entire poslist will fit on the current leaf. So copy
220939 ** it in one go. */
220940 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
220941 }else{
220942 /* The entire poslist will not fit on this leaf. So it needs
220943 ** to be broken into sections. The only qualification being
220944 ** that each varint must be stored contiguously. */
220945 const u8 *pPoslist = &pDoclist[iOff];
220946 int iPos = 0;
220947 while( p->rc==SQLITE_OK ){
220948 int nSpace = pgsz - pBuf->n - pPgidx->n;
220949 int n = 0;
220950 if( (nCopy - iPos)<=nSpace ){
220951 n = nCopy - iPos;
220952 }else{
220953 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
220954 }
220955 assert( n>0 );
220956 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
220957 iPos += n;
220958 if( (pBuf->n + pPgidx->n)>=pgsz ){
220959 fts5WriteFlushLeaf(p, &writer);
220960 }
220961 if( iPos>=nCopy ) break;
220962 }
220963 }
220964 iOff += nCopy;
220965 }
220966 }
220967 }
220968
220969 /* TODO2: Doclist terminator written here. */
220970 /* pBuf->p[pBuf->n++] = '\0'; */
220971 assert( pBuf->n<=pBuf->nSpace );
220972 if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(pHash);
220973 }
220974 sqlite3Fts5HashClear(pHash);
220975 fts5WriteFinish(p, &writer, &pgnoLast);
220976
220977 /* Update the Fts5Structure. It is written back to the database by the
220978 ** fts5StructureRelease() call below. */
220979 if( pStruct->nLevel==0 ){
220980 fts5StructureAddLevel(&p->rc, &pStruct);
220981 }
220982 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
220983 if( p->rc==SQLITE_OK ){
220984 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
220985 pSeg->iSegid = iSegid;
220986 pSeg->pgnoFirst = 1;
220987 pSeg->pgnoLast = pgnoLast;
220988 pStruct->nSegment++;
220989 }
220990 fts5StructurePromote(p, 0, pStruct);
220991 }
220992
220993 fts5IndexAutomerge(p, &pStruct, pgnoLast);
220994 fts5IndexCrisismerge(p, &pStruct);
220995 fts5StructureWrite(p, pStruct);
220996 fts5StructureRelease(pStruct);
220997}
220998
220999/*
221000** Flush any data stored in the in-memory hash tables to the database.
221001*/
221002static void fts5IndexFlush(Fts5Index *p){
221003 /* Unless it is empty, flush the hash table to disk */
221004 if( p->nPendingData ){
221005 assert( p->pHash );
221006 p->nPendingData = 0;
221007 fts5FlushOneHash(p);
221008 }
221009}
221010
221011static Fts5Structure *fts5IndexOptimizeStruct(
221012 Fts5Index *p,
221013 Fts5Structure *pStruct
221014){
221015 Fts5Structure *pNew = 0;
221016 sqlite3_int64 nByte = sizeof(Fts5Structure);
221017 int nSeg = pStruct->nSegment;
221018 int i;
221019
221020 /* Figure out if this structure requires optimization. A structure does
221021 ** not require optimization if either:
221022 **
221023 ** + it consists of fewer than two segments, or
221024 ** + all segments are on the same level, or
221025 ** + all segments except one are currently inputs to a merge operation.
221026 **
221027 ** In the first case, return NULL. In the second, increment the ref-count
221028 ** on *pStruct and return a copy of the pointer to it.
221029 */
221030 if( nSeg<2 ) return 0;
221031 for(i=0; i<pStruct->nLevel; i++){
221032 int nThis = pStruct->aLevel[i].nSeg;
221033 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
221034 fts5StructureRef(pStruct);
221035 return pStruct;
221036 }
221037 assert( pStruct->aLevel[i].nMerge<=nThis );
221038 }
221039
221040 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
221041 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
221042
221043 if( pNew ){
221044 Fts5StructureLevel *pLvl;
221045 nByte = nSeg * sizeof(Fts5StructureSegment);
221046 pNew->nLevel = pStruct->nLevel+1;
221047 pNew->nRef = 1;
221048 pNew->nWriteCounter = pStruct->nWriteCounter;
221049 pLvl = &pNew->aLevel[pStruct->nLevel];
221050 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
221051 if( pLvl->aSeg ){
221052 int iLvl, iSeg;
221053 int iSegOut = 0;
221054 /* Iterate through all segments, from oldest to newest. Add them to
221055 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
221056 ** segment in the data structure. */
221057 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
221058 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
221059 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
221060 iSegOut++;
221061 }
221062 }
221063 pNew->nSegment = pLvl->nSeg = nSeg;
221064 }else{
221065 sqlite3_free(pNew);
221066 pNew = 0;
221067 }
221068 }
221069
221070 return pNew;
221071}
221072
221073static int sqlite3Fts5IndexOptimize(Fts5Index *p){
221074 Fts5Structure *pStruct;
221075 Fts5Structure *pNew = 0;
221076
221077 assert( p->rc==SQLITE_OK );
221078 fts5IndexFlush(p);
221079 pStruct = fts5StructureRead(p);
221080 fts5StructureInvalidate(p);
221081
221082 if( pStruct ){
221083 pNew = fts5IndexOptimizeStruct(p, pStruct);
221084 }
221085 fts5StructureRelease(pStruct);
221086
221087 assert( pNew==0 || pNew->nSegment>0 );
221088 if( pNew ){
221089 int iLvl;
221090 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
221091 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
221092 int nRem = FTS5_OPT_WORK_UNIT;
221093 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
221094 }
221095
221096 fts5StructureWrite(p, pNew);
221097 fts5StructureRelease(pNew);
221098 }
221099
221100 return fts5IndexReturn(p);
221101}
221102
221103/*
221104** This is called to implement the special "VALUES('merge', $nMerge)"
221105** INSERT command.
221106*/
221107static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
221108 Fts5Structure *pStruct = fts5StructureRead(p);
221109 if( pStruct ){
221110 int nMin = p->pConfig->nUsermerge;
221111 fts5StructureInvalidate(p);
221112 if( nMerge<0 ){
221113 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
221114 fts5StructureRelease(pStruct);
221115 pStruct = pNew;
221116 nMin = 2;
221117 nMerge = nMerge*-1;
221118 }
221119 if( pStruct && pStruct->nLevel ){
221120 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
221121 fts5StructureWrite(p, pStruct);
221122 }
221123 }
221124 fts5StructureRelease(pStruct);
221125 }
221126 return fts5IndexReturn(p);
221127}
221128
221129static void fts5AppendRowid(
221130 Fts5Index *p,
221131 i64 iDelta,
221132 Fts5Iter *pUnused,
221133 Fts5Buffer *pBuf
221134){
221135 UNUSED_PARAM(pUnused);
221136 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
221137}
221138
221139static void fts5AppendPoslist(
221140 Fts5Index *p,
221141 i64 iDelta,
221142 Fts5Iter *pMulti,
221143 Fts5Buffer *pBuf
221144){
221145 int nData = pMulti->base.nData;
221146 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
221147 assert( nData>0 );
221148 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
221149 fts5BufferSafeAppendVarint(pBuf, iDelta);
221150 fts5BufferSafeAppendVarint(pBuf, nData*2);
221151 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
221152 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING);
221153 }
221154}
221155
221156
221157static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
221158 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
221159
221160 assert( pIter->aPoslist );
221161 if( p>=pIter->aEof ){
221162 pIter->aPoslist = 0;
221163 }else{
221164 i64 iDelta;
221165
221166 p += fts5GetVarint(p, (u64*)&iDelta);
221167 pIter->iRowid += iDelta;
221168
221169 /* Read position list size */
221170 if( p[0] & 0x80 ){
221171 int nPos;
221172 pIter->nSize = fts5GetVarint32(p, nPos);
221173 pIter->nPoslist = (nPos>>1);
221174 }else{
221175 pIter->nPoslist = ((int)(p[0])) >> 1;
221176 pIter->nSize = 1;
221177 }
221178
221179 pIter->aPoslist = p;
221180 }
221181}
221182
221183static void fts5DoclistIterInit(
221184 Fts5Buffer *pBuf,
221185 Fts5DoclistIter *pIter
221186){
221187 memset(pIter, 0, sizeof(*pIter));
221188 pIter->aPoslist = pBuf->p;
221189 pIter->aEof = &pBuf->p[pBuf->n];
221190 fts5DoclistIterNext(pIter);
221191}
221192
221193#if 0
221194/*
221195** Append a doclist to buffer pBuf.
221196**
221197** This function assumes that space within the buffer has already been
221198** allocated.
221199*/
221200static void fts5MergeAppendDocid(
221201 Fts5Buffer *pBuf, /* Buffer to write to */
221202 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
221203 i64 iRowid /* Rowid to append */
221204){
221205 assert( pBuf->n!=0 || (*piLastRowid)==0 );
221206 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
221207 *piLastRowid = iRowid;
221208}
221209#endif
221210
221211#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
221212 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
221213 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
221214 (iLastRowid) = (iRowid); \
221215}
221216
221217/*
221218** Swap the contents of buffer *p1 with that of *p2.
221219*/
221220static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
221221 Fts5Buffer tmp = *p1;
221222 *p1 = *p2;
221223 *p2 = tmp;
221224}
221225
221226static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
221227 int i = *piOff;
221228 if( i>=pBuf->n ){
221229 *piOff = -1;
221230 }else{
221231 u64 iVal;
221232 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
221233 *piRowid += iVal;
221234 }
221235}
221236
221237/*
221238** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
221239** In this case the buffers consist of a delta-encoded list of rowids only.
221240*/
221241static void fts5MergeRowidLists(
221242 Fts5Index *p, /* FTS5 backend object */
221243 Fts5Buffer *p1, /* First list to merge */
221244 Fts5Buffer *p2 /* Second list to merge */
221245){
221246 int i1 = 0;
221247 int i2 = 0;
221248 i64 iRowid1 = 0;
221249 i64 iRowid2 = 0;
221250 i64 iOut = 0;
221251
221252 Fts5Buffer out;
221253 memset(&out, 0, sizeof(out));
221254 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
221255 if( p->rc ) return;
221256
221257 fts5NextRowid(p1, &i1, &iRowid1);
221258 fts5NextRowid(p2, &i2, &iRowid2);
221259 while( i1>=0 || i2>=0 ){
221260 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
221261 assert( iOut==0 || iRowid1>iOut );
221262 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
221263 iOut = iRowid1;
221264 fts5NextRowid(p1, &i1, &iRowid1);
221265 }else{
221266 assert( iOut==0 || iRowid2>iOut );
221267 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
221268 iOut = iRowid2;
221269 if( i1>=0 && iRowid1==iRowid2 ){
221270 fts5NextRowid(p1, &i1, &iRowid1);
221271 }
221272 fts5NextRowid(p2, &i2, &iRowid2);
221273 }
221274 }
221275
221276 fts5BufferSwap(&out, p1);
221277 fts5BufferFree(&out);
221278}
221279
221280/*
221281** Buffers p1 and p2 contain doclists. This function merges the content
221282** of the two doclists together and sets buffer p1 to the result before
221283** returning.
221284**
221285** If an error occurs, an error code is left in p->rc. If an error has
221286** already occurred, this function is a no-op.
221287*/
221288static void fts5MergePrefixLists(
221289 Fts5Index *p, /* FTS5 backend object */
221290 Fts5Buffer *p1, /* First list to merge */
221291 Fts5Buffer *p2 /* Second list to merge */
221292){
221293 if( p2->n ){
221294 i64 iLastRowid = 0;
221295 Fts5DoclistIter i1;
221296 Fts5DoclistIter i2;
221297 Fts5Buffer out = {0, 0, 0};
221298 Fts5Buffer tmp = {0, 0, 0};
221299
221300 /* The maximum size of the output is equal to the sum of the two
221301 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
221302 ** first rowid in one input is a large negative number, and the first in
221303 ** the other a non-negative number, the delta for the non-negative
221304 ** number will be larger on disk than the literal integer value
221305 ** was.
221306 **
221307 ** Or, if the input position-lists are corrupt, then the output might
221308 ** include up to 2 extra 10-byte positions created by interpreting -1
221309 ** (the value PoslistNext64() uses for EOF) as a position and appending
221310 ** it to the output. This can happen at most once for each input
221311 ** position-list, hence two 10 byte paddings. */
221312 if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9+10+10) ) return;
221313 fts5DoclistIterInit(p1, &i1);
221314 fts5DoclistIterInit(p2, &i2);
221315
221316 while( 1 ){
221317 if( i1.iRowid<i2.iRowid ){
221318 /* Copy entry from i1 */
221319 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
221320 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
221321 fts5DoclistIterNext(&i1);
221322 if( i1.aPoslist==0 ) break;
221323 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
221324 }
221325 else if( i2.iRowid!=i1.iRowid ){
221326 /* Copy entry from i2 */
221327 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
221328 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
221329 fts5DoclistIterNext(&i2);
221330 if( i2.aPoslist==0 ) break;
221331 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
221332 }
221333 else{
221334 /* Merge the two position lists. */
221335 i64 iPos1 = 0;
221336 i64 iPos2 = 0;
221337 int iOff1 = 0;
221338 int iOff2 = 0;
221339 u8 *a1 = &i1.aPoslist[i1.nSize];
221340 u8 *a2 = &i2.aPoslist[i2.nSize];
221341 int nCopy;
221342 u8 *aCopy;
221343
221344 i64 iPrev = 0;
221345 Fts5PoslistWriter writer;
221346 memset(&writer, 0, sizeof(writer));
221347
221348 /* See the earlier comment in this function for an explanation of why
221349 ** corrupt input position lists might cause the output to consume
221350 ** at most 20 bytes of unexpected space. */
221351 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
221352 fts5BufferZero(&tmp);
221353 sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist + 10 + 10);
221354 if( p->rc ) break;
221355
221356 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
221357 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
221358 assert_nc( iPos1>=0 && iPos2>=0 );
221359
221360 if( iPos1<iPos2 ){
221361 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
221362 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
221363 }else{
221364 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
221365 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
221366 }
221367 if( iPos1>=0 && iPos2>=0 ){
221368 while( 1 ){
221369 if( iPos1<iPos2 ){
221370 if( iPos1!=iPrev ){
221371 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
221372 }
221373 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
221374 if( iPos1<0 ) break;
221375 }else{
221376 assert_nc( iPos2!=iPrev );
221377 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
221378 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
221379 if( iPos2<0 ) break;
221380 }
221381 }
221382 }
221383
221384 if( iPos1>=0 ){
221385 if( iPos1!=iPrev ){
221386 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
221387 }
221388 aCopy = &a1[iOff1];
221389 nCopy = i1.nPoslist - iOff1;
221390 }else{
221391 assert_nc( iPos2>=0 && iPos2!=iPrev );
221392 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
221393 aCopy = &a2[iOff2];
221394 nCopy = i2.nPoslist - iOff2;
221395 }
221396 if( nCopy>0 ){
221397 fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
221398 }
221399
221400 /* WRITEPOSLISTSIZE */
221401 assert_nc( tmp.n<=i1.nPoslist+i2.nPoslist );
221402 assert( tmp.n<=i1.nPoslist+i2.nPoslist+10+10 );
221403 if( tmp.n>i1.nPoslist+i2.nPoslist ){
221404 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
221405 break;
221406 }
221407 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
221408 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
221409 fts5DoclistIterNext(&i1);
221410 fts5DoclistIterNext(&i2);
221411 assert_nc( out.n<=(p1->n+p2->n+9) );
221412 if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
221413 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
221414 }
221415 }
221416
221417 if( i1.aPoslist ){
221418 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
221419 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
221420 }
221421 else if( i2.aPoslist ){
221422 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
221423 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
221424 }
221425 assert_nc( out.n<=(p1->n+p2->n+9) );
221426
221427 fts5BufferSet(&p->rc, p1, out.n, out.p);
221428 fts5BufferFree(&tmp);
221429 fts5BufferFree(&out);
221430 }
221431}
221432
221433static void fts5SetupPrefixIter(
221434 Fts5Index *p, /* Index to read from */
221435 int bDesc, /* True for "ORDER BY rowid DESC" */
221436 const u8 *pToken, /* Buffer containing prefix to match */
221437 int nToken, /* Size of buffer pToken in bytes */
221438 Fts5Colset *pColset, /* Restrict matches to these columns */
221439 Fts5Iter **ppIter /* OUT: New iterator */
221440){
221441 Fts5Structure *pStruct;
221442 Fts5Buffer *aBuf;
221443 const int nBuf = 32;
221444
221445 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
221446 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
221447 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
221448 xMerge = fts5MergeRowidLists;
221449 xAppend = fts5AppendRowid;
221450 }else{
221451 xMerge = fts5MergePrefixLists;
221452 xAppend = fts5AppendPoslist;
221453 }
221454
221455 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
221456 pStruct = fts5StructureRead(p);
221457
221458 if( aBuf && pStruct ){
221459 const int flags = FTS5INDEX_QUERY_SCAN
221460 | FTS5INDEX_QUERY_SKIPEMPTY
221461 | FTS5INDEX_QUERY_NOOUTPUT;
221462 int i;
221463 i64 iLastRowid = 0;
221464 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
221465 Fts5Data *pData;
221466 Fts5Buffer doclist;
221467 int bNewTerm = 1;
221468
221469 memset(&doclist, 0, sizeof(doclist));
221470 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
221471 fts5IterSetOutputCb(&p->rc, p1);
221472 for( /* no-op */ ;
221473 fts5MultiIterEof(p, p1)==0;
221474 fts5MultiIterNext2(p, p1, &bNewTerm)
221475 ){
221476 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
221477 int nTerm = pSeg->term.n;
221478 const u8 *pTerm = pSeg->term.p;
221479 p1->xSetOutputs(p1, pSeg);
221480
221481 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
221482 if( bNewTerm ){
221483 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
221484 }
221485
221486 if( p1->base.nData==0 ) continue;
221487
221488 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
221489 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
221490 assert( i<nBuf );
221491 if( aBuf[i].n==0 ){
221492 fts5BufferSwap(&doclist, &aBuf[i]);
221493 fts5BufferZero(&doclist);
221494 }else{
221495 xMerge(p, &doclist, &aBuf[i]);
221496 fts5BufferZero(&aBuf[i]);
221497 }
221498 }
221499 iLastRowid = 0;
221500 }
221501
221502 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
221503 iLastRowid = p1->base.iRowid;
221504 }
221505
221506 for(i=0; i<nBuf; i++){
221507 if( p->rc==SQLITE_OK ){
221508 xMerge(p, &doclist, &aBuf[i]);
221509 }
221510 fts5BufferFree(&aBuf[i]);
221511 }
221512 fts5MultiIterFree(p1);
221513
221514 pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
221515 if( pData ){
221516 pData->p = (u8*)&pData[1];
221517 pData->nn = pData->szLeaf = doclist.n;
221518 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
221519 fts5MultiIterNew2(p, pData, bDesc, ppIter);
221520 }
221521 fts5BufferFree(&doclist);
221522 }
221523
221524 fts5StructureRelease(pStruct);
221525 sqlite3_free(aBuf);
221526}
221527
221528
221529/*
221530** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
221531** to the document with rowid iRowid.
221532*/
221533static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
221534 assert( p->rc==SQLITE_OK );
221535
221536 /* Allocate the hash table if it has not already been allocated */
221537 if( p->pHash==0 ){
221538 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
221539 }
221540
221541 /* Flush the hash table to disk if required */
221542 if( iRowid<p->iWriteRowid
221543 || (iRowid==p->iWriteRowid && p->bDelete==0)
221544 || (p->nPendingData > p->pConfig->nHashSize)
221545 ){
221546 fts5IndexFlush(p);
221547 }
221548
221549 p->iWriteRowid = iRowid;
221550 p->bDelete = bDelete;
221551 return fts5IndexReturn(p);
221552}
221553
221554/*
221555** Commit data to disk.
221556*/
221557static int sqlite3Fts5IndexSync(Fts5Index *p){
221558 assert( p->rc==SQLITE_OK );
221559 fts5IndexFlush(p);
221560 sqlite3Fts5IndexCloseReader(p);
221561 return fts5IndexReturn(p);
221562}
221563
221564/*
221565** Discard any data stored in the in-memory hash tables. Do not write it
221566** to the database. Additionally, assume that the contents of the %_data
221567** table may have changed on disk. So any in-memory caches of %_data
221568** records must be invalidated.
221569*/
221570static int sqlite3Fts5IndexRollback(Fts5Index *p){
221571 sqlite3Fts5IndexCloseReader(p);
221572 fts5IndexDiscardData(p);
221573 fts5StructureInvalidate(p);
221574 /* assert( p->rc==SQLITE_OK ); */
221575 return SQLITE_OK;
221576}
221577
221578/*
221579** The %_data table is completely empty when this function is called. This
221580** function populates it with the initial structure objects for each index,
221581** and the initial version of the "averages" record (a zero-byte blob).
221582*/
221583static int sqlite3Fts5IndexReinit(Fts5Index *p){
221584 Fts5Structure s;
221585 fts5StructureInvalidate(p);
221586 fts5IndexDiscardData(p);
221587 memset(&s, 0, sizeof(Fts5Structure));
221588 fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
221589 fts5StructureWrite(p, &s);
221590 return fts5IndexReturn(p);
221591}
221592
221593/*
221594** Open a new Fts5Index handle. If the bCreate argument is true, create
221595** and initialize the underlying %_data table.
221596**
221597** If successful, set *pp to point to the new object and return SQLITE_OK.
221598** Otherwise, set *pp to NULL and return an SQLite error code.
221599*/
221600static int sqlite3Fts5IndexOpen(
221601 Fts5Config *pConfig,
221602 int bCreate,
221603 Fts5Index **pp,
221604 char **pzErr
221605){
221606 int rc = SQLITE_OK;
221607 Fts5Index *p; /* New object */
221608
221609 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
221610 if( rc==SQLITE_OK ){
221611 p->pConfig = pConfig;
221612 p->nWorkUnit = FTS5_WORK_UNIT;
221613 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
221614 if( p->zDataTbl && bCreate ){
221615 rc = sqlite3Fts5CreateTable(
221616 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
221617 );
221618 if( rc==SQLITE_OK ){
221619 rc = sqlite3Fts5CreateTable(pConfig, "idx",
221620 "segid, term, pgno, PRIMARY KEY(segid, term)",
221621 1, pzErr
221622 );
221623 }
221624 if( rc==SQLITE_OK ){
221625 rc = sqlite3Fts5IndexReinit(p);
221626 }
221627 }
221628 }
221629
221630 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
221631 if( rc ){
221632 sqlite3Fts5IndexClose(p);
221633 *pp = 0;
221634 }
221635 return rc;
221636}
221637
221638/*
221639** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
221640*/
221641static int sqlite3Fts5IndexClose(Fts5Index *p){
221642 int rc = SQLITE_OK;
221643 if( p ){
221644 assert( p->pReader==0 );
221645 fts5StructureInvalidate(p);
221646 sqlite3_finalize(p->pWriter);
221647 sqlite3_finalize(p->pDeleter);
221648 sqlite3_finalize(p->pIdxWriter);
221649 sqlite3_finalize(p->pIdxDeleter);
221650 sqlite3_finalize(p->pIdxSelect);
221651 sqlite3_finalize(p->pDataVersion);
221652 sqlite3Fts5HashFree(p->pHash);
221653 sqlite3_free(p->zDataTbl);
221654 sqlite3_free(p);
221655 }
221656 return rc;
221657}
221658
221659/*
221660** Argument p points to a buffer containing utf-8 text that is n bytes in
221661** size. Return the number of bytes in the nChar character prefix of the
221662** buffer, or 0 if there are less than nChar characters in total.
221663*/
221664static int sqlite3Fts5IndexCharlenToBytelen(
221665 const char *p,
221666 int nByte,
221667 int nChar
221668){
221669 int n = 0;
221670 int i;
221671 for(i=0; i<nChar; i++){
221672 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
221673 if( (unsigned char)p[n++]>=0xc0 ){
221674 if( n>=nByte ) return 0;
221675 while( (p[n] & 0xc0)==0x80 ){
221676 n++;
221677 if( n>=nByte ){
221678 if( i+1==nChar ) break;
221679 return 0;
221680 }
221681 }
221682 }
221683 }
221684 return n;
221685}
221686
221687/*
221688** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
221689** unicode characters in the string.
221690*/
221691static int fts5IndexCharlen(const char *pIn, int nIn){
221692 int nChar = 0;
221693 int i = 0;
221694 while( i<nIn ){
221695 if( (unsigned char)pIn[i++]>=0xc0 ){
221696 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
221697 }
221698 nChar++;
221699 }
221700 return nChar;
221701}
221702
221703/*
221704** Insert or remove data to or from the index. Each time a document is
221705** added to or removed from the index, this function is called one or more
221706** times.
221707**
221708** For an insert, it must be called once for each token in the new document.
221709** If the operation is a delete, it must be called (at least) once for each
221710** unique token in the document with an iCol value less than zero. The iPos
221711** argument is ignored for a delete.
221712*/
221713static int sqlite3Fts5IndexWrite(
221714 Fts5Index *p, /* Index to write to */
221715 int iCol, /* Column token appears in (-ve -> delete) */
221716 int iPos, /* Position of token within column */
221717 const char *pToken, int nToken /* Token to add or remove to or from index */
221718){
221719 int i; /* Used to iterate through indexes */
221720 int rc = SQLITE_OK; /* Return code */
221721 Fts5Config *pConfig = p->pConfig;
221722
221723 assert( p->rc==SQLITE_OK );
221724 assert( (iCol<0)==p->bDelete );
221725
221726 /* Add the entry to the main terms index. */
221727 rc = sqlite3Fts5HashWrite(
221728 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
221729 );
221730
221731 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
221732 const int nChar = pConfig->aPrefix[i];
221733 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
221734 if( nByte ){
221735 rc = sqlite3Fts5HashWrite(p->pHash,
221736 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
221737 nByte
221738 );
221739 }
221740 }
221741
221742 return rc;
221743}
221744
221745/*
221746** Open a new iterator to iterate though all rowid that match the
221747** specified token or token prefix.
221748*/
221749static int sqlite3Fts5IndexQuery(
221750 Fts5Index *p, /* FTS index to query */
221751 const char *pToken, int nToken, /* Token (or prefix) to query for */
221752 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
221753 Fts5Colset *pColset, /* Match these columns only */
221754 Fts5IndexIter **ppIter /* OUT: New iterator object */
221755){
221756 Fts5Config *pConfig = p->pConfig;
221757 Fts5Iter *pRet = 0;
221758 Fts5Buffer buf = {0, 0, 0};
221759
221760 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
221761 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
221762
221763 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
221764 int iIdx = 0; /* Index to search */
221765 if( nToken ) memcpy(&buf.p[1], pToken, nToken);
221766
221767 /* Figure out which index to search and set iIdx accordingly. If this
221768 ** is a prefix query for which there is no prefix index, set iIdx to
221769 ** greater than pConfig->nPrefix to indicate that the query will be
221770 ** satisfied by scanning multiple terms in the main index.
221771 **
221772 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
221773 ** prefix-query. Instead of using a prefix-index (if one exists),
221774 ** evaluate the prefix query using the main FTS index. This is used
221775 ** for internal sanity checking by the integrity-check in debug
221776 ** mode only. */
221777#ifdef SQLITE_DEBUG
221778 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
221779 assert( flags & FTS5INDEX_QUERY_PREFIX );
221780 iIdx = 1+pConfig->nPrefix;
221781 }else
221782#endif
221783 if( flags & FTS5INDEX_QUERY_PREFIX ){
221784 int nChar = fts5IndexCharlen(pToken, nToken);
221785 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
221786 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
221787 }
221788 }
221789
221790 if( iIdx<=pConfig->nPrefix ){
221791 /* Straight index lookup */
221792 Fts5Structure *pStruct = fts5StructureRead(p);
221793 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
221794 if( pStruct ){
221795 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
221796 pColset, buf.p, nToken+1, -1, 0, &pRet
221797 );
221798 fts5StructureRelease(pStruct);
221799 }
221800 }else{
221801 /* Scan multiple terms in the main index */
221802 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
221803 buf.p[0] = FTS5_MAIN_PREFIX;
221804 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
221805 assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
221806 fts5IterSetOutputCb(&p->rc, pRet);
221807 if( p->rc==SQLITE_OK ){
221808 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
221809 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
221810 }
221811 }
221812
221813 if( p->rc ){
221814 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
221815 pRet = 0;
221816 sqlite3Fts5IndexCloseReader(p);
221817 }
221818
221819 *ppIter = (Fts5IndexIter*)pRet;
221820 sqlite3Fts5BufferFree(&buf);
221821 }
221822 return fts5IndexReturn(p);
221823}
221824
221825/*
221826** Return true if the iterator passed as the only argument is at EOF.
221827*/
221828/*
221829** Move to the next matching rowid.
221830*/
221831static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
221832 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221833 assert( pIter->pIndex->rc==SQLITE_OK );
221834 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
221835 return fts5IndexReturn(pIter->pIndex);
221836}
221837
221838/*
221839** Move to the next matching term/rowid. Used by the fts5vocab module.
221840*/
221841static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
221842 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221843 Fts5Index *p = pIter->pIndex;
221844
221845 assert( pIter->pIndex->rc==SQLITE_OK );
221846
221847 fts5MultiIterNext(p, pIter, 0, 0);
221848 if( p->rc==SQLITE_OK ){
221849 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
221850 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
221851 fts5DataRelease(pSeg->pLeaf);
221852 pSeg->pLeaf = 0;
221853 pIter->base.bEof = 1;
221854 }
221855 }
221856
221857 return fts5IndexReturn(pIter->pIndex);
221858}
221859
221860/*
221861** Move to the next matching rowid that occurs at or after iMatch. The
221862** definition of "at or after" depends on whether this iterator iterates
221863** in ascending or descending rowid order.
221864*/
221865static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
221866 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221867 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
221868 return fts5IndexReturn(pIter->pIndex);
221869}
221870
221871/*
221872** Return the current term.
221873*/
221874static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
221875 int n;
221876 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
221877 *pn = n-1;
221878 return &z[1];
221879}
221880
221881/*
221882** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
221883*/
221884static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
221885 if( pIndexIter ){
221886 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
221887 Fts5Index *pIndex = pIter->pIndex;
221888 fts5MultiIterFree(pIter);
221889 sqlite3Fts5IndexCloseReader(pIndex);
221890 }
221891}
221892
221893/*
221894** Read and decode the "averages" record from the database.
221895**
221896** Parameter anSize must point to an array of size nCol, where nCol is
221897** the number of user defined columns in the FTS table.
221898*/
221899static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
221900 int nCol = p->pConfig->nCol;
221901 Fts5Data *pData;
221902
221903 *pnRow = 0;
221904 memset(anSize, 0, sizeof(i64) * nCol);
221905 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
221906 if( p->rc==SQLITE_OK && pData->nn ){
221907 int i = 0;
221908 int iCol;
221909 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
221910 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
221911 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
221912 }
221913 }
221914
221915 fts5DataRelease(pData);
221916 return fts5IndexReturn(p);
221917}
221918
221919/*
221920** Replace the current "averages" record with the contents of the buffer
221921** supplied as the second argument.
221922*/
221923static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
221924 assert( p->rc==SQLITE_OK );
221925 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
221926 return fts5IndexReturn(p);
221927}
221928
221929/*
221930** Return the total number of blocks this module has read from the %_data
221931** table since it was created.
221932*/
221933static int sqlite3Fts5IndexReads(Fts5Index *p){
221934 return p->nRead;
221935}
221936
221937/*
221938** Set the 32-bit cookie value stored at the start of all structure
221939** records to the value passed as the second argument.
221940**
221941** Return SQLITE_OK if successful, or an SQLite error code if an error
221942** occurs.
221943*/
221944static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
221945 int rc; /* Return code */
221946 Fts5Config *pConfig = p->pConfig; /* Configuration object */
221947 u8 aCookie[4]; /* Binary representation of iNew */
221948 sqlite3_blob *pBlob = 0;
221949
221950 assert( p->rc==SQLITE_OK );
221951 sqlite3Fts5Put32(aCookie, iNew);
221952
221953 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
221954 "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
221955 );
221956 if( rc==SQLITE_OK ){
221957 sqlite3_blob_write(pBlob, aCookie, 4, 0);
221958 rc = sqlite3_blob_close(pBlob);
221959 }
221960
221961 return rc;
221962}
221963
221964static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
221965 Fts5Structure *pStruct;
221966 pStruct = fts5StructureRead(p);
221967 fts5StructureRelease(pStruct);
221968 return fts5IndexReturn(p);
221969}
221970
221971
221972/*************************************************************************
221973**************************************************************************
221974** Below this point is the implementation of the integrity-check
221975** functionality.
221976*/
221977
221978/*
221979** Return a simple checksum value based on the arguments.
221980*/
221981static u64 sqlite3Fts5IndexEntryCksum(
221982 i64 iRowid,
221983 int iCol,
221984 int iPos,
221985 int iIdx,
221986 const char *pTerm,
221987 int nTerm
221988){
221989 int i;
221990 u64 ret = iRowid;
221991 ret += (ret<<3) + iCol;
221992 ret += (ret<<3) + iPos;
221993 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
221994 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
221995 return ret;
221996}
221997
221998#ifdef SQLITE_DEBUG
221999/*
222000** This function is purely an internal test. It does not contribute to
222001** FTS functionality, or even the integrity-check, in any way.
222002**
222003** Instead, it tests that the same set of pgno/rowid combinations are
222004** visited regardless of whether the doclist-index identified by parameters
222005** iSegid/iLeaf is iterated in forwards or reverse order.
222006*/
222007static void fts5TestDlidxReverse(
222008 Fts5Index *p,
222009 int iSegid, /* Segment id to load from */
222010 int iLeaf /* Load doclist-index for this leaf */
222011){
222012 Fts5DlidxIter *pDlidx = 0;
222013 u64 cksum1 = 13;
222014 u64 cksum2 = 13;
222015
222016 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
222017 fts5DlidxIterEof(p, pDlidx)==0;
222018 fts5DlidxIterNext(p, pDlidx)
222019 ){
222020 i64 iRowid = fts5DlidxIterRowid(pDlidx);
222021 int pgno = fts5DlidxIterPgno(pDlidx);
222022 assert( pgno>iLeaf );
222023 cksum1 += iRowid + ((i64)pgno<<32);
222024 }
222025 fts5DlidxIterFree(pDlidx);
222026 pDlidx = 0;
222027
222028 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
222029 fts5DlidxIterEof(p, pDlidx)==0;
222030 fts5DlidxIterPrev(p, pDlidx)
222031 ){
222032 i64 iRowid = fts5DlidxIterRowid(pDlidx);
222033 int pgno = fts5DlidxIterPgno(pDlidx);
222034 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
222035 cksum2 += iRowid + ((i64)pgno<<32);
222036 }
222037 fts5DlidxIterFree(pDlidx);
222038 pDlidx = 0;
222039
222040 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
222041}
222042
222043static int fts5QueryCksum(
222044 Fts5Index *p, /* Fts5 index object */
222045 int iIdx,
222046 const char *z, /* Index key to query for */
222047 int n, /* Size of index key in bytes */
222048 int flags, /* Flags for Fts5IndexQuery */
222049 u64 *pCksum /* IN/OUT: Checksum value */
222050){
222051 int eDetail = p->pConfig->eDetail;
222052 u64 cksum = *pCksum;
222053 Fts5IndexIter *pIter = 0;
222054 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
222055
222056 while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
222057 i64 rowid = pIter->iRowid;
222058
222059 if( eDetail==FTS5_DETAIL_NONE ){
222060 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
222061 }else{
222062 Fts5PoslistReader sReader;
222063 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
222064 sReader.bEof==0;
222065 sqlite3Fts5PoslistReaderNext(&sReader)
222066 ){
222067 int iCol = FTS5_POS2COLUMN(sReader.iPos);
222068 int iOff = FTS5_POS2OFFSET(sReader.iPos);
222069 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
222070 }
222071 }
222072 if( rc==SQLITE_OK ){
222073 rc = sqlite3Fts5IterNext(pIter);
222074 }
222075 }
222076 sqlite3Fts5IterClose(pIter);
222077
222078 *pCksum = cksum;
222079 return rc;
222080}
222081
222082/*
222083** Check if buffer z[], size n bytes, contains as series of valid utf-8
222084** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
222085** contain valid utf-8, return non-zero.
222086*/
222087static int fts5TestUtf8(const char *z, int n){
222088 int i = 0;
222089 assert_nc( n>0 );
222090 while( i<n ){
222091 if( (z[i] & 0x80)==0x00 ){
222092 i++;
222093 }else
222094 if( (z[i] & 0xE0)==0xC0 ){
222095 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
222096 i += 2;
222097 }else
222098 if( (z[i] & 0xF0)==0xE0 ){
222099 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
222100 i += 3;
222101 }else
222102 if( (z[i] & 0xF8)==0xF0 ){
222103 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
222104 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
222105 i += 3;
222106 }else{
222107 return 1;
222108 }
222109 }
222110
222111 return 0;
222112}
222113
222114/*
222115** This function is also purely an internal test. It does not contribute to
222116** FTS functionality, or even the integrity-check, in any way.
222117*/
222118static void fts5TestTerm(
222119 Fts5Index *p,
222120 Fts5Buffer *pPrev, /* Previous term */
222121 const char *z, int n, /* Possibly new term to test */
222122 u64 expected,
222123 u64 *pCksum
222124){
222125 int rc = p->rc;
222126 if( pPrev->n==0 ){
222127 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
222128 }else
222129 if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
222130 u64 cksum3 = *pCksum;
222131 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
222132 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
222133 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
222134 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
222135 u64 ck1 = 0;
222136 u64 ck2 = 0;
222137
222138 /* Check that the results returned for ASC and DESC queries are
222139 ** the same. If not, call this corruption. */
222140 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
222141 if( rc==SQLITE_OK ){
222142 int f = flags|FTS5INDEX_QUERY_DESC;
222143 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
222144 }
222145 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
222146
222147 /* If this is a prefix query, check that the results returned if the
222148 ** the index is disabled are the same. In both ASC and DESC order.
222149 **
222150 ** This check may only be performed if the hash table is empty. This
222151 ** is because the hash table only supports a single scan query at
222152 ** a time, and the multi-iter loop from which this function is called
222153 ** is already performing such a scan.
222154 **
222155 ** Also only do this if buffer zTerm contains nTerm bytes of valid
222156 ** utf-8. Otherwise, the last part of the buffer contents might contain
222157 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
222158 ** character stored in the main fts index, which will cause the
222159 ** test to fail. */
222160 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
222161 if( iIdx>0 && rc==SQLITE_OK ){
222162 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
222163 ck2 = 0;
222164 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
222165 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
222166 }
222167 if( iIdx>0 && rc==SQLITE_OK ){
222168 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
222169 ck2 = 0;
222170 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
222171 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
222172 }
222173 }
222174
222175 cksum3 ^= ck1;
222176 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
222177
222178 if( rc==SQLITE_OK && cksum3!=expected ){
222179 rc = FTS5_CORRUPT;
222180 }
222181 *pCksum = cksum3;
222182 }
222183 p->rc = rc;
222184}
222185
222186#else
222187# define fts5TestDlidxReverse(x,y,z)
222188# define fts5TestTerm(u,v,w,x,y,z)
222189#endif
222190
222191/*
222192** Check that:
222193**
222194** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
222195** contain zero terms.
222196** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
222197** contain zero rowids.
222198*/
222199static void fts5IndexIntegrityCheckEmpty(
222200 Fts5Index *p,
222201 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
222202 int iFirst,
222203 int iNoRowid,
222204 int iLast
222205){
222206 int i;
222207
222208 /* Now check that the iter.nEmpty leaves following the current leaf
222209 ** (a) exist and (b) contain no terms. */
222210 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
222211 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
222212 if( pLeaf ){
222213 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
222214 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
222215 }
222216 fts5DataRelease(pLeaf);
222217 }
222218}
222219
222220static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
222221 int iTermOff = 0;
222222 int ii;
222223
222224 Fts5Buffer buf1 = {0,0,0};
222225 Fts5Buffer buf2 = {0,0,0};
222226
222227 ii = pLeaf->szLeaf;
222228 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
222229 int res;
222230 int iOff;
222231 int nIncr;
222232
222233 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
222234 iTermOff += nIncr;
222235 iOff = iTermOff;
222236
222237 if( iOff>=pLeaf->szLeaf ){
222238 p->rc = FTS5_CORRUPT;
222239 }else if( iTermOff==nIncr ){
222240 int nByte;
222241 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
222242 if( (iOff+nByte)>pLeaf->szLeaf ){
222243 p->rc = FTS5_CORRUPT;
222244 }else{
222245 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
222246 }
222247 }else{
222248 int nKeep, nByte;
222249 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
222250 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
222251 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
222252 p->rc = FTS5_CORRUPT;
222253 }else{
222254 buf1.n = nKeep;
222255 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
222256 }
222257
222258 if( p->rc==SQLITE_OK ){
222259 res = fts5BufferCompare(&buf1, &buf2);
222260 if( res<=0 ) p->rc = FTS5_CORRUPT;
222261 }
222262 }
222263 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
222264 }
222265
222266 fts5BufferFree(&buf1);
222267 fts5BufferFree(&buf2);
222268}
222269
222270static void fts5IndexIntegrityCheckSegment(
222271 Fts5Index *p, /* FTS5 backend object */
222272 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
222273){
222274 Fts5Config *pConfig = p->pConfig;
222275 sqlite3_stmt *pStmt = 0;
222276 int rc2;
222277 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
222278 int iDlidxPrevLeaf = pSeg->pgnoLast;
222279
222280 if( pSeg->pgnoFirst==0 ) return;
222281
222282 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
222283 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
222284 "ORDER BY 1, 2",
222285 pConfig->zDb, pConfig->zName, pSeg->iSegid
222286 ));
222287
222288 /* Iterate through the b-tree hierarchy. */
222289 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
222290 i64 iRow; /* Rowid for this leaf */
222291 Fts5Data *pLeaf; /* Data for this leaf */
222292
222293 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
222294 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
222295 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
222296 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
222297
222298 /* If the leaf in question has already been trimmed from the segment,
222299 ** ignore this b-tree entry. Otherwise, load it into memory. */
222300 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
222301 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
222302 pLeaf = fts5LeafRead(p, iRow);
222303 if( pLeaf==0 ) break;
222304
222305 /* Check that the leaf contains at least one term, and that it is equal
222306 ** to or larger than the split-key in zIdxTerm. Also check that if there
222307 ** is also a rowid pointer within the leaf page header, it points to a
222308 ** location before the term. */
222309 if( pLeaf->nn<=pLeaf->szLeaf ){
222310 p->rc = FTS5_CORRUPT;
222311 }else{
222312 int iOff; /* Offset of first term on leaf */
222313 int iRowidOff; /* Offset of first rowid on leaf */
222314 int nTerm; /* Size of term on leaf in bytes */
222315 int res; /* Comparison of term and split-key */
222316
222317 iOff = fts5LeafFirstTermOff(pLeaf);
222318 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
222319 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
222320 p->rc = FTS5_CORRUPT;
222321 }else{
222322 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
222323 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
222324 if( res==0 ) res = nTerm - nIdxTerm;
222325 if( res<0 ) p->rc = FTS5_CORRUPT;
222326 }
222327
222328 fts5IntegrityCheckPgidx(p, pLeaf);
222329 }
222330 fts5DataRelease(pLeaf);
222331 if( p->rc ) break;
222332
222333 /* Now check that the iter.nEmpty leaves following the current leaf
222334 ** (a) exist and (b) contain no terms. */
222335 fts5IndexIntegrityCheckEmpty(
222336 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
222337 );
222338 if( p->rc ) break;
222339
222340 /* If there is a doclist-index, check that it looks right. */
222341 if( bIdxDlidx ){
222342 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
222343 int iPrevLeaf = iIdxLeaf;
222344 int iSegid = pSeg->iSegid;
222345 int iPg = 0;
222346 i64 iKey;
222347
222348 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
222349 fts5DlidxIterEof(p, pDlidx)==0;
222350 fts5DlidxIterNext(p, pDlidx)
222351 ){
222352
222353 /* Check any rowid-less pages that occur before the current leaf. */
222354 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
222355 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
222356 pLeaf = fts5DataRead(p, iKey);
222357 if( pLeaf ){
222358 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
222359 fts5DataRelease(pLeaf);
222360 }
222361 }
222362 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
222363
222364 /* Check that the leaf page indicated by the iterator really does
222365 ** contain the rowid suggested by the same. */
222366 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
222367 pLeaf = fts5DataRead(p, iKey);
222368 if( pLeaf ){
222369 i64 iRowid;
222370 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
222371 ASSERT_SZLEAF_OK(pLeaf);
222372 if( iRowidOff>=pLeaf->szLeaf ){
222373 p->rc = FTS5_CORRUPT;
222374 }else{
222375 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
222376 if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
222377 }
222378 fts5DataRelease(pLeaf);
222379 }
222380 }
222381
222382 iDlidxPrevLeaf = iPg;
222383 fts5DlidxIterFree(pDlidx);
222384 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
222385 }else{
222386 iDlidxPrevLeaf = pSeg->pgnoLast;
222387 /* TODO: Check there is no doclist index */
222388 }
222389
222390 iIdxPrevLeaf = iIdxLeaf;
222391 }
222392
222393 rc2 = sqlite3_finalize(pStmt);
222394 if( p->rc==SQLITE_OK ) p->rc = rc2;
222395
222396 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
222397#if 0
222398 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
222399 p->rc = FTS5_CORRUPT;
222400 }
222401#endif
222402}
222403
222404
222405/*
222406** Run internal checks to ensure that the FTS index (a) is internally
222407** consistent and (b) contains entries for which the XOR of the checksums
222408** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
222409**
222410** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
222411** checksum does not match. Return SQLITE_OK if all checks pass without
222412** error, or some other SQLite error code if another error (e.g. OOM)
222413** occurs.
222414*/
222415static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
222416 int eDetail = p->pConfig->eDetail;
222417 u64 cksum2 = 0; /* Checksum based on contents of indexes */
222418 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
222419 Fts5Iter *pIter; /* Used to iterate through entire index */
222420 Fts5Structure *pStruct; /* Index structure */
222421
222422#ifdef SQLITE_DEBUG
222423 /* Used by extra internal tests only run if NDEBUG is not defined */
222424 u64 cksum3 = 0; /* Checksum based on contents of indexes */
222425 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
222426#endif
222427 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
222428
222429 /* Load the FTS index structure */
222430 pStruct = fts5StructureRead(p);
222431
222432 /* Check that the internal nodes of each segment match the leaves */
222433 if( pStruct ){
222434 int iLvl, iSeg;
222435 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
222436 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
222437 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
222438 fts5IndexIntegrityCheckSegment(p, pSeg);
222439 }
222440 }
222441 }
222442
222443 /* The cksum argument passed to this function is a checksum calculated
222444 ** based on all expected entries in the FTS index (including prefix index
222445 ** entries). This block checks that a checksum calculated based on the
222446 ** actual contents of FTS index is identical.
222447 **
222448 ** Two versions of the same checksum are calculated. The first (stack
222449 ** variable cksum2) based on entries extracted from the full-text index
222450 ** while doing a linear scan of each individual index in turn.
222451 **
222452 ** As each term visited by the linear scans, a separate query for the
222453 ** same term is performed. cksum3 is calculated based on the entries
222454 ** extracted by these queries.
222455 */
222456 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
222457 fts5MultiIterEof(p, pIter)==0;
222458 fts5MultiIterNext(p, pIter, 0, 0)
222459 ){
222460 int n; /* Size of term in bytes */
222461 i64 iPos = 0; /* Position read from poslist */
222462 int iOff = 0; /* Offset within poslist */
222463 i64 iRowid = fts5MultiIterRowid(pIter);
222464 char *z = (char*)fts5MultiIterTerm(pIter, &n);
222465
222466 /* If this is a new term, query for it. Update cksum3 with the results. */
222467 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
222468
222469 if( eDetail==FTS5_DETAIL_NONE ){
222470 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
222471 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
222472 }
222473 }else{
222474 poslist.n = 0;
222475 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
222476 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
222477 int iCol = FTS5_POS2COLUMN(iPos);
222478 int iTokOff = FTS5_POS2OFFSET(iPos);
222479 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
222480 }
222481 }
222482 }
222483 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
222484
222485 fts5MultiIterFree(pIter);
222486 if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
222487
222488 fts5StructureRelease(pStruct);
222489#ifdef SQLITE_DEBUG
222490 fts5BufferFree(&term);
222491#endif
222492 fts5BufferFree(&poslist);
222493 return fts5IndexReturn(p);
222494}
222495
222496/*************************************************************************
222497**************************************************************************
222498** Below this point is the implementation of the fts5_decode() scalar
222499** function only.
222500*/
222501
222502/*
222503** Decode a segment-data rowid from the %_data table. This function is
222504** the opposite of macro FTS5_SEGMENT_ROWID().
222505*/
222506static void fts5DecodeRowid(
222507 i64 iRowid, /* Rowid from %_data table */
222508 int *piSegid, /* OUT: Segment id */
222509 int *pbDlidx, /* OUT: Dlidx flag */
222510 int *piHeight, /* OUT: Height */
222511 int *piPgno /* OUT: Page number */
222512){
222513 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
222514 iRowid >>= FTS5_DATA_PAGE_B;
222515
222516 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
222517 iRowid >>= FTS5_DATA_HEIGHT_B;
222518
222519 *pbDlidx = (int)(iRowid & 0x0001);
222520 iRowid >>= FTS5_DATA_DLI_B;
222521
222522 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
222523}
222524
222525static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
222526 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
222527 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
222528
222529 if( iSegid==0 ){
222530 if( iKey==FTS5_AVERAGES_ROWID ){
222531 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
222532 }else{
222533 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
222534 }
222535 }
222536 else{
222537 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
222538 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
222539 );
222540 }
222541}
222542
222543static void fts5DebugStructure(
222544 int *pRc, /* IN/OUT: error code */
222545 Fts5Buffer *pBuf,
222546 Fts5Structure *p
222547){
222548 int iLvl, iSeg; /* Iterate through levels, segments */
222549
222550 for(iLvl=0; iLvl<p->nLevel; iLvl++){
222551 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
222552 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
222553 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
222554 );
222555 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
222556 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
222557 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
222558 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
222559 );
222560 }
222561 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
222562 }
222563}
222564
222565/*
222566** This is part of the fts5_decode() debugging aid.
222567**
222568** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
222569** function appends a human-readable representation of the same object
222570** to the buffer passed as the second argument.
222571*/
222572static void fts5DecodeStructure(
222573 int *pRc, /* IN/OUT: error code */
222574 Fts5Buffer *pBuf,
222575 const u8 *pBlob, int nBlob
222576){
222577 int rc; /* Return code */
222578 Fts5Structure *p = 0; /* Decoded structure object */
222579
222580 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
222581 if( rc!=SQLITE_OK ){
222582 *pRc = rc;
222583 return;
222584 }
222585
222586 fts5DebugStructure(pRc, pBuf, p);
222587 fts5StructureRelease(p);
222588}
222589
222590/*
222591** This is part of the fts5_decode() debugging aid.
222592**
222593** Arguments pBlob/nBlob contain an "averages" record. This function
222594** appends a human-readable representation of record to the buffer passed
222595** as the second argument.
222596*/
222597static void fts5DecodeAverages(
222598 int *pRc, /* IN/OUT: error code */
222599 Fts5Buffer *pBuf,
222600 const u8 *pBlob, int nBlob
222601){
222602 int i = 0;
222603 const char *zSpace = "";
222604
222605 while( i<nBlob ){
222606 u64 iVal;
222607 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
222608 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
222609 zSpace = " ";
222610 }
222611}
222612
222613/*
222614** Buffer (a/n) is assumed to contain a list of serialized varints. Read
222615** each varint and append its string representation to buffer pBuf. Return
222616** after either the input buffer is exhausted or a 0 value is read.
222617**
222618** The return value is the number of bytes read from the input buffer.
222619*/
222620static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
222621 int iOff = 0;
222622 while( iOff<n ){
222623 int iVal;
222624 iOff += fts5GetVarint32(&a[iOff], iVal);
222625 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
222626 }
222627 return iOff;
222628}
222629
222630/*
222631** The start of buffer (a/n) contains the start of a doclist. The doclist
222632** may or may not finish within the buffer. This function appends a text
222633** representation of the part of the doclist that is present to buffer
222634** pBuf.
222635**
222636** The return value is the number of bytes read from the input buffer.
222637*/
222638static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
222639 i64 iDocid = 0;
222640 int iOff = 0;
222641
222642 if( n>0 ){
222643 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
222644 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
222645 }
222646 while( iOff<n ){
222647 int nPos;
222648 int bDel;
222649 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
222650 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
222651 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
222652 if( iOff<n ){
222653 i64 iDelta;
222654 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
222655 iDocid += iDelta;
222656 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
222657 }
222658 }
222659
222660 return iOff;
222661}
222662
222663/*
222664** This function is part of the fts5_decode() debugging function. It is
222665** only ever used with detail=none tables.
222666**
222667** Buffer (pData/nData) contains a doclist in the format used by detail=none
222668** tables. This function appends a human-readable version of that list to
222669** buffer pBuf.
222670**
222671** If *pRc is other than SQLITE_OK when this function is called, it is a
222672** no-op. If an OOM or other error occurs within this function, *pRc is
222673** set to an SQLite error code before returning. The final state of buffer
222674** pBuf is undefined in this case.
222675*/
222676static void fts5DecodeRowidList(
222677 int *pRc, /* IN/OUT: Error code */
222678 Fts5Buffer *pBuf, /* Buffer to append text to */
222679 const u8 *pData, int nData /* Data to decode list-of-rowids from */
222680){
222681 int i = 0;
222682 i64 iRowid = 0;
222683
222684 while( i<nData ){
222685 const char *zApp = "";
222686 u64 iVal;
222687 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
222688 iRowid += iVal;
222689
222690 if( i<nData && pData[i]==0x00 ){
222691 i++;
222692 if( i<nData && pData[i]==0x00 ){
222693 i++;
222694 zApp = "+";
222695 }else{
222696 zApp = "*";
222697 }
222698 }
222699
222700 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
222701 }
222702}
222703
222704/*
222705** The implementation of user-defined scalar function fts5_decode().
222706*/
222707static void fts5DecodeFunction(
222708 sqlite3_context *pCtx, /* Function call context */
222709 int nArg, /* Number of args (always 2) */
222710 sqlite3_value **apVal /* Function arguments */
222711){
222712 i64 iRowid; /* Rowid for record being decoded */
222713 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
222714 const u8 *aBlob; int n; /* Record to decode */
222715 u8 *a = 0;
222716 Fts5Buffer s; /* Build up text to return here */
222717 int rc = SQLITE_OK; /* Return code */
222718 sqlite3_int64 nSpace = 0;
222719 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
222720
222721 assert( nArg==2 );
222722 UNUSED_PARAM(nArg);
222723 memset(&s, 0, sizeof(Fts5Buffer));
222724 iRowid = sqlite3_value_int64(apVal[0]);
222725
222726 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
222727 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
222728 ** buffer overreads even if the record is corrupt. */
222729 n = sqlite3_value_bytes(apVal[1]);
222730 aBlob = sqlite3_value_blob(apVal[1]);
222731 nSpace = n + FTS5_DATA_ZERO_PADDING;
222732 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
222733 if( a==0 ) goto decode_out;
222734 if( n>0 ) memcpy(a, aBlob, n);
222735
222736 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
222737
222738 fts5DebugRowid(&rc, &s, iRowid);
222739 if( bDlidx ){
222740 Fts5Data dlidx;
222741 Fts5DlidxLvl lvl;
222742
222743 dlidx.p = a;
222744 dlidx.nn = n;
222745
222746 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
222747 lvl.pData = &dlidx;
222748 lvl.iLeafPgno = iPgno;
222749
222750 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
222751 sqlite3Fts5BufferAppendPrintf(&rc, &s,
222752 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
222753 );
222754 }
222755 }else if( iSegid==0 ){
222756 if( iRowid==FTS5_AVERAGES_ROWID ){
222757 fts5DecodeAverages(&rc, &s, a, n);
222758 }else{
222759 fts5DecodeStructure(&rc, &s, a, n);
222760 }
222761 }else if( eDetailNone ){
222762 Fts5Buffer term; /* Current term read from page */
222763 int szLeaf;
222764 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
222765 int iTermOff;
222766 int nKeep = 0;
222767 int iOff;
222768
222769 memset(&term, 0, sizeof(Fts5Buffer));
222770
222771 /* Decode any entries that occur before the first term. */
222772 if( szLeaf<n ){
222773 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
222774 }else{
222775 iTermOff = szLeaf;
222776 }
222777 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
222778
222779 iOff = iTermOff;
222780 while( iOff<szLeaf ){
222781 int nAppend;
222782
222783 /* Read the term data for the next term*/
222784 iOff += fts5GetVarint32(&a[iOff], nAppend);
222785 term.n = nKeep;
222786 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
222787 sqlite3Fts5BufferAppendPrintf(
222788 &rc, &s, " term=%.*s", term.n, (const char*)term.p
222789 );
222790 iOff += nAppend;
222791
222792 /* Figure out where the doclist for this term ends */
222793 if( iPgidxOff<n ){
222794 int nIncr;
222795 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
222796 iTermOff += nIncr;
222797 }else{
222798 iTermOff = szLeaf;
222799 }
222800
222801 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
222802 iOff = iTermOff;
222803 if( iOff<szLeaf ){
222804 iOff += fts5GetVarint32(&a[iOff], nKeep);
222805 }
222806 }
222807
222808 fts5BufferFree(&term);
222809 }else{
222810 Fts5Buffer term; /* Current term read from page */
222811 int szLeaf; /* Offset of pgidx in a[] */
222812 int iPgidxOff;
222813 int iPgidxPrev = 0; /* Previous value read from pgidx */
222814 int iTermOff = 0;
222815 int iRowidOff = 0;
222816 int iOff;
222817 int nDoclist;
222818
222819 memset(&term, 0, sizeof(Fts5Buffer));
222820
222821 if( n<4 ){
222822 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
222823 goto decode_out;
222824 }else{
222825 iRowidOff = fts5GetU16(&a[0]);
222826 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
222827 if( iPgidxOff<n ){
222828 fts5GetVarint32(&a[iPgidxOff], iTermOff);
222829 }else if( iPgidxOff>n ){
222830 rc = FTS5_CORRUPT;
222831 goto decode_out;
222832 }
222833 }
222834
222835 /* Decode the position list tail at the start of the page */
222836 if( iRowidOff!=0 ){
222837 iOff = iRowidOff;
222838 }else if( iTermOff!=0 ){
222839 iOff = iTermOff;
222840 }else{
222841 iOff = szLeaf;
222842 }
222843 if( iOff>n ){
222844 rc = FTS5_CORRUPT;
222845 goto decode_out;
222846 }
222847 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
222848
222849 /* Decode any more doclist data that appears on the page before the
222850 ** first term. */
222851 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
222852 if( nDoclist+iOff>n ){
222853 rc = FTS5_CORRUPT;
222854 goto decode_out;
222855 }
222856 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
222857
222858 while( iPgidxOff<n && rc==SQLITE_OK ){
222859 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
222860 int nByte; /* Bytes of data */
222861 int iEnd;
222862
222863 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
222864 iPgidxPrev += nByte;
222865 iOff = iPgidxPrev;
222866
222867 if( iPgidxOff<n ){
222868 fts5GetVarint32(&a[iPgidxOff], nByte);
222869 iEnd = iPgidxPrev + nByte;
222870 }else{
222871 iEnd = szLeaf;
222872 }
222873 if( iEnd>szLeaf ){
222874 rc = FTS5_CORRUPT;
222875 break;
222876 }
222877
222878 if( bFirst==0 ){
222879 iOff += fts5GetVarint32(&a[iOff], nByte);
222880 if( nByte>term.n ){
222881 rc = FTS5_CORRUPT;
222882 break;
222883 }
222884 term.n = nByte;
222885 }
222886 iOff += fts5GetVarint32(&a[iOff], nByte);
222887 if( iOff+nByte>n ){
222888 rc = FTS5_CORRUPT;
222889 break;
222890 }
222891 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
222892 iOff += nByte;
222893
222894 sqlite3Fts5BufferAppendPrintf(
222895 &rc, &s, " term=%.*s", term.n, (const char*)term.p
222896 );
222897 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
222898 }
222899
222900 fts5BufferFree(&term);
222901 }
222902
222903 decode_out:
222904 sqlite3_free(a);
222905 if( rc==SQLITE_OK ){
222906 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
222907 }else{
222908 sqlite3_result_error_code(pCtx, rc);
222909 }
222910 fts5BufferFree(&s);
222911}
222912
222913/*
222914** The implementation of user-defined scalar function fts5_rowid().
222915*/
222916static void fts5RowidFunction(
222917 sqlite3_context *pCtx, /* Function call context */
222918 int nArg, /* Number of args (always 2) */
222919 sqlite3_value **apVal /* Function arguments */
222920){
222921 const char *zArg;
222922 if( nArg==0 ){
222923 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
222924 }else{
222925 zArg = (const char*)sqlite3_value_text(apVal[0]);
222926 if( 0==sqlite3_stricmp(zArg, "segment") ){
222927 i64 iRowid;
222928 int segid, pgno;
222929 if( nArg!=3 ){
222930 sqlite3_result_error(pCtx,
222931 "should be: fts5_rowid('segment', segid, pgno))", -1
222932 );
222933 }else{
222934 segid = sqlite3_value_int(apVal[1]);
222935 pgno = sqlite3_value_int(apVal[2]);
222936 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
222937 sqlite3_result_int64(pCtx, iRowid);
222938 }
222939 }else{
222940 sqlite3_result_error(pCtx,
222941 "first arg to fts5_rowid() must be 'segment'" , -1
222942 );
222943 }
222944 }
222945}
222946
222947/*
222948** This is called as part of registering the FTS5 module with database
222949** connection db. It registers several user-defined scalar functions useful
222950** with FTS5.
222951**
222952** If successful, SQLITE_OK is returned. If an error occurs, some other
222953** SQLite error code is returned instead.
222954*/
222955static int sqlite3Fts5IndexInit(sqlite3 *db){
222956 int rc = sqlite3_create_function(
222957 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
222958 );
222959
222960 if( rc==SQLITE_OK ){
222961 rc = sqlite3_create_function(
222962 db, "fts5_decode_none", 2,
222963 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
222964 );
222965 }
222966
222967 if( rc==SQLITE_OK ){
222968 rc = sqlite3_create_function(
222969 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
222970 );
222971 }
222972 return rc;
222973}
222974
222975
222976static int sqlite3Fts5IndexReset(Fts5Index *p){
222977 assert( p->pStruct==0 || p->iStructVersion!=0 );
222978 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
222979 fts5StructureInvalidate(p);
222980 }
222981 return fts5IndexReturn(p);
222982}
222983
222984/*
222985** 2014 Jun 09
222986**
222987** The author disclaims copyright to this source code. In place of
222988** a legal notice, here is a blessing:
222989**
222990** May you do good and not evil.
222991** May you find forgiveness for yourself and forgive others.
222992** May you share freely, never taking more than you give.
222993**
222994******************************************************************************
222995**
222996** This is an SQLite module implementing full-text search.
222997*/
222998
222999
223000/* #include "fts5Int.h" */
223001
223002/*
223003** This variable is set to false when running tests for which the on disk
223004** structures should not be corrupt. Otherwise, true. If it is false, extra
223005** assert() conditions in the fts5 code are activated - conditions that are
223006** only true if it is guaranteed that the fts5 database is not corrupt.
223007*/
223008SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
223009
223010
223011typedef struct Fts5Auxdata Fts5Auxdata;
223012typedef struct Fts5Auxiliary Fts5Auxiliary;
223013typedef struct Fts5Cursor Fts5Cursor;
223014typedef struct Fts5FullTable Fts5FullTable;
223015typedef struct Fts5Sorter Fts5Sorter;
223016typedef struct Fts5TokenizerModule Fts5TokenizerModule;
223017
223018/*
223019** NOTES ON TRANSACTIONS:
223020**
223021** SQLite invokes the following virtual table methods as transactions are
223022** opened and closed by the user:
223023**
223024** xBegin(): Start of a new transaction.
223025** xSync(): Initial part of two-phase commit.
223026** xCommit(): Final part of two-phase commit.
223027** xRollback(): Rollback the transaction.
223028**
223029** Anything that is required as part of a commit that may fail is performed
223030** in the xSync() callback. Current versions of SQLite ignore any errors
223031** returned by xCommit().
223032**
223033** And as sub-transactions are opened/closed:
223034**
223035** xSavepoint(int S): Open savepoint S.
223036** xRelease(int S): Commit and close savepoint S.
223037** xRollbackTo(int S): Rollback to start of savepoint S.
223038**
223039** During a write-transaction the fts5_index.c module may cache some data
223040** in-memory. It is flushed to disk whenever xSync(), xRelease() or
223041** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
223042** is called.
223043**
223044** Additionally, if SQLITE_DEBUG is defined, an instance of the following
223045** structure is used to record the current transaction state. This information
223046** is not required, but it is used in the assert() statements executed by
223047** function fts5CheckTransactionState() (see below).
223048*/
223049struct Fts5TransactionState {
223050 int eState; /* 0==closed, 1==open, 2==synced */
223051 int iSavepoint; /* Number of open savepoints (0 -> none) */
223052};
223053
223054/*
223055** A single object of this type is allocated when the FTS5 module is
223056** registered with a database handle. It is used to store pointers to
223057** all registered FTS5 extensions - tokenizers and auxiliary functions.
223058*/
223059struct Fts5Global {
223060 fts5_api api; /* User visible part of object (see fts5.h) */
223061 sqlite3 *db; /* Associated database connection */
223062 i64 iNextId; /* Used to allocate unique cursor ids */
223063 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
223064 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
223065 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
223066 Fts5Cursor *pCsr; /* First in list of all open cursors */
223067};
223068
223069/*
223070** Each auxiliary function registered with the FTS5 module is represented
223071** by an object of the following type. All such objects are stored as part
223072** of the Fts5Global.pAux list.
223073*/
223074struct Fts5Auxiliary {
223075 Fts5Global *pGlobal; /* Global context for this function */
223076 char *zFunc; /* Function name (nul-terminated) */
223077 void *pUserData; /* User-data pointer */
223078 fts5_extension_function xFunc; /* Callback function */
223079 void (*xDestroy)(void*); /* Destructor function */
223080 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
223081};
223082
223083/*
223084** Each tokenizer module registered with the FTS5 module is represented
223085** by an object of the following type. All such objects are stored as part
223086** of the Fts5Global.pTok list.
223087*/
223088struct Fts5TokenizerModule {
223089 char *zName; /* Name of tokenizer */
223090 void *pUserData; /* User pointer passed to xCreate() */
223091 fts5_tokenizer x; /* Tokenizer functions */
223092 void (*xDestroy)(void*); /* Destructor function */
223093 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
223094};
223095
223096struct Fts5FullTable {
223097 Fts5Table p; /* Public class members from fts5Int.h */
223098 Fts5Storage *pStorage; /* Document store */
223099 Fts5Global *pGlobal; /* Global (connection wide) data */
223100 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
223101#ifdef SQLITE_DEBUG
223102 struct Fts5TransactionState ts;
223103#endif
223104};
223105
223106struct Fts5MatchPhrase {
223107 Fts5Buffer *pPoslist; /* Pointer to current poslist */
223108 int nTerm; /* Size of phrase in terms */
223109};
223110
223111/*
223112** pStmt:
223113** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
223114**
223115** aIdx[]:
223116** There is one entry in the aIdx[] array for each phrase in the query,
223117** the value of which is the offset within aPoslist[] following the last
223118** byte of the position list for the corresponding phrase.
223119*/
223120struct Fts5Sorter {
223121 sqlite3_stmt *pStmt;
223122 i64 iRowid; /* Current rowid */
223123 const u8 *aPoslist; /* Position lists for current row */
223124 int nIdx; /* Number of entries in aIdx[] */
223125 int aIdx[1]; /* Offsets into aPoslist for current row */
223126};
223127
223128
223129/*
223130** Virtual-table cursor object.
223131**
223132** iSpecial:
223133** If this is a 'special' query (refer to function fts5SpecialMatch()),
223134** then this variable contains the result of the query.
223135**
223136** iFirstRowid, iLastRowid:
223137** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
223138** cursor iterates in ascending order of rowids, iFirstRowid is the lower
223139** limit of rowids to return, and iLastRowid the upper. In other words, the
223140** WHERE clause in the user's query might have been:
223141**
223142** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
223143**
223144** If the cursor iterates in descending order of rowid, iFirstRowid
223145** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
223146** the lower.
223147*/
223148struct Fts5Cursor {
223149 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
223150 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
223151 int *aColumnSize; /* Values for xColumnSize() */
223152 i64 iCsrId; /* Cursor id */
223153
223154 /* Zero from this point onwards on cursor reset */
223155 int ePlan; /* FTS5_PLAN_XXX value */
223156 int bDesc; /* True for "ORDER BY rowid DESC" queries */
223157 i64 iFirstRowid; /* Return no rowids earlier than this */
223158 i64 iLastRowid; /* Return no rowids later than this */
223159 sqlite3_stmt *pStmt; /* Statement used to read %_content */
223160 Fts5Expr *pExpr; /* Expression for MATCH queries */
223161 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
223162 int csrflags; /* Mask of cursor flags (see below) */
223163 i64 iSpecial; /* Result of special query */
223164
223165 /* "rank" function. Populated on demand from vtab.xColumn(). */
223166 char *zRank; /* Custom rank function */
223167 char *zRankArgs; /* Custom rank function args */
223168 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
223169 int nRankArg; /* Number of trailing arguments for rank() */
223170 sqlite3_value **apRankArg; /* Array of trailing arguments */
223171 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
223172
223173 /* Auxiliary data storage */
223174 Fts5Auxiliary *pAux; /* Currently executing extension function */
223175 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
223176
223177 /* Cache used by auxiliary functions xInst() and xInstCount() */
223178 Fts5PoslistReader *aInstIter; /* One for each phrase */
223179 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
223180 int nInstCount; /* Number of phrase instances */
223181 int *aInst; /* 3 integers per phrase instance */
223182};
223183
223184/*
223185** Bits that make up the "idxNum" parameter passed indirectly by
223186** xBestIndex() to xFilter().
223187*/
223188#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
223189#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
223190#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
223191#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
223192#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
223193
223194#define FTS5_BI_ORDER_RANK 0x0020
223195#define FTS5_BI_ORDER_ROWID 0x0040
223196#define FTS5_BI_ORDER_DESC 0x0080
223197
223198/*
223199** Values for Fts5Cursor.csrflags
223200*/
223201#define FTS5CSR_EOF 0x01
223202#define FTS5CSR_REQUIRE_CONTENT 0x02
223203#define FTS5CSR_REQUIRE_DOCSIZE 0x04
223204#define FTS5CSR_REQUIRE_INST 0x08
223205#define FTS5CSR_FREE_ZRANK 0x10
223206#define FTS5CSR_REQUIRE_RESEEK 0x20
223207#define FTS5CSR_REQUIRE_POSLIST 0x40
223208
223209#define BitFlagAllTest(x,y) (((x) & (y))==(y))
223210#define BitFlagTest(x,y) (((x) & (y))!=0)
223211
223212
223213/*
223214** Macros to Set(), Clear() and Test() cursor flags.
223215*/
223216#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
223217#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
223218#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
223219
223220struct Fts5Auxdata {
223221 Fts5Auxiliary *pAux; /* Extension to which this belongs */
223222 void *pPtr; /* Pointer value */
223223 void(*xDelete)(void*); /* Destructor */
223224 Fts5Auxdata *pNext; /* Next object in linked list */
223225};
223226
223227#ifdef SQLITE_DEBUG
223228#define FTS5_BEGIN 1
223229#define FTS5_SYNC 2
223230#define FTS5_COMMIT 3
223231#define FTS5_ROLLBACK 4
223232#define FTS5_SAVEPOINT 5
223233#define FTS5_RELEASE 6
223234#define FTS5_ROLLBACKTO 7
223235static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
223236 switch( op ){
223237 case FTS5_BEGIN:
223238 assert( p->ts.eState==0 );
223239 p->ts.eState = 1;
223240 p->ts.iSavepoint = -1;
223241 break;
223242
223243 case FTS5_SYNC:
223244 assert( p->ts.eState==1 );
223245 p->ts.eState = 2;
223246 break;
223247
223248 case FTS5_COMMIT:
223249 assert( p->ts.eState==2 );
223250 p->ts.eState = 0;
223251 break;
223252
223253 case FTS5_ROLLBACK:
223254 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
223255 p->ts.eState = 0;
223256 break;
223257
223258 case FTS5_SAVEPOINT:
223259 assert( p->ts.eState==1 );
223260 assert( iSavepoint>=0 );
223261 assert( iSavepoint>=p->ts.iSavepoint );
223262 p->ts.iSavepoint = iSavepoint;
223263 break;
223264
223265 case FTS5_RELEASE:
223266 assert( p->ts.eState==1 );
223267 assert( iSavepoint>=0 );
223268 assert( iSavepoint<=p->ts.iSavepoint );
223269 p->ts.iSavepoint = iSavepoint-1;
223270 break;
223271
223272 case FTS5_ROLLBACKTO:
223273 assert( p->ts.eState==1 );
223274 assert( iSavepoint>=-1 );
223275 /* The following assert() can fail if another vtab strikes an error
223276 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
223277 ** having called xSavepoint() on this vtab. */
223278 /* assert( iSavepoint<=p->ts.iSavepoint ); */
223279 p->ts.iSavepoint = iSavepoint;
223280 break;
223281 }
223282}
223283#else
223284# define fts5CheckTransactionState(x,y,z)
223285#endif
223286
223287/*
223288** Return true if pTab is a contentless table.
223289*/
223290static int fts5IsContentless(Fts5FullTable *pTab){
223291 return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
223292}
223293
223294/*
223295** Delete a virtual table handle allocated by fts5InitVtab().
223296*/
223297static void fts5FreeVtab(Fts5FullTable *pTab){
223298 if( pTab ){
223299 sqlite3Fts5IndexClose(pTab->p.pIndex);
223300 sqlite3Fts5StorageClose(pTab->pStorage);
223301 sqlite3Fts5ConfigFree(pTab->p.pConfig);
223302 sqlite3_free(pTab);
223303 }
223304}
223305
223306/*
223307** The xDisconnect() virtual table method.
223308*/
223309static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
223310 fts5FreeVtab((Fts5FullTable*)pVtab);
223311 return SQLITE_OK;
223312}
223313
223314/*
223315** The xDestroy() virtual table method.
223316*/
223317static int fts5DestroyMethod(sqlite3_vtab *pVtab){
223318 Fts5Table *pTab = (Fts5Table*)pVtab;
223319 int rc = sqlite3Fts5DropAll(pTab->pConfig);
223320 if( rc==SQLITE_OK ){
223321 fts5FreeVtab((Fts5FullTable*)pVtab);
223322 }
223323 return rc;
223324}
223325
223326/*
223327** This function is the implementation of both the xConnect and xCreate
223328** methods of the FTS3 virtual table.
223329**
223330** The argv[] array contains the following:
223331**
223332** argv[0] -> module name ("fts5")
223333** argv[1] -> database name
223334** argv[2] -> table name
223335** argv[...] -> "column name" and other module argument fields.
223336*/
223337static int fts5InitVtab(
223338 int bCreate, /* True for xCreate, false for xConnect */
223339 sqlite3 *db, /* The SQLite database connection */
223340 void *pAux, /* Hash table containing tokenizers */
223341 int argc, /* Number of elements in argv array */
223342 const char * const *argv, /* xCreate/xConnect argument array */
223343 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
223344 char **pzErr /* Write any error message here */
223345){
223346 Fts5Global *pGlobal = (Fts5Global*)pAux;
223347 const char **azConfig = (const char**)argv;
223348 int rc = SQLITE_OK; /* Return code */
223349 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
223350 Fts5FullTable *pTab = 0; /* New virtual table object */
223351
223352 /* Allocate the new vtab object and parse the configuration */
223353 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
223354 if( rc==SQLITE_OK ){
223355 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
223356 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
223357 }
223358 if( rc==SQLITE_OK ){
223359 pTab->p.pConfig = pConfig;
223360 pTab->pGlobal = pGlobal;
223361 }
223362
223363 /* Open the index sub-system */
223364 if( rc==SQLITE_OK ){
223365 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
223366 }
223367
223368 /* Open the storage sub-system */
223369 if( rc==SQLITE_OK ){
223370 rc = sqlite3Fts5StorageOpen(
223371 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
223372 );
223373 }
223374
223375 /* Call sqlite3_declare_vtab() */
223376 if( rc==SQLITE_OK ){
223377 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
223378 }
223379
223380 /* Load the initial configuration */
223381 if( rc==SQLITE_OK ){
223382 assert( pConfig->pzErrmsg==0 );
223383 pConfig->pzErrmsg = pzErr;
223384 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
223385 sqlite3Fts5IndexRollback(pTab->p.pIndex);
223386 pConfig->pzErrmsg = 0;
223387 }
223388
223389 if( rc!=SQLITE_OK ){
223390 fts5FreeVtab(pTab);
223391 pTab = 0;
223392 }else if( bCreate ){
223393 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
223394 }
223395 *ppVTab = (sqlite3_vtab*)pTab;
223396 return rc;
223397}
223398
223399/*
223400** The xConnect() and xCreate() methods for the virtual table. All the
223401** work is done in function fts5InitVtab().
223402*/
223403static int fts5ConnectMethod(
223404 sqlite3 *db, /* Database connection */
223405 void *pAux, /* Pointer to tokenizer hash table */
223406 int argc, /* Number of elements in argv array */
223407 const char * const *argv, /* xCreate/xConnect argument array */
223408 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
223409 char **pzErr /* OUT: sqlite3_malloc'd error message */
223410){
223411 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
223412}
223413static int fts5CreateMethod(
223414 sqlite3 *db, /* Database connection */
223415 void *pAux, /* Pointer to tokenizer hash table */
223416 int argc, /* Number of elements in argv array */
223417 const char * const *argv, /* xCreate/xConnect argument array */
223418 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
223419 char **pzErr /* OUT: sqlite3_malloc'd error message */
223420){
223421 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
223422}
223423
223424/*
223425** The different query plans.
223426*/
223427#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
223428#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
223429#define FTS5_PLAN_SPECIAL 3 /* An internal query */
223430#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
223431#define FTS5_PLAN_SCAN 5 /* No usable constraint */
223432#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
223433
223434/*
223435** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
223436** extension is currently being used by a version of SQLite too old to
223437** support index-info flags. In that case this function is a no-op.
223438*/
223439static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
223440#if SQLITE_VERSION_NUMBER>=3008012
223441#ifndef SQLITE_CORE
223442 if( sqlite3_libversion_number()>=3008012 )
223443#endif
223444 {
223445 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
223446 }
223447#endif
223448}
223449
223450/*
223451** Implementation of the xBestIndex method for FTS5 tables. Within the
223452** WHERE constraint, it searches for the following:
223453**
223454** 1. A MATCH constraint against the table column.
223455** 2. A MATCH constraint against the "rank" column.
223456** 3. A MATCH constraint against some other column.
223457** 4. An == constraint against the rowid column.
223458** 5. A < or <= constraint against the rowid column.
223459** 6. A > or >= constraint against the rowid column.
223460**
223461** Within the ORDER BY, the following are supported:
223462**
223463** 5. ORDER BY rank [ASC|DESC]
223464** 6. ORDER BY rowid [ASC|DESC]
223465**
223466** Information for the xFilter call is passed via both the idxNum and
223467** idxStr variables. Specifically, idxNum is a bitmask of the following
223468** flags used to encode the ORDER BY clause:
223469**
223470** FTS5_BI_ORDER_RANK
223471** FTS5_BI_ORDER_ROWID
223472** FTS5_BI_ORDER_DESC
223473**
223474** idxStr is used to encode data from the WHERE clause. For each argument
223475** passed to the xFilter method, the following is appended to idxStr:
223476**
223477** Match against table column: "m"
223478** Match against rank column: "r"
223479** Match against other column: "<column-number>"
223480** Equality constraint against the rowid: "="
223481** A < or <= against the rowid: "<"
223482** A > or >= against the rowid: ">"
223483**
223484** This function ensures that there is at most one "r" or "=". And that if
223485** there exists an "=" then there is no "<" or ">".
223486**
223487** Costs are assigned as follows:
223488**
223489** a) If an unusable MATCH operator is present in the WHERE clause, the
223490** cost is unconditionally set to 1e50 (a really big number).
223491**
223492** a) If a MATCH operator is present, the cost depends on the other
223493** constraints also present. As follows:
223494**
223495** * No other constraints: cost=1000.0
223496** * One rowid range constraint: cost=750.0
223497** * Both rowid range constraints: cost=500.0
223498** * An == rowid constraint: cost=100.0
223499**
223500** b) Otherwise, if there is no MATCH:
223501**
223502** * No other constraints: cost=1000000.0
223503** * One rowid range constraint: cost=750000.0
223504** * Both rowid range constraints: cost=250000.0
223505** * An == rowid constraint: cost=10.0
223506**
223507** Costs are not modified by the ORDER BY clause.
223508*/
223509static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
223510 Fts5Table *pTab = (Fts5Table*)pVTab;
223511 Fts5Config *pConfig = pTab->pConfig;
223512 const int nCol = pConfig->nCol;
223513 int idxFlags = 0; /* Parameter passed through to xFilter() */
223514 int i;
223515
223516 char *idxStr;
223517 int iIdxStr = 0;
223518 int iCons = 0;
223519
223520 int bSeenEq = 0;
223521 int bSeenGt = 0;
223522 int bSeenLt = 0;
223523 int bSeenMatch = 0;
223524 int bSeenRank = 0;
223525
223526
223527 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
223528 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
223529 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
223530 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
223531 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
223532
223533 if( pConfig->bLock ){
223534 pTab->base.zErrMsg = sqlite3_mprintf(
223535 "recursively defined fts5 content table"
223536 );
223537 return SQLITE_ERROR;
223538 }
223539
223540 idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 6 + 1);
223541 if( idxStr==0 ) return SQLITE_NOMEM;
223542 pInfo->idxStr = idxStr;
223543 pInfo->needToFreeIdxStr = 1;
223544
223545 for(i=0; i<pInfo->nConstraint; i++){
223546 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
223547 int iCol = p->iColumn;
223548 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
223549 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
223550 ){
223551 /* A MATCH operator or equivalent */
223552 if( p->usable==0 || iCol<0 ){
223553 /* As there exists an unusable MATCH constraint this is an
223554 ** unusable plan. Set a prohibitively high cost. */
223555 pInfo->estimatedCost = 1e50;
223556 assert( iIdxStr < pInfo->nConstraint*6 + 1 );
223557 idxStr[iIdxStr] = 0;
223558 return SQLITE_OK;
223559 }else{
223560 if( iCol==nCol+1 ){
223561 if( bSeenRank ) continue;
223562 idxStr[iIdxStr++] = 'r';
223563 bSeenRank = 1;
223564 }else{
223565 bSeenMatch = 1;
223566 idxStr[iIdxStr++] = 'm';
223567 if( iCol<nCol ){
223568 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
223569 idxStr += strlen(&idxStr[iIdxStr]);
223570 assert( idxStr[iIdxStr]=='\0' );
223571 }
223572 }
223573 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
223574 pInfo->aConstraintUsage[i].omit = 1;
223575 }
223576 }
223577 else if( p->usable && bSeenEq==0
223578 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0
223579 ){
223580 idxStr[iIdxStr++] = '=';
223581 bSeenEq = 1;
223582 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
223583 }
223584 }
223585
223586 if( bSeenEq==0 ){
223587 for(i=0; i<pInfo->nConstraint; i++){
223588 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
223589 if( p->iColumn<0 && p->usable ){
223590 int op = p->op;
223591 if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
223592 if( bSeenLt ) continue;
223593 idxStr[iIdxStr++] = '<';
223594 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
223595 bSeenLt = 1;
223596 }else
223597 if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
223598 if( bSeenGt ) continue;
223599 idxStr[iIdxStr++] = '>';
223600 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
223601 bSeenGt = 1;
223602 }
223603 }
223604 }
223605 }
223606 idxStr[iIdxStr] = '\0';
223607
223608 /* Set idxFlags flags for the ORDER BY clause */
223609 if( pInfo->nOrderBy==1 ){
223610 int iSort = pInfo->aOrderBy[0].iColumn;
223611 if( iSort==(pConfig->nCol+1) && bSeenMatch ){
223612 idxFlags |= FTS5_BI_ORDER_RANK;
223613 }else if( iSort==-1 ){
223614 idxFlags |= FTS5_BI_ORDER_ROWID;
223615 }
223616 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
223617 pInfo->orderByConsumed = 1;
223618 if( pInfo->aOrderBy[0].desc ){
223619 idxFlags |= FTS5_BI_ORDER_DESC;
223620 }
223621 }
223622 }
223623
223624 /* Calculate the estimated cost based on the flags set in idxFlags. */
223625 if( bSeenEq ){
223626 pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
223627 if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
223628 }else if( bSeenLt && bSeenGt ){
223629 pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
223630 }else if( bSeenLt || bSeenGt ){
223631 pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
223632 }else{
223633 pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
223634 }
223635
223636 pInfo->idxNum = idxFlags;
223637 return SQLITE_OK;
223638}
223639
223640static int fts5NewTransaction(Fts5FullTable *pTab){
223641 Fts5Cursor *pCsr;
223642 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
223643 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
223644 }
223645 return sqlite3Fts5StorageReset(pTab->pStorage);
223646}
223647
223648/*
223649** Implementation of xOpen method.
223650*/
223651static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
223652 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
223653 Fts5Config *pConfig = pTab->p.pConfig;
223654 Fts5Cursor *pCsr = 0; /* New cursor object */
223655 sqlite3_int64 nByte; /* Bytes of space to allocate */
223656 int rc; /* Return code */
223657
223658 rc = fts5NewTransaction(pTab);
223659 if( rc==SQLITE_OK ){
223660 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
223661 pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
223662 if( pCsr ){
223663 Fts5Global *pGlobal = pTab->pGlobal;
223664 memset(pCsr, 0, (size_t)nByte);
223665 pCsr->aColumnSize = (int*)&pCsr[1];
223666 pCsr->pNext = pGlobal->pCsr;
223667 pGlobal->pCsr = pCsr;
223668 pCsr->iCsrId = ++pGlobal->iNextId;
223669 }else{
223670 rc = SQLITE_NOMEM;
223671 }
223672 }
223673 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
223674 return rc;
223675}
223676
223677static int fts5StmtType(Fts5Cursor *pCsr){
223678 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
223679 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
223680 }
223681 return FTS5_STMT_LOOKUP;
223682}
223683
223684/*
223685** This function is called after the cursor passed as the only argument
223686** is moved to point at a different row. It clears all cached data
223687** specific to the previous row stored by the cursor object.
223688*/
223689static void fts5CsrNewrow(Fts5Cursor *pCsr){
223690 CsrFlagSet(pCsr,
223691 FTS5CSR_REQUIRE_CONTENT
223692 | FTS5CSR_REQUIRE_DOCSIZE
223693 | FTS5CSR_REQUIRE_INST
223694 | FTS5CSR_REQUIRE_POSLIST
223695 );
223696}
223697
223698static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
223699 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
223700 Fts5Auxdata *pData;
223701 Fts5Auxdata *pNext;
223702
223703 sqlite3_free(pCsr->aInstIter);
223704 sqlite3_free(pCsr->aInst);
223705 if( pCsr->pStmt ){
223706 int eStmt = fts5StmtType(pCsr);
223707 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
223708 }
223709 if( pCsr->pSorter ){
223710 Fts5Sorter *pSorter = pCsr->pSorter;
223711 sqlite3_finalize(pSorter->pStmt);
223712 sqlite3_free(pSorter);
223713 }
223714
223715 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
223716 sqlite3Fts5ExprFree(pCsr->pExpr);
223717 }
223718
223719 for(pData=pCsr->pAuxdata; pData; pData=pNext){
223720 pNext = pData->pNext;
223721 if( pData->xDelete ) pData->xDelete(pData->pPtr);
223722 sqlite3_free(pData);
223723 }
223724
223725 sqlite3_finalize(pCsr->pRankArgStmt);
223726 sqlite3_free(pCsr->apRankArg);
223727
223728 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
223729 sqlite3_free(pCsr->zRank);
223730 sqlite3_free(pCsr->zRankArgs);
223731 }
223732
223733 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
223734 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
223735}
223736
223737
223738/*
223739** Close the cursor. For additional information see the documentation
223740** on the xClose method of the virtual table interface.
223741*/
223742static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
223743 if( pCursor ){
223744 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
223745 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223746 Fts5Cursor **pp;
223747
223748 fts5FreeCursorComponents(pCsr);
223749 /* Remove the cursor from the Fts5Global.pCsr list */
223750 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
223751 *pp = pCsr->pNext;
223752
223753 sqlite3_free(pCsr);
223754 }
223755 return SQLITE_OK;
223756}
223757
223758static int fts5SorterNext(Fts5Cursor *pCsr){
223759 Fts5Sorter *pSorter = pCsr->pSorter;
223760 int rc;
223761
223762 rc = sqlite3_step(pSorter->pStmt);
223763 if( rc==SQLITE_DONE ){
223764 rc = SQLITE_OK;
223765 CsrFlagSet(pCsr, FTS5CSR_EOF);
223766 }else if( rc==SQLITE_ROW ){
223767 const u8 *a;
223768 const u8 *aBlob;
223769 int nBlob;
223770 int i;
223771 int iOff = 0;
223772 rc = SQLITE_OK;
223773
223774 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
223775 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
223776 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
223777
223778 /* nBlob==0 in detail=none mode. */
223779 if( nBlob>0 ){
223780 for(i=0; i<(pSorter->nIdx-1); i++){
223781 int iVal;
223782 a += fts5GetVarint32(a, iVal);
223783 iOff += iVal;
223784 pSorter->aIdx[i] = iOff;
223785 }
223786 pSorter->aIdx[i] = &aBlob[nBlob] - a;
223787 pSorter->aPoslist = a;
223788 }
223789
223790 fts5CsrNewrow(pCsr);
223791 }
223792
223793 return rc;
223794}
223795
223796
223797/*
223798** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
223799** open on table pTab.
223800*/
223801static void fts5TripCursors(Fts5FullTable *pTab){
223802 Fts5Cursor *pCsr;
223803 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
223804 if( pCsr->ePlan==FTS5_PLAN_MATCH
223805 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
223806 ){
223807 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
223808 }
223809 }
223810}
223811
223812/*
223813** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
223814** argument, close and reopen all Fts5IndexIter iterators that the cursor
223815** is using. Then attempt to move the cursor to a rowid equal to or laster
223816** (in the cursors sort order - ASC or DESC) than the current rowid.
223817**
223818** If the new rowid is not equal to the old, set output parameter *pbSkip
223819** to 1 before returning. Otherwise, leave it unchanged.
223820**
223821** Return SQLITE_OK if successful or if no reseek was required, or an
223822** error code if an error occurred.
223823*/
223824static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
223825 int rc = SQLITE_OK;
223826 assert( *pbSkip==0 );
223827 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
223828 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
223829 int bDesc = pCsr->bDesc;
223830 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
223831
223832 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
223833 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
223834 *pbSkip = 1;
223835 }
223836
223837 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
223838 fts5CsrNewrow(pCsr);
223839 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
223840 CsrFlagSet(pCsr, FTS5CSR_EOF);
223841 *pbSkip = 1;
223842 }
223843 }
223844 return rc;
223845}
223846
223847
223848/*
223849** Advance the cursor to the next row in the table that matches the
223850** search criteria.
223851**
223852** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
223853** even if we reach end-of-file. The fts5EofMethod() will be called
223854** subsequently to determine whether or not an EOF was hit.
223855*/
223856static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
223857 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
223858 int rc;
223859
223860 assert( (pCsr->ePlan<3)==
223861 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
223862 );
223863 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
223864
223865 if( pCsr->ePlan<3 ){
223866 int bSkip = 0;
223867 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
223868 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
223869 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
223870 fts5CsrNewrow(pCsr);
223871 }else{
223872 switch( pCsr->ePlan ){
223873 case FTS5_PLAN_SPECIAL: {
223874 CsrFlagSet(pCsr, FTS5CSR_EOF);
223875 rc = SQLITE_OK;
223876 break;
223877 }
223878
223879 case FTS5_PLAN_SORTED_MATCH: {
223880 rc = fts5SorterNext(pCsr);
223881 break;
223882 }
223883
223884 default: {
223885 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
223886 pConfig->bLock++;
223887 rc = sqlite3_step(pCsr->pStmt);
223888 pConfig->bLock--;
223889 if( rc!=SQLITE_ROW ){
223890 CsrFlagSet(pCsr, FTS5CSR_EOF);
223891 rc = sqlite3_reset(pCsr->pStmt);
223892 if( rc!=SQLITE_OK ){
223893 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
223894 "%s", sqlite3_errmsg(pConfig->db)
223895 );
223896 }
223897 }else{
223898 rc = SQLITE_OK;
223899 }
223900 break;
223901 }
223902 }
223903 }
223904
223905 return rc;
223906}
223907
223908
223909static int fts5PrepareStatement(
223910 sqlite3_stmt **ppStmt,
223911 Fts5Config *pConfig,
223912 const char *zFmt,
223913 ...
223914){
223915 sqlite3_stmt *pRet = 0;
223916 int rc;
223917 char *zSql;
223918 va_list ap;
223919
223920 va_start(ap, zFmt);
223921 zSql = sqlite3_vmprintf(zFmt, ap);
223922 if( zSql==0 ){
223923 rc = SQLITE_NOMEM;
223924 }else{
223925 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
223926 SQLITE_PREPARE_PERSISTENT, &pRet, 0);
223927 if( rc!=SQLITE_OK ){
223928 *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
223929 }
223930 sqlite3_free(zSql);
223931 }
223932
223933 va_end(ap);
223934 *ppStmt = pRet;
223935 return rc;
223936}
223937
223938static int fts5CursorFirstSorted(
223939 Fts5FullTable *pTab,
223940 Fts5Cursor *pCsr,
223941 int bDesc
223942){
223943 Fts5Config *pConfig = pTab->p.pConfig;
223944 Fts5Sorter *pSorter;
223945 int nPhrase;
223946 sqlite3_int64 nByte;
223947 int rc;
223948 const char *zRank = pCsr->zRank;
223949 const char *zRankArgs = pCsr->zRankArgs;
223950
223951 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
223952 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
223953 pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
223954 if( pSorter==0 ) return SQLITE_NOMEM;
223955 memset(pSorter, 0, (size_t)nByte);
223956 pSorter->nIdx = nPhrase;
223957
223958 /* TODO: It would be better to have some system for reusing statement
223959 ** handles here, rather than preparing a new one for each query. But that
223960 ** is not possible as SQLite reference counts the virtual table objects.
223961 ** And since the statement required here reads from this very virtual
223962 ** table, saving it creates a circular reference.
223963 **
223964 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
223965 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
223966 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
223967 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
223968 (zRankArgs ? ", " : ""),
223969 (zRankArgs ? zRankArgs : ""),
223970 bDesc ? "DESC" : "ASC"
223971 );
223972
223973 pCsr->pSorter = pSorter;
223974 if( rc==SQLITE_OK ){
223975 assert( pTab->pSortCsr==0 );
223976 pTab->pSortCsr = pCsr;
223977 rc = fts5SorterNext(pCsr);
223978 pTab->pSortCsr = 0;
223979 }
223980
223981 if( rc!=SQLITE_OK ){
223982 sqlite3_finalize(pSorter->pStmt);
223983 sqlite3_free(pSorter);
223984 pCsr->pSorter = 0;
223985 }
223986
223987 return rc;
223988}
223989
223990static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
223991 int rc;
223992 Fts5Expr *pExpr = pCsr->pExpr;
223993 rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
223994 if( sqlite3Fts5ExprEof(pExpr) ){
223995 CsrFlagSet(pCsr, FTS5CSR_EOF);
223996 }
223997 fts5CsrNewrow(pCsr);
223998 return rc;
223999}
224000
224001/*
224002** Process a "special" query. A special query is identified as one with a
224003** MATCH expression that begins with a '*' character. The remainder of
224004** the text passed to the MATCH operator are used as the special query
224005** parameters.
224006*/
224007static int fts5SpecialMatch(
224008 Fts5FullTable *pTab,
224009 Fts5Cursor *pCsr,
224010 const char *zQuery
224011){
224012 int rc = SQLITE_OK; /* Return code */
224013 const char *z = zQuery; /* Special query text */
224014 int n; /* Number of bytes in text at z */
224015
224016 while( z[0]==' ' ) z++;
224017 for(n=0; z[n] && z[n]!=' '; n++);
224018
224019 assert( pTab->p.base.zErrMsg==0 );
224020 pCsr->ePlan = FTS5_PLAN_SPECIAL;
224021
224022 if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
224023 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
224024 }
224025 else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
224026 pCsr->iSpecial = pCsr->iCsrId;
224027 }
224028 else{
224029 /* An unrecognized directive. Return an error message. */
224030 pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
224031 rc = SQLITE_ERROR;
224032 }
224033
224034 return rc;
224035}
224036
224037/*
224038** Search for an auxiliary function named zName that can be used with table
224039** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
224040** structure. Otherwise, if no such function exists, return NULL.
224041*/
224042static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
224043 Fts5Auxiliary *pAux;
224044
224045 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
224046 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
224047 }
224048
224049 /* No function of the specified name was found. Return 0. */
224050 return 0;
224051}
224052
224053
224054static int fts5FindRankFunction(Fts5Cursor *pCsr){
224055 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224056 Fts5Config *pConfig = pTab->p.pConfig;
224057 int rc = SQLITE_OK;
224058 Fts5Auxiliary *pAux = 0;
224059 const char *zRank = pCsr->zRank;
224060 const char *zRankArgs = pCsr->zRankArgs;
224061
224062 if( zRankArgs ){
224063 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
224064 if( zSql ){
224065 sqlite3_stmt *pStmt = 0;
224066 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
224067 SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
224068 sqlite3_free(zSql);
224069 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
224070 if( rc==SQLITE_OK ){
224071 if( SQLITE_ROW==sqlite3_step(pStmt) ){
224072 sqlite3_int64 nByte;
224073 pCsr->nRankArg = sqlite3_column_count(pStmt);
224074 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
224075 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
224076 if( rc==SQLITE_OK ){
224077 int i;
224078 for(i=0; i<pCsr->nRankArg; i++){
224079 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
224080 }
224081 }
224082 pCsr->pRankArgStmt = pStmt;
224083 }else{
224084 rc = sqlite3_finalize(pStmt);
224085 assert( rc!=SQLITE_OK );
224086 }
224087 }
224088 }
224089 }
224090
224091 if( rc==SQLITE_OK ){
224092 pAux = fts5FindAuxiliary(pTab, zRank);
224093 if( pAux==0 ){
224094 assert( pTab->p.base.zErrMsg==0 );
224095 pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
224096 rc = SQLITE_ERROR;
224097 }
224098 }
224099
224100 pCsr->pRank = pAux;
224101 return rc;
224102}
224103
224104
224105static int fts5CursorParseRank(
224106 Fts5Config *pConfig,
224107 Fts5Cursor *pCsr,
224108 sqlite3_value *pRank
224109){
224110 int rc = SQLITE_OK;
224111 if( pRank ){
224112 const char *z = (const char*)sqlite3_value_text(pRank);
224113 char *zRank = 0;
224114 char *zRankArgs = 0;
224115
224116 if( z==0 ){
224117 if( sqlite3_value_type(pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
224118 }else{
224119 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
224120 }
224121 if( rc==SQLITE_OK ){
224122 pCsr->zRank = zRank;
224123 pCsr->zRankArgs = zRankArgs;
224124 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
224125 }else if( rc==SQLITE_ERROR ){
224126 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
224127 "parse error in rank function: %s", z
224128 );
224129 }
224130 }else{
224131 if( pConfig->zRank ){
224132 pCsr->zRank = (char*)pConfig->zRank;
224133 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
224134 }else{
224135 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
224136 pCsr->zRankArgs = 0;
224137 }
224138 }
224139 return rc;
224140}
224141
224142static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
224143 if( pVal ){
224144 int eType = sqlite3_value_numeric_type(pVal);
224145 if( eType==SQLITE_INTEGER ){
224146 return sqlite3_value_int64(pVal);
224147 }
224148 }
224149 return iDefault;
224150}
224151
224152/*
224153** This is the xFilter interface for the virtual table. See
224154** the virtual table xFilter method documentation for additional
224155** information.
224156**
224157** There are three possible query strategies:
224158**
224159** 1. Full-text search using a MATCH operator.
224160** 2. A by-rowid lookup.
224161** 3. A full-table scan.
224162*/
224163static int fts5FilterMethod(
224164 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
224165 int idxNum, /* Strategy index */
224166 const char *idxStr, /* Unused */
224167 int nVal, /* Number of elements in apVal */
224168 sqlite3_value **apVal /* Arguments for the indexing scheme */
224169){
224170 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
224171 Fts5Config *pConfig = pTab->p.pConfig;
224172 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
224173 int rc = SQLITE_OK; /* Error code */
224174 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
224175 int bOrderByRank; /* True if ORDER BY rank */
224176 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
224177 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
224178 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
224179 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
224180 int iCol; /* Column on LHS of MATCH operator */
224181 char **pzErrmsg = pConfig->pzErrmsg;
224182 int i;
224183 int iIdxStr = 0;
224184 Fts5Expr *pExpr = 0;
224185
224186 if( pConfig->bLock ){
224187 pTab->p.base.zErrMsg = sqlite3_mprintf(
224188 "recursively defined fts5 content table"
224189 );
224190 return SQLITE_ERROR;
224191 }
224192
224193 if( pCsr->ePlan ){
224194 fts5FreeCursorComponents(pCsr);
224195 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
224196 }
224197
224198 assert( pCsr->pStmt==0 );
224199 assert( pCsr->pExpr==0 );
224200 assert( pCsr->csrflags==0 );
224201 assert( pCsr->pRank==0 );
224202 assert( pCsr->zRank==0 );
224203 assert( pCsr->zRankArgs==0 );
224204 assert( pTab->pSortCsr==0 || nVal==0 );
224205
224206 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
224207 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
224208
224209 /* Decode the arguments passed through to this function. */
224210 for(i=0; i<nVal; i++){
224211 switch( idxStr[iIdxStr++] ){
224212 case 'r':
224213 pRank = apVal[i];
224214 break;
224215 case 'm': {
224216 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
224217 if( zText==0 ) zText = "";
224218
224219 if( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ){
224220 iCol = 0;
224221 do{
224222 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
224223 iIdxStr++;
224224 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
224225 }else{
224226 iCol = pConfig->nCol;
224227 }
224228
224229 if( zText[0]=='*' ){
224230 /* The user has issued a query of the form "MATCH '*...'". This
224231 ** indicates that the MATCH expression is not a full text query,
224232 ** but a request for an internal parameter. */
224233 rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
224234 goto filter_out;
224235 }else{
224236 char **pzErr = &pTab->p.base.zErrMsg;
224237 rc = sqlite3Fts5ExprNew(pConfig, iCol, zText, &pExpr, pzErr);
224238 if( rc==SQLITE_OK ){
224239 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
224240 pExpr = 0;
224241 }
224242 if( rc!=SQLITE_OK ) goto filter_out;
224243 }
224244
224245 break;
224246 }
224247 case '=':
224248 pRowidEq = apVal[i];
224249 break;
224250 case '<':
224251 pRowidLe = apVal[i];
224252 break;
224253 default: assert( idxStr[iIdxStr-1]=='>' );
224254 pRowidGe = apVal[i];
224255 break;
224256 }
224257 }
224258 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
224259 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
224260
224261 /* Set the cursor upper and lower rowid limits. Only some strategies
224262 ** actually use them. This is ok, as the xBestIndex() method leaves the
224263 ** sqlite3_index_constraint.omit flag clear for range constraints
224264 ** on the rowid field. */
224265 if( pRowidEq ){
224266 pRowidLe = pRowidGe = pRowidEq;
224267 }
224268 if( bDesc ){
224269 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
224270 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
224271 }else{
224272 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
224273 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
224274 }
224275
224276 if( pTab->pSortCsr ){
224277 /* If pSortCsr is non-NULL, then this call is being made as part of
224278 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
224279 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
224280 ** return results to the user for this query. The current cursor
224281 ** (pCursor) is used to execute the query issued by function
224282 ** fts5CursorFirstSorted() above. */
224283 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
224284 assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
224285 assert( pCsr->iLastRowid==LARGEST_INT64 );
224286 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
224287 if( pTab->pSortCsr->bDesc ){
224288 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
224289 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
224290 }else{
224291 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
224292 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
224293 }
224294 pCsr->ePlan = FTS5_PLAN_SOURCE;
224295 pCsr->pExpr = pTab->pSortCsr->pExpr;
224296 rc = fts5CursorFirst(pTab, pCsr, bDesc);
224297 }else if( pCsr->pExpr ){
224298 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
224299 if( rc==SQLITE_OK ){
224300 if( bOrderByRank ){
224301 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
224302 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
224303 }else{
224304 pCsr->ePlan = FTS5_PLAN_MATCH;
224305 rc = fts5CursorFirst(pTab, pCsr, bDesc);
224306 }
224307 }
224308 }else if( pConfig->zContent==0 ){
224309 *pConfig->pzErrmsg = sqlite3_mprintf(
224310 "%s: table does not support scanning", pConfig->zName
224311 );
224312 rc = SQLITE_ERROR;
224313 }else{
224314 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
224315 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
224316 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
224317 rc = sqlite3Fts5StorageStmt(
224318 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
224319 );
224320 if( rc==SQLITE_OK ){
224321 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
224322 sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
224323 }else{
224324 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
224325 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
224326 }
224327 rc = fts5NextMethod(pCursor);
224328 }
224329 }
224330
224331 filter_out:
224332 sqlite3Fts5ExprFree(pExpr);
224333 pConfig->pzErrmsg = pzErrmsg;
224334 return rc;
224335}
224336
224337/*
224338** This is the xEof method of the virtual table. SQLite calls this
224339** routine to find out if it has reached the end of a result set.
224340*/
224341static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
224342 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
224343 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
224344}
224345
224346/*
224347** Return the rowid that the cursor currently points to.
224348*/
224349static i64 fts5CursorRowid(Fts5Cursor *pCsr){
224350 assert( pCsr->ePlan==FTS5_PLAN_MATCH
224351 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
224352 || pCsr->ePlan==FTS5_PLAN_SOURCE
224353 );
224354 if( pCsr->pSorter ){
224355 return pCsr->pSorter->iRowid;
224356 }else{
224357 return sqlite3Fts5ExprRowid(pCsr->pExpr);
224358 }
224359}
224360
224361/*
224362** This is the xRowid method. The SQLite core calls this routine to
224363** retrieve the rowid for the current row of the result set. fts5
224364** exposes %_content.rowid as the rowid for the virtual table. The
224365** rowid should be written to *pRowid.
224366*/
224367static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
224368 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
224369 int ePlan = pCsr->ePlan;
224370
224371 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
224372 switch( ePlan ){
224373 case FTS5_PLAN_SPECIAL:
224374 *pRowid = 0;
224375 break;
224376
224377 case FTS5_PLAN_SOURCE:
224378 case FTS5_PLAN_MATCH:
224379 case FTS5_PLAN_SORTED_MATCH:
224380 *pRowid = fts5CursorRowid(pCsr);
224381 break;
224382
224383 default:
224384 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
224385 break;
224386 }
224387
224388 return SQLITE_OK;
224389}
224390
224391/*
224392** If the cursor requires seeking (bSeekRequired flag is set), seek it.
224393** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
224394**
224395** If argument bErrormsg is true and an error occurs, an error message may
224396** be left in sqlite3_vtab.zErrMsg.
224397*/
224398static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
224399 int rc = SQLITE_OK;
224400
224401 /* If the cursor does not yet have a statement handle, obtain one now. */
224402 if( pCsr->pStmt==0 ){
224403 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224404 int eStmt = fts5StmtType(pCsr);
224405 rc = sqlite3Fts5StorageStmt(
224406 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
224407 );
224408 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 );
224409 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
224410 }
224411
224412 if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
224413 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
224414 assert( pCsr->pExpr );
224415 sqlite3_reset(pCsr->pStmt);
224416 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
224417 pTab->pConfig->bLock++;
224418 rc = sqlite3_step(pCsr->pStmt);
224419 pTab->pConfig->bLock--;
224420 if( rc==SQLITE_ROW ){
224421 rc = SQLITE_OK;
224422 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
224423 }else{
224424 rc = sqlite3_reset(pCsr->pStmt);
224425 if( rc==SQLITE_OK ){
224426 rc = FTS5_CORRUPT;
224427 }else if( pTab->pConfig->pzErrmsg ){
224428 *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
224429 "%s", sqlite3_errmsg(pTab->pConfig->db)
224430 );
224431 }
224432 }
224433 }
224434 return rc;
224435}
224436
224437static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
224438 va_list ap; /* ... printf arguments */
224439 va_start(ap, zFormat);
224440 assert( p->p.base.zErrMsg==0 );
224441 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
224442 va_end(ap);
224443}
224444
224445/*
224446** This function is called to handle an FTS INSERT command. In other words,
224447** an INSERT statement of the form:
224448**
224449** INSERT INTO fts(fts) VALUES($pCmd)
224450** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
224451**
224452** Argument pVal is the value assigned to column "fts" by the INSERT
224453** statement. This function returns SQLITE_OK if successful, or an SQLite
224454** error code if an error occurs.
224455**
224456** The commands implemented by this function are documented in the "Special
224457** INSERT Directives" section of the documentation. It should be updated if
224458** more commands are added to this function.
224459*/
224460static int fts5SpecialInsert(
224461 Fts5FullTable *pTab, /* Fts5 table object */
224462 const char *zCmd, /* Text inserted into table-name column */
224463 sqlite3_value *pVal /* Value inserted into rank column */
224464){
224465 Fts5Config *pConfig = pTab->p.pConfig;
224466 int rc = SQLITE_OK;
224467 int bError = 0;
224468
224469 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
224470 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
224471 fts5SetVtabError(pTab,
224472 "'delete-all' may only be used with a "
224473 "contentless or external content fts5 table"
224474 );
224475 rc = SQLITE_ERROR;
224476 }else{
224477 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
224478 }
224479 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
224480 if( pConfig->eContent==FTS5_CONTENT_NONE ){
224481 fts5SetVtabError(pTab,
224482 "'rebuild' may not be used with a contentless fts5 table"
224483 );
224484 rc = SQLITE_ERROR;
224485 }else{
224486 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
224487 }
224488 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
224489 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
224490 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
224491 int nMerge = sqlite3_value_int(pVal);
224492 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
224493 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
224494 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
224495#ifdef SQLITE_DEBUG
224496 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
224497 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
224498#endif
224499 }else{
224500 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
224501 if( rc==SQLITE_OK ){
224502 rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
224503 }
224504 if( rc==SQLITE_OK ){
224505 if( bError ){
224506 rc = SQLITE_ERROR;
224507 }else{
224508 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
224509 }
224510 }
224511 }
224512 return rc;
224513}
224514
224515static int fts5SpecialDelete(
224516 Fts5FullTable *pTab,
224517 sqlite3_value **apVal
224518){
224519 int rc = SQLITE_OK;
224520 int eType1 = sqlite3_value_type(apVal[1]);
224521 if( eType1==SQLITE_INTEGER ){
224522 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
224523 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
224524 }
224525 return rc;
224526}
224527
224528static void fts5StorageInsert(
224529 int *pRc,
224530 Fts5FullTable *pTab,
224531 sqlite3_value **apVal,
224532 i64 *piRowid
224533){
224534 int rc = *pRc;
224535 if( rc==SQLITE_OK ){
224536 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
224537 }
224538 if( rc==SQLITE_OK ){
224539 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
224540 }
224541 *pRc = rc;
224542}
224543
224544/*
224545** This function is the implementation of the xUpdate callback used by
224546** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
224547** inserted, updated or deleted.
224548**
224549** A delete specifies a single argument - the rowid of the row to remove.
224550**
224551** Update and insert operations pass:
224552**
224553** 1. The "old" rowid, or NULL.
224554** 2. The "new" rowid.
224555** 3. Values for each of the nCol matchable columns.
224556** 4. Values for the two hidden columns (<tablename> and "rank").
224557*/
224558static int fts5UpdateMethod(
224559 sqlite3_vtab *pVtab, /* Virtual table handle */
224560 int nArg, /* Size of argument array */
224561 sqlite3_value **apVal, /* Array of arguments */
224562 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
224563){
224564 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224565 Fts5Config *pConfig = pTab->p.pConfig;
224566 int eType0; /* value_type() of apVal[0] */
224567 int rc = SQLITE_OK; /* Return code */
224568
224569 /* A transaction must be open when this is called. */
224570 assert( pTab->ts.eState==1 );
224571
224572 assert( pVtab->zErrMsg==0 );
224573 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
224574 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER
224575 || sqlite3_value_type(apVal[0])==SQLITE_NULL
224576 );
224577 assert( pTab->p.pConfig->pzErrmsg==0 );
224578 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
224579
224580 /* Put any active cursors into REQUIRE_SEEK state. */
224581 fts5TripCursors(pTab);
224582
224583 eType0 = sqlite3_value_type(apVal[0]);
224584 if( eType0==SQLITE_NULL
224585 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL
224586 ){
224587 /* A "special" INSERT op. These are handled separately. */
224588 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
224589 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
224590 && 0==sqlite3_stricmp("delete", z)
224591 ){
224592 rc = fts5SpecialDelete(pTab, apVal);
224593 }else{
224594 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
224595 }
224596 }else{
224597 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
224598 ** any conflict on the rowid value must be detected before any
224599 ** modifications are made to the database file. There are 4 cases:
224600 **
224601 ** 1) DELETE
224602 ** 2) UPDATE (rowid not modified)
224603 ** 3) UPDATE (rowid modified)
224604 ** 4) INSERT
224605 **
224606 ** Cases 3 and 4 may violate the rowid constraint.
224607 */
224608 int eConflict = SQLITE_ABORT;
224609 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
224610 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
224611 }
224612
224613 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
224614 assert( nArg!=1 || eType0==SQLITE_INTEGER );
224615
224616 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
224617 ** This is not suported. */
224618 if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
224619 pTab->p.base.zErrMsg = sqlite3_mprintf(
224620 "cannot %s contentless fts5 table: %s",
224621 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
224622 );
224623 rc = SQLITE_ERROR;
224624 }
224625
224626 /* DELETE */
224627 else if( nArg==1 ){
224628 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
224629 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
224630 }
224631
224632 /* INSERT or UPDATE */
224633 else{
224634 int eType1 = sqlite3_value_numeric_type(apVal[1]);
224635
224636 if( eType1!=SQLITE_INTEGER && eType1!=SQLITE_NULL ){
224637 rc = SQLITE_MISMATCH;
224638 }
224639
224640 else if( eType0!=SQLITE_INTEGER ){
224641 /* If this is a REPLACE, first remove the current entry (if any) */
224642 if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){
224643 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
224644 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
224645 }
224646 fts5StorageInsert(&rc, pTab, apVal, pRowid);
224647 }
224648
224649 /* UPDATE */
224650 else{
224651 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
224652 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
224653 if( eType1==SQLITE_INTEGER && iOld!=iNew ){
224654 if( eConflict==SQLITE_REPLACE ){
224655 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
224656 if( rc==SQLITE_OK ){
224657 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
224658 }
224659 fts5StorageInsert(&rc, pTab, apVal, pRowid);
224660 }else{
224661 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
224662 if( rc==SQLITE_OK ){
224663 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
224664 }
224665 if( rc==SQLITE_OK ){
224666 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal,*pRowid);
224667 }
224668 }
224669 }else{
224670 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
224671 fts5StorageInsert(&rc, pTab, apVal, pRowid);
224672 }
224673 }
224674 }
224675 }
224676
224677 pTab->p.pConfig->pzErrmsg = 0;
224678 return rc;
224679}
224680
224681/*
224682** Implementation of xSync() method.
224683*/
224684static int fts5SyncMethod(sqlite3_vtab *pVtab){
224685 int rc;
224686 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224687 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
224688 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
224689 fts5TripCursors(pTab);
224690 rc = sqlite3Fts5StorageSync(pTab->pStorage);
224691 pTab->p.pConfig->pzErrmsg = 0;
224692 return rc;
224693}
224694
224695/*
224696** Implementation of xBegin() method.
224697*/
224698static int fts5BeginMethod(sqlite3_vtab *pVtab){
224699 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
224700 fts5NewTransaction((Fts5FullTable*)pVtab);
224701 return SQLITE_OK;
224702}
224703
224704/*
224705** Implementation of xCommit() method. This is a no-op. The contents of
224706** the pending-terms hash-table have already been flushed into the database
224707** by fts5SyncMethod().
224708*/
224709static int fts5CommitMethod(sqlite3_vtab *pVtab){
224710 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
224711 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
224712 return SQLITE_OK;
224713}
224714
224715/*
224716** Implementation of xRollback(). Discard the contents of the pending-terms
224717** hash-table. Any changes made to the database are reverted by SQLite.
224718*/
224719static int fts5RollbackMethod(sqlite3_vtab *pVtab){
224720 int rc;
224721 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
224722 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
224723 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
224724 return rc;
224725}
224726
224727static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
224728
224729static void *fts5ApiUserData(Fts5Context *pCtx){
224730 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224731 return pCsr->pAux->pUserData;
224732}
224733
224734static int fts5ApiColumnCount(Fts5Context *pCtx){
224735 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224736 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
224737}
224738
224739static int fts5ApiColumnTotalSize(
224740 Fts5Context *pCtx,
224741 int iCol,
224742 sqlite3_int64 *pnToken
224743){
224744 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224745 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224746 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
224747}
224748
224749static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
224750 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224751 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224752 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
224753}
224754
224755static int fts5ApiTokenize(
224756 Fts5Context *pCtx,
224757 const char *pText, int nText,
224758 void *pUserData,
224759 int (*xToken)(void*, int, const char*, int, int, int)
224760){
224761 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224762 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
224763 return sqlite3Fts5Tokenize(
224764 pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken
224765 );
224766}
224767
224768static int fts5ApiPhraseCount(Fts5Context *pCtx){
224769 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224770 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
224771}
224772
224773static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
224774 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224775 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
224776}
224777
224778static int fts5ApiColumnText(
224779 Fts5Context *pCtx,
224780 int iCol,
224781 const char **pz,
224782 int *pn
224783){
224784 int rc = SQLITE_OK;
224785 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224786 if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
224787 || pCsr->ePlan==FTS5_PLAN_SPECIAL
224788 ){
224789 *pz = 0;
224790 *pn = 0;
224791 }else{
224792 rc = fts5SeekCursor(pCsr, 0);
224793 if( rc==SQLITE_OK ){
224794 *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
224795 *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
224796 }
224797 }
224798 return rc;
224799}
224800
224801static int fts5CsrPoslist(
224802 Fts5Cursor *pCsr,
224803 int iPhrase,
224804 const u8 **pa,
224805 int *pn
224806){
224807 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
224808 int rc = SQLITE_OK;
224809 int bLive = (pCsr->pSorter==0);
224810
224811 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
224812
224813 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
224814 Fts5PoslistPopulator *aPopulator;
224815 int i;
224816 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
224817 if( aPopulator==0 ) rc = SQLITE_NOMEM;
224818 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
224819 int n; const char *z;
224820 rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
224821 if( rc==SQLITE_OK ){
224822 rc = sqlite3Fts5ExprPopulatePoslists(
224823 pConfig, pCsr->pExpr, aPopulator, i, z, n
224824 );
224825 }
224826 }
224827 sqlite3_free(aPopulator);
224828
224829 if( pCsr->pSorter ){
224830 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
224831 }
224832 }
224833 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
224834 }
224835
224836 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
224837 Fts5Sorter *pSorter = pCsr->pSorter;
224838 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
224839 *pn = pSorter->aIdx[iPhrase] - i1;
224840 *pa = &pSorter->aPoslist[i1];
224841 }else{
224842 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
224843 }
224844
224845 return rc;
224846}
224847
224848/*
224849** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
224850** correctly for the current view. Return SQLITE_OK if successful, or an
224851** SQLite error code otherwise.
224852*/
224853static int fts5CacheInstArray(Fts5Cursor *pCsr){
224854 int rc = SQLITE_OK;
224855 Fts5PoslistReader *aIter; /* One iterator for each phrase */
224856 int nIter; /* Number of iterators/phrases */
224857 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
224858
224859 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
224860 if( pCsr->aInstIter==0 ){
224861 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
224862 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
224863 }
224864 aIter = pCsr->aInstIter;
224865
224866 if( aIter ){
224867 int nInst = 0; /* Number instances seen so far */
224868 int i;
224869
224870 /* Initialize all iterators */
224871 for(i=0; i<nIter && rc==SQLITE_OK; i++){
224872 const u8 *a;
224873 int n;
224874 rc = fts5CsrPoslist(pCsr, i, &a, &n);
224875 if( rc==SQLITE_OK ){
224876 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
224877 }
224878 }
224879
224880 if( rc==SQLITE_OK ){
224881 while( 1 ){
224882 int *aInst;
224883 int iBest = -1;
224884 for(i=0; i<nIter; i++){
224885 if( (aIter[i].bEof==0)
224886 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
224887 ){
224888 iBest = i;
224889 }
224890 }
224891 if( iBest<0 ) break;
224892
224893 nInst++;
224894 if( nInst>=pCsr->nInstAlloc ){
224895 pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
224896 aInst = (int*)sqlite3_realloc64(
224897 pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
224898 );
224899 if( aInst ){
224900 pCsr->aInst = aInst;
224901 }else{
224902 rc = SQLITE_NOMEM;
224903 break;
224904 }
224905 }
224906
224907 aInst = &pCsr->aInst[3 * (nInst-1)];
224908 aInst[0] = iBest;
224909 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
224910 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
224911 if( aInst[1]<0 || aInst[1]>=nCol ){
224912 rc = FTS5_CORRUPT;
224913 break;
224914 }
224915 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
224916 }
224917 }
224918
224919 pCsr->nInstCount = nInst;
224920 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
224921 }
224922 return rc;
224923}
224924
224925static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
224926 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224927 int rc = SQLITE_OK;
224928 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
224929 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
224930 *pnInst = pCsr->nInstCount;
224931 }
224932 return rc;
224933}
224934
224935static int fts5ApiInst(
224936 Fts5Context *pCtx,
224937 int iIdx,
224938 int *piPhrase,
224939 int *piCol,
224940 int *piOff
224941){
224942 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224943 int rc = SQLITE_OK;
224944 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
224945 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
224946 ){
224947 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
224948 rc = SQLITE_RANGE;
224949#if 0
224950 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
224951 *piPhrase = pCsr->aInst[iIdx*3];
224952 *piCol = pCsr->aInst[iIdx*3 + 2];
224953 *piOff = -1;
224954#endif
224955 }else{
224956 *piPhrase = pCsr->aInst[iIdx*3];
224957 *piCol = pCsr->aInst[iIdx*3 + 1];
224958 *piOff = pCsr->aInst[iIdx*3 + 2];
224959 }
224960 }
224961 return rc;
224962}
224963
224964static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
224965 return fts5CursorRowid((Fts5Cursor*)pCtx);
224966}
224967
224968static int fts5ColumnSizeCb(
224969 void *pContext, /* Pointer to int */
224970 int tflags,
224971 const char *pUnused, /* Buffer containing token */
224972 int nUnused, /* Size of token in bytes */
224973 int iUnused1, /* Start offset of token */
224974 int iUnused2 /* End offset of token */
224975){
224976 int *pCnt = (int*)pContext;
224977 UNUSED_PARAM2(pUnused, nUnused);
224978 UNUSED_PARAM2(iUnused1, iUnused2);
224979 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
224980 (*pCnt)++;
224981 }
224982 return SQLITE_OK;
224983}
224984
224985static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
224986 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
224987 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
224988 Fts5Config *pConfig = pTab->p.pConfig;
224989 int rc = SQLITE_OK;
224990
224991 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
224992 if( pConfig->bColumnsize ){
224993 i64 iRowid = fts5CursorRowid(pCsr);
224994 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
224995 }else if( pConfig->zContent==0 ){
224996 int i;
224997 for(i=0; i<pConfig->nCol; i++){
224998 if( pConfig->abUnindexed[i]==0 ){
224999 pCsr->aColumnSize[i] = -1;
225000 }
225001 }
225002 }else{
225003 int i;
225004 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
225005 if( pConfig->abUnindexed[i]==0 ){
225006 const char *z; int n;
225007 void *p = (void*)(&pCsr->aColumnSize[i]);
225008 pCsr->aColumnSize[i] = 0;
225009 rc = fts5ApiColumnText(pCtx, i, &z, &n);
225010 if( rc==SQLITE_OK ){
225011 rc = sqlite3Fts5Tokenize(
225012 pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb
225013 );
225014 }
225015 }
225016 }
225017 }
225018 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
225019 }
225020 if( iCol<0 ){
225021 int i;
225022 *pnToken = 0;
225023 for(i=0; i<pConfig->nCol; i++){
225024 *pnToken += pCsr->aColumnSize[i];
225025 }
225026 }else if( iCol<pConfig->nCol ){
225027 *pnToken = pCsr->aColumnSize[iCol];
225028 }else{
225029 *pnToken = 0;
225030 rc = SQLITE_RANGE;
225031 }
225032 return rc;
225033}
225034
225035/*
225036** Implementation of the xSetAuxdata() method.
225037*/
225038static int fts5ApiSetAuxdata(
225039 Fts5Context *pCtx, /* Fts5 context */
225040 void *pPtr, /* Pointer to save as auxdata */
225041 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
225042){
225043 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225044 Fts5Auxdata *pData;
225045
225046 /* Search through the cursors list of Fts5Auxdata objects for one that
225047 ** corresponds to the currently executing auxiliary function. */
225048 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
225049 if( pData->pAux==pCsr->pAux ) break;
225050 }
225051
225052 if( pData ){
225053 if( pData->xDelete ){
225054 pData->xDelete(pData->pPtr);
225055 }
225056 }else{
225057 int rc = SQLITE_OK;
225058 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
225059 if( pData==0 ){
225060 if( xDelete ) xDelete(pPtr);
225061 return rc;
225062 }
225063 pData->pAux = pCsr->pAux;
225064 pData->pNext = pCsr->pAuxdata;
225065 pCsr->pAuxdata = pData;
225066 }
225067
225068 pData->xDelete = xDelete;
225069 pData->pPtr = pPtr;
225070 return SQLITE_OK;
225071}
225072
225073static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
225074 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225075 Fts5Auxdata *pData;
225076 void *pRet = 0;
225077
225078 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
225079 if( pData->pAux==pCsr->pAux ) break;
225080 }
225081
225082 if( pData ){
225083 pRet = pData->pPtr;
225084 if( bClear ){
225085 pData->pPtr = 0;
225086 pData->xDelete = 0;
225087 }
225088 }
225089
225090 return pRet;
225091}
225092
225093static void fts5ApiPhraseNext(
225094 Fts5Context *pUnused,
225095 Fts5PhraseIter *pIter,
225096 int *piCol, int *piOff
225097){
225098 UNUSED_PARAM(pUnused);
225099 if( pIter->a>=pIter->b ){
225100 *piCol = -1;
225101 *piOff = -1;
225102 }else{
225103 int iVal;
225104 pIter->a += fts5GetVarint32(pIter->a, iVal);
225105 if( iVal==1 ){
225106 pIter->a += fts5GetVarint32(pIter->a, iVal);
225107 *piCol = iVal;
225108 *piOff = 0;
225109 pIter->a += fts5GetVarint32(pIter->a, iVal);
225110 }
225111 *piOff += (iVal-2);
225112 }
225113}
225114
225115static int fts5ApiPhraseFirst(
225116 Fts5Context *pCtx,
225117 int iPhrase,
225118 Fts5PhraseIter *pIter,
225119 int *piCol, int *piOff
225120){
225121 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225122 int n;
225123 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
225124 if( rc==SQLITE_OK ){
225125 pIter->b = &pIter->a[n];
225126 *piCol = 0;
225127 *piOff = 0;
225128 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
225129 }
225130 return rc;
225131}
225132
225133static void fts5ApiPhraseNextColumn(
225134 Fts5Context *pCtx,
225135 Fts5PhraseIter *pIter,
225136 int *piCol
225137){
225138 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225139 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
225140
225141 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
225142 if( pIter->a>=pIter->b ){
225143 *piCol = -1;
225144 }else{
225145 int iIncr;
225146 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
225147 *piCol += (iIncr-2);
225148 }
225149 }else{
225150 while( 1 ){
225151 int dummy;
225152 if( pIter->a>=pIter->b ){
225153 *piCol = -1;
225154 return;
225155 }
225156 if( pIter->a[0]==0x01 ) break;
225157 pIter->a += fts5GetVarint32(pIter->a, dummy);
225158 }
225159 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
225160 }
225161}
225162
225163static int fts5ApiPhraseFirstColumn(
225164 Fts5Context *pCtx,
225165 int iPhrase,
225166 Fts5PhraseIter *pIter,
225167 int *piCol
225168){
225169 int rc = SQLITE_OK;
225170 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225171 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
225172
225173 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
225174 Fts5Sorter *pSorter = pCsr->pSorter;
225175 int n;
225176 if( pSorter ){
225177 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
225178 n = pSorter->aIdx[iPhrase] - i1;
225179 pIter->a = &pSorter->aPoslist[i1];
225180 }else{
225181 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
225182 }
225183 if( rc==SQLITE_OK ){
225184 pIter->b = &pIter->a[n];
225185 *piCol = 0;
225186 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
225187 }
225188 }else{
225189 int n;
225190 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
225191 if( rc==SQLITE_OK ){
225192 pIter->b = &pIter->a[n];
225193 if( n<=0 ){
225194 *piCol = -1;
225195 }else if( pIter->a[0]==0x01 ){
225196 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
225197 }else{
225198 *piCol = 0;
225199 }
225200 }
225201 }
225202
225203 return rc;
225204}
225205
225206
225207static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
225208 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
225209);
225210
225211static const Fts5ExtensionApi sFts5Api = {
225212 2, /* iVersion */
225213 fts5ApiUserData,
225214 fts5ApiColumnCount,
225215 fts5ApiRowCount,
225216 fts5ApiColumnTotalSize,
225217 fts5ApiTokenize,
225218 fts5ApiPhraseCount,
225219 fts5ApiPhraseSize,
225220 fts5ApiInstCount,
225221 fts5ApiInst,
225222 fts5ApiRowid,
225223 fts5ApiColumnText,
225224 fts5ApiColumnSize,
225225 fts5ApiQueryPhrase,
225226 fts5ApiSetAuxdata,
225227 fts5ApiGetAuxdata,
225228 fts5ApiPhraseFirst,
225229 fts5ApiPhraseNext,
225230 fts5ApiPhraseFirstColumn,
225231 fts5ApiPhraseNextColumn,
225232};
225233
225234/*
225235** Implementation of API function xQueryPhrase().
225236*/
225237static int fts5ApiQueryPhrase(
225238 Fts5Context *pCtx,
225239 int iPhrase,
225240 void *pUserData,
225241 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
225242){
225243 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
225244 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
225245 int rc;
225246 Fts5Cursor *pNew = 0;
225247
225248 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
225249 if( rc==SQLITE_OK ){
225250 pNew->ePlan = FTS5_PLAN_MATCH;
225251 pNew->iFirstRowid = SMALLEST_INT64;
225252 pNew->iLastRowid = LARGEST_INT64;
225253 pNew->base.pVtab = (sqlite3_vtab*)pTab;
225254 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
225255 }
225256
225257 if( rc==SQLITE_OK ){
225258 for(rc = fts5CursorFirst(pTab, pNew, 0);
225259 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
225260 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
225261 ){
225262 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
225263 if( rc!=SQLITE_OK ){
225264 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
225265 break;
225266 }
225267 }
225268 }
225269
225270 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
225271 return rc;
225272}
225273
225274static void fts5ApiInvoke(
225275 Fts5Auxiliary *pAux,
225276 Fts5Cursor *pCsr,
225277 sqlite3_context *context,
225278 int argc,
225279 sqlite3_value **argv
225280){
225281 assert( pCsr->pAux==0 );
225282 pCsr->pAux = pAux;
225283 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
225284 pCsr->pAux = 0;
225285}
225286
225287static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
225288 Fts5Cursor *pCsr;
225289 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
225290 if( pCsr->iCsrId==iCsrId ) break;
225291 }
225292 return pCsr;
225293}
225294
225295static void fts5ApiCallback(
225296 sqlite3_context *context,
225297 int argc,
225298 sqlite3_value **argv
225299){
225300
225301 Fts5Auxiliary *pAux;
225302 Fts5Cursor *pCsr;
225303 i64 iCsrId;
225304
225305 assert( argc>=1 );
225306 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
225307 iCsrId = sqlite3_value_int64(argv[0]);
225308
225309 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
225310 if( pCsr==0 || pCsr->ePlan==0 ){
225311 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
225312 sqlite3_result_error(context, zErr, -1);
225313 sqlite3_free(zErr);
225314 }else{
225315 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
225316 }
225317}
225318
225319
225320/*
225321** Given cursor id iId, return a pointer to the corresponding Fts5Table
225322** object. Or NULL If the cursor id does not exist.
225323*/
225324static Fts5Table *sqlite3Fts5TableFromCsrid(
225325 Fts5Global *pGlobal, /* FTS5 global context for db handle */
225326 i64 iCsrId /* Id of cursor to find */
225327){
225328 Fts5Cursor *pCsr;
225329 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
225330 if( pCsr ){
225331 return (Fts5Table*)pCsr->base.pVtab;
225332 }
225333 return 0;
225334}
225335
225336/*
225337** Return a "position-list blob" corresponding to the current position of
225338** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
225339** the current position-list for each phrase in the query associated with
225340** cursor pCsr.
225341**
225342** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
225343** the number of phrases in the query. Following the varints are the
225344** concatenated position lists for each phrase, in order.
225345**
225346** The first varint (if it exists) contains the size of the position list
225347** for phrase 0. The second (same disclaimer) contains the size of position
225348** list 1. And so on. There is no size field for the final position list,
225349** as it can be derived from the total size of the blob.
225350*/
225351static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
225352 int i;
225353 int rc = SQLITE_OK;
225354 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
225355 Fts5Buffer val;
225356
225357 memset(&val, 0, sizeof(Fts5Buffer));
225358 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
225359 case FTS5_DETAIL_FULL:
225360
225361 /* Append the varints */
225362 for(i=0; i<(nPhrase-1); i++){
225363 const u8 *dummy;
225364 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
225365 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
225366 }
225367
225368 /* Append the position lists */
225369 for(i=0; i<nPhrase; i++){
225370 const u8 *pPoslist;
225371 int nPoslist;
225372 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
225373 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
225374 }
225375 break;
225376
225377 case FTS5_DETAIL_COLUMNS:
225378
225379 /* Append the varints */
225380 for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
225381 const u8 *dummy;
225382 int nByte;
225383 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
225384 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
225385 }
225386
225387 /* Append the position lists */
225388 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
225389 const u8 *pPoslist;
225390 int nPoslist;
225391 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
225392 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
225393 }
225394 break;
225395
225396 default:
225397 break;
225398 }
225399
225400 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
225401 return rc;
225402}
225403
225404/*
225405** This is the xColumn method, called by SQLite to request a value from
225406** the row that the supplied cursor currently points to.
225407*/
225408static int fts5ColumnMethod(
225409 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
225410 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
225411 int iCol /* Index of column to read value from */
225412){
225413 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
225414 Fts5Config *pConfig = pTab->p.pConfig;
225415 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
225416 int rc = SQLITE_OK;
225417
225418 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
225419
225420 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
225421 if( iCol==pConfig->nCol ){
225422 sqlite3_result_int64(pCtx, pCsr->iSpecial);
225423 }
225424 }else
225425
225426 if( iCol==pConfig->nCol ){
225427 /* User is requesting the value of the special column with the same name
225428 ** as the table. Return the cursor integer id number. This value is only
225429 ** useful in that it may be passed as the first argument to an FTS5
225430 ** auxiliary function. */
225431 sqlite3_result_int64(pCtx, pCsr->iCsrId);
225432 }else if( iCol==pConfig->nCol+1 ){
225433
225434 /* The value of the "rank" column. */
225435 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
225436 fts5PoslistBlob(pCtx, pCsr);
225437 }else if(
225438 pCsr->ePlan==FTS5_PLAN_MATCH
225439 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
225440 ){
225441 if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
225442 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
225443 }
225444 }
225445 }else if( !fts5IsContentless(pTab) ){
225446 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
225447 rc = fts5SeekCursor(pCsr, 1);
225448 if( rc==SQLITE_OK ){
225449 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
225450 }
225451 pConfig->pzErrmsg = 0;
225452 }
225453 return rc;
225454}
225455
225456
225457/*
225458** This routine implements the xFindFunction method for the FTS3
225459** virtual table.
225460*/
225461static int fts5FindFunctionMethod(
225462 sqlite3_vtab *pVtab, /* Virtual table handle */
225463 int nUnused, /* Number of SQL function arguments */
225464 const char *zName, /* Name of SQL function */
225465 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
225466 void **ppArg /* OUT: User data for *pxFunc */
225467){
225468 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
225469 Fts5Auxiliary *pAux;
225470
225471 UNUSED_PARAM(nUnused);
225472 pAux = fts5FindAuxiliary(pTab, zName);
225473 if( pAux ){
225474 *pxFunc = fts5ApiCallback;
225475 *ppArg = (void*)pAux;
225476 return 1;
225477 }
225478
225479 /* No function of the specified name was found. Return 0. */
225480 return 0;
225481}
225482
225483/*
225484** Implementation of FTS5 xRename method. Rename an fts5 table.
225485*/
225486static int fts5RenameMethod(
225487 sqlite3_vtab *pVtab, /* Virtual table handle */
225488 const char *zName /* New name of table */
225489){
225490 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
225491 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
225492}
225493
225494static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
225495 fts5TripCursors((Fts5FullTable*)pTab);
225496 return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
225497}
225498
225499/*
225500** The xSavepoint() method.
225501**
225502** Flush the contents of the pending-terms table to disk.
225503*/
225504static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
225505 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
225506 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
225507 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
225508}
225509
225510/*
225511** The xRelease() method.
225512**
225513** This is a no-op.
225514*/
225515static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
225516 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
225517 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
225518 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
225519}
225520
225521/*
225522** The xRollbackTo() method.
225523**
225524** Discard the contents of the pending terms table.
225525*/
225526static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
225527 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
225528 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
225529 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
225530 fts5TripCursors(pTab);
225531 return sqlite3Fts5StorageRollback(pTab->pStorage);
225532}
225533
225534/*
225535** Register a new auxiliary function with global context pGlobal.
225536*/
225537static int fts5CreateAux(
225538 fts5_api *pApi, /* Global context (one per db handle) */
225539 const char *zName, /* Name of new function */
225540 void *pUserData, /* User data for aux. function */
225541 fts5_extension_function xFunc, /* Aux. function implementation */
225542 void(*xDestroy)(void*) /* Destructor for pUserData */
225543){
225544 Fts5Global *pGlobal = (Fts5Global*)pApi;
225545 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
225546 if( rc==SQLITE_OK ){
225547 Fts5Auxiliary *pAux;
225548 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
225549 sqlite3_int64 nByte; /* Bytes of space to allocate */
225550
225551 nName = strlen(zName) + 1;
225552 nByte = sizeof(Fts5Auxiliary) + nName;
225553 pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
225554 if( pAux ){
225555 memset(pAux, 0, (size_t)nByte);
225556 pAux->zFunc = (char*)&pAux[1];
225557 memcpy(pAux->zFunc, zName, nName);
225558 pAux->pGlobal = pGlobal;
225559 pAux->pUserData = pUserData;
225560 pAux->xFunc = xFunc;
225561 pAux->xDestroy = xDestroy;
225562 pAux->pNext = pGlobal->pAux;
225563 pGlobal->pAux = pAux;
225564 }else{
225565 rc = SQLITE_NOMEM;
225566 }
225567 }
225568
225569 return rc;
225570}
225571
225572/*
225573** Register a new tokenizer. This is the implementation of the
225574** fts5_api.xCreateTokenizer() method.
225575*/
225576static int fts5CreateTokenizer(
225577 fts5_api *pApi, /* Global context (one per db handle) */
225578 const char *zName, /* Name of new function */
225579 void *pUserData, /* User data for aux. function */
225580 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
225581 void(*xDestroy)(void*) /* Destructor for pUserData */
225582){
225583 Fts5Global *pGlobal = (Fts5Global*)pApi;
225584 Fts5TokenizerModule *pNew;
225585 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
225586 sqlite3_int64 nByte; /* Bytes of space to allocate */
225587 int rc = SQLITE_OK;
225588
225589 nName = strlen(zName) + 1;
225590 nByte = sizeof(Fts5TokenizerModule) + nName;
225591 pNew = (Fts5TokenizerModule*)sqlite3_malloc64(nByte);
225592 if( pNew ){
225593 memset(pNew, 0, (size_t)nByte);
225594 pNew->zName = (char*)&pNew[1];
225595 memcpy(pNew->zName, zName, nName);
225596 pNew->pUserData = pUserData;
225597 pNew->x = *pTokenizer;
225598 pNew->xDestroy = xDestroy;
225599 pNew->pNext = pGlobal->pTok;
225600 pGlobal->pTok = pNew;
225601 if( pNew->pNext==0 ){
225602 pGlobal->pDfltTok = pNew;
225603 }
225604 }else{
225605 rc = SQLITE_NOMEM;
225606 }
225607
225608 return rc;
225609}
225610
225611static Fts5TokenizerModule *fts5LocateTokenizer(
225612 Fts5Global *pGlobal,
225613 const char *zName
225614){
225615 Fts5TokenizerModule *pMod = 0;
225616
225617 if( zName==0 ){
225618 pMod = pGlobal->pDfltTok;
225619 }else{
225620 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
225621 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
225622 }
225623 }
225624
225625 return pMod;
225626}
225627
225628/*
225629** Find a tokenizer. This is the implementation of the
225630** fts5_api.xFindTokenizer() method.
225631*/
225632static int fts5FindTokenizer(
225633 fts5_api *pApi, /* Global context (one per db handle) */
225634 const char *zName, /* Name of new function */
225635 void **ppUserData,
225636 fts5_tokenizer *pTokenizer /* Populate this object */
225637){
225638 int rc = SQLITE_OK;
225639 Fts5TokenizerModule *pMod;
225640
225641 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
225642 if( pMod ){
225643 *pTokenizer = pMod->x;
225644 *ppUserData = pMod->pUserData;
225645 }else{
225646 memset(pTokenizer, 0, sizeof(fts5_tokenizer));
225647 rc = SQLITE_ERROR;
225648 }
225649
225650 return rc;
225651}
225652
225653static int sqlite3Fts5GetTokenizer(
225654 Fts5Global *pGlobal,
225655 const char **azArg,
225656 int nArg,
225657 Fts5Tokenizer **ppTok,
225658 fts5_tokenizer **ppTokApi,
225659 char **pzErr
225660){
225661 Fts5TokenizerModule *pMod;
225662 int rc = SQLITE_OK;
225663
225664 pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
225665 if( pMod==0 ){
225666 assert( nArg>0 );
225667 rc = SQLITE_ERROR;
225668 *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
225669 }else{
225670 rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
225671 *ppTokApi = &pMod->x;
225672 if( rc!=SQLITE_OK && pzErr ){
225673 *pzErr = sqlite3_mprintf("error in tokenizer constructor");
225674 }
225675 }
225676
225677 if( rc!=SQLITE_OK ){
225678 *ppTokApi = 0;
225679 *ppTok = 0;
225680 }
225681
225682 return rc;
225683}
225684
225685static void fts5ModuleDestroy(void *pCtx){
225686 Fts5TokenizerModule *pTok, *pNextTok;
225687 Fts5Auxiliary *pAux, *pNextAux;
225688 Fts5Global *pGlobal = (Fts5Global*)pCtx;
225689
225690 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
225691 pNextAux = pAux->pNext;
225692 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
225693 sqlite3_free(pAux);
225694 }
225695
225696 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
225697 pNextTok = pTok->pNext;
225698 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
225699 sqlite3_free(pTok);
225700 }
225701
225702 sqlite3_free(pGlobal);
225703}
225704
225705static void fts5Fts5Func(
225706 sqlite3_context *pCtx, /* Function call context */
225707 int nArg, /* Number of args */
225708 sqlite3_value **apArg /* Function arguments */
225709){
225710 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
225711 fts5_api **ppApi;
225712 UNUSED_PARAM(nArg);
225713 assert( nArg==1 );
225714 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
225715 if( ppApi ) *ppApi = &pGlobal->api;
225716}
225717
225718/*
225719** Implementation of fts5_source_id() function.
225720*/
225721static void fts5SourceIdFunc(
225722 sqlite3_context *pCtx, /* Function call context */
225723 int nArg, /* Number of args */
225724 sqlite3_value **apUnused /* Function arguments */
225725){
225726 assert( nArg==0 );
225727 UNUSED_PARAM2(nArg, apUnused);
225728 sqlite3_result_text(pCtx, "fts5: 2020-08-14 13:23:32 fca8dc8b578f215a969cd899336378966156154710873e68b3d9ac5881b0ff3f", -1, SQLITE_TRANSIENT);
225729}
225730
225731/*
225732** Return true if zName is the extension on one of the shadow tables used
225733** by this module.
225734*/
225735static int fts5ShadowName(const char *zName){
225736 static const char *azName[] = {
225737 "config", "content", "data", "docsize", "idx"
225738 };
225739 unsigned int i;
225740 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
225741 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
225742 }
225743 return 0;
225744}
225745
225746static int fts5Init(sqlite3 *db){
225747 static const sqlite3_module fts5Mod = {
225748 /* iVersion */ 3,
225749 /* xCreate */ fts5CreateMethod,
225750 /* xConnect */ fts5ConnectMethod,
225751 /* xBestIndex */ fts5BestIndexMethod,
225752 /* xDisconnect */ fts5DisconnectMethod,
225753 /* xDestroy */ fts5DestroyMethod,
225754 /* xOpen */ fts5OpenMethod,
225755 /* xClose */ fts5CloseMethod,
225756 /* xFilter */ fts5FilterMethod,
225757 /* xNext */ fts5NextMethod,
225758 /* xEof */ fts5EofMethod,
225759 /* xColumn */ fts5ColumnMethod,
225760 /* xRowid */ fts5RowidMethod,
225761 /* xUpdate */ fts5UpdateMethod,
225762 /* xBegin */ fts5BeginMethod,
225763 /* xSync */ fts5SyncMethod,
225764 /* xCommit */ fts5CommitMethod,
225765 /* xRollback */ fts5RollbackMethod,
225766 /* xFindFunction */ fts5FindFunctionMethod,
225767 /* xRename */ fts5RenameMethod,
225768 /* xSavepoint */ fts5SavepointMethod,
225769 /* xRelease */ fts5ReleaseMethod,
225770 /* xRollbackTo */ fts5RollbackToMethod,
225771 /* xShadowName */ fts5ShadowName
225772 };
225773
225774 int rc;
225775 Fts5Global *pGlobal = 0;
225776
225777 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
225778 if( pGlobal==0 ){
225779 rc = SQLITE_NOMEM;
225780 }else{
225781 void *p = (void*)pGlobal;
225782 memset(pGlobal, 0, sizeof(Fts5Global));
225783 pGlobal->db = db;
225784 pGlobal->api.iVersion = 2;
225785 pGlobal->api.xCreateFunction = fts5CreateAux;
225786 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
225787 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
225788 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
225789 if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
225790 if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
225791 if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
225792 if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
225793 if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
225794 if( rc==SQLITE_OK ){
225795 rc = sqlite3_create_function(
225796 db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0
225797 );
225798 }
225799 if( rc==SQLITE_OK ){
225800 rc = sqlite3_create_function(
225801 db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0
225802 );
225803 }
225804 }
225805
225806 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
225807 ** fts5_test_mi.c is compiled and linked into the executable. And call
225808 ** its entry point to enable the matchinfo() demo. */
225809#ifdef SQLITE_FTS5_ENABLE_TEST_MI
225810 if( rc==SQLITE_OK ){
225811 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
225812 rc = sqlite3Fts5TestRegisterMatchinfo(db);
225813 }
225814#endif
225815
225816 return rc;
225817}
225818
225819/*
225820** The following functions are used to register the module with SQLite. If
225821** this module is being built as part of the SQLite core (SQLITE_CORE is
225822** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
225823**
225824** Or, if this module is being built as a loadable extension,
225825** sqlite3Fts5Init() is omitted and the two standard entry points
225826** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
225827*/
225828#ifndef SQLITE_CORE
225829#ifdef _WIN32
225830__declspec(dllexport)
225831#endif
225832SQLITE_API int sqlite3_fts_init(
225833 sqlite3 *db,
225834 char **pzErrMsg,
225835 const sqlite3_api_routines *pApi
225836){
225837 SQLITE_EXTENSION_INIT2(pApi);
225838 (void)pzErrMsg; /* Unused parameter */
225839 return fts5Init(db);
225840}
225841
225842#ifdef _WIN32
225843__declspec(dllexport)
225844#endif
225845SQLITE_API int sqlite3_fts5_init(
225846 sqlite3 *db,
225847 char **pzErrMsg,
225848 const sqlite3_api_routines *pApi
225849){
225850 SQLITE_EXTENSION_INIT2(pApi);
225851 (void)pzErrMsg; /* Unused parameter */
225852 return fts5Init(db);
225853}
225854#else
225855SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
225856 return fts5Init(db);
225857}
225858#endif
225859
225860/*
225861** 2014 May 31
225862**
225863** The author disclaims copyright to this source code. In place of
225864** a legal notice, here is a blessing:
225865**
225866** May you do good and not evil.
225867** May you find forgiveness for yourself and forgive others.
225868** May you share freely, never taking more than you give.
225869**
225870******************************************************************************
225871**
225872*/
225873
225874
225875
225876/* #include "fts5Int.h" */
225877
225878struct Fts5Storage {
225879 Fts5Config *pConfig;
225880 Fts5Index *pIndex;
225881 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
225882 i64 nTotalRow; /* Total number of rows in FTS table */
225883 i64 *aTotalSize; /* Total sizes of each column */
225884 sqlite3_stmt *aStmt[11];
225885};
225886
225887
225888#if FTS5_STMT_SCAN_ASC!=0
225889# error "FTS5_STMT_SCAN_ASC mismatch"
225890#endif
225891#if FTS5_STMT_SCAN_DESC!=1
225892# error "FTS5_STMT_SCAN_DESC mismatch"
225893#endif
225894#if FTS5_STMT_LOOKUP!=2
225895# error "FTS5_STMT_LOOKUP mismatch"
225896#endif
225897
225898#define FTS5_STMT_INSERT_CONTENT 3
225899#define FTS5_STMT_REPLACE_CONTENT 4
225900#define FTS5_STMT_DELETE_CONTENT 5
225901#define FTS5_STMT_REPLACE_DOCSIZE 6
225902#define FTS5_STMT_DELETE_DOCSIZE 7
225903#define FTS5_STMT_LOOKUP_DOCSIZE 8
225904#define FTS5_STMT_REPLACE_CONFIG 9
225905#define FTS5_STMT_SCAN 10
225906
225907/*
225908** Prepare the two insert statements - Fts5Storage.pInsertContent and
225909** Fts5Storage.pInsertDocsize - if they have not already been prepared.
225910** Return SQLITE_OK if successful, or an SQLite error code if an error
225911** occurs.
225912*/
225913static int fts5StorageGetStmt(
225914 Fts5Storage *p, /* Storage handle */
225915 int eStmt, /* FTS5_STMT_XXX constant */
225916 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
225917 char **pzErrMsg /* OUT: Error message (if any) */
225918){
225919 int rc = SQLITE_OK;
225920
225921 /* If there is no %_docsize table, there should be no requests for
225922 ** statements to operate on it. */
225923 assert( p->pConfig->bColumnsize || (
225924 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
225925 && eStmt!=FTS5_STMT_DELETE_DOCSIZE
225926 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
225927 ));
225928
225929 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
225930 if( p->aStmt[eStmt]==0 ){
225931 const char *azStmt[] = {
225932 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
225933 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
225934 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
225935
225936 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
225937 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
225938 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
225939 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
225940 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
225941
225942 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
225943
225944 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
225945 "SELECT %s FROM %s AS T", /* SCAN */
225946 };
225947 Fts5Config *pC = p->pConfig;
225948 char *zSql = 0;
225949
225950 switch( eStmt ){
225951 case FTS5_STMT_SCAN:
225952 zSql = sqlite3_mprintf(azStmt[eStmt],
225953 pC->zContentExprlist, pC->zContent
225954 );
225955 break;
225956
225957 case FTS5_STMT_SCAN_ASC:
225958 case FTS5_STMT_SCAN_DESC:
225959 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
225960 pC->zContent, pC->zContentRowid, pC->zContentRowid,
225961 pC->zContentRowid
225962 );
225963 break;
225964
225965 case FTS5_STMT_LOOKUP:
225966 zSql = sqlite3_mprintf(azStmt[eStmt],
225967 pC->zContentExprlist, pC->zContent, pC->zContentRowid
225968 );
225969 break;
225970
225971 case FTS5_STMT_INSERT_CONTENT:
225972 case FTS5_STMT_REPLACE_CONTENT: {
225973 int nCol = pC->nCol + 1;
225974 char *zBind;
225975 int i;
225976
225977 zBind = sqlite3_malloc64(1 + nCol*2);
225978 if( zBind ){
225979 for(i=0; i<nCol; i++){
225980 zBind[i*2] = '?';
225981 zBind[i*2 + 1] = ',';
225982 }
225983 zBind[i*2-1] = '\0';
225984 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
225985 sqlite3_free(zBind);
225986 }
225987 break;
225988 }
225989
225990 default:
225991 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
225992 break;
225993 }
225994
225995 if( zSql==0 ){
225996 rc = SQLITE_NOMEM;
225997 }else{
225998 int f = SQLITE_PREPARE_PERSISTENT;
225999 if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
226000 p->pConfig->bLock++;
226001 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
226002 p->pConfig->bLock--;
226003 sqlite3_free(zSql);
226004 if( rc!=SQLITE_OK && pzErrMsg ){
226005 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
226006 }
226007 }
226008 }
226009
226010 *ppStmt = p->aStmt[eStmt];
226011 sqlite3_reset(*ppStmt);
226012 return rc;
226013}
226014
226015
226016static int fts5ExecPrintf(
226017 sqlite3 *db,
226018 char **pzErr,
226019 const char *zFormat,
226020 ...
226021){
226022 int rc;
226023 va_list ap; /* ... printf arguments */
226024 char *zSql;
226025
226026 va_start(ap, zFormat);
226027 zSql = sqlite3_vmprintf(zFormat, ap);
226028
226029 if( zSql==0 ){
226030 rc = SQLITE_NOMEM;
226031 }else{
226032 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
226033 sqlite3_free(zSql);
226034 }
226035
226036 va_end(ap);
226037 return rc;
226038}
226039
226040/*
226041** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
226042** code otherwise.
226043*/
226044static int sqlite3Fts5DropAll(Fts5Config *pConfig){
226045 int rc = fts5ExecPrintf(pConfig->db, 0,
226046 "DROP TABLE IF EXISTS %Q.'%q_data';"
226047 "DROP TABLE IF EXISTS %Q.'%q_idx';"
226048 "DROP TABLE IF EXISTS %Q.'%q_config';",
226049 pConfig->zDb, pConfig->zName,
226050 pConfig->zDb, pConfig->zName,
226051 pConfig->zDb, pConfig->zName
226052 );
226053 if( rc==SQLITE_OK && pConfig->bColumnsize ){
226054 rc = fts5ExecPrintf(pConfig->db, 0,
226055 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
226056 pConfig->zDb, pConfig->zName
226057 );
226058 }
226059 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
226060 rc = fts5ExecPrintf(pConfig->db, 0,
226061 "DROP TABLE IF EXISTS %Q.'%q_content';",
226062 pConfig->zDb, pConfig->zName
226063 );
226064 }
226065 return rc;
226066}
226067
226068static void fts5StorageRenameOne(
226069 Fts5Config *pConfig, /* Current FTS5 configuration */
226070 int *pRc, /* IN/OUT: Error code */
226071 const char *zTail, /* Tail of table name e.g. "data", "config" */
226072 const char *zName /* New name of FTS5 table */
226073){
226074 if( *pRc==SQLITE_OK ){
226075 *pRc = fts5ExecPrintf(pConfig->db, 0,
226076 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
226077 pConfig->zDb, pConfig->zName, zTail, zName, zTail
226078 );
226079 }
226080}
226081
226082static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
226083 Fts5Config *pConfig = pStorage->pConfig;
226084 int rc = sqlite3Fts5StorageSync(pStorage);
226085
226086 fts5StorageRenameOne(pConfig, &rc, "data", zName);
226087 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
226088 fts5StorageRenameOne(pConfig, &rc, "config", zName);
226089 if( pConfig->bColumnsize ){
226090 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
226091 }
226092 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
226093 fts5StorageRenameOne(pConfig, &rc, "content", zName);
226094 }
226095 return rc;
226096}
226097
226098/*
226099** Create the shadow table named zPost, with definition zDefn. Return
226100** SQLITE_OK if successful, or an SQLite error code otherwise.
226101*/
226102static int sqlite3Fts5CreateTable(
226103 Fts5Config *pConfig, /* FTS5 configuration */
226104 const char *zPost, /* Shadow table to create (e.g. "content") */
226105 const char *zDefn, /* Columns etc. for shadow table */
226106 int bWithout, /* True for without rowid */
226107 char **pzErr /* OUT: Error message */
226108){
226109 int rc;
226110 char *zErr = 0;
226111
226112 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
226113 pConfig->zDb, pConfig->zName, zPost, zDefn,
226114#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
226115 bWithout?" WITHOUT ROWID":
226116#endif
226117 ""
226118 );
226119 if( zErr ){
226120 *pzErr = sqlite3_mprintf(
226121 "fts5: error creating shadow table %q_%s: %s",
226122 pConfig->zName, zPost, zErr
226123 );
226124 sqlite3_free(zErr);
226125 }
226126
226127 return rc;
226128}
226129
226130/*
226131** Open a new Fts5Index handle. If the bCreate argument is true, create
226132** and initialize the underlying tables
226133**
226134** If successful, set *pp to point to the new object and return SQLITE_OK.
226135** Otherwise, set *pp to NULL and return an SQLite error code.
226136*/
226137static int sqlite3Fts5StorageOpen(
226138 Fts5Config *pConfig,
226139 Fts5Index *pIndex,
226140 int bCreate,
226141 Fts5Storage **pp,
226142 char **pzErr /* OUT: Error message */
226143){
226144 int rc = SQLITE_OK;
226145 Fts5Storage *p; /* New object */
226146 sqlite3_int64 nByte; /* Bytes of space to allocate */
226147
226148 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
226149 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
226150 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
226151 if( !p ) return SQLITE_NOMEM;
226152
226153 memset(p, 0, (size_t)nByte);
226154 p->aTotalSize = (i64*)&p[1];
226155 p->pConfig = pConfig;
226156 p->pIndex = pIndex;
226157
226158 if( bCreate ){
226159 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
226160 int nDefn = 32 + pConfig->nCol*10;
226161 char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 10);
226162 if( zDefn==0 ){
226163 rc = SQLITE_NOMEM;
226164 }else{
226165 int i;
226166 int iOff;
226167 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
226168 iOff = (int)strlen(zDefn);
226169 for(i=0; i<pConfig->nCol; i++){
226170 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
226171 iOff += (int)strlen(&zDefn[iOff]);
226172 }
226173 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
226174 }
226175 sqlite3_free(zDefn);
226176 }
226177
226178 if( rc==SQLITE_OK && pConfig->bColumnsize ){
226179 rc = sqlite3Fts5CreateTable(
226180 pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
226181 );
226182 }
226183 if( rc==SQLITE_OK ){
226184 rc = sqlite3Fts5CreateTable(
226185 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
226186 );
226187 }
226188 if( rc==SQLITE_OK ){
226189 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
226190 }
226191 }
226192
226193 if( rc ){
226194 sqlite3Fts5StorageClose(p);
226195 *pp = 0;
226196 }
226197 return rc;
226198}
226199
226200/*
226201** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
226202*/
226203static int sqlite3Fts5StorageClose(Fts5Storage *p){
226204 int rc = SQLITE_OK;
226205 if( p ){
226206 int i;
226207
226208 /* Finalize all SQL statements */
226209 for(i=0; i<ArraySize(p->aStmt); i++){
226210 sqlite3_finalize(p->aStmt[i]);
226211 }
226212
226213 sqlite3_free(p);
226214 }
226215 return rc;
226216}
226217
226218typedef struct Fts5InsertCtx Fts5InsertCtx;
226219struct Fts5InsertCtx {
226220 Fts5Storage *pStorage;
226221 int iCol;
226222 int szCol; /* Size of column value in tokens */
226223};
226224
226225/*
226226** Tokenization callback used when inserting tokens into the FTS index.
226227*/
226228static int fts5StorageInsertCallback(
226229 void *pContext, /* Pointer to Fts5InsertCtx object */
226230 int tflags,
226231 const char *pToken, /* Buffer containing token */
226232 int nToken, /* Size of token in bytes */
226233 int iUnused1, /* Start offset of token */
226234 int iUnused2 /* End offset of token */
226235){
226236 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
226237 Fts5Index *pIdx = pCtx->pStorage->pIndex;
226238 UNUSED_PARAM2(iUnused1, iUnused2);
226239 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
226240 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
226241 pCtx->szCol++;
226242 }
226243 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
226244}
226245
226246/*
226247** If a row with rowid iDel is present in the %_content table, add the
226248** delete-markers to the FTS index necessary to delete it. Do not actually
226249** remove the %_content row at this time though.
226250*/
226251static int fts5StorageDeleteFromIndex(
226252 Fts5Storage *p,
226253 i64 iDel,
226254 sqlite3_value **apVal
226255){
226256 Fts5Config *pConfig = p->pConfig;
226257 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
226258 int rc; /* Return code */
226259 int rc2; /* sqlite3_reset() return code */
226260 int iCol;
226261 Fts5InsertCtx ctx;
226262
226263 if( apVal==0 ){
226264 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
226265 if( rc!=SQLITE_OK ) return rc;
226266 sqlite3_bind_int64(pSeek, 1, iDel);
226267 if( sqlite3_step(pSeek)!=SQLITE_ROW ){
226268 return sqlite3_reset(pSeek);
226269 }
226270 }
226271
226272 ctx.pStorage = p;
226273 ctx.iCol = -1;
226274 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
226275 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
226276 if( pConfig->abUnindexed[iCol-1]==0 ){
226277 const char *zText;
226278 int nText;
226279 if( pSeek ){
226280 zText = (const char*)sqlite3_column_text(pSeek, iCol);
226281 nText = sqlite3_column_bytes(pSeek, iCol);
226282 }else{
226283 zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
226284 nText = sqlite3_value_bytes(apVal[iCol-1]);
226285 }
226286 ctx.szCol = 0;
226287 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
226288 zText, nText, (void*)&ctx, fts5StorageInsertCallback
226289 );
226290 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
226291 }
226292 }
226293 p->nTotalRow--;
226294
226295 rc2 = sqlite3_reset(pSeek);
226296 if( rc==SQLITE_OK ) rc = rc2;
226297 return rc;
226298}
226299
226300
226301/*
226302** Insert a record into the %_docsize table. Specifically, do:
226303**
226304** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
226305**
226306** If there is no %_docsize table (as happens if the columnsize=0 option
226307** is specified when the FTS5 table is created), this function is a no-op.
226308*/
226309static int fts5StorageInsertDocsize(
226310 Fts5Storage *p, /* Storage module to write to */
226311 i64 iRowid, /* id value */
226312 Fts5Buffer *pBuf /* sz value */
226313){
226314 int rc = SQLITE_OK;
226315 if( p->pConfig->bColumnsize ){
226316 sqlite3_stmt *pReplace = 0;
226317 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
226318 if( rc==SQLITE_OK ){
226319 sqlite3_bind_int64(pReplace, 1, iRowid);
226320 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
226321 sqlite3_step(pReplace);
226322 rc = sqlite3_reset(pReplace);
226323 sqlite3_bind_null(pReplace, 2);
226324 }
226325 }
226326 return rc;
226327}
226328
226329/*
226330** Load the contents of the "averages" record from disk into the
226331** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
226332** argument bCache is true, set the p->bTotalsValid flag to indicate
226333** that the contents of aTotalSize[] and nTotalRow are valid until
226334** further notice.
226335**
226336** Return SQLITE_OK if successful, or an SQLite error code if an error
226337** occurs.
226338*/
226339static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
226340 int rc = SQLITE_OK;
226341 if( p->bTotalsValid==0 ){
226342 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
226343 p->bTotalsValid = bCache;
226344 }
226345 return rc;
226346}
226347
226348/*
226349** Store the current contents of the p->nTotalRow and p->aTotalSize[]
226350** variables in the "averages" record on disk.
226351**
226352** Return SQLITE_OK if successful, or an SQLite error code if an error
226353** occurs.
226354*/
226355static int fts5StorageSaveTotals(Fts5Storage *p){
226356 int nCol = p->pConfig->nCol;
226357 int i;
226358 Fts5Buffer buf;
226359 int rc = SQLITE_OK;
226360 memset(&buf, 0, sizeof(buf));
226361
226362 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
226363 for(i=0; i<nCol; i++){
226364 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
226365 }
226366 if( rc==SQLITE_OK ){
226367 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
226368 }
226369 sqlite3_free(buf.p);
226370
226371 return rc;
226372}
226373
226374/*
226375** Remove a row from the FTS table.
226376*/
226377static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
226378 Fts5Config *pConfig = p->pConfig;
226379 int rc;
226380 sqlite3_stmt *pDel = 0;
226381
226382 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
226383 rc = fts5StorageLoadTotals(p, 1);
226384
226385 /* Delete the index records */
226386 if( rc==SQLITE_OK ){
226387 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
226388 }
226389
226390 /* Delete the %_docsize record */
226391 if( rc==SQLITE_OK && pConfig->bColumnsize ){
226392 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
226393 if( rc==SQLITE_OK ){
226394 sqlite3_bind_int64(pDel, 1, iDel);
226395 sqlite3_step(pDel);
226396 rc = sqlite3_reset(pDel);
226397 }
226398 }
226399
226400 /* Delete the %_content record */
226401 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
226402 if( rc==SQLITE_OK ){
226403 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
226404 }
226405 if( rc==SQLITE_OK ){
226406 sqlite3_bind_int64(pDel, 1, iDel);
226407 sqlite3_step(pDel);
226408 rc = sqlite3_reset(pDel);
226409 }
226410 }
226411
226412 return rc;
226413}
226414
226415/*
226416** Delete all entries in the FTS5 index.
226417*/
226418static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
226419 Fts5Config *pConfig = p->pConfig;
226420 int rc;
226421
226422 p->bTotalsValid = 0;
226423
226424 /* Delete the contents of the %_data and %_docsize tables. */
226425 rc = fts5ExecPrintf(pConfig->db, 0,
226426 "DELETE FROM %Q.'%q_data';"
226427 "DELETE FROM %Q.'%q_idx';",
226428 pConfig->zDb, pConfig->zName,
226429 pConfig->zDb, pConfig->zName
226430 );
226431 if( rc==SQLITE_OK && pConfig->bColumnsize ){
226432 rc = fts5ExecPrintf(pConfig->db, 0,
226433 "DELETE FROM %Q.'%q_docsize';",
226434 pConfig->zDb, pConfig->zName
226435 );
226436 }
226437
226438 /* Reinitialize the %_data table. This call creates the initial structure
226439 ** and averages records. */
226440 if( rc==SQLITE_OK ){
226441 rc = sqlite3Fts5IndexReinit(p->pIndex);
226442 }
226443 if( rc==SQLITE_OK ){
226444 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
226445 }
226446 return rc;
226447}
226448
226449static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
226450 Fts5Buffer buf = {0,0,0};
226451 Fts5Config *pConfig = p->pConfig;
226452 sqlite3_stmt *pScan = 0;
226453 Fts5InsertCtx ctx;
226454 int rc, rc2;
226455
226456 memset(&ctx, 0, sizeof(Fts5InsertCtx));
226457 ctx.pStorage = p;
226458 rc = sqlite3Fts5StorageDeleteAll(p);
226459 if( rc==SQLITE_OK ){
226460 rc = fts5StorageLoadTotals(p, 1);
226461 }
226462
226463 if( rc==SQLITE_OK ){
226464 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
226465 }
226466
226467 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
226468 i64 iRowid = sqlite3_column_int64(pScan, 0);
226469
226470 sqlite3Fts5BufferZero(&buf);
226471 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
226472 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
226473 ctx.szCol = 0;
226474 if( pConfig->abUnindexed[ctx.iCol]==0 ){
226475 const char *zText = (const char*)sqlite3_column_text(pScan, ctx.iCol+1);
226476 int nText = sqlite3_column_bytes(pScan, ctx.iCol+1);
226477 rc = sqlite3Fts5Tokenize(pConfig,
226478 FTS5_TOKENIZE_DOCUMENT,
226479 zText, nText,
226480 (void*)&ctx,
226481 fts5StorageInsertCallback
226482 );
226483 }
226484 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
226485 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
226486 }
226487 p->nTotalRow++;
226488
226489 if( rc==SQLITE_OK ){
226490 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
226491 }
226492 }
226493 sqlite3_free(buf.p);
226494 rc2 = sqlite3_reset(pScan);
226495 if( rc==SQLITE_OK ) rc = rc2;
226496
226497 /* Write the averages record */
226498 if( rc==SQLITE_OK ){
226499 rc = fts5StorageSaveTotals(p);
226500 }
226501 return rc;
226502}
226503
226504static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
226505 return sqlite3Fts5IndexOptimize(p->pIndex);
226506}
226507
226508static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
226509 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
226510}
226511
226512static int sqlite3Fts5StorageReset(Fts5Storage *p){
226513 return sqlite3Fts5IndexReset(p->pIndex);
226514}
226515
226516/*
226517** Allocate a new rowid. This is used for "external content" tables when
226518** a NULL value is inserted into the rowid column. The new rowid is allocated
226519** by inserting a dummy row into the %_docsize table. The dummy will be
226520** overwritten later.
226521**
226522** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
226523** this case the user is required to provide a rowid explicitly.
226524*/
226525static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
226526 int rc = SQLITE_MISMATCH;
226527 if( p->pConfig->bColumnsize ){
226528 sqlite3_stmt *pReplace = 0;
226529 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
226530 if( rc==SQLITE_OK ){
226531 sqlite3_bind_null(pReplace, 1);
226532 sqlite3_bind_null(pReplace, 2);
226533 sqlite3_step(pReplace);
226534 rc = sqlite3_reset(pReplace);
226535 }
226536 if( rc==SQLITE_OK ){
226537 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
226538 }
226539 }
226540 return rc;
226541}
226542
226543/*
226544** Insert a new row into the FTS content table.
226545*/
226546static int sqlite3Fts5StorageContentInsert(
226547 Fts5Storage *p,
226548 sqlite3_value **apVal,
226549 i64 *piRowid
226550){
226551 Fts5Config *pConfig = p->pConfig;
226552 int rc = SQLITE_OK;
226553
226554 /* Insert the new row into the %_content table. */
226555 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
226556 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
226557 *piRowid = sqlite3_value_int64(apVal[1]);
226558 }else{
226559 rc = fts5StorageNewRowid(p, piRowid);
226560 }
226561 }else{
226562 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
226563 int i; /* Counter variable */
226564 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
226565 for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
226566 rc = sqlite3_bind_value(pInsert, i, apVal[i]);
226567 }
226568 if( rc==SQLITE_OK ){
226569 sqlite3_step(pInsert);
226570 rc = sqlite3_reset(pInsert);
226571 }
226572 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
226573 }
226574
226575 return rc;
226576}
226577
226578/*
226579** Insert new entries into the FTS index and %_docsize table.
226580*/
226581static int sqlite3Fts5StorageIndexInsert(
226582 Fts5Storage *p,
226583 sqlite3_value **apVal,
226584 i64 iRowid
226585){
226586 Fts5Config *pConfig = p->pConfig;
226587 int rc = SQLITE_OK; /* Return code */
226588 Fts5InsertCtx ctx; /* Tokenization callback context object */
226589 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
226590
226591 memset(&buf, 0, sizeof(Fts5Buffer));
226592 ctx.pStorage = p;
226593 rc = fts5StorageLoadTotals(p, 1);
226594
226595 if( rc==SQLITE_OK ){
226596 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
226597 }
226598 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
226599 ctx.szCol = 0;
226600 if( pConfig->abUnindexed[ctx.iCol]==0 ){
226601 const char *zText = (const char*)sqlite3_value_text(apVal[ctx.iCol+2]);
226602 int nText = sqlite3_value_bytes(apVal[ctx.iCol+2]);
226603 rc = sqlite3Fts5Tokenize(pConfig,
226604 FTS5_TOKENIZE_DOCUMENT,
226605 zText, nText,
226606 (void*)&ctx,
226607 fts5StorageInsertCallback
226608 );
226609 }
226610 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
226611 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
226612 }
226613 p->nTotalRow++;
226614
226615 /* Write the %_docsize record */
226616 if( rc==SQLITE_OK ){
226617 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
226618 }
226619 sqlite3_free(buf.p);
226620
226621 return rc;
226622}
226623
226624static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
226625 Fts5Config *pConfig = p->pConfig;
226626 char *zSql;
226627 int rc;
226628
226629 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
226630 pConfig->zDb, pConfig->zName, zSuffix
226631 );
226632 if( zSql==0 ){
226633 rc = SQLITE_NOMEM;
226634 }else{
226635 sqlite3_stmt *pCnt = 0;
226636 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
226637 if( rc==SQLITE_OK ){
226638 if( SQLITE_ROW==sqlite3_step(pCnt) ){
226639 *pnRow = sqlite3_column_int64(pCnt, 0);
226640 }
226641 rc = sqlite3_finalize(pCnt);
226642 }
226643 }
226644
226645 sqlite3_free(zSql);
226646 return rc;
226647}
226648
226649/*
226650** Context object used by sqlite3Fts5StorageIntegrity().
226651*/
226652typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
226653struct Fts5IntegrityCtx {
226654 i64 iRowid;
226655 int iCol;
226656 int szCol;
226657 u64 cksum;
226658 Fts5Termset *pTermset;
226659 Fts5Config *pConfig;
226660};
226661
226662
226663/*
226664** Tokenization callback used by integrity check.
226665*/
226666static int fts5StorageIntegrityCallback(
226667 void *pContext, /* Pointer to Fts5IntegrityCtx object */
226668 int tflags,
226669 const char *pToken, /* Buffer containing token */
226670 int nToken, /* Size of token in bytes */
226671 int iUnused1, /* Start offset of token */
226672 int iUnused2 /* End offset of token */
226673){
226674 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
226675 Fts5Termset *pTermset = pCtx->pTermset;
226676 int bPresent;
226677 int ii;
226678 int rc = SQLITE_OK;
226679 int iPos;
226680 int iCol;
226681
226682 UNUSED_PARAM2(iUnused1, iUnused2);
226683 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
226684
226685 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
226686 pCtx->szCol++;
226687 }
226688
226689 switch( pCtx->pConfig->eDetail ){
226690 case FTS5_DETAIL_FULL:
226691 iPos = pCtx->szCol-1;
226692 iCol = pCtx->iCol;
226693 break;
226694
226695 case FTS5_DETAIL_COLUMNS:
226696 iPos = pCtx->iCol;
226697 iCol = 0;
226698 break;
226699
226700 default:
226701 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
226702 iPos = 0;
226703 iCol = 0;
226704 break;
226705 }
226706
226707 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
226708 if( rc==SQLITE_OK && bPresent==0 ){
226709 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
226710 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
226711 );
226712 }
226713
226714 for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
226715 const int nChar = pCtx->pConfig->aPrefix[ii];
226716 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
226717 if( nByte ){
226718 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
226719 if( bPresent==0 ){
226720 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
226721 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
226722 );
226723 }
226724 }
226725 }
226726
226727 return rc;
226728}
226729
226730/*
226731** Check that the contents of the FTS index match that of the %_content
226732** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
226733** some other SQLite error code if an error occurs while attempting to
226734** determine this.
226735*/
226736static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
226737 Fts5Config *pConfig = p->pConfig;
226738 int rc; /* Return code */
226739 int *aColSize; /* Array of size pConfig->nCol */
226740 i64 *aTotalSize; /* Array of size pConfig->nCol */
226741 Fts5IntegrityCtx ctx;
226742 sqlite3_stmt *pScan;
226743
226744 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
226745 ctx.pConfig = p->pConfig;
226746 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
226747 if( !aTotalSize ) return SQLITE_NOMEM;
226748 aColSize = (int*)&aTotalSize[pConfig->nCol];
226749 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
226750
226751 /* Generate the expected index checksum based on the contents of the
226752 ** %_content table. This block stores the checksum in ctx.cksum. */
226753 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
226754 if( rc==SQLITE_OK ){
226755 int rc2;
226756 while( SQLITE_ROW==sqlite3_step(pScan) ){
226757 int i;
226758 ctx.iRowid = sqlite3_column_int64(pScan, 0);
226759 ctx.szCol = 0;
226760 if( pConfig->bColumnsize ){
226761 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
226762 }
226763 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
226764 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
226765 }
226766 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
226767 if( pConfig->abUnindexed[i] ) continue;
226768 ctx.iCol = i;
226769 ctx.szCol = 0;
226770 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
226771 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
226772 }
226773 if( rc==SQLITE_OK ){
226774 const char *zText = (const char*)sqlite3_column_text(pScan, i+1);
226775 int nText = sqlite3_column_bytes(pScan, i+1);
226776 rc = sqlite3Fts5Tokenize(pConfig,
226777 FTS5_TOKENIZE_DOCUMENT,
226778 zText, nText,
226779 (void*)&ctx,
226780 fts5StorageIntegrityCallback
226781 );
226782 }
226783 if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
226784 rc = FTS5_CORRUPT;
226785 }
226786 aTotalSize[i] += ctx.szCol;
226787 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
226788 sqlite3Fts5TermsetFree(ctx.pTermset);
226789 ctx.pTermset = 0;
226790 }
226791 }
226792 sqlite3Fts5TermsetFree(ctx.pTermset);
226793 ctx.pTermset = 0;
226794
226795 if( rc!=SQLITE_OK ) break;
226796 }
226797 rc2 = sqlite3_reset(pScan);
226798 if( rc==SQLITE_OK ) rc = rc2;
226799 }
226800
226801 /* Test that the "totals" (sometimes called "averages") record looks Ok */
226802 if( rc==SQLITE_OK ){
226803 int i;
226804 rc = fts5StorageLoadTotals(p, 0);
226805 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
226806 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
226807 }
226808 }
226809
226810 /* Check that the %_docsize and %_content tables contain the expected
226811 ** number of rows. */
226812 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
226813 i64 nRow = 0;
226814 rc = fts5StorageCount(p, "content", &nRow);
226815 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
226816 }
226817 if( rc==SQLITE_OK && pConfig->bColumnsize ){
226818 i64 nRow = 0;
226819 rc = fts5StorageCount(p, "docsize", &nRow);
226820 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
226821 }
226822
226823 /* Pass the expected checksum down to the FTS index module. It will
226824 ** verify, amongst other things, that it matches the checksum generated by
226825 ** inspecting the index itself. */
226826 if( rc==SQLITE_OK ){
226827 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
226828 }
226829
226830 sqlite3_free(aTotalSize);
226831 return rc;
226832}
226833
226834/*
226835** Obtain an SQLite statement handle that may be used to read data from the
226836** %_content table.
226837*/
226838static int sqlite3Fts5StorageStmt(
226839 Fts5Storage *p,
226840 int eStmt,
226841 sqlite3_stmt **pp,
226842 char **pzErrMsg
226843){
226844 int rc;
226845 assert( eStmt==FTS5_STMT_SCAN_ASC
226846 || eStmt==FTS5_STMT_SCAN_DESC
226847 || eStmt==FTS5_STMT_LOOKUP
226848 );
226849 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
226850 if( rc==SQLITE_OK ){
226851 assert( p->aStmt[eStmt]==*pp );
226852 p->aStmt[eStmt] = 0;
226853 }
226854 return rc;
226855}
226856
226857/*
226858** Release an SQLite statement handle obtained via an earlier call to
226859** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
226860** must match that passed to the sqlite3Fts5StorageStmt() call.
226861*/
226862static void sqlite3Fts5StorageStmtRelease(
226863 Fts5Storage *p,
226864 int eStmt,
226865 sqlite3_stmt *pStmt
226866){
226867 assert( eStmt==FTS5_STMT_SCAN_ASC
226868 || eStmt==FTS5_STMT_SCAN_DESC
226869 || eStmt==FTS5_STMT_LOOKUP
226870 );
226871 if( p->aStmt[eStmt]==0 ){
226872 sqlite3_reset(pStmt);
226873 p->aStmt[eStmt] = pStmt;
226874 }else{
226875 sqlite3_finalize(pStmt);
226876 }
226877}
226878
226879static int fts5StorageDecodeSizeArray(
226880 int *aCol, int nCol, /* Array to populate */
226881 const u8 *aBlob, int nBlob /* Record to read varints from */
226882){
226883 int i;
226884 int iOff = 0;
226885 for(i=0; i<nCol; i++){
226886 if( iOff>=nBlob ) return 1;
226887 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
226888 }
226889 return (iOff!=nBlob);
226890}
226891
226892/*
226893** Argument aCol points to an array of integers containing one entry for
226894** each table column. This function reads the %_docsize record for the
226895** specified rowid and populates aCol[] with the results.
226896**
226897** An SQLite error code is returned if an error occurs, or SQLITE_OK
226898** otherwise.
226899*/
226900static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
226901 int nCol = p->pConfig->nCol; /* Number of user columns in table */
226902 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
226903 int rc; /* Return Code */
226904
226905 assert( p->pConfig->bColumnsize );
226906 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
226907 if( rc==SQLITE_OK ){
226908 int bCorrupt = 1;
226909 sqlite3_bind_int64(pLookup, 1, iRowid);
226910 if( SQLITE_ROW==sqlite3_step(pLookup) ){
226911 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
226912 int nBlob = sqlite3_column_bytes(pLookup, 0);
226913 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
226914 bCorrupt = 0;
226915 }
226916 }
226917 rc = sqlite3_reset(pLookup);
226918 if( bCorrupt && rc==SQLITE_OK ){
226919 rc = FTS5_CORRUPT;
226920 }
226921 }
226922
226923 return rc;
226924}
226925
226926static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
226927 int rc = fts5StorageLoadTotals(p, 0);
226928 if( rc==SQLITE_OK ){
226929 *pnToken = 0;
226930 if( iCol<0 ){
226931 int i;
226932 for(i=0; i<p->pConfig->nCol; i++){
226933 *pnToken += p->aTotalSize[i];
226934 }
226935 }else if( iCol<p->pConfig->nCol ){
226936 *pnToken = p->aTotalSize[iCol];
226937 }else{
226938 rc = SQLITE_RANGE;
226939 }
226940 }
226941 return rc;
226942}
226943
226944static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
226945 int rc = fts5StorageLoadTotals(p, 0);
226946 if( rc==SQLITE_OK ){
226947 /* nTotalRow being zero does not necessarily indicate a corrupt
226948 ** database - it might be that the FTS5 table really does contain zero
226949 ** rows. However this function is only called from the xRowCount() API,
226950 ** and there is no way for that API to be invoked if the table contains
226951 ** no rows. Hence the FTS5_CORRUPT return. */
226952 *pnRow = p->nTotalRow;
226953 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
226954 }
226955 return rc;
226956}
226957
226958/*
226959** Flush any data currently held in-memory to disk.
226960*/
226961static int sqlite3Fts5StorageSync(Fts5Storage *p){
226962 int rc = SQLITE_OK;
226963 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
226964 if( p->bTotalsValid ){
226965 rc = fts5StorageSaveTotals(p);
226966 p->bTotalsValid = 0;
226967 }
226968 if( rc==SQLITE_OK ){
226969 rc = sqlite3Fts5IndexSync(p->pIndex);
226970 }
226971 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
226972 return rc;
226973}
226974
226975static int sqlite3Fts5StorageRollback(Fts5Storage *p){
226976 p->bTotalsValid = 0;
226977 return sqlite3Fts5IndexRollback(p->pIndex);
226978}
226979
226980static int sqlite3Fts5StorageConfigValue(
226981 Fts5Storage *p,
226982 const char *z,
226983 sqlite3_value *pVal,
226984 int iVal
226985){
226986 sqlite3_stmt *pReplace = 0;
226987 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
226988 if( rc==SQLITE_OK ){
226989 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
226990 if( pVal ){
226991 sqlite3_bind_value(pReplace, 2, pVal);
226992 }else{
226993 sqlite3_bind_int(pReplace, 2, iVal);
226994 }
226995 sqlite3_step(pReplace);
226996 rc = sqlite3_reset(pReplace);
226997 sqlite3_bind_null(pReplace, 1);
226998 }
226999 if( rc==SQLITE_OK && pVal ){
227000 int iNew = p->pConfig->iCookie + 1;
227001 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
227002 if( rc==SQLITE_OK ){
227003 p->pConfig->iCookie = iNew;
227004 }
227005 }
227006 return rc;
227007}
227008
227009/*
227010** 2014 May 31
227011**
227012** The author disclaims copyright to this source code. In place of
227013** a legal notice, here is a blessing:
227014**
227015** May you do good and not evil.
227016** May you find forgiveness for yourself and forgive others.
227017** May you share freely, never taking more than you give.
227018**
227019******************************************************************************
227020*/
227021
227022
227023/* #include "fts5Int.h" */
227024
227025/**************************************************************************
227026** Start of ascii tokenizer implementation.
227027*/
227028
227029/*
227030** For tokenizers with no "unicode" modifier, the set of token characters
227031** is the same as the set of ASCII range alphanumeric characters.
227032*/
227033static unsigned char aAsciiTokenChar[128] = {
227034 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
227035 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
227036 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
227037 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
227038 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
227039 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
227040 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
227041 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
227042};
227043
227044typedef struct AsciiTokenizer AsciiTokenizer;
227045struct AsciiTokenizer {
227046 unsigned char aTokenChar[128];
227047};
227048
227049static void fts5AsciiAddExceptions(
227050 AsciiTokenizer *p,
227051 const char *zArg,
227052 int bTokenChars
227053){
227054 int i;
227055 for(i=0; zArg[i]; i++){
227056 if( (zArg[i] & 0x80)==0 ){
227057 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
227058 }
227059 }
227060}
227061
227062/*
227063** Delete a "ascii" tokenizer.
227064*/
227065static void fts5AsciiDelete(Fts5Tokenizer *p){
227066 sqlite3_free(p);
227067}
227068
227069/*
227070** Create an "ascii" tokenizer.
227071*/
227072static int fts5AsciiCreate(
227073 void *pUnused,
227074 const char **azArg, int nArg,
227075 Fts5Tokenizer **ppOut
227076){
227077 int rc = SQLITE_OK;
227078 AsciiTokenizer *p = 0;
227079 UNUSED_PARAM(pUnused);
227080 if( nArg%2 ){
227081 rc = SQLITE_ERROR;
227082 }else{
227083 p = sqlite3_malloc(sizeof(AsciiTokenizer));
227084 if( p==0 ){
227085 rc = SQLITE_NOMEM;
227086 }else{
227087 int i;
227088 memset(p, 0, sizeof(AsciiTokenizer));
227089 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
227090 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
227091 const char *zArg = azArg[i+1];
227092 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
227093 fts5AsciiAddExceptions(p, zArg, 1);
227094 }else
227095 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
227096 fts5AsciiAddExceptions(p, zArg, 0);
227097 }else{
227098 rc = SQLITE_ERROR;
227099 }
227100 }
227101 if( rc!=SQLITE_OK ){
227102 fts5AsciiDelete((Fts5Tokenizer*)p);
227103 p = 0;
227104 }
227105 }
227106 }
227107
227108 *ppOut = (Fts5Tokenizer*)p;
227109 return rc;
227110}
227111
227112
227113static void asciiFold(char *aOut, const char *aIn, int nByte){
227114 int i;
227115 for(i=0; i<nByte; i++){
227116 char c = aIn[i];
227117 if( c>='A' && c<='Z' ) c += 32;
227118 aOut[i] = c;
227119 }
227120}
227121
227122/*
227123** Tokenize some text using the ascii tokenizer.
227124*/
227125static int fts5AsciiTokenize(
227126 Fts5Tokenizer *pTokenizer,
227127 void *pCtx,
227128 int iUnused,
227129 const char *pText, int nText,
227130 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
227131){
227132 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
227133 int rc = SQLITE_OK;
227134 int ie;
227135 int is = 0;
227136
227137 char aFold[64];
227138 int nFold = sizeof(aFold);
227139 char *pFold = aFold;
227140 unsigned char *a = p->aTokenChar;
227141
227142 UNUSED_PARAM(iUnused);
227143
227144 while( is<nText && rc==SQLITE_OK ){
227145 int nByte;
227146
227147 /* Skip any leading divider characters. */
227148 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
227149 is++;
227150 }
227151 if( is==nText ) break;
227152
227153 /* Count the token characters */
227154 ie = is+1;
227155 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
227156 ie++;
227157 }
227158
227159 /* Fold to lower case */
227160 nByte = ie-is;
227161 if( nByte>nFold ){
227162 if( pFold!=aFold ) sqlite3_free(pFold);
227163 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
227164 if( pFold==0 ){
227165 rc = SQLITE_NOMEM;
227166 break;
227167 }
227168 nFold = nByte*2;
227169 }
227170 asciiFold(pFold, &pText[is], nByte);
227171
227172 /* Invoke the token callback */
227173 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
227174 is = ie+1;
227175 }
227176
227177 if( pFold!=aFold ) sqlite3_free(pFold);
227178 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
227179 return rc;
227180}
227181
227182/**************************************************************************
227183** Start of unicode61 tokenizer implementation.
227184*/
227185
227186
227187/*
227188** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
227189** from the sqlite3 source file utf.c. If this file is compiled as part
227190** of the amalgamation, they are not required.
227191*/
227192#ifndef SQLITE_AMALGAMATION
227193
227194static const unsigned char sqlite3Utf8Trans1[] = {
227195 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
227196 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
227197 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
227198 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
227199 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
227200 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
227201 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
227202 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
227203};
227204
227205#define READ_UTF8(zIn, zTerm, c) \
227206 c = *(zIn++); \
227207 if( c>=0xc0 ){ \
227208 c = sqlite3Utf8Trans1[c-0xc0]; \
227209 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
227210 c = (c<<6) + (0x3f & *(zIn++)); \
227211 } \
227212 if( c<0x80 \
227213 || (c&0xFFFFF800)==0xD800 \
227214 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
227215 }
227216
227217
227218#define WRITE_UTF8(zOut, c) { \
227219 if( c<0x00080 ){ \
227220 *zOut++ = (unsigned char)(c&0xFF); \
227221 } \
227222 else if( c<0x00800 ){ \
227223 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
227224 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
227225 } \
227226 else if( c<0x10000 ){ \
227227 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
227228 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
227229 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
227230 }else{ \
227231 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
227232 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
227233 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
227234 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
227235 } \
227236}
227237
227238#endif /* ifndef SQLITE_AMALGAMATION */
227239
227240typedef struct Unicode61Tokenizer Unicode61Tokenizer;
227241struct Unicode61Tokenizer {
227242 unsigned char aTokenChar[128]; /* ASCII range token characters */
227243 char *aFold; /* Buffer to fold text into */
227244 int nFold; /* Size of aFold[] in bytes */
227245 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
227246 int nException;
227247 int *aiException;
227248
227249 unsigned char aCategory[32]; /* True for token char categories */
227250};
227251
227252/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
227253#define FTS5_REMOVE_DIACRITICS_NONE 0
227254#define FTS5_REMOVE_DIACRITICS_SIMPLE 1
227255#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
227256
227257static int fts5UnicodeAddExceptions(
227258 Unicode61Tokenizer *p, /* Tokenizer object */
227259 const char *z, /* Characters to treat as exceptions */
227260 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
227261){
227262 int rc = SQLITE_OK;
227263 int n = (int)strlen(z);
227264 int *aNew;
227265
227266 if( n>0 ){
227267 aNew = (int*)sqlite3_realloc64(p->aiException,
227268 (n+p->nException)*sizeof(int));
227269 if( aNew ){
227270 int nNew = p->nException;
227271 const unsigned char *zCsr = (const unsigned char*)z;
227272 const unsigned char *zTerm = (const unsigned char*)&z[n];
227273 while( zCsr<zTerm ){
227274 u32 iCode;
227275 int bToken;
227276 READ_UTF8(zCsr, zTerm, iCode);
227277 if( iCode<128 ){
227278 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
227279 }else{
227280 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
227281 assert( (bToken==0 || bToken==1) );
227282 assert( (bTokenChars==0 || bTokenChars==1) );
227283 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
227284 int i;
227285 for(i=0; i<nNew; i++){
227286 if( (u32)aNew[i]>iCode ) break;
227287 }
227288 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
227289 aNew[i] = iCode;
227290 nNew++;
227291 }
227292 }
227293 }
227294 p->aiException = aNew;
227295 p->nException = nNew;
227296 }else{
227297 rc = SQLITE_NOMEM;
227298 }
227299 }
227300
227301 return rc;
227302}
227303
227304/*
227305** Return true if the p->aiException[] array contains the value iCode.
227306*/
227307static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
227308 if( p->nException>0 ){
227309 int *a = p->aiException;
227310 int iLo = 0;
227311 int iHi = p->nException-1;
227312
227313 while( iHi>=iLo ){
227314 int iTest = (iHi + iLo) / 2;
227315 if( iCode==a[iTest] ){
227316 return 1;
227317 }else if( iCode>a[iTest] ){
227318 iLo = iTest+1;
227319 }else{
227320 iHi = iTest-1;
227321 }
227322 }
227323 }
227324
227325 return 0;
227326}
227327
227328/*
227329** Delete a "unicode61" tokenizer.
227330*/
227331static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
227332 if( pTok ){
227333 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
227334 sqlite3_free(p->aiException);
227335 sqlite3_free(p->aFold);
227336 sqlite3_free(p);
227337 }
227338 return;
227339}
227340
227341static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
227342 const char *z = zCat;
227343
227344 while( *z ){
227345 while( *z==' ' || *z=='\t' ) z++;
227346 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
227347 return SQLITE_ERROR;
227348 }
227349 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
227350 }
227351
227352 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
227353 return SQLITE_OK;
227354}
227355
227356/*
227357** Create a "unicode61" tokenizer.
227358*/
227359static int fts5UnicodeCreate(
227360 void *pUnused,
227361 const char **azArg, int nArg,
227362 Fts5Tokenizer **ppOut
227363){
227364 int rc = SQLITE_OK; /* Return code */
227365 Unicode61Tokenizer *p = 0; /* New tokenizer object */
227366
227367 UNUSED_PARAM(pUnused);
227368
227369 if( nArg%2 ){
227370 rc = SQLITE_ERROR;
227371 }else{
227372 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
227373 if( p ){
227374 const char *zCat = "L* N* Co";
227375 int i;
227376 memset(p, 0, sizeof(Unicode61Tokenizer));
227377
227378 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
227379 p->nFold = 64;
227380 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
227381 if( p->aFold==0 ){
227382 rc = SQLITE_NOMEM;
227383 }
227384
227385 /* Search for a "categories" argument */
227386 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
227387 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
227388 zCat = azArg[i+1];
227389 }
227390 }
227391
227392 if( rc==SQLITE_OK ){
227393 rc = unicodeSetCategories(p, zCat);
227394 }
227395
227396 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
227397 const char *zArg = azArg[i+1];
227398 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
227399 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
227400 rc = SQLITE_ERROR;
227401 }else{
227402 p->eRemoveDiacritic = (zArg[0] - '0');
227403 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
227404 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
227405 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
227406 );
227407 }
227408 }else
227409 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
227410 rc = fts5UnicodeAddExceptions(p, zArg, 1);
227411 }else
227412 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
227413 rc = fts5UnicodeAddExceptions(p, zArg, 0);
227414 }else
227415 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
227416 /* no-op */
227417 }else{
227418 rc = SQLITE_ERROR;
227419 }
227420 }
227421
227422 }else{
227423 rc = SQLITE_NOMEM;
227424 }
227425 if( rc!=SQLITE_OK ){
227426 fts5UnicodeDelete((Fts5Tokenizer*)p);
227427 p = 0;
227428 }
227429 *ppOut = (Fts5Tokenizer*)p;
227430 }
227431 return rc;
227432}
227433
227434/*
227435** Return true if, for the purposes of tokenizing with the tokenizer
227436** passed as the first argument, codepoint iCode is considered a token
227437** character (not a separator).
227438*/
227439static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
227440 return (
227441 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
227442 ^ fts5UnicodeIsException(p, iCode)
227443 );
227444}
227445
227446static int fts5UnicodeTokenize(
227447 Fts5Tokenizer *pTokenizer,
227448 void *pCtx,
227449 int iUnused,
227450 const char *pText, int nText,
227451 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
227452){
227453 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
227454 int rc = SQLITE_OK;
227455 unsigned char *a = p->aTokenChar;
227456
227457 unsigned char *zTerm = (unsigned char*)&pText[nText];
227458 unsigned char *zCsr = (unsigned char *)pText;
227459
227460 /* Output buffer */
227461 char *aFold = p->aFold;
227462 int nFold = p->nFold;
227463 const char *pEnd = &aFold[nFold-6];
227464
227465 UNUSED_PARAM(iUnused);
227466
227467 /* Each iteration of this loop gobbles up a contiguous run of separators,
227468 ** then the next token. */
227469 while( rc==SQLITE_OK ){
227470 u32 iCode; /* non-ASCII codepoint read from input */
227471 char *zOut = aFold;
227472 int is;
227473 int ie;
227474
227475 /* Skip any separator characters. */
227476 while( 1 ){
227477 if( zCsr>=zTerm ) goto tokenize_done;
227478 if( *zCsr & 0x80 ) {
227479 /* A character outside of the ascii range. Skip past it if it is
227480 ** a separator character. Or break out of the loop if it is not. */
227481 is = zCsr - (unsigned char*)pText;
227482 READ_UTF8(zCsr, zTerm, iCode);
227483 if( fts5UnicodeIsAlnum(p, iCode) ){
227484 goto non_ascii_tokenchar;
227485 }
227486 }else{
227487 if( a[*zCsr] ){
227488 is = zCsr - (unsigned char*)pText;
227489 goto ascii_tokenchar;
227490 }
227491 zCsr++;
227492 }
227493 }
227494
227495 /* Run through the tokenchars. Fold them into the output buffer along
227496 ** the way. */
227497 while( zCsr<zTerm ){
227498
227499 /* Grow the output buffer so that there is sufficient space to fit the
227500 ** largest possible utf-8 character. */
227501 if( zOut>pEnd ){
227502 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
227503 if( aFold==0 ){
227504 rc = SQLITE_NOMEM;
227505 goto tokenize_done;
227506 }
227507 zOut = &aFold[zOut - p->aFold];
227508 memcpy(aFold, p->aFold, nFold);
227509 sqlite3_free(p->aFold);
227510 p->aFold = aFold;
227511 p->nFold = nFold = nFold*2;
227512 pEnd = &aFold[nFold-6];
227513 }
227514
227515 if( *zCsr & 0x80 ){
227516 /* An non-ascii-range character. Fold it into the output buffer if
227517 ** it is a token character, or break out of the loop if it is not. */
227518 READ_UTF8(zCsr, zTerm, iCode);
227519 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
227520 non_ascii_tokenchar:
227521 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
227522 if( iCode ) WRITE_UTF8(zOut, iCode);
227523 }else{
227524 break;
227525 }
227526 }else if( a[*zCsr]==0 ){
227527 /* An ascii-range separator character. End of token. */
227528 break;
227529 }else{
227530 ascii_tokenchar:
227531 if( *zCsr>='A' && *zCsr<='Z' ){
227532 *zOut++ = *zCsr + 32;
227533 }else{
227534 *zOut++ = *zCsr;
227535 }
227536 zCsr++;
227537 }
227538 ie = zCsr - (unsigned char*)pText;
227539 }
227540
227541 /* Invoke the token callback */
227542 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
227543 }
227544
227545 tokenize_done:
227546 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
227547 return rc;
227548}
227549
227550/**************************************************************************
227551** Start of porter stemmer implementation.
227552*/
227553
227554/* Any tokens larger than this (in bytes) are passed through without
227555** stemming. */
227556#define FTS5_PORTER_MAX_TOKEN 64
227557
227558typedef struct PorterTokenizer PorterTokenizer;
227559struct PorterTokenizer {
227560 fts5_tokenizer tokenizer; /* Parent tokenizer module */
227561 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
227562 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
227563};
227564
227565/*
227566** Delete a "porter" tokenizer.
227567*/
227568static void fts5PorterDelete(Fts5Tokenizer *pTok){
227569 if( pTok ){
227570 PorterTokenizer *p = (PorterTokenizer*)pTok;
227571 if( p->pTokenizer ){
227572 p->tokenizer.xDelete(p->pTokenizer);
227573 }
227574 sqlite3_free(p);
227575 }
227576}
227577
227578/*
227579** Create a "porter" tokenizer.
227580*/
227581static int fts5PorterCreate(
227582 void *pCtx,
227583 const char **azArg, int nArg,
227584 Fts5Tokenizer **ppOut
227585){
227586 fts5_api *pApi = (fts5_api*)pCtx;
227587 int rc = SQLITE_OK;
227588 PorterTokenizer *pRet;
227589 void *pUserdata = 0;
227590 const char *zBase = "unicode61";
227591
227592 if( nArg>0 ){
227593 zBase = azArg[0];
227594 }
227595
227596 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
227597 if( pRet ){
227598 memset(pRet, 0, sizeof(PorterTokenizer));
227599 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
227600 }else{
227601 rc = SQLITE_NOMEM;
227602 }
227603 if( rc==SQLITE_OK ){
227604 int nArg2 = (nArg>0 ? nArg-1 : 0);
227605 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
227606 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
227607 }
227608
227609 if( rc!=SQLITE_OK ){
227610 fts5PorterDelete((Fts5Tokenizer*)pRet);
227611 pRet = 0;
227612 }
227613 *ppOut = (Fts5Tokenizer*)pRet;
227614 return rc;
227615}
227616
227617typedef struct PorterContext PorterContext;
227618struct PorterContext {
227619 void *pCtx;
227620 int (*xToken)(void*, int, const char*, int, int, int);
227621 char *aBuf;
227622};
227623
227624typedef struct PorterRule PorterRule;
227625struct PorterRule {
227626 const char *zSuffix;
227627 int nSuffix;
227628 int (*xCond)(char *zStem, int nStem);
227629 const char *zOutput;
227630 int nOutput;
227631};
227632
227633#if 0
227634static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
227635 int ret = -1;
227636 int nBuf = *pnBuf;
227637 PorterRule *p;
227638
227639 for(p=aRule; p->zSuffix; p++){
227640 assert( strlen(p->zSuffix)==p->nSuffix );
227641 assert( strlen(p->zOutput)==p->nOutput );
227642 if( nBuf<p->nSuffix ) continue;
227643 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
227644 }
227645
227646 if( p->zSuffix ){
227647 int nStem = nBuf - p->nSuffix;
227648 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
227649 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
227650 *pnBuf = nStem + p->nOutput;
227651 ret = p - aRule;
227652 }
227653 }
227654
227655 return ret;
227656}
227657#endif
227658
227659static int fts5PorterIsVowel(char c, int bYIsVowel){
227660 return (
227661 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
227662 );
227663}
227664
227665static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
227666 int i;
227667 int bCons = bPrevCons;
227668
227669 /* Scan for a vowel */
227670 for(i=0; i<nStem; i++){
227671 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
227672 }
227673
227674 /* Scan for a consonent */
227675 for(i++; i<nStem; i++){
227676 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
227677 }
227678 return 0;
227679}
227680
227681/* porter rule condition: (m > 0) */
227682static int fts5Porter_MGt0(char *zStem, int nStem){
227683 return !!fts5PorterGobbleVC(zStem, nStem, 0);
227684}
227685
227686/* porter rule condition: (m > 1) */
227687static int fts5Porter_MGt1(char *zStem, int nStem){
227688 int n;
227689 n = fts5PorterGobbleVC(zStem, nStem, 0);
227690 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
227691 return 1;
227692 }
227693 return 0;
227694}
227695
227696/* porter rule condition: (m = 1) */
227697static int fts5Porter_MEq1(char *zStem, int nStem){
227698 int n;
227699 n = fts5PorterGobbleVC(zStem, nStem, 0);
227700 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
227701 return 1;
227702 }
227703 return 0;
227704}
227705
227706/* porter rule condition: (*o) */
227707static int fts5Porter_Ostar(char *zStem, int nStem){
227708 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
227709 return 0;
227710 }else{
227711 int i;
227712 int mask = 0;
227713 int bCons = 0;
227714 for(i=0; i<nStem; i++){
227715 bCons = !fts5PorterIsVowel(zStem[i], bCons);
227716 assert( bCons==0 || bCons==1 );
227717 mask = (mask << 1) + bCons;
227718 }
227719 return ((mask & 0x0007)==0x0005);
227720 }
227721}
227722
227723/* porter rule condition: (m > 1 and (*S or *T)) */
227724static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
227725 assert( nStem>0 );
227726 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
227727 && fts5Porter_MGt1(zStem, nStem);
227728}
227729
227730/* porter rule condition: (*v*) */
227731static int fts5Porter_Vowel(char *zStem, int nStem){
227732 int i;
227733 for(i=0; i<nStem; i++){
227734 if( fts5PorterIsVowel(zStem[i], i>0) ){
227735 return 1;
227736 }
227737 }
227738 return 0;
227739}
227740
227741
227742/**************************************************************************
227743***************************************************************************
227744** GENERATED CODE STARTS HERE (mkportersteps.tcl)
227745*/
227746
227747static int fts5PorterStep4(char *aBuf, int *pnBuf){
227748 int ret = 0;
227749 int nBuf = *pnBuf;
227750 switch( aBuf[nBuf-2] ){
227751
227752 case 'a':
227753 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
227754 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227755 *pnBuf = nBuf - 2;
227756 }
227757 }
227758 break;
227759
227760 case 'c':
227761 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
227762 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227763 *pnBuf = nBuf - 4;
227764 }
227765 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
227766 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227767 *pnBuf = nBuf - 4;
227768 }
227769 }
227770 break;
227771
227772 case 'e':
227773 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
227774 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227775 *pnBuf = nBuf - 2;
227776 }
227777 }
227778 break;
227779
227780 case 'i':
227781 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
227782 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227783 *pnBuf = nBuf - 2;
227784 }
227785 }
227786 break;
227787
227788 case 'l':
227789 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
227790 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227791 *pnBuf = nBuf - 4;
227792 }
227793 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
227794 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227795 *pnBuf = nBuf - 4;
227796 }
227797 }
227798 break;
227799
227800 case 'n':
227801 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
227802 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227803 *pnBuf = nBuf - 3;
227804 }
227805 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
227806 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
227807 *pnBuf = nBuf - 5;
227808 }
227809 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
227810 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
227811 *pnBuf = nBuf - 4;
227812 }
227813 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
227814 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227815 *pnBuf = nBuf - 3;
227816 }
227817 }
227818 break;
227819
227820 case 'o':
227821 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
227822 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
227823 *pnBuf = nBuf - 3;
227824 }
227825 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
227826 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
227827 *pnBuf = nBuf - 2;
227828 }
227829 }
227830 break;
227831
227832 case 's':
227833 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
227834 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227835 *pnBuf = nBuf - 3;
227836 }
227837 }
227838 break;
227839
227840 case 't':
227841 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
227842 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227843 *pnBuf = nBuf - 3;
227844 }
227845 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
227846 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227847 *pnBuf = nBuf - 3;
227848 }
227849 }
227850 break;
227851
227852 case 'u':
227853 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
227854 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227855 *pnBuf = nBuf - 3;
227856 }
227857 }
227858 break;
227859
227860 case 'v':
227861 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
227862 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227863 *pnBuf = nBuf - 3;
227864 }
227865 }
227866 break;
227867
227868 case 'z':
227869 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
227870 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
227871 *pnBuf = nBuf - 3;
227872 }
227873 }
227874 break;
227875
227876 }
227877 return ret;
227878}
227879
227880
227881static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
227882 int ret = 0;
227883 int nBuf = *pnBuf;
227884 switch( aBuf[nBuf-2] ){
227885
227886 case 'a':
227887 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
227888 memcpy(&aBuf[nBuf-2], "ate", 3);
227889 *pnBuf = nBuf - 2 + 3;
227890 ret = 1;
227891 }
227892 break;
227893
227894 case 'b':
227895 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
227896 memcpy(&aBuf[nBuf-2], "ble", 3);
227897 *pnBuf = nBuf - 2 + 3;
227898 ret = 1;
227899 }
227900 break;
227901
227902 case 'i':
227903 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
227904 memcpy(&aBuf[nBuf-2], "ize", 3);
227905 *pnBuf = nBuf - 2 + 3;
227906 ret = 1;
227907 }
227908 break;
227909
227910 }
227911 return ret;
227912}
227913
227914
227915static int fts5PorterStep2(char *aBuf, int *pnBuf){
227916 int ret = 0;
227917 int nBuf = *pnBuf;
227918 switch( aBuf[nBuf-2] ){
227919
227920 case 'a':
227921 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
227922 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227923 memcpy(&aBuf[nBuf-7], "ate", 3);
227924 *pnBuf = nBuf - 7 + 3;
227925 }
227926 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
227927 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
227928 memcpy(&aBuf[nBuf-6], "tion", 4);
227929 *pnBuf = nBuf - 6 + 4;
227930 }
227931 }
227932 break;
227933
227934 case 'c':
227935 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
227936 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227937 memcpy(&aBuf[nBuf-4], "ence", 4);
227938 *pnBuf = nBuf - 4 + 4;
227939 }
227940 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
227941 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227942 memcpy(&aBuf[nBuf-4], "ance", 4);
227943 *pnBuf = nBuf - 4 + 4;
227944 }
227945 }
227946 break;
227947
227948 case 'e':
227949 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
227950 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227951 memcpy(&aBuf[nBuf-4], "ize", 3);
227952 *pnBuf = nBuf - 4 + 3;
227953 }
227954 }
227955 break;
227956
227957 case 'g':
227958 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
227959 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227960 memcpy(&aBuf[nBuf-4], "log", 3);
227961 *pnBuf = nBuf - 4 + 3;
227962 }
227963 }
227964 break;
227965
227966 case 'l':
227967 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
227968 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227969 memcpy(&aBuf[nBuf-3], "ble", 3);
227970 *pnBuf = nBuf - 3 + 3;
227971 }
227972 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
227973 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
227974 memcpy(&aBuf[nBuf-4], "al", 2);
227975 *pnBuf = nBuf - 4 + 2;
227976 }
227977 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
227978 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227979 memcpy(&aBuf[nBuf-5], "ent", 3);
227980 *pnBuf = nBuf - 5 + 3;
227981 }
227982 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
227983 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
227984 memcpy(&aBuf[nBuf-3], "e", 1);
227985 *pnBuf = nBuf - 3 + 1;
227986 }
227987 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
227988 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
227989 memcpy(&aBuf[nBuf-5], "ous", 3);
227990 *pnBuf = nBuf - 5 + 3;
227991 }
227992 }
227993 break;
227994
227995 case 'o':
227996 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
227997 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
227998 memcpy(&aBuf[nBuf-7], "ize", 3);
227999 *pnBuf = nBuf - 7 + 3;
228000 }
228001 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
228002 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228003 memcpy(&aBuf[nBuf-5], "ate", 3);
228004 *pnBuf = nBuf - 5 + 3;
228005 }
228006 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
228007 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
228008 memcpy(&aBuf[nBuf-4], "ate", 3);
228009 *pnBuf = nBuf - 4 + 3;
228010 }
228011 }
228012 break;
228013
228014 case 's':
228015 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
228016 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228017 memcpy(&aBuf[nBuf-5], "al", 2);
228018 *pnBuf = nBuf - 5 + 2;
228019 }
228020 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
228021 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
228022 memcpy(&aBuf[nBuf-7], "ive", 3);
228023 *pnBuf = nBuf - 7 + 3;
228024 }
228025 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
228026 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
228027 memcpy(&aBuf[nBuf-7], "ful", 3);
228028 *pnBuf = nBuf - 7 + 3;
228029 }
228030 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
228031 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
228032 memcpy(&aBuf[nBuf-7], "ous", 3);
228033 *pnBuf = nBuf - 7 + 3;
228034 }
228035 }
228036 break;
228037
228038 case 't':
228039 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
228040 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228041 memcpy(&aBuf[nBuf-5], "al", 2);
228042 *pnBuf = nBuf - 5 + 2;
228043 }
228044 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
228045 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228046 memcpy(&aBuf[nBuf-5], "ive", 3);
228047 *pnBuf = nBuf - 5 + 3;
228048 }
228049 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
228050 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
228051 memcpy(&aBuf[nBuf-6], "ble", 3);
228052 *pnBuf = nBuf - 6 + 3;
228053 }
228054 }
228055 break;
228056
228057 }
228058 return ret;
228059}
228060
228061
228062static int fts5PorterStep3(char *aBuf, int *pnBuf){
228063 int ret = 0;
228064 int nBuf = *pnBuf;
228065 switch( aBuf[nBuf-2] ){
228066
228067 case 'a':
228068 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
228069 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
228070 memcpy(&aBuf[nBuf-4], "ic", 2);
228071 *pnBuf = nBuf - 4 + 2;
228072 }
228073 }
228074 break;
228075
228076 case 's':
228077 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
228078 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
228079 *pnBuf = nBuf - 4;
228080 }
228081 }
228082 break;
228083
228084 case 't':
228085 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
228086 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228087 memcpy(&aBuf[nBuf-5], "ic", 2);
228088 *pnBuf = nBuf - 5 + 2;
228089 }
228090 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
228091 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228092 memcpy(&aBuf[nBuf-5], "ic", 2);
228093 *pnBuf = nBuf - 5 + 2;
228094 }
228095 }
228096 break;
228097
228098 case 'u':
228099 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
228100 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
228101 *pnBuf = nBuf - 3;
228102 }
228103 }
228104 break;
228105
228106 case 'v':
228107 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
228108 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228109 *pnBuf = nBuf - 5;
228110 }
228111 }
228112 break;
228113
228114 case 'z':
228115 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
228116 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
228117 memcpy(&aBuf[nBuf-5], "al", 2);
228118 *pnBuf = nBuf - 5 + 2;
228119 }
228120 }
228121 break;
228122
228123 }
228124 return ret;
228125}
228126
228127
228128static int fts5PorterStep1B(char *aBuf, int *pnBuf){
228129 int ret = 0;
228130 int nBuf = *pnBuf;
228131 switch( aBuf[nBuf-2] ){
228132
228133 case 'e':
228134 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
228135 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
228136 memcpy(&aBuf[nBuf-3], "ee", 2);
228137 *pnBuf = nBuf - 3 + 2;
228138 }
228139 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
228140 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
228141 *pnBuf = nBuf - 2;
228142 ret = 1;
228143 }
228144 }
228145 break;
228146
228147 case 'n':
228148 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
228149 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
228150 *pnBuf = nBuf - 3;
228151 ret = 1;
228152 }
228153 }
228154 break;
228155
228156 }
228157 return ret;
228158}
228159
228160/*
228161** GENERATED CODE ENDS HERE (mkportersteps.tcl)
228162***************************************************************************
228163**************************************************************************/
228164
228165static void fts5PorterStep1A(char *aBuf, int *pnBuf){
228166 int nBuf = *pnBuf;
228167 if( aBuf[nBuf-1]=='s' ){
228168 if( aBuf[nBuf-2]=='e' ){
228169 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
228170 || (nBuf>3 && aBuf[nBuf-3]=='i' )
228171 ){
228172 *pnBuf = nBuf-2;
228173 }else{
228174 *pnBuf = nBuf-1;
228175 }
228176 }
228177 else if( aBuf[nBuf-2]!='s' ){
228178 *pnBuf = nBuf-1;
228179 }
228180 }
228181}
228182
228183static int fts5PorterCb(
228184 void *pCtx,
228185 int tflags,
228186 const char *pToken,
228187 int nToken,
228188 int iStart,
228189 int iEnd
228190){
228191 PorterContext *p = (PorterContext*)pCtx;
228192
228193 char *aBuf;
228194 int nBuf;
228195
228196 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
228197 aBuf = p->aBuf;
228198 nBuf = nToken;
228199 memcpy(aBuf, pToken, nBuf);
228200
228201 /* Step 1. */
228202 fts5PorterStep1A(aBuf, &nBuf);
228203 if( fts5PorterStep1B(aBuf, &nBuf) ){
228204 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
228205 char c = aBuf[nBuf-1];
228206 if( fts5PorterIsVowel(c, 0)==0
228207 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
228208 ){
228209 nBuf--;
228210 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
228211 aBuf[nBuf++] = 'e';
228212 }
228213 }
228214 }
228215
228216 /* Step 1C. */
228217 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
228218 aBuf[nBuf-1] = 'i';
228219 }
228220
228221 /* Steps 2 through 4. */
228222 fts5PorterStep2(aBuf, &nBuf);
228223 fts5PorterStep3(aBuf, &nBuf);
228224 fts5PorterStep4(aBuf, &nBuf);
228225
228226 /* Step 5a. */
228227 assert( nBuf>0 );
228228 if( aBuf[nBuf-1]=='e' ){
228229 if( fts5Porter_MGt1(aBuf, nBuf-1)
228230 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
228231 ){
228232 nBuf--;
228233 }
228234 }
228235
228236 /* Step 5b. */
228237 if( nBuf>1 && aBuf[nBuf-1]=='l'
228238 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
228239 ){
228240 nBuf--;
228241 }
228242
228243 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
228244
228245 pass_through:
228246 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
228247}
228248
228249/*
228250** Tokenize using the porter tokenizer.
228251*/
228252static int fts5PorterTokenize(
228253 Fts5Tokenizer *pTokenizer,
228254 void *pCtx,
228255 int flags,
228256 const char *pText, int nText,
228257 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
228258){
228259 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
228260 PorterContext sCtx;
228261 sCtx.xToken = xToken;
228262 sCtx.pCtx = pCtx;
228263 sCtx.aBuf = p->aBuf;
228264 return p->tokenizer.xTokenize(
228265 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
228266 );
228267}
228268
228269/*
228270** Register all built-in tokenizers with FTS5.
228271*/
228272static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
228273 struct BuiltinTokenizer {
228274 const char *zName;
228275 fts5_tokenizer x;
228276 } aBuiltin[] = {
228277 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
228278 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
228279 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
228280 };
228281
228282 int rc = SQLITE_OK; /* Return code */
228283 int i; /* To iterate through builtin functions */
228284
228285 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
228286 rc = pApi->xCreateTokenizer(pApi,
228287 aBuiltin[i].zName,
228288 (void*)pApi,
228289 &aBuiltin[i].x,
228290 0
228291 );
228292 }
228293
228294 return rc;
228295}
228296
228297/*
228298** 2012-05-25
228299**
228300** The author disclaims copyright to this source code. In place of
228301** a legal notice, here is a blessing:
228302**
228303** May you do good and not evil.
228304** May you find forgiveness for yourself and forgive others.
228305** May you share freely, never taking more than you give.
228306**
228307******************************************************************************
228308*/
228309
228310/*
228311** DO NOT EDIT THIS MACHINE GENERATED FILE.
228312*/
228313
228314
228315/* #include <assert.h> */
228316
228317
228318
228319/*
228320** If the argument is a codepoint corresponding to a lowercase letter
228321** in the ASCII range with a diacritic added, return the codepoint
228322** of the ASCII letter only. For example, if passed 235 - "LATIN
228323** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
228324** E"). The resuls of passing a codepoint that corresponds to an
228325** uppercase letter are undefined.
228326*/
228327static int fts5_remove_diacritic(int c, int bComplex){
228328 unsigned short aDia[] = {
228329 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
228330 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
228331 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
228332 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
228333 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
228334 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
228335 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
228336 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
228337 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
228338 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
228339 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
228340 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
228341 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
228342 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
228343 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
228344 63182, 63242, 63274, 63310, 63368, 63390,
228345 };
228346#define HIBIT ((unsigned char)0x80)
228347 unsigned char aChar[] = {
228348 '\0', 'a', 'c', 'e', 'i', 'n',
228349 'o', 'u', 'y', 'y', 'a', 'c',
228350 'd', 'e', 'e', 'g', 'h', 'i',
228351 'j', 'k', 'l', 'n', 'o', 'r',
228352 's', 't', 'u', 'u', 'w', 'y',
228353 'z', 'o', 'u', 'a', 'i', 'o',
228354 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
228355 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
228356 'e', 'i', 'o', 'r', 'u', 's',
228357 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
228358 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
228359 '\0', '\0', '\0', '\0', 'a', 'b',
228360 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
228361 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
228362 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
228363 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
228364 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
228365 'w', 'x', 'y', 'z', 'h', 't',
228366 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
228367 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
228368 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
228369 };
228370
228371 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
228372 int iRes = 0;
228373 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
228374 int iLo = 0;
228375 while( iHi>=iLo ){
228376 int iTest = (iHi + iLo) / 2;
228377 if( key >= aDia[iTest] ){
228378 iRes = iTest;
228379 iLo = iTest+1;
228380 }else{
228381 iHi = iTest-1;
228382 }
228383 }
228384 assert( key>=aDia[iRes] );
228385 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
228386 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
228387}
228388
228389
228390/*
228391** Return true if the argument interpreted as a unicode codepoint
228392** is a diacritical modifier character.
228393*/
228394static int sqlite3Fts5UnicodeIsdiacritic(int c){
228395 unsigned int mask0 = 0x08029FDF;
228396 unsigned int mask1 = 0x000361F8;
228397 if( c<768 || c>817 ) return 0;
228398 return (c < 768+32) ?
228399 (mask0 & ((unsigned int)1 << (c-768))) :
228400 (mask1 & ((unsigned int)1 << (c-768-32)));
228401}
228402
228403
228404/*
228405** Interpret the argument as a unicode codepoint. If the codepoint
228406** is an upper case character that has a lower case equivalent,
228407** return the codepoint corresponding to the lower case version.
228408** Otherwise, return a copy of the argument.
228409**
228410** The results are undefined if the value passed to this function
228411** is less than zero.
228412*/
228413static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
228414 /* Each entry in the following array defines a rule for folding a range
228415 ** of codepoints to lower case. The rule applies to a range of nRange
228416 ** codepoints starting at codepoint iCode.
228417 **
228418 ** If the least significant bit in flags is clear, then the rule applies
228419 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
228420 ** need to be folded). Or, if it is set, then the rule only applies to
228421 ** every second codepoint in the range, starting with codepoint C.
228422 **
228423 ** The 7 most significant bits in flags are an index into the aiOff[]
228424 ** array. If a specific codepoint C does require folding, then its lower
228425 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
228426 **
228427 ** The contents of this array are generated by parsing the CaseFolding.txt
228428 ** file distributed as part of the "Unicode Character Database". See
228429 ** http://www.unicode.org for details.
228430 */
228431 static const struct TableEntry {
228432 unsigned short iCode;
228433 unsigned char flags;
228434 unsigned char nRange;
228435 } aEntry[] = {
228436 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
228437 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
228438 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
228439 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
228440 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
228441 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
228442 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
228443 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
228444 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
228445 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
228446 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
228447 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
228448 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
228449 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
228450 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
228451 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
228452 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
228453 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
228454 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
228455 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
228456 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
228457 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
228458 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
228459 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
228460 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
228461 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
228462 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
228463 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
228464 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
228465 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
228466 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
228467 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
228468 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
228469 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
228470 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
228471 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
228472 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
228473 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
228474 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
228475 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
228476 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
228477 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
228478 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
228479 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
228480 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
228481 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
228482 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
228483 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
228484 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
228485 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
228486 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
228487 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
228488 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
228489 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
228490 {65313, 14, 26},
228491 };
228492 static const unsigned short aiOff[] = {
228493 1, 2, 8, 15, 16, 26, 28, 32,
228494 37, 38, 40, 48, 63, 64, 69, 71,
228495 79, 80, 116, 202, 203, 205, 206, 207,
228496 209, 210, 211, 213, 214, 217, 218, 219,
228497 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
228498 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
228499 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
228500 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
228501 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
228502 65514, 65521, 65527, 65528, 65529,
228503 };
228504
228505 int ret = c;
228506
228507 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
228508
228509 if( c<128 ){
228510 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
228511 }else if( c<65536 ){
228512 const struct TableEntry *p;
228513 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
228514 int iLo = 0;
228515 int iRes = -1;
228516
228517 assert( c>aEntry[0].iCode );
228518 while( iHi>=iLo ){
228519 int iTest = (iHi + iLo) / 2;
228520 int cmp = (c - aEntry[iTest].iCode);
228521 if( cmp>=0 ){
228522 iRes = iTest;
228523 iLo = iTest+1;
228524 }else{
228525 iHi = iTest-1;
228526 }
228527 }
228528
228529 assert( iRes>=0 && c>=aEntry[iRes].iCode );
228530 p = &aEntry[iRes];
228531 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
228532 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
228533 assert( ret>0 );
228534 }
228535
228536 if( eRemoveDiacritic ){
228537 ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
228538 }
228539 }
228540
228541 else if( c>=66560 && c<66600 ){
228542 ret = c + 40;
228543 }
228544
228545 return ret;
228546}
228547
228548
228549static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
228550 aArray[0] = 1;
228551 switch( zCat[0] ){
228552 case 'C':
228553 switch( zCat[1] ){
228554 case 'c': aArray[1] = 1; break;
228555 case 'f': aArray[2] = 1; break;
228556 case 'n': aArray[3] = 1; break;
228557 case 's': aArray[4] = 1; break;
228558 case 'o': aArray[31] = 1; break;
228559 case '*':
228560 aArray[1] = 1;
228561 aArray[2] = 1;
228562 aArray[3] = 1;
228563 aArray[4] = 1;
228564 aArray[31] = 1;
228565 break;
228566 default: return 1; }
228567 break;
228568
228569 case 'L':
228570 switch( zCat[1] ){
228571 case 'l': aArray[5] = 1; break;
228572 case 'm': aArray[6] = 1; break;
228573 case 'o': aArray[7] = 1; break;
228574 case 't': aArray[8] = 1; break;
228575 case 'u': aArray[9] = 1; break;
228576 case 'C': aArray[30] = 1; break;
228577 case '*':
228578 aArray[5] = 1;
228579 aArray[6] = 1;
228580 aArray[7] = 1;
228581 aArray[8] = 1;
228582 aArray[9] = 1;
228583 aArray[30] = 1;
228584 break;
228585 default: return 1; }
228586 break;
228587
228588 case 'M':
228589 switch( zCat[1] ){
228590 case 'c': aArray[10] = 1; break;
228591 case 'e': aArray[11] = 1; break;
228592 case 'n': aArray[12] = 1; break;
228593 case '*':
228594 aArray[10] = 1;
228595 aArray[11] = 1;
228596 aArray[12] = 1;
228597 break;
228598 default: return 1; }
228599 break;
228600
228601 case 'N':
228602 switch( zCat[1] ){
228603 case 'd': aArray[13] = 1; break;
228604 case 'l': aArray[14] = 1; break;
228605 case 'o': aArray[15] = 1; break;
228606 case '*':
228607 aArray[13] = 1;
228608 aArray[14] = 1;
228609 aArray[15] = 1;
228610 break;
228611 default: return 1; }
228612 break;
228613
228614 case 'P':
228615 switch( zCat[1] ){
228616 case 'c': aArray[16] = 1; break;
228617 case 'd': aArray[17] = 1; break;
228618 case 'e': aArray[18] = 1; break;
228619 case 'f': aArray[19] = 1; break;
228620 case 'i': aArray[20] = 1; break;
228621 case 'o': aArray[21] = 1; break;
228622 case 's': aArray[22] = 1; break;
228623 case '*':
228624 aArray[16] = 1;
228625 aArray[17] = 1;
228626 aArray[18] = 1;
228627 aArray[19] = 1;
228628 aArray[20] = 1;
228629 aArray[21] = 1;
228630 aArray[22] = 1;
228631 break;
228632 default: return 1; }
228633 break;
228634
228635 case 'S':
228636 switch( zCat[1] ){
228637 case 'c': aArray[23] = 1; break;
228638 case 'k': aArray[24] = 1; break;
228639 case 'm': aArray[25] = 1; break;
228640 case 'o': aArray[26] = 1; break;
228641 case '*':
228642 aArray[23] = 1;
228643 aArray[24] = 1;
228644 aArray[25] = 1;
228645 aArray[26] = 1;
228646 break;
228647 default: return 1; }
228648 break;
228649
228650 case 'Z':
228651 switch( zCat[1] ){
228652 case 'l': aArray[27] = 1; break;
228653 case 'p': aArray[28] = 1; break;
228654 case 's': aArray[29] = 1; break;
228655 case '*':
228656 aArray[27] = 1;
228657 aArray[28] = 1;
228658 aArray[29] = 1;
228659 break;
228660 default: return 1; }
228661 break;
228662
228663 }
228664 return 0;
228665}
228666
228667static u16 aFts5UnicodeBlock[] = {
228668 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
228669 1760, 1760, 1760, 1760, 1760, 1763, 1765,
228670 };
228671static u16 aFts5UnicodeMap[] = {
228672 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
228673 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
228674 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
228675 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
228676 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
228677 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
228678 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
228679 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
228680 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
228681 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
228682 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
228683 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
228684 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
228685 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
228686 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
228687 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
228688 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
228689 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
228690 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
228691 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
228692 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
228693 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
228694 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
228695 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
228696 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
228697 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
228698 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
228699 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
228700 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
228701 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
228702 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
228703 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
228704 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
228705 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
228706 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
228707 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
228708 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
228709 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
228710 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
228711 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
228712 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
228713 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
228714 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
228715 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
228716 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
228717 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
228718 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
228719 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
228720 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
228721 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
228722 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
228723 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
228724 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
228725 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
228726 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
228727 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
228728 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
228729 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
228730 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
228731 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
228732 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
228733 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
228734 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
228735 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
228736 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
228737 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
228738 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
228739 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
228740 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
228741 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
228742 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
228743 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
228744 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
228745 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
228746 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
228747 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
228748 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
228749 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
228750 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
228751 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
228752 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
228753 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
228754 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
228755 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
228756 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
228757 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
228758 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
228759 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
228760 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
228761 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
228762 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
228763 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
228764 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
228765 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
228766 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
228767 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
228768 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
228769 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
228770 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
228771 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
228772 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
228773 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
228774 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
228775 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
228776 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
228777 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
228778 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
228779 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
228780 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
228781 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
228782 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
228783 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
228784 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
228785 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
228786 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
228787 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
228788 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
228789 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
228790 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
228791 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
228792 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
228793 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
228794 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
228795 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
228796 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
228797 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
228798 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
228799 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
228800 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
228801 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
228802 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
228803 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
228804 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
228805 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
228806 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
228807 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
228808 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
228809 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
228810 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
228811 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
228812 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
228813 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
228814 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
228815 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
228816 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
228817 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
228818 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
228819 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
228820 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
228821 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
228822 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
228823 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
228824 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
228825 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
228826 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
228827 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
228828 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
228829 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
228830 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
228831 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
228832 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
228833 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
228834 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
228835 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
228836 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
228837 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
228838 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
228839 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
228840 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
228841 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
228842 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
228843 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
228844 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
228845 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
228846 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
228847 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
228848 1, 32, 256, 0, 65533,
228849 };
228850static u16 aFts5UnicodeData[] = {
228851 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
228852 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
228853 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
228854 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
228855 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
228856 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
228857 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
228858 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
228859 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
228860 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
228861 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
228862 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
228863 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
228864 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
228865 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
228866 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
228867 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
228868 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
228869 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
228870 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
228871 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
228872 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
228873 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
228874 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
228875 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
228876 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
228877 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
228878 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
228879 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
228880 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
228881 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
228882 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
228883 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
228884 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
228885 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
228886 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
228887 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
228888 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
228889 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
228890 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
228891 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
228892 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
228893 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
228894 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
228895 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
228896 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
228897 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
228898 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
228899 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
228900 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
228901 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
228902 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
228903 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
228904 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
228905 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
228906 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
228907 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
228908 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
228909 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
228910 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
228911 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
228912 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
228913 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
228914 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
228915 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
228916 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
228917 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
228918 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
228919 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
228920 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
228921 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
228922 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
228923 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
228924 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
228925 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
228926 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
228927 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
228928 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
228929 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
228930 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
228931 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
228932 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
228933 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
228934 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
228935 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
228936 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
228937 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
228938 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
228939 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
228940 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
228941 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
228942 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
228943 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
228944 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
228945 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
228946 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
228947 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
228948 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
228949 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
228950 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
228951 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
228952 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
228953 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
228954 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
228955 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
228956 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
228957 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
228958 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
228959 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
228960 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
228961 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
228962 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
228963 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
228964 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
228965 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
228966 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
228967 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
228968 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
228969 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
228970 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
228971 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
228972 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
228973 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
228974 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
228975 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
228976 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
228977 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
228978 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
228979 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
228980 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
228981 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
228982 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
228983 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
228984 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
228985 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
228986 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
228987 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
228988 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
228989 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
228990 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
228991 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
228992 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
228993 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
228994 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
228995 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
228996 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
228997 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
228998 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
228999 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
229000 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
229001 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
229002 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
229003 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
229004 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
229005 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
229006 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
229007 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
229008 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
229009 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
229010 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
229011 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
229012 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
229013 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
229014 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
229015 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
229016 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
229017 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
229018 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
229019 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
229020 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
229021 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
229022 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
229023 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
229024 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
229025 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
229026 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
229027 34, 3074, 7692, 63, 63,
229028 };
229029
229030static int sqlite3Fts5UnicodeCategory(u32 iCode) {
229031 int iRes = -1;
229032 int iHi;
229033 int iLo;
229034 int ret;
229035 u16 iKey;
229036
229037 if( iCode>=(1<<20) ){
229038 return 0;
229039 }
229040 iLo = aFts5UnicodeBlock[(iCode>>16)];
229041 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
229042 iKey = (iCode & 0xFFFF);
229043 while( iHi>iLo ){
229044 int iTest = (iHi + iLo) / 2;
229045 assert( iTest>=iLo && iTest<iHi );
229046 if( iKey>=aFts5UnicodeMap[iTest] ){
229047 iRes = iTest;
229048 iLo = iTest+1;
229049 }else{
229050 iHi = iTest;
229051 }
229052 }
229053
229054 if( iRes<0 ) return 0;
229055 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
229056 ret = aFts5UnicodeData[iRes] & 0x1F;
229057 if( ret!=30 ) return ret;
229058 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
229059}
229060
229061static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
229062 int i = 0;
229063 int iTbl = 0;
229064 while( i<128 ){
229065 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
229066 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
229067 for(; i<128 && i<n; i++){
229068 aAscii[i] = (u8)bToken;
229069 }
229070 iTbl++;
229071 }
229072}
229073
229074/*
229075** 2015 May 30
229076**
229077** The author disclaims copyright to this source code. In place of
229078** a legal notice, here is a blessing:
229079**
229080** May you do good and not evil.
229081** May you find forgiveness for yourself and forgive others.
229082** May you share freely, never taking more than you give.
229083**
229084******************************************************************************
229085**
229086** Routines for varint serialization and deserialization.
229087*/
229088
229089
229090/* #include "fts5Int.h" */
229091
229092/*
229093** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
229094** Except, this version does handle the single byte case that the core
229095** version depends on being handled before its function is called.
229096*/
229097static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
229098 u32 a,b;
229099
229100 /* The 1-byte case. Overwhelmingly the most common. */
229101 a = *p;
229102 /* a: p0 (unmasked) */
229103 if (!(a&0x80))
229104 {
229105 /* Values between 0 and 127 */
229106 *v = a;
229107 return 1;
229108 }
229109
229110 /* The 2-byte case */
229111 p++;
229112 b = *p;
229113 /* b: p1 (unmasked) */
229114 if (!(b&0x80))
229115 {
229116 /* Values between 128 and 16383 */
229117 a &= 0x7f;
229118 a = a<<7;
229119 *v = a | b;
229120 return 2;
229121 }
229122
229123 /* The 3-byte case */
229124 p++;
229125 a = a<<14;
229126 a |= *p;
229127 /* a: p0<<14 | p2 (unmasked) */
229128 if (!(a&0x80))
229129 {
229130 /* Values between 16384 and 2097151 */
229131 a &= (0x7f<<14)|(0x7f);
229132 b &= 0x7f;
229133 b = b<<7;
229134 *v = a | b;
229135 return 3;
229136 }
229137
229138 /* A 32-bit varint is used to store size information in btrees.
229139 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
229140 ** A 3-byte varint is sufficient, for example, to record the size
229141 ** of a 1048569-byte BLOB or string.
229142 **
229143 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
229144 ** rare larger cases can be handled by the slower 64-bit varint
229145 ** routine.
229146 */
229147 {
229148 u64 v64;
229149 u8 n;
229150 p -= 2;
229151 n = sqlite3Fts5GetVarint(p, &v64);
229152 *v = ((u32)v64) & 0x7FFFFFFF;
229153 assert( n>3 && n<=9 );
229154 return n;
229155 }
229156}
229157
229158
229159/*
229160** Bitmasks used by sqlite3GetVarint(). These precomputed constants
229161** are defined here rather than simply putting the constant expressions
229162** inline in order to work around bugs in the RVT compiler.
229163**
229164** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
229165**
229166** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
229167*/
229168#define SLOT_2_0 0x001fc07f
229169#define SLOT_4_2_0 0xf01fc07f
229170
229171/*
229172** Read a 64-bit variable-length integer from memory starting at p[0].
229173** Return the number of bytes read. The value is stored in *v.
229174*/
229175static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
229176 u32 a,b,s;
229177
229178 a = *p;
229179 /* a: p0 (unmasked) */
229180 if (!(a&0x80))
229181 {
229182 *v = a;
229183 return 1;
229184 }
229185
229186 p++;
229187 b = *p;
229188 /* b: p1 (unmasked) */
229189 if (!(b&0x80))
229190 {
229191 a &= 0x7f;
229192 a = a<<7;
229193 a |= b;
229194 *v = a;
229195 return 2;
229196 }
229197
229198 /* Verify that constants are precomputed correctly */
229199 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
229200 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
229201
229202 p++;
229203 a = a<<14;
229204 a |= *p;
229205 /* a: p0<<14 | p2 (unmasked) */
229206 if (!(a&0x80))
229207 {
229208 a &= SLOT_2_0;
229209 b &= 0x7f;
229210 b = b<<7;
229211 a |= b;
229212 *v = a;
229213 return 3;
229214 }
229215
229216 /* CSE1 from below */
229217 a &= SLOT_2_0;
229218 p++;
229219 b = b<<14;
229220 b |= *p;
229221 /* b: p1<<14 | p3 (unmasked) */
229222 if (!(b&0x80))
229223 {
229224 b &= SLOT_2_0;
229225 /* moved CSE1 up */
229226 /* a &= (0x7f<<14)|(0x7f); */
229227 a = a<<7;
229228 a |= b;
229229 *v = a;
229230 return 4;
229231 }
229232
229233 /* a: p0<<14 | p2 (masked) */
229234 /* b: p1<<14 | p3 (unmasked) */
229235 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
229236 /* moved CSE1 up */
229237 /* a &= (0x7f<<14)|(0x7f); */
229238 b &= SLOT_2_0;
229239 s = a;
229240 /* s: p0<<14 | p2 (masked) */
229241
229242 p++;
229243 a = a<<14;
229244 a |= *p;
229245 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
229246 if (!(a&0x80))
229247 {
229248 /* we can skip these cause they were (effectively) done above in calc'ing s */
229249 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
229250 /* b &= (0x7f<<14)|(0x7f); */
229251 b = b<<7;
229252 a |= b;
229253 s = s>>18;
229254 *v = ((u64)s)<<32 | a;
229255 return 5;
229256 }
229257
229258 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
229259 s = s<<7;
229260 s |= b;
229261 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
229262
229263 p++;
229264 b = b<<14;
229265 b |= *p;
229266 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
229267 if (!(b&0x80))
229268 {
229269 /* we can skip this cause it was (effectively) done above in calc'ing s */
229270 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
229271 a &= SLOT_2_0;
229272 a = a<<7;
229273 a |= b;
229274 s = s>>18;
229275 *v = ((u64)s)<<32 | a;
229276 return 6;
229277 }
229278
229279 p++;
229280 a = a<<14;
229281 a |= *p;
229282 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
229283 if (!(a&0x80))
229284 {
229285 a &= SLOT_4_2_0;
229286 b &= SLOT_2_0;
229287 b = b<<7;
229288 a |= b;
229289 s = s>>11;
229290 *v = ((u64)s)<<32 | a;
229291 return 7;
229292 }
229293
229294 /* CSE2 from below */
229295 a &= SLOT_2_0;
229296 p++;
229297 b = b<<14;
229298 b |= *p;
229299 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
229300 if (!(b&0x80))
229301 {
229302 b &= SLOT_4_2_0;
229303 /* moved CSE2 up */
229304 /* a &= (0x7f<<14)|(0x7f); */
229305 a = a<<7;
229306 a |= b;
229307 s = s>>4;
229308 *v = ((u64)s)<<32 | a;
229309 return 8;
229310 }
229311
229312 p++;
229313 a = a<<15;
229314 a |= *p;
229315 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
229316
229317 /* moved CSE2 up */
229318 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
229319 b &= SLOT_2_0;
229320 b = b<<8;
229321 a |= b;
229322
229323 s = s<<4;
229324 b = p[-4];
229325 b &= 0x7f;
229326 b = b>>3;
229327 s |= b;
229328
229329 *v = ((u64)s)<<32 | a;
229330
229331 return 9;
229332}
229333
229334/*
229335** The variable-length integer encoding is as follows:
229336**
229337** KEY:
229338** A = 0xxxxxxx 7 bits of data and one flag bit
229339** B = 1xxxxxxx 7 bits of data and one flag bit
229340** C = xxxxxxxx 8 bits of data
229341**
229342** 7 bits - A
229343** 14 bits - BA
229344** 21 bits - BBA
229345** 28 bits - BBBA
229346** 35 bits - BBBBA
229347** 42 bits - BBBBBA
229348** 49 bits - BBBBBBA
229349** 56 bits - BBBBBBBA
229350** 64 bits - BBBBBBBBC
229351*/
229352
229353#ifdef SQLITE_NOINLINE
229354# define FTS5_NOINLINE SQLITE_NOINLINE
229355#else
229356# define FTS5_NOINLINE
229357#endif
229358
229359/*
229360** Write a 64-bit variable-length integer to memory starting at p[0].
229361** The length of data write will be between 1 and 9 bytes. The number
229362** of bytes written is returned.
229363**
229364** A variable-length integer consists of the lower 7 bits of each byte
229365** for all bytes that have the 8th bit set and one byte with the 8th
229366** bit clear. Except, if we get to the 9th byte, it stores the full
229367** 8 bits and is the last byte.
229368*/
229369static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
229370 int i, j, n;
229371 u8 buf[10];
229372 if( v & (((u64)0xff000000)<<32) ){
229373 p[8] = (u8)v;
229374 v >>= 8;
229375 for(i=7; i>=0; i--){
229376 p[i] = (u8)((v & 0x7f) | 0x80);
229377 v >>= 7;
229378 }
229379 return 9;
229380 }
229381 n = 0;
229382 do{
229383 buf[n++] = (u8)((v & 0x7f) | 0x80);
229384 v >>= 7;
229385 }while( v!=0 );
229386 buf[0] &= 0x7f;
229387 assert( n<=9 );
229388 for(i=0, j=n-1; j>=0; j--, i++){
229389 p[i] = buf[j];
229390 }
229391 return n;
229392}
229393
229394static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
229395 if( v<=0x7f ){
229396 p[0] = v&0x7f;
229397 return 1;
229398 }
229399 if( v<=0x3fff ){
229400 p[0] = ((v>>7)&0x7f)|0x80;
229401 p[1] = v&0x7f;
229402 return 2;
229403 }
229404 return fts5PutVarint64(p,v);
229405}
229406
229407
229408static int sqlite3Fts5GetVarintLen(u32 iVal){
229409#if 0
229410 if( iVal<(1 << 7 ) ) return 1;
229411#endif
229412 assert( iVal>=(1 << 7) );
229413 if( iVal<(1 << 14) ) return 2;
229414 if( iVal<(1 << 21) ) return 3;
229415 if( iVal<(1 << 28) ) return 4;
229416 return 5;
229417}
229418
229419/*
229420** 2015 May 08
229421**
229422** The author disclaims copyright to this source code. In place of
229423** a legal notice, here is a blessing:
229424**
229425** May you do good and not evil.
229426** May you find forgiveness for yourself and forgive others.
229427** May you share freely, never taking more than you give.
229428**
229429******************************************************************************
229430**
229431** This is an SQLite virtual table module implementing direct access to an
229432** existing FTS5 index. The module may create several different types of
229433** tables:
229434**
229435** col:
229436** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
229437**
229438** One row for each term/column combination. The value of $doc is set to
229439** the number of fts5 rows that contain at least one instance of term
229440** $term within column $col. Field $cnt is set to the total number of
229441** instances of term $term in column $col (in any row of the fts5 table).
229442**
229443** row:
229444** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
229445**
229446** One row for each term in the database. The value of $doc is set to
229447** the number of fts5 rows that contain at least one instance of term
229448** $term. Field $cnt is set to the total number of instances of term
229449** $term in the database.
229450**
229451** instance:
229452** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
229453**
229454** One row for each term instance in the database.
229455*/
229456
229457
229458/* #include "fts5Int.h" */
229459
229460
229461typedef struct Fts5VocabTable Fts5VocabTable;
229462typedef struct Fts5VocabCursor Fts5VocabCursor;
229463
229464struct Fts5VocabTable {
229465 sqlite3_vtab base;
229466 char *zFts5Tbl; /* Name of fts5 table */
229467 char *zFts5Db; /* Db containing fts5 table */
229468 sqlite3 *db; /* Database handle */
229469 Fts5Global *pGlobal; /* FTS5 global object for this database */
229470 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
229471 unsigned bBusy; /* True if busy */
229472};
229473
229474struct Fts5VocabCursor {
229475 sqlite3_vtab_cursor base;
229476 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
229477 Fts5Table *pFts5; /* Associated FTS5 table */
229478
229479 int bEof; /* True if this cursor is at EOF */
229480 Fts5IndexIter *pIter; /* Term/rowid iterator object */
229481
229482 int nLeTerm; /* Size of zLeTerm in bytes */
229483 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
229484
229485 /* These are used by 'col' tables only */
229486 int iCol;
229487 i64 *aCnt;
229488 i64 *aDoc;
229489
229490 /* Output values used by all tables. */
229491 i64 rowid; /* This table's current rowid value */
229492 Fts5Buffer term; /* Current value of 'term' column */
229493
229494 /* Output values Used by 'instance' tables only */
229495 i64 iInstPos;
229496 int iInstOff;
229497};
229498
229499#define FTS5_VOCAB_COL 0
229500#define FTS5_VOCAB_ROW 1
229501#define FTS5_VOCAB_INSTANCE 2
229502
229503#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
229504#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
229505#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
229506
229507/*
229508** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
229509*/
229510#define FTS5_VOCAB_TERM_EQ 0x01
229511#define FTS5_VOCAB_TERM_GE 0x02
229512#define FTS5_VOCAB_TERM_LE 0x04
229513
229514
229515/*
229516** Translate a string containing an fts5vocab table type to an
229517** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
229518** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
229519** and return SQLITE_ERROR.
229520*/
229521static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
229522 int rc = SQLITE_OK;
229523 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
229524 if( rc==SQLITE_OK ){
229525 sqlite3Fts5Dequote(zCopy);
229526 if( sqlite3_stricmp(zCopy, "col")==0 ){
229527 *peType = FTS5_VOCAB_COL;
229528 }else
229529
229530 if( sqlite3_stricmp(zCopy, "row")==0 ){
229531 *peType = FTS5_VOCAB_ROW;
229532 }else
229533 if( sqlite3_stricmp(zCopy, "instance")==0 ){
229534 *peType = FTS5_VOCAB_INSTANCE;
229535 }else
229536 {
229537 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
229538 rc = SQLITE_ERROR;
229539 }
229540 sqlite3_free(zCopy);
229541 }
229542
229543 return rc;
229544}
229545
229546
229547/*
229548** The xDisconnect() virtual table method.
229549*/
229550static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
229551 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
229552 sqlite3_free(pTab);
229553 return SQLITE_OK;
229554}
229555
229556/*
229557** The xDestroy() virtual table method.
229558*/
229559static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
229560 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
229561 sqlite3_free(pTab);
229562 return SQLITE_OK;
229563}
229564
229565/*
229566** This function is the implementation of both the xConnect and xCreate
229567** methods of the FTS3 virtual table.
229568**
229569** The argv[] array contains the following:
229570**
229571** argv[0] -> module name ("fts5vocab")
229572** argv[1] -> database name
229573** argv[2] -> table name
229574**
229575** then:
229576**
229577** argv[3] -> name of fts5 table
229578** argv[4] -> type of fts5vocab table
229579**
229580** or, for tables in the TEMP schema only.
229581**
229582** argv[3] -> name of fts5 tables database
229583** argv[4] -> name of fts5 table
229584** argv[5] -> type of fts5vocab table
229585*/
229586static int fts5VocabInitVtab(
229587 sqlite3 *db, /* The SQLite database connection */
229588 void *pAux, /* Pointer to Fts5Global object */
229589 int argc, /* Number of elements in argv array */
229590 const char * const *argv, /* xCreate/xConnect argument array */
229591 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
229592 char **pzErr /* Write any error message here */
229593){
229594 const char *azSchema[] = {
229595 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
229596 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
229597 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
229598 };
229599
229600 Fts5VocabTable *pRet = 0;
229601 int rc = SQLITE_OK; /* Return code */
229602 int bDb;
229603
229604 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
229605
229606 if( argc!=5 && bDb==0 ){
229607 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
229608 rc = SQLITE_ERROR;
229609 }else{
229610 int nByte; /* Bytes of space to allocate */
229611 const char *zDb = bDb ? argv[3] : argv[1];
229612 const char *zTab = bDb ? argv[4] : argv[3];
229613 const char *zType = bDb ? argv[5] : argv[4];
229614 int nDb = (int)strlen(zDb)+1;
229615 int nTab = (int)strlen(zTab)+1;
229616 int eType = 0;
229617
229618 rc = fts5VocabTableType(zType, pzErr, &eType);
229619 if( rc==SQLITE_OK ){
229620 assert( eType>=0 && eType<ArraySize(azSchema) );
229621 rc = sqlite3_declare_vtab(db, azSchema[eType]);
229622 }
229623
229624 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
229625 pRet = sqlite3Fts5MallocZero(&rc, nByte);
229626 if( pRet ){
229627 pRet->pGlobal = (Fts5Global*)pAux;
229628 pRet->eType = eType;
229629 pRet->db = db;
229630 pRet->zFts5Tbl = (char*)&pRet[1];
229631 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
229632 memcpy(pRet->zFts5Tbl, zTab, nTab);
229633 memcpy(pRet->zFts5Db, zDb, nDb);
229634 sqlite3Fts5Dequote(pRet->zFts5Tbl);
229635 sqlite3Fts5Dequote(pRet->zFts5Db);
229636 }
229637 }
229638
229639 *ppVTab = (sqlite3_vtab*)pRet;
229640 return rc;
229641}
229642
229643
229644/*
229645** The xConnect() and xCreate() methods for the virtual table. All the
229646** work is done in function fts5VocabInitVtab().
229647*/
229648static int fts5VocabConnectMethod(
229649 sqlite3 *db, /* Database connection */
229650 void *pAux, /* Pointer to tokenizer hash table */
229651 int argc, /* Number of elements in argv array */
229652 const char * const *argv, /* xCreate/xConnect argument array */
229653 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
229654 char **pzErr /* OUT: sqlite3_malloc'd error message */
229655){
229656 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
229657}
229658static int fts5VocabCreateMethod(
229659 sqlite3 *db, /* Database connection */
229660 void *pAux, /* Pointer to tokenizer hash table */
229661 int argc, /* Number of elements in argv array */
229662 const char * const *argv, /* xCreate/xConnect argument array */
229663 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
229664 char **pzErr /* OUT: sqlite3_malloc'd error message */
229665){
229666 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
229667}
229668
229669/*
229670** Implementation of the xBestIndex method.
229671**
229672** Only constraints of the form:
229673**
229674** term <= ?
229675** term == ?
229676** term >= ?
229677**
229678** are interpreted. Less-than and less-than-or-equal are treated
229679** identically, as are greater-than and greater-than-or-equal.
229680*/
229681static int fts5VocabBestIndexMethod(
229682 sqlite3_vtab *pUnused,
229683 sqlite3_index_info *pInfo
229684){
229685 int i;
229686 int iTermEq = -1;
229687 int iTermGe = -1;
229688 int iTermLe = -1;
229689 int idxNum = 0;
229690 int nArg = 0;
229691
229692 UNUSED_PARAM(pUnused);
229693
229694 for(i=0; i<pInfo->nConstraint; i++){
229695 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
229696 if( p->usable==0 ) continue;
229697 if( p->iColumn==0 ){ /* term column */
229698 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
229699 if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
229700 if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
229701 if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
229702 if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
229703 }
229704 }
229705
229706 if( iTermEq>=0 ){
229707 idxNum |= FTS5_VOCAB_TERM_EQ;
229708 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
229709 pInfo->estimatedCost = 100;
229710 }else{
229711 pInfo->estimatedCost = 1000000;
229712 if( iTermGe>=0 ){
229713 idxNum |= FTS5_VOCAB_TERM_GE;
229714 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
229715 pInfo->estimatedCost = pInfo->estimatedCost / 2;
229716 }
229717 if( iTermLe>=0 ){
229718 idxNum |= FTS5_VOCAB_TERM_LE;
229719 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
229720 pInfo->estimatedCost = pInfo->estimatedCost / 2;
229721 }
229722 }
229723
229724 /* This virtual table always delivers results in ascending order of
229725 ** the "term" column (column 0). So if the user has requested this
229726 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
229727 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
229728 ** are already in sorted order. */
229729 if( pInfo->nOrderBy==1
229730 && pInfo->aOrderBy[0].iColumn==0
229731 && pInfo->aOrderBy[0].desc==0
229732 ){
229733 pInfo->orderByConsumed = 1;
229734 }
229735
229736 pInfo->idxNum = idxNum;
229737 return SQLITE_OK;
229738}
229739
229740/*
229741** Implementation of xOpen method.
229742*/
229743static int fts5VocabOpenMethod(
229744 sqlite3_vtab *pVTab,
229745 sqlite3_vtab_cursor **ppCsr
229746){
229747 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
229748 Fts5Table *pFts5 = 0;
229749 Fts5VocabCursor *pCsr = 0;
229750 int rc = SQLITE_OK;
229751 sqlite3_stmt *pStmt = 0;
229752 char *zSql = 0;
229753
229754 if( pTab->bBusy ){
229755 pVTab->zErrMsg = sqlite3_mprintf(
229756 "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
229757 );
229758 return SQLITE_ERROR;
229759 }
229760 zSql = sqlite3Fts5Mprintf(&rc,
229761 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
229762 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
229763 );
229764 if( zSql ){
229765 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
229766 }
229767 sqlite3_free(zSql);
229768 assert( rc==SQLITE_OK || pStmt==0 );
229769 if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
229770
229771 pTab->bBusy = 1;
229772 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
229773 i64 iId = sqlite3_column_int64(pStmt, 0);
229774 pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
229775 }
229776 pTab->bBusy = 0;
229777
229778 if( rc==SQLITE_OK ){
229779 if( pFts5==0 ){
229780 rc = sqlite3_finalize(pStmt);
229781 pStmt = 0;
229782 if( rc==SQLITE_OK ){
229783 pVTab->zErrMsg = sqlite3_mprintf(
229784 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
229785 );
229786 rc = SQLITE_ERROR;
229787 }
229788 }else{
229789 rc = sqlite3Fts5FlushToDisk(pFts5);
229790 }
229791 }
229792
229793 if( rc==SQLITE_OK ){
229794 int nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
229795 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
229796 }
229797
229798 if( pCsr ){
229799 pCsr->pFts5 = pFts5;
229800 pCsr->pStmt = pStmt;
229801 pCsr->aCnt = (i64*)&pCsr[1];
229802 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
229803 }else{
229804 sqlite3_finalize(pStmt);
229805 }
229806
229807 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
229808 return rc;
229809}
229810
229811static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
229812 pCsr->rowid = 0;
229813 sqlite3Fts5IterClose(pCsr->pIter);
229814 pCsr->pIter = 0;
229815 sqlite3_free(pCsr->zLeTerm);
229816 pCsr->nLeTerm = -1;
229817 pCsr->zLeTerm = 0;
229818 pCsr->bEof = 0;
229819}
229820
229821/*
229822** Close the cursor. For additional information see the documentation
229823** on the xClose method of the virtual table interface.
229824*/
229825static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
229826 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229827 fts5VocabResetCursor(pCsr);
229828 sqlite3Fts5BufferFree(&pCsr->term);
229829 sqlite3_finalize(pCsr->pStmt);
229830 sqlite3_free(pCsr);
229831 return SQLITE_OK;
229832}
229833
229834static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
229835 int rc = SQLITE_OK;
229836
229837 if( sqlite3Fts5IterEof(pCsr->pIter) ){
229838 pCsr->bEof = 1;
229839 }else{
229840 const char *zTerm;
229841 int nTerm;
229842 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229843 if( pCsr->nLeTerm>=0 ){
229844 int nCmp = MIN(nTerm, pCsr->nLeTerm);
229845 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
229846 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
229847 pCsr->bEof = 1;
229848 }
229849 }
229850
229851 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
229852 }
229853 return rc;
229854}
229855
229856static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
229857 int eDetail = pCsr->pFts5->pConfig->eDetail;
229858 int rc = SQLITE_OK;
229859 Fts5IndexIter *pIter = pCsr->pIter;
229860 i64 *pp = &pCsr->iInstPos;
229861 int *po = &pCsr->iInstOff;
229862
229863 assert( sqlite3Fts5IterEof(pIter)==0 );
229864 assert( pCsr->bEof==0 );
229865 while( eDetail==FTS5_DETAIL_NONE
229866 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
229867 ){
229868 pCsr->iInstPos = 0;
229869 pCsr->iInstOff = 0;
229870
229871 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
229872 if( rc==SQLITE_OK ){
229873 rc = fts5VocabInstanceNewTerm(pCsr);
229874 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
229875 }
229876 if( rc ){
229877 pCsr->bEof = 1;
229878 break;
229879 }
229880 }
229881
229882 return rc;
229883}
229884
229885/*
229886** Advance the cursor to the next row in the table.
229887*/
229888static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
229889 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
229890 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
229891 int rc = SQLITE_OK;
229892 int nCol = pCsr->pFts5->pConfig->nCol;
229893
229894 pCsr->rowid++;
229895
229896 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
229897 return fts5VocabInstanceNext(pCsr);
229898 }
229899
229900 if( pTab->eType==FTS5_VOCAB_COL ){
229901 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
229902 if( pCsr->aDoc[pCsr->iCol] ) break;
229903 }
229904 }
229905
229906 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
229907 if( sqlite3Fts5IterEof(pCsr->pIter) ){
229908 pCsr->bEof = 1;
229909 }else{
229910 const char *zTerm;
229911 int nTerm;
229912
229913 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229914 assert( nTerm>=0 );
229915 if( pCsr->nLeTerm>=0 ){
229916 int nCmp = MIN(nTerm, pCsr->nLeTerm);
229917 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
229918 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
229919 pCsr->bEof = 1;
229920 return SQLITE_OK;
229921 }
229922 }
229923
229924 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
229925 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
229926 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
229927 pCsr->iCol = 0;
229928
229929 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
229930 while( rc==SQLITE_OK ){
229931 int eDetail = pCsr->pFts5->pConfig->eDetail;
229932 const u8 *pPos; int nPos; /* Position list */
229933 i64 iPos = 0; /* 64-bit position read from poslist */
229934 int iOff = 0; /* Current offset within position list */
229935
229936 pPos = pCsr->pIter->pData;
229937 nPos = pCsr->pIter->nData;
229938
229939 switch( pTab->eType ){
229940 case FTS5_VOCAB_ROW:
229941 if( eDetail==FTS5_DETAIL_FULL ){
229942 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
229943 pCsr->aCnt[0]++;
229944 }
229945 }
229946 pCsr->aDoc[0]++;
229947 break;
229948
229949 case FTS5_VOCAB_COL:
229950 if( eDetail==FTS5_DETAIL_FULL ){
229951 int iCol = -1;
229952 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
229953 int ii = FTS5_POS2COLUMN(iPos);
229954 if( iCol!=ii ){
229955 if( ii>=nCol ){
229956 rc = FTS5_CORRUPT;
229957 break;
229958 }
229959 pCsr->aDoc[ii]++;
229960 iCol = ii;
229961 }
229962 pCsr->aCnt[ii]++;
229963 }
229964 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
229965 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
229966 assert_nc( iPos>=0 && iPos<nCol );
229967 if( iPos>=nCol ){
229968 rc = FTS5_CORRUPT;
229969 break;
229970 }
229971 pCsr->aDoc[iPos]++;
229972 }
229973 }else{
229974 assert( eDetail==FTS5_DETAIL_NONE );
229975 pCsr->aDoc[0]++;
229976 }
229977 break;
229978
229979 default:
229980 assert( pTab->eType==FTS5_VOCAB_INSTANCE );
229981 break;
229982 }
229983
229984 if( rc==SQLITE_OK ){
229985 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
229986 }
229987 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
229988
229989 if( rc==SQLITE_OK ){
229990 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
229991 if( nTerm!=pCsr->term.n
229992 || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
229993 ){
229994 break;
229995 }
229996 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
229997 }
229998 }
229999 }
230000 }
230001
230002 if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
230003 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
230004 if( pCsr->iCol==nCol ){
230005 rc = FTS5_CORRUPT;
230006 }
230007 }
230008 return rc;
230009}
230010
230011/*
230012** This is the xFilter implementation for the virtual table.
230013*/
230014static int fts5VocabFilterMethod(
230015 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
230016 int idxNum, /* Strategy index */
230017 const char *zUnused, /* Unused */
230018 int nUnused, /* Number of elements in apVal */
230019 sqlite3_value **apVal /* Arguments for the indexing scheme */
230020){
230021 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
230022 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
230023 int eType = pTab->eType;
230024 int rc = SQLITE_OK;
230025
230026 int iVal = 0;
230027 int f = FTS5INDEX_QUERY_SCAN;
230028 const char *zTerm = 0;
230029 int nTerm = 0;
230030
230031 sqlite3_value *pEq = 0;
230032 sqlite3_value *pGe = 0;
230033 sqlite3_value *pLe = 0;
230034
230035 UNUSED_PARAM2(zUnused, nUnused);
230036
230037 fts5VocabResetCursor(pCsr);
230038 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
230039 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
230040 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
230041
230042 if( pEq ){
230043 zTerm = (const char *)sqlite3_value_text(pEq);
230044 nTerm = sqlite3_value_bytes(pEq);
230045 f = 0;
230046 }else{
230047 if( pGe ){
230048 zTerm = (const char *)sqlite3_value_text(pGe);
230049 nTerm = sqlite3_value_bytes(pGe);
230050 }
230051 if( pLe ){
230052 const char *zCopy = (const char *)sqlite3_value_text(pLe);
230053 if( zCopy==0 ) zCopy = "";
230054 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
230055 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
230056 if( pCsr->zLeTerm==0 ){
230057 rc = SQLITE_NOMEM;
230058 }else{
230059 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
230060 }
230061 }
230062 }
230063
230064 if( rc==SQLITE_OK ){
230065 Fts5Index *pIndex = pCsr->pFts5->pIndex;
230066 rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
230067 }
230068 if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
230069 rc = fts5VocabInstanceNewTerm(pCsr);
230070 }
230071 if( rc==SQLITE_OK && !pCsr->bEof
230072 && (eType!=FTS5_VOCAB_INSTANCE
230073 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
230074 ){
230075 rc = fts5VocabNextMethod(pCursor);
230076 }
230077
230078 return rc;
230079}
230080
230081/*
230082** This is the xEof method of the virtual table. SQLite calls this
230083** routine to find out if it has reached the end of a result set.
230084*/
230085static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
230086 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
230087 return pCsr->bEof;
230088}
230089
230090static int fts5VocabColumnMethod(
230091 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
230092 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
230093 int iCol /* Index of column to read value from */
230094){
230095 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
230096 int eDetail = pCsr->pFts5->pConfig->eDetail;
230097 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
230098 i64 iVal = 0;
230099
230100 if( iCol==0 ){
230101 sqlite3_result_text(
230102 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT
230103 );
230104 }else if( eType==FTS5_VOCAB_COL ){
230105 assert( iCol==1 || iCol==2 || iCol==3 );
230106 if( iCol==1 ){
230107 if( eDetail!=FTS5_DETAIL_NONE ){
230108 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
230109 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
230110 }
230111 }else if( iCol==2 ){
230112 iVal = pCsr->aDoc[pCsr->iCol];
230113 }else{
230114 iVal = pCsr->aCnt[pCsr->iCol];
230115 }
230116 }else if( eType==FTS5_VOCAB_ROW ){
230117 assert( iCol==1 || iCol==2 );
230118 if( iCol==1 ){
230119 iVal = pCsr->aDoc[0];
230120 }else{
230121 iVal = pCsr->aCnt[0];
230122 }
230123 }else{
230124 assert( eType==FTS5_VOCAB_INSTANCE );
230125 switch( iCol ){
230126 case 1:
230127 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
230128 break;
230129 case 2: {
230130 int ii = -1;
230131 if( eDetail==FTS5_DETAIL_FULL ){
230132 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
230133 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
230134 ii = (int)pCsr->iInstPos;
230135 }
230136 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
230137 const char *z = pCsr->pFts5->pConfig->azCol[ii];
230138 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
230139 }
230140 break;
230141 }
230142 default: {
230143 assert( iCol==3 );
230144 if( eDetail==FTS5_DETAIL_FULL ){
230145 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
230146 sqlite3_result_int(pCtx, ii);
230147 }
230148 break;
230149 }
230150 }
230151 }
230152
230153 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
230154 return SQLITE_OK;
230155}
230156
230157/*
230158** This is the xRowid method. The SQLite core calls this routine to
230159** retrieve the rowid for the current row of the result set. The
230160** rowid should be written to *pRowid.
230161*/
230162static int fts5VocabRowidMethod(
230163 sqlite3_vtab_cursor *pCursor,
230164 sqlite_int64 *pRowid
230165){
230166 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
230167 *pRowid = pCsr->rowid;
230168 return SQLITE_OK;
230169}
230170
230171static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
230172 static const sqlite3_module fts5Vocab = {
230173 /* iVersion */ 2,
230174 /* xCreate */ fts5VocabCreateMethod,
230175 /* xConnect */ fts5VocabConnectMethod,
230176 /* xBestIndex */ fts5VocabBestIndexMethod,
230177 /* xDisconnect */ fts5VocabDisconnectMethod,
230178 /* xDestroy */ fts5VocabDestroyMethod,
230179 /* xOpen */ fts5VocabOpenMethod,
230180 /* xClose */ fts5VocabCloseMethod,
230181 /* xFilter */ fts5VocabFilterMethod,
230182 /* xNext */ fts5VocabNextMethod,
230183 /* xEof */ fts5VocabEofMethod,
230184 /* xColumn */ fts5VocabColumnMethod,
230185 /* xRowid */ fts5VocabRowidMethod,
230186 /* xUpdate */ 0,
230187 /* xBegin */ 0,
230188 /* xSync */ 0,
230189 /* xCommit */ 0,
230190 /* xRollback */ 0,
230191 /* xFindFunction */ 0,
230192 /* xRename */ 0,
230193 /* xSavepoint */ 0,
230194 /* xRelease */ 0,
230195 /* xRollbackTo */ 0,
230196 /* xShadowName */ 0
230197 };
230198 void *p = (void*)pGlobal;
230199
230200 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
230201}
230202
230203
230204
230205#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
230206
230207/************** End of fts5.c ************************************************/
230208/************** Begin file stmt.c ********************************************/
230209/*
230210** 2017-05-31
230211**
230212** The author disclaims copyright to this source code. In place of
230213** a legal notice, here is a blessing:
230214**
230215** May you do good and not evil.
230216** May you find forgiveness for yourself and forgive others.
230217** May you share freely, never taking more than you give.
230218**
230219*************************************************************************
230220**
230221** This file demonstrates an eponymous virtual table that returns information
230222** about all prepared statements for the database connection.
230223**
230224** Usage example:
230225**
230226** .load ./stmt
230227** .mode line
230228** .header on
230229** SELECT * FROM stmt;
230230*/
230231#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
230232#if !defined(SQLITEINT_H)
230233/* #include "sqlite3ext.h" */
230234#endif
230235SQLITE_EXTENSION_INIT1
230236/* #include <assert.h> */
230237/* #include <string.h> */
230238
230239#ifndef SQLITE_OMIT_VIRTUALTABLE
230240
230241/* stmt_vtab is a subclass of sqlite3_vtab which will
230242** serve as the underlying representation of a stmt virtual table
230243*/
230244typedef struct stmt_vtab stmt_vtab;
230245struct stmt_vtab {
230246 sqlite3_vtab base; /* Base class - must be first */
230247 sqlite3 *db; /* Database connection for this stmt vtab */
230248};
230249
230250/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
230251** serve as the underlying representation of a cursor that scans
230252** over rows of the result
230253*/
230254typedef struct stmt_cursor stmt_cursor;
230255struct stmt_cursor {
230256 sqlite3_vtab_cursor base; /* Base class - must be first */
230257 sqlite3 *db; /* Database connection for this cursor */
230258 sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
230259 sqlite3_int64 iRowid; /* The rowid */
230260};
230261
230262/*
230263** The stmtConnect() method is invoked to create a new
230264** stmt_vtab that describes the stmt virtual table.
230265**
230266** Think of this routine as the constructor for stmt_vtab objects.
230267**
230268** All this routine needs to do is:
230269**
230270** (1) Allocate the stmt_vtab object and initialize all fields.
230271**
230272** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
230273** result set of queries against stmt will look like.
230274*/
230275static int stmtConnect(
230276 sqlite3 *db,
230277 void *pAux,
230278 int argc, const char *const*argv,
230279 sqlite3_vtab **ppVtab,
230280 char **pzErr
230281){
230282 stmt_vtab *pNew;
230283 int rc;
230284
230285/* Column numbers */
230286#define STMT_COLUMN_SQL 0 /* SQL for the statement */
230287#define STMT_COLUMN_NCOL 1 /* Number of result columns */
230288#define STMT_COLUMN_RO 2 /* True if read-only */
230289#define STMT_COLUMN_BUSY 3 /* True if currently busy */
230290#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
230291#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
230292#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
230293#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
230294#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
230295#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
230296#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
230297
230298
230299 rc = sqlite3_declare_vtab(db,
230300 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
230301 "reprep,run,mem)");
230302 if( rc==SQLITE_OK ){
230303 pNew = sqlite3_malloc( sizeof(*pNew) );
230304 *ppVtab = (sqlite3_vtab*)pNew;
230305 if( pNew==0 ) return SQLITE_NOMEM;
230306 memset(pNew, 0, sizeof(*pNew));
230307 pNew->db = db;
230308 }
230309 return rc;
230310}
230311
230312/*
230313** This method is the destructor for stmt_cursor objects.
230314*/
230315static int stmtDisconnect(sqlite3_vtab *pVtab){
230316 sqlite3_free(pVtab);
230317 return SQLITE_OK;
230318}
230319
230320/*
230321** Constructor for a new stmt_cursor object.
230322*/
230323static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
230324 stmt_cursor *pCur;
230325 pCur = sqlite3_malloc( sizeof(*pCur) );
230326 if( pCur==0 ) return SQLITE_NOMEM;
230327 memset(pCur, 0, sizeof(*pCur));
230328 pCur->db = ((stmt_vtab*)p)->db;
230329 *ppCursor = &pCur->base;
230330 return SQLITE_OK;
230331}
230332
230333/*
230334** Destructor for a stmt_cursor.
230335*/
230336static int stmtClose(sqlite3_vtab_cursor *cur){
230337 sqlite3_free(cur);
230338 return SQLITE_OK;
230339}
230340
230341
230342/*
230343** Advance a stmt_cursor to its next row of output.
230344*/
230345static int stmtNext(sqlite3_vtab_cursor *cur){
230346 stmt_cursor *pCur = (stmt_cursor*)cur;
230347 pCur->iRowid++;
230348 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
230349 return SQLITE_OK;
230350}
230351
230352/*
230353** Return values of columns for the row at which the stmt_cursor
230354** is currently pointing.
230355*/
230356static int stmtColumn(
230357 sqlite3_vtab_cursor *cur, /* The cursor */
230358 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
230359 int i /* Which column to return */
230360){
230361 stmt_cursor *pCur = (stmt_cursor*)cur;
230362 switch( i ){
230363 case STMT_COLUMN_SQL: {
230364 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
230365 break;
230366 }
230367 case STMT_COLUMN_NCOL: {
230368 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
230369 break;
230370 }
230371 case STMT_COLUMN_RO: {
230372 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
230373 break;
230374 }
230375 case STMT_COLUMN_BUSY: {
230376 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
230377 break;
230378 }
230379 default: {
230380 assert( i==STMT_COLUMN_MEM );
230381 i = SQLITE_STMTSTATUS_MEMUSED +
230382 STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
230383 /* Fall thru */
230384 }
230385 case STMT_COLUMN_NSCAN:
230386 case STMT_COLUMN_NSORT:
230387 case STMT_COLUMN_NAIDX:
230388 case STMT_COLUMN_NSTEP:
230389 case STMT_COLUMN_REPREP:
230390 case STMT_COLUMN_RUN: {
230391 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
230392 i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
230393 break;
230394 }
230395 }
230396 return SQLITE_OK;
230397}
230398
230399/*
230400** Return the rowid for the current row. In this implementation, the
230401** rowid is the same as the output value.
230402*/
230403static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
230404 stmt_cursor *pCur = (stmt_cursor*)cur;
230405 *pRowid = pCur->iRowid;
230406 return SQLITE_OK;
230407}
230408
230409/*
230410** Return TRUE if the cursor has been moved off of the last
230411** row of output.
230412*/
230413static int stmtEof(sqlite3_vtab_cursor *cur){
230414 stmt_cursor *pCur = (stmt_cursor*)cur;
230415 return pCur->pStmt==0;
230416}
230417
230418/*
230419** This method is called to "rewind" the stmt_cursor object back
230420** to the first row of output. This method is always called at least
230421** once prior to any call to stmtColumn() or stmtRowid() or
230422** stmtEof().
230423*/
230424static int stmtFilter(
230425 sqlite3_vtab_cursor *pVtabCursor,
230426 int idxNum, const char *idxStr,
230427 int argc, sqlite3_value **argv
230428){
230429 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
230430 pCur->pStmt = 0;
230431 pCur->iRowid = 0;
230432 return stmtNext(pVtabCursor);
230433}
230434
230435/*
230436** SQLite will invoke this method one or more times while planning a query
230437** that uses the stmt virtual table. This routine needs to create
230438** a query plan for each invocation and compute an estimated cost for that
230439** plan.
230440*/
230441static int stmtBestIndex(
230442 sqlite3_vtab *tab,
230443 sqlite3_index_info *pIdxInfo
230444){
230445 pIdxInfo->estimatedCost = (double)500;
230446 pIdxInfo->estimatedRows = 500;
230447 return SQLITE_OK;
230448}
230449
230450/*
230451** This following structure defines all the methods for the
230452** stmt virtual table.
230453*/
230454static sqlite3_module stmtModule = {
230455 0, /* iVersion */
230456 0, /* xCreate */
230457 stmtConnect, /* xConnect */
230458 stmtBestIndex, /* xBestIndex */
230459 stmtDisconnect, /* xDisconnect */
230460 0, /* xDestroy */
230461 stmtOpen, /* xOpen - open a cursor */
230462 stmtClose, /* xClose - close a cursor */
230463 stmtFilter, /* xFilter - configure scan constraints */
230464 stmtNext, /* xNext - advance a cursor */
230465 stmtEof, /* xEof - check for end of scan */
230466 stmtColumn, /* xColumn - read data */
230467 stmtRowid, /* xRowid - read data */
230468 0, /* xUpdate */
230469 0, /* xBegin */
230470 0, /* xSync */
230471 0, /* xCommit */
230472 0, /* xRollback */
230473 0, /* xFindMethod */
230474 0, /* xRename */
230475 0, /* xSavepoint */
230476 0, /* xRelease */
230477 0, /* xRollbackTo */
230478 0, /* xShadowName */
230479};
230480
230481#endif /* SQLITE_OMIT_VIRTUALTABLE */
230482
230483SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
230484 int rc = SQLITE_OK;
230485#ifndef SQLITE_OMIT_VIRTUALTABLE
230486 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
230487#endif
230488 return rc;
230489}
230490
230491#ifndef SQLITE_CORE
230492#ifdef _WIN32
230493__declspec(dllexport)
230494#endif
230495SQLITE_API int sqlite3_stmt_init(
230496 sqlite3 *db,
230497 char **pzErrMsg,
230498 const sqlite3_api_routines *pApi
230499){
230500 int rc = SQLITE_OK;
230501 SQLITE_EXTENSION_INIT2(pApi);
230502#ifndef SQLITE_OMIT_VIRTUALTABLE
230503 rc = sqlite3StmtVtabInit(db);
230504#endif
230505 return rc;
230506}
230507#endif /* SQLITE_CORE */
230508#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
230509
230510/************** End of stmt.c ************************************************/
230511#if __LINE__!=230511
230512#undef SQLITE_SOURCE_ID
230513#define SQLITE_SOURCE_ID "2020-08-14 13:23:32 fca8dc8b578f215a969cd899336378966156154710873e68b3d9ac5881b0alt2"
230514#endif
230515/* Return the source-id for this library */
230516SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
230517/************************** End of sqlite3.c ******************************/
230518